@dxos/functions 0.8.4-main.b97322e → 0.8.4-main.dedc0f3
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-ANP3DFCO.mjs +623 -0
- package/dist/lib/browser/chunk-ANP3DFCO.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 +892 -130
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +77 -39
- 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-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-MPKVY7ZR.mjs +625 -0
- package/dist/lib/node-esm/chunk-MPKVY7ZR.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 +892 -130
- 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 +77 -39
- 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 +77 -8
- 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 +4 -1
- package/dist/types/src/executor/executor.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +40 -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 +16 -3
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/database.d.ts +75 -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/index.d.ts +2 -1
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/local-function-execution.d.ts +25 -0
- package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
- package/dist/types/src/services/queues.d.ts +21 -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 +5 -5
- package/dist/types/src/services/service-container.d.ts.map +1 -1
- package/dist/types/src/services/service-registry.d.ts +1 -1
- package/dist/types/src/services/service-registry.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +37 -5
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +7 -2
- package/dist/types/src/testing/layer.d.ts.map +1 -1
- 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 +6 -17
- 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 +75 -0
- package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +2 -0
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-state-store.d.ts +27 -0
- package/dist/types/src/triggers/trigger-state-store.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +49 -249
- 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 +39 -34
- 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 +8 -0
- package/src/examples/fib.ts +30 -0
- package/src/examples/index.ts +7 -0
- package/src/examples/reply.ts +18 -0
- package/src/examples/sleep.ts +22 -0
- package/src/executor/executor.ts +9 -9
- package/src/handler.ts +99 -18
- package/src/index.ts +3 -3
- package/src/schema.ts +11 -0
- package/src/services/credentials.ts +79 -3
- package/src/services/database.ts +118 -18
- package/src/services/event-logger.ts +68 -37
- package/src/services/index.ts +2 -1
- package/src/services/local-function-execution.ts +114 -0
- package/src/services/queues.ts +37 -10
- package/src/services/remote-function-execution-service.ts +46 -0
- package/src/services/service-container.ts +11 -10
- package/src/services/service-registry.ts +5 -2
- package/src/services/tracing.ts +105 -7
- package/src/testing/layer.ts +83 -3
- package/src/testing/logger.ts +4 -4
- package/src/testing/persist-database.test.ts +87 -0
- package/src/testing/services.ts +10 -63
- 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 +652 -0
- package/src/triggers/trigger-dispatcher.ts +512 -0
- package/src/triggers/trigger-state-store.ts +60 -0
- package/src/types.ts +22 -33
- package/src/url.ts +13 -10
- package/dist/lib/browser/chunk-3NGCSUEW.mjs +0 -328
- package/dist/lib/browser/chunk-3NGCSUEW.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-FJ2MU7TL.mjs +0 -330
- package/dist/lib/node-esm/chunk-FJ2MU7TL.mjs.map +0 -7
- 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/function-call-service.ts +0 -64
|
@@ -9,9 +9,9 @@ import { entries } from '@dxos/util';
|
|
|
9
9
|
|
|
10
10
|
import { ConfiguredCredentialsService, CredentialsService } from './credentials';
|
|
11
11
|
import { DatabaseService } from './database';
|
|
12
|
-
import {
|
|
13
|
-
import { FunctionCallService } from './function-call-service';
|
|
12
|
+
import { ComputeEventLogger } from './event-logger';
|
|
14
13
|
import { QueueService } from './queues';
|
|
14
|
+
import { RemoteFunctionExecutionService } from './remote-function-execution-service';
|
|
15
15
|
import { TracingService } from './tracing';
|
|
16
16
|
|
|
17
17
|
// TODO(dmaretskyi): Refactor this module to only rely on tags and not the human-assigned names.
|
|
@@ -20,11 +20,11 @@ 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
|
-
eventLogger:
|
|
27
|
-
functionCallService:
|
|
26
|
+
eventLogger: ComputeEventLogger,
|
|
27
|
+
functionCallService: RemoteFunctionExecutionService,
|
|
28
28
|
queues: QueueService,
|
|
29
29
|
tracing: TracingService,
|
|
30
30
|
} as const satisfies Record<string, Context.TagClass<any, string, any>>;
|
|
@@ -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(),
|
|
@@ -102,10 +103,10 @@ export class ServiceContainer {
|
|
|
102
103
|
const queues =
|
|
103
104
|
this._services.queues != null ? Layer.succeed(QueueService, this._services.queues) : QueueService.notAvailable;
|
|
104
105
|
const tracing = Layer.succeed(TracingService, this._services.tracing ?? TracingService.noop);
|
|
105
|
-
const eventLogger = Layer.succeed(
|
|
106
|
+
const eventLogger = Layer.succeed(ComputeEventLogger, this._services.eventLogger ?? ComputeEventLogger.noop);
|
|
106
107
|
const functionCallService = Layer.succeed(
|
|
107
|
-
|
|
108
|
-
this._services.functionCallService ??
|
|
108
|
+
RemoteFunctionExecutionService,
|
|
109
|
+
this._services.functionCallService ?? RemoteFunctionExecutionService.mock(),
|
|
109
110
|
);
|
|
110
111
|
|
|
111
112
|
return Layer.mergeAll(ai, credentials, database, queues, tracing, eventLogger, functionCallService);
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Context, Effect,
|
|
5
|
+
import { Context, Effect, type Option, flow } from 'effect';
|
|
6
6
|
|
|
7
7
|
import { ServiceNotAvailableError } from '../errors';
|
|
8
8
|
|
|
@@ -12,7 +12,10 @@ export namespace ServiceRegistry {
|
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export class ServiceRegistry extends Context.Tag('ServiceRegistry')<
|
|
15
|
+
export class ServiceRegistry extends Context.Tag('@dxos/functions/ServiceRegistry')<
|
|
16
|
+
ServiceRegistry,
|
|
17
|
+
ServiceRegistry.Service
|
|
18
|
+
>() {
|
|
16
19
|
/**
|
|
17
20
|
* Resolves the service from the registry.
|
|
18
21
|
* @param tag Service tag to resolve.
|
package/src/services/tracing.ts
CHANGED
|
@@ -2,41 +2,139 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Context, Effect } from 'effect';
|
|
5
|
+
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)
|
|
13
16
|
* to emit an execution trace as a series of structured ECHO objects.
|
|
14
17
|
*/
|
|
15
|
-
export class TracingService extends Context.Tag('TracingService')<
|
|
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: () => ({}) };
|
|
34
|
+
|
|
35
|
+
static layerNoop = Layer.succeed(TracingService, TracingService.noop);
|
|
26
36
|
|
|
27
37
|
static console: Context.Tag.Service<TracingService> = {
|
|
28
38
|
write: (event) => {
|
|
29
39
|
// eslint-disable-next-line no-console
|
|
30
40
|
console.log(event);
|
|
31
41
|
},
|
|
42
|
+
getTraceContext: () => ({}),
|
|
32
43
|
};
|
|
33
44
|
|
|
45
|
+
static layerConsole = Layer.succeed(TracingService, TracingService.console);
|
|
46
|
+
|
|
47
|
+
static layerLogInfo = () =>
|
|
48
|
+
Layer.succeed(TracingService, {
|
|
49
|
+
write: (event) => {
|
|
50
|
+
if (Obj.instanceOf(AgentStatus, event)) {
|
|
51
|
+
log.info('status', { message: event.message });
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
getTraceContext: () => ({}),
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Creates a TracingService layer that emits events to the parent tracing service.
|
|
59
|
+
*/
|
|
60
|
+
static layerSubframe = (mapContext: (currentContext: TracingService.TraceContext) => TracingService.TraceContext) =>
|
|
61
|
+
Layer.effect(
|
|
62
|
+
TracingService,
|
|
63
|
+
Effect.gen(function* () {
|
|
64
|
+
const tracing = yield* TracingService;
|
|
65
|
+
const context = mapContext(tracing.getTraceContext());
|
|
66
|
+
return {
|
|
67
|
+
write: (event) => tracing.write(event),
|
|
68
|
+
getTraceContext: () => context,
|
|
69
|
+
};
|
|
70
|
+
}),
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
static layerQueue = (queue: Queue) =>
|
|
74
|
+
Layer.effect(
|
|
75
|
+
TracingService,
|
|
76
|
+
Effect.gen(function* () {
|
|
77
|
+
// TODO(dmaretskyi): Batching.
|
|
78
|
+
return {
|
|
79
|
+
write: (event) => queue.append([event]),
|
|
80
|
+
getTraceContext: () => ({
|
|
81
|
+
debugInfo: {
|
|
82
|
+
queue: queue.dxn.toString(),
|
|
83
|
+
},
|
|
84
|
+
}),
|
|
85
|
+
};
|
|
86
|
+
}),
|
|
87
|
+
);
|
|
88
|
+
|
|
34
89
|
/**
|
|
35
90
|
* Emit the current human-readable execution status.
|
|
36
91
|
*/
|
|
37
|
-
static emitStatus: (data: Obj.MakeProps<typeof AgentStatus>) => Effect.Effect<
|
|
92
|
+
static emitStatus: (data: Obj.MakeProps<typeof AgentStatus>) => Effect.Effect<void, never, TracingService> =
|
|
38
93
|
Effect.fnUntraced(function* (data) {
|
|
39
94
|
const tracing = yield* TracingService;
|
|
40
|
-
tracing.write(
|
|
95
|
+
tracing.write(
|
|
96
|
+
Obj.make(AgentStatus, {
|
|
97
|
+
parentMessage: tracing.getTraceContext().parentMessage,
|
|
98
|
+
toolCallId: tracing.getTraceContext().toolCallId,
|
|
99
|
+
...data,
|
|
100
|
+
}),
|
|
101
|
+
);
|
|
41
102
|
});
|
|
103
|
+
|
|
104
|
+
static emitConverationMessage: (
|
|
105
|
+
data: Obj.MakeProps<typeof DataType.Message>,
|
|
106
|
+
) => Effect.Effect<void, never, TracingService> = Effect.fnUntraced(function* (data) {
|
|
107
|
+
const tracing = yield* TracingService;
|
|
108
|
+
tracing.write(
|
|
109
|
+
Obj.make(DataType.Message, {
|
|
110
|
+
parentMessage: tracing.getTraceContext().parentMessage,
|
|
111
|
+
...data,
|
|
112
|
+
properties: {
|
|
113
|
+
[MESSAGE_PROPERTY_TOOL_CALL_ID]: tracing.getTraceContext().toolCallId,
|
|
114
|
+
...data.properties,
|
|
115
|
+
},
|
|
116
|
+
}),
|
|
117
|
+
);
|
|
118
|
+
});
|
|
42
119
|
}
|
|
120
|
+
|
|
121
|
+
export namespace TracingService {
|
|
122
|
+
export interface TraceContext {
|
|
123
|
+
/**
|
|
124
|
+
* If this thread sprung from a tool call, this is the ID of the message containing the tool call.
|
|
125
|
+
*/
|
|
126
|
+
parentMessage?: ObjectId;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* If the current thread is a byproduct of a tool call, this is the ID of the tool call.
|
|
130
|
+
*/
|
|
131
|
+
toolCallId?: string;
|
|
132
|
+
|
|
133
|
+
debugInfo?: unknown;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Goes into {@link DataType.Message['properties']}
|
|
139
|
+
*/
|
|
140
|
+
export const MESSAGE_PROPERTY_TOOL_CALL_ID = 'toolCallId' as const;
|
package/src/testing/layer.ts
CHANGED
|
@@ -2,27 +2,107 @@
|
|
|
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
|
+
storagePath?: string;
|
|
29
|
+
onInit?: () => Effect.Effect<void, never, DatabaseService | QueueService>;
|
|
19
30
|
};
|
|
20
31
|
|
|
21
|
-
export const TestDatabaseLayer = ({ indexing, types }: TestDatabaseOptions = {}) =>
|
|
32
|
+
export const TestDatabaseLayer = ({ indexing, types, storagePath, onInit }: TestDatabaseOptions = {}) =>
|
|
22
33
|
Layer.scopedContext(
|
|
23
34
|
Effect.gen(function* () {
|
|
24
35
|
const builder = yield* testBuilder;
|
|
25
|
-
|
|
36
|
+
|
|
37
|
+
let kv: LevelDB | undefined;
|
|
38
|
+
if (storagePath) {
|
|
39
|
+
kv = createTestLevel(storagePath);
|
|
40
|
+
yield* Effect.promise(() => kv!.open());
|
|
41
|
+
// const keyCount = yield* Effect.promise(async () => (await kv!.iterator({ values: false }).all()).length);
|
|
42
|
+
// log.info('opened test db', { storagePath, keyCount });
|
|
43
|
+
}
|
|
44
|
+
const peer = yield* Effect.promise(() => builder.createPeer({ indexing, types, kv }));
|
|
45
|
+
|
|
46
|
+
let db: EchoDatabaseImpl | undefined;
|
|
47
|
+
let queues: QueueFactory | undefined;
|
|
48
|
+
|
|
49
|
+
if (storagePath) {
|
|
50
|
+
const testMetadata = yield* Effect.promise(async () => {
|
|
51
|
+
try {
|
|
52
|
+
return await kv!.get('test-metadata', { valueEncoding: 'json' });
|
|
53
|
+
} catch (e) {
|
|
54
|
+
if ((e as any).code === 'LEVEL_NOT_FOUND') {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
throw e;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
log('starting persistant test db', { storagePath, testMetadata });
|
|
61
|
+
if (!testMetadata) {
|
|
62
|
+
const key = PublicKey.random();
|
|
63
|
+
db = yield* Effect.promise(() => peer.createDatabase(key));
|
|
64
|
+
queues = peer.client.constructQueueFactory(db.spaceId);
|
|
65
|
+
|
|
66
|
+
yield* Effect.promise(() =>
|
|
67
|
+
kv!.put('test-metadata', { key: key.toHex(), rootUrl: db!.rootUrl }, { valueEncoding: 'json' }),
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
if (onInit) {
|
|
71
|
+
yield* onInit().pipe(
|
|
72
|
+
Effect.provideService(DatabaseService, DatabaseService.make(db)),
|
|
73
|
+
Effect.provideService(QueueService, QueueService.make(queues, undefined)),
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
} else {
|
|
77
|
+
const key = PublicKey.from((testMetadata as any).key);
|
|
78
|
+
const rootUrl = (testMetadata as any).rootUrl;
|
|
79
|
+
db = yield* Effect.promise(() => peer.openDatabase(key, rootUrl));
|
|
80
|
+
queues = peer.client.constructQueueFactory(db.spaceId);
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
db = yield* Effect.promise(() => peer.createDatabase());
|
|
84
|
+
queues = peer.client.constructQueueFactory(db.spaceId);
|
|
85
|
+
if (onInit) {
|
|
86
|
+
yield* onInit().pipe(
|
|
87
|
+
Effect.provideService(DatabaseService, DatabaseService.make(db)),
|
|
88
|
+
Effect.provideService(QueueService, QueueService.make(queues, undefined)),
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
yield* Effect.addFinalizer(() =>
|
|
94
|
+
Effect.promise(async () => {
|
|
95
|
+
if (kv) {
|
|
96
|
+
// {
|
|
97
|
+
// const keyCount = (await kv.iterator({ values: false }).all()).length;
|
|
98
|
+
// log.info('closing persistant test db', { storagePath, keyCount });
|
|
99
|
+
// }
|
|
100
|
+
|
|
101
|
+
await kv.close();
|
|
102
|
+
}
|
|
103
|
+
}),
|
|
104
|
+
);
|
|
105
|
+
|
|
26
106
|
return Context.mergeAll(
|
|
27
107
|
Context.make(DatabaseService, DatabaseService.make(db)),
|
|
28
108
|
Context.make(QueueService, QueueService.make(queues, undefined)),
|
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* ({ expect: _ }) {
|
|
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* ({ expect: _ }) {
|
|
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* ({ expect: _ }) {
|
|
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,24 +4,23 @@
|
|
|
4
4
|
|
|
5
5
|
import { type Context } from 'effect';
|
|
6
6
|
|
|
7
|
-
import { AiService, type AiServiceClient, type AiServiceEdgeClientOptions, EdgeAiServiceClient } 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 {
|
|
12
|
+
type ComputeEventLogger,
|
|
15
13
|
ConfiguredCredentialsService,
|
|
16
14
|
type CredentialsService,
|
|
17
15
|
DatabaseService,
|
|
18
|
-
type EventLogger,
|
|
19
16
|
QueueService,
|
|
20
17
|
ServiceContainer,
|
|
21
18
|
type ServiceCredential,
|
|
22
19
|
type TracingService,
|
|
23
20
|
} from '../services';
|
|
24
21
|
|
|
22
|
+
import { consoleLogger, noopLogger } from './logger';
|
|
23
|
+
|
|
25
24
|
// TODO(burdon): Factor out.
|
|
26
25
|
export type OneOf<T> = {
|
|
27
26
|
[K in keyof T]: { [P in K]: T[P] } & { [P in Exclude<keyof T, K>]?: never };
|
|
@@ -33,23 +32,7 @@ export type TestServiceOptions = {
|
|
|
33
32
|
/**
|
|
34
33
|
* AI service configuration.
|
|
35
34
|
*/
|
|
36
|
-
ai?:
|
|
37
|
-
/**
|
|
38
|
-
* Custom AI service client.
|
|
39
|
-
*/
|
|
40
|
-
client?: AiServiceClient;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Edge AI service at specified endpoint.
|
|
44
|
-
*/
|
|
45
|
-
endpoint?: AiServiceEdgeClientOptions['endpoint'];
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Predefined AI service configuration.
|
|
49
|
-
*/
|
|
50
|
-
// TODO(burdon): 'dev' and 'edge' are redundant with providing an endpoint.
|
|
51
|
-
provider?: AiServiceProvider;
|
|
52
|
-
}>;
|
|
35
|
+
ai?: any;
|
|
53
36
|
|
|
54
37
|
/**
|
|
55
38
|
* Credentials service configuration.
|
|
@@ -82,7 +65,7 @@ export type TestServiceOptions = {
|
|
|
82
65
|
*/
|
|
83
66
|
logging?: {
|
|
84
67
|
enabled?: boolean;
|
|
85
|
-
logger?: Context.Tag.Service<
|
|
68
|
+
logger?: Context.Tag.Service<ComputeEventLogger>;
|
|
86
69
|
};
|
|
87
70
|
|
|
88
71
|
/**
|
|
@@ -95,6 +78,9 @@ export type TestServiceOptions = {
|
|
|
95
78
|
};
|
|
96
79
|
};
|
|
97
80
|
|
|
81
|
+
/**
|
|
82
|
+
* @deprecated
|
|
83
|
+
*/
|
|
98
84
|
export const createTestServices = ({
|
|
99
85
|
ai,
|
|
100
86
|
credentials,
|
|
@@ -104,10 +90,10 @@ export const createTestServices = ({
|
|
|
104
90
|
space,
|
|
105
91
|
tracing,
|
|
106
92
|
}: TestServiceOptions = {}): ServiceContainer => {
|
|
107
|
-
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');
|
|
108
94
|
|
|
109
95
|
return new ServiceContainer().setServices({
|
|
110
|
-
ai: createAiService(ai),
|
|
96
|
+
// ai: createAiService(ai),
|
|
111
97
|
credentials: createCredentialsService(credentials),
|
|
112
98
|
database: space || db ? DatabaseService.make(space?.db || db!) : undefined,
|
|
113
99
|
eventLogger: (logging?.logger ?? logging?.enabled) ? consoleLogger : noopLogger,
|
|
@@ -116,45 +102,6 @@ export const createTestServices = ({
|
|
|
116
102
|
});
|
|
117
103
|
};
|
|
118
104
|
|
|
119
|
-
// TODO(burdon): Enable model configuration.
|
|
120
|
-
const createAiService = (ai: TestServiceOptions['ai']): Context.Tag.Service<AiService> | undefined => {
|
|
121
|
-
if (ai?.client != null) {
|
|
122
|
-
return AiService.make(ai.client);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (ai?.endpoint != null) {
|
|
126
|
-
return AiService.make(new EdgeAiServiceClient({ endpoint: ai.endpoint }));
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
switch (ai?.provider) {
|
|
130
|
-
case 'dev':
|
|
131
|
-
return AiService.make(
|
|
132
|
-
new EdgeAiServiceClient({
|
|
133
|
-
endpoint: AI_SERVICE_ENDPOINT.LOCAL,
|
|
134
|
-
defaultGenerationOptions: {
|
|
135
|
-
model: '@anthropic/claude-3-5-sonnet-20241022',
|
|
136
|
-
},
|
|
137
|
-
}),
|
|
138
|
-
);
|
|
139
|
-
|
|
140
|
-
case 'edge':
|
|
141
|
-
return AiService.make(
|
|
142
|
-
new EdgeAiServiceClient({
|
|
143
|
-
endpoint: AI_SERVICE_ENDPOINT.REMOTE,
|
|
144
|
-
defaultGenerationOptions: {
|
|
145
|
-
model: '@anthropic/claude-3-5-sonnet-20241022',
|
|
146
|
-
},
|
|
147
|
-
}),
|
|
148
|
-
);
|
|
149
|
-
|
|
150
|
-
case 'ollama':
|
|
151
|
-
return AiService.make(createTestAiServiceClient());
|
|
152
|
-
|
|
153
|
-
case 'lmstudio':
|
|
154
|
-
throw new Error('LMStudio is not supported');
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
|
|
158
105
|
const createCredentialsService = (
|
|
159
106
|
credentials: TestServiceOptions['credentials'] | undefined,
|
|
160
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,
|