@difizen/libro-prompt-cell 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/es/code-block.d.ts +3 -0
  2. package/es/code-block.d.ts.map +1 -0
  3. package/es/code-block.js +110 -0
  4. package/es/index.less +69 -6
  5. package/es/input-handler/variable-name-input.d.ts +6 -6
  6. package/es/input-handler/variable-name-input.d.ts.map +1 -1
  7. package/es/interpreter-code-output-render.d.ts +6 -0
  8. package/es/interpreter-code-output-render.d.ts.map +1 -0
  9. package/es/interpreter-code-output-render.js +25 -0
  10. package/es/interpreter-code-output-rendermime-contribution.d.ts +13 -0
  11. package/es/interpreter-code-output-rendermime-contribution.d.ts.map +1 -0
  12. package/es/interpreter-code-output-rendermime-contribution.js +22 -0
  13. package/es/libro-formatter-prompt-magic-contribution.d.ts +15 -1
  14. package/es/libro-formatter-prompt-magic-contribution.d.ts.map +1 -1
  15. package/es/libro-formatter-prompt-magic-contribution.js +18 -2
  16. package/es/module.d.ts.map +1 -1
  17. package/es/module.js +2 -1
  18. package/es/prompt-cell-model.d.ts +14 -18
  19. package/es/prompt-cell-model.d.ts.map +1 -1
  20. package/es/prompt-cell-model.js +66 -27
  21. package/es/prompt-cell-protocol.d.ts +7 -0
  22. package/es/prompt-cell-protocol.d.ts.map +1 -1
  23. package/es/prompt-cell-script.d.ts +1 -0
  24. package/es/prompt-cell-script.d.ts.map +1 -1
  25. package/es/prompt-cell-script.js +3 -0
  26. package/es/prompt-cell-view.d.ts +16 -9
  27. package/es/prompt-cell-view.d.ts.map +1 -1
  28. package/es/prompt-cell-view.js +329 -62
  29. package/es/prompt-output-render.d.ts.map +1 -1
  30. package/es/prompt-output-render.js +17 -3
  31. package/package.json +11 -6
  32. package/src/code-block.tsx +107 -0
  33. package/src/index.less +69 -6
  34. package/src/input-handler/variable-name-input.tsx +6 -6
  35. package/src/interpreter-code-output-render.tsx +28 -0
  36. package/src/interpreter-code-output-rendermime-contribution.ts +18 -0
  37. package/src/libro-formatter-prompt-magic-contribution.ts +21 -0
  38. package/src/module.ts +2 -0
  39. package/src/prompt-cell-model.ts +42 -11
  40. package/src/prompt-cell-protocol.ts +8 -0
  41. package/src/prompt-cell-script.ts +3 -0
  42. package/src/prompt-cell-view.tsx +251 -14
  43. package/src/prompt-output-render.tsx +22 -8
  44. package/es/libro-llm-render.d.ts +0 -10
  45. package/es/libro-llm-render.d.ts.map +0 -1
  46. package/es/libro-llm-render.js +0 -75
  47. package/src/libro-llm-render.tsx +0 -64
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@difizen/libro-prompt-cell",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "libro",
@@ -32,14 +32,18 @@
32
32
  "src"
33
33
  ],
34
34
  "dependencies": {
35
- "@difizen/libro-code-editor": "^0.3.1",
36
- "@difizen/libro-jupyter": "^0.3.1",
37
- "@difizen/libro-rendermime": "^0.3.1",
38
- "@difizen/libro-common": "^0.3.1",
39
- "@difizen/libro-core": "^0.3.1",
35
+ "@difizen/libro-code-editor": "^0.3.2",
36
+ "@difizen/libro-jupyter": "^0.3.2",
37
+ "@difizen/libro-rendermime": "^0.3.2",
38
+ "@difizen/libro-common": "^0.3.2",
39
+ "@difizen/libro-core": "^0.3.2",
40
40
  "@ant-design/icons": "^5.1.0",
41
41
  "@difizen/mana-l10n": "latest",
42
42
  "@difizen/mana-app": "latest",
43
+ "@difizen/magent-chat": "^0.1.27",
44
+ "react-syntax-highlighter": "^15.5.0",
45
+ "remark-breaks": "^4.0.0",
46
+ "remark-gfm": "^4.0.0",
43
47
  "classnames": "^2.3.2",
44
48
  "highlight.js": "^11.8.0",
45
49
  "marked": "^5.1.1",
@@ -55,6 +59,7 @@
55
59
  },
