@guideai/react-native 0.2.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/dist/index.cjs +114 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +194 -4
- package/dist/index.mjs +113 -4
- package/dist/index.mjs.map +1 -1
- package/dist/{useOnboarding-DWkCW5v0.cjs → setup-Bl0ZLz8B.cjs} +16 -288
- package/dist/setup-Bl0ZLz8B.cjs.map +1 -0
- package/dist/{useOnboarding-DAEGoYSs.js → setup-CkXajERE.js} +17 -289
- 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 +17 -3
- package/dist/useOnboarding-DAEGoYSs.js.map +0 -1
- package/dist/useOnboarding-DWkCW5v0.cjs.map +0 -1
|
@@ -1,28 +1,4 @@
|
|
|
1
|
-
import { useContext, createContext
|
|
2
|
-
const DRAWING_URL = /^https:\/\/res\.cloudinary\.com\/[\w-]+\/image\/upload\//;
|
|
3
|
-
const IMAGE_BLOCK = /```image[ \t]*\n([\s\S]*?)```/g;
|
|
4
|
-
function headlessImages(text) {
|
|
5
|
-
const images = [];
|
|
6
|
-
const out = (text || "").replace(IMAGE_BLOCK, (_block, raw) => {
|
|
7
|
-
let data;
|
|
8
|
-
try {
|
|
9
|
-
data = JSON.parse(raw.trim());
|
|
10
|
-
} catch {
|
|
11
|
-
return "";
|
|
12
|
-
}
|
|
13
|
-
if (typeof data.url !== "string" || !DRAWING_URL.test(data.url)) return "";
|
|
14
|
-
const image = {
|
|
15
|
-
url: data.url,
|
|
16
|
-
svg: typeof data.svg === "string" && DRAWING_URL.test(data.svg) ? data.svg : null,
|
|
17
|
-
alt: typeof data.alt === "string" ? data.alt.slice(0, 200) : "",
|
|
18
|
-
width: Math.round(Number(data.width)) || 1600,
|
|
19
|
-
height: Math.round(Number(data.height)) || 900
|
|
20
|
-
};
|
|
21
|
-
images.push(image);
|
|
22
|
-
return `![${image.alt.replace(/[[\]]/g, "")}](${image.url})`;
|
|
23
|
-
});
|
|
24
|
-
return { text: out, images };
|
|
25
|
-
}
|
|
1
|
+
import { useContext, createContext } from "react";
|
|
26
2
|
const NAME_PREFIXES = [
|
|
27
3
|
/^api[\s_-]*v?\d*[\s_-]*/i,
|
|
28
4
|
/^(get|fetch|list|read|retrieve|show|query|search|find|lookup|look up)[\s_-]+/i,
|
|
@@ -3289,7 +3265,8 @@ function configure(config) {
|
|
|
3289
3265
|
api: baseApi,
|
|
3290
3266
|
debug,
|
|
3291
3267
|
visitorId: (_e = config.visitorId) != null ? _e : null,
|
|
3292
|
-
profile: (_f = config.userProfile) != null ? _f : null
|
|
3268
|
+
profile: (_f = config.userProfile) != null ? _f : null,
|
|
3269
|
+
followUps: cleanFollowUps(config.followUps)
|
|
3293
3270
|
};
|
|
3294
3271
|
}
|
|
3295
3272
|
function identify(userId) {
|
|
@@ -3304,6 +3281,14 @@ function setUserProfile(profile) {
|
|
|
3304
3281
|
if (!configured) throw new Error("Call GuideAI.configure() before setUserProfile().");
|
|
3305
3282
|
configured.profile = profile;
|
|
3306
3283
|
}
|
|
3284
|
+
function setFollowUps(followUps) {
|
|
3285
|
+
if (!configured) throw new Error("Call GuideAI.configure() before setFollowUps().");
|
|
3286
|
+
configured.followUps = cleanFollowUps(followUps != null ? followUps : void 0);
|
|
3287
|
+
}
|
|
3288
|
+
function cleanFollowUps(list) {
|
|
3289
|
+
const clean = (list != null ? list : []).map((text) => text.trim()).filter(Boolean).slice(0, 4);
|
|
3290
|
+
return clean.length ? clean : void 0;
|
|
3291
|
+
}
|
|
3307
3292
|
function requireConfigured() {
|
|
3308
3293
|
if (!configured) {
|
|
3309
3294
|
throw new Error(
|
|
@@ -3318,275 +3303,18 @@ function isConfigured() {
|
|
|
3318
3303
|
function reset() {
|
|
3319
3304
|
configured = null;
|
|
3320
3305
|
}
|
|
3321
|
-
const GuideAI = { configure, identify, setUserProfile, setToolContext, isConfigured, reset };
|
|
3322
|
-
const AUDIO_TYPES = {
|
|
3323
|
-
m4a: "audio/m4a",
|
|
3324
|
-
mp4: "audio/mp4",
|
|
3325
|
-
aac: "audio/aac",
|
|
3326
|
-
caf: "audio/x-caf",
|
|
3327
|
-
wav: "audio/wav",
|
|
3328
|
-
mp3: "audio/mpeg",
|
|
3329
|
-
webm: "audio/webm",
|
|
3330
|
-
ogg: "audio/ogg",
|
|
3331
|
-
"3gp": "audio/3gpp"
|
|
3332
|
-
};
|
|
3333
|
-
function audioType(uri) {
|
|
3334
|
-
var _a, _b;
|
|
3335
|
-
const extension = ((_a = uri.split("?")[0].split(".").pop()) != null ? _a : "").toLowerCase();
|
|
3336
|
-
const type = (_b = AUDIO_TYPES[extension]) != null ? _b : "audio/m4a";
|
|
3337
|
-
return { type, name: `voice.${AUDIO_TYPES[extension] ? extension : "m4a"}` };
|
|
3338
|
-
}
|
|
3339
|
-
function replyFields(result, streamed) {
|
|
3340
|
-
var _a, _b;
|
|
3341
|
-
const { text, images } = headlessImages(answerText(result, streamed));
|
|
3342
|
-
const call = result.confirmationNeeded ? result.pendingToolCall : void 0;
|
|
3343
|
-
return {
|
|
3344
|
-
content: text,
|
|
3345
|
-
images: images.length ? images : void 0,
|
|
3346
|
-
suggestions: ((_a = result.quickReplies) == null ? void 0 : _a.length) ? result.quickReplies.slice(0, 4) : void 0,
|
|
3347
|
-
pendingAction: call ? { label: ((_b = call.label) == null ? void 0 : _b.trim()) || describeToolCall(call), tool: call.tool } : void 0
|
|
3348
|
-
};
|
|
3349
|
-
}
|
|
3350
|
-
let nextId = 0;
|
|
3351
|
-
const nextMessageId = () => `m${nextId += 1}`;
|
|
3352
|
-
function answerText(result, fallback) {
|
|
3353
|
-
var _a, _b;
|
|
3354
|
-
const answer = ((_a = result.answer) != null ? _a : "").trim();
|
|
3355
|
-
if (answer) return answer;
|
|
3356
|
-
const first = (_b = result.guideSteps) == null ? void 0 : _b[0];
|
|
3357
|
-
const body = typeof (first == null ? void 0 : first.body) === "string" ? first.body.trim() : "";
|
|
3358
|
-
if (body) return body;
|
|
3359
|
-
const title = typeof (first == null ? void 0 : first.title) === "string" ? first.title.trim() : "";
|
|
3360
|
-
if (title) return title;
|
|
3361
|
-
return fallback || "Done.";
|
|
3362
|
-
}
|
|
3363
|
-
const FAILURE_MESSAGE = "I'm sorry, I can't complete your request right now. Would you like to talk to a human?";
|
|
3364
|
-
function useGuideAI(options = {}) {
|
|
3365
|
-
var _a, _b;
|
|
3366
|
-
const runtime = useGuideAIRuntime();
|
|
3367
|
-
const configured2 = requireConfigured();
|
|
3368
|
-
const harness = (_a = options.harness) != null ? _a : configured2.harness;
|
|
3369
|
-
const trace = (_b = options.trace) != null ? _b : configured2.debug;
|
|
3370
|
-
const mode = "assistant";
|
|
3371
|
-
const [messages, setMessages] = useState([]);
|
|
3372
|
-
const [input, setInput] = useState("");
|
|
3373
|
-
const [busy, setBusy] = useState(false);
|
|
3374
|
-
const [status, setStatus] = useState(null);
|
|
3375
|
-
const [transcribing, setTranscribing] = useState(false);
|
|
3376
|
-
const lastQuestion = useRef(null);
|
|
3377
|
-
const pending = useRef(/* @__PURE__ */ new Map());
|
|
3378
|
-
const mounted = useRef(true);
|
|
3379
|
-
useEffect(() => () => {
|
|
3380
|
-
mounted.current = false;
|
|
3381
|
-
}, []);
|
|
3382
|
-
useEffect(() => {
|
|
3383
|
-
if (typeof harness.hasHistory !== "function" || typeof harness.getConversationHistory !== "function") return;
|
|
3384
|
-
if (!harness.hasHistory()) return;
|
|
3385
|
-
setMessages((previous) => {
|
|
3386
|
-
if (previous.length > 0) return previous;
|
|
3387
|
-
return harness.getConversationHistory().filter((entry) => typeof entry.content === "string" && entry.content.trim()).map((entry, index) => {
|
|
3388
|
-
var _a2;
|
|
3389
|
-
return {
|
|
3390
|
-
id: (_a2 = entry.id) != null ? _a2 : `h${index}`,
|
|
3391
|
-
role: entry.role,
|
|
3392
|
-
content: entry.content
|
|
3393
|
-
};
|
|
3394
|
-
});
|
|
3395
|
-
});
|
|
3396
|
-
}, [harness]);
|
|
3397
|
-
const initialized = useRef(null);
|
|
3398
|
-
const ensureInitialized = useCallback(() => {
|
|
3399
|
-
if (!initialized.current) initialized.current = harness.initialize();
|
|
3400
|
-
return initialized.current;
|
|
3401
|
-
}, [harness]);
|
|
3402
|
-
const runTurn = useCallback(async (replyId, question, work) => {
|
|
3403
|
-
var _a2, _b2;
|
|
3404
|
-
setBusy(true);
|
|
3405
|
-
setStatus(null);
|
|
3406
|
-
let streamed = "";
|
|
3407
|
-
const appendDelta = (delta) => {
|
|
3408
|
-
streamed += delta;
|
|
3409
|
-
if (!mounted.current) return;
|
|
3410
|
-
setMessages((prev) => prev.map((message) => message.id === replyId ? { ...message, content: message.content + delta } : message));
|
|
3411
|
-
};
|
|
3412
|
-
try {
|
|
3413
|
-
await ((_a2 = runtime.auth) == null ? void 0 : _a2.refresh());
|
|
3414
|
-
const result = await work((next2) => {
|
|
3415
|
-
if (mounted.current) setStatus(next2);
|
|
3416
|
-
}, appendDelta);
|
|
3417
|
-
if (trace) traceAssistantAnswer((_b2 = result.answer) != null ? _b2 : "(empty)", result.mode);
|
|
3418
|
-
if (result.confirmationNeeded && result.pendingToolCall) {
|
|
3419
|
-
pending.current.set(replyId, { call: result.pendingToolCall, question });
|
|
3420
|
-
}
|
|
3421
|
-
if (!mounted.current) return;
|
|
3422
|
-
setMessages((prev) => prev.map((message) => message.id === replyId ? { ...message, ...replyFields(result, streamed), streaming: false } : message));
|
|
3423
|
-
} catch (error) {
|
|
3424
|
-
console.log("[GuideAI] ✗ turn failed:", error);
|
|
3425
|
-
if (error instanceof Error && error.stack) {
|
|
3426
|
-
console.log("[GuideAI] stack:", error.stack);
|
|
3427
|
-
}
|
|
3428
|
-
if (!mounted.current) return;
|
|
3429
|
-
setMessages((prev) => prev.map((message) => message.id === replyId ? {
|
|
3430
|
-
...message,
|
|
3431
|
-
content: FAILURE_MESSAGE,
|
|
3432
|
-
streaming: false,
|
|
3433
|
-
error: true
|
|
3434
|
-
} : message));
|
|
3435
|
-
} finally {
|
|
3436
|
-
if (mounted.current) {
|
|
3437
|
-
setBusy(false);
|
|
3438
|
-
setStatus(null);
|
|
3439
|
-
}
|
|
3440
|
-
}
|
|
3441
|
-
}, [runtime, trace]);
|
|
3442
|
-
const send = useCallback(async (question) => {
|
|
3443
|
-
const text = (question != null ? question : input).trim();
|
|
3444
|
-
if (!text || busy) return;
|
|
3445
|
-
lastQuestion.current = text;
|
|
3446
|
-
if (question === void 0) setInput("");
|
|
3447
|
-
if (trace) traceUserMessage(text);
|
|
3448
|
-
const replyId = nextMessageId();
|
|
3449
|
-
setMessages((prev) => [
|
|
3450
|
-
...prev,
|
|
3451
|
-
{ id: nextMessageId(), role: "user", content: text },
|
|
3452
|
-
{ id: replyId, role: "assistant", content: "", streaming: true }
|
|
3453
|
-
]);
|
|
3454
|
-
await runTurn(replyId, text, async (onStatus, onDelta) => {
|
|
3455
|
-
await ensureInitialized();
|
|
3456
|
-
const screen = runtime.getActiveScreen();
|
|
3457
|
-
const { profile, visitorId } = requireConfigured();
|
|
3458
|
-
return harness.processMessage(
|
|
3459
|
-
text,
|
|
3460
|
-
screen == null ? void 0 : screen.path,
|
|
3461
|
-
runtime.captureSnapshot(),
|
|
3462
|
-
mode,
|
|
3463
|
-
onStatus,
|
|
3464
|
-
onDelta,
|
|
3465
|
-
void 0,
|
|
3466
|
-
void 0,
|
|
3467
|
-
// Who is asking, as the app knows them; the agent pitches to it.
|
|
3468
|
-
profile ? { profile } : void 0,
|
|
3469
|
-
visitorId != null ? visitorId : void 0
|
|
3470
|
-
);
|
|
3471
|
-
});
|
|
3472
|
-
}, [busy, input, harness, mode, runtime, ensureInitialized, trace, runTurn]);
|
|
3473
|
-
const retry = useCallback(async () => {
|
|
3474
|
-
if (!lastQuestion.current || busy) return;
|
|
3475
|
-
setMessages((prev) => prev.slice(0, -2));
|
|
3476
|
-
await send(lastQuestion.current);
|
|
3477
|
-
}, [busy, send]);
|
|
3478
|
-
const clear = useCallback(() => {
|
|
3479
|
-
setMessages([]);
|
|
3480
|
-
lastQuestion.current = null;
|
|
3481
|
-
pending.current.clear();
|
|
3482
|
-
}, []);
|
|
3483
|
-
const settleAction = useCallback((messageId) => {
|
|
3484
|
-
const action = pending.current.get(messageId);
|
|
3485
|
-
pending.current.delete(messageId);
|
|
3486
|
-
setMessages((prev) => prev.map((message) => message.id === messageId ? { ...message, pendingAction: void 0 } : message));
|
|
3487
|
-
return action;
|
|
3488
|
-
}, []);
|
|
3489
|
-
const confirm = useCallback(async (messageId) => {
|
|
3490
|
-
if (busy) return;
|
|
3491
|
-
const action = settleAction(messageId);
|
|
3492
|
-
if (!action) return;
|
|
3493
|
-
const replyId = nextMessageId();
|
|
3494
|
-
setMessages((prev) => [...prev, { id: replyId, role: "assistant", content: "", streaming: true }]);
|
|
3495
|
-
await runTurn(replyId, action.question, async (onStatus, onDelta) => {
|
|
3496
|
-
await ensureInitialized();
|
|
3497
|
-
return harness.executeToolCall(action.call, action.question, onStatus, onDelta);
|
|
3498
|
-
});
|
|
3499
|
-
}, [busy, settleAction, runTurn, ensureInitialized, harness]);
|
|
3500
|
-
const cancel = useCallback((messageId) => {
|
|
3501
|
-
if (!settleAction(messageId)) return;
|
|
3502
|
-
setMessages((prev) => [...prev, { id: nextMessageId(), role: "assistant", content: "Okay, I haven't changed anything." }]);
|
|
3503
|
-
}, [settleAction]);
|
|
3504
|
-
const sendVoice = useCallback(async (recording) => {
|
|
3505
|
-
var _a2;
|
|
3506
|
-
if (busy || transcribing) return { text: null, error: "Wait for the current reply to finish." };
|
|
3507
|
-
const guessed = audioType(recording.uri);
|
|
3508
|
-
setTranscribing(true);
|
|
3509
|
-
try {
|
|
3510
|
-
const result = await requireConfigured().api.postFile(
|
|
3511
|
-
EP.batchVoiceTranscribe(),
|
|
3512
|
-
"audio",
|
|
3513
|
-
{ uri: recording.uri, name: guessed.name, type: recording.mimeType || guessed.type },
|
|
3514
|
-
{ language_code: recording.languageCode || "en-US" },
|
|
3515
|
-
45e3
|
|
3516
|
-
);
|
|
3517
|
-
const heard = ((_a2 = result == null ? void 0 : result.text) == null ? void 0 : _a2.trim()) || "";
|
|
3518
|
-
if (!heard) return { text: null, error: "I didn't catch that. Please try again." };
|
|
3519
|
-
if (mounted.current) setTranscribing(false);
|
|
3520
|
-
await send(heard);
|
|
3521
|
-
return { text: heard };
|
|
3522
|
-
} catch (error) {
|
|
3523
|
-
console.log("[GuideAI] ✗ voice transcription failed:", error);
|
|
3524
|
-
const message = error instanceof UploadError && error.status === 422 ? error.message : "I couldn't process that recording. Please try again or type your message.";
|
|
3525
|
-
return { text: null, error: message };
|
|
3526
|
-
} finally {
|
|
3527
|
-
if (mounted.current) setTranscribing(false);
|
|
3528
|
-
}
|
|
3529
|
-
}, [busy, transcribing, send]);
|
|
3530
|
-
return {
|
|
3531
|
-
messages,
|
|
3532
|
-
input,
|
|
3533
|
-
setInput,
|
|
3534
|
-
send,
|
|
3535
|
-
busy,
|
|
3536
|
-
canSend: Boolean(input.trim()) && !busy,
|
|
3537
|
-
status,
|
|
3538
|
-
retry,
|
|
3539
|
-
clear,
|
|
3540
|
-
confirm,
|
|
3541
|
-
cancel,
|
|
3542
|
-
sendVoice,
|
|
3543
|
-
transcribing
|
|
3544
|
-
};
|
|
3545
|
-
}
|
|
3546
|
-
function useOnboarding() {
|
|
3547
|
-
const runtime = useGuideAIRuntime();
|
|
3548
|
-
const [checklist, setChecklist] = useState(null);
|
|
3549
|
-
const [loading, setLoading] = useState(true);
|
|
3550
|
-
const refresh = useCallback(async () => {
|
|
3551
|
-
var _a;
|
|
3552
|
-
const { api, visitorId } = requireConfigured();
|
|
3553
|
-
if (!visitorId) {
|
|
3554
|
-
setChecklist(null);
|
|
3555
|
-
setLoading(false);
|
|
3556
|
-
return;
|
|
3557
|
-
}
|
|
3558
|
-
try {
|
|
3559
|
-
const current = await api.get(EP.onboardingCurrent(visitorId));
|
|
3560
|
-
const screen = (_a = runtime.getActiveScreen()) == null ? void 0 : _a.path;
|
|
3561
|
-
const updated = current && !current.completed && !current.dismissed && screen ? await api.post(EP.onboardingObserve(), { user_id: visitorId, screen }) : current;
|
|
3562
|
-
setChecklist(updated);
|
|
3563
|
-
} catch {
|
|
3564
|
-
} finally {
|
|
3565
|
-
setLoading(false);
|
|
3566
|
-
}
|
|
3567
|
-
}, [runtime]);
|
|
3568
|
-
const dismiss = useCallback(async () => {
|
|
3569
|
-
const { api, visitorId } = requireConfigured();
|
|
3570
|
-
if (!visitorId) return;
|
|
3571
|
-
const updated = await api.post(EP.onboardingDismiss(), { user_id: visitorId });
|
|
3572
|
-
setChecklist(updated);
|
|
3573
|
-
}, []);
|
|
3574
|
-
useEffect(() => {
|
|
3575
|
-
void refresh();
|
|
3576
|
-
const timer = setInterval(() => void refresh(), 5e3);
|
|
3577
|
-
return () => clearInterval(timer);
|
|
3578
|
-
}, [refresh]);
|
|
3579
|
-
return { checklist, loading, refresh, dismiss };
|
|
3580
|
-
}
|
|
3306
|
+
const GuideAI = { configure, identify, setUserProfile, setToolContext, setFollowUps, isConfigured, reset };
|
|
3581
3307
|
export {
|
|
3582
3308
|
EP as E,
|
|
3583
3309
|
GuideAI as G,
|
|
3584
3310
|
RegistryContext as R,
|
|
3585
3311
|
ScreenRegistry as S,
|
|
3312
|
+
UploadError as U,
|
|
3586
3313
|
useScreenRegistry as a,
|
|
3587
|
-
|
|
3588
|
-
|
|
3314
|
+
traceUserMessage as b,
|
|
3315
|
+
describeToolCall as d,
|
|
3589
3316
|
requireConfigured as r,
|
|
3317
|
+
traceAssistantAnswer as t,
|
|
3590
3318
|
useGuideAIRuntime as u
|
|
3591
3319
|
};
|
|
3592
|
-
//# sourceMappingURL=
|
|
3320
|
+
//# sourceMappingURL=setup-CkXajERE.js.map
|