Skip to content

Commit e589dd9

Browse files
committed
chore: updated linting, formatting and version bump
1 parent d0c9b23 commit e589dd9

File tree

11 files changed

+134
-108
lines changed

11 files changed

+134
-108
lines changed

examples/0-api-key.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async function main() {
2323
const response = await client.chat.completions.create({
2424
model: "google/gemma-3-27b-it",
2525
messages: [
26-
{ role: "user", content: "Hello! Can you help me with something?" }
26+
{ role: "user", content: "Hello! Can you help me with something?" },
2727
],
2828
});
2929

examples/1-delegation-token.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@ async function main() {
2222
// >>> Server initializes a delegation token server
2323
// The server is responsible for creating delegation tokens
2424
// and managing their expiration and usage.
25-
const server = new DelegationTokenServer(
26-
API_KEY,
27-
{
28-
nilauthInstance: NilAuthInstance.SANDBOX,
29-
expirationTime: 10, // 10 seconds validity of delegation tokens
30-
tokenMaxUses: 1, // 1 use of a delegation token
31-
},
32-
);
25+
const server = new DelegationTokenServer(API_KEY, {
26+
nilauthInstance: NilAuthInstance.SANDBOX,
27+
expirationTime: 10, // 10 seconds validity of delegation tokens
28+
tokenMaxUses: 1, // 1 use of a delegation token
29+
});
3330

3431
// >>> Client initializes a client
3532
// The client is responsible for making requests to the Nilai API.
@@ -42,12 +39,12 @@ async function main() {
4239
});
4340

4441
// >>> Client produces a delegation request
45-
const delegationRequest: DelegationTokenRequest = client.getDelegationRequest();
42+
const delegationRequest: DelegationTokenRequest =
43+
client.getDelegationRequest();
4644

4745
// <<< Server creates a delegation token
48-
const delegationToken: DelegationTokenResponse = await server.createDelegationToken(
49-
delegationRequest
50-
);
46+
const delegationToken: DelegationTokenResponse =
47+
await server.createDelegationToken(delegationRequest);
5148

5249
// >>> Client sets internally the delegation token
5350
client.updateDelegation(delegationToken);
@@ -56,7 +53,7 @@ async function main() {
5653
const response = await client.chat.completions.create({
5754
model: "google/gemma-3-27b-it",
5855
messages: [
59-
{ role: "user", content: "Hello! Can you help me with something?" }
56+
{ role: "user", content: "Hello! Can you help me with something?" },
6057
],
6158
});
6259

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

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
type DelegationTokenResponse,
88
NilAuthInstance,
99
} from "@nillion/nilai-ts";
10-
import { Keypair, Did as DidClass } from "@nillion/nuc";
10+
import { Did as DidClass } from "@nillion/nuc";
1111

1212
// To obtain an API key, navigate to https://nilpay.vercel.app/
1313
// and create a new subscription.
@@ -34,23 +34,22 @@ async function main() {
3434
if (!API_KEY) {
3535
throw new Error("NILLION_API_KEY environment variable is required");
3636
}
37-
// First, we store the prompt in nilDB
38-
const [doc_id, owner_did] = await store_to_nildb("You are a very clever model that answers with cheese answers and always starting with the word cheese");
37+
// First, we store the prompt in nilDB
38+
const [doc_id, owner_did] = await store_to_nildb(
39+
"You are a very clever model that answers with cheese answers and always starting with the word cheese",
40+
);
3941
// >>> Server initializes a delegation token server
4042
// The server is responsible for creating delegation tokens
4143
// and managing their expiration and usage.
42-
const server = new DelegationTokenServer(
43-
API_KEY,
44-
{
45-
nilauthInstance: NilAuthInstance.PRODUCTION,
46-
expirationTime: 60 * 60, // 3600 seconds = 1 hour validity of delegation tokens
47-
tokenMaxUses: 10, // 10 uses of a delegation token
48-
prompt_document: {
49-
owner_did: owner_did, // Replace with your DID
50-
doc_id: doc_id // Replace with your document ID
51-
}
44+
const server = new DelegationTokenServer(API_KEY, {
45+
nilauthInstance: NilAuthInstance.PRODUCTION,
46+
expirationTime: 60 * 60, // 3600 seconds = 1 hour validity of delegation tokens
47+
tokenMaxUses: 10, // 10 uses of a delegation token
48+
prompt_document: {
49+
owner_did: owner_did, // Replace with your DID
50+
doc_id: doc_id, // Replace with your document ID
5251
},
53-
);
52+
});
5453

5554
// >>> Client initializes a client
5655
// The client is responsible for making requests to the Nilai API.
@@ -64,17 +63,20 @@ async function main() {
6463
console.log("Requesting delegation token from NilAI Server");
6564

6665
// >>> Client produces a delegation request
67-
const delegationRequest: DelegationTokenRequest = client.getDelegationRequest();
68-
console.log("Client DID for delegation:", `did:nil:${delegationRequest.public_key}`);
66+
const delegationRequest: DelegationTokenRequest =
67+
client.getDelegationRequest();
68+
console.log(
69+
"Client DID for delegation:",
70+
`did:nil:${delegationRequest.public_key}`,
71+
);
6972

7073
console.log(`Delegation Request: ${JSON.stringify(delegationRequest)}`);
7174

7275
// <<< Server creates a delegation token
73-
const delegationToken: DelegationTokenResponse = await server.createDelegationToken(
74-
delegationRequest
75-
);
76+
const delegationToken: DelegationTokenResponse =
77+
await server.createDelegationToken(delegationRequest);
7678

77-
console.log(`Delegation Token: ${JSON.stringify(delegationToken)}`);
79+
console.log(`Delegation Token: ${JSON.stringify(delegationToken)}`);
7880

7981
// >>> Client sets internally the delegation token
8082
client.updateDelegation(delegationToken);
@@ -84,7 +86,7 @@ async function main() {
8486
const response = await client.chat.completions.create({
8587
model: "openai/gpt-oss-20b",
8688
messages: [
87-
{ role: "user", content: "Hello! Can you help me with something?" }
89+
{ role: "user", content: "Hello! Can you help me with something?" },
8890
],
8991
});
9092

examples/3-web-search.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@ async function main() {
2020
});
2121

2222
// Make a request to the Nilai API
23-
const response = await client.chat.completions.create({
24-
model: "google/gemma-3-27b-it",
25-
messages: [
26-
{ role: "user", content: "Hello! Can you help me understand the latest news of AI?" }
27-
]
28-
}, {
29-
extra_body: { web_search: true } // Enable web search
30-
} as any);
23+
const response = await client.chat.completions.create(
24+
{
25+
model: "google/gemma-3-27b-it",
26+
messages: [
27+
{
28+
role: "user",
29+
content: "Hello! Can you help me understand the latest news of AI?",
30+
},
31+
],
32+
},
33+
{
34+
extra_body: { web_search: true }, // Enable web search
35+
} as any,
36+
);
3137

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

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nillion/nilai-ts",
3-
"version": "0.0.0-alpha.4",
3+
"version": "0.2.0-alpha.0",
44
"description": "Nilai Typescript SDK",
55
"type": "module",
66
"main": "dist/index.js",
@@ -30,7 +30,6 @@
3030
"prepublishOnly": "pnpm run build",
3131
"example:api-key": "tsx examples/0-api-key.ts",
3232
"example:delegation-token": "tsx examples/1-delegation-token.ts",
33-
"example:nildb-storage": "tsx examples/2-nildb-storage.ts",
3433
"example:nildb-prompt-retrieval": "tsx examples/2-nildb-prompt-retrieval.ts",
3534
"example:web-search": "tsx examples/3-web-search.ts"
3635
},

src/client.ts

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { OpenAI, type ClientOptions } from "openai";
2-
import { randomUUID } from 'node:crypto';
2+
import { randomUUID } from "node:crypto";
33
import {
44
type NucTokenEnvelope,
55
NucTokenBuilder,
@@ -20,12 +20,17 @@ import {
2020
type NilAuthPublicKey,
2121
RequestType,
2222
DefaultNilDBConfig,
23-
NilDBDelegation,
23+
type NilDBDelegation,
2424
} from "./types";
2525

2626
import { isExpired } from "./utils";
27-
import { AclDto, CreateOwnedDataRequest, ListDataReferencesResponse, SecretVaultUserClient, Did } from "@nillion/secretvaults";
28-
import { z } from "zod/v4";
27+
import {
28+
type AclDto,
29+
type CreateOwnedDataRequest,
30+
type ListDataReferencesResponse,
31+
SecretVaultUserClient,
32+
Did,
33+
} from "@nillion/secretvaults";
2934

3035
export interface NilaiOpenAIClientOptions
3136
extends NilaiClientOptions,
@@ -265,11 +270,12 @@ export class NilaiOpenAIClient extends OpenAI {
265270
const client = await SecretVaultUserClient.from({
266271
keypair: keypair,
267272
baseUrls: DefaultNilDBConfig.baseUrls,
268-
})
273+
});
269274

270-
const response: ListDataReferencesResponse = await client.listDataReferences()
275+
const response: ListDataReferencesResponse =
276+
await client.listDataReferences();
271277

272-
return response
278+
return response;
273279
}
274280
async requestNildbDelegationToken(): Promise<NilDBDelegation> {
275281
if (!this.nilAuthPrivateKey) {
@@ -281,19 +287,21 @@ export class NilaiOpenAIClient extends OpenAI {
281287

282288
try {
283289
const url = new URL(`${this.baseURL}delegation`);
284-
url.searchParams.append('prompt_delegation_request', userDid);
285-
290+
url.searchParams.append("prompt_delegation_request", userDid);
291+
286292
const response = await fetch(url.toString(), {
287-
method: 'GET',
293+
method: "GET",
288294
headers: {
289-
'Authorization': `Bearer ${authToken}`,
290-
'Content-Type': 'application/json',
295+
Authorization: `Bearer ${authToken}`,
296+
"Content-Type": "application/json",
291297
},
292298
});
293299

294300
if (!response.ok) {
295301
const errorText = await response.text();
296-
throw new Error(`Failed to retrieve the delegation token: ${errorText}`);
302+
throw new Error(
303+
`Failed to retrieve the delegation token: ${errorText}`,
304+
);
297305
}
298306

299307
const jsonResponse = await response.json();
@@ -307,8 +315,8 @@ export class NilaiOpenAIClient extends OpenAI {
307315
const client = await SecretVaultUserClient.from({
308316
keypair: Keypair.from(this.nilAuthPrivateKey!.privateKey()),
309317
baseUrls: DefaultNilDBConfig.baseUrls,
310-
blindfold: { operation: 'store' },
311-
})
318+
blindfold: { operation: "store" },
319+
});
312320
// Get delegation from nilAI for the user
313321

314322
const delegation = await this.requestNildbDelegationToken();
@@ -336,8 +344,8 @@ export class NilaiOpenAIClient extends OpenAI {
336344
{
337345
_id: randomUUID(),
338346
prompt: {
339-
key: 'prompt',
340-
'%allot': prompt, // encrypted field
347+
key: "prompt",
348+
"%allot": prompt, // encrypted field
341349
},
342350
},
343351
];
@@ -350,27 +358,33 @@ export class NilaiOpenAIClient extends OpenAI {
350358
};
351359

352360
const createResponse = await client.createData(
353-
delegation.token, createDataRequest
354-
)
361+
delegation.token,
362+
createDataRequest,
363+
);
355364

356365
const createdIds = [];
357-
for (const [nodeId, nodeResponse] of Object.entries(createResponse)) {
366+
for (const [_nodeId, nodeResponse] of Object.entries(createResponse)) {
358367
if (nodeResponse.data?.created) {
359368
createdIds.push(...nodeResponse.data.created);
360369
}
361370
}
362-
console.log("CreateData response:", JSON.stringify(createResponse, null, 2));
363-
371+
console.log(
372+
"CreateData response:",
373+
JSON.stringify(createResponse, null, 2),
374+
);
375+
364376
// If there are errors, log the detailed error bodies
365377
if (Array.isArray(createResponse)) {
366378
createResponse.forEach((item, index) => {
367379
if (item.error) {
368-
console.log(`Error ${index + 1} details:`, JSON.stringify(item.error.body, null, 2));
380+
console.log(
381+
`Error ${index + 1} details:`,
382+
JSON.stringify(item.error.body, null, 2),
383+
);
369384
}
370385
});
371386
}
372-
373-
return createdIds
374-
}
375387

376-
}
388+
return createdIds;
389+
}
390+
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Import polyfills first to ensure they're available when other modules load
2-
import './polyfills';
2+
import "./polyfills";
33

44
export { NilaiOpenAIClient } from "./client";
5-
export { DelegationTokenServer } from './server';
5+
export { DelegationTokenServer } from "./server";
66
export {
77
AuthType,
88
NilAuthInstance,

0 commit comments

Comments
 (0)