@dxos/functions 0.8.4-main.67995b8 → 0.8.4-main.a4bbb77

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 (139) hide show
  1. package/dist/lib/browser/bundler/index.mjs +56 -38
  2. package/dist/lib/browser/bundler/index.mjs.map +3 -3
  3. package/dist/lib/browser/chunk-C2Z7LCJ2.mjs +649 -0
  4. package/dist/lib/browser/chunk-C2Z7LCJ2.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
  6. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
  7. package/dist/lib/browser/edge/index.mjs +22 -8
  8. package/dist/lib/browser/edge/index.mjs.map +3 -3
  9. package/dist/lib/browser/index.mjs +992 -127
  10. package/dist/lib/browser/index.mjs.map +4 -4
  11. package/dist/lib/browser/meta.json +1 -1
  12. package/dist/lib/browser/testing/index.mjs +76 -6
  13. package/dist/lib/browser/testing/index.mjs.map +3 -3
  14. package/dist/lib/node-esm/bundler/index.mjs +55 -38
  15. package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
  16. package/dist/lib/node-esm/chunk-AH3AZM2U.mjs +651 -0
  17. package/dist/lib/node-esm/chunk-AH3AZM2U.mjs.map +7 -0
  18. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
  19. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
  20. package/dist/lib/node-esm/edge/index.mjs +21 -8
  21. package/dist/lib/node-esm/edge/index.mjs.map +3 -3
  22. package/dist/lib/node-esm/index.mjs +992 -127
  23. package/dist/lib/node-esm/index.mjs.map +4 -4
  24. package/dist/lib/node-esm/meta.json +1 -1
  25. package/dist/lib/node-esm/testing/index.mjs +76 -6
  26. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  27. package/dist/types/src/bundler/bundler.d.ts +11 -12
  28. package/dist/types/src/bundler/bundler.d.ts.map +1 -1
  29. package/dist/types/src/edge/functions.d.ts +3 -2
  30. package/dist/types/src/edge/functions.d.ts.map +1 -1
  31. package/dist/types/src/errors.d.ts +89 -20
  32. package/dist/types/src/errors.d.ts.map +1 -1
  33. package/dist/types/src/examples/fib.d.ts +7 -0
  34. package/dist/types/src/examples/fib.d.ts.map +1 -0
  35. package/dist/types/src/examples/index.d.ts +4 -0
  36. package/dist/types/src/examples/index.d.ts.map +1 -0
  37. package/dist/types/src/examples/reply.d.ts +3 -0
  38. package/dist/types/src/examples/reply.d.ts.map +1 -0
  39. package/dist/types/src/examples/sleep.d.ts +5 -0
  40. package/dist/types/src/examples/sleep.d.ts.map +1 -0
  41. package/dist/types/src/executor/executor.d.ts +7 -1
  42. package/dist/types/src/executor/executor.d.ts.map +1 -1
  43. package/dist/types/src/handler.d.ts +52 -8
  44. package/dist/types/src/handler.d.ts.map +1 -1
  45. package/dist/types/src/index.d.ts +3 -1
  46. package/dist/types/src/index.d.ts.map +1 -1
  47. package/dist/types/src/schema.d.ts +6 -1
  48. package/dist/types/src/schema.d.ts.map +1 -1
  49. package/dist/types/src/services/credentials.d.ts +15 -3
  50. package/dist/types/src/services/credentials.d.ts.map +1 -1
  51. package/dist/types/src/services/database.d.ts +39 -6
  52. package/dist/types/src/services/database.d.ts.map +1 -1
  53. package/dist/types/src/services/event-logger.d.ts +1 -1
  54. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  55. package/dist/types/src/services/function-invocation-service.d.ts +26 -0
  56. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
  57. package/dist/types/src/services/function-invocation-service.test.d.ts +2 -0
  58. package/dist/types/src/services/function-invocation-service.test.d.ts.map +1 -0
  59. package/dist/types/src/services/index.d.ts +4 -3
  60. package/dist/types/src/services/index.d.ts.map +1 -1
  61. package/dist/types/src/services/local-function-execution.d.ts +23 -2
  62. package/dist/types/src/services/local-function-execution.d.ts.map +1 -1
  63. package/dist/types/src/services/queues.d.ts +19 -5
  64. package/dist/types/src/services/queues.d.ts.map +1 -1
  65. package/dist/types/src/services/remote-function-execution-service.d.ts +9 -4
  66. package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -1
  67. package/dist/types/src/services/service-container.d.ts +1 -1
  68. package/dist/types/src/services/service-container.d.ts.map +1 -1
  69. package/dist/types/src/services/service-registry.d.ts.map +1 -1
  70. package/dist/types/src/services/tracing.d.ts +34 -3
  71. package/dist/types/src/services/tracing.d.ts.map +1 -1
  72. package/dist/types/src/testing/layer.d.ts +9 -2
  73. package/dist/types/src/testing/layer.d.ts.map +1 -1
  74. package/dist/types/src/testing/logger.d.ts.map +1 -1
  75. package/dist/types/src/testing/persist-database.test.d.ts +2 -0
  76. package/dist/types/src/testing/persist-database.test.d.ts.map +1 -0
  77. package/dist/types/src/testing/services.d.ts +1 -1
  78. package/dist/types/src/testing/services.d.ts.map +1 -1
  79. package/dist/types/src/trace.d.ts +20 -22
  80. package/dist/types/src/trace.d.ts.map +1 -1
  81. package/dist/types/src/triggers/index.d.ts +4 -0
  82. package/dist/types/src/triggers/index.d.ts.map +1 -0
  83. package/dist/types/src/triggers/input-builder.d.ts +3 -0
  84. package/dist/types/src/triggers/input-builder.d.ts.map +1 -0
  85. package/dist/types/src/triggers/invocation-tracer.d.ts +35 -0
  86. package/dist/types/src/triggers/invocation-tracer.d.ts.map +1 -0
  87. package/dist/types/src/triggers/trigger-dispatcher.d.ts +74 -0
  88. package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +1 -0
  89. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +2 -0
  90. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +1 -0
  91. package/dist/types/src/triggers/trigger-state-store.d.ts +27 -0
  92. package/dist/types/src/triggers/trigger-state-store.d.ts.map +1 -0
  93. package/dist/types/src/types.d.ts +60 -250
  94. package/dist/types/src/types.d.ts.map +1 -1
  95. package/dist/types/src/url.d.ts +10 -6
  96. package/dist/types/src/url.d.ts.map +1 -1
  97. package/dist/types/tsconfig.tsbuildinfo +1 -1
  98. package/package.json +43 -43
  99. package/src/bundler/bundler.test.ts +8 -9
  100. package/src/bundler/bundler.ts +32 -33
  101. package/src/edge/functions.ts +8 -5
  102. package/src/errors.ts +13 -5
  103. package/src/examples/fib.ts +31 -0
  104. package/src/examples/index.ts +7 -0
  105. package/src/examples/reply.ts +19 -0
  106. package/src/examples/sleep.ts +23 -0
  107. package/src/executor/executor.ts +12 -9
  108. package/src/handler.ts +120 -18
  109. package/src/index.ts +3 -3
  110. package/src/schema.ts +11 -0
  111. package/src/services/credentials.ts +80 -5
  112. package/src/services/database.ts +115 -18
  113. package/src/services/event-logger.ts +2 -2
  114. package/src/services/function-invocation-service.test.ts +79 -0
  115. package/src/services/function-invocation-service.ts +82 -0
  116. package/src/services/index.ts +4 -3
  117. package/src/services/local-function-execution.ts +97 -17
  118. package/src/services/queues.ts +34 -10
  119. package/src/services/remote-function-execution-service.ts +38 -43
  120. package/src/services/service-container.ts +4 -3
  121. package/src/services/service-registry.ts +1 -1
  122. package/src/services/tracing.ts +106 -9
  123. package/src/testing/layer.ts +84 -3
  124. package/src/testing/logger.ts +1 -1
  125. package/src/testing/persist-database.test.ts +87 -0
  126. package/src/testing/services.ts +3 -2
  127. package/src/trace.ts +17 -19
  128. package/src/triggers/index.ts +7 -0
  129. package/src/triggers/input-builder.ts +35 -0
  130. package/src/triggers/invocation-tracer.ts +99 -0
  131. package/src/triggers/trigger-dispatcher.test.ts +651 -0
  132. package/src/triggers/trigger-dispatcher.ts +522 -0
  133. package/src/triggers/trigger-state-store.ts +60 -0
  134. package/src/types.ts +39 -36
  135. package/src/url.ts +13 -10
  136. package/dist/lib/browser/chunk-6PTFLPCO.mjs +0 -462
  137. package/dist/lib/browser/chunk-6PTFLPCO.mjs.map +0 -7
  138. package/dist/lib/node-esm/chunk-NYJ2TSXO.mjs +0 -464
  139. package/dist/lib/node-esm/chunk-NYJ2TSXO.mjs.map +0 -7
