@dxos/functions 0.8.4-main.3f58842 → 0.8.4-main.5ea62a8

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 (109) hide show
  1. package/dist/lib/browser/bundler/index.mjs +54 -38
  2. package/dist/lib/browser/bundler/index.mjs.map +3 -3
  3. package/dist/lib/browser/chunk-7NQ77AIQ.mjs +618 -0
  4. package/dist/lib/browser/chunk-7NQ77AIQ.mjs.map +7 -0
  5. package/dist/lib/browser/edge/index.mjs +20 -8
  6. package/dist/lib/browser/edge/index.mjs.map +3 -3
  7. package/dist/lib/browser/index.mjs +149 -87
  8. package/dist/lib/browser/index.mjs.map +4 -4
  9. package/dist/lib/browser/meta.json +1 -1
  10. package/dist/lib/browser/testing/index.mjs +69 -38
  11. package/dist/lib/browser/testing/index.mjs.map +3 -3
  12. package/dist/lib/node-esm/bundler/index.mjs +54 -38
  13. package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
  14. package/dist/lib/node-esm/chunk-KCGC6QQT.mjs +620 -0
  15. package/dist/lib/node-esm/chunk-KCGC6QQT.mjs.map +7 -0
  16. package/dist/lib/node-esm/edge/index.mjs +20 -8
  17. package/dist/lib/node-esm/edge/index.mjs.map +3 -3
  18. package/dist/lib/node-esm/index.mjs +149 -87
  19. package/dist/lib/node-esm/index.mjs.map +4 -4
  20. package/dist/lib/node-esm/meta.json +1 -1
  21. package/dist/lib/node-esm/testing/index.mjs +69 -38
  22. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  23. package/dist/types/src/bundler/bundler.d.ts +11 -12
  24. package/dist/types/src/bundler/bundler.d.ts.map +1 -1
  25. package/dist/types/src/edge/functions.d.ts +3 -2
  26. package/dist/types/src/edge/functions.d.ts.map +1 -1
  27. package/dist/types/src/errors.d.ts +10 -8
  28. package/dist/types/src/errors.d.ts.map +1 -1
  29. package/dist/types/src/examples/fib.d.ts +7 -0
  30. package/dist/types/src/examples/fib.d.ts.map +1 -0
  31. package/dist/types/src/examples/reply.d.ts +3 -0
  32. package/dist/types/src/examples/reply.d.ts.map +1 -0
  33. package/dist/types/src/examples/sleep.d.ts +5 -0
  34. package/dist/types/src/examples/sleep.d.ts.map +1 -0
  35. package/dist/types/src/executor/executor.d.ts +4 -1
  36. package/dist/types/src/executor/executor.d.ts.map +1 -1
  37. package/dist/types/src/handler.d.ts +10 -7
  38. package/dist/types/src/handler.d.ts.map +1 -1
  39. package/dist/types/src/schema.d.ts +7 -2
  40. package/dist/types/src/schema.d.ts.map +1 -1
  41. package/dist/types/src/services/credentials.d.ts +16 -3
  42. package/dist/types/src/services/credentials.d.ts.map +1 -1
  43. package/dist/types/src/services/database.d.ts +75 -6
  44. package/dist/types/src/services/database.d.ts.map +1 -1
  45. package/dist/types/src/services/event-logger.d.ts +65 -30
  46. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  47. package/dist/types/src/services/index.d.ts +2 -1
  48. package/dist/types/src/services/index.d.ts.map +1 -1
  49. package/dist/types/src/services/local-function-execution.d.ts +12 -0
  50. package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
  51. package/dist/types/src/services/queues.d.ts +20 -6
  52. package/dist/types/src/services/queues.d.ts.map +1 -1
  53. package/dist/types/src/services/remote-function-execution-service.d.ts +15 -0
  54. package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -0
  55. package/dist/types/src/services/service-container.d.ts +5 -5
  56. package/dist/types/src/services/service-container.d.ts.map +1 -1
  57. package/dist/types/src/services/service-registry.d.ts +1 -1
  58. package/dist/types/src/services/service-registry.d.ts.map +1 -1
  59. package/dist/types/src/services/tracing.d.ts +36 -5
  60. package/dist/types/src/services/tracing.d.ts.map +1 -1
  61. package/dist/types/src/testing/layer.d.ts +6 -2
  62. package/dist/types/src/testing/layer.d.ts.map +1 -1
  63. package/dist/types/src/testing/logger.d.ts +3 -3
  64. package/dist/types/src/testing/logger.d.ts.map +1 -1
  65. package/dist/types/src/testing/persist-database.test.d.ts +2 -0
  66. package/dist/types/src/testing/persist-database.test.d.ts.map +1 -0
  67. package/dist/types/src/testing/services.d.ts +6 -17
  68. package/dist/types/src/testing/services.d.ts.map +1 -1
  69. package/dist/types/src/trace.d.ts +34 -8
  70. package/dist/types/src/trace.d.ts.map +1 -1
  71. package/dist/types/src/types.d.ts +141 -224
  72. package/dist/types/src/types.d.ts.map +1 -1
  73. package/dist/types/src/url.d.ts +10 -6
  74. package/dist/types/src/url.d.ts.map +1 -1
  75. package/dist/types/tsconfig.tsbuildinfo +1 -1
  76. package/package.json +37 -31
  77. package/src/bundler/bundler.test.ts +8 -9
  78. package/src/bundler/bundler.ts +32 -33
  79. package/src/edge/functions.ts +8 -5
  80. package/src/examples/fib.ts +30 -0
  81. package/src/examples/reply.ts +18 -0
  82. package/src/examples/sleep.ts +22 -0
  83. package/src/executor/executor.ts +9 -9
  84. package/src/handler.ts +12 -10
  85. package/src/schema.ts +11 -0
  86. package/src/services/credentials.ts +79 -3
  87. package/src/services/database.ts +118 -18
  88. package/src/services/event-logger.ts +68 -37
  89. package/src/services/index.ts +2 -1
  90. package/src/services/local-function-execution.ts +77 -0
  91. package/src/services/queues.ts +32 -10
  92. package/src/services/remote-function-execution-service.ts +46 -0
  93. package/src/services/service-container.ts +11 -10
  94. package/src/services/service-registry.ts +5 -2
  95. package/src/services/tracing.ts +99 -7
  96. package/src/testing/layer.ts +69 -3
  97. package/src/testing/logger.ts +4 -4
  98. package/src/testing/persist-database.test.ts +87 -0
  99. package/src/testing/services.ts +9 -62
  100. package/src/trace.ts +5 -7
  101. package/src/types.ts +17 -25
  102. package/src/url.ts +13 -10
  103. package/dist/lib/browser/chunk-3NGCSUEW.mjs +0 -328
  104. package/dist/lib/browser/chunk-3NGCSUEW.mjs.map +0 -7
  105. package/dist/lib/node-esm/chunk-FJ2MU7TL.mjs +0 -330
  106. package/dist/lib/node-esm/chunk-FJ2MU7TL.mjs.map +0 -7
  107. package/dist/types/src/services/function-call-service.d.ts +0 -16
  108. package/dist/types/src/services/function-call-service.d.ts.map +0 -1
  109. package/src/services/function-call-service.ts +0 -64
