// ***************************************************************************** // Copyright (C) 2024 EclipseSource GmbH. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** import { ChatResponseContent, CodeChatResponseContent, } from '@theia/ai-chat/lib/common'; import { ContributionProvider, UNTITLED_SCHEME, UntitledResourceResolver, URI } from '@theia/core'; import { generateUuid } from '@theia/core/lib/common/uuid'; import { ContextMenuRenderer, TreeNode } from '@theia/core/lib/browser'; import { ClipboardService } from '@theia/core/lib/browser/clipboard-service'; import { inject, injectable, named } from '@theia/core/shared/inversify'; import * as React from '@theia/core/shared/react'; import { ReactNode } from '@theia/core/shared/react'; import { nls } from '@theia/core/lib/common/nls'; import { Position } from '@theia/core/shared/vscode-languageserver-protocol'; import { EditorManager, EditorWidget } from '@theia/editor/lib/browser'; import { SimpleMonacoEditor } from '@theia/monaco/lib/browser/simple-monaco-editor'; import { MonacoEditorProvider } from '@theia/monaco/lib/browser/monaco-editor-provider'; import { MonacoLanguages } from '@theia/monaco/lib/browser/monaco-languages'; import { ChatResponsePartRenderer } from '../chat-response-part-renderer.js'; import { ChatViewTreeWidget, ResponseNode } from '../chat-tree-view/chat-view-tree-widget.js'; import { IMouseEvent } from '@theia/monaco-editor-core'; import { CodeWrapperEditors } from './code-wrapper-editors.js'; export const CodePartRendererAction = Symbol('CodePartRendererAction'); /** * The CodePartRenderer offers to contribute arbitrary React nodes to the rendered code part. * Technically anything can be rendered, however it is intended to be used for actions, like * "Copy to Clipboard" or "Insert at Cursor". */ export interface CodePartRendererAction { render(response: CodeChatResponseContent, parentNode: ResponseNode): ReactNode; /** * Determines if the action should be rendered for the given response. */ canRender?(response: CodeChatResponseContent, parentNode: ResponseNode): boolean; /** * The priority determines the order in which the actions are rendered. * The default priorities are 10 and 20. */ priority: number; } @injectable() export class CodePartRenderer implements ChatResponsePartRenderer { @inject(EditorManager) protected readonly editorManager: EditorManager; @inject(UntitledResourceResolver) protected readonly untitledResourceResolver: UntitledResourceResolver; @inject(MonacoEditorProvider) protected readonly editorProvider: MonacoEditorProvider; @inject(MonacoLanguages) protected readonly languageService: MonacoLanguages; @inject(ContextMenuRenderer) protected readonly contextMenuRenderer: ContextMenuRenderer; @inject(ContributionProvider) @named(CodePartRendererAction) protected readonly codePartRendererActions: ContributionProvider; canHandle(response: ChatResponseContent): number { if (CodeChatResponseContent.is(response)) { return 10; } return -1; } render(response: CodeChatResponseContent, parentNode: ResponseNode): ReactNode { const language = response.language ? this.languageService.getExtension(response.language) : undefined; return (
{this.renderTitle(response)}
{this.codePartRendererActions.getContributions() .filter(action => action.canRender ? action.canRender(response, parentNode) : true) .sort((a, b) => a.priority - b.priority) .map(action => action.render(response, parentNode))}
this.handleContextMenuEvent(parentNode, e, response.code)}>
); } protected renderTitle(response: CodeChatResponseContent): ReactNode { const uri = response.location?.uri; const position = response.location?.position; if (uri && position) { return {this.getTitle(response.location?.uri, response.language)}; } return this.getTitle(response.location?.uri, response.language); } private getTitle(uri: URI | undefined, language: string | undefined): string { // If there is a URI, use the file name as the title. Otherwise, use the language as the title. // If there is no language, use a generic fallback title. return uri?.path?.toString().split('/').pop() ?? language ?? nls.localize('theia/ai/chat-ui/code-part-renderer/generatedCode', 'Generated Code'); } /** * Opens a file and moves the cursor to the specified position. * * @param uri - The URI of the file to open. * @param position - The position to move the cursor to, specified as {line, character}. */ async openFileAtPosition(uri: URI, position: Position): Promise { const editorWidget = await this.editorManager.open(uri) as EditorWidget; if (editorWidget) { const editor = editorWidget.editor; editor.revealPosition(position); editor.focus(); editor.cursor = position; } } protected handleContextMenuEvent(node: TreeNode | undefined, event: IMouseEvent, code: string): void { this.contextMenuRenderer.render({ menuPath: ChatViewTreeWidget.CONTEXT_MENU, anchor: { x: event.posx, y: event.posy }, args: [node, { code }], context: event.target }); event.preventDefault(); } } @injectable() export class CopyToClipboardButtonAction implements CodePartRendererAction { @inject(ClipboardService) protected readonly clipboardService: ClipboardService; priority = 10; render(response: CodeChatResponseContent): ReactNode { return ; } } const CopyToClipboardButton = (props: { code: string, clipboardService: ClipboardService }) => { const { code, clipboardService } = props; const [copied, setCopied] = React.useState(false); const timeoutRef = React.useRef | undefined>(undefined); React.useEffect(() => () => { if (timeoutRef.current !== undefined) { clearTimeout(timeoutRef.current); } }, []); const copyCodeToClipboard = React.useCallback(() => { clipboardService.writeText(code); setCopied(true); if (timeoutRef.current !== undefined) { clearTimeout(timeoutRef.current); } timeoutRef.current = setTimeout(() => { setCopied(false); timeoutRef.current = undefined; }, 2000); }, [code, clipboardService]); const iconClass = copied ? 'codicon-check' : 'codicon-copy'; const title = copied ? nls.localizeByDefault('Copied') : nls.localizeByDefault('Copy'); return
; }; @injectable() export class InsertCodeAtCursorButtonAction implements CodePartRendererAction { @inject(EditorManager) protected readonly editorManager: EditorManager; priority = 20; render(response: CodeChatResponseContent): ReactNode { return ; } } const InsertCodeAtCursorButton = (props: { code: string, editorManager: EditorManager }) => { const { code, editorManager } = props; const insertCode = React.useCallback(() => { const editor = editorManager.currentEditor; if (editor) { const currentEditor = editor.editor; const selection = currentEditor.selection; // Insert the text at the current cursor position // If there is a selection, replace the selection with the text currentEditor.executeEdits([{ range: { start: selection.start, end: selection.end }, newText: code }]); } }, [code, editorManager]); return
; }; /** * Renders the given code within a Monaco Editor */ export const CodeWrapper = (props: { content: string, language?: string, untitledResourceResolver: UntitledResourceResolver, editorProvider: MonacoEditorProvider, contextMenuCallback: (e: IMouseEvent) => void }) => { // eslint-disable-next-line no-null/no-null const ref = React.useRef(null); const editorRef = React.useRef(undefined); const createInputElement = async () => { // Use a unique URI so that a remounted code block cannot reuse the URI of a just disposed one, // whose removal may not have reached the plugin host yet. const uri = new URI().resolve(`Untitled-${generateUuid()}${props.language ?? ''}`).withScheme(UNTITLED_SCHEME); const resource = await props.untitledResourceResolver.createUntitledResource(undefined, props.language, uri); const editor = await props.editorProvider.createSimpleInline(resource.uri, ref.current!, { readOnly: true, autoSizing: true, scrollBeyondLastLine: false, scrollBeyondLastColumn: 0, renderFinalNewline: 'off', maxHeight: -1, scrollbar: { vertical: 'hidden', alwaysConsumeMouseWheel: false }, wordWrap: 'off', codeLens: false, inlayHints: { enabled: 'off' }, hover: { enabled: 'off' } }); editor.document.textEditorModel.setValue(props.content); editor.getControl().onContextMenu(e => props.contextMenuCallback(e.event)); editorRef.current = editor; if (ref.current) { CodeWrapperEditors.set(ref.current, editor); } }; React.useEffect(() => { createInputElement(); const element = ref.current; return () => { if (element) { CodeWrapperEditors.remove(element); } if (editorRef.current) { editorRef.current.dispose(); } }; }, []); React.useEffect(() => { if (editorRef.current) { editorRef.current.document.textEditorModel.setValue(props.content); } }, [props.content]); editorRef.current?.resizeToFit(); return
; };