@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.
Files changed (118) hide show
  1. package/dist/lib/browser/bundler/index.mjs +22 -10
  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 +2 -2
  6. package/dist/lib/browser/edge/index.mjs.map +1 -1
  7. package/dist/lib/browser/index.mjs +51 -37
  8. package/dist/lib/browser/index.mjs.map +3 -3
  9. package/dist/lib/browser/meta.json +1 -1
  10. package/dist/lib/browser/testing/index.mjs +39 -8
  11. package/dist/lib/browser/testing/index.mjs.map +4 -4
  12. package/dist/lib/node-esm/bundler/index.mjs +22 -10
  13. package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
  14. package/dist/lib/node-esm/chunk-K7REX6H4.mjs +488 -0
  15. package/dist/lib/node-esm/chunk-K7REX6H4.mjs.map +7 -0
  16. package/dist/lib/node-esm/edge/index.mjs +2 -2
  17. package/dist/lib/node-esm/edge/index.mjs.map +1 -1
  18. package/dist/lib/node-esm/index.mjs +51 -37
  19. package/dist/lib/node-esm/index.mjs.map +3 -3
  20. package/dist/lib/node-esm/meta.json +1 -1
  21. package/dist/lib/node-esm/testing/index.mjs +39 -8
  22. package/dist/lib/node-esm/testing/index.mjs.map +4 -4
  23. package/dist/types/src/bundler/bundler.d.ts +1 -2
  24. package/dist/types/src/bundler/bundler.d.ts.map +1 -1
  25. package/dist/types/src/edge/functions.d.ts +1 -1
  26. package/dist/types/src/edge/functions.d.ts.map +1 -1
  27. package/dist/types/src/errors.d.ts +70 -0
  28. package/dist/types/src/errors.d.ts.map +1 -0
  29. package/dist/types/src/executor/executor.d.ts +3 -0
  30. package/dist/types/src/executor/executor.d.ts.map +1 -1
  31. package/dist/types/src/handler.d.ts +16 -15
  32. package/dist/types/src/handler.d.ts.map +1 -1
  33. package/dist/types/src/index.d.ts +1 -0
  34. package/dist/types/src/index.d.ts.map +1 -1
  35. package/dist/types/src/schema.d.ts +5 -5
  36. package/dist/types/src/services/credentials.d.ts +7 -3
  37. package/dist/types/src/services/credentials.d.ts.map +1 -1
  38. package/dist/types/src/services/database.d.ts +71 -3
  39. package/dist/types/src/services/database.d.ts.map +1 -1
  40. package/dist/types/src/services/event-logger.d.ts +65 -30
  41. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  42. package/dist/types/src/services/index.d.ts +2 -2
  43. package/dist/types/src/services/index.d.ts.map +1 -1
  44. package/dist/types/src/services/local-function-execution.d.ts +12 -0
  45. package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
  46. package/dist/types/src/services/queues.d.ts +32 -6
  47. package/dist/types/src/services/queues.d.ts.map +1 -1
  48. package/dist/types/src/services/remote-function-execution-service.d.ts +15 -0
  49. package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -0
  50. package/dist/types/src/services/service-container.d.ts +29 -17
  51. package/dist/types/src/services/service-container.d.ts.map +1 -1
  52. package/dist/types/src/services/service-registry.d.ts +29 -0
  53. package/dist/types/src/services/service-registry.d.ts.map +1 -0
  54. package/dist/types/src/services/service-registry.test.d.ts +2 -0
  55. package/dist/types/src/services/service-registry.test.d.ts.map +1 -0
  56. package/dist/types/src/services/tracing.d.ts +13 -2
  57. package/dist/types/src/services/tracing.d.ts.map +1 -1
  58. package/dist/types/src/testing/index.d.ts +1 -0
  59. package/dist/types/src/testing/index.d.ts.map +1 -1
  60. package/dist/types/src/testing/layer.d.ts +10 -0
  61. package/dist/types/src/testing/layer.d.ts.map +1 -0
  62. package/dist/types/src/testing/logger.d.ts +3 -3
  63. package/dist/types/src/testing/logger.d.ts.map +1 -1
  64. package/dist/types/src/testing/services.d.ts +55 -9
  65. package/dist/types/src/testing/services.d.ts.map +1 -1
  66. package/dist/types/src/trace.d.ts +3 -5
  67. package/dist/types/src/trace.d.ts.map +1 -1
  68. package/dist/types/src/translations.d.ts +9 -9
  69. package/dist/types/src/translations.d.ts.map +1 -1
  70. package/dist/types/src/types.d.ts +7 -7
  71. package/dist/types/src/types.d.ts.map +1 -1
  72. package/dist/types/tsconfig.tsbuildinfo +1 -1
  73. package/package.json +36 -26
  74. package/src/bundler/bundler.test.ts +1 -1
  75. package/src/bundler/bundler.ts +26 -4
  76. package/src/edge/functions.ts +1 -1
  77. package/src/errors.ts +13 -0
  78. package/src/executor/executor.ts +20 -13
  79. package/src/handler.ts +34 -25
  80. package/src/index.ts +1 -0
  81. package/src/services/credentials.ts +47 -4
  82. package/src/services/database.ts +91 -3
  83. package/src/services/event-logger.ts +68 -37
  84. package/src/services/index.ts +2 -2
  85. package/src/services/local-function-execution.ts +74 -0
  86. package/src/services/queues.ts +51 -11
  87. package/src/services/{function-call-service.ts → remote-function-execution-service.ts} +7 -5
  88. package/src/services/service-container.ts +47 -42
  89. package/src/services/service-registry.test.ts +42 -0
  90. package/src/services/service-registry.ts +59 -0
  91. package/src/services/tracing.ts +19 -2
  92. package/src/testing/index.ts +1 -0
  93. package/src/testing/layer.ts +31 -0
  94. package/src/testing/logger.ts +4 -4
  95. package/src/testing/services.ts +97 -14
  96. package/src/trace.ts +3 -5
  97. package/src/translations.ts +4 -4
  98. package/src/types.ts +1 -1
  99. package/dist/lib/browser/chunk-WEFZUEL2.mjs +0 -300
  100. package/dist/lib/browser/chunk-WEFZUEL2.mjs.map +0 -7
  101. package/dist/lib/node/bundler/index.cjs +0 -260
  102. package/dist/lib/node/bundler/index.cjs.map +0 -7
  103. package/dist/lib/node/chunk-IJAE7FZK.cjs +0 -320
  104. package/dist/lib/node/chunk-IJAE7FZK.cjs.map +0 -7
  105. package/dist/lib/node/edge/index.cjs +0 -94
  106. package/dist/lib/node/edge/index.cjs.map +0 -7
  107. package/dist/lib/node/index.cjs +0 -522
  108. package/dist/lib/node/index.cjs.map +0 -7
  109. package/dist/lib/node/meta.json +0 -1
  110. package/dist/lib/node/testing/index.cjs +0 -43
  111. package/dist/lib/node/testing/index.cjs.map +0 -7
  112. package/dist/lib/node-esm/chunk-LIYPMWNQ.mjs +0 -302
  113. package/dist/lib/node-esm/chunk-LIYPMWNQ.mjs.map +0 -7
  114. package/dist/types/src/services/ai.d.ts +0 -12
  115. package/dist/types/src/services/ai.d.ts.map +0 -1
  116. package/dist/types/src/services/function-call-service.d.ts +0 -16
  117. package/dist/types/src/services/function-call-service.d.ts.map +0 -1
  118. package/src/services/ai.ts +0 -32
