mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
30 lines
1004 B
TypeScript
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);
|
|
} |