56
60
  "devDependencies": {
57
61
  "@types/marked": "^5.0.1",
62
+ "@types/react-syntax-highlighter": "^15.5.13",
58
63
  "@types/react": "^18.2.25",
59
64
  "@types/uuid": "^9.0.2",
60
65
  "@types/react-dom": "^18.2.4"
@@ -0,0 +1,107 @@
1
+ import { CopyOutlined, EditOutlined } from '@ant-design/icons';
2
+ import type { DisplayDataOutputModel } from '@difizen/libro-jupyter';
3
+ import { copy2clipboard } from '@difizen/libro-jupyter';
4
+ import { useInject, ViewInstance } from '@difizen/mana-app';
5
+ import { Collapse } from 'antd';
6
+ import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
7
+
8
+ import { LibroPromptCellView } from './prompt-cell-view.js';
9
+
10
+ export const CodeBlock = (props: any) => {
11
+ const { className, children } = props;
12
+
13
+ if (!props.inline && className) {
14
+ const [, lang] = /language-(\w+)/.exec(className || '') || [];
15
+
16
+ return (
17
+ <pre className={`chat-msg-md-code-wrap`}>
18
+ {lang && <div className={`chat-msg-md-code-lang`}>{lang}</div>}
19
+ <CopyOutlined
20
+ onClick={() => {
21
+ copy2clipboard(children);
22
+ }}
23
+ className={`chat-msg-md-code-copy`}
24
+ />
25
+ <SyntaxHighlighter
26
+ className={`libro-llm-syntax-highlighter`}
27
+ language={lang}
28
+ style={{}}
29
+ >
30
+ {typeof children === 'string' ? children.trim() : children}
31
+ </SyntaxHighlighter>
32
+ </pre>
33
+ );
34
+ }
35
+
36
+ return <code className={`chat-msg-md-code-code`}>{children}</code>;
37
+ };
38
+
39
+ export const InterpreterCodeBlock = (props: any) => {
40
+ const { className, children } = props;
41
+ const instance = useInject<DisplayDataOutputModel>(ViewInstance);
42
+ const cell = instance.cell;
43
+ if (!(cell instanceof LibroPromptCellView)) {
44
+ return null;
45
+ }
46
+
47
+ const replace = (data: string) => {
48
+ if (cell.editor) {
49
+ const length = cell.editor.model.value.length;
50
+ const start = cell.editor.getPositionAt(0);
51
+ const end = cell.editor.getPositionAt(length);
52
+ if (start && end) {
53
+ cell.editor.replaceSelection(data, {
54
+ start,
55
+ end,
56
+ });
57
+ }
58
+ }
59
+ };
60
+
61
+ if (!props.inline && className) {
62
+ const [, lang] = /language-(\w+)/.exec(className || '') || [];
63
+
64
+ return (
65
+ <Collapse
66
+ ghost
67
+ items={[
68
+ {
69
+ key: '1',
70
+ label: 'Code',
71
+ children: (
72
+ <p>
73
+ <div className="libro-code-interpreter-code">
74
+ <pre className={`chat-msg-md-code-wrap`}>
75
+ {
76
+ <div
77
+ className="libro-interpreter-edit-container"
78
+ onClick={() => {
79
+ cell.interpreterEditMode = true;
80
+ if (cell.model.interpreterCode) {
81
+ replace(cell.model.interpreterCode);
82
+ }
83
+ }}
84
+ >
85
+ <div className="libro-interpreter-edit-tip">代码编辑</div>
86
+ <EditOutlined className="libro-interpreter-edit-icon" />
87
+ </div>
88
+ }
89
+ <SyntaxHighlighter
90
+ className={`libro-llm-syntax-highlighter`}
91
+ language={lang}
92
+ style={{}}
93
+ >
94
+ {typeof children === 'string' ? children.trim() : children}
95
+ </SyntaxHighlighter>
96
+ </pre>
97
+ </div>
98
+ </p>
99
+ ),
100
+ },
101
+ ]}
102
+ ></Collapse>
103
+ );
104
+ }
105
+
106
+ return <code className={`chat-msg-md-code-code`}>{children}</code>;
107
+ };
package/src/index.less CHANGED
@@ -1,7 +1,29 @@
1
+ @ant-prefix: ant;
2
+
1
3
  .libro-prompt-cell-header {
2
4
  display: flex;
3
5
  align-items: center;
4
6
  justify-content: space-between;
7
+ border-bottom: 1px solid var(--mana-libro-code-border-color);
8
+ }
9
+
10
+ .libro-interpreter-code-output-render {
11
+ padding: 10px 24px;
12
+
13
+ .@{ant-prefix}-collapse-content > .@{ant-prefix}-collapse-content-box {
14
+ padding: unset;
15
+ }
16
+ .@{ant-prefix}-collapse-ghost
17
+ > .@{ant-prefix}-collapse-item
18
+ > .@{ant-prefix}-collapse-content
19
+ > .@{ant-prefix}-collapse-content-box {
20
+ padding: unset;
21
+ }
22
+ .@{ant-prefix}-collapse
23
+ > .@{ant-prefix}-collapse-item
24
+ > .@{ant-prefix}-collapse-header {
25
+ padding: unset;
26
+ }
5
27
  }
6
28
 
7
29
  .libro-llm-hljs {
@@ -36,17 +58,13 @@
36
58
  }
37
59
  }
