Make Nostr Auth work

This commit is contained in:
Alex Gleason committed 2025-09-11 17:53:36 -05:00
1 parent d7807f3911
commit bd0ed0a10f
13 files changed
+152 -113

No files matched your search

+10 -8
View File
@@ -2,7 +2,9 @@ import OpenAI from 'openai';
import type { JSRuntimeFS } from './JSRuntime';
import { DotAI } from './DotAI';
import { parseProviderModel } from './parseProviderModel';
import { createAIClient } from './ai-client';
import type { Tool } from './tools/Tool';
import type { NUser } from '@nostrify/react/login';
export type AIMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam;
@@ -44,17 +46,20 @@ 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 }> };
private aiSettings: { providers: Record<string, { baseURL: string; apiKey?: string; nostr?: boolean }> };
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 }> },
getProviderModels?: () => Array<{ id: string; provider: string; contextLength?: number; pricing?: { prompt: import('decimal.js').Decimal; completion: import('decimal.js').Decimal } }>
aiSettings: { providers: Record<string, { baseURL: string; apiKey?: string; nostr?: boolean }> },
getProviderModels?: () => Array<{ id: string; provider: string; contextLength?: number; pricing?: { prompt: import('decimal.js').Decimal; completion: import('decimal.js').Decimal } }>,
getCurrentUser?: () => NUser | undefined
) {
this.fs = fs;
this.aiSettings = aiSettings;
this.getProviderModels = getProviderModels;
this.getCurrentUser = getCurrentUser;
}
/**
@@ -195,11 +200,8 @@ export class SessionManager {
const modelName = parsed.model;
// Initialize OpenAI client
const openai = new OpenAI({
baseURL: connectionConfig.baseURL,
apiKey: connectionConfig.apiKey,
dangerouslyAllowBrowser: true
});
const currentUser = this.getCurrentUser?.();
const openai = createAIClient(connectionConfig, currentUser);
let stepCount = 0;
const maxSteps = session.maxSteps || 50;
+30
View File
@@ -0,0 +1,30 @@
import OpenAI from 'openai';
import { NIP98Client } from './NIP98Client';
import type { AIConnection } from '@/contexts/AISettingsContext';
import type { NUser } from '@nostrify/react/login';
/**
* Create an OpenAI client instance with the appropriate configuration.
* If the connection requires Nostr authentication (NIP-98), it will use
* the NIP98Client for authenticated requests.
*/
export function createAIClient(connection: AIConnection, user?: NUser): OpenAI {
const baseConfig: ConstructorParameters<typeof OpenAI>[0] = {
baseURL: connection.baseURL,
apiKey: connection.apiKey ?? '',
dangerouslyAllowBrowser: true,
};
// If Nostr authentication is required and we have a user, use NIP-98
if (connection.nostr && user?.signer) {
const nip98Client = new NIP98Client(user.signer);
return new OpenAI({
...baseConfig,
fetch: (input, init) => nip98Client.fetch(input, init),
});
}
// Standard OpenAI client
return new OpenAI(baseConfig);
}
+4 -8
View File
@@ -24,7 +24,7 @@ describe('parseProviderModel', () => {
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);
@@ -32,7 +32,7 @@ describe('parseProviderModel', () => {
it('should parse complex model names with slashes', () => {
const result = parseProviderModel('openrouter/anthropic/claude-sonnet-4', mockProviders);
expect(result.provider).toBe('openrouter');
expect(result.model).toBe('anthropic/claude-sonnet-4');
expect(result.connection).toEqual(mockProviders.openrouter);
@@ -40,7 +40,7 @@ describe('parseProviderModel', () => {
it('should handle model names with multiple slashes', () => {
const result = parseProviderModel('openrouter/meta/llama-3.1-405b-instruct', mockProviders);
expect(result.provider).toBe('openrouter');
expect(result.model).toBe('meta/llama-3.1-405b-instruct');
expect(result.connection).toEqual(mockProviders.openrouter);
@@ -70,9 +70,5 @@ describe('parseProviderModel', () => {
}).toThrow('Provider "unknown" not found. Available providers: openai, openrouter, anthropic, unconfigured');
});
it('should throw error for provider without API key', () => {
expect(() => {
parseProviderModel('unconfigured/some-model', mockProviders);
}).toThrow('Provider "unconfigured" is not configured with an API key');
});
});
-4
View File
@@ -33,10 +33,6 @@ export function parseProviderModel(
throw new Error(`Provider "${provider}" not found. Available providers: ${Object.keys(providers).join(', ')}`);
}
if (!connection.apiKey) {
throw new Error(`Provider "${provider}" is not configured with an API key`);
}
return {
provider,
model,