| 375 | } |
| 376 | |
| 377 | async function fetchRepresentativeRows(args: Args, messageIds: string[]) { |
| 378 | if (messageIds.length === 0) return new Map<string, SessionRow>() |
| 379 | |
| 380 | const query = ` |
| 381 | SELECT |
| 382 | id, |
| 383 | finished_at, |
| 384 | JSON_VALUE(request, '$.codebuff_metadata.run_id') AS run_id, |
| 385 | ARRAY_LENGTH(JSON_QUERY_ARRAY(request, '$.messages')) AS message_count, |
| 386 | request AS request_json, |
| 387 | response, |
| 388 | reasoning_text |
| 389 | FROM \`${args.dataset}.message\` |
| 390 | WHERE id IN UNNEST(@messageIds) |
| 391 | AND JSON_VALUE(request, '$.codebuff_metadata.cost_mode') = 'free' |
| 392 | AND JSON_QUERY_ARRAY(request, '$.messages') IS NOT NULL |
| 393 | AND COALESCE(JSON_VALUE(request, '$.messages_omitted'), 'false') != 'true' |
| 394 | ` |
| 395 | |
| 396 | const [rows] = await new BigQuery().query({ |
| 397 | query, |
| 398 | params: { messageIds }, |
| 399 | }) |
| 400 | |
| 401 | return new Map((rows as SessionRow[]).map((row) => [row.id, row])) |
| 402 | } |
| 403 | |
| 404 | function buildTrace( |
| 405 | candidate: CandidateRow, |