@dxos/functions 0.8.4-main.f9ba587 → 0.8.4-main.fd6878d

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.
Files changed (103) hide show
  1. package/dist/lib/browser/bundler/index.mjs +2 -1
  2. package/dist/lib/browser/bundler/index.mjs.map +3 -3
  3. package/dist/lib/browser/chunk-M2OIFLGE.mjs +486 -0
  4. package/dist/lib/browser/chunk-M2OIFLGE.mjs.map +7 -0
  5. package/dist/lib/browser/edge/index.mjs.map +1 -1
  6. package/dist/lib/browser/index.mjs +34 -26
  7. package/dist/lib/browser/index.mjs.map +3 -3
  8. package/dist/lib/browser/meta.json +1 -1
  9. package/dist/lib/browser/testing/index.mjs +19 -39
  10. package/dist/lib/browser/testing/index.mjs.map +4 -4
  11. package/dist/lib/node-esm/bundler/index.mjs +2 -1
  12. package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
  13. package/dist/lib/node-esm/chunk-K7REX6H4.mjs +488 -0
  14. package/dist/lib/node-esm/chunk-K7REX6H4.mjs.map +7 -0
  15. package/dist/lib/node-esm/edge/index.mjs.map +1 -1
  16. package/dist/lib/node-esm/index.mjs +34 -26
  17. package/dist/lib/node-esm/index.mjs.map +3 -3
  18. package/dist/lib/node-esm/meta.json +1 -1
  19. package/dist/lib/node-esm/testing/index.mjs +19 -39
  20. package/dist/lib/node-esm/testing/index.mjs.map +4 -4
  21. package/dist/types/src/bundler/bundler.d.ts.map +1 -1
  22. package/dist/types/src/edge/functions.d.ts +1 -1
  23. package/dist/types/src/edge/functions.d.ts.map +1 -1
  24. package/dist/types/src/errors.d.ts +70 -0
  25. package/dist/types/src/errors.d.ts.map +1 -0
  26. package/dist/types/src/executor/executor.d.ts +3 -0
  27. package/dist/types/src/executor/executor.d.ts.map +1 -1
  28. package/dist/types/src/handler.d.ts +14 -10
  29. package/dist/types/src/handler.d.ts.map +1 -1
  30. package/dist/types/src/index.d.ts +1 -0
  31. package/dist/types/src/index.d.ts.map +1 -1
  32. package/dist/types/src/schema.d.ts +2 -2
  33. package/dist/types/src/services/credentials.d.ts +6 -2
  34. package/dist/types/src/services/credentials.d.ts.map +1 -1
  35. package/dist/types/src/services/database.d.ts +70 -6
  36. package/dist/types/src/services/database.d.ts.map +1 -1
  37. package/dist/types/src/services/event-logger.d.ts +65 -30
  38. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  39. package/dist/types/src/services/index.d.ts +2 -3
  40. package/dist/types/src/services/index.d.ts.map +1 -1
  41. package/dist/types/src/services/local-function-execution.d.ts +12 -0
  42. package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
  43. package/dist/types/src/services/queues.d.ts +30 -4
  44. package/dist/types/src/services/queues.d.ts.map +1 -1
  45. package/dist/types/src/services/remote-function-execution-service.d.ts +15 -0
  46. package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -0
  47. package/dist/types/src/services/service-container.d.ts +28 -18
  48. package/dist/types/src/services/service-container.d.ts.map +1 -1
  49. package/dist/types/src/services/service-registry.d.ts +29 -0
  50. package/dist/types/src/services/service-registry.d.ts.map +1 -0
  51. package/dist/types/src/services/service-registry.test.d.ts +2 -0
  52. package/dist/types/src/services/service-registry.test.d.ts.map +1 -0
  53. package/dist/types/src/services/tracing.d.ts +13 -2
  54. package/dist/types/src/services/tracing.d.ts.map +1 -1
  55. package/dist/types/src/testing/index.d.ts +1 -0
  56. package/dist/types/src/testing/index.d.ts.map +1 -1
  57. package/dist/types/src/testing/layer.d.ts +10 -0
  58. package/dist/types/src/testing/layer.d.ts.map +1 -0
  59. package/dist/types/src/testing/logger.d.ts +3 -3
  60. package/dist/types/src/testing/logger.d.ts.map +1 -1
  61. package/dist/types/src/testing/services.d.ts +7 -19
  62. package/dist/types/src/testing/services.d.ts.map +1 -1
  63. package/dist/types/src/trace.d.ts +3 -5
  64. package/dist/types/src/trace.d.ts.map +1 -1
  65. package/dist/types/src/types.d.ts +7 -7
  66. package/dist/types/src/types.d.ts.map +1 -1
  67. package/dist/types/tsconfig.tsbuildinfo +1 -1
  68. package/package.json +30 -23
  69. package/src/bundler/bundler.ts +2 -2
  70. package/src/edge/functions.ts +1 -1
  71. package/src/errors.ts +13 -0
  72. package/src/executor/executor.ts +5 -5
  73. package/src/handler.ts +30 -21
  74. package/src/index.ts +1 -0
  75. package/src/services/credentials.ts +46 -3
  76. package/src/services/database.ts +86 -20
  77. package/src/services/event-logger.ts +68 -37
  78. package/src/services/index.ts +2 -3
  79. package/src/services/local-function-execution.ts +74 -0
  80. package/src/services/queues.ts +43 -8
  81. package/src/services/{function-call-service.ts → remote-function-execution-service.ts} +7 -5
  82. package/src/services/service-container.ts +45 -58
  83. package/src/services/service-registry.test.ts +42 -0
  84. package/src/services/service-registry.ts +59 -0
  85. package/src/services/tracing.ts +19 -2
  86. package/src/testing/index.ts +1 -0
  87. package/src/testing/layer.ts +31 -0
  88. package/src/testing/logger.ts +4 -4
  89. package/src/testing/services.ts +10 -69
  90. package/src/trace.ts +3 -5
  91. package/src/types.ts +1 -1
  92. package/dist/lib/browser/chunk-54U464M4.mjs +0 -360
  93. package/dist/lib/browser/chunk-54U464M4.mjs.map +0 -7
  94. package/dist/lib/node-esm/chunk-XDSX35BS.mjs +0 -362
  95. package/dist/lib/node-esm/chunk-XDSX35BS.mjs.map +0 -7
  96. package/dist/types/src/services/ai.d.ts +0 -12
  97. package/dist/types/src/services/ai.d.ts.map +0 -1
  98. package/dist/types/src/services/function-call-service.d.ts +0 -16
  99. package/dist/types/src/services/function-call-service.d.ts.map +0 -1
  100. package/dist/types/src/services/tool-resolver.d.ts +0 -14
  101. package/dist/types/src/services/tool-resolver.d.ts.map +0 -1
  102. package/src/services/ai.ts +0 -32
  103. package/src/services/tool-resolver.ts +0 -31
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/functions",
3
- "version": "0.8.4-main.f9ba587",
3
+ "version": "0.8.4-main.fd6878d",
4
4
  "description": "Functions API and runtime.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -10,21 +10,25 @@
