Skip to content

Commit 9110a93

Browse files
committed
✨ feat: ChatGPT Prompt 支持配置在单独文件里
1 parent c115120 commit 9110a93

File tree

14 files changed

+119
-41
lines changed

14 files changed

+119
-41
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626

2727
![gpt1.png](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c121c1cf260e4f96b2b68bbcdc3cd5d4~tplv-k3u1fbpfcp-zoom-1.image?)
2828

29-
拉到最底部,配置 chatGPT 字段:
29+
配置 chatGPT 字段:
3030

31-
![gpt2.png](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/b9366c6928c445eb984e9eb036c3d55d~tplv-k3u1fbpfcp-zoom-1.image?)
31+
![image.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5552fb27cb3947b89fb91d948ca3e0aa~tplv-k3u1fbpfcp-zoom-1.image?)
3232

3333
commandPrompt 既右键菜单选择模板后发送的内容,支持 EJS 模板语法。
3434

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "lowcode",
44
"description": "lowcode tool, support ChatGPT",
55
"author": "wjkang <[email protected]>",
6-
"version": "1.4.4",
6+
"version": "1.4.5",
77
"icon": "asset/icon.png",
88
"publisher": "wjkang",
99
"repository": "https://github.com/lowcoding/lowcode-vscode",
@@ -290,4 +290,4 @@
290290
"tar": "^6.0.5",
291291
"typescript-json-schema": "^0.50.1"
292292
}
293-
}
293+
}

