@dxos/functions 0.8.4-main.5acf9ea → 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 (100) 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 +141 -77
  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 +68 -5
  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 +141 -77
  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 +68 -5
  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 +15 -3
  42. package/dist/types/src/services/credentials.d.ts.map +1 -1
  43. package/dist/types/src/services/database.d.ts +74 -6
  44. package/dist/types/src/services/database.d.ts.map +1 -1
  45. package/dist/types/src/services/event-logger.d.ts +1 -1
  46. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  47. package/dist/types/src/services/local-function-execution.d.ts +2 -1
  48. package/dist/types/src/services/local-function-execution.d.ts.map +1 -1
  49. package/dist/types/src/services/queues.d.ts +18 -5
  50. package/dist/types/src/services/queues.d.ts.map +1 -1
  51. package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -1
  52. package/dist/types/src/services/service-container.d.ts +1 -1
  53. package/dist/types/src/services/service-container.d.ts.map +1 -1
  54. package/dist/types/src/services/service-registry.d.ts.map +1 -1
  55. package/dist/types/src/services/tracing.d.ts +33 -3
  56. package/dist/types/src/services/tracing.d.ts.map +1 -1
  57. package/dist/types/src/testing/layer.d.ts +6 -2
  58. package/dist/types/src/testing/layer.d.ts.map +1 -1
  59. package/dist/types/src/testing/logger.d.ts.map +1 -1
  60. package/dist/types/src/testing/persist-database.test.d.ts +2 -0
  61. package/dist/types/src/testing/persist-database.test.d.ts.map +1 -0
  62. package/dist/types/src/testing/services.d.ts +1 -1
  63. package/dist/types/src/testing/services.d.ts.map +1 -1
  64. package/dist/types/src/trace.d.ts +34 -8
  65. package/dist/types/src/trace.d.ts.map +1 -1
  66. package/dist/types/src/types.d.ts +141 -224
  67. package/dist/types/src/types.d.ts.map +1 -1
  68. package/dist/types/src/url.d.ts +10 -6
  69. package/dist/types/src/url.d.ts.map +1 -1
  70. package/dist/types/tsconfig.tsbuildinfo +1 -1
  71. package/package.json +40 -39
  72. package/src/bundler/bundler.test.ts +8 -9
  73. package/src/bundler/bundler.ts +32 -33
  74. package/src/edge/functions.ts +8 -5
  75. package/src/examples/fib.ts +30 -0
  76. package/src/examples/reply.ts +18 -0
  77. package/src/examples/sleep.ts +22 -0
  78. package/src/executor/executor.ts +9 -9
  79. package/src/handler.ts +12 -10
  80. package/src/schema.ts +11 -0
  81. package/src/services/credentials.ts +78 -5
  82. package/src/services/database.ts +114 -18
  83. package/src/services/event-logger.ts +2 -2
  84. package/src/services/local-function-execution.ts +20 -13
  85. package/src/services/queues.ts +29 -10
  86. package/src/services/remote-function-execution-service.ts +2 -22
  87. package/src/services/service-container.ts +4 -3
  88. package/src/services/service-registry.ts +1 -1
  89. package/src/services/tracing.ts +95 -5
  90. package/src/testing/layer.ts +69 -3
  91. package/src/testing/logger.ts +1 -1
  92. package/src/testing/persist-database.test.ts +87 -0
  93. package/src/testing/services.ts +2 -1
  94. package/src/trace.ts +5 -7
  95. package/src/types.ts +17 -25
  96. package/src/url.ts +13 -10
  97. package/dist/lib/browser/chunk-6PTFLPCO.mjs +0 -462
  98. package/dist/lib/browser/chunk-6PTFLPCO.mjs.map +0 -7
  99. package/dist/lib/node-esm/chunk-NYJ2TSXO.mjs +0 -464
  100. package/dist/lib/node-esm/chunk-NYJ2TSXO.mjs.map +0 -7
