tinfoil 0.4.4 → 0.6.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.
package/README.md CHANGED
@@ -17,9 +17,7 @@ npm install tinfoil
17
17
  import { TinfoilAI } from 'tinfoil';
18
18
 
19
19
  const client = new TinfoilAI({
20
- enclave: 'enclave.example.com', // or use TINFOIL_ENCLAVE env var
21
- repo: 'org/model-repo', // or use TINFOIL_REPO env var
22
- apiKey: 'your-api-key' // or use OPENAI_API_KEY env var
20
+ apiKey: '<YOUR_API_KEY>' // or use TINFOIL_API_KEY env var
23
21
  });
24
22
 
25
23
  // Uses identical method calls as the OpenAI client
@@ -44,9 +42,7 @@ npm install
44
42
  3. Optionally create a `.env` file with your configuration:
45
43
 
46
44
  ```bash
47
- TINFOIL_ENCLAVE=enclave.example.com
48
- TINFOIL_REPO=org/model-repo
49
- OPENAI_API_KEY=your-api-key
45
+ TINFOIL_API_KEY=<YOUR_API_KEY>
50
46
  ```
51
47
 
52
48
  4. Run the example:
@@ -1,9 +1,7 @@
1
1
  /**
2
- * Creates an AI SDK provider with the specified repository and enclave.
2
+ * Creates an AI SDK provider with the specified API key.
3
3
  *
4
- * @param repo - The repository identifier
5
- * @param enclave - The enclave URL/identifier
6
4
  * @param apiKey - The API key for the Tinfoil API
7
5
  * @returns A TinfoilAI instance
8
6
  */
9
- export declare function createTinfoilAI(repo: string, enclave: string, apiKey: string): Promise<import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<string, string, string, string>>;
7
+ export declare function createTinfoilAI(apiKey: string): Promise<import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<string, string, string, string>>;
@@ -3,19 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createTinfoilAI = createTinfoilAI;
4
4
  const openai_compatible_1 = require("@ai-sdk/openai-compatible");
5
5
  const secure_client_1 = require("./secure-client");
6
+ const config_1 = require("./config");
6
7
  const undici_1 = require("undici");
7
8
  const node_tls_1 = require("node:tls");
8
9
  const node_crypto_1 = require("node:crypto");
9
10
  /**
10
- * Creates an AI SDK provider with the specified repository and enclave.
11
+ * Creates an AI SDK provider with the specified API key.
11
12
  *
12
- * @param repo - The repository identifier
13
- * @param enclave - The enclave URL/identifier
14
13
  * @param apiKey - The API key for the Tinfoil API
15
14
  * @returns A TinfoilAI instance
16
15
  */
