tinfoil 0.8.5 → 0.8.6

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,5 +1,5 @@
1
- export { TinfoilAI } from "./client.browser";
2
- export { TinfoilAI as default } from "./client.browser";
1
+ export { TinfoilAI } from "./tinfoilai";
2
+ export { TinfoilAI as default } from "./tinfoilai";
3
3
  export * from "./verifier";
4
4
  export * from "./verification-runner";
5
5
  export * from "./ai-sdk-provider";
@@ -1,6 +1,6 @@
1
1
  // Browser-safe entry point: avoids Node built-ins
2
- export { TinfoilAI } from "./client.browser";
3
- export { TinfoilAI as default } from "./client.browser";
2
+ export { TinfoilAI } from "./tinfoilai";
3
+ export { TinfoilAI as default } from "./tinfoilai";
4
4
  export * from "./verifier";
5
5
  export * from "./verification-runner";
6
6
  export * from "./ai-sdk-provider";
@@ -1,5 +1,5 @@
1
- export { TinfoilAI } from "./client";
2
- export { TinfoilAI as default } from "./client";
1
+ export { TinfoilAI } from "./tinfoilai";
2
+ export { TinfoilAI as default } from "./tinfoilai";
3
3
  export * from "./verifier";
4
4
  export * from "./verification-runner";
5
5
  export * from "./ai-sdk-provider";
package/dist/esm/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Re-export the TinfoilAI class
2
- export { TinfoilAI } from "./client";
3
- export { TinfoilAI as default } from "./client";
2
+ export { TinfoilAI } from "./tinfoilai";
3
+ export { TinfoilAI as default } from "./tinfoilai";
4
4
  // Export verifier and loader
5
5
  export * from "./verifier";
6
6
  export * from "./verification-runner";
@@ -1,6 +1,7 @@
1
1
  import OpenAI from "openai";
2
2
  import { SecureClient } from "tinfoil/secure-client";
3
3
  import { TINFOIL_CONFIG } from "./config";
