@antasphere/chassis-contract 0.0.0-stage → 1.0.1
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/LICENSE +82 -0
- package/dist/define.d.ts +177 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +35 -0
- package/dist/define.js.map +1 -0
- package/dist/entitlements.d.ts +265 -0
- package/dist/entitlements.d.ts.map +1 -0
- package/dist/entitlements.js +155 -0
- package/dist/entitlements.js.map +1 -0
- package/dist/identity.d.ts +56 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +2 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/routes/index.d.ts +5403 -0
- package/dist/routes/index.d.ts.map +1 -0
- package/dist/routes/index.js +940 -0
- package/dist/routes/index.js.map +1 -0
- package/dist/schemas/api-keys.d.ts +54 -0
- package/dist/schemas/api-keys.d.ts.map +1 -0
- package/dist/schemas/api-keys.js +48 -0
- package/dist/schemas/api-keys.js.map +1 -0
- package/dist/schemas/audit.d.ts +74 -0
- package/dist/schemas/audit.d.ts.map +1 -0
- package/dist/schemas/audit.js +65 -0
- package/dist/schemas/audit.js.map +1 -0
- package/dist/schemas/break-glass.d.ts +42 -0
- package/dist/schemas/break-glass.d.ts.map +1 -0
- package/dist/schemas/break-glass.js +38 -0
- package/dist/schemas/break-glass.js.map +1 -0
- package/dist/schemas/cli-auth.d.ts +59 -0
- package/dist/schemas/cli-auth.d.ts.map +1 -0
- package/dist/schemas/cli-auth.js +68 -0
- package/dist/schemas/cli-auth.js.map +1 -0
- package/dist/schemas/common.d.ts +96 -0
- package/dist/schemas/common.d.ts.map +1 -0
- package/dist/schemas/common.js +205 -0
- package/dist/schemas/common.js.map +1 -0
- package/dist/schemas/demo-passes.d.ts +90 -0
- package/dist/schemas/demo-passes.d.ts.map +1 -0
- package/dist/schemas/demo-passes.js +60 -0
- package/dist/schemas/demo-passes.js.map +1 -0
- package/dist/schemas/files.d.ts +39 -0
- package/dist/schemas/files.d.ts.map +1 -0
- package/dist/schemas/files.js +26 -0
- package/dist/schemas/files.js.map +1 -0
- package/dist/schemas/instance.d.ts +58 -0
- package/dist/schemas/instance.d.ts.map +1 -0
- package/dist/schemas/instance.js +70 -0
- package/dist/schemas/instance.js.map +1 -0
- package/dist/schemas/invitations.d.ts +100 -0
- package/dist/schemas/invitations.d.ts.map +1 -0
- package/dist/schemas/invitations.js +55 -0
- package/dist/schemas/invitations.js.map +1 -0
- package/dist/schemas/me.d.ts +81 -0
- package/dist/schemas/me.d.ts.map +1 -0
- package/dist/schemas/me.js +178 -0
- package/dist/schemas/me.js.map +1 -0
- package/dist/schemas/members.d.ts +60 -0
- package/dist/schemas/members.d.ts.map +1 -0
- package/dist/schemas/members.js +43 -0
- package/dist/schemas/members.js.map +1 -0
- package/dist/schemas/projects.d.ts +232 -0
- package/dist/schemas/projects.d.ts.map +1 -0
- package/dist/schemas/projects.js +140 -0
- package/dist/schemas/projects.js.map +1 -0
- package/dist/schemas/scope.d.ts +14 -0
- package/dist/schemas/scope.d.ts.map +1 -0
- package/dist/schemas/scope.js +5 -0
- package/dist/schemas/scope.js.map +1 -0
- package/dist/schemas/setup.d.ts +28 -0
- package/dist/schemas/setup.d.ts.map +1 -0
- package/dist/schemas/setup.js +28 -0
- package/dist/schemas/setup.js.map +1 -0
- package/dist/schemas/sso-connect.d.ts +30 -0
- package/dist/schemas/sso-connect.d.ts.map +1 -0
- package/dist/schemas/sso-connect.js +38 -0
- package/dist/schemas/sso-connect.js.map +1 -0
- package/dist/schemas/sso-logout.d.ts +17 -0
- package/dist/schemas/sso-logout.d.ts.map +1 -0
- package/dist/schemas/sso-logout.js +16 -0
- package/dist/schemas/sso-logout.js.map +1 -0
- package/dist/schemas/teams.d.ts +100 -0
- package/dist/schemas/teams.d.ts.map +1 -0
- package/dist/schemas/teams.js +84 -0
- package/dist/schemas/teams.js.map +1 -0
- package/dist/schemas/workspaces.d.ts +99 -0
- package/dist/schemas/workspaces.d.ts.map +1 -0
- package/dist/schemas/workspaces.js +96 -0
- package/dist/schemas/workspaces.js.map +1 -0
- package/dist/seams.d.ts +185 -0
- package/dist/seams.d.ts.map +1 -0
- package/dist/seams.js +37 -0
- package/dist/seams.js.map +1 -0
- package/package.json +41 -3
- package/src/define.ts +64 -0
- package/src/entitlements.ts +340 -0
- package/src/identity.ts +49 -0
- package/src/index.ts +37 -0
- package/src/routes/index.ts +1146 -0
- package/src/schemas/api-keys.ts +53 -0
- package/src/schemas/audit.ts +74 -0
- package/src/schemas/break-glass.ts +46 -0
- package/src/schemas/cli-auth.ts +78 -0
- package/src/schemas/common.ts +206 -0
- package/src/schemas/demo-passes.ts +73 -0
- package/src/schemas/files.ts +30 -0
- package/src/schemas/instance.ts +71 -0
- package/src/schemas/invitations.ts +66 -0
- package/src/schemas/me.ts +184 -0
- package/src/schemas/members.ts +53 -0
- package/src/schemas/projects.ts +174 -0
- package/src/schemas/scope.ts +18 -0
- package/src/schemas/setup.ts +31 -0
- package/src/schemas/sso-connect.ts +40 -0
- package/src/schemas/sso-logout.ts +17 -0
- package/src/schemas/teams.ts +101 -0
- package/src/schemas/workspaces.ts +109 -0
- package/src/seams.ts +202 -0
- package/test/define.test.ts +59 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antasphere/chassis-contract",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/antasphere/chassis.git",
|
|
8
|
+
"directory": "packages/chassis-contract"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"src",
|
|
17
|
+
"test",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"default": "./dist/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./routes": {
|
|
26
|
+
"types": "./dist/routes/index.d.ts",
|
|
27
|
+
"default": "./dist/routes/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@antasphere/contract": "^0.16.3",
|
|
32
|
+
"@hono/zod-openapi": "^1.4.0",
|
|
33
|
+
"zod": "^4.1.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"vitest": "^3.2.0"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsc -p tsconfig.json",
|
|
40
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
41
|
+
"lint": "eslint src test",
|
|
42
|
+
"test": "vitest run --passWithNoTests"
|
|
43
|
+
}
|
|
6
44
|
}
|
package/src/define.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
import { defineApiKeySchemas } from './schemas/api-keys.js';
|
|
3
|
+
import { defineCliAuthSchemas } from './schemas/cli-auth.js';
|
|
4
|
+
import { defineMeSchemas } from './schemas/me.js';
|
|
5
|
+
import { defineScopeSchema, type ScopeSchema, type ScopeTuple } from './schemas/scope.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The scope-dependent half of the chassis contract. Everything else in this
|
|
9
|
+
* package is a plain static export; these schemas transitively hold the
|
|
10
|
+
* tool's scope enum, so they are BUILT, once per tool, from the scopes the
|
|
11
|
+
* tool names — no module-level state, the returned object is the contract.
|
|
12
|
+
*
|
|
13
|
+
* const { read, write, dataExport } = IDENTITY.scopes; // the tool's `ToolIdentity` value
|
|
14
|
+
* const contract = defineChassisContract({ scopes: [read, write, dataExport] });
|
|
15
|
+
* export const { scopeSchema, apiKeySchema, meResponseSchema } = contract;
|
|
16
|
+
* export type Scope = ScopeOf<typeof contract>;
|
|
17
|
+
*
|
|
18
|
+
* The scopes are a TUPLE, in the order the enum publishes them: a tool with
|
|
19
|
+
* more scopes than the three parts of its identity appends them here.
|
|
20
|
+
*
|
|
21
|
+
* The route contracts over these schemas come from
|
|
22
|
+
* `defineChassisRoutes(contract, IDENTITY, copy)` on the `./routes` entry (it pulls
|
|
23
|
+
* Hono; this one stays client-safe).
|
|
24
|
+
*/
|
|
25
|
+
export function defineChassisContract<const TScopes extends ScopeTuple>(tool: {
|
|
26
|
+
scopes: TScopes;
|
|
27
|
+
}): ChassisContract<TScopes[number]> {
|
|
28
|
+
return buildChassisContract(defineScopeSchema(tool.scopes));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function buildChassisContract<TScope extends string>(scopeSchema: ScopeSchema<TScope>) {
|
|
32
|
+
const apiKeys = defineApiKeySchemas(scopeSchema);
|
|
33
|
+
return {
|
|
34
|
+
scopeSchema,
|
|
35
|
+
...apiKeys,
|
|
36
|
+
...defineCliAuthSchemas(apiKeys.apiKeySchema),
|
|
37
|
+
...defineMeSchemas(scopeSchema)
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The contract a tool gets for its scopes (`TScope` = the union of the scope literals). */
|
|
42
|
+
export type ChassisContract<TScope extends string> = ReturnType<typeof buildChassisContract<TScope>>;
|
|
43
|
+
|
|
44
|
+
/** The loosest reading of a contract — what the `…Of` type helpers below accept. */
|
|
45
|
+
export type ChassisContractShape = { [K in keyof ChassisContract<string>]: z.ZodType };
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The inferred types of an instantiated contract, under the names their
|
|
49
|
+
* schemas have always had: `type Scope = ScopeOf<typeof contract>`.
|
|
50
|
+
*/
|
|
51
|
+
export type ScopeOf<C extends Pick<ChassisContractShape, 'scopeSchema'>> = z.infer<C['scopeSchema']>;
|
|
52
|
+
export type ApiKeyInfoOf<C extends Pick<ChassisContractShape, 'apiKeySchema'>> = z.infer<C['apiKeySchema']>;
|
|
53
|
+
export type ApiKeyCreateOf<C extends Pick<ChassisContractShape, 'apiKeyCreateSchema'>> = z.infer<
|
|
54
|
+
C['apiKeyCreateSchema']
|
|
55
|
+
>;
|
|
56
|
+
export type ApiKeyCreatedOf<C extends Pick<ChassisContractShape, 'apiKeyCreatedSchema'>> = z.infer<
|
|
57
|
+
C['apiKeyCreatedSchema']
|
|
58
|
+
>;
|
|
59
|
+
export type CliAuthCompletedOf<C extends Pick<ChassisContractShape, 'cliAuthCompletedSchema'>> = z.infer<
|
|
60
|
+
C['cliAuthCompletedSchema']
|
|
61
|
+
>;
|
|
62
|
+
export type MeResponseOf<C extends Pick<ChassisContractShape, 'meResponseSchema'>> = z.infer<
|
|
63
|
+
C['meResponseSchema']
|
|
64
|
+
>;
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { BILLING_PLANS, billingPlanSchema, type BillingPlan } from '@antasphere/contract';
|
|
3
|
+
import type { HeaderReader, Principal } from './seams.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The billing rail's declaration layer (the pay-per-use billing rail spec,
|
|
7
|
+
* §7 and §8b): a route says ONCE, next to its contract, what it meters, which
|
|
8
|
+
* limit it checks and which feature it needs. One chassis middleware reads
|
|
9
|
+
* the declaration and enforces it for every client of `/api/v1` at once (the
|
|
10
|
+
* dashboard, the CLI and the MCP tools are all HTTP clients of it), and a
|
|
11
|
+
* handler never checks or emits by hand.
|
|
12
|
+
*
|
|
13
|
+
* Client-safe on purpose: no Hono here. A route is referenced by its
|
|
14
|
+
* `{ method, path }` as the OpenAPI helper spells it (`{id}` params), so the
|
|
15
|
+
* tool's contract builds the registry from its route objects and the SDK can
|
|
16
|
+
* read the same declarations without pulling the server.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// ── The hub's half: the messages the tool exchanges with the hub ──────────
|
|
20
|
+
//
|
|
21
|
+
// The hub owns these shapes and publishes them as `@antasphere/contract`
|
|
22
|
+
// (the hub's own contract package, on npm at the hub's version); this file
|
|
23
|
+
// re-exports them under the names the chassis has always used, so a tool
|
|
24
|
+
// reads exactly what the hub wrote and a change at the hub reaches every
|
|
25
|
+
// tool through one dependency bump (PRDCT-3325; before it, a hand copy checked
|
|
26
|
+
// against the hub's wire snapshot). The chassis's OWN half — the
|
|
27
|
+
// declaration layer below — stays here.
|
|
28
|
+
export {
|
|
29
|
+
BILLING_PLANS,
|
|
30
|
+
USAGE_CHECK_REASONS,
|
|
31
|
+
USAGE_EVENT_MAX_FUTURE_MS,
|
|
32
|
+
USAGE_EVENT_MAX_PAST_MS,
|
|
33
|
+
USAGE_EVENTS_BATCH_MAX,
|
|
34
|
+
USAGE_REJECT_REASONS,
|
|
35
|
+
USAGE_ROUTE_ERRORS,
|
|
36
|
+
USAGE_WRITE_SCOPE,
|
|
37
|
+
usageCheckRequestSchema,
|
|
38
|
+
usageCheckSchema,
|
|
39
|
+
usageEventOccurrenceIssue,
|
|
40
|
+
usageEventResultSchema,
|
|
41
|
+
usageEventSchema,
|
|
42
|
+
usageEventsBatchSchema,
|
|
43
|
+
usageIngestResultSchema,
|
|
44
|
+
usageRejectReasonSchema
|
|
45
|
+
} from '@antasphere/contract';
|
|
46
|
+
export type {
|
|
47
|
+
UsageCheck,
|
|
48
|
+
UsageCheckReason,
|
|
49
|
+
UsageCheckRequest,
|
|
50
|
+
UsageEventResult,
|
|
51
|
+
UsageEventsBatch,
|
|
52
|
+
UsageIngestResult,
|
|
53
|
+
UsageRejectReason,
|
|
54
|
+
UsageRouteError
|
|
55
|
+
} from '@antasphere/contract';
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The plan tiers, a CLOSED enum the hub owns (`BILLING_PLANS` in
|
|
59
|
+
* `@antasphere/contract`), under the chassis's own names (§8b): `free` is the
|
|
60
|
+
* default with no subscription; `pro` is the paid plan. "Enterprise" is not
|
|
61
|
+
* a tier but a per-account override the hub serves.
|
|
62
|
+
*/
|
|
63
|
+
export const ENTITLEMENT_TIERS = BILLING_PLANS;
|
|
64
|
+
export type EntitlementTier = BillingPlan;
|
|
65
|
+
export const entitlementTierSchema = billingPlanSchema;
|
|
66
|
+
|
|
67
|
+
/** The wire code of a credit refusal (§7 step 4), and its `details`. */
|
|
68
|
+
export const ENTITLEMENT_DENIED = 'entitlement_denied';
|
|
69
|
+
export interface EntitlementDeniedDetails {
|
|
70
|
+
/** The price of this action, in credits. */
|
|
71
|
+
credits: number;
|
|
72
|
+
/** The organization's balance, in credits. */
|
|
73
|
+
balance: number;
|
|
74
|
+
/** The hub's billing page for the organization: where a human adds credits. */
|
|
75
|
+
topUpUrl: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* What a declaration's functions see: the request as the chassis reads it.
|
|
80
|
+
* `audit` is present only AFTER the handler ran (the emit), carrying what the
|
|
81
|
+
* handler recorded for its audit row — the place a stored size or a created
|
|
82
|
+
* resource id lives.
|
|
83
|
+
*/
|
|
84
|
+
export interface EntitlementRequest {
|
|
85
|
+
method: string;
|
|
86
|
+
path: string;
|
|
87
|
+
headers: HeaderReader;
|
|
88
|
+
/** Route params by name (`{id}` → its value). */
|
|
89
|
+
params: Record<string, string>;
|
|
90
|
+
principal: Principal | null;
|
|
91
|
+
/**
|
|
92
|
+
* The parsed JSON body of the request, read once and shared with the
|
|
93
|
+
* handler's own validation (the server caches the parse); `undefined`
|
|
94
|
+
* when the request carries none or it does not parse (the route's
|
|
95
|
+
* validator then answers 400 itself). A count limit or a conditional
|
|
96
|
+
* feature reads what the request asks for here (PRDCT-2702): the mint
|
|
97
|
+
* of a share link with a password, the address an invitation names.
|
|
98
|
+
*/
|
|
99
|
+
body: () => Promise<unknown>;
|
|
100
|
+
audit?:
|
|
101
|
+
| { action: string; resourceType: string; resourceId?: string; metadata?: Record<string, unknown> }
|
|
102
|
+
| undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Who pays and who is reported (§8): the organization's account pays, the
|
|
107
|
+
* user is reported. The default actor is the principal; a route on an
|
|
108
|
+
* anonymous surface resolves its resource's owner here instead (share token
|
|
109
|
+
* → deck → owner → the owner's workspace and account) — the viewer is never
|
|
110
|
+
* identified.
|
|
111
|
+
*/
|
|
112
|
+
export interface ActorRef {
|
|
113
|
+
/** The reported user, or null when the actor is a resource owner the tool could not name. */
|
|
114
|
+
userId: string | null;
|
|
115
|
+
workspaceId: string;
|
|
116
|
+
/** The paying account (the workspace's central account id); absent = unmetered (oss, or a cloud-local workspace). */
|
|
117
|
+
accountRef?: string | undefined;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The actor of a request when it is not the principal: the owner of a
|
|
122
|
+
* resource reached anonymously (a form response through a share link), or
|
|
123
|
+
* the workspace a public door opens (an invitation accepted, a collaborator
|
|
124
|
+
* grant claimed). Resolves null to leave the route to its own handling.
|
|
125
|
+
*/
|
|
126
|
+
export type ActorHook = (ctx: EntitlementRequest) => Promise<ActorRef | null> | ActorRef | null;
|
|
127
|
+
|
|
128
|
+
export interface MeterDeclaration {
|
|
129
|
+
/** The action key, namespaced by the tool's price book (`things.publish`). */
|
|
130
|
+
key: string;
|
|
131
|
+
/** The unit the price is per (`call`, `bytes`, `items`). */
|
|
132
|
+
unit: string;
|
|
133
|
+
/** The quantity of this request; default 1. Called before the handler (the check) and after it (the emit, with `audit` set). */
|
|
134
|
+
quantity?: (ctx: EntitlementRequest) => number;
|
|
135
|
+
/** The actor when it is not the principal (the owner of a resource reached anonymously). */
|
|
136
|
+
actor?: ActorHook;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface LimitDeclaration {
|
|
140
|
+
/** The limit key (`things.maxBytes`), declared with its per-tier values in the tool's `entitlements` slot. */
|
|
141
|
+
key: string;
|
|
142
|
+
/**
|
|
143
|
+
* The value this request observes, compared against the tier's value; over
|
|
144
|
+
* it is a plan refusal. A SIZE limit reads the declared length
|
|
145
|
+
* (`declaredContentLength`, synchronous: the gate then also demands a
|
|
146
|
+
* Content-Length on a metered account). A COUNT limit (PRDCT-2702) may be
|
|
147
|
+
* asynchronous and read the body and the tool's own tables: it returns the
|
|
148
|
+
* count AFTER this action (the resource's things plus this one), so
|
|
149
|
+
* `observed > max` is the refusal, and `null` when there is nothing to
|
|
150
|
+
* judge (the resource does not resolve, the caller may not see it): the
|
|
151
|
+
* gate then lets the route answer on its own.
|
|
152
|
+
*/
|
|
153
|
+
value: (ctx: EntitlementRequest) => number | null | Promise<number | null>;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* A feature a route needs, with an optional condition on the request
|
|
158
|
+
* (PRDCT-2702): a route that creates a thing needs `things.premium` only
|
|
159
|
+
* when the body asks for the premium option, so a declaration on the route
|
|
160
|
+
* refuses the act and never the route. Absent `when`, the whole route needs
|
|
161
|
+
* the feature.
|
|
162
|
+
*/
|
|
163
|
+
export interface FeatureDeclaration {
|
|
164
|
+
key: string;
|
|
165
|
+
when?: (ctx: EntitlementRequest) => boolean | Promise<boolean>;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export interface RouteEntitlement {
|
|
169
|
+
meter?: MeterDeclaration | undefined;
|
|
170
|
+
limit?: LimitDeclaration | undefined;
|
|
171
|
+
/** A feature key (`things.premium`), declared with its per-tier switch in the tool's `entitlements` slot, or the key with a condition. */
|
|
172
|
+
feature?: string | FeatureDeclaration | undefined;
|
|
173
|
+
/**
|
|
174
|
+
* The actor of a route that meters nothing but checks a limit or a feature
|
|
175
|
+
* on a PUBLIC door (no principal): the workspace the door opens pays and is
|
|
176
|
+
* judged. A meter's own `actor` serves the same purpose on a metered route;
|
|
177
|
+
* this one covers the limit-only shape. Either makes the route a viewer's
|
|
178
|
+
* surface: every refusal to it is one neutral sentence.
|
|
179
|
+
*/
|
|
180
|
+
actor?: ActorHook | undefined;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** The key of a route's feature declaration, whichever shape it uses; null when none. */
|
|
184
|
+
export function featureKeyOf(entry: RouteEntitlement): string | null {
|
|
185
|
+
if (!entry.feature) return null;
|
|
186
|
+
return typeof entry.feature === 'string' ? entry.feature : entry.feature.key;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** The actor hook of a route, the entry's own or its meter's; null when the principal is the actor. */
|
|
190
|
+
export function actorHookOf(entry: RouteEntitlement): ActorHook | null {
|
|
191
|
+
return entry.actor ?? entry.meter?.actor ?? null;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** A route as the contract spells it: the OpenAPI method and the `{param}` path. */
|
|
195
|
+
export interface RouteRef {
|
|
196
|
+
method: string;
|
|
197
|
+
path: string;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export interface RouteEntitlementEntry extends RouteEntitlement {
|
|
201
|
+
route: RouteRef;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** `METHOD /path` — the registry key of a route. */
|
|
205
|
+
export function routeEntitlementKey(route: RouteRef): string {
|
|
206
|
+
return `${route.method.toUpperCase()} ${route.path}`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export type RouteEntitlementDeclarations = ReadonlyMap<string, RouteEntitlementEntry>;
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Build the registry from the tool's route objects. One entry per route: a
|
|
213
|
+
* second declaration of the same route throws at module load, so a typo or a
|
|
214
|
+
* copy cannot silently double or replace a price.
|
|
215
|
+
*/
|
|
216
|
+
export function declareRouteEntitlements(
|
|
217
|
+
entries: ReadonlyArray<RouteEntitlementEntry>
|
|
218
|
+
): RouteEntitlementDeclarations {
|
|
219
|
+
const map = new Map<string, RouteEntitlementEntry>();
|
|
220
|
+
for (const entry of entries) {
|
|
221
|
+
const key = routeEntitlementKey(entry.route);
|
|
222
|
+
if (map.has(key)) throw new Error(`route entitlement declared twice: ${key}`);
|
|
223
|
+
if (!entry.meter && !entry.limit && !entry.feature) {
|
|
224
|
+
throw new Error(`route entitlement declares nothing: ${key}`);
|
|
225
|
+
}
|
|
226
|
+
map.set(key, entry);
|
|
227
|
+
}
|
|
228
|
+
return map;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** The declared body size of a request (its `Content-Length`), 0 when absent or malformed. */
|
|
232
|
+
export function declaredContentLength(ctx: EntitlementRequest): number {
|
|
233
|
+
const n = Number(ctx.headers.get('content-length') ?? '0');
|
|
234
|
+
return Number.isFinite(n) && n > 0 ? n : 0;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* The stored size a handler recorded in its audit metadata (`sizeBytes`),
|
|
239
|
+
* falling back to the declared body size before the handler ran. The check
|
|
240
|
+
* sees the declared size, the emit sees the bytes that were actually kept.
|
|
241
|
+
*/
|
|
242
|
+
export function auditedSizeBytes(ctx: EntitlementRequest): number {
|
|
243
|
+
const stored = ctx.audit?.metadata?.sizeBytes;
|
|
244
|
+
if (typeof stored === 'number' && Number.isFinite(stored) && stored >= 0) return stored;
|
|
245
|
+
return declaredContentLength(ctx);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// ── The tool's declared defaults, as `GET /instance` shows them ────────────
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* A priced action: the default the hub seeds its price book from (§7); the
|
|
252
|
+
* hub's rows win afterwards. `creditsPerUnit` credits buy `per` units of
|
|
253
|
+
* `unit` (`per` defaults to 1): an action metered in bytes and priced per
|
|
254
|
+
* mebibyte declares `{ creditsPerUnit: 5, unit: 'bytes', per: 1048576 }`,
|
|
255
|
+
* so the meter stays exact, the price reads as the seed table says it, and
|
|
256
|
+
* the two agree by construction (PRDCT-2627: the boot refuses a route whose
|
|
257
|
+
* meter unit differs from its action's). The hub prices an event as
|
|
258
|
+
* `creditsPerUnit × quantity / per`; rounding a fraction of a credit is the
|
|
259
|
+
* hub's rule, not the tool's.
|
|
260
|
+
*/
|
|
261
|
+
export const entitlementActionSchema = z.object({
|
|
262
|
+
key: z.string().min(1),
|
|
263
|
+
creditsPerUnit: z.number().int().min(0),
|
|
264
|
+
unit: z.string().min(1),
|
|
265
|
+
/** How many units one `creditsPerUnit` buys; 1 when absent. */
|
|
266
|
+
per: z.number().int().min(1).optional(),
|
|
267
|
+
label: z.string().min(1)
|
|
268
|
+
});
|
|
269
|
+
export type EntitlementAction = z.infer<typeof entitlementActionSchema>;
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* The credits an action declares for a quantity: `creditsPerUnit × quantity
|
|
273
|
+
* / per`, exact (a fraction is the hub's to round). A test pins the seed
|
|
274
|
+
* values with it, so a unit slip (5 per byte where 5 per MB was meant)
|
|
275
|
+
* fails before the price book is seeded from discovery.
|
|
276
|
+
*/
|
|
277
|
+
export function declaredCredits(
|
|
278
|
+
action: Pick<EntitlementAction, 'creditsPerUnit' | 'per'>,
|
|
279
|
+
quantity: number
|
|
280
|
+
): number {
|
|
281
|
+
return (action.creditsPerUnit * quantity) / (action.per ?? 1);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** A limit's value per tier plus the oss value (the operator's own knob); null = unlimited. */
|
|
285
|
+
export const tierLimitSchema = z.object({
|
|
286
|
+
oss: z.number().nullable(),
|
|
287
|
+
free: z.number().nullable(),
|
|
288
|
+
pro: z.number().nullable()
|
|
289
|
+
});
|
|
290
|
+
export type TierLimit = z.infer<typeof tierLimitSchema>;
|
|
291
|
+
|
|
292
|
+
/** A feature's switch per tier (oss has every feature: a self-hosted instance knows no plan). */
|
|
293
|
+
export const tierFeatureSchema = z.object({
|
|
294
|
+
free: z.boolean(),
|
|
295
|
+
pro: z.boolean()
|
|
296
|
+
});
|
|
297
|
+
export type TierFeature = z.infer<typeof tierFeatureSchema>;
|
|
298
|
+
|
|
299
|
+
export const toolEntitlementsSchema = z.object({
|
|
300
|
+
actions: z.array(entitlementActionSchema),
|
|
301
|
+
limits: z.record(z.string(), tierLimitSchema),
|
|
302
|
+
features: z.record(z.string(), tierFeatureSchema)
|
|
303
|
+
});
|
|
304
|
+
export type ToolEntitlements = z.infer<typeof toolEntitlementsSchema>;
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* The hub's answer to `GET /usage/entitlements?accountRef=` (§5): the hub's
|
|
308
|
+
* `usageEntitlementsSchema` (PRDCT-2636), the hub's own shape in
|
|
309
|
+
* snapshot (PRDCT-2677). The account's
|
|
310
|
+
* plan, resolved from the tier's defaults and the account's overrides; in
|
|
311
|
+
* phase 1 `limits` and `features` are empty and the tool's own declared
|
|
312
|
+
* values fill the numbers. A limit's value is a number or a boolean: the
|
|
313
|
+
* hub may switch a numeric limit off or on per account (`true` = unlimited,
|
|
314
|
+
* `false` = nothing allowed; `profiles.ts` resolves it so). Since phase 2
|
|
315
|
+
* (PRDCT-2663) the hub serves its real rows — every limit key it holds for
|
|
316
|
+
* the calling tool and the features on for the account — and `upgradeUrl`,
|
|
317
|
+
* its upgrade page for the organization carrying `org`, `tool` and `plan`;
|
|
318
|
+
* the chassis appends `&key=<key>&requiredPlan=<tier>` at refusal time.
|
|
319
|
+
* Required: every hub since 0.11.0 sends it.
|
|
320
|
+
*/
|
|
321
|
+
export const entitlementProfileSchema = z.object({
|
|
322
|
+
accountRef: z.uuid(),
|
|
323
|
+
plan: entitlementTierSchema,
|
|
324
|
+
/** Until when the plan is paid for (ISO 8601); null on `free` and on a live subscription. */
|
|
325
|
+
planUntil: z.string().nullable(),
|
|
326
|
+
limits: z.record(z.string(), z.union([z.number(), z.boolean()])),
|
|
327
|
+
features: z.array(z.string()),
|
|
328
|
+
upgradeUrl: z.string()
|
|
329
|
+
});
|
|
330
|
+
export type EntitlementProfile = z.infer<typeof entitlementProfileSchema>;
|
|
331
|
+
|
|
332
|
+
/** The wire code of a plan refusal, and its `details` (§7 step 2). */
|
|
333
|
+
export const PLAN_REQUIRED = 'plan_required';
|
|
334
|
+
export interface PlanRequiredDetails {
|
|
335
|
+
key: string;
|
|
336
|
+
plan: EntitlementTier;
|
|
337
|
+
/** The first tier that allows the action, or null when none does. */
|
|
338
|
+
requiredPlan: EntitlementTier | null;
|
|
339
|
+
upgradeUrl: string;
|
|
340
|
+
}
|
package/src/identity.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A tool's IDENTITY: every value that names the tool where a person, an agent
|
|
3
|
+
* or an operator can see it. The chassis packages spell none of them; a tool
|
|
4
|
+
* writes ONE value of this type (in its own contract package, the one package
|
|
5
|
+
* its server, SDK, CLI and dashboard all depend on) and hands it to the three
|
|
6
|
+
* chassis entry points: `defineChassisContract` + `defineChassisRoutes` here,
|
|
7
|
+
* the server's `ToolDefinition.identity`, and the CLI's `defineCli`.
|
|
8
|
+
*
|
|
9
|
+
* Pure data, client-safe. The tool's domain WORDING (a refusal that names its
|
|
10
|
+
* resource) is not identity: it goes to the server's `copy` slot.
|
|
11
|
+
*/
|
|
12
|
+
export interface ToolIdentity<TScope extends string = string> {
|
|
13
|
+
/** Machine name, lower case (`acme`): the CLI's config namespace and its hub connect name. */
|
|
14
|
+
slug: string;
|
|
15
|
+
/** Human name (`Acme`): mails, the no-dashboard page, CLI messages, the name of an instance not set up yet. */
|
|
16
|
+
displayName: string;
|
|
17
|
+
/** API key prefix, without the underscore (`acm` mints `acm_<keyId>_<secret>`). Never change it once keys exist. */
|
|
18
|
+
apiKeyPrefix: string;
|
|
19
|
+
/** The scope vocabulary, by the part each scope plays. `dataExport` is the opt-in full-workspace export. */
|
|
20
|
+
scopes: { read: TScope; write: TScope; dataExport: TScope };
|
|
21
|
+
/** How the OpenAPI document names the CLI key's grant (`things:read+write`). A literal: a contraction, not a join. */
|
|
22
|
+
cliKeyScopesLabel: string;
|
|
23
|
+
/**
|
|
24
|
+
* The tool's cloud home (PRDCT-2947): the instance Antasphere operates
|
|
25
|
+
* (`https://acme.antasphere.com`). The CLI resolves its URL there last
|
|
26
|
+
* (flag → env → profile → this), so `antasphere login` then `acme list`
|
|
27
|
+
* works on a clean machine; the implicit `cloud` profile lives at it. A
|
|
28
|
+
* self-hosted instance is named once, as a profile named for its host.
|
|
29
|
+
*/
|
|
30
|
+
cloud: { url: string };
|
|
31
|
+
cli: {
|
|
32
|
+
/** The binary's name (`acme`). Must equal the `bin` key of the CLI's package.json. */
|
|
33
|
+
bin: string;
|
|
34
|
+
/** Prefix of the CLI's environment variables (`ACME` reads `ACME_URL`, `ACME_API_KEY`, `ACME_WORKSPACE`). */
|
|
35
|
+
envPrefix: string;
|
|
36
|
+
/** A pre-hub config directory still read once for migration (`~/.config/<dir>/config.json`). */
|
|
37
|
+
legacyConfigDir: string;
|
|
38
|
+
};
|
|
39
|
+
mcp: {
|
|
40
|
+
/** `serverInfo.name` of the bundled MCP server. */
|
|
41
|
+
serverName: string;
|
|
42
|
+
/** Prefix of the tool's MCP tool names, underscore included (`acme_`). The tool registers `<prefix>whoami`. */
|
|
43
|
+
toolPrefix: string;
|
|
44
|
+
};
|
|
45
|
+
/** OpenTelemetry `service.name`. */
|
|
46
|
+
otelServiceName: string;
|
|
47
|
+
/** The published container image. Read by nothing at run time: recorded so an instantiate script has one source. */
|
|
48
|
+
imageName: string;
|
|
49
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Client-safe entry: pure zod schemas + seam types. No Hono anywhere on this
|
|
2
|
+
// path — the dashboard and SDK import from here and must stay server-free.
|
|
3
|
+
//
|
|
4
|
+
// The generic half of the platform contract. Everything is a static export
|
|
5
|
+
// EXCEPT what carries the tool's scope vocabulary: that comes out of
|
|
6
|
+
// `defineChassisContract({ scopes })`, instantiated once by the tool from its
|
|
7
|
+
// identity (`ToolIdentity`, ./identity.ts: the type only, the value is the tool's).
|
|
8
|
+
export * from './schemas/common.js';
|
|
9
|
+
export * from './schemas/scope.js';
|
|
10
|
+
export * from './schemas/instance.js';
|
|
11
|
+
export * from './schemas/setup.js';
|
|
12
|
+
export { onboardingDismissedSchema, type OnboardingDismissed } from './schemas/me.js';
|
|
13
|
+
export * from './schemas/workspaces.js';
|
|
14
|
+
export * from './schemas/members.js';
|
|
15
|
+
export {
|
|
16
|
+
cliAuthRequestSchema,
|
|
17
|
+
cliAuthRequestedSchema,
|
|
18
|
+
cliAuthCompleteSchema,
|
|
19
|
+
cliAuthRevokedSchema,
|
|
20
|
+
type CliAuthRequest,
|
|
21
|
+
type CliAuthRequested,
|
|
22
|
+
type CliAuthComplete,
|
|
23
|
+
type CliAuthRevoked
|
|
24
|
+
} from './schemas/cli-auth.js';
|
|
25
|
+
export * from './schemas/sso-connect.js';
|
|
26
|
+
export * from './schemas/sso-logout.js';
|
|
27
|
+
export * from './schemas/invitations.js';
|
|
28
|
+
export * from './schemas/audit.js';
|
|
29
|
+
export * from './schemas/break-glass.js';
|
|
30
|
+
export * from './schemas/files.js';
|
|
31
|
+
export * from './schemas/projects.js';
|
|
32
|
+
export * from './schemas/teams.js';
|
|
33
|
+
export * from './schemas/demo-passes.js';
|
|
34
|
+
export * from './seams.js';
|
|
35
|
+
export * from './entitlements.js';
|
|
36
|
+
export * from './define.js';
|
|
37
|
+
export type { ToolIdentity } from './identity.js';
|