efficient-storefront-blocks 0.126.2 → 0.126.4
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/CHANGELOG.md +30 -0
- package/dist/index.js +22 -22
- package/dist/render/components/AssistantChatPanel.js +1 -1
- package/dist/render/components/ChatContext.js +1 -1
- package/dist/render/lib/chat-socket.d.ts +14 -2
- package/dist/render/lib/chat-socket.js +1 -1
- package/dist/routine-index.json +1 -1
- package/dist/value-index.json +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
"use client";import{jsx as I}from"react/jsx-runtime";import{useCallback as o,useEffect as O,useMemo as R,useRef as s,useState as m}from"react";import{AssistantChat as _}from"efficient-shell/ai";import"efficient-shell/ai.css";import{api as d,API_URL as w}from"../lib/api/index.js";import{auth as r}from"../lib/auth.js";import{safeLocalStorage as S}from"efficient-web-builder/format/lib/safe-storage";import{webRuntimeSiteSlug as
|
|
1
|
+
"use client";import{jsx as I}from"react/jsx-runtime";import{useCallback as o,useEffect as O,useMemo as R,useRef as s,useState as m}from"react";import{AssistantChat as _}from"efficient-shell/ai";import"efficient-shell/ai.css";import{api as d,API_URL as w}from"../lib/api/index.js";import{auth as r}from"../lib/auth.js";import{safeLocalStorage as S}from"efficient-web-builder/format/lib/safe-storage";import{webRuntimeSiteSlug as F}from"efficient-web-builder/format/lib/web-runtime";import{ASSISTANT_LABELS as k,ASSISTANT_PLACEHOLDER as v,STOREFRONT_TOOL_LABELS as H,assistantHeaders as N,assistantSiteKey as x,assistantStorageKey as B,assistantWording as D,createAssistantFetch as K,readStoredAssistantChat as P,writeStoredAssistantChat as U}from"../lib/assistant-chat.js";import{isOffStreamMessage as j,openChatSocket as G}from"../lib/chat-socket.js";import{DEFAULT_GREETING as M,DEFAULT_TITLE as W}from"./ChatWidget.js";function $({title:p,greeting:g,onUnavailable:i}){const[A,h]=m(!1),[T,c]=m(null),n=s(""),e=s(void 0),u=s(null),l=s(i);l.current=i,O(()=>{n.current=B(x(window.location.host));const t=P(S,n.current);e.current=t?.guestToken,c(t),u.current=d.restoreSession().catch(()=>{}),h(!0)},[]);const L=o(async()=>(await u.current,N(r.getAccess(),e.current)),[]),b=R(()=>K({fetchImpl:(t,a)=>fetch(t,a),siteSlug:F,access:r.getAccess,refresh:async()=>(await d.refresh()).access,signOut:r.clear,onUnavailable:()=>l.current()}),[]),E=o(t=>{e.current=t?.guestToken,U(S,n.current,t),c(t)},[]),y=o((t,a)=>G({conversationId:t.id,credential:()=>({access:r.getAccess(),guest:e.current}),onFrame:C=>{j(C)&&a()}}),[]),f=D({title:p,greeting:g},{title:W,greeting:M});return A?I(_,{variant:"bubble",defaultOpen:!0,apiBase:`${w}/api`,getHeaders:L,fetch:b,conversationId:T?.id??null,onConversation:E,subscribe:y,title:f.title,placeholder:v,toolLabels:H,labels:{...k,emptyState:f.emptyState}}):null}export{$ as default};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use client";import{jsx as
|
|
1
|
+
"use client";import{jsx as z}from"react/jsx-runtime";import{createContext as B,useCallback as f,useContext as D,useEffect as k,useMemo as F,useRef as x,useState as c}from"react";import{auth as G}from"../lib/auth.js";import{api as Q}from"../lib/api/index.js";import{chatSocketTarget as X}from"../lib/chat-socket.js";import{safeLocalStorage as I}from"efficient-web-builder/format/lib/safe-storage";import{getChatThread as Z,postChatMessage as ee,startChat as te}from"../lib/api/chat.js";const se="sf:aid",A="sf:chat",H=B(null);function ne(){const u=I.getItem(A);if(!u)return null;try{return JSON.parse(u)}catch{return null}}function fe({children:u,preview:t=!1}){const[m,y]=c(!1),[e,O]=c(null),[T,g]=c([]),[b,E]=c(!1),[v,P]=c(null),[M,C]=c(0),[N,R]=c(!1),[J,L]=c(!1),S=x(m);S.current=m;const h=x(null),_=x(e);_.current=e,k(()=>{t||O(ne())},[t]);const l=f(async r=>{try{const o=await Z(r.id,r.token);g(o.messages)}catch{}},[]),i=f(()=>(t||h.current||(h.current=Q.restoreSession().then(()=>{R(!0),_.current&&l(_.current)},()=>R(!1))),h.current),[t,l]);k(()=>{t||e&&l(e)},[t,e,l]),k(()=>{if(t||!e)return;const r={access:G.getAccess(),guest:e.token};let o=!1,s=null,n,a=3e3;const d=()=>{if(o)return;const{url:V,protocols:W}=X(e.id,r);s=new WebSocket(V,W),s.onmessage=$=>{try{const p=JSON.parse($.data);p.type==="chat_event"&&p.event==="message"&&(l(e),!S.current&&p.payload?.sender_role==="employee"&&C(q=>q+1))}catch{}},s.onclose=()=>{o||(n=setTimeout(d,a),a=Math.min(a*2,3e4))},s.onerror=()=>s?.close()};return d(),()=>{o=!0,n&&clearTimeout(n),s?.close()}},[t,e,l]);const K=f(()=>{y(!0),C(0),i()},[i]),Y=f(()=>y(!1),[]),j=f(()=>{S.current||i(),y(r=>(r||C(0),!r))},[i]),w=f(async(r,o)=>{const s=r.trim();if(!s)return!1;if(t)return g(n=>[...n,{id:`preview-${n.length}`,sender_role:"customer",sender_name:o?.name||"You",body:s,image_url:null,created_at:""}]),L(!0),!0;E(!0),P(null),await i();try{if(e){const n=await ee(e.id,e.token,s);g(a=>[...a,n])}else{const n=I.getItem(se)||"",a=await te({body:s,guestName:o?.name,guestEmail:o?.email,guestKey:n}),d={id:a.id,token:a.guest_token};I.setItem(A,JSON.stringify(d)),O(d),g(a.messages)}return!0}catch{return P("Could not send your message. Please try again shortly."),!1}finally{E(!1)}},[e,t,i]),U=F(()=>({isOpen:m,open:K,close:Y,toggle:j,messages:T,sending:b,error:v,unread:M,hasConversation:t?J:!!e,isSignedIn:N,send:w}),[m,K,Y,j,T,b,v,M,e,N,w,t,J]);return z(H.Provider,{value:U,children:u})}function de(){const u=D(H);if(!u)throw new Error("useChat must be used within <ChatProvider>");return u}export{fe as ChatProvider,de as useChat};
|
|
@@ -1,8 +1,20 @@
|
|
|
1
|
+
export declare const ACCESS_TOKEN_SUBPROTOCOL = "efficient-access-token";
|
|
2
|
+
export declare const GUEST_TOKEN_SUBPROTOCOL = "efficient-chat-guest";
|
|
3
|
+
export interface ChatSocketCredential {
|
|
4
|
+
access?: string | null;
|
|
5
|
+
guest?: string | null;
|
|
6
|
+
}
|
|
7
|
+
export interface ChatSocketTarget {
|
|
8
|
+
url: string;
|
|
9
|
+
protocols: string[];
|
|
10
|
+
}
|
|
11
|
+
export declare function chatSocketTarget(id: string, credential: ChatSocketCredential): ChatSocketTarget;
|
|
1
12
|
export declare function chatSocketUrl(id: string, token: string): string;
|
|
2
13
|
export declare function isOffStreamMessage(raw: unknown): boolean;
|
|
3
14
|
export interface ChatSocketOptions {
|
|
4
15
|
conversationId: string;
|
|
5
|
-
|
|
16
|
+
credential?: () => ChatSocketCredential;
|
|
17
|
+
token?: () => string;
|
|
6
18
|
onFrame: (raw: unknown) => void;
|
|
7
19
|
}
|
|
8
|
-
export declare function openChatSocket({ conversationId, token, onFrame }: ChatSocketOptions): () => void;
|
|
20
|
+
export declare function openChatSocket({ conversationId, credential, token, onFrame }: ChatSocketOptions): () => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{API_URL as
|
|
1
|
+
import{API_URL as S}from"./api/index.js";function f(){return(S||(typeof window<"u"?window.location.origin:"")).replace(/^http/,"ws")}const m="efficient-access-token",_="efficient-chat-guest";function h(t,e){const n=`${f()}/ws/chat/?conversation=${encodeURIComponent(t)}`;return e.access?{url:n,protocols:[m,e.access]}:e.guest?{url:n,protocols:[_,e.guest]}:{url:n,protocols:[]}}function O(t,e){return`${f()}/ws/chat/?conversation=${t}&token=${encodeURIComponent(e)}`}const g=new Set(["employee","system"]);function k(t){if(typeof t!="string")return!1;try{const e=JSON.parse(t);return e.type==="chat_event"&&e.event==="message"&&typeof e.payload?.sender_role=="string"&&g.has(e.payload.sender_role)}catch{return!1}}function T({conversationId:t,credential:e,token:n,onFrame:u}){let s=!1,o=null,c,r=3e3;const a=()=>{if(s)return;const{url:l,protocols:p}=e?h(t,e()):{url:O(t,n?.()??""),protocols:[]};o=new WebSocket(l,p),o.onopen=()=>{r=3e3},o.onmessage=i=>u(i.data),o.onclose=()=>{s||(c=setTimeout(a,r),r=Math.min(r*2,3e4))},o.onerror=()=>o?.close()};return a(),()=>{s=!0,c&&clearTimeout(c),o?.close()}}export{m as ACCESS_TOKEN_SUBPROTOCOL,_ as GUEST_TOKEN_SUBPROTOCOL,h as chatSocketTarget,O as chatSocketUrl,k as isOffStreamMessage,T as openChatSocket};
|