@north-light/sdk 0.3.836 → 0.3.838
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.
|
@@ -2,7 +2,7 @@ import { type Units } from './inference-pricing.js';
|
|
|
2
2
|
/** The scope an inference token carries. */
|
|
3
3
|
export declare const INFERENCE_SCOPE = "crtr:llm";
|
|
4
4
|
export type InferenceRung = 'ultra' | 'strong' | 'medium' | 'light';
|
|
5
|
-
/** One model call's usage report to the directory (`POST /v1/billing/settle-model`). `list_usd` is the Anthropic list price of the served model; `cost_usd` is what Northlight paid for the call
|
|
5
|
+
/** One model call's usage report to the directory (`POST /v1/billing/settle-model`). `list_usd` is the Anthropic list price of the served model; `cost_usd` is what Northlight paid for the call the app's API key serves every call, so it equals `list_usd`. */
|
|
6
6
|
export interface InferenceSettlement {
|
|
7
7
|
admission_id: string;
|
|
8
8
|
served_model: string;
|
|
@@ -36,7 +36,7 @@ export interface InferenceEndpointOptions {
|
|
|
36
36
|
baseURL: string;
|
|
37
37
|
/** The app's own principal, `app:<id>`; it prefixes the vendor reference of a call the vendor gave no request id. */
|
|
38
38
|
app: string;
|
|
39
|
-
/** The app's Anthropic API key. It serves
|
|
39
|
+
/** The app's Anthropic API key. It serves every call. It is never logged or returned. */
|
|
40
40
|
apiKey: string;
|
|
41
41
|
/** Strength to model: `anthropic/<model id>` for each rung the endpoint serves. A request naming any other model is refused. Every model needs a list price in this SDK; the constructor throws for one that has none. */
|
|
42
42
|
rungs: Partial<Record<InferenceRung, string>>;
|
|
@@ -66,16 +66,7 @@ export interface InferenceEndpointOptions {
|
|
|
66
66
|
* decides, per call and from current state, whether the call is admitted: a refusal reaches the runtime as a 402 carrying the
|
|
67
67
|
* directory's `payment` body, and a directory that cannot be reached or answers anything unexpected is 503 with nothing sent upstream.
|
|
68
68
|
*
|
|
69
|
-
*
|
|
70
|
-
* `pool.url` with `pool.token`, and a call the pool serves settles at list price with `cost_usd` 0. An answer without it (the person
|
|
71
|
-
* pays) sends the call to Anthropic with the app's `apiKey`, and `cost_usd` equals the list price.
|
|
72
|
-
*
|
|
73
|
-
* When the pool cannot serve a Northlight-paid call, the same request is sent to Anthropic with `apiKey` instead of being refused.
|
|
74
|
-
* That happens when the pool is unreachable or answers 401, 402, 403, 404, 408, 429 or any 5xx (every login rate-limited, no login,
|
|
75
|
-
* a pool-side refusal, a pool failure). Any other pool answer, notably a 400 for a malformed request, goes back to the caller
|
|
76
|
-
* unchanged, because the key would fail the same way. A call the key serves settles under the same admission with `cost_usd` equal
|
|
77
|
-
* to the list price (Northlight paid real money for it), so the directory counts it as it counts a pool call, and the log entry
|
|
78
|
-
* says `upstream: 'api_key_fallback'` with the pool's status. If the key's attempt fails too, its answer (or a 502) reaches the caller.
|
|
69
|
+
* Every admitted call is sent to Anthropic with `apiKey`, and settles at list price with `cost_usd` equal to `list_usd`.
|
|
79
70
|
*
|
|
80
71
|
* The answer streams back with `Crtr-Charge-Id` and calculated `crtr_charge` on its terminal event (or JSON body), without waiting
|
|
81
72
|
* for settlement. Usage is read off the stream and reported to the directory at the list price of the model the vendor reports it
|
|
@@ -4,14 +4,10 @@ import { hasListPrice, listUsd, longCacheWriteOf, pricedModel, unitsOf } from '.
|
|
|
4
4
|
export const INFERENCE_SCOPE = 'crtr:llm';
|
|
5
5
|
const VENDOR_URL = 'https://api.anthropic.com';
|
|
6
6
|
const MESSAGES_PATH = 'v1/messages';
|
|
7
|
-
const POOL_MESSAGES_PATH = 'v1/llm/anthropic/messages';
|
|
8
7
|
const DIRECTORY_TIMEOUT_MS = 10_000;
|
|
9
8
|
/** Posts made in the request's own lifetime before the stored report is left to `sweep`. */
|
|
10
9
|
const IMMEDIATE_ATTEMPTS = 3;
|
|
11
10
|
const SWEEP_BATCH = 50;
|
|
12
|
-
/** Pool answers the API key could plausibly serve instead: the pool or its login is unavailable or refused the call (401, 402, 403, 404 no login, 408), every login is rate-limited (429), or the pool failed (5xx). Any other status, 400 above all, would fail the same way at Anthropic and reaches the caller. */
|
|
13
|
-
const POOL_FALLBACK_STATUSES = new Set([401, 402, 403, 404, 408, 429]);
|
|
14
|
-
const poolFallbackStatus = (status) => POOL_FALLBACK_STATUSES.has(status) || status >= 500;
|
|
15
11
|
const STRIPPED_REQUEST_HEADERS = ['host', 'connection', 'keep-alive', 'transfer-encoding', 'te', 'upgrade', 'content-length', 'authorization', 'x-api-key', 'cookie', 'proxy-authorization'];
|
|
16
12
|
// The body is decoded and re-streamed, so these describe the upstream wire form, not what we send.
|
|
17
13
|
const STRIPPED_RESPONSE_HEADERS = ['connection', 'keep-alive', 'transfer-encoding', 'content-encoding', 'content-length'];
|
|
@@ -152,16 +148,7 @@ function chargedBody(body, contentType, charge) {
|
|
|
152
148
|
* decides, per call and from current state, whether the call is admitted: a refusal reaches the runtime as a 402 carrying the
|
|
153
149
|
* directory's `payment` body, and a directory that cannot be reached or answers anything unexpected is 503 with nothing sent upstream.
|
|
154
150
|
*
|
|
155
|
-
*
|
|
156
|
-
* `pool.url` with `pool.token`, and a call the pool serves settles at list price with `cost_usd` 0. An answer without it (the person
|
|
157
|
-
* pays) sends the call to Anthropic with the app's `apiKey`, and `cost_usd` equals the list price.
|
|
158
|
-
*
|
|
159
|
-
* When the pool cannot serve a Northlight-paid call, the same request is sent to Anthropic with `apiKey` instead of being refused.
|
|
160
|
-
* That happens when the pool is unreachable or answers 401, 402, 403, 404, 408, 429 or any 5xx (every login rate-limited, no login,
|
|
161
|
-
* a pool-side refusal, a pool failure). Any other pool answer, notably a 400 for a malformed request, goes back to the caller
|
|
162
|
-
* unchanged, because the key would fail the same way. A call the key serves settles under the same admission with `cost_usd` equal
|
|
163
|
-
* to the list price (Northlight paid real money for it), so the directory counts it as it counts a pool call, and the log entry
|
|
164
|
-
* says `upstream: 'api_key_fallback'` with the pool's status. If the key's attempt fails too, its answer (or a 502) reaches the caller.
|
|
151
|
+
* Every admitted call is sent to Anthropic with `apiKey`, and settles at list price with `cost_usd` equal to `list_usd`.
|
|
165
152
|
*
|
|
166
153
|
* The answer streams back with `Crtr-Charge-Id` and calculated `crtr_charge` on its terminal event (or JSON body), without waiting
|
|
167
154
|
* for settlement. Usage is read off the stream and reported to the directory at the list price of the model the vendor reports it
|
|
@@ -215,15 +202,9 @@ export function inferenceEndpoint(options) {
|
|
|
215
202
|
}
|
|
216
203
|
const body = object(answer.json);
|
|
217
204
|
if (answer.status === 200) {
|
|
218
|
-
const { status, admission_id, charge_id
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
if (pool === undefined)
|
|
222
|
-
return { kind: 'admitted', admissionId: admission_id, chargeId: charge_id, pool: undefined };
|
|
223
|
-
const { url, token: poolToken } = object(pool) ?? {};
|
|
224
|
-
// An admission whose pool the helper cannot read is malformed, so the call is not served.
|
|
225
|
-
return typeof url === 'string' && url !== '' && typeof poolToken === 'string' && poolToken !== ''
|
|
226
|
-
? { kind: 'admitted', admissionId: admission_id, chargeId: charge_id, pool: { url: url.replace(/\/+$/, ''), token: poolToken } }
|
|
205
|
+
const { status, admission_id, charge_id } = body ?? {};
|
|
206
|
+
return status === 'ok' && typeof admission_id === 'string' && typeof charge_id === 'string'
|
|
207
|
+
? { kind: 'admitted', admissionId: admission_id, chargeId: charge_id }
|
|
227
208
|
: { kind: 'unavailable' };
|
|
228
209
|
}
|
|
229
210
|
const error = object(body?.error);
|
|
@@ -316,7 +297,7 @@ export function inferenceEndpoint(options) {
|
|
|
316
297
|
if (!served.has(model))
|
|
317
298
|
return refuse(404, 'model_unavailable', `This endpoint does not serve ${model}.`);
|
|
318
299
|
const admission = appPaid
|
|
319
|
-
? { kind: 'admitted', admissionId: crypto.randomUUID(), chargeId: ''
|
|
300
|
+
? { kind: 'admitted', admissionId: crypto.randomUUID(), chargeId: '' }
|
|
320
301
|
: await admit(token, model);
|
|
321
302
|
if (admission.kind === 'refused') {
|
|
322
303
|
return Response.json({ type: 'error', error: { type: 'invalid_request_error', message: admission.payment.message }, payment: admission.payment }, { status: 402 });
|
|
@@ -329,43 +310,17 @@ export function inferenceEndpoint(options) {
|
|
|
329
310
|
for (const name of STRIPPED_REQUEST_HEADERS)
|
|
330
311
|
baseHeaders.delete(name);
|
|
331
312
|
// Not tied to the caller's connection: the vendor bills the whole answer, so the usage branch reads it to its end and the settlement is complete.
|
|
332
|
-
const sendToPool = (pool) => {
|
|
333
|
-
const headers = new Headers(baseHeaders);
|
|
334
|
-
headers.set('authorization', `Bearer ${pool.token}`);
|
|
335
|
-
return upstreamFetch(`${pool.url}/${POOL_MESSAGES_PATH}`, { method: 'POST', headers, body, redirect: 'manual' });
|
|
336
|
-
};
|
|
337
313
|
const sendToVendor = () => {
|
|
338
314
|
const headers = new Headers(baseHeaders);
|
|
339
315
|
headers.set('x-api-key', options.apiKey);
|
|
340
316
|
return upstreamFetch(`${VENDOR_URL}/${MESSAGES_PATH}`, { method: 'POST', headers, body, redirect: 'manual' });
|
|
341
317
|
};
|
|
342
318
|
let answer;
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
let poolFailure;
|
|
346
|
-
try {
|
|
347
|
-
answer = await sendToPool(admission.pool);
|
|
348
|
-
if (poolFallbackStatus(answer.status)) {
|
|
349
|
-
poolFailure = String(answer.status);
|
|
350
|
-
void answer.body?.cancel().catch(() => undefined);
|
|
351
|
-
answer = undefined;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
catch (error) {
|
|
355
|
-
poolFailure = String(error);
|
|
356
|
-
}
|
|
357
|
-
if (poolFailure !== undefined) {
|
|
358
|
-
upstream = 'api_key_fallback';
|
|
359
|
-
log({ sub, model, admission: admission.admissionId, upstream, pool_failed: poolFailure });
|
|
360
|
-
}
|
|
319
|
+
try {
|
|
320
|
+
answer = await sendToVendor();
|
|
361
321
|
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
answer = await sendToVendor();
|
|
365
|
-
}
|
|
366
|
-
catch {
|
|
367
|
-
return refuse(502, 'upstream_unreachable', `${upstream === 'api_key_fallback' ? 'The Claude pool and anthropic' : 'anthropic'} could not be reached.`);
|
|
368
|
-
}
|
|
322
|
+
catch {
|
|
323
|
+
return refuse(502, 'upstream_unreachable', 'anthropic could not be reached.');
|
|
369
324
|
}
|
|
370
325
|
const responseHeaders = new Headers(answer.headers);
|
|
371
326
|
for (const name of STRIPPED_RESPONSE_HEADERS)
|
|
@@ -409,7 +364,7 @@ export function inferenceEndpoint(options) {
|
|
|
409
364
|
served_model: servedModel,
|
|
410
365
|
units,
|
|
411
366
|
list_usd: list,
|
|
412
|
-
cost_usd:
|
|
367
|
+
cost_usd: list,
|
|
413
368
|
vendor_ref: vendorRef,
|
|
414
369
|
};
|
|
415
370
|
return { settlement, usage };
|
|
@@ -420,7 +375,7 @@ export function inferenceEndpoint(options) {
|
|
|
420
375
|
const { settlement, usage } = result;
|
|
421
376
|
if (!appPaid)
|
|
422
377
|
await record(settlement);
|
|
423
|
-
log({ sub, model, served_model: settlement.served_model, admission: admission.admissionId, upstream, list_usd: settlement.list_usd, cost_usd: settlement.cost_usd, usage });
|
|
378
|
+
log({ sub, model, served_model: settlement.served_model, admission: admission.admissionId, upstream: 'api_key', list_usd: settlement.list_usd, cost_usd: settlement.cost_usd, usage });
|
|
424
379
|
}).catch((error) => { log({ sub, admission: admission.admissionId, settle_failed: String(error) }); });
|
|
425
380
|
const charge = calculated.then((result) => result === undefined || appPaid ? undefined : { id: admission.chargeId, served_model: result.settlement.served_model, list_usd: result.settlement.list_usd }, () => undefined);
|
|
426
381
|
return new Response(chargedBody(toCaller, contentType, charge), { status: answer.status, statusText: answer.statusText, headers: responseHeaders });
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
name: northlight
|
|
3
3
|
description: "Use when building, debugging or shipping an app that signs people in with Northlight, starts runs on their Northlight runtime, or calls Northlight providers (sms, search, integrations, context, device) with @north-light/sdk."
|
|
4
4
|
metadata:
|
|
5
|
-
sdk-version: "0.3.
|
|
5
|
+
sdk-version: "0.3.838"
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
# Building a Northlight app
|
|
@@ -27,7 +27,7 @@ Never text a person first: Comms refuses a send to anyone who has not texted you
|
|
|
27
27
|
|
|
28
28
|
## First: check this skill matches the SDK
|
|
29
29
|
|
|
30
|
-
This skill is for SDK version `0.3.
|
|
30
|
+
This skill is for SDK version `0.3.838`. If the project has `node_modules/@north-light/sdk/package.json`, compare its `version` with that number. If they differ, run `npx @north-light/sdk skill` in the project, then read the rewritten `.agents/skills/northlight/SKILL.md` (or `.claude/skills/northlight/SKILL.md`) instead of this copy. It needs no account. A project that has no SDK yet starts with `npx @north-light/sdk@latest init <dir>`.
|
|
31
31
|
|
|
32
32
|
## Where things run
|
|
33
33
|
|
package/dist/version.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** This SDK's release version. Informational only: compatibility is the contract level, not this number. */
|
|
2
|
-
export declare const SDK_VERSION = "0.3.
|
|
2
|
+
export declare const SDK_VERSION = "0.3.838";
|
|
3
3
|
/** The release sent in `Crouter-Minimum-Runtime-Version` on every request. It is frozen, so a runtime from before the
|
|
4
4
|
* contract level existed (which only knows this header) refuses this SDK, while every later runtime accepts it. */
|
|
5
5
|
export declare const LEGACY_FLOOR = "0.3.423";
|
package/dist/version.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// SDK_VERSION is written by scripts/release-version.mjs on every release; do not edit by hand.
|
|
2
2
|
/** This SDK's release version. Informational only: compatibility is the contract level, not this number. */
|
|
3
|
-
export const SDK_VERSION = '0.3.
|
|
3
|
+
export const SDK_VERSION = '0.3.838';
|
|
4
4
|
// LEGACY_FLOOR is written once by `scripts/contract.mjs release`, at the first release that carries the contract level
|
|
5
5
|
// (the `0.0.0` placeholder is replaced there and never again); do not edit by hand.
|
|
6
6
|
/** The release sent in `Crouter-Minimum-Runtime-Version` on every request. It is frozen, so a runtime from before the
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@north-light/sdk",
|
|
3
|
-
"version": "0.3.
|
|
4
|
-
"crouterContract": "1.
|
|
3
|
+
"version": "0.3.838",
|
|
4
|
+
"crouterContract": "1.97",
|
|
5
5
|
"description": "Typed Node and browser client for running crouter agents through the crtrd /v1 API.",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.js",
|
|
@@ -44,9 +44,9 @@
|
|
|
44
44
|
"prepublishOnly": "node -e \"if (process.env.npm_config_tag !== 'next') { console.error('@north-light/sdk publishes only with --tag next; scripts/promote-sdk.mjs is the only mover of latest.'); process.exit(1); }\""
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@north-light/api": "0.3.
|
|
48
|
-
"@north-light/identity": "0.3.
|
|
49
|
-
"@north-light/plugin": "0.3.
|
|
47
|
+
"@north-light/api": "0.3.838",
|
|
48
|
+
"@north-light/identity": "0.3.838",
|
|
49
|
+
"@north-light/plugin": "0.3.838",
|
|
50
50
|
"jose": "^6.2.1"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|