Spaces:
Running
Running
File size: 4,605 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 184 185 186 |
import type { MessageSegment } from "./chat-data";
export interface SegmentView {
readonly segment: MessageSegment;
readonly isExpanded: boolean;
readonly displayTitle: string;
readonly displayStatus: string;
readonly statusColor: string;
readonly canExpand: boolean;
readonly formattedContent?: string;
readonly metadata?: SegmentMetadata;
}
export interface SegmentMetadata {
readonly toolType?: "todo" | "console" | "file" | "default";
readonly fileName?: string;
readonly lineCount?: number;
readonly errorMessage?: string;
readonly duration?: number;
}
export interface TodoListView {
readonly tasks: TodoTask[];
readonly completedCount: number;
readonly totalCount: number;
readonly lastUpdated: number;
}
export interface TodoTask {
readonly id: number;
readonly description: string;
readonly status: "pending" | "in_progress" | "completed";
readonly emoji: string;
}
export function createSegmentView(
segment: MessageSegment,
isExpanded: boolean = false,
): SegmentView {
const statusColors: Record<string, string> = {
pending: "rgba(255, 193, 7, 0.8)",
running: "rgba(33, 150, 243, 0.8)",
completed: "rgba(76, 175, 80, 0.8)",
error: "rgba(244, 67, 54, 0.8)",
};
const displayTitle = getSegmentTitle(segment);
const displayStatus = getSegmentStatus(segment);
const statusColor =
statusColors[segment.toolStatus || "pending"] || "rgba(156, 163, 175, 0.8)";
const canExpand =
segment.type === "tool-invocation" || segment.type === "tool-result";
return {
segment,
isExpanded,
displayTitle,
displayStatus,
statusColor,
canExpand,
metadata: extractMetadata(segment),
};
}
function getSegmentTitle(segment: MessageSegment): string {
if (segment.type === "text") {
return "Text";
}
if (segment.type === "reasoning") {
return "Reasoning";
}
if (segment.toolName) {
const toolNames: Record<string, string> = {
plan_tasks: "π Plan Tasks",
update_task: "βοΈ Update Task",
view_tasks: "π View Tasks",
observe_console: "πΊ Console Output",
};
return toolNames[segment.toolName] || `π§ ${segment.toolName}`;
}
return "Tool";
}
function getSegmentStatus(segment: MessageSegment): string {
if (segment.streaming) {
return "streaming...";
}
if (segment.toolStatus) {
const statusLabels: Record<string, string> = {
pending: "Pending",
running: "Running",
completed: "Completed",
error: "Error",
};
return statusLabels[segment.toolStatus] || segment.toolStatus;
}
if (segment.endTime && segment.startTime) {
const duration = segment.endTime - segment.startTime;
if (duration < 1000) {
return `${duration}ms`;
}
return `${(duration / 1000).toFixed(1)}s`;
}
return "";
}
function extractMetadata(segment: MessageSegment): SegmentMetadata {
let toolType: SegmentMetadata["toolType"];
let fileName: string | undefined;
let errorMessage: string | undefined;
let duration: number | undefined;
let lineCount: number | undefined;
if (segment.toolName?.includes("task")) {
toolType = "todo";
} else if (segment.toolName === "observe_console") {
toolType = "console";
} else if (segment.toolName?.includes("file")) {
toolType = "file";
if (segment.toolArgs?.path) {
fileName = segment.toolArgs.path as string;
}
} else if (segment.toolName) {
toolType = "default";
}
if (segment.toolError) {
errorMessage = segment.toolError;
}
if (segment.endTime && segment.startTime) {
duration = segment.endTime - segment.startTime;
}
if (segment.toolOutput) {
lineCount = segment.toolOutput.split("\n").length;
}
return {
toolType,
fileName,
errorMessage,
duration,
lineCount,
};
}
export function parseTodoList(content: string): TodoListView | null {
const lines = content.split("\n");
const tasks: TodoTask[] = [];
for (const line of lines) {
const match = line.match(
/([β³πβ
])\s*\[(\d+)\]\s*(.+?)\s*\((pending|in_progress|completed)\)/u,
);
if (match) {
const [, emoji, id, description, status] = match;
tasks.push({
id: parseInt(id, 10),
description: description.trim(),
status: status as TodoTask["status"],
emoji,
});
}
}
if (tasks.length === 0) {
return null;
}
const completedCount = tasks.filter((t) => t.status === "completed").length;
return {
tasks,
completedCount,
totalCount: tasks.length,
lastUpdated: Date.now(),
};
}
|