(params: {
trace: GetRelevantFilesTrace
fileBlobs: GetExpandedFileContextForTrainingBlobTrace
promptContext: PromptContext
})
| 331 | } |
| 332 | |
| 333 | async function relabelWithRelace(params: { |
| 334 | trace: GetRelevantFilesTrace |
| 335 | fileBlobs: GetExpandedFileContextForTrainingBlobTrace |
| 336 | promptContext: PromptContext |
| 337 | }) { |
| 338 | const { trace, fileBlobs, promptContext } = params |
| 339 | logger.info(`Relabeling ${trace.id} with LLM reranker`) |
| 340 | |
| 341 | const filesWithPath = Object.entries(fileBlobs.payload.files).map( |
| 342 | ([path, file]) => ({ |
| 343 | path, |
| 344 | content: file.content, |
| 345 | }), |
| 346 | ) |
| 347 | |
| 348 | const query = extractQueryFromMessages(trace.payload.messages) |
| 349 | const prompt = [ |
| 350 | `A developer asked: "${query}".`, |
| 351 | 'Rank the following files from most relevant to least relevant for answering the request.', |
| 352 | 'Return only the file paths, one per line, and only include files from the list below.', |
| 353 | filesWithPath.map((file) => `- ${file.path}`).join('\n'), |
| 354 | ].join('\n\n') |
| 355 | |
| 356 | const ranked = unwrapPromptResult( |
| 357 | await promptAiSdk({ |
| 358 | ...promptContext, |
| 359 | model: models.openrouter_claude_sonnet_4, |
| 360 | messages: [userMessage(prompt)], |
| 361 | includeCacheControl: false, |
| 362 | }), |
| 363 | ) |
| 364 | |
| 365 | const rankedFiles = |
| 366 | ranked |
| 367 | .split('\n') |
| 368 | .map((line: string) => line.trim()) |
| 369 | .filter((line: string) => line.length > 0) || [] |
| 370 | |
| 371 | const output = |
| 372 | rankedFiles.length > 0 |
| 373 | ? rankedFiles.join('\n') |
| 374 | : filesWithPath.map((file) => file.path).join('\n') |
| 375 | |
| 376 | const relabel: Relabel = { |
| 377 | id: generateCompactId(), |
| 378 | agent_step_id: trace.agent_step_id, |
| 379 | user_id: trace.user_id, |
| 380 | created_at: new Date(), |
| 381 | model: 'relace-ranker', |
| 382 | payload: { |
| 383 | user_input_id: trace.payload.user_input_id, |
| 384 | client_session_id: trace.payload.client_session_id, |
| 385 | fingerprint_id: trace.payload.fingerprint_id, |
| 386 | output, |
| 387 | }, |
| 388 | } |
| 389 | |
| 390 | await insertRelabel({ relabel, logger }) |
no test coverage detected