|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { Agent } from "./agent"; |
| 3 | +import { createMockLanguageModel, defaultMockResponse } from "./test-utils"; |
| 4 | + |
| 5 | +describe("prepareStep", () => { |
| 6 | + it("should accept prepareStep in AgentOptions", () => { |
| 7 | + const prepareStep = vi.fn(() => ({})); |
| 8 | + const model = createMockLanguageModel(); |
| 9 | + |
| 10 | + const agent = new Agent({ |
| 11 | + name: "test-agent", |
| 12 | + instructions: "test", |
| 13 | + model, |
| 14 | + prepareStep, |
| 15 | + }); |
| 16 | + |
| 17 | + expect(agent.prepareStep).toBe(prepareStep); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should default to undefined when prepareStep is not provided", () => { |
| 21 | + const model = createMockLanguageModel(); |
| 22 | + |
| 23 | + const agent = new Agent({ |
| 24 | + name: "test-agent", |
| 25 | + instructions: "test", |
| 26 | + model, |
| 27 | + }); |
| 28 | + |
| 29 | + expect(agent.prepareStep).toBeUndefined(); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should pass agent-level prepareStep to generateText", async () => { |
| 33 | + const prepareStep = vi.fn(() => ({})); |
| 34 | + const model = createMockLanguageModel({ |
| 35 | + doGenerate: { |
| 36 | + ...defaultMockResponse, |
| 37 | + content: [{ type: "text", text: "done" }], |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + const agent = new Agent({ |
| 42 | + name: "test-agent", |
| 43 | + instructions: "test", |
| 44 | + model, |
| 45 | + prepareStep, |
| 46 | + }); |
| 47 | + |
| 48 | + await agent.generateText("hello"); |
| 49 | + |
| 50 | + // prepareStep is called by the AI SDK on each step |
| 51 | + expect(prepareStep).toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should pass agent-level prepareStep to streamText", async () => { |
| 55 | + const prepareStep = vi.fn(() => ({})); |
| 56 | + const model = createMockLanguageModel(); |
| 57 | + |
| 58 | + const agent = new Agent({ |
| 59 | + name: "test-agent", |
| 60 | + instructions: "test", |
| 61 | + model, |
| 62 | + prepareStep, |
| 63 | + }); |
| 64 | + |
| 65 | + const result = await agent.streamText("hello"); |
| 66 | + // consume the stream to completion |
| 67 | + for await (const _part of result.textStream) { |
| 68 | + // drain |
| 69 | + } |
| 70 | + |
| 71 | + expect(prepareStep).toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should allow per-call prepareStep to override agent-level", async () => { |
| 75 | + const agentPrepareStep = vi.fn(() => ({})); |
| 76 | + const callPrepareStep = vi.fn(() => ({})); |
| 77 | + const model = createMockLanguageModel({ |
| 78 | + doGenerate: { |
| 79 | + ...defaultMockResponse, |
| 80 | + content: [{ type: "text", text: "done" }], |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + const agent = new Agent({ |
| 85 | + name: "test-agent", |
| 86 | + instructions: "test", |
| 87 | + model, |
| 88 | + prepareStep: agentPrepareStep, |
| 89 | + }); |
| 90 | + |
| 91 | + await agent.generateText("hello", { |
| 92 | + prepareStep: callPrepareStep, |
| 93 | + }); |
| 94 | + |
| 95 | + // per-call should be used, not agent-level |
| 96 | + expect(callPrepareStep).toHaveBeenCalled(); |
| 97 | + expect(agentPrepareStep).not.toHaveBeenCalled(); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should allow per-call prepareStep to override agent-level in streamText", async () => { |
| 101 | + const agentPrepareStep = vi.fn(() => ({})); |
| 102 | + const callPrepareStep = vi.fn(() => ({})); |
| 103 | + const model = createMockLanguageModel(); |
| 104 | + |
| 105 | + const agent = new Agent({ |
| 106 | + name: "test-agent", |
| 107 | + instructions: "test", |
| 108 | + model, |
| 109 | + prepareStep: agentPrepareStep, |
| 110 | + }); |
| 111 | + |
| 112 | + const result = await agent.streamText("hello", { |
| 113 | + prepareStep: callPrepareStep, |
| 114 | + }); |
| 115 | + for await (const _part of result.textStream) { |
| 116 | + // drain |
| 117 | + } |
| 118 | + |
| 119 | + expect(callPrepareStep).toHaveBeenCalled(); |
| 120 | + expect(agentPrepareStep).not.toHaveBeenCalled(); |
| 121 | + }); |
| 122 | +}); |
0 commit comments