@@ -2,13 +2,18 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context, Effect, Layer } from 'effect';
5
+ import { Context, Effect, Layer, Option, 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, SchemaRegistryQuery } from '@dxos/echo-db';
9
+ import type { SchemaRegistryPreparedQuery } from '@dxos/echo-db';
10
+ import type { EchoSchema } from '@dxos/echo-schema';
11
+ import { promiseWithCauseCapture } from '@dxos/effect';
12
+ import { BaseError } from '@dxos/errors';
13
+ import { invariant } from '@dxos/invariant';
9
14
  import type { DXN } from '@dxos/keys';
10
15
 
11
- export class DatabaseService extends Context.Tag('DatabaseService')<
16
+ export class DatabaseService extends Context.Tag('@dxos/functions/DatabaseService')<
12
17
  DatabaseService,
13
18
  {
14
19
  readonly db: EchoDatabase;
@@ -28,23 +33,96 @@ export class DatabaseService extends Context.Tag('DatabaseService')<
28
33
  };
29
34
  };
30
35
 
31
- static resolve: (dxn: DXN) => Effect.Effect<Obj.Any | Relation.Any, Error, DatabaseService> = Effect.fn(
32
- function* (dxn) {
36
+ static layer = (db: EchoDatabase): Layer.Layer<DatabaseService> => {
37
+ return Layer.succeed(DatabaseService, DatabaseService.make(db));
38
+ };
39
+
40
+ /**
41
+ * Resolves an object by its DXN.
42
+ */
43
+ static resolve: {
44
+ // No type check.
45
+ (dxn: DXN): Effect.Effect<Obj.Any | Relation.Any, never, DatabaseService>;
46
+ // Check matches schema.
47
+ <S extends Type.Obj.Any | Type.Relation.Any>(
48
+ dxn: DXN,
49
+ schema: S,
50
+ ): Effect.Effect<Schema.Schema.Type<S>, ObjectNotFoundError, DatabaseService>;
51
+ } = (<S extends Type.Obj.Any | Type.Relation.Any>(
52
+ dxn: DXN,
53
+ schema?: S,
54
+ ): Effect.Effect<Schema.Schema.Type<S>, ObjectNotFoundError, DatabaseService> =>
55
+ Effect.gen(function* () {
33
56
  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
- );
57
+ const object = yield* promiseWithCauseCapture(() =>
58
+ db.graph
59
+ .createRefResolver({
60
+ context: {
61
+ space: db.spaceId,
62
+ },
63
+ })
64
+ .resolve(dxn),
65
+ );
66
+
67
+ if (!object) {
68
+ return yield* Effect.fail(new ObjectNotFoundError({ dxn }));
69
+ }
70
+ invariant(!schema || Obj.instanceOf(schema, object), 'Object type mismatch.');
71
+ return object as any;
72
+ })) as any;
73
+
74
+ /**
75
+ * Loads an object reference.
76
+ */
77
+ static load: <T>(ref: Ref.Ref<T>) => Effect.Effect<T, ObjectNotFoundError, never> = Effect.fn(function* (ref) {
78
+ const object = yield* promiseWithCauseCapture(() => ref.tryLoad());
79
+ if (!object) {
80
+ return yield* Effect.fail(new ObjectNotFoundError({ dxn: ref.dxn }));
81
+ }
82
+ return object;
83
+ });
43
84
 
44
- static loadRef: <T>(ref: Ref.Ref<T>) => Effect.Effect<T, never, never> = Effect.fn(function* (ref) {
45
- return yield* Effect.promise(() => ref.load());
85
+ /**
86
+ * Loads an object reference option.
87
+ */
88
+ // TODO(burdon): Option?
89
+ static loadOption: <T>(ref: Ref.Ref<T>) => Effect.Effect<Option.Option<T>, never, never> = Effect.fn(function* (ref) {
90
+ const object = yield* DatabaseService.load(ref).pipe(
91
+ Effect.catchTag('OBJECT_NOT_FOUND', () => Effect.succeed(undefined)),
92
+ );
93
+ return Option.fromNullable(object);
46
94
  });
47
95
 
96
+ // TODO(burdon): Can we create a proxy for the following methods on EchoDatabase? Use @inheritDoc?
97
+ // TODO(burdon): Figure out how to chain query().run();
98
+
99
+ /**
100
+ * @link EchoDatabase.add
101
+ */
102
+ static add = <T extends Obj.Any | Relation.Any>(obj: T): Effect.Effect<T, never, DatabaseService> =>
103
+ DatabaseService.pipe(Effect.map(({ db }) => db.add(obj)));
104
+
105
+ /**
106
+ * @link EchoDatabase.remove
107
+ */
108
+ static remove = <T extends Obj.Any | Relation.Any>(obj: T): Effect.Effect<void, never, DatabaseService> =>
109
+ DatabaseService.pipe(Effect.map(({ db }) => db.remove(obj)));
110
+
111
+ /**
112
+ * @link EchoDatabase.flush
113
+ */
114
+ static flush = (opts?: FlushOptions) =>
115
+ DatabaseService.pipe(Effect.flatMap(({ db }) => promiseWithCauseCapture(() => db.flush(opts))));
116
+
117
+ /**
118
+ * @link EchoDatabase.getObjectById
119
+ */
120
+ static getObjectById = <T extends Obj.Any | Relation.Any>(
121
+ id: string,
122
+ ): Effect.Effect<Live<T> | undefined, never, DatabaseService> => {
123
+ return DatabaseService.pipe(Effect.map(({ db }) => db.getObjectById(id)));
124
+ };
125
+
48
126
  /**
49
127
  * Creates a `QueryResult` object that can be subscribed to.
50
128
  */
@@ -65,6 +143,28 @@ export class DatabaseService extends Context.Tag('DatabaseService')<
65
143
  <F extends Filter.Any>(filter: F): Effect.Effect<OneShotQueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;
66
144
  } = (queryOrFilter: Query.Any | Filter.Any) =>
67
145
  DatabaseService.query(queryOrFilter as any).pipe(
68
- Effect.flatMap((queryResult) => Effect.promise(() => queryResult.run())),
146
+ Effect.flatMap((queryResult) => promiseWithCauseCapture(() => queryResult.run())),
69
147
  );
148
+
149
+ static schemaQuery = <Q extends SchemaRegistryQuery>(
150
+ query: Q,
151
+ ): Effect.Effect<SchemaRegistryPreparedQuery<EchoSchema>, never, DatabaseService> =>
152
+ DatabaseService.pipe(
153
+ Effect.map(({ db }) => db.schemaRegistry.query(query)),
154
+ Effect.withSpan('DatabaseService.schemaQuery'),
155
+ );
156
+
157
+ static runSchemaQuery = <Q extends SchemaRegistryQuery>(
158
+ query: Q,
159
+ ): Effect.Effect<EchoSchema[], never, DatabaseService> =>
160
+ DatabaseService.schemaQuery(query).pipe(
161
+ Effect.flatMap((queryResult) => promiseWithCauseCapture(() => queryResult.run())),
162
+ );
163
+ }
164
+
165
+ // TODO(burdon): Move to echo/errors.
166
+ class ObjectNotFoundError extends BaseError.extend('OBJECT_NOT_FOUND') {
167
+ constructor(context?: Record<string, unknown>) {
168
+ super('Object not found', { context });
169
+ }
70
170
  }
@@ -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,77 @@
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(functionDef: FunctionDefinition<any, any>, input: unknown): Effect.Effect<unknown, never, Services>;
20
+ }
21
+ >() {
22
+ static layer = Layer.succeed(LocalFunctionExecutionService, {
23
+ invokeFunction: (functionDef, input) => invokeFunction(functionDef, input),
24
+ });
25
+
26
+ static invokeFunction: <F extends FunctionDefinition.Any>(
27
+ functionDef: 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 = (
34
+ functionDef: FunctionDefinition<any, any>,
35
+ input: any,
36
+ ): Effect.Effect<unknown, never, Services> =>
37
+ Effect.gen(function* () {
38
+ // Assert input matches schema
39
+ const assertInput = functionDef.inputSchema.pipe(Schema.asserts);
40
+ (assertInput as any)(input);
41
+
42
+ const context: FunctionContext = {
43
+ space: undefined,
44
+ getService: () => todo(),
45
+ getSpace: async (_spaceId: any) => {
46
+ throw new Error('Not available. Use the database service instead.');
47
+ },
48
+ };
49
+
50
+ // TODO(dmaretskyi): This should be delegated to a function invoker service.
51
+ const data = yield* Effect.gen(function* () {
52
+ const result = functionDef.handler({ context, data: input });
53
+ if (Effect.isEffect(result)) {
54
+ return yield* (result as Effect.Effect<unknown, unknown, Services>).pipe(Effect.orDie);
55
+ } else if (
56
+ typeof result === 'object' &&
57
+ result !== null &&
58
+ 'then' in result &&
59
+ typeof result.then === 'function'
60
+ ) {
61
+ return yield* Effect.promise(() => result);
62
+ } else {
63
+ return result;
64
+ }
65
+ }).pipe(
66
+ Effect.orDie,
67
+ Effect.catchAllDefect((defect) =>
68
+ Effect.die(new FunctionError('Error running function', { context: { name: functionDef.name }, cause: defect })),
69
+ ),
70
+ );
71
+
72
+ // Assert output matches schema
73
+ const assertOutput = functionDef.outputSchema?.pipe(Schema.asserts);
74
+ (assertOutput as any)(data);
75
+
76
+ return data;
77
+ }).pipe(Effect.withSpan('invokeFunction', { attributes: { name: functionDef.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
+ }
@@ -0,0 +1,46 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { Context, Layer } from 'effect';
6
+
7
+ import type { SpaceId } from '@dxos/keys';
8
+
9
+ import { getInvocationUrl } from '../url';
10
+
11
+ /**
12
+ * Allows calling into other functions.
13
+ */
14
+ export class RemoteFunctionExecutionService extends Context.Tag('@dxos/functions/RemoteFunctionExecutionService')<
15
+ RemoteFunctionExecutionService,
16
+ {
17
+ callFunction(deployedFunctionId: string, input: any, spaceId?: SpaceId): Promise<any>;
18
+ }
19
+ >() {
20
+ static fromClient(baseUrl: string, spaceId: SpaceId): Context.Tag.Service<RemoteFunctionExecutionService> {
21
+ return {
22
+ callFunction: async (deployedFunctionId: string, input: any) => {
23
+ const url = getInvocationUrl(deployedFunctionId, baseUrl, { spaceId });
24
+ const result = await fetch(url, {
25
+ method: 'POST',
26
+ headers: { 'Content-Type': 'application/json' },
27
+ body: JSON.stringify(input),
28
+ });
29
+ if (result.status >= 300 || result.status < 200) {
30
+ throw new Error('Failed to invoke function', { cause: new Error(`HTTP error: ${await result.text()}`) });
31
+ }
32
+ return await result.json();
33
+ },
34
+ };
35
+ }
36
+
37
+ static mock = (): Context.Tag.Service<RemoteFunctionExecutionService> => {
38
+ return {
39
+ callFunction: async (deployedFunctionId: string, input: any) => {
40
+ return input;
41
+ },
42
+ };
43
+ };
44
+
45
+ static mockLayer = Layer.succeed(RemoteFunctionExecutionService, RemoteFunctionExecutionService.mock());
46
+ }
@@ -9,9 +9,9 @@ import { entries } from '@dxos/util';
9
9
 
10
10
  import { ConfiguredCredentialsService, CredentialsService } from './credentials';
11
11
  import { DatabaseService } from './database';
12
- import { EventLogger } from './event-logger';
13
- import { FunctionCallService } from './function-call-service';
12
+ import { ComputeEventLogger } from './event-logger';
14
13
  import { QueueService } from './queues';
14
+ import { RemoteFunctionExecutionService } from './remote-function-execution-service';
15
15
  import { TracingService } from './tracing';
16
16
 
17
17
  // TODO(dmaretskyi): Refactor this module to only rely on tags and not the human-assigned names.
@@ -20,11 +20,11 @@ import { TracingService } from './tracing';
20
20
  * List of all services.
21
21
  */
22
22
  const SERVICES = {
23
- ai: AiService,
23
+ ai: AiService.AiService,
24
24
  credentials: CredentialsService,
25
25
  database: DatabaseService,
26
- eventLogger: EventLogger,
27
- functionCallService: FunctionCallService,
26
+ eventLogger: ComputeEventLogger,
27
+ functionCallService: RemoteFunctionExecutionService,
28
28
  queues: QueueService,
29
29
  tracing: TracingService,
30
30
  } as const satisfies Record<string, Context.TagClass<any, string, any>>;
@@ -88,9 +88,10 @@ export class ServiceContainer {
88
88
  return new ServiceContainer().setServices({ ...this._services });
89
89
  }
90
90
 
91
- // TODO(dmaretskyi): `getService` is designed to error at runtime if the service is not available, but layer forces us to provide all services and makes stubs for the ones that are not available.
91
+ // TODO(dmaretskyi): `getService` is designed to error at runtime if the service is not available, but Layer forces us to provide all services and makes stubs for the ones that are not available.
92
92
  createLayer(): Layer.Layer<Services> {
93
- const ai = this._services.ai != null ? Layer.succeed(AiService, this._services.ai) : AiService.notAvailable;
93
+ const ai =
94
+ this._services.ai != null ? Layer.succeed(AiService.AiService, this._services.ai) : AiService.notAvailable;
94
95
  const credentials = Layer.succeed(
95
96
  CredentialsService,
96
97
  this._services.credentials ?? new ConfiguredCredentialsService(),
@@ -102,10 +103,10 @@ export class ServiceContainer {
102
103
  const queues =
103
104
  this._services.queues != null ? Layer.succeed(QueueService, this._services.queues) : QueueService.notAvailable;
104
105
  const tracing = Layer.succeed(TracingService, this._services.tracing ?? TracingService.noop);
105
- const eventLogger = Layer.succeed(EventLogger, this._services.eventLogger ?? EventLogger.noop);
106
+ const eventLogger = Layer.succeed(ComputeEventLogger, this._services.eventLogger ?? ComputeEventLogger.noop);
106
107
  const functionCallService = Layer.succeed(
107
- FunctionCallService,
108
- this._services.functionCallService ?? FunctionCallService.mock(),
108
+ RemoteFunctionExecutionService,
109
+ this._services.functionCallService ?? RemoteFunctionExecutionService.mock(),
109
110
  );
110
111
 
111
112
  return Layer.mergeAll(ai, credentials, database, queues, tracing, eventLogger, functionCallService);
@@ -2,7 +2,7 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context, Effect, flow, type Option } from 'effect';
5
+ import { Context, Effect, type Option, flow } from 'effect';
6
6
 
7
7
  import { ServiceNotAvailableError } from '../errors';
8
8
 
@@ -12,7 +12,10 @@ export namespace ServiceRegistry {
12
12
  }
13
13
  }
14
14
 
15
- export class ServiceRegistry extends Context.Tag('ServiceRegistry')<ServiceRegistry, ServiceRegistry.Service>() {
15
+ export class ServiceRegistry extends Context.Tag('@dxos/functions/ServiceRegistry')<
16
+ ServiceRegistry,
17
+ ServiceRegistry.Service
18
+ >() {
16
19
  /**
17
20
  * Resolves the service from the registry.
18
21
  * @param tag Service tag to resolve.