Skip to content

The agent that grows with you

refactor(desktop): one transcript directive parser for everyone

alt-glitchc1a7a64dbe0d
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 8e0afd92306..8ab6b169540 100644
--- a/apps/desktop/src/components/assistant-ui/transcript-directive.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/transcript-directive.test.tsx
@@ -1,11 +1,11 @@
// @vitest-environment jsdom
-import { cleanup, render, screen } from '@testing-library/react'
+import { cleanup, render, renderHook, screen } from '@testing-library/react'
import { afterEach, describe, expect, it } from 'vitest'
import { registry } from '@/contrib/registry'
import { TRANSCRIPT_DIRECTIVE_AREA, type TranscriptDirectiveContribution } from '@/lib/transcript-directives'
-import { paragraphPlainText, TranscriptDirectiveLeaf } from './transcript-directive'
+import { paragraphPlainText, TranscriptDirectiveLeaf, useResolvedParagraph } from './transcript-directive'
describe('paragraphPlainText', () => {
it('passes through a plain string', () => {
@@ -49,6 +49,17 @@ describe('TranscriptDirectiveLeaf', () => {
}
})
+ it('renders every claimed directive in one paragraph, in order', () => {
+ const dispose = contribution()
+
+ try {
+ render(<TranscriptDirectiveLeaf text='::demo{label="one"} ::demo{label="two"}' />)
+ expect(screen.getAllByTestId('demo-card').map(card => card.textContent)).toEqual(['one', 'two'])
+ } finally {
+ dispose()
+ }
+ })
+
it('renders nothing for an unclaimed directive', () => {
const { container } = render(<TranscriptDirectiveLeaf text="::nobody-home" />)
@@ -77,3 +88,26 @@ describe('TranscriptDirectiveLeaf', () => {
}
})
})
+
+describe('useResolvedParagraph', () => {
+ it('lifts a claimed directive out of the sentence around it', () => {
+ const dispose = registry.register({
+ id: 'test:inline',
+ area: TRANSCRIPT_DIRECTIVE_AREA,
+ source: 'plugin:test',
+ data: { name: 'inline', render: () => null } satisfies TranscriptDirectiveContribution
+ })
+
+ try {
+ const { result } = renderHook(() => useResolvedParagraph('See ::inline{x="1"} and ::other here'))
+
+ expect(result.current).toEqual([
+ { kind: 'prose', text: 'See ' },
+ { kind: 'directive', source: '::inline{x="1"}' },
+ { kind: 'prose', text: ' and ::other here' }
+ ])
+ } finally {
+ dispose()
+ }
+ })
+})
diff --git a/apps/desktop/src/components/assistant-ui/transcript-directive.tsx b/apps/desktop/src/components/assistant-ui/transcript-directive.tsx
index 0e52324f86a..46aaff306c0 100644
--- a/apps/desktop/src/components/assistant-ui/transcript-directive.tsx
+++ b/apps/desktop/src/components/assistant-ui/transcript-directive.tsx
@@ -2,20 +2,15 @@ import type { FC, ReactNode } from 'react'
import { useMemo } from 'react'
import { type Contribution, useContributions } from '@/contrib'
-import { ContribBoundary, ContribRender } from '@/contrib/react/boundary'
-import { isOnboardingEnabled } from '@/lib/onboarding-enabled'
+import { ContribBoundary } from '@/contrib/react/boundary'
import {
type ParsedTranscriptDirective,
- parseTranscriptDirective,
segmentTranscriptDirectives,
TRANSCRIPT_DIRECTIVE_AREA,
type TranscriptDirectiveContribution,
type TranscriptParagraphSegment
} from '@/lib/transcript-directives'
-// B4 reworks the parser; until then the prototype's parser is an onboarding feature.
-const onboardingEnabled = isOnboardingEnabled()
-
/**
* The transcript's directive slot. Given text, renders the plugin component
* for every claimed `::name{...}` in it, in order — several, because models
@@ -75,15 +70,7 @@ const DirectiveEntry: FC<{
export const TranscriptDirectiveLeaf: FC<{ text: string; streaming?: boolean }> = ({ text, streaming }) => {
const contributions = useContributions(TRANSCRIPT_DIRECTIVE_AREA)
- const segments = useMemo<TranscriptParagraphSegment[] | null>(() => {
- if (onboardingEnabled) {
- return segmentTranscriptDirectives(text)
- }
-
- const parsed = parseTranscriptDirective(text)
-
- return parsed ? [{ kind: 'directive', directive: parsed }] : null
- }, [text])
+ const segments = useMemo<TranscriptParagraphSegment[] | null>(() => segmentTranscriptDirectives(text), [text])
const entries = useMemo(
() =>
@@ -99,34 +86,6 @@ export const TranscriptDirectiveLeaf: FC<{ text: string; streaming?: boolean }>
[contributions, segments]
)
- const match = entries[0]?.match
- const parsed = entries[0]?.parsed
- // SAFETY: claimFor resolved this entry from the directive area by its registered name.
- const render = (match?.data as TranscriptDirectiveContribution | undefined)?.render
-
- // Stable component identity for ContribRender (which mounts this AS a
- // component): a fresh closure per render would remount the widget on
- // every parent render.
- const renderLeaf = useMemo(
- () =>
- render && parsed
- ? () => render({ attrs: parsed.attrs, source: parsed.source, streaming: streaming ?? false })
- : null,
- [render, parsed, streaming]
- )
-
- if (!onboardingEnabled) {
- if (!match || !renderLeaf) {
- return null
- }
-
- return (
- <ContribBoundary id={match.id} variant="chip">
- <ContribRender render={renderLeaf} />
- </ContribBoundary>
- )
- }
-
if (entries.length === 0) {
return null
}
@@ -161,12 +120,6 @@ export function useResolvedParagraph(text: string | null): ResolvedParagraphSegm
const contributions = useContributions(TRANSCRIPT_DIRECTIVE_AREA)
return useMemo(() => {
- if (!onboardingEnabled) {
- const parsed = text === null ? null : parseTranscriptDirective(text)
-
- return parsed && claimFor(contributions, parsed.name) ? [{ kind: 'directive', source: parsed.source }] : null
- }
-
const segments = text === null ? null : segmentTranscriptDirectives(text)
if (!segments) {