@@ -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,11 +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';
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,36 +2,76 @@
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 { Queue, QueueFactory } from '@dxos/echo-db';
7
+ import type { Obj, Relation } from '@dxos/echo';
8
+ import type { Queue, QueueAPI, QueueFactory } from '@dxos/echo-db';
9
+ import type { DXN, QueueSubspaceTag } from '@dxos/keys';
8
10
 
9
- export class QueueService extends Context.Tag('QueueService')<
11
+ /**
12
+ * Gives access to all queues.
13
+ */
14
+ export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
10
15
  QueueService,
11
16
  {
12
17
  /**
13
18
  * API to access the queues.
14
19
  */
15
- readonly queues: QueueFactory;
20
+ readonly queues: QueueAPI;
16
21
 
17
22
  /**
18
23
  * The queue that is used to store the context of the current research.
24
+ * @deprecated Use `ContextQueueService` instead.
19
25
  */
20
- // TODO(dmaretskyi): Is this really part of the queue service?
21
- readonly contextQueue: Queue | undefined;
26
+ readonly queue: Queue | undefined;
22
27
  }
23
28
  >() {
24
29
  static notAvailable = Layer.succeed(QueueService, {
25
- get queues(): QueueFactory {
26
- throw new Error('Queues not available');
30
+ queues: {
31
+ get(_dxn) {
32
+ throw new Error('Queues not available');
33
+ },
34
+ create() {
35
+ throw new Error('Queues not available');
36
+ },
27
37
  },
28
- contextQueue: undefined,
38
+ queue: undefined,
29
39
  });
30
40
 
31
- static make = (queues: QueueFactory, contextQueue: Queue | undefined): Context.Tag.Service<QueueService> => {
41
+ static make = (queues: QueueFactory, queue?: Queue): Context.Tag.Service<QueueService> => {
32
42
  return {
33
43
  queues,
34
- contextQueue,
44
+ queue,
35
45
  };
36
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)));
65
+ }
66
+
67
+ /**
68
+ * Gives access to a specific queue passed as a context.
69
+ */
70
+ export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQueueService')<
71
+ ContextQueueService,
72
+ {
73
+ readonly queue: Queue;
74
+ }
75
+ >() {
76
+ static layer = (queue: Queue) => Layer.succeed(ContextQueueService, { queue });
37
77
  }
