input-intent-persistence.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import {
  2. telemetryInputOrigin,
  3. backgroundBatchTelemetryAttributes,
  4. TELEMETRY_INPUT_ORIGIN_ATTRIBUTE,
  5. } from "../src/runtime/methods/input-intent-persistence.js";
  6. import { describe, expect, it } from "vitest";
  7. import type { TurnInputIntentMetadata } from "@zcode/contracts";
  8. import { buildPersistedConversationInputIntent } from "../src/runtime/methods/input-intent-persistence.js";
  9. describe("persisted conversation input intent", () => {
  10. it.each(["build", "guarded", "yolo"] as const)(
  11. "persists %s with canonical goal text and queue intent",
  12. (mode) => {
  13. const intent: TurnInputIntentMetadata = {
  14. sourceCommandId: "command-goal",
  15. queueItemId: "queue-goal",
  16. clientId: "desktop-goal",
  17. kind: "sendGoalCommand",
  18. text: "X",
  19. admissionSeq: 3,
  20. admittedAt: 999,
  21. requestedDelivery: "queue",
  22. admittedDelivery: "queue",
  23. modelSelection: {
  24. providerId: "account:bigmodel-team-coding-plan",
  25. modelId: "GLM-5.3",
  26. options: { reasoningLevel: "high" },
  27. },
  28. mode,
  29. sharedContextRefs: [{ kind: "shared_context_import", context_id: "context-1" }],
  30. genUiContext: [{ path: "/project/example.html", state: { stage: "entry" } }],
  31. };
  32. expect(
  33. buildPersistedConversationInputIntent("/GoAl replace X", intent, "queued"),
  34. ).toMatchObject({
  35. kind: "sendGoalCommand",
  36. text: "X",
  37. modelSelection: intent.modelSelection,
  38. mode,
  39. sharedContextRefs: intent.sharedContextRefs,
  40. genUiContext: intent.genUiContext,
  41. });
  42. },
  43. );
  44. it("persists retry provenance with the rebuilt command intent", () => {
  45. const intent: TurnInputIntentMetadata = {
  46. sourceCommandId: "command-retry",
  47. queueItemId: "queue-retry",
  48. clientId: "desktop-new",
  49. kind: "sendText",
  50. text: "original text",
  51. admissionSeq: 4,
  52. admittedAt: 1000,
  53. requestedDelivery: "guide",
  54. admittedDelivery: "queue",
  55. provenance: {
  56. sourceCommandId: "command-original",
  57. queueItemId: "queue-original",
  58. clientId: "desktop-original",
  59. },
  60. };
  61. expect(buildPersistedConversationInputIntent("original text", intent, "drained")).toMatchObject(
  62. {
  63. sourceCommandId: "command-retry",
  64. provenance: {
  65. sourceCommandId: "command-original",
  66. queueItemId: "queue-original",
  67. clientId: "desktop-original",
  68. },
  69. },
  70. );
  71. });
  72. });
  73. it("后台因果来源按整批判定,新输入覆盖旧来源", () => {
  74. const mobile = { attributes: { [TELEMETRY_INPUT_ORIGIN_ATTRIBUTE]: "mobile" } };
  75. const desktop = { attributes: { [TELEMETRY_INPUT_ORIGIN_ATTRIBUTE]: "desktop" } };
  76. expect(
  77. telemetryInputOrigin(undefined, backgroundBatchTelemetryAttributes([mobile, mobile])),
  78. ).toBe("mobile");
  79. expect(
  80. telemetryInputOrigin(undefined, backgroundBatchTelemetryAttributes([mobile, desktop])),
  81. ).toBe("mobile");
  82. expect(telemetryInputOrigin(undefined, backgroundBatchTelemetryAttributes([mobile, {}]))).toBe(
  83. "mobile",
  84. );
  85. expect(
  86. telemetryInputOrigin({ inputOrigin: "desktop" } as TurnInputIntentMetadata, mobile.attributes),
  87. ).toBe("desktop");
  88. });