@bikdotai/bik-component-library 0.0.868-beta.9 → 0.0.868
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 +0 -3
- package/dist/cjs/components/agent-builder/constants/tools.js +1 -1
- package/dist/cjs/components/agent-builder/constants/tools.js.map +1 -1
- package/dist/cjs/components/bik-copilot/BIKCopilot.js +2 -0
- package/dist/cjs/components/bik-copilot/BIKCopilot.js.map +1 -0
- package/dist/cjs/components/bik-copilot/ControlledCopilot.js +2 -0
- package/dist/cjs/components/bik-copilot/ControlledCopilot.js.map +1 -0
- package/dist/cjs/components/bik-copilot/ControlledCopilot.styled.js +27 -0
- package/dist/cjs/components/bik-copilot/ControlledCopilot.styled.js.map +1 -0
- package/dist/cjs/components/bik-copilot/CopilotBall.js +2 -0
- package/dist/cjs/components/bik-copilot/CopilotBall.js.map +1 -0
- package/dist/cjs/components/bik-copilot/CopilotBall.styled.js +42 -0
- package/dist/cjs/components/bik-copilot/CopilotBall.styled.js.map +1 -0
- package/dist/cjs/components/bik-copilot/CopilotFeedbackModal.js +2 -0
- package/dist/cjs/components/bik-copilot/CopilotFeedbackModal.js.map +1 -0
- package/dist/cjs/components/bik-copilot/CopilotFeedbackModal.styled.js +108 -0
- package/dist/cjs/components/bik-copilot/CopilotFeedbackModal.styled.js.map +1 -0
- package/dist/cjs/components/bik-copilot/CopilotPanel.js +2 -0
- package/dist/cjs/components/bik-copilot/CopilotPanel.js.map +1 -0
- package/dist/cjs/components/bik-copilot/CopilotPanel.styled.js +534 -0
- package/dist/cjs/components/bik-copilot/CopilotPanel.styled.js.map +1 -0
- package/dist/cjs/components/bik-copilot/copilotPlacement.js +2 -0
- package/dist/cjs/components/bik-copilot/copilotPlacement.js.map +1 -0
- package/dist/cjs/components/bik-copilot/copilotRegenerate.js +2 -0
- package/dist/cjs/components/bik-copilot/copilotRegenerate.js.map +1 -0
- package/dist/cjs/components/bik-copilot/services/copilotDb.js +2 -0
- package/dist/cjs/components/bik-copilot/services/copilotDb.js.map +1 -0
- package/dist/cjs/components/bik-copilot/services/copilotFeedbackClient.js +2 -0
- package/dist/cjs/components/bik-copilot/services/copilotFeedbackClient.js.map +1 -0
- package/dist/cjs/components/bik-copilot/services/copilotIcebreakersClient.js +2 -0
- package/dist/cjs/components/bik-copilot/services/copilotIcebreakersClient.js.map +1 -0
- package/dist/cjs/components/bik-copilot/services/copilotMessagesClient.js +2 -0
- package/dist/cjs/components/bik-copilot/services/copilotMessagesClient.js.map +1 -0
- package/dist/cjs/components/bik-copilot/services/copilotPageSession.js +2 -0
- package/dist/cjs/components/bik-copilot/services/copilotPageSession.js.map +1 -0
- package/dist/cjs/components/bik-copilot/services/copilotSseParser.js +7 -0
- package/dist/cjs/components/bik-copilot/services/copilotSseParser.js.map +1 -0
- package/dist/cjs/components/bik-copilot/services/copilotStreamClient.js +2 -0
- package/dist/cjs/components/bik-copilot/services/copilotStreamClient.js.map +1 -0
- package/dist/cjs/components/bik-copilot/useCopilotIcebreakers.js +2 -0
- package/dist/cjs/components/bik-copilot/useCopilotIcebreakers.js.map +1 -0
- package/dist/cjs/components/bik-copilot/useCopilotStreamChat.js +2 -0
- package/dist/cjs/components/bik-copilot/useCopilotStreamChat.js.map +1 -0
- package/dist/cjs/components/bik-copilot/useDraggableFloater.js +2 -0
- package/dist/cjs/components/bik-copilot/useDraggableFloater.js.map +1 -0
- package/dist/cjs/components/bik-copilot/utils/markdown.js +4 -0
- package/dist/cjs/components/bik-copilot/utils/markdown.js.map +1 -0
- package/dist/cjs/components/bik-copilot/utils/toolCtas.js +2 -0
- package/dist/cjs/components/bik-copilot/utils/toolCtas.js.map +1 -0
- package/dist/cjs/components/button/themes.js +2 -2
- package/dist/cjs/components/button/themes.js.map +1 -1
- package/dist/cjs/components/datePicker/DatePickerWrapper.js +1 -1
- package/dist/cjs/components/datePicker/DatePickerWrapper.js.map +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/esm/components/agent-builder/constants/tools.d.ts +1 -0
- package/dist/esm/components/agent-builder/constants/tools.js +18 -18
- package/dist/esm/components/agent-builder/constants/tools.js.map +1 -1
- package/dist/esm/components/bik-copilot/BIKCopilot.d.ts +31 -0
- package/dist/esm/components/bik-copilot/BIKCopilot.js +118 -0
- package/dist/esm/components/bik-copilot/BIKCopilot.js.map +1 -0
- package/dist/esm/components/bik-copilot/ControlledCopilot.d.ts +14 -0
- package/dist/esm/components/bik-copilot/ControlledCopilot.js +174 -0
- package/dist/esm/components/bik-copilot/ControlledCopilot.js.map +1 -0
- package/dist/esm/components/bik-copilot/ControlledCopilot.styled.d.ts +7 -0
- package/dist/esm/components/bik-copilot/ControlledCopilot.styled.js +31 -0
- package/dist/esm/components/bik-copilot/ControlledCopilot.styled.js.map +1 -0
- package/dist/esm/components/bik-copilot/CopilotBall.d.ts +13 -0
- package/dist/esm/components/bik-copilot/CopilotBall.js +50 -0
- package/dist/esm/components/bik-copilot/CopilotBall.js.map +1 -0
- package/dist/esm/components/bik-copilot/CopilotBall.styled.d.ts +9 -0
- package/dist/esm/components/bik-copilot/CopilotBall.styled.js +46 -0
- package/dist/esm/components/bik-copilot/CopilotBall.styled.js.map +1 -0
- package/dist/esm/components/bik-copilot/CopilotFeedbackModal.d.ts +29 -0
- package/dist/esm/components/bik-copilot/CopilotFeedbackModal.js +73 -0
- package/dist/esm/components/bik-copilot/CopilotFeedbackModal.js.map +1 -0
- package/dist/esm/components/bik-copilot/CopilotFeedbackModal.styled.d.ts +12 -0
- package/dist/esm/components/bik-copilot/CopilotFeedbackModal.styled.js +122 -0
- package/dist/esm/components/bik-copilot/CopilotFeedbackModal.styled.js.map +1 -0
- package/dist/esm/components/bik-copilot/CopilotPanel.d.ts +9 -0
- package/dist/esm/components/bik-copilot/CopilotPanel.js +536 -0
- package/dist/esm/components/bik-copilot/CopilotPanel.js.map +1 -0
- package/dist/esm/components/bik-copilot/CopilotPanel.styled.d.ts +67 -0
- package/dist/esm/components/bik-copilot/CopilotPanel.styled.js +573 -0
- package/dist/esm/components/bik-copilot/CopilotPanel.styled.js.map +1 -0
- package/dist/esm/components/bik-copilot/copilotPlacement.d.ts +24 -0
- package/dist/esm/components/bik-copilot/copilotPlacement.js +38 -0
- package/dist/esm/components/bik-copilot/copilotPlacement.js.map +1 -0
- package/dist/esm/components/bik-copilot/copilotRegenerate.d.ts +22 -0
- package/dist/esm/components/bik-copilot/copilotRegenerate.js +6 -0
- package/dist/esm/components/bik-copilot/copilotRegenerate.js.map +1 -0
- package/dist/esm/components/bik-copilot/index.d.ts +19 -0
- package/dist/esm/components/bik-copilot/model.d.ts +804 -0
- package/dist/esm/components/bik-copilot/services/copilotDb.d.ts +90 -0
- package/dist/esm/components/bik-copilot/services/copilotDb.js +96 -0
- package/dist/esm/components/bik-copilot/services/copilotDb.js.map +1 -0
- package/dist/esm/components/bik-copilot/services/copilotFeedbackClient.d.ts +15 -0
- package/dist/esm/components/bik-copilot/services/copilotFeedbackClient.js +55 -0
- package/dist/esm/components/bik-copilot/services/copilotFeedbackClient.js.map +1 -0
- package/dist/esm/components/bik-copilot/services/copilotIcebreakersClient.d.ts +11 -0
- package/dist/esm/components/bik-copilot/services/copilotIcebreakersClient.js +54 -0
- package/dist/esm/components/bik-copilot/services/copilotIcebreakersClient.js.map +1 -0
- package/dist/esm/components/bik-copilot/services/copilotMessagesClient.d.ts +23 -0
- package/dist/esm/components/bik-copilot/services/copilotMessagesClient.js +78 -0
- package/dist/esm/components/bik-copilot/services/copilotMessagesClient.js.map +1 -0
- package/dist/esm/components/bik-copilot/services/copilotPageSession.d.ts +37 -0
- package/dist/esm/components/bik-copilot/services/copilotPageSession.js +35 -0
- package/dist/esm/components/bik-copilot/services/copilotPageSession.js.map +1 -0
- package/dist/esm/components/bik-copilot/services/copilotSseParser.d.ts +22 -0
- package/dist/esm/components/bik-copilot/services/copilotSseParser.js +52 -0
- package/dist/esm/components/bik-copilot/services/copilotSseParser.js.map +1 -0
- package/dist/esm/components/bik-copilot/services/copilotStreamClient.d.ts +35 -0
- package/dist/esm/components/bik-copilot/services/copilotStreamClient.js +110 -0
- package/dist/esm/components/bik-copilot/services/copilotStreamClient.js.map +1 -0
- package/dist/esm/components/bik-copilot/useCopilotIcebreakers.d.ts +17 -0
- package/dist/esm/components/bik-copilot/useCopilotIcebreakers.js +60 -0
- package/dist/esm/components/bik-copilot/useCopilotIcebreakers.js.map +1 -0
- package/dist/esm/components/bik-copilot/useCopilotStreamChat.d.ts +20 -0
- package/dist/esm/components/bik-copilot/useCopilotStreamChat.js +377 -0
- package/dist/esm/components/bik-copilot/useCopilotStreamChat.js.map +1 -0
- package/dist/esm/components/bik-copilot/useDraggableFloater.d.ts +9 -0
- package/dist/esm/components/bik-copilot/useDraggableFloater.js +118 -0
- package/dist/esm/components/bik-copilot/useDraggableFloater.js.map +1 -0
- package/dist/esm/components/bik-copilot/utils/markdown.d.ts +1 -0
- package/dist/esm/components/bik-copilot/utils/markdown.js +48 -0
- package/dist/esm/components/bik-copilot/utils/markdown.js.map +1 -0
- package/dist/esm/components/bik-copilot/utils/toolCtas.d.ts +6 -0
- package/dist/esm/components/bik-copilot/utils/toolCtas.js +27 -0
- package/dist/esm/components/bik-copilot/utils/toolCtas.js.map +1 -0
- package/dist/esm/components/button/themes.js +1 -2
- package/dist/esm/components/button/themes.js.map +1 -1
- package/dist/esm/components/datePicker/DatePickerWrapper.d.ts +0 -7
- package/dist/esm/components/datePicker/DatePickerWrapper.js +64 -66
- package/dist/esm/components/datePicker/DatePickerWrapper.js.map +1 -1
- package/dist/esm/components/datePicker/index.d.ts +0 -1
- package/dist/esm/index.d.ts +1 -3
- package/dist/esm/index.js +844 -828
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/cjs/components/chip-selector/ChipSelector.js +0 -2
- package/dist/cjs/components/chip-selector/ChipSelector.js.map +0 -1
- package/dist/cjs/components/chip-selector/ChipSelector.styled.js +0 -36
- package/dist/cjs/components/chip-selector/ChipSelector.styled.js.map +0 -1
- package/dist/cjs/components/chip-selector/ChipSelectorGroup.js +0 -2
- package/dist/cjs/components/chip-selector/ChipSelectorGroup.js.map +0 -1
- package/dist/cjs/components/combo-box/ComboBox.js +0 -2
- package/dist/cjs/components/combo-box/ComboBox.js.map +0 -1
- package/dist/cjs/components/combo-box/ComboBox.styled.js +0 -33
- package/dist/cjs/components/combo-box/ComboBox.styled.js.map +0 -1
- package/dist/cjs/components/schedule-builder/RefreshSegmentToggle.js +0 -2
- package/dist/cjs/components/schedule-builder/RefreshSegmentToggle.js.map +0 -1
- package/dist/cjs/components/schedule-builder/ScheduleBuilder.js +0 -2
- package/dist/cjs/components/schedule-builder/ScheduleBuilder.js.map +0 -1
- package/dist/cjs/components/schedule-builder/ScheduleBuilder.styled.js +0 -138
- package/dist/cjs/components/schedule-builder/ScheduleBuilder.styled.js.map +0 -1
- package/dist/cjs/components/schedule-builder/ScheduleEndPicker.js +0 -2
- package/dist/cjs/components/schedule-builder/ScheduleEndPicker.js.map +0 -1
- package/dist/cjs/components/schedule-builder/ScheduleFrequencyPicker.js +0 -2
- package/dist/cjs/components/schedule-builder/ScheduleFrequencyPicker.js.map +0 -1
- package/dist/cjs/components/schedule-builder/ScheduleHelper.js +0 -2
- package/dist/cjs/components/schedule-builder/ScheduleHelper.js.map +0 -1
- package/dist/cjs/components/schedule-builder/SendModeSelector.js +0 -2
- package/dist/cjs/components/schedule-builder/SendModeSelector.js.map +0 -1
- package/dist/cjs/components/schedule-builder/StartDateTimePicker.js +0 -2
- package/dist/cjs/components/schedule-builder/StartDateTimePicker.js.map +0 -1
- package/dist/cjs/components/schedule-builder/UpcomingRunsPreview.js +0 -2
- package/dist/cjs/components/schedule-builder/UpcomingRunsPreview.js.map +0 -1
- package/dist/cjs/components/schedule-builder/model.js +0 -2
- package/dist/cjs/components/schedule-builder/model.js.map +0 -1
- package/dist/esm/components/chip-selector/ChipSelector.d.ts +0 -9
- package/dist/esm/components/chip-selector/ChipSelector.js +0 -27
- package/dist/esm/components/chip-selector/ChipSelector.js.map +0 -1
- package/dist/esm/components/chip-selector/ChipSelector.styled.d.ts +0 -10
- package/dist/esm/components/chip-selector/ChipSelector.styled.js +0 -48
- package/dist/esm/components/chip-selector/ChipSelector.styled.js.map +0 -1
- package/dist/esm/components/chip-selector/ChipSelectorGroup.d.ts +0 -10
- package/dist/esm/components/chip-selector/ChipSelectorGroup.js +0 -48
- package/dist/esm/components/chip-selector/ChipSelectorGroup.js.map +0 -1
- package/dist/esm/components/chip-selector/index.d.ts +0 -4
- package/dist/esm/components/chip-selector/model.d.ts +0 -106
- package/dist/esm/components/combo-box/ComboBox.d.ts +0 -15
- package/dist/esm/components/combo-box/ComboBox.js +0 -118
- package/dist/esm/components/combo-box/ComboBox.js.map +0 -1
- package/dist/esm/components/combo-box/ComboBox.styled.d.ts +0 -12
- package/dist/esm/components/combo-box/ComboBox.styled.js +0 -40
- package/dist/esm/components/combo-box/ComboBox.styled.js.map +0 -1
- package/dist/esm/components/combo-box/index.d.ts +0 -3
- package/dist/esm/components/combo-box/model.d.ts +0 -55
- package/dist/esm/components/schedule-builder/RefreshSegmentToggle.d.ts +0 -22
- package/dist/esm/components/schedule-builder/RefreshSegmentToggle.js +0 -31
- package/dist/esm/components/schedule-builder/RefreshSegmentToggle.js.map +0 -1
- package/dist/esm/components/schedule-builder/ScheduleBuilder.d.ts +0 -88
- package/dist/esm/components/schedule-builder/ScheduleBuilder.js +0 -105
- package/dist/esm/components/schedule-builder/ScheduleBuilder.js.map +0 -1
- package/dist/esm/components/schedule-builder/ScheduleBuilder.styled.d.ts +0 -81
- package/dist/esm/components/schedule-builder/ScheduleBuilder.styled.js +0 -168
- package/dist/esm/components/schedule-builder/ScheduleBuilder.styled.js.map +0 -1
- package/dist/esm/components/schedule-builder/ScheduleEndPicker.d.ts +0 -17
- package/dist/esm/components/schedule-builder/ScheduleEndPicker.js +0 -105
- package/dist/esm/components/schedule-builder/ScheduleEndPicker.js.map +0 -1
- package/dist/esm/components/schedule-builder/ScheduleFrequencyPicker.d.ts +0 -21
- package/dist/esm/components/schedule-builder/ScheduleFrequencyPicker.js +0 -136
- package/dist/esm/components/schedule-builder/ScheduleFrequencyPicker.js.map +0 -1
- package/dist/esm/components/schedule-builder/ScheduleHelper.d.ts +0 -110
- package/dist/esm/components/schedule-builder/ScheduleHelper.js +0 -196
- package/dist/esm/components/schedule-builder/ScheduleHelper.js.map +0 -1
- package/dist/esm/components/schedule-builder/SendModeSelector.d.ts +0 -29
- package/dist/esm/components/schedule-builder/SendModeSelector.js +0 -41
- package/dist/esm/components/schedule-builder/SendModeSelector.js.map +0 -1
- package/dist/esm/components/schedule-builder/StartDateTimePicker.d.ts +0 -51
- package/dist/esm/components/schedule-builder/StartDateTimePicker.js +0 -120
- package/dist/esm/components/schedule-builder/StartDateTimePicker.js.map +0 -1
- package/dist/esm/components/schedule-builder/UpcomingRunsPreview.d.ts +0 -19
- package/dist/esm/components/schedule-builder/UpcomingRunsPreview.js +0 -44
- package/dist/esm/components/schedule-builder/UpcomingRunsPreview.js.map +0 -1
- package/dist/esm/components/schedule-builder/index.d.ts +0 -18
- package/dist/esm/components/schedule-builder/model.d.ts +0 -69
- package/dist/esm/components/schedule-builder/model.js +0 -8
- package/dist/esm/components/schedule-builder/model.js.map +0 -1
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
import { useState as I, useRef as p, useCallback as i, useEffect as Y, useMemo as gt } from "react";
|
|
2
|
+
import { COPILOT_REGENERATE_PROMPT as It } from "./copilotRegenerate.js";
|
|
3
|
+
import { persistCopilotTurn as ht, pruneCopilotSessions as yt, deleteCopilotSession as Ct, loadCopilotSession as Tt } from "./services/copilotDb.js";
|
|
4
|
+
import { sendCopilotFeedback as Et } from "./services/copilotFeedbackClient.js";
|
|
5
|
+
import { fetchCopilotMessages as wt, COPILOT_HISTORY_PAGE_SIZE as St } from "./services/copilotMessagesClient.js";
|
|
6
|
+
import { resetCopilotSessionForRoute as vt, resolveCopilotSessionIdForRoute as Ot, getCopilotSessionIdForRoute as Mt } from "./services/copilotPageSession.js";
|
|
7
|
+
import { readCopilotEventContent as Z, streamCopilotChat as Pt } from "./services/copilotStreamClient.js";
|
|
8
|
+
import { DEFAULT_COPILOT_ROUTE as Ht, DEFAULT_COPILOT_AGENT as _t } from "./useCopilotIcebreakers.js";
|
|
9
|
+
const nt = "Something went wrong. Please try again.", Nt = ({
|
|
10
|
+
storeId: a,
|
|
11
|
+
agent: H = _t,
|
|
12
|
+
route: m = Ht,
|
|
13
|
+
messagesPath: j,
|
|
14
|
+
feedbackPath: q,
|
|
15
|
+
user: J,
|
|
16
|
+
loadHistory: K = !0,
|
|
17
|
+
systemPrompt: Q,
|
|
18
|
+
sessionId: h,
|
|
19
|
+
persist: l = !0,
|
|
20
|
+
onError: w,
|
|
21
|
+
onResponseComplete: V,
|
|
22
|
+
onSessionIdChange: N,
|
|
23
|
+
baseUrl: _,
|
|
24
|
+
endpointPath: W,
|
|
25
|
+
headers: b,
|
|
26
|
+
authToken: x,
|
|
27
|
+
getAuthToken: L,
|
|
28
|
+
fetchImpl: A
|
|
29
|
+
}) => {
|
|
30
|
+
const [D, B] = I([]), [st, tt] = I(
|
|
31
|
+
h ?? null
|
|
32
|
+
), [O, S] = I(!1), [ot, et] = I(!0), [ct, M] = I(), [ut, it] = I(), [at, F] = I(!1), [$, R] = I(!1), f = p([]), z = p(void 0), y = p(null), c = p(null), C = p(h ?? null), k = p(m);
|
|
33
|
+
k.current = m;
|
|
34
|
+
const T = p(1), P = p(!1), u = i(
|
|
35
|
+
(t) => {
|
|
36
|
+
const e = t(f.current);
|
|
37
|
+
f.current = e, B(e);
|
|
38
|
+
},
|
|
39
|
+
[]
|
|
40
|
+
), v = i((t) => {
|
|
41
|
+
z.current = t, it(t);
|
|
42
|
+
}, []), g = i(() => {
|
|
43
|
+
const t = C.current;
|
|
44
|
+
!l || !t || ht({
|
|
45
|
+
route: k.current,
|
|
46
|
+
sessionId: t,
|
|
47
|
+
messages: f.current,
|
|
48
|
+
previousResponseId: z.current,
|
|
49
|
+
storeId: a,
|
|
50
|
+
agent: H
|
|
51
|
+
});
|
|
52
|
+
}, [H, l, a]), U = p({
|
|
53
|
+
baseUrl: _,
|
|
54
|
+
messagesPath: j,
|
|
55
|
+
feedbackPath: q,
|
|
56
|
+
headers: b,
|
|
57
|
+
authToken: x,
|
|
58
|
+
getAuthToken: L,
|
|
59
|
+
fetchImpl: A,
|
|
60
|
+
storeId: a,
|
|
61
|
+
loadHistory: K,
|
|
62
|
+
user: J
|
|
63
|
+
});
|
|
64
|
+
U.current = {
|
|
65
|
+
baseUrl: _,
|
|
66
|
+
messagesPath: j,
|
|
67
|
+
feedbackPath: q,
|
|
68
|
+
headers: b,
|
|
69
|
+
authToken: x,
|
|
70
|
+
getAuthToken: L,
|
|
71
|
+
fetchImpl: A,
|
|
72
|
+
storeId: a,
|
|
73
|
+
loadHistory: K,
|
|
74
|
+
user: J
|
|
75
|
+
};
|
|
76
|
+
const G = i(async (t) => {
|
|
77
|
+
const e = U.current, r = C.current;
|
|
78
|
+
return !e.loadHistory || !r ? null : wt({
|
|
79
|
+
baseUrl: e.baseUrl,
|
|
80
|
+
endpointPath: e.messagesPath,
|
|
81
|
+
headers: e.headers,
|
|
82
|
+
authToken: e.authToken,
|
|
83
|
+
getAuthToken: e.getAuthToken,
|
|
84
|
+
fetchImpl: e.fetchImpl,
|
|
85
|
+
storeId: e.storeId,
|
|
86
|
+
sessionId: r,
|
|
87
|
+
page: t,
|
|
88
|
+
pageSize: St
|
|
89
|
+
});
|
|
90
|
+
}, []);
|
|
91
|
+
Y(() => {
|
|
92
|
+
let t = !1;
|
|
93
|
+
return (async () => {
|
|
94
|
+
c.current?.(), c.current = null, y.current = null, S(!1), M(void 0), et(!0), R(!1), P.current = !1, F(!1);
|
|
95
|
+
const r = h ?? (l ? await Ot({ route: m, storeId: a }) : Mt({ route: m, storeId: a })), n = l ? await Tt(m, r) : null;
|
|
96
|
+
if (!t) {
|
|
97
|
+
if (C.current = r, tt(r), f.current = n?.messages ?? [], B(f.current), v(n?.previousResponseId), et(!1), N?.(r), f.current.length > 0) {
|
|
98
|
+
T.current = 2, R(U.current.loadHistory);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (!h) {
|
|
102
|
+
T.current = 1, R(!1);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
T.current = 1;
|
|
106
|
+
try {
|
|
107
|
+
const s = await G(1);
|
|
108
|
+
if (t || !s)
|
|
109
|
+
return;
|
|
110
|
+
T.current = 2, u(() => s.messages), R(s.hasMore);
|
|
111
|
+
} catch {
|
|
112
|
+
R(!1);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
})(), () => {
|
|
116
|
+
t = !0;
|
|
117
|
+
};
|
|
118
|
+
}, [
|
|
119
|
+
m,
|
|
120
|
+
a,
|
|
121
|
+
h,
|
|
122
|
+
l,
|
|
123
|
+
v,
|
|
124
|
+
u,
|
|
125
|
+
G
|
|
126
|
+
]);
|
|
127
|
+
const lt = i(() => {
|
|
128
|
+
if (P.current || !$)
|
|
129
|
+
return;
|
|
130
|
+
P.current = !0, F(!0);
|
|
131
|
+
const t = T.current;
|
|
132
|
+
(async () => {
|
|
133
|
+
try {
|
|
134
|
+
const e = await G(t);
|
|
135
|
+
if (!e) {
|
|
136
|
+
R(!1);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
T.current = t + 1, R(e.hasMore), u((r) => {
|
|
140
|
+
const n = new Set(
|
|
141
|
+
r.flatMap(
|
|
142
|
+
(o) => o.messageId ? [o.id, o.messageId] : [o.id]
|
|
143
|
+
)
|
|
144
|
+
), s = e.messages.filter(
|
|
145
|
+
(o) => !n.has(o.id)
|
|
146
|
+
);
|
|
147
|
+
return s.length > 0 ? [...s, ...r] : r;
|
|
148
|
+
});
|
|
149
|
+
} catch (e) {
|
|
150
|
+
w?.(
|
|
151
|
+
e instanceof Error ? e : new Error(String(e))
|
|
152
|
+
);
|
|
153
|
+
} finally {
|
|
154
|
+
P.current = !1, F(!1);
|
|
155
|
+
}
|
|
156
|
+
})();
|
|
157
|
+
}, [u, $, w, G]);
|
|
158
|
+
Y(() => {
|
|
159
|
+
l && yt();
|
|
160
|
+
}, [l]), Y(() => () => c.current?.(), []);
|
|
161
|
+
const E = i(
|
|
162
|
+
(t) => {
|
|
163
|
+
const e = y.current;
|
|
164
|
+
y.current = null, e && u(
|
|
165
|
+
(r) => r.map((n) => {
|
|
166
|
+
if (n.id !== e)
|
|
167
|
+
return n;
|
|
168
|
+
const s = {
|
|
169
|
+
...n,
|
|
170
|
+
isStreaming: !1
|
|
171
|
+
};
|
|
172
|
+
return t ? t(s) : s;
|
|
173
|
+
}).filter(
|
|
174
|
+
(n) => n.id !== e || n.content.length > 0 || n.wasInterrupted === !0
|
|
175
|
+
)
|
|
176
|
+
);
|
|
177
|
+
},
|
|
178
|
+
[u]
|
|
179
|
+
), rt = i(
|
|
180
|
+
(t) => {
|
|
181
|
+
const e = y.current;
|
|
182
|
+
if (t.type === "STREAM") {
|
|
183
|
+
const r = Z(t).text;
|
|
184
|
+
if (!e || !r)
|
|
185
|
+
return;
|
|
186
|
+
u(
|
|
187
|
+
(n) => n.map(
|
|
188
|
+
(s) => s.id === e ? {
|
|
189
|
+
...s,
|
|
190
|
+
content: s.content + r,
|
|
191
|
+
messageId: t.messageId ?? s.messageId
|
|
192
|
+
} : s
|
|
193
|
+
)
|
|
194
|
+
);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (t.type === "TEXT") {
|
|
198
|
+
const {
|
|
199
|
+
text: r,
|
|
200
|
+
followUps: n,
|
|
201
|
+
toolResults: s,
|
|
202
|
+
responseId: o
|
|
203
|
+
} = Z(t);
|
|
204
|
+
E((d) => ({
|
|
205
|
+
...d,
|
|
206
|
+
content: r,
|
|
207
|
+
messageId: t.messageId ?? d.messageId,
|
|
208
|
+
...n ? { followUps: n } : {},
|
|
209
|
+
// Kept on the message, not just handled in passing, so the
|
|
210
|
+
// action button is still there after a reload.
|
|
211
|
+
...s ? { toolResults: s } : {}
|
|
212
|
+
})), v(o), g(), V?.({
|
|
213
|
+
text: r,
|
|
214
|
+
responseId: o,
|
|
215
|
+
followUps: n
|
|
216
|
+
});
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
t.type === "ERROR" && (M(Z(t).text || nt), E(), g());
|
|
220
|
+
},
|
|
221
|
+
[
|
|
222
|
+
u,
|
|
223
|
+
E,
|
|
224
|
+
V,
|
|
225
|
+
g,
|
|
226
|
+
v
|
|
227
|
+
]
|
|
228
|
+
), X = i(
|
|
229
|
+
(t) => {
|
|
230
|
+
const e = t.trim(), r = C.current;
|
|
231
|
+
if (!e || !r || O)
|
|
232
|
+
return;
|
|
233
|
+
M(void 0);
|
|
234
|
+
const n = Date.now(), s = `copilot-a-${n}`;
|
|
235
|
+
y.current = s, u((o) => [
|
|
236
|
+
...o,
|
|
237
|
+
{
|
|
238
|
+
id: `copilot-u-${n}`,
|
|
239
|
+
role: "user",
|
|
240
|
+
content: e,
|
|
241
|
+
timestamp: n
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
id: s,
|
|
245
|
+
role: "assistant",
|
|
246
|
+
content: "",
|
|
247
|
+
timestamp: n + 1,
|
|
248
|
+
isStreaming: !0
|
|
249
|
+
}
|
|
250
|
+
]), S(!0), g(), c.current = Pt(
|
|
251
|
+
{
|
|
252
|
+
baseUrl: _,
|
|
253
|
+
endpointPath: W,
|
|
254
|
+
headers: b,
|
|
255
|
+
authToken: x,
|
|
256
|
+
getAuthToken: L,
|
|
257
|
+
fetchImpl: A,
|
|
258
|
+
storeId: a,
|
|
259
|
+
agent: H,
|
|
260
|
+
route: m,
|
|
261
|
+
userPrompt: e,
|
|
262
|
+
sessionId: r,
|
|
263
|
+
previousResponseId: z.current,
|
|
264
|
+
systemPrompt: Q
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
onEvent: rt,
|
|
268
|
+
onComplete: () => {
|
|
269
|
+
E(), S(!1), c.current = null, g();
|
|
270
|
+
},
|
|
271
|
+
onError: (o) => {
|
|
272
|
+
M(o.message || nt), E(), S(!1), c.current = null, g(), w?.(o);
|
|
273
|
+
},
|
|
274
|
+
onAbort: () => {
|
|
275
|
+
E(), S(!1), c.current = null, g();
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
);
|
|
279
|
+
},
|
|
280
|
+
[
|
|
281
|
+
u,
|
|
282
|
+
x,
|
|
283
|
+
_,
|
|
284
|
+
W,
|
|
285
|
+
A,
|
|
286
|
+
H,
|
|
287
|
+
E,
|
|
288
|
+
L,
|
|
289
|
+
rt,
|
|
290
|
+
b,
|
|
291
|
+
O,
|
|
292
|
+
w,
|
|
293
|
+
m,
|
|
294
|
+
g,
|
|
295
|
+
a,
|
|
296
|
+
Q
|
|
297
|
+
]
|
|
298
|
+
), ft = i(() => {
|
|
299
|
+
f.current.some(
|
|
300
|
+
(e) => e.role === "assistant" && e.content.length > 0
|
|
301
|
+
) && X(It);
|
|
302
|
+
}, [X]), dt = i(
|
|
303
|
+
(t, e, r) => {
|
|
304
|
+
const n = U.current, s = C.current;
|
|
305
|
+
if (!s)
|
|
306
|
+
return;
|
|
307
|
+
const o = f.current.find(
|
|
308
|
+
(d) => (d.messageId ?? d.id) === t
|
|
309
|
+
);
|
|
310
|
+
Et({
|
|
311
|
+
baseUrl: n.baseUrl,
|
|
312
|
+
endpointPath: n.feedbackPath,
|
|
313
|
+
headers: n.headers,
|
|
314
|
+
authToken: n.authToken,
|
|
315
|
+
getAuthToken: n.getAuthToken,
|
|
316
|
+
fetchImpl: n.fetchImpl,
|
|
317
|
+
storeId: n.storeId,
|
|
318
|
+
sessionId: s,
|
|
319
|
+
messageId: t,
|
|
320
|
+
feedback: e,
|
|
321
|
+
route: k.current,
|
|
322
|
+
reply: o?.content,
|
|
323
|
+
user: n.user,
|
|
324
|
+
reasons: r?.reasons,
|
|
325
|
+
comment: r?.comment
|
|
326
|
+
}).catch((d) => {
|
|
327
|
+
w?.(
|
|
328
|
+
d instanceof Error ? d : new Error(String(d))
|
|
329
|
+
);
|
|
330
|
+
});
|
|
331
|
+
},
|
|
332
|
+
[w]
|
|
333
|
+
), pt = i(() => {
|
|
334
|
+
if (!c.current)
|
|
335
|
+
return;
|
|
336
|
+
const t = y.current;
|
|
337
|
+
t && u(
|
|
338
|
+
(e) => e.map(
|
|
339
|
+
(r) => r.id === t ? { ...r, wasInterrupted: !0 } : r
|
|
340
|
+
)
|
|
341
|
+
), c.current(), c.current = null;
|
|
342
|
+
}, [u]), mt = i(() => {
|
|
343
|
+
c.current?.(), c.current = null, y.current = null, S(!1), M(void 0), T.current = 1, P.current = !1, F(!1), R(!1);
|
|
344
|
+
const t = k.current, e = C.current, r = h ?? vt({ route: t, storeId: a });
|
|
345
|
+
C.current = r, tt(r), f.current = [], B([]), v(void 0), l && e && Ct(t, e), N?.(r);
|
|
346
|
+
}, [
|
|
347
|
+
N,
|
|
348
|
+
l,
|
|
349
|
+
h,
|
|
350
|
+
v,
|
|
351
|
+
a
|
|
352
|
+
]), Rt = gt(
|
|
353
|
+
() => O && !D.some((t) => t.isStreaming && t.content),
|
|
354
|
+
[O, D]
|
|
355
|
+
);
|
|
356
|
+
return {
|
|
357
|
+
messages: D,
|
|
358
|
+
sessionId: st,
|
|
359
|
+
previousResponseId: ut,
|
|
360
|
+
isStreaming: O,
|
|
361
|
+
isThinking: Rt,
|
|
362
|
+
isRestoring: ot,
|
|
363
|
+
isLoadingHistory: at,
|
|
364
|
+
hasMoreHistory: $,
|
|
365
|
+
error: ct,
|
|
366
|
+
sendMessage: X,
|
|
367
|
+
regenerate: ft,
|
|
368
|
+
submitFeedback: dt,
|
|
369
|
+
loadOlderMessages: lt,
|
|
370
|
+
stopStreaming: pt,
|
|
371
|
+
startNewConversation: mt
|
|
372
|
+
};
|
|
373
|
+
};
|
|
374
|
+
export {
|
|
375
|
+
Nt as useCopilotStreamChat
|
|
376
|
+
};
|
|
377
|
+
//# sourceMappingURL=useCopilotStreamChat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCopilotStreamChat.js","sources":["../../../../src/components/bik-copilot/useCopilotStreamChat.ts"],"sourcesContent":["import { useCallback, useEffect, useMemo, useRef, useState } from 'react';\nimport { COPILOT_REGENERATE_PROMPT } from './copilotRegenerate';\nimport {\n\tCopilotFeedbackDetail,\n\tCopilotFeedbackVote,\n\tCopilotMessage,\n\tCopilotStreamEvent,\n\tUseCopilotStreamChatOptions,\n\tUseCopilotStreamChatResult,\n} from './model';\nimport {\n\tdeleteCopilotSession,\n\tloadCopilotSession,\n\tpersistCopilotTurn,\n\tpruneCopilotSessions,\n} from './services/copilotDb';\nimport { sendCopilotFeedback } from './services/copilotFeedbackClient';\nimport {\n\tCOPILOT_HISTORY_PAGE_SIZE,\n\tfetchCopilotMessages,\n} from './services/copilotMessagesClient';\nimport {\n\tgetCopilotSessionIdForRoute,\n\tresetCopilotSessionForRoute,\n\tresolveCopilotSessionIdForRoute,\n} from './services/copilotPageSession';\nimport {\n\treadCopilotEventContent,\n\tstreamCopilotChat,\n} from './services/copilotStreamClient';\nimport {\n\tDEFAULT_COPILOT_AGENT,\n\tDEFAULT_COPILOT_ROUTE,\n} from './useCopilotIcebreakers';\n\nconst GENERIC_ERROR = 'Something went wrong. Please try again.';\n\n/**\n * Runs a merchant copilot conversation against the streaming\n * `/bik-ai/copilot-chatbot` endpoint.\n *\n * Owns three things the endpoint's contract requires:\n *\n * 1. **The session id.** One per route. Navigating away and back to the same\n * route continues that conversation, and so does a refresh: the ids live in\n * memory but are recovered from the stored records, which is what keeps them\n * lined up with the history the backend keys by them (see\n * `copilotPageSession`).\n * 2. **`previousResponseId`.** Absent on the first turn; from then on it is the\n * `responseId` of the last `TEXT` event, which is what makes OpenAI replay the\n * earlier turns server side. A turn that breaks halfway returns no\n * `responseId`, and the backend means that — the next turn starts fresh.\n * 3. **The transcript**, stored in IndexedDB under `{sessionId}:{route}` and\n * written every turn, so leaving the page mid-conversation loses nothing.\n */\nexport const useCopilotStreamChat = ({\n\tstoreId,\n\tagent = DEFAULT_COPILOT_AGENT,\n\troute = DEFAULT_COPILOT_ROUTE,\n\tmessagesPath,\n\tfeedbackPath,\n\tuser,\n\tloadHistory = true,\n\tsystemPrompt,\n\tsessionId: pinnedSessionId,\n\tpersist = true,\n\tonError,\n\tonResponseComplete,\n\tonSessionIdChange,\n\tbaseUrl,\n\tendpointPath,\n\theaders,\n\tauthToken,\n\tgetAuthToken,\n\tfetchImpl,\n}: UseCopilotStreamChatOptions): UseCopilotStreamChatResult => {\n\tconst [messages, setMessages] = useState<CopilotMessage[]>([]);\n\tconst [sessionId, setSessionId] = useState<string | null>(\n\t\tpinnedSessionId ?? null,\n\t);\n\tconst [isStreaming, setIsStreaming] = useState(false);\n\tconst [isRestoring, setIsRestoring] = useState(true);\n\tconst [error, setError] = useState<string | undefined>();\n\tconst [previousResponseId, setPreviousResponseId] = useState<\n\t\tstring | undefined\n\t>();\n\tconst [isLoadingHistory, setIsLoadingHistory] = useState(false);\n\tconst [hasMoreHistory, setHasMoreHistory] = useState(false);\n\n\t// Async stream handlers run long after the render that created them, so the\n\t// live values live in refs and state is only the render mirror.\n\tconst messagesRef = useRef<CopilotMessage[]>([]);\n\tconst previousResponseIdRef = useRef<string | undefined>(undefined);\n\tconst streamingIdRef = useRef<string | null>(null);\n\tconst abortRef = useRef<(() => void) | null>(null);\n\tconst sessionIdRef = useRef<string | null>(pinnedSessionId ?? null);\n\t// The active route, for the async handlers that write the transcript.\n\tconst routeRef = useRef<string>(route);\n\trouteRef.current = route;\n\t// Next page of stored history to ask for. Starts at 2 when the local cache\n\t// already holds page 1's worth, which is the steady state.\n\tconst nextHistoryPageRef = useRef(1);\n\tconst isLoadingHistoryRef = useRef(false);\n\n\tconst applyMessages = useCallback(\n\t\t(updater: (previous: CopilotMessage[]) => CopilotMessage[]) => {\n\t\t\tconst next = updater(messagesRef.current);\n\t\t\tmessagesRef.current = next;\n\t\t\tsetMessages(next);\n\t\t},\n\t\t[],\n\t);\n\n\tconst setThreadResponseId = useCallback((responseId: string | undefined) => {\n\t\tpreviousResponseIdRef.current = responseId;\n\t\tsetPreviousResponseId(responseId);\n\t}, []);\n\n\tconst save = useCallback(() => {\n\t\tconst id = sessionIdRef.current;\n\t\tif (!persist || !id) {\n\t\t\treturn;\n\t\t}\n\t\tvoid persistCopilotTurn({\n\t\t\troute: routeRef.current,\n\t\t\tsessionId: id,\n\t\t\tmessages: messagesRef.current,\n\t\t\tpreviousResponseId: previousResponseIdRef.current,\n\t\t\tstoreId,\n\t\t\tagent,\n\t\t});\n\t}, [agent, persist, storeId]);\n\n\t// Transport config for the async readers below. Held in a ref rather than in\n\t// the effect's deps: a host that passes an inline `headers` object would\n\t// otherwise re-run restore on every render and wipe the transcript.\n\tconst transportRef = useRef({\n\t\tbaseUrl,\n\t\tmessagesPath,\n\t\tfeedbackPath,\n\t\theaders,\n\t\tauthToken,\n\t\tgetAuthToken,\n\t\tfetchImpl,\n\t\tstoreId,\n\t\tloadHistory,\n\t\tuser,\n\t});\n\ttransportRef.current = {\n\t\tbaseUrl,\n\t\tmessagesPath,\n\t\tfeedbackPath,\n\t\theaders,\n\t\tauthToken,\n\t\tgetAuthToken,\n\t\tfetchImpl,\n\t\tstoreId,\n\t\tloadHistory,\n\t\tuser,\n\t};\n\n\tconst readHistoryPage = useCallback(async (page: number) => {\n\t\tconst transport = transportRef.current;\n\t\tconst id = sessionIdRef.current;\n\t\tif (!transport.loadHistory || !id) {\n\t\t\treturn null;\n\t\t}\n\t\treturn fetchCopilotMessages({\n\t\t\tbaseUrl: transport.baseUrl,\n\t\t\tendpointPath: transport.messagesPath,\n\t\t\theaders: transport.headers,\n\t\t\tauthToken: transport.authToken,\n\t\t\tgetAuthToken: transport.getAuthToken,\n\t\t\tfetchImpl: transport.fetchImpl,\n\t\t\tstoreId: transport.storeId,\n\t\t\tsessionId: id,\n\t\t\tpage,\n\t\t\tpageSize: COPILOT_HISTORY_PAGE_SIZE,\n\t\t});\n\t}, []);\n\n\t// Resolve the conversation for this route and read it back from IndexedDB.\n\t// Runs again when `route` changes, because the route is half of a\n\t// conversation's identity — a different page is a different chat.\n\tuseEffect(() => {\n\t\tlet cancelled = false;\n\n\t\tconst restore = async () => {\n\t\t\t// Chunks from the previous route's turn must not land in this one.\n\t\t\tabortRef.current?.();\n\t\t\tabortRef.current = null;\n\t\t\tstreamingIdRef.current = null;\n\t\t\tsetIsStreaming(false);\n\t\t\tsetError(undefined);\n\t\t\tsetIsRestoring(true);\n\t\t\tsetHasMoreHistory(false);\n\t\t\tisLoadingHistoryRef.current = false;\n\t\t\tsetIsLoadingHistory(false);\n\n\t\t\tconst id =\n\t\t\t\tpinnedSessionId ??\n\t\t\t\t(persist\n\t\t\t\t\t? await resolveCopilotSessionIdForRoute({ route, storeId })\n\t\t\t\t\t: getCopilotSessionIdForRoute({ route, storeId }));\n\t\t\tconst record = persist ? await loadCopilotSession(route, id) : null;\n\t\t\tif (cancelled) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsessionIdRef.current = id;\n\t\t\tsetSessionId(id);\n\t\t\tmessagesRef.current = record?.messages ?? [];\n\t\t\tsetMessages(messagesRef.current);\n\t\t\tsetThreadResponseId(record?.previousResponseId);\n\t\t\tsetIsRestoring(false);\n\t\t\tonSessionIdChange?.(id);\n\n\t\t\t// The cache holds at most one page, so what is on screen already *is*\n\t\t\t// page 1 and the reader's first scroll-up wants page 2.\n\t\t\tif (messagesRef.current.length > 0) {\n\t\t\t\tnextHistoryPageRef.current = 2;\n\t\t\t\tsetHasMoreHistory(transportRef.current.loadHistory);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Nothing cached and nothing to scroll, so page 1 has to be fetched for\n\t\t\t// the history to be reachable at all — but only for an id the host\n\t\t\t// pinned. An id this library resolved came out of the local records, so\n\t\t\t// an empty cache for it means an empty conversation, and a freshly\n\t\t\t// minted one has nothing behind it by definition. Asking anyway would\n\t\t\t// put a pointless request on the start of every new conversation.\n\t\t\tif (!pinnedSessionId) {\n\t\t\t\tnextHistoryPageRef.current = 1;\n\t\t\t\tsetHasMoreHistory(false);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tnextHistoryPageRef.current = 1;\n\t\t\ttry {\n\t\t\t\tconst firstPage = await readHistoryPage(1);\n\t\t\t\tif (cancelled || !firstPage) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tnextHistoryPageRef.current = 2;\n\t\t\t\tapplyMessages(() => firstPage.messages);\n\t\t\t\tsetHasMoreHistory(firstPage.hasMore);\n\t\t\t} catch {\n\t\t\t\t// A session the backend has never heard of is the normal case for a\n\t\t\t\t// host-pinned id on its first use; the empty state covers it.\n\t\t\t\tsetHasMoreHistory(false);\n\t\t\t}\n\t\t};\n\n\t\tvoid restore();\n\t\treturn () => {\n\t\t\tcancelled = true;\n\t\t};\n\t\t// onSessionIdChange is event-like: re-running restore because the host\n\t\t// passed a new inline callback would wipe the transcript.\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t}, [\n\t\troute,\n\t\tstoreId,\n\t\tpinnedSessionId,\n\t\tpersist,\n\t\tsetThreadResponseId,\n\t\tapplyMessages,\n\t\treadHistoryPage,\n\t]);\n\n\t/**\n\t * Prepends the page before the oldest message on screen.\n\t *\n\t * Guarded by a ref rather than the state flag because a scroll handler can\n\t * fire many times before React re-renders, and each of those would otherwise\n\t * start its own request for the same page.\n\t */\n\tconst loadOlderMessages = useCallback(() => {\n\t\tif (isLoadingHistoryRef.current || !hasMoreHistory) {\n\t\t\treturn;\n\t\t}\n\t\tisLoadingHistoryRef.current = true;\n\t\tsetIsLoadingHistory(true);\n\n\t\tconst page = nextHistoryPageRef.current;\n\t\tvoid (async () => {\n\t\t\ttry {\n\t\t\t\tconst older = await readHistoryPage(page);\n\t\t\t\tif (!older) {\n\t\t\t\t\tsetHasMoreHistory(false);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tnextHistoryPageRef.current = page + 1;\n\t\t\t\tsetHasMoreHistory(older.hasMore);\n\t\t\t\tapplyMessages((previous) => {\n\t\t\t\t\tconst known = new Set(\n\t\t\t\t\t\tprevious.flatMap((message) =>\n\t\t\t\t\t\t\tmessage.messageId\n\t\t\t\t\t\t\t\t? [message.id, message.messageId]\n\t\t\t\t\t\t\t\t: [message.id],\n\t\t\t\t\t\t),\n\t\t\t\t\t);\n\t\t\t\t\tconst fresh = older.messages.filter(\n\t\t\t\t\t\t(message) => !known.has(message.id),\n\t\t\t\t\t);\n\t\t\t\t\treturn fresh.length > 0 ? [...fresh, ...previous] : previous;\n\t\t\t\t});\n\t\t\t} catch (historyError) {\n\t\t\t\t// Left silent in the transcript on purpose: failing to reach further\n\t\t\t\t// back is not a failure of the conversation on screen. Another\n\t\t\t\t// scroll-up retries the same page.\n\t\t\t\tonError?.(\n\t\t\t\t\thistoryError instanceof Error\n\t\t\t\t\t\t? historyError\n\t\t\t\t\t\t: new Error(String(historyError)),\n\t\t\t\t);\n\t\t\t} finally {\n\t\t\t\tisLoadingHistoryRef.current = false;\n\t\t\t\tsetIsLoadingHistory(false);\n\t\t\t}\n\t\t})();\n\t}, [applyMessages, hasMoreHistory, onError, readHistoryPage]);\n\n\t// Records pile up one per route per page load otherwise, since every refresh\n\t// writes under a new key.\n\tuseEffect(() => {\n\t\tif (persist) {\n\t\t\tvoid pruneCopilotSessions();\n\t\t}\n\t}, [persist]);\n\n\t// Abort an in-flight turn if the copilot unmounts.\n\tuseEffect(() => () => abortRef.current?.(), []);\n\n\tconst finishStreamingBubble = useCallback(\n\t\t(mutate?: (message: CopilotMessage) => CopilotMessage) => {\n\t\t\tconst streamingId = streamingIdRef.current;\n\t\t\tstreamingIdRef.current = null;\n\t\t\tif (!streamingId) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tapplyMessages((previous) =>\n\t\t\t\tprevious\n\t\t\t\t\t.map((message) => {\n\t\t\t\t\t\tif (message.id !== streamingId) {\n\t\t\t\t\t\t\treturn message;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst settled: CopilotMessage = {\n\t\t\t\t\t\t\t...message,\n\t\t\t\t\t\t\tisStreaming: false,\n\t\t\t\t\t\t};\n\t\t\t\t\t\treturn mutate ? mutate(settled) : settled;\n\t\t\t\t\t})\n\t\t\t\t\t// An assistant bubble that never received text would render as an\n\t\t\t\t\t// empty balloon; the error banner carries the information instead.\n\t\t\t\t\t// A stopped turn keeps its bubble either way — the interrupted note\n\t\t\t\t\t// is the only trace a turn the reader cut short ever happened.\n\t\t\t\t\t.filter(\n\t\t\t\t\t\t(message) =>\n\t\t\t\t\t\t\tmessage.id !== streamingId ||\n\t\t\t\t\t\t\tmessage.content.length > 0 ||\n\t\t\t\t\t\t\tmessage.wasInterrupted === true,\n\t\t\t\t\t),\n\t\t\t);\n\t\t},\n\t\t[applyMessages],\n\t);\n\n\tconst handleEvent = useCallback(\n\t\t(event: CopilotStreamEvent) => {\n\t\t\tconst streamingId = streamingIdRef.current;\n\n\t\t\tif (event.type === 'STREAM') {\n\t\t\t\tconst chunk = readCopilotEventContent(event).text;\n\t\t\t\tif (!streamingId || !chunk) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tapplyMessages((previous) =>\n\t\t\t\t\tprevious.map((message) =>\n\t\t\t\t\t\tmessage.id === streamingId\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t...message,\n\t\t\t\t\t\t\t\t\tcontent: message.content + chunk,\n\t\t\t\t\t\t\t\t\tmessageId: event.messageId ?? message.messageId,\n\t\t\t\t\t\t\t }\n\t\t\t\t\t\t\t: message,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (event.type === 'TEXT') {\n\t\t\t\t// TEXT carries the whole message, so it replaces the accumulated\n\t\t\t\t// chunks rather than appending — that keeps the bubble correct even if\n\t\t\t\t// a STREAM frame was dropped or duplicated. `responseId` rides inside\n\t\t\t\t// the content payload (as `lastResponseId`).\n\t\t\t\tconst {\n\t\t\t\t\ttext: fullText,\n\t\t\t\t\tfollowUps,\n\t\t\t\t\ttoolResults,\n\t\t\t\t\tresponseId,\n\t\t\t\t} = readCopilotEventContent(event);\n\t\t\t\tfinishStreamingBubble((message) => ({\n\t\t\t\t\t...message,\n\t\t\t\t\tcontent: fullText,\n\t\t\t\t\tmessageId: event.messageId ?? message.messageId,\n\t\t\t\t\t...(followUps ? { followUps } : {}),\n\t\t\t\t\t// Kept on the message, not just handled in passing, so the\n\t\t\t\t\t// action button is still there after a reload.\n\t\t\t\t\t...(toolResults ? { toolResults } : {}),\n\t\t\t\t}));\n\t\t\t\tsetThreadResponseId(responseId);\n\t\t\t\tsave();\n\t\t\t\tonResponseComplete?.({\n\t\t\t\t\ttext: fullText,\n\t\t\t\t\tresponseId,\n\t\t\t\t\tfollowUps,\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (event.type === 'ERROR') {\n\t\t\t\tsetError(readCopilotEventContent(event).text || GENERIC_ERROR);\n\t\t\t\tfinishStreamingBubble();\n\t\t\t\tsave();\n\t\t\t}\n\n\t\t\t// Unknown types (TOOL_STATUS is declared server side but not emitted)\n\t\t\t// are ignored so a new event type cannot break an existing client.\n\t\t},\n\t\t[\n\t\t\tapplyMessages,\n\t\t\tfinishStreamingBubble,\n\t\t\tonResponseComplete,\n\t\t\tsave,\n\t\t\tsetThreadResponseId,\n\t\t],\n\t);\n\n\tconst sendMessage = useCallback(\n\t\t(message: string) => {\n\t\t\tconst userPrompt = message.trim();\n\t\t\tconst id = sessionIdRef.current;\n\t\t\tif (!userPrompt || !id || isStreaming) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsetError(undefined);\n\t\t\tconst now = Date.now();\n\t\t\tconst assistantBubbleId = `copilot-a-${now}`;\n\t\t\tstreamingIdRef.current = assistantBubbleId;\n\n\t\t\tapplyMessages((previous) => [\n\t\t\t\t...previous,\n\t\t\t\t{\n\t\t\t\t\tid: `copilot-u-${now}`,\n\t\t\t\t\trole: 'user',\n\t\t\t\t\tcontent: userPrompt,\n\t\t\t\t\ttimestamp: now,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tid: assistantBubbleId,\n\t\t\t\t\trole: 'assistant',\n\t\t\t\t\tcontent: '',\n\t\t\t\t\ttimestamp: now + 1,\n\t\t\t\t\tisStreaming: true,\n\t\t\t\t},\n\t\t\t]);\n\t\t\tsetIsStreaming(true);\n\t\t\tsave();\n\n\t\t\tabortRef.current = streamCopilotChat(\n\t\t\t\t{\n\t\t\t\t\tbaseUrl,\n\t\t\t\t\tendpointPath,\n\t\t\t\t\theaders,\n\t\t\t\t\tauthToken,\n\t\t\t\t\tgetAuthToken,\n\t\t\t\t\tfetchImpl,\n\t\t\t\t\tstoreId,\n\t\t\t\t\tagent,\n\t\t\t\t\troute,\n\t\t\t\t\tuserPrompt,\n\t\t\t\t\tsessionId: id,\n\t\t\t\t\tpreviousResponseId: previousResponseIdRef.current,\n\t\t\t\t\tsystemPrompt,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tonEvent: handleEvent,\n\t\t\t\t\tonComplete: () => {\n\t\t\t\t\t\t// STOP closes the stream without a frame, so this is where a turn\n\t\t\t\t\t\t// ends. TEXT has usually already settled the bubble.\n\t\t\t\t\t\tfinishStreamingBubble();\n\t\t\t\t\t\tsetIsStreaming(false);\n\t\t\t\t\t\tabortRef.current = null;\n\t\t\t\t\t\tsave();\n\t\t\t\t\t},\n\t\t\t\t\tonError: (streamError) => {\n\t\t\t\t\t\tsetError(streamError.message || GENERIC_ERROR);\n\t\t\t\t\t\tfinishStreamingBubble();\n\t\t\t\t\t\tsetIsStreaming(false);\n\t\t\t\t\t\tabortRef.current = null;\n\t\t\t\t\t\tsave();\n\t\t\t\t\t\tonError?.(streamError);\n\t\t\t\t\t},\n\t\t\t\t\tonAbort: () => {\n\t\t\t\t\t\tfinishStreamingBubble();\n\t\t\t\t\t\tsetIsStreaming(false);\n\t\t\t\t\t\tabortRef.current = null;\n\t\t\t\t\t\tsave();\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t);\n\t\t},\n\t\t[\n\t\t\tapplyMessages,\n\t\t\tauthToken,\n\t\t\tbaseUrl,\n\t\t\tendpointPath,\n\t\t\tfetchImpl,\n\t\t\tagent,\n\t\t\tfinishStreamingBubble,\n\t\t\tgetAuthToken,\n\t\t\thandleEvent,\n\t\t\theaders,\n\t\t\tisStreaming,\n\t\t\tonError,\n\t\t\troute,\n\t\t\tsave,\n\t\t\tstoreId,\n\t\t\tsystemPrompt,\n\t\t],\n\t);\n\n\tconst regenerate = useCallback(() => {\n\t\tconst hasReply = messagesRef.current.some(\n\t\t\t(message) => message.role === 'assistant' && message.content.length > 0,\n\t\t);\n\t\tif (!hasReply) {\n\t\t\treturn;\n\t\t}\n\t\tsendMessage(COPILOT_REGENERATE_PROMPT);\n\t}, [sendMessage]);\n\n\t/**\n\t * Records a thumbs-up or thumbs-down on one reply.\n\t *\n\t * Fire-and-forget: a rating is a side note to the conversation, so a failed\n\t * post is reported through `onError` and otherwise ignored -- the thumb stays\n\t * lit either way rather than flickering back on a network hiccup.\n\t *\n\t * The reply's own text and the route ride along, so whoever reads the feedback\n\t * sees what was rated without looking the message up.\n\t */\n\tconst submitFeedback = useCallback(\n\t\t(\n\t\t\tmessageId: string,\n\t\t\tfeedback: CopilotFeedbackVote,\n\t\t\tdetail?: CopilotFeedbackDetail,\n\t\t) => {\n\t\t\tconst transport = transportRef.current;\n\t\t\tconst id = sessionIdRef.current;\n\t\t\tif (!id) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t// A live turn's bubble carries only a local id until the final event\n\t\t\t// lands, so match on either and let the backend see whichever it gets.\n\t\t\tconst rated = messagesRef.current.find(\n\t\t\t\t(message) => (message.messageId ?? message.id) === messageId,\n\t\t\t);\n\t\t\tvoid sendCopilotFeedback({\n\t\t\t\tbaseUrl: transport.baseUrl,\n\t\t\t\tendpointPath: transport.feedbackPath,\n\t\t\t\theaders: transport.headers,\n\t\t\t\tauthToken: transport.authToken,\n\t\t\t\tgetAuthToken: transport.getAuthToken,\n\t\t\t\tfetchImpl: transport.fetchImpl,\n\t\t\t\tstoreId: transport.storeId,\n\t\t\t\tsessionId: id,\n\t\t\t\tmessageId,\n\t\t\t\tfeedback,\n\t\t\t\troute: routeRef.current,\n\t\t\t\treply: rated?.content,\n\t\t\t\tuser: transport.user,\n\t\t\t\treasons: detail?.reasons,\n\t\t\t\tcomment: detail?.comment,\n\t\t\t}).catch((feedbackError) => {\n\t\t\t\tonError?.(\n\t\t\t\t\tfeedbackError instanceof Error\n\t\t\t\t\t\t? feedbackError\n\t\t\t\t\t\t: new Error(String(feedbackError)),\n\t\t\t\t);\n\t\t\t});\n\t\t},\n\t\t[onError],\n\t);\n\n\tconst stopStreaming = useCallback(() => {\n\t\tif (!abortRef.current) {\n\t\t\treturn;\n\t\t}\n\t\t// Flagged here rather than in `onAbort`, because an abort that lands between\n\t\t// frames ends the stream through `onComplete` instead. Whichever fires, the\n\t\t// reader stopped this turn, and whatever text had arrived stays.\n\t\tconst streamingId = streamingIdRef.current;\n\t\tif (streamingId) {\n\t\t\tapplyMessages((previous) =>\n\t\t\t\tprevious.map((message) =>\n\t\t\t\t\tmessage.id === streamingId\n\t\t\t\t\t\t? { ...message, wasInterrupted: true }\n\t\t\t\t\t\t: message,\n\t\t\t\t),\n\t\t\t);\n\t\t}\n\t\tabortRef.current();\n\t\tabortRef.current = null;\n\t}, [applyMessages]);\n\n\tconst startNewConversation = useCallback(() => {\n\t\tabortRef.current?.();\n\t\tabortRef.current = null;\n\t\tstreamingIdRef.current = null;\n\t\tsetIsStreaming(false);\n\t\tsetError(undefined);\n\t\t// A new conversation has no history behind it; the old session's pages\n\t\t// belong to the old session id.\n\t\tnextHistoryPageRef.current = 1;\n\t\tisLoadingHistoryRef.current = false;\n\t\tsetIsLoadingHistory(false);\n\t\tsetHasMoreHistory(false);\n\n\t\tconst activeRoute = routeRef.current;\n\t\t// The conversation being cleared, so its stored transcript can go with it.\n\t\tconst clearedId = sessionIdRef.current;\n\t\t// A host-pinned id is the host's to change, so only clear its transcript.\n\t\tconst nextId =\n\t\t\tpinnedSessionId ??\n\t\t\tresetCopilotSessionForRoute({ route: activeRoute, storeId });\n\t\tsessionIdRef.current = nextId;\n\t\tsetSessionId(nextId);\n\t\tmessagesRef.current = [];\n\t\tsetMessages([]);\n\t\tsetThreadResponseId(undefined);\n\n\t\t// \"Refresh chat\" wipes the record, not just the screen: the cleared\n\t\t// conversation is deleted from IndexedDB so reopening the tab starts fresh\n\t\t// instead of resuming it. Nothing is written for the new session here — its\n\t\t// first record is stamped when the first turn is sent, which is also what\n\t\t// keeps an untouched refresh from leaving an empty record behind.\n\t\tif (persist && clearedId) {\n\t\t\tvoid deleteCopilotSession(activeRoute, clearedId);\n\t\t}\n\t\tonSessionIdChange?.(nextId);\n\t}, [\n\t\tonSessionIdChange,\n\t\tpersist,\n\t\tpinnedSessionId,\n\t\tsetThreadResponseId,\n\t\tstoreId,\n\t]);\n\n\t// The thinking bubble is only for the wait before the first token; once text\n\t// starts arriving the bubble itself shows progress.\n\tconst isThinking = useMemo(\n\t\t() =>\n\t\t\tisStreaming &&\n\t\t\t!messages.some((message) => message.isStreaming && message.content),\n\t\t[isStreaming, messages],\n\t);\n\n\treturn {\n\t\tmessages,\n\t\tsessionId,\n\t\tpreviousResponseId,\n\t\tisStreaming,\n\t\tisThinking,\n\t\tisRestoring,\n\t\tisLoadingHistory,\n\t\thasMoreHistory,\n\t\terror,\n\t\tsendMessage,\n\t\tregenerate,\n\t\tsubmitFeedback,\n\t\tloadOlderMessages,\n\t\tstopStreaming,\n\t\tstartNewConversation,\n\t};\n};\n"],"names":["GENERIC_ERROR","useCopilotStreamChat","storeId","agent","DEFAULT_COPILOT_AGENT","route","DEFAULT_COPILOT_ROUTE","messagesPath","feedbackPath","user","loadHistory","systemPrompt","pinnedSessionId","persist","onError","onResponseComplete","onSessionIdChange","baseUrl","endpointPath","headers","authToken","getAuthToken","fetchImpl","messages","setMessages","useState","sessionId","setSessionId","isStreaming","setIsStreaming","isRestoring","setIsRestoring","error","setError","previousResponseId","setPreviousResponseId","isLoadingHistory","setIsLoadingHistory","hasMoreHistory","setHasMoreHistory","messagesRef","useRef","previousResponseIdRef","streamingIdRef","abortRef","sessionIdRef","routeRef","nextHistoryPageRef","isLoadingHistoryRef","applyMessages","useCallback","updater","next","setThreadResponseId","responseId","save","id","persistCopilotTurn","transportRef","readHistoryPage","page","transport","fetchCopilotMessages","COPILOT_HISTORY_PAGE_SIZE","useEffect","cancelled","resolveCopilotSessionIdForRoute","getCopilotSessionIdForRoute","record","loadCopilotSession","firstPage","loadOlderMessages","older","previous","known","message","fresh","historyError","pruneCopilotSessions","finishStreamingBubble","mutate","streamingId","settled","handleEvent","event","chunk","readCopilotEventContent","fullText","followUps","toolResults","sendMessage","userPrompt","now","assistantBubbleId","streamCopilotChat","streamError","regenerate","COPILOT_REGENERATE_PROMPT","submitFeedback","messageId","feedback","detail","rated","sendCopilotFeedback","feedbackError","stopStreaming","startNewConversation","activeRoute","clearedId","nextId","resetCopilotSessionForRoute","deleteCopilotSession","isThinking","useMemo"],"mappings":";;;;;;;;AAmCA,MAAMA,KAAgB,2CAoBTC,KAAuB,CAAC;AAAA,EACpC,SAAAC;AAAA,EACA,OAAAC,IAAQC;AAAA,EACR,OAAAC,IAAQC;AAAA,EACR,cAAAC;AAAA,EACA,cAAAC;AAAA,EACA,MAAAC;AAAA,EACA,aAAAC,IAAc;AAAA,EACd,cAAAC;AAAA,EACA,WAAWC;AAAA,EACX,SAAAC,IAAU;AAAA,EACV,SAAAC;AAAA,EACA,oBAAAC;AAAA,EACA,mBAAAC;AAAA,EACA,SAAAC;AAAA,EACA,cAAAC;AAAA,EACA,SAAAC;AAAA,EACA,WAAAC;AAAA,EACA,cAAAC;AAAA,EACA,WAAAC;AACD,MAA+D;AAC9D,QAAM,CAACC,GAAU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IAClB,MACCrF,KACA,CAACL,EAAS,KAAK,CAACoD,MAAYA,EAAQ,eAAeA,EAAQ,OAAO;AAAA,IACnE,CAAC/C,GAAaL,CAAQ;AAAA,EAAA;AAGvB,SAAO;AAAA,IACN,UAAAA;AAAA,IACA,WAAAG;AAAA,IACA,oBAAAQ;AAAA,IACA,aAAAN;AAAA,IACA,YAAAoF;AAAA,IACA,aAAAlF;AAAA,IACA,kBAAAM;AAAA,IACA,gBAAAE;AAAA,IACA,OAAAN;AAAA,IACA,aAAA0D;AAAA,IACA,YAAAM;AAAA,IACA,gBAAAE;AAAA,IACA,mBAAA3B;AAAA,IACA,eAAAkC;AAAA,IACA,sBAAAC;AAAA,EAAA;AAEF;"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { UseDraggableFloaterOptions, UseDraggableFloaterResult } from './model';
|
|
2
|
+
/**
|
|
3
|
+
* Drives a fixed-position element the user can drag anywhere in the viewport.
|
|
4
|
+
*
|
|
5
|
+
* The hook owns the element's coordinates, keeps them inside the viewport as the
|
|
6
|
+
* window resizes, and separates a tap from a drag so the same element can both
|
|
7
|
+
* be dragged and clicked.
|
|
8
|
+
*/
|
|
9
|
+
export declare const useDraggableFloater: ({ size, corner, offset, enabled, snapToEdge, initialPosition, onPositionChange, }: UseDraggableFloaterOptions) => UseDraggableFloaterResult;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { useState as P, useRef as Y, useEffect as H, useCallback as m, useMemo as _ } from "react";
|
|
2
|
+
const b = 4, c = 8, l = () => typeof window > "u" ? { width: 0, height: 0 } : { width: window.innerWidth, height: window.innerHeight }, p = (e, i, n) => {
|
|
3
|
+
if (!n.width || !n.height)
|
|
4
|
+
return e;
|
|
5
|
+
const a = Math.max(
|
|
6
|
+
c,
|
|
7
|
+
n.width - i - c
|
|
8
|
+
), s = Math.max(
|
|
9
|
+
c,
|
|
10
|
+
n.height - i - c
|
|
11
|
+
);
|
|
12
|
+
return {
|
|
13
|
+
x: Math.min(Math.max(e.x, c), a),
|
|
14
|
+
y: Math.min(Math.max(e.y, c), s)
|
|
15
|
+
};
|
|
16
|
+
}, k = (e, i, n, a) => {
|
|
17
|
+
const s = a.width - i - n, h = a.height - i - n;
|
|
18
|
+
switch (e) {
|
|
19
|
+
case "top-left":
|
|
20
|
+
return { x: n, y: n };
|
|
21
|
+
case "top-right":
|
|
22
|
+
return { x: s, y: n };
|
|
23
|
+
case "bottom-left":
|
|
24
|
+
return { x: n, y: h };
|
|
25
|
+
default:
|
|
26
|
+
return { x: s, y: h };
|
|
27
|
+
}
|
|
28
|
+
}, L = ({
|
|
29
|
+
size: e,
|
|
30
|
+
corner: i,
|
|
31
|
+
offset: n,
|
|
32
|
+
enabled: a,
|
|
33
|
+
snapToEdge: s,
|
|
34
|
+
initialPosition: h,
|
|
35
|
+
onPositionChange: M
|
|
36
|
+
}) => {
|
|
37
|
+
const [C, D] = P(l), [u, x] = P(() => {
|
|
38
|
+
const t = l();
|
|
39
|
+
return p(
|
|
40
|
+
h ?? k(i, e, n, t),
|
|
41
|
+
e,
|
|
42
|
+
t
|
|
43
|
+
);
|
|
44
|
+
}), [V, y] = P(!1), g = Y(null), w = Y(!1);
|
|
45
|
+
H(() => {
|
|
46
|
+
const t = () => {
|
|
47
|
+
const r = l();
|
|
48
|
+
D(r), x((o) => p(o, e, r));
|
|
49
|
+
};
|
|
50
|
+
return window.addEventListener("resize", t), () => window.removeEventListener("resize", t);
|
|
51
|
+
}, [e]);
|
|
52
|
+
const X = m(
|
|
53
|
+
(t) => {
|
|
54
|
+
w.current = !1, !(!a || t.button !== 0) && (t.currentTarget.setPointerCapture(t.pointerId), g.current = {
|
|
55
|
+
pointerId: t.pointerId,
|
|
56
|
+
startX: t.clientX,
|
|
57
|
+
startY: t.clientY,
|
|
58
|
+
originX: u.x,
|
|
59
|
+
originY: u.y,
|
|
60
|
+
moved: !1,
|
|
61
|
+
latest: u
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
[a, u]
|
|
65
|
+
), f = m(
|
|
66
|
+
(t) => {
|
|
67
|
+
const r = g.current;
|
|
68
|
+
if (!r || r.pointerId !== t.pointerId)
|
|
69
|
+
return;
|
|
70
|
+
const o = t.clientX - r.startX, d = t.clientY - r.startY;
|
|
71
|
+
if (!r.moved) {
|
|
72
|
+
if (Math.abs(o) < b && Math.abs(d) < b)
|
|
73
|
+
return;
|
|
74
|
+
r.moved = !0, y(!0);
|
|
75
|
+
}
|
|
76
|
+
const R = p(
|
|
77
|
+
{ x: r.originX + o, y: r.originY + d },
|
|
78
|
+
e,
|
|
79
|
+
l()
|
|
80
|
+
);
|
|
81
|
+
r.latest = R, x(R);
|
|
82
|
+
},
|
|
83
|
+
[e]
|
|
84
|
+
), I = m(
|
|
85
|
+
(t) => {
|
|
86
|
+
const r = g.current;
|
|
87
|
+
if (!r || r.pointerId !== t.pointerId || (g.current = null, t.currentTarget.hasPointerCapture(t.pointerId) && t.currentTarget.releasePointerCapture(t.pointerId), !r.moved))
|
|
88
|
+
return;
|
|
89
|
+
w.current = !0, y(!1);
|
|
90
|
+
const o = l(), d = s ? p(
|
|
91
|
+
{
|
|
92
|
+
x: r.latest.x + e / 2 < o.width / 2 ? n : o.width - e - n,
|
|
93
|
+
y: r.latest.y
|
|
94
|
+
},
|
|
95
|
+
e,
|
|
96
|
+
o
|
|
97
|
+
) : r.latest;
|
|
98
|
+
x(d), M?.(d);
|
|
99
|
+
},
|
|
100
|
+
[n, M, e, s]
|
|
101
|
+
), E = m(() => {
|
|
102
|
+
const t = w.current;
|
|
103
|
+
return w.current = !1, t;
|
|
104
|
+
}, []), T = _(
|
|
105
|
+
() => ({
|
|
106
|
+
onPointerDown: X,
|
|
107
|
+
onPointerMove: f,
|
|
108
|
+
onPointerUp: I,
|
|
109
|
+
onPointerCancel: I
|
|
110
|
+
}),
|
|
111
|
+
[I, X, f]
|
|
112
|
+
);
|
|
113
|
+
return { position: u, isDragging: V, viewport: C, dragHandlers: T, shouldIgnoreClick: E };
|
|
114
|
+
};
|
|
115
|
+
export {
|
|
116
|
+
L as useDraggableFloater
|
|
117
|
+
};
|
|
118
|
+
//# sourceMappingURL=useDraggableFloater.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDraggableFloater.js","sources":["../../../../src/components/bik-copilot/useDraggableFloater.ts"],"sourcesContent":["import {\n\tPointerEvent as ReactPointerEvent,\n\tuseCallback,\n\tuseEffect,\n\tuseMemo,\n\tuseRef,\n\tuseState,\n} from 'react';\nimport {\n\tCopilotBallPosition,\n\tCopilotCorner,\n\tCopilotViewport,\n\tUseDraggableFloaterOptions,\n\tUseDraggableFloaterResult,\n} from './model';\n\n/** Movement (px) a gesture must cover before it counts as a drag, not a tap. */\nconst DRAG_THRESHOLD_PX = 4;\n\n/** Smallest gap kept between the dragged element and the viewport edges. */\nconst VIEWPORT_MARGIN_PX = 8;\n\nconst getViewport = (): CopilotViewport =>\n\ttypeof window === 'undefined'\n\t\t? { width: 0, height: 0 }\n\t\t: { width: window.innerWidth, height: window.innerHeight };\n\nconst clampToViewport = (\n\tposition: CopilotBallPosition,\n\tsize: number,\n\tviewport: CopilotViewport,\n): CopilotBallPosition => {\n\tif (!viewport.width || !viewport.height) {\n\t\treturn position;\n\t}\n\tconst maxX = Math.max(\n\t\tVIEWPORT_MARGIN_PX,\n\t\tviewport.width - size - VIEWPORT_MARGIN_PX,\n\t);\n\tconst maxY = Math.max(\n\t\tVIEWPORT_MARGIN_PX,\n\t\tviewport.height - size - VIEWPORT_MARGIN_PX,\n\t);\n\treturn {\n\t\tx: Math.min(Math.max(position.x, VIEWPORT_MARGIN_PX), maxX),\n\t\ty: Math.min(Math.max(position.y, VIEWPORT_MARGIN_PX), maxY),\n\t};\n};\n\nconst getCornerPosition = (\n\tcorner: CopilotCorner,\n\tsize: number,\n\toffset: number,\n\tviewport: CopilotViewport,\n): CopilotBallPosition => {\n\tconst rightEdge = viewport.width - size - offset;\n\tconst bottomEdge = viewport.height - size - offset;\n\tswitch (corner) {\n\t\tcase 'top-left':\n\t\t\treturn { x: offset, y: offset };\n\t\tcase 'top-right':\n\t\t\treturn { x: rightEdge, y: offset };\n\t\tcase 'bottom-left':\n\t\t\treturn { x: offset, y: bottomEdge };\n\t\tcase 'bottom-right':\n\t\tdefault:\n\t\t\treturn { x: rightEdge, y: bottomEdge };\n\t}\n};\n\ninterface DragGesture {\n\tpointerId: number;\n\tstartX: number;\n\tstartY: number;\n\toriginX: number;\n\toriginY: number;\n\tmoved: boolean;\n\tlatest: CopilotBallPosition;\n}\n\n/**\n * Drives a fixed-position element the user can drag anywhere in the viewport.\n *\n * The hook owns the element's coordinates, keeps them inside the viewport as the\n * window resizes, and separates a tap from a drag so the same element can both\n * be dragged and clicked.\n */\nexport const useDraggableFloater = ({\n\tsize,\n\tcorner,\n\toffset,\n\tenabled,\n\tsnapToEdge,\n\tinitialPosition,\n\tonPositionChange,\n}: UseDraggableFloaterOptions): UseDraggableFloaterResult => {\n\tconst [viewport, setViewport] = useState<CopilotViewport>(getViewport);\n\tconst [position, setPosition] = useState<CopilotBallPosition>(() => {\n\t\tconst initialViewport = getViewport();\n\t\treturn clampToViewport(\n\t\t\tinitialPosition ??\n\t\t\t\tgetCornerPosition(corner, size, offset, initialViewport),\n\t\t\tsize,\n\t\t\tinitialViewport,\n\t\t);\n\t});\n\tconst [isDragging, setIsDragging] = useState(false);\n\n\tconst gestureRef = useRef<DragGesture | null>(null);\n\tconst ignoreNextClickRef = useRef(false);\n\n\t// Keep the element reachable when the window is resized around it.\n\tuseEffect(() => {\n\t\tconst handleResize = () => {\n\t\t\tconst nextViewport = getViewport();\n\t\t\tsetViewport(nextViewport);\n\t\t\tsetPosition((previous) => clampToViewport(previous, size, nextViewport));\n\t\t};\n\t\twindow.addEventListener('resize', handleResize);\n\t\treturn () => window.removeEventListener('resize', handleResize);\n\t}, [size]);\n\n\tconst handlePointerDown = useCallback(\n\t\t(event: ReactPointerEvent<HTMLElement>) => {\n\t\t\t// A stale flag would otherwise swallow the next legitimate click.\n\t\t\tignoreNextClickRef.current = false;\n\t\t\tif (!enabled || event.button !== 0) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tevent.currentTarget.setPointerCapture(event.pointerId);\n\t\t\tgestureRef.current = {\n\t\t\t\tpointerId: event.pointerId,\n\t\t\t\tstartX: event.clientX,\n\t\t\t\tstartY: event.clientY,\n\t\t\t\toriginX: position.x,\n\t\t\t\toriginY: position.y,\n\t\t\t\tmoved: false,\n\t\t\t\tlatest: position,\n\t\t\t};\n\t\t},\n\t\t[enabled, position],\n\t);\n\n\tconst handlePointerMove = useCallback(\n\t\t(event: ReactPointerEvent<HTMLElement>) => {\n\t\t\tconst gesture = gestureRef.current;\n\t\t\tif (!gesture || gesture.pointerId !== event.pointerId) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst deltaX = event.clientX - gesture.startX;\n\t\t\tconst deltaY = event.clientY - gesture.startY;\n\n\t\t\tif (!gesture.moved) {\n\t\t\t\tif (\n\t\t\t\t\tMath.abs(deltaX) < DRAG_THRESHOLD_PX &&\n\t\t\t\t\tMath.abs(deltaY) < DRAG_THRESHOLD_PX\n\t\t\t\t) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tgesture.moved = true;\n\t\t\t\tsetIsDragging(true);\n\t\t\t}\n\n\t\t\tconst next = clampToViewport(\n\t\t\t\t{ x: gesture.originX + deltaX, y: gesture.originY + deltaY },\n\t\t\t\tsize,\n\t\t\t\tgetViewport(),\n\t\t\t);\n\t\t\tgesture.latest = next;\n\t\t\tsetPosition(next);\n\t\t},\n\t\t[size],\n\t);\n\n\tconst endGesture = useCallback(\n\t\t(event: ReactPointerEvent<HTMLElement>) => {\n\t\t\tconst gesture = gestureRef.current;\n\t\t\tif (!gesture || gesture.pointerId !== event.pointerId) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tgestureRef.current = null;\n\t\t\tif (event.currentTarget.hasPointerCapture(event.pointerId)) {\n\t\t\t\tevent.currentTarget.releasePointerCapture(event.pointerId);\n\t\t\t}\n\t\t\tif (!gesture.moved) {\n\t\t\t\t// A tap: let the click handler run untouched.\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tignoreNextClickRef.current = true;\n\t\t\tsetIsDragging(false);\n\n\t\t\tconst currentViewport = getViewport();\n\t\t\tconst settled = snapToEdge\n\t\t\t\t? clampToViewport(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tx:\n\t\t\t\t\t\t\t\tgesture.latest.x + size / 2 < currentViewport.width / 2\n\t\t\t\t\t\t\t\t\t? offset\n\t\t\t\t\t\t\t\t\t: currentViewport.width - size - offset,\n\t\t\t\t\t\t\ty: gesture.latest.y,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tsize,\n\t\t\t\t\t\tcurrentViewport,\n\t\t\t\t )\n\t\t\t\t: gesture.latest;\n\n\t\t\tsetPosition(settled);\n\t\t\tonPositionChange?.(settled);\n\t\t},\n\t\t[offset, onPositionChange, size, snapToEdge],\n\t);\n\n\tconst shouldIgnoreClick = useCallback(() => {\n\t\tconst ignore = ignoreNextClickRef.current;\n\t\tignoreNextClickRef.current = false;\n\t\treturn ignore;\n\t}, []);\n\n\tconst dragHandlers = useMemo(\n\t\t() => ({\n\t\t\tonPointerDown: handlePointerDown,\n\t\t\tonPointerMove: handlePointerMove,\n\t\t\tonPointerUp: endGesture,\n\t\t\tonPointerCancel: endGesture,\n\t\t}),\n\t\t[endGesture, handlePointerDown, handlePointerMove],\n\t);\n\n\treturn { position, isDragging, viewport, dragHandlers, shouldIgnoreClick };\n};\n"],"names":["DRAG_THRESHOLD_PX","VIEWPORT_MARGIN_PX","getViewport","clampToViewport","position","size","viewport","maxX","maxY","getCornerPosition","corner","offset","rightEdge","bottomEdge","useDraggableFloater","enabled","snapToEdge","initialPosition","onPositionChange","setViewport","useState","setPosition","initialViewport","isDragging","setIsDragging","gestureRef","useRef","ignoreNextClickRef","useEffect","handleResize","nextViewport","previous","handlePointerDown","useCallback","event","handlePointerMove","gesture","deltaX","deltaY","next","endGesture","currentViewport","settled","shouldIgnoreClick","ignore","dragHandlers","useMemo"],"mappings":";AAiBA,MAAMA,IAAoB,GAGpBC,IAAqB,GAErBC,IAAc,MACnB,OAAO,SAAW,MACf,EAAE,OAAO,GAAG,QAAQ,EAAA,IACpB,EAAE,OAAO,OAAO,YAAY,QAAQ,OAAO,YAAA,GAEzCC,IAAkB,CACvBC,GACAC,GACAC,MACyB;AACzB,MAAI,CAACA,EAAS,SAAS,CAACA,EAAS;AAChC,WAAOF;AAER,QAAMG,IAAO,KAAK;AAAA,IACjBN;AAAA,IACAK,EAAS,QAAQD,IAAOJ;AAAA,EAAA,GAEnBO,IAAO,KAAK;AAAA,IACjBP;AAAA,IACAK,EAAS,SAASD,IAAOJ;AAAA,EAAA;AAE1B,SAAO;AAAA,IACN,GAAG,KAAK,IAAI,KAAK,IAAIG,EAAS,GAAGH,CAAkB,GAAGM,CAAI;AAAA,IAC1D,GAAG,KAAK,IAAI,KAAK,IAAIH,EAAS,GAAGH,CAAkB,GAAGO,CAAI;AAAA,EAAA;AAE5D,GAEMC,IAAoB,CACzBC,GACAL,GACAM,GACAL,MACyB;AACzB,QAAMM,IAAYN,EAAS,QAAQD,IAAOM,GACpCE,IAAaP,EAAS,SAASD,IAAOM;AAC5C,UAAQD,GAAA;AAAA,IACP,KAAK;AACJ,aAAO,EAAE,GAAGC,GAAQ,GAAGA,EAAA;AAAA,IACxB,KAAK;AACJ,aAAO,EAAE,GAAGC,GAAW,GAAGD,EAAA;AAAA,IAC3B,KAAK;AACJ,aAAO,EAAE,GAAGA,GAAQ,GAAGE,EAAA;AAAA,IAExB;AACC,aAAO,EAAE,GAAGD,GAAW,GAAGC,EAAA;AAAA,EAAW;AAExC,GAmBaC,IAAsB,CAAC;AAAA,EACnC,MAAAT;AAAA,EACA,QAAAK;AAAA,EACA,QAAAC;AAAA,EACA,SAAAI;AAAA,EACA,YAAAC;AAAA,EACA,iBAAAC;AAAA,EACA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;AAAA,EACR,GAAG,CAAA,CAAE,GAECC,IAAeC;AAAA,IACpB,OAAO;AAAA,MACN,eAAed;AAAA,MACf,eAAeG;AAAA,MACf,aAAaK;AAAA,MACb,iBAAiBA;AAAA,IAAA;AAAA,IAElB,CAACA,GAAYR,GAAmBG,CAAiB;AAAA,EAAA;AAGlD,SAAO,EAAE,UAAA/B,GAAU,YAAAmB,GAAY,UAAAjB,GAAU,cAAAuC,GAAc,mBAAAF,EAAA;AACxD;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function renderCopilotMarkdown(text: string): string;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const f = (p, e) => `<a href="${p}" target="_blank" rel="noopener noreferrer">${e}</a>`, m = /[.,;:!?)\]]+$/;
|
|
2
|
+
function u(p) {
|
|
3
|
+
let e = p;
|
|
4
|
+
e = e.replace(/`([^`]+)`/g, "<code>$1</code>"), e = e.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>"), e = e.replace(/(^|\s)\*(.+?)\*(?=\s|$)/g, "$1<em>$2</em>"), e = e.replace(/(^|\s)_(.+?)_(?=\s|$)/g, "$1<em>$2</em>");
|
|
5
|
+
const c = [], t = (s) => (c.push(s), `@@INLINE${c.length - 1}@@`);
|
|
6
|
+
return e = e.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (s, r, l) => {
|
|
7
|
+
const o = /^(https?:|mailto:)/i.test(l) ? l : "#";
|
|
8
|
+
return t(f(o, r));
|
|
9
|
+
}), e = e.replace(/<code>[\s\S]*?<\/code>/g, (s) => t(s)), e = e.replace(/https?:\/\/[^\s<>"']+/g, (s) => {
|
|
10
|
+
const r = s.replace(m, "");
|
|
11
|
+
return `${f(r, r)}${s.slice(r.length)}`;
|
|
12
|
+
}), e.replace(
|
|
13
|
+
/@@INLINE(\d+)@@/g,
|
|
14
|
+
(s, r) => c[Number(r)]
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
function N(p) {
|
|
18
|
+
if (!p) return "";
|
|
19
|
+
let e = p.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
20
|
+
const c = [];
|
|
21
|
+
e = e.replace(/```([\s\S]*?)```/g, (n, i) => (c.push(`<pre><code>${i.trim()}</code></pre>`), `@@CODE${c.length - 1}@@`));
|
|
22
|
+
const t = [];
|
|
23
|
+
let s = null, r = [], l = 0;
|
|
24
|
+
const o = () => {
|
|
25
|
+
s && (t.push(`</${s}>`), s = null);
|
|
26
|
+
}, a = () => {
|
|
27
|
+
r.length && (t.push(r.join("<br/>")), r = []);
|
|
28
|
+
};
|
|
29
|
+
for (const n of e.split(`
|
|
30
|
+
`)) {
|
|
31
|
+
const i = /^@@CODE\d+@@$/.test(n.trim()), h = n.match(/^(#{1,4})\s+(.+)$/), $ = n.match(/^\s*[-*]\s+(.+)$/), g = n.match(/^\s*\d+\.\s+(.+)$/);
|
|
32
|
+
if (i)
|
|
33
|
+
a(), o(), l = 0, t.push(n.trim());
|
|
34
|
+
else if (h) {
|
|
35
|
+
a(), o(), l = 0;
|
|
36
|
+
const d = h[1].length;
|
|
37
|
+
t.push(`<h${d}>${u(h[2])}</h${d}>`);
|
|
38
|
+
} else $ ? (a(), s !== "ul" && (o(), t.push("<ul>"), s = "ul"), t.push(`<li>${u($[1])}</li>`)) : g ? (a(), s !== "ol" && (o(), t.push(
|
|
39
|
+
l > 0 ? `<ol start="${l + 1}">` : "<ol>"
|
|
40
|
+
), s = "ol"), l += 1, t.push(`<li>${u(g[1])}</li>`)) : n.trim() === "" ? (a(), o(), l = 0, t.push("<br/>")) : (o(), l = 0, r.push(u(n)));
|
|
41
|
+
}
|
|
42
|
+
return a(), o(), t.join(`
|
|
43
|
+
`).replace(/@@CODE(\d+)@@/g, (n, i) => c[Number(i)]);
|
|
44
|
+
}
|
|
45
|
+
export {
|
|
46
|
+
N as renderCopilotMarkdown
|
|
47
|
+
};
|
|
48
|
+
//# sourceMappingURL=markdown.js.map
|