@inkeep/agents-ui 0.0.0-dev-20260501205503 → 0.0.0-dev-20260501215045
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/chat-error-helpers.d.ts +2 -4
- package/dist/primitives/components/embedded-chat/use-captcha.cjs +1 -0
- package/dist/primitives/components/embedded-chat/use-captcha.d.ts +30 -0
- package/dist/primitives/components/embedded-chat/use-captcha.js +67 -0
- package/dist/primitives/components/embedded-chat/use-inkeep-chat.cjs +2 -2
- package/dist/primitives/components/embedded-chat/use-inkeep-chat.d.ts +2 -0
- package/dist/primitives/components/embedded-chat/use-inkeep-chat.js +262 -237
- package/dist/primitives/hooks/use-anonymous-session.cjs +1 -1
- package/dist/primitives/hooks/use-anonymous-session.d.ts +3 -1
- package/dist/primitives/hooks/use-anonymous-session.js +56 -51
- package/dist/primitives/hooks/use-conversation-loader.cjs +1 -1
- package/dist/primitives/hooks/use-conversation-loader.d.ts +3 -1
- package/dist/primitives/hooks/use-conversation-loader.js +20 -15
- package/dist/primitives/hooks/use-feedback-api.cjs +1 -1
- package/dist/primitives/hooks/use-feedback-api.d.ts +3 -1
- package/dist/primitives/hooks/use-feedback-api.js +22 -16
- package/dist/primitives/hooks/use-inkeep-api-client.cjs +1 -1
- package/dist/primitives/hooks/use-inkeep-api-client.d.ts +11 -8
- package/dist/primitives/hooks/use-inkeep-api-client.js +64 -36
- package/dist/primitives/providers/chat-history-provider.cjs +1 -1
- package/dist/primitives/providers/chat-history-provider.js +39 -35
- package/dist/primitives/providers/feedback-provider.cjs +1 -1
- package/dist/primitives/providers/feedback-provider.js +35 -26
- package/dist/primitives/providers/root-provider.cjs +1 -1
- package/dist/primitives/providers/root-provider.js +9 -13
- package/dist/primitives/utils/default-settings.cjs +1 -1
- package/dist/primitives/utils/default-settings.d.ts +0 -1
- package/dist/primitives/utils/default-settings.js +5 -6
- package/dist/types/config/ai.d.ts +0 -8
- package/package.json +2 -2
- package/dist/node_modules/.pnpm/botid@1.5.11_next@16.2.4_@babel_core@7.29.0_@opentelemetry_api@1.9.1_react-dom@19.3.0-c_6ac35f5bf61166da3243b6cf36e09c95/node_modules/botid/dist/client/core/index.cjs +0 -1
- package/dist/node_modules/.pnpm/botid@1.5.11_next@16.2.4_@babel_core@7.29.0_@opentelemetry_api@1.9.1_react-dom@19.3.0-c_6ac35f5bf61166da3243b6cf36e09c95/node_modules/botid/dist/client/core/index.js +0 -160
- package/dist/primitives/providers/botid-initializer.cjs +0 -1
- package/dist/primitives/providers/botid-initializer.d.ts +0 -15
- package/dist/primitives/providers/botid-initializer.js +0 -69
|
@@ -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;
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
interface UseAnonymousSessionOptions {
|
|
2
2
|
baseUrl?: string;
|
|
3
3
|
appId?: string;
|
|
4
|
+
getCaptchaHeader?: () => Promise<Record<string, string>>;
|
|
5
|
+
invalidateCaptcha?: () => void;
|
|
4
6
|
optOutAllAnalytics?: boolean;
|
|
5
7
|
enabled?: boolean;
|
|
6
8
|
}
|
|
7
|
-
export declare const useAnonymousSession: ({ baseUrl, appId, optOutAllAnalytics, enabled, }: UseAnonymousSessionOptions) => {
|
|
9
|
+
export declare const useAnonymousSession: ({ baseUrl, appId, getCaptchaHeader, invalidateCaptcha, optOutAllAnalytics, enabled, }: UseAnonymousSessionOptions) => {
|
|
8
10
|
sessionToken: string | null;
|
|
9
11
|
refreshSession: () => Promise<string | null>;
|
|
10
12
|
};
|
|
@@ -1,91 +1,96 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useRef as o, useEffect as
|
|
3
|
-
import { useLocalStorage as
|
|
4
|
-
import { useInkeepApiClient as
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
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();
|
|
8
9
|
}
|
|
9
|
-
const
|
|
10
|
+
const Y = ({
|
|
10
11
|
baseUrl: n,
|
|
11
12
|
appId: e,
|
|
13
|
+
getCaptchaHeader: D,
|
|
14
|
+
invalidateCaptcha: F,
|
|
12
15
|
optOutAllAnalytics: k,
|
|
13
16
|
enabled: t = !0
|
|
14
17
|
}) => {
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
const r =
|
|
18
|
-
if (
|
|
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) {
|
|
19
22
|
try {
|
|
20
23
|
localStorage.removeItem(`inkeep_session_${e}`);
|
|
21
24
|
} catch {
|
|
22
25
|
}
|
|
23
|
-
|
|
26
|
+
h(null);
|
|
24
27
|
}
|
|
25
|
-
}, [t, e, k,
|
|
26
|
-
const { fetchWithAuth:
|
|
28
|
+
}, [t, e, k, h]);
|
|
29
|
+
const { fetchWithAuth: T } = P({
|
|
27
30
|
appId: e,
|
|
28
|
-
authToken: void 0
|
|
31
|
+
authToken: void 0,
|
|
32
|
+
getCaptchaHeader: D ?? z,
|
|
33
|
+
invalidateCaptcha: F ?? X
|
|
29
34
|
}), y = o(n);
|
|
30
35
|
y.current = n;
|
|
31
36
|
const A = o(e);
|
|
32
37
|
A.current = e;
|
|
33
|
-
const f = o(
|
|
34
|
-
f.current =
|
|
35
|
-
const w = o(
|
|
36
|
-
w.current =
|
|
37
|
-
const x = o(
|
|
38
|
-
x.current =
|
|
39
|
-
const
|
|
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);
|
|
40
45
|
M.current = t;
|
|
41
|
-
const s =
|
|
46
|
+
const s = g(async (r) => {
|
|
42
47
|
if (!M.current || !y.current || !A.current) return null;
|
|
43
|
-
if (!r &&
|
|
44
|
-
return
|
|
45
|
-
const i = `${y.current}/run/auth/apps/${A.current}/anonymous-session`,
|
|
48
|
+
if (!r && a.current)
|
|
49
|
+
return a.current;
|
|
50
|
+
const i = `${y.current}/run/auth/apps/${A.current}/anonymous-session`, m = (async () => {
|
|
46
51
|
try {
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
const
|
|
52
|
+
const u = { "Content-Type": "application/json" }, _ = f.current?.token;
|
|
53
|
+
_ && (u.Authorization = `Bearer ${_}`);
|
|
54
|
+
const v = await x.current(i, {
|
|
50
55
|
method: "POST",
|
|
51
|
-
headers:
|
|
56
|
+
headers: u,
|
|
52
57
|
signal: r
|
|
53
58
|
});
|
|
54
|
-
if (!
|
|
55
|
-
const
|
|
56
|
-
return w.current({ token:
|
|
57
|
-
} catch (
|
|
58
|
-
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;
|
|
59
64
|
} finally {
|
|
60
|
-
r || (
|
|
65
|
+
r || (a.current = null);
|
|
61
66
|
}
|
|
62
67
|
})();
|
|
63
|
-
return r || (
|
|
64
|
-
}, []),
|
|
65
|
-
|
|
68
|
+
return r || (a.current = m), m;
|
|
69
|
+
}, []), C = o(!1);
|
|
70
|
+
l(() => {
|
|
66
71
|
if (!t || !n || !e) return;
|
|
67
|
-
const r = !
|
|
68
|
-
if (
|
|
72
|
+
const r = !C.current;
|
|
73
|
+
if (C.current = !0, !r && f.current && !$(f.current) || a.current) return;
|
|
69
74
|
const i = new AbortController();
|
|
70
75
|
return s(i.signal), () => i.abort();
|
|
71
|
-
}, [n, e, t, s]),
|
|
72
|
-
if (!t || !n || !e || !
|
|
73
|
-
const R = new Date(
|
|
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();
|
|
74
79
|
if (R <= 0) return;
|
|
75
|
-
const
|
|
76
|
-
return () => clearTimeout(
|
|
77
|
-
}, [n, e, t,
|
|
80
|
+
const m = setTimeout(() => s(), Math.min(R, W));
|
|
81
|
+
return () => clearTimeout(m);
|
|
82
|
+
}, [n, e, t, c?.expiresAt, s]), l(() => {
|
|
78
83
|
if (!t || !n || !e) return;
|
|
79
84
|
const r = () => {
|
|
80
85
|
if (document.visibilityState !== "visible") return;
|
|
81
86
|
const i = f.current;
|
|
82
|
-
(!i ||
|
|
87
|
+
(!i || $(i)) && s();
|
|
83
88
|
};
|
|
84
89
|
return document.addEventListener("visibilitychange", r), () => document.removeEventListener("visibilitychange", r);
|
|
85
90
|
}, [n, e, t, s]);
|
|
86
|
-
const
|
|
87
|
-
return { sessionToken:
|
|
91
|
+
const j = g(() => s(), [s]);
|
|
92
|
+
return { sessionToken: c?.token ?? null, refreshSession: j };
|
|
88
93
|
};
|
|
89
94
|
export {
|
|
90
|
-
|
|
95
|
+
Y as useAnonymousSession
|
|
91
96
|
};
|
|
@@ -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 d=require("react"),v=require("./use-inkeep-api-client.cjs"),C=()=>{},p=({baseUrl:o,appId:s,authToken:n,getCaptchaHeader:a,invalidateCaptcha:i,refreshSession:c})=>{const{fetchWithAuth:t}=v.useInkeepApiClient({appId:s,authToken:n,getCaptchaHeader:a,invalidateCaptcha:i??C,refreshSession:c});return{loadConversation:d.useCallback(async(u,l)=>{if(!o||!n)return null;try{const e=`${o}/run/v1/conversations/${u}`,r=await t(e,{signal:l});if(!r.ok)throw new Error(`Failed to load conversation: ${r.status}`);return(await r.json()).data?.messages??[]}catch(e){return e instanceof Error&&e.name==="AbortError"||console.error("[useConversationLoader]",e),null}},[o,n,t])}};exports.useConversationLoader=p;
|
|
@@ -3,9 +3,11 @@ interface UseConversationLoaderOptions {
|
|
|
3
3
|
baseUrl: string | undefined;
|
|
4
4
|
appId: string | undefined;
|
|
5
5
|
authToken: string | null;
|
|
6
|
+
getCaptchaHeader: () => Promise<Record<string, string>>;
|
|
7
|
+
invalidateCaptcha?: () => void;
|
|
6
8
|
refreshSession?: () => Promise<string | null>;
|
|
7
9
|
}
|
|
8
|
-
export declare const useConversationLoader: ({ baseUrl, appId, authToken, refreshSession, }: UseConversationLoaderOptions) => {
|
|
10
|
+
export declare const useConversationLoader: ({ baseUrl, appId, authToken, getCaptchaHeader, invalidateCaptcha, refreshSession, }: UseConversationLoaderOptions) => {
|
|
9
11
|
loadConversation: (conversationId: string, signal?: AbortSignal) => Promise<Message[] | null>;
|
|
10
12
|
};
|
|
11
13
|
export {};
|
|
@@ -1,31 +1,36 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useCallback as
|
|
3
|
-
import { useInkeepApiClient as
|
|
4
|
-
const
|
|
5
|
-
|
|
2
|
+
import { useCallback as d } from "react";
|
|
3
|
+
import { useInkeepApiClient as v } from "./use-inkeep-api-client.js";
|
|
4
|
+
const f = () => {
|
|
5
|
+
}, w = ({
|
|
6
|
+
baseUrl: r,
|
|
6
7
|
appId: s,
|
|
7
8
|
authToken: n,
|
|
8
|
-
|
|
9
|
+
getCaptchaHeader: a,
|
|
10
|
+
invalidateCaptcha: i,
|
|
11
|
+
refreshSession: c
|
|
9
12
|
}) => {
|
|
10
|
-
const { fetchWithAuth: e } =
|
|
13
|
+
const { fetchWithAuth: e } = v({
|
|
11
14
|
appId: s,
|
|
12
15
|
authToken: n,
|
|
13
|
-
|
|
16
|
+
getCaptchaHeader: a,
|
|
17
|
+
invalidateCaptcha: i ?? f,
|
|
18
|
+
refreshSession: c
|
|
14
19
|
});
|
|
15
|
-
return { loadConversation:
|
|
16
|
-
async (
|
|
17
|
-
if (!
|
|
20
|
+
return { loadConversation: d(
|
|
21
|
+
async (l, u) => {
|
|
22
|
+
if (!r || !n) return null;
|
|
18
23
|
try {
|
|
19
|
-
const
|
|
24
|
+
const o = `${r}/run/v1/conversations/${l}`, t = await e(o, { signal: u });
|
|
20
25
|
if (!t.ok) throw new Error(`Failed to load conversation: ${t.status}`);
|
|
21
26
|
return (await t.json()).data?.messages ?? [];
|
|
22
|
-
} catch (
|
|
23
|
-
return
|
|
27
|
+
} catch (o) {
|
|
28
|
+
return o instanceof Error && o.name === "AbortError" || console.error("[useConversationLoader]", o), null;
|
|
24
29
|
}
|
|
25
30
|
},
|
|
26
|
-
[
|
|
31
|
+
[r, n, e]
|
|
27
32
|
) };
|
|
28
33
|
};
|
|
29
34
|
export {
|
|
30
|
-
|
|
35
|
+
w as useConversationLoader
|
|
31
36
|
};
|
|
@@ -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 l=require("react"),f=require("./use-inkeep-api-client.cjs"),k=()=>{};function y({baseUrl:e,appId:s,authToken:t,getCaptchaHeader:c,invalidateCaptcha:o,refreshSession:r}){const{fetchWithAuth:i}=f.useInkeepApiClient({appId:s,authToken:t,getCaptchaHeader:c,invalidateCaptcha:o??k,refreshSession:r});return{submitFeedback:l.useCallback(async({conversationId:u,messageId:a,type:d,details:p})=>{if(!e||!t)return;const b=`${e}/run/v1/feedback`,n=await i(b,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({conversationId:u,messageId:a,type:d,details:p})});if(!n.ok)throw new Error(`Feedback submission failed: ${n.status}`);return n.json()},[e,t,i])}}exports.useFeedbackApi=y;
|
|
@@ -3,6 +3,8 @@ interface UseFeedbackApiOptions {
|
|
|
3
3
|
baseUrl: string | undefined;
|
|
4
4
|
appId: string | undefined;
|
|
5
5
|
authToken: string | null | undefined;
|
|
6
|
+
getCaptchaHeader: () => Promise<Record<string, string>>;
|
|
7
|
+
invalidateCaptcha?: () => void;
|
|
6
8
|
refreshSession?: () => Promise<string | null>;
|
|
7
9
|
}
|
|
8
10
|
interface SubmitFeedbackParams {
|
|
@@ -11,7 +13,7 @@ interface SubmitFeedbackParams {
|
|
|
11
13
|
type: FeedbackType;
|
|
12
14
|
details?: string | null;
|
|
13
15
|
}
|
|
14
|
-
export declare function useFeedbackApi({ baseUrl, appId, authToken, refreshSession, }: UseFeedbackApiOptions): {
|
|
16
|
+
export declare function useFeedbackApi({ baseUrl, appId, authToken, getCaptchaHeader, invalidateCaptcha, refreshSession, }: UseFeedbackApiOptions): {
|
|
15
17
|
submitFeedback: ({ conversationId, messageId, type, details }: SubmitFeedbackParams) => Promise<any>;
|
|
16
18
|
};
|
|
17
19
|
export {};
|
|
@@ -1,31 +1,37 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useCallback as
|
|
3
|
-
import { useInkeepApiClient as
|
|
4
|
-
|
|
2
|
+
import { useCallback as b } from "react";
|
|
3
|
+
import { useInkeepApiClient as m } from "./use-inkeep-api-client.js";
|
|
4
|
+
const k = () => {
|
|
5
|
+
};
|
|
6
|
+
function C({
|
|
5
7
|
baseUrl: e,
|
|
6
|
-
appId:
|
|
8
|
+
appId: o,
|
|
7
9
|
authToken: t,
|
|
8
|
-
|
|
10
|
+
getCaptchaHeader: s,
|
|
11
|
+
invalidateCaptcha: r,
|
|
12
|
+
refreshSession: c
|
|
9
13
|
}) {
|
|
10
|
-
const { fetchWithAuth:
|
|
11
|
-
appId:
|
|
14
|
+
const { fetchWithAuth: i } = m({
|
|
15
|
+
appId: o,
|
|
12
16
|
authToken: t,
|
|
13
|
-
|
|
17
|
+
getCaptchaHeader: s,
|
|
18
|
+
invalidateCaptcha: r ?? k,
|
|
19
|
+
refreshSession: c
|
|
14
20
|
});
|
|
15
|
-
return { submitFeedback:
|
|
16
|
-
async ({ conversationId:
|
|
21
|
+
return { submitFeedback: b(
|
|
22
|
+
async ({ conversationId: a, messageId: u, type: p, details: d }) => {
|
|
17
23
|
if (!e || !t) return;
|
|
18
|
-
const
|
|
24
|
+
const f = `${e}/run/v1/feedback`, n = await i(f, {
|
|
19
25
|
method: "POST",
|
|
20
26
|
headers: { "Content-Type": "application/json" },
|
|
21
|
-
body: JSON.stringify({ conversationId:
|
|
27
|
+
body: JSON.stringify({ conversationId: a, messageId: u, type: p, details: d })
|
|
22
28
|
});
|
|
23
|
-
if (!
|
|
24
|
-
return
|
|
29
|
+
if (!n.ok) throw new Error(`Feedback submission failed: ${n.status}`);
|
|
30
|
+
return n.json();
|
|
25
31
|
},
|
|
26
|
-
[e, t,
|
|
32
|
+
[e, t, i]
|
|
27
33
|
) };
|
|
28
34
|
}
|
|
29
35
|
export {
|
|
30
|
-
|
|
36
|
+
C as useFeedbackApi
|
|
31
37
|
};
|
|
@@ -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 u=require("react");function y(e,r){if(e===400&&typeof r=="object"&&r!==null){const t=(r.detail??"").toLowerCase();if(t.includes("challenge")||t.includes("proof-of-work"))return"pow"}if(e===401&&typeof r=="object"&&r!==null){const t=r.detail??"";if(t.includes("Invalid end-user JWT")||t.includes("Bearer token required"))return"session"}return null}function H(e){if(!e)return{};if(e instanceof Headers){const r={};return e.forEach((t,c)=>{r[c]=t}),r}return Array.isArray(e)?Object.fromEntries(e):e}function p(e,r,t,c){return{...e?{"x-inkeep-app-id":e}:{},...r?{Authorization:`Bearer ${r}`}:{},...H(c),...t}}function g({appId:e,authToken:r,getCaptchaHeader:t,invalidateCaptcha:c,refreshSession:R}){const a=u.useRef(e);a.current=e;const i=u.useRef(r);i.current=r;const o=u.useRef(t);o.current=t;const w=u.useRef(c);w.current=c;const h=u.useRef(R);return h.current=R,{fetchWithAuth:u.useCallback(async(l,s)=>{const C=await o.current(),j=p(a.current,i.current,C,s?.headers),n=await fetch(l,{...s,headers:j});if(n.status!==400&&n.status!==401)return n;let A;try{A=await n.clone().json()}catch{return n}const k=y(n.status,A);if(k==="pow"){w.current();const f=await o.current(),d=p(a.current,i.current,f,s?.headers);return fetch(l,{...s,headers:d})}if(k==="session"&&h.current){const f=await h.current();if(!f)return n;const d=await o.current(),E=p(a.current,f,d,s?.headers);return fetch(l,{...s,headers:E})}return n},[])}}exports.parseAuthError=y;exports.useInkeepApiClient=g;
|
|
@@ -2,6 +2,8 @@ interface UseInkeepApiClientOptions {
|
|
|
2
2
|
appId: string | undefined;
|
|
3
3
|
/** The bearer token to send as Authorization — pass undefined for unauthenticated calls. */
|
|
4
4
|
authToken: string | null | undefined;
|
|
5
|
+
getCaptchaHeader: () => Promise<Record<string, string>>;
|
|
6
|
+
invalidateCaptcha: () => void;
|
|
5
7
|
/**
|
|
6
8
|
* Called when a 401 session error is detected.
|
|
7
9
|
* Should refresh the anonymous session and return the new token, or null on failure.
|
|
@@ -10,21 +12,22 @@ interface UseInkeepApiClientOptions {
|
|
|
10
12
|
refreshSession?: () => Promise<string | null>;
|
|
11
13
|
}
|
|
12
14
|
/**
|
|
13
|
-
* Parses
|
|
14
|
-
*
|
|
15
|
+
* Parses an API error response body to determine the failure type.
|
|
16
|
+
*
|
|
17
|
+
* The Run API returns:
|
|
18
|
+
* - 400 + detail containing "challenge" or "proof-of-work" → PoW failure
|
|
19
|
+
* - 401 + detail matching JWT/bearer messages → session token failure
|
|
15
20
|
*/
|
|
16
|
-
export declare function parseAuthError(status: number, body: unknown): 'session' | null;
|
|
21
|
+
export declare function parseAuthError(status: number, body: unknown): 'pow' | 'session' | null;
|
|
17
22
|
/**
|
|
18
23
|
* Returns a stable `fetchWithAuth` function that automatically:
|
|
19
|
-
* - Injects auth headers (x-inkeep-app-id, Authorization)
|
|
24
|
+
* - Injects auth headers (x-inkeep-app-id, Authorization, x-inkeep-challenge-solution)
|
|
25
|
+
* - Retries once on PoW errors (400) with a fresh captcha solution
|
|
20
26
|
* - Retries once on session errors (401) by refreshing the session token (when refreshSession is provided)
|
|
21
27
|
*
|
|
22
|
-
* Bot-protection headers are attached transparently by the BotID client SDK
|
|
23
|
-
* (initialized in RootProvider) — no header plumbing needed here.
|
|
24
|
-
*
|
|
25
28
|
* All options are kept in refs so `fetchWithAuth` is created once and never needs to be re-created.
|
|
26
29
|
*/
|
|
27
|
-
export declare function useInkeepApiClient({ appId, authToken, refreshSession, }: UseInkeepApiClientOptions): {
|
|
30
|
+
export declare function useInkeepApiClient({ appId, authToken, getCaptchaHeader, invalidateCaptcha, refreshSession, }: UseInkeepApiClientOptions): {
|
|
28
31
|
fetchWithAuth: (url: string, init?: RequestInit) => Promise<Response>;
|
|
29
32
|
};
|
|
30
33
|
export {};
|
|
@@ -1,59 +1,87 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useRef as
|
|
3
|
-
function
|
|
4
|
-
if (
|
|
5
|
-
const t =
|
|
2
|
+
import { useRef as u, useCallback as y } from "react";
|
|
3
|
+
function E(e, r) {
|
|
4
|
+
if (e === 400 && typeof r == "object" && r !== null) {
|
|
5
|
+
const t = (r.detail ?? "").toLowerCase();
|
|
6
|
+
if (t.includes("challenge") || t.includes("proof-of-work")) return "pow";
|
|
7
|
+
}
|
|
8
|
+
if (e === 401 && typeof r == "object" && r !== null) {
|
|
9
|
+
const t = r.detail ?? "";
|
|
6
10
|
if (t.includes("Invalid end-user JWT") || t.includes("Bearer token required"))
|
|
7
11
|
return "session";
|
|
8
12
|
}
|
|
9
13
|
return null;
|
|
10
14
|
}
|
|
11
|
-
function
|
|
12
|
-
if (!
|
|
13
|
-
if (
|
|
14
|
-
const
|
|
15
|
-
return
|
|
16
|
-
|
|
17
|
-
}),
|
|
15
|
+
function m(e) {
|
|
16
|
+
if (!e) return {};
|
|
17
|
+
if (e instanceof Headers) {
|
|
18
|
+
const r = {};
|
|
19
|
+
return e.forEach((t, c) => {
|
|
20
|
+
r[c] = t;
|
|
21
|
+
}), r;
|
|
18
22
|
}
|
|
19
|
-
return Array.isArray(
|
|
23
|
+
return Array.isArray(e) ? Object.fromEntries(e) : e;
|
|
20
24
|
}
|
|
21
|
-
function
|
|
25
|
+
function p(e, r, t, c) {
|
|
22
26
|
return {
|
|
23
|
-
...
|
|
24
|
-
...
|
|
25
|
-
...
|
|
27
|
+
...e ? { "x-inkeep-app-id": e } : {},
|
|
28
|
+
...r ? { Authorization: `Bearer ${r}` } : {},
|
|
29
|
+
...m(c),
|
|
30
|
+
// captcha last — must not be overridden by caller headers
|
|
31
|
+
...t
|
|
26
32
|
};
|
|
27
33
|
}
|
|
28
|
-
function
|
|
29
|
-
appId:
|
|
30
|
-
authToken:
|
|
31
|
-
|
|
34
|
+
function x({
|
|
35
|
+
appId: e,
|
|
36
|
+
authToken: r,
|
|
37
|
+
getCaptchaHeader: t,
|
|
38
|
+
invalidateCaptcha: c,
|
|
39
|
+
refreshSession: w
|
|
32
40
|
}) {
|
|
33
|
-
const
|
|
34
|
-
s.current = r;
|
|
35
|
-
const a = o(e);
|
|
41
|
+
const a = u(e);
|
|
36
42
|
a.current = e;
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
43
|
+
const i = u(r);
|
|
44
|
+
i.current = r;
|
|
45
|
+
const o = u(t);
|
|
46
|
+
o.current = t;
|
|
47
|
+
const A = u(c);
|
|
48
|
+
A.current = c;
|
|
49
|
+
const h = u(w);
|
|
50
|
+
return h.current = w, { fetchWithAuth: y(async (d, s) => {
|
|
51
|
+
const C = await o.current(), H = p(
|
|
52
|
+
a.current,
|
|
53
|
+
i.current,
|
|
54
|
+
C,
|
|
55
|
+
s?.headers
|
|
56
|
+
), n = await fetch(d, { ...s, headers: H });
|
|
57
|
+
if (n.status !== 400 && n.status !== 401) return n;
|
|
58
|
+
let R;
|
|
42
59
|
try {
|
|
43
|
-
|
|
60
|
+
R = await n.clone().json();
|
|
44
61
|
} catch {
|
|
45
62
|
return n;
|
|
46
63
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
|
|
64
|
+
const k = E(n.status, R);
|
|
65
|
+
if (k === "pow") {
|
|
66
|
+
A.current();
|
|
67
|
+
const f = await o.current(), l = p(
|
|
68
|
+
a.current,
|
|
69
|
+
i.current,
|
|
70
|
+
f,
|
|
71
|
+
s?.headers
|
|
72
|
+
);
|
|
73
|
+
return fetch(d, { ...s, headers: l });
|
|
74
|
+
}
|
|
75
|
+
if (k === "session" && h.current) {
|
|
76
|
+
const f = await h.current();
|
|
77
|
+
if (!f) return n;
|
|
78
|
+
const l = await o.current(), j = p(a.current, f, l, s?.headers);
|
|
79
|
+
return fetch(d, { ...s, headers: j });
|
|
52
80
|
}
|
|
53
81
|
return n;
|
|
54
82
|
}, []) };
|
|
55
83
|
}
|
|
56
84
|
export {
|
|
57
|
-
|
|
58
|
-
|
|
85
|
+
E as parseAuthError,
|
|
86
|
+
x as useInkeepApiClient
|
|
59
87
|
};
|
|
@@ -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 ee=require("react/jsx-runtime"),e=require("react"),te=require("../components/embedded-chat/chat-provider.cjs"),se=require("./config-provider.cjs"),ne=require("../hooks/use-inkeep-api-client.cjs"),U=e.createContext(void 0),K=25;function re(t){const{stop:u,clear:f,conversationId:l,isStreaming:y,loadAndRestoreSession:R,authToken:s,refreshSession:Z,getCaptchaHeader:_,invalidateCaptcha:z}=te.useChat(),{aiChatSettings:B,baseSettings:J}=se.useInkeepConfig(),{baseUrl:S,appId:H,apiKey:Q}=B,a=!!J.getAuthToken,o=!Q&&!!H&&(!!s||a),{fetchWithAuth:I}=ne.useInkeepApiClient({appId:H,authToken:s,getCaptchaHeader:_,invalidateCaptcha:z,refreshSession:Z}),[v,b]=e.useState(!1),[P,E]=e.useState([]),[k,x]=e.useState(!s&&o),[m,q]=e.useState(0),[w,T]=e.useState(!1),h=e.useRef(!1),j=e.useRef(null),M=e.useRef(a),$=e.useRef(s);e.useEffect(()=>{const r=M.current,n=$.current;M.current=a,$.current=s,(r!==a||n!==null&&s!==null&&n!==s)&&(E([]),q(0),T(!1),h.current=!1)},[a,s]);const L=e.useRef([]);L.current=P;const O=e.useRef(I);O.current=I;const c=e.useCallback(async(r,n=!1)=>{if(!(!S||!s||!o)){n||x(!0);try{const C=`${S}/run/v1/conversations?page=${r+1}&limit=${K}`,g=await O.current(C);if(!g.ok)throw new Error(`Failed to fetch conversations: ${g.status}`);const F=await g.json(),p=(F.data??[]).map(i=>({id:i.id,title:i.title??"",createdAt:new Date(i.createdAt),updatedAt:new Date(i.updatedAt)}));E(i=>{if(r===0&&!n)return p;if(r===0&&n){const d=new Set(p.map(A=>A.id));return[...p,...i.slice(K).filter(A=>!d.has(A.id))]}const N=new Set(i.map(d=>d.id));return[...i,...p.filter(d=>!N.has(d.id))]});const{page:W,pages:G}=F.pagination??{};T(W!=null&&G!=null?W<G:!1),n||q(r)}catch(C){console.error("[useChatHistory] Failed to load conversations:",C)}finally{x(!1)}}},[S,s,o]);e.useEffect(()=>{!v||!s&&!a||!o||(t==="stack"?c(0):!h.current&&s&&(h.current=!0,c(0)))},[v,s,a,o,t,c]);const D=e.useRef("");e.useEffect(()=>{o&&t==="sidepane"&&(!l||!y||h.current&&D.current!==l&&(L.current.some(r=>r.id===l)||(D.current=l,c(0,!0))))},[l,o,t,c,y]);const V=e.useCallback(()=>{!k&&w&&c(m+1)},[k,w,m,c]),X=e.useCallback(()=>{u(),f(),t!=="sidepane"&&b(!1)},[t,u,f]),Y=e.useCallback(async r=>{j.current?.abort();const n=new AbortController;j.current=n,await R(r,n.signal),!n.signal.aborted&&t!=="sidepane"&&b(!1)},[t,R]);return{isEnabled:o,isOpen:v,setIsOpen:b,sessions:P,isLoading:k,hasMore:w,loadMore:V,startNewConversation:X,loadSession:Y}}const oe=({layout:t,children:u})=>{const f=re(t);return ee.jsx(U.Provider,{value:f,children:typeof u=="function"?u(f):u})},ie=()=>{const t=e.useContext(U);if(!t)throw new Error("useChatHistory must be used within a ChatHistoryProvider");return t};exports.ChatHistoryProvider=oe;exports.useChatHistory=ie;
|