|
| 1 | +import { RLMIteration, RLMLogFile, LogMetadata, RLMConfigMetadata, extractFinalAnswer } from './types'; |
| 2 | + |
| 3 | +// Extract the context variable from code block locals |
| 4 | +export function extractContextVariable(iterations: RLMIteration[]): string | null { |
| 5 | + for (const iter of iterations) { |
| 6 | + for (const block of iter.code_blocks) { |
| 7 | + if (block.result?.locals?.context) { |
| 8 | + const ctx = block.result.locals.context; |
| 9 | + if (typeof ctx === 'string') { |
| 10 | + return ctx; |
| 11 | + } |
| 12 | + } |
| 13 | + } |
| 14 | + } |
| 15 | + return null; |
| 16 | +} |
| 17 | + |
| 18 | +// Default config when metadata is not present (backwards compatibility) |
| 19 | +function getDefaultConfig(): RLMConfigMetadata { |
| 20 | + return { |
| 21 | + root_model: null, |
| 22 | + max_depth: null, |
| 23 | + max_iterations: null, |
| 24 | + backend: null, |
| 25 | + backend_kwargs: null, |
| 26 | + environment_type: null, |
| 27 | + environment_kwargs: null, |
| 28 | + other_backends: null, |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +export interface ParsedJSONL { |
| 33 | + iterations: RLMIteration[]; |
| 34 | + config: RLMConfigMetadata; |
| 35 | +} |
| 36 | + |
| 37 | +export function parseJSONL(content: string): ParsedJSONL { |
| 38 | + const lines = content.trim().split('\n').filter(line => line.trim()); |
| 39 | + const iterations: RLMIteration[] = []; |
| 40 | + let config: RLMConfigMetadata = getDefaultConfig(); |
| 41 | + |
| 42 | + for (const line of lines) { |
| 43 | + try { |
| 44 | + const parsed = JSON.parse(line); |
| 45 | + |
| 46 | + // Check if this is a metadata entry |
| 47 | + if (parsed.type === 'metadata') { |
| 48 | + config = { |
| 49 | + root_model: parsed.root_model ?? null, |
| 50 | + max_depth: parsed.max_depth ?? null, |
| 51 | + max_iterations: parsed.max_iterations ?? null, |
| 52 | + backend: parsed.backend ?? null, |
| 53 | + backend_kwargs: parsed.backend_kwargs ?? null, |
| 54 | + environment_type: parsed.environment_type ?? null, |
| 55 | + environment_kwargs: parsed.environment_kwargs ?? null, |
| 56 | + other_backends: parsed.other_backends ?? null, |
| 57 | + }; |
| 58 | + } else { |
| 59 | + // This is an iteration entry |
| 60 | + iterations.push(parsed as RLMIteration); |
| 61 | + } |
| 62 | + } catch (e) { |
| 63 | + console.error('Failed to parse line:', line, e); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return { iterations, config }; |
| 68 | +} |
| 69 | + |
| 70 | +export function extractContextQuestion(iterations: RLMIteration[]): string { |
| 71 | + if (iterations.length === 0) return 'No context found'; |
| 72 | + |
| 73 | + const firstIteration = iterations[0]; |
| 74 | + const prompt = firstIteration.prompt; |
| 75 | + |
| 76 | + // Look for user message that contains the actual question |
| 77 | + for (const msg of prompt) { |
| 78 | + if (msg.role === 'user' && msg.content) { |
| 79 | + // Try to extract quoted query |
| 80 | + const queryMatch = msg.content.match(/original query: "([^"]+)"/); |
| 81 | + if (queryMatch) { |
| 82 | + return queryMatch[1]; |
| 83 | + } |
| 84 | + |
| 85 | + // Check if it contains the actual query pattern |
| 86 | + if (msg.content.includes('answer the prompt')) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + // Take first substantial user message |
| 91 | + if (msg.content.length > 50 && msg.content.length < 500) { |
| 92 | + return msg.content.slice(0, 200) + (msg.content.length > 200 ? '...' : ''); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // Fallback: look in system prompt for context info |
| 98 | + const systemMsg = prompt.find(m => m.role === 'system'); |
| 99 | + if (systemMsg?.content) { |
| 100 | + const contextMatch = systemMsg.content.match(/context variable.*?:(.*?)(?:\n|$)/i); |
| 101 | + if (contextMatch) { |
| 102 | + return contextMatch[1].trim().slice(0, 200); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Check code block output for actual context |
| 107 | + for (const iter of iterations) { |
| 108 | + for (const block of iter.code_blocks) { |
| 109 | + if (block.result?.locals?.context) { |
| 110 | + const ctx = block.result.locals.context; |
| 111 | + if (typeof ctx === 'string' && ctx.length < 500) { |
| 112 | + return ctx; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return 'Context available in REPL environment'; |
| 119 | +} |
| 120 | + |
| 121 | +export function computeMetadata(iterations: RLMIteration[]): LogMetadata { |
| 122 | + let totalCodeBlocks = 0; |
| 123 | + let totalSubLMCalls = 0; |
| 124 | + let totalExecutionTime = 0; |
| 125 | + let hasErrors = false; |
| 126 | + let finalAnswer: string | null = null; |
| 127 | + |
| 128 | + for (const iter of iterations) { |
| 129 | + totalCodeBlocks += iter.code_blocks.length; |
| 130 | + |
| 131 | + // Use iteration_time if available, otherwise sum code block times |
| 132 | + if (iter.iteration_time != null) { |
| 133 | + totalExecutionTime += iter.iteration_time; |
| 134 | + } else { |
| 135 | + for (const block of iter.code_blocks) { |
| 136 | + if (block.result) { |
| 137 | + totalExecutionTime += block.result.execution_time || 0; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + for (const block of iter.code_blocks) { |
| 143 | + if (block.result) { |
| 144 | + if (block.result.stderr) { |
| 145 | + hasErrors = true; |
| 146 | + } |
| 147 | + if (block.result.rlm_calls) { |
| 148 | + totalSubLMCalls += block.result.rlm_calls.length; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if (iter.final_answer) { |
| 154 | + finalAnswer = extractFinalAnswer(iter.final_answer); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return { |
| 159 | + totalIterations: iterations.length, |
| 160 | + totalCodeBlocks, |
| 161 | + totalSubLMCalls, |
| 162 | + contextQuestion: extractContextQuestion(iterations), |
| 163 | + finalAnswer, |
| 164 | + totalExecutionTime, |
| 165 | + hasErrors, |
| 166 | + }; |
| 167 | +} |
| 168 | + |
| 169 | +export function parseLogFile(fileName: string, content: string): RLMLogFile { |
| 170 | + const { iterations, config } = parseJSONL(content); |
| 171 | + const metadata = computeMetadata(iterations); |
| 172 | + |
| 173 | + return { |
| 174 | + fileName, |
| 175 | + filePath: fileName, |
| 176 | + iterations, |
| 177 | + metadata, |
| 178 | + config, |
| 179 | + }; |
| 180 | +} |
| 181 | + |
0 commit comments