@dxos/functions 0.8.4-main.b97322e → 0.8.4-main.bc2380dfbc
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/LICENSE +102 -5
- package/README.md +5 -7
- package/dist/lib/neutral/index.mjs +599 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/types/src/index.d.ts +3 -7
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
- package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
- package/dist/types/src/protocol/functions-ai-http-client.test.d.ts +2 -0
- package/dist/types/src/protocol/functions-ai-http-client.test.d.ts.map +1 -0
- package/dist/types/src/protocol/index.d.ts +2 -0
- package/dist/types/src/protocol/index.d.ts.map +1 -0
- package/dist/types/src/protocol/protocol.d.ts +24 -0
- package/dist/types/src/protocol/protocol.d.ts.map +1 -0
- package/dist/types/src/protocol/protocol.test.d.ts +2 -0
- package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
- package/dist/types/src/sdk.d.ts +10 -0
- package/dist/types/src/sdk.d.ts.map +1 -0
- package/dist/types/src/services/credentials.d.ts +24 -28
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +15 -0
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
- package/dist/types/src/services/index.d.ts +3 -6
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +1 -29
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +2 -22
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/types/index.d.ts +2 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/src/types/url.d.ts +13 -0
- package/dist/types/src/types/url.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +29 -71
- package/src/index.ts +3 -9
- package/src/protocol/functions-ai-http-client.test.ts +105 -0
- package/src/protocol/functions-ai-http-client.ts +141 -0
- package/src/{executor → protocol}/index.ts +1 -1
- package/src/protocol/protocol.test.ts +58 -0
- package/src/protocol/protocol.ts +452 -0
- package/src/sdk.ts +31 -0
- package/src/services/credentials.ts +95 -41
- package/src/services/function-invocation-service.ts +33 -0
- package/src/services/index.ts +3 -6
- package/src/services/queues.ts +1 -51
- package/src/services/tracing.ts +2 -36
- package/src/types/index.ts +5 -0
- package/src/types/url.ts +32 -0
- package/dist/lib/browser/bundler/index.mjs +0 -247
- package/dist/lib/browser/bundler/index.mjs.map +0 -7
- package/dist/lib/browser/chunk-3NGCSUEW.mjs +0 -328
- package/dist/lib/browser/chunk-3NGCSUEW.mjs.map +0 -7
- package/dist/lib/browser/edge/index.mjs +0 -69
- package/dist/lib/browser/edge/index.mjs.map +0 -7
- package/dist/lib/browser/index.mjs +0 -503
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/testing/index.mjs +0 -91
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/bundler/index.mjs +0 -249
- package/dist/lib/node-esm/bundler/index.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/lib/node-esm/edge/index.mjs +0 -71
- package/dist/lib/node-esm/edge/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -504
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/testing/index.mjs +0 -92
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/bundler/bundler.d.ts +0 -50
- package/dist/types/src/bundler/bundler.d.ts.map +0 -1
- package/dist/types/src/bundler/bundler.test.d.ts +0 -2
- package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
- package/dist/types/src/bundler/index.d.ts +0 -2
- package/dist/types/src/bundler/index.d.ts.map +0 -1
- package/dist/types/src/edge/functions.d.ts +0 -16
- package/dist/types/src/edge/functions.d.ts.map +0 -1
- package/dist/types/src/edge/index.d.ts +0 -2
- package/dist/types/src/edge/index.d.ts.map +0 -1
- package/dist/types/src/errors.d.ts +0 -68
- package/dist/types/src/errors.d.ts.map +0 -1
- package/dist/types/src/executor/executor.d.ts +0 -8
- package/dist/types/src/executor/executor.d.ts.map +0 -1
- package/dist/types/src/executor/index.d.ts +0 -2
- package/dist/types/src/executor/index.d.ts.map +0 -1
- package/dist/types/src/handler.d.ts +0 -62
- package/dist/types/src/handler.d.ts.map +0 -1
- package/dist/types/src/schema.d.ts +0 -38
- package/dist/types/src/schema.d.ts.map +0 -1
- package/dist/types/src/services/database.d.ts +0 -29
- package/dist/types/src/services/database.d.ts.map +0 -1
- package/dist/types/src/services/event-logger.d.ts +0 -37
- package/dist/types/src/services/event-logger.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/dist/types/src/services/service-container.d.ts +0 -56
- package/dist/types/src/services/service-container.d.ts.map +0 -1
- package/dist/types/src/services/service-registry.d.ts +0 -29
- package/dist/types/src/services/service-registry.d.ts.map +0 -1
- package/dist/types/src/services/service-registry.test.d.ts +0 -2
- package/dist/types/src/services/service-registry.test.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -3
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/layer.d.ts +0 -10
- package/dist/types/src/testing/layer.d.ts.map +0 -1
- package/dist/types/src/testing/logger.d.ts +0 -5
- package/dist/types/src/testing/logger.d.ts.map +0 -1
- package/dist/types/src/testing/services.d.ts +0 -70
- package/dist/types/src/testing/services.d.ts.map +0 -1
- package/dist/types/src/trace.d.ts +0 -124
- package/dist/types/src/trace.d.ts.map +0 -1
- package/dist/types/src/translations.d.ts +0 -12
- package/dist/types/src/translations.d.ts.map +0 -1
- package/dist/types/src/types.d.ts +0 -411
- package/dist/types/src/types.d.ts.map +0 -1
- package/dist/types/src/url.d.ts +0 -17
- package/dist/types/src/url.d.ts.map +0 -1
- package/src/bundler/bundler.test.ts +0 -59
- package/src/bundler/bundler.ts +0 -292
- package/src/bundler/index.ts +0 -5
- package/src/edge/functions.ts +0 -64
- package/src/edge/index.ts +0 -9
- package/src/errors.ts +0 -13
- package/src/executor/executor.ts +0 -54
- package/src/handler.ts +0 -120
- package/src/schema.ts +0 -57
- package/src/services/database.ts +0 -70
- package/src/services/event-logger.ts +0 -87
- package/src/services/function-call-service.ts +0 -64
- package/src/services/service-container.ts +0 -113
- package/src/services/service-registry.test.ts +0 -42
- package/src/services/service-registry.ts +0 -56
- package/src/testing/index.ts +0 -6
- package/src/testing/layer.ts +0 -31
- package/src/testing/logger.ts +0 -16
- package/src/testing/services.ts +0 -168
- package/src/trace.ts +0 -180
- package/src/translations.ts +0 -20
- package/src/types.ts +0 -211
- package/src/url.ts +0 -52
package/src/schema.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Schema } from 'effect';
|
|
6
|
-
|
|
7
|
-
import { Type } from '@dxos/echo';
|
|
8
|
-
import { JsonSchemaType, LabelAnnotation, Ref } from '@dxos/echo-schema';
|
|
9
|
-
import { DataType } from '@dxos/schema';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Source script.
|
|
13
|
-
*/
|
|
14
|
-
export const ScriptType = Schema.Struct({
|
|
15
|
-
name: Schema.optional(Schema.String),
|
|
16
|
-
description: Schema.optional(Schema.String),
|
|
17
|
-
// TODO(burdon): Change to hash of deployed content.
|
|
18
|
-
// Whether source has changed since last deploy.
|
|
19
|
-
changed: Schema.optional(Schema.Boolean),
|
|
20
|
-
source: Ref(DataType.Text),
|
|
21
|
-
}).pipe(
|
|
22
|
-
Type.Obj({
|
|
23
|
-
typename: 'dxos.org/type/Script',
|
|
24
|
-
version: '0.1.0',
|
|
25
|
-
}),
|
|
26
|
-
LabelAnnotation.set(['name']),
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
export interface ScriptType extends Schema.Schema.Type<typeof ScriptType> {}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Function deployment.
|
|
33
|
-
*/
|
|
34
|
-
export const FunctionType = Schema.Struct({
|
|
35
|
-
// TODO(burdon): Rename to id/uri?
|
|
36
|
-
name: Schema.NonEmptyString,
|
|
37
|
-
version: Schema.String,
|
|
38
|
-
|
|
39
|
-
description: Schema.optional(Schema.String),
|
|
40
|
-
|
|
41
|
-
// Reference to a source script if it exists within ECHO.
|
|
42
|
-
// TODO(burdon): Don't ref ScriptType directly (core).
|
|
43
|
-
source: Schema.optional(Ref(ScriptType)),
|
|
44
|
-
|
|
45
|
-
inputSchema: Schema.optional(JsonSchemaType),
|
|
46
|
-
outputSchema: Schema.optional(JsonSchemaType),
|
|
47
|
-
|
|
48
|
-
// Local binding to a function name.
|
|
49
|
-
binding: Schema.optional(Schema.String),
|
|
50
|
-
}).pipe(
|
|
51
|
-
Type.Obj({
|
|
52
|
-
typename: 'dxos.org/type/Function',
|
|
53
|
-
version: '0.1.0',
|
|
54
|
-
}),
|
|
55
|
-
LabelAnnotation.set(['name']),
|
|
56
|
-
);
|
|
57
|
-
export interface FunctionType extends Schema.Schema.Type<typeof FunctionType> {}
|
package/src/services/database.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Context, Effect, Layer } from 'effect';
|
|
6
|
-
|
|
7
|
-
import type { Filter, Live, Obj, Query, Ref, Relation } from '@dxos/echo';
|
|
8
|
-
import type { EchoDatabase, OneShotQueryResult, QueryResult } from '@dxos/echo-db';
|
|
9
|
-
import type { DXN } from '@dxos/keys';
|
|
10
|
-
|
|
11
|
-
export class DatabaseService extends Context.Tag('DatabaseService')<
|
|
12
|
-
DatabaseService,
|
|
13
|
-
{
|
|
14
|
-
readonly db: EchoDatabase;
|
|
15
|
-
}
|
|
16
|
-
>() {
|
|
17
|
-
static notAvailable = Layer.succeed(DatabaseService, {
|
|
18
|
-
get db(): EchoDatabase {
|
|
19
|
-
throw new Error('Database not available');
|
|
20
|
-
},
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
static make = (db: EchoDatabase): Context.Tag.Service<DatabaseService> => {
|
|
24
|
-
return {
|
|
25
|
-
get db() {
|
|
26
|
-
return db;
|
|
27
|
-
},
|
|
28
|
-
};
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
static resolve: (dxn: DXN) => Effect.Effect<Obj.Any | Relation.Any, Error, DatabaseService> = Effect.fn(
|
|
32
|
-
function* (dxn) {
|
|
33
|
-
const { db } = yield* DatabaseService;
|
|
34
|
-
return yield* Effect.tryPromise({
|
|
35
|
-
try: () =>
|
|
36
|
-
db.graph.createRefResolver({ context: { space: db.spaceId } }).resolve(dxn) as Promise<
|
|
37
|
-
Obj.Any | Relation.Any
|
|
38
|
-
>,
|
|
39
|
-
catch: (error) => error as Error,
|
|
40
|
-
});
|
|
41
|
-
},
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
static loadRef: <T>(ref: Ref.Ref<T>) => Effect.Effect<T, never, never> = Effect.fn(function* (ref) {
|
|
45
|
-
return yield* Effect.promise(() => ref.load());
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Creates a `QueryResult` object that can be subscribed to.
|
|
50
|
-
*/
|
|
51
|
-
static query: {
|
|
52
|
-
<Q extends Query.Any>(query: Q): Effect.Effect<QueryResult<Live<Query.Type<Q>>>, never, DatabaseService>;
|
|
53
|
-
<F extends Filter.Any>(filter: F): Effect.Effect<QueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;
|
|
54
|
-
} = (queryOrFilter: Query.Any | Filter.Any) =>
|
|
55
|
-
DatabaseService.pipe(
|
|
56
|
-
Effect.map(({ db }) => db.query(queryOrFilter as any)),
|
|
57
|
-
Effect.withSpan('DatabaseService.query'),
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Executes the query once and returns the results.
|
|
62
|
-
*/
|
|
63
|
-
static runQuery: {
|
|
64
|
-
<Q extends Query.Any>(query: Q): Effect.Effect<OneShotQueryResult<Live<Query.Type<Q>>>, never, DatabaseService>;
|
|
65
|
-
<F extends Filter.Any>(filter: F): Effect.Effect<OneShotQueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;
|
|
66
|
-
} = (queryOrFilter: Query.Any | Filter.Any) =>
|
|
67
|
-
DatabaseService.query(queryOrFilter as any).pipe(
|
|
68
|
-
Effect.flatMap((queryResult) => Effect.promise(() => queryResult.run())),
|
|
69
|
-
);
|
|
70
|
-
}
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Effect, Context } from 'effect';
|
|
6
|
-
|
|
7
|
-
import { invariant } from '@dxos/invariant';
|
|
8
|
-
import { log, LogLevel } from '@dxos/log';
|
|
9
|
-
|
|
10
|
-
export type ComputeEvent =
|
|
11
|
-
| {
|
|
12
|
-
type: 'begin-compute';
|
|
13
|
-
nodeId: string;
|
|
14
|
-
inputs: Record<string, any>;
|
|
15
|
-
}
|
|
16
|
-
| {
|
|
17
|
-
type: 'end-compute';
|
|
18
|
-
nodeId: string;
|
|
19
|
-
outputs: Record<string, any>;
|
|
20
|
-
}
|
|
21
|
-
| {
|
|
22
|
-
type: 'compute-input';
|
|
23
|
-
nodeId: string;
|
|
24
|
-
property: string;
|
|
25
|
-
value: any;
|
|
26
|
-
}
|
|
27
|
-
| {
|
|
28
|
-
type: 'compute-output';
|
|
29
|
-
nodeId: string;
|
|
30
|
-
property: string;
|
|
31
|
-
value: any;
|
|
32
|
-
}
|
|
33
|
-
| {
|
|
34
|
-
type: 'custom';
|
|
35
|
-
nodeId: string;
|
|
36
|
-
event: any;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export class EventLogger extends Context.Tag('EventLogger')<
|
|
40
|
-
EventLogger,
|
|
41
|
-
{ readonly log: (event: ComputeEvent) => void; readonly nodeId: string | undefined }
|
|
42
|
-
>() {
|
|
43
|
-
static noop: Context.Tag.Service<EventLogger> = {
|
|
44
|
-
log: () => {},
|
|
45
|
-
nodeId: undefined,
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export const logCustomEvent = (data: any) =>
|
|
50
|
-
Effect.gen(function* () {
|
|
51
|
-
const logger = yield* EventLogger;
|
|
52
|
-
if (!logger.nodeId) {
|
|
53
|
-
throw new Error('logCustomEvent must be called within a node compute function');
|
|
54
|
-
}
|
|
55
|
-
logger.log({
|
|
56
|
-
type: 'custom',
|
|
57
|
-
nodeId: logger.nodeId,
|
|
58
|
-
event: data,
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
export const createDefectLogger = <A, E, R>(): ((self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>) =>
|
|
63
|
-
Effect.catchAll((error) =>
|
|
64
|
-
Effect.gen(function* () {
|
|
65
|
-
log.error('unhandled effect error', { error });
|
|
66
|
-
throw error;
|
|
67
|
-
}),
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
export const createEventLogger = (level: LogLevel, message: string = 'event'): Context.Tag.Service<EventLogger> => {
|
|
71
|
-
const logFunction = (
|
|
72
|
-
{
|
|
73
|
-
[LogLevel.WARN]: log.warn,
|
|
74
|
-
[LogLevel.VERBOSE]: log.verbose,
|
|
75
|
-
[LogLevel.DEBUG]: log.debug,
|
|
76
|
-
[LogLevel.INFO]: log.info,
|
|
77
|
-
[LogLevel.ERROR]: log.error,
|
|
78
|
-
} as any
|
|
79
|
-
)[level];
|
|
80
|
-
invariant(logFunction);
|
|
81
|
-
return {
|
|
82
|
-
log: (event: ComputeEvent) => {
|
|
83
|
-
logFunction(message, event);
|
|
84
|
-
},
|
|
85
|
-
nodeId: undefined,
|
|
86
|
-
};
|
|
87
|
-
};
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Context } from 'effect';
|
|
6
|
-
|
|
7
|
-
import type { SpaceId } from '@dxos/keys';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Allows calling into other functions.
|
|
11
|
-
*/
|
|
12
|
-
export class FunctionCallService extends Context.Tag('FunctionCallService')<
|
|
13
|
-
FunctionCallService,
|
|
14
|
-
{
|
|
15
|
-
callFunction(deployedFunctionId: string, input: any, spaceId?: SpaceId): Promise<any>;
|
|
16
|
-
}
|
|
17
|
-
>() {
|
|
18
|
-
static fromClient(baseUrl: string, spaceId: SpaceId): Context.Tag.Service<FunctionCallService> {
|
|
19
|
-
return {
|
|
20
|
-
callFunction: async (deployedFunctionId: string, input: any) => {
|
|
21
|
-
const url = getInvocationUrl(deployedFunctionId, baseUrl, { spaceId });
|
|
22
|
-
const result = await fetch(url, {
|
|
23
|
-
method: 'POST',
|
|
24
|
-
headers: { 'Content-Type': 'application/json' },
|
|
25
|
-
body: JSON.stringify(input),
|
|
26
|
-
});
|
|
27
|
-
if (result.status >= 300 || result.status < 200) {
|
|
28
|
-
throw new Error('Failed to invoke function', { cause: new Error(`HTTP error: ${await result.text()}`) });
|
|
29
|
-
}
|
|
30
|
-
return await result.json();
|
|
31
|
-
},
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
static mock = () => {
|
|
36
|
-
return {
|
|
37
|
-
callFunction: async (deployedFunctionId: string, input: any) => {
|
|
38
|
-
return input;
|
|
39
|
-
},
|
|
40
|
-
};
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// TODO(dmaretskyi): Reconcile with `getInvocationUrl` in `@dxos/functions/edge`.
|
|
45
|
-
const getInvocationUrl = (functionUrl: string, edgeUrl: string, options: InvocationOptions = {}) => {
|
|
46
|
-
const baseUrl = new URL('functions/', edgeUrl);
|
|
47
|
-
|
|
48
|
-
// Leading slashes cause the URL to be treated as an absolute path.
|
|
49
|
-
const relativeUrl = functionUrl.replace(/^\//, '');
|
|
50
|
-
const url = new URL(`./${relativeUrl}`, baseUrl.toString());
|
|
51
|
-
options.spaceId && url.searchParams.set('spaceId', options.spaceId);
|
|
52
|
-
options.subjectId && url.searchParams.set('subjectId', options.subjectId);
|
|
53
|
-
url.protocol = isSecure(url.protocol) ? 'https' : 'http';
|
|
54
|
-
return url.toString();
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const isSecure = (protocol: string) => {
|
|
58
|
-
return protocol === 'https:' || protocol === 'wss:';
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
type InvocationOptions = {
|
|
62
|
-
spaceId?: SpaceId;
|
|
63
|
-
subjectId?: string;
|
|
64
|
-
};
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { type Context, Layer } from 'effect';
|
|
6
|
-
|
|
7
|
-
import { AiService } from '@dxos/ai';
|
|
8
|
-
import { entries } from '@dxos/util';
|
|
9
|
-
|
|
10
|
-
import { ConfiguredCredentialsService, CredentialsService } from './credentials';
|
|
11
|
-
import { DatabaseService } from './database';
|
|
12
|
-
import { EventLogger } from './event-logger';
|
|
13
|
-
import { FunctionCallService } from './function-call-service';
|
|
14
|
-
import { QueueService } from './queues';
|
|
15
|
-
import { TracingService } from './tracing';
|
|
16
|
-
|
|
17
|
-
// TODO(dmaretskyi): Refactor this module to only rely on tags and not the human-assigned names.
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* List of all services.
|
|
21
|
-
*/
|
|
22
|
-
const SERVICES = {
|
|
23
|
-
ai: AiService,
|
|
24
|
-
credentials: CredentialsService,
|
|
25
|
-
database: DatabaseService,
|
|
26
|
-
eventLogger: EventLogger,
|
|
27
|
-
functionCallService: FunctionCallService,
|
|
28
|
-
queues: QueueService,
|
|
29
|
-
tracing: TracingService,
|
|
30
|
-
} as const satisfies Record<string, Context.TagClass<any, string, any>>;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Mapping of service names to their tags.
|
|
34
|
-
*/
|
|
35
|
-
export type ServiceTagRecord = {
|
|
36
|
-
[K in keyof typeof SERVICES]: (typeof SERVICES)[K] extends { new (_: never): infer T } ? T : never;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Mapping of service names to their runtime types.
|
|
41
|
-
*/
|
|
42
|
-
export type ServiceRecord = {
|
|
43
|
-
[K in keyof ServiceTagRecord]: Context.Tag.Service<ServiceTagRecord[K]>;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Union of all services tags.
|
|
48
|
-
*/
|
|
49
|
-
export type Services = ServiceTagRecord[keyof ServiceTagRecord];
|
|
50
|
-
|
|
51
|
-
const SERVICE_MAPPING: Record<string, keyof ServiceRecord> = Object.fromEntries(
|
|
52
|
-
entries(SERVICES).map(([name, tag]) => [tag.key, name]),
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
export const SERVICE_TAGS: Context.Tag<any, any>[] = Object.values(SERVICES);
|
|
56
|
-
|
|
57
|
-
const DEFAULT_SERVICES: Partial<ServiceRecord> = {
|
|
58
|
-
tracing: TracingService.noop,
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* @deprecated
|
|
63
|
-
*/
|
|
64
|
-
export class ServiceContainer {
|
|
65
|
-
private _services: Partial<ServiceRecord> = { ...DEFAULT_SERVICES };
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Set services.
|
|
69
|
-
* @param services - Services to set.
|
|
70
|
-
* @returns The container instance.
|
|
71
|
-
*/
|
|
72
|
-
setServices(services: Partial<ServiceRecord>): this {
|
|
73
|
-
this._services = { ...this._services, ...services };
|
|
74
|
-
return this;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
getService<Id, T>(tag: Context.Tag<Id, T>): T {
|
|
78
|
-
const serviceKey = SERVICE_MAPPING[tag.key];
|
|
79
|
-
const service = serviceKey != null ? this._services[serviceKey] : undefined;
|
|
80
|
-
if (!service) {
|
|
81
|
-
throw new Error(`Service not available: ${tag.key}`);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return service as T;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
clone(): ServiceContainer {
|
|
88
|
-
return new ServiceContainer().setServices({ ...this._services });
|
|
89
|
-
}
|
|
90
|
-
|
|
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
|
-
createLayer(): Layer.Layer<Services> {
|
|
93
|
-
const ai = this._services.ai != null ? Layer.succeed(AiService, this._services.ai) : AiService.notAvailable;
|
|
94
|
-
const credentials = Layer.succeed(
|
|
95
|
-
CredentialsService,
|
|
96
|
-
this._services.credentials ?? new ConfiguredCredentialsService(),
|
|
97
|
-
);
|
|
98
|
-
const database =
|
|
99
|
-
this._services.database != null
|
|
100
|
-
? Layer.succeed(DatabaseService, this._services.database)
|
|
101
|
-
: DatabaseService.notAvailable;
|
|
102
|
-
const queues =
|
|
103
|
-
this._services.queues != null ? Layer.succeed(QueueService, this._services.queues) : QueueService.notAvailable;
|
|
104
|
-
const tracing = Layer.succeed(TracingService, this._services.tracing ?? TracingService.noop);
|
|
105
|
-
const eventLogger = Layer.succeed(EventLogger, this._services.eventLogger ?? EventLogger.noop);
|
|
106
|
-
const functionCallService = Layer.succeed(
|
|
107
|
-
FunctionCallService,
|
|
108
|
-
this._services.functionCallService ?? FunctionCallService.mock(),
|
|
109
|
-
);
|
|
110
|
-
|
|
111
|
-
return Layer.mergeAll(ai, credentials, database, queues, tracing, eventLogger, functionCallService);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { describe, it } from '@effect/vitest';
|
|
6
|
-
import { Context, Effect, Layer, Option } from 'effect';
|
|
7
|
-
|
|
8
|
-
import { ServiceRegistry } from './service-registry';
|
|
9
|
-
|
|
10
|
-
class MyTag extends Context.Tag('MyTag')<MyTag, { value: string }>() {}
|
|
11
|
-
|
|
12
|
-
const mockRegistry = Layer.succeed(ServiceRegistry, {
|
|
13
|
-
resolve: (tag) => ((tag as any) === MyTag ? Option.some({ value: 'test' } as any) : Option.none()),
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
describe('ServiceRegistry', () => {
|
|
17
|
-
it.effect(
|
|
18
|
-
'provide',
|
|
19
|
-
Effect.fn(function* ({ expect }) {
|
|
20
|
-
const body = Effect.gen(function* () {
|
|
21
|
-
const { value } = yield* MyTag;
|
|
22
|
-
expect(value).toEqual('test');
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
const eff = body.pipe(ServiceRegistry.provide(MyTag));
|
|
26
|
-
yield* eff;
|
|
27
|
-
}, Effect.provide(mockRegistry)),
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
it.effect(
|
|
31
|
-
'provide or die',
|
|
32
|
-
Effect.fn(function* ({ expect }) {
|
|
33
|
-
const body = Effect.gen(function* () {
|
|
34
|
-
const { value } = yield* MyTag;
|
|
35
|
-
expect(value).toEqual('test');
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
const eff = body.pipe(ServiceRegistry.provideOrDie(MyTag));
|
|
39
|
-
yield* eff;
|
|
40
|
-
}, Effect.provide(mockRegistry)),
|
|
41
|
-
);
|
|
42
|
-
});
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Context, Effect, flow, type Option } from 'effect';
|
|
6
|
-
|
|
7
|
-
import { ServiceNotAvailableError } from '../errors';
|
|
8
|
-
|
|
9
|
-
export namespace ServiceRegistry {
|
|
10
|
-
export interface Service {
|
|
11
|
-
resolve: <T extends Context.Tag<any, any>>(tag: T) => Option.Option<Context.Tag.Service<T>>;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export class ServiceRegistry extends Context.Tag('ServiceRegistry')<ServiceRegistry, ServiceRegistry.Service>() {
|
|
16
|
-
/**
|
|
17
|
-
* Resolves the service from the registry.
|
|
18
|
-
* @param tag Service tag to resolve.
|
|
19
|
-
* @throws {@link ServiceNotAvailableError} if the service is not found.
|
|
20
|
-
* @returns Effect that resolve to the service.
|
|
21
|
-
*/
|
|
22
|
-
static resolve: <T extends Context.Tag<any, any>>(
|
|
23
|
-
tag: T,
|
|
24
|
-
) => Effect.Effect<T, ServiceNotAvailableError, ServiceRegistry> = (tag) =>
|
|
25
|
-
ServiceRegistry.pipe(
|
|
26
|
-
Effect.flatMap((_) => _.resolve(tag)),
|
|
27
|
-
Effect.mapError(() => new ServiceNotAvailableError(tag.key)),
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
static provide: {
|
|
31
|
-
<Tags extends [Context.Tag<any, any>, ...Context.Tag<any, any>[]]>(
|
|
32
|
-
...tags: Tags
|
|
33
|
-
): <A, E, R>(
|
|
34
|
-
effect: Effect.Effect<A, E, R>,
|
|
35
|
-
) => Effect.Effect<
|
|
36
|
-
A,
|
|
37
|
-
E | ServiceNotAvailableError,
|
|
38
|
-
Exclude<R, { [K in keyof Tags]: Context.Tag.Identifier<Tags[K]> }[number]> | ServiceRegistry
|
|
39
|
-
>;
|
|
40
|
-
} = (...tags) => (flow as any)(...tags.map((tag) => Effect.provideServiceEffect(tag, ServiceRegistry.resolve(tag))));
|
|
41
|
-
|
|
42
|
-
static provideOrDie: {
|
|
43
|
-
<Tags extends [Context.Tag<any, any>, ...Context.Tag<any, any>[]]>(
|
|
44
|
-
...tags: Tags
|
|
45
|
-
): <A, E, R>(
|
|
46
|
-
effect: Effect.Effect<A, E, R>,
|
|
47
|
-
) => Effect.Effect<
|
|
48
|
-
A,
|
|
49
|
-
E,
|
|
50
|
-
Exclude<R, { [K in keyof Tags]: Context.Tag.Identifier<Tags[K]> }[number]> | ServiceRegistry
|
|
51
|
-
>;
|
|
52
|
-
} = (...tags) =>
|
|
53
|
-
(flow as any)(
|
|
54
|
-
...tags.map((tag) => Effect.provideServiceEffect(tag, ServiceRegistry.resolve(tag).pipe(Effect.orDie))),
|
|
55
|
-
);
|
|
56
|
-
}
|
package/src/testing/index.ts
DELETED
package/src/testing/layer.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Context, Effect, Layer } from 'effect';
|
|
6
|
-
import type { Schema } from 'effect';
|
|
7
|
-
|
|
8
|
-
import { EchoTestBuilder } from '@dxos/echo-db/testing';
|
|
9
|
-
import type { EchoHostIndexingConfig } from '@dxos/echo-pipeline';
|
|
10
|
-
import { accuireReleaseResource } from '@dxos/effect';
|
|
11
|
-
|
|
12
|
-
import { DatabaseService, QueueService } from '../services';
|
|
13
|
-
|
|
14
|
-
const testBuilder = accuireReleaseResource(() => new EchoTestBuilder());
|
|
15
|
-
|
|
16
|
-
export type TestDatabaseOptions = {
|
|
17
|
-
indexing?: Partial<EchoHostIndexingConfig>;
|
|
18
|
-
types?: Schema.Schema.AnyNoContext[];
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export const TestDatabaseLayer = ({ indexing, types }: TestDatabaseOptions = {}) =>
|
|
22
|
-
Layer.scopedContext(
|
|
23
|
-
Effect.gen(function* () {
|
|
24
|
-
const builder = yield* testBuilder;
|
|
25
|
-
const { db, queues } = yield* Effect.promise(() => builder.createDatabase({ indexing, types }));
|
|
26
|
-
return Context.mergeAll(
|
|
27
|
-
Context.make(DatabaseService, DatabaseService.make(db)),
|
|
28
|
-
Context.make(QueueService, QueueService.make(queues, undefined)),
|
|
29
|
-
);
|
|
30
|
-
}),
|
|
31
|
-
);
|
package/src/testing/logger.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Effect, type Context } from 'effect';
|
|
6
|
-
|
|
7
|
-
import { LogLevel } from '@dxos/log';
|
|
8
|
-
|
|
9
|
-
import { type EventLogger, createEventLogger } from '../services';
|
|
10
|
-
|
|
11
|
-
export const noopLogger: Context.Tag.Service<EventLogger> = {
|
|
12
|
-
log: () => Effect.succeed(undefined),
|
|
13
|
-
nodeId: undefined,
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export const consoleLogger: Context.Tag.Service<EventLogger> = createEventLogger(LogLevel.INFO);
|