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
fix(ai): Fix unowned `createEffect` + fix remounting messages (#2352)
Eric Hayes4e0b82e6f66e
diff --git a/js/app/packages/app/types.d.ts b/js/app/packages/app/types.d.tsindex e049c9c605..9775bf0fa8 100644--- a/js/app/packages/app/types.d.ts+++ b/js/app/packages/app/types.d.ts@@ -3,3 +3,10 @@ declare module '*.svg' {const c: Component<ComponentProps<'svg'>>;export default c;}++declare module '@aws-crypto/sha256-js' {+ export class Sha256 {+ update(data: string | Uint8Array): void;+ digestSync(): Uint8Array;+ }+}diff --git a/js/app/packages/block-chat/component/Chat.tsx b/js/app/packages/block-chat/component/Chat.tsxindex be5510ab03..cc0107dd1e 100644--- a/js/app/packages/block-chat/component/Chat.tsx+++ b/js/app/packages/block-chat/component/Chat.tsx@@ -45,7 +45,7 @@ import { createRenameDssEntityMutation } from '@macro-entity';import { invalidateUserQuota } from '@queries/auth';import { createCallback } from '@solid-primitives/rootless';import { ChatInput } from 'core/component/AI/component/input/ChatInput';-import { createEffect, createSignal, Show } from 'solid-js';+import { createEffect, createSignal, getOwner, Show } from 'solid-js';export function Chat(props: { data: ChatData }) {const loadedState = getChatInputStoredState(props.data.chat.id);@@ -71,6 +71,7 @@ function ChatInner(props: {data: ChatData;loadedInputText: string | undefined;}) {+ const owner = getOwner();const analytics = useAnalytics();const input = useChatInputContext();const chat = useChatContext();@@ -160,7 +161,7 @@ function ChatInner(props: {return;}- chat.attachStream(result.stream);+ chat.attachStream(result.stream, owner);invalidateUserQuota();});diff --git a/js/app/packages/core/component/AI/component/message/AssistantMessage.tsx b/js/app/packages/core/component/AI/component/message/AssistantMessage.tsxindex 63e11e42bf..2ea7a0906e 100644--- a/js/app/packages/core/component/AI/component/message/AssistantMessage.tsx+++ b/js/app/packages/core/component/AI/component/message/AssistantMessage.tsx@@ -20,6 +20,7 @@ import {createSignal,For,Match,+ mapArray,Show,Switch,} from 'solid-js';@@ -301,6 +302,23 @@ function pairToolCalls(parts: AssistantMessagePart[]): AssistantMessagePart[] {return pairParts(parts, []);}+function getAssistantPartKey(+ part: AssistantMessagePart,+ counts: Map<AssistantMessagePart['type'], number>+): string {+ if (+ part.type === 'toolCall' ||+ part.type === 'toolCallResponseJson' ||+ part.type === 'toolCallErr'+ ) {+ return `${part.type}:${part.id}`;+ }++ const count = counts.get(part.type) ?? 0;+ counts.set(part.type, count + 1);+ return `${part.type}:${count}`;+}+function AssistantMessageParts(props: {parts: AssistantMessagePart[];message: ChatMessageWithAttachments;@@ -327,20 +345,45 @@ function AssistantMessageParts(props: {return pairToolCalls(props.parts);});+ const keyedParts = createMemo(() => {+ const counts = new Map<AssistantMessagePart['type'], number>();+ const partsByKey = new Map<string, AssistantMessagePart>();+ const orderedKeys: string[] = [];++ for (const part of parts()) {+ const key = getAssistantPartKey(part, counts);+ orderedKeys.push(key);+ partsByKey.set(key, part);+ }++ return { orderedKeys, partsByKey };+ });++ const stableParts = mapArray(+ () => keyedParts().orderedKeys,+ (key) => createMemo(() => keyedParts().partsByKey.get(key))+ );+return (- <For each={parts()}>+ <For each={stableParts()}>{(part, i) => {- if (part.type === 'toolCall') {+ const currentPart = part();+ if (!currentPart) return null;++ if (currentPart.type === 'toolCall') {+ const toolCall = () =>+ currentPart as Extract<AssistantMessagePart, { type: 'toolCall' }>;+return (<RenderTool- tool_id={part.id}+ tool_id={toolCall().id}chat_id={'todo'}- json={part.json}- name={part.name}+ json={toolCall().json}+ name={toolCall().name}message_id={props.message.id}part_index={i()}type="call"- isComplete={isCompleteSelector(part.id)}+ isComplete={isCompleteSelector(toolCall().id)}renderContext={{renderContext: {isStreaming: props.isStreaming,@@ -348,14 +391,20 @@ function AssistantMessageParts(props: {}}/>);- } else if (part.type === 'toolCallResponseJson') {+ } else if (currentPart.type === 'toolCallResponseJson') {+ const toolResponse = () =>+ currentPart as Extract<+ AssistantMessagePart,+ { type: 'toolCallResponseJson' }+ >;+return (<RenderToolisComplete={true}- tool_id={part.id}+ tool_id={toolResponse().id}chat_id={'todo'}- json={part.json}- name={part.name}+ json={toolResponse().json}+ name={toolResponse().name}message_id={props.message.id}part_index={i()}type="response"@@ -366,11 +415,14 @@ function AssistantMessageParts(props: {}}/>);- } else if (part.type === 'text') {- if (part.text.trim().length > 0)+ } else if (currentPart.type === 'text') {+ const textPart = () =>+ currentPart as Extract<AssistantMessagePart, { type: 'text' }>;++ if (textPart().text.trim().length > 0)return (<ChatMessageMarkdown- text={part.text}+ text={textPart().text}generating={() => props.isStreaming}/>);diff --git a/js/app/packages/core/component/AI/state/createChatController.ts b/js/app/packages/core/component/AI/state/createChatController.tsindex 38eaac800e..9a739d3643 100644--- a/js/app/packages/core/component/AI/state/createChatController.ts+++ b/js/app/packages/core/component/AI/state/createChatController.ts@@ -3,8 +3,15 @@ import { asChatMessage } from '@core/component/AI/util/message';import { toast } from '@core/component/Toast/Toast';import type { ChatMessageStream } from '@service-connection/stream';import { getEntityStreams } from '@service-connection/stream';-import type { Accessor, Setter } from 'solid-js';-import { createEffect, createSignal, on, untrack } from 'solid-js';+import type { Accessor, Owner, Setter } from 'solid-js';+import {+ createEffect,+ createSignal,+ getOwner,+ on,+ runWithOwner,+ untrack,+} from 'solid-js';import { match } from 'ts-pattern';import {type ChatEvent,@@ -23,7 +30,7 @@ export type ChatController = {isWaiting: Accessor<boolean>;dispatch: (event: ChatEvent) => void;- attachStream: (stream: ChatMessageStream) => void;+ attachStream: (stream: ChatMessageStream, owner?: Owner | null) => void;/** Escape hatch for debug components that set stream directly */setStream: Setter<ChatMessageStream | undefined>;};@@ -64,46 +71,55 @@ export function createChatController(executeEffects(result.effects);}- function attachStream(newStream: ChatMessageStream) {+ function attachStream(newStream: ChatMessageStream, owner = getOwner()) {setStream(newStream);dispatch({ type: 'stream_connected' });- // Watch stream data for user messages and errors- createEffect(- on(- () => newStream.data(),- (data) => {- const latest = data.at(-1);- if (!latest) return;-- if (latest.type === 'error') {- const streamError =- 'stream_error' in latest ? latest.stream_error : undefined;- dispatch({- type: 'stream_error',- streamError: streamError as string | undefined,- });- return;+ const watchStream = () => {+ // Watch stream data for user messages and errors+ createEffect(+ on(+ () => newStream.data(),+ (data) => {+ const latest = data.at(-1);+ if (!latest) return;++ if (latest.type === 'error') {+ const streamError =+ 'stream_error' in latest ? latest.stream_error : undefined;+ dispatch({+ type: 'stream_error',+ streamError: streamError as string | undefined,+ });+ return;+ }++ if (latest.type === 'chat_user_message') {+ dispatch({+ type: 'stream_user_message',+ messageId: latest.message_id,+ content: latest.content,+ attachments: latest.attachments,+ });+ }}+ )+ );++ // Watch stream completion+ createEffect(() => {+ if (!newStream.isDone()) return;+ const message = asChatMessage(newStream.data());+ dispatch({ type: 'stream_done', message });+ });+ };++ if (owner) {+ runWithOwner(owner, watchStream);+ return;+ }- if (latest.type === 'chat_user_message') {- dispatch({- type: 'stream_user_message',- messageId: latest.message_id,- content: latest.content,- attachments: latest.attachments,- });- }- }- )- );-- // Watch stream completion- createEffect(() => {- if (!newStream.isDone()) return;- const message = asChatMessage(newStream.data());- dispatch({ type: 'stream_done', message });- });+ watchStream();}// Reconnect active streams on page refresh / chat switch