tinfoil 1.4.0 → 2.0.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) hide show
  1. package/README.md +76 -34
  2. package/dist/ai-sdk.d.ts +12 -0
  3. package/dist/ai-sdk.d.ts.map +1 -0
  4. package/dist/ai-sdk.js +12 -0
  5. package/dist/ai-sdk.js.map +1 -0
  6. package/dist/enclave-client.d.ts +64 -0
  7. package/dist/enclave-client.d.ts.map +1 -0
  8. package/dist/enclave-client.js +196 -0
  9. package/dist/enclave-client.js.map +1 -0
  10. package/dist/errors.d.ts +68 -0
  11. package/dist/errors.d.ts.map +1 -0
  12. package/dist/errors.js +84 -0
  13. package/dist/errors.js.map +1 -0
  14. package/dist/gateway-client.d.ts +71 -0
  15. package/dist/gateway-client.d.ts.map +1 -0
  16. package/dist/gateway-client.js +145 -0
  17. package/dist/gateway-client.js.map +1 -0
  18. package/dist/http.d.ts +13 -0
  19. package/dist/http.d.ts.map +1 -0
  20. package/dist/http.js +12 -0
  21. package/dist/http.js.map +1 -0
  22. package/dist/index.browser.d.ts +17 -12
  23. package/dist/index.browser.d.ts.map +1 -1
  24. package/dist/index.browser.js +17 -12
  25. package/dist/index.browser.js.map +1 -1
  26. package/dist/index.d.ts +2 -12
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +2 -11
  29. package/dist/index.js.map +1 -1
  30. package/dist/internal/ai-sdk.d.ts +58 -0
  31. package/dist/internal/ai-sdk.d.ts.map +1 -0
  32. package/dist/internal/ai-sdk.js +73 -0
  33. package/dist/internal/ai-sdk.js.map +1 -0
  34. package/dist/internal/body.js +69 -0
  35. package/dist/internal/body.js.map +1 -0
  36. package/dist/internal/cachesecret.js +281 -0
  37. package/dist/internal/cachesecret.js.map +1 -0
  38. package/dist/internal/credential.js +35 -0
  39. package/dist/internal/credential.js.map +1 -0
  40. package/dist/internal/dispose.d.ts +18 -0
  41. package/dist/internal/dispose.d.ts.map +1 -0
  42. package/dist/internal/dispose.js +20 -0
  43. package/dist/internal/dispose.js.map +1 -0
  44. package/dist/internal/enclave.js +214 -0
  45. package/dist/internal/enclave.js.map +1 -0
  46. package/dist/internal/fetch.js +136 -0
  47. package/dist/internal/fetch.js.map +1 -0
  48. package/dist/internal/gateway/catalog.js +192 -0
  49. package/dist/internal/gateway/catalog.js.map +1 -0
  50. package/dist/internal/gateway/policy.js +114 -0
  51. package/dist/internal/gateway/policy.js.map +1 -0
  52. package/dist/internal/gateway/prepare.js +296 -0
  53. package/dist/internal/gateway/prepare.js.map +1 -0
  54. package/dist/internal/gateway/transport.js +262 -0
  55. package/dist/internal/gateway/transport.js.map +1 -0
  56. package/dist/internal/platform.browser.js +27 -0
  57. package/dist/internal/platform.browser.js.map +1 -0
  58. package/dist/internal/platform.js +86 -0
  59. package/dist/internal/platform.js.map +1 -0
  60. package/dist/internal/router.js +82 -0
  61. package/dist/internal/router.js.map +1 -0
  62. package/dist/internal/transport/base-url.js +78 -0
  63. package/dist/internal/transport/base-url.js.map +1 -0
  64. package/dist/internal/transport/ehbp.js +183 -0
  65. package/dist/internal/transport/ehbp.js.map +1 -0
  66. package/dist/internal/transport/header.js +35 -0
  67. package/dist/internal/transport/header.js.map +1 -0
  68. package/dist/internal/transport/origin.js +75 -0
  69. package/dist/internal/transport/origin.js.map +1 -0
  70. package/dist/internal/transport/pin-errors.js +24 -0
  71. package/dist/internal/transport/pin-errors.js.map +1 -0
  72. package/dist/internal/transport/pin.js +31 -0
  73. package/dist/internal/transport/pin.js.map +1 -0
  74. package/dist/internal/transport/replay.js +60 -0
  75. package/dist/internal/transport/replay.js.map +1 -0
  76. package/dist/internal/transport/tls.browser.js +10 -0
  77. package/dist/internal/transport/tls.browser.js.map +1 -0
  78. package/dist/internal/transport/tls.js +189 -0
  79. package/dist/internal/transport/tls.js.map +1 -0
  80. package/dist/internal/transport/transport.js +171 -0
  81. package/dist/internal/transport/transport.js.map +1 -0
  82. package/dist/internal/transport/ws.browser.js +20 -0
  83. package/dist/internal/transport/ws.browser.js.map +1 -0
  84. package/dist/internal/transport/ws.js +220 -0
  85. package/dist/internal/transport/ws.js.map +1 -0
  86. package/dist/internal/unsafe-client.d.ts +49 -0
  87. package/dist/internal/unsafe-client.d.ts.map +1 -0
  88. package/dist/internal/unsafe-client.js +159 -0
  89. package/dist/internal/unsafe-client.js.map +1 -0
  90. package/dist/internal/verified.js +134 -0
  91. package/dist/internal/verified.js.map +1 -0
  92. package/dist/internal/websocket.js +92 -0
  93. package/dist/internal/websocket.js.map +1 -0
  94. package/dist/openai-client.d.ts +165 -0
  95. package/dist/openai-client.d.ts.map +1 -0
  96. package/dist/openai-client.js +341 -0
  97. package/dist/openai-client.js.map +1 -0
  98. package/dist/options.d.ts +296 -0
  99. package/dist/options.d.ts.map +1 -0
  100. package/dist/options.js +2 -0
  101. package/dist/options.js.map +1 -0
  102. package/dist/realtime.browser.js +4 -4
  103. package/dist/realtime.browser.js.map +1 -1
  104. package/dist/realtime.d.ts +17 -11
  105. package/dist/realtime.d.ts.map +1 -1
  106. package/dist/realtime.js +22 -9
  107. package/dist/realtime.js.map +1 -1
  108. package/dist/router-client.d.ts +48 -0
  109. package/dist/router-client.d.ts.map +1 -0
  110. package/dist/router-client.js +124 -0
  111. package/dist/router-client.js.map +1 -0
  112. package/dist/session-recovery.d.ts +25 -0
  113. package/dist/session-recovery.d.ts.map +1 -0
  114. package/dist/session-recovery.js +32 -0
  115. package/dist/session-recovery.js.map +1 -0
  116. package/dist/surface.d.ts +12 -0
  117. package/dist/surface.d.ts.map +1 -0
  118. package/dist/surface.js +12 -0
  119. package/dist/surface.js.map +1 -0
  120. package/dist/unsafe.d.ts +9 -2
  121. package/dist/unsafe.d.ts.map +1 -1
  122. package/dist/unsafe.js +9 -2
  123. package/dist/unsafe.js.map +1 -1
  124. package/dist/version.d.ts +1 -1
  125. package/dist/version.d.ts.map +1 -1
  126. package/dist/version.js +1 -1
  127. package/dist/version.js.map +1 -1
  128. package/dist/websocket.d.ts +31 -0
  129. package/dist/websocket.d.ts.map +1 -0
  130. package/dist/websocket.js +28 -0
  131. package/dist/websocket.js.map +1 -0
  132. package/package.json +30 -21
  133. package/dist/ai-sdk-provider.d.ts +0 -54
  134. package/dist/ai-sdk-provider.d.ts.map +0 -1
  135. package/dist/ai-sdk-provider.js +0 -59
  136. package/dist/ai-sdk-provider.js.map +0 -1
  137. package/dist/atc.d.ts +0 -23
  138. package/dist/atc.d.ts.map +0 -1
  139. package/dist/atc.js +0 -70
  140. package/dist/atc.js.map +0 -1
  141. package/dist/config.d.ts +0 -14
  142. package/dist/config.d.ts.map +0 -1
  143. package/dist/config.js +0 -14
  144. package/dist/config.js.map +0 -1
  145. package/dist/encrypted-body-fetch.d.ts +0 -34
  146. package/dist/encrypted-body-fetch.d.ts.map +0 -1
  147. package/dist/encrypted-body-fetch.js +0 -200
  148. package/dist/encrypted-body-fetch.js.map +0 -1
  149. package/dist/env.d.ts +0 -8
  150. package/dist/env.d.ts.map +0 -1
  151. package/dist/env.js +0 -43
  152. package/dist/env.js.map +0 -1
  153. package/dist/pinned-tls-fetch.browser.d.ts +0 -2
  154. package/dist/pinned-tls-fetch.browser.d.ts.map +0 -1
  155. package/dist/pinned-tls-fetch.browser.js +0 -13
  156. package/dist/pinned-tls-fetch.browser.js.map +0 -1
  157. package/dist/pinned-tls-fetch.d.ts +0 -3
  158. package/dist/pinned-tls-fetch.d.ts.map +0 -1
  159. package/dist/pinned-tls-fetch.js +0 -159
  160. package/dist/pinned-tls-fetch.js.map +0 -1
  161. package/dist/pinned-ws.browser.d.ts +0 -15
  162. package/dist/pinned-ws.browser.d.ts.map +0 -1
  163. package/dist/pinned-ws.browser.js +0 -10
  164. package/dist/pinned-ws.browser.js.map +0 -1
  165. package/dist/pinned-ws.d.ts +0 -31
  166. package/dist/pinned-ws.d.ts.map +0 -1
  167. package/dist/pinned-ws.js +0 -84
  168. package/dist/pinned-ws.js.map +0 -1
  169. package/dist/realtime.browser.d.ts +0 -16
  170. package/dist/realtime.browser.d.ts.map +0 -1
  171. package/dist/secure-client.d.ts +0 -242
  172. package/dist/secure-client.d.ts.map +0 -1
  173. package/dist/secure-client.js +0 -383
  174. package/dist/secure-client.js.map +0 -1
  175. package/dist/secure-fetch.d.ts +0 -16
  176. package/dist/secure-fetch.d.ts.map +0 -1
  177. package/dist/secure-fetch.js +0 -44
  178. package/dist/secure-fetch.js.map +0 -1
  179. package/dist/tinfoil-ai.d.ts +0 -179
  180. package/dist/tinfoil-ai.d.ts.map +0 -1
  181. package/dist/tinfoil-ai.js +0 -200
  182. package/dist/tinfoil-ai.js.map +0 -1
  183. package/dist/unsafe.browser.d.ts +0 -3
  184. package/dist/unsafe.browser.d.ts.map +0 -1
  185. package/dist/unsafe.browser.js +0 -3
  186. package/dist/unsafe.browser.js.map +0 -1
  187. package/dist/unverified-client.d.ts +0 -19
  188. package/dist/unverified-client.d.ts.map +0 -1
  189. package/dist/unverified-client.js +0 -69
  190. package/dist/unverified-client.js.map +0 -1
  191. package/dist/user-cache-secret-store.browser.d.ts +0 -7
  192. package/dist/user-cache-secret-store.browser.d.ts.map +0 -1
  193. package/dist/user-cache-secret-store.browser.js +0 -9
  194. package/dist/user-cache-secret-store.browser.js.map +0 -1
  195. package/dist/user-cache-secret-store.d.ts +0 -19
  196. package/dist/user-cache-secret-store.d.ts.map +0 -1
  197. package/dist/user-cache-secret-store.js +0 -160
  198. package/dist/user-cache-secret-store.js.map +0 -1
  199. package/dist/user-cache-secret.d.ts +0 -62
  200. package/dist/user-cache-secret.d.ts.map +0 -1
  201. package/dist/user-cache-secret.js +0 -400
  202. package/dist/user-cache-secret.js.map +0 -1
  203. package/dist/verifier.d.ts +0 -2
  204. package/dist/verifier.d.ts.map +0 -1
  205. package/dist/verifier.js +0 -2
  206. package/dist/verifier.js.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;GAK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AAC,QAAQ,UAAU,CAAC;IACzC,OAAO,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC;AACzF,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,GAAiB,EAAE,OAAe;IAC5D,MAAM,OAAO,GAAG,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,IAAI,GAAG,EAAE,CAAC;IAC5C,IAAI,GAAG,YAAY,YAAY;QAAE,OAAO,IAAI,YAAY,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC3E,IAAI,GAAG,YAAY,kBAAkB;QAAE,OAAO,IAAI,kBAAkB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACvF,OAAO,IAAI,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAChD,CAAC;AAED,4EAA4E;AAC5E,MAAM,UAAU,WAAW,CAAC,GAAY;IACtC,OAAO,GAAG,YAAY,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,YAAY,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;AAC9F,CAAC;AAED,iFAAiF;AACjF,MAAM,UAAU,eAAe,CAAC,GAAY;IAC1C,OAAO,GAAG,YAAY,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,gBAAgB,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;AAClG,CAAC"}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * An HTTP client for Tinfoil inference through the gateway. Each request is