38
60
 
39
- .libro-llm-output-render {
40
- p {
41
- color: var(--mana-libro-llm-response-output-text-color);
42
- }
43
- }
44
-
45
61
  .libro-prompt-output-render-container {
46
62
  padding: 10px 24px;
47
63
  }
48
64
 
49
65
  .libro-prompt-output-llm-render {
66
+ color: var(--mana-libro-llm-response-output-text-color);
67
+
50
68
  .libro-llm-output-render pre {
51
69
  background: #f4f6fb;
52
70
  padding: 16px;
@@ -70,3 +88,48 @@
70
88
  }
71
89
  }
72
90
  }
91
+
92
+ .libro-prompt-cell-model-tip,
93
+ .libro-prompt-cell-model-prompt {
94
+ padding: 10px 24px;
95
+ }
96
+
97
+ .libro-prompt-cell-model-prompt {
98
+ border-bottom: 1px solid var(--mana-color-border);
99
+ }
100
+
101
+ .libro-prompt-cell-model-tip {
102
+ color: var(--mana-libro-llm-response-output-text-color);
103
+ }
104
+
105
+ .libro-interpreter-edit-tip {
106
+ margin-right: 4px;
107
+ }
108
+
109
+ .libro-interpreter-edit-container {
110
+ display: flex;
111
+ position: absolute;
112
+ margin-top: 4px;
113
+ z-index: 100;
114
+ right: 0;
115
+ align-items: center;
116
+ padding: 0 8px;
117
+ color: #bdc0c4;
118
+ cursor: pointer;
119
+
120
+ &:hover {
121
+ color: #1590ff;
122
+ }
123
+ }
124
+
125
+ .libro-prompt-cell-right-header {
126
+ display: flex;
127
+ }
128
+
129
+ .libro-prompt-cell-interpreter-switch {
130
+ margin: 0 6px;
131
+ }
132
+
133
+ .libro-prompt-cell-interpreter-header-container {
134
+ display: inline-block;
135
+ }
@@ -11,9 +11,9 @@ import { useCallback, useState } from 'react';
11
11
  import './index.less';
12
12
 
13
13
  interface VariableNameInputPopoverContentProps {
14
- value: string;
15
- handleVariableNameChange: (variableName: string) => void;
16
- checkVariableNameAvailable: (variableName: string) => boolean;
14
+ value?: string;
15
+ handleVariableNameChange: (variableName?: string) => void;
16
+ checkVariableNameAvailable: (variableName?: string) => boolean;
17
17
  cancel: () => void;
18
18
  }
19
19
 
