@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.
- package/dist/lib/browser/bundler/index.mjs +56 -38
- package/dist/lib/browser/bundler/index.mjs.map +3 -3
- package/dist/lib/browser/chunk-C2Z7LCJ2.mjs +649 -0
- package/dist/lib/browser/chunk-C2Z7LCJ2.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 +22 -8
- package/dist/lib/browser/edge/index.mjs.map +3 -3
- package/dist/lib/browser/index.mjs +992 -127
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +76 -6
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/bundler/index.mjs +55 -38
- package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
- package/dist/lib/node-esm/chunk-AH3AZM2U.mjs +651 -0
- package/dist/lib/node-esm/chunk-AH3AZM2U.mjs.map +7 -0
- 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/edge/index.mjs +21 -8
- package/dist/lib/node-esm/edge/index.mjs.map +3 -3
- package/dist/lib/node-esm/index.mjs +992 -127
- 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 +76 -6
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/bundler/bundler.d.ts +11 -12
- package/dist/types/src/bundler/bundler.d.ts.map +1 -1
- package/dist/types/src/edge/functions.d.ts +3 -2
- package/dist/types/src/edge/functions.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +89 -20
- package/dist/types/src/errors.d.ts.map +1 -1
- 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 +7 -1
- package/dist/types/src/executor/executor.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +52 -8
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/schema.d.ts +6 -1
- package/dist/types/src/schema.d.ts.map +1 -1
- package/dist/types/src/services/credentials.d.ts +15 -3
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/database.d.ts +39 -6
- package/dist/types/src/services/database.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +1 -1
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +26 -0
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
- package/dist/types/src/services/function-invocation-service.test.d.ts +2 -0
- package/dist/types/src/services/function-invocation-service.test.d.ts.map +1 -0
- package/dist/types/src/services/index.d.ts +4 -3
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/local-function-execution.d.ts +23 -2
- package/dist/types/src/services/local-function-execution.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +19 -5
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/remote-function-execution-service.d.ts +9 -4
- package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -1
- package/dist/types/src/services/service-container.d.ts +1 -1
- package/dist/types/src/services/service-container.d.ts.map +1 -1
- package/dist/types/src/services/service-registry.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +34 -3
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +9 -2
- package/dist/types/src/testing/layer.d.ts.map +1 -1
- 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 +1 -1
- 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/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 +74 -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 +60 -250
- 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 +43 -43
- package/src/bundler/bundler.test.ts +8 -9
- package/src/bundler/bundler.ts +32 -33
- package/src/edge/functions.ts +8 -5
- package/src/errors.ts +13 -5
- package/src/examples/fib.ts +31 -0
- package/src/examples/index.ts +7 -0
- package/src/examples/reply.ts +19 -0
- package/src/examples/sleep.ts +23 -0
- package/src/executor/executor.ts +12 -9
- package/src/handler.ts +120 -18
- package/src/index.ts +3 -3
- package/src/schema.ts +11 -0
- package/src/services/credentials.ts +80 -5
- package/src/services/database.ts +115 -18
- package/src/services/event-logger.ts +2 -2
- package/src/services/function-invocation-service.test.ts +79 -0
- package/src/services/function-invocation-service.ts +82 -0
- package/src/services/index.ts +4 -3
- package/src/services/local-function-execution.ts +97 -17
- package/src/services/queues.ts +34 -10
- package/src/services/remote-function-execution-service.ts +38 -43
- package/src/services/service-container.ts +4 -3
- package/src/services/service-registry.ts +1 -1
- package/src/services/tracing.ts +106 -9
- package/src/testing/layer.ts +84 -3
- package/src/testing/logger.ts +1 -1
- package/src/testing/persist-database.test.ts +87 -0
- package/src/testing/services.ts +3 -2
- package/src/trace.ts +17 -19
- 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 +651 -0
- package/src/triggers/trigger-dispatcher.ts +522 -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-6PTFLPCO.mjs +0 -462
- package/dist/lib/browser/chunk-6PTFLPCO.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-NYJ2TSXO.mjs +0 -464
- 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 {
|
|
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(
|
|
32
|
+
invokeFunction<I, O>(functionDef: FunctionDefinition<I, O>, input: I): Effect.Effect<O, never, InvocationServices>;
|
|
19
33
|
}
|
|
20
34
|
>() {
|
|
21
|
-
static
|
|
22
|
-
|
|
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 = (
|
|
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
|
-
|
|
30
|
-
|
|
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 =
|
|
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(
|
|
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
|
-
|
|
67
|
-
|
|
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:
|
|
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
|
+
}
|
package/src/services/queues.ts
CHANGED
|
@@ -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
|
|
26
|
+
readonly queue: Queue | undefined;
|
|
25
27
|
}
|
|
26
28
|
>() {
|
|
27
29
|
static notAvailable = Layer.succeed(QueueService, {
|
|
28
30
|
queues: {
|
|
29
|
-
get(
|
|
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
|
-
|
|
38
|
+
queue: undefined,
|
|
37
39
|
});
|
|
38
40
|
|
|
39
|
-
static make = (queues: QueueFactory,
|
|
41
|
+
static make = (queues: QueueFactory, queue?: Queue): Context.Tag.Service<QueueService> => {
|
|
40
42
|
return {
|
|
41
43
|
queues,
|
|
42
|
-
|
|
44
|
+
queue,
|
|
43
45
|
};
|
|
44
46
|
};
|
|
45
47
|
|
|
46
|
-
static
|
|
47
|
-
Layer.succeed(QueueService, QueueService.make(queues,
|
|
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
|
|
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:
|
|
18
|
+
callFunction<I, O>(deployedFunctionId: string, input: I): Effect.Effect<O>;
|
|
16
19
|
}
|
|
17
20
|
>() {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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:
|
|
38
|
-
|
|
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
|
|
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
|
|
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 =
|
|
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(),
|
package/src/services/tracing.ts
CHANGED
|
@@ -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 {
|
|
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:
|
|
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: (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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;
|
package/src/testing/layer.ts
CHANGED
|
@@ -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
|
-
|
|
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)),
|