The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { msg } from "gt-react"; |
| 2 | import type { PluggableList } from "unified"; |
| 3 | import type { Root, RootContent } from "mdast"; |
| 4 | import { |
| 5 | resolveWorkEntryToolPresentation, |
| 6 | toolGroupAction, |
| 7 | } from "@t3tools/client-runtime/work-log/presentation"; |
| 8 | import { isJavaScriptReplTool } from "@t3tools/shared/toolActivity"; |
| 9 | import { formatWorkspaceRelativePath } from "../../filePathDisplay"; |
| 10 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 11 | import { type TimelineWorkEntry, type TimelineRow } from "./timelineRowContext"; |
| 12 | import { type WorkEntryIconName } from "./TimelineToolActivityIcon"; |
| 13 | |
| 14 | export function toolGroupSummaryIconName( |
| 15 | kind: Extract<TimelineRow, { kind: "work-toggle" }>["summaryKind"], |
| 16 | ): WorkEntryIconName { |
| 17 | switch (kind) { |
| 18 | case "pull-request": |
| 19 | case "link-pr": |
| 20 | case "unlink-pr": |
| 21 | case "list-prs": |
| 22 | case "watch-pr": |
| 23 | case "unwatch-pr": |
| 24 | return "pull-request"; |
| 25 | case "read": |
| 26 | return "eye"; |
| 27 | case "edit": |
| 28 | return "square-pen"; |
| 29 | case "command": |
| 30 | return "terminal"; |
| 31 | case "thread-create": |
| 32 | return "t3-code"; |
| 33 | case "browser": |
| 34 | return "browser"; |
| 35 | case "device": |
| 36 | return "device"; |
| 37 | case "search": |
| 38 | return "globe"; |
| 39 | case "code-search": |
| 40 | return "search"; |
| 41 | case "other": |
| 42 | return "wrench"; |
| 43 | case "dynamic-tool": |
| 44 | return "hammer"; |
| 45 | case "reasoning": |
| 46 | return "brain"; |
| 47 | case "agent-tool": |
| 48 | return "bot"; |
| 49 | case "tone-tool": |
| 50 | return "zap"; |
| 51 | case "update": |
| 52 | case "mixed": |
| 53 | return "hammer"; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | export function workToneIcon(tone: TimelineWorkEntry["tone"]): { |
| 58 | iconName: WorkEntryIconName; |
| 59 | className: string; |
| 60 | } { |
| 61 | if (tone === "error") { |
| 62 | return { |
| 63 | iconName: "circle-alert", |
| 64 | className: "text-foreground/92", |
| 65 | }; |
| 66 | } |
| 67 | if (tone === "thinking") { |
| 68 | return { |
| 69 | iconName: "brain", |
| 70 | className: "text-icon-muted", |
| 71 | }; |
| 72 | } |
| 73 | if (tone === "info") { |
| 74 | return { |
| 75 | iconName: "check", |
| 76 | className: "text-muted-foreground", |
| 77 | }; |
| 78 | } |
| 79 | return { |
| 80 | iconName: "zap", |
| 81 | className: "text-foreground/92", |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | export function workEntryRawCommand( |
| 86 | workEntry: Pick<TimelineWorkEntry, "command" | "rawCommand">, |
| 87 | ): string | null { |
| 88 | const rawCommand = workEntry.rawCommand?.trim(); |
| 89 | if (!rawCommand || !workEntry.command) { |
| 90 | return null; |
| 91 | } |
| 92 | return rawCommand === workEntry.command.trim() ? null : rawCommand; |
| 93 | } |
| 94 | |
| 95 | export function buildToolCallExpandedBody( |
| 96 | workEntry: TimelineWorkEntry, |
| 97 | workspaceRoot: string | undefined, |
| 98 | visibleLabel: string, |
| 99 | viewedImagePath: string | null, |
| 100 | resolve: MessageResolver = m, |
| 101 | ): string | null { |
| 102 | const blocks: string[] = []; |
| 103 | const seen = new Set<string>([visibleLabel.trim()]); |
| 104 | const addBlock = (value: string | null | undefined) => { |
| 105 | const text = value?.trim(); |
| 106 | if (!text || seen.has(text)) return; |
| 107 | seen.add(text); |
| 108 | blocks.push(text); |
| 109 | }; |
| 110 | if (workEntry.itemType === "dynamic_tool" && workEntry.toolData !== undefined) { |
| 111 | const input = |
| 112 | workEntry.structuredPayload?.type === "dynamic_tool" |
| 113 | ? workEntry.structuredPayload.input |
| 114 | : workEntry.toolData !== null && |
| 115 | typeof workEntry.toolData === "object" && |
| 116 | "input" in workEntry.toolData |
| 117 | ? workEntry.toolData.input |
| 118 | : undefined; |
| 119 | if (input !== undefined) { |
| 120 | addBlock(resolve(msg("Tool input\n{input}", { input: JSON.stringify(input, null, 2) }))); |
| 121 | } |
| 122 | } |
| 123 | const command = workEntry.command?.trim(); |
| 124 | const raw = workEntryRawCommand(workEntry); |
| 125 | if (command === visibleLabel.trim()) { |
| 126 | seen.add(command); |
| 127 | } else { |
| 128 | addBlock(raw ?? command); |
| 129 | } |
| 130 | const detail = workEntry.detail?.trim(); |
| 131 | if (detail !== viewedImagePath?.trim()) { |
| 132 | addBlock(detail); |
| 133 | } |
| 134 | const viewedImagePaths = new Set( |
| 135 | viewedImagePath |
| 136 | ? [viewedImagePath.trim(), formatWorkspaceRelativePath(viewedImagePath, workspaceRoot)] |
| 137 | : [], |
| 138 | ); |
| 139 | const changedFiles = (workEntry.changedFiles ?? []).flatMap((filePath) => { |
| 140 | const formattedPath = formatWorkspaceRelativePath(filePath, workspaceRoot); |
| 141 | return viewedImagePaths.has(filePath) || |
| 142 | viewedImagePaths.has(formattedPath) || |
| 143 | filePath.trim() === detail || |
| 144 | formattedPath === detail |
| 145 | ? [] |
| 146 | : [formattedPath]; |
| 147 | }); |
| 148 | if (changedFiles.length > 0) { |
| 149 | addBlock([...new Set(changedFiles)].join("\n")); |
| 150 | } |
| 151 | return blocks.length > 0 ? blocks.join("\n\n") : null; |
| 152 | } |
| 153 | |
| 154 | export function workEntryIconName(workEntry: TimelineWorkEntry): WorkEntryIconName { |
| 155 | if (workEntry.structuredPayload?.type === "notification") { |
| 156 | if (workEntry.structuredPayload.outcome === "failed") return "circle-alert"; |
| 157 | const source = workEntry.structuredPayload.source; |
| 158 | switch (source.kind) { |
| 159 | case "subagent": |
| 160 | case "delegated_task": |
| 161 | return "bot"; |
| 162 | case "command": |
| 163 | return "terminal"; |
| 164 | case "monitor": |
| 165 | return "eye"; |
| 166 | case "background_task": |
| 167 | return "zap"; |
| 168 | case "system": |
| 169 | return "t3-code"; |
| 170 | default: |
| 171 | source satisfies never; |
| 172 | return "zap"; |
| 173 | } |
| 174 | } |
| 175 | if (workEntry.itemType === "system_notice") { |
| 176 | return workEntry.structuredPayload?.type === "system_notice" && |
| 177 | workEntry.structuredPayload.level === "info" |
| 178 | ? "zap" |
| 179 | : "circle-alert"; |
| 180 | } |
| 181 | if (workEntry.itemType === "user_input_request" || workEntry.itemType === "approval_request") { |
| 182 | return "message-circle"; |
| 183 | } |
| 184 | if (workEntry.itemType === "todo_list") return "list-todo"; |
| 185 | if (workEntry.toolSurface) return workEntry.toolSurface; |
| 186 | if ( |
| 187 | workEntry.structuredPayload?.type === "dynamic_tool" && |
| 188 | isJavaScriptReplTool(workEntry.structuredPayload.toolName) |
| 189 | ) |
| 190 | return "code"; |
| 191 | const toolPresentation = resolveWorkEntryToolPresentation(workEntry, undefined, m); |
| 192 | if (toolPresentation) return toolPresentation.icon; |
| 193 | const action = toolGroupAction(workEntry); |
| 194 | if (action !== "other") return toolGroupSummaryIconName(action); |
| 195 | |
| 196 | switch (workEntry.itemType) { |
| 197 | case "dynamic_tool": |
| 198 | return "wrench"; |
| 199 | case "subagent": |
| 200 | return "bot"; |
| 201 | } |
| 202 | |
| 203 | return workToneIcon(workEntry.tone).iconName; |
| 204 | } |
| 205 | |
| 206 | // Look past the visible label so long destinations and inline delimiters can close. |
| 207 | // This is bounded lookahead, not document-wide Markdown parity: a closing delimiter |
| 208 | // or reference definition beyond this prefix remains unresolved by the parser. |
| 209 | export const THOUGHT_PREVIEW_SOURCE_MAX_LENGTH = 8_192; |
| 210 | export const THOUGHT_PREVIEW_MAX_LENGTH = 2_048; |
| 211 | |
| 212 | /** Compact reasoning keeps formulas while flattening other Markdown into label text. */ |
| 213 | export function remarkThoughtPreview(fallback: string) { |
| 214 | return (tree: Root) => { |
| 215 | const children: RootContent[] = []; |
| 216 | let remaining = THOUGHT_PREVIEW_MAX_LENGTH; |
| 217 | let fragments: string[] = []; |
| 218 | const flush = () => { |
| 219 | if (fragments.length === 0) return; |
| 220 | children.push({ type: "text", value: fragments.join("").replace(/\s+/g, " ") }); |
| 221 | fragments = []; |
| 222 | }; |
| 223 | const take = (value: string) => { |
| 224 | const text = value.slice(0, remaining); |
| 225 | remaining = text.length < value.length ? 0 : remaining - text.length; |
| 226 | // A cut here or at the source limit must not leave half a surrogate pair. |
| 227 | return text.replace(/[\uD800-\uDBFF]$/, ""); |
| 228 | }; |
| 229 | const append = (value: string) => { |
| 230 | fragments.push(take(value)); |
| 231 | }; |
| 232 | const visit = (node: Root | RootContent) => { |
| 233 | if (remaining === 0 || node.type === "html" || node.type === "definition") return; |
| 234 | if (node.type === "math" || node.type === "inlineMath") { |
| 235 | flush(); |
| 236 | const value = take(node.value); |
| 237 | children.push({ |
| 238 | type: "inlineMath", |
| 239 | value, |
| 240 | position: node.position, |
| 241 | data: { |
| 242 | hName: "code", |
| 243 | hProperties: { className: ["language-math", "math-inline"] }, |
| 244 | hChildren: [{ type: "text", value }], |
| 245 | }, |
| 246 | }); |
| 247 | } else if ("alt" in node) append(node.alt ?? ""); |
| 248 | else if ("value" in node) append(node.value); |
| 249 | else if ("children" in node) { |
| 250 | for (const [index, child] of node.children.entries()) { |
| 251 | if (remaining === 0) break; |
| 252 | if ( |
| 253 | index && |
| 254 | ["root", "blockquote", "list", "listItem", "table", "tableRow"].includes(node.type) |
| 255 | ) |
| 256 | append(" "); |
| 257 | visit(child); |
| 258 | } |
| 259 | } else if (node.type === "break") append(" "); |
| 260 | }; |
| 261 | visit(tree); |
| 262 | flush(); |
| 263 | const first = children[0]; |
| 264 | const last = children.at(-1); |
| 265 | if (first?.type === "text") first.value = first.value.trimStart(); |
| 266 | if (last?.type === "text") last.value = last.value.trimEnd(); |
| 267 | tree.children = children.some((child) => child.type !== "text" || child.value) |
| 268 | ? children |
| 269 | : [{ type: "text", value: fallback }]; |
| 270 | }; |
| 271 | } |
| 272 | |
| 273 | export function thoughtPreviewPlugins(fallback: string): PluggableList { |
| 274 | return [[remarkThoughtPreview, fallback]]; |
| 275 | } |