{"version":3,"file":"anthropicTools.test.mjs","names":["expect"],"sources":["../src/anthropicTools.test.ts"],"sourcesContent":["import { Effect, Option, Result, Schema } from \"effect\"\nimport * as Tool from \"@effect-uai/core/Tool\"\nimport { descriptorsOf, providerToolsOf } from \"@effect-uai/core/Tool\"\nimport { describe, expect, it } from \"vitest\"\nimport { codeExecutionTool, renderProviderTools, webSearchTool } from \"./AnthropicTools.js\"\nimport { accumulatorToTurn, buildRequestBody, emptyAccumulator } from \"./codec.js\"\nimport { KnownProviderEvent, applyEvent } from \"./streamEvents.js\"\n\nconst localTool = Tool.make({\n  name: \"get_weather\",\n  description: \"Look up the weather.\",\n  inputSchema: Tool.fromEffectSchema(Schema.Struct({ city: Schema.String })),\n  outputSchema: Schema.String,\n  run: ({ city }) => Effect.succeed(`sunny in ${city}`),\n})\n\nconst wireFor = (toolkit: Record<string, Tool.AnyTool>) =>\n  Result.getOrThrow(renderProviderTools(providerToolsOf(toolkit)))\n\nconst functionWire = (toolkit: Record<string, Tool.AnyTool>) =>\n  descriptorsOf(toolkit).map((t) => ({\n    name: t.name,\n    description: t.description,\n    input_schema: t.inputSchema,\n  }))\n\nconst buildWith = (\n  toolkit: Record<string, Tool.AnyTool>,\n  toolChoice: Option.Option<Record<string, unknown>> = Option.none(),\n) => {\n  const tools = [...functionWire(toolkit), ...wireFor(toolkit)]\n  return Result.getOrThrow(\n    buildRequestBody({\n      model: \"claude-sonnet-4-5\",\n      history: [{ type: \"message\", role: \"user\", content: [{ type: \"input_text\", text: \"hi\" }] }],\n      maxTokens: 1024,\n      temperature: Option.none(),\n      topP: Option.none(),\n      topK: Option.none(),\n      stopSequences: Option.none(),\n      thinking: Option.none(),\n      tools: tools.length > 0 ? Option.some(tools) : Option.none(),\n      toolChoice,\n      userId: Option.none(),\n      outputConfig: Option.none(),\n      cacheControl: Option.none(),\n    }),\n  )\n}\n\ndescribe(\"Anthropic provider tools\", () => {\n  it(\"renders web_search to the native wire entry with options\", () => {\n    const wire = wireFor({\n      web_search: webSearchTool({ maxUses: 3, allowedDomains: [\"example.com\"] }),\n    })\n    expect(wire).toEqual([\n      {\n        type: \"web_search_20250305\",\n        name: \"web_search\",\n        max_uses: 3,\n        allowed_domains: [\"example.com\"],\n      },\n    ])\n  })\n\n  it(\"renders code_execution to its native wire entry\", () => {\n    expect(wireFor({ code_execution: codeExecutionTool })).toEqual([\n      { type: \"code_execution_20250522\", name: \"code_execution\" },\n    ])\n  })\n\n  it(\"emits both a custom tool and the native entry in the same tools array\", () => {\n    const body = buildWith({ get_weather: localTool, web_search: webSearchTool() })\n    expect(body.tools).toEqual([\n      expect.objectContaining({ name: \"get_weather\" }),\n      { type: \"web_search_20250305\", name: \"web_search\" },\n    ])\n  })\n\n  it(\"omits tool_choice for a native-only request\", () => {\n    const body = buildWith({ web_search: webSearchTool() })\n    expect(body.tools).toHaveLength(1)\n    expect(body.tool_choice).toBeUndefined()\n  })\n\n  it(\"fails on a foreign provider tool\", () => {\n    const foreign = Tool.provider({\n      name: \"google_search\",\n      description: \"Gemini grounding.\",\n      inputSchema: Tool.noInput,\n      provider: \"gemini\",\n      config: { kind: \"google_search\" },\n    })\n    expect(Result.isFailure(renderProviderTools(providerToolsOf({ google_search: foreign })))).toBe(\n      true,\n    )\n  })\n\n  it(\"fails on an unrecognized config\", () => {\n    const bogus = Tool.provider({\n      name: \"mystery\",\n      description: \"Unknown Anthropic tool.\",\n      inputSchema: Tool.noInput,\n      provider: \"anthropic\",\n      config: { kind: \"telepathy\" },\n    })\n    expect(Result.isFailure(renderProviderTools(providerToolsOf({ mystery: bogus })))).toBe(true)\n  })\n})\n\ndescribe(\"Anthropic hosted-tool response tolerance\", () => {\n  it(\"does not decode a server_tool_use content block as a known event\", () => {\n    const serverToolUse = {\n      type: \"content_block_start\",\n      index: 0,\n      content_block: { type: \"server_tool_use\", id: \"srvtoolu_1\", name: \"web_search\", input: {} },\n    }\n    const decoded = Schema.decodeUnknownResult(KnownProviderEvent)(serverToolUse)\n    // Fails to decode → the SSE layer synthesizes `_unknown`, which is ignored.\n    expect(Result.isFailure(decoded)).toBe(true)\n  })\n\n  it(\"builds the text turn when hosted-tool blocks arrive as _unknown\", () => {\n    // Sequence: server_tool_use + web_search_tool_result blocks flow through as\n    // `_unknown` (no accumulator change); the assistant's text answer is a\n    // normal text block and survives into the turn.\n    const events = [\n      { type: \"_unknown\", raw: { type: \"content_block_start\", content_block: \"server_tool_use\" } },\n      { type: \"content_block_start\", index: 1, content_block: { type: \"text\", text: \"\" } },\n      {\n        type: \"content_block_delta\",\n        index: 1,\n        delta: { type: \"text_delta\", text: \"Grounded answer.\" },\n      },\n      { type: \"content_block_stop\", index: 1 },\n      { type: \"message_stop\" },\n    ] as const\n    const acc = events.reduce(applyEvent, emptyAccumulator)\n    const turn = accumulatorToTurn(acc)\n    expect(turn.items).toEqual([\n      {\n        type: \"message\",\n        role: \"assistant\",\n        content: [{ type: \"output_text\", text: \"Grounded answer.\" }],\n      },\n    ])\n  })\n})\n"],"mappings":";;;;;;;;AAQA,MAAM,YAAY,KAAK,KAAK;CAC1B,MAAM;CACN,aAAa;CACb,aAAa,KAAK,iBAAiB,OAAO,OAAO,EAAE,MAAM,OAAO,OAAO,CAAC,CAAC;CACzE,cAAc,OAAO;CACrB,MAAM,EAAE,WAAW,OAAO,QAAQ,YAAY,MAAM;AACtD,CAAC;AAED,MAAM,WAAW,YACf,OAAO,WAAW,oBAAoB,gBAAgB,OAAO,CAAC,CAAC;AAEjE,MAAM,gBAAgB,YACpB,cAAc,OAAO,CAAC,CAAC,KAAK,OAAO;CACjC,MAAM,EAAE;CACR,aAAa,EAAE;CACf,cAAc,EAAE;AAClB,EAAE;AAEJ,MAAM,aACJ,SACA,aAAqD,OAAO,KAAK,MAC9D;CACH,MAAM,QAAQ,CAAC,GAAG,aAAa,OAAO,GAAG,GAAG,QAAQ,OAAO,CAAC;CAC5D,OAAO,OAAO,WACZ,iBAAiB;EACf,OAAO;EACP,SAAS,CAAC;GAAE,MAAM;GAAW,M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