4
+ import { isRealBrowser } from "./env";
4
5
  function createAsyncProxy(promise) {
5
6
  return new Proxy({}, {
6
7
  get(target, prop) {
@@ -28,14 +29,16 @@ export class TinfoilAI {
28
29
  const openAIOptions = { ...options };
29
30
  // In browser builds, never read secrets from process.env to avoid
30
31
  // leaking credentials into client bundles. Require explicit apiKey.
31
- if (typeof options.apiKey === "string") {
32
+ if (options.apiKey) {
32
33
  openAIOptions.apiKey = options.apiKey;
33
34
  }
35
+ else if (!isRealBrowser() && process.env.TINFOIL_API_KEY) {
36
+ openAIOptions.apiKey = process.env.TINFOIL_API_KEY;
37
+ }
34
38
  this.apiKey = openAIOptions.apiKey;
35
39
  this.baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
36
40
  this.enclaveURL = options.enclaveURL || TINFOIL_CONFIG.ENCLAVE_URL;
37
41
  this.configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
38
- // Create the secure client for handling transport security
39
42
  this.secureClient = new SecureClient({
40
43
  baseURL: this.baseURL,
41
44
  enclaveURL: this.enclaveURL,
@@ -65,8 +68,9 @@ export class TinfoilAI {
65
68
  baseURL: this.baseURL,
66
69
  fetch: this.secureClient.fetch,
67
70
  };
68
- // In browser usage, OpenAI SDK typically needs dangerouslyAllowBrowser
69
- clientOptions.dangerouslyAllowBrowser = true;
71
+ if (isRealBrowser() || options.dangerouslyAllowBrowser === true) {
72
+ clientOptions.dangerouslyAllowBrowser = true;
73
+ }
70
74
  return new OpenAI(clientOptions);
71
75
  }
72
76
  async ensureReady() {
@@ -111,6 +115,7 @@ export class TinfoilAI {
111
115
  return createAsyncProxy(this.ensureReady().then((client) => client.beta));
112
116
  }
113
117
  }
118
+ // Namespace declaration merge to add OpenAI types to TinfoilAI
114
119
  (function (TinfoilAI) {
115
120
  TinfoilAI.Chat = OpenAI.Chat;
116
121
  TinfoilAI.Audio = OpenAI.Audio;
@@ -1,5 +1,5 @@
1
- export { TinfoilAI } from "./client.browser";
2
- export { TinfoilAI as default } from "./client.browser";
1
+ export { TinfoilAI } from "./tinfoilai";
2
+ export { TinfoilAI as default } from "./tinfoilai";
3
3
  export * from "./verifier";
4
4
  export * from "./verification-runner";
5
5
  export * from "./ai-sdk-provider";
@@ -16,10 +16,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.SecureClient = exports.default = exports.TinfoilAI = void 0;
18
18
  // Browser-safe entry point: avoids Node built-ins
19
- var client_browser_1 = require("./client.browser");
20
- Object.defineProperty(exports, "TinfoilAI", { enumerable: true, get: function () { return client_browser_1.TinfoilAI; } });
21
- var client_browser_2 = require("./client.browser");
22
- Object.defineProperty(exports, "default", { enumerable: true, get: function () { return client_browser_2.TinfoilAI; } });
19
+ var tinfoilai_1 = require("./tinfoilai");
20
+ Object.defineProperty(exports, "TinfoilAI", { enumerable: true, get: function () { return tinfoilai_1.TinfoilAI; } });
21
+ var tinfoilai_2 = require("./tinfoilai");
22
+ Object.defineProperty(exports, "default", { enumerable: true, get: function () { return tinfoilai_2.TinfoilAI; } });
23
23
  __exportStar(require("./verifier"), exports);
24
24
  __exportStar(require("./verification-runner"), exports);
25
25
  __exportStar(require("./ai-sdk-provider"), exports);
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export { TinfoilAI } from "./client";
2
- export { TinfoilAI as default } from "./client";
1
+ export { TinfoilAI } from "./tinfoilai";
2
+ export { TinfoilAI as default } from "./tinfoilai";
3
3
  export * from "./verifier";
4
4
  export * from "./verification-runner";
5
5
  export * from "./ai-sdk-provider";
package/dist/index.js CHANGED
@@ -16,10 +16,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
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.default = exports.TinfoilAI = void 0;
18
18
  // Re-export the TinfoilAI class
19
- var client_1 = require("./client");
20
- Object.defineProperty(exports, "TinfoilAI", { enumerable: true, get: function () { return client_1.TinfoilAI; } });
21
- var client_2 = require("./client");
22
- Object.defineProperty(exports, "default", { enumerable: true, get: function () { return client_2.TinfoilAI; } });
19
+ var tinfoilai_1 = require("./tinfoilai");
20
+ Object.defineProperty(exports, "TinfoilAI", { enumerable: true, get: function () { return tinfoilai_1.TinfoilAI; } });
21
+ var tinfoilai_2 = require("./tinfoilai");
22
+ Object.defineProperty(exports, "default", { enumerable: true, get: function () { return tinfoilai_2.TinfoilAI; } });
23
23
  // Export verifier and loader
24
24
  __exportStar(require("./verifier"), exports);
25
25
  __exportStar(require("./verification-runner"), exports);
@@ -7,6 +7,7 @@ exports.TinfoilAI = void 0;
7
7
  const openai_1 = __importDefault(require("openai"));
8
8
  const secure_client_1 = require("tinfoil/secure-client");
9
9
  const config_1 = require("./config");
10
+ const env_1 = require("./env");
10
11
  function createAsyncProxy(promise) {
11
12
  return new Proxy({}, {
12
13
  get(target, prop) {
@@ -34,14 +35,16 @@ class TinfoilAI {
34
35
  const openAIOptions = { ...options };
35
36
  // In browser builds, never read secrets from process.env to avoid
36
37
  // leaking credentials into client bundles. Require explicit apiKey.
37
- if (typeof options.apiKey === "string") {
38
+ if (options.apiKey) {
38
39
  openAIOptions.apiKey = options.apiKey;
39
40
  }
41
+ else if (!(0, env_1.isRealBrowser)() && process.env.TINFOIL_API_KEY) {
42
+ openAIOptions.apiKey = process.env.TINFOIL_API_KEY;
43
+ }
40
44
  this.apiKey = openAIOptions.apiKey;
41
45
  this.baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
42
46
  this.enclaveURL = options.enclaveURL || config_1.TINFOIL_CONFIG.ENCLAVE_URL;
43
47
  this.configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
44
- // Create the secure client for handling transport security
45
48
  this.secureClient = new secure_client_1.SecureClient({
46
49
  baseURL: this.baseURL,
47
50
  enclaveURL: this.enclaveURL,
@@ -71,8 +74,9 @@ class TinfoilAI {
71
74
  baseURL: this.baseURL,
72
75
  fetch: this.secureClient.fetch,
73
76
  };
74
- // In browser usage, OpenAI SDK typically needs dangerouslyAllowBrowser
75
- clientOptions.dangerouslyAllowBrowser = true;
77
+ if ((0, env_1.isRealBrowser)() || options.dangerouslyAllowBrowser === true) {
78
+ clientOptions.dangerouslyAllowBrowser = true;
79
+ }
76
80
  return new openai_1.default(clientOptions);
77
81
  }
78
82
  async ensureReady() {
@@ -118,6 +122,7 @@ class TinfoilAI {
118
122
  }
119
123
  }
120
124
  exports.TinfoilAI = TinfoilAI;
125
+ // Namespace declaration merge to add OpenAI types to TinfoilAI
121
126
  (function (TinfoilAI) {
122
127
  TinfoilAI.Chat = openai_1.default.Chat;
123
128
  TinfoilAI.Audio = openai_1.default.Audio;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tinfoil",
3
- "version": "0.8.5",
3
+ "version": "0.8.6",
4
4
  "description": "Tinfoil secure OpenAI client wrapper",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -19,7 +19,6 @@
19
19
  },
20
20
  "./client": {
21
21
  "types": "./dist/client.d.ts",
22
- "browser": "./dist/esm/client.browser.js",
23
22
  "import": "./dist/esm/client.js",
24
23
  "require": "./dist/client.js"
25
24
  },
package/dist/client.d.ts DELETED
@@ -1,123 +0,0 @@
1
- import OpenAI from "openai";
2
- import type { Audio, Beta, Chat, Embeddings, Files, FineTuning, Images, Models, Moderations, Responses } from "openai/resources";
3
- import type { VerificationDocument } from "./verifier";
4
- /**
5
- * TinfoilAI is a wrapper around the OpenAI API client that adds additional
6
- * security measures through enclave verification and an EHBP-secured transport.
7
- *
8
- * It provides:
9
- * - Automatic verification of Tinfoil secure enclaves
10
- * - EHBP-enforced transport security for each request
11
- * - Type-safe access to OpenAI's chat completion APIs
12
- */
13
- interface TinfoilAIOptions {
14
- apiKey?: string;
15
- /** Override the inference API base URL */
16
- baseURL?: string;
17
- /** Override the URL used to fetch the ENCLAVE key (defaults to baseURL) */
18
- enclaveURL?: string;
19
- /** Override the config GitHub repository */
20
- configRepo?: string;
21
- [key: string]: any;
22
- }
23
- export declare class TinfoilAI {
24
- private client?;
25
- private clientPromise;
26
- private readyPromise?;
27
- private configRepo?;
28
- private secureClient;
29
- private verificationDocument?;
30
- apiKey?: string;
31
- baseURL?: string;
32
- enclaveURL?: string;
33
- /**
34
- * Creates a new TinfoilAI instance.
35
- * @param options - Configuration options including apiKey and other OpenAI client options
36
- */
37
- constructor(options?: TinfoilAIOptions);
38
- /**
39
- * Ensures the client is ready to use.
40
- * @returns Promise that resolves when the client is initialized
41
- */
42
- ready(): Promise<void>;
43
- private initClient;
44
- private createOpenAIClient;
45
- /**
46
- * Helper method to ensure the OpenAI client is initialized before use.
47
- * Automatically calls ready() if needed.
48
- * @returns The initialized OpenAI client
49
- * @private
50
- */
51
- private ensureReady;
52
- /**
53
- * Returns the full verification document produced during client initialization.
54
- */
55
- getVerificationDocument(): Promise<VerificationDocument>;
56
- /**
57
- * Access to OpenAI's chat completions API for creating chat conversations.
58
- * Automatically initializes the client if needed.
59
- */
60
- get chat(): Chat;
61
- /**
62
- * Access to OpenAI's files API for managing files used with the API.
63
- * Automatically initializes the client if needed.
64
- */
65
- get files(): Files;
66
- /**
67
- * Access to OpenAI's fine-tuning API for creating and managing fine-tuned models.
68
- * Automatically initializes the client if needed.
69
- */
70
- get fineTuning(): FineTuning;
71
- /**
72
- * Access to OpenAI's image generation and editing API.
73
- * Automatically initializes the client if needed.
74
- */
75
- get images(): Images;
76
- /**
77
- * Access to OpenAI's audio API for speech-to-text and text-to-speech.
78
- * Automatically initializes the client if needed.
79
- */
80
- get audio(): Audio;
81
- /**
82
- * Access to the Responses API, supporting response creation, streaming, and parsing.
83
- * Automatically initializes the client if needed.
84
- */
85
- get responses(): Responses;
86
- /**
87
- * Access to OpenAI's embeddings API for creating vector embeddings of text.
88
- * Automatically initializes the client if needed.
89
- */
90
- get embeddings(): Embeddings;
91
- /**
92
- * Access to OpenAI's models API for listing and managing available models.
93
- * Automatically initializes the client if needed.
94
- */
95
- get models(): Models;
96
- /**
97
- * Access to OpenAI's content moderation API.
98
- * Automatically initializes the client if needed.
99
- */
100
- get moderations(): Moderations;
101
- /**
102
- * Access to OpenAI's beta features.
103
- * Automatically initializes the client if needed.
104
- */
105
- get beta(): Beta;
106
- }
107
- export declare namespace TinfoilAI {
108
- export import Chat = OpenAI.Chat;
109
- export import Audio = OpenAI.Audio;
110
- export import Beta = OpenAI.Beta;
111
- export import Batches = OpenAI.Batches;
112
- export import Completions = OpenAI.Completions;
113
- export import Embeddings = OpenAI.Embeddings;
114
- export import Files = OpenAI.Files;
115
- export import FineTuning = OpenAI.FineTuning;
116
- export import Images = OpenAI.Images;
117
- export import Models = OpenAI.Models;
118
- export import Moderations = OpenAI.Moderations;
119
- export import Responses = OpenAI.Responses;
120
- export import Uploads = OpenAI.Uploads;
121
- export import VectorStores = OpenAI.VectorStores;
122
- }
123
- export {};
package/dist/client.js DELETED
@@ -1,218 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.TinfoilAI = void 0;
7
- const openai_1 = __importDefault(require("openai"));
8
- const secure_client_1 = require("tinfoil/secure-client");
9
- const config_1 = require("./config");
10
- const env_1 = require("./env");
11
- /**
12
- * Creates a proxy that allows property access and method calls on a Promise before it resolves.
13
- * This enables a more ergonomic API where you can chain properties and methods without explicitly
14
- * awaiting the promise first.
15
- *
16
- * @param promise - A Promise that will resolve to an object
17
- * @returns A proxied version of the promised object that allows immediate property/method access
18
- *
19
- * @example
20
- * // Instead of:
21
- * const client = await getClient();
22
- * const result = await client.someProperty.someMethod();
23
- *
24
- * // You can write:
25
- * const client = createAsyncProxy(getClient());
26
- * const result = await client.someProperty.someMethod();
27
- *
28
- * @template T - The type of object that the promise resolves to
29
- */
30
- function createAsyncProxy(promise) {
31
- return new Proxy({}, {
32
- get(target, prop) {
33
- return new Proxy(() => { }, {
34
- get(_, nestedProp) {
35
- return (...args) => promise.then((obj) => {
36
- const value = obj[prop][nestedProp];
37
- return typeof value === "function"
38
- ? value.apply(obj[prop], args)
39
- : value;
40
- });
41
- },
42
- apply(_, __, args) {
43
- return promise.then((obj) => {
44
- const value = obj[prop];
45
- return typeof value === "function" ? value.apply(obj, args) : value;
46
- });
47
- },
48
- });
49
- },
50
- });
51
- }
52
- class TinfoilAI {
53
- /**
54
- * Creates a new TinfoilAI instance.
55
- * @param options - Configuration options including apiKey and other OpenAI client options
56
- */
57
- constructor(options = {}) {
58
- // Set apiKey from options or environment variable
59
- const openAIOptions = { ...options };
60
- if (options.apiKey || process.env.TINFOIL_API_KEY) {
61
- openAIOptions.apiKey = options.apiKey || process.env.TINFOIL_API_KEY;
62
- }
63
- // Store properties for compatibility
64
- this.apiKey = openAIOptions.apiKey;
65
- this.baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
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
- });
73
- this.clientPromise = this.initClient(openAIOptions);
74
- }
75
- /**
76
- * Ensures the client is ready to use.
77
- * @returns Promise that resolves when the client is initialized
78
- */
79
- async ready() {
80
- if (!this.readyPromise) {
81
- this.readyPromise = (async () => {
82
- this.client = await this.clientPromise;
83
- })();
84
- }
85
- return this.readyPromise;
86
- }
87
- async initClient(options) {
88
- return this.createOpenAIClient(options);
89
- }
90
- async createOpenAIClient(options = {}) {
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");
95
- }
96
- const clientOptions = {
97
- ...options,
98
- baseURL: this.baseURL,
99
- fetch: this.secureClient.fetch,
100
- };
101
- // Enable dangerouslyAllowBrowser in Node.js with WASM (which makes OpenAI SDK think we're in a browser)
102
- // OR if explicitly set by the user (for legitimate browser usage like playgrounds)
103
- if (!(0, env_1.isRealBrowser)() || options.dangerouslyAllowBrowser === true) {
104
- clientOptions.dangerouslyAllowBrowser = true;
105
- }
106
- return new openai_1.default(clientOptions);
107
- }
108
- /**
109
- * Helper method to ensure the OpenAI client is initialized before use.
110
- * Automatically calls ready() if needed.
111
- * @returns The initialized OpenAI client
112
- * @private
113
- */
114
- async ensureReady() {
115
- await this.ready();
116
- // We can safely assert this now because ready() must have completed
117
- return this.client;
118
- }
119
- /**
120
- * Returns the full verification document produced during client initialization.
121
- */
122
- async getVerificationDocument() {
123
- await this.ready();
124
- if (!this.verificationDocument) {
125
- throw new Error("Verification document unavailable: client not verified yet");
126
- }
127
- return this.verificationDocument;
128
- }
129
- /**
130
- * Access to OpenAI's chat completions API for creating chat conversations.
131
- * Automatically initializes the client if needed.
132
- */
133
- get chat() {
134
- return createAsyncProxy(this.ensureReady().then((client) => client.chat));
135
- }
136
- /**
137
- * Access to OpenAI's files API for managing files used with the API.
138
- * Automatically initializes the client if needed.
139
- */
140
- get files() {
141
- return createAsyncProxy(this.ensureReady().then((client) => client.files));
142
- }
143
- /**
144
- * Access to OpenAI's fine-tuning API for creating and managing fine-tuned models.
145
- * Automatically initializes the client if needed.
146
- */
147
- get fineTuning() {
148
- return createAsyncProxy(this.ensureReady().then((client) => client.fineTuning));
149
- }
150
- /**
151
- * Access to OpenAI's image generation and editing API.
152
- * Automatically initializes the client if needed.
153
- */
154
- get images() {
155
- return createAsyncProxy(this.ensureReady().then((client) => client.images));
156
- }
157
- /**
158
- * Access to OpenAI's audio API for speech-to-text and text-to-speech.
159
- * Automatically initializes the client if needed.
160
- */
161
- get audio() {
162
- return createAsyncProxy(this.ensureReady().then((client) => client.audio));
163
- }
164
- /**
165
- * Access to the Responses API, supporting response creation, streaming, and parsing.
166
- * Automatically initializes the client if needed.
167
- */
168
- get responses() {
169
- return createAsyncProxy(this.ensureReady().then((client) => client.responses));
170
- }
171
- /**
172
- * Access to OpenAI's embeddings API for creating vector embeddings of text.
173
- * Automatically initializes the client if needed.
174
- */
175
- get embeddings() {
176
- return createAsyncProxy(this.ensureReady().then((client) => client.embeddings));
177
- }
178
- /**
179
- * Access to OpenAI's models API for listing and managing available models.
180
- * Automatically initializes the client if needed.
181
- */
182
- get models() {
183
- return createAsyncProxy(this.ensureReady().then((client) => client.models));
184
- }
185
- /**
186
- * Access to OpenAI's content moderation API.
187
- * Automatically initializes the client if needed.
188
- */
189
- get moderations() {
190
- return createAsyncProxy(this.ensureReady().then((client) => client.moderations));
191
- }
192
- /**
193
- * Access to OpenAI's beta features.
194
- * Automatically initializes the client if needed.
195
- */
196
- get beta() {
197
- return createAsyncProxy(this.ensureReady().then((client) => client.beta));
198
- }
199
- }
200
- exports.TinfoilAI = TinfoilAI;
201
- // Namespace declaration merge to add OpenAI types to TinfoilAI
202
- (function (TinfoilAI) {
203
- // Re-export all OpenAI namespace types
204
- TinfoilAI.Chat = openai_1.default.Chat;
205
- TinfoilAI.Audio = openai_1.default.Audio;
206
- TinfoilAI.Beta = openai_1.default.Beta;
207
- TinfoilAI.Batches = openai_1.default.Batches;
208
- TinfoilAI.Completions = openai_1.default.Completions;
209
- TinfoilAI.Embeddings = openai_1.default.Embeddings;
210
- TinfoilAI.Files = openai_1.default.Files;
211
- TinfoilAI.FineTuning = openai_1.default.FineTuning;
212
- TinfoilAI.Images = openai_1.default.Images;
213
- TinfoilAI.Models = openai_1.default.Models;
214
- TinfoilAI.Moderations = openai_1.default.Moderations;
215
- TinfoilAI.Responses = openai_1.default.Responses;
216
- TinfoilAI.Uploads = openai_1.default.Uploads;
217
- TinfoilAI.VectorStores = openai_1.default.VectorStores;
218
- })(TinfoilAI || (exports.TinfoilAI = TinfoilAI = {}));
@@ -1,123 +0,0 @@
1
- import OpenAI from "openai";
2
- import type { Audio, Beta, Chat, Embeddings, Files, FineTuning, Images, Models, Moderations, Responses } from "openai/resources";
3
- import type { VerificationDocument } from "./verifier";
4
- /**
5
- * TinfoilAI is a wrapper around the OpenAI API client that adds additional
6
- * security measures through enclave verification and an EHBP-secured transport.
7
- *
8
- * It provides:
9
- * - Automatic verification of Tinfoil secure enclaves
10
- * - EHBP-enforced transport security for each request
11
- * - Type-safe access to OpenAI's chat completion APIs
12
- */
13
- interface TinfoilAIOptions {
14
- apiKey?: string;
15
- /** Override the inference API base URL */
16
- baseURL?: string;
17
- /** Override the URL used to fetch the ENCLAVE key (defaults to baseURL) */
18
- enclaveURL?: string;
19
- /** Override the config GitHub repository */
20
- configRepo?: string;
21
- [key: string]: any;
22
- }
23
- export declare class TinfoilAI {
24
- private client?;
25
- private clientPromise;
26
- private readyPromise?;
27
- private configRepo?;
28
- private secureClient;
29
- private verificationDocument?;
30
- apiKey?: string;
31
- baseURL?: string;
32
- enclaveURL?: string;
33
- /**
34
- * Creates a new TinfoilAI instance.
35
- * @param options - Configuration options including apiKey and other OpenAI client options
36
- */
37
- constructor(options?: TinfoilAIOptions);
38
- /**
39
- * Ensures the client is ready to use.
40
- * @returns Promise that resolves when the client is initialized
41
- */
42
- ready(): Promise<void>;
43
- private initClient;
44
- private createOpenAIClient;
45
- /**
46
- * Helper method to ensure the OpenAI client is initialized before use.
47
- * Automatically calls ready() if needed.
48
- * @returns The initialized OpenAI client
49
- * @private
50
- */
51
- private ensureReady;
52
- /**
53
- * Returns the full verification document produced during client initialization.
54
- */
55
- getVerificationDocument(): Promise<VerificationDocument>;
56
- /**
57
- * Access to OpenAI's chat completions API for creating chat conversations.
58
- * Automatically initializes the client if needed.
59
- */
60
- get chat(): Chat;
61
- /**
62
- * Access to OpenAI's files API for managing files used with the API.
63
- * Automatically initializes the client if needed.
64
- */
65
- get files(): Files;
66
- /**
67
- * Access to OpenAI's fine-tuning API for creating and managing fine-tuned models.
68
- * Automatically initializes the client if needed.
69
- */
70
- get fineTuning(): FineTuning;
71
- /**
72
- * Access to OpenAI's image generation and editing API.
73
- * Automatically initializes the client if needed.
74
- */
75
- get images(): Images;
76
- /**
77
- * Access to OpenAI's audio API for speech-to-text and text-to-speech.
78
- * Automatically initializes the client if needed.
79
- */
80
- get audio(): Audio;
81
- /**
82
- * Access to the Responses API, supporting response creation, streaming, and parsing.
83
- * Automatically initializes the client if needed.
84
- */
85
- get responses(): Responses;
86
- /**
87
- * Access to OpenAI's embeddings API for creating vector embeddings of text.
88
- * Automatically initializes the client if needed.
89
- */
90
- get embeddings(): Embeddings;
91
- /**
92
- * Access to OpenAI's models API for listing and managing available models.
93
- * Automatically initializes the client if needed.
94
- */
95
- get models(): Models;
96
- /**
97
- * Access to OpenAI's content moderation API.
98
- * Automatically initializes the client if needed.
99
- */
100
- get moderations(): Moderations;
101
- /**
102
- * Access to OpenAI's beta features.
103
- * Automatically initializes the client if needed.
104
- */
105
- get beta(): Beta;
106
- }
107
- export declare namespace TinfoilAI {
108
- export import Chat = OpenAI.Chat;
109
- export import Audio = OpenAI.Audio;
110
- export import Beta = OpenAI.Beta;
111
- export import Batches = OpenAI.Batches;
112
- export import Completions = OpenAI.Completions;
113
- export import Embeddings = OpenAI.Embeddings;
114
- export import Files = OpenAI.Files;
115
- export import FineTuning = OpenAI.FineTuning;
116
- export import Images = OpenAI.Images;
117
- export import Models = OpenAI.Models;
118
- export import Moderations = OpenAI.Moderations;
119
- export import Responses = OpenAI.Responses;
120
- export import Uploads = OpenAI.Uploads;
121
- export import VectorStores = OpenAI.VectorStores;
122
- }
123
- export {};
@@ -1,211 +0,0 @@
1
- import OpenAI from "openai";
2
- import { SecureClient } from "tinfoil/secure-client";
3
- import { TINFOIL_CONFIG } from "./config";
4
- import { isRealBrowser } from "./env";
5
- /**
6
- * Creates a proxy that allows property access and method calls on a Promise before it resolves.
7
- * This enables a more ergonomic API where you can chain properties and methods without explicitly
8
- * awaiting the promise first.
9
- *
10
- * @param promise - A Promise that will resolve to an object
11
- * @returns A proxied version of the promised object that allows immediate property/method access
12
- *
13
- * @example
14
- * // Instead of:
15
- * const client = await getClient();
16
- * const result = await client.someProperty.someMethod();
17
- *
18
- * // You can write:
19
- * const client = createAsyncProxy(getClient());
20
- * const result = await client.someProperty.someMethod();
21
- *
22
- * @template T - The type of object that the promise resolves to
23
- */
24
- function createAsyncProxy(promise) {
25
- return new Proxy({}, {
26
- get(target, prop) {
27
- return new Proxy(() => { }, {
28
- get(_, nestedProp) {
29
- return (...args) => promise.then((obj) => {
30
- const value = obj[prop][nestedProp];
31
- return typeof value === "function"
32
- ? value.apply(obj[prop], args)
33
- : value;
34
- });
35
- },
36
- apply(_, __, args) {
37
- return promise.then((obj) => {
38
- const value = obj[prop];
39
- return typeof value === "function" ? value.apply(obj, args) : value;
40
- });
41
- },
42
- });
43
- },
44
- });
45
- }
46
- export class TinfoilAI {
47
- /**
48
- * Creates a new TinfoilAI instance.
49
- * @param options - Configuration options including apiKey and other OpenAI client options
50
- */
51
- constructor(options = {}) {
52
- // Set apiKey from options or environment variable
53
- const openAIOptions = { ...options };
54
- if (options.apiKey || process.env.TINFOIL_API_KEY) {
55
- openAIOptions.apiKey = options.apiKey || process.env.TINFOIL_API_KEY;
56
- }
57
- // Store properties for compatibility
58
- this.apiKey = openAIOptions.apiKey;
59
- this.baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
60
- this.enclaveURL = options.enclaveURL || TINFOIL_CONFIG.ENCLAVE_URL;
61
- this.configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
62
- this.secureClient = new SecureClient({
63
- baseURL: this.baseURL,
64
- enclaveURL: this.enclaveURL,
65
- configRepo: this.configRepo,
66
- });
67
- this.clientPromise = this.initClient(openAIOptions);
68
- }
69
- /**
70
- * Ensures the client is ready to use.
71
- * @returns Promise that resolves when the client is initialized
72
- */
73
- async ready() {
74
- if (!this.readyPromise) {
75
- this.readyPromise = (async () => {
76
- this.client = await this.clientPromise;
77
- })();
78
- }
79
- return this.readyPromise;
80
- }
81
- async initClient(options) {
82
- return this.createOpenAIClient(options);
83
- }
84
- async createOpenAIClient(options = {}) {
85
- await this.secureClient.ready();
86
- this.verificationDocument = await this.secureClient.getVerificationDocument();
87
- if (!this.verificationDocument) {
88
- throw new Error("Verification document not available after successful verification");
89
- }
90
- const clientOptions = {
91
- ...options,
92
- baseURL: this.baseURL,
93
- fetch: this.secureClient.fetch,
94
- };
95
- // Enable dangerouslyAllowBrowser in Node.js with WASM (which makes OpenAI SDK think we're in a browser)
96
- // OR if explicitly set by the user (for legitimate browser usage like playgrounds)
97
- if (!isRealBrowser() || options.dangerouslyAllowBrowser === true) {
98
- clientOptions.dangerouslyAllowBrowser = true;
99
- }
100
- return new OpenAI(clientOptions);
101
- }
102
- /**
103
- * Helper method to ensure the OpenAI client is initialized before use.
104
- * Automatically calls ready() if needed.
105
- * @returns The initialized OpenAI client
106
- * @private
107
- */
108
- async ensureReady() {
109
- await this.ready();
110
- // We can safely assert this now because ready() must have completed
111
- return this.client;
112
- }
113
- /**
114
- * Returns the full verification document produced during client initialization.
115
- */
116
- async getVerificationDocument() {
117
- await this.ready();
118
- if (!this.verificationDocument) {
119
- throw new Error("Verification document unavailable: client not verified yet");
120
- }
121
- return this.verificationDocument;
122
- }
123
- /**
124
- * Access to OpenAI's chat completions API for creating chat conversations.
125
- * Automatically initializes the client if needed.
126
- */
127
- get chat() {
128
- return createAsyncProxy(this.ensureReady().then((client) => client.chat));
129
- }
130
- /**
131
- * Access to OpenAI's files API for managing files used with the API.
132
- * Automatically initializes the client if needed.
133
- */
134
- get files() {
135
- return createAsyncProxy(this.ensureReady().then((client) => client.files));
136
- }
137
- /**
138
- * Access to OpenAI's fine-tuning API for creating and managing fine-tuned models.
139
- * Automatically initializes the client if needed.
140
- */
141
- get fineTuning() {
142
- return createAsyncProxy(this.ensureReady().then((client) => client.fineTuning));
143
- }
144
- /**
145
- * Access to OpenAI's image generation and editing API.
146
- * Automatically initializes the client if needed.
147
- */
148
- get images() {
149
- return createAsyncProxy(this.ensureReady().then((client) => client.images));
150
- }
151
- /**
152
- * Access to OpenAI's audio API for speech-to-text and text-to-speech.
153
- * Automatically initializes the client if needed.
154
- */
155
- get audio() {
156
- return createAsyncProxy(this.ensureReady().then((client) => client.audio));
157
- }
158
- /**
159
- * Access to the Responses API, supporting response creation, streaming, and parsing.
160
- * Automatically initializes the client if needed.
161
- */
162
- get responses() {
163
- return createAsyncProxy(this.ensureReady().then((client) => client.responses));
164
- }
165
- /**
166
- * Access to OpenAI's embeddings API for creating vector embeddings of text.
167
- * Automatically initializes the client if needed.
168
- */
169
- get embeddings() {
170
- return createAsyncProxy(this.ensureReady().then((client) => client.embeddings));
171
- }
172
- /**
173
- * Access to OpenAI's models API for listing and managing available models.
174
- * Automatically initializes the client if needed.
175
- */
176
- get models() {
177
- return createAsyncProxy(this.ensureReady().then((client) => client.models));
178
- }
179
- /**
180
- * Access to OpenAI's content moderation API.
181
- * Automatically initializes the client if needed.
182
- */
183
- get moderations() {
184
- return createAsyncProxy(this.ensureReady().then((client) => client.moderations));
185
- }
186
- /**
187
- * Access to OpenAI's beta features.
188
- * Automatically initializes the client if needed.
189
- */
190
- get beta() {
191
- return createAsyncProxy(this.ensureReady().then((client) => client.beta));
192
- }
193
- }
194
- // Namespace declaration merge to add OpenAI types to TinfoilAI
195
- (function (TinfoilAI) {
196
- // Re-export all OpenAI namespace types
197
- TinfoilAI.Chat = OpenAI.Chat;
198
- TinfoilAI.Audio = OpenAI.Audio;
199
- TinfoilAI.Beta = OpenAI.Beta;
200
- TinfoilAI.Batches = OpenAI.Batches;
201
- TinfoilAI.Completions = OpenAI.Completions;
202
- TinfoilAI.Embeddings = OpenAI.Embeddings;
203
- TinfoilAI.Files = OpenAI.Files;
204
- TinfoilAI.FineTuning = OpenAI.FineTuning;
205
- TinfoilAI.Images = OpenAI.Images;
206
- TinfoilAI.Models = OpenAI.Models;
207
- TinfoilAI.Moderations = OpenAI.Moderations;
208
- TinfoilAI.Responses = OpenAI.Responses;
209
- TinfoilAI.Uploads = OpenAI.Uploads;
210
- TinfoilAI.VectorStores = OpenAI.VectorStores;
211
- })(TinfoilAI || (TinfoilAI = {}));