todo.ts 1.5 KB

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