@@ -2,10 +2,15 @@
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
16
  export class DatabaseService extends Context.Tag('@dxos/functions/DatabaseService')<
@@ -28,27 +33,96 @@ export class DatabaseService extends Context.Tag('@dxos/functions/DatabaseServic
28
33
  };
29
34
  };
30
35
 
31
- static makeLayer = (db: EchoDatabase): Layer.Layer<DatabaseService> => {
36
+ static layer = (db: EchoDatabase): Layer.Layer<DatabaseService> => {
32
37
  return Layer.succeed(DatabaseService, DatabaseService.make(db));
33
38
  };
34
39
 
35
- static resolve: (dxn: DXN) => Effect.Effect<Obj.Any | Relation.Any, Error, DatabaseService> = Effect.fn(
36
- function* (dxn) {
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* () {
37
56
  const { db } = yield* DatabaseService;
38
- return yield* Effect.tryPromise({
39
- try: () =>
40
- db.graph.createRefResolver({ context: { space: db.spaceId } }).resolve(dxn) as Promise<
41
- Obj.Any | Relation.Any
42
- >,
43
- catch: (error) => error as Error,
44
- });
45
- },
46
- );
57
+ const object = yield* promiseWithCauseCapture(() =>
58
+ db.graph
59
+ .createRefResolver({
60
+ context: {
61
+ space: db.spaceId,
62
+ },
63
+ })
64
+ .resolve(dxn),
65
+ );
47
66
 
48
- static loadRef: <T>(ref: Ref.Ref<T>) => Effect.Effect<T, never, never> = Effect.fn(function* (ref) {
49
- return yield* Effect.promise(() => ref.load());
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;
50
83
  });
51
84
 
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);
94
+ });
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
+
52
126
  /**
53
127
  * Creates a `QueryResult` object that can be subscribed to.
54
128
  */
@@ -69,6 +143,28 @@ export class DatabaseService extends Context.Tag('@dxos/functions/DatabaseServic
69
143
  <F extends Filter.Any>(filter: F): Effect.Effect<OneShotQueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;
70
144
  } = (queryOrFilter: Query.Any | Filter.Any) =>
71
145
  DatabaseService.query(queryOrFilter as any).pipe(
72
- Effect.flatMap((queryResult) => Effect.promise(() => queryResult.run())),
146
+ Effect.flatMap((queryResult) => promiseWithCauseCapture(() => queryResult.run())),
73
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
+ }
74
170
  }
@@ -2,11 +2,11 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Effect, Context, Schema, Layer } from 'effect';
5
+ import { Context, Effect, Layer, Schema } from 'effect';
6
6
 
7
7
  import { Obj, Type } from '@dxos/echo';
8
8
  import { invariant } from '@dxos/invariant';
9
- import { log, LogLevel } from '@dxos/log';
9
+ import { LogLevel, log } from '@dxos/log';
10
10
 
11
11
  import { TracingService } from './tracing';
12
12
 
@@ -6,43 +6,50 @@ import { Context, Effect, Layer, Schema } from 'effect';
6
6
 
7
7
  import { todo } from '@dxos/debug';
8
8
 
9
+ import { FunctionError } from '../errors';
9
10
  import type { FunctionContext, FunctionDefinition } from '../handler';
11
+
10
12
  import type { Services } from './service-container';
11
- import { FunctionError } from '../errors';
12
13
 
13
14
  export class LocalFunctionExecutionService extends Context.Tag('@dxos/functions/LocalFunctionExecutionService')<
14
15
  LocalFunctionExecutionService,
15
16
  {
16
17
  // TODO(dmaretskyi): This should take function id instead of the definition object.
17
18
  // TODO(dmaretskyi): Services should be satisfied from environment rather then bubbled up.
18
- invokeFunction(fnDef: FunctionDefinition<any, any>, input: unknown): Effect.Effect<unknown, never, Services>;
19
+ invokeFunction(functionDef: FunctionDefinition<any, any>, input: unknown): Effect.Effect<unknown, never, Services>;
19
20
  }
