@dxos/functions 0.8.4-main.c85a9c8dae → 0.8.4-main.d05539e30a
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 +1 -1
- package/dist/lib/neutral/index.mjs +294 -932
- package/dist/lib/neutral/index.mjs.map +4 -4
- package/dist/lib/neutral/meta.json +1 -1
- package/dist/types/src/index.d.ts +0 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -1
- package/dist/types/src/protocol/functions-ai-http-client.test.d.ts +2 -0
- package/dist/types/src/protocol/functions-ai-http-client.test.d.ts.map +1 -0
- package/dist/types/src/protocol/protocol.d.ts +19 -2
- package/dist/types/src/protocol/protocol.d.ts.map +1 -1
- package/dist/types/src/sdk.d.ts +5 -112
- package/dist/types/src/sdk.d.ts.map +1 -1
- package/dist/types/src/services/credentials.d.ts +15 -38
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +8 -6
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
- package/dist/types/src/services/index.d.ts +1 -4
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +1 -46
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +1 -84
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/types/index.d.ts +0 -4
- package/dist/types/src/types/index.d.ts.map +1 -1
- package/dist/types/src/types/url.d.ts +3 -3
- package/dist/types/src/types/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +18 -17
- package/src/index.ts +0 -2
- package/src/protocol/functions-ai-http-client.test.ts +105 -0
- package/src/protocol/functions-ai-http-client.ts +75 -1
- package/src/protocol/protocol.test.ts +9 -10
- package/src/protocol/protocol.ts +255 -58
- package/src/sdk.ts +12 -278
- package/src/services/credentials.ts +76 -120
- package/src/services/function-invocation-service.ts +9 -10
- package/src/services/index.ts +1 -4
- package/src/services/queues.ts +1 -78
- package/src/services/tracing.ts +0 -159
- package/src/types/index.ts +0 -4
- package/src/types/url.ts +3 -3
- package/dist/types/src/errors.d.ts +0 -121
- package/dist/types/src/errors.d.ts.map +0 -1
- package/dist/types/src/example/fib.d.ts +0 -7
- package/dist/types/src/example/fib.d.ts.map +0 -1
- package/dist/types/src/example/forex-effect.d.ts +0 -3
- package/dist/types/src/example/forex-effect.d.ts.map +0 -1
- package/dist/types/src/example/index.d.ts +0 -12
- package/dist/types/src/example/index.d.ts.map +0 -1
- package/dist/types/src/example/reply.d.ts +0 -3
- package/dist/types/src/example/reply.d.ts.map +0 -1
- package/dist/types/src/example/sleep.d.ts +0 -5
- package/dist/types/src/example/sleep.d.ts.map +0 -1
- package/dist/types/src/operation-compatibility.test.d.ts +0 -2
- package/dist/types/src/operation-compatibility.test.d.ts.map +0 -1
- package/dist/types/src/services/event-logger.d.ts +0 -81
- package/dist/types/src/services/event-logger.d.ts.map +0 -1
- package/dist/types/src/types/Function.d.ts +0 -52
- package/dist/types/src/types/Function.d.ts.map +0 -1
- package/dist/types/src/types/Script.d.ts +0 -21
- package/dist/types/src/types/Script.d.ts.map +0 -1
- package/dist/types/src/types/Trigger.d.ts +0 -122
- package/dist/types/src/types/Trigger.d.ts.map +0 -1
- package/dist/types/src/types/TriggerEvent.d.ts +0 -75
- package/dist/types/src/types/TriggerEvent.d.ts.map +0 -1
- package/src/errors.ts +0 -21
- package/src/example/fib.ts +0 -32
- package/src/example/forex-effect.ts +0 -40
- package/src/example/index.ts +0 -13
- package/src/example/reply.ts +0 -21
- package/src/example/sleep.ts +0 -24
- package/src/operation-compatibility.test.ts +0 -185
- package/src/services/event-logger.ts +0 -127
- package/src/types/Function.ts +0 -83
- package/src/types/Script.ts +0 -34
- package/src/types/Trigger.ts +0 -153
- package/src/types/TriggerEvent.ts +0 -62
package/src/protocol/protocol.ts
CHANGED
|
@@ -5,48 +5,85 @@
|
|
|
5
5
|
import * as AnthropicClient from '@effect/ai-anthropic/AnthropicClient';
|
|
6
6
|
import * as Effect from 'effect/Effect';
|
|
7
7
|
import * as Layer from 'effect/Layer';
|
|
8
|
+
import * as Option from 'effect/Option';
|
|
8
9
|
import * as Schema from 'effect/Schema';
|
|
9
10
|
import * as SchemaAST from 'effect/SchemaAST';
|
|
10
11
|
|
|
11
|
-
import { AiModelResolver, AiService } from '@dxos/ai';
|
|
12
|
+
import { AiModelResolver, AiService, OpaqueToolkit } from '@dxos/ai';
|
|
12
13
|
import { AnthropicResolver } from '@dxos/ai/resolvers';
|
|
14
|
+
import {
|
|
15
|
+
Blueprint,
|
|
16
|
+
FunctionError,
|
|
17
|
+
InvalidOperationInputError,
|
|
18
|
+
InvalidOperationOutputError,
|
|
19
|
+
Operation,
|
|
20
|
+
OperationRegistry,
|
|
21
|
+
Trace,
|
|
22
|
+
} from '@dxos/compute';
|
|
13
23
|
import { LifecycleState, Resource } from '@dxos/context';
|
|
14
24
|
import { Database, Feed, JsonSchema, Ref, type Type } from '@dxos/echo';
|
|
25
|
+
import { createFeedServiceLayer, EchoClient, type EchoDatabaseImpl, type QueueFactory } from '@dxos/echo-db';
|
|
15
26
|
import { refFromEncodedReference } from '@dxos/echo/internal';
|
|
16
|
-
import { EchoClient, type EchoDatabaseImpl, type QueueFactory, createFeedServiceLayer } from '@dxos/echo-db';
|
|
17
27
|
import { runAndForwardErrors } from '@dxos/effect';
|
|
18
28
|
import { assertState, failedInvariant, invariant } from '@dxos/invariant';
|
|
19
|
-
import { PublicKey } from '@dxos/keys';
|
|
20
|
-
import {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
25
|
-
|
|
29
|
+
import { PublicKey, type SpaceId } from '@dxos/keys';
|
|
30
|
+
import { log } from '@dxos/log';
|
|
31
|
+
import { EdgeFunctionEnv, ErrorCodec, type FunctionProtocol, type TraceProtocol } from '@dxos/protocols';
|
|
32
|
+
|
|
33
|
+
import { type FunctionServices } from '../sdk';
|
|
34
|
+
import {
|
|
35
|
+
configuredCredentialsLayer,
|
|
36
|
+
credentialsLayerFromDatabase,
|
|
37
|
+
FunctionInvocationService,
|
|
38
|
+
QueueService,
|
|
39
|
+
} from '../services';
|
|
26
40
|
import { FunctionsAiHttpClient } from './functions-ai-http-client';
|
|
27
41
|
|
|
42
|
+
export interface FunctionWrappingOptions {
|
|
43
|
+
/**
|
|
44
|
+
* Additional types to register with the database.
|
|
45
|
+
*/
|
|
46
|
+
types?: Type.AnyEntity[];
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Toolkits to make available via the `OpaqueToolkitProvider`.
|
|
50
|
+
*/
|
|
51
|
+
toolkits?: OpaqueToolkit.OpaqueToolkit[];
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Blueprint registry to expose as `Blueprint.RegistryService` inside handler Effects.
|
|
55
|
+
* Required for operations that declare `Blueprint.RegistryService` in their `services` list.
|
|
56
|
+
*/
|
|
57
|
+
blueprintRegistry?: Blueprint.Registry;
|
|
58
|
+
}
|
|
59
|
+
|
|
28
60
|
/**
|
|
29
61
|
* Wraps a function handler made with `defineFunction` to a protocol that the functions-runtime expects.
|
|
30
62
|
*/
|
|
31
|
-
export const wrapFunctionHandler = (
|
|
32
|
-
|
|
33
|
-
|
|
63
|
+
export const wrapFunctionHandler = (
|
|
64
|
+
func: Operation.WithHandler<Operation.Definition.Any>,
|
|
65
|
+
opts: FunctionWrappingOptions = {},
|
|
66
|
+
): FunctionProtocol.Func => {
|
|
67
|
+
if (!Operation.isOperationWithHandler(func)) {
|
|
68
|
+
throw new TypeError('Expected operation with handler');
|
|
34
69
|
}
|
|
35
70
|
|
|
71
|
+
const serviceTags = func.services.map((service) => service.key);
|
|
72
|
+
|
|
36
73
|
return {
|
|
37
74
|
meta: {
|
|
38
|
-
key: func.key,
|
|
39
|
-
name: func.name,
|
|
40
|
-
description: func.description,
|
|
41
|
-
inputSchema: JsonSchema.toJsonSchema(func.
|
|
42
|
-
outputSchema: func.
|
|
43
|
-
services: func.services,
|
|
75
|
+
key: func.meta.key,
|
|
76
|
+
name: func.meta.name,
|
|
77
|
+
description: func.meta.description,
|
|
78
|
+
inputSchema: JsonSchema.toJsonSchema(func.input),
|
|
79
|
+
outputSchema: func.output === undefined ? undefined : JsonSchema.toJsonSchema(func.output),
|
|
80
|
+
services: func.services.map((service) => service.key),
|
|
44
81
|
},
|
|
45
82
|
handler: async ({ data, context }) => {
|
|
46
83
|
if (
|
|
47
|
-
(
|
|
48
|
-
|
|
49
|
-
|
|
84
|
+
(serviceTags.includes(Database.Service.key) ||
|
|
85
|
+
serviceTags.includes(QueueService.key) ||
|
|
86
|
+
serviceTags.includes(Feed.FeedService.key)) &&
|
|
50
87
|
(!context.services.dataService || !context.services.queryService)
|
|
51
88
|
) {
|
|
52
89
|
throw new FunctionError({
|
|
@@ -56,31 +93,31 @@ export const wrapFunctionHandler = (func: FunctionDefinition): FunctionProtocol.
|
|
|
56
93
|
|
|
57
94
|
// eslint-disable-next-line no-useless-catch
|
|
58
95
|
try {
|
|
59
|
-
if (!SchemaAST.isAnyKeyword(func.
|
|
96
|
+
if (!SchemaAST.isAnyKeyword(func.input.ast)) {
|
|
60
97
|
try {
|
|
61
|
-
Schema.validateSync(func.
|
|
62
|
-
} catch (error) {
|
|
63
|
-
throw new
|
|
98
|
+
Schema.validateSync(func.input, { onExcessProperty: 'error' })(data);
|
|
99
|
+
} catch (error: any) {
|
|
100
|
+
throw new InvalidOperationInputError({
|
|
101
|
+
message: `Operation input did not match schema (${func.meta.key}): ${error.message}`,
|
|
102
|
+
cause: error,
|
|
103
|
+
});
|
|
64
104
|
}
|
|
65
105
|
}
|
|
66
106
|
|
|
67
|
-
await using funcContext = await new FunctionContext(context).open();
|
|
107
|
+
await using funcContext = await new FunctionContext(context, opts).open();
|
|
68
108
|
|
|
69
|
-
|
|
109
|
+
const types = [...(opts.types ?? []), ...(func.types ?? [])];
|
|
110
|
+
if (types.length > 0) {
|
|
70
111
|
invariant(funcContext.db, 'Database is required for functions with types');
|
|
71
|
-
await funcContext.db.graph.schemaRegistry.register(
|
|
112
|
+
await funcContext.db.graph.schemaRegistry.register(types as Type.AnyEntity[]);
|
|
72
113
|
}
|
|
73
114
|
|
|
74
115
|
const dataWithDecodedRefs =
|
|
75
|
-
funcContext.db && !SchemaAST.isAnyKeyword(func.
|
|
76
|
-
? decodeRefsFromSchema(func.
|
|
116
|
+
funcContext.db && !SchemaAST.isAnyKeyword(func.input.ast)
|
|
117
|
+
? decodeRefsFromSchema(func.input.ast, data, funcContext.db)
|
|
77
118
|
: data;
|
|
78
119
|
|
|
79
|
-
let result = await func.handler(
|
|
80
|
-
// TODO(dmaretskyi): Fix the types.
|
|
81
|
-
context: context as any,
|
|
82
|
-
data: dataWithDecodedRefs,
|
|
83
|
-
});
|
|
120
|
+
let result: any = await func.handler(dataWithDecodedRefs);
|
|
84
121
|
|
|
85
122
|
if (Effect.isEffect(result)) {
|
|
86
123
|
result = await runAndForwardErrors(
|
|
@@ -91,8 +128,27 @@ export const wrapFunctionHandler = (func: FunctionDefinition): FunctionProtocol.
|
|
|
91
128
|
);
|
|
92
129
|
}
|
|
93
130
|
|
|
94
|
-
|
|
95
|
-
|
|
131
|
+
// Flush in-memory ECHO writes before the function scope closes.
|
|
132
|
+
// Writes performed by `db.add` / `db.remove` are buffered in the in-memory
|
|
133
|
+
// `EchoDatabaseImpl` and only pushed across the `DataService` binding when
|
|
134
|
+
// `db.flush({ disk })` is called. `FunctionContext._close` (invoked by the
|
|
135
|
+
// `await using` above) calls `db.close()` but does NOT flush, so mutations
|
|
136
|
+
// performed by handlers that declare `Database.Service` (e.g. `object-create`,
|
|
137
|
+
// `object-update`, `relation-create`) would be silently dropped before reaching
|
|
138
|
+
// the edge `AutomergeReplicator`. Flushing here closes that hole.
|
|
139
|
+
if (serviceTags.includes(Database.Service.key) && funcContext.db) {
|
|
140
|
+
await funcContext.db.flush({ disk: true, indexes: false });
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (func.output && !SchemaAST.isAnyKeyword(func.output.ast)) {
|
|
144
|
+
try {
|
|
145
|
+
Schema.validateSync(func.output, { onExcessProperty: 'error' })(result);
|
|
146
|
+
} catch (error: any) {
|
|
147
|
+
throw new InvalidOperationOutputError({
|
|
148
|
+
message: `Operation output did not match schema (${func.meta.key}): ${error.message}`,
|
|
149
|
+
cause: error,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
96
152
|
}
|
|
97
153
|
|
|
98
154
|
return result;
|
|
@@ -112,10 +168,12 @@ class FunctionContext extends Resource {
|
|
|
112
168
|
readonly client: EchoClient | undefined;
|
|
113
169
|
db: EchoDatabaseImpl | undefined;
|
|
114
170
|
queues: QueueFactory | undefined;
|
|
171
|
+
readonly opts: FunctionWrappingOptions;
|
|
115
172
|
|
|
116
|
-
constructor(context: FunctionProtocol.Context) {
|
|
173
|
+
constructor(context: FunctionProtocol.Context, opts: FunctionWrappingOptions) {
|
|
117
174
|
super();
|
|
118
175
|
this.context = context;
|
|
176
|
+
this.opts = opts;
|
|
119
177
|
if (context.services.dataService && context.services.queryService) {
|
|
120
178
|
this.client = new EchoClient().connectToService({
|
|
121
179
|
dataService: context.services.dataService,
|
|
@@ -155,33 +213,172 @@ class FunctionContext extends Resource {
|
|
|
155
213
|
const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
|
|
156
214
|
const feedLayer = this.queues ? createFeedServiceLayer(this.queues) : Feed.notAvailable;
|
|
157
215
|
const credentials = dbLayer
|
|
158
|
-
?
|
|
159
|
-
:
|
|
160
|
-
const functionInvocationService = MockedFunctionInvocationService;
|
|
161
|
-
const tracing = TracingService.layerNoop;
|
|
216
|
+
? credentialsLayerFromDatabase({ caching: true }).pipe(Layer.provide(dbLayer))
|
|
217
|
+
: configuredCredentialsLayer([]);
|
|
162
218
|
|
|
163
219
|
const aiLayer = this.context.services.functionsAiService
|
|
164
|
-
?
|
|
165
|
-
Layer.provide(
|
|
166
|
-
AnthropicResolver.make().pipe(
|
|
167
|
-
Layer.provide(
|
|
168
|
-
AnthropicClient.layer({
|
|
169
|
-
// Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
|
|
170
|
-
apiUrl: 'http://internal/provider/anthropic',
|
|
171
|
-
}).pipe(Layer.provide(FunctionsAiHttpClient.layer(this.context.services.functionsAiService))),
|
|
172
|
-
),
|
|
173
|
-
),
|
|
174
|
-
),
|
|
175
|
-
)
|
|
220
|
+
? InternalAiServiceLayer(this.context.services.functionsAiService)
|
|
176
221
|
: AiService.notAvailable;
|
|
177
222
|
|
|
178
|
-
|
|
223
|
+
const operationServiceLayer = this.context.services.functionsService
|
|
224
|
+
? makeOperationServiceLayer(this.context.services.functionsService)
|
|
225
|
+
: unavailableOperationServiceLayer;
|
|
226
|
+
|
|
227
|
+
const operationRegistryLayer = this.context.services.functionsService
|
|
228
|
+
? makeOperationRegistryLayer(this.context.services.functionsService, this.context.spaceId as SpaceId | undefined)
|
|
229
|
+
: emptyOperationRegistryLayer;
|
|
230
|
+
|
|
231
|
+
const traceWriterLayer = this.context.services.traceService
|
|
232
|
+
? makeTraceWriterLayer(this.context.services.traceService)
|
|
233
|
+
: Trace.writerLayerNoop;
|
|
234
|
+
|
|
235
|
+
log('Creating function context layer', {
|
|
236
|
+
traceService: !!this.context.services.traceService,
|
|
237
|
+
functionsService: !!this.context.services.functionsService,
|
|
238
|
+
functionsAiService: !!this.context.services.functionsAiService,
|
|
239
|
+
spaceId: this.context.spaceId,
|
|
240
|
+
spaceRootUrl: this.context.spaceRootUrl,
|
|
241
|
+
toolkits: this.opts.toolkits?.length ?? 0,
|
|
242
|
+
types: this.opts.types?.length ?? 0,
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
const blueprintRegistryLayer = this.opts.blueprintRegistry
|
|
246
|
+
? Layer.succeed(Blueprint.RegistryService, this.opts.blueprintRegistry)
|
|
247
|
+
: Blueprint.RegistryService.notAvailable;
|
|
248
|
+
|
|
249
|
+
return Layer.mergeAll(
|
|
250
|
+
dbLayer,
|
|
251
|
+
queuesLayer,
|
|
252
|
+
feedLayer,
|
|
253
|
+
credentials,
|
|
254
|
+
operationServiceLayer,
|
|
255
|
+
operationRegistryLayer,
|
|
256
|
+
aiLayer,
|
|
257
|
+
OpaqueToolkit.providerLayer(OpaqueToolkit.merge(...(this.opts.toolkits ?? []))),
|
|
258
|
+
traceWriterLayer,
|
|
259
|
+
blueprintRegistryLayer,
|
|
260
|
+
|
|
261
|
+
// `FunctionInvocationService` is deprecated; new code should yield `Operation.Service`.
|
|
262
|
+
// The cloudflare wrapper provides only the unavailable layer to satisfy the (still-present)
|
|
263
|
+
// type union — handlers that yield it will die at invocation time.
|
|
264
|
+
FunctionInvocationService.layerNotAvailable,
|
|
265
|
+
);
|
|
179
266
|
}
|
|
180
267
|
}
|
|
181
268
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
269
|
+
/**
|
|
270
|
+
* Backs `Trace.TraceService` with the EDGE-provided `TraceService` so that operation
|
|
271
|
+
* handlers can write trace events that are forwarded to the runtime's trace sink.
|
|
272
|
+
*/
|
|
273
|
+
const makeTraceWriterLayer = (traceService: TraceProtocol.TraceService): Layer.Layer<Trace.TraceService> =>
|
|
274
|
+
Layer.succeed(Trace.TraceService, {
|
|
275
|
+
write: (eventType, payload) => {
|
|
276
|
+
log('Writing trace event', {
|
|
277
|
+
eventType: eventType.key,
|
|
278
|
+
});
|
|
279
|
+
traceService.write([
|
|
280
|
+
{
|
|
281
|
+
key: eventType.key,
|
|
282
|
+
isEphemeral: eventType.isEphemeral,
|
|
283
|
+
data: payload,
|
|
284
|
+
},
|
|
285
|
+
]);
|
|
286
|
+
},
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* AI service layer that proxies HTTP requests through the EDGE-provided `FunctionsAiService`.
|
|
291
|
+
*/
|
|
292
|
+
const InternalAiServiceLayer = (functionsAiService: EdgeFunctionEnv.FunctionsAiService) =>
|
|
293
|
+
AiModelResolver.AiModelResolver.buildAiService.pipe(
|
|
294
|
+
Layer.provide(
|
|
295
|
+
AnthropicResolver.make().pipe(
|
|
296
|
+
Layer.provide(
|
|
297
|
+
AnthropicClient.layer({
|
|
298
|
+
// Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
|
|
299
|
+
apiUrl: 'http://internal/provider/anthropic',
|
|
300
|
+
}).pipe(Layer.provide(FunctionsAiHttpClient.layer(functionsAiService))),
|
|
301
|
+
),
|
|
302
|
+
),
|
|
303
|
+
),
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Backs `Operation.Service` with the EDGE-provided `FunctionsService` so that operation
|
|
308
|
+
* handlers can invoke other deployed operations remotely. The `deployedId` on the operation
|
|
309
|
+
* definition is used as the routing key.
|
|
310
|
+
*/
|
|
311
|
+
const makeOperationServiceLayer = (
|
|
312
|
+
functionsService: EdgeFunctionEnv.FunctionsService,
|
|
313
|
+
): Layer.Layer<Operation.Service> => {
|
|
314
|
+
const invokeRemote = async (
|
|
315
|
+
op: Operation.Definition.Any,
|
|
316
|
+
input: unknown,
|
|
317
|
+
options?: Operation.InvokeOptions,
|
|
318
|
+
): Promise<{ data?: unknown; error?: Error }> => {
|
|
319
|
+
invariant(op.meta.deployedId, `Operation '${op.meta.key}' has no deployedId; cannot invoke remotely.`);
|
|
320
|
+
const result = await functionsService.invoke(op.meta.deployedId, input, {
|
|
321
|
+
spaceId: options?.spaceId,
|
|
322
|
+
// Forward the conversation DXN so the remote runtime can rebuild conversation-scoped
|
|
323
|
+
// services (e.g. `AiContext.Service`) needed by operations like `GetContext`.
|
|
324
|
+
conversation: options?.conversation,
|
|
325
|
+
});
|
|
326
|
+
if (result._kind === 'success') {
|
|
327
|
+
return { data: result.data };
|
|
328
|
+
}
|
|
329
|
+
return { error: ErrorCodec.decode(result.error) };
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
return Layer.succeed(Operation.Service, {
|
|
333
|
+
invoke: ((op: Operation.Definition.Any, input: unknown, options?: Operation.InvokeOptions) =>
|
|
334
|
+
Effect.tryPromise(() => invokeRemote(op, input, options)).pipe(
|
|
335
|
+
Effect.orDie,
|
|
336
|
+
Effect.flatMap((outcome) =>
|
|
337
|
+
outcome.error ? Effect.die(outcome.error) : Effect.succeed(outcome.data as never),
|
|
338
|
+
),
|
|
339
|
+
)) as Operation.OperationService['invoke'],
|
|
340
|
+
schedule: ((op: Operation.Definition.Any, input: unknown) =>
|
|
341
|
+
Effect.sync(() => {
|
|
342
|
+
invariant(op.meta.deployedId, `Operation '${op.meta.key}' has no deployedId; cannot schedule remotely.`);
|
|
343
|
+
// Fire and forget — schedule is intentionally non-awaiting.
|
|
344
|
+
void functionsService.invoke(op.meta.deployedId, input).catch(() => {
|
|
345
|
+
// Swallow errors — schedule is observability-only.
|
|
346
|
+
});
|
|
347
|
+
})) as Operation.OperationService['schedule'],
|
|
348
|
+
invokePromise: ((op: Operation.Definition.Any, input: unknown, options?: Operation.InvokeOptions) =>
|
|
349
|
+
invokeRemote(op, input, options).catch((error: unknown) => ({
|
|
350
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
351
|
+
}))) as Operation.OperationService['invokePromise'],
|
|
352
|
+
} satisfies Operation.OperationService);
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
const unavailableOperationServiceLayer = Layer.succeed(Operation.Service, {
|
|
356
|
+
invoke: () => Effect.die('Operation.Service is not available: missing functionsService in EDGE context.'),
|
|
357
|
+
schedule: () => Effect.die('Operation.Service is not available: missing functionsService in EDGE context.'),
|
|
358
|
+
invokePromise: async () => ({
|
|
359
|
+
error: new Error('Operation.Service is not available: missing functionsService in EDGE context.'),
|
|
360
|
+
}),
|
|
361
|
+
} as Operation.OperationService);
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Backs `OperationRegistry.Service` with the EDGE-provided `FunctionsService.query`. Returns
|
|
365
|
+
* the first persistent operation matching the requested key, or `Option.none()` when not found.
|
|
366
|
+
*/
|
|
367
|
+
const makeOperationRegistryLayer = (
|
|
368
|
+
functionsService: EdgeFunctionEnv.FunctionsService,
|
|
369
|
+
spaceId: SpaceId | undefined,
|
|
370
|
+
): Layer.Layer<OperationRegistry.Service> =>
|
|
371
|
+
Layer.succeed(OperationRegistry.Service, {
|
|
372
|
+
resolve: (key: string) =>
|
|
373
|
+
Effect.gen(function* () {
|
|
374
|
+
const records = yield* Effect.tryPromise(() => functionsService.query({ spaceId })).pipe(Effect.orDie);
|
|
375
|
+
const match = (records as Operation.PersistentOperation[]).find((record) => Operation.getKey(record) === key);
|
|
376
|
+
return match ? Option.some(Operation.deserialize(match)) : Option.none();
|
|
377
|
+
}),
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
const emptyOperationRegistryLayer = Layer.succeed(OperationRegistry.Service, {
|
|
381
|
+
resolve: () => Effect.succeed(Option.none()),
|
|
185
382
|
});
|
|
186
383
|
|
|
187
384
|
const decodeRefsFromSchema = (ast: SchemaAST.AST, value: unknown, db: EchoDatabaseImpl): unknown => {
|