@enlitt/sdk 0.0.0-stage → 0.1.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.
Files changed (108) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/LICENSE +21 -0
  3. package/README.md +61 -2
  4. package/dist/browser/auth.d.cts +44 -0
  5. package/dist/browser/auth.d.ts +45 -0
  6. package/dist/browser/auth.d.ts.map +1 -0
  7. package/dist/browser.cjs +35 -0
  8. package/dist/browser.cjs.map +1 -0
  9. package/dist/browser.d.cts +12 -0
  10. package/dist/browser.d.ts +13 -0
  11. package/dist/browser.d.ts.map +1 -0
  12. package/dist/browser.js +32 -0
  13. package/dist/browser.js.map +1 -0
  14. package/dist/chunk-26SW63P5.cjs +883 -0
  15. package/dist/chunk-26SW63P5.cjs.map +1 -0
  16. package/dist/chunk-6DPBYLA5.js +855 -0
  17. package/dist/chunk-6DPBYLA5.js.map +1 -0
  18. package/dist/chunk-6OWXYBBT.cjs +146 -0
  19. package/dist/chunk-6OWXYBBT.cjs.map +1 -0
  20. package/dist/chunk-C76K6HLL.cjs +906 -0
  21. package/dist/chunk-C76K6HLL.cjs.map +1 -0
  22. package/dist/chunk-G4DTTV5E.cjs +35 -0
  23. package/dist/chunk-G4DTTV5E.cjs.map +1 -0
  24. package/dist/chunk-SFI4RLNN.js +900 -0
  25. package/dist/chunk-SFI4RLNN.js.map +1 -0
  26. package/dist/chunk-V5TCSQ7C.js +143 -0
  27. package/dist/chunk-V5TCSQ7C.js.map +1 -0
  28. package/dist/chunk-VNVMEU34.js +33 -0
  29. package/dist/chunk-VNVMEU34.js.map +1 -0
  30. package/dist/core/errors.d.cts +254 -0
  31. package/dist/core/errors.d.ts +255 -0
  32. package/dist/core/errors.d.ts.map +1 -0
  33. package/dist/core/index.d.cts +24 -0
  34. package/dist/core/index.d.ts +25 -0
  35. package/dist/core/index.d.ts.map +1 -0
  36. package/dist/core/premium.d.cts +230 -0
  37. package/dist/core/premium.d.ts +231 -0
  38. package/dist/core/premium.d.ts.map +1 -0
  39. package/dist/core/session.d.cts +107 -0
  40. package/dist/core/session.d.ts +108 -0
  41. package/dist/core/session.d.ts.map +1 -0
  42. package/dist/core/sse.d.cts +79 -0
  43. package/dist/core/sse.d.ts +80 -0
  44. package/dist/core/sse.d.ts.map +1 -0
  45. package/dist/core/transport.d.cts +223 -0
  46. package/dist/core/transport.d.ts +224 -0
  47. package/dist/core/transport.d.ts.map +1 -0
  48. package/dist/core/usage.d.cts +35 -0
  49. package/dist/core/usage.d.ts +36 -0
  50. package/dist/core/usage.d.ts.map +1 -0
  51. package/dist/core.cjs +73 -0
  52. package/dist/core.cjs.map +1 -0
  53. package/dist/core.js +4 -0
  54. package/dist/core.js.map +1 -0
  55. package/dist/index.cjs +4 -0
  56. package/dist/index.cjs.map +1 -0
  57. package/dist/index.d.cts +16 -0
  58. package/dist/index.d.ts +17 -0
  59. package/dist/index.d.ts.map +1 -0
  60. package/dist/index.js +3 -0
  61. package/dist/index.js.map +1 -0
  62. package/dist/openai/client.d.cts +48 -0
  63. package/dist/openai/client.d.ts +49 -0
  64. package/dist/openai/client.d.ts.map +1 -0
  65. package/dist/openai/index.d.cts +1 -0
  66. package/dist/openai/index.d.ts +2 -0
  67. package/dist/openai/index.d.ts.map +1 -0
  68. package/dist/openai.cjs +479 -0
  69. package/dist/openai.cjs.map +1 -0
  70. package/dist/openai.js +473 -0
  71. package/dist/openai.js.map +1 -0
  72. package/dist/server/callback.d.cts +106 -0
  73. package/dist/server/callback.d.ts +107 -0
  74. package/dist/server/callback.d.ts.map +1 -0
  75. package/dist/server/index.d.cts +19 -0
  76. package/dist/server/index.d.ts +20 -0
  77. package/dist/server/index.d.ts.map +1 -0
  78. package/dist/server/secrets.d.cts +46 -0
  79. package/dist/server/secrets.d.ts +47 -0
  80. package/dist/server/secrets.d.ts.map +1 -0
  81. package/dist/server-unavailable.cjs +8 -0
  82. package/dist/server-unavailable.cjs.map +1 -0
  83. package/dist/server-unavailable.d.cts +10 -0
  84. package/dist/server-unavailable.d.ts +11 -0
  85. package/dist/server-unavailable.d.ts.map +1 -0
  86. package/dist/server-unavailable.js +6 -0
  87. package/dist/server-unavailable.js.map +1 -0
  88. package/dist/server.cjs +354 -0
  89. package/dist/server.cjs.map +1 -0
  90. package/dist/server.js +275 -0
  91. package/dist/server.js.map +1 -0
  92. package/dist/vercel/index.d.cts +15 -0
  93. package/dist/vercel/index.d.ts +16 -0
  94. package/dist/vercel/index.d.ts.map +1 -0
  95. package/dist/vercel/language-model.d.cts +131 -0
  96. package/dist/vercel/language-model.d.ts +132 -0
  97. package/dist/vercel/language-model.d.ts.map +1 -0
  98. package/dist/vercel/premium-language-model.d.cts +98 -0
  99. package/dist/vercel/premium-language-model.d.ts +99 -0
  100. package/dist/vercel/premium-language-model.d.ts.map +1 -0
  101. package/dist/vercel/standard-language-model.d.cts +104 -0
  102. package/dist/vercel/standard-language-model.d.ts +105 -0
  103. package/dist/vercel/standard-language-model.d.ts.map +1 -0
  104. package/dist/vercel.cjs +22 -0
  105. package/dist/vercel.cjs.map +1 -0
  106. package/dist/vercel.js +5 -0
  107. package/dist/vercel.js.map +1 -0
  108. package/package.json +129 -3
