@nylorun/runtime 0.13.0-beta → 0.15.0-beta
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 +434 -0
- package/README.md +137 -41
- package/dist/adapters/execution/restate.d.ts +7 -2
- package/dist/adapters/execution/restate.js +40 -10
- package/dist/adapters/replication/pgoutput.d.ts +21 -0
- package/dist/adapters/replication/pgoutput.js +170 -0
- package/dist/adapters/streams/s2.js +37 -20
- package/dist/admin-openapi.json +570 -0
- package/dist/api/a2a/card.d.ts +15 -0
- package/dist/api/a2a/card.js +24 -0
- package/dist/api/a2a/endpoint.d.ts +8 -0
- package/dist/api/a2a/endpoint.js +81 -0
- package/dist/api/a2a/protocol.d.ts +144 -0
- package/dist/api/a2a/protocol.js +188 -0
- package/dist/api/a2a/routes.d.ts +19 -0
- package/dist/api/a2a/routes.js +289 -0
- package/dist/api/a2a/tasks.d.ts +50 -0
- package/dist/api/a2a/tasks.js +186 -0
- package/dist/api/ag-ui/endpoint.d.ts +8 -0
- package/dist/api/ag-ui/endpoint.js +126 -0
- package/dist/api/ag-ui/history.d.ts +7 -0
- package/dist/api/ag-ui/history.js +60 -0
- package/dist/api/ag-ui/routes.d.ts +32 -0
- package/dist/api/ag-ui/routes.js +305 -0
- package/dist/api/ag-ui/session-id.d.ts +1 -0
- package/dist/api/ag-ui/session-id.js +12 -0
- package/dist/api/ag-ui/sse.d.ts +9 -0
- package/dist/api/ag-ui/sse.js +7 -0
- package/dist/api/ag-ui/translate.d.ts +38 -0
- package/dist/api/ag-ui/translate.js +222 -0
- package/dist/api/components.d.ts +8065 -0
- package/dist/api/components.js +82 -0
- package/dist/api/gate/keys.d.ts +14 -0
- package/dist/api/gate/keys.js +59 -0
- package/dist/api/gate/routes.d.ts +34 -0
- package/dist/api/gate/routes.js +235 -0
- package/dist/api/http/app.d.ts +18 -0
- package/dist/api/http/app.js +58 -0
- package/dist/api/http/body.d.ts +4 -0
- package/dist/api/http/body.js +37 -0
- package/dist/api/http/define.d.ts +39 -0
- package/dist/api/http/define.js +122 -0
- package/dist/api/http/respond.d.ts +9 -0
- package/dist/api/http/respond.js +44 -0
- package/dist/api/http/routes/access.d.ts +24 -0
- package/dist/api/http/routes/access.js +266 -0
- package/dist/api/http/routes/actions.d.ts +8 -0
- package/dist/api/http/routes/actions.js +76 -0
- package/dist/api/http/routes/endpoints.d.ts +7 -0
- package/dist/api/http/routes/endpoints.js +100 -0
- package/dist/api/http/routes/sessions.d.ts +3 -0
- package/dist/api/http/routes/sessions.js +205 -0
- package/dist/api/http/routes/tenant.d.ts +11 -0
- package/dist/api/http/routes/tenant.js +244 -0
- package/dist/api/http/routes/vaults.d.ts +8 -0
- package/dist/api/http/routes/vaults.js +167 -0
- package/dist/api/openapi.d.ts +19 -0
- package/dist/api/openapi.js +137 -0
- package/dist/api/route.d.ts +12 -0
- package/dist/api/route.js +4 -0
- package/dist/core/bearer.d.ts +8 -0
- package/dist/core/bearer.js +11 -0
- package/dist/core/flow-host.d.ts +7 -15
- package/dist/core/flow-host.js +16 -41
- package/dist/core/sandbox-routes.d.ts +12 -7
- package/dist/core/sandbox-routes.js +24 -25
- package/dist/execution/memory.d.ts +8 -5
- package/dist/execution/memory.js +33 -13
- package/dist/execution/types.d.ts +38 -6
- package/dist/execution/types.js +12 -3
- package/dist/gates/contract.d.ts +51 -0
- package/dist/gates/contract.js +37 -0
- package/dist/gates/handler.d.ts +21 -0
- package/dist/gates/handler.js +55 -0
- package/dist/gates/http-client.d.ts +12 -0
- package/dist/gates/http-client.js +160 -0
- package/dist/gates/in-process.d.ts +32 -0
- package/dist/gates/in-process.js +42 -0
- package/dist/gates/inflight.d.ts +39 -0
- package/dist/gates/inflight.js +70 -0
- package/dist/gates/mcp-handler.d.ts +35 -0
- package/dist/gates/mcp-handler.js +142 -0
- package/dist/gates/meter.d.ts +24 -0
- package/dist/gates/meter.js +165 -0
- package/dist/gates/model-gate.d.ts +45 -0
- package/dist/gates/model-gate.js +1 -0
- package/dist/gates/tenant-vaults.d.ts +64 -0
- package/dist/gates/tenant-vaults.js +94 -0
- package/dist/gates/tool-calls.d.ts +51 -0
- package/dist/gates/tool-calls.js +64 -0
- package/dist/gates/tool-client.d.ts +12 -0
- package/dist/gates/tool-client.js +201 -0
- package/dist/gates/tool-contract.d.ts +116 -0
- package/dist/gates/tool-contract.js +64 -0
- package/dist/gates/tool-gate.d.ts +45 -0
- package/dist/gates/tool-gate.js +10 -0
- package/dist/host/admin-api.d.ts +23 -0
- package/dist/host/admin-api.js +74 -0
- package/dist/host/app.d.ts +34 -0
- package/dist/host/app.js +181 -0
- package/dist/host/config-for.d.ts +3 -1
- package/dist/host/config-for.js +3 -2
- package/dist/host/config.d.ts +11 -0
- package/dist/host/cors.d.ts +27 -0
- package/dist/host/cors.js +64 -0
- package/dist/host/create-host.d.ts +36 -8
- package/dist/host/create-host.js +161 -330
- package/dist/host/execution.d.ts +20 -18
- package/dist/host/execution.js +2 -2
- package/dist/host/gates.d.ts +58 -0
- package/dist/host/gates.js +127 -0
- package/dist/host/http.d.ts +20 -4
- package/dist/host/http.js +81 -23
- package/dist/host/index.d.ts +1 -1
- package/dist/host/main.js +189 -58
- package/dist/host/stack-config.d.ts +91 -19
- package/dist/host/stack-config.js +246 -17
- package/dist/infra/database.d.ts +2 -2
- package/dist/infra/execution.d.ts +5 -5
- package/dist/infra/execution.js +8 -6
- package/dist/infra/index.d.ts +1 -1
- package/dist/infra/workers.d.ts +17 -17
- package/dist/infra/workers.js +9 -9
- package/dist/keys/client.d.ts +9 -0
- package/dist/keys/client.js +70 -0
- package/dist/keys/contract.d.ts +24 -0
- package/dist/keys/contract.js +12 -0
- package/dist/keys/keys.d.ts +41 -0
- package/dist/keys/keys.js +50 -0
- package/dist/mcp/connect.d.ts +43 -3
- package/dist/mcp/connect.js +12 -5
- package/dist/mcp/pool.d.ts +39 -3
- package/dist/mcp/pool.js +74 -11
- package/dist/model/catalog.d.ts +2 -10
- package/dist/model/classify.d.ts +13 -0
- package/dist/model/classify.js +61 -0
- package/dist/model/models.d.ts +10 -1
- package/dist/model/models.js +14 -6
- package/dist/model/pi-model.d.ts +17 -2
- package/dist/model/pi-model.js +366 -222
- package/dist/openapi.json +25889 -0
- package/dist/record/append.d.ts +51 -0
- package/dist/record/append.js +20 -0
- package/dist/record/envelope.d.ts +19 -0
- package/dist/record/envelope.js +49 -0
- package/dist/record/index.d.ts +11 -0
- package/dist/record/index.js +11 -0
- package/dist/sandbox/manager.d.ts +13 -1
- package/dist/sandbox/manager.js +8 -11
- package/dist/sandbox/resolve.d.ts +33 -0
- package/dist/sandbox/resolve.js +81 -0
- package/dist/sandbox/session-sandbox.d.ts +11 -0
- package/dist/sandbox/session-sandbox.js +51 -0
- package/dist/sandbox/share.d.ts +14 -2
- package/dist/sandbox/share.js +33 -3
- package/dist/sandbox/tenant-config.d.ts +37 -0
- package/dist/sandbox/tenant-config.js +51 -0
- package/dist/sandbox/tools.d.ts +7 -9
- package/dist/sandbox/tools.js +11 -3
- package/dist/sandbox/types.d.ts +8 -2
- package/dist/store/postgres/connect.d.ts +25 -9
- package/dist/store/postgres/connect.js +27 -10
- package/dist/store/postgres/db.d.ts +16 -0
- package/dist/store/postgres/db.js +28 -0
- package/dist/store/postgres/drizzle/0000_foundation.sql +22 -0
- package/dist/store/postgres/drizzle/0001_baseline.sql +255 -0
- package/dist/store/postgres/drizzle/0002_stream_relay.sql +4 -0
- package/dist/store/postgres/drizzle/0003_tool_crossings.sql +9 -0
- package/dist/store/postgres/drizzle/meta/_journal.json +34 -0
- package/dist/store/postgres/migrate.d.ts +68 -0
- package/dist/store/postgres/migrate.js +183 -0
- package/dist/store/postgres/record-writer.d.ts +4 -0
- package/dist/store/postgres/record-writer.js +34 -0
- package/dist/store/postgres/record.d.ts +5 -0
- package/dist/store/postgres/record.js +60 -0
- package/dist/store/postgres/schema.d.ts +3096 -0
- package/dist/store/postgres/schema.js +363 -0
- package/dist/store/postgres/store.d.ts +2 -4
- package/dist/store/postgres/store.js +590 -434
- package/dist/store/postgres/tenant.d.ts +56 -0
- package/dist/store/postgres/tenant.js +154 -0
- package/dist/store/types.d.ts +183 -132
- package/dist/store/types.js +0 -1
- package/dist/streams/basin.d.ts +18 -1
- package/dist/streams/basin.js +34 -2
- package/dist/streams/control.d.ts +18 -0
- package/dist/streams/control.js +33 -0
- package/dist/streams/memory.d.ts +3 -1
- package/dist/streams/memory.js +14 -8
- package/dist/streams/relay/core.d.ts +35 -0
- package/dist/streams/relay/core.js +366 -0
- package/dist/streams/relay/types.d.ts +78 -0
- package/dist/streams/relay/types.js +8 -0
- package/dist/streams/types.d.ts +71 -36
- package/dist/streams/types.js +18 -53
- package/dist/tenant/access-policy.d.ts +30 -0
- package/dist/tenant/access-policy.js +52 -0
- package/dist/tenant/actions.d.ts +10 -149
- package/dist/tenant/actions.js +5 -167
- package/dist/tenant/advance.d.ts +7 -2
- package/dist/tenant/advance.js +125 -14
- package/dist/tenant/auth.d.ts +19 -17
- package/dist/tenant/auth.js +90 -93
- package/dist/tenant/browser.d.ts +15 -0
- package/dist/tenant/browser.js +39 -0
- package/dist/tenant/cause.d.ts +25 -0
- package/dist/tenant/cause.js +45 -0
- package/dist/tenant/commands.d.ts +18 -2
- package/dist/tenant/commands.js +111 -115
- package/dist/tenant/context.d.ts +101 -21
- package/dist/tenant/context.js +14 -10
- package/dist/tenant/delivery-token.d.ts +41 -0
- package/dist/tenant/delivery-token.js +104 -0
- package/dist/tenant/delivery.d.ts +62 -0
- package/dist/tenant/delivery.js +481 -0
- package/dist/tenant/effects.d.ts +20 -1
- package/dist/tenant/effects.js +159 -42
- package/dist/tenant/endpoints.d.ts +67 -0
- package/dist/tenant/endpoints.js +90 -0
- package/dist/tenant/ephemeral.d.ts +22 -7
- package/dist/tenant/ephemeral.js +83 -125
- package/dist/tenant/history.d.ts +64 -0
- package/dist/tenant/history.js +120 -0
- package/dist/tenant/http.d.ts +8 -9
- package/dist/tenant/http.js +13 -27
- package/dist/tenant/jwt.d.ts +16 -0
- package/dist/tenant/jwt.js +50 -0
- package/dist/tenant/model-setting.d.ts +2 -2
- package/dist/tenant/module.d.ts +17 -28
- package/dist/tenant/module.js +133 -279
- package/dist/tenant/outbound.d.ts +44 -0
- package/dist/tenant/outbound.js +127 -0
- package/dist/tenant/paths.d.ts +11 -16
- package/dist/tenant/paths.js +24 -68
- package/dist/tenant/pool.d.ts +1 -3
- package/dist/tenant/pool.js +1 -19
- package/dist/tenant/principals.d.ts +23 -11
- package/dist/tenant/principals.js +61 -49
- package/dist/tenant/reset.d.ts +0 -4
- package/dist/tenant/reset.js +0 -5
- package/dist/tenant/runtime.d.ts +28 -20
- package/dist/tenant/runtime.js +95 -64
- package/dist/tenant/scheduler.d.ts +10 -3
- package/dist/tenant/scheduler.js +9 -5
- package/dist/tenant/session-streams.d.ts +127 -0
- package/dist/tenant/session-streams.js +404 -0
- package/dist/tenant/sessions.d.ts +28 -4
- package/dist/tenant/sessions.js +118 -24
- package/dist/tenant/signing-keys.d.ts +55 -0
- package/dist/tenant/signing-keys.js +172 -0
- package/dist/tenant/slim.d.ts +8 -0
- package/dist/tenant/slim.js +21 -0
- package/dist/tenant/status.d.ts +3 -3
- package/dist/tenant/status.js +20 -13
- package/dist/tenant/store-pg.d.ts +9 -6
- package/dist/tenant/store-pg.js +60 -125
- package/dist/tenant/streams.d.ts +62 -85
- package/dist/tenant/streams.js +143 -226
- package/dist/tenant/subject-limits.d.ts +16 -0
- package/dist/tenant/subject-limits.js +45 -0
- package/dist/tenant/sweep.d.ts +1 -10
- package/dist/tenant/sweep.js +29 -69
- package/dist/tenant/tokens.d.ts +14 -0
- package/dist/tenant/tokens.js +180 -0
- package/dist/tenant/transcript.d.ts +48 -5
- package/dist/tenant/transcript.js +58 -1
- package/dist/tenant/types.d.ts +69 -98
- package/dist/tenant/types.js +4 -15
- package/dist/tenant/worker.d.ts +3 -1
- package/dist/tenant/worker.js +13 -7
- package/dist/vault/host-model.d.ts +31 -0
- package/dist/vault/host-model.js +57 -0
- package/dist/vault/service.d.ts +44 -17
- package/dist/vault/service.js +126 -121
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +25 -9
- package/dist/core/executors.d.ts +0 -44
- package/dist/core/executors.js +0 -95
- package/dist/store/memory.d.ts +0 -57
- package/dist/store/memory.js +0 -645
- package/dist/store/postgres/migrations/001_initial.d.ts +0 -27
- package/dist/store/postgres/migrations/001_initial.js +0 -196
- package/dist/store/postgres/migrations/002_session_owner.d.ts +0 -6
- package/dist/store/postgres/migrations/002_session_owner.js +0 -13
- package/dist/store/postgres/migrations/index.d.ts +0 -49
- package/dist/store/postgres/migrations/index.js +0 -76
- package/dist/store/postgres/names.d.ts +0 -11
- package/dist/store/postgres/names.js +0 -42
- package/dist/store/postgres/tenants.d.ts +0 -90
- package/dist/store/postgres/tenants.js +0 -214
- package/dist/streams/relay.d.ts +0 -37
- package/dist/streams/relay.js +0 -254
- package/dist/tenant/envelope.d.ts +0 -7
- package/dist/tenant/envelope.js +0 -22
- package/dist/tenant/live.d.ts +0 -92
- package/dist/tenant/live.js +0 -290
- package/dist/tenant/quarantine-error.d.ts +0 -6
- package/dist/tenant/quarantine-error.js +0 -9
- package/dist/tenant/quarantine.d.ts +0 -27
- package/dist/tenant/quarantine.js +0 -72
- package/dist/tenant/routes-tenant.d.ts +0 -12
- package/dist/tenant/routes-tenant.js +0 -137
- package/dist/tenant/routes.d.ts +0 -9
- package/dist/tenant/routes.js +0 -151
- package/dist/tenant/store-memory.d.ts +0 -16
- package/dist/tenant/store-memory.js +0 -111
- /package/dist/{store → record}/cursor.d.ts +0 -0
- /package/dist/{store → record}/cursor.js +0 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { AuthScope, TenantContext } from "./context.js";
|
|
2
|
+
/** What a delivery token is for: one delivery of an Action, or a ping. */
|
|
3
|
+
export type DeliveryTokenFor = {
|
|
4
|
+
kind: "action";
|
|
5
|
+
actionId: string;
|
|
6
|
+
agentId: string;
|
|
7
|
+
generation: number;
|
|
8
|
+
} | {
|
|
9
|
+
kind: "ping";
|
|
10
|
+
agentId: string;
|
|
11
|
+
};
|
|
12
|
+
export interface MintedDeliveryToken {
|
|
13
|
+
token: string;
|
|
14
|
+
/** Milliseconds since the epoch. */
|
|
15
|
+
expiresAt: number;
|
|
16
|
+
tokenId: string;
|
|
17
|
+
keyId: string;
|
|
18
|
+
}
|
|
19
|
+
/** The request scope of a verified delivery token (`AuthScope` kind `delivery`). */
|
|
20
|
+
export type DeliveryScope = Extract<AuthScope, {
|
|
21
|
+
kind: "delivery";
|
|
22
|
+
}>;
|
|
23
|
+
/** base64url SHA-256 of a request body: the token's `bdy` claim. */
|
|
24
|
+
export declare function bodyHash(body: string | Uint8Array): string;
|
|
25
|
+
/**
|
|
26
|
+
* Signs a token for `delivery`, for the endpoint at `audience`, covering `body`. `ttlSeconds`
|
|
27
|
+
* is capped at `DELIVERY_TOKEN_MAX_TTL_SECONDS`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function mintDeliveryToken(ctx: TenantContext, options: {
|
|
30
|
+
for: DeliveryTokenFor;
|
|
31
|
+
audience: string;
|
|
32
|
+
body: string | Uint8Array;
|
|
33
|
+
ttlSeconds: number;
|
|
34
|
+
}): Promise<MintedDeliveryToken>;
|
|
35
|
+
/**
|
|
36
|
+
* Verifies a delivery token presented as a bearer. Answers `401 token_expired` for an expired
|
|
37
|
+
* token or a revoked key, and the opaque 404 for everything else, a ping token included: only
|
|
38
|
+
* an Action's token reaches its callbacks. Whether the Action is still being delivered at the
|
|
39
|
+
* token's generation is checked where the Action is read (`auth.ts` `scoped`).
|
|
40
|
+
*/
|
|
41
|
+
export declare function verifyDeliveryToken(ctx: TenantContext, raw: string): Promise<DeliveryScope>;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Delivery tokens (design: Action endpoints §8.1): the ES256 JWT the Runtime signs, with the
|
|
3
|
+
* Tenant's current signing key, for one delivery of one Action, or for a ping. It travels in
|
|
4
|
+
* `Nylorun-Signature` on the request to the Action endpoint, which verifies it with the
|
|
5
|
+
* Tenant's public keys, and comes back as the bearer of that Action's callbacks (a background
|
|
6
|
+
* result, heartbeats, sandbox tools).
|
|
7
|
+
*
|
|
8
|
+
* A token lives at most `DELIVERY_TOKEN_MAX_TTL_SECONDS`, the subject-token maximum, and key
|
|
9
|
+
* rotation waits at least that long before it revokes the previous key
|
|
10
|
+
* (`signing-keys.ts`), so a rotation never revokes a live token.
|
|
11
|
+
*/
|
|
12
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
13
|
+
import { errors, jwtVerify } from "jose";
|
|
14
|
+
import { DELIVERY_TOKEN_MAX_TTL_SECONDS, DELIVERY_TOKEN_TYPE, subjectTokenIssuer, } from "@nylorun/core/contracts";
|
|
15
|
+
import { failOpaque, HttpError } from "./http.js";
|
|
16
|
+
import { CLOCK_TOLERANCE_SECONDS, tokenKeyId } from "./jwt.js";
|
|
17
|
+
/** base64url SHA-256 of a request body: the token's `bdy` claim. */
|
|
18
|
+
export function bodyHash(body) {
|
|
19
|
+
return createHash("sha256").update(body).digest("base64url");
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Signs a token for `delivery`, for the endpoint at `audience`, covering `body`. `ttlSeconds`
|
|
23
|
+
* is capped at `DELIVERY_TOKEN_MAX_TTL_SECONDS`.
|
|
24
|
+
*/
|
|
25
|
+
export async function mintDeliveryToken(ctx, options) {
|
|
26
|
+
const ttl = Math.max(1, Math.min(Math.floor(options.ttlSeconds), DELIVERY_TOKEN_MAX_TTL_SECONDS));
|
|
27
|
+
const iat = Math.floor(Date.now() / 1000);
|
|
28
|
+
const tokenId = randomUUID();
|
|
29
|
+
const delivery = options.for;
|
|
30
|
+
// Signed by the keys service (F4.2): this process never holds the private key.
|
|
31
|
+
const { token, keyId } = await ctx.keys.sign({
|
|
32
|
+
typ: DELIVERY_TOKEN_TYPE,
|
|
33
|
+
claims: {
|
|
34
|
+
agt: delivery.agentId,
|
|
35
|
+
gen: delivery.kind === "action" ? delivery.generation : 0,
|
|
36
|
+
bdy: bodyHash(options.body),
|
|
37
|
+
iss: subjectTokenIssuer(ctx.config.tenantId),
|
|
38
|
+
aud: options.audience,
|
|
39
|
+
sub: delivery.kind === "action" ? delivery.actionId : "ping",
|
|
40
|
+
iat,
|
|
41
|
+
exp: iat + ttl,
|
|
42
|
+
jti: tokenId,
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
return { token, expiresAt: (iat + ttl) * 1000, tokenId, keyId };
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Verifies a delivery token presented as a bearer. Answers `401 token_expired` for an expired
|
|
49
|
+
* token or a revoked key, and the opaque 404 for everything else, a ping token included: only
|
|
50
|
+
* an Action's token reaches its callbacks. Whether the Action is still being delivered at the
|
|
51
|
+
* token's generation is checked where the Action is read (`auth.ts` `scoped`).
|
|
52
|
+
*/
|
|
53
|
+
export async function verifyDeliveryToken(ctx, raw) {
|
|
54
|
+
const checked = tokenKeyId(raw, DELIVERY_TOKEN_TYPE);
|
|
55
|
+
if ("refused" in checked)
|
|
56
|
+
return refused(ctx, `delivery_token_${checked.refused}`);
|
|
57
|
+
const { kid } = checked;
|
|
58
|
+
const row = await ctx.store.tx((t) => t.signingKey(kid));
|
|
59
|
+
if (!row)
|
|
60
|
+
return refused(ctx, "delivery_token_key_unknown");
|
|
61
|
+
let claims;
|
|
62
|
+
try {
|
|
63
|
+
const verified = await jwtVerify(raw, await ctx.signingKeys.publicKey(row), {
|
|
64
|
+
algorithms: ["ES256"],
|
|
65
|
+
issuer: subjectTokenIssuer(ctx.config.tenantId),
|
|
66
|
+
typ: DELIVERY_TOKEN_TYPE,
|
|
67
|
+
clockTolerance: CLOCK_TOLERANCE_SECONDS,
|
|
68
|
+
requiredClaims: ["sub", "aud", "iat", "exp", "jti"],
|
|
69
|
+
});
|
|
70
|
+
claims = verified.payload;
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (error instanceof errors.JWTExpired)
|
|
74
|
+
expired(ctx, "delivery_token_expired");
|
|
75
|
+
return refused(ctx, "delivery_token_invalid");
|
|
76
|
+
}
|
|
77
|
+
if (row.state === "revoked")
|
|
78
|
+
expired(ctx, "delivery_token_key_revoked");
|
|
79
|
+
const { sub, agt, gen, exp, iat, jti } = claims;
|
|
80
|
+
if (sub === "ping" ||
|
|
81
|
+
typeof agt !== "string" ||
|
|
82
|
+
typeof gen !== "number" ||
|
|
83
|
+
!Number.isInteger(gen) ||
|
|
84
|
+
gen < 1 ||
|
|
85
|
+
exp - iat > DELIVERY_TOKEN_MAX_TTL_SECONDS)
|
|
86
|
+
return refused(ctx, "delivery_token_claims");
|
|
87
|
+
return {
|
|
88
|
+
kind: "delivery",
|
|
89
|
+
actionId: sub,
|
|
90
|
+
agentId: agt,
|
|
91
|
+
generation: gen,
|
|
92
|
+
expiresAt: exp * 1000,
|
|
93
|
+
tokenId: jti,
|
|
94
|
+
keyId: kid,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function refused(ctx, reason) {
|
|
98
|
+
ctx.config.logger.warn("credential rejected", { reason });
|
|
99
|
+
return failOpaque();
|
|
100
|
+
}
|
|
101
|
+
function expired(ctx, reason) {
|
|
102
|
+
ctx.config.logger.warn("delivery token refused", { reason });
|
|
103
|
+
throw new HttpError(401, "The delivery token has expired or its key was revoked", { code: "token_expired" }, { "www-authenticate": 'Bearer error="invalid_token"' });
|
|
104
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type Action, type ActionOutcome, type DeliveryHeartbeatResponse, type EndpointPingResponse } from "@nylorun/core/contracts";
|
|
2
|
+
import type { DeliverResult } from "../execution/types.js";
|
|
3
|
+
import type { Tx } from "../store/types.js";
|
|
4
|
+
import { type Session, type TenantContext } from "./context.js";
|
|
5
|
+
import { type DeliveryScope } from "./delivery-token.js";
|
|
6
|
+
import type { OutboundResult } from "./outbound.js";
|
|
7
|
+
/**
|
|
8
|
+
* Offers an Action that just became `pending` to its agent's endpoint, after the transaction
|
|
9
|
+
* commits. Without an endpoint it waits for the agent's next registration (`PUT /v1/endpoints`).
|
|
10
|
+
*/
|
|
11
|
+
export declare function offerAction(t: Tx, ctx: Pick<TenantContext, "deliver">, action: Pick<Action, "actionId" | "agentId">): Promise<void>;
|
|
12
|
+
/** Delivers one Action (`WorkerHandlers.deliver`). Throws only on infrastructure errors. */
|
|
13
|
+
export declare function deliverAction(ctx: TenantContext, actionId: string, signal: AbortSignal): Promise<DeliverResult>;
|
|
14
|
+
type Settlement = {
|
|
15
|
+
kind: "outcome";
|
|
16
|
+
outcome: ActionOutcome;
|
|
17
|
+
} | {
|
|
18
|
+
kind: "accepted";
|
|
19
|
+
} | {
|
|
20
|
+
kind: "retry";
|
|
21
|
+
code: string;
|
|
22
|
+
message: string;
|
|
23
|
+
retryAfterMs?: number;
|
|
24
|
+
} | {
|
|
25
|
+
kind: "lost";
|
|
26
|
+
code: string;
|
|
27
|
+
message: string;
|
|
28
|
+
};
|
|
29
|
+
/** What an endpoint's answer means for the Action (design §5.2, §5.3). */
|
|
30
|
+
export declare function interpret(action: Action, result: OutboundResult): Settlement;
|
|
31
|
+
/**
|
|
32
|
+
* A delivery that may have reached the endpoint got no answer. A tool becomes `uncertain`, with
|
|
33
|
+
* its effect and its session. A hook,
|
|
34
|
+
* `fn` or `verify` is pure or repeat-safe and goes back to `pending`: delivered again right away
|
|
35
|
+
* (`after-commit`) or by the caller's retry (`retry`). Returns true when it will be delivered again.
|
|
36
|
+
*/
|
|
37
|
+
export declare function loseAction(t: Tx, ctx: Pick<TenantContext, "deliver">, s: Session, action: Action, options: {
|
|
38
|
+
redeliver: "after-commit" | "retry";
|
|
39
|
+
}): Promise<boolean>;
|
|
40
|
+
/**
|
|
41
|
+
* The sweep's part (design §7): deliveries whose deadline passed without an answer are lost,
|
|
42
|
+
* and pending Actions of agents with an endpoint are sent again, in case a send was lost
|
|
43
|
+
* between a commit and the execution. Returns how many deliveries it settled.
|
|
44
|
+
*/
|
|
45
|
+
export declare function sweepDeliveries(ctx: TenantContext, now?: Date): Promise<number>;
|
|
46
|
+
/**
|
|
47
|
+
* `POST /v1/endpoints/:agentId/ping`: a signed ping through the endpoint, so a wrong URL, a
|
|
48
|
+
* tunnel that is down or a handler serving other definitions shows up before the first turn.
|
|
49
|
+
*/
|
|
50
|
+
export declare function pingEndpoint(ctx: TenantContext, agentId: string): Promise<EndpointPingResponse>;
|
|
51
|
+
/**
|
|
52
|
+
* `POST /v1/actions/:id/heartbeat` with a delivery token: an endpoint that answered `202` keeps
|
|
53
|
+
* its delivery alive for another lease, and gets a fresh token for its next callbacks. `409`
|
|
54
|
+
* when the delivery was cancelled, lost or sent again, which tells the endpoint to stop.
|
|
55
|
+
*/
|
|
56
|
+
export declare function deliveryHeartbeat(ctx: TenantContext, scope: DeliveryScope, actionId: string): Promise<DeliveryHeartbeatResponse>;
|
|
57
|
+
/**
|
|
58
|
+
* `POST /v1/actions/:id/result` with a delivery token: the outcome of a delivery answered with
|
|
59
|
+
* `202`. The same result again returns the first receipt; a different one is `409`.
|
|
60
|
+
*/
|
|
61
|
+
export declare function deliveryResult(ctx: TenantContext, scope: DeliveryScope, actionId: string, outcome: ActionOutcome): Promise<Record<string, unknown>>;
|
|
62
|
+
export {};
|