Skip to content

Commit

Permalink
fix(prompt): data related interpreter persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshinesmilelk committed Nov 28, 2024
1 parent 554540a commit 783a4d0
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 45 deletions.
14 changes: 10 additions & 4 deletions packages/libro-core/src/formatter/libro-formatter-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,21 @@ export const DefaultDecodedFormatter = {
};

export const DefaultEncodedFormatter = {
is: (arg: Record<any, any> | undefined): arg is DefaultEncodedFormatter => {
is: (
arg: Record<string, any> | 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'
);
},
};
8 changes: 2 additions & 6 deletions packages/libro-prompt-cell/src/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface PromptDecodedFormatter extends DefaultDecodedFormatter {
variableName?: string;
cellId?: string;
record?: string;
interpreterCode?: string;
interpreterEnabled?: boolean;
}

@singleton({ contrib: FormatterContribution })
Expand Down Expand Up @@ -44,6 +46,10 @@ export class FormatterPromptMagicContribution
source: encodeValue,
metadata: {
libroFormatter: this.formatter,
interpreter: {
interpreter_code: source.interpreterCode,
interpreter_enabled: source.interpreterEnabled,
},
},
};
};
Expand All @@ -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;
Expand All @@ -64,6 +77,8 @@ export class FormatterPromptMagicContribution
chatKey,
cellId,
record,
interpreterCode,
interpreterEnabled,
};
}
return {
Expand Down
13 changes: 10 additions & 3 deletions packages/libro-prompt-cell/src/prompt-cell-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -52,11 +53,14 @@ export class LibroPromptCellModel
@prop()
chatKey?: string;

@prop()
interpreterEnabled?: boolean;

@prop()
interpreterCode?: string;

@prop()
variableName: string;
variableName?: string;

@prop()
executing: boolean;
Expand All @@ -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;
Expand Down Expand Up @@ -121,7 +129,6 @@ export class LibroPromptCellModel
source: this.source,
metadata: this.metadata,
execution_count: this.executeCount,
// outputs: this.outputs,
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/libro-prompt-cell/src/prompt-cell-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
62 changes: 36 additions & 26 deletions packages/libro-prompt-cell/src/prompt-cell-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -49,6 +50,7 @@ export interface ChatObject {
order: number;
key: string;
disabled?: boolean;
interpreterEnabled?: boolean;
}

function ChatObjectFromKey(key: string): ChatObject {
Expand Down Expand Up @@ -164,8 +166,8 @@ const PropmtEditorViewComponent = React.forwardRef<HTMLDivElement>(
// 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);
};

Expand Down Expand Up @@ -211,27 +213,30 @@ const PropmtEditorViewComponent = React.forwardRef<HTMLDivElement>(
/>
</div>
<div className="libro-prompt-cell-right-header">
<div className="libro-interpreter-edit-container">
<Tag bordered={false} color="geekblue">
interpreter
</Tag>
<div className="libro-interpreter-edit-tip">代码编辑</div>
<Switch
size="small"
onChange={(checked) => {
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);
}
}}
/>
</div>
{instance.model.interpreterEnabled && (
<div className="libro-interpreter-edit-container">
<Tag bordered={false} color="geekblue">
interpreter
</Tag>
<div className="libro-interpreter-edit-tip">代码编辑</div>
<Switch
size="small"
disabled={!instance.model.interpreterCode}
onChange={(checked) => {
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);
}
}}
/>
</div>
)}
<ChatRecordInput
value={instance.model.record}
handleChange={instance.handleRecordChange}
Expand Down Expand Up @@ -283,13 +288,16 @@ export class LibroPromptCellView extends LibroEditableExecutableCellView {
this.model.mimeType = MIME.python;
this.outputArea.clear();
this.parent.enterEditMode();
this.parent.model.runnable = false;
} else {
this.model.interpreterCode = this.model.value;
this.model.metadata.interpreter = {
...this.model.metadata.interpreter,
interpreter_code: this.model.interpreterCode,
interpreter_enabled: this.model.interpreterEnabled,
};
this.model.mimeType = 'application/vnd.libro.prompt+json';
this.parent.model.runnable = true;
this.handleInterpreterOutput();
}
}
Expand Down Expand Up @@ -674,6 +682,7 @@ export class LibroPromptCellView extends LibroEditableExecutableCellView {
value: item.key,
label: <SelectionItemLabel item={item} />,
disabled: !!item.disabled,
interpreterEnabled: item.interpreterEnabled,
};
};

Expand Down Expand Up @@ -710,7 +719,7 @@ export class LibroPromptCellView extends LibroEditableExecutableCellView {
}
};

checkVariableNameAvailable = (variableName: string) => {
checkVariableNameAvailable = (variableName?: string) => {
return (
this.parent.model.cells.findIndex(
(cell) =>
Expand All @@ -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) => {
Expand Down

0 comments on commit 783a4d0

Please sign in to comment.