@dxos/functions 0.8.4-main.fd6878d → 0.8.4-main.fffef41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/index.mjs +788 -361
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +788 -361
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/errors.d.ts +87 -28
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/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/index.d.ts +5 -7
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/protocol/index.d.ts +2 -0
- package/dist/types/src/protocol/index.d.ts.map +1 -0
- package/dist/types/src/protocol/protocol.d.ts +7 -0
- package/dist/types/src/protocol/protocol.d.ts.map +1 -0
- package/dist/types/src/protocol/protocol.test.d.ts +2 -0
- package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
- package/dist/types/src/sdk.d.ts +89 -0
- package/dist/types/src/sdk.d.ts.map +1 -0
- package/dist/types/src/services/credentials.d.ts +17 -3
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +20 -5
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +11 -0
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
- package/dist/types/src/services/index.d.ts +6 -6
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +4 -1
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +34 -6
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/types/Function.d.ts +58 -0
- package/dist/types/src/types/Function.d.ts.map +1 -0
- package/dist/types/src/types/Script.d.ts +28 -0
- package/dist/types/src/types/Script.d.ts.map +1 -0
- package/dist/types/src/types/Trigger.d.ts +139 -0
- package/dist/types/src/types/Trigger.d.ts.map +1 -0
- package/dist/types/src/types/TriggerEvent.d.ts +44 -0
- package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +6 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/src/types/url.d.ts +12 -0
- package/dist/types/src/types/url.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +18 -72
- package/src/errors.ts +13 -5
- 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/index.ts +5 -9
- package/src/{executor → protocol}/index.ts +1 -1
- package/src/protocol/protocol.test.ts +59 -0
- package/src/protocol/protocol.ts +145 -0
- package/src/sdk.ts +226 -0
- package/src/services/credentials.ts +51 -6
- package/src/services/event-logger.ts +12 -3
- package/src/services/function-invocation-service.ts +23 -0
- package/src/services/index.ts +7 -6
- package/src/services/queues.ts +8 -1
- package/src/services/tracing.ts +81 -17
- package/src/types/Function.ts +62 -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 +9 -0
- package/src/types/url.ts +31 -0
- package/dist/lib/browser/bundler/index.mjs +0 -248
- package/dist/lib/browser/bundler/index.mjs.map +0 -7
- package/dist/lib/browser/chunk-M2OIFLGE.mjs +0 -486
- package/dist/lib/browser/chunk-M2OIFLGE.mjs.map +0 -7
- package/dist/lib/browser/edge/index.mjs +0 -69
- package/dist/lib/browser/edge/index.mjs.map +0 -7
- package/dist/lib/browser/testing/index.mjs +0 -59
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/bundler/index.mjs +0 -250
- package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-K7REX6H4.mjs +0 -488
- package/dist/lib/node-esm/chunk-K7REX6H4.mjs.map +0 -7
- package/dist/lib/node-esm/edge/index.mjs +0 -71
- package/dist/lib/node-esm/edge/index.mjs.map +0 -7
- package/dist/lib/node-esm/testing/index.mjs +0 -60
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/bundler/bundler.d.ts +0 -50
- package/dist/types/src/bundler/bundler.d.ts.map +0 -1
- package/dist/types/src/bundler/bundler.test.d.ts +0 -2
- package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
- package/dist/types/src/bundler/index.d.ts +0 -2
- package/dist/types/src/bundler/index.d.ts.map +0 -1
- package/dist/types/src/edge/functions.d.ts +0 -16
- package/dist/types/src/edge/functions.d.ts.map +0 -1
- package/dist/types/src/edge/index.d.ts +0 -2
- package/dist/types/src/edge/index.d.ts.map +0 -1
- package/dist/types/src/executor/executor.d.ts +0 -11
- package/dist/types/src/executor/executor.d.ts.map +0 -1
- package/dist/types/src/executor/index.d.ts +0 -2
- package/dist/types/src/executor/index.d.ts.map +0 -1
- package/dist/types/src/handler.d.ts +0 -65
- package/dist/types/src/handler.d.ts.map +0 -1
- package/dist/types/src/schema.d.ts +0 -38
- package/dist/types/src/schema.d.ts.map +0 -1
- package/dist/types/src/services/database.d.ts +0 -79
- package/dist/types/src/services/database.d.ts.map +0 -1
- package/dist/types/src/services/local-function-execution.d.ts +0 -12
- package/dist/types/src/services/local-function-execution.d.ts.map +0 -1
- package/dist/types/src/services/remote-function-execution-service.d.ts +0 -15
- package/dist/types/src/services/remote-function-execution-service.d.ts.map +0 -1
- package/dist/types/src/services/service-container.d.ts +0 -56
- package/dist/types/src/services/service-container.d.ts.map +0 -1
- package/dist/types/src/services/service-registry.d.ts +0 -29
- package/dist/types/src/services/service-registry.d.ts.map +0 -1
- package/dist/types/src/services/service-registry.test.d.ts +0 -2
- package/dist/types/src/services/service-registry.test.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -3
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/layer.d.ts +0 -10
- package/dist/types/src/testing/layer.d.ts.map +0 -1
- package/dist/types/src/testing/logger.d.ts +0 -5
- package/dist/types/src/testing/logger.d.ts.map +0 -1
- package/dist/types/src/testing/services.d.ts +0 -59
- package/dist/types/src/testing/services.d.ts.map +0 -1
- package/dist/types/src/trace.d.ts +0 -122
- package/dist/types/src/trace.d.ts.map +0 -1
- package/dist/types/src/translations.d.ts +0 -12
- package/dist/types/src/translations.d.ts.map +0 -1
- package/dist/types/src/types.d.ts +0 -411
- package/dist/types/src/types.d.ts.map +0 -1
- package/dist/types/src/url.d.ts +0 -17
- package/dist/types/src/url.d.ts.map +0 -1
- package/src/bundler/bundler.test.ts +0 -59
- package/src/bundler/bundler.ts +0 -292
- package/src/bundler/index.ts +0 -5
- package/src/edge/functions.ts +0 -64
- package/src/edge/index.ts +0 -9
- package/src/executor/executor.ts +0 -54
- package/src/handler.ts +0 -122
- package/src/schema.ts +0 -57
- package/src/services/database.ts +0 -116
- package/src/services/local-function-execution.ts +0 -74
- package/src/services/remote-function-execution-service.ts +0 -66
- package/src/services/service-container.ts +0 -114
- package/src/services/service-registry.test.ts +0 -42
- package/src/services/service-registry.ts +0 -59
- package/src/testing/index.ts +0 -6
- package/src/testing/layer.ts +0 -31
- package/src/testing/logger.ts +0 -16
- package/src/testing/services.ts +0 -115
- package/src/trace.ts +0 -178
- package/src/translations.ts +0 -20
- package/src/types.ts +0 -211
- package/src/url.ts +0 -52
package/src/sdk.ts
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
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 { type AiService } from '@dxos/ai';
|
|
10
|
+
import { Obj, Type } from '@dxos/echo';
|
|
11
|
+
import { type DatabaseService } from '@dxos/echo-db';
|
|
12
|
+
import { assertArgument, failedInvariant } from '@dxos/invariant';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
type CredentialsService,
|
|
16
|
+
type FunctionInvocationService,
|
|
17
|
+
type QueueService,
|
|
18
|
+
type TracingService,
|
|
19
|
+
} from './services';
|
|
20
|
+
import { Function } from './types';
|
|
21
|
+
import { getUserFunctionIdInMetadata, setUserFunctionIdInMetadata } from './types';
|
|
22
|
+
|
|
23
|
+
// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
|
|
24
|
+
// https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
|
|
25
|
+
// https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
|
|
26
|
+
// https://www.npmjs.com/package/aws-lambda
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Services that are provided at the function call site by the caller.
|
|
30
|
+
*/
|
|
31
|
+
export type InvocationServices = TracingService;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Services that are available to invoked functions.
|
|
35
|
+
*/
|
|
36
|
+
export type FunctionServices =
|
|
37
|
+
| InvocationServices
|
|
38
|
+
| AiService.AiService
|
|
39
|
+
| CredentialsService
|
|
40
|
+
| DatabaseService
|
|
41
|
+
| QueueService
|
|
42
|
+
| FunctionInvocationService;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Function handler.
|
|
46
|
+
*/
|
|
47
|
+
export type FunctionHandler<TData = {}, TOutput = any, S extends FunctionServices = FunctionServices> = (params: {
|
|
48
|
+
/**
|
|
49
|
+
* Context available to the function.
|
|
50
|
+
*/
|
|
51
|
+
context: FunctionContext;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Data passed as the input to the function.
|
|
55
|
+
* Must match the function's input schema.
|
|
56
|
+
* This will be the payload from the trigger or other data passed into the function in a workflow.
|
|
57
|
+
*/
|
|
58
|
+
data: TData;
|
|
59
|
+
}) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any, S>;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Function context.
|
|
63
|
+
*/
|
|
64
|
+
export interface FunctionContext {
|
|
65
|
+
// TODO(dmaretskyi): Consider what we should put into context.
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const typeId = Symbol.for('@dxos/functions/FunctionDefinition');
|
|
69
|
+
|
|
70
|
+
export type FunctionDefinition<T = any, O = any, S extends FunctionServices = FunctionServices> = {
|
|
71
|
+
[typeId]: true;
|
|
72
|
+
key: string;
|
|
73
|
+
name: string;
|
|
74
|
+
description?: string;
|
|
75
|
+
inputSchema: Schema.Schema<T, any>;
|
|
76
|
+
outputSchema?: Schema.Schema<O, any>;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Keys of the required services.
|
|
80
|
+
*/
|
|
81
|
+
services: readonly string[];
|
|
82
|
+
|
|
83
|
+
handler: FunctionHandler<T, O, S>;
|
|
84
|
+
meta?: {
|
|
85
|
+
/**
|
|
86
|
+
* Tools that are projected from functions have this annotation.
|
|
87
|
+
*
|
|
88
|
+
* deployedFunctionId:
|
|
89
|
+
* - Backend deployment ID assigned by the EDGE function service (typically a UUID).
|
|
90
|
+
* - Used for remote invocation via `FunctionInvocationService` → `RemoteFunctionExecutionService`.
|
|
91
|
+
* - Persisted on the corresponding ECHO `Function.Function` object's metadata under the
|
|
92
|
+
* `FUNCTIONS_META_KEY` and retrieved with `getUserFunctionIdInMetadata`.
|
|
93
|
+
*/
|
|
94
|
+
deployedFunctionId?: string;
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export declare namespace FunctionDefinition {
|
|
99
|
+
export type Any = FunctionDefinition<any, any, any>;
|
|
100
|
+
export type Input<T extends Any> = T extends FunctionDefinition<infer I, infer _O, infer _S> ? I : never;
|
|
101
|
+
export type Output<T extends Any> = T extends FunctionDefinition<infer _I, infer O, infer _S> ? O : never;
|
|
102
|
+
export type Services<T extends Any> = T extends FunctionDefinition<infer _I, infer _O, infer S> ? S : never;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export type FunctionProps<T, O> = {
|
|
106
|
+
key: string;
|
|
107
|
+
name: string;
|
|
108
|
+
description?: string;
|
|
109
|
+
inputSchema: Schema.Schema<T, any>;
|
|
110
|
+
outputSchema?: Schema.Schema<O, any>;
|
|
111
|
+
// TODO(dmaretskyi): This currently doesn't cause a compile-time error if the handler requests a service that is not specified
|
|
112
|
+
services?: readonly Context.Tag<any, any>[];
|
|
113
|
+
|
|
114
|
+
handler: FunctionHandler<T, O, FunctionServices>;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// TODO(dmaretskyi): Output type doesn't get typechecked.
|
|
118
|
+
export const defineFunction: {
|
|
119
|
+
<I, O>(params: FunctionProps<I, O>): FunctionDefinition<I, O, FunctionServices>;
|
|
120
|
+
} = ({ key, name, description, inputSchema, outputSchema = Schema.Any, handler, services }) => {
|
|
121
|
+
if (!Schema.isSchema(inputSchema)) {
|
|
122
|
+
throw new Error('Input schema must be a valid schema');
|
|
123
|
+
}
|
|
124
|
+
if (typeof handler !== 'function') {
|
|
125
|
+
throw new Error('Handler must be a function');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Captures the function definition location.
|
|
129
|
+
const limit = Error.stackTraceLimit;
|
|
130
|
+
Error.stackTraceLimit = 2;
|
|
131
|
+
const traceError = new Error();
|
|
132
|
+
Error.stackTraceLimit = limit;
|
|
133
|
+
let cache: false | string = false;
|
|
134
|
+
const captureStackTrace = () => {
|
|
135
|
+
if (cache !== false) {
|
|
136
|
+
return cache;
|
|
137
|
+
}
|
|
138
|
+
if (traceError.stack !== undefined) {
|
|
139
|
+
const stack = traceError.stack.split('\n');
|
|
140
|
+
if (stack[2] !== undefined) {
|
|
141
|
+
cache = stack[2].trim();
|
|
142
|
+
return cache;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const handlerWithSpan = (...args: any[]) => {
|
|
148
|
+
const result = (handler as any)(...args);
|
|
149
|
+
if (Effect.isEffect(result)) {
|
|
150
|
+
return Effect.withSpan(result, `${key ?? name}`, {
|
|
151
|
+
captureStackTrace,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
return result;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
[typeId]: true,
|
|
159
|
+
key,
|
|
160
|
+
name,
|
|
161
|
+
description,
|
|
162
|
+
inputSchema,
|
|
163
|
+
outputSchema,
|
|
164
|
+
handler: handlerWithSpan,
|
|
165
|
+
services: !services ? [] : getServiceKeys(services),
|
|
166
|
+
} satisfies FunctionDefinition.Any;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const getServiceKeys = (services: readonly Context.Tag<any, any>[]) => {
|
|
170
|
+
return services.map((tag: any) => {
|
|
171
|
+
if (typeof tag.key === 'string') {
|
|
172
|
+
return tag.key;
|
|
173
|
+
}
|
|
174
|
+
console.log(tag);
|
|
175
|
+
failedInvariant();
|
|
176
|
+
});
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export const FunctionDefinition = {
|
|
180
|
+
make: defineFunction,
|
|
181
|
+
isFunction: (value: unknown): value is FunctionDefinition.Any => {
|
|
182
|
+
return typeof value === 'object' && value !== null && Symbol.for('@dxos/functions/FunctionDefinition') in value;
|
|
183
|
+
},
|
|
184
|
+
serialize: (functionDef: FunctionDefinition.Any): Function.Function => {
|
|
185
|
+
assertArgument(FunctionDefinition.isFunction(functionDef), 'functionDef');
|
|
186
|
+
return serializeFunction(functionDef);
|
|
187
|
+
},
|
|
188
|
+
deserialize: (functionObj: Function.Function): FunctionDefinition.Any => {
|
|
189
|
+
assertArgument(Obj.instanceOf(Function.Function, functionObj), 'functionObj');
|
|
190
|
+
return deserializeFunction(functionObj);
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
export const serializeFunction = (functionDef: FunctionDefinition.Any): Function.Function => {
|
|
195
|
+
const fn = Function.make({
|
|
196
|
+
key: functionDef.key,
|
|
197
|
+
name: functionDef.name,
|
|
198
|
+
version: '0.1.0',
|
|
199
|
+
description: functionDef.description,
|
|
200
|
+
inputSchema: Type.toJsonSchema(functionDef.inputSchema),
|
|
201
|
+
outputSchema: !functionDef.outputSchema ? undefined : Type.toJsonSchema(functionDef.outputSchema),
|
|
202
|
+
services: functionDef.services,
|
|
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: Function.Function): 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
|
+
services: functionObj.services ?? [],
|
|
222
|
+
meta: {
|
|
223
|
+
deployedFunctionId: getUserFunctionIdInMetadata(Obj.getMeta(functionObj)),
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
};
|
|
@@ -2,14 +2,19 @@
|
|
|
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';
|
|
8
|
-
import {
|
|
14
|
+
import { DatabaseService } from '@dxos/echo-db';
|
|
15
|
+
import { AccessToken } from '@dxos/types';
|
|
9
16
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
type CredentialQuery = {
|
|
17
|
+
export type CredentialQuery = {
|
|
13
18
|
service?: string;
|
|
14
19
|
};
|
|
15
20
|
|
|
@@ -43,16 +48,42 @@ export class CredentialsService extends Context.Tag('@dxos/functions/Credentials
|
|
|
43
48
|
return yield* Effect.promise(() => credentials.getCredential(query));
|
|
44
49
|
});
|
|
45
50
|
|
|
51
|
+
static getApiKey = (query: CredentialQuery): Effect.Effect<Redacted.Redacted<string>, never, CredentialsService> =>
|
|
52
|
+
Effect.gen(function* () {
|
|
53
|
+
const credential = yield* CredentialsService.getCredential(query);
|
|
54
|
+
if (!credential.apiKey) {
|
|
55
|
+
throw new Error(`API key not found for service: ${query.service}`);
|
|
56
|
+
}
|
|
57
|
+
return Redacted.make(credential.apiKey);
|
|
58
|
+
});
|
|
59
|
+
|
|
46
60
|
static configuredLayer = (credentials: ServiceCredential[]) =>
|
|
47
61
|
Layer.succeed(CredentialsService, new ConfiguredCredentialsService(credentials));
|
|
48
62
|
|
|
63
|
+
static layerConfig = (credentials: { service: string; apiKey: Config.Config<Redacted.Redacted<string>> }[]) =>
|
|
64
|
+
Layer.effect(
|
|
65
|
+
CredentialsService,
|
|
66
|
+
Effect.gen(function* () {
|
|
67
|
+
const serviceCredentials = yield* Effect.forEach(credentials, ({ service, apiKey }) =>
|
|
68
|
+
Effect.gen(function* () {
|
|
69
|
+
return {
|
|
70
|
+
service,
|
|
71
|
+
apiKey: Redacted.value(yield* apiKey),
|
|
72
|
+
};
|
|
73
|
+
}),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
return new ConfiguredCredentialsService(serviceCredentials);
|
|
77
|
+
}),
|
|
78
|
+
);
|
|
79
|
+
|
|
49
80
|
static layerFromDatabase = () =>
|
|
50
81
|
Layer.effect(
|
|
51
82
|
CredentialsService,
|
|
52
83
|
Effect.gen(function* () {
|
|
53
84
|
const dbService = yield* DatabaseService;
|
|
54
85
|
const queryCredentials = async (query: CredentialQuery): Promise<ServiceCredential[]> => {
|
|
55
|
-
const { objects: accessTokens } = await dbService.db.query(Query.type(
|
|
86
|
+
const { objects: accessTokens } = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
|
|
56
87
|
return accessTokens
|
|
57
88
|
.filter((accessToken) => accessToken.source === query.service)
|
|
58
89
|
.map((accessToken) => ({
|
|
@@ -66,6 +97,7 @@ export class CredentialsService extends Context.Tag('@dxos/functions/Credentials
|
|
|
66
97
|
if (credentials.length === 0) {
|
|
67
98
|
throw new Error(`Credential not found for service: ${query.service}`);
|
|
68
99
|
}
|
|
100
|
+
|
|
69
101
|
return credentials[0];
|
|
70
102
|
},
|
|
71
103
|
queryCredentials: async (query) => {
|
|
@@ -93,6 +125,19 @@ export class ConfiguredCredentialsService implements Context.Tag.Service<Credent
|
|
|
93
125
|
if (!credential) {
|
|
94
126
|
throw new Error(`Credential not found for service: ${query.service}`);
|
|
95
127
|
}
|
|
128
|
+
|
|
96
129
|
return credential;
|
|
97
130
|
}
|
|
98
131
|
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Maps the request to include the API key from the credential.
|
|
135
|
+
*/
|
|
136
|
+
export const withAuthorization = (query: CredentialQuery, kind?: 'Bearer' | 'Basic') =>
|
|
137
|
+
HttpClient.mapRequestEffect(
|
|
138
|
+
Effect.fnUntraced(function* (request) {
|
|
139
|
+
const key = yield* CredentialsService.getApiKey(query).pipe(Effect.map(Redacted.value));
|
|
140
|
+
const authorization = kind ? `${kind} ${key}` : key;
|
|
141
|
+
return HttpClientRequest.setHeader(request, 'Authorization', authorization);
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
@@ -2,7 +2,10 @@
|
|
|
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';
|
|
@@ -14,12 +17,18 @@ export const ComputeEventPayload = Schema.Union(
|
|
|
14
17
|
Schema.Struct({
|
|
15
18
|
type: Schema.Literal('begin-compute'),
|
|
16
19
|
nodeId: Schema.String,
|
|
17
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Names of the inputs begin computed.
|
|
22
|
+
*/
|
|
23
|
+
inputs: Schema.Array(Schema.String),
|
|
18
24
|
}),
|
|
19
25
|
Schema.Struct({
|
|
20
26
|
type: Schema.Literal('end-compute'),
|
|
21
27
|
nodeId: Schema.String,
|
|
22
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Names of the outputs computed.
|
|
30
|
+
*/
|
|
31
|
+
outputs: Schema.Array(Schema.String),
|
|
23
32
|
}),
|
|
24
33
|
Schema.Struct({
|
|
25
34
|
type: Schema.Literal('compute-input'),
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
import * as Context from 'effect/Context';
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
|
|
7
|
+
import { type FunctionDefinition, type InvocationServices } from '../sdk';
|
|
8
|
+
|
|
9
|
+
export class FunctionInvocationService extends Context.Tag('@dxos/functions/FunctionInvocationService')<
|
|
10
|
+
FunctionInvocationService,
|
|
11
|
+
{
|
|
12
|
+
invokeFunction<I, O>(
|
|
13
|
+
functionDef: FunctionDefinition<I, O, any>,
|
|
14
|
+
input: I,
|
|
15
|
+
): Effect.Effect<O, never, InvocationServices>;
|
|
16
|
+
}
|
|
17
|
+
>() {
|
|
18
|
+
static invokeFunction = <I, O>(
|
|
19
|
+
functionDef: FunctionDefinition<I, O, any>,
|
|
20
|
+
input: I,
|
|
21
|
+
): Effect.Effect<O, never, FunctionInvocationService | InvocationServices> =>
|
|
22
|
+
Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.invokeFunction)(functionDef, input);
|
|
23
|
+
}
|
package/src/services/index.ts
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
export * from './service-container';
|
|
5
|
+
export { DatabaseService } from '@dxos/echo-db';
|
|
6
|
+
|
|
8
7
|
export * from './credentials';
|
|
9
|
-
export
|
|
8
|
+
export { ConfiguredCredentialsService, type ServiceCredential } from './credentials';
|
|
10
9
|
export * from './event-logger';
|
|
11
|
-
export
|
|
12
|
-
export * from './
|
|
10
|
+
export { createEventLogger, createDefectLogger } from './event-logger';
|
|
11
|
+
export * from './function-invocation-service';
|
|
12
|
+
export * from './queues';
|
|
13
|
+
export * from './tracing';
|
package/src/services/queues.ts
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
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';
|
|
6
8
|
|
|
7
9
|
import type { Obj, Relation } from '@dxos/echo';
|
|
8
10
|
import type { Queue, QueueAPI, QueueFactory } from '@dxos/echo-db';
|
|
@@ -62,6 +64,11 @@ export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
|
|
|
62
64
|
subspaceTag?: QueueSubspaceTag;
|
|
63
65
|
}): Effect.Effect<Queue<T>, never, QueueService> =>
|
|
64
66
|
QueueService.pipe(Effect.map(({ queues }) => queues.create<T>(options)));
|
|
67
|
+
|
|
68
|
+
static append = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(
|
|
69
|
+
queue: Queue<T>,
|
|
70
|
+
objects: T[],
|
|
71
|
+
): Effect.Effect<void> => Effect.promise(() => queue.append(objects));
|
|
65
72
|
}
|
|
66
73
|
|
|
67
74
|
/**
|
package/src/services/tracing.ts
CHANGED
|
@@ -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';
|
|
6
8
|
|
|
7
9
|
import { AgentStatus } from '@dxos/ai';
|
|
8
10
|
import { Obj } from '@dxos/echo';
|
|
9
|
-
import type {
|
|
11
|
+
import type { ObjectId } from '@dxos/echo/internal';
|
|
12
|
+
import { Message } from '@dxos/types';
|
|
10
13
|
|
|
11
14
|
/**
|
|
12
15
|
* Provides a way for compute primitives (functions, workflows, tools)
|
|
@@ -15,30 +18,91 @@ import type { AnyEchoObject } from '@dxos/echo-schema';
|
|
|
15
18
|
export class TracingService extends Context.Tag('@dxos/functions/TracingService')<
|
|
16
19
|
TracingService,
|
|
17
20
|
{
|
|
21
|
+
/**
|
|
22
|
+
* Gets the parent message ID.
|
|
23
|
+
*/
|
|
24
|
+
getTraceContext: () => TracingService.TraceContext;
|
|
25
|
+
|
|
18
26
|
/**
|
|
19
27
|
* Write an event to the tracing queue.
|
|
20
28
|
* @param event - The event to write. Must be an a typed object.
|
|
21
29
|
*/
|
|
22
|
-
write(event:
|
|
30
|
+
write: (event: Obj.Any) => void;
|
|
23
31
|
}
|
|
24
32
|
>() {
|
|
25
|
-
static noop: Context.Tag.Service<TracingService> = {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
static console: Context.Tag.Service<TracingService> = {
|
|
30
|
-
write: (event) => {
|
|
31
|
-
// eslint-disable-next-line no-console
|
|
32
|
-
console.log(event);
|
|
33
|
-
},
|
|
33
|
+
static noop: Context.Tag.Service<TracingService> = {
|
|
34
|
+
getTraceContext: () => ({}),
|
|
35
|
+
write: () => {},
|
|
34
36
|
};
|
|
35
37
|
|
|
38
|
+
static layerNoop: Layer.Layer<TracingService> = Layer.succeed(TracingService, TracingService.noop);
|
|
39
|
+
/**
|
|
40
|
+
* Creates a TracingService layer that emits events to the parent tracing service.
|
|
41
|
+
*/
|
|
42
|
+
static layerSubframe = (mapContext: (currentContext: TracingService.TraceContext) => TracingService.TraceContext) =>
|
|
43
|
+
Layer.effect(
|
|
44
|
+
TracingService,
|
|
45
|
+
Effect.gen(function* () {
|
|
46
|
+
const tracing = yield* TracingService;
|
|
47
|
+
const context = mapContext(tracing.getTraceContext());
|
|
48
|
+
return {
|
|
49
|
+
write: (event) => tracing.write(event),
|
|
50
|
+
getTraceContext: () => context,
|
|
51
|
+
};
|
|
52
|
+
}),
|
|
53
|
+
);
|
|
54
|
+
|
|
36
55
|
/**
|
|
37
56
|
* Emit the current human-readable execution status.
|
|
38
57
|
*/
|
|
39
|
-
static emitStatus: (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
58
|
+
static emitStatus: (
|
|
59
|
+
data: Omit<Obj.MakeProps<typeof AgentStatus>, 'created'>,
|
|
60
|
+
) => Effect.Effect<void, never, TracingService> = Effect.fnUntraced(function* (data) {
|
|
61
|
+
const tracing = yield* TracingService;
|
|
62
|
+
tracing.write(
|
|
63
|
+
Obj.make(AgentStatus, {
|
|
64
|
+
parentMessage: tracing.getTraceContext().parentMessage,
|
|
65
|
+
toolCallId: tracing.getTraceContext().toolCallId,
|
|
66
|
+
created: new Date().toISOString(),
|
|
67
|
+
...data,
|
|
68
|
+
}),
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
static emitConverationMessage: (
|
|
73
|
+
data: Obj.MakeProps<typeof Message.Message>,
|
|
74
|
+
) => Effect.Effect<void, never, TracingService> = Effect.fnUntraced(function* (data) {
|
|
75
|
+
const tracing = yield* TracingService;
|
|
76
|
+
tracing.write(
|
|
77
|
+
Obj.make(Message.Message, {
|
|
78
|
+
parentMessage: tracing.getTraceContext().parentMessage,
|
|
79
|
+
...data,
|
|
80
|
+
properties: {
|
|
81
|
+
[MESSAGE_PROPERTY_TOOL_CALL_ID]: tracing.getTraceContext().toolCallId,
|
|
82
|
+
...data.properties,
|
|
83
|
+
},
|
|
84
|
+
}),
|
|
85
|
+
);
|
|
86
|
+
});
|
|
44
87
|
}
|
|
88
|
+
|
|
89
|
+
export namespace TracingService {
|
|
90
|
+
export interface TraceContext {
|
|
91
|
+
/**
|
|
92
|
+
* If this thread sprung from a tool call, this is the ID of the message containing the tool call.
|
|
93
|
+
*/
|
|
94
|
+
parentMessage?: ObjectId;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* If the current thread is a byproduct of a tool call, this is the ID of the tool call.
|
|
98
|
+
*/
|
|
99
|
+
toolCallId?: string;
|
|
100
|
+
|
|
101
|
+
debugInfo?: unknown;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Goes into {@link Message['properties']}
|
|
107
|
+
*/
|
|
108
|
+
export const MESSAGE_PROPERTY_TOOL_CALL_ID = 'toolCallId' as const;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
|
|
7
|
+
import { Obj, Type } from '@dxos/echo';
|
|
8
|
+
import { JsonSchemaType, LabelAnnotation, Ref } from '@dxos/echo/internal';
|
|
9
|
+
|
|
10
|
+
import { Script } from './Script';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Function deployment.
|
|
14
|
+
*/
|
|
15
|
+
export const Function = Schema.Struct({
|
|
16
|
+
/**
|
|
17
|
+
* Global registry ID.
|
|
18
|
+
* NOTE: The `key` property refers to the original registry entry.
|
|
19
|
+
*/
|
|
20
|
+
// TODO(burdon): Create Format type for DXN-like ids, such as this and schema type.
|
|
21
|
+
// TODO(dmaretskyi): Consider making it part of ECHO meta.
|
|
22
|
+
// TODO(dmaretskyi): Make required.
|
|
23
|
+
key: Schema.optional(Schema.String).annotations({
|
|
24
|
+
description: 'Unique registration key for the blueprint',
|
|
25
|
+
}),
|
|
26
|
+
|
|
27
|
+
// TODO(burdon): Rename to id/uri?
|
|
28
|
+
name: Schema.NonEmptyString,
|
|
29
|
+
version: Schema.String,
|
|
30
|
+
|
|
31
|
+
description: Schema.optional(Schema.String),
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* ISO date string of the last deployment.
|
|
35
|
+
*/
|
|
36
|
+
updated: Schema.optional(Schema.String),
|
|
37
|
+
|
|
38
|
+
// Reference to a source script if it exists within ECHO.
|
|
39
|
+
// TODO(burdon): Don't ref ScriptType directly (core).
|
|
40
|
+
source: Schema.optional(Ref(Script)),
|
|
41
|
+
|
|
42
|
+
inputSchema: Schema.optional(JsonSchemaType),
|
|
43
|
+
outputSchema: Schema.optional(JsonSchemaType),
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* List of required services.
|
|
47
|
+
* Match the Context.Tag keys of the FunctionServices variants.
|
|
48
|
+
*/
|
|
49
|
+
services: Schema.optional(Schema.Array(Schema.String)),
|
|
50
|
+
|
|
51
|
+
// Local binding to a function name.
|
|
52
|
+
binding: Schema.optional(Schema.String),
|
|
53
|
+
}).pipe(
|
|
54
|
+
Type.Obj({
|
|
55
|
+
typename: 'dxos.org/type/Function',
|
|
56
|
+
version: '0.1.0',
|
|
57
|
+
}),
|
|
58
|
+
LabelAnnotation.set(['name']),
|
|
59
|
+
);
|
|
60
|
+
export interface Function extends Schema.Schema.Type<typeof Function> {}
|
|
61
|
+
|
|
62
|
+
export const make = (props: Obj.MakeProps<typeof Function>) => Obj.make(Function, props);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
|
|
7
|
+
import { Obj, Ref, Type } from '@dxos/echo';
|
|
8
|
+
import { FormAnnotation, LabelAnnotation } from '@dxos/echo/internal';
|
|
9
|
+
import { Text } from '@dxos/schema';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Source script.
|
|
13
|
+
*/
|
|
14
|
+
export const Script = Schema.Struct({
|
|
15
|
+
name: Schema.String.pipe(Schema.optional),
|
|
16
|
+
description: Schema.String.pipe(Schema.optional),
|
|
17
|
+
// TODO(burdon): Change to hash of deployed content.
|
|
18
|
+
// Whether source has changed since last deploy.
|
|
19
|
+
changed: Schema.Boolean.pipe(FormAnnotation.set(false), Schema.optional),
|
|
20
|
+
source: Type.Ref(Text.Text).pipe(FormAnnotation.set(false)),
|
|
21
|
+
}).pipe(
|
|
22
|
+
Type.Obj({
|
|
23
|
+
typename: 'dxos.org/type/Script',
|
|
24
|
+
version: '0.1.0',
|
|
25
|
+
}),
|
|
26
|
+
LabelAnnotation.set(['name']),
|
|
27
|
+
);
|
|
28
|
+
export interface Script extends Schema.Schema.Type<typeof Script> {}
|
|
29
|
+
|
|
30
|
+
type Props = Omit<Obj.MakeProps<typeof Script>, 'source'> & { source?: string };
|
|
31
|
+
|
|
32
|
+
export const make = ({ source = '', ...props }: Props = {}) =>
|
|
33
|
+
Obj.make(Script, { ...props, source: Ref.make(Text.make(source)) });
|