5875
Os
ollama-space-molt-player / files
ollama-space-molt-player / packages / cui / src / formatter.ts
4.3 KB
1const DETAIL_LINE_MAX_LENGTH = 300; 2const VALUE_MAX_LENGTH = 100; 3const MAX_LINES_NORMAL = 4; 4const MAX_MULTILINE_NORMAL = 5; 5 6export function formatDetailLines(details: unknown, verbose: boolean): string[] { 7 if (details === undefined || details === null) { 8 return []; 9 } 10 if (typeof details === "string") { 11 return formatMultiline(details, verbose); 12 } 13 return formatPayloadLines(details, "", verbose, { current: 0 }); 14} 15 16export function formatMultiline(text: string, verbose: boolean): string[] { 17 const lines = text.split("\n"); 18 const truncated = 19 verbose || lines.length <= MAX_MULTILINE_NORMAL 20 ? lines 21 : [lines[0], `[... ${lines.length - 4} more lines ...]`, ...lines.slice(-3)]; 22 return truncated.map((line) => truncateLine(line)); 23} 24 25export function truncateLine(line: string): string { 26 if (line.length <= DETAIL_LINE_MAX_LENGTH) { 27 return line; 28 } 29 return `${line.substring(0, DETAIL_LINE_MAX_LENGTH)}...`; 30} 31 32export function formatPayloadLines( 33 payload: unknown, 34 indent: string, 35 verbose: boolean, 36 lineCount: { current: number }, 37): string[] { 38 if (!payload || typeof payload !== "object") { 39 lineCount.current++; 40 return [`${indent}${String(payload)}`]; 41 } 42 43 const lines: string[] = []; 44 for (const [key, value] of Object.entries(payload)) { 45 if (!verbose && lineCount.current >= MAX_LINES_NORMAL) { 46 lines.push(`${indent}...`); 47 return lines; 48 } 49 if (value === null || value === undefined) { 50 continue; 51 } 52 if (Array.isArray(value)) { 53 if (value.length === 0) { 54 lines.push(`${indent}${key}: []`); 55 lineCount.current++; 56 } else if (typeof value[0] === "object") { 57 lines.push(`${indent}${key}: [${value.length} items]`); 58 lineCount.current++; 59 if (verbose) { 60 value.forEach((item, idx) => { 61 lines.push(`${indent} [${idx}]:`); 62 lineCount.current++; 63 lines.push(...formatPayloadLines(item, `${indent} `, verbose, lineCount)); 64 }); 65 } 66 } else { 67 lines.push(`${indent}${key}: [${value.join(", ")}]`); 68 lineCount.current++; 69 } 70 } else if (typeof value === "object") { 71 lines.push(`${indent}${key}:`); 72 lineCount.current++; 73 if (verbose || lineCount.current < MAX_LINES_NORMAL) { 74 lines.push(...formatPayloadLines(value, `${indent} `, verbose, lineCount)); 75 } 76 } else if (typeof value === "string") { 77 const truncated = 78 value.length > VALUE_MAX_LENGTH ? `${value.substring(0, VALUE_MAX_LENGTH)}...` : value; 79 lines.push(`${indent}${key}: ${truncated}`); 80 lineCount.current++; 81 } else { 82 lines.push(`${indent}${key}: ${String(value)}`); 83 lineCount.current++; 84 } 85 } 86 87 return lines.map((line) => truncateLine(line)); 88} 89 90export function maskSensitiveData(payload: unknown): unknown { 91 if (payload === null || payload === undefined) { 92 return payload; 93 } 94 if (typeof payload !== "object") { 95 return payload; 96 } 97 if (Array.isArray(payload)) { 98 return payload.map((item) => maskSensitiveData(item)); 99 } 100 const result: Record<string, unknown> = {}; 101 const sensitiveKeys = ["token", "password"]; 102 for (const [key, value] of Object.entries(payload as Record<string, unknown>)) { 103 const keyLower = key.toLowerCase(); 104 if (sensitiveKeys.some((sensitive) => keyLower.includes(sensitive))) { 105 if (typeof value === "string") { 106 result[key] = value.length > 8 ? `${value.substring(0, 8)}...` : value; 107 } else { 108 result[key] = value; 109 } 110 } else { 111 result[key] = maskSensitiveData(value); 112 } 113 } 114 return result; 115} 116 117export function formatThinking(thinking: string, verbose: boolean): string { 118 if (verbose) { 119 return thinking; 120 } 121 122 const sentences = splitSentences(thinking); 123 if (sentences.length <= 3) { 124 return sentences.join(" ").trim(); 125 } 126 const first = sentences[0]; 127 const lastTwo = sentences.slice(-2); 128 const omittedCount = sentences.length - 3; 129 return `${first} [... ${omittedCount} sentences omitted ...] ${lastTwo.join(" ")}`.trim(); 130} 131 132export function splitSentences(text: string): string[] { 133 const normalized = text.replace(/\s+/g, " ").trim(); 134 if (!normalized) { 135 return []; 136 } 137 const matches = normalized.match(/[^.!?]+[.!?]+|[^.!?]+$/g); 138 if (!matches) { 139 return [normalized]; 140 } 141 return matches.map((sentence) => sentence.trim()).filter((sentence) => sentence.length > 0); 142} 143 144export function normalizeErrorDetails(details: unknown): unknown { 145 if (details instanceof Error) { 146 return { 147 message: details.message, 148 stack: details.stack, 149 }; 150 } 151 return details; 152}