3
+ * sealed to a verified replica of the model it names, as with the `"ehbp"`
4
+ * transport: the gateway sees the method, URL and headers, and protocol
5
+ * upgrades are refused.
6
+ *
7
+ * @experimental The gateway route is new; this API may change without a major version.
8
+ */
9
+ import type { Verification } from "@tinfoilsh/verifier";
10
+ import { asyncDispose } from "./internal/dispose.js";
11
+ import type { CallOptions, Catalog, Fetch, GatewayOptions, ReplicaVerification } from "./options.js";
12
+ /**
13
+ * Tinfoil's gateway, used when no `url` option is given.
14
+ *
15
+ * @experimental The gateway route is new; this API may change without a major version.
16
+ */
17
+ export declare const DEFAULT_GATEWAY_URL = "https://inference-gateway.tinfoil.sh/v1/";
18
+ /**
19
+ * The HTTP client for Tinfoil inference through the gateway; see the module
20
+ * header for what it protects. The constructor is private: type-checked
21
+ * code cannot call it, and a call from untyped code is refused at runtime.
22
+ * Clients come from {@link GatewayHTTPClient.create} and
23
+ * {@link GatewayOpenAIClient.create}.
24
+ *
25
+ * @experimental The gateway route is new; this API may change without a major version.
26
+ */
27
+ export declare class GatewayHTTPClient {
28
+ #private;
29
+ /** As {@link EnclaveHTTPClient.fetch}. */
30
+ readonly fetch: Fetch;
31
+ /** The gateway's base URL, which requests are sent to. Always ends with `/`, so a relative path resolves against it with `new URL(path, client.baseURL)`. */
32
+ readonly baseURL: string;
33
+ private constructor();
34
+ /**
35
+ * Checks the options before any network I/O, then fetches the catalog
36
+ * before returning. A model's replicas are verified on its first request,
37
+ * so `modelPins` may name models the catalog doesn't list.
38
+ */
39
+ static create(options?: GatewayOptions): Promise<GatewayHTTPClient>;
40
+ /**
41
+ * The catalog's models that the client's policy serves, sorted, without
42
+ * verifying replicas. A failed catalog refresh doesn't fail it; only the
43
+ * first fetch can.
44
+ */
45
+ models(options?: CallOptions): Promise<string[]>;
46
+ /** A verification valid now of a replica serving `model`, verifying replicas only if needed. */
47
+ ensureVerified(model: string, options?: CallOptions): Promise<Verification>;
48
+ /** The last successful verification of each replica used so far, unchecked: they may have expired or been rejected. A new array on each read. */
49
+ get current(): ReplicaVerification[];
50
+ /**
51
+ * Refuses further use: `fetch`, `models` and `ensureVerified` reject with
52
+ * a {@link ConfigurationError} afterwards, and no replica is verified
53
+ * again; `current` still reports the replicas' last verifications.
54
+ * Requests are sealed and sent through the runtime's fetch, or the `fetch`
55
+ * option, so the client holds no connections of its own to release; a sealed exchange already
56
+ * under way completes, while a request still preparing, verifying or
57
+ * rerouting rejects as closed. Closing again does nothing.
58
+ */
59
+ close(): Promise<void>;
60
+ /** Closes the client at the end of an `await using` block; see {@link close}. */
61
+ [asyncDispose](): Promise<void>;
62
+ }
63
+ /**
64
+ * Reads the catalog of the gateway at `host`, keeping only models with
65
+ * replicas of a bare `tinfoilsh/` repository, without a tag or digest. The
66
+ * catalog is untrusted: replicas are verified when they are used.
67
+ *
68
+ * @experimental The gateway route is new; this API may change without a major version.
69
+ */
70
+ export declare function fetchCatalog(host: string, options?: CallOptions): Promise<Catalog>;
71
+ //# sourceMappingURL=gateway-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gateway-client.d.ts","sourceRoot":"","sources":["../src/gateway-client.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAIxD,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAMrD,OAAO,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAErG;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,6CAA6C,CAAC;AAmD9E;;;;;;;;GAQG;AACH,qBAAa,iBAAiB;;IAC5B,0CAA0C;IAC1C,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,6JAA6J;IAC7J,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAGzB,OAAO;IAaP;;;;OAIG;IACH,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAInE;;;;OAIG;IACH,MAAM,CAAC,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAIhD,gGAAgG;IAChG,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;IAI3E,iJAAiJ;IACjJ,IAAI,OAAO,IAAI,mBAAmB,EAAE,CAEnC;IAED;;;;;;;;OAQG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5B,iFAAiF;IACjF,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC;CAGhC;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,CAElF"}
@@ -0,0 +1,145 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _GatewayHTTPClient_transport;
13
+ import { ehbpOptionsFor } from "./enclave-client.js";
14
+ import { ConfigurationError } from "./errors.js";
15
+ import { resolveUserCacheSecret } from "./internal/cachesecret.js";
16
+ import { asyncDispose } from "./internal/dispose.js";
17
+ import { fetchCatalogWith } from "./internal/gateway/catalog.js";
18
+ import { GatewayPolicy } from "./internal/gateway/policy.js";
19
+ import { GatewayTransport } from "./internal/gateway/transport.js";
20
+ import { normalizeBaseURL, validateBaseURL } from "./internal/transport/base-url.js";
21
+ import { withOriginRestriction } from "./internal/transport/origin.js";
22
+ /**
23
+ * Tinfoil's gateway, used when no `url` option is given.
24
+ *
25
+ * @experimental The gateway route is new; this API may change without a major version.
26
+ */
27
+ export const DEFAULT_GATEWAY_URL = "https://inference-gateway.tinfoil.sh/v1/";
28
+ /** Only this module holds the token the constructor requires, so untyped code that calls it is refused. */
29
+ const TOKEN = Symbol("GatewayHTTPClient");
30
+ /** Calls the private constructor; the class assigns it, as the only place the constructor can be called. */
31
+ let construct;
32
+ /** @internal The factory with its test seams; {@link GatewayHTTPClient.create} is this without them. */
33
+ export async function createGatewayHTTPClient(options, seams) {
34
+ const given = options.url === undefined || options.url === "" ? DEFAULT_GATEWAY_URL : options.url;
35
+ let base;
36
+ try {
37
+ base = new URL(given);
38
+ }
39
+ catch {
40
+ throw new ConfigurationError(`gateway base URL must be an absolute HTTPS URL: ${JSON.stringify(given)}`);
41
+ }
42
+ if (base.protocol !== "https:" || base.host === "")
43
+ throw new ConfigurationError(`gateway base URL must be an absolute HTTPS URL: ${JSON.stringify(given)}`);
44
+ // Credentials in the URL would go out in plaintext to the gateway with every request.
45
+ if (base.username !== "" || base.password !== "")
46
+ throw new ConfigurationError("gateway base URL must not carry a username or password");
47
+ // The rules every base URL follows, such as no query string or fragment; HTTPS is already required above.
48
+ validateBaseURL(given);
49
+ const baseURL = normalizeBaseURL(given);
50
+ const policy = await GatewayPolicy.create(options.modelPins, options.pinnedModelsOnly ?? false, options.verifier);
51
+ const secret = await (seams.resolveSecret ?? resolveUserCacheSecret)(options.userCacheSecret);
52
+ const transport = new GatewayTransport({
53
+ baseURL,
54
+ relay: base.host,
55
+ catalog: options.catalog,
56
+ policy,
57
+ verificationCheck: options.verificationCheck,
58
+ secret,
59
+ catalogFetch: seams.catalogFetch,
60
+ documentFetch: seams.documentFetch,
61
+ clock: seams.clock,
62
+ nonce: seams.nonce,
63
+ sleep: seams.sleep,
64
+ ehbp: ehbpOptionsFor(options.fetch, seams),
65
+ });
66
+ const client = construct(transport, baseURL, withOriginRestriction(transport.fetch, "", baseURL));
67
+ // An unreachable gateway fails here rather than on the first request.
68
+ await client.models({ signal: options.signal });
69
+ return client;
70
+ }
71
+ /**
72
+ * The HTTP client for Tinfoil inference through the gateway; see the module
73
+ * header for what it protects. The constructor is private: type-checked
74
+ * code cannot call it, and a call from untyped code is refused at runtime.
75
+ * Clients come from {@link GatewayHTTPClient.create} and
76
+ * {@link GatewayOpenAIClient.create}.
77
+ *
78
+ * @experimental The gateway route is new; this API may change without a major version.
79
+ */
80
+ export class GatewayHTTPClient {
81
+ constructor(token, transport, baseURL, fetchImpl) {
82
+ _GatewayHTTPClient_transport.set(this, void 0);
83
+ if (token !== TOKEN)
84
+ throw new ConfigurationError("GatewayHTTPClient is created with GatewayHTTPClient.create");
85
+ __classPrivateFieldSet(this, _GatewayHTTPClient_transport, transport, "f");
86
+ this.fetch = fetchImpl;
87
+ this.baseURL = baseURL;
88
+ // The client's state lives in the transport; the client itself never changes once created.
89
+ Object.freeze(this);
90
+ }
91
+ /**
92
+ * Checks the options before any network I/O, then fetches the catalog
93
+ * before returning. A model's replicas are verified on its first request,
94
+ * so `modelPins` may name models the catalog doesn't list.
95
+ */
96
+ static create(options) {
97
+ return createGatewayHTTPClient(options ?? {}, {});
98
+ }
99
+ /**
100
+ * The catalog's models that the client's policy serves, sorted, without
101
+ * verifying replicas. A failed catalog refresh doesn't fail it; only the
102
+ * first fetch can.
103
+ */
104
+ models(options) {
105
+ return __classPrivateFieldGet(this, _GatewayHTTPClient_transport, "f").models(options?.signal);
106
+ }
107
+ /** A verification valid now of a replica serving `model`, verifying replicas only if needed. */
108
+ ensureVerified(model, options) {
109
+ return __classPrivateFieldGet(this, _GatewayHTTPClient_transport, "f").ensureVerified(model, options?.signal);
110
+ }
111
+ /** The last successful verification of each replica used so far, unchecked: they may have expired or been rejected. A new array on each read. */
112
+ get current() {
113
+ return __classPrivateFieldGet(this, _GatewayHTTPClient_transport, "f").verifications();
114
+ }
115
+ /**
116
+ * Refuses further use: `fetch`, `models` and `ensureVerified` reject with
117
+ * a {@link ConfigurationError} afterwards, and no replica is verified
118
+ * again; `current` still reports the replicas' last verifications.
119
+ * Requests are sealed and sent through the runtime's fetch, or the `fetch`
120
+ * option, so the client holds no connections of its own to release; a sealed exchange already
121
+ * under way completes, while a request still preparing, verifying or
122
+ * rerouting rejects as closed. Closing again does nothing.
123
+ */
124
+ async close() {
125
+ __classPrivateFieldGet(this, _GatewayHTTPClient_transport, "f").close();
126
+ }
127
+ /** Closes the client at the end of an `await using` block; see {@link close}. */
128
+ [(_GatewayHTTPClient_transport = new WeakMap(), asyncDispose)]() {
129
+ return this.close();
130
+ }
131
+ }
132
+ (() => {
133
+ construct = (...parts) => new GatewayHTTPClient(TOKEN, ...parts);
134
+ })();
135
+ /**
136
+ * Reads the catalog of the gateway at `host`, keeping only models with
137
+ * replicas of a bare `tinfoilsh/` repository, without a tag or digest. The
138
+ * catalog is untrusted: replicas are verified when they are used.
139
+ *
140
+ * @experimental The gateway route is new; this API may change without a major version.
141
+ */
142
+ export function fetchCatalog(host, options) {
143
+ return fetchCatalogWith(host, (input, init) => fetch(input, init), options?.signal);
144
+ }
145
+ //# sourceMappingURL=gateway-client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gateway-client.js","sourceRoot":"","sources":["../src/gateway-client.ts"],"names":[],"mappings":";;;;;;;;;;;;AASA,OAAO,EAAE,cAAc,EAA2B,MAAM,qBAAqB,CAAC;AAC9E,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AACnE,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAA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AAE,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;AACtF,CAAC"}
package/dist/http.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Attested HTTP clients without the OpenAI or AI SDK adapters. This entry
3
+ * shares the root entry's clients, verification, transports and lifecycle;
4
+ * the package's browser mappings select EHBP-only implementations in a page.
5
+ */
6
+ export { EnclaveHTTPClient } from "./enclave-client.js";
7
+ export { RouterHTTPClient } from "./router-client.js";
8
+ export { GatewayHTTPClient, fetchCatalog } from "./gateway-client.js";
9
+ export { TinfoilError, ConfigurationError, NetworkError, AttestationError, type ErrorCategory } from "./errors.js";
10
+ export { sessionRecoveryToken, serializeSessionRecoveryToken, deserializeSessionRecoveryToken, decryptResponseWithToken, type SessionRecoveryToken, } from "./session-recovery.js";
11
+ export type { Fetch, TransportMode, VerificationCheck, CallOptions, EnclaveOptions, RouterOptions, GatewayOptions, ModelPin, Catalog, CatalogEntry, ReplicaVerification, } from "./options.js";
12
+ export { SDK_VERSION } from "./version.js";
13
+ //# sourceMappingURL=http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,YAAY,EAAE,gBAAgB,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AACnH,OAAO,EACL,oBAAoB,EACpB,6BAA6B,EAC7B,+BAA+B,EAC/B,wBAAwB,EACxB,KAAK,oBAAoB,GAC1B,MAAM,uBAAuB,CAAC;AAC/B,YAAY,EACV,KAAK,EACL,aAAa,EACb,iBAAiB,EACjB,WAAW,EACX,cAAc,EACd,aAAa,EACb,cAAc,EACd,QAAQ,EACR,OAAO,EACP,YAAY,EACZ,mBAAmB,GACpB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC"}
package/dist/http.js ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Attested HTTP clients without the OpenAI or AI SDK adapters. This entry
3
+ * shares the root entry's clients, verification, transports and lifecycle;
4
+ * the package's browser mappings select EHBP-only implementations in a page.
5
+ */
6
+ export { EnclaveHTTPClient } from "./enclave-client.js";
7
+ export { RouterHTTPClient } from "./router-client.js";
8
+ export { GatewayHTTPClient, fetchCatalog } from "./gateway-client.js";
9
+ export { TinfoilError, ConfigurationError, NetworkError, AttestationError } from "./errors.js";
10
+ export { sessionRecoveryToken, serializeSessionRecoveryToken, deserializeSessionRecoveryToken, decryptResponseWithToken, } from "./session-recovery.js";
11
+ export { SDK_VERSION } from "./version.js";
12
+ //# sourceMappingURL=http.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.js","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,YAAY,EAAE,gBAAgB,EAAsB,MAAM,aAAa,CAAC;AACnH,OAAO,EACL,oBAAoB,EACpB,6BAA6B,EAC7B,+BAA+B,EAC/B,wBAAwB,GAEzB,MAAM,uBAAuB,CAAC;AAc/B,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC"}
@@ -1,13 +1,18 @@
1
- export { TinfoilAI } from "./tinfoil-ai.js";
2
- export { TinfoilAI as default } from "./tinfoil-ai.js";
3
- export type { TinfoilAIOptions } from "./tinfoil-ai.js";
4
- export { TinfoilError, ConfigurationError, AttestationError, Verifier, assembleAttestationBundle, } from "./verifier.js";
5
- export type { AttestationDocument, AttestationMeasurement, AttestationResponse, AttestationBundle, VerificationDocument, VerificationStepState, HardwareMeasurement, VerifierOptions, CertVerificationResult, } from "./verifier.js";
6
- export { createTinfoilAI } from "./ai-sdk-provider.js";
7
- export type { CreateTinfoilAIOptions } from "./ai-sdk-provider.js";
8
- export { SecureClient, type SecureClientOptions, type TransportMode, type SecureWebSocketOptions, } from "./secure-client.js";
9
- export type { SessionRecoveryToken } from "ehbp";
10
- export { serializeSessionRecoveryToken, deserializeSessionRecoveryToken, decryptResponseWithToken, } from "ehbp";
11
- export { fetchRouter, fetchAttestationBundle, type FetchAttestationBundleOptions, } from "./atc.js";
12
- export { type Uploadable, toFile, APIPromise, PagePromise, OpenAIError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError, } from "openai";
1
+ /**
2
+ * The client for browsers: the same surface as the Node entry, with the
3
+ * `"ehbp"` transport the only one available and the default. Without a
4
+ * `userCacheSecret` option, the user cache secret is generated in memory
5
+ * for the page's life, as it is for a process on the server.
6
+ * An OpenAI client in a page takes its credential as `bearerToken`: a
7
+ * short-lived token from the application's own sign-in, which also allows
8
+ * browser use. A long-lived API key belongs on a server, and is refused here.
9
+ *
10
+ * The type declarations are shared with the Node build, so a browser
11
+ * project type-checks a few names that this build refuses with a
12
+ * `ConfigurationError` when they are called: the pinned WebSockets
13
+ * (`createWebSocket` and `pinnedWebSocketOptions`), realtime sessions
14
+ * (`createRealtimeWebSocket`), and the `"tls"` transport mode, since a
15
+ * browser exposes nothing of the TLS connection that they would pin.
16
+ */
17
+ export * from "./surface.js";
13
18
  //# sourceMappingURL=index.browser.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.browser.d.ts","sourceRoot":"","sources":["../src/index.browser.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,SAAS,IAAI,OAAO,EAAE,MAAM,iBAAiB,CAAC;AACvD,YAAY,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,OAAO,EACL,YAAY,EACZ,kBAAkB,EAClB,gBAAgB,EAChB,QAAQ,EACR,yBAAyB,GAC1B,MAAM,eAAe,CAAC;AAEvB,YAAY,EACV,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,EACnB,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,mBAAmB,EACnB,eAAe,EACf,sBAAsB,GACvB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,YAAY,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AACnE,OAAO,EACL,YAAY,EACZ,KAAK,mBAAmB,EACxB,KAAK,aAAa,EAClB,KAAK,sBAAsB,GAC5B,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EAAE,oBAAoB,EAAE,MAAM,MAAM,CAAC;AACjD,OAAO,EACL,6BAA6B,EAC7B,+BAA+B,EAC/B,wBAAwB,GACzB,MAAM,MAAM,CAAC;AACd,OAAO,EACL,WAAW,EACX,sBAAsB,EACtB,KAAK,6BAA6B,GACnC,MAAM,UAAU,CAAC;AAIlB,OAAO,EACL,KAAK,UAAU,EACf,MAAM,EACN,UAAU,EACV,WAAW,EACX,WAAW,EACX,QAAQ,EACR,kBAAkB,EAClB,yBAAyB,EACzB,iBAAiB,EACjB,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,qBAAqB,EACrB,wBAAwB,GACzB,MAAM,QAAQ,CAAC"}
