@chatterfly/widget 0.2.0 → 0.4.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 +12 -2
- package/dist/embed.css +1 -1
- package/dist/embed.css.map +1 -1
- package/dist/embed.global.js +12 -12
- package/dist/embed.global.js.map +1 -1
- package/dist/index.css +115 -0
- package/dist/index.css.map +1 -1
- package/dist/index.d.mts +80 -4
- package/dist/index.d.ts +80 -4
- package/dist/index.js +459 -99
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +459 -99
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -15,11 +15,68 @@ var WidgetError = class {
|
|
|
15
15
|
return this.message;
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
|
-
|
|
18
|
+
|
|
19
|
+
// src/strings.ts
|
|
20
|
+
var defaultStrings = {
|
|
21
|
+
statusInitialising: "Initialising\u2026",
|
|
22
|
+
statusConnecting: "Connecting\u2026",
|
|
23
|
+
statusRunning: "Running\u2026",
|
|
24
|
+
statusCompletedTitle: "Completed",
|
|
25
|
+
statusCompletedBody: "This workflow run has finished.",
|
|
26
|
+
statusErrorTitle: "Something went wrong",
|
|
27
|
+
statusOfflineTitle: "Connection lost",
|
|
28
|
+
statusOfflineBody: "Unable to reach the server. Please check your connection and refresh.",
|
|
29
|
+
statusExpiredTitle: "Session expired",
|
|
30
|
+
statusExpiredBody: "This conversation's session has expired. Start a new one to continue.",
|
|
31
|
+
statusListening: "Listening\u2026",
|
|
32
|
+
resumingConversation: "Resuming conversation\u2026",
|
|
33
|
+
reconnecting: "Reconnecting\u2026",
|
|
34
|
+
conversationCompleted: "Conversation completed.",
|
|
35
|
+
somethingWentWrong: "Something went wrong",
|
|
36
|
+
chatInputPlaceholder: "Type a message\u2026",
|
|
37
|
+
sendButton: "Send",
|
|
38
|
+
handBackButton: "Hand Back",
|
|
39
|
+
converseDoneButton: "I'm done",
|
|
40
|
+
startConversation: "Start conversation",
|
|
41
|
+
clickToStartDescription: "Click below to begin.",
|
|
42
|
+
resumeTitle: "Continue where you left off?",
|
|
43
|
+
resumeContinue: "Continue",
|
|
44
|
+
resumeRestart: "Start fresh",
|
|
45
|
+
startNewConversation: "Start new conversation",
|
|
46
|
+
conversationEnded: "This conversation has ended."
|
|
47
|
+
};
|
|
48
|
+
function resolveStrings(overrides) {
|
|
49
|
+
return overrides ? { ...defaultStrings, ...overrides } : defaultStrings;
|
|
50
|
+
}
|
|
51
|
+
var RECONNECT_BASE_DELAY_MS = 2e3;
|
|
52
|
+
var RECONNECT_MAX_DELAY_MS = 3e4;
|
|
19
53
|
var MAX_RECONNECT_ATTEMPTS = 5;
|
|
20
|
-
var TERMINAL_STATUSES = ["completed", "error", "offline"];
|
|
54
|
+
var TERMINAL_STATUSES = ["completed", "error", "offline", "expired"];
|
|
55
|
+
var PARKED_STATUSES = [
|
|
56
|
+
"awaiting_input",
|
|
57
|
+
"awaiting_voice",
|
|
58
|
+
"awaiting_form",
|
|
59
|
+
"awaiting_chat",
|
|
60
|
+
"awaiting_editor"
|
|
61
|
+
];
|
|
62
|
+
var DEFAULT_IDLE_DISCONNECT_MS = 5 * 6e4;
|
|
63
|
+
var DEFAULT_POLL_INTERVAL_MS = 45e3;
|
|
64
|
+
var DEFAULT_HIBERNATE_AFTER_MS = 60 * 6e4;
|
|
65
|
+
function computeReconnectDelay(attempt) {
|
|
66
|
+
const cap = Math.min(
|
|
67
|
+
RECONNECT_MAX_DELAY_MS,
|
|
68
|
+
RECONNECT_BASE_DELAY_MS * 2 ** attempt
|
|
69
|
+
);
|
|
70
|
+
return Math.random() * cap;
|
|
71
|
+
}
|
|
21
72
|
function useRunStream(config, onEvent) {
|
|
73
|
+
var _a, _b, _c;
|
|
22
74
|
const { runId, participantToken, apiBase } = config;
|
|
75
|
+
const policyConfig = config.connectionPolicy;
|
|
76
|
+
const dormancyDisabled = policyConfig === "always-on";
|
|
77
|
+
const idleDisconnectMs = (_a = !dormancyDisabled && typeof policyConfig === "object" ? policyConfig == null ? void 0 : policyConfig.idleDisconnectMs : void 0) != null ? _a : DEFAULT_IDLE_DISCONNECT_MS;
|
|
78
|
+
const pollIntervalMs = (_b = !dormancyDisabled && typeof policyConfig === "object" ? policyConfig == null ? void 0 : policyConfig.pollIntervalMs : void 0) != null ? _b : DEFAULT_POLL_INTERVAL_MS;
|
|
79
|
+
const hibernateAfterMs = (_c = !dormancyDisabled && typeof policyConfig === "object" ? policyConfig == null ? void 0 : policyConfig.hibernateAfterMs : void 0) != null ? _c : DEFAULT_HIBERNATE_AFTER_MS;
|
|
23
80
|
const [status, setStatus] = react.useState("idle");
|
|
24
81
|
const [activeSession, setActiveSession] = react.useState(null);
|
|
25
82
|
const [messages, setMessages] = react.useState([]);
|
|
@@ -28,11 +85,19 @@ function useRunStream(config, onEvent) {
|
|
|
28
85
|
const [output, setOutput] = react.useState(null);
|
|
29
86
|
const lastSeqRef = react.useRef(0);
|
|
30
87
|
const [lastSeq, setLastSeq] = react.useState(0);
|
|
88
|
+
const [currentToken, setCurrentToken] = react.useState(participantToken);
|
|
89
|
+
const currentTokenRef = react.useRef(currentToken);
|
|
90
|
+
currentTokenRef.current = currentToken;
|
|
91
|
+
react.useEffect(() => setCurrentToken(participantToken), [participantToken]);
|
|
31
92
|
const wsRef = react.useRef(null);
|
|
32
93
|
const statusRef = react.useRef("idle");
|
|
33
94
|
const reconnectTimerRef = react.useRef(null);
|
|
34
95
|
const replayTimeoutRef = react.useRef(null);
|
|
35
96
|
const mountedRef = react.useRef(true);
|
|
97
|
+
const connectionStateRef = react.useRef("active");
|
|
98
|
+
const idleTimerRef = react.useRef(null);
|
|
99
|
+
const hibernateTimerRef = react.useRef(null);
|
|
100
|
+
const pollTimerRef = react.useRef(null);
|
|
36
101
|
const reconnectAttemptsRef = react.useRef(0);
|
|
37
102
|
const manualDisconnectRef = react.useRef(false);
|
|
38
103
|
const onEventRef = react.useRef(onEvent);
|
|
@@ -42,8 +107,8 @@ function useRunStream(config, onEvent) {
|
|
|
42
107
|
setStatus(next);
|
|
43
108
|
}, []);
|
|
44
109
|
const authHeaders = react.useCallback(
|
|
45
|
-
() => ({ Authorization: `Bearer ${
|
|
46
|
-
[
|
|
110
|
+
() => ({ Authorization: `Bearer ${currentTokenRef.current}` }),
|
|
111
|
+
[]
|
|
47
112
|
);
|
|
48
113
|
const mergeTimeline = react.useCallback(
|
|
49
114
|
(incoming) => {
|
|
@@ -65,9 +130,18 @@ function useRunStream(config, onEvent) {
|
|
|
65
130
|
if (!mountedRef.current) return;
|
|
66
131
|
if (!runId || !apiBase) return;
|
|
67
132
|
manualDisconnectRef.current = false;
|
|
133
|
+
connectionStateRef.current = "active";
|
|
134
|
+
if (pollTimerRef.current) {
|
|
135
|
+
clearInterval(pollTimerRef.current);
|
|
136
|
+
pollTimerRef.current = null;
|
|
137
|
+
}
|
|
138
|
+
if (hibernateTimerRef.current) {
|
|
139
|
+
clearTimeout(hibernateTimerRef.current);
|
|
140
|
+
hibernateTimerRef.current = null;
|
|
141
|
+
}
|
|
68
142
|
updateStatus("connecting");
|
|
69
143
|
const wsUrl = apiBase.replace(/^http/, "ws") + `/api/participant/runs/${runId}/stream`;
|
|
70
|
-
const params = new URLSearchParams({ token:
|
|
144
|
+
const params = new URLSearchParams({ token: currentTokenRef.current });
|
|
71
145
|
const ws = new WebSocket(`${wsUrl}?${params}`);
|
|
72
146
|
wsRef.current = ws;
|
|
73
147
|
ws.onopen = () => {
|
|
@@ -105,15 +179,128 @@ function useRunStream(config, onEvent) {
|
|
|
105
179
|
if (isOffline) {
|
|
106
180
|
updateStatus("offline");
|
|
107
181
|
} else {
|
|
182
|
+
const attempt = reconnectAttemptsRef.current;
|
|
108
183
|
reconnectAttemptsRef.current += 1;
|
|
109
|
-
reconnectTimerRef.current = setTimeout(
|
|
184
|
+
reconnectTimerRef.current = setTimeout(
|
|
185
|
+
connect,
|
|
186
|
+
computeReconnectDelay(attempt)
|
|
187
|
+
);
|
|
110
188
|
}
|
|
111
189
|
}
|
|
112
190
|
};
|
|
113
191
|
}, [runId, participantToken, apiBase]);
|
|
192
|
+
const remintToken = react.useCallback(async () => {
|
|
193
|
+
try {
|
|
194
|
+
const res = await fetch(`${apiBase}/api/public/runs/${runId}/token`, {
|
|
195
|
+
method: "POST",
|
|
196
|
+
headers: { "Content-Type": "application/json" },
|
|
197
|
+
body: JSON.stringify({ participant_token: currentTokenRef.current })
|
|
198
|
+
});
|
|
199
|
+
if (!res.ok) return null;
|
|
200
|
+
const data = await res.json();
|
|
201
|
+
if (!data.participant_token) return null;
|
|
202
|
+
setCurrentToken(data.participant_token);
|
|
203
|
+
return data.participant_token;
|
|
204
|
+
} catch {
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
}, [apiBase, runId]);
|
|
208
|
+
const stopPolling = react.useCallback(() => {
|
|
209
|
+
if (pollTimerRef.current) {
|
|
210
|
+
clearInterval(pollTimerRef.current);
|
|
211
|
+
pollTimerRef.current = null;
|
|
212
|
+
}
|
|
213
|
+
if (hibernateTimerRef.current) {
|
|
214
|
+
clearTimeout(hibernateTimerRef.current);
|
|
215
|
+
hibernateTimerRef.current = null;
|
|
216
|
+
}
|
|
217
|
+
}, []);
|
|
218
|
+
const wake = react.useCallback(() => {
|
|
219
|
+
if (!mountedRef.current) return;
|
|
220
|
+
if (connectionStateRef.current === "active") return;
|
|
221
|
+
stopPolling();
|
|
222
|
+
reconnectAttemptsRef.current = 0;
|
|
223
|
+
connect();
|
|
224
|
+
}, [connect, stopPolling]);
|
|
225
|
+
const startPolling = react.useCallback(() => {
|
|
226
|
+
if (pollTimerRef.current) return;
|
|
227
|
+
pollTimerRef.current = setInterval(async () => {
|
|
228
|
+
if (!mountedRef.current) return;
|
|
229
|
+
try {
|
|
230
|
+
const res = await fetch(
|
|
231
|
+
`${apiBase}/api/participant/runs/${runId}/timeline?since_seq=${lastSeqRef.current}`,
|
|
232
|
+
{ headers: { Authorization: `Bearer ${currentTokenRef.current}` } }
|
|
233
|
+
);
|
|
234
|
+
if (res.status === 401) {
|
|
235
|
+
const fresh = await remintToken();
|
|
236
|
+
if (!fresh) {
|
|
237
|
+
stopPolling();
|
|
238
|
+
updateStatus("expired");
|
|
239
|
+
}
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if (!res.ok) return;
|
|
243
|
+
const entries = await res.json();
|
|
244
|
+
if (Array.isArray(entries) && entries.length > 0) {
|
|
245
|
+
wake();
|
|
246
|
+
}
|
|
247
|
+
} catch {
|
|
248
|
+
}
|
|
249
|
+
}, pollIntervalMs);
|
|
250
|
+
hibernateTimerRef.current = setTimeout(() => {
|
|
251
|
+
connectionStateRef.current = "hibernating";
|
|
252
|
+
if (pollTimerRef.current) {
|
|
253
|
+
clearInterval(pollTimerRef.current);
|
|
254
|
+
pollTimerRef.current = null;
|
|
255
|
+
}
|
|
256
|
+
}, hibernateAfterMs);
|
|
257
|
+
}, [apiBase, runId, pollIntervalMs, hibernateAfterMs, remintToken, stopPolling, updateStatus, wake]);
|
|
258
|
+
const goDormant = react.useCallback(() => {
|
|
259
|
+
var _a2;
|
|
260
|
+
if (connectionStateRef.current !== "active") return;
|
|
261
|
+
connectionStateRef.current = "dormant";
|
|
262
|
+
manualDisconnectRef.current = true;
|
|
263
|
+
(_a2 = wsRef.current) == null ? void 0 : _a2.close();
|
|
264
|
+
wsRef.current = null;
|
|
265
|
+
startPolling();
|
|
266
|
+
}, [startPolling]);
|
|
267
|
+
react.useEffect(() => {
|
|
268
|
+
if (dormancyDisabled) return;
|
|
269
|
+
if (idleTimerRef.current) {
|
|
270
|
+
clearTimeout(idleTimerRef.current);
|
|
271
|
+
idleTimerRef.current = null;
|
|
272
|
+
}
|
|
273
|
+
if (PARKED_STATUSES.includes(status) && connectionStateRef.current === "active") {
|
|
274
|
+
idleTimerRef.current = setTimeout(goDormant, idleDisconnectMs);
|
|
275
|
+
}
|
|
276
|
+
return () => {
|
|
277
|
+
if (idleTimerRef.current) {
|
|
278
|
+
clearTimeout(idleTimerRef.current);
|
|
279
|
+
idleTimerRef.current = null;
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
}, [status, dormancyDisabled, idleDisconnectMs, goDormant]);
|
|
283
|
+
react.useEffect(() => {
|
|
284
|
+
if (dormancyDisabled) return;
|
|
285
|
+
const handleVisibility = () => {
|
|
286
|
+
if (document.visibilityState === "visible") wake();
|
|
287
|
+
};
|
|
288
|
+
document.addEventListener("visibilitychange", handleVisibility);
|
|
289
|
+
window.addEventListener("online", wake);
|
|
290
|
+
window.addEventListener("focus", wake);
|
|
291
|
+
window.addEventListener("pointerdown", wake);
|
|
292
|
+
window.addEventListener("keydown", wake);
|
|
293
|
+
return () => {
|
|
294
|
+
document.removeEventListener("visibilitychange", handleVisibility);
|
|
295
|
+
window.removeEventListener("online", wake);
|
|
296
|
+
window.removeEventListener("focus", wake);
|
|
297
|
+
window.removeEventListener("pointerdown", wake);
|
|
298
|
+
window.removeEventListener("keydown", wake);
|
|
299
|
+
};
|
|
300
|
+
}, [dormancyDisabled, wake]);
|
|
114
301
|
function handleEvent(event) {
|
|
115
|
-
var
|
|
116
|
-
(
|
|
302
|
+
var _a2, _b2, _c2, _d, _e;
|
|
303
|
+
(_a2 = onEventRef.current) == null ? void 0 : _a2.call(onEventRef, event);
|
|
117
304
|
switch (event.type) {
|
|
118
305
|
case "timeline.replay": {
|
|
119
306
|
mergeTimeline(event.payload);
|
|
@@ -150,7 +337,7 @@ function useRunStream(config, onEvent) {
|
|
|
150
337
|
entry_context: event.entry_context,
|
|
151
338
|
status: "open",
|
|
152
339
|
messages: replayedMessages,
|
|
153
|
-
document_content: (
|
|
340
|
+
document_content: (_b2 = event.document_content) != null ? _b2 : null
|
|
154
341
|
};
|
|
155
342
|
setActiveSession(session);
|
|
156
343
|
if (replayedMessages.length > 0) {
|
|
@@ -163,7 +350,7 @@ function useRunStream(config, onEvent) {
|
|
|
163
350
|
} else if (event.layout === "form") {
|
|
164
351
|
updateStatus("awaiting_form");
|
|
165
352
|
} else {
|
|
166
|
-
const surface = (
|
|
353
|
+
const surface = (_c2 = event.entry_context) == null ? void 0 : _c2.surface;
|
|
167
354
|
const isVoice = typeof surface === "string" && (surface === "widget/voice" || surface === "phone" || surface === "phone/outbound");
|
|
168
355
|
if (isVoice) {
|
|
169
356
|
updateStatus("awaiting_voice");
|
|
@@ -203,17 +390,21 @@ function useRunStream(config, onEvent) {
|
|
|
203
390
|
mountedRef.current = true;
|
|
204
391
|
reconnectTimerRef.current = setTimeout(connect, 0);
|
|
205
392
|
return () => {
|
|
206
|
-
var
|
|
393
|
+
var _a2;
|
|
207
394
|
mountedRef.current = false;
|
|
208
395
|
if (reconnectTimerRef.current) clearTimeout(reconnectTimerRef.current);
|
|
209
396
|
if (replayTimeoutRef.current) clearTimeout(replayTimeoutRef.current);
|
|
210
|
-
(
|
|
397
|
+
if (idleTimerRef.current) clearTimeout(idleTimerRef.current);
|
|
398
|
+
if (pollTimerRef.current) clearInterval(pollTimerRef.current);
|
|
399
|
+
if (hibernateTimerRef.current) clearTimeout(hibernateTimerRef.current);
|
|
400
|
+
(_a2 = wsRef.current) == null ? void 0 : _a2.close();
|
|
211
401
|
wsRef.current = null;
|
|
212
402
|
};
|
|
213
403
|
}, [connect]);
|
|
214
404
|
const sendMessage = react.useCallback(
|
|
215
405
|
async (content) => {
|
|
216
406
|
if (!activeSession) return;
|
|
407
|
+
wake();
|
|
217
408
|
setMessages((prev) => [
|
|
218
409
|
...prev,
|
|
219
410
|
{ role: "human", content }
|
|
@@ -240,12 +431,13 @@ function useRunStream(config, onEvent) {
|
|
|
240
431
|
throw new Error(`sendMessage failed: ${res.status} ${text}`);
|
|
241
432
|
}
|
|
242
433
|
},
|
|
243
|
-
[activeSession, apiBase, authHeaders, updateStatus]
|
|
434
|
+
[activeSession, apiBase, authHeaders, updateStatus, wake]
|
|
244
435
|
);
|
|
245
436
|
const submitHandback = react.useCallback(
|
|
246
437
|
async (output2, idempotencyKey) => {
|
|
247
|
-
var
|
|
438
|
+
var _a2;
|
|
248
439
|
if (!activeSession) return null;
|
|
440
|
+
wake();
|
|
249
441
|
const body = { output: output2 };
|
|
250
442
|
if (idempotencyKey) body.idempotency_key = idempotencyKey;
|
|
251
443
|
let res;
|
|
@@ -267,7 +459,7 @@ function useRunStream(config, onEvent) {
|
|
|
267
459
|
}
|
|
268
460
|
if (res.status === 422) {
|
|
269
461
|
const data = await res.json();
|
|
270
|
-
return (
|
|
462
|
+
return (_a2 = data.errors) != null ? _a2 : [{ field: "_", message: "Validation failed" }];
|
|
271
463
|
}
|
|
272
464
|
if (!res.ok) {
|
|
273
465
|
const text = await res.text();
|
|
@@ -278,13 +470,13 @@ function useRunStream(config, onEvent) {
|
|
|
278
470
|
updateStatus("running");
|
|
279
471
|
return null;
|
|
280
472
|
},
|
|
281
|
-
[activeSession, apiBase, authHeaders, updateStatus]
|
|
473
|
+
[activeSession, apiBase, authHeaders, updateStatus, wake]
|
|
282
474
|
);
|
|
283
475
|
const disconnect = react.useCallback(() => {
|
|
284
|
-
var
|
|
476
|
+
var _a2;
|
|
285
477
|
manualDisconnectRef.current = true;
|
|
286
478
|
if (reconnectTimerRef.current) clearTimeout(reconnectTimerRef.current);
|
|
287
|
-
(
|
|
479
|
+
(_a2 = wsRef.current) == null ? void 0 : _a2.close();
|
|
288
480
|
wsRef.current = null;
|
|
289
481
|
updateStatus("offline");
|
|
290
482
|
}, [updateStatus]);
|
|
@@ -492,15 +684,24 @@ var widget_default = {
|
|
|
492
684
|
converseHint: "widget_converseHint",
|
|
493
685
|
converseHintDot: "widget_converseHintDot",
|
|
494
686
|
"cf-converse-pulse": "widget_cf-converse-pulse",
|
|
687
|
+
converseDoneButton: "widget_converseDoneButton",
|
|
495
688
|
newMessageChip: "widget_newMessageChip",
|
|
496
689
|
activeInputRow: "widget_activeInputRow",
|
|
497
690
|
activeInputField: "widget_activeInputField",
|
|
498
691
|
activeInputSubmit: "widget_activeInputSubmit",
|
|
499
692
|
inlineError: "widget_inlineError",
|
|
500
693
|
resumingBanner: "widget_resumingBanner",
|
|
694
|
+
reconnectingPill: "widget_reconnectingPill",
|
|
501
695
|
terminalBanner: "widget_terminalBanner",
|
|
502
696
|
terminalCompleted: "widget_terminalBanner widget_terminalCompleted",
|
|
503
697
|
terminalError: "widget_terminalBanner widget_terminalError",
|
|
698
|
+
startNewConversationButton: "widget_startNewConversationButton",
|
|
699
|
+
gateCard: "widget_gateCard",
|
|
700
|
+
gateTitle: "widget_gateTitle",
|
|
701
|
+
gateDescription: "widget_gateDescription",
|
|
702
|
+
gateActions: "widget_gateActions",
|
|
703
|
+
gateButtonPrimary: "widget_gateButtonPrimary",
|
|
704
|
+
gateButtonSecondary: "widget_gateButtonSecondary",
|
|
504
705
|
typingIndicator: "widget_typingIndicator",
|
|
505
706
|
typingDot: "widget_typingDot",
|
|
506
707
|
cfTyping: "widget_cfTyping",
|
|
@@ -2669,28 +2870,6 @@ function FormRenderer({
|
|
|
2669
2870
|
) })
|
|
2670
2871
|
] });
|
|
2671
2872
|
}
|
|
2672
|
-
|
|
2673
|
-
// src/strings.ts
|
|
2674
|
-
var defaultStrings = {
|
|
2675
|
-
statusInitialising: "Initialising\u2026",
|
|
2676
|
-
statusConnecting: "Connecting\u2026",
|
|
2677
|
-
statusRunning: "Running\u2026",
|
|
2678
|
-
statusCompletedTitle: "Completed",
|
|
2679
|
-
statusCompletedBody: "This workflow run has finished.",
|
|
2680
|
-
statusErrorTitle: "Something went wrong",
|
|
2681
|
-
statusOfflineTitle: "Connection lost",
|
|
2682
|
-
statusOfflineBody: "Unable to reach the server. Please check your connection and refresh.",
|
|
2683
|
-
statusListening: "Listening\u2026",
|
|
2684
|
-
resumingConversation: "Resuming conversation\u2026",
|
|
2685
|
-
conversationCompleted: "Conversation completed.",
|
|
2686
|
-
somethingWentWrong: "Something went wrong",
|
|
2687
|
-
chatInputPlaceholder: "Type a message\u2026",
|
|
2688
|
-
sendButton: "Send",
|
|
2689
|
-
handBackButton: "Hand Back"
|
|
2690
|
-
};
|
|
2691
|
-
function resolveStrings(overrides) {
|
|
2692
|
-
return overrides ? { ...defaultStrings, ...overrides } : defaultStrings;
|
|
2693
|
-
}
|
|
2694
2873
|
function StatusDisplay({ status, error, strings }) {
|
|
2695
2874
|
const t = strings ? { ...defaultStrings, ...strings } : defaultStrings;
|
|
2696
2875
|
switch (status) {
|
|
@@ -2720,6 +2899,11 @@ function StatusDisplay({ status, error, strings }) {
|
|
|
2720
2899
|
/* @__PURE__ */ jsxRuntime.jsx("div", { className: `${widget_default.statusTitle} ${widget_default.statusError}`, children: t.statusOfflineTitle }),
|
|
2721
2900
|
/* @__PURE__ */ jsxRuntime.jsx("div", { className: `${widget_default.statusLabel} ${widget_default.statusError}`, children: t.statusOfflineBody })
|
|
2722
2901
|
] });
|
|
2902
|
+
case "expired":
|
|
2903
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: widget_default.statusWrapper, children: [
|
|
2904
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: `${widget_default.statusTitle} ${widget_default.statusError}`, children: t.statusExpiredTitle }),
|
|
2905
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: `${widget_default.statusLabel} ${widget_default.statusError}`, children: t.statusExpiredBody })
|
|
2906
|
+
] });
|
|
2723
2907
|
case "awaiting_voice":
|
|
2724
2908
|
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: widget_default.statusWrapper, children: [
|
|
2725
2909
|
/* @__PURE__ */ jsxRuntime.jsx("div", { className: widget_default.spinner, "aria-hidden": true }),
|
|
@@ -5817,6 +6001,7 @@ var ChatAdapter = react.forwardRef(
|
|
|
5817
6001
|
runId,
|
|
5818
6002
|
participantToken,
|
|
5819
6003
|
apiBase,
|
|
6004
|
+
connectionPolicy,
|
|
5820
6005
|
className,
|
|
5821
6006
|
onCompleted,
|
|
5822
6007
|
onError,
|
|
@@ -5836,7 +6021,7 @@ var ChatAdapter = react.forwardRef(
|
|
|
5836
6021
|
components,
|
|
5837
6022
|
strings
|
|
5838
6023
|
}, ref) {
|
|
5839
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i
|
|
6024
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
5840
6025
|
const t = resolveStrings(strings);
|
|
5841
6026
|
const apiBaseResolved = apiBase;
|
|
5842
6027
|
const appBaseResolved = typeof window !== "undefined" ? window.location.origin : (_a = process.env.APP_URL) != null ? _a : "http://localhost:3000";
|
|
@@ -5872,7 +6057,7 @@ var ChatAdapter = react.forwardRef(
|
|
|
5872
6057
|
output,
|
|
5873
6058
|
disconnect,
|
|
5874
6059
|
reconnect
|
|
5875
|
-
} = useRunStream({ runId, participantToken, apiBase }, onEventFirehose);
|
|
6060
|
+
} = useRunStream({ runId, participantToken, apiBase, connectionPolicy }, onEventFirehose);
|
|
5876
6061
|
react.useImperativeHandle(
|
|
5877
6062
|
ref,
|
|
5878
6063
|
() => ({
|
|
@@ -5994,6 +6179,7 @@ var ChatAdapter = react.forwardRef(
|
|
|
5994
6179
|
}, [timeline, isVoiceSurface, enqueueSpeak]);
|
|
5995
6180
|
const completedFiredRef = react.useRef(false);
|
|
5996
6181
|
const errorFiredRef = react.useRef(false);
|
|
6182
|
+
const expiredFiredRef = react.useRef(false);
|
|
5997
6183
|
const inputAreaRef = react.useRef(null);
|
|
5998
6184
|
const lastFocusedSessionRef = react.useRef(null);
|
|
5999
6185
|
const activeSessionId = (_c = activeSession == null ? void 0 : activeSession.id) != null ? _c : null;
|
|
@@ -6020,6 +6206,12 @@ var ChatAdapter = react.forwardRef(
|
|
|
6020
6206
|
errorFiredRef.current = true;
|
|
6021
6207
|
onError == null ? void 0 : onError(new WidgetError("run_failed", "The run encountered an error."));
|
|
6022
6208
|
}
|
|
6209
|
+
if (status === "expired" && !expiredFiredRef.current) {
|
|
6210
|
+
expiredFiredRef.current = true;
|
|
6211
|
+
onError == null ? void 0 : onError(
|
|
6212
|
+
new WidgetError("token_expired", "The participant token expired and could not be renewed.")
|
|
6213
|
+
);
|
|
6214
|
+
}
|
|
6023
6215
|
}, [status, output, onCompleted, onError]);
|
|
6024
6216
|
const colorSchemeClass = colorScheme === "dark" ? widget_default.cfDark : colorScheme === "auto" ? widget_default.cfAuto : void 0;
|
|
6025
6217
|
const cls = [widget_default.widget, colorSchemeClass, className].filter(Boolean).join(" ");
|
|
@@ -6066,10 +6258,11 @@ var ChatAdapter = react.forwardRef(
|
|
|
6066
6258
|
},
|
|
6067
6259
|
[handleSubmit]
|
|
6068
6260
|
);
|
|
6069
|
-
|
|
6261
|
+
const isSilentReconnect = (status === "connecting" || status === "replaying") && timeline.length > 0;
|
|
6262
|
+
if (status === "idle" || status === "connecting" && !isSilentReconnect) {
|
|
6070
6263
|
return /* @__PURE__ */ jsxRuntime.jsx("div", { className: cls, style: rootStyle, children: loadingFallback != null ? loadingFallback : /* @__PURE__ */ jsxRuntime.jsx(StatusDisplay, { status, strings }) });
|
|
6071
6264
|
}
|
|
6072
|
-
if (status === "replaying") {
|
|
6265
|
+
if (status === "replaying" && !isSilentReconnect) {
|
|
6073
6266
|
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: cls, style: rootStyle, children: [
|
|
6074
6267
|
header,
|
|
6075
6268
|
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: widget_default.resumingBanner, children: [
|
|
@@ -6128,7 +6321,11 @@ var ChatAdapter = react.forwardRef(
|
|
|
6128
6321
|
}
|
|
6129
6322
|
) });
|
|
6130
6323
|
}
|
|
6131
|
-
|
|
6324
|
+
const converseCtx = activeSession == null ? void 0 : activeSession.entry_context;
|
|
6325
|
+
const isConverseSession = (converseCtx == null ? void 0 : converseCtx.converse_session) === true;
|
|
6326
|
+
const converseUserCanEnd = (converseCtx == null ? void 0 : converseCtx.converse_user_can_end) === true;
|
|
6327
|
+
const field = activeSession ? extractField(activeSession) : null;
|
|
6328
|
+
if (status === "awaiting_chat" && activeSession && !(isConverseSession && field)) {
|
|
6132
6329
|
const hbCtx = (_e = activeSession.entry_context) == null ? void 0 : _e.handback;
|
|
6133
6330
|
const outputSchema = hbCtx == null ? void 0 : hbCtx.output_schema;
|
|
6134
6331
|
const requireOutput = (_f = hbCtx == null ? void 0 : hbCtx.require_output) != null ? _f : false;
|
|
@@ -6224,18 +6421,16 @@ var ChatAdapter = react.forwardRef(
|
|
|
6224
6421
|
] })
|
|
6225
6422
|
] });
|
|
6226
6423
|
}
|
|
6227
|
-
const field = activeSession ? extractField(activeSession) : null;
|
|
6228
6424
|
const surface = activeSession ? extractSurface(activeSession) : null;
|
|
6229
6425
|
const surfaceConfig = activeSession ? extractSurfaceConfig(activeSession) : null;
|
|
6230
6426
|
const nodeVoice = activeSession ? extractNodeVoice(activeSession) : null;
|
|
6231
6427
|
const InputComponent = field ? getInputComponent(field.input_type) : null;
|
|
6232
|
-
const isConverseSession = ((_g = activeSession == null ? void 0 : activeSession.entry_context) == null ? void 0 : _g.converse_session) === true;
|
|
6233
6428
|
const effectiveVoiceMode = resolveVoiceMode(surface, surfaceConfig, nodeVoice);
|
|
6234
6429
|
const shouldUseVoice = effectiveVoiceMode !== "off";
|
|
6235
6430
|
const voiceRenderer = effectiveVoiceMode === "push_to_talk" ? "click" : "continuous";
|
|
6236
6431
|
const isLiveVoice = surface === "widget/voice" && (surfaceConfig == null ? void 0 : surfaceConfig.style) === "live";
|
|
6237
6432
|
if (shouldUseVoice && activeSession && field) {
|
|
6238
|
-
const spokenHint = (
|
|
6433
|
+
const spokenHint = (_h = nodeVoice == null ? void 0 : nodeVoice.hint) != null ? _h : (_g = field.voice) == null ? void 0 : _g.hint;
|
|
6239
6434
|
const handleVoiceValue = (value) => {
|
|
6240
6435
|
setInputValue(value);
|
|
6241
6436
|
void submitHandback(value, crypto.randomUUID());
|
|
@@ -6267,7 +6462,7 @@ var ChatAdapter = react.forwardRef(
|
|
|
6267
6462
|
participantToken,
|
|
6268
6463
|
onValue: handleVoiceValue,
|
|
6269
6464
|
autoSubmitMode: "confirm",
|
|
6270
|
-
confidenceThreshold: (
|
|
6465
|
+
confidenceThreshold: (_i = nodeVoice == null ? void 0 : nodeVoice.confidence_threshold) != null ? _i : typeof (surfaceConfig == null ? void 0 : surfaceConfig.confidence_threshold) === "number" ? surfaceConfig.confidence_threshold : 0.7,
|
|
6271
6466
|
enqueueSpeak,
|
|
6272
6467
|
isSpeaking,
|
|
6273
6468
|
disabled: submitting
|
|
@@ -6300,6 +6495,10 @@ var ChatAdapter = react.forwardRef(
|
|
|
6300
6495
|
) });
|
|
6301
6496
|
}
|
|
6302
6497
|
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: cls, style: rootStyle, children: [
|
|
6498
|
+
isSilentReconnect && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: widget_default.reconnectingPill, children: [
|
|
6499
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: widget_default.spinner }),
|
|
6500
|
+
t.reconnecting
|
|
6501
|
+
] }),
|
|
6303
6502
|
header,
|
|
6304
6503
|
/* @__PURE__ */ jsxRuntime.jsx(ChatTimeline, { entries: timeline, showTyping, streamingText, components }),
|
|
6305
6504
|
activeSession && field && InputComponent && /* @__PURE__ */ jsxRuntime.jsxs("form", { ref: inputAreaRef, className: widget_default.activeInputArea, onSubmit: handleSubmit, children: [
|
|
@@ -6342,7 +6541,17 @@ var ChatAdapter = react.forwardRef(
|
|
|
6342
6541
|
serverErrors.length > 1 && serverErrors.slice(1).map((e, i) => /* @__PURE__ */ jsxRuntime.jsxs("div", { className: widget_default.inlineError, children: [
|
|
6343
6542
|
e.field !== "_" ? `${e.field}: ` : "",
|
|
6344
6543
|
e.message
|
|
6345
|
-
] }, i))
|
|
6544
|
+
] }, i)),
|
|
6545
|
+
isConverseSession && converseUserCanEnd && /* @__PURE__ */ jsxRuntime.jsx(
|
|
6546
|
+
"button",
|
|
6547
|
+
{
|
|
6548
|
+
type: "button",
|
|
6549
|
+
className: widget_default.converseDoneButton,
|
|
6550
|
+
onClick: () => void submitHandback({ _done: true }),
|
|
6551
|
+
disabled: submitting,
|
|
6552
|
+
children: t.converseDoneButton
|
|
6553
|
+
}
|
|
6554
|
+
)
|
|
6346
6555
|
] }),
|
|
6347
6556
|
!activeSession && status === "running" && timeline.length === 0 && (emptyState != null ? emptyState : /* @__PURE__ */ jsxRuntime.jsx(StatusDisplay, { status: "running", strings })),
|
|
6348
6557
|
footer
|
|
@@ -6487,15 +6696,44 @@ function clearPersistedSession(config, target) {
|
|
|
6487
6696
|
} catch {
|
|
6488
6697
|
}
|
|
6489
6698
|
}
|
|
6699
|
+
async function fetchPersistedRunTerminalStatus(apiBase, runId, participantToken) {
|
|
6700
|
+
var _a;
|
|
6701
|
+
try {
|
|
6702
|
+
const res = await fetch(`${apiBase}/api/participant/runs/${runId}/timeline`, {
|
|
6703
|
+
headers: { Authorization: `Bearer ${participantToken}` }
|
|
6704
|
+
});
|
|
6705
|
+
if (!res.ok) return null;
|
|
6706
|
+
const entries = await res.json();
|
|
6707
|
+
if (!Array.isArray(entries)) return null;
|
|
6708
|
+
for (let i = entries.length - 1; i >= 0; i--) {
|
|
6709
|
+
const e = entries[i];
|
|
6710
|
+
if (e.event_type === "status_change") {
|
|
6711
|
+
const s = (_a = e.payload) == null ? void 0 : _a.status;
|
|
6712
|
+
if (s === "completed" || s === "failed") return s;
|
|
6713
|
+
}
|
|
6714
|
+
}
|
|
6715
|
+
return null;
|
|
6716
|
+
} catch {
|
|
6717
|
+
return null;
|
|
6718
|
+
}
|
|
6719
|
+
}
|
|
6490
6720
|
var ChatterflyWidget = react.forwardRef(
|
|
6491
6721
|
function ChatterflyWidget2(props, ref) {
|
|
6492
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
6722
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
|
|
6493
6723
|
const [resolved, setResolved] = react.useState(null);
|
|
6494
6724
|
const [configError, setConfigError] = react.useState(null);
|
|
6495
6725
|
const [restartNonce, setRestartNonce] = react.useState(0);
|
|
6496
6726
|
const adapterRef = react.useRef(null);
|
|
6497
6727
|
const autoStarting = Boolean(!props.runId && (props.deploymentId || props.workflowId));
|
|
6498
6728
|
const persistTarget = (_a = props.workflowId) != null ? _a : props.deploymentId;
|
|
6729
|
+
const effectivePersistSession = props.persistSession === void 0 ? true : props.persistSession;
|
|
6730
|
+
const showCompletedHistory = (_b = props.showCompletedHistory) != null ? _b : true;
|
|
6731
|
+
const t = { ...defaultStrings, ...props.strings };
|
|
6732
|
+
const [gate, setGate] = react.useState("idle");
|
|
6733
|
+
const [terminalReplayActive, setTerminalReplayActive] = react.useState(false);
|
|
6734
|
+
const pendingPersistedRef = react.useRef(null);
|
|
6735
|
+
const startNewRunRef = react.useRef(() => {
|
|
6736
|
+
});
|
|
6499
6737
|
react.useEffect(() => {
|
|
6500
6738
|
var _a2;
|
|
6501
6739
|
if (props.runId) return;
|
|
@@ -6510,52 +6748,109 @@ var ChatterflyWidget = react.forwardRef(
|
|
|
6510
6748
|
return;
|
|
6511
6749
|
}
|
|
6512
6750
|
setConfigError(null);
|
|
6513
|
-
|
|
6514
|
-
|
|
6515
|
-
|
|
6516
|
-
|
|
6517
|
-
|
|
6751
|
+
const base = props.apiBase;
|
|
6752
|
+
const doStart = (input) => {
|
|
6753
|
+
const startUrl = props.workflowId ? `${base}/api/public/workflows/${props.workflowId}/start` : `${base}/api/public/surveys/${props.deploymentId}/start`;
|
|
6754
|
+
const body = input ? JSON.stringify({ input }) : void 0;
|
|
6755
|
+
fetch(startUrl, {
|
|
6756
|
+
method: "POST",
|
|
6757
|
+
headers: body ? { "Content-Type": "application/json" } : void 0,
|
|
6758
|
+
body
|
|
6759
|
+
}).then(async (r) => {
|
|
6760
|
+
if (!r.ok) {
|
|
6761
|
+
const code = r.status === 404 ? "no_live_deployment" : "workflow_not_public";
|
|
6762
|
+
const detail = await r.text().catch(() => "");
|
|
6763
|
+
throw new WidgetError(
|
|
6764
|
+
code,
|
|
6765
|
+
`Failed to start run (HTTP ${r.status})${detail ? `: ${detail}` : ""}`
|
|
6766
|
+
);
|
|
6767
|
+
}
|
|
6768
|
+
return r.json();
|
|
6769
|
+
}).then(({ run_id, participant_token }) => {
|
|
6770
|
+
setGate("idle");
|
|
6771
|
+
setTerminalReplayActive(false);
|
|
6772
|
+
setResolved({ runId: run_id, participantToken: participant_token });
|
|
6773
|
+
savePersistedSession(effectivePersistSession, persistTarget, {
|
|
6774
|
+
runId: run_id,
|
|
6775
|
+
participantToken: participant_token
|
|
6776
|
+
});
|
|
6777
|
+
}).catch((err) => {
|
|
6778
|
+
var _a3;
|
|
6779
|
+
const widgetErr = err instanceof WidgetError ? err : new WidgetError("network", err instanceof Error ? err.message : String(err));
|
|
6780
|
+
(_a3 = props.onError) == null ? void 0 : _a3.call(props, widgetErr);
|
|
6781
|
+
});
|
|
6782
|
+
};
|
|
6783
|
+
startNewRunRef.current = doStart;
|
|
6784
|
+
if (restartNonce > 0) {
|
|
6785
|
+
doStart(props.initialInput);
|
|
6786
|
+
return;
|
|
6787
|
+
}
|
|
6788
|
+
const persisted = loadPersistedSession(effectivePersistSession, persistTarget);
|
|
6789
|
+
if (!persisted) {
|
|
6790
|
+
if (props.clickToStart) {
|
|
6791
|
+
setGate("click-to-start");
|
|
6792
|
+
} else {
|
|
6793
|
+
doStart(props.initialInput);
|
|
6518
6794
|
}
|
|
6795
|
+
return;
|
|
6519
6796
|
}
|
|
6520
|
-
|
|
6521
|
-
|
|
6522
|
-
|
|
6523
|
-
|
|
6524
|
-
|
|
6525
|
-
|
|
6526
|
-
|
|
6527
|
-
|
|
6528
|
-
|
|
6529
|
-
|
|
6530
|
-
|
|
6531
|
-
|
|
6532
|
-
|
|
6533
|
-
|
|
6534
|
-
|
|
6797
|
+
let cancelled = false;
|
|
6798
|
+
fetchPersistedRunTerminalStatus(base, persisted.runId, persisted.participantToken).then(
|
|
6799
|
+
(terminal) => {
|
|
6800
|
+
if (cancelled) return;
|
|
6801
|
+
if (terminal) {
|
|
6802
|
+
if (showCompletedHistory) {
|
|
6803
|
+
setTerminalReplayActive(true);
|
|
6804
|
+
setGate("idle");
|
|
6805
|
+
setResolved(persisted);
|
|
6806
|
+
} else {
|
|
6807
|
+
clearPersistedSession(effectivePersistSession, persistTarget);
|
|
6808
|
+
doStart(props.initialInput);
|
|
6809
|
+
}
|
|
6810
|
+
} else if (props.resumePrompt) {
|
|
6811
|
+
pendingPersistedRef.current = persisted;
|
|
6812
|
+
setGate("resume-choice");
|
|
6813
|
+
} else {
|
|
6814
|
+
setResolved(persisted);
|
|
6815
|
+
}
|
|
6535
6816
|
}
|
|
6536
|
-
|
|
6537
|
-
|
|
6538
|
-
|
|
6539
|
-
|
|
6540
|
-
runId: run_id,
|
|
6541
|
-
participantToken: participant_token
|
|
6542
|
-
});
|
|
6543
|
-
}).catch((err) => {
|
|
6544
|
-
var _a3;
|
|
6545
|
-
const widgetErr = err instanceof WidgetError ? err : new WidgetError("network", err instanceof Error ? err.message : String(err));
|
|
6546
|
-
(_a3 = props.onError) == null ? void 0 : _a3.call(props, widgetErr);
|
|
6547
|
-
});
|
|
6817
|
+
);
|
|
6818
|
+
return () => {
|
|
6819
|
+
cancelled = true;
|
|
6820
|
+
};
|
|
6548
6821
|
}, [props.deploymentId, props.workflowId, props.apiBase, restartNonce]);
|
|
6549
6822
|
const restart = react.useCallback(() => {
|
|
6550
6823
|
var _a2;
|
|
6551
6824
|
if (autoStarting) {
|
|
6552
|
-
clearPersistedSession(
|
|
6825
|
+
clearPersistedSession(effectivePersistSession, persistTarget);
|
|
6553
6826
|
setResolved(null);
|
|
6827
|
+
setTerminalReplayActive(false);
|
|
6828
|
+
setGate("idle");
|
|
6554
6829
|
setRestartNonce((n) => n + 1);
|
|
6555
6830
|
} else {
|
|
6556
6831
|
(_a2 = adapterRef.current) == null ? void 0 : _a2.restart();
|
|
6557
6832
|
}
|
|
6558
|
-
}, [autoStarting,
|
|
6833
|
+
}, [autoStarting, effectivePersistSession, persistTarget]);
|
|
6834
|
+
const handleClickToStart = react.useCallback(() => {
|
|
6835
|
+
startNewRunRef.current(props.initialInput);
|
|
6836
|
+
}, [props.initialInput]);
|
|
6837
|
+
const handleResumeContinue = react.useCallback(() => {
|
|
6838
|
+
if (pendingPersistedRef.current) {
|
|
6839
|
+
setResolved(pendingPersistedRef.current);
|
|
6840
|
+
setGate("idle");
|
|
6841
|
+
}
|
|
6842
|
+
}, []);
|
|
6843
|
+
const handleResumeRestart = react.useCallback(() => {
|
|
6844
|
+
clearPersistedSession(effectivePersistSession, persistTarget);
|
|
6845
|
+
setGate("idle");
|
|
6846
|
+
startNewRunRef.current(props.initialInput);
|
|
6847
|
+
}, [effectivePersistSession, persistTarget, props.initialInput]);
|
|
6848
|
+
const handleStartNewFromReplay = react.useCallback(() => {
|
|
6849
|
+
clearPersistedSession(effectivePersistSession, persistTarget);
|
|
6850
|
+
setTerminalReplayActive(false);
|
|
6851
|
+
setResolved(null);
|
|
6852
|
+
startNewRunRef.current(props.initialInput);
|
|
6853
|
+
}, [effectivePersistSession, persistTarget, props.initialInput]);
|
|
6559
6854
|
const disconnect = react.useCallback(() => {
|
|
6560
6855
|
var _a2;
|
|
6561
6856
|
(_a2 = adapterRef.current) == null ? void 0 : _a2.disconnect();
|
|
@@ -6569,10 +6864,10 @@ var ChatterflyWidget = react.forwardRef(
|
|
|
6569
6864
|
disconnect,
|
|
6570
6865
|
sendMessage
|
|
6571
6866
|
]);
|
|
6572
|
-
const runId = (
|
|
6573
|
-
const participantToken = (
|
|
6574
|
-
const mode = (
|
|
6575
|
-
const [open, setOpen] = react.useState((
|
|
6867
|
+
const runId = (_c = resolved == null ? void 0 : resolved.runId) != null ? _c : props.runId;
|
|
6868
|
+
const participantToken = (_d = resolved == null ? void 0 : resolved.participantToken) != null ? _d : props.participantToken;
|
|
6869
|
+
const mode = (_e = props.mode) != null ? _e : "inline";
|
|
6870
|
+
const [open, setOpen] = react.useState((_f = props.defaultOpen) != null ? _f : false);
|
|
6576
6871
|
const [unreadCount, setUnreadCount] = react.useState(0);
|
|
6577
6872
|
const toggleOpen = react.useCallback(() => {
|
|
6578
6873
|
setOpen((prev) => {
|
|
@@ -6594,10 +6889,62 @@ var ChatterflyWidget = react.forwardRef(
|
|
|
6594
6889
|
);
|
|
6595
6890
|
let content;
|
|
6596
6891
|
if (configError) {
|
|
6597
|
-
content = (
|
|
6892
|
+
content = (_g = props.loadingFallback) != null ? _g : /* @__PURE__ */ jsxRuntime.jsx(StatusDisplay, { status: "error" });
|
|
6893
|
+
} else if (gate === "click-to-start") {
|
|
6894
|
+
const cfg = typeof props.clickToStart === "object" ? props.clickToStart : void 0;
|
|
6895
|
+
content = /* @__PURE__ */ jsxRuntime.jsxs("div", { className: widget_default.gateCard, children: [
|
|
6896
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: widget_default.gateTitle, children: (_h = cfg == null ? void 0 : cfg.label) != null ? _h : t.startConversation }),
|
|
6897
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: widget_default.gateDescription, children: (_i = cfg == null ? void 0 : cfg.description) != null ? _i : t.clickToStartDescription }),
|
|
6898
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: widget_default.gateActions, children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
6899
|
+
"button",
|
|
6900
|
+
{
|
|
6901
|
+
type: "button",
|
|
6902
|
+
className: widget_default.gateButtonPrimary,
|
|
6903
|
+
onClick: handleClickToStart,
|
|
6904
|
+
children: (_j = cfg == null ? void 0 : cfg.label) != null ? _j : t.startConversation
|
|
6905
|
+
}
|
|
6906
|
+
) })
|
|
6907
|
+
] });
|
|
6908
|
+
} else if (gate === "resume-choice") {
|
|
6909
|
+
const cfg = typeof props.resumePrompt === "object" ? props.resumePrompt : void 0;
|
|
6910
|
+
content = /* @__PURE__ */ jsxRuntime.jsxs("div", { className: widget_default.gateCard, children: [
|
|
6911
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: widget_default.gateTitle, children: (_k = cfg == null ? void 0 : cfg.title) != null ? _k : t.resumeTitle }),
|
|
6912
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: widget_default.gateActions, children: [
|
|
6913
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
6914
|
+
"button",
|
|
6915
|
+
{
|
|
6916
|
+
type: "button",
|
|
6917
|
+
className: widget_default.gateButtonPrimary,
|
|
6918
|
+
onClick: handleResumeContinue,
|
|
6919
|
+
children: (_l = cfg == null ? void 0 : cfg.continueLabel) != null ? _l : t.resumeContinue
|
|
6920
|
+
}
|
|
6921
|
+
),
|
|
6922
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
6923
|
+
"button",
|
|
6924
|
+
{
|
|
6925
|
+
type: "button",
|
|
6926
|
+
className: widget_default.gateButtonSecondary,
|
|
6927
|
+
onClick: handleResumeRestart,
|
|
6928
|
+
children: (_m = cfg == null ? void 0 : cfg.restartLabel) != null ? _m : t.resumeRestart
|
|
6929
|
+
}
|
|
6930
|
+
)
|
|
6931
|
+
] })
|
|
6932
|
+
] });
|
|
6598
6933
|
} else if (!runId || !participantToken) {
|
|
6599
|
-
content = (
|
|
6934
|
+
content = (_n = props.loadingFallback) != null ? _n : /* @__PURE__ */ jsxRuntime.jsx(StatusDisplay, { status: autoStarting ? "connecting" : "error" });
|
|
6600
6935
|
} else {
|
|
6936
|
+
const replayCompletedState = terminalReplayActive ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
6937
|
+
t.conversationEnded,
|
|
6938
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
6939
|
+
"button",
|
|
6940
|
+
{
|
|
6941
|
+
type: "button",
|
|
6942
|
+
className: widget_default.startNewConversationButton,
|
|
6943
|
+
onClick: handleStartNewFromReplay,
|
|
6944
|
+
children: t.startNewConversation
|
|
6945
|
+
}
|
|
6946
|
+
)
|
|
6947
|
+
] }) : void 0;
|
|
6601
6948
|
content = /* @__PURE__ */ jsxRuntime.jsx(
|
|
6602
6949
|
ChatAdapter,
|
|
6603
6950
|
{
|
|
@@ -6605,14 +6952,16 @@ var ChatterflyWidget = react.forwardRef(
|
|
|
6605
6952
|
ref: adapterRef,
|
|
6606
6953
|
runId,
|
|
6607
6954
|
participantToken,
|
|
6608
|
-
onMessage: handleMessage
|
|
6955
|
+
onMessage: handleMessage,
|
|
6956
|
+
completedState: (_o = props.completedState) != null ? _o : replayCompletedState,
|
|
6957
|
+
errorState: (_p = props.errorState) != null ? _p : replayCompletedState
|
|
6609
6958
|
}
|
|
6610
6959
|
);
|
|
6611
6960
|
}
|
|
6612
6961
|
if (mode !== "floating") {
|
|
6613
6962
|
return content;
|
|
6614
6963
|
}
|
|
6615
|
-
const position = (
|
|
6964
|
+
const position = (_q = props.position) != null ? _q : "bottom-right";
|
|
6616
6965
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
6617
6966
|
FloatingLauncherChrome,
|
|
6618
6967
|
{
|
|
@@ -6670,7 +7019,8 @@ function FloatingLauncherChrome({
|
|
|
6670
7019
|
}
|
|
6671
7020
|
function useChatterflyRun(options) {
|
|
6672
7021
|
var _a, _b;
|
|
6673
|
-
const { runId, participantToken, apiBase, deploymentId, workflowId, initialInput, persistSession, onEvent } = options;
|
|
7022
|
+
const { runId, participantToken, apiBase, deploymentId, workflowId, initialInput, persistSession, connectionPolicy, onEvent } = options;
|
|
7023
|
+
const effectivePersistSession = persistSession === void 0 ? true : persistSession;
|
|
6674
7024
|
const [resolved, setResolved] = react.useState(null);
|
|
6675
7025
|
const [error, setError] = react.useState(null);
|
|
6676
7026
|
const [restartNonce, setRestartNonce] = react.useState(0);
|
|
@@ -6689,7 +7039,7 @@ function useChatterflyRun(options) {
|
|
|
6689
7039
|
}
|
|
6690
7040
|
setError(null);
|
|
6691
7041
|
if (restartNonce === 0) {
|
|
6692
|
-
const persisted = loadPersistedSession(
|
|
7042
|
+
const persisted = loadPersistedSession(effectivePersistSession, persistTarget);
|
|
6693
7043
|
if (persisted) {
|
|
6694
7044
|
setResolved({ runId: persisted.runId, participantToken: persisted.participantToken });
|
|
6695
7045
|
return;
|
|
@@ -6713,7 +7063,7 @@ function useChatterflyRun(options) {
|
|
|
6713
7063
|
return r.json();
|
|
6714
7064
|
}).then(({ run_id, participant_token }) => {
|
|
6715
7065
|
setResolved({ runId: run_id, participantToken: participant_token });
|
|
6716
|
-
savePersistedSession(
|
|
7066
|
+
savePersistedSession(effectivePersistSession, persistTarget, {
|
|
6717
7067
|
runId: run_id,
|
|
6718
7068
|
participantToken: participant_token
|
|
6719
7069
|
});
|
|
@@ -6729,19 +7079,29 @@ function useChatterflyRun(options) {
|
|
|
6729
7079
|
{
|
|
6730
7080
|
runId: effectiveRunId != null ? effectiveRunId : "",
|
|
6731
7081
|
participantToken: effectiveToken != null ? effectiveToken : "",
|
|
6732
|
-
apiBase: apiBase != null ? apiBase : ""
|
|
7082
|
+
apiBase: apiBase != null ? apiBase : "",
|
|
7083
|
+
connectionPolicy
|
|
6733
7084
|
},
|
|
6734
7085
|
onEvent
|
|
6735
7086
|
);
|
|
6736
7087
|
const restart = react.useCallback(() => {
|
|
6737
7088
|
if (autoStarting) {
|
|
6738
|
-
clearPersistedSession(
|
|
7089
|
+
clearPersistedSession(effectivePersistSession, persistTarget);
|
|
6739
7090
|
setResolved(null);
|
|
6740
7091
|
setRestartNonce((n) => n + 1);
|
|
6741
7092
|
} else {
|
|
6742
7093
|
stream.reconnect();
|
|
6743
7094
|
}
|
|
6744
|
-
}, [autoStarting,
|
|
7095
|
+
}, [autoStarting, effectivePersistSession, persistTarget, stream]);
|
|
7096
|
+
const expiredErrorFiredRef = react.useRef(false);
|
|
7097
|
+
react.useEffect(() => {
|
|
7098
|
+
if (stream.status === "expired" && !expiredErrorFiredRef.current) {
|
|
7099
|
+
expiredErrorFiredRef.current = true;
|
|
7100
|
+
setError(
|
|
7101
|
+
new WidgetError("token_expired", "The participant token expired and could not be renewed.")
|
|
7102
|
+
);
|
|
7103
|
+
}
|
|
7104
|
+
}, [stream.status]);
|
|
6745
7105
|
const connecting = autoStarting && !resolved && !error;
|
|
6746
7106
|
return {
|
|
6747
7107
|
...stream,
|