Spaces:
Running
Running
File size: 4,835 Bytes
bc7e9cd |
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
import type { MessageSegment } from "../models/chat-data";
import type { TodoListView } from "../models/segment-view";
import { parseTodoList } from "../models/segment-view";
export class SegmentFormatter {
private static todoListCache: Map<string, TodoListView> = new Map();
static formatSegmentContent(segment: MessageSegment): string {
switch (segment.type) {
case "text":
return segment.content;
case "reasoning":
return segment.content;
case "tool-invocation":
return this.formatToolInvocation(segment);
case "tool-result":
return this.formatToolResult(segment);
default:
return segment.content;
}
}
static formatToolInvocation(segment: MessageSegment): string {
const args = segment.toolArgs
? JSON.stringify(segment.toolArgs, null, 2)
: "No arguments";
return `Tool: ${segment.toolName}\nArguments:\n${args}`;
}
static formatToolResult(segment: MessageSegment): string {
if (segment.toolError) {
return `β Error: ${segment.toolError}`;
}
if (segment.toolName?.includes("task")) {
return this.formatTodoResult(segment);
}
if (segment.toolName === "observe_console") {
return this.formatConsoleOutput(segment);
}
return segment.toolOutput || segment.content || "No output";
}
static formatTodoResult(segment: MessageSegment): string {
const content = segment.toolOutput || segment.content;
const todoList = parseTodoList(content);
if (todoList) {
this.todoListCache.set(segment.id, todoList);
return this.renderTodoList(todoList);
}
return content;
}
static formatConsoleOutput(segment: MessageSegment): string {
const output = segment.toolOutput || segment.content;
const lines = output.split("\n");
const formatted = lines
.map((line) => {
if (line.includes("[error]")) {
return `π΄ ${line}`;
} else if (line.includes("[warn]")) {
return `π‘ ${line}`;
} else if (line.includes("[info]")) {
return `π΅ ${line}`;
} else if (line.includes("[debug]")) {
return `βͺ ${line}`;
}
return line;
})
.join("\n");
return formatted;
}
static renderTodoList(todoList: TodoListView): string {
const header = `π Tasks (${todoList.completedCount}/${todoList.totalCount} completed)\n`;
const separator = "β".repeat(40) + "\n";
const tasks = todoList.tasks
.map((task) => `${task.emoji} [${task.id}] ${task.description}`)
.join("\n");
return header + separator + tasks;
}
static getLatestTodoList(): TodoListView | null {
if (this.todoListCache.size === 0) {
return null;
}
let latest: TodoListView | null = null;
let latestTime = 0;
for (const todoList of this.todoListCache.values()) {
if (todoList.lastUpdated > latestTime) {
latest = todoList;
latestTime = todoList.lastUpdated;
}
}
return latest;
}
static shouldCollapseByDefault(segment: MessageSegment): boolean {
if (segment.type !== "tool-invocation" && segment.type !== "tool-result") {
return false;
}
if (segment.toolError) {
return false;
}
if (segment.toolName?.includes("task")) {
return false;
}
const output = segment.toolOutput || segment.content || "";
const lineCount = output.split("\n").length;
return lineCount > 10;
}
static getSegmentIcon(segment: MessageSegment): string {
const iconMap: Record<string, string> = {
text: "π¬",
reasoning: "π€",
"tool-invocation": "π§",
"tool-result": "π",
};
if (segment.toolName) {
const toolIcons: Record<string, string> = {
plan_tasks: "π",
update_task: "βοΈ",
view_tasks: "π",
observe_console: "πΊ",
};
return toolIcons[segment.toolName] || iconMap[segment.type] || "π";
}
return iconMap[segment.type] || "π";
}
static formatDuration(ms: number): string {
if (ms < 1000) {
return `${ms}ms`;
} else if (ms < 60000) {
return `${(ms / 1000).toFixed(1)}s`;
} else {
const minutes = Math.floor(ms / 60000);
const seconds = Math.floor((ms % 60000) / 1000);
return `${minutes}m ${seconds}s`;
}
}
static truncateContent(content: string, maxLength: number = 100): string {
if (content.length <= maxLength) {
return content;
}
const truncated = content.substring(0, maxLength);
const lastSpace = truncated.lastIndexOf(" ");
if (lastSpace > maxLength * 0.8) {
return truncated.substring(0, lastSpace) + "...";
}
return truncated + "...";
}
}
export const segmentFormatter = new SegmentFormatter();
|