@dxos/functions 0.8.4-main.dedc0f3 → 0.8.4-main.e8ec1fe
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 +732 -1070
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +732 -1070
- 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 +47 -55
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/{examples → example}/fib.d.ts +1 -1
- 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/{examples → example}/reply.d.ts +1 -1
- package/dist/types/src/example/reply.d.ts.map +1 -0
- package/dist/types/src/{examples → example}/sleep.d.ts +1 -1
- package/dist/types/src/example/sleep.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +4 -8
- 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 +7 -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 +3 -1
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +9 -12
- 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/{url.d.ts → types/url.d.ts} +1 -10
- package/dist/types/src/types/url.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +16 -70
- package/src/errors.ts +9 -9
- package/src/{examples → example}/fib.ts +5 -3
- package/src/example/forex-effect.ts +40 -0
- package/src/example/index.ts +13 -0
- package/src/{examples → example}/reply.ts +6 -3
- package/src/{examples → example}/sleep.ts +5 -3
- package/src/index.ts +4 -8
- 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 +12 -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 +3 -1
- package/src/services/tracing.ts +24 -56
- package/src/{schema.ts → types/Function.ts} +20 -26
- 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 -265
- package/dist/lib/browser/bundler/index.mjs.map +0 -7
- package/dist/lib/browser/chunk-ANP3DFCO.mjs +0 -623
- package/dist/lib/browser/chunk-ANP3DFCO.mjs.map +0 -7
- package/dist/lib/browser/chunk-J5LGTIGS.mjs +0 -10
- package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +0 -7
- package/dist/lib/browser/edge/index.mjs +0 -83
- package/dist/lib/browser/edge/index.mjs.map +0 -7
- package/dist/lib/browser/testing/index.mjs +0 -129
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/bundler/index.mjs +0 -266
- package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-MPKVY7ZR.mjs +0 -625
- package/dist/lib/node-esm/chunk-MPKVY7ZR.mjs.map +0 -7
- package/dist/lib/node-esm/edge/index.mjs +0 -84
- package/dist/lib/node-esm/edge/index.mjs.map +0 -7
- package/dist/lib/node-esm/testing/index.mjs +0 -130
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/bundler/bundler.d.ts +0 -49
- 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 -17
- 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/examples/fib.d.ts.map +0 -1
- package/dist/types/src/examples/index.d.ts +0 -4
- package/dist/types/src/examples/index.d.ts.map +0 -1
- package/dist/types/src/examples/reply.d.ts.map +0 -1
- package/dist/types/src/examples/sleep.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 -94
- package/dist/types/src/handler.d.ts.map +0 -1
- package/dist/types/src/schema.d.ts +0 -43
- package/dist/types/src/schema.d.ts.map +0 -1
- package/dist/types/src/services/database.d.ts +0 -98
- package/dist/types/src/services/database.d.ts.map +0 -1
- package/dist/types/src/services/local-function-execution.d.ts +0 -25
- 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 -15
- 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/persist-database.test.d.ts +0 -2
- package/dist/types/src/testing/persist-database.test.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/triggers/index.d.ts +0 -4
- package/dist/types/src/triggers/index.d.ts.map +0 -1
- package/dist/types/src/triggers/input-builder.d.ts +0 -3
- package/dist/types/src/triggers/input-builder.d.ts.map +0 -1
- package/dist/types/src/triggers/invocation-tracer.d.ts +0 -35
- package/dist/types/src/triggers/invocation-tracer.d.ts.map +0 -1
- package/dist/types/src/triggers/trigger-dispatcher.d.ts +0 -75
- package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +0 -1
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +0 -2
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +0 -1
- package/dist/types/src/triggers/trigger-state-store.d.ts +0 -27
- package/dist/types/src/triggers/trigger-state-store.d.ts.map +0 -1
- package/dist/types/src/types.d.ts +0 -211
- package/dist/types/src/types.d.ts.map +0 -1
- package/dist/types/src/url.d.ts.map +0 -1
- package/src/bundler/bundler.test.ts +0 -58
- package/src/bundler/bundler.ts +0 -291
- package/src/bundler/index.ts +0 -5
- package/src/edge/functions.ts +0 -67
- package/src/edge/index.ts +0 -9
- package/src/examples/index.ts +0 -7
- package/src/executor/executor.ts +0 -54
- package/src/handler.ts +0 -201
- package/src/services/database.ts +0 -170
- package/src/services/local-function-execution.ts +0 -114
- package/src/services/remote-function-execution-service.ts +0 -46
- 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 -111
- package/src/testing/logger.ts +0 -16
- package/src/testing/persist-database.test.ts +0 -87
- package/src/testing/services.ts +0 -115
- package/src/trace.ts +0 -178
- package/src/translations.ts +0 -20
- package/src/triggers/index.ts +0 -7
- package/src/triggers/input-builder.ts +0 -35
- package/src/triggers/invocation-tracer.ts +0 -99
- package/src/triggers/trigger-dispatcher.test.ts +0 -652
- package/src/triggers/trigger-dispatcher.ts +0 -512
- package/src/triggers/trigger-state-store.ts +0 -60
- package/src/types.ts +0 -200
- package/src/url.ts +0 -55
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,13 +2,17 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
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';
|
|
7
12
|
|
|
8
13
|
import { Query } from '@dxos/echo';
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
import { DatabaseService } from './database';
|
|
14
|
+
import { DatabaseService } from '@dxos/echo-db';
|
|
15
|
+
import { AccessToken } from '@dxos/types';
|
|
12
16
|
|
|
13
17
|
export type CredentialQuery = {
|
|
14
18
|
service?: string;
|
|
@@ -79,7 +83,7 @@ export class CredentialsService extends Context.Tag('@dxos/functions/Credentials
|
|
|
79
83
|
Effect.gen(function* () {
|
|
80
84
|
const dbService = yield* DatabaseService;
|
|
81
85
|
const queryCredentials = async (query: CredentialQuery): Promise<ServiceCredential[]> => {
|
|
82
|
-
const { objects: accessTokens } = await dbService.db.query(Query.type(
|
|
86
|
+
const { objects: accessTokens } = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
|
|
83
87
|
return accessTokens
|
|
84
88
|
.filter((accessToken) => accessToken.source === query.service)
|
|
85
89
|
.map((accessToken) => ({
|
|
@@ -93,6 +97,7 @@ export class CredentialsService extends Context.Tag('@dxos/functions/Credentials
|
|
|
93
97
|
if (credentials.length === 0) {
|
|
94
98
|
throw new Error(`Credential not found for service: ${query.service}`);
|
|
95
99
|
}
|
|
100
|
+
|
|
96
101
|
return credentials[0];
|
|
97
102
|
},
|
|
98
103
|
queryCredentials: async (query) => {
|
|
@@ -120,6 +125,7 @@ export class ConfiguredCredentialsService implements Context.Tag.Service<Credent
|
|
|
120
125
|
if (!credential) {
|
|
121
126
|
throw new Error(`Credential not found for service: ${query.service}`);
|
|
122
127
|
}
|
|
128
|
+
|
|
123
129
|
return credential;
|
|
124
130
|
}
|
|
125
131
|
}
|
|
@@ -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';
|
package/src/services/tracing.ts
CHANGED
|
@@ -2,14 +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 {
|
|
10
|
-
import
|
|
11
|
-
import { log } from '@dxos/log';
|
|
12
|
-
import { DataType } from '@dxos/schema';
|
|
11
|
+
import type { ObjectId } from '@dxos/echo/internal';
|
|
12
|
+
import { Message } from '@dxos/types';
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Provides a way for compute primitives (functions, workflows, tools)
|
|
@@ -30,30 +30,12 @@ export class TracingService extends Context.Tag('@dxos/functions/TracingService'
|
|
|
30
30
|
write: (event: Obj.Any) => void;
|
|
31
31
|
}
|
|
32
32
|
>() {
|
|
33
|
-
static noop: Context.Tag.Service<TracingService> = {
|
|
34
|
-
|
|
35
|
-
static layerNoop = Layer.succeed(TracingService, TracingService.noop);
|
|
36
|
-
|
|
37
|
-
static console: Context.Tag.Service<TracingService> = {
|
|
38
|
-
write: (event) => {
|
|
39
|
-
// eslint-disable-next-line no-console
|
|
40
|
-
console.log(event);
|
|
41
|
-
},
|
|
33
|
+
static noop: Context.Tag.Service<TracingService> = {
|
|
42
34
|
getTraceContext: () => ({}),
|
|
35
|
+
write: () => {},
|
|
43
36
|
};
|
|
44
37
|
|
|
45
|
-
static
|
|
46
|
-
|
|
47
|
-
static layerLogInfo = () =>
|
|
48
|
-
Layer.succeed(TracingService, {
|
|
49
|
-
write: (event) => {
|
|
50
|
-
if (Obj.instanceOf(AgentStatus, event)) {
|
|
51
|
-
log.info('status', { message: event.message });
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
|
-
getTraceContext: () => ({}),
|
|
55
|
-
});
|
|
56
|
-
|
|
38
|
+
static layerNoop: Layer.Layer<TracingService> = Layer.succeed(TracingService, TracingService.noop);
|
|
57
39
|
/**
|
|
58
40
|
* Creates a TracingService layer that emits events to the parent tracing service.
|
|
59
41
|
*/
|
|
@@ -70,43 +52,29 @@ export class TracingService extends Context.Tag('@dxos/functions/TracingService'
|
|
|
70
52
|
}),
|
|
71
53
|
);
|
|
72
54
|
|
|
73
|
-
static layerQueue = (queue: Queue) =>
|
|
74
|
-
Layer.effect(
|
|
75
|
-
TracingService,
|
|
76
|
-
Effect.gen(function* () {
|
|
77
|
-
// TODO(dmaretskyi): Batching.
|
|
78
|
-
return {
|
|
79
|
-
write: (event) => queue.append([event]),
|
|
80
|
-
getTraceContext: () => ({
|
|
81
|
-
debugInfo: {
|
|
82
|
-
queue: queue.dxn.toString(),
|
|
83
|
-
},
|
|
84
|
-
}),
|
|
85
|
-
};
|
|
86
|
-
}),
|
|
87
|
-
);
|
|
88
|
-
|
|
89
55
|
/**
|
|
90
56
|
* Emit the current human-readable execution status.
|
|
91
57
|
*/
|
|
92
|
-
static emitStatus: (
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
+
});
|
|
103
71
|
|
|
104
72
|
static emitConverationMessage: (
|
|
105
|
-
data: Obj.MakeProps<typeof
|
|
73
|
+
data: Obj.MakeProps<typeof Message.Message>,
|
|
106
74
|
) => Effect.Effect<void, never, TracingService> = Effect.fnUntraced(function* (data) {
|
|
107
75
|
const tracing = yield* TracingService;
|
|
108
76
|
tracing.write(
|
|
109
|
-
Obj.make(
|
|
77
|
+
Obj.make(Message.Message, {
|
|
110
78
|
parentMessage: tracing.getTraceContext().parentMessage,
|
|
111
79
|
...data,
|
|
112
80
|
properties: {
|
|
@@ -135,6 +103,6 @@ export namespace TracingService {
|
|
|
135
103
|
}
|
|
136
104
|
|
|
137
105
|
/**
|
|
138
|
-
* Goes into {@link
|
|
106
|
+
* Goes into {@link Message['properties']}
|
|
139
107
|
*/
|
|
140
108
|
export const MESSAGE_PROPERTY_TOOL_CALL_ID = 'toolCallId' as const;
|
|
@@ -2,36 +2,17 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
6
|
|
|
7
|
-
import { Type } from '@dxos/echo';
|
|
8
|
-
import { JsonSchemaType, LabelAnnotation, Ref } from '@dxos/echo
|
|
9
|
-
import { DataType } from '@dxos/schema';
|
|
7
|
+
import { Obj, Type } from '@dxos/echo';
|
|
8
|
+
import { JsonSchemaType, LabelAnnotation, Ref } from '@dxos/echo/internal';
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
* Source script.
|
|
13
|
-
*/
|
|
14
|
-
export const ScriptType = Schema.Struct({
|
|
15
|
-
name: Schema.optional(Schema.String),
|
|
16
|
-
description: Schema.optional(Schema.String),
|
|
17
|
-
// TODO(burdon): Change to hash of deployed content.
|
|
18
|
-
// Whether source has changed since last deploy.
|
|
19
|
-
changed: Schema.optional(Schema.Boolean),
|
|
20
|
-
source: Ref(DataType.Text),
|
|
21
|
-
}).pipe(
|
|
22
|
-
Type.Obj({
|
|
23
|
-
typename: 'dxos.org/type/Script',
|
|
24
|
-
version: '0.1.0',
|
|
25
|
-
}),
|
|
26
|
-
LabelAnnotation.set(['name']),
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
export interface ScriptType extends Schema.Schema.Type<typeof ScriptType> {}
|
|
10
|
+
import { Script } from './Script';
|
|
30
11
|
|
|
31
12
|
/**
|
|
32
13
|
* Function deployment.
|
|
33
14
|
*/
|
|
34
|
-
export const
|
|
15
|
+
export const Function = Schema.Struct({
|
|
35
16
|
/**
|
|
36
17
|
* Global registry ID.
|
|
37
18
|
* NOTE: The `key` property refers to the original registry entry.
|
|
@@ -49,13 +30,24 @@ export const FunctionType = Schema.Struct({
|
|
|
49
30
|
|
|
50
31
|
description: Schema.optional(Schema.String),
|
|
51
32
|
|
|
33
|
+
/**
|
|
34
|
+
* ISO date string of the last deployment.
|
|
35
|
+
*/
|
|
36
|
+
updated: Schema.optional(Schema.String),
|
|
37
|
+
|
|
52
38
|
// Reference to a source script if it exists within ECHO.
|
|
53
39
|
// TODO(burdon): Don't ref ScriptType directly (core).
|
|
54
|
-
source: Schema.optional(Ref(
|
|
40
|
+
source: Schema.optional(Ref(Script)),
|
|
55
41
|
|
|
56
42
|
inputSchema: Schema.optional(JsonSchemaType),
|
|
57
43
|
outputSchema: Schema.optional(JsonSchemaType),
|
|
58
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
|
+
|
|
59
51
|
// Local binding to a function name.
|
|
60
52
|
binding: Schema.optional(Schema.String),
|
|
61
53
|
}).pipe(
|
|
@@ -65,4 +57,6 @@ export const FunctionType = Schema.Struct({
|
|
|
65
57
|
}),
|
|
66
58
|
LabelAnnotation.set(['name']),
|
|
67
59
|
);
|
|
68
|
-
export interface
|
|
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)) });
|