Skip to content

Commit 73d7e60

Browse files
authored
Merge pull request #4 from NillionNetwork/feat/phase_1_release
feat: phase 1 release
2 parents e589dd9 + dd48c96 commit 73d7e60

18 files changed

+123
-77
lines changed

biome.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@
6565
}
6666
}
6767
}
68+
},
69+
{
70+
"includes": ["**/src/client.ts", "**/src/internal/debug_client.ts"],
71+
"linter": {
72+
"rules": {
73+
"suspicious": {
74+
"noExplicitAny": "off"
75+
}
76+
}
77+
}
6878
}
6979
]
7080
}

examples/0-api-key.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import "dotenv/config";
2-
import { NilaiOpenAIClient, NilAuthInstance } from "@nillion/nilai-ts";
2+
import { NilAuthInstance, NilaiOpenAIClient } from "@nillion/nilai-ts";
33

44
// To obtain an API key, navigate to https://nilpay.vercel.app/
55
// and create a new subscription.

examples/1-delegation-token.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import "dotenv/config";
22
import {
3-
NilaiOpenAIClient,
4-
DelegationTokenServer,
53
AuthType,
64
type DelegationTokenRequest,
75
type DelegationTokenResponse,
6+
DelegationTokenServer,
87
NilAuthInstance,
8+
NilaiOpenAIClient,
99
} from "@nillion/nilai-ts";
1010

1111
// To obtain an API key, navigate to https://nilpay.vercel.app/

examples/2-nildb-prompt-store-retrieve.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import "dotenv/config";
22
import {
3-
NilaiOpenAIClient,
4-
DelegationTokenServer,
53
AuthType,
64
type DelegationTokenRequest,
75
type DelegationTokenResponse,
6+
DelegationTokenServer,
87
NilAuthInstance,
8+
NilaiOpenAIClient,
99
} from "@nillion/nilai-ts";
1010
import { Did as DidClass } from "@nillion/nuc";
1111

@@ -26,7 +26,11 @@ async function store_to_nildb(prompt: string): Promise<[string, string]> {
2626

2727
const createdIds: string[] = await client.createPrompt(prompt);
2828
console.log(`Created IDS on nilDB: ${createdIds}`);
29-
const ownerDid = new DidClass(client.getKeypair()!.publicKey()).toString();
29+
const keypair = client.getKeypair();
30+
if (!keypair) {
31+
throw new Error("Keypair not available");
32+
}
33+
const ownerDid = new DidClass(keypair.publicKey()).toString();
3034
return [createdIds[0], ownerDid];
3135
}
3236

examples/3-web-search.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import "dotenv/config";
2-
import { NilaiOpenAIClient, NilAuthInstance } from "@nillion/nilai-ts";
2+
import { NilAuthInstance, NilaiOpenAIClient } from "@nillion/nilai-ts";
33

44
// To obtain an API key, navigate to https://nilpay.vercel.app/
55
// and create a new subscription.
@@ -32,7 +32,7 @@ async function main() {
3232
},
3333
{
3434
extra_body: { web_search: true }, // Enable web search
35-
} as any,
35+
} as Record<string, unknown>,
3636
);
3737