@@ -4,45 +4,99 @@
4
4
 
5
5
  import { Context, Effect, Layer, Schema } from 'effect';
6
6
 
7
+ import { AiService } from '@dxos/ai';
7
8
  import { todo } from '@dxos/debug';
9
+ import { log } from '@dxos/log';
8
10
 
11
+ import { FunctionError, FunctionNotFoundError } from '../errors';
9
12
  import type { FunctionContext, FunctionDefinition } from '../handler';
13
+
14
+ import { CredentialsService } from './credentials';
15
+ import { DatabaseService } from './database';
16
+ import { type ComputeEventLogger } from './event-logger';
17
+ import { QueueService } from './queues';
18
+ import { RemoteFunctionExecutionService } from './remote-function-execution-service';
10
19
  import type { Services } from './service-container';
11
- import { FunctionError } from '../errors';
20
+ import { type TracingService } from './tracing';
21
+
22
+ /**
23
+ * Services that are provided at the function call site.
24
+ */
25
+ export type InvocationServices = TracingService | ComputeEventLogger;
12
26
 
13
27
  export class LocalFunctionExecutionService extends Context.Tag('@dxos/functions/LocalFunctionExecutionService')<
14
28
  LocalFunctionExecutionService,
15
29
  {
16
30
  // TODO(dmaretskyi): This should take function id instead of the definition object.
17
31
  // 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>;
32
+ invokeFunction<I, O>(functionDef: FunctionDefinition<I, O>, input: I): Effect.Effect<O, never, InvocationServices>;
19
33
  }
