@guuey/agent-client 0.2.3 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,85 +0,0 @@
1
- import { asGguiRenderBootstrap, gguiShellHtml, MCP_APP_AI_GGUI_RENDER_META_KEY, } from "@ggui-ai/protocol/integrations/mcp-apps";
2
- import { isJsonObject } from "./block-ui";
3
- export { asGguiRenderBootstrap, gguiShellHtml, } from "@ggui-ai/protocol/integrations/mcp-apps";
4
- /**
5
- * The `_meta` key the ggui render bootstrap rides on. Alias of the
6
- * protocol package's own constant — one spelling, owned upstream.
7
- */
8
- export const GGUI_RENDER_META_KEY = MCP_APP_AI_GGUI_RENDER_META_KEY;
9
- /** The `ui://` scheme prefix every ggui render resource uri carries. */
10
- const UI_SCHEME = "ui://";
11
- /**
12
- * A tool result's `uiData` (+ its `_meta`, when carried) → a ggui render
13
- * descriptor, or `undefined` for anything that is not one.
14
- *
15
- * The `ui://` scheme gate is deliberate: `uiData` is a general-purpose channel
16
- * (every `structuredContent` of a `_meta.ui`-stamped tool lands there), so a
17
- * bare `resourceUri` string is not on its own a claim of generative UI.
18
- */
19
- export function asGguiRender(uiData, meta) {
20
- if (!isJsonObject(uiData))
21
- return undefined;
22
- const resourceUri = uiData.resourceUri;
23
- if (typeof resourceUri !== "string" || !resourceUri.startsWith(UI_SCHEME))
24
- return undefined;
25
- const bootstrap = asGguiRenderBootstrap(meta);
26
- return {
27
- resourceUri,
28
- ...(typeof uiData.sessionId === "string" ? { sessionId: uiData.sessionId } : {}),
29
- ...(bootstrap ? { bootstrap } : {}),
30
- };
31
- }
32
- /**
33
- * A live `tool-result` AgBlock → its ggui render descriptor, if it is one.
34
- *
35
- * NOTE: `@ggui-ai/protocol/integrations/mcp-apps` exports a helper of the
36
- * same name that narrows a spec-canonical MCP `CallToolResult` instead. This
37
- * one is the silverprotocol-side twin — the input is the FOLDED block, whose
38
- * `uiData`/`_meta` carriage is `@silverprotocol/core`'s contract, not ggui's.
39
- */
40
- export function toolResultGguiRender(block) {
41
- return asGguiRender(block.uiData, block._meta);
42
- }
43
- /** An untyped (persisted-snapshot) block → its ggui render descriptor, if it is one. */
44
- export function blockGguiRender(block) {
45
- if (!isJsonObject(block))
46
- return undefined;
47
- if (block.type !== "tool-result")
48
- return undefined;
49
- return asGguiRender(block.uiData, block._meta);
50
- }
51
- /**
52
- * A ggui render descriptor → the mountable resource the host's existing
53
- * mcp-ui path already knows how to mount, or `undefined` when the descriptor
54
- * carries no bootstrap (history cards, and any fold that dropped `_meta`).
55
- *
56
- * The `uri` is the render's REAL `resourceUri` — the shell is the payload, not
57
- * a renaming of the resource.
58
- *
59
- * The shell is built `background: 'transparent'`: every guuey host that
60
- * mounts through this adapter (widget, portal web, Studio) draws its own
61
- * card chrome around the iframe, so the host page composits behind the card.
62
- * The upstream default (`'surface'`) is for standalone served documents —
63
- * see `GguiShellHtmlOptions` in `@ggui-ai/protocol/integrations/mcp-apps`.
64
- *
65
- * **On `_meta` being required to MOUNT (but never to RECOGNISE).** Recognition
66
- * — "this tool result is a ggui card" — is keyed on `uiData.resourceUri` alone
67
- * and never waits for anything (see {@link asGguiRender}); nothing in this
68
- * package is blocked on an upstream change. Mounting is different, and the
69
- * requirement is ggui's, not ours: its runtime rejects a slice without
70
- * `runtimeUrl` AND without at least one mode discriminator (`wsUrl`+`wsToken`,
71
- * `codeUrl`, or `kind`) as `MALFORMED_BOOTSTRAP` and renders nothing. `uiData`
72
- * carries none of those fields, so a bootstrap-less descriptor could only ever
73
- * produce a blank frame; returning `undefined` and letting the host show its
74
- * own placeholder is the honest answer, not a deferral. `@silverprotocol/core`'s
75
- * `Reducer` is what puts `_meta` on the block for a live turn, in-repo, today.
76
- */
77
- export function gguiRenderResource(render) {
78
- if (!render.bootstrap)
79
- return undefined;
80
- return {
81
- uri: render.resourceUri,
82
- mimeType: "text/html",
83
- text: gguiShellHtml(render.bootstrap, { background: "transparent" }),
84
- };
85
- }
package/src/block-ui.ts DELETED
@@ -1,212 +0,0 @@
1
- /**
2
- * Pure block-walk / resource-narrowing helpers for a block-preserving agent
3
- * transcript — no React, no DOM, so the narrowing logic stays unit-testable in
4
- * isolation (this package's vitest runs a `node` environment) and can be shared
5
- * by every host renderer (Studio's `AgentBlocks`, Portal-web's agent chat).
6
- *
7
- * The pod's AgJSON wire carries generative-UI payloads on `tool.done` events,
8
- * which the reducer folds onto `tool-result` blocks. Two channels reach us:
9
- *
10
- * 1. **`uiData`** — the MCP-Apps *surface* channel. The pod's Claude facet
11
- * routes a tool result's `structuredContent` here when the server stamped
12
- * `_meta.ui`. Any resource here is intended as UI.
13
- * 2. **`provider-raw` content blocks** — an MCP embedded `resource` content
14
- * part does NOT survive as a first-class `resource` AgBlock in the Claude
15
- * facet; it degrades to `{ type:'provider-raw', vendor, raw:<part> }`. So a
16
- * `ui://` resource can be hiding inside `provider-raw.raw` and must be
17
- * scanned for defensively.
18
- *
19
- * The resource-narrowing (opaque `JsonValue` → typed payload) mirrors the
20
- * proven `create-agentic-app` web template — structural validation, never a cast.
21
- */
22
- import type { AgBlock, AgMessage, JsonValue } from "@silverprotocol/core";
23
- import type { HistoryCard } from "./types";
24
-
25
- /** A narrowed MCP embedded UI resource (the `_meta.ui.resource` shape). */
26
- export interface McpUiResourcePayload {
27
- uri: string;
28
- mimeType?: string;
29
- text?: string;
30
- blob?: string;
31
- }
32
-
33
- /** Narrow an opaque `JsonValue` to a plain (non-array) JSON object. */
34
- export function isJsonObject(v: JsonValue | undefined): v is { [key: string]: JsonValue } {
35
- return typeof v === "object" && v !== null && !Array.isArray(v);
36
- }
37
-
38
- /**
39
- * A JSON object → an MCP UI resource, if it has a `uri` plus renderable
40
- * payload (`text` or base64 `blob`). Returns `undefined` for anything else.
41
- */
42
- export function asResourcePayload(v: JsonValue | undefined): McpUiResourcePayload | undefined {
43
- if (!isJsonObject(v)) return undefined;
44
- if (typeof v.uri !== "string") return undefined;
45
- if (typeof v.text !== "string" && typeof v.blob !== "string") return undefined;
46
- return {
47
- uri: v.uri,
48
- ...(typeof v.mimeType === "string" ? { mimeType: v.mimeType } : {}),
49
- ...(typeof v.text === "string" ? { text: v.text } : {}),
50
- ...(typeof v.blob === "string" ? { blob: v.blob } : {}),
51
- };
52
- }
53
-
54
- /**
55
- * Does a `tool-result` block's `uiData` carry an MCP embedded UI resource?
56
- * Accepts the resource inlined directly, or wrapped as `{ resource: {...} }`
57
- * (the shape an MCP `resource` content part carries). No `ui://` scheme gate
58
- * here on purpose: `uiData` is the explicit *surface* channel (the server
59
- * stamped `_meta.ui`), so any resource on it is meant to render.
60
- */
61
- export function asUiResource(uiData: JsonValue | undefined): McpUiResourcePayload | undefined {
62
- if (!isJsonObject(uiData)) return undefined;
63
- const direct = asResourcePayload(uiData);
64
- if (direct) return direct;
65
- return asResourcePayload(uiData.resource);
66
- }
67
-
68
- /**
69
- * Scan a `provider-raw` block's `raw` (the vendor tool_result content part)
70
- * for a *generative-UI* resource. Unlike {@link asUiResource}, this path IS
71
- * gated on the `ui://` scheme: `provider-raw` degradation is a lossy catch-all,
72
- * so a plain file/text resource riding it is NOT a UI to mount — only the
73
- * mcp-ui `ui://` convention is.
74
- */
75
- export function scanProviderRawForUiResource(
76
- raw: JsonValue | undefined,
77
- ): McpUiResourcePayload | undefined {
78
- if (!isJsonObject(raw)) return undefined;
79
- const candidate =
80
- raw.resource !== undefined ? asResourcePayload(raw.resource) : asResourcePayload(raw);
81
- if (!candidate) return undefined;
82
- return candidate.uri.startsWith("ui://") ? candidate : undefined;
83
- }
84
-
85
- /**
86
- * Extract a mountable UI resource from an opaque AgBlock-shaped `JsonValue`
87
- * (used for persisted card snapshot parts, which arrive untyped). Dispatches
88
- * by `block.type`:
89
- * - `tool-result` → its `uiData` surface channel, then a `ui://` resource
90
- * degraded into a `provider-raw` content part — the SAME two channels
91
- * the live path ({@link toolResultUiResource}) mounts. The write side
92
- * (`nocode-runtime`'s `uiCardArtifactsFromMessages`, guuey#86) persists
93
- * card rows for both, so the snapshot arm must mount both or
94
- * provider-raw-only cards rehydrate as placeholders.
95
- * - `provider-raw` → a `ui://` resource hiding in `raw`
96
- * - `resource` → a first-class embedded resource (gated on `ui://`)
97
- * Everything else → `undefined`.
98
- */
99
- export function blockUiResource(block: JsonValue): McpUiResourcePayload | undefined {
100
- if (!isJsonObject(block)) return undefined;
101
- switch (block.type) {
102
- case "tool-result": {
103
- const fromUiData = asUiResource(block.uiData);
104
- if (fromUiData) return fromUiData;
105
- if (Array.isArray(block.content)) {
106
- for (const part of block.content) {
107
- if (isJsonObject(part) && part.type === "provider-raw") {
108
- const found = scanProviderRawForUiResource(part.raw);
109
- if (found) return found;
110
- }
111
- }
112
- }
113
- return undefined;
114
- }
115
- case "provider-raw":
116
- return scanProviderRawForUiResource(block.raw);
117
- case "resource": {
118
- const r = asResourcePayload(block.resource);
119
- return r && r.uri.startsWith("ui://") ? r : undefined;
120
- }
121
- default:
122
- return undefined;
123
- }
124
- }
125
-
126
- /**
127
- * A live `tool-result` AgBlock → its mountable UI resource, checking BOTH
128
- * channels the Claude facet uses:
129
- * 1. the `uiData` surface channel (server stamped `_meta.ui`), and
130
- * 2. an embedded `ui://` resource that degraded into a `provider-raw`
131
- * content part inside the tool result (MCP `resource` parts do NOT survive
132
- * as first-class `resource` AgBlocks here).
133
- * First-class `resource` content parts are intentionally not scanned in this
134
- * typed live path (the Claude facet never emits them); the untyped card path
135
- * ({@link blockUiResource}) covers them for other facets' persisted snapshots.
136
- */
137
- export function toolResultUiResource(
138
- block: Extract<AgBlock, { type: "tool-result" }>,
139
- ): McpUiResourcePayload | undefined {
140
- const fromUiData = asUiResource(block.uiData);
141
- if (fromUiData) return fromUiData;
142
- for (const part of block.content) {
143
- if (part.type === "provider-raw") {
144
- const found = scanProviderRawForUiResource(part.raw);
145
- if (found) return found;
146
- }
147
- }
148
- return undefined;
149
- }
150
-
151
- /**
152
- * A persisted `HistoryCard`'s `cardSnapshot` → a mountable UI resource. The
153
- * snapshot is the verbatim `AgArtifact` the pod stored (`{ parts: AgBlock[] }`),
154
- * so walk its `parts` for the first block that yields a resource; fall back to
155
- * treating the snapshot root itself as a block.
156
- *
157
- * NOTE (`no-ggui-tools`): a ggui-rendered card carries NO inline HTML resource —
158
- * its UI rides `_meta.ggui.bootstrap` and mounts via `@ggui-ai/react`'s
159
- * `McpAppIframe`. That branch is OUT OF SCOPE for v1 (deferred-pending-capture).
160
- * So a real ggui card resolves to `undefined` here and renders as the host's
161
- * coherent placeholder, not a broken mount.
162
- */
163
- export function cardUiResource(cardSnapshot: JsonValue): McpUiResourcePayload | undefined {
164
- if (!isJsonObject(cardSnapshot)) return undefined;
165
- const parts = cardSnapshot.parts;
166
- if (Array.isArray(parts)) {
167
- for (const part of parts) {
168
- const found = blockUiResource(part);
169
- if (found) return found;
170
- }
171
- }
172
- return blockUiResource(cardSnapshot);
173
- }
174
-
175
- /**
176
- * The resource's HTML: inline `text` wins; else base64-decode `blob`. `atob`
177
- * alone yields a Latin-1 string (mojibake on multibyte UTF-8), so decode via
178
- * bytes + `TextDecoder`. Invalid base64 → `undefined` (no renderable payload).
179
- */
180
- export function resourceHtml(resource: McpUiResourcePayload): string | undefined {
181
- if (resource.text !== undefined) return resource.text;
182
- if (resource.blob !== undefined) {
183
- try {
184
- return new TextDecoder().decode(Uint8Array.from(atob(resource.blob), (c) => c.charCodeAt(0)));
185
- } catch {
186
- return undefined;
187
- }
188
- }
189
- return undefined;
190
- }
191
-
192
- /**
193
- * The tool name for a `tool-result` block, read off its paired `tool-call`
194
- * block in the same message (the reducer keeps both in one message's content).
195
- * Falls back to `"tool"` when the pair is missing.
196
- */
197
- export function toolNameFor(message: AgMessage, toolCallId: string): string {
198
- for (const b of message.content) {
199
- if (b.type === "tool-call" && b.toolCallId === toolCallId) return b.name;
200
- }
201
- return "tool";
202
- }
203
-
204
- /**
205
- * Persisted cards, ascending by transcript `seq` (stable; input untouched).
206
- * These are PRIOR-turn cards — they always precede the live fold, so a
207
- * block-preserving renderer surfaces them first (e.g. under an "Earlier in
208
- * this conversation" divider).
209
- */
210
- export function sortHistoryCards(cards: readonly HistoryCard[]): HistoryCard[] {
211
- return [...cards].sort((a, b) => a.seq - b.seq);
212
- }
package/src/card-mount.ts DELETED
@@ -1,101 +0,0 @@
1
- /**
2
- * The card-mount dispatcher: ONE narrowing that answers "what, if anything,
3
- * does this block mount?" across BOTH generative-UI channels a guuey pod
4
- * emits.
5
- *
6
- * 1. **inline mcp-ui resource** — `{uri, text|blob}` on `uiData`, or a
7
- * `ui://` resource degraded into a `provider-raw` content part. Handled
8
- * verbatim by `block-ui.ts`; this module does not touch that path, it
9
- * only tries it FIRST.
10
- * 2. **ggui render** — `uiData.resourceUri` + the `_meta["ai.ggui/render"]`
11
- * bootstrap, mounted through ggui's self-contained shell. See
12
- * `ggui-render.ts`.
13
- *
14
- * Both channels land on the SAME `McpUiResourcePayload`, which is the whole
15
- * point: a host that already mounts inline resources through
16
- * `@mcp-ui/client`'s `AppRenderer` in a second-origin sandbox gains ggui cards
17
- * without a second mount mechanism, a second iframe contract, or a second
18
- * security posture to review.
19
- *
20
- * Precedence is inline-first and deliberate: an inline resource is the
21
- * server's explicit, self-sufficient HTML. A ggui render only ever wins when
22
- * there is no inline resource to prefer, so this dispatcher can never change
23
- * what an existing inline card renders.
24
- *
25
- * ## Why the CHANNEL is returned alongside the resource
26
- *
27
- * The payload alone cannot say where it came from — a ggui shell is a string
28
- * of HTML like any other. But a host has one decision that genuinely depends
29
- * on the origin of that HTML: WHICH sandbox host page to mount it in. A ggui
30
- * shell must load ggui's runtime bundle and open its WSS, so it needs a page
31
- * whose CSP names the ggui origins; an inline card is arbitrary tenant HTML
32
- * and must keep the self-only page it has always had. Handing back the channel
33
- * keeps that one narrowing in one place — the alternative was for every host
34
- * to re-run `toolResultGguiRender` beside this call and ask again.
35
- */
36
- import { cardUiResource, toolResultUiResource, type McpUiResourcePayload } from "./block-ui";
37
- import { blockGguiRender, gguiRenderResource, toolResultGguiRender } from "./ggui-render";
38
- import type { AgBlock, JsonValue } from "@silverprotocol/core";
39
-
40
- /** Which generative-UI channel produced a mount. See this module's header. */
41
- export type CardMountChannel = "inline" | "ggui";
42
-
43
- /** A mountable card: the resource to hand the host, and where it came from. */
44
- export interface CardMount {
45
- /** The payload an mcp-ui host mounts, identical in shape for both channels. */
46
- resource: McpUiResourcePayload;
47
- /**
48
- * `"inline"` — the server's own HTML, untrusted tenant content.
49
- * `"ggui"` — a shell that boots the ggui runtime from a platform-pinned
50
- * origin, and therefore needs a host page whose CSP allows that origin.
51
- */
52
- channel: CardMountChannel;
53
- }
54
-
55
- /**
56
- * A live `tool-result` block → the card to mount, across both channels.
57
- * `undefined` when the block carries no generative UI at all (or carries a
58
- * ggui render whose bootstrap did not reach us — see `ggui-render.ts`).
59
- */
60
- export function toolResultCardMount(
61
- block: Extract<AgBlock, { type: "tool-result" }>,
62
- ): CardMount | undefined {
63
- const inline = toolResultUiResource(block);
64
- if (inline) return { resource: inline, channel: "inline" };
65
- const ggui = toolResultGguiRender(block);
66
- const resource = ggui ? gguiRenderResource(ggui) : undefined;
67
- return resource ? { resource, channel: "ggui" } : undefined;
68
- }
69
-
70
- /**
71
- * A persisted `HistoryCard`'s `cardSnapshot` → the card to mount, across both
72
- * channels.
73
- *
74
- * The ggui arm is reached only for a snapshot that stored the render's
75
- * `_meta`; a bootstrap old enough to have been persisted has an expired
76
- * `wsToken` anyway, so in practice a ggui history card resolves to `undefined`
77
- * and the host renders its placeholder — honest, and NOT a broken mount.
78
- */
79
- export function cardCardMount(cardSnapshot: JsonValue): CardMount | undefined {
80
- const inline = cardUiResource(cardSnapshot);
81
- if (inline) return { resource: inline, channel: "inline" };
82
- for (const block of snapshotBlocks(cardSnapshot)) {
83
- const ggui = blockGguiRender(block);
84
- const resource = ggui ? gguiRenderResource(ggui) : undefined;
85
- if (resource) return { resource, channel: "ggui" };
86
- }
87
- return undefined;
88
- }
89
-
90
- /**
91
- * The blocks to scan inside a card snapshot: the stored `AgArtifact`'s `parts`
92
- * when present, then the snapshot root itself — exactly `cardUiResource`'s own
93
- * walk order, so both channels see the same candidates in the same order.
94
- */
95
- function snapshotBlocks(cardSnapshot: JsonValue): JsonValue[] {
96
- if (typeof cardSnapshot !== "object" || cardSnapshot === null || Array.isArray(cardSnapshot)) {
97
- return [];
98
- }
99
- const parts = cardSnapshot.parts;
100
- return Array.isArray(parts) ? [...parts, cardSnapshot] : [cardSnapshot];
101
- }
@@ -1,168 +0,0 @@
1
- /**
2
- * The **ggui render** channel: narrowing + self-contained shell construction
3
- * for a generative-UI card produced by the ggui MCP server (`ggui_render`).
4
- *
5
- * ## Where the pieces live (guuey#108 / ggui#427)
6
- *
7
- * The two halves of this channel have different owners, and the module is
8
- * split along that line:
9
- *
10
- * - **ggui's wire contract** — what makes a `_meta` slice a mountable
11
- * render bootstrap, and what the self-contained shell must contain — is
12
- * OWNED by ggui and imported from
13
- * `@ggui-ai/protocol/integrations/mcp-apps` ({@link asGguiRenderBootstrap},
14
- * {@link gguiShellHtml}, the `ai.ggui/render` key). This package used to
15
- * carry byte-compatible private copies (lifted upstream as ggui#427);
16
- * re-exporting the originals means a shell-contract change lands here by
17
- * bumping the pin, not by mirror-editing two repos.
18
- * - **host/silverprotocol shapes** — the `uiData`-keyed RECOGNITION signal,
19
- * the `AgBlock` tool-result narrowing, and the `McpUiResourcePayload`
20
- * adapter onto the host's existing mcp-ui mount path — are guuey-side
21
- * contracts and stay implemented here.
22
- *
23
- * ## Why recognition and mounting are separate
24
- *
25
- * A ggui render's `tool.done` carries two distinct signals:
26
- *
27
- * 1. **`uiData.resourceUri` is the RECOGNITION signal.** It is the only part
28
- * of the render's identity that survives `@silverprotocol/core`'s fold
29
- * (the reducer copies `uiData` — and, as of `@silverprotocol/core`
30
- * 0.4.1 (workspace#9), `_meta` — onto the `tool-result` block).
31
- * 2. **`_meta["ai.ggui/render"]` is the MOUNT MATERIAL.** Everything needed
32
- * to boot the card — which runtime bundle to load, which live-channel to
33
- * open, which props to seed — lives there and nowhere else.
34
- *
35
- * The shell {@link gguiShellHtml} builds is a string of HTML, so the ggui
36
- * card rides the host's EXISTING mcp-ui mount path unchanged: it narrows to
37
- * the same `McpUiResourcePayload` an inline resource does, so
38
- * `@mcp-ui/client`'s `AppRenderer` posts it as `srcdoc` into the
39
- * second-origin `mcp-app-sandbox.html` page — same double-iframe rule, same
40
- * sandbox origin, same opaque inner frame. No second mount mechanism.
41
- *
42
- * NOT in scope here: rehydrating a ggui card from persisted history. The
43
- * bootstrap's `wsToken` expires minutes after the render, so a stored
44
- * bootstrap is dead on arrival — a history card without a live bootstrap
45
- * correctly resolves to `undefined` and renders the host's placeholder
46
- * rather than a broken mount.
47
- */
48
- import type { AgBlock, JsonValue } from "@silverprotocol/core";
49
- import {
50
- asGguiRenderBootstrap,
51
- gguiShellHtml,
52
- MCP_APP_AI_GGUI_RENDER_META_KEY,
53
- type GguiRenderBootstrap,
54
- } from "@ggui-ai/protocol/integrations/mcp-apps";
55
- import { isJsonObject, type McpUiResourcePayload } from "./block-ui";
56
-
57
- export {
58
- asGguiRenderBootstrap,
59
- gguiShellHtml,
60
- } from "@ggui-ai/protocol/integrations/mcp-apps";
61
- export type {
62
- GguiRenderBootstrap,
63
- GguiShellHtmlOptions,
64
- } from "@ggui-ai/protocol/integrations/mcp-apps";
65
-
66
- /**
67
- * The `_meta` key the ggui render bootstrap rides on. Alias of the
68
- * protocol package's own constant — one spelling, owned upstream.
69
- */
70
- export const GGUI_RENDER_META_KEY = MCP_APP_AI_GGUI_RENDER_META_KEY;
71
-
72
- /** The `ui://` scheme prefix every ggui render resource uri carries. */
73
- const UI_SCHEME = "ui://";
74
-
75
- /** A ggui render recognised on a tool result: its resource uri + mount material. */
76
- export interface GguiRenderDescriptor {
77
- /** `uiData.resourceUri` — `ui://ggui/render/<sessionId>/<contractHash>`. */
78
- resourceUri: string;
79
- /** `uiData.sessionId`, when present. */
80
- sessionId?: string;
81
- /**
82
- * The `_meta["ai.ggui/render"]` slice, when it reached us. Absent for a
83
- * persisted history card and for any consumer folding without `fold.ts`'s
84
- * `_meta` carriage — such a descriptor is recognised but NOT mountable.
85
- */
86
- bootstrap?: GguiRenderBootstrap;
87
- }
88
-
89
- /**
90
- * A tool result's `uiData` (+ its `_meta`, when carried) → a ggui render
91
- * descriptor, or `undefined` for anything that is not one.
92
- *
93
- * The `ui://` scheme gate is deliberate: `uiData` is a general-purpose channel
94
- * (every `structuredContent` of a `_meta.ui`-stamped tool lands there), so a
95
- * bare `resourceUri` string is not on its own a claim of generative UI.
96
- */
97
- export function asGguiRender(
98
- uiData: JsonValue | undefined,
99
- meta: JsonValue | undefined,
100
- ): GguiRenderDescriptor | undefined {
101
- if (!isJsonObject(uiData)) return undefined;
102
- const resourceUri = uiData.resourceUri;
103
- if (typeof resourceUri !== "string" || !resourceUri.startsWith(UI_SCHEME)) return undefined;
104
- const bootstrap = asGguiRenderBootstrap(meta);
105
- return {
106
- resourceUri,
107
- ...(typeof uiData.sessionId === "string" ? { sessionId: uiData.sessionId } : {}),
108
- ...(bootstrap ? { bootstrap } : {}),
109
- };
110
- }
111
-
112
- /**
113
- * A live `tool-result` AgBlock → its ggui render descriptor, if it is one.
114
- *
115
- * NOTE: `@ggui-ai/protocol/integrations/mcp-apps` exports a helper of the
116
- * same name that narrows a spec-canonical MCP `CallToolResult` instead. This
117
- * one is the silverprotocol-side twin — the input is the FOLDED block, whose
118
- * `uiData`/`_meta` carriage is `@silverprotocol/core`'s contract, not ggui's.
119
- */
120
- export function toolResultGguiRender(
121
- block: Extract<AgBlock, { type: "tool-result" }>,
122
- ): GguiRenderDescriptor | undefined {
123
- return asGguiRender(block.uiData, block._meta);
124
- }
125
-
126
- /** An untyped (persisted-snapshot) block → its ggui render descriptor, if it is one. */
127
- export function blockGguiRender(block: JsonValue): GguiRenderDescriptor | undefined {
128
- if (!isJsonObject(block)) return undefined;
129
- if (block.type !== "tool-result") return undefined;
130
- return asGguiRender(block.uiData, block._meta);
131
- }
132
-
133
- /**
134
- * A ggui render descriptor → the mountable resource the host's existing
135
- * mcp-ui path already knows how to mount, or `undefined` when the descriptor
136
- * carries no bootstrap (history cards, and any fold that dropped `_meta`).
137
- *
138
- * The `uri` is the render's REAL `resourceUri` — the shell is the payload, not
139
- * a renaming of the resource.
140
- *
141
- * The shell is built `background: 'transparent'`: every guuey host that
142
- * mounts through this adapter (widget, portal web, Studio) draws its own
143
- * card chrome around the iframe, so the host page composits behind the card.
144
- * The upstream default (`'surface'`) is for standalone served documents —
145
- * see `GguiShellHtmlOptions` in `@ggui-ai/protocol/integrations/mcp-apps`.
146
- *
147
- * **On `_meta` being required to MOUNT (but never to RECOGNISE).** Recognition
148
- * — "this tool result is a ggui card" — is keyed on `uiData.resourceUri` alone
149
- * and never waits for anything (see {@link asGguiRender}); nothing in this
150
- * package is blocked on an upstream change. Mounting is different, and the
151
- * requirement is ggui's, not ours: its runtime rejects a slice without
152
- * `runtimeUrl` AND without at least one mode discriminator (`wsUrl`+`wsToken`,
153
- * `codeUrl`, or `kind`) as `MALFORMED_BOOTSTRAP` and renders nothing. `uiData`
154
- * carries none of those fields, so a bootstrap-less descriptor could only ever
155
- * produce a blank frame; returning `undefined` and letting the host show its
156
- * own placeholder is the honest answer, not a deferral. `@silverprotocol/core`'s
157
- * `Reducer` is what puts `_meta` on the block for a live turn, in-repo, today.
158
- */
159
- export function gguiRenderResource(
160
- render: GguiRenderDescriptor,
161
- ): McpUiResourcePayload | undefined {
162
- if (!render.bootstrap) return undefined;
163
- return {
164
- uri: render.resourceUri,
165
- mimeType: "text/html",
166
- text: gguiShellHtml(render.bootstrap, { background: "transparent" }),
167
- };
168
- }