tinfoil 0.8.3 → 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/dist/ai-sdk-provider.d.ts +1 -11
  2. package/dist/ai-sdk-provider.js +9 -151
  3. package/dist/client.browser.d.ts +3 -2
  4. package/dist/client.browser.js +13 -28
  5. package/dist/client.d.ts +4 -3
  6. package/dist/client.js +13 -153
  7. package/dist/config.d.ts +4 -3
  8. package/dist/config.js +4 -3
  9. package/dist/encrypted-body-fetch.d.ts +2 -2
  10. package/dist/encrypted-body-fetch.js +4 -4
  11. package/dist/esm/ai-sdk-provider.d.ts +1 -11
  12. package/dist/esm/ai-sdk-provider.js +9 -148
  13. package/dist/esm/client.browser.d.ts +3 -2
  14. package/dist/esm/client.browser.js +13 -28
  15. package/dist/esm/client.d.ts +4 -3
  16. package/dist/esm/client.js +13 -153
  17. package/dist/esm/config.d.ts +4 -3
  18. package/dist/esm/config.js +4 -3
  19. package/dist/esm/encrypted-body-fetch.d.ts +2 -2
  20. package/dist/esm/encrypted-body-fetch.js +4 -4
  21. package/dist/esm/index.browser.d.ts +2 -1
  22. package/dist/esm/index.browser.js +2 -1
  23. package/dist/esm/index.d.ts +1 -0
  24. package/dist/esm/index.js +1 -0
  25. package/dist/esm/pinned-tls-fetch.d.ts +1 -0
  26. package/dist/esm/pinned-tls-fetch.js +110 -0
  27. package/dist/esm/secure-client.browser.d.ts +20 -0
  28. package/dist/esm/secure-client.browser.js +57 -0
  29. package/dist/esm/secure-client.d.ts +20 -0
  30. package/dist/esm/secure-client.js +69 -0
  31. package/dist/esm/verifier.js +2 -2
  32. package/dist/index.browser.d.ts +2 -1
  33. package/dist/index.browser.js +4 -2
  34. package/dist/index.d.ts +1 -0
  35. package/dist/index.js +1 -0
  36. package/dist/pinned-tls-fetch.d.ts +1 -0
  37. package/dist/pinned-tls-fetch.js +116 -0
  38. package/dist/secure-client.browser.d.ts +20 -0
  39. package/dist/secure-client.browser.js +61 -0
  40. package/dist/secure-client.d.ts +20 -0
  41. package/dist/secure-client.js +73 -0
  42. package/dist/verifier.js +2 -2
  43. package/package.json +8 -3
  44. package/dist/ai-sdk-provider.browser.d.ts +0 -7
  45. package/dist/ai-sdk-provider.browser.js +0 -30
  46. package/dist/esm/ai-sdk-provider.browser.d.ts +0 -7
  47. package/dist/esm/ai-sdk-provider.browser.js +0 -27
@@ -1,17 +1,7 @@
1
1
  interface CreateTinfoilAIOptions {
2
- /** Override the inference API base URL */
3
2
  baseURL?: string;
4
- /** Override the URL used to fetch the HPKE (defaults to baseURL) */
5
- hpkeKeyURL?: string;
6
- /** Override the config GitHub repository */
3
+ enclaveURL?: string;
7
4
  configRepo?: string;
8
5
  }
9
- /**
10
- * Creates an AI SDK provider with the specified API key.
11
- *
12
- * @param apiKey - The API key for the Tinfoil API
13
- * @param options - Optional configuration options
14
- * @returns A TinfoilAI instance
15
- */
16
6
  export declare function createTinfoilAI(apiKey: string, options?: CreateTinfoilAIOptions): Promise<import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<string, string, string, string>>;
17
7
  export {};
@@ -1,165 +1,23 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.createTinfoilAI = createTinfoilAI;
7
4
  const openai_compatible_1 = require("@ai-sdk/openai-compatible");
