-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathrouter.test.ts
More file actions
281 lines (234 loc) · 9.4 KB
/
router.test.ts
File metadata and controls
281 lines (234 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright (c) 2025 The Linux Foundation and each contributor.
// SPDX-License-Identifier: MIT
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Router Agent Tests with Real AI Model Execution
*
* Tests router agent with actual Bedrock model and real Tinybird MCP tools
*/
import { describe, test, expect, beforeAll, afterAll } from 'vitest';
import { createAmazonBedrock, type AmazonBedrockProvider } from '@ai-sdk/amazon-bedrock';
import { experimental_createMCPClient as createMCPClient, type LanguageModelV1 } from 'ai';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import { RouterAgent } from '../agents/router';
import { RouterDecisionAction } from '../enums';
import type { ChatMessage, RouterAgentInput } from '../types';
let bedrock: AmazonBedrockProvider | null = null;
describe('Router Agent', () => {
let model: LanguageModelV1;
let mcpClient: any;
let tbTools: Record<string, any> = {};
let toolsOverview: string = '';
beforeAll(async () => {
// Check if we have the required environment variables
const tinybirdToken = process.env.NUXT_INSIGHTS_DATA_COPILOT_TINYBIRD_TOKEN;
const tinybirdBaseUrl = process.env.NUXT_TINYBIRD_BASE_URL;
const hasAwsCredentials = process.env.NUXT_AWS_BEDROCK_ACCESS_KEY_ID;
if (!tinybirdToken || !tinybirdBaseUrl || !hasAwsCredentials) {
console.warn('⚠️ Skipping real integration tests - missing credentials');
console.warn(
'Required: NUXT_INSIGHTS_DATA_COPILOT_TINYBIRD_TOKEN, NUXT_TINYBIRD_BASE_URL, AWS Bedrock credentials',
);
return;
}
// Initialize AWS Bedrock model exactly like DataCopilot
bedrock = createAmazonBedrock({
accessKeyId: process.env.NUXT_AWS_BEDROCK_ACCESS_KEY_ID,
secretAccessKey: process.env.NUXT_AWS_BEDROCK_SECRET_ACCESS_KEY,
region: process.env.NUXT_AWS_BEDROCK_REGION,
});
// Initialize model once, like DataCopilot does in constructor
const BEDROCK_MODEL_ID = 'us.anthropic.claude-opus-4-6-v1';
model = bedrock(BEDROCK_MODEL_ID);
// Initialize MCP client to get real tools - same as DataCopilot
const tbMcpUrl = `https://mcp.tinybird.co?token=${tinybirdToken}&host=${tinybirdBaseUrl}`;
const url = new URL(tbMcpUrl);
try {
mcpClient = await createMCPClient({
transport: new StreamableHTTPClientTransport(url, {
sessionId: `test_session_${Date.now()}`,
}),
});
// Load real tools from Tinybird MCP
tbTools = await mcpClient.tools({});
buildToolsOverview();
console.warn(`✅ Connected to Tinybird MCP - ${Object.keys(tbTools).length} tools loaded`);
} catch (error) {
console.error('❌ Failed to connect to Tinybird MCP:', error);
throw error;
}
}, 30000);
afterAll(async () => {
if (mcpClient) {
try {
await mcpClient.close?.();
} catch (error) {
console.warn('Warning: Could not close MCP client:', error);
}
}
});
// Build tools overview exactly like DataCopilot does
function buildToolsOverview(): void {
const excludedFromOverview = new Set([
'explore_data',
'execute_query',
'text_to_sql',
'list_endpoints',
'list_service_datasources',
]);
const toolDescriptions: string[] = [];
for (const [toolName, tool] of Object.entries(tbTools)) {
if (excludedFromOverview.has(toolName)) continue;
const description = tool.description || tool.meta?.description || 'No description available';
toolDescriptions.push(`- ${toolName}: ${description}`);
}
toolsOverview = toolDescriptions.join('\n');
}
function createTestInput(userQuery: string): RouterAgentInput {
const messages: ChatMessage[] = [{ role: 'user', content: userQuery }];
console.warn('📝 Creating test input for query:', userQuery);
return {
model,
messages,
tools: tbTools,
toolsOverview,
date: new Date().toISOString().slice(0, 10),
projectName: 'test-project',
pipe: 'test-pipe',
parametersString: '{}',
segmentId: 'test-segment',
};
}
function skipIfNoCredentials() {
const hasCredentials =
process.env.NUXT_INSIGHTS_DATA_COPILOT_TINYBIRD_TOKEN &&
process.env.NUXT_TINYBIRD_BASE_URL &&
process.env.NUXT_AWS_BEDROCK_ACCESS_KEY_ID;
if (!hasCredentials) {
console.warn('Skipping test - missing credentials');
return true;
}
return false;
}
describe('Basic functionality', () => {
test('should create original router agent successfully', () => {
const router = new RouterAgent();
expect(router).toBeDefined();
expect(router.name).toBe('Router');
expect(router.temperature).toBe(0);
});
test('should validate output schema for both implementations', () => {
const originalRouter = new RouterAgent();
const validOutput = {
next_action: RouterDecisionAction.PIPES,
reasoning: 'Test reasoning',
reformulated_question: 'Test question',
tools: ['activities_count'],
};
// Both should use the same schema
expect(originalRouter.outputSchema.safeParse(validOutput).success).toBe(true);
});
test('should reject invalid output for both implementations', () => {
const originalRouter = new RouterAgent();
const invalidOutput = {
next_action: 'INVALID_ACTION',
reasoning: 'Test reasoning',
};
// Both should reject invalid output
expect(originalRouter.outputSchema.safeParse(invalidOutput).success).toBe(false);
});
test('should connect to MCP and load tools', () => {
if (skipIfNoCredentials()) return;
expect(Object.keys(tbTools).length).toBeGreaterThan(0);
expect(tbTools.list_datasources).toBeDefined();
expect(toolsOverview).toContain('activities');
});
});
describe('Real AI routing decisions', () => {
describe('PIPES routing', () => {
test.each([
'Show me commits this week',
'Show me stars for the previous week',
'Show me forks for the last year',
'List of companies contributing in project',
])(
'should route "%s" to PIPES',
async (query) => {
if (skipIfNoCredentials()) return;
console.warn(`🤖 Testing query: "${query}"`);
const router = new RouterAgent();
const input = createTestInput(query);
const result = await router.execute(input);
expect(result.next_action).toBe(RouterDecisionAction.PIPES);
expect(result.reasoning).toBeTruthy();
expect(result.reformulated_question).toBeTruthy();
expect(Array.isArray(result.tools)).toBe(true);
console.warn(`✅ "${query}" → ${result.next_action}`);
console.warn(`🔍 Reasoning: ${result.reasoning}`);
},
15000,
);
});
describe('CREATE_QUERY (TEXT_TO_SQL) routing', () => {
test.each([
'Show me commit activity by company over all time period',
'Show me the total counts of each contribution type for 2024.',
])(
'should route "%s" to CREATE_QUERY',
async (query) => {
if (skipIfNoCredentials()) return;
console.warn(`🤖 Testing query: "${query}"`);
const router = new RouterAgent();
const input = createTestInput(query);
const result = await router.execute(input);
expect(result.next_action).toBe(RouterDecisionAction.CREATE_QUERY);
expect(result.reasoning).toBeTruthy();
expect(result.reformulated_question).toBeTruthy();
console.warn(`✅ "${query}" → ${result.next_action}`);
console.warn(`🔍 Reasoning: ${result.reasoning}`);
},
15000,
);
});
describe('STOP routing', () => {
test.each([
"What's the weather forecast for contributors?",
'Show me contributors from Brazil',
])(
'should route "%s" to STOP',
async (query) => {
if (skipIfNoCredentials()) return;
console.warn(`🤖 Testing query: "${query}"`);
const router = new RouterAgent();
const input = createTestInput(query);
const result = await router.execute(input);
expect(result.next_action).toBe(RouterDecisionAction.STOP);
expect(result.reasoning).toBeTruthy();
expect(result.tools).toEqual([]);
console.warn(`✅ "${query}" → ${result.next_action}`);
console.warn(`🔍 Reasoning: ${result.reasoning}`);
},
15000,
);
});
describe('ASK_CLARIFICATION routing', () => {
test.each(['Show me the activity', 'Give me stats for last period', 'Show me metrics'])(
'should route "%s" to ASK_CLARIFICATION',
async (query) => {
if (skipIfNoCredentials()) return;
console.warn(`🤖 Testing query: "${query}"`);
const router = new RouterAgent();
const input = createTestInput(query);
const result = await router.execute(input);
expect(result.next_action).toBe(RouterDecisionAction.ASK_CLARIFICATION);
expect(result.reasoning).toBeTruthy();
expect(result.clarification_question).toBeTruthy();
console.warn(`✅ "${query}" → ${result.next_action}`);
console.warn(`🔍 Reasoning: ${result.reasoning}`);
console.warn(`❓ Clarification: ${result.clarification_question}`);
},
15000,
);
});
});
});