tool.ts 987 B

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