@inkeep/agents-ui 0.15.17 → 0.15.19
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/chat-history/index.cjs +1 -1
- package/dist/primitives/components/chat-history/index.d.ts +1 -1
- package/dist/primitives/components/chat-history/index.js +65 -64
- package/dist/primitives/components/embedded-chat/use-inkeep-chat.cjs +2 -2
- package/dist/primitives/components/embedded-chat/use-inkeep-chat.js +208 -193
- 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 +32 -32
- package/dist/types/events.d.ts +8 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("react/jsx-runtime"),a=require("react"),e=require("../factory.cjs"),s=require("../../providers/chat-history-provider.cjs"),x=require("../../utils/misc.cjs"),m=require("../../utils/compose-event-handlers.cjs"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("react/jsx-runtime"),a=require("react"),e=require("../factory.cjs"),s=require("../../providers/chat-history-provider.cjs"),x=require("../../providers/chat-form-provider.cjs"),E=require("../../utils/misc.cjs"),m=require("../../utils/compose-event-handlers.cjs"),T=require("../../atoms/icons/custom-icon.cjs"),w=e.ikp("div",{_id:"aiChatHistory"}),H=t=>{const{isOpen:i}=s.useChatHistory(),{form:o}=x.useChatForm();return o?null:r.jsx(w,{"data-hidden":E.dataAttr(!i),...t})},d=e.ikp("div",{_id:"aiChatHistory__Header"}),R=e.ikp(T.CustomIcon,{_id:"aiChatHistory__BackButtonIcon"}),l=a.memo(t=>r.jsx(R,{...t,iconKey:"backToChat"})),q=e.ikp("button",{_id:"aiChatHistory__BackButton"}),P=({onClick:t,...i})=>{const{setIsOpen:o}=s.useChatHistory();return r.jsx(q,{type:"button",onClick:m.composeEventHandlers(t,()=>o(!1)),...i})},_=e.ikp("h2",{_id:"aiChatHistory__Title"}),F=e.ikp("div",{_id:"aiChatHistory__List"}),p=a.forwardRef(({children:t,...i},o)=>{const{hasMore:n,loadMore:u,isLoading:h,sessions:b}=s.useChatHistory(),y=a.useRef(null),c=b.length>0&&n;return a.useEffect(()=>{const C=y.current;if(!C||!c)return;const v=new IntersectionObserver(j=>{j[0]?.isIntersecting&&n&&!h&&u()},{threshold:.1});return v.observe(C),()=>v.disconnect()},[n,h,u,c]),r.jsxs(F,{ref:o,...i,children:[t,c&&r.jsx("div",{ref:y,"data-part":"sentinel","aria-hidden":"true",style:{height:1}})]})}),N=e.ikp("button",{_id:"aiChatHistory__Item"}),k=a.memo(({sessionId:t,onClick:i,...o})=>{const{loadSession:n}=s.useChatHistory();return r.jsx(N,{type:"button",onClick:m.composeEventHandlers(i,()=>n(t)),...o})}),B=e.ikp("span",{_id:"aiChatHistory__ItemTitle"}),I=e.ikp("span",{_id:"aiChatHistory__ItemDate"}),D=e.ikp("div",{_id:"aiChatHistory__EmptyState"}),f=t=>{const{sessions:i,isLoading:o}=s.useChatHistory();return i.length>0||o?null:r.jsx(D,{...t})},O=e.ikp("div",{_id:"aiChatHistory__LoadingState"}),g=t=>{const{isLoading:i}=s.useChatHistory();return i?r.jsx(O,{...t}):null},S=e.ikp("div",{_id:"aiChatHistory__Footer"}),M=e.ikp("button",{_id:"aiChatHistory__NewConversationButton"}),L=({onClick:t,...i})=>{const{startNewConversation:o}=s.useChatHistory();return r.jsx(M,{type:"button",onClick:m.composeEventHandlers(t,o),...i})};exports.BackButton=P;exports.BackButtonIcon=l;exports.ChatHistoryPrimitiveBackButton=P;exports.ChatHistoryPrimitiveBackButtonIcon=l;exports.ChatHistoryPrimitiveEmptyState=f;exports.ChatHistoryPrimitiveFooter=S;exports.ChatHistoryPrimitiveHeader=d;exports.ChatHistoryPrimitiveItem=k;exports.ChatHistoryPrimitiveItemDate=I;exports.ChatHistoryPrimitiveItemTitle=B;exports.ChatHistoryPrimitiveList=p;exports.ChatHistoryPrimitiveLoadingState=g;exports.ChatHistoryPrimitiveNewConversationButton=L;exports.ChatHistoryPrimitiveRoot=H;exports.ChatHistoryPrimitiveTitle=_;exports.EmptyState=f;exports.Footer=S;exports.Header=d;exports.Item=k;exports.ItemDate=I;exports.ItemTitle=B;exports.List=p;exports.LoadingState=g;exports.NewConversationButton=L;exports.Root=H;exports.Title=_;
|
|
@@ -2,7 +2,7 @@ import { ComponentPropsWithRef, ForwardRefExoticComponent, DetailedHTMLProps, HT
|
|
|
2
2
|
import { PolymorphicProps } from '..';
|
|
3
3
|
import { CustomIconProps } from '../..';
|
|
4
4
|
declare const PrimitiveRoot: ForwardRefExoticComponent< PolymorphicProps & Omit< DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "_id"> & Partial<Pick< DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "_id">>>;
|
|
5
|
-
export declare const ChatHistoryPrimitiveRoot: (props: ComponentPropsWithRef<typeof PrimitiveRoot>) => import("react/jsx-runtime").JSX.Element;
|
|
5
|
+
export declare const ChatHistoryPrimitiveRoot: (props: ComponentPropsWithRef<typeof PrimitiveRoot>) => import("react/jsx-runtime").JSX.Element | null;
|
|
6
6
|
export declare const ChatHistoryPrimitiveHeader: ForwardRefExoticComponent< PolymorphicProps & Omit< DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "_id"> & Partial<Pick< DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "_id">>>;
|
|
7
7
|
declare const PrimitiveBackButtonIcon: ForwardRefExoticComponent< PolymorphicProps & Omit< CustomIconProps, "_id"> & Partial<Pick< CustomIconProps, "_id">>>;
|
|
8
8
|
export declare const ChatHistoryPrimitiveBackButtonIcon: MemoExoticComponent<(props: ComponentPropsWithRef<typeof PrimitiveBackButtonIcon>) => import("react/jsx-runtime").JSX.Element>;
|
|
@@ -1,51 +1,52 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { memo as
|
|
1
|
+
import { jsx as r, jsxs as H } from "react/jsx-runtime";
|
|
2
|
+
import { memo as v, forwardRef as l, useRef as p, useEffect as f } from "react";
|
|
3
3
|
import { ikp as o } from "../factory.js";
|
|
4
|
-
import { useChatHistory as
|
|
4
|
+
import { useChatHistory as s } from "../../providers/chat-history-provider.js";
|
|
5
|
+
import { useChatForm as P } from "../../providers/chat-form-provider.js";
|
|
5
6
|
import { dataAttr as B } from "../../utils/misc.js";
|
|
6
7
|
import { composeEventHandlers as c } from "../../utils/compose-event-handlers.js";
|
|
7
|
-
import { CustomIcon as
|
|
8
|
-
const
|
|
9
|
-
const { isOpen: i } =
|
|
10
|
-
return /* @__PURE__ */
|
|
11
|
-
},
|
|
8
|
+
import { CustomIcon as I } from "../../atoms/icons/custom-icon.js";
|
|
9
|
+
const k = o("div", { _id: "aiChatHistory" }), A = (t) => {
|
|
10
|
+
const { isOpen: i } = s(), { form: e } = P();
|
|
11
|
+
return e ? null : /* @__PURE__ */ r(k, { "data-hidden": B(!i), ...t });
|
|
12
|
+
}, K = o("div", { _id: "aiChatHistory__Header" }), g = o(I, {
|
|
12
13
|
_id: "aiChatHistory__BackButtonIcon"
|
|
13
|
-
}),
|
|
14
|
-
(t) => /* @__PURE__ */
|
|
15
|
-
),
|
|
14
|
+
}), q = v(
|
|
15
|
+
(t) => /* @__PURE__ */ r(g, { ...t, iconKey: "backToChat" })
|
|
16
|
+
), L = o("button", { _id: "aiChatHistory__BackButton" }), z = ({
|
|
16
17
|
onClick: t,
|
|
17
18
|
...i
|
|
18
19
|
}) => {
|
|
19
|
-
const { setIsOpen: e } =
|
|
20
|
-
return /* @__PURE__ */
|
|
21
|
-
|
|
20
|
+
const { setIsOpen: e } = s();
|
|
21
|
+
return /* @__PURE__ */ r(
|
|
22
|
+
L,
|
|
22
23
|
{
|
|
23
24
|
type: "button",
|
|
24
25
|
onClick: c(t, () => e(!1)),
|
|
25
26
|
...i
|
|
26
27
|
}
|
|
27
28
|
);
|
|
28
|
-
},
|
|
29
|
-
const { hasMore: n, loadMore: m, isLoading: d, sessions: _ } =
|
|
30
|
-
return
|
|
31
|
-
const
|
|
32
|
-
if (!
|
|
33
|
-
const
|
|
29
|
+
}, G = o("h2", { _id: "aiChatHistory__Title" }), b = o("div", { _id: "aiChatHistory__List" }), J = l(({ children: t, ...i }, e) => {
|
|
30
|
+
const { hasMore: n, loadMore: m, isLoading: d, sessions: _ } = s(), u = p(null), a = _.length > 0 && n;
|
|
31
|
+
return f(() => {
|
|
32
|
+
const h = u.current;
|
|
33
|
+
if (!h || !a) return;
|
|
34
|
+
const C = new IntersectionObserver(
|
|
34
35
|
(y) => {
|
|
35
36
|
y[0]?.isIntersecting && n && !d && m();
|
|
36
37
|
},
|
|
37
38
|
{ threshold: 0.1 }
|
|
38
39
|
);
|
|
39
|
-
return
|
|
40
|
-
}, [n, d, m, a]), /* @__PURE__ */ H(
|
|
40
|
+
return C.observe(h), () => C.disconnect();
|
|
41
|
+
}, [n, d, m, a]), /* @__PURE__ */ H(b, { ref: e, ...i, children: [
|
|
41
42
|
t,
|
|
42
|
-
a && /* @__PURE__ */
|
|
43
|
+
a && /* @__PURE__ */ r("div", { ref: u, "data-part": "sentinel", "aria-hidden": "true", style: { height: 1 } })
|
|
43
44
|
] });
|
|
44
|
-
}),
|
|
45
|
+
}), S = o("button", { _id: "aiChatHistory__Item" }), Q = v(
|
|
45
46
|
({ sessionId: t, onClick: i, ...e }) => {
|
|
46
|
-
const { loadSession: n } =
|
|
47
|
-
return /* @__PURE__ */
|
|
48
|
-
|
|
47
|
+
const { loadSession: n } = s();
|
|
48
|
+
return /* @__PURE__ */ r(
|
|
49
|
+
S,
|
|
49
50
|
{
|
|
50
51
|
type: "button",
|
|
51
52
|
onClick: c(i, () => n(t)),
|
|
@@ -53,21 +54,21 @@ const I = o("div", { _id: "aiChatHistory" }), j = (t) => {
|
|
|
53
54
|
}
|
|
54
55
|
);
|
|
55
56
|
}
|
|
56
|
-
),
|
|
57
|
-
const { sessions: i, isLoading: e } =
|
|
58
|
-
return i.length > 0 || e ? null : /* @__PURE__ */
|
|
59
|
-
},
|
|
60
|
-
const { isLoading: i } =
|
|
61
|
-
return i ? /* @__PURE__ */
|
|
62
|
-
},
|
|
57
|
+
), U = o("span", { _id: "aiChatHistory__ItemTitle" }), V = o("span", { _id: "aiChatHistory__ItemDate" }), w = o("div", { _id: "aiChatHistory__EmptyState" }), W = (t) => {
|
|
58
|
+
const { sessions: i, isLoading: e } = s();
|
|
59
|
+
return i.length > 0 || e ? null : /* @__PURE__ */ r(w, { ...t });
|
|
60
|
+
}, T = o("div", { _id: "aiChatHistory__LoadingState" }), X = (t) => {
|
|
61
|
+
const { isLoading: i } = s();
|
|
62
|
+
return i ? /* @__PURE__ */ r(T, { ...t }) : null;
|
|
63
|
+
}, Y = o("div", { _id: "aiChatHistory__Footer" }), E = o("button", {
|
|
63
64
|
_id: "aiChatHistory__NewConversationButton"
|
|
64
|
-
}),
|
|
65
|
+
}), Z = ({
|
|
65
66
|
onClick: t,
|
|
66
67
|
...i
|
|
67
68
|
}) => {
|
|
68
|
-
const { startNewConversation: e } =
|
|
69
|
-
return /* @__PURE__ */
|
|
70
|
-
|
|
69
|
+
const { startNewConversation: e } = s();
|
|
70
|
+
return /* @__PURE__ */ r(
|
|
71
|
+
E,
|
|
71
72
|
{
|
|
72
73
|
type: "button",
|
|
73
74
|
onClick: c(t, e),
|
|
@@ -76,30 +77,30 @@ const I = o("div", { _id: "aiChatHistory" }), j = (t) => {
|
|
|
76
77
|
);
|
|
77
78
|
};
|
|
78
79
|
export {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
80
|
+
z as BackButton,
|
|
81
|
+
q as BackButtonIcon,
|
|
82
|
+
z as ChatHistoryPrimitiveBackButton,
|
|
83
|
+
q as ChatHistoryPrimitiveBackButtonIcon,
|
|
84
|
+
W as ChatHistoryPrimitiveEmptyState,
|
|
85
|
+
Y as ChatHistoryPrimitiveFooter,
|
|
86
|
+
K as ChatHistoryPrimitiveHeader,
|
|
87
|
+
Q as ChatHistoryPrimitiveItem,
|
|
88
|
+
V as ChatHistoryPrimitiveItemDate,
|
|
89
|
+
U as ChatHistoryPrimitiveItemTitle,
|
|
90
|
+
J as ChatHistoryPrimitiveList,
|
|
91
|
+
X as ChatHistoryPrimitiveLoadingState,
|
|
92
|
+
Z as ChatHistoryPrimitiveNewConversationButton,
|
|
93
|
+
A as ChatHistoryPrimitiveRoot,
|
|
94
|
+
G as ChatHistoryPrimitiveTitle,
|
|
95
|
+
W as EmptyState,
|
|
96
|
+
Y as Footer,
|
|
97
|
+
K as Header,
|
|
98
|
+
Q as Item,
|
|
99
|
+
V as ItemDate,
|
|
100
|
+
U as ItemTitle,
|
|
101
|
+
J as List,
|
|
102
|
+
X as LoadingState,
|
|
103
|
+
Z as NewConversationButton,
|
|
104
|
+
A as Root,
|
|
105
|
+
G as Title
|
|
105
106
|
};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const Ne=require("@ai-sdk/react"),
|
|
1
|
+
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const Ne=require("@ai-sdk/react"),Pe=require("ai"),t=require("react"),Oe=require("../modal/modal-provider.cjs"),Fe=require("../../providers/chat-bubble-provider.cjs"),Ue=require("../../providers/sidebar-chat-provider.cjs"),Be=require("../../providers/config-provider.cjs"),De=require("./use-captcha.cjs"),Le=require("../../hooks/use-media-query.cjs"),He=require("../../hooks/use-anonymous-session.cjs"),$e=require("../../hooks/use-conversation-loader.cjs"),w=require("../../utils/generate-uid.cjs"),ze=require("../../providers/base-events-provider.cjs"),We=require("../../providers/chat-form-provider.cjs"),Ke=require("../../providers/widget-provider.cjs"),je=require("@radix-ui/react-use-controllable-state"),Ge=require("../../hooks/use-streaming-events.cjs"),Je=require("../../hooks/use-inkeep-api-client.cjs");function Qe(g){const l=g.message??"";let s=Number(g.code)||Number(g.statusCode);if(Number.isNaN(s))try{s=Number(JSON.parse(l).status)}catch{}const d=Je.parseAuthError(s,{detail:l});return d!==null?d:s===401?"session":null}const $=`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.`,Ve=()=>{const{baseSettings:g,aiChatSettings:l}=Be.useInkeepConfig(),[s="",d]=je.useControllableState({prop:l.conversationId,defaultProp:l.conversationId??""}),ue=Oe.useModal(),le=Fe.useOptionalChatBubble(),de=Ue.useOptionalSidebarChat(),{logEvent:p}=ze.useBaseEvents(),{setConversationId:pe,emitToParent:C}=Ge.useStreamingEvents(),z=t.useRef(s);t.useEffect(()=>{const e=z.current;z.current=s,e!==s&&p({eventName:"chat_conversation_changed",properties:{conversationId:s,previousConversationId:e}})},[s,p]);const[v,k]=t.useState(""),fe=e=>k(e.target.value),{shouldBypassCaptcha:W,filters:K,privacyPreferences:ge}=g,{onInputMessageChange:he,filters:j,baseUrl:S,agentUrl:me,context:G,headers:J,appId:y,apiKey:f}=l,ve=ue?.isOpen??le?.isOpen??de?.isOpen??!0,{getCaptchaHeader:b,invalidate:u}=De.useCaptcha({baseUrl:S,shouldBypassCaptcha:W||!!f,shouldMakeInitialRequest:ve}),Q=t.useRef(b);Q.current=b;const V=me||`${S}/run/api/chat`,{sessionToken:q,refreshSession:x}=He.useAnonymousSession({baseUrl:S,appId:y,getCaptchaHeader:b,invalidateCaptcha:u,optOutAllAnalytics:ge?.optOutAllAnalytics}),{loadConversation:Z}=$e.useConversationLoader({baseUrl:S,appId:y,authToken:f??q,getCaptchaHeader:b,invalidateCaptcha:u,refreshSession:f?void 0:x}),[ye,X]=t.useState(!1),R=t.useRef(null);R.current=q;const h=t.useRef(0),Y=t.useRef(null),T=t.useRef(null),ee=t.useRef(J);ee.current=J;const _=t.useRef(void 0);_.current=K||j?JSON.stringify({...K,...j}):void 0;const be=e=>{switch(e.code){case 400:return e.message;case 403:return`There seems to be a configuration error. Please contact ${g.organizationDisplayName??"Administrator"}`;default:return $}},[Ce,I]=t.useState([]),[Se,N]=t.useState(null),Re=t.useMemo(()=>new Pe.DefaultChatTransport({api:V,headers:()=>({"x-inkeep-client-timezone":Intl.DateTimeFormat().resolvedOptions().timeZone,"x-inkeep-client-timestamp":new Date().toISOString(),...y?{"x-inkeep-app-id":y}:{},...R.current?{Authorization:`Bearer ${R.current}`}:{},...f?{Authorization:`Bearer ${f}`}:{},..._.current?{"inkeep-filters":_.current}:{},...ee.current}),prepareSendMessagesRequest:async e=>{const r=await Q.current();return{body:{...e.body,id:e.id,messages:e.messages,trigger:e.trigger,messageId:e.messageId},headers:{...e.headers,...r}}},body:{requestContext:G}}),[V,G,y,f]),{messages:A,sendMessage:P,addToolApprovalResponse:O,status:te,setMessages:m,stop:se,error:F}=Ne.useChat({transport:Re,onData(e){C(e.type,e.data)},async onFinish(){C("completion",{conversationId:s}),await p({eventName:"assistant_message_received",properties:{conversationId:s}}),p({eventName:"assistant_answer_displayed",properties:{conversationId:s}})},onError(e){console.error("onError",{code:e.code,message:e.message});const r=W||f?null:Qe(e);if(r!==null&&h.current<1){h.current++;const a=T.current,n=Y.current;(async()=>{if(r==="session"){const c=await x();c&&(R.current=c)}else u();if(a){O(a);return}n&&(m(c=>{let i=[...c];return i.at(-1)?.role==="assistant"&&(i=i.slice(0,-1)),i.at(-1)?.role==="user"&&(i=i.slice(0,-1)),i}),P({text:n.content},{body:n.body}))})().catch(()=>{h.current=0,u(),m(c=>{const i=[...c],H=i[i.length-1];if(!H)return i;const ce=$;return H.role==="user"?i.push({id:w.generateUid(16),role:"assistant",parts:[{type:"text",text:ce}]}):H.parts=[{type:"text",text:ce}],i})});return}h.current=0,r!==null&&u(),p({eventName:"chat_error",properties:{conversationId:s,error:e.message}}),m(a=>{const n=[...a],o=n[n.length-1];if(o){const c=be(e);o.role==="user"?n.push({id:w.generateUid(16),role:"assistant",parts:[{type:"text",text:c}]}):o.parts=[{type:"text",text:c}]}return n})}}),re=te==="submitted",U=te==="streaming",Ie=t.useMemo(()=>{const e=o=>{if(!o||typeof o!="object")return!1;const c=o;return typeof c.type=="string"&&c.type.startsWith("tool-")},a=[...A??[]].reverse().find(o=>o.role==="assistant");if(!a)return!1;const n=a.parts?.at(-1);return!(!e(n)||n.state!=="output-available"||!n.approval?.id||U)},[A,U]),ne=U||Ie,ae=re||ne,Ae=A.length===0,B=!v.trim()||ae,Ee=Le.useMediaQuery("(max-width: 768px)"),[Me,E]=t.useState(null);t.useEffect(()=>{F&&E(F)},[F]);const we=()=>E(null),oe=t.useRef(null);t.useEffect(()=>{he?.(v)},[v]);const ke=e=>{e.key==="Enter"&&!e.shiftKey&&!B&&!e.nativeEvent.isComposing&&(e.preventDefault(),D())},D=async(e=v)=>{if(B&&(!e||e.trim().length===0))return;I([]),k(""),h.current=0,T.current=null,await p({eventName:"user_message_submitted",properties:{conversationId:s}});let r=s;r||(r=`conv_${w.generateUid(16)}`,d(r)),pe(r),Y.current={content:e,body:{conversationId:r}},P({text:e},{body:{conversationId:r}})},qe=t.useCallback(e=>{h.current=0,T.current=e,O(e)},[O]),L=t.useCallback(()=>{se().then(()=>{C("aborted",{conversationId:s})})},[se,s,C]),ie=()=>{we(),m([]),d(""),I([]),N(null),u(),p({eventName:"chat_clear_button_clicked",properties:{conversationId:s}})},M=t.useCallback((e,r)=>{E(null),m(r),d(e),I([]),N(null),u()},[m,d,u]),xe=t.useCallback(async(e,r)=>{L(),M(e,[]),X(!0);try{const a=await Z(e,r),n=a?.[a.length-1],o=a!==null&&n?.role==="user"?[...a,{id:w.generateUid(16),role:"assistant",parts:[{type:"text",text:"This session was interrupted. Please check back in a few minutes or start a new conversation."}]}]:a;o!==null&&M(e,o)}finally{r?.aborted||X(!1)}},[M,Z,L]),{openForm:Te}=We.useChatForm(),_e=Ke.useWidget();return t.useImperativeHandle(l.chatFunctionsRef,()=>({submitMessage:D,updateInputMessage(e){k(e)},clearChat:ie,openForm:e=>{_e?.setView("chat"),Te(e,void 0)},focusInput:()=>{oe.current?.focus()}})),{messages:A,sendMessage:P,addToolApprovalResponse:qe,isLoading:re,isStreaming:ne,isBusy:ae,error:Me,setError:E,isSubmitDisabled:B,input:v,handleInputChange:fe,handleInputKeyDown:ke,handleSubmit:D,stop:L,clear:ie,inputRef:oe,isMobile:Ee,messageAttachments:Ce,setMessageAttachments:I,selectedWorkflow:Se,setSelectedWorkflow:N,isNewChat:Ae,conversationId:s,restoreSession:M,loadAndRestoreSession:xe,isSessionLoading:ye,sessionToken:q,refreshSession:x,getCaptchaHeader:b,invalidateCaptcha:u}};exports.DEFAULT_ERROR_MESSAGE=$;exports.useInkeepChat=Ve;
|
|
@@ -1,97 +1,108 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useChat as
|
|
3
|
-
import { DefaultChatTransport as
|
|
4
|
-
import {
|
|
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 { useConversationLoader as
|
|
13
|
-
import { generateUid as
|
|
14
|
-
import { useBaseEvents as
|
|
15
|
-
import { useChatForm as
|
|
16
|
-
import { useWidget as
|
|
17
|
-
import { useControllableState as
|
|
18
|
-
import { useStreamingEvents as
|
|
19
|
-
import { parseAuthError as
|
|
20
|
-
function
|
|
21
|
-
const u =
|
|
22
|
-
let
|
|
23
|
-
if (Number.isNaN(
|
|
2
|
+
import { useChat as Pe } from "@ai-sdk/react";
|
|
3
|
+
import { DefaultChatTransport as He } from "ai";
|
|
4
|
+
import { useRef as l, useEffect as q, useState as C, useMemo as pe, useCallback as E, useImperativeHandle as Le } from "react";
|
|
5
|
+
import { useModal as Ue } from "../modal/modal-provider.js";
|
|
6
|
+
import { useOptionalChatBubble as $e } from "../../providers/chat-bubble-provider.js";
|
|
7
|
+
import { useOptionalSidebarChat as ze } from "../../providers/sidebar-chat-provider.js";
|
|
8
|
+
import { useInkeepConfig as We } from "../../providers/config-provider.js";
|
|
9
|
+
import { useCaptcha as qe } from "./use-captcha.js";
|
|
10
|
+
import { useMediaQuery as Ke } from "../../hooks/use-media-query.js";
|
|
11
|
+
import { useAnonymousSession as Je } from "../../hooks/use-anonymous-session.js";
|
|
12
|
+
import { useConversationLoader as je } from "../../hooks/use-conversation-loader.js";
|
|
13
|
+
import { generateUid as k } from "../../utils/generate-uid.js";
|
|
14
|
+
import { useBaseEvents as Ge } from "../../providers/base-events-provider.js";
|
|
15
|
+
import { useChatForm as Qe } from "../../providers/chat-form-provider.js";
|
|
16
|
+
import { useWidget as Ve } from "../../providers/widget-provider.js";
|
|
17
|
+
import { useControllableState as Ze } from "@radix-ui/react-use-controllable-state";
|
|
18
|
+
import { useStreamingEvents as Xe } from "../../hooks/use-streaming-events.js";
|
|
19
|
+
import { parseAuthError as Ye } from "../../hooks/use-inkeep-api-client.js";
|
|
20
|
+
function et(f) {
|
|
21
|
+
const u = f.message ?? "";
|
|
22
|
+
let t = Number(f.code) || Number(f.statusCode);
|
|
23
|
+
if (Number.isNaN(t))
|
|
24
24
|
try {
|
|
25
|
-
|
|
25
|
+
t = Number(JSON.parse(u).status);
|
|
26
26
|
} catch {
|
|
27
27
|
}
|
|
28
|
-
const p =
|
|
29
|
-
return p !== null ? p :
|
|
28
|
+
const p = Ye(t, { detail: u });
|
|
29
|
+
return p !== null ? p : t === 401 ? "session" : null;
|
|
30
30
|
}
|
|
31
|
-
const
|
|
31
|
+
const me = `Hmm..
|
|
32
32
|
|
|
33
|
-
It seems I might be having some issues right now. Please clear the chat and try again.`,
|
|
34
|
-
const { baseSettings:
|
|
33
|
+
It seems I might be having some issues right now. Please clear the chat and try again.`, bt = () => {
|
|
34
|
+
const { baseSettings: f, aiChatSettings: u } = We(), [t = "", p] = Ze({
|
|
35
35
|
prop: u.conversationId,
|
|
36
36
|
defaultProp: u.conversationId ?? ""
|
|
37
|
-
}),
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
}), de = Ue(), fe = $e(), ge = ze(), { logEvent: m } = Ge(), { setConversationId: he, emitToParent: S } = Xe(), K = l(t);
|
|
38
|
+
q(() => {
|
|
39
|
+
const e = K.current;
|
|
40
|
+
K.current = t, e !== t && m({
|
|
41
|
+
eventName: "chat_conversation_changed",
|
|
42
|
+
properties: {
|
|
43
|
+
conversationId: t,
|
|
44
|
+
previousConversationId: e
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}, [t, m]);
|
|
48
|
+
const [v, N] = C(""), ve = (e) => N(e.target.value), { shouldBypassCaptcha: J, filters: j, privacyPreferences: ye } = f, {
|
|
49
|
+
onInputMessageChange: be,
|
|
50
|
+
filters: G,
|
|
40
51
|
baseUrl: I,
|
|
41
|
-
agentUrl:
|
|
42
|
-
context:
|
|
43
|
-
headers:
|
|
44
|
-
appId:
|
|
45
|
-
apiKey:
|
|
46
|
-
} = u,
|
|
52
|
+
agentUrl: Ce,
|
|
53
|
+
context: Q,
|
|
54
|
+
headers: V,
|
|
55
|
+
appId: y,
|
|
56
|
+
apiKey: d
|
|
57
|
+
} = u, Se = de?.isOpen ?? fe?.isOpen ?? ge?.isOpen ?? !0, { getCaptchaHeader: b, invalidate: c } = qe({
|
|
47
58
|
baseUrl: I,
|
|
48
|
-
shouldBypassCaptcha:
|
|
49
|
-
shouldMakeInitialRequest:
|
|
50
|
-
}),
|
|
51
|
-
|
|
52
|
-
const
|
|
59
|
+
shouldBypassCaptcha: J || !!d,
|
|
60
|
+
shouldMakeInitialRequest: Se
|
|
61
|
+
}), Z = l(b);
|
|
62
|
+
Z.current = b;
|
|
63
|
+
const X = Ce || `${I}/run/api/chat`, { sessionToken: T, refreshSession: _ } = Je({
|
|
53
64
|
baseUrl: I,
|
|
54
|
-
appId:
|
|
65
|
+
appId: y,
|
|
55
66
|
getCaptchaHeader: b,
|
|
56
67
|
invalidateCaptcha: c,
|
|
57
68
|
optOutAllAnalytics: ye?.optOutAllAnalytics
|
|
58
|
-
}), { loadConversation:
|
|
69
|
+
}), { loadConversation: Y } = je({
|
|
59
70
|
baseUrl: I,
|
|
60
|
-
appId:
|
|
61
|
-
authToken:
|
|
71
|
+
appId: y,
|
|
72
|
+
authToken: d ?? T,
|
|
62
73
|
getCaptchaHeader: b,
|
|
63
74
|
invalidateCaptcha: c,
|
|
64
75
|
// Only pass refreshSession for anonymous-session flows; API key auth has no token to refresh.
|
|
65
|
-
refreshSession:
|
|
66
|
-
}), [
|
|
67
|
-
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
const
|
|
76
|
+
refreshSession: d ? void 0 : _
|
|
77
|
+
}), [Ie, ee] = C(!1), w = l(null);
|
|
78
|
+
w.current = T;
|
|
79
|
+
const g = l(0), te = l(null), O = l(null), se = l(V);
|
|
80
|
+
se.current = V;
|
|
81
|
+
const F = l(void 0);
|
|
82
|
+
F.current = j || G ? JSON.stringify({ ...j, ...G }) : void 0;
|
|
83
|
+
const we = (e) => {
|
|
73
84
|
switch (e.code) {
|
|
74
85
|
case 400:
|
|
75
86
|
return e.message;
|
|
76
87
|
case 403:
|
|
77
|
-
return `There seems to be a configuration error. Please contact ${
|
|
88
|
+
return `There seems to be a configuration error. Please contact ${f.organizationDisplayName ?? "Administrator"}`;
|
|
78
89
|
default:
|
|
79
|
-
return
|
|
90
|
+
return me;
|
|
80
91
|
}
|
|
81
|
-
}, [Ae,
|
|
82
|
-
() => new
|
|
83
|
-
api:
|
|
92
|
+
}, [Ae, A] = C([]), [Re, B] = C(null), Me = pe(
|
|
93
|
+
() => new He({
|
|
94
|
+
api: X,
|
|
84
95
|
headers: () => ({
|
|
85
96
|
"x-inkeep-client-timezone": Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
86
97
|
"x-inkeep-client-timestamp": (/* @__PURE__ */ new Date()).toISOString(),
|
|
87
|
-
...
|
|
88
|
-
...
|
|
89
|
-
...
|
|
90
|
-
...
|
|
91
|
-
...
|
|
98
|
+
...y ? { "x-inkeep-app-id": y } : {},
|
|
99
|
+
...w.current ? { Authorization: `Bearer ${w.current}` } : {},
|
|
100
|
+
...d ? { Authorization: `Bearer ${d}` } : {},
|
|
101
|
+
...F.current ? { "inkeep-filters": F.current } : {},
|
|
102
|
+
...se.current
|
|
92
103
|
}),
|
|
93
104
|
prepareSendMessagesRequest: async (e) => {
|
|
94
|
-
const
|
|
105
|
+
const s = await Z.current();
|
|
95
106
|
return {
|
|
96
107
|
body: {
|
|
97
108
|
...e.body,
|
|
@@ -102,211 +113,215 @@ It seems I might be having some issues right now. Please clear the chat and try
|
|
|
102
113
|
},
|
|
103
114
|
headers: {
|
|
104
115
|
...e.headers,
|
|
105
|
-
...
|
|
116
|
+
...s
|
|
106
117
|
}
|
|
107
118
|
};
|
|
108
119
|
},
|
|
109
120
|
body: {
|
|
110
|
-
requestContext:
|
|
121
|
+
requestContext: Q
|
|
111
122
|
}
|
|
112
123
|
}),
|
|
113
|
-
[
|
|
124
|
+
[X, Q, y, d]
|
|
114
125
|
), {
|
|
115
|
-
messages:
|
|
116
|
-
sendMessage:
|
|
117
|
-
addToolApprovalResponse:
|
|
118
|
-
status:
|
|
119
|
-
setMessages:
|
|
120
|
-
stop:
|
|
121
|
-
error:
|
|
122
|
-
} =
|
|
123
|
-
transport:
|
|
126
|
+
messages: R,
|
|
127
|
+
sendMessage: D,
|
|
128
|
+
addToolApprovalResponse: P,
|
|
129
|
+
status: re,
|
|
130
|
+
setMessages: h,
|
|
131
|
+
stop: ne,
|
|
132
|
+
error: H
|
|
133
|
+
} = Pe({
|
|
134
|
+
transport: Me,
|
|
124
135
|
onData(e) {
|
|
125
136
|
S(e.type, e.data);
|
|
126
137
|
},
|
|
127
138
|
async onFinish() {
|
|
128
|
-
S("completion", { conversationId:
|
|
139
|
+
S("completion", { conversationId: t }), await m({
|
|
129
140
|
eventName: "assistant_message_received",
|
|
130
141
|
properties: {
|
|
131
|
-
conversationId:
|
|
142
|
+
conversationId: t
|
|
132
143
|
}
|
|
133
|
-
}),
|
|
144
|
+
}), m({
|
|
134
145
|
eventName: "assistant_answer_displayed",
|
|
135
146
|
properties: {
|
|
136
|
-
conversationId:
|
|
147
|
+
conversationId: t
|
|
137
148
|
}
|
|
138
149
|
});
|
|
139
150
|
},
|
|
140
151
|
onError(e) {
|
|
141
152
|
console.error("onError", { code: e.code, message: e.message });
|
|
142
|
-
const
|
|
143
|
-
if (
|
|
144
|
-
|
|
145
|
-
const
|
|
153
|
+
const s = J || d ? null : et(e);
|
|
154
|
+
if (s !== null && g.current < 1) {
|
|
155
|
+
g.current++;
|
|
156
|
+
const n = O.current, r = te.current;
|
|
146
157
|
(async () => {
|
|
147
|
-
if (
|
|
148
|
-
const
|
|
149
|
-
|
|
158
|
+
if (s === "session") {
|
|
159
|
+
const i = await _();
|
|
160
|
+
i && (w.current = i);
|
|
150
161
|
} else
|
|
151
162
|
c();
|
|
152
|
-
if (
|
|
153
|
-
|
|
163
|
+
if (n) {
|
|
164
|
+
P(n);
|
|
154
165
|
return;
|
|
155
166
|
}
|
|
156
|
-
|
|
157
|
-
let
|
|
158
|
-
return
|
|
159
|
-
}),
|
|
167
|
+
r && (h((i) => {
|
|
168
|
+
let a = [...i];
|
|
169
|
+
return a.at(-1)?.role === "assistant" && (a = a.slice(0, -1)), a.at(-1)?.role === "user" && (a = a.slice(0, -1)), a;
|
|
170
|
+
}), D({ text: r.content }, { body: r.body }));
|
|
160
171
|
})().catch(() => {
|
|
161
|
-
|
|
162
|
-
const
|
|
163
|
-
if (!
|
|
164
|
-
const
|
|
165
|
-
return
|
|
166
|
-
id:
|
|
172
|
+
g.current = 0, c(), h((i) => {
|
|
173
|
+
const a = [...i], W = a[a.length - 1];
|
|
174
|
+
if (!W) return a;
|
|
175
|
+
const ue = me;
|
|
176
|
+
return W.role === "user" ? a.push({
|
|
177
|
+
id: k(16),
|
|
167
178
|
role: "assistant",
|
|
168
|
-
parts: [{ type: "text", text:
|
|
169
|
-
}) :
|
|
179
|
+
parts: [{ type: "text", text: ue }]
|
|
180
|
+
}) : W.parts = [{ type: "text", text: ue }], a;
|
|
170
181
|
});
|
|
171
182
|
});
|
|
172
183
|
return;
|
|
173
184
|
}
|
|
174
|
-
|
|
185
|
+
g.current = 0, s !== null && c(), m({
|
|
175
186
|
eventName: "chat_error",
|
|
176
187
|
properties: {
|
|
177
|
-
conversationId:
|
|
188
|
+
conversationId: t,
|
|
178
189
|
error: e.message
|
|
179
190
|
}
|
|
180
|
-
}),
|
|
181
|
-
const
|
|
182
|
-
if (
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
id:
|
|
191
|
+
}), h((n) => {
|
|
192
|
+
const r = [...n], o = r[r.length - 1];
|
|
193
|
+
if (o) {
|
|
194
|
+
const i = we(e);
|
|
195
|
+
o.role === "user" ? r.push({
|
|
196
|
+
id: k(16),
|
|
186
197
|
role: "assistant",
|
|
187
|
-
parts: [{ type: "text", text:
|
|
188
|
-
}) :
|
|
198
|
+
parts: [{ type: "text", text: i }]
|
|
199
|
+
}) : o.parts = [{ type: "text", text: i }];
|
|
189
200
|
}
|
|
190
|
-
return
|
|
201
|
+
return r;
|
|
191
202
|
});
|
|
192
203
|
}
|
|
193
|
-
}),
|
|
194
|
-
const e = (
|
|
195
|
-
if (!
|
|
196
|
-
const
|
|
197
|
-
return typeof
|
|
198
|
-
},
|
|
199
|
-
if (!
|
|
200
|
-
const
|
|
201
|
-
return !(!e(
|
|
202
|
-
}, [
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}, [
|
|
206
|
-
const
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
}, [
|
|
210
|
-
const
|
|
211
|
-
e.key === "Enter" && !e.shiftKey && !
|
|
212
|
-
},
|
|
213
|
-
if (
|
|
214
|
-
|
|
204
|
+
}), oe = re === "submitted", L = re === "streaming", xe = pe(() => {
|
|
205
|
+
const e = (o) => {
|
|
206
|
+
if (!o || typeof o != "object") return !1;
|
|
207
|
+
const i = o;
|
|
208
|
+
return typeof i.type == "string" && i.type.startsWith("tool-");
|
|
209
|
+
}, n = [...R ?? []].reverse().find((o) => o.role === "assistant");
|
|
210
|
+
if (!n) return !1;
|
|
211
|
+
const r = n.parts?.at(-1);
|
|
212
|
+
return !(!e(r) || r.state !== "output-available" || !r.approval?.id || L);
|
|
213
|
+
}, [R, L]), ae = L || xe, ie = oe || ae, Ee = R.length === 0, U = !v.trim() || ie, ke = Ke("(max-width: 768px)"), [Ne, M] = C(null);
|
|
214
|
+
q(() => {
|
|
215
|
+
H && M(H);
|
|
216
|
+
}, [H]);
|
|
217
|
+
const Te = () => M(null), ce = l(null);
|
|
218
|
+
q(() => {
|
|
219
|
+
be?.(v);
|
|
220
|
+
}, [v]);
|
|
221
|
+
const _e = (e) => {
|
|
222
|
+
e.key === "Enter" && !e.shiftKey && !U && !e.nativeEvent.isComposing && (e.preventDefault(), $());
|
|
223
|
+
}, $ = async (e = v) => {
|
|
224
|
+
if (U && (!e || e.trim().length === 0)) return;
|
|
225
|
+
A([]), N(""), g.current = 0, O.current = null, await m({
|
|
215
226
|
eventName: "user_message_submitted",
|
|
216
227
|
properties: {
|
|
217
|
-
conversationId:
|
|
228
|
+
conversationId: t
|
|
218
229
|
}
|
|
219
230
|
});
|
|
220
|
-
let
|
|
221
|
-
|
|
231
|
+
let s = t;
|
|
232
|
+
s || (s = `conv_${k(16)}`, p(s)), he(s), te.current = {
|
|
222
233
|
content: e,
|
|
223
|
-
body: { conversationId:
|
|
224
|
-
},
|
|
234
|
+
body: { conversationId: s }
|
|
235
|
+
}, D(
|
|
225
236
|
{ text: e },
|
|
226
237
|
{
|
|
227
|
-
body: { conversationId:
|
|
238
|
+
body: { conversationId: s }
|
|
228
239
|
}
|
|
229
240
|
);
|
|
230
|
-
}, Oe =
|
|
241
|
+
}, Oe = E(
|
|
231
242
|
(e) => {
|
|
232
|
-
|
|
243
|
+
g.current = 0, O.current = e, P(e);
|
|
233
244
|
},
|
|
234
|
-
[
|
|
235
|
-
),
|
|
236
|
-
|
|
237
|
-
S("aborted", { conversationId:
|
|
245
|
+
[P]
|
|
246
|
+
), z = E(() => {
|
|
247
|
+
ne().then(() => {
|
|
248
|
+
S("aborted", { conversationId: t });
|
|
238
249
|
});
|
|
239
|
-
}, [
|
|
240
|
-
|
|
250
|
+
}, [ne, t, S]), le = () => {
|
|
251
|
+
Te(), h([]), p(""), A([]), B(null), c(), m({
|
|
241
252
|
eventName: "chat_clear_button_clicked",
|
|
242
253
|
properties: {
|
|
243
|
-
conversationId:
|
|
254
|
+
conversationId: t
|
|
244
255
|
}
|
|
245
256
|
});
|
|
246
|
-
},
|
|
247
|
-
(e,
|
|
248
|
-
M(null),
|
|
257
|
+
}, x = E(
|
|
258
|
+
(e, s) => {
|
|
259
|
+
M(null), h(s), p(e), A([]), B(null), c();
|
|
249
260
|
},
|
|
250
|
-
[
|
|
251
|
-
),
|
|
252
|
-
async (e,
|
|
253
|
-
|
|
261
|
+
[h, p, c]
|
|
262
|
+
), Fe = E(
|
|
263
|
+
async (e, s) => {
|
|
264
|
+
z(), x(e, []), ee(!0);
|
|
254
265
|
try {
|
|
255
|
-
const
|
|
256
|
-
|
|
266
|
+
const n = await Y(e, s), r = n?.[n.length - 1], o = n !== null && r?.role === "user" ? [...n, {
|
|
267
|
+
id: k(16),
|
|
268
|
+
role: "assistant",
|
|
269
|
+
parts: [{ type: "text", text: "This session was interrupted. Please check back in a few minutes or start a new conversation." }]
|
|
270
|
+
}] : n;
|
|
271
|
+
o !== null && x(e, o);
|
|
257
272
|
} finally {
|
|
258
|
-
|
|
273
|
+
s?.aborted || ee(!1);
|
|
259
274
|
}
|
|
260
275
|
},
|
|
261
|
-
[
|
|
262
|
-
), { openForm:
|
|
263
|
-
return
|
|
264
|
-
submitMessage:
|
|
276
|
+
[x, Y, z]
|
|
277
|
+
), { openForm: Be } = Qe(), De = Ve();
|
|
278
|
+
return Le(u.chatFunctionsRef, () => ({
|
|
279
|
+
submitMessage: $,
|
|
265
280
|
updateInputMessage(e) {
|
|
266
|
-
|
|
281
|
+
N(e);
|
|
267
282
|
},
|
|
268
|
-
clearChat:
|
|
283
|
+
clearChat: le,
|
|
269
284
|
openForm: (e) => {
|
|
270
|
-
|
|
285
|
+
De?.setView("chat"), Be(e, void 0);
|
|
271
286
|
},
|
|
272
287
|
focusInput: () => {
|
|
273
|
-
|
|
288
|
+
ce.current?.focus();
|
|
274
289
|
}
|
|
275
290
|
})), {
|
|
276
|
-
messages:
|
|
277
|
-
sendMessage:
|
|
291
|
+
messages: R,
|
|
292
|
+
sendMessage: D,
|
|
278
293
|
addToolApprovalResponse: Oe,
|
|
279
|
-
isLoading:
|
|
280
|
-
isStreaming:
|
|
281
|
-
isBusy:
|
|
282
|
-
error:
|
|
294
|
+
isLoading: oe,
|
|
295
|
+
isStreaming: ae,
|
|
296
|
+
isBusy: ie,
|
|
297
|
+
error: Ne,
|
|
283
298
|
setError: M,
|
|
284
|
-
isSubmitDisabled:
|
|
285
|
-
input:
|
|
286
|
-
handleInputChange:
|
|
287
|
-
handleInputKeyDown:
|
|
288
|
-
handleSubmit:
|
|
289
|
-
stop:
|
|
290
|
-
clear:
|
|
291
|
-
inputRef:
|
|
292
|
-
isMobile:
|
|
299
|
+
isSubmitDisabled: U,
|
|
300
|
+
input: v,
|
|
301
|
+
handleInputChange: ve,
|
|
302
|
+
handleInputKeyDown: _e,
|
|
303
|
+
handleSubmit: $,
|
|
304
|
+
stop: z,
|
|
305
|
+
clear: le,
|
|
306
|
+
inputRef: ce,
|
|
307
|
+
isMobile: ke,
|
|
293
308
|
// Additional state for attachments and workflow
|
|
294
309
|
messageAttachments: Ae,
|
|
295
|
-
setMessageAttachments:
|
|
310
|
+
setMessageAttachments: A,
|
|
296
311
|
selectedWorkflow: Re,
|
|
297
|
-
setSelectedWorkflow:
|
|
312
|
+
setSelectedWorkflow: B,
|
|
298
313
|
isNewChat: Ee,
|
|
299
|
-
conversationId:
|
|
300
|
-
restoreSession:
|
|
301
|
-
loadAndRestoreSession:
|
|
302
|
-
isSessionLoading:
|
|
303
|
-
sessionToken:
|
|
304
|
-
refreshSession:
|
|
314
|
+
conversationId: t,
|
|
315
|
+
restoreSession: x,
|
|
316
|
+
loadAndRestoreSession: Fe,
|
|
317
|
+
isSessionLoading: Ie,
|
|
318
|
+
sessionToken: T,
|
|
319
|
+
refreshSession: _,
|
|
305
320
|
getCaptchaHeader: b,
|
|
306
321
|
invalidateCaptcha: c
|
|
307
322
|
};
|
|
308
323
|
};
|
|
309
324
|
export {
|
|
310
|
-
|
|
311
|
-
|
|
325
|
+
me as DEFAULT_ERROR_MESSAGE,
|
|
326
|
+
bt as useInkeepChat
|
|
312
327
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const E=require("react/jsx-runtime"),t=require("react"),l=require("./config-provider.cjs"),a=t.createContext(void 0),p=({children:e})=>{const{baseSettings:s,componentType:n}=l.useInkeepConfig(),{tags:o,analyticsProperties:r}=s,i=t.useMemo(()=>({widgetLibraryVersion:"0.15.
|
|
1
|
+
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const E=require("react/jsx-runtime"),t=require("react"),l=require("./config-provider.cjs"),a=t.createContext(void 0),p=({children:e})=>{const{baseSettings:s,componentType:n}=l.useInkeepConfig(),{tags:o,analyticsProperties:r}=s,i=t.useMemo(()=>({widgetLibraryVersion:"0.15.19",componentType:n,tags:o}),[n,o]),u={logEvent:t.useCallback(async c=>{const v={...i,...c.properties,...r},d={eventName:c.eventName,properties:v};return s.onEvent?.(d)},[s,i,r])};return E.jsx(a.Provider,{value:u,children:e})},g=()=>{const e=t.useContext(a);if(!e)throw new Error("useBaseEvents must be used within a BaseEventsProvider");return e};exports.BaseEventsProvider=p;exports.useBaseEvents=g;
|
|
@@ -5,7 +5,7 @@ import { useInkeepConfig as g } from "./config-provider.js";
|
|
|
5
5
|
const a = d(void 0), P = ({ children: e }) => {
|
|
6
6
|
const { baseSettings: t, componentType: o } = g(), { tags: s, analyticsProperties: n } = t, r = u(
|
|
7
7
|
() => ({
|
|
8
|
-
widgetLibraryVersion: "0.15.
|
|
8
|
+
widgetLibraryVersion: "0.15.19",
|
|
9
9
|
componentType: o,
|
|
10
10
|
tags: s
|
|
11
11
|
}),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const X=require("react/jsx-runtime"),e=require("react"),Y=require("../components/embedded-chat/chat-provider.cjs"),N=require("./config-provider.cjs"),ee=require("../hooks/use-inkeep-api-client.cjs"),
|
|
1
|
+
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const X=require("react/jsx-runtime"),e=require("react"),Y=require("../components/embedded-chat/chat-provider.cjs"),N=require("./config-provider.cjs"),ee=require("../hooks/use-inkeep-api-client.cjs"),D=e.createContext(void 0),T=25;function te(t){const{stop:a,clear:c,conversationId:u,isStreaming:H,loadAndRestoreSession:P,sessionToken:f,refreshSession:F,getCaptchaHeader:W,invalidateCaptcha:G}=Y.useChat(),{aiChatSettings:K}=N.useInkeepConfig(),{baseUrl:h,appId:k,apiKey:U}=K,o=!U&&!!k,C=f,{fetchWithAuth:I}=ee.useInkeepApiClient({appId:k,authToken:C,getCaptchaHeader:W,invalidateCaptcha:G,refreshSession:F}),[p,S]=e.useState(!1),[R,Z]=e.useState([]),[g,E]=e.useState(!1),[x,_]=e.useState(0),[v,z]=e.useState(!1),b=e.useRef(!1),m=e.useRef(null),q=e.useRef([]);q.current=R;const j=e.useRef(I);j.current=I;const i=e.useCallback(async(s,n=!1)=>{if(!(!h||!C||!o)){n||E(!0);try{const w=`${h}/run/v1/conversations?page=${s+1}&limit=${T}`,y=await j.current(w);if(!y.ok)throw new Error(`Failed to fetch conversations: ${y.status}`);const $=await y.json(),d=($.data??[]).map(r=>({id:r.id,title:r.title??"",createdAt:new Date(r.createdAt),updatedAt:new Date(r.updatedAt)}));Z(r=>{if(s===0&&!n)return d;if(s===0&&n){const l=new Set(d.map(A=>A.id));return[...d,...r.slice(T).filter(A=>!l.has(A.id))]}const V=new Set(r.map(l=>l.id));return[...r,...d.filter(l=>!V.has(l.id))]});const{page:L,pages:O}=$.pagination??{};z(L!=null&&O!=null?L<O:!1),n||_(s)}catch(w){console.error("[useChatHistory] Failed to load conversations:",w)}finally{E(!1)}}},[h,C,o]);e.useEffect(()=>{!p||!f||!o||(t==="stack"?i(0):b.current||(b.current=!0,i(0)))},[p,f,o,t,i]);const M=e.useRef("");e.useEffect(()=>{o&&t==="sidepane"&&(!u||!H||b.current&&M.current!==u&&(q.current.some(s=>s.id===u)||(M.current=u,i(0,!0))))},[u,o,t,i,H]);const B=e.useCallback(()=>{!g&&v&&i(x+1)},[g,v,x,i]),J=e.useCallback(()=>{a(),c(),t!=="sidepane"&&S(!1)},[t,a,c]),Q=e.useCallback(async s=>{m.current?.abort();const n=new AbortController;m.current=n,await P(s,n.signal),!n.signal.aborted&&t!=="sidepane"&&S(!1)},[t,P]);return{isEnabled:o,isOpen:p,setIsOpen:S,sessions:R,isLoading:g,hasMore:v,loadMore:B,startNewConversation:J,loadSession:Q}}const se=({layout:t,children:a})=>{const c=te(t);return X.jsx(D.Provider,{value:c,children:typeof a=="function"?a(c):a})},ne=()=>{const t=e.useContext(D);if(!t)throw new Error("useChatHistory must be used within a ChatHistoryProvider");return t};exports.ChatHistoryProvider=se;exports.useChatHistory=ne;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as tt } from "react/jsx-runtime";
|
|
3
|
-
import { createContext as et, useContext as st, useState as f, useRef as d, useCallback as h, useEffect as
|
|
3
|
+
import { createContext as et, useContext as st, useState as f, useRef as d, useCallback as h, useEffect as W } from "react";
|
|
4
4
|
import { useChat as nt } from "../components/embedded-chat/chat-provider.js";
|
|
5
5
|
import { useInkeepConfig as rt } from "./config-provider.js";
|
|
6
6
|
import { useInkeepApiClient as ot } from "../hooks/use-inkeep-api-client.js";
|
|
7
|
-
const
|
|
7
|
+
const K = et(void 0), G = 25;
|
|
8
8
|
function it(t) {
|
|
9
9
|
const {
|
|
10
10
|
stop: i,
|
|
@@ -13,68 +13,68 @@ function it(t) {
|
|
|
13
13
|
isStreaming: y,
|
|
14
14
|
loadAndRestoreSession: P,
|
|
15
15
|
sessionToken: p,
|
|
16
|
-
refreshSession:
|
|
17
|
-
getCaptchaHeader:
|
|
18
|
-
invalidateCaptcha:
|
|
19
|
-
} = nt(), { aiChatSettings:
|
|
16
|
+
refreshSession: U,
|
|
17
|
+
getCaptchaHeader: Z,
|
|
18
|
+
invalidateCaptcha: _
|
|
19
|
+
} = nt(), { aiChatSettings: q } = rt(), { baseUrl: C, appId: k, apiKey: z } = q, r = !z && !!k, g = p, { fetchWithAuth: x } = ot({
|
|
20
20
|
appId: k,
|
|
21
21
|
authToken: g,
|
|
22
|
-
getCaptchaHeader:
|
|
23
|
-
invalidateCaptcha:
|
|
24
|
-
refreshSession:
|
|
25
|
-
}), [w, m] = f(!1), [E,
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
|
|
22
|
+
getCaptchaHeader: Z,
|
|
23
|
+
invalidateCaptcha: _,
|
|
24
|
+
refreshSession: U
|
|
25
|
+
}), [w, m] = f(!1), [E, B] = f([]), [S, $] = f(!1), [L, J] = f(0), [v, Q] = f(!1), A = d(!1), M = d(null), R = d([]);
|
|
26
|
+
R.current = E;
|
|
27
|
+
const j = d(x);
|
|
28
|
+
j.current = x;
|
|
29
29
|
const o = h(
|
|
30
30
|
async (e, s = !1) => {
|
|
31
31
|
if (!(!C || !g || !r)) {
|
|
32
|
-
s ||
|
|
32
|
+
s || $(!0);
|
|
33
33
|
try {
|
|
34
|
-
const b = `${C}/run/v1/conversations?page=${e + 1}&limit=${
|
|
34
|
+
const b = `${C}/run/v1/conversations?page=${e + 1}&limit=${G}`, H = await j.current(b);
|
|
35
35
|
if (!H.ok) throw new Error(`Failed to fetch conversations: ${H.status}`);
|
|
36
|
-
const
|
|
36
|
+
const F = await H.json(), l = (F.data ?? []).map((n) => ({
|
|
37
37
|
id: n.id,
|
|
38
38
|
title: n.title ?? "",
|
|
39
39
|
createdAt: new Date(n.createdAt),
|
|
40
40
|
updatedAt: new Date(n.updatedAt)
|
|
41
41
|
}));
|
|
42
|
-
|
|
42
|
+
B((n) => {
|
|
43
43
|
if (e === 0 && !s) return l;
|
|
44
44
|
if (e === 0 && s) {
|
|
45
45
|
const u = new Set(l.map((I) => I.id));
|
|
46
|
-
return [...l, ...n.slice(
|
|
46
|
+
return [...l, ...n.slice(G).filter((I) => !u.has(I.id))];
|
|
47
47
|
}
|
|
48
48
|
const N = new Set(n.map((u) => u.id));
|
|
49
49
|
return [...n, ...l.filter((u) => !N.has(u.id))];
|
|
50
50
|
});
|
|
51
|
-
const { page:
|
|
52
|
-
Q(
|
|
51
|
+
const { page: O, pages: T } = F.pagination ?? {};
|
|
52
|
+
Q(O != null && T != null ? O < T : !1), s || J(e);
|
|
53
53
|
} catch (b) {
|
|
54
54
|
console.error("[useChatHistory] Failed to load conversations:", b);
|
|
55
55
|
} finally {
|
|
56
|
-
|
|
56
|
+
$(!1);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
},
|
|
60
60
|
[C, g, r]
|
|
61
61
|
);
|
|
62
|
-
|
|
63
|
-
!w || !p || !r || (t === "stack" ?
|
|
62
|
+
W(() => {
|
|
63
|
+
!w || !p || !r || (t === "stack" ? o(0) : A.current || (A.current = !0, o(0)));
|
|
64
64
|
}, [w, p, r, t, o]);
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
r && t === "sidepane" && (!c || !y || A.current &&
|
|
65
|
+
const D = d("");
|
|
66
|
+
W(() => {
|
|
67
|
+
r && t === "sidepane" && (!c || !y || A.current && D.current !== c && (R.current.some((e) => e.id === c) || (D.current = c, o(0, !0))));
|
|
68
68
|
}, [c, r, t, o, y]);
|
|
69
69
|
const V = h(() => {
|
|
70
|
-
!S && v && o(
|
|
71
|
-
}, [S, v,
|
|
70
|
+
!S && v && o(L + 1);
|
|
71
|
+
}, [S, v, L, o]), X = h(() => {
|
|
72
72
|
i(), a(), t !== "sidepane" && m(!1);
|
|
73
73
|
}, [t, i, a]), Y = h(
|
|
74
74
|
async (e) => {
|
|
75
|
-
|
|
75
|
+
M.current?.abort();
|
|
76
76
|
const s = new AbortController();
|
|
77
|
-
|
|
77
|
+
M.current = s, await P(e, s.signal), !s.signal.aborted && t !== "sidepane" && m(!1);
|
|
78
78
|
},
|
|
79
79
|
[t, P]
|
|
80
80
|
);
|
|
@@ -92,9 +92,9 @@ function it(t) {
|
|
|
92
92
|
}
|
|
93
93
|
const ht = ({ layout: t, children: i }) => {
|
|
94
94
|
const a = it(t);
|
|
95
|
-
return /* @__PURE__ */ tt(
|
|
95
|
+
return /* @__PURE__ */ tt(K.Provider, { value: a, children: typeof i == "function" ? i(a) : i });
|
|
96
96
|
}, pt = () => {
|
|
97
|
-
const t = st(
|
|
97
|
+
const t = st(K);
|
|
98
98
|
if (!t)
|
|
99
99
|
throw new Error("useChatHistory must be used within a ChatHistoryProvider");
|
|
100
100
|
return t;
|
package/dist/types/events.d.ts
CHANGED
|
@@ -43,7 +43,7 @@ type ExtendPropertiesWithCommon<T> = T extends {
|
|
|
43
43
|
export type InkeepEventWithCommon = ExtendPropertiesWithCommon<InkeepEvent>;
|
|
44
44
|
export type InkeepCallbackEvent = InkeepEventWithCommon;
|
|
45
45
|
export type SearchEvent = SearchQueryResponseReceivedEvent | SearchQuerySubmittedEvent | SearchResultClickedEvent;
|
|
46
|
-
export type ChatEvent = AssistantMessageReceivedEvent | AssistantAnswerDisplayedEvent | UserMessageSubmittedEvent | UserEscalationIndicatedEvent | SharedChatLoadedEvent | AssistantPositiveFeedbackSubmittedEvent | AssistantNegativeFeedbackSubmittedEvent | ChatClearButtonClickedEvent | AssistantMessageCopiedEvent | GetHelpOptionClickedEvent | ChatShareButtonClickedEvent | AssistantSourceItemClickedEvent | AssistantMessageLinkOpenedEvent | AssistantCodeBlockCopiedEvent | ChatErrorEvent;
|
|
46
|
+
export type ChatEvent = AssistantMessageReceivedEvent | AssistantAnswerDisplayedEvent | UserMessageSubmittedEvent | UserEscalationIndicatedEvent | SharedChatLoadedEvent | AssistantPositiveFeedbackSubmittedEvent | AssistantNegativeFeedbackSubmittedEvent | ChatClearButtonClickedEvent | ChatConversationChangedEvent | AssistantMessageCopiedEvent | GetHelpOptionClickedEvent | ChatShareButtonClickedEvent | AssistantSourceItemClickedEvent | AssistantMessageLinkOpenedEvent | AssistantCodeBlockCopiedEvent | ChatErrorEvent;
|
|
47
47
|
export type WidgetEvent = ModalOpenedEvent | ModalClosedEvent | ChatBubbleOpenedEvent | ChatBubbleClosedEvent;
|
|
48
48
|
export type InkeepEvent = SearchEvent | ChatEvent | WidgetEvent;
|
|
49
49
|
export interface AssistantMessageReceivedEvent {
|
|
@@ -104,6 +104,13 @@ export interface ChatClearButtonClickedEvent {
|
|
|
104
104
|
conversationId: string;
|
|
105
105
|
};
|
|
106
106
|
}
|
|
107
|
+
export interface ChatConversationChangedEvent {
|
|
108
|
+
eventName: 'chat_conversation_changed';
|
|
109
|
+
properties: {
|
|
110
|
+
conversationId: string;
|
|
111
|
+
previousConversationId: string;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
107
114
|
export interface ChatShareButtonClickedEvent {
|
|
108
115
|
eventName: 'chat_share_button_clicked';
|
|
109
116
|
properties: {
|