tool.ts 1.2 KB

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