todo.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { z } from "zod"
  2. import { Tool } from "./tool"
  3. import DESCRIPTION_WRITE from "./todowrite.txt"
  4. import { Instance } from "../project/instance"
  5. const TodoInfo = z.object({
  6. content: z.string().describe("Brief description of the task"),
  7. status: z.string().describe("Current status of the task: pending, in_progress, completed, cancelled"),
  8. priority: z.string().describe("Priority level of the task: high, medium, low"),
  9. id: z.string().describe("Unique identifier for the todo item"),
  10. })
  11. type TodoInfo = z.infer<typeof TodoInfo>
  12. const state = Instance.state(
  13. () => {
  14. const todos: {
  15. [sessionId: string]: TodoInfo[]
  16. } = {}
  17. return todos
  18. },
  19. )
  20. export const TodoWriteTool = Tool.define("todowrite", {
  21. description: DESCRIPTION_WRITE,
  22. parameters: z.object({
  23. todos: z.array(TodoInfo).describe("The updated todo list"),
  24. }),
  25. async execute(params, opts) {
  26. const todos = state()
  27. todos[opts.sessionID] = params.todos
  28. return {
  29. title: `${params.todos.filter((x) => x.status !== "completed").length} todos`,
  30. output: JSON.stringify(params.todos, null, 2),
  31. metadata: {
  32. todos: params.todos,
  33. },
  34. }
  35. },
  36. })
  37. export const TodoReadTool = Tool.define("todoread", {
  38. description: "Use this tool to read your todo list",
  39. parameters: z.object({}),
  40. async execute(_params, opts) {
  41. const todos = state()[opts.sessionID] ?? []
  42. return {
  43. title: `${todos.filter((x) => x.status !== "completed").length} todos`,
  44. metadata: {
  45. todos,
  46. },
  47. output: JSON.stringify(todos, null, 2),
  48. }
  49. },
  50. })