20
21
  >() {
21
22
  static layer = Layer.succeed(LocalFunctionExecutionService, {
22
- invokeFunction: (fnDef, input) => invokeFunction(fnDef, input),
23
+ invokeFunction: (functionDef, input) => invokeFunction(functionDef, input),
23
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);
24
31
  }
25
32
 
26
- const invokeFunction = (fnDef: FunctionDefinition<any, any>, input: any): Effect.Effect<unknown, never, Services> =>
33
+ const invokeFunction = (
34
+ functionDef: FunctionDefinition<any, any>,
35
+ input: any,
36
+ ): Effect.Effect<unknown, never, Services> =>
27
37
  Effect.gen(function* () {
28
38
  // Assert input matches schema
29
- const assertInput = fnDef.inputSchema.pipe(Schema.asserts);
39
+ const assertInput = functionDef.inputSchema.pipe(Schema.asserts);
30
40
  (assertInput as any)(input);
31
41
 
32
42
  const context: FunctionContext = {
43
+ space: undefined,
33
44
  getService: () => todo(),
34
45
  getSpace: async (_spaceId: any) => {
35
46
  throw new Error('Not available. Use the database service instead.');
36
47
  },
37
- space: undefined,
38
- get ai(): never {
39
- throw new Error('Not available. Use the ai service instead.');
40
- },
41
48
  };
42
49
 
43
50
  // TODO(dmaretskyi): This should be delegated to a function invoker service.
44
51
  const data = yield* Effect.gen(function* () {
45
- const result = fnDef.handler({ context, data: input });
52
+ const result = functionDef.handler({ context, data: input });
46
53
  if (Effect.isEffect(result)) {
47
54
  return yield* (result as Effect.Effect<unknown, unknown, Services>).pipe(Effect.orDie);
48
55
  } else if (
@@ -58,13 +65,13 @@ const invokeFunction = (fnDef: FunctionDefinition<any, any>, input: any): Effect
58
65
  }).pipe(
59
66
  Effect.orDie,
60
67
  Effect.catchAllDefect((defect) =>
61
- Effect.die(new FunctionError('Error running function', { context: { name: fnDef.name }, cause: defect })),
68
+ Effect.die(new FunctionError('Error running function', { context: { name: functionDef.name }, cause: defect })),
62
69
  ),
63
70
  );
64
71
 
65
72
  // Assert output matches schema
66
- const assertOutput = fnDef.outputSchema?.pipe(Schema.asserts);
73
+ const assertOutput = functionDef.outputSchema?.pipe(Schema.asserts);
67
74
  (assertOutput as any)(data);
68
75
 
69
76
  return data;
70
- }).pipe(Effect.withSpan('invokeFunction', { attributes: { name: fnDef.name } }));
77
+ }).pipe(Effect.withSpan('invokeFunction', { attributes: { name: functionDef.name } }));
@@ -2,9 +2,11 @@
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.
@@ -21,30 +23,45 @@ export class QueueService extends Context.Tag('@dxos/functions/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
  };
45
47
 
46
- static makeLayer = (queues: QueueFactory, contextQueue?: Queue): Layer.Layer<QueueService> =>
47
- Layer.succeed(QueueService, QueueService.make(queues, contextQueue));
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)));
48
65
  }
49
66
 
50
67
  /**
@@ -53,6 +70,8 @@ export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
53
70
  export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQueueService')<
54
71
  ContextQueueService,
55
72
  {
56
- readonly contextQueue: Queue;
73
+ readonly queue: Queue;
57
74
  }
58
- >() {}
75
+ >() {
76
+ static layer = (queue: Queue) => Layer.succeed(ContextQueueService, { queue });
77
+ }
@@ -6,6 +6,8 @@ import { Context, Layer } from 'effect';
6
6
 
7
7
  import type { SpaceId } from '@dxos/keys';
8
8
 
9
+ import { getInvocationUrl } from '../url';
10
+
9
11
  /**
10
12
  * Allows calling into other functions.
11
13
  */
