@dxos/functions 0.8.4-main.67995b8 → 0.8.4-main.72ec0f3
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 +789 -363
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +789 -363
- 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 +89 -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 +19 -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 +21 -5
- 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 +20 -74
- 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 +84 -5
- package/src/services/event-logger.ts +13 -4
- package/src/services/function-invocation-service.ts +23 -0
- package/src/services/index.ts +7 -6
- package/src/services/queues.ts +36 -10
- 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 -247
- package/dist/lib/browser/bundler/index.mjs.map +0 -7
- package/dist/lib/browser/chunk-6PTFLPCO.mjs +0 -462
- package/dist/lib/browser/chunk-6PTFLPCO.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 -249
- package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-NYJ2TSXO.mjs +0 -464
- package/dist/lib/node-esm/chunk-NYJ2TSXO.mjs.map +0 -7
- package/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 -8
- 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 -62
- 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 -30
- package/dist/types/src/services/database.d.ts.map +0 -1
- package/dist/types/src/services/local-function-execution.d.ts +0 -11
- 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 -124
- 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 -120
- package/src/schema.ts +0 -57
- package/src/services/database.ts +0 -74
- package/src/services/local-function-execution.ts +0 -70
- package/src/services/remote-function-execution-service.ts +0 -66
- package/src/services/service-container.ts +0 -113
- 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 -114
- package/src/trace.ts +0 -180
- 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,9 +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';
|
|
14
|
+
import { DatabaseService } from '@dxos/echo-db';
|
|
15
|
+
import { AccessToken } from '@dxos/types';
|
|
16
|
+
|
|
17
|
+
export type CredentialQuery = {
|
|
8
18
|
service?: string;
|
|
9
19
|
};
|
|
10
20
|
|
|
@@ -32,14 +42,70 @@ export class CredentialsService extends Context.Tag('@dxos/functions/Credentials
|
|
|
32
42
|
getCredential: (query: CredentialQuery) => Promise<ServiceCredential>;
|
|
33
43
|
}
|
|
34
44
|
>() {
|
|
35
|
-
static configuredLayer = (credentials: ServiceCredential[]) =>
|
|
36
|
-
Layer.succeed(CredentialsService, new ConfiguredCredentialsService(credentials));
|
|
37
|
-
|
|
38
45
|
static getCredential = (query: CredentialQuery): Effect.Effect<ServiceCredential, never, CredentialsService> =>
|
|
39
46
|
Effect.gen(function* () {
|
|
40
47
|
const credentials = yield* CredentialsService;
|
|
41
48
|
return yield* Effect.promise(() => credentials.getCredential(query));
|
|
42
49
|
});
|
|
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
|
+
|
|
60
|
+
static configuredLayer = (credentials: ServiceCredential[]) =>
|
|
61
|
+
Layer.succeed(CredentialsService, new ConfiguredCredentialsService(credentials));
|
|
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
|
+
|
|
80
|
+
static layerFromDatabase = () =>
|
|
81
|
+
Layer.effect(
|
|
82
|
+
CredentialsService,
|
|
83
|
+
Effect.gen(function* () {
|
|
84
|
+
const dbService = yield* DatabaseService;
|
|
85
|
+
const queryCredentials = async (query: CredentialQuery): Promise<ServiceCredential[]> => {
|
|
86
|
+
const { objects: accessTokens } = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
|
|
87
|
+
return accessTokens
|
|
88
|
+
.filter((accessToken) => accessToken.source === query.service)
|
|
89
|
+
.map((accessToken) => ({
|
|
90
|
+
service: accessToken.source,
|
|
91
|
+
apiKey: accessToken.token,
|
|
92
|
+
}));
|
|
93
|
+
};
|
|
94
|
+
return {
|
|
95
|
+
getCredential: async (query) => {
|
|
96
|
+
const credentials = await queryCredentials(query);
|
|
97
|
+
if (credentials.length === 0) {
|
|
98
|
+
throw new Error(`Credential not found for service: ${query.service}`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return credentials[0];
|
|
102
|
+
},
|
|
103
|
+
queryCredentials: async (query) => {
|
|
104
|
+
return queryCredentials(query);
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}),
|
|
108
|
+
);
|
|
43
109
|
}
|
|
44
110
|
|
|
45
111
|
export class ConfiguredCredentialsService implements Context.Tag.Service<CredentialsService> {
|
|
@@ -59,6 +125,19 @@ export class ConfiguredCredentialsService implements Context.Tag.Service<Credent
|
|
|
59
125
|
if (!credential) {
|
|
60
126
|
throw new Error(`Credential not found for service: ${query.service}`);
|
|
61
127
|
}
|
|
128
|
+
|
|
62
129
|
return credential;
|
|
63
130
|
}
|
|
64
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,11 +2,14 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Context from 'effect/Context';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Layer from 'effect/Layer';
|
|
8
|
+
import * as Schema from 'effect/Schema';
|
|
6
9
|
|
|
7
10
|
import { Obj, Type } from '@dxos/echo';
|
|
8
11
|
import { invariant } from '@dxos/invariant';
|
|
9
|
-
import {
|
|
12
|
+
import { LogLevel, log } from '@dxos/log';
|
|
10
13
|
|
|
11
14
|
import { TracingService } from './tracing';
|
|
12
15
|
|
|
@@ -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,9 +2,13 @@
|
|
|
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
|
|
|
9
|
+
import type { Obj, Relation } from '@dxos/echo';
|
|
7
10
|
import type { Queue, QueueAPI, QueueFactory } from '@dxos/echo-db';
|
|
11
|
+
import type { DXN, QueueSubspaceTag } from '@dxos/keys';
|
|
8
12
|
|
|
9
13
|
/**
|
|
10
14
|
* Gives access to all queues.
|
|
@@ -21,30 +25,50 @@ export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
|
|
|
21
25
|
* The queue that is used to store the context of the current research.
|
|
22
26
|
* @deprecated Use `ContextQueueService` instead.
|
|
23
27
|
*/
|
|
24
|
-
readonly
|
|
28
|
+
readonly queue: Queue | undefined;
|
|
25
29
|
}
|
|
26
30
|
>() {
|
|
27
31
|
static notAvailable = Layer.succeed(QueueService, {
|
|
28
32
|
queues: {
|
|
29
|
-
get(
|
|
33
|
+
get(_dxn) {
|
|
30
34
|
throw new Error('Queues not available');
|
|
31
35
|
},
|
|
32
36
|
create() {
|
|
33
37
|
throw new Error('Queues not available');
|
|
34
38
|
},
|
|
35
39
|
},
|
|
36
|
-
|
|
40
|
+
queue: undefined,
|
|
37
41
|
});
|
|
38
42
|
|
|
39
|
-
static make = (queues: QueueFactory,
|
|
43
|
+
static make = (queues: QueueFactory, queue?: Queue): Context.Tag.Service<QueueService> => {
|
|
40
44
|
return {
|
|
41
45
|
queues,
|
|
42
|
-
|
|
46
|
+
queue,
|
|
43
47
|
};
|
|
44
48
|
};
|
|
45
49
|
|
|
46
|
-
static
|
|
47
|
-
Layer.succeed(QueueService, QueueService.make(queues,
|
|
50
|
+
static layer = (queues: QueueFactory, queue?: Queue): Layer.Layer<QueueService> =>
|
|
51
|
+
Layer.succeed(QueueService, QueueService.make(queues, queue));
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Gets a queue by its DXN.
|
|
55
|
+
*/
|
|
56
|
+
static getQueue = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(
|
|
57
|
+
dxn: DXN,
|
|
58
|
+
): Effect.Effect<Queue<T>, never, QueueService> => QueueService.pipe(Effect.map(({ queues }) => queues.get<T>(dxn)));
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Creates a new queue.
|
|
62
|
+
*/
|
|
63
|
+
static createQueue = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(options?: {
|
|
64
|
+
subspaceTag?: QueueSubspaceTag;
|
|
65
|
+
}): Effect.Effect<Queue<T>, never, QueueService> =>
|
|
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));
|
|
48
72
|
}
|
|
49
73
|
|
|
50
74
|
/**
|
|
@@ -53,6 +77,8 @@ export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
|
|
|
53
77
|
export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQueueService')<
|
|
54
78
|
ContextQueueService,
|
|
55
79
|
{
|
|
56
|
-
readonly
|
|
80
|
+
readonly queue: Queue;
|
|
57
81
|
}
|
|
58
|
-
>() {
|
|
82
|
+
>() {
|
|
83
|
+
static layer = (queue: Queue) => Layer.succeed(ContextQueueService, { queue });
|
|
84
|
+
}
|
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);
|