-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy patheval-protocol.ts
More file actions
632 lines (600 loc) · 19.1 KB
/
eval-protocol.ts
File metadata and controls
632 lines (600 loc) · 19.1 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
import { z } from "zod";
// Base schemas
export const ChatCompletionContentPartTextParamSchema = z.object({
text: z.string().describe("The text content."),
type: z
.literal("text")
.default("text")
.describe("The type of the content part."),
});
export const ChatCompletionContentPartImageParamSchema = z.object({
type: z
.literal("image_url")
.default("image_url")
.describe("The type of the content part."),
image_url: z
.object({
url: z.string().describe("Image URL or data URI."),
detail: z.enum(["auto", "low", "high"]).optional(),
})
.or(z.record(z.string(), z.any()))
.describe("Image descriptor payload."),
});
export const ChatCompletionContentPartParamSchema = z.union([
ChatCompletionContentPartTextParamSchema,
ChatCompletionContentPartImageParamSchema,
]);
export const FunctionCallSchema = z.object({
name: z.string(),
arguments: z.string(),
});
export const ChatCompletionMessageToolCallSchema = z.object({
id: z.string(),
type: z.literal("function"),
function: FunctionCallSchema,
});
export const MessageSchema = z.object({
role: z.string().describe("assistant, user, system, tool"),
content: z
.union([z.string(), z.array(ChatCompletionContentPartParamSchema)])
.optional()
.default("")
.describe("The content of the message."),
reasoning_content: z
.string()
.optional()
.describe("Optional hidden chain-of-thought or reasoning content."),
name: z.string().optional(),
tool_call_id: z.string().optional(),
tool_calls: z.array(ChatCompletionMessageToolCallSchema).optional(),
function_call: FunctionCallSchema.optional(),
control_plane_step: z.record(z.string(), z.any()).optional(),
});
export const MetricResultSchema = z.object({
is_score_valid: z.boolean().default(true),
score: z.number().min(0.0).max(1.0),
reason: z.string(),
});
export const StepOutputSchema = z.object({
step_index: z
.union([z.number(), z.string()])
.describe(
"User-defined index for the step (e.g., assistant message index, turn number). This is used by the system to map this output to the internal StepData."
),
base_reward: z
.number()
.describe(
"Base reward calculated by the user's reward function for this step."
),
terminated: z
.boolean()
.default(false)
.describe("Whether the environment signaled termination at this step."),
control_plane_info: z
.record(z.string(), z.any())
.optional()
.describe("Structured info from the environment's control plane."),
metrics: z
.record(z.string(), z.any())
.default({})
.describe("Optional dictionary of custom metrics for this step."),
reason: z
.string()
.optional()
.describe("Optional explanation for the step's base reward or metrics."),
});
export const EvaluateResultSchema = z.object({
score: z
.number()
.describe("The overall evaluation score, typically between 0.0 and 1.0."),
is_score_valid: z
.boolean()
.default(true)
.describe("Whether the overall score is valid."),
reason: z
.string()
.optional()
.describe("Optional explanation for the overall score."),
metrics: z
.record(z.string(), MetricResultSchema)
.default({})
.describe("Dictionary of component metrics for detailed breakdown."),
step_outputs: z
.array(StepOutputSchema)
.optional()
.describe(
"For RL, a list of outputs for each conceptual step, providing base rewards."
),
error: z
.string()
.optional()
.describe(
"Optional error message if the evaluation itself encountered an issue."
),
trajectory_info: z
.record(z.string(), z.any())
.optional()
.describe(
"Additional trajectory-level information (duration, steps, termination_reason, etc.)."
),
final_control_plane_info: z
.record(z.string(), z.any())
.optional()
.describe("The final control plane state that led to termination."),
agg_score: z
.number()
.optional()
.describe("The aggregated score of the evaluation across all runs."),
standard_error: z
.number()
.optional()
.describe("The standard error of the evaluation across all runs."),
});
export const CompletionParamsSchema = z.record(z.string(), z.any());
// AIP-193 ErrorInfo model for structured error details
export const ErrorInfoSchema = z.object({
reason: z
.string()
.describe("Short snake_case description of the error cause"),
domain: z.string().describe("Logical grouping for the error reason"),
metadata: z
.record(z.string(), z.any())
.default({})
.describe("Additional dynamic information as context"),
});
// AIP-193 compatible Status model (matches Python Status)
export const StatusCodeSchema = z
.enum([
"OK",
"CANCELLED",
"UNKNOWN",
"INVALID_ARGUMENT",
"DEADLINE_EXCEEDED",
"NOT_FOUND",
"ALREADY_EXISTS",
"PERMISSION_DENIED",
"RESOURCE_EXHAUSTED",
"FAILED_PRECONDITION",
"ABORTED",
"OUT_OF_RANGE",
"UNIMPLEMENTED",
"INTERNAL",
"UNAVAILABLE",
"DATA_LOSS",
"UNAUTHENTICATED",
"FINISHED",
"RUNNING",
"SCORE_INVALID",
])
.describe("Common gRPC status codes as defined in google.rpc.Code");
// Mapping from integer status codes to their corresponding code names
export const STATUS_CODE_MAP: Record<number, StatusCode> = {
0: "OK",
1: "CANCELLED",
2: "UNKNOWN",
3: "INVALID_ARGUMENT",
4: "DEADLINE_EXCEEDED",
5: "NOT_FOUND",
6: "ALREADY_EXISTS",
7: "PERMISSION_DENIED",
8: "RESOURCE_EXHAUSTED",
9: "FAILED_PRECONDITION",
10: "ABORTED",
11: "OUT_OF_RANGE",
12: "UNIMPLEMENTED",
13: "INTERNAL",
14: "UNAVAILABLE",
15: "DATA_LOSS",
16: "UNAUTHENTICATED",
100: "FINISHED",
101: "RUNNING",
102: "SCORE_INVALID",
} as const;
// Helper function to get status code name from integer
export const getStatusCodeName = (code: number): StatusCode => {
return STATUS_CODE_MAP[code] || "UNKNOWN";
};
export const StatusSchema = z.object({
code: z
.number()
.describe("The status code (numeric value from google.rpc.Code enum)"),
message: z
.string()
.describe("Developer-facing, human-readable debug message in English"),
details: z
.array(z.record(z.string(), z.any()))
.default([])
.describe(
"Additional error information, each packed in a google.protobuf.Any message format"
),
});
// Evaluation threshold configuration
export const EvaluationThresholdSchema = z.object({
success: z
.number()
.min(0.0)
.max(1.0)
.describe(
"Minimum success rate threshold (fraction of total score, 0.0 to 1.0)"
),
standard_error: z
.number()
.min(0.0)
.max(1.0)
.optional()
.describe(
"Maximum standard error threshold (fraction of total score, 0.0 to 1.0)"
),
});
export const InputMetadataSchema = z
.object({
row_id: z
.string()
.optional()
.describe(
"Unique string to ID the row. If not provided, a stable hash will be generated based on the row's content. The hash removes fields that are not typically stable across processes such as created_at, execution_metadata, and pid."
),
completion_params: CompletionParamsSchema.describe(
"Completion endpoint parameters used"
),
dataset_info: z
.record(z.string(), z.any())
.optional()
.describe(
"Dataset row details: seed, system_prompt, environment_context, etc"
),
session_data: z
.record(z.string(), z.any())
.optional()
.describe(
"Session metadata like timestamp (input only, no duration/usage)"
),
})
.loose(); // equivalent to extra="allow" in Pydantic
export const CompletionUsageSchema = z.object({
prompt_tokens: z.number(),
completion_tokens: z.number(),
total_tokens: z.number(),
});
export const EvalMetadataSchema = z.object({
name: z.string().describe("Name of the evaluation"),
description: z.string().optional().describe("Description of the evaluation"),
version: z
.string()
.describe(
"Version of the evaluation. Should be populated with a PEP 440 version string."
),
status: StatusSchema.optional().describe("Status of the evaluation"),
num_runs: z
.number()
.int()
.describe("Number of times the evaluation was repeated"),
aggregation_method: z
.string()
.describe("Method used to aggregate scores across runs"),
passed_threshold: EvaluationThresholdSchema.optional().describe(
"Threshold configuration for test success"
),
passed: z
.boolean()
.optional()
.describe("Whether the evaluation passed based on the threshold"),
});
export const CostMetricsSchema = z.object({
input_cost: z
.number()
.nullable()
.optional()
.describe("Cost in USD for input tokens."),
output_cost: z
.number()
.nullable()
.optional()
.describe("Cost in USD for output tokens."),
total_cost_dollar: z
.number()
.nullable()
.optional()
.describe("Total cost in USD for the API call."),
});
export const ExecutionMetadataSchema = z.object({
rollout_start_time: z
.preprocess(
(val) => (typeof val === "string" ? new Date(val) : val),
z.date()
)
.optional()
.describe("UTC timestamp when the rollout started."),
invocation_id: z
.string()
.optional()
.describe("The ID of the invocation that this row belongs to."),
experiment_id: z
.string()
.optional()
.describe("The ID of the experiment that this row belongs to."),
rollout_id: z
.string()
.optional()
.describe("The ID of the rollout that this row belongs to."),
run_id: z
.string()
.optional()
.describe("The ID of the run that this row belongs to."),
usage: CompletionUsageSchema.optional().describe(
"Token usage statistics from LLM calls during execution."
),
cost_metrics: CostMetricsSchema.optional().describe(
"Cost breakdown for LLM API calls."
),
duration_seconds: z
.number()
.nullable()
.optional()
.describe("Processing duration in seconds for this evaluation row."),
rollout_duration_seconds: z
.number()
.nullable()
.optional()
.describe("Processing duration in seconds for the rollout of this row."),
eval_duration_seconds: z
.number()
.nullable()
.optional()
.describe("Processing duration in seconds for the evaluation of this row."),
experiment_duration_seconds: z
.number()
.nullable()
.optional()
.describe("Processing duration in seconds for an entire experiment."),
});
export const EvaluationRowSchema = z.object({
messages: z
.array(MessageSchema)
.describe("List of messages in the conversation/trajectory."),
tools: z
.array(z.record(z.string(), z.any()))
.optional()
.describe("Available tools/functions that were provided to the agent."),
input_metadata: InputMetadataSchema.describe(
"Metadata related to the input (dataset info, model config, session data, etc.)."
),
rollout_status: StatusSchema.describe(
"The status of the rollout following AIP-193 standards."
),
execution_metadata: ExecutionMetadataSchema.optional().describe(
"Metadata about the execution of the evaluation."
),
ground_truth: z
.union([
z.string(),
z.number(),
z.boolean(),
z.array(z.any()),
z.record(z.string(), z.any()),
])
.nullable()
.optional()
.describe("JSON-serializable ground truth reference for this evaluation."),
evaluation_result: EvaluateResultSchema.optional().describe(
"The evaluation result for this row/trajectory."
),
created_at: z
.preprocess(
(val) => (typeof val === "string" ? new Date(val) : val),
z.date()
)
.describe(
"The timestamp when the row was created. Accepts string and parses to Date."
),
eval_metadata: EvalMetadataSchema.optional().describe(
"Metadata about the evaluation that was run."
),
pid: z
.number()
.optional()
.describe(
"The PID of the process that created the row. This is used by the evaluation watcher to detect stopped evaluations."
),
});
// Agent Evaluation Framework (V2) schemas
export const ResourceServerConfigSchema = z.object({
start_command: z
.string()
.describe(
"The command to start the server. The string '{port}' will be replaced with a dynamically allocated free port."
),
health_check_url: z
.string()
.describe(
"The URL to poll to check if the server is ready. The string '{port}' will be replaced with the allocated port."
),
});
export const EvaluationCriteriaModelSchema = z.object({
final_state_query: z
.string()
.optional()
.describe("A query (e.g., SQL) to run on the final state of the resource."),
expected_query_result_transform: z
.string()
.optional()
.describe(
"A Python lambda string (e.g., 'lambda x: x > 0') to transform and evaluate the query result to a boolean."
),
ground_truth_function_calls: z
.array(z.array(z.string()))
.optional()
.describe("Ground truth function calls for BFCL evaluation."),
ground_truth_comparable_state: z
.record(z.string(), z.any())
.optional()
.describe("Ground truth comparable state for BFCL evaluation."),
});
export const TaskDefinitionModelSchema = z
.object({
name: z.string().describe("Unique name for the task."),
description: z
.string()
.optional()
.describe("A brief description of the task."),
resource_type: z
.string()
.describe(
"The type of ForkableResource to use (e.g., 'SQLResource', 'PythonStateResource', 'FileSystemResource', 'DockerResource')."
),
base_resource_config: z
.record(z.string(), z.any())
.default({})
.describe(
"Configuration dictionary passed to the base resource's setup() method."
),
tools_module_path: z
.string()
.optional()
.describe(
"Optional Python import path to a module containing custom tool functions for this task."
),
reward_function_path: z
.string()
.describe(
"Python import path to the reward function (e.g., 'my_module.my_reward_func')."
),
goal_description: z
.string()
.optional()
.describe(
"A human-readable description of the agent's goal for this task."
),
evaluation_criteria: EvaluationCriteriaModelSchema.optional().describe(
"Criteria used by the Orchestrator to determine if the primary goal was achieved."
),
initial_user_prompt: z
.string()
.optional()
.describe(
"The initial prompt or message to start the agent interaction. Deprecated if 'messages' field is used for multi-turn."
),
messages: z
.array(z.record(z.string(), z.any()))
.optional()
.describe(
"A list of messages to start the conversation, can represent multiple user turns for sequential processing."
),
poc_max_turns: z
.number()
.int()
.min(1)
.default(3)
.describe(
"For PoC Orchestrator, the maximum number of interaction turns."
),
resource_server: ResourceServerConfigSchema.optional().describe(
"Configuration for a background server required for the task."
),
num_rollouts: z
.number()
.int()
.min(1)
.default(1)
.describe(
"Number of parallel rollouts to execute for this task definition."
),
dataset_path: z
.string()
.optional()
.describe(
"Path to dataset file (JSONL) containing experimental conditions for data-driven evaluation."
),
num_rollouts_per_sample: z
.number()
.int()
.min(1)
.default(1)
.describe("Number of rollouts to execute per sample from the dataset."),
})
.loose(); // equivalent to extra="allow" in Pydantic
// MCP Configuration schemas
export const MCPConfigurationServerStdioSchema = z.object({
command: z.string().describe("command to run the MCP server"),
args: z.array(z.string()).default([]).describe("to pass to the command"),
env: z
.array(z.string())
.default([])
.describe(
"List of environment variables to verify exist in the environment"
),
});
export const MCPConfigurationServerUrlSchema = z.object({
url: z.string().describe("url to the MCP server"),
});
export const MCPMultiClientConfigurationSchema = z.object({
mcpServers: z.record(
z.string(),
z.union([
MCPConfigurationServerStdioSchema,
MCPConfigurationServerUrlSchema,
])
),
});
// Export TypeScript types derived from the schemas
export type ChatCompletionContentPartTextParam = z.infer<
typeof ChatCompletionContentPartTextParamSchema
>;
export type ChatCompletionContentPartImageParam = z.infer<
typeof ChatCompletionContentPartImageParamSchema
>;
export type ChatCompletionContentPartParam = z.infer<
typeof ChatCompletionContentPartParamSchema
>;
export type Message = z.infer<typeof MessageSchema>;
export type MetricResult = z.infer<typeof MetricResultSchema>;
export type StepOutput = z.infer<typeof StepOutputSchema>;
export type EvaluateResult = z.infer<typeof EvaluateResultSchema>;
export type CompletionParams = z.infer<typeof CompletionParamsSchema>;
export type InputMetadata = z.infer<typeof InputMetadataSchema>;
export type CompletionUsage = z.infer<typeof CompletionUsageSchema>;
export type EvalMetadata = z.infer<typeof EvalMetadataSchema>;
export type EvaluationRow = z.infer<typeof EvaluationRowSchema>;
export type Status = z.infer<typeof StatusSchema>;
export type StatusCode = z.infer<typeof StatusCodeSchema>;
export type ErrorInfo = z.infer<typeof ErrorInfoSchema>;
export type EvaluationThreshold = z.infer<typeof EvaluationThresholdSchema>;
export type ResourceServerConfig = z.infer<typeof ResourceServerConfigSchema>;
export type EvaluationCriteriaModel = z.infer<
typeof EvaluationCriteriaModelSchema
>;
export type TaskDefinitionModel = z.infer<typeof TaskDefinitionModelSchema>;
export type MCPConfigurationServerStdio = z.infer<
typeof MCPConfigurationServerStdioSchema
>;
export type MCPConfigurationServerUrl = z.infer<
typeof MCPConfigurationServerUrlSchema
>;
export type MCPMultiClientConfiguration = z.infer<
typeof MCPMultiClientConfigurationSchema
>;
// Log-related schemas
export const LogEntrySchema = z.object({
"@timestamp": z.string().describe("ISO 8601 timestamp of the log entry"),
level: z.string().describe("Log level (DEBUG, INFO, WARNING, ERROR)"),
message: z.string().describe("The log message"),
logger_name: z
.string()
.describe("Name of the logger that created this entry"),
rollout_id: z.string().describe("ID of the rollout this log belongs to"),
status_code: z.number().optional().describe("Optional status code"),
status_message: z.string().optional().describe("Optional status message"),
status_details: z
.array(z.any())
.optional()
.describe("Optional status details"),
});
export const LogsResponseSchema = z.object({
logs: z.array(LogEntrySchema),
total: z.number().describe("Total number of logs available"),
rollout_id: z.string().describe("The rollout ID these logs belong to"),
filtered_by_level: z.string().optional().describe("Log level filter applied"),
});
// Type exports
export type LogEntry = z.infer<typeof LogEntrySchema>;
export type LogsResponse = z.infer<typeof LogsResponseSchema>;