tool.ts 949 B

1234567891011121314151617181920212223242526272829303132333435363738
  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 function tool<Args extends z.ZodRawShape>(input: {
  27. description: string
  28. args: Args
  29. execute(args: z.infer<z.ZodObject<Args>>, context: ToolContext): Promise<string>
  30. }) {
  31. return input
  32. }
  33. tool.schema = z
  34. export type ToolDefinition = ReturnType<typeof tool>