-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: add unverified organizations mode for openai #8200
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
fbuechler
wants to merge
6
commits into
RooCodeInc:main
Choose a base branch
from
fbuechler:feat/openai-unverified-organization
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 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c7f4096
feat: add unverified organizations mode for openai
fbuechler 32fcebc
chore: add missing translations
fbuechler a552493
chore: code improvements
fbuechler c0fbde7
chore: extract retry logic
fbuechler 9c2d228
feat: add options to disable streaming and reasoning summaries for Op…
fbuechler 05eb5bb
fix: missing translations
fbuechler 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
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 |
|---|---|---|
|
|
@@ -125,6 +125,143 @@ describe("OpenAiNativeHandler", () => { | |
| } | ||
| }).rejects.toThrow("OpenAI service error") | ||
| }) | ||
|
|
||
| it("should handle non-streaming responses via SDK when stream=false", async () => { | ||
| // Reconfigure handler to force non-stream (buildRequestBody sets stream = !openAiNativeUnverifiedOrg) | ||
| handler = new OpenAiNativeHandler({ | ||
| ...mockOptions, | ||
| openAiNativeUnverifiedOrg: true, // => stream: false | ||
| }) | ||
|
|
||
| // Mock SDK non-streaming JSON response | ||
| mockResponsesCreate.mockResolvedValueOnce({ | ||
| id: "resp_nonstream_1", | ||
| output: [ | ||
| { | ||
| type: "message", | ||
| content: [{ type: "output_text", text: "Non-streamed reply" }], | ||
| }, | ||
| ], | ||
| usage: { | ||
| input_tokens: 12, | ||
| output_tokens: 7, | ||
| cache_read_input_tokens: 0, | ||
| cache_creation_input_tokens: 0, | ||
| }, | ||
| }) | ||
|
|
||
| const stream = handler.createMessage(systemPrompt, messages) | ||
| const chunks: any[] = [] | ||
| for await (const chunk of stream) { | ||
| chunks.push(chunk) | ||
| } | ||
|
|
||
| // Verify yielded content and usage from non-streaming path | ||
| expect(chunks.length).toBeGreaterThan(0) | ||
| expect(chunks[0]).toEqual({ type: "text", text: "Non-streamed reply" }) | ||
| const usage = chunks.find((c) => c.type === "usage") | ||
| expect(usage).toBeTruthy() | ||
| expect(usage.inputTokens).toBe(12) | ||
| expect(usage.outputTokens).toBe(7) | ||
|
|
||
| // Ensure SDK was called with stream=false and structured input | ||
| expect(mockResponsesCreate).toHaveBeenCalledTimes(1) | ||
| const body = mockResponsesCreate.mock.calls[0][0] | ||
| expect(body.stream).toBe(false) | ||
| expect(body.instructions).toBe(systemPrompt) | ||
| expect(body.input).toEqual([{ role: "user", content: [{ type: "input_text", text: "Hello!" }] }]) | ||
| }) | ||
|
|
||
| it("should retry non-streaming when previous_response_id is invalid (400) and then succeed", async () => { | ||
| // Reconfigure handler to force non-stream (stream=false) | ||
| handler = new OpenAiNativeHandler({ | ||
| ...mockOptions, | ||
| openAiNativeUnverifiedOrg: true, | ||
| }) | ||
|
|
||
| // First SDK call fails with 400 indicating previous_response_id not found | ||
| const err: any = new Error("Previous response not found") | ||
| err.status = 400 | ||
| err.response = { status: 400 } | ||
| mockResponsesCreate.mockRejectedValueOnce(err).mockResolvedValueOnce({ | ||
| id: "resp_after_retry", | ||
| output: [ | ||
| { | ||
| type: "message", | ||
| content: [{ type: "output_text", text: "Reply after retry" }], | ||
| }, | ||
| ], | ||
| usage: { | ||
| input_tokens: 9, | ||
| output_tokens: 3, | ||
| cache_read_input_tokens: 0, | ||
| cache_creation_input_tokens: 0, | ||
| }, | ||
| }) | ||
|
|
||
| const stream = handler.createMessage(systemPrompt, messages, { | ||
| taskId: "t-1", | ||
| previousResponseId: "resp_invalid", | ||
| }) | ||
|
|
||
| const chunks: any[] = [] | ||
| for await (const chunk of stream) { | ||
| chunks.push(chunk) | ||
| } | ||
|
|
||
| // Two SDK calls (retry path) | ||
| expect(mockResponsesCreate).toHaveBeenCalledTimes(2) | ||
|
|
||
| // First call: includes previous_response_id and only latest user message | ||
| const firstBody = mockResponsesCreate.mock.calls[0][0] | ||
| expect(firstBody.stream).toBe(false) | ||
| expect(firstBody.previous_response_id).toBe("resp_invalid") | ||
| expect(firstBody.input).toEqual([{ role: "user", content: [{ type: "input_text", text: "Hello!" }] }]) | ||
|
|
||
| // Second call (retry): no previous_response_id, includes full conversation (still single latest message in this test) | ||
| const secondBody = mockResponsesCreate.mock.calls[1][0] | ||
| expect(secondBody.stream).toBe(false) | ||
| expect(secondBody.previous_response_id).toBeUndefined() | ||
| expect(secondBody.instructions).toBe(systemPrompt) | ||
| // With only one message in this suite, the "full conversation" equals the single user message | ||
| expect(secondBody.input).toEqual([{ role: "user", content: [{ type: "input_text", text: "Hello!" }] }]) | ||
|
|
||
| // Verify yielded chunks from retry | ||
| expect(chunks[0]).toEqual({ type: "text", text: "Reply after retry" }) | ||
| const usage = chunks.find((c) => c.type === "usage") | ||
| expect(usage.inputTokens).toBe(9) | ||
| expect(usage.outputTokens).toBe(3) | ||
| }) | ||
|
|
||
| it("should NOT fallback to SSE when unverified org is true and non-stream SDK error occurs", async () => { | ||
| // Force non-stream path via unverified org toggle | ||
| handler = new OpenAiNativeHandler({ | ||
| ...mockOptions, | ||
| openAiNativeUnverifiedOrg: true, // => stream: false | ||
| }) | ||
|
|
||
| // Make SDK throw a non-previous_response error (e.g., 500) | ||
| const err: any = new Error("Some server error") | ||
| err.status = 500 | ||
| err.response = { status: 500 } | ||
| mockResponsesCreate.mockRejectedValueOnce(err) | ||
|
|
||
| // Prepare a fetch mock to detect any unintended SSE fallback usage | ||
| const mockFetch = vitest.fn() | ||
| ;(global as any).fetch = mockFetch as any | ||
|
|
||
| const stream = handler.createMessage(systemPrompt, messages) | ||
|
|
||
| // Expect iteration to reject and no SSE fallback to be attempted | ||
| await expect(async () => { | ||
| for await (const _ of stream) { | ||
| // consume | ||
| } | ||
| }).rejects.toThrow("Some server error") | ||
|
|
||
| // Ensure SSE fallback was NOT invoked | ||
| expect(mockFetch).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
|
|
||
| describe("completePrompt", () => { | ||
|
|
@@ -1734,3 +1871,87 @@ describe("GPT-5 streaming event coverage (additional)", () => { | |
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe("Unverified org gating behavior", () => { | ||
| beforeEach(() => { | ||
| // Ensure call counts don't accumulate from previous test suites | ||
| mockResponsesCreate.mockClear() | ||
| // Ensure no SSE fallback interference | ||
| if ((global as any).fetch) { | ||
| delete (global as any).fetch | ||
| } | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| // Clean up any accidental fetch mocks | ||
| if ((global as any).fetch) { | ||
| delete (global as any).fetch | ||
| } | ||
| }) | ||
|
|
||
| it("omits reasoning.summary in createMessage request when unverified org is true (GPT-5)", async () => { | ||
| // Arrange | ||
| const handler = new OpenAiNativeHandler({ | ||
| apiModelId: "gpt-5-2025-08-07", | ||
| openAiNativeApiKey: "test-api-key", | ||
| openAiNativeUnverifiedOrg: true, // => stream=false, and summary must be omitted | ||
| }) | ||
|
|
||
| // SDK returns a minimal valid non-stream response | ||
| mockResponsesCreate.mockResolvedValueOnce({ | ||
| id: "resp_nonstream_2", | ||
| output: [], | ||
| usage: { input_tokens: 1, output_tokens: 1 }, | ||
| }) | ||
|
|
||
| // Act | ||
| const systemPrompt = "You are a helpful assistant." | ||
| const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hello!" }] | ||
| const stream = handler.createMessage(systemPrompt, messages) | ||
| for await (const _ of stream) { | ||
| // drain | ||
| } | ||
|
|
||
| // Assert | ||
| expect(mockResponsesCreate).toHaveBeenCalledTimes(1) | ||
| const body = mockResponsesCreate.mock.calls[0][0] | ||
| expect(body.model).toBe("gpt-5-2025-08-07") | ||
| expect(body.stream).toBe(false) | ||
| // GPT-5 includes reasoning effort; summary must be omitted for unverified orgs | ||
| expect(body.reasoning?.effort).toBeDefined() | ||
| expect(body.reasoning?.summary).toBeUndefined() | ||
|
Contributor
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. Consider adding a test that explicitly verifies reasoning summaries ARE included when openAiNativeUnverifiedOrg is false, to ensure the feature works both ways. Currently tests only verify the omission case. |
||
| }) | ||
|
|
||
| it("omits reasoning.summary in completePrompt request when unverified org is true (GPT-5)", async () => { | ||
| // Arrange | ||
| const handler = new OpenAiNativeHandler({ | ||
| apiModelId: "gpt-5-2025-08-07", | ||
| openAiNativeApiKey: "test-api-key", | ||
| openAiNativeUnverifiedOrg: true, // => summary must be omitted in completePrompt too | ||
| }) | ||
|
|
||
| // SDK returns a non-stream completion | ||
| mockResponsesCreate.mockResolvedValueOnce({ | ||
| output: [ | ||
| { | ||
| type: "message", | ||
| content: [{ type: "output_text", text: "Completion" }], | ||
| }, | ||
| ], | ||
| }) | ||
|
|
||
| // Act | ||
| const result = await handler.completePrompt("Prompt text") | ||
|
|
||
| // Assert | ||
| expect(result).toBe("Completion") | ||
| expect(mockResponsesCreate).toHaveBeenCalledTimes(1) | ||
| const body = mockResponsesCreate.mock.calls[0][0] | ||
| expect(body.model).toBe("gpt-5-2025-08-07") | ||
| expect(body.stream).toBe(false) | ||
| expect(body.store).toBe(false) | ||
| // Reasoning present, but summary must be omitted | ||
| expect(body.reasoning?.effort).toBeDefined() | ||
| expect(body.reasoning?.summary).toBeUndefined() | ||
| }) | ||
| }) | ||
Oops, something went wrong.
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.
Consider adding JSDoc documentation for this new field explaining when and why users should enable this option.