{"version":3,"file":"hooks.cjs","names":["z"],"sources":["../src/hooks.ts"],"sourcesContent":["import { z } from \"zod/v3\";\nimport type { Command } from \"@langchain/langgraph\";\nimport type { EmbeddedResource } from \"@modelcontextprotocol/sdk/types.js\";\nimport type { ContentBlock } from \"@langchain/core/messages\";\nimport type { RunnableConfig } from \"@langchain/core/runnables\";\nimport type { ToolMessage } from \"@langchain/core/messages\";\n\n/**\n * state messages\n *\n * Note: this may not be defined in cases you don't use LangGraph or a LangGraph implementation like `createAgent`.\n * Also state can be defined arbitrarily by the user.\n */\nexport type State = Record<string, unknown>;\n\nconst toolCallRequestSchema = z.object({\n  serverName: z.string(),\n  name: z.string(),\n  args: z.unknown(),\n});\nexport type ToolCallRequest = z.output<typeof toolCallRequestSchema>;\n\nconst toolResultBeforeSchema = z.tuple([\n  z.custom<string | (ContentBlock | ContentBlock.Data.DataContentBlock)[]>(),\n  z.array(\n    z.union([\n      z.custom<EmbeddedResource>(),\n      z.custom<ContentBlock.Multimodal.Standard>(),\n    ])\n  ),\n]);\n\n/**\n * Tool result schema that users can return within the `afterToolCall` callback\n */\nconst toolResultSchema = z.union([\n  /**\n   * a basic string result\n   */\n  z.string(),\n  /**\n   * Command from LangGraph\n   */\n  z.custom<Command>(),\n  /**\n   * 2-tuple of content, artifact\n   */\n  toolResultBeforeSchema,\n  /**\n   * ToolMessage return\n   */\n  z.custom<ToolMessage>(),\n]);\nexport type ToolResult = z.output<typeof toolResultSchema>;\n\nconst toolCallResultSchema = z.object({\n  ...toolCallRequestSchema.shape,\n  result: toolResultBeforeSchema,\n});\n\nconst modifiedToolCallResultSchema = z.object({\n  ...toolCallRequestSchema.shape,\n  result: toolResultSchema,\n});\nexport type ModifiedToolCallResult = z.output<\n  typeof modifiedToolCallResultSchema\n>;\n\nconst toolCallModificationSchema = z\n  .object({\n    headers: z.record(z.string()),\n    args: z.unknown(),\n  })\n  .partial();\nexport type ToolCallModification = z.output<typeof toolCallModificationSchema>;\n\nexport const toolHooksSchema = z.object({\n  /**\n   * Called before a tool call is made.\n   * Allows you to modify the tool call arguments or return a different tool call.\n   *\n   * @param toolCallRequest - The tool call request\n   * @param toolCallRequest.name - The tool name\n   * @param toolCallRequest.args - The tool call arguments\n   * @param toolCallRequest.serverName - The server name\n   * @param config - The runnable config\n   * @returns The tool call modification\n   *\n   * @example\n   * ```ts\n   * const interceptor = {\n   *   beforeToolCall: (toolCallRequest, state, runtime) => {\n   *     return {\n   *       args: {\n   *         ...toolCallRequest.args,\n   *         custom: \"Custom Value\"\n   *       },\n   *       header: { \"X-Custom-Header\": \"Custom Value\" }\n   *     };\n   *   },\n   * };\n   * ```\n   */\n  beforeToolCall: z\n    .function()\n    .args(toolCallRequestSchema, z.custom<State>(), z.custom<RunnableConfig>())\n    .returns(\n      z.union([\n        z.promise(toolCallModificationSchema),\n        toolCallModificationSchema,\n        z.void(),\n        z.promise(z.void()),\n      ])\n    )\n    .optional(),\n\n  /**\n   * Called after a tool call is made.\n   * Allows you to modify the tool call result or return a different tool call result.\n   *\n   * @param toolCallResult - The tool call result\n   * @param toolCallResult.args - The tool call arguments\n   * @param toolCallResult.serverName - The server name\n   * @param toolCallResult.name - The tool name\n   * @param toolCallResult.result - The tool call result\n   * @param config - The runnable config\n   * @returns The tool call modification\n   * @example\n   * ```ts\n   * const interceptor = {\n   *   afterToolCall: (toolCallResult, state, runtime) => {\n   *     if (toolCallResult.name === \"calculator\") {\n   *       return [\"Custom Value\", []];\n   *     }\n   *     return toolCallResult.result;\n   *   },\n   * };\n   * ```\n   */\n  afterToolCall: z\n    .function()\n    .args(toolCallResultSchema, z.custom<State>(), z.custom<RunnableConfig>())\n    .returns(\n      z.union([\n        z.promise(modifiedToolCallResultSchema.pick({ result: true })),\n        modifiedToolCallResultSchema.pick({ result: true }),\n        z.void(),\n        z.promise(z.void()),\n      ])\n    )\n    .optional(),\n});\nexport type ToolHooks = z.input<typeof toolHooksSchema>;\n"],"mappings":";;AAeA,MAAM,wBAAwBA,OAAAA,EAAE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