@webority/mobile-chat 0.160.0 → 0.161.0
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ChatThread=ChatThread;var _chatCore=require("@webority/chat-core"),_mobileUi=require("@webority/mobile-ui"),_react=require("react"),_reactNative=require("react-native"),_AgentByline=require("./AgentByline.js"),_ChatEntrance=require("./ChatEntrance.js"),_ChatMessageActions=require("./ChatMessageActions.js"),_chatShared=require("./chatShared.js"),_ChatLoader=require("./loaders/ChatLoader.js"),_ReplyRing=require("./loaders/ReplyRing.js"),_TypingPill=require("./loaders/TypingPill.js"),_index=require("./renderers/index.js"),_jsxRuntime=require("react/jsx-runtime");const MINE=new Set(["Visitor","Customer"]),visuallyHidden={position:"absolute",left:-1e4,width:1,height:1,overflow:"hidden"},spokenAuthor=(e,t,s)=>MINE.has(e.author.kind)?t.you:e.author.displayName?e.author.displayName:"Agent"===e.author.kind?t.agent:s??t.assistant,joinedText=e=>e.filter(e=>"Text"===e.kind).map(e=>e.text).join("\n\n");function isKind(e,t){return e.kind===t}const kindWrappers=new WeakMap;function forKind(e,t){const s=kindWrappers.get(t)??new Map,a=s.get(e);if(a)return a;function KindEntry(s){if(!isKind(s.part,e))throw new Error(`renderer for ${e} received a ${s.part.kind} part`);return(0,_react.createElement)(t,{part:s.part,message:s.message,turn:s.turn,pickAttachments:s.pickAttachments})}return s.set(e,KindEntry),kindWrappers.set(t,s),KindEntry}const defaults=_index.defaultRenderers;function entryFor(e,t){return forKind(e,t??defaults[e])}function resolveRenderers(e){return{Text:entryFor("Text",e?.Text),Sources:entryFor("Sources",e?.Sources),QuickReplies:entryFor("QuickReplies",e?.QuickReplies),InputRequest:entryFor("InputRequest",e?.InputRequest),Action:entryFor("Action",e?.Action),Notice:entryFor("Notice",e?.Notice),Table:entryFor("Table",e?.Table),Chart:entryFor("Chart",e?.Chart),Figures:entryFor("Figures",e?.Figures),RecordCard:entryFor("RecordCard",e?.RecordCard),Image:entryFor("Image",e?.Image),File:entryFor("File",e?.File),Ticket:entryFor("Ticket",e?.Ticket),Handoff:entryFor("Handoff",e?.Handoff),VoiceClip:entryFor("VoiceClip",e?.VoiceClip)}}const renderPart=(e,t,s,a,i,r)=>{const n=e[t.kind];return(0,_react.createElement)(n,{key:r,part:t,message:s,turn:a,pickAttachments:i})},sameMessage=(e,t)=>e.signature===t.signature&&e.message.id===t.message.id&&e.message.rating===t.message.rating&&e.message.stored===t.message.stored&&e.groupPosition===t.groupPosition&&e.isLast===t.isLast&&e.handoffSettled===t.handoffSettled&&e.isLastVisitor===t.isLastVisitor&&e.thinking===t.thinking&&e.streaming===t.streaming&&e.pickAttachments===t.pickAttachments&&e.labels===t.labels&&e.all===t.all&&e.layoutParts===t.layoutParts&&e.chat.session===t.chat.session&&e.chat.features===t.chat.features&&e.chat.onNavigate===t.chat.onNavigate&&e.onNavigate===t.onNavigate&&e.onOpenAttachment===t.onOpenAttachment&&e.onOpenTarget===t.onOpenTarget&&e.openTicket===t.openTicket&&e.resolveLink===t.resolveLink&&e.focusComposer===t.focusComposer&&e.byline===t.byline&&e.avatar===t.avatar&&e.replyAvatars===t.replyAvatars&&e.avatarStart===t.avatarStart&&e.expressive===t.expressive&&e.reduced===t.reduced&&e.handoffHosts===t.handoffHosts;function ThreadMessageView({chat:e,message:t,labels:s,all:a,layoutParts:i,groupPosition:r,isLast:n,isLastVisitor:o,pickAttachments:l,onOpenAttachment:c,onOpenTarget:d,onNavigate:u,openTicket:m,handoffHosts:g,resolveLink:p,focusComposer:h,byline:y,avatar:_,replyAvatars:x,avatarStart:f,expressive:v,reduced:j,sourceRows:R,testID:T}){const k=(0,_mobileUi.useTheme)(),b=Object.assign((0,_chatCore.createTurnApi)(e.session,{messageId:t.id,labels:s,openAttachment:c,openTarget:d??_chatShared.defaultOpenTarget,onNavigate:u??e.onNavigate,openTicket:m,handoffHosts:g,openCitation:e=>{return t=String(e),void(0,_chatShared.focusForAccessibility)(R.current.get(t));var t}}),{resolveLink:p,focusComposer:h,registerSourceRow:(e,t)=>{t?R.current.set(e,t):R.current.delete(e)}});let A=0;const nextSlot=()=>(A+=1,A),N=(0,_jsxRuntime.jsx)(_ChatMessageActions.ChatMessageActions,{chat:e,message:t,labels:s,isLast:n,isLastVisitor:o,testID:`${T}-actions`});if(MINE.has(t.author.kind)){const e=t.parts.filter(e=>"File"===e.kind||"VoiceClip"===e.kind),s=(0,_jsxRuntime.jsxs)(_reactNative.View,{style:{gap:k.space.xs},children:[e.map(e=>renderPart(a,e,t,b,l,`${t.id}-file-${nextSlot()}`)),N]});return(0,_jsxRuntime.jsx)(_mobileUi.MessageBubble,{side:"mine",text:joinedText(t.parts),labels:{you:""},groupPosition:r,footer:s,testID:T})}const w="Agent"===t.author.kind,C=t.author.displayName||s.agent,M=(i?i(t):t.parts).map(e=>Array.isArray(e)?(0,_jsxRuntime.jsx)(_reactNative.View,{style:{flexDirection:"row",flexWrap:"wrap",gap:k.space.xs},children:e.map(e=>renderPart(a,e,t,b,l,`${t.id}-part-${nextSlot()}`))},`${t.id}-group-${nextSlot()}`):renderPart(a,e,t,b,l,`${t.id}-item-${nextSlot()}`)),S=w&&y?(0,_jsxRuntime.jsx)(_AgentByline.AgentByline,{author:t.author,labels:s,showAvatar:x},`${t.id}-byline`):null,I=S?[S,...M]:M,P=(0,_chatCore.messagePlainText)(t.parts),V=(0,_jsxRuntime.jsx)(_mobileUi.MessageBubble,{side:"theirs",...w?{}:{author:`${t.author.displayName||(e.profile?.assistant.name??s.assistant)}:`},text:w?`${s.agentSays(C)} ${P}`:P,streaming:t.streaming,groupPosition:r,footer:N,testID:T,children:I});if(!x||!_||"Assistant"!==t.author.kind)return V;const F=t.author.displayName||(e.profile?.assistant.name??s.assistant);return(0,_jsxRuntime.jsxs)(_reactNative.View,{style:{flexDirection:"row",alignItems:"flex-end",gap:k.space.xs},children:[(0,_jsxRuntime.jsx)(_reactNative.View,{accessibilityElementsHidden:!0,importantForAccessibility:"no-hide-descendants",style:f?void 0:{opacity:0},children:(0,_jsxRuntime.jsx)(_ReplyRing.ReplyRing,{active:v&&t.streaming&&f,reduced:j,children:(0,_jsxRuntime.jsx)(_mobileUi.Avatar,{size:"xs",name:F,...f?{source:{uri:_}}:{}})})}),(0,_jsxRuntime.jsx)(_reactNative.View,{style:{flex:1,minWidth:0},children:V})]})}const ThreadMessage=(0,_react.memo)(ThreadMessageView,sameMessage),TYPING_ORB_MARK=14,TYPING_ASSISTANT_MARK=8,TypingMark=({size:e})=>{const t=(0,_mobileUi.useTheme)();return(0,_jsxRuntime.jsx)(_reactNative.View,{style:{width:e,height:e,borderRadius:t.radius.pill,backgroundColor:t.colors.primary}})},TYPING_DOT=6,TypingDots=()=>{const e=(0,_mobileUi.useTheme)();return(0,_jsxRuntime.jsx)(_reactNative.View,{style:{flexDirection:"row",alignItems:"center",gap:e.space.xxs},children:[0,1,2].map(t=>(0,_jsxRuntime.jsx)(_reactNative.View,{style:{width:6,height:6,borderRadius:3,backgroundColor:e.colors["text-muted"]}},t))})},TurnGap=()=>{const e=(0,_mobileUi.useTheme)();return(0,_jsxRuntime.jsx)(_reactNative.View,{style:{height:e.space.lg}})},followNew=()=>!0;function ChatThread({chat:e,labels:t,renderers:s,layoutParts:a,loader:i="Steps",motion:r="On",avatar:n,replyAvatars:o=!0,expressive:l=!1,pickAttachments:c,onOpenAttachment:d,onOpenTarget:u,onNavigate:m,openTicket:g,handoffHosts:p,resolveLink:h,focusComposer:y,keyboardAware:_,keyboardOffset:x,empty:f,style:v,testID:j="chat-thread"}){const R=(0,_mobileUi.useTheme)(),T=(0,_mobileUi.useReducedMotion)(),k="Reduced"===r||T,b=(0,_react.useMemo)(()=>resolveRenderers(s),[s]),A=e.messages[0],N=e.greeting??"",w=(0,_react.useMemo)(()=>""!==N&&void 0!==A&&MINE.has(A.author.kind)?{...A,id:"greeting",author:{...A.author,kind:"Assistant",displayName:null,avatarUrl:null},parts:[{kind:"Text",text:N}],stored:!1,streaming:!1,restored:!1,rating:null}:null,[N,A]),C=(0,_react.useMemo)(()=>{const t=e.messages.filter(e=>!(e.streaming&&0===e.parts.length)).reverse();return w?[...t,w]:t},[e.messages,w]),M=e.messages[e.messages.length-1]?.id,S=(0,_react.useRef)(new Map),I=(0,_react.useRef)(null);(0,_react.useEffect)(()=>{if(null!==I.current)for(const s of e.messages){if(MINE.has(s.author.kind)||s.streaming||s.restored||I.current.has(s.id))continue;if("Agent"===s.author.kind&&!s.stored)continue;I.current.add(s.id);const e=(0,_chatCore.messagePlainText)(s.parts);""!==e&&_reactNative.AccessibilityInfo.announceForAccessibility("Agent"===s.author.kind?`${t.agentSays(s.author.displayName||t.agent)} ${e}`:e)}else I.current=new Set(e.messages.map(e=>e.id))},[e.messages,t]);const P=(0,_react.useMemo)(()=>{for(let t=e.messages.length-1;t>=0;t-=1){const s=e.messages[t];if(s?.stored&&"Visitor"===s.author.kind)return s.id}},[e.messages]),V=(0,_react.useMemo)(()=>{const t=new Set;return e.messages.forEach((s,a)=>{if("Agent"!==s.author.kind)return;const i=e.messages[a-1];"Agent"===i?.author.kind&&(i.author.displayName||"")===(s.author.displayName||"")||t.add(s.id)}),t},[e.messages]),F=(0,_react.useMemo)(()=>{const t=new Set,s=e.messages.filter(e=>!(e.streaming&&0===e.parts.length));return w&&t.add(w.id),s.forEach((e,a)=>{"Assistant"===e.author.kind&&"Assistant"!==s[a-1]?.author.kind&&t.add(e.id)}),t},[e.messages,w]),L=(0,_react.useMemo)(()=>(0,_chatCore.handoffSettledIds)(e.messages),[e.messages]),$=e.turnError,O="daily_limit"===$?.code,D=null!==$&&/feature_off$/.test($.code),E=O&&$?.resetsDateTimeUtc?` ${t.dailyLimitRefresh((0,_chatCore.formatChatDateTime)($.resetsDateTimeUtc))}`:"",U=e.streaming||null===$&&!e.stopped?null:(0,_jsxRuntime.jsxs)(_reactNative.View,{style:{gap:R.space.xs},testID:`${j}-error`,children:[null!==$?(0,_jsxRuntime.jsx)(_mobileUi.Text,{variant:"bodySmall",color:"danger",accessibilityRole:O?void 0:"alert",accessibilityLiveRegion:O?"polite":void 0,children:`${$.message}${E}`}):(0,_jsxRuntime.jsx)(_mobileUi.Text,{variant:"bodySmall",color:"danger",accessibilityLiveRegion:"polite",children:t.stopped}),!e.features.Retry||O||D?null:(0,_jsxRuntime.jsx)(_reactNative.View,{style:{flexDirection:"row"},children:(0,_jsxRuntime.jsx)(_mobileUi.Button,{label:t.retry,variant:"link",size:"sm",onPress:()=>{e.retry()},testID:`${j}-retry`})})]}),q=e.thinking&&null!==e.profile?(0,_jsxRuntime.jsx)(_ChatLoader.ChatLoader,{style:i,steps:e.steps,phrases:e.profile.experience.pacing.thinkingPhrases,intervalMs:e.profile.experience.pacing.phraseIntervalMs,announce:t.preparingAnswer(e.profile.assistant.name),motion:r}):null,H=!0===e.pending?.typing?(0,_jsxRuntime.jsx)(_TypingPill.TypingPill,{motion:r,mark:(0,_jsxRuntime.jsx)(_ReplyRing.ReplyRing,{active:l,reduced:k,children:n?(0,_jsxRuntime.jsx)(_mobileUi.Avatar,{size:"xs",name:e.profile?.assistant.name??t.panelTitle,source:{uri:n}}):(0,_jsxRuntime.jsx)(TypingMark,{size:"Orb"===i?TYPING_ORB_MARK:TYPING_ASSISTANT_MARK})})}):null,K=null!==e.agentTyping&&"WithAgent"===e.conversationState?(0,_jsxRuntime.jsxs)(_reactNative.View,{style:{gap:R.space.xs,alignSelf:"flex-start"},children:[(0,_jsxRuntime.jsx)(_TypingPill.TypingPill,{motion:r,mark:(0,_jsxRuntime.jsx)(TypingDots,{})}),(0,_jsxRuntime.jsx)(_mobileUi.Text,{variant:"caption",color:"secondary",accessibilityLiveRegion:"polite",children:t.agentTyping(e.agentTyping.displayName||t.agent)})]}):null,B=H||q||U||K?(0,_jsxRuntime.jsxs)(_reactNative.View,{style:{gap:R.space.sm},children:[H,q,K,U]}):void 0,G=(0,_jsxRuntime.jsx)(_mobileUi.MessageList,{messages:C,keyExtractor:e=>e.id,renderMessage:({item:s,groupPosition:i})=>(0,_jsxRuntime.jsxs)(_reactNative.View,{role:"listitem",children:[(0,_jsxRuntime.jsx)(_mobileUi.Text,{style:visuallyHidden,children:`${spokenAuthor(s,t,e.profile?.assistant.name)}: `}),(0,_jsxRuntime.jsx)(_ChatEntrance.ChatEntrance,{animate:!s.restored,children:(0,_jsxRuntime.jsx)(ThreadMessage,{chat:e,message:s,signature:(0,_chatShared.messageSignature)(s),labels:t,all:b,layoutParts:a,groupPosition:i,isLast:s.id===M,isLastVisitor:s.id===P,handoffSettled:L.has(s.id),thinking:e.thinking,streaming:e.streaming,pickAttachments:c,onOpenAttachment:d,onOpenTarget:u,onNavigate:m,openTicket:g,handoffHosts:p,resolveLink:h,focusComposer:y,byline:V.has(s.id),avatar:n,replyAvatars:o,avatarStart:F.has(s.id),expressive:l,reduced:k,sourceRows:S,testID:`${j}-${s.id}`})})]}),getAuthorKey:e=>e.author.kind,getDate:e=>e.createdDateTimeUtc,followNew:followNew,typing:B,empty:f,ItemSeparatorComponent:TurnGap,...void 0===_?{}:{keyboardAware:_},...void 0===x?{}:{keyboardOffset:x},..."web"===_reactNative.Platform.OS?{}:{accessibilityLabel:t.thread},accessibilityRole:"list",style:v,testID:j});return"web"!==_reactNative.Platform.OS?G:(0,_jsxRuntime.jsx)(_reactNative.View,{role:"log","aria-live":"polite","aria-label":t.thread,style:{flex:1},children:G})}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ChatThread=ChatThread;var _chatCore=require("@webority/chat-core"),_mobileUi=require("@webority/mobile-ui"),_react=require("react"),_reactNative=require("react-native"),_AgentByline=require("./AgentByline.js"),_ChatEntrance=require("./ChatEntrance.js"),_ChatMessageActions=require("./ChatMessageActions.js"),_chatShared=require("./chatShared.js"),_ChatLoader=require("./loaders/ChatLoader.js"),_ReplyRing=require("./loaders/ReplyRing.js"),_TypingPill=require("./loaders/TypingPill.js"),_index=require("./renderers/index.js"),_jsxRuntime=require("react/jsx-runtime");const MINE=new Set(["Visitor","Customer"]),visuallyHidden={position:"absolute",left:-1e4,width:1,height:1,overflow:"hidden"},spokenAuthor=(e,t,s)=>MINE.has(e.author.kind)?t.you:e.author.displayName?e.author.displayName:"Agent"===e.author.kind?t.agent:s??t.assistant,joinedText=e=>e.filter(e=>"Text"===e.kind).map(e=>e.text).join("\n\n");function isKind(e,t){return e.kind===t}const kindWrappers=new WeakMap;function forKind(e,t){const s=kindWrappers.get(t)??new Map,a=s.get(e);if(a)return a;function KindEntry(s){if(!isKind(s.part,e))throw new Error(`renderer for ${e} received a ${s.part.kind} part`);return(0,_react.createElement)(t,{part:s.part,message:s.message,turn:s.turn,pickAttachments:s.pickAttachments})}return s.set(e,KindEntry),kindWrappers.set(t,s),KindEntry}const defaults=_index.defaultRenderers;function entryFor(e,t){return forKind(e,t??defaults[e])}function resolveRenderers(e){return{Text:entryFor("Text",e?.Text),Sources:entryFor("Sources",e?.Sources),QuickReplies:entryFor("QuickReplies",e?.QuickReplies),InputRequest:entryFor("InputRequest",e?.InputRequest),Action:entryFor("Action",e?.Action),Notice:entryFor("Notice",e?.Notice),Table:entryFor("Table",e?.Table),Chart:entryFor("Chart",e?.Chart),Figures:entryFor("Figures",e?.Figures),RecordCard:entryFor("RecordCard",e?.RecordCard),Image:entryFor("Image",e?.Image),File:entryFor("File",e?.File),Ticket:entryFor("Ticket",e?.Ticket),Handoff:entryFor("Handoff",e?.Handoff),VoiceClip:entryFor("VoiceClip",e?.VoiceClip)}}const renderPart=(e,t,s,a,i,r)=>{const n=e[t.kind];return(0,_react.createElement)(n,{key:r,part:t,message:s,turn:a,pickAttachments:i})},sameMessage=(e,t)=>e.signature===t.signature&&e.message.id===t.message.id&&e.message.rating===t.message.rating&&e.message.stored===t.message.stored&&e.groupPosition===t.groupPosition&&e.isLast===t.isLast&&e.handoffSettled===t.handoffSettled&&e.isLastVisitor===t.isLastVisitor&&e.thinking===t.thinking&&e.streaming===t.streaming&&e.pickAttachments===t.pickAttachments&&e.labels===t.labels&&e.all===t.all&&e.layoutParts===t.layoutParts&&e.chat.session===t.chat.session&&e.chat.features===t.chat.features&&e.chat.onNavigate===t.chat.onNavigate&&e.onNavigate===t.onNavigate&&e.onOpenAttachment===t.onOpenAttachment&&e.onOpenTarget===t.onOpenTarget&&e.openTicket===t.openTicket&&e.resolveLink===t.resolveLink&&e.focusComposer===t.focusComposer&&e.byline===t.byline&&e.avatar===t.avatar&&e.replyAvatars===t.replyAvatars&&e.avatarStart===t.avatarStart&&e.expressive===t.expressive&&e.reduced===t.reduced&&e.handoffHosts===t.handoffHosts;function ThreadMessageView({chat:e,message:t,labels:s,all:a,layoutParts:i,groupPosition:r,isLast:n,isLastVisitor:o,pickAttachments:l,onOpenAttachment:c,onOpenTarget:d,onNavigate:u,openTicket:m,handoffHosts:g,resolveLink:p,focusComposer:h,byline:y,avatar:_,replyAvatars:x,avatarStart:f,expressive:v,reduced:j,sourceRows:R,testID:T}){const k=(0,_mobileUi.useTheme)(),b=Object.assign((0,_chatCore.createTurnApi)(e.session,{messageId:t.id,labels:s,openAttachment:c,openTarget:d??_chatShared.defaultOpenTarget,onNavigate:u??e.onNavigate,openTicket:m,handoffHosts:g,openCitation:e=>{return t=String(e),void(0,_chatShared.focusForAccessibility)(R.current.get(t));var t}}),{resolveLink:p,focusComposer:h,registerSourceRow:(e,t)=>{t?R.current.set(e,t):R.current.delete(e)}});let A=0;const nextSlot=()=>(A+=1,A),N=(0,_jsxRuntime.jsx)(_ChatMessageActions.ChatMessageActions,{chat:e,message:t,labels:s,isLast:n,isLastVisitor:o,testID:`${T}-actions`});if(MINE.has(t.author.kind)){const e=t.parts.filter(e=>"File"===e.kind||"VoiceClip"===e.kind),s=(0,_jsxRuntime.jsxs)(_reactNative.View,{style:{gap:k.space.xs},children:[e.map(e=>renderPart(a,e,t,b,l,`${t.id}-file-${nextSlot()}`)),N]});return(0,_jsxRuntime.jsx)(_mobileUi.MessageBubble,{side:"mine",text:joinedText(t.parts),labels:{you:""},groupPosition:r,footer:s,testID:T})}const w="Agent"===t.author.kind,C=t.author.displayName||s.agent,M=(i?i(t):t.parts).map(e=>Array.isArray(e)?(0,_jsxRuntime.jsx)(_reactNative.View,{style:{flexDirection:"row",flexWrap:"wrap",gap:k.space.xs},children:e.map(e=>renderPart(a,e,t,b,l,`${t.id}-part-${nextSlot()}`))},`${t.id}-group-${nextSlot()}`):renderPart(a,e,t,b,l,`${t.id}-item-${nextSlot()}`)),S=w&&y?(0,_jsxRuntime.jsx)(_AgentByline.AgentByline,{author:t.author,labels:s,showAvatar:x},`${t.id}-byline`):null,I=S?[S,...M]:M,P=(0,_chatCore.messagePlainText)(t.parts),V=(0,_jsxRuntime.jsx)(_mobileUi.MessageBubble,{side:"theirs",...w?{}:{author:`${t.author.displayName||(e.profile?.assistant.name??s.assistant)}:`},text:w?`${s.agentSays(C)} ${P}`:P,streaming:t.streaming,groupPosition:r,footer:N,testID:T,children:I});if(!x||!_||"Assistant"!==t.author.kind)return V;const F=t.author.displayName||(e.profile?.assistant.name??s.assistant);return(0,_jsxRuntime.jsxs)(_reactNative.View,{style:{flexDirection:"row",alignItems:"flex-end",gap:k.space.xs},children:[(0,_jsxRuntime.jsx)(_reactNative.View,{accessibilityElementsHidden:!0,importantForAccessibility:"no-hide-descendants",style:f?void 0:{opacity:0},children:(0,_jsxRuntime.jsx)(_ReplyRing.ReplyRing,{active:v&&t.streaming&&f,reduced:j,children:(0,_jsxRuntime.jsx)(_mobileUi.Avatar,{size:"xs",name:F,...f?{source:{uri:_}}:{}})})}),(0,_jsxRuntime.jsx)(_reactNative.View,{style:{flex:1,minWidth:0},children:V})]})}const ThreadMessage=(0,_react.memo)(ThreadMessageView,sameMessage),TYPING_ORB_MARK=14,TYPING_ASSISTANT_MARK=8,TypingMark=({size:e})=>{const t=(0,_mobileUi.useTheme)();return(0,_jsxRuntime.jsx)(_reactNative.View,{style:{width:e,height:e,borderRadius:t.radius.pill,backgroundColor:t.colors.primary}})},TYPING_DOT=6,TypingDots=()=>{const e=(0,_mobileUi.useTheme)();return(0,_jsxRuntime.jsx)(_reactNative.View,{style:{flexDirection:"row",alignItems:"center",gap:e.space.xxs},children:[0,1,2].map(t=>(0,_jsxRuntime.jsx)(_reactNative.View,{style:{width:6,height:6,borderRadius:3,backgroundColor:e.colors["text-muted"]}},t))})},TurnGap=()=>{const e=(0,_mobileUi.useTheme)();return(0,_jsxRuntime.jsx)(_reactNative.View,{style:{height:e.space.lg}})},followNew=()=>!0;function ChatThread({chat:e,labels:t,renderers:s,layoutParts:a,loader:i="Steps",motion:r="On",avatar:n,replyAvatars:o="First",expressive:l=!1,pickAttachments:c,onOpenAttachment:d,onOpenTarget:u,onNavigate:m,openTicket:g,handoffHosts:p,resolveLink:h,focusComposer:y,keyboardAware:_,keyboardOffset:x,empty:f,style:v,testID:j="chat-thread"}){const R=(0,_mobileUi.useTheme)(),T=(0,_mobileUi.useReducedMotion)(),k="Reduced"===r||T,b=(0,_react.useMemo)(()=>resolveRenderers(s),[s]),A=e.messages[0],N=e.greeting??"",w=(0,_react.useMemo)(()=>""!==N&&void 0!==A&&MINE.has(A.author.kind)?{...A,id:"greeting",author:{...A.author,kind:"Assistant",displayName:null,avatarUrl:null},parts:[{kind:"Text",text:N}],stored:!1,streaming:!1,restored:!1,rating:null}:null,[N,A]),C=(0,_react.useMemo)(()=>{const t=e.messages.filter(e=>!(e.streaming&&0===e.parts.length)).reverse();return w?[...t,w]:t},[e.messages,w]),M=e.messages[e.messages.length-1]?.id,S=(0,_react.useRef)(new Map),I=(0,_react.useRef)(null);(0,_react.useEffect)(()=>{if(null!==I.current)for(const s of e.messages){if(MINE.has(s.author.kind)||s.streaming||s.restored||I.current.has(s.id))continue;if("Agent"===s.author.kind&&!s.stored)continue;I.current.add(s.id);const e=(0,_chatCore.messagePlainText)(s.parts);""!==e&&_reactNative.AccessibilityInfo.announceForAccessibility("Agent"===s.author.kind?`${t.agentSays(s.author.displayName||t.agent)} ${e}`:e)}else I.current=new Set(e.messages.map(e=>e.id))},[e.messages,t]);const P=(0,_react.useMemo)(()=>{for(let t=e.messages.length-1;t>=0;t-=1){const s=e.messages[t];if(s?.stored&&"Visitor"===s.author.kind)return s.id}},[e.messages]),V=(0,_react.useMemo)(()=>{const t=new Set;return e.messages.forEach((s,a)=>{if("Agent"!==s.author.kind)return;const i=e.messages[a-1];"Agent"===i?.author.kind&&(i.author.displayName||"")===(s.author.displayName||"")||t.add(s.id)}),t},[e.messages]),F=(0,_react.useMemo)(()=>{const t=new Set,s=e.messages.filter(e=>!(e.streaming&&0===e.parts.length));return w&&t.add(w.id),s.forEach((e,a)=>{"Assistant"===e.author.kind&&"Assistant"!==s[a-1]?.author.kind&&t.add(e.id)}),t},[e.messages,w]),L=(0,_react.useMemo)(()=>(0,_chatCore.handoffSettledIds)(e.messages),[e.messages]),$=e.turnError,E="daily_limit"===$?.code,O=null!==$&&/feature_off$/.test($.code),D=E&&$?.resetsDateTimeUtc?` ${t.dailyLimitRefresh((0,_chatCore.formatChatDateTime)($.resetsDateTimeUtc))}`:"",U=e.streaming||null===$&&!e.stopped?null:(0,_jsxRuntime.jsxs)(_reactNative.View,{style:{gap:R.space.xs},testID:`${j}-error`,children:[null!==$?(0,_jsxRuntime.jsx)(_mobileUi.Text,{variant:"bodySmall",color:"danger",accessibilityRole:E?void 0:"alert",accessibilityLiveRegion:E?"polite":void 0,children:`${$.message}${D}`}):(0,_jsxRuntime.jsx)(_mobileUi.Text,{variant:"bodySmall",color:"danger",accessibilityLiveRegion:"polite",children:t.stopped}),!e.features.Retry||E||O?null:(0,_jsxRuntime.jsx)(_reactNative.View,{style:{flexDirection:"row"},children:(0,_jsxRuntime.jsx)(_mobileUi.Button,{label:t.retry,variant:"link",size:"sm",onPress:()=>{e.retry()},testID:`${j}-retry`})})]}),q=e.thinking&&null!==e.profile?(0,_jsxRuntime.jsx)(_ChatLoader.ChatLoader,{style:i,steps:e.steps,phrases:e.profile.experience.pacing.thinkingPhrases,intervalMs:e.profile.experience.pacing.phraseIntervalMs,announce:t.preparingAnswer(e.profile.assistant.name),motion:r}):null,H=!0===e.pending?.typing?(0,_jsxRuntime.jsx)(_TypingPill.TypingPill,{motion:r,mark:(0,_jsxRuntime.jsx)(_ReplyRing.ReplyRing,{active:l,reduced:k,children:n?(0,_jsxRuntime.jsx)(_mobileUi.Avatar,{size:"xs",name:e.profile?.assistant.name??t.panelTitle,source:{uri:n}}):(0,_jsxRuntime.jsx)(TypingMark,{size:"Orb"===i?TYPING_ORB_MARK:TYPING_ASSISTANT_MARK})})}):null,K=null!==e.agentTyping&&"WithAgent"===e.conversationState?(0,_jsxRuntime.jsxs)(_reactNative.View,{style:{gap:R.space.xs,alignSelf:"flex-start"},children:[(0,_jsxRuntime.jsx)(_TypingPill.TypingPill,{motion:r,mark:(0,_jsxRuntime.jsx)(TypingDots,{})}),(0,_jsxRuntime.jsx)(_mobileUi.Text,{variant:"caption",color:"secondary",accessibilityLiveRegion:"polite",children:t.agentTyping(e.agentTyping.displayName||t.agent)})]}):null,B=H||q||U||K?(0,_jsxRuntime.jsxs)(_reactNative.View,{style:{gap:R.space.sm},children:[H,q,K,U]}):void 0,G=(0,_jsxRuntime.jsx)(_mobileUi.MessageList,{messages:C,keyExtractor:e=>e.id,renderMessage:({item:s,groupPosition:i})=>(0,_jsxRuntime.jsxs)(_reactNative.View,{role:"listitem",children:[(0,_jsxRuntime.jsx)(_mobileUi.Text,{style:visuallyHidden,children:`${spokenAuthor(s,t,e.profile?.assistant.name)}: `}),(0,_jsxRuntime.jsx)(_ChatEntrance.ChatEntrance,{animate:!s.restored,children:(0,_jsxRuntime.jsx)(ThreadMessage,{chat:e,message:s,signature:(0,_chatShared.messageSignature)(s),labels:t,all:b,layoutParts:a,groupPosition:i,isLast:s.id===M,isLastVisitor:s.id===P,handoffSettled:L.has(s.id),thinking:e.thinking,streaming:e.streaming,pickAttachments:c,onOpenAttachment:d,onOpenTarget:u,onNavigate:m,openTicket:g,handoffHosts:p,resolveLink:h,focusComposer:y,byline:V.has(s.id),avatar:n,replyAvatars:"None"!==o,avatarStart:"Every"===o||F.has(s.id),expressive:l,reduced:k,sourceRows:S,testID:`${j}-${s.id}`})})]}),getAuthorKey:e=>e.author.kind,getDate:e=>e.createdDateTimeUtc,followNew:followNew,typing:B,empty:f,ItemSeparatorComponent:TurnGap,...void 0===_?{}:{keyboardAware:_},...void 0===x?{}:{keyboardOffset:x},..."web"===_reactNative.Platform.OS?{}:{accessibilityLabel:t.thread},accessibilityRole:"list",style:v,testID:j});return"web"!==_reactNative.Platform.OS?G:(0,_jsxRuntime.jsx)(_reactNative.View,{role:"log","aria-live":"polite","aria-label":t.thread,style:{flex:1},children:G})}
|
package/lib/module/ChatThread.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";import{createTurnApi,formatChatDateTime,handoffSettledIds,messagePlainText}from"@webority/chat-core";import{Avatar,Button,MessageBubble,MessageList,Text,useReducedMotion,useTheme}from"@webority/mobile-ui";import{createElement,memo,useEffect,useMemo,useRef}from"react";import{AccessibilityInfo,Platform,View}from"react-native";import{AgentByline}from"./AgentByline.js";import{ChatEntrance}from"./ChatEntrance.js";import{ChatMessageActions}from"./ChatMessageActions.js";import{defaultOpenTarget,focusForAccessibility,messageSignature}from"./chatShared.js";import{ChatLoader}from"./loaders/ChatLoader.js";import{ReplyRing}from"./loaders/ReplyRing.js";import{TypingPill}from"./loaders/TypingPill.js";import{defaultRenderers}from"./renderers/index.js";import{jsx as _jsx,jsxs as _jsxs}from"react/jsx-runtime";const MINE=new Set(["Visitor","Customer"]),visuallyHidden={position:"absolute",left:-1e4,width:1,height:1,overflow:"hidden"},spokenAuthor=(e,t,s)=>MINE.has(e.author.kind)?t.you:e.author.displayName?e.author.displayName:"Agent"===e.author.kind?t.agent:s??t.assistant,joinedText=e=>e.filter(e=>"Text"===e.kind).map(e=>e.text).join("\n\n");function isKind(e,t){return e.kind===t}const kindWrappers=new WeakMap;function forKind(e,t){const s=kindWrappers.get(t)??new Map,a=s.get(e);if(a)return a;function KindEntry(s){if(!isKind(s.part,e))throw new Error(`renderer for ${e} received a ${s.part.kind} part`);return createElement(t,{part:s.part,message:s.message,turn:s.turn,pickAttachments:s.pickAttachments})}return s.set(e,KindEntry),kindWrappers.set(t,s),KindEntry}const defaults=defaultRenderers;function entryFor(e,t){return forKind(e,t??defaults[e])}function resolveRenderers(e){return{Text:entryFor("Text",e?.Text),Sources:entryFor("Sources",e?.Sources),QuickReplies:entryFor("QuickReplies",e?.QuickReplies),InputRequest:entryFor("InputRequest",e?.InputRequest),Action:entryFor("Action",e?.Action),Notice:entryFor("Notice",e?.Notice),Table:entryFor("Table",e?.Table),Chart:entryFor("Chart",e?.Chart),Figures:entryFor("Figures",e?.Figures),RecordCard:entryFor("RecordCard",e?.RecordCard),Image:entryFor("Image",e?.Image),File:entryFor("File",e?.File),Ticket:entryFor("Ticket",e?.Ticket),Handoff:entryFor("Handoff",e?.Handoff),VoiceClip:entryFor("VoiceClip",e?.VoiceClip)}}const renderPart=(e,t,s,a,r,i)=>{const n=e[t.kind];return createElement(n,{key:i,part:t,message:s,turn:a,pickAttachments:r})},sameMessage=(e,t)=>e.signature===t.signature&&e.message.id===t.message.id&&e.message.rating===t.message.rating&&e.message.stored===t.message.stored&&e.groupPosition===t.groupPosition&&e.isLast===t.isLast&&e.handoffSettled===t.handoffSettled&&e.isLastVisitor===t.isLastVisitor&&e.thinking===t.thinking&&e.streaming===t.streaming&&e.pickAttachments===t.pickAttachments&&e.labels===t.labels&&e.all===t.all&&e.layoutParts===t.layoutParts&&e.chat.session===t.chat.session&&e.chat.features===t.chat.features&&e.chat.onNavigate===t.chat.onNavigate&&e.onNavigate===t.onNavigate&&e.onOpenAttachment===t.onOpenAttachment&&e.onOpenTarget===t.onOpenTarget&&e.openTicket===t.openTicket&&e.resolveLink===t.resolveLink&&e.focusComposer===t.focusComposer&&e.byline===t.byline&&e.avatar===t.avatar&&e.replyAvatars===t.replyAvatars&&e.avatarStart===t.avatarStart&&e.expressive===t.expressive&&e.reduced===t.reduced&&e.handoffHosts===t.handoffHosts;function ThreadMessageView({chat:e,message:t,labels:s,all:a,layoutParts:r,groupPosition:i,isLast:n,isLastVisitor:o,pickAttachments:l,onOpenAttachment:d,onOpenTarget:c,onNavigate:u,openTicket:p,handoffHosts:g,resolveLink:m,focusComposer:h,byline:f,avatar:y,replyAvatars:x,avatarStart:k,expressive:T,reduced:v,sourceRows:A,testID:b}){const w=useTheme(),j=Object.assign(createTurnApi(e.session,{messageId:t.id,labels:s,openAttachment:d,openTarget:c??defaultOpenTarget,onNavigate:u??e.onNavigate,openTicket:p,handoffHosts:g,openCitation:e=>{return t=String(e),void focusForAccessibility(A.current.get(t));var t}}),{resolveLink:m,focusComposer:h,registerSourceRow:(e,t)=>{t?A.current.set(e,t):A.current.delete(e)}});let _=0;const nextSlot=()=>(_+=1,_),R=_jsx(ChatMessageActions,{chat:e,message:t,labels:s,isLast:n,isLastVisitor:o,testID:`${b}-actions`});if(MINE.has(t.author.kind)){const e=t.parts.filter(e=>"File"===e.kind||"VoiceClip"===e.kind),s=_jsxs(View,{style:{gap:w.space.xs},children:[e.map(e=>renderPart(a,e,t,j,l,`${t.id}-file-${nextSlot()}`)),R]});return _jsx(MessageBubble,{side:"mine",text:joinedText(t.parts),labels:{you:""},groupPosition:i,footer:s,testID:b})}const M="Agent"===t.author.kind,N=t.author.displayName||s.agent,S=(r?r(t):t.parts).map(e=>Array.isArray(e)?_jsx(View,{style:{flexDirection:"row",flexWrap:"wrap",gap:w.space.xs},children:e.map(e=>renderPart(a,e,t,j,l,`${t.id}-part-${nextSlot()}`))},`${t.id}-group-${nextSlot()}`):renderPart(a,e,t,j,l,`${t.id}-item-${nextSlot()}`)),C=M&&f?_jsx(AgentByline,{author:t.author,labels:s,showAvatar:x},`${t.id}-byline`):null,I=C?[C,...S]:S,P=messagePlainText(t.parts),V=_jsx(MessageBubble,{side:"theirs",...M?{}:{author:`${t.author.displayName||(e.profile?.assistant.name??s.assistant)}:`},text:M?`${s.agentSays(N)} ${P}`:P,streaming:t.streaming,groupPosition:i,footer:R,testID:b,children:I});if(!x||!y||"Assistant"!==t.author.kind)return V;const F=t.author.displayName||(e.profile?.assistant.name??s.assistant);return _jsxs(View,{style:{flexDirection:"row",alignItems:"flex-end",gap:w.space.xs},children:[_jsx(View,{accessibilityElementsHidden:!0,importantForAccessibility:"no-hide-descendants",style:k?void 0:{opacity:0},children:_jsx(ReplyRing,{active:T&&t.streaming&&k,reduced:v,children:_jsx(Avatar,{size:"xs",name:F,...k?{source:{uri:y}}:{}})})}),_jsx(View,{style:{flex:1,minWidth:0},children:V})]})}const ThreadMessage=memo(ThreadMessageView,sameMessage),TYPING_ORB_MARK=14,TYPING_ASSISTANT_MARK=8,TypingMark=({size:e})=>{const t=useTheme();return _jsx(View,{style:{width:e,height:e,borderRadius:t.radius.pill,backgroundColor:t.colors.primary}})},TYPING_DOT=6,TypingDots=()=>{const e=useTheme();return _jsx(View,{style:{flexDirection:"row",alignItems:"center",gap:e.space.xxs},children:[0,1,2].map(t=>_jsx(View,{style:{width:6,height:6,borderRadius:3,backgroundColor:e.colors["text-muted"]}},t))})},TurnGap=()=>{const e=useTheme();return _jsx(View,{style:{height:e.space.lg}})},followNew=()=>!0;export function ChatThread({chat:e,labels:t,renderers:s,layoutParts:a,loader:r="Steps",motion:i="On",avatar:n,replyAvatars:o=!0,expressive:l=!1,pickAttachments:d,onOpenAttachment:c,onOpenTarget:u,onNavigate:p,openTicket:g,handoffHosts:m,resolveLink:h,focusComposer:f,keyboardAware:y,keyboardOffset:x,empty:k,style:T,testID:v="chat-thread"}){const A=useTheme(),b=useReducedMotion(),w="Reduced"===i||b,j=useMemo(()=>resolveRenderers(s),[s]),_=e.messages[0],R=e.greeting??"",M=useMemo(()=>""!==R&&void 0!==_&&MINE.has(_.author.kind)?{..._,id:"greeting",author:{..._.author,kind:"Assistant",displayName:null,avatarUrl:null},parts:[{kind:"Text",text:R}],stored:!1,streaming:!1,restored:!1,rating:null}:null,[R,_]),N=useMemo(()=>{const t=e.messages.filter(e=>!(e.streaming&&0===e.parts.length)).reverse();return M?[...t,M]:t},[e.messages,M]),S=e.messages[e.messages.length-1]?.id,C=useRef(new Map),I=useRef(null);useEffect(()=>{if(null!==I.current)for(const s of e.messages){if(MINE.has(s.author.kind)||s.streaming||s.restored||I.current.has(s.id))continue;if("Agent"===s.author.kind&&!s.stored)continue;I.current.add(s.id);const e=messagePlainText(s.parts);""!==e&&AccessibilityInfo.announceForAccessibility("Agent"===s.author.kind?`${t.agentSays(s.author.displayName||t.agent)} ${e}`:e)}else I.current=new Set(e.messages.map(e=>e.id))},[e.messages,t]);const P=useMemo(()=>{for(let t=e.messages.length-1;t>=0;t-=1){const s=e.messages[t];if(s?.stored&&"Visitor"===s.author.kind)return s.id}},[e.messages]),V=useMemo(()=>{const t=new Set;return e.messages.forEach((s,a)=>{if("Agent"!==s.author.kind)return;const r=e.messages[a-1];"Agent"===r?.author.kind&&(r.author.displayName||"")===(s.author.displayName||"")||t.add(s.id)}),t},[e.messages]),F=useMemo(()=>{const t=new Set,s=e.messages.filter(e=>!(e.streaming&&0===e.parts.length));return M&&t.add(M.id),s.forEach((e,a)=>{"Assistant"===e.author.kind&&"Assistant"!==s[a-1]?.author.kind&&t.add(e.id)}),t},[e.messages,M]),L=useMemo(()=>handoffSettledIds(e.messages),[e.messages]),$=e.turnError,D="daily_limit"===$?.code,E=null!==$&&/feature_off$/.test($.code),O=D&&$?.resetsDateTimeUtc?` ${t.dailyLimitRefresh(formatChatDateTime($.resetsDateTimeUtc))}`:"",H=e.streaming||null===$&&!e.stopped?null:_jsxs(View,{style:{gap:A.space.xs},testID:`${v}-error`,children:[_jsx(Text,null!==$?{variant:"bodySmall",color:"danger",accessibilityRole:D?void 0:"alert",accessibilityLiveRegion:D?"polite":void 0,children:`${$.message}${O}`}:{variant:"bodySmall",color:"danger",accessibilityLiveRegion:"polite",children:t.stopped}),!e.features.Retry||D||E?null:_jsx(View,{style:{flexDirection:"row"},children:_jsx(Button,{label:t.retry,variant:"link",size:"sm",onPress:()=>{e.retry()},testID:`${v}-retry`})})]}),K=e.thinking&&null!==e.profile?_jsx(ChatLoader,{style:r,steps:e.steps,phrases:e.profile.experience.pacing.thinkingPhrases,intervalMs:e.profile.experience.pacing.phraseIntervalMs,announce:t.preparingAnswer(e.profile.assistant.name),motion:i}):null,B=!0===e.pending?.typing?_jsx(TypingPill,{motion:i,mark:_jsx(ReplyRing,{active:l,reduced:w,children:n?_jsx(Avatar,{size:"xs",name:e.profile?.assistant.name??t.panelTitle,source:{uri:n}}):_jsx(TypingMark,{size:"Orb"===r?14:8})})}):null,W=null!==e.agentTyping&&"WithAgent"===e.conversationState?_jsxs(View,{style:{gap:A.space.xs,alignSelf:"flex-start"},children:[_jsx(TypingPill,{motion:i,mark:_jsx(TypingDots,{})}),_jsx(Text,{variant:"caption",color:"secondary",accessibilityLiveRegion:"polite",children:t.agentTyping(e.agentTyping.displayName||t.agent)})]}):null,z=B||K||H||W?_jsxs(View,{style:{gap:A.space.sm},children:[B,K,W,H]}):void 0,G=_jsx(MessageList,{messages:N,keyExtractor:e=>e.id,renderMessage:({item:s,groupPosition:r})=>_jsxs(View,{role:"listitem",children:[_jsx(Text,{style:visuallyHidden,children:`${spokenAuthor(s,t,e.profile?.assistant.name)}: `}),_jsx(ChatEntrance,{animate:!s.restored,children:_jsx(ThreadMessage,{chat:e,message:s,signature:messageSignature(s),labels:t,all:j,layoutParts:a,groupPosition:r,isLast:s.id===S,isLastVisitor:s.id===P,handoffSettled:L.has(s.id),thinking:e.thinking,streaming:e.streaming,pickAttachments:d,onOpenAttachment:c,onOpenTarget:u,onNavigate:p,openTicket:g,handoffHosts:m,resolveLink:h,focusComposer:f,byline:V.has(s.id),avatar:n,replyAvatars:o,avatarStart:F.has(s.id),expressive:l,reduced:w,sourceRows:C,testID:`${v}-${s.id}`})})]}),getAuthorKey:e=>e.author.kind,getDate:e=>e.createdDateTimeUtc,followNew:followNew,typing:z,empty:k,ItemSeparatorComponent:TurnGap,...void 0===y?{}:{keyboardAware:y},...void 0===x?{}:{keyboardOffset:x},..."web"===Platform.OS?{}:{accessibilityLabel:t.thread},accessibilityRole:"list",style:T,testID:v});return"web"!==Platform.OS?G:_jsx(View,{role:"log","aria-live":"polite","aria-label":t.thread,style:{flex:1},children:G})}
|
|
1
|
+
"use strict";import{createTurnApi,formatChatDateTime,handoffSettledIds,messagePlainText}from"@webority/chat-core";import{Avatar,Button,MessageBubble,MessageList,Text,useReducedMotion,useTheme}from"@webority/mobile-ui";import{createElement,memo,useEffect,useMemo,useRef}from"react";import{AccessibilityInfo,Platform,View}from"react-native";import{AgentByline}from"./AgentByline.js";import{ChatEntrance}from"./ChatEntrance.js";import{ChatMessageActions}from"./ChatMessageActions.js";import{defaultOpenTarget,focusForAccessibility,messageSignature}from"./chatShared.js";import{ChatLoader}from"./loaders/ChatLoader.js";import{ReplyRing}from"./loaders/ReplyRing.js";import{TypingPill}from"./loaders/TypingPill.js";import{defaultRenderers}from"./renderers/index.js";import{jsx as _jsx,jsxs as _jsxs}from"react/jsx-runtime";const MINE=new Set(["Visitor","Customer"]),visuallyHidden={position:"absolute",left:-1e4,width:1,height:1,overflow:"hidden"},spokenAuthor=(e,t,s)=>MINE.has(e.author.kind)?t.you:e.author.displayName?e.author.displayName:"Agent"===e.author.kind?t.agent:s??t.assistant,joinedText=e=>e.filter(e=>"Text"===e.kind).map(e=>e.text).join("\n\n");function isKind(e,t){return e.kind===t}const kindWrappers=new WeakMap;function forKind(e,t){const s=kindWrappers.get(t)??new Map,a=s.get(e);if(a)return a;function KindEntry(s){if(!isKind(s.part,e))throw new Error(`renderer for ${e} received a ${s.part.kind} part`);return createElement(t,{part:s.part,message:s.message,turn:s.turn,pickAttachments:s.pickAttachments})}return s.set(e,KindEntry),kindWrappers.set(t,s),KindEntry}const defaults=defaultRenderers;function entryFor(e,t){return forKind(e,t??defaults[e])}function resolveRenderers(e){return{Text:entryFor("Text",e?.Text),Sources:entryFor("Sources",e?.Sources),QuickReplies:entryFor("QuickReplies",e?.QuickReplies),InputRequest:entryFor("InputRequest",e?.InputRequest),Action:entryFor("Action",e?.Action),Notice:entryFor("Notice",e?.Notice),Table:entryFor("Table",e?.Table),Chart:entryFor("Chart",e?.Chart),Figures:entryFor("Figures",e?.Figures),RecordCard:entryFor("RecordCard",e?.RecordCard),Image:entryFor("Image",e?.Image),File:entryFor("File",e?.File),Ticket:entryFor("Ticket",e?.Ticket),Handoff:entryFor("Handoff",e?.Handoff),VoiceClip:entryFor("VoiceClip",e?.VoiceClip)}}const renderPart=(e,t,s,a,r,i)=>{const n=e[t.kind];return createElement(n,{key:i,part:t,message:s,turn:a,pickAttachments:r})},sameMessage=(e,t)=>e.signature===t.signature&&e.message.id===t.message.id&&e.message.rating===t.message.rating&&e.message.stored===t.message.stored&&e.groupPosition===t.groupPosition&&e.isLast===t.isLast&&e.handoffSettled===t.handoffSettled&&e.isLastVisitor===t.isLastVisitor&&e.thinking===t.thinking&&e.streaming===t.streaming&&e.pickAttachments===t.pickAttachments&&e.labels===t.labels&&e.all===t.all&&e.layoutParts===t.layoutParts&&e.chat.session===t.chat.session&&e.chat.features===t.chat.features&&e.chat.onNavigate===t.chat.onNavigate&&e.onNavigate===t.onNavigate&&e.onOpenAttachment===t.onOpenAttachment&&e.onOpenTarget===t.onOpenTarget&&e.openTicket===t.openTicket&&e.resolveLink===t.resolveLink&&e.focusComposer===t.focusComposer&&e.byline===t.byline&&e.avatar===t.avatar&&e.replyAvatars===t.replyAvatars&&e.avatarStart===t.avatarStart&&e.expressive===t.expressive&&e.reduced===t.reduced&&e.handoffHosts===t.handoffHosts;function ThreadMessageView({chat:e,message:t,labels:s,all:a,layoutParts:r,groupPosition:i,isLast:n,isLastVisitor:o,pickAttachments:l,onOpenAttachment:d,onOpenTarget:c,onNavigate:u,openTicket:p,handoffHosts:g,resolveLink:m,focusComposer:h,byline:f,avatar:y,replyAvatars:x,avatarStart:k,expressive:T,reduced:v,sourceRows:A,testID:b}){const w=useTheme(),j=Object.assign(createTurnApi(e.session,{messageId:t.id,labels:s,openAttachment:d,openTarget:c??defaultOpenTarget,onNavigate:u??e.onNavigate,openTicket:p,handoffHosts:g,openCitation:e=>{return t=String(e),void focusForAccessibility(A.current.get(t));var t}}),{resolveLink:m,focusComposer:h,registerSourceRow:(e,t)=>{t?A.current.set(e,t):A.current.delete(e)}});let _=0;const nextSlot=()=>(_+=1,_),R=_jsx(ChatMessageActions,{chat:e,message:t,labels:s,isLast:n,isLastVisitor:o,testID:`${b}-actions`});if(MINE.has(t.author.kind)){const e=t.parts.filter(e=>"File"===e.kind||"VoiceClip"===e.kind),s=_jsxs(View,{style:{gap:w.space.xs},children:[e.map(e=>renderPart(a,e,t,j,l,`${t.id}-file-${nextSlot()}`)),R]});return _jsx(MessageBubble,{side:"mine",text:joinedText(t.parts),labels:{you:""},groupPosition:i,footer:s,testID:b})}const M="Agent"===t.author.kind,N=t.author.displayName||s.agent,S=(r?r(t):t.parts).map(e=>Array.isArray(e)?_jsx(View,{style:{flexDirection:"row",flexWrap:"wrap",gap:w.space.xs},children:e.map(e=>renderPart(a,e,t,j,l,`${t.id}-part-${nextSlot()}`))},`${t.id}-group-${nextSlot()}`):renderPart(a,e,t,j,l,`${t.id}-item-${nextSlot()}`)),C=M&&f?_jsx(AgentByline,{author:t.author,labels:s,showAvatar:x},`${t.id}-byline`):null,I=C?[C,...S]:S,P=messagePlainText(t.parts),V=_jsx(MessageBubble,{side:"theirs",...M?{}:{author:`${t.author.displayName||(e.profile?.assistant.name??s.assistant)}:`},text:M?`${s.agentSays(N)} ${P}`:P,streaming:t.streaming,groupPosition:i,footer:R,testID:b,children:I});if(!x||!y||"Assistant"!==t.author.kind)return V;const F=t.author.displayName||(e.profile?.assistant.name??s.assistant);return _jsxs(View,{style:{flexDirection:"row",alignItems:"flex-end",gap:w.space.xs},children:[_jsx(View,{accessibilityElementsHidden:!0,importantForAccessibility:"no-hide-descendants",style:k?void 0:{opacity:0},children:_jsx(ReplyRing,{active:T&&t.streaming&&k,reduced:v,children:_jsx(Avatar,{size:"xs",name:F,...k?{source:{uri:y}}:{}})})}),_jsx(View,{style:{flex:1,minWidth:0},children:V})]})}const ThreadMessage=memo(ThreadMessageView,sameMessage),TYPING_ORB_MARK=14,TYPING_ASSISTANT_MARK=8,TypingMark=({size:e})=>{const t=useTheme();return _jsx(View,{style:{width:e,height:e,borderRadius:t.radius.pill,backgroundColor:t.colors.primary}})},TYPING_DOT=6,TypingDots=()=>{const e=useTheme();return _jsx(View,{style:{flexDirection:"row",alignItems:"center",gap:e.space.xxs},children:[0,1,2].map(t=>_jsx(View,{style:{width:6,height:6,borderRadius:3,backgroundColor:e.colors["text-muted"]}},t))})},TurnGap=()=>{const e=useTheme();return _jsx(View,{style:{height:e.space.lg}})},followNew=()=>!0;export function ChatThread({chat:e,labels:t,renderers:s,layoutParts:a,loader:r="Steps",motion:i="On",avatar:n,replyAvatars:o="First",expressive:l=!1,pickAttachments:d,onOpenAttachment:c,onOpenTarget:u,onNavigate:p,openTicket:g,handoffHosts:m,resolveLink:h,focusComposer:f,keyboardAware:y,keyboardOffset:x,empty:k,style:T,testID:v="chat-thread"}){const A=useTheme(),b=useReducedMotion(),w="Reduced"===i||b,j=useMemo(()=>resolveRenderers(s),[s]),_=e.messages[0],R=e.greeting??"",M=useMemo(()=>""!==R&&void 0!==_&&MINE.has(_.author.kind)?{..._,id:"greeting",author:{..._.author,kind:"Assistant",displayName:null,avatarUrl:null},parts:[{kind:"Text",text:R}],stored:!1,streaming:!1,restored:!1,rating:null}:null,[R,_]),N=useMemo(()=>{const t=e.messages.filter(e=>!(e.streaming&&0===e.parts.length)).reverse();return M?[...t,M]:t},[e.messages,M]),S=e.messages[e.messages.length-1]?.id,C=useRef(new Map),I=useRef(null);useEffect(()=>{if(null!==I.current)for(const s of e.messages){if(MINE.has(s.author.kind)||s.streaming||s.restored||I.current.has(s.id))continue;if("Agent"===s.author.kind&&!s.stored)continue;I.current.add(s.id);const e=messagePlainText(s.parts);""!==e&&AccessibilityInfo.announceForAccessibility("Agent"===s.author.kind?`${t.agentSays(s.author.displayName||t.agent)} ${e}`:e)}else I.current=new Set(e.messages.map(e=>e.id))},[e.messages,t]);const P=useMemo(()=>{for(let t=e.messages.length-1;t>=0;t-=1){const s=e.messages[t];if(s?.stored&&"Visitor"===s.author.kind)return s.id}},[e.messages]),V=useMemo(()=>{const t=new Set;return e.messages.forEach((s,a)=>{if("Agent"!==s.author.kind)return;const r=e.messages[a-1];"Agent"===r?.author.kind&&(r.author.displayName||"")===(s.author.displayName||"")||t.add(s.id)}),t},[e.messages]),F=useMemo(()=>{const t=new Set,s=e.messages.filter(e=>!(e.streaming&&0===e.parts.length));return M&&t.add(M.id),s.forEach((e,a)=>{"Assistant"===e.author.kind&&"Assistant"!==s[a-1]?.author.kind&&t.add(e.id)}),t},[e.messages,M]),L=useMemo(()=>handoffSettledIds(e.messages),[e.messages]),$=e.turnError,E="daily_limit"===$?.code,D=null!==$&&/feature_off$/.test($.code),O=E&&$?.resetsDateTimeUtc?` ${t.dailyLimitRefresh(formatChatDateTime($.resetsDateTimeUtc))}`:"",H=e.streaming||null===$&&!e.stopped?null:_jsxs(View,{style:{gap:A.space.xs},testID:`${v}-error`,children:[_jsx(Text,null!==$?{variant:"bodySmall",color:"danger",accessibilityRole:E?void 0:"alert",accessibilityLiveRegion:E?"polite":void 0,children:`${$.message}${O}`}:{variant:"bodySmall",color:"danger",accessibilityLiveRegion:"polite",children:t.stopped}),!e.features.Retry||E||D?null:_jsx(View,{style:{flexDirection:"row"},children:_jsx(Button,{label:t.retry,variant:"link",size:"sm",onPress:()=>{e.retry()},testID:`${v}-retry`})})]}),K=e.thinking&&null!==e.profile?_jsx(ChatLoader,{style:r,steps:e.steps,phrases:e.profile.experience.pacing.thinkingPhrases,intervalMs:e.profile.experience.pacing.phraseIntervalMs,announce:t.preparingAnswer(e.profile.assistant.name),motion:i}):null,B=!0===e.pending?.typing?_jsx(TypingPill,{motion:i,mark:_jsx(ReplyRing,{active:l,reduced:w,children:n?_jsx(Avatar,{size:"xs",name:e.profile?.assistant.name??t.panelTitle,source:{uri:n}}):_jsx(TypingMark,{size:"Orb"===r?14:8})})}):null,W=null!==e.agentTyping&&"WithAgent"===e.conversationState?_jsxs(View,{style:{gap:A.space.xs,alignSelf:"flex-start"},children:[_jsx(TypingPill,{motion:i,mark:_jsx(TypingDots,{})}),_jsx(Text,{variant:"caption",color:"secondary",accessibilityLiveRegion:"polite",children:t.agentTyping(e.agentTyping.displayName||t.agent)})]}):null,z=B||K||H||W?_jsxs(View,{style:{gap:A.space.sm},children:[B,K,W,H]}):void 0,G=_jsx(MessageList,{messages:N,keyExtractor:e=>e.id,renderMessage:({item:s,groupPosition:r})=>_jsxs(View,{role:"listitem",children:[_jsx(Text,{style:visuallyHidden,children:`${spokenAuthor(s,t,e.profile?.assistant.name)}: `}),_jsx(ChatEntrance,{animate:!s.restored,children:_jsx(ThreadMessage,{chat:e,message:s,signature:messageSignature(s),labels:t,all:j,layoutParts:a,groupPosition:r,isLast:s.id===S,isLastVisitor:s.id===P,handoffSettled:L.has(s.id),thinking:e.thinking,streaming:e.streaming,pickAttachments:d,onOpenAttachment:c,onOpenTarget:u,onNavigate:p,openTicket:g,handoffHosts:m,resolveLink:h,focusComposer:f,byline:V.has(s.id),avatar:n,replyAvatars:"None"!==o,avatarStart:"Every"===o||F.has(s.id),expressive:l,reduced:w,sourceRows:C,testID:`${v}-${s.id}`})})]}),getAuthorKey:e=>e.author.kind,getDate:e=>e.createdDateTimeUtc,followNew:followNew,typing:z,empty:k,ItemSeparatorComponent:TurnGap,...void 0===y?{}:{keyboardAware:y},...void 0===x?{}:{keyboardOffset:x},..."web"===Platform.OS?{}:{accessibilityLabel:t.thread},accessibilityRole:"list",style:T,testID:v});return"web"!==Platform.OS?G:_jsx(View,{role:"log","aria-live":"polite","aria-label":t.thread,style:{flex:1},children:G})}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ChatLabels, ChatLoaderStyle, ChatMotion, ChatPart } from "@webority/chat-core";
|
|
1
|
+
import type { ChatLabels, ChatLoaderStyle, ChatMotion, ChatPart, ChatReplyAvatars } from "@webority/chat-core";
|
|
2
2
|
import type React from "react";
|
|
3
3
|
import { type StyleProp, type ViewStyle } from "react-native";
|
|
4
4
|
import type { ChatLayoutParts, ChatOpenTicketHandler, ChatRenderers, PickAttachments } from "./types";
|
|
@@ -12,8 +12,8 @@ export interface ChatThreadProps {
|
|
|
12
12
|
motion?: ChatMotion;
|
|
13
13
|
/** The assistant's picture, drawn beside its replies. */
|
|
14
14
|
avatar?: string | null;
|
|
15
|
-
/**
|
|
16
|
-
replyAvatars?:
|
|
15
|
+
/** Which replies carry the picture: None, the first of each run (First, the default) or Every reply. */
|
|
16
|
+
replyAvatars?: ChatReplyAvatars;
|
|
17
17
|
/** A ring turns around the thinking mark and the streaming reply's picture. Off unless the look sets it. */
|
|
18
18
|
expressive?: boolean;
|
|
19
19
|
pickAttachments?: PickAttachments | undefined;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ChatLabels, ChatLoaderStyle, ChatMotion, ChatPart } from "@webority/chat-core";
|
|
1
|
+
import type { ChatLabels, ChatLoaderStyle, ChatMotion, ChatPart, ChatReplyAvatars } from "@webority/chat-core";
|
|
2
2
|
import type React from "react";
|
|
3
3
|
import { type StyleProp, type ViewStyle } from "react-native";
|
|
4
4
|
import type { ChatLayoutParts, ChatOpenTicketHandler, ChatRenderers, PickAttachments } from "./types.js";
|
|
@@ -12,8 +12,8 @@ export interface ChatThreadProps {
|
|
|
12
12
|
motion?: ChatMotion;
|
|
13
13
|
/** The assistant's picture, drawn beside its replies. */
|
|
14
14
|
avatar?: string | null;
|
|
15
|
-
/**
|
|
16
|
-
replyAvatars?:
|
|
15
|
+
/** Which replies carry the picture: None, the first of each run (First, the default) or Every reply. */
|
|
16
|
+
replyAvatars?: ChatReplyAvatars;
|
|
17
17
|
/** A ring turns around the thinking mark and the streaming reply's picture. Off unless the look sets it. */
|
|
18
18
|
expressive?: boolean;
|
|
19
19
|
pickAttachments?: PickAttachments | undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webority/mobile-chat",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.161.0",
|
|
4
4
|
"description": "Webority Chat for React Native apps: the chat screen, sheet and embedded shells, thread, composer, one renderer per message part kind and the useChat hook, built on @webority/chat-core and @webority/mobile-ui.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react-native",
|
|
@@ -42,10 +42,10 @@
|
|
|
42
42
|
"!**/*.map"
|
|
43
43
|
],
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@webority/chat-core": "0.
|
|
46
|
-
"@webority/markdown-core": "^0.
|
|
47
|
-
"@webority/mobile-core": "^0.
|
|
48
|
-
"@webority/mobile-ui": "^0.
|
|
45
|
+
"@webority/chat-core": "0.161.0",
|
|
46
|
+
"@webority/markdown-core": "^0.191.0",
|
|
47
|
+
"@webority/mobile-core": "^0.191.0",
|
|
48
|
+
"@webority/mobile-ui": "^0.191.0",
|
|
49
49
|
"expo-audio": ">=58.0.0",
|
|
50
50
|
"expo-crypto": ">=58.0.0",
|
|
51
51
|
"expo-file-system": ">=58.0.0",
|
|
@@ -99,11 +99,11 @@
|
|
|
99
99
|
"@testing-library/react-native": "14.1.0",
|
|
100
100
|
"@types/jest": "29.5.14",
|
|
101
101
|
"@types/react": "19.3.0",
|
|
102
|
-
"@webority/canvas-kit": "0.
|
|
103
|
-
"@webority/chat-core": "0.
|
|
104
|
-
"@webority/markdown-core": "0.
|
|
105
|
-
"@webority/mobile-core": "0.
|
|
106
|
-
"@webority/mobile-ui": "0.
|
|
102
|
+
"@webority/canvas-kit": "0.191.0",
|
|
103
|
+
"@webority/chat-core": "0.161.0",
|
|
104
|
+
"@webority/markdown-core": "0.191.0",
|
|
105
|
+
"@webority/mobile-core": "0.191.0",
|
|
106
|
+
"@webority/mobile-ui": "0.191.0",
|
|
107
107
|
"babel-jest": "29.7.0",
|
|
108
108
|
"expo": "58.0.23",
|
|
109
109
|
"expo-application": "58.1.2",
|