diff --git a/src/index.ts b/src/index.ts index efa6a5e..b9e82bc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,6 +38,12 @@ import { getTaskDetailSchema, } from "./tools/taskTools.js"; +// 導入思維鏈工具 +import { + processThought, + processThoughtSchema, +} from "./tools/thoughtChainTools.js"; + async function main() { try { console.log("啟動蝦米任務管理器服務..."); @@ -180,6 +186,16 @@ async function main() { } ); + // 註冊思維鏈工具 + server.tool( + "process_thought", + "你可以透過靈活的、可適應和發展的思考過程來分析問題,隨著理解的加深,每個想法都可以建立、質疑或修改先前的見解。你可以質疑想法、假設想法、驗證想法,並且可以建立新的想法。你將重複這個過程,直到你對問題有足夠的理解,並且能夠提出有效的解決方案。", + processThoughtSchema.shape, + async (args) => { + return await processThought(args); + } + ); + // 建立連接 const transport = new StdioServerTransport(); await server.connect(transport); diff --git a/src/prompts/templates/planTask.ts b/src/prompts/templates/planTask.ts index dbcb9d6..a47948a 100644 --- a/src/prompts/templates/planTask.ts +++ b/src/prompts/templates/planTask.ts @@ -90,6 +90,6 @@ export const infoCollectionGuideItems3 = `3. **網路搜索** - 當出現你不 export const nextStepsTemplate = `## 下一步\n\n`; export const nextStepsContent1 = `**第一步:根據任務描述決定是否查詢記憶**\n`; export const nextStepsContent1Detail = `- 判斷任務是否屬於必查情境,若是,請先使用「query_task」查詢過往記錄;否則,可直接進行分析。\n\n`; -export const nextStepsContent2 = `**第二步:使用 analyze_task 提交分析結果**\n`; -export const nextStepsContent2Detail1 = `1. **任務摘要** - 目標、範圍、挑戰和限制條件\n`; -export const nextStepsContent2Detail2 = `2. **初步解答構想** - 可行的技術方案和實施計劃\n`; +export const nextStepsContent2 = `**第二步:使用「process_thought」思考答案:\n`; +export const nextStepsContent2Detail1 = `1. **思考過程** - 逐步推理過程,包括假設、驗證和調整\n`; +export const nextStepsContent2Detail2 = `2. 如果不需要思考更多,請使用「analyze_task」提交分析結果\n`; diff --git a/src/tools/thoughtChainTools.ts b/src/tools/thoughtChainTools.ts new file mode 100644 index 0000000..a6013c5 --- /dev/null +++ b/src/tools/thoughtChainTools.ts @@ -0,0 +1,153 @@ +import { z } from "zod"; +import { ThoughtData, ThoughtStage } from "../types/index.js"; + +/** + * 思維資料驗證結構 + */ +const thoughtDataSchema = z.object({ + thought: z + .string() + .min(1, { + message: "思維內容不能為空,請提供有效的思考內容", + }) + .describe("思維內容"), + thoughtNumber: z + .number() + .int() + .positive({ + message: "思維編號必須是正整數", + }) + .describe("思維編號"), + totalThoughts: z + .number() + .int() + .positive({ + message: "總思維數必須是正整數", + }) + .describe("總思維數"), + nextThoughtNeeded: z.boolean().describe("是否需要更多思維"), + stage: z + .nativeEnum(ThoughtStage, { + message: "思維階段不能為空,請提供有效的思考階段", + }) + .describe("思維階段"), + tags: z.array(z.string()).optional().describe("思維標籤"), + axioms_used: z.array(z.string()).optional().describe("使用的公理"), + assumptions_challenged: z.array(z.string()).optional().describe("挑戰的假設"), +}); + +/** + * 格式化思維內容,返回美觀的格式化輸出 + * @param thoughtData 思維資料 + * @returns 格式化後的思維輸出 + */ +function formatThought(thoughtData: ThoughtData): string { + // 創建基本思維標題,包含編號、總數和階段 + const thoughtHeader = `## 思維 ${thoughtData.thoughtNumber}/${thoughtData.totalThoughts} - ${thoughtData.stage}`; + + // 格式化思維正文 + const thoughtContent = thoughtData.thought; + + // 準備可選元素的陣列 + const optionalElements: string[] = []; + + // 如果有標籤,則添加到可選元素 + if (thoughtData.tags && thoughtData.tags.length > 0) { + optionalElements.push(`**標籤:** ${thoughtData.tags.join(", ")}`); + } + + // 如果有使用的公理,則添加到可選元素 + if (thoughtData.axioms_used && thoughtData.axioms_used.length > 0) { + optionalElements.push( + `**使用的原則:** ${thoughtData.axioms_used.join(", ")}` + ); + } + + // 如果有挑戰的假設,則添加到可選元素 + if ( + thoughtData.assumptions_challenged && + thoughtData.assumptions_challenged.length > 0 + ) { + optionalElements.push( + `**挑戰的假設:** ${thoughtData.assumptions_challenged.join(", ")}` + ); + } + + // 添加下一步指示 + const nextStepIndication = thoughtData.nextThoughtNeeded + ? "需要更多思考,繼續使用 「process_thought」 工具思考找尋答案" + : "思考完成。下一步使用「analyze_task」 提交分析結果\n1. **任務摘要** - 目標、範圍、挑戰和限制條件\n2. **初步解答構想** - 可行的技術方案和實施計劃"; + + // 組合所有元素 + return [ + thoughtHeader, + thoughtContent, + ...optionalElements, + `\n*${nextStepIndication}*`, + ].join("\n\n"); +} + +/** + * processThought工具的參數結構 + */ +export const processThoughtSchema = z.object({ + thought: z.string().describe("思維內容"), + thought_number: z.number().int().positive().describe("當前思維編號"), + total_thoughts: z.number().int().positive().describe("預計總思維數量"), + next_thought_needed: z.boolean().describe("是否需要下一步思維"), + stage: z.string().describe("思考階段"), + tags: z.array(z.string()).optional().describe("思維標籤"), + axioms_used: z.array(z.string()).optional().describe("使用的公理"), + assumptions_challenged: z.array(z.string()).optional().describe("挑戰的假設"), +}); + +/** + * 處理單一思維並返回格式化輸出 + */ +export async function processThought( + params: z.infer +) { + try { + // 將參數轉換為規範的ThoughtData格式 + const thoughtData: ThoughtData = { + thought: params.thought, + thoughtNumber: params.thought_number, + totalThoughts: params.total_thoughts, + nextThoughtNeeded: params.next_thought_needed, + stage: params.stage, + tags: params.tags, + axioms_used: params.axioms_used, + assumptions_challenged: params.assumptions_challenged, + }; + + // 確保思維編號不超過總思維數 + if (thoughtData.thoughtNumber > thoughtData.totalThoughts) { + // 自動調整總思維數量 + thoughtData.totalThoughts = thoughtData.thoughtNumber; + } + + // 格式化思維輸出 + const formattedThought = formatThought(thoughtData); + + // 返回成功響應 + return { + content: [ + { + type: "text" as const, + text: formattedThought, + }, + ], + }; + } catch (error) { + // 捕獲並處理所有未預期的錯誤 + const errorMessage = error instanceof Error ? error.message : "未知錯誤"; + return { + content: [ + { + type: "text" as const, + text: `處理思維時發生錯誤: ${errorMessage}`, + }, + ], + }; + } +} diff --git a/src/types/index.ts b/src/types/index.ts index cd90692..74d3bb3 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -158,3 +158,6 @@ export interface TaskComplexityAssessment { }; recommendations: string[]; // 處理建議列表 } + +// 思維鏈資料結構 +export * from "./thoughtChain.js"; diff --git a/src/types/thoughtChain.ts b/src/types/thoughtChain.ts new file mode 100644 index 0000000..bc607ed --- /dev/null +++ b/src/types/thoughtChain.ts @@ -0,0 +1,32 @@ +/** + * 思維鏈資料結構定義 + * + * 此文件定義了思維鏈工具所需的核心資料結構,僅包含處理單一思維所需的介面, + * 不包含儲存歷史記錄的功能。設計符合現有專案架構風格。 + */ + +/** + * 思維階段枚舉:定義思考過程中的不同階段 + */ +export enum ThoughtStage { + PROBLEM_DEFINITION = "問題定義", // 定義問題和目標的階段 + RESEARCH = "研究", // 收集和分析資訊的階段 + ANALYSIS = "分析", // 深入解析問題和可能解決方案的階段 + SYNTHESIS = "綜合", // 整合分析結果形成方案的階段 + CONCLUSION = "結論", // 總結思考過程並提出最終解決方案的階段 + QUESTIONING = "質疑", // 質疑和批判的階段 +} + +/** + * 思維資料介面:定義思維的完整資料結構 + */ +export interface ThoughtData { + thought: string; // 思維內容(字串) + thoughtNumber: number; // 當前思維編號(數字) + totalThoughts: number; // 預估總思維數量(數字) + nextThoughtNeeded: boolean; // 是否需要更多思維(布林值) + stage: string; // 思維階段(字串,如「問題定義」、「研究」、「分析」、「綜合」、「結論」、「質疑」) + tags?: string[]; // 可選的思維關鍵詞或分類(字串陣列) + axioms_used?: string[]; // 可選的此思維中使用的原則或公理(字串陣列) + assumptions_challenged?: string[]; // 可選的此思維挑戰的假設(字串陣列) +}