@dxos/functions 0.8.3 → 0.8.4-main.28f8d3d
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/bundler/index.mjs +22 -10
- package/dist/lib/browser/bundler/index.mjs.map +3 -3
- package/dist/lib/browser/chunk-M2OIFLGE.mjs +486 -0
- package/dist/lib/browser/chunk-M2OIFLGE.mjs.map +7 -0
- package/dist/lib/browser/edge/index.mjs +2 -2
- package/dist/lib/browser/edge/index.mjs.map +1 -1
- package/dist/lib/browser/index.mjs +51 -37
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +39 -8
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node-esm/bundler/index.mjs +22 -10
- package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
- package/dist/lib/node-esm/chunk-K7REX6H4.mjs +488 -0
- package/dist/lib/node-esm/chunk-K7REX6H4.mjs.map +7 -0
- package/dist/lib/node-esm/edge/index.mjs +2 -2
- package/dist/lib/node-esm/edge/index.mjs.map +1 -1
- package/dist/lib/node-esm/index.mjs +51 -37
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +39 -8
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- package/dist/types/src/bundler/bundler.d.ts +1 -2
- package/dist/types/src/bundler/bundler.d.ts.map +1 -1
- package/dist/types/src/edge/functions.d.ts +1 -1
- package/dist/types/src/edge/functions.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +70 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/executor/executor.d.ts +3 -0
- package/dist/types/src/executor/executor.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +16 -15
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/schema.d.ts +5 -5
- package/dist/types/src/services/credentials.d.ts +7 -3
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/database.d.ts +71 -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 +12 -0
- package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
- package/dist/types/src/services/queues.d.ts +32 -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 +13 -2
- 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 +10 -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/services.d.ts +55 -9
- package/dist/types/src/testing/services.d.ts.map +1 -1
- package/dist/types/src/trace.d.ts +3 -5
- 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/types.d.ts +7 -7
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +36 -26
- package/src/bundler/bundler.test.ts +1 -1
- package/src/bundler/bundler.ts +26 -4
- package/src/edge/functions.ts +1 -1
- package/src/errors.ts +13 -0
- package/src/executor/executor.ts +20 -13
- package/src/handler.ts +34 -25
- package/src/index.ts +1 -0
- package/src/services/credentials.ts +47 -4
- package/src/services/database.ts +91 -3
- package/src/services/event-logger.ts +68 -37
- package/src/services/index.ts +2 -2
- package/src/services/local-function-execution.ts +74 -0
- package/src/services/queues.ts +51 -11
- package/src/services/{function-call-service.ts → remote-function-execution-service.ts} +7 -5
- 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 +19 -2
- package/src/testing/index.ts +1 -0
- package/src/testing/layer.ts +31 -0
- package/src/testing/logger.ts +4 -4
- package/src/testing/services.ts +97 -14
- package/src/trace.ts +3 -5
- package/src/translations.ts +4 -4
- package/src/types.ts +1 -1
- 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
|
@@ -2,53 +2,81 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Effect,
|
|
5
|
+
import { Context, Effect, Layer, Schema } from 'effect';
|
|
6
6
|
|
|
7
|
+
import { Obj, Type } from '@dxos/echo';
|
|
7
8
|
import { invariant } from '@dxos/invariant';
|
|
8
|
-
import {
|
|
9
|
+
import { LogLevel, log } from '@dxos/log';
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
11
|
+
import { TracingService } from './tracing';
|
|
12
|
+
|
|
13
|
+
export const ComputeEventPayload = Schema.Union(
|
|
14
|
+
Schema.Struct({
|
|
15
|
+
type: Schema.Literal('begin-compute'),
|
|
16
|
+
nodeId: Schema.String,
|
|
17
|
+
inputs: Schema.Record({ key: Schema.String, value: Schema.Any }),
|
|
18
|
+
}),
|
|
19
|
+
Schema.Struct({
|
|
20
|
+
type: Schema.Literal('end-compute'),
|
|
21
|
+
nodeId: Schema.String,
|
|
22
|
+
outputs: Schema.Record({ key: Schema.String, value: Schema.Any }),
|
|
23
|
+
}),
|
|
24
|
+
Schema.Struct({
|
|
25
|
+
type: Schema.Literal('compute-input'),
|
|
26
|
+
nodeId: Schema.String,
|
|
27
|
+
property: Schema.String,
|
|
28
|
+
value: Schema.Any,
|
|
29
|
+
}),
|
|
30
|
+
Schema.Struct({
|
|
31
|
+
type: Schema.Literal('compute-output'),
|
|
32
|
+
nodeId: Schema.String,
|
|
33
|
+
property: Schema.String,
|
|
34
|
+
value: Schema.Any,
|
|
35
|
+
}),
|
|
36
|
+
Schema.Struct({
|
|
37
|
+
type: Schema.Literal('custom'),
|
|
38
|
+
nodeId: Schema.String,
|
|
39
|
+
event: Schema.Any,
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
export type ComputeEventPayload = Schema.Schema.Type<typeof ComputeEventPayload>;
|
|
38
43
|
|
|
39
|
-
export
|
|
40
|
-
|
|
41
|
-
|
|
44
|
+
export const ComputeEvent = Schema.Struct({
|
|
45
|
+
payload: ComputeEventPayload,
|
|
46
|
+
}).pipe(Type.Obj({ typename: 'dxos.org/type/ComputeEvent', version: '0.1.0' }));
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Logs event for the compute workflows.
|
|
50
|
+
*/
|
|
51
|
+
export class ComputeEventLogger extends Context.Tag('@dxos/functions/ComputeEventLogger')<
|
|
52
|
+
ComputeEventLogger,
|
|
53
|
+
{ readonly log: (event: ComputeEventPayload) => void; readonly nodeId: string | undefined }
|
|
42
54
|
>() {
|
|
43
|
-
static noop: Context.Tag.Service<
|
|
55
|
+
static noop: Context.Tag.Service<ComputeEventLogger> = {
|
|
44
56
|
log: () => {},
|
|
45
57
|
nodeId: undefined,
|
|
46
58
|
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Implements ComputeEventLogger using TracingService.
|
|
62
|
+
*/
|
|
63
|
+
static layerFromTracing = Layer.effect(
|
|
64
|
+
ComputeEventLogger,
|
|
65
|
+
Effect.gen(function* () {
|
|
66
|
+
const tracing = yield* TracingService;
|
|
67
|
+
return {
|
|
68
|
+
log: (event: ComputeEventPayload) => {
|
|
69
|
+
tracing.write(Obj.make(ComputeEvent, { payload: event }));
|
|
70
|
+
},
|
|
71
|
+
nodeId: undefined,
|
|
72
|
+
};
|
|
73
|
+
}),
|
|
74
|
+
);
|
|
47
75
|
}
|
|
48
76
|
|
|
49
77
|
export const logCustomEvent = (data: any) =>
|
|
50
78
|
Effect.gen(function* () {
|
|
51
|
-
const logger = yield*
|
|
79
|
+
const logger = yield* ComputeEventLogger;
|
|
52
80
|
if (!logger.nodeId) {
|
|
53
81
|
throw new Error('logCustomEvent must be called within a node compute function');
|
|
54
82
|
}
|
|
@@ -67,7 +95,10 @@ export const createDefectLogger = <A, E, R>(): ((self: Effect.Effect<A, E, R>) =
|
|
|
67
95
|
}),
|
|
68
96
|
);
|
|
69
97
|
|
|
70
|
-
export const createEventLogger = (
|
|
98
|
+
export const createEventLogger = (
|
|
99
|
+
level: LogLevel,
|
|
100
|
+
message: string = 'event',
|
|
101
|
+
): Context.Tag.Service<ComputeEventLogger> => {
|
|
71
102
|
const logFunction = (
|
|
72
103
|
{
|
|
73
104
|
[LogLevel.WARN]: log.warn,
|
|
@@ -79,7 +110,7 @@ export const createEventLogger = (level: LogLevel, message: string = 'event'): C
|
|
|
79
110
|
)[level];
|
|
80
111
|
invariant(logFunction);
|
|
81
112
|
return {
|
|
82
|
-
log: (event:
|
|
113
|
+
log: (event: ComputeEventPayload) => {
|
|
83
114
|
logFunction(message, event);
|
|
84
115
|
},
|
|
85
116
|
nodeId: undefined,
|
package/src/services/index.ts
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
export * from './ai';
|
|
6
5
|
export * from './database';
|
|
7
6
|
export * from './queues';
|
|
8
7
|
export * from './service-container';
|
|
9
8
|
export * from './credentials';
|
|
10
9
|
export * from './tracing';
|
|
11
10
|
export * from './event-logger';
|
|
12
|
-
export * from './function-
|
|
11
|
+
export * from './remote-function-execution-service';
|
|
12
|
+
export * from './local-function-execution';
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Context, Effect, Layer, Schema } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { todo } from '@dxos/debug';
|
|
8
|
+
|
|
9
|
+
import { FunctionError } from '../errors';
|
|
10
|
+
import type { FunctionContext, FunctionDefinition } from '../handler';
|
|
11
|
+
|
|
12
|
+
import type { Services } from './service-container';
|
|
13
|
+
|
|
14
|
+
export class LocalFunctionExecutionService extends Context.Tag('@dxos/functions/LocalFunctionExecutionService')<
|
|
15
|
+
LocalFunctionExecutionService,
|
|
16
|
+
{
|
|
17
|
+
// TODO(dmaretskyi): This should take function id instead of the definition object.
|
|
18
|
+
// TODO(dmaretskyi): Services should be satisfied from environment rather then bubbled up.
|
|
19
|
+
invokeFunction(fnDef: FunctionDefinition<any, any>, input: unknown): Effect.Effect<unknown, never, Services>;
|
|
20
|
+
}
|
|
21
|
+
>() {
|
|
22
|
+
static layer = Layer.succeed(LocalFunctionExecutionService, {
|
|
23
|
+
invokeFunction: (fnDef, input) => invokeFunction(fnDef, input),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
static invokeFunction: <F extends FunctionDefinition.Any>(
|
|
27
|
+
fnDef: F,
|
|
28
|
+
input: FunctionDefinition.Input<F>,
|
|
29
|
+
) => Effect.Effect<FunctionDefinition.Output<F>, never, Services | LocalFunctionExecutionService> =
|
|
30
|
+
Effect.serviceFunctionEffect(LocalFunctionExecutionService, (_) => _.invokeFunction as any);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const invokeFunction = (fnDef: FunctionDefinition<any, any>, input: any): Effect.Effect<unknown, never, Services> =>
|
|
34
|
+
Effect.gen(function* () {
|
|
35
|
+
// Assert input matches schema
|
|
36
|
+
const assertInput = fnDef.inputSchema.pipe(Schema.asserts);
|
|
37
|
+
(assertInput as any)(input);
|
|
38
|
+
|
|
39
|
+
const context: FunctionContext = {
|
|
40
|
+
space: undefined,
|
|
41
|
+
getService: () => todo(),
|
|
42
|
+
getSpace: async (_spaceId: any) => {
|
|
43
|
+
throw new Error('Not available. Use the database service instead.');
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// TODO(dmaretskyi): This should be delegated to a function invoker service.
|
|
48
|
+
const data = yield* Effect.gen(function* () {
|
|
49
|
+
const result = fnDef.handler({ context, data: input });
|
|
50
|
+
if (Effect.isEffect(result)) {
|
|
51
|
+
return yield* (result as Effect.Effect<unknown, unknown, Services>).pipe(Effect.orDie);
|
|
52
|
+
} else if (
|
|
53
|
+
typeof result === 'object' &&
|
|
54
|
+
result !== null &&
|
|
55
|
+
'then' in result &&
|
|
56
|
+
typeof result.then === 'function'
|
|
57
|
+
) {
|
|
58
|
+
return yield* Effect.promise(() => result);
|
|
59
|
+
} else {
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
}).pipe(
|
|
63
|
+
Effect.orDie,
|
|
64
|
+
Effect.catchAllDefect((defect) =>
|
|
65
|
+
Effect.die(new FunctionError('Error running function', { context: { name: fnDef.name }, cause: defect })),
|
|
66
|
+
),
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
// Assert output matches schema
|
|
70
|
+
const assertOutput = fnDef.outputSchema?.pipe(Schema.asserts);
|
|
71
|
+
(assertOutput as any)(data);
|
|
72
|
+
|
|
73
|
+
return data;
|
|
74
|
+
}).pipe(Effect.withSpan('invokeFunction', { attributes: { name: fnDef.name } }));
|
package/src/services/queues.ts
CHANGED
|
@@ -2,36 +2,76 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Context, Layer } from 'effect';
|
|
5
|
+
import { Context, Effect, Layer } from 'effect';
|
|
6
6
|
|
|
7
|
-
import type {
|
|
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
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Gives access to a specific queue passed as a context.
|
|
69
|
+
*/
|
|
70
|
+
export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQueueService')<
|
|
71
|
+
ContextQueueService,
|
|
72
|
+
{
|
|
73
|
+
readonly queue: Queue;
|
|
74
|
+
}
|
|
75
|
+
>() {
|
|
76
|
+
static layer = (queue: Queue) => Layer.succeed(ContextQueueService, { queue });
|
|
37
77
|
}
|
|
@@ -2,20 +2,20 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Context } from 'effect';
|
|
5
|
+
import { Context, Layer } from 'effect';
|
|
6
6
|
|
|
7
7
|
import type { SpaceId } from '@dxos/keys';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Allows calling into other functions.
|
|
11
11
|
*/
|
|
12
|
-
export class
|
|
13
|
-
|
|
12
|
+
export class RemoteFunctionExecutionService extends Context.Tag('@dxos/functions/RemoteFunctionExecutionService')<
|
|
13
|
+
RemoteFunctionExecutionService,
|
|
14
14
|
{
|
|
15
15
|
callFunction(deployedFunctionId: string, input: any, spaceId?: SpaceId): Promise<any>;
|
|
16
16
|
}
|
|
17
17
|
>() {
|
|
18
|
-
static fromClient(baseUrl: string, spaceId: SpaceId): Context.Tag.Service<
|
|
18
|
+
static fromClient(baseUrl: string, spaceId: SpaceId): Context.Tag.Service<RemoteFunctionExecutionService> {
|
|
19
19
|
return {
|
|
20
20
|
callFunction: async (deployedFunctionId: string, input: any) => {
|
|
21
21
|
const url = getInvocationUrl(deployedFunctionId, baseUrl, { spaceId });
|
|
@@ -32,13 +32,15 @@ export class FunctionCallService extends Context.Tag('FunctionCallService')<
|
|
|
32
32
|
};
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
static mock = () => {
|
|
35
|
+
static mock = (): Context.Tag.Service<RemoteFunctionExecutionService> => {
|
|
36
36
|
return {
|
|
37
37
|
callFunction: async (deployedFunctionId: string, input: any) => {
|
|
38
38
|
return input;
|
|
39
39
|
},
|
|
40
40
|
};
|
|
41
41
|
};
|
|
42
|
+
|
|
43
|
+
static mockLayer = Layer.succeed(RemoteFunctionExecutionService, RemoteFunctionExecutionService.mock());
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
// TODO(dmaretskyi): Reconcile with `getInvocationUrl` in `@dxos/functions/edge`.
|
|
@@ -2,65 +2,65 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
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,11 +2,17 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Context } from 'effect';
|
|
5
|
+
import { Context, Effect, Layer } from 'effect';
|
|
6
6
|
|
|
7
|
+
import { AgentStatus } from '@dxos/ai';
|
|
8
|
+
import { Obj } from '@dxos/echo';
|
|
7
9
|
import type { AnyEchoObject } from '@dxos/echo-schema';
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Provides a way for compute primitives (functions, workflows, tools)
|
|
13
|
+
* to emit an execution trace as a series of structured ECHO objects.
|
|
14
|
+
*/
|
|
15
|
+
export class TracingService extends Context.Tag('@dxos/functions/TracingService')<
|
|
10
16
|
TracingService,
|
|
11
17
|
{
|
|
12
18
|
/**
|
|
@@ -18,10 +24,21 @@ export class TracingService extends Context.Tag('TracingService')<
|
|
|
18
24
|
>() {
|
|
19
25
|
static noop: Context.Tag.Service<TracingService> = { write: () => {} };
|
|
20
26
|
|
|
27
|
+
static layerNoop = Layer.succeed(TracingService, TracingService.noop);
|
|
28
|
+
|
|
21
29
|
static console: Context.Tag.Service<TracingService> = {
|
|
22
30
|
write: (event) => {
|
|
23
31
|
// eslint-disable-next-line no-console
|
|
24
32
|
console.log(event);
|
|
25
33
|
},
|
|
26
34
|
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Emit the current human-readable execution status.
|
|
38
|
+
*/
|
|
39
|
+
static emitStatus: (data: Obj.MakeProps<typeof AgentStatus>) => Effect.Effect<void, never, TracingService> =
|
|
40
|
+
Effect.fnUntraced(function* (data) {
|
|
41
|
+
const tracing = yield* TracingService;
|
|
42
|
+
tracing.write(Obj.make(AgentStatus, data));
|
|
43
|
+
});
|
|
27
44
|
}
|
package/src/testing/index.ts
CHANGED