import { EditOutlined } from '@ant-design/icons'; import type { IRange } from '@difizen/libro-code-editor'; import type { ICodeCell, IOutput } from '@difizen/libro-common'; import { MIME } from '@difizen/libro-common'; import { isOutput } from '@difizen/libro-common'; import type { ExecutionMeta, KernelMessage, IOutputAreaOption, LibroCell, CellViewOptions, } from '@difizen/libro-jupyter'; import { KernelError, LibroJupyterModel, CellService, LibroEditableExecutableCellView, LibroOutputArea, } from '@difizen/libro-jupyter'; import { ChatComponents } from '@difizen/magent-chat'; import { getOrigin, inject, prop, transient, useInject, view, ViewInstance, ViewManager, ViewOption, ViewRender, watch, Deferred, ConfigurationService, } from '@difizen/mana-app'; import { l10n } from '@difizen/mana-l10n'; import { Select, Tag } from 'antd'; import type { DefaultOptionType } from 'antd/es/select/index.js'; import classNames from 'classnames'; import React, { useEffect, useState } from 'react'; import breaks from 'remark-breaks'; import remarkGfm from 'remark-gfm'; import { CodeBlock } from './code-block.js'; import { ChatRecordInput, VariableNameInput } from './input-handler/index.js'; import { LibroPromptCellModel } from './prompt-cell-model.js'; import { PromptScript } from './prompt-cell-script.js'; import './index.less'; export interface ChatObject { name: string; type: string; order: number; key: string; disabled?: boolean; support_interpreter?: 'dynamic' | 'immutable' | 'disable'; interpreter_enabled?: boolean; [key: string]: any; } function ChatObjectFromKey(key: string): ChatObject { const [type, name] = key.split(':'); return { name, type, key, order: 0, disabled: true, }; } export interface ChatObjectOptions { order?: number; color?: string; } const ChatObjectOptions = (type: string): ChatObjectOptions => { switch (type) { case 'LLM': return { order: 1, color: 'blue', }; case 'LMM': return { order: 2, color: 'cyan', }; case 'VARIABLE': return { order: 3, color: 'red', }; case 'API': return { order: 4, color: 'green', }; case 'CUSTOM': return { order: 5, color: undefined, }; default: return { order: undefined, color: undefined, }; } }; const InterpreterMode = () => { const instance = useInject(ViewInstance); // const handleInterpreterSwitch = (checked: boolean) => { // instance.model.interpreterEnabled = checked; // if (instance.model.chatKey) { // instance.switchInterpreterMode(instance.model.chatKey, checked); // instance.model.promptOutput = undefined; // instance.model.interpreterCode = undefined; // } // }; if (instance.model.supportInterpreter === 'immutable') { return ( Interpreter ); } // if (instance.model.supportInterpreter === 'dynamic') { // return ( //
// // {instance.model.interpreterEnabled ? '关闭 Interpreter' : '开启 Interpreter'} // // //
// ); // } return null; }; const SelectionItemLabel: React.FC<{ item: ChatObject }> = (props: { item: ChatObject; }) => { const item = props.item; return ( {item.type} {item.name} ); }; const CellEditorRaw: React.FC = () => { const instance = useInject(ViewInstance); useEffect(() => { if (instance.editorView?.editor) { instance.editor = getOrigin(instance.editorView?.editor); } }, [instance, instance.editorView?.editor]); return <>{instance.editorView && }; }; export const CellEditor = React.memo(CellEditorRaw); const PropmtEditorViewComponent = React.forwardRef( function PropmtEditorViewComponent(props, ref) { const instance = useInject(ViewInstance); const LLMRender = ChatComponents.Markdown; const [selectedModel, setSelectedModel] = useState(l10n.t('暂无内置模型')); useEffect(() => { // TODO: Data initialization should not depend on view initialization, which causes limitations in usage scenarios and multiple renderings. instance.model.variableName = instance.model.decodeObject.variableName; instance .updateChatObjects() .then(() => { const len = instance.chatObjects.length; if (len > 0) { if (!instance.model.decodeObject.chatKey) { instance.model.chatKey = instance.chatObjects[len - 1].key; } else { instance.model.chatKey = instance.model.decodeObject.chatKey; } setSelectedModel(instance.model.chatKey); return; } return; }) .catch(() => { // }); instance.updateChatRecords(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const handleChange = (value: string, options?: DefaultOptionType) => { instance.handleModelNameChange(value, options); setSelectedModel(value); }; const replace = (data: string) => { if (instance instanceof LibroPromptCellView && instance.editor) { const length = instance.editor.model.value.length; const start = instance.editor.getPositionAt(0); const end = instance.editor.getPositionAt(length); if (start && end) { instance.editor.replaceSelection(data, { start, end, }); } } }; return (