model-execution-key-id.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { describe, expect, it } from "vitest";
  2. import { zcodeSessionSendParamsSchema } from "@zcode/shared";
  3. import { createModelExecutionContext } from "../src/zcode-protocol/model-execution.js";
  4. describe("请求期 Key ID 执行上下文", () => {
  5. it("协议与冻结投影保留真实 ID,不将其加入模型 Header", async () => {
  6. const requestAuth = { apiKey: "pat", apiKeyId: "real-key-id", accountScope: "a".repeat(64) };
  7. const execution = createModelExecutionContext(
  8. zcodeSessionSendParamsSchema.shape.modelExecution
  9. .unwrap()
  10. .parse({ selectionScope: "execution", requestAuth }),
  11. );
  12. const value = await execution.requestDependencies?.requestAuth?.source?.resolve({
  13. attempt: 1,
  14. providerId: "account:bigmodel-off-peak",
  15. modelId: "glm",
  16. });
  17. expect(value).toEqual(requestAuth);
  18. expect(Object.isFrozen(value)).toBe(true);
  19. expect(value?.headers).toBeUndefined();
  20. });
  21. it("原样透传 selectionFallback 声明,包括发起方声明的退回目标", () => {
  22. // 退回目标必须完整到达 core:冷恢复竞态下 runtime 可能没有常驻选择,这是唯一的退回依据。
  23. const selectionFallback = {
  24. providerId: "account:bigmodel-highspeed-card",
  25. rules: [
  26. { reason: "highspeed_card_expired" as const, providerErrorCode: "3402" },
  27. { reason: "highspeed_request_failed" as const },
  28. ],
  29. target: {
  30. providerId: "account:bigmodel-team-coding-plan",
  31. modelId: "GLM-5.3",
  32. options: { reasoningLevel: "low" },
  33. },
  34. };
  35. const execution = createModelExecutionContext(
  36. zcodeSessionSendParamsSchema.shape.modelExecution
  37. .unwrap()
  38. .parse({ selectionScope: "execution", selectionFallback }),
  39. );
  40. expect(execution.selectionFallback).toEqual(selectionFallback);
  41. });
  42. });