turn-machine.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // ============================================================
  2. // Test imports
  3. // ============================================================
  4. import { describe, it, expect } from "vitest";
  5. import { createToolCallId } from "@zcode/contracts";
  6. import { TurnMachineImpl } from "../src/agent/turn-machine.js";
  7. import { TurnPhase } from "../src/agent/turn-state.js";
  8. // -----------------------------------------------
  9. // Turn Machine Tests
  10. // -----------------------------------------------
  11. describe("TurnMachine", () => {
  12. describe("state transitions", () => {
  13. it("should create a turn machine in Idle phase", () => {
  14. const machine = TurnMachineImpl.create("sess_123" as any, 1, "Hello");
  15. expect(machine.state.phase).toBe(TurnPhase.Idle);
  16. expect(machine.state.input).toBe("Hello");
  17. });
  18. it("should transition from Idle to ProcessingInput on start()", () => {
  19. const machine = TurnMachineImpl.create("sess_123" as any, 1, "Hello");
  20. const nextState = machine.start();
  21. expect(nextState.phase).toBe(TurnPhase.ProcessingInput);
  22. });
  23. it("should transition to AwaitingModelResponse", () => {
  24. const machine = startedMachine();
  25. const nextState = machine.startModelRequest("claude", []);
  26. expect(nextState.phase).toBe(TurnPhase.AwaitingModelResponse);
  27. });
  28. it("should drive a model-tool-model loop", () => {
  29. const toolCallId = createToolCallId("loop");
  30. const machine = modelWaitingMachine();
  31. const streaming = new TurnMachineImpl(machine.receiveModelResponse(""));
  32. const scheduled = new TurnMachineImpl(
  33. streaming.scheduleTools(
  34. [{ id: toolCallId, name: "Read", input: {} }],
  35. {
  36. items: [{ toolCallId, dependencies: [], canRunParallel: true }],
  37. parallelGroups: [[toolCallId]],
  38. executionOrder: [toolCallId],
  39. },
  40. ),
  41. );
  42. const executing = new TurnMachineImpl(scheduled.startToolExecution());
  43. const completed = new TurnMachineImpl(
  44. executing.completeTool(toolCallId, { success: true, content: "ok" }),
  45. );
  46. const aggregating = new TurnMachineImpl(completed.aggregateResults());
  47. const nextModel = aggregating.startModelRequest("claude", []);
  48. expect(nextModel.phase).toBe(TurnPhase.AwaitingModelResponse);
  49. });
  50. it("preserves structured tool result content", () => {
  51. const toolCallId = createToolCallId("image-result");
  52. const imageContent = [
  53. {
  54. type: "image" as const,
  55. mediaType: "image/png",
  56. dataUrl: "data:image/png;base64,aW1hZ2U=",
  57. source: { id: "read-image", kind: "inline" as const, placeholder: "Read image" },
  58. },
  59. ];
  60. const machine = modelWaitingMachine();
  61. const streaming = new TurnMachineImpl(machine.receiveModelResponse(""));
  62. const scheduled = new TurnMachineImpl(
  63. streaming.scheduleTools(
  64. [{ id: toolCallId, name: "ReadImage", input: {} }],
  65. {
  66. items: [{ toolCallId, dependencies: [], canRunParallel: true }],
  67. parallelGroups: [[toolCallId]],
  68. executionOrder: [toolCallId],
  69. },
  70. ),
  71. );
  72. const executing = new TurnMachineImpl(scheduled.startToolExecution());
  73. const completed = executing.completeTool(toolCallId, {
  74. success: true,
  75. content: imageContent,
  76. });
  77. expect(completed.toolResults.at(-1)?.content).toEqual(imageContent);
  78. expect(completed.toolCalls.at(-1)?.result?.content).toEqual(imageContent);
  79. });
  80. it("should complete a turn", () => {
  81. const machine = modelWaitingMachine();
  82. const nextState = machine.complete("Goodbye");
  83. expect(nextState.phase).toBe(TurnPhase.Completing);
  84. expect(nextState.finalResponse).toBe("Goodbye");
  85. });
  86. it("should throw on invalid transition", () => {
  87. const machine = TurnMachineImpl.create("sess_123" as any, 1, "Hello");
  88. expect(() => machine.complete("test")).toThrow();
  89. });
  90. });
  91. });
  92. function startedMachine(): TurnMachineImpl {
  93. const machine = TurnMachineImpl.create("sess_123" as any, 1, "Hello");
  94. return new TurnMachineImpl(machine.start());
  95. }
  96. function modelWaitingMachine(): TurnMachineImpl {
  97. const machine = startedMachine();
  98. return new TurnMachineImpl(machine.startModelRequest("claude", []));
  99. }