20
34
  >() {
21
- static layer = Layer.succeed(LocalFunctionExecutionService, {
22
- invokeFunction: (fnDef, input) => invokeFunction(fnDef, input),
23
- });
35
+ static layerLive = Layer.effect(
36
+ LocalFunctionExecutionService,
37
+ Effect.gen(function* () {
38
+ // TODO(dmaretskyi): Use `yield* Effect.context()`;
39
+ const resolver = yield* FunctionImplementationResolver;
40
+ const ai = yield* AiService.AiService;
41
+ const credentials = yield* CredentialsService;
42
+ const database = yield* DatabaseService;
43
+ const queues = yield* QueueService;
44
+ // TODO(mykola): Delete, should not be required for local execution.
45
+ const functionCallService = yield* RemoteFunctionExecutionService;
46
+ return {
47
+ // TODO(dmaretskyi): Better error types.
48
+ invokeFunction: <I, O>(
49
+ functionDef: FunctionDefinition<I, O>,
50
+ input: I,
51
+ ): Effect.Effect<O, never, InvocationServices> =>
52
+ Effect.gen(function* () {
53
+ const resolved = yield* resolver.resolveFunctionImplementation(functionDef).pipe(Effect.orDie);
54
+ const output = yield* invokeFunction(resolved, input);
55
+ return output as O;
56
+ }).pipe(
57
+ Effect.provideService(AiService.AiService, ai),
58
+ Effect.provideService(CredentialsService, credentials),
59
+ Effect.provideService(DatabaseService, database),
60
+ Effect.provideService(QueueService, queues),
61
+ Effect.provideService(RemoteFunctionExecutionService, functionCallService),
62
+ ),
63
+ };
64
+ }),
65
+ );
66
+
67
+ static invokeFunction: <F extends FunctionDefinition.Any>(
68
+ functionDef: F,
69
+ input: FunctionDefinition.Input<F>,
70
+ ) => Effect.Effect<FunctionDefinition.Output<F>, never, Services | LocalFunctionExecutionService> =
71
+ Effect.serviceFunctionEffect(LocalFunctionExecutionService, (_) => _.invokeFunction as any);
24
72
  }
