@becrafter/prompt-manager 0.1.21 → 0.1.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/package.json +23 -23
  2. package/packages/resources/tools/agent-browser/README.md +640 -0
  3. package/packages/resources/tools/agent-browser/agent-browser.tool.js +1388 -0
  4. package/packages/resources/tools/thinking/README.md +324 -0
  5. package/packages/resources/tools/thinking/thinking.tool.js +911 -0
  6. package/packages/server/README.md +3 -4
  7. package/packages/server/api/admin.routes.js +668 -664
  8. package/packages/server/api/open.routes.js +68 -67
  9. package/packages/server/api/surge.routes.js +5 -6
  10. package/packages/server/api/tool.routes.js +70 -71
  11. package/packages/server/app.js +70 -73
  12. package/packages/server/configs/authors.json +40 -0
  13. package/packages/server/configs/models/built-in/bigmodel.yaml +6 -6
  14. package/packages/server/configs/models/providers.yaml +4 -4
  15. package/packages/server/configs/templates/built-in/general-iteration.yaml +1 -1
  16. package/packages/server/configs/templates/built-in/general-optimize.yaml +1 -1
  17. package/packages/server/configs/templates/built-in/output-format-optimize.yaml +1 -1
  18. package/packages/server/index.js +3 -9
  19. package/packages/server/mcp/heartbeat-patch.js +1 -3
  20. package/packages/server/mcp/mcp.server.js +64 -134
  21. package/packages/server/mcp/prompt.handler.js +101 -95
  22. package/packages/server/middlewares/auth.middleware.js +31 -31
  23. package/packages/server/server.js +60 -45
  24. package/packages/server/services/TerminalService.js +156 -70
  25. package/packages/server/services/WebSocketService.js +35 -34
  26. package/packages/server/services/author-config.service.js +199 -0
  27. package/packages/server/services/manager.js +66 -60
  28. package/packages/server/services/model.service.js +5 -9
  29. package/packages/server/services/optimization.service.js +25 -22
  30. package/packages/server/services/template.service.js +3 -8
  31. package/packages/server/toolm/author-sync.service.js +97 -0
  32. package/packages/server/toolm/index.js +1 -2
  33. package/packages/server/toolm/package-installer.service.js +47 -50
  34. package/packages/server/toolm/tool-context.service.js +64 -62
  35. package/packages/server/toolm/tool-dependency.service.js +28 -30
  36. package/packages/server/toolm/tool-description-generator-optimized.service.js +55 -55
  37. package/packages/server/toolm/tool-description-generator.service.js +20 -23
  38. package/packages/server/toolm/tool-environment.service.js +45 -44
  39. package/packages/server/toolm/tool-execution.service.js +49 -48
  40. package/packages/server/toolm/tool-loader.service.js +13 -18
  41. package/packages/server/toolm/tool-logger.service.js +33 -39
  42. package/packages/server/toolm/tool-manager.handler.js +17 -15
  43. package/packages/server/toolm/tool-manual-generator.service.js +107 -87
  44. package/packages/server/toolm/tool-mode-handlers.service.js +52 -59
  45. package/packages/server/toolm/tool-storage.service.js +11 -12
  46. package/packages/server/toolm/tool-sync.service.js +36 -39
  47. package/packages/server/toolm/tool-utils.js +0 -1
  48. package/packages/server/toolm/tool-yaml-parser.service.js +12 -11
  49. package/packages/server/toolm/validate-system.js +56 -84
  50. package/packages/server/utils/config.js +98 -13
  51. package/packages/server/utils/logger.js +1 -1
  52. package/packages/server/utils/port-checker.js +8 -8
  53. package/packages/server/utils/util.js +470 -467
  54. package/packages/resources/tools/cognitive-thinking/README.md +0 -284
  55. package/packages/resources/tools/cognitive-thinking/cognitive-thinking.tool.js +0 -837
  56. package/packages/server/mcp/sequential-thinking.handler.js +0 -318
  57. package/packages/server/mcp/think-plan.handler.js +0 -274
  58. package/packages/server/mcp/thinking-toolkit.handler.js +0 -380
  59. package/packages/web/0.d1c5a72339dfc32ad86a.js +0 -1
  60. package/packages/web/112.8807b976372b2b0541a8.js +0 -1
  61. package/packages/web/130.584c7e365da413f5d9be.js +0 -1
  62. package/packages/web/142.72c985bc29720f975cca.js +0 -1
  63. package/packages/web/165.a05fc53bf84d18db36b8.js +0 -2
  64. package/packages/web/165.a05fc53bf84d18db36b8.js.LICENSE.txt +0 -9
  65. package/packages/web/203.724ab9f717b80554c397.js +0 -1
  66. package/packages/web/241.bf941d4f02866795f64a.js +0 -1
  67. package/packages/web/249.54cfb224af63f5f5ec55.js +0 -1
  68. package/packages/web/291.6df35042f8f296fca7cd.js +0 -1
  69. package/packages/web/319.2fab900a31b29873f666.js +0 -1
  70. package/packages/web/32.c78d866281995ec33a7b.js +0 -1
  71. package/packages/web/325.9ca297d0f73f38468ce9.js +0 -1
  72. package/packages/web/366.2f9b48fdbf8eee039e57.js +0 -1
  73. package/packages/web/378.6be08c612cd5a3ef97dc.js +0 -1
  74. package/packages/web/393.7a2f817515c5e90623d7.js +0 -1
  75. package/packages/web/412.062df5f732d5ba203415.js +0 -1
  76. package/packages/web/426.08656fef4918b3fb19ad.js +0 -1
  77. package/packages/web/465.2be8018327130a3bd798.js +0 -1
  78. package/packages/web/48.8ca96fc93667a715e67a.js +0 -1
  79. package/packages/web/480.44c1f1a2927486ac3d4f.js +0 -1
  80. package/packages/web/489.e041a8d0db15dc96d607.js +0 -1
  81. package/packages/web/490.9ffb26c907de020d671b.js +0 -1
  82. package/packages/web/492.58781369e348d91fc06a.js +0 -1
  83. package/packages/web/495.ed63e99791a87167c6b3.js +0 -1
  84. package/packages/web/510.4cc07ab7d30d5c1cd17f.js +0 -1
  85. package/packages/web/543.3af155ed4fa237664308.js +0 -1
  86. package/packages/web/567.f04ab60f8e2c2fb0745a.js +0 -1
  87. package/packages/web/592.f3ad085fa9c1849daa06.js +0 -1
  88. package/packages/web/616.b03fb801b3433b17750f.js +0 -1
  89. package/packages/web/617.d88def54921d2c4dc44c.js +0 -1
  90. package/packages/web/641.d30787d674f548928261.js +0 -1
  91. package/packages/web/672.5269c8399fa42a5af95d.js +0 -1
  92. package/packages/web/731.97cab92b71811c502bda.js +0 -1
  93. package/packages/web/746.3947c6f0235407e420fb.js +0 -1
  94. package/packages/web/756.a53233b3f3913900d5ac.js +0 -1
  95. package/packages/web/77.68801af593a28a631fbf.js +0 -1
  96. package/packages/web/802.53b2bff3cf2a69f7b80c.js +0 -1
  97. package/packages/web/815.b6dfab82265f56c7e046.js +0 -1
  98. package/packages/web/821.f5a13e5c735aac244eb9.js +0 -1
  99. package/packages/web/846.b9bf97d5f559270675ce.js +0 -1
  100. package/packages/web/869.7c10403f500e6201407f.js +0 -1
  101. package/packages/web/885.135050364f99e6924fb5.js +0 -1
  102. package/packages/web/901.fd5aeb9df630609a2b43.js +0 -1
  103. package/packages/web/928.f67e590de3caa4daa3ae.js +0 -1
  104. package/packages/web/955.d833403521ba4dd567ee.js +0 -1
  105. package/packages/web/981.a45cb745cf424044c8c8.js +0 -1
  106. package/packages/web/992.645320b60c74c8787482.js +0 -1
  107. package/packages/web/996.ed9a963dc9e7439eca9a.js +0 -1
  108. package/packages/web/css/codemirror-theme_xq-light.css +0 -43
  109. package/packages/web/css/codemirror.css +0 -344
  110. package/packages/web/css/main.196f434e6a88cd448158.css +0 -7278
  111. package/packages/web/css/terminal-fix.css +0 -571
  112. package/packages/web/index.html +0 -3
  113. package/packages/web/main.dceff50c7307dda04873.js +0 -2
  114. package/packages/web/main.dceff50c7307dda04873.js.LICENSE.txt +0 -3
