@dxos/functions 0.8.4-main.c1de068 → 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 (79) 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/index.mjs +22 -25
  6. package/dist/lib/browser/index.mjs.map +4 -4
  7. package/dist/lib/browser/meta.json +1 -1
  8. package/dist/lib/browser/testing/index.mjs +1 -1
  9. package/dist/lib/browser/testing/index.mjs.map +2 -2
  10. package/dist/lib/node-esm/bundler/index.mjs +2 -1
  11. package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
  12. package/dist/lib/node-esm/chunk-K7REX6H4.mjs +488 -0
  13. package/dist/lib/node-esm/chunk-K7REX6H4.mjs.map +7 -0
  14. package/dist/lib/node-esm/index.mjs +22 -25
  15. package/dist/lib/node-esm/index.mjs.map +4 -4
  16. package/dist/lib/node-esm/meta.json +1 -1
  17. package/dist/lib/node-esm/testing/index.mjs +1 -1
  18. package/dist/lib/node-esm/testing/index.mjs.map +2 -2
  19. package/dist/types/src/bundler/bundler.d.ts.map +1 -1
  20. package/dist/types/src/errors.d.ts +10 -8
  21. package/dist/types/src/errors.d.ts.map +1 -1
  22. package/dist/types/src/executor/executor.d.ts +3 -0
  23. package/dist/types/src/executor/executor.d.ts.map +1 -1
  24. package/dist/types/src/handler.d.ts +8 -5
  25. package/dist/types/src/handler.d.ts.map +1 -1
  26. package/dist/types/src/schema.d.ts +2 -2
  27. package/dist/types/src/services/credentials.d.ts +5 -2
  28. package/dist/types/src/services/credentials.d.ts.map +1 -1
  29. package/dist/types/src/services/database.d.ts +56 -6
  30. package/dist/types/src/services/database.d.ts.map +1 -1
  31. package/dist/types/src/services/event-logger.d.ts +65 -30
  32. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  33. package/dist/types/src/services/index.d.ts +2 -1
  34. package/dist/types/src/services/index.d.ts.map +1 -1
  35. package/dist/types/src/services/local-function-execution.d.ts +12 -0
  36. package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
  37. package/dist/types/src/services/queues.d.ts +20 -6
  38. package/dist/types/src/services/queues.d.ts.map +1 -1
  39. package/dist/types/src/services/remote-function-execution-service.d.ts +15 -0
  40. package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -0
  41. package/dist/types/src/services/service-container.d.ts +5 -5
  42. package/dist/types/src/services/service-container.d.ts.map +1 -1
  43. package/dist/types/src/services/service-registry.d.ts +1 -1
  44. package/dist/types/src/services/service-registry.d.ts.map +1 -1
  45. package/dist/types/src/services/tracing.d.ts +4 -3
  46. package/dist/types/src/services/tracing.d.ts.map +1 -1
  47. package/dist/types/src/testing/logger.d.ts +3 -3
  48. package/dist/types/src/testing/logger.d.ts.map +1 -1
  49. package/dist/types/src/testing/services.d.ts +2 -2
  50. package/dist/types/src/testing/services.d.ts.map +1 -1
  51. package/dist/types/src/trace.d.ts +3 -5
  52. package/dist/types/src/trace.d.ts.map +1 -1
  53. package/dist/types/src/types.d.ts +8 -8
  54. package/dist/types/src/types.d.ts.map +1 -1
  55. package/dist/types/tsconfig.tsbuildinfo +1 -1
  56. package/package.json +30 -25
  57. package/src/bundler/bundler.ts +2 -2
  58. package/src/executor/executor.ts +5 -5
  59. package/src/handler.ts +9 -7
  60. package/src/services/credentials.ts +39 -2
  61. package/src/services/database.ts +62 -16
  62. package/src/services/event-logger.ts +68 -37
  63. package/src/services/index.ts +2 -1
  64. package/src/services/local-function-execution.ts +74 -0
  65. package/src/services/queues.ts +32 -10
  66. package/src/services/{function-call-service.ts → remote-function-execution-service.ts} +7 -5
  67. package/src/services/service-container.ts +11 -10
  68. package/src/services/service-registry.ts +5 -2
  69. package/src/services/tracing.ts +5 -3
  70. package/src/testing/logger.ts +4 -4
  71. package/src/testing/services.ts +3 -2
  72. package/src/trace.ts +3 -5
  73. package/src/types.ts +1 -1
  74. package/dist/lib/browser/chunk-3NGCSUEW.mjs +0 -328
  75. package/dist/lib/browser/chunk-3NGCSUEW.mjs.map +0 -7
  76. package/dist/lib/node-esm/chunk-FJ2MU7TL.mjs +0 -330
  77. package/dist/lib/node-esm/chunk-FJ2MU7TL.mjs.map +0 -7
  78. package/dist/types/src/services/function-call-service.d.ts +0 -16
  79. package/dist/types/src/services/function-call-service.d.ts.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/functions",
