diff --git a/packages/libro-prompt-cell/src/code-block.tsx b/packages/libro-prompt-cell/src/code-block.tsx index 46d66e60..508bc627 100644 --- a/packages/libro-prompt-cell/src/code-block.tsx +++ b/packages/libro-prompt-cell/src/code-block.tsx @@ -2,6 +2,7 @@ import { CopyOutlined, EditOutlined } from '@ant-design/icons'; import type { DisplayDataOutputModel } from '@difizen/libro-jupyter'; import { copy2clipboard } from '@difizen/libro-jupyter'; import { useInject, ViewInstance } from '@difizen/mana-app'; +import { Collapse } from 'antd'; import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; import { LibroPromptCellView } from './prompt-cell-view.js'; @@ -61,29 +62,44 @@ export const InterpreterCodeBlock = (props: any) => { const [, lang] = /language-(\w+)/.exec(className || '') || []; return ( -
-        {cell.model.interpreterEnabled && (
-          
{ - cell.interpreterEditMode = true; - if (cell.model.interpreterCode) { - replace(cell.model.interpreterCode); - } - }} - > -
代码编辑
- -
- )} - - {typeof children === 'string' ? children.trim() : children} - -
+ +
+
+                    {
+                      
{ + cell.interpreterEditMode = true; + if (cell.model.interpreterCode) { + replace(cell.model.interpreterCode); + } + }} + > +
代码编辑
+ +
+ } + + {typeof children === 'string' ? children.trim() : children} + +
+
+

