mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Convert AI providers into array instead of object, allow dragging to change their order
This commit is contained in:
27 files changed
+556
-283
No files matched your search
@@ -25,12 +25,13 @@ const mockFS: JSRuntimeFS = {
|
||||
describe('SessionManager', () => {
|
||||
let sessionManager: SessionManager;
|
||||
const mockAISettings = {
|
||||
providers: {
|
||||
'test-provider': {
|
||||
providers: [
|
||||
{
|
||||
id: 'test-provider',
|
||||
baseURL: 'https://api.test.com',
|
||||
apiKey: 'test-key'
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
@@ -5,6 +5,7 @@ import { parseProviderModel } from './parseProviderModel';
|
||||
import { createAIClient } from './ai-client';
|
||||
import type { Tool } from './tools/Tool';
|
||||
import type { NUser } from '@nostrify/react/login';
|
||||
import type { AIProvider } from '@/contexts/AISettingsContext';
|
||||
|
||||
export type AIMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam | {
|
||||
role: 'assistant';
|
||||
@@ -52,13 +53,13 @@ export class SessionManager {
|
||||
private sessions = new Map<string, SessionState>();
|
||||
private listeners: Partial<Record<keyof SessionManagerEvents, Set<(...args: unknown[]) => void>>> = {};
|
||||
private fs: JSRuntimeFS;
|
||||
private aiSettings: { providers: Record<string, { baseURL: string; apiKey?: string; nostr?: boolean }> };
|
||||
private aiSettings: { providers: AIProvider[] };
|
||||
private getProviderModels?: () => Array<{ id: string; provider: string; contextLength?: number; pricing?: { prompt: import('decimal.js').Decimal; completion: import('decimal.js').Decimal } }>;
|
||||
private getCurrentUser?: () => NUser | undefined;
|
||||
|
||||
constructor(
|
||||
fs: JSRuntimeFS,
|
||||
aiSettings: { providers: Record<string, { baseURL: string; apiKey?: string; nostr?: boolean }> },
|
||||
aiSettings: { providers: AIProvider[] },
|
||||
getProviderModels?: () => Array<{ id: string; provider: string; contextLength?: number; pricing?: { prompt: import('decimal.js').Decimal; completion: import('decimal.js').Decimal } }>,
|
||||
getCurrentUser?: () => NUser | undefined
|
||||
) {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import OpenAI from 'openai';
|
||||
import { NIP98Client } from '@nostrify/nostrify';
|
||||
import type { AIConnection } from '@/contexts/AISettingsContext';
|
||||
import type { NUser } from '@nostrify/react/login';
|
||||
|
||||
/**
|
||||
@@ -8,7 +7,11 @@ import type { NUser } from '@nostrify/react/login';
|
||||
* If the connection requires Nostr authentication (NIP-98), it will use
|
||||
* the NIP98Client for authenticated requests.
|
||||
*/
|
||||
export function createAIClient(connection: AIConnection, user?: NUser): OpenAI {
|
||||
export function createAIClient(connection: {
|
||||
baseURL: string;
|
||||
apiKey?: string;
|
||||
nostr?: boolean;
|
||||
}, user?: NUser): OpenAI {
|
||||
const baseConfig: ConstructorParameters<typeof OpenAI>[0] = {
|
||||
baseURL: connection.baseURL,
|
||||
apiKey: connection.apiKey ?? '',
|
||||
|
||||
@@ -54,12 +54,13 @@ describe('configUtils', () => {
|
||||
|
||||
describe('AI Settings', () => {
|
||||
const sampleAISettings: AISettings = {
|
||||
providers: {
|
||||
'openai': {
|
||||
providers: [
|
||||
{
|
||||
id: 'openai',
|
||||
baseURL: 'https://api.openai.com/v1',
|
||||
apiKey: 'sk-test123',
|
||||
},
|
||||
},
|
||||
],
|
||||
recentlyUsedModels: ['gpt-4', 'gpt-3.5-turbo'],
|
||||
};
|
||||
|
||||
@@ -80,7 +81,7 @@ describe('configUtils', () => {
|
||||
const result = await readAISettings(mockFS);
|
||||
|
||||
expect(result).toEqual({
|
||||
providers: {},
|
||||
providers: [],
|
||||
recentlyUsedModels: [],
|
||||
});
|
||||
});
|
||||
@@ -92,7 +93,7 @@ describe('configUtils', () => {
|
||||
const result = await readAISettings(mockFS);
|
||||
|
||||
expect(result).toEqual({
|
||||
providers: {},
|
||||
providers: [],
|
||||
recentlyUsedModels: [],
|
||||
});
|
||||
});
|
||||
|
||||
+40
-4
@@ -1,5 +1,5 @@
|
||||
import type { JSRuntimeFS } from '@/lib/JSRuntime';
|
||||
import type { AISettings } from '@/contexts/AISettingsContext';
|
||||
import type { AISettings, AIProvider } from '@/contexts/AISettingsContext';
|
||||
import type { GitSettings } from '@/contexts/GitSettingsContext';
|
||||
|
||||
const CONFIG_DIR = '/config';
|
||||
@@ -27,8 +27,22 @@ async function migrateAISettingsFromLocalStorage(fs: JSRuntimeFS): Promise<AISet
|
||||
try {
|
||||
const parsed = JSON.parse(stored);
|
||||
if (parsed && typeof parsed === 'object' && 'providers' in parsed) {
|
||||
// Handle migration from old map format to new array format
|
||||
let providers;
|
||||
if (Array.isArray(parsed.providers)) {
|
||||
providers = parsed.providers;
|
||||
} else if (parsed.providers && typeof parsed.providers === 'object') {
|
||||
// Convert map to array
|
||||
providers = Object.entries(parsed.providers).map(([id, connection]) => ({
|
||||
id,
|
||||
...(connection as Omit<AIProvider, 'id'>),
|
||||
}));
|
||||
} else {
|
||||
providers = [];
|
||||
}
|
||||
|
||||
const settings: AISettings = {
|
||||
providers: parsed.providers || {},
|
||||
providers,
|
||||
recentlyUsedModels: parsed.recentlyUsedModels || [],
|
||||
};
|
||||
|
||||
@@ -51,7 +65,7 @@ async function migrateAISettingsFromLocalStorage(fs: JSRuntimeFS): Promise<AISet
|
||||
*/
|
||||
export async function readAISettings(fs: JSRuntimeFS): Promise<AISettings> {
|
||||
const defaultSettings: AISettings = {
|
||||
providers: {},
|
||||
providers: [],
|
||||
recentlyUsedModels: [],
|
||||
};
|
||||
|
||||
@@ -61,8 +75,30 @@ export async function readAISettings(fs: JSRuntimeFS): Promise<AISettings> {
|
||||
const parsed = JSON.parse(content);
|
||||
|
||||
if (parsed && typeof parsed === 'object' && 'providers' in parsed) {
|
||||
// Handle migration from old map format to new array format
|
||||
let providers;
|
||||
if (Array.isArray(parsed.providers)) {
|
||||
providers = parsed.providers;
|
||||
} else if (parsed.providers && typeof parsed.providers === 'object') {
|
||||
// Convert map to array
|
||||
providers = Object.entries(parsed.providers).map(([id, connection]) => ({
|
||||
id,
|
||||
...(connection as Omit<AIProvider, 'id'>),
|
||||
}));
|
||||
|
||||
// Write back the migrated format
|
||||
const migratedSettings: AISettings = {
|
||||
providers,
|
||||
recentlyUsedModels: parsed.recentlyUsedModels || [],
|
||||
};
|
||||
await writeAISettings(fs, migratedSettings);
|
||||
return migratedSettings;
|
||||
} else {
|
||||
providers = [];
|
||||
}
|
||||
|
||||
return {
|
||||
providers: parsed.providers || {},
|
||||
providers,
|
||||
recentlyUsedModels: parsed.recentlyUsedModels || [],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -17,8 +17,6 @@ export class Git {
|
||||
this.corsProxy = corsProxy;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Repository initialization and configuration
|
||||
async init(options: Omit<Parameters<typeof git.init>[0], 'fs'>) {
|
||||
return git.init({
|
||||
|
||||
@@ -1,33 +1,41 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { parseProviderModel } from './parseProviderModel';
|
||||
import type { AIConnection } from '@/contexts/AISettingsContext';
|
||||
import type { AIProvider } from '@/contexts/AISettingsContext';
|
||||
|
||||
describe('parseProviderModel', () => {
|
||||
const mockProviders: Record<string, AIConnection> = {
|
||||
openai: {
|
||||
const mockProviders: AIProvider[] = [
|
||||
{
|
||||
id: 'openai',
|
||||
baseURL: 'https://api.openai.com/v1',
|
||||
apiKey: 'sk-test-key',
|
||||
},
|
||||
openrouter: {
|
||||
{
|
||||
id: 'openrouter',
|
||||
baseURL: 'https://openrouter.ai/api/v1',
|
||||
apiKey: 'sk-or-test-key',
|
||||
},
|
||||
anthropic: {
|
||||
{
|
||||
id: 'anthropic',
|
||||
baseURL: 'https://api.anthropic.com',
|
||||
apiKey: 'sk-ant-test-key',
|
||||
},
|
||||
unconfigured: {
|
||||
{
|
||||
id: 'unconfigured',
|
||||
baseURL: 'https://example.com/v1',
|
||||
apiKey: '',
|
||||
},
|
||||
};
|
||||
];
|
||||
|
||||
it('should parse simple provider/model format', () => {
|
||||
const result = parseProviderModel('openai/gpt-4o', mockProviders);
|
||||
|
||||
expect(result.provider).toBe('openai');
|
||||
expect(result.model).toBe('gpt-4o');
|
||||
expect(result.connection).toEqual(mockProviders.openai);
|
||||
expect(result.connection).toEqual({
|
||||
baseURL: mockProviders[0].baseURL,
|
||||
apiKey: mockProviders[0].apiKey,
|
||||
nostr: mockProviders[0].nostr,
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse complex model names with slashes', () => {
|
||||
@@ -35,7 +43,11 @@ describe('parseProviderModel', () => {
|
||||
|
||||
expect(result.provider).toBe('openrouter');
|
||||
expect(result.model).toBe('anthropic/claude-sonnet-4');
|
||||
expect(result.connection).toEqual(mockProviders.openrouter);
|
||||
expect(result.connection).toEqual({
|
||||
baseURL: mockProviders[1].baseURL,
|
||||
apiKey: mockProviders[1].apiKey,
|
||||
nostr: mockProviders[1].nostr,
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle model names with multiple slashes', () => {
|
||||
@@ -43,7 +55,11 @@ describe('parseProviderModel', () => {
|
||||
|
||||
expect(result.provider).toBe('openrouter');
|
||||
expect(result.model).toBe('meta/llama-3.1-405b-instruct');
|
||||
expect(result.connection).toEqual(mockProviders.openrouter);
|
||||
expect(result.connection).toEqual({
|
||||
baseURL: mockProviders[1].baseURL,
|
||||
apiKey: mockProviders[1].apiKey,
|
||||
nostr: mockProviders[1].nostr,
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw error for invalid format without slash', () => {
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
import type { AIConnection } from '@/contexts/AISettingsContext';
|
||||
import type { AIProvider } from '@/contexts/AISettingsContext';
|
||||
|
||||
export interface ParsedProviderModel {
|
||||
provider: string;
|
||||
model: string;
|
||||
connection: AIConnection;
|
||||
connection: {
|
||||
baseURL: string;
|
||||
apiKey?: string;
|
||||
nostr?: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -13,29 +17,33 @@ export interface ParsedProviderModel {
|
||||
*/
|
||||
export function parseProviderModel(
|
||||
providerModelString: string,
|
||||
providers: Record<string, AIConnection>
|
||||
providers: AIProvider[]
|
||||
): ParsedProviderModel {
|
||||
const firstSlashIndex = providerModelString.indexOf('/');
|
||||
|
||||
|
||||
if (firstSlashIndex === -1) {
|
||||
throw new Error('Invalid format. Use provider/model (e.g., "openai/gpt-4o")');
|
||||
}
|
||||
|
||||
|
||||
const provider = providerModelString.substring(0, firstSlashIndex);
|
||||
const model = providerModelString.substring(firstSlashIndex + 1);
|
||||
|
||||
|
||||
if (!provider || !model) {
|
||||
throw new Error('Invalid format. Both provider and model must be specified');
|
||||
}
|
||||
|
||||
const connection = providers[provider];
|
||||
if (!connection) {
|
||||
throw new Error(`Provider "${provider}" not found. Available providers: ${Object.keys(providers).join(', ')}`);
|
||||
|
||||
const providerObj = providers.find(p => p.id === provider);
|
||||
if (!providerObj) {
|
||||
throw new Error(`Provider "${provider}" not found. Available providers: ${providers.map(p => p.id).join(', ')}`);
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
provider,
|
||||
model,
|
||||
connection,
|
||||
connection: {
|
||||
baseURL: providerObj.baseURL,
|
||||
apiKey: providerObj.apiKey,
|
||||
nostr: providerObj.nostr,
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user