@tangle-network/agent-app 0.60.270 → 0.60.272
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/dist/assistant/index.js +5 -5
- package/dist/chat-react/index.js +3 -3
- package/dist/chat-routes/index.d.ts +1 -0
- package/dist/chat-routes/index.js +6 -5
- package/dist/chat-routes/index.js.map +1 -1
- package/dist/chat-routes/minted-thread.d.ts +63 -0
- package/dist/chat-routes/turn-route-contracts.d.ts +12 -3
- package/dist/chat-routes/turn-routes.d.ts +1 -1
- package/dist/chat-routes/wire.d.ts +7 -0
- package/dist/chat-store/index.js +2 -2
- package/dist/{chunk-UJKWM4UY.js → chunk-DJIN3YUJ.js} +1 -59
- package/dist/chunk-DJIN3YUJ.js.map +1 -0
- package/dist/chunk-F4TUZYMX.js +63 -0
- package/dist/chunk-F4TUZYMX.js.map +1 -0
- package/dist/{chunk-RZLCBWGG.js → chunk-FAXSYC2C.js} +2 -2
- package/dist/{chunk-5TSRFF5W.js → chunk-G7M2C72Q.js} +1 -1
- package/dist/chunk-G7M2C72Q.js.map +1 -0
- package/dist/{chunk-NHVSO47Q.js → chunk-IUVDR3KJ.js} +2 -2
- package/dist/{chunk-CNBVWPQC.js → chunk-LXYQOOXU.js} +2 -2
- package/dist/{chunk-3RRYI24K.js → chunk-S2IEOSN3.js} +22 -4
- package/dist/chunk-S2IEOSN3.js.map +1 -0
- package/dist/{chunk-O2RCDK5D.js → chunk-SKI2F3NL.js} +7 -3
- package/dist/chunk-SKI2F3NL.js.map +1 -0
- package/dist/operator/index.js +2 -2
- package/dist/platform/billable-turn-admission.d.ts +83 -0
- package/dist/platform/index.d.ts +1 -0
- package/dist/platform/index.js +49 -0
- package/dist/platform/index.js.map +1 -1
- package/dist/public-consultation/index.d.ts +2 -2
- package/dist/public-consultation/index.js +3 -3
- package/dist/sandbox/index.js +4 -2
- package/dist/web-react/chat-stream.d.ts +7 -0
- package/dist/web-react/index.js +7 -5
- package/package.json +1 -1
- package/dist/chunk-3RRYI24K.js.map +0 -1
- package/dist/chunk-5TSRFF5W.js.map +0 -1
- package/dist/chunk-O2RCDK5D.js.map +0 -1
- package/dist/chunk-UJKWM4UY.js.map +0 -1
- /package/dist/{chunk-RZLCBWGG.js.map → chunk-FAXSYC2C.js.map} +0 -0
- /package/dist/{chunk-NHVSO47Q.js.map → chunk-IUVDR3KJ.js.map} +0 -0
- /package/dist/{chunk-CNBVWPQC.js.map → chunk-LXYQOOXU.js.map} +0 -0
package/dist/operator/index.js
CHANGED
|
@@ -5,11 +5,11 @@ import {
|
|
|
5
5
|
} from "../chunk-XB4KZVUV.js";
|
|
6
6
|
import {
|
|
7
7
|
parseChatAttachmentInputs
|
|
8
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-IUVDR3KJ.js";
|
|
9
9
|
import {
|
|
10
10
|
createApiKeyFetch
|
|
11
11
|
} from "../chunk-RN4WPTBH.js";
|
|
12
|
-
import "../chunk-
|
|
12
|
+
import "../chunk-G7M2C72Q.js";
|
|
13
13
|
import {
|
|
14
14
|
messageHasTurnId
|
|
15
15
|
} from "../chunk-YG66IHDU.js";
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type WarmCacheOptions, type WarmCacheSource } from '../sandbox/warm-cache';
|
|
2
|
+
/**
|
|
3
|
+
* A billable turn's seat and balance verdict, read from Platform once per user
|
|
4
|
+
* per window instead of on every turn. Lifted from Creative
|
|
5
|
+
* (creative-agent#692): in production on 2026-10-21 its seat and balance reads
|
|
6
|
+
* took 723 ms each, most of the 949 ms of its turn gates.
|
|
7
|
+
*
|
|
8
|
+
* Only a passing verdict is kept. A refusal is never cached, so a seatless or
|
|
9
|
+
* unfunded user always gets the live read and its 402. Within the window the
|
|
10
|
+
* balance stays enforced where money moves: the Tangle Router holds spend
|
|
11
|
+
* before it calls a provider and refuses a balance at or below zero. The entry
|
|
12
|
+
* ends at `windowMs` or at the seat's `paidThrough`, whichever is first, and is
|
|
13
|
+
* dropped on any refusal: a seat or balance refusal read here, or one the
|
|
14
|
+
* product sees elsewhere and reports with `forget` (Creative calls it when
|
|
15
|
+
* Platform refuses to fund a turn's Router key). Only the data center's cache
|
|
16
|
+
* holds it (no isolate copy), so that drop reaches every isolate in the data
|
|
17
|
+
* center where it runs. The Workers cache is per data center: a verdict kept
|
|
18
|
+
* in another data center lives on until its own window ends, and the Tangle
|
|
19
|
+
* Router still refuses an unfunded key there.
|
|
20
|
+
*
|
|
21
|
+
* The verdict is keyed by `userId` unless `key` says otherwise: an app whose
|
|
22
|
+
* seat or balance read depends on more than the user (a workspace, an
|
|
23
|
+
* organization) must pass `key`, or one scope's verdict admits another.
|
|
24
|
+
*
|
|
25
|
+
* `windowMs` has no default. Platform sends apps no billing webhook, so the
|
|
26
|
+
* window is how long a seat or balance change made on Platform can go unseen
|
|
27
|
+
* by admission. Creative ships 5 minutes (`5 * 60_000`); the window for other
|
|
28
|
+
* apps is Drew's call.
|
|
29
|
+
*/
|
|
30
|
+
/** A Platform read's verdict: only `allowed` decides admission. */
|
|
31
|
+
export interface BillableTurnVerdict {
|
|
32
|
+
allowed: boolean;
|
|
33
|
+
}
|
|
34
|
+
/** A seat verdict; `paidThrough` (ISO) ends a kept verdict early, `null` when access is not a dated seat. */
|
|
35
|
+
export interface BillableTurnSeatVerdict extends BillableTurnVerdict {
|
|
36
|
+
paidThrough?: string | null;
|
|
37
|
+
}
|
|
38
|
+
export interface BillableTurnAdmissionOptions<Input extends {
|
|
39
|
+
userId: string;
|
|
40
|
+
}, Seat extends BillableTurnSeatVerdict, Balance extends BillableTurnVerdict> {
|
|
41
|
+
/** The cache URL's host, one per product, such as `billing-admission.creative.internal`. */
|
|
42
|
+
namespace: string;
|
|
43
|
+
/** How long a passing verdict is kept. Required: see the module note. */
|
|
44
|
+
windowMs: number;
|
|
45
|
+
readSeat(input: Input): Promise<Seat>;
|
|
46
|
+
readBalance(input: Input): Promise<Balance>;
|
|
47
|
+
/** The deployment environment in the cache key (see {@link WarmCacheOptions.env}). */
|
|
48
|
+
env?: WarmCacheOptions['env'];
|
|
49
|
+
/** What a verdict is kept under: everything `readSeat` and `readBalance` depend on. Default `input.userId`. */
|
|
50
|
+
key?(input: Input): string;
|
|
51
|
+
}
|
|
52
|
+
export interface AdmitBillableTurnOptions {
|
|
53
|
+
/** Turns a Platform read failure into the caller's error. */
|
|
54
|
+
mapError?: (error: unknown) => never;
|
|
55
|
+
/** Names each Platform read (`gate_seat`, `gate_balance`) for the turn's timing. */
|
|
56
|
+
track?: <T>(name: string, work: Promise<T>) => Promise<T>;
|
|
57
|
+
/** Read Platform even over a kept verdict, so the window starts now (a prewarm step). */
|
|
58
|
+
refresh?: boolean;
|
|
59
|
+
}
|
|
60
|
+
export type BillableTurnAdmissionResult<Seat, Balance> = {
|
|
61
|
+
source: WarmCacheSource;
|
|
62
|
+
} | {
|
|
63
|
+
source: 'platform';
|
|
64
|
+
seat: Seat;
|
|
65
|
+
balance?: Balance;
|
|
66
|
+
};
|
|
67
|
+
export interface BillableTurnAdmission<Input extends {
|
|
68
|
+
userId: string;
|
|
69
|
+
}, Seat, Balance> {
|
|
70
|
+
/**
|
|
71
|
+
* Seat, then balance, read concurrently: a seat refusal or seat read failure
|
|
72
|
+
* wins over anything the balance read returned. A kept verdict returns
|
|
73
|
+
* without calling Platform. `balance` is absent only when the seat refused.
|
|
74
|
+
*/
|
|
75
|
+
admit(input: Input & AdmitBillableTurnOptions): Promise<BillableTurnAdmissionResult<Seat, Balance>>;
|
|
76
|
+
/** A refusal seen elsewhere: the next turn under this key (the user id unless `key` is set) reads Platform again. */
|
|
77
|
+
forget(key: string): Promise<void>;
|
|
78
|
+
/** How long a verdict read now is kept: the window, or less when `paidThrough` comes first. */
|
|
79
|
+
heldMs(paidThrough: string | null | undefined): number;
|
|
80
|
+
}
|
|
81
|
+
export declare function createBillableTurnAdmission<Input extends {
|
|
82
|
+
userId: string;
|
|
83
|
+
}, Seat extends BillableTurnSeatVerdict, Balance extends BillableTurnVerdict>(options: BillableTurnAdmissionOptions<Input, Seat, Balance>): BillableTurnAdmission<Input, Seat, Balance>;
|
package/dist/platform/index.d.ts
CHANGED
package/dist/platform/index.js
CHANGED
|
@@ -25,6 +25,9 @@ import {
|
|
|
25
25
|
import {
|
|
26
26
|
assertWorkspaceKeyCryptoUsable
|
|
27
27
|
} from "../chunk-VGS2NXLB.js";
|
|
28
|
+
import {
|
|
29
|
+
createWarmCache
|
|
30
|
+
} from "../chunk-F4TUZYMX.js";
|
|
28
31
|
import {
|
|
29
32
|
resolveTangleExecutionEnvironment
|
|
30
33
|
} from "../chunk-D6UJEOZQ.js";
|
|
@@ -1320,6 +1323,51 @@ function delegatedKeyFallbackFetch(input) {
|
|
|
1320
1323
|
return send();
|
|
1321
1324
|
};
|
|
1322
1325
|
}
|
|
1326
|
+
|
|
1327
|
+
// src/platform/billable-turn-admission.ts
|
|
1328
|
+
function createBillableTurnAdmission(options) {
|
|
1329
|
+
if (!(Number.isFinite(options.windowMs) && options.windowMs > 0)) {
|
|
1330
|
+
throw new Error("createBillableTurnAdmission: windowMs must be a positive number of milliseconds");
|
|
1331
|
+
}
|
|
1332
|
+
const verdicts = createWarmCache(options.namespace, { isolate: false, env: options.env });
|
|
1333
|
+
const keyOf = options.key ?? ((input) => input.userId);
|
|
1334
|
+
const heldMs = (paidThrough) => Math.min(options.windowMs, paidThrough ? Date.parse(paidThrough) - Date.now() : Infinity);
|
|
1335
|
+
return {
|
|
1336
|
+
heldMs,
|
|
1337
|
+
forget: (key) => verdicts.forget(key),
|
|
1338
|
+
async admit(input) {
|
|
1339
|
+
const key = keyOf(input);
|
|
1340
|
+
const warm = input.refresh ? null : await verdicts.read(key);
|
|
1341
|
+
if (warm && (warm.value.paidThrough === null || Date.parse(warm.value.paidThrough) > Date.now())) {
|
|
1342
|
+
return { source: warm.source };
|
|
1343
|
+
}
|
|
1344
|
+
const track = input.track ?? ((_name, work) => work);
|
|
1345
|
+
const mapError = input.mapError ?? ((error) => {
|
|
1346
|
+
throw error;
|
|
1347
|
+
});
|
|
1348
|
+
const [seatSettled, balanceSettled] = await Promise.allSettled([
|
|
1349
|
+
track("gate_seat", options.readSeat(input).catch(mapError)),
|
|
1350
|
+
track("gate_balance", options.readBalance(input).catch(mapError))
|
|
1351
|
+
]);
|
|
1352
|
+
if (seatSettled.status === "rejected") throw seatSettled.reason;
|
|
1353
|
+
const seat = seatSettled.value;
|
|
1354
|
+
if (!seat.allowed) {
|
|
1355
|
+
await verdicts.forget(key);
|
|
1356
|
+
return { source: "platform", seat };
|
|
1357
|
+
}
|
|
1358
|
+
if (balanceSettled.status === "rejected") throw balanceSettled.reason;
|
|
1359
|
+
const balance = balanceSettled.value;
|
|
1360
|
+
if (!balance.allowed) {
|
|
1361
|
+
await verdicts.forget(key);
|
|
1362
|
+
return { source: "platform", seat, balance };
|
|
1363
|
+
}
|
|
1364
|
+
const paidThrough = seat.paidThrough ?? null;
|
|
1365
|
+
const ttlMs = heldMs(paidThrough);
|
|
1366
|
+
if (ttlMs > 0) await verdicts.write(key, { paidThrough }, ttlMs);
|
|
1367
|
+
return { source: "platform", seat, balance };
|
|
1368
|
+
}
|
|
1369
|
+
};
|
|
1370
|
+
}
|
|
1323
1371
|
export {
|
|
1324
1372
|
DEFAULT_SEAT_BILLING_ENABLED_ENV_VAR,
|
|
1325
1373
|
DEFAULT_TANGLE_TIER_POLICY,
|
|
@@ -1341,6 +1389,7 @@ export {
|
|
|
1341
1389
|
createApiKeyRequestAuth,
|
|
1342
1390
|
createAuthGuard,
|
|
1343
1391
|
createBetterAuthSessionCookieMinter,
|
|
1392
|
+
createBillableTurnAdmission,
|
|
1344
1393
|
createD1TangleIdentityStore,
|
|
1345
1394
|
createDelegatedKeyProvisioner,
|
|
1346
1395
|
createDelegatedTurnKeys,
|