@dxos/functions 0.8.4-main.406dc2a → 0.8.4-main.548089c
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/browser/index.mjs +598 -1190
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +598 -1190
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/errors.d.ts +16 -32
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/example/fib.d.ts.map +1 -0
- package/dist/types/src/example/forex-effect.d.ts +3 -0
- package/dist/types/src/example/forex-effect.d.ts.map +1 -0
- package/dist/types/src/example/index.d.ts +12 -0
- package/dist/types/src/example/index.d.ts.map +1 -0
- package/dist/types/src/example/reply.d.ts.map +1 -0
- package/dist/types/src/example/sleep.d.ts.map +1 -0
- package/dist/types/src/handler.d.ts +28 -55
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -6
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/services/credentials.d.ts +1 -1
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +16 -4
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +2 -19
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
- package/dist/types/src/services/index.d.ts +3 -4
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +4 -9
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/types/Function.d.ts +52 -0
- package/dist/types/src/types/Function.d.ts.map +1 -0
- package/dist/types/src/types/Script.d.ts +28 -0
- package/dist/types/src/types/Script.d.ts.map +1 -0
- package/dist/types/src/{types.d.ts → types/Trigger.d.ts} +47 -138
- package/dist/types/src/types/Trigger.d.ts.map +1 -0
- package/dist/types/src/types/TriggerEvent.d.ts +44 -0
- package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +5 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/src/url.d.ts +0 -9
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +12 -59
- package/src/example/forex-effect.ts +40 -0
- package/src/example/index.ts +13 -0
- package/src/handler.ts +45 -69
- package/src/index.ts +2 -6
- package/src/services/credentials.ts +3 -4
- package/src/services/event-logger.ts +8 -2
- package/src/services/function-invocation-service.ts +6 -70
- package/src/services/index.ts +4 -4
- package/src/services/tracing.ts +7 -42
- package/src/{schema.ts → types/Function.ts} +11 -26
- package/src/types/Script.ts +33 -0
- package/src/types/Trigger.ts +139 -0
- package/src/types/TriggerEvent.ts +62 -0
- package/src/types/index.ts +8 -0
- package/src/url.ts +0 -24
- package/dist/lib/browser/bundler/index.mjs +0 -269
- package/dist/lib/browser/bundler/index.mjs.map +0 -7
- package/dist/lib/browser/chunk-J5LGTIGS.mjs +0 -10
- package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +0 -7
- package/dist/lib/browser/chunk-LKYT2SAL.mjs +0 -665
- package/dist/lib/browser/chunk-LKYT2SAL.mjs.map +0 -7
- package/dist/lib/browser/edge/index.mjs +0 -83
- package/dist/lib/browser/edge/index.mjs.map +0 -7
- package/dist/lib/browser/testing/index.mjs +0 -131
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/bundler/index.mjs +0 -270
- package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-NAQIKLZB.mjs +0 -667
- package/dist/lib/node-esm/chunk-NAQIKLZB.mjs.map +0 -7
- package/dist/lib/node-esm/edge/index.mjs +0 -84
- package/dist/lib/node-esm/edge/index.mjs.map +0 -7
- package/dist/lib/node-esm/testing/index.mjs +0 -132
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/bundler/bundler.d.ts +0 -49
- package/dist/types/src/bundler/bundler.d.ts.map +0 -1
- package/dist/types/src/bundler/bundler.test.d.ts +0 -2
- package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
- package/dist/types/src/bundler/index.d.ts +0 -2
- package/dist/types/src/bundler/index.d.ts.map +0 -1
- package/dist/types/src/edge/functions.d.ts +0 -17
- package/dist/types/src/edge/functions.d.ts.map +0 -1
- package/dist/types/src/edge/index.d.ts +0 -2
- package/dist/types/src/edge/index.d.ts.map +0 -1
- package/dist/types/src/examples/fib.d.ts.map +0 -1
- package/dist/types/src/examples/index.d.ts +0 -4
- package/dist/types/src/examples/index.d.ts.map +0 -1
- package/dist/types/src/examples/reply.d.ts.map +0 -1
- package/dist/types/src/examples/sleep.d.ts.map +0 -1
- package/dist/types/src/executor/executor.d.ts +0 -14
- package/dist/types/src/executor/executor.d.ts.map +0 -1
- package/dist/types/src/executor/index.d.ts +0 -2
- package/dist/types/src/executor/index.d.ts.map +0 -1
- package/dist/types/src/schema.d.ts +0 -43
- package/dist/types/src/schema.d.ts.map +0 -1
- package/dist/types/src/services/database.d.ts +0 -67
- package/dist/types/src/services/database.d.ts.map +0 -1
- package/dist/types/src/services/function-invocation-service.test.d.ts +0 -2
- package/dist/types/src/services/function-invocation-service.test.d.ts.map +0 -1
- package/dist/types/src/services/local-function-execution.d.ts +0 -34
- package/dist/types/src/services/local-function-execution.d.ts.map +0 -1
- package/dist/types/src/services/remote-function-execution-service.d.ts +0 -22
- package/dist/types/src/services/remote-function-execution-service.d.ts.map +0 -1
- package/dist/types/src/services/service-container.d.ts +0 -57
- package/dist/types/src/services/service-container.d.ts.map +0 -1
- package/dist/types/src/services/service-registry.d.ts +0 -31
- package/dist/types/src/services/service-registry.d.ts.map +0 -1
- package/dist/types/src/services/service-registry.test.d.ts +0 -2
- package/dist/types/src/services/service-registry.test.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -3
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/layer.d.ts +0 -18
- package/dist/types/src/testing/layer.d.ts.map +0 -1
- package/dist/types/src/testing/logger.d.ts +0 -5
- package/dist/types/src/testing/logger.d.ts.map +0 -1
- package/dist/types/src/testing/persist-database.test.d.ts +0 -2
- package/dist/types/src/testing/persist-database.test.d.ts.map +0 -1
- package/dist/types/src/testing/services.d.ts +0 -59
- package/dist/types/src/testing/services.d.ts.map +0 -1
- package/dist/types/src/trace.d.ts +0 -122
- package/dist/types/src/trace.d.ts.map +0 -1
- package/dist/types/src/translations.d.ts +0 -12
- package/dist/types/src/translations.d.ts.map +0 -1
- package/dist/types/src/triggers/index.d.ts +0 -4
- package/dist/types/src/triggers/index.d.ts.map +0 -1
- package/dist/types/src/triggers/input-builder.d.ts +0 -3
- package/dist/types/src/triggers/input-builder.d.ts.map +0 -1
- package/dist/types/src/triggers/invocation-tracer.d.ts +0 -37
- package/dist/types/src/triggers/invocation-tracer.d.ts.map +0 -1
- package/dist/types/src/triggers/trigger-dispatcher.d.ts +0 -78
- package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +0 -1
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +0 -2
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +0 -1
- package/dist/types/src/triggers/trigger-state-store.d.ts +0 -28
- package/dist/types/src/triggers/trigger-state-store.d.ts.map +0 -1
- package/dist/types/src/types.d.ts.map +0 -1
- package/src/bundler/bundler.test.ts +0 -58
- package/src/bundler/bundler.ts +0 -295
- package/src/bundler/index.ts +0 -5
- package/src/edge/functions.ts +0 -67
- package/src/edge/index.ts +0 -9
- package/src/examples/index.ts +0 -7
- package/src/executor/executor.ts +0 -58
- package/src/executor/index.ts +0 -5
- package/src/services/database.ts +0 -175
- package/src/services/function-invocation-service.test.ts +0 -81
- package/src/services/local-function-execution.ts +0 -153
- package/src/services/remote-function-execution-service.ts +0 -63
- package/src/services/service-container.ts +0 -115
- package/src/services/service-registry.test.ts +0 -45
- package/src/services/service-registry.ts +0 -63
- package/src/testing/index.ts +0 -6
- package/src/testing/layer.ts +0 -114
- package/src/testing/logger.ts +0 -17
- package/src/testing/persist-database.test.ts +0 -87
- package/src/testing/services.ts +0 -115
- package/src/trace.ts +0 -178
- package/src/translations.ts +0 -20
- package/src/triggers/index.ts +0 -7
- package/src/triggers/input-builder.ts +0 -35
- package/src/triggers/invocation-tracer.ts +0 -101
- package/src/triggers/trigger-dispatcher.test.ts +0 -665
- package/src/triggers/trigger-dispatcher.ts +0 -533
- package/src/triggers/trigger-state-store.ts +0 -61
- package/src/types.ts +0 -218
- /package/dist/types/src/{examples → example}/fib.d.ts +0 -0
- /package/dist/types/src/{examples → example}/reply.d.ts +0 -0
- /package/dist/types/src/{examples → example}/sleep.d.ts +0 -0
- /package/src/{examples → example}/fib.ts +0 -0
- /package/src/{examples → example}/reply.ts +0 -0
- /package/src/{examples → example}/sleep.ts +0 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/functions",
|
|
3
|
-
"version": "0.8.4-main.
|
|
4
|
-
"description": "Functions API
|
|
3
|
+
"version": "0.8.4-main.548089c",
|
|
4
|
+
"description": "Functions API.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
7
|
"license": "MIT",
|
|
@@ -14,37 +14,9 @@
|
|
|
14
14
|
"types": "./dist/types/src/index.d.ts",
|
|
15
15
|
"browser": "./dist/lib/browser/index.mjs",
|
|
16
16
|
"node": "./dist/lib/node-esm/index.mjs"
|
|
17
|
-
},
|
|
18
|
-
"./bundler": {
|
|
19
|
-
"source": "./src/bundler/index.ts",
|
|
20
|
-
"types": "./dist/types/src/bundler/index.d.ts",
|
|
21
|
-
"browser": "./dist/lib/browser/bundler/index.mjs",
|
|
22
|
-
"node": "./dist/lib/node-esm/bundler/index.mjs"
|
|
23
|
-
},
|
|
24
|
-
"./edge": {
|
|
25
|
-
"source": "./src/edge/index.ts",
|
|
26
|
-
"types": "./dist/types/src/edge/index.d.ts",
|
|
27
|
-
"browser": "./dist/lib/browser/edge/index.mjs",
|
|
28
|
-
"node": "./dist/lib/node-esm/edge/index.mjs"
|
|
29
|
-
},
|
|
30
|
-
"./testing": {
|
|
31
|
-
"source": "./src/testing/index.ts",
|
|
32
|
-
"types": "./dist/types/src/testing/index.d.ts",
|
|
33
|
-
"browser": "./dist/lib/browser/testing/index.mjs",
|
|
34
|
-
"node": "./dist/lib/node-esm/testing/index.mjs"
|
|
35
17
|
}
|
|
36
18
|
},
|
|
37
19
|
"types": "dist/types/src/index.d.ts",
|
|
38
|
-
"typesVersions": {
|
|
39
|
-
"*": {
|
|
40
|
-
"bundler": [
|
|
41
|
-
"dist/types/src/bundler/index.d.ts"
|
|
42
|
-
],
|
|
43
|
-
"edge": [
|
|
44
|
-
"dist/types/src/edge/index.d.ts"
|
|
45
|
-
]
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
20
|
"files": [
|
|
49
21
|
"dist",
|
|
50
22
|
"schema",
|
|
@@ -53,35 +25,16 @@
|
|
|
53
25
|
"dependencies": {
|
|
54
26
|
"@effect/platform": "0.92.1",
|
|
55
27
|
"effect": "3.18.3",
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"@dxos/
|
|
60
|
-
"@dxos/
|
|
61
|
-
"@dxos/
|
|
62
|
-
"@dxos/
|
|
63
|
-
"@dxos/
|
|
64
|
-
"@dxos/
|
|
65
|
-
"@dxos/
|
|
66
|
-
"@dxos/echo-db": "0.8.4-main.406dc2a",
|
|
67
|
-
"@dxos/echo-protocol": "0.8.4-main.406dc2a",
|
|
68
|
-
"@dxos/edge-client": "0.8.4-main.406dc2a",
|
|
69
|
-
"@dxos/echo-pipeline": "0.8.4-main.406dc2a",
|
|
70
|
-
"@dxos/echo": "0.8.4-main.406dc2a",
|
|
71
|
-
"@dxos/effect": "0.8.4-main.406dc2a",
|
|
72
|
-
"@dxos/errors": "0.8.4-main.406dc2a",
|
|
73
|
-
"@dxos/invariant": "0.8.4-main.406dc2a",
|
|
74
|
-
"@dxos/keys": "0.8.4-main.406dc2a",
|
|
75
|
-
"@dxos/kv-store": "0.8.4-main.406dc2a",
|
|
76
|
-
"@dxos/log": "0.8.4-main.406dc2a",
|
|
77
|
-
"@dxos/node-std": "0.8.4-main.406dc2a",
|
|
78
|
-
"@dxos/protocols": "0.8.4-main.406dc2a",
|
|
79
|
-
"@dxos/live-object": "0.8.4-main.406dc2a",
|
|
80
|
-
"@dxos/schema": "0.8.4-main.406dc2a",
|
|
81
|
-
"@dxos/util": "0.8.4-main.406dc2a"
|
|
82
|
-
},
|
|
83
|
-
"devDependencies": {
|
|
84
|
-
"@dxos/agent": "0.8.4-main.406dc2a"
|
|
28
|
+
"@dxos/ai": "0.8.4-main.548089c",
|
|
29
|
+
"@dxos/errors": "0.8.4-main.548089c",
|
|
30
|
+
"@dxos/invariant": "0.8.4-main.548089c",
|
|
31
|
+
"@dxos/keys": "0.8.4-main.548089c",
|
|
32
|
+
"@dxos/log": "0.8.4-main.548089c",
|
|
33
|
+
"@dxos/node-std": "0.8.4-main.548089c",
|
|
34
|
+
"@dxos/schema": "0.8.4-main.548089c",
|
|
35
|
+
"@dxos/echo-db": "0.8.4-main.548089c",
|
|
36
|
+
"@dxos/echo": "0.8.4-main.548089c",
|
|
37
|
+
"@dxos/types": "0.8.4-main.548089c"
|
|
85
38
|
},
|
|
86
39
|
"publishConfig": {
|
|
87
40
|
"access": "public"
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
import { S, defineFunction } from 'dxos:functions';
|
|
7
|
+
import {
|
|
8
|
+
FetchHttpClient,
|
|
9
|
+
HttpClient,
|
|
10
|
+
HttpClientRequest,
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
} from 'https://esm.sh/@effect/platform@0.89.0?deps=effect@3.17.0&bundle=false';
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
import { Effect, Schedule } from 'https://esm.sh/effect@3.17.0?bundle=false';
|
|
15
|
+
|
|
16
|
+
export default defineFunction({
|
|
17
|
+
key: 'dxos.org/script/forex-effect',
|
|
18
|
+
name: 'Forex Effect',
|
|
19
|
+
description: 'Returns the exchange rate between two currencies.',
|
|
20
|
+
|
|
21
|
+
inputSchema: S.Struct({
|
|
22
|
+
from: S.String.annotations({ description: 'The source currency' }),
|
|
23
|
+
to: S.String.annotations({ description: 'The target currency' }),
|
|
24
|
+
}),
|
|
25
|
+
|
|
26
|
+
outputSchema: S.String.annotations({ description: 'The exchange rate between the two currencies' }),
|
|
27
|
+
|
|
28
|
+
handler: async ({ data: { from, to } }: any) =>
|
|
29
|
+
Effect.gen(function* () {
|
|
30
|
+
const res = yield* HttpClientRequest.get(`https://free.ratesdb.com/v1/rates?from=${from}&to=${to}`).pipe(
|
|
31
|
+
HttpClient.execute,
|
|
32
|
+
Effect.flatMap((res: any) => res.json),
|
|
33
|
+
Effect.timeout('1 second'),
|
|
34
|
+
Effect.retry(Schedule.exponential(1_000).pipe(Schedule.compose(Schedule.recurs(3)))),
|
|
35
|
+
Effect.scoped,
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
return res.data.rates[to].toString();
|
|
39
|
+
}).pipe(Effect.provide(FetchHttpClient.layer)),
|
|
40
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { default as fib$ } from './fib';
|
|
6
|
+
import { default as reply$ } from './reply';
|
|
7
|
+
import { default as sleep$ } from './sleep';
|
|
8
|
+
|
|
9
|
+
export namespace Example {
|
|
10
|
+
export const fib = fib$;
|
|
11
|
+
export const reply = reply$;
|
|
12
|
+
export const sleep = sleep$;
|
|
13
|
+
}
|
package/src/handler.ts
CHANGED
|
@@ -2,20 +2,21 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import type * as Context from 'effect/Context';
|
|
6
5
|
import * as Effect from 'effect/Effect';
|
|
7
6
|
import * as Schema from 'effect/Schema';
|
|
8
|
-
import type * as Types from 'effect/Types';
|
|
9
7
|
|
|
8
|
+
import { type AiService } from '@dxos/ai';
|
|
10
9
|
import { Obj, Type } from '@dxos/echo';
|
|
11
|
-
import { type
|
|
12
|
-
import { type EchoDatabase } from '@dxos/echo-db';
|
|
10
|
+
import { type DatabaseService } from '@dxos/echo-db';
|
|
13
11
|
import { assertArgument } from '@dxos/invariant';
|
|
14
|
-
import { type DXN, type SpaceId } from '@dxos/keys';
|
|
15
|
-
import { type QueryResult } from '@dxos/protocols';
|
|
16
12
|
|
|
17
|
-
import {
|
|
18
|
-
|
|
13
|
+
import {
|
|
14
|
+
type CredentialsService,
|
|
15
|
+
type FunctionInvocationService,
|
|
16
|
+
type QueueService,
|
|
17
|
+
type TracingService,
|
|
18
|
+
} from './services';
|
|
19
|
+
import { Function } from './types';
|
|
19
20
|
import { getUserFunctionIdInMetadata, setUserFunctionIdInMetadata } from './url';
|
|
20
21
|
|
|
21
22
|
// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
|
|
@@ -23,6 +24,22 @@ import { getUserFunctionIdInMetadata, setUserFunctionIdInMetadata } from './url'
|
|
|
23
24
|
// https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
|
|
24
25
|
// https://www.npmjs.com/package/aws-lambda
|
|
25
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Services that are provided at the function call site by the caller.
|
|
29
|
+
*/
|
|
30
|
+
export type InvocationServices = TracingService;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Services that are available to invoked functions.
|
|
34
|
+
*/
|
|
35
|
+
export type FunctionServices =
|
|
36
|
+
| InvocationServices
|
|
37
|
+
| AiService.AiService
|
|
38
|
+
| CredentialsService
|
|
39
|
+
| DatabaseService
|
|
40
|
+
| QueueService
|
|
41
|
+
| FunctionInvocationService;
|
|
42
|
+
|
|
26
43
|
/**
|
|
27
44
|
* Function handler.
|
|
28
45
|
*/
|
|
@@ -38,58 +55,15 @@ export type FunctionHandler<TData = {}, TOutput = any> = (params: {
|
|
|
38
55
|
* This will be the payload from the trigger or other data passed into the function in a workflow.
|
|
39
56
|
*/
|
|
40
57
|
data: TData;
|
|
41
|
-
}) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any,
|
|
58
|
+
}) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any, FunctionServices>;
|
|
42
59
|
|
|
43
60
|
/**
|
|
44
61
|
* Function context.
|
|
45
62
|
*/
|
|
46
63
|
export interface FunctionContext {
|
|
47
|
-
|
|
48
|
-
* Space from which the function was invoked.
|
|
49
|
-
*/
|
|
50
|
-
space: SpaceAPI | undefined;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Resolves a service available to the function.
|
|
54
|
-
* @throws if the service is not available.
|
|
55
|
-
*/
|
|
56
|
-
getService: <T extends Context.Tag<any, any>>(tag: T) => Context.Tag.Service<T>;
|
|
57
|
-
|
|
58
|
-
getSpace: (spaceId: SpaceId) => Promise<SpaceAPI>;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export interface FunctionContextAi {
|
|
62
|
-
// TODO(dmaretskyi): Refer to cloudflare AI docs for more comprehensive typedefs.
|
|
63
|
-
run(model: string, inputs: any, options?: any): Promise<any>;
|
|
64
|
+
// TODO(dmaretskyi):
|
|
64
65
|
}
|
|
65
66
|
|
|
66
|
-
//
|
|
67
|
-
// API.
|
|
68
|
-
//
|
|
69
|
-
|
|
70
|
-
// TODO(dmaretskyi): Temporary API to get the queues working.
|
|
71
|
-
// TODO(dmaretskyi): To be replaced with integrating queues into echo.
|
|
72
|
-
export interface QueuesAPI {
|
|
73
|
-
queryQueue(queue: DXN, options?: {}): Promise<QueryResult>;
|
|
74
|
-
insertIntoQueue(queue: DXN, objects: HasId[]): Promise<void>;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Space interface available to functions.
|
|
79
|
-
*/
|
|
80
|
-
export interface SpaceAPI {
|
|
81
|
-
get id(): SpaceId;
|
|
82
|
-
get db(): EchoDatabase;
|
|
83
|
-
|
|
84
|
-
// TODO(dmaretskyi): Align with echo api: queues.get(id).append(items);
|
|
85
|
-
get queues(): QueuesAPI;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// TODO(wittjosiah): Queues are incompatible.
|
|
89
|
-
const __assertFunctionSpaceIsCompatibleWithTheClientSpace = () => {
|
|
90
|
-
// const _: SpaceAPI = {} as Space;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
67
|
const typeId = Symbol.for('@dxos/functions/FunctionDefinition');
|
|
94
68
|
|
|
95
69
|
export type FunctionDefinition<T = any, O = any> = {
|
|
@@ -107,23 +81,25 @@ export type FunctionDefinition<T = any, O = any> = {
|
|
|
107
81
|
* deployedFunctionId:
|
|
108
82
|
* - Backend deployment ID assigned by the EDGE function service (typically a UUID).
|
|
109
83
|
* - Used for remote invocation via `FunctionInvocationService` → `RemoteFunctionExecutionService`.
|
|
110
|
-
* - Persisted on the corresponding ECHO `
|
|
84
|
+
* - Persisted on the corresponding ECHO `Function.Function` object's metadata under the
|
|
111
85
|
* `FUNCTIONS_META_KEY` and retrieved with `getUserFunctionIdInMetadata`.
|
|
112
86
|
*/
|
|
113
87
|
deployedFunctionId?: string;
|
|
114
88
|
};
|
|
115
89
|
};
|
|
116
90
|
|
|
91
|
+
export type FunctionProps<T, O> = {
|
|
92
|
+
key: string;
|
|
93
|
+
name: string;
|
|
94
|
+
description?: string;
|
|
95
|
+
inputSchema: Schema.Schema<T, any>;
|
|
96
|
+
outputSchema?: Schema.Schema<O, any>;
|
|
97
|
+
handler: FunctionHandler<T, O>;
|
|
98
|
+
};
|
|
99
|
+
|
|
117
100
|
// TODO(dmaretskyi): Output type doesn't get typechecked.
|
|
118
101
|
export const defineFunction: {
|
|
119
|
-
<I, O>(params:
|
|
120
|
-
key: string;
|
|
121
|
-
name: string;
|
|
122
|
-
description?: string;
|
|
123
|
-
inputSchema: Schema.Schema<I, any>;
|
|
124
|
-
outputSchema?: Schema.Schema<O, any>;
|
|
125
|
-
handler: Types.NoInfer<FunctionHandler<I, O>>;
|
|
126
|
-
}): FunctionDefinition<I, O>;
|
|
102
|
+
<I, O>(params: FunctionProps<I, O>): FunctionDefinition<I, O>;
|
|
127
103
|
} = ({ key, name, description, inputSchema, outputSchema = Schema.Any, handler }) => {
|
|
128
104
|
if (!Schema.isSchema(inputSchema)) {
|
|
129
105
|
throw new Error('Input schema must be a valid schema');
|
|
@@ -169,7 +145,7 @@ export const defineFunction: {
|
|
|
169
145
|
inputSchema,
|
|
170
146
|
outputSchema,
|
|
171
147
|
handler: handlerWithSpan,
|
|
172
|
-
};
|
|
148
|
+
} satisfies FunctionDefinition.Any;
|
|
173
149
|
};
|
|
174
150
|
|
|
175
151
|
export const FunctionDefinition = {
|
|
@@ -177,12 +153,12 @@ export const FunctionDefinition = {
|
|
|
177
153
|
isFunction: (value: unknown): value is FunctionDefinition.Any => {
|
|
178
154
|
return typeof value === 'object' && value !== null && Symbol.for('@dxos/functions/FunctionDefinition') in value;
|
|
179
155
|
},
|
|
180
|
-
serialize: (functionDef: FunctionDefinition.Any):
|
|
156
|
+
serialize: (functionDef: FunctionDefinition.Any): Function.Function => {
|
|
181
157
|
assertArgument(FunctionDefinition.isFunction(functionDef), 'functionDef');
|
|
182
158
|
return serializeFunction(functionDef);
|
|
183
159
|
},
|
|
184
|
-
deserialize: (functionObj:
|
|
185
|
-
assertArgument(Obj.instanceOf(
|
|
160
|
+
deserialize: (functionObj: Function.Function): FunctionDefinition.Any => {
|
|
161
|
+
assertArgument(Obj.instanceOf(Function.Function, functionObj), 'functionObj');
|
|
186
162
|
return deserializeFunction(functionObj);
|
|
187
163
|
},
|
|
188
164
|
};
|
|
@@ -192,8 +168,8 @@ export declare namespace FunctionDefinition {
|
|
|
192
168
|
export type Output<T extends FunctionDefinition> = T extends FunctionDefinition<any, infer O> ? O : never;
|
|
193
169
|
}
|
|
194
170
|
|
|
195
|
-
export const serializeFunction = (functionDef: FunctionDefinition<any, any>):
|
|
196
|
-
const fn =
|
|
171
|
+
export const serializeFunction = (functionDef: FunctionDefinition<any, any>): Function.Function => {
|
|
172
|
+
const fn = Function.make({
|
|
197
173
|
key: functionDef.key,
|
|
198
174
|
name: functionDef.name,
|
|
199
175
|
version: '0.1.0',
|
|
@@ -207,7 +183,7 @@ export const serializeFunction = (functionDef: FunctionDefinition<any, any>): Fu
|
|
|
207
183
|
return fn;
|
|
208
184
|
};
|
|
209
185
|
|
|
210
|
-
export const deserializeFunction = (functionObj:
|
|
186
|
+
export const deserializeFunction = (functionObj: Function.Function): FunctionDefinition<unknown, unknown> => {
|
|
211
187
|
return {
|
|
212
188
|
[typeId]: true,
|
|
213
189
|
// TODO(dmaretskyi): Fix key.
|
package/src/index.ts
CHANGED
|
@@ -3,12 +3,8 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
export * from './errors';
|
|
6
|
+
export * from './example';
|
|
6
7
|
export * from './handler';
|
|
7
|
-
export * from './
|
|
8
|
-
export * from './trace';
|
|
8
|
+
export * from './services';
|
|
9
9
|
export * from './types';
|
|
10
10
|
export * from './url';
|
|
11
|
-
export * from './triggers';
|
|
12
|
-
export * from './services';
|
|
13
|
-
export * from './executor';
|
|
14
|
-
export * as exampleFunctions from './examples';
|
|
@@ -11,9 +11,8 @@ import * as Layer from 'effect/Layer';
|
|
|
11
11
|
import * as Redacted from 'effect/Redacted';
|
|
12
12
|
|
|
13
13
|
import { Query } from '@dxos/echo';
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
import { DatabaseService } from './database';
|
|
14
|
+
import { DatabaseService } from '@dxos/echo-db';
|
|
15
|
+
import { AccessToken } from '@dxos/types';
|
|
17
16
|
|
|
18
17
|
export type CredentialQuery = {
|
|
19
18
|
service?: string;
|
|
@@ -84,7 +83,7 @@ export class CredentialsService extends Context.Tag('@dxos/functions/Credentials
|
|
|
84
83
|
Effect.gen(function* () {
|
|
85
84
|
const dbService = yield* DatabaseService;
|
|
86
85
|
const queryCredentials = async (query: CredentialQuery): Promise<ServiceCredential[]> => {
|
|
87
|
-
const { objects: accessTokens } = await dbService.db.query(Query.type(
|
|
86
|
+
const { objects: accessTokens } = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
|
|
88
87
|
return accessTokens
|
|
89
88
|
.filter((accessToken) => accessToken.source === query.service)
|
|
90
89
|
.map((accessToken) => ({
|
|
@@ -17,12 +17,18 @@ export const ComputeEventPayload = Schema.Union(
|
|
|
17
17
|
Schema.Struct({
|
|
18
18
|
type: Schema.Literal('begin-compute'),
|
|
19
19
|
nodeId: Schema.String,
|
|
20
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Names of the inputs begin computed.
|
|
22
|
+
*/
|
|
23
|
+
inputs: Schema.Array(Schema.String),
|
|
21
24
|
}),
|
|
22
25
|
Schema.Struct({
|
|
23
26
|
type: Schema.Literal('end-compute'),
|
|
24
27
|
nodeId: Schema.String,
|
|
25
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Names of the outputs computed.
|
|
30
|
+
*/
|
|
31
|
+
outputs: Schema.Array(Schema.String),
|
|
26
32
|
}),
|
|
27
33
|
Schema.Struct({
|
|
28
34
|
type: Schema.Literal('compute-input'),
|
|
@@ -3,21 +3,8 @@
|
|
|
3
3
|
//
|
|
4
4
|
import * as Context from 'effect/Context';
|
|
5
5
|
import * as Effect from 'effect/Effect';
|
|
6
|
-
import * as Layer from 'effect/Layer';
|
|
7
6
|
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
import { type FunctionDefinition } from '../handler';
|
|
11
|
-
|
|
12
|
-
import { CredentialsService } from './credentials';
|
|
13
|
-
import { DatabaseService } from './database';
|
|
14
|
-
import {
|
|
15
|
-
FunctionImplementationResolver,
|
|
16
|
-
type InvocationServices,
|
|
17
|
-
LocalFunctionExecutionService,
|
|
18
|
-
} from './local-function-execution';
|
|
19
|
-
import { QueueService } from './queues';
|
|
20
|
-
import { RemoteFunctionExecutionService } from './remote-function-execution-service';
|
|
7
|
+
import { type FunctionDefinition, type InvocationServices } from '../handler';
|
|
21
8
|
|
|
22
9
|
export class FunctionInvocationService extends Context.Tag('@dxos/functions/FunctionInvocationService')<
|
|
23
10
|
FunctionInvocationService,
|
|
@@ -25,60 +12,9 @@ export class FunctionInvocationService extends Context.Tag('@dxos/functions/Func
|
|
|
25
12
|
invokeFunction<I, O>(functionDef: FunctionDefinition<I, O>, input: I): Effect.Effect<O, never, InvocationServices>;
|
|
26
13
|
}
|
|
27
14
|
>() {
|
|
28
|
-
static invokeFunction =
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
Effect.
|
|
33
|
-
const localExecutioner = yield* LocalFunctionExecutionService;
|
|
34
|
-
const remoteExecutioner = yield* RemoteFunctionExecutionService;
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
invokeFunction: <I, O>(
|
|
38
|
-
functionDef: FunctionDefinition<I, O>,
|
|
39
|
-
input: I,
|
|
40
|
-
): Effect.Effect<O, never, InvocationServices> =>
|
|
41
|
-
Effect.gen(function* () {
|
|
42
|
-
if (functionDef.meta?.deployedFunctionId) {
|
|
43
|
-
return yield* remoteExecutioner.callFunction<I, O>(functionDef.meta.deployedFunctionId, input);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return yield* localExecutioner.invokeFunction(functionDef, input);
|
|
47
|
-
}),
|
|
48
|
-
} satisfies Context.Tag.Service<FunctionInvocationService>;
|
|
49
|
-
}),
|
|
50
|
-
);
|
|
51
|
-
|
|
52
|
-
// TODO(dmaretskyi): Don't provide `FunctionImplementationResolver`.
|
|
53
|
-
static layerTest = ({
|
|
54
|
-
functions = [],
|
|
55
|
-
}: {
|
|
56
|
-
functions?: FunctionDefinition<any, any>[];
|
|
57
|
-
} = {}): Layer.Layer<
|
|
58
|
-
FunctionInvocationService,
|
|
59
|
-
never,
|
|
60
|
-
AiService.AiService | CredentialsService | DatabaseService | QueueService
|
|
61
|
-
> =>
|
|
62
|
-
FunctionInvocationService.layer.pipe(
|
|
63
|
-
Layer.provide(LocalFunctionExecutionService.layerLive),
|
|
64
|
-
Layer.provide(FunctionImplementationResolver.layerTest({ functions })),
|
|
65
|
-
Layer.provide(RemoteFunctionExecutionService.layerMock),
|
|
66
|
-
);
|
|
67
|
-
|
|
68
|
-
// TODO(dmaretskyi): This shouldn't default to all services being not available.
|
|
69
|
-
// TODO(dmaretskyi): Don't provide `FunctionImplementationResolver`.
|
|
70
|
-
/**
|
|
71
|
-
* @deprecated Use {@link layerTest} instead.
|
|
72
|
-
*/
|
|
73
|
-
static layerTestMocked = ({
|
|
74
|
-
functions,
|
|
75
|
-
}: {
|
|
76
|
-
functions: FunctionDefinition<any, any>[];
|
|
77
|
-
}): Layer.Layer<FunctionInvocationService> =>
|
|
78
|
-
FunctionInvocationService.layerTest({ functions }).pipe(
|
|
79
|
-
Layer.provide(AiService.notAvailable),
|
|
80
|
-
Layer.provide(CredentialsService.configuredLayer([])),
|
|
81
|
-
Layer.provide(DatabaseService.notAvailable),
|
|
82
|
-
Layer.provide(QueueService.notAvailable),
|
|
83
|
-
);
|
|
15
|
+
static invokeFunction = <I, O>(
|
|
16
|
+
functionDef: FunctionDefinition<I, O>,
|
|
17
|
+
input: I,
|
|
18
|
+
): Effect.Effect<O, never, FunctionInvocationService | InvocationServices> =>
|
|
19
|
+
Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.invokeFunction)(functionDef, input);
|
|
84
20
|
}
|
package/src/services/index.ts
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
export { DatabaseService } from '@dxos/echo-db';
|
|
6
|
+
|
|
5
7
|
export * from './credentials';
|
|
6
|
-
export
|
|
8
|
+
export { ConfiguredCredentialsService, type ServiceCredential } from './credentials';
|
|
7
9
|
export * from './event-logger';
|
|
10
|
+
export { createEventLogger, createDefectLogger } from './event-logger';
|
|
8
11
|
export * from './function-invocation-service';
|
|
9
|
-
export * from './local-function-execution';
|
|
10
12
|
export * from './queues';
|
|
11
|
-
export * from './service-container';
|
|
12
13
|
export * from './tracing';
|
|
13
|
-
export * from './remote-function-execution-service';
|
package/src/services/tracing.ts
CHANGED
|
@@ -9,9 +9,7 @@ import * as Layer from 'effect/Layer';
|
|
|
9
9
|
import { AgentStatus } from '@dxos/ai';
|
|
10
10
|
import { Obj } from '@dxos/echo';
|
|
11
11
|
import type { ObjectId } from '@dxos/echo/internal';
|
|
12
|
-
import
|
|
13
|
-
import { log } from '@dxos/log';
|
|
14
|
-
import { DataType } from '@dxos/schema';
|
|
12
|
+
import { Message } from '@dxos/types';
|
|
15
13
|
|
|
16
14
|
/**
|
|
17
15
|
* Provides a way for compute primitives (functions, workflows, tools)
|
|
@@ -32,29 +30,12 @@ export class TracingService extends Context.Tag('@dxos/functions/TracingService'
|
|
|
32
30
|
write: (event: Obj.Any) => void;
|
|
33
31
|
}
|
|
34
32
|
>() {
|
|
35
|
-
static noop: Context.Tag.Service<TracingService> = {
|
|
36
|
-
|
|
37
|
-
static layerNoop = Layer.succeed(TracingService, TracingService.noop);
|
|
38
|
-
|
|
39
|
-
static console: Context.Tag.Service<TracingService> = {
|
|
40
|
-
write: (event) => {
|
|
41
|
-
console.log(event);
|
|
42
|
-
},
|
|
33
|
+
static noop: Context.Tag.Service<TracingService> = {
|
|
43
34
|
getTraceContext: () => ({}),
|
|
35
|
+
write: () => {},
|
|
44
36
|
};
|
|
45
37
|
|
|
46
|
-
static
|
|
47
|
-
|
|
48
|
-
static layerLogInfo = () =>
|
|
49
|
-
Layer.succeed(TracingService, {
|
|
50
|
-
write: (event) => {
|
|
51
|
-
if (Obj.instanceOf(AgentStatus, event)) {
|
|
52
|
-
log.info('status', { message: event.message });
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
getTraceContext: () => ({}),
|
|
56
|
-
});
|
|
57
|
-
|
|
38
|
+
static layerNoop: Layer.Layer<TracingService> = Layer.succeed(TracingService, TracingService.noop);
|
|
58
39
|
/**
|
|
59
40
|
* Creates a TracingService layer that emits events to the parent tracing service.
|
|
60
41
|
*/
|
|
@@ -71,22 +52,6 @@ export class TracingService extends Context.Tag('@dxos/functions/TracingService'
|
|
|
71
52
|
}),
|
|
72
53
|
);
|
|
73
54
|
|
|
74
|
-
static layerQueue = (queue: Queue) =>
|
|
75
|
-
Layer.effect(
|
|
76
|
-
TracingService,
|
|
77
|
-
Effect.gen(function* () {
|
|
78
|
-
// TODO(dmaretskyi): Batching.
|
|
79
|
-
return {
|
|
80
|
-
write: (event) => queue.append([event]),
|
|
81
|
-
getTraceContext: () => ({
|
|
82
|
-
debugInfo: {
|
|
83
|
-
queue: queue.dxn.toString(),
|
|
84
|
-
},
|
|
85
|
-
}),
|
|
86
|
-
};
|
|
87
|
-
}),
|
|
88
|
-
);
|
|
89
|
-
|
|
90
55
|
/**
|
|
91
56
|
* Emit the current human-readable execution status.
|
|
92
57
|
*/
|
|
@@ -105,11 +70,11 @@ export class TracingService extends Context.Tag('@dxos/functions/TracingService'
|
|
|
105
70
|
});
|
|
106
71
|
|
|
107
72
|
static emitConverationMessage: (
|
|
108
|
-
data: Obj.MakeProps<typeof
|
|
73
|
+
data: Obj.MakeProps<typeof Message.Message>,
|
|
109
74
|
) => Effect.Effect<void, never, TracingService> = Effect.fnUntraced(function* (data) {
|
|
110
75
|
const tracing = yield* TracingService;
|
|
111
76
|
tracing.write(
|
|
112
|
-
Obj.make(
|
|
77
|
+
Obj.make(Message.Message, {
|
|
113
78
|
parentMessage: tracing.getTraceContext().parentMessage,
|
|
114
79
|
...data,
|
|
115
80
|
properties: {
|
|
@@ -138,6 +103,6 @@ export namespace TracingService {
|
|
|
138
103
|
}
|
|
139
104
|
|
|
140
105
|
/**
|
|
141
|
-
* Goes into {@link
|
|
106
|
+
* Goes into {@link Message['properties']}
|
|
142
107
|
*/
|
|
143
108
|
export const MESSAGE_PROPERTY_TOOL_CALL_ID = 'toolCallId' as const;
|
|
@@ -4,36 +4,15 @@
|
|
|
4
4
|
|
|
5
5
|
import * as Schema from 'effect/Schema';
|
|
6
6
|
|
|
7
|
-
import { Type } from '@dxos/echo';
|
|
7
|
+
import { Obj, Type } from '@dxos/echo';
|
|
8
8
|
import { JsonSchemaType, LabelAnnotation, Ref } from '@dxos/echo/internal';
|
|
9
|
-
import { DataType } from '@dxos/schema';
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Source script.
|
|
15
|
-
*/
|
|
16
|
-
export const ScriptType = Schema.Struct({
|
|
17
|
-
name: Schema.optional(Schema.String),
|
|
18
|
-
description: Schema.optional(Schema.String),
|
|
19
|
-
// TODO(burdon): Change to hash of deployed content.
|
|
20
|
-
// Whether source has changed since last deploy.
|
|
21
|
-
changed: Schema.optional(Schema.Boolean),
|
|
22
|
-
source: Ref(DataType.Text),
|
|
23
|
-
}).pipe(
|
|
24
|
-
Type.Obj({
|
|
25
|
-
typename: 'dxos.org/type/Script',
|
|
26
|
-
version: '0.1.0',
|
|
27
|
-
}),
|
|
28
|
-
LabelAnnotation.set(['name']),
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
export interface ScriptType extends Schema.Schema.Type<typeof ScriptType> {}
|
|
10
|
+
import { Script } from './Script';
|
|
32
11
|
|
|
33
12
|
/**
|
|
34
13
|
* Function deployment.
|
|
35
14
|
*/
|
|
36
|
-
export const
|
|
15
|
+
export const Function = Schema.Struct({
|
|
37
16
|
/**
|
|
38
17
|
* Global registry ID.
|
|
39
18
|
* NOTE: The `key` property refers to the original registry entry.
|
|
@@ -51,9 +30,14 @@ export const FunctionType = Schema.Struct({
|
|
|
51
30
|
|
|
52
31
|
description: Schema.optional(Schema.String),
|
|
53
32
|
|
|
33
|
+
/**
|
|
34
|
+
* ISO date string of the last deployment.
|
|
35
|
+
*/
|
|
36
|
+
updated: Schema.optional(Schema.String),
|
|
37
|
+
|
|
54
38
|
// Reference to a source script if it exists within ECHO.
|
|
55
39
|
// TODO(burdon): Don't ref ScriptType directly (core).
|
|
56
|
-
source: Schema.optional(Ref(
|
|
40
|
+
source: Schema.optional(Ref(Script)),
|
|
57
41
|
|
|
58
42
|
inputSchema: Schema.optional(JsonSchemaType),
|
|
59
43
|
outputSchema: Schema.optional(JsonSchemaType),
|
|
@@ -67,5 +51,6 @@ export const FunctionType = Schema.Struct({
|
|
|
67
51
|
}),
|
|
68
52
|
LabelAnnotation.set(['name']),
|
|
69
53
|
);
|
|
54
|
+
export interface Function extends Schema.Schema.Type<typeof Function> {}
|
|
70
55
|
|
|
71
|
-
export
|
|
56
|
+
export const make = (props: Obj.MakeProps<typeof Function>) => Obj.make(Function, props);
|