// ***************************************************************************** // 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, MultiSelectQuestionResponseHandler, QuestionResponseContent, QuestionResponseHandler } from '@theia/ai-chat'; import { nls } from '@theia/core'; import { codicon, OpenerService } from '@theia/core/lib/browser'; import { inject, injectable } from '@theia/core/shared/inversify'; import * as React from '@theia/core/shared/react'; import { ReactNode } from '@theia/core/shared/react'; import { ChatResponsePartRenderer } from '../chat-response-part-renderer.js'; import { ResponseNode } from '../chat-tree-view/index.js'; import { useMarkdownRendering } from './markdown-part-renderer.js'; @injectable() export class QuestionPartRenderer implements ChatResponsePartRenderer { @inject(OpenerService) protected readonly openerService: OpenerService; canHandle(response: ChatResponseContent): number { if (QuestionResponseContent.is(response)) { return 10; } return -1; } render(question: QuestionResponseContent, node: ResponseNode): ReactNode { if (question.multiSelect) { return ; } return ; } renderConfirmation(question: QuestionResponseContent, node: ResponseNode): ReactNode { if (question.multiSelect) { return ; } return ; } } function isResolved(question: QuestionResponseContent): boolean { return question.selectedOptions !== undefined; } function skipQuestion(question: QuestionResponseContent): void { if (question.isReadOnly) { return; } question.selectedOptions = []; if (question.multiSelect) { if (question.handler) { (question.handler as MultiSelectQuestionResponseHandler)([]); } } else { question.onSkip?.(); } } function isOptionSelected(question: QuestionResponseContent, option: { text: string }): boolean { return question.selectedOptions?.some(s => s.text === option.text) === true; } function DismissButton({ question, disabled }: { question: QuestionResponseContent, disabled: boolean }): React.JSX.Element | undefined { if (disabled) { return undefined; } return ( )) } ); } function MultiSelectQuestion({ question, node, openerService }: { question: QuestionResponseContent, node: ResponseNode, openerService: OpenerService }): React.JSX.Element { const questionRef = useMarkdownRendering(question.question, openerService); const restoredIndices = React.useMemo(() => { if (question.selectedOptions && question.selectedOptions.length > 0) { const indices = new Set(); for (const selected of question.selectedOptions) { const idx = question.options.findIndex(o => o.text === selected.text); if (idx >= 0) { indices.add(idx); } } return indices; } return new Set(); }, []); const [selectedIndices, setSelectedIndices] = React.useState>(restoredIndices); const [confirmed, setConfirmed] = React.useState(isResolved(question)); const isDisabled = question.isReadOnly || confirmed || !node.response.isWaitingForInput; const hasDescriptions = question.options.some(option => option.description); const toggleOption = React.useCallback((index: number): void => { if (isDisabled) { return; } setSelectedIndices(prev => { const next = new Set(prev); if (next.has(index)) { next.delete(index); } else { next.add(index); } return next; }); }, [isDisabled]); const handleConfirm = React.useCallback((): void => { if (isDisabled || selectedIndices.size === 0) { return; } const selectedOpts = Array.from(selectedIndices) .sort((a, b) => a - b) .map(i => question.options[i]); question.selectedOptions = selectedOpts; setConfirmed(true); if (question.handler) { (question.handler as MultiSelectQuestionResponseHandler)(selectedOpts); } }, [isDisabled, selectedIndices]); return (
{question.header &&
{question.header}
}
{question.options.map((option, index) => ( ))}
{!isDisabled && ( )}
); }