3838
console.log(`Response: ${response.choices[0].message.content}`);

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
{
22
"name": "@nillion/nilai-ts",
3-
"version": "0.2.0-alpha.0",
3+
"version": "0.2.0-alpha.1",
44
"description": "Nilai Typescript SDK",
55
"type": "module",
6-
"main": "dist/index.js",
7-
"types": "dist/index.d.ts",
6+
"main": "./dist/index.cjs",
7+
"module": "./dist/index.js",
8+
"types": "./dist/index.d.ts",
89
"exports": {
910
".": {
1011
"types": "./dist/index.d.ts",
11-
"import": "./dist/index.js"
12+
"import": "./dist/index.js",
13+
"require": "./dist/index.cjs"
1214
},
1315
"./polyfills": {
1416
"types": "./dist/polyfills.d.ts",
15-
"import": "./dist/polyfills.js"
17+
"import": "./dist/polyfills.js",
18+
"require": "./dist/polyfills.cjs"
1619
},
1720
"./package.json": "./package.json"
1821
},

src/client.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
1-
import { OpenAI, type ClientOptions } from "openai";
21
import { randomUUID } from "node:crypto";
32
import {
4-
type NucTokenEnvelope,
5-
NucTokenBuilder,
6-
NilauthClient,
7-
PayerBuilder,
8-
Keypair,
93
Did as DidClass,
104
InvocationBody,
5+
Keypair,
6+
NilauthClient,
7+
NucTokenBuilder,
8+
type NucTokenEnvelope,
119
NucTokenEnvelopeSchema,
10+
PayerBuilder,
1211
} 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";
1420
import {
1521
AuthType,
16-
NilAuthInstance,
22+
DefaultNilDBConfig,
1723
type DelegationTokenRequest,
1824
type DelegationTokenResponse,
19-
type NilaiClientOptions,
25+
NilAuthInstance,
2026
type NilAuthPublicKey,
21-
RequestType,
22-
DefaultNilDBConfig,
27+
type NilaiClientOptions,
2328
type NilDBDelegation,
29+
RequestType,
2430
} from "./types";
25-
2631
import { isExpired } from "./utils";
27-
import {
28-
type AclDto,
29-
type CreateOwnedDataRequest,
30-
type ListDataReferencesResponse,
31-
SecretVaultUserClient,
32-
Did,
33-
} from "@nillion/secretvaults";
3432

3533
export interface NilaiOpenAIClientOptions
3634
extends NilaiClientOptions,
@@ -90,7 +88,10 @@ export class NilaiOpenAIClient extends OpenAI {
9088
private _initializeAuth(api_key?: string): void {
9189
switch (this.authType) {
9290
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);
9495
break;
9596
case AuthType.DELEGATION_TOKEN:
9697
this._delegationTokenInit();
@@ -266,7 +267,7 @@ export class NilaiOpenAIClient extends OpenAI {
266267
};
267268
}
268269

269-
async listPrompts(keypair: Keypair) {
270+
async listPrompts(keypair: Keypair): Promise<ListDataReferencesResponse> {
270271
const client = await SecretVaultUserClient.from({
271272
keypair: keypair,
272273
baseUrls: DefaultNilDBConfig.baseUrls,
@@ -312,8 +313,11 @@ export class NilaiOpenAIClient extends OpenAI {
312313
}
313314

314315
async createPrompt(prompt: string): Promise<string[]> {
316+
if (!this.nilAuthPrivateKey) {
317+
throw new Error("NilAuthPrivateKey not set. Call _initializeAuth first.");
318+
}
315319
const client = await SecretVaultUserClient.from({
316-
keypair: Keypair.from(this.nilAuthPrivateKey!.privateKey()),
320+
keypair: Keypair.from(this.nilAuthPrivateKey.privateKey()),
317321
baseUrls: DefaultNilDBConfig.baseUrls,
318322
blindfold: { operation: "store" },
319323
});

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ export { NilaiOpenAIClient } from "./client";
55
export { DelegationTokenServer } from "./server";
66
export {
77
AuthType,
8-
NilAuthInstance,
9-
RequestType,
8+
DefaultDelegationTokenServerConfig,
9+
type DelegationServerConfig,
1010
type DelegationTokenRequest,
1111
type DelegationTokenResponse,
12-
type DelegationServerConfig,
13-
type NilaiClientOptions,
1412
type InvocationArgs,
15-
DefaultDelegationTokenServerConfig,
13+
NilAuthInstance,
14+
type NilaiClientOptions,
15+
RequestType,
1616
} from "./types";
17-
export { isExpired, hexToBytes, bytesToHex } from "./utils";
17+
export { bytesToHex, hexToBytes, isExpired } from "./utils";

src/internal/debug_client.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import OpenAI from "openai"; // [oai_citation:0‡github.com](https://github.com/openai/openai-node?utm_source=chatgpt.com)
22

33
export class DebugOpenAI extends OpenAI {
4-
constructor(opts?: ConstructorParameters<typeof OpenAI>[0]) {
5-
super(opts);
6-
}
7-
84
/**
95
* Used as a callback for mutating the given `RequestInit` object.
106
*

src/polyfills.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function ensureGlobals() {
7575
if (typeof globalThis.crypto === "undefined") {
7676
try {
7777
if (typeof require !== "undefined") {
78-
const crypto = require("crypto");
78+
const crypto = require("node:crypto");
7979
// Use webcrypto if available (Node.js 16+), otherwise use crypto
8080
globalThis.crypto = crypto.webcrypto || crypto;
8181
}
@@ -91,7 +91,7 @@ function ensureGlobals() {
9191
if (typeof globalThis.TextEncoder === "undefined") {
9292
try {
9393
if (typeof require !== "undefined") {
94-
const util = require("util");
94+
const util = require("node:util");
9595
globalThis.TextEncoder = util.TextEncoder;
9696
globalThis.TextDecoder = util.TextDecoder;
9797
}

0 commit comments

Comments
 (0)