Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | 50x 2x 48x 48x 48x 1x 1x 2x 1x 1x 1x 1x 1x 8x 6x 19x 3x 3x 8x 8x 6x 6x 2x 1x 1x 6x 1x 5x 5x 1x 4x 4x 3x 3x 2x 4x 4x 10x 3x 7x 7x 7x 1x 1x 6x 5x 5x 3x 7x 19x 19x 6x 6x 5x 5x 3x 3x 19x 19x 26x | import type { AgentEvent, AgentSharedState, Block, Suggestion, TurnMetadata } from "./events";
/**
* Map ONE raw AG-UI protocol event (as JSON from the SSE frame) into zero or
* more normalized `AgentEvent`s. Matches on the `type` string literals so this
* stays free of any `@ag-ui/*` import — the backend's event names are the only
* contract. Unknown events map to `[]` (ignored).
*
* A RUN_FINISHED carrying an interrupt outcome yields TWO events: the
* `PendingAction` (drives the HITL card) followed by `RunFinished`.
*/
export function mapAguiEvent(raw: unknown): AgentEvent[] {
if (!raw || typeof raw !== "object") {
return [];
}
const event = raw as Record<string, unknown>;
const type = typeof event.type === "string" ? event.type : "";
switch (type) {
case "RUN_STARTED":
return [
{
_tag: "RunStarted",
threadId: str(event.threadId),
runId: str(event.runId),
},
];
case "TEXT_MESSAGE_START":
return [{ _tag: "TextStart", messageId: str(event.messageId) }];
case "TEXT_MESSAGE_CONTENT":
return [
{
_tag: "TextDelta",
messageId: str(event.messageId),
delta: str(event.delta),
},
];
case "TEXT_MESSAGE_END":
return [{ _tag: "TextEnd", messageId: str(event.messageId) }];
case "TOOL_CALL_START":
return [
{
_tag: "ToolCallStart",
toolCallId: str(event.toolCallId),
name: str(event.toolCallName),
},
];
case "TOOL_CALL_ARGS":
return [
{
_tag: "ToolCallArgs",
toolCallId: str(event.toolCallId),
argsDelta: str(event.delta),
},
];
case "TOOL_CALL_END":
return [{ _tag: "ToolCallEnd", toolCallId: str(event.toolCallId) }];
case "TOOL_CALL_RESULT":
return [
{
_tag: "ToolResult",
toolCallId: str(event.toolCallId),
content: str(event.content),
},
];
case "CUSTOM":
return mapCustom(str(event.name), event.value);
case "STATE_SNAPSHOT":
return mapStateSnapshot(event.snapshot);
case "RUN_FINISHED":
return mapRunFinished(event.outcome);
case "RUN_ERROR":
return [
{
_tag: "RunError",
message: str(event.message) || "Agent run failed.",
},
];
default:
return [];
}
}
function mapCustom(name: string, value: unknown): AgentEvent[] {
const record = value && typeof value === "object" ? (value as Record<string, unknown>) : {};
if (name === "message.blocks") {
const blocks = Array.isArray(record.blocks) ? (record.blocks as Block[]) : [];
return [
{
_tag: "Blocks",
blocks,
suggestions: toSuggestions(record.suggestions),
},
];
}
if (name === "message.metadata") {
return [{ _tag: "Metadata", metadata: record as unknown as TurnMetadata }];
}
return [];
}
/**
* The snapshot is the backend's curated AgentSharedState. A malformed payload
* maps to [] (same policy as unknown events) — never a crashed stream.
*/
function mapStateSnapshot(snapshot: unknown): AgentEvent[] {
if (!snapshot || typeof snapshot !== "object") {
return [];
}
const record = snapshot as Record<string, unknown>;
if (typeof record.scope !== "string") {
return [];
}
let pendingAction: AgentSharedState["pendingAction"] = null;
if (record.pendingAction && typeof record.pendingAction === "object") {
const raw = record.pendingAction as Record<string, unknown>;
if (typeof raw.id === "string" && typeof raw.actionType === "string") {
pendingAction = {
id: raw.id,
actionType: raw.actionType,
...(typeof raw.summary === "string" ? { summary: raw.summary } : {}),
...(typeof raw.expiresAt === "string" ? { expiresAt: raw.expiresAt } : {}),
};
}
}
const state: AgentSharedState = {
scope: record.scope,
pendingAction,
canMutate: record.canMutate === true,
hasCachedImage: record.hasCachedImage === true,
suggestions: toSuggestions(record.suggestions),
};
return [{ _tag: "SharedState", state }];
}
/**
* Normalize chips: {label, value} objects from the current backend, plain
* strings from older builds (label === value). Malformed entries are dropped
* (same policy as unknown events) — never a crashed stream.
*/
function toSuggestions(raw: unknown): Suggestion[] {
if (!Array.isArray(raw)) {
return [];
}
const chips: Suggestion[] = [];
for (const entry of raw) {
if (typeof entry === "string" && entry.length > 0) {
chips.push({ label: entry, value: entry });
continue;
}
if (entry && typeof entry === "object") {
const record = entry as Record<string, unknown>;
if (typeof record.label === "string" && record.label.length > 0) {
chips.push({
label: record.label,
value:
typeof record.value === "string" && record.value.length > 0
? record.value
: record.label,
});
}
}
}
return chips;
}
function mapRunFinished(outcome: unknown): AgentEvent[] {
const events: AgentEvent[] = [];
if (outcome && typeof outcome === "object") {
const record = outcome as Record<string, unknown>;
if (record.type === "interrupt" && Array.isArray(record.interrupts)) {
const first = record.interrupts[0] as Record<string, unknown> | undefined;
if (first && typeof first.id === "string") {
const metadata =
first.metadata && typeof first.metadata === "object"
? (first.metadata as Record<string, unknown>)
: {};
events.push({
_tag: "PendingAction",
pending: {
pendingActionId: first.id,
actionType: typeof metadata.actionType === "string" ? metadata.actionType : undefined,
message: typeof first.message === "string" ? first.message : undefined,
},
});
}
}
}
events.push({ _tag: "RunFinished" });
return events;
}
function str(value: unknown): string {
return typeof value === "string" ? value : "";
}
|