+ ), + }, + ]} + >
); } diff --git a/packages/libro-prompt-cell/src/index.less b/packages/libro-prompt-cell/src/index.less index fc0e44eb..e61eb033 100644 --- a/packages/libro-prompt-cell/src/index.less +++ b/packages/libro-prompt-cell/src/index.less @@ -1,3 +1,5 @@ +@ant-prefix: ant; + .libro-prompt-cell-header { display: flex; align-items: center; @@ -7,6 +9,21 @@ .libro-interpreter-code-output-render { padding: 10px 24px; + + .@{ant-prefix}-collapse-content > .@{ant-prefix}-collapse-content-box { + padding: unset; + } + .@{ant-prefix}-collapse-ghost + > .@{ant-prefix}-collapse-item + > .@{ant-prefix}-collapse-content + > .@{ant-prefix}-collapse-content-box { + padding: unset; + } + .@{ant-prefix}-collapse + > .@{ant-prefix}-collapse-item + > .@{ant-prefix}-collapse-header { + padding: unset; + } } .libro-llm-hljs { @@ -108,3 +125,11 @@ .libro-prompt-cell-right-header { display: flex; } + +.libro-prompt-cell-interpreter-switch { + margin: 0 6px; +} + +.libro-prompt-cell-interpreter-header-container { + display: inline-block; +} diff --git a/packages/libro-prompt-cell/src/libro-formatter-prompt-magic-contribution.ts b/packages/libro-prompt-cell/src/libro-formatter-prompt-magic-contribution.ts index 02cfcb11..3916483d 100644 --- a/packages/libro-prompt-cell/src/libro-formatter-prompt-magic-contribution.ts +++ b/packages/libro-prompt-cell/src/libro-formatter-prompt-magic-contribution.ts @@ -15,6 +15,7 @@ export interface PromptDecodedFormatter extends DefaultDecodedFormatter { cellId?: string; record?: string; interpreterCode?: string; + supportInterpreter?: 'dynamic' | 'immutable' | 'disable'; interpreterEnabled?: boolean; } @@ -48,6 +49,7 @@ export class FormatterPromptMagicContribution libroFormatter: this.formatter, interpreter: { interpreter_code: source.interpreterCode, + support_interpreter: source.supportInterpreter, interpreter_enabled: source.interpreterEnabled, }, }, @@ -61,9 +63,12 @@ export class FormatterPromptMagicContribution const runValue = JSON.parse(run); const codeValue = runValue.prompt; let interpreterCode; + let supportInterpreter; let interpreterEnabled; if (formatterValue.metadata['interpreter']) { interpreterCode = formatterValue.metadata['interpreter']['interpreter_code']; + supportInterpreter = + formatterValue.metadata['interpreter']['support_interpreter']; interpreterEnabled = formatterValue.metadata['interpreter']['interpreter_enabled']; } @@ -78,6 +83,7 @@ export class FormatterPromptMagicContribution cellId, record, interpreterCode, + supportInterpreter, interpreterEnabled, }; } diff --git a/packages/libro-prompt-cell/src/prompt-cell-model.ts b/packages/libro-prompt-cell/src/prompt-cell-model.ts index f869e1b6..c98da5ab 100644 --- a/packages/libro-prompt-cell/src/prompt-cell-model.ts +++ b/packages/libro-prompt-cell/src/prompt-cell-model.ts @@ -53,6 +53,9 @@ export class LibroPromptCellModel @prop() chatKey?: string; + @prop() + supportInterpreter?: 'dynamic' | 'immutable' | 'disable'; + @prop() interpreterEnabled?: boolean; @@ -86,6 +89,7 @@ export class LibroPromptCellModel value: this._interpreterEditMode ? this.prompt : this.value, cellId: this.id, interpreterCode: this.interpreterCode, + supportInterpreter: this.supportInterpreter, interpreterEnabled: this.interpreterEnabled, }; } @@ -97,6 +101,7 @@ export class LibroPromptCellModel this.chatKey = data.chatKey; this.record = data.record; this.interpreterCode = data.interpreterCode; + this.supportInterpreter = data.supportInterpreter; this.interpreterEnabled = data.interpreterEnabled; } @@ -126,6 +131,7 @@ export class LibroPromptCellModel this.metadata.interpreter = { ...this.metadata.interpreter, interpreter_code: this.interpreterCode, + support_interpreter: this.supportInterpreter, interpreter_enabled: this.interpreterEnabled, }; return { diff --git a/packages/libro-prompt-cell/src/prompt-cell-protocol.ts b/packages/libro-prompt-cell/src/prompt-cell-protocol.ts index c8e17e43..38b3429c 100644 --- a/packages/libro-prompt-cell/src/prompt-cell-protocol.ts +++ b/packages/libro-prompt-cell/src/prompt-cell-protocol.ts @@ -9,6 +9,7 @@ export type LibroPromptCellModelFactory = ( export const LibroPromptCellModelFactory = Symbol('LibroPromptCellModelFactory'); export interface InterpreterMeta extends PartialJSONObject { + support_interpreter?: 'dynamic' | 'immutable' | 'disable'; interpreter_enabled?: boolean; interpreter_code?: string; interpreter_text?: string; diff --git a/packages/libro-prompt-cell/src/prompt-cell-script.ts b/packages/libro-prompt-cell/src/prompt-cell-script.ts index 1b1e20a1..f630f183 100644 --- a/packages/libro-prompt-cell/src/prompt-cell-script.ts +++ b/packages/libro-prompt-cell/src/prompt-cell-script.ts @@ -6,4 +6,7 @@ export class PromptScript { chat_object_manager.dump_kernel_list_json()`; public readonly getChatRecoreds: string = `from libro_ai import chat_record_provider chat_record_provider.get_records()`; + switchInterpreterMode = (key: string, mode: boolean) => { + return `from libro_ai import chat_object_manager\nexecutor = chat_object_manager.get_executor('${key}')\nexecutor.set_interpreter_support(${mode ? 'True' : 'False'})`; + }; } diff --git a/packages/libro-prompt-cell/src/prompt-cell-view.tsx b/packages/libro-prompt-cell/src/prompt-cell-view.tsx index b10dbe3e..b59e455c 100644 --- a/packages/libro-prompt-cell/src/prompt-cell-view.tsx +++ b/packages/libro-prompt-cell/src/prompt-cell-view.tsx @@ -33,13 +33,14 @@ import { Deferred, } from '@difizen/mana-app'; import { l10n } from '@difizen/mana-l10n'; -import { Select, Tag } from 'antd'; +import { Select, Switch, 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'; @@ -51,7 +52,9 @@ export interface ChatObject { order: number; key: string; disabled?: boolean; - interpreterEnabled?: boolean; + support_interpreter?: 'dynamic' | 'immutable' | 'disable'; + interpreter_enabled?: boolean; + [key: string]: any; } function ChatObjectFromKey(key: string): ChatObject { @@ -104,6 +107,42 @@ const ChatObjectOptions = (type: string): ChatObjectOptions => { } }; +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; }) => { @@ -207,6 +246,9 @@ const PropmtEditorViewComponent = React.forwardRef( }} /> +
+ +
( {instance.model.prompt}
- + {instance.model.promptOutput}
)}
- {instance.model.interpreterEnabled && instance.interpreterEditMode && ( + {instance.interpreterEditMode && (
{ @@ -648,6 +694,18 @@ export class LibroPromptCellView extends LibroEditableExecutableCellView { ); }; + switchInterpreterMode = async (chatKey: string, mode: boolean) => { + return this.fetch( + { + code: this.promptScript.switchInterpreterMode(chatKey, mode), + store_history: false, + }, + (msg) => { + // + }, + ); + }; + updateChatRecords = async () => { return this.fetch( { @@ -670,7 +728,7 @@ export class LibroPromptCellView extends LibroEditableExecutableCellView { value: item.key, label: , disabled: !!item.disabled, - interpreterEnabled: item.interpreterEnabled, + support_interpreter: item.support_interpreter, }; }; @@ -718,7 +776,7 @@ export class LibroPromptCellView extends LibroEditableExecutableCellView { }; handleModelNameChange = (value: string, option: DefaultOptionType) => { this.model.chatKey = value; - this.model.interpreterEnabled = option['interpreterEnabled']; + this.model.supportInterpreter = option['support_interpreter']; }; handleVariableNameChange = (value?: string) => { this.model.variableName = value;