@becrafter/prompt-manager 0.1.22 → 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/package.json +31 -24
- package/packages/resources/tools/agent-browser/README.md +640 -0
- package/packages/resources/tools/agent-browser/agent-browser.tool.js +1389 -0
- package/packages/resources/tools/thinking/README.md +324 -0
- package/packages/resources/tools/thinking/thinking.tool.js +911 -0
- package/packages/server/README.md +3 -4
- package/packages/server/api/admin.routes.js +668 -664
- package/packages/server/api/open.routes.js +68 -67
- package/packages/server/api/surge.routes.js +5 -6
- package/packages/server/api/tool.routes.js +70 -71
- package/packages/server/app.js +70 -73
- package/packages/server/configs/authors.json +40 -0
- package/packages/server/configs/models/built-in/bigmodel.yaml +6 -6
- package/packages/server/configs/models/providers.yaml +4 -4
- package/packages/server/configs/templates/built-in/general-iteration.yaml +1 -1
- package/packages/server/configs/templates/built-in/general-optimize.yaml +1 -1
- package/packages/server/configs/templates/built-in/output-format-optimize.yaml +1 -1
- package/packages/server/index.js +3 -9
- package/packages/server/mcp/heartbeat-patch.js +1 -3
- package/packages/server/mcp/mcp.server.js +64 -134
- package/packages/server/mcp/prompt.handler.js +101 -95
- package/packages/server/middlewares/auth.middleware.js +31 -31
- package/packages/server/server.js +60 -45
- package/packages/server/services/TerminalService.js +156 -70
- package/packages/server/services/WebSocketService.js +35 -34
- package/packages/server/services/author-config.service.js +199 -0
- package/packages/server/services/manager.js +66 -60
- package/packages/server/services/model.service.js +5 -9
- package/packages/server/services/optimization.service.js +25 -22
- package/packages/server/services/template.service.js +3 -8
- package/packages/server/toolm/author-sync.service.js +97 -0
- package/packages/server/toolm/index.js +1 -2
- package/packages/server/toolm/package-installer.service.js +47 -50
- package/packages/server/toolm/tool-context.service.js +64 -62
- package/packages/server/toolm/tool-dependency.service.js +28 -30
- package/packages/server/toolm/tool-description-generator-optimized.service.js +55 -55
- package/packages/server/toolm/tool-description-generator.service.js +20 -23
- package/packages/server/toolm/tool-environment.service.js +45 -44
- package/packages/server/toolm/tool-execution.service.js +49 -48
- package/packages/server/toolm/tool-loader.service.js +13 -18
- package/packages/server/toolm/tool-logger.service.js +33 -39
- package/packages/server/toolm/tool-manager.handler.js +17 -15
- package/packages/server/toolm/tool-manual-generator.service.js +107 -87
- package/packages/server/toolm/tool-mode-handlers.service.js +52 -59
- package/packages/server/toolm/tool-storage.service.js +11 -12
- package/packages/server/toolm/tool-sync.service.js +36 -39
- package/packages/server/toolm/tool-utils.js +0 -1
- package/packages/server/toolm/tool-yaml-parser.service.js +12 -11
- package/packages/server/toolm/validate-system.js +56 -84
- package/packages/server/utils/config.js +97 -12
- package/packages/server/utils/logger.js +1 -1
- package/packages/server/utils/port-checker.js +8 -8
- package/packages/server/utils/util.js +470 -467
- package/packages/resources/tools/cognitive-thinking/README.md +0 -284
- package/packages/resources/tools/cognitive-thinking/cognitive-thinking.tool.js +0 -837
- package/packages/server/mcp/sequential-thinking.handler.js +0 -318
- package/packages/server/mcp/think-plan.handler.js +0 -274
- package/packages/server/mcp/thinking-toolkit.handler.js +0 -380
- package/packages/web/0.d1c5a72339dfc32ad86a.js +0 -1
- package/packages/web/112.8807b976372b2b0541a8.js +0 -1
- package/packages/web/130.584c7e365da413f5d9be.js +0 -1
- package/packages/web/142.72c985bc29720f975cca.js +0 -1
- package/packages/web/165.a05fc53bf84d18db36b8.js +0 -2
- package/packages/web/165.a05fc53bf84d18db36b8.js.LICENSE.txt +0 -9
- package/packages/web/203.724ab9f717b80554c397.js +0 -1
- package/packages/web/241.bf941d4f02866795f64a.js +0 -1
- package/packages/web/249.54cfb224af63f5f5ec55.js +0 -1
- package/packages/web/291.6df35042f8f296fca7cd.js +0 -1
- package/packages/web/319.2fab900a31b29873f666.js +0 -1
- package/packages/web/32.c78d866281995ec33a7b.js +0 -1
- package/packages/web/325.9ca297d0f73f38468ce9.js +0 -1
- package/packages/web/366.2f9b48fdbf8eee039e57.js +0 -1
- package/packages/web/378.6be08c612cd5a3ef97dc.js +0 -1
- package/packages/web/393.7a2f817515c5e90623d7.js +0 -1
- package/packages/web/412.062df5f732d5ba203415.js +0 -1
- package/packages/web/426.08656fef4918b3fb19ad.js +0 -1
- package/packages/web/465.2be8018327130a3bd798.js +0 -1
- package/packages/web/48.8ca96fc93667a715e67a.js +0 -1
- package/packages/web/480.44c1f1a2927486ac3d4f.js +0 -1
- package/packages/web/489.e041a8d0db15dc96d607.js +0 -1
- package/packages/web/490.9ffb26c907de020d671b.js +0 -1
- package/packages/web/492.58781369e348d91fc06a.js +0 -1
- package/packages/web/495.ed63e99791a87167c6b3.js +0 -1
- package/packages/web/510.4cc07ab7d30d5c1cd17f.js +0 -1
- package/packages/web/543.3af155ed4fa237664308.js +0 -1
- package/packages/web/567.f04ab60f8e2c2fb0745a.js +0 -1
- package/packages/web/592.f3ad085fa9c1849daa06.js +0 -1
- package/packages/web/616.b03fb801b3433b17750f.js +0 -1
- package/packages/web/617.d88def54921d2c4dc44c.js +0 -1
- package/packages/web/641.d30787d674f548928261.js +0 -1
- package/packages/web/672.5269c8399fa42a5af95d.js +0 -1
- package/packages/web/731.97cab92b71811c502bda.js +0 -1
- package/packages/web/746.3947c6f0235407e420fb.js +0 -1
- package/packages/web/756.a53233b3f3913900d5ac.js +0 -1
- package/packages/web/77.68801af593a28a631fbf.js +0 -1
- package/packages/web/802.53b2bff3cf2a69f7b80c.js +0 -1
- package/packages/web/815.b6dfab82265f56c7e046.js +0 -1
- package/packages/web/821.f5a13e5c735aac244eb9.js +0 -1
- package/packages/web/846.b9bf97d5f559270675ce.js +0 -1
- package/packages/web/869.7c10403f500e6201407f.js +0 -1
- package/packages/web/885.135050364f99e6924fb5.js +0 -1
- package/packages/web/901.fd5aeb9df630609a2b43.js +0 -1
- package/packages/web/928.f67e590de3caa4daa3ae.js +0 -1
- package/packages/web/955.d833403521ba4dd567ee.js +0 -1
- package/packages/web/981.a45cb745cf424044c8c8.js +0 -1
- package/packages/web/992.645320b60c74c8787482.js +0 -1
- package/packages/web/996.ed9a963dc9e7439eca9a.js +0 -1
- package/packages/web/css/codemirror-theme_xq-light.css +0 -43
- package/packages/web/css/codemirror.css +0 -344
- package/packages/web/css/main.196f434e6a88cd448158.css +0 -7278
- package/packages/web/css/terminal-fix.css +0 -571
- package/packages/web/index.html +0 -3
- package/packages/web/main.dceff50c7307dda04873.js +0 -2
- package/packages/web/main.dceff50c7307dda04873.js.LICENSE.txt +0 -3
|
@@ -1,837 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 认知思考工具 (Cognitive Thinking Tool)
|
|
3
|
-
*
|
|
4
|
-
* 战略意义:
|
|
5
|
-
* 1. 架构纯粹性:将分散的思考工具统一为一个自适应工具
|
|
6
|
-
* 2. 智能路由:基于用户需求自动选择最适合的思考模式
|
|
7
|
-
* 3. 系统整合:让思考过程更加连贯和智能化
|
|
8
|
-
* 4. 自适应调用:根据上下文动态调整思考策略
|
|
9
|
-
*
|
|
10
|
-
* 核心功能:
|
|
11
|
-
* - 自适应思考路由:自动识别探索性vs执行性需求
|
|
12
|
-
* - 顺序思考模式:动态反思性思维,支持分支和修订
|
|
13
|
-
* - 规划执行模式:结构化思考-计划-行动的三元结构
|
|
14
|
-
* - 融合工具模式:根据场景参数显式选择思考模式
|
|
15
|
-
*
|
|
16
|
-
* 注意:此工具将在独立沙箱环境中运行,依赖将自动安装到工具目录的 node_modules 中
|
|
17
|
-
* 所有日志将输出到 ~/.prompt-manager/toolbox/cognitive-thinking/run.log 文件中
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 思考状态管理类
|
|
22
|
-
* 支持顺序思考的状态追踪
|
|
23
|
-
*/
|
|
24
|
-
class ThinkingState {
|
|
25
|
-
constructor() {
|
|
26
|
-
this.thoughts = [];
|
|
27
|
-
this.currentThoughtNumber = 0;
|
|
28
|
-
this.branches = new Map();
|
|
29
|
-
this.revisions = [];
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
addThought(thought, metadata = {}) {
|
|
33
|
-
const thoughtObj = {
|
|
34
|
-
number: this.currentThoughtNumber + 1,
|
|
35
|
-
content: thought,
|
|
36
|
-
timestamp: new Date().toISOString(),
|
|
37
|
-
...metadata
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
this.thoughts.push(thoughtObj);
|
|
41
|
-
this.currentThoughtNumber = thoughtObj.number;
|
|
42
|
-
|
|
43
|
-
return thoughtObj;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
reviseThought(thoughtNumber, newThought) {
|
|
47
|
-
const originalThought = this.thoughts.find(t => t.number === thoughtNumber);
|
|
48
|
-
if (!originalThought) {
|
|
49
|
-
throw new Error(`思考 ${thoughtNumber} 不存在`);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const revision = {
|
|
53
|
-
originalNumber: thoughtNumber,
|
|
54
|
-
newContent: newThought,
|
|
55
|
-
timestamp: new Date().toISOString()
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
this.revisions.push(revision);
|
|
59
|
-
originalThought.revised = true;
|
|
60
|
-
originalThought.revision = revision;
|
|
61
|
-
|
|
62
|
-
return revision;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
createBranch(fromThoughtNumber, branchId, branchThought) {
|
|
66
|
-
const branch = {
|
|
67
|
-
fromThought: fromThoughtNumber,
|
|
68
|
-
branchId: branchId,
|
|
69
|
-
thoughts: [branchThought],
|
|
70
|
-
timestamp: new Date().toISOString()
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
this.branches.set(branchId, branch);
|
|
74
|
-
return branch;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
getThoughts() {
|
|
78
|
-
return this.thoughts;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
getBranches() {
|
|
82
|
-
return Array.from(this.branches.values());
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
getRevisions() {
|
|
86
|
-
return this.revisions;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
getSummary() {
|
|
90
|
-
return {
|
|
91
|
-
totalThoughts: this.thoughts.length,
|
|
92
|
-
totalBranches: this.branches.size,
|
|
93
|
-
totalRevisions: this.revisions.length,
|
|
94
|
-
currentThought: this.currentThoughtNumber
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
reset() {
|
|
99
|
-
this.thoughts = [];
|
|
100
|
-
this.currentThoughtNumber = 0;
|
|
101
|
-
this.branches.clear();
|
|
102
|
-
this.revisions = [];
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* 思考和规划条目类
|
|
108
|
-
*/
|
|
109
|
-
class ThinkPlanEntry {
|
|
110
|
-
constructor(thoughtNumber, thought, plan, action) {
|
|
111
|
-
this.thoughtNumber = thoughtNumber;
|
|
112
|
-
this.thought = thought;
|
|
113
|
-
this.plan = plan;
|
|
114
|
-
this.action = action;
|
|
115
|
-
this.timestamp = new Date();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
toJSON() {
|
|
119
|
-
return {
|
|
120
|
-
thoughtNumber: this.thoughtNumber,
|
|
121
|
-
thought: this.thought,
|
|
122
|
-
plan: this.plan,
|
|
123
|
-
action: this.action,
|
|
124
|
-
timestamp: this.timestamp.toISOString()
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* 思考和规划内存管理类
|
|
131
|
-
*/
|
|
132
|
-
class ThinkPlanMemory {
|
|
133
|
-
constructor() {
|
|
134
|
-
this.entries = [];
|
|
135
|
-
this.mutex = new Map(); // 用于会话级别的并发控制
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
addEntry(thoughtNumber, thought, plan, action, sessionId = 'default') {
|
|
139
|
-
const entry = new ThinkPlanEntry(thoughtNumber, thought, plan, action);
|
|
140
|
-
|
|
141
|
-
if (!this.mutex.has(sessionId)) {
|
|
142
|
-
this.mutex.set(sessionId, []);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
this.mutex.get(sessionId).push(entry);
|
|
146
|
-
this.entries.push(entry);
|
|
147
|
-
|
|
148
|
-
return entry;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
getAllEntries(sessionId = 'default') {
|
|
152
|
-
if (sessionId === 'default') {
|
|
153
|
-
return [...this.entries];
|
|
154
|
-
}
|
|
155
|
-
return [...(this.mutex.get(sessionId) || [])];
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
getEntryByNumber(thoughtNumber, sessionId = 'default') {
|
|
159
|
-
const entries = sessionId === 'default' ? this.entries : (this.mutex.get(sessionId) || []);
|
|
160
|
-
return entries.find(entry => entry.thoughtNumber === thoughtNumber) || null;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
clearEntries(sessionId = 'default') {
|
|
164
|
-
if (sessionId === 'default') {
|
|
165
|
-
this.entries = [];
|
|
166
|
-
this.mutex.clear();
|
|
167
|
-
} else {
|
|
168
|
-
this.mutex.delete(sessionId);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
getSummary(sessionId = 'default') {
|
|
173
|
-
const entries = this.getAllEntries(sessionId);
|
|
174
|
-
|
|
175
|
-
if (entries.length === 0) {
|
|
176
|
-
return '暂无思考和规划记录';
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
let summary = `思考和规划过程摘要 (共 ${entries.length} 个步骤):\n\n`;
|
|
180
|
-
|
|
181
|
-
entries.forEach((entry, index) => {
|
|
182
|
-
summary += `${index + 1}. [${entry.thoughtNumber}] ${entry.thought}\n`;
|
|
183
|
-
if (index < entries.length - 1) {
|
|
184
|
-
summary += '\n';
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
return summary;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
getCount(sessionId = 'default') {
|
|
192
|
-
const entries = this.getAllEntries(sessionId);
|
|
193
|
-
return entries.length;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
// 全局状态实例
|
|
198
|
-
const thinkingState = new ThinkingState();
|
|
199
|
-
const thinkPlanMemory = new ThinkPlanMemory();
|
|
200
|
-
|
|
201
|
-
// 思考模式枚举
|
|
202
|
-
const THINKING_MODES = {
|
|
203
|
-
EXPLORATORY: 'exploratory', // 探索性思考
|
|
204
|
-
EXECUTION: 'execution', // 执行性思考
|
|
205
|
-
ADAPTIVE: 'adaptive' // 自适应模式
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* 自适应思考路由器
|
|
210
|
-
* 根据输入内容自动识别合适的思考模式
|
|
211
|
-
*/
|
|
212
|
-
class AdaptiveRouter {
|
|
213
|
-
static analyzeThinkingMode(input) {
|
|
214
|
-
// 分析输入特征来判断思考模式
|
|
215
|
-
|
|
216
|
-
// 执行性思考的特征
|
|
217
|
-
const executionPatterns = [
|
|
218
|
-
/需要.*实现|要.*做|应该.*执行|计划.*步骤/i,
|
|
219
|
-
/部署|上线|发布|迁移|重构/i,
|
|
220
|
-
/完成.*任务|执行.*计划|实施.*方案/i,
|
|
221
|
-
/action|plan|执行|行动|步骤/i,
|
|
222
|
-
/\d+\..*然后|\d+\..*接着|\d+\..*最后/i, // 步骤化描述
|
|
223
|
-
/backup|deploy|test|verify/i // 技术操作关键词
|
|
224
|
-
];
|
|
225
|
-
|
|
226
|
-
// 探索性思考的特征
|
|
227
|
-
const exploratoryPatterns = [
|
|
228
|
-
/为什么|怎么|是什么|原因|分析|诊断/i,
|
|
229
|
-
/可能.*原因|潜在.*问题|需要.*调查/i,
|
|
230
|
-
/探索|发现|理解|学习|研究/i,
|
|
231
|
-
/thought|thinking|反思|假设|验证/i,
|
|
232
|
-
/如果.*那么|假设|可能|或许/i, // 假设性思考
|
|
233
|
-
/问题|issue|bug|error|异常/i // 问题诊断
|
|
234
|
-
];
|
|
235
|
-
|
|
236
|
-
const inputText = JSON.stringify(input).toLowerCase();
|
|
237
|
-
|
|
238
|
-
// 计算匹配分数
|
|
239
|
-
let executionScore = 0;
|
|
240
|
-
let exploratoryScore = 0;
|
|
241
|
-
|
|
242
|
-
executionPatterns.forEach(pattern => {
|
|
243
|
-
if (pattern.test(inputText)) executionScore++;
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
exploratoryPatterns.forEach(pattern => {
|
|
247
|
-
if (pattern.test(inputText)) exploratoryScore++;
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
// 如果明确指定了模式,使用指定模式
|
|
251
|
-
if (input.mode) {
|
|
252
|
-
return input.mode;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// 如果明确指定了场景,使用场景映射
|
|
256
|
-
if (input.scenario) {
|
|
257
|
-
return input.scenario === 'exploratory' ? THINKING_MODES.EXPLORATORY : THINKING_MODES.EXECUTION;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
// 根据分数判断模式
|
|
261
|
-
if (executionScore > exploratoryScore) {
|
|
262
|
-
return THINKING_MODES.EXECUTION;
|
|
263
|
-
} else if (exploratoryScore > executionScore) {
|
|
264
|
-
return THINKING_MODES.EXPLORATORY;
|
|
265
|
-
} else {
|
|
266
|
-
// 分数相等时,检查是否有明确的执行指示
|
|
267
|
-
if (input.action || input.plan || input.thoughtNumber) {
|
|
268
|
-
return THINKING_MODES.EXECUTION;
|
|
269
|
-
}
|
|
270
|
-
return THINKING_MODES.EXPLORATORY;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* 顺序思考处理器
|
|
277
|
-
*/
|
|
278
|
-
class SequentialThinkingHandler {
|
|
279
|
-
static async handle(args) {
|
|
280
|
-
const {
|
|
281
|
-
thought,
|
|
282
|
-
nextThoughtNeeded = true,
|
|
283
|
-
thoughtNumber,
|
|
284
|
-
totalThoughts = 10,
|
|
285
|
-
isRevision = false,
|
|
286
|
-
revisesThought,
|
|
287
|
-
branchFromThought,
|
|
288
|
-
branchId,
|
|
289
|
-
needsMoreThoughts = false
|
|
290
|
-
} = args;
|
|
291
|
-
|
|
292
|
-
if (!thought) {
|
|
293
|
-
throw new Error('缺少必需参数: thought');
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// 处理思考记录
|
|
297
|
-
let thoughtRecord;
|
|
298
|
-
|
|
299
|
-
if (isRevision && revisesThought) {
|
|
300
|
-
// 修订现有思考
|
|
301
|
-
thinkingState.reviseThought(revisesThought, thought);
|
|
302
|
-
thoughtRecord = {
|
|
303
|
-
number: revisesThought,
|
|
304
|
-
type: 'revision',
|
|
305
|
-
revisesThought: revisesThought,
|
|
306
|
-
content: thought
|
|
307
|
-
};
|
|
308
|
-
} else if (branchFromThought && branchId) {
|
|
309
|
-
// 创建分支
|
|
310
|
-
const branch = thinkingState.createBranch(branchFromThought, branchId, thought);
|
|
311
|
-
thoughtRecord = {
|
|
312
|
-
number: branchFromThought,
|
|
313
|
-
type: 'branch',
|
|
314
|
-
branchFromThought: branchFromThought,
|
|
315
|
-
branchId: branchId,
|
|
316
|
-
content: thought
|
|
317
|
-
};
|
|
318
|
-
} else {
|
|
319
|
-
// 添加新思考
|
|
320
|
-
const addedThought = thinkingState.addThought(thought, {
|
|
321
|
-
thoughtNumber: thoughtNumber,
|
|
322
|
-
totalThoughts: totalThoughts,
|
|
323
|
-
nextThoughtNeeded: nextThoughtNeeded,
|
|
324
|
-
needsMoreThoughts: needsMoreThoughts
|
|
325
|
-
});
|
|
326
|
-
thoughtRecord = {
|
|
327
|
-
number: addedThought.number,
|
|
328
|
-
type: 'regular',
|
|
329
|
-
content: thought
|
|
330
|
-
};
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
// 获取当前状态摘要
|
|
334
|
-
const summary = thinkingState.getSummary();
|
|
335
|
-
const allThoughts = thinkingState.getThoughts();
|
|
336
|
-
const branches = thinkingState.getBranches();
|
|
337
|
-
const revisions = thinkingState.getRevisions();
|
|
338
|
-
|
|
339
|
-
// 构建响应
|
|
340
|
-
const response = {
|
|
341
|
-
success: true,
|
|
342
|
-
thought: thoughtRecord,
|
|
343
|
-
summary: summary,
|
|
344
|
-
progress: {
|
|
345
|
-
current: thoughtNumber || summary.currentThought,
|
|
346
|
-
total: totalThoughts,
|
|
347
|
-
percentage: Math.round(((thoughtNumber || summary.currentThought) / totalThoughts) * 100)
|
|
348
|
-
},
|
|
349
|
-
nextThoughtNeeded: nextThoughtNeeded || needsMoreThoughts,
|
|
350
|
-
thoughts: allThoughts.map(t => ({
|
|
351
|
-
number: t.number,
|
|
352
|
-
content: t.content,
|
|
353
|
-
revised: t.revised || false
|
|
354
|
-
})),
|
|
355
|
-
...(branches.length > 0 && { branches: branches }),
|
|
356
|
-
...(revisions.length > 0 && { revisions: revisions })
|
|
357
|
-
};
|
|
358
|
-
|
|
359
|
-
// 如果不需要更多思考,可以生成最终总结
|
|
360
|
-
if (!nextThoughtNeeded && !needsMoreThoughts) {
|
|
361
|
-
response.finalSummary = SequentialThinkingHandler.generateFinalSummary(allThoughts, branches, revisions);
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
return response;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
static generateFinalSummary(thoughts, branches, revisions) {
|
|
368
|
-
const summary = {
|
|
369
|
-
totalSteps: thoughts.length,
|
|
370
|
-
keyInsights: [],
|
|
371
|
-
conclusions: [],
|
|
372
|
-
recommendations: []
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
// 提取关键洞察
|
|
376
|
-
thoughts.forEach(thought => {
|
|
377
|
-
if (thought.content.includes('关键') || thought.content.includes('重要') ||
|
|
378
|
-
thought.content.includes('结论') || thought.content.includes('发现')) {
|
|
379
|
-
summary.keyInsights.push({
|
|
380
|
-
step: thought.number,
|
|
381
|
-
content: thought.content
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
// 提取结论
|
|
387
|
-
const lastThoughts = thoughts.slice(-3);
|
|
388
|
-
summary.conclusions = lastThoughts.map(t => ({
|
|
389
|
-
step: t.number,
|
|
390
|
-
content: t.content
|
|
391
|
-
}));
|
|
392
|
-
|
|
393
|
-
return summary;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
static formatOutput(response) {
|
|
397
|
-
let output = '';
|
|
398
|
-
|
|
399
|
-
output += '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n';
|
|
400
|
-
output += '🧠 **顺序思考模式**\n';
|
|
401
|
-
output += '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n';
|
|
402
|
-
|
|
403
|
-
// 当前思考
|
|
404
|
-
output += `**思考 ${response.thought.number || response.summary.currentThought}**\n`;
|
|
405
|
-
output += `${response.thought.content}\n\n`;
|
|
406
|
-
|
|
407
|
-
// 进度信息
|
|
408
|
-
output += `**进度**: ${response.progress.current}/${response.progress.total} (${response.progress.percentage}%)\n\n`;
|
|
409
|
-
|
|
410
|
-
// 思考历史
|
|
411
|
-
if (response.thoughts && response.thoughts.length > 0) {
|
|
412
|
-
output += '**思考历史**:\n';
|
|
413
|
-
response.thoughts.forEach(t => {
|
|
414
|
-
const marker = t.revised ? '↩️' : '•';
|
|
415
|
-
output += `${marker} [${t.number}] ${t.content.substring(0, 100)}${t.content.length > 100 ? '...' : ''}\n`;
|
|
416
|
-
});
|
|
417
|
-
output += '\n';
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
// 分支信息
|
|
421
|
-
if (response.branches && response.branches.length > 0) {
|
|
422
|
-
output += '**分支**:\n';
|
|
423
|
-
response.branches.forEach(branch => {
|
|
424
|
-
output += ` └─ 分支 ${branch.branchId} (来自思考 ${branch.fromThought})\n`;
|
|
425
|
-
});
|
|
426
|
-
output += '\n';
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
// 修订信息
|
|
430
|
-
if (response.revisions && response.revisions.length > 0) {
|
|
431
|
-
output += '**修订**:\n';
|
|
432
|
-
response.revisions.forEach(rev => {
|
|
433
|
-
output += ` ↻ 思考 ${rev.originalNumber} 已修订\n`;
|
|
434
|
-
});
|
|
435
|
-
output += '\n';
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
// 下一步
|
|
439
|
-
if (response.nextThoughtNeeded) {
|
|
440
|
-
output += '**下一步**: 继续思考...\n';
|
|
441
|
-
} else {
|
|
442
|
-
output += '**状态**: 思考完成\n';
|
|
443
|
-
if (response.finalSummary) {
|
|
444
|
-
output += '\n**最终总结**:\n';
|
|
445
|
-
output += `- 总步骤数: ${response.finalSummary.totalSteps}\n`;
|
|
446
|
-
if (response.finalSummary.keyInsights.length > 0) {
|
|
447
|
-
output += '- 关键洞察:\n';
|
|
448
|
-
response.finalSummary.keyInsights.forEach(insight => {
|
|
449
|
-
output += ` • [步骤 ${insight.step}] ${insight.content}\n`;
|
|
450
|
-
});
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
output += '\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n';
|
|
456
|
-
|
|
457
|
-
return output;
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
/**
|
|
462
|
-
* 思考规划处理器
|
|
463
|
-
*/
|
|
464
|
-
class ThinkPlanHandler {
|
|
465
|
-
static async handle(args) {
|
|
466
|
-
const {
|
|
467
|
-
thought,
|
|
468
|
-
plan,
|
|
469
|
-
action,
|
|
470
|
-
thoughtNumber,
|
|
471
|
-
sessionId = 'default'
|
|
472
|
-
} = args;
|
|
473
|
-
|
|
474
|
-
// 验证必需参数
|
|
475
|
-
if (!thought || thought.trim().length === 0) {
|
|
476
|
-
throw new Error('缺少必需参数: thought');
|
|
477
|
-
}
|
|
478
|
-
if (!plan || plan.trim().length === 0) {
|
|
479
|
-
throw new Error('缺少必需参数: plan');
|
|
480
|
-
}
|
|
481
|
-
if (!action || action.trim().length === 0) {
|
|
482
|
-
throw new Error('缺少必需参数: action');
|
|
483
|
-
}
|
|
484
|
-
if (!thoughtNumber || thoughtNumber.trim().length === 0) {
|
|
485
|
-
throw new Error('缺少必需参数: thoughtNumber');
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
// 添加新条目
|
|
489
|
-
const entry = thinkPlanMemory.addEntry(thoughtNumber, thought, plan, action, sessionId);
|
|
490
|
-
const currentCount = thinkPlanMemory.getCount(sessionId);
|
|
491
|
-
|
|
492
|
-
// 构建响应
|
|
493
|
-
const response = {
|
|
494
|
-
success: true,
|
|
495
|
-
entry: entry,
|
|
496
|
-
currentCount: currentCount,
|
|
497
|
-
sessionId: sessionId
|
|
498
|
-
};
|
|
499
|
-
|
|
500
|
-
return response;
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
static formatOutput(response) {
|
|
504
|
-
const entry = response.entry;
|
|
505
|
-
const timestamp = entry.timestamp.toLocaleString('zh-CN', {
|
|
506
|
-
year: 'numeric',
|
|
507
|
-
month: '2-digit',
|
|
508
|
-
day: '2-digit',
|
|
509
|
-
hour: '2-digit',
|
|
510
|
-
minute: '2-digit',
|
|
511
|
-
second: '2-digit'
|
|
512
|
-
});
|
|
513
|
-
|
|
514
|
-
let output = '';
|
|
515
|
-
output += '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n';
|
|
516
|
-
output += '📋 **思考规划模式**\n';
|
|
517
|
-
output += '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n';
|
|
518
|
-
|
|
519
|
-
output += `**思考编号**: [${entry.thoughtNumber}]\n\n`;
|
|
520
|
-
|
|
521
|
-
output += '🤔 **思考内容**:\n';
|
|
522
|
-
output += `${entry.thought}\n\n`;
|
|
523
|
-
|
|
524
|
-
output += '📋 **规划方案**:\n';
|
|
525
|
-
output += `${entry.plan}\n\n`;
|
|
526
|
-
|
|
527
|
-
output += '🎯 **下一步行动**:\n';
|
|
528
|
-
output += `${entry.action}\n\n`;
|
|
529
|
-
|
|
530
|
-
output += `⏰ **记录时间**: ${timestamp}\n\n`;
|
|
531
|
-
output += `📊 **当前已记录**: ${response.currentCount} 个思考步骤\n`;
|
|
532
|
-
output += `🔖 **会话ID**: ${response.sessionId}\n`;
|
|
533
|
-
|
|
534
|
-
output += '\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n';
|
|
535
|
-
|
|
536
|
-
return output;
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
/**
|
|
541
|
-
* 主工具类
|
|
542
|
-
*/
|
|
543
|
-
export default {
|
|
544
|
-
/**
|
|
545
|
-
* 获取工具依赖
|
|
546
|
-
* 返回的依赖将被安装到工具的独立 node_modules 目录中
|
|
547
|
-
*/
|
|
548
|
-
getDependencies() {
|
|
549
|
-
return {
|
|
550
|
-
// 此工具不依赖第三方包,使用 Node.js 内置模块
|
|
551
|
-
};
|
|
552
|
-
},
|
|
553
|
-
|
|
554
|
-
/**
|
|
555
|
-
* 获取工具元信息
|
|
556
|
-
*/
|
|
557
|
-
getMetadata() {
|
|
558
|
-
return {
|
|
559
|
-
id: 'cognitive-thinking',
|
|
560
|
-
name: '认知思考工具',
|
|
561
|
-
description: '统一的认知思考工具,支持自适应路由、顺序思考和规划执行三种模式,让系统架构更加纯粹',
|
|
562
|
-
version: '1.0.0',
|
|
563
|
-
category: 'ai',
|
|
564
|
-
author: 'Prompt Manager',
|
|
565
|
-
tags: ['thinking', 'cognitive', 'adaptive', 'planning', 'exploratory'],
|
|
566
|
-
scenarios: [
|
|
567
|
-
'复杂问题分析和诊断',
|
|
568
|
-
'项目规划和执行',
|
|
569
|
-
'决策制定和策略思考',
|
|
570
|
-
'创意发散和方案探索',
|
|
571
|
-
'系统架构设计',
|
|
572
|
-
'技术方案评估'
|
|
573
|
-
],
|
|
574
|
-
limitations: [
|
|
575
|
-
'不直接执行外部操作',
|
|
576
|
-
'依赖用户提供准确的上下文信息',
|
|
577
|
-
'思考深度受限于输入信息的质量'
|
|
578
|
-
]
|
|
579
|
-
};
|
|
580
|
-
},
|
|
581
|
-
|
|
582
|
-
/**
|
|
583
|
-
* 获取参数Schema
|
|
584
|
-
*/
|
|
585
|
-
getSchema() {
|
|
586
|
-
return {
|
|
587
|
-
parameters: {
|
|
588
|
-
type: 'object',
|
|
589
|
-
properties: {
|
|
590
|
-
// 自适应模式参数
|
|
591
|
-
mode: {
|
|
592
|
-
type: 'string',
|
|
593
|
-
description: '思考模式,可选:adaptive(自适应)、exploratory(探索性)、execution(执行性)',
|
|
594
|
-
enum: ['adaptive', 'exploratory', 'execution'],
|
|
595
|
-
default: 'adaptive'
|
|
596
|
-
},
|
|
597
|
-
|
|
598
|
-
// 顺序思考模式参数
|
|
599
|
-
thought: {
|
|
600
|
-
type: 'string',
|
|
601
|
-
description: '当前思考内容(顺序思考模式必需)'
|
|
602
|
-
},
|
|
603
|
-
nextThoughtNeeded: {
|
|
604
|
-
type: 'boolean',
|
|
605
|
-
description: '是否需要继续思考',
|
|
606
|
-
default: true
|
|
607
|
-
},
|
|
608
|
-
thoughtNumber: {
|
|
609
|
-
type: 'number',
|
|
610
|
-
description: '思考步骤编号'
|
|
611
|
-
},
|
|
612
|
-
totalThoughts: {
|
|
613
|
-
type: 'number',
|
|
614
|
-
description: '预计总思考步骤数',
|
|
615
|
-
default: 10
|
|
616
|
-
},
|
|
617
|
-
isRevision: {
|
|
618
|
-
type: 'boolean',
|
|
619
|
-
description: '是否为对之前思考的修订',
|
|
620
|
-
default: false
|
|
621
|
-
},
|
|
622
|
-
revisesThought: {
|
|
623
|
-
type: 'number',
|
|
624
|
-
description: '被修订的思考编号(当isRevision为true时必需)'
|
|
625
|
-
},
|
|
626
|
-
branchFromThought: {
|
|
627
|
-
type: 'number',
|
|
628
|
-
description: '分支起始的思考编号'
|
|
629
|
-
},
|
|
630
|
-
branchId: {
|
|
631
|
-
type: 'string',
|
|
632
|
-
description: '分支标识符'
|
|
633
|
-
},
|
|
634
|
-
needsMoreThoughts: {
|
|
635
|
-
type: 'boolean',
|
|
636
|
-
description: '是否需要更多思考步骤',
|
|
637
|
-
default: false
|
|
638
|
-
},
|
|
639
|
-
|
|
640
|
-
// 规划执行模式参数
|
|
641
|
-
plan: {
|
|
642
|
-
type: 'string',
|
|
643
|
-
description: '规划方案(执行性模式必需)'
|
|
644
|
-
},
|
|
645
|
-
action: {
|
|
646
|
-
type: 'string',
|
|
647
|
-
description: '下一步行动(执行性模式必需)'
|
|
648
|
-
},
|
|
649
|
-
sessionId: {
|
|
650
|
-
type: 'string',
|
|
651
|
-
description: '会话标识符',
|
|
652
|
-
default: 'default'
|
|
653
|
-
}
|
|
654
|
-
},
|
|
655
|
-
required: [] // 自适应模式下参数可动态确定
|
|
656
|
-
},
|
|
657
|
-
environment: {
|
|
658
|
-
type: 'object',
|
|
659
|
-
properties: {
|
|
660
|
-
DEFAULT_SESSION_ID: {
|
|
661
|
-
type: 'string',
|
|
662
|
-
description: '默认会话ID',
|
|
663
|
-
default: 'default'
|
|
664
|
-
},
|
|
665
|
-
MAX_THINKING_STEPS: {
|
|
666
|
-
type: 'number',
|
|
667
|
-
description: '最大思考步骤数限制',
|
|
668
|
-
default: 100
|
|
669
|
-
}
|
|
670
|
-
},
|
|
671
|
-
required: []
|
|
672
|
-
}
|
|
673
|
-
};
|
|
674
|
-
},
|
|
675
|
-
|
|
676
|
-
/**
|
|
677
|
-
* 获取业务错误定义
|
|
678
|
-
*/
|
|
679
|
-
getBusinessErrors() {
|
|
680
|
-
return [
|
|
681
|
-
{
|
|
682
|
-
code: 'MISSING_THOUGHT',
|
|
683
|
-
description: '缺少必需的思考内容参数',
|
|
684
|
-
match: /缺少必需参数: thought/i,
|
|
685
|
-
solution: '请提供 thought 参数描述您的当前思考',
|
|
686
|
-
retryable: true
|
|
687
|
-
},
|
|
688
|
-
{
|
|
689
|
-
code: 'INVALID_MODE',
|
|
690
|
-
description: '无效的思考模式',
|
|
691
|
-
match: /不支持.*模式|无效.*模式/i,
|
|
692
|
-
solution: '请使用 adaptive、exploratory 或 execution 模式',
|
|
693
|
-
retryable: true
|
|
694
|
-
},
|
|
695
|
-
{
|
|
696
|
-
code: 'MISSING_EXECUTION_PARAMS',
|
|
697
|
-
description: '执行模式缺少必需参数',
|
|
698
|
-
match: /缺少必需参数.*plan|缺少必需参数.*action/i,
|
|
699
|
-
solution: '执行模式需要提供 thought、plan 和 action 参数',
|
|
700
|
-
retryable: true
|
|
701
|
-
},
|
|
702
|
-
{
|
|
703
|
-
code: 'THINKING_OVERFLOW',
|
|
704
|
-
description: '思考步骤超过限制',
|
|
705
|
-
match: /思考步骤.*超过.*限制/i,
|
|
706
|
-
solution: '请控制思考步骤数量,或使用不同的会话ID',
|
|
707
|
-
retryable: false
|
|
708
|
-
}
|
|
709
|
-
];
|
|
710
|
-
},
|
|
711
|
-
|
|
712
|
-
/**
|
|
713
|
-
* 执行工具
|
|
714
|
-
*/
|
|
715
|
-
async execute(params) {
|
|
716
|
-
const { api } = this;
|
|
717
|
-
|
|
718
|
-
// 记录执行开始
|
|
719
|
-
api?.logger?.info('认知思考工具执行开始', {
|
|
720
|
-
mode: params.mode,
|
|
721
|
-
hasThought: !!params.thought,
|
|
722
|
-
hasPlan: !!params.plan,
|
|
723
|
-
hasAction: !!params.action
|
|
724
|
-
});
|
|
725
|
-
|
|
726
|
-
try {
|
|
727
|
-
// 参数验证和预处理
|
|
728
|
-
const validatedParams = this.validateAndPreprocessParams(params);
|
|
729
|
-
|
|
730
|
-
// 自适应路由分析
|
|
731
|
-
const thinkingMode = AdaptiveRouter.analyzeThinkingMode(validatedParams);
|
|
732
|
-
api?.logger?.info('自适应路由分析结果', { detectedMode: thinkingMode });
|
|
733
|
-
|
|
734
|
-
let result;
|
|
735
|
-
|
|
736
|
-
// 根据模式路由到对应的处理器
|
|
737
|
-
switch (thinkingMode) {
|
|
738
|
-
case THINKING_MODES.EXPLORATORY:
|
|
739
|
-
result = await SequentialThinkingHandler.handle(validatedParams);
|
|
740
|
-
result.formattedOutput = SequentialThinkingHandler.formatOutput(result);
|
|
741
|
-
break;
|
|
742
|
-
|
|
743
|
-
case THINKING_MODES.EXECUTION:
|
|
744
|
-
result = await ThinkPlanHandler.handle(validatedParams);
|
|
745
|
-
result.formattedOutput = ThinkPlanHandler.formatOutput(result);
|
|
746
|
-
break;
|
|
747
|
-
|
|
748
|
-
default:
|
|
749
|
-
throw new Error(`不支持的思考模式: ${thinkingMode}`);
|
|
750
|
-
}
|
|
751
|
-
|
|
752
|
-
// 记录执行成功
|
|
753
|
-
api?.logger?.info('认知思考工具执行完成', {
|
|
754
|
-
mode: thinkingMode,
|
|
755
|
-
success: result.success
|
|
756
|
-
});
|
|
757
|
-
|
|
758
|
-
return result.formattedOutput;
|
|
759
|
-
|
|
760
|
-
} catch (error) {
|
|
761
|
-
// 记录错误
|
|
762
|
-
api?.logger?.error('认知思考工具执行失败', {
|
|
763
|
-
error: error.message,
|
|
764
|
-
params: JSON.stringify(params).substring(0, 200)
|
|
765
|
-
});
|
|
766
|
-
throw error;
|
|
767
|
-
}
|
|
768
|
-
},
|
|
769
|
-
|
|
770
|
-
/**
|
|
771
|
-
* 参数验证和预处理
|
|
772
|
-
*/
|
|
773
|
-
validateAndPreprocessParams(params) {
|
|
774
|
-
if (!params || typeof params !== 'object') {
|
|
775
|
-
throw new Error('参数必须是对象');
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
// 深度克隆参数避免修改原对象
|
|
779
|
-
const processedParams = JSON.parse(JSON.stringify(params));
|
|
780
|
-
|
|
781
|
-
// 基本类型转换
|
|
782
|
-
if (processedParams.thoughtNumber && typeof processedParams.thoughtNumber === 'string') {
|
|
783
|
-
const num = parseInt(processedParams.thoughtNumber, 10);
|
|
784
|
-
if (!isNaN(num)) {
|
|
785
|
-
processedParams.thoughtNumber = num;
|
|
786
|
-
}
|
|
787
|
-
}
|
|
788
|
-
|
|
789
|
-
if (processedParams.totalThoughts && typeof processedParams.totalThoughts === 'string') {
|
|
790
|
-
const num = parseInt(processedParams.totalThoughts, 10);
|
|
791
|
-
if (!isNaN(num)) {
|
|
792
|
-
processedParams.totalThoughts = num;
|
|
793
|
-
}
|
|
794
|
-
}
|
|
795
|
-
|
|
796
|
-
// 布尔值标准化
|
|
797
|
-
['nextThoughtNeeded', 'isRevision', 'needsMoreThoughts'].forEach(key => {
|
|
798
|
-
if (processedParams[key] !== undefined) {
|
|
799
|
-
processedParams[key] = Boolean(processedParams[key]);
|
|
800
|
-
}
|
|
801
|
-
});
|
|
802
|
-
|
|
803
|
-
return processedParams;
|
|
804
|
-
},
|
|
805
|
-
|
|
806
|
-
/**
|
|
807
|
-
* 获取思考状态摘要
|
|
808
|
-
*/
|
|
809
|
-
getThinkingSummary(sessionId = 'default') {
|
|
810
|
-
const thinkingSummary = thinkingState.getSummary();
|
|
811
|
-
const planningSummary = thinkPlanMemory.getSummary(sessionId);
|
|
812
|
-
|
|
813
|
-
return {
|
|
814
|
-
sequentialThinking: thinkingSummary,
|
|
815
|
-
thinkPlanning: planningSummary,
|
|
816
|
-
combinedStats: {
|
|
817
|
-
totalThinkingSteps: thinkingSummary.totalThoughts,
|
|
818
|
-
totalPlanningSteps: thinkPlanMemory.getCount(sessionId),
|
|
819
|
-
totalBranches: thinkingSummary.totalBranches,
|
|
820
|
-
totalRevisions: thinkingSummary.totalRevisions
|
|
821
|
-
}
|
|
822
|
-
};
|
|
823
|
-
},
|
|
824
|
-
|
|
825
|
-
/**
|
|
826
|
-
* 重置思考状态
|
|
827
|
-
*/
|
|
828
|
-
resetThinkingState(sessionId = 'default') {
|
|
829
|
-
thinkingState.reset();
|
|
830
|
-
thinkPlanMemory.clearEntries(sessionId);
|
|
831
|
-
|
|
832
|
-
return {
|
|
833
|
-
success: true,
|
|
834
|
-
message: `认知思考状态已重置 (会话: ${sessionId})`
|
|
835
|
-
};
|
|
836
|
-
}
|
|
837
|
-
};
|