From 783a4d003d7d95e8544259489341fa24f5b03e17 Mon Sep 17 00:00:00 2001 From: sunshinesmilelk Date: Thu, 28 Nov 2024 14:41:49 +0800 Subject: [PATCH] fix(prompt): data related interpreter persistence --- .../src/formatter/libro-formatter-protocol.ts | 14 +++-- packages/libro-prompt-cell/src/index.less | 8 +-- .../src/input-handler/variable-name-input.tsx | 12 ++-- ...bro-formatter-prompt-magic-contribution.ts | 15 +++++ .../src/prompt-cell-model.ts | 13 +++- .../src/prompt-cell-protocol.ts | 1 + .../src/prompt-cell-view.tsx | 62 +++++++++++-------- 7 files changed, 80 insertions(+), 45 deletions(-) diff --git a/packages/libro-core/src/formatter/libro-formatter-protocol.ts b/packages/libro-core/src/formatter/libro-formatter-protocol.ts index f201eceb..68376850 100644 --- a/packages/libro-core/src/formatter/libro-formatter-protocol.ts +++ b/packages/libro-core/src/formatter/libro-formatter-protocol.ts @@ -49,15 +49,21 @@ export const DefaultDecodedFormatter = { }; export const DefaultEncodedFormatter = { - is: (arg: Record | undefined): arg is DefaultEncodedFormatter => { + is: ( + arg: Record | undefined, + ): arg is { + source: unknown; + metadata: { libroFormatter: string } & { [key: string]: unknown }; + } => { return ( !!arg && - // eslint-disable-next-line @typescript-eslint/no-explicit-any + typeof arg === 'object' && 'source' in arg && 'metadata' in arg && + typeof arg['metadata'] === 'object' && + arg['metadata'] !== null && 'libroFormatter' in arg['metadata'] && - typeof (arg as any).metadata.libroFormatter === 'string' && - typeof (arg as any).metadata === 'object' + typeof arg['metadata'].libroFormatter === 'string' ); }, }; diff --git a/packages/libro-prompt-cell/src/index.less b/packages/libro-prompt-cell/src/index.less index 768ab041..485afa4d 100644 --- a/packages/libro-prompt-cell/src/index.less +++ b/packages/libro-prompt-cell/src/index.less @@ -41,17 +41,13 @@ } } -.libro-llm-output-render { - p { - color: var(--mana-libro-llm-response-output-text-color); - } -} - .libro-prompt-output-render-container { padding: 10px 24px; } .libro-prompt-output-llm-render { + color: var(--mana-libro-llm-response-output-text-color); + .libro-llm-output-render pre { background: #f4f6fb; padding: 16px; diff --git a/packages/libro-prompt-cell/src/input-handler/variable-name-input.tsx b/packages/libro-prompt-cell/src/input-handler/variable-name-input.tsx index 15edced4..4c4777eb 100644 --- a/packages/libro-prompt-cell/src/input-handler/variable-name-input.tsx +++ b/packages/libro-prompt-cell/src/input-handler/variable-name-input.tsx @@ -11,9 +11,9 @@ import { useCallback, useState } from 'react'; import './index.less'; interface VariableNameInputPopoverContentProps { - value: string; - handleVariableNameChange: (variableName: string) => void; - checkVariableNameAvailable: (variableName: string) => boolean; + value?: string; + handleVariableNameChange: (variableName?: string) => void; + checkVariableNameAvailable: (variableName?: string) => boolean; cancel: () => void; } @@ -69,9 +69,9 @@ export const VariableNameInputPopoverContent: FC< }; interface VariableNameInputProps { - value: string; - handleVariableNameChange: (variableName: string) => void; - checkVariableNameAvailable: (variableName: string) => boolean; + value?: string; + handleVariableNameChange: (variableName?: string) => void; + checkVariableNameAvailable: (variableName?: string) => boolean; classname?: string; } const variableNameInputCls = 'libro-variable-name-input'; 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 4461a862..02cfcb11 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 @@ -14,6 +14,8 @@ export interface PromptDecodedFormatter extends DefaultDecodedFormatter { variableName?: string; cellId?: string; record?: string; + interpreterCode?: string; + interpreterEnabled?: boolean; } @singleton({ contrib: FormatterContribution }) @@ -44,6 +46,10 @@ export class FormatterPromptMagicContribution source: encodeValue, metadata: { libroFormatter: this.formatter, + interpreter: { + interpreter_code: source.interpreterCode, + interpreter_enabled: source.interpreterEnabled, + }, }, }; }; @@ -54,6 +60,13 @@ export class FormatterPromptMagicContribution const run = value.split('%%prompt \n')[1]; const runValue = JSON.parse(run); const codeValue = runValue.prompt; + let interpreterCode; + let interpreterEnabled; + if (formatterValue.metadata['interpreter']) { + interpreterCode = formatterValue.metadata['interpreter']['interpreter_code']; + interpreterEnabled = + formatterValue.metadata['interpreter']['interpreter_enabled']; + } const chatKey = runValue.chat_key || runValue.model_name; const variableName = runValue.variable_name; const cellId = runValue.cell_id; @@ -64,6 +77,8 @@ export class FormatterPromptMagicContribution chatKey, cellId, record, + interpreterCode, + interpreterEnabled, }; } return { diff --git a/packages/libro-prompt-cell/src/prompt-cell-model.ts b/packages/libro-prompt-cell/src/prompt-cell-model.ts index 69086334..834c8ff7 100644 --- a/packages/libro-prompt-cell/src/prompt-cell-model.ts +++ b/packages/libro-prompt-cell/src/prompt-cell-model.ts @@ -14,6 +14,7 @@ import { ViewManager } from '@difizen/mana-app'; import { inject } from '@difizen/mana-app'; import type { Event as ManaEvent } from '@difizen/mana-app'; +import type { PromptDecodedFormatter } from './libro-formatter-prompt-magic-contribution.js'; import type { InterpreterMeta } from './prompt-cell-protocol.js'; export interface PromptCellMetadata extends ICodeCellMetadata { @@ -52,11 +53,14 @@ export class LibroPromptCellModel @prop() chatKey?: string; + @prop() + interpreterEnabled?: boolean; + @prop() interpreterCode?: string; @prop() - variableName: string; + variableName?: string; @prop() executing: boolean; @@ -81,15 +85,19 @@ export class LibroPromptCellModel record: this.record, value: this._interpreterEditMode ? this.prompt : this.value, cellId: this.id, + interpreterCode: this.interpreterCode, + interpreterEnabled: this.interpreterEnabled, }; } - override set decodeObject(data) { + override set decodeObject(data: PromptDecodedFormatter) { this.value = data.value; this.prompt = data.value; this.variableName = data.variableName; this.chatKey = data.chatKey; this.record = data.record; + this.interpreterCode = data.interpreterCode; + this.interpreterEnabled = data.interpreterEnabled; } viewManager: ViewManager; @@ -121,7 +129,6 @@ export class LibroPromptCellModel source: this.source, metadata: this.metadata, execution_count: this.executeCount, - // outputs: this.outputs, }; } diff --git a/packages/libro-prompt-cell/src/prompt-cell-protocol.ts b/packages/libro-prompt-cell/src/prompt-cell-protocol.ts index 73d865fa..c8e17e43 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 { + interpreter_enabled?: boolean; interpreter_code?: string; interpreter_text?: string; } diff --git a/packages/libro-prompt-cell/src/prompt-cell-view.tsx b/packages/libro-prompt-cell/src/prompt-cell-view.tsx index 821efea9..a2adac8a 100644 --- a/packages/libro-prompt-cell/src/prompt-cell-view.tsx +++ b/packages/libro-prompt-cell/src/prompt-cell-view.tsx @@ -33,6 +33,7 @@ import { } from '@difizen/mana-app'; import { l10n } from '@difizen/mana-l10n'; 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'; @@ -49,6 +50,7 @@ export interface ChatObject { order: number; key: string; disabled?: boolean; + interpreterEnabled?: boolean; } function ChatObjectFromKey(key: string): ChatObject { @@ -164,8 +166,8 @@ const PropmtEditorViewComponent = React.forwardRef( // eslint-disable-next-line react-hooks/exhaustive-deps }, []); - const handleChange = (value: string) => { - instance.handleModelNameChange(value); + const handleChange = (value: string, options: DefaultOptionType) => { + instance.handleModelNameChange(value, options); setSelectedModel(value); }; @@ -211,27 +213,30 @@ const PropmtEditorViewComponent = React.forwardRef( />
-
- - interpreter - -
代码编辑
- { - instance.interpreterEditMode = checked; - if (!instance.editorView) { - return; - } - if (checked && instance.model.interpreterCode) { - replace(instance.model.interpreterCode); - } - if (!checked && instance.model.prompt) { - replace(instance.model.prompt); - } - }} - /> -
+ {instance.model.interpreterEnabled && ( +
+ + interpreter + +
代码编辑
+ { + instance.interpreterEditMode = checked; + if (!instance.editorView) { + return; + } + if (checked && instance.model.interpreterCode) { + replace(instance.model.interpreterCode); + } + if (!checked && instance.model.prompt) { + replace(instance.model.prompt); + } + }} + /> +
+ )} , disabled: !!item.disabled, + interpreterEnabled: item.interpreterEnabled, }; }; @@ -710,7 +719,7 @@ export class LibroPromptCellView extends LibroEditableExecutableCellView { } }; - checkVariableNameAvailable = (variableName: string) => { + checkVariableNameAvailable = (variableName?: string) => { return ( this.parent.model.cells.findIndex( (cell) => @@ -719,10 +728,11 @@ export class LibroPromptCellView extends LibroEditableExecutableCellView { ) > -1 ); }; - handleModelNameChange = (value: string) => { + handleModelNameChange = (value: string, option: DefaultOptionType) => { this.model.chatKey = value; + this.model.interpreterEnabled = option['interpreterEnabled']; }; - handleVariableNameChange = (value: string) => { + handleVariableNameChange = (value?: string) => { this.model.variableName = value; }; handleRecordChange = (value: string | undefined) => {