@dxos/functions 0.8.4-main.3f58842 → 0.8.4-main.5ea62a8
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 +54 -38
- package/dist/lib/browser/bundler/index.mjs.map +3 -3
- package/dist/lib/browser/chunk-7NQ77AIQ.mjs +618 -0
- package/dist/lib/browser/chunk-7NQ77AIQ.mjs.map +7 -0
- package/dist/lib/browser/edge/index.mjs +20 -8
- package/dist/lib/browser/edge/index.mjs.map +3 -3
- package/dist/lib/browser/index.mjs +149 -87
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +69 -38
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/bundler/index.mjs +54 -38
- package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
- package/dist/lib/node-esm/chunk-KCGC6QQT.mjs +620 -0
- package/dist/lib/node-esm/chunk-KCGC6QQT.mjs.map +7 -0
- package/dist/lib/node-esm/edge/index.mjs +20 -8
- package/dist/lib/node-esm/edge/index.mjs.map +3 -3
- package/dist/lib/node-esm/index.mjs +149 -87
- 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 +69 -38
- 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 +10 -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/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 +10 -7
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/schema.d.ts +7 -2
- 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 +12 -0
- package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
- package/dist/types/src/services/queues.d.ts +20 -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 +36 -5
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +6 -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 +34 -8
- package/dist/types/src/trace.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +141 -224
- 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 +37 -31
- package/src/bundler/bundler.test.ts +8 -9
- package/src/bundler/bundler.ts +32 -33
- package/src/edge/functions.ts +8 -5
- package/src/examples/fib.ts +30 -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 +12 -10
- 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 +77 -0
- package/src/services/queues.ts +32 -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 +99 -7
- package/src/testing/layer.ts +69 -3
- package/src/testing/logger.ts +4 -4
- package/src/testing/persist-database.test.ts +87 -0
- package/src/testing/services.ts +9 -62
- package/src/trace.ts +5 -7
- package/src/types.ts +17 -25
- 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
package/src/services/tracing.ts
CHANGED
|
@@ -2,41 +2,133 @@
|
|
|
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
|
+
};
|
|
82
|
+
}),
|
|
83
|
+
);
|
|
84
|
+
|
|
34
85
|
/**
|
|
35
86
|
* Emit the current human-readable execution status.
|
|
36
87
|
*/
|
|
37
|
-
static emitStatus: (data: Obj.MakeProps<typeof AgentStatus>) => Effect.Effect<
|
|
88
|
+
static emitStatus: (data: Obj.MakeProps<typeof AgentStatus>) => Effect.Effect<void, never, TracingService> =
|
|
38
89
|
Effect.fnUntraced(function* (data) {
|
|
39
90
|
const tracing = yield* TracingService;
|
|
40
|
-
tracing.write(
|
|
91
|
+
tracing.write(
|
|
92
|
+
Obj.make(AgentStatus, {
|
|
93
|
+
parentMessage: tracing.getTraceContext().parentMessage,
|
|
94
|
+
toolCallId: tracing.getTraceContext().toolCallId,
|
|
95
|
+
...data,
|
|
96
|
+
}),
|
|
97
|
+
);
|
|
41
98
|
});
|
|
99
|
+
|
|
100
|
+
static emitConverationMessage: (
|
|
101
|
+
data: Obj.MakeProps<typeof DataType.Message>,
|
|
102
|
+
) => Effect.Effect<void, never, TracingService> = Effect.fnUntraced(function* (data) {
|
|
103
|
+
const tracing = yield* TracingService;
|
|
104
|
+
tracing.write(
|
|
105
|
+
Obj.make(DataType.Message, {
|
|
106
|
+
parentMessage: tracing.getTraceContext().parentMessage,
|
|
107
|
+
...data,
|
|
108
|
+
properties: {
|
|
109
|
+
[MESSAGE_PROPERTY_TOOL_CALL_ID]: tracing.getTraceContext().toolCallId,
|
|
110
|
+
...data.properties,
|
|
111
|
+
},
|
|
112
|
+
}),
|
|
113
|
+
);
|
|
114
|
+
});
|
|
42
115
|
}
|
|
116
|
+
|
|
117
|
+
export namespace TracingService {
|
|
118
|
+
export interface TraceContext {
|
|
119
|
+
/**
|
|
120
|
+
* If this thread sprung from a tool call, this is the ID of the message containing the tool call.
|
|
121
|
+
*/
|
|
122
|
+
parentMessage?: ObjectId;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* If the current thread is a byproduct of a tool call, this is the ID of the tool call.
|
|
126
|
+
*/
|
|
127
|
+
toolCallId?: string;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Goes into {@link DataType.Message['properties']}
|
|
133
|
+
*/
|
|
134
|
+
export const MESSAGE_PROPERTY_TOOL_CALL_ID = 'toolCallId' as const;
|
package/src/testing/layer.ts
CHANGED
|
@@ -2,27 +2,93 @@
|
|
|
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;
|
|
19
29
|
};
|
|
20
30
|
|
|
21
|
-
export const TestDatabaseLayer = ({ indexing, types }: TestDatabaseOptions = {}) =>
|
|
31
|
+
export const TestDatabaseLayer = ({ indexing, types, storagePath }: TestDatabaseOptions = {}) =>
|
|
22
32
|
Layer.scopedContext(
|
|
23
33
|
Effect.gen(function* () {
|
|
24
34
|
const builder = yield* testBuilder;
|
|
25
|
-
|
|
35
|
+
|
|
36
|
+
let kv: LevelDB | undefined;
|
|
37
|
+
if (storagePath) {
|
|
38
|
+
kv = createTestLevel(storagePath);
|
|
39
|
+
yield* Effect.promise(() => kv!.open());
|
|
40
|
+
// const keyCount = yield* Effect.promise(async () => (await kv!.iterator({ values: false }).all()).length);
|
|
41
|
+
// log.info('opened test db', { storagePath, keyCount });
|
|
42
|
+
}
|
|
43
|
+
const peer = yield* Effect.promise(() => builder.createPeer({ indexing, types, kv }));
|
|
44
|
+
|
|
45
|
+
let db: EchoDatabaseImpl | undefined;
|
|
46
|
+
let queues: QueueFactory | undefined;
|
|
47
|
+
|
|
48
|
+
if (storagePath) {
|
|
49
|
+
const testMetadata = yield* Effect.promise(async () => {
|
|
50
|
+
try {
|
|
51
|
+
return await kv!.get('test-metadata', { valueEncoding: 'json' });
|
|
52
|
+
} catch (e) {
|
|
53
|
+
if ((e as any).code === 'LEVEL_NOT_FOUND') {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
throw e;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
log('starting persistant test db', { storagePath, testMetadata });
|
|
60
|
+
if (!testMetadata) {
|
|
61
|
+
const key = PublicKey.random();
|
|
62
|
+
db = yield* Effect.promise(() => peer.createDatabase(key));
|
|
63
|
+
queues = peer.client.constructQueueFactory(db.spaceId);
|
|
64
|
+
|
|
65
|
+
yield* Effect.promise(() =>
|
|
66
|
+
kv!.put('test-metadata', { key: key.toHex(), rootUrl: db!.rootUrl }, { valueEncoding: 'json' }),
|
|
67
|
+
);
|
|
68
|
+
} else {
|
|
69
|
+
const key = PublicKey.from((testMetadata as any).key);
|
|
70
|
+
const rootUrl = (testMetadata as any).rootUrl;
|
|
71
|
+
db = yield* Effect.promise(() => peer.openDatabase(key, rootUrl));
|
|
72
|
+
queues = peer.client.constructQueueFactory(db.spaceId);
|
|
73
|
+
}
|
|
74
|
+
} else {
|
|
75
|
+
db = yield* Effect.promise(() => peer.createDatabase());
|
|
76
|
+
queues = peer.client.constructQueueFactory(db.spaceId);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
yield* Effect.addFinalizer(() =>
|
|
80
|
+
Effect.promise(async () => {
|
|
81
|
+
if (kv) {
|
|
82
|
+
// {
|
|
83
|
+
// const keyCount = (await kv.iterator({ values: false }).all()).length;
|
|
84
|
+
// log.info('closing persistant test db', { storagePath, keyCount });
|
|
85
|
+
// }
|
|
86
|
+
|
|
87
|
+
await kv.close();
|
|
88
|
+
}
|
|
89
|
+
}),
|
|
90
|
+
);
|
|
91
|
+
|
|
26
92
|
return Context.mergeAll(
|
|
27
93
|
Context.make(DatabaseService, DatabaseService.make(db)),
|
|
28
94
|
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,
|
|
@@ -107,7 +93,7 @@ export const createTestServices = ({
|
|
|
107
93
|
assertArgument(!(!!space && (!!db || !!queues)), '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',
|
|
@@ -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' }));
|
|
@@ -123,7 +121,7 @@ export type InvocationSpan = {
|
|
|
123
121
|
durationMs: number;
|
|
124
122
|
invocationTraceQueue: Ref.Ref<Queue>;
|
|
125
123
|
invocationTarget: Ref.Ref<Type.Expando>;
|
|
126
|
-
trigger?: Ref.Ref<
|
|
124
|
+
trigger?: Ref.Ref<FunctionTrigger>;
|
|
127
125
|
exception?: TraceEventException;
|
|
128
126
|
};
|
|
129
127
|
|
package/src/types.ts
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
import { Schema, SchemaAST } from 'effect';
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { Type } from '@dxos/echo';
|
|
8
|
+
import { Expando, OptionsAnnotationId, RawObject, Ref } from '@dxos/echo-schema';
|
|
8
9
|
import { DXN } from '@dxos/keys';
|
|
9
10
|
|
|
10
11
|
import { FunctionType } from './schema';
|
|
@@ -14,13 +15,8 @@ import { FunctionType } from './schema';
|
|
|
14
15
|
* Every spec has a type field of type TriggerKind that we can use to understand which type we're working with.
|
|
15
16
|
* https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions
|
|
16
17
|
*/
|
|
17
|
-
export
|
|
18
|
-
|
|
19
|
-
Webhook = 'webhook',
|
|
20
|
-
Subscription = 'subscription',
|
|
21
|
-
Email = 'email',
|
|
22
|
-
Queue = 'queue',
|
|
23
|
-
}
|
|
18
|
+
export const TriggerKinds = ['timer', 'webhook', 'subscription', 'email', 'queue'] as const;
|
|
19
|
+
export type TriggerKind = (typeof TriggerKinds)[number];
|
|
24
20
|
|
|
25
21
|
const kindLiteralAnnotations = { title: 'Kind' };
|
|
26
22
|
|
|
@@ -28,7 +24,7 @@ const kindLiteralAnnotations = { title: 'Kind' };
|
|
|
28
24
|
* Cron timer.
|
|
29
25
|
*/
|
|
30
26
|
const TimerTriggerSchema = Schema.Struct({
|
|
31
|
-
kind: Schema.Literal(
|
|
27
|
+
kind: Schema.Literal('timer').annotations(kindLiteralAnnotations),
|
|
32
28
|
cron: Schema.String.annotations({
|
|
33
29
|
title: 'Cron',
|
|
34
30
|
[SchemaAST.ExamplesAnnotationId]: ['0 0 * * *'],
|
|
@@ -37,12 +33,12 @@ const TimerTriggerSchema = Schema.Struct({
|
|
|
37
33
|
export type TimerTrigger = Schema.Schema.Type<typeof TimerTriggerSchema>;
|
|
38
34
|
|
|
39
35
|
const EmailTriggerSchema = Schema.Struct({
|
|
40
|
-
kind: Schema.Literal(
|
|
36
|
+
kind: Schema.Literal('email').annotations(kindLiteralAnnotations),
|
|
41
37
|
}).pipe(Schema.mutable);
|
|
42
38
|
export type EmailTrigger = Schema.Schema.Type<typeof EmailTriggerSchema>;
|
|
43
39
|
|
|
44
40
|
const QueueTriggerSchema = Schema.Struct({
|
|
45
|
-
kind: Schema.Literal(
|
|
41
|
+
kind: Schema.Literal('queue').annotations(kindLiteralAnnotations),
|
|
46
42
|
queue: DXN.Schema,
|
|
47
43
|
}).pipe(Schema.mutable);
|
|
48
44
|
export type QueueTrigger = Schema.Schema.Type<typeof QueueTriggerSchema>;
|
|
@@ -51,7 +47,7 @@ export type QueueTrigger = Schema.Schema.Type<typeof QueueTriggerSchema>;
|
|
|
51
47
|
* Webhook.
|
|
52
48
|
*/
|
|
53
49
|
const WebhookTriggerSchema = Schema.Struct({
|
|
54
|
-
kind: Schema.Literal(
|
|
50
|
+
kind: Schema.Literal('webhook').annotations(kindLiteralAnnotations),
|
|
55
51
|
method: Schema.optional(
|
|
56
52
|
Schema.String.annotations({
|
|
57
53
|
title: 'Method',
|
|
@@ -76,7 +72,7 @@ const QuerySchema = Schema.Struct({
|
|
|
76
72
|
* Subscription.
|
|
77
73
|
*/
|
|
78
74
|
const SubscriptionTriggerSchema = Schema.Struct({
|
|
79
|
-
kind: Schema.Literal(
|
|
75
|
+
kind: Schema.Literal('subscription').annotations(kindLiteralAnnotations),
|
|
80
76
|
// TODO(burdon): Define query DSL (from ECHO). Reconcile with Table.Query.
|
|
81
77
|
filter: QuerySchema,
|
|
82
78
|
options: Schema.optional(
|
|
@@ -155,7 +151,7 @@ export type TimerTriggerOutput = Schema.Schema.Type<typeof TimerTriggerOutput>;
|
|
|
155
151
|
* Function is invoked with the `payload` passed as input data.
|
|
156
152
|
* The event that triggers the function is available in the function context.
|
|
157
153
|
*/
|
|
158
|
-
export const
|
|
154
|
+
export const FunctionTrigger = Schema.Struct({
|
|
159
155
|
/**
|
|
160
156
|
* Function or workflow to invoke.
|
|
161
157
|
*/
|
|
@@ -185,17 +181,13 @@ export const FunctionTriggerSchema = Schema.Struct({
|
|
|
185
181
|
* }
|
|
186
182
|
*/
|
|
187
183
|
input: Schema.optional(Schema.mutable(Schema.Record({ key: Schema.String, value: Schema.Any }))),
|
|
188
|
-
})
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
export class FunctionTrigger extends TypedObject({
|
|
196
|
-
typename: 'dxos.org/type/FunctionTrigger',
|
|
197
|
-
version: '0.2.0',
|
|
198
|
-
})(FunctionTriggerSchema.fields) {}
|
|
184
|
+
}).pipe(
|
|
185
|
+
Type.Obj({
|
|
186
|
+
typename: 'dxos.org/type/FunctionTrigger',
|
|
187
|
+
version: '0.2.0',
|
|
188
|
+
}),
|
|
189
|
+
);
|
|
190
|
+
export type FunctionTrigger = Schema.Schema.Type<typeof FunctionTrigger>;
|
|
199
191
|
|
|
200
192
|
// TODO(wittjosiah): Remove?
|
|
201
193
|
|
package/src/url.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { type ObjectMeta } from '@dxos/echo-schema';
|
|
|
6
6
|
import { type SpaceId } from '@dxos/keys';
|
|
7
7
|
|
|
8
8
|
// TODO: use URL scheme for source?
|
|
9
|
-
const FUNCTIONS_META_KEY = 'dxos.org/service/function';
|
|
9
|
+
export const FUNCTIONS_META_KEY = 'dxos.org/service/function';
|
|
10
10
|
|
|
11
11
|
export const FUNCTIONS_PRESET_META_KEY = 'dxos.org/service/function-preset';
|
|
12
12
|
|
|
@@ -14,32 +14,35 @@ const isSecure = (protocol: string) => {
|
|
|
14
14
|
return protocol === 'https:' || protocol === 'wss:';
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* NOTE: functionId is backend ID, not ECHO object id.
|
|
19
|
+
*/
|
|
20
|
+
export const getUserFunctionIdInMetadata = (meta: ObjectMeta) => {
|
|
18
21
|
return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;
|
|
19
22
|
};
|
|
20
23
|
|
|
21
|
-
|
|
24
|
+
/**
|
|
25
|
+
* NOTE: functionId is backend ID, not ECHO object id.
|
|
26
|
+
*/
|
|
27
|
+
export const setUserFunctionIdInMetadata = (meta: ObjectMeta, functionId: string) => {
|
|
22
28
|
const key = meta.keys.find((key) => key.source === FUNCTIONS_META_KEY);
|
|
23
29
|
if (key) {
|
|
24
|
-
if (key.id !==
|
|
30
|
+
if (key.id !== functionId) {
|
|
25
31
|
throw new Error('Metadata mismatch');
|
|
26
32
|
}
|
|
27
33
|
} else {
|
|
28
|
-
meta.keys.push({ source: FUNCTIONS_META_KEY, id:
|
|
34
|
+
meta.keys.push({ source: FUNCTIONS_META_KEY, id: functionId });
|
|
29
35
|
}
|
|
30
36
|
};
|
|
31
37
|
|
|
32
38
|
/**
|
|
33
39
|
* NOTE: functionId is backend ID, not ECHO object id.
|
|
34
40
|
*/
|
|
35
|
-
export const
|
|
36
|
-
|
|
37
|
-
export const getInvocationUrl = (functionUrl: string, edgeUrl: string, options: InvocationOptions = {}) => {
|
|
41
|
+
export const getInvocationUrl = (functionId: string, edgeUrl: string, options: InvocationOptions = {}) => {
|
|
38
42
|
const baseUrl = new URL('functions/', edgeUrl);
|
|
39
43
|
|
|
40
44
|
// Leading slashes cause the URL to be treated as an absolute path.
|
|
41
|
-
const
|
|
42
|
-
const url = new URL(`./${relativeUrl}`, baseUrl.toString());
|
|
45
|
+
const url = new URL(`./${functionId}`, baseUrl.toString());
|
|
43
46
|
options.spaceId && url.searchParams.set('spaceId', options.spaceId);
|
|
44
47
|
options.subjectId && url.searchParams.set('subjectId', options.subjectId);
|
|
45
48
|
url.protocol = isSecure(url.protocol) ? 'https' : 'http';
|