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