@inkeep/cxkit-primitives 0.5.5 → 0.5.8

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.
Files changed (54) hide show
  1. package/dist/atoms/api/analytics/events.cjs +1 -1
  2. package/dist/atoms/api/analytics/events.js +2 -2
  3. package/dist/components/embedded-chat/use-chat-action.cjs +1 -1
  4. package/dist/components/embedded-chat/use-chat-action.js +14 -16
  5. package/dist/components/embedded-chat/use-inkeep-chat.cjs +3 -3
  6. package/dist/components/embedded-chat/use-inkeep-chat.js +122 -128
  7. package/dist/components/embedded-chat.cjs +5 -5
  8. package/dist/components/embedded-chat.d.cts +9 -8
  9. package/dist/components/embedded-chat.d.ts +9 -8
  10. package/dist/components/embedded-chat.js +952 -963
  11. package/dist/components/embedded-search/use-inkeep-search.cjs +1 -1
  12. package/dist/components/embedded-search/use-inkeep-search.js +10 -10
  13. package/dist/components/embedded-search.d.cts +2 -2
  14. package/dist/components/embedded-search.d.ts +2 -2
  15. package/dist/components/embedded-search.js +79 -79
  16. package/dist/components/intelligent-form/intelligent-form-provider.cjs +1 -1
  17. package/dist/components/intelligent-form/intelligent-form-provider.d.cts +3 -3
  18. package/dist/components/intelligent-form/intelligent-form-provider.d.ts +3 -3
  19. package/dist/components/intelligent-form/intelligent-form-provider.js +2 -2
  20. package/dist/components/intelligent-form/use-inkeep-intelligent-form.cjs +1 -1
  21. package/dist/components/intelligent-form/use-inkeep-intelligent-form.d.cts +5 -5
  22. package/dist/components/intelligent-form/use-inkeep-intelligent-form.d.ts +5 -5
  23. package/dist/components/intelligent-form/use-inkeep-intelligent-form.js +88 -109
  24. package/dist/components/intelligent-form.cjs +1 -1
  25. package/dist/components/intelligent-form.d.cts +4 -3
  26. package/dist/components/intelligent-form.d.ts +4 -3
  27. package/dist/components/intelligent-form.js +191 -188
  28. package/dist/components/modal.cjs +1 -1
  29. package/dist/components/modal.d.cts +1 -1
  30. package/dist/components/modal.d.ts +1 -1
  31. package/dist/components/modal.js +26 -26
  32. package/dist/providers/base-events-provider.cjs +1 -1
  33. package/dist/providers/base-events-provider.js +1 -1
  34. package/dist/providers/chat-events-provider.cjs +1 -1
  35. package/dist/providers/chat-events-provider.d.cts +9 -1
  36. package/dist/providers/chat-events-provider.d.ts +9 -1
  37. package/dist/providers/chat-events-provider.js +54 -57
  38. package/dist/providers/chat-form-provider.cjs +1 -1
  39. package/dist/providers/chat-form-provider.d.cts +3 -3
  40. package/dist/providers/chat-form-provider.d.ts +3 -3
  41. package/dist/providers/chat-form-provider.js +17 -17
  42. package/dist/providers/feedback-provider.cjs +1 -1
  43. package/dist/providers/feedback-provider.js +23 -23
  44. package/dist/utils/default-settings.cjs +1 -1
  45. package/dist/utils/default-settings.d.cts +1 -0
  46. package/dist/utils/default-settings.d.ts +1 -0
  47. package/dist/utils/default-settings.js +13 -11
  48. package/dist/utils/misc.cjs +1 -1
  49. package/dist/utils/misc.d.cts +2 -2
  50. package/dist/utils/misc.d.ts +2 -2
  51. package/dist/utils/misc.js +15 -13
  52. package/package.json +5 -4
  53. package/dist/atoms/dialog.cjs +0 -1
  54. package/dist/atoms/dialog.js +0 -185
