@inkeep/agents-ui 0.0.0-dev-20260501205503 → 0.0.0-dev-20260501222329

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 (35) hide show
  1. package/dist/primitives/components/embedded-chat/chat-error-helpers.d.ts +2 -4
  2. package/dist/primitives/components/embedded-chat/use-captcha.cjs +1 -0
  3. package/dist/primitives/components/embedded-chat/use-captcha.d.ts +30 -0
  4. package/dist/primitives/components/embedded-chat/use-captcha.js +67 -0
  5. package/dist/primitives/components/embedded-chat/use-inkeep-chat.cjs +2 -2
  6. package/dist/primitives/components/embedded-chat/use-inkeep-chat.d.ts +2 -0
  7. package/dist/primitives/components/embedded-chat/use-inkeep-chat.js +262 -237
  8. package/dist/primitives/hooks/use-anonymous-session.cjs +1 -1
  9. package/dist/primitives/hooks/use-anonymous-session.d.ts +3 -1
  10. package/dist/primitives/hooks/use-anonymous-session.js +56 -51
  11. package/dist/primitives/hooks/use-conversation-loader.cjs +1 -1
  12. package/dist/primitives/hooks/use-conversation-loader.d.ts +3 -1
  13. package/dist/primitives/hooks/use-conversation-loader.js +20 -15
  14. package/dist/primitives/hooks/use-feedback-api.cjs +1 -1
  15. package/dist/primitives/hooks/use-feedback-api.d.ts +3 -1
  16. package/dist/primitives/hooks/use-feedback-api.js +22 -16
  17. package/dist/primitives/hooks/use-inkeep-api-client.cjs +1 -1
  18. package/dist/primitives/hooks/use-inkeep-api-client.d.ts +11 -8
  19. package/dist/primitives/hooks/use-inkeep-api-client.js +64 -36
  20. package/dist/primitives/providers/chat-history-provider.cjs +1 -1
  21. package/dist/primitives/providers/chat-history-provider.js +39 -35
  22. package/dist/primitives/providers/feedback-provider.cjs +1 -1
  23. package/dist/primitives/providers/feedback-provider.js +35 -26
  24. package/dist/primitives/providers/root-provider.cjs +1 -1
  25. package/dist/primitives/providers/root-provider.js +9 -13
  26. package/dist/primitives/utils/default-settings.cjs +1 -1
  27. package/dist/primitives/utils/default-settings.d.ts +0 -1
  28. package/dist/primitives/utils/default-settings.js +5 -6
  29. package/dist/types/config/ai.d.ts +0 -8
  30. package/package.json +2 -2
  31. package/dist/node_modules/.pnpm/botid@1.5.11_next@16.2.4_@babel_core@7.29.0_@opentelemetry_api@1.9.1_react-dom@19.3.0-c_6ac35f5bf61166da3243b6cf36e09c95/node_modules/botid/dist/client/core/index.cjs +0 -1
  32. package/dist/node_modules/.pnpm/botid@1.5.11_next@16.2.4_@babel_core@7.29.0_@opentelemetry_api@1.9.1_react-dom@19.3.0-c_6ac35f5bf61166da3243b6cf36e09c95/node_modules/botid/dist/client/core/index.js +0 -160
  33. package/dist/primitives/providers/botid-initializer.cjs +0 -1
  34. package/dist/primitives/providers/botid-initializer.d.ts +0 -15
  35. package/dist/primitives/providers/botid-initializer.js +0 -69
