Skip to content

Commit

Permalink
fix(stream-text): ensure SSE handling conforms to the spec
Browse files Browse the repository at this point in the history
  • Loading branch information
iseki0 committed Jan 18, 2025
1 parent 3b43f4c commit b837a58
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions packages/stream-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface StreamTextResult {
usage?: Usage
}

const chunkHeaderPrefix = 'data:'
const chunkHeaderPrefix = /^data(?:: ?)?/

/**
* @experimental WIP, does not support function calling (tools).
Expand Down Expand Up @@ -79,14 +79,10 @@ export const streamText = async (options: StreamTextOptions): Promise<StreamText
// Check for comments
if (line.startsWith(':'))
continue
// Check if line starts with data: (allowing spaces)
if (line.startsWith(chunkHeaderPrefix)) {
// Check if line starts with chunk header
if (chunkHeaderPrefix.test(line)) {
// Extract content after data: (remove leading single space if present)
const content = line.slice(chunkHeaderPrefix.length)
dataLines.push(content.length > 0 && content[0] === ' ' ? content.slice(1) : content)
}
else {
break // event ended, dispatch
dataLines.push(line.replace(chunkHeaderPrefix, ''))
}
}

Expand Down

0 comments on commit b837a58

Please sign in to comment.