@forbespro/lead-agent
Version:
Lead Chat Agent React Component
2 lines (1 loc) • 9.13 kB
JavaScript
;Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("react/jsx-runtime"),w=require("framer-motion"),y=require("react"),$=require("./index27.js"),k=require("./index28.js"),_=({product:l})=>l?e.jsx("div",{className:"fpl-mt-3 fpl-mb-4 fpl-border fpl-rounded-lg fpl-overflow-hidden fpl-shadow-sm fpl-bg-white",children:e.jsx("a",{href:`${l.slug||l.id?`/products/${l.slug||l.id}`:"#"}`,className:"fpl-block fpl-no-underline fpl-text-inherit hover:fpl-bg-gray-50","aria-label":`View ${l.name||l.product_type} details`,children:e.jsxs("div",{className:"fpl-flex fpl-items-center",children:[l.product_image&&e.jsx("div",{className:"fpl-w-20 fpl-h-20 fpl-flex-shrink-0",children:e.jsx("img",{src:l.product_image,alt:l.product_image_alt||l.name||l.product_type,className:"fpl-w-full fpl-h-full fpl-object-cover"})}),e.jsxs("div",{className:"fpl-p-3 fpl-flex-1",children:[e.jsxs("h4",{className:"fpl-font-medium fpl-text-primary fpl-text-sm",children:[l.manufacturer," ",l.name||l.product_type]}),l.model_number&&e.jsxs("div",{className:"fpl-text-xs fpl-text-gray-600",children:["Model: ",l.model_number]}),e.jsx("span",{className:"fpl-text-xs fpl-text-blue-600",children:"View details"})]})]})})}):null,S=l=>l.length>0?l.map((o,n)=>{const t=`product-card-${o.id||o.objectID||n}-${Date.now()}-${Math.random().toString(36).substring(2,9)}`;return e.jsx(_,{product:o},t)}):null,g=({text:l,className:o,role:n})=>{if(n==="user")return e.jsx("div",{className:o||"fpl-inline",children:e.jsx("div",{className:"fpl-leading-relaxed",children:l})});const i=y.useMemo(()=>typeof l!="string"?"":l,[l]);return e.jsx("div",{className:o||"fpl-inline",children:e.jsx("div",{className:"fpl-leading-relaxed fpl-overflow-auto last:!fpl-mb-0",children:e.jsx(k.Markdown,{components:{a:({href:t,children:s})=>e.jsx("a",{href:t,target:"_blank",rel:"noopener noreferrer",className:"fpl-text-blue-600 fpl-underline fpl-text-base",children:s}),p:({children:t})=>e.jsx("p",{className:"fpl-mb-2 fpl-text-base",children:t}),ul:({children:t})=>e.jsx("ul",{className:"fpl-list-disc fpl-ml-5 fpl-mb-2 fpl-text-base",children:t}),ol:({children:t})=>e.jsx("ol",{className:"fpl-list-decimal fpl-ml-5 fpl-mb-2 fpl-text-base ",children:t}),li:({children:t})=>e.jsx("li",{className:"fpl-mb-1 fpl-text-sm",children:t}),h1:({children:t})=>e.jsx("h1",{className:"fpl-text-lg fpl-font-bold fpl-mb-2",children:t}),h2:({children:t})=>e.jsx("h2",{className:"fpl-text-md fpl-font-bold fpl-mb-1",children:t}),h3:({children:t})=>e.jsx("h3",{className:"fpl-text-base fpl-font-bold fpl-mb-1",children:t}),code:({children:t})=>e.jsx("code",{className:"fpl-bg-gray-100 fpl-p-1 fpl-rounded",children:t}),pre:({children:t})=>e.jsx("pre",{className:"fpl-bg-gray-100 fpl-p-2 fpl-rounded fpl-mb-4 fpl-overflow-auto",children:t}),strong:({children:t})=>e.jsx("strong",{className:"fpl-font-bold",children:t})},children:i})})})},j=l=>{if(!l)return"";const o=typeof l=="object"?l:new Date(l),n=typeof navigator!="undefined"?navigator.language:"en-GB";return o.toLocaleDateString(n,{month:"numeric",day:"numeric",year:"numeric"})+", "+o.toLocaleTimeString(n,{hour:"2-digit",minute:"2-digit",hour12:!0})},M=l=>{var o;if(((o=l.toolInvocation)==null?void 0:o.toolName)==="searchProducts"&&l.toolInvocation.result){const n=l.toolInvocation.result.products;if(!n||n.length===0)return null;const i=n.reduce((s,f)=>{const r=f.id||f.objectID||JSON.stringify(f);return s.some(c=>(c.id||c.objectID)===r)||s.push(f),s},[]);if(i.length===0)return null;const t=`product-tool-${l.toolInvocation.toolCallId}-${Date.now()}-${Math.random().toString(36).substring(2,9)}`;return e.jsx("div",{className:"fpl-mt-2",children:i.map((s,f)=>{const r=s.id||s.objectID||f,c=`product-${l.toolInvocation.toolCallId}-${r}-${f}-${Date.now()}-${Math.random().toString(36).substring(2,9)}`;return e.jsx("div",{children:S([s])},c)})},t)}return null},C=(l,o)=>{var n;return((n=l.toolInvocation)==null?void 0:n.toolName)==="searchBookings"&&l.toolInvocation.result?e.jsx("div",{className:"fpl-mt-2",children:l.toolInvocation.result.bookings.map((i,t)=>e.jsx("div",{children:o([i])},`booking-${l.toolInvocation.toolCallId}-${t}`))},`booking-tool-${l.toolInvocation.toolCallId}`):null},q=(l,o)=>{var n;return((n=l.toolInvocation)==null?void 0:n.toolName)==="buildQuote"&&l.toolInvocation.result?e.jsx("div",{className:"fpl-mt-2",children:o(l.toolInvocation.result)},`quote-tool-${l.toolInvocation.toolCallId}`):null},v=({type:l="text",placeholder:o="",onSubmit:n})=>{const[i,t]=y.useState(""),s=f=>{f.preventDefault(),n(i),t("")};return e.jsxs("form",{onSubmit:s,className:"fpl-mt-2",children:[e.jsx("input",{type:l,placeholder:o,value:i,onChange:f=>t(f.target.value),className:"fpl-w-full fpl-p-2 fpl-border fpl-rounded",required:!0}),e.jsx("button",{type:"submit",className:"fpl-mt-2 fpl-px-4 fpl-py-2 fpl-bg-blue-500 fpl-text-white fpl-rounded",children:"Submit"})]})},N=({options:l,onSelect:o})=>e.jsx("div",{className:"fpl-flex fpl-flex-wrap fpl-gap-2 fpl-mt-2",children:l.map((n,i)=>e.jsx("button",{onClick:()=>o(n),className:"fpl-px-4 fpl-py-2 fpl-bg-gray-200 hover:fpl-bg-gray-300 fpl-rounded",children:n},i))}),T=({question:l,onSelect:o})=>e.jsxs("div",{className:"fpl-mt-2",children:[e.jsx("p",{className:"fpl-mb-2",children:l}),e.jsxs("div",{className:"fpl-flex fpl-gap-2",children:[e.jsx("button",{onClick:()=>o("Yes"),className:"fpl-px-4 fpl-py-2 fpl-bg-green-500 fpl-text-white fpl-rounded",children:"Yes"}),e.jsx("button",{onClick:()=>o("No"),className:"fpl-px-4 fpl-py-2 fpl-bg-gray-500 fpl-text-white fpl-rounded",children:"No"})]})]}),I=({message:l,renderBookingResults:o,renderQuoteBuilder:n,isStreaming:i=!1,handleTriggerAction:t})=>{const s=(r,c)=>{t&&t(r,c)},f=()=>{var c;const r=(c=l.annotations)==null?void 0:c.find(p=>p.type==="ui_trigger");if(l.parts&&Array.isArray(l.parts)&&l.parts.length>0){const p=l.parts.filter(a=>a.type==="text").length;return e.jsx(e.Fragment,{children:l.parts.map((a,u)=>{var d,x,m,b,h;if(a.type==="step-start")return e.jsx("div",{className:"fpl-hidden fpl-mt-0"},`step-start-${u}`);if(a.type==="text")return e.jsx("div",{"aria-label":`Message content for ${l.role}`,className:`fpl-order-first fpl-max-w-[275px] ${p>1?"flp-mb-2":""} fpl-rounded-lg fpl-p-3 fpl-w-full fpl-flex-f fpl-shadow-sm fpl-break-words ${l.role==="user"?"fpl-bg-[#132968] !fpl-text-white fpl-rounded-br-none":"fpl-bg-gray-200 !fpl-text-black fpl-rounded-bl-none"}`,children:e.jsx(g,{text:a.text||"",role:l.role,isStreaming:i,className:`${l.role==="user"?"!fpl-text-white":"!fpl-text-black"} fpl-text-base`})},`text-${u}`);if(a.type==="tool-invocation"){if(((d=a.toolInvocation)==null?void 0:d.toolName)==="openExternalLink"&&((x=a.toolInvocation.result)!=null&&x.url))return e.jsx($.default,{url:a.toolInvocation.result.url,displayText:a.toolInvocation.result.displayText||"Open Link",openInNewTab:!0},`link-${u}`);if(((m=a.toolInvocation)==null?void 0:m.toolName)==="searchProducts")return M(a);if(((b=a.toolInvocation)==null?void 0:b.toolName)==="searchBookings"&&o)return C(a,o);if(((h=a.toolInvocation)==null?void 0:h.toolName)==="buildQuote"&&n)return q(a,n)}return null})})}else return e.jsxs(e.Fragment,{children:[e.jsx("div",{"aria-label":`Message content for ${l.role}`,className:`fpl-order-first fpl-max-w-[275px] fpl-rounded-lg fpl-p-3 fpl-w-full fpl-flex-f fpl-shadow-sm fpl-break-words ${l.role==="user"?"fpl-bg-[#132968] !fpl-text-white fpl-rounded-br-none":"fpl-bg-gray-200 !fpl-text-black fpl-rounded-bl-none"}`,children:e.jsx(g,{text:l.content||"",role:l.role,isStreaming:i,className:`fpl-mb-2 ${l.role==="user"?"!fpl-text-white":"!fpl-text-black"} fpl-text-base`})}),l.role==="assistant"&&r&&e.jsxs("div",{className:"fpl-mt-4 fpl-w-full",children:[r.trigger==="input_field"&&e.jsx(v,{type:r.inputType||"text",placeholder:r.placeholder||"",onSubmit:p=>s(r.trigger,p)}),r.trigger==="button_options"&&r.options&&e.jsx(N,{options:r.options,onSelect:p=>s(r.trigger,p)}),r.trigger==="yes_no_buttons"&&e.jsx(T,{question:r.question||"Please confirm:",onSelect:p=>s(r.trigger,p)}),r.trigger==="sector_search"&&e.jsx(v,{type:"text",placeholder:"Search for your industry",onSubmit:p=>s("sector",p)}),r.trigger==="welcome_options"&&e.jsx(N,{options:["Get a quote","Product information","Contact us"],onSelect:p=>s("welcome",p)})]})]})};return i?null:e.jsxs(w.motion.div,{className:`fpl-mb-4 fpl-flex ${l.role==="user"?"fpl-flex-col fpl-items-end":"fpl-flex-col fpl-items-start"}`,initial:{opacity:0,y:10},animate:{opacity:1,y:0},transition:{duration:.3},role:"listitem","aria-label":`${l.role==="user"?"You":"Assistant"} message`,children:[e.jsx("div",{className:`fpl-message-content fpl-mb-2 fpl-flex ${l.role==="user"?"fpl-flex-col fpl-items-end":"fpl-flex-col fpl-items-start"}`,lang:"en",children:f()}),(!l.parts||l.parts.length!==1||l.parts[0].type!=="step-start")&&e.jsx("div",{className:`fpl-text-xs fpl-mt-1 ${l.role==="user"?"fpl-text-right fpl-text-gray-500":"fpl-text-left fpl-text-gray-500"}`,"aria-label":`Sent ${j(l.createdAt||Date.now())}`,children:j(l.createdAt||Date.now())})]})};exports.ChatMessage=I;exports.default=I;