|
| 1 | +import { tool } from "ai"; |
| 2 | +import { MockLanguageModelV3 } from "ai/test"; |
| 3 | +import { expect, it, vi } from "vitest"; |
| 4 | +import { z } from "zod"; |
| 5 | +import { hasSuccessfulSearchValidation } from "../../tools/support/search-events/agent"; |
| 6 | +import { callEmbeddedAgent } from "./callEmbeddedAgent"; |
| 7 | +import { getAgentProvider } from "./provider-factory"; |
| 8 | + |
| 9 | +vi.mock("./provider-factory", () => ({ getAgentProvider: vi.fn() })); |
| 10 | +vi.mock("../../telem/logging", () => ({ |
| 11 | + logIssue: vi.fn(), |
| 12 | + logWarn: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +const usage = { |
| 16 | + inputTokens: { |
| 17 | + total: 1, |
| 18 | + noCache: 1, |
| 19 | + cacheRead: 0, |
| 20 | + cacheWrite: 0, |
| 21 | + }, |
| 22 | + outputTokens: { total: 1, text: 1, reasoning: 0 }, |
| 23 | +}; |
| 24 | + |
| 25 | +function setupAgent( |
| 26 | + validationPassesOnLastStep: boolean, |
| 27 | + finalStepValidationCount = 1, |
| 28 | +) { |
| 29 | + let callCount = 0; |
| 30 | + const model = new MockLanguageModelV3({ |
| 31 | + doGenerate: async () => { |
| 32 | + const step = ++callCount; |
| 33 | + if (step <= 5) { |
| 34 | + return { |
| 35 | + content: Array.from( |
| 36 | + { length: step === 5 ? finalStepValidationCount : 1 }, |
| 37 | + (_, candidate) => ({ |
| 38 | + type: "tool-call" as const, |
| 39 | + toolCallId: `validation-${step}-${candidate}`, |
| 40 | + toolName: "validateSearch", |
| 41 | + input: JSON.stringify({ step, candidate }), |
| 42 | + }), |
| 43 | + ), |
| 44 | + finishReason: { unified: "tool-calls" as const, raw: undefined }, |
| 45 | + usage, |
| 46 | + warnings: [], |
| 47 | + }; |
| 48 | + } |
| 49 | + return { |
| 50 | + content: [{ type: "text" as const, text: '{"result":"ready"}' }], |
| 51 | + finishReason: { unified: "stop" as const, raw: undefined }, |
| 52 | + usage, |
| 53 | + warnings: [], |
| 54 | + }; |
| 55 | + }, |
| 56 | + }); |
| 57 | + vi.mocked(getAgentProvider).mockReturnValue({ |
| 58 | + type: "openai", |
| 59 | + label: "mock", |
| 60 | + getModel: () => model, |
| 61 | + getProviderOptions: () => ({}), |
| 62 | + }); |
| 63 | + |
| 64 | + const validateSearch = tool({ |
| 65 | + inputSchema: z.object({ step: z.number(), candidate: z.number() }), |
| 66 | + execute: async ({ step }) => ({ |
| 67 | + result: { valid: validationPassesOnLastStep && step === 5 }, |
| 68 | + }), |
| 69 | + }); |
| 70 | + |
| 71 | + return { model, validateSearch }; |
| 72 | +} |
| 73 | + |
| 74 | +it("returns a structured answer after three validations pass on the fifth step", async () => { |
| 75 | + const { model, validateSearch } = setupAgent(true, 3); |
| 76 | + |
| 77 | + const result = await callEmbeddedAgent({ |
| 78 | + system: "Translate the query", |
| 79 | + prompt: "Test query", |
| 80 | + tools: { validateSearch }, |
| 81 | + schema: z.object({ result: z.string() }), |
| 82 | + isReadyToFinalize: ({ toolResults }) => |
| 83 | + hasSuccessfulSearchValidation(toolResults), |
| 84 | + }); |
| 85 | + |
| 86 | + expect(result.result).toEqual({ result: "ready" }); |
| 87 | + expect(result.toolCalls).toHaveLength(7); |
| 88 | + expect(model.doGenerateCalls).toHaveLength(6); |
| 89 | + expect(model.doGenerateCalls[5]?.toolChoice).toEqual({ type: "none" }); |
| 90 | +}); |
| 91 | + |
| 92 | +it("stops after five steps when the final validation fails", async () => { |
| 93 | + const { model, validateSearch } = setupAgent(false); |
| 94 | + |
| 95 | + await expect( |
| 96 | + callEmbeddedAgent({ |
| 97 | + system: "Translate the query", |
| 98 | + prompt: "Test query", |
| 99 | + tools: { validateSearch }, |
| 100 | + schema: z.object({ result: z.string() }), |
| 101 | + isReadyToFinalize: ({ toolResults }) => |
| 102 | + hasSuccessfulSearchValidation(toolResults), |
| 103 | + }), |
| 104 | + ).rejects.toThrow("could not construct a valid query"); |
| 105 | + |
| 106 | + expect(model.doGenerateCalls).toHaveLength(5); |
| 107 | +}); |
0 commit comments