@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 +1 @@
|
|
|
1
|
-
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),
|
|
1
|
+
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),j=require("./use-local-storage.cjs"),p=require("./use-inkeep-api-client.cjs"),_=30*1e3,q=2147483647,P=()=>{},B=async()=>({});function x(s){return new Date(s.expiresAt).getTime()-_<=Date.now()}const W=({baseUrl:s,appId:t,getCaptchaHeader:$,invalidateCaptcha:D,optOutAllAnalytics:m,enabled:n=!0})=>{const F=n&&t&&!m?`inkeep_session_${t}`:null,[c,h]=j.useLocalStorage(F),E=e.useRef(n);e.useEffect(()=>{const r=E.current;if(E.current=n,r&&!n&&t&&!m){try{localStorage.removeItem(`inkeep_session_${t}`)}catch{}h(null)}},[n,t,m,h]);const{fetchWithAuth:v}=p.useInkeepApiClient({appId:t,authToken:void 0,getCaptchaHeader:$??B,invalidateCaptcha:D??P}),R=e.useRef(s);R.current=s;const y=e.useRef(t);y.current=t;const f=e.useRef(c);f.current=c;const T=e.useRef(h);T.current=h;const g=e.useRef(v);g.current=v;const a=e.useRef(null),w=e.useRef(n);w.current=n;const o=e.useCallback(async r=>{if(!w.current||!R.current||!y.current)return null;if(!r&&a.current)return a.current;const u=`${R.current}/run/auth/apps/${y.current}/anonymous-session`,l=(async()=>{try{const i={"Content-Type":"application/json"},M=f.current?.token;M&&(i.Authorization=`Bearer ${M}`);const A=await g.current(u,{method:"POST",headers:i,signal:r});if(!A.ok)throw new Error(`Failed to fetch anonymous session: ${A.status}`);const S=await A.json();return T.current({token:S.token,expiresAt:S.expiresAt}),S.token}catch(i){return i instanceof Error&&i.name==="AbortError"||console.error("[anonymous-session] fetch failed",i),null}finally{r||(a.current=null)}})();return r||(a.current=l),l},[]),C=e.useRef(!1);e.useEffect(()=>{if(!n||!s||!t)return;const r=!C.current;if(C.current=!0,!r&&f.current&&!x(f.current)||a.current)return;const u=new AbortController;return o(u.signal),()=>u.abort()},[s,t,n,o]),e.useEffect(()=>{if(!n||!s||!t||!c?.expiresAt)return;const k=new Date(c.expiresAt).getTime()-_-Date.now();if(k<=0)return;const l=setTimeout(()=>o(),Math.min(k,q));return()=>clearTimeout(l)},[s,t,n,c?.expiresAt,o]),e.useEffect(()=>{if(!n||!s||!t)return;const r=()=>{if(document.visibilityState!=="visible")return;const u=f.current;(!u||x(u))&&o()};return document.addEventListener("visibilitychange",r),()=>document.removeEventListener("visibilitychange",r)},[s,t,n,o]);const L=e.useCallback(()=>o(),[o]);return{sessionToken:c?.token??null,refreshSession:L}};exports.useAnonymousSession=W;
|
|
@@ -4,8 +4,9 @@ interface UseAnonymousSessionOptions {
|
|
|
4
4
|
getCaptchaHeader?: () => Promise<Record<string, string>>;
|
|
5
5
|
invalidateCaptcha?: () => void;
|
|
6
6
|
optOutAllAnalytics?: boolean;
|
|
7
|
+
enabled?: boolean;
|
|
7
8
|
}
|
|
8
|
-
export declare const useAnonymousSession: ({ baseUrl, appId, getCaptchaHeader, invalidateCaptcha, optOutAllAnalytics, }: UseAnonymousSessionOptions) => {
|
|
9
|
+
export declare const useAnonymousSession: ({ baseUrl, appId, getCaptchaHeader, invalidateCaptcha, optOutAllAnalytics, enabled, }: UseAnonymousSessionOptions) => {
|
|
9
10
|
sessionToken: string | null;
|
|
10
11
|
refreshSession: () => Promise<string | null>;
|
|
11
12
|
};
|
|
@@ -1,82 +1,96 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useRef as
|
|
3
|
-
import { useLocalStorage as
|
|
4
|
-
import { useInkeepApiClient as
|
|
5
|
-
const
|
|
6
|
-
},
|
|
7
|
-
function
|
|
8
|
-
return new Date(
|
|
2
|
+
import { useRef as o, useEffect as l, useCallback as g } from "react";
|
|
3
|
+
import { useLocalStorage as B } from "./use-local-storage.js";
|
|
4
|
+
import { useInkeepApiClient as P } from "./use-inkeep-api-client.js";
|
|
5
|
+
const p = 30 * 1e3, W = 2147483647, X = () => {
|
|
6
|
+
}, z = async () => ({});
|
|
7
|
+
function $(n) {
|
|
8
|
+
return new Date(n.expiresAt).getTime() - p <= Date.now();
|
|
9
9
|
}
|
|
10
|
-
const
|
|
11
|
-
baseUrl:
|
|
10
|
+
const Y = ({
|
|
11
|
+
baseUrl: n,
|
|
12
12
|
appId: e,
|
|
13
|
-
getCaptchaHeader:
|
|
14
|
-
invalidateCaptcha:
|
|
15
|
-
optOutAllAnalytics:
|
|
13
|
+
getCaptchaHeader: D,
|
|
14
|
+
invalidateCaptcha: F,
|
|
15
|
+
optOutAllAnalytics: k,
|
|
16
|
+
enabled: t = !0
|
|
16
17
|
}) => {
|
|
17
|
-
const
|
|
18
|
+
const L = t && e && !k ? `inkeep_session_${e}` : null, [c, h] = B(L), S = o(t);
|
|
19
|
+
l(() => {
|
|
20
|
+
const r = S.current;
|
|
21
|
+
if (S.current = t, r && !t && e && !k) {
|
|
22
|
+
try {
|
|
23
|
+
localStorage.removeItem(`inkeep_session_${e}`);
|
|
24
|
+
} catch {
|
|
25
|
+
}
|
|
26
|
+
h(null);
|
|
27
|
+
}
|
|
28
|
+
}, [t, e, k, h]);
|
|
29
|
+
const { fetchWithAuth: T } = P({
|
|
18
30
|
appId: e,
|
|
19
31
|
authToken: void 0,
|
|
20
|
-
getCaptchaHeader:
|
|
21
|
-
invalidateCaptcha:
|
|
22
|
-
}),
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
getCaptchaHeader: D ?? z,
|
|
33
|
+
invalidateCaptcha: F ?? X
|
|
34
|
+
}), y = o(n);
|
|
35
|
+
y.current = n;
|
|
36
|
+
const A = o(e);
|
|
37
|
+
A.current = e;
|
|
38
|
+
const f = o(c);
|
|
39
|
+
f.current = c;
|
|
40
|
+
const w = o(h);
|
|
41
|
+
w.current = h;
|
|
42
|
+
const x = o(T);
|
|
43
|
+
x.current = T;
|
|
44
|
+
const a = o(null), M = o(t);
|
|
45
|
+
M.current = t;
|
|
46
|
+
const s = g(async (r) => {
|
|
47
|
+
if (!M.current || !y.current || !A.current) return null;
|
|
48
|
+
if (!r && a.current)
|
|
49
|
+
return a.current;
|
|
50
|
+
const i = `${y.current}/run/auth/apps/${A.current}/anonymous-session`, m = (async () => {
|
|
37
51
|
try {
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
const
|
|
52
|
+
const u = { "Content-Type": "application/json" }, _ = f.current?.token;
|
|
53
|
+
_ && (u.Authorization = `Bearer ${_}`);
|
|
54
|
+
const v = await x.current(i, {
|
|
41
55
|
method: "POST",
|
|
42
|
-
headers:
|
|
43
|
-
signal:
|
|
56
|
+
headers: u,
|
|
57
|
+
signal: r
|
|
44
58
|
});
|
|
45
|
-
if (!
|
|
46
|
-
const
|
|
47
|
-
return
|
|
48
|
-
} catch (
|
|
49
|
-
return
|
|
59
|
+
if (!v.ok) throw new Error(`Failed to fetch anonymous session: ${v.status}`);
|
|
60
|
+
const E = await v.json();
|
|
61
|
+
return w.current({ token: E.token, expiresAt: E.expiresAt }), E.token;
|
|
62
|
+
} catch (u) {
|
|
63
|
+
return u instanceof Error && u.name === "AbortError" || console.error("[anonymous-session] fetch failed", u), null;
|
|
50
64
|
} finally {
|
|
51
|
-
|
|
65
|
+
r || (a.current = null);
|
|
52
66
|
}
|
|
53
67
|
})();
|
|
54
|
-
return
|
|
55
|
-
}, []),
|
|
56
|
-
|
|
57
|
-
if (!t || !e) return;
|
|
58
|
-
const
|
|
59
|
-
if (
|
|
60
|
-
const
|
|
61
|
-
return
|
|
62
|
-
}, [
|
|
63
|
-
if (!t || !e || !
|
|
64
|
-
const
|
|
65
|
-
if (
|
|
66
|
-
const
|
|
67
|
-
return () => clearTimeout(
|
|
68
|
-
}, [
|
|
69
|
-
if (!t || !e) return;
|
|
70
|
-
const
|
|
68
|
+
return r || (a.current = m), m;
|
|
69
|
+
}, []), C = o(!1);
|
|
70
|
+
l(() => {
|
|
71
|
+
if (!t || !n || !e) return;
|
|
72
|
+
const r = !C.current;
|
|
73
|
+
if (C.current = !0, !r && f.current && !$(f.current) || a.current) return;
|
|
74
|
+
const i = new AbortController();
|
|
75
|
+
return s(i.signal), () => i.abort();
|
|
76
|
+
}, [n, e, t, s]), l(() => {
|
|
77
|
+
if (!t || !n || !e || !c?.expiresAt) return;
|
|
78
|
+
const R = new Date(c.expiresAt).getTime() - p - Date.now();
|
|
79
|
+
if (R <= 0) return;
|
|
80
|
+
const m = setTimeout(() => s(), Math.min(R, W));
|
|
81
|
+
return () => clearTimeout(m);
|
|
82
|
+
}, [n, e, t, c?.expiresAt, s]), l(() => {
|
|
83
|
+
if (!t || !n || !e) return;
|
|
84
|
+
const r = () => {
|
|
71
85
|
if (document.visibilityState !== "visible") return;
|
|
72
|
-
const
|
|
73
|
-
(!
|
|
86
|
+
const i = f.current;
|
|
87
|
+
(!i || $(i)) && s();
|
|
74
88
|
};
|
|
75
|
-
return document.addEventListener("visibilitychange",
|
|
76
|
-
}, [
|
|
77
|
-
const
|
|
78
|
-
return { sessionToken:
|
|
89
|
+
return document.addEventListener("visibilitychange", r), () => document.removeEventListener("visibilitychange", r);
|
|
90
|
+
}, [n, e, t, s]);
|
|
91
|
+
const j = g(() => s(), [s]);
|
|
92
|
+
return { sessionToken: c?.token ?? null, refreshSession: j };
|
|
79
93
|
};
|
|
80
94
|
export {
|
|
81
|
-
|
|
95
|
+
Y as useAnonymousSession
|
|
82
96
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),T=require("../providers/config-provider.cjs"),p=30*1e3,y=2147483647;function d(c){try{const e=c.split(".")[1];if(!e)return null;const n=JSON.parse(atob(e.replace(/-/g,"+").replace(/_/g,"/")));return typeof n.exp=="number"?n.exp*1e3:null}catch{return null}}const A=()=>{const{baseSettings:c}=T.useInkeepConfig(),{getAuthToken:e}=c,[n,l]=t.useState(null),[h,f]=t.useState(!!e),o=t.useRef(null),i=t.useRef(e);i.current=e;const u=t.useCallback(async()=>{const s=i.current;if(!s)return null;if(o.current)return o.current;const a=(async()=>{try{const r=await s();return l(r),r}catch(r){return console.error("[useAuthToken] getAuthToken failed",r),l(null),null}finally{o.current=null,f(!1)}})();return o.current=a,a},[]);t.useEffect(()=>{if(!e){l(null),f(!1);return}u()},[e,u]),t.useEffect(()=>{if(!e||!n)return;const s=d(n);if(!s)return;const r=s-p-Date.now();if(r<=0)return;const k=setTimeout(()=>u(),Math.min(r,y));return()=>clearTimeout(k)},[e,n,u]);const g=t.useCallback(async()=>i.current?u():null,[u]);return{authToken:n,isLoading:h,refreshToken:g}};exports.useAuthToken=A;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useState as f, useRef as h, useCallback as p, useEffect as k } from "react";
|
|
3
|
+
import { useInkeepConfig as y } from "../providers/config-provider.js";
|
|
4
|
+
const A = 30 * 1e3, d = 2147483647;
|
|
5
|
+
function M(s) {
|
|
6
|
+
try {
|
|
7
|
+
const e = s.split(".")[1];
|
|
8
|
+
if (!e) return null;
|
|
9
|
+
const t = JSON.parse(atob(e.replace(/-/g, "+").replace(/_/g, "/")));
|
|
10
|
+
return typeof t.exp == "number" ? t.exp * 1e3 : null;
|
|
11
|
+
} catch {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const E = () => {
|
|
16
|
+
const { baseSettings: s } = y(), { getAuthToken: e } = s, [t, c] = f(null), [T, a] = f(!!e), o = h(null), l = h(e);
|
|
17
|
+
l.current = e;
|
|
18
|
+
const r = p(async () => {
|
|
19
|
+
const u = l.current;
|
|
20
|
+
if (!u) return null;
|
|
21
|
+
if (o.current) return o.current;
|
|
22
|
+
const i = (async () => {
|
|
23
|
+
try {
|
|
24
|
+
const n = await u();
|
|
25
|
+
return c(n), n;
|
|
26
|
+
} catch (n) {
|
|
27
|
+
return console.error("[useAuthToken] getAuthToken failed", n), c(null), null;
|
|
28
|
+
} finally {
|
|
29
|
+
o.current = null, a(!1);
|
|
30
|
+
}
|
|
31
|
+
})();
|
|
32
|
+
return o.current = i, i;
|
|
33
|
+
}, []);
|
|
34
|
+
k(() => {
|
|
35
|
+
if (!e) {
|
|
36
|
+
c(null), a(!1);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
r();
|
|
40
|
+
}, [e, r]), k(() => {
|
|
41
|
+
if (!e || !t) return;
|
|
42
|
+
const u = M(t);
|
|
43
|
+
if (!u) return;
|
|
44
|
+
const n = u - A - Date.now();
|
|
45
|
+
if (n <= 0) return;
|
|
46
|
+
const m = setTimeout(() => r(), Math.min(n, d));
|
|
47
|
+
return () => clearTimeout(m);
|
|
48
|
+
}, [e, t, r]);
|
|
49
|
+
const g = p(async () => l.current ? r() : null, [r]);
|
|
50
|
+
return { authToken: t, isLoading: T, refreshToken: g };
|
|
51
|
+
};
|
|
52
|
+
export {
|
|
53
|
+
E as useAuthToken
|
|
54
|
+
};
|
|
@@ -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.22",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.22",
|
|
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
|
|
1
|
+
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const te=require("react/jsx-runtime"),e=require("react"),se=require("../components/embedded-chat/chat-provider.cjs"),ne=require("./config-provider.cjs"),re=require("../hooks/use-inkeep-api-client.cjs"),Z=e.createContext(void 0),U=25;function oe(t){const{stop:u,clear:f,conversationId:l,isStreaming:R,loadAndRestoreSession:H,sessionToken:r,refreshSession:_,getCaptchaHeader:z,invalidateCaptcha:B}=se.useChat(),{aiChatSettings:J,baseSettings:Q}=ne.useInkeepConfig(),{baseUrl:S,appId:I,apiKey:V}=J,i=!!Q.getAuthToken,a=!V&&!!I&&(!!r||i),v=r,{fetchWithAuth:P}=re.useInkeepApiClient({appId:I,authToken:v,getCaptchaHeader:z,invalidateCaptcha:B,refreshSession:_}),[b,k]=e.useState(!1),[E,x]=e.useState([]),[w,m]=e.useState(!1),[T,q]=e.useState(0),[A,j]=e.useState(!1),h=e.useRef(!1),M=e.useRef(null),$=e.useRef(i),L=e.useRef(r);e.useEffect(()=>{const n=$.current,s=L.current;$.current=i,L.current=r,(n!==i||s!==null&&r!==null&&s!==r)&&(x([]),q(0),j(!1),h.current=!1)},[i,r]);const O=e.useRef([]);O.current=E;const D=e.useRef(P);D.current=P;const c=e.useCallback(async(n,s=!1)=>{if(!(!S||!v||!a)){s||m(!0);try{const C=`${S}/run/v1/conversations?page=${n+1}&limit=${U}`,g=await D.current(C);if(!g.ok)throw new Error(`Failed to fetch conversations: ${g.status}`);const W=await g.json(),p=(W.data??[]).map(o=>({id:o.id,title:o.title??"",createdAt:new Date(o.createdAt),updatedAt:new Date(o.updatedAt)}));x(o=>{if(n===0&&!s)return p;if(n===0&&s){const d=new Set(p.map(y=>y.id));return[...p,...o.slice(U).filter(y=>!d.has(y.id))]}const ee=new Set(o.map(d=>d.id));return[...o,...p.filter(d=>!ee.has(d.id))]});const{page:G,pages:K}=W.pagination??{};j(G!=null&&K!=null?G<K:!1),s||q(n)}catch(C){console.error("[useChatHistory] Failed to load conversations:",C)}finally{m(!1)}}},[S,v,a]);e.useEffect(()=>{!b||!r&&!i||!a||(t==="stack"?c(0):h.current||(h.current=!0,c(0)))},[b,r,i,a,t,c]);const F=e.useRef("");e.useEffect(()=>{a&&t==="sidepane"&&(!l||!R||h.current&&F.current!==l&&(O.current.some(n=>n.id===l)||(F.current=l,c(0,!0))))},[l,a,t,c,R]);const X=e.useCallback(()=>{!w&&A&&c(T+1)},[w,A,T,c]),Y=e.useCallback(()=>{u(),f(),t!=="sidepane"&&k(!1)},[t,u,f]),N=e.useCallback(async n=>{M.current?.abort();const s=new AbortController;M.current=s,await H(n,s.signal),!s.signal.aborted&&t!=="sidepane"&&k(!1)},[t,H]);return{isEnabled:a,isOpen:b,setIsOpen:k,sessions:E,isLoading:w,hasMore:A,loadMore:X,startNewConversation:Y,loadSession:N}}const ie=({layout:t,children:u})=>{const f=oe(t);return te.jsx(Z.Provider,{value:f,children:typeof u=="function"?u(f):u})},ae=()=>{const t=e.useContext(Z);if(!t)throw new Error("useChatHistory must be used within a ChatHistoryProvider");return t};exports.ChatHistoryProvider=ie;exports.useChatHistory=ae;
|
|
@@ -1,105 +1,110 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { jsx as
|
|
3
|
-
import { createContext as
|
|
4
|
-
import { useChat as
|
|
5
|
-
import { useInkeepConfig as
|
|
6
|
-
import { useInkeepApiClient as
|
|
7
|
-
const
|
|
8
|
-
function
|
|
2
|
+
import { jsx as re } from "react/jsx-runtime";
|
|
3
|
+
import { createContext as oe, useContext as ie, useState as h, useRef as c, useEffect as y, useCallback as S } from "react";
|
|
4
|
+
import { useChat as ae } from "../components/embedded-chat/chat-provider.js";
|
|
5
|
+
import { useInkeepConfig as ce } from "./config-provider.js";
|
|
6
|
+
import { useInkeepApiClient as ue } from "../hooks/use-inkeep-api-client.js";
|
|
7
|
+
const B = oe(void 0), z = 25;
|
|
8
|
+
function fe(e) {
|
|
9
9
|
const {
|
|
10
|
-
stop:
|
|
11
|
-
clear:
|
|
12
|
-
conversationId:
|
|
13
|
-
isStreaming:
|
|
14
|
-
loadAndRestoreSession:
|
|
15
|
-
sessionToken:
|
|
16
|
-
refreshSession:
|
|
17
|
-
getCaptchaHeader:
|
|
18
|
-
invalidateCaptcha:
|
|
19
|
-
} =
|
|
20
|
-
appId:
|
|
21
|
-
authToken:
|
|
22
|
-
getCaptchaHeader:
|
|
23
|
-
invalidateCaptcha:
|
|
24
|
-
refreshSession:
|
|
25
|
-
}), [
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
10
|
+
stop: u,
|
|
11
|
+
clear: f,
|
|
12
|
+
conversationId: d,
|
|
13
|
+
isStreaming: P,
|
|
14
|
+
loadAndRestoreSession: x,
|
|
15
|
+
sessionToken: s,
|
|
16
|
+
refreshSession: J,
|
|
17
|
+
getCaptchaHeader: Q,
|
|
18
|
+
invalidateCaptcha: V
|
|
19
|
+
} = ae(), { aiChatSettings: X, baseSettings: Y } = ce(), { baseUrl: w, appId: E, apiKey: N } = X, o = !!Y.getAuthToken, i = !N && !!E && (!!s || o), m = s, { fetchWithAuth: R } = ue({
|
|
20
|
+
appId: E,
|
|
21
|
+
authToken: m,
|
|
22
|
+
getCaptchaHeader: Q,
|
|
23
|
+
invalidateCaptcha: V,
|
|
24
|
+
refreshSession: J
|
|
25
|
+
}), [A, k] = h(!1), [T, $] = h([]), [b, L] = h(!1), [M, j] = h(0), [I, D] = h(!1), p = c(!1), F = c(null), O = c(o), W = c(s);
|
|
26
|
+
y(() => {
|
|
27
|
+
const n = O.current, t = W.current;
|
|
28
|
+
O.current = o, W.current = s, (n !== o || t !== null && s !== null && t !== s) && ($([]), j(0), D(!1), p.current = !1);
|
|
29
|
+
}, [o, s]);
|
|
30
|
+
const G = c([]);
|
|
31
|
+
G.current = T;
|
|
32
|
+
const K = c(R);
|
|
33
|
+
K.current = R;
|
|
34
|
+
const a = S(
|
|
35
|
+
async (n, t = !1) => {
|
|
36
|
+
if (!(!w || !m || !i)) {
|
|
37
|
+
t || L(!0);
|
|
33
38
|
try {
|
|
34
|
-
const
|
|
35
|
-
if (!
|
|
36
|
-
const
|
|
37
|
-
id:
|
|
38
|
-
title:
|
|
39
|
-
createdAt: new Date(
|
|
40
|
-
updatedAt: new Date(
|
|
39
|
+
const C = `${w}/run/v1/conversations?page=${n + 1}&limit=${z}`, g = await K.current(C);
|
|
40
|
+
if (!g.ok) throw new Error(`Failed to fetch conversations: ${g.status}`);
|
|
41
|
+
const Z = await g.json(), v = (Z.data ?? []).map((r) => ({
|
|
42
|
+
id: r.id,
|
|
43
|
+
title: r.title ?? "",
|
|
44
|
+
createdAt: new Date(r.createdAt),
|
|
45
|
+
updatedAt: new Date(r.updatedAt)
|
|
41
46
|
}));
|
|
42
|
-
|
|
43
|
-
if (
|
|
44
|
-
if (
|
|
45
|
-
const
|
|
46
|
-
return [...
|
|
47
|
+
$((r) => {
|
|
48
|
+
if (n === 0 && !t) return v;
|
|
49
|
+
if (n === 0 && t) {
|
|
50
|
+
const l = new Set(v.map((H) => H.id));
|
|
51
|
+
return [...v, ...r.slice(z).filter((H) => !l.has(H.id))];
|
|
47
52
|
}
|
|
48
|
-
const
|
|
49
|
-
return [...
|
|
53
|
+
const se = new Set(r.map((l) => l.id));
|
|
54
|
+
return [...r, ...v.filter((l) => !se.has(l.id))];
|
|
50
55
|
});
|
|
51
|
-
const { page:
|
|
52
|
-
|
|
53
|
-
} catch (
|
|
54
|
-
console.error("[useChatHistory] Failed to load conversations:",
|
|
56
|
+
const { page: _, pages: q } = Z.pagination ?? {};
|
|
57
|
+
D(_ != null && q != null ? _ < q : !1), t || j(n);
|
|
58
|
+
} catch (C) {
|
|
59
|
+
console.error("[useChatHistory] Failed to load conversations:", C);
|
|
55
60
|
} finally {
|
|
56
|
-
|
|
61
|
+
L(!1);
|
|
57
62
|
}
|
|
58
63
|
}
|
|
59
64
|
},
|
|
60
|
-
[
|
|
65
|
+
[w, m, i]
|
|
61
66
|
);
|
|
62
|
-
|
|
63
|
-
!
|
|
64
|
-
}, [
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}, [
|
|
69
|
-
const
|
|
70
|
-
!
|
|
71
|
-
}, [
|
|
72
|
-
|
|
73
|
-
}, [
|
|
74
|
-
async (
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
|
|
67
|
+
y(() => {
|
|
68
|
+
!A || !s && !o || !i || (e === "stack" ? a(0) : p.current || (p.current = !0, a(0)));
|
|
69
|
+
}, [A, s, o, i, e, a]);
|
|
70
|
+
const U = c("");
|
|
71
|
+
y(() => {
|
|
72
|
+
i && e === "sidepane" && (!d || !P || p.current && U.current !== d && (G.current.some((n) => n.id === d) || (U.current = d, a(0, !0))));
|
|
73
|
+
}, [d, i, e, a, P]);
|
|
74
|
+
const ee = S(() => {
|
|
75
|
+
!b && I && a(M + 1);
|
|
76
|
+
}, [b, I, M, a]), te = S(() => {
|
|
77
|
+
u(), f(), e !== "sidepane" && k(!1);
|
|
78
|
+
}, [e, u, f]), ne = S(
|
|
79
|
+
async (n) => {
|
|
80
|
+
F.current?.abort();
|
|
81
|
+
const t = new AbortController();
|
|
82
|
+
F.current = t, await x(n, t.signal), !t.signal.aborted && e !== "sidepane" && k(!1);
|
|
78
83
|
},
|
|
79
|
-
[
|
|
84
|
+
[e, x]
|
|
80
85
|
);
|
|
81
86
|
return {
|
|
82
|
-
isEnabled:
|
|
83
|
-
isOpen:
|
|
84
|
-
setIsOpen:
|
|
85
|
-
sessions:
|
|
86
|
-
isLoading:
|
|
87
|
-
hasMore:
|
|
88
|
-
loadMore:
|
|
89
|
-
startNewConversation:
|
|
90
|
-
loadSession:
|
|
87
|
+
isEnabled: i,
|
|
88
|
+
isOpen: A,
|
|
89
|
+
setIsOpen: k,
|
|
90
|
+
sessions: T,
|
|
91
|
+
isLoading: b,
|
|
92
|
+
hasMore: I,
|
|
93
|
+
loadMore: ee,
|
|
94
|
+
startNewConversation: te,
|
|
95
|
+
loadSession: ne
|
|
91
96
|
};
|
|
92
97
|
}
|
|
93
|
-
const
|
|
94
|
-
const
|
|
95
|
-
return /* @__PURE__ */
|
|
96
|
-
},
|
|
97
|
-
const
|
|
98
|
-
if (!
|
|
98
|
+
const ve = ({ layout: e, children: u }) => {
|
|
99
|
+
const f = fe(e);
|
|
100
|
+
return /* @__PURE__ */ re(B.Provider, { value: f, children: typeof u == "function" ? u(f) : u });
|
|
101
|
+
}, Se = () => {
|
|
102
|
+
const e = ie(B);
|
|
103
|
+
if (!e)
|
|
99
104
|
throw new Error("useChatHistory must be used within a ChatHistoryProvider");
|
|
100
|
-
return
|
|
105
|
+
return e;
|
|
101
106
|
};
|
|
102
107
|
export {
|
|
103
|
-
|
|
104
|
-
|
|
108
|
+
ve as ChatHistoryProvider,
|
|
109
|
+
Se as useChatHistory
|
|
105
110
|
};
|
|
@@ -59,8 +59,15 @@ export interface InkeepBaseSettings {
|
|
|
59
59
|
/**
|
|
60
60
|
* Authentication token for the current user.
|
|
61
61
|
* Used for authenticated API requests if required.
|
|
62
|
+
* @deprecated Use getAuthToken instead for automatic token refresh on 401.
|
|
62
63
|
*/
|
|
63
64
|
userAuthToken?: string;
|
|
65
|
+
/**
|
|
66
|
+
* Async callback that returns a JWT for authenticated chat sessions.
|
|
67
|
+
* When provided, the widget uses JWT-based auth instead of anonymous sessions.
|
|
68
|
+
* The callback is re-invoked automatically on 401 responses to refresh the token.
|
|
69
|
+
*/
|
|
70
|
+
getAuthToken?: () => Promise<string>;
|
|
64
71
|
/**
|
|
65
72
|
* Additional properties to be sent with analytics events.
|
|
66
73
|
* These properties will be merged with the event properties.
|