@dashx/react 0.1.6 → 0.1.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.
- package/dist/cjs/hooks/use-agent.d.ts +2 -2
- package/dist/cjs/hooks/use-agent.js +1 -1
- package/dist/cjs/hooks/use-agent.js.map +2 -2
- package/dist/cjs/widgets/ChatBody.d.ts +2 -2
- package/dist/cjs/widgets/ChatBody.js +1 -1
- package/dist/cjs/widgets/ChatBody.js.map +2 -2
- package/dist/esm/hooks/use-agent.d.ts +2 -2
- package/dist/esm/hooks/use-agent.js +1 -1
- package/dist/esm/hooks/use-agent.js.map +2 -2
- package/dist/esm/widgets/ChatBody.d.ts +2 -2
- package/dist/esm/widgets/ChatBody.js +1 -1
- package/dist/esm/widgets/ChatBody.js.map +2 -2
- package/package.json +2 -2
- package/src/hooks/use-agent.ts +6 -6
- package/src/widgets/ChatBody.tsx +7 -7
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type { AiAgent,
|
|
1
|
+
import type { AiAgent, AiNotification } from '@dashx/browser';
|
|
2
2
|
type UseAgentHookProps = {
|
|
3
3
|
publicEmbedKey: string;
|
|
4
4
|
};
|
|
5
5
|
type UseAgentHookResponse = {
|
|
6
6
|
agent?: AiAgent;
|
|
7
7
|
conversationId: string | null;
|
|
8
|
-
messages:
|
|
8
|
+
messages: AiNotification[];
|
|
9
9
|
isThinking: boolean;
|
|
10
10
|
error: string | null;
|
|
11
11
|
sendMessage: (text: string) => Promise<void>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var U=Object.create;var l=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var N=Object.getPrototypeOf,R=Object.prototype.hasOwnProperty;var C=(e,s)=>{for(var n in s)l(e,n,{get:s[n],enumerable:!0})},d=(e,s,n,r)=>{if(s&&typeof s=="object"||typeof s=="function")for(let t of P(s))!R.call(e,t)&&t!==n&&l(e,t,{get:()=>s[t],enumerable:!(r=y(s,t))||r.enumerable});return e};var p=(e,s,n)=>(n=e!=null?U(N(e)):{},d(s||!e||!e.__esModule?l(n,"default",{value:e,enumerable:!0}):n,e)),M=e=>d(l({},"__esModule",{value:!0}),e);var x={};C(x,{default:()=>w});module.exports=M(x);var f=p(require("uuid-random")),o=require("react"),m=p(require("./use-dashx-provider.js"));const T=({publicEmbedKey:e})=>{const s=(0,m.default)(),[n,r]=(0,o.useState)(),[t,k]=(0,o.useState)(null),[h,A]=(0,o.useState)([]),[v,u]=(0,o.useState)(!1),[H,a]=(0,o.useState)(null);(0,o.useEffect)(()=>{s.loadAiAgent({publicEmbedKey:e}).then(i=>{r(i),a(null)}).catch(i=>{a(i)})},[s,e]);const I=(0,o.useCallback)(async i=>{if(!(!i.trim()||!n))try{A(c=>[...c,{id:(0,f.default)(),aiRole:"user",renderedContent:i}]),u(!0),a(null);const g=await s.invokeAiAgent({conversationId:t,prompt:i,publicEmbedKey:e});g.conversationId&&!t&&k(g.conversationId),A(c=>[...c,g])}catch{a("Failed to send message")}finally{u(!1)}},[s,n,e,t]);return{agent:n,conversationId:t,messages:h,isThinking:v,error:H,sendMessage:I}};var w=T;
|
|
2
2
|
//# sourceMappingURL=use-agent.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/hooks/use-agent.ts"],
|
|
4
|
-
"sourcesContent": ["import uuid from 'uuid-random';\nimport { useState, useEffect, useCallback } from 'react';\nimport type { AiAgent,
|
|
5
|
-
"mappings": "0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAAI,EAAiB,0BACjBC,EAAiD,iBAGjDC,EAA6B,sCAe7B,MAAMC,EAAW,CAAC,CAAE,eAAAC,CAAe,IAA+C,CAChF,MAAMC,KAAQ,EAAAC,SAAiB,EAEzB,CAACC,EAAOC,CAAQ,KAAI,YAAkB,EAEtC,CAACC,EAAgBC,CAAiB,KAAI,YAAwB,IAAI,EAClE,CAACC,EAAUC,CAAW,KAAI,
|
|
4
|
+
"sourcesContent": ["import uuid from 'uuid-random';\nimport { useState, useEffect, useCallback } from 'react';\nimport type { AiAgent, AiNotification } from '@dashx/browser';\n\nimport useDashXProvider from './use-dashx-provider.js';\n\ntype UseAgentHookProps = {\n publicEmbedKey: string;\n}\n\ntype UseAgentHookResponse = {\n agent?: AiAgent;\n conversationId: string | null;\n messages: AiNotification[];\n isThinking: boolean;\n error: string | null;\n sendMessage: (text: string) => Promise<void>;\n}\n\nconst useAgent = ({ publicEmbedKey }: UseAgentHookProps): UseAgentHookResponse => {\n const dashX = useDashXProvider();\n\n const [agent, setAgent] = useState<AiAgent>();\n\n const [conversationId, setConversationId] = useState<string | null>(null);\n const [messages, setMessages] = useState<AiNotification[]>([]);\n const [isThinking, setIsThinking] = useState(false);\n const [error, setError] = useState<string | null>(null);\n\n useEffect(() => {\n dashX\n .loadAiAgent({\n publicEmbedKey,\n })\n .then((agent) => {\n setAgent(agent);\n setError(null);\n })\n .catch((err) => {\n setError(err);\n });\n }, [dashX, publicEmbedKey]);\n\n const sendMessage = useCallback(\n async (text: string) => {\n if (!text.trim() || !agent) return;\n\n try {\n setMessages((prev) => [...prev, {\n id: uuid(),\n aiRole: 'user',\n renderedContent: text\n } as AiNotification]);\n setIsThinking(true);\n setError(null);\n\n const response = await dashX.invokeAiAgent({\n conversationId,\n prompt: text,\n publicEmbedKey,\n });\n\n if (response.conversationId && !conversationId) {\n setConversationId(response.conversationId);\n }\n\n setMessages((prev) => [...prev, response]);\n } catch (err) {\n setError('Failed to send message');\n } finally {\n setIsThinking(false);\n }\n },\n [dashX, agent, publicEmbedKey, conversationId]\n );\n\n return {\n agent,\n conversationId,\n messages,\n isThinking,\n error,\n sendMessage,\n };\n};\n\nexport type { UseAgentHookProps, UseAgentHookResponse };\n\nexport default useAgent;\n"],
|
|
5
|
+
"mappings":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,CACL,MAAAF,EACA,eAAAE,EACA,SAAAE,EACA,WAAAE,EACA,MAAAE,EACA,YAAAG,CACF,CACF,EAIA,IAAOpB,EAAQK",
|
|
6
6
|
"names": ["use_agent_exports", "__export", "use_agent_default", "__toCommonJS", "import_uuid_random", "import_react", "import_use_dashx_provider", "useAgent", "publicEmbedKey", "dashX", "useDashXProvider", "agent", "setAgent", "conversationId", "setConversationId", "messages", "setMessages", "isThinking", "setIsThinking", "error", "setError", "err", "sendMessage", "text", "prev", "uuid", "response"]
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { AiAgent,
|
|
1
|
+
import type { AiAgent, AiNotification } from '@dashx/browser';
|
|
2
2
|
type ChatBodyProps = {
|
|
3
3
|
agent: AiAgent;
|
|
4
|
-
messages:
|
|
4
|
+
messages: AiNotification[];
|
|
5
5
|
isThinking: boolean;
|
|
6
6
|
error: string | null;
|
|
7
7
|
sendMessage: (message: string) => void;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var F=Object.create;var d=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var U=Object.getOwnPropertyNames;var $=Object.getPrototypeOf,z=Object.prototype.hasOwnProperty;var G=(t
|
|
1
|
+
"use strict";var F=Object.create;var d=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var U=Object.getOwnPropertyNames;var $=Object.getPrototypeOf,z=Object.prototype.hasOwnProperty;var G=(e,t)=>{for(var a in t)d(e,a,{get:t[a],enumerable:!0})},w=(e,t,a,m)=>{if(t&&typeof t=="object"||typeof t=="function")for(let c of U(t))!z.call(e,c)&&c!==a&&d(e,c,{get:()=>t[c],enumerable:!(m=P(t,c))||m.enumerable});return e};var M=(e,t,a)=>(a=e!=null?F($(e)):{},w(t||!e||!e.__esModule?d(a,"default",{value:e,enumerable:!0}):a,e)),I=e=>w(d({},"__esModule",{value:!0}),e);var W={};G(W,{default:()=>V});module.exports=I(W);var r=require("react/jsx-runtime"),g=M(require("@radix-ui/react-scroll-area")),n=require("react"),l=require("../components"),f=require("../utils/cn.js"),A=require("../components/markdown-renderer.js"),N=M(require("../hooks/use-auto-scroll.js"));const v="max-w-[max(80%,350px)]",D="max-w-[80%]",H=({agent:e,messages:t,isThinking:a,error:m,sendMessage:c,setIsAnimating:p,isAnimating:h,isPopoverOpen:S=!1})=>{const x=(0,n.useRef)(t.length),C=(0,n.useRef)(t.length),{scrollAreaRef:R,scrollToBottom:i,checkIfUserAtBottom:b,isUserAtBottom:u}=(0,N.default)(),T=(0,n.useCallback)(()=>{p(!1)},[p]);(0,n.useEffect)(()=>{const o=t[t.length-1],s=t.length>x.current;s&&o?.aiRole==="assistant"&&p(!0),s&&setTimeout(()=>i({smooth:!0}),0),x.current=t.length},[t,a,p,i,u]),(0,n.useEffect)(()=>{a&&setTimeout(()=>i({smooth:!0}),0)},[a,i,u]),(0,n.useEffect)(()=>{if(h&&u){const o=setInterval(()=>{b(),u&&i({smooth:!1})},100);return()=>clearInterval(o)}},[h,i,b,u]),(0,n.useEffect)(()=>{S&&setTimeout(()=>i({smooth:!0}),100)},[S,i]);const B=o=>{c(o.content)},k=(o,s)=>{const y=o.aiRole==="user",E=s===t.length-1&&!y,_=s>=C.current,L=E&&_;return(0,r.jsx)("div",{className:(0,f.cn)([y?`bg-bg ml-auto items-end rounded-lg py-2 px-3 ${D}`:`mr-auto items-start ${v}`]),children:(0,r.jsx)(A.MarkdownRenderer,{animate:L,onAnimationComplete:T,children:o.renderedContent||""})},o.id||s)};return(0,r.jsx)(l.Flex,{direction:"column",className:"grow overflow-auto bg-gray-50",children:(0,r.jsxs)(g.Root,{ref:R,className:"h-full grow-1 p-6",children:[(0,r.jsx)(g.Viewport,{className:"h-full",children:(0,r.jsxs)(l.Flex,{direction:"column",gap:6,children:[e.starterMessages?.map((o,s)=>(0,r.jsx)("div",{className:(0,f.cn)("mr-auto items-start",v),children:(0,r.jsx)(A.MarkdownRenderer,{children:o.content||""})},`message-${s}`)),e.starterSuggestions&&t.length===0&&(0,r.jsx)(l.Flex,{gap:2,wrap:"wrap",children:e.starterSuggestions.map((o,s)=>(0,r.jsx)(l.Button,{variant:"fill",mode:"subtle",roundness:"full",size:"extrasmall",onPress:()=>B(o),children:o.label||o.content},`suggestion-${s}`))}),t.map((o,s)=>k(o,s)),a&&(0,r.jsx)(l.Text,{as:"p",size:2,className:"mr-auto items-start text-gray-500",children:"Thinking..."}),m&&(0,r.jsxs)(l.Text,{as:"p",size:2,className:"bg-negative-100 text-negative-900 mx-auto items-start rounded-lg py-2 px-3",children:["Error: ",m]})]})}),(0,r.jsx)(g.Scrollbar,{orientation:"vertical",className:"w-1 bg-gray-200 opacity-50",children:(0,r.jsx)(g.Thumb,{className:"flex-1 rounded-2xl bg-gray-400"})}),(0,r.jsx)(g.Corner,{})]})})};var V=H;
|
|
2
2
|
//# sourceMappingURL=ChatBody.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/widgets/ChatBody.tsx"],
|
|
4
|
-
"sourcesContent": ["import * as ScrollArea from '@radix-ui/react-scroll-area';\nimport React, { useEffect, useRef, useCallback } from 'react';\nimport type { AiAgent,
|
|
5
|
-
"mappings": "0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GAsGQ,IAAAI,EAAA,6BAtGRC,EAA4B,0CAC5BC,EAAsD,iBAGtDC,EAAmC,yBACnCC,EAAmB,0BACnBC,EAAiC,8CACjCC,EAA0B,0CAE1B,MAAMC,EAA4B,yBAC5BC,EAA2B,cAa3BC,EAAW,CAAC,CAChB,MAAAC,EACA,SAAAC,EACA,WAAAC,EACA,MAAAC,EACA,YAAAC,EACA,eAAAC,EACA,YAAAC,EACA,cAAAC,EAAgB,EAClB,IAAqB,CACnB,MAAMC,KAA0B,UAAOP,EAAS,MAAM,EAChDQ,KAAyB,UAAOR,EAAS,MAAM,EAC/C,CAAE,cAAAS,EAAe,eAAAC,EAAgB,oBAAAC,EAAqB,eAAAC,CAAe,KAAI,EAAAC,SAAc,EAEvFC,KAA0B,eAAY,IAAM,CAChDV,EAAe,EAAK,CACtB,EAAG,CAACA,CAAc,CAAC,KAEnB,aAAU,IAAM,CACd,MAAMW,EAAcf,EAASA,EAAS,OAAS,CAAC,EAC1CgB,EAAgBhB,EAAS,OAASO,EAAwB,QAE5DS,GAAiBD,GAAa,
|
|
4
|
+
"sourcesContent": ["import * as ScrollArea from '@radix-ui/react-scroll-area';\nimport React, { useEffect, useRef, useCallback } from 'react';\nimport type { AiAgent, AiNotification, AiAgentStarterSuggestion } from '@dashx/browser';\n\nimport { Button, Flex, Text } from '../components';\nimport { cn } from '../utils/cn.js';\nimport { MarkdownRenderer } from '../components/markdown-renderer.js';\nimport useAutoScroll from '../hooks/use-auto-scroll.js';\n\nconst AGENT_MESSAGE_WIDTH_CLASS = 'max-w-[max(80%,350px)]';\nconst USER_MESSAGE_WIDTH_CLASS = 'max-w-[80%]';\n\ntype ChatBodyProps = {\n agent: AiAgent;\n messages: AiNotification[];\n isThinking: boolean;\n error: string | null;\n sendMessage: (message: string) => void;\n isAnimating: boolean;\n setIsAnimating: (animating: boolean) => void;\n isPopoverOpen?: boolean;\n};\n\nconst ChatBody = ({\n agent,\n messages,\n isThinking,\n error,\n sendMessage,\n setIsAnimating,\n isAnimating,\n isPopoverOpen = false,\n}: ChatBodyProps) => {\n const previousMessageCountRef = useRef(messages.length);\n const initialMessageCountRef = useRef(messages.length);\n const { scrollAreaRef, scrollToBottom, checkIfUserAtBottom, isUserAtBottom } = useAutoScroll();\n\n const handleAnimationComplete = useCallback(() => {\n setIsAnimating(false);\n }, [setIsAnimating]);\n\n useEffect(() => {\n const lastMessage = messages[messages.length - 1];\n const hasNewMessage = messages.length > previousMessageCountRef.current;\n \n if (hasNewMessage && lastMessage?.aiRole === 'assistant') {\n setIsAnimating(true);\n }\n\n if (hasNewMessage) {\n setTimeout(() => scrollToBottom({ smooth: true }), 0);\n }\n\n previousMessageCountRef.current = messages.length;\n }, [messages, isThinking, setIsAnimating, scrollToBottom, isUserAtBottom]);\n\n useEffect(() => {\n if (isThinking) {\n setTimeout(() => scrollToBottom({ smooth: true }), 0);\n }\n }, [isThinking, scrollToBottom, isUserAtBottom]);\n\n useEffect(() => {\n if (isAnimating && isUserAtBottom) {\n const interval = setInterval(() => {\n checkIfUserAtBottom();\n if (isUserAtBottom) {\n scrollToBottom({ smooth: false });\n }\n }, 100);\n\n return () => clearInterval(interval);\n }\n }, [isAnimating, scrollToBottom, checkIfUserAtBottom, isUserAtBottom]);\n\n useEffect(() => {\n if (isPopoverOpen) {\n // Use a small delay to ensure the popover content is fully rendered\n setTimeout(() => scrollToBottom({ smooth: true }), 100);\n }\n }, [isPopoverOpen, scrollToBottom]);\n\n const handleSuggestionClick = (suggestion: AiAgentStarterSuggestion) => {\n sendMessage(suggestion.content);\n };\n\n const renderMessage = (message: AiNotification, index: number) => {\n const isUser = message.aiRole === 'user';\n const isLastMessage = index === messages.length - 1;\n const isLastAgentMessage = isLastMessage && !isUser;\n const isNewMessage = index >= initialMessageCountRef.current;\n const shouldAnimate = isLastAgentMessage && isNewMessage;\n\n return (\n <div\n key={message.id || index}\n className={cn([\n isUser\n ? `bg-bg ml-auto items-end rounded-lg py-2 px-3 ${USER_MESSAGE_WIDTH_CLASS}`\n : `mr-auto items-start ${AGENT_MESSAGE_WIDTH_CLASS}`,\n ])}\n >\n <MarkdownRenderer animate={shouldAnimate} onAnimationComplete={handleAnimationComplete}>\n {message.renderedContent || ''}\n </MarkdownRenderer>\n </div>\n );\n };\n\n return (\n <Flex direction=\"column\" className=\"grow overflow-auto bg-gray-50\">\n <ScrollArea.Root ref={scrollAreaRef} className=\"h-full grow-1 p-6\">\n <ScrollArea.Viewport className=\"h-full\">\n <Flex direction=\"column\" gap={6}>\n {agent.starterMessages?.map((msg, index) => (\n <div\n key={`message-${index}`}\n className={cn('mr-auto items-start', AGENT_MESSAGE_WIDTH_CLASS)}\n >\n <MarkdownRenderer>{msg.content || ''}</MarkdownRenderer>\n </div>\n ))}\n\n {agent.starterSuggestions && messages.length === 0 && (\n <Flex gap={2} wrap=\"wrap\">\n {agent.starterSuggestions.map((suggestion, index) => (\n <Button\n key={`suggestion-${index}`}\n variant=\"fill\"\n mode=\"subtle\"\n roundness=\"full\"\n size=\"extrasmall\"\n onPress={() => handleSuggestionClick(suggestion)}\n >\n {suggestion.label || suggestion.content}\n </Button>\n ))}\n </Flex>\n )}\n\n {messages.map((message, index) => renderMessage(message, index))}\n\n {isThinking && (\n <Text as=\"p\" size={2} className=\"mr-auto items-start text-gray-500\">\n Thinking...\n </Text>\n )}\n\n {error && (\n <Text\n as=\"p\"\n size={2}\n className=\"bg-negative-100 text-negative-900 mx-auto items-start rounded-lg py-2 px-3\"\n >\n Error: {error}\n </Text>\n )}\n </Flex>\n </ScrollArea.Viewport>\n <ScrollArea.Scrollbar orientation=\"vertical\" className=\"w-1 bg-gray-200 opacity-50\">\n <ScrollArea.Thumb className=\"flex-1 rounded-2xl bg-gray-400\" />\n </ScrollArea.Scrollbar>\n <ScrollArea.Corner />\n </ScrollArea.Root>\n </Flex>\n );\n};\n\nexport type { ChatBodyProps };\n\nexport default ChatBody;\n"],
|
|
5
|
+
"mappings": "0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GAsGQ,IAAAI,EAAA,6BAtGRC,EAA4B,0CAC5BC,EAAsD,iBAGtDC,EAAmC,yBACnCC,EAAmB,0BACnBC,EAAiC,8CACjCC,EAA0B,0CAE1B,MAAMC,EAA4B,yBAC5BC,EAA2B,cAa3BC,EAAW,CAAC,CAChB,MAAAC,EACA,SAAAC,EACA,WAAAC,EACA,MAAAC,EACA,YAAAC,EACA,eAAAC,EACA,YAAAC,EACA,cAAAC,EAAgB,EAClB,IAAqB,CACnB,MAAMC,KAA0B,UAAOP,EAAS,MAAM,EAChDQ,KAAyB,UAAOR,E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AIA,IAAOH,EAAQW",
|
|
6
6
|
"names": ["ChatBody_exports", "__export", "ChatBody_default", "__toCommonJS", "import_jsx_runtime", "ScrollArea", "import_react", "import_components", "import_cn", "import_markdown_renderer", "import_use_auto_scroll", "AGENT_MESSAGE_WIDTH_CLASS", "USER_MESSAGE_WIDTH_CLASS", "ChatBody", "agent", "messages", "isThinking", "error", "sendMessage", "setIsAnimating", "isAnimating", "isPopoverOpen", "previousMessageCountRef", "initialMessageCountRef", "scrollAreaRef", "scrollToBottom", "checkIfUserAtBottom", "isUserAtBottom", "useAutoScroll", "handleAnimationComplete", "lastMessage", "hasNewMessage", "interval", "handleSuggestionClick", "suggestion", "renderMessage", "message", "index", "isUser", "isLastAgentMessage", "isNewMessage", "shouldAnimate", "msg"]
|
|
7
7
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type { AiAgent,
|
|
1
|
+
import type { AiAgent, AiNotification } from '@dashx/browser';
|
|
2
2
|
type UseAgentHookProps = {
|
|
3
3
|
publicEmbedKey: string;
|
|
4
4
|
};
|
|
5
5
|
type UseAgentHookResponse = {
|
|
6
6
|
agent?: AiAgent;
|
|
7
7
|
conversationId: string | null;
|
|
8
|
-
messages:
|
|
8
|
+
messages: AiNotification[];
|
|
9
9
|
isThinking: boolean;
|
|
10
10
|
error: string | null;
|
|
11
11
|
sendMessage: (text: string) => Promise<void>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import k from"uuid-random";import{useState as s,useEffect as h,useCallback as v}from"react";import
|
|
1
|
+
import k from"uuid-random";import{useState as s,useEffect as h,useCallback as v}from"react";import H from"./use-dashx-provider.js";const I=({publicEmbedKey:n})=>{const t=H(),[a,A]=s(),[o,u]=s(null),[d,l]=s([]),[p,c]=s(!1),[f,i]=s(null);h(()=>{t.loadAiAgent({publicEmbedKey:n}).then(e=>{A(e),i(null)}).catch(e=>{i(e)})},[t,n]);const m=v(async e=>{if(!(!e.trim()||!a))try{l(g=>[...g,{id:k(),aiRole:"user",renderedContent:e}]),c(!0),i(null);const r=await t.invokeAiAgent({conversationId:o,prompt:e,publicEmbedKey:n});r.conversationId&&!o&&u(r.conversationId),l(g=>[...g,r])}catch{i("Failed to send message")}finally{c(!1)}},[t,a,n,o]);return{agent:a,conversationId:o,messages:d,isThinking:p,error:f,sendMessage:m}};var N=I;export{N as default};
|
|
2
2
|
//# sourceMappingURL=use-agent.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/hooks/use-agent.ts"],
|
|
4
|
-
"sourcesContent": ["import uuid from 'uuid-random';\nimport { useState, useEffect, useCallback } from 'react';\nimport type { AiAgent,
|
|
5
|
-
"mappings": "AAAA,OAAOA,MAAU,cACjB,OAAS,YAAAC,EAAU,aAAAC,EAAW,eAAAC,MAAmB,QAGjD,OAAOC,MAAsB,0BAe7B,MAAMC,EAAW,CAAC,CAAE,eAAAC,CAAe,IAA+C,CAChF,MAAMC,EAAQH,EAAiB,EAEzB,CAACI,EAAOC,CAAQ,EAAIR,EAAkB,EAEtC,CAACS,EAAgBC,CAAiB,EAAIV,EAAwB,IAAI,EAClE,CAACW,EAAUC,CAAW,EAAIZ,
|
|
4
|
+
"sourcesContent": ["import uuid from 'uuid-random';\nimport { useState, useEffect, useCallback } from 'react';\nimport type { AiAgent, AiNotification } from '@dashx/browser';\n\nimport useDashXProvider from './use-dashx-provider.js';\n\ntype UseAgentHookProps = {\n publicEmbedKey: string;\n}\n\ntype UseAgentHookResponse = {\n agent?: AiAgent;\n conversationId: string | null;\n messages: AiNotification[];\n isThinking: boolean;\n error: string | null;\n sendMessage: (text: string) => Promise<void>;\n}\n\nconst useAgent = ({ publicEmbedKey }: UseAgentHookProps): UseAgentHookResponse => {\n const dashX = useDashXProvider();\n\n const [agent, setAgent] = useState<AiAgent>();\n\n const [conversationId, setConversationId] = useState<string | null>(null);\n const [messages, setMessages] = useState<AiNotification[]>([]);\n const [isThinking, setIsThinking] = useState(false);\n const [error, setError] = useState<string | null>(null);\n\n useEffect(() => {\n dashX\n .loadAiAgent({\n publicEmbedKey,\n })\n .then((agent) => {\n setAgent(agent);\n setError(null);\n })\n .catch((err) => {\n setError(err);\n });\n }, [dashX, publicEmbedKey]);\n\n const sendMessage = useCallback(\n async (text: string) => {\n if (!text.trim() || !agent) return;\n\n try {\n setMessages((prev) => [...prev, {\n id: uuid(),\n aiRole: 'user',\n renderedContent: text\n } as AiNotification]);\n setIsThinking(true);\n setError(null);\n\n const response = await dashX.invokeAiAgent({\n conversationId,\n prompt: text,\n publicEmbedKey,\n });\n\n if (response.conversationId && !conversationId) {\n setConversationId(response.conversationId);\n }\n\n setMessages((prev) => [...prev, response]);\n } catch (err) {\n setError('Failed to send message');\n } finally {\n setIsThinking(false);\n }\n },\n [dashX, agent, publicEmbedKey, conversationId]\n );\n\n return {\n agent,\n conversationId,\n messages,\n isThinking,\n error,\n sendMessage,\n };\n};\n\nexport type { UseAgentHookProps, UseAgentHookResponse };\n\nexport default useAgent;\n"],
|
|
5
|
+
"mappings": "AAAA,OAAOA,MAAU,cACjB,OAAS,YAAAC,EAAU,aAAAC,EAAW,eAAAC,MAAmB,QAGjD,OAAOC,MAAsB,0BAe7B,MAAMC,EAAW,CAAC,CAAE,eAAAC,CAAe,IAA+C,CAChF,MAAMC,EAAQH,EAAiB,EAEzB,CAACI,EAAOC,CAAQ,EAAIR,EAAkB,EAEtC,CAACS,EAAgBC,CAAiB,EAAIV,EAAwB,IAAI,EAClE,CAACW,EAAUC,CAAW,EAAIZ,EAA2B,CAAC,CAAC,EACvD,CAACa,EAAYC,CAAa,EAAId,EAAS,EAAK,EAC5C,CAACe,EAAOC,CAAQ,EAAIhB,EAAwB,IAAI,EAEtDC,EAAU,IAAM,CACdK,EACG,YAAY,CACX,eAAAD,CACF,CAAC,EACA,KAAME,GAAU,CACfC,EAASD,CAAK,EACdS,EAAS,IAAI,CACf,CAAC,EACA,MAAOC,GAAQ,CACdD,EAASC,CAAG,CACd,CAAC,CACL,EAAG,CAACX,EAAOD,CAAc,CAAC,EAE1B,MAAMa,EAAchB,EAClB,MAAOiB,GAAiB,CACtB,GAAI,GAACA,EAAK,KAAK,GAAK,CAACZ,GAErB,GAAI,CACFK,EAAaQ,GAAS,CAAC,GAAGA,EAAM,CAC9B,GAAIrB,EAAK,EACT,OAAQ,OACR,gBAAiBoB,CACnB,CAAmB,CAAC,EACpBL,EAAc,EAAI,EAClBE,EAAS,IAAI,EAEb,MAAMK,EAAW,MAAMf,EAAM,cAAc,CACzC,eAAAG,EACA,OAAQU,EACR,eAAAd,CACF,CAAC,EAEGgB,EAAS,gBAAkB,CAACZ,GAC9BC,EAAkBW,EAAS,cAAc,EAG3CT,EAAaQ,GAAS,CAAC,GAAGA,EAAMC,CAAQ,CAAC,CAC3C,MAAc,CACZL,EAAS,wBAAwB,CACnC,QAAE,CACAF,EAAc,EAAK,CACrB,CACF,EACA,CAACR,EAAOC,EAAOF,EAAgBI,CAAc,CAC/C,EAEA,MAAO,CACL,MAAAF,EACA,eAAAE,EACA,SAAAE,EACA,WAAAE,EACA,MAAAE,EACA,YAAAG,CACF,CACF,EAIA,IAAOI,EAAQlB",
|
|
6
6
|
"names": ["uuid", "useState", "useEffect", "useCallback", "useDashXProvider", "useAgent", "publicEmbedKey", "dashX", "agent", "setAgent", "conversationId", "setConversationId", "messages", "setMessages", "isThinking", "setIsThinking", "error", "setError", "err", "sendMessage", "text", "prev", "response", "use_agent_default"]
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { AiAgent,
|
|
1
|
+
import type { AiAgent, AiNotification } from '@dashx/browser';
|
|
2
2
|
type ChatBodyProps = {
|
|
3
3
|
agent: AiAgent;
|
|
4
|
-
messages:
|
|
4
|
+
messages: AiNotification[];
|
|
5
5
|
isThinking: boolean;
|
|
6
6
|
error: string | null;
|
|
7
7
|
sendMessage: (message: string) => void;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{jsx as o,jsxs as g}from"react/jsx-runtime";import*as n from"@radix-ui/react-scroll-area";import{useEffect as c,useRef as x,useCallback as L}from"react";import{Button as F,Flex as u,Text as
|
|
1
|
+
import{jsx as o,jsxs as g}from"react/jsx-runtime";import*as n from"@radix-ui/react-scroll-area";import{useEffect as c,useRef as x,useCallback as L}from"react";import{Button as F,Flex as u,Text as b}from"../components";import{cn as y}from"../utils/cn.js";import{MarkdownRenderer as w}from"../components/markdown-renderer.js";import P from"../hooks/use-auto-scroll.js";const M="max-w-[max(80%,350px)]",U="max-w-[80%]",$=({agent:m,messages:r,isThinking:l,error:p,sendMessage:v,setIsAnimating:i,isAnimating:d,isPopoverOpen:f=!1})=>{const A=x(r.length),N=x(r.length),{scrollAreaRef:C,scrollToBottom:a,checkIfUserAtBottom:h,isUserAtBottom:s}=P(),R=L(()=>{i(!1)},[i]);c(()=>{const t=r[r.length-1],e=r.length>A.current;e&&t?.aiRole==="assistant"&&i(!0),e&&setTimeout(()=>a({smooth:!0}),0),A.current=r.length},[r,l,i,a,s]),c(()=>{l&&setTimeout(()=>a({smooth:!0}),0)},[l,a,s]),c(()=>{if(d&&s){const t=setInterval(()=>{h(),s&&a({smooth:!1})},100);return()=>clearInterval(t)}},[d,a,h,s]),c(()=>{f&&setTimeout(()=>a({smooth:!0}),100)},[f,a]);const T=t=>{v(t.content)},B=(t,e)=>{const S=t.aiRole==="user",k=e===r.length-1&&!S,E=e>=N.current,_=k&&E;return o("div",{className:y([S?`bg-bg ml-auto items-end rounded-lg py-2 px-3 ${U}`:`mr-auto items-start ${M}`]),children:o(w,{animate:_,onAnimationComplete:R,children:t.renderedContent||""})},t.id||e)};return o(u,{direction:"column",className:"grow overflow-auto bg-gray-50",children:g(n.Root,{ref:C,className:"h-full grow-1 p-6",children:[o(n.Viewport,{className:"h-full",children:g(u,{direction:"column",gap:6,children:[m.starterMessages?.map((t,e)=>o("div",{className:y("mr-auto items-start",M),children:o(w,{children:t.content||""})},`message-${e}`)),m.starterSuggestions&&r.length===0&&o(u,{gap:2,wrap:"wrap",children:m.starterSuggestions.map((t,e)=>o(F,{variant:"fill",mode:"subtle",roundness:"full",size:"extrasmall",onPress:()=>T(t),children:t.label||t.content},`suggestion-${e}`))}),r.map((t,e)=>B(t,e)),l&&o(b,{as:"p",size:2,className:"mr-auto items-start text-gray-500",children:"Thinking..."}),p&&g(b,{as:"p",size:2,className:"bg-negative-100 text-negative-900 mx-auto items-start rounded-lg py-2 px-3",children:["Error: ",p]})]})}),o(n.Scrollbar,{orientation:"vertical",className:"w-1 bg-gray-200 opacity-50",children:o(n.Thumb,{className:"flex-1 rounded-2xl bg-gray-400"})}),o(n.Corner,{})]})})};var q=$;export{q as default};
|
|
2
2
|
//# sourceMappingURL=ChatBody.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/widgets/ChatBody.tsx"],
|
|
4
|
-
"sourcesContent": ["import * as ScrollArea from '@radix-ui/react-scroll-area';\nimport React, { useEffect, useRef, useCallback } from 'react';\nimport type { AiAgent,
|
|
5
|
-
"mappings": "AAsGQ,cAAAA,EA+CM,QAAAC,MA/CN,oBAtGR,UAAYC,MAAgB,8BAC5B,OAAgB,aAAAC,EAAW,UAAAC,EAAQ,eAAAC,MAAmB,QAGtD,OAAS,UAAAC,EAAQ,QAAAC,EAAM,QAAAC,MAAY,gBACnC,OAAS,MAAAC,MAAU,iBACnB,OAAS,oBAAAC,MAAwB,qCACjC,OAAOC,MAAmB,8BAE1B,MAAMC,EAA4B,yBAC5BC,EAA2B,cAa3BC,EAAW,CAAC,CAChB,MAAAC,EACA,SAAAC,EACA,WAAAC,EACA,MAAAC,EACA,YAAAC,EACA,eAAAC,EACA,YAAAC,EACA,cAAAC,EAAgB,EAClB,IAAqB,CACnB,MAAMC,EAA0BnB,EAAOY,EAAS,MAAM,EAChDQ,EAAyBpB,EAAOY,EAAS,MAAM,EAC/C,CAAE,cAAAS,EAAe,eAAAC,EAAgB,oBAAAC,EAAqB,eAAAC,CAAe,EAAIjB,EAAc,EAEvFkB,EAA0BxB,EAAY,IAAM,CAChDe,EAAe,EAAK,CACtB,EAAG,CAACA,CAAc,CAAC,EAEnBjB,EAAU,IAAM,CACd,MAAM2B,EAAcd,EAASA,EAAS,OAAS,CAAC,EAC1Ce,EAAgBf,EAAS,OAASO,EAAwB,QAE5DQ,GAAiBD,GAAa,
|
|
4
|
+
"sourcesContent": ["import * as ScrollArea from '@radix-ui/react-scroll-area';\nimport React, { useEffect, useRef, useCallback } from 'react';\nimport type { AiAgent, AiNotification, AiAgentStarterSuggestion } from '@dashx/browser';\n\nimport { Button, Flex, Text } from '../components';\nimport { cn } from '../utils/cn.js';\nimport { MarkdownRenderer } from '../components/markdown-renderer.js';\nimport useAutoScroll from '../hooks/use-auto-scroll.js';\n\nconst AGENT_MESSAGE_WIDTH_CLASS = 'max-w-[max(80%,350px)]';\nconst USER_MESSAGE_WIDTH_CLASS = 'max-w-[80%]';\n\ntype ChatBodyProps = {\n agent: AiAgent;\n messages: AiNotification[];\n isThinking: boolean;\n error: string | null;\n sendMessage: (message: string) => void;\n isAnimating: boolean;\n setIsAnimating: (animating: boolean) => void;\n isPopoverOpen?: boolean;\n};\n\nconst ChatBody = ({\n agent,\n messages,\n isThinking,\n error,\n sendMessage,\n setIsAnimating,\n isAnimating,\n isPopoverOpen = false,\n}: ChatBodyProps) => {\n const previousMessageCountRef = useRef(messages.length);\n const initialMessageCountRef = useRef(messages.length);\n const { scrollAreaRef, scrollToBottom, checkIfUserAtBottom, isUserAtBottom } = useAutoScroll();\n\n const handleAnimationComplete = useCallback(() => {\n setIsAnimating(false);\n }, [setIsAnimating]);\n\n useEffect(() => {\n const lastMessage = messages[messages.length - 1];\n const hasNewMessage = messages.length > previousMessageCountRef.current;\n \n if (hasNewMessage && lastMessage?.aiRole === 'assistant') {\n setIsAnimating(true);\n }\n\n if (hasNewMessage) {\n setTimeout(() => scrollToBottom({ smooth: true }), 0);\n }\n\n previousMessageCountRef.current = messages.length;\n }, [messages, isThinking, setIsAnimating, scrollToBottom, isUserAtBottom]);\n\n useEffect(() => {\n if (isThinking) {\n setTimeout(() => scrollToBottom({ smooth: true }), 0);\n }\n }, [isThinking, scrollToBottom, isUserAtBottom]);\n\n useEffect(() => {\n if (isAnimating && isUserAtBottom) {\n const interval = setInterval(() => {\n checkIfUserAtBottom();\n if (isUserAtBottom) {\n scrollToBottom({ smooth: false });\n }\n }, 100);\n\n return () => clearInterval(interval);\n }\n }, [isAnimating, scrollToBottom, checkIfUserAtBottom, isUserAtBottom]);\n\n useEffect(() => {\n if (isPopoverOpen) {\n // Use a small delay to ensure the popover content is fully rendered\n setTimeout(() => scrollToBottom({ smooth: true }), 100);\n }\n }, [isPopoverOpen, scrollToBottom]);\n\n const handleSuggestionClick = (suggestion: AiAgentStarterSuggestion) => {\n sendMessage(suggestion.content);\n };\n\n const renderMessage = (message: AiNotification, index: number) => {\n const isUser = message.aiRole === 'user';\n const isLastMessage = index === messages.length - 1;\n const isLastAgentMessage = isLastMessage && !isUser;\n const isNewMessage = index >= initialMessageCountRef.current;\n const shouldAnimate = isLastAgentMessage && isNewMessage;\n\n return (\n <div\n key={message.id || index}\n className={cn([\n isUser\n ? `bg-bg ml-auto items-end rounded-lg py-2 px-3 ${USER_MESSAGE_WIDTH_CLASS}`\n : `mr-auto items-start ${AGENT_MESSAGE_WIDTH_CLASS}`,\n ])}\n >\n <MarkdownRenderer animate={shouldAnimate} onAnimationComplete={handleAnimationComplete}>\n {message.renderedContent || ''}\n </MarkdownRenderer>\n </div>\n );\n };\n\n return (\n <Flex direction=\"column\" className=\"grow overflow-auto bg-gray-50\">\n <ScrollArea.Root ref={scrollAreaRef} className=\"h-full grow-1 p-6\">\n <ScrollArea.Viewport className=\"h-full\">\n <Flex direction=\"column\" gap={6}>\n {agent.starterMessages?.map((msg, index) => (\n <div\n key={`message-${index}`}\n className={cn('mr-auto items-start', AGENT_MESSAGE_WIDTH_CLASS)}\n >\n <MarkdownRenderer>{msg.content || ''}</MarkdownRenderer>\n </div>\n ))}\n\n {agent.starterSuggestions && messages.length === 0 && (\n <Flex gap={2} wrap=\"wrap\">\n {agent.starterSuggestions.map((suggestion, index) => (\n <Button\n key={`suggestion-${index}`}\n variant=\"fill\"\n mode=\"subtle\"\n roundness=\"full\"\n size=\"extrasmall\"\n onPress={() => handleSuggestionClick(suggestion)}\n >\n {suggestion.label || suggestion.content}\n </Button>\n ))}\n </Flex>\n )}\n\n {messages.map((message, index) => renderMessage(message, index))}\n\n {isThinking && (\n <Text as=\"p\" size={2} className=\"mr-auto items-start text-gray-500\">\n Thinking...\n </Text>\n )}\n\n {error && (\n <Text\n as=\"p\"\n size={2}\n className=\"bg-negative-100 text-negative-900 mx-auto items-start rounded-lg py-2 px-3\"\n >\n Error: {error}\n </Text>\n )}\n </Flex>\n </ScrollArea.Viewport>\n <ScrollArea.Scrollbar orientation=\"vertical\" className=\"w-1 bg-gray-200 opacity-50\">\n <ScrollArea.Thumb className=\"flex-1 rounded-2xl bg-gray-400\" />\n </ScrollArea.Scrollbar>\n <ScrollArea.Corner />\n </ScrollArea.Root>\n </Flex>\n );\n};\n\nexport type { ChatBodyProps };\n\nexport default ChatBody;\n"],
|
|
5
|
+
"mappings": "AAsGQ,cAAAA,EA+CM,QAAAC,MA/CN,oBAtGR,UAAYC,MAAgB,8BAC5B,OAAgB,aAAAC,EAAW,UAAAC,EAAQ,eAAAC,MAAmB,QAGtD,OAAS,UAAAC,EAAQ,QAAAC,EAAM,QAAAC,MAAY,gBACnC,OAAS,MAAAC,MAAU,iBACnB,OAAS,oBAAAC,MAAwB,qCACjC,OAAOC,MAAmB,8BAE1B,MAAMC,EAA4B,yBAC5BC,EAA2B,cAa3BC,EAAW,CAAC,CAChB,MAAAC,EACA,SAAAC,EACA,WAAAC,EACA,MAAAC,EACA,YAAAC,EACA,eAAAC,EACA,YAAAC,EACA,cAAAC,EAAgB,EAClB,IAAqB,CACn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ACV,GAEJ,EACF,EACAlB,EAACE,EAAW,UAAX,CAAqB,YAAY,WAAW,UAAU,6BACrD,SAAAF,EAACE,EAAW,MAAX,CAAiB,UAAU,iCAAiC,EAC/D,EACAF,EAACE,EAAW,OAAX,EAAkB,GACrB,EACF,CAEJ,EAIA,IAAOyC,EAAQ7B",
|
|
6
6
|
"names": ["jsx", "jsxs", "ScrollArea", "useEffect", "useRef", "useCallback", "Button", "Flex", "Text", "cn", "MarkdownRenderer", "useAutoScroll", "AGENT_MESSAGE_WIDTH_CLASS", "USER_MESSAGE_WIDTH_CLASS", "ChatBody", "agent", "messages", "isThinking", "error", "sendMessage", "setIsAnimating", "isAnimating", "isPopoverOpen", "previousMessageCountRef", "initialMessageCountRef", "scrollAreaRef", "scrollToBottom", "checkIfUserAtBottom", "isUserAtBottom", "handleAnimationComplete", "lastMessage", "hasNewMessage", "interval", "handleSuggestionClick", "suggestion", "renderMessage", "message", "index", "isUser", "isLastAgentMessage", "isNewMessage", "shouldAnimate", "msg", "ChatBody_default"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dashx/react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
"clean": "rm -rf .turbo node_modules dist tokens layout *.css"
|
|
76
76
|
},
|
|
77
77
|
"dependencies": {
|
|
78
|
-
"@dashx/browser": "^0.4.
|
|
78
|
+
"@dashx/browser": "^0.4.11",
|
|
79
79
|
"@radix-ui/colors": "^3.0.0",
|
|
80
80
|
"@radix-ui/primitive": "^1.0.1",
|
|
81
81
|
"@radix-ui/react-accessible-icon": "^1.0.3",
|
package/src/hooks/use-agent.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import uuid from 'uuid-random';
|
|
2
2
|
import { useState, useEffect, useCallback } from 'react';
|
|
3
|
-
import type { AiAgent,
|
|
3
|
+
import type { AiAgent, AiNotification } from '@dashx/browser';
|
|
4
4
|
|
|
5
5
|
import useDashXProvider from './use-dashx-provider.js';
|
|
6
6
|
|
|
@@ -11,7 +11,7 @@ type UseAgentHookProps = {
|
|
|
11
11
|
type UseAgentHookResponse = {
|
|
12
12
|
agent?: AiAgent;
|
|
13
13
|
conversationId: string | null;
|
|
14
|
-
messages:
|
|
14
|
+
messages: AiNotification[];
|
|
15
15
|
isThinking: boolean;
|
|
16
16
|
error: string | null;
|
|
17
17
|
sendMessage: (text: string) => Promise<void>;
|
|
@@ -23,7 +23,7 @@ const useAgent = ({ publicEmbedKey }: UseAgentHookProps): UseAgentHookResponse =
|
|
|
23
23
|
const [agent, setAgent] = useState<AiAgent>();
|
|
24
24
|
|
|
25
25
|
const [conversationId, setConversationId] = useState<string | null>(null);
|
|
26
|
-
const [messages, setMessages] = useState<
|
|
26
|
+
const [messages, setMessages] = useState<AiNotification[]>([]);
|
|
27
27
|
const [isThinking, setIsThinking] = useState(false);
|
|
28
28
|
const [error, setError] = useState<string | null>(null);
|
|
29
29
|
|
|
@@ -48,9 +48,9 @@ const useAgent = ({ publicEmbedKey }: UseAgentHookProps): UseAgentHookResponse =
|
|
|
48
48
|
try {
|
|
49
49
|
setMessages((prev) => [...prev, {
|
|
50
50
|
id: uuid(),
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
} as
|
|
51
|
+
aiRole: 'user',
|
|
52
|
+
renderedContent: text
|
|
53
|
+
} as AiNotification]);
|
|
54
54
|
setIsThinking(true);
|
|
55
55
|
setError(null);
|
|
56
56
|
|
package/src/widgets/ChatBody.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as ScrollArea from '@radix-ui/react-scroll-area';
|
|
2
2
|
import React, { useEffect, useRef, useCallback } from 'react';
|
|
3
|
-
import type { AiAgent,
|
|
3
|
+
import type { AiAgent, AiNotification, AiAgentStarterSuggestion } from '@dashx/browser';
|
|
4
4
|
|
|
5
5
|
import { Button, Flex, Text } from '../components';
|
|
6
6
|
import { cn } from '../utils/cn.js';
|
|
@@ -12,7 +12,7 @@ const USER_MESSAGE_WIDTH_CLASS = 'max-w-[80%]';
|
|
|
12
12
|
|
|
13
13
|
type ChatBodyProps = {
|
|
14
14
|
agent: AiAgent;
|
|
15
|
-
messages:
|
|
15
|
+
messages: AiNotification[];
|
|
16
16
|
isThinking: boolean;
|
|
17
17
|
error: string | null;
|
|
18
18
|
sendMessage: (message: string) => void;
|
|
@@ -42,8 +42,8 @@ const ChatBody = ({
|
|
|
42
42
|
useEffect(() => {
|
|
43
43
|
const lastMessage = messages[messages.length - 1];
|
|
44
44
|
const hasNewMessage = messages.length > previousMessageCountRef.current;
|
|
45
|
-
|
|
46
|
-
if (hasNewMessage && lastMessage?.
|
|
45
|
+
|
|
46
|
+
if (hasNewMessage && lastMessage?.aiRole === 'assistant') {
|
|
47
47
|
setIsAnimating(true);
|
|
48
48
|
}
|
|
49
49
|
|
|
@@ -84,8 +84,8 @@ const ChatBody = ({
|
|
|
84
84
|
sendMessage(suggestion.content);
|
|
85
85
|
};
|
|
86
86
|
|
|
87
|
-
const renderMessage = (message:
|
|
88
|
-
const isUser = message.
|
|
87
|
+
const renderMessage = (message: AiNotification, index: number) => {
|
|
88
|
+
const isUser = message.aiRole === 'user';
|
|
89
89
|
const isLastMessage = index === messages.length - 1;
|
|
90
90
|
const isLastAgentMessage = isLastMessage && !isUser;
|
|
91
91
|
const isNewMessage = index >= initialMessageCountRef.current;
|
|
@@ -101,7 +101,7 @@ const ChatBody = ({
|
|
|
101
101
|
])}
|
|
102
102
|
>
|
|
103
103
|
<MarkdownRenderer animate={shouldAnimate} onAnimationComplete={handleAnimationComplete}>
|
|
104
|
-
{message.
|
|
104
|
+
{message.renderedContent || ''}
|
|
105
105
|
</MarkdownRenderer>
|
|
106
106
|
</div>
|
|
107
107
|
);
|