Skip to content

The agent that grows with you

fix(desktop): tool media renderers see bridged plugin tools and own their block

Tekniumcff668d7e903
diff --git a/apps/desktop/src/components/assistant-ui/markdown-text.tsx b/apps/desktop/src/components/assistant-ui/markdown-text.tsx
index ca5cd1e543b..fec68d220b7 100644
--- a/apps/desktop/src/components/assistant-ui/markdown-text.tsx
+++ b/apps/desktop/src/components/assistant-ui/markdown-text.tsx
@@ -8,7 +8,16 @@ import {
tailBoundedRemend
} from '@assistant-ui/react-streamdown'
import type { code as streamdownCode } from '@streamdown/code'
-import { type ComponentProps, isValidElement, memo, type ReactNode, useEffect, useMemo, useState } from 'react'
+import {
+ Children,
+ type ComponentProps,
+ isValidElement,
+ memo,
+ type ReactNode,
+ useEffect,
+ useMemo,
+ useState
+} from 'react'
import { defaultRemarkPlugins } from 'streamdown'
import { ExpandableBlock } from '@/components/chat/expandable-block'
@@ -17,6 +26,7 @@ import { chunkByLines, SyntaxHighlighter } from '@/components/chat/shiki-highlig
import { TranscriptVideo } from '@/components/chat/transcript-video'
import { ZoomableImage } from '@/components/chat/zoomable-image'
import { ErrorBoundary } from '@/components/error-boundary'
+import { useContributions } from '@/contrib'
import { useMediaImage } from '@/hooks/use-media-image'
import { detectArtifact } from '@/lib/artifact-detect'
import { renderMediaTags } from '@/lib/chat-messages/parts'
@@ -39,6 +49,7 @@ import {
import { previewTargetFromMarkdownHref } from '@/lib/preview-targets'
import { remarkSoftBreaks } from '@/lib/remark-soft-breaks'
import { sessionRefFromMarkdownHref } from '@/lib/session-refs'
+import { TOOL_MEDIA_RENDERER_AREA } from '@/lib/tool-media-renderers'
import { remarkTranscriptDirectives } from '@/lib/transcript-directives'
import { cn } from '@/lib/utils'
import { useForcedTextDirection } from '@/store/text-direction'
@@ -581,6 +592,9 @@ function MarkdownParagraph({
}: ComponentProps<'p'> & { scratchpad?: boolean; streaming?: boolean }) {
const plain = paragraphPlainText(children)
const resolved = useResolvedParagraph(scratchpad ? null : plain)
+ // A plugin's tool media renderer draws block content (a card, a viewer), which
+ // a <p> cannot hold; a paragraph delivering a MEDIA: file becomes a <div> then.
+ const blockMedia = useContributions(TOOL_MEDIA_RENDERER_AREA).length > 0 && holdsMediaLink(children)
// Vertical rhythm is owned by styles.css (`--paragraph-gap`), which must
// out-specify Tailwind Typography's `prose` margins — so no `my-*` here.
@@ -604,6 +618,14 @@ function MarkdownParagraph({
)
}
+ if (blockMedia) {
+ return (
+ <div className={paragraphClass} data-tool-media-paragraph="">
+ {children}
+ </div>
+ )
+ }
+
return (
<p className={paragraphClass} {...props}>
{children}
@@ -611,6 +633,13 @@ function MarkdownParagraph({
)
}
+function holdsMediaLink(children: ReactNode): boolean {
+ return Children.toArray(children).some(
+ child =>
+ isValidElement<{ href?: unknown }>(child) && mediaPathFromMarkdownHref(String(child.props.href ?? '')) !== null
+ )
+}
+
function MarkdownTextSurface({
containerClassName,
containerProps,
diff --git a/apps/desktop/src/components/assistant-ui/tool-media-slot.test.tsx b/apps/desktop/src/components/assistant-ui/tool-media-slot.test.tsx
index 6ca2690e07d..5a8ba0c8cb4 100644
--- a/apps/desktop/src/components/assistant-ui/tool-media-slot.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/tool-media-slot.test.tsx
@@ -1,6 +1,6 @@
// @vitest-environment jsdom
import { MessagePrimitive, type ThreadMessage, ThreadPrimitive } from '@assistant-ui/react'
-import { cleanup, render, screen, waitFor } from '@testing-library/react'
+import { act, cleanup, render, screen, waitFor } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, type MockInstance, vi } from 'vitest'
import { $pluginRecords } from '@/contrib/plugins-store'
@@ -22,21 +22,32 @@ const SFX_PATH = '/home/u/.hermes/cache/generated/audio/boom.mp3'
const TTS_PATH = '/home/u/.hermes/cache/audio/tts_reply.ogg'
const UPLOAD_PATH = '/home/u/Music/upload.mp3'
-function turn(): ThreadMessage[] {
+function turn(bridged = false): ThreadMessage[] {
+ const sfxCall = bridged
+ ? {
+ type: 'tool-call',
+ toolCallId: 'call-sfx',
+ toolName: 'tool_call',
+ args: { calls: [{ name: 'sfx_make', arguments: { prompt: 'boom' } }] },
+ argsText: '{}',
+ result: { file_path: SFX_PATH, media_tag: `MEDIA:${SFX_PATH}` }
+ }
+ : {
+ type: 'tool-call',
+ toolCallId: 'call-sfx',
+ toolName: 'sfx_make',
+ args: { prompt: 'boom' },
+ argsText: '{"prompt":"boom"}',
+ result: { file_path: SFX_PATH, media_tag: `MEDIA:${SFX_PATH}` }
+ }
+
return [
userMessage('u1', 'make a boom and say hi'),
{
id: 'a1',
role: 'assistant',
content: [
- {
- type: 'tool-call',
- toolCallId: 'call-sfx',
- toolName: 'sfx_make',
- args: { prompt: 'boom' },
- argsText: '{"prompt":"boom"}',
- result: { file_path: SFX_PATH, media_tag: `MEDIA:${SFX_PATH}` }
- },
+ sfxCall,
{
type: 'tool-call',
toolCallId: 'call-tts',
@@ -63,9 +74,9 @@ const Assistant = () => (
</MessagePrimitive.Root>
)
-function renderTurn() {
+function renderTurn(bridged = false) {
return render(
- <ThreadRuntime messages={turn()}>
+ <ThreadRuntime messages={turn(bridged)}>
<ThreadPrimitive.Root>
<ThreadPrimitive.Messages components={{ AssistantMessage: Assistant, UserMessage: () => null }} />
</ThreadPrimitive.Root>
@@ -125,6 +136,7 @@ describe('tool media renderers', () => {
it('renders the owning plugin component for the file its tool produced, and the built-in player for the rest', async () => {
disposers.push(claim('sfx', ['sfx_make']))
+ const error = vi.spyOn(console, 'error').mockImplementation(() => {})
const { container } = renderTurn()
await waitFor(() => expect(screen.getByTestId('card-sfx').textContent).toBe('boom.mp3'))
@@ -140,6 +152,18 @@ describe('tool media renderers', () => {
expect(props.file).toMatchObject({ kind: 'audio', name: 'boom.mp3', path: SFX_PATH })
expect(typeof props.file.resolveUrl).toBe('function')
expect(props.fallback).toBeTruthy()
+ // The plugin's block card never lands inside a <p> (invalid DOM nesting).
+ expect(screen.getByTestId('card-sfx').closest('p')).toBeNull()
+ expect(error.mock.calls.map(call => String(call[0])).join('\n')).not.toMatch(/descendant/)
+ error.mockRestore()
+ })
+
+ it('recognises a plugin tool the model ran through the deferred-tool bridge (tool_call)', async () => {
+ disposers.push(claim('sfx', ['sfx_make']))
+ renderTurn(true)
+
+ await waitFor(() => expect(screen.getByTestId('card-sfx').textContent).toBe('boom.mp3'))
+ expect(seen.at(-1)).toMatchObject({ args: { prompt: 'boom' }, toolName: 'sfx_make' })
})
it('refuses a claim on a core tool or another plugin’s tool, with a console warning', async () => {
@@ -169,7 +193,7 @@ describe('tool media renderers', () => {
const { container } = renderTurn()
await waitFor(() => expect(screen.getByTestId('card-sfx')).toBeTruthy())
- dispose()
+ act(() => dispose())
await waitFor(() => expect(container.querySelectorAll('audio[controls]')).toHaveLength(3))
expect(screen.queryByTestId('card-sfx')).toBeNull()
})
diff --git a/apps/desktop/src/components/assistant-ui/tool-media-slot.tsx b/apps/desktop/src/components/assistant-ui/tool-media-slot.tsx
index 16180becd4d..d9c47ac3d4b 100644
--- a/apps/desktop/src/components/assistant-ui/tool-media-slot.tsx
+++ b/apps/desktop/src/components/assistant-ui/tool-media-slot.tsx
@@ -8,6 +8,7 @@ import { type Contribution, useContributions } from '@/contrib'
import { $pluginRecords } from '@/contrib/plugins-store'
import { mediaKind, mediaMime, mediaName, resolveMediaPlaybackSrc } from '@/lib/media'
import {
+ effectiveToolCall,
findProducingToolCall,
refusedClaimPatterns,
resolveToolMediaClaim,
@@ -75,7 +76,8 @@ const ClaimedToolMediaSlot: FC<{ children: ReactNode; claims: readonly Contribut
claims,
path
}) => {
- const producer = useProducingToolCall(path)
+ const part = useProducingToolCall(path)
+ const producer = useMemo(() => (part ? effectiveToolCall(part) : null), [part])
const owners = useStore($toolMediaOwners)
// Re-resolve when a plugin record (enable state, package) changes.
useStore($pluginRecords)
diff --git a/apps/desktop/src/lib/tool-media-renderers.test.ts b/apps/desktop/src/lib/tool-media-renderers.test.ts
index 4987d85c9ec..42184701e51 100644
--- a/apps/desktop/src/lib/tool-media-renderers.test.ts
+++ b/apps/desktop/src/lib/tool-media-renderers.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it, vi } from 'vitest'
import {
claimPatternOwned,
+ effectiveToolCall,
findProducingToolCall,
ownerOfTool,
refusedClaimPatterns,
@@ -54,6 +55,17 @@ describe('findProducingToolCall', () => {
})
})
+describe('effectiveToolCall', () => {
+ it('unwraps a single-entry deferred-tool bridge call and leaves batches and plain calls alone', () => {
+ const bridged = call('tool_call', {}, { args: { calls: [{ name: 'sfx_make', arguments: { prompt: 'zap' } }] } })
+ const batch = call('tool_call', {}, { args: { calls: [{ name: 'a' }, { name: 'b' }] } })
+
+ expect(effectiveToolCall(bridged)).toMatchObject({ args: { prompt: 'zap' }, toolName: 'sfx_make' })
+ expect(effectiveToolCall(batch).toolName).toBe('tool_call')
+ expect(effectiveToolCall(call('terminal', {})).toolName).toBe('terminal')
+ })
+})
+
describe('ownership', () => {
const owners = new Map<string, ToolOwnership>([
['sfx', { prefixes: [], tools: new Set(['sfx_make']) }],
diff --git a/apps/desktop/src/lib/tool-media-renderers.ts b/apps/desktop/src/lib/tool-media-renderers.ts
index 19781624448..eaead8a468c 100644
--- a/apps/desktop/src/lib/tool-media-renderers.ts
+++ b/apps/desktop/src/lib/tool-media-renderers.ts
@@ -158,6 +158,28 @@ export function findProducingToolCall(
return null
}
+/** The tool a call really ran. The deferred-tool bridge (`tool_call`) runs one
+ * local tool per call (batches are split before they run), with the tool's
+ * name and arguments in `args.calls[0]` and its own result as the result, so
+ * a plugin tool loaded on demand is still recognised. A multi-entry batch
+ * (connectors only) stays the bridge: no plugin owns it. */
+export function effectiveToolCall(part: ToolCallLike): ToolCallLike {
+ const calls =
+ part.toolName === 'tool_call' && part.args && typeof part.args === 'object'
+ ? (part.args as { calls?: unknown }).calls
+ : undefined
+
+ if (!Array.isArray(calls) || calls.length !== 1) {
+ return part
+ }
+
+ const entry = calls[0] as { arguments?: unknown; name?: unknown } | null
+
+ return typeof entry?.name === 'string' && entry.name
+ ? { ...part, args: entry.arguments ?? {}, toolName: entry.name }
+ : part
+}
+
/** The package that owns `toolName`: an exact registration first, else the
* longest MCP namespace that covers it (so `mcp__a__` never takes a server
* named `a__b`'s tools). */
diff --git a/apps/desktop/src/styles.css b/apps/desktop/src/styles.css
index 219de061127..60168c47c86 100644
--- a/apps/desktop/src/styles.css
+++ b/apps/desktop/src/styles.css
@@ -1761,7 +1761,7 @@ body.guest-pointer-lock :is(webview, iframe) {
selector ties our `my-*` utility on specificity and wins on source order, so
paragraph spacing must be reclaimed here at higher specificity. One tight
top-margin (bottom zeroed to avoid doubling), first child reset to flush. */
-[data-slot='aui_assistant-message-content'] .aui-md :where(p) {
+[data-slot='aui_assistant-message-content'] .aui-md :where(p, [data-tool-media-paragraph]) {
margin-block: var(--paragraph-gap) 0;
}