-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Modify smoothStream transform to support tool call streams #5326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kac487
wants to merge
3
commits into
vercel:main
Choose a base branch
from
kac487:kac487/smooth-stream-for-tool-usage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,33 +43,114 @@ export function smoothStream<TOOLS extends ToolSet>({ | |
} | ||
|
||
return () => { | ||
let buffer = ''; | ||
// Separate buffers for text and tool call args | ||
let textBuffer = ''; | ||
let toolCallBuffer: { id: string; name: string; buffer: string } | null = | ||
null; | ||
|
||
return new TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>>({ | ||
async transform(chunk, controller) { | ||
if (chunk.type === 'step-finish') { | ||
if (buffer.length > 0) { | ||
controller.enqueue({ type: 'text-delta', textDelta: buffer }); | ||
buffer = ''; | ||
// Handle special cases that should flush buffers and pass through | ||
if ( | ||
chunk.type === 'step-finish' || | ||
chunk.type === 'tool-result' || | ||
chunk.type === 'tool-call' | ||
) { | ||
// Flush text buffer if any | ||
if (textBuffer.length > 0) { | ||
controller.enqueue({ type: 'text-delta', textDelta: textBuffer }); | ||
textBuffer = ''; | ||
} | ||
|
||
// For tool-call states (call/result), clear the buffer without sending an extra delta | ||
// This prevents an extra partial-call from appearing between call and result states | ||
if (chunk.type === 'tool-call') { | ||
// Just clear the buffer without sending anything | ||
toolCallBuffer = null; | ||
} | ||
// For other cases, flush the buffer normally | ||
else if (toolCallBuffer && toolCallBuffer.buffer.length > 0) { | ||
controller.enqueue({ | ||
type: 'tool-call-delta', | ||
toolCallId: toolCallBuffer.id, | ||
toolName: toolCallBuffer.name, | ||
argsTextDelta: toolCallBuffer.buffer, | ||
}); | ||
toolCallBuffer = null; | ||
} | ||
|
||
controller.enqueue(chunk); | ||
return; | ||
} | ||
|
||
if (chunk.type !== 'text-delta') { | ||
// Pass through any non-streaming chunks | ||
if (chunk.type !== 'text-delta' && chunk.type !== 'tool-call-delta') { | ||
controller.enqueue(chunk); | ||
return; | ||
} | ||
|
||
buffer += chunk.textDelta; | ||
// Handle text-delta chunks | ||
if (chunk.type === 'text-delta') { | ||
textBuffer += chunk.textDelta; | ||
|
||
let match; | ||
while ((match = chunkingRegexp.exec(textBuffer)) != null) { | ||
const chunkText = match[0]; | ||
controller.enqueue({ type: 'text-delta', textDelta: chunkText }); | ||
textBuffer = textBuffer.slice(chunkText.length); | ||
|
||
await delay(delayInMs); | ||
} | ||
return; | ||
} | ||
|
||
// Handle tool-call-delta chunks | ||
if (chunk.type === 'tool-call-delta') { | ||
// Initialize tool call buffer if needed | ||
if (!toolCallBuffer || toolCallBuffer.id !== chunk.toolCallId) { | ||
toolCallBuffer = { | ||
id: chunk.toolCallId, | ||
name: chunk.toolName, | ||
buffer: '', | ||
}; | ||
} | ||
|
||
// Add new content to buffer | ||
toolCallBuffer.buffer += chunk.argsTextDelta; | ||
|
||
let match; | ||
while ((match = chunkingRegexp.exec(buffer)) != null) { | ||
const chunk = match[0]; | ||
controller.enqueue({ type: 'text-delta', textDelta: chunk }); | ||
buffer = buffer.slice(chunk.length); | ||
// Process buffer chunks | ||
let match; | ||
while ((match = chunkingRegexp.exec(toolCallBuffer.buffer)) != null) { | ||
const chunkText = match[0]; | ||
controller.enqueue({ | ||
type: 'tool-call-delta', | ||
toolCallId: toolCallBuffer.id, | ||
toolName: toolCallBuffer.name, | ||
argsTextDelta: chunkText, | ||
}); | ||
toolCallBuffer.buffer = toolCallBuffer.buffer.slice( | ||
chunkText.length, | ||
); | ||
|
||
await delay(delayInMs); | ||
} | ||
return; | ||
} | ||
}, | ||
flush(controller) { | ||
// Flush any remaining content in the text buffer | ||
if (textBuffer.length > 0) { | ||
controller.enqueue({ type: 'text-delta', textDelta: textBuffer }); | ||
} | ||
|
||
await delay(delayInMs); | ||
// Flush any remaining content in the tool call buffer | ||
if (toolCallBuffer && toolCallBuffer.buffer.length > 0) { | ||
controller.enqueue({ | ||
type: 'tool-call-delta', | ||
toolCallId: toolCallBuffer.id, | ||
toolName: toolCallBuffer.name, | ||
argsTextDelta: toolCallBuffer.buffer, | ||
}); | ||
} | ||
Comment on lines
+147
to
154
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bug: what if there are several ongoing tool calls? |
||
}, | ||
}); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bug: what if there are several ongoing tool calls?