@@ -42,25 +44,3 @@ export class RemoteFunctionExecutionService extends Context.Tag('@dxos/functions
42
44
 
43
45
  static mockLayer = Layer.succeed(RemoteFunctionExecutionService, RemoteFunctionExecutionService.mock());
44
46
  }
45
-
46
- // TODO(dmaretskyi): Reconcile with `getInvocationUrl` in `@dxos/functions/edge`.
47
- const getInvocationUrl = (functionUrl: string, edgeUrl: string, options: InvocationOptions = {}) => {
48
- const baseUrl = new URL('functions/', edgeUrl);
49
-
50
- // Leading slashes cause the URL to be treated as an absolute path.
51
- const relativeUrl = functionUrl.replace(/^\//, '');
52
- const url = new URL(`./${relativeUrl}`, baseUrl.toString());
53
- options.spaceId && url.searchParams.set('spaceId', options.spaceId);
54
- options.subjectId && url.searchParams.set('subjectId', options.subjectId);
55
- url.protocol = isSecure(url.protocol) ? 'https' : 'http';
56
- return url.toString();
57
- };
58
-
59
- const isSecure = (protocol: string) => {
60
- return protocol === 'https:' || protocol === 'wss:';
61
- };
62
-
63
- type InvocationOptions = {
64
- spaceId?: SpaceId;
65
- subjectId?: string;
66
- };
@@ -20,7 +20,7 @@ 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
26
  eventLogger: ComputeEventLogger,
@@ -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(),
@@ -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
 
@@ -6,7 +6,10 @@ import { Context, Effect, Layer } from 'effect';
6
6
 
7
7
  import { AgentStatus } from '@dxos/ai';
8
8
  import { Obj } from '@dxos/echo';
9
- import type { AnyEchoObject } from '@dxos/echo-schema';
9
+ import type { Queue } from '@dxos/echo-db';
10
+ import type { ObjectId } from '@dxos/echo-schema';
11
+ import { log } from '@dxos/log';
12
+ import { DataType } from '@dxos/schema';
10
13
 
11
14
  /**
12
15
  * Provides a way for compute primitives (functions, workflows, tools)
@@ -15,14 +18,19 @@ import type { AnyEchoObject } from '@dxos/echo-schema';
15
18
  export class TracingService extends Context.Tag('@dxos/functions/TracingService')<
16
19
  TracingService,
17
20
  {
21
+ /**
22
+ * Gets the parent message ID.
23
+ */
24
+ getTraceContext: () => TracingService.TraceContext;
25
+
18
26
  /**
19
27
  * Write an event to the tracing queue.
20
28
  * @param event - The event to write. Must be an a typed object.
21
29
  */
22
- write(event: AnyEchoObject): void;
30
+ write: (event: Obj.Any) => void;
23
31
  }
24
32
  >() {
25
- static noop: Context.Tag.Service<TracingService> = { write: () => {} };
33
+ static noop: Context.Tag.Service<TracingService> = { write: () => {}, getTraceContext: () => ({}) };
26
34
 
27
35
  static layerNoop = Layer.succeed(TracingService, TracingService.noop);
28
36
 
@@ -31,14 +39,96 @@ export class TracingService extends Context.Tag('@dxos/functions/TracingService'
31
39
  // eslint-disable-next-line no-console
32
40
  console.log(event);
33
41
  },
42
+ getTraceContext: () => ({}),
34
43
  };
35
44
 
