agentic-ui-kit 0.8.0 → 0.9.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.
- package/README.md +19 -15
- package/dist/ask-user-CwDYhXX4.js +388 -0
- package/dist/core.js +3 -103
- package/dist/extensions-CJ4Asuxk.js +329 -0
- package/dist/index.js +43 -1394
- package/dist/partial-json-w_oXePqJ.js +111 -0
- package/dist/types/core/src/blocks.d.ts +1 -1
- package/dist/types/core/src/events.d.ts +1 -1
- package/dist/types/core/src/index.d.ts +9 -20
- package/dist/types/core/src/reduce.d.ts +2 -2
- package/dist/types/core/src/sse.d.ts +1 -1
- package/dist/types/core/src/ui-components.d.ts +1 -1
- package/dist/types/core/src/ui-markup.d.ts +1 -1
- package/dist/types/core/src/visuals.d.ts +11 -0
- package/dist/types/react/src/components/approval-card.d.ts +1 -1
- package/dist/types/react/src/components/ask-user.d.ts +1 -1
- package/dist/types/react/src/components/chat-list.d.ts +1 -1
- package/dist/types/react/src/components/composer.d.ts +1 -1
- package/dist/types/react/src/components/extensions.d.ts +50 -0
- package/dist/types/react/src/components/message-list.d.ts +1 -1
- package/dist/types/react/src/components/messages.d.ts +1 -1
- package/dist/types/react/src/components/tool-prompt.d.ts +1 -1
- package/dist/types/react/src/components/tools.d.ts +2 -2
- package/dist/types/react/src/hooks/sse-transport.d.ts +1 -1
- package/dist/types/react/src/hooks/use-chat.d.ts +1 -1
- package/dist/types/react/src/index.d.ts +86 -90
- package/dist/types/react/src/primitives/chat-theme.d.ts +3 -3
- package/dist/types/react/src/views/chat-transcript.d.ts +2 -5
- package/dist/types/react/src/views/chat-view.d.ts +4 -11
- package/dist/types/react/src/views/sidebar.d.ts +1 -1
- package/dist/types/react/src/visuals/core.d.ts +1 -0
- package/dist/types/react/src/visuals/index.d.ts +8 -0
- package/dist/types/react/src/visuals/provider.d.ts +26 -0
- package/dist/types/react/src/{components → visuals}/ui-components.d.ts +2 -2
- package/dist/types/react/src/{components → visuals}/ui-tree.d.ts +2 -1
- package/dist/types/react/src/{components → visuals}/widget-frame.d.ts +2 -1
- package/dist/{src-BrPYkHdt.js → visuals-CxCXnEob.js} +96 -490
- package/dist/visuals-core.js +1 -0
- package/dist/visuals.js +1134 -0
- package/package.json +9 -1
- /package/dist/types/react/src/{components → visuals}/replacements.d.ts +0 -0
|
@@ -1,427 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
const tryParse = (text) => {
|
|
3
|
-
try {
|
|
4
|
-
return JSON.parse(text);
|
|
5
|
-
} catch {
|
|
6
|
-
return;
|
|
7
|
-
}
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* Best-effort value of JSON that is still streaming, so a tool call's fields can show while the model
|
|
11
|
-
* writes them: `{"title":"Stand-up","day":"Tu` gives `{ title: "Stand-up", day: "Tu" }`. Open strings are
|
|
12
|
-
* cut where the text ends, open objects and arrays are closed, and a key without its value yet is left
|
|
13
|
-
* out. Returns undefined when nothing usable has arrived. One pass over the text.
|
|
14
|
-
*/
|
|
15
|
-
function parsePartialJson(text) {
|
|
16
|
-
const whole = tryParse(text);
|
|
17
|
-
if (whole !== void 0) return whole;
|
|
18
|
-
const stack = [];
|
|
19
|
-
const closers = () => stack.map((f) => f.close).reverse().join("");
|
|
20
|
-
let safe = 0;
|
|
21
|
-
let safeClose = "";
|
|
22
|
-
const valueDone = (end) => {
|
|
23
|
-
const top = stack.at(-1);
|
|
24
|
-
if (top) top.expect = "comma";
|
|
25
|
-
safe = end;
|
|
26
|
-
safeClose = closers();
|
|
27
|
-
};
|
|
28
|
-
let i = 0;
|
|
29
|
-
const n = text.length;
|
|
30
|
-
while (i < n) {
|
|
31
|
-
const ch = text[i];
|
|
32
|
-
if (ch === " " || ch === "\n" || ch === "\r" || ch === " ") {
|
|
33
|
-
i++;
|
|
34
|
-
continue;
|
|
35
|
-
}
|
|
36
|
-
const top = stack.at(-1);
|
|
37
|
-
if (ch === "\"") {
|
|
38
|
-
let j = i + 1;
|
|
39
|
-
let escaped = false;
|
|
40
|
-
for (; j < n; j++) {
|
|
41
|
-
const c = text[j];
|
|
42
|
-
if (escaped) escaped = false;
|
|
43
|
-
else if (c === "\\") escaped = true;
|
|
44
|
-
else if (c === "\"") break;
|
|
45
|
-
}
|
|
46
|
-
const isKey = top?.close === "}" && top.expect === "key";
|
|
47
|
-
if (j >= n) {
|
|
48
|
-
if (isKey) break;
|
|
49
|
-
const body = text.slice(i, n).replace(/\\(u[0-9a-fA-F]{0,3})?$/, "");
|
|
50
|
-
return tryParse(text.slice(0, i) + body + "\"" + closers()) ?? tryParse(text.slice(0, safe) + safeClose);
|
|
51
|
-
}
|
|
52
|
-
i = j + 1;
|
|
53
|
-
if (isKey) top.expect = "colon";
|
|
54
|
-
else valueDone(i);
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
if (ch === ":") {
|
|
58
|
-
if (top) top.expect = "value";
|
|
59
|
-
i++;
|
|
60
|
-
continue;
|
|
61
|
-
}
|
|
62
|
-
if (ch === ",") {
|
|
63
|
-
if (top) top.expect = top.close === "}" ? "key" : "value";
|
|
64
|
-
i++;
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
if (ch === "{" || ch === "[") {
|
|
68
|
-
stack.push({
|
|
69
|
-
close: ch === "{" ? "}" : "]",
|
|
70
|
-
expect: ch === "{" ? "key" : "value"
|
|
71
|
-
});
|
|
72
|
-
i++;
|
|
73
|
-
safe = i;
|
|
74
|
-
safeClose = closers();
|
|
75
|
-
continue;
|
|
76
|
-
}
|
|
77
|
-
if (ch === "}" || ch === "]") {
|
|
78
|
-
stack.pop();
|
|
79
|
-
i++;
|
|
80
|
-
valueDone(i);
|
|
81
|
-
continue;
|
|
82
|
-
}
|
|
83
|
-
let j = i;
|
|
84
|
-
while (j < n && !" \n\r ,]}:".includes(text[j])) j++;
|
|
85
|
-
if (j >= n) {
|
|
86
|
-
if (/^-?\d+(\.\d+)?$/.test(text.slice(i, j))) return tryParse(text.slice(0, j) + closers()) ?? tryParse(text.slice(0, safe) + safeClose);
|
|
87
|
-
break;
|
|
88
|
-
}
|
|
89
|
-
i = j;
|
|
90
|
-
valueDone(i);
|
|
91
|
-
}
|
|
92
|
-
return safe === 0 ? void 0 : tryParse(text.slice(0, safe) + safeClose);
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* JSON as far as it is valid: a stray bracket midway keeps what came before it, instead of losing it all.
|
|
96
|
-
* `error` says where it broke, for feedback to the model.
|
|
97
|
-
*/
|
|
98
|
-
function salvageJson(text) {
|
|
99
|
-
try {
|
|
100
|
-
return { value: JSON.parse(text) };
|
|
101
|
-
} catch (error) {
|
|
102
|
-
const m = /position (\d+)|line 1 column (\d+)/.exec(String(error));
|
|
103
|
-
const at = m ? Number(m[1] ?? Number(m[2]) - 1) : text.length;
|
|
104
|
-
const near = text.slice(Math.max(0, at - 30), at + 10);
|
|
105
|
-
return {
|
|
106
|
-
value: parsePartialJson(text.slice(0, at)),
|
|
107
|
-
error: `invalid JSON at character ${at}, near ${JSON.stringify(near)}`
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
// ../core/src/reduce.ts
|
|
112
|
-
/**
|
|
113
|
-
* Applies one stream event to the message it builds. Pure and immutable: returns the same object when the
|
|
114
|
-
* event changes nothing, so React can skip renders.
|
|
115
|
-
*
|
|
116
|
-
* Events with a seq at or below `message.lastSeq` are dropped, so replaying from an earlier seq after a
|
|
117
|
-
* reconnect is safe. Events other than run.start need the message to exist; without it they are dropped.
|
|
118
|
-
*/
|
|
119
|
-
function applyEvent(message, event) {
|
|
120
|
-
if (event.type === "run.start") {
|
|
121
|
-
if (message?.runId === event.runId && (message.lastSeq ?? -1) >= event.seq) return message;
|
|
122
|
-
if (message?.id === event.messageId) return {
|
|
123
|
-
...message,
|
|
124
|
-
status: "streaming",
|
|
125
|
-
runId: event.runId,
|
|
126
|
-
lastSeq: event.seq
|
|
127
|
-
};
|
|
128
|
-
return {
|
|
129
|
-
id: event.messageId,
|
|
130
|
-
parentId: event.parentId,
|
|
131
|
-
role: "assistant",
|
|
132
|
-
status: "streaming",
|
|
133
|
-
parts: [],
|
|
134
|
-
createdAt: event.createdAt,
|
|
135
|
-
runId: event.runId,
|
|
136
|
-
lastSeq: event.seq,
|
|
137
|
-
...event.model !== void 0 && { model: event.model },
|
|
138
|
-
...event.effort !== void 0 && { effort: event.effort }
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
if (!message || message.runId !== event.runId) return message;
|
|
142
|
-
if ((message.lastSeq ?? -1) >= event.seq) return message;
|
|
143
|
-
const seen = {
|
|
144
|
-
...message,
|
|
145
|
-
lastSeq: event.seq
|
|
146
|
-
};
|
|
147
|
-
switch (event.type) {
|
|
148
|
-
case "part.start":
|
|
149
|
-
if (message.parts.some((p) => p.id === event.part.id)) return seen;
|
|
150
|
-
return {
|
|
151
|
-
...seen,
|
|
152
|
-
parts: [...message.parts, event.part]
|
|
153
|
-
};
|
|
154
|
-
case "part.delta": return updatePart(seen, event.partId, (part) => appendDelta(part, event.text, event.inputJson));
|
|
155
|
-
case "part.update": return updatePart(seen, event.partId, (part) => applyFields(part, event));
|
|
156
|
-
case "part.end": return updatePart(seen, event.partId, (part) => closePart(applyFields(part, event)));
|
|
157
|
-
case "run.usage": return {
|
|
158
|
-
...seen,
|
|
159
|
-
usage: event.usage
|
|
160
|
-
};
|
|
161
|
-
case "run.end": return {
|
|
162
|
-
...seen,
|
|
163
|
-
status: event.status,
|
|
164
|
-
parts: message.parts.map((p) => closeOpenPart(p, event.status)),
|
|
165
|
-
...event.usage !== void 0 && { usage: event.usage },
|
|
166
|
-
...event.error !== void 0 && { error: event.error }
|
|
167
|
-
};
|
|
168
|
-
case "title": return seen;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
/** Builds a message from a full event list, e.g. a stored run or a test fixture. */
|
|
172
|
-
function reduceEvents(events, initial) {
|
|
173
|
-
let message = initial;
|
|
174
|
-
for (const event of events) message = applyEvent(message, event);
|
|
175
|
-
return message;
|
|
176
|
-
}
|
|
177
|
-
function updatePart(message, partId, update) {
|
|
178
|
-
const index = message.parts.findIndex((p) => p.id === partId);
|
|
179
|
-
if (index === -1) return message;
|
|
180
|
-
const parts = message.parts.slice();
|
|
181
|
-
parts[index] = update(parts[index]);
|
|
182
|
-
return {
|
|
183
|
-
...message,
|
|
184
|
-
parts
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
function appendDelta(part, text = "", inputJson = "") {
|
|
188
|
-
switch (part.type) {
|
|
189
|
-
case "text":
|
|
190
|
-
case "thinking": return text ? {
|
|
191
|
-
...part,
|
|
192
|
-
text: part.text + text
|
|
193
|
-
} : part;
|
|
194
|
-
case "custom": return text ? {
|
|
195
|
-
...part,
|
|
196
|
-
text: (part.text ?? "") + text
|
|
197
|
-
} : part;
|
|
198
|
-
case "widget": return text ? {
|
|
199
|
-
...part,
|
|
200
|
-
html: (part.html ?? "") + text
|
|
201
|
-
} : part;
|
|
202
|
-
case "tool_call":
|
|
203
|
-
if (!inputJson) return part;
|
|
204
|
-
return {
|
|
205
|
-
...part,
|
|
206
|
-
inputJson: part.inputJson + inputJson,
|
|
207
|
-
partialInput: parsePartialJson(part.inputJson + inputJson)
|
|
208
|
-
};
|
|
209
|
-
case "attachment": return part;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
/** Copies the fields from `fields` that are valid for the part's type. */
|
|
213
|
-
function applyFields(part, fields) {
|
|
214
|
-
const pick = (...keys) => {
|
|
215
|
-
const out = {};
|
|
216
|
-
for (const key of keys) if (fields[key] !== void 0) out[key] = fields[key];
|
|
217
|
-
return out;
|
|
218
|
-
};
|
|
219
|
-
const status = pick("status");
|
|
220
|
-
switch (part.type) {
|
|
221
|
-
case "text": return {
|
|
222
|
-
...part,
|
|
223
|
-
...status
|
|
224
|
-
};
|
|
225
|
-
case "thinking": return {
|
|
226
|
-
...part,
|
|
227
|
-
...status,
|
|
228
|
-
...pick("title", "durationMs")
|
|
229
|
-
};
|
|
230
|
-
case "tool_call": {
|
|
231
|
-
const next = {
|
|
232
|
-
...part,
|
|
233
|
-
...status,
|
|
234
|
-
...pick("title", "subtitle", "durationMs", "input", "result", "data", "error", "needsApproval")
|
|
235
|
-
};
|
|
236
|
-
return next.status === "input" || next.status === "streaming" ? next : withParsedInput(next);
|
|
237
|
-
}
|
|
238
|
-
case "attachment": return {
|
|
239
|
-
...part,
|
|
240
|
-
...status,
|
|
241
|
-
...pick("url")
|
|
242
|
-
};
|
|
243
|
-
case "widget": return {
|
|
244
|
-
...part,
|
|
245
|
-
...status,
|
|
246
|
-
...pick("title", "resourceUri")
|
|
247
|
-
};
|
|
248
|
-
case "custom": return {
|
|
249
|
-
...part,
|
|
250
|
-
...status,
|
|
251
|
-
...pick("data")
|
|
252
|
-
};
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
function closePart(part) {
|
|
256
|
-
return withParsedInput(part.status === "error" ? part : {
|
|
257
|
-
...part,
|
|
258
|
-
status: "done"
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
/** Parses a tool call's streamed input if it is complete JSON and not parsed yet. */
|
|
262
|
-
function withParsedInput(part) {
|
|
263
|
-
if (part.type !== "tool_call") return part;
|
|
264
|
-
const { partialInput: _, ...rest } = part;
|
|
265
|
-
if (part.input !== void 0 || !part.inputJson) return "partialInput" in part ? rest : part;
|
|
266
|
-
const input = parseJson(part.inputJson);
|
|
267
|
-
if (input !== void 0) return {
|
|
268
|
-
...rest,
|
|
269
|
-
input
|
|
270
|
-
};
|
|
271
|
-
return part.status === "error" ? rest : {
|
|
272
|
-
...rest,
|
|
273
|
-
status: "error",
|
|
274
|
-
error: part.error ?? "The tool input was cut off or is not valid JSON"
|
|
275
|
-
};
|
|
276
|
-
}
|
|
277
|
-
/** At the end of a run, parts still open are closed. A tool call cut off by a stop or an error is an error. */
|
|
278
|
-
function closeOpenPart(part, runStatus) {
|
|
279
|
-
if (part.status === "done" || part.status === "error") return part;
|
|
280
|
-
if (part.type === "tool_call" && part.status === "awaiting" && runStatus === "done") return withParsedInput(part);
|
|
281
|
-
if (part.type === "tool_call" && runStatus !== "done") {
|
|
282
|
-
const error = part.error ?? (runStatus === "stopped" ? "Stopped" : "Failed");
|
|
283
|
-
return withParsedInput({
|
|
284
|
-
...part,
|
|
285
|
-
status: "error",
|
|
286
|
-
error
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
return closePart(part);
|
|
290
|
-
}
|
|
291
|
-
function parseJson(text) {
|
|
292
|
-
try {
|
|
293
|
-
return JSON.parse(text);
|
|
294
|
-
} catch {
|
|
295
|
-
return;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
/** Rough token count of text, about four characters per token. For live counters, not billing. */
|
|
299
|
-
function estimateTokens(text) {
|
|
300
|
-
return Math.ceil(text.length / 4);
|
|
301
|
-
}
|
|
302
|
-
/**
|
|
303
|
-
* Tokens the model spent inside an activity group: its reasoning and the tool inputs it wrote. Tool
|
|
304
|
-
* results are not model output, so they do not count. An estimate; per-step counts are not reported.
|
|
305
|
-
*/
|
|
306
|
-
function activityTokens(parts) {
|
|
307
|
-
let chars = 0;
|
|
308
|
-
for (const part of parts) if (part.type === "thinking") chars += part.text.length;
|
|
309
|
-
else if (part.type === "tool_call") chars += part.inputJson.length;
|
|
310
|
-
return Math.ceil(chars / 4);
|
|
311
|
-
}
|
|
312
|
-
/**
|
|
313
|
-
* Output tokens of a message so far: the backend's count when it has sent one, else an estimate from
|
|
314
|
-
* everything the model has written (text, thinking, tool input, widget html).
|
|
315
|
-
*/
|
|
316
|
-
function outputTokens(message) {
|
|
317
|
-
if (message.usage?.outputTokens !== void 0) return message.usage.outputTokens;
|
|
318
|
-
let chars = 0;
|
|
319
|
-
for (const part of message.parts) if (part.type === "text" || part.type === "thinking") chars += part.text.length;
|
|
320
|
-
else if (part.type === "tool_call") chars += part.inputJson.length;
|
|
321
|
-
else if (part.type === "widget") chars += part.html?.length ?? 0;
|
|
322
|
-
else if (part.type === "custom") chars += part.text?.length ?? 0;
|
|
323
|
-
return Math.ceil(chars / 4);
|
|
324
|
-
}
|
|
325
|
-
/**
|
|
326
|
-
* The effort to send with a model: the chosen one if the model offers it, else the model's default, else
|
|
327
|
-
* none. Models without effort levels never get one.
|
|
328
|
-
*/
|
|
329
|
-
function resolveEffort(model, effort) {
|
|
330
|
-
const levels = model?.efforts;
|
|
331
|
-
if (!levels?.length) return void 0;
|
|
332
|
-
if (effort && levels.some((e) => e.id === effort)) return effort;
|
|
333
|
-
return model?.defaultEffort ?? levels[0].id;
|
|
334
|
-
}
|
|
335
|
-
/** Tool calls the client has to act on: run them (client tools) or ask the user (approvals). */
|
|
336
|
-
function awaitingToolCalls(message) {
|
|
337
|
-
return message.parts.filter((p) => p.type === "tool_call" && p.status === "awaiting");
|
|
338
|
-
}
|
|
339
|
-
/** Sets fields on one tool call locally, e.g. a client tool's result or the user's approval. */
|
|
340
|
-
function settleToolCall(message, partId, fields) {
|
|
341
|
-
return {
|
|
342
|
-
...message,
|
|
343
|
-
parts: message.parts.map((p) => p.id === partId && p.type === "tool_call" ? {
|
|
344
|
-
...p,
|
|
345
|
-
...fields
|
|
346
|
-
} : p)
|
|
347
|
-
};
|
|
348
|
-
}
|
|
349
|
-
// ../core/src/blocks.ts
|
|
350
|
-
function isActivityPart(part) {
|
|
351
|
-
return part.type === "thinking" || part.type === "tool_call";
|
|
352
|
-
}
|
|
353
|
-
function toBlocks(parts, standalone) {
|
|
354
|
-
const blocks = [];
|
|
355
|
-
for (const part of parts) {
|
|
356
|
-
if (part.type === "tool_call" && standalone?.(part)) {
|
|
357
|
-
blocks.push({
|
|
358
|
-
kind: "tool",
|
|
359
|
-
id: part.id,
|
|
360
|
-
part
|
|
361
|
-
});
|
|
362
|
-
continue;
|
|
363
|
-
}
|
|
364
|
-
if (!isActivityPart(part)) {
|
|
365
|
-
blocks.push({
|
|
366
|
-
kind: "part",
|
|
367
|
-
id: part.id,
|
|
368
|
-
part
|
|
369
|
-
});
|
|
370
|
-
continue;
|
|
371
|
-
}
|
|
372
|
-
const last = blocks.at(-1);
|
|
373
|
-
if (last?.kind === "activity") last.parts.push(part);
|
|
374
|
-
else blocks.push({
|
|
375
|
-
kind: "activity",
|
|
376
|
-
id: part.id,
|
|
377
|
-
parts: [part]
|
|
378
|
-
});
|
|
379
|
-
}
|
|
380
|
-
return blocks;
|
|
381
|
-
}
|
|
382
|
-
// ../core/src/sse.ts
|
|
383
|
-
/**
|
|
384
|
-
* Reads a server-sent event stream of `StreamEvent`s. Each SSE message carries one JSON event in `data`;
|
|
385
|
-
* comments and messages without data (keep-alives) are skipped.
|
|
386
|
-
*/
|
|
387
|
-
async function* readEventStream(body) {
|
|
388
|
-
const reader = body.getReader();
|
|
389
|
-
const decoder = new TextDecoder();
|
|
390
|
-
let buffer = "";
|
|
391
|
-
try {
|
|
392
|
-
for (;;) {
|
|
393
|
-
const { value, done } = await reader.read();
|
|
394
|
-
if (done) break;
|
|
395
|
-
buffer += decoder.decode(value, { stream: true });
|
|
396
|
-
let end;
|
|
397
|
-
while ((end = messageEnd(buffer)) !== -1) {
|
|
398
|
-
const event = parseMessage(buffer.slice(0, end));
|
|
399
|
-
buffer = buffer.slice(end).replace(BLANK_LINE_START, "");
|
|
400
|
-
if (event) yield event;
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
const event = parseMessage(buffer + decoder.decode());
|
|
404
|
-
if (event) yield event;
|
|
405
|
-
} finally {
|
|
406
|
-
reader.releaseLock();
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
/** Formats one event as an SSE message, for servers written in TS. */
|
|
410
|
-
function formatEvent(event) {
|
|
411
|
-
return `id: ${event.seq}\ndata: ${JSON.stringify(event)}\n\n`;
|
|
412
|
-
}
|
|
413
|
-
const BLANK_LINE = /\r\n\r\n|\n\n|\r\r/;
|
|
414
|
-
const BLANK_LINE_START = /^(\r\n\r\n|\n\n|\r\r)/;
|
|
415
|
-
function messageEnd(buffer) {
|
|
416
|
-
const match = BLANK_LINE.exec(buffer);
|
|
417
|
-
return match ? match.index : -1;
|
|
418
|
-
}
|
|
419
|
-
function parseMessage(text) {
|
|
420
|
-
const data = [];
|
|
421
|
-
for (const line of text.split(/\r\n|\r|\n/)) if (line.startsWith("data:")) data.push(line.slice(line.startsWith("data: ") ? 6 : 5));
|
|
422
|
-
if (data.length === 0) return void 0;
|
|
423
|
-
return JSON.parse(data.join("\n"));
|
|
424
|
-
}
|
|
1
|
+
import { n as salvageJson } from "./partial-json-w_oXePqJ.js";
|
|
425
2
|
// ../core/src/widget-loader.ts
|
|
426
3
|
/**
|
|
427
4
|
* The frame page for `WidgetHost.frameUrl`. Serve this HTML from your own origin (e.g. at /widget-frame.html)
|
|
@@ -481,77 +58,106 @@ function withCallId(id, feedback) {
|
|
|
481
58
|
function resolveReplaces(ref, ids, titles) {
|
|
482
59
|
return ids.has(ref) ? ref : titles.get(ref);
|
|
483
60
|
}
|
|
484
|
-
// ../core/src/
|
|
61
|
+
// ../core/src/visualize.ts
|
|
62
|
+
/**
|
|
63
|
+
* The visualize preset: a tool the model calls to draw an interactive visual inline (similar to claude.ai's
|
|
64
|
+
* visualize tool), and the design guide to put in the system prompt. The kit's `show_widget` tool view feeds
|
|
65
|
+
* the streaming `widget_code` into a sandboxed WidgetFrame. The backend runs the tool as a no-op ("shown").
|
|
66
|
+
*/
|
|
485
67
|
/** The tool definition to give the model. Same shape as a page tool's `ToolSpec`. */
|
|
486
|
-
const
|
|
487
|
-
name: "
|
|
488
|
-
description: "
|
|
68
|
+
const visualizeTool = {
|
|
69
|
+
name: "show_widget",
|
|
70
|
+
description: "Draw an interactive visual inline in your reply: a chart, diagram, map, calendar, calculator, planner. Write an HTML fragment or an <svg> in widget_code; it runs in a sandboxed frame that matches the chat's theme. Follow the visual design guide in your instructions.",
|
|
489
71
|
inputSchema: {
|
|
490
72
|
type: "object",
|
|
491
|
-
properties: {
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
},
|
|
511
|
-
options: {
|
|
512
|
-
type: "array",
|
|
513
|
-
items: { type: "string" },
|
|
514
|
-
minItems: 2,
|
|
515
|
-
maxItems: 8,
|
|
516
|
-
description: "Short option texts. Do not add a catch-all: the user can always type their own answer."
|
|
517
|
-
}
|
|
518
|
-
},
|
|
519
|
-
required: ["question", "options"]
|
|
520
|
-
}
|
|
521
|
-
} },
|
|
522
|
-
required: ["questions"]
|
|
73
|
+
properties: {
|
|
74
|
+
widget_code: {
|
|
75
|
+
type: "string",
|
|
76
|
+
description: "An HTML fragment (no <html>, <head> or <body>) or an <svg>. Inline <style> and <script> are allowed."
|
|
77
|
+
},
|
|
78
|
+
title: {
|
|
79
|
+
type: "string",
|
|
80
|
+
description: "Short snake_case name for the visual, e.g. gym_week."
|
|
81
|
+
},
|
|
82
|
+
loading_messages: {
|
|
83
|
+
type: "array",
|
|
84
|
+
items: { type: "string" },
|
|
85
|
+
minItems: 1,
|
|
86
|
+
maxItems: 4,
|
|
87
|
+
description: "1-4 short lines shown while you write it, e.g. \"Finding free slots\"."
|
|
88
|
+
},
|
|
89
|
+
replaces: replacesProperty
|
|
90
|
+
},
|
|
91
|
+
required: ["widget_code"]
|
|
523
92
|
}
|
|
524
93
|
};
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
94
|
+
/** Design guide for the system prompt when the visualize tool is on. */
|
|
95
|
+
const visualizeGuide = `## Visuals (show_widget)
|
|
96
|
+
Use show_widget when a visual says it better than text: data to compare, a plan to adjust, a place to see, a
|
|
97
|
+
process to step through. Keep explaining in text around it; the widget is not the whole answer.
|
|
98
|
+
|
|
99
|
+
How the frame works
|
|
100
|
+
- widget_code is a fragment: no <!doctype>, <html>, <head> or <body>. Put <style> first, then markup, then <script>.
|
|
101
|
+
- It renders while you write it; scripts run once it is complete, in order. Put scripts last.
|
|
102
|
+
- No network unless the host allows it: no fetch, no external images or fonts. Inline your data.
|
|
103
|
+
- The background is transparent and the frame sizes itself to your content. Add no outer margin or padding,
|
|
104
|
+
no outer border or card around the whole widget, and no fixed height on the root.
|
|
105
|
+
- Two globals: sendPrompt(text) sends a message as the user (for "Add these to my calendar", "Explain this"),
|
|
106
|
+
openLink(url) opens an http(s) link. Prefer local state (JavaScript in the widget) over round trips: filter,
|
|
107
|
+
toggle, recalculate and animate in place; use sendPrompt only for what needs you.
|
|
108
|
+
- ${replacesGuide}
|
|
109
|
+
|
|
110
|
+
Look
|
|
111
|
+
- Use the chat's theme variables, never hard-coded colors or fonts: var(--chat-fg) text, var(--chat-muted) and
|
|
112
|
+
var(--chat-subtle) secondary text, var(--chat-surface) and var(--chat-raised) fills, var(--chat-border) and
|
|
113
|
+
var(--chat-divider) lines, var(--chat-hover) hover, var(--chat-accent) with var(--chat-accent-fg) for the one
|
|
114
|
+
thing that matters most, var(--chat-check) good, var(--chat-warning), var(--chat-danger) bad;
|
|
115
|
+
var(--chat-font-sans), var(--chat-font-mono); var(--chat-ui-font-size), var(--chat-ui-font-size-sm);
|
|
116
|
+
var(--chat-radius-sm), var(--chat-radius-md), var(--chat-radius-lg).
|
|
117
|
+
- Quiet by default: thin borders, few fills, one accent. Readable labels, aligned numbers, no decoration.
|
|
118
|
+
- Set apart what you added or changed from what was already there: your proposals in the accent with a small
|
|
119
|
+
"New" (or "Changed") tag, existing data quiet. The user must see at a glance what is yours.
|
|
120
|
+
- Make it work on a narrow screen (down to 320px wide) and with a keyboard: real <button>s, visible focus.`;
|
|
121
|
+
const originOf = (url) => {
|
|
122
|
+
try {
|
|
123
|
+
return new URL(url).origin.toLowerCase();
|
|
124
|
+
} catch {
|
|
125
|
+
return;
|
|
547
126
|
}
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Checks widget code before it reaches the user, so a harness can send problems back to the model as the
|
|
130
|
+
* tool result. `allow` is what the host lets widgets load (the same `allow` as the chat's `widgets`).
|
|
131
|
+
*/
|
|
132
|
+
function checkWidgetCode(html, allow = {}, toolName = visualizeTool.name) {
|
|
133
|
+
const errors = [];
|
|
134
|
+
const warnings = [];
|
|
135
|
+
if (typeof html !== "string" || !html.trim()) errors.push("widget_code: required, an HTML fragment or an <svg>");
|
|
136
|
+
else {
|
|
137
|
+
if (/<!doctype|<html[\s>]|<head[\s>]|<body[\s>]/i.test(html)) errors.push("Send a fragment: no <!doctype>, <html>, <head> or <body>.");
|
|
138
|
+
const allowed = (allow.resourceDomains ?? []).map((d) => d.toLowerCase().replace(/\/$/, ""));
|
|
139
|
+
const fits = (origin) => allowed.some((a) => a === origin || a.includes("*.") && origin.endsWith(a.slice(a.indexOf("*.") + 1)) && origin.startsWith(a.slice(0, a.indexOf("*."))));
|
|
140
|
+
const blocked = /* @__PURE__ */ new Set();
|
|
141
|
+
for (const m of html.matchAll(/(?:src|href)\s*=\s*["']?(https?:\/\/[^"'\s>]+)|url\(\s*["']?(https?:\/\/[^"')\s]+)|@import\s+["'](https?:\/\/[^"']+)/gi)) {
|
|
142
|
+
const origin = originOf(m[1] ?? m[2] ?? m[3] ?? "");
|
|
143
|
+
if (origin && !fits(origin) && !/<a\s[^>]*$/i.test(html.slice(0, m.index))) blocked.add(origin);
|
|
144
|
+
}
|
|
145
|
+
if (blocked.size) errors.push(`Blocked by the sandbox: ${[...blocked].join(", ")}. ${allowed.length ? `Only ${allowed.join(", ")} may be loaded.` : "Nothing external may be loaded."} Inline it instead.`);
|
|
146
|
+
if (!(allow.connectDomains ?? []).length && /\bfetch\s*\(|XMLHttpRequest|new\s+WebSocket|EventSource\s*\(/.test(html)) errors.push("Network calls (fetch, XHR, WebSocket) are blocked: inline the data.");
|
|
147
|
+
if (/\b(localStorage|sessionStorage|document\.cookie|indexedDB)\b/.test(html)) errors.push("Storage (localStorage, cookies, indexedDB) is not available in the sandbox: keep state in variables.");
|
|
148
|
+
if (/\b(alert|confirm|prompt)\s*\(/.test(html)) errors.push("alert/confirm/prompt are blocked: show it in the widget, or use sendPrompt(text).");
|
|
149
|
+
if (/<form[\s>]/i.test(html) && /type\s*=\s*["']?submit/i.test(html)) warnings.push("Form submits are blocked: handle the button's click in a script.");
|
|
150
|
+
const colors = new Set([...html.matchAll(/(?<![\w-])(#[0-9a-f]{3,8}\b|rgba?\([^)]*\)|hsla?\([^)]*\))/gi)].map((m) => m[1].toLowerCase()));
|
|
151
|
+
if (colors.size) warnings.push(`Hard-coded colors (${[...colors].slice(0, 5).join(", ")}${colors.size > 5 ? ", ..." : ""}): use var(--chat-*) so it matches the theme.`);
|
|
152
|
+
}
|
|
153
|
+
const ok = errors.length === 0;
|
|
154
|
+
const lines = [...errors, ...warnings].map((e) => `- ${e}`).join("\n");
|
|
155
|
+
return {
|
|
156
|
+
ok,
|
|
157
|
+
errors,
|
|
158
|
+
warnings,
|
|
159
|
+
feedback: ok && !warnings.length ? "Shown to the user." : ok ? `Shown to the user, and it works: do not draw it again now. Next time, to fit the chat better:\n${lines}` : `Shown, but it will not work as written. Fix these and call ${toolName} again with "replaces" set to this call's id, without remarking on the fix to the user:\n${lines}`
|
|
160
|
+
};
|
|
555
161
|
}
|
|
556
162
|
// ../core/src/ui-tree.ts
|
|
557
163
|
/**
|
|
@@ -1427,4 +1033,4 @@ function uiSpecSet({ include, exclude } = {}) {
|
|
|
1427
1033
|
}
|
|
1428
1034
|
/** All of `uiSpecs` as a list: `uiSpecSet()`. */
|
|
1429
1035
|
const uiComponentSpecs = uiSpecSet();
|
|
1430
|
-
export { widgetLoaderMessages as A,
|
|
1036
|
+
export { widgetLoaderMessages as A, visualizeTool as C, resolveReplaces as D, replacesProperty as E, withCallId as O, visualizeGuide as S, replacesGuide as T, renderUiTool as _, checkUiMarkup as a, uiActionSchema as b, parseUiMarkup as c, showUiToolName as d, uiMarkupGuide as f, matchesSchema as g, isStateRef as h, uiSpecs as i, widgetFrameLoader as k, readUiValue as l, checkUiTree as m, uiSpecGroups as n, checkUiMarkupCode as o, uiMarkupLanguage as p, uiSpecSet as r, findUiBlocks as s, uiComponentSpecs as t, showUiTool as u, renderUiToolName as v, replacedId as w, checkWidgetCode as x, schemaErrors as y };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { a as checkUiMarkup, o as checkUiMarkupCode, m as checkUiTree, x as checkWidgetCode, s as findUiBlocks, h as isStateRef, g as matchesSchema, c as parseUiMarkup, l as readUiValue, _ as renderUiTool, v as renderUiToolName, w as replacedId, T as replacesGuide, E as replacesProperty, D as resolveReplaces, y as schemaErrors, u as showUiTool, d as showUiToolName, b as uiActionSchema, t as uiComponentSpecs, f as uiMarkupGuide, p as uiMarkupLanguage, n as uiSpecGroups, r as uiSpecSet, i as uiSpecs, S as visualizeGuide, C as visualizeTool, k as widgetFrameLoader, A as widgetLoaderMessages, O as withCallId } from "./visuals-CxCXnEob.js";
|