tool.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { z } from "zod"
  2. import { Effect } from "effect"
  3. export type ToolContext = {
  4. sessionID: string
  5. messageID: string
  6. agent: string
  7. /**
  8. * Current project directory for this session.
  9. * Prefer this over process.cwd() when resolving relative paths.
  10. */
  11. directory: string
  12. /**
  13. * Project worktree root for this session.
  14. * Useful for generating stable relative paths (e.g. path.relative(worktree, absPath)).
  15. */
  16. worktree: string
  17. abort: AbortSignal
  18. metadata(input: { title?: string; metadata?: { [key: string]: any } }): void
  19. ask(input: AskInput): Effect.Effect<void>
  20. }
  21. type AskInput = {
  22. permission: string
  23. patterns: string[]
  24. always: string[]
  25. metadata: { [key: string]: any }
  26. }
  27. export type ToolAttachment = {
  28. type: "file"
  29. mime: string
  30. url: string
  31. filename?: string
  32. }
  33. export type ToolResult =
  34. | string
  35. | {
  36. title?: string
  37. output: string
  38. metadata?: { [key: string]: any }
  39. attachments?: ToolAttachment[]
  40. }
  41. export function tool<Args extends z.ZodRawShape>(input: {
  42. description: string
  43. args: Args
  44. execute(args: z.infer<z.ZodObject<Args>>, context: ToolContext): Promise<ToolResult>
  45. }) {
  46. return input
  47. }
  48. tool.schema = z
  49. export type ToolDefinition = ReturnType<typeof tool>