45
+ static layerConsole = Layer.succeed(TracingService, TracingService.console);
46
+
47
+ static layerLogInfo = () =>
48
+ Layer.succeed(TracingService, {
49
+ write: (event) => {
50
+ if (Obj.instanceOf(AgentStatus, event)) {
51
+ log.info('status', { message: event.message });
52
+ }
53
+ },
54
+ getTraceContext: () => ({}),
55
+ });
56
+
57
+ /**
58
+ * Creates a TracingService layer that emits events to the parent tracing service.
59
+ */
60
+ static layerSubframe = (mapContext: (currentContext: TracingService.TraceContext) => TracingService.TraceContext) =>
61
+ Layer.effect(
62
+ TracingService,
63
+ Effect.gen(function* () {
64
+ const tracing = yield* TracingService;
65
+ const context = mapContext(tracing.getTraceContext());
66
+ return {
67
+ write: (event) => tracing.write(event),
68
+ getTraceContext: () => context,
69
+ };
70
+ }),
71
+ );
72
+
73
+ static layerQueue = (queue: Queue) =>
74
+ Layer.effect(
75
+ TracingService,
76
+ Effect.gen(function* () {
77
+ // TODO(dmaretskyi): Batching.
78
+ return {
79
+ write: (event) => queue.append([event]),
80
+ getTraceContext: () => ({}),
81
+ };
82
+ }),
83
+ );
84
+
36
85
  /**
37
86
  * Emit the current human-readable execution status.
38
87
  */
39
- static emitStatus: (data: Obj.MakeProps<typeof AgentStatus>) => Effect.Effect<unknown, never, void> =
88
+ static emitStatus: (data: Obj.MakeProps<typeof AgentStatus>) => Effect.Effect<void, never, TracingService> =
40
89
  Effect.fnUntraced(function* (data) {
41
90
  const tracing = yield* TracingService;
42
- tracing.write(Obj.make(AgentStatus, data));
91
+ tracing.write(
92
+ Obj.make(AgentStatus, {
93
+ parentMessage: tracing.getTraceContext().parentMessage,
94
+ toolCallId: tracing.getTraceContext().toolCallId,
95
+ ...data,
96
+ }),
97
+ );
43
98
  });
99
+
100
+ static emitConverationMessage: (
101
+ data: Obj.MakeProps<typeof DataType.Message>,
102
+ ) => Effect.Effect<void, never, TracingService> = Effect.fnUntraced(function* (data) {
103
+ const tracing = yield* TracingService;
104
+ tracing.write(
105
+ Obj.make(DataType.Message, {
106
+ parentMessage: tracing.getTraceContext().parentMessage,
107
+ ...data,
108
+ properties: {
109
+ [MESSAGE_PROPERTY_TOOL_CALL_ID]: tracing.getTraceContext().toolCallId,
110
+ ...data.properties,
111
+ },
112
+ }),
113
+ );
114
+ });
44
115
  }