8
5
  const config_1 = require("./config");
9
- const encrypted_body_fetch_1 = require("./encrypted-body-fetch");
10
- const verifier_1 = require("./verifier");
11
- const env_1 = require("./env");
12
- const https_1 = __importDefault(require("https"));
13
- const tls_1 = require("tls");
14
- const crypto_1 = require("crypto");
15
- const stream_1 = require("stream");
16
- /**
17
- * Creates an AI SDK provider with the specified API key.
18
- *
19
- * @param apiKey - The API key for the Tinfoil API
20
- * @param options - Optional configuration options
21
- * @returns A TinfoilAI instance
22
- */
6
+ const secure_client_1 = require("tinfoil/secure-client");
23
7
  async function createTinfoilAI(apiKey, options = {}) {
24
8
  const baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
25
- const hpkeKeyURL = options.hpkeKeyURL || config_1.TINFOIL_CONFIG.HPKE_KEY_URL;
9
+ const enclaveURL = options.enclaveURL || config_1.TINFOIL_CONFIG.ENCLAVE_URL;
26
10
  const configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
27
- // step 1: verify the enclave and extract the public keys
28
- // from the attestation response
29
- const verifier = new verifier_1.Verifier({ serverURL: baseURL, configRepo });
30
- const attestationResponse = await verifier.verify();
31
- const hpkePublicKey = attestationResponse.hpkePublicKey;
32
- const tlsPublicKeyFingerprint = attestationResponse.tlsPublicKeyFingerprint;
33
- // step 2: create the appropriate fetch function based on available keys
34
- let fetchFunction;
35
- if (hpkePublicKey) {
36
- // HPKE available: use encrypted body fetch
37
- fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(baseURL, hpkePublicKey, hpkeKeyURL);
38
- }
39
- else {
40
- // HPKE not available: check if we're in a browser
41
- if ((0, env_1.isRealBrowser)()) {
42
- throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
43
- "Only HPKE-enabled enclaves can be used in browser environments.");
44
- }
45
- // Node.js environment: fall back to TLS-only verification using pinned TLS fetch
46
- if (!tlsPublicKeyFingerprint) {
47
- throw new Error("Neither HPKE public key nor TLS public key fingerprint available for verification");
48
- }
49
- fetchFunction = createPinnedTlsFetch(tlsPublicKeyFingerprint);
50
- }
51
- // step 3: create the openai compatible provider
52
- // that uses the appropriate fetch function
11
+ const secureClient = new secure_client_1.SecureClient({
12
+ baseURL,
13
+ enclaveURL,
14
+ configRepo,
15
+ });
16
+ await secureClient.ready();
53
17
  return (0, openai_compatible_1.createOpenAICompatible)({
54
18
  name: "tinfoil",
55
19
  baseURL: baseURL.replace(/\/$/, ""),
56
20
  apiKey: apiKey,
57
- fetch: fetchFunction,
58
- });
59
- }
60
- function createPinnedTlsFetch(expectedFingerprintHex) {
61
- return (async (input, init) => {
62
- // Normalize URL
63
- const makeURL = (value) => {
64
- if (typeof value === "string")
65
- return new URL(value);
66
- if (value instanceof URL)
67
- return value;
68
- return new URL(value.url);
69
- };
70
- const url = makeURL(input);
71
- if (url.protocol !== "https:") {
72
- throw new Error(`HTTP connections are not allowed. Use HTTPS. URL: ${url.toString()}`);
73
- }
74
- // Gather method and headers
75
- const method = (init?.method || input.method || "GET").toUpperCase();
76
- const headers = new Headers(init?.headers || input?.headers || {});
77
- const headerObj = {};
78
- headers.forEach((v, k) => {
79
- headerObj[k] = v;
80
- });
81
- // Resolve body
82
- let body = init?.body;
83
- if (!body && input instanceof Request) {
84
- // If the original was a Request with a body, read it
85
- try {
86
- const buf = await input.arrayBuffer();
87
- if (buf && buf.byteLength)
88
- body = Buffer.from(buf);
89
- }
90
- catch { }
91
- }
92
- // Convert web streams to Node streams if needed
93
- if (body && typeof body.getReader === "function") {
94
- body = stream_1.Readable.fromWeb(body);
95
- }
96
- if (body instanceof ArrayBuffer) {
97
- body = Buffer.from(body);
98
- }
99
- if (ArrayBuffer.isView(body)) {
100
- body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
101
- }
102
- const requestOptions = {
103
- protocol: url.protocol,
104
- hostname: url.hostname,
105
- port: url.port ? Number(url.port) : 443,
106
- path: `${url.pathname}${url.search}`,
107
- method,
108
- headers: headerObj,
109
- checkServerIdentity: (host, cert) => {
110
- const raw = cert.raw;
111
- if (!raw) {
112
- return new Error("Certificate raw bytes are unavailable for pinning");
113
- }
114
- const x509 = new crypto_1.X509Certificate(raw);
115
- const publicKeyDer = x509.publicKey.export({ type: "spki", format: "der" });
116
- const fp = (0, crypto_1.createHash)("sha256").update(publicKeyDer).digest("hex");
117
- if (fp !== expectedFingerprintHex) {
118
- return new Error(`Certificate public key fingerprint mismatch. Expected: ${expectedFingerprintHex}, Got: ${fp}`);
119
- }
120
- return (0, tls_1.checkServerIdentity)(host, cert);
121
- },
122
- };
123
- const { signal } = init || {};
124
- const res = await new Promise((resolve, reject) => {
125
- const req = https_1.default.request(requestOptions, resolve);
126
- req.on("error", reject);
127
- if (signal) {
128
- if (signal.aborted) {
129
- req.destroy(new Error("Request aborted"));
130
- return;
131
- }
132
- signal.addEventListener("abort", () => req.destroy(new Error("Request aborted")));
133
- }
134
- if (body === undefined || body === null) {
135
- req.end();
136
- }
137
- else if (typeof body === "string" || Buffer.isBuffer(body) || ArrayBuffer.isView(body)) {
138
- req.end(body);
139
- }
140
- else if (typeof body.pipe === "function") {
141
- body.pipe(req);
142
- }
143
- else {
144
- // Fallback: try to serialize objects
145
- req.end(String(body));
146
- }
147
- });
148
- const responseHeaders = new Headers();
149
- for (const [k, v] of Object.entries(res.headers)) {
150
- if (Array.isArray(v)) {
151
- v.forEach(item => responseHeaders.append(k, item));
152
- }
153
- else if (v != null) {
154
- responseHeaders.set(k, String(v));
155
- }
156
- }
157
- // Convert Node stream to Web ReadableStream
158
- const webStream = stream_1.Readable.toWeb(res);
159
- return new Response(webStream, {
160
- status: res.statusCode || 0,
161
- statusText: res.statusMessage || "",
162
- headers: responseHeaders,
163
- });
21
+ fetch: secureClient.fetch,
164
22
  });
165
23
  }
