-
-
Notifications
You must be signed in to change notification settings - Fork 51
feat(cz-git): enhance OpenAI API integration with streaming support #252
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bd038a7
feat(cz-git): enhance OpenAI API integration with streaming support
Zhengqbbb 5183a6c
chore: remove npmrc
Zhengqbbb 387e4da
fix(cz-git): update DeepSeek model name and cap stream output to seen…
Zhengqbbb 9e5b3b5
chore: bump dependencies
Zhengqbbb 96cd38f
fix(cz-git): add tests and support for non-stream OpenAI chat complet…
Zhengqbbb 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 was deleted.
Oops, something went wrong.
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| export * from './editor' | ||
| export * from './util' | ||
| export * from './rule' | ||
| export * from './stream' | ||
| export * from './util' | ||
| export * from './wrap' |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /** | ||
| * @description Parse OpenAI-compatible `chat/completions` streaming (SSE) bodies | ||
| * @author Zhengqbbb <zhengqbbb@gmail.com> | ||
| * @license MIT | ||
| */ | ||
|
|
||
| import readline from 'node:readline' | ||
| import { Readable } from 'node:stream' | ||
| import type { ReadableStream as WebReadableStream } from 'node:stream/web' | ||
|
|
||
| /** | ||
| * Normalize `fetch` response body to a Node.js readable stream for `readline`. | ||
| */ | ||
| export function bodyToNodeReadable(body: unknown): NodeJS.ReadableStream { | ||
| if (body == null) | ||
| throw new Error('Response has no body') | ||
| if (typeof (body as WebReadableStream).getReader === 'function') | ||
| return Readable.fromWeb(body as WebReadableStream) | ||
| return body as NodeJS.ReadableStream | ||
| } | ||
|
|
||
| /** | ||
| * Append only user-visible completion tokens from `delta.content`. | ||
| * Skips reasoning / `reasoning_content` (not present on `content` in typical deltas). | ||
| */ | ||
| export function appendVisibleDelta(acc: string, delta: { content?: unknown } | undefined): string { | ||
| if (!delta) | ||
| return acc | ||
| const c = delta.content | ||
| if (c == null) | ||
| return acc | ||
| if (typeof c === 'string') | ||
| return acc + c | ||
| if (Array.isArray(c)) { | ||
| let s = acc | ||
| for (const p of c) { | ||
| if (p && typeof p === 'object' && (p as { type?: string, text?: string }).type === 'text') { | ||
| const t = (p as { text?: string }).text | ||
| if (typeof t === 'string') | ||
| s += t | ||
| } | ||
| } | ||
| return s | ||
| } | ||
| return acc | ||
| } | ||
|
|
||
| interface StreamChoiceChunk { index?: number, delta?: { content?: unknown } } | ||
|
|
||
| /** | ||
| * Read an SSE stream and return one finished string per completion choice. | ||
| * Buckets by `choices[].index` up to `choiceCount` (requested `n`). | ||
| * Returned length matches how many indices actually appeared in the stream (capped by `choiceCount`), | ||
| * mirroring non-stream `json.choices.length` when the provider returns fewer parallel completions. | ||
| */ | ||
| export async function readChatCompletionStreamToSubjects( | ||
| input: NodeJS.ReadableStream, | ||
| choiceCount: number, | ||
| ): Promise<string[]> { | ||
| if (choiceCount < 1) | ||
| throw new Error('choiceCount must be at least 1') | ||
|
|
||
| const buffers = Array.from({ length: choiceCount }, () => '') | ||
| let maxIndexSeen = -1 | ||
| const rl = readline.createInterface({ input, crlfDelay: Infinity }) | ||
|
|
||
| for await (const line of rl) { | ||
| const trimmed = line.trim() | ||
| if (!trimmed.startsWith('data:')) | ||
| continue | ||
| const payload = trimmed.slice(5).trim() | ||
| if (payload === '[DONE]') | ||
| continue | ||
| try { | ||
| const json = JSON.parse(payload) as { | ||
| error?: { message?: string } | ||
| choices?: StreamChoiceChunk[] | ||
| } | ||
| if (json.error) | ||
| throw new Error(json.error.message || 'OpenAI stream error') | ||
|
|
||
| for (const ch of json.choices ?? []) { | ||
| const idx = typeof ch.index === 'number' ? ch.index : 0 | ||
| if (idx >= 0 && idx < choiceCount) { | ||
| buffers[idx] = appendVisibleDelta(buffers[idx], ch.delta) | ||
| maxIndexSeen = Math.max(maxIndexSeen, idx) | ||
| } | ||
| } | ||
| } | ||
| catch (e) { | ||
| if (e instanceof SyntaxError) | ||
| continue | ||
| throw e | ||
| } | ||
| } | ||
|
|
||
| const effectiveLen = maxIndexSeen < 0 ? 1 : maxIndexSeen + 1 | ||
| return buffers.slice(0, effectiveLen) | ||
|
Zhengqbbb marked this conversation as resolved.
Outdated
|
||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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.
Uh oh!
There was an error while loading. Please reload this page.