builtin-mcs-search-wire.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { readFile } from "node:fs/promises";
  2. import { describe, expect, it, vi } from "vitest";
  3. import {
  4. createModelId,
  5. createModelProviderId,
  6. WEBSEARCH_PROVIDER_NATIVE_SPEC,
  7. WEBSEARCH_TOOL_CONTRACT,
  8. } from "@zcode/contracts";
  9. import {
  10. createRegistryModelConfig,
  11. createRegistryProviderConfig,
  12. parseModelConfig,
  13. parseProviderConfig,
  14. parseZCodeBuiltinModelConfigRules,
  15. } from "@zcode/provider";
  16. import { buildProviderRequestMessages } from "../../core/src/runtime/helpers/provider-request-messages.js";
  17. import { AiSdkModelAdapter } from "../src/model/runner.js";
  18. describe("Published MCS and search final Anthropic wire", () => {
  19. it.each([
  20. [
  21. "account:zai-start-plan",
  22. "https://zcode.z.ai/api/v1/zcode-plan/anthropic",
  23. "GLM-5.3-Flash",
  24. true,
  25. true,
  26. ],
  27. [
  28. "account:bigmodel-start-plan",
  29. "https://zcode.z.ai/api/v1/zcode-plan/anthropic",
  30. "GLM-5.3-Flash",
  31. true,
  32. true,
  33. ],
  34. [
  35. "account:zai-offpeak-idle-plan",
  36. "https://zcode.z.ai/api/v1/off-peak/anthropic",
  37. "GLM-5.3-Flash",
  38. true,
  39. true,
  40. ],
  41. [
  42. "account:bigmodel-offpeak-idle-plan",
  43. "https://zcode.z.ai/api/v1/off-peak/anthropic",
  44. "GLM-5.3-Flash",
  45. true,
  46. true,
  47. ],
  48. ["fixture", "https://api.anthropic.com/v1", "claude-sonnet-5", false, true],
  49. ["fixture", "https://proxy.invalid/v1", "vendor/claude-opus-4-8", true, false],
  50. ["personal-off", "https://proxy.invalid/v1", "vendor/claude-opus-4-8", false, false],
  51. ] as const)("%s / %s / %s", async (providerId, baseUrl, modelId, mcs, search) => {
  52. const release = JSON.parse(
  53. await readFile(
  54. new URL("../../../../../config/provider/zcode-builtin.json", import.meta.url),
  55. "utf8",
  56. ),
  57. );
  58. const rules = parseZCodeBuiltinModelConfigRules(release.config.modelConfigRules);
  59. let config = rules.resolve({ providerId, baseUrl, modelId, apiType: "anthropic-messages" });
  60. if (providerId === "personal-off")
  61. config = config.overlay(
  62. parseModelConfig({ properties: { supportsMidConversationSystem: false } }),
  63. );
  64. const modelConfig = createRegistryModelConfig(config);
  65. const providerConfig = createRegistryProviderConfig(
  66. parseProviderConfig({
  67. group: "standard-personal",
  68. access: { type: "api-key", apiKey: "fixture-not-a-real-key" },
  69. api: { type: "anthropic-messages", baseUrl },
  70. }),
  71. );
  72. if (!modelConfig.ok || !providerConfig.ok) throw new Error("Incomplete fixture");
  73. const bodies: Array<{
  74. messages: Array<{ role: string; content: unknown }>;
  75. tools?: unknown[];
  76. }> = [];
  77. vi.stubGlobal("fetch", async (url: RequestInfo | URL, init?: RequestInit) => {
  78. const request = new Request(url, init);
  79. expect(request.url).toBe(`${baseUrl}${baseUrl.endsWith("/v1") ? "" : "/v1"}/messages`);
  80. bodies.push(await request.json());
  81. return Response.json({
  82. id: "fixture",
  83. type: "message",
  84. role: "assistant",
  85. model: modelId,
  86. content: [{ type: "text", text: "ok" }],
  87. stop_reason: "end_turn",
  88. stop_sequence: null,
  89. usage: { input_tokens: 1, output_tokens: 1 },
  90. });
  91. });
  92. try {
  93. const adapter = new AiSdkModelAdapter({ env: {}, retry: { maxAttempts: 1 } });
  94. const model = adapter.createModel({
  95. providerId: createModelProviderId(providerId),
  96. modelId: createModelId(modelId),
  97. providerConfig: providerConfig.config,
  98. modelConfig: modelConfig.config,
  99. options: {
  100. maxOutputTokens: 4_000,
  101. reasoningLevel: config.optionSpecs!.reasoningLevel!.values!.at(-1)!,
  102. },
  103. });
  104. await model.generateText({
  105. messages: buildProviderRequestMessages({
  106. useMidConversationSystem: model.properties.supportsMidConversationSystem,
  107. entries: [
  108. { message: { role: "system", content: "stable prefix" } },
  109. { message: { role: "user", content: "hello" }, metadata: { source: "real_user" } },
  110. {
  111. kind: "attachment",
  112. content: "CAPABILITY_REMINDER",
  113. metadata: { source: "output_style" },
  114. },
  115. ],
  116. }).messages,
  117. tools: search
  118. ? [
  119. {
  120. name: "web_search",
  121. capability: "web_search",
  122. description: "fixture search",
  123. executionMode: "providerNative",
  124. inputSchema: WEBSEARCH_TOOL_CONTRACT.inputSchema,
  125. outputSchema: WEBSEARCH_TOOL_CONTRACT.outputSchema,
  126. providerNative: WEBSEARCH_PROVIDER_NATIVE_SPEC,
  127. },
  128. ]
  129. : [],
  130. });
  131. expect(bodies).toHaveLength(1);
  132. const body = bodies[0]!;
  133. const reminder = body.messages.find((message) =>
  134. JSON.stringify(message.content).includes("CAPABILITY_REMINDER"),
  135. );
  136. expect(reminder?.role).toBe(mcs ? "system" : "user");
  137. if (!mcs) expect(JSON.stringify(reminder?.content)).toContain("<system-reminder>");
  138. if (search)
  139. expect(body.tools).toContainEqual(
  140. expect.objectContaining({ name: "web_search", type: "web_search_20260209" }),
  141. );
  142. } finally {
  143. vi.unstubAllGlobals();
  144. }
  145. });
  146. });