entangled-worlds-orbiters-shared 2.9.1 → 2.11.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/README.md +26 -0
- package/dist/capabilities/errors.d.ts +79 -0
- package/dist/capabilities/errors.js +87 -0
- package/dist/capabilities/index.d.ts +9 -0
- package/dist/capabilities/index.js +9 -0
- package/dist/capabilities/registry.d.ts +106 -0
- package/dist/capabilities/registry.js +215 -0
- package/dist/capabilities/schema.d.ts +52 -0
- package/dist/capabilities/schema.js +139 -0
- package/dist/star/blackbody.d.ts +12 -0
- package/dist/star/blackbody.js +38 -0
- package/dist/star/bounds.d.ts +39 -0
- package/dist/star/bounds.js +47 -0
- package/dist/star/framing.d.ts +31 -0
- package/dist/star/framing.js +68 -0
- package/dist/star/index.d.ts +21 -0
- package/dist/star/index.js +15 -0
- package/dist/star/layout.d.ts +121 -0
- package/dist/star/layout.js +307 -0
- package/dist/star/lighting.d.ts +65 -0
- package/dist/star/lighting.js +115 -0
- package/dist/star/params.d.ts +79 -0
- package/dist/star/params.js +122 -0
- package/dist/star/quality.d.ts +16 -0
- package/dist/star/quality.js +16 -0
- package/dist/star/shaders.d.ts +20 -0
- package/dist/star/shaders.js +186 -0
- package/dist/star/starBody.d.ts +58 -0
- package/dist/star/starBody.js +308 -0
- package/dist/star/starManager.d.ts +73 -0
- package/dist/star/starManager.js +190 -0
- package/dist/star/stellarData.d.ts +82 -0
- package/dist/star/stellarData.js +347 -0
- package/dist/studio/autosaveSession.d.ts +56 -0
- package/dist/studio/autosaveSession.js +138 -0
- package/dist/studio/headerBroadcast.d.ts +9 -0
- package/dist/studio/headerBroadcast.js +37 -0
- package/dist/studio/history.d.ts +37 -0
- package/dist/studio/history.js +50 -0
- package/dist/studio/hostTransport.d.ts +20 -0
- package/dist/studio/hostTransport.js +61 -0
- package/dist/studio/index.d.ts +6 -0
- package/dist/studio/index.js +9 -0
- package/dist/studio/patchStore.d.ts +7 -0
- package/dist/studio/patchStore.js +30 -0
- package/dist/studio/statusChannel.d.ts +12 -0
- package/dist/studio/statusChannel.js +14 -0
- package/dist/uploads/index.d.ts +34 -0
- package/dist/uploads/index.js +49 -0
- package/package.json +30 -6
- package/src/capabilities/errors.ts +115 -0
- package/src/capabilities/index.ts +22 -0
- package/src/capabilities/registry.ts +307 -0
- package/src/capabilities/schema.ts +164 -0
- package/src/core/index.js +1 -1
- package/src/core/lights.js +17 -1
- package/src/loaders/glbLoader.js +5 -2
- package/src/moons/index.js +11 -1
- package/src/moons/moonsGeneration.js +24 -8
- package/src/moons/moonsManager.js +18 -0
- package/src/star/blackbody.ts +46 -0
- package/src/star/bounds.ts +73 -0
- package/src/star/framing.ts +96 -0
- package/src/star/index.ts +59 -0
- package/src/star/layout.ts +400 -0
- package/src/star/lighting.ts +142 -0
- package/src/star/params.ts +161 -0
- package/src/star/quality.ts +33 -0
- package/src/star/shaders.ts +195 -0
- package/src/star/starBody.ts +385 -0
- package/src/star/starManager.ts +224 -0
- package/src/star/stellarData.ts +387 -0
- package/src/studio/autosaveSession.ts +186 -0
- package/src/studio/headerBroadcast.ts +52 -0
- package/src/studio/history.ts +91 -0
- package/src/studio/hostTransport.ts +70 -0
- package/src/studio/index.ts +30 -0
- package/src/studio/patchStore.ts +38 -0
- package/src/studio/statusChannel.ts +29 -0
- package/src/uploads/index.ts +93 -0
- package/src/world/cloudShell.js +2 -3
- package/src/world/worldGLB.js +3 -2
- package/src/world/worldManager.js +1 -0
- package/src/world/worldTexture.js +4 -3
package/README.md
CHANGED
|
@@ -71,6 +71,32 @@ import * as EntangledWorlds from 'entangled-worlds-orbiters-shared';
|
|
|
71
71
|
### Configuration
|
|
72
72
|
- `textureConfig` - Texture configuration system
|
|
73
73
|
|
|
74
|
+
### Star (`entangled-worlds-orbiters-shared/star`)
|
|
75
|
+
- `createStar`, `StarManager` - The host star of a world: single, binary or trinary, emissive, shader-only glow (two draw calls for any multiplicity; lights nothing)
|
|
76
|
+
- `DEFAULT_STAR_PARAMS`, `normalizeStarParams` - Author params (temperature, radius, luminosity, activity, multiplicity, separation, seed, placement) + validator for stored params
|
|
77
|
+
- `starParamsFromStellarData` - Catalogue host-star data (Teff, radius, mass, luminosity, spectral type, star count or a "(AB)b" name suffix) → params
|
|
78
|
+
- `blackbodyColor`, `spectralClass`, `starLayout`, `starPositions` - Pure colour / layout / orbit helpers
|
|
79
|
+
- `getFocus` (on `createStar` / `StarManager`), `framingDistance`, `defaultStarPlacement` - Camera focus on the system barycentre and host-camera-aware placement
|
|
80
|
+
- The star lights the scene: `StarManager` key + camera-side fill lights, `setLighting`, `lightingFromLegacy`, `DEFAULT_AMBIENT` (pair with `setHemisphereEnabled(scene, false)` from `./core`; moons `emissiveLift`, GLB `emissiveIntensity` for night sides)
|
|
81
|
+
- Real binaries (close pair from component masses, a distant companion for wide pairs) and an optional debris disc; `mainSequenceTemperature` / `Radius` / `Luminosity` helpers
|
|
82
|
+
- Perf + look harness: `npm run bench:star` (see `bench/star/results/REPORT.md`)
|
|
83
|
+
|
|
84
|
+
### Capabilities (`entangled-worlds-orbiters-shared/capabilities`)
|
|
85
|
+
- `createCapabilityRegistry` - Validated, named registry of studio actions (query / mutation / generative)
|
|
86
|
+
- `validate`, `assertValid` - JSON-Schema-subset validator used for capability inputs
|
|
87
|
+
- Error taxonomy - `ValidationError`, `UnavailableError`, `PrecheckError`, …
|
|
88
|
+
|
|
89
|
+
### Studio session (`entangled-worlds-orbiters-shared/studio`)
|
|
90
|
+
- `createHistory` - Snapshot undo/redo with injected snapshot/restore
|
|
91
|
+
- `createAutosaveSession` - Debounced autosave state machine (dirty / in flight / queued / host ack)
|
|
92
|
+
- `createPatchStore`, `makeHeaderBroadcast` - Header mirror for a UI that mounts after the controller
|
|
93
|
+
- `createStatusChannel` - Status-line seam ("Saving…", "Saved")
|
|
94
|
+
- `isTrustedHostOrigin`, `isEmbedded`, `postToHost` - Host-frame transport
|
|
95
|
+
|
|
96
|
+
### Uploads (`entangled-worlds-orbiters-shared/uploads`)
|
|
97
|
+
- `uploadViaSignedUrl` - The API's signed-URL upload: `/spaces/upload-init` → PUT → `/spaces/upload-complete`, over the app's own authenticated JSON caller
|
|
98
|
+
- `putToSignedUrl` - The storage PUT alone, with progress and abort
|
|
99
|
+
|
|
74
100
|
## Runtime base configuration (env)
|
|
75
101
|
|
|
76
102
|
This package is built statically. At runtime, it can use a public base URL to turn relative asset paths into absolute URLs. If you don’t provide a base, it will use same-origin root-relative paths (e.g., `/default/...`).
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capability layer error taxonomy (shared machinery).
|
|
3
|
+
*
|
|
4
|
+
* This is the ONE generic capability machinery, app-agnostic and layer-neutral
|
|
5
|
+
* (`src/capabilities/` imports nothing app-level — see `.agents/decisions/`). Both the studio
|
|
6
|
+
* registry (`src/studio/capabilities/`, the credit-metered superset caller) and the edit-mode
|
|
7
|
+
* registry (`src/core/edit/capabilities/`, the lean preview caller) build on it. It supersedes
|
|
8
|
+
* the previously duplicated `studio/capabilities/errors.js` + `core/edit/capabilities/errors.ts`.
|
|
9
|
+
*
|
|
10
|
+
* One base class so callers — today the UI, tomorrow the agent loop — branch on `err.code`
|
|
11
|
+
* instead of string-matching messages. Every error carries the offending capability name when
|
|
12
|
+
* known, so failures are traceable without a stack dive.
|
|
13
|
+
*/
|
|
14
|
+
export type CapabilityErrorCode = 'CAPABILITY_ERROR' | 'VALIDATION' | 'UNAVAILABLE' | 'PRECHECK_FAILED' | 'NOT_IMPLEMENTED' | 'INSUFFICIENT_CREDITS' | 'UNKNOWN_CAPABILITY';
|
|
15
|
+
export interface SchemaIssue {
|
|
16
|
+
path: string;
|
|
17
|
+
message: string;
|
|
18
|
+
}
|
|
19
|
+
export declare class CapabilityError extends Error {
|
|
20
|
+
code: CapabilityErrorCode;
|
|
21
|
+
capability: string | null;
|
|
22
|
+
cause?: unknown;
|
|
23
|
+
constructor(message: string, { code, capability, cause }?: {
|
|
24
|
+
code?: CapabilityErrorCode;
|
|
25
|
+
capability?: string | null;
|
|
26
|
+
cause?: unknown;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/** Input args failed the capability's inputSchema. Thrown before any write. */
|
|
30
|
+
export declare class ValidationError extends CapabilityError {
|
|
31
|
+
issues: SchemaIssue[];
|
|
32
|
+
constructor(message: string, { capability, issues }?: {
|
|
33
|
+
capability?: string | null;
|
|
34
|
+
issues?: SchemaIssue[];
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/** Capability is declared but not invokable yet (status: 'planned' / available()===false). */
|
|
38
|
+
export declare class UnavailableError extends CapabilityError {
|
|
39
|
+
reason: string | null;
|
|
40
|
+
constructor(message: string, { capability, reason }?: {
|
|
41
|
+
capability?: string | null;
|
|
42
|
+
reason?: string | null;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Capability's precheck rejected the args against live state (no-op target, missing element,
|
|
47
|
+
* guard condition). Thrown before pushHistory / any write.
|
|
48
|
+
*/
|
|
49
|
+
export declare class PrecheckError extends CapabilityError {
|
|
50
|
+
reason: string | null;
|
|
51
|
+
constructor(message: string, { capability, reason }?: {
|
|
52
|
+
capability?: string | null;
|
|
53
|
+
reason?: string | null;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Wired capability whose execute() is not implemented (generative stubs). Distinct from
|
|
58
|
+
* UnavailableError: "active but unfinished", reachable only if status was flipped to 'active'
|
|
59
|
+
* before the implementation landed.
|
|
60
|
+
*/
|
|
61
|
+
export declare class NotImplementedError extends CapabilityError {
|
|
62
|
+
constructor(capability: string, message?: string | null);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Reserved for the future generative path: the backend credit check failed (402-style).
|
|
66
|
+
* Declared now so the agent loop can catch it without a follow-up refactor.
|
|
67
|
+
*/
|
|
68
|
+
export declare class InsufficientCreditsError extends CapabilityError {
|
|
69
|
+
required: number | null;
|
|
70
|
+
available: number | null;
|
|
71
|
+
constructor(message: string, { capability, required, available }?: {
|
|
72
|
+
capability?: string | null;
|
|
73
|
+
required?: number | null;
|
|
74
|
+
available?: number | null;
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
export declare class UnknownCapabilityError extends CapabilityError {
|
|
78
|
+
constructor(name: string);
|
|
79
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capability layer error taxonomy (shared machinery).
|
|
3
|
+
*
|
|
4
|
+
* This is the ONE generic capability machinery, app-agnostic and layer-neutral
|
|
5
|
+
* (`src/capabilities/` imports nothing app-level — see `.agents/decisions/`). Both the studio
|
|
6
|
+
* registry (`src/studio/capabilities/`, the credit-metered superset caller) and the edit-mode
|
|
7
|
+
* registry (`src/core/edit/capabilities/`, the lean preview caller) build on it. It supersedes
|
|
8
|
+
* the previously duplicated `studio/capabilities/errors.js` + `core/edit/capabilities/errors.ts`.
|
|
9
|
+
*
|
|
10
|
+
* One base class so callers — today the UI, tomorrow the agent loop — branch on `err.code`
|
|
11
|
+
* instead of string-matching messages. Every error carries the offending capability name when
|
|
12
|
+
* known, so failures are traceable without a stack dive.
|
|
13
|
+
*/
|
|
14
|
+
export class CapabilityError extends Error {
|
|
15
|
+
code;
|
|
16
|
+
capability;
|
|
17
|
+
cause;
|
|
18
|
+
constructor(message, { code = 'CAPABILITY_ERROR', capability = null, cause = null } = {}) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = 'CapabilityError';
|
|
21
|
+
this.code = code;
|
|
22
|
+
this.capability = capability;
|
|
23
|
+
if (cause)
|
|
24
|
+
this.cause = cause;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** Input args failed the capability's inputSchema. Thrown before any write. */
|
|
28
|
+
export class ValidationError extends CapabilityError {
|
|
29
|
+
issues;
|
|
30
|
+
constructor(message, { capability = null, issues = [] } = {}) {
|
|
31
|
+
super(message, { code: 'VALIDATION', capability });
|
|
32
|
+
this.name = 'ValidationError';
|
|
33
|
+
this.issues = issues;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** Capability is declared but not invokable yet (status: 'planned' / available()===false). */
|
|
37
|
+
export class UnavailableError extends CapabilityError {
|
|
38
|
+
reason;
|
|
39
|
+
constructor(message, { capability = null, reason = null } = {}) {
|
|
40
|
+
super(message, { code: 'UNAVAILABLE', capability });
|
|
41
|
+
this.name = 'UnavailableError';
|
|
42
|
+
this.reason = reason;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Capability's precheck rejected the args against live state (no-op target, missing element,
|
|
47
|
+
* guard condition). Thrown before pushHistory / any write.
|
|
48
|
+
*/
|
|
49
|
+
export class PrecheckError extends CapabilityError {
|
|
50
|
+
reason;
|
|
51
|
+
constructor(message, { capability = null, reason = null } = {}) {
|
|
52
|
+
super(message, { code: 'PRECHECK_FAILED', capability });
|
|
53
|
+
this.name = 'PrecheckError';
|
|
54
|
+
this.reason = reason;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Wired capability whose execute() is not implemented (generative stubs). Distinct from
|
|
59
|
+
* UnavailableError: "active but unfinished", reachable only if status was flipped to 'active'
|
|
60
|
+
* before the implementation landed.
|
|
61
|
+
*/
|
|
62
|
+
export class NotImplementedError extends CapabilityError {
|
|
63
|
+
constructor(capability, message = null) {
|
|
64
|
+
super(message || `Capability "${capability}" is not implemented yet`, { code: 'NOT_IMPLEMENTED', capability });
|
|
65
|
+
this.name = 'NotImplementedError';
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Reserved for the future generative path: the backend credit check failed (402-style).
|
|
70
|
+
* Declared now so the agent loop can catch it without a follow-up refactor.
|
|
71
|
+
*/
|
|
72
|
+
export class InsufficientCreditsError extends CapabilityError {
|
|
73
|
+
required;
|
|
74
|
+
available;
|
|
75
|
+
constructor(message, { capability = null, required = null, available = null } = {}) {
|
|
76
|
+
super(message, { code: 'INSUFFICIENT_CREDITS', capability });
|
|
77
|
+
this.name = 'InsufficientCreditsError';
|
|
78
|
+
this.required = required;
|
|
79
|
+
this.available = available;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export class UnknownCapabilityError extends CapabilityError {
|
|
83
|
+
constructor(name) {
|
|
84
|
+
super(`Unknown capability "${name}"`, { code: 'UNKNOWN_CAPABILITY', capability: name });
|
|
85
|
+
this.name = 'UnknownCapabilityError';
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared capability machinery — app-agnostic, layer-neutral registry + validator +
|
|
3
|
+
* error taxonomy. Both `src/studio/capabilities/` and `src/core/edit/capabilities/` build their
|
|
4
|
+
* own registry instances and descriptors on top of this. Imports nothing app-level (see
|
|
5
|
+
* `.agents/decisions/`). This is the generic core of the AI-ready capability seam.
|
|
6
|
+
*/
|
|
7
|
+
export { createCapabilityRegistry, type CapabilityRegistry, type CapabilityDescriptor, type CapabilityInfo, type CapabilityKind, type CapabilityStatus, type Reactivity, type InvokeMeta, type InvokeMode, type PrecheckResult, type RegistryCtx, } from './registry.js';
|
|
8
|
+
export { validate, assertValid, type JsonSchema, type JsonType } from './schema.js';
|
|
9
|
+
export * from './errors.js';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared capability machinery — app-agnostic, layer-neutral registry + validator +
|
|
3
|
+
* error taxonomy. Both `src/studio/capabilities/` and `src/core/edit/capabilities/` build their
|
|
4
|
+
* own registry instances and descriptors on top of this. Imports nothing app-level (see
|
|
5
|
+
* `.agents/decisions/`). This is the generic core of the AI-ready capability seam.
|
|
6
|
+
*/
|
|
7
|
+
export { createCapabilityRegistry, } from './registry.js';
|
|
8
|
+
export { validate, assertValid } from './schema.js';
|
|
9
|
+
export * from './errors.js';
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capability registry — the single, named, validated chokepoint for edits (shared machinery).
|
|
3
|
+
*
|
|
4
|
+
* The ONE registry both callers build on (supersedes the duplicated
|
|
5
|
+
* `studio/capabilities/registry.js` + `core/edit/capabilities/registry.ts`). It is the SUPERSET:
|
|
6
|
+
* the studio uses the full pipeline (precheck → reversible history → credit cost → generative),
|
|
7
|
+
* the edit-mode caller uses the lean subset (validation + named dispatch + the preview/commit
|
|
8
|
+
* `mode`) and simply leaves the extra features unset. App-agnostic: `src/capabilities/` imports
|
|
9
|
+
* nothing app-level (see `.agents/decisions/`); all state/functions arrive via the injected ctx.
|
|
10
|
+
*
|
|
11
|
+
* Today the UI calls invoke()/invokeSync() with source:'ui'; tomorrow the agent loop calls the
|
|
12
|
+
* SAME registry with source:'agent'. Every mutation flows through one pipeline so history,
|
|
13
|
+
* validation, and availability are enforced in exactly one place.
|
|
14
|
+
*
|
|
15
|
+
* invoke() pipeline:
|
|
16
|
+
* 1. resolve unknown name -> UnknownCapabilityError
|
|
17
|
+
* 2. assertAvailable status:'planned' / available(ctx)===false -> UnavailableError (no writes)
|
|
18
|
+
* 3. validate args vs inputSchema -> ValidationError (no writes)
|
|
19
|
+
* 4. precheck PURE, no writes. Returns { ok, reason, willMutate }. !ok -> PrecheckError
|
|
20
|
+
* 5. commit only now: if reversible && willMutate !== false -> ctx.pushHistory()
|
|
21
|
+
* 6. execute the actual write
|
|
22
|
+
*
|
|
23
|
+
* `mode` is the edit-mode preview/commit distinction (ignored by studio capabilities):
|
|
24
|
+
* 'preview' → live side effect only, no host broadcast 'commit' → side effect + emit.
|
|
25
|
+
*/
|
|
26
|
+
import { type JsonSchema } from './schema.js';
|
|
27
|
+
export type CapabilityKind = 'mutation' | 'query' | 'generative';
|
|
28
|
+
export type Reactivity = 'live' | 'structural';
|
|
29
|
+
export type CapabilityStatus = 'active' | 'planned';
|
|
30
|
+
export type InvokeMode = 'preview' | 'commit';
|
|
31
|
+
export interface InvokeMeta {
|
|
32
|
+
source: string;
|
|
33
|
+
mode: InvokeMode;
|
|
34
|
+
}
|
|
35
|
+
export interface PrecheckResult {
|
|
36
|
+
ok: boolean;
|
|
37
|
+
reason?: string | null;
|
|
38
|
+
/** false suppresses the no-op history snapshot at the commit boundary. */
|
|
39
|
+
willMutate?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The optional history hook the registry calls at the commit boundary for reversible mutations.
|
|
43
|
+
* A caller's ctx may include it (the studio does) or not (edit-mode has no history). It is NOT a
|
|
44
|
+
* generic constraint — the ctx type is free-form per caller — the registry just reads pushHistory
|
|
45
|
+
* off it when present.
|
|
46
|
+
*/
|
|
47
|
+
export interface RegistryCtx {
|
|
48
|
+
pushHistory?: () => void;
|
|
49
|
+
}
|
|
50
|
+
export interface CapabilityDescriptor<Ctx = Record<string, unknown>, Args = Record<string, unknown>> {
|
|
51
|
+
name: string;
|
|
52
|
+
kind: CapabilityKind;
|
|
53
|
+
description?: string;
|
|
54
|
+
inputSchema?: JsonSchema;
|
|
55
|
+
status?: CapabilityStatus;
|
|
56
|
+
reversible?: boolean;
|
|
57
|
+
/** Cost class of applying the edit: 'live' (cheap in-place) vs 'structural' (rebuild). */
|
|
58
|
+
reactivity?: Reactivity;
|
|
59
|
+
available?: (ctx: Ctx) => boolean;
|
|
60
|
+
cost?: (args: Args, ctx: Ctx) => number;
|
|
61
|
+
precheck?: (args: Args, ctx: Ctx, meta: InvokeMeta) => PrecheckResult | Promise<PrecheckResult>;
|
|
62
|
+
execute: (args: Args, ctx: Ctx, meta: InvokeMeta) => unknown;
|
|
63
|
+
}
|
|
64
|
+
/** Public, function-free view of a capability — what the agent tool-catalog builder consumes. */
|
|
65
|
+
export interface CapabilityInfo {
|
|
66
|
+
name: string;
|
|
67
|
+
kind: CapabilityKind;
|
|
68
|
+
status: CapabilityStatus;
|
|
69
|
+
description: string;
|
|
70
|
+
inputSchema: JsonSchema;
|
|
71
|
+
reversible: boolean;
|
|
72
|
+
reactivity: Reactivity | null;
|
|
73
|
+
}
|
|
74
|
+
interface NormalizedCapability<Ctx> {
|
|
75
|
+
name: string;
|
|
76
|
+
kind: CapabilityKind;
|
|
77
|
+
status: CapabilityStatus;
|
|
78
|
+
description: string;
|
|
79
|
+
inputSchema: JsonSchema;
|
|
80
|
+
reversible: boolean;
|
|
81
|
+
reactivity: Reactivity | null;
|
|
82
|
+
available: (ctx: Ctx) => boolean;
|
|
83
|
+
cost: (args: never, ctx: Ctx) => number;
|
|
84
|
+
precheck: (args: never, ctx: Ctx, meta: InvokeMeta) => PrecheckResult | Promise<PrecheckResult>;
|
|
85
|
+
execute: (args: never, ctx: Ctx, meta: InvokeMeta) => unknown;
|
|
86
|
+
}
|
|
87
|
+
export interface CapabilityRegistry<Ctx = Record<string, unknown>> {
|
|
88
|
+
register: (descriptor: CapabilityDescriptor<Ctx>) => NormalizedCapability<Ctx>;
|
|
89
|
+
registerAll: (descriptors: CapabilityDescriptor<Ctx>[]) => CapabilityRegistry<Ctx>;
|
|
90
|
+
has: (name: string) => boolean;
|
|
91
|
+
get: (name: string) => NormalizedCapability<Ctx> | null;
|
|
92
|
+
/** Public catalog view of one capability by name (null if unknown). */
|
|
93
|
+
describe: (name: string) => CapabilityInfo | null;
|
|
94
|
+
list: (opts?: {
|
|
95
|
+
includePlanned?: boolean;
|
|
96
|
+
kind?: CapabilityKind | null;
|
|
97
|
+
}) => CapabilityInfo[];
|
|
98
|
+
isAvailable: (name: string) => boolean;
|
|
99
|
+
cost: (name: string, args?: Record<string, unknown>) => number;
|
|
100
|
+
invoke: (name: string, args?: Record<string, unknown>, opts?: Partial<InvokeMeta>) => Promise<unknown>;
|
|
101
|
+
invokeSync: (name: string, args?: Record<string, unknown>, opts?: Partial<InvokeMeta>) => unknown;
|
|
102
|
+
}
|
|
103
|
+
export declare function createCapabilityRegistry<Ctx = Record<string, unknown>>({ ctx }?: {
|
|
104
|
+
ctx?: Ctx;
|
|
105
|
+
}): CapabilityRegistry<Ctx>;
|
|
106
|
+
export {};
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capability registry — the single, named, validated chokepoint for edits (shared machinery).
|
|
3
|
+
*
|
|
4
|
+
* The ONE registry both callers build on (supersedes the duplicated
|
|
5
|
+
* `studio/capabilities/registry.js` + `core/edit/capabilities/registry.ts`). It is the SUPERSET:
|
|
6
|
+
* the studio uses the full pipeline (precheck → reversible history → credit cost → generative),
|
|
7
|
+
* the edit-mode caller uses the lean subset (validation + named dispatch + the preview/commit
|
|
8
|
+
* `mode`) and simply leaves the extra features unset. App-agnostic: `src/capabilities/` imports
|
|
9
|
+
* nothing app-level (see `.agents/decisions/`); all state/functions arrive via the injected ctx.
|
|
10
|
+
*
|
|
11
|
+
* Today the UI calls invoke()/invokeSync() with source:'ui'; tomorrow the agent loop calls the
|
|
12
|
+
* SAME registry with source:'agent'. Every mutation flows through one pipeline so history,
|
|
13
|
+
* validation, and availability are enforced in exactly one place.
|
|
14
|
+
*
|
|
15
|
+
* invoke() pipeline:
|
|
16
|
+
* 1. resolve unknown name -> UnknownCapabilityError
|
|
17
|
+
* 2. assertAvailable status:'planned' / available(ctx)===false -> UnavailableError (no writes)
|
|
18
|
+
* 3. validate args vs inputSchema -> ValidationError (no writes)
|
|
19
|
+
* 4. precheck PURE, no writes. Returns { ok, reason, willMutate }. !ok -> PrecheckError
|
|
20
|
+
* 5. commit only now: if reversible && willMutate !== false -> ctx.pushHistory()
|
|
21
|
+
* 6. execute the actual write
|
|
22
|
+
*
|
|
23
|
+
* `mode` is the edit-mode preview/commit distinction (ignored by studio capabilities):
|
|
24
|
+
* 'preview' → live side effect only, no host broadcast 'commit' → side effect + emit.
|
|
25
|
+
*/
|
|
26
|
+
import { PrecheckError, UnavailableError, UnknownCapabilityError, ValidationError, } from './errors.js';
|
|
27
|
+
import { assertValid } from './schema.js';
|
|
28
|
+
const VALID_KINDS = new Set(['mutation', 'query', 'generative']);
|
|
29
|
+
// Fill descriptor defaults once at registration so invoke() stays branch-light.
|
|
30
|
+
function normalize(descriptor) {
|
|
31
|
+
if (!descriptor || typeof descriptor !== 'object')
|
|
32
|
+
throw new Error('[capabilities] descriptor must be an object');
|
|
33
|
+
const { name, kind } = descriptor;
|
|
34
|
+
if (typeof name !== 'string' || !name)
|
|
35
|
+
throw new Error('[capabilities] descriptor.name is required');
|
|
36
|
+
if (!VALID_KINDS.has(kind)) {
|
|
37
|
+
throw new Error(`[capabilities] "${name}" has invalid kind "${kind}" (expected mutation|query|generative)`);
|
|
38
|
+
}
|
|
39
|
+
const status = descriptor.status ?? 'active';
|
|
40
|
+
if (status !== 'active' && status !== 'planned') {
|
|
41
|
+
throw new Error(`[capabilities] "${name}" has invalid status "${status}" (expected active|planned)`);
|
|
42
|
+
}
|
|
43
|
+
if (typeof descriptor.execute !== 'function')
|
|
44
|
+
throw new Error(`[capabilities] "${name}" must define an execute() function`);
|
|
45
|
+
return {
|
|
46
|
+
name,
|
|
47
|
+
kind,
|
|
48
|
+
status,
|
|
49
|
+
description: descriptor.description ?? '',
|
|
50
|
+
inputSchema: descriptor.inputSchema ?? { type: 'object' },
|
|
51
|
+
reversible: descriptor.reversible === true,
|
|
52
|
+
reactivity: descriptor.reactivity ?? null,
|
|
53
|
+
available: typeof descriptor.available === 'function' ? descriptor.available : () => status === 'active',
|
|
54
|
+
cost: (typeof descriptor.cost === 'function' ? descriptor.cost : () => 0),
|
|
55
|
+
precheck: (typeof descriptor.precheck === 'function' ? descriptor.precheck : () => ({ ok: true })),
|
|
56
|
+
execute: descriptor.execute,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export function createCapabilityRegistry({ ctx = {} } = {}) {
|
|
60
|
+
const caps = new Map();
|
|
61
|
+
function register(descriptor) {
|
|
62
|
+
const cap = normalize(descriptor);
|
|
63
|
+
if (caps.has(cap.name))
|
|
64
|
+
throw new Error(`[capabilities] "${cap.name}" is already registered`);
|
|
65
|
+
caps.set(cap.name, cap);
|
|
66
|
+
return cap;
|
|
67
|
+
}
|
|
68
|
+
const has = (name) => caps.has(name);
|
|
69
|
+
const get = (name) => caps.get(name) || null;
|
|
70
|
+
// Function-free catalog view of a capability — never leaks execute/precheck to catalog-only
|
|
71
|
+
// callers (the future tool-schema builder).
|
|
72
|
+
function info(cap) {
|
|
73
|
+
return {
|
|
74
|
+
name: cap.name,
|
|
75
|
+
kind: cap.kind,
|
|
76
|
+
status: cap.status,
|
|
77
|
+
description: cap.description,
|
|
78
|
+
inputSchema: cap.inputSchema,
|
|
79
|
+
reversible: cap.reversible,
|
|
80
|
+
reactivity: cap.reactivity,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function describe(name) {
|
|
84
|
+
const cap = get(name);
|
|
85
|
+
return cap ? info(cap) : null;
|
|
86
|
+
}
|
|
87
|
+
function list({ includePlanned = false, kind = null } = {}) {
|
|
88
|
+
const out = [];
|
|
89
|
+
for (const cap of caps.values()) {
|
|
90
|
+
if (!includePlanned && cap.status !== 'active')
|
|
91
|
+
continue;
|
|
92
|
+
if (kind && cap.kind !== kind)
|
|
93
|
+
continue;
|
|
94
|
+
out.push(info(cap));
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
function isAvailable(name) {
|
|
99
|
+
const cap = get(name);
|
|
100
|
+
if (!cap || cap.status !== 'active')
|
|
101
|
+
return false;
|
|
102
|
+
try {
|
|
103
|
+
return cap.available(ctx) !== false;
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function cost(name, args = {}) {
|
|
110
|
+
const cap = get(name);
|
|
111
|
+
if (!cap)
|
|
112
|
+
return 0;
|
|
113
|
+
try {
|
|
114
|
+
return Number(cap.cost(args, ctx)) || 0;
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
return 0;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// Steps 1–3 (resolve → availability → validate): pure, no side effects. Shared by both paths.
|
|
121
|
+
function prepareInvocation(name, args, meta) {
|
|
122
|
+
const cap = get(name);
|
|
123
|
+
if (!cap)
|
|
124
|
+
throw new UnknownCapabilityError(name);
|
|
125
|
+
if (cap.status !== 'active') {
|
|
126
|
+
throw new UnavailableError(`Capability "${name}" is declared but not available yet`, { capability: name, reason: `status:${cap.status}` });
|
|
127
|
+
}
|
|
128
|
+
let availResult;
|
|
129
|
+
try {
|
|
130
|
+
availResult = cap.available(ctx);
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
throw new UnavailableError(`Capability "${name}" availability check threw`, { capability: name, reason: err?.message || String(err) });
|
|
134
|
+
}
|
|
135
|
+
if (availResult === false) {
|
|
136
|
+
throw new UnavailableError(`Capability "${name}" is not currently available`, { capability: name, reason: 'available(ctx) returned false' });
|
|
137
|
+
}
|
|
138
|
+
assertValid(cap.inputSchema, args, name);
|
|
139
|
+
void meta;
|
|
140
|
+
return cap;
|
|
141
|
+
}
|
|
142
|
+
function wrapPrecheckError(name, err) {
|
|
143
|
+
if (err instanceof ValidationError || err instanceof PrecheckError)
|
|
144
|
+
return err;
|
|
145
|
+
return new PrecheckError(`Precheck for "${name}" threw: ${err?.message || err}`, { capability: name, reason: err?.message || String(err) });
|
|
146
|
+
}
|
|
147
|
+
function resolvePrecheck(name, pre) {
|
|
148
|
+
const r = pre || { ok: true };
|
|
149
|
+
if (r.ok === false) {
|
|
150
|
+
throw new PrecheckError(`Precheck for "${name}" rejected: ${r.reason || 'no reason given'}`, { capability: name, reason: r.reason ?? null });
|
|
151
|
+
}
|
|
152
|
+
return r;
|
|
153
|
+
}
|
|
154
|
+
// commit boundary — snapshot only a change that will actually mutate.
|
|
155
|
+
function commitHistory(cap, pre) {
|
|
156
|
+
const pushHistory = ctx?.pushHistory;
|
|
157
|
+
if (cap.reversible && pre.willMutate !== false && typeof pushHistory === 'function') {
|
|
158
|
+
pushHistory();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const fullMeta = (opts) => ({ source: opts?.source ?? 'ui', mode: opts?.mode ?? 'commit' });
|
|
162
|
+
// Async pipeline — supports async precheck/execute (the generative/agent path).
|
|
163
|
+
async function invoke(name, args = {}, opts) {
|
|
164
|
+
const meta = fullMeta(opts);
|
|
165
|
+
const cap = prepareInvocation(name, args, meta);
|
|
166
|
+
let pre;
|
|
167
|
+
try {
|
|
168
|
+
pre = resolvePrecheck(name, await cap.precheck(args, ctx, meta));
|
|
169
|
+
}
|
|
170
|
+
catch (err) {
|
|
171
|
+
throw wrapPrecheckError(name, err);
|
|
172
|
+
}
|
|
173
|
+
commitHistory(cap, pre);
|
|
174
|
+
return cap.execute(args, ctx, meta);
|
|
175
|
+
}
|
|
176
|
+
// Synchronous pipeline for UI mutation/query callers — preserves handler ordering (the write
|
|
177
|
+
// lands before the caller's following side effects). Rejects async precheck/execute.
|
|
178
|
+
function invokeSync(name, args = {}, opts) {
|
|
179
|
+
const meta = fullMeta(opts);
|
|
180
|
+
const cap = prepareInvocation(name, args, meta);
|
|
181
|
+
let preResult;
|
|
182
|
+
try {
|
|
183
|
+
preResult = cap.precheck(args, ctx, meta);
|
|
184
|
+
}
|
|
185
|
+
catch (err) {
|
|
186
|
+
throw wrapPrecheckError(name, err);
|
|
187
|
+
}
|
|
188
|
+
if (preResult && typeof preResult.then === 'function') {
|
|
189
|
+
throw new Error(`Capability "${name}" has an async precheck — use invoke()`);
|
|
190
|
+
}
|
|
191
|
+
const pre = resolvePrecheck(name, preResult);
|
|
192
|
+
commitHistory(cap, pre);
|
|
193
|
+
const result = cap.execute(args, ctx, meta);
|
|
194
|
+
if (result && typeof result.then === 'function') {
|
|
195
|
+
throw new Error(`Capability "${name}" execute() is async — use invoke() (e.g. generative)`);
|
|
196
|
+
}
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
const api = {
|
|
200
|
+
register,
|
|
201
|
+
registerAll(descriptors) {
|
|
202
|
+
descriptors.forEach(register);
|
|
203
|
+
return api;
|
|
204
|
+
},
|
|
205
|
+
has,
|
|
206
|
+
get,
|
|
207
|
+
describe,
|
|
208
|
+
list,
|
|
209
|
+
isAvailable,
|
|
210
|
+
cost,
|
|
211
|
+
invoke,
|
|
212
|
+
invokeSync,
|
|
213
|
+
};
|
|
214
|
+
return api;
|
|
215
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tiny JSON-Schema-subset validator (shared machinery, TypeScript).
|
|
3
|
+
*
|
|
4
|
+
* The ONE validator both registries use (supersedes the duplicated
|
|
5
|
+
* `studio/capabilities/schema.js` + `core/edit/capabilities/schema.ts`). Deliberately NOT a
|
|
6
|
+
* dependency (no zod/ajv) — the app is bundle-weight sensitive and the house style is
|
|
7
|
+
* lightweight guard clauses. Each capability carries an `inputSchema` literal in this subset;
|
|
8
|
+
* the SAME literal is what the future agent layer hands the model as a tool definition, so
|
|
9
|
+
* there is no second schema language to keep in sync.
|
|
10
|
+
*
|
|
11
|
+
* Supported keywords (a pragmatic subset of JSON Schema draft-07):
|
|
12
|
+
* type 'object' | 'array' | 'string' | 'number' | 'integer' | 'boolean' | 'null'
|
|
13
|
+
* (or an array of those — value matches if it satisfies any)
|
|
14
|
+
* properties per-key subschemas (objects)
|
|
15
|
+
* required array of keys that must be present and not undefined (objects)
|
|
16
|
+
* items subschema applied to every array element
|
|
17
|
+
* enum value must be one of the listed values
|
|
18
|
+
* const value must deep-equal the given value
|
|
19
|
+
* nullable when true, null always passes regardless of type
|
|
20
|
+
* minimum / maximum numbers
|
|
21
|
+
* minLength / maxLength strings
|
|
22
|
+
* minItems / maxItems arrays
|
|
23
|
+
* pattern string regex source (tested with RegExp)
|
|
24
|
+
*
|
|
25
|
+
* A schema with no `type` accepts any value (still honouring enum/const/properties if set).
|
|
26
|
+
* Unknown keywords are ignored — forward-compatible, never throws on shape it doesn't know.
|
|
27
|
+
*/
|
|
28
|
+
import { type SchemaIssue } from './errors.js';
|
|
29
|
+
export type JsonType = 'object' | 'array' | 'string' | 'number' | 'integer' | 'boolean' | 'null';
|
|
30
|
+
export interface JsonSchema {
|
|
31
|
+
type?: JsonType | JsonType[];
|
|
32
|
+
properties?: Record<string, JsonSchema>;
|
|
33
|
+
required?: string[];
|
|
34
|
+
items?: JsonSchema;
|
|
35
|
+
enum?: unknown[];
|
|
36
|
+
const?: unknown;
|
|
37
|
+
nullable?: boolean;
|
|
38
|
+
minimum?: number;
|
|
39
|
+
maximum?: number;
|
|
40
|
+
minLength?: number;
|
|
41
|
+
maxLength?: number;
|
|
42
|
+
minItems?: number;
|
|
43
|
+
maxItems?: number;
|
|
44
|
+
pattern?: string;
|
|
45
|
+
}
|
|
46
|
+
/** Returns { ok, issues }. Never throws. */
|
|
47
|
+
export declare function validate(schema: JsonSchema, value: unknown): {
|
|
48
|
+
ok: boolean;
|
|
49
|
+
issues: SchemaIssue[];
|
|
50
|
+
};
|
|
51
|
+
/** Throws ValidationError when invalid; returns the value otherwise. Used by the registry. */
|
|
52
|
+
export declare function assertValid<T>(schema: JsonSchema, value: T, capability?: string | null): T;
|