25
73
 
26
- const invokeFunction = (fnDef: FunctionDefinition<any, any>, input: any): Effect.Effect<unknown, never, Services> =>
74
+ const invokeFunction = (
75
+ functionDef: FunctionDefinition<any, any>,
76
+ input: any,
77
+ ): Effect.Effect<unknown, never, Services> =>
27
78
  Effect.gen(function* () {
28
79
  // Assert input matches schema
29
- const assertInput = fnDef.inputSchema.pipe(Schema.asserts);
30
- (assertInput as any)(input);
80
+ try {
81
+ const assertInput = functionDef.inputSchema.pipe(Schema.asserts);
82
+ (assertInput as any)(input);
83
+ } catch (e) {
84
+ throw new FunctionError({ message: 'Invalid function input', context: { name: functionDef.name }, cause: e });
85
+ }
31
86
 
32
87
  const context: FunctionContext = {
88
+ space: undefined,
33
89
  getService: () => todo(),
34
90
  getSpace: async (_spaceId: any) => {
35
91
  throw new Error('Not available. Use the database service instead.');
36
92
  },
37
- space: undefined,
38
- get ai(): never {
39
- throw new Error('Not available. Use the ai service instead.');
40
- },
41
93
  };
42
94
 
95
+ log.info('Invoking function', { name: functionDef.name, input });
96
+
43
97
  // TODO(dmaretskyi): This should be delegated to a function invoker service.
44
98
  const data = yield* Effect.gen(function* () {
45
- const result = fnDef.handler({ context, data: input });
99
+ const result = functionDef.handler({ context, data: input });
46
100
  if (Effect.isEffect(result)) {
47
101
  return yield* (result as Effect.Effect<unknown, unknown, Services>).pipe(Effect.orDie);
48
102
  } else if (
@@ -58,13 +112,39 @@ const invokeFunction = (fnDef: FunctionDefinition<any, any>, input: any): Effect
58
112
  }).pipe(
59
113
  Effect.orDie,
60
114
  Effect.catchAllDefect((defect) =>
61
- Effect.die(new FunctionError('Error running function', { context: { name: fnDef.name }, cause: defect })),
115
+ Effect.die(new FunctionError({ context: { name: functionDef.name }, cause: defect })),
62
116
  ),
63
117
  );
64
118
 
119
+ log.info('Function completed', { name: functionDef.name, input, data });
120
+
65
121
  // Assert output matches schema
66
- const assertOutput = fnDef.outputSchema?.pipe(Schema.asserts);
67
- (assertOutput as any)(data);
122
+ try {
123
+ const assertOutput = functionDef.outputSchema?.pipe(Schema.asserts);
124
+ (assertOutput as any)(data);
125
+ } catch (e) {
126
+ throw new FunctionError({ message: 'Invalid function output', context: { name: functionDef.name }, cause: e });
127
+ }
68
128
 
69
129
  return data;
70
- }).pipe(Effect.withSpan('invokeFunction', { attributes: { name: fnDef.name } }));
130
+ }).pipe(Effect.withSpan('invokeFunction', { attributes: { name: functionDef.name } }));
131
+
132
+ export class FunctionImplementationResolver extends Context.Tag('@dxos/functions/FunctionImplementationResolver')<
133
+ FunctionImplementationResolver,
134
+ {
135
+ resolveFunctionImplementation<I, O>(
136
+ functionDef: FunctionDefinition<I, O>,
137
+ ): Effect.Effect<FunctionDefinition<I, O>, FunctionNotFoundError>;
138
+ }
139
+ >() {
140
+ static layerTest = ({ functions }: { functions: FunctionDefinition<any, any>[] }) =>
141
+ Layer.succeed(FunctionImplementationResolver, {
142
+ resolveFunctionImplementation: <I, O>(functionDef: FunctionDefinition<I, O>) => {
143
+ const resolved = functions.find((f) => f.key === functionDef.key);
144
+ if (!resolved) {
145
+ return Effect.fail(new FunctionNotFoundError(functionDef.name));
146
+ }
147
+ return Effect.succeed(resolved);
148
+ },
149
+ });
150
+ }
@@ -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,50 @@ 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)));
65
+
66
+ static append = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(
67
+ queue: Queue<T>,
68
+ objects: T[],
69
+ ): Effect.Effect<void> => Effect.promise(() => queue.append(objects));
48
70
  }
