Files
shakespeare/src/lib/ai-client.ts
T
2025-09-11 17:53:36 -05:00

30 lines
1004 B
TypeScript

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);
}