@dxos/functions 0.8.4-main.5ea62a8 → 0.8.4-main.72ec0f3
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/index.mjs +769 -407
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +769 -407
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/errors.d.ts +87 -28
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/{examples → example}/fib.d.ts +1 -1
- package/dist/types/src/example/fib.d.ts.map +1 -0
- package/dist/types/src/example/forex-effect.d.ts +3 -0
- package/dist/types/src/example/forex-effect.d.ts.map +1 -0
- package/dist/types/src/example/index.d.ts +12 -0
- package/dist/types/src/example/index.d.ts.map +1 -0
- package/dist/types/src/{examples → example}/reply.d.ts +1 -1
- package/dist/types/src/example/reply.d.ts.map +1 -0
- package/dist/types/src/{examples → example}/sleep.d.ts +1 -1
- package/dist/types/src/example/sleep.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +5 -7
- package/dist/types/src/index.d.ts.map +1 -1
- 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 +7 -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 +89 -0
- package/dist/types/src/sdk.d.ts.map +1 -0
- package/dist/types/src/services/credentials.d.ts +7 -3
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +20 -5
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +11 -0
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
- package/dist/types/src/services/index.d.ts +6 -6
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +4 -1
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +10 -12
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/types/Function.d.ts +58 -0
- package/dist/types/src/types/Function.d.ts.map +1 -0
- package/dist/types/src/types/Script.d.ts +28 -0
- package/dist/types/src/types/Script.d.ts.map +1 -0
- package/dist/types/src/types/Trigger.d.ts +139 -0
- package/dist/types/src/types/Trigger.d.ts.map +1 -0
- package/dist/types/src/types/TriggerEvent.d.ts +44 -0
- package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +6 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/src/{url.d.ts → types/url.d.ts} +1 -10
- package/dist/types/src/types/url.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +17 -72
- package/src/errors.ts +13 -5
- package/src/{examples → example}/fib.ts +5 -3
- package/src/example/forex-effect.ts +40 -0
- package/src/example/index.ts +13 -0
- package/src/{examples → example}/reply.ts +6 -3
- package/src/{examples → example}/sleep.ts +5 -3
- package/src/index.ts +5 -9
- package/src/{executor → protocol}/index.ts +1 -1
- package/src/protocol/protocol.test.ts +59 -0
- package/src/protocol/protocol.ts +145 -0
- package/src/sdk.ts +226 -0
- package/src/services/credentials.ts +12 -6
- package/src/services/event-logger.ts +12 -3
- package/src/services/function-invocation-service.ts +23 -0
- package/src/services/index.ts +7 -6
- package/src/services/queues.ts +8 -1
- package/src/services/tracing.ts +26 -52
- package/src/{schema.ts → types/Function.ts} +20 -26
- package/src/types/Script.ts +33 -0
- package/src/types/Trigger.ts +139 -0
- package/src/types/TriggerEvent.ts +62 -0
- package/src/types/index.ts +9 -0
- package/src/types/url.ts +31 -0
- package/dist/lib/browser/bundler/index.mjs +0 -263
- package/dist/lib/browser/bundler/index.mjs.map +0 -7
- package/dist/lib/browser/chunk-7NQ77AIQ.mjs +0 -618
- package/dist/lib/browser/chunk-7NQ77AIQ.mjs.map +0 -7
- package/dist/lib/browser/edge/index.mjs +0 -81
- package/dist/lib/browser/edge/index.mjs.map +0 -7
- package/dist/lib/browser/testing/index.mjs +0 -122
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/bundler/index.mjs +0 -265
- package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-KCGC6QQT.mjs +0 -620
- package/dist/lib/node-esm/chunk-KCGC6QQT.mjs.map +0 -7
- package/dist/lib/node-esm/edge/index.mjs +0 -83
- package/dist/lib/node-esm/edge/index.mjs.map +0 -7
- package/dist/lib/node-esm/testing/index.mjs +0 -123
- 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/examples/fib.d.ts.map +0 -1
- package/dist/types/src/examples/reply.d.ts.map +0 -1
- 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 -65
- 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/local-function-execution.d.ts +0 -12
- 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 -14
- 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 -150
- 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 -328
- package/dist/types/src/types.d.ts.map +0 -1
- 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/executor/executor.ts +0 -54
- package/src/handler.ts +0 -122
- package/src/services/database.ts +0 -170
- package/src/services/local-function-execution.ts +0 -77
- 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 -97
- 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/types.ts +0 -203
- package/src/url.ts +0 -55
package/src/bundler/index.ts
DELETED
package/src/edge/functions.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { type DID } from 'iso-did/types';
|
|
6
|
-
|
|
7
|
-
import { type Client } from '@dxos/client';
|
|
8
|
-
import { createEdgeIdentity } from '@dxos/client/edge';
|
|
9
|
-
import { EdgeHttpClient } from '@dxos/edge-client';
|
|
10
|
-
import { invariant } from '@dxos/invariant';
|
|
11
|
-
import { type PublicKey } from '@dxos/keys';
|
|
12
|
-
import { log } from '@dxos/log';
|
|
13
|
-
import { type UploadFunctionResponseBody } from '@dxos/protocols';
|
|
14
|
-
|
|
15
|
-
export type UploadWorkerArgs = {
|
|
16
|
-
client: Client;
|
|
17
|
-
version: string;
|
|
18
|
-
name?: string;
|
|
19
|
-
functionId?: string;
|
|
20
|
-
ownerPublicKey: PublicKey;
|
|
21
|
-
entryPoint: string;
|
|
22
|
-
assets: Record<string, Uint8Array>;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export const uploadWorkerFunction = async ({
|
|
26
|
-
client,
|
|
27
|
-
version,
|
|
28
|
-
name,
|
|
29
|
-
functionId,
|
|
30
|
-
ownerPublicKey,
|
|
31
|
-
entryPoint,
|
|
32
|
-
assets,
|
|
33
|
-
}: UploadWorkerArgs): Promise<UploadFunctionResponseBody> => {
|
|
34
|
-
log('uploading function', { functionId, name, version, ownerPublicKey });
|
|
35
|
-
const edgeUrl = client.config.values.runtime?.services?.edge?.url;
|
|
36
|
-
invariant(edgeUrl, 'Edge is not configured.');
|
|
37
|
-
const edgeClient = new EdgeHttpClient(edgeUrl);
|
|
38
|
-
const edgeIdentity = createEdgeIdentity(client);
|
|
39
|
-
edgeClient.setIdentity(edgeIdentity);
|
|
40
|
-
const response = await edgeClient.uploadFunction(
|
|
41
|
-
{ functionId },
|
|
42
|
-
{ name, version, ownerPublicKey: ownerPublicKey.toHex(), entryPoint, assets },
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
// TODO(burdon): Edge service log.
|
|
46
|
-
log('uploaded', {
|
|
47
|
-
identityKey: edgeIdentity.identityKey,
|
|
48
|
-
functionId,
|
|
49
|
-
name,
|
|
50
|
-
version,
|
|
51
|
-
response,
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
return response;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
export const incrementSemverPatch = (version: string): string => {
|
|
58
|
-
const [major, minor, patch] = version.split('.');
|
|
59
|
-
const patchNum = Number(patch);
|
|
60
|
-
invariant(!Number.isNaN(patchNum), `Unexpected function version format: ${version}`);
|
|
61
|
-
return [major, minor, String(patchNum + 1)].join('.');
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
// TODO(burdon): Factor out.
|
|
65
|
-
export const publicKeyToDid = (key: PublicKey): DID => {
|
|
66
|
-
return `did:key:${key.toHex()}`;
|
|
67
|
-
};
|
package/src/edge/index.ts
DELETED
package/src/executor/executor.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Effect, Schema } from 'effect';
|
|
6
|
-
|
|
7
|
-
import { type SpaceId } from '@dxos/client/echo';
|
|
8
|
-
import { runAndForwardErrors } from '@dxos/effect';
|
|
9
|
-
|
|
10
|
-
import type { FunctionContext, FunctionDefinition } from '../handler';
|
|
11
|
-
import type { ServiceContainer, Services } from '../services';
|
|
12
|
-
|
|
13
|
-
export class FunctionExecutor {
|
|
14
|
-
constructor(private readonly _services: ServiceContainer) {}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Invoke function.
|
|
18
|
-
*/
|
|
19
|
-
// TODO(dmaretskyi): Invocation context: queue, space, etc...
|
|
20
|
-
async invoke<F extends FunctionDefinition<any, any>>(
|
|
21
|
-
functionDef: F,
|
|
22
|
-
input: F extends FunctionDefinition<infer I, infer _O> ? I : never,
|
|
23
|
-
): Promise<F extends FunctionDefinition<infer _I, infer O> ? O : never> {
|
|
24
|
-
// Assert input matches schema
|
|
25
|
-
const assertInput = functionDef.inputSchema.pipe(Schema.asserts);
|
|
26
|
-
(assertInput as any)(input);
|
|
27
|
-
|
|
28
|
-
const context: FunctionContext = {
|
|
29
|
-
space: undefined,
|
|
30
|
-
getService: this._services.getService.bind(this._services),
|
|
31
|
-
getSpace: async (_spaceId: SpaceId) => {
|
|
32
|
-
throw new Error('Not available. Use the database service instead.');
|
|
33
|
-
},
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const result = functionDef.handler({ context, data: input });
|
|
37
|
-
|
|
38
|
-
let data: unknown;
|
|
39
|
-
if (Effect.isEffect(result)) {
|
|
40
|
-
data = await (result as Effect.Effect<unknown, unknown, Services>).pipe(
|
|
41
|
-
Effect.provide(this._services.createLayer()),
|
|
42
|
-
runAndForwardErrors,
|
|
43
|
-
);
|
|
44
|
-
} else {
|
|
45
|
-
data = await result;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// Assert output matches schema
|
|
49
|
-
const assertOutput = functionDef.outputSchema?.pipe(Schema.asserts);
|
|
50
|
-
(assertOutput as any)(data);
|
|
51
|
-
|
|
52
|
-
return data as any;
|
|
53
|
-
}
|
|
54
|
-
}
|
package/src/handler.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { type Context, type Effect, Schema } from 'effect';
|
|
6
|
-
|
|
7
|
-
import { type EchoDatabase } from '@dxos/echo-db';
|
|
8
|
-
import { type HasId } from '@dxos/echo-schema';
|
|
9
|
-
import { type DXN, type SpaceId } from '@dxos/keys';
|
|
10
|
-
import { type QueryResult } from '@dxos/protocols';
|
|
11
|
-
|
|
12
|
-
import { type Services } from './services';
|
|
13
|
-
|
|
14
|
-
// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
|
|
15
|
-
// https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
|
|
16
|
-
// https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
|
|
17
|
-
// https://www.npmjs.com/package/aws-lambda
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Function handler.
|
|
21
|
-
*/
|
|
22
|
-
export type FunctionHandler<TData = {}, TOutput = any> = (params: {
|
|
23
|
-
/**
|
|
24
|
-
* Services and context available to the function.
|
|
25
|
-
*/
|
|
26
|
-
context: FunctionContext;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Data passed as the input to the function.
|
|
30
|
-
* Must match the function's input schema.
|
|
31
|
-
* This will be the payload from the trigger or other data passed into the function in a workflow.
|
|
32
|
-
*/
|
|
33
|
-
data: TData;
|
|
34
|
-
}) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any, Services>;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Function context.
|
|
38
|
-
*/
|
|
39
|
-
export interface FunctionContext {
|
|
40
|
-
/**
|
|
41
|
-
* Space from which the function was invoked.
|
|
42
|
-
*/
|
|
43
|
-
space: SpaceAPI | undefined;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Resolves a service available to the function.
|
|
47
|
-
* @throws if the service is not available.
|
|
48
|
-
*/
|
|
49
|
-
getService: <T extends Context.Tag<any, any>>(tag: T) => Context.Tag.Service<T>;
|
|
50
|
-
|
|
51
|
-
getSpace: (spaceId: SpaceId) => Promise<SpaceAPI>;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export interface FunctionContextAi {
|
|
55
|
-
// TODO(dmaretskyi): Refer to cloudflare AI docs for more comprehensive typedefs.
|
|
56
|
-
run(model: string, inputs: any, options?: any): Promise<any>;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
//
|
|
60
|
-
// API.
|
|
61
|
-
//
|
|
62
|
-
|
|
63
|
-
// TODO(dmaretskyi): Temporary API to get the queues working.
|
|
64
|
-
// TODO(dmaretskyi): To be replaced with integrating queues into echo.
|
|
65
|
-
export interface QueuesAPI {
|
|
66
|
-
queryQueue(queue: DXN, options?: {}): Promise<QueryResult>;
|
|
67
|
-
insertIntoQueue(queue: DXN, objects: HasId[]): Promise<void>;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Space interface available to functions.
|
|
72
|
-
*/
|
|
73
|
-
export interface SpaceAPI {
|
|
74
|
-
get id(): SpaceId;
|
|
75
|
-
get db(): EchoDatabase;
|
|
76
|
-
|
|
77
|
-
// TODO(dmaretskyi): Align with echo api: queues.get(id).append(items);
|
|
78
|
-
get queues(): QueuesAPI;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// TODO(wittjosiah): Queues are incompatible.
|
|
82
|
-
const __assertFunctionSpaceIsCompatibleWithTheClientSpace = () => {
|
|
83
|
-
// const _: SpaceAPI = {} as Space;
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
export type FunctionDefinition<T = any, O = any> = {
|
|
87
|
-
// TODO(dmaretskyi): Use `key` for FQN and `name` for human-readable-name.
|
|
88
|
-
name: string;
|
|
89
|
-
description?: string;
|
|
90
|
-
inputSchema: Schema.Schema<T, any>;
|
|
91
|
-
outputSchema?: Schema.Schema<O, any>;
|
|
92
|
-
handler: FunctionHandler<T, O>;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
export const defineFunction = <T, O>({
|
|
96
|
-
name,
|
|
97
|
-
description,
|
|
98
|
-
inputSchema,
|
|
99
|
-
outputSchema = Schema.Any,
|
|
100
|
-
handler,
|
|
101
|
-
}: FunctionDefinition<T, O>): FunctionDefinition<T, O> => {
|
|
102
|
-
if (!Schema.isSchema(inputSchema)) {
|
|
103
|
-
throw new Error('Input schema must be a valid schema');
|
|
104
|
-
}
|
|
105
|
-
if (typeof handler !== 'function') {
|
|
106
|
-
throw new Error('Handler must be a function');
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return {
|
|
110
|
-
name,
|
|
111
|
-
description,
|
|
112
|
-
inputSchema,
|
|
113
|
-
outputSchema,
|
|
114
|
-
handler,
|
|
115
|
-
};
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
export namespace FunctionDefinition {
|
|
119
|
-
export type Any = FunctionDefinition<any, any>;
|
|
120
|
-
export type Input<T extends FunctionDefinition> = T extends FunctionDefinition<infer I, any> ? I : never;
|
|
121
|
-
export type Output<T extends FunctionDefinition> = T extends FunctionDefinition<any, infer O> ? O : never;
|
|
122
|
-
}
|
package/src/services/database.ts
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Context, Effect, Layer, Option, type Schema } from 'effect';
|
|
6
|
-
|
|
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';
|
|
14
|
-
import type { DXN } from '@dxos/keys';
|
|
15
|
-
|
|
16
|
-
export class DatabaseService extends Context.Tag('@dxos/functions/DatabaseService')<
|
|
17
|
-
DatabaseService,
|
|
18
|
-
{
|
|
19
|
-
readonly db: EchoDatabase;
|
|
20
|
-
}
|
|
21
|
-
>() {
|
|
22
|
-
static notAvailable = Layer.succeed(DatabaseService, {
|
|
23
|
-
get db(): EchoDatabase {
|
|
24
|
-
throw new Error('Database not available');
|
|
25
|
-
},
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
static make = (db: EchoDatabase): Context.Tag.Service<DatabaseService> => {
|
|
29
|
-
return {
|
|
30
|
-
get db() {
|
|
31
|
-
return db;
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
};
|
|
35
|
-
|
|
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* () {
|
|
56
|
-
const { db } = yield* DatabaseService;
|
|
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
|
-
});
|
|
84
|
-
|
|
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);
|
|
94
|
-
});
|
|
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
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Creates a `QueryResult` object that can be subscribed to.
|
|
128
|
-
*/
|
|
129
|
-
static query: {
|
|
130
|
-
<Q extends Query.Any>(query: Q): Effect.Effect<QueryResult<Live<Query.Type<Q>>>, never, DatabaseService>;
|
|
131
|
-
<F extends Filter.Any>(filter: F): Effect.Effect<QueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;
|
|
132
|
-
} = (queryOrFilter: Query.Any | Filter.Any) =>
|
|
133
|
-
DatabaseService.pipe(
|
|
134
|
-
Effect.map(({ db }) => db.query(queryOrFilter as any)),
|
|
135
|
-
Effect.withSpan('DatabaseService.query'),
|
|
136
|
-
);
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Executes the query once and returns the results.
|
|
140
|
-
*/
|
|
141
|
-
static runQuery: {
|
|
142
|
-
<Q extends Query.Any>(query: Q): Effect.Effect<OneShotQueryResult<Live<Query.Type<Q>>>, never, DatabaseService>;
|
|
143
|
-
<F extends Filter.Any>(filter: F): Effect.Effect<OneShotQueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;
|
|
144
|
-
} = (queryOrFilter: Query.Any | Filter.Any) =>
|
|
145
|
-
DatabaseService.query(queryOrFilter as any).pipe(
|
|
146
|
-
Effect.flatMap((queryResult) => promiseWithCauseCapture(() => queryResult.run())),
|
|
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
|
-
}
|
|
170
|
-
}
|
|
@@ -1,77 +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 } from '../errors';
|
|
10
|
-
import type { FunctionContext, FunctionDefinition } from '../handler';
|
|
11
|
-
|
|
12
|
-
import type { Services } from './service-container';
|
|
13
|
-
|
|
14
|
-
export class LocalFunctionExecutionService extends Context.Tag('@dxos/functions/LocalFunctionExecutionService')<
|
|
15
|
-
LocalFunctionExecutionService,
|
|
16
|
-
{
|
|
17
|
-
// TODO(dmaretskyi): This should take function id instead of the definition object.
|
|
18
|
-
// TODO(dmaretskyi): Services should be satisfied from environment rather then bubbled up.
|
|
19
|
-
invokeFunction(functionDef: FunctionDefinition<any, any>, input: unknown): Effect.Effect<unknown, never, Services>;
|
|
20
|
-
}
|
|
21
|
-
>() {
|
|
22
|
-
static layer = Layer.succeed(LocalFunctionExecutionService, {
|
|
23
|
-
invokeFunction: (functionDef, input) => invokeFunction(functionDef, input),
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
static invokeFunction: <F extends FunctionDefinition.Any>(
|
|
27
|
-
functionDef: F,
|
|
28
|
-
input: FunctionDefinition.Input<F>,
|
|
29
|
-
) => Effect.Effect<FunctionDefinition.Output<F>, never, Services | LocalFunctionExecutionService> =
|
|
30
|
-
Effect.serviceFunctionEffect(LocalFunctionExecutionService, (_) => _.invokeFunction as any);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const invokeFunction = (
|
|
34
|
-
functionDef: FunctionDefinition<any, any>,
|
|
35
|
-
input: any,
|
|
36
|
-
): Effect.Effect<unknown, never, Services> =>
|
|
37
|
-
Effect.gen(function* () {
|
|
38
|
-
// Assert input matches schema
|
|
39
|
-
const assertInput = functionDef.inputSchema.pipe(Schema.asserts);
|
|
40
|
-
(assertInput as any)(input);
|
|
41
|
-
|
|
42
|
-
const context: FunctionContext = {
|
|
43
|
-
space: undefined,
|
|
44
|
-
getService: () => todo(),
|
|
45
|
-
getSpace: async (_spaceId: any) => {
|
|
46
|
-
throw new Error('Not available. Use the database service instead.');
|
|
47
|
-
},
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
// TODO(dmaretskyi): This should be delegated to a function invoker service.
|
|
51
|
-
const data = yield* Effect.gen(function* () {
|
|
52
|
-
const result = functionDef.handler({ context, data: input });
|
|
53
|
-
if (Effect.isEffect(result)) {
|
|
54
|
-
return yield* (result as Effect.Effect<unknown, unknown, Services>).pipe(Effect.orDie);
|
|
55
|
-
} else if (
|
|
56
|
-
typeof result === 'object' &&
|
|
57
|
-
result !== null &&
|
|
58
|
-
'then' in result &&
|
|
59
|
-
typeof result.then === 'function'
|
|
60
|
-
) {
|
|
61
|
-
return yield* Effect.promise(() => result);
|
|
62
|
-
} else {
|
|
63
|
-
return result;
|
|
64
|
-
}
|
|
65
|
-
}).pipe(
|
|
66
|
-
Effect.orDie,
|
|
67
|
-
Effect.catchAllDefect((defect) =>
|
|
68
|
-
Effect.die(new FunctionError('Error running function', { context: { name: functionDef.name }, cause: defect })),
|
|
69
|
-
),
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
// Assert output matches schema
|
|
73
|
-
const assertOutput = functionDef.outputSchema?.pipe(Schema.asserts);
|
|
74
|
-
(assertOutput as any)(data);
|
|
75
|
-
|
|
76
|
-
return data;
|
|
77
|
-
}).pipe(Effect.withSpan('invokeFunction', { attributes: { name: functionDef.name } }));
|
|
@@ -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
|
-
}
|