forked from nexu-io/open-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqoder-stream.ts
More file actions
173 lines (156 loc) · 4.87 KB
/
Copy pathqoder-stream.ts
File metadata and controls
173 lines (156 loc) · 4.87 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Parses Qoder CLI's `--output-format stream-json` JSONL stream into the
* small event set consumed by the chat UI. Qoder's top-level records are
* wrapper objects (`system`, `assistant`, `result`) with adapter-specific
* fields, so keep this parser separate from Claude/Codex-compatible streams.
*/
import { Buffer } from 'node:buffer';
type JsonRecord = Record<string, unknown>;
type QoderEvent = Record<string, unknown>;
type QoderEventSink = (event: QoderEvent) => void;
function isRecord(value: unknown): value is JsonRecord {
return Boolean(value) && typeof value === 'object';
}
function stringifyContent(value: unknown): string {
if (typeof value === 'string') return value;
if (Buffer.isBuffer(value)) return value.toString('utf8');
if (value == null) return '';
try {
return JSON.stringify(value);
} catch {
return String(value);
}
}
function textFromContentBlock(block: unknown): string {
if (!isRecord(block)) return '';
if (block.type === 'text' && typeof block.text === 'string') return block.text;
if (typeof block.text === 'string') return block.text;
return '';
}
function messageFromError(error: unknown): string {
if (isRecord(error) && typeof error.message === 'string') {
return error.message;
}
if (typeof error === 'string' && error.length > 0) return error;
return 'Unknown Qoder error';
}
function messageFromResult(obj: JsonRecord): string {
if (typeof obj.error === 'string' && obj.error.length > 0) return obj.error;
if (
isRecord(obj.error) &&
typeof obj.error.message === 'string' &&
obj.error.message.length > 0
) {
return obj.error.message;
}
if (typeof obj.message === 'string' && obj.message.length > 0) {
return obj.message;
}
if (typeof obj.stop_reason === 'string' && obj.stop_reason.length > 0) {
return `Qoder run failed: ${obj.stop_reason}`;
}
return 'Qoder run failed';
}
export function createQoderStreamHandler(onEvent: QoderEventSink) {
let buffer = '';
let emittedThinkingStart = false;
function handleObject(obj: unknown, rawLine: string) {
if (!isRecord(obj)) return;
if (obj.type === 'system' && obj.subtype === 'init') {
onEvent({
type: 'status',
label: 'initializing',
model: typeof obj.model === 'string' ? obj.model : undefined,
sessionId: typeof obj.session_id === 'string' ? obj.session_id : undefined,
qodercliVersion:
typeof obj.qodercli_version === 'string'
? obj.qodercli_version
: undefined,
});
return;
}
if (obj.type === 'assistant' && isRecord(obj.message)) {
const content = Array.isArray(obj.message.content)
? obj.message.content
: [];
let emittedText = false;
for (const block of content) {
const text = textFromContentBlock(block);
if (text.length > 0) {
emittedText = true;
onEvent({ type: 'text_delta', delta: text });
continue;
}
if (
isRecord(block) &&
block.type === 'thinking' &&
typeof block.thinking === 'string' &&
block.thinking.length > 0
) {
if (!emittedThinkingStart) {
emittedThinkingStart = true;
onEvent({ type: 'thinking_start' });
}
onEvent({ type: 'thinking_delta', delta: block.thinking });
}
}
if (!emittedText && typeof obj.message.content === 'string') {
onEvent({ type: 'text_delta', delta: obj.message.content });
emittedText = true;
}
if (obj.error && !emittedText) {
onEvent({
type: 'error',
message: messageFromError(obj.error),
raw: rawLine,
});
}
return;
}
if (obj.type === 'result') {
const isError = Boolean(obj.is_error);
onEvent({
type: 'usage',
usage: obj.usage ?? null,
modelUsage: obj.modelUsage ?? undefined,
costUsd: obj.total_cost_usd ?? null,
durationMs: typeof obj.duration_ms === 'number' ? obj.duration_ms : null,
stopReason: obj.stop_reason ?? null,
isError,
});
if (isError) {
onEvent({
type: 'error',
message: messageFromResult(obj),
raw: rawLine,
});
}
return;
}
onEvent({ type: 'raw', line: rawLine });
}
function handleLine(line: string) {
try {
handleObject(JSON.parse(line), line);
} catch {
onEvent({ type: 'raw', line });
}
}
function feed(chunk: unknown) {
buffer += stringifyContent(chunk);
let nl;
while ((nl = buffer.indexOf('\n')) !== -1) {
const line = buffer.slice(0, nl).trim();
buffer = buffer.slice(nl + 1);
if (!line) continue;
handleLine(line);
}
}
function flush() {
const rem = buffer.trim();
buffer = '';
if (!rem) return;
handleLine(rem);
}
return { feed, flush };
}