3
- "version": "0.8.4-main.c1de068",
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,43 +51,44 @@
47
51
  "src"
48
52
  ],
49
53
  "dependencies": {
50
- "@effect/platform": "0.89.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.17.0",
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.c1de068",
62
- "@dxos/client": "0.8.4-main.c1de068",
63
- "@dxos/async": "0.8.4-main.c1de068",
64
- "@dxos/context": "0.8.4-main.c1de068",
65
- "@dxos/echo": "0.8.4-main.c1de068",
66
- "@dxos/crypto": "0.8.4-main.c1de068",
67
- "@dxos/echo-db": "0.8.4-main.c1de068",
68
- "@dxos/echo-pipeline": "0.8.4-main.c1de068",
69
- "@dxos/echo-protocol": "0.8.4-main.c1de068",
70
- "@dxos/edge-client": "0.8.4-main.c1de068",
71
- "@dxos/echo-schema": "0.8.4-main.c1de068",
72
- "@dxos/errors": "0.8.4-main.c1de068",
73
- "@dxos/effect": "0.8.4-main.c1de068",
74
- "@dxos/invariant": "0.8.4-main.c1de068",
75
- "@dxos/keys": "0.8.4-main.c1de068",
76
- "@dxos/live-object": "0.8.4-main.c1de068",
77
- "@dxos/log": "0.8.4-main.c1de068",
78
- "@dxos/node-std": "0.8.4-main.c1de068",
79
- "@dxos/schema": "0.8.4-main.c1de068",
80
- "@dxos/protocols": "0.8.4-main.c1de068",
81
- "@dxos/util": "0.8.4-main.c1de068"
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"
82
87
  },
83
88
  "devDependencies": {
84
89
  "@types/express": "^4.17.17",
85
90
  "@types/ws": "^7.4.0",
86
- "@dxos/agent": "0.8.4-main.c1de068"
91
+ "@dxos/agent": "0.8.4-main.fd6878d"
87
92
  },
