@dxos/functions 0.8.4-main.3eb6e50203 → 0.8.4-main.3fbcb4aa9b
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/README.md +4 -6
- package/dist/lib/neutral/index.mjs +283 -924
- 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 +14 -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 +5 -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 +17 -16
- 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 +224 -58
- package/src/sdk.ts +12 -270
- package/src/services/credentials.ts +75 -118
- package/src/services/function-invocation-service.ts +6 -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 -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/types/Function.ts +0 -82
- package/src/types/Script.ts +0 -34
- package/src/types/Trigger.ts +0 -152
- 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
|
+
FunctionError,
|
|
16
|
+
InvalidOperationInputError,
|
|
17
|
+
InvalidOperationOutputError,
|
|
18
|
+
Operation,
|
|
19
|
+
OperationRegistry,
|
|
20
|
+
Trace,
|
|
21
|
+
} from '@dxos/compute';
|
|
13
22
|
import { LifecycleState, Resource } from '@dxos/context';
|
|
14
|
-
import { Database, Ref, Type } from '@dxos/echo';
|
|
23
|
+
import { Database, Feed, JsonSchema, Ref, type Type } from '@dxos/echo';
|
|
24
|
+
import { createFeedServiceLayer, EchoClient, type EchoDatabaseImpl, type QueueFactory } from '@dxos/echo-db';
|
|
15
25
|
import { refFromEncodedReference } from '@dxos/echo/internal';
|
|
16
|
-
import { EchoClient, type EchoDatabaseImpl, type QueueFactory } from '@dxos/echo-db';
|
|
17
26
|
import { runAndForwardErrors } from '@dxos/effect';
|
|
18
27
|
import { assertState, failedInvariant, invariant } from '@dxos/invariant';
|
|
19
|
-
import { PublicKey } from '@dxos/keys';
|
|
20
|
-
import {
|
|
28
|
+
import { PublicKey, type SpaceId } from '@dxos/keys';
|
|
29
|
+
import { log } from '@dxos/log';
|
|
30
|
+
import { EdgeFunctionEnv, ErrorCodec, type FunctionProtocol, type TraceProtocol } from '@dxos/protocols';
|
|
31
|
+
|
|
32
|
+
import { type FunctionServices } from '../sdk';
|
|
33
|
+
import {
|
|
34
|
+
configuredCredentialsLayer,
|
|
35
|
+
credentialsLayerFromDatabase,
|
|
36
|
+
FunctionInvocationService,
|
|
37
|
+
QueueService,
|
|
38
|
+
} from '../services';
|
|
39
|
+
import { FunctionsAiHttpClient } from './functions-ai-http-client';
|
|
21
40
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
41
|
+
export interface FunctionWrappingOptions {
|
|
42
|
+
/**
|
|
43
|
+
* Additional types to register with the database.
|
|
44
|
+
*/
|
|
45
|
+
types?: Type.AnyEntity[];
|
|
25
46
|
|
|
26
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Toolkits to make available via the `OpaqueToolkitProvider`.
|
|
49
|
+
*/
|
|
50
|
+
toolkits?: OpaqueToolkit.OpaqueToolkit[];
|
|
51
|
+
}
|
|
27
52
|
|
|
28
53
|
/**
|
|
29
54
|
* Wraps a function handler made with `defineFunction` to a protocol that the functions-runtime expects.
|
|
30
55
|
*/
|
|
31
|
-
export const wrapFunctionHandler = (
|
|
32
|
-
|
|
33
|
-
|
|
56
|
+
export const wrapFunctionHandler = (
|
|
57
|
+
func: Operation.WithHandler<Operation.Definition.Any>,
|
|
58
|
+
opts: FunctionWrappingOptions = {},
|
|
59
|
+
): FunctionProtocol.Func => {
|
|
60
|
+
if (!Operation.isOperationWithHandler(func)) {
|
|
61
|
+
throw new TypeError('Expected operation with handler');
|
|
34
62
|
}
|
|
35
63
|
|
|
64
|
+
const serviceTags = func.services.map((service) => service.key);
|
|
65
|
+
|
|
36
66
|
return {
|
|
37
67
|
meta: {
|
|
38
|
-
key: func.key,
|
|
39
|
-
name: func.name,
|
|
40
|
-
description: func.description,
|
|
41
|
-
inputSchema:
|
|
42
|
-
outputSchema: func.
|
|
43
|
-
services: func.services,
|
|
68
|
+
key: func.meta.key,
|
|
69
|
+
name: func.meta.name,
|
|
70
|
+
description: func.meta.description,
|
|
71
|
+
inputSchema: JsonSchema.toJsonSchema(func.input),
|
|
72
|
+
outputSchema: func.output === undefined ? undefined : JsonSchema.toJsonSchema(func.output),
|
|
73
|
+
services: func.services.map((service) => service.key),
|
|
44
74
|
},
|
|
45
75
|
handler: async ({ data, context }) => {
|
|
46
76
|
if (
|
|
47
|
-
(
|
|
77
|
+
(serviceTags.includes(Database.Service.key) ||
|
|
78
|
+
serviceTags.includes(QueueService.key) ||
|
|
79
|
+
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,15 @@ export const wrapFunctionHandler = (func: FunctionDefinition): FunctionProtocol.
|
|
|
89
121
|
);
|
|
90
122
|
}
|
|
91
123
|
|
|
92
|
-
if (func.
|
|
93
|
-
|
|
124
|
+
if (func.output && !SchemaAST.isAnyKeyword(func.output.ast)) {
|
|
125
|
+
try {
|
|
126
|
+
Schema.validateSync(func.output, { onExcessProperty: 'error' })(result);
|
|
127
|
+
} catch (error: any) {
|
|
128
|
+
throw new InvalidOperationOutputError({
|
|
129
|
+
message: `Operation output did not match schema (${func.meta.key}): ${error.message}`,
|
|
130
|
+
cause: error,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
94
133
|
}
|
|
95
134
|
|
|
96
135
|
return result;
|
|
@@ -110,10 +149,12 @@ class FunctionContext extends Resource {
|
|
|
110
149
|
readonly client: EchoClient | undefined;
|
|
111
150
|
db: EchoDatabaseImpl | undefined;
|
|
112
151
|
queues: QueueFactory | undefined;
|
|
152
|
+
readonly opts: FunctionWrappingOptions;
|
|
113
153
|
|
|
114
|
-
constructor(context: FunctionProtocol.Context) {
|
|
154
|
+
constructor(context: FunctionProtocol.Context, opts: FunctionWrappingOptions) {
|
|
115
155
|
super();
|
|
116
156
|
this.context = context;
|
|
157
|
+
this.opts = opts;
|
|
117
158
|
if (context.services.dataService && context.services.queryService) {
|
|
118
159
|
this.client = new EchoClient().connectToService({
|
|
119
160
|
dataService: context.services.dataService,
|
|
@@ -151,41 +192,166 @@ class FunctionContext extends Resource {
|
|
|
151
192
|
|
|
152
193
|
const dbLayer = this.db ? Database.layer(this.db) : Database.notAvailable;
|
|
153
194
|
const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
|
|
195
|
+
const feedLayer = this.queues ? createFeedServiceLayer(this.queues) : Feed.notAvailable;
|
|
154
196
|
const credentials = dbLayer
|
|
155
|
-
?
|
|
156
|
-
:
|
|
157
|
-
const functionInvocationService = MockedFunctionInvocationService;
|
|
158
|
-
const tracing = TracingService.layerNoop;
|
|
197
|
+
? credentialsLayerFromDatabase({ caching: true }).pipe(Layer.provide(dbLayer))
|
|
198
|
+
: configuredCredentialsLayer([]);
|
|
159
199
|
|
|
160
200
|
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
|
-
)
|
|
201
|
+
? InternalAiServiceLayer(this.context.services.functionsAiService)
|
|
173
202
|
: AiService.notAvailable;
|
|
174
203
|
|
|
204
|
+
const operationServiceLayer = this.context.services.functionsService
|
|
205
|
+
? makeOperationServiceLayer(this.context.services.functionsService)
|
|
206
|
+
: unavailableOperationServiceLayer;
|
|
207
|
+
|
|
208
|
+
const operationRegistryLayer = this.context.services.functionsService
|
|
209
|
+
? makeOperationRegistryLayer(this.context.services.functionsService, this.context.spaceId as SpaceId | undefined)
|
|
210
|
+
: emptyOperationRegistryLayer;
|
|
211
|
+
|
|
212
|
+
const traceWriterLayer = this.context.services.traceService
|
|
213
|
+
? makeTraceWriterLayer(this.context.services.traceService)
|
|
214
|
+
: Trace.writerLayerNoop;
|
|
215
|
+
|
|
216
|
+
log('Creating function context layer', {
|
|
217
|
+
traceService: !!this.context.services.traceService,
|
|
218
|
+
functionsService: !!this.context.services.functionsService,
|
|
219
|
+
functionsAiService: !!this.context.services.functionsAiService,
|
|
220
|
+
spaceId: this.context.spaceId,
|
|
221
|
+
spaceRootUrl: this.context.spaceRootUrl,
|
|
222
|
+
toolkits: this.opts.toolkits?.length ?? 0,
|
|
223
|
+
types: this.opts.types?.length ?? 0,
|
|
224
|
+
});
|
|
225
|
+
|
|
175
226
|
return Layer.mergeAll(
|
|
176
|
-
dbLayer,
|
|
227
|
+
dbLayer,
|
|
177
228
|
queuesLayer,
|
|
229
|
+
feedLayer,
|
|
178
230
|
credentials,
|
|
179
|
-
|
|
231
|
+
operationServiceLayer,
|
|
232
|
+
operationRegistryLayer,
|
|
180
233
|
aiLayer,
|
|
181
|
-
|
|
234
|
+
OpaqueToolkit.providerLayer(OpaqueToolkit.merge(...(this.opts.toolkits ?? []))),
|
|
235
|
+
traceWriterLayer,
|
|
236
|
+
|
|
237
|
+
// `FunctionInvocationService` is deprecated; new code should yield `Operation.Service`.
|
|
238
|
+
// The cloudflare wrapper provides only the unavailable layer to satisfy the (still-present)
|
|
239
|
+
// type union — handlers that yield it will die at invocation time.
|
|
240
|
+
FunctionInvocationService.layerNotAvailable,
|
|
182
241
|
);
|
|
183
242
|
}
|
|
184
243
|
}
|
|
185
244
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
245
|
+
/**
|
|
246
|
+
* Backs `Trace.TraceService` with the EDGE-provided `TraceService` so that operation
|
|
247
|
+
* handlers can write trace events that are forwarded to the runtime's trace sink.
|
|
248
|
+
*/
|
|
249
|
+
const makeTraceWriterLayer = (traceService: TraceProtocol.TraceService): Layer.Layer<Trace.TraceService> =>
|
|
250
|
+
Layer.succeed(Trace.TraceService, {
|
|
251
|
+
write: (eventType, payload) => {
|
|
252
|
+
log('Writing trace event', {
|
|
253
|
+
eventType: eventType.key,
|
|
254
|
+
});
|
|
255
|
+
traceService.write([
|
|
256
|
+
{
|
|
257
|
+
key: eventType.key,
|
|
258
|
+
isEphemeral: eventType.isEphemeral,
|
|
259
|
+
data: payload,
|
|
260
|
+
},
|
|
261
|
+
]);
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* AI service layer that proxies HTTP requests through the EDGE-provided `FunctionsAiService`.
|
|
267
|
+
*/
|
|
268
|
+
const InternalAiServiceLayer = (functionsAiService: EdgeFunctionEnv.FunctionsAiService) =>
|
|
269
|
+
AiModelResolver.AiModelResolver.buildAiService.pipe(
|
|
270
|
+
Layer.provide(
|
|
271
|
+
AnthropicResolver.make().pipe(
|
|
272
|
+
Layer.provide(
|
|
273
|
+
AnthropicClient.layer({
|
|
274
|
+
// Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
|
|
275
|
+
apiUrl: 'http://internal/provider/anthropic',
|
|
276
|
+
}).pipe(Layer.provide(FunctionsAiHttpClient.layer(functionsAiService))),
|
|
277
|
+
),
|
|
278
|
+
),
|
|
279
|
+
),
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Backs `Operation.Service` with the EDGE-provided `FunctionsService` so that operation
|
|
284
|
+
* handlers can invoke other deployed operations remotely. The `deployedId` on the operation
|
|
285
|
+
* definition is used as the routing key.
|
|
286
|
+
*/
|
|
287
|
+
const makeOperationServiceLayer = (
|
|
288
|
+
functionsService: EdgeFunctionEnv.FunctionsService,
|
|
289
|
+
): Layer.Layer<Operation.Service> => {
|
|
290
|
+
const invokeRemote = async (
|
|
291
|
+
op: Operation.Definition.Any,
|
|
292
|
+
input: unknown,
|
|
293
|
+
options?: Operation.InvokeOptions,
|
|
294
|
+
): Promise<{ data?: unknown; error?: Error }> => {
|
|
295
|
+
invariant(op.meta.deployedId, `Operation '${op.meta.key}' has no deployedId; cannot invoke remotely.`);
|
|
296
|
+
const result = await functionsService.invoke(op.meta.deployedId, input, {
|
|
297
|
+
spaceId: options?.spaceId,
|
|
298
|
+
});
|
|
299
|
+
if (result._kind === 'success') {
|
|
300
|
+
return { data: result.data };
|
|
301
|
+
}
|
|
302
|
+
return { error: ErrorCodec.decode(result.error) };
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
return Layer.succeed(Operation.Service, {
|
|
306
|
+
invoke: ((op: Operation.Definition.Any, input: unknown, options?: Operation.InvokeOptions) =>
|
|
307
|
+
Effect.tryPromise(() => invokeRemote(op, input, options)).pipe(
|
|
308
|
+
Effect.orDie,
|
|
309
|
+
Effect.flatMap((outcome) =>
|
|
310
|
+
outcome.error ? Effect.die(outcome.error) : Effect.succeed(outcome.data as never),
|
|
311
|
+
),
|
|
312
|
+
)) as Operation.OperationService['invoke'],
|
|
313
|
+
schedule: ((op: Operation.Definition.Any, input: unknown) =>
|
|
314
|
+
Effect.sync(() => {
|
|
315
|
+
invariant(op.meta.deployedId, `Operation '${op.meta.key}' has no deployedId; cannot schedule remotely.`);
|
|
316
|
+
// Fire and forget — schedule is intentionally non-awaiting.
|
|
317
|
+
void functionsService.invoke(op.meta.deployedId, input).catch(() => {
|
|
318
|
+
// Swallow errors — schedule is observability-only.
|
|
319
|
+
});
|
|
320
|
+
})) as Operation.OperationService['schedule'],
|
|
321
|
+
invokePromise: ((op: Operation.Definition.Any, input: unknown, options?: Operation.InvokeOptions) =>
|
|
322
|
+
invokeRemote(op, input, options).catch((error: unknown) => ({
|
|
323
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
324
|
+
}))) as Operation.OperationService['invokePromise'],
|
|
325
|
+
} satisfies Operation.OperationService);
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
const unavailableOperationServiceLayer = Layer.succeed(Operation.Service, {
|
|
329
|
+
invoke: () => Effect.die('Operation.Service is not available: missing functionsService in EDGE context.'),
|
|
330
|
+
schedule: () => Effect.die('Operation.Service is not available: missing functionsService in EDGE context.'),
|
|
331
|
+
invokePromise: async () => ({
|
|
332
|
+
error: new Error('Operation.Service is not available: missing functionsService in EDGE context.'),
|
|
333
|
+
}),
|
|
334
|
+
} as Operation.OperationService);
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Backs `OperationRegistry.Service` with the EDGE-provided `FunctionsService.query`. Returns
|
|
338
|
+
* the first persistent operation matching the requested key, or `Option.none()` when not found.
|
|
339
|
+
*/
|
|
340
|
+
const makeOperationRegistryLayer = (
|
|
341
|
+
functionsService: EdgeFunctionEnv.FunctionsService,
|
|
342
|
+
spaceId: SpaceId | undefined,
|
|
343
|
+
): Layer.Layer<OperationRegistry.Service> =>
|
|
344
|
+
Layer.succeed(OperationRegistry.Service, {
|
|
345
|
+
resolve: (key: string) =>
|
|
346
|
+
Effect.gen(function* () {
|
|
347
|
+
const records = yield* Effect.tryPromise(() => functionsService.query({ spaceId })).pipe(Effect.orDie);
|
|
348
|
+
const match = (records as Operation.PersistentOperation[]).find((record) => record.key === key);
|
|
349
|
+
return match ? Option.some(Operation.deserialize(match)) : Option.none();
|
|
350
|
+
}),
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
const emptyOperationRegistryLayer = Layer.succeed(OperationRegistry.Service, {
|
|
354
|
+
resolve: () => Effect.succeed(Option.none()),
|
|
189
355
|
});
|
|
190
356
|
|
|
191
357
|
const decodeRefsFromSchema = (ast: SchemaAST.AST, value: unknown, db: EchoDatabaseImpl): unknown => {
|
package/src/sdk.ts
CHANGED
|
@@ -2,288 +2,30 @@
|
|
|
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, type QueueService } 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
|
|
24
|
+
// TODO(wittjosiah): Remove QueueService — use Feed.FeedService instead.
|
|
42
25
|
| QueueService
|
|
43
|
-
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
};
|
|
26
|
+
| Feed.FeedService
|
|
27
|
+
| Trace.TraceService
|
|
28
|
+
// TODO(dmaretskyi): `FunctionInvocationService` is being phased out in favour of `Operation.Service`;
|
|
29
|
+
// it's kept in the union until `functions-runtime/local-function-execution.ts` migrates.
|
|
30
|
+
| FunctionInvocationService
|
|
31
|
+
| Operation.Service;
|