1
+ {"version":3,"file":"index.browser.d.ts","sourceRoot":"","sources":["../src/index.browser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,cAAc,cAAc,CAAC"}
@@ -1,13 +1,18 @@
1
- // Browser entry point - uses same consolidated modules as server
2
- // Runtime detection in secure-fetch.ts handles browser-specific behavior
3
- export { TinfoilAI } from "./tinfoil-ai.js";
4
- export { TinfoilAI as default } from "./tinfoil-ai.js";
5
- export { TinfoilError, ConfigurationError, AttestationError, Verifier, assembleAttestationBundle, } from "./verifier.js";
6
- export { createTinfoilAI } from "./ai-sdk-provider.js";
7
- export { SecureClient, } from "./secure-client.js";
8
- export { serializeSessionRecoveryToken, deserializeSessionRecoveryToken, decryptResponseWithToken, } from "ehbp";
9
- export { fetchRouter, fetchAttestationBundle, } from "./atc.js";
10
- // Re-export OpenAI utility types and classes that users might need
11
- // Using public exports from the main OpenAI package instead of deep imports
12
- export { toFile, APIPromise, PagePromise, OpenAIError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError, } from "openai";
1
+ /**
2
+ * The client for browsers: the same surface as the Node entry, with the
3
+ * `"ehbp"` transport the only one available and the default. Without a
4
+ * `userCacheSecret` option, the user cache secret is generated in memory
5
+ * for the page's life, as it is for a process on the server.
6
+ * An OpenAI client in a page takes its credential as `bearerToken`: a
7
+ * short-lived token from the application's own sign-in, which also allows
8
+ * browser use. A long-lived API key belongs on a server, and is refused here.
9
+ *
10
+ * The type declarations are shared with the Node build, so a browser
11
+ * project type-checks a few names that this build refuses with a
12
+ * `ConfigurationError` when they are called: the pinned WebSockets
13
+ * (`createWebSocket` and `pinnedWebSocketOptions`), realtime sessions
14
+ * (`createRealtimeWebSocket`), and the `"tls"` transport mode, since a
15
+ * browser exposes nothing of the TLS connection that they would pin.
16
+ */
17
+ export * from "./surface.js";
13
18
  //# sourceMappingURL=index.browser.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.browser.js","sourceRoot":"","sources":["../src/index.browser.ts"],"names":[],"mappings":"AAAA,iEAAiE;AACjE,yEAAyE;AACzE,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,SAAS,IAAI,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAGvD,OAAO,EACL,YAAY,EACZ,kBAAkB,EAClB,gBAAgB,EAChB,QAAQ,EACR,yBAAyB,GAC1B,MAAM,eAAe,CAAC;AAavB,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,OAAO,EACL,YAAY,GAIb,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EACL,6BAA6B,EAC7B,+BAA+B,EAC/B,wBAAwB,GACzB,MAAM,MAAM,CAAC;AACd,OAAO,EACL,WAAW,EACX,sBAAsB,GAEvB,MAAM,UAAU,CAAC;AAElB,mEAAmE;AACnE,4EAA4E;AAC5E,OAAO,EAEL,MAAM,EACN,UAAU,EACV,WAAW,EACX,WAAW,EACX,QAAQ,EACR,kBAAkB,EAClB,yBAAyB,EACzB,iBAAiB,EACjB,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,qBAAqB,EACrB,wBAAwB,GACzB,MAAM,QAAQ,CAAC"}
1
+ {"version":3,"file":"index.browser.js","sourceRoot":"","sources":["../src/index.browser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,cAAc,cAAc,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,13 +1,3 @@
1
- export { TinfoilAI } from "./tinfoil-ai.js";
2
- export { TinfoilAI as default } from "./tinfoil-ai.js";
3
- export type { TinfoilAIOptions } from "./tinfoil-ai.js";
4
- export { TinfoilError, ConfigurationError, AttestationError, Verifier, assembleAttestationBundle, } from "./verifier.js";
5
- export type { AttestationDocument, AttestationMeasurement, AttestationResponse, AttestationBundle, VerificationDocument, VerificationStepState, HardwareMeasurement, VerifierOptions, CertVerificationResult, } from "./verifier.js";
6
- export { createTinfoilAI } from "./ai-sdk-provider.js";
7
- export type { CreateTinfoilAIOptions } from "./ai-sdk-provider.js";
8
- export { SecureClient, type SecureClientOptions, type TransportMode, type SecureWebSocketOptions, } from "./secure-client.js";
9
- export type { SessionRecoveryToken } from "ehbp";
10
- export { serializeSessionRecoveryToken, deserializeSessionRecoveryToken, decryptResponseWithToken, } from "ehbp";
11
- export { fetchRouter, fetchAttestationBundle, type FetchAttestationBundleOptions, } from "./atc.js";
12
- export { type Uploadable, toFile, APIPromise, PagePromise, OpenAIError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError, } from "openai";
1
+ /** The client for Node and Bun: the `"tls"` transport is available and the default. */
2
+ export * from "./surface.js";
13
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,SAAS,IAAI,OAAO,EAAE,MAAM,iBAAiB,CAAC;AACvD,YAAY,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,OAAO,EACL,YAAY,EACZ,kBAAkB,EAClB,gBAAgB,EAChB,QAAQ,EACR,yBAAyB,GAC1B,MAAM,eAAe,CAAC;AAEvB,YAAY,EACV,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,EACnB,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,mBAAmB,EACnB,eAAe,EACf,sBAAsB,GACvB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,YAAY,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AACnE,OAAO,EACL,YAAY,EACZ,KAAK,mBAAmB,EACxB,KAAK,aAAa,EAClB,KAAK,sBAAsB,GAC5B,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EAAE,oBAAoB,EAAE,MAAM,MAAM,CAAC;AACjD,OAAO,EACL,6BAA6B,EAC7B,+BAA+B,EAC/B,wBAAwB,GACzB,MAAM,MAAM,CAAC;AACd,OAAO,EACL,WAAW,EACX,sBAAsB,EACtB,KAAK,6BAA6B,GACnC,MAAM,UAAU,CAAC;AAIlB,OAAO,EACL,KAAK,UAAU,EACf,MAAM,EACN,UAAU,EACV,WAAW,EACX,WAAW,EACX,QAAQ,EACR,kBAAkB,EAClB,yBAAyB,EACzB,iBAAiB,EACjB,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,qBAAqB,EACrB,wBAAwB,GACzB,MAAM,QAAQ,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,uFAAuF;AACvF,cAAc,cAAc,CAAC"}
package/dist/index.js CHANGED
@@ -1,12 +1,3 @@
1
- // Re-export the TinfoilAI class
2
- export { TinfoilAI } from "./tinfoil-ai.js";
3
- export { TinfoilAI as default } from "./tinfoil-ai.js";
4
- export { TinfoilError, ConfigurationError, AttestationError, Verifier, assembleAttestationBundle, } from "./verifier.js";
5
- export { createTinfoilAI } from "./ai-sdk-provider.js";
6
- export { SecureClient, } from "./secure-client.js";
7
- export { serializeSessionRecoveryToken, deserializeSessionRecoveryToken, decryptResponseWithToken, } from "ehbp";
8
- export { fetchRouter, fetchAttestationBundle, } from "./atc.js";
9
- // Re-export OpenAI utility types and classes that users might need
10
- // Using public exports from the main OpenAI package instead of deep imports
11
- export { toFile, APIPromise, PagePromise, OpenAIError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError, } from "openai";
1
+ /** The client for Node and Bun: the `"tls"` transport is available and the default. */
2
+ export * from "./surface.js";
12
3
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,SAAS,IAAI,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAGvD,OAAO,EACL,YAAY,EACZ,kBAAkB,EAClB,gBAAgB,EAChB,QAAQ,EACR,yBAAyB,GAC1B,MAAM,eAAe,CAAC;AAavB,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,OAAO,EACL,YAAY,GAIb,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EACL,6BAA6B,EAC7B,+BAA+B,EAC/B,wBAAwB,GACzB,MAAM,MAAM,CAAC;AACd,OAAO,EACL,WAAW,EACX,sBAAsB,GAEvB,MAAM,UAAU,CAAC;AAElB,mEAAmE;AACnE,4EAA4E;AAC5E,OAAO,EAEL,MAAM,EACN,UAAU,EACV,WAAW,EACX,WAAW,EACX,QAAQ,EACR,kBAAkB,EAClB,yBAAyB,EACzB,iBAAiB,EACjB,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,qBAAqB,EACrB,wBAAwB,GACzB,MAAM,QAAQ,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,uFAAuF;AACvF,cAAc,cAAc,CAAC"}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * A provider for the Vercel AI SDK whose requests go through an attested
3
+ * client: the discovered router by default, or one enclave when named. The
4
+ * provider is the OpenAI-compatible one the AI SDK ships, bound to the
5
+ * client's `fetch` and base URL, so `generateText`, `streamText` and the
6
+ * rest send through the client's protections and stream responses as the
7
+ * client delivers them. The attested client stays reachable from the
8
+ * provider for its verification facts. The `tinfoil/ai-sdk` entry
9
+ * exports the factory, so the AI SDK stays out of bundles that don't use it.
10
+ */
11
+ import { type OpenAICompatibleProvider } from "@ai-sdk/openai-compatible";
12
+ import { EnclaveHTTPClient } from "../enclave-client.js";
13
+ import type { EnclaveOptions, RouterOptions } from "../options.js";
14
+ import { RouterHTTPClient } from "../router-client.js";
15
+ /** The enclave a provider is bound to instead of the discovered router. */
16
+ export interface TinfoilProviderEnclave {
17
+ readonly host: string;
18
+ /** `owner/name[@tag][@sha256:digest]`; empty accepts configs the verifier option authorizes. */
19
+ readonly repo: string;
20
+ }
21
+ /**
22
+ * Configures {@link createTinfoil}. Exactly one credential is given: an
23
+ * `apiKey` on a server, or a `bearerToken` a browser page may hold. The
24
+ * remaining options configure the attested client: the router client's when
25
+ * `enclave` is unset, otherwise the enclave client's. `fetch` is the network
26
+ * implementation that carries the client's sealed requests in the `"ehbp"`
27
+ * transport mode, as for the clients, and is refused with `"tls"`.
28
+ */
29
+ export interface TinfoilProviderOptions extends Pick<RouterOptions & EnclaveOptions, "transportMode" | "baseURL" | "verifier" | "verificationCheck" | "userCacheSecret" | "signal" | "fetch"> {
30
+ /** A long-lived API key, sent as the bearer credential. It belongs on a server, never in a page. */
31
+ readonly apiKey?: string;
32
+ /** A credential a browser page may hold, such as a short-lived token from the application's own sign-in. */
33
+ readonly bearerToken?: string;
34
+ /** Binds the provider to this enclave rather than the discovered router. */
35
+ readonly enclave?: TinfoilProviderEnclave;
36
+ /** Headers added to every request, after the credential. */
37
+ readonly headers?: Readonly<Record<string, string>>;
38
+ }
39
+ /** The AI SDK provider, with the attested client its requests go through. */
40
+ export interface TinfoilProvider extends OpenAICompatibleProvider {
41
+ /**
42
+ * The client requests go through. It verifies the service again when the
43
+ * verification expires or its key changes, and is bound to that service.
44
+ */
45
+ readonly httpClient: EnclaveHTTPClient | RouterHTTPClient;
46
+ }
47
+ /**
48
+ * A provider for the Vercel AI SDK over an attested client, verified before
49
+ * it returns: the discovered router, or the `enclave` option. Use it with
50
+ * `generateText`, `streamText` and the other AI SDK functions:
51
+ *
52
+ * ```ts
53
+ * const tinfoil = await createTinfoil({ apiKey });
54
+ * const { text } = await generateText({ model: tinfoil("gpt-oss-120b"), prompt: "Hello!" });
55
+ * ```
56
+ */
57
+ export declare function createTinfoil(options: TinfoilProviderOptions): Promise<TinfoilProvider>;
58
+ //# sourceMappingURL=ai-sdk.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ai-sdk.d.ts","sourceRoot":"","sources":["../../src/internal/ai-sdk.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAA0B,KAAK,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AAClG,OAAO,EAA2B,iBAAiB,EAAwC,MAAM,sBAAsB,CAAC;AAIxH,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACnE,OAAO,EAA0B,gBAAgB,EAA0B,MAAM,qBAAqB,CAAC;AAEvG,2EAA2E;AAC3E,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,gGAAgG;IAChG,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,sBAAuB,SAAQ,IAAI,CAAC,aAAa,GAAG,cAAc,EAAE,eAAe,GAAG,SAAS,GAAG,UAAU,GAAG,mBAAmB,GAAG,iBAAiB,GAAG,QAAQ,GAAG,OAAO,CAAC;IAC3L,oGAAoG;IACpG,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,4GAA4G;IAC5G,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,4EAA4E;IAC5E,QAAQ,CAAC,OAAO,CAAC,EAAE,sBAAsB,CAAC;IAC1C,4DAA4D;IAC5D,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CACrD;AAED,6EAA6E;AAC7E,MAAM,WAAW,eAAgB,SAAQ,wBAAwB;IAC/D;;;OAGG;IACH,QAAQ,CAAC,UAAU,EAAE,iBAAiB,GAAG,gBAAgB,CAAC;CAC3D;AAiDD;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,eAAe,CAAC,CAEvF"}
@@ -0,0 +1,73 @@
1
+ /**
2
+ * A provider for the Vercel AI SDK whose requests go through an attested
3
+ * client: the discovered router by default, or one enclave when named. The
4
+ * provider is the OpenAI-compatible one the AI SDK ships, bound to the
5
+ * client's `fetch` and base URL, so `generateText`, `streamText` and the
6
+ * rest send through the client's protections and stream responses as the
7
+ * client delivers them. The attested client stays reachable from the
8
+ * provider for its verification facts. The `tinfoil/ai-sdk` entry
9
+ * exports the factory, so the AI SDK stays out of bundles that don't use it.
10
+ */
11
+ import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
12
+ import { createEnclaveHTTPClient, EnclaveHTTPClient, openAIURLOf } from "../enclave-client.js";
13
+ import { ConfigurationError } from "../errors.js";
14
+ import { checkAPIKeyRuntime } from "./credential.js";
15
+ import { createRouterHTTPClient, RouterHTTPClient } from "../router-client.js";
16
+ /**
17
+ * The one credential the options carry, or a {@link ConfigurationError} when
18
+ * there is none or more than one. A long-lived API key is refused where the
19
+ * runtime isn't a server, with the same error the OpenAI clients give: a
20
+ * page or a worker holds a `bearerToken` instead.
21
+ */
22
+ function credentialOf(options, globals) {
23
+ const apiKey = options.apiKey ?? "";
24
+ const bearerToken = options.bearerToken ?? "";
25
+ if (apiKey !== "" && bearerToken !== "")
26
+ throw new ConfigurationError("give either apiKey or bearerToken, not both");
27
+ if (apiKey === "" && bearerToken === "")
28
+ throw new ConfigurationError("a credential is required: apiKey on a server, or bearerToken in a browser");
29
+ checkAPIKeyRuntime(apiKey, globals);
30
+ return apiKey !== "" ? apiKey : bearerToken;
31
+ }
32
+ /** @internal The factory with its test seams; {@link createTinfoil} is this without them. */
33
+ export async function createTinfoilProvider(options, seams) {
34
+ // The credential is checked first, so a provider without one fails before any network I/O.
35
+ const credential = credentialOf(options, seams.globals);
36
+ const clientOptions = {
37
+ transportMode: options.transportMode,
38
+ baseURL: options.baseURL,
39
+ verifier: options.verifier,
40
+ verificationCheck: options.verificationCheck,
41
+ userCacheSecret: options.userCacheSecret,
42
+ signal: options.signal,
43
+ fetch: options.fetch,
44
+ };
45
+ const client = options.enclave === undefined ? await createRouterHTTPClient(clientOptions, seams) : await createEnclaveHTTPClient(options.enclave.host, options.enclave.repo, clientOptions, seams);
46
+ const provider = createOpenAICompatible({
47
+ name: "tinfoil",
48
+ baseURL: openAIURLOf(client),
49
+ apiKey: credential,
50
+ ...(options.headers === undefined ? {} : { headers: { ...options.headers } }),
51
+ // The client's fetch has the global fetch's call signature, which is all a DOM or Node `typeof fetch` is. Bun's
52
+ // typings merge a `preconnect` function onto the global, so under them a wrapped fetch is never `typeof fetch`.
53
+ fetch: client.fetch,
54
+ supportsStructuredOutputs: true,
55
+ });
56
+ // The provider is a function with the models' factories as properties; the client joins them as a fixed one.
57
+ Object.defineProperty(provider, "httpClient", { value: client, writable: false, configurable: false, enumerable: true });
58
+ return provider;
59
+ }
60
+ /**
61
+ * A provider for the Vercel AI SDK over an attested client, verified before
62
+ * it returns: the discovered router, or the `enclave` option. Use it with
63
+ * `generateText`, `streamText` and the other AI SDK functions:
64
+ *
65
+ * ```ts
66
+ * const tinfoil = await createTinfoil({ apiKey });
67
+ * const { text } = await generateText({ model: tinfoil("gpt-oss-120b"), prompt: "Hello!" });
68
+ * ```
69
+ */
70
+ export function createTinfoil(options) {
71
+ return createTinfoilProvider(options, {});
72
+ }
73
+ //# sourceMappingURL=ai-sdk.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ai-sdk.js","sourceRoot":"","sources":["../../src/internal/ai-sdk.ts"],"names":[],"mappings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C,eAAe,EAAE,OAAO,CAAC,eAAe;QACxC,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,KAAK,EAAE,OAAO,CAAC,KAAK;KACrB,CAAC;IACF,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,sBAAsB,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,uBAAuB,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,aAAa,EAAE,KAAK,CAAC,CAAC;IACpM,MAAM,QAAQ,GAAG,sBAAsB,CAAC;QACtC,IAAI,EAAE,SAAS;QACf,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC;QAC5B,MAAM,EAAE,UAAU;QAClB,GAAG,CAAC,OAAO,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;QAC7E,gHAAgH;QAChH,gHAAgH;QAChH,KAAK,EAAE,MAAM,CAAC,KAAgC;QAC9C,yBAAyB,EAAE,IAAI;KAChC,CAAC,CAAC;IACH,6GAA6G;IAC7G,MAAM,CAAC,cAAc,CAAC,QAAQ,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC;IACzH,OAAO,QAA2B,CAAC;AACrC,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,aAAa,CAAC,OAA+B;IAC3D,OAAO,qBAAqB,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;AAC5C,CAAC"}
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Reads a request body into memory for the layers that inspect or rewrite
3
+ * one before it is sent, bounded in size and stopped by the caller's abort.
4
+ */
5
+ import { ConfigurationError } from "../errors.js";
6
+ /**
7
+ * The body's bytes, within `limit`; a larger body is a {@link ConfigurationError}
8
+ * naming `what`. An abort of `signal` cancels the stream, stops the read and
9
+ * rejects with the signal's reason. A stream body is consumed; every other
10
+ * kind is copied.
11
+ */
12
+ export async function readAll(body, limit, what, signal) {
13
+ signal?.throwIfAborted();
14
+ if (typeof body === "string") {
15
+ const encoded = new TextEncoder().encode(body);
16
+ if (encoded.length > limit)
17
+ throw new ConfigurationError(`reading ${what}: body exceeds ${limit} bytes`);
18
+ return encoded;
19
+ }
20
+ const stream = body instanceof ReadableStream ? body : new Response(body).body;
21
+ if (stream === null)
22
+ return new Uint8Array();
23
+ const reader = stream.getReader();
24
+ const chunks = [];
25
+ let total = 0;
26
+ // One listener serves the whole read: it cancels the stream, which settles
27
+ // a pending read, and rejects the race for a source whose cancel stalls.
28
+ // The listener is removed once the read ends, so a long-lived signal keeps
29
+ // nothing of a completed read.
30
+ let onAbort;
31
+ const aborted = new Promise((_, reject) => {
32
+ if (signal === undefined)
33
+ return;
34
+ onAbort = () => {
35
+ reject(signal.reason);
36
+ void reader.cancel(signal.reason).catch(() => { });
37
+ };
38
+ signal.addEventListener("abort", onAbort, { once: true });
39
+ });
40
+ // An abort between two reads rejects with nothing racing it yet.
41
+ aborted.catch(() => { });
42
+ try {
43
+ for (;;) {
44
+ const { done, value } = await Promise.race([reader.read(), aborted]);
45
+ signal?.throwIfAborted();
46
+ if (done)
47
+ break;
48
+ total += value.length;
49
+ if (total > limit) {
50
+ // Not awaited: a source whose cancel stalls or fails must not hide the size error.
51
+ void reader.cancel().catch(() => { });
52
+ throw new ConfigurationError(`reading ${what}: body exceeds ${limit} bytes`);
53
+ }
54
+ chunks.push(value);
55
+ }
56
+ }
57
+ finally {
58
+ if (onAbort !== undefined)
59
+ signal?.removeEventListener("abort", onAbort);
60
+ }
61
+ const out = new Uint8Array(new ArrayBuffer(total));
62
+ let at = 0;
63
+ for (const chunk of chunks) {
64
+ out.set(chunk, at);
65
+ at += chunk.length;
66
+ }
67
+ return out;
68
+ }
69
+ //# sourceMappingURL=body.js.map