@@ -4,7 +4,7 @@ import type { VerificationDocument } from "./verifier";
4
4
  interface TinfoilAIOptions {
5
5
  apiKey?: string;
6
6
  baseURL?: string;
7
- hpkeKeyURL?: string;
7
+ enclaveURL?: string;
8
8
  configRepo?: string;
9
9
  [key: string]: any;
10
10
  }
@@ -13,10 +13,11 @@ export declare class TinfoilAI {
13
13
  private clientPromise;
14
14
  private readyPromise?;
15
15
  private configRepo?;
16
+ private secureClient;
16
17
  private verificationDocument?;
17
18
  apiKey?: string;
18
19
  baseURL?: string;
19
- hpkeKeyURL?: string;
20
+ enclaveURL?: string;
20
21
  constructor(options?: TinfoilAIOptions);
21
22
  ready(): Promise<void>;
22
23
  private initClient;
@@ -5,10 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.TinfoilAI = void 0;
7
7
  const openai_1 = __importDefault(require("openai"));
8
- const verifier_1 = require("./verifier");
8
+ const secure_client_1 = require("tinfoil/secure-client");
9
9
  const config_1 = require("./config");
10
- const encrypted_body_fetch_1 = require("./encrypted-body-fetch");
11
- const env_1 = require("./env");
12
10
  function createAsyncProxy(promise) {
13
11
  return new Proxy({}, {
14
12
  get(target, prop) {
@@ -41,8 +39,14 @@ class TinfoilAI {
41
39
  }
42
40
  this.apiKey = openAIOptions.apiKey;
43
41
  this.baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
44
- this.hpkeKeyURL = options.hpkeKeyURL || config_1.TINFOIL_CONFIG.HPKE_KEY_URL;
42
+ this.enclaveURL = options.enclaveURL || config_1.TINFOIL_CONFIG.ENCLAVE_URL;
45
43
  this.configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
44
+ // Create the secure client for handling transport security
45
+ this.secureClient = new secure_client_1.SecureClient({
46
+ baseURL: this.baseURL,
47
+ enclaveURL: this.enclaveURL,
48
+ configRepo: this.configRepo,
49
+ });
46
50
  this.clientPromise = this.initClient(openAIOptions);
47
51
  }
48
52
  async ready() {
@@ -57,34 +61,15 @@ class TinfoilAI {
57
61
  return this.createOpenAIClient(options);
58
62
  }
59
63
  async createOpenAIClient(options = {}) {
60
- const verifier = new verifier_1.Verifier({
61
- serverURL: this.baseURL,
62
- configRepo: this.configRepo,
63
- });
64
- try {
65
- await verifier.verify();
66
- this.verificationDocument = verifier.getVerificationDocument();
67
- if (!this.verificationDocument) {
68
- throw new Error("Verification document not available after successful verification");
69
- }
70
- }
71
- catch (error) {
72
- throw new Error(`Failed to verify enclave: ${error}`);
73
- }
74
- const hpkePublicKey = this.verificationDocument.enclaveMeasurement.hpkePublicKey;
75
- if (!hpkePublicKey) {
76
- // In browsers we require HPKE; TLS pinning fallback is Node-only
77
- if ((0, env_1.isRealBrowser)()) {
78
- throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
79
- "Only HPKE-enabled enclaves can be used in browser environments.");
80
- }
81
- throw new Error("HPKE public key is required in browser environments");
64
+ await this.secureClient.ready();
65
+ this.verificationDocument = await this.secureClient.getVerificationDocument();
66
+ if (!this.verificationDocument) {
67
+ throw new Error("Verification document not available after successful verification");
82
68
  }
83
- const fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(this.baseURL, hpkePublicKey, this.hpkeKeyURL);
84
69
  const clientOptions = {
85
70
  ...options,
86
71
  baseURL: this.baseURL,
87
- fetch: fetchFunction,
72
+ fetch: this.secureClient.fetch,
88
73
  };
89
74
  // In browser usage, OpenAI SDK typically needs dangerouslyAllowBrowser
90
75
  clientOptions.dangerouslyAllowBrowser = true;
package/dist/client.d.ts CHANGED
@@ -14,8 +14,8 @@ interface TinfoilAIOptions {
14
14
  apiKey?: string;
15
15
  /** Override the inference API base URL */
16
16
  baseURL?: string;
17
- /** Override the URL used to fetch the HPKE key (defaults to baseURL) */
18
- hpkeKeyURL?: string;
17
+ /** Override the URL used to fetch the ENCLAVE key (defaults to baseURL) */
18
+ enclaveURL?: string;
19
19
  /** Override the config GitHub repository */
20
20
  configRepo?: string;
21
21
  [key: string]: any;
@@ -25,10 +25,11 @@ export declare class TinfoilAI {
25
25
  private clientPromise;
26
26
  private readyPromise?;
27
27
  private configRepo?;
28
+ private secureClient;
28
29
  private verificationDocument?;
29
30
  apiKey?: string;
30
31
  baseURL?: string;
31
- hpkeKeyURL?: string;
32
+ enclaveURL?: string;
32
33
  /**
33
34
  * Creates a new TinfoilAI instance.
34
35
  * @param options - Configuration options including apiKey and other OpenAI client options
package/dist/client.js CHANGED
@@ -5,17 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.TinfoilAI = void 0;
7
7
  const openai_1 = __importDefault(require("openai"));
8
- const verifier_1 = require("./verifier");
8
+ const secure_client_1 = require("tinfoil/secure-client");
9
9
  const config_1 = require("./config");
10
- const encrypted_body_fetch_1 = require("./encrypted-body-fetch");
11
- const https_1 = __importDefault(require("https"));
12
- const tls_1 = require("tls");
13
- const crypto_1 = require("crypto");
14
- const stream_1 = require("stream");
15
- /**
16
- * Detects if the code is running in a real browser environment.
17
- * Returns false for Node.js environments, even with WASM loaded.
18
- */
19
10
  const env_1 = require("./env");
20
11
  /**
21
12
  * Creates a proxy that allows property access and method calls on a Promise before it resolves.
@@ -72,9 +63,13 @@ class TinfoilAI {
72
63
  // Store properties for compatibility
73
64
  this.apiKey = openAIOptions.apiKey;
74
65
  this.baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
75
- this.hpkeKeyURL = options.hpkeKeyURL || config_1.TINFOIL_CONFIG.HPKE_KEY_URL;
76
- this.configRepo =
77
- options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
66
+ this.enclaveURL = options.enclaveURL || config_1.TINFOIL_CONFIG.ENCLAVE_URL;
67
+ this.configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
68
+ this.secureClient = new secure_client_1.SecureClient({
69
+ baseURL: this.baseURL,
70
+ enclaveURL: this.enclaveURL,
71
+ configRepo: this.configRepo,
72
+ });
78
73
  this.clientPromise = this.initClient(openAIOptions);
79
74
  }
80
75
  /**
@@ -93,44 +88,15 @@ class TinfoilAI {
93
88
  return this.createOpenAIClient(options);
94
89
  }
95
90
  async createOpenAIClient(options = {}) {
96
- // Verify the enclave before establishing a transport
97
- const verifier = new verifier_1.Verifier({
98
- serverURL: this.baseURL,
99
- configRepo: this.configRepo,
100
- });
101
- try {
102
- await verifier.verify();
103
- this.verificationDocument = verifier.getVerificationDocument();
104
- if (!this.verificationDocument) {
105
- throw new Error("Verification document not available after successful verification");
106
- }
107
- }
108
- catch (error) {
109
- throw new Error(`Failed to verify enclave: ${error}`);
110
- }
111
- const hpkePublicKey = this.verificationDocument.enclaveMeasurement.hpkePublicKey;
112
- const tlsPublicKeyFingerprint = this.verificationDocument.enclaveMeasurement.tlsPublicKeyFingerprint;
113
- let fetchFunction;
114
- if (hpkePublicKey) {
115
- // HPKE available: use encrypted body fetch
116
- fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(this.baseURL, hpkePublicKey, this.hpkeKeyURL);
117
- }
118
- else {
119
- // HPKE not available: check if we're in a browser
120
- if ((0, env_1.isRealBrowser)()) {
121
- throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
122
- "Only HPKE-enabled enclaves can be used in browser environments.");
123
- }
124
- // Node.js environment: fall back to TLS-only verification using pinned TLS fetch
125
- if (!tlsPublicKeyFingerprint) {
126
- throw new Error("Neither HPKE public key nor TLS public key fingerprint available for verification");
127
- }
128
- fetchFunction = createPinnedTlsFetch(tlsPublicKeyFingerprint);
91
+ await this.secureClient.ready();
92
+ this.verificationDocument = await this.secureClient.getVerificationDocument();
93
+ if (!this.verificationDocument) {
94
+ throw new Error("Verification document not available after successful verification");
129
95
  }
130
96
  const clientOptions = {
131
97
  ...options,
132
98
  baseURL: this.baseURL,
133
- fetch: fetchFunction,
99
+ fetch: this.secureClient.fetch,
134
100
  };
135
101
  // Enable dangerouslyAllowBrowser in Node.js with WASM (which makes OpenAI SDK think we're in a browser)
136
102
  // OR if explicitly set by the user (for legitimate browser usage like playgrounds)
@@ -250,109 +216,3 @@ exports.TinfoilAI = TinfoilAI;
250
216
  TinfoilAI.Uploads = openai_1.default.Uploads;
251
217
  TinfoilAI.VectorStores = openai_1.default.VectorStores;
252
218
  })(TinfoilAI || (exports.TinfoilAI = TinfoilAI = {}));
253
- function createPinnedTlsFetch(expectedFingerprintHex) {
254
- return (async (input, init) => {
255
- // Normalize URL
256
- const makeURL = (value) => {
257
- if (typeof value === "string")
258
- return new URL(value);
259
- if (value instanceof URL)
260
- return value;
261
- return new URL(value.url);
262
- };
263
- const url = makeURL(input);
264
- if (url.protocol !== "https:") {
265
- throw new Error(`HTTP connections are not allowed. Use HTTPS. URL: ${url.toString()}`);
266
- }
267
- // Gather method and headers
268
- const method = (init?.method || input.method || "GET").toUpperCase();
269
- const headers = new Headers(init?.headers || input?.headers || {});
270
- const headerObj = {};
271
- headers.forEach((v, k) => {
272
- headerObj[k] = v;
273
- });
274
- // Resolve body
275
- let body = init?.body;
276
- if (!body && input instanceof Request) {
277
- // If the original was a Request with a body, read it
278
- try {
279
- const buf = await input.arrayBuffer();
280
- if (buf && buf.byteLength)
281
- body = Buffer.from(buf);
282
- }
283
- catch { }
284
- }
285
- // Convert web streams to Node streams if needed
286
- if (body && typeof body.getReader === "function") {
287
- body = stream_1.Readable.fromWeb(body);
288
- }
289
- if (body instanceof ArrayBuffer) {
290
- body = Buffer.from(body);
291
- }
292
- if (ArrayBuffer.isView(body)) {
293
- body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
294
- }
295
- const requestOptions = {
296
- protocol: url.protocol,
297
- hostname: url.hostname,
298
- port: url.port ? Number(url.port) : 443,
299
- path: `${url.pathname}${url.search}`,
300
- method,
301
- headers: headerObj,
302
- checkServerIdentity: (host, cert) => {
303
- const raw = cert.raw;
304
- if (!raw) {
305
- return new Error("Certificate raw bytes are unavailable for pinning");
306
- }
307
- const x509 = new crypto_1.X509Certificate(raw);
308
- const publicKeyDer = x509.publicKey.export({ type: "spki", format: "der" });
309
- const fp = (0, crypto_1.createHash)("sha256").update(publicKeyDer).digest("hex");
310
- if (fp !== expectedFingerprintHex) {
311
- return new Error(`Certificate public key fingerprint mismatch. Expected: ${expectedFingerprintHex}, Got: ${fp}`);
312
- }
313
- return (0, tls_1.checkServerIdentity)(host, cert);
314
- },
315
- };
316
- const { signal } = init || {};
317
- const res = await new Promise((resolve, reject) => {
318
- const req = https_1.default.request(requestOptions, resolve);
319
- req.on("error", reject);
320
- if (signal) {
321
- if (signal.aborted) {
322
- req.destroy(new Error("Request aborted"));
323
- return;
324
- }
325
- signal.addEventListener("abort", () => req.destroy(new Error("Request aborted")));
326
- }
327
- if (body === undefined || body === null) {
328
- req.end();
329
- }
330
- else if (typeof body === "string" || Buffer.isBuffer(body) || ArrayBuffer.isView(body)) {
331
- req.end(body);
332
- }
333
- else if (typeof body.pipe === "function") {
334
- body.pipe(req);
335
- }
336
- else {
337
- // Fallback: try to serialize objects
338
- req.end(String(body));
339
- }
340
- });
341
- const responseHeaders = new Headers();
342
- for (const [k, v] of Object.entries(res.headers)) {
343
- if (Array.isArray(v)) {
344
- v.forEach(item => responseHeaders.append(k, item));
345
- }
346
- else if (v != null) {
347
- responseHeaders.set(k, String(v));
348
- }
349
- }
350
- // Convert Node stream to Web ReadableStream
351
- const webStream = stream_1.Readable.toWeb(res);
352
- return new Response(webStream, {
353
- status: res.statusCode || 0,
354
- statusText: res.statusMessage || "",
355
- headers: responseHeaders,
356
- });
357
- });
358
- }
package/dist/config.d.ts CHANGED
@@ -7,11 +7,12 @@ export declare const TINFOIL_CONFIG: {
7
7
  */
8
8
  readonly INFERENCE_BASE_URL: "https://inference.tinfoil.sh/v1/";
9
9
  /**
10
- * The URL used to fetch the HPKE server public key (via EHBP .well-known endpoint).
11
- * Defaults to the same value as `INFERENCE_BASE_URL` so hpkeKeyURL and baseURL share
10
+ * The URL used to fetch the enclave server public key (via EHBP .well-known endpoint).
11
+ * and the attestation (vial the attestation .well-known endpoint too).
12
+ * Defaults to the same value as `INFERENCE_BASE_URL` so enclaveURL and baseURL share
12
13
  * the same default. Callers may override independently if key discovery is hosted elsewhere.
13
14
  */
14
- readonly HPKE_KEY_URL: "https://inference.tinfoil.sh/v1/";
15
+ readonly ENCLAVE_URL: "https://inference.tinfoil.sh/v1/";
15
16
  /**
16
17
  * The GitHub repository for the confidential inference proxy
17
18
  */
package/dist/config.js CHANGED
@@ -10,11 +10,12 @@ exports.TINFOIL_CONFIG = {
10
10
  */
11
11
  INFERENCE_BASE_URL: "https://inference.tinfoil.sh/v1/",
12
12
  /**
13
- * The URL used to fetch the HPKE server public key (via EHBP .well-known endpoint).
14
- * Defaults to the same value as `INFERENCE_BASE_URL` so hpkeKeyURL and baseURL share
13
+ * The URL used to fetch the enclave server public key (via EHBP .well-known endpoint).
14
+ * and the attestation (vial the attestation .well-known endpoint too).
15
+ * Defaults to the same value as `INFERENCE_BASE_URL` so enclaveURL and baseURL share
15
16
  * the same default. Callers may override independently if key discovery is hosted elsewhere.
16
17
  */
17
- HPKE_KEY_URL: "https://inference.tinfoil.sh/v1/",
18
+ ENCLAVE_URL: "https://inference.tinfoil.sh/v1/",
18
19
  /**
19
20
  * The GitHub repository for the confidential inference proxy
20
21
  */
@@ -3,8 +3,8 @@ export declare function normalizeEncryptedBodyRequestArgs(input: RequestInfo | U
3
3
  url: string;
4
4
  init?: RequestInit;
5
5
  };
6
- export declare function encryptedBodyRequest(input: RequestInfo | URL, hpkePublicKey: string, init?: RequestInit, hpkeKeyURL?: string): Promise<Response>;
7
- export declare function createEncryptedBodyFetch(baseURL: string, hpkePublicKey: string, hpkeKeyURL?: string): typeof fetch;
6
+ export declare function encryptedBodyRequest(input: RequestInfo | URL, hpkePublicKey: string, init?: RequestInit, enclaveURL?: string): Promise<Response>;
7
+ export declare function createEncryptedBodyFetch(baseURL: string, hpkePublicKey: string, enclaveURL?: string): typeof fetch;
8
8
  export declare function __setEhbpModuleForTests(module: EhbpModule | undefined): void;
9
9
  export declare function __resetEhbpModuleStateForTests(): void;
10
10
  export {};
@@ -64,12 +64,12 @@ function normalizeEncryptedBodyRequestArgs(input, init) {
64
64
  init: { ...derivedInit, ...init },
65
65
  };
66
66
  }
67
- async function encryptedBodyRequest(input, hpkePublicKey, init, hpkeKeyURL) {
67
+ async function encryptedBodyRequest(input, hpkePublicKey, init, enclaveURL) {
68
68
  const { url: requestUrl, init: requestInit } = normalizeEncryptedBodyRequestArgs(input, init);
69
69
  const u = new URL(requestUrl);
70
70
  const { origin } = u;
71
71
  // Determine which origin to fetch the HPKE key from (may be different from request origin)
72
- const keyOrigin = hpkeKeyURL ? new URL(hpkeKeyURL).origin : origin;
72
+ const keyOrigin = enclaveURL ? new URL(enclaveURL).origin : origin;
73
73
  // If key origin matches request origin, use the standard transport (fetches keys from origin)
74
74
  // Otherwise, build a composite transport that routes to `origin` but uses the key fetched from `keyOrigin`.
75
75
  const transport = keyOrigin === origin
@@ -91,11 +91,11 @@ async function encryptedBodyRequest(input, hpkePublicKey, init, hpkeKeyURL) {
91
91
  }
92
92
  return transport.request(requestUrl, requestInit);
93
93
  }
94
- function createEncryptedBodyFetch(baseURL, hpkePublicKey, hpkeKeyURL) {
94
+ function createEncryptedBodyFetch(baseURL, hpkePublicKey, enclaveURL) {
95
95
  return (async (input, init) => {
96
96
  const normalized = normalizeEncryptedBodyRequestArgs(input, init);
97
97
  const targetUrl = new URL(normalized.url, baseURL);
98
- return encryptedBodyRequest(targetUrl.toString(), hpkePublicKey, normalized.init, hpkeKeyURL);
98
+ return encryptedBodyRequest(targetUrl.toString(), hpkePublicKey, normalized.init, enclaveURL);
99
99
  });
100
100
  }
101
101
  // Private helper functions
@@ -1,17 +1,7 @@
1
1
  interface CreateTinfoilAIOptions {
2
- /** Override the inference API base URL */
3
2
  baseURL?: string;
4
- /** Override the URL used to fetch the HPKE (defaults to baseURL) */
5
- hpkeKeyURL?: string;
6
- /** Override the config GitHub repository */
3
+ enclaveURL?: string;
7
4
  configRepo?: string;
8
5
  }
9
- /**
10
- * Creates an AI SDK provider with the specified API key.
11
- *
12
- * @param apiKey - The API key for the Tinfoil API
13
- * @param options - Optional configuration options
14
- * @returns A TinfoilAI instance
15
- */
16
6
  export declare function createTinfoilAI(apiKey: string, options?: CreateTinfoilAIOptions): Promise<import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<string, string, string, string>>;
17
7
  export {};