@dxos/compute 0.8.4-main.fcfe5033a5 → 0.8.4-staging.60fe92afc8
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/README.md +36 -1
- package/dist/lib/neutral/StorageService.mjs +22 -0
- package/dist/lib/neutral/StorageService.mjs.map +7 -0
- package/dist/lib/neutral/chunk-2B2KSJT6.mjs +17 -0
- package/dist/lib/neutral/chunk-2B2KSJT6.mjs.map +7 -0
- package/dist/lib/neutral/chunk-AFBNVFAY.mjs +25 -0
- package/dist/lib/neutral/chunk-AFBNVFAY.mjs.map +7 -0
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/neutral/chunk-JAU4E4EK.mjs +53 -0
- package/dist/lib/neutral/chunk-JAU4E4EK.mjs.map +7 -0
- package/dist/lib/neutral/chunk-MEPE4HHX.mjs +21 -0
- package/dist/lib/neutral/chunk-MEPE4HHX.mjs.map +7 -0
- package/dist/lib/neutral/chunk-QZS4G6PE.mjs +240 -0
- package/dist/lib/neutral/chunk-QZS4G6PE.mjs.map +7 -0
- package/dist/lib/neutral/chunk-XEVFBI6S.mjs +62 -0
- package/dist/lib/neutral/chunk-XEVFBI6S.mjs.map +7 -0
- package/dist/lib/neutral/chunk-Z3Y7FQGN.mjs +174 -0
- package/dist/lib/neutral/chunk-Z3Y7FQGN.mjs.map +7 -0
- package/dist/lib/neutral/fib-HBDUGQW7.mjs +10 -0
- package/dist/lib/neutral/fib-HBDUGQW7.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +1179 -51
- package/dist/lib/neutral/index.mjs.map +4 -4
- package/dist/lib/neutral/meta.json +1 -1
- package/dist/lib/neutral/reply-RJIAJ6FV.mjs +10 -0
- package/dist/lib/neutral/reply-RJIAJ6FV.mjs.map +7 -0
- package/dist/lib/neutral/sleep-3T2CZ7RE.mjs +10 -0
- package/dist/lib/neutral/sleep-3T2CZ7RE.mjs.map +7 -0
- package/dist/lib/neutral/testing/index.mjs +27 -214
- package/dist/lib/neutral/testing/index.mjs.map +4 -4
- package/dist/types/src/Blueprint.d.ts +182 -0
- package/dist/types/src/Blueprint.d.ts.map +1 -0
- package/dist/types/src/Credential.d.ts +27 -0
- package/dist/types/src/Credential.d.ts.map +1 -0
- package/dist/types/src/Header.d.ts +9 -0
- package/dist/types/src/Header.d.ts.map +1 -0
- package/dist/types/src/Header.test.d.ts +2 -0
- package/dist/types/src/Header.test.d.ts.map +1 -0
- package/dist/types/src/LayerSpec.d.ts +56 -0
- package/dist/types/src/LayerSpec.d.ts.map +1 -0
- package/dist/types/src/McpServer.d.ts +26 -0
- package/dist/types/src/McpServer.d.ts.map +1 -0
- package/dist/types/src/Operation.d.ts +385 -0
- package/dist/types/src/Operation.d.ts.map +1 -0
- package/dist/types/src/OperationHandlerSet.d.ts +69 -0
- package/dist/types/src/OperationHandlerSet.d.ts.map +1 -0
- 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 +23 -0
- package/dist/types/src/OperationRegistry.d.ts.map +1 -0
- package/dist/types/src/Process.d.ts +255 -0
- package/dist/types/src/Process.d.ts.map +1 -0
- package/dist/types/src/Routine.d.ts +54 -0
- package/dist/types/src/Routine.d.ts.map +1 -0
- package/dist/types/src/Script.d.ts +21 -0
- package/dist/types/src/Script.d.ts.map +1 -0
- package/dist/types/src/ServiceResolver.d.ts +92 -0
- package/dist/types/src/ServiceResolver.d.ts.map +1 -0
- package/dist/types/src/StorageService.d.ts +58 -0
- package/dist/types/src/StorageService.d.ts.map +1 -0
- package/dist/types/src/Template.d.ts +61 -0
- package/dist/types/src/Template.d.ts.map +1 -0
- package/dist/types/src/Template.test.d.ts +2 -0
- package/dist/types/src/Template.test.d.ts.map +1 -0
- package/dist/types/src/Trace.d.ts +266 -0
- package/dist/types/src/Trace.d.ts.map +1 -0
- package/dist/types/src/Trigger.d.ts +192 -0
- package/dist/types/src/Trigger.d.ts.map +1 -0
- package/dist/types/src/TriggerEvent.d.ts +83 -0
- package/dist/types/src/TriggerEvent.d.ts.map +1 -0
- package/dist/types/src/errors.d.ts +308 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +18 -5
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/testing/definitions.d.ts +11 -0
- package/dist/types/src/testing/definitions.d.ts.map +1 -0
- package/dist/types/src/testing/fib.d.ts +8 -0
- package/dist/types/src/testing/fib.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +6 -4
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/reply.d.ts +4 -0
- package/dist/types/src/testing/reply.d.ts.map +1 -0
- package/dist/types/src/testing/sleep.d.ts +6 -0
- package/dist/types/src/testing/sleep.d.ts.map +1 -0
- package/dist/types/src/umbrella.test.d.ts +2 -0
- package/dist/types/src/umbrella.test.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +25 -36
- package/src/Blueprint.ts +224 -0
- package/src/Credential.ts +53 -0
- package/src/Header.test.ts +132 -0
- package/src/Header.ts +38 -0
- package/src/LayerSpec.ts +91 -0
- package/src/McpServer.ts +44 -0
- package/src/Operation.ts +646 -0
- package/src/OperationHandlerSet.test.ts +97 -0
- package/src/OperationHandlerSet.ts +181 -0
- package/src/OperationRegistry.ts +50 -0
- package/src/Process.ts +541 -0
- package/src/Routine.ts +68 -0
- package/src/Script.ts +33 -0
- package/src/ServiceResolver.ts +200 -0
- package/src/StorageService.ts +101 -0
- package/src/Template.test.ts +164 -0
- package/src/Template.ts +125 -0
- package/src/Trace.ts +363 -0
- package/src/Trigger.ts +196 -0
- package/src/TriggerEvent.ts +64 -0
- package/src/errors.ts +71 -0
- package/src/index.ts +20 -6
- package/src/testing/definitions.ts +51 -0
- package/src/testing/fib.ts +22 -0
- package/src/testing/index.ts +13 -5
- package/src/testing/reply.ts +18 -0
- package/src/testing/sleep.ts +16 -0
- package/src/umbrella.test.ts +51 -0
- package/dist/lib/neutral/chunk-JRV42MW3.mjs +0 -3101
- package/dist/lib/neutral/chunk-JRV42MW3.mjs.map +0 -7
- package/dist/types/src/compute-graph-registry.d.ts +0 -37
- package/dist/types/src/compute-graph-registry.d.ts.map +0 -1
- package/dist/types/src/compute-graph-registry.test.d.ts +0 -2
- package/dist/types/src/compute-graph-registry.test.d.ts.map +0 -1
- package/dist/types/src/compute-graph.d.ts +0 -67
- package/dist/types/src/compute-graph.d.ts.map +0 -1
- package/dist/types/src/compute-graph.test.d.ts +0 -2
- package/dist/types/src/compute-graph.test.d.ts.map +0 -1
- package/dist/types/src/compute-node.d.ts +0 -24
- package/dist/types/src/compute-node.d.ts.map +0 -1
- package/dist/types/src/functions/async-function.d.ts +0 -68
- package/dist/types/src/functions/async-function.d.ts.map +0 -1
- package/dist/types/src/functions/edge-function.d.ts +0 -20
- package/dist/types/src/functions/edge-function.d.ts.map +0 -1
- package/dist/types/src/functions/function-defs.d.ts +0 -11
- package/dist/types/src/functions/function-defs.d.ts.map +0 -1
- package/dist/types/src/functions/index.d.ts +0 -4
- package/dist/types/src/functions/index.d.ts.map +0 -1
- package/dist/types/src/hyperformula.test.d.ts +0 -2
- package/dist/types/src/hyperformula.test.d.ts.map +0 -1
- package/dist/types/src/testing/test-builder.d.ts +0 -15
- package/dist/types/src/testing/test-builder.d.ts.map +0 -1
- package/dist/types/src/testing/test-plugin.d.ts +0 -35
- package/dist/types/src/testing/test-plugin.d.ts.map +0 -1
- package/dist/types/src/testing/test-runtime.d.ts +0 -10
- package/dist/types/src/testing/test-runtime.d.ts.map +0 -1
- package/dist/types/src/testing/util.d.ts +0 -2
- package/dist/types/src/testing/util.d.ts.map +0 -1
- package/dist/types/src/types/hyperformula.d.ts +0 -8
- package/dist/types/src/types/hyperformula.d.ts.map +0 -1
- package/dist/types/src/types/index.d.ts +0 -3
- package/dist/types/src/types/index.d.ts.map +0 -1
- package/dist/types/src/types/types.d.ts +0 -20
- package/dist/types/src/types/types.d.ts.map +0 -1
- package/dist/types/src/types/types.test.d.ts +0 -2
- package/dist/types/src/types/types.test.d.ts.map +0 -1
- package/dist/types/src/util.d.ts +0 -2
- package/dist/types/src/util.d.ts.map +0 -1
- package/src/compute-graph-registry.test.ts +0 -167
- package/src/compute-graph-registry.ts +0 -97
- package/src/compute-graph.test.ts +0 -139
- package/src/compute-graph.ts +0 -265
- package/src/compute-node.ts +0 -59
- package/src/functions/async-function.ts +0 -184
- package/src/functions/edge-function.ts +0 -108
- package/src/functions/function-defs.ts +0 -2427
- package/src/functions/index.ts +0 -7
- package/src/hyperformula.test.ts +0 -14
- package/src/testing/test-builder.ts +0 -59
- package/src/testing/test-plugin.ts +0 -98
- package/src/testing/test-runtime.ts +0 -53
- package/src/testing/util.ts +0 -9
- package/src/types/hyperformula.ts +0 -19
- package/src/types/index.ts +0 -6
- package/src/types/types.test.ts +0 -91
- package/src/types/types.ts +0 -80
- package/src/util.ts +0 -8
package/src/Operation.ts
ADDED
|
@@ -0,0 +1,646 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
// @import-as-namespace
|
|
6
|
+
|
|
7
|
+
import * as Context from 'effect/Context';
|
|
8
|
+
import * as Effect from 'effect/Effect';
|
|
9
|
+
import * as Layer from 'effect/Layer';
|
|
10
|
+
import * as Option from 'effect/Option';
|
|
11
|
+
import * as Pipeable from 'effect/Pipeable';
|
|
12
|
+
import * as Schema$ from 'effect/Schema';
|
|
13
|
+
import type * as Types from 'effect/Types';
|
|
14
|
+
|
|
15
|
+
import { DXN, Annotation, JsonSchema, Migration, Obj, Ref, Type, type Key } from '@dxos/echo';
|
|
16
|
+
import type { URI } from '@dxos/keys';
|
|
17
|
+
|
|
18
|
+
import type { NoHandlerError } from './errors';
|
|
19
|
+
import type { Operation } from './index';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Schema type that accepts any Encoded form but requires no Context.
|
|
23
|
+
* This allows ECHO object schemas where Type !== Encoded due to [KindId] symbol.
|
|
24
|
+
*/
|
|
25
|
+
type Schema<T> = Schema$.Schema<T, any, never>;
|
|
26
|
+
|
|
27
|
+
export const DefinitionTypeId = '~@dxos/operation/OperationDefinition' as const;
|
|
28
|
+
export type DefinitionTypeId = typeof DefinitionTypeId;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Serializable definition of an Operation.
|
|
32
|
+
* Contains schema and metadata, but no runtime logic.
|
|
33
|
+
*/
|
|
34
|
+
export interface Definition<I, O, S = any> extends Pipeable.Pipeable, Definition.Variance<I, O, S> {
|
|
35
|
+
/**
|
|
36
|
+
* Input schema for the operation.
|
|
37
|
+
*/
|
|
38
|
+
readonly input: Schema<I>;
|
|
39
|
+
/**
|
|
40
|
+
* Output schema for the operation.
|
|
41
|
+
*/
|
|
42
|
+
readonly output: Schema<O>;
|
|
43
|
+
|
|
44
|
+
readonly meta: {
|
|
45
|
+
readonly key: DXN.DXN;
|
|
46
|
+
readonly name?: string;
|
|
47
|
+
readonly version?: string;
|
|
48
|
+
readonly description?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Phosphor icon identifier in `ph--<name>--<variant>` format (e.g. `ph--file-text--regular`).
|
|
51
|
+
*/
|
|
52
|
+
readonly icon?: string;
|
|
53
|
+
/**
|
|
54
|
+
* Deployment ID for remote invocation.
|
|
55
|
+
* Assigned by the EDGE function service when deployed.
|
|
56
|
+
*/
|
|
57
|
+
readonly deployedId?: string;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Dictionary of annotations for the operation.
|
|
61
|
+
*/
|
|
62
|
+
// TODO(dmaretskyi): Make required, but this complicates `make` to fill in defaults.
|
|
63
|
+
readonly annotations?: Annotation.Dictionary;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* When true, this operation is not serialized into the hypergraph registry.
|
|
67
|
+
* Use for UI-only operations that should not be discoverable by the AI agent.
|
|
68
|
+
*/
|
|
69
|
+
readonly skipRegistry?: boolean;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Execution mode for the operation.
|
|
74
|
+
* - 'sync': Operation completes synchronously (fast, UI-blocking acceptable).
|
|
75
|
+
* - 'async': Operation may take time (should not block UI).
|
|
76
|
+
*/
|
|
77
|
+
readonly executionMode: 'sync' | 'async';
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* ECHO types the operation uses.
|
|
81
|
+
* Ensures types are available when the operation is executed remotely.
|
|
82
|
+
*/
|
|
83
|
+
readonly types: readonly Type.AnyEntity[];
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Effect services required by this operation.
|
|
87
|
+
* These services will be automatically provided to the handler at invocation time.
|
|
88
|
+
*/
|
|
89
|
+
readonly services: readonly Context.Tag<any, any>[];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Namespace for OperationDefinition helper types.
|
|
94
|
+
*/
|
|
95
|
+
export declare namespace Definition {
|
|
96
|
+
export interface Variance<I, O, S> {
|
|
97
|
+
[DefinitionTypeId]: {
|
|
98
|
+
readonly _Input: Types.Contravariant<I>;
|
|
99
|
+
readonly _Output: Types.Covariant<O>;
|
|
100
|
+
readonly _Services: Types.Covariant<S>;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Any operation definition, regardless of input/output types.
|
|
106
|
+
*/
|
|
107
|
+
export type Any = Definition<any, any, any>;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Extract the input type from an operation definition.
|
|
111
|
+
*/
|
|
112
|
+
export type Input<T extends Any> = T extends Variance<infer I, infer _O, infer _S> ? I : never;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Extract the output type from an operation definition.
|
|
116
|
+
*/
|
|
117
|
+
export type Output<T extends Any> = T extends Variance<infer _I, infer O, infer _S> ? O : never;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Extract the service identifier types from an operation's services array.
|
|
121
|
+
* Returns `never` if the operation has no services declared.
|
|
122
|
+
*/
|
|
123
|
+
export type Services<T extends Any> = T extends Variance<infer _I, infer _O, infer S> ? S : never;
|
|
124
|
+
|
|
125
|
+
export type HandlerType<T extends Any> =
|
|
126
|
+
T extends Variance<infer I, infer O, infer S> ? Handler<I, O, any, S> : never;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Runtime handler for an Operation.
|
|
131
|
+
*/
|
|
132
|
+
export type Handler<I, O, E = Error, R = never> = (input: I) => Effect.Effect<O, E, R>;
|
|
133
|
+
|
|
134
|
+
export type WithHandler<T extends Definition.Any> = T & {
|
|
135
|
+
handler: Definition.HandlerType<T>;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Checks if a value is an operation definition.
|
|
140
|
+
*/
|
|
141
|
+
export const isOperationDefinition = (value: unknown): value is Definition.Any => {
|
|
142
|
+
return typeof value === 'object' && value !== null && DefinitionTypeId in value;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Checks if a value is an operation with a handler.
|
|
147
|
+
*/
|
|
148
|
+
export const isOperationWithHandler = (value: unknown): value is WithHandler<Definition.Any> => {
|
|
149
|
+
return isOperationDefinition(value) && 'handler' in value;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Props for creating an Operation definition.
|
|
154
|
+
* Derived from OperationDefinition with executionMode made optional (defaults to 'async').
|
|
155
|
+
*/
|
|
156
|
+
export type Props<I, O> = Omit<Definition<I, O>, DefinitionTypeId | 'pipe' | 'executionMode' | 'types' | 'services'> & {
|
|
157
|
+
readonly executionMode?: 'sync' | 'async';
|
|
158
|
+
readonly types?: Definition<I, O>['types'];
|
|
159
|
+
readonly services?: Definition<I, O>['services'];
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Creates a new Operation definition.
|
|
164
|
+
* Applies default executionMode of 'async' if not specified.
|
|
165
|
+
* The returned type preserves the literal types of props (including services).
|
|
166
|
+
*/
|
|
167
|
+
export const make = <const P extends Types.NoExcessProperties<Props<any, any>, P>>(
|
|
168
|
+
props: P,
|
|
169
|
+
): Definition<
|
|
170
|
+
Schema$.Schema.Type<P['input']>,
|
|
171
|
+
Schema$.Schema.Type<P['output']>,
|
|
172
|
+
Context.Tag.Identifier<NonNullable<P['services']>[number]>
|
|
173
|
+
> => {
|
|
174
|
+
return {
|
|
175
|
+
[DefinitionTypeId]: {},
|
|
176
|
+
...props,
|
|
177
|
+
executionMode: props.executionMode ?? 'async',
|
|
178
|
+
types: props.types ?? [],
|
|
179
|
+
services: props.services ?? [],
|
|
180
|
+
pipe() {
|
|
181
|
+
// eslint-disable-next-line prefer-rest-params
|
|
182
|
+
return Pipeable.pipeArguments(this, arguments);
|
|
183
|
+
},
|
|
184
|
+
} as any;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Attaches a handler to an Operation definition.
|
|
189
|
+
* The handler may use any services declared in the operation, plus Operation.Service (always available).
|
|
190
|
+
* Dual API: can be called directly or used in a pipe.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* const MyOp = Operation.make({
|
|
195
|
+
* input: Schema.Void,
|
|
196
|
+
* output: Schema.Void,
|
|
197
|
+
* meta: { key: 'my-op' },
|
|
198
|
+
* services: [DatabaseService],
|
|
199
|
+
* });
|
|
200
|
+
*
|
|
201
|
+
* // Direct call - handler can use DatabaseService
|
|
202
|
+
* const op = Operation.withHandler(MyOp, (input) =>
|
|
203
|
+
* Effect.gen(function* () {
|
|
204
|
+
* const db = yield* DatabaseService;
|
|
205
|
+
* return {};
|
|
206
|
+
* }),
|
|
207
|
+
* );
|
|
208
|
+
*
|
|
209
|
+
* // Piped call
|
|
210
|
+
* const op = MyOp.pipe(Operation.withHandler((input) => Effect.succeed({})));
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
export const withHandler: {
|
|
214
|
+
<Def extends Definition<any, any>, E = never>(
|
|
215
|
+
handler: Handler<Definition.Input<Def>, Definition.Output<Def>, E, Definition.Services<Def> | Service>,
|
|
216
|
+
): (op: Def) => WithHandler<Def>;
|
|
217
|
+
<Def extends Definition<any, any>, E = never>(
|
|
218
|
+
op: Def,
|
|
219
|
+
handler: Handler<Definition.Input<Def>, Definition.Output<Def>, E, Definition.Services<Def> | Service>,
|
|
220
|
+
): WithHandler<Def>;
|
|
221
|
+
} = <Def extends Definition<any, any>, E = never>(
|
|
222
|
+
opOrHandler: Def | Handler<Definition.Input<Def>, Definition.Output<Def>, E, Definition.Services<Def> | Service>,
|
|
223
|
+
handler?: Handler<Definition.Input<Def>, Definition.Output<Def>, E, Definition.Services<Def> | Service>,
|
|
224
|
+
): WithHandler<Def> => {
|
|
225
|
+
// If called with just handler (piped usage).
|
|
226
|
+
if (handler === undefined) {
|
|
227
|
+
const handlerFn = opOrHandler as Handler<
|
|
228
|
+
Definition.Input<Def>,
|
|
229
|
+
Definition.Output<Def>,
|
|
230
|
+
E,
|
|
231
|
+
Definition.Services<Def> | Service
|
|
232
|
+
>;
|
|
233
|
+
return ((op: Def) => ({
|
|
234
|
+
...op,
|
|
235
|
+
handler: handlerFn,
|
|
236
|
+
})) as any;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// If called with both op and handler (direct usage).
|
|
240
|
+
const op = opOrHandler as Def;
|
|
241
|
+
return {
|
|
242
|
+
...op,
|
|
243
|
+
handler,
|
|
244
|
+
} as any;
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Helper to make the handler type opaque.
|
|
249
|
+
* Workaround for TypeScript's inability to infer the handler type from the operation definition:
|
|
250
|
+
* ```
|
|
251
|
+
* error TS2742: The inferred type of 'default' cannot be named without a reference.
|
|
252
|
+
* ```
|
|
253
|
+
*
|
|
254
|
+
* Most of the time the exact handler type is not needed.
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```ts
|
|
258
|
+
* const MyOp = Operation.make({
|
|
259
|
+
* input: Schema.Void,
|
|
260
|
+
* output: Schema.Void,
|
|
261
|
+
* meta: { key: 'my-op' },
|
|
262
|
+
* services: [DatabaseService],
|
|
263
|
+
* });
|
|
264
|
+
*
|
|
265
|
+
* export default MyOp.pipe(
|
|
266
|
+
* Operation.withHandler(
|
|
267
|
+
* Effect.fn(function* (input) {
|
|
268
|
+
* return { result: 'ok' };
|
|
269
|
+
* }),
|
|
270
|
+
* ),
|
|
271
|
+
* Operation.opaqueHandler,
|
|
272
|
+
* );
|
|
273
|
+
* ```
|
|
274
|
+
*/
|
|
275
|
+
export const opaqueHandler = <T extends Operation.Definition.Any>(
|
|
276
|
+
handler: Operation.WithHandler<T>,
|
|
277
|
+
): Operation.WithHandler<Operation.Definition.Any> => handler;
|
|
278
|
+
|
|
279
|
+
//
|
|
280
|
+
// Invocation Interfaces
|
|
281
|
+
//
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Local invocation of an operation.
|
|
285
|
+
*/
|
|
286
|
+
export type Invoke = <I, O, E>(op: Definition<I, O>, input: I) => Effect.Effect<O, E>;
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Remote invocation of an operation.
|
|
290
|
+
*/
|
|
291
|
+
export type InvokeRemote = <I, O, E>(
|
|
292
|
+
op: Definition<I, O>,
|
|
293
|
+
input: I,
|
|
294
|
+
options?: { timeout?: number },
|
|
295
|
+
) => Effect.Effect<O, E>;
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Database record of an operation.
|
|
299
|
+
* The registry `key` and `version` live in the object meta — access via
|
|
300
|
+
* `Obj.getMeta(record).key` and `Obj.getMeta(record).version`.
|
|
301
|
+
*/
|
|
302
|
+
export const PersistentOperation = Schema$.Struct({
|
|
303
|
+
name: Schema$.NonEmptyString,
|
|
304
|
+
|
|
305
|
+
description: Schema$.optional(Schema$.String),
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* ISO date string of the last deployment.
|
|
309
|
+
*/
|
|
310
|
+
updated: Schema$.optional(Schema$.String),
|
|
311
|
+
|
|
312
|
+
// Reference to a source script if it exists within ECHO.
|
|
313
|
+
// TODO(burdon): Don't ref ScriptType directly (core).
|
|
314
|
+
source: Schema$.optional(Ref.Ref(Obj.Unknown)),
|
|
315
|
+
|
|
316
|
+
inputSchema: Schema$.optional(JsonSchema.JsonSchema),
|
|
317
|
+
outputSchema: Schema$.optional(JsonSchema.JsonSchema),
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* List of required services.
|
|
321
|
+
* Match the Context.Tag keys of the FunctionServices variants.
|
|
322
|
+
*/
|
|
323
|
+
services: Schema$.optional(Schema$.Array(Schema$.String)),
|
|
324
|
+
|
|
325
|
+
// Local binding to a function name.
|
|
326
|
+
// TODO(dmaretskyi): Add this field to Operation.Definition.
|
|
327
|
+
binding: Schema$.optional(Schema$.String),
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Phosphor icon identifier in `ph--<name>--<variant>` format (e.g. `ph--file-text--regular`).
|
|
331
|
+
*/
|
|
332
|
+
icon: Schema$.optional(Schema$.String),
|
|
333
|
+
}).pipe(
|
|
334
|
+
Annotation.LabelAnnotation.set(['name']),
|
|
335
|
+
Annotation.IconAnnotation.set({ icon: 'ph--function--regular', hue: 'blue' }),
|
|
336
|
+
Annotation.HiddenAnnotation.set(true),
|
|
337
|
+
// TODO(dmaretskyi): Keep typename as 'org.dxos.type.function' (not 'operation') to maintain
|
|
338
|
+
// backward compatibility with existing data and avoid requiring data migration.
|
|
339
|
+
Type.makeObject(DXN.make('org.dxos.type.function', '0.2.0')),
|
|
340
|
+
);
|
|
341
|
+
export type PersistentOperation = Type.InstanceType<typeof PersistentOperation>;
|
|
342
|
+
|
|
343
|
+
const FUNCTION_META_KEY = 'org.dxos.service.function';
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Get the registry key for a persistent operation record (from the object meta).
|
|
347
|
+
*/
|
|
348
|
+
export const getKey = (record: PersistentOperation): string | undefined => Obj.getMeta(record).key;
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Get the registry version for a persistent operation record (from the object meta).
|
|
352
|
+
*/
|
|
353
|
+
export const getVersion = (record: PersistentOperation): string | undefined => Obj.getMeta(record).version;
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Serialize an operation definition to a persistent operation record.
|
|
357
|
+
*/
|
|
358
|
+
export const serialize = (operation: Definition.Any): PersistentOperation => {
|
|
359
|
+
return Obj.make(PersistentOperation, {
|
|
360
|
+
[Obj.Meta]: {
|
|
361
|
+
key: operation.meta.key,
|
|
362
|
+
version: operation.meta.version ?? '0.0.0',
|
|
363
|
+
keys: operation.meta.deployedId ? [{ source: FUNCTION_META_KEY, id: operation.meta.deployedId }] : [],
|
|
364
|
+
annotations: operation.meta.annotations,
|
|
365
|
+
},
|
|
366
|
+
name: operation.meta.name ?? '',
|
|
367
|
+
description: operation.meta.description,
|
|
368
|
+
icon: operation.meta.icon,
|
|
369
|
+
updated: undefined,
|
|
370
|
+
source: undefined,
|
|
371
|
+
inputSchema: JsonSchema.toJsonSchema(operation.input),
|
|
372
|
+
outputSchema: JsonSchema.toJsonSchema(operation.output),
|
|
373
|
+
services: operation.services.map((service) => service.key),
|
|
374
|
+
});
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Deserialize a persistent operation record to an operation definition.
|
|
379
|
+
*/
|
|
380
|
+
export const deserialize = (record: PersistentOperation): Definition.Any => {
|
|
381
|
+
const meta = Obj.getMeta(record);
|
|
382
|
+
// Extract deployed function ID from ECHO meta keys (matches FUNCTIONS_META_KEY in @dxos/functions).
|
|
383
|
+
const deployedId = meta.keys.find((key) => key.source === FUNCTION_META_KEY)?.id;
|
|
384
|
+
return make({
|
|
385
|
+
input: record.inputSchema ? JsonSchema.toEffectSchema(record.inputSchema) : Schema$.Unknown,
|
|
386
|
+
output: record.outputSchema ? JsonSchema.toEffectSchema(record.outputSchema) : Schema$.Unknown,
|
|
387
|
+
services: record.services?.map((service) => Context.GenericTag(service)) ?? [],
|
|
388
|
+
executionMode: 'async',
|
|
389
|
+
types: [],
|
|
390
|
+
meta: {
|
|
391
|
+
key: DXN.tryMake(meta.key ?? record.name) ?? DXN.make(meta.key ?? record.name),
|
|
392
|
+
name: record.name,
|
|
393
|
+
version: meta.version ?? '0.0.0',
|
|
394
|
+
description: record.description,
|
|
395
|
+
icon: record.icon,
|
|
396
|
+
deployedId,
|
|
397
|
+
annotations: meta.annotations ?? {},
|
|
398
|
+
},
|
|
399
|
+
});
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Update properties on the target operation record from the source operation record.
|
|
404
|
+
*/
|
|
405
|
+
export const setFrom = (target: PersistentOperation, source: PersistentOperation) => {
|
|
406
|
+
Obj.update(target, (target) => {
|
|
407
|
+
target.name = source.name ?? target.name;
|
|
408
|
+
target.description = source.description;
|
|
409
|
+
target.icon = source.icon;
|
|
410
|
+
target.updated = source.updated;
|
|
411
|
+
// TODO(dmaretskyi): A workaround for an ECHO bug.
|
|
412
|
+
target.inputSchema = source.inputSchema ? JSON.parse(JSON.stringify(source.inputSchema)) : undefined;
|
|
413
|
+
target.outputSchema = source.outputSchema ? JSON.parse(JSON.stringify(source.outputSchema)) : undefined;
|
|
414
|
+
const sourceMeta = Obj.getMeta(source);
|
|
415
|
+
const targetMeta = Obj.getMeta(target);
|
|
416
|
+
targetMeta.key = sourceMeta.key ?? targetMeta.key;
|
|
417
|
+
targetMeta.version = sourceMeta.version;
|
|
418
|
+
// Only overwrite foreign keys when the source actually carries them; otherwise we'd wipe
|
|
419
|
+
// bindings (e.g. the deployedId set by `deserialize`) on records produced by `serialize`.
|
|
420
|
+
if (sourceMeta.keys.length > 0) {
|
|
421
|
+
targetMeta.keys = JSON.parse(JSON.stringify(sourceMeta.keys));
|
|
422
|
+
}
|
|
423
|
+
targetMeta.annotations = sourceMeta.annotations ?? targetMeta.annotations;
|
|
424
|
+
});
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Translatable label — a plain string or an i18next-style `[key, options]` tuple.
|
|
429
|
+
* Defined locally to avoid a core dependency on UI translation packages; structurally compatible with
|
|
430
|
+
* the app-level `Label` type so values flow into UI toasts unchanged.
|
|
431
|
+
*/
|
|
432
|
+
export type Label = string | [string, { ns: string | readonly string[]; count?: number; defaultValue?: string }];
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Per-phase user notification messages for an invocation.
|
|
436
|
+
* A phase is notified to the user iff its message is provided; messages are translatable {@link Label}s.
|
|
437
|
+
*/
|
|
438
|
+
export interface NotifyOptions {
|
|
439
|
+
/** Shown when the invocation starts. */
|
|
440
|
+
start?: Label;
|
|
441
|
+
/** Shown when the invocation succeeds. */
|
|
442
|
+
success?: Label;
|
|
443
|
+
/** Shown when the invocation fails. */
|
|
444
|
+
error?: Label;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Options for operation invocation.
|
|
449
|
+
*/
|
|
450
|
+
export interface InvokeOptions {
|
|
451
|
+
/**
|
|
452
|
+
* Space ID to provide database context for the handler.
|
|
453
|
+
*/
|
|
454
|
+
spaceId?: Key.SpaceId;
|
|
455
|
+
/**
|
|
456
|
+
* Request user-facing notifications at the given invocation phases.
|
|
457
|
+
*/
|
|
458
|
+
notify?: NotifyOptions;
|
|
459
|
+
/**
|
|
460
|
+
* URI of the conversation feed (queue) — today always an EID, but typed as
|
|
461
|
+
* `URI.URI` to accommodate future entity-kind extensions. Narrow with `EID.parse`
|
|
462
|
+
* at the point of use.
|
|
463
|
+
* Passed to the process environment so nested operations can resolve AiContext.Service and related services.
|
|
464
|
+
*/
|
|
465
|
+
conversation?: URI.URI;
|
|
466
|
+
/**
|
|
467
|
+
* Optional process-runtime tracing metadata (consumed by `@dxos/functions-runtime` when wired).
|
|
468
|
+
*/
|
|
469
|
+
tracing?: unknown;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Annotation that marks an operation as idempotent — safe to retry even if a previous execution
|
|
474
|
+
* was interrupted mid-handler. When absent the operation is treated as non-idempotent, and the
|
|
475
|
+
* process runtime will fail (rather than re-run) any handler that was interrupted.
|
|
476
|
+
*/
|
|
477
|
+
export const IdempotentAnnotation = Annotation.make({
|
|
478
|
+
id: 'org.dxos.operation.idempotent',
|
|
479
|
+
schema: Schema$.Boolean,
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Returns true when the operation is explicitly annotated as idempotent.
|
|
484
|
+
*/
|
|
485
|
+
export const isIdempotent = (op: Definition.Any): boolean =>
|
|
486
|
+
op.meta.annotations
|
|
487
|
+
? Option.getOrElse(Annotation.getDictionary(op.meta.annotations, IdempotentAnnotation), () => false)
|
|
488
|
+
: false;
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Operation service interface - provides unified access to operation invocation and scheduling.
|
|
492
|
+
* This service is automatically provided to operation handlers.
|
|
493
|
+
*/
|
|
494
|
+
export interface OperationService {
|
|
495
|
+
/**
|
|
496
|
+
* Invoke an operation as an Effect.
|
|
497
|
+
* Returns an Effect that resolves to the operation output.
|
|
498
|
+
*/
|
|
499
|
+
invoke: <I, O>(
|
|
500
|
+
op: Operation.Definition<I, O>,
|
|
501
|
+
...args: void extends I ? [input?: I, options?: InvokeOptions] : [input: I, options?: InvokeOptions]
|
|
502
|
+
) => Effect.Effect<O, NoHandlerError>;
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Schedule an operation to run as a followup.
|
|
506
|
+
* The followup is tracked and won't be cancelled when the parent operation completes.
|
|
507
|
+
* Returns an Effect that completes immediately after scheduling.
|
|
508
|
+
*/
|
|
509
|
+
schedule: <I, O>(
|
|
510
|
+
op: Operation.Definition<I, O>,
|
|
511
|
+
...args: void extends I ? [input?: I, options?: InvokeOptions] : [input: I, options?: InvokeOptions]
|
|
512
|
+
) => Effect.Effect<void>;
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Invoke an operation and return a Promise.
|
|
516
|
+
* Useful for async contexts where Effect is not available.
|
|
517
|
+
*/
|
|
518
|
+
invokePromise: <I, O>(
|
|
519
|
+
op: Operation.Definition<I, O>,
|
|
520
|
+
...args: void extends I ? [input?: I, options?: InvokeOptions] : [input: I, options?: InvokeOptions]
|
|
521
|
+
) => Promise<{ data?: O; error?: Error }>;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Context tag for the Operation service.
|
|
526
|
+
* Operation handlers can yield this to invoke other operations or schedule followups.
|
|
527
|
+
*
|
|
528
|
+
* @example
|
|
529
|
+
* ```ts
|
|
530
|
+
* const handler = (input) => Effect.gen(function* () {
|
|
531
|
+
* // Schedule a followup (fire and forget)
|
|
532
|
+
* yield* Operation.schedule(AnalyticsOperation, { event: 'completed' });
|
|
533
|
+
*
|
|
534
|
+
* // Invoke another operation
|
|
535
|
+
* return yield* Operation.invoke(OtherOperation, { data: input.data });
|
|
536
|
+
* });
|
|
537
|
+
* ```
|
|
538
|
+
*/
|
|
539
|
+
// TODO(dmaretskyi): Rename Operation.Invoker
|
|
540
|
+
export class Service extends Context.Tag('@dxos/operation/Service')<Service, OperationService>() {}
|
|
541
|
+
|
|
542
|
+
//
|
|
543
|
+
// Namespace functions - ergonomic access to Operation.Service methods.
|
|
544
|
+
//
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* Invoke an operation as an Effect.
|
|
548
|
+
* Yields the Operation.Service internally.
|
|
549
|
+
*
|
|
550
|
+
* @example
|
|
551
|
+
* ```ts
|
|
552
|
+
* yield* Operation.invoke(MyOperation, { data: 'test' });
|
|
553
|
+
* ```
|
|
554
|
+
*/
|
|
555
|
+
export const invoke = <I, O>(
|
|
556
|
+
op: Operation.Definition<I, O>,
|
|
557
|
+
...args: void extends I ? [input?: I, options?: InvokeOptions] : [input: I, options?: InvokeOptions]
|
|
558
|
+
): Effect.Effect<O, NoHandlerError, Service> =>
|
|
559
|
+
Effect.flatMap(Service, (ops) => ops.invoke(op, ...(args as [I, InvokeOptions?]))).pipe(
|
|
560
|
+
Effect.withSpan('Operation.invoke'),
|
|
561
|
+
);
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* Schedule an operation to run as a followup.
|
|
565
|
+
* Yields the Operation.Service internally.
|
|
566
|
+
*
|
|
567
|
+
* @example
|
|
568
|
+
* ```ts
|
|
569
|
+
* yield* Operation.schedule(AnalyticsOperation, { event: 'completed' });
|
|
570
|
+
* yield* Operation.schedule(VoidOperation); // input optional when void
|
|
571
|
+
* ```
|
|
572
|
+
*/
|
|
573
|
+
export const schedule = <I, O>(
|
|
574
|
+
op: Operation.Definition<I, O>,
|
|
575
|
+
...args: void extends I ? [input?: I, options?: InvokeOptions] : [input: I, options?: InvokeOptions]
|
|
576
|
+
): Effect.Effect<void, never, Service> =>
|
|
577
|
+
Effect.flatMap(Service, (ops) => ops.schedule(op, ...(args as [I, InvokeOptions?]))).pipe(
|
|
578
|
+
Effect.withSpan('Operation.schedule'),
|
|
579
|
+
);
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* Provides additional invocation options to all invocations.
|
|
583
|
+
*/
|
|
584
|
+
export const withInvocationOptions = (options: InvokeOptions): Layer.Layer<Service, never, Service> =>
|
|
585
|
+
Layer.effect(
|
|
586
|
+
Service,
|
|
587
|
+
Effect.gen(function* () {
|
|
588
|
+
const service = yield* Service;
|
|
589
|
+
return Service.of({
|
|
590
|
+
invoke: ((op: Operation.Definition.Any, input: any, invocationOptions: InvokeOptions) =>
|
|
591
|
+
service.invoke(op, input, { ...options, ...invocationOptions })) as any,
|
|
592
|
+
invokePromise: ((op: Operation.Definition.Any, input: any, invocationOptions: InvokeOptions) =>
|
|
593
|
+
service.invokePromise(op, input, { ...options, ...invocationOptions })) as any,
|
|
594
|
+
schedule: ((op: Operation.Definition.Any, input: any, invocationOptions: InvokeOptions) =>
|
|
595
|
+
service.schedule(op, input, { ...options, ...invocationOptions })) as any,
|
|
596
|
+
});
|
|
597
|
+
}),
|
|
598
|
+
);
|
|
599
|
+
|
|
600
|
+
//
|
|
601
|
+
// Legacy schemas and migrations.
|
|
602
|
+
//
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* Persistent operation schema v0.1.0 — `key` and `version` are stored as data properties.
|
|
606
|
+
* @deprecated Use {@link PersistentOperation} (v0.2.0) instead; the `key` and `version` now live in the object meta.
|
|
607
|
+
*/
|
|
608
|
+
export const PersistentOperation_v0_1_0 = Schema$.Struct({
|
|
609
|
+
key: Schema$.optional(Schema$.String),
|
|
610
|
+
name: Schema$.NonEmptyString,
|
|
611
|
+
version: Schema$.String,
|
|
612
|
+
description: Schema$.optional(Schema$.String),
|
|
613
|
+
updated: Schema$.optional(Schema$.String),
|
|
614
|
+
source: Schema$.optional(Ref.Ref(Obj.Unknown)),
|
|
615
|
+
inputSchema: Schema$.optional(JsonSchema.JsonSchema),
|
|
616
|
+
outputSchema: Schema$.optional(JsonSchema.JsonSchema),
|
|
617
|
+
services: Schema$.optional(Schema$.Array(Schema$.String)),
|
|
618
|
+
binding: Schema$.optional(Schema$.String),
|
|
619
|
+
}).pipe(Type.makeObject(DXN.make('org.dxos.type.function', '0.1.0')));
|
|
620
|
+
export type PersistentOperation_v0_1_0 = Type.InstanceType<typeof PersistentOperation_v0_1_0>;
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* Migration from {@link PersistentOperation_v0_1_0} (v0.1.0) to {@link PersistentOperation} (v0.2.0).
|
|
624
|
+
* Moves `key` and `version` from the data section into the object meta.
|
|
625
|
+
*/
|
|
626
|
+
const _migration = Migration.define({
|
|
627
|
+
from: PersistentOperation_v0_1_0,
|
|
628
|
+
to: PersistentOperation,
|
|
629
|
+
transform: async (from) => ({
|
|
630
|
+
[Obj.Meta]: { key: from.key, version: from.version },
|
|
631
|
+
name: from.name,
|
|
632
|
+
description: from.description,
|
|
633
|
+
updated: from.updated,
|
|
634
|
+
source: from.source as any,
|
|
635
|
+
inputSchema: from.inputSchema,
|
|
636
|
+
outputSchema: from.outputSchema,
|
|
637
|
+
services: from.services,
|
|
638
|
+
binding: from.binding,
|
|
639
|
+
}),
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* Schema migrations exported by this module.
|
|
644
|
+
* Exported as an array for extensibility — append future versions here.
|
|
645
|
+
*/
|
|
646
|
+
export const migrations = [_migration];
|