-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy patheval_gemini.ts
More file actions
348 lines (300 loc) · 9.19 KB
/
eval_gemini.ts
File metadata and controls
348 lines (300 loc) · 9.19 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import fs from 'node:fs';
import path from 'node:path';
import {parseArgs} from 'node:util';
import {
GoogleGenerativeAI,
type FunctionDeclaration,
SchemaType,
} from '@google/generative-ai';
import {Client} from '@modelcontextprotocol/sdk/client/index.js';
import {StdioClientTransport} from '@modelcontextprotocol/sdk/client/stdio.js';
import {TestServer} from '../build/tests/server.js';
const ROOT_DIR = path.resolve(import.meta.dirname, '..');
const SCENARIOS_DIR = path.join(import.meta.dirname, 'eval_scenarios');
// Define schema for our test scenarios
export interface CapturedFunctionCall {
name: string;
args: Record<string, unknown>;
}
export interface TestScenario {
prompt: string;
maxTurns: number;
expectations: (calls: CapturedFunctionCall[]) => void;
htmlRoute?: {
path: string;
htmlContent: string;
};
}
async function loadScenario(scenarioPath: string): Promise<TestScenario> {
const module = await import(scenarioPath);
if (!module.scenario) {
throw new Error(
`Scenario file ${scenarioPath} does not export a 'scenario' object.`,
);
}
return module.scenario;
}
function isRecord(v: unknown): v is Record<string, unknown> {
return typeof v === 'object' && v !== null && !Array.isArray(v);
}
const cleanSchemaRecursive = (schema: unknown): unknown => {
if (!isRecord(schema)) {
return schema;
}
const out: Record<string, unknown> = {};
for (const key in schema) {
if (
key === 'default' ||
key === 'additionalProperties' ||
key === 'exclusiveMinimum'
) {
continue;
}
const value = schema[key];
if (Array.isArray(value)) {
out[key] = value.map(cleanSchemaRecursive);
} else if (isRecord(value)) {
out[key] = cleanSchemaRecursive(value);
} else {
out[key] = value;
}
}
return out;
};
async function runSingleScenario(
scenarioPath: string,
apiKey: string,
server: TestServer,
modelId: string,
debug: boolean,
): Promise<void> {
const debugLog = (...args: unknown[]) => {
if (debug) {
console.log(...args);
}
};
const absolutePath = path.resolve(scenarioPath);
debugLog(
`\n### Running Scenario: ${path.relative(ROOT_DIR, absolutePath)} ###`,
);
let client: Client | undefined;
let transport: StdioClientTransport | undefined;
try {
const scenario = await loadScenario(absolutePath);
if (scenario.htmlRoute) {
server.addHtmlRoute(
scenario.htmlRoute.path,
scenario.htmlRoute.htmlContent,
);
scenario.prompt = scenario.prompt.replace(
'<TEST_URL>',
server.getRoute(scenario.htmlRoute.path),
);
}
// Path to the compiled MCP server
const serverPath = path.join(ROOT_DIR, 'build/src/index.js');
if (!fs.existsSync(serverPath)) {
throw new Error(
`MCP server not found at ${serverPath}. Please run 'npm run build' first.`,
);
}
// Environment variables
const env: Record<string, string> = {};
Object.entries(process.env).forEach(([key, value]) => {
if (value !== undefined) {
env[key] = value;
}
});
const args = [serverPath];
if (!debug) {
args.push('--headless');
}
transport = new StdioClientTransport({
command: 'node',
args,
env,
stderr: debug ? 'inherit' : 'ignore',
});
client = new Client(
{name: 'gemini-eval-client', version: '1.0.0'},
{capabilities: {}},
);
await client.connect(transport);
const toolsResult = await client.listTools();
const mcpTools = toolsResult.tools;
// Convert MCP tools to Gemini function declarations
const functionDeclarations: FunctionDeclaration[] = mcpTools.map(tool => ({
name: tool.name.replace(/-/g, '_').replace(/\./g, '_'), // Sanitize name for Gemini
description: tool.description?.substring(0, 1024) || '',
parameters: cleanSchemaRecursive({
type: SchemaType.OBJECT,
properties:
isRecord(tool.inputSchema) && 'properties' in tool.inputSchema
? tool.inputSchema.properties
: {},
required:
isRecord(tool.inputSchema) &&
'required' in tool.inputSchema &&
Array.isArray(tool.inputSchema.required)
? tool.inputSchema.required
: [],
}) as FunctionDeclaration['parameters'],
}));
// Keep a map of sanitized names to original names for execution
const contentToolsMap = new Map<string, string>();
for (const tool of mcpTools) {
const sanitized = tool.name.replace(/-/g, '_').replace(/\./g, '_');
contentToolsMap.set(sanitized, tool.name);
}
const genAI = new GoogleGenerativeAI(apiKey);
const model = genAI.getGenerativeModel({
model: modelId,
tools: [{functionDeclarations}],
});
const chat = model.startChat({
systemInstruction: {
role: 'system',
parts: [{text: `Use available tools.`}],
},
});
const expectations = scenario.expectations;
const allCalls: CapturedFunctionCall[] = [];
// Execute turns
let turnCount = 0;
debugLog(`\n--- Turn 1 (User) ---`);
debugLog(scenario.prompt);
let result = await chat.sendMessage(scenario.prompt, {
timeout: 5000,
});
let response = result.response;
while (turnCount < scenario.maxTurns) {
turnCount++;
debugLog(`\n--- Turn ${turnCount} (Model) ---`);
const text = response.text();
if (text) {
debugLog(`Text: ${text}`);
}
const functionCalls = response.functionCalls();
if (functionCalls && functionCalls.length > 0) {
debugLog(`Function Calls: ${JSON.stringify(functionCalls, null, 2)}`);
const functionResponses = [];
for (const call of functionCalls) {
const originalName = contentToolsMap.get(call.name);
if (!originalName) {
console.error(`Unknown tool called: ${call.name}`);
functionResponses.push({
functionResponse: {
name: call.name,
response: {error: `Unknown tool: ${call.name}`},
},
});
continue;
}
const safeArgs = isRecord(call.args) ? call.args : {};
debugLog(
`Executing tool: ${originalName} with args: ${JSON.stringify(call.args)}`,
);
allCalls.push({
name: originalName,
args: safeArgs,
});
try {
const toolResult = await client.callTool({
name: originalName,
arguments: safeArgs,
});
functionResponses.push({
functionResponse: {
name: call.name,
response: {name: call.name, content: toolResult},
},
});
} catch (e) {
const errorMessage = e instanceof Error ? e.message : String(e);
console.error(`Error executing tool ${originalName}:`, e);
functionResponses.push({
functionResponse: {
name: call.name,
response: {error: errorMessage},
},
});
}
}
// Send tool results back
debugLog(`Sending ${functionResponses.length} tool outputs back...`);
result = await chat.sendMessage(functionResponses);
response = result.response;
} else {
debugLog('No tool calls. Interaction finished.');
break;
}
}
debugLog('\nVerifying expectations...');
expectations(allCalls);
} finally {
await client?.close();
await transport?.close();
}
}
async function main() {
const apiKey = process.env.GEMINI_API_KEY;
if (!apiKey) {
throw new Error('GEMINI_API_KEY environment variable is required.');
}
const {values, positionals} = parseArgs({
options: {
model: {
type: 'string',
default: 'gemini-2.5-flash',
},
debug: {
type: 'boolean',
default: false,
},
},
allowPositionals: true,
});
const modelId = values.model;
const debug = values.debug;
const scenarioFiles =
positionals.length > 0
? positionals.map(p => path.resolve(p))
: fs
.readdirSync(SCENARIOS_DIR)
.filter(file => file.endsWith('.ts') || file.endsWith('.js'))
.map(file => path.join(SCENARIOS_DIR, file));
const server = new TestServer(TestServer.randomPort());
await server.start();
let successCount = 0;
let failureCount = 0;
try {
for (const scenarioPath of scenarioFiles) {
try {
await runSingleScenario(scenarioPath, apiKey, server, modelId, debug);
console.log(`✔ ${path.relative(ROOT_DIR, scenarioPath)}`);
successCount++;
} catch (e) {
console.error(`✖ ${path.relative(ROOT_DIR, scenarioPath)}`);
console.error(e);
failureCount++;
} finally {
server.restore();
}
}
} finally {
await server.stop();
}
console.log(`\nSummary: ${successCount} passed, ${failureCount} failed`);
if (failureCount > 0) {
process.exit(1);
}
}
main().catch(error => {
console.error('Fatal error:', error);
process.exit(1);
});