tinfoil 1.1.7 → 1.1.9

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 +1 @@
1
- {"version":3,"file":"secure-fetch.js","sourceRoot":"","sources":["../src/secure-fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,wBAAwB,EAAwB,MAAM,2BAA2B,CAAC;AAE3F;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,OAAe,EACf,aAAsB,EACtB,uBAAgC,EAChC,UAAmB;IAEnB,IAAI,aAAa,EAAE,CAAC;QAClB,OAAO,wBAAwB,CAAC,OAAO,EAAE,aAAa,EAAE,UAAU,CAAC,CAAC;IACtE,CAAC;IAED,IAAI,aAAa,EAAE,EAAE,CAAC;QACpB,MAAM,IAAI,kBAAkB,CAC1B,wFAAwF;YACxF,iEAAiE,CAClE,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,uBAAuB,EAAE,CAAC;QAC7B,MAAM,IAAI,kBAAkB,CAC1B,mFAAmF,CACpF,CAAC;IACJ,CAAC;IAED,6EAA6E;IAC7E,MAAM,EAAE,oBAAoB,EAAE,GAAG,MAAM,MAAM,CAAC,uBAAuB,CAAC,CAAC;IACvE,MAAM,WAAW,GAAG,MAAM,oBAAoB,CAAC,OAAO,EAAE,uBAAuB,CAAC,CAAC;IACjF,OAAO;QACL,KAAK,EAAE,WAAW;QAClB,KAAK,CAAC,uBAAuB;YAC3B,MAAM,IAAI,KAAK,CAAC,mEAAmE,CAAC,CAAC;QACvF,CAAC;KACF,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"secure-fetch.js","sourceRoot":"","sources":["../src/secure-fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,wBAAwB,EAAwB,MAAM,2BAA2B,CAAC;AAC3F,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAE7D;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,OAAe,EACf,aAAsB,EACtB,uBAAgC,EAChC,UAAmB,EACnB,kBAA0B,EAAE;IAE5B,IAAI,aAAa,EAAE,CAAC;QAClB,OAAO,wBAAwB,CAAC,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,eAAe,CAAC,CAAC;IACvF,CAAC;IAED,IAAI,aAAa,EAAE,EAAE,CAAC;QACpB,MAAM,IAAI,kBAAkB,CAC1B,wFAAwF;YACxF,iEAAiE,CAClE,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,uBAAuB,EAAE,CAAC;QAC7B,MAAM,IAAI,kBAAkB,CAC1B,mFAAmF,CACpF,CAAC;IACJ,CAAC;IAED,6EAA6E;IAC7E,MAAM,EAAE,oBAAoB,EAAE,GAAG,MAAM,MAAM,CAAC,uBAAuB,CAAC,CAAC;IACvE,MAAM,WAAW,GAAG,MAAM,oBAAoB,CAAC,OAAO,EAAE,uBAAuB,CAAC,CAAC;IACjF,OAAO;QACL,yEAAyE;QACzE,oEAAoE;QACpE,KAAK,EAAE,mBAAmB,CAAC,WAAW,EAAE,OAAO,EAAE,eAAe,CAAC;QACjE,KAAK,CAAC,uBAAuB;YAC3B,MAAM,IAAI,KAAK,CAAC,mEAAmE,CAAC,CAAC;QACvF,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -53,6 +53,15 @@ export interface TinfoilAIOptions {
53
53
  transport?: TransportMode;
54
54
  /** URL to fetch the attestation bundle from. */
55
55
  attestationBundleURL?: string;
56
+ /**
57
+ * Secret scoping the router's prompt cache for this client's requests
58
+ * (e.g. one stable value per end user). Defaults to the
59
+ * TINFOIL_USER_CACHE_SECRET environment variable, otherwise to a generated
60
+ * secret persisted at `~/.tinfoil/user_cache_secret`. Pass an empty string
61
+ * to disable prompt-cache scoping (tenant-wide caching). A
62
+ * `user_cache_secret` field set in a request body always wins.
63
+ */
64
+ userCacheSecret?: string;
56
65
  /** Additional OpenAI client options (passed through to underlying client) */
57
66
  [key: string]: any;
58
67
  }
@@ -123,7 +132,7 @@ export declare class TinfoilAI {
123
132
  * Returns an `OpenAIRealtimeWS` from the OpenAI SDK, connected to the
124
133
  * enclave with the TLS connection pinned to the attested enclave key.
125
134
  *
126
- * Node.js only. Not supported with a proxy `baseURL`.
135
+ * Node.js only. Connections are made directly to the verified enclave.
127
136
  *
128
137
  * @example
129
138
  * ```typescript
@@ -1 +1 @@
1
- {"version":3,"file":"tinfoil-ai.d.ts","sourceRoot":"","sources":["../src/tinfoil-ai.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,KAAK,EACV,KAAK,EACL,IAAI,EACJ,IAAI,EACJ,UAAU,EACV,KAAK,EACL,UAAU,EACV,MAAM,EACN,MAAM,EACN,WAAW,EACX,SAAS,EACV,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAgB,KAAK,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,KAAK,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAE1D,OAAO,KAAK,KAAK,EAAE,MAAM,IAAI,CAAC;AAC9B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AA0B3D;;;;;;;;;;;;;;GAcG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB,0FAA0F;IAC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AI,UAAU,IAAI,UAAU,CAI3B;IAED,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED,IAAI,WAAW,IAAI,WAAW,CAI7B;IAED,IAAI,IAAI,IAAI,IAAI,CAEf;CACF;AAID,yBAAiB,SAAS,CAAC;IACzB,MAAM,QAAQ,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;IACjC,MAAM,QAAQ,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;IACnC,MAAM,QAAQ,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;IACjC,MAAM,QAAQ,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;IACvC,MAAM,QAAQ,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;IAC/C,MAAM,QAAQ,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;IAC7C,MAAM,QAAQ,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;IACnC,MAAM,QAAQ,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;IAC7C,MAAM,QAAQ,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IACrC,MAAM,QAAQ,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IACrC,MAAM,QAAQ,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;IAC/C,MAAM,QAAQ,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;IAC3C,MAAM,QAAQ,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;IACvC,MAAM,QAAQ,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;CAClD"}
@@ -72,6 +72,7 @@ export class TinfoilAI {
72
72
  configRepo: options.configRepo,
73
73
  transport: options.transport,
74
74
  attestationBundleURL: options.attestationBundleURL,
75
+ userCacheSecret: options.userCacheSecret,
75
76
  });
76
77
  }
77
78
  /**
@@ -128,7 +129,7 @@ export class TinfoilAI {
128
129
  * Returns an `OpenAIRealtimeWS` from the OpenAI SDK, connected to the
129
130
  * enclave with the TLS connection pinned to the attested enclave key.
130
131
  *
131
- * Node.js only. Not supported with a proxy `baseURL`.
132
+ * Node.js only. Connections are made directly to the verified enclave.
132
133
  *
133
134
  * @example
134
135
  * ```typescript
@@ -141,7 +142,7 @@ export class TinfoilAI {
141
142
  const pinnedOptions = await this.secureClient.getPinnedWebSocketOptions();
142
143
  const client = await this.ensureReady();
143
144
  const { createPinnedRealtimeWS } = await import("./realtime.js");
144
- return createPinnedRealtimeWS(props, { apiKey: client.apiKey, baseURL: client.baseURL }, pinnedOptions);
145
+ return createPinnedRealtimeWS(props, { apiKey: client.apiKey, baseURL: this.secureClient.getEnclaveBaseURL() }, pinnedOptions);
145
146
  }
146
147
  get chat() {
147
148
  return createAsyncProxy(this.ensureReady().then((client) => client.chat));
@@ -1 +1 @@
1
- {"version":3,"file":"tinfoil-ai.js","sourceRoot":"","sources":["../src/tinfoil-ai.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAa5B,OAAO,EAAE,YAAY,EAAsB,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAA6B,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAIzC,SAAS,gBAAgB,CAAmB,OAAmB;IAC7D,OAAO,IAAI,KAAK,CAAC,EAAO,EAAE;QACxB,GAAG,CAAC,MAAM,EAAE,IAAI;YACd,OAAO,IAAI,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,EAAE;gBACzB,GAAG,CAAC,CAAC,EAAE,UAAU;oBACf,OAAO,CAAC,GAAG,IAAW,EAAE,EAAE,CACxB,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE;wBACnB,MAAM,KAAK,GAAI,GAAW,CAAC,IAAI,CAAC,CAAC,UAAU,CAAC,CAAC;wBAC7C,OAAO,OAAO,KAAK,KAAK,UAAU;4BAChC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAE,GAAW,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC;4BACvC,CA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EAAE,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;IAC5E,CAAC;CACF;AAED,+DAA+D;AAC/D,2DAA2D;AAC3D,WAAiB,SAAS;IACV,cAAI,GAAG,MAAM,CAAC,IAAI,CAAC;IACnB,eAAK,GAAG,MAAM,CAAC,KAAK,CAAC;IACrB,cAAI,GAAG,MAAM,CAAC,IAAI,CAAC;IACnB,iBAAO,GAAG,MAAM,CAAC,OAAO,CAAC;IACzB,qBAAW,GAAG,MAAM,CAAC,WAAW,CAAC;IACjC,oBAAU,GAAG,MAAM,CAAC,UAAU,CAAC;IAC/B,eAAK,GAAG,MAAM,CAAC,KAAK,CAAC;IACrB,oBAAU,GAAG,MAAM,CAAC,UAAU,CAAC;IAC/B,gBAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IACvB,gBAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IACvB,qBAAW,GAAG,MAAM,CAAC,WAAW,CAAC;IACjC,mBAAS,GAAG,MAAM,CAAC,SAAS,CAAC;IAC7B,iBAAO,GAAG,MAAM,CAAC,OAAO,CAAC;IACzB,sBAAY,GAAG,MAAM,CAAC,YAAY,CAAC;AACnD,CAAC,EAfgB,SAAS,KAAT,SAAS,QAezB"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Browser build of the user-cache-secret store. Browsers have no filesystem
3
+ * to persist the secret in, so resolution always falls back to the
4
+ * process-lifetime in-memory secret.
5
+ */
6
+ export declare function loadOrPersistUserCacheSecret(_generateSecret: () => string): string | null;
7
+ //# sourceMappingURL=user-cache-secret-store.browser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user-cache-secret-store.browser.d.ts","sourceRoot":"","sources":["../src/user-cache-secret-store.browser.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,wBAAgB,4BAA4B,CAAC,eAAe,EAAE,MAAM,MAAM,GAAG,MAAM,GAAG,IAAI,CAEzF"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Browser build of the user-cache-secret store. Browsers have no filesystem
3
+ * to persist the secret in, so resolution always falls back to the
4
+ * process-lifetime in-memory secret.
5
+ */
6
+ export function loadOrPersistUserCacheSecret(_generateSecret) {
7
+ return null;
8
+ }
9
+ //# sourceMappingURL=user-cache-secret-store.browser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user-cache-secret-store.browser.js","sourceRoot":"","sources":["../src/user-cache-secret-store.browser.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,UAAU,4BAA4B,CAAC,eAA6B;IACxE,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Node.js persistence for the user cache secret. The other Tinfoil SDKs use
3
+ * the same file, so one machine gets one cache namespace across tools. The
4
+ * browser build swaps this module for a stub (see package.json's `browser`
5
+ * field) — browsers have no filesystem, so resolution falls back to the
6
+ * process-lifetime in-memory secret.
7
+ */
8
+ /** Directory under the home directory holding the secret file (mode 0700). */
9
+ export declare const USER_CACHE_SECRET_DIR_NAME = ".tinfoil";
10
+ /** Secret file name under the directory (mode 0600). */
11
+ export declare const USER_CACHE_SECRET_FILE_NAME = "user_cache_secret";
12
+ /**
13
+ * Returns the secret persisted under the user's home directory, generating
14
+ * (via `generateSecret`) and persisting one on first use. Returns null when
15
+ * the home directory is unavailable or unwritable — the caller falls back to
16
+ * a process-lifetime in-memory secret. Never throws.
17
+ */
18
+ export declare function loadOrPersistUserCacheSecret(generateSecret: () => string): string | null;
19
+ //# sourceMappingURL=user-cache-secret-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user-cache-secret-store.d.ts","sourceRoot":"","sources":["../src/user-cache-secret-store.ts"],"names":[],"mappings":"AAgBA;;;;;;GAMG;AAEH,8EAA8E;AAC9E,eAAO,MAAM,0BAA0B,aAAa,CAAC;AAErD,wDAAwD;AACxD,eAAO,MAAM,2BAA2B,sBAAsB,CAAC;AAiE/D;;;;;GAKG;AACH,wBAAgB,4BAA4B,CAAC,cAAc,EAAE,MAAM,MAAM,GAAG,MAAM,GAAG,IAAI,CAgFxF"}
@@ -0,0 +1,161 @@
1
+ import { closeSync, constants, fstatSync, linkSync, lstatSync, mkdirSync, openSync, readFileSync, unlinkSync, writeFileSync, } from "node:fs";
2
+ import { randomUUID } from "node:crypto";
3
+ import { homedir } from "node:os";
4
+ import { join } from "node:path";
5
+ /**
6
+ * Node.js persistence for the user cache secret. The other Tinfoil SDKs use
7
+ * the same file, so one machine gets one cache namespace across tools. The
8
+ * browser build swaps this module for a stub (see package.json's `browser`
9
+ * field) — browsers have no filesystem, so resolution falls back to the
10
+ * process-lifetime in-memory secret.
11
+ */
12
+ /** Directory under the home directory holding the secret file (mode 0700). */
13
+ export const USER_CACHE_SECRET_DIR_NAME = ".tinfoil";
14
+ /** Secret file name under the directory (mode 0600). */
15
+ export const USER_CACHE_SECRET_FILE_NAME = "user_cache_secret";
16
+ const HAS_POSIX_PERMISSIONS = process.platform !== "win32";
17
+ function posixReadFlags() {
18
+ return constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK;
19
+ }
20
+ function trimPersistenceWhitespace(value) {
21
+ return value.replace(/^\p{White_Space}+|\p{White_Space}+$/gu, "");
22
+ }
23
+ function isExpectedWindowsPath(path, expectedType) {
24
+ try {
25
+ const stats = lstatSync(path);
26
+ if (stats.isSymbolicLink()) {
27
+ return false;
28
+ }
29
+ return expectedType === "directory" ? stats.isDirectory() : stats.isFile();
30
+ }
31
+ catch (err) {
32
+ return err?.code === "ENOENT" ? undefined : false;
33
+ }
34
+ }
35
+ /** Reads the secret file, distinguishing a missing file from unusable state. */
36
+ function readSecretFile(path) {
37
+ let fd;
38
+ try {
39
+ if (!HAS_POSIX_PERMISSIONS) {
40
+ const pathState = isExpectedWindowsPath(path, "file");
41
+ if (pathState !== true) {
42
+ return pathState === undefined ? undefined : null;
43
+ }
44
+ }
45
+ fd = openSync(path, HAS_POSIX_PERMISSIONS ? posixReadFlags() : "r");
46
+ if (HAS_POSIX_PERMISSIONS) {
47
+ if (!fstatSync(fd).isFile()) {
48
+ return null;
49
+ }
50
+ }
51
+ let secret;
52
+ try {
53
+ secret = trimPersistenceWhitespace(new TextDecoder("utf-8", { fatal: true }).decode(readFileSync(fd)));
54
+ }
55
+ catch {
56
+ return null;
57
+ }
58
+ return secret === "" ? null : secret;
59
+ }
60
+ catch (err) {
61
+ return err?.code === "ENOENT" ? undefined : null;
62
+ }
63
+ finally {
64
+ if (fd !== undefined) {
65
+ try {
66
+ closeSync(fd);
67
+ }
68
+ catch {
69
+ // Best-effort cleanup after the read has completed.
70
+ }
71
+ }
72
+ }
73
+ }
74
+ /**
75
+ * Returns the secret persisted under the user's home directory, generating
76
+ * (via `generateSecret`) and persisting one on first use. Returns null when
77
+ * the home directory is unavailable or unwritable — the caller falls back to
78
+ * a process-lifetime in-memory secret. Never throws.
79
+ */
80
+ export function loadOrPersistUserCacheSecret(generateSecret) {
81
+ let home;
82
+ try {
83
+ home = homedir();
84
+ }
85
+ catch {
86
+ return null;
87
+ }
88
+ if (!home) {
89
+ return null;
90
+ }
91
+ const dir = join(home, USER_CACHE_SECRET_DIR_NAME);
92
+ const path = join(dir, USER_CACHE_SECRET_FILE_NAME);
93
+ try {
94
+ if (!HAS_POSIX_PERMISSIONS && isExpectedWindowsPath(dir, "directory") === false) {
95
+ return null;
96
+ }
97
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
98
+ if (HAS_POSIX_PERMISSIONS) {
99
+ let dirFd;
100
+ try {
101
+ dirFd = openSync(dir, posixReadFlags() | constants.O_DIRECTORY);
102
+ if (!fstatSync(dirFd).isDirectory()) {
103
+ return null;
104
+ }
105
+ }
106
+ finally {
107
+ if (dirFd !== undefined) {
108
+ closeSync(dirFd);
109
+ }
110
+ }
111
+ }
112
+ else if (isExpectedWindowsPath(dir, "directory") !== true) {
113
+ return null;
114
+ }
115
+ }
116
+ catch {
117
+ return null;
118
+ }
119
+ const existing = readSecretFile(path);
120
+ if (existing === null) {
121
+ return null;
122
+ }
123
+ if (existing !== undefined) {
124
+ return existing;
125
+ }
126
+ const secret = generateSecret();
127
+ if (secret === "") {
128
+ // CSPRNG failure (the generator already warned): tenant-wide caching,
129
+ // nothing worth persisting.
130
+ return "";
131
+ }
132
+ const candidatePath = join(dir, `${USER_CACHE_SECRET_FILE_NAME}.${process.pid}.${randomUUID()}.tmp`);
133
+ try {
134
+ // A complete temporary file is linked directly to the destination.
135
+ // Hard-link creation is atomic, so concurrent first users either win or
136
+ // adopt the complete destination created by another process.
137
+ writeFileSync(candidatePath, secret, { encoding: "utf8", flag: "wx", mode: 0o600 });
138
+ try {
139
+ linkSync(candidatePath, path);
140
+ }
141
+ catch (err) {
142
+ if (err?.code !== "EEXIST") {
143
+ return null;
144
+ }
145
+ }
146
+ const persisted = readSecretFile(path);
147
+ return persisted ?? null;
148
+ }
149
+ catch {
150
+ return null;
151
+ }
152
+ finally {
153
+ try {
154
+ unlinkSync(candidatePath);
155
+ }
156
+ catch {
157
+ // Best-effort cleanup: the candidate is never the persisted file.
158
+ }
159
+ }
160
+ }
161
+ //# sourceMappingURL=user-cache-secret-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user-cache-secret-store.js","sourceRoot":"","sources":["../src/user-cache-secret-store.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,SAAS,EACT,SAAS,EACT,QAAQ,EACR,SAAS,EACT,SAAS,EACT,QAAQ,EACR,YAAY,EACZ,UAAU,EACV,aAAa,GACd,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC;;;;;;GAMG;AAEH,8EAA8E;AAC9E,MAAM,CAAC,MAAM,0BAA0B,GAAG,UAAU,CAAC;AAErD,wDAAw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E,4BAA4B;QAC5B,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,MAAM,aAAa,GAAG,IAAI,CACxB,GAAG,EACH,GAAG,2BAA2B,IAAI,OAAO,CAAC,GAAG,IAAI,UAAU,EAAE,MAAM,CACpE,CAAC;IAEF,IAAI,CAAC;QACH,mEAAmE;QACnE,wEAAwE;QACxE,6DAA6D;QAC7D,aAAa,CAAC,aAAa,EAAE,MAAM,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QACpF,IAAI,CAAC;YACH,QAAQ,CAAC,aAAa,EAAE,IAAI,CAAC,CAAC;QAChC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAK,GAAgC,EAAE,IAAI,KAAK,QAAQ,EAAE,CAAC;gBACzD,OAAO,IAAI,CAAC;YACd,CAAC;QACH,CAAC;QACD,MAAM,SAAS,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;QACvC,OAAO,SAAS,IAAI,IAAI,CAAC;IAC3B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;YAAS,CAAC;QACT,IAAI,CAAC;YACH,UAAU,CAAC,aAAa,CAAC,CAAC;QAC5B,CAAC;QAAC,MAAM,CAAC;YACP,kEAAkE;QACpE,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,67 @@
1
+ /**
2
+ * `user_cache_secret` provisions the per-user prompt-cache secret defined by
3
+ * the secure prompt caching contract. The router derives the request's
4
+ * prefix-cache namespace from it: requests carrying the same secret (under
5
+ * the same API identity) share cached prompt prefixes, requests carrying
6
+ * different secrets cannot observe each other's cache timing. The secret
7
+ * itself is stripped by the router and never reaches the model.
8
+ *
9
+ * Resolution order, mirroring the other Tinfoil clients:
10
+ *
11
+ * 1. an explicit per-request `user_cache_secret` field in the body (never
12
+ * overwritten here),
13
+ * 2. the `userCacheSecret` client option,
14
+ * 3. the TINFOIL_USER_CACHE_SECRET environment variable,
15
+ * 4. a generated secret persisted at ~/.tinfoil/user_cache_secret (0600),
16
+ * shared with the other Tinfoil SDKs on the same machine. Runtimes
17
+ * without a filesystem (browsers, edge runtimes) fall back to a
18
+ * process-lifetime in-memory secret.
19
+ *
20
+ * Injection happens inside the secure transport — before the EHBP transport
21
+ * encrypts the body, or on the way into the TLS connection pinned to the
22
+ * attested key — so the secret is only ever visible to the verified enclave.
23
+ */
24
+ /**
25
+ * The router-only request-body field. A non-empty string scopes the prompt
26
+ * cache to that secret; an absent or empty value leaves the request in the
27
+ * tenant-wide namespace.
28
+ */
29
+ export declare const USER_CACHE_SECRET_FIELD = "user_cache_secret";
30
+ /**
31
+ * Provisions the secret via the environment. Setting it to an empty string
32
+ * disables generation entirely (tenant-wide caching), which is the right
33
+ * call for pooled multi-user deployments that would otherwise mint a fresh
34
+ * namespace per container.
35
+ */
36
+ export declare const USER_CACHE_SECRET_ENV = "TINFOIL_USER_CACHE_SECRET";
37
+ /**
38
+ * Resolves the client-level secret: the explicit option wins (an explicit
39
+ * empty string disables provisioning), then the environment, then the
40
+ * persisted (or generated) secret. An empty result means injection is
41
+ * disabled. Never throws.
42
+ */
43
+ export declare function resolveUserCacheSecret(explicit?: string): Promise<string>;
44
+ /**
45
+ * Adds the field to a JSON-object body. Returns null — forward the original
46
+ * body untouched — for non-object bodies, trailing data, or a body that
47
+ * already carries the field (an explicit per-request value, including an
48
+ * explicit empty string, always wins).
49
+ */
50
+ export declare function injectUserCacheSecret(raw: string, secret: string): string | null;
51
+ /**
52
+ * Injects the client-level secret into an eligible request: a POST with a
53
+ * JSON-object body to one of the supported endpoints. Anything else — other
54
+ * endpoints, non-object bodies, or a body that already carries the field —
55
+ * passes through untouched. Never throws. The caller
56
+ * runs this inside the sealing boundary, so retries and redirects below the
57
+ * transport replay the injected body, not the caller's original.
58
+ */
59
+ export declare function injectUserCacheSecretIntoRequestInit(init: RequestInit | undefined, pathname: string, secret: string): Promise<RequestInit | undefined>;
60
+ /**
61
+ * Wraps a fetch function so eligible request bodies carry the secret. The
62
+ * wrapper sits directly above the sealing fetch (the TLS connection pinned
63
+ * to the attested key), so the injected field never travels outside the
64
+ * protected channel. An empty secret disables injection entirely.
65
+ */
66
+ export declare function withUserCacheSecret(inner: typeof fetch, baseURL: string, secret: string): typeof fetch;
67
+ //# sourceMappingURL=user-cache-secret.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user-cache-secret.d.ts","sourceRoot":"","sources":["../src/user-cache-secret.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,sBAAsB,CAAC;AAE3D;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,8BAA8B,CAAC;AAejE;;;;;GAKG;AACH,wBAAsB,sBAAsB,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAW/E;AAgED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAyBhF;AAwCD;;;;;;;GAOG;AACH,wBAAsB,oCAAoC,CACxD,IAAI,EAAE,WAAW,GAAG,SAAS,EAC7B,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CAuBlC;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,OAAO,KAAK,EACnB,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,GACb,OAAO,KAAK,CAsBd"}
@@ -0,0 +1,259 @@
1
+ import { isRealBrowser } from "./env.js";
2
+ /**
3
+ * `user_cache_secret` provisions the per-user prompt-cache secret defined by
4
+ * the secure prompt caching contract. The router derives the request's
5
+ * prefix-cache namespace from it: requests carrying the same secret (under
6
+ * the same API identity) share cached prompt prefixes, requests carrying
7
+ * different secrets cannot observe each other's cache timing. The secret
8
+ * itself is stripped by the router and never reaches the model.
9
+ *
10
+ * Resolution order, mirroring the other Tinfoil clients:
11
+ *
12
+ * 1. an explicit per-request `user_cache_secret` field in the body (never
13
+ * overwritten here),
14
+ * 2. the `userCacheSecret` client option,
15
+ * 3. the TINFOIL_USER_CACHE_SECRET environment variable,
16
+ * 4. a generated secret persisted at ~/.tinfoil/user_cache_secret (0600),
17
+ * shared with the other Tinfoil SDKs on the same machine. Runtimes
18
+ * without a filesystem (browsers, edge runtimes) fall back to a
19
+ * process-lifetime in-memory secret.
20
+ *
21
+ * Injection happens inside the secure transport — before the EHBP transport
22
+ * encrypts the body, or on the way into the TLS connection pinned to the
23
+ * attested key — so the secret is only ever visible to the verified enclave.
24
+ */
25
+ /**
26
+ * The router-only request-body field. A non-empty string scopes the prompt
27
+ * cache to that secret; an absent or empty value leaves the request in the
28
+ * tenant-wide namespace.
29
+ */
30
+ export const USER_CACHE_SECRET_FIELD = "user_cache_secret";
31
+ /**
32
+ * Provisions the secret via the environment. Setting it to an empty string
33
+ * disables generation entirely (tenant-wide caching), which is the right
34
+ * call for pooled multi-user deployments that would otherwise mint a fresh
35
+ * namespace per container.
36
+ */
37
+ export const USER_CACHE_SECRET_ENV = "TINFOIL_USER_CACHE_SECRET";
38
+ /**
39
+ * The OpenAI-compatible endpoints whose bodies carry the field. Matched by
40
+ * path suffix with no /v1 prefix required, so custom base URLs
41
+ * (path-prefixed proxies or /v1-less roots) still qualify. Other endpoints
42
+ * (embeddings, audio, files) are excluded: their engines do not prefix-cache
43
+ * and may reject unknown fields.
44
+ */
45
+ const USER_CACHE_SECRET_PATHS = [
46
+ "/chat/completions",
47
+ "/completions",
48
+ "/responses",
49
+ ];
50
+ /**
51
+ * Resolves the client-level secret: the explicit option wins (an explicit
52
+ * empty string disables provisioning), then the environment, then the
53
+ * persisted (or generated) secret. An empty result means injection is
54
+ * disabled. Never throws.
55
+ */
56
+ export async function resolveUserCacheSecret(explicit) {
57
+ if (explicit !== undefined) {
58
+ return explicit;
59
+ }
60
+ // A set-but-empty variable counts as set: it disables generation, which is
61
+ // what pooled multi-user deployments want. Browser bundles have no process.
62
+ const env = typeof process !== "undefined" ? process.env?.[USER_CACHE_SECRET_ENV] : undefined;
63
+ if (env !== undefined) {
64
+ return env;
65
+ }
66
+ return loadOrGenerateUserCacheSecret();
67
+ }
68
+ /** Returns a fresh 256-bit random secret, hex-encoded. */
69
+ function newUserCacheSecret() {
70
+ try {
71
+ const bytes = new Uint8Array(32);
72
+ crypto.getRandomValues(bytes);
73
+ return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
74
+ }
75
+ catch {
76
+ // Never fall back to a weak secret: no secret means tenant-wide caching,
77
+ // which is safe.
78
+ console.warn("[tinfoil] could not generate a user cache secret; requests stay in the tenant-wide cache namespace");
79
+ return "";
80
+ }
81
+ }
82
+ /**
83
+ * The process-lifetime fallback for when the secret cannot be persisted. An
84
+ * unpersisted secret still isolates this process's cache namespace, but
85
+ * continuity is lost on restart — like a session ID, it silently resets the
86
+ * namespace every deploy — so the fallback warns once per process.
87
+ */
88
+ let ephemeralUserCacheSecret;
89
+ function getEphemeralUserCacheSecret() {
90
+ if (ephemeralUserCacheSecret === undefined) {
91
+ ephemeralUserCacheSecret = newUserCacheSecret();
92
+ if (ephemeralUserCacheSecret !== "") {
93
+ console.warn("[tinfoil] could not persist the user cache secret; using an in-memory secret, " +
94
+ "so prompt-cache continuity resets when this process exits " +
95
+ `(set ${USER_CACHE_SECRET_ENV} or the userCacheSecret option to pin one)`);
96
+ }
97
+ }
98
+ return ephemeralUserCacheSecret;
99
+ }
100
+ /**
101
+ * Returns the secret persisted under the user's home directory, generating
102
+ * and persisting one on first use. Without a usable filesystem (browsers,
103
+ * edge runtimes, an unwritable home directory) it falls back to the
104
+ * process-lifetime in-memory secret.
105
+ */
106
+ async function loadOrGenerateUserCacheSecret() {
107
+ if (!isRealBrowser()) {
108
+ try {
109
+ // Dynamic import to keep Node.js fs/os modules out of browser bundles;
110
+ // package.json's browser field maps the store to a stub, the same way
111
+ // pinned-tls-fetch is gated.
112
+ const { loadOrPersistUserCacheSecret } = await import("./user-cache-secret-store.js");
113
+ const secret = loadOrPersistUserCacheSecret(newUserCacheSecret);
114
+ if (secret !== null) {
115
+ return secret;
116
+ }
117
+ }
118
+ catch {
119
+ // No usable filesystem in this runtime: fall through.
120
+ }
121
+ }
122
+ return getEphemeralUserCacheSecret();
123
+ }
124
+ /**
125
+ * Adds the field to a JSON-object body. Returns null — forward the original
126
+ * body untouched — for non-object bodies, trailing data, or a body that
127
+ * already carries the field (an explicit per-request value, including an
128
+ * explicit empty string, always wins).
129
+ */
130
+ export function injectUserCacheSecret(raw, secret) {
131
+ let parsed;
132
+ try {
133
+ // JSON.parse enforces exactly the framing we need: a single
134
+ // value followed by nothing but whitespace. Trailing data ('{...}}',
135
+ // '{...} garbage') throws, so a body the server would reject is forwarded
136
+ // as-is instead of being quietly rewritten into one it accepts.
137
+ parsed = JSON.parse(raw);
138
+ }
139
+ catch {
140
+ return null;
141
+ }
142
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
143
+ return null;
144
+ }
145
+ if (Object.prototype.hasOwnProperty.call(parsed, USER_CACHE_SECRET_FIELD)) {
146
+ return null;
147
+ }
148
+ // Splice the field into the original text instead of re-serializing the
149
+ // parsed object: parsing round-trips numbers through float64, which would
150
+ // corrupt int64-range values such as `"seed": 9007199254740993`. The last
151
+ // '}' is the object's closing brace — only whitespace may follow it.
152
+ const end = raw.lastIndexOf("}");
153
+ const field = `${JSON.stringify(USER_CACHE_SECRET_FIELD)}:${JSON.stringify(secret)}`;
154
+ const separator = Object.keys(parsed).length > 0 ? "," : "";
155
+ return raw.slice(0, end) + separator + field + raw.slice(end);
156
+ }
157
+ /**
158
+ * Decodes supported replayable in-memory bodies. Other representations are
159
+ * forwarded unchanged, so callers using them must provide the field.
160
+ */
161
+ function bodyText(body) {
162
+ if (typeof body === "string") {
163
+ return body;
164
+ }
165
+ if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) {
166
+ try {
167
+ return new TextDecoder("utf-8", { fatal: true }).decode(body);
168
+ }
169
+ catch {
170
+ return null;
171
+ }
172
+ }
173
+ return null;
174
+ }
175
+ /**
176
+ * Rewrites a caller-supplied Content-Length header to describe the injected
177
+ * body. Fetch implementations compute the length themselves when the header
178
+ * is absent, so it is only touched when the caller set one.
179
+ */
180
+ function headersWithUpdatedBodyMetadata(headers, body) {
181
+ if (headers === undefined) {
182
+ return headers;
183
+ }
184
+ const updated = new Headers(headers);
185
+ if (updated.has("content-length")) {
186
+ updated.set("content-length", String(new TextEncoder().encode(body).byteLength));
187
+ return updated;
188
+ }
189
+ return headers;
190
+ }
191
+ /**
192
+ * Injects the client-level secret into an eligible request: a POST with a
193
+ * JSON-object body to one of the supported endpoints. Anything else — other
194
+ * endpoints, non-object bodies, or a body that already carries the field —
195
+ * passes through untouched. Never throws. The caller
196
+ * runs this inside the sealing boundary, so retries and redirects below the
197
+ * transport replay the injected body, not the caller's original.
198
+ */
199
+ export async function injectUserCacheSecretIntoRequestInit(init, pathname, secret) {
200
+ if (secret === "" || init === undefined) {
201
+ return init;
202
+ }
203
+ if ((init.method ?? "GET").toUpperCase() !== "POST") {
204
+ return init;
205
+ }
206
+ if (!USER_CACHE_SECRET_PATHS.some((path) => pathname.endsWith(path))) {
207
+ return init;
208
+ }
209
+ const raw = bodyText(init.body);
210
+ if (raw === null || raw === "") {
211
+ return init;
212
+ }
213
+ const injected = injectUserCacheSecret(raw, secret);
214
+ if (injected === null) {
215
+ return init;
216
+ }
217
+ return {
218
+ ...init,
219
+ body: injected,
220
+ headers: headersWithUpdatedBodyMetadata(init.headers, injected),
221
+ };
222
+ }
223
+ /**
224
+ * Wraps a fetch function so eligible request bodies carry the secret. The
225
+ * wrapper sits directly above the sealing fetch (the TLS connection pinned
226
+ * to the attested key), so the injected field never travels outside the
227
+ * protected channel. An empty secret disables injection entirely.
228
+ */
229
+ export function withUserCacheSecret(inner, baseURL, secret) {
230
+ if (secret === "") {
231
+ return inner;
232
+ }
233
+ return (async (input, init) => {
234
+ let pathname;
235
+ try {
236
+ pathname = requestPathname(input, baseURL);
237
+ }
238
+ catch {
239
+ // An unresolvable URL is the inner fetch's error to raise.
240
+ return inner(input, init);
241
+ }
242
+ const normalized = input instanceof Request && init?.body !== undefined
243
+ ? { method: input.method, headers: input.headers, ...init }
244
+ : init;
245
+ const injected = await injectUserCacheSecretIntoRequestInit(normalized, pathname, secret);
246
+ return inner(input, injected);
247
+ });
248
+ }
249
+ /** Resolves the request path the same way the pinned TLS fetch does. */
250
+ function requestPathname(input, baseURL) {
251
+ if (typeof input === "string") {
252
+ return new URL(input, baseURL).pathname;
253
+ }
254
+ if (input instanceof URL) {
255
+ return input.pathname;
256
+ }
257
+ return new URL(input.url, baseURL).pathname;
258
+ }
259
+ //# sourceMappingURL=user-cache-secret.js.map