@guuey/agent-client 0.2.2 → 0.3.0

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,117 +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
- /** A narrowed MCP embedded UI resource (the `_meta.ui.resource` shape). */
25
- export interface McpUiResourcePayload {
26
- uri: string;
27
- mimeType?: string;
28
- text?: string;
29
- blob?: string;
30
- }
31
- /** Narrow an opaque `JsonValue` to a plain (non-array) JSON object. */
32
- export declare function isJsonObject(v: JsonValue | undefined): v is {
33
- [key: string]: JsonValue;
34
- };
35
- /**
36
- * A JSON object → an MCP UI resource, if it has a `uri` plus renderable
37
- * payload (`text` or base64 `blob`). Returns `undefined` for anything else.
38
- */
39
- export declare function asResourcePayload(v: JsonValue | undefined): McpUiResourcePayload | undefined;
40
- /**
41
- * Does a `tool-result` block's `uiData` carry an MCP embedded UI resource?
42
- * Accepts the resource inlined directly, or wrapped as `{ resource: {...} }`
43
- * (the shape an MCP `resource` content part carries). No `ui://` scheme gate
44
- * here on purpose: `uiData` is the explicit *surface* channel (the server
45
- * stamped `_meta.ui`), so any resource on it is meant to render.
46
- */
47
- export declare function asUiResource(uiData: JsonValue | undefined): McpUiResourcePayload | undefined;
48
- /**
49
- * Scan a `provider-raw` block's `raw` (the vendor tool_result content part)
50
- * for a *generative-UI* resource. Unlike {@link asUiResource}, this path IS
51
- * gated on the `ui://` scheme: `provider-raw` degradation is a lossy catch-all,
52
- * so a plain file/text resource riding it is NOT a UI to mount — only the
53
- * mcp-ui `ui://` convention is.
54
- */
55
- export declare function scanProviderRawForUiResource(raw: JsonValue | undefined): McpUiResourcePayload | undefined;
56
- /**
57
- * Extract a mountable UI resource from an opaque AgBlock-shaped `JsonValue`
58
- * (used for persisted card snapshot parts, which arrive untyped). Dispatches
59
- * by `block.type`:
60
- * - `tool-result` → its `uiData` surface channel, then a `ui://` resource
61
- * degraded into a `provider-raw` content part — the SAME two channels
62
- * the live path ({@link toolResultUiResource}) mounts. The write side
63
- * (`nocode-runtime`'s `uiCardArtifactsFromMessages`, guuey#86) persists
64
- * card rows for both, so the snapshot arm must mount both or
65
- * provider-raw-only cards rehydrate as placeholders.
66
- * - `provider-raw` → a `ui://` resource hiding in `raw`
67
- * - `resource` → a first-class embedded resource (gated on `ui://`)
68
- * Everything else → `undefined`.
69
- */
70
- export declare function blockUiResource(block: JsonValue): McpUiResourcePayload | undefined;
71
- /**
72
- * A live `tool-result` AgBlock → its mountable UI resource, checking BOTH
73
- * channels the Claude facet uses:
74
- * 1. the `uiData` surface channel (server stamped `_meta.ui`), and
75
- * 2. an embedded `ui://` resource that degraded into a `provider-raw`
76
- * content part inside the tool result (MCP `resource` parts do NOT survive
77
- * as first-class `resource` AgBlocks here).
78
- * First-class `resource` content parts are intentionally not scanned in this
79
- * typed live path (the Claude facet never emits them); the untyped card path
80
- * ({@link blockUiResource}) covers them for other facets' persisted snapshots.
81
- */
82
- export declare function toolResultUiResource(block: Extract<AgBlock, {
83
- type: "tool-result";
84
- }>): McpUiResourcePayload | undefined;
85
- /**
86
- * A persisted `HistoryCard`'s `cardSnapshot` → a mountable UI resource. The
87
- * snapshot is the verbatim `AgArtifact` the pod stored (`{ parts: AgBlock[] }`),
88
- * so walk its `parts` for the first block that yields a resource; fall back to
89
- * treating the snapshot root itself as a block.
90
- *
91
- * NOTE (`no-ggui-tools`): a ggui-rendered card carries NO inline HTML resource —
92
- * its UI rides `_meta.ggui.bootstrap` and mounts via `@ggui-ai/react`'s
93
- * `McpAppIframe`. That branch is OUT OF SCOPE for v1 (deferred-pending-capture).
94
- * So a real ggui card resolves to `undefined` here and renders as the host's
95
- * coherent placeholder, not a broken mount.
96
- */
97
- export declare function cardUiResource(cardSnapshot: JsonValue): McpUiResourcePayload | undefined;
98
- /**
99
- * The resource's HTML: inline `text` wins; else base64-decode `blob`. `atob`
100
- * alone yields a Latin-1 string (mojibake on multibyte UTF-8), so decode via
101
- * bytes + `TextDecoder`. Invalid base64 → `undefined` (no renderable payload).
102
- */
103
- export declare function resourceHtml(resource: McpUiResourcePayload): string | undefined;
104
- /**
105
- * The tool name for a `tool-result` block, read off its paired `tool-call`
106
- * block in the same message (the reducer keeps both in one message's content).
107
- * Falls back to `"tool"` when the pair is missing.
108
- */
109
- export declare function toolNameFor(message: AgMessage, toolCallId: string): string;
110
- /**
111
- * Persisted cards, ascending by transcript `seq` (stable; input untouched).
112
- * These are PRIOR-turn cards — they always precede the live fold, so a
113
- * block-preserving renderer surfaces them first (e.g. under an "Earlier in
114
- * this conversation" divider).
115
- */
116
- export declare function sortHistoryCards(cards: readonly HistoryCard[]): HistoryCard[];
117
- //# sourceMappingURL=block-ui.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"block-ui.d.ts","sourceRoot":"","sources":["../src/block-ui.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAC1E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C,2EAA2E;AAC3E,MAAM,WAAW,oBAAoB;IACnC,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,uEAAuE;AACvE,wBAAgB,YAAY,CAAC,CAAC,EAAE,SAAS,GAAG,SAAS,GAAG,CAAC,IAAI;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAExF;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,SAAS,GAAG,SAAS,GAAG,oBAAoB,GAAG,SAAS,CAU5F;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,SAAS,GAAG,SAAS,GAAG,oBAAoB,GAAG,SAAS,CAK5F;AAED;;;;;;GAMG;AACH,wBAAgB,4BAA4B,CAC1C,GAAG,EAAE,SAAS,GAAG,SAAS,GACzB,oBAAoB,GAAG,SAAS,CAMlC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,SAAS,GAAG,oBAAoB,GAAG,SAAS,CAyBlF;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,OAAO,CAAC,OAAO,EAAE;IAAE,IAAI,EAAE,aAAa,CAAA;CAAE,CAAC,GAC/C,oBAAoB,GAAG,SAAS,CAUlC;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,cAAc,CAAC,YAAY,EAAE,SAAS,GAAG,oBAAoB,GAAG,SAAS,CAUxF;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,oBAAoB,GAAG,MAAM,GAAG,SAAS,CAU/E;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAK1E;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,SAAS,WAAW,EAAE,GAAG,WAAW,EAAE,CAE7E"}
package/dist/block-ui.js DELETED
@@ -1,183 +0,0 @@
1
- /** Narrow an opaque `JsonValue` to a plain (non-array) JSON object. */
2
- export function isJsonObject(v) {
3
- return typeof v === "object" && v !== null && !Array.isArray(v);
4
- }
5
- /**
6
- * A JSON object → an MCP UI resource, if it has a `uri` plus renderable
7
- * payload (`text` or base64 `blob`). Returns `undefined` for anything else.
8
- */
9
- export function asResourcePayload(v) {
10
- if (!isJsonObject(v))
11
- return undefined;
12
- if (typeof v.uri !== "string")
13
- return undefined;
14
- if (typeof v.text !== "string" && typeof v.blob !== "string")
15
- return undefined;
16
- return {
17
- uri: v.uri,
18
- ...(typeof v.mimeType === "string" ? { mimeType: v.mimeType } : {}),
19
- ...(typeof v.text === "string" ? { text: v.text } : {}),
20
- ...(typeof v.blob === "string" ? { blob: v.blob } : {}),
21
- };
22
- }
23
- /**
24
- * Does a `tool-result` block's `uiData` carry an MCP embedded UI resource?
25
- * Accepts the resource inlined directly, or wrapped as `{ resource: {...} }`
26
- * (the shape an MCP `resource` content part carries). No `ui://` scheme gate
27
- * here on purpose: `uiData` is the explicit *surface* channel (the server
28
- * stamped `_meta.ui`), so any resource on it is meant to render.
29
- */
30
- export function asUiResource(uiData) {
31
- if (!isJsonObject(uiData))
32
- return undefined;
33
- const direct = asResourcePayload(uiData);
34
- if (direct)
35
- return direct;
36
- return asResourcePayload(uiData.resource);
37
- }
38
- /**
39
- * Scan a `provider-raw` block's `raw` (the vendor tool_result content part)
40
- * for a *generative-UI* resource. Unlike {@link asUiResource}, this path IS
41
- * gated on the `ui://` scheme: `provider-raw` degradation is a lossy catch-all,
42
- * so a plain file/text resource riding it is NOT a UI to mount — only the
43
- * mcp-ui `ui://` convention is.
44
- */
45
- export function scanProviderRawForUiResource(raw) {
46
- if (!isJsonObject(raw))
47
- return undefined;
48
- const candidate = raw.resource !== undefined ? asResourcePayload(raw.resource) : asResourcePayload(raw);
49
- if (!candidate)
50
- return undefined;
51
- return candidate.uri.startsWith("ui://") ? candidate : undefined;
52
- }
53
- /**
54
- * Extract a mountable UI resource from an opaque AgBlock-shaped `JsonValue`
55
- * (used for persisted card snapshot parts, which arrive untyped). Dispatches
56
- * by `block.type`:
57
- * - `tool-result` → its `uiData` surface channel, then a `ui://` resource
58
- * degraded into a `provider-raw` content part — the SAME two channels
59
- * the live path ({@link toolResultUiResource}) mounts. The write side
60
- * (`nocode-runtime`'s `uiCardArtifactsFromMessages`, guuey#86) persists
61
- * card rows for both, so the snapshot arm must mount both or
62
- * provider-raw-only cards rehydrate as placeholders.
63
- * - `provider-raw` → a `ui://` resource hiding in `raw`
64
- * - `resource` → a first-class embedded resource (gated on `ui://`)
65
- * Everything else → `undefined`.
66
- */
67
- export function blockUiResource(block) {
68
- if (!isJsonObject(block))
69
- return undefined;
70
- switch (block.type) {
71
- case "tool-result": {
72
- const fromUiData = asUiResource(block.uiData);
73
- if (fromUiData)
74
- return fromUiData;
75
- if (Array.isArray(block.content)) {
76
- for (const part of block.content) {
77
- if (isJsonObject(part) && part.type === "provider-raw") {
78
- const found = scanProviderRawForUiResource(part.raw);
79
- if (found)
80
- return found;
81
- }
82
- }
83
- }
84
- return undefined;
85
- }
86
- case "provider-raw":
87
- return scanProviderRawForUiResource(block.raw);
88
- case "resource": {
89
- const r = asResourcePayload(block.resource);
90
- return r && r.uri.startsWith("ui://") ? r : undefined;
91
- }
92
- default:
93
- return undefined;
94
- }
95
- }
96
- /**
97
- * A live `tool-result` AgBlock → its mountable UI resource, checking BOTH
98
- * channels the Claude facet uses:
99
- * 1. the `uiData` surface channel (server stamped `_meta.ui`), and
100
- * 2. an embedded `ui://` resource that degraded into a `provider-raw`
101
- * content part inside the tool result (MCP `resource` parts do NOT survive
102
- * as first-class `resource` AgBlocks here).
103
- * First-class `resource` content parts are intentionally not scanned in this
104
- * typed live path (the Claude facet never emits them); the untyped card path
105
- * ({@link blockUiResource}) covers them for other facets' persisted snapshots.
106
- */
107
- export function toolResultUiResource(block) {
108
- const fromUiData = asUiResource(block.uiData);
109
- if (fromUiData)
110
- return fromUiData;
111
- for (const part of block.content) {
112
- if (part.type === "provider-raw") {
113
- const found = scanProviderRawForUiResource(part.raw);
114
- if (found)
115
- return found;
116
- }
117
- }
118
- return undefined;
119
- }
120
- /**
121
- * A persisted `HistoryCard`'s `cardSnapshot` → a mountable UI resource. The
122
- * snapshot is the verbatim `AgArtifact` the pod stored (`{ parts: AgBlock[] }`),
123
- * so walk its `parts` for the first block that yields a resource; fall back to
124
- * treating the snapshot root itself as a block.
125
- *
126
- * NOTE (`no-ggui-tools`): a ggui-rendered card carries NO inline HTML resource —
127
- * its UI rides `_meta.ggui.bootstrap` and mounts via `@ggui-ai/react`'s
128
- * `McpAppIframe`. That branch is OUT OF SCOPE for v1 (deferred-pending-capture).
129
- * So a real ggui card resolves to `undefined` here and renders as the host's
130
- * coherent placeholder, not a broken mount.
131
- */
132
- export function cardUiResource(cardSnapshot) {
133
- if (!isJsonObject(cardSnapshot))
134
- return undefined;
135
- const parts = cardSnapshot.parts;
136
- if (Array.isArray(parts)) {
137
- for (const part of parts) {
138
- const found = blockUiResource(part);
139
- if (found)
140
- return found;
141
- }
142
- }
143
- return blockUiResource(cardSnapshot);
144
- }
145
- /**
146
- * The resource's HTML: inline `text` wins; else base64-decode `blob`. `atob`
147
- * alone yields a Latin-1 string (mojibake on multibyte UTF-8), so decode via
148
- * bytes + `TextDecoder`. Invalid base64 → `undefined` (no renderable payload).
149
- */
150
- export function resourceHtml(resource) {
151
- if (resource.text !== undefined)
152
- return resource.text;
153
- if (resource.blob !== undefined) {
154
- try {
155
- return new TextDecoder().decode(Uint8Array.from(atob(resource.blob), (c) => c.charCodeAt(0)));
156
- }
157
- catch {
158
- return undefined;
159
- }
160
- }
161
- return undefined;
162
- }
163
- /**
164
- * The tool name for a `tool-result` block, read off its paired `tool-call`
165
- * block in the same message (the reducer keeps both in one message's content).
166
- * Falls back to `"tool"` when the pair is missing.
167
- */
168
- export function toolNameFor(message, toolCallId) {
169
- for (const b of message.content) {
170
- if (b.type === "tool-call" && b.toolCallId === toolCallId)
171
- return b.name;
172
- }
173
- return "tool";
174
- }
175
- /**
176
- * Persisted cards, ascending by transcript `seq` (stable; input untouched).
177
- * These are PRIOR-turn cards — they always precede the live fold, so a
178
- * block-preserving renderer surfaces them first (e.g. under an "Earlier in
179
- * this conversation" divider).
180
- */
181
- export function sortHistoryCards(cards) {
182
- return [...cards].sort((a, b) => a.seq - b.seq);
183
- }
@@ -1,69 +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 { type McpUiResourcePayload } from "./block-ui";
37
- import type { AgBlock, JsonValue } from "@silverprotocol/core";
38
- /** Which generative-UI channel produced a mount. See this module's header. */
39
- export type CardMountChannel = "inline" | "ggui";
40
- /** A mountable card: the resource to hand the host, and where it came from. */
41
- export interface CardMount {
42
- /** The payload an mcp-ui host mounts, identical in shape for both channels. */
43
- resource: McpUiResourcePayload;
44
- /**
45
- * `"inline"` — the server's own HTML, untrusted tenant content.
46
- * `"ggui"` — a shell that boots the ggui runtime from a platform-pinned
47
- * origin, and therefore needs a host page whose CSP allows that origin.
48
- */
49
- channel: CardMountChannel;
50
- }
51
- /**
52
- * A live `tool-result` block → the card to mount, across both channels.
53
- * `undefined` when the block carries no generative UI at all (or carries a
54
- * ggui render whose bootstrap did not reach us — see `ggui-render.ts`).
55
- */
56
- export declare function toolResultCardMount(block: Extract<AgBlock, {
57
- type: "tool-result";
58
- }>): CardMount | undefined;
59
- /**
60
- * A persisted `HistoryCard`'s `cardSnapshot` → the card to mount, across both
61
- * channels.
62
- *
63
- * The ggui arm is reached only for a snapshot that stored the render's
64
- * `_meta`; a bootstrap old enough to have been persisted has an expired
65
- * `wsToken` anyway, so in practice a ggui history card resolves to `undefined`
66
- * and the host renders its placeholder — honest, and NOT a broken mount.
67
- */
68
- export declare function cardCardMount(cardSnapshot: JsonValue): CardMount | undefined;
69
- //# sourceMappingURL=card-mount.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"card-mount.d.ts","sourceRoot":"","sources":["../src/card-mount.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,OAAO,EAAwC,KAAK,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAE7F,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAE/D,8EAA8E;AAC9E,MAAM,MAAM,gBAAgB,GAAG,QAAQ,GAAG,MAAM,CAAC;AAEjD,+EAA+E;AAC/E,MAAM,WAAW,SAAS;IACxB,+EAA+E;IAC/E,QAAQ,EAAE,oBAAoB,CAAC;IAC/B;;;;OAIG;IACH,OAAO,EAAE,gBAAgB,CAAC;CAC3B;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,OAAO,CAAC,OAAO,EAAE;IAAE,IAAI,EAAE,aAAa,CAAA;CAAE,CAAC,GAC/C,SAAS,GAAG,SAAS,CAMvB;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,YAAY,EAAE,SAAS,GAAG,SAAS,GAAG,SAAS,CAS5E"}
@@ -1,83 +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 } from "./block-ui";
37
- import { blockGguiRender, gguiRenderResource, toolResultGguiRender } from "./ggui-render";
38
- /**
39
- * A live `tool-result` block → the card to mount, across both channels.
40
- * `undefined` when the block carries no generative UI at all (or carries a
41
- * ggui render whose bootstrap did not reach us — see `ggui-render.ts`).
42
- */
43
- export function toolResultCardMount(block) {
44
- const inline = toolResultUiResource(block);
45
- if (inline)
46
- return { resource: inline, channel: "inline" };
47
- const ggui = toolResultGguiRender(block);
48
- const resource = ggui ? gguiRenderResource(ggui) : undefined;
49
- return resource ? { resource, channel: "ggui" } : undefined;
50
- }
51
- /**
52
- * A persisted `HistoryCard`'s `cardSnapshot` → the card to mount, across both
53
- * channels.
54
- *
55
- * The ggui arm is reached only for a snapshot that stored the render's
56
- * `_meta`; a bootstrap old enough to have been persisted has an expired
57
- * `wsToken` anyway, so in practice a ggui history card resolves to `undefined`
58
- * and the host renders its placeholder — honest, and NOT a broken mount.
59
- */
60
- export function cardCardMount(cardSnapshot) {
61
- const inline = cardUiResource(cardSnapshot);
62
- if (inline)
63
- return { resource: inline, channel: "inline" };
64
- for (const block of snapshotBlocks(cardSnapshot)) {
65
- const ggui = blockGguiRender(block);
66
- const resource = ggui ? gguiRenderResource(ggui) : undefined;
67
- if (resource)
68
- return { resource, channel: "ggui" };
69
- }
70
- return undefined;
71
- }
72
- /**
73
- * The blocks to scan inside a card snapshot: the stored `AgArtifact`'s `parts`
74
- * when present, then the snapshot root itself — exactly `cardUiResource`'s own
75
- * walk order, so both channels see the same candidates in the same order.
76
- */
77
- function snapshotBlocks(cardSnapshot) {
78
- if (typeof cardSnapshot !== "object" || cardSnapshot === null || Array.isArray(cardSnapshot)) {
79
- return [];
80
- }
81
- const parts = cardSnapshot.parts;
82
- return Array.isArray(parts) ? [...parts, cardSnapshot] : [cardSnapshot];
83
- }
@@ -1,161 +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
- * ## Why this is a second channel and not the existing one
6
- *
7
- * `block-ui.ts`'s original narrowing accepts exactly one shape — an mcp-ui
8
- * embedded resource that carries its HTML INLINE (`{uri, text|blob}`). A ggui
9
- * render carries no HTML at all. Its `tool.done` looks like this on the wire
10
- * (shaped identically to the production capture the widget's fixtures
11
- * replay, but with the SAME synthetic ids those redacted fixtures use —
12
- * `apps/widget/src/fixtures/issue2627-render-capture.sse.txt` seq 48):
13
- *
14
- * ```jsonc
15
- * {"type":"tool.done", "toolCallId":"toolu_0000…0005",
16
- * "uiData":{"sessionId":"render_0000…0001",
17
- * "resourceUri":"ui://ggui/render/render_0000…0001/c10a2055…", … },
18
- * "_meta":{"ai.ggui/render":{"sessionId":"render_0000…0001","appId":"APP00000",
19
- * "runtimeUrl":"https://dev.mcp.sandbox.ggui.ai/_ggui/iframe-runtime.js",
20
- * "wsUrl":"wss://…/ws","wsToken":"eyJ…","expiresAt":"…",
21
- * "propsJson":"{…}"},
22
- * "ui":{"resourceUri":"ui://ggui/render/…"}}}
23
- * ```
24
- *
25
- * Two facts follow, and they shape everything below:
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
- * ## How the card mounts (the ggui-documented self-contained shell)
36
- *
37
- * ggui's iframe runtime accepts its bootstrap from three delivery channels;
38
- * the highest-priority one is the **self-contained shell**: HTML that inlines
39
- * the slice envelope at `globalThis.__GGUI_META__` synchronously BEFORE the
40
- * runtime bundle's `<script type="module">` evaluates, after which the runtime
41
- * autostarts, creates its own mount container and renders — no postMessage
42
- * round-trip, no host-side ggui code. That contract is stated verbatim by the
43
- * runtime's own reader (`@ggui-ai/iframe-runtime`'s `parseMetaFromGlobal`:
44
- * *"The global carries the SAME slice envelope shape as the wire `_meta`
45
- * (`{ "ai.ggui/render": {...} }`) … per-render shells populate this
46
- * synchronously BEFORE the runtime bundle's `<script type="module">`
47
- * evaluates"*), and by its boot resolver (`runtime.js`'s autostart:
48
- * `readSelfContainedMeta()` first, postMessage channels after).
49
- *
50
- * {@link gguiShellHtml} builds exactly that shell. Because the shell IS a
51
- * string of HTML, the ggui card then rides the host's EXISTING mcp-ui mount
52
- * path unchanged: it narrows to the same `McpUiResourcePayload` an inline
53
- * resource does, so `@mcp-ui/client`'s `AppRenderer` posts it as `srcdoc` into
54
- * the second-origin `mcp-app-sandbox.html` page — same double-iframe rule,
55
- * same sandbox origin, same opaque inner frame. No second mount mechanism.
56
- *
57
- * The slice is inlined **verbatim**: `runtimeUrl` is honored as given (ggui's
58
- * host checklist item 8 — "no fallback URL, no substitution"), and every other
59
- * field is passed through untouched for the runtime's own projector to
60
- * validate. This module reads exactly one field (`runtimeUrl`) and only to
61
- * prove the slice is mountable at all.
62
- *
63
- * NOT in scope here: rehydrating a ggui card from persisted history. The
64
- * bootstrap's `wsToken` expires minutes after the render (`expiresAt` in the
65
- * capture above), so a stored bootstrap is dead on arrival — a history card
66
- * without a live bootstrap correctly resolves to `undefined` and renders the
67
- * host's placeholder rather than a broken mount.
68
- */
69
- import type { AgBlock, JsonValue } from "@silverprotocol/core";
70
- import { type McpUiResourcePayload } from "./block-ui";
71
- /** The `_meta` key the ggui render bootstrap rides on (MCP-Apps slice convention). */
72
- export declare const GGUI_RENDER_META_KEY = "ai.ggui/render";
73
- /**
74
- * A ggui render bootstrap: the runtime bundle URL this module reads, plus the
75
- * WHOLE `ai.ggui/render` slice, verbatim, for the shell to inline.
76
- */
77
- export interface GguiRenderBootstrap {
78
- /** `runtimeUrl` — the ESM bundle the shell loads. Honored as given. */
79
- runtimeUrl: string;
80
- /**
81
- * The verbatim slice. Open-ended by construction: it is ggui's wire
82
- * contract, not one this package owns, and the runtime's own projector is
83
- * the authority on every field. Re-declaring it here would duplicate a
84
- * contract we do not own and rot at ggui's next field addition.
85
- */
86
- slice: {
87
- [key: string]: JsonValue;
88
- };
89
- }
90
- /** A ggui render recognised on a tool result: its resource uri + mount material. */
91
- export interface GguiRenderDescriptor {
92
- /** `uiData.resourceUri` — `ui://ggui/render/<sessionId>/<contractHash>`. */
93
- resourceUri: string;
94
- /** `uiData.sessionId`, when present. */
95
- sessionId?: string;
96
- /**
97
- * The `_meta["ai.ggui/render"]` slice, when it reached us. Absent for a
98
- * persisted history card and for any consumer folding without `fold.ts`'s
99
- * `_meta` carriage — such a descriptor is recognised but NOT mountable.
100
- */
101
- bootstrap?: GguiRenderBootstrap;
102
- }
103
- /**
104
- * A `_meta` container → the ggui render bootstrap, or `undefined`.
105
- *
106
- * Two hard requirements, both the runtime's own `validateMeta` enforces
107
- * (`MALFORMED_BOOTSTRAP`): a non-empty `runtimeUrl`, AND at least one mode
108
- * discriminator (see {@link hasModeDiscriminator}). A slice with `runtimeUrl`
109
- * alone has a bundle to load but nothing for it to mount — the runtime would
110
- * boot into a blank shell rather than a card, so this guard treats that shape
111
- * as unmountable too and returns `undefined`.
112
- */
113
- export declare function asGguiRenderBootstrap(meta: JsonValue | undefined): GguiRenderBootstrap | undefined;
114
- /**
115
- * A tool result's `uiData` (+ its `_meta`, when carried) → a ggui render
116
- * descriptor, or `undefined` for anything that is not one.
117
- *
118
- * The `ui://` scheme gate is deliberate: `uiData` is a general-purpose channel
119
- * (every `structuredContent` of a `_meta.ui`-stamped tool lands there), so a
120
- * bare `resourceUri` string is not on its own a claim of generative UI.
121
- */
122
- export declare function asGguiRender(uiData: JsonValue | undefined, meta: JsonValue | undefined): GguiRenderDescriptor | undefined;
123
- /** A live `tool-result` AgBlock → its ggui render descriptor, if it is one. */
124
- export declare function toolResultGguiRender(block: Extract<AgBlock, {
125
- type: "tool-result";
126
- }>): GguiRenderDescriptor | undefined;
127
- /** An untyped (persisted-snapshot) block → its ggui render descriptor, if it is one. */
128
- export declare function blockGguiRender(block: JsonValue): GguiRenderDescriptor | undefined;
129
- /**
130
- * The ggui **self-contained shell** for a render bootstrap — see this module's
131
- * header for the contract it implements.
132
- *
133
- * Ordering is guaranteed twice over: the classic `<script>` runs during parse,
134
- * and the runtime's `<script type="module">` is deferred by definition, so the
135
- * global is always populated before the bundle evaluates.
136
- */
137
- export declare function gguiShellHtml(bootstrap: GguiRenderBootstrap): string;
138
- /**
139
- * A ggui render descriptor → the mountable resource the host's existing
140
- * mcp-ui path already knows how to mount, or `undefined` when the descriptor
141
- * carries no bootstrap (history cards, and any fold that dropped `_meta`).
142
- *
143
- * The `uri` is the render's REAL `resourceUri` — the shell is the payload, not
144
- * a renaming of the resource.
145
- *
146
- * **On `_meta` being required to MOUNT (but never to RECOGNISE).** Recognition
147
- * — "this tool result is a ggui card" — is keyed on `uiData.resourceUri` alone
148
- * and never waits for anything (see {@link asGguiRender}); nothing in this
149
- * package is blocked on an upstream change. Mounting is different, and the
150
- * requirement is ggui's, not ours: its runtime rejects a slice without
151
- * `runtimeUrl` AND without at least one mode discriminator (`wsUrl`+`wsToken`,
152
- * `codeUrl`, or `kind`) as `MALFORMED_BOOTSTRAP` and renders nothing. `uiData`
153
- * carries none of those fields — it has `sessionId`, `resourceUri`, `action`,
154
- * `contractHash`, `blueprintId`, `variantKey`, `cache`, `nextStep`, and that is
155
- * all. So a bootstrap-less descriptor could only ever produce a blank frame;
156
- * returning `undefined` and letting the host show its own placeholder is the
157
- * honest answer, not a deferral. `@silverprotocol/core`'s `Reducer` is what
158
- * puts `_meta` on the block for a live turn, in-repo, today.
159
- */
160
- export declare function gguiRenderResource(render: GguiRenderDescriptor): McpUiResourcePayload | undefined;
161
- //# sourceMappingURL=ggui-render.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ggui-render.d.ts","sourceRoot":"","sources":["../src/ggui-render.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmEG;AACH,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAC/D,OAAO,EAAgB,KAAK,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAErE,sFAAsF;AACtF,eAAO,MAAM,oBAAoB,mBAAmB,CAAC;AAKrD;;;GAGG;AACH,MAAM,WAAW,mBAAmB;IAClC,uEAAuE;IACvE,UAAU,EAAE,MAAM,CAAC;IACnB;;;;;OAKG;IACH,KAAK,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,CAAC;CACrC;AAED,oFAAoF;AACpF,MAAM,WAAW,oBAAoB;IACnC,4EAA4E;IAC5E,WAAW,EAAE,MAAM,CAAC;IACpB,wCAAwC;IACxC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;OAIG;IACH,SAAS,CAAC,EAAE,mBAAmB,CAAC;CACjC;AAwBD;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,SAAS,GAAG,SAAS,GAAG,mBAAmB,GAAG,SAAS,CAQlG;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAC1B,MAAM,EAAE,SAAS,GAAG,SAAS,EAC7B,IAAI,EAAE,SAAS,GAAG,SAAS,GAC1B,oBAAoB,GAAG,SAAS,CAUlC;AAED,+EAA+E;AAC/E,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,OAAO,CAAC,OAAO,EAAE;IAAE,IAAI,EAAE,aAAa,CAAA;CAAE,CAAC,GAC/C,oBAAoB,GAAG,SAAS,CAElC;AAED,wFAAwF;AACxF,wBAAgB,eAAe,CAAC,KAAK,EAAE,SAAS,GAAG,oBAAoB,GAAG,SAAS,CAIlF;AA0BD;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,SAAS,EAAE,mBAAmB,GAAG,MAAM,CAkBpE;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,oBAAoB,GAC3B,oBAAoB,GAAG,SAAS,CAOlC"}