@dxos/functions 0.8.4-main.dedc0f3 → 0.8.4-main.dfabb4ec29
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/README.md +4 -6
- package/dist/lib/neutral/index.mjs +593 -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 -9
- 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 +19 -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 +22 -39
- 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 -7
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +1 -44
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +2 -54
- 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 +25 -72
- 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 +428 -0
- package/src/sdk.ts +31 -0
- package/src/services/credentials.ts +90 -111
- package/src/services/function-invocation-service.ts +33 -0
- package/src/services/index.ts +3 -7
- package/src/services/queues.ts +1 -78
- package/src/services/tracing.ts +1 -133
- package/src/types/index.ts +5 -0
- package/src/types/url.ts +32 -0
- package/dist/lib/browser/bundler/index.mjs +0 -265
- package/dist/lib/browser/bundler/index.mjs.map +0 -7
- package/dist/lib/browser/chunk-ANP3DFCO.mjs +0 -623
- package/dist/lib/browser/chunk-ANP3DFCO.mjs.map +0 -7
- package/dist/lib/browser/chunk-J5LGTIGS.mjs +0 -10
- package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +0 -7
- package/dist/lib/browser/edge/index.mjs +0 -83
- package/dist/lib/browser/edge/index.mjs.map +0 -7
- package/dist/lib/browser/index.mjs +0 -1265
- 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 -129
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/bundler/index.mjs +0 -266
- package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-MPKVY7ZR.mjs +0 -625
- package/dist/lib/node-esm/chunk-MPKVY7ZR.mjs.map +0 -7
- package/dist/lib/node-esm/edge/index.mjs +0 -84
- package/dist/lib/node-esm/edge/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -1266
- 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 -130
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/bundler/bundler.d.ts +0 -49
- 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 -17
- 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 -137
- package/dist/types/src/errors.d.ts.map +0 -1
- package/dist/types/src/examples/fib.d.ts +0 -7
- package/dist/types/src/examples/fib.d.ts.map +0 -1
- package/dist/types/src/examples/index.d.ts +0 -4
- package/dist/types/src/examples/index.d.ts.map +0 -1
- package/dist/types/src/examples/reply.d.ts +0 -3
- package/dist/types/src/examples/reply.d.ts.map +0 -1
- package/dist/types/src/examples/sleep.d.ts +0 -5
- package/dist/types/src/examples/sleep.d.ts.map +0 -1
- package/dist/types/src/executor/executor.d.ts +0 -11
- 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 -94
- package/dist/types/src/handler.d.ts.map +0 -1
- package/dist/types/src/schema.d.ts +0 -43
- package/dist/types/src/schema.d.ts.map +0 -1
- package/dist/types/src/services/database.d.ts +0 -98
- package/dist/types/src/services/database.d.ts.map +0 -1
- package/dist/types/src/services/event-logger.d.ts +0 -72
- package/dist/types/src/services/event-logger.d.ts.map +0 -1
- package/dist/types/src/services/local-function-execution.d.ts +0 -25
- package/dist/types/src/services/local-function-execution.d.ts.map +0 -1
- package/dist/types/src/services/remote-function-execution-service.d.ts +0 -15
- package/dist/types/src/services/remote-function-execution-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 -15
- 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/persist-database.test.d.ts +0 -2
- package/dist/types/src/testing/persist-database.test.d.ts.map +0 -1
- package/dist/types/src/testing/services.d.ts +0 -59
- package/dist/types/src/testing/services.d.ts.map +0 -1
- package/dist/types/src/trace.d.ts +0 -122
- 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/triggers/index.d.ts +0 -4
- package/dist/types/src/triggers/index.d.ts.map +0 -1
- package/dist/types/src/triggers/input-builder.d.ts +0 -3
- package/dist/types/src/triggers/input-builder.d.ts.map +0 -1
- package/dist/types/src/triggers/invocation-tracer.d.ts +0 -35
- package/dist/types/src/triggers/invocation-tracer.d.ts.map +0 -1
- package/dist/types/src/triggers/trigger-dispatcher.d.ts +0 -75
- package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +0 -1
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +0 -2
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +0 -1
- package/dist/types/src/triggers/trigger-state-store.d.ts +0 -27
- package/dist/types/src/triggers/trigger-state-store.d.ts.map +0 -1
- package/dist/types/src/types.d.ts +0 -211
- package/dist/types/src/types.d.ts.map +0 -1
- package/dist/types/src/url.d.ts +0 -21
- package/dist/types/src/url.d.ts.map +0 -1
- package/src/bundler/bundler.test.ts +0 -58
- package/src/bundler/bundler.ts +0 -291
- package/src/bundler/index.ts +0 -5
- package/src/edge/functions.ts +0 -67
- package/src/edge/index.ts +0 -9
- package/src/errors.ts +0 -21
- package/src/examples/fib.ts +0 -30
- package/src/examples/index.ts +0 -7
- package/src/examples/reply.ts +0 -18
- package/src/examples/sleep.ts +0 -22
- package/src/executor/executor.ts +0 -54
- package/src/handler.ts +0 -201
- package/src/schema.ts +0 -68
- package/src/services/database.ts +0 -170
- package/src/services/event-logger.ts +0 -118
- package/src/services/local-function-execution.ts +0 -114
- package/src/services/remote-function-execution-service.ts +0 -46
- package/src/services/service-container.ts +0 -114
- package/src/services/service-registry.test.ts +0 -42
- package/src/services/service-registry.ts +0 -59
- package/src/testing/index.ts +0 -6
- package/src/testing/layer.ts +0 -111
- package/src/testing/logger.ts +0 -16
- package/src/testing/persist-database.test.ts +0 -87
- package/src/testing/services.ts +0 -115
- package/src/trace.ts +0 -178
- package/src/translations.ts +0 -20
- package/src/triggers/index.ts +0 -7
- package/src/triggers/input-builder.ts +0 -35
- package/src/triggers/invocation-tracer.ts +0 -99
- package/src/triggers/trigger-dispatcher.test.ts +0 -652
- package/src/triggers/trigger-dispatcher.ts +0 -512
- package/src/triggers/trigger-state-store.ts +0 -60
- package/src/types.ts +0 -200
- package/src/url.ts +0 -55
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Context, Effect, Layer, Schema } from 'effect';
|
|
6
|
-
|
|
7
|
-
import { Obj, Type } from '@dxos/echo';
|
|
8
|
-
import { invariant } from '@dxos/invariant';
|
|
9
|
-
import { LogLevel, log } from '@dxos/log';
|
|
10
|
-
|
|
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>;
|
|
43
|
-
|
|
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 }
|
|
54
|
-
>() {
|
|
55
|
-
static noop: Context.Tag.Service<ComputeEventLogger> = {
|
|
56
|
-
log: () => {},
|
|
57
|
-
nodeId: undefined,
|
|
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
|
-
);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export const logCustomEvent = (data: any) =>
|
|
78
|
-
Effect.gen(function* () {
|
|
79
|
-
const logger = yield* ComputeEventLogger;
|
|
80
|
-
if (!logger.nodeId) {
|
|
81
|
-
throw new Error('logCustomEvent must be called within a node compute function');
|
|
82
|
-
}
|
|
83
|
-
logger.log({
|
|
84
|
-
type: 'custom',
|
|
85
|
-
nodeId: logger.nodeId,
|
|
86
|
-
event: data,
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
export const createDefectLogger = <A, E, R>(): ((self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>) =>
|
|
91
|
-
Effect.catchAll((error) =>
|
|
92
|
-
Effect.gen(function* () {
|
|
93
|
-
log.error('unhandled effect error', { error });
|
|
94
|
-
throw error;
|
|
95
|
-
}),
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
export const createEventLogger = (
|
|
99
|
-
level: LogLevel,
|
|
100
|
-
message: string = 'event',
|
|
101
|
-
): Context.Tag.Service<ComputeEventLogger> => {
|
|
102
|
-
const logFunction = (
|
|
103
|
-
{
|
|
104
|
-
[LogLevel.WARN]: log.warn,
|
|
105
|
-
[LogLevel.VERBOSE]: log.verbose,
|
|
106
|
-
[LogLevel.DEBUG]: log.debug,
|
|
107
|
-
[LogLevel.INFO]: log.info,
|
|
108
|
-
[LogLevel.ERROR]: log.error,
|
|
109
|
-
} as any
|
|
110
|
-
)[level];
|
|
111
|
-
invariant(logFunction);
|
|
112
|
-
return {
|
|
113
|
-
log: (event: ComputeEventPayload) => {
|
|
114
|
-
logFunction(message, event);
|
|
115
|
-
},
|
|
116
|
-
nodeId: undefined,
|
|
117
|
-
};
|
|
118
|
-
};
|
|
@@ -1,114 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,114 +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 { ComputeEventLogger } from './event-logger';
|
|
13
|
-
import { QueueService } from './queues';
|
|
14
|
-
import { RemoteFunctionExecutionService } from './remote-function-execution-service';
|
|
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.AiService,
|
|
24
|
-
credentials: CredentialsService,
|
|
25
|
-
database: DatabaseService,
|
|
26
|
-
eventLogger: ComputeEventLogger,
|
|
27
|
-
functionCallService: RemoteFunctionExecutionService,
|
|
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 =
|
|
94
|
-
this._services.ai != null ? Layer.succeed(AiService.AiService, this._services.ai) : AiService.notAvailable;
|
|
95
|
-
const credentials = Layer.succeed(
|
|
96
|
-
CredentialsService,
|
|
97
|
-
this._services.credentials ?? new ConfiguredCredentialsService(),
|
|
98
|
-
);
|
|
99
|
-
const database =
|
|
100
|
-
this._services.database != null
|
|
101
|
-
? Layer.succeed(DatabaseService, this._services.database)
|
|
102
|
-
: DatabaseService.notAvailable;
|
|
103
|
-
const queues =
|
|
104
|
-
this._services.queues != null ? Layer.succeed(QueueService, this._services.queues) : QueueService.notAvailable;
|
|
105
|
-
const tracing = Layer.succeed(TracingService, this._services.tracing ?? TracingService.noop);
|
|
106
|
-
const eventLogger = Layer.succeed(ComputeEventLogger, this._services.eventLogger ?? ComputeEventLogger.noop);
|
|
107
|
-
const functionCallService = Layer.succeed(
|
|
108
|
-
RemoteFunctionExecutionService,
|
|
109
|
-
this._services.functionCallService ?? RemoteFunctionExecutionService.mock(),
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
return Layer.mergeAll(ai, credentials, database, queues, tracing, eventLogger, functionCallService);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
@@ -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,59 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Context, Effect, type Option, flow } 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('@dxos/functions/ServiceRegistry')<
|
|
16
|
-
ServiceRegistry,
|
|
17
|
-
ServiceRegistry.Service
|
|
18
|
-
>() {
|
|
19
|
-
/**
|
|
20
|
-
* Resolves the service from the registry.
|
|
21
|
-
* @param tag Service tag to resolve.
|
|
22
|
-
* @throws {@link ServiceNotAvailableError} if the service is not found.
|
|
23
|
-
* @returns Effect that resolve to the service.
|
|
24
|
-
*/
|
|
25
|
-
static resolve: <T extends Context.Tag<any, any>>(
|
|
26
|
-
tag: T,
|
|
27
|
-
) => Effect.Effect<T, ServiceNotAvailableError, ServiceRegistry> = (tag) =>
|
|
28
|
-
ServiceRegistry.pipe(
|
|
29
|
-
Effect.flatMap((_) => _.resolve(tag)),
|
|
30
|
-
Effect.mapError(() => new ServiceNotAvailableError(tag.key)),
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
static provide: {
|
|
34
|
-
<Tags extends [Context.Tag<any, any>, ...Context.Tag<any, any>[]]>(
|
|
35
|
-
...tags: Tags
|
|
36
|
-
): <A, E, R>(
|
|
37
|
-
effect: Effect.Effect<A, E, R>,
|
|
38
|
-
) => Effect.Effect<
|
|
39
|
-
A,
|
|
40
|
-
E | ServiceNotAvailableError,
|
|
41
|
-
Exclude<R, { [K in keyof Tags]: Context.Tag.Identifier<Tags[K]> }[number]> | ServiceRegistry
|
|
42
|
-
>;
|
|
43
|
-
} = (...tags) => (flow as any)(...tags.map((tag) => Effect.provideServiceEffect(tag, ServiceRegistry.resolve(tag))));
|
|
44
|
-
|
|
45
|
-
static provideOrDie: {
|
|
46
|
-
<Tags extends [Context.Tag<any, any>, ...Context.Tag<any, any>[]]>(
|
|
47
|
-
...tags: Tags
|
|
48
|
-
): <A, E, R>(
|
|
49
|
-
effect: Effect.Effect<A, E, R>,
|
|
50
|
-
) => Effect.Effect<
|
|
51
|
-
A,
|
|
52
|
-
E,
|
|
53
|
-
Exclude<R, { [K in keyof Tags]: Context.Tag.Identifier<Tags[K]> }[number]> | ServiceRegistry
|
|
54
|
-
>;
|
|
55
|
-
} = (...tags) =>
|
|
56
|
-
(flow as any)(
|
|
57
|
-
...tags.map((tag) => Effect.provideServiceEffect(tag, ServiceRegistry.resolve(tag).pipe(Effect.orDie))),
|
|
58
|
-
);
|
|
59
|
-
}
|
package/src/testing/index.ts
DELETED
package/src/testing/layer.ts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import type { Schema } from 'effect';
|
|
6
|
-
import { Context, Effect, Layer } from 'effect';
|
|
7
|
-
|
|
8
|
-
import type { EchoDatabaseImpl, QueueFactory } from '@dxos/echo-db';
|
|
9
|
-
import { EchoTestBuilder } from '@dxos/echo-db/testing';
|
|
10
|
-
import type { EchoHostIndexingConfig } from '@dxos/echo-pipeline';
|
|
11
|
-
import { accuireReleaseResource } from '@dxos/effect';
|
|
12
|
-
import { PublicKey } from '@dxos/keys';
|
|
13
|
-
import type { LevelDB } from '@dxos/kv-store';
|
|
14
|
-
import { createTestLevel } from '@dxos/kv-store/testing';
|
|
15
|
-
import { log } from '@dxos/log';
|
|
16
|
-
|
|
17
|
-
import { DatabaseService, QueueService } from '../services';
|
|
18
|
-
|
|
19
|
-
const testBuilder = accuireReleaseResource(() => new EchoTestBuilder());
|
|
20
|
-
|
|
21
|
-
export const testStoragePath = ({ name = PublicKey.random().toHex() }: { name?: string }) => {
|
|
22
|
-
return `/tmp/dxos-${name}`;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export type TestDatabaseOptions = {
|
|
26
|
-
indexing?: Partial<EchoHostIndexingConfig>;
|
|
27
|
-
types?: Schema.Schema.AnyNoContext[];
|
|
28
|
-
storagePath?: string;
|
|
29
|
-
onInit?: () => Effect.Effect<void, never, DatabaseService | QueueService>;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export const TestDatabaseLayer = ({ indexing, types, storagePath, onInit }: TestDatabaseOptions = {}) =>
|
|
33
|
-
Layer.scopedContext(
|
|
34
|
-
Effect.gen(function* () {
|
|
35
|
-
const builder = yield* testBuilder;
|
|
36
|
-
|
|
37
|
-
let kv: LevelDB | undefined;
|
|
38
|
-
if (storagePath) {
|
|
39
|
-
kv = createTestLevel(storagePath);
|
|
40
|
-
yield* Effect.promise(() => kv!.open());
|
|
41
|
-
// const keyCount = yield* Effect.promise(async () => (await kv!.iterator({ values: false }).all()).length);
|
|
42
|
-
// log.info('opened test db', { storagePath, keyCount });
|
|
43
|
-
}
|
|
44
|
-
const peer = yield* Effect.promise(() => builder.createPeer({ indexing, types, kv }));
|
|
45
|
-
|
|
46
|
-
let db: EchoDatabaseImpl | undefined;
|
|
47
|
-
let queues: QueueFactory | undefined;
|
|
48
|
-
|
|
49
|
-
if (storagePath) {
|
|
50
|
-
const testMetadata = yield* Effect.promise(async () => {
|
|
51
|
-
try {
|
|
52
|
-
return await kv!.get('test-metadata', { valueEncoding: 'json' });
|
|
53
|
-
} catch (e) {
|
|
54
|
-
if ((e as any).code === 'LEVEL_NOT_FOUND') {
|
|
55
|
-
return undefined;
|
|
56
|
-
}
|
|
57
|
-
throw e;
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
log('starting persistant test db', { storagePath, testMetadata });
|
|
61
|
-
if (!testMetadata) {
|
|
62
|
-
const key = PublicKey.random();
|
|
63
|
-
db = yield* Effect.promise(() => peer.createDatabase(key));
|
|
64
|
-
queues = peer.client.constructQueueFactory(db.spaceId);
|
|
65
|
-
|
|
66
|
-
yield* Effect.promise(() =>
|
|
67
|
-
kv!.put('test-metadata', { key: key.toHex(), rootUrl: db!.rootUrl }, { valueEncoding: 'json' }),
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
if (onInit) {
|
|
71
|
-
yield* onInit().pipe(
|
|
72
|
-
Effect.provideService(DatabaseService, DatabaseService.make(db)),
|
|
73
|
-
Effect.provideService(QueueService, QueueService.make(queues, undefined)),
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
} else {
|
|
77
|
-
const key = PublicKey.from((testMetadata as any).key);
|
|
78
|
-
const rootUrl = (testMetadata as any).rootUrl;
|
|
79
|
-
db = yield* Effect.promise(() => peer.openDatabase(key, rootUrl));
|
|
80
|
-
queues = peer.client.constructQueueFactory(db.spaceId);
|
|
81
|
-
}
|
|
82
|
-
} else {
|
|
83
|
-
db = yield* Effect.promise(() => peer.createDatabase());
|
|
84
|
-
queues = peer.client.constructQueueFactory(db.spaceId);
|
|
85
|
-
if (onInit) {
|
|
86
|
-
yield* onInit().pipe(
|
|
87
|
-
Effect.provideService(DatabaseService, DatabaseService.make(db)),
|
|
88
|
-
Effect.provideService(QueueService, QueueService.make(queues, undefined)),
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
yield* Effect.addFinalizer(() =>
|
|
94
|
-
Effect.promise(async () => {
|
|
95
|
-
if (kv) {
|
|
96
|
-
// {
|
|
97
|
-
// const keyCount = (await kv.iterator({ values: false }).all()).length;
|
|
98
|
-
// log.info('closing persistant test db', { storagePath, keyCount });
|
|
99
|
-
// }
|
|
100
|
-
|
|
101
|
-
await kv.close();
|
|
102
|
-
}
|
|
103
|
-
}),
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
return Context.mergeAll(
|
|
107
|
-
Context.make(DatabaseService, DatabaseService.make(db)),
|
|
108
|
-
Context.make(QueueService, QueueService.make(queues, undefined)),
|
|
109
|
-
);
|
|
110
|
-
}),
|
|
111
|
-
);
|
package/src/testing/logger.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { type Context, Effect } from 'effect';
|
|
6
|
-
|
|
7
|
-
import { LogLevel } from '@dxos/log';
|
|
8
|
-
|
|
9
|
-
import { type ComputeEventLogger, createEventLogger } from '../services';
|
|
10
|
-
|
|
11
|
-
export const noopLogger: Context.Tag.Service<ComputeEventLogger> = {
|
|
12
|
-
log: () => Effect.succeed(undefined),
|
|
13
|
-
nodeId: undefined,
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export const consoleLogger: Context.Tag.Service<ComputeEventLogger> = createEventLogger(LogLevel.INFO);
|