@dxos/functions 0.8.4-main.84f28bd → 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 +1007 -133
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +91 -41
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- 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 +1007 -133
- 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 +91 -41
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- 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 +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 +7 -1
- package/dist/types/src/executor/executor.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +57 -12
- 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 +5 -0
- package/dist/types/src/schema.d.ts.map +1 -1
- package/dist/types/src/services/credentials.d.ts +17 -3
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/database.d.ts +54 -6
- 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/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 +5 -5
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/local-function-execution.d.ts +32 -0
- package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
- package/dist/types/src/services/queues.d.ts +31 -4
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/remote-function-execution-service.d.ts +20 -0
- package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -0
- package/dist/types/src/services/service-container.d.ts +28 -18
- 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 +17 -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 +7 -19
- 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 +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 +40 -33
- package/src/bundler/bundler.test.ts +8 -9
- package/src/bundler/bundler.ts +32 -33
- package/src/edge/functions.ts +9 -6
- package/src/errors.ts +21 -0
- 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 +133 -24
- package/src/index.ts +3 -2
- package/src/schema.ts +11 -0
- package/src/services/credentials.ts +88 -4
- package/src/services/database.ts +141 -20
- package/src/services/event-logger.ts +68 -37
- package/src/services/function-invocation-service.test.ts +79 -0
- package/src/services/function-invocation-service.ts +82 -0
- package/src/services/index.ts +5 -5
- package/src/services/local-function-execution.ts +150 -0
- package/src/services/queues.ts +48 -8
- package/src/services/remote-function-execution-service.ts +61 -0
- package/src/services/service-container.ts +45 -58
- package/src/services/service-registry.test.ts +42 -0
- package/src/services/service-registry.ts +59 -0
- package/src/services/tracing.ts +120 -6
- package/src/testing/index.ts +1 -0
- package/src/testing/layer.ts +112 -0
- package/src/testing/logger.ts +4 -4
- package/src/testing/persist-database.test.ts +87 -0
- package/src/testing/services.ts +11 -70
- 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-54U464M4.mjs +0 -360
- package/dist/lib/browser/chunk-54U464M4.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-XDSX35BS.mjs +0 -362
- package/dist/lib/node-esm/chunk-XDSX35BS.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/dist/types/src/services/tool-resolver.d.ts +0 -14
- package/dist/types/src/services/tool-resolver.d.ts.map +0 -1
- package/src/services/ai.ts +0 -32
- package/src/services/function-call-service.ts +0 -64
- package/src/services/tool-resolver.ts +0 -31
|
@@ -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,140 @@
|
|
|
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
|
-
// eslint-disable-next-line no-console
|
|
24
39
|
console.log(event);
|
|
25
40
|
},
|
|
41
|
+
getTraceContext: () => ({}),
|
|
26
42
|
};
|
|
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
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Emit the current human-readable execution status.
|
|
90
|
+
*/
|
|
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
|
+
});
|
|
27
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/index.ts
CHANGED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type { Schema } from 'effect';
|
|
6
|
+
import { Context, Effect, Layer } from 'effect';
|
|
7
|
+
|
|
8
|
+
import type { EchoDatabaseImpl, QueueFactory } from '@dxos/echo-db';
|
|
9
|
+
import { EchoTestBuilder } from '@dxos/echo-db/testing';
|
|
10
|
+
import type { EchoHostIndexingConfig } from '@dxos/echo-pipeline';
|
|
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';
|
|
16
|
+
|
|
17
|
+
import { DatabaseService, QueueService } from '../services';
|
|
18
|
+
|
|
19
|
+
const testBuilder = accuireReleaseResource(() => new EchoTestBuilder());
|
|
20
|
+
|
|
21
|
+
export const testStoragePath = ({ name = PublicKey.random().toHex() }: { name?: string }) => {
|
|
22
|
+
return `/tmp/dxos-${name}`;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type TestDatabaseOptions = {
|
|
26
|
+
indexing?: Partial<EchoHostIndexingConfig>;
|
|
27
|
+
types?: Schema.Schema.AnyNoContext[];
|
|
28
|
+
spaceKey?: PublicKey;
|
|
29
|
+
storagePath?: string;
|
|
30
|
+
onInit?: () => Effect.Effect<void, never, DatabaseService | QueueService>;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const TestDatabaseLayer = ({ indexing, types, spaceKey, storagePath, onInit }: TestDatabaseOptions = {}) =>
|
|
34
|
+
Layer.scopedContext(
|
|
35
|
+
Effect.gen(function* () {
|
|
36
|
+
const builder = yield* testBuilder;
|
|
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
|
+
|
|
107
|
+
return Context.mergeAll(
|
|
108
|
+
Context.make(DatabaseService, DatabaseService.make(db)),
|
|
109
|
+
Context.make(QueueService, QueueService.make(queues, undefined)),
|
|
110
|
+
);
|
|
111
|
+
}),
|
|
112
|
+
);
|
package/src/testing/logger.ts
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { type Context, Effect } from 'effect';
|
|
6
6
|
|
|
7
7
|
import { LogLevel } from '@dxos/log';
|
|
8
8
|
|
|
9
|
-
import { type
|
|
9
|
+
import { type ComputeEventLogger, createEventLogger } from '../services';
|
|
10
10
|
|
|
11
|
-
export const noopLogger: Context.Tag.Service<
|
|
11
|
+
export const noopLogger: Context.Tag.Service<ComputeEventLogger> = {
|
|
12
12
|
log: () => Effect.succeed(undefined),
|
|
13
13
|
nodeId: undefined,
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
export const consoleLogger: Context.Tag.Service<
|
|
16
|
+
export const consoleLogger: Context.Tag.Service<ComputeEventLogger> = createEventLogger(LogLevel.INFO);
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, expect, it } from '@effect/vitest';
|
|
6
|
+
import { Effect } from 'effect';
|
|
7
|
+
|
|
8
|
+
import { Filter, Obj, Query, Type } from '@dxos/echo';
|
|
9
|
+
import { DataType } from '@dxos/schema';
|
|
10
|
+
|
|
11
|
+
import { DatabaseService } from '../services';
|
|
12
|
+
|
|
13
|
+
import { TestDatabaseLayer, testStoragePath } from './layer';
|
|
14
|
+
|
|
15
|
+
describe('TestDatabaseLayer', { timeout: 600_000 }, () => {
|
|
16
|
+
it.effect(
|
|
17
|
+
'persist database to disk',
|
|
18
|
+
Effect.fnUntraced(function* (_) {
|
|
19
|
+
const DbLayer = TestDatabaseLayer({
|
|
20
|
+
storagePath: testStoragePath({ name: `feed-test-${Date.now()}` }),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
yield* Effect.gen(function* () {
|
|
24
|
+
yield* DatabaseService.add(Obj.make(Type.Expando, { label: 'test' }));
|
|
25
|
+
yield* DatabaseService.flush({ indexes: true });
|
|
26
|
+
}).pipe(Effect.provide(DbLayer));
|
|
27
|
+
|
|
28
|
+
yield* Effect.gen(function* () {
|
|
29
|
+
const { objects } = yield* DatabaseService.runQuery(Query.select(Filter.everything()));
|
|
30
|
+
expect(objects[0]?.label).toEqual('test');
|
|
31
|
+
}).pipe(Effect.provide(DbLayer));
|
|
32
|
+
}),
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
it.effect(
|
|
36
|
+
'reload database -- save index before restart',
|
|
37
|
+
Effect.fnUntraced(function* (_) {
|
|
38
|
+
const NUM_OBJECTS = 500;
|
|
39
|
+
const DbLayer = TestDatabaseLayer({
|
|
40
|
+
types: [DataType.Person],
|
|
41
|
+
storagePath: testStoragePath({ name: `reload-test-${Date.now()}` }),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
yield* Effect.gen(function* () {
|
|
45
|
+
for (let i = 0; i < NUM_OBJECTS; i++) {
|
|
46
|
+
yield* DatabaseService.add(Obj.make(DataType.Person, { nickname: `Person ${i}` }));
|
|
47
|
+
}
|
|
48
|
+
yield* DatabaseService.flush({ indexes: true });
|
|
49
|
+
}).pipe(Effect.provide(DbLayer));
|
|
50
|
+
|
|
51
|
+
yield* Effect.gen(function* () {
|
|
52
|
+
const { objects } = yield* DatabaseService.runQuery(Query.select(Filter.type(DataType.Person)));
|
|
53
|
+
expect(objects.length).toEqual(NUM_OBJECTS);
|
|
54
|
+
}).pipe(Effect.provide(DbLayer));
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
it.effect.skip(
|
|
59
|
+
'reload database -- save index before restart [manual]',
|
|
60
|
+
Effect.fnUntraced(
|
|
61
|
+
function* (_) {
|
|
62
|
+
const NUM_OBJECTS = 500;
|
|
63
|
+
|
|
64
|
+
{
|
|
65
|
+
const { objects } = yield* DatabaseService.runQuery(Query.select(Filter.type(DataType.Person)));
|
|
66
|
+
console.log({ count: objects.length });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for (let i = 0; i < NUM_OBJECTS; i++) {
|
|
70
|
+
yield* DatabaseService.add(Obj.make(DataType.Person, { nickname: `Person ${i}` }));
|
|
71
|
+
}
|
|
72
|
+
yield* DatabaseService.flush({ indexes: true });
|
|
73
|
+
|
|
74
|
+
{
|
|
75
|
+
const { objects } = yield* DatabaseService.runQuery(Query.select(Filter.type(DataType.Person)));
|
|
76
|
+
console.log({ count: objects.length });
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
Effect.provide(
|
|
80
|
+
TestDatabaseLayer({
|
|
81
|
+
types: [DataType.Person],
|
|
82
|
+
storagePath: testStoragePath({ name: `reload-test` }),
|
|
83
|
+
}),
|
|
84
|
+
),
|
|
85
|
+
),
|
|
86
|
+
);
|
|
87
|
+
});
|
package/src/testing/services.ts
CHANGED
|
@@ -4,26 +4,23 @@
|
|
|
4
4
|
|
|
5
5
|
import { type Context } from 'effect';
|
|
6
6
|
|
|
7
|
-
import { type AiServiceEdgeClientOptions, EdgeAiServiceClient, type AiServiceClient, ToolRegistry } from '@dxos/ai';
|
|
8
|
-
import { AI_SERVICE_ENDPOINT, createTestAiServiceClient } from '@dxos/ai/testing';
|
|
9
7
|
import type { Space } from '@dxos/client/echo';
|
|
10
8
|
import type { EchoDatabase, QueueFactory } from '@dxos/echo-db';
|
|
11
9
|
import { assertArgument } from '@dxos/invariant';
|
|
12
10
|
|
|
13
|
-
import { consoleLogger, noopLogger } from './logger';
|
|
14
11
|
import {
|
|
15
|
-
|
|
12
|
+
type ComputeEventLogger,
|
|
16
13
|
ConfiguredCredentialsService,
|
|
17
14
|
type CredentialsService,
|
|
18
15
|
DatabaseService,
|
|
19
|
-
type EventLogger,
|
|
20
16
|
QueueService,
|
|
21
17
|
ServiceContainer,
|
|
22
18
|
type ServiceCredential,
|
|
23
|
-
ToolResolverService,
|
|
24
19
|
type TracingService,
|
|
25
20
|
} from '../services';
|
|
26
21
|
|
|
22
|
+
import { consoleLogger, noopLogger } from './logger';
|
|
23
|
+
|
|
27
24
|
// TODO(burdon): Factor out.
|
|
28
25
|
export type OneOf<T> = {
|
|
29
26
|
[K in keyof T]: { [P in K]: T[P] } & { [P in Exclude<keyof T, K>]?: never };
|
|
@@ -35,23 +32,7 @@ export type TestServiceOptions = {
|
|
|
35
32
|
/**
|
|
36
33
|
* AI service configuration.
|
|
37
34
|
*/
|
|
38
|
-
ai?:
|
|
39
|
-
/**
|
|
40
|
-
* Custom AI service client.
|
|
41
|
-
*/
|
|
42
|
-
client?: AiServiceClient;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Edge AI service at specified endpoint.
|
|
46
|
-
*/
|
|
47
|
-
endpoint?: AiServiceEdgeClientOptions['endpoint'];
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Predefined AI service configuration.
|
|
51
|
-
*/
|
|
52
|
-
// TODO(burdon): 'dev' and 'edge' are redundant with providing an endpoint.
|
|
53
|
-
provider?: AiServiceProvider;
|
|
54
|
-
}>;
|
|
35
|
+
ai?: any;
|
|
55
36
|
|
|
56
37
|
/**
|
|
57
38
|
* Credentials service configuration.
|
|
@@ -84,7 +65,7 @@ export type TestServiceOptions = {
|
|
|
84
65
|
*/
|
|
85
66
|
logging?: {
|
|
86
67
|
enabled?: boolean;
|
|
87
|
-
logger?: Context.Tag.Service<
|
|
68
|
+
logger?: Context.Tag.Service<ComputeEventLogger>;
|
|
88
69
|
};
|
|
89
70
|
|
|
90
71
|
/**
|
|
@@ -95,10 +76,11 @@ export type TestServiceOptions = {
|
|
|
95
76
|
tracing?: {
|
|
96
77
|
service?: Context.Tag.Service<TracingService>;
|
|
97
78
|
};
|
|
98
|
-
|
|
99
|
-
toolResolver?: Context.Tag.Service<ToolResolverService>;
|
|
100
79
|
};
|
|
101
80
|
|
|
81
|
+
/**
|
|
82
|
+
* @deprecated
|
|
83
|
+
*/
|
|
102
84
|
export const createTestServices = ({
|
|
103
85
|
ai,
|
|
104
86
|
credentials,
|
|
@@ -107,60 +89,19 @@ export const createTestServices = ({
|
|
|
107
89
|
queues,
|
|
108
90
|
space,
|
|
109
91
|
tracing,
|
|
110
|
-
toolResolver,
|
|
111
92
|
}: TestServiceOptions = {}): ServiceContainer => {
|
|
112
|
-
assertArgument(!(!!space && (!!db || !!queues)), 'space can be provided only if db and queues are not');
|
|
93
|
+
assertArgument(!(!!space && (!!db || !!queues)), 'space', 'space can be provided only if db and queues are not');
|
|
113
94
|
|
|
114
95
|
return new ServiceContainer().setServices({
|
|
115
|
-
ai: createAiService(ai),
|
|
96
|
+
// ai: createAiService(ai),
|
|
116
97
|
credentials: createCredentialsService(credentials),
|
|
117
98
|
database: space || db ? DatabaseService.make(space?.db || db!) : undefined,
|
|
118
|
-
eventLogger: logging?.logger ?? logging?.enabled ? consoleLogger : noopLogger,
|
|
99
|
+
eventLogger: (logging?.logger ?? logging?.enabled) ? consoleLogger : noopLogger,
|
|
119
100
|
queues: space || queues ? QueueService.make(space?.queues || queues!, undefined) : undefined,
|
|
120
101
|
tracing: tracing?.service,
|
|
121
|
-
toolResolver: toolResolver ?? ToolResolverService.make(new ToolRegistry([])),
|
|
122
102
|
});
|
|
123
103
|
};
|
|
124
104
|
|
|
125
|
-
// TODO(burdon): Enable model configuration.
|
|
126
|
-
const createAiService = (ai: TestServiceOptions['ai']): Context.Tag.Service<AiService> | undefined => {
|
|
127
|
-
if (ai?.client != null) {
|
|
128
|
-
return AiService.make(ai.client);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (ai?.endpoint != null) {
|
|
132
|
-
return AiService.make(new EdgeAiServiceClient({ endpoint: ai.endpoint }));
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
switch (ai?.provider) {
|
|
136
|
-
case 'dev':
|
|
137
|
-
return AiService.make(
|
|
138
|
-
new EdgeAiServiceClient({
|
|
139
|
-
endpoint: AI_SERVICE_ENDPOINT.LOCAL,
|
|
140
|
-
defaultGenerationOptions: {
|
|
141
|
-
model: '@anthropic/claude-3-5-sonnet-20241022',
|
|
142
|
-
},
|
|
143
|
-
}),
|
|
144
|
-
);
|
|
145
|
-
|
|
146
|
-
case 'edge':
|
|
147
|
-
return AiService.make(
|
|
148
|
-
new EdgeAiServiceClient({
|
|
149
|
-
endpoint: AI_SERVICE_ENDPOINT.REMOTE,
|
|
150
|
-
defaultGenerationOptions: {
|
|
151
|
-
model: '@anthropic/claude-3-5-sonnet-20241022',
|
|
152
|
-
},
|
|
153
|
-
}),
|
|
154
|
-
);
|
|
155
|
-
|
|
156
|
-
case 'ollama':
|
|
157
|
-
return AiService.make(createTestAiServiceClient());
|
|
158
|
-
|
|
159
|
-
case 'lmstudio':
|
|
160
|
-
throw new Error('LMStudio is not supported');
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
|
|
164
105
|
const createCredentialsService = (
|
|
165
106
|
credentials: TestServiceOptions['credentials'] | undefined,
|
|
166
107
|
): Context.Tag.Service<CredentialsService> | undefined => {
|
package/src/trace.ts
CHANGED
|
@@ -4,12 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
import { Schema } from 'effect';
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { type Ref, Type } from '@dxos/echo';
|
|
8
8
|
import { Queue } from '@dxos/echo-db';
|
|
9
9
|
import { ObjectId } from '@dxos/echo-schema';
|
|
10
10
|
import { log } from '@dxos/log';
|
|
11
11
|
|
|
12
|
-
import { FunctionTrigger
|
|
12
|
+
import { FunctionTrigger } from './types';
|
|
13
13
|
|
|
14
14
|
export enum InvocationOutcome {
|
|
15
15
|
SUCCESS = 'success',
|
|
@@ -24,7 +24,7 @@ export enum InvocationTraceEventType {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export const TraceEventException = Schema.Struct({
|
|
27
|
-
|
|
27
|
+
timestamp: Schema.Number,
|
|
28
28
|
message: Schema.String,
|
|
29
29
|
name: Schema.String,
|
|
30
30
|
stack: Schema.optional(Schema.String),
|
|
@@ -44,7 +44,7 @@ export const InvocationTraceStartEvent = Schema.Struct({
|
|
|
44
44
|
/**
|
|
45
45
|
* Event generation time.
|
|
46
46
|
*/
|
|
47
|
-
|
|
47
|
+
timestamp: Schema.Number,
|
|
48
48
|
/**
|
|
49
49
|
* Data passed to function / workflow as an argument.
|
|
50
50
|
*/
|
|
@@ -53,11 +53,11 @@ export const InvocationTraceStartEvent = Schema.Struct({
|
|
|
53
53
|
/**
|
|
54
54
|
* Queue for function/workflow invocation events.
|
|
55
55
|
*/
|
|
56
|
-
invocationTraceQueue: Type.Ref(Queue),
|
|
56
|
+
invocationTraceQueue: Schema.optional(Type.Ref(Queue)),
|
|
57
57
|
/**
|
|
58
58
|
* DXN of the invoked function/workflow.
|
|
59
59
|
*/
|
|
60
|
-
invocationTarget: Type.Ref(Type.Expando),
|
|
60
|
+
invocationTarget: Schema.optional(Type.Ref(Type.Expando)),
|
|
61
61
|
/**
|
|
62
62
|
* Present for automatic invocations.
|
|
63
63
|
*/
|
|
@@ -80,7 +80,7 @@ export const InvocationTraceEndEvent = Schema.Struct({
|
|
|
80
80
|
* Event generation time.
|
|
81
81
|
*/
|
|
82
82
|
// TODO(burdon): Remove ms suffix.
|
|
83
|
-
|
|
83
|
+
timestamp: Schema.Number,
|
|
84
84
|
outcome: Schema.Enums(InvocationOutcome),
|
|
85
85
|
exception: Schema.optional(TraceEventException),
|
|
86
86
|
}).pipe(Type.Obj({ typename: 'dxos.org/type/InvocationTraceEnd', version: '0.1.0' }));
|
|
@@ -90,7 +90,7 @@ export type InvocationTraceEndEvent = Schema.Schema.Type<typeof InvocationTraceE
|
|
|
90
90
|
export type InvocationTraceEvent = InvocationTraceStartEvent | InvocationTraceEndEvent;
|
|
91
91
|
|
|
92
92
|
export const TraceEventLog = Schema.Struct({
|
|
93
|
-
|
|
93
|
+
timestamp: Schema.Number,
|
|
94
94
|
level: Schema.String,
|
|
95
95
|
message: Schema.String,
|
|
96
96
|
context: Schema.optional(Schema.Object),
|
|
@@ -101,10 +101,8 @@ export const TraceEvent = Schema.Struct({
|
|
|
101
101
|
// TODO(burdon): Need enum/numeric result (not string).
|
|
102
102
|
outcome: Schema.String,
|
|
103
103
|
truncated: Schema.Boolean,
|
|
104
|
-
/**
|
|
105
|
-
|
|
106
|
-
*/
|
|
107
|
-
ingestionTimestampMs: Schema.Number,
|
|
104
|
+
/** Time when the event was persisted. */
|
|
105
|
+
ingestionTimestamp: Schema.Number,
|
|
108
106
|
logs: Schema.Array(TraceEventLog),
|
|
109
107
|
exceptions: Schema.Array(TraceEventException),
|
|
110
108
|
}).pipe(Type.Obj({ typename: 'dxos.org/type/TraceEvent', version: '0.1.0' }));
|
|
@@ -117,13 +115,13 @@ export type TraceEvent = Schema.Schema.Type<typeof TraceEvent>;
|
|
|
117
115
|
*/
|
|
118
116
|
export type InvocationSpan = {
|
|
119
117
|
id: string;
|
|
120
|
-
|
|
118
|
+
timestamp: number;
|
|
119
|
+
duration: number;
|
|
121
120
|
outcome: InvocationOutcome;
|
|
122
121
|
input: object;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
trigger?: Ref.Ref<FunctionTriggerType>;
|
|
122
|
+
invocationTraceQueue?: Ref.Ref<Queue>;
|
|
123
|
+
invocationTarget?: Ref.Ref<Type.Expando>;
|
|
124
|
+
trigger?: Ref.Ref<FunctionTrigger>;
|
|
127
125
|
exception?: TraceEventException;
|
|
128
126
|
};
|
|
129
127
|
|
|
@@ -165,8 +163,8 @@ export const createInvocationSpans = (items?: InvocationTraceEvent[]): Invocatio
|
|
|
165
163
|
|
|
166
164
|
result.push({
|
|
167
165
|
id: invocationId,
|
|
168
|
-
|
|
169
|
-
|
|
166
|
+
timestamp: start.timestamp,
|
|
167
|
+
duration: isInProgress ? now - start.timestamp : end!.timestamp - start.timestamp,
|
|
170
168
|
outcome: end?.outcome ?? InvocationOutcome.PENDING,
|
|
171
169
|
exception: end?.exception,
|
|
172
170
|
input: start.input,
|