@@ -69,9 +69,9 @@ export const VariableNameInputPopoverContent: FC<
69
69
  };
70
70
 
71
71
  interface VariableNameInputProps {
72
- value: string;
73
- handleVariableNameChange: (variableName: string) => void;
74
- checkVariableNameAvailable: (variableName: string) => boolean;
72
+ value?: string;
73
+ handleVariableNameChange: (variableName?: string) => void;
74
+ checkVariableNameAvailable: (variableName?: string) => boolean;
75
75
  classname?: string;
76
76
  }
77
77
  const variableNameInputCls = 'libro-variable-name-input';
@@ -0,0 +1,28 @@
1
+ import type { BaseOutputView } from '@difizen/libro-jupyter';
2
+ import React from 'react';
3
+
4
+ import { InterpreterCodeBlock } from './code-block.js';
5
+ import { LibroPromptCellView } from './prompt-cell-view.js';
6
+
7
+ export const InterpreterCodeOutputRender: React.FC<{
8
+ model: BaseOutputView;
9
+ }> = (props: { model: BaseOutputView }) => {
10
+ const { model } = props;
11
+ if (!model.data['application/vnd.libro.interpreter.code+text']) {
12
+ return null;
13
+ }
14
+ if (!(model.cell instanceof LibroPromptCellView)) {
15
+ return null;
16
+ }
17
+
18
+ const data = model.data['application/vnd.libro.interpreter.code+text'] as string;
19
+ model.cell.model.interpreterCode = data;
20
+
21
+ return (
22
+ <div className="libro-interpreter-code-output-render-container">
23
+ <div className="libro-interpreter-code-output-render">
24
+ <InterpreterCodeBlock className="language-python">{data}</InterpreterCodeBlock>
25
+ </div>
26
+ </div>
27
+ );
28
+ };
@@ -0,0 +1,18 @@
1
+ import type { BaseOutputView } from '@difizen/libro-jupyter';
2
+ import { RenderMimeContribution } from '@difizen/libro-jupyter';
3
+ import { singleton } from '@difizen/mana-app';
4
+
5
+ import { InterpreterCodeOutputRender } from './interpreter-code-output-render.js';
6
+
7
+ @singleton({ contrib: RenderMimeContribution })
8
+ export class LibroInterpreterCodeOutputMimeTypeContribution
9
+ implements RenderMimeContribution
10
+ {
11
+ canHandle = (model: BaseOutputView) => {
12
+ return 200;
13
+ };
14
+ renderType = 'interpreterCodeOutputRender';
15
+ safe = true;
16
+ mimeTypes = ['application/vnd.libro.interpreter.code+text'];
17
+ render = InterpreterCodeOutputRender;
18
+ }
@@ -14,6 +14,9 @@ export interface PromptDecodedFormatter extends DefaultDecodedFormatter {
14
14
  variableName?: string;
15
15
  cellId?: string;
16
16
  record?: string;
17
+ interpreterCode?: string;
18
+ supportInterpreter?: 'dynamic' | 'immutable' | 'disable';
19
+ interpreterEnabled?: boolean;
17
20
  }
18
21
 
19
22
  @singleton({ contrib: FormatterContribution })
@@ -44,6 +47,11 @@ export class FormatterPromptMagicContribution
44
47
  source: encodeValue,
45
48
  metadata: {
46
49
  libroFormatter: this.formatter,
50
+ interpreter: {
51
+ interpreter_code: source.interpreterCode,
52
+ support_interpreter: source.supportInterpreter,
53
+ interpreter_enabled: source.interpreterEnabled,
54
+ },
47
55
  },
48
56
  };
49
57
  };
@@ -54,6 +62,16 @@ export class FormatterPromptMagicContribution
54
62
  const run = value.split('%%prompt \n')[1];
55
63
  const runValue = JSON.parse(run);
56
64
  const codeValue = runValue.prompt;
65
+ let interpreterCode;
66
+ let supportInterpreter;
67
+ let interpreterEnabled;
68
+ if (formatterValue.metadata['interpreter']) {
69
+ interpreterCode = formatterValue.metadata['interpreter']['interpreter_code'];
70
+ supportInterpreter =
71
+ formatterValue.metadata['interpreter']['support_interpreter'];
72
+ interpreterEnabled =
73
+ formatterValue.metadata['interpreter']['interpreter_enabled'];
74
+ }
57
75
  const chatKey = runValue.chat_key || runValue.model_name;
