@dxos/functions 0.8.3 → 0.8.4-main.1da679c
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.
- package/dist/lib/browser/bundler/index.mjs +73 -44
- package/dist/lib/browser/bundler/index.mjs.map +3 -3
- package/dist/lib/browser/chunk-D2XO7XXY.mjs +611 -0
- package/dist/lib/browser/chunk-D2XO7XXY.mjs.map +7 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/browser/edge/index.mjs +24 -10
- package/dist/lib/browser/edge/index.mjs.map +3 -3
- package/dist/lib/browser/index.mjs +981 -137
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +110 -9
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node-esm/bundler/index.mjs +72 -44
- package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-Z4CJ62WS.mjs +613 -0
- package/dist/lib/node-esm/chunk-Z4CJ62WS.mjs.map +7 -0
- package/dist/lib/node-esm/edge/index.mjs +23 -10
- package/dist/lib/node-esm/edge/index.mjs.map +3 -3
- package/dist/lib/node-esm/index.mjs +981 -137
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +110 -9
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- package/dist/types/src/bundler/bundler.d.ts +12 -14
- package/dist/types/src/bundler/bundler.d.ts.map +1 -1
- package/dist/types/src/edge/functions.d.ts +4 -3
- package/dist/types/src/edge/functions.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +137 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/examples/fib.d.ts +7 -0
- package/dist/types/src/examples/fib.d.ts.map +1 -0
- package/dist/types/src/examples/index.d.ts +4 -0
- package/dist/types/src/examples/index.d.ts.map +1 -0
- package/dist/types/src/examples/reply.d.ts +3 -0
- package/dist/types/src/examples/reply.d.ts.map +1 -0
- package/dist/types/src/examples/sleep.d.ts +5 -0
- package/dist/types/src/examples/sleep.d.ts.map +1 -0
- package/dist/types/src/executor/executor.d.ts +4 -1
- package/dist/types/src/executor/executor.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +46 -16
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/schema.d.ts +8 -3
- package/dist/types/src/schema.d.ts.map +1 -1
- package/dist/types/src/services/credentials.d.ts +18 -4
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/database.d.ts +55 -3
- package/dist/types/src/services/database.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +65 -30
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/index.d.ts +2 -2
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/local-function-execution.d.ts +25 -0
- package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
- package/dist/types/src/services/queues.d.ts +33 -6
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/remote-function-execution-service.d.ts +15 -0
- package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -0
- package/dist/types/src/services/service-container.d.ts +29 -17
- package/dist/types/src/services/service-container.d.ts.map +1 -1
- package/dist/types/src/services/service-registry.d.ts +29 -0
- package/dist/types/src/services/service-registry.d.ts.map +1 -0
- package/dist/types/src/services/service-registry.test.d.ts +2 -0
- package/dist/types/src/services/service-registry.test.d.ts.map +1 -0
- package/dist/types/src/services/tracing.d.ts +46 -4
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +1 -0
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +15 -0
- package/dist/types/src/testing/layer.d.ts.map +1 -0
- package/dist/types/src/testing/logger.d.ts +3 -3
- package/dist/types/src/testing/logger.d.ts.map +1 -1
- package/dist/types/src/testing/persist-database.test.d.ts +2 -0
- package/dist/types/src/testing/persist-database.test.d.ts.map +1 -0
- package/dist/types/src/testing/services.d.ts +55 -9
- package/dist/types/src/testing/services.d.ts.map +1 -1
- package/dist/types/src/trace.d.ts +20 -22
- package/dist/types/src/trace.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +9 -9
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/triggers/index.d.ts +4 -0
- package/dist/types/src/triggers/index.d.ts.map +1 -0
- package/dist/types/src/triggers/input-builder.d.ts +3 -0
- package/dist/types/src/triggers/input-builder.d.ts.map +1 -0
- package/dist/types/src/triggers/invocation-tracer.d.ts +35 -0
- package/dist/types/src/triggers/invocation-tracer.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-dispatcher.d.ts +75 -0
- package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +2 -0
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-state-store.d.ts +27 -0
- package/dist/types/src/triggers/trigger-state-store.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +55 -245
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/url.d.ts +10 -6
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +35 -25
- package/src/bundler/bundler.test.ts +9 -10
- package/src/bundler/bundler.ts +56 -35
- package/src/edge/functions.ts +9 -6
- package/src/errors.ts +21 -0
- package/src/examples/fib.ts +30 -0
- package/src/examples/index.ts +7 -0
- package/src/examples/reply.ts +18 -0
- package/src/examples/sleep.ts +22 -0
- package/src/executor/executor.ts +22 -15
- package/src/handler.ts +117 -27
- package/src/index.ts +3 -2
- package/src/schema.ts +11 -0
- package/src/services/credentials.ts +87 -5
- package/src/services/database.ts +146 -3
- package/src/services/event-logger.ts +68 -37
- package/src/services/index.ts +2 -2
- package/src/services/local-function-execution.ts +127 -0
- package/src/services/queues.ts +56 -11
- package/src/services/remote-function-execution-service.ts +46 -0
- package/src/services/service-container.ts +47 -42
- package/src/services/service-registry.test.ts +42 -0
- package/src/services/service-registry.ts +59 -0
- package/src/services/tracing.ts +118 -5
- package/src/testing/index.ts +1 -0
- package/src/testing/layer.ts +111 -0
- package/src/testing/logger.ts +4 -4
- package/src/testing/persist-database.test.ts +87 -0
- package/src/testing/services.ts +97 -14
- package/src/trace.ts +17 -19
- package/src/translations.ts +4 -4
- package/src/triggers/index.ts +7 -0
- package/src/triggers/input-builder.ts +35 -0
- package/src/triggers/invocation-tracer.ts +99 -0
- package/src/triggers/trigger-dispatcher.test.ts +652 -0
- package/src/triggers/trigger-dispatcher.ts +516 -0
- package/src/triggers/trigger-state-store.ts +60 -0
- package/src/types.ts +39 -36
- package/src/url.ts +13 -10
- package/dist/lib/browser/chunk-WEFZUEL2.mjs +0 -300
- package/dist/lib/browser/chunk-WEFZUEL2.mjs.map +0 -7
- package/dist/lib/node/bundler/index.cjs +0 -260
- package/dist/lib/node/bundler/index.cjs.map +0 -7
- package/dist/lib/node/chunk-IJAE7FZK.cjs +0 -320
- package/dist/lib/node/chunk-IJAE7FZK.cjs.map +0 -7
- package/dist/lib/node/edge/index.cjs +0 -94
- package/dist/lib/node/edge/index.cjs.map +0 -7
- package/dist/lib/node/index.cjs +0 -522
- package/dist/lib/node/index.cjs.map +0 -7
- package/dist/lib/node/meta.json +0 -1
- package/dist/lib/node/testing/index.cjs +0 -43
- package/dist/lib/node/testing/index.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-LIYPMWNQ.mjs +0 -302
- package/dist/lib/node-esm/chunk-LIYPMWNQ.mjs.map +0 -7
- package/dist/types/src/services/ai.d.ts +0 -12
- package/dist/types/src/services/ai.d.ts.map +0 -1
- package/dist/types/src/services/function-call-service.d.ts +0 -16
- package/dist/types/src/services/function-call-service.d.ts.map +0 -1
- package/src/services/ai.ts +0 -32
- package/src/services/function-call-service.ts +0 -64
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Context, Effect, Layer, Schema } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { todo } from '@dxos/debug';
|
|
8
|
+
import { log } from '@dxos/log';
|
|
9
|
+
|
|
10
|
+
import { FunctionError, FunctionNotFoundError } from '../errors';
|
|
11
|
+
import type { FunctionContext, FunctionDefinition } from '../handler';
|
|
12
|
+
|
|
13
|
+
import type { Services } from './service-container';
|
|
14
|
+
|
|
15
|
+
export class LocalFunctionExecutionService extends Context.Tag('@dxos/functions/LocalFunctionExecutionService')<
|
|
16
|
+
LocalFunctionExecutionService,
|
|
17
|
+
{
|
|
18
|
+
// TODO(dmaretskyi): This should take function id instead of the definition object.
|
|
19
|
+
// TODO(dmaretskyi): Services should be satisfied from environment rather then bubbled up.
|
|
20
|
+
invokeFunction(functionDef: FunctionDefinition<any, any>, input: unknown): Effect.Effect<unknown, never, Services>;
|
|
21
|
+
}
|
|
22
|
+
>() {
|
|
23
|
+
/**
|
|
24
|
+
* @deprecated Use layerLive instead.
|
|
25
|
+
*/
|
|
26
|
+
static layer = Layer.succeed(LocalFunctionExecutionService, {
|
|
27
|
+
invokeFunction: (functionDef, input) => invokeFunction(functionDef, input),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
static layerLive = Layer.effect(
|
|
31
|
+
LocalFunctionExecutionService,
|
|
32
|
+
Effect.gen(function* () {
|
|
33
|
+
const resolver = yield* FunctionImplementationResolver;
|
|
34
|
+
return {
|
|
35
|
+
invokeFunction: Effect.fn('invokeFunction')(function* (functionDef, input) {
|
|
36
|
+
// TODO(dmaretskyi): Better error types
|
|
37
|
+
const resolved = yield* resolver.resolveFunctionImplementation(functionDef).pipe(Effect.orDie);
|
|
38
|
+
return yield* invokeFunction(resolved, input);
|
|
39
|
+
}),
|
|
40
|
+
};
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
static invokeFunction: <F extends FunctionDefinition.Any>(
|
|
45
|
+
functionDef: F,
|
|
46
|
+
input: FunctionDefinition.Input<F>,
|
|
47
|
+
) => Effect.Effect<FunctionDefinition.Output<F>, never, Services | LocalFunctionExecutionService> =
|
|
48
|
+
Effect.serviceFunctionEffect(LocalFunctionExecutionService, (_) => _.invokeFunction as any);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const invokeFunction = (
|
|
52
|
+
functionDef: FunctionDefinition<any, any>,
|
|
53
|
+
input: any,
|
|
54
|
+
): Effect.Effect<unknown, never, Services> =>
|
|
55
|
+
Effect.gen(function* () {
|
|
56
|
+
// Assert input matches schema
|
|
57
|
+
try {
|
|
58
|
+
const assertInput = functionDef.inputSchema.pipe(Schema.asserts);
|
|
59
|
+
(assertInput as any)(input);
|
|
60
|
+
} catch (e) {
|
|
61
|
+
throw new FunctionError({ message: 'Invalid function input', context: { name: functionDef.name }, cause: e });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const context: FunctionContext = {
|
|
65
|
+
space: undefined,
|
|
66
|
+
getService: () => todo(),
|
|
67
|
+
getSpace: async (_spaceId: any) => {
|
|
68
|
+
throw new Error('Not available. Use the database service instead.');
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
log.info('Invoking function', { name: functionDef.name, input });
|
|
73
|
+
|
|
74
|
+
// TODO(dmaretskyi): This should be delegated to a function invoker service.
|
|
75
|
+
const data = yield* Effect.gen(function* () {
|
|
76
|
+
const result = functionDef.handler({ context, data: input });
|
|
77
|
+
if (Effect.isEffect(result)) {
|
|
78
|
+
return yield* (result as Effect.Effect<unknown, unknown, Services>).pipe(Effect.orDie);
|
|
79
|
+
} else if (
|
|
80
|
+
typeof result === 'object' &&
|
|
81
|
+
result !== null &&
|
|
82
|
+
'then' in result &&
|
|
83
|
+
typeof result.then === 'function'
|
|
84
|
+
) {
|
|
85
|
+
return yield* Effect.promise(() => result);
|
|
86
|
+
} else {
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
}).pipe(
|
|
90
|
+
Effect.orDie,
|
|
91
|
+
Effect.catchAllDefect((defect) =>
|
|
92
|
+
Effect.die(new FunctionError({ context: { name: functionDef.name }, cause: defect })),
|
|
93
|
+
),
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
log.info('Function completed', { name: functionDef.name, input, data });
|
|
97
|
+
|
|
98
|
+
// Assert output matches schema
|
|
99
|
+
try {
|
|
100
|
+
const assertOutput = functionDef.outputSchema?.pipe(Schema.asserts);
|
|
101
|
+
(assertOutput as any)(data);
|
|
102
|
+
} catch (e) {
|
|
103
|
+
throw new FunctionError({ message: 'Invalid function output', context: { name: functionDef.name }, cause: e });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return data;
|
|
107
|
+
}).pipe(Effect.withSpan('invokeFunction', { attributes: { name: functionDef.name } }));
|
|
108
|
+
|
|
109
|
+
export class FunctionImplementationResolver extends Context.Tag('@dxos/functions/FunctionImplementationResolver')<
|
|
110
|
+
FunctionImplementationResolver,
|
|
111
|
+
{
|
|
112
|
+
resolveFunctionImplementation(
|
|
113
|
+
functionDef: FunctionDefinition<any, any>,
|
|
114
|
+
): Effect.Effect<FunctionDefinition<any, any>, FunctionNotFoundError>;
|
|
115
|
+
}
|
|
116
|
+
>() {
|
|
117
|
+
static layerTest = ({ functions }: { functions: FunctionDefinition<any, any>[] }) =>
|
|
118
|
+
Layer.succeed(FunctionImplementationResolver, {
|
|
119
|
+
resolveFunctionImplementation: (functionDef) => {
|
|
120
|
+
const resolved = functions.find((f) => f.name === functionDef.name);
|
|
121
|
+
if (!resolved) {
|
|
122
|
+
return Effect.fail(new FunctionNotFoundError(functionDef.name));
|
|
123
|
+
}
|
|
124
|
+
return Effect.succeed(resolved);
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
}
|
package/src/services/queues.ts
CHANGED
|
@@ -2,36 +2,81 @@
|
|
|
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 {
|
|
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
|
-
|
|
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:
|
|
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
|
-
|
|
21
|
-
readonly contextQueue: Queue | undefined;
|
|
26
|
+
readonly queue: Queue | undefined;
|
|
22
27
|
}
|
|
23
28
|
>() {
|
|
24
29
|
static notAvailable = Layer.succeed(QueueService, {
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
38
|
+
queue: undefined,
|
|
29
39
|
});
|
|
30
40
|
|
|
31
|
-
static make = (queues: QueueFactory,
|
|
41
|
+
static make = (queues: QueueFactory, queue?: Queue): Context.Tag.Service<QueueService> => {
|
|
32
42
|
return {
|
|
33
43
|
queues,
|
|
34
|
-
|
|
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
|
+
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));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Gives access to a specific queue passed as a context.
|
|
74
|
+
*/
|
|
75
|
+
export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQueueService')<
|
|
76
|
+
ContextQueueService,
|
|
77
|
+
{
|
|
78
|
+
readonly queue: Queue;
|
|
79
|
+
}
|
|
80
|
+
>() {
|
|
81
|
+
static layer = (queue: Queue) => Layer.succeed(ContextQueueService, { queue });
|
|
37
82
|
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Context, Layer } from 'effect';
|
|
6
|
+
|
|
7
|
+
import type { SpaceId } from '@dxos/keys';
|
|
8
|
+
|
|
9
|
+
import { getInvocationUrl } from '../url';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Allows calling into other functions.
|
|
13
|
+
*/
|
|
14
|
+
export class RemoteFunctionExecutionService extends Context.Tag('@dxos/functions/RemoteFunctionExecutionService')<
|
|
15
|
+
RemoteFunctionExecutionService,
|
|
16
|
+
{
|
|
17
|
+
callFunction(deployedFunctionId: string, input: any, spaceId?: SpaceId): Promise<any>;
|
|
18
|
+
}
|
|
19
|
+
>() {
|
|
20
|
+
static fromClient(baseUrl: string, spaceId: SpaceId): Context.Tag.Service<RemoteFunctionExecutionService> {
|
|
21
|
+
return {
|
|
22
|
+
callFunction: async (deployedFunctionId: string, input: any) => {
|
|
23
|
+
const url = getInvocationUrl(deployedFunctionId, baseUrl, { spaceId });
|
|
24
|
+
const result = await fetch(url, {
|
|
25
|
+
method: 'POST',
|
|
26
|
+
headers: { 'Content-Type': 'application/json' },
|
|
27
|
+
body: JSON.stringify(input),
|
|
28
|
+
});
|
|
29
|
+
if (result.status >= 300 || result.status < 200) {
|
|
30
|
+
throw new Error('Failed to invoke function', { cause: new Error(`HTTP error: ${await result.text()}`) });
|
|
31
|
+
}
|
|
32
|
+
return await result.json();
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static mock = (): Context.Tag.Service<RemoteFunctionExecutionService> => {
|
|
38
|
+
return {
|
|
39
|
+
callFunction: async (deployedFunctionId: string, input: any) => {
|
|
40
|
+
return input;
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
static mockLayer = Layer.succeed(RemoteFunctionExecutionService, RemoteFunctionExecutionService.mock());
|
|
46
|
+
}
|
|
@@ -2,65 +2,65 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
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 {
|
|
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
|
|
20
|
+
* List of all services.
|
|
17
21
|
*/
|
|
18
|
-
|
|
19
|
-
ai: AiService
|
|
20
|
-
credentials: CredentialsService
|
|
21
|
-
database: DatabaseService
|
|
22
|
-
eventLogger:
|
|
23
|
-
functionCallService:
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
*
|
|
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
|
-
[
|
|
42
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
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 =
|
|
93
|
-
|
|
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(
|
|
106
|
+
const eventLogger = Layer.succeed(ComputeEventLogger, this._services.eventLogger ?? ComputeEventLogger.noop);
|
|
102
107
|
const functionCallService = Layer.succeed(
|
|
103
|
-
|
|
104
|
-
this._services.functionCallService ??
|
|
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
|
+
}
|
package/src/services/tracing.ts
CHANGED
|
@@ -2,26 +2,139 @@
|
|
|
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
|
|
7
|
+
import { AgentStatus } from '@dxos/ai';
|
|
8
|
+
import { Obj } from '@dxos/echo';
|
|
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';
|
|
8
13
|
|
|
9
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Provides a way for compute primitives (functions, workflows, tools)
|
|
16
|
+
* to emit an execution trace as a series of structured ECHO objects.
|
|
17
|
+
*/
|
|
18
|
+
export class TracingService extends Context.Tag('@dxos/functions/TracingService')<
|
|
10
19
|
TracingService,
|
|
11
20
|
{
|
|
21
|
+
/**
|
|
22
|
+
* Gets the parent message ID.
|
|
23
|
+
*/
|
|
24
|
+
getTraceContext: () => TracingService.TraceContext;
|
|
25
|
+
|
|
12
26
|
/**
|
|
13
27
|
* Write an event to the tracing queue.
|
|
14
28
|
* @param event - The event to write. Must be an a typed object.
|
|
15
29
|
*/
|
|
16
|
-
write(event:
|
|
30
|
+
write: (event: Obj.Any) => void;
|
|
17
31
|
}
|
|
18
32
|
>() {
|
|
19
|
-
static noop: Context.Tag.Service<TracingService> = { write: () => {} };
|
|
33
|
+
static noop: Context.Tag.Service<TracingService> = { write: () => {}, getTraceContext: () => ({}) };
|
|
34
|
+
|
|
35
|
+
static layerNoop = Layer.succeed(TracingService, TracingService.noop);
|
|
20
36
|
|
|
21
37
|
static console: Context.Tag.Service<TracingService> = {
|
|
22
38
|
write: (event) => {
|
|
23
39
|
// eslint-disable-next-line no-console
|
|
24
40
|
console.log(event);
|
|
25
41
|
},
|
|
42
|
+
getTraceContext: () => ({}),
|
|
26
43
|
};
|
|
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
|
+
debugInfo: {
|
|
82
|
+
queue: queue.dxn.toString(),
|
|
83
|
+
},
|
|
84
|
+
}),
|
|
85
|
+
};
|
|
86
|
+
}),
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Emit the current human-readable execution status.
|
|
91
|
+
*/
|
|
92
|
+
static emitStatus: (data: Obj.MakeProps<typeof AgentStatus>) => Effect.Effect<void, never, TracingService> =
|
|
93
|
+
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
|
+
...data,
|
|
100
|
+
}),
|
|
101
|
+
);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
static emitConverationMessage: (
|
|
105
|
+
data: Obj.MakeProps<typeof DataType.Message>,
|
|
106
|
+
) => Effect.Effect<void, never, TracingService> = Effect.fnUntraced(function* (data) {
|
|
107
|
+
const tracing = yield* TracingService;
|
|
108
|
+
tracing.write(
|
|
109
|
+
Obj.make(DataType.Message, {
|
|
110
|
+
parentMessage: tracing.getTraceContext().parentMessage,
|
|
111
|
+
...data,
|
|
112
|
+
properties: {
|
|
113
|
+
[MESSAGE_PROPERTY_TOOL_CALL_ID]: tracing.getTraceContext().toolCallId,
|
|
114
|
+
...data.properties,
|
|
115
|
+
},
|
|
116
|
+
}),
|
|
117
|
+
);
|
|
118
|
+
});
|
|
27
119
|
}
|
|
120
|
+
|
|
121
|
+
export namespace TracingService {
|
|
122
|
+
export interface TraceContext {
|
|
123
|
+
/**
|
|
124
|
+
* If this thread sprung from a tool call, this is the ID of the message containing the tool call.
|
|
125
|
+
*/
|
|
126
|
+
parentMessage?: ObjectId;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* If the current thread is a byproduct of a tool call, this is the ID of the tool call.
|
|
130
|
+
*/
|
|
131
|
+
toolCallId?: string;
|
|
132
|
+
|
|
133
|
+
debugInfo?: unknown;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Goes into {@link DataType.Message['properties']}
|
|
139
|
+
*/
|
|
140
|
+
export const MESSAGE_PROPERTY_TOOL_CALL_ID = 'toolCallId' as const;
|
package/src/testing/index.ts
CHANGED