Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions libs/chat/src/lib/a2ui/partial-args-bridge.performance.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, it, vi } from 'vitest';
import { TestBed } from '@angular/core/testing';
import { createPartialArgsBridge } from './partial-args-bridge';
import { createA2uiSurfaceStore } from './surface-store';

const parsed = vi.hoisted(() => ({ bytes: 0 }));
vi.mock('@cacheplane/partial-json', async (importOriginal) => {
const original = await importOriginal<typeof import('@cacheplane/partial-json')>();
return {
...original,
createPartialJsonParser: (...args: Parameters<typeof original.createPartialJsonParser>) => {
const parser = original.createPartialJsonParser(...args);
const push = parser.push.bind(parser);
parser.push = (text: string) => {
parsed.bytes += text.length;
return push(text);
};
return parser;
},
};
});

describe('partial argument parsing work', () => {
it('parses each streamed character once, including duplicate cumulative updates', () => {
TestBed.configureTestingModule({});
const store = TestBed.runInInjectionContext(() => createA2uiSurfaceStore());
const bridge = createPartialArgsBridge(store);
const args = JSON.stringify({ envelopes: [
{ version: 'v0.9', createSurface: { surfaceId: 'report', catalogId: 'basic' } },
{ version: 'v0.9', updateComponents: { surfaceId: 'report', components: [{ id: 'root', component: 'Text', text: 'Cleanup complete' }] } },
] });
parsed.bytes = 0;
for (let end = 1; end <= args.length; end++) {
bridge.push('research-report', args.slice(0, end));
bridge.push('research-report', args.slice(0, end));
}
expect(parsed.bytes).toBe(args.length);
expect(store.surfaces().get('report')?.components.has('root')).toBe(true);
expect(bridge.isPoisoned('research-report')).toBe(false);
});
});
16 changes: 11 additions & 5 deletions libs/chat/src/lib/a2ui/partial-args-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface PartialArgsBridge {

interface BridgeState {
parser: ReturnType<typeof createPartialJsonParser>;
args: string;
/** Number of envelopes already dispatched to the store. */
dispatchedCount: number;
/** Surface ids for which a createSurface (real or synthesised) has been
Expand Down Expand Up @@ -179,6 +180,7 @@ export function createPartialArgsBridge(store: A2uiSurfaceStore): PartialArgsBri
if (!s) {
s = {
parser: createPartialJsonParser(),
args: '',
dispatchedCount: 0,
createDispatched: new Set(),
poisoned: false,
Expand All @@ -191,17 +193,21 @@ export function createPartialArgsBridge(store: A2uiSurfaceStore): PartialArgsBri
function push(toolCallId: string, argsSoFar: string): void {
const state = stateOf(toolCallId);
if (state.poisoned) return;
if (argsSoFar === state.args) return;
// Pre-check: poison if the args string isn't a valid JSON prefix.
if (!isValidJsonPrefix(argsSoFar)) {
state.poisoned = true;
return;
}
try {
// Reset the parser to a fresh state and feed the entire cumulative
// string. The parser is monotonic — same input always yields the
// same tree — so re-parsing is safe and avoids delta-tracking bugs.
state.parser = createPartialJsonParser();
state.parser.push(argsSoFar);
// Cumulative stream updates normally append. Replaying every previous
// character on each update makes a fast-forward seek quadratic.
if (!argsSoFar.startsWith(state.args)) {
state.parser = createPartialJsonParser();
state.args = '';
}
state.parser.push(argsSoFar.slice(state.args.length));
state.args = argsSoFar;
} catch {
state.poisoned = true;
return;
Expand Down
Loading