@dxos/functions 0.8.4-main.c85a9c8dae → 0.8.4-main.d05539e30a
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/LICENSE +102 -5
- package/README.md +1 -1
- package/dist/lib/neutral/index.mjs +294 -932
- package/dist/lib/neutral/index.mjs.map +4 -4
- package/dist/lib/neutral/meta.json +1 -1
- package/dist/types/src/index.d.ts +0 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -1
- package/dist/types/src/protocol/functions-ai-http-client.test.d.ts +2 -0
- package/dist/types/src/protocol/functions-ai-http-client.test.d.ts.map +1 -0
- package/dist/types/src/protocol/protocol.d.ts +19 -2
- package/dist/types/src/protocol/protocol.d.ts.map +1 -1
- package/dist/types/src/sdk.d.ts +5 -112
- package/dist/types/src/sdk.d.ts.map +1 -1
- package/dist/types/src/services/credentials.d.ts +15 -38
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +8 -6
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
- package/dist/types/src/services/index.d.ts +1 -4
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +1 -46
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +1 -84
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/types/index.d.ts +0 -4
- package/dist/types/src/types/index.d.ts.map +1 -1
- package/dist/types/src/types/url.d.ts +3 -3
- package/dist/types/src/types/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +18 -17
- package/src/index.ts +0 -2
- package/src/protocol/functions-ai-http-client.test.ts +105 -0
- package/src/protocol/functions-ai-http-client.ts +75 -1
- package/src/protocol/protocol.test.ts +9 -10
- package/src/protocol/protocol.ts +255 -58
- package/src/sdk.ts +12 -278
- package/src/services/credentials.ts +76 -120
- package/src/services/function-invocation-service.ts +9 -10
- package/src/services/index.ts +1 -4
- package/src/services/queues.ts +1 -78
- package/src/services/tracing.ts +0 -159
- package/src/types/index.ts +0 -4
- package/src/types/url.ts +3 -3
- package/dist/types/src/errors.d.ts +0 -121
- package/dist/types/src/errors.d.ts.map +0 -1
- package/dist/types/src/example/fib.d.ts +0 -7
- package/dist/types/src/example/fib.d.ts.map +0 -1
- package/dist/types/src/example/forex-effect.d.ts +0 -3
- package/dist/types/src/example/forex-effect.d.ts.map +0 -1
- package/dist/types/src/example/index.d.ts +0 -12
- package/dist/types/src/example/index.d.ts.map +0 -1
- package/dist/types/src/example/reply.d.ts +0 -3
- package/dist/types/src/example/reply.d.ts.map +0 -1
- package/dist/types/src/example/sleep.d.ts +0 -5
- package/dist/types/src/example/sleep.d.ts.map +0 -1
- package/dist/types/src/operation-compatibility.test.d.ts +0 -2
- package/dist/types/src/operation-compatibility.test.d.ts.map +0 -1
- package/dist/types/src/services/event-logger.d.ts +0 -81
- package/dist/types/src/services/event-logger.d.ts.map +0 -1
- package/dist/types/src/types/Function.d.ts +0 -52
- package/dist/types/src/types/Function.d.ts.map +0 -1
- package/dist/types/src/types/Script.d.ts +0 -21
- package/dist/types/src/types/Script.d.ts.map +0 -1
- package/dist/types/src/types/Trigger.d.ts +0 -122
- package/dist/types/src/types/Trigger.d.ts.map +0 -1
- package/dist/types/src/types/TriggerEvent.d.ts +0 -75
- package/dist/types/src/types/TriggerEvent.d.ts.map +0 -1
- package/src/errors.ts +0 -21
- package/src/example/fib.ts +0 -32
- package/src/example/forex-effect.ts +0 -40
- package/src/example/index.ts +0 -13
- package/src/example/reply.ts +0 -21
- package/src/example/sleep.ts +0 -24
- package/src/operation-compatibility.test.ts +0 -185
- package/src/services/event-logger.ts +0 -127
- package/src/types/Function.ts +0 -83
- package/src/types/Script.ts +0 -34
- package/src/types/Trigger.ts +0 -153
- package/src/types/TriggerEvent.ts +0 -62
package/src/sdk.ts
CHANGED
|
@@ -2,296 +2,30 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
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
5
|
import { type AiService } from '@dxos/ai';
|
|
10
|
-
import { type
|
|
11
|
-
import { type Database } from '@dxos/echo';
|
|
12
|
-
import { assertArgument, failedInvariant } from '@dxos/invariant';
|
|
13
|
-
import { Operation } from '@dxos/operation';
|
|
6
|
+
import { type Credential, type Operation, type Trace } from '@dxos/compute';
|
|
7
|
+
import { type Database, type Feed } from '@dxos/echo';
|
|
14
8
|
|
|
15
|
-
import {
|
|
16
|
-
type CredentialsService,
|
|
17
|
-
type FunctionInvocationService,
|
|
18
|
-
type QueueService,
|
|
19
|
-
type TracingService,
|
|
20
|
-
} from './services';
|
|
21
|
-
import { Function } from './types';
|
|
22
|
-
import { getUserFunctionIdInMetadata, setUserFunctionIdInMetadata } from './types';
|
|
9
|
+
import { type FunctionInvocationService, type QueueService } from './services';
|
|
23
10
|
|
|
24
11
|
// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
|
|
25
12
|
// https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
|
|
26
13
|
// https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
|
|
27
14
|
// https://www.npmjs.com/package/aws-lambda
|
|
28
15
|
|
|
29
|
-
/**
|
|
30
|
-
* Services that are provided at the function call site by the caller.
|
|
31
|
-
*/
|
|
32
|
-
export type InvocationServices = TracingService;
|
|
33
|
-
|
|
34
16
|
/**
|
|
35
17
|
* Services that are available to invoked functions.
|
|
18
|
+
* @deprecated
|
|
36
19
|
*/
|
|
37
20
|
export type FunctionServices =
|
|
38
|
-
| InvocationServices
|
|
39
21
|
| AiService.AiService
|
|
40
|
-
| CredentialsService
|
|
22
|
+
| Credential.CredentialsService
|
|
41
23
|
| Database.Service
|
|
42
|
-
// TODO(wittjosiah): Remove QueueService — use Feed.
|
|
24
|
+
// TODO(wittjosiah): Remove QueueService — use Feed.FeedService instead.
|
|
43
25
|
| QueueService
|
|
44
|
-
| Feed.
|
|
45
|
-
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
export type FunctionHandler<TData = {}, TOutput = any, S extends FunctionServices = FunctionServices> = (params: {
|
|
51
|
-
/**
|
|
52
|
-
* Context available to the function.
|
|
53
|
-
*/
|
|
54
|
-
context: FunctionContext;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Data passed as the input to the function.
|
|
58
|
-
* Must match the function's input schema.
|
|
59
|
-
* This will be the payload from the trigger or other data passed into the function in a workflow.
|
|
60
|
-
*/
|
|
61
|
-
data: TData;
|
|
62
|
-
}) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any, S>;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Function context.
|
|
66
|
-
*/
|
|
67
|
-
export interface FunctionContext {
|
|
68
|
-
// TODO(dmaretskyi): Consider what we should put into context.
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const typeId = Symbol.for('@dxos/functions/FunctionDefinition');
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Deployable function definition.
|
|
75
|
-
*/
|
|
76
|
-
export type FunctionDefinition<TInput = any, TOutput = any, S extends FunctionServices = FunctionServices> = {
|
|
77
|
-
[typeId]: true;
|
|
78
|
-
|
|
79
|
-
key: string;
|
|
80
|
-
name: string;
|
|
81
|
-
description?: string;
|
|
82
|
-
inputSchema: Schema.Schema<TInput, any>;
|
|
83
|
-
outputSchema?: Schema.Schema<TOutput, any>;
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* List of types the function uses.
|
|
87
|
-
* This is used to ensure that the types are available when the function is executed.
|
|
88
|
-
*/
|
|
89
|
-
types: readonly Type.AnyEntity[];
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Keys of the required services.
|
|
93
|
-
*/
|
|
94
|
-
services: readonly string[];
|
|
95
|
-
|
|
96
|
-
meta?: {
|
|
97
|
-
/**
|
|
98
|
-
* Tools that are projected from functions have this annotation.
|
|
99
|
-
*
|
|
100
|
-
* deployedFunctionId:
|
|
101
|
-
* - Backend deployment ID assigned by the EDGE function service (typically a UUID).
|
|
102
|
-
* - Used for remote invocation via `FunctionInvocationService` → `RemoteFunctionExecutionService`.
|
|
103
|
-
* - Persisted on the corresponding ECHO `Function.Function` object's metadata under the
|
|
104
|
-
* `FUNCTIONS_META_KEY` and retrieved with `getUserFunctionIdInMetadata`.
|
|
105
|
-
*/
|
|
106
|
-
deployedFunctionId?: string;
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
handler: FunctionHandler<TInput, TOutput, S>;
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
export declare namespace FunctionDefinition {
|
|
113
|
-
export type Any = FunctionDefinition<any, any, any>;
|
|
114
|
-
export type Input<T extends Any> = T extends FunctionDefinition<infer I, infer _O, infer _S> ? I : never;
|
|
115
|
-
export type Output<T extends Any> = T extends FunctionDefinition<infer _I, infer O, infer _S> ? O : never;
|
|
116
|
-
export type Services<T extends Any> = T extends FunctionDefinition<infer _I, infer _O, infer S> ? S : never;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export type FunctionProps<T, O> = {
|
|
120
|
-
key: string;
|
|
121
|
-
name: string;
|
|
122
|
-
description?: string;
|
|
123
|
-
inputSchema: Schema.Schema<T, any>;
|
|
124
|
-
outputSchema?: Schema.Schema<O, any>;
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* List of types the function uses.
|
|
128
|
-
* This is used to ensure that the types are available when the function is executed.
|
|
129
|
-
*/
|
|
130
|
-
types?: readonly Type.AnyEntity[];
|
|
131
|
-
|
|
132
|
-
// TODO(dmaretskyi): This currently doesn't cause a compile-time error if the handler requests a service that is not specified
|
|
133
|
-
services?: readonly Context.Tag<any, any>[];
|
|
134
|
-
|
|
135
|
-
handler: FunctionHandler<T, O, FunctionServices>;
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
// TODO(dmaretskyi): Output type doesn't get typechecked.
|
|
139
|
-
export const defineFunction: {
|
|
140
|
-
<I, O>(params: FunctionProps<I, O>): FunctionDefinition<I, O, FunctionServices>;
|
|
141
|
-
} = ({ key, name, description, inputSchema, outputSchema = Schema.Any, handler, types, services }) => {
|
|
142
|
-
if (!Schema.isSchema(inputSchema)) {
|
|
143
|
-
throw new Error('Input schema must be a valid schema');
|
|
144
|
-
}
|
|
145
|
-
if (typeof handler !== 'function') {
|
|
146
|
-
throw new Error('Handler must be a function');
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// Captures the function definition location.
|
|
150
|
-
const limit = Error.stackTraceLimit;
|
|
151
|
-
Error.stackTraceLimit = 2;
|
|
152
|
-
const traceError = new Error();
|
|
153
|
-
Error.stackTraceLimit = limit;
|
|
154
|
-
let cache: false | string = false;
|
|
155
|
-
const captureStackTrace = () => {
|
|
156
|
-
if (cache !== false) {
|
|
157
|
-
return cache;
|
|
158
|
-
}
|
|
159
|
-
if (traceError.stack !== undefined) {
|
|
160
|
-
const stack = traceError.stack.split('\n');
|
|
161
|
-
if (stack[2] !== undefined) {
|
|
162
|
-
cache = stack[2].trim();
|
|
163
|
-
return cache;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
const handlerWithSpan = (...args: any[]) => {
|
|
169
|
-
const result = (handler as any)(...args);
|
|
170
|
-
if (Effect.isEffect(result)) {
|
|
171
|
-
return Effect.withSpan(result, `${key ?? name}`, {
|
|
172
|
-
captureStackTrace,
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
return result;
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
return {
|
|
179
|
-
[typeId]: true,
|
|
180
|
-
key,
|
|
181
|
-
name,
|
|
182
|
-
description,
|
|
183
|
-
inputSchema,
|
|
184
|
-
outputSchema,
|
|
185
|
-
handler: handlerWithSpan,
|
|
186
|
-
types: types ?? [],
|
|
187
|
-
services: !services ? [] : getServiceKeys(services),
|
|
188
|
-
};
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
const getServiceKeys = (services: readonly Context.Tag<any, any>[]) => {
|
|
192
|
-
return services.map((tag: any) => {
|
|
193
|
-
if (typeof tag.key === 'string') {
|
|
194
|
-
return tag.key;
|
|
195
|
-
}
|
|
196
|
-
failedInvariant();
|
|
197
|
-
});
|
|
198
|
-
};
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Converts a FunctionDefinition to an OperationDefinition with handler.
|
|
202
|
-
* The function handler is adapted to the OperationHandler format.
|
|
203
|
-
*
|
|
204
|
-
* Note: FunctionDefinition stores service keys as strings, not Tag types,
|
|
205
|
-
* so we can't use Operation.withHandler's type inference here.
|
|
206
|
-
*/
|
|
207
|
-
export const toOperation = <T, O, S extends FunctionServices = FunctionServices>(
|
|
208
|
-
functionDef: FunctionDefinition<T, O, S>,
|
|
209
|
-
): Operation.Definition<T, O> & { handler: Operation.Handler<T, O, any, S> } => {
|
|
210
|
-
const op = Operation.make({
|
|
211
|
-
schema: {
|
|
212
|
-
input: functionDef.inputSchema,
|
|
213
|
-
output: functionDef.outputSchema ?? Schema.Any,
|
|
214
|
-
},
|
|
215
|
-
meta: {
|
|
216
|
-
key: functionDef.key,
|
|
217
|
-
name: functionDef.name,
|
|
218
|
-
description: functionDef.description,
|
|
219
|
-
},
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
// Adapt FunctionHandler signature to OperationHandler format.
|
|
223
|
-
// FunctionHandler expects { context, data }, OperationHandler expects just input.
|
|
224
|
-
const operationHandler: Operation.Handler<T, O, any, S> = (input: T) => {
|
|
225
|
-
const result = functionDef.handler({
|
|
226
|
-
context: {} as FunctionContext,
|
|
227
|
-
data: input,
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
// Convert Promise or plain value to Effect.
|
|
231
|
-
if (Effect.isEffect(result)) {
|
|
232
|
-
return result;
|
|
233
|
-
}
|
|
234
|
-
if (result instanceof Promise) {
|
|
235
|
-
return Effect.tryPromise(() => result);
|
|
236
|
-
}
|
|
237
|
-
return Effect.succeed(result as O);
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
// Manually attach handler since FunctionDefinition stores service keys as strings,
|
|
241
|
-
// not Tag types, so withHandler's type inference doesn't apply.
|
|
242
|
-
return {
|
|
243
|
-
...op,
|
|
244
|
-
handler: operationHandler,
|
|
245
|
-
};
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
export const FunctionDefinition = {
|
|
249
|
-
make: defineFunction,
|
|
250
|
-
isFunction: (value: unknown): value is FunctionDefinition.Any => {
|
|
251
|
-
return typeof value === 'object' && value !== null && Symbol.for('@dxos/functions/FunctionDefinition') in value;
|
|
252
|
-
},
|
|
253
|
-
serialize: (functionDef: FunctionDefinition.Any): Function.Function => {
|
|
254
|
-
assertArgument(FunctionDefinition.isFunction(functionDef), 'functionDef');
|
|
255
|
-
return serializeFunction(functionDef);
|
|
256
|
-
},
|
|
257
|
-
deserialize: (functionObj: Function.Function): FunctionDefinition.Any => {
|
|
258
|
-
assertArgument(Obj.instanceOf(Function.Function, functionObj), 'functionObj');
|
|
259
|
-
return deserializeFunction(functionObj);
|
|
260
|
-
},
|
|
261
|
-
toOperation,
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
export const serializeFunction = (functionDef: FunctionDefinition.Any): Function.Function => {
|
|
265
|
-
const fn = Function.make({
|
|
266
|
-
key: functionDef.key,
|
|
267
|
-
name: functionDef.name,
|
|
268
|
-
version: '0.1.0',
|
|
269
|
-
description: functionDef.description,
|
|
270
|
-
inputSchema: JsonSchema.toJsonSchema(functionDef.inputSchema),
|
|
271
|
-
outputSchema: !functionDef.outputSchema ? undefined : JsonSchema.toJsonSchema(functionDef.outputSchema),
|
|
272
|
-
services: functionDef.services,
|
|
273
|
-
});
|
|
274
|
-
if (functionDef.meta?.deployedFunctionId) {
|
|
275
|
-
Obj.change(fn, (fn) => setUserFunctionIdInMetadata(Obj.getMeta(fn), functionDef.meta!.deployedFunctionId!));
|
|
276
|
-
}
|
|
277
|
-
return fn;
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
export const deserializeFunction = (functionObj: Function.Function): FunctionDefinition<unknown, unknown> => {
|
|
281
|
-
return {
|
|
282
|
-
[typeId]: true,
|
|
283
|
-
// TODO(dmaretskyi): Fix key.
|
|
284
|
-
key: functionObj.key ?? functionObj.name,
|
|
285
|
-
name: functionObj.name,
|
|
286
|
-
description: functionObj.description,
|
|
287
|
-
inputSchema: !functionObj.inputSchema ? Schema.Unknown : JsonSchema.toEffectSchema(functionObj.inputSchema),
|
|
288
|
-
outputSchema: !functionObj.outputSchema ? undefined : JsonSchema.toEffectSchema(functionObj.outputSchema),
|
|
289
|
-
// TODO(dmaretskyi): This should throw error.
|
|
290
|
-
handler: () => {},
|
|
291
|
-
services: functionObj.services ?? [],
|
|
292
|
-
types: [],
|
|
293
|
-
meta: {
|
|
294
|
-
deployedFunctionId: getUserFunctionIdInMetadata(Obj.getMeta(functionObj)),
|
|
295
|
-
},
|
|
296
|
-
};
|
|
297
|
-
};
|
|
26
|
+
| Feed.FeedService
|
|
27
|
+
| Trace.TraceService
|
|
28
|
+
// TODO(dmaretskyi): `FunctionInvocationService` is being phased out in favour of `Operation.Service`;
|
|
29
|
+
// it's kept in the union until `functions-runtime/local-function-execution.ts` migrates.
|
|
30
|
+
| FunctionInvocationService
|
|
31
|
+
| Operation.Service;
|
|
@@ -10,136 +10,23 @@ import * as Effect from 'effect/Effect';
|
|
|
10
10
|
import * as Layer from 'effect/Layer';
|
|
11
11
|
import * as Redacted from 'effect/Redacted';
|
|
12
12
|
|
|
13
|
-
import {
|
|
14
|
-
import { Database } from '@dxos/echo';
|
|
13
|
+
import { Credential } from '@dxos/compute';
|
|
14
|
+
import { Database, Query } from '@dxos/echo';
|
|
15
15
|
import { AccessToken } from '@dxos/types';
|
|
16
16
|
|
|
17
|
-
export
|
|
18
|
-
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
// TODO(dmaretskyi): Unify with other apis.
|
|
22
|
-
// packages/sdk/schema/src/common/access-token.ts
|
|
23
|
-
export type ServiceCredential = {
|
|
24
|
-
service: string;
|
|
25
|
-
|
|
26
|
-
// TODO(dmaretskyi): Build out.
|
|
27
|
-
apiKey?: string;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export class CredentialsService extends Context.Tag('@dxos/functions/CredentialsService')<
|
|
31
|
-
CredentialsService,
|
|
32
|
-
{
|
|
33
|
-
/**
|
|
34
|
-
* Query all.
|
|
35
|
-
*/
|
|
36
|
-
queryCredentials: (query: CredentialQuery) => Promise<ServiceCredential[]>;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Get a single credential.
|
|
40
|
-
* @throws {Error} If no credential is found.
|
|
41
|
-
*/
|
|
42
|
-
getCredential: (query: CredentialQuery) => Promise<ServiceCredential>;
|
|
43
|
-
}
|
|
44
|
-
>() {
|
|
45
|
-
static getCredential = (query: CredentialQuery): Effect.Effect<ServiceCredential, never, CredentialsService> =>
|
|
46
|
-
Effect.gen(function* () {
|
|
47
|
-
const credentials = yield* CredentialsService;
|
|
48
|
-
return yield* Effect.promise(() => credentials.getCredential(query));
|
|
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 = (
|
|
64
|
-
credentials: {
|
|
65
|
-
service: string;
|
|
66
|
-
apiKey: Config.Config<Redacted.Redacted<string>>;
|
|
67
|
-
}[],
|
|
68
|
-
) =>
|
|
69
|
-
Layer.effect(
|
|
70
|
-
CredentialsService,
|
|
71
|
-
Effect.gen(function* () {
|
|
72
|
-
const serviceCredentials = yield* Effect.forEach(credentials, ({ service, apiKey }) =>
|
|
73
|
-
Effect.gen(function* () {
|
|
74
|
-
return {
|
|
75
|
-
service,
|
|
76
|
-
apiKey: Redacted.value(yield* apiKey),
|
|
77
|
-
};
|
|
78
|
-
}),
|
|
79
|
-
);
|
|
80
|
-
|
|
81
|
-
return new ConfiguredCredentialsService(serviceCredentials);
|
|
82
|
-
}),
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
static layerFromDatabase = ({ caching = false }: { caching?: boolean } = {}) =>
|
|
86
|
-
Layer.effect(
|
|
87
|
-
CredentialsService,
|
|
88
|
-
Effect.gen(function* () {
|
|
89
|
-
const dbService = yield* Database.Service;
|
|
90
|
-
const cache = new Map<string, ServiceCredential[]>();
|
|
91
|
-
|
|
92
|
-
const queryCredentials = async (query: CredentialQuery): Promise<ServiceCredential[]> => {
|
|
93
|
-
const cacheKey = JSON.stringify(query);
|
|
94
|
-
if (caching && cache.has(cacheKey)) {
|
|
95
|
-
return cache.get(cacheKey)!;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const accessTokens = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
|
|
99
|
-
const credentials = accessTokens
|
|
100
|
-
.filter((accessToken) => accessToken.source === query.service)
|
|
101
|
-
.map((accessToken) => ({
|
|
102
|
-
service: accessToken.source,
|
|
103
|
-
apiKey: accessToken.token,
|
|
104
|
-
}));
|
|
105
|
-
|
|
106
|
-
if (caching) {
|
|
107
|
-
cache.set(cacheKey, credentials);
|
|
108
|
-
}
|
|
17
|
+
export class ConfiguredCredentialsService implements Context.Tag.Service<Credential.CredentialsService> {
|
|
18
|
+
constructor(private readonly credentials: Credential.ServiceCredential[] = []) {}
|
|
109
19
|
|
|
110
|
-
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
return {
|
|
114
|
-
getCredential: async (query) => {
|
|
115
|
-
const credentials = await queryCredentials(query);
|
|
116
|
-
if (credentials.length === 0) {
|
|
117
|
-
throw new Error(`Credential not found for service: ${query.service}`);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return credentials[0];
|
|
121
|
-
},
|
|
122
|
-
queryCredentials: async (query) => {
|
|
123
|
-
return queryCredentials(query);
|
|
124
|
-
},
|
|
125
|
-
};
|
|
126
|
-
}),
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export class ConfiguredCredentialsService implements Context.Tag.Service<CredentialsService> {
|
|
131
|
-
constructor(private readonly credentials: ServiceCredential[] = []) {}
|
|
132
|
-
|
|
133
|
-
addCredentials(credentials: ServiceCredential[]): ConfiguredCredentialsService {
|
|
20
|
+
addCredentials(credentials: Credential.ServiceCredential[]): ConfiguredCredentialsService {
|
|
134
21
|
this.credentials.push(...credentials);
|
|
135
22
|
return this;
|
|
136
23
|
}
|
|
137
24
|
|
|
138
|
-
async queryCredentials(query: CredentialQuery): Promise<ServiceCredential[]> {
|
|
25
|
+
async queryCredentials(query: Credential.CredentialQuery): Promise<Credential.ServiceCredential[]> {
|
|
139
26
|
return this.credentials.filter((credential) => credential.service === query.service);
|
|
140
27
|
}
|
|
141
28
|
|
|
142
|
-
async getCredential(query: CredentialQuery): Promise<ServiceCredential> {
|
|
29
|
+
async getCredential(query: Credential.CredentialQuery): Promise<Credential.ServiceCredential> {
|
|
143
30
|
const credential = this.credentials.find((credential) => credential.service === query.service);
|
|
144
31
|
if (!credential) {
|
|
145
32
|
throw new Error(`Credential not found for service: ${query.service}`);
|
|
@@ -157,3 +44,72 @@ export const withAuthorization = (token: string, kind?: 'Bearer' | 'Basic') =>
|
|
|
157
44
|
const authorization = kind ? `${kind} ${token}` : token;
|
|
158
45
|
return HttpClientRequest.setHeader(request, 'Authorization', authorization);
|
|
159
46
|
});
|
|
47
|
+
|
|
48
|
+
export const configuredCredentialsLayer = (credentials: Credential.ServiceCredential[]) =>
|
|
49
|
+
Layer.succeed(Credential.CredentialsService, new ConfiguredCredentialsService(credentials));
|
|
50
|
+
|
|
51
|
+
export const credentialsLayerConfig = (
|
|
52
|
+
credentials: {
|
|
53
|
+
service: string;
|
|
54
|
+
apiKey: Config.Config<Redacted.Redacted<string>>;
|
|
55
|
+
}[],
|
|
56
|
+
) =>
|
|
57
|
+
Layer.effect(
|
|
58
|
+
Credential.CredentialsService,
|
|
59
|
+
Effect.gen(function* () {
|
|
60
|
+
const serviceCredentials = yield* Effect.forEach(credentials, ({ service, apiKey }) =>
|
|
61
|
+
Effect.gen(function* () {
|
|
62
|
+
return {
|
|
63
|
+
service,
|
|
64
|
+
apiKey: Redacted.value(yield* apiKey),
|
|
65
|
+
};
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
return new ConfiguredCredentialsService(serviceCredentials);
|
|
70
|
+
}),
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
export const credentialsLayerFromDatabase = ({ caching = false }: { caching?: boolean } = {}) =>
|
|
74
|
+
Layer.effect(
|
|
75
|
+
Credential.CredentialsService,
|
|
76
|
+
Effect.gen(function* () {
|
|
77
|
+
const dbService = yield* Database.Service;
|
|
78
|
+
const cache = new Map<string, Credential.ServiceCredential[]>();
|
|
79
|
+
|
|
80
|
+
const queryCredentials = async (query: Credential.CredentialQuery): Promise<Credential.ServiceCredential[]> => {
|
|
81
|
+
const cacheKey = JSON.stringify(query);
|
|
82
|
+
if (caching && cache.has(cacheKey)) {
|
|
83
|
+
return cache.get(cacheKey)!;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const accessTokens = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
|
|
87
|
+
const credentials = accessTokens
|
|
88
|
+
.filter((accessToken) => accessToken.source === query.service)
|
|
89
|
+
.map((accessToken) => ({
|
|
90
|
+
service: accessToken.source,
|
|
91
|
+
apiKey: accessToken.token,
|
|
92
|
+
}));
|
|
93
|
+
|
|
94
|
+
if (caching) {
|
|
95
|
+
cache.set(cacheKey, credentials);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return credentials;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
getCredential: async (query) => {
|
|
103
|
+
const credentials = await queryCredentials(query);
|
|
104
|
+
if (credentials.length === 0) {
|
|
105
|
+
throw new Error(`Credential not found for service: ${query.service}`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return credentials[0];
|
|
109
|
+
},
|
|
110
|
+
queryCredentials: async (query) => {
|
|
111
|
+
return queryCredentials(query);
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}),
|
|
115
|
+
);
|
|
@@ -5,18 +5,17 @@ import * as Context from 'effect/Context';
|
|
|
5
5
|
import * as Effect from 'effect/Effect';
|
|
6
6
|
import * as Layer from 'effect/Layer';
|
|
7
7
|
|
|
8
|
-
import type
|
|
9
|
-
import { type FunctionDefinition, type InvocationServices } from '../sdk';
|
|
8
|
+
import { type FunctionNotFoundError, Operation } from '@dxos/compute';
|
|
10
9
|
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated
|
|
12
|
+
*/
|
|
11
13
|
export class FunctionInvocationService extends Context.Tag('@dxos/functions/FunctionInvocationService')<
|
|
12
14
|
FunctionInvocationService,
|
|
13
15
|
{
|
|
14
|
-
invokeFunction<I, O>(
|
|
15
|
-
functionDef: FunctionDefinition<I, O, any>,
|
|
16
|
-
input: I,
|
|
17
|
-
): Effect.Effect<O, never, InvocationServices>;
|
|
16
|
+
invokeFunction<I, O>(functionDef: Operation.Definition<I, O, any>, input: I): Effect.Effect<O>;
|
|
18
17
|
|
|
19
|
-
resolveFunction(key: string): Effect.Effect<
|
|
18
|
+
resolveFunction(key: string): Effect.Effect<Operation.Definition.Any, FunctionNotFoundError>;
|
|
20
19
|
}
|
|
21
20
|
>() {
|
|
22
21
|
static layerNotAvailable = Layer.succeed(FunctionInvocationService, {
|
|
@@ -25,13 +24,13 @@ export class FunctionInvocationService extends Context.Tag('@dxos/functions/Func
|
|
|
25
24
|
});
|
|
26
25
|
|
|
27
26
|
static invokeFunction = <I, O>(
|
|
28
|
-
functionDef:
|
|
27
|
+
functionDef: Operation.Definition<I, O, any>,
|
|
29
28
|
input: I,
|
|
30
|
-
): Effect.Effect<O, never, FunctionInvocationService
|
|
29
|
+
): Effect.Effect<O, never, FunctionInvocationService> =>
|
|
31
30
|
Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.invokeFunction)(functionDef, input);
|
|
32
31
|
|
|
33
32
|
static resolveFunction = (
|
|
34
33
|
key: string,
|
|
35
|
-
): Effect.Effect<
|
|
34
|
+
): Effect.Effect<Operation.Definition.Any, FunctionNotFoundError, FunctionInvocationService> =>
|
|
36
35
|
Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.resolveFunction)(key);
|
|
37
36
|
}
|
package/src/services/index.ts
CHANGED
|
@@ -3,9 +3,6 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
export * from './credentials';
|
|
6
|
-
export { ConfiguredCredentialsService, type ServiceCredential } from './credentials';
|
|
7
|
-
export * from './event-logger';
|
|
8
|
-
export { createEventLogger, createDefectLogger } from './event-logger';
|
|
9
6
|
export * from './function-invocation-service';
|
|
10
7
|
export * from './queues';
|
|
11
|
-
export
|
|
8
|
+
export { MESSAGE_PROPERTY_TOOL_CALL_ID } from './tracing';
|
package/src/services/queues.ts
CHANGED
|
@@ -2,81 +2,4 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
import * as Effect from 'effect/Effect';
|
|
7
|
-
import * as Layer from 'effect/Layer';
|
|
8
|
-
|
|
9
|
-
import type { Entity } from '@dxos/echo';
|
|
10
|
-
import type { Queue, QueueAPI, QueueFactory } from '@dxos/echo-db';
|
|
11
|
-
import type { DXN, QueueSubspaceTag } from '@dxos/keys';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Gives access to all queues.
|
|
15
|
-
*/
|
|
16
|
-
export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
|
|
17
|
-
QueueService,
|
|
18
|
-
{
|
|
19
|
-
/**
|
|
20
|
-
* API to access the queues.
|
|
21
|
-
*/
|
|
22
|
-
readonly queues: QueueAPI;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* The queue that is used to store the context of the current research.
|
|
26
|
-
* @deprecated Use `ContextQueueService` instead.
|
|
27
|
-
*/
|
|
28
|
-
readonly queue: Queue | undefined;
|
|
29
|
-
}
|
|
30
|
-
>() {
|
|
31
|
-
static notAvailable = Layer.succeed(QueueService, {
|
|
32
|
-
queues: {
|
|
33
|
-
get(_dxn) {
|
|
34
|
-
throw new Error('Queues not available');
|
|
35
|
-
},
|
|
36
|
-
create() {
|
|
37
|
-
throw new Error('Queues not available');
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
queue: undefined,
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
static make = (queues: QueueFactory, queue?: Queue): Context.Tag.Service<QueueService> => {
|
|
44
|
-
return {
|
|
45
|
-
queues,
|
|
46
|
-
queue,
|
|
47
|
-
};
|
|
48
|
-
};
|
|
49
|
-
|
|
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 Entity.Unknown = Entity.Unknown>(
|
|
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 Entity.Unknown = Entity.Unknown>(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 Entity.Unknown = Entity.Unknown>(queue: Queue<T>, objects: T[]): Effect.Effect<void> =>
|
|
69
|
-
Effect.promise(() => queue.append(objects));
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Gives access to a specific queue passed as a context.
|
|
74
|
-
*/
|
|
75
|
-
export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQueueService')<
|
|
76
|
-
ContextQueueService,
|
|
77
|
-
{
|
|
78
|
-
readonly queue: Queue;
|
|
79
|
-
}
|
|
80
|
-
>() {
|
|
81
|
-
static layer = (queue: Queue) => Layer.succeed(ContextQueueService, { queue });
|
|
82
|
-
}
|
|
5
|
+
export { QueueService, feedServiceFromQueueServiceLayer } from '@dxos/echo-db';
|