Skip to content

Commit 0f1a157

Browse files
feat: expand part-aware message rendering (anomalyco#258)
1 parent cf6cc10 commit 0f1a157

1 file changed

Lines changed: 252 additions & 24 deletions

File tree

‎packages/app/src/app/components/part-view.tsx‎

Lines changed: 252 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Match, Show, Switch, createMemo } from "solid-js";
1+
import { For, Match, Show, Switch, createEffect, createMemo, createSignal, onCleanup } from "solid-js";
22
import { marked } from "marked";
33
import type { Part } from "@opencode-ai/sdk/v2/client";
44
import { safeStringify } from "../utils";
@@ -16,6 +16,30 @@ function clampText(text: string, max = 800) {
1616
return `${text.slice(0, max)}\n\n… (truncated)`;
1717
}
1818

19+
function useThrottledValue<T>(value: () => T, delayMs = 80) {
20+
const [state, setState] = createSignal<T>(value());
21+
let timer: ReturnType<typeof setTimeout> | undefined;
22+
23+
createEffect(() => {
24+
const next = value();
25+
if (!delayMs) {
26+
setState(next);
27+
return;
28+
}
29+
if (timer) clearTimeout(timer);
30+
timer = setTimeout(() => {
31+
setState(next);
32+
timer = undefined;
33+
}, delayMs);
34+
});
35+
36+
onCleanup(() => {
37+
if (timer) clearTimeout(timer);
38+
});
39+
40+
return state;
41+
}
42+
1943
function createCustomRenderer(tone: "light" | "dark") {
2044
const renderer = new marked.Renderer();
2145
const codeBlockClass =
@@ -101,11 +125,16 @@ export default function PartView(props: Props) {
101125
const textClass = () => (tone() === "dark" ? "text-gray-12" : "text-gray-12");
102126
const subtleTextClass = () => (tone() === "dark" ? "text-gray-12/70" : "text-gray-11");
103127
const panelBgClass = () => (tone() === "dark" ? "bg-gray-2/10" : "bg-gray-2/30");
104-
const toolOnly = () => developerMode();
128+
const toolOnly = () => true;
105129
const showToolOutput = () => developerMode();
130+
const markdownSource = createMemo(() => {
131+
if (!renderMarkdown() || p().type !== "text") return "";
132+
return "text" in p() ? String((p() as { text: string }).text ?? "") : "";
133+
});
134+
const throttledMarkdownSource = useThrottledValue(markdownSource, 100);
106135
const renderedMarkdown = createMemo(() => {
107136
if (!renderMarkdown() || p().type !== "text") return null;
108-
const text = "text" in p() ? String((p() as { text: string }).text ?? "") : "";
137+
const text = throttledMarkdownSource();
109138
if (!text.trim()) return "";
110139

111140
try {
@@ -124,6 +153,122 @@ export default function PartView(props: Props) {
124153
}
125154
});
126155

156+
const toolData = () => {
157+
if (p().type !== "tool") return null;
158+
return p() as any;
159+
};
160+
161+
const toolState = () => toolData()?.state ?? {};
162+
const toolName = () => (toolData()?.tool ? String(toolData()?.tool) : "tool");
163+
const toolTitle = () => (toolState()?.title ? String(toolState().title) : toolName());
164+
const toolStatus = () => (toolState()?.status ? String(toolState().status) : "unknown");
165+
const toolSubtitle = () =>
166+
toolState()?.subtitle || toolState()?.detail || toolState()?.summary
167+
? String(toolState().subtitle ?? toolState().detail ?? toolState().summary)
168+
: "";
169+
170+
const extractDiff = () => {
171+
const state = toolState();
172+
const candidates = [state?.diff, state?.patch, state?.output];
173+
for (const candidate of candidates) {
174+
if (typeof candidate !== "string") continue;
175+
if (candidate.includes("@@") || candidate.includes("+++ ") || candidate.includes("--- ")) {
176+
return candidate;
177+
}
178+
}
179+
return null;
180+
};
181+
182+
const diffText = createMemo(() => (p().type === "tool" ? extractDiff() : null));
183+
const diffLines = createMemo(() => (diffText() ? diffText()!.split("\n") : []));
184+
const diffLineClass = (line: string) => {
185+
if (line.startsWith("+")) return "text-green-11 bg-green-1/40";
186+
if (line.startsWith("-")) return "text-red-11 bg-red-1/40";
187+
if (line.startsWith("@@")) return "text-blue-11 bg-blue-1/30";
188+
return "text-gray-12";
189+
};
190+
191+
const toolOutput = () => {
192+
const output = toolState()?.output;
193+
return typeof output === "string" ? output : null;
194+
};
195+
196+
const toolError = () => {
197+
const error = toolState()?.error;
198+
return typeof error === "string" ? error : null;
199+
};
200+
201+
const toolInput = () => toolState()?.input;
202+
203+
const diagnostics = () => {
204+
const items = toolState()?.diagnostics;
205+
return Array.isArray(items) ? items : [];
206+
};
207+
208+
const formatDiagnosticLocation = (diagnostic: any) => {
209+
const raw = diagnostic?.file ?? diagnostic?.path ?? diagnostic?.uri ?? "";
210+
const file = typeof raw === "string" ? raw.replace(/^file:\/\//, "") : "";
211+
const line = diagnostic?.line ?? diagnostic?.range?.start?.line;
212+
const character = diagnostic?.character ?? diagnostic?.range?.start?.character;
213+
const location =
214+
typeof line === "number"
215+
? `${line + 1}${typeof character === "number" ? `:${character + 1}` : ""}`
216+
: "";
217+
return `${file}${file && location ? ":" : ""}${location}`.trim();
218+
};
219+
220+
const formatDiagnosticLabel = (diagnostic: any) => {
221+
const severity = diagnostic?.severity ?? diagnostic?.level;
222+
if (typeof severity === "string") return severity;
223+
if (severity === 1) return "error";
224+
if (severity === 2) return "warning";
225+
if (severity === 3) return "info";
226+
if (severity === 4) return "hint";
227+
return "diagnostic";
228+
};
229+
230+
const isLargeOutput = createMemo(() => {
231+
const output = toolOutput();
232+
return !!output && output.length > 800;
233+
});
234+
235+
const [expandedOutput, setExpandedOutput] = createSignal(false);
236+
const outputPreview = createMemo(() => {
237+
const output = toolOutput();
238+
if (!output) return "";
239+
if (isLargeOutput() && !expandedOutput()) {
240+
return `${output.slice(0, 800)}\n\n… (truncated)`;
241+
}
242+
return output;
243+
});
244+
245+
const toolImages = () => {
246+
const state = toolState();
247+
const candidates = Array.isArray(state?.images) ? state.images : [];
248+
return candidates
249+
.map((item: any) => {
250+
if (typeof item === "string") return { src: item, alt: "" };
251+
const src = item?.url ?? item?.src ?? item?.data;
252+
if (!src) return null;
253+
if (item?.data && item?.mediaType && !String(item.data).startsWith("data:")) {
254+
return { src: `data:${item.mediaType};base64,${item.data}`, alt: item?.alt ?? "" };
255+
}
256+
return { src, alt: item?.alt ?? "" };
257+
})
258+
.filter(Boolean);
259+
};
260+
261+
const inlineImage = () => {
262+
if (p().type !== "image") return null;
263+
const record = p() as any;
264+
const src = record?.url ?? record?.src ?? record?.data ?? record?.source;
265+
if (!src) return null;
266+
if (record?.data && record?.mediaType && !String(record.data).startsWith("data:")) {
267+
return `data:${record.mediaType};base64,${record.data}`;
268+
}
269+
return src as string;
270+
};
271+
127272
return (
128273
<Switch>
129274
<Match when={p().type === "text"}>
@@ -188,64 +333,147 @@ export default function PartView(props: Props) {
188333

189334
<Match when={p().type === "tool"}>
190335
<Show when={toolOnly()}>
191-
<div class="grid gap-2">
192-
<div class="flex items-center justify-between gap-3">
193-
<div
194-
class={`text-xs font-medium ${tone() === "dark" ? "text-gray-1" : "text-gray-12"}`.trim()}
195-
>
196-
Tool · {("tool" in p() ? String((p() as { tool: string }).tool) : "unknown")}
336+
<div class="grid gap-3">
337+
<div class="flex items-start justify-between gap-3">
338+
<div class="space-y-1">
339+
<div
340+
class={`text-xs font-medium ${tone() === "dark" ? "text-gray-1" : "text-gray-12"}`.trim()}
341+
>
342+
{toolTitle()}
343+
</div>
344+
<div class={`text-[11px] ${subtleTextClass()}`.trim()}>{toolName()}</div>
197345
</div>
198346
<div
199347
class={`rounded-full px-2 py-0.5 text-[11px] font-medium ${
200-
"state" in p() && (p() as any).state?.status === "completed"
348+
toolStatus() === "completed"
201349
? "bg-green-3/15 text-green-12"
202-
: "state" in p() && (p() as any).state?.status === "running"
350+
: toolStatus() === "running"
203351
? "bg-blue-3/15 text-blue-12"
204-
: "state" in p() && (p() as any).state?.status === "error"
352+
: toolStatus() === "error"
205353
? "bg-red-3/15 text-red-12"
206354
: "bg-gray-2/10 text-gray-1"
207355
}`}
208356
>
209-
{("state" in p() ? String((p() as any).state?.status ?? "unknown") : "unknown")}
357+
{toolStatus()}
210358
</div>
211359
</div>
212360

213-
<Show when={"state" in p() && (p() as any).state?.title}>
214-
<div class={`text-xs ${subtleTextClass()}`.trim()}>{String((p() as any).state.title)}</div>
361+
<Show when={toolSubtitle()}>
362+
<div class={`text-xs ${subtleTextClass()}`.trim()}>{toolSubtitle()}</div>
363+
</Show>
364+
365+
<Show when={diagnostics().length > 0}>
366+
<div class={`rounded-lg border ${panelBgClass()} p-2`.trim()}>
367+
<div class={`text-[11px] font-medium ${subtleTextClass()}`.trim()}>Diagnostics</div>
368+
<div class="mt-2 grid gap-2">
369+
<For each={diagnostics()}>
370+
{(diag: any) => (
371+
<div class="flex items-start justify-between gap-4 text-xs">
372+
<div>
373+
<div class="font-medium text-gray-12">{String(diag?.message ?? "")}</div>
374+
<Show when={diag?.source || diag?.code}>
375+
<div class="text-[11px] text-gray-10">
376+
{[diag?.source, diag?.code].filter(Boolean).join(" · ")}
377+
</div>
378+
</Show>
379+
</div>
380+
<div class="text-[11px] text-gray-10 text-right">
381+
<div>{formatDiagnosticLabel(diag)}</div>
382+
<Show when={formatDiagnosticLocation(diag)}>
383+
<div>{formatDiagnosticLocation(diag)}</div>
384+
</Show>
385+
</div>
386+
</div>
387+
)}
388+
</For>
389+
</div>
390+
</div>
215391
</Show>
216392

217-
<Show when={showToolOutput() && (p() as any).state?.output && typeof (p() as any).state.output === "string"}>
393+
<Show when={diffText()}>
394+
<div class={`rounded-lg border ${panelBgClass()} p-2`.trim()}>
395+
<div class={`text-[11px] font-medium ${subtleTextClass()}`.trim()}>Diff</div>
396+
<div class="mt-2 grid gap-1 rounded-md overflow-hidden">
397+
<For each={diffLines()}>
398+
{(line) => (
399+
<div
400+
class={`font-mono text-[11px] leading-relaxed px-2 py-0.5 whitespace-pre-wrap break-words ${diffLineClass(
401+
line,
402+
)}`.trim()}
403+
>
404+
{line || " "}
405+
</div>
406+
)}
407+
</For>
408+
</div>
409+
</div>
410+
</Show>
411+
412+
<Show when={toolImages().length > 0}>
413+
<div class="grid gap-2">
414+
<For each={toolImages()}>
415+
{(image: any) => (
416+
<img
417+
src={image.src}
418+
alt={image.alt || ""}
419+
class="max-w-full h-auto rounded-lg border border-gray-6/50"
420+
/>
421+
)}
422+
</For>
423+
</div>
424+
</Show>
425+
426+
<Show when={toolError()}>
427+
<div class="rounded-lg bg-red-1/40 p-2 text-xs text-red-12">
428+
{toolError()}
429+
</div>
430+
</Show>
431+
432+
<Show when={showToolOutput() && toolOutput() && toolOutput() !== diffText()}>
218433
<pre
219434
class={`whitespace-pre-wrap break-words rounded-lg ${panelBgClass()} p-2 text-xs ${
220435
tone() === "dark" ? "text-gray-12" : "text-gray-1"
221436
}`.trim()}
222437
>
223-
{clampText(String((p() as any).state.output))}
438+
{outputPreview()}
224439
</pre>
225440
</Show>
226441

227-
<Show when={showToolOutput() && (p() as any).state?.error && typeof (p() as any).state.error === "string"}>
228-
<div class="rounded-lg bg-red-1/40 p-2 text-xs text-red-12">
229-
{String((p() as any).state.error)}
230-
</div>
442+
<Show when={showToolOutput() && isLargeOutput()}>
443+
<button
444+
class={`text-[11px] ${subtleTextClass()} hover:text-gray-12 transition-colors`}
445+
onClick={() => setExpandedOutput((current) => !current)}
446+
>
447+
{expandedOutput() ? "Show less" : "Show more"}
448+
</button>
231449
</Show>
232450

233-
<Show when={showToolOutput() && (p() as any).state?.input != null}>
451+
<Show when={showToolOutput() && toolInput() != null}>
234452
<details class={`rounded-lg ${panelBgClass()} p-2`.trim()}>
235453
<summary class={`cursor-pointer text-xs ${subtleTextClass()}`.trim()}>Input</summary>
236454
<pre
237455
class={`mt-2 whitespace-pre-wrap break-words text-xs ${
238456
tone() === "dark" ? "text-gray-12" : "text-gray-1"
239457
}`.trim()}
240458
>
241-
{safeStringify((p() as any).state.input)}
459+
{safeStringify(toolInput())}
242460
</pre>
243461
</details>
244462
</Show>
245463
</div>
246464
</Show>
247465
</Match>
248466

467+
<Match when={p().type === "image"}>
468+
<Show when={inlineImage()}>
469+
<img
470+
src={inlineImage()!}
471+
alt=""
472+
class="max-w-full h-auto rounded-xl border border-gray-6/50"
473+
/>
474+
</Show>
475+
</Match>
476+
249477
<Match when={p().type === "step-start" || p().type === "step-finish"}>
250478
<div class={`text-xs ${subtleTextClass()}`.trim()}>
251479
{p().type === "step-start" ? "Step started" : "Step finished"}
@@ -270,4 +498,4 @@ export default function PartView(props: Props) {
270498
</Match>
271499
</Switch>
272500
);
273-
}
501+
}

0 commit comments

Comments
 (0)