Implementation:777genius Agent teams ai parseStreamJsonToGroups
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Session_Analysis, Parsing |
| Last Updated | 2026-10-22 00:00 GMT |
Overview
Concrete tool for parsing multi-line NDJSON (stream-json) CLI output into structured display groups for rich rendering, provided by the Agent Teams application.
Description
The parseStreamJsonToGroups() function takes raw CLI stream-json stdout text and converts it into an array of `StreamJsonGroup` objects. Each group represents one or more consecutive assistant messages, with non-assistant lines silently skipped.
Key behaviors:
- Group assembly: Consecutive assistant messages share a group ID and timestamp.
- Message ID disambiguation: A `msgIdOccurrences` map tracks how many times each message ID has been seen; duplicate IDs get suffixes (`-{occurrence}`).
- Hash disambiguation: A `hashOccurrences` map similarly disambiguates content hashes.
- Timestamp caching: A `lineTimestampCache` (keyed by line content with `MAX_TIMESTAMP_CACHE_SIZE` and LRU eviction) ensures the same line always gets the same timestamp across re-parses, preventing visual flicker.
- Agent ID tracking: Each group carries an optional `agentId` for multi-agent session rendering.
- Display items: Each group contains `AIGroupDisplayItem[]` built from assistant message content, tool calls, and thinking.
Usage
- Live CLI output: Call on the tail of CLI stream-json stdout for real-time rendering.
- Re-parsing: The timestamp cache ensures stable timestamps when the same content is re-parsed.
Code Reference
Source Location
- Repository: agent-teams
- File: src/renderer/utils/streamJsonParser.ts
Signature
export function parseStreamJsonToGroups(
cliLogsTail: string
): StreamJsonGroup[];
export interface StreamJsonGroup {
id: string;
items: AIGroupDisplayItem[];
summary: string;
timestamp: Date | null;
agentId?: string;
}
Import
import { parseStreamJsonToGroups } from '@renderer/utils/streamJsonParser';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| cliLogsTail | string | Yes | Multi-line string of stream-json NDJSON from Claude/Codex/OpenCode CLI |
Outputs
| Name | Type | Description |
|---|---|---|
| return | StreamJsonGroup[] | Parsed groups with display items |
| StreamJsonGroup.id | string | Unique group ID (disambiguated for duplicates) |
| StreamJsonGroup.items | AIGroupDisplayItem[] | Display items from assistant messages |
| StreamJsonGroup.timestamp | null | Cached timestamp for the group |
| StreamJsonGroup.agentId | undefined | Agent ID for multi-agent sessions |
Usage Examples
Parsing CLI Output
import { parseStreamJsonToGroups } from '@renderer/utils/streamJsonParser';
// Tail of stream-json stdout from a running CLI process
const cliOutput = `
{"type":"assistant","message":{"id":"msg-1","content":"Hello"}}
{"type":"assistant","message":{"id":"msg-2","content":"World"}}
{"type":"result","subtype":"success"}
`;
const groups = parseStreamJsonToGroups(cliOutput);
console.log(`${groups.length} groups`);
for (const group of groups) {
console.log(` ${group.id}: ${group.items.length} items at ${group.timestamp}`);
}
In a Real-Time Update Loop
// On each CLI stdout chunk, re-parse the tail
function onCliStdoutChunk(tail: string) {
const groups = parseStreamJsonToGroups(tail);
// Timestamp cache ensures stable rendering across re-parses
updateTimeline(groups);
}
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment