You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Stop streamed text at the earliest matching sequence - #1769
With stops ordered as ['<LATE>', '<EARLY>'], a text delta containing answer<EARLY>hidden<LATE>tail currently emits answer<EARLY>hidden. The handler returns on the first configured sequence it finds instead of the first match in the text. Splitting the same response just after <EARLY> instead emits only answer.
The SDK uses this handler in promptAiSdkStream. Emitting a configured stop marker and the following text violates the stop contract that returned text excludes the stop sequence (API reference).
Changes
Select the smallest complete-match index in the buffered text before entering the finished state. Keep the existing partial-sequence buffering, flush behavior, and handler API.
Add one regression using two configuration orders and every two-chunk split of the same response. It checks the output, completion state, and suppression of later text.
Verification
On unchanged main c21711a, the new regression fails; the two affected test files report 19 passed / 1 failed. With the fix: 20 passed / 0 failed, 327 assertions.
An injected-fetch SSE fixture through the actual SDK provider converter and promptAiSdkStream also fails on main. With the fix, both combined and split deltas emit only answer, make one request each, and yield no error chunks.
Common and SDK typechecks pass with the locked native TypeScript 7.0.2 compiler; frozen-lockfile install passes.
ESLint: 0 errors, 4 local resolver warnings. The new test is formatted; the source file's existing Prettier differences are unchanged.
Raw SDK build fails on both main and this branch: the lock assigns TypeScript 7.0.2 to dts-bundle-generator, whose import needs ts.sys. Raw binary build fails through the same SDK step.
Supplemental SDK declaration/binary builds, version, and smoke pass when only the local declaration-builder dependency is temporarily mapped to the already installed JavaScript TypeScript 5.5.4. The original dependency directory was restored afterward. These are supplemental results, not a pass of the unmodified dependency build.
Notes / Scope
This changes text filtering when multiple complete stops are available in one buffer. Existing overlap/prefix buffering semantics stay as they are. The patch contains two common-package files and no dependency, compiler, cancellation, retry, or tool-execution changes.
Validation ran on Windows with Bun 1.3.14 and local fixtures. Linux/macOS, live providers, and the full monorepo suite were not run. The independent declaration-build dependency problem remains for the upstream export to resolve.
Good catch and a clean, minimal fix. The bug is real: iterating stopSequences in configured order and returning on the first indexOf hit means a later-configured-but-earlier-occurring stop sequence gets missed, which indeed violates the stop contract (text after the earliest stop marker should never leak out). The fix in stop-sequence.ts correctly scans all stop sequences, tracks the minimum index, and only finalizes after checking them all — that's the right approach and matches how this class of 'first match in text' logic should work.
The added test in stop-sequence.test.ts is thorough: it checks both configuration orders and every two-chunk split point of the sample text, which is a sensible way to exercise the partial-buffer logic alongside the new earliest-match logic. That's more rigor than most PRs of this size bring.
A few things worth tightening before merge:
The loop still does a fresh this.buffer.indexOf(stopSequence) per stop sequence per chunk, which is fine for correctness but O(n*m) on large buffers with many stop sequences — not a blocker, just worth a comment if intentional.
Worth adding a test case where one stop sequence is a prefix of another (e.g. <A> and <AB>) combined with the earliest-index logic, since that's the trickiest interaction with the existing overlap-buffering code that wasn't touched.
The PR body's extensive verification notes about unrelated build/toolchain issues (dts-bundle-generator, raw SDK build) are good diligence but not relevant to reviewing this change itself; that noise could be trimmed in future PRs.
This file contains hidden or 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
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.
Problem / Why
With stops ordered as
['<LATE>', '<EARLY>'], a text delta containinganswer<EARLY>hidden<LATE>tailcurrently emitsanswer<EARLY>hidden. The handler returns on the first configured sequence it finds instead of the first match in the text. Splitting the same response just after<EARLY>instead emits onlyanswer.The SDK uses this handler in
promptAiSdkStream. Emitting a configured stop marker and the following text violates the stop contract that returned text excludes the stop sequence (API reference).Changes
Select the smallest complete-match index in the buffered text before entering the finished state. Keep the existing partial-sequence buffering, flush behavior, and handler API.
Add one regression using two configuration orders and every two-chunk split of the same response. It checks the output, completion state, and suppression of later text.
Verification
c21711a, the new regression fails; the two affected test files report 19 passed / 1 failed. With the fix: 20 passed / 0 failed, 327 assertions.promptAiSdkStreamalso fails on main. With the fix, both combined and split deltas emit onlyanswer, make one request each, and yield no error chunks.dts-bundle-generator, whose import needsts.sys. Raw binary build fails through the same SDK step.Notes / Scope
This changes text filtering when multiple complete stops are available in one buffer. Existing overlap/prefix buffering semantics stay as they are. The patch contains two common-package files and no dependency, compiler, cancellation, retry, or tool-execution changes.
Validation ran on Windows with Bun 1.3.14 and local fixtures. Linux/macOS, live providers, and the full monorepo suite were not run. The independent declaration-build dependency problem remains for the upstream export to resolve.