@@ -1 +1 @@
1
- "use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});async function a(e,o,s){const i=`${s}/events`,r={...e,type:e.eventName};if("searchQuery"in e.properties)r.entityType="search",r.searchQuery=e.properties.searchQuery;else if("messageId"in e.properties)r.entityType="message",r.messageId=e.properties.messageId;else{if(r.entityType="conversation",!e.properties.conversationId)return;r.conversationId=e.properties.conversationId}const t=await fetch(i,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${o}`},body:JSON.stringify(r)});if(!t.ok){const n=await t.text();throw new Error(`Failed to log event: ${n}`)}}exports.logEvent=a;
1
+ "use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});async function a(e,o,s){const i=`${s}/events`,r={...e,type:e.eventName};if("searchQuery"in e.properties)r.entityType="search",r.searchQuery=e.properties.searchQuery;else if("messageId"in e.properties)r.entityType="message",r.messageId=e.properties.messageId;else{if(r.entityType="conversation",!e.properties.conversation)return;r.conversationId=e.properties.conversation.id}const t=await fetch(i,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${o}`},body:JSON.stringify(r)});if(!t.ok){const n=await t.text();throw new Error(`Failed to log event: ${n}`)}}exports.logEvent=a;
@@ -9,8 +9,8 @@ async function a(e, o, s) {
9
9
  else if ("messageId" in e.properties)
10
10
  r.entityType = "message", r.messageId = e.properties.messageId;
11
11
  else {
12
- if (r.entityType = "conversation", !e.properties.conversationId) return;
13
- r.conversationId = e.properties.conversationId;
12
+ if (r.entityType = "conversation", !e.properties.conversation) return;
13
+ r.conversationId = e.properties.conversation.id;
14
14
  }
15
15
  const t = await fetch(i, {
16
16
  method: "POST",
@@ -1 +1 @@
1
- "use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("../../providers/conversation-provider.cjs"),u=require("../../providers/chat-events-provider.cjs"),d=require("../../providers/chat-form-provider.cjs"),p=require("../modal/modal-provider.cjs"),v=e=>{const{logEvent:t}=u.useChatEvents(),{conversation:o}=c.useInkeepConversation(),{openForm:r}=d.useChatForm(),i=p.useModal(),s=a=>{e.action.type==="open_form"?(a?.onOpenForm?.(),r?.(e.action.formConfig)):e.action.type==="invoke_callback"&&(e.action.callback({conversation:o}),e.action.shouldCloseModal&&i?.closeModal())},n=e.action.type,l=n==="open_link"?e.action.url:void 0;return{handleAction:s,logHelpAction:()=>{"name"in e&&t({eventName:"get_help_option_clicked",properties:{actionType:n,name:e.name,url:l,conversationId:o.id}})}}};exports.useChatAction=v;
1
+ "use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("../../providers/conversation-provider.cjs"),l=require("../../providers/chat-events-provider.cjs"),c=require("../../providers/chat-form-provider.cjs"),a=require("../modal/modal-provider.cjs"),u=e=>{const{logEvent:n}=l.useChatEvents(),{conversation:o}=s.useInkeepConversation(),{openForm:t}=c.useChatForm(),r=a.useModal();return{handleAction:i=>{e.action.type==="open_form"?(i?.onOpenForm?.(),t?.(e.action.formSettings)):e.action.type==="invoke_callback"&&(e.action.callback({conversation:o}),e.action.shouldCloseModal&&r?.closeModal())},logHelpAction:()=>{"name"in e&&n({eventName:"get_help_option_clicked",properties:{getHelpOption:e,conversation:o}})}}};exports.useChatAction=u;
@@ -1,26 +1,24 @@
1
1
  "use client";
2
- import { useInkeepConversation as a } from "../../providers/conversation-provider.js";
3
- import { useChatEvents as m } from "../../providers/chat-events-provider.js";
4
- import { useChatForm as c } from "../../providers/chat-form-provider.js";
5
- import { useModal as f } from "../modal/modal-provider.js";
6
- const _ = (o) => {
7
- const { logEvent: t } = m(), { conversation: e } = a(), { openForm: i } = c(), r = f(), l = (p) => {
8
- o.action.type === "open_form" ? (p?.onOpenForm?.(), i?.(o.action.formConfig)) : o.action.type === "invoke_callback" && (o.action.callback({
2
+ import { useInkeepConversation as r } from "../../providers/conversation-provider.js";
3
+ import { useChatEvents as s } from "../../providers/chat-events-provider.js";
4
+ import { useChatForm as p } from "../../providers/chat-form-provider.js";
5
+ import { useModal as a } from "../modal/modal-provider.js";
6
+ const v = (o) => {
7
+ const { logEvent: n } = s(), { conversation: e } = r(), { openForm: t } = p(), l = a();
8
+ return { handleAction: (i) => {
9
+ o.action.type === "open_form" ? (i?.onOpenForm?.(), t?.(o.action.formSettings)) : o.action.type === "invoke_callback" && (o.action.callback({
9
10
  conversation: e
10
- }), o.action.shouldCloseModal && r?.closeModal());
11
- }, n = o.action.type, s = n === "open_link" ? o.action.url : void 0;
12
- return { handleAction: l, logHelpAction: () => {
13
- "name" in o && t({
11
+ }), o.action.shouldCloseModal && l?.closeModal());
12
+ }, logHelpAction: () => {
13
+ "name" in o && n({
14
14
  eventName: "get_help_option_clicked",
15
15
  properties: {
16
- actionType: n,
17
- name: o.name,
18
- url: s,
19
- conversationId: e.id
16
+ getHelpOption: o,
17
+ conversation: e
20
18
  }
21
19
  });
22
20
  } };
23
21
  };
24
22
  export {
25
- _ as useChatAction
23
+ v as useChatAction
26
24
  };
@@ -1,4 +1,4 @@
1
- "use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const be=require("openai"),t=require("react"),qe=require("../../atoms/api/analytics/conversation.cjs"),Pe=require("../../providers/config-provider.cjs"),Ee=require("../../providers/conversation-provider.cjs"),_e=require("../../providers/chat-events-provider.cjs"),$=require("../../utils/get-message-metadata.cjs"),V=require("../../utils/misc.cjs"),X=require("../../utils/reset-query-params.cjs"),Ne=require("../../providers/chat-form-provider.cjs"),Re=require("../../providers/widget-provider.cjs"),Ae=require("../../hooks/use-media-query.cjs"),Le=require("./use-captcha.cjs"),$e=require("../modal/modal-provider.cjs"),xe=()=>{const{chatId:m,setConversation:Y,conversation:Z,resetConversation:ee}=Ee.useInkeepConversation(),{baseSettings:k,aiChatSettings:x}=Pe.useInkeepConfig(),{aiApiBaseUrl:D,shouldBypassCaptcha:O}=k,U=$e.useModal(),{getSolution:te,prefetchSolution:b,invalidateSolution:K}=Le.useCaptcha({fetchUrl:`${D}/v1/challenge`,shouldBypassCaptcha:O,shouldMakeInitialRequest:U?U.isOpen:!0}),se=new be({baseURL:`${D}/v1`,apiKey:k.apiKey,dangerouslyAllowBrowser:!0}),{onInputMessageChange:ne,tools:oe,prompts:ae,model:re,onToolCall:ie}=x,[h,_]=t.useState(""),T=t.useRef(null);t.useEffect(()=>{ne?.(h)},[h]);const[u,l]=t.useState([]),[B,p]=t.useState(!1),[ce,f]=t.useState(!1),[le,F]=t.useState(null),[v,Q]=t.useState(null),[I,y]=t.useState([]),ue=e=>{y(n=>[...n,e])},[de,W]=t.useState({}),i=t.useRef([]),S=t.useRef(null),{logEvent:q}=_e.useChatEvents(),z=ae?.map((e,n)=>({id:`system-${n}`,role:"system",content:e})),[ge,me]=t.useState(!!m);t.useEffect(()=>{if(m){const{apiKey:e,analyticsApiBaseUrl:n}=k;q({eventName:"chat_shared_session_loaded",properties:{conversationId:m}}),qe.getConversation(m,e,n).then(o=>{if(o){me(!1),Y({...o,id:""});const a=o.messages.map(c=>({id:Date.now().toString()+c.id,content:V.parseIfJson(c.content),role:c.role,links:c.links}));l(a),i.current=a}})}},[m]);const he=e=>{switch(e.code){case 400:return e.message;case 403:return`There seems to be a configuration error. Please contact ${k.organizationDisplayName??"Administrator"}`;default:return`Hmm..
1
+ "use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const ke=require("openai"),t=require("react"),be=require("../../atoms/api/analytics/conversation.cjs"),Pe=require("../../providers/config-provider.cjs"),qe=require("../../providers/conversation-provider.cjs"),Ee=require("../../providers/chat-events-provider.cjs"),$=require("../../utils/get-message-metadata.cjs"),Ne=require("../../utils/misc.cjs"),V=require("../../utils/reset-query-params.cjs"),_e=require("../../providers/chat-form-provider.cjs"),Ae=require("../../providers/widget-provider.cjs"),Re=require("../../hooks/use-media-query.cjs"),$e=require("./use-captcha.cjs"),De=require("../modal/modal-provider.cjs"),Le=()=>{const{chatId:m,setConversation:X,conversation:Y,resetConversation:Z}=qe.useInkeepConversation(),{baseSettings:I,aiChatSettings:D}=Pe.useInkeepConfig(),{aiApiBaseUrl:L,shouldBypassCaptcha:x}=I,O=De.useModal(),{getSolution:ee,prefetchSolution:k,invalidateSolution:K}=$e.useCaptcha({fetchUrl:`${L}/v1/challenge`,shouldBypassCaptcha:x,shouldMakeInitialRequest:O?O.isOpen:!0}),te=new ke({baseURL:`${L}/v1`,apiKey:I.apiKey,dangerouslyAllowBrowser:!0}),{onInputMessageChange:se,tools:ne,prompts:oe,model:ae,onToolCall:re}=D,[h,q]=t.useState(""),T=t.useRef(null);t.useEffect(()=>{se?.(h)},[h]);const[u,l]=t.useState([]),[U,p]=t.useState(!1),[ie,f]=t.useState(!1),[ce,B]=t.useState(null),[E,F]=t.useState(null),[v,S]=t.useState([]),le=e=>{S(s=>[...s,e])},[ue,Q]=t.useState({}),i=t.useRef([]),y=t.useRef(null),{logEvent:b}=Ee.useChatEvents(),W=oe?.map((e,s)=>({id:`system-${Date.now().toString()}-${s}`,role:"system",content:e})),[de,ge]=t.useState(!!m);t.useEffect(()=>{if(m){const{apiKey:e,analyticsApiBaseUrl:s}=I;be.getConversation(m,e,s).then(n=>{if(n){console.log("res",n),b({eventName:"shared_chat_loaded",properties:{conversation:n}}),ge(!1),X({...n,id:""});const o=n.messages.map(c=>({id:Date.now().toString()+c.id,content:Ne.parseIfJson(c.content),role:c.role,links:c.links}));l(o),i.current=o}})}},[m]);const me=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.`}},H=e=>{F(e),l(n=>{const o=[...n],a=o[o.length-1];return a&&(a.content=he(e)),o}),p(!1),f(!1),K(),b()},N=!h.trim()&&!I.length||B,pe=e=>{e.key==="Enter"&&!e.shiftKey&&!N&&!e.nativeEvent.isComposing&&(e.preventDefault(),R())},fe=e=>{const n=e.target.value;_(n)},R=async(e=h)=>{if(N&&(!e||e.trim().length===0))return;p(!0);const n=v?u.length===2:u.length===0,o=$.getMessageMetadata(v,I,n);let a;const c=$.serializeAttachments(I);I.length&&c?a=[{type:"text",text:c}]:a=e;const P={id:`${Date.now().toString()}1`,role:"user",content:a,metadata:o},E=[...u.length?[]:z,P];m&&X.resetQueryParams(),l(C=>[...C,...E]),i.current=[...i.current,...E],y([]),_(""),q({eventName:"chat_message_submitted",properties:{messages:i.current,messageId:P.id,content:e,workflowId:v?.id}});try{const C={model:re,messages:i.current,stream:!0,tools:oe,tool_choice:"auto"},j=await te(),Me=j?btoa(JSON.stringify(j)):null,G=O?void 0:{"X-INKEEP-CHALLENGE-SOLUTION":Me},M=se.beta.chat.completions.stream(C,G?{headers:G}:void 0),s={id:`${Date.now().toString()}2`,role:"assistant",content:"",links:[]};l(d=>[...d,s]),M.on("content",(d,A)=>{f(!0),s.content=A,l(L=>{const r=[...L],g=r.findIndex(w=>w.id===s.id);return g===-1?[...r,s]:(r[g]={...s},r)})}),M.on("tool_calls.function.arguments.done",d=>{const{name:A,arguments:L}=d;if(A==="provideLinks"){const r=JSON.parse(L);r.links?.length>0&&(s.links=r.links,l(g=>{const w=[...g],we=w.findIndex(ke=>ke.id===s.id);return w[we]={...s},w}))}else{const r=ie?.(d);r&&W(g=>({...g,[s.id]:[...g[s.id]||[],r]}))}}),M.on("error",d=>{H(d)}),M.finalChatCompletion().then(()=>{i.current=[...i.current,s],S.current=null,p(!1),f(!1),b(),q({eventName:"chat_message_bot_response_received",properties:{messages:i.current,messageId:s.id,question:V.getMessageContent(P),responseMessage:s,linksUsedInResponse:s.links,workflowId:v?.id}})}),S.current=M.controller}catch(C){H(C),p(!1),f(!1)}},ve=()=>{S.current&&(S.current.abort(),S.current=null),p(!1),f(!1),b()},J=()=>{F(null),l([]),i.current=[],ee(),W({}),y([]),X.resetQueryParams(),Q(null),K(),b(),q({eventName:"chat_history_cleared",properties:{conversationId:Z.id}})},Ie=e=>{Q(e);const{initialReplyMessage:n,displayName:o}=e,a={id:u.length.toString(),role:"user",content:o},c={id:(u.length+1).toString(),role:"assistant",content:n,links:[]},E=[{id:`system-${e.id}`,role:"system",content:`Company has asked user to follow this guided workflow:
4
- ${$.serializeWorkflow(e)}`},...z,a,c];l(E),y([])},{openForm:ye}=Ne.useChatForm(),Se=Re.useWidget();t.useImperativeHandle(x.chatFunctionsRef,()=>({submitMessage:R,updateInputMessage(e){_(e)},clearChat:J,openForm:e=>{Se?.setView("chat"),ye(e)},focusInput:()=>{T.current?.focus()}}));const Ce=Ae.useMediaQuery("(max-width: 768px)");return{messages:u,input:h,isLoading:B,isStreaming:ce,isLoadingConversation:ge,error:le,isSubmitDisabled:N,handleInputChange:fe,handleInputKeyDown:pe,handleSubmit:R,stop:ve,clear:J,handleWorkflow:Ie,selectedWorkflow:v,messageAttachments:I,setMessageAttachments:y,addAttachment:ue,messageToolCalls:de,isEmpty:u.length===0,inputRef:T,isMobile:Ce}};exports.useInkeepChat=xe;
3
+ It seems I might be having some issues right now. Please clear the chat and try again.`}},z=e=>{B(e),l(s=>{const n=[...s],o=n[n.length-1];return o&&(o.content=me(e)),n}),p(!1),f(!1),K(),k()},N=!h.trim()&&!v.length||U,he=e=>{e.key==="Enter"&&!e.shiftKey&&!N&&!e.nativeEvent.isComposing&&(e.preventDefault(),_())},pe=e=>{const s=e.target.value;q(s)},_=async(e=h)=>{if(N&&(!e||e.trim().length===0))return;p(!0);const s=E?u.length===2:u.length===0,n=$.getMessageMetadata(E,v,s);let o;const c=$.serializeAttachments(v);v.length&&c?o=[{type:"text",text:c}]:o=e;const J={id:`${Date.now().toString()}1`,role:"user",content:o,metadata:n},P=[...u.length?[]:W,J];m&&V.resetQueryParams(),l(C=>[...C,...P]),i.current=[...i.current,...P],S([]),q(""),b({eventName:"user_message_submitted",properties:{}},i.current);try{const C={model:ae,messages:i.current,stream:!0,tools:ne,tool_choice:"auto"},j=await ee(),Me=j?btoa(JSON.stringify(j)):null,G=x?void 0:{"X-INKEEP-CHALLENGE-SOLUTION":Me},M=te.beta.chat.completions.stream(C,G?{headers:G}:void 0),a={id:`${Date.now().toString()}2`,role:"assistant",content:"",links:[]};l(d=>[...d,a]),M.on("content",(d,A)=>{f(!0),a.content=A,l(R=>{const r=[...R],g=r.findIndex(w=>w.id===a.id);return g===-1?[...r,a]:(r[g]={...a},r)})}),M.on("tool_calls.function.arguments.done",d=>{const{name:A,arguments:R}=d;if(A==="provideLinks"){const r=JSON.parse(R);r.links?.length>0&&(a.links=r.links,l(g=>{const w=[...g],we=w.findIndex(Ie=>Ie.id===a.id);return w[we]={...a},w}))}else{const r=re?.(d);r&&Q(g=>({...g,[a.id]:[...g[a.id]||[],r]}))}}),M.on("error",d=>{z(d)}),M.finalChatCompletion().then(()=>{i.current=[...i.current,a],y.current=null,p(!1),f(!1),k(),b({eventName:"assistant_message_received",properties:{}},i.current)}),y.current=M.controller}catch(C){z(C),p(!1),f(!1)}},fe=()=>{y.current&&(y.current.abort(),y.current=null),p(!1),f(!1),k()},H=()=>{B(null),l([]),i.current=[],Z(),Q({}),S([]),V.resetQueryParams(),F(null),K(),k(),b({eventName:"chat_clear_button_clicked",properties:{conversation:Y}})},ve=e=>{F(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
+ ${$.serializeWorkflow(e)}`},...W,o,c];l(P),S([])},{openForm:Se}=_e.useChatForm(),ye=Ae.useWidget();t.useImperativeHandle(D.chatFunctionsRef,()=>({submitMessage:_,updateInputMessage(e){q(e)},clearChat:H,openForm:e=>{ye?.setView("chat"),Se(e)},focusInput:()=>{T.current?.focus()}}));const Ce=Re.useMediaQuery("(max-width: 768px)");return{messages:u,input:h,isLoading:U,isStreaming:ie,isLoadingConversation:de,error:ce,isSubmitDisabled:N,handleInputChange:pe,handleInputKeyDown:he,handleSubmit:_,stop:fe,clear:H,handleWorkflow:ve,selectedWorkflow:E,messageAttachments:v,setMessageAttachments:S,addAttachment:le,messageToolCalls:ue,isEmpty:u.length===0,inputRef:T,isMobile:Ce}};exports.useInkeepChat=Le;
@@ -1,60 +1,60 @@
1
1
  "use client";
2
2
  import be from "openai";
3
- import { useState as c, useRef as D, useEffect as X, useImperativeHandle as _e } from "react";
4
- import { getConversation as Ee } from "../../atoms/api/analytics/conversation.js";
5
- import { useInkeepConfig as Ne } from "../../providers/config-provider.js";
3
+ import { useState as c, useRef as L, useEffect as X, useImperativeHandle as Ee } from "react";
4
+ import { getConversation as Ne } from "../../atoms/api/analytics/conversation.js";
5
+ import { useInkeepConfig as _e } from "../../providers/config-provider.js";
6
6
  import { useInkeepConversation as Ae } from "../../providers/conversation-provider.js";
7
- import { useChatEvents as xe } from "../../providers/chat-events-provider.js";
8
- import { getMessageMetadata as Le, serializeAttachments as Re, serializeWorkflow as $e } from "../../utils/get-message-metadata.js";
9
- import { parseIfJson as De, getMessageContent as Pe } from "../../utils/misc.js";
7
+ import { useChatEvents as $e } from "../../providers/chat-events-provider.js";
8
+ import { getMessageMetadata as xe, serializeAttachments as De, serializeWorkflow as Le } from "../../utils/get-message-metadata.js";
9
+ import { parseIfJson as Re } from "../../utils/misc.js";
10
10
  import { resetQueryParams as j } from "../../utils/reset-query-params.js";
11
- import { useChatForm as Ue } from "../../providers/chat-form-provider.js";
11
+ import { useChatForm as Pe } from "../../providers/chat-form-provider.js";
12
12
  import { useWidget as Ke } from "../../providers/widget-provider.js";
13
13
  import { useMediaQuery as Oe } from "../../hooks/use-media-query.js";
14
- import { useCaptcha as Be } from "./use-captcha.js";
15
- import { useModal as Te } from "../modal/modal-provider.js";
16
- const tt = () => {
17
- const { chatId: g, setConversation: Y, conversation: Z, resetConversation: ee } = Ae(), { baseSettings: S, aiChatSettings: P } = Ne(), { aiApiBaseUrl: U, shouldBypassCaptcha: K } = S, O = Te(), { getSolution: te, prefetchSolution: b, invalidateSolution: B } = Be({
18
- fetchUrl: `${U}/v1/challenge`,
14
+ import { useCaptcha as Ue } from "./use-captcha.js";
15
+ import { useModal as Be } from "../modal/modal-provider.js";
16
+ const et = () => {
17
+ const { chatId: d, setConversation: Y, conversation: Z, resetConversation: ee } = Ae(), { baseSettings: S, aiChatSettings: R } = _e(), { aiApiBaseUrl: P, shouldBypassCaptcha: K } = S, O = Be(), { getSolution: te, prefetchSolution: k, invalidateSolution: U } = Ue({
18
+ fetchUrl: `${P}/v1/challenge`,
19
19
  shouldBypassCaptcha: K,
20
20
  shouldMakeInitialRequest: O ? O.isOpen : !0
21
21
  }), se = new be({
22
- baseURL: `${U}/v1`,
22
+ baseURL: `${P}/v1`,
23
23
  apiKey: S.apiKey,
24
24
  dangerouslyAllowBrowser: !0
25
- }), { onInputMessageChange: ne, tools: oe, prompts: ae, model: re, onToolCall: ie } = P, [p, A] = c(""), T = D(null);
25
+ }), { onInputMessageChange: ne, tools: oe, prompts: ae, model: re, onToolCall: ie } = R, [p, N] = c(""), B = L(null);
26
26
  X(() => {
27
27
  ne?.(p);
28
28
  }, [p]);
29
- const [u, l] = c([]), [W, f] = c(!1), [ce, h] = c(!1), [le, F] = c(null), [I, z] = c(null), [w, y] = c([]), ue = (e) => {
30
- y((s) => [...s, e]);
31
- }, [me, H] = c({}), r = D([]), C = D(null), { logEvent: _ } = xe(), J = ae?.map((e, s) => ({
32
- id: `system-${s}`,
29
+ const [u, l] = c([]), [T, f] = c(!1), [ce, h] = c(!1), [le, W] = c(null), [_, F] = c(null), [y, C] = c([]), ue = (e) => {
30
+ C((t) => [...t, e]);
31
+ }, [me, z] = c({}), r = L([]), w = L(null), { logEvent: b } = $e(), H = ae?.map((e, t) => ({
32
+ id: `system-${Date.now().toString()}-${t}`,
33
33
  role: "system",
34
34
  content: e
35
- })), [de, ge] = c(!!g);
35
+ })), [ge, de] = c(!!d);
36
36
  X(() => {
37
- if (g) {
38
- const { apiKey: e, analyticsApiBaseUrl: s } = S;
39
- _({
40
- eventName: "chat_shared_session_loaded",
41
- properties: {
42
- conversationId: g
43
- }
44
- }), Ee(g, e, s).then((n) => {
45
- if (n) {
46
- ge(!1), Y({ ...n, id: "" });
47
- const o = n.messages.map((i) => ({
37
+ if (d) {
38
+ const { apiKey: e, analyticsApiBaseUrl: t } = S;
39
+ Ne(d, e, t).then((s) => {
40
+ if (s) {
41
+ console.log("res", s), b({
42
+ eventName: "shared_chat_loaded",
43
+ properties: {
44
+ conversation: s
45
+ }
46
+ }), de(!1), Y({ ...s, id: "" });
47
+ const n = s.messages.map((i) => ({
48
48
  id: Date.now().toString() + i.id,
49
- content: De(i.content),
49
+ content: Re(i.content),
50
50
  role: i.role,
51
51
  links: i.links
52
52
  }));
53
- l(o), r.current = o;
53
+ l(n), r.current = n;
54
54
  }
55
55
  });
56
56
  }
57
- }, [g]);
57
+ }, [d]);
58
58
  const pe = (e) => {
59
59
  switch (e.code) {
60
60
  case 400:
@@ -66,40 +66,38 @@ const tt = () => {
66
66
 
67
67
  It seems I might be having some issues right now. Please clear the chat and try again.`;
68
68
  }
69
- }, q = (e) => {
70
- F(e), l((s) => {
71
- const n = [...s], o = n[n.length - 1];
72
- return o && (o.content = pe(e)), n;
73
- }), f(!1), h(!1), B(), b();
74
- }, x = !p.trim() && !w.length || W, fe = (e) => {
75
- e.key === "Enter" && !e.shiftKey && !x && !e.nativeEvent.isComposing && (e.preventDefault(), L());
69
+ }, J = (e) => {
70
+ W(e), l((t) => {
71
+ const s = [...t], n = s[s.length - 1];
72
+ return n && (n.content = pe(e)), s;
73
+ }), f(!1), h(!1), U(), k();
74
+ }, A = !p.trim() && !y.length || T, fe = (e) => {
75
+ e.key === "Enter" && !e.shiftKey && !A && !e.nativeEvent.isComposing && (e.preventDefault(), $());
76
76
  }, he = (e) => {
77
- const s = e.target.value;
78
- A(s);
79
- }, L = async (e = p) => {
80
- if (x && (!e || e.trim().length === 0)) return;
77
+ const t = e.target.value;
78
+ N(t);
79
+ }, $ = async (e = p) => {
80
+ if (A && (!e || e.trim().length === 0)) return;
81
81
  f(!0);
82
- const s = I ? u.length === 2 : u.length === 0, n = Le(I, w, s);
83
- let o;
84
- const i = Re(w);
85
- w.length && i ? o = [{ type: "text", text: i }] : o = e;
86
- const E = {
82
+ const t = _ ? u.length === 2 : u.length === 0, s = xe(_, y, t);
83
+ let n;
84
+ const i = De(y);
85
+ y.length && i ? n = [{ type: "text", text: i }] : n = e;
86
+ const q = {
87
87
  id: `${Date.now().toString()}1`,
88
88
  role: "user",
89
- content: o,
90
- metadata: n
91
- }, N = [...u.length ? [] : J, E];
92
- g && j(), l((v) => [...v, ...N]), r.current = [...r.current, ...N], y([]), A(""), _({
93
- eventName: "chat_message_submitted",
94
- properties: {
95
- messages: r.current,
96
- messageId: E.id,
97
- content: e,
98
- workflowId: I?.id
99
- }
100
- });
89
+ content: n,
90
+ metadata: s
91
+ }, E = [...u.length ? [] : H, q];
92
+ d && j(), l((I) => [...I, ...E]), r.current = [...r.current, ...E], C([]), N(""), b(
93
+ {
94
+ eventName: "user_message_submitted",
95
+ properties: {}
96
+ },
97
+ r.current
98
+ );
101
99
  try {
102
- const v = {
100
+ const I = {
103
101
  model: re,
104
102
  messages: r.current,
105
103
  stream: !0,
@@ -107,121 +105,117 @@ It seems I might be having some issues right now. Please clear the chat and try
107
105
  tool_choice: "auto"
108
106
  }, G = await te(), Me = G ? btoa(JSON.stringify(G)) : null, V = K ? void 0 : {
109
107
  "X-INKEEP-CHALLENGE-SOLUTION": Me
110
- }, M = se.beta.chat.completions.stream(
111
- v,
108
+ }, v = se.beta.chat.completions.stream(
109
+ I,
112
110
  V ? { headers: V } : void 0
113
- ), t = {
111
+ ), o = {
114
112
  id: `${Date.now().toString()}2`,
115
113
  role: "assistant",
116
114
  content: "",
117
115
  links: []
118
116
  };
119
- l((m) => [...m, t]), M.on("content", (m, R) => {
120
- h(!0), t.content = R, l(($) => {
121
- const a = [...$], d = a.findIndex((k) => k.id === t.id);
122
- return d === -1 ? [...a, t] : (a[d] = { ...t }, a);
117
+ l((m) => [...m, o]), v.on("content", (m, x) => {
118
+ h(!0), o.content = x, l((D) => {
119
+ const a = [...D], g = a.findIndex((M) => M.id === o.id);
120
+ return g === -1 ? [...a, o] : (a[g] = { ...o }, a);
123
121
  });
124
- }), M.on("tool_calls.function.arguments.done", (m) => {
125
- const { name: R, arguments: $ } = m;
126
- if (R === "provideLinks") {
127
- const a = JSON.parse($);
128
- a.links?.length > 0 && (t.links = a.links, l((d) => {
129
- const k = [...d], ke = k.findIndex((Se) => Se.id === t.id);
130
- return k[ke] = { ...t }, k;
122
+ }), v.on("tool_calls.function.arguments.done", (m) => {
123
+ const { name: x, arguments: D } = m;
124
+ if (x === "provideLinks") {
125
+ const a = JSON.parse(D);
126
+ a.links?.length > 0 && (o.links = a.links, l((g) => {
127
+ const M = [...g], Se = M.findIndex((ke) => ke.id === o.id);
128
+ return M[Se] = { ...o }, M;
131
129
  }));
132
130
  } else {
133
131
  const a = ie?.(m);
134
- a && H((d) => ({
135
- ...d,
136
- [t.id]: [...d[t.id] || [], a]
132
+ a && z((g) => ({
133
+ ...g,
134
+ [o.id]: [...g[o.id] || [], a]
137
135
  }));
138
136
  }
139
- }), M.on("error", (m) => {
140
- q(m);
141
- }), M.finalChatCompletion().then(() => {
142
- r.current = [...r.current, t], C.current = null, f(!1), h(!1), b(), _({
143
- eventName: "chat_message_bot_response_received",
144
- properties: {
145
- messages: r.current,
146
- messageId: t.id,
147
- question: Pe(E),
148
- responseMessage: t,
149
- linksUsedInResponse: t.links,
150
- workflowId: I?.id
151
- }
152
- });
153
- }), C.current = M.controller;
154
- } catch (v) {
155
- q(v), f(!1), h(!1);
137
+ }), v.on("error", (m) => {
138
+ J(m);
139
+ }), v.finalChatCompletion().then(() => {
140
+ r.current = [...r.current, o], w.current = null, f(!1), h(!1), k(), b(
141
+ {
142
+ eventName: "assistant_message_received",
143
+ properties: {}
144
+ },
145
+ r.current
146
+ );
147
+ }), w.current = v.controller;
148
+ } catch (I) {
149
+ J(I), f(!1), h(!1);
156
150
  }
157
- }, Ie = () => {
158
- C.current && (C.current.abort(), C.current = null), f(!1), h(!1), b();
151
+ }, ye = () => {
152
+ w.current && (w.current.abort(), w.current = null), f(!1), h(!1), k();
159
153
  }, Q = () => {
160
- F(null), l([]), r.current = [], ee(), H({}), y([]), j(), z(null), B(), b(), _({
161
- eventName: "chat_history_cleared",
154
+ W(null), l([]), r.current = [], ee(), z({}), C([]), j(), F(null), U(), k(), b({
155
+ eventName: "chat_clear_button_clicked",
162
156
  properties: {
163
- conversationId: Z.id
157
+ conversation: Z
164
158
  }
165
159
  });
166
- }, we = (e) => {
167
- z(e);
168
- const { initialReplyMessage: s, displayName: n } = e, o = {
160
+ }, Ce = (e) => {
161
+ F(e);
162
+ const { initialReplyMessage: t, displayName: s } = e, n = {
169
163
  id: u.length.toString(),
170
164
  role: "user",
171
- content: n
165
+ content: s
172
166
  }, i = {
173
167
  id: (u.length + 1).toString(),
174
168
  role: "assistant",
175
- content: s,
169
+ content: t,
176
170
  links: []
177
- }, N = [{
178
- id: `system-${e.id}`,
171
+ }, E = [{
172
+ id: `system-${Date.now().toString()}-${e.id}`,
179
173
  role: "system",
180
174
  content: `Company has asked user to follow this guided workflow:
181
- ${$e(
175
+ ${Le(
182
176
  e
183
177
  )}`
184
- }, ...J, o, i];
185
- l(N), y([]);
186
- }, { openForm: ye } = Ue(), Ce = Ke();
187
- _e(P.chatFunctionsRef, () => ({
188
- submitMessage: L,
178
+ }, ...H, n, i];
179
+ l(E), C([]);
180
+ }, { openForm: we } = Pe(), Ie = Ke();
181
+ Ee(R.chatFunctionsRef, () => ({
182
+ submitMessage: $,
189
183
  updateInputMessage(e) {
190
- A(e);
184
+ N(e);
191
185
  },
192
186
  clearChat: Q,
193
187
  openForm: (e) => {
194
- Ce?.setView("chat"), ye(e);
188
+ Ie?.setView("chat"), we(e);
195
189
  },
196
190
  focusInput: () => {
197
- T.current?.focus();
191
+ B.current?.focus();
198
192
  }
199
193
  }));
200
194
  const ve = Oe("(max-width: 768px)");
201
195
  return {
202
196
  messages: u,
203
197
  input: p,
204
- isLoading: W,
198
+ isLoading: T,
205
199
  isStreaming: ce,
206
- isLoadingConversation: de,
200
+ isLoadingConversation: ge,
207
201
  error: le,
208
- isSubmitDisabled: x,
202
+ isSubmitDisabled: A,
209
203
  handleInputChange: he,
210
204
  handleInputKeyDown: fe,
211
- handleSubmit: L,
212
- stop: Ie,
205
+ handleSubmit: $,
206
+ stop: ye,
213
207
  clear: Q,
214
- handleWorkflow: we,
215
- selectedWorkflow: I,
216
- messageAttachments: w,
217
- setMessageAttachments: y,
208
+ handleWorkflow: Ce,
209
+ selectedWorkflow: _,
210
+ messageAttachments: y,
211
+ setMessageAttachments: C,
218
212
  addAttachment: ue,
219
213
  messageToolCalls: me,
220
214
  isEmpty: u.length === 0,
221
- inputRef: T,
215
+ inputRef: B,
222
216
  isMobile: ve
223
217
  };
224
218
  };
225
219
  export {
226
- tt as useInkeepChat
220
+ et as useInkeepChat
227
221
  };