49
71
 
50
72
  /**
@@ -53,6 +75,8 @@ export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
53
75
  export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQueueService')<
54
76
  ContextQueueService,
55
77
  {
56
- readonly contextQueue: Queue;
78
+ readonly queue: Queue;
57
79
  }
58
- >() {}
80
+ >() {
81
+ static layer = (queue: Queue) => Layer.succeed(ContextQueueService, { queue });
82
+ }
@@ -2,65 +2,60 @@
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
7
  import type { SpaceId } from '@dxos/keys';
8
8
 
9
+ import { FunctionError } from '../errors';
10
+ import { getInvocationUrl } from '../url';
11
+
9
12
  /**
10
13
  * Allows calling into other functions.
11
14
  */
12
15
  export class RemoteFunctionExecutionService extends Context.Tag('@dxos/functions/RemoteFunctionExecutionService')<
13
16
  RemoteFunctionExecutionService,
14
17
  {
15
- callFunction(deployedFunctionId: string, input: any, spaceId?: SpaceId): Promise<any>;
18
+ callFunction<I, O>(deployedFunctionId: string, input: I): Effect.Effect<O>;
16
19
  }
17
20
  >() {
18
- static fromClient(baseUrl: string, spaceId: SpaceId): Context.Tag.Service<RemoteFunctionExecutionService> {
19
- return {
20
- callFunction: async (deployedFunctionId: string, input: any) => {
21
- const url = getInvocationUrl(deployedFunctionId, baseUrl, { spaceId });
22
- const result = await fetch(url, {
23
- method: 'POST',
24
- headers: { 'Content-Type': 'application/json' },
25
- body: JSON.stringify(input),
26
- });
27
- if (result.status >= 300 || result.status < 200) {
28
- throw new Error('Failed to invoke function', { cause: new Error(`HTTP error: ${await result.text()}`) });
29
- }
30
- return await result.json();
31
- },
32
- };
21
+ /**
22
+ * @param baseUrl URL of the EDGE server.
23
+ * @param spaceId - The space ID to invoke the function in. If not provided, the function will be without space context.
24
+ * @returns
25
+ */
26
+ static fromClient(baseUrl: string, spaceId?: SpaceId): Layer.Layer<RemoteFunctionExecutionService> {
27
+ return Layer.succeed(RemoteFunctionExecutionService, {
28
+ callFunction: <I, O>(deployedFunctionId: string, input: I): Effect.Effect<O> =>
29
+ Effect.gen(function* () {
30
+ const url = getInvocationUrl(deployedFunctionId, baseUrl, { spaceId });
31
+ const result = yield* Effect.promise(() =>
32
+ fetch(url, {
33
+ method: 'POST',
34
+ headers: { 'Content-Type': 'application/json' },
35
+ body: JSON.stringify(input),
36
+ }),
37
+ );
38
+ if (result.status >= 300 || result.status < 200) {
39
+ const text = yield* Effect.promise(() => result.text());
40
+ return yield* Effect.die(
41
+ new FunctionError({
42
+ message: 'Failed to invoke function',
43
+ cause: new Error(`HTTP error: ${text}`),
44
+ }),
45
+ );
46
+ }
47
+ const data = (yield* Effect.promise(() => result.json())) as O;
48
+ return data;
49
+ }),
50
+ });
33
51
  }
34
52
 
35
53
  static mock = (): Context.Tag.Service<RemoteFunctionExecutionService> => {
36
54
  return {
37
- callFunction: async (deployedFunctionId: string, input: any) => {
38
- return input;
39
- },
55
+ callFunction: <I, O>(deployedFunctionId: string, input: I): Effect.Effect<O> =>
56
+ Effect.succeed(input as unknown as O),
40
57
  };
41
58
  };
42
59
 
43
- static mockLayer = Layer.succeed(RemoteFunctionExecutionService, RemoteFunctionExecutionService.mock());
60
+ static layerMock = Layer.succeed(RemoteFunctionExecutionService, RemoteFunctionExecutionService.mock());
44
61
  }
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,30 +18,124 @@ 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
 
29
37
  static console: Context.Tag.Service<TracingService> = {
30
38
  write: (event) => {
31
- // eslint-disable-next-line no-console
32
39
  console.log(event);
33
40
  },
41
+ getTraceContext: () => ({}),
34
42
  };
35
43
 
44
+ static layerConsole = Layer.succeed(TracingService, TracingService.console);
45
+
46
+ static layerLogInfo = () =>
47
+ Layer.succeed(TracingService, {
48
+ write: (event) => {
49
+ if (Obj.instanceOf(AgentStatus, event)) {
50
+ log.info('status', { message: event.message });
51
+ }
52
+ },
53
+ getTraceContext: () => ({}),
54
+ });
55
+
56
+ /**
57
+ * Creates a TracingService layer that emits events to the parent tracing service.
58
+ */
59
+ static layerSubframe = (mapContext: (currentContext: TracingService.TraceContext) => TracingService.TraceContext) =>
60
+ Layer.effect(
61
+ TracingService,
62
+ Effect.gen(function* () {
63
+ const tracing = yield* TracingService;
64
+ const context = mapContext(tracing.getTraceContext());
65
+ return {
66
+ write: (event) => tracing.write(event),
67
+ getTraceContext: () => context,
68
+ };
69
+ }),
70
+ );
71
+
72
+ static layerQueue = (queue: Queue) =>
73
+ Layer.effect(
74
+ TracingService,
75
+ Effect.gen(function* () {
76
+ // TODO(dmaretskyi): Batching.
77
+ return {
78
+ write: (event) => queue.append([event]),
79
+ getTraceContext: () => ({
80
+ debugInfo: {
81
+ queue: queue.dxn.toString(),
82
+ },
83
+ }),
84
+ };
85
+ }),
86
+ );
87
+
36
88
  /**
37
89
  * Emit the current human-readable execution status.
38
90
  */
39
- static emitStatus: (data: Obj.MakeProps<typeof AgentStatus>) => Effect.Effect<unknown, never, void> =
40
- Effect.fnUntraced(function* (data) {
41
- const tracing = yield* TracingService;
42
- tracing.write(Obj.make(AgentStatus, data));
43
- });
91
+ static emitStatus: (
92
+ data: Omit<Obj.MakeProps<typeof AgentStatus>, 'created'>,
93
+ ) => Effect.Effect<void, never, TracingService> = Effect.fnUntraced(function* (data) {
94
+ const tracing = yield* TracingService;
95
+ tracing.write(
96
+ Obj.make(AgentStatus, {
97
+ parentMessage: tracing.getTraceContext().parentMessage,
98
+ toolCallId: tracing.getTraceContext().toolCallId,
99
+ created: new Date().toISOString(),
100
+ ...data,
101
+ }),
102
+ );
103
+ });
104
+
105
+ static emitConverationMessage: (
106
+ data: Obj.MakeProps<typeof DataType.Message>,
107
+ ) => Effect.Effect<void, never, TracingService> = Effect.fnUntraced(function* (data) {
108
+ const tracing = yield* TracingService;
109
+ tracing.write(
110
+ Obj.make(DataType.Message, {
111
+ parentMessage: tracing.getTraceContext().parentMessage,
112
+ ...data,
113
+ properties: {
114
+ [MESSAGE_PROPERTY_TOOL_CALL_ID]: tracing.getTraceContext().toolCallId,
115
+ ...data.properties,
116
+ },
117
+ }),
118
+ );
119
+ });
44
120
  }
