|
1 | | -import { OpenAI, type ClientOptions } from "openai"; |
2 | 1 | import { randomUUID } from "node:crypto"; |
3 | 2 | import { |
4 | | - type NucTokenEnvelope, |
5 | | - NucTokenBuilder, |
6 | | - NilauthClient, |
7 | | - PayerBuilder, |
8 | | - Keypair, |
9 | 3 | Did as DidClass, |
10 | 4 | InvocationBody, |
| 5 | + Keypair, |
| 6 | + NilauthClient, |
| 7 | + NucTokenBuilder, |
| 8 | + type NucTokenEnvelope, |
11 | 9 | NucTokenEnvelopeSchema, |
| 10 | + PayerBuilder, |
12 | 11 | } from "@nillion/nuc"; |
13 | | - |
| 12 | +import { |
| 13 | + type AclDto, |
| 14 | + type CreateOwnedDataRequest, |
| 15 | + Did, |
| 16 | + type ListDataReferencesResponse, |
| 17 | + SecretVaultUserClient, |
| 18 | +} from "@nillion/secretvaults"; |
| 19 | +import { type ClientOptions, OpenAI } from "openai"; |
14 | 20 | import { |
15 | 21 | AuthType, |
16 | | - NilAuthInstance, |
| 22 | + DefaultNilDBConfig, |
17 | 23 | type DelegationTokenRequest, |
18 | 24 | type DelegationTokenResponse, |
19 | | - type NilaiClientOptions, |
| 25 | + NilAuthInstance, |
20 | 26 | type NilAuthPublicKey, |
21 | | - RequestType, |
22 | | - DefaultNilDBConfig, |
| 27 | + type NilaiClientOptions, |
23 | 28 | type NilDBDelegation, |
| 29 | + RequestType, |
24 | 30 | } from "./types"; |
25 | | - |
26 | 31 | import { isExpired } from "./utils"; |
27 | | -import { |
28 | | - type AclDto, |
29 | | - type CreateOwnedDataRequest, |
30 | | - type ListDataReferencesResponse, |
31 | | - SecretVaultUserClient, |
32 | | - Did, |
33 | | -} from "@nillion/secretvaults"; |
34 | 32 |
|
35 | 33 | export interface NilaiOpenAIClientOptions |
36 | 34 | extends NilaiClientOptions, |
@@ -90,7 +88,10 @@ export class NilaiOpenAIClient extends OpenAI { |
90 | 88 | private _initializeAuth(api_key?: string): void { |
91 | 89 | switch (this.authType) { |
92 | 90 | case AuthType.API_KEY: |
93 | | - this._apiKeyInit(api_key!); |
| 91 | + if (!api_key) { |
| 92 | + throw new Error("API key is required for API_KEY auth type"); |
| 93 | + } |
| 94 | + this._apiKeyInit(api_key); |
94 | 95 | break; |
95 | 96 | case AuthType.DELEGATION_TOKEN: |
96 | 97 | this._delegationTokenInit(); |
@@ -266,7 +267,7 @@ export class NilaiOpenAIClient extends OpenAI { |
266 | 267 | }; |
267 | 268 | } |
268 | 269 |
|
269 | | - async listPrompts(keypair: Keypair) { |
| 270 | + async listPrompts(keypair: Keypair): Promise<ListDataReferencesResponse> { |
270 | 271 | const client = await SecretVaultUserClient.from({ |
271 | 272 | keypair: keypair, |
272 | 273 | baseUrls: DefaultNilDBConfig.baseUrls, |
@@ -312,8 +313,11 @@ export class NilaiOpenAIClient extends OpenAI { |
312 | 313 | } |
313 | 314 |
|
314 | 315 | async createPrompt(prompt: string): Promise<string[]> { |
| 316 | + if (!this.nilAuthPrivateKey) { |
| 317 | + throw new Error("NilAuthPrivateKey not set. Call _initializeAuth first."); |
| 318 | + } |
315 | 319 | const client = await SecretVaultUserClient.from({ |
316 | | - keypair: Keypair.from(this.nilAuthPrivateKey!.privateKey()), |
| 320 | + keypair: Keypair.from(this.nilAuthPrivateKey.privateKey()), |
317 | 321 | baseUrls: DefaultNilDBConfig.baseUrls, |
318 | 322 | blindfold: { operation: "store" }, |
319 | 323 | }); |
|
0 commit comments