@inkeep/cxkit-primitives 0.5.23 → 0.5.24
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/components/embedded-chat/use-inkeep-chat.cjs +3 -3
- package/dist/components/embedded-chat/use-inkeep-chat.js +106 -96
- package/dist/components/embedded-search/use-inkeep-search.cjs +1 -1
- package/dist/components/embedded-search/use-inkeep-search.js +69 -66
- package/dist/providers/base-events-provider.cjs +1 -1
- package/dist/providers/base-events-provider.js +1 -1
- package/package.json +4 -4
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const qe=require("openai"),t=require("react"),Ee=require("../../atoms/api/analytics/conversation.cjs"),Ne=require("../../providers/config-provider.cjs"),_e=require("../../providers/conversation-provider.cjs"),Ae=require("../../providers/chat-events-provider.cjs"),D=require("../../utils/get-message-metadata.cjs"),Re=require("../../utils/misc.cjs"),ee=require("../../utils/reset-query-params.cjs"),$e=require("../../providers/chat-form-provider.cjs"),De=require("../../providers/widget-provider.cjs"),Le=require("../../hooks/use-media-query.cjs"),Oe=require("./use-captcha.cjs"),xe=require("../modal/modal-provider.cjs"),Fe=()=>{const{chatId:m,setConversation:te,conversation:se,resetConversation:ne}=_e.useInkeepConversation(),{baseSettings:I,aiChatSettings:L}=Ne.useInkeepConfig(),{aiApiBaseUrl:O,shouldBypassCaptcha:x,filters:F}=I,K=xe.useModal(),{getSolution:oe,prefetchSolution:k,invalidateSolution:T}=Oe.useCaptcha({fetchUrl:`${O}/v1/challenge`,shouldBypassCaptcha:x,shouldMakeInitialRequest:K?K.isOpen:!0}),ae=new qe({baseURL:`${O}/v1`,apiKey:I.apiKey,dangerouslyAllowBrowser:!0}),{onInputMessageChange:re,tools:ie,prompts:ce,model:le,onToolCall:ue,filters:U}=L,[h,E]=t.useState(""),B=t.useRef(null);t.useEffect(()=>{re?.(h)},[h]);const[u,l]=t.useState([]),[Q,p]=t.useState(!1),[ge,f]=t.useState(!1),[de,W]=t.useState(null),[N,J]=t.useState(null),[v,y]=t.useState([]),me=e=>{y(s=>[...s,e])},[he,z]=t.useState({}),i=t.useRef([]),S=t.useRef(null),{logEvent:b}=Ae.useChatEvents(),H=ce?.map((e,s)=>({id:`system-${Date.now().toString()}-${s}`,role:"system",content:e})),[pe,fe]=t.useState(!!m);t.useEffect(()=>{if(m){const{apiKey:e,analyticsApiBaseUrl:s}=I;Ee.getConversation(m,e,s).then(n=>{if(n){console.log("res",n),b({eventName:"shared_chat_loaded",properties:{conversation:n}}),fe(!1),te({...n,id:""});const o=n.messages.map(c=>({id:Date.now().toString()+c.id,content:Re.parseIfJson(c.content),role:c.role,links:c.links}));l(o),i.current=o}})}},[m]);const ve=e=>{switch(e.code){case 400:return e.message;case 403:return`There seems to be a configuration error. Please contact ${I.organizationDisplayName??"Administrator"}`;default:return`Hmm..
|
|
2
2
|
|
|
3
|
-
It seems I might be having some issues right now. Please clear the chat and try again.`}},
|
|
4
|
-
${
|
|
3
|
+
It seems I might be having some issues right now. Please clear the chat and try again.`}},j=e=>{W(e),l(s=>{const n=[...s],o=n[n.length-1];return o&&(o.content=ve(e)),n}),p(!1),f(!1),T(),k()},_=!h.trim()&&!v.length||Q,ye=e=>{e.key==="Enter"&&!e.shiftKey&&!_&&!e.nativeEvent.isComposing&&(e.preventDefault(),A())},Se=e=>{const s=e.target.value;E(s)},A=async(e=h)=>{if(_&&(!e||e.trim().length===0))return;p(!0);const s=N?u.length===2:u.length===0,n=D.getMessageMetadata(N,v,s);let o;const c=D.serializeAttachments(v);v.length&&c?o=[{type:"text",text:c}]:o=e;const V={id:`${Date.now().toString()}1`,role:"user",content:o,metadata:n},P=[...u.length?[]:H,V];m&&ee.resetQueryParams(),l(C=>[...C,...P]),i.current=[...i.current,...P],y([]),E(""),b({eventName:"user_message_submitted",properties:{}},i.current);try{const C={model:le,messages:i.current,stream:!0,tools:ie,tool_choice:"auto"},X=await oe(),Y=X?btoa(JSON.stringify(X)):null,Z=F||U?JSON.stringify({...F,...U}):void 0,q={};!x&&Y&&(q["X-INKEEP-CHALLENGE-SOLUTION"]=Y),Z&&(q["inkeep-filters"]=Z);const M=ae.beta.chat.completions.stream(C,Object.keys(q).length>0?{headers:q}:void 0),a={id:`${Date.now().toString()}2`,role:"assistant",content:"",links:[]};l(g=>[...g,a]),M.on("content",(g,R)=>{f(!0),a.content=R,l($=>{const r=[...$],d=r.findIndex(w=>w.id===a.id);return d===-1?[...r,a]:(r[d]={...a},r)})}),M.on("tool_calls.function.arguments.done",g=>{const{name:R,arguments:$}=g;if(R==="provideLinks"){const r=JSON.parse($);r.links?.length>0&&(a.links=r.links,l(d=>{const w=[...d],be=w.findIndex(Pe=>Pe.id===a.id);return w[be]={...a},w}))}else{const r=ue?.(g);r&&z(d=>({...d,[a.id]:[...d[a.id]||[],r]}))}}),M.on("error",g=>{j(g)}),M.finalChatCompletion().then(()=>{i.current=[...i.current,a],S.current=null,p(!1),f(!1),k(),b({eventName:"assistant_message_received",properties:{}},i.current)}),S.current=M.controller}catch(C){j(C),p(!1),f(!1)}},Ce=()=>{S.current&&(S.current.abort(),S.current=null),p(!1),f(!1),k()},G=()=>{W(null),l([]),i.current=[],ne(),z({}),y([]),ee.resetQueryParams(),J(null),T(),k(),b({eventName:"chat_clear_button_clicked",properties:{conversation:se}})},Me=e=>{J(e);const{initialReplyMessage:s,displayName:n}=e,o={id:u.length.toString(),role:"user",content:n},c={id:(u.length+1).toString(),role:"assistant",content:s,links:[]},P=[{id:`system-${Date.now().toString()}-${e.id}`,role:"system",content:`Company has asked user to follow this guided workflow:
|
|
4
|
+
${D.serializeWorkflow(e)}`},...H,o,c];l(P),y([])},{openForm:we}=$e.useChatForm(),Ie=De.useWidget();t.useImperativeHandle(L.chatFunctionsRef,()=>({submitMessage:A,updateInputMessage(e){E(e)},clearChat:G,openForm:e=>{Ie?.setView("chat"),we(e)},focusInput:()=>{B.current?.focus()}}));const ke=Le.useMediaQuery("(max-width: 768px)");return{messages:u,input:h,isLoading:Q,isStreaming:ge,isLoadingConversation:pe,error:de,isSubmitDisabled:_,handleInputChange:Se,handleInputKeyDown:ye,handleSubmit:A,stop:Ce,clear:G,handleWorkflow:Me,selectedWorkflow:N,messageAttachments:v,setMessageAttachments:y,addAttachment:me,messageToolCalls:he,isEmpty:u.length===0,inputRef:B,isMobile:ke}};exports.useInkeepChat=Fe;
|
|
@@ -1,52 +1,59 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import
|
|
3
|
-
import { useState as c, useRef as
|
|
4
|
-
import { getConversation as
|
|
5
|
-
import { useInkeepConfig as
|
|
6
|
-
import { useInkeepConversation as
|
|
7
|
-
import { useChatEvents as
|
|
8
|
-
import { getMessageMetadata as
|
|
9
|
-
import { parseIfJson as
|
|
10
|
-
import { resetQueryParams as
|
|
11
|
-
import { useChatForm as
|
|
12
|
-
import { useWidget as
|
|
13
|
-
import { useMediaQuery as
|
|
14
|
-
import { useCaptcha as
|
|
15
|
-
import { useModal as
|
|
16
|
-
const
|
|
17
|
-
const { chatId: d, setConversation:
|
|
2
|
+
import _e from "openai";
|
|
3
|
+
import { useState as c, useRef as O, useEffect as ee, useImperativeHandle as Ae } from "react";
|
|
4
|
+
import { getConversation as $e } from "../../atoms/api/analytics/conversation.js";
|
|
5
|
+
import { useInkeepConfig as xe } from "../../providers/config-provider.js";
|
|
6
|
+
import { useInkeepConversation as De } from "../../providers/conversation-provider.js";
|
|
7
|
+
import { useChatEvents as Le } from "../../providers/chat-events-provider.js";
|
|
8
|
+
import { getMessageMetadata as Oe, serializeAttachments as Re, serializeWorkflow as Pe } from "../../utils/get-message-metadata.js";
|
|
9
|
+
import { parseIfJson as Fe } from "../../utils/misc.js";
|
|
10
|
+
import { resetQueryParams as te } from "../../utils/reset-query-params.js";
|
|
11
|
+
import { useChatForm as Ke } from "../../providers/chat-form-provider.js";
|
|
12
|
+
import { useWidget as Ue } from "../../providers/widget-provider.js";
|
|
13
|
+
import { useMediaQuery as Be } from "../../hooks/use-media-query.js";
|
|
14
|
+
import { useCaptcha as Te } from "./use-captcha.js";
|
|
15
|
+
import { useModal as We } from "../modal/modal-provider.js";
|
|
16
|
+
const nt = () => {
|
|
17
|
+
const { chatId: d, setConversation: se, conversation: ne, resetConversation: oe } = De(), { baseSettings: S, aiChatSettings: R } = xe(), { aiApiBaseUrl: P, shouldBypassCaptcha: F, filters: K } = S, U = We(), { getSolution: ae, prefetchSolution: k, invalidateSolution: B } = Te({
|
|
18
18
|
fetchUrl: `${P}/v1/challenge`,
|
|
19
|
-
shouldBypassCaptcha:
|
|
20
|
-
shouldMakeInitialRequest:
|
|
21
|
-
}),
|
|
19
|
+
shouldBypassCaptcha: F,
|
|
20
|
+
shouldMakeInitialRequest: U ? U.isOpen : !0
|
|
21
|
+
}), re = new _e({
|
|
22
22
|
baseURL: `${P}/v1`,
|
|
23
23
|
apiKey: S.apiKey,
|
|
24
24
|
dangerouslyAllowBrowser: !0
|
|
25
|
-
}), {
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
}), {
|
|
26
|
+
onInputMessageChange: ie,
|
|
27
|
+
tools: ce,
|
|
28
|
+
prompts: le,
|
|
29
|
+
model: ue,
|
|
30
|
+
onToolCall: me,
|
|
31
|
+
filters: T
|
|
32
|
+
} = R, [p, _] = c(""), W = O(null);
|
|
33
|
+
ee(() => {
|
|
34
|
+
ie?.(p);
|
|
28
35
|
}, [p]);
|
|
29
|
-
const [u, l] = c([]), [
|
|
36
|
+
const [u, l] = c([]), [J, f] = c(!1), [ge, h] = c(!1), [de, z] = c(null), [A, H] = c(null), [y, C] = c([]), pe = (e) => {
|
|
30
37
|
C((t) => [...t, e]);
|
|
31
|
-
}, [
|
|
38
|
+
}, [fe, Q] = c({}), r = O([]), w = O(null), { logEvent: b } = Le(), j = le?.map((e, t) => ({
|
|
32
39
|
id: `system-${Date.now().toString()}-${t}`,
|
|
33
40
|
role: "system",
|
|
34
41
|
content: e
|
|
35
|
-
})), [
|
|
36
|
-
|
|
42
|
+
})), [he, ye] = c(!!d);
|
|
43
|
+
ee(() => {
|
|
37
44
|
if (d) {
|
|
38
45
|
const { apiKey: e, analyticsApiBaseUrl: t } = S;
|
|
39
|
-
|
|
46
|
+
$e(d, e, t).then((s) => {
|
|
40
47
|
if (s) {
|
|
41
48
|
console.log("res", s), b({
|
|
42
49
|
eventName: "shared_chat_loaded",
|
|
43
50
|
properties: {
|
|
44
51
|
conversation: s
|
|
45
52
|
}
|
|
46
|
-
}),
|
|
53
|
+
}), ye(!1), se({ ...s, id: "" });
|
|
47
54
|
const n = s.messages.map((i) => ({
|
|
48
55
|
id: Date.now().toString() + i.id,
|
|
49
|
-
content:
|
|
56
|
+
content: Fe(i.content),
|
|
50
57
|
role: i.role,
|
|
51
58
|
links: i.links
|
|
52
59
|
}));
|
|
@@ -55,7 +62,7 @@ const et = () => {
|
|
|
55
62
|
});
|
|
56
63
|
}
|
|
57
64
|
}, [d]);
|
|
58
|
-
const
|
|
65
|
+
const Ce = (e) => {
|
|
59
66
|
switch (e.code) {
|
|
60
67
|
case 400:
|
|
61
68
|
return e.message;
|
|
@@ -66,30 +73,30 @@ const et = () => {
|
|
|
66
73
|
|
|
67
74
|
It seems I might be having some issues right now. Please clear the chat and try again.`;
|
|
68
75
|
}
|
|
69
|
-
},
|
|
70
|
-
|
|
76
|
+
}, q = (e) => {
|
|
77
|
+
z(e), l((t) => {
|
|
71
78
|
const s = [...t], n = s[s.length - 1];
|
|
72
|
-
return n && (n.content =
|
|
73
|
-
}), f(!1), h(!1),
|
|
74
|
-
},
|
|
75
|
-
e.key === "Enter" && !e.shiftKey &&
|
|
76
|
-
},
|
|
79
|
+
return n && (n.content = Ce(e)), s;
|
|
80
|
+
}), f(!1), h(!1), B(), k();
|
|
81
|
+
}, $ = !p.trim() && !y.length || J, we = (e) => {
|
|
82
|
+
e.key === "Enter" && !e.shiftKey && !$ && !e.nativeEvent.isComposing && (e.preventDefault(), x());
|
|
83
|
+
}, Ie = (e) => {
|
|
77
84
|
const t = e.target.value;
|
|
78
|
-
|
|
79
|
-
},
|
|
80
|
-
if (
|
|
85
|
+
_(t);
|
|
86
|
+
}, x = async (e = p) => {
|
|
87
|
+
if ($ && (!e || e.trim().length === 0)) return;
|
|
81
88
|
f(!0);
|
|
82
|
-
const t =
|
|
89
|
+
const t = A ? u.length === 2 : u.length === 0, s = Oe(A, y, t);
|
|
83
90
|
let n;
|
|
84
|
-
const i =
|
|
91
|
+
const i = Re(y);
|
|
85
92
|
y.length && i ? n = [{ type: "text", text: i }] : n = e;
|
|
86
|
-
const
|
|
93
|
+
const V = {
|
|
87
94
|
id: `${Date.now().toString()}1`,
|
|
88
95
|
role: "user",
|
|
89
96
|
content: n,
|
|
90
97
|
metadata: s
|
|
91
|
-
}, E = [...u.length ? [] :
|
|
92
|
-
d &&
|
|
98
|
+
}, E = [...u.length ? [] : j, V];
|
|
99
|
+
d && te(), l((I) => [...I, ...E]), r.current = [...r.current, ...E], C([]), _(""), b(
|
|
93
100
|
{
|
|
94
101
|
eventName: "user_message_submitted",
|
|
95
102
|
properties: {}
|
|
@@ -98,44 +105,47 @@ It seems I might be having some issues right now. Please clear the chat and try
|
|
|
98
105
|
);
|
|
99
106
|
try {
|
|
100
107
|
const I = {
|
|
101
|
-
model:
|
|
108
|
+
model: ue,
|
|
102
109
|
messages: r.current,
|
|
103
110
|
stream: !0,
|
|
104
|
-
tools:
|
|
111
|
+
tools: ce,
|
|
105
112
|
tool_choice: "auto"
|
|
106
|
-
},
|
|
107
|
-
|
|
108
|
-
|
|
113
|
+
}, X = await ae(), Y = X ? btoa(JSON.stringify(X)) : null, Z = K || T ? JSON.stringify({
|
|
114
|
+
...K,
|
|
115
|
+
...T
|
|
116
|
+
}) : void 0, N = {};
|
|
117
|
+
!F && Y && (N["X-INKEEP-CHALLENGE-SOLUTION"] = Y), Z && (N["inkeep-filters"] = Z);
|
|
118
|
+
const v = re.beta.chat.completions.stream(
|
|
109
119
|
I,
|
|
110
|
-
|
|
120
|
+
Object.keys(N).length > 0 ? { headers: N } : void 0
|
|
111
121
|
), o = {
|
|
112
122
|
id: `${Date.now().toString()}2`,
|
|
113
123
|
role: "assistant",
|
|
114
124
|
content: "",
|
|
115
125
|
links: []
|
|
116
126
|
};
|
|
117
|
-
l((m) => [...m, o]), v.on("content", (m,
|
|
118
|
-
h(!0), o.content =
|
|
119
|
-
const a = [...
|
|
127
|
+
l((m) => [...m, o]), v.on("content", (m, D) => {
|
|
128
|
+
h(!0), o.content = D, l((L) => {
|
|
129
|
+
const a = [...L], g = a.findIndex((M) => M.id === o.id);
|
|
120
130
|
return g === -1 ? [...a, o] : (a[g] = { ...o }, a);
|
|
121
131
|
});
|
|
122
132
|
}), v.on("tool_calls.function.arguments.done", (m) => {
|
|
123
|
-
const { name:
|
|
124
|
-
if (
|
|
125
|
-
const a = JSON.parse(
|
|
133
|
+
const { name: D, arguments: L } = m;
|
|
134
|
+
if (D === "provideLinks") {
|
|
135
|
+
const a = JSON.parse(L);
|
|
126
136
|
a.links?.length > 0 && (o.links = a.links, l((g) => {
|
|
127
|
-
const M = [...g],
|
|
128
|
-
return M[
|
|
137
|
+
const M = [...g], Ee = M.findIndex((Ne) => Ne.id === o.id);
|
|
138
|
+
return M[Ee] = { ...o }, M;
|
|
129
139
|
}));
|
|
130
140
|
} else {
|
|
131
|
-
const a =
|
|
132
|
-
a &&
|
|
141
|
+
const a = me?.(m);
|
|
142
|
+
a && Q((g) => ({
|
|
133
143
|
...g,
|
|
134
144
|
[o.id]: [...g[o.id] || [], a]
|
|
135
145
|
}));
|
|
136
146
|
}
|
|
137
147
|
}), v.on("error", (m) => {
|
|
138
|
-
|
|
148
|
+
q(m);
|
|
139
149
|
}), v.finalChatCompletion().then(() => {
|
|
140
150
|
r.current = [...r.current, o], w.current = null, f(!1), h(!1), k(), b(
|
|
141
151
|
{
|
|
@@ -146,19 +156,19 @@ It seems I might be having some issues right now. Please clear the chat and try
|
|
|
146
156
|
);
|
|
147
157
|
}), w.current = v.controller;
|
|
148
158
|
} catch (I) {
|
|
149
|
-
|
|
159
|
+
q(I), f(!1), h(!1);
|
|
150
160
|
}
|
|
151
|
-
},
|
|
161
|
+
}, ve = () => {
|
|
152
162
|
w.current && (w.current.abort(), w.current = null), f(!1), h(!1), k();
|
|
153
|
-
},
|
|
154
|
-
|
|
163
|
+
}, G = () => {
|
|
164
|
+
z(null), l([]), r.current = [], oe(), Q({}), C([]), te(), H(null), B(), k(), b({
|
|
155
165
|
eventName: "chat_clear_button_clicked",
|
|
156
166
|
properties: {
|
|
157
|
-
conversation:
|
|
167
|
+
conversation: ne
|
|
158
168
|
}
|
|
159
169
|
});
|
|
160
|
-
},
|
|
161
|
-
|
|
170
|
+
}, Me = (e) => {
|
|
171
|
+
H(e);
|
|
162
172
|
const { initialReplyMessage: t, displayName: s } = e, n = {
|
|
163
173
|
id: u.length.toString(),
|
|
164
174
|
role: "user",
|
|
@@ -172,50 +182,50 @@ It seems I might be having some issues right now. Please clear the chat and try
|
|
|
172
182
|
id: `system-${Date.now().toString()}-${e.id}`,
|
|
173
183
|
role: "system",
|
|
174
184
|
content: `Company has asked user to follow this guided workflow:
|
|
175
|
-
${
|
|
185
|
+
${Pe(
|
|
176
186
|
e
|
|
177
187
|
)}`
|
|
178
|
-
}, ...
|
|
188
|
+
}, ...j, n, i];
|
|
179
189
|
l(E), C([]);
|
|
180
|
-
}, { openForm:
|
|
181
|
-
|
|
182
|
-
submitMessage:
|
|
190
|
+
}, { openForm: Se } = Ke(), ke = Ue();
|
|
191
|
+
Ae(R.chatFunctionsRef, () => ({
|
|
192
|
+
submitMessage: x,
|
|
183
193
|
updateInputMessage(e) {
|
|
184
|
-
|
|
194
|
+
_(e);
|
|
185
195
|
},
|
|
186
|
-
clearChat:
|
|
196
|
+
clearChat: G,
|
|
187
197
|
openForm: (e) => {
|
|
188
|
-
|
|
198
|
+
ke?.setView("chat"), Se(e);
|
|
189
199
|
},
|
|
190
200
|
focusInput: () => {
|
|
191
|
-
|
|
201
|
+
W.current?.focus();
|
|
192
202
|
}
|
|
193
203
|
}));
|
|
194
|
-
const
|
|
204
|
+
const be = Be("(max-width: 768px)");
|
|
195
205
|
return {
|
|
196
206
|
messages: u,
|
|
197
207
|
input: p,
|
|
198
|
-
isLoading:
|
|
199
|
-
isStreaming:
|
|
200
|
-
isLoadingConversation:
|
|
201
|
-
error:
|
|
202
|
-
isSubmitDisabled:
|
|
203
|
-
handleInputChange:
|
|
204
|
-
handleInputKeyDown:
|
|
205
|
-
handleSubmit:
|
|
206
|
-
stop:
|
|
207
|
-
clear:
|
|
208
|
-
handleWorkflow:
|
|
209
|
-
selectedWorkflow:
|
|
208
|
+
isLoading: J,
|
|
209
|
+
isStreaming: ge,
|
|
210
|
+
isLoadingConversation: he,
|
|
211
|
+
error: de,
|
|
212
|
+
isSubmitDisabled: $,
|
|
213
|
+
handleInputChange: Ie,
|
|
214
|
+
handleInputKeyDown: we,
|
|
215
|
+
handleSubmit: x,
|
|
216
|
+
stop: ve,
|
|
217
|
+
clear: G,
|
|
218
|
+
handleWorkflow: Me,
|
|
219
|
+
selectedWorkflow: A,
|
|
210
220
|
messageAttachments: y,
|
|
211
221
|
setMessageAttachments: C,
|
|
212
|
-
addAttachment:
|
|
213
|
-
messageToolCalls:
|
|
222
|
+
addAttachment: pe,
|
|
223
|
+
messageToolCalls: fe,
|
|
214
224
|
isEmpty: u.length === 0,
|
|
215
|
-
inputRef:
|
|
216
|
-
isMobile:
|
|
225
|
+
inputRef: W,
|
|
226
|
+
isMobile: be
|
|
217
227
|
};
|
|
218
228
|
};
|
|
219
229
|
export {
|
|
220
|
-
|
|
230
|
+
nt as useInkeepChat
|
|
221
231
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),
|
|
1
|
+
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),X=require("../../providers/config-provider.cjs"),Y=require("../../hooks/use-media-query.cjs"),Z=require("./search-query.graphql.cjs"),ee=require("./transform-results.cjs"),te=require("../../providers/search-events-provider.cjs"),se=require("../../hooks/use-instant-update.cjs"),re=require("../../utils/graphql-client.cjs"),ne=()=>{const{baseSettings:N,searchSettings:O}=X.useInkeepConfig(),{apiKey:S,aiApiBaseUrl:x,userAuthToken:l,filters:R}=N,{maxResults:Q,placeholder:q,defaultQuery:B,debounceTimeMs:C,onQueryChange:H,searchFunctionsRef:$,tabs:i,filters:T}=O,{logEvent:k}=te.useSearchEvents(),[c,v]=t.useState(B),E=t.useRef(null),[M,w]=t.useState(!1),[z,h]=t.useState(null),[D,a]=t.useState("NONE"),K=t.useCallback(e=>{a(e)},[]),[f,d]=t.useState([]),s=ee.useTransformResults(f,c),I=typeof i?.[0]=="string"?i[0]:i[0][0],[y,A]=t.useState(I),{disableTransition:P,disabled:W}=se.useInstantUpdate(),G=t.useCallback(e=>{P(),A(e);const r=s[e][0].id;a(r)},[s,P]);t.useEffect(()=>{let e=y in s?y:Object.keys(s)[0];if(!s[e]?.length){const n=Object.keys(s).find(u=>s[u]?.length);n?e=n:e=I}if(!e)return;A(e);const r=s[e]?.[0];r&&a(r.id)},[s]);const b=Y.useMediaQuery("(max-width: 768px)"),o=t.useRef({}),m=t.useRef(null),g=t.useRef(null);t.useImperativeHandle($,()=>({updateQuery(e){v(e)},focusInput:()=>{E.current?.focus()}}));const U=`${x}/graphql`,_=t.useMemo(()=>({Authorization:`Bearer ${S}`,"Content-Type":"application/json",...l?{"User-Token":l}:{}}),[S,l]),j=t.useCallback(async e=>{m.current&&m.current.abort();const r=new AbortController;m.current=r;try{w(!0),h(null);const u=(await re.graphqlRequest(U,Z.default,{searchInput:e},_))?.search.searchHits;o.current[e.searchQuery]=u,d(u)}catch(n){if(n.name==="AbortError")return;h("Failed to fetch search results. Please try again.")}w(!1)},[U,_]),F=t.useCallback(e=>{const r=o.current[e.searchQuery];k({eventName:"search_query_response_received",properties:{searchQuery:e.searchQuery,totalResults:r?.length}})},[]),L=t.useCallback(e=>{if(g.current&&window.clearTimeout(g.current),!e.searchQuery){d([]),h(null);return}g.current=window.setTimeout(()=>{k({eventName:"search_query_submitted",properties:{searchQuery:e.searchQuery}}),o.current[e.searchQuery]?(d(o.current[e.searchQuery]),F(e)):j(e).then(()=>{F(e)})},C)},[C,j]),p=t.useMemo(()=>({searchQuery:c,filters:{...R,...T,limit:Q}}),[c,Q,R,T]);t.useEffect(()=>{H?.(p.searchQuery),L(p)},[L,p]);const J=t.useCallback(e=>v(e.target.value),[]),V=t.useMemo(()=>q||(b?"Search...":"Search for anything..."),[q,b]);return{results:s,resultsList:f,hasContent:f.length>0||M,loading:M,error:z,query:c,onInput:J,placeholder:V,tab:y,onTabChange:G,selectedItem:D,setSelectedItem:a,onSelectedItemChange:K,transitionsDisabled:W,inputRef:E,isMobile:b}};exports.useInkeepSearch=ne;
|
|
@@ -1,140 +1,143 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useState as s, useRef as l, useCallback as n, useEffect as
|
|
3
|
-
import { useInkeepConfig as
|
|
4
|
-
import { useMediaQuery as
|
|
5
|
-
import
|
|
6
|
-
import { useTransformResults as
|
|
7
|
-
import { useSearchEvents as
|
|
8
|
-
import { useInstantUpdate as
|
|
9
|
-
import { graphqlRequest as
|
|
10
|
-
const
|
|
11
|
-
const { baseSettings:
|
|
12
|
-
maxResults:
|
|
13
|
-
placeholder:
|
|
14
|
-
defaultQuery:
|
|
15
|
-
debounceTimeMs:
|
|
16
|
-
onQueryChange:
|
|
17
|
-
searchFunctionsRef:
|
|
18
|
-
tabs: f
|
|
19
|
-
|
|
2
|
+
import { useState as s, useRef as l, useCallback as n, useEffect as H, useImperativeHandle as te, useMemo as T } from "react";
|
|
3
|
+
import { useInkeepConfig as re } from "../../providers/config-provider.js";
|
|
4
|
+
import { useMediaQuery as se } from "../../hooks/use-media-query.js";
|
|
5
|
+
import ne from "./search-query.graphql.js";
|
|
6
|
+
import { useTransformResults as oe } from "./transform-results.js";
|
|
7
|
+
import { useSearchEvents as ce } from "../../providers/search-events-provider.js";
|
|
8
|
+
import { useInstantUpdate as ae } from "../../hooks/use-instant-update.js";
|
|
9
|
+
import { graphqlRequest as ie } from "../../utils/graphql-client.js";
|
|
10
|
+
const be = () => {
|
|
11
|
+
const { baseSettings: P, searchSettings: $ } = re(), { apiKey: S, aiApiBaseUrl: z, userAuthToken: h, filters: C } = P, {
|
|
12
|
+
maxResults: v,
|
|
13
|
+
placeholder: k,
|
|
14
|
+
defaultQuery: D,
|
|
15
|
+
debounceTimeMs: w,
|
|
16
|
+
onQueryChange: K,
|
|
17
|
+
searchFunctionsRef: W,
|
|
18
|
+
tabs: f,
|
|
19
|
+
filters: q
|
|
20
|
+
} = $, { logEvent: E } = ce(), [c, A] = s(D), I = l(null), [_, x] = s(!1), [G, m] = s(null), [J, a] = s("NONE"), V = n((e) => {
|
|
20
21
|
a(e);
|
|
21
|
-
}, []), [d, p] = s([]), t =
|
|
22
|
+
}, []), [d, p] = s([]), t = oe(d, c), F = typeof f?.[0] == "string" ? f[0] : f[0][0], [y, L] = s(F), { disableTransition: M, disabled: X } = ae(), Y = n(
|
|
22
23
|
(e) => {
|
|
23
|
-
|
|
24
|
+
M(), L(e);
|
|
24
25
|
const r = t[e][0].id;
|
|
25
26
|
a(r);
|
|
26
27
|
},
|
|
27
|
-
[t,
|
|
28
|
+
[t, M]
|
|
28
29
|
);
|
|
29
|
-
|
|
30
|
+
H(() => {
|
|
30
31
|
let e = y in t ? y : Object.keys(t)[0];
|
|
31
32
|
if (!t[e]?.length) {
|
|
32
|
-
const o = Object.keys(t).find((
|
|
33
|
-
o ? e = o : e =
|
|
33
|
+
const o = Object.keys(t).find((u) => t[u]?.length);
|
|
34
|
+
o ? e = o : e = F;
|
|
34
35
|
}
|
|
35
36
|
if (!e) return;
|
|
36
|
-
|
|
37
|
+
L(e);
|
|
37
38
|
const r = t[e]?.[0];
|
|
38
39
|
r && a(r.id);
|
|
39
40
|
}, [t]);
|
|
40
|
-
const b =
|
|
41
|
-
|
|
41
|
+
const b = se("(max-width: 768px)"), i = l({}), g = l(null), Q = l(null);
|
|
42
|
+
te(W, () => ({
|
|
42
43
|
updateQuery(e) {
|
|
43
|
-
|
|
44
|
+
A(e);
|
|
44
45
|
},
|
|
45
46
|
focusInput: () => {
|
|
46
|
-
|
|
47
|
+
I.current?.focus();
|
|
47
48
|
}
|
|
48
49
|
}));
|
|
49
|
-
const
|
|
50
|
+
const N = `${z}/graphql`, U = T(
|
|
50
51
|
() => ({
|
|
51
52
|
Authorization: `Bearer ${S}`,
|
|
52
53
|
"Content-Type": "application/json",
|
|
53
54
|
...h ? { "User-Token": h } : {}
|
|
54
55
|
}),
|
|
55
56
|
[S, h]
|
|
56
|
-
),
|
|
57
|
+
), j = n(
|
|
57
58
|
async (e) => {
|
|
58
59
|
g.current && g.current.abort();
|
|
59
60
|
const r = new AbortController();
|
|
60
61
|
g.current = r;
|
|
61
62
|
try {
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
|
|
63
|
+
x(!0), m(null);
|
|
64
|
+
const u = (await ie(N, ne, { searchInput: e }, U))?.search.searchHits;
|
|
65
|
+
i.current[e.searchQuery] = u, p(u);
|
|
65
66
|
} catch (o) {
|
|
66
67
|
if (o.name === "AbortError")
|
|
67
68
|
return;
|
|
68
69
|
m("Failed to fetch search results. Please try again.");
|
|
69
70
|
}
|
|
70
|
-
|
|
71
|
+
x(!1);
|
|
71
72
|
},
|
|
72
|
-
[
|
|
73
|
-
),
|
|
74
|
-
const r =
|
|
75
|
-
|
|
73
|
+
[N, U]
|
|
74
|
+
), O = n((e) => {
|
|
75
|
+
const r = i.current[e.searchQuery];
|
|
76
|
+
E({
|
|
76
77
|
eventName: "search_query_response_received",
|
|
77
78
|
properties: {
|
|
78
79
|
searchQuery: e.searchQuery,
|
|
79
80
|
totalResults: r?.length
|
|
80
81
|
}
|
|
81
82
|
});
|
|
82
|
-
}, []),
|
|
83
|
+
}, []), B = n(
|
|
83
84
|
(e) => {
|
|
84
85
|
if (Q.current && window.clearTimeout(Q.current), !e.searchQuery) {
|
|
85
86
|
p([]), m(null);
|
|
86
87
|
return;
|
|
87
88
|
}
|
|
88
89
|
Q.current = window.setTimeout(() => {
|
|
89
|
-
|
|
90
|
+
E({
|
|
90
91
|
eventName: "search_query_submitted",
|
|
91
92
|
properties: {
|
|
92
93
|
searchQuery: e.searchQuery
|
|
93
94
|
}
|
|
94
|
-
}),
|
|
95
|
-
|
|
95
|
+
}), i.current[e.searchQuery] ? (p(i.current[e.searchQuery]), O(e)) : j(e).then(() => {
|
|
96
|
+
O(e);
|
|
96
97
|
});
|
|
97
|
-
},
|
|
98
|
+
}, w);
|
|
98
99
|
},
|
|
99
|
-
[
|
|
100
|
+
[w, j]
|
|
100
101
|
), R = T(
|
|
101
102
|
() => ({
|
|
102
103
|
searchQuery: c,
|
|
103
104
|
filters: {
|
|
104
|
-
|
|
105
|
+
...C,
|
|
106
|
+
...q,
|
|
107
|
+
limit: v
|
|
105
108
|
}
|
|
106
109
|
}),
|
|
107
|
-
[c, C]
|
|
110
|
+
[c, v, C, q]
|
|
108
111
|
);
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}, [
|
|
112
|
-
const
|
|
113
|
-
(e) =>
|
|
112
|
+
H(() => {
|
|
113
|
+
K?.(R.searchQuery), B(R);
|
|
114
|
+
}, [B, R]);
|
|
115
|
+
const Z = n(
|
|
116
|
+
(e) => A(e.target.value),
|
|
114
117
|
[]
|
|
115
|
-
),
|
|
116
|
-
() =>
|
|
117
|
-
[
|
|
118
|
+
), ee = T(
|
|
119
|
+
() => k || (b ? "Search..." : "Search for anything..."),
|
|
120
|
+
[k, b]
|
|
118
121
|
);
|
|
119
122
|
return {
|
|
120
123
|
results: t,
|
|
121
124
|
resultsList: d,
|
|
122
|
-
hasContent: d.length > 0 ||
|
|
123
|
-
loading:
|
|
124
|
-
error:
|
|
125
|
+
hasContent: d.length > 0 || _,
|
|
126
|
+
loading: _,
|
|
127
|
+
error: G,
|
|
125
128
|
query: c,
|
|
126
|
-
onInput:
|
|
127
|
-
placeholder:
|
|
129
|
+
onInput: Z,
|
|
130
|
+
placeholder: ee,
|
|
128
131
|
tab: y,
|
|
129
|
-
onTabChange:
|
|
130
|
-
selectedItem:
|
|
132
|
+
onTabChange: Y,
|
|
133
|
+
selectedItem: J,
|
|
131
134
|
setSelectedItem: a,
|
|
132
|
-
onSelectedItemChange:
|
|
133
|
-
transitionsDisabled:
|
|
134
|
-
inputRef:
|
|
135
|
+
onSelectedItemChange: V,
|
|
136
|
+
transitionsDisabled: X,
|
|
137
|
+
inputRef: I,
|
|
135
138
|
isMobile: b
|
|
136
139
|
};
|
|
137
140
|
};
|
|
138
141
|
export {
|
|
139
|
-
|
|
142
|
+
be as useInkeepSearch
|
|
140
143
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const P=require("react/jsx-runtime"),t=require("react"),y=require("../atoms/api/analytics/events.cjs"),B=require("./config-provider.cjs"),b=require("./user-provider.cjs"),p=t.createContext(void 0),f=({children:e})=>{const{baseSettings:s,componentType:n}=B.useInkeepConfig(),{apiKey:o,analyticsApiBaseUrl:r,tags:i,privacyPreferences:d,env:E}=s,{userProperties:c}=b.useUser(),a=t.useMemo(()=>({widgetLibraryVersion:"0.5.
|
|
1
|
+
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const P=require("react/jsx-runtime"),t=require("react"),y=require("../atoms/api/analytics/events.cjs"),B=require("./config-provider.cjs"),b=require("./user-provider.cjs"),p=t.createContext(void 0),f=({children:e})=>{const{baseSettings:s,componentType:n}=B.useInkeepConfig(),{apiKey:o,analyticsApiBaseUrl:r,tags:i,privacyPreferences:d,env:E}=s,{userProperties:c}=b.useUser(),a=t.useMemo(()=>({widgetLibraryVersion:"0.5.24",componentType:n,tags:i}),[n,i]),u=!d.optOutAllAnalytics&&E!=="development",g={logEvent:t.useCallback(async v=>{const m={...a,...v.properties},l={eventName:v.eventName,properties:m,userProperties:c};u&&y.logEvent(l,o,r),s.onEvent?.(l)},[u,s,o,r,c,a])};return P.jsx(p.Provider,{value:g,children:e})},x=()=>{const e=t.useContext(p);if(!e)throw new Error("useBaseEvents must be used within a BaseEventsProvider");return e};exports.BaseEventsProvider=f;exports.useBaseEvents=x;
|
|
@@ -7,7 +7,7 @@ import { useUser as b } from "./user-provider.js";
|
|
|
7
7
|
const m = x(void 0), U = ({ children: e }) => {
|
|
8
8
|
const { baseSettings: t, componentType: o } = C(), { apiKey: n, analyticsApiBaseUrl: s, tags: r, privacyPreferences: u, env: l } = t, { userProperties: i } = b(), a = d(
|
|
9
9
|
() => ({
|
|
10
|
-
widgetLibraryVersion: "0.5.
|
|
10
|
+
widgetLibraryVersion: "0.5.24",
|
|
11
11
|
componentType: o,
|
|
12
12
|
tags: r
|
|
13
13
|
}),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inkeep/cxkit-primitives",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.24",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "Inkeep, Inc. Customer License (IICL) v1.1",
|
|
6
6
|
"homepage": "",
|
|
@@ -58,9 +58,9 @@
|
|
|
58
58
|
"rehype-raw": "7.0.0",
|
|
59
59
|
"unist-util-visit": "^5.0.0",
|
|
60
60
|
"use-sync-external-store": "^1.4.0",
|
|
61
|
-
"@inkeep/cxkit-color-mode": "0.5.
|
|
62
|
-
"@inkeep/cxkit-theme": "0.5.
|
|
63
|
-
"@inkeep/cxkit-types": "0.5.
|
|
61
|
+
"@inkeep/cxkit-color-mode": "0.5.24",
|
|
62
|
+
"@inkeep/cxkit-theme": "0.5.24",
|
|
63
|
+
"@inkeep/cxkit-types": "0.5.24"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
66
|
"@biomejs/biome": "1.9.4",
|