@dxos/functions 0.8.3 → 0.8.4-main.28f8d3d
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/bundler/index.mjs +22 -10
- package/dist/lib/browser/bundler/index.mjs.map +3 -3
- package/dist/lib/browser/chunk-M2OIFLGE.mjs +486 -0
- package/dist/lib/browser/chunk-M2OIFLGE.mjs.map +7 -0
- package/dist/lib/browser/edge/index.mjs +2 -2
- package/dist/lib/browser/edge/index.mjs.map +1 -1
- package/dist/lib/browser/index.mjs +51 -37
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +39 -8
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node-esm/bundler/index.mjs +22 -10
- package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
- package/dist/lib/node-esm/chunk-K7REX6H4.mjs +488 -0
- package/dist/lib/node-esm/chunk-K7REX6H4.mjs.map +7 -0
- package/dist/lib/node-esm/edge/index.mjs +2 -2
- package/dist/lib/node-esm/edge/index.mjs.map +1 -1
- package/dist/lib/node-esm/index.mjs +51 -37
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +39 -8
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- package/dist/types/src/bundler/bundler.d.ts +1 -2
- package/dist/types/src/bundler/bundler.d.ts.map +1 -1
- package/dist/types/src/edge/functions.d.ts +1 -1
- package/dist/types/src/edge/functions.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +70 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/executor/executor.d.ts +3 -0
- package/dist/types/src/executor/executor.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +16 -15
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/schema.d.ts +5 -5
- package/dist/types/src/services/credentials.d.ts +7 -3
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/database.d.ts +71 -3
- package/dist/types/src/services/database.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +65 -30
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/index.d.ts +2 -2
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/local-function-execution.d.ts +12 -0
- package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
- package/dist/types/src/services/queues.d.ts +32 -6
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/remote-function-execution-service.d.ts +15 -0
- package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -0
- package/dist/types/src/services/service-container.d.ts +29 -17
- package/dist/types/src/services/service-container.d.ts.map +1 -1
- package/dist/types/src/services/service-registry.d.ts +29 -0
- package/dist/types/src/services/service-registry.d.ts.map +1 -0
- package/dist/types/src/services/service-registry.test.d.ts +2 -0
- package/dist/types/src/services/service-registry.test.d.ts.map +1 -0
- package/dist/types/src/services/tracing.d.ts +13 -2
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +1 -0
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +10 -0
- package/dist/types/src/testing/layer.d.ts.map +1 -0
- package/dist/types/src/testing/logger.d.ts +3 -3
- package/dist/types/src/testing/logger.d.ts.map +1 -1
- package/dist/types/src/testing/services.d.ts +55 -9
- package/dist/types/src/testing/services.d.ts.map +1 -1
- package/dist/types/src/trace.d.ts +3 -5
- package/dist/types/src/trace.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +9 -9
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +7 -7
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +36 -26
- package/src/bundler/bundler.test.ts +1 -1
- package/src/bundler/bundler.ts +26 -4
- package/src/edge/functions.ts +1 -1
- package/src/errors.ts +13 -0
- package/src/executor/executor.ts +20 -13
- package/src/handler.ts +34 -25
- package/src/index.ts +1 -0
- package/src/services/credentials.ts +47 -4
- package/src/services/database.ts +91 -3
- package/src/services/event-logger.ts +68 -37
- package/src/services/index.ts +2 -2
- package/src/services/local-function-execution.ts +74 -0
- package/src/services/queues.ts +51 -11
- package/src/services/{function-call-service.ts → remote-function-execution-service.ts} +7 -5
- package/src/services/service-container.ts +47 -42
- package/src/services/service-registry.test.ts +42 -0
- package/src/services/service-registry.ts +59 -0
- package/src/services/tracing.ts +19 -2
- package/src/testing/index.ts +1 -0
- package/src/testing/layer.ts +31 -0
- package/src/testing/logger.ts +4 -4
- package/src/testing/services.ts +97 -14
- package/src/trace.ts +3 -5
- package/src/translations.ts +4 -4
- package/src/types.ts +1 -1
- package/dist/lib/browser/chunk-WEFZUEL2.mjs +0 -300
- package/dist/lib/browser/chunk-WEFZUEL2.mjs.map +0 -7
- package/dist/lib/node/bundler/index.cjs +0 -260
- package/dist/lib/node/bundler/index.cjs.map +0 -7
- package/dist/lib/node/chunk-IJAE7FZK.cjs +0 -320
- package/dist/lib/node/chunk-IJAE7FZK.cjs.map +0 -7
- package/dist/lib/node/edge/index.cjs +0 -94
- package/dist/lib/node/edge/index.cjs.map +0 -7
- package/dist/lib/node/index.cjs +0 -522
- package/dist/lib/node/index.cjs.map +0 -7
- package/dist/lib/node/meta.json +0 -1
- package/dist/lib/node/testing/index.cjs +0 -43
- package/dist/lib/node/testing/index.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-LIYPMWNQ.mjs +0 -302
- package/dist/lib/node-esm/chunk-LIYPMWNQ.mjs.map +0 -7
- package/dist/types/src/services/ai.d.ts +0 -12
- package/dist/types/src/services/ai.d.ts.map +0 -1
- package/dist/types/src/services/function-call-service.d.ts +0 -16
- package/dist/types/src/services/function-call-service.d.ts.map +0 -1
- package/src/services/ai.ts +0 -32
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/functions",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.4-main.28f8d3d",
|
|
4
4
|
"description": "Functions API and runtime.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -9,22 +9,26 @@
|
|
|
9
9
|
"sideEffects": true,
|
|
10
10
|
"type": "module",
|
|
11
11
|
"exports": {
|
|
12
|
-
".": {
|
|
13
|
-
"types": "./dist/types/src/index.d.ts",
|
|
14
|
-
"browser": "./dist/lib/browser/index.mjs",
|
|
15
|
-
"node": "./dist/lib/node-esm/index.mjs"
|
|
16
|
-
},
|
|
17
12
|
"./bundler": {
|
|
13
|
+
"source": "./src/bundler/index.ts",
|
|
18
14
|
"types": "./dist/types/src/bundler/index.d.ts",
|
|
19
15
|
"browser": "./dist/lib/browser/bundler/index.mjs",
|
|
20
16
|
"node": "./dist/lib/node-esm/bundler/index.mjs"
|
|
21
17
|
},
|
|
22
18
|
"./edge": {
|
|
19
|
+
"source": "./src/edge/index.ts",
|
|
23
20
|
"types": "./dist/types/src/edge/index.d.ts",
|
|
24
21
|
"browser": "./dist/lib/browser/edge/index.mjs",
|
|
25
22
|
"node": "./dist/lib/node-esm/edge/index.mjs"
|
|
26
23
|
},
|
|
24
|
+
".": {
|
|
25
|
+
"source": "./src/index.ts",
|
|
26
|
+
"types": "./dist/types/src/index.d.ts",
|
|
27
|
+
"browser": "./dist/lib/browser/index.mjs",
|
|
28
|
+
"node": "./dist/lib/node-esm/index.mjs"
|
|
29
|
+
},
|
|
27
30
|
"./testing": {
|
|
31
|
+
"source": "./src/testing/index.ts",
|
|
28
32
|
"types": "./dist/types/src/testing/index.d.ts",
|
|
29
33
|
"browser": "./dist/lib/browser/testing/index.mjs",
|
|
30
34
|
"node": "./dist/lib/node-esm/testing/index.mjs"
|
|
@@ -47,38 +51,44 @@
|
|
|
47
51
|
"src"
|
|
48
52
|
],
|
|
49
53
|
"dependencies": {
|
|
54
|
+
"@effect/platform": "0.90.2",
|
|
50
55
|
"@preact/signals-core": "^1.9.0",
|
|
51
56
|
"cron": "^3.1.6",
|
|
52
57
|
"cron-schedule": "^5.0.4",
|
|
53
|
-
"effect": "3.
|
|
58
|
+
"effect": "3.17.7",
|
|
54
59
|
"esbuild-wasm": "^0.16.14",
|
|
55
60
|
"express": "^4.19.2",
|
|
56
61
|
"get-port-please": "^3.1.1",
|
|
62
|
+
"i18next": "^24.2.1",
|
|
57
63
|
"iso-did": "^1.6.0",
|
|
58
64
|
"ws": "^8.14.2",
|
|
59
|
-
"@dxos/ai": "0.8.
|
|
60
|
-
"@dxos/
|
|
61
|
-
"@dxos/
|
|
62
|
-
"@dxos/
|
|
63
|
-
"@dxos/
|
|
64
|
-
"@dxos/
|
|
65
|
-
"@dxos/
|
|
66
|
-
"@dxos/echo-db": "0.8.
|
|
67
|
-
"@dxos/echo-
|
|
68
|
-
"@dxos/
|
|
69
|
-
"@dxos/
|
|
70
|
-
"@dxos/
|
|
71
|
-
"@dxos/
|
|
72
|
-
"@dxos/
|
|
73
|
-
"@dxos/
|
|
74
|
-
"@dxos/
|
|
75
|
-
"@dxos/
|
|
76
|
-
"@dxos/
|
|
65
|
+
"@dxos/ai": "0.8.4-main.28f8d3d",
|
|
66
|
+
"@dxos/async": "0.8.4-main.28f8d3d",
|
|
67
|
+
"@dxos/client": "0.8.4-main.28f8d3d",
|
|
68
|
+
"@dxos/context": "0.8.4-main.28f8d3d",
|
|
69
|
+
"@dxos/crypto": "0.8.4-main.28f8d3d",
|
|
70
|
+
"@dxos/echo": "0.8.4-main.28f8d3d",
|
|
71
|
+
"@dxos/debug": "0.8.4-main.28f8d3d",
|
|
72
|
+
"@dxos/echo-db": "0.8.4-main.28f8d3d",
|
|
73
|
+
"@dxos/echo-pipeline": "0.8.4-main.28f8d3d",
|
|
74
|
+
"@dxos/echo-protocol": "0.8.4-main.28f8d3d",
|
|
75
|
+
"@dxos/echo-schema": "0.8.4-main.28f8d3d",
|
|
76
|
+
"@dxos/edge-client": "0.8.4-main.28f8d3d",
|
|
77
|
+
"@dxos/errors": "0.8.4-main.28f8d3d",
|
|
78
|
+
"@dxos/effect": "0.8.4-main.28f8d3d",
|
|
79
|
+
"@dxos/invariant": "0.8.4-main.28f8d3d",
|
|
80
|
+
"@dxos/keys": "0.8.4-main.28f8d3d",
|
|
81
|
+
"@dxos/live-object": "0.8.4-main.28f8d3d",
|
|
82
|
+
"@dxos/log": "0.8.4-main.28f8d3d",
|
|
83
|
+
"@dxos/node-std": "0.8.4-main.28f8d3d",
|
|
84
|
+
"@dxos/protocols": "0.8.4-main.28f8d3d",
|
|
85
|
+
"@dxos/schema": "0.8.4-main.28f8d3d",
|
|
86
|
+
"@dxos/util": "0.8.4-main.28f8d3d"
|
|
77
87
|
},
|
|
78
88
|
"devDependencies": {
|
|
79
89
|
"@types/express": "^4.17.17",
|
|
80
90
|
"@types/ws": "^7.4.0",
|
|
81
|
-
"@dxos/agent": "0.8.
|
|
91
|
+
"@dxos/agent": "0.8.4-main.28f8d3d"
|
|
82
92
|
},
|
|
83
93
|
"publishConfig": {
|
|
84
94
|
"access": "public"
|
|
@@ -37,7 +37,7 @@ describe('Bundler', () => {
|
|
|
37
37
|
expect(result.error).to.not.exist;
|
|
38
38
|
});
|
|
39
39
|
|
|
40
|
-
// TODO(dmaretskyi): Flaky on CI
|
|
40
|
+
// TODO(dmaretskyi): Flaky on CI.
|
|
41
41
|
test.skip('HTTPS Import', async () => {
|
|
42
42
|
const bundler = new Bundler({ platform: 'node', sandboxedModules: [], remoteModules: {} });
|
|
43
43
|
const result = await bundler.bundle({
|
package/src/bundler/bundler.ts
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { FetchHttpClient, HttpClient } from '@effect/platform';
|
|
6
|
+
import { Duration, Effect, Schedule, pipe } from 'effect';
|
|
7
|
+
import { type BuildOptions, type BuildResult, type Loader, type Plugin, build, initialize } from 'esbuild-wasm';
|
|
7
8
|
|
|
8
9
|
import { subtleCrypto } from '@dxos/crypto';
|
|
10
|
+
import { runAndForwardErrors } from '@dxos/effect';
|
|
9
11
|
import { invariant } from '@dxos/invariant';
|
|
10
12
|
import { log } from '@dxos/log';
|
|
11
13
|
|
|
@@ -242,6 +244,9 @@ const analyzeSourceFileImports = (code: string): ParsedImport[] => {
|
|
|
242
244
|
});
|
|
243
245
|
};
|
|
244
246
|
|
|
247
|
+
const MAX_RETRIES = 5;
|
|
248
|
+
const INITIAL_DELAY = 1_000;
|
|
249
|
+
|
|
245
250
|
const httpPlugin: Plugin = {
|
|
246
251
|
name: 'http',
|
|
247
252
|
setup: (build) => {
|
|
@@ -263,8 +268,25 @@ const httpPlugin: Plugin = {
|
|
|
263
268
|
// When a URL is loaded, we want to actually download the content from the internet.
|
|
264
269
|
// This has just enough logic to be able to handle the example import from unpkg.com but in reality this would probably need to be more complex.
|
|
265
270
|
build.onLoad({ filter: /.*/, namespace: 'http-url' }, async (args) => {
|
|
266
|
-
|
|
267
|
-
|
|
271
|
+
return Effect.gen(function* () {
|
|
272
|
+
const response = yield* HttpClient.get(args.path);
|
|
273
|
+
if (response.status !== 200) {
|
|
274
|
+
throw new Error(`failed to fetch: ${response.status}`);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const text = yield* response.text;
|
|
278
|
+
return { contents: text, loader: 'jsx' as Loader };
|
|
279
|
+
}).pipe(
|
|
280
|
+
Effect.retry(
|
|
281
|
+
pipe(
|
|
282
|
+
Schedule.exponential(Duration.millis(INITIAL_DELAY)),
|
|
283
|
+
Schedule.jittered,
|
|
284
|
+
Schedule.intersect(Schedule.recurs(MAX_RETRIES - 1)),
|
|
285
|
+
),
|
|
286
|
+
),
|
|
287
|
+
Effect.provide(FetchHttpClient.layer),
|
|
288
|
+
runAndForwardErrors,
|
|
289
|
+
);
|
|
268
290
|
});
|
|
269
291
|
},
|
|
270
292
|
};
|
package/src/edge/functions.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { type Client } from '@dxos/client';
|
|
|
8
8
|
import { createEdgeIdentity } from '@dxos/client/edge';
|
|
9
9
|
import { EdgeHttpClient } from '@dxos/edge-client';
|
|
10
10
|
import { invariant } from '@dxos/invariant';
|
|
11
|
-
import type
|
|
11
|
+
import { type PublicKey } from '@dxos/keys';
|
|
12
12
|
import { log } from '@dxos/log';
|
|
13
13
|
import { type UploadFunctionResponseBody } from '@dxos/protocols';
|
|
14
14
|
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { BaseError } from '@dxos/errors';
|
|
6
|
+
|
|
7
|
+
export class ServiceNotAvailableError extends BaseError.extend('SERVICE_NOT_AVAILABLE') {
|
|
8
|
+
constructor(serviceName: string) {
|
|
9
|
+
super(`Service not available: ${serviceName}`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class FunctionError extends BaseError.extend('FUNCTION_ERROR') {}
|
package/src/executor/executor.ts
CHANGED
|
@@ -4,14 +4,18 @@
|
|
|
4
4
|
|
|
5
5
|
import { Effect, Schema } from 'effect';
|
|
6
6
|
|
|
7
|
-
import type
|
|
7
|
+
import { type SpaceId } from '@dxos/client/echo';
|
|
8
|
+
import { runAndForwardErrors } from '@dxos/effect';
|
|
8
9
|
|
|
9
10
|
import type { FunctionContext, FunctionDefinition } from '../handler';
|
|
10
|
-
import type { ServiceContainer } from '../services';
|
|
11
|
+
import type { ServiceContainer, Services } from '../services';
|
|
11
12
|
|
|
12
13
|
export class FunctionExecutor {
|
|
13
14
|
constructor(private readonly _services: ServiceContainer) {}
|
|
14
15
|
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
15
19
|
// TODO(dmaretskyi): Invocation context: queue, space, etc...
|
|
16
20
|
async invoke<F extends FunctionDefinition<any, any>>(
|
|
17
21
|
fnDef: F,
|
|
@@ -22,26 +26,29 @@ export class FunctionExecutor {
|
|
|
22
26
|
(assertInput as any)(input);
|
|
23
27
|
|
|
24
28
|
const context: FunctionContext = {
|
|
29
|
+
space: undefined,
|
|
25
30
|
getService: this._services.getService.bind(this._services),
|
|
26
31
|
getSpace: async (_spaceId: SpaceId) => {
|
|
27
32
|
throw new Error('Not available. Use the database service instead.');
|
|
28
33
|
},
|
|
29
|
-
space: undefined,
|
|
30
|
-
get ai(): never {
|
|
31
|
-
throw new Error('Not available. Use the ai service instead.');
|
|
32
|
-
},
|
|
33
34
|
};
|
|
34
35
|
|
|
35
|
-
const result =
|
|
36
|
-
|
|
37
|
-
// Assert output matches schema
|
|
38
|
-
const assertOutput = fnDef.outputSchema?.pipe(Schema.asserts);
|
|
39
|
-
(assertOutput as any)(result);
|
|
36
|
+
const result = fnDef.handler({ context, data: input });
|
|
40
37
|
|
|
38
|
+
let data: unknown;
|
|
41
39
|
if (Effect.isEffect(result)) {
|
|
42
|
-
|
|
40
|
+
data = await (result as Effect.Effect<unknown, unknown, Services>).pipe(
|
|
41
|
+
Effect.provide(this._services.createLayer()),
|
|
42
|
+
runAndForwardErrors,
|
|
43
|
+
);
|
|
44
|
+
} else {
|
|
45
|
+
data = await result;
|
|
43
46
|
}
|
|
44
47
|
|
|
45
|
-
|
|
48
|
+
// Assert output matches schema
|
|
49
|
+
const assertOutput = fnDef.outputSchema?.pipe(Schema.asserts);
|
|
50
|
+
(assertOutput as any)(data);
|
|
51
|
+
|
|
52
|
+
return data as any;
|
|
46
53
|
}
|
|
47
54
|
}
|
package/src/handler.ts
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { type Context, type Effect, Schema } from 'effect';
|
|
6
6
|
|
|
7
|
-
import { type
|
|
8
|
-
// import { type Space } from '@dxos/client/echo';
|
|
9
|
-
import type { CoreDatabase, EchoDatabase } from '@dxos/echo-db';
|
|
7
|
+
import { type EchoDatabase } from '@dxos/echo-db';
|
|
10
8
|
import { type HasId } from '@dxos/echo-schema';
|
|
11
|
-
import { type
|
|
9
|
+
import { type DXN, type SpaceId } from '@dxos/keys';
|
|
12
10
|
import { type QueryResult } from '@dxos/protocols';
|
|
13
11
|
|
|
12
|
+
import type { Services } from './services';
|
|
13
|
+
|
|
14
14
|
// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
|
|
15
15
|
// https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
|
|
16
16
|
// https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
|
|
@@ -31,12 +31,17 @@ export type FunctionHandler<TData = {}, TOutput = any> = (params: {
|
|
|
31
31
|
* This will be the payload from the trigger or other data passed into the function in a workflow.
|
|
32
32
|
*/
|
|
33
33
|
data: TData;
|
|
34
|
-
}) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any>;
|
|
34
|
+
}) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any, Services>;
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
37
|
* Function context.
|
|
38
38
|
*/
|
|
39
39
|
export interface FunctionContext {
|
|
40
|
+
/**
|
|
41
|
+
* Space from which the function was invoked.
|
|
42
|
+
*/
|
|
43
|
+
space: SpaceAPI | undefined;
|
|
44
|
+
|
|
40
45
|
/**
|
|
41
46
|
* Resolves a service available to the function.
|
|
42
47
|
* @throws if the service is not available.
|
|
@@ -44,13 +49,6 @@ export interface FunctionContext {
|
|
|
44
49
|
getService: <T extends Context.Tag<any, any>>(tag: T) => Context.Tag.Service<T>;
|
|
45
50
|
|
|
46
51
|
getSpace: (spaceId: SpaceId) => Promise<SpaceAPI>;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Space from which the function was invoked.
|
|
50
|
-
*/
|
|
51
|
-
space: SpaceAPI | undefined;
|
|
52
|
-
|
|
53
|
-
ai: AiServiceClient;
|
|
54
52
|
}
|
|
55
53
|
|
|
56
54
|
export interface FunctionContextAi {
|
|
@@ -74,12 +72,9 @@ export interface QueuesAPI {
|
|
|
74
72
|
*/
|
|
75
73
|
export interface SpaceAPI {
|
|
76
74
|
get id(): SpaceId;
|
|
77
|
-
/**
|
|
78
|
-
* @deprecated
|
|
79
|
-
*/
|
|
80
|
-
get crud(): CoreDatabase;
|
|
81
75
|
get db(): EchoDatabase;
|
|
82
|
-
|
|
76
|
+
|
|
77
|
+
// TODO(dmaretskyi): Align with echo api: queues.get(id).append(items);
|
|
83
78
|
get queues(): QueuesAPI;
|
|
84
79
|
}
|
|
85
80
|
|
|
@@ -89,6 +84,7 @@ const __assertFunctionSpaceIsCompatibleWithTheClientSpace = () => {
|
|
|
89
84
|
};
|
|
90
85
|
|
|
91
86
|
export type FunctionDefinition<T = {}, O = any> = {
|
|
87
|
+
name: string;
|
|
92
88
|
description?: string;
|
|
93
89
|
inputSchema: Schema.Schema<T, any>;
|
|
94
90
|
outputSchema?: Schema.Schema<O, any>;
|
|
@@ -96,18 +92,31 @@ export type FunctionDefinition<T = {}, O = any> = {
|
|
|
96
92
|
};
|
|
97
93
|
|
|
98
94
|
// TODO(dmaretskyi): Bind input type to function handler.
|
|
99
|
-
export const defineFunction = <T, O>(
|
|
100
|
-
|
|
95
|
+
export const defineFunction = <T, O>({
|
|
96
|
+
name,
|
|
97
|
+
description,
|
|
98
|
+
inputSchema,
|
|
99
|
+
outputSchema = Schema.Any,
|
|
100
|
+
handler,
|
|
101
|
+
}: FunctionDefinition<T, O>): FunctionDefinition<T, O> => {
|
|
102
|
+
if (!Schema.isSchema(inputSchema)) {
|
|
101
103
|
throw new Error('Input schema must be a valid schema');
|
|
102
104
|
}
|
|
103
|
-
if (typeof
|
|
105
|
+
if (typeof handler !== 'function') {
|
|
104
106
|
throw new Error('Handler must be a function');
|
|
105
107
|
}
|
|
106
108
|
|
|
107
109
|
return {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
110
|
+
name,
|
|
111
|
+
description,
|
|
112
|
+
inputSchema,
|
|
113
|
+
outputSchema,
|
|
114
|
+
handler,
|
|
112
115
|
};
|
|
113
116
|
};
|
|
117
|
+
|
|
118
|
+
export namespace FunctionDefinition {
|
|
119
|
+
export type Any = FunctionDefinition<any, any>;
|
|
120
|
+
export type Input<T extends FunctionDefinition> = T extends FunctionDefinition<infer I, any> ? I : never;
|
|
121
|
+
export type Output<T extends FunctionDefinition> = T extends FunctionDefinition<any, infer O> ? O : never;
|
|
122
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Context } from 'effect';
|
|
5
|
+
import { Context, Effect, Layer } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { Query } from '@dxos/echo';
|
|
8
|
+
import { DataType } from '@dxos/schema';
|
|
9
|
+
|
|
10
|
+
import { DatabaseService } from './database';
|
|
6
11
|
|
|
7
12
|
type CredentialQuery = {
|
|
8
13
|
service?: string;
|
|
@@ -10,14 +15,14 @@ type CredentialQuery = {
|
|
|
10
15
|
|
|
11
16
|
// TODO(dmaretskyi): Unify with other apis.
|
|
12
17
|
// packages/sdk/schema/src/common/access-token.ts
|
|
13
|
-
type ServiceCredential = {
|
|
18
|
+
export type ServiceCredential = {
|
|
14
19
|
service: string;
|
|
15
20
|
|
|
16
21
|
// TODO(dmaretskyi): Build out.
|
|
17
22
|
apiKey?: string;
|
|
18
23
|
};
|
|
19
24
|
|
|
20
|
-
export class CredentialsService extends Context.Tag('CredentialsService')<
|
|
25
|
+
export class CredentialsService extends Context.Tag('@dxos/functions/CredentialsService')<
|
|
21
26
|
CredentialsService,
|
|
22
27
|
{
|
|
23
28
|
/**
|
|
@@ -31,7 +36,45 @@ export class CredentialsService extends Context.Tag('CredentialsService')<
|
|
|
31
36
|
*/
|
|
32
37
|
getCredential: (query: CredentialQuery) => Promise<ServiceCredential>;
|
|
33
38
|
}
|
|
34
|
-
>() {
|
|
39
|
+
>() {
|
|
40
|
+
static getCredential = (query: CredentialQuery): Effect.Effect<ServiceCredential, never, CredentialsService> =>
|
|
41
|
+
Effect.gen(function* () {
|
|
42
|
+
const credentials = yield* CredentialsService;
|
|
43
|
+
return yield* Effect.promise(() => credentials.getCredential(query));
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
static configuredLayer = (credentials: ServiceCredential[]) =>
|
|
47
|
+
Layer.succeed(CredentialsService, new ConfiguredCredentialsService(credentials));
|
|
48
|
+
|
|
49
|
+
static layerFromDatabase = () =>
|
|
50
|
+
Layer.effect(
|
|
51
|
+
CredentialsService,
|
|
52
|
+
Effect.gen(function* () {
|
|
53
|
+
const dbService = yield* DatabaseService;
|
|
54
|
+
const queryCredentials = async (query: CredentialQuery): Promise<ServiceCredential[]> => {
|
|
55
|
+
const { objects: accessTokens } = await dbService.db.query(Query.type(DataType.AccessToken)).run();
|
|
56
|
+
return accessTokens
|
|
57
|
+
.filter((accessToken) => accessToken.source === query.service)
|
|
58
|
+
.map((accessToken) => ({
|
|
59
|
+
service: accessToken.source,
|
|
60
|
+
apiKey: accessToken.token,
|
|
61
|
+
}));
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
getCredential: async (query) => {
|
|
65
|
+
const credentials = await queryCredentials(query);
|
|
66
|
+
if (credentials.length === 0) {
|
|
67
|
+
throw new Error(`Credential not found for service: ${query.service}`);
|
|
68
|
+
}
|
|
69
|
+
return credentials[0];
|
|
70
|
+
},
|
|
71
|
+
queryCredentials: async (query) => {
|
|
72
|
+
return queryCredentials(query);
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
35
78
|
|
|
36
79
|
export class ConfiguredCredentialsService implements Context.Tag.Service<CredentialsService> {
|
|
37
80
|
constructor(private readonly credentials: ServiceCredential[] = []) {}
|
package/src/services/database.ts
CHANGED
|
@@ -2,11 +2,15 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Context, Layer } from 'effect';
|
|
5
|
+
import { Context, Effect, Layer, type Schema } from 'effect';
|
|
6
6
|
|
|
7
|
-
import type
|
|
7
|
+
import { type Filter, type Live, Obj, type Query, type Ref, type Relation, type Type } from '@dxos/echo';
|
|
8
|
+
import type { EchoDatabase, FlushOptions, OneShotQueryResult, QueryResult } from '@dxos/echo-db';
|
|
9
|
+
import { BaseError } from '@dxos/errors';
|
|
10
|
+
import { invariant } from '@dxos/invariant';
|
|
11
|
+
import type { DXN } from '@dxos/keys';
|
|
8
12
|
|
|
9
|
-
export class DatabaseService extends Context.Tag('DatabaseService')<
|
|
13
|
+
export class DatabaseService extends Context.Tag('@dxos/functions/DatabaseService')<
|
|
10
14
|
DatabaseService,
|
|
11
15
|
{
|
|
12
16
|
readonly db: EchoDatabase;
|
|
@@ -25,4 +29,88 @@ export class DatabaseService extends Context.Tag('DatabaseService')<
|
|
|
25
29
|
},
|
|
26
30
|
};
|
|
27
31
|
};
|
|
32
|
+
|
|
33
|
+
static layer = (db: EchoDatabase): Layer.Layer<DatabaseService> => {
|
|
34
|
+
return Layer.succeed(DatabaseService, DatabaseService.make(db));
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Resolves an object by its DXN.
|
|
39
|
+
*/
|
|
40
|
+
static resolve: {
|
|
41
|
+
// No type check.
|
|
42
|
+
(dxn: DXN): Effect.Effect<Obj.Any | Relation.Any, never, DatabaseService>;
|
|
43
|
+
// Check matches schema.
|
|
44
|
+
<S extends Type.Obj.Any | Type.Relation.Any>(
|
|
45
|
+
dxn: DXN,
|
|
46
|
+
schema: S,
|
|
47
|
+
): Effect.Effect<Schema.Schema.Type<S>, ObjectNotFoundError, DatabaseService>;
|
|
48
|
+
} = (<S extends Type.Obj.Any | Type.Relation.Any>(
|
|
49
|
+
dxn: DXN,
|
|
50
|
+
schema?: S,
|
|
51
|
+
): Effect.Effect<Schema.Schema.Type<S>, ObjectNotFoundError, DatabaseService> =>
|
|
52
|
+
Effect.gen(function* () {
|
|
53
|
+
const { db } = yield* DatabaseService;
|
|
54
|
+
const object = yield* Effect.promise(() =>
|
|
55
|
+
db.graph
|
|
56
|
+
.createRefResolver({
|
|
57
|
+
context: {
|
|
58
|
+
space: db.spaceId,
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
.resolve(dxn),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
if (!object) {
|
|
65
|
+
return yield* Effect.fail(new ObjectNotFoundError({ dxn }));
|
|
66
|
+
}
|
|
67
|
+
invariant(!schema || Obj.instanceOf(schema, object), 'Object type mismatch.');
|
|
68
|
+
return object as any;
|
|
69
|
+
})) as any;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Loads an object reference.
|
|
73
|
+
*/
|
|
74
|
+
static load: <T>(ref: Ref.Ref<T>) => Effect.Effect<T, never, never> = Effect.fn(function* (ref) {
|
|
75
|
+
return yield* Effect.promise(() => ref.load());
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Creates a `QueryResult` object that can be subscribed to.
|
|
80
|
+
*/
|
|
81
|
+
static query: {
|
|
82
|
+
<Q extends Query.Any>(query: Q): Effect.Effect<QueryResult<Live<Query.Type<Q>>>, never, DatabaseService>;
|
|
83
|
+
<F extends Filter.Any>(filter: F): Effect.Effect<QueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;
|
|
84
|
+
} = (queryOrFilter: Query.Any | Filter.Any) =>
|
|
85
|
+
DatabaseService.pipe(
|
|
86
|
+
Effect.map(({ db }) => db.query(queryOrFilter as any)),
|
|
87
|
+
Effect.withSpan('DatabaseService.query'),
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Executes the query once and returns the results.
|
|
92
|
+
*/
|
|
93
|
+
static runQuery: {
|
|
94
|
+
<Q extends Query.Any>(query: Q): Effect.Effect<OneShotQueryResult<Live<Query.Type<Q>>>, never, DatabaseService>;
|
|
95
|
+
<F extends Filter.Any>(filter: F): Effect.Effect<OneShotQueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;
|
|
96
|
+
} = (queryOrFilter: Query.Any | Filter.Any) =>
|
|
97
|
+
DatabaseService.query(queryOrFilter as any).pipe(
|
|
98
|
+
Effect.flatMap((queryResult) => Effect.promise(() => queryResult.run())),
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Adds an object to the database.
|
|
103
|
+
*/
|
|
104
|
+
static add = <T extends Obj.Any | Relation.Any>(obj: T): Effect.Effect<T, never, DatabaseService> =>
|
|
105
|
+
DatabaseService.pipe(Effect.map(({ db }) => db.add(obj)));
|
|
106
|
+
|
|
107
|
+
static flush = (opts?: FlushOptions) =>
|
|
108
|
+
DatabaseService.pipe(Effect.flatMap(({ db }) => Effect.promise(() => db.flush(opts))));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// TODO(burdon): Move to echo/errors.
|
|
112
|
+
class ObjectNotFoundError extends BaseError.extend('OBJECT_NOT_FOUND') {
|
|
113
|
+
constructor(context?: Record<string, unknown>) {
|
|
114
|
+
super('Object not found', { context });
|
|
115
|
+
}
|
|
28
116
|
}
|