@dxos/functions 0.8.4-main.b97322e → 0.8.4-main.c4373fc
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 +1035 -143
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +81 -41
- 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 +1035 -143
- 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 +81 -41
- 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 +20 -3
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/database.d.ts +44 -6
- package/dist/types/src/services/database.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +68 -30
- 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 +5 -3
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/local-function-execution.d.ts +34 -0
- package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
- package/dist/types/src/services/queues.d.ts +23 -6
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/remote-function-execution-service.d.ts +22 -0
- package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -0
- package/dist/types/src/services/service-container.d.ts +7 -6
- package/dist/types/src/services/service-container.d.ts.map +1 -1
- package/dist/types/src/services/service-registry.d.ts +4 -2
- package/dist/types/src/services/service-registry.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +39 -5
- 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 +4 -4
- 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 +7 -18
- 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 +38 -42
- 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 +86 -3
- package/src/services/database.ts +123 -18
- package/src/services/event-logger.ts +71 -37
- package/src/services/function-invocation-service.test.ts +81 -0
- package/src/services/function-invocation-service.ts +84 -0
- package/src/services/index.ts +5 -3
- package/src/services/local-function-execution.ts +153 -0
- package/src/services/queues.ts +39 -10
- package/src/services/remote-function-execution-service.ts +63 -0
- package/src/services/service-container.ts +13 -11
- package/src/services/service-registry.test.ts +4 -1
- package/src/services/service-registry.ts +11 -4
- package/src/services/tracing.ts +112 -11
- package/src/testing/layer.ts +87 -4
- package/src/testing/logger.ts +5 -4
- package/src/testing/persist-database.test.ts +87 -0
- package/src/testing/services.ts +11 -64
- 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-3NGCSUEW.mjs +0 -328
- package/dist/lib/browser/chunk-3NGCSUEW.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-FJ2MU7TL.mjs +0 -330
- package/dist/lib/node-esm/chunk-FJ2MU7TL.mjs.map +0 -7
- package/dist/types/src/services/function-call-service.d.ts +0 -16
- package/dist/types/src/services/function-call-service.d.ts.map +0 -1
- package/src/services/function-call-service.ts +0 -64
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
|
|
|
@@ -17,7 +28,7 @@ export type ServiceCredential = {
|
|
|
17
28
|
apiKey?: string;
|
|
18
29
|
};
|
|
19
30
|
|
|
20
|
-
export class CredentialsService extends Context.Tag('CredentialsService')<
|
|
31
|
+
export class CredentialsService extends Context.Tag('@dxos/functions/CredentialsService')<
|
|
21
32
|
CredentialsService,
|
|
22
33
|
{
|
|
23
34
|
/**
|
|
@@ -37,6 +48,65 @@ export class CredentialsService extends Context.Tag('CredentialsService')<
|
|
|
37
48
|
const credentials = yield* CredentialsService;
|
|
38
49
|
return yield* Effect.promise(() => credentials.getCredential(query));
|
|
39
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
|
+
);
|
|
40
110
|
}
|
|
41
111
|
|
|
42
112
|
export class ConfiguredCredentialsService implements Context.Tag.Service<CredentialsService> {
|
|
@@ -56,6 +126,19 @@ export class ConfiguredCredentialsService implements Context.Tag.Service<Credent
|
|
|
56
126
|
if (!credential) {
|
|
57
127
|
throw new Error(`Credential not found for service: ${query.service}`);
|
|
58
128
|
}
|
|
129
|
+
|
|
59
130
|
return credential;
|
|
60
131
|
}
|
|
61
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,13 +2,30 @@
|
|
|
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
|
-
export class DatabaseService extends Context.Tag('DatabaseService')<
|
|
28
|
+
export class DatabaseService extends Context.Tag('@dxos/functions/DatabaseService')<
|
|
12
29
|
DatabaseService,
|
|
13
30
|
{
|
|
14
31
|
readonly db: EchoDatabase;
|
|
@@ -28,23 +45,96 @@ export class DatabaseService extends Context.Tag('DatabaseService')<
|
|
|
28
45
|
};
|
|
29
46
|
};
|
|
30
47
|
|
|
31
|
-
static
|
|
32
|
-
|
|
48
|
+
static layer = (db: EchoDatabase): Layer.Layer<DatabaseService> => {
|
|
49
|
+
return Layer.succeed(DatabaseService, DatabaseService.make(db));
|
|
50
|
+
};
|
|
51
|
+
|
|
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* () {
|
|
33
68
|
const { db } = yield* DatabaseService;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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;
|
|
85
|
+
|
|
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;
|
|
95
|
+
});
|
|
43
96
|
|
|
44
|
-
|
|
45
|
-
|
|
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);
|
|
46
106
|
});
|
|
47
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
|
+
|
|
48
138
|
/**
|
|
49
139
|
* Creates a `QueryResult` object that can be subscribed to.
|
|
50
140
|
*/
|
|
@@ -65,6 +155,21 @@ export class DatabaseService extends Context.Tag('DatabaseService')<
|
|
|
65
155
|
<F extends Filter.Any>(filter: F): Effect.Effect<OneShotQueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;
|
|
66
156
|
} = (queryOrFilter: Query.Any | Filter.Any) =>
|
|
67
157
|
DatabaseService.query(queryOrFilter as any).pipe(
|
|
68
|
-
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())),
|
|
69
174
|
);
|
|
70
175
|
}
|
|
@@ -2,53 +2,84 @@
|
|
|
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
|
|
|
10
|
+
import { Obj, Type } from '@dxos/echo';
|
|
7
11
|
import { invariant } from '@dxos/invariant';
|
|
8
|
-
import {
|
|
12
|
+
import { LogLevel, log } from '@dxos/log';
|
|
9
13
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
14
|
+
import { TracingService } from './tracing';
|
|
15
|
+
|
|
16
|
+
export const ComputeEventPayload = Schema.Union(
|
|
17
|
+
Schema.Struct({
|
|
18
|
+
type: Schema.Literal('begin-compute'),
|
|
19
|
+
nodeId: Schema.String,
|
|
20
|
+
inputs: Schema.Record({ key: Schema.String, value: Schema.Any }),
|
|
21
|
+
}),
|
|
22
|
+
Schema.Struct({
|
|
23
|
+
type: Schema.Literal('end-compute'),
|
|
24
|
+
nodeId: Schema.String,
|
|
25
|
+
outputs: Schema.Record({ key: Schema.String, value: Schema.Any }),
|
|
26
|
+
}),
|
|
27
|
+
Schema.Struct({
|
|
28
|
+
type: Schema.Literal('compute-input'),
|
|
29
|
+
nodeId: Schema.String,
|
|
30
|
+
property: Schema.String,
|
|
31
|
+
value: Schema.Any,
|
|
32
|
+
}),
|
|
33
|
+
Schema.Struct({
|
|
34
|
+
type: Schema.Literal('compute-output'),
|
|
35
|
+
nodeId: Schema.String,
|
|
36
|
+
property: Schema.String,
|
|
37
|
+
value: Schema.Any,
|
|
38
|
+
}),
|
|
39
|
+
Schema.Struct({
|
|
40
|
+
type: Schema.Literal('custom'),
|
|
41
|
+
nodeId: Schema.String,
|
|
42
|
+
event: Schema.Any,
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
45
|
+
export type ComputeEventPayload = Schema.Schema.Type<typeof ComputeEventPayload>;
|
|
38
46
|
|
|
39
|
-
export
|
|
40
|
-
|
|
41
|
-
|
|
47
|
+
export const ComputeEvent = Schema.Struct({
|
|
48
|
+
payload: ComputeEventPayload,
|
|
49
|
+
}).pipe(Type.Obj({ typename: 'dxos.org/type/ComputeEvent', version: '0.1.0' }));
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Logs event for the compute workflows.
|
|
53
|
+
*/
|
|
54
|
+
export class ComputeEventLogger extends Context.Tag('@dxos/functions/ComputeEventLogger')<
|
|
55
|
+
ComputeEventLogger,
|
|
56
|
+
{ readonly log: (event: ComputeEventPayload) => void; readonly nodeId: string | undefined }
|
|
42
57
|
>() {
|
|
43
|
-
static noop: Context.Tag.Service<
|
|
58
|
+
static noop: Context.Tag.Service<ComputeEventLogger> = {
|
|
44
59
|
log: () => {},
|
|
45
60
|
nodeId: undefined,
|
|
46
61
|
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Implements ComputeEventLogger using TracingService.
|
|
65
|
+
*/
|
|
66
|
+
static layerFromTracing = Layer.effect(
|
|
67
|
+
ComputeEventLogger,
|
|
68
|
+
Effect.gen(function* () {
|
|
69
|
+
const tracing = yield* TracingService;
|
|
70
|
+
return {
|
|
71
|
+
log: (event: ComputeEventPayload) => {
|
|
72
|
+
tracing.write(Obj.make(ComputeEvent, { payload: event }));
|
|
73
|
+
},
|
|
74
|
+
nodeId: undefined,
|
|
75
|
+
};
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
47
78
|
}
|
|
48
79
|
|
|
49
80
|
export const logCustomEvent = (data: any) =>
|
|
50
81
|
Effect.gen(function* () {
|
|
51
|
-
const logger = yield*
|
|
82
|
+
const logger = yield* ComputeEventLogger;
|
|
52
83
|
if (!logger.nodeId) {
|
|
53
84
|
throw new Error('logCustomEvent must be called within a node compute function');
|
|
54
85
|
}
|
|
@@ -67,7 +98,10 @@ export const createDefectLogger = <A, E, R>(): ((self: Effect.Effect<A, E, R>) =
|
|
|
67
98
|
}),
|
|
68
99
|
);
|
|
69
100
|
|
|
70
|
-
export const createEventLogger = (
|
|
101
|
+
export const createEventLogger = (
|
|
102
|
+
level: LogLevel,
|
|
103
|
+
message: string = 'event',
|
|
104
|
+
): Context.Tag.Service<ComputeEventLogger> => {
|
|
71
105
|
const logFunction = (
|
|
72
106
|
{
|
|
73
107
|
[LogLevel.WARN]: log.warn,
|
|
@@ -79,7 +113,7 @@ export const createEventLogger = (level: LogLevel, message: string = 'event'): C
|
|
|
79
113
|
)[level];
|
|
80
114
|
invariant(logFunction);
|
|
81
115
|
return {
|
|
82
|
-
log: (event:
|
|
116
|
+
log: (event: ComputeEventPayload) => {
|
|
83
117
|
logFunction(message, event);
|
|
84
118
|
},
|
|
85
119
|
nodeId: undefined,
|