@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.
- package/CHANGELOG.md +8 -0
- package/LICENSE +21 -0
- package/README.md +61 -2
- package/dist/browser/auth.d.cts +44 -0
- package/dist/browser/auth.d.ts +45 -0
- package/dist/browser/auth.d.ts.map +1 -0
- package/dist/browser.cjs +35 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.cts +12 -0
- package/dist/browser.d.ts +13 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +32 -0
- package/dist/browser.js.map +1 -0
- package/dist/chunk-26SW63P5.cjs +883 -0
- package/dist/chunk-26SW63P5.cjs.map +1 -0
- package/dist/chunk-6DPBYLA5.js +855 -0
- package/dist/chunk-6DPBYLA5.js.map +1 -0
- package/dist/chunk-6OWXYBBT.cjs +146 -0
- package/dist/chunk-6OWXYBBT.cjs.map +1 -0
- package/dist/chunk-C76K6HLL.cjs +906 -0
- package/dist/chunk-C76K6HLL.cjs.map +1 -0
- package/dist/chunk-G4DTTV5E.cjs +35 -0
- package/dist/chunk-G4DTTV5E.cjs.map +1 -0
- package/dist/chunk-SFI4RLNN.js +900 -0
- package/dist/chunk-SFI4RLNN.js.map +1 -0
- package/dist/chunk-V5TCSQ7C.js +143 -0
- package/dist/chunk-V5TCSQ7C.js.map +1 -0
- package/dist/chunk-VNVMEU34.js +33 -0
- package/dist/chunk-VNVMEU34.js.map +1 -0
- package/dist/core/errors.d.cts +254 -0
- package/dist/core/errors.d.ts +255 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/index.d.cts +24 -0
- package/dist/core/index.d.ts +25 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/premium.d.cts +230 -0
- package/dist/core/premium.d.ts +231 -0
- package/dist/core/premium.d.ts.map +1 -0
- package/dist/core/session.d.cts +107 -0
- package/dist/core/session.d.ts +108 -0
- package/dist/core/session.d.ts.map +1 -0
- package/dist/core/sse.d.cts +79 -0
- package/dist/core/sse.d.ts +80 -0
- package/dist/core/sse.d.ts.map +1 -0
- package/dist/core/transport.d.cts +223 -0
- package/dist/core/transport.d.ts +224 -0
- package/dist/core/transport.d.ts.map +1 -0
- package/dist/core/usage.d.cts +35 -0
- package/dist/core/usage.d.ts +36 -0
- package/dist/core/usage.d.ts.map +1 -0
- package/dist/core.cjs +73 -0
- package/dist/core.cjs.map +1 -0
- package/dist/core.js +4 -0
- package/dist/core.js.map +1 -0
- package/dist/index.cjs +4 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +16 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/openai/client.d.cts +48 -0
- package/dist/openai/client.d.ts +49 -0
- package/dist/openai/client.d.ts.map +1 -0
- package/dist/openai/index.d.cts +1 -0
- package/dist/openai/index.d.ts +2 -0
- package/dist/openai/index.d.ts.map +1 -0
- package/dist/openai.cjs +479 -0
- package/dist/openai.cjs.map +1 -0
- package/dist/openai.js +473 -0
- package/dist/openai.js.map +1 -0
- package/dist/server/callback.d.cts +106 -0
- package/dist/server/callback.d.ts +107 -0
- package/dist/server/callback.d.ts.map +1 -0
- package/dist/server/index.d.cts +19 -0
- package/dist/server/index.d.ts +20 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/secrets.d.cts +46 -0
- package/dist/server/secrets.d.ts +47 -0
- package/dist/server/secrets.d.ts.map +1 -0
- package/dist/server-unavailable.cjs +8 -0
- package/dist/server-unavailable.cjs.map +1 -0
- package/dist/server-unavailable.d.cts +10 -0
- package/dist/server-unavailable.d.ts +11 -0
- package/dist/server-unavailable.d.ts.map +1 -0
- package/dist/server-unavailable.js +6 -0
- package/dist/server-unavailable.js.map +1 -0
- package/dist/server.cjs +354 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.js +275 -0
- package/dist/server.js.map +1 -0
- package/dist/vercel/index.d.cts +15 -0
- package/dist/vercel/index.d.ts +16 -0
- package/dist/vercel/index.d.ts.map +1 -0
- package/dist/vercel/language-model.d.cts +131 -0
- package/dist/vercel/language-model.d.ts +132 -0
- package/dist/vercel/language-model.d.ts.map +1 -0
- package/dist/vercel/premium-language-model.d.cts +98 -0
- package/dist/vercel/premium-language-model.d.ts +99 -0
- package/dist/vercel/premium-language-model.d.ts.map +1 -0
- package/dist/vercel/standard-language-model.d.cts +104 -0
- package/dist/vercel/standard-language-model.d.ts +105 -0
- package/dist/vercel/standard-language-model.d.ts.map +1 -0
- package/dist/vercel.cjs +22 -0
- package/dist/vercel.cjs.map +1 -0
- package/dist/vercel.js +5 -0
- package/dist/vercel.js.map +1 -0
- package/package.json +129 -3
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one premium billing lifecycle, with no framework dependency.
|
|
3
|
+
*
|
|
4
|
+
* `specs/SDK-side routing.md` gives this file the framework-agnostic core of
|
|
5
|
+
* premium mode: a business runs a model call it pays for, and Enlitt only
|
|
6
|
+
* accounts for it. The call is bracketed by a content-free begin (a maximum
|
|
7
|
+
* charge in NANOUSD, declared before the provider runs) and exactly one
|
|
8
|
+
* terminal report (a status and the actual NANOUSD charge, sent after it).
|
|
9
|
+
*
|
|
10
|
+
* `runPremiumCharge()` is the single implementation of that lifecycle: one
|
|
11
|
+
* begin, one status rule, one charge rule, one report path. The public
|
|
12
|
+
* `withPremiumCharge()` in this file and the Vercel adapter in
|
|
13
|
+
* `vercel/premium-language-model.ts` are both thin layers over it, so moving a
|
|
14
|
+
* call between the two changes how the charge is computed and nothing about how
|
|
15
|
+
* it is settled.
|
|
16
|
+
*
|
|
17
|
+
* This file imports nothing from the Vercel AI SDK, and it never sends prompts,
|
|
18
|
+
* messages, outputs, files, tools, provider options, provider keys, raw errors,
|
|
19
|
+
* or content hashes to Enlitt: every body it builds is assembled field by field
|
|
20
|
+
* from bounded named inputs, never by forwarding a caller-supplied object.
|
|
21
|
+
*
|
|
22
|
+
* Every NANOUSD amount is a canonical base-10 integer string and is compared
|
|
23
|
+
* only as a `bigint`; none is ever converted to `number`, rounded, or displayed.
|
|
24
|
+
*/
|
|
25
|
+
import { type EnlittOptions, type InternalEnlittClient } from "./transport.cjs";
|
|
26
|
+
/**
|
|
27
|
+
* Records the NANOUSD amount to report for the call. Callable any number of
|
|
28
|
+
* times; the last call wins. Throws `EnlittConfigurationError` synchronously
|
|
29
|
+
* for an amount that is not a canonical nonnegative integer string, and for a
|
|
30
|
+
* call made after the report has been sent.
|
|
31
|
+
*/
|
|
32
|
+
export type PremiumChargeFunction = (amountNanoUsd: string) => void;
|
|
33
|
+
/**
|
|
34
|
+
* The optional content-free facts a business may attach to the terminal
|
|
35
|
+
* report. None is required and none has a financial effect.
|
|
36
|
+
*/
|
|
37
|
+
export interface PremiumTerminalFacts {
|
|
38
|
+
readonly usage?: {
|
|
39
|
+
inputTokens?: number;
|
|
40
|
+
outputTokens?: number;
|
|
41
|
+
cachedInputTokens?: number;
|
|
42
|
+
reasoningTokens?: number;
|
|
43
|
+
};
|
|
44
|
+
readonly finishReason?: string;
|
|
45
|
+
readonly providerResponseId?: string;
|
|
46
|
+
readonly delivery?: {
|
|
47
|
+
outputStarted: boolean;
|
|
48
|
+
outputCompleted: boolean;
|
|
49
|
+
};
|
|
50
|
+
readonly timing?: {
|
|
51
|
+
startedAt: string;
|
|
52
|
+
endedAt: string;
|
|
53
|
+
};
|
|
54
|
+
readonly errorCategory?: "provider" | "timeout" | "network" | "invalid_request" | "canceled" | "unknown";
|
|
55
|
+
}
|
|
56
|
+
/** The admitted operation, as handed to a `withPremiumCharge()` callback. */
|
|
57
|
+
export interface PremiumChargeHandle {
|
|
58
|
+
readonly operationId: string;
|
|
59
|
+
/** The most the report can charge: the declared maximum, lowered by the server to this user's per-call limit. */
|
|
60
|
+
readonly effectiveMaxChargeNanoUsd: string;
|
|
61
|
+
readonly terminalDeadline: string;
|
|
62
|
+
/** Records safe facts for the terminal report; the last call wins. */
|
|
63
|
+
describe(facts: PremiumTerminalFacts): void;
|
|
64
|
+
}
|
|
65
|
+
/** What one premium operation declares at begin. */
|
|
66
|
+
export interface PremiumChargeRequest {
|
|
67
|
+
readonly maxCharge: string;
|
|
68
|
+
readonly provider: {
|
|
69
|
+
id: string;
|
|
70
|
+
model: string;
|
|
71
|
+
};
|
|
72
|
+
readonly callId?: string;
|
|
73
|
+
readonly reportingDeadlineMs?: number;
|
|
74
|
+
readonly metadata?: Record<string, string>;
|
|
75
|
+
readonly signal?: AbortSignal;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The admitted operation as returned by Enlitt, carried opaquely to the
|
|
79
|
+
* terminal report. The response's equal `reserved_nano_usd` is validated and
|
|
80
|
+
* otherwise not exposed; no credit or pricing-version field exists on the wire.
|
|
81
|
+
*/
|
|
82
|
+
export interface PremiumOperation {
|
|
83
|
+
readonly operationId: string;
|
|
84
|
+
readonly effectiveMaxChargeNanoUsd: string;
|
|
85
|
+
readonly terminalDeadline: string;
|
|
86
|
+
}
|
|
87
|
+
/** One terminal report in the TypeScript shape this module works in. */
|
|
88
|
+
export interface PremiumTerminalReport extends PremiumTerminalFacts {
|
|
89
|
+
readonly reportId: string;
|
|
90
|
+
readonly status: "completed" | "failed" | "canceled";
|
|
91
|
+
readonly chargeNanoUsd: string;
|
|
92
|
+
}
|
|
93
|
+
/** Options for {@link withPremiumCharge}: the shared construction options plus one premium call's declaration. */
|
|
94
|
+
export interface WithPremiumChargeOptions extends EnlittOptions {
|
|
95
|
+
/** NANOUSD, a canonical nonnegative integer string. */
|
|
96
|
+
maxCharge: string;
|
|
97
|
+
/** Bounded labels, as on the premium begin request. */
|
|
98
|
+
provider: {
|
|
99
|
+
id: string;
|
|
100
|
+
model: string;
|
|
101
|
+
};
|
|
102
|
+
/** The call ID; default a fresh random one. */
|
|
103
|
+
idempotencyKey?: string;
|
|
104
|
+
reportingDeadlineMs?: number;
|
|
105
|
+
metadata?: Record<string, string>;
|
|
106
|
+
signal?: AbortSignal;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* A supplied idempotency key is a base, not the call identity: the Vercel
|
|
110
|
+
* adapter appends `.<prompt item count>`. The floor is the protocol's own
|
|
111
|
+
* 16-character floor, imported rather than restated, so every derived ID
|
|
112
|
+
* satisfies the wire bound by construction; the ceiling stays well under 255
|
|
113
|
+
* so the appended suffix can never push a derived ID over it.
|
|
114
|
+
*/
|
|
115
|
+
export declare const IDEMPOTENCY_KEY_OPTION_MIN_LENGTH = 16;
|
|
116
|
+
export declare const IDEMPOTENCY_KEY_OPTION_MAX_LENGTH = 200;
|
|
117
|
+
/**
|
|
118
|
+
* Validates a NANOUSD amount: a canonical nonnegative base-10 integer string
|
|
119
|
+
* with no sign, fraction, exponent, whitespace, or noncanonical leading zero.
|
|
120
|
+
* `"0"` is valid. Parsed as `bigint` (so values beyond the safe-integer range
|
|
121
|
+
* keep full precision) and returned as the exact string the caller supplied.
|
|
122
|
+
*/
|
|
123
|
+
export declare function assertNanoUsd(value: unknown, label: string): string;
|
|
124
|
+
/** Validates a business-supplied idempotency key base (16-200 visible ASCII characters). */
|
|
125
|
+
export declare function assertIdempotencyKeyBase(value: unknown, label: string): string;
|
|
126
|
+
/**
|
|
127
|
+
* Validates a requested reporting deadline as a positive safe integer. The
|
|
128
|
+
* deployed maximum is server policy and is not knowable locally, so no local
|
|
129
|
+
* maximum is applied; the server bounds it and its returned deadline is
|
|
130
|
+
* authoritative.
|
|
131
|
+
*/
|
|
132
|
+
export declare function assertReportingDeadlineMs(value: unknown, label: string): number;
|
|
133
|
+
/**
|
|
134
|
+
* Validates caller metadata with the protocol's own `Record<string, string>`
|
|
135
|
+
* rule (at most 16 entries, bounded keys and values, no `enlitt_` prefix): one
|
|
136
|
+
* rule and one validator rather than a per-mode type.
|
|
137
|
+
*/
|
|
138
|
+
export declare function assertCallerMetadata(value: unknown, label: string): Record<string, string>;
|
|
139
|
+
/** Validates a call ID against the protocol's 16-255 visible-ASCII `Idempotency-Key` bound. */
|
|
140
|
+
export declare function assertCallId(callId: string): string;
|
|
141
|
+
/** A freshly generated, unique-per-operation random value of at least 16 visible ASCII characters (a UUID is 36). */
|
|
142
|
+
export declare function generateRandomCallId(): string;
|
|
143
|
+
/**
|
|
144
|
+
* Calls **Begin premium operation** before the provider ever runs.
|
|
145
|
+
*
|
|
146
|
+
* Sends `mode: "premium"`, `max_charge_nano_usd` exactly as the business
|
|
147
|
+
* declared it, `reporting_deadline_ms` when supplied, the bounded `provider`
|
|
148
|
+
* labels, and `metadata`. The call ID travels as the request's
|
|
149
|
+
* `Idempotency-Key`, never as a body field: the protocol lists no such field.
|
|
150
|
+
* Enlitt performs the conversion to credit units; this function sends no credit
|
|
151
|
+
* figure and reads no balance figure.
|
|
152
|
+
*
|
|
153
|
+
* The body is validated with the protocol's strict request validator before it
|
|
154
|
+
* is sent (strict out). The response is parsed in tolerant mode (tolerant in):
|
|
155
|
+
* an extra field or an unknown `billing` value still admits the operation. The
|
|
156
|
+
* returned `effective_max_charge_nano_usd` and `terminal_deadline` are
|
|
157
|
+
* authoritative regardless of what was requested.
|
|
158
|
+
*
|
|
159
|
+
* A refusal, a business authentication failure, or a transport failure
|
|
160
|
+
* surfaces as the corresponding typed error with `providerInvoked: false`;
|
|
161
|
+
* there is no path from a refusal to a provider invocation, including a
|
|
162
|
+
* `rate_limit_exceeded` whose transport wait was exhausted (defensive: the
|
|
163
|
+
* server never sends it on the premium path).
|
|
164
|
+
*/
|
|
165
|
+
export declare function beginPremiumOperation(client: InternalEnlittClient, request: PremiumChargeRequest, callId: string): Promise<PremiumOperation>;
|
|
166
|
+
/**
|
|
167
|
+
* Sends one idempotent content-free terminal report to **Report premium
|
|
168
|
+
* terminal state**, through the transport's bounded transient retry, which
|
|
169
|
+
* resends the identical body and `Idempotency-Key` so a retry is a replay and
|
|
170
|
+
* never a second report. `report.reportId` is generated once per operation.
|
|
171
|
+
*
|
|
172
|
+
* Never propagates a delivery failure into its caller. When the transport's
|
|
173
|
+
* retries are exhausted, or Enlitt answers `409`, the typed error has already
|
|
174
|
+
* been handed to `client.onTerminalReportError` (by the transport) and is
|
|
175
|
+
* dropped here: a delivered provider output is a fact the business already
|
|
176
|
+
* paid for, and turning an unrelated reporting failure into a thrown error
|
|
177
|
+
* would make it discard output it already has. An undelivered report instead
|
|
178
|
+
* lets the operation expire at its deadline, charging nothing.
|
|
179
|
+
*
|
|
180
|
+
* The response is parsed tolerantly. A report whose `status` reads as `"other"`
|
|
181
|
+
* is still a delivered report: it is neither retried nor passed to
|
|
182
|
+
* `onTerminalReportError`.
|
|
183
|
+
*/
|
|
184
|
+
export declare function reportPremiumTerminalState(client: InternalEnlittClient, operation: PremiumOperation, report: PremiumTerminalReport): Promise<void>;
|
|
185
|
+
/**
|
|
186
|
+
* Whether `run` rejected because the call was canceled: the request's own
|
|
187
|
+
* signal is aborted, or the error is an `AbortError`.
|
|
188
|
+
*/
|
|
189
|
+
export declare function isCancellation(thrown: unknown, signal: AbortSignal | undefined): boolean;
|
|
190
|
+
/**
|
|
191
|
+
* Wraps exactly one model call: begins one operation, runs `run` only after
|
|
192
|
+
* admission, and sends exactly one terminal report.
|
|
193
|
+
*
|
|
194
|
+
* - **Begin.** `request.callId` when supplied (validated against the protocol's
|
|
195
|
+
* `Idempotency-Key` bounds), otherwise a fresh random ID. A refusal,
|
|
196
|
+
* authentication failure, or transport failure rejects with the typed error
|
|
197
|
+
* and `run` is never called.
|
|
198
|
+
* - **Run.** `run` receives a `charge` function and the admitted operation.
|
|
199
|
+
* `charge(amount)` records the amount to report; the last call wins.
|
|
200
|
+
* - **Status.** `run` resolving is `completed`. `run` rejecting while
|
|
201
|
+
* `request.signal` is aborted, or with an `AbortError`, is `canceled`. Any
|
|
202
|
+
* other rejection is `failed`. The status is decided by how `run` settles,
|
|
203
|
+
* never by the caller naming one, so a caller cannot report a failure as a
|
|
204
|
+
* charged cancellation.
|
|
205
|
+
* - **Charge.** `"0"` for `failed`, whatever `charge()` received (a failed call
|
|
206
|
+
* is always free). For `completed` and `canceled`, the last amount passed to
|
|
207
|
+
* `charge()`, or `"0"` if never called, lowered to the effective maximum
|
|
208
|
+
* without an error.
|
|
209
|
+
* - **Report.** One `PremiumTerminalReport` with a `reportId` generated once,
|
|
210
|
+
* awaited before settling so a serverless process cannot end between the
|
|
211
|
+
* call and its report.
|
|
212
|
+
* - **Result.** `run`'s value, or `run`'s own error unchanged.
|
|
213
|
+
*/
|
|
214
|
+
export declare function runPremiumCharge<T>(client: InternalEnlittClient, request: PremiumChargeRequest, run: (charge: PremiumChargeFunction, operation: PremiumChargeHandle) => Promise<T>): Promise<T>;
|
|
215
|
+
/**
|
|
216
|
+
* Accounts one premium model call made by code outside the Vercel AI SDK: a
|
|
217
|
+
* direct provider SDK call, a raw `fetch`, anything the business runs itself.
|
|
218
|
+
*
|
|
219
|
+
* It performs `enlitt()`'s construction checks (server runtime, business
|
|
220
|
+
* secret, base URL) by building one internal client from the `EnlittOptions`
|
|
221
|
+
* fields, validates `maxCharge`, `idempotencyKey`, `reportingDeadlineMs`, and
|
|
222
|
+
* `metadata` under the same rules the Vercel adapter applies, and delegates to
|
|
223
|
+
* {@link runPremiumCharge} with `idempotencyKey` as the call ID. Every failure
|
|
224
|
+
* of those checks is an `EnlittConfigurationError` before any request.
|
|
225
|
+
*
|
|
226
|
+
* It is one model call. A loop of calls is a loop of `withPremiumCharge()`
|
|
227
|
+
* calls; retrying the same call with the same `idempotencyKey` replays that
|
|
228
|
+
* operation instead of charging again.
|
|
229
|
+
*/
|
|
230
|
+
export declare function withPremiumCharge<T>(options: WithPremiumChargeOptions, run: (charge: PremiumChargeFunction, operation: PremiumChargeHandle) => Promise<T>): Promise<T>;
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one premium billing lifecycle, with no framework dependency.
|
|
3
|
+
*
|
|
4
|
+
* `specs/SDK-side routing.md` gives this file the framework-agnostic core of
|
|
5
|
+
* premium mode: a business runs a model call it pays for, and Enlitt only
|
|
6
|
+
* accounts for it. The call is bracketed by a content-free begin (a maximum
|
|
7
|
+
* charge in NANOUSD, declared before the provider runs) and exactly one
|
|
8
|
+
* terminal report (a status and the actual NANOUSD charge, sent after it).
|
|
9
|
+
*
|
|
10
|
+
* `runPremiumCharge()` is the single implementation of that lifecycle: one
|
|
11
|
+
* begin, one status rule, one charge rule, one report path. The public
|
|
12
|
+
* `withPremiumCharge()` in this file and the Vercel adapter in
|
|
13
|
+
* `vercel/premium-language-model.ts` are both thin layers over it, so moving a
|
|
14
|
+
* call between the two changes how the charge is computed and nothing about how
|
|
15
|
+
* it is settled.
|
|
16
|
+
*
|
|
17
|
+
* This file imports nothing from the Vercel AI SDK, and it never sends prompts,
|
|
18
|
+
* messages, outputs, files, tools, provider options, provider keys, raw errors,
|
|
19
|
+
* or content hashes to Enlitt: every body it builds is assembled field by field
|
|
20
|
+
* from bounded named inputs, never by forwarding a caller-supplied object.
|
|
21
|
+
*
|
|
22
|
+
* Every NANOUSD amount is a canonical base-10 integer string and is compared
|
|
23
|
+
* only as a `bigint`; none is ever converted to `number`, rounded, or displayed.
|
|
24
|
+
*/
|
|
25
|
+
import { type EnlittOptions, type InternalEnlittClient } from "./transport.js";
|
|
26
|
+
/**
|
|
27
|
+
* Records the NANOUSD amount to report for the call. Callable any number of
|
|
28
|
+
* times; the last call wins. Throws `EnlittConfigurationError` synchronously
|
|
29
|
+
* for an amount that is not a canonical nonnegative integer string, and for a
|
|
30
|
+
* call made after the report has been sent.
|
|
31
|
+
*/
|
|
32
|
+
export type PremiumChargeFunction = (amountNanoUsd: string) => void;
|
|
33
|
+
/**
|
|
34
|
+
* The optional content-free facts a business may attach to the terminal
|
|
35
|
+
* report. None is required and none has a financial effect.
|
|
36
|
+
*/
|
|
37
|
+
export interface PremiumTerminalFacts {
|
|
38
|
+
readonly usage?: {
|
|
39
|
+
inputTokens?: number;
|
|
40
|
+
outputTokens?: number;
|
|
41
|
+
cachedInputTokens?: number;
|
|
42
|
+
reasoningTokens?: number;
|
|
43
|
+
};
|
|
44
|
+
readonly finishReason?: string;
|
|
45
|
+
readonly providerResponseId?: string;
|
|
46
|
+
readonly delivery?: {
|
|
47
|
+
outputStarted: boolean;
|
|
48
|
+
outputCompleted: boolean;
|
|
49
|
+
};
|
|
50
|
+
readonly timing?: {
|
|
51
|
+
startedAt: string;
|
|
52
|
+
endedAt: string;
|
|
53
|
+
};
|
|
54
|
+
readonly errorCategory?: "provider" | "timeout" | "network" | "invalid_request" | "canceled" | "unknown";
|
|
55
|
+
}
|
|
56
|
+
/** The admitted operation, as handed to a `withPremiumCharge()` callback. */
|
|
57
|
+
export interface PremiumChargeHandle {
|
|
58
|
+
readonly operationId: string;
|
|
59
|
+
/** The most the report can charge: the declared maximum, lowered by the server to this user's per-call limit. */
|
|
60
|
+
readonly effectiveMaxChargeNanoUsd: string;
|
|
61
|
+
readonly terminalDeadline: string;
|
|
62
|
+
/** Records safe facts for the terminal report; the last call wins. */
|
|
63
|
+
describe(facts: PremiumTerminalFacts): void;
|
|
64
|
+
}
|
|
65
|
+
/** What one premium operation declares at begin. */
|
|
66
|
+
export interface PremiumChargeRequest {
|
|
67
|
+
readonly maxCharge: string;
|
|
68
|
+
readonly provider: {
|
|
69
|
+
id: string;
|
|
70
|
+
model: string;
|
|
71
|
+
};
|
|
72
|
+
readonly callId?: string;
|
|
73
|
+
readonly reportingDeadlineMs?: number;
|
|
74
|
+
readonly metadata?: Record<string, string>;
|
|
75
|
+
readonly signal?: AbortSignal;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The admitted operation as returned by Enlitt, carried opaquely to the
|
|
79
|
+
* terminal report. The response's equal `reserved_nano_usd` is validated and
|
|
80
|
+
* otherwise not exposed; no credit or pricing-version field exists on the wire.
|
|
81
|
+
*/
|
|
82
|
+
export interface PremiumOperation {
|
|
83
|
+
readonly operationId: string;
|
|
84
|
+
readonly effectiveMaxChargeNanoUsd: string;
|
|
85
|
+
readonly terminalDeadline: string;
|
|
86
|
+
}
|
|
87
|
+
/** One terminal report in the TypeScript shape this module works in. */
|
|
88
|
+
export interface PremiumTerminalReport extends PremiumTerminalFacts {
|
|
89
|
+
readonly reportId: string;
|
|
90
|
+
readonly status: "completed" | "failed" | "canceled";
|
|
91
|
+
readonly chargeNanoUsd: string;
|
|
92
|
+
}
|
|
93
|
+
/** Options for {@link withPremiumCharge}: the shared construction options plus one premium call's declaration. */
|
|
94
|
+
export interface WithPremiumChargeOptions extends EnlittOptions {
|
|
95
|
+
/** NANOUSD, a canonical nonnegative integer string. */
|
|
96
|
+
maxCharge: string;
|
|
97
|
+
/** Bounded labels, as on the premium begin request. */
|
|
98
|
+
provider: {
|
|
99
|
+
id: string;
|
|
100
|
+
model: string;
|
|
101
|
+
};
|
|
102
|
+
/** The call ID; default a fresh random one. */
|
|
103
|
+
idempotencyKey?: string;
|
|
104
|
+
reportingDeadlineMs?: number;
|
|
105
|
+
metadata?: Record<string, string>;
|
|
106
|
+
signal?: AbortSignal;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* A supplied idempotency key is a base, not the call identity: the Vercel
|
|
110
|
+
* adapter appends `.<prompt item count>`. The floor is the protocol's own
|
|
111
|
+
* 16-character floor, imported rather than restated, so every derived ID
|
|
112
|
+
* satisfies the wire bound by construction; the ceiling stays well under 255
|
|
113
|
+
* so the appended suffix can never push a derived ID over it.
|
|
114
|
+
*/
|
|
115
|
+
export declare const IDEMPOTENCY_KEY_OPTION_MIN_LENGTH = 16;
|
|
116
|
+
export declare const IDEMPOTENCY_KEY_OPTION_MAX_LENGTH = 200;
|
|
117
|
+
/**
|
|
118
|
+
* Validates a NANOUSD amount: a canonical nonnegative base-10 integer string
|
|
119
|
+
* with no sign, fraction, exponent, whitespace, or noncanonical leading zero.
|
|
120
|
+
* `"0"` is valid. Parsed as `bigint` (so values beyond the safe-integer range
|
|
121
|
+
* keep full precision) and returned as the exact string the caller supplied.
|
|
122
|
+
*/
|
|
123
|
+
export declare function assertNanoUsd(value: unknown, label: string): string;
|
|
124
|
+
/** Validates a business-supplied idempotency key base (16-200 visible ASCII characters). */
|
|
125
|
+
export declare function assertIdempotencyKeyBase(value: unknown, label: string): string;
|
|
126
|
+
/**
|
|
127
|
+
* Validates a requested reporting deadline as a positive safe integer. The
|
|
128
|
+
* deployed maximum is server policy and is not knowable locally, so no local
|
|
129
|
+
* maximum is applied; the server bounds it and its returned deadline is
|
|
130
|
+
* authoritative.
|
|
131
|
+
*/
|
|
132
|
+
export declare function assertReportingDeadlineMs(value: unknown, label: string): number;
|
|
133
|
+
/**
|
|
134
|
+
* Validates caller metadata with the protocol's own `Record<string, string>`
|
|
135
|
+
* rule (at most 16 entries, bounded keys and values, no `enlitt_` prefix): one
|
|
136
|
+
* rule and one validator rather than a per-mode type.
|
|
137
|
+
*/
|
|
138
|
+
export declare function assertCallerMetadata(value: unknown, label: string): Record<string, string>;
|
|
139
|
+
/** Validates a call ID against the protocol's 16-255 visible-ASCII `Idempotency-Key` bound. */
|
|
140
|
+
export declare function assertCallId(callId: string): string;
|
|
141
|
+
/** A freshly generated, unique-per-operation random value of at least 16 visible ASCII characters (a UUID is 36). */
|
|
142
|
+
export declare function generateRandomCallId(): string;
|
|
143
|
+
/**
|
|
144
|
+
* Calls **Begin premium operation** before the provider ever runs.
|
|
145
|
+
*
|
|
146
|
+
* Sends `mode: "premium"`, `max_charge_nano_usd` exactly as the business
|
|
147
|
+
* declared it, `reporting_deadline_ms` when supplied, the bounded `provider`
|
|
148
|
+
* labels, and `metadata`. The call ID travels as the request's
|
|
149
|
+
* `Idempotency-Key`, never as a body field: the protocol lists no such field.
|
|
150
|
+
* Enlitt performs the conversion to credit units; this function sends no credit
|
|
151
|
+
* figure and reads no balance figure.
|
|
152
|
+
*
|
|
153
|
+
* The body is validated with the protocol's strict request validator before it
|
|
154
|
+
* is sent (strict out). The response is parsed in tolerant mode (tolerant in):
|
|
155
|
+
* an extra field or an unknown `billing` value still admits the operation. The
|
|
156
|
+
* returned `effective_max_charge_nano_usd` and `terminal_deadline` are
|
|
157
|
+
* authoritative regardless of what was requested.
|
|
158
|
+
*
|
|
159
|
+
* A refusal, a business authentication failure, or a transport failure
|
|
160
|
+
* surfaces as the corresponding typed error with `providerInvoked: false`;
|
|
161
|
+
* there is no path from a refusal to a provider invocation, including a
|
|
162
|
+
* `rate_limit_exceeded` whose transport wait was exhausted (defensive: the
|
|
163
|
+
* server never sends it on the premium path).
|
|
164
|
+
*/
|
|
165
|
+
export declare function beginPremiumOperation(client: InternalEnlittClient, request: PremiumChargeRequest, callId: string): Promise<PremiumOperation>;
|
|
166
|
+
/**
|
|
167
|
+
* Sends one idempotent content-free terminal report to **Report premium
|
|
168
|
+
* terminal state**, through the transport's bounded transient retry, which
|
|
169
|
+
* resends the identical body and `Idempotency-Key` so a retry is a replay and
|
|
170
|
+
* never a second report. `report.reportId` is generated once per operation.
|
|
171
|
+
*
|
|
172
|
+
* Never propagates a delivery failure into its caller. When the transport's
|
|
173
|
+
* retries are exhausted, or Enlitt answers `409`, the typed error has already
|
|
174
|
+
* been handed to `client.onTerminalReportError` (by the transport) and is
|
|
175
|
+
* dropped here: a delivered provider output is a fact the business already
|
|
176
|
+
* paid for, and turning an unrelated reporting failure into a thrown error
|
|
177
|
+
* would make it discard output it already has. An undelivered report instead
|
|
178
|
+
* lets the operation expire at its deadline, charging nothing.
|
|
179
|
+
*
|
|
180
|
+
* The response is parsed tolerantly. A report whose `status` reads as `"other"`
|
|
181
|
+
* is still a delivered report: it is neither retried nor passed to
|
|
182
|
+
* `onTerminalReportError`.
|
|
183
|
+
*/
|
|
184
|
+
export declare function reportPremiumTerminalState(client: InternalEnlittClient, operation: PremiumOperation, report: PremiumTerminalReport): Promise<void>;
|
|
185
|
+
/**
|
|
186
|
+
* Whether `run` rejected because the call was canceled: the request's own
|
|
187
|
+
* signal is aborted, or the error is an `AbortError`.
|
|
188
|
+
*/
|
|
189
|
+
export declare function isCancellation(thrown: unknown, signal: AbortSignal | undefined): boolean;
|
|
190
|
+
/**
|
|
191
|
+
* Wraps exactly one model call: begins one operation, runs `run` only after
|
|
192
|
+
* admission, and sends exactly one terminal report.
|
|
193
|
+
*
|
|
194
|
+
* - **Begin.** `request.callId` when supplied (validated against the protocol's
|
|
195
|
+
* `Idempotency-Key` bounds), otherwise a fresh random ID. A refusal,
|
|
196
|
+
* authentication failure, or transport failure rejects with the typed error
|
|
197
|
+
* and `run` is never called.
|
|
198
|
+
* - **Run.** `run` receives a `charge` function and the admitted operation.
|
|
199
|
+
* `charge(amount)` records the amount to report; the last call wins.
|
|
200
|
+
* - **Status.** `run` resolving is `completed`. `run` rejecting while
|
|
201
|
+
* `request.signal` is aborted, or with an `AbortError`, is `canceled`. Any
|
|
202
|
+
* other rejection is `failed`. The status is decided by how `run` settles,
|
|
203
|
+
* never by the caller naming one, so a caller cannot report a failure as a
|
|
204
|
+
* charged cancellation.
|
|
205
|
+
* - **Charge.** `"0"` for `failed`, whatever `charge()` received (a failed call
|
|
206
|
+
* is always free). For `completed` and `canceled`, the last amount passed to
|
|
207
|
+
* `charge()`, or `"0"` if never called, lowered to the effective maximum
|
|
208
|
+
* without an error.
|
|
209
|
+
* - **Report.** One `PremiumTerminalReport` with a `reportId` generated once,
|
|
210
|
+
* awaited before settling so a serverless process cannot end between the
|
|
211
|
+
* call and its report.
|
|
212
|
+
* - **Result.** `run`'s value, or `run`'s own error unchanged.
|
|
213
|
+
*/
|
|
214
|
+
export declare function runPremiumCharge<T>(client: InternalEnlittClient, request: PremiumChargeRequest, run: (charge: PremiumChargeFunction, operation: PremiumChargeHandle) => Promise<T>): Promise<T>;
|
|
215
|
+
/**
|
|
216
|
+
* Accounts one premium model call made by code outside the Vercel AI SDK: a
|
|
217
|
+
* direct provider SDK call, a raw `fetch`, anything the business runs itself.
|
|
218
|
+
*
|
|
219
|
+
* It performs `enlitt()`'s construction checks (server runtime, business
|
|
220
|
+
* secret, base URL) by building one internal client from the `EnlittOptions`
|
|
221
|
+
* fields, validates `maxCharge`, `idempotencyKey`, `reportingDeadlineMs`, and
|
|
222
|
+
* `metadata` under the same rules the Vercel adapter applies, and delegates to
|
|
223
|
+
* {@link runPremiumCharge} with `idempotencyKey` as the call ID. Every failure
|
|
224
|
+
* of those checks is an `EnlittConfigurationError` before any request.
|
|
225
|
+
*
|
|
226
|
+
* It is one model call. A loop of calls is a loop of `withPremiumCharge()`
|
|
227
|
+
* calls; retrying the same call with the same `idempotencyKey` replays that
|
|
228
|
+
* operation instead of charging again.
|
|
229
|
+
*/
|
|
230
|
+
export declare function withPremiumCharge<T>(options: WithPremiumChargeOptions, run: (charge: PremiumChargeFunction, operation: PremiumChargeHandle) => Promise<T>): Promise<T>;
|
|
231
|
+
//# sourceMappingURL=premium.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"premium.d.ts","sourceRoot":"","sources":["../../src/core/premium.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The read end of the Enlitt session cookie.
|
|
3
|
+
*
|
|
4
|
+
* `specs/SDK-side routing.md` gives this file the session handle, the
|
|
5
|
+
* middleware, and the async-context accessors. It owns "the user logs in and
|
|
6
|
+
* their credits are just used": it carries Enlitt's opaque session token from
|
|
7
|
+
* the cookie Account's login callback already set, through an
|
|
8
|
+
* `AsyncLocalStorage` context, to whatever call site needs it, without ever
|
|
9
|
+
* reading, parsing, or validating what the token contains. Validity is
|
|
10
|
+
* Enlitt's answer to give at call time, not this file's to guess.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* The canonical Enlitt session cookie name, and the default of both
|
|
14
|
+
* `CallbackConfig.cookieName` and `SessionMiddlewareOptions.cookieName`.
|
|
15
|
+
*
|
|
16
|
+
* A business that passes a custom `cookieName` to the callback must pass the
|
|
17
|
+
* identical value to the middleware. That is the one case the two ends can
|
|
18
|
+
* still disagree, and it is the business's own to keep consistent; the default
|
|
19
|
+
* is not, which is why it is one constant rather than two literals.
|
|
20
|
+
*/
|
|
21
|
+
export declare const ENLITT_SESSION_COOKIE_NAME = "enlitt_session";
|
|
22
|
+
/**
|
|
23
|
+
* An opaque handle around Enlitt's session token.
|
|
24
|
+
*
|
|
25
|
+
* The token has no subject, expiry, or scope readable from the SDK side, and
|
|
26
|
+
* every fact about the user lives on Enlitt's servers. Deliberately just a
|
|
27
|
+
* string wrapper: exposing it as a bare `string` anywhere in the call surface
|
|
28
|
+
* would invite a business to construct or compare tokens as ordinary strings,
|
|
29
|
+
* and there is no format a business can legitimately synthesize.
|
|
30
|
+
*/
|
|
31
|
+
export interface EnlittSession {
|
|
32
|
+
readonly token: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The only way to construct an `EnlittSession`.
|
|
36
|
+
*
|
|
37
|
+
* The token is a string the SDK never parses, decodes, validates, or
|
|
38
|
+
* inspects; this is what lets Enlitt rotate or restructure the underlying
|
|
39
|
+
* credential without a business shipping a release, and what keeps the
|
|
40
|
+
* business from ever holding an OAuth token or implementing a refresh.
|
|
41
|
+
* Constructing one from a value a business stored itself is legitimate and is
|
|
42
|
+
* how linked accounts and background jobs work; constructing one from a value
|
|
43
|
+
* the business derived, guessed, or built from a user identifier is not a
|
|
44
|
+
* session, and Enlitt rejects it at call time. The factory validates only
|
|
45
|
+
* that the token is a non-empty bounded string, because anything more would
|
|
46
|
+
* be inspecting a credential this module must treat as opaque.
|
|
47
|
+
*/
|
|
48
|
+
export declare function enlittSession(token: string): EnlittSession;
|
|
49
|
+
/**
|
|
50
|
+
* Options for `enlittSessionMiddleware()`.
|
|
51
|
+
*
|
|
52
|
+
* `cookieName` exists solely to mirror a business's own custom
|
|
53
|
+
* `CallbackConfig.cookieName`; the default matches `ENLITT_SESSION_COOKIE_NAME`
|
|
54
|
+
* so the common case names nothing.
|
|
55
|
+
*/
|
|
56
|
+
export interface SessionMiddlewareOptions {
|
|
57
|
+
cookieName?: string;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The shape `enlittSessionMiddleware()` returns.
|
|
61
|
+
*
|
|
62
|
+
* Framework-agnostic over Web `Request` and `Response`, exactly as Account's
|
|
63
|
+
* callback handlers are: v1 ships no per-framework adapter, so this is the
|
|
64
|
+
* one shape every framework's own middleware layer can wrap.
|
|
65
|
+
*/
|
|
66
|
+
export type EnlittMiddleware = (request: Request, next: () => Promise<Response>) => Promise<Response>;
|
|
67
|
+
/**
|
|
68
|
+
* Reads the Enlitt session cookie from an incoming request, runs `next()`
|
|
69
|
+
* with an `AsyncLocalStorage` context carrying the resulting `EnlittSession`
|
|
70
|
+
* or an explicit `null`, and leaves the context when the request ends.
|
|
71
|
+
*
|
|
72
|
+
* It never rejects a request: plenty of a business's routes are for
|
|
73
|
+
* signed-out visitors, and deciding the business's authentication policy is
|
|
74
|
+
* not this middleware's job. It performs no network call and no validation,
|
|
75
|
+
* because validity is Enlitt's answer to give at call time and pre-validating
|
|
76
|
+
* here would add a round trip to every request, including the ones that never
|
|
77
|
+
* call a model. The cookie lives on the business's own domain and was set by
|
|
78
|
+
* Enlitt's login callback, so no cross-site cookie, third-party storage, or
|
|
79
|
+
* browser-side token is involved.
|
|
80
|
+
*/
|
|
81
|
+
export declare function enlittSessionMiddleware(options?: SessionMiddlewareOptions): EnlittMiddleware;
|
|
82
|
+
/**
|
|
83
|
+
* Runs `fn` with an explicit `EnlittSession` (or `null`) bound as the ambient
|
|
84
|
+
* session for its entire synchronous and asynchronous extent.
|
|
85
|
+
*
|
|
86
|
+
* The escape hatch for a framework whose middleware shape
|
|
87
|
+
* `enlittSessionMiddleware()` cannot wrap directly: a business on such a
|
|
88
|
+
* framework establishes the same context in one line instead of waiting on a
|
|
89
|
+
* per-framework adapter that v1 does not ship.
|
|
90
|
+
*/
|
|
91
|
+
export declare function runWithEnlittSession<T>(session: EnlittSession | null, fn: () => T): T;
|
|
92
|
+
/**
|
|
93
|
+
* Returns the ambient session for the current request context.
|
|
94
|
+
*
|
|
95
|
+
* Throws `EnlittConfigurationError` when there is no context at all: no
|
|
96
|
+
* context means the middleware is not installed on this path, which is the
|
|
97
|
+
* business's own wiring bug, and treating that as "signed out" would make
|
|
98
|
+
* every user of a misconfigured deployment silently unauthenticated. Returns
|
|
99
|
+
* `null` when there is a context and no session, which is the genuine
|
|
100
|
+
* signed-out state a business checks before calling a model. These two
|
|
101
|
+
* conditions must never collapse into one: a deployment bug and a
|
|
102
|
+
* signed-out visitor look identical at the call site and must not be
|
|
103
|
+
* reported identically. A session this returns may still be expired or
|
|
104
|
+
* revoked; that is not knowable locally, so the call carries it and Enlitt
|
|
105
|
+
* answers with `personal_login_required` if it must.
|
|
106
|
+
*/
|
|
107
|
+
export declare function getEnlittSession(): EnlittSession | null;
|