@@ -0,0 +1,911 @@
1
+ /**
2
+ * Thinking Tool - AI-Friendly Unified Thinking Tool
3
+ *
4
+ * Features:
5
+ * - Sequential Thinking: Step-by-step reasoning with branches and revisions
6
+ * - Think-Plan Mode: Structured planning with thought-plan-action entries
7
+ * - AI-Friendly Design: Smart parameter inference and contextual suggestions
8
+ * - Session Management: Isolated planning sessions
9
+ * - ToolM Interface: Full compliance with Prompt Manager tool ecosystem
10
+ */
11
+
12
+ import { randomUUID } from 'crypto';
13
+
14
+ /**
15
+ * Sequential Thinking State Management
16
+ */
17
+ class ThinkingState {
18
+ constructor() {
19
+ this.thoughts = [];
20
+ this.currentThoughtNumber = 0;
21
+ this.branches = new Map();
22
+ this.revisions = [];
23
+ }
24
+
25
+ /**
26
+ * Add a new thought to the sequence
27
+ */
28
+ addThought(thought, metadata = {}) {
29
+ const thoughtObj = {
30
+ number: this.currentThoughtNumber + 1,
31
+ content: thought,
32
+ timestamp: new Date().toISOString(),
33
+ ...metadata
34
+ };
35
+
36
+ this.thoughts.push(thoughtObj);
37
+ this.currentThoughtNumber = thoughtObj.number;
38
+
39
+ return thoughtObj;
40
+ }
41
+
42
+ /**
43
+ * Revise an existing thought
44
+ */
45
+ reviseThought(thoughtNumber, newThought) {
46
+ const originalThought = this.thoughts.find(t => t.number === thoughtNumber);
47
+ if (!originalThought) {
48
+ throw new Error(`思考 ${thoughtNumber} 不存在`);
49
+ }
50
+
51
+ const revision = {
52
+ originalNumber: thoughtNumber,
53
+ newContent: newThought,
54
+ timestamp: new Date().toISOString()
55
+ };
56
+
57
+ this.revisions.push(revision);
58
+ originalThought.revised = true;
59
+ originalThought.revision = revision;
60
+
61
+ return revision;
62
+ }
63
+
64
+ /**
65
+ * Create a thinking branch
66
+ */
67
+ createBranch(fromThoughtNumber, branchId, branchThought) {
68
+ const branch = {
69
+ fromThought: fromThoughtNumber,
70
+ branchId,
71
+ thoughts: [branchThought],
72
+ timestamp: new Date().toISOString()
73
+ };
74
+
75
+ this.branches.set(branchId, branch);
76
+ return branch;
77
+ }
78
+
79
+ /**
80
+ * Get all thoughts
81
+ */
82
+ getThoughts() {
83
+ return this.thoughts;
84
+ }
85
+
86
+ /**
87
+ * Get all branches
88
+ */
89
+ getBranches() {
90
+ return Array.from(this.branches.values());
91
+ }
92
+
93
+ /**
94
+ * Get all revisions
95
+ */
96
+ getRevisions() {
97
+ return this.revisions;
98
+ }
99
+
100
+ /**
101
+ * Get summary statistics
102
+ */
103
+ getSummary() {
104
+ return {
105
+ totalThoughts: this.thoughts.length,
106
+ totalBranches: this.branches.size,
107
+ totalRevisions: this.revisions.length,
108
+ currentThought: this.currentThoughtNumber
109
+ };
110
+ }
111
+
112
+ /**
113
+ * Reset all state
114
+ */
115
+ reset() {
116
+ this.thoughts = [];
117
+ this.currentThoughtNumber = 0;
118
+ this.branches.clear();
119
+ this.revisions = [];
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Think-Plan Entry Class
125
+ */
126
+ class ThinkPlanEntry {
127
+ constructor(thoughtNumber, thought, plan, action) {
128
+ this.thoughtNumber = thoughtNumber;
129
+ this.thought = thought;
130
+ this.plan = plan;
131
+ this.action = action;
132
+ this.timestamp = new Date();
133
+ }
134
+
135
+ toJSON() {
136
+ return {
137
+ thoughtNumber: this.thoughtNumber,
138
+ thought: this.thought,
139
+ plan: this.plan,
140
+ action: this.action,
141
+ timestamp: this.timestamp.toISOString()
142
+ };
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Think-Plan Memory Management
148
+ */
149
+ class ThinkPlanMemory {
150
+ constructor() {
151
+ this.entries = [];
152
+ this.mutex = new Map();
153
+ }
154
+
155
+ /**
156
+ * Add a new planning entry
157
+ */
158
+ addEntry(thoughtNumber, thought, plan, action, sessionId = 'default') {
159
+ const entry = new ThinkPlanEntry(thoughtNumber, thought, plan, action);
160
+ entry.sessionId = sessionId; // Add session ID to entry
161
+
162
+ if (!this.mutex.has(sessionId)) {
163
+ this.mutex.set(sessionId, []);
164
+ }
165
+
166
+ this.mutex.get(sessionId).push(entry);
167
+ this.entries.push(entry);
168
+
169
+ return entry;
170
+ }
171
+
172
+ /**
173
+ * Get all entries for a session
174
+ */
175
+ getAllEntries(sessionId = 'default') {
176
+ if (sessionId === 'default') {
177
+ return [...this.entries];
178
+ }
179
+ return [...(this.mutex.get(sessionId) || [])];
180
+ }
181
+
182
+ /**
183
+ * Get specific entry by thought number
184
+ */
185
+ getEntryByNumber(thoughtNumber, sessionId = 'default') {
186
+ const entries = this.getAllEntries(sessionId);
187
+ return entries.find(entry => entry.thoughtNumber === thoughtNumber) || null;
188
+ }
189
+
190
+ /**
191
+ * Get entries as JSON string
192
+ */
193
+ getEntriesAsJSON(sessionId = 'default') {
194
+ const entries = this.getAllEntries(sessionId);
195
+ return JSON.stringify(
196
+ entries.map(e => e.toJSON()),
197
+ null,
198
+ 2
199
+ );
200
+ }
201
+
202
+ /**
203
+ * Clear entries for a session
204
+ */
205
+ clearEntries(sessionId = 'default') {
206
+ if (sessionId === 'default') {
207
+ this.entries = [];
208
+ this.mutex.clear();
209
+ } else {
210
+ this.mutex.delete(sessionId);
211
+ this.entries = this.entries.filter(entry => entry.sessionId !== sessionId);
212
+ }
213
+ }
214
+
215
+ /**
216
+ * Generate summary text
217
+ */
218
+ getSummary(sessionId = 'default') {
219
+ const entries = this.getAllEntries(sessionId);
220
+
221
+ if (entries.length === 0) {
222
+ return '暂无思考和规划记录';
223
+ }
224
+
225
+ let summary = `思考和规划过程摘要 (共 ${entries.length} 个步骤):\n\n`;
226
+
227
+ entries.forEach((entry, index) => {
228
+ summary += `${index + 1}. [${entry.thoughtNumber}] ${entry.thought}\n`;
229
+ if (index < entries.length - 1) {
230
+ summary += '\n';
231
+ }
232
+ });
233
+
234
+ return summary;
235
+ }
236
+
237
+ /**
238
+ * Get entry count for session
239
+ */
240
+ getCount(sessionId = 'default') {
241
+ const entries = this.getAllEntries(sessionId);
242
+ return entries.length;
243
+ }
244
+ }
245
+
246
+ const thinkingState = new ThinkingState();
247
+ const thinkPlanMemory = new ThinkPlanMemory();
248
+ export default {
249
+ /**
250
+ * Get tool dependencies
251
+ */
252
+ getDependencies() {
253
+ return {
254
+ };
255
+ },
256
+
257
+ /**
258
+ * Get tool metadata
259
+ */
260
+ getMetadata() {
261
+ return {
262
+ id: 'thinking',
263
+ name: 'Thinking Tool',
264
+ description: 'AI友好的思考工具,支持顺序思考和思考规划模式。支持结构化推理、规划和迭代思维过程。',
265
+ version: '1.0.0',
266
+ category: 'thinking',
267
+ author: 'Prompt Manager',
268
+ tags: ['thinking', 'reasoning', 'planning', 'sequential', 'mcp', 'ai-friendly'],
269
+ scenarios: [
270
+ '复杂问题分析和分解',
271
+ '结构化项目规划和执行',
272
+ '决策分析与替代方案探索',
273
+ '迭代思维精炼和修订',
274
+ '基于会话的规划隔离'
275
+ ],
276
+ limitations: [
277
+ '顺序思考使用全局状态(无会话隔离)',
278
+ '思考规划模式支持会话隔离',
279
+ '最大思考步骤可通过环境配置',
280
+ '无外部数据源集成'
281
+ ]
282
+ };
283
+ },
284
+
285
+ /**
286
+ * Get parameter schema
287
+ */
288
+ getSchema() {
289
+ return {
290
+ parameters: {
291
+ type: 'object',
292
+ properties: {
293
+ method: {
294
+ type: 'string',
295
+ description: 'Operation method',
296
+ enum: [
297
+ 'add_thought',
298
+ 'revise_thought',
299
+ 'create_branch',
300
+ 'get_thoughts',
301
+ 'get_thought_summary',
302
+ 'reset_thoughts',
303
+ 'add_plan_entry',
304
+ 'get_plan_entries',
305
+ 'get_plan_entry',
306
+ 'clear_plan_entries',
307
+ 'get_plan_summary'
308
+ ]
309
+ },
310
+ thought: { type: 'string', description: '思考内容' },
311
+ thoughtNumber: { type: 'number', description: '思考步骤编号' },
312
+ totalThoughts: { type: 'number', description: '预期总步骤数' },
313
+ nextThoughtNeeded: { type: 'boolean', description: '是否继续思考' },
314
+ newThought: { type: 'string', description: '修订的思考内容' },
315
+ fromThoughtNumber: { type: 'number', description: '分支的源思考' },
316
+ branchId: { type: 'string', description: '分支标识符' },
317
+ branchThought: { type: 'string', description: '分支思考内容' },
318
+ plan: { type: 'string', description: '规划策略' },
319
+ action: { type: 'string', description: '下一步行动' },
320
+ sessionId: { type: 'string', description: '会话标识符' }
321
+ },
322
+ required: ['method']
323
+ },
324
+ environment: {
325
+ type: 'object',
326
+ properties: {
327
+ DEFAULT_SESSION_ID: {
328
+ type: 'string',
329
+ description: '思考规划操作的默认会话ID',
330
+ default: 'default'
331
+ },
332
+ MAX_THINKING_STEPS: {
333
+ type: 'number',
334
+ description: '允许的最大思考步骤数',
335
+ default: 100
336
+ }
337
+ },
338
+ required: []
339
+ }
340
+ };
341
+ },
342
+
343
+ /**
344
+ * Get business error definitions
345
+ */
346
+ getBusinessErrors() {
347
+ return [
348
+ {
349
+ code: 'THOUGHT_NOT_FOUND',
350
+ description: '思考编号不存在',
351
+ match: /思考.*不存在|thought.*not.*found/i,
352
+ solution: '请使用 get_thoughts 查看当前的思考编号',
353
+ retryable: true
354
+ },
355
+ {
356
+ code: 'THOUGHT_NUMBER_CONFLICT',
357
+ description: '思考编号冲突',
358
+ match: /思考编号.*冲突|number.*conflict/i,
359
+ solution: '请确保每个思考的编号唯一',
360
+ retryable: true
361
+ },
362
+ {
363
+ code: 'PLAN_ENTRY_NOT_FOUND',
364
+ description: '规划条目不存在',
365
+ match: /规划.*不存在|plan.*entry.*not.*found/i,
366
+ solution: '请使用 get_plan_entries 查看当前的规划记录',
367
+ retryable: true
368
+ },
369
+ {
370
+ code: 'SESSION_NOT_FOUND',
371
+ description: '会话不存在',
372
+ match: /会话.*不存在|session.*not.*found/i,
373
+ solution: '请提供正确的 sessionId,或使用默认会话',
374
+ retryable: true
375
+ },
376
+ {
377
+ code: 'INVALID_METHOD',
378
+ description: '不支持的方法',
379
+ match: /不支持.*方法|invalid.*method|unknown.*method/i,
380
+ solution: '请使用 mode: manual 查看所有可用方法',
381
+ retryable: false
382
+ },
383
+ {
384
+ code: 'MISSING_REQUIRED_PARAM',
385
+ description: '缺少必需参数',
386
+ match: /缺少必需参数|missing.*required/i,
387
+ solution: '请提供必需的参数,参考手册了解每个方法的要求',
388
+ retryable: false
389
+ },
390
+ {
391
+ code: 'MAX_STEPS_EXCEEDED',
392
+ description: '思考步骤超过限制',
393
+ match: /思考步骤.*超过.*限制|max.*steps.*exceeded/i,
394
+ solution: '请控制思考步骤数量,或使用不同的会话ID',
395
+ retryable: false
396
+ }
397
+ ];
398
+ },
399
+
400
+ /**
401
+ * Main execution method
402
+ */
403
+ async execute(params, mode = 'execute') {
404
+ const { api } = this;
405
+
406
+ api?.logger?.info('Thinking Tool execution started', {
407
+ mode,
408
+ method: params.method,
409
+ hasApi: !!api
410
+ });
411
+
412
+ try {
413
+ if (mode === 'manual') {
414
+ return {
415
+ content: [
416
+ {
417
+ type: 'text',
418
+ text: this.generateManual()
419
+ }
420
+ ]
421
+ };
422
+ }
423
+
424
+ if (mode === 'configure') {
425
+ return this.handleConfigure(params, api);
426
+ }
427
+
428
+ this.validateParams(params);
429
+
430
+ let result;
431
+ switch (params.method) {
432
+ case 'add_thought':
433
+ result = this.handleAddThought(params);
434
+ break;
435
+ case 'revise_thought':
436
+ result = this.handleReviseThought(params);
437
+ break;
438
+ case 'create_branch':
439
+ result = this.handleCreateBranch(params);
440
+ break;
441
+ case 'get_thoughts':
442
+ result = this.handleGetThoughts(params);
443
+ break;
444
+ case 'get_thought_summary':
445
+ result = this.handleGetThoughtSummary(params);
446
+ break;
447
+ case 'reset_thoughts':
448
+ result = this.handleResetThoughts(params);
449
+ break;
450
+
451
+ case 'add_plan_entry':
452
+ result = this.handleAddPlanEntry(params);
453
+ break;
454
+ case 'get_plan_entries':
455
+ result = this.handleGetPlanEntries(params);
456
+ break;
457
+ case 'get_plan_entry':
458
+ result = this.handleGetPlanEntry(params);
459
+ break;
460
+ case 'clear_plan_entries':
461
+ result = this.handleClearPlanEntries(params);
462
+ break;
463
+ case 'get_plan_summary':
464
+ result = this.handleGetPlanSummary(params);
465
+ break;
466
+
467
+ default:
468
+ throw new Error(`不支持的方法: ${params.method}`);
469
+ }
470
+
471
+ return {
472
+ content: [
473
+ {
474
+ type: 'text',
475
+ text: result
476
+ }
477
+ ]
478
+ };
479
+ } catch (error) {
480
+ api?.logger?.error('Thinking Tool execution failed', {
481
+ mode,
482
+ method: params.method,
483
+ error: error.message
484
+ });
485
+ throw error;
486
+ }
487
+ },
488
+
489
+ /**
490
+ * Parameter validation
491
+ */
492
+ validateParams(params) {
493
+ const { method } = params;
494
+ if (!method) {
495
+ throw new Error('缺少必需参数: method');
496
+ }
497
+
498
+ const methodRequirements = {
499
+ 'add_thought': ['thought'],
500
+ 'revise_thought': ['thoughtNumber', 'newThought'],
501
+ 'create_branch': ['fromThoughtNumber', 'branchId', 'branchThought'],
502
+ 'add_plan_entry': ['thought', 'plan', 'action', 'thoughtNumber'],
503
+ // Other methods have no required params beyond method
504
+ };
505
+
506
+ const required = methodRequirements[method];
507
+ if (!required) return;
508
+
509
+ const missing = required.filter(field => !params[field] || params[field] === '');
510
+ if (missing.length > 0) {
511
+ throw new Error(`方法 ${method} 缺少必需参数: ${missing.join(', ')}`);
512
+ }
513
+ },
514
+
515
+ /**
516
+ * Handle add thought
517
+ */
518
+ handleAddThought(params) {
519
+ const addedThought = thinkingState.addThought(params.thought, {
520
+ thoughtNumber: params.thoughtNumber,
521
+ totalThoughts: params.totalThoughts || 10,
522
+ nextThoughtNeeded: params.nextThoughtNeeded !== false
523
+ });
524
+
525
+ const summary = thinkingState.getSummary();
526
+ const progress = {
527
+ current: addedThought.number,
528
+ total: params.totalThoughts || 10,
529
+ percentage: Math.round((addedThought.number / (params.totalThoughts || 10)) * 100)
530
+ };
531
+
532
+ const data = {
533
+ thought: {
534
+ number: addedThought.number,
535
+ content: params.thought,
536
+ type: 'regular'
537
+ },
538
+ summary,
539
+ progress,
540
+ nextThoughtNeeded: params.nextThoughtNeeded !== false,
541
+ thoughts: thinkingState.getThoughts().map(t => ({
542
+ number: t.number,
543
+ content: t.content,
544
+ revised: t.revised || false
545
+ })),
546
+ ...(thinkingState.getBranches().length > 0 && { branches: thinkingState.getBranches() }),
547
+ ...(thinkingState.getRevisions().length > 0 && { revisions: thinkingState.getRevisions() })
548
+ };
549
+
550
+ return this.formatSequentialThinkingOutput(data);
551
+ },
552
+
553
+ handleReviseThought(params) {
554
+ thinkingState.reviseThought(params.thoughtNumber, params.newThought);
555
+ const data = {
556
+ thought: {
557
+ number: params.thoughtNumber,
558
+ content: params.newThought,
559
+ type: 'revision',
560
+ revisesThought: params.thoughtNumber
561
+ },
562
+ revisions: thinkingState.getRevisions(),
563
+ thoughts: thinkingState.getThoughts().map(t => ({
564
+ number: t.number,
565
+ content: t.content,
566
+ revised: t.revised || false
567
+ }))
568
+ };
569
+ return this.formatSequentialThinkingOutput(data);
570
+ },
571
+
572
+ handleCreateBranch(params) {
573
+ const branch = thinkingState.createBranch(
574
+ params.fromThoughtNumber,
575
+ params.branchId,
576
+ params.branchThought
577
+ );
578
+ const data = {
579
+ thought: {
580
+ number: params.fromThoughtNumber,
581
+ type: 'branch',
582
+ branchFromThought: params.fromThoughtNumber,
583
+ branchId: params.branchId,
584
+ content: params.branchThought
585
+ },
586
+ branches: thinkingState.getBranches(),
587
+ thoughts: thinkingState.getThoughts().map(t => ({
588
+ number: t.number,
589
+ content: t.content,
590
+ revised: t.revised || false
591
+ }))
592
+ };
593
+ return this.formatSequentialThinkingOutput(data);
594
+ },
595
+
596
+ handleGetThoughts(params) {
597
+ const data = {
598
+ thoughts: thinkingState.getThoughts(),
599
+ branches: thinkingState.getBranches(),
600
+ revisions: thinkingState.getRevisions(),
601
+ summary: thinkingState.getSummary()
602
+ };
603
+ return this.formatSequentialThinkingOutput(data);
604
+ },
605
+
606
+ handleGetThoughtSummary(params) {
607
+ const summary = thinkingState.getSummary();
608
+ return `思考状态摘要:
609
+ - 总思考数: ${summary.totalThoughts}
610
+ - 分支数: ${summary.totalBranches}
611
+ - 修订数: ${summary.totalRevisions}
612
+ - 当前思考编号: ${summary.currentThought}`;
613
+ },
614
+
615
+ handleResetThoughts(params) {
616
+ thinkingState.reset();
617
+ return `✅ 思考状态已重置
618
+ 所有思考记录、分支和修订历史已被清除。`;
619
+ },
620
+ handleAddPlanEntry(params) {
621
+ const sessionId = params.sessionId || 'default';
622
+ const entry = thinkPlanMemory.addEntry(
623
+ params.thoughtNumber,
624
+ params.thought,
625
+ params.plan,
626
+ params.action,
627
+ sessionId
628
+ );
629
+ const currentCount = thinkPlanMemory.getCount(sessionId);
630
+
631
+ return this.formatThinkPlanOutput(entry, currentCount);
632
+ },
633
+
634
+ handleGetPlanEntries(params) {
635
+ const sessionId = params.sessionId || 'default';
636
+ const entries = thinkPlanMemory.getAllEntries(sessionId);
637
+ let result = `📋 规划记录列表 (会话: ${sessionId})\n`;
638
+ result += `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n`;
639
+ result += `📊 共 ${entries.length} 个规划条目\n\n`;
640
+
641
+ entries.forEach((entry, index) => {
642
+ result += `${index + 1}. [${entry.thoughtNumber}] ${entry.thought}\n`;
643
+ result += ` 📋 ${entry.plan}\n`;
644
+ result += ` 🎯 ${entry.action}\n`;
645
+ result += ` ⏰ ${entry.timestamp.toLocaleString('zh-CN')}\n\n`;
646
+ });
647
+
648
+ result += `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n`;
649
+ return result;
650
+ },
651
+
652
+ handleGetPlanEntry(params) {
653
+ const sessionId = params.sessionId || 'default';
654
+ const entry = thinkPlanMemory.getEntryByNumber(params.thoughtNumber, sessionId);
655
+ if (!entry) {
656
+ throw new Error(`规划条目 ${params.thoughtNumber} 不存在`);
657
+ }
658
+ return this.formatThinkPlanOutput(entry, thinkPlanMemory.getCount(sessionId));
659
+ },
660
+
661
+ handleClearPlanEntries(params) {
662
+ const sessionId = params.sessionId || 'default';
663
+ const beforeCount = thinkPlanMemory.getCount(sessionId);
664
+ thinkPlanMemory.clearEntries(sessionId);
665
+ return `✅ 规划记录已清空\n会话: ${sessionId}\n清空条目数: ${beforeCount}`;
666
+ },
667
+
668
+ handleGetPlanSummary(params) {
669
+ const sessionId = params.sessionId || 'default';
670
+ const summary = thinkPlanMemory.getSummary(sessionId);
671
+ const count = thinkPlanMemory.getCount(sessionId);
672
+ return `📊 规划摘要 (会话: ${sessionId})\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n${summary}\n\n📈 统计信息:\n- 规划条目总数: ${count}\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`;
673
+ },
674
+
675
+ formatSequentialThinkingOutput(data) {
676
+ let output = '';
677
+ output += '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n';
678
+ output += '🧠 **顺序思考工具**\n';
679
+ output += '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n';
680
+
681
+ if (data.thought) {
682
+ output += `**思考 ${data.thought.number}**\n`;
683
+ output += `${data.thought.content}\n\n`;
684
+ }
685
+
686
+ if (data.progress) {
687
+ output += `**进度**: ${data.progress.current}/${data.progress.total} (${data.progress.percentage}%)\n\n`;
688
+ }
689
+
690
+ if (data.thoughts && data.thoughts.length > 0) {
691
+ output += '**思考历史**:\n';
692
+ data.thoughts.forEach(t => {
693
+ const marker = t.revised ? '↩️' : '•';
694
+ const content = t.content.length > 100 ? t.content.substring(0, 100) + '...' : t.content;
695
+ output += `${marker} [${t.number}] ${content}\n`;
696
+ });
697
+ output += '\n';
698
+ }
699
+
700
+ if (data.branches && data.branches.length > 0) {
701
+ output += '**分支**:\n';
702
+ data.branches.forEach(branch => {
703
+ output += ` └─ 分支 ${branch.branchId} (来自思考 ${branch.fromThought})\n`;
704
+ });
705
+ output += '\n';
706
+ }
707
+
708
+ if (data.revisions && data.revisions.length > 0) {
709
+ output += '**修订**:\n';
710
+ data.revisions.forEach(rev => {
711
+ output += ` ↻ 思考 ${rev.originalNumber} 已修订\n`;
712
+ });
713
+ output += '\n';
714
+ }
715
+
716
+ if (data.nextThoughtNeeded === false) {
717
+ output += '**状态**: 思考完成\n\n';
718
+ } else {
719
+ output += '**下一步**: 继续思考...\n\n';
720
+ }
721
+
722
+ if (data.finalSummary) {
723
+ output += '**最终总结**:\n';
724
+ output += `- 总步骤数: ${data.finalSummary.totalSteps}\n`;
725
+ if (data.finalSummary.keyInsights && data.finalSummary.keyInsights.length > 0) {
726
+ output += '- 关键洞察:\n';
727
+ data.finalSummary.keyInsights.forEach(insight => {
728
+ output += ` • [步骤 ${insight.step}] ${insight.content}\n`;
729
+ });
730
+ }
731
+ }
732
+
733
+ output += '\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n';
734
+ return output;
735
+ },
736
+
737
+ formatThinkPlanOutput(entry, currentCount) {
738
+ const timestamp = entry.timestamp.toLocaleString('zh-CN', {
739
+ year: 'numeric',
740
+ month: '2-digit',
741
+ day: '2-digit',
742
+ hour: '2-digit',
743
+ minute: '2-digit',
744
+ second: '2-digit'
745
+ });
746
+
747
+ let output = '';
748
+ output += '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n';
749
+ output += '🧠 **思考和规划记录**\n';
750
+ output += '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n';
751
+
752
+ output += `**思考编号**: [${entry.thoughtNumber}]\n\n`;
753
+
754
+ output += '🤔 **思考内容**:\n';
755
+ output += `${entry.thought}\n\n`;
756
+
757
+ output += '📋 **规划方案**:\n';
758
+ output += `${entry.plan}\n\n`;
759
+
760
+ output += '🎯 **下一步行动**:\n';
761
+ output += `${entry.action}\n\n`;
762
+
763
+ output += `⏰ **记录时间**: ${timestamp}\n\n`;
764
+ output += `📊 **当前已记录**: ${currentCount} 个思考步骤\n`;
765
+
766
+ output += '\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n';
767
+ return output;
768
+ },
769
+
770
+ generateManual2() {
771
+ let manual = `# 🧠 Thinking Tool - 统一思考工具
772
+
773
+ ## 🎯 60秒快速开始
774
+
775
+ ### 最常用的3个方法
776
+
777
+ #### 1️⃣ add_thought - 添加思考
778
+ **何时使用**: 开始思考或添加新的思考步骤
779
+ **必填参数**: thought (思考内容)
780
+ **最简示例**:
781
+ \`\`\`yaml
782
+ tool: tool://thinking
783
+ mode: execute
784
+ parameters:
785
+ method: add_thought
786
+ thought: "分析问题的原因"
787
+ \`\`\`
788
+
789
+ #### 2️⃣ add_plan_entry - 添加规划
790
+ **何时使用**: 需要结构化思考和执行计划
791
+ **必填参数**: thought, plan, action
792
+ **最简示例**:
793
+ \`\`\`yaml
794
+ tool: tool://thinking
795
+ mode: execute
796
+ parameters:
797
+ method: add_plan_entry
798
+ thought: "需要部署新版本"
799
+ plan: "1. 备份数据 2. 部署代码 3. 验证"
800
+ action: "执行备份脚本"
801
+ \`\`\`
802
+
803
+ #### 3️⃣ get_thoughts - 查看思考
804
+ **何时使用**: 查看所有已记录的思考
805
+ **参数**: 无
806
+ **最简示例**:
807
+ \`\`\`yaml
808
+ tool: tool://thinking
809
+ mode: execute
810
+ parameters:
811
+ method: get_thoughts
812
+ \`\`\`
813
+
814
+ ---
815
+
816
+ ## 📝 方法完整列表
817
+
818
+ ### Sequential Thinking 方法群
819
+ - add_thought - 添加思考步骤
820
+ - revise_thought - 修订思考
821
+ - create_branch - 创建分支
822
+ - get_thoughts - 获取所有思考
823
+ - get_thought_summary - 获取摘要
824
+ - reset_thoughts - 重置思考状态
825
+
826
+ ### Think Plan 方法群
827
+ - add_plan_entry - 添加规划条目
828
+ - get_plan_entries - 获取规划记录
829
+ - get_plan_entry - 获取特定条目
830
+ - clear_plan_entries - 清空规划
831
+ - get_plan_summary - 获取规划摘要
832
+
833
+ ---
834
+
835
+ ## 🎯 使用场景
836
+
837
+ ### Sequential Thinking
838
+ 适合复杂问题分析、方案探索、决策制定
839
+
840
+ ### Think Plan
841
+ 适合项目规划、任务执行、流程设计
842
+
843
+ ---
844
+
845
+ ## 🔧 环境配置
846
+
847
+ 支持环境变量配置:
848
+ - DEFAULT_SESSION_ID (默认: 'default')
849
+ - MAX_THINKING_STEPS (默认: 100)
850
+
851
+ 配置方法:
852
+ \`\`\`yaml
853
+ tool: tool://thinking
854
+ mode: configure
855
+ parameters:
856
+ DEFAULT_SESSION_ID: "my-project"
857
+ \`\`\`
858
+
859
+ ---
860
+
861
+ ## 📚 完整文档
862
+
863
+ 使用 mode: manual 查看完整文档和所有参数说明。`;
864
+
865
+ return {
866
+ content: [
867
+ {
868
+ type: 'text',
869
+ text: manual
870
+ }
871
+ ]
872
+ };
873
+ },
874
+
875
+ /**
876
+ * Handle configuration
877
+ */
878
+ handleConfigure(params, api) {
879
+ const configured = [];
880
+
881
+ if (params.DEFAULT_SESSION_ID !== undefined) {
882
+ api?.environment?.set('DEFAULT_SESSION_ID', params.DEFAULT_SESSION_ID);
883
+ configured.push(`DEFAULT_SESSION_ID: "${params.DEFAULT_SESSION_ID}"`);
884
+ }
885
+
886
+ if (params.MAX_THINKING_STEPS !== undefined) {
887
+ api?.environment?.set('MAX_THINKING_STEPS', params.MAX_THINKING_STEPS.toString());
888
+ configured.push(`MAX_THINKING_STEPS: ${params.MAX_THINKING_STEPS}`);
889
+ }
890
+
891
+ if (configured.length === 0) {
892
+ return {
893
+ content: [
894
+ {
895
+ type: 'text',
896
+ text: '⚠️ 没有配置任何环境变量\n\n可配置的环境变量:\n- DEFAULT_SESSION_ID (默认: "default")\n- MAX_THINKING_STEPS (默认: 100)'
897
+ }
898
+ ]
899
+ };
900
+ }
901
+
902
+ return {
903
+ content: [
904
+ {
905
+ type: 'text',
906
+ text: `✅ 环境变量配置完成\n\n已配置:\n${configured.map(c => `• ${c}`).join('\n')}\n\n配置将在下次重启后生效。`
907
+ }
908
+ ]
909
+ };
910
+ },
911
+ };