@comate/zulu 0.2.1-beta.2 → 0.2.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.
- package/README.md +1 -0
- package/comate-engine/bin/comate.js +9 -0
- package/comate-engine/node_modules/@comate/plugin-engine/dist/index.js +8 -8
- package/comate-engine/node_modules/@comate/plugin-host/dist/index.js +1 -1
- package/comate-engine/node_modules/@comate/plugin-host/dist/main.js +1 -1
- package/comate-engine/node_modules/@comate/plugin-host/dist/user-DQgjkMe9.js +44 -0
- package/comate-engine/node_modules/@comate/plugin-shared-internals/dist/index.d.ts +24 -6
- package/comate-engine/node_modules/@comate/plugin-shared-internals/dist/index.js +8 -8
- package/comate-engine/node_modules/@comate/preview-proxy/dist/index.js +59 -72
- package/comate-engine/node_modules/@comate/preview-proxy/static/comate-preview-injector.css +1 -1
- package/comate-engine/node_modules/@comate/preview-proxy/static/comate-preview-injector.js +1 -1
- package/comate-engine/node_modules/better-sqlite3/node_modules/.bin/prebuild-install +2 -2
- package/comate-engine/plugins/demo-feature/dist/index.js +337 -30
- package/comate-engine/server.js +11 -11
- package/dist/bundle/index.js +8 -8
- package/package.json +1 -1
- package/comate-engine/node_modules/@comate/plugin-host/dist/user-Buhpnssj.js +0 -44
- package/comate-engine/plugins/aiscan/assets/icon.svg +0 -12
- package/comate-engine/plugins/aiscan/dist/index.js +0 -53861
- package/comate-engine/plugins/aiscan/package.json +0 -67
- package/comate-engine/plugins/appdev/assets/icon.png +0 -0
- package/comate-engine/plugins/appdev/dist/help.md +0 -12
- package/comate-engine/plugins/appdev/dist/index.d.ts +0 -2
- package/comate-engine/plugins/appdev/dist/index.js +0 -194
- package/comate-engine/plugins/appdev/dist/providers/err.d.ts +0 -8
- package/comate-engine/plugins/appdev/dist/providers/err.js +0 -37
- package/comate-engine/plugins/appdev/dist/providers/fallback.d.ts +0 -5
- package/comate-engine/plugins/appdev/dist/providers/fallback.js +0 -28
- package/comate-engine/plugins/appdev/dist/providers/help.d.ts +0 -8
- package/comate-engine/plugins/appdev/dist/providers/help.js +0 -19
- package/comate-engine/plugins/appdev/dist/providers/medusa.d.ts +0 -8
- package/comate-engine/plugins/appdev/dist/providers/medusa.js +0 -39
- package/comate-engine/plugins/appdev/dist/providers/qa.d.ts +0 -8
- package/comate-engine/plugins/appdev/dist/providers/qa.js +0 -33
- package/comate-engine/plugins/appdev/package.json +0 -67
- package/comate-engine/plugins/bfc/assets/bfc-icon.png +0 -0
- package/comate-engine/plugins/bfc/dist/config/config.d.ts +0 -4
- package/comate-engine/plugins/bfc/dist/config/config.js +0 -4
- package/comate-engine/plugins/bfc/dist/help.md +0 -11
- package/comate-engine/plugins/bfc/dist/index.d.ts +0 -2
- package/comate-engine/plugins/bfc/dist/index.js +0 -452
- package/comate-engine/plugins/bfc/dist/providers/bfcInstructMode.d.ts +0 -8
- package/comate-engine/plugins/bfc/dist/providers/bfcInstructMode.js +0 -77
- package/comate-engine/plugins/bfc/dist/providers/genAbnormalCode.d.ts +0 -8
- package/comate-engine/plugins/bfc/dist/providers/genAbnormalCode.js +0 -71
- package/comate-engine/plugins/bfc/dist/providers/genBfcCode.d.ts +0 -8
- package/comate-engine/plugins/bfc/dist/providers/genBfcCode.js +0 -74
- package/comate-engine/plugins/bfc/dist/providers/getBatchCases.d.ts +0 -10
- package/comate-engine/plugins/bfc/dist/providers/getBatchCases.js +0 -71
- package/comate-engine/plugins/bfc/dist/providers/help.d.ts +0 -8
- package/comate-engine/plugins/bfc/dist/providers/help.js +0 -19
- package/comate-engine/plugins/bfc/dist/service/generateService.d.ts +0 -14
- package/comate-engine/plugins/bfc/dist/service/generateService.js +0 -27
- package/comate-engine/plugins/bfc/package.json +0 -85
- package/comate-engine/plugins/bpm/assets/bpm.png +0 -0
- package/comate-engine/plugins/bpm/dist/index.js +0 -313
- package/comate-engine/plugins/bpm/package.json +0 -65
- package/comate-engine/plugins/casebuilder/assets/icon.svg +0 -1
- package/comate-engine/plugins/casebuilder/dist/help.md +0 -33
- package/comate-engine/plugins/casebuilder/dist/index.js +0 -837
- package/comate-engine/plugins/casebuilder/package.json +0 -80
- package/comate-engine/plugins/centrino/assets/icon.png +0 -0
- package/comate-engine/plugins/centrino/dist/help.md +0 -30
- package/comate-engine/plugins/centrino/dist/index.js +0 -1046
- package/comate-engine/plugins/centrino/package.json +0 -68
- package/comate-engine/plugins/cnap/assets/cnap.png +0 -0
- package/comate-engine/plugins/cnap/dist/help.md +0 -11
- package/comate-engine/plugins/cnap/dist/index.js +0 -171
- package/comate-engine/plugins/cnap/package.json +0 -65
- package/comate-engine/plugins/demo-timer/assets/icon.svg +0 -2
- package/comate-engine/plugins/demo-timer/dist/index.js +0 -463
- package/comate-engine/plugins/demo-timer/package.json +0 -73
- package/comate-engine/plugins/dev-tools/assets/icon.png +0 -0
- package/comate-engine/plugins/dev-tools/dist/help.md +0 -18
- package/comate-engine/plugins/dev-tools/dist/index.js +0 -166160
- package/comate-engine/plugins/dev-tools/package.json +0 -95
- package/comate-engine/plugins/devaux/assets/icon.svg +0 -1
- package/comate-engine/plugins/devaux/dist/help.md +0 -41
- package/comate-engine/plugins/devaux/dist/index.js +0 -74138
- package/comate-engine/plugins/devaux/dist/static/tree-sitter-cpp.wasm +0 -0
- package/comate-engine/plugins/devaux/dist/static/tree-sitter-go.wasm +0 -0
- package/comate-engine/plugins/devaux/dist/static/tree-sitter.wasm +0 -0
- package/comate-engine/plugins/devaux/dist/tree-sitter.wasm +0 -0
- package/comate-engine/plugins/devaux/package.json +0 -83
- package/comate-engine/plugins/f2c/assets/icon.svg +0 -11
- package/comate-engine/plugins/f2c/dist/help.md +0 -13
- package/comate-engine/plugins/f2c/dist/index.js +0 -81787
- package/comate-engine/plugins/f2c/package.json +0 -56
- package/comate-engine/plugins/fcnap/assets/icon.png +0 -0
- package/comate-engine/plugins/fcnap/dist/help.md +0 -9
- package/comate-engine/plugins/fcnap/dist/index.js +0 -105
- package/comate-engine/plugins/fcnap/package.json +0 -50
- package/comate-engine/plugins/front-end-skills/assets/icon.svg +0 -1
- package/comate-engine/plugins/front-end-skills/dist/help.md +0 -17
- package/comate-engine/plugins/front-end-skills/dist/index.js +0 -8172
- package/comate-engine/plugins/front-end-skills/package.json +0 -79
- package/comate-engine/plugins/gdp/assets/icon.png +0 -0
- package/comate-engine/plugins/gdp/dist/help.md +0 -11
- package/comate-engine/plugins/gdp/dist/index.js +0 -53065
- package/comate-engine/plugins/gdp/package.json +0 -79
- package/comate-engine/plugins/git/assets/git.svg +0 -1
- package/comate-engine/plugins/git/assets/icon.svg +0 -1
- package/comate-engine/plugins/git/dist/help.md +0 -7
- package/comate-engine/plugins/git/dist/index.js +0 -32675
- package/comate-engine/plugins/git/package.json +0 -91
- package/comate-engine/plugins/harmonyos/assets/harmonyos.png +0 -0
- package/comate-engine/plugins/harmonyos/dist/index.js +0 -78
- package/comate-engine/plugins/harmonyos/package.json +0 -37
- package/comate-engine/plugins/iapi/assets/icon.png +0 -0
- package/comate-engine/plugins/iapi/dist/help.md +0 -13
- package/comate-engine/plugins/iapi/dist/index.js +0 -458
- package/comate-engine/plugins/iapi/dist/static/lang/tree-sitter-go.wasm +0 -0
- package/comate-engine/plugins/iapi/dist/static/lang/tree-sitter-java.wasm +0 -0
- package/comate-engine/plugins/iapi/dist/static/tree-sitter-go.wasm +0 -0
- package/comate-engine/plugins/iapi/dist/static/tree-sitter-java.wasm +0 -0
- package/comate-engine/plugins/iapi/dist/tree-sitter.wasm +0 -0
- package/comate-engine/plugins/iapi/package.json +0 -102
- package/comate-engine/plugins/icafe/assets/icafe.svg +0 -1
- package/comate-engine/plugins/icafe/dist/help.md +0 -5
- package/comate-engine/plugins/icafe/dist/index.js +0 -885
- package/comate-engine/plugins/icafe/package.json +0 -74
- package/comate-engine/plugins/ievalue/assets/iEValue.svg +0 -10
- package/comate-engine/plugins/ievalue/dist/index.js +0 -2920
- package/comate-engine/plugins/ievalue/package.json +0 -88
- package/comate-engine/plugins/ipipe/assets/iPipePlugin.png +0 -0
- package/comate-engine/plugins/ipipe/dist/help.md +0 -15
- package/comate-engine/plugins/ipipe/dist/index.js +0 -7264
- package/comate-engine/plugins/ipipe/package.json +0 -62
- package/comate-engine/plugins/jarvis/assets/icon.svg +0 -1
- package/comate-engine/plugins/jarvis/dist/help.md +0 -17
- package/comate-engine/plugins/jarvis/dist/index.js +0 -140416
- package/comate-engine/plugins/jarvis/package.json +0 -93
- package/comate-engine/plugins/mapsearch/assets/mapsearchicon.png +0 -0
- package/comate-engine/plugins/mapsearch/dist/help.md +0 -4
- package/comate-engine/plugins/mapsearch/dist/index.js +0 -145
- package/comate-engine/plugins/mapsearch/package.json +0 -53
- package/comate-engine/plugins/paddle/assets/icon.png +0 -0
- package/comate-engine/plugins/paddle/assets/paconvert-3.0.1-py3-none-any.whl +0 -0
- package/comate-engine/plugins/paddle/dist/help.md +0 -31
- package/comate-engine/plugins/paddle/dist/index.js +0 -1011
- package/comate-engine/plugins/paddle/package.json +0 -129
- package/comate-engine/plugins/robot/assets/Robot.png +0 -0
- package/comate-engine/plugins/robot/dist/index.js +0 -288
- package/comate-engine/plugins/robot/package.json +0 -67
- package/comate-engine/plugins/security/assets/comate.png +0 -0
- package/comate-engine/plugins/security/dist/index.js +0 -18393
- package/comate-engine/plugins/security/package.json +0 -82
- package/comate-engine/plugins/smartapp/assets/icon.png +0 -0
- package/comate-engine/plugins/smartapp/dist/help.md +0 -4
- package/comate-engine/plugins/smartapp/dist/index.js +0 -113
- package/comate-engine/plugins/smartapp/package.json +0 -54
- package/comate-engine/plugins/testmate/assets/icon.svg +0 -1
- package/comate-engine/plugins/testmate/dist/help.md +0 -62
- package/comate-engine/plugins/testmate/dist/index.js +0 -21483
- package/comate-engine/plugins/testmate/package.json +0 -85
- package/comate-engine/plugins/tor/assets/TorPlugin.png +0 -0
- package/comate-engine/plugins/tor/dist/help.md +0 -22
- package/comate-engine/plugins/tor/dist/index.js +0 -807
- package/comate-engine/plugins/tor/package.json +0 -99
- package/comate-engine/plugins/weiyun/assets/icon.png +0 -0
- package/comate-engine/plugins/weiyun/dist/help.md +0 -11
- package/comate-engine/plugins/weiyun/dist/index.js +0 -100491
- package/comate-engine/plugins/weiyun/package.json +0 -55
- package/comate-engine/plugins/workcard/assets/favicon.png +0 -0
- package/comate-engine/plugins/workcard/dist/help.md +0 -30
- package/comate-engine/plugins/workcard/dist/index.js +0 -490
- package/comate-engine/plugins/workcard/package.json +0 -95
|
@@ -1,2920 +0,0 @@
|
|
|
1
|
-
import { FallbackProvider, StringChunkStream, SkillProvider, ElementChunkStream } from '@comate/plugin-host';
|
|
2
|
-
import fs from 'node:fs';
|
|
3
|
-
import Path from 'node:path';
|
|
4
|
-
import ChildProcess from 'node:child_process';
|
|
5
|
-
import Path$1 from 'path';
|
|
6
|
-
import fs$1 from 'fs';
|
|
7
|
-
|
|
8
|
-
function jsx(type, props) {
|
|
9
|
-
// 按照React的逻辑,`boolean`、`null`、`undefined`是不会显示出来的
|
|
10
|
-
const children = Array.isArray(props.children) ? props.children.filter((v)=>v != null && typeof v !== 'boolean') : props.children;
|
|
11
|
-
return {
|
|
12
|
-
type,
|
|
13
|
-
...props,
|
|
14
|
-
children
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const config = {
|
|
19
|
-
pluginName: 'Prompt',
|
|
20
|
-
token: 'G9js+A7jZfZXOhPv2kOMp1E0y71ED3pDGFI7AVRWoUo=',
|
|
21
|
-
outputDir: '.comate/ievalue',
|
|
22
|
-
/** 是否开启调试模式,开启后将打印出更多调试信息 */ isDebug: true,
|
|
23
|
-
/** 调试日志文件名 */ debugLogFile: 'debug.log',
|
|
24
|
-
/** 普通日志文件名 */ generalLogFile: 'general.log',
|
|
25
|
-
/** 当前环境 */ env: "online"
|
|
26
|
-
};
|
|
27
|
-
const host = 'http://ievalue-api.baidu-int.com';
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @file 所有引用到的用户文档的网址
|
|
31
|
-
* 统一管理,方便维护
|
|
32
|
-
*/ const USER_DOCS = {
|
|
33
|
-
/** 脚本解释器 */ interpreter: 'https://ku.baidu-int.com/knowledge/HFVrC7hq1Q/pKzJfZczuc/DBLKIO3bMt/AhINgSaBqqD_Ku',
|
|
34
|
-
/** userSetting 中的 API Key */ getAPIKey: 'https://ku.baidu-int.com/knowledge/HFVrC7hq1Q/_SKPgSwp2G/Hb6OQz5Jc7/1EFTDCIbt1kuRI#anchor-9d082e80-706d-11ef-ab78-f310a5fa9930',
|
|
35
|
-
/** userSetting 中的 exportFirstCode */ exportFirstCode: 'https://ku.baidu-int.com/knowledge/HFVrC7hq1Q/_SKPgSwp2G/Hb6OQz5Jc7/1EFTDCIbt1kuRI#anchor-4042a080-7069-11ef-bc51-6dc3e97824b5',
|
|
36
|
-
metaPrompt: 'https://ku.baidu-int.com/knowledge/HFVrC7hq1Q/_SKPgSwp2G/Hb6OQz5Jc7/1EFTDCIbt1kuRI#anchor-43b0d590-a0d7-11ef-aa70-9b9f6ad4150b',
|
|
37
|
-
modelOptions: 'https://ku.baidu-int.com/knowledge/HFVrC7hq1Q/_SKPgSwp2G/Hb6OQz5Jc7/1EFTDCIbt1kuRI#anchor-3d9c1ff0-a0ed-11ef-bac8-7d68aa7f6b87',
|
|
38
|
-
/** 用户手册 */ userManual: 'https://ku.baidu-int.com/knowledge/HFVrC7hq1Q/_SKPgSwp2G/Hb6OQz5Jc7/1EFTDCIbt1kuRI'
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
class HelpFallbackProvider extends FallbackProvider {
|
|
42
|
-
static{
|
|
43
|
-
this.description = '介绍插件能力与开发方式';
|
|
44
|
-
}
|
|
45
|
-
async *handleQuery() {
|
|
46
|
-
const stream = new StringChunkStream();
|
|
47
|
-
yield stream.flush(/*#__PURE__*/ jsx("h1", {
|
|
48
|
-
children: "插件介绍"
|
|
49
|
-
}));
|
|
50
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
51
|
-
children: [
|
|
52
|
-
config.pluginName,
|
|
53
|
-
"插件支持自定义提示词,智能生成、执行、诊断提示词。"
|
|
54
|
-
]
|
|
55
|
-
}));
|
|
56
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
57
|
-
children: [
|
|
58
|
-
"详情请查看",
|
|
59
|
-
/*#__PURE__*/ jsx("a", {
|
|
60
|
-
href: USER_DOCS.userManual,
|
|
61
|
-
children: "用户文档"
|
|
62
|
-
})
|
|
63
|
-
]
|
|
64
|
-
}));
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const skills = {
|
|
69
|
-
runGPT: {
|
|
70
|
-
name: 'run-gpt',
|
|
71
|
-
description: '调用 chatgpt'
|
|
72
|
-
},
|
|
73
|
-
runEB: {
|
|
74
|
-
name: 'run-eb',
|
|
75
|
-
description: '调用文心大模型'
|
|
76
|
-
},
|
|
77
|
-
runPrompt: {
|
|
78
|
-
name: 'run-prompt',
|
|
79
|
-
displayName: '执行提示词',
|
|
80
|
-
description: '运行提示词生成程序'
|
|
81
|
-
},
|
|
82
|
-
generatePromptByDescription: {
|
|
83
|
-
name: 'generate-prompt-by-description',
|
|
84
|
-
description: '根据描述生成提示词'
|
|
85
|
-
},
|
|
86
|
-
getPrompt: {
|
|
87
|
-
name: 'get-prompt',
|
|
88
|
-
description: '下载提示词内容到本地'
|
|
89
|
-
},
|
|
90
|
-
// 根据示例生成提示词
|
|
91
|
-
generatePromptByExample: {
|
|
92
|
-
name: 'generate-prompt-by-example',
|
|
93
|
-
description: '根据示例生成提示词'
|
|
94
|
-
},
|
|
95
|
-
diagnosisPrompt: {
|
|
96
|
-
name: 'diagnosis-prompt',
|
|
97
|
-
description: '对提示词进行诊断'
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
function parseJsonc(jsoncString) {
|
|
102
|
-
const stripComments = (input)=>{
|
|
103
|
-
let inString = false;
|
|
104
|
-
let inComment = false;
|
|
105
|
-
let inMultilineComment = false;
|
|
106
|
-
let newStr = '';
|
|
107
|
-
let char = '';
|
|
108
|
-
let nextChar = '';
|
|
109
|
-
for(let i = 0; i < input.length; i++){
|
|
110
|
-
char = input[i];
|
|
111
|
-
nextChar = input[i + 1];
|
|
112
|
-
if (!inComment && !inMultilineComment) {
|
|
113
|
-
if (char === '"') {
|
|
114
|
-
inString = !inString;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
if (!inString) {
|
|
118
|
-
if (!inComment && !inMultilineComment) {
|
|
119
|
-
if (char === '/' && nextChar === '/') {
|
|
120
|
-
inComment = true;
|
|
121
|
-
i++;
|
|
122
|
-
} else if (char === '/' && nextChar === '*') {
|
|
123
|
-
inMultilineComment = true;
|
|
124
|
-
i++;
|
|
125
|
-
} else {
|
|
126
|
-
newStr += char;
|
|
127
|
-
}
|
|
128
|
-
} else if (inComment && char === '\n') {
|
|
129
|
-
inComment = false;
|
|
130
|
-
newStr += char;
|
|
131
|
-
} else if (inMultilineComment && char === '*' && nextChar === '/') {
|
|
132
|
-
inMultilineComment = false;
|
|
133
|
-
i++;
|
|
134
|
-
}
|
|
135
|
-
} else {
|
|
136
|
-
newStr += char;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
return newStr;
|
|
140
|
-
};
|
|
141
|
-
const strippedJson = stripComments(jsoncString);
|
|
142
|
-
return JSON.parse(strippedJson);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* 带注释和顺序的 JSON 格式化工具
|
|
147
|
-
* @template T 数据类型
|
|
148
|
-
*/ class JSONTemplate {
|
|
149
|
-
/**
|
|
150
|
-
* @param schema 预期格式,包括 key 和对应的注释
|
|
151
|
-
*/ constructor(schema){
|
|
152
|
-
this.schema = schema;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* 格式化对象为带注释的 JSON 字符串
|
|
156
|
-
* @param obj 要格式化的对象
|
|
157
|
-
* @returns 格式化后的 JSON 字符串
|
|
158
|
-
*/ format(obj) {
|
|
159
|
-
const lines = [
|
|
160
|
-
'{'
|
|
161
|
-
];
|
|
162
|
-
const fullSchema = [
|
|
163
|
-
...this.schema,
|
|
164
|
-
...Object.keys(obj).filter((key)=>!this.schema.some((item)=>item.key === key)).map((key)=>({
|
|
165
|
-
key: key
|
|
166
|
-
}))
|
|
167
|
-
];
|
|
168
|
-
fullSchema.forEach(({ key, comment, blockComment }, index)=>{
|
|
169
|
-
const value = obj[key];
|
|
170
|
-
const formattedValue = JSON.stringify(value);
|
|
171
|
-
if (blockComment) {
|
|
172
|
-
lines.push(` /** ${blockComment} */`);
|
|
173
|
-
}
|
|
174
|
-
if (comment) {
|
|
175
|
-
lines.push(` // ${comment}`);
|
|
176
|
-
}
|
|
177
|
-
const valueLine = ` "${String(key)}": ${formattedValue}${index < fullSchema.length - 1 ? ',' : ''}`;
|
|
178
|
-
lines.push(valueLine);
|
|
179
|
-
});
|
|
180
|
-
lines.push('}');
|
|
181
|
-
return lines.join('\n');
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* 解析带注释的 JSON 字符串
|
|
185
|
-
* @param jsonc 带注释的 JSON 字符串
|
|
186
|
-
* @returns 解析后的对象
|
|
187
|
-
*/ parse(jsonc) {
|
|
188
|
-
try {
|
|
189
|
-
return parseJsonc(jsonc);
|
|
190
|
-
} catch (error) {
|
|
191
|
-
if (error instanceof Error) {
|
|
192
|
-
throw new Error('无效的 JSONC 格式\n' + jsonc + '\n' + error.message);
|
|
193
|
-
}
|
|
194
|
-
throw new Error('无效的 JSONC 格式');
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const fsp$7 = fs.promises;
|
|
200
|
-
class FileState {
|
|
201
|
-
constructor(options){
|
|
202
|
-
this.data = null;
|
|
203
|
-
this.path = options.path;
|
|
204
|
-
this.jsonTemplate = options.jsonTemplate || new JSONTemplate([]);
|
|
205
|
-
if (!fs.existsSync(this.path)) {
|
|
206
|
-
const dir = Path.dirname(this.path);
|
|
207
|
-
if (!fs.existsSync(dir)) {
|
|
208
|
-
fs.mkdirSync(dir, {
|
|
209
|
-
recursive: true
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
fs.writeFileSync(this.path, this.jsonTemplate.format(options.defaultData));
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
async getItem(key) {
|
|
216
|
-
if (!this.data) {
|
|
217
|
-
await this.loadData();
|
|
218
|
-
}
|
|
219
|
-
return this.data ? this.data[key] : null;
|
|
220
|
-
}
|
|
221
|
-
async setItem(key, value) {
|
|
222
|
-
if (!this.data) {
|
|
223
|
-
await this.loadData();
|
|
224
|
-
}
|
|
225
|
-
if (this.data) {
|
|
226
|
-
this.data[key] = value;
|
|
227
|
-
await this.saveData();
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
async loadData() {
|
|
231
|
-
try {
|
|
232
|
-
const content = await fsp$7.readFile(this.path, 'utf-8');
|
|
233
|
-
this.data = this.jsonTemplate.parse(content);
|
|
234
|
-
} catch (error) {
|
|
235
|
-
this.data = {};
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
async saveData() {
|
|
239
|
-
if (this.data) {
|
|
240
|
-
const content = this.jsonTemplate.format(this.data);
|
|
241
|
-
await fsp$7.writeFile(this.path, content);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
const fsp$6 = fs.promises;
|
|
247
|
-
function toAbsolutePath(dir, path) {
|
|
248
|
-
return Path.isAbsolute(path) ? path : Path.resolve(dir, path);
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* 创建文件,如果文件不存在则创建,如果文件已存在则不做任何操作
|
|
252
|
-
* @param filePath 文件路径
|
|
253
|
-
* @param content 文件内容
|
|
254
|
-
* @returns 是否成功创建文件
|
|
255
|
-
*/ async function createFileIfNotExists(filePath, content) {
|
|
256
|
-
try {
|
|
257
|
-
// 使用 Path.normalize 来确保路径格式正确
|
|
258
|
-
const normalizedPath = Path.normalize(filePath);
|
|
259
|
-
await fsp$6.mkdir(Path.dirname(normalizedPath), {
|
|
260
|
-
recursive: true
|
|
261
|
-
});
|
|
262
|
-
// 尝试创建文件,如果文件已存在则会抛出错误
|
|
263
|
-
await fsp$6.writeFile(normalizedPath, content, {
|
|
264
|
-
flag: 'wx'
|
|
265
|
-
});
|
|
266
|
-
return true;
|
|
267
|
-
} catch (error) {
|
|
268
|
-
if (error.code === 'EEXIST') {
|
|
269
|
-
// 文件已存在,不做任何操作
|
|
270
|
-
return false;
|
|
271
|
-
}
|
|
272
|
-
// 其他错误,重新抛出
|
|
273
|
-
throw error;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* 创建或覆盖文件
|
|
278
|
-
* @param filePath 文件路径
|
|
279
|
-
* @param content 文件内容
|
|
280
|
-
* @returns 如果是覆盖现有文件返回true,如果是新建文件返回false
|
|
281
|
-
*/ async function createOrOverwriteFile(filePath, content) {
|
|
282
|
-
try {
|
|
283
|
-
// 使用 Path.normalize 来确保路径格式正确
|
|
284
|
-
const normalizedPath = Path.normalize(filePath);
|
|
285
|
-
await fsp$6.mkdir(Path.dirname(normalizedPath), {
|
|
286
|
-
recursive: true
|
|
287
|
-
});
|
|
288
|
-
// 检查文件是否存在
|
|
289
|
-
const fileExists = await fsp$6.access(normalizedPath).then(()=>true).catch(()=>false);
|
|
290
|
-
// 写入文件内容
|
|
291
|
-
await fsp$6.writeFile(normalizedPath, content);
|
|
292
|
-
// 返回是否为覆盖操作
|
|
293
|
-
return fileExists;
|
|
294
|
-
} catch (error) {
|
|
295
|
-
// 处理其他错误
|
|
296
|
-
console.error(`创建或覆盖文件失败: ${filePath}`, error);
|
|
297
|
-
throw error;
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
async function safeReadFile(filePath) {
|
|
301
|
-
if (!fs.existsSync(filePath)) {
|
|
302
|
-
return '';
|
|
303
|
-
}
|
|
304
|
-
return fsp$6.readFile(filePath, 'utf-8');
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* 读取目录内容
|
|
308
|
-
* @param dirPath 目录路径
|
|
309
|
-
* @returns 目录内容列表,文件夹不存在则返回空
|
|
310
|
-
*/ async function readDir(dirPath) {
|
|
311
|
-
// 检查目录是否存在
|
|
312
|
-
const dirExists = await fsp$6.access(dirPath).then(()=>true).catch(()=>false);
|
|
313
|
-
if (!dirExists) {
|
|
314
|
-
// 如果目录不存在,返回空数组
|
|
315
|
-
return;
|
|
316
|
-
}
|
|
317
|
-
// 读取并返回目录内容
|
|
318
|
-
return await fsp$6.readdir(dirPath);
|
|
319
|
-
}
|
|
320
|
-
/**
|
|
321
|
-
* 读取目录内容,如果目录不存在则创建
|
|
322
|
-
* @param dirPath 目录路径
|
|
323
|
-
* @returns 目录内容列表,如果是新创建的目录则返回空数组
|
|
324
|
-
*/ async function readOrCreateDir(dirPath) {
|
|
325
|
-
// 检查目录是否存在
|
|
326
|
-
const dirExists = await fsp$6.access(dirPath).then(()=>true).catch(()=>false);
|
|
327
|
-
// 如果目录不存在,创建它
|
|
328
|
-
if (!dirExists) {
|
|
329
|
-
await fsp$6.mkdir(dirPath, {
|
|
330
|
-
recursive: true
|
|
331
|
-
});
|
|
332
|
-
}
|
|
333
|
-
// 读取并返回目录内容
|
|
334
|
-
return await fsp$6.readdir(dirPath);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
/**
|
|
338
|
-
* 字符串参数的注入,替换掉 {{}} 中的参数
|
|
339
|
-
* @param template 原始模版
|
|
340
|
-
* @param params 参数映射关系
|
|
341
|
-
* @returns
|
|
342
|
-
*/ function injectParamsToText(template, params) {
|
|
343
|
-
let result = template;
|
|
344
|
-
for (let [key, value] of Object.entries(params)){
|
|
345
|
-
result = result.replace(`{{${key}}}`, value);
|
|
346
|
-
}
|
|
347
|
-
return result;
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* 处理特殊字段 index,这个是一个动态字段,作用是避免文件名重复
|
|
351
|
-
*/ async function injectSpecialParamIndex(template) {
|
|
352
|
-
const absolutePath = template;
|
|
353
|
-
const keyword = '{{index}}';
|
|
354
|
-
async function nextIndexFor(absolutePath, keyword) {
|
|
355
|
-
if (absolutePath.includes(keyword)) {
|
|
356
|
-
const [prefix, suffix] = absolutePath.split(keyword);
|
|
357
|
-
const dir = Path$1.dirname(absolutePath);
|
|
358
|
-
const files = await readDir(dir) || [];
|
|
359
|
-
const indexList = files.map((v)=>toAbsolutePath(dir, v)).filter((v)=>v.startsWith(prefix) && v.endsWith(suffix)).map((v)=>{
|
|
360
|
-
const indexStr = v.replace(prefix, '').replace(suffix, '');
|
|
361
|
-
if (indexStr.match(/^\d+$/)) {
|
|
362
|
-
return Number(indexStr);
|
|
363
|
-
}
|
|
364
|
-
return 0;
|
|
365
|
-
});
|
|
366
|
-
return Math.max(0, ...indexList) + 1;
|
|
367
|
-
}
|
|
368
|
-
return undefined;
|
|
369
|
-
}
|
|
370
|
-
const index = await nextIndexFor(absolutePath, keyword);
|
|
371
|
-
return absolutePath.replace(keyword, index?.toString() || '');
|
|
372
|
-
}
|
|
373
|
-
async function pathForResultCodeExport(filePathTemplate, params) {
|
|
374
|
-
// const filePathTemplate = await this.userSetting.getItem('exportFirstCode');
|
|
375
|
-
if (!filePathTemplate) {
|
|
376
|
-
return undefined;
|
|
377
|
-
}
|
|
378
|
-
// 如果结果是相对路径,转换为基于 cwd 的绝对路径
|
|
379
|
-
const absolutePath = toAbsolutePath(params.fileDir, injectParamsToText(filePathTemplate, params));
|
|
380
|
-
return injectSpecialParamIndex(absolutePath);
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
const fsp$5 = fs.promises;
|
|
384
|
-
function shell(command) {
|
|
385
|
-
return new Promise((resolve, reject)=>{
|
|
386
|
-
ChildProcess.exec(command, (error, stdout, stderr)=>{
|
|
387
|
-
if (error) {
|
|
388
|
-
reject(error);
|
|
389
|
-
} else if (stderr) {
|
|
390
|
-
reject(new Error(`Shell command error: ${stderr}`));
|
|
391
|
-
} else {
|
|
392
|
-
resolve(stdout);
|
|
393
|
-
}
|
|
394
|
-
});
|
|
395
|
-
});
|
|
396
|
-
}
|
|
397
|
-
/**
|
|
398
|
-
* Lisp 风格脚本代码解释器
|
|
399
|
-
*/ class Interpreter {
|
|
400
|
-
constructor(env, log, options){
|
|
401
|
-
this.env = env;
|
|
402
|
-
this.log = log;
|
|
403
|
-
this.options = options;
|
|
404
|
-
this.toString = (value)=>{
|
|
405
|
-
if (value !== null && value !== undefined) {
|
|
406
|
-
return `${value}`;
|
|
407
|
-
}
|
|
408
|
-
return '';
|
|
409
|
-
};
|
|
410
|
-
this.toAbsolutePath = (path)=>{
|
|
411
|
-
return toAbsolutePath(this.options.cwd, path);
|
|
412
|
-
};
|
|
413
|
-
this.// 如果结果是相对路径,转换为基于 cwd 的绝对路径
|
|
414
|
-
evalStatement = (statement)=>{
|
|
415
|
-
// @ts-ignore this.env 类型不对
|
|
416
|
-
const injectEnv = (template)=>injectParamsToText(template, this.env);
|
|
417
|
-
if (Array.isArray(statement)) {
|
|
418
|
-
const [functionName, ...rest] = statement;
|
|
419
|
-
switch(functionName){
|
|
420
|
-
case 'writeFile':
|
|
421
|
-
{
|
|
422
|
-
const [fileNameTemplate, content] = rest;
|
|
423
|
-
const fileName = injectEnv(this.toString(fileNameTemplate));
|
|
424
|
-
const absolutePath = this.toAbsolutePath(fileName);
|
|
425
|
-
const fileContent = injectEnv(this.toString(content));
|
|
426
|
-
this.log(`写入文件: ${absolutePath}, 内容: ${fileContent}`);
|
|
427
|
-
return fsp$5.writeFile(absolutePath, fileContent);
|
|
428
|
-
}
|
|
429
|
-
case 'appendFile':
|
|
430
|
-
{
|
|
431
|
-
const [fileNameTemplate, content] = rest;
|
|
432
|
-
const fileName = injectEnv(this.toString(fileNameTemplate));
|
|
433
|
-
const absolutePath = this.toAbsolutePath(fileName);
|
|
434
|
-
const fileContent = injectEnv(this.toString(content));
|
|
435
|
-
this.log(`追加文件: ${absolutePath}, 内容: ${fileContent}`);
|
|
436
|
-
return fsp$5.appendFile(absolutePath, fileContent);
|
|
437
|
-
}
|
|
438
|
-
case 'shell':
|
|
439
|
-
{
|
|
440
|
-
return (async ()=>{
|
|
441
|
-
try {
|
|
442
|
-
const [command, ...args] = rest.map((v)=>injectEnv(this.toString(v)));
|
|
443
|
-
const commandStr = [
|
|
444
|
-
'cd',
|
|
445
|
-
this.options.cwd,
|
|
446
|
-
'&&',
|
|
447
|
-
'sh',
|
|
448
|
-
command,
|
|
449
|
-
...args
|
|
450
|
-
].join(' ');
|
|
451
|
-
this.log(`执行 shell 命令: ${commandStr}`);
|
|
452
|
-
await shell(commandStr);
|
|
453
|
-
} catch (err) {
|
|
454
|
-
const errMsg = err instanceof Error ? err.message : `${err}`;
|
|
455
|
-
this.log(`shell 命令失败: ${errMsg}`);
|
|
456
|
-
}
|
|
457
|
-
})();
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
return statement;
|
|
462
|
-
};
|
|
463
|
-
}
|
|
464
|
-
static of(params) {
|
|
465
|
-
return new Interpreter(params.env, params.log, params.options);
|
|
466
|
-
}
|
|
467
|
-
async evalBlock(script) {
|
|
468
|
-
for (const statement of script){
|
|
469
|
-
try {
|
|
470
|
-
await this.evalStatement(statement);
|
|
471
|
-
} catch (err) {
|
|
472
|
-
const errMsg = err instanceof Error ? err.message : `${err}`;
|
|
473
|
-
return {
|
|
474
|
-
success: false,
|
|
475
|
-
error: err,
|
|
476
|
-
message: `执行语句失败\n语句: ${statement}\n错误: ${errMsg}`
|
|
477
|
-
};
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
return {
|
|
481
|
-
success: true,
|
|
482
|
-
length: script.length
|
|
483
|
-
};
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
/**
|
|
488
|
-
* 提示词相关的前置后置回调
|
|
489
|
-
*/ class PromptActions {
|
|
490
|
-
constructor(log, actionConfig){
|
|
491
|
-
this.log = log;
|
|
492
|
-
this.actionConfig = actionConfig;
|
|
493
|
-
}
|
|
494
|
-
static of(params) {
|
|
495
|
-
return new PromptActions(params.log, params.actionConfig);
|
|
496
|
-
}
|
|
497
|
-
static ofEmpty() {
|
|
498
|
-
return new PromptActions(()=>{}, {});
|
|
499
|
-
}
|
|
500
|
-
afterCreateDialog(env, options) {
|
|
501
|
-
const afterCreateDialog = this.actionConfig.afterCreateDialog;
|
|
502
|
-
if (!afterCreateDialog) {
|
|
503
|
-
return Promise.resolve({
|
|
504
|
-
success: true,
|
|
505
|
-
length: 0
|
|
506
|
-
});
|
|
507
|
-
}
|
|
508
|
-
this.log('开始执行 afterCreateDialog 脚本,内容为:' + afterCreateDialog.join(''));
|
|
509
|
-
const interpreter = Interpreter.of({
|
|
510
|
-
env,
|
|
511
|
-
log: this.log,
|
|
512
|
-
options
|
|
513
|
-
});
|
|
514
|
-
return interpreter.evalBlock(afterCreateDialog);
|
|
515
|
-
}
|
|
516
|
-
afterContinueDialog(env, options) {
|
|
517
|
-
const afterContinueDialog = this.actionConfig.afterContinueDialog;
|
|
518
|
-
if (!afterContinueDialog) {
|
|
519
|
-
return Promise.resolve({
|
|
520
|
-
success: true,
|
|
521
|
-
length: 0
|
|
522
|
-
});
|
|
523
|
-
}
|
|
524
|
-
this.log('开始执行 afterContinueDialog 脚本,内容为:' + afterContinueDialog.join(''));
|
|
525
|
-
const interpreter = Interpreter.of({
|
|
526
|
-
env,
|
|
527
|
-
log: this.log,
|
|
528
|
-
options
|
|
529
|
-
});
|
|
530
|
-
return interpreter.evalBlock(afterContinueDialog);
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
const emptyPromptData = {
|
|
535
|
-
text: '',
|
|
536
|
-
inputType: 'text',
|
|
537
|
-
variables: [],
|
|
538
|
-
modelParams: {},
|
|
539
|
-
promptCode: '',
|
|
540
|
-
promptID: 0,
|
|
541
|
-
promptName: '<无提示词>',
|
|
542
|
-
spaceCode: '',
|
|
543
|
-
spaceName: '',
|
|
544
|
-
submitStatus: 0,
|
|
545
|
-
versionID: 0,
|
|
546
|
-
versionName: '',
|
|
547
|
-
versionCreateTime: ''
|
|
548
|
-
};
|
|
549
|
-
const userInputPromptData = {
|
|
550
|
-
text: '{{IDE_QUERY}}',
|
|
551
|
-
inputType: 'text',
|
|
552
|
-
variables: [],
|
|
553
|
-
modelParams: {},
|
|
554
|
-
promptCode: '',
|
|
555
|
-
promptID: 0,
|
|
556
|
-
promptName: '<用户输入作为提示词>',
|
|
557
|
-
spaceCode: '',
|
|
558
|
-
spaceName: '',
|
|
559
|
-
submitStatus: 0,
|
|
560
|
-
versionID: 0,
|
|
561
|
-
versionName: '',
|
|
562
|
-
versionCreateTime: ''
|
|
563
|
-
};
|
|
564
|
-
/**
|
|
565
|
-
* 宏提示词
|
|
566
|
-
* 包括基础的提示词(prompt)和操作(action),其为用户提供完整业务功能。
|
|
567
|
-
* 例1 优化代码:提示词 优化{{IDE_CODE}},操作 AI输出写入当前文件
|
|
568
|
-
* 例2 生成提交:提示词 输出git message{{IDE_CODE}}, 操作 调起 commit.sh
|
|
569
|
-
* 后续可能还需要加快捷键触发等
|
|
570
|
-
*/ class MacroPrompt {
|
|
571
|
-
constructor(_data, _promptActions){
|
|
572
|
-
this._data = _data;
|
|
573
|
-
this._promptActions = _promptActions;
|
|
574
|
-
}
|
|
575
|
-
static of(params) {
|
|
576
|
-
return new MacroPrompt(params.data, params.promptActions);
|
|
577
|
-
}
|
|
578
|
-
static ofEmpty() {
|
|
579
|
-
return new MacroPrompt(emptyPromptData, PromptActions.ofEmpty());
|
|
580
|
-
}
|
|
581
|
-
static{
|
|
582
|
-
/**
|
|
583
|
-
* 特殊提示词,使用用户输入作为提示词
|
|
584
|
-
*/ this.defaultPromptWithUserInput = new MacroPrompt(userInputPromptData, PromptActions.ofEmpty());
|
|
585
|
-
}
|
|
586
|
-
static ofData(data) {
|
|
587
|
-
return new MacroPrompt(data, PromptActions.ofEmpty());
|
|
588
|
-
}
|
|
589
|
-
/**
|
|
590
|
-
* 提示词的唯一标识
|
|
591
|
-
* 本地提示词的 ID 都是 0,所以还需要加上 promptName
|
|
592
|
-
*/ get promptKey() {
|
|
593
|
-
return `${this._data.promptID}_${this._data.promptName}`;
|
|
594
|
-
}
|
|
595
|
-
/**
|
|
596
|
-
* 主要给根据示例或者描述生成提示词用
|
|
597
|
-
*/ static ofSimplePanda(params) {
|
|
598
|
-
return new MacroPrompt({
|
|
599
|
-
...emptyPromptData,
|
|
600
|
-
promptName: params.promptName,
|
|
601
|
-
text: params.text,
|
|
602
|
-
modelParams: {
|
|
603
|
-
system: params.system
|
|
604
|
-
}
|
|
605
|
-
}, PromptActions.ofEmpty());
|
|
606
|
-
}
|
|
607
|
-
get data() {
|
|
608
|
-
return this._data;
|
|
609
|
-
}
|
|
610
|
-
get actions() {
|
|
611
|
-
return this._promptActions;
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
function promptFileName(prompt) {
|
|
615
|
-
return `${prompt.promptName}_${prompt.versionName}-${prompt.spaceName}`;
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
const fsp$4 = fs.promises;
|
|
619
|
-
class PromptManager {
|
|
620
|
-
static{
|
|
621
|
-
this.SYSTEM_FILENAME = 'system.jsonc';
|
|
622
|
-
}
|
|
623
|
-
static{
|
|
624
|
-
this.META_PROMPT_FILENAME = 'meta-prompt.md';
|
|
625
|
-
}
|
|
626
|
-
static{
|
|
627
|
-
this.ACTIONS_FILENAME = 'actions.jsonc';
|
|
628
|
-
}
|
|
629
|
-
constructor(promptDir, log){
|
|
630
|
-
this.promptDir = promptDir;
|
|
631
|
-
this.log = log;
|
|
632
|
-
this.promptDataTemplate = new JSONTemplate([
|
|
633
|
-
{
|
|
634
|
-
key: 'modelParams',
|
|
635
|
-
blockComment: '模型参数'
|
|
636
|
-
},
|
|
637
|
-
{
|
|
638
|
-
key: 'promptName',
|
|
639
|
-
blockComment: '提示词名称,注意不要重名'
|
|
640
|
-
}
|
|
641
|
-
]);
|
|
642
|
-
}
|
|
643
|
-
async loadAllPrompts() {
|
|
644
|
-
try {
|
|
645
|
-
const files = await readOrCreateDir(this.promptDir);
|
|
646
|
-
const prompts = [];
|
|
647
|
-
for (const file of files){
|
|
648
|
-
const folderPath = Path.join(this.promptDir, file);
|
|
649
|
-
const stat = await fsp$4.stat(folderPath);
|
|
650
|
-
if (stat.isDirectory()) {
|
|
651
|
-
const promptData = await this.loadPromptFromFolder(folderPath);
|
|
652
|
-
if (promptData) {
|
|
653
|
-
prompts.push(promptData);
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
return prompts;
|
|
658
|
-
} catch (error) {
|
|
659
|
-
console.error(`加载提示词失败: ${error?.message}`);
|
|
660
|
-
throw error;
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
async getPromptByKey(promptKey) {
|
|
664
|
-
const allPrompts = await this.loadAllPrompts();
|
|
665
|
-
return [
|
|
666
|
-
...allPrompts,
|
|
667
|
-
MacroPrompt.defaultPromptWithUserInput
|
|
668
|
-
].find((v)=>promptKey === v.promptKey);
|
|
669
|
-
}
|
|
670
|
-
async loadPromptFromFolder(folderPath) {
|
|
671
|
-
const ieValuePath = Path.join(folderPath, PromptManager.SYSTEM_FILENAME);
|
|
672
|
-
const metaPromptPath = Path.join(folderPath, PromptManager.META_PROMPT_FILENAME);
|
|
673
|
-
if (await fs.existsSync(ieValuePath)) {
|
|
674
|
-
const promptData = await this.loadPromptData(ieValuePath);
|
|
675
|
-
const variables = await this.loadVariables(folderPath);
|
|
676
|
-
promptData.variables = variables;
|
|
677
|
-
if (await fs.existsSync(metaPromptPath)) {
|
|
678
|
-
promptData.text = await this.loadMetaPrompt(metaPromptPath);
|
|
679
|
-
}
|
|
680
|
-
const actions = await this.loadActions(folderPath);
|
|
681
|
-
const macroPrompt = MacroPrompt.of({
|
|
682
|
-
data: promptData,
|
|
683
|
-
promptActions: actions
|
|
684
|
-
});
|
|
685
|
-
return macroPrompt;
|
|
686
|
-
}
|
|
687
|
-
return null;
|
|
688
|
-
}
|
|
689
|
-
async loadPromptData(ieValuePath) {
|
|
690
|
-
const content = await fsp$4.readFile(ieValuePath, 'utf-8');
|
|
691
|
-
return this.promptDataTemplate.parse(content);
|
|
692
|
-
}
|
|
693
|
-
async loadMetaPrompt(metaPromptPath) {
|
|
694
|
-
return await fsp$4.readFile(metaPromptPath, 'utf-8');
|
|
695
|
-
}
|
|
696
|
-
async loadVariables(folderPath) {
|
|
697
|
-
const files = await fsp$4.readdir(folderPath);
|
|
698
|
-
const variables = [];
|
|
699
|
-
for (const file of files){
|
|
700
|
-
if (file !== PromptManager.SYSTEM_FILENAME && file !== PromptManager.META_PROMPT_FILENAME) {
|
|
701
|
-
const filePath = Path.join(folderPath, file);
|
|
702
|
-
const fileContent = await fsp$4.readFile(filePath, 'utf-8');
|
|
703
|
-
variables.push({
|
|
704
|
-
key: file,
|
|
705
|
-
value: fileContent,
|
|
706
|
-
type: 'string'
|
|
707
|
-
});
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
return variables;
|
|
711
|
-
}
|
|
712
|
-
async loadActions(folderPath) {
|
|
713
|
-
const actionsPath = Path.join(folderPath, PromptManager.ACTIONS_FILENAME);
|
|
714
|
-
return PromptActions.of({
|
|
715
|
-
// TODO: 动态类型检查
|
|
716
|
-
actionConfig: parseJsonc(await safeReadFile(actionsPath)) || {},
|
|
717
|
-
log: this.log
|
|
718
|
-
});
|
|
719
|
-
}
|
|
720
|
-
async savePrompt(prompt) {
|
|
721
|
-
const folderName = this.promptFoldName(prompt);
|
|
722
|
-
const folderPath = Path.join(this.promptDir, folderName);
|
|
723
|
-
try {
|
|
724
|
-
const promptDetailPath = Path.join(folderPath, PromptManager.SYSTEM_FILENAME);
|
|
725
|
-
const { variables, text, ...promptWithoutVariablesAndText } = prompt.data;
|
|
726
|
-
const promptData = prompt.data;
|
|
727
|
-
const formattedPromptData = this.promptDataTemplate.format({
|
|
728
|
-
promptCode: promptData.promptCode,
|
|
729
|
-
promptID: promptData.promptID,
|
|
730
|
-
promptName: promptData.promptName,
|
|
731
|
-
spaceCode: promptData.spaceCode,
|
|
732
|
-
spaceName: promptData.spaceName,
|
|
733
|
-
submitStatus: promptData.submitStatus,
|
|
734
|
-
versionID: promptData.versionID,
|
|
735
|
-
versionName: promptData.versionName,
|
|
736
|
-
versionCreateTime: promptData.versionCreateTime,
|
|
737
|
-
inputType: promptData.inputType,
|
|
738
|
-
modelParams: {
|
|
739
|
-
system: promptData.modelParams.system
|
|
740
|
-
}
|
|
741
|
-
});
|
|
742
|
-
const result = await this.createOrOverwriteFile(promptDetailPath, formattedPromptData);
|
|
743
|
-
const metaPromptPath = Path.join(folderPath, PromptManager.META_PROMPT_FILENAME);
|
|
744
|
-
await this.createOrOverwriteFile(metaPromptPath, text || '');
|
|
745
|
-
// 初始化回调文件
|
|
746
|
-
const actionsPath = Path.join(folderPath, PromptManager.ACTIONS_FILENAME);
|
|
747
|
-
await createFileIfNotExists(actionsPath, '{\n' + ` // ${USER_DOCS.interpreter}` + '\n}');
|
|
748
|
-
// 初始化参数文件
|
|
749
|
-
for (const variable of variables || []){
|
|
750
|
-
const paramFilePath = Path.join(folderPath, variable.key);
|
|
751
|
-
await this.createOrOverwriteFile(paramFilePath, variable.value);
|
|
752
|
-
}
|
|
753
|
-
return result ? 'overwrite' : 'create';
|
|
754
|
-
} catch (error) {
|
|
755
|
-
console.error(`保存提示词 ${prompt.data.promptName} 失败: ${error?.message}`);
|
|
756
|
-
throw error;
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
async createOrOverwriteFile(filePath, content) {
|
|
760
|
-
return createOrOverwriteFile(filePath, content);
|
|
761
|
-
}
|
|
762
|
-
promptFoldName(prompt) {
|
|
763
|
-
return `${prompt.data.promptName}_${prompt.data.promptCode}`;
|
|
764
|
-
}
|
|
765
|
-
pathForPromptIEValue(prompt) {
|
|
766
|
-
return Path.join(this.promptDir, this.promptFoldName(prompt), PromptManager.SYSTEM_FILENAME);
|
|
767
|
-
}
|
|
768
|
-
pathForPromptMeta(prompt) {
|
|
769
|
-
return Path.join(this.promptDir, this.promptFoldName(prompt), PromptManager.META_PROMPT_FILENAME);
|
|
770
|
-
}
|
|
771
|
-
pathForPromptActions(prompt) {
|
|
772
|
-
return Path.join(this.promptDir, this.promptFoldName(prompt), PromptManager.ACTIONS_FILENAME);
|
|
773
|
-
}
|
|
774
|
-
}
|
|
775
|
-
|
|
776
|
-
/**
|
|
777
|
-
* 执行对象的深复制。
|
|
778
|
-
*
|
|
779
|
-
* @param obj 需要被深复制的对象
|
|
780
|
-
* @returns 深复制后的新对象
|
|
781
|
-
*/ function deepCopy(obj) {
|
|
782
|
-
if (obj === null || typeof obj !== 'object') {
|
|
783
|
-
return obj;
|
|
784
|
-
}
|
|
785
|
-
if (obj instanceof Date) {
|
|
786
|
-
return new Date(obj.getTime());
|
|
787
|
-
}
|
|
788
|
-
if (obj instanceof Array) {
|
|
789
|
-
return obj.map((item)=>deepCopy(item));
|
|
790
|
-
}
|
|
791
|
-
if (obj instanceof Object) {
|
|
792
|
-
const copy = {};
|
|
793
|
-
Object.keys(obj).forEach((key)=>{
|
|
794
|
-
copy[key] = deepCopy(obj[key]);
|
|
795
|
-
});
|
|
796
|
-
return copy;
|
|
797
|
-
}
|
|
798
|
-
throw new Error('Unable to copy obj! Its type isn\'t supported.');
|
|
799
|
-
}
|
|
800
|
-
|
|
801
|
-
class MemoryState {
|
|
802
|
-
constructor(defaultData){
|
|
803
|
-
this.data = deepCopy(defaultData);
|
|
804
|
-
}
|
|
805
|
-
async getItem(key) {
|
|
806
|
-
return this.data[key];
|
|
807
|
-
}
|
|
808
|
-
async setItem(key, value) {
|
|
809
|
-
this.data[key] = value;
|
|
810
|
-
}
|
|
811
|
-
static of(params) {
|
|
812
|
-
return new MemoryState(params.defaultData);
|
|
813
|
-
}
|
|
814
|
-
}
|
|
815
|
-
|
|
816
|
-
const fsp$3 = fs$1.promises;
|
|
817
|
-
class Logger {
|
|
818
|
-
constructor(isDebug, root, config){
|
|
819
|
-
this.isDebug = isDebug;
|
|
820
|
-
this.root = root;
|
|
821
|
-
this.config = config;
|
|
822
|
-
this.debugLog = (value)=>{
|
|
823
|
-
if (!this.isDebug) {
|
|
824
|
-
return;
|
|
825
|
-
}
|
|
826
|
-
const loggerFile = Path$1.join(this.root, this.config.debugLogFile);
|
|
827
|
-
const time = new Date().toLocaleString();
|
|
828
|
-
const content = JSON.stringify(value, undefined, 2);
|
|
829
|
-
fsp$3.appendFile(loggerFile, `[${time}]\n${content}\n`);
|
|
830
|
-
};
|
|
831
|
-
this./** 普通日志,输出基本信息 */ log = (value)=>{
|
|
832
|
-
const loggerFile = Path$1.join(this.root, this.config.generalLogFile);
|
|
833
|
-
const time = new Date().toLocaleString();
|
|
834
|
-
const content = typeof value === 'object' ? JSON.stringify(value) : String(value);
|
|
835
|
-
fsp$3.appendFile(loggerFile, `[${time}] ${content}\n`);
|
|
836
|
-
};
|
|
837
|
-
}
|
|
838
|
-
static of(params) {
|
|
839
|
-
return new Logger(params.isDebug, params.root, params.config);
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
|
|
843
|
-
var ModelName = /*#__PURE__*/ function(ModelName) {
|
|
844
|
-
ModelName["ERNIE_BOT"] = "ernie-bot";
|
|
845
|
-
ModelName["ERNIE_BOT_4_0"] = "ernie-bot-4.0";
|
|
846
|
-
ModelName["ERNIE_4_0_8K"] = "ernie-4.0-8k";
|
|
847
|
-
ModelName["ERNIE_4_0_TURBO_8K"] = "ernie-4.0-turbo-8k";
|
|
848
|
-
ModelName["ERNIE_3_5_8K"] = "ernie-3.5-8k";
|
|
849
|
-
ModelName["ERNIE_SPEED_8K"] = "ernie-speed-8k";
|
|
850
|
-
ModelName["ERNIE_SPEED_128K"] = "ernie-speed-128k";
|
|
851
|
-
ModelName["ERNIE_LITE_8K"] = "ernie-lite-8k";
|
|
852
|
-
ModelName["GPT_3_5_TURBO"] = "gpt-3.5-turbo";
|
|
853
|
-
ModelName["GPT_3_5_TURBO_16K"] = "gpt-3.5-turbo-16k";
|
|
854
|
-
ModelName["GPT_4"] = "gpt-4";
|
|
855
|
-
ModelName["GPT_4_1106_PREVIEW"] = "gpt-4-1106-preview";
|
|
856
|
-
ModelName["GPT_4_TURBO_PREVIEW"] = "gpt-4-turbo-preview";
|
|
857
|
-
ModelName["GPT_4_TURBO"] = "gpt-4-turbo";
|
|
858
|
-
ModelName["GPT_4_VISION_PREVIEW"] = "gpt-4-vision-preview";
|
|
859
|
-
ModelName["GPT_4O"] = "gpt-4o";
|
|
860
|
-
ModelName["GPT_4O_2024_08_06"] = "gpt-4o-2024-08-06";
|
|
861
|
-
ModelName["GPT_4O_MINI"] = "gpt-4o-mini";
|
|
862
|
-
return ModelName;
|
|
863
|
-
}({});
|
|
864
|
-
class Model {
|
|
865
|
-
constructor(data){
|
|
866
|
-
this.data = data;
|
|
867
|
-
}
|
|
868
|
-
static of(params) {
|
|
869
|
-
return new Model(params.data);
|
|
870
|
-
}
|
|
871
|
-
static ofName(model) {
|
|
872
|
-
return Model.of({
|
|
873
|
-
data: {
|
|
874
|
-
modelID: 0,
|
|
875
|
-
modelType: 'PANDA',
|
|
876
|
-
modelParams: {},
|
|
877
|
-
model
|
|
878
|
-
}
|
|
879
|
-
});
|
|
880
|
-
}
|
|
881
|
-
get modelKey() {
|
|
882
|
-
return this.data.model;
|
|
883
|
-
}
|
|
884
|
-
get displayName() {
|
|
885
|
-
return this.data.model;
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
|
|
889
|
-
class ModelManager {
|
|
890
|
-
constructor(modelDir){
|
|
891
|
-
this.modelDir = modelDir;
|
|
892
|
-
this.modelTemplate = new JSONTemplate([
|
|
893
|
-
{
|
|
894
|
-
key: 'modelParams',
|
|
895
|
-
blockComment: '模型参数,https://ku.baidu-int.com/knowledge/HFVrC7hq1Q/_SKPgSwp2G/Hb6OQz5Jc7/1EFTDCIbt1kuRI#anchor-cd57bb90-a25a-11ef-8219-e7448a3b189e'
|
|
896
|
-
}
|
|
897
|
-
]);
|
|
898
|
-
}
|
|
899
|
-
static of(params) {
|
|
900
|
-
return new ModelManager(params.modelDir);
|
|
901
|
-
}
|
|
902
|
-
async saveModel(model) {
|
|
903
|
-
const modelFilePath = this.pathForModel(model);
|
|
904
|
-
const content = this.modelTemplate.format({
|
|
905
|
-
model: model.data.model,
|
|
906
|
-
modelParams: model.data.modelParams
|
|
907
|
-
});
|
|
908
|
-
return createFileIfNotExists(modelFilePath, content);
|
|
909
|
-
}
|
|
910
|
-
async loadAllModel() {
|
|
911
|
-
const fileNames = await readOrCreateDir(this.modelDir);
|
|
912
|
-
const filePaths = fileNames.map((fileName)=>Path$1.resolve(this.modelDir, fileName));
|
|
913
|
-
return Promise.all(filePaths.map(async (filePath)=>{
|
|
914
|
-
const content = await safeReadFile(filePath);
|
|
915
|
-
const modelData = this.modelTemplate.parse(content);
|
|
916
|
-
return Model.of({
|
|
917
|
-
data: {
|
|
918
|
-
model: modelData.model,
|
|
919
|
-
modelParams: modelData.modelParams,
|
|
920
|
-
modelID: 0,
|
|
921
|
-
modelType: 'PANDA'
|
|
922
|
-
}
|
|
923
|
-
});
|
|
924
|
-
}));
|
|
925
|
-
}
|
|
926
|
-
async getModelByKey(modelKey) {
|
|
927
|
-
const models = await this.loadAllModel();
|
|
928
|
-
return models.find((model)=>model.modelKey === modelKey);
|
|
929
|
-
}
|
|
930
|
-
pathForModel(model) {
|
|
931
|
-
return Path$1.resolve(this.modelDir, `${model.modelKey}.jsonc`);
|
|
932
|
-
}
|
|
933
|
-
}
|
|
934
|
-
|
|
935
|
-
const fsp$2 = fs.promises;
|
|
936
|
-
new MemoryState({
|
|
937
|
-
sessionID: undefined,
|
|
938
|
-
activePromptID: undefined
|
|
939
|
-
});
|
|
940
|
-
class BasePromptProvider extends SkillProvider {
|
|
941
|
-
get ievalueRoot() {
|
|
942
|
-
const dir = Path.join(this.init.cwd, config.outputDir);
|
|
943
|
-
return dir;
|
|
944
|
-
}
|
|
945
|
-
get projectRoot() {
|
|
946
|
-
return this.init.cwd;
|
|
947
|
-
}
|
|
948
|
-
getDirPaths() {
|
|
949
|
-
return {
|
|
950
|
-
root: this.ievalueRoot,
|
|
951
|
-
prompt: Path.join(this.ievalueRoot, 'prompt'),
|
|
952
|
-
model: Path.join(this.ievalueRoot, 'model'),
|
|
953
|
-
logs: this.ievalueRoot
|
|
954
|
-
};
|
|
955
|
-
}
|
|
956
|
-
getPaths() {
|
|
957
|
-
return {
|
|
958
|
-
...this.getDirPaths(),
|
|
959
|
-
// localState: Path.join(this.getDirPaths().root, 'localState.jsonc'),
|
|
960
|
-
sessionState: Path.join(this.getDirPaths().root, 'sessionState.jsonc'),
|
|
961
|
-
userSetting: Path.join(this.getDirPaths().root, 'userSetting.jsonc')
|
|
962
|
-
};
|
|
963
|
-
}
|
|
964
|
-
constructor(init){
|
|
965
|
-
super(init), this.init = init, this.isDebug = config.isDebug, this.debugLog = (value)=>{
|
|
966
|
-
this.fileLogger.debugLog(value);
|
|
967
|
-
}, this.log = (value)=>{
|
|
968
|
-
this.fileLogger.log(value);
|
|
969
|
-
};
|
|
970
|
-
this.initPluginFolder();
|
|
971
|
-
this.userSetting = new FileState({
|
|
972
|
-
path: this.getPaths().userSetting,
|
|
973
|
-
defaultData: {
|
|
974
|
-
apiKey: '',
|
|
975
|
-
exportFirstCode: ''
|
|
976
|
-
},
|
|
977
|
-
jsonTemplate: new JSONTemplate([
|
|
978
|
-
{
|
|
979
|
-
key: 'apiKey',
|
|
980
|
-
comment: `从 panda 平台获取的 API Key,文档参考:${USER_DOCS.getAPIKey}`
|
|
981
|
-
},
|
|
982
|
-
{
|
|
983
|
-
key: 'exportFirstCode',
|
|
984
|
-
comment: `输出内容中代码块的写入文件路径,例如: {{fileDir}}/{{fileName}}-{{index}}{{fileExt}} 更多配置参见 ${USER_DOCS.exportFirstCode}`
|
|
985
|
-
}
|
|
986
|
-
])
|
|
987
|
-
});
|
|
988
|
-
this.userSetting.getItem('isDebug').then((v)=>this.isDebug = v === undefined || v === null ? config.isDebug : v);
|
|
989
|
-
this.promptManager = new PromptManager(this.getDirPaths().prompt, this.debugLog);
|
|
990
|
-
this.modelManager = ModelManager.of({
|
|
991
|
-
modelDir: this.getDirPaths().model
|
|
992
|
-
});
|
|
993
|
-
this.fileLogger = Logger.of({
|
|
994
|
-
root: this.ievalueRoot,
|
|
995
|
-
isDebug: this.isDebug,
|
|
996
|
-
config: {
|
|
997
|
-
debugLogFile: 'debug.log',
|
|
998
|
-
generalLogFile: 'general.log'
|
|
999
|
-
}
|
|
1000
|
-
});
|
|
1001
|
-
}
|
|
1002
|
-
async initPluginFolder() {
|
|
1003
|
-
const paths = this.getDirPaths();
|
|
1004
|
-
for (const [key, path] of Object.entries(paths)){
|
|
1005
|
-
try {
|
|
1006
|
-
await fsp$2.mkdir(path, {
|
|
1007
|
-
recursive: true
|
|
1008
|
-
});
|
|
1009
|
-
this.debugLog(`成功创建目录: ${key} - ${path}`);
|
|
1010
|
-
} catch (e) {
|
|
1011
|
-
this.debugLog(`创建目录失败: ${key} - ${path}, 错误: ${e}`);
|
|
1012
|
-
}
|
|
1013
|
-
}
|
|
1014
|
-
}
|
|
1015
|
-
async *execute(arg) {
|
|
1016
|
-
const stream = new StringChunkStream();
|
|
1017
|
-
yield stream.flush('未实现的命令');
|
|
1018
|
-
}
|
|
1019
|
-
/* ---- 提示词相关 ---- */ async savePrompt(prompt) {
|
|
1020
|
-
const result = await this.promptManager.savePrompt(prompt);
|
|
1021
|
-
this.debugLog(`提示词 ${prompt.data.promptName} 保存结果: ${result}`);
|
|
1022
|
-
return result;
|
|
1023
|
-
}
|
|
1024
|
-
// TODO: 最好拆分出去,有一个专门处理常用用户指令的对象来处理
|
|
1025
|
-
async *savePromptWithStream(macroPrompt) {
|
|
1026
|
-
const stream = new ElementChunkStream();
|
|
1027
|
-
const promptName = macroPrompt.data.promptName;
|
|
1028
|
-
const result = await this.savePrompt(macroPrompt);
|
|
1029
|
-
const iEValueFile = this.pathForPromptSystemFile(macroPrompt);
|
|
1030
|
-
const metaFile = this.pathForPromptMetaFile(macroPrompt);
|
|
1031
|
-
// NOTE: 覆盖的话最好有一个其他的提示
|
|
1032
|
-
if (result === 'create' || result === 'overwrite') {
|
|
1033
|
-
yield stream.flush(/*#__PURE__*/ jsx("h1", {
|
|
1034
|
-
children: "创建成功"
|
|
1035
|
-
}));
|
|
1036
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
1037
|
-
children: [
|
|
1038
|
-
"配置文件:",
|
|
1039
|
-
/*#__PURE__*/ jsx("file-link", {
|
|
1040
|
-
to: iEValueFile,
|
|
1041
|
-
line: 1,
|
|
1042
|
-
children: "system.jsonc"
|
|
1043
|
-
})
|
|
1044
|
-
]
|
|
1045
|
-
}));
|
|
1046
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
1047
|
-
children: [
|
|
1048
|
-
"内容文件:",
|
|
1049
|
-
/*#__PURE__*/ jsx("file-link", {
|
|
1050
|
-
to: metaFile,
|
|
1051
|
-
line: 1,
|
|
1052
|
-
children: "模板"
|
|
1053
|
-
})
|
|
1054
|
-
]
|
|
1055
|
-
}));
|
|
1056
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
1057
|
-
children: [
|
|
1058
|
-
"调用 /执行提示词 选择 ",
|
|
1059
|
-
promptName,
|
|
1060
|
-
" 命令可使用"
|
|
1061
|
-
]
|
|
1062
|
-
}));
|
|
1063
|
-
} else if (result === 'error') {
|
|
1064
|
-
yield stream.flush(/*#__PURE__*/ jsx("h1", {
|
|
1065
|
-
children: "创建失败"
|
|
1066
|
-
}));
|
|
1067
|
-
}
|
|
1068
|
-
}
|
|
1069
|
-
async loadAllPrompts() {
|
|
1070
|
-
return this.promptManager.loadAllPrompts();
|
|
1071
|
-
}
|
|
1072
|
-
async getPromptByKey(promptKey) {
|
|
1073
|
-
return this.promptManager.getPromptByKey(promptKey);
|
|
1074
|
-
}
|
|
1075
|
-
/* ---- Model 相关 ---- */ async loadAllModels() {
|
|
1076
|
-
return this.modelManager.loadAllModel();
|
|
1077
|
-
}
|
|
1078
|
-
async saveModel(model) {
|
|
1079
|
-
return this.modelManager.saveModel(model);
|
|
1080
|
-
}
|
|
1081
|
-
async getModelByKey(modelKey) {
|
|
1082
|
-
return this.modelManager.getModelByKey(modelKey);
|
|
1083
|
-
}
|
|
1084
|
-
async getAPIKey() {
|
|
1085
|
-
return this.userSetting.getItem('apiKey').then((v)=>v || undefined);
|
|
1086
|
-
}
|
|
1087
|
-
async getUserName() {
|
|
1088
|
-
if (this.userName) {
|
|
1089
|
-
return this.userName;
|
|
1090
|
-
}
|
|
1091
|
-
const user = await this.currentUser.requestDetail();
|
|
1092
|
-
if (user) {
|
|
1093
|
-
this.userName = user.name;
|
|
1094
|
-
return user.name;
|
|
1095
|
-
}
|
|
1096
|
-
}
|
|
1097
|
-
pathForPromptSystemFile(prompt) {
|
|
1098
|
-
return this.promptManager.pathForPromptIEValue(prompt);
|
|
1099
|
-
}
|
|
1100
|
-
pathForPromptMetaFile(prompt) {
|
|
1101
|
-
return this.promptManager.pathForPromptMeta(prompt);
|
|
1102
|
-
}
|
|
1103
|
-
pathForActionFile(prompt) {
|
|
1104
|
-
return this.promptManager.pathForPromptActions(prompt);
|
|
1105
|
-
}
|
|
1106
|
-
pathForModelFile(model) {
|
|
1107
|
-
return this.modelManager.pathForModel(model);
|
|
1108
|
-
}
|
|
1109
|
-
fileExist(path) {
|
|
1110
|
-
return fs.existsSync(path);
|
|
1111
|
-
}
|
|
1112
|
-
/**
|
|
1113
|
-
* 获取编辑器当前状态
|
|
1114
|
-
*/ get editorState() {
|
|
1115
|
-
const query = this.commandContext?.query ?? this.currentContext.query;
|
|
1116
|
-
const select = this.commandContext?.selectedCode ?? this.currentContext.selectedCode;
|
|
1117
|
-
return {
|
|
1118
|
-
activeFileContent: this.commandContext?.activeFileContent ?? this.currentContext.activeFileContent,
|
|
1119
|
-
activeFilePath: this.commandContext?.activeFilePath ?? this.currentContext.activeFilePath,
|
|
1120
|
-
query,
|
|
1121
|
-
queryOrSelection: query || select,
|
|
1122
|
-
selectedCode: select
|
|
1123
|
-
};
|
|
1124
|
-
}
|
|
1125
|
-
}
|
|
1126
|
-
|
|
1127
|
-
async function apiGetPromptDetail(params) {
|
|
1128
|
-
const { promptCode, versionName } = params;
|
|
1129
|
-
const myHeaders = new Headers();
|
|
1130
|
-
myHeaders.append('token', config.token);
|
|
1131
|
-
myHeaders.append('User-Agent', 'iAPI/1.0.0 (https://iapi.baidu-int.com)');
|
|
1132
|
-
const requestOptions = {
|
|
1133
|
-
method: 'GET',
|
|
1134
|
-
headers: myHeaders,
|
|
1135
|
-
redirect: 'follow'
|
|
1136
|
-
};
|
|
1137
|
-
try {
|
|
1138
|
-
const response = await fetch(`${host}/openApi/v1/plugin/prompt/detail?promptCode=${promptCode}&versionName=${versionName}`, requestOptions);
|
|
1139
|
-
const result = await response.json();
|
|
1140
|
-
return result;
|
|
1141
|
-
} catch (error) {
|
|
1142
|
-
console.error('获取提示词详情时出错:', error);
|
|
1143
|
-
throw error;
|
|
1144
|
-
}
|
|
1145
|
-
}
|
|
1146
|
-
|
|
1147
|
-
async function apiGetPromptList(params) {
|
|
1148
|
-
const { username } = params;
|
|
1149
|
-
const myHeaders = new Headers();
|
|
1150
|
-
myHeaders.append('token', config.token);
|
|
1151
|
-
myHeaders.append('User-Agent', 'iAPI/1.0.0 (https://iapi.baidu-int.com)');
|
|
1152
|
-
const data = await fetch(`${host}/openApi/v1/plugin/prompt/recommend?username=${username}`, {
|
|
1153
|
-
method: 'GET',
|
|
1154
|
-
headers: myHeaders,
|
|
1155
|
-
redirect: 'follow'
|
|
1156
|
-
});
|
|
1157
|
-
const result = await data.json();
|
|
1158
|
-
return result;
|
|
1159
|
-
}
|
|
1160
|
-
|
|
1161
|
-
async function apiSearchPromptList(params) {
|
|
1162
|
-
const { username, promptNameKeyword = '', versionNameKeyword = '' } = params;
|
|
1163
|
-
const myHeaders = new Headers();
|
|
1164
|
-
myHeaders.append('token', config.token);
|
|
1165
|
-
myHeaders.append('User-Agent', 'iAPI/1.0.0 (https://iapi.baidu-int.com)');
|
|
1166
|
-
const requestOptions = {
|
|
1167
|
-
method: 'GET',
|
|
1168
|
-
headers: myHeaders,
|
|
1169
|
-
redirect: 'follow'
|
|
1170
|
-
};
|
|
1171
|
-
try {
|
|
1172
|
-
const response = await fetch(`${host}/openApi/v1/plugin/prompt/query?username=${username}&promptNameKeyword=${promptNameKeyword}&versionNameKeyword=${versionNameKeyword}`, requestOptions);
|
|
1173
|
-
const result = await response.json();
|
|
1174
|
-
return result;
|
|
1175
|
-
} catch (error) {
|
|
1176
|
-
console.error('搜索提示词列表时出错:', error);
|
|
1177
|
-
throw error;
|
|
1178
|
-
}
|
|
1179
|
-
}
|
|
1180
|
-
async function apiSmartSearchPromptList(params) {
|
|
1181
|
-
const { username, promptNameKeyword, versionNameKeyword } = params;
|
|
1182
|
-
// 如果关键词都为空,调用 getPromptList
|
|
1183
|
-
if (!promptNameKeyword && !versionNameKeyword) {
|
|
1184
|
-
return await apiGetPromptList({
|
|
1185
|
-
username
|
|
1186
|
-
});
|
|
1187
|
-
}
|
|
1188
|
-
// 否则调用 apiSearchPromptList
|
|
1189
|
-
return await apiSearchPromptList(params);
|
|
1190
|
-
}
|
|
1191
|
-
|
|
1192
|
-
var ResultCode = /*#__PURE__*/ function(ResultCode) {
|
|
1193
|
-
ResultCode[ResultCode["withoutAPIKey"] = 73002] = "withoutAPIKey";
|
|
1194
|
-
ResultCode[ResultCode["ok"] = 200] = "ok";
|
|
1195
|
-
return ResultCode;
|
|
1196
|
-
}({});
|
|
1197
|
-
|
|
1198
|
-
function commandButton$3({ commandName, data, content }) {
|
|
1199
|
-
return /*#__PURE__*/ jsx("command-button", {
|
|
1200
|
-
commandName: commandName,
|
|
1201
|
-
data: data,
|
|
1202
|
-
children: content
|
|
1203
|
-
});
|
|
1204
|
-
}
|
|
1205
|
-
function parseUserQuery(query) {
|
|
1206
|
-
const parts = query.trim().split(/\s+/).filter(Boolean);
|
|
1207
|
-
return {
|
|
1208
|
-
promptNameKeyword: parts[0] || '',
|
|
1209
|
-
versionNameKeyword: parts[1] || ''
|
|
1210
|
-
};
|
|
1211
|
-
}
|
|
1212
|
-
/**
|
|
1213
|
-
* 获取提示词
|
|
1214
|
-
*/ class GetPromptProvider extends BasePromptProvider {
|
|
1215
|
-
static{
|
|
1216
|
-
this.skillName = skills.getPrompt.name;
|
|
1217
|
-
}
|
|
1218
|
-
static{
|
|
1219
|
-
this.description = skills.getPrompt.description;
|
|
1220
|
-
}
|
|
1221
|
-
static{
|
|
1222
|
-
this.parameters = {
|
|
1223
|
-
type: 'object',
|
|
1224
|
-
properties: {},
|
|
1225
|
-
required: []
|
|
1226
|
-
};
|
|
1227
|
-
}
|
|
1228
|
-
async *execute(arg) {
|
|
1229
|
-
const userQuery = this.editorState.queryOrSelection;
|
|
1230
|
-
const { promptNameKeyword, versionNameKeyword } = parseUserQuery(userQuery);
|
|
1231
|
-
const stream = new ElementChunkStream();
|
|
1232
|
-
const username = await this.getUserName();
|
|
1233
|
-
if (!username) {
|
|
1234
|
-
return stream.flush(/*#__PURE__*/ jsx("p", {
|
|
1235
|
-
children: "请先登录"
|
|
1236
|
-
}));
|
|
1237
|
-
}
|
|
1238
|
-
const data = await apiSmartSearchPromptList({
|
|
1239
|
-
username,
|
|
1240
|
-
promptNameKeyword,
|
|
1241
|
-
versionNameKeyword
|
|
1242
|
-
});
|
|
1243
|
-
if (data.code !== ResultCode.ok) {
|
|
1244
|
-
yield stream.flush(/*#__PURE__*/ jsx("h1", {
|
|
1245
|
-
children: "获取提示词失败"
|
|
1246
|
-
}));
|
|
1247
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
1248
|
-
children: [
|
|
1249
|
-
"错误代码:",
|
|
1250
|
-
data.code
|
|
1251
|
-
]
|
|
1252
|
-
}));
|
|
1253
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
1254
|
-
children: [
|
|
1255
|
-
"错误原因:",
|
|
1256
|
-
data.msg
|
|
1257
|
-
]
|
|
1258
|
-
}));
|
|
1259
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
1260
|
-
children: data.desc
|
|
1261
|
-
}));
|
|
1262
|
-
}
|
|
1263
|
-
this.debugLog({
|
|
1264
|
-
data
|
|
1265
|
-
});
|
|
1266
|
-
const promptDetails = await Promise.all(data.data.map(async (prompt)=>{
|
|
1267
|
-
const detail = await apiGetPromptDetail({
|
|
1268
|
-
promptCode: prompt.promptCode,
|
|
1269
|
-
versionName: prompt.versionName
|
|
1270
|
-
});
|
|
1271
|
-
return detail.data;
|
|
1272
|
-
}));
|
|
1273
|
-
this.debugLog({
|
|
1274
|
-
promptDetails
|
|
1275
|
-
});
|
|
1276
|
-
if (promptDetails.length === 0) {
|
|
1277
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
1278
|
-
children: "未找到提示词"
|
|
1279
|
-
}));
|
|
1280
|
-
return;
|
|
1281
|
-
}
|
|
1282
|
-
yield stream.flush(/*#__PURE__*/ jsx("h1", {
|
|
1283
|
-
children: "提示词如下"
|
|
1284
|
-
}));
|
|
1285
|
-
yield stream.flush(/*#__PURE__*/ jsx("ul", {
|
|
1286
|
-
children: data.data.map((item)=>{
|
|
1287
|
-
return /*#__PURE__*/ jsx("li", {
|
|
1288
|
-
children: /*#__PURE__*/ jsx("flex", {
|
|
1289
|
-
betweenCentered: true,
|
|
1290
|
-
children: [
|
|
1291
|
-
/*#__PURE__*/ jsx("strong", {
|
|
1292
|
-
children: item.promptName
|
|
1293
|
-
}),
|
|
1294
|
-
item.versionName,
|
|
1295
|
-
"-",
|
|
1296
|
-
item.spaceName,
|
|
1297
|
-
commandButton$3({
|
|
1298
|
-
commandName: 'comate.ievalue.prompt.setPrompt',
|
|
1299
|
-
data: item,
|
|
1300
|
-
content: `点击获取`
|
|
1301
|
-
})
|
|
1302
|
-
]
|
|
1303
|
-
})
|
|
1304
|
-
});
|
|
1305
|
-
})
|
|
1306
|
-
}));
|
|
1307
|
-
}
|
|
1308
|
-
async *handleCommand(commandName, data) {
|
|
1309
|
-
this.log({
|
|
1310
|
-
commandName,
|
|
1311
|
-
data
|
|
1312
|
-
});
|
|
1313
|
-
switch(commandName){
|
|
1314
|
-
case 'comate.ievalue.prompt.setPrompt':
|
|
1315
|
-
yield* this.downloadDetail(data);
|
|
1316
|
-
break;
|
|
1317
|
-
}
|
|
1318
|
-
}
|
|
1319
|
-
async *downloadDetail(promptSummary) {
|
|
1320
|
-
try {
|
|
1321
|
-
const { promptCode, versionName } = promptSummary;
|
|
1322
|
-
const detail = await apiGetPromptDetail({
|
|
1323
|
-
promptCode,
|
|
1324
|
-
versionName
|
|
1325
|
-
});
|
|
1326
|
-
const prompt = MacroPrompt.of({
|
|
1327
|
-
data: detail.data,
|
|
1328
|
-
promptActions: PromptActions.ofEmpty()
|
|
1329
|
-
});
|
|
1330
|
-
const actionType = await this.savePrompt(prompt);
|
|
1331
|
-
if (actionType === 'error') {
|
|
1332
|
-
return;
|
|
1333
|
-
}
|
|
1334
|
-
const stream = new ElementChunkStream();
|
|
1335
|
-
const iEValueFile = this.pathForPromptSystemFile(prompt);
|
|
1336
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
1337
|
-
children: [
|
|
1338
|
-
actionType === 'overwrite' ? '已更新' : '已获取',
|
|
1339
|
-
' ',
|
|
1340
|
-
/*#__PURE__*/ jsx("file-link", {
|
|
1341
|
-
to: iEValueFile,
|
|
1342
|
-
line: 1,
|
|
1343
|
-
children: promptFileName(promptSummary)
|
|
1344
|
-
}),
|
|
1345
|
-
",并将 Prompt 模版、自定义变量、调试参数文件下载到本地"
|
|
1346
|
-
]
|
|
1347
|
-
}));
|
|
1348
|
-
const item = detail.data;
|
|
1349
|
-
const mateFile = this.pathForPromptMetaFile(prompt);
|
|
1350
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
1351
|
-
children: [
|
|
1352
|
-
"Prompt 模版地址:",
|
|
1353
|
-
/*#__PURE__*/ jsx("file-link", {
|
|
1354
|
-
to: mateFile,
|
|
1355
|
-
line: 1,
|
|
1356
|
-
children: [
|
|
1357
|
-
item.promptName,
|
|
1358
|
-
"模版"
|
|
1359
|
-
]
|
|
1360
|
-
})
|
|
1361
|
-
]
|
|
1362
|
-
}));
|
|
1363
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
1364
|
-
children: [
|
|
1365
|
-
"调试并保存后使用 ",
|
|
1366
|
-
/*#__PURE__*/ jsx("code", {
|
|
1367
|
-
children: "@Prompt /执行提示词"
|
|
1368
|
-
}),
|
|
1369
|
-
" 即可获取执行结果"
|
|
1370
|
-
]
|
|
1371
|
-
}));
|
|
1372
|
-
} catch (err) {
|
|
1373
|
-
const stream = new ElementChunkStream();
|
|
1374
|
-
yield stream.flush(/*#__PURE__*/ jsx("h1", {
|
|
1375
|
-
children: [
|
|
1376
|
-
"获取 ",
|
|
1377
|
-
promptFileName(promptSummary),
|
|
1378
|
-
" 失败"
|
|
1379
|
-
]
|
|
1380
|
-
}));
|
|
1381
|
-
if (err instanceof Error) {
|
|
1382
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
1383
|
-
children: err.message
|
|
1384
|
-
}));
|
|
1385
|
-
}
|
|
1386
|
-
this.debugLog(err);
|
|
1387
|
-
this.log(err);
|
|
1388
|
-
}
|
|
1389
|
-
}
|
|
1390
|
-
}
|
|
1391
|
-
|
|
1392
|
-
function formatTime(date) {
|
|
1393
|
-
// 格式为 YYYY-MM-DD HH:mm:ss
|
|
1394
|
-
const year = date.getFullYear();
|
|
1395
|
-
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
1396
|
-
const day = String(date.getDate()).padStart(2, '0');
|
|
1397
|
-
const hours = String(date.getHours()).padStart(2, '0');
|
|
1398
|
-
const minutes = String(date.getMinutes()).padStart(2, '0');
|
|
1399
|
-
const seconds = String(date.getSeconds()).padStart(2, '0');
|
|
1400
|
-
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
|
1401
|
-
}
|
|
1402
|
-
|
|
1403
|
-
function codeblocksInMarkdown(markdown) {
|
|
1404
|
-
const codeblocks = [];
|
|
1405
|
-
const regex = /```(\w+)?\n([\s\S]*?)```/g;
|
|
1406
|
-
let match;
|
|
1407
|
-
while((match = regex.exec(markdown)) !== null){
|
|
1408
|
-
codeblocks.push({
|
|
1409
|
-
fileType: match[1] || '',
|
|
1410
|
-
content: match[2].trim()
|
|
1411
|
-
});
|
|
1412
|
-
}
|
|
1413
|
-
return codeblocks;
|
|
1414
|
-
}
|
|
1415
|
-
|
|
1416
|
-
// 封装一个 POST 请求工具函数
|
|
1417
|
-
async function postRequest({ url, token, data }) {
|
|
1418
|
-
// 创建请求头
|
|
1419
|
-
const headers = new Headers({
|
|
1420
|
-
token: token,
|
|
1421
|
-
'User-Agent': 'iAPI/1.0.0 (https://iapi.baidu-int.com)',
|
|
1422
|
-
'Content-Type': 'application/json'
|
|
1423
|
-
});
|
|
1424
|
-
// 将请求数据转换为 JSON 字符串
|
|
1425
|
-
const requestBody = JSON.stringify(data);
|
|
1426
|
-
// 设置请求选项
|
|
1427
|
-
const requestOptions = {
|
|
1428
|
-
method: 'POST',
|
|
1429
|
-
headers: headers,
|
|
1430
|
-
body: requestBody,
|
|
1431
|
-
redirect: 'follow'
|
|
1432
|
-
};
|
|
1433
|
-
// 发送请求并返回响应
|
|
1434
|
-
const response = await fetch(url, requestOptions);
|
|
1435
|
-
return response.json();
|
|
1436
|
-
}
|
|
1437
|
-
|
|
1438
|
-
const resultTypeNameMapping = {
|
|
1439
|
-
bad_sentence: '语意',
|
|
1440
|
-
conflict: '上下文冲突',
|
|
1441
|
-
unclear: '描述不清晰'
|
|
1442
|
-
};
|
|
1443
|
-
async function apiDiagnosisPrompt(params) {
|
|
1444
|
-
const result = await postRequest({
|
|
1445
|
-
url: `${host}/openApi/v1/plugin/prompt/quality/run`,
|
|
1446
|
-
token: config.token,
|
|
1447
|
-
data: {
|
|
1448
|
-
username: params.username,
|
|
1449
|
-
text: params.text
|
|
1450
|
-
}
|
|
1451
|
-
});
|
|
1452
|
-
return result;
|
|
1453
|
-
}
|
|
1454
|
-
|
|
1455
|
-
async function apiGeneratePromptByDescription(params) {
|
|
1456
|
-
const res = postRequest({
|
|
1457
|
-
url: `${host}/openApi/v1/plugin/prompt/generate/run`,
|
|
1458
|
-
token: config.token,
|
|
1459
|
-
data: params
|
|
1460
|
-
});
|
|
1461
|
-
return res;
|
|
1462
|
-
}
|
|
1463
|
-
|
|
1464
|
-
/**
|
|
1465
|
-
* Prompt 的概要信息,是 Detail 接口的子集,用于列表等场景
|
|
1466
|
-
*/ const emptyPromptDetailData = {
|
|
1467
|
-
promptCode: '',
|
|
1468
|
-
promptID: 0,
|
|
1469
|
-
promptName: '',
|
|
1470
|
-
spaceCode: '',
|
|
1471
|
-
spaceName: '',
|
|
1472
|
-
submitStatus: 0,
|
|
1473
|
-
versionID: 0,
|
|
1474
|
-
versionName: '',
|
|
1475
|
-
versionCreateTime: '',
|
|
1476
|
-
inputType: '',
|
|
1477
|
-
modelID: 0,
|
|
1478
|
-
model: '',
|
|
1479
|
-
modelType: '',
|
|
1480
|
-
modelParams: {},
|
|
1481
|
-
debugParams: [],
|
|
1482
|
-
text: undefined,
|
|
1483
|
-
variables: undefined
|
|
1484
|
-
};
|
|
1485
|
-
|
|
1486
|
-
async function apiRunPrompt(params) {
|
|
1487
|
-
const headers = new Headers({
|
|
1488
|
-
token: config.token,
|
|
1489
|
-
'User-Agent': 'iAPI/1.0.0 (https://iapi.baidu-int.com)',
|
|
1490
|
-
'Content-Type': 'application/json'
|
|
1491
|
-
});
|
|
1492
|
-
// https://iapi.baidu-int.com/apidoc/shared-df088616-528d-4261-8c9d-7480bd7f26a4/api-4462368
|
|
1493
|
-
const requestBody = JSON.stringify({
|
|
1494
|
-
promptCode: params.data.promptCode,
|
|
1495
|
-
text: params.data.text,
|
|
1496
|
-
variables: params.data.variables,
|
|
1497
|
-
modelType: params.data.modelType,
|
|
1498
|
-
model: params.data.model,
|
|
1499
|
-
modelParams: params.data.modelParams,
|
|
1500
|
-
username: params.username,
|
|
1501
|
-
apiKey: params.apiKey,
|
|
1502
|
-
input: params.input,
|
|
1503
|
-
sessionID: params.sessionID
|
|
1504
|
-
});
|
|
1505
|
-
const requestOptions = {
|
|
1506
|
-
method: 'POST',
|
|
1507
|
-
headers: headers,
|
|
1508
|
-
body: requestBody,
|
|
1509
|
-
redirect: 'follow'
|
|
1510
|
-
};
|
|
1511
|
-
try {
|
|
1512
|
-
const response = await fetch(`${host}/openApi/v1/plugin/prompt/run`, requestOptions);
|
|
1513
|
-
if (!response.ok) {
|
|
1514
|
-
const text = await response.text();
|
|
1515
|
-
throw new Error(`网络错误!状态码: ${response.status}。响应内容: ${text}`);
|
|
1516
|
-
}
|
|
1517
|
-
const result = await response.json();
|
|
1518
|
-
return result;
|
|
1519
|
-
} catch (error) {
|
|
1520
|
-
console.error('运行提示词时出错:', error);
|
|
1521
|
-
throw error;
|
|
1522
|
-
}
|
|
1523
|
-
}
|
|
1524
|
-
|
|
1525
|
-
function keyBy(obj, keyFor) {
|
|
1526
|
-
const result = {};
|
|
1527
|
-
for (const item of obj){
|
|
1528
|
-
result[keyFor(item)] = item;
|
|
1529
|
-
}
|
|
1530
|
-
return result;
|
|
1531
|
-
}
|
|
1532
|
-
function mapValues(obj, fn) {
|
|
1533
|
-
const result = {};
|
|
1534
|
-
for(const key in obj){
|
|
1535
|
-
result[key] = fn(obj[key]);
|
|
1536
|
-
}
|
|
1537
|
-
return result;
|
|
1538
|
-
}
|
|
1539
|
-
|
|
1540
|
-
function variablesWithNewItem(arr, item) {
|
|
1541
|
-
const overwriteKeys = new Set(item.map((v)=>v.key));
|
|
1542
|
-
return [
|
|
1543
|
-
...arr.filter((v)=>!overwriteKeys.has(v.key)),
|
|
1544
|
-
...item
|
|
1545
|
-
];
|
|
1546
|
-
}
|
|
1547
|
-
/**
|
|
1548
|
-
* API服务
|
|
1549
|
-
* 对于原始请求进行一些封装,以更突出其业务逻辑
|
|
1550
|
-
*/ class APIService {
|
|
1551
|
-
constructor(log){
|
|
1552
|
-
this.log = log;
|
|
1553
|
-
this.createDialog = async (params)=>{
|
|
1554
|
-
const prompt = params.prompt.data;
|
|
1555
|
-
const model = params.model.data;
|
|
1556
|
-
const { username, apiKey } = params.config;
|
|
1557
|
-
const variables = variablesWithNewItem(prompt.variables || [], this.getVariables(params.localStates));
|
|
1558
|
-
const result = await apiRunPrompt({
|
|
1559
|
-
data: {
|
|
1560
|
-
variables: variables,
|
|
1561
|
-
promptCode: prompt.promptCode,
|
|
1562
|
-
promptID: prompt.promptID,
|
|
1563
|
-
promptName: prompt.promptName,
|
|
1564
|
-
spaceCode: prompt.promptCode,
|
|
1565
|
-
spaceName: prompt.spaceCode,
|
|
1566
|
-
submitStatus: prompt.submitStatus,
|
|
1567
|
-
versionID: prompt.versionID ?? '',
|
|
1568
|
-
versionName: prompt.versionName,
|
|
1569
|
-
versionCreateTime: prompt.versionCreateTime ?? '',
|
|
1570
|
-
text: prompt.text,
|
|
1571
|
-
inputType: prompt.inputType,
|
|
1572
|
-
modelID: model.modelID,
|
|
1573
|
-
model: model.model,
|
|
1574
|
-
modelType: model.modelType,
|
|
1575
|
-
modelParams: {
|
|
1576
|
-
temperature: model.modelParams.temperature,
|
|
1577
|
-
topP: model.modelParams.topP,
|
|
1578
|
-
// NOTE: 注意,这里使用的是 prompt 里面的参数
|
|
1579
|
-
system: prompt.modelParams.system
|
|
1580
|
-
},
|
|
1581
|
-
debugParams: []
|
|
1582
|
-
},
|
|
1583
|
-
username,
|
|
1584
|
-
apiKey,
|
|
1585
|
-
input: undefined,
|
|
1586
|
-
sessionID: undefined
|
|
1587
|
-
});
|
|
1588
|
-
const variablesMapping = mapValues(keyBy(variables, (v)=>v.key), (v)=>v.value);
|
|
1589
|
-
const input = injectParamsToText(prompt.text ?? '', variablesMapping);
|
|
1590
|
-
switch(result.code){
|
|
1591
|
-
case ResultCode.ok:
|
|
1592
|
-
return {
|
|
1593
|
-
success: true,
|
|
1594
|
-
message: result.data.output || '运行成功',
|
|
1595
|
-
sessionID: result.data.sessionID,
|
|
1596
|
-
output: result.data.output,
|
|
1597
|
-
input
|
|
1598
|
-
};
|
|
1599
|
-
case ResultCode.withoutAPIKey:
|
|
1600
|
-
return {
|
|
1601
|
-
success: false,
|
|
1602
|
-
message: '请先在设置中配置API Key',
|
|
1603
|
-
input
|
|
1604
|
-
};
|
|
1605
|
-
default:
|
|
1606
|
-
return {
|
|
1607
|
-
success: false,
|
|
1608
|
-
message: `${result.desc}`,
|
|
1609
|
-
input
|
|
1610
|
-
};
|
|
1611
|
-
}
|
|
1612
|
-
};
|
|
1613
|
-
this.continueDialog = async (params)=>{
|
|
1614
|
-
const { sessionID, config, localStates } = params;
|
|
1615
|
-
const { username, apiKey } = config;
|
|
1616
|
-
const { query } = localStates;
|
|
1617
|
-
const result = await apiRunPrompt({
|
|
1618
|
-
data: emptyPromptDetailData,
|
|
1619
|
-
username,
|
|
1620
|
-
apiKey,
|
|
1621
|
-
input: query,
|
|
1622
|
-
sessionID
|
|
1623
|
-
});
|
|
1624
|
-
const input = localStates.query;
|
|
1625
|
-
switch(result.code){
|
|
1626
|
-
case ResultCode.ok:
|
|
1627
|
-
return {
|
|
1628
|
-
success: true,
|
|
1629
|
-
message: result.data.output || '运行成功',
|
|
1630
|
-
sessionID,
|
|
1631
|
-
output: result.data.output,
|
|
1632
|
-
input
|
|
1633
|
-
};
|
|
1634
|
-
case ResultCode.withoutAPIKey:
|
|
1635
|
-
return {
|
|
1636
|
-
success: false,
|
|
1637
|
-
message: '请先在设置中配置API Key',
|
|
1638
|
-
input
|
|
1639
|
-
};
|
|
1640
|
-
default:
|
|
1641
|
-
return {
|
|
1642
|
-
success: false,
|
|
1643
|
-
message: `${result.desc}`,
|
|
1644
|
-
input
|
|
1645
|
-
};
|
|
1646
|
-
}
|
|
1647
|
-
};
|
|
1648
|
-
this./**
|
|
1649
|
-
* 根据是否有 sessionID 决定是创建还是继续
|
|
1650
|
-
*/ autoDialog = async (params)=>{
|
|
1651
|
-
const { prompt, sessionID, config, localStates, model } = params;
|
|
1652
|
-
try {
|
|
1653
|
-
if (sessionID) {
|
|
1654
|
-
return this.continueDialog({
|
|
1655
|
-
config,
|
|
1656
|
-
localStates,
|
|
1657
|
-
sessionID
|
|
1658
|
-
});
|
|
1659
|
-
} else {
|
|
1660
|
-
return this.createDialog({
|
|
1661
|
-
prompt,
|
|
1662
|
-
model,
|
|
1663
|
-
localStates,
|
|
1664
|
-
config
|
|
1665
|
-
});
|
|
1666
|
-
}
|
|
1667
|
-
} catch (e) {
|
|
1668
|
-
return {
|
|
1669
|
-
success: false,
|
|
1670
|
-
message: `运行失败:${e}`,
|
|
1671
|
-
input: localStates.query
|
|
1672
|
-
};
|
|
1673
|
-
}
|
|
1674
|
-
};
|
|
1675
|
-
this.generatePromptByDescription = async (params)=>{
|
|
1676
|
-
const { username } = params.config;
|
|
1677
|
-
const { query } = params;
|
|
1678
|
-
const result = await apiGeneratePromptByDescription({
|
|
1679
|
-
username,
|
|
1680
|
-
taskDesc: query,
|
|
1681
|
-
examples: [],
|
|
1682
|
-
// 1:一句话任务描述:3:输入输出示例
|
|
1683
|
-
templateCode: 1
|
|
1684
|
-
});
|
|
1685
|
-
return result;
|
|
1686
|
-
};
|
|
1687
|
-
this.generatePromptByExample = async (params)=>{
|
|
1688
|
-
const { examples, config } = params;
|
|
1689
|
-
const { username } = config;
|
|
1690
|
-
const result = await apiGeneratePromptByDescription({
|
|
1691
|
-
username,
|
|
1692
|
-
taskDesc: '',
|
|
1693
|
-
examples,
|
|
1694
|
-
// 1:一句话任务描述:3:输入输出示例
|
|
1695
|
-
templateCode: 3
|
|
1696
|
-
});
|
|
1697
|
-
return result;
|
|
1698
|
-
};
|
|
1699
|
-
this.diagnosisPrompt = async (params)=>{
|
|
1700
|
-
const { text, username } = params;
|
|
1701
|
-
return apiDiagnosisPrompt({
|
|
1702
|
-
text,
|
|
1703
|
-
username
|
|
1704
|
-
});
|
|
1705
|
-
};
|
|
1706
|
-
}
|
|
1707
|
-
static of(params) {
|
|
1708
|
-
return new APIService(params.log);
|
|
1709
|
-
}
|
|
1710
|
-
getVariables(localStates) {
|
|
1711
|
-
return [
|
|
1712
|
-
{
|
|
1713
|
-
key: 'IDE_QUERY',
|
|
1714
|
-
value: localStates.query,
|
|
1715
|
-
type: 'string'
|
|
1716
|
-
},
|
|
1717
|
-
{
|
|
1718
|
-
key: 'IDE_CODE',
|
|
1719
|
-
value: localStates.currentCode,
|
|
1720
|
-
type: 'string'
|
|
1721
|
-
}
|
|
1722
|
-
];
|
|
1723
|
-
}
|
|
1724
|
-
}
|
|
1725
|
-
|
|
1726
|
-
const internalEBModels = [
|
|
1727
|
-
ModelName.ERNIE_BOT,
|
|
1728
|
-
ModelName.ERNIE_BOT_4_0,
|
|
1729
|
-
ModelName.ERNIE_4_0_8K,
|
|
1730
|
-
ModelName.ERNIE_4_0_TURBO_8K,
|
|
1731
|
-
ModelName.ERNIE_3_5_8K,
|
|
1732
|
-
ModelName.ERNIE_SPEED_8K,
|
|
1733
|
-
ModelName.ERNIE_SPEED_128K,
|
|
1734
|
-
ModelName.ERNIE_LITE_8K
|
|
1735
|
-
].map((v)=>Model.of({
|
|
1736
|
-
data: {
|
|
1737
|
-
model: v,
|
|
1738
|
-
modelID: 0,
|
|
1739
|
-
modelType: 'PANDA',
|
|
1740
|
-
modelParams: {
|
|
1741
|
-
temperature: 0.8,
|
|
1742
|
-
topP: 0.8
|
|
1743
|
-
}
|
|
1744
|
-
}
|
|
1745
|
-
}));
|
|
1746
|
-
const internalGPTModels = [
|
|
1747
|
-
ModelName.GPT_3_5_TURBO,
|
|
1748
|
-
ModelName.GPT_3_5_TURBO_16K,
|
|
1749
|
-
ModelName.GPT_4,
|
|
1750
|
-
ModelName.GPT_4_1106_PREVIEW,
|
|
1751
|
-
ModelName.GPT_4_TURBO_PREVIEW,
|
|
1752
|
-
ModelName.GPT_4_TURBO,
|
|
1753
|
-
ModelName.GPT_4_VISION_PREVIEW,
|
|
1754
|
-
ModelName.GPT_4O,
|
|
1755
|
-
ModelName.GPT_4O_2024_08_06,
|
|
1756
|
-
ModelName.GPT_4O_MINI
|
|
1757
|
-
].map((v)=>Model.of({
|
|
1758
|
-
data: {
|
|
1759
|
-
model: v,
|
|
1760
|
-
modelID: 0,
|
|
1761
|
-
modelType: 'PANDA',
|
|
1762
|
-
modelParams: {
|
|
1763
|
-
temperature: 1,
|
|
1764
|
-
topP: 1
|
|
1765
|
-
}
|
|
1766
|
-
}
|
|
1767
|
-
}));
|
|
1768
|
-
const internalModel = [
|
|
1769
|
-
...internalEBModels,
|
|
1770
|
-
...internalGPTModels
|
|
1771
|
-
];
|
|
1772
|
-
|
|
1773
|
-
const fsp$1 = fs.promises;
|
|
1774
|
-
// 请只使用赋值语法,保持对象属性是不变的
|
|
1775
|
-
let runningState = {
|
|
1776
|
-
type: 0
|
|
1777
|
-
};
|
|
1778
|
-
function commandButton$2({ commandName, data, content }) {
|
|
1779
|
-
return /*#__PURE__*/ jsx("command-button", {
|
|
1780
|
-
commandName: commandName,
|
|
1781
|
-
data: data,
|
|
1782
|
-
children: content
|
|
1783
|
-
});
|
|
1784
|
-
}
|
|
1785
|
-
/**
|
|
1786
|
-
* 执行提示词
|
|
1787
|
-
*/ class RunPromptProvider extends BasePromptProvider {
|
|
1788
|
-
static{
|
|
1789
|
-
this.skillName = skills.getPrompt.name;
|
|
1790
|
-
}
|
|
1791
|
-
static{
|
|
1792
|
-
this.description = skills.getPrompt.description;
|
|
1793
|
-
}
|
|
1794
|
-
static{
|
|
1795
|
-
this.parameters = {
|
|
1796
|
-
type: 'object',
|
|
1797
|
-
properties: {},
|
|
1798
|
-
required: []
|
|
1799
|
-
};
|
|
1800
|
-
}
|
|
1801
|
-
async loadAllPromptsWithInternal() {
|
|
1802
|
-
// const promptList = await this.loadAllPrompts();
|
|
1803
|
-
// return [...internalPrompts, ...promptList];
|
|
1804
|
-
return this.loadAllPrompts();
|
|
1805
|
-
}
|
|
1806
|
-
async initInternalPrompt() {
|
|
1807
|
-
// TODO: 初始化模型
|
|
1808
|
-
const allModel = await this.loadAllModels();
|
|
1809
|
-
const modelSet = new Set(allModel.map((m)=>m.modelKey));
|
|
1810
|
-
const modelNotInit = internalModel.filter((m)=>!modelSet.has(m.modelKey));
|
|
1811
|
-
const saveResult = await Promise.allSettled(modelNotInit.map((m)=>this.saveModel(m)));
|
|
1812
|
-
return saveResult;
|
|
1813
|
-
}
|
|
1814
|
-
async *execute(arg) {
|
|
1815
|
-
await this.initInternalPrompt();
|
|
1816
|
-
const stream = new ElementChunkStream();
|
|
1817
|
-
await this.loadAllPromptsWithInternal();
|
|
1818
|
-
switch(runningState.type){
|
|
1819
|
-
case 0:
|
|
1820
|
-
{
|
|
1821
|
-
yield* this.displayModelList();
|
|
1822
|
-
return;
|
|
1823
|
-
}
|
|
1824
|
-
case 1:
|
|
1825
|
-
{
|
|
1826
|
-
{
|
|
1827
|
-
runningState = {
|
|
1828
|
-
type: 2,
|
|
1829
|
-
modelKey: runningState.modelKey,
|
|
1830
|
-
promptKey: MacroPrompt.defaultPromptWithUserInput.promptKey
|
|
1831
|
-
};
|
|
1832
|
-
yield* this.createDialog(runningState);
|
|
1833
|
-
}
|
|
1834
|
-
return;
|
|
1835
|
-
}
|
|
1836
|
-
case 2:
|
|
1837
|
-
{
|
|
1838
|
-
const prompt = this.getPromptByKey(runningState.promptKey);
|
|
1839
|
-
if (!prompt) {
|
|
1840
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
1841
|
-
children: "找不到该提示词"
|
|
1842
|
-
}));
|
|
1843
|
-
runningState = {
|
|
1844
|
-
type: 0
|
|
1845
|
-
};
|
|
1846
|
-
return;
|
|
1847
|
-
}
|
|
1848
|
-
yield* this.createDialog(runningState);
|
|
1849
|
-
return;
|
|
1850
|
-
}
|
|
1851
|
-
case 3:
|
|
1852
|
-
{
|
|
1853
|
-
const prompt = this.getPromptByKey(runningState.promptKey);
|
|
1854
|
-
if (!prompt) {
|
|
1855
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
1856
|
-
children: "找不到该提示词"
|
|
1857
|
-
}));
|
|
1858
|
-
runningState = {
|
|
1859
|
-
type: 0
|
|
1860
|
-
};
|
|
1861
|
-
return;
|
|
1862
|
-
}
|
|
1863
|
-
const query = this.editorState.queryOrSelection;
|
|
1864
|
-
if (!query) {
|
|
1865
|
-
runningState = {
|
|
1866
|
-
type: 0
|
|
1867
|
-
};
|
|
1868
|
-
yield* this.displayModelList();
|
|
1869
|
-
return;
|
|
1870
|
-
}
|
|
1871
|
-
yield* this.continueDialog(runningState);
|
|
1872
|
-
return;
|
|
1873
|
-
}
|
|
1874
|
-
}
|
|
1875
|
-
}
|
|
1876
|
-
async *handleCommand(commandName, data) {
|
|
1877
|
-
// interface 我卡不住,所以就卡这里吧
|
|
1878
|
-
const params = {
|
|
1879
|
-
commandName,
|
|
1880
|
-
data
|
|
1881
|
-
};
|
|
1882
|
-
this.debugLog({
|
|
1883
|
-
object: params
|
|
1884
|
-
});
|
|
1885
|
-
const stream = new ElementChunkStream();
|
|
1886
|
-
switch(params.commandName){
|
|
1887
|
-
case "comate.ievalue.prompt.selectModel":
|
|
1888
|
-
{
|
|
1889
|
-
const { modelKey } = params.data;
|
|
1890
|
-
runningState = {
|
|
1891
|
-
type: 1,
|
|
1892
|
-
modelKey
|
|
1893
|
-
};
|
|
1894
|
-
const model = await this.getModelByKey(modelKey);
|
|
1895
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
1896
|
-
children: [
|
|
1897
|
-
"已切换模型为:",
|
|
1898
|
-
model?.displayName || ''
|
|
1899
|
-
]
|
|
1900
|
-
}));
|
|
1901
|
-
yield* this.displayPromptList(runningState);
|
|
1902
|
-
return;
|
|
1903
|
-
}
|
|
1904
|
-
case "comate.ievalue.prompt.runPrompt":
|
|
1905
|
-
{
|
|
1906
|
-
const { promptKey, previousState } = params.data;
|
|
1907
|
-
runningState = {
|
|
1908
|
-
type: 2,
|
|
1909
|
-
modelKey: previousState.modelKey,
|
|
1910
|
-
promptKey
|
|
1911
|
-
};
|
|
1912
|
-
{
|
|
1913
|
-
yield* this.createDialog({
|
|
1914
|
-
type: 2,
|
|
1915
|
-
modelKey: previousState.modelKey,
|
|
1916
|
-
promptKey
|
|
1917
|
-
});
|
|
1918
|
-
}
|
|
1919
|
-
return;
|
|
1920
|
-
}
|
|
1921
|
-
case "comate.ievalue.prompt.displayPromptList":
|
|
1922
|
-
{
|
|
1923
|
-
yield* this.displayPromptList(params.data.previousState);
|
|
1924
|
-
return;
|
|
1925
|
-
}
|
|
1926
|
-
case "comate.ievalue.prompt.displayModelList":
|
|
1927
|
-
{
|
|
1928
|
-
yield* this.displayModelList();
|
|
1929
|
-
return;
|
|
1930
|
-
}
|
|
1931
|
-
}
|
|
1932
|
-
}
|
|
1933
|
-
async *displayModelList() {
|
|
1934
|
-
const allModel = await this.loadAllModels();
|
|
1935
|
-
this.debugLog({
|
|
1936
|
-
allModel
|
|
1937
|
-
});
|
|
1938
|
-
const stream = new ElementChunkStream();
|
|
1939
|
-
yield stream.flush(/*#__PURE__*/ jsx("h1", {
|
|
1940
|
-
children: "请选择要使用的模型"
|
|
1941
|
-
}));
|
|
1942
|
-
yield stream.flush(/*#__PURE__*/ jsx("ul", {
|
|
1943
|
-
children: allModel.map((model)=>{
|
|
1944
|
-
return /*#__PURE__*/ jsx("li", {
|
|
1945
|
-
children: /*#__PURE__*/ jsx("flex", {
|
|
1946
|
-
betweenCentered: true,
|
|
1947
|
-
children: [
|
|
1948
|
-
/*#__PURE__*/ jsx("span", {
|
|
1949
|
-
children: [
|
|
1950
|
-
model.displayName,
|
|
1951
|
-
"(",
|
|
1952
|
-
/*#__PURE__*/ jsx("file-link", {
|
|
1953
|
-
to: this.pathForModelFile(model),
|
|
1954
|
-
line: 1,
|
|
1955
|
-
children: "参数"
|
|
1956
|
-
}),
|
|
1957
|
-
")"
|
|
1958
|
-
]
|
|
1959
|
-
}),
|
|
1960
|
-
commandButton$2({
|
|
1961
|
-
commandName: "comate.ievalue.prompt.selectModel",
|
|
1962
|
-
data: {
|
|
1963
|
-
modelKey: model.modelKey
|
|
1964
|
-
},
|
|
1965
|
-
content: '选择模型'
|
|
1966
|
-
})
|
|
1967
|
-
]
|
|
1968
|
-
})
|
|
1969
|
-
});
|
|
1970
|
-
})
|
|
1971
|
-
}));
|
|
1972
|
-
}
|
|
1973
|
-
async *displayPromptList(previousState) {
|
|
1974
|
-
const macroPromptList = await this.loadAllPromptsWithInternal();
|
|
1975
|
-
const model = await this.getModelByKey(previousState.modelKey);
|
|
1976
|
-
const stream = new ElementChunkStream();
|
|
1977
|
-
if (macroPromptList.length === 0) {
|
|
1978
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
1979
|
-
children: [
|
|
1980
|
-
"当前没有可执行的提示词,您可以使用",
|
|
1981
|
-
/*#__PURE__*/ jsx("code", {
|
|
1982
|
-
children: "/生成提示词"
|
|
1983
|
-
}),
|
|
1984
|
-
"创建,或输入内容直接开始对话"
|
|
1985
|
-
]
|
|
1986
|
-
}));
|
|
1987
|
-
return;
|
|
1988
|
-
}
|
|
1989
|
-
yield stream.flush(/*#__PURE__*/ jsx("h1", {
|
|
1990
|
-
children: "可用提示词如下"
|
|
1991
|
-
}));
|
|
1992
|
-
yield stream.flush(/*#__PURE__*/ jsx("ul", {
|
|
1993
|
-
children: macroPromptList.map((macroPrompt)=>{
|
|
1994
|
-
const prompt = macroPrompt.data;
|
|
1995
|
-
return /*#__PURE__*/ jsx("li", {
|
|
1996
|
-
children: /*#__PURE__*/ jsx("flex", {
|
|
1997
|
-
betweenCentered: true,
|
|
1998
|
-
children: [
|
|
1999
|
-
/*#__PURE__*/ jsx("span", {
|
|
2000
|
-
children: prompt.promptName
|
|
2001
|
-
}),
|
|
2002
|
-
prompt.versionName,
|
|
2003
|
-
"-",
|
|
2004
|
-
prompt.spaceName,
|
|
2005
|
-
commandButton$2({
|
|
2006
|
-
commandName: "comate.ievalue.prompt.runPrompt",
|
|
2007
|
-
data: {
|
|
2008
|
-
promptKey: macroPrompt.promptKey,
|
|
2009
|
-
previousState
|
|
2010
|
-
},
|
|
2011
|
-
content: '点击执行'
|
|
2012
|
-
})
|
|
2013
|
-
]
|
|
2014
|
-
})
|
|
2015
|
-
});
|
|
2016
|
-
})
|
|
2017
|
-
}));
|
|
2018
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2019
|
-
children: [
|
|
2020
|
-
"当前模型为",
|
|
2021
|
-
/*#__PURE__*/ jsx("strong", {
|
|
2022
|
-
children: model?.displayName ?? ''
|
|
2023
|
-
}),
|
|
2024
|
-
", 您可以",
|
|
2025
|
-
/*#__PURE__*/ jsx("code", {
|
|
2026
|
-
children: "选择提示词"
|
|
2027
|
-
}),
|
|
2028
|
-
"、",
|
|
2029
|
-
/*#__PURE__*/ jsx("code", {
|
|
2030
|
-
children: "直接输入问题"
|
|
2031
|
-
}),
|
|
2032
|
-
"或",
|
|
2033
|
-
/*#__PURE__*/ jsx("code", {
|
|
2034
|
-
children: "框选编辑区文本"
|
|
2035
|
-
}),
|
|
2036
|
-
"来开始对话"
|
|
2037
|
-
]
|
|
2038
|
-
}));
|
|
2039
|
-
}
|
|
2040
|
-
async *createDialog(previousState) {
|
|
2041
|
-
const stream = new ElementChunkStream();
|
|
2042
|
-
const prompt = await this.getPromptByKey(previousState.promptKey);
|
|
2043
|
-
if (!prompt) {
|
|
2044
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2045
|
-
children: "找不到该提示词"
|
|
2046
|
-
}));
|
|
2047
|
-
return;
|
|
2048
|
-
}
|
|
2049
|
-
const model = await this.getModelByKey(previousState.modelKey);
|
|
2050
|
-
if (!model) {
|
|
2051
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2052
|
-
children: "找不到该模型"
|
|
2053
|
-
}));
|
|
2054
|
-
return;
|
|
2055
|
-
}
|
|
2056
|
-
const promptData = prompt.data;
|
|
2057
|
-
const username = await this.getUserName();
|
|
2058
|
-
if (!username) {
|
|
2059
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2060
|
-
children: "请先登录"
|
|
2061
|
-
}));
|
|
2062
|
-
return;
|
|
2063
|
-
}
|
|
2064
|
-
const apiKey = await this.getAPIKey();
|
|
2065
|
-
if (!apiKey) {
|
|
2066
|
-
const apiKeyPath = this.getPaths().userSetting;
|
|
2067
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2068
|
-
children: "请先在下面路径中配置API Key"
|
|
2069
|
-
}));
|
|
2070
|
-
yield stream.flush(/*#__PURE__*/ jsx("file-link", {
|
|
2071
|
-
to: apiKeyPath,
|
|
2072
|
-
line: 1,
|
|
2073
|
-
children: apiKeyPath
|
|
2074
|
-
}));
|
|
2075
|
-
return;
|
|
2076
|
-
}
|
|
2077
|
-
const result = await this.apiService.autoDialog({
|
|
2078
|
-
model,
|
|
2079
|
-
sessionID: undefined,
|
|
2080
|
-
prompt: prompt,
|
|
2081
|
-
localStates: {
|
|
2082
|
-
query: this.editorState.queryOrSelection,
|
|
2083
|
-
currentCode: this.editorState.activeFileContent
|
|
2084
|
-
},
|
|
2085
|
-
config: {
|
|
2086
|
-
username,
|
|
2087
|
-
apiKey
|
|
2088
|
-
}
|
|
2089
|
-
});
|
|
2090
|
-
this.log(result);
|
|
2091
|
-
const name = model.displayName;
|
|
2092
|
-
// 输出用户提示信息
|
|
2093
|
-
if (!result.success) {
|
|
2094
|
-
yield stream.flush(/*#__PURE__*/ jsx("h1", {
|
|
2095
|
-
children: [
|
|
2096
|
-
name,
|
|
2097
|
-
"执行失败"
|
|
2098
|
-
]
|
|
2099
|
-
}));
|
|
2100
|
-
yield stream.flush(/*#__PURE__*/ jsx("markdown", {
|
|
2101
|
-
children: result.message
|
|
2102
|
-
}));
|
|
2103
|
-
return;
|
|
2104
|
-
}
|
|
2105
|
-
yield stream.flush(/*#__PURE__*/ jsx("h1", {
|
|
2106
|
-
children: [
|
|
2107
|
-
name,
|
|
2108
|
-
"执行成功"
|
|
2109
|
-
]
|
|
2110
|
-
}));
|
|
2111
|
-
yield stream.flush(/*#__PURE__*/ jsx("h2", {
|
|
2112
|
-
children: "输入内容为:"
|
|
2113
|
-
}));
|
|
2114
|
-
yield stream.flush(/*#__PURE__*/ jsx("markdown", {
|
|
2115
|
-
children: result.input
|
|
2116
|
-
}));
|
|
2117
|
-
yield stream.flush(/*#__PURE__*/ jsx("hr", {}));
|
|
2118
|
-
yield stream.flush(/*#__PURE__*/ jsx("h2", {
|
|
2119
|
-
children: "输出内容为:"
|
|
2120
|
-
}));
|
|
2121
|
-
yield stream.flush(/*#__PURE__*/ jsx("markdown", {
|
|
2122
|
-
children: result.message
|
|
2123
|
-
}));
|
|
2124
|
-
// 更新 sessionID
|
|
2125
|
-
const sessionID = result.sessionID;
|
|
2126
|
-
runningState = {
|
|
2127
|
-
type: 3,
|
|
2128
|
-
promptKey: prompt.promptKey,
|
|
2129
|
-
modelKey: model.modelKey,
|
|
2130
|
-
sessionID
|
|
2131
|
-
};
|
|
2132
|
-
// 保存第一个代码块
|
|
2133
|
-
yield stream.flush(/*#__PURE__*/ jsx("hr", {}));
|
|
2134
|
-
const path = this.editorState.activeFilePath;
|
|
2135
|
-
if (path) {
|
|
2136
|
-
yield* this.saveFirstCode({
|
|
2137
|
-
output: result.output,
|
|
2138
|
-
prompt,
|
|
2139
|
-
stream,
|
|
2140
|
-
path
|
|
2141
|
-
});
|
|
2142
|
-
}
|
|
2143
|
-
const evalResult = await prompt.actions.afterCreateDialog({
|
|
2144
|
-
fileDir: Path.dirname(path),
|
|
2145
|
-
fileName: Path.basename(path, Path.extname(path)),
|
|
2146
|
-
fileExt: Path.extname(path),
|
|
2147
|
-
promptName: promptData.promptName,
|
|
2148
|
-
spaceName: promptData.spaceName,
|
|
2149
|
-
formatTime: formatTime(new Date()),
|
|
2150
|
-
aiResponse: result.output,
|
|
2151
|
-
aiResponseFirstCode: codeblocksInMarkdown(result.output)[0]?.content
|
|
2152
|
-
}, {
|
|
2153
|
-
cwd: this.init.cwd
|
|
2154
|
-
});
|
|
2155
|
-
if (!evalResult.success) {
|
|
2156
|
-
yield stream.flush(/*#__PURE__*/ jsx("h2", {
|
|
2157
|
-
children: "后置操作执行失败"
|
|
2158
|
-
}));
|
|
2159
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2160
|
-
children: evalResult.message
|
|
2161
|
-
}));
|
|
2162
|
-
} else if (evalResult.length) {
|
|
2163
|
-
yield stream.flush(/*#__PURE__*/ jsx("h2", {
|
|
2164
|
-
children: "后置操作执行成功"
|
|
2165
|
-
}));
|
|
2166
|
-
}
|
|
2167
|
-
const info = await this.additionalInfo({
|
|
2168
|
-
stream
|
|
2169
|
-
});
|
|
2170
|
-
const buttons = await this.additionalButtons({
|
|
2171
|
-
stream
|
|
2172
|
-
});
|
|
2173
|
-
yield stream.flush(/*#__PURE__*/ jsx("flex", {
|
|
2174
|
-
betweenCentered: true,
|
|
2175
|
-
children: [
|
|
2176
|
-
...buttons,
|
|
2177
|
-
...info
|
|
2178
|
-
]
|
|
2179
|
-
}));
|
|
2180
|
-
}
|
|
2181
|
-
async *continueDialog(previousState) {
|
|
2182
|
-
const stream = new ElementChunkStream();
|
|
2183
|
-
const username = await this.getUserName();
|
|
2184
|
-
if (!username) {
|
|
2185
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2186
|
-
children: "请先登录"
|
|
2187
|
-
}));
|
|
2188
|
-
return;
|
|
2189
|
-
}
|
|
2190
|
-
const apiKey = await this.getAPIKey();
|
|
2191
|
-
if (!apiKey) {
|
|
2192
|
-
const apiKeyPath = this.getPaths().userSetting;
|
|
2193
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2194
|
-
children: "请先在下面路径中配置API Key"
|
|
2195
|
-
}));
|
|
2196
|
-
yield stream.flush(/*#__PURE__*/ jsx("file-link", {
|
|
2197
|
-
to: apiKeyPath,
|
|
2198
|
-
line: 1,
|
|
2199
|
-
children: apiKeyPath
|
|
2200
|
-
}));
|
|
2201
|
-
return;
|
|
2202
|
-
}
|
|
2203
|
-
const model = await this.getModelByKey(previousState.modelKey);
|
|
2204
|
-
if (!model) {
|
|
2205
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2206
|
-
children: "找不到该模型"
|
|
2207
|
-
}));
|
|
2208
|
-
return;
|
|
2209
|
-
}
|
|
2210
|
-
const prompt = await this.getPromptByKey(previousState.promptKey);
|
|
2211
|
-
if (!prompt) {
|
|
2212
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2213
|
-
children: "找不到该提示词"
|
|
2214
|
-
}));
|
|
2215
|
-
return;
|
|
2216
|
-
}
|
|
2217
|
-
const { sessionID } = previousState;
|
|
2218
|
-
const result = await this.apiService.autoDialog({
|
|
2219
|
-
model,
|
|
2220
|
-
prompt: MacroPrompt.ofEmpty(),
|
|
2221
|
-
sessionID,
|
|
2222
|
-
localStates: {
|
|
2223
|
-
query: this.editorState.queryOrSelection,
|
|
2224
|
-
currentCode: this.editorState.activeFileContent
|
|
2225
|
-
},
|
|
2226
|
-
config: {
|
|
2227
|
-
username,
|
|
2228
|
-
apiKey
|
|
2229
|
-
}
|
|
2230
|
-
});
|
|
2231
|
-
this.log(result);
|
|
2232
|
-
const promptData = prompt.data;
|
|
2233
|
-
const name = model.displayName;
|
|
2234
|
-
// 输出用户提示信息
|
|
2235
|
-
if (!result.success) {
|
|
2236
|
-
yield stream.flush(/*#__PURE__*/ jsx("h1", {
|
|
2237
|
-
children: [
|
|
2238
|
-
name,
|
|
2239
|
-
"执行失败"
|
|
2240
|
-
]
|
|
2241
|
-
}));
|
|
2242
|
-
yield stream.flush(/*#__PURE__*/ jsx("markdown", {
|
|
2243
|
-
children: result.message
|
|
2244
|
-
}));
|
|
2245
|
-
return;
|
|
2246
|
-
}
|
|
2247
|
-
yield stream.flush(/*#__PURE__*/ jsx("h1", {
|
|
2248
|
-
children: [
|
|
2249
|
-
name,
|
|
2250
|
-
"执行成功"
|
|
2251
|
-
]
|
|
2252
|
-
}));
|
|
2253
|
-
yield stream.flush(/*#__PURE__*/ jsx("markdown", {
|
|
2254
|
-
children: result.message
|
|
2255
|
-
}));
|
|
2256
|
-
// 保存第一个代码块
|
|
2257
|
-
yield stream.flush(/*#__PURE__*/ jsx("hr", {}));
|
|
2258
|
-
const path = this.editorState.activeFilePath;
|
|
2259
|
-
if (path) {
|
|
2260
|
-
yield* this.saveFirstCode({
|
|
2261
|
-
output: result.output,
|
|
2262
|
-
prompt,
|
|
2263
|
-
stream,
|
|
2264
|
-
path
|
|
2265
|
-
});
|
|
2266
|
-
}
|
|
2267
|
-
const evalResult = await prompt.actions.afterContinueDialog({
|
|
2268
|
-
fileDir: Path.dirname(path),
|
|
2269
|
-
fileName: Path.basename(path, Path.extname(path)),
|
|
2270
|
-
fileExt: Path.extname(path),
|
|
2271
|
-
promptName: promptData.promptName,
|
|
2272
|
-
spaceName: promptData.spaceName,
|
|
2273
|
-
formatTime: formatTime(new Date()),
|
|
2274
|
-
aiResponse: result.output,
|
|
2275
|
-
aiResponseFirstCode: codeblocksInMarkdown(result.output)[0]?.content
|
|
2276
|
-
}, {
|
|
2277
|
-
cwd: this.init.cwd
|
|
2278
|
-
});
|
|
2279
|
-
if (!evalResult.success) {
|
|
2280
|
-
yield stream.flush(/*#__PURE__*/ jsx("h2", {
|
|
2281
|
-
children: "后置操作执行失败"
|
|
2282
|
-
}));
|
|
2283
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2284
|
-
children: evalResult.message
|
|
2285
|
-
}));
|
|
2286
|
-
} else if (evalResult.length) {
|
|
2287
|
-
yield stream.flush(/*#__PURE__*/ jsx("h2", {
|
|
2288
|
-
children: "后置操作执行成功"
|
|
2289
|
-
}));
|
|
2290
|
-
}
|
|
2291
|
-
const info = await this.additionalInfo({
|
|
2292
|
-
stream
|
|
2293
|
-
});
|
|
2294
|
-
const buttons = await this.additionalButtons({
|
|
2295
|
-
stream
|
|
2296
|
-
});
|
|
2297
|
-
yield stream.flush(/*#__PURE__*/ jsx("flex", {
|
|
2298
|
-
betweenCentered: true,
|
|
2299
|
-
children: [
|
|
2300
|
-
...buttons,
|
|
2301
|
-
...info
|
|
2302
|
-
]
|
|
2303
|
-
}));
|
|
2304
|
-
}
|
|
2305
|
-
async *saveFirstCode(params) {
|
|
2306
|
-
const { prompt, stream, path, output } = params;
|
|
2307
|
-
const codeList = codeblocksInMarkdown(output);
|
|
2308
|
-
const firstCode = codeList[0];
|
|
2309
|
-
const content = firstCode?.content;
|
|
2310
|
-
const filePathTemplate = await this.userSetting.getItem('exportFirstCode');
|
|
2311
|
-
if (!filePathTemplate) {
|
|
2312
|
-
return;
|
|
2313
|
-
}
|
|
2314
|
-
const exportFirstCode = await pathForResultCodeExport(filePathTemplate, {
|
|
2315
|
-
fileDir: Path.dirname(path),
|
|
2316
|
-
fileName: Path.basename(path, Path.extname(path)),
|
|
2317
|
-
fileExt: Path.extname(path),
|
|
2318
|
-
promptName: prompt.data.promptName,
|
|
2319
|
-
spaceName: prompt.data.spaceName,
|
|
2320
|
-
formatTime: formatTime(new Date())
|
|
2321
|
-
});
|
|
2322
|
-
if (content && exportFirstCode) {
|
|
2323
|
-
try {
|
|
2324
|
-
await fsp$1.writeFile(exportFirstCode, content);
|
|
2325
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2326
|
-
children: "已将第一个代码块保存到文件:"
|
|
2327
|
-
}));
|
|
2328
|
-
yield stream.flush(/*#__PURE__*/ jsx("file-link", {
|
|
2329
|
-
to: exportFirstCode,
|
|
2330
|
-
line: 1,
|
|
2331
|
-
children: exportFirstCode
|
|
2332
|
-
}));
|
|
2333
|
-
} catch (error) {
|
|
2334
|
-
this.debugLog(`保存第一个代码块失败: ${error}`);
|
|
2335
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2336
|
-
children: [
|
|
2337
|
-
"保存第一个代码块失败: ",
|
|
2338
|
-
String(error)
|
|
2339
|
-
]
|
|
2340
|
-
}));
|
|
2341
|
-
}
|
|
2342
|
-
}
|
|
2343
|
-
}
|
|
2344
|
-
async getCurrentPrompt() {
|
|
2345
|
-
if (runningState.type === 0 || runningState.type === 1) {
|
|
2346
|
-
return;
|
|
2347
|
-
}
|
|
2348
|
-
const promptKey = runningState.promptKey;
|
|
2349
|
-
const macroPrompt = this.getPromptByKey(promptKey);
|
|
2350
|
-
return macroPrompt;
|
|
2351
|
-
}
|
|
2352
|
-
async getCurrentModel() {
|
|
2353
|
-
if (runningState.type === 0) {
|
|
2354
|
-
return;
|
|
2355
|
-
}
|
|
2356
|
-
return this.getModelByKey(runningState.modelKey);
|
|
2357
|
-
}
|
|
2358
|
-
async additionalInfo(params) {
|
|
2359
|
-
const currentPrompt = await this.getCurrentPrompt();
|
|
2360
|
-
const currentModel = await this.getCurrentModel();
|
|
2361
|
-
const metaPromptPath = currentPrompt && this.pathForPromptMetaFile(currentPrompt);
|
|
2362
|
-
const info = [
|
|
2363
|
-
metaPromptPath && this.fileExist(metaPromptPath) && /*#__PURE__*/ jsx("file-link", {
|
|
2364
|
-
to: metaPromptPath,
|
|
2365
|
-
line: 1,
|
|
2366
|
-
children: "提示词模板"
|
|
2367
|
-
}),
|
|
2368
|
-
currentModel && /*#__PURE__*/ jsx("file-link", {
|
|
2369
|
-
to: this.pathForModelFile(currentModel),
|
|
2370
|
-
line: 1,
|
|
2371
|
-
children: "模型参数"
|
|
2372
|
-
})
|
|
2373
|
-
];
|
|
2374
|
-
return info;
|
|
2375
|
-
}
|
|
2376
|
-
async additionalButtons(params) {
|
|
2377
|
-
const currentPrompt = await this.getCurrentPrompt();
|
|
2378
|
-
const model = await this.getCurrentModel();
|
|
2379
|
-
const buttons = [
|
|
2380
|
-
currentPrompt && model && commandButton$2({
|
|
2381
|
-
content: '切换提示词',
|
|
2382
|
-
commandName: "comate.ievalue.prompt.displayPromptList",
|
|
2383
|
-
data: {
|
|
2384
|
-
previousState: {
|
|
2385
|
-
type: 1,
|
|
2386
|
-
modelKey: model.modelKey
|
|
2387
|
-
}
|
|
2388
|
-
}
|
|
2389
|
-
}),
|
|
2390
|
-
model && commandButton$2({
|
|
2391
|
-
commandName: "comate.ievalue.prompt.displayModelList",
|
|
2392
|
-
data: {},
|
|
2393
|
-
content: '切换模型'
|
|
2394
|
-
})
|
|
2395
|
-
];
|
|
2396
|
-
return buttons;
|
|
2397
|
-
}
|
|
2398
|
-
constructor(...args){
|
|
2399
|
-
super(...args), this.apiService = APIService.of({
|
|
2400
|
-
log: this.debugLog
|
|
2401
|
-
});
|
|
2402
|
-
}
|
|
2403
|
-
}
|
|
2404
|
-
|
|
2405
|
-
/**
|
|
2406
|
-
* 通用模型构造器
|
|
2407
|
-
* @param prompt 特殊模型的模版 prompt
|
|
2408
|
-
* @returns
|
|
2409
|
-
*/ function buildProvider(prompt, model) {
|
|
2410
|
-
let sessionID = undefined;
|
|
2411
|
-
class RunGPTProvider extends BasePromptProvider {
|
|
2412
|
-
static{
|
|
2413
|
-
this.skillName = skills.runGPT.name;
|
|
2414
|
-
}
|
|
2415
|
-
static{
|
|
2416
|
-
this.description = skills.runGPT.description;
|
|
2417
|
-
}
|
|
2418
|
-
static{
|
|
2419
|
-
this.parameters = {
|
|
2420
|
-
type: 'object',
|
|
2421
|
-
properties: {},
|
|
2422
|
-
required: []
|
|
2423
|
-
};
|
|
2424
|
-
}
|
|
2425
|
-
// private async *initPrompt() {
|
|
2426
|
-
// const allPrompt = await this.loadAllPrompts();
|
|
2427
|
-
// const key = defaultPrompt.promptKey;
|
|
2428
|
-
// const hasCreated = allPrompt.some(p => p.promptKey === key);
|
|
2429
|
-
// // 已经创建过,就不再重复创建,避免覆盖用户的自定义配置
|
|
2430
|
-
// if (!hasCreated) {
|
|
2431
|
-
// return this.savePrompt(defaultPrompt)
|
|
2432
|
-
// }
|
|
2433
|
-
// }
|
|
2434
|
-
async *execute(arg) {
|
|
2435
|
-
// yield* this.initPrompt();
|
|
2436
|
-
// const allPrompt = await this.loadAllPrompts();
|
|
2437
|
-
// const prompt = allPrompt.find(p => p.promptKey === defaultPrompt.promptKey);
|
|
2438
|
-
const stream = new ElementChunkStream();
|
|
2439
|
-
const apiKey = await this.getAPIKey();
|
|
2440
|
-
const username = await this.getUserName() || '';
|
|
2441
|
-
const input = this.editorState.queryOrSelection;
|
|
2442
|
-
if (!apiKey) {
|
|
2443
|
-
const apiKeyPath = this.getPaths().userSetting;
|
|
2444
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2445
|
-
children: "请先在下面路径中配置API Key"
|
|
2446
|
-
}));
|
|
2447
|
-
yield stream.flush(/*#__PURE__*/ jsx("file-link", {
|
|
2448
|
-
to: apiKeyPath,
|
|
2449
|
-
line: 1,
|
|
2450
|
-
children: apiKeyPath
|
|
2451
|
-
}));
|
|
2452
|
-
return;
|
|
2453
|
-
}
|
|
2454
|
-
const result = await this.apiService.autoDialog({
|
|
2455
|
-
model,
|
|
2456
|
-
prompt,
|
|
2457
|
-
sessionID,
|
|
2458
|
-
localStates: {
|
|
2459
|
-
query: input,
|
|
2460
|
-
currentCode: this.editorState.activeFileContent
|
|
2461
|
-
},
|
|
2462
|
-
config: {
|
|
2463
|
-
username,
|
|
2464
|
-
apiKey
|
|
2465
|
-
}
|
|
2466
|
-
});
|
|
2467
|
-
if (!result.success) {
|
|
2468
|
-
yield stream.flush(/*#__PURE__*/ jsx("h1", {
|
|
2469
|
-
children: "执行失败"
|
|
2470
|
-
}));
|
|
2471
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2472
|
-
children: result.message
|
|
2473
|
-
}));
|
|
2474
|
-
return;
|
|
2475
|
-
}
|
|
2476
|
-
sessionID = result.sessionID;
|
|
2477
|
-
yield stream.flush(/*#__PURE__*/ jsx("markdown", {
|
|
2478
|
-
children: result.message
|
|
2479
|
-
}));
|
|
2480
|
-
// yield stream.flush(<hr />);
|
|
2481
|
-
// const iEValueFile = this.pathForPromptIEValueFile(prompt);
|
|
2482
|
-
// const metaFile = this.pathForPromptMetaFile(prompt);
|
|
2483
|
-
// yield stream.flush(<p>
|
|
2484
|
-
// 修改提示词模板:
|
|
2485
|
-
// <file-link to={metaFile} line={1}>meta-prompt.md</file-link>
|
|
2486
|
-
// (<a href={USER_DOCS.metaPrompt}>模板文档</a>)
|
|
2487
|
-
// </p>);
|
|
2488
|
-
// yield stream.flush(<p>修改模型参数:<file-link to={iEValueFile} line={1}>system.jsonc</file-link></p>);
|
|
2489
|
-
}
|
|
2490
|
-
constructor(...args){
|
|
2491
|
-
super(...args), this.apiService = APIService.of({
|
|
2492
|
-
log: this.debugLog
|
|
2493
|
-
});
|
|
2494
|
-
}
|
|
2495
|
-
}
|
|
2496
|
-
return RunGPTProvider;
|
|
2497
|
-
}
|
|
2498
|
-
const RunGPTProvider = buildProvider(MacroPrompt.ofSimplePanda({
|
|
2499
|
-
promptName: '命令gpt-4o-mini',
|
|
2500
|
-
text: '{{IDE_QUERY}}',
|
|
2501
|
-
system: ''
|
|
2502
|
-
}), Model.ofName(ModelName.GPT_4O_MINI));
|
|
2503
|
-
const RunEB4Provider = buildProvider(MacroPrompt.ofSimplePanda({
|
|
2504
|
-
promptName: '命令eb4',
|
|
2505
|
-
text: '{{IDE_QUERY}}',
|
|
2506
|
-
system: ''
|
|
2507
|
-
}), Model.ofName(ModelName.ERNIE_BOT_4_0));
|
|
2508
|
-
|
|
2509
|
-
function commandButton$1({ commandName, data, content }) {
|
|
2510
|
-
return /*#__PURE__*/ jsx("command-button", {
|
|
2511
|
-
commandName: commandName,
|
|
2512
|
-
data: data,
|
|
2513
|
-
children: content
|
|
2514
|
-
});
|
|
2515
|
-
}
|
|
2516
|
-
class GeneratePromptByDescriptionProvider extends BasePromptProvider {
|
|
2517
|
-
static{
|
|
2518
|
-
this.skillName = skills.generatePromptByDescription.name;
|
|
2519
|
-
}
|
|
2520
|
-
static{
|
|
2521
|
-
this.description = skills.generatePromptByDescription.description;
|
|
2522
|
-
}
|
|
2523
|
-
static{
|
|
2524
|
-
this.parameters = {
|
|
2525
|
-
type: 'object',
|
|
2526
|
-
properties: {},
|
|
2527
|
-
required: []
|
|
2528
|
-
};
|
|
2529
|
-
}
|
|
2530
|
-
async *execute(arg) {
|
|
2531
|
-
const stream = new ElementChunkStream();
|
|
2532
|
-
const example = '你是一名资深前端,用ts实现下面的功能';
|
|
2533
|
-
const rawQuery = this.editorState.queryOrSelection;
|
|
2534
|
-
const query = rawQuery || example;
|
|
2535
|
-
const isExample = !rawQuery;
|
|
2536
|
-
const prompt = await this.apiService.generatePromptByDescription({
|
|
2537
|
-
query,
|
|
2538
|
-
config: {
|
|
2539
|
-
username: await this.getUserName() || '',
|
|
2540
|
-
apiKey: await this.getAPIKey() || ''
|
|
2541
|
-
}
|
|
2542
|
-
});
|
|
2543
|
-
try {
|
|
2544
|
-
yield stream.flush(/*#__PURE__*/ jsx("h1", {
|
|
2545
|
-
children: "原始描述"
|
|
2546
|
-
}));
|
|
2547
|
-
yield stream.flush(/*#__PURE__*/ jsx("span", {
|
|
2548
|
-
children: [
|
|
2549
|
-
isExample ? '[示例]' : '',
|
|
2550
|
-
query
|
|
2551
|
-
]
|
|
2552
|
-
}));
|
|
2553
|
-
yield stream.flush(/*#__PURE__*/ jsx("h1", {
|
|
2554
|
-
children: "生成结果:"
|
|
2555
|
-
}));
|
|
2556
|
-
const { result, systemResult } = prompt.data;
|
|
2557
|
-
const arr = [
|
|
2558
|
-
{
|
|
2559
|
-
title: 'system',
|
|
2560
|
-
value: systemResult
|
|
2561
|
-
},
|
|
2562
|
-
{
|
|
2563
|
-
title: 'prompt内容',
|
|
2564
|
-
value: result
|
|
2565
|
-
}
|
|
2566
|
-
];
|
|
2567
|
-
for (const { value, title } of arr){
|
|
2568
|
-
if (value) {
|
|
2569
|
-
yield stream.flush(/*#__PURE__*/ jsx("span", {
|
|
2570
|
-
children: title
|
|
2571
|
-
}));
|
|
2572
|
-
yield stream.flush(/*#__PURE__*/ jsx("code-block", {
|
|
2573
|
-
closed: false,
|
|
2574
|
-
language: "",
|
|
2575
|
-
children: value
|
|
2576
|
-
}));
|
|
2577
|
-
}
|
|
2578
|
-
}
|
|
2579
|
-
yield stream.flush(commandButton$1({
|
|
2580
|
-
commandName: 'comate.ievalue.prompt.createPrompt',
|
|
2581
|
-
data: {
|
|
2582
|
-
system: systemResult,
|
|
2583
|
-
prompt: result,
|
|
2584
|
-
title: query.slice(0, 10) + `(${formatTime(new Date())})`
|
|
2585
|
-
},
|
|
2586
|
-
content: '保存提示词'
|
|
2587
|
-
}));
|
|
2588
|
-
} catch (err) {
|
|
2589
|
-
yield stream.flush(/*#__PURE__*/ jsx("code-block", {
|
|
2590
|
-
closed: false,
|
|
2591
|
-
language: "",
|
|
2592
|
-
children: JSON.stringify(err, null, 2)
|
|
2593
|
-
}));
|
|
2594
|
-
}
|
|
2595
|
-
}
|
|
2596
|
-
async *handleCommand(commandName, data) {
|
|
2597
|
-
// interface 我卡不住,所以就卡这里吧
|
|
2598
|
-
const object = {
|
|
2599
|
-
commandName,
|
|
2600
|
-
data
|
|
2601
|
-
};
|
|
2602
|
-
this.debugLog({
|
|
2603
|
-
object
|
|
2604
|
-
});
|
|
2605
|
-
switch(object.commandName){
|
|
2606
|
-
case 'comate.ievalue.prompt.createPrompt':
|
|
2607
|
-
{
|
|
2608
|
-
const { system, prompt, title } = object.data;
|
|
2609
|
-
const macroPrompt = MacroPrompt.ofSimplePanda({
|
|
2610
|
-
text: prompt,
|
|
2611
|
-
system,
|
|
2612
|
-
promptName: title
|
|
2613
|
-
});
|
|
2614
|
-
yield* this.savePromptWithStream(macroPrompt);
|
|
2615
|
-
}
|
|
2616
|
-
}
|
|
2617
|
-
}
|
|
2618
|
-
constructor(...args){
|
|
2619
|
-
super(...args), this.apiService = APIService.of({
|
|
2620
|
-
log: this.debugLog
|
|
2621
|
-
});
|
|
2622
|
-
}
|
|
2623
|
-
}
|
|
2624
|
-
|
|
2625
|
-
const fsp = fs$1.promises;
|
|
2626
|
-
function commandButton({ commandName, data, content }) {
|
|
2627
|
-
return /*#__PURE__*/ jsx("command-button", {
|
|
2628
|
-
commandName: commandName,
|
|
2629
|
-
data: data,
|
|
2630
|
-
children: content
|
|
2631
|
-
});
|
|
2632
|
-
}
|
|
2633
|
-
class GeneratePromptByExampleProvider extends BasePromptProvider {
|
|
2634
|
-
static{
|
|
2635
|
-
this.skillName = skills.generatePromptByExample.name;
|
|
2636
|
-
}
|
|
2637
|
-
static{
|
|
2638
|
-
this.description = skills.generatePromptByExample.description;
|
|
2639
|
-
}
|
|
2640
|
-
static{
|
|
2641
|
-
this.parameters = {
|
|
2642
|
-
type: 'object',
|
|
2643
|
-
properties: {},
|
|
2644
|
-
required: []
|
|
2645
|
-
};
|
|
2646
|
-
}
|
|
2647
|
-
async getFileExample(file, log) {
|
|
2648
|
-
const absolutePath = Path$1.resolve(this.projectRoot, file.path || '');
|
|
2649
|
-
const ext = Path$1.extname(absolutePath);
|
|
2650
|
-
const fileContent = await fsp.readFile(absolutePath);
|
|
2651
|
-
switch(ext){
|
|
2652
|
-
// case '.xlsx': {
|
|
2653
|
-
// const sheet = xlsx.parse(fileContent);
|
|
2654
|
-
// const rows = sheet[0]
|
|
2655
|
-
// .data
|
|
2656
|
-
// // NOTE: 因为第一行是表头
|
|
2657
|
-
// .slice(1);
|
|
2658
|
-
// const examples = rows.map(row => ({
|
|
2659
|
-
// input: row[0] || '',
|
|
2660
|
-
// output: row[1] || '',
|
|
2661
|
-
// }));
|
|
2662
|
-
// return examples;
|
|
2663
|
-
// }
|
|
2664
|
-
case '.csv':
|
|
2665
|
-
{
|
|
2666
|
-
const rows = fileContent.toString().split('\n')// NOTE: 因为第一行是表头
|
|
2667
|
-
.slice(1);
|
|
2668
|
-
const examples = rows.map((row)=>{
|
|
2669
|
-
const [input, output] = row.split(',');
|
|
2670
|
-
return {
|
|
2671
|
-
input: input || '',
|
|
2672
|
-
output: output || ''
|
|
2673
|
-
};
|
|
2674
|
-
});
|
|
2675
|
-
return examples;
|
|
2676
|
-
}
|
|
2677
|
-
default:
|
|
2678
|
-
return [];
|
|
2679
|
-
}
|
|
2680
|
-
}
|
|
2681
|
-
async *showExampleFile(stream) {
|
|
2682
|
-
const { csvPath } = await this.createExampleFile();
|
|
2683
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2684
|
-
children: "输入 # 选择文件,支持 .csv 格式"
|
|
2685
|
-
}));
|
|
2686
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2687
|
-
children: "文件第一行为表头,示例文件请参考"
|
|
2688
|
-
}));
|
|
2689
|
-
yield stream.flush(/*#__PURE__*/ jsx("file-link", {
|
|
2690
|
-
to: csvPath,
|
|
2691
|
-
line: 1,
|
|
2692
|
-
children: csvPath
|
|
2693
|
-
}));
|
|
2694
|
-
// yield stream.flush(<file-link to={xlsxPath} line={1}>{xlsxPath}</file-link>);
|
|
2695
|
-
// yield stream.flush(<p>xlsx格式可能无法直接用 vscode 打开,请使用excel等软件查看</p>);
|
|
2696
|
-
}
|
|
2697
|
-
async *execute(arg) {
|
|
2698
|
-
const stream = new ElementChunkStream();
|
|
2699
|
-
const selectedKnowledgeList = this.retriever.selectedKnowledgeList();
|
|
2700
|
-
const files = selectedKnowledgeList.filter((item)=>item.type === 'FILE' || item.type === 'CURRENT_FILE');
|
|
2701
|
-
if (files.length === 0) {
|
|
2702
|
-
yield* this.showExampleFile(stream);
|
|
2703
|
-
return;
|
|
2704
|
-
}
|
|
2705
|
-
try {
|
|
2706
|
-
const examples = await this.getFileExample(files[0], this.debugLog);
|
|
2707
|
-
if (examples.length === 0) {
|
|
2708
|
-
yield stream.flush(/*#__PURE__*/ jsx("p", {
|
|
2709
|
-
children: "文件中没有示例数据"
|
|
2710
|
-
}));
|
|
2711
|
-
yield* this.showExampleFile(stream);
|
|
2712
|
-
return;
|
|
2713
|
-
}
|
|
2714
|
-
const prompt = await this.apiService.generatePromptByExample({
|
|
2715
|
-
examples,
|
|
2716
|
-
config: {
|
|
2717
|
-
username: await this.getUserName() || '',
|
|
2718
|
-
apiKey: await this.getAPIKey() || ''
|
|
2719
|
-
}
|
|
2720
|
-
});
|
|
2721
|
-
yield stream.flush(/*#__PURE__*/ jsx("span", {
|
|
2722
|
-
children: [
|
|
2723
|
-
"共选中了",
|
|
2724
|
-
files.length,
|
|
2725
|
-
"个文件",
|
|
2726
|
-
examples.length,
|
|
2727
|
-
"个示例"
|
|
2728
|
-
]
|
|
2729
|
-
}));
|
|
2730
|
-
const { result, systemResult } = prompt.data;
|
|
2731
|
-
const arr = [
|
|
2732
|
-
{
|
|
2733
|
-
title: 'system',
|
|
2734
|
-
value: systemResult
|
|
2735
|
-
},
|
|
2736
|
-
{
|
|
2737
|
-
title: 'prompt内容',
|
|
2738
|
-
value: result
|
|
2739
|
-
}
|
|
2740
|
-
];
|
|
2741
|
-
// yield stream.flush(<code-block closed={false} language="json">{JSON.stringify(prompt, null, 2)}</code-block>);
|
|
2742
|
-
for (const { value, title } of arr){
|
|
2743
|
-
if (value) {
|
|
2744
|
-
yield stream.flush(/*#__PURE__*/ jsx("span", {
|
|
2745
|
-
children: title
|
|
2746
|
-
}));
|
|
2747
|
-
yield stream.flush(/*#__PURE__*/ jsx("code-block", {
|
|
2748
|
-
closed: false,
|
|
2749
|
-
language: "",
|
|
2750
|
-
children: value
|
|
2751
|
-
}));
|
|
2752
|
-
}
|
|
2753
|
-
}
|
|
2754
|
-
yield stream.flush(commandButton({
|
|
2755
|
-
commandName: 'comate.ievalue.prompt.createPrompt',
|
|
2756
|
-
data: {
|
|
2757
|
-
system: systemResult,
|
|
2758
|
-
prompt: result,
|
|
2759
|
-
title: `新建提示词(${formatTime(new Date())})`
|
|
2760
|
-
},
|
|
2761
|
-
content: '保存提示词'
|
|
2762
|
-
}));
|
|
2763
|
-
} catch (err) {
|
|
2764
|
-
yield stream.flush(/*#__PURE__*/ jsx("code-block", {
|
|
2765
|
-
closed: false,
|
|
2766
|
-
language: "",
|
|
2767
|
-
children: JSON.stringify(err, null, 2)
|
|
2768
|
-
}));
|
|
2769
|
-
}
|
|
2770
|
-
}
|
|
2771
|
-
async *handleCommand(commandName, data) {
|
|
2772
|
-
// interface 我卡不住,所以就卡这里吧
|
|
2773
|
-
const object = {
|
|
2774
|
-
commandName,
|
|
2775
|
-
data
|
|
2776
|
-
};
|
|
2777
|
-
this.debugLog({
|
|
2778
|
-
object
|
|
2779
|
-
});
|
|
2780
|
-
switch(object.commandName){
|
|
2781
|
-
case 'comate.ievalue.prompt.createPrompt':
|
|
2782
|
-
{
|
|
2783
|
-
const { system, prompt, title } = object.data;
|
|
2784
|
-
const macroPrompt = MacroPrompt.ofSimplePanda({
|
|
2785
|
-
text: prompt,
|
|
2786
|
-
system,
|
|
2787
|
-
promptName: title
|
|
2788
|
-
});
|
|
2789
|
-
yield* this.savePromptWithStream(macroPrompt);
|
|
2790
|
-
}
|
|
2791
|
-
}
|
|
2792
|
-
}
|
|
2793
|
-
constructor(...args){
|
|
2794
|
-
super(...args), this.apiService = APIService.of({
|
|
2795
|
-
log: this.debugLog
|
|
2796
|
-
}), this.createExampleFile = async ()=>{
|
|
2797
|
-
const data = [
|
|
2798
|
-
[
|
|
2799
|
-
'问题',
|
|
2800
|
-
'答案'
|
|
2801
|
-
],
|
|
2802
|
-
[
|
|
2803
|
-
'1 + 1 等于多少?',
|
|
2804
|
-
'2'
|
|
2805
|
-
],
|
|
2806
|
-
[
|
|
2807
|
-
'2 + 2 等于多少?',
|
|
2808
|
-
'4'
|
|
2809
|
-
],
|
|
2810
|
-
[
|
|
2811
|
-
'3 + 5 等于多少?',
|
|
2812
|
-
'8'
|
|
2813
|
-
]
|
|
2814
|
-
];
|
|
2815
|
-
// 将数据转换为工作表
|
|
2816
|
-
// const xlsxPath = Path.resolve(this.ievalueRoot, '示例.xlsx');
|
|
2817
|
-
const csvPath = Path$1.resolve(this.ievalueRoot, '示例.csv');
|
|
2818
|
-
// const xlsxBuffer = xlsx.build([{name: 'Sheet1', data: data, options: {}}]);
|
|
2819
|
-
const csvData = data.map((row)=>row.join(',')).join('\n');
|
|
2820
|
-
// await createFileIfNotExists(xlsxPath, xlsxBuffer);
|
|
2821
|
-
await createFileIfNotExists(csvPath, csvData);
|
|
2822
|
-
return {
|
|
2823
|
-
// xlsxPath,
|
|
2824
|
-
csvPath
|
|
2825
|
-
};
|
|
2826
|
-
};
|
|
2827
|
-
}
|
|
2828
|
-
}
|
|
2829
|
-
|
|
2830
|
-
class DiagnosisPromptProvider extends BasePromptProvider {
|
|
2831
|
-
static{
|
|
2832
|
-
this.skillName = skills.diagnosisPrompt.name;
|
|
2833
|
-
}
|
|
2834
|
-
static{
|
|
2835
|
-
this.description = skills.diagnosisPrompt.description;
|
|
2836
|
-
}
|
|
2837
|
-
static{
|
|
2838
|
-
this.parameters = {
|
|
2839
|
-
type: 'object',
|
|
2840
|
-
properties: {},
|
|
2841
|
-
required: []
|
|
2842
|
-
};
|
|
2843
|
-
}
|
|
2844
|
-
async *execute(arg) {
|
|
2845
|
-
const query = this.editorState.queryOrSelection;
|
|
2846
|
-
const username = await this.getUserName();
|
|
2847
|
-
const stream = new ElementChunkStream();
|
|
2848
|
-
if (!username) {
|
|
2849
|
-
yield stream.flush(/*#__PURE__*/ jsx("span", {
|
|
2850
|
-
children: "请先登录"
|
|
2851
|
-
}));
|
|
2852
|
-
return;
|
|
2853
|
-
}
|
|
2854
|
-
if (!query) {
|
|
2855
|
-
yield stream.flush(/*#__PURE__*/ jsx("span", {
|
|
2856
|
-
children: "请先输入诊断内容"
|
|
2857
|
-
}));
|
|
2858
|
-
return;
|
|
2859
|
-
}
|
|
2860
|
-
const result = await this.apiService.diagnosisPrompt({
|
|
2861
|
-
text: query,
|
|
2862
|
-
username
|
|
2863
|
-
});
|
|
2864
|
-
// yield stream.flush(<span>{JSON.stringify(result, null, 2)}</span>);
|
|
2865
|
-
const msgList = result.data.result ?? [];
|
|
2866
|
-
for (const msg of msgList){
|
|
2867
|
-
const { reason, resultSrc, resultTgt, resultFlag, resultType } = msg;
|
|
2868
|
-
const resultTypeName = resultTypeNameMapping[resultType];
|
|
2869
|
-
const resultFlagName = resultFlag === 0 ? '✅' : '❌';
|
|
2870
|
-
yield stream.flush(/*#__PURE__*/ jsx("h1", {
|
|
2871
|
-
children: [
|
|
2872
|
-
resultTypeName,
|
|
2873
|
-
" ",
|
|
2874
|
-
resultFlagName
|
|
2875
|
-
]
|
|
2876
|
-
}));
|
|
2877
|
-
yield stream.flush(/*#__PURE__*/ jsx("span", {
|
|
2878
|
-
children: reason
|
|
2879
|
-
}));
|
|
2880
|
-
const arr = [
|
|
2881
|
-
{
|
|
2882
|
-
title: '原始语句',
|
|
2883
|
-
value: resultSrc
|
|
2884
|
-
},
|
|
2885
|
-
{
|
|
2886
|
-
title: '改进建议',
|
|
2887
|
-
value: resultTgt
|
|
2888
|
-
}
|
|
2889
|
-
].filter((item)=>item.value);
|
|
2890
|
-
for (const { title, value } of arr){
|
|
2891
|
-
yield stream.flush(/*#__PURE__*/ jsx("h2", {
|
|
2892
|
-
children: title
|
|
2893
|
-
}));
|
|
2894
|
-
yield stream.flush(/*#__PURE__*/ jsx("code-block", {
|
|
2895
|
-
closed: false,
|
|
2896
|
-
language: "",
|
|
2897
|
-
children: value
|
|
2898
|
-
}));
|
|
2899
|
-
}
|
|
2900
|
-
}
|
|
2901
|
-
}
|
|
2902
|
-
constructor(...args){
|
|
2903
|
-
super(...args), this.apiService = APIService.of({
|
|
2904
|
-
log: this.debugLog
|
|
2905
|
-
});
|
|
2906
|
-
}
|
|
2907
|
-
}
|
|
2908
|
-
|
|
2909
|
-
function setup({ registry }) {
|
|
2910
|
-
registry.registerFallbackProvider('help', HelpFallbackProvider);
|
|
2911
|
-
registry.registerSkillProvider(skills.runGPT.name, RunGPTProvider);
|
|
2912
|
-
registry.registerSkillProvider(skills.runEB.name, RunEB4Provider);
|
|
2913
|
-
registry.registerSkillProvider(skills.runPrompt.name, RunPromptProvider);
|
|
2914
|
-
registry.registerSkillProvider(skills.generatePromptByDescription.name, GeneratePromptByDescriptionProvider);
|
|
2915
|
-
registry.registerSkillProvider(skills.getPrompt.name, GetPromptProvider);
|
|
2916
|
-
registry.registerSkillProvider(skills.generatePromptByExample.name, GeneratePromptByExampleProvider);
|
|
2917
|
-
registry.registerSkillProvider(skills.diagnosisPrompt.name, DiagnosisPromptProvider);
|
|
2918
|
-
}
|
|
2919
|
-
|
|
2920
|
-
export { setup };
|