|
| 1 | +--- |
| 2 | +title: Agent Adapter |
| 3 | +description: Implementing custom agent adapters for Agent QA |
| 4 | +--- |
| 5 | + |
| 6 | +# Agent Adapter |
| 7 | + |
| 8 | +The `AgentAdapter` interface allows you to customize how Agent QA communicates with your AI agent. |
| 9 | + |
| 10 | +## Interface |
| 11 | + |
| 12 | +```typescript |
| 13 | +interface AgentAdapter { |
| 14 | + chat(options: ChatOptions): Promise<AgentResponse>; |
| 15 | + cleanup?(): Promise<void>; |
| 16 | +} |
| 17 | + |
| 18 | +interface ChatOptions { |
| 19 | + message: string; |
| 20 | + userId: string; |
| 21 | + conversationId?: string; |
| 22 | + maxToolCalls?: number; |
| 23 | + timeout?: number; |
| 24 | +} |
| 25 | + |
| 26 | +interface AgentResponse { |
| 27 | + text: string; |
| 28 | + toolCalls: ToolCall[]; |
| 29 | + conversationId: string; |
| 30 | + correlationId?: string; |
| 31 | + usage?: TokenUsage; |
| 32 | + detailedUsage?: DetailedUsage; |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +## Built-in HTTP Adapter |
| 37 | + |
| 38 | +The default adapter communicates via HTTP: |
| 39 | + |
| 40 | +```typescript |
| 41 | +import { createHttpAgentFromConfig } from '@agent-qa/core'; |
| 42 | + |
| 43 | +const adapter = createHttpAgentFromConfig({ |
| 44 | + baseUrl: 'http://localhost:4000', |
| 45 | + token: 'your-token', |
| 46 | + chatEndpoint: '/v1/chat', |
| 47 | +}); |
| 48 | +``` |
| 49 | + |
| 50 | +## Example: WebSocket Adapter |
| 51 | + |
| 52 | +```typescript |
| 53 | +import type { AgentAdapter, ChatOptions, AgentResponse } from '@agent-qa/core'; |
| 54 | + |
| 55 | +export function createWebSocketAdapter(url: string): AgentAdapter { |
| 56 | + let ws: WebSocket | null = null; |
| 57 | + |
| 58 | + return { |
| 59 | + async chat(options: ChatOptions): Promise<AgentResponse> { |
| 60 | + if (!ws || ws.readyState !== WebSocket.OPEN) { |
| 61 | + ws = new WebSocket(url); |
| 62 | + await new Promise((resolve, reject) => { |
| 63 | + ws!.onopen = resolve; |
| 64 | + ws!.onerror = reject; |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + return new Promise((resolve, reject) => { |
| 69 | + const timeout = setTimeout(() => { |
| 70 | + reject(new Error('Chat timeout')); |
| 71 | + }, options.timeout ?? 60000); |
| 72 | + |
| 73 | + ws!.onmessage = (event) => { |
| 74 | + clearTimeout(timeout); |
| 75 | + const data = JSON.parse(event.data); |
| 76 | + resolve({ |
| 77 | + text: data.response, |
| 78 | + toolCalls: data.toolCalls ?? [], |
| 79 | + conversationId: data.conversationId, |
| 80 | + correlationId: data.correlationId, |
| 81 | + usage: data.usage, |
| 82 | + }); |
| 83 | + }; |
| 84 | + |
| 85 | + ws!.send(JSON.stringify({ |
| 86 | + message: options.message, |
| 87 | + userId: options.userId, |
| 88 | + conversationId: options.conversationId, |
| 89 | + })); |
| 90 | + }); |
| 91 | + }, |
| 92 | + |
| 93 | + async cleanup(): Promise<void> { |
| 94 | + if (ws) { |
| 95 | + ws.close(); |
| 96 | + ws = null; |
| 97 | + } |
| 98 | + }, |
| 99 | + }; |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +## Using Custom Agent Adapter |
| 104 | + |
| 105 | +```typescript |
| 106 | +// agentqa.config.ts |
| 107 | +import { defineConfig } from '@agent-qa/core'; |
| 108 | +import { createWebSocketAdapter } from './ws-adapter'; |
| 109 | + |
| 110 | +export default defineConfig({ |
| 111 | + name: 'MyApp', |
| 112 | + |
| 113 | + agent: { |
| 114 | + adapter: createWebSocketAdapter('ws://localhost:4000/chat'), |
| 115 | + }, |
| 116 | +}); |
| 117 | +``` |
| 118 | + |
| 119 | +## Programmatic Usage |
| 120 | + |
| 121 | +```typescript |
| 122 | +import { createTestRunner } from '@agent-qa/core'; |
| 123 | +import { createWebSocketAdapter } from './ws-adapter'; |
| 124 | + |
| 125 | +const runner = createTestRunner(config, { |
| 126 | + agent: createWebSocketAdapter('ws://localhost:4000/chat'), |
| 127 | +}); |
| 128 | +``` |
0 commit comments