tinfoil 0.9.1 → 0.10.0

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.
@@ -1,13 +1,14 @@
1
1
  import { Verifier } from "./verifier";
2
2
  import { TINFOIL_CONFIG } from "./config";
3
3
  import { createSecureFetch } from "tinfoil/secure-fetch";
4
+ import { fetchRouter } from "./router";
4
5
  export class SecureClient {
5
6
  constructor(options = {}) {
6
7
  this.initPromise = null;
7
8
  this.verificationDocument = null;
8
9
  this._fetch = null;
9
- this.baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
10
- this.enclaveURL = options.enclaveURL || TINFOIL_CONFIG.ENCLAVE_URL;
10
+ this.baseURL = options.baseURL;
11
+ this.enclaveURL = options.enclaveURL;
11
12
  this.configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
12
13
  }
13
14
  async ready() {
@@ -17,34 +18,136 @@ export class SecureClient {
17
18
  return this.initPromise;
18
19
  }
19
20
  async initSecureClient() {
21
+ // Only fetch router if neither baseURL nor enclaveURL is provided
22
+ if (!this.baseURL && !this.enclaveURL) {
23
+ const routerAddress = await fetchRouter();
24
+ this.enclaveURL = `https://${routerAddress}`;
25
+ this.baseURL = `https://${routerAddress}/v1/`;
26
+ }
27
+ // Ensure both baseURL and enclaveURL are initialized
28
+ if (!this.baseURL) {
29
+ if (this.enclaveURL) {
30
+ // If enclaveURL is provided but baseURL is not, derive baseURL from enclaveURL
31
+ const enclaveUrl = new URL(this.enclaveURL);
32
+ this.baseURL = `${enclaveUrl.origin}/v1/`;
33
+ }
34
+ else {
35
+ throw new Error("Unable to determine baseURL: neither baseURL nor enclaveURL provided");
36
+ }
37
+ }
38
+ if (!this.enclaveURL) {
39
+ if (this.baseURL) {
40
+ // If baseURL is provided but enclaveURL is not, derive enclaveURL from baseURL
41
+ const baseUrl = new URL(this.baseURL);
42
+ this.enclaveURL = baseUrl.origin;
43
+ }
44
+ else {
45
+ throw new Error("Unable to determine enclaveURL: neither baseURL nor enclaveURL provided");
46
+ }
47
+ }
20
48
  const verifier = new Verifier({
21
49
  serverURL: this.enclaveURL,
22
50
  configRepo: this.configRepo,
23
51
  });
24
- await verifier.verify();
25
- const doc = verifier.getVerificationDocument();
26
- if (!doc) {
27
- throw new Error("Verification document not available after successful verification");
28
- }
29
- this.verificationDocument = doc;
30
- // Extract keys from the verification document
31
- const { hpkePublicKey, tlsPublicKeyFingerprint } = this.verificationDocument.enclaveMeasurement;
32
- this._fetch = createSecureFetch(this.baseURL, this.enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint);
52
+ try {
53
+ await verifier.verify();
54
+ const doc = verifier.getVerificationDocument();
55
+ if (!doc) {
56
+ throw new Error("Verification document not available after successful verification");
57
+ }
58
+ this.verificationDocument = doc;
59
+ // Extract keys from the verification document
60
+ const { hpkePublicKey, tlsPublicKeyFingerprint } = this.verificationDocument.enclaveMeasurement;
61
+ try {
62
+ this._fetch = createSecureFetch(this.baseURL, this.enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint);
63
+ }
64
+ catch (transportError) {
65
+ this.verificationDocument.steps.createTransport = {
66
+ status: 'failed',
67
+ error: transportError.message
68
+ };
69
+ this.verificationDocument.securityVerified = false;
70
+ throw transportError;
71
+ }
72
+ }
73
+ catch (error) {
74
+ const doc = verifier.getVerificationDocument();
75
+ if (doc) {
76
+ this.verificationDocument = doc;
77
+ }
78
+ else {
79
+ this.verificationDocument = {
80
+ configRepo: this.configRepo,
81
+ enclaveHost: new URL(this.enclaveURL).hostname,
82
+ releaseDigest: '',
83
+ codeMeasurement: { type: '', registers: [] },
84
+ enclaveMeasurement: { measurement: { type: '', registers: [] } },
85
+ securityVerified: false,
86
+ steps: {
87
+ fetchDigest: { status: 'pending' },
88
+ verifyCode: { status: 'pending' },
89
+ verifyEnclave: { status: 'pending' },
90
+ compareMeasurements: { status: 'pending' },
91
+ otherError: { status: 'failed', error: error.message }
92
+ }
93
+ };
94
+ }
95
+ throw error;
96
+ }
33
97
  }
34
98
  async getVerificationDocument() {
35
99
  if (!this.initPromise) {
36
100
  await this.ready();
37
101
  }
38
- await this.initPromise;
102
+ await this.initPromise.catch(() => { });
39
103
  if (!this.verificationDocument) {
40
104
  throw new Error("Verification document unavailable: client not verified yet");
41
105
  }
42
106
  return this.verificationDocument;
43
107
  }
108
+ getBaseURL() {
109
+ return this.baseURL;
110
+ }
44
111
  get fetch() {
45
112
  return async (input, init) => {
46
113
  await this.ready();
47
- return this._fetch(input, init);
114
+ try {
115
+ return await this._fetch(input, init);
116
+ }
117
+ catch (error) {
118
+ if (this.verificationDocument) {
119
+ const errorMessage = error.message;
120
+ if (errorMessage.includes('HPKE public key mismatch')) {
121
+ this.verificationDocument.steps.verifyHPKEKey = {
122
+ status: 'failed',
123
+ error: errorMessage
124
+ };
125
+ this.verificationDocument.securityVerified = false;
126
+ }
127
+ else if (errorMessage.includes('Transport initialization failed') || errorMessage.includes('Request initialization failed')) {
128
+ this.verificationDocument.steps.createTransport = {
129
+ status: 'failed',
130
+ error: errorMessage
131
+ };
132
+ this.verificationDocument.securityVerified = false;
133
+ }
134
+ else if (errorMessage.includes('Failed to get HPKE key')) {
135
+ this.verificationDocument.steps.verifyHPKEKey = {
136
+ status: 'failed',
137
+ error: errorMessage
138
+ };
139
+ this.verificationDocument.securityVerified = false;
140
+ }
141
+ else {
142
+ this.verificationDocument.steps.otherError = {
143
+ status: 'failed',
144
+ error: errorMessage
145
+ };
146
+ this.verificationDocument.securityVerified = false;
147
+ }
148
+ }
149
+ throw error;
150
+ }
48
151
  };
49
152
  }
50
153
  }
@@ -36,8 +36,8 @@ export class TinfoilAI {
36
36
  openAIOptions.apiKey = process.env.TINFOIL_API_KEY;
37
37
  }
38
38
  this.apiKey = openAIOptions.apiKey;
39
- this.baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
40
- this.enclaveURL = options.enclaveURL || TINFOIL_CONFIG.ENCLAVE_URL;
39
+ this.baseURL = options.baseURL;
40
+ this.enclaveURL = options.enclaveURL;
41
41
  this.configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
42
42
  this.secureClient = new SecureClient({
43
43
  baseURL: this.baseURL,
@@ -63,9 +63,11 @@ export class TinfoilAI {
63
63
  if (!this.verificationDocument) {
64
64
  throw new Error("Verification document not available after successful verification");
65
65
  }
66
+ // Use the provided baseURL, or get it from SecureClient after initialization
67
+ const baseURL = this.baseURL || this.secureClient.getBaseURL();
66
68
  const clientOptions = {
67
69
  ...options,
68
- baseURL: this.baseURL,
70
+ baseURL: baseURL,
69
71
  fetch: this.secureClient.fetch,
70
72
  };
71
73
  if (isRealBrowser() || options.dangerouslyAllowBrowser === true) {
@@ -6,8 +6,8 @@ interface UnverifiedClientOptions {
6
6
  export declare class UnverifiedClient {
7
7
  private initPromise;
8
8
  private _fetch;
9
- private readonly baseURL;
10
- private readonly enclaveURL;
9
+ private baseURL?;
10
+ private enclaveURL?;
11
11
  private readonly configRepo;
12
12
  constructor(options?: UnverifiedClientOptions);
13
13
  ready(): Promise<void>;
@@ -1,11 +1,12 @@
1
1
  import { TINFOIL_CONFIG } from "./config";
2
2
  import { createEncryptedBodyFetch } from "./encrypted-body-fetch";
3
+ import { fetchRouter } from "./router";
3
4
  export class UnverifiedClient {
4
5
  constructor(options = {}) {
5
6
  this.initPromise = null;
6
7
  this._fetch = null;
7
- this.baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
8
- this.enclaveURL = options.enclaveURL || TINFOIL_CONFIG.ENCLAVE_URL;
8
+ this.baseURL = options.baseURL;
9
+ this.enclaveURL = options.enclaveURL;
9
10
  this.configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
10
11
  }
11
12
  async ready() {
@@ -15,6 +16,33 @@ export class UnverifiedClient {
15
16
  return this.initPromise;
16
17
  }
17
18
  async initUnverifiedClient() {
19
+ // Only fetch router if neither baseURL nor enclaveURL is provided
20
+ if (!this.baseURL && !this.enclaveURL) {
21
+ const routerAddress = await fetchRouter();
22
+ this.enclaveURL = `https://${routerAddress}`;
23
+ this.baseURL = `https://${routerAddress}/v1/`;
24
+ }
25
+ // Ensure both baseURL and enclaveURL are initialized
26
+ if (!this.baseURL) {
27
+ if (this.enclaveURL) {
28
+ // If enclaveURL is provided but baseURL is not, derive baseURL from enclaveURL
29
+ const enclaveUrl = new URL(this.enclaveURL);
30
+ this.baseURL = `${enclaveUrl.origin}/v1/`;
31
+ }
32
+ else {
33
+ throw new Error("Unable to determine baseURL: neither baseURL nor enclaveURL provided");
34
+ }
35
+ }
36
+ if (!this.enclaveURL) {
37
+ if (this.baseURL) {
38
+ // If baseURL is provided but enclaveURL is not, derive enclaveURL from baseURL
39
+ const baseUrl = new URL(this.baseURL);
40
+ this.enclaveURL = baseUrl.origin;
41
+ }
42
+ else {
43
+ throw new Error("Unable to determine enclaveURL: neither baseURL nor enclaveURL provided");
44
+ }
45
+ }
18
46
  this._fetch = createEncryptedBodyFetch(this.baseURL, undefined, this.enclaveURL);
19
47
  }
20
48
  async getVerificationDocument() {
@@ -37,6 +37,9 @@ export interface VerificationDocument {
37
37
  verifyCode: VerificationStepState;
38
38
  verifyEnclave: VerificationStepState;
39
39
  compareMeasurements: VerificationStepState;
40
+ createTransport?: VerificationStepState;
41
+ verifyHPKEKey?: VerificationStepState;
42
+ otherError?: VerificationStepState;
40
43
  };
41
44
  }
42
45
  export interface MeasurementComparisonResult {
@@ -84,7 +87,7 @@ export declare class Verifier {
84
87
  private static executeWithWasm;
85
88
  /**
86
89
  * Fetch the latest release digest from GitHub
87
- * @param configRepo - Repository name (e.g., "tinfoilsh/confidential-inference-proxy")
90
+ * @param configRepo - Repository name (e.g., "tinfoilsh/confidential-model-router")
88
91
  * @returns The digest hash
89
92
  */
90
93
  fetchLatestDigest(configRepo?: string): Promise<string>;
@@ -124,7 +124,8 @@ function compareMeasurementsError(codeMeasurement, runtimeMeasurement) {
124
124
  }
125
125
  if (codeMeasurement.type === PLATFORM_TYPES.SNP_TDX_MULTI_PLATFORM_V1) {
126
126
  switch (runtimeMeasurement.type) {
127
- case PLATFORM_TYPES.TDX_GUEST_V1: {
127
+ case PLATFORM_TYPES.TDX_GUEST_V1:
128
+ case PLATFORM_TYPES.TDX_GUEST_V2: {
128
129
  if (codeMeasurement.registers.length < 3 || runtimeMeasurement.registers.length < 4) {
129
130
  return new Error(MEASUREMENT_ERROR_MESSAGES.FEW_REGISTERS);
130
131
  }
@@ -194,8 +195,10 @@ export function compareMeasurements(codeMeasurement, runtimeMeasurement) {
194
195
  */
195
196
  export class Verifier {
196
197
  constructor(options) {
197
- const serverURL = options?.serverURL ?? TINFOIL_CONFIG.INFERENCE_BASE_URL;
198
- this.serverURL = new URL(serverURL).hostname;
198
+ if (!options?.serverURL) {
199
+ throw new Error("serverURL is required for Verifier");
200
+ }
201
+ this.serverURL = new URL(options.serverURL).hostname;
199
202
  this.configRepo = options?.configRepo ?? TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
200
203
  }
201
204
  /**
@@ -256,7 +259,7 @@ export class Verifier {
256
259
  }
257
260
  /**
258
261
  * Fetch the latest release digest from GitHub
259
- * @param configRepo - Repository name (e.g., "tinfoilsh/confidential-inference-proxy")
262
+ * @param configRepo - Repository name (e.g., "tinfoilsh/confidential-model-router")
260
263
  * @returns The digest hash
261
264
  */
262
265
  async fetchLatestDigest(configRepo) {
@@ -439,6 +442,15 @@ export class Verifier {
439
442
  }
440
443
  catch (error) {
441
444
  steps.fetchDigest = { status: 'failed', error: error.message };
445
+ this.lastVerificationDocument = {
446
+ configRepo: this.configRepo,
447
+ enclaveHost: this.serverURL,
448
+ releaseDigest: '',
449
+ codeMeasurement: { type: '', registers: [] },
450
+ enclaveMeasurement: { measurement: { type: '', registers: [] } },
451
+ securityVerified: false,
452
+ steps,
453
+ };
442
454
  throw error;
443
455
  }
444
456
  try {
package/dist/index.d.ts CHANGED
@@ -5,4 +5,5 @@ export * from "./ai-sdk-provider";
5
5
  export * from "./config";
6
6
  export { SecureClient } from "./secure-client";
7
7
  export { UnverifiedClient } from "./unverified-client";
8
+ export { fetchRouter } from "./router";
8
9
  export { type Uploadable, toFile, APIPromise, PagePromise, OpenAIError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError, } from "openai";
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.UnprocessableEntityError = exports.PermissionDeniedError = exports.InternalServerError = exports.AuthenticationError = exports.BadRequestError = exports.RateLimitError = exports.ConflictError = exports.NotFoundError = exports.APIUserAbortError = exports.APIConnectionTimeoutError = exports.APIConnectionError = exports.APIError = exports.OpenAIError = exports.PagePromise = exports.APIPromise = exports.toFile = exports.UnverifiedClient = exports.SecureClient = exports.default = exports.TinfoilAI = void 0;
17
+ exports.UnprocessableEntityError = exports.PermissionDeniedError = exports.InternalServerError = exports.AuthenticationError = exports.BadRequestError = exports.RateLimitError = exports.ConflictError = exports.NotFoundError = exports.APIUserAbortError = exports.APIConnectionTimeoutError = exports.APIConnectionError = exports.APIError = exports.OpenAIError = exports.PagePromise = exports.APIPromise = exports.toFile = exports.fetchRouter = exports.UnverifiedClient = exports.SecureClient = exports.default = exports.TinfoilAI = void 0;
18
18
  // Re-export the TinfoilAI class
19
19
  var tinfoilai_1 = require("./tinfoilai");
20
20
  Object.defineProperty(exports, "TinfoilAI", { enumerable: true, get: function () { return tinfoilai_1.TinfoilAI; } });
@@ -28,6 +28,8 @@ var secure_client_1 = require("./secure-client");
28
28
  Object.defineProperty(exports, "SecureClient", { enumerable: true, get: function () { return secure_client_1.SecureClient; } });
29
29
  var unverified_client_1 = require("./unverified-client");
30
30
  Object.defineProperty(exports, "UnverifiedClient", { enumerable: true, get: function () { return unverified_client_1.UnverifiedClient; } });
31
+ var router_1 = require("./router");
32
+ Object.defineProperty(exports, "fetchRouter", { enumerable: true, get: function () { return router_1.fetchRouter; } });
31
33
  // Re-export OpenAI utility types and classes that users might need
32
34
  // Using public exports from the main OpenAI package instead of deep imports
33
35
  var openai_1 = require("openai");
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Router utilities for fetching available Tinfoil routers
3
+ */
4
+ /**
5
+ * Fetches the list of available routers from the ATC API
6
+ * and returns a randomly selected address.
7
+ *
8
+ * @returns Promise<string> A randomly selected router address
9
+ * @throws Error if no routers are found or if the request fails
10
+ */
11
+ export declare function fetchRouter(): Promise<string>;
package/dist/router.js ADDED
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fetchRouter = fetchRouter;
4
+ const config_1 = require("./config");
5
+ /**
6
+ * Router utilities for fetching available Tinfoil routers
7
+ */
8
+ /**
9
+ * Fetches the list of available routers from the ATC API
10
+ * and returns a randomly selected address.
11
+ *
12
+ * @returns Promise<string> A randomly selected router address
13
+ * @throws Error if no routers are found or if the request fails
14
+ */
15
+ async function fetchRouter() {
16
+ const routersUrl = config_1.TINFOIL_CONFIG.ATC_API_URL;
17
+ try {
18
+ const response = await fetch(routersUrl);
19
+ if (!response.ok) {
20
+ throw new Error(`Failed to fetch routers: ${response.status} ${response.statusText}`);
21
+ }
22
+ const routers = await response.json();
23
+ if (!Array.isArray(routers) || routers.length === 0) {
24
+ throw new Error("No routers found in the response");
25
+ }
26
+ // Return a randomly selected router
27
+ const randomIndex = Math.floor(Math.random() * routers.length);
28
+ return routers[randomIndex];
29
+ }
30
+ catch (error) {
31
+ if (error instanceof Error) {
32
+ throw new Error(`Failed to fetch router: ${error.message}`);
33
+ }
34
+ throw new Error("Failed to fetch router: Unknown error");
35
+ }
36
+ }
@@ -8,13 +8,14 @@ export declare class SecureClient {
8
8
  private initPromise;
9
9
  private verificationDocument;
10
10
  private _fetch;
11
- private readonly baseURL?;
12
- private readonly enclaveURL?;
11
+ private baseURL?;
12
+ private enclaveURL?;
13
13
  private readonly configRepo?;
14
14
  constructor(options?: SecureClientOptions);
15
15
  ready(): Promise<void>;
16
16
  private initSecureClient;
17
17
  getVerificationDocument(): Promise<VerificationDocument>;
18
+ getBaseURL(): string | undefined;
18
19
  get fetch(): typeof fetch;
19
20
  }
20
21
  export {};
@@ -4,13 +4,14 @@ exports.SecureClient = void 0;
4
4
  const verifier_1 = require("./verifier");
5
5
  const config_1 = require("./config");
6
6
  const secure_fetch_1 = require("tinfoil/secure-fetch");
7
+ const router_1 = require("./router");
7
8
  class SecureClient {
8
9
  constructor(options = {}) {
9
10
  this.initPromise = null;
10
11
  this.verificationDocument = null;
11
12
  this._fetch = null;
12
- this.baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
13
- this.enclaveURL = options.enclaveURL || config_1.TINFOIL_CONFIG.ENCLAVE_URL;
13
+ this.baseURL = options.baseURL;
14
+ this.enclaveURL = options.enclaveURL;
14
15
  this.configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
15
16
  }
16
17
  async ready() {
@@ -20,34 +21,136 @@ class SecureClient {
20
21
  return this.initPromise;
21
22
  }
22
23
  async initSecureClient() {
24
+ // Only fetch router if neither baseURL nor enclaveURL is provided
25
+ if (!this.baseURL && !this.enclaveURL) {
26
+ const routerAddress = await (0, router_1.fetchRouter)();
27
+ this.enclaveURL = `https://${routerAddress}`;
28
+ this.baseURL = `https://${routerAddress}/v1/`;
29
+ }
30
+ // Ensure both baseURL and enclaveURL are initialized
31
+ if (!this.baseURL) {
32
+ if (this.enclaveURL) {
33
+ // If enclaveURL is provided but baseURL is not, derive baseURL from enclaveURL
34
+ const enclaveUrl = new URL(this.enclaveURL);
35
+ this.baseURL = `${enclaveUrl.origin}/v1/`;
36
+ }
37
+ else {
38
+ throw new Error("Unable to determine baseURL: neither baseURL nor enclaveURL provided");
39
+ }
40
+ }
41
+ if (!this.enclaveURL) {
42
+ if (this.baseURL) {
43
+ // If baseURL is provided but enclaveURL is not, derive enclaveURL from baseURL
44
+ const baseUrl = new URL(this.baseURL);
45
+ this.enclaveURL = baseUrl.origin;
46
+ }
47
+ else {
48
+ throw new Error("Unable to determine enclaveURL: neither baseURL nor enclaveURL provided");
49
+ }
50
+ }
23
51
  const verifier = new verifier_1.Verifier({
24
52
  serverURL: this.enclaveURL,
25
53
  configRepo: this.configRepo,
26
54
  });
27
- await verifier.verify();
28
- const doc = verifier.getVerificationDocument();
29
- if (!doc) {
30
- throw new Error("Verification document not available after successful verification");
31
- }
32
- this.verificationDocument = doc;
33
- // Extract keys from the verification document
34
- const { hpkePublicKey, tlsPublicKeyFingerprint } = this.verificationDocument.enclaveMeasurement;
35
- this._fetch = (0, secure_fetch_1.createSecureFetch)(this.baseURL, this.enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint);
55
+ try {
56
+ await verifier.verify();
57
+ const doc = verifier.getVerificationDocument();
58
+ if (!doc) {
59
+ throw new Error("Verification document not available after successful verification");
60
+ }
61
+ this.verificationDocument = doc;
62
+ // Extract keys from the verification document
63
+ const { hpkePublicKey, tlsPublicKeyFingerprint } = this.verificationDocument.enclaveMeasurement;
64
+ try {
65
+ this._fetch = (0, secure_fetch_1.createSecureFetch)(this.baseURL, this.enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint);
66
+ }
67
+ catch (transportError) {
68
+ this.verificationDocument.steps.createTransport = {
69
+ status: 'failed',
70
+ error: transportError.message
71
+ };
72
+ this.verificationDocument.securityVerified = false;
73
+ throw transportError;
74
+ }
75
+ }
76
+ catch (error) {
77
+ const doc = verifier.getVerificationDocument();
78
+ if (doc) {
79
+ this.verificationDocument = doc;
80
+ }
81
+ else {
82
+ this.verificationDocument = {
83
+ configRepo: this.configRepo,
84
+ enclaveHost: new URL(this.enclaveURL).hostname,
85
+ releaseDigest: '',
86
+ codeMeasurement: { type: '', registers: [] },
87
+ enclaveMeasurement: { measurement: { type: '', registers: [] } },
88
+ securityVerified: false,
89
+ steps: {
90
+ fetchDigest: { status: 'pending' },
91
+ verifyCode: { status: 'pending' },
92
+ verifyEnclave: { status: 'pending' },
93
+ compareMeasurements: { status: 'pending' },
94
+ otherError: { status: 'failed', error: error.message }
95
+ }
96
+ };
97
+ }
98
+ throw error;
99
+ }
36
100
  }
37
101
  async getVerificationDocument() {
38
102
  if (!this.initPromise) {
39
103
  await this.ready();
40
104
  }
41
- await this.initPromise;
105
+ await this.initPromise.catch(() => { });
42
106
  if (!this.verificationDocument) {
43
107
  throw new Error("Verification document unavailable: client not verified yet");
44
108
  }
45
109
  return this.verificationDocument;
46
110
  }
111
+ getBaseURL() {
112
+ return this.baseURL;
113
+ }
47
114
  get fetch() {
48
115
  return async (input, init) => {
49
116
  await this.ready();
50
- return this._fetch(input, init);
117
+ try {
118
+ return await this._fetch(input, init);
119
+ }
120
+ catch (error) {
121
+ if (this.verificationDocument) {
122
+ const errorMessage = error.message;
123
+ if (errorMessage.includes('HPKE public key mismatch')) {
124
+ this.verificationDocument.steps.verifyHPKEKey = {
125
+ status: 'failed',
126
+ error: errorMessage
127
+ };
128
+ this.verificationDocument.securityVerified = false;
129
+ }
130
+ else if (errorMessage.includes('Transport initialization failed') || errorMessage.includes('Request initialization failed')) {
131
+ this.verificationDocument.steps.createTransport = {
132
+ status: 'failed',
133
+ error: errorMessage
134
+ };
135
+ this.verificationDocument.securityVerified = false;
136
+ }
137
+ else if (errorMessage.includes('Failed to get HPKE key')) {
138
+ this.verificationDocument.steps.verifyHPKEKey = {
139
+ status: 'failed',
140
+ error: errorMessage
141
+ };
142
+ this.verificationDocument.securityVerified = false;
143
+ }
144
+ else {
145
+ this.verificationDocument.steps.otherError = {
146
+ status: 'failed',
147
+ error: errorMessage
148
+ };
149
+ this.verificationDocument.securityVerified = false;
150
+ }
151
+ }
152
+ throw error;
153
+ }
51
154
  };
52
155
  }
53
156
  }
package/dist/tinfoilai.js CHANGED
@@ -42,8 +42,8 @@ class TinfoilAI {
42
42
  openAIOptions.apiKey = process.env.TINFOIL_API_KEY;
43
43
  }
44
44
  this.apiKey = openAIOptions.apiKey;
45
- this.baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
46
- this.enclaveURL = options.enclaveURL || config_1.TINFOIL_CONFIG.ENCLAVE_URL;
45
+ this.baseURL = options.baseURL;
46
+ this.enclaveURL = options.enclaveURL;
47
47
  this.configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
48
48
  this.secureClient = new secure_client_1.SecureClient({
49
49
  baseURL: this.baseURL,
@@ -69,9 +69,11 @@ class TinfoilAI {
69
69
  if (!this.verificationDocument) {
70
70
  throw new Error("Verification document not available after successful verification");
71
71
  }
72
+ // Use the provided baseURL, or get it from SecureClient after initialization
73
+ const baseURL = this.baseURL || this.secureClient.getBaseURL();
72
74
  const clientOptions = {
73
75
  ...options,
74
- baseURL: this.baseURL,
76
+ baseURL: baseURL,
75
77
  fetch: this.secureClient.fetch,
76
78
  };
77
79
  if ((0, env_1.isRealBrowser)() || options.dangerouslyAllowBrowser === true) {
@@ -6,8 +6,8 @@ interface UnverifiedClientOptions {
6
6
  export declare class UnverifiedClient {
7
7
  private initPromise;
8
8
  private _fetch;
9
- private readonly baseURL;
10
- private readonly enclaveURL;
9
+ private baseURL?;
10
+ private enclaveURL?;
11
11
  private readonly configRepo;
12
12
  constructor(options?: UnverifiedClientOptions);
13
13
  ready(): Promise<void>;
@@ -3,12 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.UnverifiedClient = void 0;
4
4
  const config_1 = require("./config");
5
5
  const encrypted_body_fetch_1 = require("./encrypted-body-fetch");
6
+ const router_1 = require("./router");
6
7
  class UnverifiedClient {
7
8
  constructor(options = {}) {
8
9
  this.initPromise = null;
9
10
  this._fetch = null;
10
- this.baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
11
- this.enclaveURL = options.enclaveURL || config_1.TINFOIL_CONFIG.ENCLAVE_URL;
11
+ this.baseURL = options.baseURL;
12
+ this.enclaveURL = options.enclaveURL;
12
13
  this.configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
13
14
  }
14
15
  async ready() {
@@ -18,6 +19,33 @@ class UnverifiedClient {
18
19
  return this.initPromise;
19
20
  }
20
21
  async initUnverifiedClient() {
22
+ // Only fetch router if neither baseURL nor enclaveURL is provided
23
+ if (!this.baseURL && !this.enclaveURL) {
24
+ const routerAddress = await (0, router_1.fetchRouter)();
25
+ this.enclaveURL = `https://${routerAddress}`;
26
+ this.baseURL = `https://${routerAddress}/v1/`;
27
+ }
28
+ // Ensure both baseURL and enclaveURL are initialized
29
+ if (!this.baseURL) {
30
+ if (this.enclaveURL) {
31
+ // If enclaveURL is provided but baseURL is not, derive baseURL from enclaveURL
32
+ const enclaveUrl = new URL(this.enclaveURL);
33
+ this.baseURL = `${enclaveUrl.origin}/v1/`;
34
+ }
35
+ else {
36
+ throw new Error("Unable to determine baseURL: neither baseURL nor enclaveURL provided");
37
+ }
38
+ }
39
+ if (!this.enclaveURL) {
40
+ if (this.baseURL) {
41
+ // If baseURL is provided but enclaveURL is not, derive enclaveURL from baseURL
42
+ const baseUrl = new URL(this.baseURL);
43
+ this.enclaveURL = baseUrl.origin;
44
+ }
45
+ else {
46
+ throw new Error("Unable to determine enclaveURL: neither baseURL nor enclaveURL provided");
47
+ }
48
+ }
21
49
  this._fetch = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(this.baseURL, undefined, this.enclaveURL);
22
50
  }
23
51
  async getVerificationDocument() {