Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(chat): render directive syntax in user messages as a chip

M3gA-Mind9d8bd721fa63
diff --git a/app/src/components/assistant-ui/thread.directiveText.test.tsx b/app/src/components/assistant-ui/thread.directiveText.test.tsx
new file mode 100644
index 0000000000..301faa9682
--- /dev/null
+++ b/app/src/components/assistant-ui/thread.directiveText.test.tsx
@@ -0,0 +1,77 @@
+import {
+ AssistantRuntimeProvider,
+ type ThreadMessageLike,
+ unstable_defaultDirectiveFormatter,
+ useExternalStoreRuntime,
+} from '@assistant-ui/react';
+import { render } from '@testing-library/react';
+import { describe, expect, it } from 'vitest';
+
+import { Thread } from './thread';
+
+/**
+ * A slash command leaves an audit-trail chip in the composer, and that chip is
+ * directive syntax — so a user message can contain it and something has to
+ * render it.
+ *
+ * The live composer produces this without anyone opting in. `thread.tsx` builds
+ * its `/` popover from `unstable_useSlashCommandAdapter`, which returns an
+ * `action` behaviour and does not set `removeOnExecute`. In the runtime's
+ * `triggerSelectionResource`, an action whose `removeOnExecute` is falsy takes
+ * `else insertDirective()` — the typed `/clear` is *replaced* by
+ * `formatter.serialize(item)` rather than removed. With the default formatter
+ * (`:type[label]{name=id}`) and `toItem`'s `label = '/' + id`, selecting the one
+ * registered command (`clear`, globalActions.ts) writes
+ * `:command[/clear]{name=clear}` into the composer, which the user then sends.
+ *
+ * Without a `Text` part component on the user message that text renders as raw
+ * syntax. The fixture below is the serializer's own output rather than a
+ * hand-written string, so this cannot drift from what the composer actually
+ * inserts if upstream changes the syntax.
+ *
+ * Driven through `Thread` on `useExternalStoreRuntime` — the runtime family
+ * `/chat` uses — rather than through the demo, which runs
+ * `useRemoteThreadListRuntime` and proves nothing about the live route.
+ */
+const DIRECTIVE = unstable_defaultDirectiveFormatter.serialize({
+ type: 'command',
+ label: '/clear',
+ id: 'clear',
+});
+
+const messages: ThreadMessageLike[] = [
+ { role: 'user', content: [{ type: 'text', text: `${DIRECTIVE} please` }] },
+];
+
+function Harness() {
+ const runtime = useExternalStoreRuntime({
+ messages,
+ isRunning: false,
+ convertMessage: (m: ThreadMessageLike) => m,
+ onNew: async () => {},
+ });
+ return (
+ <AssistantRuntimeProvider runtime={runtime}>
+ <Thread />
+ </AssistantRuntimeProvider>
+ );
+}
+
+describe('directive syntax in a user message', () => {
+ it('renders as a chip rather than raw syntax', () => {
+ const { container } = render(<Harness />);
+
+ const chip = container.querySelector('[data-slot="directive-text-chip"]');
+ expect(
+ chip,
+ 'the composer inserts directive syntax, so it must render as a chip'
+ ).not.toBeNull();
+ expect(chip?.getAttribute('data-directive-type')).toBe('command');
+ expect(chip?.getAttribute('data-directive-id')).toBe('clear');
+ expect(chip?.textContent).toContain('/clear');
+
+ // The surrounding prose survives, and the raw syntax is gone from the DOM.
+ expect(container.textContent).toContain('please');
+ expect(container.textContent).not.toContain(':command[');
+ });
+});
diff --git a/app/src/components/assistant-ui/thread.tsx b/app/src/components/assistant-ui/thread.tsx
index a872bdc2c2..cbac7b871f 100644
--- a/app/src/components/assistant-ui/thread.tsx
+++ b/app/src/components/assistant-ui/thread.tsx
@@ -6,6 +6,7 @@ import {
UserMessageAttachments,
} from '@/components/assistant-ui/attachment';
import { ComposerTriggerPopover } from '@/components/assistant-ui/composer-trigger-popover';
+import { DirectiveText } from '@/components/assistant-ui/directive-text';
import { File } from '@/components/assistant-ui/file';
import { ThreadFollowupSuggestions } from '@/components/assistant-ui/follow-up-suggestions';
import { Image } from '@/components/assistant-ui/image';
@@ -1115,7 +1116,19 @@ const UserMessage: FC = () => {
<div className="aui-user-message-content-wrapper relative col-start-2 min-w-0">
<div className="aui-user-message-content peer bg-muted text-foreground rounded-xl px-4 py-2 wrap-break-word empty:hidden">
- <MessagePrimitive.Parts components={{ File: UserFilePart, Image: UserImagePart }} />
+ {/* `Text: DirectiveText` because the composer can put directive syntax
+ into a user message without anyone opting in. The `/` popover is
+ built from `unstable_useSlashCommandAdapter`, which returns an
+ `action` behaviour and sets no `removeOnExecute`; the runtime's
+ `triggerSelectionResource` then takes `else insertDirective()`,
+ replacing the typed `/clear` with `formatter.serialize(item)` —
+ `:command[/clear]{name=clear}` — as an audit-trail chip. Without a
+ `Text` component here that renders as raw syntax and is sent to the
+ model verbatim. Assistant text is unaffected: it renders through
+ `MarkdownText` on the part switch below, a different slot. */}
+ <MessagePrimitive.Parts
+ components={{ Text: DirectiveText, File: UserFilePart, Image: UserImagePart }}
+ />
</div>
<div className="aui-user-action-bar-wrapper absolute inset-s-0 top-1/2 -translate-x-full -translate-y-1/2 pe-2 peer-empty:hidden rtl:translate-x-full">
<UserActionBar />