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 | 39x 15x 25x 17x 12x 12x 8x 33x 28x 25x 25x 25x 7x 7x 6x 2x 4x 6x 2x 2x 1x 1x 1x 1x 2x 2x 4x 4x 2x 25x 14x 12x 12x 10x 10x 10x 10x 12x 12x 12x 12x 12x 12x | import { Block, ChatbotPendingAction, ChatbotSharedState, SuggestionChip } from "@/types/chat";
type BackendBlock = Record<string, unknown>;
export function toSuggestionChips(raw: unknown): SuggestionChip[] {
if (!Array.isArray(raw)) return [];
return raw.flatMap((entry): SuggestionChip[] => {
if (typeof entry === "string" && entry.trim()) return [{ label: entry, value: entry }];
if (!entry || typeof entry !== "object") return [];
const record = entry as Record<string, unknown>;
if (typeof record.label !== "string" || !record.label.trim()) return [];
return [
{
label: record.label,
value:
typeof record.value === "string" && record.value.trim() ? record.value : record.label,
},
];
});
}
export function mapBackendBlocks(blocks: unknown[]): Block[] {
return blocks.reduce<Block[]>((result, block) => {
if (!block || typeof block !== "object") return result;
const backendBlock = block as BackendBlock;
let mapped: Block[] = [];
switch (backendBlock.type) {
case "assistant-message":
case "text":
mapped = [
{
type: "text",
text: typeof backendBlock.text === "string" ? backendBlock.text : "",
isStreaming: backendBlock.isStreaming === true,
},
];
break;
case "table":
mapped = [
{
type: "table",
// The agent names the list it returned ("Manager List", "Staff on
// shift"); without this the sheet has to invent a heading.
...(typeof backendBlock.title === "string" && backendBlock.title.trim()
? { title: backendBlock.title.trim() }
: {}),
rows: (Array.isArray(backendBlock.rows) ? backendBlock.rows : []).map((row) =>
Object.fromEntries(
(Array.isArray(backendBlock.columns) ? backendBlock.columns : []).map((col, i) => [
String(col),
Array.isArray(row) ? row[i] : undefined,
]),
),
),
},
];
break;
case "status":
mapped = [
{
type: "status",
status: backendBlock.status === "success" ? "success" : "in_progress",
description: typeof backendBlock.text === "string" ? backendBlock.text : undefined,
},
];
break;
case "pending-confirmation":
mapped = [
{
type: "confirmation",
title: "Please review this request",
description:
typeof backendBlock.summary === "string" ? backendBlock.summary : undefined,
},
];
break;
case "suggestion":
mapped = [{ type: "suggestions", items: toSuggestionChips(backendBlock.suggestions) }];
break;
case "suggestions":
mapped = [
{
type: "suggestions",
items: toSuggestionChips(backendBlock.items ?? backendBlock.suggestions),
},
];
break;
case "error":
mapped = [
{
type: "error",
text:
typeof backendBlock.message === "string"
? backendBlock.message
: "Something went wrong.",
},
];
break;
default:
break;
}
return result.concat(mapped);
}, []);
}
export function sharedStateFromSnapshot(snapshot: unknown): ChatbotSharedState | null {
if (!snapshot || typeof snapshot !== "object") return null;
const record = snapshot as Record<string, unknown>;
if (typeof record.scope !== "string") return null;
const raw = record.pendingAction;
const rawRecord = raw && typeof raw === "object" ? (raw as Record<string, unknown>) : null;
const pendingAction =
rawRecord && typeof rawRecord.id === "string" && typeof rawRecord.actionType === "string"
? {
id: rawRecord.id,
actionType: rawRecord.actionType,
...(typeof rawRecord.summary === "string" ? { summary: rawRecord.summary } : {}),
...(typeof rawRecord.expiresAt === "string" ? { expiresAt: rawRecord.expiresAt } : {}),
...(typeof rawRecord.createdAt === "string" ? { createdAt: rawRecord.createdAt } : {}),
}
: null;
return {
scope: record.scope,
pendingAction,
canMutate: record.canMutate === true,
hasCachedImage: record.hasCachedImage === true,
suggestions: toSuggestionChips(record.suggestions),
};
}
export function pendingFromOutcome(outcome: unknown): ChatbotPendingAction | null {
const record =
outcome && typeof outcome === "object" ? (outcome as Record<string, unknown>) : null;
const interrupts = record?.interrupts;
const interrupt =
record?.type === "interrupt" && Array.isArray(interrupts) ? interrupts[0] : null;
const interruptRecord =
interrupt && typeof interrupt === "object" ? (interrupt as Record<string, unknown>) : null;
const metadata =
interruptRecord?.metadata && typeof interruptRecord.metadata === "object"
? (interruptRecord.metadata as Record<string, unknown>)
: null;
return typeof interruptRecord?.id === "string"
? {
id: interruptRecord.id,
type: typeof metadata?.actionType === "string" ? metadata.actionType : "mutation",
status: "awaiting_confirmation",
summary: interruptRecord.message,
}
: null;
}
|