@inkeep/cxkit-primitives 0.5.23 → 0.5.25
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-chat.cjs +3 -3
- package/dist/components/embedded-chat.js +853 -854
- package/dist/components/embedded-search/use-inkeep-search.cjs +1 -1
- package/dist/components/embedded-search/use-inkeep-search.js +69 -66
- package/dist/index.d.cts +6 -2
- package/dist/index.d.ts +6 -2
- 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
|
};
|