Skip to content

Commit ad7fd6a

Browse files
committed
Fixed more comments
1 parent dadd006 commit ad7fd6a

3 files changed

Lines changed: 82 additions & 31 deletions

File tree

examples/agentic/client/src/app/transport-helpers.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -276,18 +276,39 @@ export const readOutcomeFromEvent = (event: TransportEvent): OutcomeSummary | nu
276276
};
277277

278278
type UiChunkMessage = Extract<TransportEvent["message"], { type: "ui.chunk" }>;
279-
type UiChunk = UiChunkMessage["chunk"];
280279

281-
const readUiChunkType = (chunk: UiChunk) => chunk.type;
280+
const readChunkType = (chunk: unknown) => {
281+
if (!chunk || typeof chunk !== "object") {
282+
return null;
283+
}
284+
if (!("type" in chunk)) {
285+
return null;
286+
}
287+
const value = (chunk as { type?: unknown }).type;
288+
return typeof value === "string" ? value : null;
289+
};
282290

283-
const isUiChunk = (message: TransportEvent["message"]): message is UiChunkMessage =>
284-
message.type === "ui.chunk";
291+
const isUiChunk = (message: TransportEvent["message"]): message is UiChunkMessage => {
292+
if (!message || typeof message !== "object") {
293+
return false;
294+
}
295+
if (!("type" in message)) {
296+
return false;
297+
}
298+
if ((message as { type?: unknown }).type !== "ui.chunk") {
299+
return false;
300+
}
301+
return "chunk" in message;
302+
};
285303

286-
const isDataChunk = (chunk: UiChunk) => readUiChunkType(chunk).startsWith("data-");
304+
const isDataChunk = (chunk: unknown) => {
305+
const chunkType = readChunkType(chunk);
306+
return typeof chunkType === "string" && chunkType.startsWith("data-");
307+
};
287308

288309
const readArrayLength = (value: unknown) => (Array.isArray(value) ? value.length : 0);
289310

