|
| 1 | +import { asOptionalRecord as readRecord } from "@openclaw/normalization-core/record-coerce"; |
| 2 | +import { extractCanvasFromDetails, extractCanvasFromText } from "../chat/canvas-render.js"; |
| 3 | +import { truncateChatHistoryText } from "./chat-display-projection.helpers.js"; |
| 4 | + |
| 5 | +/** Return true for known tool-call/tool-result block type spellings in transcripts. */ |
| 6 | +export function isToolHistoryBlockType(type: unknown): boolean { |
| 7 | + if (typeof type !== "string") { |
| 8 | + return false; |
| 9 | + } |
| 10 | + const normalized = type.trim().toLowerCase(); |
| 11 | + return ( |
| 12 | + normalized === "toolcall" || |
| 13 | + normalized === "tool_call" || |
| 14 | + normalized === "tooluse" || |
| 15 | + normalized === "tool_use" || |
| 16 | + normalized === "toolresult" || |
| 17 | + normalized === "tool_result" |
| 18 | + ); |
| 19 | +} |
| 20 | + |
| 21 | +export function isToolResultHistoryBlockType(type: unknown): boolean { |
| 22 | + if (typeof type !== "string") { |
| 23 | + return false; |
| 24 | + } |
| 25 | + const normalized = type.trim().toLowerCase(); |
| 26 | + return normalized === "toolresult" || normalized === "tool_result"; |
| 27 | +} |
| 28 | + |
| 29 | +export function projectToolResultDetails( |
| 30 | + details: unknown, |
| 31 | + maxChars: number, |
| 32 | +): Record<string, unknown> | undefined { |
| 33 | + const record = readRecord(details); |
| 34 | + if (!record) { |
| 35 | + return undefined; |
| 36 | + } |
| 37 | + const projected: Record<string, unknown> = {}; |
| 38 | + for (const key of ["changed", "created"] as const) { |
| 39 | + if (typeof record[key] === "boolean") { |
| 40 | + projected[key] = record[key]; |
| 41 | + } |
| 42 | + } |
| 43 | + if (typeof record.diff === "string" && record.diff.trim()) { |
| 44 | + projected.diff = truncateChatHistoryText(record.diff, maxChars).text; |
| 45 | + } |
| 46 | + const preview = extractCanvasFromDetails(record); |
| 47 | + if (preview?.mcpApp && preview.viewId) { |
| 48 | + projected.mcpAppPreview = { |
| 49 | + kind: "canvas", |
| 50 | + view: { |
| 51 | + id: preview.viewId, |
| 52 | + ...(preview.url ? { url: preview.url } : {}), |
| 53 | + ...(preview.title ? { title: preview.title } : {}), |
| 54 | + }, |
| 55 | + presentation: { |
| 56 | + target: "assistant_message", |
| 57 | + ...(preview.title ? { title: preview.title } : {}), |
| 58 | + ...(preview.preferredHeight ? { preferred_height: preview.preferredHeight } : {}), |
| 59 | + ...(preview.sandbox ? { sandbox: preview.sandbox } : {}), |
| 60 | + }, |
| 61 | + mcpApp: preview.mcpApp, |
| 62 | + }; |
| 63 | + } |
| 64 | + return Object.keys(projected).length > 0 ? projected : undefined; |
| 65 | +} |
| 66 | + |
| 67 | +export function messageHasToolResultShape(message: Record<string, unknown>): boolean { |
| 68 | + const role = typeof message.role === "string" ? message.role.toLowerCase() : ""; |
| 69 | + if (role === "toolresult" || role === "tool_result" || role === "tool" || role === "function") { |
| 70 | + return true; |
| 71 | + } |
| 72 | + const content = Array.isArray(message.content) ? message.content : []; |
| 73 | + if ( |
| 74 | + content.some( |
| 75 | + (block) => |
| 76 | + block && |
| 77 | + typeof block === "object" && |
| 78 | + isToolResultHistoryBlockType((block as { type?: unknown }).type), |
| 79 | + ) |
| 80 | + ) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + const hasToolCallBlock = content.some( |
| 84 | + (block) => |
| 85 | + block && |
| 86 | + typeof block === "object" && |
| 87 | + isToolHistoryBlockType((block as { type?: unknown }).type) && |
| 88 | + !isToolResultHistoryBlockType((block as { type?: unknown }).type), |
| 89 | + ); |
| 90 | + const hasToolId = |
| 91 | + typeof message.toolCallId === "string" || |
| 92 | + typeof message.tool_call_id === "string" || |
| 93 | + typeof message.toolUseId === "string" || |
| 94 | + typeof message.tool_use_id === "string"; |
| 95 | + const hasToolName = typeof message.toolName === "string" || typeof message.tool_name === "string"; |
| 96 | + return hasToolId && hasToolName && !hasToolCallBlock; |
| 97 | +} |
| 98 | + |
| 99 | +export function extractChatHistoryBlockText(message: unknown): string | undefined { |
| 100 | + if (!message || typeof message !== "object") { |
| 101 | + return undefined; |
| 102 | + } |
| 103 | + const entry = message as Record<string, unknown>; |
| 104 | + if (typeof entry.content === "string") { |
| 105 | + return entry.content; |
| 106 | + } |
| 107 | + if (typeof entry.text === "string") { |
| 108 | + return entry.text; |
| 109 | + } |
| 110 | + if (!Array.isArray(entry.content)) { |
| 111 | + return undefined; |
| 112 | + } |
| 113 | + const textParts = entry.content |
| 114 | + .map((block) => { |
| 115 | + if (!block || typeof block !== "object") { |
| 116 | + return undefined; |
| 117 | + } |
| 118 | + const typed = block as { text?: unknown }; |
| 119 | + return typeof typed.text === "string" ? typed.text : undefined; |
| 120 | + }) |
| 121 | + .filter((value): value is string => typeof value === "string"); |
| 122 | + return textParts.length > 0 ? textParts.join("\n") : undefined; |
| 123 | +} |
| 124 | + |
| 125 | +function extractChatHistoryCanvasPreview(message: Record<string, unknown>) { |
| 126 | + const direct = extractCanvasFromDetails(message.details); |
| 127 | + if (direct) { |
| 128 | + return direct; |
| 129 | + } |
| 130 | + if (!Array.isArray(message.content)) { |
| 131 | + return undefined; |
| 132 | + } |
| 133 | + for (const block of message.content) { |
| 134 | + const preview = extractCanvasFromDetails(readRecord(block)?.details); |
| 135 | + if (preview) { |
| 136 | + return preview; |
| 137 | + } |
| 138 | + } |
| 139 | + return undefined; |
| 140 | +} |
| 141 | + |
| 142 | +function appendCanvasBlockToAssistantHistoryMessage(params: { |
| 143 | + message: unknown; |
| 144 | + preview: ReturnType<typeof extractCanvasFromText>; |
| 145 | + rawText: string | null; |
| 146 | +}): unknown { |
| 147 | + const preview = params.preview; |
| 148 | + if (!preview || !params.message || typeof params.message !== "object") { |
| 149 | + return params.message; |
| 150 | + } |
| 151 | + const entry = params.message as Record<string, unknown>; |
| 152 | + const baseContent = Array.isArray(entry.content) |
| 153 | + ? [...entry.content] |
| 154 | + : typeof entry.content === "string" |
| 155 | + ? [{ type: "text", text: entry.content }] |
| 156 | + : typeof entry.text === "string" |
| 157 | + ? [{ type: "text", text: entry.text }] |
| 158 | + : []; |
| 159 | + const alreadyPresent = baseContent.some((block) => { |
| 160 | + if (!block || typeof block !== "object") { |
| 161 | + return false; |
| 162 | + } |
| 163 | + const typed = block as { type?: unknown; preview?: unknown }; |
| 164 | + return ( |
| 165 | + typed.type === "canvas" && |
| 166 | + typed.preview && |
| 167 | + typeof typed.preview === "object" && |
| 168 | + (((typed.preview as { viewId?: unknown }).viewId && |
| 169 | + (typed.preview as { viewId?: unknown }).viewId === preview.viewId) || |
| 170 | + ((typed.preview as { url?: unknown }).url && |
| 171 | + (typed.preview as { url?: unknown }).url === preview.url)) |
| 172 | + ); |
| 173 | + }); |
| 174 | + if (!alreadyPresent) { |
| 175 | + baseContent.push({ |
| 176 | + type: "canvas", |
| 177 | + preview, |
| 178 | + rawText: params.rawText, |
| 179 | + }); |
| 180 | + } |
| 181 | + return { |
| 182 | + ...entry, |
| 183 | + content: baseContent, |
| 184 | + }; |
| 185 | +} |
| 186 | + |
| 187 | +function messageContainsToolHistoryContent(message: unknown): boolean { |
| 188 | + if (!message || typeof message !== "object") { |
| 189 | + return false; |
| 190 | + } |
| 191 | + const entry = message as Record<string, unknown>; |
| 192 | + if ( |
| 193 | + typeof entry.toolCallId === "string" || |
| 194 | + typeof entry.tool_call_id === "string" || |
| 195 | + typeof entry.toolName === "string" || |
| 196 | + typeof entry.tool_name === "string" |
| 197 | + ) { |
| 198 | + return true; |
| 199 | + } |
| 200 | + if (!Array.isArray(entry.content)) { |
| 201 | + return false; |
| 202 | + } |
| 203 | + return entry.content.some((block) => { |
| 204 | + if (!block || typeof block !== "object") { |
| 205 | + return false; |
| 206 | + } |
| 207 | + return isToolHistoryBlockType((block as { type?: unknown }).type); |
| 208 | + }); |
| 209 | +} |
| 210 | + |
| 211 | +export function augmentChatHistoryWithCanvasBlocks(messages: unknown[]): unknown[] { |
| 212 | + if (messages.length === 0) { |
| 213 | + return messages; |
| 214 | + } |
| 215 | + const next = [...messages]; |
| 216 | + let changed = false; |
| 217 | + let lastAssistantIndex = -1; |
| 218 | + let lastRenderableAssistantIndex = -1; |
| 219 | + const pending: Array<{ |
| 220 | + preview: NonNullable<ReturnType<typeof extractCanvasFromText>>; |
| 221 | + rawText: string | null; |
| 222 | + }> = []; |
| 223 | + for (let index = 0; index < next.length; index++) { |
| 224 | + const message = next[index]; |
| 225 | + if (!message || typeof message !== "object") { |
| 226 | + continue; |
| 227 | + } |
| 228 | + const entry = message as Record<string, unknown>; |
| 229 | + const role = typeof entry.role === "string" ? entry.role.toLowerCase() : ""; |
| 230 | + if (role === "assistant") { |
| 231 | + lastAssistantIndex = index; |
| 232 | + if (!messageContainsToolHistoryContent(entry)) { |
| 233 | + lastRenderableAssistantIndex = index; |
| 234 | + if (pending.length > 0) { |
| 235 | + let target = next[index]; |
| 236 | + for (const item of pending) { |
| 237 | + target = appendCanvasBlockToAssistantHistoryMessage({ |
| 238 | + message: target, |
| 239 | + preview: item.preview, |
| 240 | + rawText: item.rawText, |
| 241 | + }); |
| 242 | + } |
| 243 | + next[index] = target; |
| 244 | + pending.length = 0; |
| 245 | + changed = true; |
| 246 | + } |
| 247 | + } |
| 248 | + continue; |
| 249 | + } |
| 250 | + if (!messageContainsToolHistoryContent(entry)) { |
| 251 | + continue; |
| 252 | + } |
| 253 | + const toolName = |
| 254 | + typeof entry.toolName === "string" |
| 255 | + ? entry.toolName |
| 256 | + : typeof entry.tool_name === "string" |
| 257 | + ? entry.tool_name |
| 258 | + : undefined; |
| 259 | + const text = extractChatHistoryBlockText(entry); |
| 260 | + const detailsPreview = extractChatHistoryCanvasPreview(entry); |
| 261 | + const preview = detailsPreview ?? extractCanvasFromText(text, toolName); |
| 262 | + if (!preview) { |
| 263 | + continue; |
| 264 | + } |
| 265 | + pending.push({ |
| 266 | + preview, |
| 267 | + rawText: detailsPreview ? null : (text ?? null), |
| 268 | + }); |
| 269 | + } |
| 270 | + if (pending.length > 0) { |
| 271 | + const targetIndex = |
| 272 | + lastRenderableAssistantIndex >= 0 ? lastRenderableAssistantIndex : lastAssistantIndex; |
| 273 | + if (targetIndex >= 0) { |
| 274 | + let target = next[targetIndex]; |
| 275 | + for (const item of pending) { |
| 276 | + target = appendCanvasBlockToAssistantHistoryMessage({ |
| 277 | + message: target, |
| 278 | + preview: item.preview, |
| 279 | + rawText: item.rawText, |
| 280 | + }); |
| 281 | + } |
| 282 | + next[targetIndex] = target; |
| 283 | + changed = true; |
| 284 | + } |
| 285 | + } |
| 286 | + return changed ? next : messages; |
| 287 | +} |
0 commit comments