@dxos/functions 0.8.4-main.b97322e → 0.8.4-main.dedc0f3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/bundler/index.mjs +56 -38
- package/dist/lib/browser/bundler/index.mjs.map +3 -3
- package/dist/lib/browser/chunk-ANP3DFCO.mjs +623 -0
- package/dist/lib/browser/chunk-ANP3DFCO.mjs.map +7 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/browser/edge/index.mjs +22 -8
- package/dist/lib/browser/edge/index.mjs.map +3 -3
- package/dist/lib/browser/index.mjs +892 -130
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +77 -39
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/bundler/index.mjs +55 -38
- package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-MPKVY7ZR.mjs +625 -0
- package/dist/lib/node-esm/chunk-MPKVY7ZR.mjs.map +7 -0
- package/dist/lib/node-esm/edge/index.mjs +21 -8
- package/dist/lib/node-esm/edge/index.mjs.map +3 -3
- package/dist/lib/node-esm/index.mjs +892 -130
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +77 -39
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/bundler/bundler.d.ts +11 -12
- package/dist/types/src/bundler/bundler.d.ts.map +1 -1
- package/dist/types/src/edge/functions.d.ts +3 -2
- package/dist/types/src/edge/functions.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +77 -8
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/examples/fib.d.ts +7 -0
- package/dist/types/src/examples/fib.d.ts.map +1 -0
- package/dist/types/src/examples/index.d.ts +4 -0
- package/dist/types/src/examples/index.d.ts.map +1 -0
- package/dist/types/src/examples/reply.d.ts +3 -0
- package/dist/types/src/examples/reply.d.ts.map +1 -0
- package/dist/types/src/examples/sleep.d.ts +5 -0
- package/dist/types/src/examples/sleep.d.ts.map +1 -0
- package/dist/types/src/executor/executor.d.ts +4 -1
- package/dist/types/src/executor/executor.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +40 -8
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/schema.d.ts +6 -1
- package/dist/types/src/schema.d.ts.map +1 -1
- package/dist/types/src/services/credentials.d.ts +16 -3
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/database.d.ts +75 -6
- package/dist/types/src/services/database.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +65 -30
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/index.d.ts +2 -1
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/local-function-execution.d.ts +25 -0
- package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
- package/dist/types/src/services/queues.d.ts +21 -6
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/remote-function-execution-service.d.ts +15 -0
- package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -0
- package/dist/types/src/services/service-container.d.ts +5 -5
- package/dist/types/src/services/service-container.d.ts.map +1 -1
- package/dist/types/src/services/service-registry.d.ts +1 -1
- package/dist/types/src/services/service-registry.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +37 -5
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +7 -2
- package/dist/types/src/testing/layer.d.ts.map +1 -1
- package/dist/types/src/testing/logger.d.ts +3 -3
- package/dist/types/src/testing/logger.d.ts.map +1 -1
- package/dist/types/src/testing/persist-database.test.d.ts +2 -0
- package/dist/types/src/testing/persist-database.test.d.ts.map +1 -0
- package/dist/types/src/testing/services.d.ts +6 -17
- package/dist/types/src/testing/services.d.ts.map +1 -1
- package/dist/types/src/trace.d.ts +20 -22
- package/dist/types/src/trace.d.ts.map +1 -1
- package/dist/types/src/triggers/index.d.ts +4 -0
- package/dist/types/src/triggers/index.d.ts.map +1 -0
- package/dist/types/src/triggers/input-builder.d.ts +3 -0
- package/dist/types/src/triggers/input-builder.d.ts.map +1 -0
- package/dist/types/src/triggers/invocation-tracer.d.ts +35 -0
- package/dist/types/src/triggers/invocation-tracer.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-dispatcher.d.ts +75 -0
- package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +2 -0
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-state-store.d.ts +27 -0
- package/dist/types/src/triggers/trigger-state-store.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +49 -249
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/url.d.ts +10 -6
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +39 -34
- package/src/bundler/bundler.test.ts +8 -9
- package/src/bundler/bundler.ts +32 -33
- package/src/edge/functions.ts +8 -5
- package/src/errors.ts +8 -0
- package/src/examples/fib.ts +30 -0
- package/src/examples/index.ts +7 -0
- package/src/examples/reply.ts +18 -0
- package/src/examples/sleep.ts +22 -0
- package/src/executor/executor.ts +9 -9
- package/src/handler.ts +99 -18
- package/src/index.ts +3 -3
- package/src/schema.ts +11 -0
- package/src/services/credentials.ts +79 -3
- package/src/services/database.ts +118 -18
- package/src/services/event-logger.ts +68 -37
- package/src/services/index.ts +2 -1
- package/src/services/local-function-execution.ts +114 -0
- package/src/services/queues.ts +37 -10
- package/src/services/remote-function-execution-service.ts +46 -0
- package/src/services/service-container.ts +11 -10
- package/src/services/service-registry.ts +5 -2
- package/src/services/tracing.ts +105 -7
- package/src/testing/layer.ts +83 -3
- package/src/testing/logger.ts +4 -4
- package/src/testing/persist-database.test.ts +87 -0
- package/src/testing/services.ts +10 -63
- package/src/trace.ts +17 -19
- package/src/triggers/index.ts +7 -0
- package/src/triggers/input-builder.ts +35 -0
- package/src/triggers/invocation-tracer.ts +99 -0
- package/src/triggers/trigger-dispatcher.test.ts +652 -0
- package/src/triggers/trigger-dispatcher.ts +512 -0
- package/src/triggers/trigger-state-store.ts +60 -0
- package/src/types.ts +22 -33
- package/src/url.ts +13 -10
- package/dist/lib/browser/chunk-3NGCSUEW.mjs +0 -328
- package/dist/lib/browser/chunk-3NGCSUEW.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-FJ2MU7TL.mjs +0 -330
- package/dist/lib/node-esm/chunk-FJ2MU7TL.mjs.map +0 -7
- package/dist/types/src/services/function-call-service.d.ts +0 -16
- package/dist/types/src/services/function-call-service.d.ts.map +0 -1
- package/src/services/function-call-service.ts +0 -64
package/src/schema.ts
CHANGED
|
@@ -32,6 +32,17 @@ export interface ScriptType extends Schema.Schema.Type<typeof ScriptType> {}
|
|
|
32
32
|
* Function deployment.
|
|
33
33
|
*/
|
|
34
34
|
export const FunctionType = Schema.Struct({
|
|
35
|
+
/**
|
|
36
|
+
* Global registry ID.
|
|
37
|
+
* NOTE: The `key` property refers to the original registry entry.
|
|
38
|
+
*/
|
|
39
|
+
// TODO(burdon): Create Format type for DXN-like ids, such as this and schema type.
|
|
40
|
+
// TODO(dmaretskyi): Consider making it part of ECHO meta.
|
|
41
|
+
// TODO(dmaretskyi): Make required.
|
|
42
|
+
key: Schema.optional(Schema.String).annotations({
|
|
43
|
+
description: 'Unique registration key for the blueprint',
|
|
44
|
+
}),
|
|
45
|
+
|
|
35
46
|
// TODO(burdon): Rename to id/uri?
|
|
36
47
|
name: Schema.NonEmptyString,
|
|
37
48
|
version: Schema.String,
|
|
@@ -2,9 +2,15 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { HttpClient, HttpClientRequest } from '@effect/platform';
|
|
6
|
+
import { type Config, Context, Effect, Layer, Redacted } from 'effect';
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
import { Query } from '@dxos/echo';
|
|
9
|
+
import { DataType } from '@dxos/schema';
|
|
10
|
+
|
|
11
|
+
import { DatabaseService } from './database';
|
|
12
|
+
|
|
13
|
+
export type CredentialQuery = {
|
|
8
14
|
service?: string;
|
|
9
15
|
};
|
|
10
16
|
|
|
@@ -17,7 +23,7 @@ export type ServiceCredential = {
|
|
|
17
23
|
apiKey?: string;
|
|
18
24
|
};
|
|
19
25
|
|
|
20
|
-
export class CredentialsService extends Context.Tag('CredentialsService')<
|
|
26
|
+
export class CredentialsService extends Context.Tag('@dxos/functions/CredentialsService')<
|
|
21
27
|
CredentialsService,
|
|
22
28
|
{
|
|
23
29
|
/**
|
|
@@ -37,6 +43,64 @@ export class CredentialsService extends Context.Tag('CredentialsService')<
|
|
|
37
43
|
const credentials = yield* CredentialsService;
|
|
38
44
|
return yield* Effect.promise(() => credentials.getCredential(query));
|
|
39
45
|
});
|
|
46
|
+
|
|
47
|
+
static getApiKey = (query: CredentialQuery): Effect.Effect<Redacted.Redacted<string>, never, CredentialsService> =>
|
|
48
|
+
Effect.gen(function* () {
|
|
49
|
+
const credential = yield* CredentialsService.getCredential(query);
|
|
50
|
+
if (!credential.apiKey) {
|
|
51
|
+
throw new Error(`API key not found for service: ${query.service}`);
|
|
52
|
+
}
|
|
53
|
+
return Redacted.make(credential.apiKey);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
static configuredLayer = (credentials: ServiceCredential[]) =>
|
|
57
|
+
Layer.succeed(CredentialsService, new ConfiguredCredentialsService(credentials));
|
|
58
|
+
|
|
59
|
+
static layerConfig = (credentials: { service: string; apiKey: Config.Config<Redacted.Redacted<string>> }[]) =>
|
|
60
|
+
Layer.effect(
|
|
61
|
+
CredentialsService,
|
|
62
|
+
Effect.gen(function* () {
|
|
63
|
+
const serviceCredentials = yield* Effect.forEach(credentials, ({ service, apiKey }) =>
|
|
64
|
+
Effect.gen(function* () {
|
|
65
|
+
return {
|
|
66
|
+
service,
|
|
67
|
+
apiKey: Redacted.value(yield* apiKey),
|
|
68
|
+
};
|
|
69
|
+
}),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return new ConfiguredCredentialsService(serviceCredentials);
|
|
73
|
+
}),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
static layerFromDatabase = () =>
|
|
77
|
+
Layer.effect(
|
|
78
|
+
CredentialsService,
|
|
79
|
+
Effect.gen(function* () {
|
|
80
|
+
const dbService = yield* DatabaseService;
|
|
81
|
+
const queryCredentials = async (query: CredentialQuery): Promise<ServiceCredential[]> => {
|
|
82
|
+
const { objects: accessTokens } = await dbService.db.query(Query.type(DataType.AccessToken)).run();
|
|
83
|
+
return accessTokens
|
|
84
|
+
.filter((accessToken) => accessToken.source === query.service)
|
|
85
|
+
.map((accessToken) => ({
|
|
86
|
+
service: accessToken.source,
|
|
87
|
+
apiKey: accessToken.token,
|
|
88
|
+
}));
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
getCredential: async (query) => {
|
|
92
|
+
const credentials = await queryCredentials(query);
|
|
93
|
+
if (credentials.length === 0) {
|
|
94
|
+
throw new Error(`Credential not found for service: ${query.service}`);
|
|
95
|
+
}
|
|
96
|
+
return credentials[0];
|
|
97
|
+
},
|
|
98
|
+
queryCredentials: async (query) => {
|
|
99
|
+
return queryCredentials(query);
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}),
|
|
103
|
+
);
|
|
40
104
|
}
|
|
41
105
|
|
|
42
106
|
export class ConfiguredCredentialsService implements Context.Tag.Service<CredentialsService> {
|
|
@@ -59,3 +123,15 @@ export class ConfiguredCredentialsService implements Context.Tag.Service<Credent
|
|
|
59
123
|
return credential;
|
|
60
124
|
}
|
|
61
125
|
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Maps the request to include the API key from the credential.
|
|
129
|
+
*/
|
|
130
|
+
export const withAuthorization = (query: CredentialQuery, kind?: 'Bearer' | 'Basic') =>
|
|
131
|
+
HttpClient.mapRequestEffect(
|
|
132
|
+
Effect.fnUntraced(function* (request) {
|
|
133
|
+
const key = yield* CredentialsService.getApiKey(query).pipe(Effect.map(Redacted.value));
|
|
134
|
+
const authorization = kind ? `${kind} ${key}` : key;
|
|
135
|
+
return HttpClientRequest.setHeader(request, 'Authorization', authorization);
|
|
136
|
+
}),
|
|
137
|
+
);
|
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,114 @@
|
|
|
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, FunctionNotFoundError } 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
|
+
/**
|
|
23
|
+
* @deprecated Use layerLive instead.
|
|
24
|
+
*/
|
|
25
|
+
static layer = Layer.succeed(LocalFunctionExecutionService, {
|
|
26
|
+
invokeFunction: (functionDef, input) => invokeFunction(functionDef, input),
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
static layerLive = Layer.effect(
|
|
30
|
+
LocalFunctionExecutionService,
|
|
31
|
+
Effect.gen(function* () {
|
|
32
|
+
const resolver = yield* FunctionImplementationResolver;
|
|
33
|
+
return {
|
|
34
|
+
invokeFunction: Effect.fn('invokeFunction')(function* (functionDef, input) {
|
|
35
|
+
// TODO(dmaretskyi): Better error types
|
|
36
|
+
const resolved = yield* resolver.resolveFunctionImplementation(functionDef).pipe(Effect.orDie);
|
|
37
|
+
return yield* invokeFunction(resolved, input);
|
|
38
|
+
}),
|
|
39
|
+
};
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
static invokeFunction: <F extends FunctionDefinition.Any>(
|
|
44
|
+
functionDef: F,
|
|
45
|
+
input: FunctionDefinition.Input<F>,
|
|
46
|
+
) => Effect.Effect<FunctionDefinition.Output<F>, never, Services | LocalFunctionExecutionService> =
|
|
47
|
+
Effect.serviceFunctionEffect(LocalFunctionExecutionService, (_) => _.invokeFunction as any);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const invokeFunction = (
|
|
51
|
+
functionDef: FunctionDefinition<any, any>,
|
|
52
|
+
input: any,
|
|
53
|
+
): Effect.Effect<unknown, never, Services> =>
|
|
54
|
+
Effect.gen(function* () {
|
|
55
|
+
// Assert input matches schema
|
|
56
|
+
const assertInput = functionDef.inputSchema.pipe(Schema.asserts);
|
|
57
|
+
(assertInput as any)(input);
|
|
58
|
+
|
|
59
|
+
const context: FunctionContext = {
|
|
60
|
+
space: undefined,
|
|
61
|
+
getService: () => todo(),
|
|
62
|
+
getSpace: async (_spaceId: any) => {
|
|
63
|
+
throw new Error('Not available. Use the database service instead.');
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// TODO(dmaretskyi): This should be delegated to a function invoker service.
|
|
68
|
+
const data = yield* Effect.gen(function* () {
|
|
69
|
+
const result = functionDef.handler({ context, data: input });
|
|
70
|
+
if (Effect.isEffect(result)) {
|
|
71
|
+
return yield* (result as Effect.Effect<unknown, unknown, Services>).pipe(Effect.orDie);
|
|
72
|
+
} else if (
|
|
73
|
+
typeof result === 'object' &&
|
|
74
|
+
result !== null &&
|
|
75
|
+
'then' in result &&
|
|
76
|
+
typeof result.then === 'function'
|
|
77
|
+
) {
|
|
78
|
+
return yield* Effect.promise(() => result);
|
|
79
|
+
} else {
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
}).pipe(
|
|
83
|
+
Effect.orDie,
|
|
84
|
+
Effect.catchAllDefect((defect) =>
|
|
85
|
+
Effect.die(new FunctionError('Error running function', { context: { name: functionDef.name }, cause: defect })),
|
|
86
|
+
),
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
// Assert output matches schema
|
|
90
|
+
const assertOutput = functionDef.outputSchema?.pipe(Schema.asserts);
|
|
91
|
+
(assertOutput as any)(data);
|
|
92
|
+
|
|
93
|
+
return data;
|
|
94
|
+
}).pipe(Effect.withSpan('invokeFunction', { attributes: { name: functionDef.name } }));
|
|
95
|
+
|
|
96
|
+
export class FunctionImplementationResolver extends Context.Tag('@dxos/functions/FunctionImplementationResolver')<
|
|
97
|
+
FunctionImplementationResolver,
|
|
98
|
+
{
|
|
99
|
+
resolveFunctionImplementation(
|
|
100
|
+
functionDef: FunctionDefinition<any, any>,
|
|
101
|
+
): Effect.Effect<FunctionDefinition<any, any>, FunctionNotFoundError>;
|
|
102
|
+
}
|
|
103
|
+
>() {
|
|
104
|
+
static layerTest = ({ functions }: { functions: FunctionDefinition<any, any>[] }) =>
|
|
105
|
+
Layer.succeed(FunctionImplementationResolver, {
|
|
106
|
+
resolveFunctionImplementation: (functionDef) => {
|
|
107
|
+
const resolved = functions.find((f) => f.name === functionDef.name);
|
|
108
|
+
if (!resolved) {
|
|
109
|
+
return Effect.fail(new FunctionNotFoundError(functionDef.name));
|
|
110
|
+
}
|
|
111
|
+
return Effect.succeed(resolved);
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
}
|
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,60 @@ 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)));
|
|
65
|
+
|
|
66
|
+
static append = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(
|
|
67
|
+
queue: Queue<T>,
|
|
68
|
+
objects: T[],
|
|
69
|
+
): Effect.Effect<void> => Effect.promise(() => queue.append(objects));
|
|
45
70
|
}
|
|
46
71
|
|
|
47
72
|
/**
|
|
48
73
|
* Gives access to a specific queue passed as a context.
|
|
49
74
|
*/
|
|
50
|
-
export class ContextQueueService extends Context.Tag('ContextQueueService')<
|
|
75
|
+
export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQueueService')<
|
|
51
76
|
ContextQueueService,
|
|
52
77
|
{
|
|
53
|
-
readonly
|
|
78
|
+
readonly queue: Queue;
|
|
54
79
|
}
|
|
55
|
-
>() {
|
|
80
|
+
>() {
|
|
81
|
+
static layer = (queue: Queue) => Layer.succeed(ContextQueueService, { queue });
|
|
82
|
+
}
|
|
@@ -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
|
+
}
|