( userId?: string, limit = 50, pageCursor?: string, dataset = DATASET, )
| 489 | relabels: Relabel[] |
| 490 | } |
| 491 | export async function getTracesAndAllDataForUser( |
| 492 | userId?: string, |
| 493 | limit = 50, |
| 494 | pageCursor?: string, |
| 495 | dataset = DATASET, |
| 496 | ): Promise<TraceBundle[]> { |
| 497 | const EXTRA_TRACE_TYPES = [ |
| 498 | 'get-expanded-file-context-for-training', |
| 499 | 'get-expanded-file-context-for-training-blobs', |
| 500 | 'grade-run', |
| 501 | ] as const |
| 502 | |
| 503 | /* prettier-ignore */ |
| 504 | const sql = ` |
| 505 | /*──────────────── base (latest N get-relevant-files rows) ─────────────*/ |
| 506 | WITH base AS ( |
| 507 | SELECT id, agent_step_id, user_id, created_at, type, payload |
| 508 | FROM \`${dataset}.${TRACES_TABLE}\` |
| 509 | WHERE ${userId ? 'user_id = @userId AND' : ''} type = 'get-relevant-files' AND JSON_EXTRACT_SCALAR(payload, '$.request_type') = 'Key' |
| 510 | ${pageCursor ? 'AND created_at < @pageCursor' : ''} |
| 511 | ORDER BY created_at DESC |
| 512 | LIMIT @limit |
| 513 | ), |
| 514 | |
| 515 | /*───────────── extra traces that share agent_step_id,user_id ──────────*/ |
| 516 | filtered AS ( |
| 517 | SELECT t.id, t.agent_step_id, t.user_id, t.created_at, t.type, t.payload |
| 518 | FROM \`${dataset}.${TRACES_TABLE}\` AS t |
| 519 | JOIN base AS b |
| 520 | USING (agent_step_id, user_id) |
| 521 | WHERE t.type IN (${EXTRA_TRACE_TYPES.map(t => `'${t}'`).join(', ')}) |
| 522 | ), |
| 523 | |
| 524 | all_rows AS ( |
| 525 | SELECT * FROM base |
| 526 | UNION ALL |
| 527 | SELECT * FROM filtered |
| 528 | ) |
| 529 | |
| 530 | /*────────────────────────── final aggregation ─────────────────────────*/ |
| 531 | SELECT |
| 532 | ANY_VALUE(IF(type='get-relevant-files', t.id, NULL)) AS id, |
| 533 | t.agent_step_id, |
| 534 | ANY_VALUE(IF(type='get-relevant-files', t.user_id, NULL)) AS user_id, |
| 535 | ANY_VALUE(IF(type='get-relevant-files', t.created_at, NULL)) AS created_at, |
| 536 | ANY_VALUE(IF(type='get-relevant-files', t.payload, NULL)) AS payload, |
| 537 | |
| 538 | ARRAY_AGG( |
| 539 | CASE |
| 540 | WHEN type <> 'get-relevant-files' |
| 541 | THEN STRUCT(t.id, t.created_at, t.type, t.payload) |
| 542 | END |
| 543 | IGNORE NULLS |
| 544 | ORDER BY t.created_at |
| 545 | ) AS related_traces, |
| 546 | |
| 547 | ARRAY_AGG(r IGNORE NULLS) AS relabels |
| 548 | FROM all_rows AS t |
no test coverage detected