@dxos/functions 0.8.4-main.ef1bc66f44 → 0.8.4-main.effb148878
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 +5 -7
- package/dist/lib/neutral/index.mjs +291 -925
- 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 -109
- 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 -5
- package/dist/types/src/services/index.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 +244 -60
- package/src/sdk.ts +11 -271
- package/src/services/credentials.ts +76 -120
- package/src/services/function-invocation-service.ts +9 -10
- package/src/services/index.ts +1 -5
- 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/services/queues.d.ts +0 -47
- package/dist/types/src/services/queues.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 -121
- package/dist/types/src/types/Trigger.d.ts.map +0 -1
- package/dist/types/src/types/TriggerEvent.d.ts +0 -74
- 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/services/queues.ts +0 -82
- package/src/types/Function.ts +0 -82
- package/src/types/Script.ts +0 -34
- package/src/types/Trigger.ts +0 -143
- package/src/types/TriggerEvent.ts +0 -62
package/src/protocol/protocol.ts
CHANGED
|
@@ -5,46 +5,78 @@
|
|
|
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
|
-
import { Database, Ref, Type } from '@dxos/echo';
|
|
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 } 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
|
-
import { FunctionError } from '../errors';
|
|
23
|
-
import { FunctionDefinition, type FunctionServices } from '../sdk';
|
|
24
|
-
import { CredentialsService, FunctionInvocationService, QueueService, TracingService } from '../services';
|
|
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';
|
|
25
32
|
|
|
33
|
+
import { type FunctionServices } from '../sdk';
|
|
34
|
+
import { configuredCredentialsLayer, credentialsLayerFromDatabase, FunctionInvocationService } from '../services';
|
|
26
35
|
import { FunctionsAiHttpClient } from './functions-ai-http-client';
|
|
27
36
|
|
|
37
|
+
export interface FunctionWrappingOptions {
|
|
38
|
+
/**
|
|
39
|
+
* Additional types to register with the database.
|
|
40
|
+
*/
|
|
41
|
+
types?: Type.AnyEntity[];
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Toolkits to make available via the `OpaqueToolkitProvider`.
|
|
45
|
+
*/
|
|
46
|
+
toolkits?: OpaqueToolkit.OpaqueToolkit[];
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Blueprint registry to expose as `Blueprint.RegistryService` inside handler Effects.
|
|
50
|
+
* Required for operations that declare `Blueprint.RegistryService` in their `services` list.
|
|
51
|
+
*/
|
|
52
|
+
blueprintRegistry?: Blueprint.Registry;
|
|
53
|
+
}
|
|
54
|
+
|
|
28
55
|
/**
|
|
29
56
|
* Wraps a function handler made with `defineFunction` to a protocol that the functions-runtime expects.
|
|
30
57
|
*/
|
|
31
|
-
export const wrapFunctionHandler = (
|
|
32
|
-
|
|
33
|
-
|
|
58
|
+
export const wrapFunctionHandler = (
|
|
59
|
+
func: Operation.WithHandler<Operation.Definition.Any>,
|
|
60
|
+
opts: FunctionWrappingOptions = {},
|
|
61
|
+
): FunctionProtocol.Func => {
|
|
62
|
+
if (!Operation.isOperationWithHandler(func)) {
|
|
63
|
+
throw new TypeError('Expected operation with handler');
|
|
34
64
|
}
|
|
35
65
|
|
|
66
|
+
const serviceTags = func.services.map((service) => service.key);
|
|
67
|
+
|
|
36
68
|
return {
|
|
37
69
|
meta: {
|
|
38
|
-
key: func.key,
|
|
39
|
-
name: func.name,
|
|
40
|
-
description: func.description,
|
|
41
|
-
inputSchema:
|
|
42
|
-
outputSchema: func.
|
|
43
|
-
services: func.services,
|
|
70
|
+
key: func.meta.key,
|
|
71
|
+
name: func.meta.name,
|
|
72
|
+
description: func.meta.description,
|
|
73
|
+
inputSchema: JsonSchema.toJsonSchema(func.input),
|
|
74
|
+
outputSchema: func.output === undefined ? undefined : JsonSchema.toJsonSchema(func.output),
|
|
75
|
+
services: func.services.map((service) => service.key),
|
|
44
76
|
},
|
|
45
77
|
handler: async ({ data, context }) => {
|
|
46
78
|
if (
|
|
47
|
-
(
|
|
79
|
+
(serviceTags.includes(Database.Service.key) || serviceTags.includes(Feed.FeedService.key)) &&
|
|
48
80
|
(!context.services.dataService || !context.services.queryService)
|
|
49
81
|
) {
|
|
50
82
|
throw new FunctionError({
|
|
@@ -54,31 +86,31 @@ export const wrapFunctionHandler = (func: FunctionDefinition): FunctionProtocol.
|
|
|
54
86
|
|
|
55
87
|
// eslint-disable-next-line no-useless-catch
|
|
56
88
|
try {
|
|
57
|
-
if (!SchemaAST.isAnyKeyword(func.
|
|
89
|
+
if (!SchemaAST.isAnyKeyword(func.input.ast)) {
|
|
58
90
|
try {
|
|
59
|
-
Schema.validateSync(func.
|
|
60
|
-
} catch (error) {
|
|
61
|
-
throw new
|
|
91
|
+
Schema.validateSync(func.input, { onExcessProperty: 'error' })(data);
|
|
92
|
+
} catch (error: any) {
|
|
93
|
+
throw new InvalidOperationInputError({
|
|
94
|
+
message: `Operation input did not match schema (${func.meta.key}): ${error.message}`,
|
|
95
|
+
cause: error,
|
|
96
|
+
});
|
|
62
97
|
}
|
|
63
98
|
}
|
|
64
99
|
|
|
65
|
-
await using funcContext = await new FunctionContext(context).open();
|
|
100
|
+
await using funcContext = await new FunctionContext(context, opts).open();
|
|
66
101
|
|
|
67
|
-
|
|
102
|
+
const types = [...(opts.types ?? []), ...(func.types ?? [])];
|
|
103
|
+
if (types.length > 0) {
|
|
68
104
|
invariant(funcContext.db, 'Database is required for functions with types');
|
|
69
|
-
await funcContext.db.graph.schemaRegistry.register(
|
|
105
|
+
await funcContext.db.graph.schemaRegistry.register(types as Type.AnyEntity[]);
|
|
70
106
|
}
|
|
71
107
|
|
|
72
108
|
const dataWithDecodedRefs =
|
|
73
|
-
funcContext.db && !SchemaAST.isAnyKeyword(func.
|
|
74
|
-
? decodeRefsFromSchema(func.
|
|
109
|
+
funcContext.db && !SchemaAST.isAnyKeyword(func.input.ast)
|
|
110
|
+
? decodeRefsFromSchema(func.input.ast, data, funcContext.db)
|
|
75
111
|
: data;
|
|
76
112
|
|
|
77
|
-
let result = await func.handler(
|
|
78
|
-
// TODO(dmaretskyi): Fix the types.
|
|
79
|
-
context: context as any,
|
|
80
|
-
data: dataWithDecodedRefs,
|
|
81
|
-
});
|
|
113
|
+
let result: any = await func.handler(dataWithDecodedRefs);
|
|
82
114
|
|
|
83
115
|
if (Effect.isEffect(result)) {
|
|
84
116
|
result = await runAndForwardErrors(
|
|
@@ -89,8 +121,27 @@ export const wrapFunctionHandler = (func: FunctionDefinition): FunctionProtocol.
|
|
|
89
121
|
);
|
|
90
122
|
}
|
|
91
123
|
|
|
92
|
-
|
|
93
|
-
|
|
124
|
+
// Flush in-memory ECHO writes before the function scope closes.
|
|
125
|
+
// Writes performed by `db.add` / `db.remove` are buffered in the in-memory
|
|
126
|
+
// `EchoDatabaseImpl` and only pushed across the `DataService` binding when
|
|
127
|
+
// `db.flush({ disk })` is called. `FunctionContext._close` (invoked by the
|
|
128
|
+
// `await using` above) calls `db.close()` but does NOT flush, so mutations
|
|
129
|
+
// performed by handlers that declare `Database.Service` (e.g. `object-create`,
|
|
130
|
+
// `object-update`, `relation-create`) would be silently dropped before reaching
|
|
131
|
+
// the edge `AutomergeReplicator`. Flushing here closes that hole.
|
|
132
|
+
if (serviceTags.includes(Database.Service.key) && funcContext.db) {
|
|
133
|
+
await funcContext.db.flush({ disk: true, indexes: false });
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (func.output && !SchemaAST.isAnyKeyword(func.output.ast)) {
|
|
137
|
+
try {
|
|
138
|
+
Schema.validateSync(func.output, { onExcessProperty: 'error' })(result);
|
|
139
|
+
} catch (error: any) {
|
|
140
|
+
throw new InvalidOperationOutputError({
|
|
141
|
+
message: `Operation output did not match schema (${func.meta.key}): ${error.message}`,
|
|
142
|
+
cause: error,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
94
145
|
}
|
|
95
146
|
|
|
96
147
|
return result;
|
|
@@ -110,10 +161,12 @@ class FunctionContext extends Resource {
|
|
|
110
161
|
readonly client: EchoClient | undefined;
|
|
111
162
|
db: EchoDatabaseImpl | undefined;
|
|
112
163
|
queues: QueueFactory | undefined;
|
|
164
|
+
readonly opts: FunctionWrappingOptions;
|
|
113
165
|
|
|
114
|
-
constructor(context: FunctionProtocol.Context) {
|
|
166
|
+
constructor(context: FunctionProtocol.Context, opts: FunctionWrappingOptions) {
|
|
115
167
|
super();
|
|
116
168
|
this.context = context;
|
|
169
|
+
this.opts = opts;
|
|
117
170
|
if (context.services.dataService && context.services.queryService) {
|
|
118
171
|
this.client = new EchoClient().connectToService({
|
|
119
172
|
dataService: context.services.dataService,
|
|
@@ -150,42 +203,173 @@ class FunctionContext extends Resource {
|
|
|
150
203
|
assertState(this._lifecycleState === LifecycleState.OPEN, 'FunctionContext is not open');
|
|
151
204
|
|
|
152
205
|
const dbLayer = this.db ? Database.layer(this.db) : Database.notAvailable;
|
|
153
|
-
const
|
|
206
|
+
const feedLayer = this.queues ? createFeedServiceLayer(this.queues) : Feed.notAvailable;
|
|
154
207
|
const credentials = dbLayer
|
|
155
|
-
?
|
|
156
|
-
:
|
|
157
|
-
const functionInvocationService = MockedFunctionInvocationService;
|
|
158
|
-
const tracing = TracingService.layerNoop;
|
|
208
|
+
? credentialsLayerFromDatabase({ caching: true }).pipe(Layer.provide(dbLayer))
|
|
209
|
+
: configuredCredentialsLayer([]);
|
|
159
210
|
|
|
160
211
|
const aiLayer = this.context.services.functionsAiService
|
|
161
|
-
?
|
|
162
|
-
Layer.provide(
|
|
163
|
-
AnthropicResolver.make().pipe(
|
|
164
|
-
Layer.provide(
|
|
165
|
-
AnthropicClient.layer({
|
|
166
|
-
// Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
|
|
167
|
-
apiUrl: 'http://internal/provider/anthropic',
|
|
168
|
-
}).pipe(Layer.provide(FunctionsAiHttpClient.layer(this.context.services.functionsAiService))),
|
|
169
|
-
),
|
|
170
|
-
),
|
|
171
|
-
),
|
|
172
|
-
)
|
|
212
|
+
? InternalAiServiceLayer(this.context.services.functionsAiService)
|
|
173
213
|
: AiService.notAvailable;
|
|
174
214
|
|
|
215
|
+
const operationServiceLayer = this.context.services.functionsService
|
|
216
|
+
? makeOperationServiceLayer(this.context.services.functionsService)
|
|
217
|
+
: unavailableOperationServiceLayer;
|
|
218
|
+
|
|
219
|
+
const operationRegistryLayer = this.context.services.functionsService
|
|
220
|
+
? makeOperationRegistryLayer(this.context.services.functionsService, this.context.spaceId as SpaceId | undefined)
|
|
221
|
+
: emptyOperationRegistryLayer;
|
|
222
|
+
|
|
223
|
+
const traceWriterLayer = this.context.services.traceService
|
|
224
|
+
? makeTraceWriterLayer(this.context.services.traceService)
|
|
225
|
+
: Trace.writerLayerNoop;
|
|
226
|
+
|
|
227
|
+
log('Creating function context layer', {
|
|
228
|
+
traceService: !!this.context.services.traceService,
|
|
229
|
+
functionsService: !!this.context.services.functionsService,
|
|
230
|
+
functionsAiService: !!this.context.services.functionsAiService,
|
|
231
|
+
spaceId: this.context.spaceId,
|
|
232
|
+
spaceRootUrl: this.context.spaceRootUrl,
|
|
233
|
+
toolkits: this.opts.toolkits?.length ?? 0,
|
|
234
|
+
types: this.opts.types?.length ?? 0,
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
const blueprintRegistryLayer = this.opts.blueprintRegistry
|
|
238
|
+
? Layer.succeed(Blueprint.RegistryService, this.opts.blueprintRegistry)
|
|
239
|
+
: Blueprint.RegistryService.notAvailable;
|
|
240
|
+
|
|
175
241
|
return Layer.mergeAll(
|
|
176
|
-
dbLayer,
|
|
177
|
-
|
|
242
|
+
dbLayer,
|
|
243
|
+
feedLayer,
|
|
178
244
|
credentials,
|
|
179
|
-
|
|
245
|
+
operationServiceLayer,
|
|
246
|
+
operationRegistryLayer,
|
|
180
247
|
aiLayer,
|
|
181
|
-
|
|
248
|
+
OpaqueToolkit.providerLayer(OpaqueToolkit.merge(...(this.opts.toolkits ?? []))),
|
|
249
|
+
traceWriterLayer,
|
|
250
|
+
blueprintRegistryLayer,
|
|
251
|
+
|
|
252
|
+
// `FunctionInvocationService` is deprecated; new code should yield `Operation.Service`.
|
|
253
|
+
// The cloudflare wrapper provides only the unavailable layer to satisfy the (still-present)
|
|
254
|
+
// type union — handlers that yield it will die at invocation time.
|
|
255
|
+
FunctionInvocationService.layerNotAvailable,
|
|
182
256
|
);
|
|
183
257
|
}
|
|
184
258
|
}
|
|
185
259
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
260
|
+
/**
|
|
261
|
+
* Backs `Trace.TraceService` with the EDGE-provided `TraceService` so that operation
|
|
262
|
+
* handlers can write trace events that are forwarded to the runtime's trace sink.
|
|
263
|
+
*/
|
|
264
|
+
const makeTraceWriterLayer = (traceService: TraceProtocol.TraceService): Layer.Layer<Trace.TraceService> =>
|
|
265
|
+
Layer.succeed(Trace.TraceService, {
|
|
266
|
+
write: (eventType, payload) => {
|
|
267
|
+
log('Writing trace event', {
|
|
268
|
+
eventType: eventType.key,
|
|
269
|
+
});
|
|
270
|
+
traceService.write([
|
|
271
|
+
{
|
|
272
|
+
key: eventType.key,
|
|
273
|
+
isEphemeral: eventType.isEphemeral,
|
|
274
|
+
data: payload,
|
|
275
|
+
},
|
|
276
|
+
]);
|
|
277
|
+
},
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* AI service layer that proxies HTTP requests through the EDGE-provided `FunctionsAiService`.
|
|
282
|
+
*/
|
|
283
|
+
const InternalAiServiceLayer = (functionsAiService: EdgeFunctionEnv.FunctionsAiService) =>
|
|
284
|
+
AiModelResolver.AiModelResolver.buildAiService.pipe(
|
|
285
|
+
Layer.provide(
|
|
286
|
+
AnthropicResolver.make().pipe(
|
|
287
|
+
Layer.provide(
|
|
288
|
+
AnthropicClient.layer({
|
|
289
|
+
// Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
|
|
290
|
+
apiUrl: 'http://internal/provider/anthropic',
|
|
291
|
+
}).pipe(Layer.provide(FunctionsAiHttpClient.layer(functionsAiService))),
|
|
292
|
+
),
|
|
293
|
+
),
|
|
294
|
+
),
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Backs `Operation.Service` with the EDGE-provided `FunctionsService` so that operation
|
|
299
|
+
* handlers can invoke other deployed operations remotely. The `deployedId` on the operation
|
|
300
|
+
* definition is used as the routing key.
|
|
301
|
+
*/
|
|
302
|
+
const makeOperationServiceLayer = (
|
|
303
|
+
functionsService: EdgeFunctionEnv.FunctionsService,
|
|
304
|
+
): Layer.Layer<Operation.Service> => {
|
|
305
|
+
const invokeRemote = async (
|
|
306
|
+
op: Operation.Definition.Any,
|
|
307
|
+
input: unknown,
|
|
308
|
+
options?: Operation.InvokeOptions,
|
|
309
|
+
): Promise<{ data?: unknown; error?: Error }> => {
|
|
310
|
+
invariant(op.meta.deployedId, `Operation '${op.meta.key}' has no deployedId; cannot invoke remotely.`);
|
|
311
|
+
const result = await functionsService.invoke(op.meta.deployedId, input, {
|
|
312
|
+
spaceId: options?.spaceId,
|
|
313
|
+
// Forward the conversation DXN so the remote runtime can rebuild conversation-scoped
|
|
314
|
+
// services (e.g. `AiContext.Service`) needed by operations like `GetContext`.
|
|
315
|
+
conversation: options?.conversation,
|
|
316
|
+
});
|
|
317
|
+
if (result._kind === 'success') {
|
|
318
|
+
return { data: result.data };
|
|
319
|
+
}
|
|
320
|
+
return { error: ErrorCodec.decode(result.error) };
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
return Layer.succeed(Operation.Service, {
|
|
324
|
+
invoke: ((op: Operation.Definition.Any, input: unknown, options?: Operation.InvokeOptions) =>
|
|
325
|
+
Effect.tryPromise(() => invokeRemote(op, input, options)).pipe(
|
|
326
|
+
Effect.orDie,
|
|
327
|
+
Effect.flatMap((outcome) =>
|
|
328
|
+
outcome.error ? Effect.die(outcome.error) : Effect.succeed(outcome.data as never),
|
|
329
|
+
),
|
|
330
|
+
)) as Operation.OperationService['invoke'],
|
|
331
|
+
schedule: ((op: Operation.Definition.Any, input: unknown, _options?: Operation.InvokeOptions) =>
|
|
332
|
+
Effect.sync(() => {
|
|
333
|
+
invariant(op.meta.deployedId, `Operation '${op.meta.key}' has no deployedId; cannot schedule remotely.`);
|
|
334
|
+
// Fire and forget — schedule is intentionally non-awaiting.
|
|
335
|
+
void functionsService.invoke(op.meta.deployedId, input).catch(() => {
|
|
336
|
+
// Swallow errors — schedule is observability-only.
|
|
337
|
+
});
|
|
338
|
+
})) as Operation.OperationService['schedule'],
|
|
339
|
+
invokePromise: ((op: Operation.Definition.Any, input: unknown, options?: Operation.InvokeOptions) =>
|
|
340
|
+
invokeRemote(op, input, options).catch((error: unknown) => ({
|
|
341
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
342
|
+
}))) as Operation.OperationService['invokePromise'],
|
|
343
|
+
} satisfies Operation.OperationService);
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
const unavailableOperationServiceLayer = Layer.succeed(Operation.Service, {
|
|
347
|
+
invoke: () => Effect.die('Operation.Service is not available: missing functionsService in EDGE context.'),
|
|
348
|
+
schedule: () => Effect.die('Operation.Service is not available: missing functionsService in EDGE context.'),
|
|
349
|
+
invokePromise: async () => ({
|
|
350
|
+
error: new Error('Operation.Service is not available: missing functionsService in EDGE context.'),
|
|
351
|
+
}),
|
|
352
|
+
} as Operation.OperationService);
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Backs `OperationRegistry.Service` with the EDGE-provided `FunctionsService.query`. Returns
|
|
356
|
+
* the first persistent operation matching the requested key, or `Option.none()` when not found.
|
|
357
|
+
*/
|
|
358
|
+
const makeOperationRegistryLayer = (
|
|
359
|
+
functionsService: EdgeFunctionEnv.FunctionsService,
|
|
360
|
+
spaceId: SpaceId | undefined,
|
|
361
|
+
): Layer.Layer<OperationRegistry.Service> =>
|
|
362
|
+
Layer.succeed(OperationRegistry.Service, {
|
|
363
|
+
resolve: (key: string) =>
|
|
364
|
+
Effect.gen(function* () {
|
|
365
|
+
const records = yield* Effect.tryPromise(() => functionsService.query({ spaceId })).pipe(Effect.orDie);
|
|
366
|
+
const match = (records as Operation.PersistentOperation[]).find((record) => Operation.getKey(record) === key);
|
|
367
|
+
return match ? Option.some(Operation.deserialize(match)) : Option.none();
|
|
368
|
+
}),
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
const emptyOperationRegistryLayer = Layer.succeed(OperationRegistry.Service, {
|
|
372
|
+
resolve: () => Effect.succeed(Option.none()),
|
|
189
373
|
});
|
|
190
374
|
|
|
191
375
|
const decodeRefsFromSchema = (ast: SchemaAST.AST, value: unknown, db: EchoDatabaseImpl): unknown => {
|
package/src/sdk.ts
CHANGED
|
@@ -2,288 +2,28 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import type * as Context from 'effect/Context';
|
|
6
|
-
import * as Effect from 'effect/Effect';
|
|
7
|
-
import * as Schema from 'effect/Schema';
|
|
8
|
-
|
|
9
5
|
import { type AiService } from '@dxos/ai';
|
|
10
|
-
import {
|
|
11
|
-
import { type Database } from '@dxos/echo';
|
|
12
|
-
import { assertArgument, failedInvariant } from '@dxos/invariant';
|
|
13
|
-
import { Operation } from '@dxos/operation';
|
|
6
|
+
import { type Credential, type Operation, type Trace } from '@dxos/compute';
|
|
7
|
+
import { type Database, type Feed } from '@dxos/echo';
|
|
14
8
|
|
|
15
|
-
import {
|
|
16
|
-
type CredentialsService,
|
|
17
|
-
type FunctionInvocationService,
|
|
18
|
-
type QueueService,
|
|
19
|
-
type TracingService,
|
|
20
|
-
} from './services';
|
|
21
|
-
import { Function } from './types';
|
|
22
|
-
import { getUserFunctionIdInMetadata, setUserFunctionIdInMetadata } from './types';
|
|
9
|
+
import { type FunctionInvocationService } from './services';
|
|
23
10
|
|
|
24
11
|
// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
|
|
25
12
|
// https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
|
|
26
13
|
// https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
|
|
27
14
|
// https://www.npmjs.com/package/aws-lambda
|
|
28
15
|
|
|
29
|
-
/**
|
|
30
|
-
* Services that are provided at the function call site by the caller.
|
|
31
|
-
*/
|
|
32
|
-
export type InvocationServices = TracingService;
|
|
33
|
-
|
|
34
16
|
/**
|
|
35
17
|
* Services that are available to invoked functions.
|
|
18
|
+
* @deprecated
|
|
36
19
|
*/
|
|
37
20
|
export type FunctionServices =
|
|
38
|
-
| InvocationServices
|
|
39
21
|
| AiService.AiService
|
|
40
|
-
| CredentialsService
|
|
22
|
+
| Credential.CredentialsService
|
|
41
23
|
| Database.Service
|
|
42
|
-
|
|
|
43
|
-
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
export type FunctionHandler<TData = {}, TOutput = any, S extends FunctionServices = FunctionServices> = (params: {
|
|
49
|
-
/**
|
|
50
|
-
* Context available to the function.
|
|
51
|
-
*/
|
|
52
|
-
context: FunctionContext;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Data passed as the input to the function.
|
|
56
|
-
* Must match the function's input schema.
|
|
57
|
-
* This will be the payload from the trigger or other data passed into the function in a workflow.
|
|
58
|
-
*/
|
|
59
|
-
data: TData;
|
|
60
|
-
}) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any, S>;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Function context.
|
|
64
|
-
*/
|
|
65
|
-
export interface FunctionContext {
|
|
66
|
-
// TODO(dmaretskyi): Consider what we should put into context.
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const typeId = Symbol.for('@dxos/functions/FunctionDefinition');
|
|
70
|
-
|
|
71
|
-
export type FunctionDefinition<T = any, O = any, S extends FunctionServices = FunctionServices> = {
|
|
72
|
-
[typeId]: true;
|
|
73
|
-
key: string;
|
|
74
|
-
name: string;
|
|
75
|
-
description?: string;
|
|
76
|
-
inputSchema: Schema.Schema<T, any>;
|
|
77
|
-
outputSchema?: Schema.Schema<O, any>;
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* List of types the function uses.
|
|
81
|
-
* This is used to ensure that the types are available when the function is executed.
|
|
82
|
-
*/
|
|
83
|
-
types: readonly Type.Entity.Any[];
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Keys of the required services.
|
|
87
|
-
*/
|
|
88
|
-
services: readonly string[];
|
|
89
|
-
|
|
90
|
-
handler: FunctionHandler<T, O, S>;
|
|
91
|
-
meta?: {
|
|
92
|
-
/**
|
|
93
|
-
* Tools that are projected from functions have this annotation.
|
|
94
|
-
*
|
|
95
|
-
* deployedFunctionId:
|
|
96
|
-
* - Backend deployment ID assigned by the EDGE function service (typically a UUID).
|
|
97
|
-
* - Used for remote invocation via `FunctionInvocationService` → `RemoteFunctionExecutionService`.
|
|
98
|
-
* - Persisted on the corresponding ECHO `Function.Function` object's metadata under the
|
|
99
|
-
* `FUNCTIONS_META_KEY` and retrieved with `getUserFunctionIdInMetadata`.
|
|
100
|
-
*/
|
|
101
|
-
deployedFunctionId?: string;
|
|
102
|
-
};
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
export declare namespace FunctionDefinition {
|
|
106
|
-
export type Any = FunctionDefinition<any, any, any>;
|
|
107
|
-
export type Input<T extends Any> = T extends FunctionDefinition<infer I, infer _O, infer _S> ? I : never;
|
|
108
|
-
export type Output<T extends Any> = T extends FunctionDefinition<infer _I, infer O, infer _S> ? O : never;
|
|
109
|
-
export type Services<T extends Any> = T extends FunctionDefinition<infer _I, infer _O, infer S> ? S : never;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export type FunctionProps<T, O> = {
|
|
113
|
-
key: string;
|
|
114
|
-
name: string;
|
|
115
|
-
description?: string;
|
|
116
|
-
inputSchema: Schema.Schema<T, any>;
|
|
117
|
-
outputSchema?: Schema.Schema<O, any>;
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* List of types the function uses.
|
|
121
|
-
* This is used to ensure that the types are available when the function is executed.
|
|
122
|
-
*/
|
|
123
|
-
types?: readonly Type.Entity.Any[];
|
|
124
|
-
// TODO(dmaretskyi): This currently doesn't cause a compile-time error if the handler requests a service that is not specified
|
|
125
|
-
services?: readonly Context.Tag<any, any>[];
|
|
126
|
-
|
|
127
|
-
handler: FunctionHandler<T, O, FunctionServices>;
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
// TODO(dmaretskyi): Output type doesn't get typechecked.
|
|
131
|
-
export const defineFunction: {
|
|
132
|
-
<I, O>(params: FunctionProps<I, O>): FunctionDefinition<I, O, FunctionServices>;
|
|
133
|
-
} = ({ key, name, description, inputSchema, outputSchema = Schema.Any, handler, types, services }) => {
|
|
134
|
-
if (!Schema.isSchema(inputSchema)) {
|
|
135
|
-
throw new Error('Input schema must be a valid schema');
|
|
136
|
-
}
|
|
137
|
-
if (typeof handler !== 'function') {
|
|
138
|
-
throw new Error('Handler must be a function');
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Captures the function definition location.
|
|
142
|
-
const limit = Error.stackTraceLimit;
|
|
143
|
-
Error.stackTraceLimit = 2;
|
|
144
|
-
const traceError = new Error();
|
|
145
|
-
Error.stackTraceLimit = limit;
|
|
146
|
-
let cache: false | string = false;
|
|
147
|
-
const captureStackTrace = () => {
|
|
148
|
-
if (cache !== false) {
|
|
149
|
-
return cache;
|
|
150
|
-
}
|
|
151
|
-
if (traceError.stack !== undefined) {
|
|
152
|
-
const stack = traceError.stack.split('\n');
|
|
153
|
-
if (stack[2] !== undefined) {
|
|
154
|
-
cache = stack[2].trim();
|
|
155
|
-
return cache;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
const handlerWithSpan = (...args: any[]) => {
|
|
161
|
-
const result = (handler as any)(...args);
|
|
162
|
-
if (Effect.isEffect(result)) {
|
|
163
|
-
return Effect.withSpan(result, `${key ?? name}`, {
|
|
164
|
-
captureStackTrace,
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
return result;
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
return {
|
|
171
|
-
[typeId]: true,
|
|
172
|
-
key,
|
|
173
|
-
name,
|
|
174
|
-
description,
|
|
175
|
-
inputSchema,
|
|
176
|
-
outputSchema,
|
|
177
|
-
handler: handlerWithSpan,
|
|
178
|
-
types: types ?? [],
|
|
179
|
-
services: !services ? [] : getServiceKeys(services),
|
|
180
|
-
} satisfies FunctionDefinition.Any;
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
const getServiceKeys = (services: readonly Context.Tag<any, any>[]) => {
|
|
184
|
-
return services.map((tag: any) => {
|
|
185
|
-
if (typeof tag.key === 'string') {
|
|
186
|
-
return tag.key;
|
|
187
|
-
}
|
|
188
|
-
failedInvariant();
|
|
189
|
-
});
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Converts a FunctionDefinition to an OperationDefinition with handler.
|
|
194
|
-
* The function handler is adapted to the OperationHandler format.
|
|
195
|
-
*
|
|
196
|
-
* Note: FunctionDefinition stores service keys as strings, not Tag types,
|
|
197
|
-
* so we can't use Operation.withHandler's type inference here.
|
|
198
|
-
*/
|
|
199
|
-
export const toOperation = <T, O, S extends FunctionServices = FunctionServices>(
|
|
200
|
-
functionDef: FunctionDefinition<T, O, S>,
|
|
201
|
-
): Operation.Definition<T, O> & { handler: Operation.Handler<T, O, any, S> } => {
|
|
202
|
-
const op = Operation.make({
|
|
203
|
-
schema: {
|
|
204
|
-
input: functionDef.inputSchema,
|
|
205
|
-
output: functionDef.outputSchema ?? Schema.Any,
|
|
206
|
-
},
|
|
207
|
-
meta: {
|
|
208
|
-
key: functionDef.key,
|
|
209
|
-
name: functionDef.name,
|
|
210
|
-
description: functionDef.description,
|
|
211
|
-
},
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
// Adapt FunctionHandler signature to OperationHandler format.
|
|
215
|
-
// FunctionHandler expects { context, data }, OperationHandler expects just input.
|
|
216
|
-
const operationHandler: Operation.Handler<T, O, any, S> = (input: T) => {
|
|
217
|
-
const result = functionDef.handler({
|
|
218
|
-
context: {} as FunctionContext,
|
|
219
|
-
data: input,
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
// Convert Promise or plain value to Effect.
|
|
223
|
-
if (Effect.isEffect(result)) {
|
|
224
|
-
return result;
|
|
225
|
-
}
|
|
226
|
-
if (result instanceof Promise) {
|
|
227
|
-
return Effect.tryPromise(() => result);
|
|
228
|
-
}
|
|
229
|
-
return Effect.succeed(result as O);
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
// Manually attach handler since FunctionDefinition stores service keys as strings,
|
|
233
|
-
// not Tag types, so withHandler's type inference doesn't apply.
|
|
234
|
-
return {
|
|
235
|
-
...op,
|
|
236
|
-
handler: operationHandler,
|
|
237
|
-
};
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
export const FunctionDefinition = {
|
|
241
|
-
make: defineFunction,
|
|
242
|
-
isFunction: (value: unknown): value is FunctionDefinition.Any => {
|
|
243
|
-
return typeof value === 'object' && value !== null && Symbol.for('@dxos/functions/FunctionDefinition') in value;
|
|
244
|
-
},
|
|
245
|
-
serialize: (functionDef: FunctionDefinition.Any): Function.Function => {
|
|
246
|
-
assertArgument(FunctionDefinition.isFunction(functionDef), 'functionDef');
|
|
247
|
-
return serializeFunction(functionDef);
|
|
248
|
-
},
|
|
249
|
-
deserialize: (functionObj: Function.Function): FunctionDefinition.Any => {
|
|
250
|
-
assertArgument(Obj.instanceOf(Function.Function, functionObj), 'functionObj');
|
|
251
|
-
return deserializeFunction(functionObj);
|
|
252
|
-
},
|
|
253
|
-
toOperation,
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
export const serializeFunction = (functionDef: FunctionDefinition.Any): Function.Function => {
|
|
257
|
-
const fn = Function.make({
|
|
258
|
-
key: functionDef.key,
|
|
259
|
-
name: functionDef.name,
|
|
260
|
-
version: '0.1.0',
|
|
261
|
-
description: functionDef.description,
|
|
262
|
-
inputSchema: Type.toJsonSchema(functionDef.inputSchema),
|
|
263
|
-
outputSchema: !functionDef.outputSchema ? undefined : Type.toJsonSchema(functionDef.outputSchema),
|
|
264
|
-
services: functionDef.services,
|
|
265
|
-
});
|
|
266
|
-
if (functionDef.meta?.deployedFunctionId) {
|
|
267
|
-
Obj.change(fn, (fn) => setUserFunctionIdInMetadata(Obj.getMeta(fn), functionDef.meta!.deployedFunctionId!));
|
|
268
|
-
}
|
|
269
|
-
return fn;
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
export const deserializeFunction = (functionObj: Function.Function): FunctionDefinition<unknown, unknown> => {
|
|
273
|
-
return {
|
|
274
|
-
[typeId]: true,
|
|
275
|
-
// TODO(dmaretskyi): Fix key.
|
|
276
|
-
key: functionObj.key ?? functionObj.name,
|
|
277
|
-
name: functionObj.name,
|
|
278
|
-
description: functionObj.description,
|
|
279
|
-
inputSchema: !functionObj.inputSchema ? Schema.Unknown : Type.toEffectSchema(functionObj.inputSchema),
|
|
280
|
-
outputSchema: !functionObj.outputSchema ? undefined : Type.toEffectSchema(functionObj.outputSchema),
|
|
281
|
-
// TODO(dmaretskyi): This should throw error.
|
|
282
|
-
handler: () => {},
|
|
283
|
-
services: functionObj.services ?? [],
|
|
284
|
-
types: [],
|
|
285
|
-
meta: {
|
|
286
|
-
deployedFunctionId: getUserFunctionIdInMetadata(Obj.getMeta(functionObj)),
|
|
287
|
-
},
|
|
288
|
-
};
|
|
289
|
-
};
|
|
24
|
+
| Feed.FeedService
|
|
25
|
+
| Trace.TraceService
|
|
26
|
+
// TODO(dmaretskyi): `FunctionInvocationService` is being phased out in favour of `Operation.Service`;
|
|
27
|
+
// it's kept in the union until `functions-runtime/local-function-execution.ts` migrates.
|
|
28
|
+
| FunctionInvocationService
|
|
29
|
+
| Operation.Service;
|