@dxos/functions 0.8.4-main.c85a9c8dae → 0.8.4-main.cb12b3f963
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/dist/lib/neutral/index.mjs +155 -920
- 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/protocol.d.ts +2 -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 +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/protocol.test.ts +9 -10
- package/src/protocol/protocol.ts +68 -34
- package/src/sdk.ts +10 -278
- 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 -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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/functions",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.cb12b3f963",
|
|
4
4
|
"description": "Functions API.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -26,23 +26,24 @@
|
|
|
26
26
|
"src"
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
|
+
"@effect-atom/atom": "^0.5.1",
|
|
29
30
|
"@effect/ai-anthropic": "0.23.0",
|
|
30
31
|
"@effect/platform": "0.94.4",
|
|
31
|
-
"effect": "3.
|
|
32
|
-
"@dxos/
|
|
33
|
-
"@dxos/echo": "0.8.4-main.
|
|
34
|
-
"@dxos/
|
|
35
|
-
"@dxos/effect": "0.8.4-main.
|
|
36
|
-
"@dxos/
|
|
37
|
-
"@dxos/
|
|
38
|
-
"@dxos/
|
|
39
|
-
"@dxos/
|
|
40
|
-
"@dxos/
|
|
41
|
-
"@dxos/
|
|
42
|
-
"@dxos/types": "0.8.4-main.
|
|
43
|
-
"@dxos/
|
|
44
|
-
"@dxos/
|
|
45
|
-
"@dxos/
|
|
32
|
+
"effect": "3.20.0",
|
|
33
|
+
"@dxos/ai": "0.8.4-main.cb12b3f963",
|
|
34
|
+
"@dxos/echo": "0.8.4-main.cb12b3f963",
|
|
35
|
+
"@dxos/context": "0.8.4-main.cb12b3f963",
|
|
36
|
+
"@dxos/effect": "0.8.4-main.cb12b3f963",
|
|
37
|
+
"@dxos/compute": "0.8.4-main.cb12b3f963",
|
|
38
|
+
"@dxos/echo-db": "0.8.4-main.cb12b3f963",
|
|
39
|
+
"@dxos/errors": "0.8.4-main.cb12b3f963",
|
|
40
|
+
"@dxos/invariant": "0.8.4-main.cb12b3f963",
|
|
41
|
+
"@dxos/log": "0.8.4-main.cb12b3f963",
|
|
42
|
+
"@dxos/keys": "0.8.4-main.cb12b3f963",
|
|
43
|
+
"@dxos/types": "0.8.4-main.cb12b3f963",
|
|
44
|
+
"@dxos/protocols": "0.8.4-main.cb12b3f963",
|
|
45
|
+
"@dxos/schema": "0.8.4-main.cb12b3f963",
|
|
46
|
+
"@dxos/node-std": "0.8.4-main.cb12b3f963"
|
|
46
47
|
},
|
|
47
48
|
"publishConfig": {
|
|
48
49
|
"access": "public"
|
package/src/index.ts
CHANGED
|
@@ -4,17 +4,16 @@
|
|
|
4
4
|
|
|
5
5
|
import { describe, test } from 'vitest';
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
9
|
-
import replyFunc from '../example/reply';
|
|
7
|
+
import { InvalidOperationInputError } from '@dxos/compute';
|
|
8
|
+
import { FibonacciHandler, ReplyHandler } from '@dxos/compute/testing';
|
|
10
9
|
|
|
11
10
|
import { wrapFunctionHandler } from './protocol';
|
|
12
11
|
|
|
13
12
|
describe('wrapFunctionHandler', () => {
|
|
14
13
|
test('wraps reply function and executes handler', async ({ expect }) => {
|
|
15
|
-
const wrapped = wrapFunctionHandler(
|
|
14
|
+
const wrapped = wrapFunctionHandler(ReplyHandler);
|
|
16
15
|
|
|
17
|
-
expect(wrapped.meta.key).toBe('example.
|
|
16
|
+
expect(wrapped.meta.key).toBe('org.example.function.reply');
|
|
18
17
|
expect(wrapped.meta.name).toBe('Reply');
|
|
19
18
|
|
|
20
19
|
const testData = { message: 'hello' };
|
|
@@ -29,9 +28,9 @@ describe('wrapFunctionHandler', () => {
|
|
|
29
28
|
});
|
|
30
29
|
|
|
31
30
|
test('wraps fibonacci function with valid input', async ({ expect }) => {
|
|
32
|
-
const wrapped = wrapFunctionHandler(
|
|
31
|
+
const wrapped = wrapFunctionHandler(FibonacciHandler);
|
|
33
32
|
|
|
34
|
-
expect(wrapped.meta.key).toBe('example.
|
|
33
|
+
expect(wrapped.meta.key).toBe('org.example.function.fib');
|
|
35
34
|
expect(wrapped.meta.name).toBe('Fibonacci');
|
|
36
35
|
|
|
37
36
|
const result = await wrapped.handler({
|
|
@@ -44,8 +43,8 @@ describe('wrapFunctionHandler', () => {
|
|
|
44
43
|
expect(result).toEqual({ result: '55' });
|
|
45
44
|
});
|
|
46
45
|
|
|
47
|
-
test('throws
|
|
48
|
-
const wrapped = wrapFunctionHandler(
|
|
46
|
+
test('throws InvalidOperationInputError on invalid input schema for fibonacci', async ({ expect }) => {
|
|
47
|
+
const wrapped = wrapFunctionHandler(FibonacciHandler);
|
|
49
48
|
|
|
50
49
|
await expect(
|
|
51
50
|
wrapped.handler({
|
|
@@ -54,6 +53,6 @@ describe('wrapFunctionHandler', () => {
|
|
|
54
53
|
services: {},
|
|
55
54
|
},
|
|
56
55
|
}),
|
|
57
|
-
).rejects.toThrow(
|
|
56
|
+
).rejects.toThrow(InvalidOperationInputError);
|
|
58
57
|
});
|
|
59
58
|
});
|
package/src/protocol/protocol.ts
CHANGED
|
@@ -10,43 +10,55 @@ import * as SchemaAST from 'effect/SchemaAST';
|
|
|
10
10
|
|
|
11
11
|
import { AiModelResolver, AiService } from '@dxos/ai';
|
|
12
12
|
import { AnthropicResolver } from '@dxos/ai/resolvers';
|
|
13
|
+
import {
|
|
14
|
+
FunctionError,
|
|
15
|
+
InvalidOperationInputError,
|
|
16
|
+
InvalidOperationOutputError,
|
|
17
|
+
Operation,
|
|
18
|
+
Trace,
|
|
19
|
+
} from '@dxos/compute';
|
|
13
20
|
import { LifecycleState, Resource } from '@dxos/context';
|
|
14
21
|
import { Database, Feed, JsonSchema, Ref, type Type } from '@dxos/echo';
|
|
15
|
-
import { refFromEncodedReference } from '@dxos/echo/internal';
|
|
16
22
|
import { EchoClient, type EchoDatabaseImpl, type QueueFactory, createFeedServiceLayer } from '@dxos/echo-db';
|
|
23
|
+
import { refFromEncodedReference } from '@dxos/echo/internal';
|
|
17
24
|
import { runAndForwardErrors } from '@dxos/effect';
|
|
18
25
|
import { assertState, failedInvariant, invariant } from '@dxos/invariant';
|
|
19
26
|
import { PublicKey } from '@dxos/keys';
|
|
20
27
|
import { type FunctionProtocol } from '@dxos/protocols';
|
|
21
28
|
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
24
|
-
|
|
25
|
-
|
|
29
|
+
import { type FunctionServices } from '../sdk';
|
|
30
|
+
import {
|
|
31
|
+
configuredCredentialsLayer,
|
|
32
|
+
credentialsLayerFromDatabase,
|
|
33
|
+
FunctionInvocationService,
|
|
34
|
+
QueueService,
|
|
35
|
+
} from '../services';
|
|
26
36
|
import { FunctionsAiHttpClient } from './functions-ai-http-client';
|
|
27
37
|
|
|
28
38
|
/**
|
|
29
39
|
* Wraps a function handler made with `defineFunction` to a protocol that the functions-runtime expects.
|
|
30
40
|
*/
|
|
31
|
-
export const wrapFunctionHandler = (func:
|
|
32
|
-
if (!
|
|
33
|
-
throw new TypeError('
|
|
41
|
+
export const wrapFunctionHandler = (func: Operation.WithHandler<Operation.Definition.Any>): FunctionProtocol.Func => {
|
|
42
|
+
if (!Operation.isOperationWithHandler(func)) {
|
|
43
|
+
throw new TypeError('Expected operation with handler');
|
|
34
44
|
}
|
|
35
45
|
|
|
46
|
+
const serviceTags = func.services.map((service) => service.key);
|
|
47
|
+
|
|
36
48
|
return {
|
|
37
49
|
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,
|
|
50
|
+
key: func.meta.key,
|
|
51
|
+
name: func.meta.name,
|
|
52
|
+
description: func.meta.description,
|
|
53
|
+
inputSchema: JsonSchema.toJsonSchema(func.input),
|
|
54
|
+
outputSchema: func.output === undefined ? undefined : JsonSchema.toJsonSchema(func.output),
|
|
55
|
+
services: func.services.map((service) => service.key),
|
|
44
56
|
},
|
|
45
57
|
handler: async ({ data, context }) => {
|
|
46
58
|
if (
|
|
47
|
-
(
|
|
48
|
-
|
|
49
|
-
|
|
59
|
+
(serviceTags.includes(Database.Service.key) ||
|
|
60
|
+
serviceTags.includes(QueueService.key) ||
|
|
61
|
+
serviceTags.includes(Feed.FeedService.key)) &&
|
|
50
62
|
(!context.services.dataService || !context.services.queryService)
|
|
51
63
|
) {
|
|
52
64
|
throw new FunctionError({
|
|
@@ -56,11 +68,14 @@ export const wrapFunctionHandler = (func: FunctionDefinition): FunctionProtocol.
|
|
|
56
68
|
|
|
57
69
|
// eslint-disable-next-line no-useless-catch
|
|
58
70
|
try {
|
|
59
|
-
if (!SchemaAST.isAnyKeyword(func.
|
|
71
|
+
if (!SchemaAST.isAnyKeyword(func.input.ast)) {
|
|
60
72
|
try {
|
|
61
|
-
Schema.validateSync(func.
|
|
62
|
-
} catch (error) {
|
|
63
|
-
throw new
|
|
73
|
+
Schema.validateSync(func.input, { onExcessProperty: 'error' })(data);
|
|
74
|
+
} catch (error: any) {
|
|
75
|
+
throw new InvalidOperationInputError({
|
|
76
|
+
message: `Operation input did not match schema (${func.meta.key}): ${error.message}`,
|
|
77
|
+
cause: error,
|
|
78
|
+
});
|
|
64
79
|
}
|
|
65
80
|
}
|
|
66
81
|
|
|
@@ -72,15 +87,11 @@ export const wrapFunctionHandler = (func: FunctionDefinition): FunctionProtocol.
|
|
|
72
87
|
}
|
|
73
88
|
|
|
74
89
|
const dataWithDecodedRefs =
|
|
75
|
-
funcContext.db && !SchemaAST.isAnyKeyword(func.
|
|
76
|
-
? decodeRefsFromSchema(func.
|
|
90
|
+
funcContext.db && !SchemaAST.isAnyKeyword(func.input.ast)
|
|
91
|
+
? decodeRefsFromSchema(func.input.ast, data, funcContext.db)
|
|
77
92
|
: data;
|
|
78
93
|
|
|
79
|
-
let result = await func.handler(
|
|
80
|
-
// TODO(dmaretskyi): Fix the types.
|
|
81
|
-
context: context as any,
|
|
82
|
-
data: dataWithDecodedRefs,
|
|
83
|
-
});
|
|
94
|
+
let result: any = await func.handler(dataWithDecodedRefs);
|
|
84
95
|
|
|
85
96
|
if (Effect.isEffect(result)) {
|
|
86
97
|
result = await runAndForwardErrors(
|
|
@@ -91,8 +102,15 @@ export const wrapFunctionHandler = (func: FunctionDefinition): FunctionProtocol.
|
|
|
91
102
|
);
|
|
92
103
|
}
|
|
93
104
|
|
|
94
|
-
if (func.
|
|
95
|
-
|
|
105
|
+
if (func.output && !SchemaAST.isAnyKeyword(func.output.ast)) {
|
|
106
|
+
try {
|
|
107
|
+
Schema.validateSync(func.output, { onExcessProperty: 'error' })(result);
|
|
108
|
+
} catch (error: any) {
|
|
109
|
+
throw new InvalidOperationOutputError({
|
|
110
|
+
message: `Operation output did not match schema (${func.meta.key}): ${error.message}`,
|
|
111
|
+
cause: error,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
96
114
|
}
|
|
97
115
|
|
|
98
116
|
return result;
|
|
@@ -155,10 +173,10 @@ class FunctionContext extends Resource {
|
|
|
155
173
|
const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
|
|
156
174
|
const feedLayer = this.queues ? createFeedServiceLayer(this.queues) : Feed.notAvailable;
|
|
157
175
|
const credentials = dbLayer
|
|
158
|
-
?
|
|
159
|
-
:
|
|
176
|
+
? credentialsLayerFromDatabase({ caching: true }).pipe(Layer.provide(dbLayer))
|
|
177
|
+
: configuredCredentialsLayer([]);
|
|
160
178
|
const functionInvocationService = MockedFunctionInvocationService;
|
|
161
|
-
const
|
|
179
|
+
const operationServiceLayer = MockedOperationServiceLayer;
|
|
162
180
|
|
|
163
181
|
const aiLayer = this.context.services.functionsAiService
|
|
164
182
|
? AiModelResolver.AiModelResolver.buildAiService.pipe(
|
|
@@ -175,7 +193,17 @@ class FunctionContext extends Resource {
|
|
|
175
193
|
)
|
|
176
194
|
: AiService.notAvailable;
|
|
177
195
|
|
|
178
|
-
return Layer.mergeAll(
|
|
196
|
+
return Layer.mergeAll(
|
|
197
|
+
dbLayer,
|
|
198
|
+
queuesLayer,
|
|
199
|
+
feedLayer,
|
|
200
|
+
credentials,
|
|
201
|
+
functionInvocationService,
|
|
202
|
+
operationServiceLayer,
|
|
203
|
+
aiLayer,
|
|
204
|
+
// TODO(dmaretskyi): Forward trace events.
|
|
205
|
+
Trace.writerLayerNoop,
|
|
206
|
+
);
|
|
179
207
|
}
|
|
180
208
|
}
|
|
181
209
|
|
|
@@ -184,6 +212,12 @@ const MockedFunctionInvocationService = Layer.succeed(FunctionInvocationService,
|
|
|
184
212
|
resolveFunction: () => Effect.die('Not implemented.'),
|
|
185
213
|
});
|
|
186
214
|
|
|
215
|
+
const MockedOperationServiceLayer = Layer.succeed(Operation.Service, {
|
|
216
|
+
invoke: () => Effect.die('Calling operations from functions is not implemented yet.'),
|
|
217
|
+
schedule: () => Effect.die('Not implemented.'),
|
|
218
|
+
invokePromise: async () => ({ error: new Error('Not implemented') }),
|
|
219
|
+
} as any);
|
|
220
|
+
|
|
187
221
|
const decodeRefsFromSchema = (ast: SchemaAST.AST, value: unknown, db: EchoDatabaseImpl): unknown => {
|
|
188
222
|
if (value == null) {
|
|
189
223
|
return value;
|
package/src/sdk.ts
CHANGED
|
@@ -2,296 +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 { type
|
|
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
|
|
42
|
-
// TODO(wittjosiah): Remove QueueService — use Feed.
|
|
24
|
+
// TODO(wittjosiah): Remove QueueService — use Feed.FeedService instead.
|
|
43
25
|
| QueueService
|
|
44
|
-
| Feed.
|
|
45
|
-
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
* Function handler.
|
|
49
|
-
*/
|
|
50
|
-
export type FunctionHandler<TData = {}, TOutput = any, S extends FunctionServices = FunctionServices> = (params: {
|
|
51
|
-
/**
|
|
52
|
-
* Context available to the function.
|
|
53
|
-
*/
|
|
54
|
-
context: FunctionContext;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Data passed as the input to the function.
|
|
58
|
-
* Must match the function's input schema.
|
|
59
|
-
* This will be the payload from the trigger or other data passed into the function in a workflow.
|
|
60
|
-
*/
|
|
61
|
-
data: TData;
|
|
62
|
-
}) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any, S>;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Function context.
|
|
66
|
-
*/
|
|
67
|
-
export interface FunctionContext {
|
|
68
|
-
// TODO(dmaretskyi): Consider what we should put into context.
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const typeId = Symbol.for('@dxos/functions/FunctionDefinition');
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Deployable function definition.
|
|
75
|
-
*/
|
|
76
|
-
export type FunctionDefinition<TInput = any, TOutput = any, S extends FunctionServices = FunctionServices> = {
|
|
77
|
-
[typeId]: true;
|
|
78
|
-
|
|
79
|
-
key: string;
|
|
80
|
-
name: string;
|
|
81
|
-
description?: string;
|
|
82
|
-
inputSchema: Schema.Schema<TInput, any>;
|
|
83
|
-
outputSchema?: Schema.Schema<TOutput, any>;
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* List of types the function uses.
|
|
87
|
-
* This is used to ensure that the types are available when the function is executed.
|
|
88
|
-
*/
|
|
89
|
-
types: readonly Type.AnyEntity[];
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Keys of the required services.
|
|
93
|
-
*/
|
|
94
|
-
services: readonly string[];
|
|
95
|
-
|
|
96
|
-
meta?: {
|
|
97
|
-
/**
|
|
98
|
-
* Tools that are projected from functions have this annotation.
|
|
99
|
-
*
|
|
100
|
-
* deployedFunctionId:
|
|
101
|
-
* - Backend deployment ID assigned by the EDGE function service (typically a UUID).
|
|
102
|
-
* - Used for remote invocation via `FunctionInvocationService` → `RemoteFunctionExecutionService`.
|
|
103
|
-
* - Persisted on the corresponding ECHO `Function.Function` object's metadata under the
|
|
104
|
-
* `FUNCTIONS_META_KEY` and retrieved with `getUserFunctionIdInMetadata`.
|
|
105
|
-
*/
|
|
106
|
-
deployedFunctionId?: string;
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
handler: FunctionHandler<TInput, TOutput, S>;
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
export declare namespace FunctionDefinition {
|
|
113
|
-
export type Any = FunctionDefinition<any, any, any>;
|
|
114
|
-
export type Input<T extends Any> = T extends FunctionDefinition<infer I, infer _O, infer _S> ? I : never;
|
|
115
|
-
export type Output<T extends Any> = T extends FunctionDefinition<infer _I, infer O, infer _S> ? O : never;
|
|
116
|
-
export type Services<T extends Any> = T extends FunctionDefinition<infer _I, infer _O, infer S> ? S : never;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export type FunctionProps<T, O> = {
|
|
120
|
-
key: string;
|
|
121
|
-
name: string;
|
|
122
|
-
description?: string;
|
|
123
|
-
inputSchema: Schema.Schema<T, any>;
|
|
124
|
-
outputSchema?: Schema.Schema<O, any>;
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* List of types the function uses.
|
|
128
|
-
* This is used to ensure that the types are available when the function is executed.
|
|
129
|
-
*/
|
|
130
|
-
types?: readonly Type.AnyEntity[];
|
|
131
|
-
|
|
132
|
-
// TODO(dmaretskyi): This currently doesn't cause a compile-time error if the handler requests a service that is not specified
|
|
133
|
-
services?: readonly Context.Tag<any, any>[];
|
|
134
|
-
|
|
135
|
-
handler: FunctionHandler<T, O, FunctionServices>;
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
// TODO(dmaretskyi): Output type doesn't get typechecked.
|
|
139
|
-
export const defineFunction: {
|
|
140
|
-
<I, O>(params: FunctionProps<I, O>): FunctionDefinition<I, O, FunctionServices>;
|
|
141
|
-
} = ({ key, name, description, inputSchema, outputSchema = Schema.Any, handler, types, services }) => {
|
|
142
|
-
if (!Schema.isSchema(inputSchema)) {
|
|
143
|
-
throw new Error('Input schema must be a valid schema');
|
|
144
|
-
}
|
|
145
|
-
if (typeof handler !== 'function') {
|
|
146
|
-
throw new Error('Handler must be a function');
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// Captures the function definition location.
|
|
150
|
-
const limit = Error.stackTraceLimit;
|
|
151
|
-
Error.stackTraceLimit = 2;
|
|
152
|
-
const traceError = new Error();
|
|
153
|
-
Error.stackTraceLimit = limit;
|
|
154
|
-
let cache: false | string = false;
|
|
155
|
-
const captureStackTrace = () => {
|
|
156
|
-
if (cache !== false) {
|
|
157
|
-
return cache;
|
|
158
|
-
}
|
|
159
|
-
if (traceError.stack !== undefined) {
|
|
160
|
-
const stack = traceError.stack.split('\n');
|
|
161
|
-
if (stack[2] !== undefined) {
|
|
162
|
-
cache = stack[2].trim();
|
|
163
|
-
return cache;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
const handlerWithSpan = (...args: any[]) => {
|
|
169
|
-
const result = (handler as any)(...args);
|
|
170
|
-
if (Effect.isEffect(result)) {
|
|
171
|
-
return Effect.withSpan(result, `${key ?? name}`, {
|
|
172
|
-
captureStackTrace,
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
return result;
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
return {
|
|
179
|
-
[typeId]: true,
|
|
180
|
-
key,
|
|
181
|
-
name,
|
|
182
|
-
description,
|
|
183
|
-
inputSchema,
|
|
184
|
-
outputSchema,
|
|
185
|
-
handler: handlerWithSpan,
|
|
186
|
-
types: types ?? [],
|
|
187
|
-
services: !services ? [] : getServiceKeys(services),
|
|
188
|
-
};
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
const getServiceKeys = (services: readonly Context.Tag<any, any>[]) => {
|
|
192
|
-
return services.map((tag: any) => {
|
|
193
|
-
if (typeof tag.key === 'string') {
|
|
194
|
-
return tag.key;
|
|
195
|
-
}
|
|
196
|
-
failedInvariant();
|
|
197
|
-
});
|
|
198
|
-
};
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Converts a FunctionDefinition to an OperationDefinition with handler.
|
|
202
|
-
* The function handler is adapted to the OperationHandler format.
|
|
203
|
-
*
|
|
204
|
-
* Note: FunctionDefinition stores service keys as strings, not Tag types,
|
|
205
|
-
* so we can't use Operation.withHandler's type inference here.
|
|
206
|
-
*/
|
|
207
|
-
export const toOperation = <T, O, S extends FunctionServices = FunctionServices>(
|
|
208
|
-
functionDef: FunctionDefinition<T, O, S>,
|
|
209
|
-
): Operation.Definition<T, O> & { handler: Operation.Handler<T, O, any, S> } => {
|
|
210
|
-
const op = Operation.make({
|
|
211
|
-
schema: {
|
|
212
|
-
input: functionDef.inputSchema,
|
|
213
|
-
output: functionDef.outputSchema ?? Schema.Any,
|
|
214
|
-
},
|
|
215
|
-
meta: {
|
|
216
|
-
key: functionDef.key,
|
|
217
|
-
name: functionDef.name,
|
|
218
|
-
description: functionDef.description,
|
|
219
|
-
},
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
// Adapt FunctionHandler signature to OperationHandler format.
|
|
223
|
-
// FunctionHandler expects { context, data }, OperationHandler expects just input.
|
|
224
|
-
const operationHandler: Operation.Handler<T, O, any, S> = (input: T) => {
|
|
225
|
-
const result = functionDef.handler({
|
|
226
|
-
context: {} as FunctionContext,
|
|
227
|
-
data: input,
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
// Convert Promise or plain value to Effect.
|
|
231
|
-
if (Effect.isEffect(result)) {
|
|
232
|
-
return result;
|
|
233
|
-
}
|
|
234
|
-
if (result instanceof Promise) {
|
|
235
|
-
return Effect.tryPromise(() => result);
|
|
236
|
-
}
|
|
237
|
-
return Effect.succeed(result as O);
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
// Manually attach handler since FunctionDefinition stores service keys as strings,
|
|
241
|
-
// not Tag types, so withHandler's type inference doesn't apply.
|
|
242
|
-
return {
|
|
243
|
-
...op,
|
|
244
|
-
handler: operationHandler,
|
|
245
|
-
};
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
export const FunctionDefinition = {
|
|
249
|
-
make: defineFunction,
|
|
250
|
-
isFunction: (value: unknown): value is FunctionDefinition.Any => {
|
|
251
|
-
return typeof value === 'object' && value !== null && Symbol.for('@dxos/functions/FunctionDefinition') in value;
|
|
252
|
-
},
|
|
253
|
-
serialize: (functionDef: FunctionDefinition.Any): Function.Function => {
|
|
254
|
-
assertArgument(FunctionDefinition.isFunction(functionDef), 'functionDef');
|
|
255
|
-
return serializeFunction(functionDef);
|
|
256
|
-
},
|
|
257
|
-
deserialize: (functionObj: Function.Function): FunctionDefinition.Any => {
|
|
258
|
-
assertArgument(Obj.instanceOf(Function.Function, functionObj), 'functionObj');
|
|
259
|
-
return deserializeFunction(functionObj);
|
|
260
|
-
},
|
|
261
|
-
toOperation,
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
export const serializeFunction = (functionDef: FunctionDefinition.Any): Function.Function => {
|
|
265
|
-
const fn = Function.make({
|
|
266
|
-
key: functionDef.key,
|
|
267
|
-
name: functionDef.name,
|
|
268
|
-
version: '0.1.0',
|
|
269
|
-
description: functionDef.description,
|
|
270
|
-
inputSchema: JsonSchema.toJsonSchema(functionDef.inputSchema),
|
|
271
|
-
outputSchema: !functionDef.outputSchema ? undefined : JsonSchema.toJsonSchema(functionDef.outputSchema),
|
|
272
|
-
services: functionDef.services,
|
|
273
|
-
});
|
|
274
|
-
if (functionDef.meta?.deployedFunctionId) {
|
|
275
|
-
Obj.change(fn, (fn) => setUserFunctionIdInMetadata(Obj.getMeta(fn), functionDef.meta!.deployedFunctionId!));
|
|
276
|
-
}
|
|
277
|
-
return fn;
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
export const deserializeFunction = (functionObj: Function.Function): FunctionDefinition<unknown, unknown> => {
|
|
281
|
-
return {
|
|
282
|
-
[typeId]: true,
|
|
283
|
-
// TODO(dmaretskyi): Fix key.
|
|
284
|
-
key: functionObj.key ?? functionObj.name,
|
|
285
|
-
name: functionObj.name,
|
|
286
|
-
description: functionObj.description,
|
|
287
|
-
inputSchema: !functionObj.inputSchema ? Schema.Unknown : JsonSchema.toEffectSchema(functionObj.inputSchema),
|
|
288
|
-
outputSchema: !functionObj.outputSchema ? undefined : JsonSchema.toEffectSchema(functionObj.outputSchema),
|
|
289
|
-
// TODO(dmaretskyi): This should throw error.
|
|
290
|
-
handler: () => {},
|
|
291
|
-
services: functionObj.services ?? [],
|
|
292
|
-
types: [],
|
|
293
|
-
meta: {
|
|
294
|
-
deployedFunctionId: getUserFunctionIdInMetadata(Obj.getMeta(functionObj)),
|
|
295
|
-
},
|
|
296
|
-
};
|
|
297
|
-
};
|
|
26
|
+
| Feed.FeedService
|
|
27
|
+
| Trace.TraceService
|
|
28
|
+
| FunctionInvocationService
|
|
29
|
+
| Operation.Service;
|