Skip to content

The agent that grows with you

refactor(desktop): delete the whole-paragraph directive parser

alt-glitchc72c39a9ff83
diff --git a/apps/desktop/src/lib/transcript-directives.test.ts b/apps/desktop/src/lib/transcript-directives.test.ts
index b40f0838b6c..766a7743903 100644
--- a/apps/desktop/src/lib/transcript-directives.test.ts
+++ b/apps/desktop/src/lib/transcript-directives.test.ts
@@ -1,54 +1,41 @@
import { describe, expect, it } from 'vitest'
-import { parseTranscriptDirective } from './transcript-directives'
+import { segmentTranscriptDirectives } from './transcript-directives'
-describe('parseTranscriptDirective', () => {
- it('parses a bare directive with no attributes', () => {
- expect(parseTranscriptDirective('::tasks')).toEqual({ name: 'tasks', attrs: {}, source: '::tasks' })
- })
+const directive = (text: string) => segmentTranscriptDirectives(text)?.find(segment => segment.kind === 'directive')
- it('parses double-quoted attributes', () => {
- expect(parseTranscriptDirective('::preview{file="demo.html"}')).toEqual({
- name: 'preview',
- attrs: { file: 'demo.html' },
- source: '::preview{file="demo.html"}'
- })
+describe('segmentTranscriptDirectives', () => {
+ it('parses a bare directive with no attributes', () => {
+ expect(segmentTranscriptDirectives('::tasks')).toEqual([
+ { kind: 'directive', directive: { name: 'tasks', attrs: {}, source: '::tasks' } }
+ ])
})
it('parses multiple attributes and accepts single quotes', () => {
- expect(parseTranscriptDirective(`::vis{file="a b.html" height='480'}`)?.attrs).toEqual({
- file: 'a b.html',
- height: '480'
+ expect(directive(`::vis{file="a b.html" height='480'}`)).toEqual({
+ kind: 'directive',
+ directive: { name: 'vis', attrs: { file: 'a b.html', height: '480' }, source: `::vis{file="a b.html" height='480'}` }
})
})
it('lowercases attribute keys but preserves values', () => {
- expect(parseTranscriptDirective('::vis{File="A.html"}')?.attrs).toEqual({ file: 'A.html' })
- })
-
- it('tolerates surrounding whitespace', () => {
- expect(parseTranscriptDirective(' ::tasks{id="1"} ')?.name).toBe('tasks')
- })
-
- it('rejects prose containing a directive mid-text', () => {
- expect(parseTranscriptDirective('see ::preview{file="x.html"} above')).toBeNull()
+ expect(directive('::vis{File="A.html"}')).toMatchObject({ directive: { attrs: { file: 'A.html' } } })
})
- it('rejects multi-line paragraphs', () => {
- expect(parseTranscriptDirective('::preview{file="x.html"}\nmore')).toBeNull()
+ it('ignores unquoted attribute values', () => {
+ expect(directive('::preview{file=demo.html}')).toMatchObject({ directive: { attrs: {} } })
})
- it('rejects C++ scope-resolution lookalikes', () => {
- expect(parseTranscriptDirective('::std')).toEqual({ name: 'std', attrs: {}, source: '::std' })
- expect(parseTranscriptDirective('std::vector<int>')).toBeNull()
- expect(parseTranscriptDirective('::Vector')).toBeNull()
+ it('does not read C++ scope resolution or capitalised names as directives', () => {
+ expect(segmentTranscriptDirectives('std::vector<int>')).toBeNull()
+ expect(segmentTranscriptDirectives('::Vector')).toBeNull()
})
- it('rejects unquoted attribute values', () => {
- expect(parseTranscriptDirective('::preview{file=demo.html}')?.attrs).toEqual({})
+ it('leaves a directive whose attributes do not parse as text', () => {
+ expect(segmentTranscriptDirectives('::x{unclosed')).toBeNull()
})
it('bounds pathological input instead of scanning it', () => {
- expect(parseTranscriptDirective(`::x{${'a="b" '.repeat(400)}}`)).toBeNull()
+ expect(segmentTranscriptDirectives(`::x{${'a="b" '.repeat(1000)}}`)).toBeNull()
})
})
diff --git a/apps/desktop/src/lib/transcript-directives.ts b/apps/desktop/src/lib/transcript-directives.ts
index a01f771c362..a70919e88dc 100644
--- a/apps/desktop/src/lib/transcript-directives.ts
+++ b/apps/desktop/src/lib/transcript-directives.ts
@@ -10,11 +10,7 @@ import type { ReactNode } from 'react'
* fences get promoted whether or not the model asked), directives are
* addressed (nothing renders unless a plugin claimed the name).
*
- * The product parser requires the entire paragraph to be one directive, so
- * mid-prose text and malformed or unclaimed directives stay prose.
- *
- * For the guided chat segmenter, the guard is the CLAIM, not its position:
- * a name nobody registered — and a malformed one — stays exactly the text it
+ * The guard is the CLAIM, not its position: a name nobody registered — and a malformed one — stays exactly the text it
* always was. Position used to be the guard too (a directive had to be the
* whole paragraph), and that cost more than it bought: a model that wrote the
* directive at the end of its sentence instead of alone under it put the raw
@@ -55,10 +51,6 @@ export interface ParsedTranscriptDirective {
export type TranscriptParagraphSegment =
{ kind: 'prose'; text: string } | { kind: 'directive'; directive: ParsedTranscriptDirective }
-// The whole paragraph, nothing else on the line: `::name` or `::name{...}`.
-// Length caps bound the attr scan on adversarial input.
-const DIRECTIVE_RE = /^::([a-z][a-z0-9-]{0,63})(?:\{([^{}]{0,1024})\})?$/
-
// `::name` or `::name{...}`, anywhere a word can start — so `std::vector` is
// never a directive. Length caps bound the attr scan on adversarial input.
const SEGMENT_RE = /(?<=^|\s)::([a-z][a-z0-9-]{0,63})(?:\{([^{}]{0,1024})\})?/g
@@ -66,36 +58,6 @@ const SEGMENT_RE = /(?<=^|\s)::([a-z][a-z0-9-]{0,63})(?:\{([^{}]{0,1024})\})?/g
// `key="value"` pairs; single quotes accepted for model sloppiness.
const ATTR_RE = /([a-z][\w-]{0,63})=(?:"([^"]*)"|'([^']*)')/gi
-/**
- * Parse a paragraph as a transcript directive. Returns null unless the ENTIRE
- * trimmed text is one directive — prose containing `::` stays prose.
- * Pure and synchronous — safe to call during render.
- */
-export function parseTranscriptDirective(text: string): ParsedTranscriptDirective | null {
- const trimmed = text.trim()
-
- // Cheap reject before the regex: directives are short single lines.
- if (!trimmed.startsWith('::') || trimmed.length > 1200 || trimmed.includes('\n')) {
- return null
- }
-
- const match = DIRECTIVE_RE.exec(trimmed)
-
- if (!match) {
- return null
- }
-
- const attrs: Record<string, string> = {}
-
- if (match[2]) {
- for (const pair of match[2].matchAll(ATTR_RE)) {
- attrs[pair[1].toLowerCase()] = pair[2] ?? pair[3] ?? ''
- }
- }
-
- return { name: match[1], attrs, source: trimmed }
-}
-
function parseAttrs(body: string | undefined): ParsedTranscriptDirective['attrs'] {
const attrs: Record<string, string> = {}