58
76
  const variableName = runValue.variable_name;
59
77
  const cellId = runValue.cell_id;
@@ -64,6 +82,9 @@ export class FormatterPromptMagicContribution
64
82
  chatKey,
65
83
  cellId,
66
84
  record,
85
+ interpreterCode,
86
+ supportInterpreter,
87
+ interpreterEnabled,
67
88
  };
68
89
  }
69
90
  return {
package/src/module.ts CHANGED
@@ -2,6 +2,7 @@ import { CellOptions, LibroModule, OutputModule } from '@difizen/libro-core';
2
2
  import { LibroRenderMimeModule } from '@difizen/libro-rendermime';
3
3
  import { ManaModule } from '@difizen/mana-app';
4
4
 
5
+ import { LibroInterpreterCodeOutputMimeTypeContribution } from './interpreter-code-output-rendermime-contribution.js';
5
6
  import { FormatterPromptMagicContribution } from './libro-formatter-prompt-magic-contribution.js';
6
7
  import { LibroPromptCellCommandContribution } from './prompt-cell-command-contribution.js';
7
8
  import { PromptCellContribution } from './prompt-cell-contribution.js';
@@ -22,6 +23,7 @@ export const LibroPromptCellModule = ManaModule.create()
22
23
  LibroPromptCellView,
23
24
  LibroPromptCellModel,
24
25
  LibroPromptOutputMimeTypeContribution,
26
+ LibroInterpreterCodeOutputMimeTypeContribution,
25
27
  LibroPromptCellCommandContribution,
26
28
  FormatterPromptMagicContribution,
27
29
  {
@@ -1,9 +1,8 @@
1
1
  import type {
2
- ICellMetadata,
2
+ ICodeCell,
3
3
  ExecutionCount,
4
4
  ICodeCellMetadata,
5
5
  } from '@difizen/libro-common';
6
- import type { ICodeCell } from '@difizen/libro-common';
7
6
  import type { ExecutableCellModel } from '@difizen/libro-core';
8
7
  import { LibroCellModel } from '@difizen/libro-core';
9
8
  import { CellOptions } from '@difizen/libro-core';
@@ -15,8 +14,12 @@ import { ViewManager } from '@difizen/mana-app';
15
14
  import { inject } from '@difizen/mana-app';
16
15
  import type { Event as ManaEvent } from '@difizen/mana-app';
17
16
 
17
+ import type { PromptDecodedFormatter } from './libro-formatter-prompt-magic-contribution.js';
18
+ import type { InterpreterMeta } from './prompt-cell-protocol.js';
19
+
18
20
  export interface PromptCellMetadata extends ICodeCellMetadata {
19
21
  execution: ExecutionMeta;
22
+ interpreter: InterpreterMeta;
20
23
  }
21
24
 
22
25
  @transient()
@@ -32,7 +35,7 @@ export class LibroPromptCellModel
32
35
  @prop()
33
36
  hasExecutedError = false;
34
37
  @prop()
35
- override metadata: Partial<PromptCellMetadata | ICellMetadata>;
38
+ override metadata: Partial<PromptCellMetadata>;
36
39
  @prop()
37
40
  kernelExecuting = false;
38
41
 
@@ -42,11 +45,25 @@ export class LibroPromptCellModel
42
45
  @prop()
43
46
  modelType?: string;
44
47
 
48
+ @prop()
49
+ prompt = '';
50
+
51
+ promptOutput?: string;
52
+
45
53
  @prop()
46
54
  chatKey?: string;
47
55
 
48
56
  @prop()
49
- variableName: string;
57
+ supportInterpreter?: 'dynamic' | 'immutable' | 'disable';
58
+
59
+ @prop()
60
+ interpreterEnabled?: boolean;
61
+
62
+ @prop()
63
+ interpreterCode?: string;
64
+
65
+ @prop()
66
+ variableName?: string;
50
67
 
51
68
  @prop()
52
69
  executing: boolean;
@@ -54,6 +71,7 @@ export class LibroPromptCellModel
54
71
  hasOutputHidden: boolean;
55
72
  @prop()
56
73
  hasOutputsScrolled: boolean;
74
+ _interpreterEditMode = false;
57
75
 
58
76
  // Emitter Msg
59
77
  msgChangeEmitter: Emitter<any>;
@@ -68,16 +86,23 @@ export class LibroPromptCellModel
68
86
  variableName: this.variableName,
69
87
  chatKey: this.chatKey,
70
88
  record: this.record,
71
- value: this.value,
89
+ value: this._interpreterEditMode ? this.prompt : this.value,
72
90
  cellId: this.id,
91
+ interpreterCode: this.interpreterCode,
92
+ supportInterpreter: this.supportInterpreter,
93
+ interpreterEnabled: this.interpreterEnabled,
73
94
  };
74
95
  }
