@dxos/functions 0.8.4-main.67995b8 → 0.8.4-main.a4bbb77
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/bundler/index.mjs +56 -38
- package/dist/lib/browser/bundler/index.mjs.map +3 -3
- package/dist/lib/browser/chunk-C2Z7LCJ2.mjs +649 -0
- package/dist/lib/browser/chunk-C2Z7LCJ2.mjs.map +7 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/browser/edge/index.mjs +22 -8
- package/dist/lib/browser/edge/index.mjs.map +3 -3
- package/dist/lib/browser/index.mjs +992 -127
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +76 -6
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/bundler/index.mjs +55 -38
- package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
- package/dist/lib/node-esm/chunk-AH3AZM2U.mjs +651 -0
- package/dist/lib/node-esm/chunk-AH3AZM2U.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
- package/dist/lib/node-esm/edge/index.mjs +21 -8
- package/dist/lib/node-esm/edge/index.mjs.map +3 -3
- package/dist/lib/node-esm/index.mjs +992 -127
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +76 -6
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/bundler/bundler.d.ts +11 -12
- package/dist/types/src/bundler/bundler.d.ts.map +1 -1
- package/dist/types/src/edge/functions.d.ts +3 -2
- package/dist/types/src/edge/functions.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +89 -20
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/examples/fib.d.ts +7 -0
- package/dist/types/src/examples/fib.d.ts.map +1 -0
- package/dist/types/src/examples/index.d.ts +4 -0
- package/dist/types/src/examples/index.d.ts.map +1 -0
- package/dist/types/src/examples/reply.d.ts +3 -0
- package/dist/types/src/examples/reply.d.ts.map +1 -0
- package/dist/types/src/examples/sleep.d.ts +5 -0
- package/dist/types/src/examples/sleep.d.ts.map +1 -0
- package/dist/types/src/executor/executor.d.ts +7 -1
- package/dist/types/src/executor/executor.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +52 -8
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/schema.d.ts +6 -1
- package/dist/types/src/schema.d.ts.map +1 -1
- package/dist/types/src/services/credentials.d.ts +15 -3
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/database.d.ts +39 -6
- package/dist/types/src/services/database.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +1 -1
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +26 -0
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
- package/dist/types/src/services/function-invocation-service.test.d.ts +2 -0
- package/dist/types/src/services/function-invocation-service.test.d.ts.map +1 -0
- package/dist/types/src/services/index.d.ts +4 -3
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/local-function-execution.d.ts +23 -2
- package/dist/types/src/services/local-function-execution.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +19 -5
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/remote-function-execution-service.d.ts +9 -4
- package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -1
- package/dist/types/src/services/service-container.d.ts +1 -1
- package/dist/types/src/services/service-container.d.ts.map +1 -1
- package/dist/types/src/services/service-registry.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +34 -3
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +9 -2
- package/dist/types/src/testing/layer.d.ts.map +1 -1
- package/dist/types/src/testing/logger.d.ts.map +1 -1
- package/dist/types/src/testing/persist-database.test.d.ts +2 -0
- package/dist/types/src/testing/persist-database.test.d.ts.map +1 -0
- package/dist/types/src/testing/services.d.ts +1 -1
- package/dist/types/src/testing/services.d.ts.map +1 -1
- package/dist/types/src/trace.d.ts +20 -22
- package/dist/types/src/trace.d.ts.map +1 -1
- package/dist/types/src/triggers/index.d.ts +4 -0
- package/dist/types/src/triggers/index.d.ts.map +1 -0
- package/dist/types/src/triggers/input-builder.d.ts +3 -0
- package/dist/types/src/triggers/input-builder.d.ts.map +1 -0
- package/dist/types/src/triggers/invocation-tracer.d.ts +35 -0
- package/dist/types/src/triggers/invocation-tracer.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-dispatcher.d.ts +74 -0
- package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +2 -0
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-state-store.d.ts +27 -0
- package/dist/types/src/triggers/trigger-state-store.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +60 -250
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/url.d.ts +10 -6
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +43 -43
- package/src/bundler/bundler.test.ts +8 -9
- package/src/bundler/bundler.ts +32 -33
- package/src/edge/functions.ts +8 -5
- package/src/errors.ts +13 -5
- package/src/examples/fib.ts +31 -0
- package/src/examples/index.ts +7 -0
- package/src/examples/reply.ts +19 -0
- package/src/examples/sleep.ts +23 -0
- package/src/executor/executor.ts +12 -9
- package/src/handler.ts +120 -18
- package/src/index.ts +3 -3
- package/src/schema.ts +11 -0
- package/src/services/credentials.ts +80 -5
- package/src/services/database.ts +115 -18
- package/src/services/event-logger.ts +2 -2
- package/src/services/function-invocation-service.test.ts +79 -0
- package/src/services/function-invocation-service.ts +82 -0
- package/src/services/index.ts +4 -3
- package/src/services/local-function-execution.ts +97 -17
- package/src/services/queues.ts +34 -10
- package/src/services/remote-function-execution-service.ts +38 -43
- package/src/services/service-container.ts +4 -3
- package/src/services/service-registry.ts +1 -1
- package/src/services/tracing.ts +106 -9
- package/src/testing/layer.ts +84 -3
- package/src/testing/logger.ts +1 -1
- package/src/testing/persist-database.test.ts +87 -0
- package/src/testing/services.ts +3 -2
- package/src/trace.ts +17 -19
- package/src/triggers/index.ts +7 -0
- package/src/triggers/input-builder.ts +35 -0
- package/src/triggers/invocation-tracer.ts +99 -0
- package/src/triggers/trigger-dispatcher.test.ts +651 -0
- package/src/triggers/trigger-dispatcher.ts +522 -0
- package/src/triggers/trigger-state-store.ts +60 -0
- package/src/types.ts +39 -36
- package/src/url.ts +13 -10
- package/dist/lib/browser/chunk-6PTFLPCO.mjs +0 -462
- package/dist/lib/browser/chunk-6PTFLPCO.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-NYJ2TSXO.mjs +0 -464
- package/dist/lib/node-esm/chunk-NYJ2TSXO.mjs.map +0 -7
package/src/handler.ts
CHANGED
|
@@ -2,16 +2,18 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { type Context, Effect, Schema, type Types } from 'effect';
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
import type { EchoDatabase } from '@dxos/echo-db';
|
|
7
|
+
import { Obj, Type } from '@dxos/echo';
|
|
8
|
+
import { type EchoDatabase } from '@dxos/echo-db';
|
|
10
9
|
import { type HasId } from '@dxos/echo-schema';
|
|
11
|
-
import {
|
|
10
|
+
import { assertArgument } from '@dxos/invariant';
|
|
11
|
+
import { type DXN, type SpaceId } from '@dxos/keys';
|
|
12
12
|
import { type QueryResult } from '@dxos/protocols';
|
|
13
13
|
|
|
14
|
-
import
|
|
14
|
+
import { FunctionType } from './schema';
|
|
15
|
+
import { type Services } from './services';
|
|
16
|
+
import { getUserFunctionIdInMetadata, setUserFunctionIdInMetadata } from './url';
|
|
15
17
|
|
|
16
18
|
// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
|
|
17
19
|
// https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
|
|
@@ -44,8 +46,6 @@ export interface FunctionContext {
|
|
|
44
46
|
*/
|
|
45
47
|
space: SpaceAPI | undefined;
|
|
46
48
|
|
|
47
|
-
ai: AiServiceClient;
|
|
48
|
-
|
|
49
49
|
/**
|
|
50
50
|
* Resolves a service available to the function.
|
|
51
51
|
* @throws if the service is not available.
|
|
@@ -87,22 +87,41 @@ const __assertFunctionSpaceIsCompatibleWithTheClientSpace = () => {
|
|
|
87
87
|
// const _: SpaceAPI = {} as Space;
|
|
88
88
|
};
|
|
89
89
|
|
|
90
|
-
|
|
90
|
+
const typeId = Symbol.for('@dxos/functions/FunctionDefinition');
|
|
91
|
+
|
|
92
|
+
export type FunctionDefinition<T = any, O = any> = {
|
|
93
|
+
[typeId]: true;
|
|
94
|
+
key: string;
|
|
91
95
|
name: string;
|
|
92
96
|
description?: string;
|
|
93
97
|
inputSchema: Schema.Schema<T, any>;
|
|
94
98
|
outputSchema?: Schema.Schema<O, any>;
|
|
95
99
|
handler: FunctionHandler<T, O>;
|
|
100
|
+
meta?: {
|
|
101
|
+
/**
|
|
102
|
+
* Tools that are projected from functions have this annotation.
|
|
103
|
+
*
|
|
104
|
+
* deployedFunctionId:
|
|
105
|
+
* - Backend deployment ID assigned by the EDGE function service (typically a UUID).
|
|
106
|
+
* - Used for remote invocation via `FunctionInvocationService` → `RemoteFunctionExecutionService`.
|
|
107
|
+
* - Persisted on the corresponding ECHO `FunctionType` object's metadata under the
|
|
108
|
+
* `FUNCTIONS_META_KEY` and retrieved with `getUserFunctionIdInMetadata`.
|
|
109
|
+
*/
|
|
110
|
+
deployedFunctionId?: string;
|
|
111
|
+
};
|
|
96
112
|
};
|
|
97
113
|
|
|
98
|
-
// TODO(dmaretskyi):
|
|
99
|
-
export const defineFunction
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
114
|
+
// TODO(dmaretskyi): Output type doesn't get typechecked.
|
|
115
|
+
export const defineFunction: {
|
|
116
|
+
<I, O>(params: {
|
|
117
|
+
key: string;
|
|
118
|
+
name: string;
|
|
119
|
+
description?: string;
|
|
120
|
+
inputSchema: Schema.Schema<I, any>;
|
|
121
|
+
outputSchema?: Schema.Schema<O, any>;
|
|
122
|
+
handler: Types.NoInfer<FunctionHandler<I, O>>;
|
|
123
|
+
}): FunctionDefinition<I, O>;
|
|
124
|
+
} = ({ key, name, description, inputSchema, outputSchema = Schema.Any, handler }) => {
|
|
106
125
|
if (!Schema.isSchema(inputSchema)) {
|
|
107
126
|
throw new Error('Input schema must be a valid schema');
|
|
108
127
|
}
|
|
@@ -110,11 +129,94 @@ export const defineFunction = <T, O>({
|
|
|
110
129
|
throw new Error('Handler must be a function');
|
|
111
130
|
}
|
|
112
131
|
|
|
132
|
+
// Captures the function definition location.
|
|
133
|
+
const limit = Error.stackTraceLimit;
|
|
134
|
+
Error.stackTraceLimit = 2;
|
|
135
|
+
const traceError = new Error();
|
|
136
|
+
Error.stackTraceLimit = limit;
|
|
137
|
+
let cache: false | string = false;
|
|
138
|
+
const captureStackTrace = () => {
|
|
139
|
+
if (cache !== false) {
|
|
140
|
+
return cache;
|
|
141
|
+
}
|
|
142
|
+
if (traceError.stack !== undefined) {
|
|
143
|
+
const stack = traceError.stack.split('\n');
|
|
144
|
+
if (stack[2] !== undefined) {
|
|
145
|
+
cache = stack[2].trim();
|
|
146
|
+
return cache;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const handlerWithSpan = (...args: any[]) => {
|
|
152
|
+
const result = (handler as any)(...args);
|
|
153
|
+
if (Effect.isEffect(result)) {
|
|
154
|
+
return Effect.withSpan(result, `${key ?? name}`, {
|
|
155
|
+
captureStackTrace,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
159
|
+
};
|
|
160
|
+
|
|
113
161
|
return {
|
|
162
|
+
[typeId]: true,
|
|
163
|
+
key,
|
|
114
164
|
name,
|
|
115
165
|
description,
|
|
116
166
|
inputSchema,
|
|
117
167
|
outputSchema,
|
|
118
|
-
handler,
|
|
168
|
+
handler: handlerWithSpan,
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
export const FunctionDefinition = {
|
|
173
|
+
make: defineFunction,
|
|
174
|
+
isFunction: (value: unknown): value is FunctionDefinition.Any => {
|
|
175
|
+
return typeof value === 'object' && value !== null && Symbol.for('@dxos/functions/FunctionDefinition') in value;
|
|
176
|
+
},
|
|
177
|
+
serialize: (functionDef: FunctionDefinition.Any): FunctionType => {
|
|
178
|
+
assertArgument(FunctionDefinition.isFunction(functionDef), 'functionDef');
|
|
179
|
+
return serializeFunction(functionDef);
|
|
180
|
+
},
|
|
181
|
+
deserialize: (functionObj: FunctionType): FunctionDefinition.Any => {
|
|
182
|
+
assertArgument(Obj.instanceOf(FunctionType, functionObj), 'functionObj');
|
|
183
|
+
return deserializeFunction(functionObj);
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
export declare namespace FunctionDefinition {
|
|
187
|
+
export type Any = FunctionDefinition<any, any>;
|
|
188
|
+
export type Input<T extends FunctionDefinition> = T extends FunctionDefinition<infer I, any> ? I : never;
|
|
189
|
+
export type Output<T extends FunctionDefinition> = T extends FunctionDefinition<any, infer O> ? O : never;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export const serializeFunction = (functionDef: FunctionDefinition<any, any>): FunctionType => {
|
|
193
|
+
const fn = Obj.make(FunctionType, {
|
|
194
|
+
key: functionDef.key,
|
|
195
|
+
name: functionDef.name,
|
|
196
|
+
version: '0.1.0',
|
|
197
|
+
description: functionDef.description,
|
|
198
|
+
inputSchema: Type.toJsonSchema(functionDef.inputSchema),
|
|
199
|
+
outputSchema: !functionDef.outputSchema ? undefined : Type.toJsonSchema(functionDef.outputSchema),
|
|
200
|
+
});
|
|
201
|
+
if (functionDef.meta?.deployedFunctionId) {
|
|
202
|
+
setUserFunctionIdInMetadata(Obj.getMeta(fn), functionDef.meta.deployedFunctionId);
|
|
203
|
+
}
|
|
204
|
+
return fn;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
export const deserializeFunction = (functionObj: FunctionType): FunctionDefinition<unknown, unknown> => {
|
|
208
|
+
return {
|
|
209
|
+
[typeId]: true,
|
|
210
|
+
// TODO(dmaretskyi): Fix key.
|
|
211
|
+
key: functionObj.key ?? functionObj.name,
|
|
212
|
+
name: functionObj.name,
|
|
213
|
+
description: functionObj.description,
|
|
214
|
+
inputSchema: !functionObj.inputSchema ? Schema.Unknown : Type.toEffectSchema(functionObj.inputSchema),
|
|
215
|
+
outputSchema: !functionObj.outputSchema ? undefined : Type.toEffectSchema(functionObj.outputSchema),
|
|
216
|
+
// TODO(dmaretskyi): This should throw error.
|
|
217
|
+
handler: () => {},
|
|
218
|
+
meta: {
|
|
219
|
+
deployedFunctionId: getUserFunctionIdInMetadata(Obj.getMeta(functionObj)),
|
|
220
|
+
},
|
|
119
221
|
};
|
|
120
222
|
};
|
package/src/index.ts
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
export * from './errors';
|
|
5
6
|
export * from './handler';
|
|
6
7
|
export * from './schema';
|
|
7
8
|
export * from './trace';
|
|
8
9
|
export * from './types';
|
|
9
10
|
export * from './url';
|
|
11
|
+
export * from './triggers';
|
|
10
12
|
export * from './services';
|
|
11
13
|
export * from './executor';
|
|
12
|
-
export * from './
|
|
13
|
-
|
|
14
|
-
// Blow up cache
|
|
14
|
+
export * as exampleFunctions from './examples';
|
package/src/schema.ts
CHANGED
|
@@ -32,6 +32,17 @@ export interface ScriptType extends Schema.Schema.Type<typeof ScriptType> {}
|
|
|
32
32
|
* Function deployment.
|
|
33
33
|
*/
|
|
34
34
|
export const FunctionType = Schema.Struct({
|
|
35
|
+
/**
|
|
36
|
+
* Global registry ID.
|
|
37
|
+
* NOTE: The `key` property refers to the original registry entry.
|
|
38
|
+
*/
|
|
39
|
+
// TODO(burdon): Create Format type for DXN-like ids, such as this and schema type.
|
|
40
|
+
// TODO(dmaretskyi): Consider making it part of ECHO meta.
|
|
41
|
+
// TODO(dmaretskyi): Make required.
|
|
42
|
+
key: Schema.optional(Schema.String).annotations({
|
|
43
|
+
description: 'Unique registration key for the blueprint',
|
|
44
|
+
}),
|
|
45
|
+
|
|
35
46
|
// TODO(burdon): Rename to id/uri?
|
|
36
47
|
name: Schema.NonEmptyString,
|
|
37
48
|
version: Schema.String,
|
|
@@ -2,9 +2,15 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { HttpClient, HttpClientRequest } from '@effect/platform';
|
|
6
|
+
import { type Config, Context, Effect, Layer, Redacted } from 'effect';
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
import { Query } from '@dxos/echo';
|
|
9
|
+
import { DataType } from '@dxos/schema';
|
|
10
|
+
|
|
11
|
+
import { DatabaseService } from './database';
|
|
12
|
+
|
|
13
|
+
export type CredentialQuery = {
|
|
8
14
|
service?: string;
|
|
9
15
|
};
|
|
10
16
|
|
|
@@ -32,14 +38,70 @@ export class CredentialsService extends Context.Tag('@dxos/functions/Credentials
|
|
|
32
38
|
getCredential: (query: CredentialQuery) => Promise<ServiceCredential>;
|
|
33
39
|
}
|
|
34
40
|
>() {
|
|
35
|
-
static configuredLayer = (credentials: ServiceCredential[]) =>
|
|
36
|
-
Layer.succeed(CredentialsService, new ConfiguredCredentialsService(credentials));
|
|
37
|
-
|
|
38
41
|
static getCredential = (query: CredentialQuery): Effect.Effect<ServiceCredential, never, CredentialsService> =>
|
|
39
42
|
Effect.gen(function* () {
|
|
40
43
|
const credentials = yield* CredentialsService;
|
|
41
44
|
return yield* Effect.promise(() => credentials.getCredential(query));
|
|
42
45
|
});
|
|
46
|
+
|
|
47
|
+
static getApiKey = (query: CredentialQuery): Effect.Effect<Redacted.Redacted<string>, never, CredentialsService> =>
|
|
48
|
+
Effect.gen(function* () {
|
|
49
|
+
const credential = yield* CredentialsService.getCredential(query);
|
|
50
|
+
if (!credential.apiKey) {
|
|
51
|
+
throw new Error(`API key not found for service: ${query.service}`);
|
|
52
|
+
}
|
|
53
|
+
return Redacted.make(credential.apiKey);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
static configuredLayer = (credentials: ServiceCredential[]) =>
|
|
57
|
+
Layer.succeed(CredentialsService, new ConfiguredCredentialsService(credentials));
|
|
58
|
+
|
|
59
|
+
static layerConfig = (credentials: { service: string; apiKey: Config.Config<Redacted.Redacted<string>> }[]) =>
|
|
60
|
+
Layer.effect(
|
|
61
|
+
CredentialsService,
|
|
62
|
+
Effect.gen(function* () {
|
|
63
|
+
const serviceCredentials = yield* Effect.forEach(credentials, ({ service, apiKey }) =>
|
|
64
|
+
Effect.gen(function* () {
|
|
65
|
+
return {
|
|
66
|
+
service,
|
|
67
|
+
apiKey: Redacted.value(yield* apiKey),
|
|
68
|
+
};
|
|
69
|
+
}),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return new ConfiguredCredentialsService(serviceCredentials);
|
|
73
|
+
}),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
static layerFromDatabase = () =>
|
|
77
|
+
Layer.effect(
|
|
78
|
+
CredentialsService,
|
|
79
|
+
Effect.gen(function* () {
|
|
80
|
+
const dbService = yield* DatabaseService;
|
|
81
|
+
const queryCredentials = async (query: CredentialQuery): Promise<ServiceCredential[]> => {
|
|
82
|
+
const { objects: accessTokens } = await dbService.db.query(Query.type(DataType.AccessToken)).run();
|
|
83
|
+
return accessTokens
|
|
84
|
+
.filter((accessToken) => accessToken.source === query.service)
|
|
85
|
+
.map((accessToken) => ({
|
|
86
|
+
service: accessToken.source,
|
|
87
|
+
apiKey: accessToken.token,
|
|
88
|
+
}));
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
getCredential: async (query) => {
|
|
92
|
+
const credentials = await queryCredentials(query);
|
|
93
|
+
if (credentials.length === 0) {
|
|
94
|
+
throw new Error(`Credential not found for service: ${query.service}`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return credentials[0];
|
|
98
|
+
},
|
|
99
|
+
queryCredentials: async (query) => {
|
|
100
|
+
return queryCredentials(query);
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}),
|
|
104
|
+
);
|
|
43
105
|
}
|
|
44
106
|
|
|
45
107
|
export class ConfiguredCredentialsService implements Context.Tag.Service<CredentialsService> {
|
|
@@ -59,6 +121,19 @@ export class ConfiguredCredentialsService implements Context.Tag.Service<Credent
|
|
|
59
121
|
if (!credential) {
|
|
60
122
|
throw new Error(`Credential not found for service: ${query.service}`);
|
|
61
123
|
}
|
|
124
|
+
|
|
62
125
|
return credential;
|
|
63
126
|
}
|
|
64
127
|
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Maps the request to include the API key from the credential.
|
|
131
|
+
*/
|
|
132
|
+
export const withAuthorization = (query: CredentialQuery, kind?: 'Bearer' | 'Basic') =>
|
|
133
|
+
HttpClient.mapRequestEffect(
|
|
134
|
+
Effect.fnUntraced(function* (request) {
|
|
135
|
+
const key = yield* CredentialsService.getApiKey(query).pipe(Effect.map(Redacted.value));
|
|
136
|
+
const authorization = kind ? `${kind} ${key}` : key;
|
|
137
|
+
return HttpClientRequest.setHeader(request, 'Authorization', authorization);
|
|
138
|
+
}),
|
|
139
|
+
);
|
package/src/services/database.ts
CHANGED
|
@@ -2,10 +2,23 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Context, Effect, Layer } from 'effect';
|
|
5
|
+
import { Context, Effect, Layer, Option, type Schema } from 'effect';
|
|
6
6
|
|
|
7
|
-
import
|
|
8
|
-
|
|
7
|
+
import {
|
|
8
|
+
type Filter,
|
|
9
|
+
type Live,
|
|
10
|
+
Obj,
|
|
11
|
+
ObjectNotFoundError,
|
|
12
|
+
type Query,
|
|
13
|
+
type Ref,
|
|
14
|
+
type Relation,
|
|
15
|
+
type Type,
|
|
16
|
+
} from '@dxos/echo';
|
|
17
|
+
import type { EchoDatabase, FlushOptions, OneShotQueryResult, QueryResult, SchemaRegistryQuery } from '@dxos/echo-db';
|
|
18
|
+
import type { SchemaRegistryPreparedQuery } from '@dxos/echo-db';
|
|
19
|
+
import type { EchoSchema } from '@dxos/echo-schema';
|
|
20
|
+
import { promiseWithCauseCapture } from '@dxos/effect';
|
|
21
|
+
import { invariant } from '@dxos/invariant';
|
|
9
22
|
import type { DXN } from '@dxos/keys';
|
|
10
23
|
|
|
11
24
|
export class DatabaseService extends Context.Tag('@dxos/functions/DatabaseService')<
|
|
@@ -28,27 +41,96 @@ export class DatabaseService extends Context.Tag('@dxos/functions/DatabaseServic
|
|
|
28
41
|
};
|
|
29
42
|
};
|
|
30
43
|
|
|
31
|
-
static
|
|
44
|
+
static layer = (db: EchoDatabase): Layer.Layer<DatabaseService> => {
|
|
32
45
|
return Layer.succeed(DatabaseService, DatabaseService.make(db));
|
|
33
46
|
};
|
|
34
47
|
|
|
35
|
-
|
|
36
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Resolves an object by its DXN.
|
|
50
|
+
*/
|
|
51
|
+
static resolve: {
|
|
52
|
+
// No type check.
|
|
53
|
+
(dxn: DXN): Effect.Effect<Obj.Any | Relation.Any, never, DatabaseService>;
|
|
54
|
+
// Check matches schema.
|
|
55
|
+
<S extends Type.Obj.Any | Type.Relation.Any>(
|
|
56
|
+
dxn: DXN,
|
|
57
|
+
schema: S,
|
|
58
|
+
): Effect.Effect<Schema.Schema.Type<S>, ObjectNotFoundError, DatabaseService>;
|
|
59
|
+
} = (<S extends Type.Obj.Any | Type.Relation.Any>(
|
|
60
|
+
dxn: DXN,
|
|
61
|
+
schema?: S,
|
|
62
|
+
): Effect.Effect<Schema.Schema.Type<S>, ObjectNotFoundError, DatabaseService> =>
|
|
63
|
+
Effect.gen(function* () {
|
|
37
64
|
const { db } = yield* DatabaseService;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
65
|
+
const object = yield* promiseWithCauseCapture(() =>
|
|
66
|
+
db.graph
|
|
67
|
+
.createRefResolver({
|
|
68
|
+
context: {
|
|
69
|
+
space: db.spaceId,
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
.resolve(dxn),
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
if (!object) {
|
|
76
|
+
return yield* Effect.fail(new ObjectNotFoundError(dxn));
|
|
77
|
+
}
|
|
78
|
+
invariant(!schema || Obj.instanceOf(schema, object), 'Object type mismatch.');
|
|
79
|
+
return object as any;
|
|
80
|
+
})) as any;
|
|
47
81
|
|
|
48
|
-
|
|
49
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Loads an object reference.
|
|
84
|
+
*/
|
|
85
|
+
static load: <T>(ref: Ref.Ref<T>) => Effect.Effect<T, ObjectNotFoundError, never> = Effect.fn(function* (ref) {
|
|
86
|
+
const object = yield* promiseWithCauseCapture(() => ref.tryLoad());
|
|
87
|
+
if (!object) {
|
|
88
|
+
return yield* Effect.fail(new ObjectNotFoundError(ref.dxn));
|
|
89
|
+
}
|
|
90
|
+
return object;
|
|
50
91
|
});
|
|
51
92
|
|
|
93
|
+
/**
|
|
94
|
+
* Loads an object reference option.
|
|
95
|
+
*/
|
|
96
|
+
// TODO(burdon): Option?
|
|
97
|
+
static loadOption: <T>(ref: Ref.Ref<T>) => Effect.Effect<Option.Option<T>, never, never> = Effect.fn(function* (ref) {
|
|
98
|
+
const object = yield* DatabaseService.load(ref).pipe(
|
|
99
|
+
Effect.catchTag('OBJECT_NOT_FOUND', () => Effect.succeed(undefined)),
|
|
100
|
+
);
|
|
101
|
+
return Option.fromNullable(object);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// TODO(burdon): Can we create a proxy for the following methods on EchoDatabase? Use @inheritDoc?
|
|
105
|
+
// TODO(burdon): Figure out how to chain query().run();
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @link EchoDatabase.add
|
|
109
|
+
*/
|
|
110
|
+
static add = <T extends Obj.Any | Relation.Any>(obj: T): Effect.Effect<T, never, DatabaseService> =>
|
|
111
|
+
DatabaseService.pipe(Effect.map(({ db }) => db.add(obj)));
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @link EchoDatabase.remove
|
|
115
|
+
*/
|
|
116
|
+
static remove = <T extends Obj.Any | Relation.Any>(obj: T): Effect.Effect<void, never, DatabaseService> =>
|
|
117
|
+
DatabaseService.pipe(Effect.map(({ db }) => db.remove(obj)));
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @link EchoDatabase.flush
|
|
121
|
+
*/
|
|
122
|
+
static flush = (opts?: FlushOptions) =>
|
|
123
|
+
DatabaseService.pipe(Effect.flatMap(({ db }) => promiseWithCauseCapture(() => db.flush(opts))));
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* @link EchoDatabase.getObjectById
|
|
127
|
+
*/
|
|
128
|
+
static getObjectById = <T extends Obj.Any | Relation.Any>(
|
|
129
|
+
id: string,
|
|
130
|
+
): Effect.Effect<Live<T> | undefined, never, DatabaseService> => {
|
|
131
|
+
return DatabaseService.pipe(Effect.map(({ db }) => db.getObjectById(id)));
|
|
132
|
+
};
|
|
133
|
+
|
|
52
134
|
/**
|
|
53
135
|
* Creates a `QueryResult` object that can be subscribed to.
|
|
54
136
|
*/
|
|
@@ -69,6 +151,21 @@ export class DatabaseService extends Context.Tag('@dxos/functions/DatabaseServic
|
|
|
69
151
|
<F extends Filter.Any>(filter: F): Effect.Effect<OneShotQueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;
|
|
70
152
|
} = (queryOrFilter: Query.Any | Filter.Any) =>
|
|
71
153
|
DatabaseService.query(queryOrFilter as any).pipe(
|
|
72
|
-
Effect.flatMap((queryResult) =>
|
|
154
|
+
Effect.flatMap((queryResult) => promiseWithCauseCapture(() => queryResult.run())),
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
static schemaQuery = <Q extends SchemaRegistryQuery>(
|
|
158
|
+
query: Q,
|
|
159
|
+
): Effect.Effect<SchemaRegistryPreparedQuery<EchoSchema>, never, DatabaseService> =>
|
|
160
|
+
DatabaseService.pipe(
|
|
161
|
+
Effect.map(({ db }) => db.schemaRegistry.query(query)),
|
|
162
|
+
Effect.withSpan('DatabaseService.schemaQuery'),
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
static runSchemaQuery = <Q extends SchemaRegistryQuery>(
|
|
166
|
+
query: Q,
|
|
167
|
+
): Effect.Effect<EchoSchema[], never, DatabaseService> =>
|
|
168
|
+
DatabaseService.schemaQuery(query).pipe(
|
|
169
|
+
Effect.flatMap((queryResult) => promiseWithCauseCapture(() => queryResult.run())),
|
|
73
170
|
);
|
|
74
171
|
}
|
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Effect,
|
|
5
|
+
import { Context, Effect, Layer, Schema } from 'effect';
|
|
6
6
|
|
|
7
7
|
import { Obj, Type } from '@dxos/echo';
|
|
8
8
|
import { invariant } from '@dxos/invariant';
|
|
9
|
-
import {
|
|
9
|
+
import { LogLevel, log } from '@dxos/log';
|
|
10
10
|
|
|
11
11
|
import { TracingService } from './tracing';
|
|
12
12
|
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, expect, it } from '@effect/vitest';
|
|
6
|
+
import { Effect, Layer, Schema } from 'effect';
|
|
7
|
+
|
|
8
|
+
import { AiService } from '@dxos/ai';
|
|
9
|
+
|
|
10
|
+
import { defineFunction } from '../handler';
|
|
11
|
+
import { TestDatabaseLayer } from '../testing';
|
|
12
|
+
|
|
13
|
+
import { FunctionInvocationService } from './function-invocation-service';
|
|
14
|
+
import { FunctionImplementationResolver } from './local-function-execution';
|
|
15
|
+
|
|
16
|
+
const TestLayer = Layer.mergeAll(AiService.model('@anthropic/claude-opus-4-0')).pipe(
|
|
17
|
+
Layer.provideMerge(
|
|
18
|
+
Layer.mergeAll(
|
|
19
|
+
TestDatabaseLayer({
|
|
20
|
+
indexing: { vector: true },
|
|
21
|
+
types: [],
|
|
22
|
+
}),
|
|
23
|
+
FunctionInvocationService.layer,
|
|
24
|
+
),
|
|
25
|
+
),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
describe('FunctionInvocationService', () => {
|
|
29
|
+
it(
|
|
30
|
+
'should be defined',
|
|
31
|
+
Effect.fnUntraced(function* () {
|
|
32
|
+
const service = yield* FunctionInvocationService;
|
|
33
|
+
expect(service).toBeDefined();
|
|
34
|
+
}, Effect.provide(TestLayer)),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
it(
|
|
38
|
+
'routes to local when implementation is available',
|
|
39
|
+
Effect.fnUntraced(function* () {
|
|
40
|
+
const add = defineFunction({
|
|
41
|
+
key: 'example.org/function/add',
|
|
42
|
+
name: 'add',
|
|
43
|
+
inputSchema: Schema.Struct({ a: Schema.Number, b: Schema.Number }),
|
|
44
|
+
outputSchema: Schema.Number,
|
|
45
|
+
handler: ({ data }) => data.a + data.b,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const layer = TestLayer.pipe(Layer.provideMerge(FunctionImplementationResolver.layerTest({ functions: [add] })));
|
|
49
|
+
|
|
50
|
+
const result = yield* Effect.gen(function* () {
|
|
51
|
+
return yield* FunctionInvocationService.invokeFunction(add, { a: 2, b: 3 });
|
|
52
|
+
}).pipe(Effect.provide(layer));
|
|
53
|
+
|
|
54
|
+
expect(result).toEqual(5);
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
it(
|
|
59
|
+
'routes to remote when no local implementation is found',
|
|
60
|
+
Effect.fnUntraced(function* () {
|
|
61
|
+
// This function is not deployed, so mock layer will be used.
|
|
62
|
+
const echo = defineFunction({
|
|
63
|
+
key: 'example.org/function/echo',
|
|
64
|
+
name: 'function-that-is-deployed',
|
|
65
|
+
inputSchema: Schema.Unknown,
|
|
66
|
+
outputSchema: Schema.Unknown,
|
|
67
|
+
handler: () => {},
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// No resolver provided → resolveFunctionImplementation will fail → remote path is used.
|
|
71
|
+
const result = yield* Effect.gen(function* () {
|
|
72
|
+
return yield* FunctionInvocationService.invokeFunction(echo, { hello: 'world' });
|
|
73
|
+
}).pipe(Effect.provide(TestLayer));
|
|
74
|
+
|
|
75
|
+
// RemoteFunctionExecutionService.mock echos input back.
|
|
76
|
+
expect(result).toEqual({ hello: 'world', resolved: 'remote' });
|
|
77
|
+
}),
|
|
78
|
+
);
|
|
79
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
import { Context, Effect, Layer } from 'effect';
|
|
5
|
+
|
|
6
|
+
import { AiService } from '@dxos/ai';
|
|
7
|
+
|
|
8
|
+
import { type FunctionDefinition } from '../handler';
|
|
9
|
+
|
|
10
|
+
import { CredentialsService } from './credentials';
|
|
11
|
+
import { DatabaseService } from './database';
|
|
12
|
+
import {
|
|
13
|
+
FunctionImplementationResolver,
|
|
14
|
+
type InvocationServices,
|
|
15
|
+
LocalFunctionExecutionService,
|
|
16
|
+
} from './local-function-execution';
|
|
17
|
+
import { QueueService } from './queues';
|
|
18
|
+
import { RemoteFunctionExecutionService } from './remote-function-execution-service';
|
|
19
|
+
|
|
20
|
+
export class FunctionInvocationService extends Context.Tag('@dxos/functions/FunctionInvocationService')<
|
|
21
|
+
FunctionInvocationService,
|
|
22
|
+
{
|
|
23
|
+
invokeFunction<I, O>(functionDef: FunctionDefinition<I, O>, input: I): Effect.Effect<O, never, InvocationServices>;
|
|
24
|
+
}
|
|
25
|
+
>() {
|
|
26
|
+
static invokeFunction = Effect.serviceFunctionEffect(FunctionInvocationService, (_) => _.invokeFunction);
|
|
27
|
+
|
|
28
|
+
static layer = Layer.effect(
|
|
29
|
+
FunctionInvocationService,
|
|
30
|
+
Effect.gen(function* () {
|
|
31
|
+
const localExecutioner = yield* LocalFunctionExecutionService;
|
|
32
|
+
const remoteExecutioner = yield* RemoteFunctionExecutionService;
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
invokeFunction: <I, O>(
|
|
36
|
+
functionDef: FunctionDefinition<I, O>,
|
|
37
|
+
input: I,
|
|
38
|
+
): Effect.Effect<O, never, InvocationServices> =>
|
|
39
|
+
Effect.gen(function* () {
|
|
40
|
+
if (functionDef.meta?.deployedFunctionId) {
|
|
41
|
+
return yield* remoteExecutioner.callFunction<I, O>(functionDef.meta.deployedFunctionId, input);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return yield* localExecutioner.invokeFunction(functionDef, input);
|
|
45
|
+
}),
|
|
46
|
+
} satisfies Context.Tag.Service<FunctionInvocationService>;
|
|
47
|
+
}),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
// TODO(dmaretskyi): Don't provide `FunctionImplementationResolver`.
|
|
51
|
+
static layerTest = ({
|
|
52
|
+
functions = [],
|
|
53
|
+
}: {
|
|
54
|
+
functions?: FunctionDefinition<any, any>[];
|
|
55
|
+
} = {}): Layer.Layer<
|
|
56
|
+
FunctionInvocationService,
|
|
57
|
+
never,
|
|
58
|
+
AiService.AiService | CredentialsService | DatabaseService | QueueService
|
|
59
|
+
> =>
|
|
60
|
+
FunctionInvocationService.layer.pipe(
|
|
61
|
+
Layer.provide(LocalFunctionExecutionService.layerLive),
|
|
62
|
+
Layer.provide(FunctionImplementationResolver.layerTest({ functions })),
|
|
63
|
+
Layer.provide(RemoteFunctionExecutionService.layerMock),
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
// TODO(dmaretskyi): This shouldn't default to all services being not available.
|
|
67
|
+
// TODO(dmaretskyi): Don't provide `FunctionImplementationResolver`.
|
|
68
|
+
/**
|
|
69
|
+
* @deprecated Use {@link layerTest} instead.
|
|
70
|
+
*/
|
|
71
|
+
static layerTestMocked = ({
|
|
72
|
+
functions,
|
|
73
|
+
}: {
|
|
74
|
+
functions: FunctionDefinition<any, any>[];
|
|
75
|
+
}): Layer.Layer<FunctionInvocationService> =>
|
|
76
|
+
FunctionInvocationService.layerTest({ functions }).pipe(
|
|
77
|
+
Layer.provide(AiService.notAvailable),
|
|
78
|
+
Layer.provide(CredentialsService.configuredLayer([])),
|
|
79
|
+
Layer.provide(DatabaseService.notAvailable),
|
|
80
|
+
Layer.provide(QueueService.notAvailable),
|
|
81
|
+
);
|
|
82
|
+
}
|
package/src/services/index.ts
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
export * from './credentials';
|
|
5
6
|
export * from './database';
|
|
7
|
+
export * from './event-logger';
|
|
8
|
+
export * from './function-invocation-service';
|
|
9
|
+
export * from './local-function-execution';
|
|
6
10
|
export * from './queues';
|
|
7
11
|
export * from './service-container';
|
|
8
|
-
export * from './credentials';
|
|
9
12
|
export * from './tracing';
|
|
10
|
-
export * from './event-logger';
|
|
11
13
|
export * from './remote-function-execution-service';
|
|
12
|
-
export * from './local-function-execution';
|