|
| 1 | +import { ManagedAgent } from '../src/api/agent/ManagedAgent'; |
| 2 | +import { LDAIConfigTracker } from '../src/api/config/LDAIConfigTracker'; |
| 3 | +import { LDAIAgentConfig } from '../src/api/config/types'; |
| 4 | +import { Evaluator } from '../src/api/judge/Evaluator'; |
| 5 | +import { LDJudgeResult } from '../src/api/judge/types'; |
| 6 | +import { RunnerResult } from '../src/api/model/types'; |
| 7 | +import { Runner } from '../src/api/providers/Runner'; |
| 8 | + |
| 9 | +describe('ManagedAgent', () => { |
| 10 | + let mockRunner: jest.Mocked<Runner>; |
| 11 | + let mockTracker: jest.Mocked<LDAIConfigTracker>; |
| 12 | + let agentConfig: LDAIAgentConfig; |
| 13 | + |
| 14 | + const runnerResult: RunnerResult = { |
| 15 | + content: 'Agent response', |
| 16 | + metrics: { success: true }, |
| 17 | + }; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + mockRunner = { |
| 21 | + run: jest.fn().mockResolvedValue(runnerResult), |
| 22 | + }; |
| 23 | + |
| 24 | + mockTracker = { |
| 25 | + trackMetricsOf: jest.fn().mockImplementation(async (_extractor: any, func: any) => func()), |
| 26 | + trackJudgeResult: jest.fn(), |
| 27 | + resumptionToken: 'agent-resumption-token', |
| 28 | + getTrackData: jest.fn().mockReturnValue({}), |
| 29 | + trackDuration: jest.fn(), |
| 30 | + trackTokens: jest.fn(), |
| 31 | + trackSuccess: jest.fn(), |
| 32 | + trackError: jest.fn(), |
| 33 | + trackFeedback: jest.fn(), |
| 34 | + trackTimeToFirstToken: jest.fn(), |
| 35 | + trackDurationOf: jest.fn(), |
| 36 | + trackOpenAIMetrics: jest.fn(), |
| 37 | + trackBedrockConverseMetrics: jest.fn(), |
| 38 | + trackVercelAIMetrics: jest.fn(), |
| 39 | + getSummary: jest.fn(), |
| 40 | + } as any; |
| 41 | + |
| 42 | + agentConfig = { |
| 43 | + key: 'test-agent', |
| 44 | + enabled: true, |
| 45 | + instructions: 'You are a helpful agent.', |
| 46 | + model: { name: 'gpt-4' }, |
| 47 | + provider: { name: 'openai' }, |
| 48 | + createTracker: () => mockTracker, |
| 49 | + }; |
| 50 | + }); |
| 51 | + |
| 52 | + it('returns a ManagedResult with content and metrics', async () => { |
| 53 | + const agent = new ManagedAgent(agentConfig, mockRunner); |
| 54 | + const result = await agent.run('Hello agent'); |
| 55 | + |
| 56 | + expect(result.content).toBe('Agent response'); |
| 57 | + expect(result.metrics.success).toBe(true); |
| 58 | + expect(result.metrics.resumptionToken).toBe('agent-resumption-token'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('passes the prompt directly to the runner', async () => { |
| 62 | + const agent = new ManagedAgent(agentConfig, mockRunner); |
| 63 | + await agent.run('My question'); |
| 64 | + |
| 65 | + expect(mockRunner.run).toHaveBeenCalledWith('My question'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('resolves to empty evaluations when no evaluator configured', async () => { |
| 69 | + const agent = new ManagedAgent(agentConfig, mockRunner); |
| 70 | + const result = await agent.run('Hello'); |
| 71 | + const evaluations = await result.evaluations; |
| 72 | + expect(evaluations).toEqual([]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('resolves to empty evaluations with noop evaluator', async () => { |
| 76 | + const configWithNoop: LDAIAgentConfig = { |
| 77 | + ...agentConfig, |
| 78 | + evaluator: Evaluator.noop(), |
| 79 | + }; |
| 80 | + const agent = new ManagedAgent(configWithNoop, mockRunner); |
| 81 | + const result = await agent.run('Hello'); |
| 82 | + const evaluations = await result.evaluations; |
| 83 | + expect(evaluations).toEqual([]); |
| 84 | + }); |
| 85 | + |
| 86 | + it('awaiting evaluations calls tracker.trackJudgeResult', async () => { |
| 87 | + const judgeResult: LDJudgeResult = { |
| 88 | + success: true, |
| 89 | + sampled: true, |
| 90 | + score: 0.85, |
| 91 | + metricKey: 'quality', |
| 92 | + }; |
| 93 | + const mockEvaluator = { |
| 94 | + judgeConfiguration: { judges: [{ key: 'judge-1', samplingRate: 1.0 }] }, |
| 95 | + evaluate: jest.fn().mockResolvedValue([judgeResult]), |
| 96 | + judges: new Map(), |
| 97 | + } as unknown as Evaluator; |
| 98 | + |
| 99 | + const configWithEvaluator: LDAIAgentConfig = { |
| 100 | + ...agentConfig, |
| 101 | + evaluator: mockEvaluator, |
| 102 | + }; |
| 103 | + |
| 104 | + const agent = new ManagedAgent(configWithEvaluator, mockRunner); |
| 105 | + const result = await agent.run('Hello'); |
| 106 | + |
| 107 | + await result.evaluations; |
| 108 | + expect(mockTracker.trackJudgeResult).toHaveBeenCalledWith(judgeResult); |
| 109 | + }); |
| 110 | + |
| 111 | + it('passes the prompt to evaluator.evaluate as input', async () => { |
| 112 | + const mockEvaluator = { |
| 113 | + judgeConfiguration: { judges: [{ key: 'judge-1', samplingRate: 1.0 }] }, |
| 114 | + evaluate: jest.fn().mockResolvedValue([]), |
| 115 | + judges: new Map(), |
| 116 | + } as unknown as Evaluator; |
| 117 | + |
| 118 | + const configWithEvaluator: LDAIAgentConfig = { |
| 119 | + ...agentConfig, |
| 120 | + evaluator: mockEvaluator, |
| 121 | + }; |
| 122 | + |
| 123 | + const agent = new ManagedAgent(configWithEvaluator, mockRunner); |
| 124 | + const result = await agent.run('user prompt'); |
| 125 | + await result.evaluations; |
| 126 | + |
| 127 | + expect(mockEvaluator.evaluate).toHaveBeenCalledWith('user prompt', 'Agent response'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('returns before evaluations resolve', async () => { |
| 131 | + let resolveEval!: (v: LDJudgeResult[]) => void; |
| 132 | + const slowEvaluator = { |
| 133 | + judgeConfiguration: { judges: [{ key: 'judge-1', samplingRate: 1.0 }] }, |
| 134 | + evaluate: jest.fn().mockReturnValue( |
| 135 | + new Promise<LDJudgeResult[]>((resolve) => { |
| 136 | + resolveEval = resolve; |
| 137 | + }), |
| 138 | + ), |
| 139 | + judges: new Map(), |
| 140 | + } as unknown as Evaluator; |
| 141 | + |
| 142 | + const configWithEvaluator: LDAIAgentConfig = { |
| 143 | + ...agentConfig, |
| 144 | + evaluator: slowEvaluator, |
| 145 | + }; |
| 146 | + |
| 147 | + const agent = new ManagedAgent(configWithEvaluator, mockRunner); |
| 148 | + |
| 149 | + let evaluationsResolved = false; |
| 150 | + const result = await agent.run('Hello'); |
| 151 | + |
| 152 | + expect(result.content).toBe('Agent response'); |
| 153 | + |
| 154 | + result.evaluations.then(() => { |
| 155 | + evaluationsResolved = true; |
| 156 | + }); |
| 157 | + |
| 158 | + await Promise.resolve(); |
| 159 | + expect(evaluationsResolved).toBe(false); |
| 160 | + |
| 161 | + resolveEval([{ success: true, sampled: true, score: 0.9 }]); |
| 162 | + await result.evaluations; |
| 163 | + expect(evaluationsResolved).toBe(true); |
| 164 | + }); |
| 165 | + |
| 166 | + it('exposes the agent config via getConfig', () => { |
| 167 | + const agent = new ManagedAgent(agentConfig, mockRunner); |
| 168 | + expect(agent.getConfig()).toBe(agentConfig); |
| 169 | + }); |
| 170 | +}); |
0 commit comments