@dxos/functions 0.8.4-main.84f28bd → 0.8.4-main.ae835ea
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 +43 -34
- 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-M6EXIREF.mjs +610 -0
- package/dist/lib/browser/chunk-M6EXIREF.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 +1106 -252
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +94 -42
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node-esm/bundler/index.mjs +42 -34
- 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-P3IATZMZ.mjs +612 -0
- package/dist/lib/node-esm/chunk-P3IATZMZ.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 +1106 -252
- 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 +94 -42
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- 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/e2e/deploy.test.d.ts +2 -0
- package/dist/types/src/e2e/deploy.test.d.ts.map +1 -0
- package/dist/types/src/edge/functions.d.ts +4 -3
- package/dist/types/src/edge/functions.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +137 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/example/fib.d.ts +7 -0
- package/dist/types/src/example/fib.d.ts.map +1 -0
- package/dist/types/src/example/forex-effect.d.ts +3 -0
- package/dist/types/src/example/forex-effect.d.ts.map +1 -0
- package/dist/types/src/example/index.d.ts +12 -0
- package/dist/types/src/example/index.d.ts.map +1 -0
- package/dist/types/src/example/reply.d.ts +3 -0
- package/dist/types/src/example/reply.d.ts.map +1 -0
- package/dist/types/src/example/sleep.d.ts +5 -0
- package/dist/types/src/example/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 +54 -13
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +5 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/services/credentials.d.ts +21 -3
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/database.d.ts +58 -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 -5
- 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 +33 -4
- 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 +29 -18
- package/dist/types/src/services/service-container.d.ts.map +1 -1
- package/dist/types/src/services/service-registry.d.ts +31 -0
- package/dist/types/src/services/service-registry.d.ts.map +1 -0
- package/dist/types/src/services/service-registry.test.d.ts +2 -0
- package/dist/types/src/services/service-registry.test.d.ts.map +1 -0
- package/dist/types/src/services/tracing.d.ts +48 -4
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +1 -0
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +18 -0
- package/dist/types/src/testing/layer.d.ts.map +1 -0
- 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 +8 -20
- 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/translations.d.ts +2 -2
- package/dist/types/src/translations.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/Function.d.ts +47 -0
- package/dist/types/src/types/Function.d.ts.map +1 -0
- package/dist/types/src/types/Script.d.ts +28 -0
- package/dist/types/src/types/Script.d.ts.map +1 -0
- package/dist/types/src/types/Trigger.d.ts +139 -0
- package/dist/types/src/types/Trigger.d.ts.map +1 -0
- package/dist/types/src/types/TriggerEvent.d.ts +44 -0
- package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +5 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- 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 +37 -42
- package/src/bundler/bundler.test.ts +8 -9
- package/src/bundler/bundler.ts +38 -35
- package/src/e2e/deploy.test.ts +69 -0
- package/src/edge/functions.ts +9 -6
- package/src/errors.ts +21 -0
- package/src/example/fib.ts +32 -0
- package/src/example/forex-effect.ts +40 -0
- package/src/example/index.ts +13 -0
- package/src/example/reply.ts +21 -0
- package/src/example/sleep.ts +24 -0
- package/src/executor/executor.ts +14 -10
- package/src/handler.ts +139 -26
- package/src/index.ts +5 -5
- package/src/services/credentials.ts +93 -4
- package/src/services/database.ts +145 -20
- 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 -5
- package/src/services/local-function-execution.ts +153 -0
- package/src/services/queues.ts +50 -8
- package/src/services/remote-function-execution-service.ts +63 -0
- package/src/services/service-container.ts +46 -58
- package/src/services/service-registry.test.ts +45 -0
- package/src/services/service-registry.ts +63 -0
- package/src/services/tracing.ts +122 -6
- package/src/testing/index.ts +1 -0
- package/src/testing/layer.ts +114 -0
- package/src/testing/logger.ts +5 -4
- package/src/testing/persist-database.test.ts +87 -0
- package/src/testing/services.ts +12 -71
- package/src/trace.ts +20 -22
- package/src/translations.ts +2 -2
- 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 +664 -0
- package/src/triggers/trigger-dispatcher.ts +521 -0
- package/src/triggers/trigger-state-store.ts +61 -0
- package/src/types/Function.ts +51 -0
- package/src/types/Script.ts +33 -0
- package/src/types/Trigger.ts +139 -0
- package/src/types/TriggerEvent.ts +62 -0
- package/src/types/index.ts +8 -0
- package/src/url.ts +14 -11
- package/dist/lib/browser/chunk-54U464M4.mjs +0 -360
- package/dist/lib/browser/chunk-54U464M4.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-XDSX35BS.mjs +0 -362
- package/dist/lib/node-esm/chunk-XDSX35BS.mjs.map +0 -7
- package/dist/types/src/schema.d.ts +0 -38
- package/dist/types/src/schema.d.ts.map +0 -1
- package/dist/types/src/services/ai.d.ts +0 -12
- package/dist/types/src/services/ai.d.ts.map +0 -1
- 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/dist/types/src/services/tool-resolver.d.ts +0 -14
- package/dist/types/src/services/tool-resolver.d.ts.map +0 -1
- package/dist/types/src/types.d.ts +0 -411
- package/dist/types/src/types.d.ts.map +0 -1
- package/src/schema.ts +0 -57
- package/src/services/ai.ts +0 -32
- package/src/services/function-call-service.ts +0 -64
- package/src/services/tool-resolver.ts +0 -31
- package/src/types.ts +0 -211
package/src/handler.ts
CHANGED
|
@@ -2,16 +2,20 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
import
|
|
10
|
-
import { type HasId } from '@dxos/echo
|
|
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
|
+
|
|
9
|
+
import { Obj, Type } from '@dxos/echo';
|
|
10
|
+
import { type HasId } from '@dxos/echo/internal';
|
|
11
|
+
import { type EchoDatabase } from '@dxos/echo-db';
|
|
12
|
+
import { assertArgument } from '@dxos/invariant';
|
|
13
|
+
import { type DXN, type SpaceId } from '@dxos/keys';
|
|
12
14
|
import { type QueryResult } from '@dxos/protocols';
|
|
13
15
|
|
|
14
|
-
import type
|
|
16
|
+
import { type Services } from './services';
|
|
17
|
+
import { Function } from './types';
|
|
18
|
+
import { getUserFunctionIdInMetadata, setUserFunctionIdInMetadata } from './url';
|
|
15
19
|
|
|
16
20
|
// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
|
|
17
21
|
// https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
|
|
@@ -39,6 +43,11 @@ export type FunctionHandler<TData = {}, TOutput = any> = (params: {
|
|
|
39
43
|
* Function context.
|
|
40
44
|
*/
|
|
41
45
|
export interface FunctionContext {
|
|
46
|
+
/**
|
|
47
|
+
* Space from which the function was invoked.
|
|
48
|
+
*/
|
|
49
|
+
space: SpaceAPI | undefined;
|
|
50
|
+
|
|
42
51
|
/**
|
|
43
52
|
* Resolves a service available to the function.
|
|
44
53
|
* @throws if the service is not available.
|
|
@@ -46,13 +55,6 @@ export interface FunctionContext {
|
|
|
46
55
|
getService: <T extends Context.Tag<any, any>>(tag: T) => Context.Tag.Service<T>;
|
|
47
56
|
|
|
48
57
|
getSpace: (spaceId: SpaceId) => Promise<SpaceAPI>;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Space from which the function was invoked.
|
|
52
|
-
*/
|
|
53
|
-
space: SpaceAPI | undefined;
|
|
54
|
-
|
|
55
|
-
ai: AiServiceClient;
|
|
56
58
|
}
|
|
57
59
|
|
|
58
60
|
export interface FunctionContextAi {
|
|
@@ -76,10 +78,9 @@ export interface QueuesAPI {
|
|
|
76
78
|
*/
|
|
77
79
|
export interface SpaceAPI {
|
|
78
80
|
get id(): SpaceId;
|
|
79
|
-
|
|
80
81
|
get db(): EchoDatabase;
|
|
81
82
|
|
|
82
|
-
// TODO(dmaretskyi): Align with echo api
|
|
83
|
+
// TODO(dmaretskyi): Align with echo api: queues.get(id).append(items);
|
|
83
84
|
get queues(): QueuesAPI;
|
|
84
85
|
}
|
|
85
86
|
|
|
@@ -88,26 +89,138 @@ const __assertFunctionSpaceIsCompatibleWithTheClientSpace = () => {
|
|
|
88
89
|
// const _: SpaceAPI = {} as Space;
|
|
89
90
|
};
|
|
90
91
|
|
|
91
|
-
|
|
92
|
+
const typeId = Symbol.for('@dxos/functions/FunctionDefinition');
|
|
93
|
+
|
|
94
|
+
export type FunctionDefinition<T = any, O = any> = {
|
|
95
|
+
[typeId]: true;
|
|
96
|
+
key: string;
|
|
97
|
+
name: string;
|
|
98
|
+
description?: string;
|
|
99
|
+
inputSchema: Schema.Schema<T, any>;
|
|
100
|
+
outputSchema?: Schema.Schema<O, any>;
|
|
101
|
+
handler: FunctionHandler<T, O>;
|
|
102
|
+
meta?: {
|
|
103
|
+
/**
|
|
104
|
+
* Tools that are projected from functions have this annotation.
|
|
105
|
+
*
|
|
106
|
+
* deployedFunctionId:
|
|
107
|
+
* - Backend deployment ID assigned by the EDGE function service (typically a UUID).
|
|
108
|
+
* - Used for remote invocation via `FunctionInvocationService` → `RemoteFunctionExecutionService`.
|
|
109
|
+
* - Persisted on the corresponding ECHO `Function.Function` object's metadata under the
|
|
110
|
+
* `FUNCTIONS_META_KEY` and retrieved with `getUserFunctionIdInMetadata`.
|
|
111
|
+
*/
|
|
112
|
+
deployedFunctionId?: string;
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export type FunctionProps<T, O> = {
|
|
117
|
+
key: string;
|
|
118
|
+
name: string;
|
|
92
119
|
description?: string;
|
|
93
120
|
inputSchema: Schema.Schema<T, any>;
|
|
94
121
|
outputSchema?: Schema.Schema<O, any>;
|
|
95
122
|
handler: FunctionHandler<T, O>;
|
|
96
123
|
};
|
|
97
124
|
|
|
98
|
-
// TODO(dmaretskyi):
|
|
99
|
-
export const defineFunction
|
|
100
|
-
|
|
125
|
+
// TODO(dmaretskyi): Output type doesn't get typechecked.
|
|
126
|
+
export const defineFunction: {
|
|
127
|
+
<I, O>(params: FunctionProps<I, O>): FunctionDefinition<I, O>;
|
|
128
|
+
} = ({ key, name, description, inputSchema, outputSchema = Schema.Any, handler }) => {
|
|
129
|
+
if (!Schema.isSchema(inputSchema)) {
|
|
101
130
|
throw new Error('Input schema must be a valid schema');
|
|
102
131
|
}
|
|
103
|
-
if (typeof
|
|
132
|
+
if (typeof handler !== 'function') {
|
|
104
133
|
throw new Error('Handler must be a function');
|
|
105
134
|
}
|
|
106
135
|
|
|
136
|
+
// Captures the function definition location.
|
|
137
|
+
const limit = Error.stackTraceLimit;
|
|
138
|
+
Error.stackTraceLimit = 2;
|
|
139
|
+
const traceError = new Error();
|
|
140
|
+
Error.stackTraceLimit = limit;
|
|
141
|
+
let cache: false | string = false;
|
|
142
|
+
const captureStackTrace = () => {
|
|
143
|
+
if (cache !== false) {
|
|
144
|
+
return cache;
|
|
145
|
+
}
|
|
146
|
+
if (traceError.stack !== undefined) {
|
|
147
|
+
const stack = traceError.stack.split('\n');
|
|
148
|
+
if (stack[2] !== undefined) {
|
|
149
|
+
cache = stack[2].trim();
|
|
150
|
+
return cache;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const handlerWithSpan = (...args: any[]) => {
|
|
156
|
+
const result = (handler as any)(...args);
|
|
157
|
+
if (Effect.isEffect(result)) {
|
|
158
|
+
return Effect.withSpan(result, `${key ?? name}`, {
|
|
159
|
+
captureStackTrace,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return result;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
[typeId]: true,
|
|
167
|
+
key,
|
|
168
|
+
name,
|
|
169
|
+
description,
|
|
170
|
+
inputSchema,
|
|
171
|
+
outputSchema,
|
|
172
|
+
handler: handlerWithSpan,
|
|
173
|
+
} satisfies FunctionDefinition.Any;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
export const FunctionDefinition = {
|
|
177
|
+
make: defineFunction,
|
|
178
|
+
isFunction: (value: unknown): value is FunctionDefinition.Any => {
|
|
179
|
+
return typeof value === 'object' && value !== null && Symbol.for('@dxos/functions/FunctionDefinition') in value;
|
|
180
|
+
},
|
|
181
|
+
serialize: (functionDef: FunctionDefinition.Any): Function.Function => {
|
|
182
|
+
assertArgument(FunctionDefinition.isFunction(functionDef), 'functionDef');
|
|
183
|
+
return serializeFunction(functionDef);
|
|
184
|
+
},
|
|
185
|
+
deserialize: (functionObj: Function.Function): FunctionDefinition.Any => {
|
|
186
|
+
assertArgument(Obj.instanceOf(Function.Function, functionObj), 'functionObj');
|
|
187
|
+
return deserializeFunction(functionObj);
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
export declare namespace FunctionDefinition {
|
|
191
|
+
export type Any = FunctionDefinition<any, any>;
|
|
192
|
+
export type Input<T extends FunctionDefinition> = T extends FunctionDefinition<infer I, any> ? I : never;
|
|
193
|
+
export type Output<T extends FunctionDefinition> = T extends FunctionDefinition<any, infer O> ? O : never;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export const serializeFunction = (functionDef: FunctionDefinition<any, any>): Function.Function => {
|
|
197
|
+
const fn = Function.make({
|
|
198
|
+
key: functionDef.key,
|
|
199
|
+
name: functionDef.name,
|
|
200
|
+
version: '0.1.0',
|
|
201
|
+
description: functionDef.description,
|
|
202
|
+
inputSchema: Type.toJsonSchema(functionDef.inputSchema),
|
|
203
|
+
outputSchema: !functionDef.outputSchema ? undefined : Type.toJsonSchema(functionDef.outputSchema),
|
|
204
|
+
});
|
|
205
|
+
if (functionDef.meta?.deployedFunctionId) {
|
|
206
|
+
setUserFunctionIdInMetadata(Obj.getMeta(fn), functionDef.meta.deployedFunctionId);
|
|
207
|
+
}
|
|
208
|
+
return fn;
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
export const deserializeFunction = (functionObj: Function.Function): FunctionDefinition<unknown, unknown> => {
|
|
107
212
|
return {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
213
|
+
[typeId]: true,
|
|
214
|
+
// TODO(dmaretskyi): Fix key.
|
|
215
|
+
key: functionObj.key ?? functionObj.name,
|
|
216
|
+
name: functionObj.name,
|
|
217
|
+
description: functionObj.description,
|
|
218
|
+
inputSchema: !functionObj.inputSchema ? Schema.Unknown : Type.toEffectSchema(functionObj.inputSchema),
|
|
219
|
+
outputSchema: !functionObj.outputSchema ? undefined : Type.toEffectSchema(functionObj.outputSchema),
|
|
220
|
+
// TODO(dmaretskyi): This should throw error.
|
|
221
|
+
handler: () => {},
|
|
222
|
+
meta: {
|
|
223
|
+
deployedFunctionId: getUserFunctionIdInMetadata(Obj.getMeta(functionObj)),
|
|
224
|
+
},
|
|
112
225
|
};
|
|
113
226
|
};
|
package/src/index.ts
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
export * from './errors';
|
|
6
|
+
export * from './example';
|
|
7
|
+
export * from './executor';
|
|
5
8
|
export * from './handler';
|
|
6
|
-
export * from './
|
|
9
|
+
export * from './services';
|
|
7
10
|
export * from './trace';
|
|
11
|
+
export * from './triggers';
|
|
8
12
|
export * from './types';
|
|
9
13
|
export * from './url';
|
|
10
|
-
export * from './services';
|
|
11
|
-
export * from './executor';
|
|
12
|
-
|
|
13
|
-
// Blow up cache
|
|
@@ -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
|
/**
|
|
@@ -31,7 +42,72 @@ export class CredentialsService extends Context.Tag('CredentialsService')<
|
|
|
31
42
|
*/
|
|
32
43
|
getCredential: (query: CredentialQuery) => Promise<ServiceCredential>;
|
|
33
44
|
}
|
|
34
|
-
>() {
|
|
45
|
+
>() {
|
|
46
|
+
static getCredential = (query: CredentialQuery): Effect.Effect<ServiceCredential, never, CredentialsService> =>
|
|
47
|
+
Effect.gen(function* () {
|
|
48
|
+
const credentials = yield* CredentialsService;
|
|
49
|
+
return yield* Effect.promise(() => credentials.getCredential(query));
|
|
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
|
+
);
|
|
110
|
+
}
|
|
35
111
|
|
|
36
112
|
export class ConfiguredCredentialsService implements Context.Tag.Service<CredentialsService> {
|
|
37
113
|
constructor(private readonly credentials: ServiceCredential[] = []) {}
|
|
@@ -50,6 +126,19 @@ export class ConfiguredCredentialsService implements Context.Tag.Service<Credent
|
|
|
50
126
|
if (!credential) {
|
|
51
127
|
throw new Error(`Credential not found for service: ${query.service}`);
|
|
52
128
|
}
|
|
129
|
+
|
|
53
130
|
return credential;
|
|
54
131
|
}
|
|
55
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,131 @@ 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;
|
|
43
85
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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;
|
|
49
95
|
});
|
|
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
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Creates a `QueryResult` object that can be subscribed to.
|
|
140
|
+
*/
|
|
141
|
+
static query: {
|
|
142
|
+
<Q extends Query.Any>(query: Q): Effect.Effect<QueryResult<Live<Query.Type<Q>>>, never, DatabaseService>;
|
|
143
|
+
<F extends Filter.Any>(filter: F): Effect.Effect<QueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;
|
|
144
|
+
} = (queryOrFilter: Query.Any | Filter.Any) =>
|
|
145
|
+
DatabaseService.pipe(
|
|
146
|
+
Effect.map(({ db }) => db.query(queryOrFilter as any)),
|
|
147
|
+
Effect.withSpan('DatabaseService.query'),
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Executes the query once and returns the results.
|
|
152
|
+
*/
|
|
153
|
+
static runQuery: {
|
|
154
|
+
<Q extends Query.Any>(query: Q): Effect.Effect<OneShotQueryResult<Live<Query.Type<Q>>>, never, DatabaseService>;
|
|
155
|
+
<F extends Filter.Any>(filter: F): Effect.Effect<OneShotQueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;
|
|
156
|
+
} = (queryOrFilter: Query.Any | Filter.Any) =>
|
|
157
|
+
DatabaseService.query(queryOrFilter as any).pipe(
|
|
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())),
|
|
174
|
+
);
|
|
50
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,
|