-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy patheval-runner.ts
More file actions
411 lines (377 loc) · 11.8 KB
/
eval-runner.ts
File metadata and controls
411 lines (377 loc) · 11.8 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**
* PromptFoo eval runner for Studio Code agent.
*
* Hooks into startAiAgent() to capture tool calls, tool results, and
* assistant text. Returns raw structured data — assertions live in the
* promptfoo config, not here.
*/
import { writeFileSync, writeSync as fsWriteSync } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { isAiModelId, type AiModelId } from '@studio/common/ai/models';
import { startAiAgent } from 'cli/ai/agent';
import {
resolveAiEnvironment,
resolveInitialAiProvider,
resolveUnavailableAiProvider,
} from 'cli/ai/auth';
import type { AiProviderId } from 'cli/ai/providers';
import type { SDKMessage } from 'cli/ai/runtimes/messages';
interface EvalRunnerInput {
prompt: string;
timeoutMs?: number;
model?: AiModelId;
}
function normalizeToolName( name: string ): string {
return name.replace( /^mcp__studio__/, '' );
}
function extractToolCalls( message: SDKMessage ) {
if ( message.type !== 'assistant' ) {
return [];
}
const content = message.message.content ?? [];
return content
.filter(
( block ): block is Extract< ( typeof content )[ number ], { type: 'tool_use' } > =>
block.type === 'tool_use'
)
.map( ( block ) => ( {
id: block.id,
name: normalizeToolName( block.name ),
input: block.input,
} ) );
}
type TextBlock = { type: 'text'; text: string };
type ToolResultBlock = {
type: 'tool_result';
tool_use_id: string;
is_error?: boolean;
content?: unknown;
};
type ToolCallRecord = {
id: string;
name: string;
input: unknown;
};
type ToolEvent = {
toolUseId: string;
toolName: string;
input: unknown;
startedAtMs: number;
endedAtMs?: number;
durationMs?: number;
isError?: boolean;
turnIndex: number;
};
type FirstToolError = {
toolUseId: string | null;
toolName: string | null;
input?: unknown;
error: string;
turnIndex: number;
};
type TranscriptEvent = {
index: number;
type: SDKMessage[ 'type' ];
turnIndex: number;
elapsedMs: number;
text?: string[];
toolCalls?: ToolCallRecord[];
toolResult?: {
toolUseId: string | null;
toolName: string | null;
isError: boolean;
text?: string;
};
stopReason?: string | null;
result?: string;
subtype?: string;
isError?: boolean;
errors?: string[];
numTurns?: number;
};
function truncateText( value: string, maxLength = 4000 ): string {
return value.length > maxLength
? `${ value.slice( 0, maxLength ) }…[truncated ${ value.length - maxLength } chars]`
: value;
}
function extractTextSegments( message: SDKMessage ): string[] {
if ( message.type !== 'assistant' ) {
return [];
}
const content = ( message.message.content ?? [] ) as Array< { type: string } >;
return content
.filter( ( block ): block is TextBlock => block.type === 'text' )
.map( ( block ) => block.text );
}
function extractToolResult( message: SDKMessage ): {
toolUseId: string | null;
isError: boolean;
text?: string;
} | null {
if ( message.type !== 'user' || ! Array.isArray( message.message.content ) ) {
return null;
}
const content = message.message.content as Array< { type: string } >;
const block = content.find( ( b ): b is ToolResultBlock => b.type === 'tool_result' );
if ( ! block ) {
return null;
}
let text: string | undefined;
if ( typeof block.content === 'string' ) {
text = block.content;
} else if ( Array.isArray( block.content ) ) {
const tb = ( block.content as Array< { type: string } > ).find(
( b ): b is TextBlock => b.type === 'text'
);
if ( tb ) {
text = tb.text;
}
}
return { toolUseId: block.tool_use_id ?? null, isError: block.is_error === true, text };
}
function readInput(): EvalRunnerInput {
const prompt = process.argv[ 2 ];
if ( ! prompt ) {
throw new Error( 'Missing prompt argument' );
}
let vars: Record< string, unknown > = {};
if ( process.argv[ 4 ] ) {
try {
vars = JSON.parse( process.argv[ 4 ] )?.vars ?? {};
} catch {
// ignore
}
}
const envModel = process.env.STUDIO_EVAL_MODEL?.trim();
const varModel = typeof vars.model === 'string' ? vars.model.trim() : undefined;
const rawModel = varModel || envModel;
const model = rawModel && isAiModelId( rawModel ) ? rawModel : undefined;
return {
prompt: ( vars.prompt as string ) ?? prompt,
timeoutMs: typeof vars.timeoutMs === 'number' ? vars.timeoutMs : undefined,
model,
};
}
async function runEval( input: EvalRunnerInput ) {
const evalStartedAt = Date.now();
const elapsed = () => Date.now() - evalStartedAt;
const phaseTimingsMs: Record< string, number > = {};
let phaseStartedAt = Date.now();
let aiProvider: AiProviderId = await resolveInitialAiProvider();
phaseTimingsMs.resolve_initial_provider_ms = Date.now() - phaseStartedAt;
phaseStartedAt = Date.now();
aiProvider = ( await resolveUnavailableAiProvider( aiProvider ) ) ?? aiProvider;
phaseTimingsMs.resolve_unavailable_provider_ms = Date.now() - phaseStartedAt;
phaseStartedAt = Date.now();
const aiEnvironment = await resolveAiEnvironment( aiProvider );
phaseTimingsMs.resolve_ai_environment_ms = Date.now() - phaseStartedAt;
const env = {
...( process.env as Record< string, string > ),
...aiEnvironment,
};
// Allow running inside a Claude Code session
delete env.CLAUDECODE;
const toolCalls: ToolCallRecord[] = [];
const toolResults: {
toolUseId: string | null;
toolName: string | null;
isError: boolean;
text?: string;
}[] = [];
const toolEvents: ToolEvent[] = [];
const textSegments: string[] = [];
const transcript: TranscriptEvent[] = [];
const includeTranscript = process.env.STUDIO_EVAL_INCLUDE_TRANSCRIPT === '1';
const toolNameById = new Map< string, string >();
const toolEventById = new Map< string, ToolEvent >();
let firstToolError: FirstToolError | null = null;
// Wall-clock per turn, measured between successive assistant messages.
const turnDurationsMs: number[] = [];
let turnIndex = 0;
let numTurns: number | null = null;
let success = false;
let error: string | null = null;
let timedOut = false;
let resultStopReason: string | null = null;
let resultText = '';
let resultSubtype: string | null = null;
let resultIsError = false;
let resultErrors: string[] = [];
let messageIndex = 0;
phaseStartedAt = Date.now();
const query = startAiAgent( {
prompt: input.prompt.trim(),
env,
...( input.model ? { model: input.model } : {} ),
} );
phaseTimingsMs.start_ai_agent_ms = Date.now() - phaseStartedAt;
const queryStartedAt = Date.now();
let turnStart = queryStartedAt;
const timeout = setTimeout( () => {
timedOut = true;
void query.interrupt();
}, input.timeoutMs ?? 300000 );
try {
for await ( const message of query ) {
messageIndex += 1;
const event: TranscriptEvent = {
index: messageIndex,
type: message.type,
turnIndex,
elapsedMs: elapsed(),
};
if ( message.type === 'assistant' ) {
const now = Date.now();
turnDurationsMs.push( now - turnStart );
turnIndex += 1;
event.turnIndex = turnIndex;
if ( turnIndex === 1 ) {
phaseTimingsMs.first_assistant_message_ms = now - queryStartedAt;
}
turnStart = now;
event.stopReason = message.message.stop_reason ?? null;
}
const messageToolCalls = extractToolCalls( message );
if ( messageToolCalls.length ) {
event.toolCalls = messageToolCalls;
}
for ( const tc of messageToolCalls ) {
toolCalls.push( tc );
toolNameById.set( tc.id, tc.name );
const toolEvent: ToolEvent = {
toolUseId: tc.id,
toolName: tc.name,
input: tc.input,
startedAtMs: elapsed(),
turnIndex,
};
toolEvents.push( toolEvent );
toolEventById.set( tc.id, toolEvent );
}
const messageTextSegments = extractTextSegments( message );
if ( messageTextSegments.length ) {
event.text = messageTextSegments.map( ( text ) => truncateText( text ) );
}
textSegments.push( ...messageTextSegments );
if ( message.type === 'user' ) {
const tr = extractToolResult( message );
if ( tr ) {
const id = tr.toolUseId ?? message.parent_tool_use_id ?? null;
const toolEvent = id ? toolEventById.get( id ) : undefined;
if ( toolEvent ) {
toolEvent.endedAtMs = elapsed();
toolEvent.durationMs = toolEvent.endedAtMs - toolEvent.startedAtMs;
toolEvent.isError = tr.isError;
}
event.toolResult = {
toolUseId: id,
toolName: id ? toolNameById.get( id ) ?? null : null,
isError: tr.isError,
...( tr.text ? { text: truncateText( tr.text ) } : {} ),
};
if ( tr.isError && ! firstToolError ) {
firstToolError = {
toolUseId: id,
toolName: id ? toolNameById.get( id ) ?? null : null,
...( toolEvent?.input ? { input: toolEvent.input } : {} ),
error: tr.text ?? 'Tool returned an error result.',
turnIndex,
};
}
toolResults.push( {
toolUseId: id,
toolName: id ? toolNameById.get( id ) ?? null : null,
isError: tr.isError,
...( tr.text ? { text: tr.text } : {} ),
} );
}
}
if ( message.type === 'result' ) {
success = message.subtype === 'success';
numTurns = message.num_turns ?? null;
resultStopReason = message.stop_reason ?? null;
resultText = message.result ?? '';
resultSubtype = message.subtype ?? null;
resultIsError = message.is_error === true;
resultErrors = Array.isArray( message.errors ) ? message.errors : [];
event.subtype = message.subtype;
event.isError = message.is_error;
event.stopReason = message.stop_reason ?? null;
event.result = truncateText( message.result ?? '' );
event.numTurns = message.num_turns;
if ( resultErrors.length ) {
event.errors = resultErrors;
}
}
if ( includeTranscript ) {
transcript.push( event );
}
}
} catch ( caught ) {
error = caught instanceof Error ? caught.message : String( caught );
} finally {
clearTimeout( timeout );
}
phaseTimingsMs.total_eval_ms = elapsed();
if ( success && ! textSegments.length && ! resultText.trim() ) {
success = false;
error = 'Agent completed without assistant text or result output.';
}
return {
success,
error,
timedOut,
numTurns,
resultSubtype,
resultIsError,
resultStopReason,
resultText,
resultErrors,
phaseTimingsMs,
turnDurationsMs,
toolCalls,
toolResults,
toolEvents,
firstToolError,
textSegments,
...( includeTranscript ? { transcript } : {} ),
};
}
const RESULT_PREFIX = 'EVAL_RUNNER_RESULT_FILE=';
// Studio tools and the Agent SDK freely print to stdout (pi-tui spinners,
// daemon status, …). promptfoo's `exec:` provider wraps us in
// `child_process.exec`, whose default 1 MB stdout buffer long runs overflow.
// Redirect stdout writes to stderr during the run, serialize the result to a
// tmp file, and emit only `EVAL_RUNNER_RESULT_FILE=<path>` via a raw
// `fs.writeSync(1, …)` that bypasses the wrapper.
async function main() {
const filePath = path.join( os.tmpdir(), `studio-eval-${ Date.now() }-${ process.pid }.json` );
( process.stdout as unknown as { write: ( ...args: unknown[] ) => boolean } ).write = (
...args: unknown[]
) => {
return ( process.stderr.write as unknown as ( ...args: unknown[] ) => boolean )( ...args );
};
const rawStdout = ( line: string ) => fsWriteSync( 1, line );
const emit = ( payload: unknown ) => {
try {
writeFileSync( filePath, JSON.stringify( payload ) );
rawStdout( `${ RESULT_PREFIX }${ filePath }` );
} catch ( writeError ) {
const msg = writeError instanceof Error ? writeError.message : String( writeError );
process.stderr.write( `[eval-runner] failed to write ${ filePath }: ${ msg }\n` );
rawStdout( JSON.stringify( { success: false, error: msg } ) );
}
};
let exitCode = 0;
try {
emit( await runEval( readInput() ) );
} catch ( error ) {
emit( { success: false, error: error instanceof Error ? error.message : String( error ) } );
exitCode = 1;
}
// The Agent SDK keeps internal handles open past conversation end; bail out
// rather than leaving promptfoo waiting on its exec child.
process.exit( exitCode );
}
void main();