17
- async function createTinfoilAI(repo, enclave, apiKey) {
18
- const sc = new secure_client_1.SecureClient(enclave, repo);
16
+ async function createTinfoilAI(apiKey) {
17
+ const sc = new secure_client_1.SecureClient();
19
18
  const groundTruth = await sc.verify();
20
19
  const connect = (0, undici_1.buildConnector)({
21
20
  checkServerIdentity: (host, cert) => {
@@ -37,7 +36,7 @@ async function createTinfoilAI(repo, enclave, apiKey) {
37
36
  const agent = new undici_1.Agent({ connect });
38
37
  return (0, openai_compatible_1.createOpenAICompatible)({
39
38
  name: "tinfoil",
40
- baseURL: `https://${enclave}/v1`,
39
+ baseURL: config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL.replace(/\/$/, ''),
41
40
  apiKey: apiKey,
42
41
  fetch: (async (input, init) => {
43
42
  if (!init) {
package/dist/client.d.ts CHANGED
@@ -9,22 +9,17 @@ import type { Chat, Files, FineTuning, Images, Audio, Embeddings, Models, Modera
9
9
  * - Type-safe access to OpenAI's chat completion APIs
10
10
  */
11
11
  interface TinfoilAIOptions {
12
- enclave?: string;
13
- repo?: string;
14
12
  apiKey?: string;
15
13
  [key: string]: any;
16
14
  }
17
15
  export declare class TinfoilAI {
18
16
  private client?;
19
- private enclave;
20
- private repo;
21
17
  private groundTruth?;
22
18
  private clientPromise;
23
19
  private readyPromise?;
24
20
  /**
25
21
  * Creates a new TinfoilAI instance.
26
- * @param options - Configuration options including enclave, repo, apiKey, and other OpenAI client options
27
- * @throws Error if enclave or repo are not provided either in options or environment variables
22
+ * @param options - Configuration options including apiKey and other OpenAI client options
28
23
  */
29
24
  constructor(options?: TinfoilAIOptions);
30
25
  /**
package/dist/client.js CHANGED
@@ -8,6 +8,26 @@ const openai_1 = __importDefault(require("openai"));
8
8
  const crypto_1 = require("crypto");
9
9
  const secure_client_1 = require("./secure-client");
10
10
  const https_1 = __importDefault(require("https"));
11
+ const config_1 = require("./config");
12
+ /**
13
+ * Detects if the code is running in a real browser environment.
14
+ * Returns false for Node.js environments, even with WASM loaded.
15
+ */
16
+ function isRealBrowser() {
17
+ // Check for Node.js-specific globals that wouldn't exist in a real browser
18
+ if (typeof process !== 'undefined' && process.versions && process.versions.node) {
19
+ return false; // Definitely Node.js
20
+ }
21
+ // Check for browser-specific window object AND ensure it's not a Node.js global mock
22
+ if (typeof window !== 'undefined' && typeof window.document !== 'undefined') {
23
+ // Additional check: real browsers have navigator.userAgent
24
+ if (typeof navigator !== 'undefined' && navigator.userAgent) {
25
+ return true; // Likely a real browser
26
+ }
27
+ }
28
+ // Default to safe: assume it's not a browser (Node.js with WASM)
29
+ return false;
30
+ }
11
31
  /**
12
32
  * Creates a proxy that allows property access and method calls on a Promise before it resolves.
13
33
  * This enables a more ergonomic API where you can chain properties and methods without explicitly
@@ -50,21 +70,13 @@ function createAsyncProxy(promise) {
50
70
  class TinfoilAI {
51
71
  /**
52
72
  * Creates a new TinfoilAI instance.
53
- * @param options - Configuration options including enclave, repo, apiKey, and other OpenAI client options
54
- * @throws Error if enclave or repo are not provided either in options or environment variables
73
+ * @param options - Configuration options including apiKey and other OpenAI client options
55
74
  */
56
75
  constructor(options = {}) {
57
- // Extract TinfoilAI-specific options with environment variable fallbacks
58
- this.enclave = options.enclave || process.env.TINFOIL_ENCLAVE || '';
59
- this.repo = options.repo || process.env.TINFOIL_REPO || '';
60
- if (!this.enclave || !this.repo) {
61
- throw new Error('tinfoil: enclave and repo must be specified either in options or via TINFOIL_ENCLAVE and TINFOIL_REPO environment variables');
62
- }
63
- // Prepare OpenAI client options
64
- const { enclave: _, repo: __, ...openAIOptions } = options;
65
76
  // Set apiKey from options or environment variable
66
- if (options.apiKey || process.env.OPENAI_API_KEY) {
67
- openAIOptions.apiKey = options.apiKey || process.env.OPENAI_API_KEY;
77
+ const openAIOptions = { ...options };
78
+ if (options.apiKey || process.env.TINFOIL_API_KEY) {
79
+ openAIOptions.apiKey = options.apiKey || process.env.TINFOIL_API_KEY;
68
80
  }
69
81
  this.clientPromise = this.initClient(openAIOptions);
70
82
  }
@@ -85,7 +97,7 @@ class TinfoilAI {
85
97
  }
86
98
  async createOpenAIClient(options = {}) {
87
99
  // Verify the enclave and get the certificate fingerprint
88
- const secureClient = new secure_client_1.SecureClient(this.enclave, this.repo);
100
+ const secureClient = new secure_client_1.SecureClient();
89
101
  try {
90
102
  this.groundTruth = await secureClient.verify();
91
103
  }
@@ -114,11 +126,18 @@ class TinfoilAI {
114
126
  }
115
127
  });
116
128
  // Create the OpenAI client with our custom configuration
117
- return new openai_1.default({
129
+ // Note: baseURL will need to be determined by the verification process
130
+ const clientOptions = {
118
131
  ...options,
119
- baseURL: `https://${this.enclave}/v1/`,
120
- httpAgent: httpsAgent,
121
- });
132
+ baseURL: config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL,
133
+ };
134
+ // Only enable dangerouslyAllowBrowser when we're NOT in a real browser
135
+ // This prevents API key exposure if code is ever bundled for browser use
136
+ if (!isRealBrowser()) {
137
+ // We're in Node.js with WASM, which makes OpenAI SDK think we're in a browser
138
+ clientOptions.dangerouslyAllowBrowser = true;
139
+ }
140
+ return new openai_1.default(clientOptions);
122
141
  }
123
142
  /**
124
143
  * Helper method to ensure the OpenAI client is initialized before use.
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Configuration constants for the Tinfoil Node SDK
3
+ */
4
+ export declare const TINFOIL_CONFIG: {
5
+ /**
6
+ * The full base URL for the inference API
7
+ */
8
+ readonly INFERENCE_BASE_URL: "https://inference.tinfoil.sh/v1/";
9
+ /**
10
+ * The GitHub repository for the confidential inference proxy
11
+ */
12
+ readonly INFERENCE_PROXY_REPO: "tinfoilsh/confidential-inference-proxy";
13
+ };
package/dist/config.js ADDED
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TINFOIL_CONFIG = void 0;
4
+ /**
5
+ * Configuration constants for the Tinfoil Node SDK
6
+ */
7
+ exports.TINFOIL_CONFIG = {
8
+ /**
9
+ * The full base URL for the inference API
10
+ */
11
+ INFERENCE_BASE_URL: 'https://inference.tinfoil.sh/v1/',
12
+ /**
13
+ * The GitHub repository for the confidential inference proxy
14
+ */
15
+ INFERENCE_PROXY_REPO: 'tinfoilsh/confidential-inference-proxy',
16
+ };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './client';
2
2
  export * from './secure-client';
3
3
  export * from './ai-sdk-provider';
4
+ export * from './config';
package/dist/index.js CHANGED
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./client"), exports);
18
18
  __exportStar(require("./secure-client"), exports);
19
19
  __exportStar(require("./ai-sdk-provider"), exports);
20
+ __exportStar(require("./config"), exports);
@@ -9,11 +9,11 @@ export interface GroundTruth {
9
9
  * SecureClient handles verification of code and runtime measurements using WebAssembly
10
10
  */
11
11
  export declare class SecureClient {
12
- private enclave;
13
- private repo;
14
12
  private static goInstance;
15
13
  private static initializationPromise;
16
- constructor(enclave: string, repo: string);
14
+ private readonly enclave;
15
+ private readonly repo;
16
+ constructor();
17
17
  /**
18
18
  * Static method to initialize WASM module
19
19
  * This starts automatically when the class is loaded
@@ -1,11 +1,9 @@
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.SecureClient = void 0;
7
- const node_fetch_1 = __importDefault(require("node-fetch"));
4
+ const undici_1 = require("undici");
8
5
  const util_1 = require("util");
6
+ const config_1 = require("./config");
9
7
  // Set up browser-like globals that the Go WASM runtime expects
10
8
  const globalThis = global;
11
9
  // Performance API
@@ -62,9 +60,10 @@ require('./wasm-exec.js');
62
60
  * SecureClient handles verification of code and runtime measurements using WebAssembly
63
61
  */
64
62
  class SecureClient {
65
- constructor(enclave, repo) {
66
- this.enclave = enclave;
67
- this.repo = repo;
63
+ constructor() {
64
+ // Values for the Tinfoil inference proxy from config
65
+ this.enclave = new URL(config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL).hostname;
66
+ this.repo = config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
68
67
  }
69
68
  /**
70
69
  * Static method to initialize WASM module
@@ -77,7 +76,7 @@ class SecureClient {
77
76
  SecureClient.initializationPromise = (async () => {
78
77
  try {
79
78
  SecureClient.goInstance = new globalThis.Go();
80
- const wasmResponse = await (0, node_fetch_1.default)('https://tinfoilsh.github.io/verifier-js/tinfoil-verifier.wasm');
79
+ const wasmResponse = await (0, undici_1.fetch)('https://tinfoilsh.github.io/verifier-js/tinfoil-verifier.wasm');
81
80
  const wasmBuffer = await wasmResponse.arrayBuffer();
82
81
  const result = await WebAssembly.instantiate(wasmBuffer, SecureClient.goInstance.importObject);
83
82
  const runPromise = SecureClient.goInstance.run(result.instance);
@@ -117,7 +116,7 @@ class SecureClient {
117
116
  if (typeof globalThis.verifyCode !== 'function' || typeof globalThis.verifyEnclave !== 'function') {
118
117
  throw new Error('WASM functions not available');
119
118
  }
120
- const releaseResponse = await (0, node_fetch_1.default)(`https://api.github.com/repos/${this.repo}/releases/latest`, {
119
+ const releaseResponse = await (0, undici_1.fetch)(`https://api.github.com/repos/${this.repo}/releases/latest`, {
121
120
  headers: {
122
121
  'Accept': 'application/vnd.github.v3+json',
123
122
  'User-Agent': 'tinfoil-node-client'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tinfoil",
3
- "version": "0.4.4",
3
+ "version": "0.6.0",
4
4
  "description": "Tinfoil secure OpenAI client wrapper",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -28,22 +28,21 @@
28
28
  "author": "Tinfoil",
29
29
  "license": "AGPL-3.0-or-later",
30
30
  "dependencies": {
31
- "@ai-sdk/openai-compatible": "^0.2.14",
32
- "ai": "^4.3.15",
33
- "dotenv": "^16.4.7",
34
- "node-fetch": "^2.7.0",
35
- "openai": "^4.87.3",
31
+ "@ai-sdk/openai-compatible": "^1.0.10",
32
+ "ai": "^5.0.19",
33
+ "dotenv": "^17.2.1",
34
+ "openai": "^5.13.1",
36
35
  "ts-node": "^10.9.2",
37
- "undici": "^7.9.0"
36
+ "undici": "^7.14.0"
38
37
  },
39
38
  "devDependencies": {
40
39
  "@types/jest": "^29.5.12",
41
- "@types/node": "^20.11.24",
42
- "@typescript-eslint/eslint-plugin": "^8.26.1",
43
- "@typescript-eslint/parser": "^8.26.1",
40
+ "@types/node": "^24.3.0",
41
+ "@typescript-eslint/eslint-plugin": "^8.40.0",
42
+ "@typescript-eslint/parser": "^8.40.0",
44
43
  "eslint": "^8.57.0",
45
44
  "jest": "^29.7.0",
46
- "ts-jest": "^29.1.2",
47
- "typescript": "~5.5.0"
45
+ "ts-jest": "^29.4.1",
46
+ "typescript": "^5.9.2"
48
47
  }
49
48
  }