Skip to content

Commit 960a20e

Browse files
committed
feat: add streaming parser for claude
1 parent def1057 commit 960a20e

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

assets/components/modai/js/mgr/executor.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@
7575
};
7676
}
7777
},
78+
claude: {
79+
content: (newData, currentData = undefined) => {
80+
const currentContent = currentData?.content ?? '';
81+
82+
const content = newData.delta?.text || '';
83+
84+
return {
85+
content: `${currentContent}${content}`
86+
};
87+
}
88+
},
7889
gemini: {
7990
content: (newData, currentData = undefined) => {
8091
const currentContent = currentData?.content ?? '';
@@ -144,7 +155,36 @@
144155

145156
try {
146157
const parsedData = JSON.parse(data);
147-
console.log(parsedData);
158+
currentData = services.stream[service][parser](parsedData, currentData);
159+
if(onChunkStream) {
160+
onChunkStream(currentData);
161+
}
162+
} catch {}
163+
}
164+
}
165+
166+
buffer = buffer.slice(lastNewlineIndex);
167+
}
168+
169+
if (service === 'claude') {
170+
buffer += chunk;
171+
172+
let lastNewlineIndex = 0;
173+
let newlineIndex;
174+
175+
while ((newlineIndex = buffer.indexOf('\n', lastNewlineIndex)) !== -1) {
176+
const line = buffer.slice(lastNewlineIndex, newlineIndex).trim();
177+
lastNewlineIndex = newlineIndex + 1;
178+
179+
if (line.startsWith('data: ')) {
180+
const data = line.slice(6);
181+
182+
try {
183+
const parsedData = JSON.parse(data);
184+
if (parsedData.type !== 'content_block_delta') {
185+
continue;
186+
}
187+
148188
currentData = services.stream[service][parser](parsedData, currentData);
149189
if(onChunkStream) {
150190
onChunkStream(currentData);

0 commit comments

Comments
 (0)