121
+
122
+ export namespace TracingService {
123
+ export interface TraceContext {
124
+ /**
125
+ * If this thread sprung from a tool call, this is the ID of the message containing the tool call.
126
+ */
127
+ parentMessage?: ObjectId;
128
+
129
+ /**
130
+ * If the current thread is a byproduct of a tool call, this is the ID of the tool call.
131
+ */
132
+ toolCallId?: string;
133
+
134
+ debugInfo?: unknown;
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Goes into {@link DataType.Message['properties']}
140
+ */
141
+ export const MESSAGE_PROPERTY_TOOL_CALL_ID = 'toolCallId' as const;
@@ -2,27 +2,108 @@
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
+ spaceKey?: PublicKey;
29
+ storagePath?: string;
30
+ onInit?: () => Effect.Effect<void, never, DatabaseService | QueueService>;
19
31
  };
20
32
 
21
- export const TestDatabaseLayer = ({ indexing, types }: TestDatabaseOptions = {}) =>
33
+ export const TestDatabaseLayer = ({ indexing, types, spaceKey, storagePath, onInit }: TestDatabaseOptions = {}) =>
22
34
  Layer.scopedContext(
23
35
  Effect.gen(function* () {
24
36
  const builder = yield* testBuilder;
25
- const { db, queues } = yield* Effect.promise(() => builder.createDatabase({ indexing, types }));
37
+
38
+ let kv: LevelDB | undefined;
39
+ if (storagePath) {
40
+ kv = createTestLevel(storagePath);
41
+ yield* Effect.promise(() => kv!.open());
42
+ // const keyCount = yield* Effect.promise(async () => (await kv!.iterator({ values: false }).all()).length);
43
+ // log.info('opened test db', { storagePath, keyCount });
44
+ }
45
+ const peer = yield* Effect.promise(() => builder.createPeer({ indexing, types, kv }));
46
+
47
+ let db: EchoDatabaseImpl | undefined;
48
+ let queues: QueueFactory | undefined;
49
+
50
+ if (storagePath) {
51
+ const testMetadata = yield* Effect.promise(async () => {
52
+ try {
53
+ return await kv!.get('test-metadata', { valueEncoding: 'json' });
54
+ } catch (e) {
55
+ if ((e as any).code === 'LEVEL_NOT_FOUND') {
56
+ return undefined;
57
+ }
58
+ throw e;
59
+ }
60
+ });
61
+ log('starting persistant test db', { storagePath, testMetadata });
62
+ if (!testMetadata) {
63
+ const key = spaceKey ?? PublicKey.random();
64
+ db = yield* Effect.promise(() => peer.createDatabase(key));
65
+ queues = peer.client.constructQueueFactory(db.spaceId);
66
+
67
+ yield* Effect.promise(() =>
68
+ kv!.put('test-metadata', { key: key.toHex(), rootUrl: db!.rootUrl }, { valueEncoding: 'json' }),
69
+ );
70
+
71
+ if (onInit) {
72
+ yield* onInit().pipe(
73
+ Effect.provideService(DatabaseService, DatabaseService.make(db)),
74
+ Effect.provideService(QueueService, QueueService.make(queues, undefined)),
75
+ );
76
+ }
77
+ } else {
78
+ const key = PublicKey.from((testMetadata as any).key);
79
+ const rootUrl = (testMetadata as any).rootUrl;
80
+ db = yield* Effect.promise(() => peer.openDatabase(key, rootUrl));
81
+ queues = peer.client.constructQueueFactory(db.spaceId);
82
+ }
83
+ } else {
84
+ db = yield* Effect.promise(() => peer.createDatabase(spaceKey));
85
+ queues = peer.client.constructQueueFactory(db.spaceId);
86
+ if (onInit) {
87
+ yield* onInit().pipe(
88
+ Effect.provideService(DatabaseService, DatabaseService.make(db)),
89
+ Effect.provideService(QueueService, QueueService.make(queues, undefined)),
90
+ );
91
+ }
92
+ }
93
+
94
+ yield* Effect.addFinalizer(() =>
95
+ Effect.promise(async () => {
96
+ if (kv) {
97
+ // {
98
+ // const keyCount = (await kv.iterator({ values: false }).all()).length;
99
+ // log.info('closing persistant test db', { storagePath, keyCount });
100
+ // }
101
+
102
+ await kv.close();
103
+ }
104
+ }),
105
+ );
106
+
26
107
  return Context.mergeAll(
27
108
  Context.make(DatabaseService, DatabaseService.make(db)),
28
109
  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