75
96
 
76
- override set decodeObject(value) {
77
- super.decodeObject = value;
78
- this.variableName = value.variableName;
79
- this.chatKey = value.chatKey;
80
- this.record = value.record;
97
+ override set decodeObject(data: PromptDecodedFormatter) {
98
+ this.value = data.value;
99
+ this.prompt = data.value;
100
+ this.variableName = data.variableName;
101
+ this.chatKey = data.chatKey;
102
+ this.record = data.record;
103
+ this.interpreterCode = data.interpreterCode;
104
+ this.supportInterpreter = data.supportInterpreter;
105
+ this.interpreterEnabled = data.interpreterEnabled;
81
106
  }
82
107
 
83
108
  viewManager: ViewManager;
@@ -96,19 +121,25 @@ export class LibroPromptCellModel
96
121
  this.libroFormatType = 'formatter-prompt-magic';
97
122
  this.mimeType = 'application/vnd.libro.prompt+json';
98
123
  this.metadata = {
124
+ interpreter: {},
99
125
  ...options?.cell?.metadata,
100
126
  libroFormatter: this.libroFormatType,
101
127
  };
102
128
  }
103
129
 
104
130
  override toJSON(): Omit<ICodeCell, 'outputs'> {
131
+ this.metadata.interpreter = {
132
+ ...this.metadata.interpreter,
133
+ interpreter_code: this.interpreterCode,
134
+ support_interpreter: this.supportInterpreter,
135
+ interpreter_enabled: this.interpreterEnabled,
136
+ };
105
137
  return {
106
138
  id: this.id,
107
139
  cell_type: this.type,
108
140
  source: this.source,
109
141
  metadata: this.metadata,
110
142
  execution_count: this.executeCount,
111
- // outputs: this.outputs,
112
143
  };
113
144
  }
114
145
 
@@ -1,3 +1,4 @@
1
+ import type { PartialJSONObject } from '@difizen/libro-common';
1
2
  import type { CellOptions } from '@difizen/libro-core';
2
3
 
3
4
  import type { LibroPromptCellModel } from './prompt-cell-model.js';
@@ -6,3 +7,10 @@ export type LibroPromptCellModelFactory = (
6
7
  options: CellOptions,
7
8
  ) => LibroPromptCellModel;
8
9
  export const LibroPromptCellModelFactory = Symbol('LibroPromptCellModelFactory');
10
+
11
+ export interface InterpreterMeta extends PartialJSONObject {
12
+ support_interpreter?: 'dynamic' | 'immutable' | 'disable';
13
+ interpreter_enabled?: boolean;
14
+ interpreter_code?: string;
15
+ interpreter_text?: string;
16
+ }
@@ -6,4 +6,7 @@ export class PromptScript {
6
6
  chat_object_manager.dump_kernel_list_json()`;
7
7
  public readonly getChatRecoreds: string = `from libro_ai import chat_record_provider
8
8
  chat_record_provider.get_records()`;
9
+ switchInterpreterMode = (key: string, mode: boolean) => {
10
+ return `from libro_ai import chat_object_manager\nexecutor = chat_object_manager.get_executor('${key}')\nexecutor.set_interpreter_support(${mode ? 'True' : 'False'})`;
11
+ };
9
12
  }