@@ -10,11 +10,9 @@ export interface ErrorWithCode extends Error {
10
10
  */
11
11
  export declare function resolveHttpStatusCode(error: Error): number | null;
12
12
  /**
13
- * Classifies a streaming-transport error as a session auth failure, or null.
14
- * Bot-protection failures (403) are not retried transparently — they surface
15
- * to the user as a config error.
13
+ * Classifies a streaming-transport error as a PoW or session auth failure, or null.
16
14
  */
17
- export declare function resolveStreamingAuthError(error: Error): 'session' | null;
15
+ export declare function resolveStreamingAuthError(error: Error): 'pow' | 'session' | null;
18
16
  /**
19
17
  * Recoverable errors are input-validation failures where the user can fix
20
18
  * their input and retry without clearing the conversation.
@@ -0,0 +1 @@
1
+ "use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("altcha-lib"),c=require("react"),x=30*1e3;function b(l){const e=/[?&]expires=(\d+)/.exec(l);return e?Number.parseInt(e[1],10)*1e3:null}const p=async l=>{try{const e=await fetch(l,{method:"GET"});if(!e.ok)throw new Error(`Failed to fetch challenge: ${e.statusText}`);const i=await e.json(),{challenge:u,algorithm:n,salt:r,maxnumber:t}=i,{promise:g}=await w.solveChallenge(u,r,n,t),f=await g||void 0;if(!f)throw new Error("Challenge not solved");return{payload:{number:f.number,...i},expiresAt:b(r)}}catch(e){console.warn("[captcha] Error fetching/solving challenge:",e);return}},m=({baseUrl:l,shouldBypassCaptcha:e=!1,shouldMakeInitialRequest:i=!0})=>{const u=`${l}/run/auth/pow/challenge`,n=c.useRef(null),r=c.useRef(null),t=c.useCallback(()=>{const o=p(u);n.current=o,o.then(a=>{a&&(r.current=a.expiresAt)})},[u]),g=c.useCallback(async()=>{if(e)return{};n.current||t();const o=n.current;n.current=null,t();const a=n.current,h=await o;if(!h){const s=await a;return s?{"x-inkeep-challenge-solution":btoa(JSON.stringify(s.payload))}:{}}if(h.expiresAt!==null&&Date.now()>=h.expiresAt){const s=await a;return s?{"x-inkeep-challenge-solution":btoa(JSON.stringify(s.payload))}:{}}return{"x-inkeep-challenge-solution":btoa(JSON.stringify(h.payload))}},[e,t]),f=c.useCallback(()=>{e||t()},[e,t]);return c.useEffect(()=>{if(!i||e)return;const o=r.current!==null&&Date.now()>=r.current-x;(!n.current||o)&&t()},[i,e,t]),{getCaptchaHeader:g,invalidate:f}};exports.fetchAndSolveChallenge=p;exports.useCaptcha=m;
@@ -0,0 +1,30 @@
1
+ import { Payload } from 'altcha-lib/types';
2
+ interface UseCaptchaOptions {
3
+ baseUrl: string;
4
+ shouldBypassCaptcha?: boolean;
5
+ shouldMakeInitialRequest?: boolean;
6
+ }
7
+ interface SolvedEntry {
8
+ payload: Payload;
9
+ expiresAt: number | null;
10
+ }
11
+ export declare const fetchAndSolveChallenge: (fetchUrl: string) => Promise<SolvedEntry | undefined>;
12
+ /**
13
+ * Manages the PoW captcha lifecycle for /run/* API requests.
14
+ *
15
+ * - getCaptchaHeader(): atomically consumes the current pre-fetched challenge,
16
+ * immediately kicks off the next fetch so a fresh solution is always warm, and
17
+ * returns the ready-to-use `{ 'x-inkeep-challenge-solution': '...' }` header
18
+ * (or {} when bypassing or on failure). Checks token expiry and falls back to
19
+ * the already-started pre-fetch if the resolved token is stale.
20
+ *
21
+ * - invalidate(): discards any in-flight solution and starts a fresh fetch.
22
+ * Call this on request errors to recover from a rejected/stale challenge.
23
+ *
24
+ * All hooks are called unconditionally — bypass logic lives inside callbacks.
25
+ */
26
+ export declare const useCaptcha: ({ baseUrl, shouldBypassCaptcha, shouldMakeInitialRequest, }: UseCaptchaOptions) => {
27
+ getCaptchaHeader: () => Promise<Record<string, string>>;
28
+ invalidate: () => void;
29
+ };
30
+ export {};
@@ -0,0 +1,67 @@
1
+ "use client";
2
+ import { solveChallenge as m } from "altcha-lib";
3
+ import { useRef as p, useCallback as h, useEffect as x } from "react";
4
+ const w = 30 * 1e3;
5
+ function E(i) {
6
+ const e = /[?&]expires=(\d+)/.exec(i);
7
+ return e ? Number.parseInt(e[1], 10) * 1e3 : null;
8
+ }
9
+ const y = async (i) => {
10
+ try {
11
+ const e = await fetch(i, { method: "GET" });
12
+ if (!e.ok)
13
+ throw new Error(`Failed to fetch challenge: ${e.statusText}`);
14
+ const c = await e.json(), { challenge: s, algorithm: t, salt: r, maxnumber: n } = c, { promise: g } = await m(
15
+ s,
16
+ r,
17
+ t,
18
+ n
19
+ ), u = await g || void 0;
20
+ if (!u)
21
+ throw new Error("Challenge not solved");
22
+ return {
23
+ payload: { number: u.number, ...c },
24
+ expiresAt: E(r)
25
+ };
26
+ } catch (e) {
27
+ console.warn("[captcha] Error fetching/solving challenge:", e);
28
+ return;
29
+ }
30
+ }, k = ({
31
+ baseUrl: i,
32
+ shouldBypassCaptcha: e = !1,
33
+ shouldMakeInitialRequest: c = !0
34
+ }) => {
35
+ const s = `${i}/run/auth/pow/challenge`, t = p(null), r = p(null), n = h(() => {
36
+ const o = y(s);
37
+ t.current = o, o.then((l) => {
38
+ l && (r.current = l.expiresAt);
39
+ });
40
+ }, [s]), g = h(async () => {
41
+ if (e) return {};
42
+ t.current || n();
43
+ const o = t.current;
44
+ t.current = null, n();
45
+ const l = t.current, f = await o;
46
+ if (!f) {
47
+ const a = await l;
48
+ return a ? { "x-inkeep-challenge-solution": btoa(JSON.stringify(a.payload)) } : {};
49
+ }
50
+ if (f.expiresAt !== null && Date.now() >= f.expiresAt) {
51
+ const a = await l;
52
+ return a ? { "x-inkeep-challenge-solution": btoa(JSON.stringify(a.payload)) } : {};
53
+ }
54
+ return { "x-inkeep-challenge-solution": btoa(JSON.stringify(f.payload)) };
55
+ }, [e, n]), u = h(() => {
56
+ e || n();
57
+ }, [e, n]);
58
+ return x(() => {
59
+ if (!c || e) return;
60
+ const o = r.current !== null && Date.now() >= r.current - w;
61
+ (!t.current || o) && n();
62
+ }, [c, e, n]), { getCaptchaHeader: g, invalidate: u };
63
+ };
64
+ export {
65
+ y as fetchAndSolveChallenge,
66
+ k as useCaptcha
67
+ };
@@ -1,2 +1,2 @@
1
- "use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const je=require("@ai-sdk/react"),Ce=require("./file-upload-input.cjs"),He=require("ai"),t=require("react"),Je=require("../../providers/config-provider.cjs"),Qe=require("../../hooks/use-media-query.cjs"),We=require("../../hooks/use-anonymous-session.cjs"),Ze=require("../../hooks/use-auth-token.cjs"),Xe=require("../../hooks/use-conversation-loader.cjs"),Ye=require("../../hooks/use-initial-conversation.cjs"),P=require("../../utils/generate-uid.cjs"),et=require("../../providers/base-events-provider.cjs"),tt=require("../../providers/chat-form-provider.cjs"),st=require("../../providers/widget-provider.cjs"),rt=require("@radix-ui/react-use-controllable-state"),nt=require("../../hooks/use-streaming-events.cjs"),ot=require("../../hooks/use-input-notification.cjs"),d=require("./chat-error-helpers.cjs"),at=()=>{const{baseSettings:O,aiChatSettings:v}=Je.useInkeepConfig(),[u="",h]=rt.useControllableState({prop:v.conversationIdOverride,defaultProp:v.conversationIdOverride??""}),{logEvent:g}=et.useBaseEvents(),{setConversationId:Ie,emitToParent:M}=nt.useStreamingEvents(),re=t.useRef(u);t.useEffect(()=>{const e=re.current;re.current=u,e!==u&&g({eventName:"chat_conversation_changed",properties:{conversationId:u,previousConversationId:e}})},[u,g]);const[y,R]=t.useState(""),Te=e=>R(e.target.value),{filters:ne,privacyPreferences:be,userProperties:N}=O,{authToken:F,isLoading:ke,refreshToken:oe}=Ze.useAuthToken(),ae=!!O.getAuthToken,f=!!F,{onInputMessageChange:we,filters:ie,baseUrl:D,agentUrl:Me,context:ue,headers:ce,appId:S,apiKey:E,files:A}=v,le=Me||`${D}/run/api/chat`,{sessionToken:U,refreshSession:$}=We.useAnonymousSession({baseUrl:D,appId:S,optOutAllAnalytics:be?.optOutAllAnalytics,enabled:!f&&!ke}),de=E??(f?F:U),{loadConversation:fe}=Xe.useConversationLoader({baseUrl:D,appId:S,authToken:de,refreshSession:E||f?void 0:$}),[Fe,pe]=t.useState(!1),B=t.useRef(null);B.current=U;const z=t.useRef(null);z.current=F;const G=t.useRef(void 0);G.current=N&&Object.keys(N).length>0?JSON.stringify(N):void 0;const m=t.useRef(0),K=t.useRef(null),V=t.useRef(null),C=t.useRef(void 0),_e=A?.map(e=>`${e.filename??""}:${e.mediaType}:${e.url.length}:${e.url.slice(0,64)}:${e.url.slice(-32)}`).join(`
2
- `)??"";t.useEffect(()=>{C.current=A?.length?A:void 0},[_e]);const ge=t.useRef(ce);ge.current=ce;const j=t.useRef(void 0);j.current=ne||ie?JSON.stringify({...ne,...ie}):void 0;const xe=e=>{switch(d.resolveHttpStatusCode(e)){case 400:try{const s=JSON.parse(e.message??"");return s.detail??s.error?.message??d.RECOVERABLE_FALLBACK_MESSAGE}catch{return e.message?.trim()||d.RECOVERABLE_FALLBACK_MESSAGE}case 401:return ae?"Authentication failed. Please try again.":d.DEFAULT_ERROR_MESSAGE;case 403:return`There seems to be a configuration error. Please contact ${O.organizationDisplayName??"Administrator"}`;case 429:return d.RATE_LIMIT_MESSAGE;default:return d.DEFAULT_ERROR_MESSAGE}},[_,H]=t.useState([]),qe=t.useMemo(()=>new He.DefaultChatTransport({api:le,headers:()=>{const e=E??z.current??B.current;return{"x-inkeep-client-timezone":Intl.DateTimeFormat().resolvedOptions().timeZone,"x-inkeep-client-timestamp":new Date().toISOString(),"x-inkeep-invocation-type":"chat_widget",...S?{"x-inkeep-app-id":S}:{},...e?{Authorization:`Bearer ${e}`}:{},...j.current?{"inkeep-filters":j.current}:{},...G.current?{"x-inkeep-user-properties":G.current}:{},...ge.current}},prepareSendMessagesRequest:e=>{const a=e.messages[e.messages.length-1];return a||console.warn("[useInkeepChat] prepareSendMessagesRequest called with empty messages array"),{body:{...e.body,id:e.id,messages:a?[a]:[],trigger:e.trigger,messageId:e.messageId},headers:e.headers}},body:{requestContext:ue}}),[le,ue,S,E]),{messages:x,sendMessage:J,addToolApprovalResponse:Q,status:he,setMessages:p,stop:q,error:L}=je.useChat({transport:qe,onData(e){M(e.type,e.data)},async onFinish(){M("completion",{conversationId:u}),await g({eventName:"assistant_message_received",properties:{conversationId:u}}),g({eventName:"assistant_answer_displayed",properties:{conversationId:u}})},onError(e){console.error("onError",{code:e.code,message:e.message});const a=E?null:d.resolveStreamingAuthError(e);if(a!==null&&m.current<1){m.current++;const o=V.current,r=K.current;(async()=>{if(a==="session"&&ae){const c=await oe();if(!c)throw new Error("Auth token refresh failed");z.current=c}else if(a==="session"){const c=await $();c&&(B.current=c)}if(o){Q(o);return}r&&(p(c=>{let n=[...c];return n.at(-1)?.role==="assistant"&&(n=n.slice(0,-1)),n.at(-1)?.role==="user"&&(n=n.slice(0,-1)),n}),J(r.files?.length?{parts:[{type:"text",text:r.content},...r.files]}:{text:r.content},{body:r.body}))})().catch(()=>{m.current=0,p(c=>{const n=[...c],b=n[n.length-1];if(!b)return n;const k=d.DEFAULT_ERROR_MESSAGE;return b.role==="user"?n.push({id:P.generateUid(16),role:"assistant",parts:[{type:"text",text:k}]}):b.parts=[{type:"text",text:k}],n})});return}m.current=0;const s=d.isRecoverableError(e),l=xe(e);if(g({eventName:"chat_error",properties:{conversationId:u,error:e.message}}),s){ee({title:"Request failed",message:l},d.RECOVERABLE_NOTIFICATION_DURATION_MS),p(r=>{let i=[...r];return i.at(-1)?.role==="assistant"&&(i=i.slice(0,-1)),i.at(-1)?.role==="user"&&(i=i.slice(0,-1)),i});const o=K.current?.content;o&&R(o),Y.current=e;return}p(o=>{const r=[...o],i=r[r.length-1];return i&&(i.role==="user"?r.push({id:P.generateUid(16),role:"assistant",parts:[{type:"text",text:l}]}):i.parts=[{type:"text",text:l}]),r})}}),me=t.useRef(f);t.useEffect(()=>{const e=me.current;me.current=f,e!==f&&(q(),I(null),p([]),h(""),R(""),H([]))},[f,q,p,h]);const ve=he==="submitted",W=he==="streaming",Le=t.useMemo(()=>{const e=o=>{if(!o||typeof o!="object")return!1;const r=o;return typeof r.type=="string"&&r.type.startsWith("tool-")},s=[...x??[]].reverse().find(o=>o.role==="assistant");if(!s)return!1;const l=s.parts?.at(-1);return!(!e(l)||l.state!=="output-available"||!l.approval?.id||W)},[x,W]),[Pe,Z]=t.useState(!1),ye=W||Le&&!Pe,Re=ve||ye,Oe=x.length===0,X=!y.trim()&&_.length===0||Re,Ne=Qe.useMediaQuery("(max-width: 768px)"),[De,I]=t.useState(null),Y=t.useRef(null);t.useEffect(()=>{if(L){if(Y.current===L){Y.current=null;return}I(L)}},[L]);const Ue=()=>I(null),{inputNotification:$e,showInputNotification:ee,clearInputNotification:Be}=ot.useInputNotification(),Se=t.useRef(null);t.useEffect(()=>{we?.(y)},[y]);const ze=e=>{e.key==="Enter"&&!e.shiftKey&&!X&&!e.nativeEvent.isComposing&&(e.preventDefault(),te())},te=async(e=y)=>{if(X&&(!e||e.trim().length===0)&&_.length===0)return;const a=_;H([]),R(""),m.current=0,V.current=null,Z(!1),await g({eventName:"user_message_submitted",properties:{conversationId:u}});let s=u;s||(s=`conv_${P.generateUid(16)}`,h(s)),Ie(s);const l=C.current;C.current=void 0;let o,r;if(l?.length){let i;try{i=await Promise.all(a.map(c=>{const n=Ce.normalizeFileType(c);return new Promise((b,k)=>{const w=new FileReader;w.onload=()=>{if(typeof w.result!="string"){k(new Error(`Failed to read file "${n.name}"`));return}b({type:"file",url:w.result,mediaType:n.type,filename:n.name})},w.onerror=()=>k(new Error(`Failed to read file "${n.name}"`)),w.readAsDataURL(n)})}))}catch{ee({title:"Failed to attach files",message:"Could not read one or more files. Please try again."});return}r=[...i,...l],o=e.trim()?{parts:[{type:"text",text:e},...r]}:{parts:r}}else if(a.length>0){const i=new DataTransfer;for(const n of a)i.items.add(Ce.normalizeFileType(n));const c=i.files;o=e.trim()?{text:e,files:c}:{files:c}}else o={text:e};K.current={content:e,body:{conversationId:s},files:r},J(o,{body:{conversationId:s}})},Ge=t.useCallback(e=>{m.current=0,V.current=e,Z(!1),Q(e)},[Q]),se=t.useCallback(()=>{Z(!0),q().then(()=>{M("aborted",{conversationId:u})})},[q,u,M]),Ee=()=>{Ue(),p([]),h(""),C.current=A?.length?A:void 0,g({eventName:"chat_clear_button_clicked",properties:{conversationId:u}})},T=t.useCallback((e,a)=>{I(null),p(a),h(e),C.current=void 0},[p,h]),Ae=t.useCallback(async(e,a)=>{se(),T(e,[]),pe(!0);try{const s=await fe(e,a);if(s===null)return!1;const o=s[s.length-1]?.role==="user"?[...s,{id:P.generateUid(16),role:"assistant",parts:[{type:"text",text:"This session was interrupted. Please check back in a few minutes or start a new conversation."}]}]:s;return T(e,o),!0}finally{a?.aborted||pe(!1)}},[T,fe,se]);Ye.useInitialConversation({conversationId:v.conversationId,effectiveAuthToken:de,loadAndRestoreSession:Ae,onLoadFailed:()=>T("",[])});const{openForm:Ke}=tt.useChatForm(),Ve=st.useWidget();return t.useImperativeHandle(v.chatFunctionsRef,()=>({submitMessage:te,updateInputMessage(e){R(e)},clearChat:Ee,openForm:e=>{Ve?.setView("chat"),Ke(e,void 0)},focusInput:()=>{Se.current?.focus()}})),{messages:x,sendMessage:J,addToolApprovalResponse:Ge,isLoading:ve,isStreaming:ye,isBusy:Re,error:De,setError:I,isSubmitDisabled:X,input:y,handleInputChange:Te,handleInputKeyDown:ze,handleSubmit:te,stop:se,clear:Ee,inputRef:Se,isMobile:Ne,files:_,setFiles:H,isNewChat:Oe,conversationId:u,restoreSession:T,loadAndRestoreSession:Ae,isSessionLoading:Fe,authToken:f?F:U,refreshSession:f?oe:$,inputNotification:$e,showInputNotification:ee,clearInputNotification:Be}};exports.useInkeepChat=at;
1
+ "use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const et=require("@ai-sdk/react"),we=require("./file-upload-input.cjs"),tt=require("ai"),t=require("react"),st=require("../modal/modal-provider.cjs"),rt=require("../../providers/chat-bubble-provider.cjs"),nt=require("../../providers/sidebar-chat-provider.cjs"),at=require("../../providers/config-provider.cjs"),ot=require("./use-captcha.cjs"),it=require("../../hooks/use-media-query.cjs"),ut=require("../../hooks/use-anonymous-session.cjs"),ct=require("../../hooks/use-auth-token.cjs"),lt=require("../../hooks/use-conversation-loader.cjs"),dt=require("../../hooks/use-initial-conversation.cjs"),D=require("../../utils/generate-uid.cjs"),pt=require("../../providers/base-events-provider.cjs"),ft=require("../../providers/chat-form-provider.cjs"),gt=require("../../providers/widget-provider.cjs"),ht=require("@radix-ui/react-use-controllable-state"),mt=require("../../hooks/use-streaming-events.cjs"),vt=require("../../hooks/use-input-notification.cjs"),p=require("./chat-error-helpers.cjs"),yt=()=>{const{baseSettings:U,aiChatSettings:R}=at.useInkeepConfig(),[u="",m]=ht.useControllableState({prop:R.conversationIdOverride,defaultProp:R.conversationIdOverride??""}),ke=st.useModal(),Me=rt.useOptionalChatBubble(),Fe=nt.useOptionalSidebarChat(),{logEvent:h}=pt.useBaseEvents(),{setConversationId:_e,emitToParent:_}=mt.useStreamingEvents(),ae=t.useRef(u);t.useEffect(()=>{const e=ae.current;ae.current=u,e!==u&&h({eventName:"chat_conversation_changed",properties:{conversationId:u,previousConversationId:e}})},[u,h]);const[C,S]=t.useState(""),qe=e=>S(e.target.value),{shouldBypassCaptcha:oe,filters:ie,privacyPreferences:xe,userProperties:B}=U,{authToken:q,isLoading:Oe,refreshToken:ue}=ct.useAuthToken(),ce=!!U.getAuthToken,f=!!q,{onInputMessageChange:Pe,filters:le,baseUrl:x,agentUrl:Le,context:de,headers:pe,appId:E,apiKey:v,files:A}=R,Ne=ke?.isOpen??Me?.isOpen??Fe?.isOpen??!0,{getCaptchaHeader:b,invalidate:l}=ot.useCaptcha({baseUrl:x,shouldBypassCaptcha:oe||!!v,shouldMakeInitialRequest:Ne}),fe=t.useRef(b);fe.current=b;const ge=Le||`${x}/run/api/chat`,{sessionToken:$,refreshSession:z}=ut.useAnonymousSession({baseUrl:x,appId:E,getCaptchaHeader:b,invalidateCaptcha:l,optOutAllAnalytics:xe?.optOutAllAnalytics,enabled:!f&&!Oe}),he=v??(f?q:$),{loadConversation:me}=lt.useConversationLoader({baseUrl:x,appId:E,authToken:he,getCaptchaHeader:b,invalidateCaptcha:l,refreshSession:v||f?void 0:z}),[De,ve]=t.useState(!1),G=t.useRef(null);G.current=$;const H=t.useRef(null);H.current=q;const K=t.useRef(void 0);K.current=B&&Object.keys(B).length>0?JSON.stringify(B):void 0;const y=t.useRef(0),V=t.useRef(null),j=t.useRef(null),I=t.useRef(void 0),Ue=A?.map(e=>`${e.filename??""}:${e.mediaType}:${e.url.length}:${e.url.slice(0,64)}:${e.url.slice(-32)}`).join(`
2
+ `)??"";t.useEffect(()=>{I.current=A?.length?A:void 0},[Ue]);const ye=t.useRef(pe);ye.current=pe;const J=t.useRef(void 0);J.current=ie||le?JSON.stringify({...ie,...le}):void 0;const Be=e=>{switch(p.resolveHttpStatusCode(e)){case 400:try{const s=JSON.parse(e.message??"");return s.detail??s.error?.message??p.RECOVERABLE_FALLBACK_MESSAGE}catch{return e.message?.trim()||p.RECOVERABLE_FALLBACK_MESSAGE}case 401:return ce?"Authentication failed. Please try again.":p.DEFAULT_ERROR_MESSAGE;case 403:return`There seems to be a configuration error. Please contact ${U.organizationDisplayName??"Administrator"}`;case 429:return p.RATE_LIMIT_MESSAGE;default:return p.DEFAULT_ERROR_MESSAGE}},[O,Q]=t.useState([]),$e=t.useMemo(()=>new tt.DefaultChatTransport({api:ge,headers:()=>{const e=v??H.current??G.current;return{"x-inkeep-client-timezone":Intl.DateTimeFormat().resolvedOptions().timeZone,"x-inkeep-client-timestamp":new Date().toISOString(),"x-inkeep-invocation-type":"chat_widget",...E?{"x-inkeep-app-id":E}:{},...e?{Authorization:`Bearer ${e}`}:{},...J.current?{"inkeep-filters":J.current}:{},...K.current?{"x-inkeep-user-properties":K.current}:{},...ye.current}},prepareSendMessagesRequest:async e=>{const i=await fe.current(),s=e.messages[e.messages.length-1];return s||console.warn("[useInkeepChat] prepareSendMessagesRequest called with empty messages array"),{body:{...e.body,id:e.id,messages:s?[s]:[],trigger:e.trigger,messageId:e.messageId},headers:{...e.headers,...i}}},body:{requestContext:de}}),[ge,de,E,v]),{messages:P,sendMessage:W,addToolApprovalResponse:Z,status:Re,setMessages:g,stop:L,error:N}=et.useChat({transport:$e,onData(e){_(e.type,e.data)},async onFinish(){_("completion",{conversationId:u}),await h({eventName:"assistant_message_received",properties:{conversationId:u}}),h({eventName:"assistant_answer_displayed",properties:{conversationId:u}})},onError(e){console.error("onError",{code:e.code,message:e.message});const i=oe||v?null:p.resolveStreamingAuthError(e);if(i!==null&&y.current<1){y.current++;const a=j.current,r=V.current;(async()=>{if(i==="session"&&ce){const c=await ue();if(!c)throw new Error("Auth token refresh failed");H.current=c}else if(i==="session"){const c=await z();c&&(G.current=c)}else l();if(a){Z(a);return}r&&(g(c=>{let n=[...c];return n.at(-1)?.role==="assistant"&&(n=n.slice(0,-1)),n.at(-1)?.role==="user"&&(n=n.slice(0,-1)),n}),W(r.files?.length?{parts:[{type:"text",text:r.content},...r.files]}:{text:r.content},{body:r.body}))})().catch(()=>{y.current=0,l(),g(c=>{const n=[...c],k=n[n.length-1];if(!k)return n;const M=p.DEFAULT_ERROR_MESSAGE;return k.role==="user"?n.push({id:D.generateUid(16),role:"assistant",parts:[{type:"text",text:M}]}):k.parts=[{type:"text",text:M}],n})});return}y.current=0,i!==null&&l();const s=p.isRecoverableError(e),d=Be(e);if(h({eventName:"chat_error",properties:{conversationId:u,error:e.message}}),s){se({title:"Request failed",message:d},p.RECOVERABLE_NOTIFICATION_DURATION_MS),g(r=>{let o=[...r];return o.at(-1)?.role==="assistant"&&(o=o.slice(0,-1)),o.at(-1)?.role==="user"&&(o=o.slice(0,-1)),o});const a=V.current?.content;a&&S(a),te.current=e;return}g(a=>{const r=[...a],o=r[r.length-1];return o&&(o.role==="user"?r.push({id:D.generateUid(16),role:"assistant",parts:[{type:"text",text:d}]}):o.parts=[{type:"text",text:d}]),r})}}),Ce=t.useRef(f);t.useEffect(()=>{const e=Ce.current;Ce.current=f,e!==f&&(L(),T(null),g([]),m(""),S(""),Q([]),l())},[f,L,g,m,l]);const Se=Re==="submitted",X=Re==="streaming",ze=t.useMemo(()=>{const e=a=>{if(!a||typeof a!="object")return!1;const r=a;return typeof r.type=="string"&&r.type.startsWith("tool-")},s=[...P??[]].reverse().find(a=>a.role==="assistant");if(!s)return!1;const d=s.parts?.at(-1);return!(!e(d)||d.state!=="output-available"||!d.approval?.id||X)},[P,X]),[Ge,Y]=t.useState(!1),Ee=X||ze&&!Ge,Ae=Se||Ee,He=P.length===0,ee=!C.trim()&&O.length===0||Ae,Ke=it.useMediaQuery("(max-width: 768px)"),[Ve,T]=t.useState(null),te=t.useRef(null);t.useEffect(()=>{if(N){if(te.current===N){te.current=null;return}T(N)}},[N]);const je=()=>T(null),{inputNotification:Je,showInputNotification:se,clearInputNotification:Qe}=vt.useInputNotification(),be=t.useRef(null);t.useEffect(()=>{Pe?.(C)},[C]);const We=e=>{e.key==="Enter"&&!e.shiftKey&&!ee&&!e.nativeEvent.isComposing&&(e.preventDefault(),re())},re=async(e=C)=>{if(ee&&(!e||e.trim().length===0)&&O.length===0)return;const i=O;Q([]),S(""),y.current=0,j.current=null,Y(!1),await h({eventName:"user_message_submitted",properties:{conversationId:u}});let s=u;s||(s=`conv_${D.generateUid(16)}`,m(s)),_e(s);const d=I.current;I.current=void 0;let a,r;if(d?.length){let o;try{o=await Promise.all(i.map(c=>{const n=we.normalizeFileType(c);return new Promise((k,M)=>{const F=new FileReader;F.onload=()=>{if(typeof F.result!="string"){M(new Error(`Failed to read file "${n.name}"`));return}k({type:"file",url:F.result,mediaType:n.type,filename:n.name})},F.onerror=()=>M(new Error(`Failed to read file "${n.name}"`)),F.readAsDataURL(n)})}))}catch{se({title:"Failed to attach files",message:"Could not read one or more files. Please try again."});return}r=[...o,...d],a=e.trim()?{parts:[{type:"text",text:e},...r]}:{parts:r}}else if(i.length>0){const o=new DataTransfer;for(const n of i)o.items.add(we.normalizeFileType(n));const c=o.files;a=e.trim()?{text:e,files:c}:{files:c}}else a={text:e};V.current={content:e,body:{conversationId:s},files:r},W(a,{body:{conversationId:s}})},Ze=t.useCallback(e=>{y.current=0,j.current=e,Y(!1),Z(e)},[Z]),ne=t.useCallback(()=>{Y(!0),L().then(()=>{_("aborted",{conversationId:u})})},[L,u,_]),Ie=()=>{je(),g([]),m(""),l(),I.current=A?.length?A:void 0,h({eventName:"chat_clear_button_clicked",properties:{conversationId:u}})},w=t.useCallback((e,i)=>{T(null),g(i),m(e),l(),I.current=void 0},[g,m,l]),Te=t.useCallback(async(e,i)=>{ne(),w(e,[]),ve(!0);try{const s=await me(e,i);if(s===null)return!1;const a=s[s.length-1]?.role==="user"?[...s,{id:D.generateUid(16),role:"assistant",parts:[{type:"text",text:"This session was interrupted. Please check back in a few minutes or start a new conversation."}]}]:s;return w(e,a),!0}finally{i?.aborted||ve(!1)}},[w,me,ne]);dt.useInitialConversation({conversationId:R.conversationId,effectiveAuthToken:he,loadAndRestoreSession:Te,onLoadFailed:()=>w("",[])});const{openForm:Xe}=ft.useChatForm(),Ye=gt.useWidget();return t.useImperativeHandle(R.chatFunctionsRef,()=>({submitMessage:re,updateInputMessage(e){S(e)},clearChat:Ie,openForm:e=>{Ye?.setView("chat"),Xe(e,void 0)},focusInput:()=>{be.current?.focus()}})),{messages:P,sendMessage:W,addToolApprovalResponse:Ze,isLoading:Se,isStreaming:Ee,isBusy:Ae,error:Ve,setError:T,isSubmitDisabled:ee,input:C,handleInputChange:qe,handleInputKeyDown:We,handleSubmit:re,stop:ne,clear:Ie,inputRef:be,isMobile:Ke,files:O,setFiles:Q,isNewChat:He,conversationId:u,restoreSession:w,loadAndRestoreSession:Te,isSessionLoading:De,authToken:f?q:$,refreshSession:f?ue:z,getCaptchaHeader:b,invalidateCaptcha:l,inputNotification:Je,showInputNotification:se,clearInputNotification:Qe}};exports.useInkeepChat=yt;
@@ -50,6 +50,8 @@ export declare const useInkeepChat: () => {
50
50
  isSessionLoading: boolean;
51
51
  authToken: string | null;
52
52
  refreshSession: () => Promise<string | null>;
53
+ getCaptchaHeader: () => Promise<Record<string, string>>;
54
+ invalidateCaptcha: () => void;
53
55
  inputNotification: InputNotification | null;
54
56
  showInputNotification: (n: InputNotification, durationMs?: number) => void;
55
57
  clearInputNotification: () => void;