Skip to content

The agent that grows with you

feat(desktop): name the owning chat in transcript directive props

Tekniumc78ca093aa4b
diff --git a/apps/desktop/src/components/assistant-ui/transcript-directive.test.tsx b/apps/desktop/src/components/assistant-ui/transcript-directive.test.tsx
index 8ab6b169540..5a74a60719f 100644
--- a/apps/desktop/src/components/assistant-ui/transcript-directive.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/transcript-directive.test.tsx
@@ -1,9 +1,14 @@
// @vitest-environment jsdom
-import { cleanup, render, renderHook, screen } from '@testing-library/react'
+import { cleanup, fireEvent, render, renderHook, screen, waitFor } from '@testing-library/react'
+import { atom } from 'nanostores'
import { afterEach, describe, expect, it } from 'vitest'
+import { ackComposerInsert } from '@/app/chat/composer/focus'
+import { PRIMARY_SESSION_VIEW, type SessionView, SessionViewProvider } from '@/app/chat/session-view'
import { registry } from '@/contrib/registry'
import { TRANSCRIPT_DIRECTIVE_AREA, type TranscriptDirectiveContribution } from '@/lib/transcript-directives'
+import { composerHost } from '@/sdk/composer'
+import { $selectedStoredSessionId, setActiveSessionId } from '@/store/session'
import { paragraphPlainText, TranscriptDirectiveLeaf, useResolvedParagraph } from './transcript-directive'
@@ -87,6 +92,64 @@ describe('TranscriptDirectiveLeaf', () => {
dispose()
}
})
+
+ it("routes a card's action to the chat that renders it, not to the active composer", async () => {
+ // Primary pane shows A (and holds the active composer); a tile shows B.
+ setActiveSessionId('rt-a')
+ $selectedStoredSessionId.set('sess-a')
+
+ const tileB: SessionView = {
+ ...PRIMARY_SESSION_VIEW,
+ $runtimeId: atom<null | string>('rt-b'),
+ $storedId: atom<null | string>('sess-b'),
+ kind: 'tile'
+ }
+
+ const targets: string[] = []
+
+ const onInsert = (event: Event) => {
+ const { target, token } = (event as CustomEvent<{ target: string; token: number }>).detail
+
+ targets.push(target)
+ ackComposerInsert(token, true)
+ }
+
+ // What a plugin card does: its own chat when the host names it, else the
+ // active composer (the pre-`sessionId` call, unchanged).
+ const dispose = contribution({
+ render: props => (
+ <button
+ data-testid={`next-${props.attrs.label}`}
+ onClick={() => void composerHost.insertText(props.sessionId ?? null, 'next step')}
+ type="button"
+ />
+ )
+ })
+
+ window.addEventListener('hermes:composer-insert', onInsert)
+
+ try {
+ render(
+ <>
+ <TranscriptDirectiveLeaf text='::demo{label="a"}' />
+ <SessionViewProvider value={tileB}>
+ <TranscriptDirectiveLeaf text='::demo{label="b"}' />
+ </SessionViewProvider>
+ </>
+ )
+
+ fireEvent.click(screen.getByTestId('next-b'))
+ fireEvent.click(screen.getByTestId('next-a'))
+ await waitFor(() => expect(targets).toHaveLength(2))
+
+ expect(targets).toEqual(['tile:sess-b', 'main'])
+ } finally {
+ window.removeEventListener('hermes:composer-insert', onInsert)
+ dispose()
+ setActiveSessionId(null)
+ $selectedStoredSessionId.set(null)
+ }
+ })
})
describe('useResolvedParagraph', () => {
diff --git a/apps/desktop/src/components/assistant-ui/transcript-directive.tsx b/apps/desktop/src/components/assistant-ui/transcript-directive.tsx
index 46aaff306c0..24162d2712b 100644
--- a/apps/desktop/src/components/assistant-ui/transcript-directive.tsx
+++ b/apps/desktop/src/components/assistant-ui/transcript-directive.tsx
@@ -1,6 +1,8 @@
+import { useStore } from '@nanostores/react'
import type { FC, ReactNode } from 'react'
import { useMemo } from 'react'
+import { useSessionView } from '@/app/chat/session-view'
import { type Contribution, useContributions } from '@/contrib'
import { ContribBoundary } from '@/contrib/react/boundary'
import {
@@ -47,22 +49,35 @@ const DirectiveEntry: FC<{
streaming: boolean
}> = ({ contribution, parsed, streaming }) => {
const render = (contribution.data as TranscriptDirectiveContribution).render
+ // The surface this transcript renders in (primary pane or a tile) names the
+ // chat that owns the card, so its actions can address that chat's composer.
+ const view = useSessionView()
+ const storedId = useStore(view.$storedId)
+ const runtimeId = useStore(view.$runtimeId)
+ const sessionId = storedId || runtimeId || null
// Stable component IDENTITY per (render, parsed) — a fresh type per parent
- // render would remount the widget (card-sized jump). Streaming arrives as a
- // real prop on that stable type, so the settle flip re-renders the same
- // mount instead of being memo-skipped (ref-only reads were exactly that).
+ // render would remount the widget (card-sized jump). Streaming and the owning
+ // session arrive as real props on that stable type, so the settle flip
+ // re-renders the same mount instead of being memo-skipped (ref-only reads
+ // were exactly that).
const Leaf = useMemo(
() =>
- function DirectiveLeafHost({ streaming: live }: { streaming: boolean }) {
- return <>{render({ attrs: parsed.attrs, source: parsed.source, streaming: live })}</>
+ function DirectiveLeafHost({
+ sessionId: owner,
+ streaming: live
+ }: {
+ sessionId: null | string
+ streaming: boolean
+ }) {
+ return <>{render({ attrs: parsed.attrs, sessionId: owner, source: parsed.source, streaming: live })}</>
},
[render, parsed]
)
return (
<ContribBoundary id={contribution.id} variant="chip">
- <Leaf streaming={streaming} />
+ <Leaf sessionId={sessionId} streaming={streaming} />
</ContribBoundary>
)
}
diff --git a/apps/desktop/src/lib/transcript-directives.ts b/apps/desktop/src/lib/transcript-directives.ts
index 62b9521d2ce..c636c2d9134 100644
--- a/apps/desktop/src/lib/transcript-directives.ts
+++ b/apps/desktop/src/lib/transcript-directives.ts
@@ -30,6 +30,13 @@ export interface TranscriptDirectiveProps {
source: string
/** True while the surrounding message is still streaming. */
streaming: boolean
+ /** The session whose transcript renders this directive (stored id, else the
+ * runtime id), or null while that chat has no session yet. Pass it to
+ * `host.composer.*` so an action lands in THIS chat's composer rather than
+ * whichever one is active. Hosts before this field omit it, so plugins
+ * feature-detect (`props.sessionId !== undefined`) and keep their old
+ * call otherwise. */
+ sessionId: null | string
}
/** Payload of a `transcript.directives` contribution's `data`. */
diff --git a/website/docs/developer-guide/desktop-plugin-sdk.md b/website/docs/developer-guide/desktop-plugin-sdk.md
index e8bae8cbcdd..4e83feb369c 100644
--- a/website/docs/developer-guide/desktop-plugin-sdk.md
+++ b/website/docs/developer-guide/desktop-plugin-sdk.md
@@ -1093,11 +1093,32 @@ ctx.register({
area: TRANSCRIPT_DIRECTIVE_AREA,
data: {
name: 'task', // the model writes ::task{id="BB-12"}
- render: ({ attrs, streaming }) => jsx(TaskCard, { taskId: attrs.id, streaming })
+ render: ({ attrs, sessionId, streaming }) => jsx(TaskCard, { taskId: attrs.id, sessionId, streaming })
}
})
```
+`render` receives `{ attrs, source, streaming, sessionId }`. `sessionId` names
+the chat whose transcript shows the card (its stored id, else its runtime id;
+`null` while that chat has no session yet). Pass it to the `host.composer`
+verbs so a card action lands in **that** chat, even when another chat open
+side by side holds the active composer:
+
+```javascript
+function TaskCard({ taskId, sessionId }) {
+ const next = () => {
+ // Older hosts don't pass sessionId (undefined): fall back to the active
+ // composer, which is what `null` addresses.
+ const sent = host.composer.submit(sessionId ?? null, `Continue task ${taskId}`)
+ if (!sent) host.notify({ kind: 'warning', message: 'Open that chat to continue' })
+ }
+ return jsx('button', { onClick: next, children: 'Next' })
+}
+```
+
+Addressed verbs fail closed: when the card's chat has no visible composer,
+`submit` returns `false` and nothing is sent anywhere else.
+
Rules the host enforces so the surface stays safe:
- The directive must be the **entire paragraph** — `::name` mid-prose stays