input-intent-persistence.test.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. source: { kind: "session", sessionId: "source-session", title: "来源会话" },
  52. admissionSeq: 4,
  53. admittedAt: 1000,
  54. requestedDelivery: "guide",
  55. admittedDelivery: "queue",
  56. provenance: {
  57. sourceCommandId: "command-original",
  58. queueItemId: "queue-original",
  59. clientId: "desktop-original",
  60. },
  61. };
  62. expect(buildPersistedConversationInputIntent("original text", intent, "drained")).toMatchObject(
  63. {
  64. sourceCommandId: "command-retry",
  65. source: intent.source,
  66. provenance: {
  67. sourceCommandId: "command-original",
  68. queueItemId: "queue-original",
  69. clientId: "desktop-original",
  70. },
  71. },
  72. );
  73. });
  74. });
  75. it("后台因果来源按整批判定,新输入覆盖旧来源", () => {
  76. const mobile = { attributes: { [TELEMETRY_INPUT_ORIGIN_ATTRIBUTE]: "mobile" } };
  77. const desktop = { attributes: { [TELEMETRY_INPUT_ORIGIN_ATTRIBUTE]: "desktop" } };
  78. expect(
  79. telemetryInputOrigin(undefined, backgroundBatchTelemetryAttributes([mobile, mobile])),
  80. ).toBe("mobile");
  81. expect(
  82. telemetryInputOrigin(undefined, backgroundBatchTelemetryAttributes([mobile, desktop])),
  83. ).toBe("mobile");
  84. expect(telemetryInputOrigin(undefined, backgroundBatchTelemetryAttributes([mobile, {}]))).toBe(
  85. "mobile",
  86. );
  87. expect(
  88. telemetryInputOrigin({ inputOrigin: "desktop" } as TurnInputIntentMetadata, mobile.attributes),
  89. ).toBe("desktop");
  90. });