src/commands/chatGPT.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export const registerChatGPTCommand = (context: vscode.ExtensionContext) => {
3131
'lowcode.askChatGPTWithTemplate',
3232
async () => {
3333
const templateList = getSnippets().filter(
34-
(s) => s.preview.chatGPT && s.preview.chatGPT.commandPrompt,
34+
(s) =>
35+
s.commandPrompt ||
36+
(s.preview.chatGPT && s.preview.chatGPT.commandPrompt),
3537
);
3638
if (templateList.length === 0) {
3739
vscode.window.showErrorMessage('请配置 Prompt 模板');
@@ -50,7 +52,7 @@ export const registerChatGPTCommand = (context: vscode.ExtensionContext) => {
5052
rawClipboardText: getClipboardText() || '',
5153
};
5254
const code = compileEjs(
53-
template!.preview.chatGPT?.commandPrompt!,
55+
template?.commandPrompt || template!.preview.chatGPT?.commandPrompt!,
5456
model as Model,
5557
);
5658
showChatGPTView({

src/utils/materials.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,24 @@ export const getLocalMaterials = (
3232
viewPrompt?: string;
3333
};
3434
};
35+
commandPrompt?: string; // 优先级比 preview.chatGPT 高
36+
viewPrompt?: string; // 优先级比 preview.chatGPT 高
3537
template: string;
3638
}[] = [];
3739
try {
3840
materials = fs.readdirSync(materialsFullPath).map((s) => {
3941
const fullPath = path.join(materialsFullPath, s);
4042
let model = {} as any;
4143
let schema = {} as any;
42-
let preview = { img: '', category: [], schema: 'form-render' };
44+
let preview = {
45+
img: '',
46+
category: [],
47+
schema: 'form-render',
48+
chatGPT: { commandPrompt: '', viewPrompt: '' },
49+
};
4350
let template = '';
51+
let commandPrompt = '';
52+
let viewPrompt = '';
4453
try {
4554
model = JSON.parse(
4655
getFileContent(path.join(fullPath, 'config', 'model.json'), true),
@@ -56,13 +65,32 @@ export const getLocalMaterials = (
5665
getFileContent(path.join(fullPath, 'config', 'preview.json'), true),
5766
);
5867
} catch {}
68+
try {
69+
commandPrompt = getFileContent(
70+
path.join(fullPath, 'config', 'commandPrompt.ejs'),
71+
true,
72+
);
73+
} catch {}
74+
try {
75+
viewPrompt = getFileContent(
76+
path.join(fullPath, 'config', 'viewPrompt.ejs'),
77+
true,
78+
);
79+
} catch {}
5980
if (!preview.img) {
6081
preview.img =
6182
'https://gitee.com/img-host/img-host/raw/master/2020/11/05/1604587962875.jpg';
6283
}
6384
if (!preview.schema) {
6485
preview.schema = 'form-render';
6586
}
87+
if (!commandPrompt) {
88+
commandPrompt = preview.chatGPT?.commandPrompt;
89+
}
90+
if (!viewPrompt) {
91+
viewPrompt = preview.chatGPT?.viewPrompt;
92+
}
93+
6694
if (type === 'snippets') {
6795
try {
6896
template = getFileContent(
@@ -100,6 +128,8 @@ export const getLocalMaterials = (
100128
schema,
101129
preview,
102130
template,
131+
commandPrompt,
132+
viewPrompt,
103133
};
104134
});
105135
} catch {}
@@ -127,7 +157,7 @@ export const getCodeTemplateListFromFiles = () => {
127157
};
128158

129159
/**
130-
* 获取 codeTemplate 目录下ejs文件作为代码模板并且合并代码片段
160+
* 获取 codeTemplate 目录下ejs文件作为代码模板并且合并代码片段 (兼容以前旧代码)
131161
*
132162
* @export
133163
* @returns
@@ -152,6 +182,8 @@ export function getSnippets() {
152182
viewPrompt?: string;
153183
};
154184
};
185+
commandPrompt?: string; // 优先级比 preview.chatGPT 高
186+
viewPrompt?: string; // 优先级比 preview.chatGPT 高
155187
template: string;
156188
}[] = getCodeTemplateListFromFiles().map((s) => ({
157189
path: s.name,

src/webview/controllers/block.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export const createBlock = (
1010
model: string;
1111
schema: string;
1212
preview: string;
13+
commandPrompt: string;
14+
viewPrompt: string;
1315
}>,
1416
) => {
1517
const blockPath = path.join(blockMaterialsPath, message.data.name);
@@ -32,6 +34,14 @@ export const createBlock = (
3234
path.join(blockPath, 'config', 'preview.json'),
3335
message.data.preview,
3436
);
37+
fs.outputFileSync(
38+
path.join(blockPath, 'config', 'commandPrompt.ejs'),
39+
message.data.commandPrompt,
40+
);
41+
fs.outputFileSync(
42+
path.join(blockPath, 'config', 'viewPrompt.ejs'),
43+
message.data.viewPrompt,
44+
);
3545
fs.outputFileSync(
3646
path.join(blockPath, 'script', 'index.js'),
3747
`const path = require("path");

src/webview/controllers/snippet.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export const addSnippets = (
1515
model: string;
1616
schema: string;
1717
preview: string;
18+
commandPrompt: string;
19+
viewPrompt: string;
1820
}>,
1921
) => {
2022
const snippetPath = path.join(snippetMaterialsPath, message.data.name);
@@ -34,6 +36,14 @@ export const addSnippets = (
3436
path.join(snippetPath, 'config', 'preview.json'),
3537
message.data.preview,
3638
);
39+
fs.outputFileSync(
40+
path.join(snippetPath, 'config', 'commandPrompt.ejs'),
41+
message.data.commandPrompt,
42+
);
43+
fs.outputFileSync(
44+
path.join(snippetPath, 'config', 'viewPrompt.ejs'),
45+
message.data.viewPrompt,
46+
);
3747
fs.outputFileSync(
3848
path.join(snippetPath, 'script', 'index.js'),
3949
`const path = require("path");

webview-react/src/components/HeaderControl/presenter.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useModel } from './model';
66
export const defaultSchema: Record<string, { model: string; schema: string }> =
77
{
88
'form-render': {
9-
model: JSON.stringify({ name: '6666' }, null, 2),
9+
model: JSON.stringify({ name: 'lowcode' }, null, 2),
1010
schema: JSON.stringify(
1111
{
1212
formSchema: {
@@ -37,7 +37,7 @@ export const defaultSchema: Record<string, { model: string; schema: string }> =
3737
),
3838
},
3939
amis: {
40-
model: JSON.stringify({ name: '6666' }, null, 2),
40+
model: JSON.stringify({ name: 'lowcode' }, null, 2),
4141
schema: JSON.stringify(
4242
{
4343
formSchema: {
@@ -139,16 +139,14 @@ export const usePresenter = () => {
139139
],
140140
category: [],
141141
schema: model.blockModal.schemaType,
142-
chatGPT: {
143-
commandPrompt:
144-
'<%- rawSelectedText || rawClipboardText %> 解释这段代码的意思',
145-
viewPrompt:
146-
'<%- model %> \r\n将这段 json 中,中文 key 翻译为英文,使用驼峰语法,返回翻译后的markdown语法的代码块',
147-
},
148142
},
149143
null,
150144
2,
151145
),
146+
commandPrompt:
147+
'<%- rawSelectedText || rawClipboardText %>\r\n解释这段代码的意思',
148+
viewPrompt:
149+
'<%- model %> \r\n将这段 json 中,中文 key 翻译为英文,使用驼峰语法,\r\n返回翻译后的markdown语法的代码块',
152150
})
153151
.then(() => {
154152
closeBlockModal();

webview-react/src/pages/blocks/Detail/model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ const useModel = () => {
1111
description?: string;
1212
img?: string | string[];
1313
schema?: 'form-render' | 'formily' | 'amis';
14-
chatGPT?: { viewPrompt?: string };
1514
};
1615
template: string;
16+
viewPrompt?: string;
1717
}>({ schema: {}, model: {} } as any);
1818
const [materials, setMaterials] = useState<typeof selectedMaterial[]>([]);
1919
const [formData, setData] = useState({});

webview-react/src/pages/blocks/Detail/useController.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ const useController = () => {
4242
};
4343

4444
const handleAskChatGPT = () => {
45-
if (!model.selectedMaterial.preview.chatGPT?.viewPrompt) {
46-
message.warn('未配置 preview.chatGPT.viewPrompt');
45+
if (!model.selectedMaterial.viewPrompt) {
46+
message.warn('未配置 viewPrompt,直接输入 model');
4747
}
4848
askChatGPTWithEjsTemplate({
49-
template:
50-
model.selectedMaterial.preview.chatGPT?.viewPrompt || '<%- model %>',
49+
template: model.selectedMaterial.viewPrompt || '<%- model %>',
5150
model: { model: JSON.stringify(model.selectedMaterial.model, null, 2) },
5251
});
5352
};

webview-react/src/pages/snippets/AddSnippet/index.tsx

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { Button, Form, Input, message, Select, Space } from 'antd';
2+
import { Button, Form, Input, message, Select } from 'antd';
33
import { history, useModel } from 'umi';
44
import CodeMirror from '@/components/CodeMirror';
55
import useController from './useController';
@@ -14,12 +14,7 @@ export default () => {
1414

1515
return (
1616
<div>
17-
<Form
18-
{...{
19-
labelCol: { span: 4 },
20-
wrapperCol: { span: 14 },
21-
}}
22-
>
17+
<Form layout="vertical">
2318
<Form.Item label="名称" required>
2419
<Input
2520
value={model.formData.name}
@@ -46,6 +41,35 @@ export default () => {
4641
}}
4742
/>
4843
</Form.Item>
44+
<Form.Item label="ChatGPT CommandPrompt" tooltip="右键菜单 Prompt 模板">
45+
<CodeMirror
46+
domId="commandPromptCodeMirror"
47+
lint={false}
48+
value={model.formData.commandPrompt}
49+
onChange={(value) => {
50+
model.setFormData((s) => ({
51+
...s,
52+
commandPrompt: value,
53+
}));
54+
}}
55+
/>
56+
</Form.Item>
57+
<Form.Item
58+
label="ChatGPT ViewPrompt"
59+
tooltip="可视化动态表单 Prompt 模板"
60+
>
61+
<CodeMirror
62+
domId="viewPromptCodeMirror"
63+
lint={false}
64+
value={model.formData.viewPrompt}
65+
onChange={(value) => {
66+
model.setFormData((s) => ({
67+
...s,
68+
viewPrompt: value,
69+
}));
70+
}}
71+
/>
72+
</Form.Item>
4973
<Form.Item label="模板数据">
5074
<CodeMirror
5175
domId="modelCodeMirror"

0 commit comments

Comments
 (0)