@shop-os/modelkit 0.15.1 → 0.16.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 +17 -0
- package/README.md +29 -2
- package/dist/catalog.cjs.map +1 -1
- package/dist/catalog.d.cts +30 -2
- package/dist/catalog.d.ts +30 -2
- package/dist/catalog.js.map +1 -1
- package/dist/index.cjs +295 -86
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +92 -2
- package/dist/index.d.ts +92 -2
- package/dist/index.js +290 -86
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,6 +9,23 @@ header in the release PR.
|
|
|
9
9
|
|
|
10
10
|
## [Unreleased]
|
|
11
11
|
|
|
12
|
+
## [0.16.0] - Unreleased
|
|
13
|
+
|
|
14
|
+
Customer provider keys per call (BYOK, ENG-3403).
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
- `CallContext.keyOverride`: a workspace's own provider keys for one call. A required name it holds is used instead of the tier's key and never falls back to it; the other names resolve through `tier` as before (a `sloosh` call still fails with `auth` for a name neither holds). Blank values count as absent, Higgsfield counts only as the key + secret pair, and `aws` is ignored (no BYOK for Bedrock). Such a call goes direct to the provider, never through the Cloudflare gateway, and a customer `anthropic` key always uses the Anthropic API. `resumeVideo`, video-tool and 3D jobs use it too.
|
|
18
|
+
- `CallContext.requireOverride`: set it on a call billed at the customer-key price. When no required key name of the call comes from `keyOverride` (missing, blank, or for another provider), the call throws a `ModelKitError` code `byok_key_missing` (`retryable: false`, with `provider` and `model`) before any provider request, so it never runs on our key. Image, video, resume, audio, video-tool, 3D, text and language-model handles all check it; a model outside the catalog always throws, since the kit resolves no key for it.
|
|
19
|
+
- `ModelKitErrorCode` `"byok_key_missing"`. It is never retried and is not a provider auth failure: the customer's key was never sent, so a caller must not record a key status for it.
|
|
20
|
+
- `UsageEvent.keySource` (`"pro" | "enterprise" | "sloosh" | "byok"`) on every usage event of a catalog call: `"byok"` when any required key came from the override, else the tier that served (an enterprise fallback is `"pro"`).
|
|
21
|
+
- `byok.ts`: `BYOK_PROVIDERS` (ark, fal, replicate, openai, google), `byokProviderForModel`, `modelsPoweredBy`, `keyOverrideFor`, and `testProviderKey`, which checks a key with one free, read-only request.
|
|
22
|
+
- `testProviderKey` returns `{ ok: true, credit }`; `credit` is `"unknown"` from every current probe (they prove the key, not a balance), so a caller must not treat a passing test as proof of credit. It takes `endpoints` and probes Ark at `endpoints.ark` (the adapter's default when unset). OpenAI and Google 403 on the models list is `unverifiable`, not `denied` (a restricted key can still generate); 401 stays `invalid`.
|
|
23
|
+
- `keyOverrideFor` and `testProviderKey` accept a stored secret with no `apiKey`: the first leaves the provider out, the second answers `invalid` without a request.
|
|
24
|
+
- `LanguageModelHandle.keySource`, the same value `generateText` and `streamText` stamp on usage.
|
|
25
|
+
|
|
26
|
+
### Changed
|
|
27
|
+
- Ark errors read BytePlus `error.code`, also on a failed task: 403 `AccountOverdueError` / `OperationDenied.ServiceOverdue` and 429 `QuotaExceeded` are `provider_billing` (not retried; a quota 429 while polling fails at once instead of polling into the timeout); 404 `ModelNotOpen` / `InvalidEndpointOrModel.NotFound` are `provider_rejected` with the code in the message. A plain 403 stays `auth`, other 429s `rate_limited`.
|
|
28
|
+
|
|
12
29
|
## [0.15.1] - Unreleased
|
|
13
30
|
|
|
14
31
|
Model names and descriptions now match what each row offers (ENG-3364). The stored `model_catalog` rows are seeded once and never updated, so each environment needs the same edits in pro-admin; the PR lists them.
|
package/README.md
CHANGED
|
@@ -117,6 +117,33 @@ row for each. All three are in the browser-safe catalog entry.
|
|
|
117
117
|
|
|
118
118
|
Keys come from `process.env` by default: bare names for pro (`OPENAI_API_KEY`, `FAL_KEY`, `BYTEDANCE_API_KEY`, `LUMA_API_KEY` ...), the `ENTERPRISE_` prefix for enterprise and the `SLOOSH_` prefix for sloosh (sloosh reads `SLOOSH_LUMA_API_KEY` for Luma). Pass `keys: { pro, enterprise, sloosh }` to override. `tier: "sloosh"` uses the sloosh keys only: a missing key is a `ModelKitError` with code `auth`, never a fallback to pro.
|
|
119
119
|
|
|
120
|
+
## Customer keys (BYOK)
|
|
121
|
+
|
|
122
|
+
`CallContext.keyOverride` (0.16.0) carries a workspace's own provider keys for one call:
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
const override = keyOverrideFor(storedSecrets); // { ark?: string, fal?: string, ... }
|
|
126
|
+
await kit.generateVideo({ model: "seedance-2.0", prompt }, { tier, keyOverride: override });
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
- A required key name the override holds is taken from it and **never** falls back to a tier key. Names it does not hold resolve through `tier` as before, so a `sloosh` call still fails with `auth` for a name neither holds. A blank value counts as absent.
|
|
130
|
+
- Higgsfield counts only as the key + secret pair. `aws` is ignored: BYOK never covers Bedrock, and a customer `anthropic` key always means the Anthropic API.
|
|
131
|
+
- A call served from the override goes direct to the provider, never through the Cloudflare gateway (whose stored secrets are the pro keys), like a sloosh call.
|
|
132
|
+
- `requireOverride: true` marks a call billed at the customer-key price. When no required key name comes from the override (missing, blank, or for another provider), the call throws `ModelKitError` code `byok_key_missing` (`retryable: false`, with `provider` and `model`) before any provider request, instead of running on our key. Every catalog method checks it, including `resumeVideo`, video-tool and 3D status/result/cancel, text and `languageModel`; a pass-through provider instance always throws. `byok_key_missing` says nothing about the customer's key, so never record a key status for it.
|
|
133
|
+
- Every usage event of a catalog call carries `keySource`: `"byok"` when any required key came from the override, else the tier that served (`"pro"`, `"enterprise"`, `"sloosh"`; an enterprise call that fell back to pro reports `"pro"`). Pass-through provider instances carry none. No key material goes into events, errors or logs.
|
|
134
|
+
|
|
135
|
+
The registry (`byok.ts`) serves a Settings page:
|
|
136
|
+
|
|
137
|
+
| Export | Use |
|
|
138
|
+
| --- | --- |
|
|
139
|
+
| `BYOK_PROVIDERS` | `ark` (BytePlus ModelArk), `fal`, `replicate`, `openai`, `google`: label, the kit key name, and the fields to store. Ark also takes optional `accessKeyId`, `secretAccessKey`, `assetGroupId` for the BytePlus asset API (verified assets), which the consumer calls itself; the kit uses only `apiKey`. |
|
|
140
|
+
| `byokProviderForModel(row)` | The provider whose key runs a row, from its adapter (`ADAPTER_KEYS`) or, for text rows, the vendor. Claude, Bedrock and every other provider give `null`. |
|
|
141
|
+
| `modelsPoweredBy(provider, rows)` | The rows that key runs ("Powers N models"). |
|
|
142
|
+
| `keyOverrideFor(secrets)` | Stored secrets to a `keyOverride`; blank or missing keys are left out (an Ark row with only the asset fields gives no `ark`). |
|
|
143
|
+
| `testProviderKey(provider, secret, { fetch, signal, endpoints })` | One free, read-only GET: `{ ok: true, credit }` or `{ ok: false, reason }`. 400/401 or no `apiKey` `invalid`, 402/403 `denied`, 429/5xx/network `unreachable`; a caller abort rethrows. Pass the kit's `config.endpoints` so the Ark probe uses the same base url as the adapter. `credit` is `"unknown"` from every current probe: they prove the key, not a balance, so never read `ok` as "the account has credit". |
|
|
144
|
+
|
|
145
|
+
Probes: Ark `GET /api/v3/contents/generations/tasks?page_num=1&page_size=1`, OpenAI `GET /v1/models`, Google `GET /v1beta/models` (key in the `x-goog-api-key` header, never in the URL), Replicate `GET /v1/account`, fal `GET https://api.fal.ai/v1/assets/collections?limit=1`. fal's `/v1/models` answers 200 for any key, so it proves nothing; the assets list needs `assets:read`, which the `API` preset holds. A fal key with a narrower custom policy gets 403 there, reported as `unverifiable`. OpenAI and Google 403 on the models list is `unverifiable` too: a restricted OpenAI key (Images write only) gets 403 there yet runs gpt-image. 401 stays `invalid`.
|
|
146
|
+
|
|
120
147
|
## Browser-safe catalog
|
|
121
148
|
|
|
122
149
|
```ts
|
|
@@ -220,7 +247,7 @@ Opt into thinking per call, for example
|
|
|
220
247
|
`providerOptions: { anthropic: { thinking: { type: "enabled", budgetTokens: 4000 } } }`.
|
|
221
248
|
|
|
222
249
|
The kit keeps its `Warning` type for coercions; the SDK's warning type is `AiWarning`.
|
|
223
|
-
`languageModel()` remains the low-level handle for Mastra and custom middleware; its `providerOptions` carry no kit-internal keys.
|
|
250
|
+
`languageModel()` remains the low-level handle for Mastra and custom middleware; its `providerOptions` carry no kit-internal keys. A caller that emits its own usage events takes `provider` from `handle.usageProvider` and `keySource` from `handle.keySource` (`"byok"` when the model runs on the call's `keyOverride`).
|
|
224
251
|
|
|
225
252
|
## Anthropic on Bedrock
|
|
226
253
|
|
|
@@ -314,7 +341,7 @@ const defaultKit = createDefaultModelKit({ keys: { pro: keysFromEnv(process.env)
|
|
|
314
341
|
| Keys | Enterprise keys win only when every key the adapter (and its route, e.g. the cloudflare token for a vendor that really goes through the gateway) needs is set on the enterprise tier; a fallback to pro keys is logged once per call. |
|
|
315
342
|
| Routing | Cloudflare AI Gateway for text only. Never for Gemini images or Omni. fal passthrough for geo-blocked regions; the fal direct route submits to the queue and polls, so `job.result({ timeoutMs, signal })` is live. |
|
|
316
343
|
| Retry | One classifier, one policy. Never on content blocks, aborts, or 4xx except 429; SDKs with their own retry (Replicate) run under a single kit attempt; a 429 on a status poll keeps polling unless `CallContext.maxRetries` is `0`. See durable media execution above. |
|
|
317
|
-
| Errors | `ModelKitError` with `code`, `provider`, `status`, `body`. Gemini `blockReason` and fal `body.detail` are lifted into `body`. A throw with no HTTP status (fetch failed, reset, DNS) is a retryable `network` error on every adapter. An out-of-credit provider account is `provider_billing` and is never retried: HTTP 402, a 4xx whose structured error code is a billing code (`insufficient_quota`, ...), or a 403/429 whose text names the exhausted account ("Exhausted balance", "Insufficient credit"). Text calls go through `ai` and surface the SDK's own error instead. |
|
|
344
|
+
| Errors | `ModelKitError` with `code`, `provider`, `status`, `body`. Gemini `blockReason` and fal `body.detail` are lifted into `body`. A throw with no HTTP status (fetch failed, reset, DNS) is a retryable `network` error on every adapter. An out-of-credit provider account is `provider_billing` and is never retried: HTTP 402, a 4xx whose structured error code is a billing code (`insufficient_quota`, ...), or a 403/429 whose text names the exhausted account ("Exhausted balance", "Insufficient credit"). BytePlus Ark codes win over the status (body `{"error":{"code","message","type"}}`, and a failed task's `error.code`): 403 `AccountOverdueError` / `OperationDenied.ServiceOverdue` and 429 `QuotaExceeded` are `provider_billing`; 404 `ModelNotOpen` / `InvalidEndpointOrModel.NotFound` are `provider_rejected` with the code in the message; a plain 403 stays `auth` and another 429 `rate_limited`. Text calls go through `ai` and surface the SDK's own error instead. |
|
|
318
345
|
| Usage | One event per call with the canonical model id and the row's `usage.unit` (adapters supply the quantity in that unit). Gemini image events carry the token counts too (`inputTokens`/`outputTokens`); Google's raw `usageMetadata` rides on `raw.providerMetadata.google`. Video usage is emitted on completion and carries the resolution the provider renders (the default it falls back to when the request has none), because pricing is keyed by it; Seedance keeps `usage`, `duration`, `framespersecond` and `resolution` from the poll on `raw`. Audio: Lyria 3 returns no duration, so it emits `unit: "images", quantity: 1` (token counts ride along as `inputTokens`/`outputTokens`); Replicate audio (Lyria 2, MMAudio) emits `unit: "compute_seconds"` with the prediction time. |
|
|
319
346
|
| Media parts | `prepareMediaPart` turns a `MediaRef` into an AI SDK file part. Google inlines bytes up to 10 MiB (base64 makes them ~4/3 larger inside Gemini's 20 MB request cap) and uploads larger payloads through the Files API; call the returned `cleanup` after the request. Other providers get the reference inline. |
|
|
320
347
|
|