write-memory.ts 1.2 KB

123456789101112131415161718192021222324252627282930
  1. import type { Tool } from "../types"
  2. import { readFile, writeFile } from "@/commands/fs"
  3. export function createWriteMemoryTool(memoryDir: string): Tool {
  4. return {
  5. name: "write_memory",
  6. description: "写入或更新记忆条目。参数 name 为记忆名称,content 为记忆内容。",
  7. category: "write",
  8. permission: "confirm",
  9. parameters: {
  10. name: { type: "string", description: "记忆条目名称", required: true },
  11. content: { type: "string", description: "记忆内容", required: true },
  12. },
  13. execute: async (params) => {
  14. const name = params.name as string
  15. const content = params.content as string
  16. const path = `${memoryDir}/${name}.md`
  17. try {
  18. await writeFile(path, content)
  19. const verified = await readFile(path)
  20. if (verified !== content) {
  21. return `已写入记忆「${name}」,警告:写入后读回验证失败,请手动检查文件内容。`
  22. }
  23. return `已写入记忆「${name}」,读回验证通过。`
  24. } catch (err) {
  25. return `错误:写入记忆失败 — ${err instanceof Error ? err.message : String(err)}`
  26. }
  27. },
  28. }
  29. }