Skip to content

Commit b67ef1e

Browse files
committed
fix: streaming
1 parent 9f9759e commit b67ef1e

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/services/anthropicService.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,23 @@ export function* createMessageStream(request: MessagesRequest): Generator<string
142142
}
143143
}
144144
yield SSEMessageFormatter('message_start', startChunk);
145+
// Get test case
146+
const testCase = model.testCases[0];
145147

146148
// Send the first chunk
147-
outputTokens += calculateTokens(model.testCases[0].response);
148149
const firstChunk: ContentBlockStartEvent = {
149150
type: 'content_block_start',
150151
index: 0,
151152
content_block: {
152153
type: 'text',
153-
text: model.testCases[0].response
154+
text: ""
154155
}
155156
}
156157
yield SSEMessageFormatter('content_block_start', firstChunk);
157158

158159
//If there are predefined streaming chunks, use them
159-
if(model.testCases[0].streamChunks && model.testCases[0].streamChunks.length > 0){
160-
for(const chunk of model.testCases[0].streamChunks){
160+
if(testCase.streamChunks && testCase.streamChunks.length > 0){
161+
for(const chunk of testCase.streamChunks){
161162
outputTokens += calculateTokens(chunk);
162163
const streamChunk: ContentBlockDeltaEvent = {
163164
type: 'content_block_delta',
@@ -169,7 +170,23 @@ export function* createMessageStream(request: MessagesRequest): Generator<string
169170
}
170171
yield SSEMessageFormatter('content_block_delta', streamChunk);
171172
}
172-
}
173+
} else{
174+
// Split the response into chunks
175+
const words = testCase.response.split(" ");
176+
for(let i = 0; i < words.length; i += 2){
177+
const chunkText = words.slice(i, i + 2).join(" ") + (i + 2 < words.length ? " " : "");
178+
outputTokens += calculateTokens(chunkText);
179+
const streamChunk: ContentBlockDeltaEvent = {
180+
type: 'content_block_delta',
181+
index: 0,
182+
delta: {
183+
type: 'text_delta',
184+
text: chunkText
185+
}
186+
}
187+
yield SSEMessageFormatter('content_block_delta', streamChunk);
188+
}
189+
}
173190

174191
//send message delta
175192
const messageDelta: MessageDeltaEvent = {

0 commit comments

Comments
 (0)