@@ -2,20 +2,20 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context } from 'effect';
5
+ import { Context, Layer } from 'effect';
6
6
 
7
7
  import type { SpaceId } from '@dxos/keys';
8
8
 
9
9
  /**
10
10
  * Allows calling into other functions.
11
11
  */
12
- export class FunctionCallService extends Context.Tag('FunctionCallService')<
13
- FunctionCallService,
12
+ export class RemoteFunctionExecutionService extends Context.Tag('@dxos/functions/RemoteFunctionExecutionService')<
13
+ RemoteFunctionExecutionService,
14
14
  {
15
15
  callFunction(deployedFunctionId: string, input: any, spaceId?: SpaceId): Promise<any>;
16
16
  }
17
17
  >() {
18
- static fromClient(baseUrl: string, spaceId: SpaceId): Context.Tag.Service<FunctionCallService> {
18
+ static fromClient(baseUrl: string, spaceId: SpaceId): Context.Tag.Service<RemoteFunctionExecutionService> {
19
19
  return {
20
20
  callFunction: async (deployedFunctionId: string, input: any) => {
21
21
  const url = getInvocationUrl(deployedFunctionId, baseUrl, { spaceId });
@@ -32,13 +32,15 @@ export class FunctionCallService extends Context.Tag('FunctionCallService')<
32
32
  };
33
33
  }
34
34
 
35
- static mock = () => {
35
+ static mock = (): Context.Tag.Service<RemoteFunctionExecutionService> => {
36
36
  return {
37
37
  callFunction: async (deployedFunctionId: string, input: any) => {
38
38
  return input;
39
39
  },
40
40
  };
41
41
  };
42
+
43
+ static mockLayer = Layer.succeed(RemoteFunctionExecutionService, RemoteFunctionExecutionService.mock());
42
44
  }
43
45
 
44
46
  // TODO(dmaretskyi): Reconcile with `getInvocationUrl` in `@dxos/functions/edge`.
@@ -2,65 +2,65 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Layer, type Context } from 'effect';
5
+ import { type Context, Layer } from 'effect';
6
+
7
+ import { AiService } from '@dxos/ai';
8
+ import { entries } from '@dxos/util';
6
9
 
7
- import { AiService } from './ai';
8
10
  import { ConfiguredCredentialsService, CredentialsService } from './credentials';
9
11
  import { DatabaseService } from './database';
10
- import { EventLogger } from './event-logger';
11
- import { FunctionCallService } from './function-call-service';
12
+ import { ComputeEventLogger } from './event-logger';
12
13
  import { QueueService } from './queues';
14
+ import { RemoteFunctionExecutionService } from './remote-function-execution-service';
13
15
  import { TracingService } from './tracing';
14
16
 
17
+ // TODO(dmaretskyi): Refactor this module to only rely on tags and not the human-assigned names.
18
+
15
19
  /**
16
- * List of all service tags and their names.
20
+ * List of all services.
17
21
  */
18
- export interface ServiceTagRecord {
19
- ai: AiService;
20
- credentials: CredentialsService;
21
- database: DatabaseService;
22
- eventLogger: EventLogger;
23
- functionCallService: FunctionCallService;
24
- tracing: TracingService;
25
- queues: QueueService;
26
- }
22
+ const SERVICES = {
23
+ ai: AiService.AiService,
24
+ credentials: CredentialsService,
25
+ database: DatabaseService,
26
+ eventLogger: ComputeEventLogger,
27
+ functionCallService: RemoteFunctionExecutionService,
28
+ queues: QueueService,
29
+ tracing: TracingService,
30
+ } as const satisfies Record<string, Context.TagClass<any, string, any>>;
31
+
32
+ /**
33
+ * Mapping of service names to their tags.
34
+ */
35
+ export type ServiceTagRecord = {
36
+ [K in keyof typeof SERVICES]: (typeof SERVICES)[K] extends { new (_: never): infer T } ? T : never;
37
+ };
27
38
 
28
39
  /**
29
- * List of all services and their runtime types.
40
+ * Mapping of service names to their runtime types.
30
41
  */
31
42
  export type ServiceRecord = {
32
43
  [K in keyof ServiceTagRecord]: Context.Tag.Service<ServiceTagRecord[K]>;
33
44
  };
34
45
 
35
46
  /**
36
- * Union of all services.
47
+ * Union of all services tags.
37
48
  */
38
49
  export type Services = ServiceTagRecord[keyof ServiceTagRecord];
39
50
 
40
- const SERVICE_MAPPING: Record<string, keyof ServiceRecord> = {
41
- [AiService.key]: 'ai',
42
- [CredentialsService.key]: 'credentials',
43
- [DatabaseService.key]: 'database',
44
- [EventLogger.key]: 'eventLogger',
45
- [FunctionCallService.key]: 'functionCallService',
46
- [QueueService.key]: 'queues',
47
- [TracingService.key]: 'tracing',
48
- };
51
+ const SERVICE_MAPPING: Record<string, keyof ServiceRecord> = Object.fromEntries(
52
+ entries(SERVICES).map(([name, tag]) => [tag.key, name]),
53
+ );
49
54
 
50
- export const SERVICE_TAGS: Context.Tag<any, any>[] = [
51
- AiService,
52
- CredentialsService,
53
- DatabaseService,
54
- EventLogger,
55
- FunctionCallService,
56
- TracingService,
57
- QueueService,
58
- ];
55
+ export const SERVICE_TAGS: Context.Tag<any, any>[] = Object.values(SERVICES);
59
56
 
60
57
  const DEFAULT_SERVICES: Partial<ServiceRecord> = {
61
58
  tracing: TracingService.noop,
62
59
  };
63
60
 
61
+ /**
62
+ * @deprecated
63
+ */
64
64
  export class ServiceContainer {
65
65
  private _services: Partial<ServiceRecord> = { ...DEFAULT_SERVICES };
66
66
 
@@ -74,23 +74,28 @@ export class ServiceContainer {
74
74
  return this;
75
75
  }
76
76
 
77
- getService<T extends Context.Tag<any, any>>(tag: T): Context.Tag.Service<T> {
77
+ getService<Id, T>(tag: Context.Tag<Id, T>): T {
78
78
  const serviceKey = SERVICE_MAPPING[tag.key];
79
79
  const service = serviceKey != null ? this._services[serviceKey] : undefined;
80
80
  if (!service) {
81
81
  throw new Error(`Service not available: ${tag.key}`);
82
82
  }
83
- return service as Context.Tag.Service<T>;
83
+
84
+ return service as T;
84
85
  }
85
86
 
86
87
  clone(): ServiceContainer {
87
88
  return new ServiceContainer().setServices({ ...this._services });
88
89
  }
89
90
 
90
- // 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.
91
92
  createLayer(): Layer.Layer<Services> {
92
- const ai = this._services.ai != null ? Layer.succeed(AiService, this._services.ai) : AiService.notAvailable;
93
- const credentials = Layer.succeed(CredentialsService, new ConfiguredCredentialsService());
93
+ const ai =
94
+ this._services.ai != null ? Layer.succeed(AiService.AiService, this._services.ai) : AiService.notAvailable;
95
+ const credentials = Layer.succeed(
96
+ CredentialsService,
97
+ this._services.credentials ?? new ConfiguredCredentialsService(),
98
+ );
94
99
  const database =
95
100
  this._services.database != null
96
101
  ? Layer.succeed(DatabaseService, this._services.database)
@@ -98,10 +103,10 @@ export class ServiceContainer {
98
103
  const queues =
99
104
  this._services.queues != null ? Layer.succeed(QueueService, this._services.queues) : QueueService.notAvailable;
100
105
  const tracing = Layer.succeed(TracingService, this._services.tracing ?? TracingService.noop);
101
- const eventLogger = Layer.succeed(EventLogger, this._services.eventLogger ?? EventLogger.noop);
106
+ const eventLogger = Layer.succeed(ComputeEventLogger, this._services.eventLogger ?? ComputeEventLogger.noop);
102
107
  const functionCallService = Layer.succeed(
103
- FunctionCallService,
104
- this._services.functionCallService ?? FunctionCallService.mock(),
108
+ RemoteFunctionExecutionService,
109
+ this._services.functionCallService ?? RemoteFunctionExecutionService.mock(),
105
110
  );
106
111
 
107
112
  return Layer.mergeAll(ai, credentials, database, queues, tracing, eventLogger, functionCallService);
@@ -0,0 +1,42 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { describe, it } from '@effect/vitest';
6
+ import { Context, Effect, Layer, Option } from 'effect';
7
+
8
+ import { ServiceRegistry } from './service-registry';
9
+
10
+ class MyTag extends Context.Tag('MyTag')<MyTag, { value: string }>() {}
11
+
12
+ const mockRegistry = Layer.succeed(ServiceRegistry, {
13
+ resolve: (tag) => ((tag as any) === MyTag ? Option.some({ value: 'test' } as any) : Option.none()),
14
+ });
15
+
16
+ describe('ServiceRegistry', () => {
17
+ it.effect(
18
+ 'provide',
19
+ Effect.fn(function* ({ expect }) {
20
+ const body = Effect.gen(function* () {
21
+ const { value } = yield* MyTag;
22
+ expect(value).toEqual('test');
23
+ });
24
+
25
+ const eff = body.pipe(ServiceRegistry.provide(MyTag));
26
+ yield* eff;
27
+ }, Effect.provide(mockRegistry)),
28
+ );
29
+
30
+ it.effect(
31
+ 'provide or die',
32
+ Effect.fn(function* ({ expect }) {
33
+ const body = Effect.gen(function* () {
34
+ const { value } = yield* MyTag;
35
+ expect(value).toEqual('test');
36
+ });
37
+
38
+ const eff = body.pipe(ServiceRegistry.provideOrDie(MyTag));
39
+ yield* eff;
40
+ }, Effect.provide(mockRegistry)),
41
+ );
42
+ });
@@ -0,0 +1,59 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { Context, Effect, type Option, flow } from 'effect';
6
+
7
+ import { ServiceNotAvailableError } from '../errors';
8
+
9
+ export namespace ServiceRegistry {
10
+ export interface Service {
11
+ resolve: <T extends Context.Tag<any, any>>(tag: T) => Option.Option<Context.Tag.Service<T>>;
12
+ }
13
+ }
14
+
15
+ export class ServiceRegistry extends Context.Tag('@dxos/functions/ServiceRegistry')<
16
+ ServiceRegistry,
17
+ ServiceRegistry.Service
18
+ >() {
19
+ /**
20
+ * Resolves the service from the registry.
21
+ * @param tag Service tag to resolve.
22
+ * @throws {@link ServiceNotAvailableError} if the service is not found.
23
+ * @returns Effect that resolve to the service.
24
+ */
25
+ static resolve: <T extends Context.Tag<any, any>>(
26
+ tag: T,
27
+ ) => Effect.Effect<T, ServiceNotAvailableError, ServiceRegistry> = (tag) =>
28
+ ServiceRegistry.pipe(
29
+ Effect.flatMap((_) => _.resolve(tag)),
30
+ Effect.mapError(() => new ServiceNotAvailableError(tag.key)),
31
+ );
32
+
33
+ static provide: {
34
+ <Tags extends [Context.Tag<any, any>, ...Context.Tag<any, any>[]]>(
35
+ ...tags: Tags
36
+ ): <A, E, R>(
37
+ effect: Effect.Effect<A, E, R>,
38
+ ) => Effect.Effect<
39
+ A,
40
+ E | ServiceNotAvailableError,
41
+ Exclude<R, { [K in keyof Tags]: Context.Tag.Identifier<Tags[K]> }[number]> | ServiceRegistry
42
+ >;
43
+ } = (...tags) => (flow as any)(...tags.map((tag) => Effect.provideServiceEffect(tag, ServiceRegistry.resolve(tag))));
44
+
45
+ static provideOrDie: {
46
+ <Tags extends [Context.Tag<any, any>, ...Context.Tag<any, any>[]]>(
47
+ ...tags: Tags
48
+ ): <A, E, R>(
49
+ effect: Effect.Effect<A, E, R>,
50
+ ) => Effect.Effect<
51
+ A,
52
+ E,
53
+ Exclude<R, { [K in keyof Tags]: Context.Tag.Identifier<Tags[K]> }[number]> | ServiceRegistry
54
+ >;
55
+ } = (...tags) =>
56
+ (flow as any)(
57
+ ...tags.map((tag) => Effect.provideServiceEffect(tag, ServiceRegistry.resolve(tag).pipe(Effect.orDie))),
58
+ );
59
+ }
@@ -2,11 +2,17 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context } from 'effect';
5
+ import { Context, Effect, Layer } from 'effect';
6
6
 
7
+ import { AgentStatus } from '@dxos/ai';
8
+ import { Obj } from '@dxos/echo';
7
9
  import type { AnyEchoObject } from '@dxos/echo-schema';
8
10
 
9
- export class TracingService extends Context.Tag('TracingService')<
11
+ /**
12
+ * Provides a way for compute primitives (functions, workflows, tools)
13
+ * to emit an execution trace as a series of structured ECHO objects.
14
+ */
15
+ export class TracingService extends Context.Tag('@dxos/functions/TracingService')<
10
16
  TracingService,
11
17
  {
12
18
  /**
@@ -18,10 +24,21 @@ export class TracingService extends Context.Tag('TracingService')<
18
24
  >() {
19
25
  static noop: Context.Tag.Service<TracingService> = { write: () => {} };
20
26
 
27
+ static layerNoop = Layer.succeed(TracingService, TracingService.noop);
28
+
21
29
  static console: Context.Tag.Service<TracingService> = {
22
30
  write: (event) => {
23
31
  // eslint-disable-next-line no-console
24
32
  console.log(event);
25
33
  },
26
34
  };
35
+
36
+ /**
37
+ * Emit the current human-readable execution status.
38
+ */
39
+ static emitStatus: (data: Obj.MakeProps<typeof AgentStatus>) => Effect.Effect<void, never, TracingService> =
40
+ Effect.fnUntraced(function* (data) {
41
+ const tracing = yield* TracingService;
42
+ tracing.write(Obj.make(AgentStatus, data));
43
+ });
27
44
  }
@@ -3,3 +3,4 @@
3
3
  //
4
4
 
5
5
  export * from './services';
6
+ export * from './layer';