@inkeep/agents-ui 0.15.20 → 0.15.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/primitives/components/embedded-chat/use-inkeep-chat.cjs +2 -2
- package/dist/primitives/components/embedded-chat/use-inkeep-chat.js +231 -212
- package/dist/primitives/components/embedded-chat.cjs +1 -1
- package/dist/primitives/components/embedded-chat.js +89 -87
- package/dist/primitives/hooks/use-anonymous-session.cjs +1 -1
- package/dist/primitives/hooks/use-anonymous-session.d.ts +2 -1
- package/dist/primitives/hooks/use-anonymous-session.js +79 -65
- package/dist/primitives/hooks/use-auth-token.cjs +1 -0
- package/dist/primitives/hooks/use-auth-token.d.ts +5 -0
- package/dist/primitives/hooks/use-auth-token.js +54 -0
- package/dist/primitives/providers/base-events-provider.cjs +1 -1
- package/dist/primitives/providers/base-events-provider.js +1 -1
- package/dist/primitives/providers/chat-history-provider.cjs +1 -1
- package/dist/primitives/providers/chat-history-provider.js +90 -85
- package/dist/types/config/base.d.ts +7 -0
- package/package.json +1 -1
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const He=require("@ai-sdk/react"),We=require("ai"),t=require("react"),$e=require("../modal/modal-provider.cjs"),je=require("../../providers/chat-bubble-provider.cjs"),ze=require("../../providers/sidebar-chat-provider.cjs"),Je=require("../../providers/config-provider.cjs"),Ke=require("./use-captcha.cjs"),Ge=require("../../hooks/use-media-query.cjs"),Qe=require("../../hooks/use-anonymous-session.cjs"),Ve=require("../../hooks/use-auth-token.cjs"),Ze=require("../../hooks/use-conversation-loader.cjs"),x=require("../../utils/generate-uid.cjs"),Xe=require("../../providers/base-events-provider.cjs"),Ye=require("../../providers/chat-form-provider.cjs"),et=require("../../providers/widget-provider.cjs"),tt=require("@radix-ui/react-use-controllable-state"),st=require("../../hooks/use-streaming-events.cjs"),rt=require("../../hooks/use-inkeep-api-client.cjs");function nt(f){const h=f.message??"";let s=Number(f.code)||Number(f.statusCode);if(Number.isNaN(s))try{s=Number(JSON.parse(h).status)}catch{}const l=rt.parseAuthError(s,{detail:h});return l!==null?l:s===401?"session":null}const P=`Hmm..
|
|
2
2
|
|
|
3
|
-
It seems I might be having some issues right now. Please clear the chat and try again.`,
|
|
3
|
+
It seems I might be having some issues right now. Please clear the chat and try again.`,at=()=>{const{baseSettings:f,aiChatSettings:h}=Je.useInkeepConfig(),[s="",l]=tt.useControllableState({prop:h.conversationId,defaultProp:h.conversationId??""}),ve=$e.useModal(),ye=je.useOptionalChatBubble(),be=ze.useOptionalSidebarChat(),{logEvent:g}=Xe.useBaseEvents(),{setConversationId:Ce,emitToParent:k}=st.useStreamingEvents(),Q=t.useRef(s);t.useEffect(()=>{const e=Q.current;Q.current=s,e!==s&&g({eventName:"chat_conversation_changed",properties:{conversationId:s,previousConversationId:e}})},[s,g]);const[y,A]=t.useState(""),Se=e=>A(e.target.value),{shouldBypassCaptcha:V,filters:Z,privacyPreferences:Re,userProperties:N}=f,{authToken:I,refreshToken:X}=Ve.useAuthToken(),Y=!!f.getAuthToken,d=!!I,{onInputMessageChange:ke,filters:ee,baseUrl:E,agentUrl:Ae,context:te,headers:se,appId:b,apiKey:m}=h,Ie=ve?.isOpen??ye?.isOpen??be?.isOpen??!0,{getCaptchaHeader:C,invalidate:c}=Ke.useCaptcha({baseUrl:E,shouldBypassCaptcha:V||!!m,shouldMakeInitialRequest:Ie}),re=t.useRef(C);re.current=C;const ne=Ae||`${E}/run/api/chat`,{sessionToken:O,refreshSession:_}=Qe.useAnonymousSession({baseUrl:E,appId:b,getCaptchaHeader:C,invalidateCaptcha:c,optOutAllAnalytics:Re?.optOutAllAnalytics,enabled:!d}),{loadConversation:ae}=Ze.useConversationLoader({baseUrl:E,appId:b,authToken:m??(d?I:O),getCaptchaHeader:C,invalidateCaptcha:c,refreshSession:m||d?void 0:_}),[Ee,oe]=t.useState(!1),F=t.useRef(null);F.current=O;const U=t.useRef(null);U.current=I;const D=t.useRef(void 0);D.current=N&&Object.keys(N).length>0?JSON.stringify(N):void 0;const v=t.useRef(0),ie=t.useRef(null),B=t.useRef(null),ue=t.useRef(se);ue.current=se;const L=t.useRef(void 0);L.current=Z||ee?JSON.stringify({...Z,...ee}):void 0;const we=e=>{switch(e.code){case 400:return e.message;case 401:return Y?"Authentication failed. Please try again.":P;case 403:return`There seems to be a configuration error. Please contact ${f.organizationDisplayName??"Administrator"}`;default:return P}},[Te,S]=t.useState([]),[Me,w]=t.useState(null),qe=t.useMemo(()=>new We.DefaultChatTransport({api:ne,headers:()=>{const e=m??U.current??F.current;return{"x-inkeep-client-timezone":Intl.DateTimeFormat().resolvedOptions().timeZone,"x-inkeep-client-timestamp":new Date().toISOString(),...b?{"x-inkeep-app-id":b}:{},...e?{Authorization:`Bearer ${e}`}:{},...L.current?{"inkeep-filters":L.current}:{},...D.current?{"x-inkeep-user-properties":D.current}:{},...ue.current}},prepareSendMessagesRequest:async e=>{const r=await re.current();return{body:{...e.body,id:e.id,messages:e.messages,trigger:e.trigger,messageId:e.messageId},headers:{...e.headers,...r}}},body:{requestContext:te}}),[ne,te,b,m]),{messages:T,sendMessage:H,addToolApprovalResponse:W,status:ce,setMessages:p,stop:M,error:$}=He.useChat({transport:qe,onData(e){k(e.type,e.data)},async onFinish(){k("completion",{conversationId:s}),await g({eventName:"assistant_message_received",properties:{conversationId:s}}),g({eventName:"assistant_answer_displayed",properties:{conversationId:s}})},onError(e){console.error("onError",{code:e.code,message:e.message});const r=V||m?null:nt(e);if(r!==null&&v.current<1){v.current++;const o=B.current,a=ie.current;(async()=>{if(r==="session"&&Y){const n=await X();if(!n)throw new Error("Auth token refresh failed");U.current=n}else if(r==="session"){const n=await _();n&&(F.current=n)}else c();if(o){W(o);return}a&&(p(n=>{let u=[...n];return u.at(-1)?.role==="assistant"&&(u=u.slice(0,-1)),u.at(-1)?.role==="user"&&(u=u.slice(0,-1)),u}),H({text:a.content},{body:a.body}))})().catch(()=>{v.current=0,c(),p(n=>{const u=[...n],G=u[u.length-1];if(!G)return u;const me=P;return G.role==="user"?u.push({id:x.generateUid(16),role:"assistant",parts:[{type:"text",text:me}]}):G.parts=[{type:"text",text:me}],u})});return}v.current=0,r!==null&&c(),g({eventName:"chat_error",properties:{conversationId:s,error:e.message}}),p(o=>{const a=[...o],i=a[a.length-1];if(i){const n=we(e);i.role==="user"?a.push({id:x.generateUid(16),role:"assistant",parts:[{type:"text",text:n}]}):i.parts=[{type:"text",text:n}]}return a})}}),le=t.useRef(d);t.useEffect(()=>{const e=le.current;le.current=d,e!==d&&(M(),R(null),p([]),l(""),A(""),S([]),w(null),c())},[d,M,p,l,c]);const de=ce==="submitted",j=ce==="streaming",xe=t.useMemo(()=>{const e=i=>{if(!i||typeof i!="object")return!1;const n=i;return typeof n.type=="string"&&n.type.startsWith("tool-")},o=[...T??[]].reverse().find(i=>i.role==="assistant");if(!o)return!1;const a=o.parts?.at(-1);return!(!e(a)||a.state!=="output-available"||!a.approval?.id||j)},[T,j]),pe=j||xe,fe=de||pe,Pe=T.length===0,z=!y.trim()||fe,Ne=Ge.useMediaQuery("(max-width: 768px)"),[Oe,R]=t.useState(null);t.useEffect(()=>{$&&R($)},[$]);const _e=()=>R(null),he=t.useRef(null);t.useEffect(()=>{ke?.(y)},[y]);const Fe=e=>{e.key==="Enter"&&!e.shiftKey&&!z&&!e.nativeEvent.isComposing&&(e.preventDefault(),J())},J=async(e=y)=>{if(z&&(!e||e.trim().length===0))return;S([]),A(""),v.current=0,B.current=null,await g({eventName:"user_message_submitted",properties:{conversationId:s}});let r=s;r||(r=`conv_${x.generateUid(16)}`,l(r)),Ce(r),ie.current={content:e,body:{conversationId:r}},H({text:e},{body:{conversationId:r}})},Ue=t.useCallback(e=>{v.current=0,B.current=e,W(e)},[W]),K=t.useCallback(()=>{M().then(()=>{k("aborted",{conversationId:s})})},[M,s,k]),ge=()=>{_e(),p([]),l(""),S([]),w(null),c(),g({eventName:"chat_clear_button_clicked",properties:{conversationId:s}})},q=t.useCallback((e,r)=>{R(null),p(r),l(e),S([]),w(null),c()},[p,l,c]),De=t.useCallback(async(e,r)=>{K(),q(e,[]),oe(!0);try{const o=await ae(e,r),a=o?.[o.length-1],i=o!==null&&a?.role==="user"?[...o,{id:x.generateUid(16),role:"assistant",parts:[{type:"text",text:"This session was interrupted. Please check back in a few minutes or start a new conversation."}]}]:o;i!==null&&q(e,i)}finally{r?.aborted||oe(!1)}},[q,ae,K]),{openForm:Be}=Ye.useChatForm(),Le=et.useWidget();return t.useImperativeHandle(h.chatFunctionsRef,()=>({submitMessage:J,updateInputMessage(e){A(e)},clearChat:ge,openForm:e=>{Le?.setView("chat"),Be(e,void 0)},focusInput:()=>{he.current?.focus()}})),{messages:T,sendMessage:H,addToolApprovalResponse:Ue,isLoading:de,isStreaming:pe,isBusy:fe,error:Oe,setError:R,isSubmitDisabled:z,input:y,handleInputChange:Se,handleInputKeyDown:Fe,handleSubmit:J,stop:K,clear:ge,inputRef:he,isMobile:Ne,messageAttachments:Te,setMessageAttachments:S,selectedWorkflow:Me,setSelectedWorkflow:w,isNewChat:Pe,conversationId:s,restoreSession:q,loadAndRestoreSession:De,isSessionLoading:Ee,sessionToken:d?I:O,refreshSession:d?X:_,getCaptchaHeader:C,invalidateCaptcha:c}};exports.DEFAULT_ERROR_MESSAGE=P;exports.useInkeepChat=at;
|
|
@@ -1,108 +1,118 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useChat as
|
|
3
|
-
import { DefaultChatTransport as
|
|
4
|
-
import { useRef as
|
|
5
|
-
import { useModal as
|
|
6
|
-
import { useOptionalChatBubble as
|
|
7
|
-
import { useOptionalSidebarChat as
|
|
8
|
-
import { useInkeepConfig as
|
|
9
|
-
import { useCaptcha as
|
|
10
|
-
import { useMediaQuery as
|
|
11
|
-
import { useAnonymousSession as
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
2
|
+
import { useChat as Je } from "@ai-sdk/react";
|
|
3
|
+
import { DefaultChatTransport as Ke } from "ai";
|
|
4
|
+
import { useRef as c, useEffect as O, useState as A, useMemo as Ce, useCallback as _, useImperativeHandle as je } from "react";
|
|
5
|
+
import { useModal as Ge } from "../modal/modal-provider.js";
|
|
6
|
+
import { useOptionalChatBubble as Qe } from "../../providers/chat-bubble-provider.js";
|
|
7
|
+
import { useOptionalSidebarChat as Ve } from "../../providers/sidebar-chat-provider.js";
|
|
8
|
+
import { useInkeepConfig as Ze } from "../../providers/config-provider.js";
|
|
9
|
+
import { useCaptcha as Xe } from "./use-captcha.js";
|
|
10
|
+
import { useMediaQuery as Ye } from "../../hooks/use-media-query.js";
|
|
11
|
+
import { useAnonymousSession as et } from "../../hooks/use-anonymous-session.js";
|
|
12
|
+
import { useAuthToken as tt } from "../../hooks/use-auth-token.js";
|
|
13
|
+
import { useConversationLoader as st } from "../../hooks/use-conversation-loader.js";
|
|
14
|
+
import { generateUid as F } from "../../utils/generate-uid.js";
|
|
15
|
+
import { useBaseEvents as rt } from "../../providers/base-events-provider.js";
|
|
16
|
+
import { useChatForm as nt } from "../../providers/chat-form-provider.js";
|
|
17
|
+
import { useWidget as ot } from "../../providers/widget-provider.js";
|
|
18
|
+
import { useControllableState as at } from "@radix-ui/react-use-controllable-state";
|
|
19
|
+
import { useStreamingEvents as it } from "../../hooks/use-streaming-events.js";
|
|
20
|
+
import { parseAuthError as ct } from "../../hooks/use-inkeep-api-client.js";
|
|
21
|
+
function ut(m) {
|
|
22
|
+
const f = m.message ?? "";
|
|
23
|
+
let t = Number(m.code) || Number(m.statusCode);
|
|
23
24
|
if (Number.isNaN(t))
|
|
24
25
|
try {
|
|
25
|
-
t = Number(JSON.parse(
|
|
26
|
+
t = Number(JSON.parse(f).status);
|
|
26
27
|
} catch {
|
|
27
28
|
}
|
|
28
|
-
const
|
|
29
|
-
return
|
|
29
|
+
const l = ct(t, { detail: f });
|
|
30
|
+
return l !== null ? l : t === 401 ? "session" : null;
|
|
30
31
|
}
|
|
31
|
-
const
|
|
32
|
+
const Z = `Hmm..
|
|
32
33
|
|
|
33
|
-
It seems I might be having some issues right now. Please clear the chat and try again.`,
|
|
34
|
-
const { baseSettings:
|
|
35
|
-
prop:
|
|
36
|
-
defaultProp:
|
|
37
|
-
}),
|
|
38
|
-
|
|
39
|
-
const e =
|
|
40
|
-
|
|
34
|
+
It seems I might be having some issues right now. Please clear the chat and try again.`, xt = () => {
|
|
35
|
+
const { baseSettings: m, aiChatSettings: f } = Ze(), [t = "", l] = at({
|
|
36
|
+
prop: f.conversationId,
|
|
37
|
+
defaultProp: f.conversationId ?? ""
|
|
38
|
+
}), Se = Ge(), ke = Qe(), Ae = Ve(), { logEvent: h } = rt(), { setConversationId: Ie, emitToParent: I } = it(), X = c(t);
|
|
39
|
+
O(() => {
|
|
40
|
+
const e = X.current;
|
|
41
|
+
X.current = t, e !== t && h({
|
|
41
42
|
eventName: "chat_conversation_changed",
|
|
42
43
|
properties: {
|
|
43
44
|
conversationId: t,
|
|
44
45
|
previousConversationId: e
|
|
45
46
|
}
|
|
46
47
|
});
|
|
47
|
-
}, [t,
|
|
48
|
-
const [
|
|
49
|
-
onInputMessageChange:
|
|
50
|
-
filters:
|
|
51
|
-
baseUrl:
|
|
52
|
-
agentUrl:
|
|
53
|
-
context:
|
|
54
|
-
headers:
|
|
55
|
-
appId:
|
|
56
|
-
apiKey:
|
|
57
|
-
} =
|
|
58
|
-
baseUrl:
|
|
59
|
-
shouldBypassCaptcha:
|
|
60
|
-
shouldMakeInitialRequest:
|
|
61
|
-
}),
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
baseUrl:
|
|
65
|
-
appId:
|
|
66
|
-
getCaptchaHeader:
|
|
67
|
-
invalidateCaptcha:
|
|
68
|
-
optOutAllAnalytics:
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
refreshSession:
|
|
77
|
-
}), [
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
const
|
|
48
|
+
}, [t, h]);
|
|
49
|
+
const [y, w] = A(""), we = (e) => w(e.target.value), { shouldBypassCaptcha: Y, filters: ee, privacyPreferences: Re, userProperties: P } = m, { authToken: R, refreshToken: te } = tt(), se = !!m.getAuthToken, p = !!R, {
|
|
50
|
+
onInputMessageChange: Te,
|
|
51
|
+
filters: re,
|
|
52
|
+
baseUrl: T,
|
|
53
|
+
agentUrl: Me,
|
|
54
|
+
context: ne,
|
|
55
|
+
headers: oe,
|
|
56
|
+
appId: b,
|
|
57
|
+
apiKey: g
|
|
58
|
+
} = f, xe = Se?.isOpen ?? ke?.isOpen ?? Ae?.isOpen ?? !0, { getCaptchaHeader: C, invalidate: u } = Xe({
|
|
59
|
+
baseUrl: T,
|
|
60
|
+
shouldBypassCaptcha: Y || !!g,
|
|
61
|
+
shouldMakeInitialRequest: xe
|
|
62
|
+
}), ae = c(C);
|
|
63
|
+
ae.current = C;
|
|
64
|
+
const ie = Me || `${T}/run/api/chat`, { sessionToken: D, refreshSession: B } = et({
|
|
65
|
+
baseUrl: T,
|
|
66
|
+
appId: b,
|
|
67
|
+
getCaptchaHeader: C,
|
|
68
|
+
invalidateCaptcha: u,
|
|
69
|
+
optOutAllAnalytics: Re?.optOutAllAnalytics,
|
|
70
|
+
enabled: !p
|
|
71
|
+
}), { loadConversation: ce } = st({
|
|
72
|
+
baseUrl: T,
|
|
73
|
+
appId: b,
|
|
74
|
+
authToken: g ?? (p ? R : D),
|
|
75
|
+
getCaptchaHeader: C,
|
|
76
|
+
invalidateCaptcha: u,
|
|
77
|
+
refreshSession: g || p ? void 0 : B
|
|
78
|
+
}), [Ee, ue] = A(!1), H = c(null);
|
|
79
|
+
H.current = D;
|
|
80
|
+
const L = c(null);
|
|
81
|
+
L.current = R;
|
|
82
|
+
const U = c(void 0);
|
|
83
|
+
U.current = P && Object.keys(P).length > 0 ? JSON.stringify(P) : void 0;
|
|
84
|
+
const v = c(0), le = c(null), W = c(null), pe = c(oe);
|
|
85
|
+
pe.current = oe;
|
|
86
|
+
const $ = c(void 0);
|
|
87
|
+
$.current = ee || re ? JSON.stringify({ ...ee, ...re }) : void 0;
|
|
88
|
+
const Ne = (e) => {
|
|
84
89
|
switch (e.code) {
|
|
85
90
|
case 400:
|
|
86
91
|
return e.message;
|
|
92
|
+
case 401:
|
|
93
|
+
return se ? "Authentication failed. Please try again." : Z;
|
|
87
94
|
case 403:
|
|
88
|
-
return `There seems to be a configuration error. Please contact ${
|
|
95
|
+
return `There seems to be a configuration error. Please contact ${m.organizationDisplayName ?? "Administrator"}`;
|
|
89
96
|
default:
|
|
90
|
-
return
|
|
97
|
+
return Z;
|
|
91
98
|
}
|
|
92
|
-
}, [
|
|
93
|
-
() => new
|
|
94
|
-
api:
|
|
95
|
-
headers: () =>
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
99
|
+
}, [Oe, S] = A([]), [_e, M] = A(null), Fe = Ce(
|
|
100
|
+
() => new Ke({
|
|
101
|
+
api: ie,
|
|
102
|
+
headers: () => {
|
|
103
|
+
const e = g ?? L.current ?? H.current;
|
|
104
|
+
return {
|
|
105
|
+
"x-inkeep-client-timezone": Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
106
|
+
"x-inkeep-client-timestamp": (/* @__PURE__ */ new Date()).toISOString(),
|
|
107
|
+
...b ? { "x-inkeep-app-id": b } : {},
|
|
108
|
+
...e ? { Authorization: `Bearer ${e}` } : {},
|
|
109
|
+
...$.current ? { "inkeep-filters": $.current } : {},
|
|
110
|
+
...U.current ? { "x-inkeep-user-properties": U.current } : {},
|
|
111
|
+
...pe.current
|
|
112
|
+
};
|
|
113
|
+
},
|
|
104
114
|
prepareSendMessagesRequest: async (e) => {
|
|
105
|
-
const s = await
|
|
115
|
+
const s = await ae.current();
|
|
106
116
|
return {
|
|
107
117
|
body: {
|
|
108
118
|
...e.body,
|
|
@@ -118,30 +128,30 @@ It seems I might be having some issues right now. Please clear the chat and try
|
|
|
118
128
|
};
|
|
119
129
|
},
|
|
120
130
|
body: {
|
|
121
|
-
requestContext:
|
|
131
|
+
requestContext: ne
|
|
122
132
|
}
|
|
123
133
|
}),
|
|
124
|
-
[
|
|
134
|
+
[ie, ne, b, g]
|
|
125
135
|
), {
|
|
126
|
-
messages:
|
|
127
|
-
sendMessage:
|
|
128
|
-
addToolApprovalResponse:
|
|
129
|
-
status:
|
|
130
|
-
setMessages:
|
|
131
|
-
stop:
|
|
132
|
-
error:
|
|
133
|
-
} =
|
|
134
|
-
transport:
|
|
136
|
+
messages: x,
|
|
137
|
+
sendMessage: q,
|
|
138
|
+
addToolApprovalResponse: z,
|
|
139
|
+
status: de,
|
|
140
|
+
setMessages: d,
|
|
141
|
+
stop: E,
|
|
142
|
+
error: J
|
|
143
|
+
} = Je({
|
|
144
|
+
transport: Fe,
|
|
135
145
|
onData(e) {
|
|
136
|
-
|
|
146
|
+
I(e.type, e.data);
|
|
137
147
|
},
|
|
138
148
|
async onFinish() {
|
|
139
|
-
|
|
149
|
+
I("completion", { conversationId: t }), await h({
|
|
140
150
|
eventName: "assistant_message_received",
|
|
141
151
|
properties: {
|
|
142
152
|
conversationId: t
|
|
143
153
|
}
|
|
144
|
-
}),
|
|
154
|
+
}), h({
|
|
145
155
|
eventName: "assistant_answer_displayed",
|
|
146
156
|
properties: {
|
|
147
157
|
conversationId: t
|
|
@@ -150,178 +160,187 @@ It seems I might be having some issues right now. Please clear the chat and try
|
|
|
150
160
|
},
|
|
151
161
|
onError(e) {
|
|
152
162
|
console.error("onError", { code: e.code, message: e.message });
|
|
153
|
-
const s =
|
|
154
|
-
if (s !== null &&
|
|
155
|
-
|
|
156
|
-
const
|
|
163
|
+
const s = Y || g ? null : ut(e);
|
|
164
|
+
if (s !== null && v.current < 1) {
|
|
165
|
+
v.current++;
|
|
166
|
+
const o = W.current, n = le.current;
|
|
157
167
|
(async () => {
|
|
158
|
-
if (s === "session") {
|
|
159
|
-
const
|
|
160
|
-
|
|
168
|
+
if (s === "session" && se) {
|
|
169
|
+
const r = await te();
|
|
170
|
+
if (!r) throw new Error("Auth token refresh failed");
|
|
171
|
+
L.current = r;
|
|
172
|
+
} else if (s === "session") {
|
|
173
|
+
const r = await B();
|
|
174
|
+
r && (H.current = r);
|
|
161
175
|
} else
|
|
162
|
-
|
|
163
|
-
if (
|
|
164
|
-
|
|
176
|
+
u();
|
|
177
|
+
if (o) {
|
|
178
|
+
z(o);
|
|
165
179
|
return;
|
|
166
180
|
}
|
|
167
|
-
|
|
168
|
-
let
|
|
169
|
-
return
|
|
170
|
-
}),
|
|
181
|
+
n && (d((r) => {
|
|
182
|
+
let i = [...r];
|
|
183
|
+
return i.at(-1)?.role === "assistant" && (i = i.slice(0, -1)), i.at(-1)?.role === "user" && (i = i.slice(0, -1)), i;
|
|
184
|
+
}), q({ text: n.content }, { body: n.body }));
|
|
171
185
|
})().catch(() => {
|
|
172
|
-
|
|
173
|
-
const
|
|
174
|
-
if (!
|
|
175
|
-
const
|
|
176
|
-
return
|
|
177
|
-
id:
|
|
186
|
+
v.current = 0, u(), d((r) => {
|
|
187
|
+
const i = [...r], V = i[i.length - 1];
|
|
188
|
+
if (!V) return i;
|
|
189
|
+
const be = Z;
|
|
190
|
+
return V.role === "user" ? i.push({
|
|
191
|
+
id: F(16),
|
|
178
192
|
role: "assistant",
|
|
179
|
-
parts: [{ type: "text", text:
|
|
180
|
-
}) :
|
|
193
|
+
parts: [{ type: "text", text: be }]
|
|
194
|
+
}) : V.parts = [{ type: "text", text: be }], i;
|
|
181
195
|
});
|
|
182
196
|
});
|
|
183
197
|
return;
|
|
184
198
|
}
|
|
185
|
-
|
|
199
|
+
v.current = 0, s !== null && u(), h({
|
|
186
200
|
eventName: "chat_error",
|
|
187
201
|
properties: {
|
|
188
202
|
conversationId: t,
|
|
189
203
|
error: e.message
|
|
190
204
|
}
|
|
191
|
-
}),
|
|
192
|
-
const
|
|
193
|
-
if (
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
id:
|
|
205
|
+
}), d((o) => {
|
|
206
|
+
const n = [...o], a = n[n.length - 1];
|
|
207
|
+
if (a) {
|
|
208
|
+
const r = Ne(e);
|
|
209
|
+
a.role === "user" ? n.push({
|
|
210
|
+
id: F(16),
|
|
197
211
|
role: "assistant",
|
|
198
|
-
parts: [{ type: "text", text:
|
|
199
|
-
}) :
|
|
212
|
+
parts: [{ type: "text", text: r }]
|
|
213
|
+
}) : a.parts = [{ type: "text", text: r }];
|
|
200
214
|
}
|
|
201
|
-
return
|
|
215
|
+
return n;
|
|
202
216
|
});
|
|
203
217
|
}
|
|
204
|
-
}),
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
218
|
+
}), me = c(p);
|
|
219
|
+
O(() => {
|
|
220
|
+
const e = me.current;
|
|
221
|
+
me.current = p, e !== p && (E(), k(null), d([]), l(""), w(""), S([]), M(null), u());
|
|
222
|
+
}, [p, E, d, l, u]);
|
|
223
|
+
const fe = de === "submitted", K = de === "streaming", Pe = Ce(() => {
|
|
224
|
+
const e = (a) => {
|
|
225
|
+
if (!a || typeof a != "object") return !1;
|
|
226
|
+
const r = a;
|
|
227
|
+
return typeof r.type == "string" && r.type.startsWith("tool-");
|
|
228
|
+
}, o = [...x ?? []].reverse().find((a) => a.role === "assistant");
|
|
229
|
+
if (!o) return !1;
|
|
230
|
+
const n = o.parts?.at(-1);
|
|
231
|
+
return !(!e(n) || n.state !== "output-available" || !n.approval?.id || K);
|
|
232
|
+
}, [x, K]), he = K || Pe, ge = fe || he, De = x.length === 0, j = !y.trim() || ge, Be = Ye("(max-width: 768px)"), [He, k] = A(null);
|
|
233
|
+
O(() => {
|
|
234
|
+
J && k(J);
|
|
235
|
+
}, [J]);
|
|
236
|
+
const Le = () => k(null), ve = c(null);
|
|
237
|
+
O(() => {
|
|
238
|
+
Te?.(y);
|
|
239
|
+
}, [y]);
|
|
240
|
+
const Ue = (e) => {
|
|
241
|
+
e.key === "Enter" && !e.shiftKey && !j && !e.nativeEvent.isComposing && (e.preventDefault(), G());
|
|
242
|
+
}, G = async (e = y) => {
|
|
243
|
+
if (j && (!e || e.trim().length === 0)) return;
|
|
244
|
+
S([]), w(""), v.current = 0, W.current = null, await h({
|
|
226
245
|
eventName: "user_message_submitted",
|
|
227
246
|
properties: {
|
|
228
247
|
conversationId: t
|
|
229
248
|
}
|
|
230
249
|
});
|
|
231
250
|
let s = t;
|
|
232
|
-
s || (s = `conv_${
|
|
251
|
+
s || (s = `conv_${F(16)}`, l(s)), Ie(s), le.current = {
|
|
233
252
|
content: e,
|
|
234
253
|
body: { conversationId: s }
|
|
235
|
-
},
|
|
254
|
+
}, q(
|
|
236
255
|
{ text: e },
|
|
237
256
|
{
|
|
238
257
|
body: { conversationId: s }
|
|
239
258
|
}
|
|
240
259
|
);
|
|
241
|
-
},
|
|
260
|
+
}, We = _(
|
|
242
261
|
(e) => {
|
|
243
|
-
|
|
262
|
+
v.current = 0, W.current = e, z(e);
|
|
244
263
|
},
|
|
245
|
-
[
|
|
246
|
-
),
|
|
247
|
-
|
|
248
|
-
|
|
264
|
+
[z]
|
|
265
|
+
), Q = _(() => {
|
|
266
|
+
E().then(() => {
|
|
267
|
+
I("aborted", { conversationId: t });
|
|
249
268
|
});
|
|
250
|
-
}, [
|
|
251
|
-
|
|
269
|
+
}, [E, t, I]), ye = () => {
|
|
270
|
+
Le(), d([]), l(""), S([]), M(null), u(), h({
|
|
252
271
|
eventName: "chat_clear_button_clicked",
|
|
253
272
|
properties: {
|
|
254
273
|
conversationId: t
|
|
255
274
|
}
|
|
256
275
|
});
|
|
257
|
-
},
|
|
276
|
+
}, N = _(
|
|
258
277
|
(e, s) => {
|
|
259
|
-
|
|
278
|
+
k(null), d(s), l(e), S([]), M(null), u();
|
|
260
279
|
},
|
|
261
|
-
[
|
|
262
|
-
),
|
|
280
|
+
[d, l, u]
|
|
281
|
+
), $e = _(
|
|
263
282
|
async (e, s) => {
|
|
264
|
-
|
|
283
|
+
Q(), N(e, []), ue(!0);
|
|
265
284
|
try {
|
|
266
|
-
const
|
|
267
|
-
id:
|
|
285
|
+
const o = await ce(e, s), n = o?.[o.length - 1], a = o !== null && n?.role === "user" ? [...o, {
|
|
286
|
+
id: F(16),
|
|
268
287
|
role: "assistant",
|
|
269
288
|
parts: [{ type: "text", text: "This session was interrupted. Please check back in a few minutes or start a new conversation." }]
|
|
270
|
-
}] :
|
|
271
|
-
|
|
289
|
+
}] : o;
|
|
290
|
+
a !== null && N(e, a);
|
|
272
291
|
} finally {
|
|
273
|
-
s?.aborted ||
|
|
292
|
+
s?.aborted || ue(!1);
|
|
274
293
|
}
|
|
275
294
|
},
|
|
276
|
-
[
|
|
277
|
-
), { openForm:
|
|
278
|
-
return
|
|
279
|
-
submitMessage:
|
|
295
|
+
[N, ce, Q]
|
|
296
|
+
), { openForm: qe } = nt(), ze = ot();
|
|
297
|
+
return je(f.chatFunctionsRef, () => ({
|
|
298
|
+
submitMessage: G,
|
|
280
299
|
updateInputMessage(e) {
|
|
281
|
-
|
|
300
|
+
w(e);
|
|
282
301
|
},
|
|
283
|
-
clearChat:
|
|
302
|
+
clearChat: ye,
|
|
284
303
|
openForm: (e) => {
|
|
285
|
-
|
|
304
|
+
ze?.setView("chat"), qe(e, void 0);
|
|
286
305
|
},
|
|
287
306
|
focusInput: () => {
|
|
288
|
-
|
|
307
|
+
ve.current?.focus();
|
|
289
308
|
}
|
|
290
309
|
})), {
|
|
291
|
-
messages:
|
|
292
|
-
sendMessage:
|
|
293
|
-
addToolApprovalResponse:
|
|
294
|
-
isLoading:
|
|
295
|
-
isStreaming:
|
|
296
|
-
isBusy:
|
|
297
|
-
error:
|
|
298
|
-
setError:
|
|
299
|
-
isSubmitDisabled:
|
|
300
|
-
input:
|
|
301
|
-
handleInputChange:
|
|
302
|
-
handleInputKeyDown:
|
|
303
|
-
handleSubmit:
|
|
304
|
-
stop:
|
|
305
|
-
clear:
|
|
306
|
-
inputRef:
|
|
307
|
-
isMobile:
|
|
310
|
+
messages: x,
|
|
311
|
+
sendMessage: q,
|
|
312
|
+
addToolApprovalResponse: We,
|
|
313
|
+
isLoading: fe,
|
|
314
|
+
isStreaming: he,
|
|
315
|
+
isBusy: ge,
|
|
316
|
+
error: He,
|
|
317
|
+
setError: k,
|
|
318
|
+
isSubmitDisabled: j,
|
|
319
|
+
input: y,
|
|
320
|
+
handleInputChange: we,
|
|
321
|
+
handleInputKeyDown: Ue,
|
|
322
|
+
handleSubmit: G,
|
|
323
|
+
stop: Q,
|
|
324
|
+
clear: ye,
|
|
325
|
+
inputRef: ve,
|
|
326
|
+
isMobile: Be,
|
|
308
327
|
// Additional state for attachments and workflow
|
|
309
|
-
messageAttachments:
|
|
310
|
-
setMessageAttachments:
|
|
311
|
-
selectedWorkflow:
|
|
312
|
-
setSelectedWorkflow:
|
|
313
|
-
isNewChat:
|
|
328
|
+
messageAttachments: Oe,
|
|
329
|
+
setMessageAttachments: S,
|
|
330
|
+
selectedWorkflow: _e,
|
|
331
|
+
setSelectedWorkflow: M,
|
|
332
|
+
isNewChat: De,
|
|
314
333
|
conversationId: t,
|
|
315
|
-
restoreSession:
|
|
316
|
-
loadAndRestoreSession:
|
|
317
|
-
isSessionLoading:
|
|
318
|
-
sessionToken:
|
|
319
|
-
refreshSession:
|
|
320
|
-
getCaptchaHeader:
|
|
321
|
-
invalidateCaptcha:
|
|
334
|
+
restoreSession: N,
|
|
335
|
+
loadAndRestoreSession: $e,
|
|
336
|
+
isSessionLoading: Ee,
|
|
337
|
+
sessionToken: p ? R : D,
|
|
338
|
+
refreshSession: p ? te : B,
|
|
339
|
+
getCaptchaHeader: C,
|
|
340
|
+
invalidateCaptcha: u
|
|
322
341
|
};
|
|
323
342
|
};
|
|
324
343
|
export {
|
|
325
|
-
|
|
326
|
-
|
|
344
|
+
Z as DEFAULT_ERROR_MESSAGE,
|
|
345
|
+
xt as useInkeepChat
|
|
327
346
|
};
|