mfjs-properties-wire.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import type { ModelToolContract } from "@zcode/contracts";
  3. import { TestAiSdkModelAdapter, TestProviderConfigFixture } from "./test-provider-config.js";
  4. import { createTestModelProperties } from "./test-model-format.js";
  5. describe("MFJS 使用固定执行模型的 properties", () => {
  6. afterEach(() => vi.unstubAllGlobals());
  7. for (const streaming of [false, true]) {
  8. it.each([false, true])(
  9. `stream=${streaming} / MFJS=%s 保持真实请求工具 schema`,
  10. async (enabled) => {
  11. const bodies: Array<{
  12. tools: Array<{
  13. input_schema: {
  14. properties: { alias: { $ref: string } };
  15. $defs?: Record<string, unknown>;
  16. };
  17. }>;
  18. }> = [];
  19. const registry = new TestProviderConfigFixture({
  20. providers: {
  21. mfjs: { kind: "anthropic", apiKey: "fake", baseURL: "https://mfjs.invalid/anthropic" },
  22. },
  23. });
  24. vi.stubGlobal("fetch", async (input: RequestInfo | URL, init?: RequestInit) => {
  25. const request = new Request(input, init);
  26. bodies.push(await request.json());
  27. // 只抓出站载荷,明确终止;不消耗账号额度,也不伪造成功模型结果。
  28. return new Response(
  29. JSON.stringify({ error: { type: "invalid_request_error", message: "captured" } }),
  30. {
  31. status: 400,
  32. headers: { "content-type": "application/json" },
  33. },
  34. );
  35. });
  36. const properties = createTestModelProperties({}, { requiresMfjsToolSchema: enabled });
  37. const model = new TestAiSdkModelAdapter({ registry }).createModel({
  38. providerId: "mfjs",
  39. modelId: "test-model",
  40. properties,
  41. });
  42. const tool: ModelToolContract = {
  43. name: "Read",
  44. capability: "read_file",
  45. description: "Read",
  46. readOnly: true,
  47. inputSchema: {
  48. type: "object",
  49. properties: {
  50. source: { type: "object", properties: { path: { type: "string" } } },
  51. alias: { $ref: "#/properties/source" },
  52. },
  53. },
  54. outputSchema: { type: "object" },
  55. };
  56. const original = structuredClone(tool);
  57. const request = { messages: [{ role: "user" as const, content: "probe" }], tools: [tool] };
  58. if (streaming) {
  59. await expect(async () => {
  60. for await (const _event of model.streamText(request)) {
  61. /* 消费到明确错误。 */
  62. }
  63. }).rejects.toThrow();
  64. } else {
  65. await expect(model.generateText(request)).rejects.toThrow();
  66. }
  67. expect(bodies).toHaveLength(1);
  68. const schema = bodies[0]!.tools[0]!.input_schema;
  69. expect(schema.properties.alias.$ref).toBe(
  70. enabled ? "#/$defs/zcode_ref_0" : "#/properties/source",
  71. );
  72. if (enabled)
  73. expect(schema.$defs?.zcode_ref_0).toEqual({
  74. type: "object",
  75. properties: { path: { type: "string" } },
  76. });
  77. else expect(schema).not.toHaveProperty("$defs");
  78. expect(tool).toEqual(original);
  79. expect(model.properties.requiresMfjsToolSchema).toBe(enabled);
  80. },
  81. );
  82. }
  83. });