88
93
  "publishConfig": {
89
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';
@@ -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';
@@ -44,8 +42,6 @@ export interface FunctionContext {
44
42
  */
45
43
  space: SpaceAPI | undefined;
46
44
 
47
- ai: AiServiceClient;
48
-
49
45
  /**
50
46
  * Resolves a service available to the function.
51
47
  * @throws if the service is not available.
@@ -118,3 +114,9 @@ export const defineFunction = <T, O>({
118
114
  handler,
119
115
  };
120
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
+ }
@@ -2,7 +2,12 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context, Effect } 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
  /**
@@ -37,6 +42,38 @@ export class CredentialsService extends Context.Tag('CredentialsService')<
37
42
  const credentials = yield* CredentialsService;
38
43
  return yield* Effect.promise(() => credentials.getCredential(query));
39
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
+ );
40
77
  }
41
78
 
42
79
  export class ConfiguredCredentialsService implements Context.Tag.Service<CredentialsService> {
@@ -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 { Filter, Live, Obj, Query, Ref, Relation } from '@dxos/echo';
8
- import type { EchoDatabase, OneShotQueryResult, QueryResult } 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,20 +30,48 @@ 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
+ );
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;
43
70
 
44
- static loadRef: <T>(ref: Ref.Ref<T>) => Effect.Effect<T, never, never> = Effect.fn(function* (ref) {
71
+ /**
72
+ * Loads an object reference.
73
+ */
74
+ static load: <T>(ref: Ref.Ref<T>) => Effect.Effect<T, never, never> = Effect.fn(function* (ref) {
45
75
  return yield* Effect.promise(() => ref.load());
46
76
  });
47
77
 
@@ -67,4 +97,20 @@ export class DatabaseService extends Context.Tag('DatabaseService')<
67
97
  DatabaseService.query(queryOrFilter as any).pipe(
68
98
  Effect.flatMap((queryResult) => Effect.promise(() => queryResult.run())),
69
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
+ }
70
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,
@@ -8,4 +8,5 @@ export * from './service-container';
8
8
  export * from './credentials';
9
9
  export * from './tracing';
10
10
  export * from './event-logger';
11
- export * from './function-call-service';
11
+ export * from './remote-function-execution-service';
12
+ export * from './local-function-execution';
@@ -0,0 +1,74 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { Context, Effect, Layer, Schema } from 'effect';
6
+
7
+ import { todo } from '@dxos/debug';
8
+
9
+ import { FunctionError } from '../errors';
10
+ import type { FunctionContext, FunctionDefinition } from '../handler';
11
+
12
+ import type { Services } from './service-container';
13
+
14
+ export class LocalFunctionExecutionService extends Context.Tag('@dxos/functions/LocalFunctionExecutionService')<
15
+ LocalFunctionExecutionService,
16
+ {
17
+ // TODO(dmaretskyi): This should take function id instead of the definition object.
18
+ // TODO(dmaretskyi): Services should be satisfied from environment rather then bubbled up.
19
+ invokeFunction(fnDef: FunctionDefinition<any, any>, input: unknown): Effect.Effect<unknown, never, Services>;
20
+ }
21
+ >() {
22
+ static layer = Layer.succeed(LocalFunctionExecutionService, {
23
+ invokeFunction: (fnDef, input) => invokeFunction(fnDef, input),
24
+ });
25
+
26
+ static invokeFunction: <F extends FunctionDefinition.Any>(
27
+ fnDef: F,
28
+ input: FunctionDefinition.Input<F>,
29
+ ) => Effect.Effect<FunctionDefinition.Output<F>, never, Services | LocalFunctionExecutionService> =
30
+ Effect.serviceFunctionEffect(LocalFunctionExecutionService, (_) => _.invokeFunction as any);
31
+ }
32
+
33
+ const invokeFunction = (fnDef: FunctionDefinition<any, any>, input: any): Effect.Effect<unknown, never, Services> =>
34
+ Effect.gen(function* () {
35
+ // Assert input matches schema
36
+ const assertInput = fnDef.inputSchema.pipe(Schema.asserts);
37
+ (assertInput as any)(input);
38
+
39
+ const context: FunctionContext = {
40
+ space: undefined,
41
+ getService: () => todo(),
42
+ getSpace: async (_spaceId: any) => {
43
+ throw new Error('Not available. Use the database service instead.');
44
+ },
45
+ };
46
+
47
+ // TODO(dmaretskyi): This should be delegated to a function invoker service.
48
+ const data = yield* Effect.gen(function* () {
49
+ const result = fnDef.handler({ context, data: input });
50
+ if (Effect.isEffect(result)) {
51
+ return yield* (result as Effect.Effect<unknown, unknown, Services>).pipe(Effect.orDie);
52
+ } else if (
53
+ typeof result === 'object' &&
54
+ result !== null &&
55
+ 'then' in result &&
56
+ typeof result.then === 'function'
57
+ ) {
58
+ return yield* Effect.promise(() => result);
59
+ } else {
60
+ return result;
61
+ }
62
+ }).pipe(
63
+ Effect.orDie,
64
+ Effect.catchAllDefect((defect) =>
65
+ Effect.die(new FunctionError('Error running function', { context: { name: fnDef.name }, cause: defect })),
66
+ ),
67
+ );
68
+
69
+ // Assert output matches schema
70
+ const assertOutput = fnDef.outputSchema?.pipe(Schema.asserts);
71
+ (assertOutput as any)(data);
72
+
73
+ return data;
74
+ }).pipe(Effect.withSpan('invokeFunction', { attributes: { name: fnDef.name } }));
@@ -2,14 +2,16 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context, Layer } from 'effect';
5
+ import { Context, Effect, Layer } from 'effect';
6
6
 
7
+ import type { Obj, Relation } from '@dxos/echo';
7
8
  import type { Queue, QueueAPI, QueueFactory } from '@dxos/echo-db';
9
+ import type { DXN, QueueSubspaceTag } from '@dxos/keys';
8
10
 
9
11
  /**
10
12
  * Gives access to all queues.
11
13
  */
12
- export class QueueService extends Context.Tag('QueueService')<
14
+ export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
13
15
  QueueService,
14
16
  {
15
17
  /**
@@ -21,35 +23,55 @@ export class QueueService extends Context.Tag('QueueService')<
21
23
  * The queue that is used to store the context of the current research.
22
24
  * @deprecated Use `ContextQueueService` instead.
23
25
  */
24
- readonly contextQueue: Queue | undefined;
26
+ readonly queue: Queue | undefined;
25
27
  }
26
28
  >() {
27
29
  static notAvailable = Layer.succeed(QueueService, {
28
30
  queues: {
29
- get(dxn) {
31
+ get(_dxn) {
30
32
  throw new Error('Queues not available');
31
33
  },
32
34
  create() {
33
35
  throw new Error('Queues not available');
34
36
  },
35
37
  },
36
- contextQueue: undefined,
38
+ queue: undefined,
37
39
  });
38
40
 
39
- static make = (queues: QueueFactory, contextQueue?: Queue): Context.Tag.Service<QueueService> => {
41
+ static make = (queues: QueueFactory, queue?: Queue): Context.Tag.Service<QueueService> => {
40
42
  return {
41
43
  queues,
42
- contextQueue,
44
+ queue,
43
45
  };
44
46
  };
47
+
48
+ static layer = (queues: QueueFactory, queue?: Queue): Layer.Layer<QueueService> =>
49
+ Layer.succeed(QueueService, QueueService.make(queues, queue));
50
+
51
+ /**
52
+ * Gets a queue by its DXN.
53
+ */
54
+ static getQueue = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(
55
+ dxn: DXN,
56
+ ): Effect.Effect<Queue<T>, never, QueueService> => QueueService.pipe(Effect.map(({ queues }) => queues.get<T>(dxn)));
57
+
58
+ /**
59
+ * Creates a new queue.
60
+ */
61
+ static createQueue = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(options?: {
62
+ subspaceTag?: QueueSubspaceTag;
63
+ }): Effect.Effect<Queue<T>, never, QueueService> =>
64
+ QueueService.pipe(Effect.map(({ queues }) => queues.create<T>(options)));
45
65
  }
46
66
 
47
67
  /**
48
68
  * Gives access to a specific queue passed as a context.
49
69
  */
50
- export class ContextQueueService extends Context.Tag('ContextQueueService')<
70
+ export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQueueService')<
51
71
  ContextQueueService,
52
72
  {
53
- readonly contextQueue: Queue;
73
+ readonly queue: Queue;
54
74
  }
55
- >() {}
75
+ >() {
76
+ static layer = (queue: Queue) => Layer.succeed(ContextQueueService, { queue });
77
+ }