2025-04-15 20:19:42 +08:00
|
|
|
import { z } from "zod";
|
2025-04-26 20:30:23 +08:00
|
|
|
import {
|
|
|
|
getProcessThoughtPrompt,
|
|
|
|
ProcessThoughtPromptParams,
|
|
|
|
} from "../prompts/generators/processThought.js";
|
2025-04-15 20:19:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* processThought工具的參數結構
|
|
|
|
*/
|
|
|
|
export const processThoughtSchema = z.object({
|
2025-04-15 21:03:56 +08:00
|
|
|
thought: z
|
|
|
|
.string()
|
|
|
|
.min(1, {
|
|
|
|
message: "思維內容不能為空,請提供有效的思考內容",
|
|
|
|
})
|
|
|
|
.describe("思維內容"),
|
|
|
|
thought_number: z
|
|
|
|
.number()
|
|
|
|
.int()
|
|
|
|
.positive({
|
|
|
|
message: "思維編號必須是正整數",
|
|
|
|
})
|
|
|
|
.describe("當前思維編號"),
|
|
|
|
total_thoughts: z
|
|
|
|
.number()
|
|
|
|
.int()
|
|
|
|
.positive({
|
|
|
|
message: "總思維數必須是正整數",
|
|
|
|
})
|
2025-04-19 03:02:44 +08:00
|
|
|
.describe("預計總思維數量,如果需要更多的思考可以隨時變更"),
|
2025-04-15 20:19:42 +08:00
|
|
|
next_thought_needed: z.boolean().describe("是否需要下一步思維"),
|
2025-04-15 21:03:56 +08:00
|
|
|
stage: z
|
2025-04-16 16:39:01 +08:00
|
|
|
.string()
|
|
|
|
.min(1, {
|
2025-04-15 21:03:56 +08:00
|
|
|
message: "思維階段不能為空,請提供有效的思考階段",
|
|
|
|
})
|
2025-04-16 16:39:01 +08:00
|
|
|
.describe(
|
|
|
|
"思考階段,可以選擇的階段有:問題定義、收集資訊、研究、分析、綜合、結論、質疑、規劃"
|
|
|
|
),
|
2025-04-15 21:03:56 +08:00
|
|
|
tags: z.array(z.string()).optional().describe("思維標籤,是一個陣列字串"),
|
|
|
|
axioms_used: z
|
|
|
|
.array(z.string())
|
|
|
|
.optional()
|
|
|
|
.describe("使用的公理,是一個陣列字串"),
|
|
|
|
assumptions_challenged: z
|
|
|
|
.array(z.string())
|
|
|
|
.optional()
|
|
|
|
.describe("挑戰的假設,是一個陣列字串"),
|
2025-04-15 20:19:42 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 處理單一思維並返回格式化輸出
|
|
|
|
*/
|
|
|
|
export async function processThought(
|
|
|
|
params: z.infer<typeof processThoughtSchema>
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
// 將參數轉換為規範的ThoughtData格式
|
2025-04-26 20:30:23 +08:00
|
|
|
const thoughtData: ProcessThoughtPromptParams = {
|
2025-04-15 20:19:42 +08:00
|
|
|
thought: params.thought,
|
|
|
|
thoughtNumber: params.thought_number,
|
|
|
|
totalThoughts: params.total_thoughts,
|
|
|
|
nextThoughtNeeded: params.next_thought_needed,
|
|
|
|
stage: params.stage,
|
2025-04-26 20:30:23 +08:00
|
|
|
tags: params.tags || [],
|
|
|
|
axioms_used: params.axioms_used || [],
|
|
|
|
assumptions_challenged: params.assumptions_challenged || [],
|
2025-04-15 20:19:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// 確保思維編號不超過總思維數
|
|
|
|
if (thoughtData.thoughtNumber > thoughtData.totalThoughts) {
|
|
|
|
// 自動調整總思維數量
|
|
|
|
thoughtData.totalThoughts = thoughtData.thoughtNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 格式化思維輸出
|
2025-04-26 20:30:23 +08:00
|
|
|
const formattedThought = getProcessThoughtPrompt(thoughtData);
|
2025-04-15 20:19:42 +08:00
|
|
|
|
|
|
|
// 返回成功響應
|
|
|
|
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}`,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|