// ***************************************************************************** // 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 { ChatResponsePartRenderer } from '../chat-response-part-renderer.js'; import { inject, injectable, named } from '@theia/core/shared/inversify'; import { ChatResponseContent, CommandChatResponseContent } from '@theia/ai-chat/lib/common'; import { ReactNode } from '@theia/core/shared/react'; import { CommandRegistry, CommandService, nls, ILogger } from '@theia/core'; @injectable() export class CommandPartRenderer implements ChatResponsePartRenderer { @inject(CommandService) private commandService: CommandService; @inject(CommandRegistry) private commandRegistry: CommandRegistry; @inject(ILogger) @named('ai-chat-ui:CommandPartRenderer') protected readonly logger: ILogger; canHandle(response: ChatResponseContent): number { if (CommandChatResponseContent.is(response)) { return 10; } return -1; } render(response: CommandChatResponseContent): ReactNode { const label = response.customCallback?.label ?? response.command?.label ?? response.command?.id .split('-') .map(s => s[0].toUpperCase() + s.substring(1)) .join(' ') ?? nls.localizeByDefault('Execute Command'); if (!response.customCallback && response.command) { const isCommandEnabled = this.commandRegistry.isEnabled(response.command.id); if (!isCommandEnabled) { return
{nls.localize('theia/ai/chat-ui/command-part-renderer/commandNotExecutable', 'The command has the id "{0}" but it is not executable from the Chat window.', response.command.id)}
; } } return ; } private onCommand(arg: CommandChatResponseContent): void { if (arg.customCallback) { arg.customCallback.callback().catch(e => { this.logger.error(e); }); } else if (arg.command) { this.commandService.executeCommand(arg.command.id, ...(arg.arguments ?? [])).catch(e => { this.logger.error(e); }); } else { this.logger.warn('No command or custom callback provided in command chat response content'); } } }