@inkeep/agents-ui 0.16.0 → 0.16.2

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