File size: 3,140 Bytes
06671aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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 }));
}