@dxos/compute 0.8.4-main.03d5cd7b56 → 0.8.4-main.05e74ebcff
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 +102 -5
- package/dist/lib/neutral/{chunk-EG7VLX2F.mjs → chunk-AKVCVI6N.mjs} +3 -3
- package/dist/lib/neutral/{chunk-JTZYM2QW.mjs → chunk-LF2ANNYW.mjs} +29 -2
- package/dist/lib/neutral/chunk-LF2ANNYW.mjs.map +7 -0
- package/dist/lib/neutral/{chunk-OA7JMLJK.mjs → chunk-LRCABAWU.mjs} +3 -3
- package/dist/lib/neutral/{chunk-YEDUHFRC.mjs → chunk-RCLYF66G.mjs} +3 -3
- package/dist/lib/neutral/{chunk-VIRNEVFD.mjs → chunk-SEBWOTOU.mjs} +58 -22
- package/dist/lib/neutral/chunk-SEBWOTOU.mjs.map +7 -0
- package/dist/lib/neutral/{chunk-CXDV2FPU.mjs → chunk-VSICPGGY.mjs} +2 -2
- package/dist/lib/neutral/fib-ZSJYREBW.mjs +10 -0
- package/dist/lib/neutral/index.mjs +243 -91
- package/dist/lib/neutral/index.mjs.map +4 -4
- package/dist/lib/neutral/meta.json +1 -1
- package/dist/lib/neutral/reply-YH3BVOUT.mjs +10 -0
- package/dist/lib/neutral/sleep-TSPIQ5EY.mjs +10 -0
- package/dist/lib/neutral/testing/index.mjs +7 -7
- package/dist/types/src/Blueprint.d.ts +57 -5
- package/dist/types/src/Blueprint.d.ts.map +1 -1
- package/dist/types/src/LayerSpec.d.ts +56 -0
- package/dist/types/src/LayerSpec.d.ts.map +1 -0
- package/dist/types/src/Operation.d.ts +35 -4
- package/dist/types/src/Operation.d.ts.map +1 -1
- package/dist/types/src/OperationHandlerSet.d.ts +7 -0
- package/dist/types/src/OperationHandlerSet.d.ts.map +1 -1
- package/dist/types/src/OperationHandlerSet.test.d.ts +2 -0
- package/dist/types/src/OperationHandlerSet.test.d.ts.map +1 -0
- package/dist/types/src/OperationRegistry.d.ts.map +1 -1
- package/dist/types/src/Process.d.ts.map +1 -1
- package/dist/types/src/Routine.d.ts +5 -5
- package/dist/types/src/Routine.d.ts.map +1 -1
- package/dist/types/src/ServiceResolver.d.ts +15 -0
- package/dist/types/src/ServiceResolver.d.ts.map +1 -1
- package/dist/types/src/Trace.d.ts +94 -0
- package/dist/types/src/Trace.d.ts.map +1 -1
- package/dist/types/src/Trigger.d.ts +1 -1
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +13 -12
- package/src/Blueprint.ts +116 -20
- package/src/LayerSpec.ts +91 -0
- package/src/Operation.ts +83 -23
- package/src/OperationHandlerSet.test.ts +92 -0
- package/src/OperationHandlerSet.ts +40 -0
- package/src/OperationRegistry.ts +2 -2
- package/src/Process.ts +14 -0
- package/src/Routine.ts +31 -44
- package/src/ServiceResolver.ts +20 -0
- package/src/Trace.ts +141 -4
- package/src/Trigger.ts +2 -2
- package/src/index.ts +1 -0
- package/dist/lib/neutral/chunk-JTZYM2QW.mjs.map +0 -7
- package/dist/lib/neutral/chunk-VIRNEVFD.mjs.map +0 -7
- package/dist/lib/neutral/fib-WSN4EY7R.mjs +0 -10
- package/dist/lib/neutral/reply-XRLTFNDF.mjs +0 -10
- package/dist/lib/neutral/sleep-7O76LQCA.mjs +0 -10
- /package/dist/lib/neutral/{chunk-EG7VLX2F.mjs.map → chunk-AKVCVI6N.mjs.map} +0 -0
- /package/dist/lib/neutral/{chunk-OA7JMLJK.mjs.map → chunk-LRCABAWU.mjs.map} +0 -0
- /package/dist/lib/neutral/{chunk-YEDUHFRC.mjs.map → chunk-RCLYF66G.mjs.map} +0 -0
- /package/dist/lib/neutral/{chunk-CXDV2FPU.mjs.map → chunk-VSICPGGY.mjs.map} +0 -0
- /package/dist/lib/neutral/{fib-WSN4EY7R.mjs.map → fib-ZSJYREBW.mjs.map} +0 -0
- /package/dist/lib/neutral/{reply-XRLTFNDF.mjs.map → reply-YH3BVOUT.mjs.map} +0 -0
- /package/dist/lib/neutral/{sleep-7O76LQCA.mjs.map → sleep-TSPIQ5EY.mjs.map} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/compute",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.05e74ebcff",
|
|
4
4
|
"description": "Umbrella package for AI-aware DXOS compute primitives.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "https://github.com/dxos/dxos"
|
|
10
10
|
},
|
|
11
|
-
"license": "
|
|
11
|
+
"license": "FSL-1.1-Apache-2.0",
|
|
12
12
|
"author": "DXOS.org",
|
|
13
13
|
"sideEffects": false,
|
|
14
14
|
"type": "module",
|
|
@@ -36,19 +36,20 @@
|
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@effect-atom/atom": "^0.5.1",
|
|
39
|
-
"@dxos/
|
|
40
|
-
"@dxos/
|
|
41
|
-
"@dxos/errors": "0.8.4-main.
|
|
42
|
-
"@dxos/
|
|
43
|
-
"@dxos/
|
|
44
|
-
"@dxos/
|
|
45
|
-
"@dxos/
|
|
46
|
-
"@dxos/vendor-kbn-handlebars": "0.8.4-main.
|
|
47
|
-
"@dxos/
|
|
39
|
+
"@dxos/ai": "0.8.4-main.05e74ebcff",
|
|
40
|
+
"@dxos/effect": "0.8.4-main.05e74ebcff",
|
|
41
|
+
"@dxos/errors": "0.8.4-main.05e74ebcff",
|
|
42
|
+
"@dxos/invariant": "0.8.4-main.05e74ebcff",
|
|
43
|
+
"@dxos/echo": "0.8.4-main.05e74ebcff",
|
|
44
|
+
"@dxos/keys": "0.8.4-main.05e74ebcff",
|
|
45
|
+
"@dxos/log": "0.8.4-main.05e74ebcff",
|
|
46
|
+
"@dxos/vendor-kbn-handlebars": "0.8.4-main.05e74ebcff",
|
|
47
|
+
"@dxos/schema": "0.8.4-main.05e74ebcff",
|
|
48
|
+
"@dxos/protocols": "0.8.4-main.05e74ebcff"
|
|
48
49
|
},
|
|
49
50
|
"devDependencies": {
|
|
50
51
|
"effect": "3.20.0",
|
|
51
|
-
"vitest": "4.1.
|
|
52
|
+
"vitest": "4.1.6"
|
|
52
53
|
},
|
|
53
54
|
"peerDependencies": {
|
|
54
55
|
"effect": "3.20.0"
|
package/src/Blueprint.ts
CHANGED
|
@@ -6,10 +6,11 @@
|
|
|
6
6
|
|
|
7
7
|
import * as Context from 'effect/Context';
|
|
8
8
|
import * as Effect from 'effect/Effect';
|
|
9
|
+
import * as Layer from 'effect/Layer';
|
|
9
10
|
import * as Schema from 'effect/Schema';
|
|
10
11
|
|
|
11
12
|
import { ToolId } from '@dxos/ai';
|
|
12
|
-
import { Annotation, Database, Filter, Obj, Type } from '@dxos/echo';
|
|
13
|
+
import { Annotation, Database, Filter, Migration, Obj, Type } from '@dxos/echo';
|
|
13
14
|
import { BaseError } from '@dxos/errors';
|
|
14
15
|
import { log } from '@dxos/log';
|
|
15
16
|
|
|
@@ -21,18 +22,12 @@ import * as Template from './Template';
|
|
|
21
22
|
* Blueprint schema defines the structure for AI assistant blueprints.
|
|
22
23
|
* Blueprints contain instructions, tools, and artifacts that guide the AI's behavior.
|
|
23
24
|
* Blueprints may use tools to create and read artifacts, which are managed by the assistant.
|
|
25
|
+
*
|
|
26
|
+
* The registry `key` and `version` are stored in the object meta — access them via
|
|
27
|
+
* `Obj.getMeta(blueprint).key` and `Obj.getMeta(blueprint).version`.
|
|
24
28
|
*/
|
|
25
29
|
// TODO(burdon): Rename Skill?
|
|
26
30
|
export const Blueprint = Schema.Struct({
|
|
27
|
-
/**
|
|
28
|
-
* Global registry ID.
|
|
29
|
-
* NOTE: The `key` property refers to the original registry entry.
|
|
30
|
-
*/
|
|
31
|
-
// TODO(burdon): Create Format type for DXN-like ids, such as this and schema type.
|
|
32
|
-
key: Schema.String.annotations({
|
|
33
|
-
description: 'Unique registration key for the blueprint',
|
|
34
|
-
}),
|
|
35
|
-
|
|
36
31
|
/**
|
|
37
32
|
* Human-readable name of the blueprint.
|
|
38
33
|
*/
|
|
@@ -76,7 +71,7 @@ export const Blueprint = Schema.Struct({
|
|
|
76
71
|
}).pipe(
|
|
77
72
|
Type.object({
|
|
78
73
|
typename: 'org.dxos.type.blueprint',
|
|
79
|
-
version: '0.
|
|
74
|
+
version: '0.2.0',
|
|
80
75
|
}),
|
|
81
76
|
Annotation.LabelAnnotation.set(['name']),
|
|
82
77
|
Annotation.IconAnnotation.set({
|
|
@@ -90,18 +85,36 @@ export const Blueprint = Schema.Struct({
|
|
|
90
85
|
*/
|
|
91
86
|
export interface Blueprint extends Schema.Schema.Type<typeof Blueprint> {}
|
|
92
87
|
|
|
93
|
-
type MakeProps =
|
|
88
|
+
type MakeProps = { key: string; version?: string; name: string } & Partial<Blueprint>;
|
|
94
89
|
|
|
95
90
|
/**
|
|
96
91
|
* Create a new Blueprint.
|
|
92
|
+
* The `key` (and optional `version`) are stored in the object meta.
|
|
97
93
|
*/
|
|
98
|
-
export const make = ({ tools = [], instructions = Template.make(), ...props }: MakeProps) =>
|
|
94
|
+
export const make = ({ key, version, tools = [], instructions = Template.make(), ...props }: MakeProps) =>
|
|
99
95
|
Obj.make(Blueprint, {
|
|
96
|
+
[Obj.Meta]: { key, version },
|
|
100
97
|
tools,
|
|
101
98
|
instructions,
|
|
102
99
|
...props,
|
|
103
100
|
});
|
|
104
101
|
|
|
102
|
+
/**
|
|
103
|
+
* Get the registry key for a blueprint.
|
|
104
|
+
*/
|
|
105
|
+
export const getKey = (blueprint: Blueprint): string => {
|
|
106
|
+
const key = Obj.getMeta(blueprint).key;
|
|
107
|
+
if (key === undefined) {
|
|
108
|
+
throw new Error('Blueprint is missing the meta key.');
|
|
109
|
+
}
|
|
110
|
+
return key;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Get the registry version for a blueprint, if any.
|
|
115
|
+
*/
|
|
116
|
+
export const getVersion = (blueprint: Blueprint): string | undefined => Obj.getMeta(blueprint).version;
|
|
117
|
+
|
|
105
118
|
/**
|
|
106
119
|
* Util to create tool definitions for a blueprint.
|
|
107
120
|
*/
|
|
@@ -134,10 +147,11 @@ export class Registry {
|
|
|
134
147
|
constructor(blueprints: Blueprint[]) {
|
|
135
148
|
const seen = new Set<string>();
|
|
136
149
|
blueprints.forEach((blueprint) => {
|
|
137
|
-
|
|
138
|
-
|
|
150
|
+
const key = getKey(blueprint);
|
|
151
|
+
if (seen.has(key)) {
|
|
152
|
+
log.warn('duplicate blueprint', { key });
|
|
139
153
|
} else {
|
|
140
|
-
seen.add(
|
|
154
|
+
seen.add(key);
|
|
141
155
|
this._blueprints.push(blueprint);
|
|
142
156
|
}
|
|
143
157
|
});
|
|
@@ -150,7 +164,7 @@ export class Registry {
|
|
|
150
164
|
}
|
|
151
165
|
|
|
152
166
|
getByKey(key: string): Blueprint | undefined {
|
|
153
|
-
return this._blueprints.find((blueprint) => blueprint.key === key);
|
|
167
|
+
return this._blueprints.find((blueprint) => Obj.getMeta(blueprint).key === key);
|
|
154
168
|
}
|
|
155
169
|
|
|
156
170
|
query(): Blueprint[] {
|
|
@@ -161,7 +175,11 @@ export class Registry {
|
|
|
161
175
|
return Effect.gen(this, function* () {
|
|
162
176
|
const blueprints = yield* Database.runQuery(Filter.type(Blueprint));
|
|
163
177
|
for (const blueprint of blueprints) {
|
|
164
|
-
const
|
|
178
|
+
const blueprintKey = Obj.getMeta(blueprint).key;
|
|
179
|
+
if (!blueprintKey) {
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
const registryBlueprint = this.getByKey(blueprintKey);
|
|
165
183
|
if (!registryBlueprint) {
|
|
166
184
|
continue;
|
|
167
185
|
}
|
|
@@ -174,7 +192,22 @@ export class Registry {
|
|
|
174
192
|
}
|
|
175
193
|
}
|
|
176
194
|
|
|
177
|
-
export class RegistryService extends Context.Tag('@dxos/blueprints/RegistryService')<RegistryService, Registry>() {
|
|
195
|
+
export class RegistryService extends Context.Tag('@dxos/blueprints/RegistryService')<RegistryService, Registry>() {
|
|
196
|
+
static notAvailable = Layer.succeed(RegistryService, {
|
|
197
|
+
get blueprints(): Blueprint[] {
|
|
198
|
+
throw new Error('Blueprint registry not available');
|
|
199
|
+
},
|
|
200
|
+
getByKey(_key: string): Blueprint | undefined {
|
|
201
|
+
throw new Error('Blueprint registry not available');
|
|
202
|
+
},
|
|
203
|
+
query(): Blueprint[] {
|
|
204
|
+
throw new Error('Blueprint registry not available');
|
|
205
|
+
},
|
|
206
|
+
updateBlueprints() {
|
|
207
|
+
throw new Error('Blueprint registry not available');
|
|
208
|
+
},
|
|
209
|
+
} as unknown as Registry);
|
|
210
|
+
}
|
|
178
211
|
|
|
179
212
|
/**
|
|
180
213
|
* Resolves a blueprint from the registry.
|
|
@@ -197,7 +230,7 @@ export const resolve = (key: string): Effect.Effect<Blueprint, NotFoundError, Re
|
|
|
197
230
|
*/
|
|
198
231
|
export const upsert = (key: string): Effect.Effect<Blueprint, NotFoundError, RegistryService | Database.Service> =>
|
|
199
232
|
Effect.gen(function* () {
|
|
200
|
-
const local = yield* Database.runQuery(Filter.type(Blueprint,
|
|
233
|
+
const local = yield* Database.runQuery(Filter.and(Filter.type(Blueprint), Filter.key(key)));
|
|
201
234
|
if (local.length > 0) {
|
|
202
235
|
return local[0];
|
|
203
236
|
}
|
|
@@ -205,3 +238,66 @@ export const upsert = (key: string): Effect.Effect<Blueprint, NotFoundError, Reg
|
|
|
205
238
|
});
|
|
206
239
|
|
|
207
240
|
export class NotFoundError extends BaseError.extend('BlueprintNotFound', 'Blueprint not found') {}
|
|
241
|
+
|
|
242
|
+
//
|
|
243
|
+
// Legacy schemas and migrations.
|
|
244
|
+
//
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Blueprint schema v0.1.0 — `key` is stored as a data property.
|
|
248
|
+
* @deprecated Use {@link Blueprint} (v0.2.0) instead; the `key` and `version` now live in the object meta.
|
|
249
|
+
*/
|
|
250
|
+
export const Blueprint_v0_1_0 = Schema.Struct({
|
|
251
|
+
/**
|
|
252
|
+
* Global registry ID.
|
|
253
|
+
* NOTE: The `key` property refers to the original registry entry.
|
|
254
|
+
*/
|
|
255
|
+
key: Schema.String.annotations({
|
|
256
|
+
description: 'Unique registration key for the blueprint',
|
|
257
|
+
}),
|
|
258
|
+
|
|
259
|
+
name: Schema.String.annotations({
|
|
260
|
+
description: 'Human-readable name of the blueprint',
|
|
261
|
+
}),
|
|
262
|
+
|
|
263
|
+
description: Schema.optional(Schema.String),
|
|
264
|
+
|
|
265
|
+
instructions: Template.Template,
|
|
266
|
+
|
|
267
|
+
tools: Schema.Array(ToolId),
|
|
268
|
+
|
|
269
|
+
agentCanEnable: Schema.optional(Schema.Boolean),
|
|
270
|
+
|
|
271
|
+
mcpServers: Schema.optional(Schema.Array(McpServer.McpServer)),
|
|
272
|
+
}).pipe(
|
|
273
|
+
Type.object({
|
|
274
|
+
typename: 'org.dxos.type.blueprint',
|
|
275
|
+
version: '0.1.0',
|
|
276
|
+
}),
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
export interface Blueprint_v0_1_0 extends Schema.Schema.Type<typeof Blueprint_v0_1_0> {}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Migration from {@link Blueprint_v0_1_0} (v0.1.0) to {@link Blueprint} (v0.2.0).
|
|
283
|
+
* Moves `key` from the data section into the object meta.
|
|
284
|
+
*/
|
|
285
|
+
const _migration = Migration.define({
|
|
286
|
+
from: Blueprint_v0_1_0,
|
|
287
|
+
to: Blueprint,
|
|
288
|
+
transform: async (from) => ({
|
|
289
|
+
[Obj.Meta]: { key: from.key, version: '0.1.0' },
|
|
290
|
+
name: from.name,
|
|
291
|
+
description: from.description,
|
|
292
|
+
instructions: from.instructions,
|
|
293
|
+
tools: from.tools,
|
|
294
|
+
agentCanEnable: from.agentCanEnable,
|
|
295
|
+
mcpServers: from.mcpServers,
|
|
296
|
+
}),
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Schema migrations exported by this module.
|
|
301
|
+
* Exported as an array for extensibility — append future versions here.
|
|
302
|
+
*/
|
|
303
|
+
export const migrations = [_migration];
|
package/src/LayerSpec.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Context from 'effect/Context';
|
|
6
|
+
import * as Layer from 'effect/Layer';
|
|
7
|
+
import * as Types from 'effect/Types';
|
|
8
|
+
|
|
9
|
+
import { SpaceId, DXN } from '@dxos/keys';
|
|
10
|
+
|
|
11
|
+
import type * as Process from './Process';
|
|
12
|
+
|
|
13
|
+
// @import-as-namespace
|
|
14
|
+
|
|
15
|
+
export const TypeId = '~@dxos/functions/LayerSpec' as const;
|
|
16
|
+
export type TypeId = typeof TypeId;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Opaque layer with introspectable tags.
|
|
20
|
+
*/
|
|
21
|
+
export interface LayerSpec {
|
|
22
|
+
readonly [TypeId]: TypeId;
|
|
23
|
+
|
|
24
|
+
readonly affinity: Affinity;
|
|
25
|
+
readonly requires: readonly Context.Tag<any, any>[];
|
|
26
|
+
readonly provides: readonly Context.Tag<any, any>[];
|
|
27
|
+
|
|
28
|
+
// opaque service creation function, layer provides the specified services and requires the specified requirements.
|
|
29
|
+
readonly make: (context: LayerContext) => Layer.Layer<unknown, never, unknown>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Affinity defines the lifetime and scope of a service.
|
|
34
|
+
*
|
|
35
|
+
* - `application` - single service instance is available throughout the entire application.
|
|
36
|
+
* - `space` - service is instantiated per space.
|
|
37
|
+
* - `process` - separate service instance is created and destroyed for each process.
|
|
38
|
+
*/
|
|
39
|
+
export type Affinity = 'application' | 'space' | 'process';
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Provides context for service resolution.
|
|
43
|
+
*/
|
|
44
|
+
// TODO(dmaretskyi): Consider making this type dependant on the specified service affinity.
|
|
45
|
+
export interface LayerContext {
|
|
46
|
+
/**
|
|
47
|
+
* Under which space the process is running.
|
|
48
|
+
* Space affinity and up only.
|
|
49
|
+
*/
|
|
50
|
+
readonly space?: SpaceId;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* DXN of the conversation feed the process is running in.
|
|
54
|
+
* Process affinity only.
|
|
55
|
+
*/
|
|
56
|
+
readonly conversation?: DXN.String;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Under which process the process is running.
|
|
60
|
+
* Process affinity only.
|
|
61
|
+
*/
|
|
62
|
+
readonly process?: Process.ID;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface MakeOpts {
|
|
66
|
+
readonly affinity: Affinity;
|
|
67
|
+
readonly requires: readonly Context.Tag<any, any>[];
|
|
68
|
+
readonly provides: readonly Context.Tag<any, any>[];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Make a service.
|
|
73
|
+
*/
|
|
74
|
+
export const make = <const Opts extends Types.NoExcessProperties<MakeOpts, Opts>>(
|
|
75
|
+
opts: Opts,
|
|
76
|
+
make: (
|
|
77
|
+
context: LayerContext,
|
|
78
|
+
) => Layer.Layer<
|
|
79
|
+
Context.Tag.Identifier<Opts['provides'][number]>,
|
|
80
|
+
never,
|
|
81
|
+
Context.Tag.Identifier<Opts['requires'][number]>
|
|
82
|
+
>,
|
|
83
|
+
): LayerSpec => {
|
|
84
|
+
return {
|
|
85
|
+
[TypeId]: TypeId,
|
|
86
|
+
affinity: opts.affinity,
|
|
87
|
+
requires: opts.requires,
|
|
88
|
+
provides: opts.provides,
|
|
89
|
+
make: make as any,
|
|
90
|
+
};
|
|
91
|
+
};
|
package/src/Operation.ts
CHANGED
|
@@ -11,7 +11,7 @@ import * as Pipeable from 'effect/Pipeable';
|
|
|
11
11
|
import * as Schema$ from 'effect/Schema';
|
|
12
12
|
import type * as Types from 'effect/Types';
|
|
13
13
|
|
|
14
|
-
import { Annotation, JsonSchema, Obj, Ref, Type, type DXN, type Key } from '@dxos/echo';
|
|
14
|
+
import { Annotation, JsonSchema, Migration, Obj, Ref, Type, type DXN, type Key } from '@dxos/echo';
|
|
15
15
|
|
|
16
16
|
import type { NoHandlerError } from './errors';
|
|
17
17
|
import type { Operation } from './index';
|
|
@@ -293,21 +293,11 @@ export type InvokeRemote = <I, O, E>(
|
|
|
293
293
|
|
|
294
294
|
/**
|
|
295
295
|
* Database record of an operation.
|
|
296
|
+
* The registry `key` and `version` live in the object meta — access via
|
|
297
|
+
* `Obj.getMeta(record).key` and `Obj.getMeta(record).version`.
|
|
296
298
|
*/
|
|
297
299
|
export const PersistentOperation = Schema$.Struct({
|
|
298
|
-
/**
|
|
299
|
-
* Global registry ID.
|
|
300
|
-
* NOTE: The `key` property refers to the original registry entry.
|
|
301
|
-
*/
|
|
302
|
-
// TODO(burdon): Create Format type for DXN-like ids, such as this and schema type.
|
|
303
|
-
// TODO(dmaretskyi): Consider making it part of ECHO meta.
|
|
304
|
-
// TODO(dmaretskyi): Make required.
|
|
305
|
-
key: Schema$.optional(Schema$.String).annotations({
|
|
306
|
-
description: 'Unique registration key for the blueprint',
|
|
307
|
-
}),
|
|
308
|
-
|
|
309
300
|
name: Schema$.NonEmptyString,
|
|
310
|
-
version: Schema$.String,
|
|
311
301
|
|
|
312
302
|
description: Schema$.optional(Schema$.String),
|
|
313
303
|
|
|
@@ -337,7 +327,7 @@ export const PersistentOperation = Schema$.Struct({
|
|
|
337
327
|
// backward compatibility with existing data and avoid requiring data migration.
|
|
338
328
|
Type.object({
|
|
339
329
|
typename: 'org.dxos.type.function',
|
|
340
|
-
version: '0.
|
|
330
|
+
version: '0.2.0',
|
|
341
331
|
}),
|
|
342
332
|
Annotation.LabelAnnotation.set(['name']),
|
|
343
333
|
Annotation.IconAnnotation.set({ icon: 'ph--function--regular', hue: 'blue' }),
|
|
@@ -347,17 +337,27 @@ export interface PersistentOperation extends Schema$.Schema.Type<typeof Persiste
|
|
|
347
337
|
|
|
348
338
|
const FUNCTION_META_KEY = 'org.dxos.service.function';
|
|
349
339
|
|
|
340
|
+
/**
|
|
341
|
+
* Get the registry key for a persistent operation record (from the object meta).
|
|
342
|
+
*/
|
|
343
|
+
export const getKey = (record: PersistentOperation): string | undefined => Obj.getMeta(record).key;
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Get the registry version for a persistent operation record (from the object meta).
|
|
347
|
+
*/
|
|
348
|
+
export const getVersion = (record: PersistentOperation): string | undefined => Obj.getMeta(record).version;
|
|
349
|
+
|
|
350
350
|
/**
|
|
351
351
|
* Serialize an operation definition to a persistent operation record.
|
|
352
352
|
*/
|
|
353
353
|
export const serialize = (operation: Definition.Any): PersistentOperation => {
|
|
354
354
|
return Obj.make(PersistentOperation, {
|
|
355
355
|
[Obj.Meta]: {
|
|
356
|
+
key: operation.meta.key,
|
|
357
|
+
version: operation.meta.version ?? '0.0.0',
|
|
356
358
|
keys: operation.meta.deployedId ? [{ source: FUNCTION_META_KEY, id: operation.meta.deployedId }] : [],
|
|
357
359
|
},
|
|
358
|
-
key: operation.meta.key,
|
|
359
360
|
name: operation.meta.name ?? '',
|
|
360
|
-
version: operation.meta.version ?? '0.0.0',
|
|
361
361
|
description: operation.meta.description,
|
|
362
362
|
updated: undefined,
|
|
363
363
|
source: undefined,
|
|
@@ -371,8 +371,9 @@ export const serialize = (operation: Definition.Any): PersistentOperation => {
|
|
|
371
371
|
* Deserialize a persistent operation record to an operation definition.
|
|
372
372
|
*/
|
|
373
373
|
export const deserialize = (record: PersistentOperation): Definition.Any => {
|
|
374
|
+
const meta = Obj.getMeta(record);
|
|
374
375
|
// Extract deployed function ID from ECHO meta keys (matches FUNCTIONS_META_KEY in @dxos/functions).
|
|
375
|
-
const deployedId =
|
|
376
|
+
const deployedId = meta.keys.find((key) => key.source === FUNCTION_META_KEY)?.id;
|
|
376
377
|
return make({
|
|
377
378
|
input: record.inputSchema ? JsonSchema.toEffectSchema(record.inputSchema) : Schema$.Unknown,
|
|
378
379
|
output: record.outputSchema ? JsonSchema.toEffectSchema(record.outputSchema) : Schema$.Unknown,
|
|
@@ -380,9 +381,9 @@ export const deserialize = (record: PersistentOperation): Definition.Any => {
|
|
|
380
381
|
executionMode: 'async',
|
|
381
382
|
types: [],
|
|
382
383
|
meta: {
|
|
383
|
-
key:
|
|
384
|
+
key: meta.key ?? record.name,
|
|
384
385
|
name: record.name,
|
|
385
|
-
version:
|
|
386
|
+
version: meta.version ?? '0.0.0',
|
|
386
387
|
description: record.description,
|
|
387
388
|
deployedId,
|
|
388
389
|
},
|
|
@@ -394,15 +395,21 @@ export const deserialize = (record: PersistentOperation): Definition.Any => {
|
|
|
394
395
|
*/
|
|
395
396
|
export const setFrom = (target: PersistentOperation, source: PersistentOperation) => {
|
|
396
397
|
Obj.update(target, (target) => {
|
|
397
|
-
target.key = source.key ?? target.key;
|
|
398
398
|
target.name = source.name ?? target.name;
|
|
399
|
-
target.version = source.version;
|
|
400
399
|
target.description = source.description;
|
|
401
400
|
target.updated = source.updated;
|
|
402
401
|
// TODO(dmaretskyi): A workaround for an ECHO bug.
|
|
403
402
|
target.inputSchema = source.inputSchema ? JSON.parse(JSON.stringify(source.inputSchema)) : undefined;
|
|
404
403
|
target.outputSchema = source.outputSchema ? JSON.parse(JSON.stringify(source.outputSchema)) : undefined;
|
|
405
|
-
|
|
404
|
+
const sourceMeta = Obj.getMeta(source);
|
|
405
|
+
const targetMeta = Obj.getMeta(target);
|
|
406
|
+
targetMeta.key = sourceMeta.key ?? targetMeta.key;
|
|
407
|
+
targetMeta.version = sourceMeta.version;
|
|
408
|
+
// Only overwrite foreign keys when the source actually carries them; otherwise we'd wipe
|
|
409
|
+
// bindings (e.g. the deployedId set by `deserialize`) on records produced by `serialize`.
|
|
410
|
+
if (sourceMeta.keys.length > 0) {
|
|
411
|
+
targetMeta.keys = JSON.parse(JSON.stringify(sourceMeta.keys));
|
|
412
|
+
}
|
|
406
413
|
});
|
|
407
414
|
};
|
|
408
415
|
|
|
@@ -416,7 +423,7 @@ export interface InvokeOptions {
|
|
|
416
423
|
spaceId?: Key.SpaceId;
|
|
417
424
|
/**
|
|
418
425
|
* DXN string of the conversation feed (queue).
|
|
419
|
-
* Passed to the process environment so nested operations can resolve
|
|
426
|
+
* Passed to the process environment so nested operations can resolve AiContext.Service and related services.
|
|
420
427
|
*/
|
|
421
428
|
conversation?: DXN.String;
|
|
422
429
|
/**
|
|
@@ -531,3 +538,56 @@ export const withInvocationOptions = (options: InvokeOptions): Layer.Layer<Servi
|
|
|
531
538
|
});
|
|
532
539
|
}),
|
|
533
540
|
);
|
|
541
|
+
|
|
542
|
+
//
|
|
543
|
+
// Legacy schemas and migrations.
|
|
544
|
+
//
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* Persistent operation schema v0.1.0 — `key` and `version` are stored as data properties.
|
|
548
|
+
* @deprecated Use {@link PersistentOperation} (v0.2.0) instead; the `key` and `version` now live in the object meta.
|
|
549
|
+
*/
|
|
550
|
+
export const PersistentOperation_v0_1_0 = Schema$.Struct({
|
|
551
|
+
key: Schema$.optional(Schema$.String),
|
|
552
|
+
name: Schema$.NonEmptyString,
|
|
553
|
+
version: Schema$.String,
|
|
554
|
+
description: Schema$.optional(Schema$.String),
|
|
555
|
+
updated: Schema$.optional(Schema$.String),
|
|
556
|
+
source: Schema$.optional(Ref.Ref(Obj.Unknown)),
|
|
557
|
+
inputSchema: Schema$.optional(JsonSchema.JsonSchema),
|
|
558
|
+
outputSchema: Schema$.optional(JsonSchema.JsonSchema),
|
|
559
|
+
services: Schema$.optional(Schema$.Array(Schema$.String)),
|
|
560
|
+
binding: Schema$.optional(Schema$.String),
|
|
561
|
+
}).pipe(
|
|
562
|
+
Type.object({
|
|
563
|
+
typename: 'org.dxos.type.function',
|
|
564
|
+
version: '0.1.0',
|
|
565
|
+
}),
|
|
566
|
+
);
|
|
567
|
+
export interface PersistentOperation_v0_1_0 extends Schema$.Schema.Type<typeof PersistentOperation_v0_1_0> {}
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* Migration from {@link PersistentOperation_v0_1_0} (v0.1.0) to {@link PersistentOperation} (v0.2.0).
|
|
571
|
+
* Moves `key` and `version` from the data section into the object meta.
|
|
572
|
+
*/
|
|
573
|
+
const _migration = Migration.define({
|
|
574
|
+
from: PersistentOperation_v0_1_0,
|
|
575
|
+
to: PersistentOperation,
|
|
576
|
+
transform: async (from) => ({
|
|
577
|
+
[Obj.Meta]: { key: from.key, version: from.version },
|
|
578
|
+
name: from.name,
|
|
579
|
+
description: from.description,
|
|
580
|
+
updated: from.updated,
|
|
581
|
+
source: from.source as any,
|
|
582
|
+
inputSchema: from.inputSchema,
|
|
583
|
+
outputSchema: from.outputSchema,
|
|
584
|
+
services: from.services,
|
|
585
|
+
binding: from.binding,
|
|
586
|
+
}),
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Schema migrations exported by this module.
|
|
591
|
+
* Exported as an array for extensibility — append future versions here.
|
|
592
|
+
*/
|
|
593
|
+
export const migrations = [_migration];
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Atom, Registry } from '@effect-atom/atom';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Schema from 'effect/Schema';
|
|
8
|
+
import { describe, test } from 'vitest';
|
|
9
|
+
|
|
10
|
+
import * as Operation from './Operation';
|
|
11
|
+
import * as OperationHandlerSet from './OperationHandlerSet';
|
|
12
|
+
|
|
13
|
+
const makeHandler = (key: string, output: string) =>
|
|
14
|
+
Operation.withHandler(Operation.make({ input: Schema.Void, output: Schema.String, meta: { key } }), () =>
|
|
15
|
+
Effect.succeed(output),
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
describe('OperationHandlerSet.reactive', () => {
|
|
19
|
+
test('merges handlers from contributed sets', async ({ expect }) => {
|
|
20
|
+
const registry = Registry.make();
|
|
21
|
+
const setA = OperationHandlerSet.make(makeHandler('a', 'A'));
|
|
22
|
+
const setB = OperationHandlerSet.make(makeHandler('b', 'B'));
|
|
23
|
+
const atom = Atom.make<readonly OperationHandlerSet.OperationHandlerSet[]>([setA, setB]).pipe(Atom.keepAlive);
|
|
24
|
+
registry.mount(atom);
|
|
25
|
+
|
|
26
|
+
const reactive = OperationHandlerSet.reactive(registry, atom);
|
|
27
|
+
const handlers = await reactive.getHandlers();
|
|
28
|
+
expect(handlers.map((handler) => handler.meta.key).sort()).toEqual(['a', 'b']);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('caches the merged result across calls', async ({ expect }) => {
|
|
32
|
+
const registry = Registry.make();
|
|
33
|
+
let resolveCount = 0;
|
|
34
|
+
const trackingSet: OperationHandlerSet.OperationHandlerSet = {
|
|
35
|
+
[OperationHandlerSet.TypeId]: OperationHandlerSet.TypeId,
|
|
36
|
+
getHandlers: () => Promise.resolve([makeHandler('a', 'A')]),
|
|
37
|
+
handlers: Effect.sync(() => {
|
|
38
|
+
resolveCount++;
|
|
39
|
+
return [makeHandler('a', 'A')];
|
|
40
|
+
}),
|
|
41
|
+
};
|
|
42
|
+
const atom = Atom.make<readonly OperationHandlerSet.OperationHandlerSet[]>([trackingSet]).pipe(Atom.keepAlive);
|
|
43
|
+
registry.mount(atom);
|
|
44
|
+
|
|
45
|
+
const reactive = OperationHandlerSet.reactive(registry, atom);
|
|
46
|
+
await reactive.getHandlers();
|
|
47
|
+
await reactive.getHandlers();
|
|
48
|
+
await reactive.getHandlers();
|
|
49
|
+
expect(resolveCount).toBe(1);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('retries after a rejection rather than caching the failure', async ({ expect }) => {
|
|
53
|
+
const registry = Registry.make();
|
|
54
|
+
let callCount = 0;
|
|
55
|
+
const flakySet: OperationHandlerSet.OperationHandlerSet = {
|
|
56
|
+
[OperationHandlerSet.TypeId]: OperationHandlerSet.TypeId,
|
|
57
|
+
getHandlers: () => Promise.resolve([]),
|
|
58
|
+
handlers: Effect.suspend(() => {
|
|
59
|
+
callCount++;
|
|
60
|
+
if (callCount === 1) {
|
|
61
|
+
return Effect.promise(() => Promise.reject(new Error('transient')));
|
|
62
|
+
}
|
|
63
|
+
return Effect.succeed([makeHandler('a', 'A')]);
|
|
64
|
+
}),
|
|
65
|
+
};
|
|
66
|
+
const atom = Atom.make<readonly OperationHandlerSet.OperationHandlerSet[]>([flakySet]).pipe(Atom.keepAlive);
|
|
67
|
+
registry.mount(atom);
|
|
68
|
+
|
|
69
|
+
const reactive = OperationHandlerSet.reactive(registry, atom);
|
|
70
|
+
await expect(reactive.getHandlers()).rejects.toThrow('transient');
|
|
71
|
+
const retried = await reactive.getHandlers();
|
|
72
|
+
expect(retried.map((handler) => handler.meta.key)).toEqual(['a']);
|
|
73
|
+
expect(callCount).toBe(2);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('invalidates and re-resolves when the atom changes', async ({ expect }) => {
|
|
77
|
+
const registry = Registry.make();
|
|
78
|
+
const setA = OperationHandlerSet.make(makeHandler('a', 'A'));
|
|
79
|
+
const setB = OperationHandlerSet.make(makeHandler('b', 'B'));
|
|
80
|
+
const atom = Atom.make<readonly OperationHandlerSet.OperationHandlerSet[]>([setA]).pipe(Atom.keepAlive);
|
|
81
|
+
registry.mount(atom);
|
|
82
|
+
|
|
83
|
+
const reactive = OperationHandlerSet.reactive(registry, atom);
|
|
84
|
+
expect((await reactive.getHandlers()).map((handler) => handler.meta.key)).toEqual(['a']);
|
|
85
|
+
|
|
86
|
+
registry.set(atom, [setA, setB]);
|
|
87
|
+
expect((await reactive.getHandlers()).map((handler) => handler.meta.key).sort()).toEqual(['a', 'b']);
|
|
88
|
+
|
|
89
|
+
registry.set(atom, []);
|
|
90
|
+
expect(await reactive.getHandlers()).toEqual([]);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
@@ -4,10 +4,13 @@
|
|
|
4
4
|
|
|
5
5
|
// @import-as-namespace
|
|
6
6
|
|
|
7
|
+
import { type Atom, type Registry } from '@effect-atom/atom';
|
|
7
8
|
import * as Context from 'effect/Context';
|
|
8
9
|
import * as Effect from 'effect/Effect';
|
|
10
|
+
import { pipe } from 'effect/Function';
|
|
9
11
|
import * as Layer from 'effect/Layer';
|
|
10
12
|
|
|
13
|
+
import { runAndForwardErrors } from '@dxos/effect';
|
|
11
14
|
import { assertArgument } from '@dxos/invariant';
|
|
12
15
|
|
|
13
16
|
import { NoHandlerError } from './errors';
|
|
@@ -67,6 +70,43 @@ export const async = (
|
|
|
67
70
|
};
|
|
68
71
|
};
|
|
69
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Builds a set backed by an atom of contributed sets. The merged result is
|
|
75
|
+
* cached and invalidated whenever the atom changes, so most accesses are
|
|
76
|
+
* cheap but newly registered handlers are picked up.
|
|
77
|
+
*/
|
|
78
|
+
export const reactive = (
|
|
79
|
+
registry: Registry.Registry,
|
|
80
|
+
atom: Atom.Atom<readonly OperationHandlerSet[]>,
|
|
81
|
+
): OperationHandlerSet => {
|
|
82
|
+
let cached: Promise<Operation.WithHandler<Operation.Definition.Any>[]> | null = null;
|
|
83
|
+
registry.subscribe(atom, () => {
|
|
84
|
+
cached = null;
|
|
85
|
+
});
|
|
86
|
+
// `suspend` defers `registry.get(atom)` until each run, so re-evaluations
|
|
87
|
+
// after cache invalidation see the current contributed sets.
|
|
88
|
+
const compute = Effect.suspend(() =>
|
|
89
|
+
pipe(
|
|
90
|
+
registry.get(atom),
|
|
91
|
+
Effect.forEach((set) => set.handlers, { concurrency: 'unbounded' }),
|
|
92
|
+
Effect.map((groups) => groups.flat()),
|
|
93
|
+
// Reset cached on failure so a transient error doesn't permanently
|
|
94
|
+
// poison subsequent calls.
|
|
95
|
+
Effect.tapErrorCause(() =>
|
|
96
|
+
Effect.sync(() => {
|
|
97
|
+
cached = null;
|
|
98
|
+
}),
|
|
99
|
+
),
|
|
100
|
+
),
|
|
101
|
+
);
|
|
102
|
+
const getHandlers = () => (cached ??= runAndForwardErrors(compute));
|
|
103
|
+
return {
|
|
104
|
+
[TypeId]: TypeId,
|
|
105
|
+
getHandlers,
|
|
106
|
+
handlers: Effect.promise(getHandlers),
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
|
|
70
110
|
/**
|
|
71
111
|
* Merges multiple operation handler sets into a single set.
|
|
72
112
|
*
|