tool.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 ToolResult = string | { output: string; metadata?: { [key: string]: any } }
  28. export function tool<Args extends z.ZodRawShape>(input: {
  29. description: string
  30. args: Args
  31. execute(args: z.infer<z.ZodObject<Args>>, context: ToolContext): Promise<ToolResult>
  32. }) {
  33. return input
  34. }
  35. tool.schema = z
  36. export type ToolDefinition = ReturnType<typeof tool>