websearch.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // ============================================================
  2. // Internal Provider-Native WebSearch Model Tool Tests
  3. // ============================================================
  4. import {
  5. WEBSEARCH_PROVIDER_NATIVE_SPEC,
  6. WEBSEARCH_TOOL_CONTRACT,
  7. type ModelToolContract,
  8. } from "@zcode/contracts";
  9. import { describe, expect, it } from "vitest";
  10. import { toAiSdkTools } from "../src/model/index.js";
  11. function createInternalProviderNativeWebSearchContract(
  12. args?: Record<string, unknown>,
  13. ): ModelToolContract {
  14. return {
  15. name: "web_search",
  16. capability: "web_search",
  17. description: "Provider-native web search used internally by WebSearch",
  18. executionMode: "providerNative",
  19. inputSchema: WEBSEARCH_TOOL_CONTRACT.inputSchema,
  20. outputSchema: WEBSEARCH_TOOL_CONTRACT.outputSchema,
  21. providerNative: {
  22. ...WEBSEARCH_PROVIDER_NATIVE_SPEC,
  23. args,
  24. },
  25. };
  26. }
  27. describe("internal provider-native WebSearch model tools", () => {
  28. it("keeps the public WebSearch model-facing tool as WebSearch", () => {
  29. const tools = toAiSdkTools(
  30. [
  31. {
  32. name: "WebSearch",
  33. capability: "web_search",
  34. description: "Search the web",
  35. executionMode: "client",
  36. inputSchema: WEBSEARCH_TOOL_CONTRACT.inputSchema,
  37. outputSchema: WEBSEARCH_TOOL_CONTRACT.outputSchema,
  38. },
  39. ],
  40. { providerKind: "anthropic" },
  41. );
  42. expect(Object.keys(tools ?? {})).toEqual(["WebSearch"]);
  43. expect(tools?.web_search).toBeUndefined();
  44. expect(tools?.WebSearch).not.toMatchObject({
  45. id: "anthropic.web_search_20260209",
  46. type: "provider",
  47. });
  48. });
  49. it("maps internal web_search to Anthropic provider tools", () => {
  50. const tools = toAiSdkTools(
  51. [
  52. createInternalProviderNativeWebSearchContract({
  53. allowedDomains: ["example.com"],
  54. blockedDomains: ["blocked.example"],
  55. maxUses: 3,
  56. }),
  57. ],
  58. {
  59. providerKind: "anthropic",
  60. supportsNativeWebSearch: true,
  61. },
  62. );
  63. expect(tools?.web_search).toMatchObject({
  64. args: {
  65. allowedDomains: ["example.com"],
  66. blockedDomains: ["blocked.example"],
  67. maxUses: 3,
  68. },
  69. id: "anthropic.web_search_20260209",
  70. type: "provider",
  71. });
  72. });
  73. it("maps Anthropic native WebSearch from the resolved Model Config fact", () => {
  74. const tools = toAiSdkTools([createInternalProviderNativeWebSearchContract()], {
  75. providerKind: "anthropic",
  76. supportsNativeWebSearch: true,
  77. });
  78. expect(tools?.web_search).toMatchObject({
  79. id: "anthropic.web_search_20260209",
  80. type: "provider",
  81. });
  82. });
  83. it.each(["openai", "openai-compatible"] as const)(
  84. "fails closed for a configured native WebSearch capability unsupported by API kind %s",
  85. (providerKind) => {
  86. expect(() =>
  87. toAiSdkTools([createInternalProviderNativeWebSearchContract()], {
  88. providerKind,
  89. supportsNativeWebSearch: true,
  90. }),
  91. ).toThrowError(/does not encode provider-native WebSearch/u);
  92. },
  93. );
  94. it("fails closed when the effective model capability is disabled", () => {
  95. expect(() =>
  96. toAiSdkTools([createInternalProviderNativeWebSearchContract()], {
  97. providerKind: "anthropic",
  98. supportsNativeWebSearch: false,
  99. }),
  100. ).toThrowError(/does not support provider-native WebSearch/u);
  101. });
  102. });