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