@guideai/react-native 0.1.0 → 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.
- package/README.md +27 -0
- package/dist/index.cjs +114 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +245 -7
- package/dist/index.mjs +113 -4
- package/dist/index.mjs.map +1 -1
- package/dist/{useOnboarding-C7eFhp2o.cjs → setup-Bl0ZLz8B.cjs} +134 -313
- package/dist/setup-Bl0ZLz8B.cjs.map +1 -0
- package/dist/{useOnboarding-DhVtf0U2.js → setup-CkXajERE.js} +135 -314
- package/dist/setup-CkXajERE.js.map +1 -0
- package/dist/ui.cjs +1293 -34
- package/dist/ui.cjs.map +1 -1
- package/dist/ui.d.ts +7 -2
- package/dist/ui.mjs +1287 -28
- package/dist/ui.mjs.map +1 -1
- package/dist/useOnboarding-BAKQq8T_.js +677 -0
- package/dist/useOnboarding-BAKQq8T_.js.map +1 -0
- package/dist/useOnboarding-CFxofFst.cjs +676 -0
- package/dist/useOnboarding-CFxofFst.cjs.map +1 -0
- package/dist/voice.cjs +89 -0
- package/dist/voice.cjs.map +1 -0
- package/dist/voice.d.ts +161 -0
- package/dist/voice.mjs +89 -0
- package/dist/voice.mjs.map +1 -0
- package/package.json +18 -4
- package/dist/useOnboarding-C7eFhp2o.cjs.map +0 -1
- package/dist/useOnboarding-DhVtf0U2.js.map +0 -1
|
@@ -0,0 +1,676 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const React = require("react");
|
|
3
|
+
const setup = require("./setup-Bl0ZLz8B.cjs");
|
|
4
|
+
const DRAWING_URL = /^https:\/\/res\.cloudinary\.com\/[\w-]+\/image\/upload\//;
|
|
5
|
+
const IMAGE_BLOCK = /```image[ \t]*\n([\s\S]*?)```/g;
|
|
6
|
+
function headlessImages(text) {
|
|
7
|
+
const images = [];
|
|
8
|
+
const out = (text || "").replace(IMAGE_BLOCK, (_block, raw) => {
|
|
9
|
+
let data;
|
|
10
|
+
try {
|
|
11
|
+
data = JSON.parse(raw.trim());
|
|
12
|
+
} catch {
|
|
13
|
+
return "";
|
|
14
|
+
}
|
|
15
|
+
if (typeof data.url !== "string" || !DRAWING_URL.test(data.url)) return "";
|
|
16
|
+
const image = {
|
|
17
|
+
url: data.url,
|
|
18
|
+
svg: typeof data.svg === "string" && DRAWING_URL.test(data.svg) ? data.svg : null,
|
|
19
|
+
alt: typeof data.alt === "string" ? data.alt.slice(0, 200) : "",
|
|
20
|
+
width: Math.round(Number(data.width)) || 1600,
|
|
21
|
+
height: Math.round(Number(data.height)) || 900
|
|
22
|
+
};
|
|
23
|
+
images.push(image);
|
|
24
|
+
return `![${image.alt.replace(/[[\]]/g, "")}](${image.url})`;
|
|
25
|
+
});
|
|
26
|
+
return { text: out, images };
|
|
27
|
+
}
|
|
28
|
+
const LIMITS = {
|
|
29
|
+
steps: 12,
|
|
30
|
+
itemsPerStep: 8,
|
|
31
|
+
items: 60,
|
|
32
|
+
say: 300,
|
|
33
|
+
text: 60,
|
|
34
|
+
art: 80,
|
|
35
|
+
title: 80,
|
|
36
|
+
freePoints: 32,
|
|
37
|
+
tableRows: 6,
|
|
38
|
+
tableCols: 4,
|
|
39
|
+
cell: 24,
|
|
40
|
+
artContours: 400,
|
|
41
|
+
contourChars: 2e4
|
|
42
|
+
};
|
|
43
|
+
const INK_KINDS = [
|
|
44
|
+
"line",
|
|
45
|
+
"arrow",
|
|
46
|
+
"poly",
|
|
47
|
+
"curve",
|
|
48
|
+
"free",
|
|
49
|
+
"oval",
|
|
50
|
+
"corner",
|
|
51
|
+
"arc",
|
|
52
|
+
"dot",
|
|
53
|
+
"circle",
|
|
54
|
+
"box",
|
|
55
|
+
"underline",
|
|
56
|
+
"cross",
|
|
57
|
+
"tick",
|
|
58
|
+
"bracket"
|
|
59
|
+
];
|
|
60
|
+
const POINT_INKS = ["poly", "curve", "free", "corner", "arc"];
|
|
61
|
+
const BOARD_W = 160;
|
|
62
|
+
const BOARD_H = 100;
|
|
63
|
+
const POINT_MARKS = ["circle", "underline", "box"];
|
|
64
|
+
class SceneError extends Error {
|
|
65
|
+
constructor() {
|
|
66
|
+
super(...arguments);
|
|
67
|
+
this.name = "SceneError";
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const ID = /^[A-Za-z0-9_-]{1,24}$/;
|
|
71
|
+
const SIZES = ["s", "m", "l"];
|
|
72
|
+
const TONES = ["ink", "blue", "red", "green"];
|
|
73
|
+
const PATH_DATA = /^[MmLlHhVvCcSsQqTtAaZz0-9eE.,\s+-]*$/;
|
|
74
|
+
const flat = (value, max) => typeof value === "string" ? value.replace(/\s+/g, " ").trim().slice(0, max) : "";
|
|
75
|
+
const clamp = (n, top = BOARD_H) => Math.round(Math.min(top, Math.max(0, n)) * 10) / 10;
|
|
76
|
+
function num(value) {
|
|
77
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
78
|
+
}
|
|
79
|
+
let wide = 1;
|
|
80
|
+
function point(raw) {
|
|
81
|
+
if (!Array.isArray(raw) || raw.length !== 2) return null;
|
|
82
|
+
const [x, y] = raw.map(num);
|
|
83
|
+
return x === null || y === null ? null : [clamp(x * wide, BOARD_W), clamp(y)];
|
|
84
|
+
}
|
|
85
|
+
function box(raw) {
|
|
86
|
+
if (!Array.isArray(raw) || raw.length !== 4) return null;
|
|
87
|
+
const v = raw.map(num);
|
|
88
|
+
if (v.some((n) => n === null)) return null;
|
|
89
|
+
const x = clamp(v[0] * wide, BOARD_W);
|
|
90
|
+
const y = clamp(v[1]);
|
|
91
|
+
const w = Math.min(Math.max(1, v[2] * wide), BOARD_W - x);
|
|
92
|
+
const h = Math.min(Math.max(1, v[3]), BOARD_H - y);
|
|
93
|
+
return w >= 1 && h >= 1 ? [x, y, w, h] : null;
|
|
94
|
+
}
|
|
95
|
+
function tone(raw) {
|
|
96
|
+
return typeof raw === "string" && TONES.includes(raw) ? raw : "ink";
|
|
97
|
+
}
|
|
98
|
+
function id(raw) {
|
|
99
|
+
return typeof raw === "string" && ID.test(raw) ? raw : void 0;
|
|
100
|
+
}
|
|
101
|
+
function item(raw) {
|
|
102
|
+
var _a;
|
|
103
|
+
if (!raw || typeof raw !== "object") return null;
|
|
104
|
+
const r = raw;
|
|
105
|
+
const kinds = ["art", "text", "ink", "rows", "mark", "erase", "point"].filter((k) => r[k] !== void 0 && r[k] !== null && r[k] !== "");
|
|
106
|
+
if (kinds.length !== 1) return null;
|
|
107
|
+
switch (kinds[0]) {
|
|
108
|
+
case "art": {
|
|
109
|
+
const at = box(r.at);
|
|
110
|
+
const art = flat(r.art, LIMITS.art);
|
|
111
|
+
return at && art ? { kind: "art", id: id(r.id), art, at, tone: tone(r.tone) } : null;
|
|
112
|
+
}
|
|
113
|
+
case "text": {
|
|
114
|
+
const at = point(Array.isArray(r.at) ? r.at.slice(0, 2) : null);
|
|
115
|
+
const text = flat(r.text, LIMITS.text);
|
|
116
|
+
const size = typeof r.size === "string" && SIZES.includes(r.size) ? r.size : "m";
|
|
117
|
+
return at && text ? { kind: "text", id: id(r.id), text, at, size, tone: tone(r.tone) } : null;
|
|
118
|
+
}
|
|
119
|
+
case "ink": {
|
|
120
|
+
const ink = r.ink;
|
|
121
|
+
if (!INK_KINDS.includes(ink)) return null;
|
|
122
|
+
const base = { kind: "ink", id: id(r.id), ink, tone: tone(r.tone) };
|
|
123
|
+
if (POINT_INKS.includes(ink)) {
|
|
124
|
+
const pts = Array.isArray(r.pts) ? r.pts.slice(0, LIMITS.freePoints).map(point).filter((p) => !!p) : [];
|
|
125
|
+
if (ink === "corner" || ink === "arc") return pts.length === 3 ? { ...base, pts } : null;
|
|
126
|
+
if (pts.length < 2) return null;
|
|
127
|
+
return ink === "poly" && r.closed === true ? { ...base, pts, closed: true } : { ...base, pts };
|
|
128
|
+
}
|
|
129
|
+
if (ink === "dot") {
|
|
130
|
+
const at2 = point(r.at);
|
|
131
|
+
return at2 ? { ...base, at: at2 } : null;
|
|
132
|
+
}
|
|
133
|
+
if (ink === "line" || ink === "arrow") {
|
|
134
|
+
const at2 = point(r.at);
|
|
135
|
+
const to = point(r.to);
|
|
136
|
+
return at2 && to ? { ...base, at: at2, to } : null;
|
|
137
|
+
}
|
|
138
|
+
const at = box(r.at);
|
|
139
|
+
return at ? { ...base, at } : null;
|
|
140
|
+
}
|
|
141
|
+
case "rows": {
|
|
142
|
+
if (!Array.isArray(r.rows) || !Array.isArray(r.at) || r.at.length < 2) return null;
|
|
143
|
+
const rows = r.rows.slice(0, LIMITS.tableRows).filter((row) => Array.isArray(row) && row.length > 0).map((row) => row.slice(0, LIMITS.tableCols).map((cell) => flat(String(cell != null ? cell : ""), LIMITS.cell)));
|
|
144
|
+
const at = point(r.at.slice(0, 2));
|
|
145
|
+
if (!rows.length || !at) return null;
|
|
146
|
+
const width = ((_a = num(r.at[2])) != null ? _a : 40) * wide;
|
|
147
|
+
return { kind: "table", id: id(r.id), rows, at: [at[0], at[1], Math.min(Math.max(5, width), BOARD_W - at[0])], tone: tone(r.tone) };
|
|
148
|
+
}
|
|
149
|
+
case "mark": {
|
|
150
|
+
const mark = id(r.mark);
|
|
151
|
+
return mark ? { kind: "mark", mark } : null;
|
|
152
|
+
}
|
|
153
|
+
case "erase": {
|
|
154
|
+
const erase = id(r.erase);
|
|
155
|
+
return erase ? { kind: "erase", erase } : null;
|
|
156
|
+
}
|
|
157
|
+
case "point": {
|
|
158
|
+
const target = id(r.point);
|
|
159
|
+
const as = typeof r.as === "string" && POINT_MARKS.includes(r.as) ? r.as : "circle";
|
|
160
|
+
return target ? { kind: "point", point: target, as } : null;
|
|
161
|
+
}
|
|
162
|
+
default:
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function artInk(raw) {
|
|
167
|
+
if (!raw || typeof raw !== "object") return null;
|
|
168
|
+
const r = raw;
|
|
169
|
+
if (typeof r.icon === "string") return /^[a-z]{1,20}$/.test(r.icon) ? { icon: r.icon } : null;
|
|
170
|
+
if (!Array.isArray(r.contours)) return null;
|
|
171
|
+
const contours = r.contours.slice(0, LIMITS.artContours).filter((d) => typeof d === "string" && d.length <= LIMITS.contourChars && PATH_DATA.test(d));
|
|
172
|
+
const viewBox = typeof r.view_box === "string" && /^[\d.\s-]+$/.test(r.view_box) ? r.view_box : "0 0 100 100";
|
|
173
|
+
const transform = typeof r.transform === "string" && /^[a-z()\d.,\s-]*$/i.test(r.transform) ? r.transform : "";
|
|
174
|
+
return contours.length ? { view_box: viewBox, transform, contours } : null;
|
|
175
|
+
}
|
|
176
|
+
function validateScene(raw) {
|
|
177
|
+
if (!raw || typeof raw !== "object") throw new SceneError("A scene is an object.");
|
|
178
|
+
const r = raw;
|
|
179
|
+
if (!Array.isArray(r.steps) || r.steps.length === 0) throw new SceneError("A scene needs steps.");
|
|
180
|
+
if (r.steps.length > LIMITS.steps) throw new SceneError(`A scene has at most ${LIMITS.steps} steps.`);
|
|
181
|
+
wide = r.v === 3 ? 1 : BOARD_W / 100;
|
|
182
|
+
let total = 0;
|
|
183
|
+
const steps = [];
|
|
184
|
+
let onBoard = /* @__PURE__ */ new Set();
|
|
185
|
+
for (const rawStep of r.steps) {
|
|
186
|
+
if (!rawStep || typeof rawStep !== "object") continue;
|
|
187
|
+
const s = rawStep;
|
|
188
|
+
const clear = s.clear === true;
|
|
189
|
+
if (clear) onBoard = /* @__PURE__ */ new Set();
|
|
190
|
+
const draw = [];
|
|
191
|
+
const items = Array.isArray(s.draw) ? s.draw.slice(0, LIMITS.itemsPerStep) : [];
|
|
192
|
+
for (const rawItem of items) {
|
|
193
|
+
if (total >= LIMITS.items) break;
|
|
194
|
+
const it = item(rawItem);
|
|
195
|
+
if (!it) continue;
|
|
196
|
+
if (it.kind === "mark" && !onBoard.has(it.mark)) continue;
|
|
197
|
+
if (it.kind === "erase") {
|
|
198
|
+
if (!onBoard.has(it.erase)) continue;
|
|
199
|
+
onBoard.delete(it.erase);
|
|
200
|
+
}
|
|
201
|
+
if ("id" in it && it.id) onBoard.add(it.id);
|
|
202
|
+
draw.push(it);
|
|
203
|
+
total += 1;
|
|
204
|
+
}
|
|
205
|
+
const say = flat(s.say, LIMITS.say);
|
|
206
|
+
if (!say && !draw.length) continue;
|
|
207
|
+
steps.push({ say, clear, draw });
|
|
208
|
+
}
|
|
209
|
+
if (!steps.length) throw new SceneError("Nothing in this scene can be drawn.");
|
|
210
|
+
const art = {};
|
|
211
|
+
if (r.art && typeof r.art === "object") {
|
|
212
|
+
for (const [name, value] of Object.entries(r.art)) {
|
|
213
|
+
const ink = artInk(value);
|
|
214
|
+
if (ink) art[name.slice(0, LIMITS.art)] = ink;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return {
|
|
218
|
+
v: 3,
|
|
219
|
+
title: flat(r.title, LIMITS.title) || "Explanation",
|
|
220
|
+
sample: r.sample === true,
|
|
221
|
+
steps,
|
|
222
|
+
art
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
const BLOCKS_PATH = "/api/v1/chat/blocks";
|
|
226
|
+
const WHITEBOARD_PATH = "/api/v1/chat/whiteboard";
|
|
227
|
+
const BLOCKS_TTL_MS = 6e4;
|
|
228
|
+
const KEEP = 12;
|
|
229
|
+
const CLOSED = { status: "closed", scene: null, error: null, question: "" };
|
|
230
|
+
function readableAnswer(text) {
|
|
231
|
+
return text.replace(/```[a-z]*[ \t]*\n[\s\S]*?```/gi, " ").replace(/\s+/g, " ").trim();
|
|
232
|
+
}
|
|
233
|
+
class WhiteboardStore {
|
|
234
|
+
constructor(api, language = () => void 0, clock = () => Date.now()) {
|
|
235
|
+
this.api = api;
|
|
236
|
+
this.language = language;
|
|
237
|
+
this.clock = clock;
|
|
238
|
+
this.state = CLOSED;
|
|
239
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
240
|
+
this.kept = /* @__PURE__ */ new Map();
|
|
241
|
+
this.blocks = null;
|
|
242
|
+
this.blocksPending = null;
|
|
243
|
+
this.request = 0;
|
|
244
|
+
this.getState = () => this.state;
|
|
245
|
+
this.subscribe = (listener) => {
|
|
246
|
+
this.listeners.add(listener);
|
|
247
|
+
return () => this.listeners.delete(listener);
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
/** Whether the Blueprint has the whiteboard. False when it cannot be asked. */
|
|
251
|
+
async available() {
|
|
252
|
+
var _a;
|
|
253
|
+
if (this.blocks && this.clock() - this.blocks.at < BLOCKS_TTL_MS) return this.blocks.on;
|
|
254
|
+
(_a = this.blocksPending) != null ? _a : this.blocksPending = this.api().get(BLOCKS_PATH, 1e4).then((res) => {
|
|
255
|
+
var _a2;
|
|
256
|
+
return ((_a2 = res == null ? void 0 : res.blocks) == null ? void 0 : _a2.whiteboard) === true;
|
|
257
|
+
}, () => false).then((on) => {
|
|
258
|
+
this.blocks = { at: this.clock(), on };
|
|
259
|
+
this.blocksPending = null;
|
|
260
|
+
return on;
|
|
261
|
+
});
|
|
262
|
+
return this.blocksPending;
|
|
263
|
+
}
|
|
264
|
+
/** Explain *question* (and the answer on screen) on the board. */
|
|
265
|
+
async explain(question, answer) {
|
|
266
|
+
const q = question.trim().slice(0, 500);
|
|
267
|
+
if (!q) return false;
|
|
268
|
+
const a = answer ? readableAnswer(answer).slice(0, 4e3) : void 0;
|
|
269
|
+
const key = `${q}\0${a != null ? a : ""}`;
|
|
270
|
+
const known = this.kept.get(key);
|
|
271
|
+
if (known) {
|
|
272
|
+
this.set({ status: "playing", scene: known, error: null, question: q, answer: a });
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
275
|
+
const token = ++this.request;
|
|
276
|
+
this.set({ status: "loading", scene: null, error: null, question: q, answer: a });
|
|
277
|
+
try {
|
|
278
|
+
const raw = await this.api().post(
|
|
279
|
+
WHITEBOARD_PATH,
|
|
280
|
+
{ question: q, answer: a || void 0, language: this.language() },
|
|
281
|
+
6e4
|
|
282
|
+
);
|
|
283
|
+
if (token !== this.request) return false;
|
|
284
|
+
const scene = validateScene(raw);
|
|
285
|
+
this.keep(key, scene);
|
|
286
|
+
this.set({ status: "playing", scene, error: null, question: q, answer: a });
|
|
287
|
+
return true;
|
|
288
|
+
} catch (err) {
|
|
289
|
+
if (token !== this.request) return false;
|
|
290
|
+
this.set({
|
|
291
|
+
status: "error",
|
|
292
|
+
scene: null,
|
|
293
|
+
error: err instanceof SceneError ? "This board could not be drawn." : "The whiteboard is not available right now.",
|
|
294
|
+
question: q,
|
|
295
|
+
answer: a
|
|
296
|
+
});
|
|
297
|
+
return false;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
/** Ask again after an error. */
|
|
301
|
+
retry() {
|
|
302
|
+
if (this.state.status === "error") void this.explain(this.state.question, this.state.answer);
|
|
303
|
+
}
|
|
304
|
+
/** Play a board the host built. A board that cannot be drawn throws SceneError. */
|
|
305
|
+
play(scene) {
|
|
306
|
+
const valid = validateScene(scene);
|
|
307
|
+
this.request += 1;
|
|
308
|
+
this.set({ status: "playing", scene: valid, error: null, question: valid.title });
|
|
309
|
+
}
|
|
310
|
+
close() {
|
|
311
|
+
this.request += 1;
|
|
312
|
+
this.set(CLOSED);
|
|
313
|
+
}
|
|
314
|
+
keep(key, scene) {
|
|
315
|
+
this.kept.delete(key);
|
|
316
|
+
this.kept.set(key, scene);
|
|
317
|
+
while (this.kept.size > KEEP) {
|
|
318
|
+
const oldest = this.kept.keys().next().value;
|
|
319
|
+
if (oldest === void 0) break;
|
|
320
|
+
this.kept.delete(oldest);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
set(next) {
|
|
324
|
+
this.state = next;
|
|
325
|
+
for (const listener of [...this.listeners]) listener();
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
let store = null;
|
|
329
|
+
let owner = null;
|
|
330
|
+
function whiteboardStore() {
|
|
331
|
+
const configured = setup.requireConfigured();
|
|
332
|
+
if (!store || owner !== configured) {
|
|
333
|
+
store == null ? void 0 : store.close();
|
|
334
|
+
owner = configured;
|
|
335
|
+
store = new WhiteboardStore(
|
|
336
|
+
() => configured.api,
|
|
337
|
+
() => {
|
|
338
|
+
var _a;
|
|
339
|
+
const language = (_a = configured.profile) == null ? void 0 : _a.language;
|
|
340
|
+
return typeof language === "string" && language ? language : void 0;
|
|
341
|
+
}
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
return store;
|
|
345
|
+
}
|
|
346
|
+
const IDLE = new WhiteboardStore(() => {
|
|
347
|
+
throw new Error("Call GuideAI.configure() before using the whiteboard.");
|
|
348
|
+
});
|
|
349
|
+
function useWhiteboard() {
|
|
350
|
+
const board = setup.GuideAI.isConfigured() ? whiteboardStore() : IDLE;
|
|
351
|
+
const state = React.useSyncExternalStore(board.subscribe, board.getState, board.getState);
|
|
352
|
+
const [available, setAvailable] = React.useState(false);
|
|
353
|
+
React.useEffect(() => {
|
|
354
|
+
if (board === IDLE) return;
|
|
355
|
+
let live = true;
|
|
356
|
+
void board.available().then((on) => {
|
|
357
|
+
if (live) setAvailable(on);
|
|
358
|
+
});
|
|
359
|
+
return () => {
|
|
360
|
+
live = false;
|
|
361
|
+
};
|
|
362
|
+
}, [board]);
|
|
363
|
+
return {
|
|
364
|
+
available,
|
|
365
|
+
state,
|
|
366
|
+
explain: React.useCallback((question, answer) => board.explain(question, answer), [board]),
|
|
367
|
+
play: React.useCallback((scene) => board.play(scene), [board]),
|
|
368
|
+
retry: React.useCallback(() => board.retry(), [board]),
|
|
369
|
+
close: React.useCallback(() => board.close(), [board])
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
const AUDIO_TYPES = {
|
|
373
|
+
m4a: "audio/m4a",
|
|
374
|
+
mp4: "audio/mp4",
|
|
375
|
+
aac: "audio/aac",
|
|
376
|
+
caf: "audio/x-caf",
|
|
377
|
+
wav: "audio/wav",
|
|
378
|
+
mp3: "audio/mpeg",
|
|
379
|
+
webm: "audio/webm",
|
|
380
|
+
ogg: "audio/ogg",
|
|
381
|
+
"3gp": "audio/3gpp"
|
|
382
|
+
};
|
|
383
|
+
function audioType(uri) {
|
|
384
|
+
var _a, _b;
|
|
385
|
+
const extension = ((_a = uri.split("?")[0].split(".").pop()) != null ? _a : "").toLowerCase();
|
|
386
|
+
const type = (_b = AUDIO_TYPES[extension]) != null ? _b : "audio/m4a";
|
|
387
|
+
return { type, name: `voice.${AUDIO_TYPES[extension] ? extension : "m4a"}` };
|
|
388
|
+
}
|
|
389
|
+
function replyFields(result, streamed) {
|
|
390
|
+
var _a, _b;
|
|
391
|
+
const { text, images } = headlessImages(answerText(result, streamed));
|
|
392
|
+
const call = result.confirmationNeeded ? result.pendingToolCall : void 0;
|
|
393
|
+
return {
|
|
394
|
+
content: text,
|
|
395
|
+
images: images.length ? images : void 0,
|
|
396
|
+
suggestions: ((_a = result.quickReplies) == null ? void 0 : _a.length) ? result.quickReplies.slice(0, 4) : void 0,
|
|
397
|
+
pendingAction: call ? { label: ((_b = call.label) == null ? void 0 : _b.trim()) || setup.describeToolCall(call), tool: call.tool } : void 0
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
let nextId = 0;
|
|
401
|
+
const nextMessageId = () => `m${nextId += 1}`;
|
|
402
|
+
function questionBefore(entries, index) {
|
|
403
|
+
for (let i = index - 1; i >= 0; i -= 1) if (entries[i].role === "user") return entries[i].content;
|
|
404
|
+
return void 0;
|
|
405
|
+
}
|
|
406
|
+
function answerText(result, fallback) {
|
|
407
|
+
var _a, _b;
|
|
408
|
+
const answer = ((_a = result.answer) != null ? _a : "").trim();
|
|
409
|
+
if (answer) return answer;
|
|
410
|
+
const first = (_b = result.guideSteps) == null ? void 0 : _b[0];
|
|
411
|
+
const body = typeof (first == null ? void 0 : first.body) === "string" ? first.body.trim() : "";
|
|
412
|
+
if (body) return body;
|
|
413
|
+
const title = typeof (first == null ? void 0 : first.title) === "string" ? first.title.trim() : "";
|
|
414
|
+
if (title) return title;
|
|
415
|
+
return fallback || "Done.";
|
|
416
|
+
}
|
|
417
|
+
const FAILURE_MESSAGE = "I'm sorry, I can't complete your request right now. Would you like to talk to a human?";
|
|
418
|
+
function useGuideAI(options = {}) {
|
|
419
|
+
var _a, _b, _c, _d;
|
|
420
|
+
const runtime = setup.useGuideAIRuntime();
|
|
421
|
+
const configured = setup.requireConfigured();
|
|
422
|
+
const harness = (_a = options.harness) != null ? _a : configured.harness;
|
|
423
|
+
const trace = (_b = options.trace) != null ? _b : configured.debug;
|
|
424
|
+
const defaultFollowUps = (_c = options.followUps) != null ? _c : configured.followUps;
|
|
425
|
+
const mode = "assistant";
|
|
426
|
+
const board = useWhiteboard();
|
|
427
|
+
const [messages, setMessages] = React.useState([]);
|
|
428
|
+
const [input, setInput] = React.useState("");
|
|
429
|
+
const [busy, setBusy] = React.useState(false);
|
|
430
|
+
const [status, setStatus] = React.useState(null);
|
|
431
|
+
const [transcribing, setTranscribing] = React.useState(false);
|
|
432
|
+
const lastQuestion = React.useRef(null);
|
|
433
|
+
const pending = React.useRef(/* @__PURE__ */ new Map());
|
|
434
|
+
const mounted = React.useRef(true);
|
|
435
|
+
React.useEffect(() => () => {
|
|
436
|
+
mounted.current = false;
|
|
437
|
+
}, []);
|
|
438
|
+
React.useEffect(() => {
|
|
439
|
+
if (typeof harness.hasHistory !== "function" || typeof harness.getConversationHistory !== "function") return;
|
|
440
|
+
if (!harness.hasHistory()) return;
|
|
441
|
+
setMessages((previous) => {
|
|
442
|
+
if (previous.length > 0) return previous;
|
|
443
|
+
return harness.getConversationHistory().filter((entry) => typeof entry.content === "string" && entry.content.trim()).map((entry, index, all) => {
|
|
444
|
+
var _a2;
|
|
445
|
+
return {
|
|
446
|
+
id: (_a2 = entry.id) != null ? _a2 : `h${index}`,
|
|
447
|
+
role: entry.role,
|
|
448
|
+
content: entry.content,
|
|
449
|
+
createdAt: typeof entry.timestamp === "number" ? entry.timestamp : Date.now(),
|
|
450
|
+
question: entry.role === "assistant" ? questionBefore(all, index) : void 0
|
|
451
|
+
};
|
|
452
|
+
});
|
|
453
|
+
});
|
|
454
|
+
}, [harness]);
|
|
455
|
+
const initialized = React.useRef(null);
|
|
456
|
+
const ensureInitialized = React.useCallback(() => {
|
|
457
|
+
if (!initialized.current) initialized.current = harness.initialize();
|
|
458
|
+
return initialized.current;
|
|
459
|
+
}, [harness]);
|
|
460
|
+
const runTurn = React.useCallback(async (replyId, question, work) => {
|
|
461
|
+
var _a2, _b2;
|
|
462
|
+
setBusy(true);
|
|
463
|
+
setStatus(null);
|
|
464
|
+
let streamed = "";
|
|
465
|
+
const appendDelta = (delta) => {
|
|
466
|
+
streamed += delta;
|
|
467
|
+
if (!mounted.current) return;
|
|
468
|
+
setMessages((prev) => prev.map((message) => message.id === replyId ? { ...message, content: message.content + delta } : message));
|
|
469
|
+
};
|
|
470
|
+
try {
|
|
471
|
+
await ((_a2 = runtime.auth) == null ? void 0 : _a2.refresh());
|
|
472
|
+
const result = await work((next) => {
|
|
473
|
+
if (mounted.current) setStatus(next);
|
|
474
|
+
}, appendDelta);
|
|
475
|
+
if (trace) setup.traceAssistantAnswer((_b2 = result.answer) != null ? _b2 : "(empty)", result.mode);
|
|
476
|
+
if (result.confirmationNeeded && result.pendingToolCall) {
|
|
477
|
+
pending.current.set(replyId, { call: result.pendingToolCall, question });
|
|
478
|
+
}
|
|
479
|
+
if (!mounted.current) return;
|
|
480
|
+
setMessages((prev) => prev.map((message) => message.id === replyId ? { ...message, ...replyFields(result, streamed), streaming: false } : message));
|
|
481
|
+
} catch (error) {
|
|
482
|
+
console.log("[GuideAI] ✗ turn failed:", error);
|
|
483
|
+
if (error instanceof Error && error.stack) {
|
|
484
|
+
console.log("[GuideAI] stack:", error.stack);
|
|
485
|
+
}
|
|
486
|
+
if (!mounted.current) return;
|
|
487
|
+
setMessages((prev) => prev.map((message) => message.id === replyId ? {
|
|
488
|
+
...message,
|
|
489
|
+
content: FAILURE_MESSAGE,
|
|
490
|
+
streaming: false,
|
|
491
|
+
error: true
|
|
492
|
+
} : message));
|
|
493
|
+
} finally {
|
|
494
|
+
if (mounted.current) {
|
|
495
|
+
setBusy(false);
|
|
496
|
+
setStatus(null);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}, [runtime, trace]);
|
|
500
|
+
const send = React.useCallback(async (question) => {
|
|
501
|
+
const text = (question != null ? question : input).trim();
|
|
502
|
+
if (!text || busy) return;
|
|
503
|
+
lastQuestion.current = text;
|
|
504
|
+
if (question === void 0) setInput("");
|
|
505
|
+
if (trace) setup.traceUserMessage(text);
|
|
506
|
+
const replyId = nextMessageId();
|
|
507
|
+
const now = Date.now();
|
|
508
|
+
setMessages((prev) => [
|
|
509
|
+
...prev,
|
|
510
|
+
{ id: nextMessageId(), role: "user", content: text, createdAt: now },
|
|
511
|
+
{ id: replyId, role: "assistant", content: "", streaming: true, createdAt: now, question: text }
|
|
512
|
+
]);
|
|
513
|
+
await runTurn(replyId, text, async (onStatus, onDelta) => {
|
|
514
|
+
await ensureInitialized();
|
|
515
|
+
const screen = runtime.getActiveScreen();
|
|
516
|
+
const { profile, visitorId } = setup.requireConfigured();
|
|
517
|
+
return harness.processMessage(
|
|
518
|
+
text,
|
|
519
|
+
screen == null ? void 0 : screen.path,
|
|
520
|
+
runtime.captureSnapshot(),
|
|
521
|
+
mode,
|
|
522
|
+
onStatus,
|
|
523
|
+
onDelta,
|
|
524
|
+
void 0,
|
|
525
|
+
void 0,
|
|
526
|
+
// Who is asking, as the app knows them; the agent pitches to it.
|
|
527
|
+
profile ? { profile } : void 0,
|
|
528
|
+
visitorId != null ? visitorId : void 0
|
|
529
|
+
);
|
|
530
|
+
});
|
|
531
|
+
}, [busy, input, harness, mode, runtime, ensureInitialized, trace, runTurn]);
|
|
532
|
+
const retry = React.useCallback(async () => {
|
|
533
|
+
if (!lastQuestion.current || busy) return;
|
|
534
|
+
setMessages((prev) => prev.slice(0, -2));
|
|
535
|
+
await send(lastQuestion.current);
|
|
536
|
+
}, [busy, send]);
|
|
537
|
+
const clear = React.useCallback(() => {
|
|
538
|
+
setMessages([]);
|
|
539
|
+
lastQuestion.current = null;
|
|
540
|
+
pending.current.clear();
|
|
541
|
+
}, []);
|
|
542
|
+
const settleAction = React.useCallback((messageId) => {
|
|
543
|
+
const action = pending.current.get(messageId);
|
|
544
|
+
pending.current.delete(messageId);
|
|
545
|
+
setMessages((prev) => prev.map((message) => message.id === messageId ? { ...message, pendingAction: void 0 } : message));
|
|
546
|
+
return action;
|
|
547
|
+
}, []);
|
|
548
|
+
const confirm = React.useCallback(async (messageId) => {
|
|
549
|
+
if (busy) return;
|
|
550
|
+
const action = settleAction(messageId);
|
|
551
|
+
if (!action) return;
|
|
552
|
+
const replyId = nextMessageId();
|
|
553
|
+
setMessages((prev) => [...prev, {
|
|
554
|
+
id: replyId,
|
|
555
|
+
role: "assistant",
|
|
556
|
+
content: "",
|
|
557
|
+
streaming: true,
|
|
558
|
+
createdAt: Date.now(),
|
|
559
|
+
question: action.question
|
|
560
|
+
}]);
|
|
561
|
+
await runTurn(replyId, action.question, async (onStatus, onDelta) => {
|
|
562
|
+
await ensureInitialized();
|
|
563
|
+
return harness.executeToolCall(action.call, action.question, onStatus, onDelta);
|
|
564
|
+
});
|
|
565
|
+
}, [busy, settleAction, runTurn, ensureInitialized, harness]);
|
|
566
|
+
const cancel = React.useCallback((messageId) => {
|
|
567
|
+
if (!settleAction(messageId)) return;
|
|
568
|
+
setMessages((prev) => [...prev, {
|
|
569
|
+
id: nextMessageId(),
|
|
570
|
+
role: "assistant",
|
|
571
|
+
content: "Okay, I haven't changed anything.",
|
|
572
|
+
createdAt: Date.now()
|
|
573
|
+
}]);
|
|
574
|
+
}, [settleAction]);
|
|
575
|
+
const sendVoice = React.useCallback(async (recording) => {
|
|
576
|
+
var _a2;
|
|
577
|
+
if (busy || transcribing) return { text: null, error: "Wait for the current reply to finish." };
|
|
578
|
+
const guessed = audioType(recording.uri);
|
|
579
|
+
setTranscribing(true);
|
|
580
|
+
try {
|
|
581
|
+
const result = await setup.requireConfigured().api.postFile(
|
|
582
|
+
setup.EP.batchVoiceTranscribe(),
|
|
583
|
+
"audio",
|
|
584
|
+
{ uri: recording.uri, name: guessed.name, type: recording.mimeType || guessed.type },
|
|
585
|
+
{ language_code: recording.languageCode || "en-US" },
|
|
586
|
+
45e3
|
|
587
|
+
);
|
|
588
|
+
const heard = ((_a2 = result == null ? void 0 : result.text) == null ? void 0 : _a2.trim()) || "";
|
|
589
|
+
if (!heard) return { text: null, error: "I didn't catch that. Please try again." };
|
|
590
|
+
if (mounted.current) setTranscribing(false);
|
|
591
|
+
await send(heard);
|
|
592
|
+
return { text: heard };
|
|
593
|
+
} catch (error) {
|
|
594
|
+
console.log("[GuideAI] ✗ voice transcription failed:", error);
|
|
595
|
+
const message = error instanceof setup.UploadError && error.status === 422 ? error.message : "I couldn't process that recording. Please try again or type your message.";
|
|
596
|
+
return { text: null, error: message };
|
|
597
|
+
} finally {
|
|
598
|
+
if (mounted.current) setTranscribing(false);
|
|
599
|
+
}
|
|
600
|
+
}, [busy, transcribing, send]);
|
|
601
|
+
const shown = React.useMemo(() => messages.filter((message) => message.content || !message.streaming).map((message) => {
|
|
602
|
+
var _a2;
|
|
603
|
+
if (message.role !== "assistant" || message.streaming || message.error || !message.content) return message;
|
|
604
|
+
const suggestions = (_a2 = message.suggestions) != null ? _a2 : (defaultFollowUps == null ? void 0 : defaultFollowUps.length) ? defaultFollowUps : void 0;
|
|
605
|
+
return { ...message, suggestions, whiteboard: board.available || void 0 };
|
|
606
|
+
}), [messages, defaultFollowUps, board.available]);
|
|
607
|
+
const last = shown[shown.length - 1];
|
|
608
|
+
const followUps = !busy && (last == null ? void 0 : last.role) === "assistant" && !last.pendingAction ? (_d = last.suggestions) != null ? _d : [] : [];
|
|
609
|
+
const typing = busy && messages.some((message) => message.streaming && !message.content);
|
|
610
|
+
const explainOnWhiteboard = React.useCallback(async (messageId) => {
|
|
611
|
+
const answer = messages.find((message) => message.id === messageId);
|
|
612
|
+
if (!answer || answer.role !== "assistant" || !answer.content) return false;
|
|
613
|
+
return board.explain(answer.question || answer.content.slice(0, 200), answer.content);
|
|
614
|
+
}, [messages, board]);
|
|
615
|
+
return {
|
|
616
|
+
messages: shown,
|
|
617
|
+
typing,
|
|
618
|
+
followUps,
|
|
619
|
+
whiteboardAvailable: board.available,
|
|
620
|
+
explainOnWhiteboard,
|
|
621
|
+
input,
|
|
622
|
+
setInput,
|
|
623
|
+
send,
|
|
624
|
+
busy,
|
|
625
|
+
canSend: Boolean(input.trim()) && !busy,
|
|
626
|
+
status,
|
|
627
|
+
retry,
|
|
628
|
+
clear,
|
|
629
|
+
confirm,
|
|
630
|
+
cancel,
|
|
631
|
+
sendVoice,
|
|
632
|
+
transcribing
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
function useOnboarding() {
|
|
636
|
+
const runtime = setup.useGuideAIRuntime();
|
|
637
|
+
const [checklist, setChecklist] = React.useState(null);
|
|
638
|
+
const [loading, setLoading] = React.useState(true);
|
|
639
|
+
const refresh = React.useCallback(async () => {
|
|
640
|
+
var _a;
|
|
641
|
+
const { api, visitorId } = setup.requireConfigured();
|
|
642
|
+
if (!visitorId) {
|
|
643
|
+
setChecklist(null);
|
|
644
|
+
setLoading(false);
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
try {
|
|
648
|
+
const current = await api.get(setup.EP.onboardingCurrent(visitorId));
|
|
649
|
+
const screen = (_a = runtime.getActiveScreen()) == null ? void 0 : _a.path;
|
|
650
|
+
const updated = current && !current.completed && !current.dismissed && screen ? await api.post(setup.EP.onboardingObserve(), { user_id: visitorId, screen }) : current;
|
|
651
|
+
setChecklist(updated);
|
|
652
|
+
} catch {
|
|
653
|
+
} finally {
|
|
654
|
+
setLoading(false);
|
|
655
|
+
}
|
|
656
|
+
}, [runtime]);
|
|
657
|
+
const dismiss = React.useCallback(async () => {
|
|
658
|
+
const { api, visitorId } = setup.requireConfigured();
|
|
659
|
+
if (!visitorId) return;
|
|
660
|
+
const updated = await api.post(setup.EP.onboardingDismiss(), { user_id: visitorId });
|
|
661
|
+
setChecklist(updated);
|
|
662
|
+
}, []);
|
|
663
|
+
React.useEffect(() => {
|
|
664
|
+
void refresh();
|
|
665
|
+
const timer = setInterval(() => void refresh(), 5e3);
|
|
666
|
+
return () => clearInterval(timer);
|
|
667
|
+
}, [refresh]);
|
|
668
|
+
return { checklist, loading, refresh, dismiss };
|
|
669
|
+
}
|
|
670
|
+
exports.BOARD_H = BOARD_H;
|
|
671
|
+
exports.BOARD_W = BOARD_W;
|
|
672
|
+
exports.LIMITS = LIMITS;
|
|
673
|
+
exports.useGuideAI = useGuideAI;
|
|
674
|
+
exports.useOnboarding = useOnboarding;
|
|
675
|
+
exports.useWhiteboard = useWhiteboard;
|
|
676
|
+
//# sourceMappingURL=useOnboarding-CFxofFst.cjs.map
|