Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(ai): improve tool rendering (#1767)
Eric Hayes7a0aecffe4e7
diff --git a/js/app/packages/core/component/AI/component/message/AssistantMessage.tsx b/js/app/packages/core/component/AI/component/message/AssistantMessage.tsxindex a04848ff15..63e11e42bf 100644--- a/js/app/packages/core/component/AI/component/message/AssistantMessage.tsx+++ b/js/app/packages/core/component/AI/component/message/AssistantMessage.tsx@@ -266,6 +266,41 @@ export function AssistantMessage(props: {);}+// tool response should follow tool call+function pairToolCalls(parts: AssistantMessagePart[]): AssistantMessagePart[] {+ function takeResult(+ parts: AssistantMessagePart[],+ callId: string+ ): [AssistantMessagePart | undefined, AssistantMessagePart[]] {+ const ti = parts.findIndex(+ (p) => p.type === 'toolCallResponseJson' && p.id === callId+ );+ if (ti === -1) return [undefined, parts];+ const response = parts[ti];+ parts.splice(ti, 1);+ return [response, parts];+ }++ function pairParts(+ parts: AssistantMessagePart[],+ newParts: AssistantMessagePart[]+ ): AssistantMessagePart[] {+ if (parts.length === 0) return newParts;+ const first = parts[0];+ if (first.type === 'toolCall') {+ const [maybeResponse, rest] = takeResult(parts.slice(1), first.id);+ newParts.push(first);+ if (maybeResponse) newParts.push(maybeResponse);+ return pairParts(rest, newParts);+ } else {+ newParts.push(first);+ return pairParts(parts.slice(1), newParts);+ }+ }++ return pairParts(parts, []);+}+function AssistantMessageParts(props: {parts: AssistantMessagePart[];message: ChatMessageWithAttachments;@@ -288,8 +323,12 @@ function AssistantMessageParts(props: {(id: string, completed) => completed.has(id));+ const parts = createMemo(() => {+ return pairToolCalls(props.parts);+ });+return (- <For each={props.parts}>+ <For each={parts()}>{(part, i) => {if (part.type === 'toolCall') {return (diff --git a/js/app/packages/core/component/AI/component/tool/BaseTool.tsx b/js/app/packages/core/component/AI/component/tool/BaseTool.tsxindex 9ff0a8f206..1c0e29a628 100644--- a/js/app/packages/core/component/AI/component/tool/BaseTool.tsx+++ b/js/app/packages/core/component/AI/component/tool/BaseTool.tsx@@ -1,28 +1,40 @@-import MacroGridLoader from '@macro-icons/macro-grid-noise-loader-4.svg';import { type Component, type JSX, Show } from 'solid-js';import type { RenderContext } from './ToolRenderer';-export function BaseTool(props: {+type ToolCallProps = {icon: Component<JSX.SvgSVGAttributes<SVGSVGElement>>;- text: string;- children?: JSX.Element;renderContext: RenderContext['renderContext'];- type: 'call' | 'response';-}) {+ type: 'call';+ children: JSX.Element;+};+type ToolResponseProps = {+ children: JSX.Element;+ renderContext: RenderContext['renderContext'];+ type: 'response';+};++function BaseToolCall(props: ToolCallProps) {return (- <div class="relative text-sm text-ink-extra-muted my-3 py-1 border-l pl-4 border-edge">- <div class="flex gap-x-2 items-center mb-0.5">- <Show- when={props.renderContext.isStreaming && props.type === 'call'}- fallback={- <props.icon class="h-[20px] w-[20px] shrink-0 text-accent" />- }- >- <MacroGridLoader width={20} height={20} class="text-accent" />+ <div class="relative text-sm text-ink-extra-muted border-l pl-4 border-edge">+ <div class="flex gap-x-2 items-center">+ <Show when={props.type === 'call'}>+ <props.icon class="h-[20px] w-[20px] shrink-0 text-accent" />+ <div class="p-2">{props.children}</div></Show>- <div class="p-2">{props.text}</div></div>+ </div>+ );+}++function BaseToolResponse(props: ToolResponseProps) {+ return (+ <div class="relative text-sm text-ink-extra-muted border-l pl-4 border-edge mb-2"><div class="pl-8">{props.children && props.children}</div></div>);}++export function BaseTool(props: ToolCallProps | ToolResponseProps) {+ if (props.type === 'call') return BaseToolCall(props);+ return BaseToolResponse(props);+}diff --git a/js/app/packages/core/component/AI/component/tool/BashCodeExecution.tsx b/js/app/packages/core/component/AI/component/tool/BashCodeExecution.tsxindex d61ee51aef..8ed74abbe5 100644--- a/js/app/packages/core/component/AI/component/tool/BashCodeExecution.tsx+++ b/js/app/packages/core/component/AI/component/tool/BashCodeExecution.tsx@@ -89,12 +89,7 @@ function BashResult(props: { result: BashCodeExecutionResult }) {const handler = createToolRenderer({name: 'bash_code_execution',renderCall: (ctx) => (- <BaseTool- icon={Terminal}- text="Executing bash"- renderContext={ctx.renderContext}- type="call"- >+ <BaseTool icon={Terminal} renderContext={ctx.renderContext} type="call"><code class="text-ink-muted font-mono text-xs">{truncateCommand(ctx.tool.data.command)}</code>@@ -104,12 +99,7 @@ const handler = createToolRenderer({const isError =ctx.tool.data.content.type === 'bash_code_execution_tool_result_error';return (- <BaseTool- icon={Terminal}- text="Executed bash"- renderContext={ctx.renderContext}- type="response"- >+ <BaseTool renderContext={ctx.renderContext} type="response"><Switch><Match when={isError}><span class="text-ink-error">Execution failed</span>diff --git a/js/app/packages/core/component/AI/component/tool/ListEntities.tsx b/js/app/packages/core/component/AI/component/tool/ListEntities.tsxindex 9efe190708..ddd75e8545 100644--- a/js/app/packages/core/component/AI/component/tool/ListEntities.tsx+++ b/js/app/packages/core/component/AI/component/tool/ListEntities.tsx@@ -94,8 +94,15 @@ const ListEntitiesToolResponse = (props: {};return (- <Show when={results().length > 0}>- <div class="border border-edge rounded w-full">+ <div class="border border-edge rounded w-full">+ <Show+ when={props.items.length > 0}+ fallback={+ <div class="flex items-center justify-between w-full text-left p-2 hover:bg-hover transition-colors">+ No Results+ </div>+ }+ ><buttonclass={`flex items-center justify-between w-full text-left p-2 hover:bg-hover transition-colors ${isExpanded() ? 'rounded-t border-b border-edge' : 'rounded'@@ -118,66 +125,67 @@ const ListEntitiesToolResponse = (props: {</Show></div></button>+ </Show>- <Show when={isExpanded()}>- <div class="max-h-[480px] overflow-hidden">- <VList- data={results()}- bufferSize={5 * 32}- itemSize={32}- style={{- height: `${Math.min(results().length * 32, 480)}px`,- contain: 'content',- }}- >- {(item) => {- const clickHandler = getClickHandler(item);+ <Show when={isExpanded()}>+ <div class="max-h-[480px] overflow-hidden">+ <VList+ data={results()}+ bufferSize={5 * 32}+ itemSize={32}+ style={{+ height: `${Math.min(results().length * 32, 480)}px`,+ contain: 'content',+ }}+ >+ {(item) => {+ const clickHandler = getClickHandler(item);- return (- <div- class="flex items-center w-full h-8 px-2 hover:bg-hover transition-colors"- onClick={clickHandler}- >- <div class="flex items-center flex-1 min-w-0 gap-2">- <EntityIcon- size="sm"- targetType={getIconType(item)}- shared={false}- />- <div class="flex-1 min-w-0">- <TruncatedText size="sm">- <span>{getItemTitle(item)}</span>- </TruncatedText>- </div>+ return (+ <div+ class="flex items-center w-full h-8 px-2 hover:bg-hover transition-colors"+ onClick={clickHandler}+ >+ <div class="flex items-center flex-1 min-w-0 gap-2">+ <EntityIcon+ size="sm"+ targetType={getIconType(item)}+ shared={false}+ />+ <div class="flex-1 min-w-0">+ <TruncatedText size="sm">+ <span>{getItemTitle(item)}</span>+ </TruncatedText></div></div>- );- }}- </VList>- </div>- </Show>- </div>- </Show>+ </div>+ );+ }}+ </VList>+ </div>+ </Show>+ </div>);};const handler = createToolRenderer({name: 'ListEntities',renderCall: (ctx) => (- <BaseTool- icon={List}- text="Browsing workspace..."- renderContext={ctx.renderContext}- type="call"- />+ <BaseTool icon={List} renderContext={ctx.renderContext} type="call">+ Filter for{' '}+ <span class="text-accent">+ {ctx.tool.data.includeTypes+ ? ctx.tool.data.includeTypes.join(', ')+ : 'All'}+ </span>{' '}+ ordered by{' '}+ <span class="text-accent">+ {ctx.tool.data.sortBy.split('_').join(' ')}+ </span>+ </BaseTool>),renderResponse: (ctx) => (- <BaseTool- icon={List}- text="Found items"- renderContext={ctx.renderContext}- type="response"- >+ <BaseTool renderContext={ctx.renderContext} type="response"><ListEntitiesToolResponseitems={ctx.tool.data.items}summary={ctx.tool.data.summary}diff --git a/js/app/packages/core/component/AI/component/tool/Read.tsx b/js/app/packages/core/component/AI/component/tool/Read.tsxindex 6c3433b5d6..eab5733129 100644--- a/js/app/packages/core/component/AI/component/tool/Read.tsx+++ b/js/app/packages/core/component/AI/component/tool/Read.tsx@@ -4,43 +4,16 @@ import { createToolRenderer } from './ToolRenderer';const handler = createToolRenderer({name: 'Read',- renderCall: (ctx) => {- const getDisplayText = () => {- switch (ctx.tool.data.contentType) {- case 'document':- return `Reading document...`;- case 'channel':- return `Reading channel...`;- case 'chat-thread':- return `Reading AI chat thread...`;- case 'chat-message':- return `Reading AI chat message...`;- case 'email-thread':- return `Reading email thread...`;- case 'email-message':- return `Reading email message...`;- default:- return 'Reading content...';- }- };-- return (- <BaseTool- type="call"- icon={EyeIcon}- text={getDisplayText()}- renderContext={ctx.renderContext}- />- );- },- renderResponse: (ctx) => (- <BaseTool- type="response"- icon={EyeIcon}- text="Finished Reading"- renderContext={ctx.renderContext}- />+ renderCall: (ctx) => (+ <BaseTool type="call" icon={EyeIcon} renderContext={ctx.renderContext}>+ Read{' '}+ <span class="text-accent">+ {ctx.tool.data.ids.length}{' '}+ {ctx.tool.data.contentType + (ctx.tool.data.ids.length > 1 ? 's' : '')}+ </span>+ </BaseTool>),+ renderResponse: (_) => undefined,});export const readHandler = handler;diff --git a/js/app/packages/core/component/AI/component/tool/Search.tsx b/js/app/packages/core/component/AI/component/tool/Search.tsxindex 018e102beb..b0b22a9f67 100644--- a/js/app/packages/core/component/AI/component/tool/Search.tsx+++ b/js/app/packages/core/component/AI/component/tool/Search.tsx@@ -1,4 +1,5 @@import { EntityIcon } from '@core/component/EntityIcon';+import type { ToolContext } from '@service-cognition/generated/tools/tool';import { TruncatedText } from '@core/component/FileList/TruncatedText';import { fileTypeToBlockName } from '@core/constant/allBlocks';import ChevronDown from '@icon/regular/caret-down.svg?component-solid';@@ -100,8 +101,15 @@ const UnifiedSearchToolResponse = (props: {};return (- <Show when={results().length > 0}>- <div class="border border-edge rounded w-full">+ <div class="border border-edge rounded w-full">+ <Show+ when={props.results.length > 0}+ fallback={+ <div class="flex items-center justify-between w-full text-left p-2 transition-colors">+ No Results+ </div>+ }+ ><buttonclass={`flex items-center justify-between w-full text-left p-2 hover:bg-hover transition-colors ${isExpanded() ? 'rounded-t border-b border-edge' : 'rounded'@@ -122,71 +130,81 @@ const UnifiedSearchToolResponse = (props: {</Show></div></button>+ </Show>- <Show when={isExpanded()}>- <div class="max-h-[480px] overflow-hidden">- <VList- data={results()}- bufferSize={5 * 32}- itemSize={32}- style={{- height: `${Math.min(results().length * 32, 480)}px`,- contain: 'content',- }}- >- {(result) => {- const clickHandler = getClickHandler(result);+ <Show when={isExpanded()}>+ <div class="max-h-[480px] overflow-hidden">+ <VList+ data={results()}+ bufferSize={5 * 32}+ itemSize={32}+ style={{+ height: `${Math.min(results().length * 32, 480)}px`,+ contain: 'content',+ }}+ >+ {(result) => {+ const clickHandler = getClickHandler(result);- return (- <div- class="flex items-center w-full h-8 px-2 hover:bg-hover transition-colors"- onClick={clickHandler}- >- <div class="flex items-center flex-1 min-w-0 gap-2">- <EntityIcon- size="sm"- targetType={- result.type === 'document'- ? (result.file_type as FileType)- : result.type- }- shared={false}- />- <div class="flex-1 min-w-0">- <TruncatedText size="sm">- <span>{getResultTitle(result)}</span>- </TruncatedText>- </div>+ return (+ <div+ class="flex items-center w-full h-8 px-2 hover:bg-hover transition-colors"+ onClick={clickHandler}+ >+ <div class="flex items-center flex-1 min-w-0 gap-2">+ <EntityIcon+ size="sm"+ targetType={+ result.type === 'document'+ ? (result.file_type as FileType)+ : result.type+ }+ shared={false}+ />+ <div class="flex-1 min-w-0">+ <TruncatedText size="sm">+ <span>{getResultTitle(result)}</span>+ </TruncatedText></div></div>- );- }}- </VList>- </div>- </Show>- </div>- </Show>+ </div>+ );+ }}+ </VList>+ </div>+ </Show>+ </div>);};+function SearchText(props: {+ ctx: ToolContext<NamedTool<'ContentSearch' | 'NameSearch', 'call'>>;+}) {+ const ctx = props.ctx;+ const queryString =+ 'query' in ctx.tool.data ? ctx.tool.data.query : ctx.tool.data.name;++ return (+ <div>+ Search <span class="text-accent"> {queryString} </span>+ </div>+ );+}+const createHandler = (name: 'NameSearch' | 'ContentSearch') =>createToolRenderer({name,renderCall: (ctx) => (<BaseToolicon={MagnifyingGlass}- text="Searching..."renderContext={ctx.renderContext}type="call"- />+ >+ <SearchText ctx={ctx} />+ </BaseTool>),renderResponse: (ctx) => (- <BaseTool- icon={MagnifyingGlass}- text="Found"- renderContext={ctx.renderContext}- type="response"- >+ <BaseTool renderContext={ctx.renderContext} type="response"><UnifiedSearchToolResponse results={ctx.tool.data.results} /></BaseTool>),diff --git a/js/app/packages/core/component/AI/component/tool/TextEditorCodeExecution.tsx b/js/app/packages/core/component/AI/component/tool/TextEditorCodeExecution.tsxindex 67e8e7477d..34293132ff 100644--- a/js/app/packages/core/component/AI/component/tool/TextEditorCodeExecution.tsx+++ b/js/app/packages/core/component/AI/component/tool/TextEditorCodeExecution.tsx@@ -61,19 +61,6 @@ function getFileNameFromPath(path: string): string {const MAX_OUTPUT_LINES = 5;-function getCommandLabel(command: string): string {- switch (command) {- case 'view':- return 'Viewing file';- case 'create':- return 'Creating file';- case 'str_replace':- return 'Editing file';- default:- return 'Editing file';- }-}-function CodeFence(props: { content: string; maxLines: number }) {const [expanded, setExpanded] = createSignal(false);@@ -317,23 +304,13 @@ const handler = createToolRenderer({}},renderCall: (ctx) => (- <BaseTool- icon={File}- text={getCommandLabel(ctx.tool.data.command)}- renderContext={ctx.renderContext}- type="call"- >+ <BaseTool icon={File} renderContext={ctx.renderContext} type="call"><code class="text-ink-muted font-mono text-xs">{ctx.tool.data.path}</code></BaseTool>),renderResponse: (ctx) => {return (- <BaseTool- icon={File}- text="File operation"- renderContext={ctx.renderContext}- type="response"- >+ <BaseTool renderContext={ctx.renderContext} type="response"><TextEditorResultcontent={ctx.tool.data.content}toolId={ctx.tool.id}diff --git a/js/app/packages/core/component/AI/component/tool/WebFetch.tsx b/js/app/packages/core/component/AI/component/tool/WebFetch.tsxindex 0cd25a2e72..97f09c30d2 100644--- a/js/app/packages/core/component/AI/component/tool/WebFetch.tsx+++ b/js/app/packages/core/component/AI/component/tool/WebFetch.tsx@@ -1,43 +1,13 @@import Globe from '@phosphor-icons/core/regular/globe.svg';-import { Match, Switch } from 'solid-js';import { BaseTool } from './BaseTool';import { createToolRenderer } from './ToolRenderer';// Runtime type for successful web fetch (schema differs from generated types)-type WebFetchSuccess = {- content: {- title?: string | null;- source: { data: string; media_type: string; type: 'text' | 'base64' };- citations?: { enabled: boolean } | null;- };- retrieved_at: string;- url: string;-};--// Runtime type for error web fetch-type WebFetchError = {- error_code: string;-};--function isWebFetchError(content: unknown): content is WebFetchError {- return (- typeof content === 'object' && content !== null && 'error_code' in content- );-}--function isWebFetchSuccess(content: unknown): content is WebFetchSuccess {- return typeof content === 'object' && content !== null && 'url' in content;-}-const handler = createToolRenderer({name: 'web_fetch',renderCall: (ctx) => (- <BaseTool- icon={Globe}- text="Fetching"- renderContext={ctx.renderContext}- type="call"- >+ <BaseTool icon={Globe} renderContext={ctx.renderContext} type="call">+ Fetched{' '}<ahref={ctx.tool.data.url}target="_blank"@@ -48,36 +18,7 @@ const handler = createToolRenderer({</a></BaseTool>),- renderResponse: (ctx) => {- const content = ctx.tool.data.content as unknown;- return (- <BaseTool- icon={Globe}- text="Fetched"- renderContext={ctx.renderContext}- type="response"- >- <Switch>- <Match when={isWebFetchError(content)}>- <div class="text-ink-error">- Error: {(content as WebFetchError).error_code}- </div>- </Match>- <Match when={isWebFetchSuccess(content)}>- <a- href={(content as WebFetchSuccess).url}- target="_blank"- rel="noopener noreferrer"- class="text-accent hover:underline"- >- {(content as WebFetchSuccess).content?.title ??- (content as WebFetchSuccess).url}- </a>- </Match>- </Switch>- </BaseTool>- );- },+ renderResponse: (_) => undefined,});export const webFetchHandler = handler;diff --git a/js/app/packages/core/component/AI/component/tool/WebSearch.tsx b/js/app/packages/core/component/AI/component/tool/WebSearch.tsxindex 1e9e2c35ed..2d1dc77fb4 100644--- a/js/app/packages/core/component/AI/component/tool/WebSearch.tsx+++ b/js/app/packages/core/component/AI/component/tool/WebSearch.tsx@@ -6,22 +6,12 @@ import { createToolRenderer } from './ToolRenderer';const handler = createToolRenderer({name: 'web_search',renderCall: (ctx) => (- <BaseTool- icon={Globe}- text="Searching For"- renderContext={ctx.renderContext}- type="call"- >- <div class="italic">{ctx.tool.data.query}</div>+ <BaseTool icon={Globe} renderContext={ctx.renderContext} type="call">+ Searched for <span class="text-accent">{ctx.tool.data.query}</span></BaseTool>),renderResponse: (ctx) => (- <BaseTool- icon={Globe}- text="Search Results"- renderContext={ctx.renderContext}- type="response"- >+ <BaseTool renderContext={ctx.renderContext} type="response"><UnfurledLinkCollectionlinks={ctx.tool.data.content.map((result) => ({title: result.title,diff --git a/js/app/packages/core/component/AI/component/tool/handler.tsx b/js/app/packages/core/component/AI/component/tool/handler.tsxindex ef9a0f40e1..6d8860e5d8 100644--- a/js/app/packages/core/component/AI/component/tool/handler.tsx+++ b/js/app/packages/core/component/AI/component/tool/handler.tsx@@ -8,8 +8,7 @@ import {type ToolHandlerMap,type ToolName,} from '@service-cognition/generated/tools/tool';-import { createStore } from 'solid-js/store';-import { Dynamic, Show } from 'solid-js/web';+import { Dynamic } from 'solid-js/web';import { bashCodeExecutionHandler } from './BashCodeExecution';import { listEntitiesHandler } from './ListEntities';import { readHandler } from './Read';@@ -19,10 +18,6 @@ import type { RenderContext } from './ToolRenderer';import { webFetchHandler } from './WebFetch';import { webSearchHandler } from './WebSearch';-const [renderStore, setRenderStore] = createStore<- Record<string, 'call' | 'response'>->({});-const toolHandlers: ToolHandlerMap<RenderContext> = {ListEntities: listEntitiesHandler,bash_code_execution: bashCodeExecutionHandler,@@ -59,7 +54,6 @@ export function RenderTool(props: ToolProps) {});if (isErr(maybeTool)) return null;const tool = maybeTool[1];- setRenderStore(props.tool_id, 'call');handler = toolHandlers[tool.name].call;const ctx: ToolContext<NamedTool<ToolName, 'call'>> = {chat_id: props.chat_id,@@ -75,7 +69,6 @@ export function RenderTool(props: ToolProps) {json: props.json,name: props.name as ToolName,});- setRenderStore(props.tool_id, 'response');if (isErr(maybeTool)) return null;const tool = maybeTool[1];handler = toolHandlers[tool.name].response;@@ -92,15 +85,13 @@ export function RenderTool(props: ToolProps) {}return (- <Show when={props.type === renderStore[props.tool_id]}>- <Dynamic- component={handler.render}- {...context}- renderContext={{- isStreaming: props.renderContext.renderContext.isStreaming,- }}- />- </Show>+ <Dynamic+ component={handler.render}+ {...context}+ renderContext={{+ isStreaming: props.renderContext.renderContext.isStreaming,+ }}+ />);}