@forgeax/engine-tool-runtime 0.1.2

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.
@@ -0,0 +1,314 @@
1
+ export type ToolRealm = 'build' | 'host' | 'engine';
2
+ export type ToolEvidenceKind = 'rhi-tape' | 'profile-capture' | 'png';
3
+ /** Stable subject identity carried by a domain contribution, never a live handle. */
4
+ export interface ToolSubjectRef {
5
+ readonly kind: string;
6
+ readonly guid: string;
7
+ }
8
+ /** The portable preview contract shared by descriptor, terminal and artifact readers. */
9
+ export interface ToolPreviewContract {
10
+ readonly realm: ToolRealm;
11
+ readonly subject: ToolSubjectRef;
12
+ readonly snapshot: SnapshotRef;
13
+ readonly requiredEvidence: readonly ToolEvidenceKind[];
14
+ }
15
+ /** Resource census captured after a lexical ToolRun lease has terminated. */
16
+ export interface ToolCleanupCensus {
17
+ readonly worlds: number;
18
+ readonly renderers: number;
19
+ readonly canvases: number;
20
+ readonly leases: number;
21
+ }
22
+ export interface ToolCleanupReport {
23
+ readonly census: ToolCleanupCensus;
24
+ readonly failures: readonly string[];
25
+ }
26
+ export type ToolTimingPhase = 'lookup' | 'lease' | 'transport' | 'execute' | 'capture' | 'finalize' | 'analyze';
27
+ export type ToolPhaseObservation = {
28
+ readonly status: 'observed';
29
+ readonly durationMs: number;
30
+ readonly workUnits?: number;
31
+ } | {
32
+ readonly status: 'not-applicable';
33
+ } | {
34
+ readonly status: 'unavailable';
35
+ readonly reason: string;
36
+ };
37
+ export type ToolResourceObservation = {
38
+ readonly status: 'observed';
39
+ readonly bytes: number;
40
+ readonly source: string;
41
+ } | {
42
+ readonly status: 'not-applicable';
43
+ } | {
44
+ readonly status: 'unavailable';
45
+ readonly reason: string;
46
+ };
47
+ export type ToolResourceKind = 'node-rss' | 'browser-js-heap' | 'artifact-bytes' | 'gpu-memory';
48
+ export type JsonPrimitive = string | number | boolean | null;
49
+ export type JsonValue = JsonPrimitive | readonly JsonValue[] | {
50
+ readonly [key: string]: JsonValue;
51
+ };
52
+ export interface ToolCapability<T> {
53
+ readonly id: string;
54
+ readonly __toolCapability?: T;
55
+ }
56
+ export type ToolCapabilityResult<T> = {
57
+ readonly ok: true;
58
+ readonly value: T;
59
+ } | {
60
+ readonly ok: false;
61
+ readonly error: ToolRuntimeError;
62
+ };
63
+ export type ToolCapabilityResolver = <T>(capability: ToolCapability<T>) => ToolCapabilityResult<T> | undefined;
64
+ export interface ToolSchema<T> {
65
+ readonly parse: (value: unknown) => ToolSchemaResult<T>;
66
+ readonly describe?: string;
67
+ }
68
+ export type ToolSchemaResult<T> = {
69
+ readonly ok: true;
70
+ readonly value: T;
71
+ } | {
72
+ readonly ok: false;
73
+ readonly error: string;
74
+ };
75
+ export interface ToolDescriptor<TArgs = unknown, TResult = unknown> {
76
+ readonly id: string;
77
+ readonly title: string;
78
+ readonly summary: string;
79
+ readonly realm: ToolRealm;
80
+ readonly argsSchema: ToolSchema<TArgs>;
81
+ readonly resultSchema: ToolSchema<TResult>;
82
+ readonly evidence: readonly ToolEvidenceKind[];
83
+ /** Domain preview descriptors bind a subject and snapshot without exposing live state. */
84
+ readonly preview?: ToolPreviewContract;
85
+ }
86
+ export interface ToolSnapshotInput {
87
+ readonly snapshot?: SnapshotRef;
88
+ readonly projectRoot?: string;
89
+ }
90
+ export interface ToolExecutionContext {
91
+ readonly runId: string;
92
+ readonly signal: AbortSignal;
93
+ readonly snapshot?: SnapshotRef;
94
+ readonly emit: (event: ToolRunEvent) => void;
95
+ readonly addCleanup: (cleanup: () => void | Promise<void>) => void;
96
+ readonly setCleanupReport: (report: ToolCleanupReport) => void;
97
+ readonly require: <T>(capability: ToolCapability<T>) => ToolCapabilityResult<T>;
98
+ readonly runChild: <TChildArgs, TChildResult>(contribution: ToolContribution<TChildArgs, TChildResult>, args: TChildArgs, options?: ToolRunOptions) => Promise<ToolTerminal<TChildResult>>;
99
+ }
100
+ export interface ToolRecoveryAction {
101
+ readonly action: 'retry' | 'switch-operation' | 'repair-owner' | 'inspect-evidence' | 'stop';
102
+ readonly hint: string;
103
+ readonly operation?: string;
104
+ }
105
+ export type ToolDomainFailure = {
106
+ readonly code: string;
107
+ readonly expected?: string;
108
+ readonly hint?: string;
109
+ readonly detail?: JsonValue;
110
+ };
111
+ export type ToolExecutor<TArgs, TResult> = (args: TArgs, context: ToolExecutionContext) => ToolExecutorResult<TResult>;
112
+ export type ToolExecutorValue<TResult> = TResult | ToolTerminal<TResult> | {
113
+ readonly ok: true;
114
+ readonly value: TResult;
115
+ readonly snapshotAfter?: SnapshotRef;
116
+ readonly artifacts?: readonly ArtifactRef[];
117
+ readonly cleanup?: ToolCleanupReport;
118
+ } | {
119
+ readonly ok: false;
120
+ readonly error: ToolDomainFailure;
121
+ };
122
+ export type ToolExecutorResult<TResult> = ToolExecutorValue<TResult> | Promise<ToolExecutorValue<TResult>>;
123
+ export interface ToolContribution<TArgs = unknown, TResult = unknown> {
124
+ readonly descriptor: ToolDescriptor<TArgs, TResult>;
125
+ readonly execute: ToolExecutor<TArgs, TResult>;
126
+ }
127
+ export interface ToolRunOptions extends ToolSnapshotInput {
128
+ readonly signal?: AbortSignal;
129
+ readonly deadlineMs?: number;
130
+ readonly evidence?: readonly ToolEvidenceKind[];
131
+ readonly capabilityResolver?: ToolCapabilityResolver;
132
+ }
133
+ export type ToolRunEvent = {
134
+ readonly kind: 'started';
135
+ readonly runId: string;
136
+ readonly atMs: number;
137
+ } | {
138
+ readonly kind: 'progress';
139
+ readonly runId: string;
140
+ readonly message: string;
141
+ readonly atMs: number;
142
+ } | {
143
+ readonly kind: 'child-started';
144
+ readonly runId: string;
145
+ readonly childRunId: string;
146
+ readonly atMs: number;
147
+ } | {
148
+ readonly kind: 'terminal';
149
+ readonly runId: string;
150
+ readonly outcome: ToolTerminal<unknown>['outcome'];
151
+ readonly atMs: number;
152
+ };
153
+ export interface ToolRun<TResult> {
154
+ readonly id: string;
155
+ readonly events: AsyncIterable<ToolRunEvent>;
156
+ readonly terminal: Promise<ToolTerminal<TResult>>;
157
+ readonly cancel: (reason?: string) => void;
158
+ readonly disconnect: (transport?: string) => void;
159
+ readonly providerExit: (provider?: string) => void;
160
+ }
161
+ export interface ToolTiming {
162
+ readonly startedAtMs: number;
163
+ readonly endedAtMs: number;
164
+ readonly durationMs: number;
165
+ readonly phases?: Readonly<Record<ToolTimingPhase, ToolPhaseObservation>>;
166
+ readonly unattributedMs?: number;
167
+ readonly resources?: Readonly<Partial<Record<ToolResourceKind, ToolResourceObservation>>>;
168
+ }
169
+ export type ToolTerminal<TResult> = {
170
+ readonly outcome: 'succeeded';
171
+ readonly result: TResult;
172
+ readonly snapshotAfter?: SnapshotRef;
173
+ readonly artifacts: readonly ArtifactRef[];
174
+ readonly cleanup?: ToolCleanupReport;
175
+ readonly timing?: ToolTiming;
176
+ } | {
177
+ readonly outcome: 'failed';
178
+ readonly failure: ToolRuntimeError;
179
+ readonly snapshotAfter?: SnapshotRef;
180
+ readonly artifacts: readonly ArtifactRef[];
181
+ readonly cleanup?: ToolCleanupReport;
182
+ readonly timing?: ToolTiming;
183
+ };
184
+ export interface SnapshotRef {
185
+ readonly revision: number;
186
+ readonly digest: string;
187
+ }
188
+ export interface ArtifactRef {
189
+ readonly kind: ToolEvidenceKind | 'tool-result' | 'receipt';
190
+ readonly digest: string;
191
+ readonly uri?: string;
192
+ readonly mediaType?: string;
193
+ readonly sizeBytes?: number;
194
+ }
195
+ export type ToolRuntimeError = {
196
+ readonly code: 'tool-bootstrap-not-clone-safe';
197
+ readonly expected: string;
198
+ readonly hint: string;
199
+ readonly detail: {
200
+ readonly message: string;
201
+ };
202
+ } | {
203
+ readonly code: 'tool-migration-live-state';
204
+ readonly expected: string;
205
+ readonly hint: string;
206
+ readonly detail: {
207
+ readonly path: string;
208
+ readonly key: string;
209
+ };
210
+ } | {
211
+ readonly code: 'tool-invalid-args';
212
+ readonly expected: string;
213
+ readonly hint: string;
214
+ readonly detail: {
215
+ readonly message: string;
216
+ readonly value: JsonValue;
217
+ };
218
+ } | {
219
+ readonly code: 'tool-capability-unavailable';
220
+ readonly expected: string;
221
+ readonly hint: string;
222
+ readonly detail: {
223
+ readonly capability: string;
224
+ readonly realm: ToolRealm;
225
+ };
226
+ } | {
227
+ readonly code: 'tool-snapshot-stale';
228
+ readonly expected: string;
229
+ readonly hint: string;
230
+ readonly detail: {
231
+ readonly expectedDigest: string;
232
+ readonly actualDigest: string;
233
+ };
234
+ } | {
235
+ readonly code: 'tool-artifact-incomplete';
236
+ readonly expected: string;
237
+ readonly hint: string;
238
+ readonly detail: {
239
+ readonly missing: readonly ToolEvidenceKind[];
240
+ readonly runId: string;
241
+ };
242
+ } | {
243
+ readonly code: 'tool-run-cancelled';
244
+ readonly expected: string;
245
+ readonly hint: string;
246
+ readonly detail: {
247
+ readonly reason: string;
248
+ };
249
+ } | {
250
+ readonly code: 'tool-run-timeout';
251
+ readonly expected: string;
252
+ readonly hint: string;
253
+ readonly detail: {
254
+ readonly deadlineMs: number;
255
+ };
256
+ } | {
257
+ readonly code: 'tool-run-disconnected';
258
+ readonly expected: string;
259
+ readonly hint: string;
260
+ readonly detail: {
261
+ readonly transport: string;
262
+ };
263
+ } | {
264
+ readonly code: 'tool-run-terminal';
265
+ readonly expected: string;
266
+ readonly hint: string;
267
+ readonly detail: {
268
+ readonly runId: string;
269
+ readonly outcome: ToolTerminal<unknown>['outcome'];
270
+ };
271
+ } | {
272
+ readonly code: 'tool-catalog-stale';
273
+ readonly expected: string;
274
+ readonly hint: string;
275
+ readonly detail: {
276
+ readonly id: string;
277
+ readonly expectedDigest: string;
278
+ readonly actualDigest?: string;
279
+ };
280
+ } | {
281
+ readonly code: 'tool-cleanup-failed';
282
+ readonly expected: string;
283
+ readonly hint: string;
284
+ readonly detail: {
285
+ readonly runId: string;
286
+ readonly message: string;
287
+ };
288
+ } | {
289
+ readonly code: 'tool-domain-failed';
290
+ readonly expected: string;
291
+ readonly hint: string;
292
+ readonly detail: {
293
+ readonly code: string;
294
+ readonly payload?: JsonValue;
295
+ readonly suggestedOperation?: string;
296
+ readonly recovery?: readonly ToolRecoveryAction[];
297
+ };
298
+ } | {
299
+ readonly code: 'tool-artifact-manifest-invalid';
300
+ readonly expected: string;
301
+ readonly hint: string;
302
+ readonly detail: {
303
+ readonly reason: string;
304
+ readonly runId?: string;
305
+ };
306
+ } | {
307
+ readonly code: 'tool-timing-invalid';
308
+ readonly expected: string;
309
+ readonly hint: string;
310
+ readonly detail: {
311
+ readonly reason: string;
312
+ };
313
+ };
314
+ export type ToolRuntimeErrorCode = ToolRuntimeError['code'];
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@forgeax/engine-tool-runtime",
3
+ "version": "0.1.2",
4
+ "private": false,
5
+ "type": "module",
6
+ "license": "Apache-2.0",
7
+ "sideEffects": false,
8
+ "description": "Realm-neutral typed tool contributions and terminal contracts for AI authoring.",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.mjs"
13
+ },
14
+ "./package.json": "./package.json"
15
+ },
16
+ "main": "./dist/index.mjs",
17
+ "types": "./dist/index.d.ts",
18
+ "files": [
19
+ "dist",
20
+ "README.md",
21
+ "LICENSE"
22
+ ],
23
+ "forgeax": {
24
+ "metrics": {
25
+ "bundle-size": {
26
+ "enabled": true,
27
+ "path": "dist/index.mjs",
28
+ "compression": "gzip"
29
+ },
30
+ "fps": {
31
+ "enabled": false,
32
+ "reason": "Tool execution is outside the frame loop"
33
+ },
34
+ "bench": {
35
+ "enabled": false,
36
+ "reason": "The runtime contract owns no hot loop"
37
+ },
38
+ "gate": {
39
+ "enabled": true,
40
+ "command": "pnpm test",
41
+ "cwd": "packages/tool-runtime"
42
+ },
43
+ "spike-report": {
44
+ "enabled": false,
45
+ "reason": "Production contract package"
46
+ }
47
+ }
48
+ },
49
+ "scripts": {
50
+ "build": "tsup",
51
+ "test": "vitest run"
52
+ }
53
+ }