todo.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { Effect, Schema } from "effect"
  2. import * as Tool from "./tool"
  3. import DESCRIPTION_WRITE from "./todowrite.txt"
  4. import { Todo } from "../session/todo"
  5. // Todo.Info is still a zod schema (session/todo.ts). Inline the field shape
  6. // here rather than referencing its `.shape` — the LLM-visible JSON Schema is
  7. // identical, and it removes the last zod dependency from this tool.
  8. const TodoItem = Schema.Struct({
  9. content: Schema.String.annotate({ description: "Brief description of the task" }),
  10. status: Schema.String.annotate({
  11. description: "Current status of the task: pending, in_progress, completed, cancelled",
  12. }),
  13. priority: Schema.String.annotate({ description: "Priority level of the task: high, medium, low" }),
  14. })
  15. export const Parameters = Schema.Struct({
  16. todos: Schema.mutable(Schema.Array(TodoItem)).annotate({ description: "The updated todo list" }),
  17. })
  18. type Metadata = {
  19. todos: Todo.Info[]
  20. }
  21. export const TodoWriteTool = Tool.define<typeof Parameters, Metadata, Todo.Service>(
  22. "todowrite",
  23. Effect.gen(function* () {
  24. const todo = yield* Todo.Service
  25. return {
  26. description: DESCRIPTION_WRITE,
  27. parameters: Parameters,
  28. execute: (params: Schema.Schema.Type<typeof Parameters>, ctx: Tool.Context<Metadata>) =>
  29. Effect.gen(function* () {
  30. yield* ctx.ask({
  31. permission: "todowrite",
  32. patterns: ["*"],
  33. always: ["*"],
  34. metadata: {},
  35. })
  36. yield* todo.update({
  37. sessionID: ctx.sessionID,
  38. todos: params.todos,
  39. })
  40. return {
  41. title: `${params.todos.filter((x) => x.status !== "completed").length} todos`,
  42. output: JSON.stringify(params.todos, null, 2),
  43. metadata: {
  44. todos: params.todos,
  45. },
  46. }
  47. }),
  48. } satisfies Tool.DefWithoutID<typeof Parameters, Metadata>
  49. }),
  50. )