@saar-ai/web-widget 0.7.0 → 0.9.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.
- package/dist/chunks/{chat-tMKkYmTC.js → chat-DAgqVvIS.js} +338 -26
- package/dist/chunks/{host-widget-B97T89b7.js → host-widget-CTK7C95y.js} +1326 -21
- package/dist/chunks/{schemas-Bl_6mxe4.js → schemas-D-uDc-9q.js} +164 -1
- package/dist/chunks/{telemetry-BI_SEWTb.js → telemetry-DQUulENJ.js} +16 -1
- package/dist/dock/dock-motion.d.ts +14 -0
- package/dist/dock/dock-motion.d.ts.map +1 -1
- package/dist/dock/dock.d.ts +71 -8
- package/dist/dock/dock.d.ts.map +1 -1
- package/dist/host-routes.d.ts +187 -0
- package/dist/host-routes.d.ts.map +1 -0
- package/dist/host-widget.d.ts +31 -2
- package/dist/host-widget.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/mount.d.ts +10 -1
- package/dist/mount.d.ts.map +1 -1
- package/dist/next/index.js +1 -1
- package/dist/use-chat-session.d.ts +99 -33
- package/dist/use-chat-session.d.ts.map +1 -1
- package/dist/use-voice-session.d.ts +46 -8
- package/dist/use-voice-session.d.ts.map +1 -1
- package/package.json +3 -3
|
@@ -1,4 +1,213 @@
|
|
|
1
|
-
import { a as httpUrl, c as number, i as discriminatedUnion, l as object, n as array, o as literal, r as boolean, t as _enum, u as
|
|
1
|
+
import { a as httpUrl, c as number, d as string, i as discriminatedUnion, l as object, n as array, o as literal, r as boolean, t as _enum, u as record } from "./schemas-D-uDc-9q.js";
|
|
2
|
+
//#region ../widget-protocol/dist/act.js
|
|
3
|
+
/**
|
|
4
|
+
* ACT EVENTS — the concierge moving the store, rather than only describing it.
|
|
5
|
+
*
|
|
6
|
+
* `cards` and `add-to-cart` (`./chat.ts`) already let the concierge put
|
|
7
|
+
* something on screen and ask for something to be bought. These three let it
|
|
8
|
+
* DRIVE the page the shopper is already on: walk them to a collection, narrow
|
|
9
|
+
* what that collection shows, or point at one card without moving them
|
|
10
|
+
* anywhere at all. Same sidecar mechanism as `add-to-cart` — a commerce tool
|
|
11
|
+
* returns one of these under `widgetEvent`, the server strips it before the
|
|
12
|
+
* model can read it back, and `@saar-ai/web-widget` performs it through
|
|
13
|
+
* `@saar-ai/widget-bridge`.
|
|
14
|
+
*
|
|
15
|
+
* ## Why a separate file from `chat.ts`
|
|
16
|
+
*
|
|
17
|
+
* `chat.ts` owns the SSE lifecycle and the storefront REPORT vocabulary; this
|
|
18
|
+
* file owns the act vocabulary. They meet in exactly one place —
|
|
19
|
+
* `WidgetToolEventSchema`'s union — which keeps a new act event from touching
|
|
20
|
+
* the report schemas at all.
|
|
21
|
+
*
|
|
22
|
+
* ## The rule every schema here encodes: the server names the destination
|
|
23
|
+
*
|
|
24
|
+
* A model that can type a URL can type ANY URL, and a concierge that walks a
|
|
25
|
+
* shopper to an attacker's page is worse than one that cannot navigate at all.
|
|
26
|
+
* So no field on this wire is model prose:
|
|
27
|
+
*
|
|
28
|
+
* - {@link SameOriginPathSchema} admits only a ROOT-RELATIVE path. Not a
|
|
29
|
+
* scheme, not a host, not a protocol-relative `//evil.example`, not a
|
|
30
|
+
* backslash a browser would normalise into one. The host page resolves it
|
|
31
|
+
* against its own origin, so "same-origin" is structural rather than a
|
|
32
|
+
* check that could be got wrong — strictly stronger than an origin
|
|
33
|
+
* allow-list, which still has to parse an attacker-chosen string correctly.
|
|
34
|
+
* - Every id-shaped field is a catalogue id the SERVER resolved
|
|
35
|
+
* (`apps/api/src/catalog/tools/`), never one the model invented — the tools
|
|
36
|
+
* throw `NOT_FOUND` rather than emit an event for a product that does not
|
|
37
|
+
* exist.
|
|
38
|
+
* - `reason` is the ONE free-text field, it is bounded, and the widget never
|
|
39
|
+
* renders it. It exists so a console timeline can say why the shopper was
|
|
40
|
+
* moved.
|
|
41
|
+
*/
|
|
42
|
+
/** Bound for an id-shaped field — mirrors `chat.ts`'s own `ID_MAX`. */
|
|
43
|
+
var ID_MAX$1 = 200;
|
|
44
|
+
/** Bound for a path. Generous (Shopify facet queries are long) but finite. */
|
|
45
|
+
var PATH_MAX = 2048;
|
|
46
|
+
/** Bound for the operator-facing "why" on a navigate. Never rendered to the shopper. */
|
|
47
|
+
var REASON_MAX = 200;
|
|
48
|
+
/** Bound for a display title carried alongside a spotlight. */
|
|
49
|
+
var TITLE_MAX = 500;
|
|
50
|
+
/** How many distinct facets one `filter` event may carry. A narrowing, not a re-query. */
|
|
51
|
+
var MAX_FACETS = 10;
|
|
52
|
+
/** How many values one facet may carry (a multi-select colour, say). */
|
|
53
|
+
var MAX_FACET_VALUES = 10;
|
|
54
|
+
var FACET_KEY_MAX = 64;
|
|
55
|
+
var FACET_VALUE_MAX = 255;
|
|
56
|
+
/**
|
|
57
|
+
* A ROOT-RELATIVE, same-origin path — `/collections/rugs?filter.p.vendor=X`.
|
|
58
|
+
*
|
|
59
|
+
* The character class is the security boundary and is deliberately narrow:
|
|
60
|
+
*
|
|
61
|
+
* - must begin with exactly one `/` — so no `https:`, no `javascript:`, no
|
|
62
|
+
* bare `evil.example/x` a browser would resolve as a relative sibling;
|
|
63
|
+
* - the second character may not be `/` or `\` — `//evil.example` is a
|
|
64
|
+
* protocol-relative URL, and browsers normalise `/\evil.example` into one;
|
|
65
|
+
* - no whitespace and no backslash anywhere — both are the raw material of
|
|
66
|
+
* every "URL that reads as one origin and resolves as another" trick, and
|
|
67
|
+
* no legitimate storefront path needs either (a space is `%20`).
|
|
68
|
+
*
|
|
69
|
+
* Anything else is a rejection, never a repair: a path this schema had to fix
|
|
70
|
+
* is a path nobody meant to emit.
|
|
71
|
+
*/
|
|
72
|
+
var SameOriginPathSchema = string().min(1).max(PATH_MAX).regex(/^\/(?![/\\])[^\s\\]*$/, "must be a root-relative, same-origin path (e.g. \"/collections/rugs\")");
|
|
73
|
+
/** Which kind of destination a {@link WidgetChatNavigateEventSchema} resolved. Drives nothing in the widget; carried so a console timeline can read the intent without re-parsing the path. */
|
|
74
|
+
var NavigateTargetSchema = _enum([
|
|
75
|
+
"product",
|
|
76
|
+
"collection",
|
|
77
|
+
"search",
|
|
78
|
+
"cart",
|
|
79
|
+
"url"
|
|
80
|
+
]);
|
|
81
|
+
/**
|
|
82
|
+
* The three act events as one union — imported by `./chat.ts` and spread into
|
|
83
|
+
* {@link WidgetToolEventSchema} and the SSE union there, so a fourth act event
|
|
84
|
+
* is added HERE and reaches both unions without touching either.
|
|
85
|
+
*/
|
|
86
|
+
var ACT_EVENT_SCHEMAS = [
|
|
87
|
+
object({
|
|
88
|
+
type: literal("navigate"),
|
|
89
|
+
/** Root-relative, same-origin. The host bridge resolves it against the page's own origin. */
|
|
90
|
+
url: SameOriginPathSchema,
|
|
91
|
+
target: NavigateTargetSchema,
|
|
92
|
+
/** Why the shopper was moved — operator-facing, bounded, never rendered to the shopper. */
|
|
93
|
+
reason: string().trim().min(1).max(REASON_MAX)
|
|
94
|
+
}),
|
|
95
|
+
object({
|
|
96
|
+
type: literal("filter"),
|
|
97
|
+
/**
|
|
98
|
+
* Where to apply the filters, when the concierge named a collection the
|
|
99
|
+
* shopper is not currently on. Absent ⇒ the page they are already on, which
|
|
100
|
+
* is what `applyFilter` operates on.
|
|
101
|
+
*/
|
|
102
|
+
collectionUrl: SameOriginPathSchema.optional(),
|
|
103
|
+
filters: record(string().trim().min(1).max(FACET_KEY_MAX), array(string().trim().min(1).max(FACET_VALUE_MAX)).min(1).max(MAX_FACET_VALUES)).refine((value) => Object.keys(value).length >= 1 && Object.keys(value).length <= MAX_FACETS, { message: `filters must carry between 1 and ${MAX_FACETS} facets` })
|
|
104
|
+
}),
|
|
105
|
+
object({
|
|
106
|
+
type: literal("spotlight"),
|
|
107
|
+
/** The INTERNAL catalog UUID — the same field, same meaning, as a card's. */
|
|
108
|
+
productId: string().min(1).max(ID_MAX$1),
|
|
109
|
+
/**
|
|
110
|
+
* The storefront handle — a SLUG, never a path, bounded by the same
|
|
111
|
+
* character class a card's `handle` uses and for the same reason: the widget
|
|
112
|
+
* builds a `a[href*="/products/<handle>"]` selector out of it.
|
|
113
|
+
*/
|
|
114
|
+
handle: string().min(1).max(ID_MAX$1).regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/i).optional(),
|
|
115
|
+
/**
|
|
116
|
+
* The PLATFORM-native product id (Shopify's numeric id), extracted
|
|
117
|
+
* server-side from the product's provider GID by the same parser the
|
|
118
|
+
* checkout permalink uses — never a string-slice, absent when the external
|
|
119
|
+
* id is not a recognised GID.
|
|
120
|
+
*/
|
|
121
|
+
platformProductId: string().min(1).max(ID_MAX$1).optional(),
|
|
122
|
+
/** The card's display title — the widget's accessible announcement says which product it pointed at. */
|
|
123
|
+
title: string().trim().min(1).max(TITLE_MAX).optional()
|
|
124
|
+
})
|
|
125
|
+
];
|
|
126
|
+
Object.freeze([
|
|
127
|
+
"navigate",
|
|
128
|
+
"filter",
|
|
129
|
+
"spotlight"
|
|
130
|
+
]);
|
|
131
|
+
/**
|
|
132
|
+
* The storefront reporting a spotlight it could NOT perform — the mirror of
|
|
133
|
+
* `cart_add_failed` (`./chat.ts`), and for the identical reason: the server
|
|
134
|
+
* asked for something that happens in a browser it has no handle on, and only
|
|
135
|
+
* the widget learns the answer.
|
|
136
|
+
*
|
|
137
|
+
* A card that is simply not on this page is the ordinary case — the shopper
|
|
138
|
+
* scrolled past it, the collection paginated, the theme lazy-renders. Without
|
|
139
|
+
* this report the concierge says "it's the one highlighted on your screen" and
|
|
140
|
+
* nothing is highlighted, which on a CALL the shopper cannot even see is
|
|
141
|
+
* wrong.
|
|
142
|
+
*
|
|
143
|
+
* `.strict()`, and deliberately no "reason" enum: the widget knows only
|
|
144
|
+
* "found" or "not found", and a richer vocabulary would be a claim about a
|
|
145
|
+
* merchant's DOM that this code cannot make.
|
|
146
|
+
*/
|
|
147
|
+
var WidgetSpotlightFailedReportSchema = object({
|
|
148
|
+
kind: literal("spotlight_failed"),
|
|
149
|
+
/** The product the concierge pointed at, so the model can name it in its correction. */
|
|
150
|
+
title: string().trim().min(1).max(TITLE_MAX),
|
|
151
|
+
productId: string().min(1).max(ID_MAX$1).optional()
|
|
152
|
+
}).strict();
|
|
153
|
+
/**
|
|
154
|
+
* The storefront reporting a `navigate` it could NOT perform — the third
|
|
155
|
+
* member of the same family as `cart_add_failed` and
|
|
156
|
+
* {@link WidgetSpotlightFailedReportSchema}, and it exists for the identical
|
|
157
|
+
* reason: the tool returned `status: 'requested'` because the move happens in
|
|
158
|
+
* a browser the server has no handle on, and only the widget learns the
|
|
159
|
+
* answer.
|
|
160
|
+
*
|
|
161
|
+
* A bridge with no `navigate` is the ordinary case, not an exotic one: §3's
|
|
162
|
+
* "every optional method is genuinely optional" means a host can ship a
|
|
163
|
+
* read-only bridge, and a `NextBridge`-style host can refuse a move its router
|
|
164
|
+
* cannot make. Without this report the concierge has said "taking you to the
|
|
165
|
+
* cushions" — out loud, on a call, where the shopper cannot see that the page
|
|
166
|
+
* did not change — and nothing ever corrects it.
|
|
167
|
+
*
|
|
168
|
+
* **No path and no url**, deliberately. The one thing the guardrails forbid
|
|
169
|
+
* the concierge to say is a URL, and a report carrying one puts it in the
|
|
170
|
+
* model's context with nothing but an instruction standing between it and the
|
|
171
|
+
* shopper's ear. `target` is the KIND of destination, which is all a
|
|
172
|
+
* correction needs ("I can't open your basket from here").
|
|
173
|
+
*/
|
|
174
|
+
var WidgetNavigateFailedReportSchema = object({
|
|
175
|
+
kind: literal("navigate_failed"),
|
|
176
|
+
/** What kind of page the concierge was trying to open. Never the path. */
|
|
177
|
+
target: NavigateTargetSchema.optional()
|
|
178
|
+
}).strict();
|
|
179
|
+
//#endregion
|
|
180
|
+
//#region ../widget-protocol/dist/currency.js
|
|
181
|
+
/**
|
|
182
|
+
* The ISO 4217 currency-code schema every currency field on this wire
|
|
183
|
+
* shares — three LETTERS, case preserved, never a bare length check.
|
|
184
|
+
*
|
|
185
|
+
* ## Why the charset is the control, not the length
|
|
186
|
+
*
|
|
187
|
+
* `z.string().length(3)` and this schema admit the same LENGTH, but
|
|
188
|
+
* `"\n- "` is three characters and only one of the schemas below refuses
|
|
189
|
+
* it. A currency value that reaches a model as prose — `chat.ts`'s cards,
|
|
190
|
+
* `@wesence/agent-core`'s `renderShopperCartForModel` — sits on the same
|
|
191
|
+
* line as a price, and a newline there forges a line boundary in a block
|
|
192
|
+
* whose entire legibility rests on one line per item. The titles beside it
|
|
193
|
+
* have always been trimmed/bounded for exactly this reason; a currency code
|
|
194
|
+
* was left at `length(3)` because three characters "looks safe," which is
|
|
195
|
+
* precisely the assumption this regex refuses to make.
|
|
196
|
+
*
|
|
197
|
+
* ## Why this lives in its own module, not just `chat.ts`
|
|
198
|
+
*
|
|
199
|
+
* This schema used to be declared privately inside `chat.ts` and never
|
|
200
|
+
* exported. `catalog.ts` and `curation.ts` — this same package — each grew
|
|
201
|
+
* their own `z.string().length(3)` instead of importing it, and
|
|
202
|
+
* `@wesence/agent-core`'s `shopper-cart.ts` grew a fourth copy of the regex
|
|
203
|
+
* itself rather than importing this one. An unexported validator gets
|
|
204
|
+
* re-invented at every call site that needs it, and re-invention is exactly
|
|
205
|
+
* how two of those four copies lost the hardening. This module is the one
|
|
206
|
+
* place `CURRENCY_CODE` is declared; every schema on both legs of a widget
|
|
207
|
+
* conversation imports it from here rather than restating it.
|
|
208
|
+
*/
|
|
209
|
+
var CURRENCY_CODE = string().regex(/^[A-Za-z]{3}$/, "must be a three-letter ISO 4217 currency code");
|
|
210
|
+
//#endregion
|
|
2
211
|
//#region ../widget-protocol/dist/chat.js
|
|
3
212
|
/**
|
|
4
213
|
* The webchat SSE wire contract, and the sidecar `widgetEvent` a tool result
|
|
@@ -64,14 +273,6 @@ var CARD_TEXT_MAX = 500;
|
|
|
64
273
|
/** Bound for an id-shaped field (product/variant/curation ids). */
|
|
65
274
|
var ID_MAX = 200;
|
|
66
275
|
/**
|
|
67
|
-
* An ISO 4217 currency code, as a schema — three LETTERS, case preserved.
|
|
68
|
-
*
|
|
69
|
-
* Shared by every currency field on this wire so none of them can drift back
|
|
70
|
-
* to a bare `length(3)`. See {@link WidgetChatCardSchema}'s `currency` for the
|
|
71
|
-
* prompt-injection reason the charset matters and not just the length.
|
|
72
|
-
*/
|
|
73
|
-
var CURRENCY_CODE = string().regex(/^[A-Za-z]{3}$/, "must be a three-letter ISO 4217 currency code");
|
|
74
|
-
/**
|
|
75
276
|
* One product/curation card. `priceMinor` is the ISO 4217 minor-unit integer
|
|
76
277
|
* (cents, paise, …) — never a float — and `currency` is the ISO 4217 code, so
|
|
77
278
|
* the widget formats price itself rather than trusting a pre-formatted
|
|
@@ -131,14 +332,113 @@ var WidgetChatTextDeltaEventSchema = object({
|
|
|
131
332
|
type: literal("text-delta"),
|
|
132
333
|
delta: string()
|
|
133
334
|
});
|
|
134
|
-
/**
|
|
335
|
+
/**
|
|
336
|
+
* Max cards in one event — a chat bubble, not a catalog page.
|
|
337
|
+
*
|
|
338
|
+
* Exported so `build_curation` (`apps/api/src/catalog/tools/build-curation.tool.ts`)
|
|
339
|
+
* can cap what it renders BEFORE persisting, rather than building an
|
|
340
|
+
* oversized array and finding out only when this schema (or
|
|
341
|
+
* {@link buildWidgetChatCardsEvent}'s own cap, for replay) refuses it. A
|
|
342
|
+
* second, hand-typed `20` here would be exactly the kind of drift this
|
|
343
|
+
* package exists to prevent.
|
|
344
|
+
*/
|
|
135
345
|
var MAX_CARDS_PER_EVENT = 20;
|
|
136
346
|
var WidgetChatCardsEventSchema = object({
|
|
137
347
|
type: literal("cards"),
|
|
138
348
|
/** Correlates this render back to whichever tool call produced it, when the tool supplies one. */
|
|
139
349
|
curationId: string().min(1).max(ID_MAX).optional(),
|
|
140
|
-
items: array(WidgetChatCardSchema).min(1).max(
|
|
350
|
+
items: array(WidgetChatCardSchema).min(1).max(20)
|
|
141
351
|
});
|
|
352
|
+
/**
|
|
353
|
+
* The optional {@link WidgetChatCard} fields a validation failure may drop
|
|
354
|
+
* WITHOUT losing the card — a card missing its image or its PDP handle is
|
|
355
|
+
* still a usable recommendation. `title`/`currency`/`priceMinor`/`productId`/
|
|
356
|
+
* `available` stay OUT of this list on purpose: there is no honest way to
|
|
357
|
+
* show a product with no price, or a price with no currency, so a failure in
|
|
358
|
+
* one of those drops the whole card instead (see {@link buildWidgetChatCard}).
|
|
359
|
+
*/
|
|
360
|
+
var DROPPABLE_CARD_FIELDS = [
|
|
361
|
+
"variantId",
|
|
362
|
+
"platformVariantId",
|
|
363
|
+
"imageUrl",
|
|
364
|
+
"handle",
|
|
365
|
+
"reason"
|
|
366
|
+
];
|
|
367
|
+
/**
|
|
368
|
+
* Generic "try the whole candidate; if every refusal is on a DROPPABLE field,
|
|
369
|
+
* drop just those and retry once" parser — no card-specific knowledge at all.
|
|
370
|
+
*
|
|
371
|
+
* Exported so `apps/api`'s `act-destinations.ts` `buildActEvent`
|
|
372
|
+
* (`apply_filter`/`spotlight`/`navigate`'s events) can DELEGATE to this
|
|
373
|
+
* instead of keeping its own copy of the identical safeParse/prune/retry
|
|
374
|
+
* algorithm. The dependency has to run this direction — this package cannot
|
|
375
|
+
* import from `apps/api`, which depends on it — so this generic form lives
|
|
376
|
+
* here and `buildActEvent` becomes a thin wrapper around it. One
|
|
377
|
+
* implementation, called from both sides, rather than two that can drift on
|
|
378
|
+
* the next edit to either.
|
|
379
|
+
*/
|
|
380
|
+
function buildWithDroppableFields(schema, candidate, droppable = []) {
|
|
381
|
+
const first = schema.safeParse(candidate);
|
|
382
|
+
if (first.success) return first.data;
|
|
383
|
+
const offending = new Set(first.error.issues.map((issue) => String(issue.path[0] ?? "")));
|
|
384
|
+
const pruned = { ...candidate };
|
|
385
|
+
let droppedAny = false;
|
|
386
|
+
for (const key of droppable) {
|
|
387
|
+
if (!offending.has(key)) continue;
|
|
388
|
+
delete pruned[key];
|
|
389
|
+
droppedAny = true;
|
|
390
|
+
}
|
|
391
|
+
if (!droppedAny) return null;
|
|
392
|
+
const second = schema.safeParse(pruned);
|
|
393
|
+
return second.success ? second.data : null;
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Build ONE validated {@link WidgetChatCard} from a raw candidate record, or
|
|
397
|
+
* `null` when it cannot be rendered as a card at all.
|
|
398
|
+
*
|
|
399
|
+
* Tries the candidate whole first; if every refusal is on a
|
|
400
|
+
* {@link DROPPABLE_CARD_FIELDS} field, drops JUST those fields and retries
|
|
401
|
+
* once. A REQUIRED field failing has no fallback, so this returns `null` and
|
|
402
|
+
* the caller drops the whole candidate.
|
|
403
|
+
*
|
|
404
|
+
* ## Why this lives here, callable by both the live and the replay leg
|
|
405
|
+
*
|
|
406
|
+
* `build_curation` (`apps/api/src/catalog/tools/build-curation.tool.ts`, the
|
|
407
|
+
* live SSE leg) and {@link buildWidgetChatCardsEvent} (the replay leg, for a
|
|
408
|
+
* resumed session) each turn catalogue data into cards, and a catalogue row
|
|
409
|
+
* with one malformed optional field — an image URL on an underscore host, an
|
|
410
|
+
* IP-address image host, a unicode handle — is exactly as malformed whichever
|
|
411
|
+
* leg reads it. Before this function existed, only the live leg had this
|
|
412
|
+
* drop-the-field-not-the-card contract (via `apps/api`'s own `buildActEvent`);
|
|
413
|
+
* replay did a single `safeParse` with no retry and dropped the WHOLE card
|
|
414
|
+
* for a field the live leg could shrug off. A shopper who reloaded
|
|
415
|
+
* mid-conversation saw fewer cards than the turn had actually streamed. Both
|
|
416
|
+
* legs now call this one function, so the SAME product produces the SAME
|
|
417
|
+
* card — field dropped, not card dropped — regardless of which leg built it.
|
|
418
|
+
*
|
|
419
|
+
* ## An empty/whitespace-only string on a DROPPABLE field is normalized to
|
|
420
|
+
* absent, BEFORE either leg's candidate ever reaches {@link WidgetChatCardSchema}
|
|
421
|
+
*
|
|
422
|
+
* Live and replay build their OWN candidate record from different source
|
|
423
|
+
* shapes and, independently, made different choices about what "no reason"
|
|
424
|
+
* looks like: one omits an empty `reason` (a truthiness check), the other
|
|
425
|
+
* keeps it as `reason: ''` (a not-null check) — and {@link WidgetChatCardSchema}'s
|
|
426
|
+
* `reason` has no `.min(1)`, so `''` parses cleanly on either side, meaning
|
|
427
|
+
* neither call site's own guard was ever exercised by the schema and the two
|
|
428
|
+
* legs rendered DIFFERENT cards for the identical row (one with no `reason`
|
|
429
|
+
* key, one with `reason: ''`). Normalizing here — in the ONE function both
|
|
430
|
+
* legs call, rather than requiring every call site to independently agree on
|
|
431
|
+
* a null convention — is what actually closes that gap, for `reason` and for
|
|
432
|
+
* any other droppable string field a future caller adds the same way.
|
|
433
|
+
*/
|
|
434
|
+
function buildWidgetChatCard(candidate) {
|
|
435
|
+
const normalized = { ...candidate };
|
|
436
|
+
for (const key of DROPPABLE_CARD_FIELDS) {
|
|
437
|
+
const value = normalized[key];
|
|
438
|
+
if (typeof value === "string" && value.trim() === "") delete normalized[key];
|
|
439
|
+
}
|
|
440
|
+
return buildWithDroppableFields(WidgetChatCardSchema, normalized, DROPPABLE_CARD_FIELDS);
|
|
441
|
+
}
|
|
142
442
|
var WidgetChatEscalatedEventSchema = object({ type: literal("escalated") });
|
|
143
443
|
/**
|
|
144
444
|
* A real Shopify checkout handoff — `checkout_handoff` (the concierge tool)
|
|
@@ -221,6 +521,7 @@ var WidgetChatSseEventSchema = discriminatedUnion("type", [
|
|
|
221
521
|
WidgetChatEscalatedEventSchema,
|
|
222
522
|
WidgetChatCheckoutEventSchema,
|
|
223
523
|
WidgetChatAddToCartEventSchema,
|
|
524
|
+
...ACT_EVENT_SCHEMAS,
|
|
224
525
|
WidgetChatDoneEventSchema,
|
|
225
526
|
WidgetChatErrorEventSchema
|
|
226
527
|
]);
|
|
@@ -236,7 +537,8 @@ var WidgetToolEventSchema = discriminatedUnion("type", [
|
|
|
236
537
|
WidgetChatCardsEventSchema,
|
|
237
538
|
WidgetChatEscalatedEventSchema,
|
|
238
539
|
WidgetChatCheckoutEventSchema,
|
|
239
|
-
WidgetChatAddToCartEventSchema
|
|
540
|
+
WidgetChatAddToCartEventSchema,
|
|
541
|
+
...ACT_EVENT_SCHEMAS
|
|
240
542
|
]);
|
|
241
543
|
/**
|
|
242
544
|
* ## Storefront reports — the OTHER direction, and why they are not messages
|
|
@@ -342,7 +644,12 @@ var WidgetCartAddFailedReportSchema = object({
|
|
|
342
644
|
* costs the shopper nothing, and the widget's own status line still says the
|
|
343
645
|
* add did not land.
|
|
344
646
|
*/
|
|
345
|
-
var WidgetChatReportSchema = discriminatedUnion("kind", [
|
|
647
|
+
var WidgetChatReportSchema = discriminatedUnion("kind", [
|
|
648
|
+
WidgetCartAddReportSchema,
|
|
649
|
+
WidgetCartAddFailedReportSchema,
|
|
650
|
+
WidgetSpotlightFailedReportSchema,
|
|
651
|
+
WidgetNavigateFailedReportSchema
|
|
652
|
+
]);
|
|
346
653
|
/**
|
|
347
654
|
* Parse a request body's `report` field. Never throws — the caller decides
|
|
348
655
|
* whether `null` means "absent" (no report was sent) or "malformed", which
|
|
@@ -364,15 +671,20 @@ function extractWidgetToolEvent(output) {
|
|
|
364
671
|
/**
|
|
365
672
|
* Rebuild the `cards` event a curation was streamed as, for replay.
|
|
366
673
|
*
|
|
367
|
-
* **Per-item
|
|
368
|
-
* live: between the turn that curated a product and the
|
|
369
|
-
* it, the product can lose its price (a variant
|
|
370
|
-
* the scheme allowlist refuses, or have its
|
|
371
|
-
* {@link WidgetChatCardSchema}'s slug regex
|
|
372
|
-
* whole array at once would make ONE such row
|
|
373
|
-
* that turn; parsing per item drops only the
|
|
374
|
-
*
|
|
375
|
-
*
|
|
674
|
+
* **Per-item {@link buildWidgetChatCard}, not one parse of the whole array.**
|
|
675
|
+
* A catalogue is live: between the turn that curated a product and the
|
|
676
|
+
* reload that replays it, the product can lose its price (a variant
|
|
677
|
+
* deleted), gain an image URL the scheme allowlist refuses, or have its
|
|
678
|
+
* handle rewritten into something {@link WidgetChatCardSchema}'s slug regex
|
|
679
|
+
* will not accept. Parsing the whole array at once would make ONE such row
|
|
680
|
+
* cost the shopper every card in that turn; parsing per item drops only the
|
|
681
|
+
* row that no longer renders — and, for a droppable field, drops only the
|
|
682
|
+
* FIELD, via {@link buildWidgetChatCard}, the exact "degrade, never
|
|
683
|
+
* fabricate" contract the live leg (`build_curation`) applies to the same
|
|
684
|
+
* data. Before this called the shared builder, a per-item `safeParse` with no
|
|
685
|
+
* retry meant a droppable field's failure still cost the whole card here —
|
|
686
|
+
* so a product with, say, an image URL on an underscore host rendered on
|
|
687
|
+
* reload with one card fewer than the live turn had actually streamed.
|
|
376
688
|
*
|
|
377
689
|
* **Filter first, then cap.** The `MAX_CARDS_PER_EVENT` cap used to be taken
|
|
378
690
|
* as `items.slice(0, MAX)` BEFORE the drops below, so twenty rows that no
|
|
@@ -392,7 +704,7 @@ function buildWidgetChatCardsEvent(curationId, items) {
|
|
|
392
704
|
for (const item of items) {
|
|
393
705
|
if (item.priceMinor === null || item.currency === null) continue;
|
|
394
706
|
if (item.available !== true) continue;
|
|
395
|
-
const
|
|
707
|
+
const card = buildWidgetChatCard({
|
|
396
708
|
productId: item.productId,
|
|
397
709
|
...item.variantId !== null ? { variantId: item.variantId } : {},
|
|
398
710
|
...item.platformVariantId !== null ? { platformVariantId: item.platformVariantId } : {},
|
|
@@ -404,13 +716,13 @@ function buildWidgetChatCardsEvent(curationId, items) {
|
|
|
404
716
|
available: true,
|
|
405
717
|
...item.reason !== null ? { reason: item.reason } : {}
|
|
406
718
|
});
|
|
407
|
-
if (
|
|
719
|
+
if (card !== null) cards.push(card);
|
|
408
720
|
}
|
|
409
721
|
if (cards.length === 0) return null;
|
|
410
722
|
return {
|
|
411
723
|
type: "cards",
|
|
412
724
|
curationId,
|
|
413
|
-
items: cards.slice(0,
|
|
725
|
+
items: cards.slice(0, 20)
|
|
414
726
|
};
|
|
415
727
|
}
|
|
416
728
|
/**
|
|
@@ -589,4 +901,4 @@ function parseWidgetChatCart(value) {
|
|
|
589
901
|
return parsed.success ? parsed.data : null;
|
|
590
902
|
}
|
|
591
903
|
//#endregion
|
|
592
|
-
export { MAX_CART_LINES, WidgetCartAddFailedReportSchema, WidgetCartAddReportSchema, WidgetChatAddToCartEventSchema, WidgetChatCardSchema, WidgetChatCardsEventSchema, WidgetChatCartLineSchema, WidgetChatCartSchema, WidgetChatCartSourceSchema, WidgetChatCheckoutEventSchema, WidgetChatDoneEventSchema, WidgetChatErrorEventSchema, WidgetChatEscalatedEventSchema, WidgetChatReportSchema, WidgetChatSseEventSchema, WidgetChatTextDeltaEventSchema, WidgetToolEventSchema, buildWidgetChatCardsEvent, extractWidgetToolEvent, parseWidgetChatCart, parseWidgetChatReport };
|
|
904
|
+
export { DROPPABLE_CARD_FIELDS, MAX_CARDS_PER_EVENT, MAX_CART_LINES, WidgetCartAddFailedReportSchema, WidgetCartAddReportSchema, WidgetChatAddToCartEventSchema, WidgetChatCardSchema, WidgetChatCardsEventSchema, WidgetChatCartLineSchema, WidgetChatCartSchema, WidgetChatCartSourceSchema, WidgetChatCheckoutEventSchema, WidgetChatDoneEventSchema, WidgetChatErrorEventSchema, WidgetChatEscalatedEventSchema, WidgetChatReportSchema, WidgetChatSseEventSchema, WidgetChatTextDeltaEventSchema, WidgetToolEventSchema, buildWidgetChatCard, buildWidgetChatCardsEvent, buildWithDroppableFields, extractWidgetToolEvent, parseWidgetChatCart, parseWidgetChatReport };
|