290-
const readChunkData = (chunk: UiChunk): unknown | null => {
311+
const readChunkData = (chunk: unknown): unknown | null => {
291312
if (!chunk || typeof chunk !== "object") {
292313
return null;
293314
}
@@ -315,8 +336,7 @@ const readSourcesSummary = (data: unknown) => {
315336
return count > 0 ? `(${count})` : "";
316337
};
317338

318-
const formatDataChunk = (chunk: UiChunk) => {
319-
const chunkType = readUiChunkType(chunk);
339+
const formatDataChunk = (chunk: unknown, chunkType: string) => {
320340
if (chunkType === "data-diagnostic") {
321341
return `${chunkType}${readDiagnosticSummary(readChunkData(chunk))}`;
322342
}
@@ -326,11 +346,15 @@ const formatDataChunk = (chunk: UiChunk) => {
326346
return chunkType;
327347
};
328348

329-
const formatUiChunk = (chunk: UiChunk) => {
349+
const formatUiChunk = (chunk: unknown) => {
350+
const chunkType = readChunkType(chunk);
351+
if (!chunkType) {
352+
return "ui.chunk: invalid";
353+
}
330354
if (isDataChunk(chunk)) {
331-
return `ui.chunk: ${formatDataChunk(chunk)}`;
355+
return `ui.chunk: ${formatDataChunk(chunk, chunkType)}`;
332356
}
333-
return `ui.chunk: ${readUiChunkType(chunk)}`;
357+
return `ui.chunk: ${chunkType}`;
334358
};
335359

336360
export const formatTransportEvent = (event: TransportEvent) => {

examples/agentic/client/src/components/assistant-ui/tool-fallback.tsx

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,49 @@
11
import type { ToolCallMessagePartComponent, ToolCallMessagePartProps } from "@assistant-ui/react";
2+
import { bindFirst } from "@geekist/llm-core";
23
import { CheckIcon, ChevronDownIcon, ChevronUpIcon, XCircleIcon } from "lucide-react";
3-
import { useState } from "react";
4+
import type { Dispatch, SetStateAction } from "react";
5+
import { useId, useState } from "react";
46
import { Button } from "../ui/button";
57
import { cn } from "../../lib/utils";
68

79
type ToolFallbackProps = ToolCallMessagePartProps;
810

11+
const applyToggleCollapsed = (value: boolean) => !value;
12+
13+
const toggleCollapsed = (setIsCollapsed: Dispatch<SetStateAction<boolean>>) => {
14+
setIsCollapsed(applyToggleCollapsed);
15+
return true;
16+
};
17+
18+
const safeStringify = (value: unknown) => {
19+
if (typeof value === "string") {
20+
return value;
21+
}
22+
try {
23+
return JSON.stringify(value);
24+
} catch {
25+
return String(value);
26+
}
27+
};
28+
29+
const safeStringifyPretty = (value: unknown) => {
30+
if (typeof value === "string") {
31+
return value;
32+
}
33+
try {
34+
return JSON.stringify(value, null, 2);
35+
} catch {
36+
return safeStringify(value);
37+
}
38+
};
39+
940
const ToolFallbackImpl = ({ toolName, argsText, result, status }: ToolFallbackProps) => {
1041
const [isCollapsed, setIsCollapsed] = useState(true);
42+
const contentId = useId();
43+
const handleToggleCollapsed = bindFirst(toggleCollapsed, setIsCollapsed);
1144

1245
const isCancelled = status?.type === "incomplete" && status.reason === "cancelled";
13-
const cancelledReason =
14-
isCancelled && status.error
15-
? typeof status.error === "string"
16-
? status.error
17-
: JSON.stringify(status.error)
18-
: null;
46+
const cancelledReason = isCancelled && status.error ? safeStringify(status.error) : null;
1947

2048
return (
2149
<div
@@ -40,21 +68,18 @@ const ToolFallbackImpl = ({ toolName, argsText, result, status }: ToolFallbackPr
4068
<b>{toolName}</b>
4169
</p>
4270
<Button
43-
onClick={() => setIsCollapsed(!isCollapsed)}
71+
onClick={handleToggleCollapsed}
4472
size="icon"
4573
variant="ghost"
4674
aria-expanded={!isCollapsed}
47-
aria-controls="tool-fallback-content"
75+
aria-controls={contentId}
4876
aria-label={isCollapsed ? "Expand tool details" : "Collapse tool details"}
4977
>
5078
{isCollapsed ? <ChevronUpIcon /> : <ChevronDownIcon />}
5179
</Button>
5280
</div>
5381
{!isCollapsed && (
54-
<div
55-
id="tool-fallback-content"
56-
className="aui-tool-fallback-content flex flex-col gap-2 border-t pt-2"
57-
>
82+
<div id={contentId} className="aui-tool-fallback-content flex flex-col gap-2 border-t pt-2">
5883
{cancelledReason && (
5984
<div className="aui-tool-fallback-cancelled-root px-4">
6085
<p className="aui-tool-fallback-cancelled-header font-semibold text-muted-foreground">
@@ -72,7 +97,7 @@ const ToolFallbackImpl = ({ toolName, argsText, result, status }: ToolFallbackPr
7297
<div className="aui-tool-fallback-result-root border-t border-dashed px-4 pt-2">
7398
<p className="aui-tool-fallback-result-header font-semibold">Result:</p>
7499
<pre className="aui-tool-fallback-result-content whitespace-pre-wrap">
75-
{typeof result === "string" ? result : JSON.stringify(result, null, 2)}
100+
{safeStringifyPretty(result)}
76101
</pre>
77102
</div>
78103
)}

src/interaction/agent-runtime-subagents-helpers.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,18 @@ export const buildSubagentCompleted = (record: SubagentRecord) =>
213213
export const buildSubagentFailed = (record: SubagentRecord, error?: string) =>
214214
buildSubagentEvent("failed", record, error);
215215

216-
export function applySubagentCompleted(record: SubagentRecord, outcome: Outcome<unknown>) {
216+
const applySubagentOutcomeRecord = (record: SubagentRecord, outcome: Outcome<unknown>) => {
217217
record.status = "idle";
218218
record.lastOutcome = outcome;
219219
return outcome;
220+
};
221+
222+
export function applySubagentCompleted(record: SubagentRecord, outcome: Outcome<unknown>) {
223+
return applySubagentOutcomeRecord(record, outcome);
220224
}
221225

222-
export function applySubagentFailed(record: SubagentRecord, error: unknown) {
223-
record.status = "idle";
224-
record.lastOutcome = toSubagentErrorOutcome(error);
225-
return error;
226+
export function applySubagentFailed(record: SubagentRecord, outcome: Outcome<unknown>) {
227+
return applySubagentOutcomeRecord(record, outcome);
226228
}
227229

228230
export function emitSubagentCompleted(input: { manager: SubagentManager; record: SubagentRecord }) {
@@ -244,7 +246,7 @@ export function applySubagentOutcome(
244246
if (outcome.status === "error") {
245247
const applyError = bindFirst(applySubagentFailed, input.record);
246248
const emitError = bindFirst(emitSubagentFailed, input);
247-
applyError(outcome.error);
249+
applyError(outcome);
248250
return maybeTap(emitError, outcome);
249251
}
250252
const applyCompleted = bindFirst(applySubagentCompleted, input.record);

0 commit comments

Comments
 (0)