@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/database.ts
CHANGED
|
@@ -2,13 +2,18 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Context, Effect, Layer } from 'effect';
|
|
5
|
+
import { Context, Effect, Layer, Option, type Schema } from 'effect';
|
|
6
6
|
|
|
7
|
-
import type
|
|
8
|
-
import type { EchoDatabase, OneShotQueryResult, QueryResult } from '@dxos/echo-db';
|
|
7
|
+
import { type Filter, type Live, Obj, type Query, type Ref, type Relation, type Type } from '@dxos/echo';
|
|
8
|
+
import type { EchoDatabase, FlushOptions, OneShotQueryResult, QueryResult, SchemaRegistryQuery } from '@dxos/echo-db';
|
|
9
|
+
import type { SchemaRegistryPreparedQuery } from '@dxos/echo-db';
|
|
10
|
+
import type { EchoSchema } from '@dxos/echo-schema';
|
|
11
|
+
import { promiseWithCauseCapture } from '@dxos/effect';
|
|
12
|
+
import { BaseError } from '@dxos/errors';
|
|
13
|
+
import { invariant } from '@dxos/invariant';
|
|
9
14
|
import type { DXN } from '@dxos/keys';
|
|
10
15
|
|
|
11
|
-
export class DatabaseService extends Context.Tag('DatabaseService')<
|
|
16
|
+
export class DatabaseService extends Context.Tag('@dxos/functions/DatabaseService')<
|
|
12
17
|
DatabaseService,
|
|
13
18
|
{
|
|
14
19
|
readonly db: EchoDatabase;
|
|
@@ -28,23 +33,96 @@ export class DatabaseService extends Context.Tag('DatabaseService')<
|
|
|
28
33
|
};
|
|
29
34
|
};
|
|
30
35
|
|
|
31
|
-
static
|
|
32
|
-
|
|
36
|
+
static layer = (db: EchoDatabase): Layer.Layer<DatabaseService> => {
|
|
37
|
+
return Layer.succeed(DatabaseService, DatabaseService.make(db));
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Resolves an object by its DXN.
|
|
42
|
+
*/
|
|
43
|
+
static resolve: {
|
|
44
|
+
// No type check.
|
|
45
|
+
(dxn: DXN): Effect.Effect<Obj.Any | Relation.Any, never, DatabaseService>;
|
|
46
|
+
// Check matches schema.
|
|
47
|
+
<S extends Type.Obj.Any | Type.Relation.Any>(
|
|
48
|
+
dxn: DXN,
|
|
49
|
+
schema: S,
|
|
50
|
+
): Effect.Effect<Schema.Schema.Type<S>, ObjectNotFoundError, DatabaseService>;
|
|
51
|
+
} = (<S extends Type.Obj.Any | Type.Relation.Any>(
|
|
52
|
+
dxn: DXN,
|
|
53
|
+
schema?: S,
|
|
54
|
+
): Effect.Effect<Schema.Schema.Type<S>, ObjectNotFoundError, DatabaseService> =>
|
|
55
|
+
Effect.gen(function* () {
|
|
33
56
|
const { db } = yield* DatabaseService;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
57
|
+
const object = yield* promiseWithCauseCapture(() =>
|
|
58
|
+
db.graph
|
|
59
|
+
.createRefResolver({
|
|
60
|
+
context: {
|
|
61
|
+
space: db.spaceId,
|
|
62
|
+
},
|
|
63
|
+
})
|
|
64
|
+
.resolve(dxn),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
if (!object) {
|
|
68
|
+
return yield* Effect.fail(new ObjectNotFoundError({ dxn }));
|
|
69
|
+
}
|
|
70
|
+
invariant(!schema || Obj.instanceOf(schema, object), 'Object type mismatch.');
|
|
71
|
+
return object as any;
|
|
72
|
+
})) as any;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Loads an object reference.
|
|
76
|
+
*/
|
|
77
|
+
static load: <T>(ref: Ref.Ref<T>) => Effect.Effect<T, ObjectNotFoundError, never> = Effect.fn(function* (ref) {
|
|
78
|
+
const object = yield* promiseWithCauseCapture(() => ref.tryLoad());
|
|
79
|
+
if (!object) {
|
|
80
|
+
return yield* Effect.fail(new ObjectNotFoundError({ dxn: ref.dxn }));
|
|
81
|
+
}
|
|
82
|
+
return object;
|
|
83
|
+
});
|
|
43
84
|
|
|
44
|
-
|
|
45
|
-
|
|
85
|
+
/**
|
|
86
|
+
* Loads an object reference option.
|
|
87
|
+
*/
|
|
88
|
+
// TODO(burdon): Option?
|
|
89
|
+
static loadOption: <T>(ref: Ref.Ref<T>) => Effect.Effect<Option.Option<T>, never, never> = Effect.fn(function* (ref) {
|
|
90
|
+
const object = yield* DatabaseService.load(ref).pipe(
|
|
91
|
+
Effect.catchTag('OBJECT_NOT_FOUND', () => Effect.succeed(undefined)),
|
|
92
|
+
);
|
|
93
|
+
return Option.fromNullable(object);
|
|
46
94
|
});
|
|
47
95
|
|
|
96
|
+
// TODO(burdon): Can we create a proxy for the following methods on EchoDatabase? Use @inheritDoc?
|
|
97
|
+
// TODO(burdon): Figure out how to chain query().run();
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @link EchoDatabase.add
|
|
101
|
+
*/
|
|
102
|
+
static add = <T extends Obj.Any | Relation.Any>(obj: T): Effect.Effect<T, never, DatabaseService> =>
|
|
103
|
+
DatabaseService.pipe(Effect.map(({ db }) => db.add(obj)));
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @link EchoDatabase.remove
|
|
107
|
+
*/
|
|
108
|
+
static remove = <T extends Obj.Any | Relation.Any>(obj: T): Effect.Effect<void, never, DatabaseService> =>
|
|
109
|
+
DatabaseService.pipe(Effect.map(({ db }) => db.remove(obj)));
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @link EchoDatabase.flush
|
|
113
|
+
*/
|
|
114
|
+
static flush = (opts?: FlushOptions) =>
|
|
115
|
+
DatabaseService.pipe(Effect.flatMap(({ db }) => promiseWithCauseCapture(() => db.flush(opts))));
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @link EchoDatabase.getObjectById
|
|
119
|
+
*/
|
|
120
|
+
static getObjectById = <T extends Obj.Any | Relation.Any>(
|
|
121
|
+
id: string,
|
|
122
|
+
): Effect.Effect<Live<T> | undefined, never, DatabaseService> => {
|
|
123
|
+
return DatabaseService.pipe(Effect.map(({ db }) => db.getObjectById(id)));
|
|
124
|
+
};
|
|
125
|
+
|
|
48
126
|
/**
|
|
49
127
|
* Creates a `QueryResult` object that can be subscribed to.
|
|
50
128
|
*/
|
|
@@ -65,6 +143,28 @@ export class DatabaseService extends Context.Tag('DatabaseService')<
|
|
|
65
143
|
<F extends Filter.Any>(filter: F): Effect.Effect<OneShotQueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;
|
|
66
144
|
} = (queryOrFilter: Query.Any | Filter.Any) =>
|
|
67
145
|
DatabaseService.query(queryOrFilter as any).pipe(
|
|
68
|
-
Effect.flatMap((queryResult) =>
|
|
146
|
+
Effect.flatMap((queryResult) => promiseWithCauseCapture(() => queryResult.run())),
|
|
69
147
|
);
|
|
148
|
+
|
|
149
|
+
static schemaQuery = <Q extends SchemaRegistryQuery>(
|
|
150
|
+
query: Q,
|
|
151
|
+
): Effect.Effect<SchemaRegistryPreparedQuery<EchoSchema>, never, DatabaseService> =>
|
|
152
|
+
DatabaseService.pipe(
|
|
153
|
+
Effect.map(({ db }) => db.schemaRegistry.query(query)),
|
|
154
|
+
Effect.withSpan('DatabaseService.schemaQuery'),
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
static runSchemaQuery = <Q extends SchemaRegistryQuery>(
|
|
158
|
+
query: Q,
|
|
159
|
+
): Effect.Effect<EchoSchema[], never, DatabaseService> =>
|
|
160
|
+
DatabaseService.schemaQuery(query).pipe(
|
|
161
|
+
Effect.flatMap((queryResult) => promiseWithCauseCapture(() => queryResult.run())),
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// TODO(burdon): Move to echo/errors.
|
|
166
|
+
class ObjectNotFoundError extends BaseError.extend('OBJECT_NOT_FOUND') {
|
|
167
|
+
constructor(context?: Record<string, unknown>) {
|
|
168
|
+
super('Object not found', { context });
|
|
169
|
+
}
|
|
70
170
|
}
|
|
@@ -2,53 +2,81 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Effect,
|
|
5
|
+
import { Context, Effect, Layer, Schema } from 'effect';
|
|
6
6
|
|
|
7
|
+
import { Obj, Type } from '@dxos/echo';
|
|
7
8
|
import { invariant } from '@dxos/invariant';
|
|
8
|
-
import {
|
|
9
|
+
import { LogLevel, log } from '@dxos/log';
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
11
|
+
import { TracingService } from './tracing';
|
|
12
|
+
|
|
13
|
+
export const ComputeEventPayload = Schema.Union(
|
|
14
|
+
Schema.Struct({
|
|
15
|
+
type: Schema.Literal('begin-compute'),
|
|
16
|
+
nodeId: Schema.String,
|
|
17
|
+
inputs: Schema.Record({ key: Schema.String, value: Schema.Any }),
|
|
18
|
+
}),
|
|
19
|
+
Schema.Struct({
|
|
20
|
+
type: Schema.Literal('end-compute'),
|
|
21
|
+
nodeId: Schema.String,
|
|
22
|
+
outputs: Schema.Record({ key: Schema.String, value: Schema.Any }),
|
|
23
|
+
}),
|
|
24
|
+
Schema.Struct({
|
|
25
|
+
type: Schema.Literal('compute-input'),
|
|
26
|
+
nodeId: Schema.String,
|
|
27
|
+
property: Schema.String,
|
|
28
|
+
value: Schema.Any,
|
|
29
|
+
}),
|
|
30
|
+
Schema.Struct({
|
|
31
|
+
type: Schema.Literal('compute-output'),
|
|
32
|
+
nodeId: Schema.String,
|
|
33
|
+
property: Schema.String,
|
|
34
|
+
value: Schema.Any,
|
|
35
|
+
}),
|
|
36
|
+
Schema.Struct({
|
|
37
|
+
type: Schema.Literal('custom'),
|
|
38
|
+
nodeId: Schema.String,
|
|
39
|
+
event: Schema.Any,
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
export type ComputeEventPayload = Schema.Schema.Type<typeof ComputeEventPayload>;
|
|
38
43
|
|
|
39
|
-
export
|
|
40
|
-
|
|
41
|
-
|
|
44
|
+
export const ComputeEvent = Schema.Struct({
|
|
45
|
+
payload: ComputeEventPayload,
|
|
46
|
+
}).pipe(Type.Obj({ typename: 'dxos.org/type/ComputeEvent', version: '0.1.0' }));
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Logs event for the compute workflows.
|
|
50
|
+
*/
|
|
51
|
+
export class ComputeEventLogger extends Context.Tag('@dxos/functions/ComputeEventLogger')<
|
|
52
|
+
ComputeEventLogger,
|
|
53
|
+
{ readonly log: (event: ComputeEventPayload) => void; readonly nodeId: string | undefined }
|
|
42
54
|
>() {
|
|
43
|
-
static noop: Context.Tag.Service<
|
|
55
|
+
static noop: Context.Tag.Service<ComputeEventLogger> = {
|
|
44
56
|
log: () => {},
|
|
45
57
|
nodeId: undefined,
|
|
46
58
|
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Implements ComputeEventLogger using TracingService.
|
|
62
|
+
*/
|
|
63
|
+
static layerFromTracing = Layer.effect(
|
|
64
|
+
ComputeEventLogger,
|
|
65
|
+
Effect.gen(function* () {
|
|
66
|
+
const tracing = yield* TracingService;
|
|
67
|
+
return {
|
|
68
|
+
log: (event: ComputeEventPayload) => {
|
|
69
|
+
tracing.write(Obj.make(ComputeEvent, { payload: event }));
|
|
70
|
+
},
|
|
71
|
+
nodeId: undefined,
|
|
72
|
+
};
|
|
73
|
+
}),
|
|
74
|
+
);
|
|
47
75
|
}
|
|
48
76
|
|
|
49
77
|
export const logCustomEvent = (data: any) =>
|
|
50
78
|
Effect.gen(function* () {
|
|
51
|
-
const logger = yield*
|
|
79
|
+
const logger = yield* ComputeEventLogger;
|
|
52
80
|
if (!logger.nodeId) {
|
|
53
81
|
throw new Error('logCustomEvent must be called within a node compute function');
|
|
54
82
|
}
|
|
@@ -67,7 +95,10 @@ export const createDefectLogger = <A, E, R>(): ((self: Effect.Effect<A, E, R>) =
|
|
|
67
95
|
}),
|
|
68
96
|
);
|
|
69
97
|
|
|
70
|
-
export const createEventLogger = (
|
|
98
|
+
export const createEventLogger = (
|
|
99
|
+
level: LogLevel,
|
|
100
|
+
message: string = 'event',
|
|
101
|
+
): Context.Tag.Service<ComputeEventLogger> => {
|
|
71
102
|
const logFunction = (
|
|
72
103
|
{
|
|
73
104
|
[LogLevel.WARN]: log.warn,
|
|
@@ -79,7 +110,7 @@ export const createEventLogger = (level: LogLevel, message: string = 'event'): C
|
|
|
79
110
|
)[level];
|
|
80
111
|
invariant(logFunction);
|
|
81
112
|
return {
|
|
82
|
-
log: (event:
|
|
113
|
+
log: (event: ComputeEventPayload) => {
|
|
83
114
|
logFunction(message, event);
|
|
84
115
|
},
|
|
85
116
|
nodeId: undefined,
|
package/src/services/index.ts
CHANGED
|
@@ -8,4 +8,5 @@ export * from './service-container';
|
|
|
8
8
|
export * from './credentials';
|
|
9
9
|
export * from './tracing';
|
|
10
10
|
export * from './event-logger';
|
|
11
|
-
export * from './function-
|
|
11
|
+
export * from './remote-function-execution-service';
|
|
12
|
+
export * from './local-function-execution';
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Context, Effect, Layer, Schema } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { todo } from '@dxos/debug';
|
|
8
|
+
|
|
9
|
+
import { FunctionError } from '../errors';
|
|
10
|
+
import type { FunctionContext, FunctionDefinition } from '../handler';
|
|
11
|
+
|
|
12
|
+
import type { Services } from './service-container';
|
|
13
|
+
|
|
14
|
+
export class LocalFunctionExecutionService extends Context.Tag('@dxos/functions/LocalFunctionExecutionService')<
|
|
15
|
+
LocalFunctionExecutionService,
|
|
16
|
+
{
|
|
17
|
+
// TODO(dmaretskyi): This should take function id instead of the definition object.
|
|
18
|
+
// TODO(dmaretskyi): Services should be satisfied from environment rather then bubbled up.
|
|
19
|
+
invokeFunction(functionDef: FunctionDefinition<any, any>, input: unknown): Effect.Effect<unknown, never, Services>;
|
|
20
|
+
}
|
|
21
|
+
>() {
|
|
22
|
+
static layer = Layer.succeed(LocalFunctionExecutionService, {
|
|
23
|
+
invokeFunction: (functionDef, input) => invokeFunction(functionDef, input),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
static invokeFunction: <F extends FunctionDefinition.Any>(
|
|
27
|
+
functionDef: F,
|
|
28
|
+
input: FunctionDefinition.Input<F>,
|
|
29
|
+
) => Effect.Effect<FunctionDefinition.Output<F>, never, Services | LocalFunctionExecutionService> =
|
|
30
|
+
Effect.serviceFunctionEffect(LocalFunctionExecutionService, (_) => _.invokeFunction as any);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const invokeFunction = (
|
|
34
|
+
functionDef: FunctionDefinition<any, any>,
|
|
35
|
+
input: any,
|
|
36
|
+
): Effect.Effect<unknown, never, Services> =>
|
|
37
|
+
Effect.gen(function* () {
|
|
38
|
+
// Assert input matches schema
|
|
39
|
+
const assertInput = functionDef.inputSchema.pipe(Schema.asserts);
|
|
40
|
+
(assertInput as any)(input);
|
|
41
|
+
|
|
42
|
+
const context: FunctionContext = {
|
|
43
|
+
space: undefined,
|
|
44
|
+
getService: () => todo(),
|
|
45
|
+
getSpace: async (_spaceId: any) => {
|
|
46
|
+
throw new Error('Not available. Use the database service instead.');
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// TODO(dmaretskyi): This should be delegated to a function invoker service.
|
|
51
|
+
const data = yield* Effect.gen(function* () {
|
|
52
|
+
const result = functionDef.handler({ context, data: input });
|
|
53
|
+
if (Effect.isEffect(result)) {
|
|
54
|
+
return yield* (result as Effect.Effect<unknown, unknown, Services>).pipe(Effect.orDie);
|
|
55
|
+
} else if (
|
|
56
|
+
typeof result === 'object' &&
|
|
57
|
+
result !== null &&
|
|
58
|
+
'then' in result &&
|
|
59
|
+
typeof result.then === 'function'
|
|
60
|
+
) {
|
|
61
|
+
return yield* Effect.promise(() => result);
|
|
62
|
+
} else {
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
}).pipe(
|
|
66
|
+
Effect.orDie,
|
|
67
|
+
Effect.catchAllDefect((defect) =>
|
|
68
|
+
Effect.die(new FunctionError('Error running function', { context: { name: functionDef.name }, cause: defect })),
|
|
69
|
+
),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
// Assert output matches schema
|
|
73
|
+
const assertOutput = functionDef.outputSchema?.pipe(Schema.asserts);
|
|
74
|
+
(assertOutput as any)(data);
|
|
75
|
+
|
|
76
|
+
return data;
|
|
77
|
+
}).pipe(Effect.withSpan('invokeFunction', { attributes: { name: functionDef.name } }));
|
package/src/services/queues.ts
CHANGED
|
@@ -2,14 +2,16 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Context, Layer } from 'effect';
|
|
5
|
+
import { Context, Effect, Layer } from 'effect';
|
|
6
6
|
|
|
7
|
+
import type { Obj, Relation } from '@dxos/echo';
|
|
7
8
|
import type { Queue, QueueAPI, QueueFactory } from '@dxos/echo-db';
|
|
9
|
+
import type { DXN, QueueSubspaceTag } from '@dxos/keys';
|
|
8
10
|
|
|
9
11
|
/**
|
|
10
12
|
* Gives access to all queues.
|
|
11
13
|
*/
|
|
12
|
-
export class QueueService extends Context.Tag('QueueService')<
|
|
14
|
+
export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
|
|
13
15
|
QueueService,
|
|
14
16
|
{
|
|
15
17
|
/**
|
|
@@ -21,35 +23,55 @@ export class QueueService extends Context.Tag('QueueService')<
|
|
|
21
23
|
* The queue that is used to store the context of the current research.
|
|
22
24
|
* @deprecated Use `ContextQueueService` instead.
|
|
23
25
|
*/
|
|
24
|
-
readonly
|
|
26
|
+
readonly queue: Queue | undefined;
|
|
25
27
|
}
|
|
26
28
|
>() {
|
|
27
29
|
static notAvailable = Layer.succeed(QueueService, {
|
|
28
30
|
queues: {
|
|
29
|
-
get(
|
|
31
|
+
get(_dxn) {
|
|
30
32
|
throw new Error('Queues not available');
|
|
31
33
|
},
|
|
32
34
|
create() {
|
|
33
35
|
throw new Error('Queues not available');
|
|
34
36
|
},
|
|
35
37
|
},
|
|
36
|
-
|
|
38
|
+
queue: undefined,
|
|
37
39
|
});
|
|
38
40
|
|
|
39
|
-
static make = (queues: QueueFactory,
|
|
41
|
+
static make = (queues: QueueFactory, queue?: Queue): Context.Tag.Service<QueueService> => {
|
|
40
42
|
return {
|
|
41
43
|
queues,
|
|
42
|
-
|
|
44
|
+
queue,
|
|
43
45
|
};
|
|
44
46
|
};
|
|
47
|
+
|
|
48
|
+
static layer = (queues: QueueFactory, queue?: Queue): Layer.Layer<QueueService> =>
|
|
49
|
+
Layer.succeed(QueueService, QueueService.make(queues, queue));
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Gets a queue by its DXN.
|
|
53
|
+
*/
|
|
54
|
+
static getQueue = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(
|
|
55
|
+
dxn: DXN,
|
|
56
|
+
): Effect.Effect<Queue<T>, never, QueueService> => QueueService.pipe(Effect.map(({ queues }) => queues.get<T>(dxn)));
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Creates a new queue.
|
|
60
|
+
*/
|
|
61
|
+
static createQueue = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(options?: {
|
|
62
|
+
subspaceTag?: QueueSubspaceTag;
|
|
63
|
+
}): Effect.Effect<Queue<T>, never, QueueService> =>
|
|
64
|
+
QueueService.pipe(Effect.map(({ queues }) => queues.create<T>(options)));
|
|
45
65
|
}
|
|
46
66
|
|
|
47
67
|
/**
|
|
48
68
|
* Gives access to a specific queue passed as a context.
|
|
49
69
|
*/
|
|
50
|
-
export class ContextQueueService extends Context.Tag('ContextQueueService')<
|
|
70
|
+
export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQueueService')<
|
|
51
71
|
ContextQueueService,
|
|
52
72
|
{
|
|
53
|
-
readonly
|
|
73
|
+
readonly queue: Queue;
|
|
54
74
|
}
|
|
55
|
-
>() {
|
|
75
|
+
>() {
|
|
76
|
+
static layer = (queue: Queue) => Layer.succeed(ContextQueueService, { queue });
|
|
77
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Context, Layer } from 'effect';
|
|
6
|
+
|
|
7
|
+
import type { SpaceId } from '@dxos/keys';
|
|
8
|
+
|
|
9
|
+
import { getInvocationUrl } from '../url';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Allows calling into other functions.
|
|
13
|
+
*/
|
|
14
|
+
export class RemoteFunctionExecutionService extends Context.Tag('@dxos/functions/RemoteFunctionExecutionService')<
|
|
15
|
+
RemoteFunctionExecutionService,
|
|
16
|
+
{
|
|
17
|
+
callFunction(deployedFunctionId: string, input: any, spaceId?: SpaceId): Promise<any>;
|
|
18
|
+
}
|
|
19
|
+
>() {
|
|
20
|
+
static fromClient(baseUrl: string, spaceId: SpaceId): Context.Tag.Service<RemoteFunctionExecutionService> {
|
|
21
|
+
return {
|
|
22
|
+
callFunction: async (deployedFunctionId: string, input: any) => {
|
|
23
|
+
const url = getInvocationUrl(deployedFunctionId, baseUrl, { spaceId });
|
|
24
|
+
const result = await fetch(url, {
|
|
25
|
+
method: 'POST',
|
|
26
|
+
headers: { 'Content-Type': 'application/json' },
|
|
27
|
+
body: JSON.stringify(input),
|
|
28
|
+
});
|
|
29
|
+
if (result.status >= 300 || result.status < 200) {
|
|
30
|
+
throw new Error('Failed to invoke function', { cause: new Error(`HTTP error: ${await result.text()}`) });
|
|
31
|
+
}
|
|
32
|
+
return await result.json();
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static mock = (): Context.Tag.Service<RemoteFunctionExecutionService> => {
|
|
38
|
+
return {
|
|
39
|
+
callFunction: async (deployedFunctionId: string, input: any) => {
|
|
40
|
+
return input;
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
static mockLayer = Layer.succeed(RemoteFunctionExecutionService, RemoteFunctionExecutionService.mock());
|
|
46
|
+
}
|
|
@@ -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.
|