-
Notifications
You must be signed in to change notification settings - Fork 0
fix: 兼容上游 reasoning 字段并统一输出 reasoning_content #78
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 all commits
Commits
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 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,98 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { openaiUpstreamAdapter } from "./openai"; | ||
|
|
||
| describe("openaiUpstreamAdapter reasoning compatibility", () => { | ||
| test("parses non-stream reasoning field into thinking block", async () => { | ||
| const response = new Response( | ||
| JSON.stringify({ | ||
| id: "chatcmpl-1", | ||
| object: "chat.completion", | ||
| created: 1700000000, | ||
| model: "test-model", | ||
| choices: [ | ||
| { | ||
| index: 0, | ||
| message: { | ||
| role: "assistant", | ||
| content: "final answer", | ||
| reasoning: "chain of thought summary", | ||
| }, | ||
| finish_reason: "stop", | ||
| }, | ||
| ], | ||
| usage: { | ||
| prompt_tokens: 1, | ||
| completion_tokens: 2, | ||
| total_tokens: 3, | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| const parsed = await openaiUpstreamAdapter.parseResponse(response); | ||
| expect(parsed.content).toEqual([ | ||
| { type: "thinking", thinking: "chain of thought summary" }, | ||
| { type: "text", text: "final answer" }, | ||
| ]); | ||
| }); | ||
|
|
||
| test("prefers reasoning_content over reasoning when both exist", async () => { | ||
| const response = new Response( | ||
| JSON.stringify({ | ||
| id: "chatcmpl-2", | ||
| object: "chat.completion", | ||
| created: 1700000001, | ||
| model: "test-model", | ||
| choices: [ | ||
| { | ||
| index: 0, | ||
| message: { | ||
| role: "assistant", | ||
| content: "final answer", | ||
| reasoning_content: "preferred reasoning content", | ||
| reasoning: "fallback reasoning", | ||
| }, | ||
| finish_reason: "stop", | ||
| }, | ||
| ], | ||
| usage: { | ||
| prompt_tokens: 1, | ||
| completion_tokens: 2, | ||
| total_tokens: 3, | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| const parsed = await openaiUpstreamAdapter.parseResponse(response); | ||
| expect(parsed.content).toEqual([ | ||
| { type: "thinking", thinking: "preferred reasoning content" }, | ||
| { type: "text", text: "final answer" }, | ||
| ]); | ||
| }); | ||
|
|
||
| test("parses stream delta reasoning field into thinking_delta", async () => { | ||
| const stream = [ | ||
| 'data: {"id":"chatcmpl-3","object":"chat.completion.chunk","created":1700000002,"model":"test-model","choices":[{"index":0,"delta":{"role":"assistant","reasoning":"stream reasoning"},"finish_reason":null}]}', | ||
| 'data: {"id":"chatcmpl-3","object":"chat.completion.chunk","created":1700000002,"model":"test-model","choices":[{"index":0,"delta":{"content":"stream text"},"finish_reason":"stop"}]}', | ||
| "data: [DONE]", | ||
| ].join("\n"); | ||
|
|
||
| const response = new Response(stream); | ||
| const chunks: Array<unknown> = []; | ||
| for await (const chunk of openaiUpstreamAdapter.parseStreamResponse( | ||
| response, | ||
| )) { | ||
| chunks.push(chunk); | ||
| } | ||
|
|
||
| expect(chunks).toContainEqual({ | ||
| type: "content_block_delta", | ||
| index: 0, | ||
| delta: { type: "thinking_delta", thinking: "stream reasoning" }, | ||
| }); | ||
| expect(chunks).toContainEqual({ | ||
| type: "content_block_delta", | ||
| index: 0, | ||
| delta: { type: "text_delta", text: "stream text" }, | ||
| }); | ||
| }); | ||
| }); |
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.
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.
To improve clarity and ensure helper functions return values that are ready for use, consider making this function body more concise. In JavaScript/TypeScript, non-empty strings are 'truthy' values, while empty strings are 'falsy' values. You can leverage the
||operator and optional chaining (?.) to achieve the same logic, making the code more concise and readable, and directly returning the desired value without requiring further processing by the caller.References