forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream-chunk-processor.ts
More file actions
57 lines (47 loc) · 1.5 KB
/
Copy pathstream-chunk-processor.ts
File metadata and controls
57 lines (47 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { appendTextToAgentBlock, appendTextToRootStream } from './block-operations'
import type { StreamChunkEvent } from './sdk-event-handlers'
import type { ContentBlock } from '../types/chat'
export type ChunkDestination =
| { type: 'root'; textType: 'text' | 'reasoning' }
| { type: 'agent'; agentId: string; textType: 'text' | 'reasoning' }
export const destinationFromTextEvent = (
event: { agentId?: string },
): ChunkDestination => {
if (event.agentId) {
return { type: 'agent', agentId: event.agentId, textType: 'text' }
}
return { type: 'root', textType: 'text' }
}
export const destinationFromChunkEvent = (
event: StreamChunkEvent,
): ChunkDestination | null => {
if (typeof event === 'string') {
return { type: 'root', textType: 'text' }
}
if (event.type === 'subagent_chunk') {
return { type: 'agent', agentId: event.agentId, textType: 'text' }
}
if (event.type === 'reasoning_chunk') {
if (event.ancestorRunIds.length === 0) {
return { type: 'root', textType: 'reasoning' }
}
return { type: 'agent', agentId: event.agentId, textType: 'reasoning' }
}
return null
}
export const processTextChunk = (
blocks: ContentBlock[],
destination: ChunkDestination,
text: string,
): ContentBlock[] => {
if (!text) {
return blocks
}
if (destination.type === 'agent') {
return appendTextToAgentBlock(blocks, destination.agentId, text, destination.textType)
}
return appendTextToRootStream(blocks, {
type: destination.textType,
text,
})
}