-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: add URL backend for custom LLM endpoints #9640
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
+1,566
−697
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Large diffs are not rendered by default.
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
235 changes: 235 additions & 0 deletions
235
packages/insomnia/src/main/__tests__/llm-config-service.test.ts
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,235 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import * as models from '../../models'; | ||
| import { | ||
| clearActiveBackend, | ||
| getActiveBackend, | ||
| getAllConfigurations, | ||
| getBackendConfig, | ||
| setActiveBackend, | ||
| updateBackendConfig, | ||
| } from '../llm-config-service'; | ||
|
|
||
| vi.mock('../../models', () => ({ | ||
| pluginData: { | ||
| getByKey: vi.fn(), | ||
| upsertByKey: vi.fn(), | ||
| removeByKey: vi.fn(), | ||
| all: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('electron', () => ({ | ||
| app: { | ||
| getPath: vi.fn(() => '/mock/user/data'), | ||
| }, | ||
| net: { | ||
| fetch: vi.fn(() => Promise.resolve({ ok: true })), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('@sentry/electron/main', () => ({ | ||
| init: vi.fn(), | ||
| captureException: vi.fn(), | ||
| captureMessage: vi.fn(), | ||
| })); | ||
|
|
||
| const mockPluginData = (key: string, value: string) => ({ key, value }) as any; | ||
|
|
||
| describe('llm-config-service', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('getBackendConfig()', () => { | ||
| it('should retrieve url field from storage', async () => { | ||
| vi.mocked(models.pluginData.all).mockResolvedValue([ | ||
| mockPluginData('url.model', 'gpt-4'), | ||
| mockPluginData('url.url', 'https://api.example.com/v1'), | ||
| ]); | ||
|
|
||
| const config = await getBackendConfig('url'); | ||
|
|
||
| expect(config).toEqual({ | ||
| backend: 'url', | ||
| model: 'gpt-4', | ||
| url: 'https://api.example.com/v1', | ||
| }); | ||
| }); | ||
|
|
||
| it('should retrieve baseURL field from storage', async () => { | ||
| vi.mocked(models.pluginData.all).mockResolvedValue([ | ||
| mockPluginData('url.model', 'claude-3'), | ||
| mockPluginData('url.baseURL', 'https://custom-llm.com'), | ||
| ]); | ||
|
|
||
| const config = await getBackendConfig('url'); | ||
|
|
||
| expect(config).toEqual({ | ||
| backend: 'url', | ||
| model: 'claude-3', | ||
| baseURL: 'https://custom-llm.com', | ||
| }); | ||
| }); | ||
|
|
||
| it('should handle both url and baseURL fields', async () => { | ||
| vi.mocked(models.pluginData.all).mockResolvedValue([ | ||
| mockPluginData('url.url', 'https://api.example.com/v1'), | ||
| mockPluginData('url.baseURL', 'https://base.example.com'), | ||
| mockPluginData('url.model', 'test-model'), | ||
| ]); | ||
|
|
||
| const config = await getBackendConfig('url'); | ||
|
|
||
| expect(config.url).toBe('https://api.example.com/v1'); | ||
| expect(config.baseURL).toBe('https://base.example.com'); | ||
| expect(config.model).toBe('test-model'); | ||
| }); | ||
|
|
||
| it('should return empty config for unconfigured backend', async () => { | ||
| vi.mocked(models.pluginData.all).mockResolvedValue([]); | ||
|
|
||
| const config = await getBackendConfig('url'); | ||
|
|
||
| expect(config).toEqual({ | ||
| backend: 'url', | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('updateBackendConfig()', () => { | ||
| it('should save url field to storage', async () => { | ||
| await updateBackendConfig('url', { | ||
| url: 'https://api.example.com/v1', | ||
| model: 'gpt-4', | ||
| }); | ||
|
|
||
| expect(models.pluginData.upsertByKey).toHaveBeenCalledWith( | ||
| 'insomnia-llm', | ||
| 'url.url', | ||
| 'https://api.example.com/v1' | ||
| ); | ||
| expect(models.pluginData.upsertByKey).toHaveBeenCalledWith( | ||
| 'insomnia-llm', | ||
| 'url.model', | ||
| 'gpt-4' | ||
| ); | ||
| }); | ||
|
|
||
| it('should save baseURL field to storage', async () => { | ||
| await updateBackendConfig('url', { | ||
| baseURL: 'https://custom-llm.com', | ||
| model: 'claude-3', | ||
| }); | ||
|
|
||
| expect(models.pluginData.upsertByKey).toHaveBeenCalledWith( | ||
| 'insomnia-llm', | ||
| 'url.baseURL', | ||
| 'https://custom-llm.com' | ||
| ); | ||
| }); | ||
|
|
||
| it('should handle partial config updates', async () => { | ||
| await updateBackendConfig('url', { | ||
| url: 'https://new-url.com/v1', | ||
| }); | ||
|
|
||
| expect(models.pluginData.upsertByKey).toHaveBeenCalledWith( | ||
| 'insomnia-llm', | ||
| 'url.url', | ||
| 'https://new-url.com/v1' | ||
| ); | ||
| expect(models.pluginData.upsertByKey).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should not save backend field', async () => { | ||
| await updateBackendConfig('url', { | ||
| backend: 'url', | ||
| url: 'https://api.example.com/v1', | ||
| }); | ||
|
|
||
| const calls = vi.mocked(models.pluginData.upsertByKey).mock.calls; | ||
| const backendFieldCall = calls.find(call => call[1] === 'url.backend'); | ||
| expect(backendFieldCall).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getAllConfigurations()', () => { | ||
| it('should include url backend in configurations', async () => { | ||
| vi.mocked(models.pluginData.all).mockResolvedValue([ | ||
| mockPluginData('url.model', 'gpt-4'), | ||
| mockPluginData('url.url', 'https://api.example.com/v1'), | ||
| mockPluginData('gguf.model', 'llama-3'), | ||
| ]); | ||
|
|
||
| const configs = await getAllConfigurations(); | ||
|
|
||
| const urlConfig = configs.find(c => c.backend === 'url'); | ||
| expect(urlConfig).toBeDefined(); | ||
| expect(urlConfig?.url).toBe('https://api.example.com/v1'); | ||
| expect(urlConfig?.model).toBe('gpt-4'); | ||
| }); | ||
|
|
||
| it('should filter out unconfigured backends', async () => { | ||
| vi.mocked(models.pluginData.all).mockResolvedValue([ | ||
| mockPluginData('claude.model', 'claude-3-opus'), | ||
| mockPluginData('claude.apiKey', 'sk-ant-123'), | ||
| ]); | ||
|
|
||
| const configs = await getAllConfigurations(); | ||
|
|
||
| // Should only return claude since it's the only one configured | ||
| expect(configs).toHaveLength(1); | ||
| expect(configs[0].backend).toBe('claude'); | ||
| }); | ||
|
|
||
| it('should include backend with only url field set', async () => { | ||
| vi.mocked(models.pluginData.all).mockResolvedValue([ | ||
| mockPluginData('url.url', 'https://api.example.com/v1'), | ||
| ]); | ||
|
|
||
| const configs = await getAllConfigurations(); | ||
|
|
||
| const urlConfig = configs.find(c => c.backend === 'url'); | ||
| expect(urlConfig).toBeDefined(); | ||
| expect(urlConfig?.url).toBe('https://api.example.com/v1'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Active backend management', () => { | ||
| it('should set url as active backend', async () => { | ||
| await setActiveBackend('url'); | ||
|
|
||
| expect(models.pluginData.upsertByKey).toHaveBeenCalledWith( | ||
| 'insomnia-llm', | ||
| 'model.active', | ||
| 'url' | ||
| ); | ||
| }); | ||
|
|
||
| it('should get url as active backend', async () => { | ||
| vi.mocked(models.pluginData.getByKey).mockResolvedValue({ value: 'url' } as any); | ||
|
|
||
| const active = await getActiveBackend(); | ||
|
|
||
| expect(active).toBe('url'); | ||
| }); | ||
|
|
||
| it('should return null when no active backend', async () => { | ||
| vi.mocked(models.pluginData.getByKey).mockResolvedValue(undefined as any); | ||
|
|
||
| const active = await getActiveBackend(); | ||
|
|
||
| expect(active).toBeNull(); | ||
| }); | ||
|
|
||
| it('should clear active backend', async () => { | ||
| await clearActiveBackend(); | ||
|
|
||
| expect(models.pluginData.removeByKey).toHaveBeenCalledWith( | ||
| 'insomnia-llm', | ||
| 'model.active' | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
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
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.
https://github.com/Kong/insomnia/blob/develop/packages/insomnia/src/plugins/types.ts#L3-L25 is used in a few places and is missing the new backend type, along with the url field. Right now, the generate commit diffs and mcp sampling both use this interface and would likely fail if the url backend were configured
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.
Good catch. Let me update this, and some other fields in this file to show what the LLM URL option can support