116
+
117
+ export namespace TracingService {
118
+ export interface TraceContext {
119
+ /**
120
+ * If this thread sprung from a tool call, this is the ID of the message containing the tool call.
121
+ */
122
+ parentMessage?: ObjectId;
123
+
124
+ /**
125
+ * If the current thread is a byproduct of a tool call, this is the ID of the tool call.
126
+ */
127
+ toolCallId?: string;
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Goes into {@link DataType.Message['properties']}
133
+ */
134
+ export const MESSAGE_PROPERTY_TOOL_CALL_ID = 'toolCallId' as const;
@@ -2,27 +2,93 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context, Effect, Layer } from 'effect';
6
5
  import type { Schema } from 'effect';
6
+ import { Context, Effect, Layer } from 'effect';
7
7
 
8
+ import type { EchoDatabaseImpl, QueueFactory } from '@dxos/echo-db';
8
9
  import { EchoTestBuilder } from '@dxos/echo-db/testing';
9
10
  import type { EchoHostIndexingConfig } from '@dxos/echo-pipeline';
10
11
  import { accuireReleaseResource } from '@dxos/effect';
12
+ import { PublicKey } from '@dxos/keys';
13
+ import type { LevelDB } from '@dxos/kv-store';
14
+ import { createTestLevel } from '@dxos/kv-store/testing';
15
+ import { log } from '@dxos/log';
11
16
 
12
17
  import { DatabaseService, QueueService } from '../services';
13
18
 
14
19
  const testBuilder = accuireReleaseResource(() => new EchoTestBuilder());
15
20
 
21
+ export const testStoragePath = ({ name = PublicKey.random().toHex() }: { name?: string }) => {
22
+ return `/tmp/dxos-${name}`;
23
+ };
24
+
16
25
  export type TestDatabaseOptions = {
17
26
  indexing?: Partial<EchoHostIndexingConfig>;
18
27
  types?: Schema.Schema.AnyNoContext[];
28
+ storagePath?: string;
19
29
  };
20
30
 
21
- export const TestDatabaseLayer = ({ indexing, types }: TestDatabaseOptions = {}) =>
31
+ export const TestDatabaseLayer = ({ indexing, types, storagePath }: TestDatabaseOptions = {}) =>
22
32
  Layer.scopedContext(
23
33
  Effect.gen(function* () {
24
34
  const builder = yield* testBuilder;
25
- const { db, queues } = yield* Effect.promise(() => builder.createDatabase({ indexing, types }));
35
+
36
+ let kv: LevelDB | undefined;
37
+ if (storagePath) {
38
+ kv = createTestLevel(storagePath);
39
+ yield* Effect.promise(() => kv!.open());
40
+ // const keyCount = yield* Effect.promise(async () => (await kv!.iterator({ values: false }).all()).length);
41
+ // log.info('opened test db', { storagePath, keyCount });
42
+ }
43
+ const peer = yield* Effect.promise(() => builder.createPeer({ indexing, types, kv }));
44
+
45
+ let db: EchoDatabaseImpl | undefined;
46
+ let queues: QueueFactory | undefined;
47
+
48
+ if (storagePath) {
49
+ const testMetadata = yield* Effect.promise(async () => {
50
+ try {
51
+ return await kv!.get('test-metadata', { valueEncoding: 'json' });
52
+ } catch (e) {
53
+ if ((e as any).code === 'LEVEL_NOT_FOUND') {
54
+ return undefined;
55
+ }
56
+ throw e;
57
+ }
58
+ });
59
+ log('starting persistant test db', { storagePath, testMetadata });
60
+ if (!testMetadata) {
61
+ const key = PublicKey.random();
62
+ db = yield* Effect.promise(() => peer.createDatabase(key));
63
+ queues = peer.client.constructQueueFactory(db.spaceId);
64
+
65
+ yield* Effect.promise(() =>
66
+ kv!.put('test-metadata', { key: key.toHex(), rootUrl: db!.rootUrl }, { valueEncoding: 'json' }),
67
+ );
68
+ } else {
69
+ const key = PublicKey.from((testMetadata as any).key);
70
+ const rootUrl = (testMetadata as any).rootUrl;
71
+ db = yield* Effect.promise(() => peer.openDatabase(key, rootUrl));
72
+ queues = peer.client.constructQueueFactory(db.spaceId);
73
+ }
74
+ } else {
75
+ db = yield* Effect.promise(() => peer.createDatabase());
76
+ queues = peer.client.constructQueueFactory(db.spaceId);
77
+ }
78
+
79
+ yield* Effect.addFinalizer(() =>
80
+ Effect.promise(async () => {
81
+ if (kv) {
82
+ // {
83
+ // const keyCount = (await kv.iterator({ values: false }).all()).length;
84
+ // log.info('closing persistant test db', { storagePath, keyCount });
85
+ // }
86
+
87
+ await kv.close();
88
+ }
89
+ }),
90
+ );
91
+
26
92
  return Context.mergeAll(
27
93
  Context.make(DatabaseService, DatabaseService.make(db)),
28
94
  Context.make(QueueService, QueueService.make(queues, undefined)),
@@ -2,7 +2,7 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Effect, type Context } from 'effect';
5
+ import { type Context, Effect } from 'effect';
6
6
 
7
7
  import { LogLevel } from '@dxos/log';
8
8