@dxos/functions 0.8.3 → 0.8.4-main.1da679c
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 +73 -44
- package/dist/lib/browser/bundler/index.mjs.map +3 -3
- package/dist/lib/browser/chunk-D2XO7XXY.mjs +611 -0
- package/dist/lib/browser/chunk-D2XO7XXY.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 +24 -10
- package/dist/lib/browser/edge/index.mjs.map +3 -3
- package/dist/lib/browser/index.mjs +981 -137
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +110 -9
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node-esm/bundler/index.mjs +72 -44
- 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-Z4CJ62WS.mjs +613 -0
- package/dist/lib/node-esm/chunk-Z4CJ62WS.mjs.map +7 -0
- package/dist/lib/node-esm/edge/index.mjs +23 -10
- package/dist/lib/node-esm/edge/index.mjs.map +3 -3
- package/dist/lib/node-esm/index.mjs +981 -137
- 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 +110 -9
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- package/dist/types/src/bundler/bundler.d.ts +12 -14
- 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 +4 -1
- package/dist/types/src/executor/executor.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +46 -16
- 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 +8 -3
- package/dist/types/src/schema.d.ts.map +1 -1
- package/dist/types/src/services/credentials.d.ts +18 -4
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/database.d.ts +55 -3
- package/dist/types/src/services/database.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +65 -30
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/index.d.ts +2 -2
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/local-function-execution.d.ts +25 -0
- package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
- package/dist/types/src/services/queues.d.ts +33 -6
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/remote-function-execution-service.d.ts +15 -0
- package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -0
- package/dist/types/src/services/service-container.d.ts +29 -17
- package/dist/types/src/services/service-container.d.ts.map +1 -1
- package/dist/types/src/services/service-registry.d.ts +29 -0
- package/dist/types/src/services/service-registry.d.ts.map +1 -0
- package/dist/types/src/services/service-registry.test.d.ts +2 -0
- package/dist/types/src/services/service-registry.test.d.ts.map +1 -0
- package/dist/types/src/services/tracing.d.ts +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 +15 -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 +55 -9
- 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/translations.d.ts +9 -9
- package/dist/types/src/translations.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 +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 +35 -25
- package/src/bundler/bundler.test.ts +9 -10
- package/src/bundler/bundler.ts +56 -35
- package/src/edge/functions.ts +9 -6
- package/src/errors.ts +21 -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 +22 -15
- package/src/handler.ts +117 -27
- package/src/index.ts +3 -2
- package/src/schema.ts +11 -0
- package/src/services/credentials.ts +87 -5
- package/src/services/database.ts +146 -3
- package/src/services/event-logger.ts +68 -37
- package/src/services/index.ts +2 -2
- package/src/services/local-function-execution.ts +127 -0
- package/src/services/queues.ts +56 -11
- package/src/services/remote-function-execution-service.ts +46 -0
- package/src/services/service-container.ts +47 -42
- package/src/services/service-registry.test.ts +42 -0
- package/src/services/service-registry.ts +59 -0
- package/src/services/tracing.ts +118 -5
- package/src/testing/index.ts +1 -0
- package/src/testing/layer.ts +111 -0
- package/src/testing/logger.ts +4 -4
- package/src/testing/persist-database.test.ts +87 -0
- package/src/testing/services.ts +97 -14
- package/src/trace.ts +17 -19
- package/src/translations.ts +4 -4
- 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 +516 -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-WEFZUEL2.mjs +0 -300
- package/dist/lib/browser/chunk-WEFZUEL2.mjs.map +0 -7
- package/dist/lib/node/bundler/index.cjs +0 -260
- package/dist/lib/node/bundler/index.cjs.map +0 -7
- package/dist/lib/node/chunk-IJAE7FZK.cjs +0 -320
- package/dist/lib/node/chunk-IJAE7FZK.cjs.map +0 -7
- package/dist/lib/node/edge/index.cjs +0 -94
- package/dist/lib/node/edge/index.cjs.map +0 -7
- package/dist/lib/node/index.cjs +0 -522
- package/dist/lib/node/index.cjs.map +0 -7
- package/dist/lib/node/meta.json +0 -1
- package/dist/lib/node/testing/index.cjs +0 -43
- package/dist/lib/node/testing/index.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-LIYPMWNQ.mjs +0 -302
- package/dist/lib/node-esm/chunk-LIYPMWNQ.mjs.map +0 -7
- package/dist/types/src/services/ai.d.ts +0 -12
- package/dist/types/src/services/ai.d.ts.map +0 -1
- package/dist/types/src/services/function-call-service.d.ts +0 -16
- package/dist/types/src/services/function-call-service.d.ts.map +0 -1
- package/src/services/ai.ts +0 -32
- package/src/services/function-call-service.ts +0 -64
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
storagePath?: string;
|
|
29
|
+
onInit?: () => Effect.Effect<void, never, DatabaseService | QueueService>;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const TestDatabaseLayer = ({ indexing, types, storagePath, onInit }: TestDatabaseOptions = {}) =>
|
|
33
|
+
Layer.scopedContext(
|
|
34
|
+
Effect.gen(function* () {
|
|
35
|
+
const builder = yield* testBuilder;
|
|
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
|
+
|
|
106
|
+
return Context.mergeAll(
|
|
107
|
+
Context.make(DatabaseService, DatabaseService.make(db)),
|
|
108
|
+
Context.make(QueueService, QueueService.make(queues, undefined)),
|
|
109
|
+
);
|
|
110
|
+
}),
|
|
111
|
+
);
|
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,29 +4,112 @@
|
|
|
4
4
|
|
|
5
5
|
import { type Context } from 'effect';
|
|
6
6
|
|
|
7
|
-
import type {
|
|
8
|
-
import type { QueueFactory } from '@dxos/echo-db';
|
|
7
|
+
import type { Space } from '@dxos/client/echo';
|
|
8
|
+
import type { EchoDatabase, QueueFactory } from '@dxos/echo-db';
|
|
9
|
+
import { assertArgument } from '@dxos/invariant';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
type ComputeEventLogger,
|
|
13
|
+
ConfiguredCredentialsService,
|
|
14
|
+
type CredentialsService,
|
|
15
|
+
DatabaseService,
|
|
16
|
+
QueueService,
|
|
17
|
+
ServiceContainer,
|
|
18
|
+
type ServiceCredential,
|
|
19
|
+
type TracingService,
|
|
20
|
+
} from '../services';
|
|
9
21
|
|
|
10
22
|
import { consoleLogger, noopLogger } from './logger';
|
|
11
|
-
|
|
12
|
-
|
|
23
|
+
|
|
24
|
+
// TODO(burdon): Factor out.
|
|
25
|
+
export type OneOf<T> = {
|
|
26
|
+
[K in keyof T]: { [P in K]: T[P] } & { [P in Exclude<keyof T, K>]?: never };
|
|
27
|
+
}[keyof T];
|
|
28
|
+
|
|
29
|
+
export type AiServiceProvider = 'dev' | 'edge' | 'ollama' | 'lmstudio';
|
|
13
30
|
|
|
14
31
|
export type TestServiceOptions = {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
32
|
+
/**
|
|
33
|
+
* AI service configuration.
|
|
34
|
+
*/
|
|
35
|
+
ai?: any;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Credentials service configuration.
|
|
39
|
+
*/
|
|
40
|
+
credentials?: OneOf<{
|
|
41
|
+
/**
|
|
42
|
+
* Predefined credentials list.
|
|
43
|
+
*/
|
|
44
|
+
services?: ServiceCredential[];
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Custom credentials service.
|
|
48
|
+
*/
|
|
49
|
+
service?: Context.Tag.Service<CredentialsService>;
|
|
50
|
+
}>;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Database configuration.
|
|
54
|
+
*/
|
|
55
|
+
db?: EchoDatabase;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Gets database and queue services from the space.
|
|
59
|
+
* Exclusive with: `db`, `queues`
|
|
60
|
+
*/
|
|
61
|
+
space?: Space;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Logging configuration.
|
|
65
|
+
*/
|
|
66
|
+
logging?: {
|
|
67
|
+
enabled?: boolean;
|
|
68
|
+
logger?: Context.Tag.Service<ComputeEventLogger>;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Queue service configuration.
|
|
73
|
+
*/
|
|
74
|
+
queues?: QueueFactory;
|
|
75
|
+
|
|
76
|
+
tracing?: {
|
|
77
|
+
service?: Context.Tag.Service<TracingService>;
|
|
78
|
+
};
|
|
19
79
|
};
|
|
20
80
|
|
|
81
|
+
/**
|
|
82
|
+
* @deprecated
|
|
83
|
+
*/
|
|
21
84
|
export const createTestServices = ({
|
|
22
85
|
ai,
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
86
|
+
credentials,
|
|
87
|
+
db,
|
|
88
|
+
logging,
|
|
89
|
+
queues,
|
|
90
|
+
space,
|
|
91
|
+
tracing,
|
|
26
92
|
}: TestServiceOptions = {}): ServiceContainer => {
|
|
93
|
+
assertArgument(!(!!space && (!!db || !!queues)), 'space', 'space can be provided only if db and queues are not');
|
|
94
|
+
|
|
27
95
|
return new ServiceContainer().setServices({
|
|
28
|
-
ai:
|
|
29
|
-
|
|
30
|
-
|
|
96
|
+
// ai: createAiService(ai),
|
|
97
|
+
credentials: createCredentialsService(credentials),
|
|
98
|
+
database: space || db ? DatabaseService.make(space?.db || db!) : undefined,
|
|
99
|
+
eventLogger: (logging?.logger ?? logging?.enabled) ? consoleLogger : noopLogger,
|
|
100
|
+
queues: space || queues ? QueueService.make(space?.queues || queues!, undefined) : undefined,
|
|
101
|
+
tracing: tracing?.service,
|
|
31
102
|
});
|
|
32
103
|
};
|
|
104
|
+
|
|
105
|
+
const createCredentialsService = (
|
|
106
|
+
credentials: TestServiceOptions['credentials'] | undefined,
|
|
107
|
+
): Context.Tag.Service<CredentialsService> | undefined => {
|
|
108
|
+
if (credentials?.services) {
|
|
109
|
+
return new ConfiguredCredentialsService(credentials.services);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (credentials?.service) {
|
|
113
|
+
return credentials.service;
|
|
114
|
+
}
|
|
115
|
+
};
|
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,
|
package/src/translations.ts
CHANGED
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { type Resource } from 'i18next';
|
|
6
6
|
|
|
7
7
|
import { ScriptType } from './schema';
|
|
8
8
|
|
|
9
|
-
export
|
|
9
|
+
export const translations = [
|
|
10
10
|
{
|
|
11
11
|
'en-US': {
|
|
12
|
-
[
|
|
12
|
+
[ScriptType.typename]: {
|
|
13
13
|
'typename label': 'Script',
|
|
14
14
|
'typename label_zero': 'Scripts',
|
|
15
15
|
'typename label_one': 'Script',
|
|
@@ -17,4 +17,4 @@ export default [
|
|
|
17
17
|
},
|
|
18
18
|
},
|
|
19
19
|
},
|
|
20
|
-
];
|
|
20
|
+
] as const satisfies Resource[];
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type EventType, type FunctionTrigger } from '@dxos/functions';
|
|
6
|
+
|
|
7
|
+
export const createInvocationPayload = (trigger: FunctionTrigger, event: EventType): any => {
|
|
8
|
+
if (!trigger.input) {
|
|
9
|
+
return event;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const payload: any = {};
|
|
13
|
+
for (const [key, value] of Object.entries(trigger.input)) {
|
|
14
|
+
if (typeof value !== 'string' || !(value.startsWith('{{') && value.endsWith('}}'))) {
|
|
15
|
+
payload[key] = value;
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const propertyPath = value.slice(2, -2);
|
|
20
|
+
let valueSubstitution: any = propertyPath.startsWith('trigger.')
|
|
21
|
+
? trigger
|
|
22
|
+
: propertyPath.startsWith('event.')
|
|
23
|
+
? event
|
|
24
|
+
: undefined;
|
|
25
|
+
|
|
26
|
+
for (const pathSegment of propertyPath.split('.').slice(1)) {
|
|
27
|
+
if (valueSubstitution && typeof valueSubstitution === 'object') {
|
|
28
|
+
valueSubstitution = valueSubstitution[pathSegment];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
payload[key] = valueSubstitution;
|
|
33
|
+
}
|
|
34
|
+
return payload;
|
|
35
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Context, Effect, Layer } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { Obj, Ref } from '@dxos/echo';
|
|
8
|
+
import { type Queue } from '@dxos/echo-db';
|
|
9
|
+
import { DXN, ObjectId } from '@dxos/keys';
|
|
10
|
+
|
|
11
|
+
import { QueueService } from '../services';
|
|
12
|
+
import {
|
|
13
|
+
InvocationOutcome,
|
|
14
|
+
InvocationTraceEndEvent,
|
|
15
|
+
InvocationTraceEventType,
|
|
16
|
+
InvocationTraceStartEvent,
|
|
17
|
+
} from '../trace';
|
|
18
|
+
import type { TriggerKind } from '../types';
|
|
19
|
+
|
|
20
|
+
export type FunctionInvocationPayload = {
|
|
21
|
+
data?: any;
|
|
22
|
+
inputNodeId?: string;
|
|
23
|
+
trigger?: {
|
|
24
|
+
id: string;
|
|
25
|
+
kind: TriggerKind;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type TraceData = {
|
|
30
|
+
invocationId: ObjectId;
|
|
31
|
+
invocationTraceQueue: Queue;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export class InvocationTracer extends Context.Tag('@dxos/functions/InvocationTracer')<
|
|
35
|
+
InvocationTracer,
|
|
36
|
+
{
|
|
37
|
+
traceInvocationStart({
|
|
38
|
+
payload,
|
|
39
|
+
target,
|
|
40
|
+
}: {
|
|
41
|
+
payload: FunctionInvocationPayload;
|
|
42
|
+
target?: DXN;
|
|
43
|
+
}): Effect.Effect<TraceData, never, QueueService>;
|
|
44
|
+
|
|
45
|
+
traceInvocationEnd({ trace, exception }: { trace: TraceData; exception?: any }): Effect.Effect<void>;
|
|
46
|
+
}
|
|
47
|
+
>() {
|
|
48
|
+
static layerLive = (opts: { invocationTraceQueue: Queue }) =>
|
|
49
|
+
Layer.effect(
|
|
50
|
+
InvocationTracer,
|
|
51
|
+
Effect.gen(function* () {
|
|
52
|
+
return {
|
|
53
|
+
traceInvocationStart: Effect.fn('traceInvocationStart')(function* ({ payload, target }) {
|
|
54
|
+
const invocationId = ObjectId.random();
|
|
55
|
+
const invocationTraceQueue = yield* QueueService.createQueue({ subspaceTag: 'trace' });
|
|
56
|
+
const now = Date.now();
|
|
57
|
+
const traceEvent = Obj.make(InvocationTraceStartEvent, {
|
|
58
|
+
type: InvocationTraceEventType.START,
|
|
59
|
+
invocationId,
|
|
60
|
+
timestamp: now,
|
|
61
|
+
// TODO(dmaretskyi): Not json-stringifying this makes ECHO fail when one ECHO object becomes embedded in another ECHO object.
|
|
62
|
+
input: JSON.parse(JSON.stringify(payload.data ?? {})),
|
|
63
|
+
invocationTraceQueue: Ref.fromDXN(invocationTraceQueue.dxn),
|
|
64
|
+
invocationTarget: target ? Ref.fromDXN(target) : undefined,
|
|
65
|
+
trigger: payload.trigger ? Ref.fromDXN(DXN.fromLocalObjectId(payload.trigger.id)) : undefined,
|
|
66
|
+
});
|
|
67
|
+
yield* QueueService.append(opts.invocationTraceQueue, [traceEvent]);
|
|
68
|
+
|
|
69
|
+
return { invocationId, invocationTraceQueue };
|
|
70
|
+
}),
|
|
71
|
+
traceInvocationEnd: Effect.fn('traceInvocationEnd')(function* ({ trace, exception }) {
|
|
72
|
+
const now = Date.now();
|
|
73
|
+
const traceEvent = Obj.make(InvocationTraceEndEvent, {
|
|
74
|
+
type: InvocationTraceEventType.END,
|
|
75
|
+
invocationId: trace.invocationId,
|
|
76
|
+
timestamp: now,
|
|
77
|
+
outcome: exception ? InvocationOutcome.FAILURE : InvocationOutcome.SUCCESS,
|
|
78
|
+
exception: exception
|
|
79
|
+
? {
|
|
80
|
+
name: exception.constructor.name,
|
|
81
|
+
timestamp: now,
|
|
82
|
+
message: exception?.message ?? 'Unknown error',
|
|
83
|
+
stack: exception?.stack,
|
|
84
|
+
}
|
|
85
|
+
: undefined,
|
|
86
|
+
});
|
|
87
|
+
yield* QueueService.append(opts.invocationTraceQueue, [traceEvent]);
|
|
88
|
+
}),
|
|
89
|
+
};
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
static layerTest = Layer.unwrapEffect(
|
|
94
|
+
Effect.gen(function* () {
|
|
95
|
+
const queue = yield* QueueService.createQueue({ subspaceTag: 'trace' });
|
|
96
|
+
return InvocationTracer.layerLive({ invocationTraceQueue: queue });
|
|
97
|
+
}),
|
|
98
|
+
);
|
|
99
|
+
}
|