export type TabularColumnSchema = { index: number; name: string; prompt: string; format?: string; tags?: string[]; }; export type TabularTraceExtracted = { summary: string; flag: "green" | "grey" | "yellow" | "red"; reasoning: string; }; export type TabularTraceCell = { column_index: number; column_name: string; column_prompt: string; format?: string; tags?: string[]; status: "done" | "error"; error_reason?: "no_output" | "provider_error" | "empty_document"; extracted: TabularTraceExtracted | null; }; export type TabularDocumentTraceSummary = { document_id: string; filename: string; cells_done: number; cells_failed: number; llm_finish_reason: string | null; }; export type TabularColumnInput = { index: number; name: string; prompt: string; format?: string; tags?: string[]; }; const TRACE_TRUNC_SUFFIX = "…[truncated]"; export function truncateForTrace(text: string, maxLen = 2000): string { if (text.length <= maxLen) return text; return `${text.slice(0, maxLen)}${TRACE_TRUNC_SUFFIX}`; } export function buildColumnsSchema( columns: TabularColumnInput[], ): TabularColumnSchema[] { return columns.map((col) => ({ index: col.index, name: col.name, prompt: truncateForTrace(col.prompt), ...(col.format ? { format: truncateForTrace(col.format) } : {}), ...(col.tags?.length ? { tags: col.tags } : {}), })); } export function buildTraceCellEntry( col: TabularColumnInput, status: "done" | "error", extracted?: TabularTraceExtracted | null, errorReason?: TabularTraceCell["error_reason"], ): TabularTraceCell { const entry: TabularTraceCell = { column_index: col.index, column_name: col.name, column_prompt: truncateForTrace(col.prompt), status, extracted: null, }; if (col.format) entry.format = truncateForTrace(col.format); if (col.tags?.length) entry.tags = col.tags; if (status === "done" && extracted) { entry.extracted = { summary: truncateForTrace(extracted.summary), flag: extracted.flag, reasoning: truncateForTrace(extracted.reasoning), }; } if (status === "error" && errorReason) { entry.error_reason = errorReason; } return entry; } export function buildDocumentTraceSummary(params: { documentId: string; filename: string; cells: TabularTraceCell[]; llmFinishReason: string | null; }): TabularDocumentTraceSummary { const cellsDone = params.cells.filter((c) => c.status === "done").length; const cellsFailed = params.cells.filter((c) => c.status === "error").length; return { document_id: params.documentId, filename: params.filename, cells_done: cellsDone, cells_failed: cellsFailed, llm_finish_reason: params.llmFinishReason, }; } export function buildColumnsRequested( columns: TabularColumnInput[], ): { index: number; name: string }[] { return columns.map((col) => ({ index: col.index, name: col.name })); }