@@ -0,0 +1,224 @@
1
+ /**
2
+ * The module's single authenticated exit to Enlitt.
3
+ *
4
+ * `specs/SDK-side routing.md` gives this file three jobs that must live in
5
+ * exactly one place: the definition of the internal client
6
+ * (`InternalEnlittClient`, built once per factory call by
7
+ * `createInternalClient()`), the one place a personal session is resolved
8
+ * (`InternalEnlittClient.resolveSession`), and the one function that ever
9
+ * issues an HTTP request to Enlitt (`requestEnlitt()`). Both the standard and
10
+ * premium model files call through this file rather than `fetch` directly, so
11
+ * every header, every safety check, and every retry the spec requires exists
12
+ * exactly once instead of once per model.
13
+ *
14
+ * It owns exactly two internal retries and no others: the bounded, jittered
15
+ * wait that absorbs `rate_limit_exceeded` (inside `requestEnlitt()` itself,
16
+ * because it applies uniformly to every request this module sends), and a
17
+ * bounded transient retry of a premium terminal report under one unchanged
18
+ * `report_id`/`Idempotency-Key` (`reportEnlittPremiumTerminal()`, layered on
19
+ * top of `requestEnlitt()` because it applies to exactly one call shape and
20
+ * must never leak onto a preflight, a standard call, or a refusal). Every
21
+ * other failure — every refusal, every conflict, every `4xx` other than `429
22
+ * rate_limit_exceeded` — is normalized and thrown once, never retried here.
23
+ *
24
+ * It never accepts a personal identity as a caller-supplied parameter: the
25
+ * only personal value that ever leaves this file is the opaque token
26
+ * `client.resolveSession()` returns, and no argument, option, or request body
27
+ * field can supply or override it. It never logs or returns that token,
28
+ * prompt content, or a raw provider error, and it never runs in a browser —
29
+ * it is reachable only through `@enlitt/sdk/server`, which the package export
30
+ * map keeps out of the `browser` condition.
31
+ */
32
+ import { EnlittError } from "./errors.js";
33
+ import { type EnlittSession } from "./session.js";
34
+ /**
35
+ * The SDK's own version, sent as `X-Enlitt-SDK-Version` so Enlitt's own logs
36
+ * can tell which client behavior produced a given request without the header
37
+ * carrying anything about the business or the user.
38
+ *
39
+ * A literal rather than a `package.json` import: this package ships as both
40
+ * ESM and CJS, its `tsup` build does not embed the manifest, and a JSON
41
+ * import here would tie a request-time value to a file layout assumption
42
+ * this module has no other reason to make. It must be bumped by hand
43
+ * alongside `package.json`'s `version` field.
44
+ */
45
+ export declare const SDK_VERSION = "0.1.0";
46
+ /**
47
+ * Caller-supplied tuning for the rate-limit wait. Every field is optional
48
+ * because `createInternalClient()` folds it over the SDK's own defaults
49
+ * exactly once, at construction, which is what lets every later read of
50
+ * `client.rateLimit` assume every field but `onRateLimit` is present.
51
+ */
52
+ export interface EnlittRateLimitRetryOptions {
53
+ /** Upper bound on attempts, including the first (minimum 1). Default {@link DEFAULT_RATE_LIMIT_MAX_ATTEMPTS}. */
54
+ readonly maxAttempts?: number;
55
+ /** Upper bound on total time spent waiting, measured from the first refusal. Default {@link DEFAULT_RATE_LIMIT_MAX_TOTAL_WAIT_MS}. */
56
+ readonly maxTotalWaitMs?: number;
57
+ /**
58
+ * Invoked once per wait, with the attempt number (1-based) about to be
59
+ * slept and the jittered delay in milliseconds. The only way a business
60
+ * observes a rate-limit wait without this module disclosing a plan fact.
61
+ */
62
+ readonly onRateLimit?: (attempt: number, delayMs: number) => void;
63
+ }
64
+ /**
65
+ * Constructor options for `createInternalClient()`.
66
+ *
67
+ * `secretKey` and `baseUrl` are exactly what `resolveBusinessAuthorizationHeader()`
68
+ * and the HTTPS assertion need and nothing more; neither this type nor the
69
+ * client it builds ever carries a personal identity, because the only
70
+ * personal value the transport ever sends is the token `resolveSession()`
71
+ * (built from `session`, or from the ambient context) produces at request
72
+ * time.
73
+ */
74
+ export interface EnlittOptions {
75
+ /** Forwarded to `resolveBusinessAuthorizationHeader()` unchanged. */
76
+ readonly secretKey?: string;
77
+ /**
78
+ * Enlitt's base URL. Resolves from this option, then `ENLITT_BASE_URL`, then
79
+ * the deployed Enlitt origin compiled into the package. Must be `https:`, or
80
+ * `http:` on a local host. Parsed and validated once, at construction.
81
+ */
82
+ readonly baseUrl?: string;
83
+ /**
84
+ * An explicit session, for a caller that cannot rely on the ambient
85
+ * `AsyncLocalStorage` context — a background job, or a framework
86
+ * `enlittSessionMiddleware()` cannot wrap. When absent, every call resolves
87
+ * the session from `getEnlittSession()` instead.
88
+ */
89
+ readonly session?: EnlittSession;
90
+ readonly rateLimit?: EnlittRateLimitRetryOptions;
91
+ /**
92
+ * Observes a failure `reportEnlittPremiumTerminal()` could not recover
93
+ * from after exhausting its bounded retry. There is no caller left to
94
+ * return the error to by the time this fires — the provider call this
95
+ * report describes has already completed — so this hook is the only way a
96
+ * business ever learns a terminal report did not land.
97
+ */
98
+ readonly onTerminalReportError?: (error: EnlittError) => void;
99
+ }
100
+ /**
101
+ * The internal client every request in this module is built from.
102
+ *
103
+ * Constructed once per factory call from the business's resolved secret and
104
+ * the deployed environment, and never held as nullable: holding a client at
105
+ * all is what makes a call managed, so there is no code path in this package
106
+ * that represents "no Enlitt configuration" as a null or optional client
107
+ * instead of failing at construction.
108
+ */
109
+ export interface InternalEnlittClient {
110
+ readonly baseUrl: string;
111
+ /** The full `Authorization` header value, e.g. `Bearer sk_...`. Never logged or returned. */
112
+ readonly businessAuthorization: string;
113
+ readonly sdkVersion: string;
114
+ /**
115
+ * The module's single session resolution. Returns the explicit
116
+ * `EnlittOptions.session` when one was given; otherwise defers to
117
+ * `getEnlittSession()`, which itself distinguishes "no middleware
118
+ * installed" (`EnlittConfigurationError`) from "signed out" (`null`,
119
+ * raised here as `EnlittLoginRequiredError` with `providerInvoked: false`).
120
+ * Both model files call this indirectly through `requestEnlitt()` rather
121
+ * than resolving a session themselves, which is what keeps the three
122
+ * outcomes from drifting apart between the standard and premium paths.
123
+ */
124
+ readonly resolveSession: () => EnlittSession;
125
+ readonly rateLimit: Required<Omit<EnlittRateLimitRetryOptions, "onRateLimit">> & Pick<EnlittRateLimitRetryOptions, "onRateLimit">;
126
+ readonly onTerminalReportError?: (error: EnlittError) => void;
127
+ }
128
+ /**
129
+ * Builds the internal client from a factory call's resolved options.
130
+ *
131
+ * Resolves the business authorization header eagerly (through
132
+ * `resolveBusinessAuthorizationHeader()`, the only path to that secret) so a
133
+ * missing or unusable key fails at construction, before any network call and
134
+ * before a provider is ever at risk of running unaccounted. Folds
135
+ * `rateLimit` over the SDK's defaults exactly once here, so no per-request
136
+ * code has to treat any field but `onRateLimit` as possibly absent.
137
+ */
138
+ export declare function createInternalClient(options: EnlittOptions): InternalEnlittClient;
139
+ /** One request this module knows how to issue. */
140
+ export interface EnlittRequestInit {
141
+ readonly path: string;
142
+ /** `POST` unless a read endpoint (`GET /v1/enlitt/usage`) says otherwise. */
143
+ readonly method?: "POST" | "GET";
144
+ /** Ignored for `GET`, which has no body. */
145
+ readonly body?: unknown;
146
+ /**
147
+ * Sent as `Idempotency-Key` (and, for a terminal report, equals the body's
148
+ * own `report_id`). Required for `POST`; a `GET` sends no `Idempotency-Key`,
149
+ * because `PROTOCOLS.md` forbids one on a route that does not deduplicate.
150
+ */
151
+ readonly callId?: string;
152
+ readonly accept: "application/json" | "text/event-stream";
153
+ readonly signal?: AbortSignal;
154
+ }
155
+ /**
156
+ * Issues one `POST` to `client.baseUrl + init.path` carrying every header the
157
+ * protocol requires and no other identity-bearing header, and returns the
158
+ * successful `Response` unread so a caller that needs a stream can consume
159
+ * it incrementally.
160
+ *
161
+ * This is the only function in the package that calls `fetch()` against
162
+ * Enlitt. Every safety property the spec requires — HTTPS, no redirects,
163
+ * bounded response size, normalization of every failure, and the
164
+ * `rate_limit_exceeded` wait — lives here so no call site can accidentally
165
+ * skip one.
166
+ */
167
+ export declare function requestEnlitt(client: InternalEnlittClient, init: EnlittRequestInit): Promise<Response>;
168
+ /**
169
+ * Retries a premium terminal report a bounded number of times on a
170
+ * transient failure, resending the identical `init` — the same body (and so
171
+ * the same `report_id`) and the same `Idempotency-Key` — on every attempt,
172
+ * which is what makes the retry safe: Enlitt's own idempotency on that key
173
+ * turns a duplicate send into a replay rather than a second report.
174
+ *
175
+ * Only a failure with no distinct business action of its own is treated as
176
+ * transient: a transport failure that never produced a response, or one of
177
+ * the three retryable server-side wire codes (`server_error`,
178
+ * `provider_error`, `no_capable_route`). A refusal, a conflict, or an
179
+ * exhausted `rate_limit_exceeded` wait (already retried once, inside
180
+ * `requestEnlitt()`) is never retried again here — retrying a refusal a
181
+ * second time under a different name is exactly what the spec forbids.
182
+ *
183
+ * Exhausting the bound reports the last error to `client.onTerminalReportError`
184
+ * and rethrows it, rather than swallowing it: the hook exists because the
185
+ * provider call this report describes has already finished and there is no
186
+ * result left to withhold from a caller, not because the failure stops
187
+ * mattering.
188
+ */
189
+ export declare function reportEnlittPremiumTerminal(client: InternalEnlittClient, init: EnlittRequestInit): Promise<Response>;
190
+ /**
191
+ * True for an unfollowed redirect under `redirect: "manual"`: a `3xx` status
192
+ * (server runtimes such as Workers and Node) or an `opaqueredirect` response
193
+ * with status `0` (browsers, which hide the redirect's details).
194
+ */
195
+ export declare function isRedirectResponse(response: Response): boolean;
196
+ /**
197
+ * The `transport_failed` error for a refused redirect. The body is cancelled
198
+ * unread so the connection is released; nothing from it is used.
199
+ */
200
+ export declare function redirectRefused(response: Response): EnlittError;
201
+ /**
202
+ * Reads a successful response's body as JSON, bounded by the same body-size
203
+ * limit every other read in this file uses. An oversized body or one that is
204
+ * not valid JSON is a `malformed_response` `EnlittError`, never a raw parser
205
+ * exception. Every JSON-bodied Enlitt response (begin, terminal report, usage,
206
+ * non-streaming standard response) is read through here, so no caller buffers
207
+ * an unbounded body or parses JSON on its own.
208
+ */
209
+ export declare function readEnlittJson(response: Response): Promise<unknown>;
210
+ /**
211
+ * HTTPS is required outside local development. `http:` is accepted only for
212
+ * the three hosts a developer's own machine can present as, and there is no
213
+ * environment flag that relaxes this — a business misconfiguring its own
214
+ * deployed base URL must fail loudly rather than send its business and
215
+ * personal credentials in the clear.
216
+ *
217
+ * Exported (in addition to being used per-request by `requestEnlitt()`) so
218
+ * `language-model.ts` can run the identical check once, at `enlitt()`
219
+ * construction, against the resolved `baseUrl` before any request is made —
220
+ * the spec requires base URL validation to happen at construction, not per
221
+ * call, and duplicating this rule there would let the two copies drift.
222
+ */
223
+ export declare function assertHttps(baseUrl: string): void;
224
+ //# sourceMappingURL=transport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../../src/core/transport.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAGH,OAAO,EAEL,WAAW,EAMZ,MAAM,aAAa,CAAC;AAErB,OAAO,EAAoB,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AAEpE;;;;;;;;;;GAUG;AACH,eAAO,MAAM,WAAW,UAAU,CAAC;AAuDnC;;;;;GAKG;AACH,MAAM,WAAW,2BAA2B;IAC1C,iHAAiH;IACjH,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,sIAAsI;IACtI,QAAQ,CAAC,cAAc,CAAC,EAAE,MAA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M,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IACjC,4CAA4C;IAC5C,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;IACxB;;;;OAIG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,kBAAkB,GAAG,mBAAmB,CAAC;IAC1D,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAC/B;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,aAAa,CAAC,MAAM,EAAE,oBAAoB,EAAE,IAAI,EAAE,iBAAiB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsD5G;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,2BAA2B,CAC/C,MAAM,EAAE,oBAAoB,EAC5B,IAAI,EAAE,iBAAiB,GACtB,OAAO,CAAC,QAAQ,CAAC,CAcnB;AA4DD;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAE9D;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,QAAQ,GAAG,WAAW,CAM/D;AA6GD;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,CAgBzE;AAiCD;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAYjD"}
@@ -0,0 +1,35 @@
1
+ /**
2
+ * The framework-neutral `usage()` helper over `GET /v1/enlitt/usage`.
3
+ *
4
+ * It builds the same internal client as `withPremiumCharge()`, resolves the
5
+ * same personal session (explicit, or ambient from the middleware), performs
6
+ * one authenticated read, and tolerant-parses the response. It returns the two
7
+ * integer percentages and the canonical NANOUSD caps unchanged: no conversion,
8
+ * caching, retry beyond transport policy, or admission inference.
9
+ *
10
+ * The result is advisory. A concurrent call can consume capacity before the
11
+ * caller acts, so it is guidance and never a reservation or quote. It exposes
12
+ * no credit unit, plan, balance, period, rate tuple, or user identity; the wire
13
+ * response carries none and this file adds none.
14
+ */
15
+ import { type EnlittOptions } from "./transport.cjs";
16
+ /** One pool's availability, exactly as the wire reports it. */
17
+ export interface UsageAvailability {
18
+ /** An integer from 0 through 100: the floor of the share of this pool still available. */
19
+ readonly remainingPercent: number;
20
+ /** The most this application could reserve for one call right now, as a canonical NANOUSD string. */
21
+ readonly limitCapNanoUsd: string;
22
+ }
23
+ /**
24
+ * Reads the signed-in user's current availability for this application.
25
+ *
26
+ * A missing ambient context raises `EnlittConfigurationError`, a context with
27
+ * no session raises `EnlittLoginRequiredError`, and every refusal (including an
28
+ * unknown error code from a newer server) goes through the shared normalizer,
29
+ * exactly as for a model call. It never raises `spending_limit_exceeded`: the
30
+ * read starts no request-rate budget and reserves nothing.
31
+ */
32
+ export declare function usage(options?: EnlittOptions): Promise<{
33
+ readonly standard: UsageAvailability;
34
+ readonly premium: UsageAvailability;
35
+ }>;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * The framework-neutral `usage()` helper over `GET /v1/enlitt/usage`.
3
+ *
4
+ * It builds the same internal client as `withPremiumCharge()`, resolves the
5
+ * same personal session (explicit, or ambient from the middleware), performs
6
+ * one authenticated read, and tolerant-parses the response. It returns the two
7
+ * integer percentages and the canonical NANOUSD caps unchanged: no conversion,
8
+ * caching, retry beyond transport policy, or admission inference.
9
+ *
10
+ * The result is advisory. A concurrent call can consume capacity before the
11
+ * caller acts, so it is guidance and never a reservation or quote. It exposes
12
+ * no credit unit, plan, balance, period, rate tuple, or user identity; the wire
13
+ * response carries none and this file adds none.
14
+ */
15
+ import { type EnlittOptions } from "./transport.js";
16
+ /** One pool's availability, exactly as the wire reports it. */
17
+ export interface UsageAvailability {
18
+ /** An integer from 0 through 100: the floor of the share of this pool still available. */
19
+ readonly remainingPercent: number;
20
+ /** The most this application could reserve for one call right now, as a canonical NANOUSD string. */
21
+ readonly limitCapNanoUsd: string;
22
+ }
23
+ /**
24
+ * Reads the signed-in user's current availability for this application.
25
+ *
26
+ * A missing ambient context raises `EnlittConfigurationError`, a context with
27
+ * no session raises `EnlittLoginRequiredError`, and every refusal (including an
28
+ * unknown error code from a newer server) goes through the shared normalizer,
29
+ * exactly as for a model call. It never raises `spending_limit_exceeded`: the
30
+ * read starts no request-rate budget and reserves nothing.
31
+ */
32
+ export declare function usage(options?: EnlittOptions): Promise<{
33
+ readonly standard: UsageAvailability;
34
+ readonly premium: UsageAvailability;
35
+ }>;
36
+ //# sourceMappingURL=usage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usage.d.ts","sourceRoot":"","sources":["../../src/core/usage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,EAAuD,KAAK,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAGzG,+DAA+D;AAC/D,MAAM,WAAW,iBAAiB;IAChC,0FAA0F;IAC1F,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;IAClC,qGAAqG;IACrG,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;CAClC;AAKD;;;;;;;;GAQG;AACH,wBAAsB,KAAK,CAAC,OAAO,GAAE,aAAkB,GAAG,OAAO,CAAC;IAAE,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,iBAAiB,CAAA;CAAE,CAAC,CA6B/I"}
package/dist/core.cjs ADDED
@@ -0,0 +1,73 @@
1
+ 'use strict';
2
+
3
+ var chunkG4DTTV5E_cjs = require('./chunk-G4DTTV5E.cjs');
4
+ var chunk26SW63P5_cjs = require('./chunk-26SW63P5.cjs');
5
+
6
+
7
+
8
+ Object.defineProperty(exports, "usage", {
9
+ enumerable: true,
10
+ get: function () { return chunkG4DTTV5E_cjs.usage; }
11
+ });
12
+ Object.defineProperty(exports, "ENLITT_SESSION_COOKIE_NAME", {
13
+ enumerable: true,
14
+ get: function () { return chunk26SW63P5_cjs.ENLITT_SESSION_COOKIE_NAME; }
15
+ });
16
+ Object.defineProperty(exports, "EnlittAuthenticationError", {
17
+ enumerable: true,
18
+ get: function () { return chunk26SW63P5_cjs.EnlittAuthenticationError; }
19
+ });
20
+ Object.defineProperty(exports, "EnlittAuthorizationError", {
21
+ enumerable: true,
22
+ get: function () { return chunk26SW63P5_cjs.EnlittAuthorizationError; }
23
+ });
24
+ Object.defineProperty(exports, "EnlittConfigurationError", {
25
+ enumerable: true,
26
+ get: function () { return chunk26SW63P5_cjs.EnlittConfigurationError; }
27
+ });
28
+ Object.defineProperty(exports, "EnlittError", {
29
+ enumerable: true,
30
+ get: function () { return chunk26SW63P5_cjs.EnlittError; }
31
+ });
32
+ Object.defineProperty(exports, "EnlittInsufficientCreditsError", {
33
+ enumerable: true,
34
+ get: function () { return chunk26SW63P5_cjs.EnlittInsufficientCreditsError; }
35
+ });
36
+ Object.defineProperty(exports, "EnlittLimitExceededError", {
37
+ enumerable: true,
38
+ get: function () { return chunk26SW63P5_cjs.EnlittLimitExceededError; }
39
+ });
40
+ Object.defineProperty(exports, "EnlittLoginRequiredError", {
41
+ enumerable: true,
42
+ get: function () { return chunk26SW63P5_cjs.EnlittLoginRequiredError; }
43
+ });
44
+ Object.defineProperty(exports, "EnlittRateLimitError", {
45
+ enumerable: true,
46
+ get: function () { return chunk26SW63P5_cjs.EnlittRateLimitError; }
47
+ });
48
+ Object.defineProperty(exports, "EnlittSubscriptionInactiveError", {
49
+ enumerable: true,
50
+ get: function () { return chunk26SW63P5_cjs.EnlittSubscriptionInactiveError; }
51
+ });
52
+ Object.defineProperty(exports, "enlittSession", {
53
+ enumerable: true,
54
+ get: function () { return chunk26SW63P5_cjs.enlittSession; }
55
+ });
56
+ Object.defineProperty(exports, "enlittSessionMiddleware", {
57
+ enumerable: true,
58
+ get: function () { return chunk26SW63P5_cjs.enlittSessionMiddleware; }
59
+ });
60
+ Object.defineProperty(exports, "getEnlittSession", {
61
+ enumerable: true,
62
+ get: function () { return chunk26SW63P5_cjs.getEnlittSession; }
63
+ });
64
+ Object.defineProperty(exports, "runWithEnlittSession", {
65
+ enumerable: true,
66
+ get: function () { return chunk26SW63P5_cjs.runWithEnlittSession; }
67
+ });
68
+ Object.defineProperty(exports, "withPremiumCharge", {
69
+ enumerable: true,
70
+ get: function () { return chunk26SW63P5_cjs.withPremiumCharge; }
71
+ });
72
+ //# sourceMappingURL=core.cjs.map
73
+ //# sourceMappingURL=core.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"core.cjs"}
package/dist/core.js ADDED
@@ -0,0 +1,4 @@
1
+ export { usage } from './chunk-VNVMEU34.js';
2
+ export { ENLITT_SESSION_COOKIE_NAME, EnlittAuthenticationError, EnlittAuthorizationError, EnlittConfigurationError, EnlittError, EnlittInsufficientCreditsError, EnlittLimitExceededError, EnlittLoginRequiredError, EnlittRateLimitError, EnlittSubscriptionInactiveError, enlittSession, enlittSessionMiddleware, getEnlittSession, runWithEnlittSession, withPremiumCharge } from './chunk-6DPBYLA5.js';
3
+ //# sourceMappingURL=core.js.map
4
+ //# sourceMappingURL=core.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"core.js"}
package/dist/index.cjs ADDED
@@ -0,0 +1,4 @@
1
+ 'use strict';
2
+
3
+ //# sourceMappingURL=index.cjs.map
4
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"index.cjs"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * `@enlitt/sdk` — the isomorphic entry point.
3
+ *
4
+ * Only declarations that are safe in both a browser bundle and a server
5
+ * runtime belong here. Anything that reads a secret lives behind
6
+ * `@enlitt/sdk/server`; anything that touches `window` lives behind
7
+ * `@enlitt/sdk/browser`. `docs/ARCHITECTURE.md` keeps that split, and the
8
+ * `browser` export condition in `package.json` enforces it at bundle time.
9
+ *
10
+ * Account contributes nothing here on purpose. Its browser half is a pair of
11
+ * navigations that only make sense in a browser, and its server half handles
12
+ * the business secret; a session type exported from here would be the first
13
+ * step towards a browser-readable session, which is exactly what the HttpOnly
14
+ * cookie exists to prevent.
15
+ */
16
+ export {};
@@ -0,0 +1,17 @@
1
+ /**
2
+ * `@enlitt/sdk` — the isomorphic entry point.
3
+ *
4
+ * Only declarations that are safe in both a browser bundle and a server
5
+ * runtime belong here. Anything that reads a secret lives behind
6
+ * `@enlitt/sdk/server`; anything that touches `window` lives behind
7
+ * `@enlitt/sdk/browser`. `docs/ARCHITECTURE.md` keeps that split, and the
8
+ * `browser` export condition in `package.json` enforces it at bundle time.
9
+ *
10
+ * Account contributes nothing here on purpose. Its browser half is a pair of
11
+ * navigations that only make sense in a browser, and its server half handles
12
+ * the business secret; a session type exported from here would be the first
13
+ * step towards a browser-readable session, which is exactly what the HttpOnly
14
+ * cookie exists to prevent.
15
+ */
16
+ export {};
17
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+
2
+ //# sourceMappingURL=index.js.map
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
@@ -0,0 +1,48 @@
1
+ /** Thin OpenAI Responses adapter over Enlitt's existing standard transport and premium lifecycle. */
2
+ import { ENLITT_STANDARD_MODEL } from "@enlitt/protocol";
3
+ import type OpenAI from "openai";
4
+ import type { Response as OpenAIResponseResult, ResponseCreateParamsNonStreaming, ResponseCreateParamsStreaming, ResponseStreamEvent, ResponseUsage } from "openai/resources/responses/responses";
5
+ import { type EnlittOptions } from "../core/transport.cjs";
6
+ export { ENLITT_STANDARD_MODEL };
7
+ export interface EnlittOpenAIRequestOptions {
8
+ readonly signal?: AbortSignal;
9
+ readonly timeout?: number;
10
+ }
11
+ export interface EnlittOpenAICallOptions {
12
+ readonly idempotencyKey?: string;
13
+ readonly maxCharge?: string;
14
+ readonly price?: EnlittOpenAIPrice;
15
+ readonly reportingDeadlineMs?: number;
16
+ readonly metadata?: Record<string, string>;
17
+ }
18
+ export interface OpenAIPremiumPriceContext {
19
+ readonly status: "completed" | "canceled";
20
+ readonly response?: OpenAIResponseResult;
21
+ readonly events?: readonly ResponseStreamEvent[];
22
+ readonly usage?: ResponseUsage;
23
+ readonly startedAt: Date;
24
+ readonly endedAt: Date;
25
+ readonly durationMs: number;
26
+ readonly timeToFirstTokenMs?: number;
27
+ readonly effectiveMaxChargeNanoUsd: string;
28
+ }
29
+ export type EnlittOpenAIPrice = string | ((context: OpenAIPremiumPriceContext) => string | Promise<string>);
30
+ export type EnlittOpenAIResponseCreateParamsNonStreaming = ResponseCreateParamsNonStreaming & {
31
+ readonly enlitt?: EnlittOpenAICallOptions;
32
+ };
33
+ export type EnlittOpenAIResponseCreateParamsStreaming = ResponseCreateParamsStreaming & {
34
+ readonly enlitt?: EnlittOpenAICallOptions;
35
+ };
36
+ export type EnlittOpenAIStream = AsyncIterable<ResponseStreamEvent>;
37
+ export interface EnlittOpenAIClient {
38
+ readonly responses: {
39
+ create(body: EnlittOpenAIResponseCreateParamsNonStreaming, options?: EnlittOpenAIRequestOptions): Promise<OpenAIResponseResult>;
40
+ create(body: EnlittOpenAIResponseCreateParamsStreaming, options?: EnlittOpenAIRequestOptions): Promise<EnlittOpenAIStream>;
41
+ };
42
+ }
43
+ export declare function enlitt(options?: EnlittOptions): EnlittOpenAIClient;
44
+ export declare function enlitt(client: OpenAI, options?: EnlittOptions): EnlittOpenAIClient;
45
+ export declare function perToken(rates: {
46
+ readonly input: string;
47
+ readonly output: string;
48
+ }): (context: OpenAIPremiumPriceContext) => string;
@@ -0,0 +1,49 @@
1
+ /** Thin OpenAI Responses adapter over Enlitt's existing standard transport and premium lifecycle. */
2
+ import { ENLITT_STANDARD_MODEL } from "@enlitt/protocol";
3
+ import type OpenAI from "openai";
4
+ import type { Response as OpenAIResponseResult, ResponseCreateParamsNonStreaming, ResponseCreateParamsStreaming, ResponseStreamEvent, ResponseUsage } from "openai/resources/responses/responses";
5
+ import { type EnlittOptions } from "../core/transport.js";
6
+ export { ENLITT_STANDARD_MODEL };
7
+ export interface EnlittOpenAIRequestOptions {
8
+ readonly signal?: AbortSignal;
9
+ readonly timeout?: number;
10
+ }
11
+ export interface EnlittOpenAICallOptions {
12
+ readonly idempotencyKey?: string;
13
+ readonly maxCharge?: string;
14
+ readonly price?: EnlittOpenAIPrice;
15
+ readonly reportingDeadlineMs?: number;
16
+ readonly metadata?: Record<string, string>;
17
+ }
18
+ export interface OpenAIPremiumPriceContext {
19
+ readonly status: "completed" | "canceled";
20
+ readonly response?: OpenAIResponseResult;
21
+ readonly events?: readonly ResponseStreamEvent[];
22
+ readonly usage?: ResponseUsage;
23
+ readonly startedAt: Date;
24
+ readonly endedAt: Date;
25
+ readonly durationMs: number;
26
+ readonly timeToFirstTokenMs?: number;
27
+ readonly effectiveMaxChargeNanoUsd: string;
28
+ }
29
+ export type EnlittOpenAIPrice = string | ((context: OpenAIPremiumPriceContext) => string | Promise<string>);
30
+ export type EnlittOpenAIResponseCreateParamsNonStreaming = ResponseCreateParamsNonStreaming & {
31
+ readonly enlitt?: EnlittOpenAICallOptions;
32
+ };
33
+ export type EnlittOpenAIResponseCreateParamsStreaming = ResponseCreateParamsStreaming & {
34
+ readonly enlitt?: EnlittOpenAICallOptions;
35
+ };
36
+ export type EnlittOpenAIStream = AsyncIterable<ResponseStreamEvent>;
37
+ export interface EnlittOpenAIClient {
38
+ readonly responses: {
39
+ create(body: EnlittOpenAIResponseCreateParamsNonStreaming, options?: EnlittOpenAIRequestOptions): Promise<OpenAIResponseResult>;
40
+ create(body: EnlittOpenAIResponseCreateParamsStreaming, options?: EnlittOpenAIRequestOptions): Promise<EnlittOpenAIStream>;
41
+ };
42
+ }
43
+ export declare function enlitt(options?: EnlittOptions): EnlittOpenAIClient;
44
+ export declare function enlitt(client: OpenAI, options?: EnlittOptions): EnlittOpenAIClient;
45
+ export declare function perToken(rates: {
46
+ readonly input: string;
47
+ readonly output: string;
48
+ }): (context: OpenAIPremiumPriceContext) => string;
49
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/openai/client.ts"],"names":[],"mappings":"AAAA,qGAAqG;AACrG,OAAO,EACL,qBAAqB,EAQtB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,KAAK,EACV,QAAQ,IAAI,oBAAoB,EAChC,gCAAgC,EAChC,6BAA6B,EAC7B,mBAAmB,EACnB,aAAa,EACd,MAAM,sCAAsC,CAAC;AAc9C,OAAO,EAIL,KAAK,aAAa,EAEnB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,qBAAqB,EAAE,CAAC;AAEjC,MAAM,WAAW,0BAA0B;IACzC,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;IAC9B,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,KAAK,CAAC,EAAE,iBAAiB,CAAC;IACnC,QAAQ,CAAC,mBAAmB,CAAC,EAAE,MAAM,CAAC;IACtC,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC5C;AAED,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,MAAM,EAAE,WAAW,GAAG,UAAU,CAAC;IAC1C,QAAQ,CAAC,QAAQ,CAAC,EAAE,oBAAo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,CAAC;AA6gBpF,wBAAgB,QAAQ,CAAC,KAAK,EAAE;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,CAAC,OAAO,EAAE,yBAAyB,KAAK,MAAM,CAInI"}
@@ -0,0 +1 @@
1
+ export { ENLITT_STANDARD_MODEL, enlitt, perToken, type EnlittOpenAICallOptions, type EnlittOpenAIClient, type EnlittOpenAIPrice, type EnlittOpenAIRequestOptions, type EnlittOpenAIResponseCreateParamsNonStreaming, type EnlittOpenAIResponseCreateParamsStreaming, type EnlittOpenAIStream, type OpenAIPremiumPriceContext } from "./client.cjs";
@@ -0,0 +1,2 @@
1
+ export { ENLITT_STANDARD_MODEL, enlitt, perToken, type EnlittOpenAICallOptions, type EnlittOpenAIClient, type EnlittOpenAIPrice, type EnlittOpenAIRequestOptions, type EnlittOpenAIResponseCreateParamsNonStreaming, type EnlittOpenAIResponseCreateParamsStreaming, type EnlittOpenAIStream, type OpenAIPremiumPriceContext } from "./client.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/openai/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,MAAM,EACN,QAAQ,EACR,KAAK,uBAAuB,EAC5B,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,EACtB,KAAK,0BAA0B,EAC/B,KAAK,4CAA4C,EACjD,KAAK,yCAAyC,EAC9C,KAAK,kBAAkB,EACvB,KAAK,yBAAyB,EAC/B,MAAM,aAAa,CAAC"}