Implementation:777genius Agent teams ai ChunkBuilder buildChunks
| Knowledge Sources | |
|---|---|
| Domains | Session_Analysis, Data_Structuring |
| Last Updated | 2026-10-22 00:00 GMT |
Overview
Concrete tool for classifying parsed session messages into independent timeline chunks (User, AI, System, Compact) with 4-category classification and subagent enrichment, provided by the Agent Teams application.
Description
The ChunkBuilder.buildChunks() method takes a flat array of parsed messages and produces typed chunks for timeline visualization. Messages are classified by `MessageClassifier` into five categories (user, system, hardNoise, ai, compact), and consecutive AI messages are buffered into a single AIChunk.
Key behaviors:
- 4-category + noise classification: 'user' → UserChunk, 'system' → SystemChunk, 'ai' → buffered into AIChunk, 'compact' → CompactChunk, 'hardNoise' → filtered out.
- Independent chunks: No pairing between User and AI; each chunk type is independent.
- AI buffering: Consecutive 'ai' messages accumulate in a buffer; any non-AI category flushes the buffer.
- Sidechain filtering: Sidechain messages (from subagents) are filtered out unless `includeSidechain: true`.
- Subagent linking: Subagent processes are passed to `buildAIChunkFromBuffer()` for detail enrichment.
- Metrics: Each chunk carries metrics computed via `calculateMetrics()` from `@main/utils/jsonl`.
- Tracing: The entire build is wrapped in a `startMainSpan('chunks.build', ...)` tracing span.
Usage
- Session timeline: Call `buildChunks()` after JSONL parsing to produce the timeline chunk list for UI rendering.
- Include sidechains: Pass `includeSidechain: true` when rendering subagent conversations.
Code Reference
Source Location
- Repository: agent-teams
- File: src/main/services/analysis/ChunkBuilder.ts
Signature
export class ChunkBuilder {
buildChunks(
messages: ParsedMessage[],
subagents?: Process[],
options?: { includeSidechain?: boolean }
): EnhancedChunk[];
}
Import
import { ChunkBuilder } from '@main/services/analysis/ChunkBuilder';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| messages | ParsedMessage[] | Yes | Parsed messages from JSONL streaming |
| subagents | Process[] | No (default []) | Linked subagent processes for detail enrichment |
| options.includeSidechain | boolean | No | If true, include sidechain (subagent) messages |
Outputs
| Name | Type | Description |
|---|---|---|
| return | EnhancedChunk[] | Typed chunks: UserChunk, AIChunk, SystemChunk, CompactChunk |
| EnhancedChunk.type | string | 'user', 'ai', 'system', or 'compact' |
| EnhancedChunk.metrics | ChunkMetrics | Tokens, cost, tools, duration per chunk |
Usage Examples
Building Chunks from Parsed Messages
import { ChunkBuilder } from '@main/services/analysis/ChunkBuilder';
const builder = new ChunkBuilder();
// messages from JSONL parsing, subagents from SubagentResolver
const chunks = builder.buildChunks(parsedMessages, subagents);
for (const chunk of chunks) {
switch (chunk.type) {
case 'user':
console.log(`UserChunk: ${chunk.message.content.slice(0, 50)}`);
break;
case 'ai':
console.log(`AIChunk: ${chunk.messages.length} messages, ${chunk.metrics.totalTokens} tokens`);
break;
case 'system':
console.log(`SystemChunk: command output`);
break;
case 'compact':
console.log(`CompactChunk: compaction event`);
break;
}
}
Including Sidechain Messages
// Include subagent sidechain messages for full conversation view
const allChunks = builder.buildChunks(parsedMessages, subagents, {
includeSidechain: true,
});