@inkeep/agents-ui 0.16.0 → 0.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/dist/color-mode/index.js +4 -4
  2. package/dist/node_modules/.pnpm/use-sync-external-store@1.6.0_react@19.3.0-canary-87ae75b3-20260128/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.cjs +9 -0
  3. package/dist/node_modules/.pnpm/{use-sync-external-store@1.5.0_react@19.0.0 → use-sync-external-store@1.6.0_react@19.3.0-canary-87ae75b3-20260128}/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.js +2 -2
  4. package/dist/primitives/atoms/cmdk/index.cjs +1 -1
  5. package/dist/primitives/atoms/cmdk/index.js +14 -2
  6. package/dist/primitives/atoms/combobox.d.ts +104 -0
  7. package/dist/primitives/atoms/portal.js +3 -3
  8. package/dist/primitives/atoms/select.cjs +1 -1
  9. package/dist/primitives/atoms/select.js +2 -2
  10. package/dist/primitives/atoms/shadow/context.js +4 -4
  11. package/dist/primitives/components/embedded-chat/chat-error-helpers.cjs +3 -0
  12. package/dist/primitives/components/embedded-chat/chat-error-helpers.d.ts +26 -0
  13. package/dist/primitives/components/embedded-chat/chat-error-helpers.js +32 -0
  14. package/dist/primitives/components/embedded-chat/file-upload-input.cjs +1 -1
  15. package/dist/primitives/components/embedded-chat/file-upload-input.d.ts +1 -1
  16. package/dist/primitives/components/embedded-chat/file-upload-input.js +119 -116
  17. package/dist/primitives/components/embedded-chat/image-preview-modal.js +3 -3
  18. package/dist/primitives/components/embedded-chat/use-inkeep-chat.cjs +2 -4
  19. package/dist/primitives/components/embedded-chat/use-inkeep-chat.d.ts +1 -2
  20. package/dist/primitives/components/embedded-chat/use-inkeep-chat.js +305 -278
  21. package/dist/primitives/components/embedded-chat/use-stream-processor.cjs +1 -1
  22. package/dist/primitives/components/embedded-chat/use-stream-processor.js +1 -1
  23. package/dist/primitives/components/embedded-chat.cjs +1 -1
  24. package/dist/primitives/components/embedded-chat.d.ts +1 -2
  25. package/dist/primitives/components/embedded-chat.js +23 -23
  26. package/dist/primitives/components/embedded-search/search-provider.js +3 -3
  27. package/dist/primitives/components/embedded-search.cjs +1 -1
  28. package/dist/primitives/components/embedded-search.d.ts +1 -1
  29. package/dist/primitives/components/embedded-search.js +3 -3
  30. package/dist/primitives/components/modal/modal-provider.js +2 -2
  31. package/dist/primitives/components/modal.js +3 -3
  32. package/dist/primitives/components/sidebar-chat/index.js +1 -1
  33. package/dist/primitives/providers/base-events-provider.cjs +1 -1
  34. package/dist/primitives/providers/base-events-provider.js +22 -22
  35. package/dist/primitives/providers/chat-bubble-provider.js +1 -1
  36. package/dist/primitives/providers/chat-form-provider.d.ts +1 -1
  37. package/dist/primitives/providers/chat-form-provider.js +4 -4
  38. package/dist/primitives/providers/chat-history-provider.js +3 -3
  39. package/dist/primitives/providers/config-provider.js +3 -3
  40. package/dist/primitives/providers/feedback-provider.js +3 -3
  41. package/dist/primitives/providers/form-field-provider.js +3 -3
  42. package/dist/primitives/providers/help-menu-provider.js +4 -4
  43. package/dist/primitives/providers/hover-popover-provider.js +3 -3
  44. package/dist/primitives/providers/image-preview-provider.js +5 -5
  45. package/dist/primitives/providers/markdown-provider.js +2 -2
  46. package/dist/primitives/providers/message-provider.js +1 -1
  47. package/dist/primitives/providers/sidebar-chat-provider.js +3 -3
  48. package/dist/primitives/providers/source-item-provider.js +3 -3
  49. package/dist/primitives/providers/widget-provider.js +2 -2
  50. package/dist/primitives/utils/misc.cjs +1 -1
  51. package/dist/primitives/utils/misc.js +2 -2
  52. package/dist/react/chat-button.js +1 -1
  53. package/dist/react/embedded-chat.cjs +1 -1
  54. package/dist/react/embedded-chat.js +5 -5
  55. package/dist/react/embedded-search.cjs +1 -1
  56. package/dist/react/embedded-search.js +18 -18
  57. package/dist/styled/components/chat-bubble.js +4 -4
  58. package/dist/styled/components/chat-button.cjs +1 -1
  59. package/dist/styled/components/chat-button.js +9 -9
  60. package/dist/styled/components/chat-history.cjs +1 -1
  61. package/dist/styled/components/chat-history.js +22 -22
  62. package/dist/styled/components/embedded-chat/file-upload-input.cjs +1 -1
  63. package/dist/styled/components/embedded-chat/file-upload-input.js +71 -72
  64. package/dist/styled/components/embedded-chat/image-preview-modal.js +5 -5
  65. package/dist/styled/components/embedded-chat.cjs +1 -1
  66. package/dist/styled/components/embedded-chat.js +198 -198
  67. package/dist/styled/components/embedded-search.cjs +1 -1
  68. package/dist/styled/components/embedded-search.js +65 -65
  69. package/dist/styled/components/loading-indicator.cjs +1 -1
  70. package/dist/styled/components/loading-indicator.js +1 -1
  71. package/dist/styled/components/message.cjs +1 -1
  72. package/dist/styled/components/message.js +90 -90
  73. package/dist/styled/components/modal.js +3 -3
  74. package/dist/styled/components/searchbar.js +8 -8
  75. package/dist/styled/components/sidebar-chat.cjs +1 -1
  76. package/dist/styled/components/sidebar-chat.js +5 -5
  77. package/dist/styled/inkeep.css.cjs +13 -3
  78. package/dist/styled/inkeep.css.js +13 -3
  79. package/dist/theme/utils/generate-theme-variables.js +6 -6
  80. package/package.json +1 -1
  81. package/dist/node_modules/.pnpm/use-sync-external-store@1.5.0_react@19.0.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.cjs +0 -9
  82. /package/dist/node_modules/.pnpm/{use-sync-external-store@1.5.0_react@19.0.0 → use-sync-external-store@1.6.0_react@19.3.0-canary-87ae75b3-20260128}/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.cjs +0 -0
  83. /package/dist/node_modules/.pnpm/{use-sync-external-store@1.5.0_react@19.0.0 → use-sync-external-store@1.6.0_react@19.3.0-canary-87ae75b3-20260128}/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.js +0 -0
  84. /package/dist/node_modules/.pnpm/{use-sync-external-store@1.5.0_react@19.0.0 → use-sync-external-store@1.6.0_react@19.3.0-canary-87ae75b3-20260128}/node_modules/use-sync-external-store/shim/index.cjs +0 -0
  85. /package/dist/node_modules/.pnpm/{use-sync-external-store@1.5.0_react@19.0.0 → use-sync-external-store@1.6.0_react@19.3.0-canary-87ae75b3-20260128}/node_modules/use-sync-external-store/shim/index.js +0 -0
@@ -1,128 +1,132 @@
1
1
  "use client";
2
- import { useChat as tt } from "@ai-sdk/react";
3
- import { normalizeFileType as ke } from "./file-upload-input.js";
4
- import { DefaultChatTransport as st } from "ai";
5
- import { useRef as l, useEffect as x, useState as D, useMemo as xe, useCallback as $, useImperativeHandle as rt } from "react";
6
- import { useModal as nt } from "../modal/modal-provider.js";
7
- import { useOptionalChatBubble as ot } from "../../providers/chat-bubble-provider.js";
8
- import { useOptionalSidebarChat as at } from "../../providers/sidebar-chat-provider.js";
9
- import { useInkeepConfig as it } from "../../providers/config-provider.js";
10
- import { useCaptcha as lt } from "./use-captcha.js";
11
- import { useMediaQuery as ct } from "../../hooks/use-media-query.js";
12
- import { useAnonymousSession as ut } from "../../hooks/use-anonymous-session.js";
13
- import { useAuthToken as pt } from "../../hooks/use-auth-token.js";
14
- import { useConversationLoader as dt } from "../../hooks/use-conversation-loader.js";
15
- import { useInitialConversation as ft } from "../../hooks/use-initial-conversation.js";
16
- import { generateUid as L } from "../../utils/generate-uid.js";
17
- import { useBaseEvents as mt } from "../../providers/base-events-provider.js";
18
- import { useChatForm as ht } from "../../providers/chat-form-provider.js";
19
- import { useWidget as gt } from "../../providers/widget-provider.js";
20
- import { useControllableState as vt } from "@radix-ui/react-use-controllable-state";
21
- import { useStreamingEvents as yt } from "../../hooks/use-streaming-events.js";
22
- import { parseAuthError as bt } from "../../hooks/use-inkeep-api-client.js";
23
- import { useInputNotification as Ct } from "../../hooks/use-input-notification.js";
24
- function It(g) {
25
- const m = g.message ?? "";
26
- let s = Number(g.code) || Number(g.statusCode);
27
- if (Number.isNaN(s))
28
- try {
29
- s = Number(JSON.parse(m).status);
30
- } catch {
31
- }
32
- const p = bt(s, { detail: m });
33
- return p !== null ? p : s === 401 ? "session" : null;
34
- }
35
- const te = `Hmm..
36
-
37
- It seems I might be having some issues right now. Please clear the chat and try again.`, Kt = () => {
38
- const { baseSettings: g, aiChatSettings: m } = it(), [s = "", p] = vt({
39
- prop: m.conversationIdOverride,
40
- defaultProp: m.conversationIdOverride ?? ""
41
- }), Me = nt(), Fe = ot(), Ee = at(), { logEvent: v } = mt(), { setConversationId: Ne, emitToParent: M } = yt(), se = l(s);
2
+ import { useChat as ot } from "@ai-sdk/react";
3
+ import { normalizeFileType as xe } from "./file-upload-input.js";
4
+ import { DefaultChatTransport as at } from "ai";
5
+ import { useRef as c, useEffect as x, useState as F, useMemo as Fe, useCallback as $, useImperativeHandle as it } from "react";
6
+ import { useModal as lt } from "../modal/modal-provider.js";
7
+ import { useOptionalChatBubble as ct } from "../../providers/chat-bubble-provider.js";
8
+ import { useOptionalSidebarChat as ut } from "../../providers/sidebar-chat-provider.js";
9
+ import { useInkeepConfig as pt } from "../../providers/config-provider.js";
10
+ import { useCaptcha as dt } from "./use-captcha.js";
11
+ import { useMediaQuery as ft } from "../../hooks/use-media-query.js";
12
+ import { useAnonymousSession as mt } from "../../hooks/use-anonymous-session.js";
13
+ import { useAuthToken as gt } from "../../hooks/use-auth-token.js";
14
+ import { useConversationLoader as ht } from "../../hooks/use-conversation-loader.js";
15
+ import { useInitialConversation as vt } from "../../hooks/use-initial-conversation.js";
16
+ import { generateUid as U } from "../../utils/generate-uid.js";
17
+ import { useBaseEvents as yt } from "../../providers/base-events-provider.js";
18
+ import { useChatForm as Ct } from "../../providers/chat-form-provider.js";
19
+ import { useWidget as At } from "../../providers/widget-provider.js";
20
+ import { useControllableState as St } from "@radix-ui/react-use-controllable-state";
21
+ import { useStreamingEvents as bt } from "../../hooks/use-streaming-events.js";
22
+ import { useInputNotification as It } from "../../hooks/use-input-notification.js";
23
+ import { resolveStreamingAuthError as Rt, DEFAULT_ERROR_MESSAGE as ie, isRecoverableError as wt, RECOVERABLE_NOTIFICATION_DURATION_MS as Tt, resolveHttpStatusCode as Et, RATE_LIMIT_MESSAGE as kt, RECOVERABLE_FALLBACK_MESSAGE as _e } from "./chat-error-helpers.js";
24
+ const Zt = () => {
25
+ const { baseSettings: q, aiChatSettings: y } = pt(), [i = "", g] = St({
26
+ prop: y.conversationIdOverride,
27
+ defaultProp: y.conversationIdOverride ?? ""
28
+ }), Oe = lt(), Ne = ct(), Pe = ut(), { logEvent: m } = yt(), { setConversationId: Le, emitToParent: _ } = bt(), le = c(i);
42
29
  x(() => {
43
- const e = se.current;
44
- se.current = s, e !== s && v({
30
+ const e = le.current;
31
+ le.current = i, e !== i && m({
45
32
  eventName: "chat_conversation_changed",
46
33
  properties: {
47
- conversationId: s,
34
+ conversationId: i,
48
35
  previousConversationId: e
49
36
  }
50
37
  });
51
- }, [s, v]);
52
- const [C, F] = D(""), Oe = (e) => F(e.target.value), { shouldBypassCaptcha: re, filters: ne, privacyPreferences: Pe, userProperties: B } = g, { authToken: E, isLoading: _e, refreshToken: oe } = pt(), ae = !!g.getAuthToken, d = !!E, {
53
- onInputMessageChange: De,
54
- filters: ie,
38
+ }, [i, m]);
39
+ const [C, A] = F(""), De = (e) => A(e.target.value), {
40
+ shouldBypassCaptcha: ce,
41
+ filters: ue,
42
+ privacyPreferences: Be,
43
+ userProperties: z
44
+ } = q, { authToken: O, isLoading: $e, refreshToken: pe } = gt(), de = !!q.getAuthToken, d = !!O, {
45
+ onInputMessageChange: Ue,
46
+ filters: fe,
55
47
  baseUrl: N,
56
- agentUrl: $e,
57
- context: le,
58
- headers: ce,
59
- appId: I,
60
- apiKey: y,
61
- files: w
62
- } = m, Le = Me?.isOpen ?? Fe?.isOpen ?? Ee?.isOpen ?? !0, { getCaptchaHeader: S, invalidate: c } = lt({
48
+ agentUrl: qe,
49
+ context: me,
50
+ headers: ge,
51
+ appId: S,
52
+ apiKey: h,
53
+ files: b
54
+ } = y, ze = Oe?.isOpen ?? Ne?.isOpen ?? Pe?.isOpen ?? !0, { getCaptchaHeader: I, invalidate: u } = dt({
63
55
  baseUrl: N,
64
- shouldBypassCaptcha: re || !!y,
65
- shouldMakeInitialRequest: Le
66
- }), ue = l(S);
67
- ue.current = S;
68
- const pe = $e || `${N}/run/api/chat`, { sessionToken: U, refreshSession: z } = ut({
56
+ shouldBypassCaptcha: ce || !!h,
57
+ shouldMakeInitialRequest: ze
58
+ }), he = c(I);
59
+ he.current = I;
60
+ const ve = qe || `${N}/run/api/chat`, { sessionToken: H, refreshSession: K } = mt({
69
61
  baseUrl: N,
70
- appId: I,
71
- getCaptchaHeader: S,
72
- invalidateCaptcha: c,
73
- optOutAllAnalytics: Pe?.optOutAllAnalytics,
74
- enabled: !d && !_e
75
- }), de = y ?? (d ? E : U), { loadConversation: fe } = dt({
62
+ appId: S,
63
+ getCaptchaHeader: I,
64
+ invalidateCaptcha: u,
65
+ optOutAllAnalytics: Be?.optOutAllAnalytics,
66
+ enabled: !d && !$e
67
+ }), ye = h ?? (d ? O : H), { loadConversation: Ce } = ht({
76
68
  baseUrl: N,
77
- appId: I,
78
- authToken: de,
79
- getCaptchaHeader: S,
80
- invalidateCaptcha: c,
81
- refreshSession: y || d ? void 0 : z
82
- }), [Be, me] = D(!1), H = l(null);
83
- H.current = U;
84
- const q = l(null);
85
- q.current = E;
86
- const K = l(void 0);
87
- K.current = B && Object.keys(B).length > 0 ? JSON.stringify(B) : void 0;
88
- const b = l(0), he = l(null), J = l(null), A = l(void 0), Ue = w?.map((e) => `${e.filename ?? ""}:${e.mediaType}:${e.url.length}:${e.url.slice(0, 64)}:${e.url.slice(-32)}`).join(`
69
+ appId: S,
70
+ authToken: ye,
71
+ getCaptchaHeader: I,
72
+ invalidateCaptcha: u,
73
+ refreshSession: h || d ? void 0 : K
74
+ }), [He, Ae] = F(!1), G = c(null);
75
+ G.current = H;
76
+ const J = c(null);
77
+ J.current = O;
78
+ const V = c(void 0);
79
+ V.current = z && Object.keys(z).length > 0 ? JSON.stringify(z) : void 0;
80
+ const v = c(0), j = c(null), W = c(null), R = c(void 0), Ke = b?.map(
81
+ (e) => `${e.filename ?? ""}:${e.mediaType}:${e.url.length}:${e.url.slice(0, 64)}:${e.url.slice(-32)}`
82
+ ).join(`
89
83
  `) ?? "";
90
84
  x(() => {
91
- A.current = w?.length ? w : void 0;
92
- }, [Ue]);
93
- const ge = l(ce);
94
- ge.current = ce;
95
- const j = l(void 0);
96
- j.current = ne || ie ? JSON.stringify({ ...ne, ...ie }) : void 0;
97
- const ze = (e) => {
98
- switch (e.code) {
85
+ R.current = b?.length ? b : void 0;
86
+ }, [Ke]);
87
+ const Se = c(ge);
88
+ Se.current = ge;
89
+ const Q = c(void 0);
90
+ Q.current = ue || fe ? JSON.stringify({ ...ue, ...fe }) : void 0;
91
+ const Ge = (e) => {
92
+ switch (Et(e)) {
99
93
  case 400:
100
- return e.message;
94
+ try {
95
+ const t = JSON.parse(e.message ?? "");
96
+ return t.detail ?? t.error?.message ?? _e;
97
+ } catch {
98
+ return e.message?.trim() || _e;
99
+ }
101
100
  case 401:
102
- return ae ? "Authentication failed. Please try again." : te;
101
+ return de ? "Authentication failed. Please try again." : ie;
103
102
  case 403:
104
- return `There seems to be a configuration error. Please contact ${g.organizationDisplayName ?? "Administrator"}`;
103
+ return `There seems to be a configuration error. Please contact ${q.organizationDisplayName ?? "Administrator"}`;
104
+ case 429:
105
+ return kt;
105
106
  default:
106
- return te;
107
+ return ie;
107
108
  }
108
- }, [O, W] = D([]), He = xe(
109
- () => new st({
110
- api: pe,
109
+ }, [P, Z] = F([]), Je = Fe(
110
+ () => new at({
111
+ api: ve,
111
112
  headers: () => {
112
- const e = y ?? q.current ?? H.current;
113
+ const e = h ?? J.current ?? G.current;
113
114
  return {
114
115
  "x-inkeep-client-timezone": Intl.DateTimeFormat().resolvedOptions().timeZone,
115
116
  "x-inkeep-client-timestamp": (/* @__PURE__ */ new Date()).toISOString(),
116
- ...I ? { "x-inkeep-app-id": I } : {},
117
+ "x-inkeep-invocation-type": "chat_widget",
118
+ ...S ? { "x-inkeep-app-id": S } : {},
117
119
  ...e ? { Authorization: `Bearer ${e}` } : {},
118
- ...j.current ? { "inkeep-filters": j.current } : {},
119
- ...K.current ? { "x-inkeep-user-properties": K.current } : {},
120
- ...ge.current
120
+ ...Q.current ? { "inkeep-filters": Q.current } : {},
121
+ ...V.current ? { "x-inkeep-user-properties": V.current } : {},
122
+ ...Se.current
121
123
  };
122
124
  },
123
125
  prepareSendMessagesRequest: async (e) => {
124
- const i = await ue.current(), t = e.messages[e.messages.length - 1];
125
- return t || console.warn("[useInkeepChat] prepareSendMessagesRequest called with empty messages array"), {
126
+ const a = await he.current(), t = e.messages[e.messages.length - 1];
127
+ return t || console.warn(
128
+ "[useInkeepChat] prepareSendMessagesRequest called with empty messages array"
129
+ ), {
126
130
  body: {
127
131
  ...e.body,
128
132
  id: e.id,
@@ -132,277 +136,300 @@ It seems I might be having some issues right now. Please clear the chat and try
132
136
  },
133
137
  headers: {
134
138
  ...e.headers,
135
- ...i
139
+ ...a
136
140
  }
137
141
  };
138
142
  },
139
143
  body: {
140
- requestContext: le
144
+ requestContext: me
141
145
  }
142
146
  }),
143
- [pe, le, I, y]
147
+ [ve, me, S, h]
144
148
  ), {
145
- messages: P,
146
- sendMessage: G,
147
- addToolApprovalResponse: Q,
148
- status: ve,
149
- setMessages: h,
150
- stop: _,
151
- error: V
152
- } = tt({
153
- transport: He,
149
+ messages: L,
150
+ sendMessage: X,
151
+ addToolApprovalResponse: Y,
152
+ status: be,
153
+ setMessages: f,
154
+ stop: D,
155
+ error: B
156
+ } = ot({
157
+ transport: Je,
154
158
  onData(e) {
155
- M(e.type, e.data);
159
+ _(e.type, e.data);
156
160
  },
157
161
  async onFinish() {
158
- M("completion", { conversationId: s }), await v({
162
+ _("completion", { conversationId: i }), await m({
159
163
  eventName: "assistant_message_received",
160
164
  properties: {
161
- conversationId: s
165
+ conversationId: i
162
166
  }
163
- }), v({
167
+ }), m({
164
168
  eventName: "assistant_answer_displayed",
165
169
  properties: {
166
- conversationId: s
170
+ conversationId: i
167
171
  }
168
172
  });
169
173
  },
170
174
  onError(e) {
171
175
  console.error("onError", { code: e.code, message: e.message });
172
- const i = re || y ? null : It(e);
173
- if (i !== null && b.current < 1) {
174
- b.current++;
175
- const t = J.current, n = he.current;
176
+ const a = ce || h ? null : Rt(e);
177
+ if (a !== null && v.current < 1) {
178
+ v.current++;
179
+ const n = W.current, s = j.current;
176
180
  (async () => {
177
- if (i === "session" && ae) {
178
- const r = await oe();
179
- if (!r) throw new Error("Auth token refresh failed");
180
- q.current = r;
181
- } else if (i === "session") {
182
- const r = await z();
183
- r && (H.current = r);
181
+ if (a === "session" && de) {
182
+ const l = await pe();
183
+ if (!l) throw new Error("Auth token refresh failed");
184
+ J.current = l;
185
+ } else if (a === "session") {
186
+ const l = await K();
187
+ l && (G.current = l);
184
188
  } else
185
- c();
186
- if (t) {
187
- Q(t);
189
+ u();
190
+ if (n) {
191
+ Y(n);
188
192
  return;
189
193
  }
190
- n && (h((r) => {
191
- let o = [...r];
192
- return o.at(-1)?.role === "assistant" && (o = o.slice(0, -1)), o.at(-1)?.role === "user" && (o = o.slice(0, -1)), o;
193
- }), G(
194
- n.files?.length ? { parts: [{ type: "text", text: n.content }, ...n.files] } : { text: n.content },
195
- { body: n.body }
194
+ s && (f((l) => {
195
+ let r = [...l];
196
+ return r.at(-1)?.role === "assistant" && (r = r.slice(0, -1)), r.at(-1)?.role === "user" && (r = r.slice(0, -1)), r;
197
+ }), X(
198
+ s.files?.length ? { parts: [{ type: "text", text: s.content }, ...s.files] } : { text: s.content },
199
+ { body: s.body }
196
200
  ));
197
201
  })().catch(() => {
198
- b.current = 0, c(), h((r) => {
199
- const o = [...r], f = o[o.length - 1];
200
- if (!f) return o;
201
- const u = te;
202
- return f.role === "user" ? o.push({
203
- id: L(16),
202
+ v.current = 0, u(), f((l) => {
203
+ const r = [...l], E = r[r.length - 1];
204
+ if (!E) return r;
205
+ const k = ie;
206
+ return E.role === "user" ? r.push({
207
+ id: U(16),
204
208
  role: "assistant",
205
- parts: [{ type: "text", text: u }]
206
- }) : f.parts = [{ type: "text", text: u }], o;
209
+ parts: [{ type: "text", text: k }]
210
+ }) : E.parts = [{ type: "text", text: k }], r;
207
211
  });
208
212
  });
209
213
  return;
210
214
  }
211
- b.current = 0, i !== null && c(), v({
215
+ v.current = 0, a !== null && u();
216
+ const t = wt(e), p = Ge(e);
217
+ if (m({
212
218
  eventName: "chat_error",
213
219
  properties: {
214
- conversationId: s,
220
+ conversationId: i,
215
221
  error: e.message
216
222
  }
217
- }), h((t) => {
218
- const n = [...t], a = n[n.length - 1];
219
- if (a) {
220
- const r = ze(e);
221
- a.role === "user" ? n.push({
222
- id: L(16),
223
- role: "assistant",
224
- parts: [{ type: "text", text: r }]
225
- }) : a.parts = [{ type: "text", text: r }];
226
- }
227
- return n;
223
+ }), t) {
224
+ ne(
225
+ { title: "Request failed", message: p },
226
+ Tt
227
+ ), f((s) => {
228
+ let o = [...s];
229
+ return o.at(-1)?.role === "assistant" && (o = o.slice(0, -1)), o.at(-1)?.role === "user" && (o = o.slice(0, -1)), o;
230
+ });
231
+ const n = j.current?.content;
232
+ n && A(n), re.current = e;
233
+ return;
234
+ }
235
+ f((n) => {
236
+ const s = [...n], o = s[s.length - 1];
237
+ return o && (o.role === "user" ? s.push({
238
+ id: U(16),
239
+ role: "assistant",
240
+ parts: [{ type: "text", text: p }]
241
+ }) : o.parts = [{ type: "text", text: p }]), s;
228
242
  });
229
243
  }
230
- }), ye = l(d);
244
+ }), Ie = c(d);
231
245
  x(() => {
232
- const e = ye.current;
233
- ye.current = d, e !== d && (_(), T(null), h([]), p(""), F(""), W([]), c());
234
- }, [d, _, h, p, c]);
235
- const be = ve === "submitted", Z = ve === "streaming", qe = xe(() => {
236
- const e = (a) => {
237
- if (!a || typeof a != "object") return !1;
238
- const r = a;
239
- return typeof r.type == "string" && r.type.startsWith("tool-");
240
- }, t = [...P ?? []].reverse().find((a) => a.role === "assistant");
246
+ const e = Ie.current;
247
+ Ie.current = d, e !== d && (D(), w(null), f([]), g(""), A(""), Z([]), u());
248
+ }, [d, D, f, g, u]);
249
+ const Re = be === "submitted", ee = be === "streaming", Ve = Fe(() => {
250
+ const e = (n) => {
251
+ if (!n || typeof n != "object") return !1;
252
+ const s = n;
253
+ return typeof s.type == "string" && s.type.startsWith("tool-");
254
+ }, t = [...L ?? []].reverse().find((n) => n.role === "assistant");
241
255
  if (!t) return !1;
242
- const n = t.parts?.at(-1);
243
- return !(!e(n) || n.state !== "output-available" || !n.approval?.id || Z);
244
- }, [P, Z]), Ce = Z || qe, Ie = be || Ce, Ke = P.length === 0, X = !C.trim() && O.length === 0 || Ie, Je = ct("(max-width: 768px)"), [je, T] = D(null);
256
+ const p = t.parts?.at(-1);
257
+ return !(!e(p) || p.state !== "output-available" || !p.approval?.id || ee);
258
+ }, [L, ee]), [je, te] = F(!1), we = ee || Ve && !je, Te = Re || we, We = L.length === 0, se = !C.trim() && P.length === 0 || Te, Qe = ft("(max-width: 768px)"), [Ze, w] = F(null), re = c(null);
245
259
  x(() => {
246
- V && T(V);
247
- }, [V]);
248
- const We = () => T(null), { inputNotification: Ge, showInputNotification: we, clearInputNotification: Qe } = Ct(), Se = l(null);
260
+ if (B) {
261
+ if (re.current === B) {
262
+ re.current = null;
263
+ return;
264
+ }
265
+ w(B);
266
+ }
267
+ }, [B]);
268
+ const Xe = () => w(null), { inputNotification: Ye, showInputNotification: ne, clearInputNotification: et } = It(), Ee = c(null);
249
269
  x(() => {
250
- De?.(C);
270
+ Ue?.(C);
251
271
  }, [C]);
252
- const Ve = (e) => {
253
- e.key === "Enter" && !e.shiftKey && !X && !e.nativeEvent.isComposing && (e.preventDefault(), Y());
254
- }, Y = async (e = C) => {
255
- if (X && (!e || e.trim().length === 0) && O.length === 0) return;
256
- const i = O;
257
- W([]), F(""), b.current = 0, J.current = null, await v({
272
+ const tt = (e) => {
273
+ e.key === "Enter" && !e.shiftKey && !se && !e.nativeEvent.isComposing && (e.preventDefault(), oe());
274
+ }, oe = async (e = C) => {
275
+ if (se && (!e || e.trim().length === 0) && P.length === 0) return;
276
+ const a = P;
277
+ Z([]), A(""), v.current = 0, W.current = null, te(!1), await m({
258
278
  eventName: "user_message_submitted",
259
279
  properties: {
260
- conversationId: s
280
+ conversationId: i
261
281
  }
262
282
  });
263
- let t = s;
264
- t || (t = `conv_${L(16)}`, p(t)), Ne(t);
265
- const n = A.current;
266
- A.current = void 0;
267
- let a, r;
268
- if (n?.length) {
283
+ let t = i;
284
+ t || (t = `conv_${U(16)}`, g(t)), Le(t);
285
+ const p = R.current;
286
+ R.current = void 0;
287
+ let n, s;
288
+ if (p?.length) {
269
289
  let o;
270
290
  try {
271
291
  o = await Promise.all(
272
- i.map((f) => {
273
- const u = ke(f);
274
- return new Promise((et, Re) => {
275
- const k = new FileReader();
276
- k.onload = () => {
277
- if (typeof k.result != "string") {
278
- Re(new Error(`Failed to read file "${u.name}"`));
292
+ a.map((l) => {
293
+ const r = xe(l);
294
+ return new Promise((E, k) => {
295
+ const M = new FileReader();
296
+ M.onload = () => {
297
+ if (typeof M.result != "string") {
298
+ k(new Error(`Failed to read file "${r.name}"`));
279
299
  return;
280
300
  }
281
- et({
301
+ E({
282
302
  type: "file",
283
- url: k.result,
284
- mediaType: u.type,
285
- filename: u.name
303
+ url: M.result,
304
+ mediaType: r.type,
305
+ filename: r.name
286
306
  });
287
- }, k.onerror = () => Re(new Error(`Failed to read file "${u.name}"`)), k.readAsDataURL(u);
307
+ }, M.onerror = () => k(new Error(`Failed to read file "${r.name}"`)), M.readAsDataURL(r);
288
308
  });
289
309
  })
290
310
  );
291
311
  } catch {
292
- we({
312
+ ne({
293
313
  title: "Failed to attach files",
294
314
  message: "Could not read one or more files. Please try again."
295
315
  });
296
316
  return;
297
317
  }
298
- r = [...o, ...n], a = e.trim() ? { parts: [{ type: "text", text: e }, ...r] } : { parts: r };
299
- } else if (i.length > 0) {
318
+ s = [...o, ...p], n = e.trim() ? { parts: [{ type: "text", text: e }, ...s] } : { parts: s };
319
+ } else if (a.length > 0) {
300
320
  const o = new DataTransfer();
301
- for (const u of i) o.items.add(ke(u));
302
- const f = o.files;
303
- a = e.trim() ? { text: e, files: f } : { files: f };
321
+ for (const r of a) o.items.add(xe(r));
322
+ const l = o.files;
323
+ n = e.trim() ? { text: e, files: l } : { files: l };
304
324
  } else
305
- a = { text: e };
306
- he.current = {
325
+ n = { text: e };
326
+ j.current = {
307
327
  content: e,
308
328
  body: { conversationId: t },
309
- files: r
310
- }, G(a, {
329
+ files: s
330
+ }, X(n, {
311
331
  body: { conversationId: t }
312
332
  });
313
- }, Ze = $(
333
+ }, st = $(
314
334
  (e) => {
315
- b.current = 0, J.current = e, Q(e);
335
+ v.current = 0, W.current = e, te(!1), Y(e);
316
336
  },
317
- [Q]
318
- ), ee = $(() => {
319
- _().then(() => {
320
- M("aborted", { conversationId: s });
337
+ [Y]
338
+ ), ae = $(() => {
339
+ te(!0), D().then(() => {
340
+ _("aborted", { conversationId: i });
321
341
  });
322
- }, [_, s, M]), Ae = () => {
323
- We(), h([]), p(""), c(), A.current = w?.length ? w : void 0, v({
342
+ }, [D, i, _]), ke = () => {
343
+ Xe(), f([]), g(""), u(), R.current = b?.length ? b : void 0, m({
324
344
  eventName: "chat_clear_button_clicked",
325
345
  properties: {
326
- conversationId: s
346
+ conversationId: i
327
347
  }
328
348
  });
329
- }, R = $(
330
- (e, i) => {
331
- T(null), h(i), p(e), c(), A.current = void 0;
349
+ }, T = $(
350
+ (e, a) => {
351
+ w(null), f(a), g(e), u(), R.current = void 0;
332
352
  },
333
- [h, p, c]
334
- ), Te = $(
335
- async (e, i) => {
336
- ee(), R(e, []), me(!0);
353
+ [f, g, u]
354
+ ), Me = $(
355
+ async (e, a) => {
356
+ ae(), T(e, []), Ae(!0);
337
357
  try {
338
- const t = await fe(e, i);
358
+ const t = await Ce(e, a);
339
359
  if (t === null) return !1;
340
- const a = t[t.length - 1]?.role === "user" ? [...t, {
341
- id: L(16),
342
- role: "assistant",
343
- parts: [{ type: "text", text: "This session was interrupted. Please check back in a few minutes or start a new conversation." }]
344
- }] : t;
345
- return R(e, a), !0;
360
+ const n = t[t.length - 1]?.role === "user" ? [
361
+ ...t,
362
+ {
363
+ id: U(16),
364
+ role: "assistant",
365
+ parts: [
366
+ {
367
+ type: "text",
368
+ text: "This session was interrupted. Please check back in a few minutes or start a new conversation."
369
+ }
370
+ ]
371
+ }
372
+ ] : t;
373
+ return T(e, n), !0;
346
374
  } finally {
347
- i?.aborted || me(!1);
375
+ a?.aborted || Ae(!1);
348
376
  }
349
377
  },
350
- [R, fe, ee]
378
+ [T, Ce, ae]
351
379
  );
352
- ft({
353
- conversationId: m.conversationId,
354
- effectiveAuthToken: de,
355
- loadAndRestoreSession: Te,
356
- onLoadFailed: () => R("", [])
380
+ vt({
381
+ conversationId: y.conversationId,
382
+ effectiveAuthToken: ye,
383
+ loadAndRestoreSession: Me,
384
+ onLoadFailed: () => T("", [])
357
385
  });
358
- const { openForm: Xe } = ht(), Ye = gt();
359
- return rt(m.chatFunctionsRef, () => ({
360
- submitMessage: Y,
386
+ const { openForm: rt } = Ct(), nt = At();
387
+ return it(y.chatFunctionsRef, () => ({
388
+ submitMessage: oe,
361
389
  updateInputMessage(e) {
362
- F(e);
390
+ A(e);
363
391
  },
364
- clearChat: Ae,
392
+ clearChat: ke,
365
393
  openForm: (e) => {
366
- Ye?.setView("chat"), Xe(e, void 0);
394
+ nt?.setView("chat"), rt(e, void 0);
367
395
  },
368
396
  focusInput: () => {
369
- Se.current?.focus();
397
+ Ee.current?.focus();
370
398
  }
371
399
  })), {
372
- messages: P,
373
- sendMessage: G,
374
- addToolApprovalResponse: Ze,
375
- isLoading: be,
376
- isStreaming: Ce,
377
- isBusy: Ie,
378
- error: je,
379
- setError: T,
380
- isSubmitDisabled: X,
400
+ messages: L,
401
+ sendMessage: X,
402
+ addToolApprovalResponse: st,
403
+ isLoading: Re,
404
+ isStreaming: we,
405
+ isBusy: Te,
406
+ error: Ze,
407
+ setError: w,
408
+ isSubmitDisabled: se,
381
409
  input: C,
382
- handleInputChange: Oe,
383
- handleInputKeyDown: Ve,
384
- handleSubmit: Y,
385
- stop: ee,
386
- clear: Ae,
387
- inputRef: Se,
388
- isMobile: Je,
389
- files: O,
390
- setFiles: W,
391
- isNewChat: Ke,
392
- conversationId: s,
393
- restoreSession: R,
394
- loadAndRestoreSession: Te,
395
- isSessionLoading: Be,
396
- authToken: d ? E : U,
397
- refreshSession: d ? oe : z,
398
- getCaptchaHeader: S,
399
- invalidateCaptcha: c,
400
- inputNotification: Ge,
401
- showInputNotification: we,
402
- clearInputNotification: Qe
410
+ handleInputChange: De,
411
+ handleInputKeyDown: tt,
412
+ handleSubmit: oe,
413
+ stop: ae,
414
+ clear: ke,
415
+ inputRef: Ee,
416
+ isMobile: Qe,
417
+ files: P,
418
+ setFiles: Z,
419
+ isNewChat: We,
420
+ conversationId: i,
421
+ restoreSession: T,
422
+ loadAndRestoreSession: Me,
423
+ isSessionLoading: He,
424
+ authToken: d ? O : H,
425
+ refreshSession: d ? pe : K,
426
+ getCaptchaHeader: I,
427
+ invalidateCaptcha: u,
428
+ inputNotification: Ye,
429
+ showInputNotification: ne,
430
+ clearInputNotification: et
403
431
  };
404
432
  };
405
433
  export {
406
- te as DEFAULT_ERROR_MESSAGE,
407
- Kt as useInkeepChat
434
+ Zt as useInkeepChat
408
435
  };