10
10
  "type": "module",
11
11
  "exports": {
12
12
  "./bundler": {
13
+ "source": "./src/bundler/index.ts",
13
14
  "types": "./dist/types/src/bundler/index.d.ts",
14
15
  "browser": "./dist/lib/browser/bundler/index.mjs",
15
16
  "node": "./dist/lib/node-esm/bundler/index.mjs"
16
17
  },
17
18
  "./edge": {
19
+ "source": "./src/edge/index.ts",
18
20
  "types": "./dist/types/src/edge/index.d.ts",
19
21
  "browser": "./dist/lib/browser/edge/index.mjs",
20
22
  "node": "./dist/lib/node-esm/edge/index.mjs"
21
23
  },
22
24
  ".": {
25
+ "source": "./src/index.ts",
23
26
  "types": "./dist/types/src/index.d.ts",
24
27
  "browser": "./dist/lib/browser/index.mjs",
25
28
  "node": "./dist/lib/node-esm/index.mjs"
26
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,41 +51,44 @@
47
51
  "src"
48
52
  ],
49
53
  "dependencies": {
50
- "@effect/platform": "0.88.0",
54
+ "@effect/platform": "0.90.2",
51
55
  "@preact/signals-core": "^1.9.0",
52
56
  "cron": "^3.1.6",
53
57
  "cron-schedule": "^5.0.4",
54
- "effect": "3.16.13",
58
+ "effect": "3.17.7",
55
59
  "esbuild-wasm": "^0.16.14",
56
60
  "express": "^4.19.2",
57
61
  "get-port-please": "^3.1.1",
58
62
  "i18next": "^24.2.1",
59
63
  "iso-did": "^1.6.0",
60
64
  "ws": "^8.14.2",
61
- "@dxos/ai": "0.8.4-main.f9ba587",
62
- "@dxos/async": "0.8.4-main.f9ba587",
63
- "@dxos/client": "0.8.4-main.f9ba587",
64
- "@dxos/context": "0.8.4-main.f9ba587",
65
- "@dxos/crypto": "0.8.4-main.f9ba587",
66
- "@dxos/echo": "0.8.4-main.f9ba587",
67
- "@dxos/echo-db": "0.8.4-main.f9ba587",
68
- "@dxos/echo-protocol": "0.8.4-main.f9ba587",
69
- "@dxos/echo-schema": "0.8.4-main.f9ba587",
70
- "@dxos/edge-client": "0.8.4-main.f9ba587",
71
- "@dxos/effect": "0.8.4-main.f9ba587",
72
- "@dxos/keys": "0.8.4-main.f9ba587",
73
- "@dxos/invariant": "0.8.4-main.f9ba587",
74
- "@dxos/live-object": "0.8.4-main.f9ba587",
75
- "@dxos/log": "0.8.4-main.f9ba587",
76
- "@dxos/node-std": "0.8.4-main.f9ba587",
77
- "@dxos/protocols": "0.8.4-main.f9ba587",
78
- "@dxos/schema": "0.8.4-main.f9ba587",
79
- "@dxos/util": "0.8.4-main.f9ba587"
65
+ "@dxos/ai": "0.8.4-main.fd6878d",
66
+ "@dxos/async": "0.8.4-main.fd6878d",
67
+ "@dxos/debug": "0.8.4-main.fd6878d",
68
+ "@dxos/crypto": "0.8.4-main.fd6878d",
69
+ "@dxos/client": "0.8.4-main.fd6878d",
70
+ "@dxos/echo": "0.8.4-main.fd6878d",
71
+ "@dxos/echo-db": "0.8.4-main.fd6878d",
72
+ "@dxos/echo-pipeline": "0.8.4-main.fd6878d",
73
+ "@dxos/echo-schema": "0.8.4-main.fd6878d",
74
+ "@dxos/echo-protocol": "0.8.4-main.fd6878d",
75
+ "@dxos/edge-client": "0.8.4-main.fd6878d",
76
+ "@dxos/effect": "0.8.4-main.fd6878d",
77
+ "@dxos/errors": "0.8.4-main.fd6878d",
78
+ "@dxos/invariant": "0.8.4-main.fd6878d",
79
+ "@dxos/live-object": "0.8.4-main.fd6878d",
80
+ "@dxos/keys": "0.8.4-main.fd6878d",
81
+ "@dxos/log": "0.8.4-main.fd6878d",
82
+ "@dxos/node-std": "0.8.4-main.fd6878d",
83
+ "@dxos/context": "0.8.4-main.fd6878d",
84
+ "@dxos/schema": "0.8.4-main.fd6878d",
85
+ "@dxos/protocols": "0.8.4-main.fd6878d",
86
+ "@dxos/util": "0.8.4-main.fd6878d"
80
87
  },
81
88
  "devDependencies": {
82
89
  "@types/express": "^4.17.17",
83
90
  "@types/ws": "^7.4.0",
84
- "@dxos/agent": "0.8.4-main.f9ba587"
91
+ "@dxos/agent": "0.8.4-main.fd6878d"
85
92
  },
86
93
  "publishConfig": {
87
94
  "access": "public"
@@ -3,8 +3,8 @@
3
3
  //
4
4
 
5
5
  import { FetchHttpClient, HttpClient } from '@effect/platform';
6
- import { Duration, Effect, pipe, Schedule } from 'effect';
7
- import { type BuildOptions, type Loader, build, initialize, type BuildResult, type Plugin } from 'esbuild-wasm';
6
+ import { Duration, Effect, Schedule, pipe } from 'effect';
7
+ import { type BuildOptions, type BuildResult, type Loader, type Plugin, build, initialize } from 'esbuild-wasm';
8
8
 
9
9
  import { subtleCrypto } from '@dxos/crypto';
10
10
  import { runAndForwardErrors } from '@dxos/effect';
@@ -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 { PublicKey } from '@dxos/keys';
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') {}
@@ -4,7 +4,7 @@
4
4
 
5
5
  import { Effect, Schema } from 'effect';
6
6
 
7
- import type { SpaceId } from '@dxos/client/echo';
7
+ import { type SpaceId } from '@dxos/client/echo';
8
8
  import { runAndForwardErrors } from '@dxos/effect';
9
9
 
10
10
  import type { FunctionContext, FunctionDefinition } from '../handler';
@@ -13,6 +13,9 @@ import type { ServiceContainer, Services } from '../services';
13
13
  export class FunctionExecutor {
14
14
  constructor(private readonly _services: ServiceContainer) {}
15
15
 
16
+ /**
17
+ *
18
+ */
16
19
  // TODO(dmaretskyi): Invocation context: queue, space, etc...
17
20
  async invoke<F extends FunctionDefinition<any, any>>(
18
21
  fnDef: F,
@@ -23,14 +26,11 @@ export class FunctionExecutor {
23
26
  (assertInput as any)(input);
24
27
 
25
28
  const context: FunctionContext = {
29
+ space: undefined,
26
30
  getService: this._services.getService.bind(this._services),
27
31
  getSpace: async (_spaceId: SpaceId) => {
28
32
  throw new Error('Not available. Use the database service instead.');
29
33
  },
30
- space: undefined,
31
- get ai(): never {
32
- throw new Error('Not available. Use the ai service instead.');
33
- },
34
34
  };
35
35
 
36
36
  const result = fnDef.handler({ context, data: input });
package/src/handler.ts CHANGED
@@ -2,13 +2,11 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { Schema, type Context, type Effect } from 'effect';
5
+ import { type Context, type Effect, Schema } from 'effect';
6
6
 
7
- import { type AiServiceClient } from '@dxos/ai';
8
- // import { type Space } from '@dxos/client/echo';
9
- import type { 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 SpaceId, type DXN } from '@dxos/keys';
9
+ import { type DXN, type SpaceId } from '@dxos/keys';
12
10
  import { type QueryResult } from '@dxos/protocols';
13
11
 
14
12
  import type { Services } from './services';
@@ -39,6 +37,11 @@ export type FunctionHandler<TData = {}, TOutput = any> = (params: {
39
37
  * Function context.
40
38
  */
41
39
  export interface FunctionContext {
40
+ /**
41
+ * Space from which the function was invoked.
42
+ */
43
+ space: SpaceAPI | undefined;
44
+
42
45
  /**
43
46
  * Resolves a service available to the function.
44
47
  * @throws if the service is not available.
@@ -46,13 +49,6 @@ export interface FunctionContext {
46
49
  getService: <T extends Context.Tag<any, any>>(tag: T) => Context.Tag.Service<T>;
47
50
 
48
51
  getSpace: (spaceId: SpaceId) => Promise<SpaceAPI>;
49
-
50
- /**
51
- * Space from which the function was invoked.
52
- */
53
- space: SpaceAPI | undefined;
54
-
55
- ai: AiServiceClient;
56
52
  }
57
53
 
58
54
  export interface FunctionContextAi {
@@ -76,10 +72,9 @@ export interface QueuesAPI {
76
72
  */
77
73
  export interface SpaceAPI {
78
74
  get id(): SpaceId;
79
-
80
75
  get db(): EchoDatabase;
81
76
 
82
- // TODO(dmaretskyi): Align with echo api --- queues.get(id).append(items);
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>(params: FunctionDefinition<T, O>): FunctionDefinition<T, O> => {
100
- if (!Schema.isSchema(params.inputSchema)) {
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 params.handler !== 'function') {
105
+ if (typeof handler !== 'function') {
104
106
  throw new Error('Handler must be a function');
105
107
  }
106
108
 
107
109
  return {
108
- description: params.description,
109
- inputSchema: params.inputSchema,
110
- outputSchema: params.outputSchema ?? Schema.Any,
111
- handler: params.handler,
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
@@ -9,5 +9,6 @@ export * from './types';
9
9
  export * from './url';
10
10
  export * from './services';
11
11
  export * from './executor';
12
+ export * from './errors';
12
13
 
13
14
  // Blow up cache
@@ -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;
@@ -17,7 +22,7 @@ export type ServiceCredential = {
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[] = []) {}
@@ -2,13 +2,15 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context, Effect, Layer } from 'effect';
5
+ import { Context, Effect, Layer, type Schema } from 'effect';
6
6
 
7
- import type { Obj, Ref, Relation } from '@dxos/echo';
8
- import type { EchoDatabase } from '@dxos/echo-db';
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';
9
11
  import type { DXN } from '@dxos/keys';
10
12
 
11
- export class DatabaseService extends Context.Tag('DatabaseService')<
13
+ export class DatabaseService extends Context.Tag('@dxos/functions/DatabaseService')<
12
14
  DatabaseService,
13
15
  {
14
16
  readonly db: EchoDatabase;
@@ -28,23 +30,87 @@ export class DatabaseService extends Context.Tag('DatabaseService')<
28
30
  };
29
31
  };
30
32
 
31
- static resolve: (dxn: DXN) => Effect.Effect<Obj.Any | Relation.Any, Error, DatabaseService> = Effect.fn(
32
- function* (dxn) {
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* () {
33
53
  const { db } = yield* DatabaseService;
34
- return yield* Effect.tryPromise({
35
- try: () =>
36
- db.graph.createRefResolver({ context: { space: db.spaceId } }).resolve(dxn) as Promise<
37
- Obj.Any | Relation.Any
38
- >,
39
- catch: (error) => error as Error,
40
- });
41
- },
42
- );
54
+ const object = yield* Effect.promise(() =>
55
+ db.graph
56
+ .createRefResolver({
57
+ context: {
58
+ space: db.spaceId,
59
+ },
60
+ })
61
+ .resolve(dxn),
62
+ );
43
63
 
44
- static loadRef: <T>(ref: Ref.Ref<T>) => Effect.Effect<T, Error, never> = Effect.fn(function* (ref) {
45
- return yield* Effect.tryPromise({
46
- try: () => ref.load(),
47
- catch: (error) => error as Error,
48
- });
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());
49
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
+ }
50
116
  }
@@ -2,53 +2,81 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Effect, Context } from 'effect';
5
+ import { Context, Effect, Layer, Schema } from 'effect';
6
6
 
7
+ import { Obj, Type } from '@dxos/echo';
7
8
  import { invariant } from '@dxos/invariant';
8
- import { log, LogLevel } from '@dxos/log';
9
+ import { LogLevel, log } from '@dxos/log';
9
10
 
10
- export type ComputeEvent =
11
- | {
12
- type: 'begin-compute';
13
- nodeId: string;
14
- inputs: Record<string, any>;
15
- }
16
- | {
17
- type: 'end-compute';
18
- nodeId: string;
19
- outputs: Record<string, any>;
20
- }
21
- | {
22
- type: 'compute-input';
23
- nodeId: string;
24
- property: string;
25
- value: any;
26
- }
27
- | {
28
- type: 'compute-output';
29
- nodeId: string;
30
- property: string;
31
- value: any;
32
- }
33
- | {
34
- type: 'custom';
35
- nodeId: string;
36
- event: any;
37
- };
11
+ import { TracingService } from './tracing';
12
+
13
+ export const ComputeEventPayload = Schema.Union(
14
+ Schema.Struct({
15
+ type: Schema.Literal('begin-compute'),
16
+ nodeId: Schema.String,
17
+ inputs: Schema.Record({ key: Schema.String, value: Schema.Any }),
18
+ }),
19
+ Schema.Struct({
20
+ type: Schema.Literal('end-compute'),
21
+ nodeId: Schema.String,
22
+ outputs: Schema.Record({ key: Schema.String, value: Schema.Any }),
23
+ }),
24
+ Schema.Struct({
25
+ type: Schema.Literal('compute-input'),
26
+ nodeId: Schema.String,
27
+ property: Schema.String,
28
+ value: Schema.Any,
29
+ }),
30
+ Schema.Struct({
31
+ type: Schema.Literal('compute-output'),
32
+ nodeId: Schema.String,
33
+ property: Schema.String,
34
+ value: Schema.Any,
35
+ }),
36
+ Schema.Struct({
37
+ type: Schema.Literal('custom'),
38
+ nodeId: Schema.String,
39
+ event: Schema.Any,
40
+ }),
41
+ );
42
+ export type ComputeEventPayload = Schema.Schema.Type<typeof ComputeEventPayload>;
38
43
 
39
- export class EventLogger extends Context.Tag('EventLogger')<
40
- EventLogger,
41
- { readonly log: (event: ComputeEvent) => void; readonly nodeId: string | undefined }
44
+ export const ComputeEvent = Schema.Struct({
45
+ payload: ComputeEventPayload,
46
+ }).pipe(Type.Obj({ typename: 'dxos.org/type/ComputeEvent', version: '0.1.0' }));
47
+
48
+ /**
49
+ * Logs event for the compute workflows.
50
+ */
51
+ export class ComputeEventLogger extends Context.Tag('@dxos/functions/ComputeEventLogger')<
52
+ ComputeEventLogger,
53
+ { readonly log: (event: ComputeEventPayload) => void; readonly nodeId: string | undefined }
42
54
  >() {
43
- static noop: Context.Tag.Service<EventLogger> = {
55
+ static noop: Context.Tag.Service<ComputeEventLogger> = {
44
56
  log: () => {},
45
57
  nodeId: undefined,
46
58
  };
59
+
60
+ /**
61
+ * Implements ComputeEventLogger using TracingService.
62
+ */
63
+ static layerFromTracing = Layer.effect(
64
+ ComputeEventLogger,
65
+ Effect.gen(function* () {
66
+ const tracing = yield* TracingService;
67
+ return {
68
+ log: (event: ComputeEventPayload) => {
69
+ tracing.write(Obj.make(ComputeEvent, { payload: event }));
70
+ },
71
+ nodeId: undefined,
72
+ };
73
+ }),
74
+ );
47
75
  }
48
76
 
49
77
  export const logCustomEvent = (data: any) =>
50
78
  Effect.gen(function* () {
51
- const logger = yield* EventLogger;
79
+ const logger = yield* ComputeEventLogger;
52
80
  if (!logger.nodeId) {
53
81
  throw new Error('logCustomEvent must be called within a node compute function');
54
82
  }
@@ -67,7 +95,10 @@ export const createDefectLogger = <A, E, R>(): ((self: Effect.Effect<A, E, R>) =
67
95
  }),
68
96
  );
69
97
 
70
- export const createEventLogger = (level: LogLevel, message: string = 'event'): Context.Tag.Service<EventLogger> => {
98
+ export const createEventLogger = (
99
+ level: LogLevel,
100
+ message: string = 'event',
101
+ ): Context.Tag.Service<ComputeEventLogger> => {
71
102
  const logFunction = (
72
103
  {
73
104
  [LogLevel.WARN]: log.warn,
@@ -79,7 +110,7 @@ export const createEventLogger = (level: LogLevel, message: string = 'event'): C
79
110
  )[level];
80
111
  invariant(logFunction);
81
112
  return {
82
- log: (event: ComputeEvent) => {
113
+ log: (event: ComputeEventPayload) => {
83
114
  logFunction(message, event);
84
115
  },
85
116
  nodeId: undefined,
@@ -2,12 +2,11 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- export * from './ai';
6
5
  export * from './database';
7
6
  export * from './queues';
8
7
  export * from './service-container';
9
8
  export * from './credentials';
10
9
  export * from './tracing';
11
10
  export * from './event-logger';
12
- export * from './function-call-service';
13
- export * from './tool-resolver';
11
+ export * from './remote-function-execution-service';
12
+ export * from './local-function-execution';