Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(ai): rendering (#2421)

Eric Hayes8fe5f5d29f35
diff --git a/js/app/packages/block-chat/component/Chat.tsx b/js/app/packages/block-chat/component/Chat.tsx
index cc0107dd1e..c008248ce5 100644
--- a/js/app/packages/block-chat/component/Chat.tsx
+++ b/js/app/packages/block-chat/component/Chat.tsx
@@ -161,7 +161,7 @@ function ChatInner(props: {
return;
}
- chat.attachStream(result.stream, owner);
+ chat.dispatch({ type: 'stream_connected', stream: 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.tsx
index ffdb95b724..bbd922ee11 100644
--- a/js/app/packages/core/component/AI/component/message/AssistantMessage.tsx
+++ b/js/app/packages/core/component/AI/component/message/AssistantMessage.tsx
@@ -25,7 +25,6 @@ import {
Show,
Switch,
} from 'solid-js';
-import { match } from 'ts-pattern';
function messageContentIsEmpty(content: ChatMessageContent) {
if (typeof content === 'string' || Array.isArray(content)) {
@@ -310,41 +309,68 @@ function AssistantMessageParts(props: {
const currentPart = () => part();
if (!currentPart()) return null;
- return match(currentPart()!)
- .with({ type: 'toolCall' }, (toolCall) => (
- <RenderTool
- tool_id={toolCall.id}
- chat_id={chat.chatId()}
- json={toolCall.json}
- name={toolCall.name}
- response={responseById().get(toolCall.id)}
- message_id={props.message.id}
- part_index={i()}
- isComplete={isCompleteSelector(toolCall.id)}
- renderContext={{
- renderContext: {
- isStreaming: props.isStreaming,
- },
- }}
- />
- ))
- .with({ type: 'toolCallErr' }, (toolError) => (
- <div class="px-2 py-1 text-sm text-ink-muted">
- Tool `{toolError.name}` failed: {toolError.description}
- </div>
- ))
- .with({ type: 'toolCallResponseJson' }, () => null)
- .with({ type: 'text' }, (textPart) => {
- if (textPart.text.trim().length === 0) return null;
+ const type = () => currentPart()?.type;
- return (
- <ChatMessageMarkdown
- text={textPart.text}
- generating={() => props.isStreaming}
- />
- );
- })
- .exhaustive();
+ return (
+ <Switch>
+ <Match when={type() === 'toolCall'}>
+ {(() => {
+ const toolCall = () =>
+ currentPart() as Extract<
+ AssistantMessagePart,
+ { type: 'toolCall' }
+ >;
+ return (
+ <RenderTool
+ tool_id={toolCall().id}
+ chat_id={chat.chatId()}
+ json={toolCall().json}
+ name={toolCall().name}
+ response={responseById().get(toolCall().id)}
+ message_id={props.message.id}
+ part_index={i()}
+ isComplete={isCompleteSelector(toolCall().id)}
+ renderContext={{
+ renderContext: {
+ isStreaming: props.isStreaming,
+ },
+ }}
+ />
+ );
+ })()}
+ </Match>
+ <Match when={type() === 'toolCallErr'}>
+ {(() => {
+ const toolError = () =>
+ currentPart() as Extract<
+ AssistantMessagePart,
+ { type: 'toolCallErr' }
+ >;
+ return (
+ <div class="px-2 py-1 text-sm text-ink-muted">
+ Tool `{toolError().name}` failed: {toolError().description}
+ </div>
+ );
+ })()}
+ </Match>
+ <Match when={type() === 'text'}>
+ {(() => {
+ const text = () => {
+ const p = currentPart();
+ return p?.type === 'text' ? p.text : '';
+ };
+ return (
+ <Show when={text().trim().length > 0}>
+ <ChatMessageMarkdown
+ text={text()}
+ generating={() => props.isStreaming}
+ />
+ </Show>
+ );
+ })()}
+ </Match>
+ </Switch>
+ );
}}
</For>
);
diff --git a/js/app/packages/core/component/AI/component/message/ChatMessages.tsx b/js/app/packages/core/component/AI/component/message/ChatMessages.tsx
index ddffad20af..bd460ad5b6 100644
--- a/js/app/packages/core/component/AI/component/message/ChatMessages.tsx
+++ b/js/app/packages/core/component/AI/component/message/ChatMessages.tsx
@@ -57,7 +57,7 @@ export function ChatMessages(props: ChatMessagesProps) {
const messages = chat.messages;
const stream = chat.stream;
- const extendedStream = createMemo(() => {
+ const timedStream = createMemo(() => {
const s = stream();
if (!s) return;
return timeStream(idStream(s));
@@ -66,7 +66,7 @@ export function ChatMessages(props: ChatMessagesProps) {
const [messageTimingMap, setTiming] = createStore<Record<string, number>>({});
createEffect(() => {
- const s = extendedStream();
+ const s = timedStream();
if (!s) return;
const ttft = s.timeToFirstMessageMs();
const id = s.messageId();
diff --git a/js/app/packages/core/component/AI/state/createChatController.ts b/js/app/packages/core/component/AI/state/createChatController.ts
index 9a739d3643..a8332041f1 100644
--- a/js/app/packages/core/component/AI/state/createChatController.ts
+++ b/js/app/packages/core/component/AI/state/createChatController.ts
@@ -20,6 +20,16 @@ import {
transition,
} from './chatState';
+type StreamConnectedEvent = {
+ type: 'stream_connected';
+ stream: ChatMessageStream;
+ owner?: Owner | null;
+};
+
+type ControllerEvent =
+ | Exclude<ChatEvent, { type: 'stream_connected' }>
+ | StreamConnectedEvent;
+
export type ChatController = {
chatId: Accessor<string>;
phase: Accessor<ChatPhase>;
@@ -29,8 +39,7 @@ export type ChatController = {
isGenerating: Accessor<boolean>;
isWaiting: Accessor<boolean>;
- dispatch: (event: ChatEvent) => void;
- attachStream: (stream: ChatMessageStream, owner?: Owner | null) => void;
+ dispatch: (event: ControllerEvent) => void;
/** Escape hatch for debug components that set stream directly */
setStream: Setter<ChatMessageStream | undefined>;
};
@@ -58,68 +67,73 @@ export function createChatController(
}
}
- function dispatch(event: ChatEvent) {
- const result = transition(untrack(phase), event);
- setPhase(result.phase);
- if (result.messages) {
- setMessages(result.messages);
- }
- // Clear stream on transition to idle
- if (result.phase.type === 'idle' && untrack(stream)) {
- setStream(undefined);
- }
- executeEffects(result.effects);
- }
-
- function attachStream(newStream: ChatMessageStream, owner = getOwner()) {
- setStream(newStream);
- dispatch({ type: 'stream_connected' });
-
- 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') {
+ function watchStream(newStream: ChatMessageStream) {
+ // Watch stream data for user messages and errors
+ createEffect(
+ on(
+ () => newStream.data(),
+ (data) => {
+ const latest = data.at(-1);
+ if (!latest) return;
+
+ match(latest)
+ .with({ type: 'error' }, (r) => {
const streamError =
- 'stream_error' in latest ? latest.stream_error : undefined;
+ 'stream_error' in r ? r.stream_error : undefined;
dispatch({
type: 'stream_error',
streamError: streamError as string | undefined,
});
- return;
- }
-
- if (latest.type === 'chat_user_message') {
+ })
+ .with({ type: 'chat_user_message' }, (r) => {
dispatch({
type: 'stream_user_message',
- messageId: latest.message_id,
- content: latest.content,
- attachments: latest.attachments,
+ messageId: r.message_id,
+ content: r.content,
+ attachments: r.attachments,
});
- }
- }
- )
- );
-
- // Watch stream completion
- createEffect(() => {
- if (!newStream.isDone()) return;
- const message = asChatMessage(newStream.data());
- dispatch({ type: 'stream_done', message });
- });
- };
-
- if (owner) {
- runWithOwner(owner, watchStream);
+ })
+ .otherwise(() => {});
+ }
+ )
+ );
+
+ // Watch stream completion
+ createEffect(() => {
+ if (!newStream.isDone()) return;
+ const message = asChatMessage(newStream.data());
+ dispatch({ type: 'stream_done', message });
+ });
+ }
+
+ function dispatch(event: ControllerEvent) {
+ // Handle stream attachment through the state transition
+ if (event.type === 'stream_connected' && 'stream' in event) {
+ const { stream: newStream, owner = getOwner() } = event;
+ setStream(newStream);
+
+ const result = transition(untrack(phase), { type: 'stream_connected' });
+ setPhase(result.phase);
+ executeEffects(result.effects);
+
+ if (owner) {
+ runWithOwner(owner, () => watchStream(newStream));
+ } else {
+ watchStream(newStream);
+ }
return;
}
- watchStream();
+ const result = transition(untrack(phase), event);
+ setPhase(result.phase);
+ if (result.messages) {
+ setMessages(result.messages);
+ }
+ // Clear stream on transition to idle
+ if (result.phase.type === 'idle' && untrack(stream)) {
+ setStream(undefined);
+ }
+ executeEffects(result.effects);
}
// Reconnect active streams on page refresh / chat switch
@@ -144,7 +158,7 @@ export function createChatController(
continue;
}
- attachStream(s);
+ dispatch({ type: 'stream_connected', stream: s });
break;
}
});
@@ -159,7 +173,6 @@ export function createChatController(
isWaiting: () => phase().type === 'sending',
dispatch,
- attachStream,
setStream,
};
}