-
Notifications
You must be signed in to change notification settings - Fork 380
feat: Add OpenAI provider support for AI assistance #1960
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
brandon-pereira
merged 9 commits into
hyperdxio:main
from
vinzee:va/enable_ai_assistance
Mar 23, 2026
+335
−6
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3099f24
feat: Add OpenAI provider support for AI assistance
vinzee b454f72
Address code review feedback on the OpenAI provider implementation:
vinzee 85997a5
fix: Address review feedback on OpenAI provider
vinzee 4f49f7f
Fix linter errors
vinzee 3e05a19
fix: add ChangeSet
vinzee fc75398
Add comprehensive tests for AI model configuration
vinzee 95c3723
Address PR comments
vinzee 71e97b0
Remove AI_EXTRA_BODY and custom fetch wrapper from OpenAI provider
vinzee ab9933b
Refactor AI request headers for greater flexibility
vinzee 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,5 @@ | ||
| --- | ||
| '@hyperdx/api': patch | ||
| --- | ||
|
|
||
| feat: Add OpenAI provider support for AI assistance |
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 |
|---|---|---|
| @@ -0,0 +1,252 @@ | ||
| import type { LanguageModel } from 'ai'; | ||
|
|
||
| const mockAnthropicModel = { | ||
| modelId: 'claude-sonnet-4-5-20250929', | ||
| } as unknown as LanguageModel; | ||
|
|
||
| const mockOpenAIModel = { | ||
| modelId: 'gpt-4o', | ||
| } as unknown as LanguageModel; | ||
|
|
||
| const mockAnthropicFactory = jest.fn((_model?: string) => mockAnthropicModel); | ||
| const mockCreateAnthropic = jest.fn( | ||
| (_opts?: Record<string, unknown>) => mockAnthropicFactory, | ||
| ); | ||
|
|
||
| const mockOpenAIChatFactory = jest.fn((_model?: string) => mockOpenAIModel); | ||
| const mockCreateOpenAI = jest.fn((_opts?: Record<string, unknown>) => ({ | ||
| chat: mockOpenAIChatFactory, | ||
| })); | ||
|
|
||
| jest.mock('@ai-sdk/anthropic', () => ({ | ||
| createAnthropic: (opts: Record<string, unknown>) => mockCreateAnthropic(opts), | ||
| })); | ||
|
|
||
| jest.mock('@ai-sdk/openai', () => ({ | ||
| createOpenAI: (opts: Record<string, unknown>) => mockCreateOpenAI(opts), | ||
| })); | ||
|
|
||
| jest.mock('@/utils/logger', () => ({ | ||
| __esModule: true, | ||
| default: { | ||
| info: jest.fn(), | ||
| warn: jest.fn(), | ||
| error: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| const mockConfig: Record<string, unknown> = { __esModule: true }; | ||
|
|
||
| jest.mock('@/config', () => mockConfig); | ||
|
|
||
| function setConfig(overrides: Record<string, string | undefined>) { | ||
| Object.keys(mockConfig).forEach(k => { | ||
| if (k !== '__esModule') delete mockConfig[k]; | ||
| }); | ||
| Object.assign(mockConfig, overrides); | ||
| } | ||
|
|
||
| import { getAIModel } from '@/controllers/ai'; | ||
|
|
||
| beforeEach(() => { | ||
| setConfig({}); | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('getAIModel', () => { | ||
| describe('provider routing', () => { | ||
| it('throws when no provider is configured', () => { | ||
| expect(() => getAIModel()).toThrow( | ||
| 'No AI provider configured. Set AI_PROVIDER and AI_API_KEY environment variables.', | ||
| ); | ||
| }); | ||
|
|
||
| it('throws on unknown provider', () => { | ||
| setConfig({ AI_PROVIDER: 'gemini' }); | ||
| expect(() => getAIModel()).toThrow( | ||
| 'Unknown AI provider: gemini. Currently supported: anthropic, openai', | ||
| ); | ||
| }); | ||
|
|
||
| it('routes to anthropic when AI_PROVIDER=anthropic', () => { | ||
| setConfig({ | ||
| AI_PROVIDER: 'anthropic', | ||
| AI_API_KEY: 'sk-test', | ||
| }); | ||
| const model = getAIModel(); | ||
| expect(model).toBe(mockAnthropicModel); | ||
| expect(mockCreateAnthropic).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('routes to openai when AI_PROVIDER=openai', () => { | ||
| setConfig({ | ||
| AI_PROVIDER: 'openai', | ||
| AI_API_KEY: 'sk-test', | ||
| AI_MODEL_NAME: 'gpt-4o', | ||
| }); | ||
| const model = getAIModel(); | ||
| expect(model).toBe(mockOpenAIModel); | ||
| expect(mockCreateOpenAI).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('legacy anthropic support', () => { | ||
| it('falls back to anthropic when ANTHROPIC_API_KEY is set without AI_PROVIDER', () => { | ||
| setConfig({ | ||
| ANTHROPIC_API_KEY: 'sk-ant-legacy', | ||
| }); | ||
| const model = getAIModel(); | ||
| expect(model).toBe(mockAnthropicModel); | ||
| expect(mockCreateAnthropic).toHaveBeenCalledWith( | ||
| expect.objectContaining({ apiKey: 'sk-ant-legacy' }), | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('anthropic provider', () => { | ||
| it('throws when no API key is set', () => { | ||
| setConfig({ AI_PROVIDER: 'anthropic' }); | ||
| expect(() => getAIModel()).toThrow( | ||
| 'No API key defined for Anthropic. Set AI_API_KEY or ANTHROPIC_API_KEY.', | ||
| ); | ||
| }); | ||
|
|
||
| it('uses AI_API_KEY over ANTHROPIC_API_KEY', () => { | ||
| setConfig({ | ||
| AI_PROVIDER: 'anthropic', | ||
| AI_API_KEY: 'sk-new', | ||
| ANTHROPIC_API_KEY: 'sk-old', | ||
| }); | ||
| getAIModel(); | ||
| expect(mockCreateAnthropic).toHaveBeenCalledWith( | ||
| expect.objectContaining({ apiKey: 'sk-new' }), | ||
| ); | ||
| }); | ||
|
|
||
| it('passes baseURL when AI_BASE_URL is set', () => { | ||
| setConfig({ | ||
| AI_PROVIDER: 'anthropic', | ||
| AI_API_KEY: 'sk-test', | ||
| AI_BASE_URL: 'https://custom.endpoint.com', | ||
| }); | ||
| getAIModel(); | ||
| expect(mockCreateAnthropic).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| apiKey: 'sk-test', | ||
| baseURL: 'https://custom.endpoint.com', | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it('uses default model when AI_MODEL_NAME is not set', () => { | ||
| setConfig({ | ||
| AI_PROVIDER: 'anthropic', | ||
| AI_API_KEY: 'sk-test', | ||
| }); | ||
| getAIModel(); | ||
| expect(mockAnthropicFactory).toHaveBeenCalledWith( | ||
| 'claude-sonnet-4-5-20250929', | ||
| ); | ||
| }); | ||
|
|
||
| it('uses custom model name when AI_MODEL_NAME is set', () => { | ||
| setConfig({ | ||
| AI_PROVIDER: 'anthropic', | ||
| AI_API_KEY: 'sk-test', | ||
| AI_MODEL_NAME: 'claude-3-haiku-20240307', | ||
| }); | ||
| getAIModel(); | ||
| expect(mockAnthropicFactory).toHaveBeenCalledWith( | ||
| 'claude-3-haiku-20240307', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('openai provider', () => { | ||
| it('throws when no API key is set', () => { | ||
| setConfig({ AI_PROVIDER: 'openai' }); | ||
| expect(() => getAIModel()).toThrow( | ||
| 'No API key defined for OpenAI provider. Set AI_API_KEY.', | ||
| ); | ||
| }); | ||
|
|
||
| it('throws when no model name is set', () => { | ||
| setConfig({ | ||
| AI_PROVIDER: 'openai', | ||
| AI_API_KEY: 'sk-test', | ||
| }); | ||
| expect(() => getAIModel()).toThrow( | ||
| 'No model name configured for OpenAI provider. Set AI_MODEL_NAME', | ||
| ); | ||
| }); | ||
|
|
||
| it('creates provider with minimal config', () => { | ||
| setConfig({ | ||
| AI_PROVIDER: 'openai', | ||
| AI_API_KEY: 'sk-test', | ||
| AI_MODEL_NAME: 'gpt-4o', | ||
| }); | ||
| getAIModel(); | ||
| expect(mockCreateOpenAI).toHaveBeenCalledWith( | ||
| expect.objectContaining({ apiKey: 'sk-test' }), | ||
| ); | ||
| expect(mockOpenAIChatFactory).toHaveBeenCalledWith('gpt-4o'); | ||
| }); | ||
|
|
||
| it('passes baseURL when AI_BASE_URL is set', () => { | ||
| setConfig({ | ||
| AI_PROVIDER: 'openai', | ||
| AI_API_KEY: 'sk-test', | ||
| AI_MODEL_NAME: 'gpt-4o', | ||
| AI_BASE_URL: 'https://proxy.example.com/v1', | ||
| }); | ||
| getAIModel(); | ||
| expect(mockCreateOpenAI).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| apiKey: 'sk-test', | ||
| baseURL: 'https://proxy.example.com/v1', | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| describe('AI_REQUEST_HEADERS', () => { | ||
| it('passes parsed headers to createOpenAI', () => { | ||
| setConfig({ | ||
| AI_PROVIDER: 'openai', | ||
| AI_API_KEY: 'sk-test', | ||
| AI_MODEL_NAME: 'gpt-4o', | ||
| AI_REQUEST_HEADERS: '{"X-Custom":"val1","X-Other":"val2"}', | ||
| }); | ||
| getAIModel(); | ||
| expect(mockCreateOpenAI).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| headers: { 'X-Custom': 'val1', 'X-Other': 'val2' }, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it('throws when AI_REQUEST_HEADERS is invalid JSON', () => { | ||
| setConfig({ | ||
| AI_PROVIDER: 'openai', | ||
| AI_API_KEY: 'sk-test', | ||
| AI_MODEL_NAME: 'gpt-4o', | ||
| AI_REQUEST_HEADERS: '{bad', | ||
| }); | ||
| expect(() => getAIModel()).toThrow( | ||
| 'AI_REQUEST_HEADERS is not valid JSON', | ||
| ); | ||
| }); | ||
|
|
||
| it('omits headers when AI_REQUEST_HEADERS is not set', () => { | ||
| setConfig({ | ||
| AI_PROVIDER: 'openai', | ||
| AI_API_KEY: 'sk-test', | ||
| AI_MODEL_NAME: 'gpt-4o', | ||
| }); | ||
| getAIModel(); | ||
| const call = mockCreateOpenAI.mock.calls[0]?.[0]; | ||
| expect(call?.headers).toBeUndefined(); | ||
| }); | ||
| }); | ||
| }); | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.