@dxos/functions 0.8.2-main.12df754 → 0.8.2-main.30e4dbb
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/bundler/index.mjs +0 -3
- package/dist/lib/browser/bundler/index.mjs.map +1 -1
- package/dist/lib/browser/edge/index.mjs +62 -7
- package/dist/lib/browser/edge/index.mjs.map +4 -4
- package/dist/lib/browser/index.mjs +370 -102
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/bundler/index.cjs +0 -1
- package/dist/lib/node/bundler/index.cjs.map +1 -1
- package/dist/lib/node/edge/index.cjs +64 -5
- package/dist/lib/node/edge/index.cjs.map +4 -4
- package/dist/lib/node/index.cjs +382 -94
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/bundler/index.mjs +0 -1
- package/dist/lib/node-esm/bundler/index.mjs.map +1 -1
- package/dist/lib/node-esm/edge/index.mjs +63 -6
- package/dist/lib/node-esm/edge/index.mjs.map +4 -4
- package/dist/lib/node-esm/index.mjs +370 -100
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/bundler/bundler.d.ts.map +1 -1
- package/dist/types/src/edge/functions.d.ts +2 -3
- package/dist/types/src/edge/functions.d.ts.map +1 -1
- package/dist/types/src/edge/index.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +15 -64
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/schema.d.ts +57 -0
- package/dist/types/src/schema.d.ts.map +1 -0
- package/dist/types/src/trace.d.ts +148 -0
- package/dist/types/src/trace.d.ts.map +1 -0
- package/dist/types/src/translations.d.ts +2 -1
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +407 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/src/{types/url.d.ts → url.d.ts} +1 -1
- package/dist/types/src/url.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +20 -36
- package/src/edge/functions.ts +2 -4
- package/src/edge/index.ts +4 -0
- package/src/handler.ts +20 -122
- package/src/index.ts +4 -5
- package/src/schema.ts +53 -0
- package/src/{types/trace.ts → trace.ts} +33 -31
- package/src/translations.ts +1 -1
- package/src/types.ts +210 -0
- package/src/{types/url.ts → url.ts} +1 -1
- package/dist/lib/browser/chunk-2YE6S7XY.mjs +0 -360
- package/dist/lib/browser/chunk-2YE6S7XY.mjs.map +0 -7
- package/dist/lib/browser/chunk-7CHDHCV3.mjs +0 -482
- package/dist/lib/browser/chunk-7CHDHCV3.mjs.map +0 -7
- package/dist/lib/browser/chunk-LT4LR4VU.mjs +0 -72
- package/dist/lib/browser/chunk-LT4LR4VU.mjs.map +0 -7
- package/dist/lib/browser/chunk-XRCXIG74.mjs +0 -12
- package/dist/lib/browser/chunk-XRCXIG74.mjs.map +0 -7
- package/dist/lib/browser/testing/index.mjs +0 -670
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/browser/types/index.mjs +0 -51
- package/dist/lib/browser/types/index.mjs.map +0 -7
- package/dist/lib/node/chunk-FBIUZ7SD.cjs +0 -496
- package/dist/lib/node/chunk-FBIUZ7SD.cjs.map +0 -7
- package/dist/lib/node/chunk-JEQ2X3Z6.cjs +0 -34
- package/dist/lib/node/chunk-JEQ2X3Z6.cjs.map +0 -7
- package/dist/lib/node/chunk-NXZNXVT3.cjs +0 -94
- package/dist/lib/node/chunk-NXZNXVT3.cjs.map +0 -7
- package/dist/lib/node/chunk-SV5NRE5L.cjs +0 -395
- package/dist/lib/node/chunk-SV5NRE5L.cjs.map +0 -7
- package/dist/lib/node/testing/index.cjs +0 -687
- package/dist/lib/node/testing/index.cjs.map +0 -7
- package/dist/lib/node/types/index.cjs +0 -72
- package/dist/lib/node/types/index.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-3XMJFSID.mjs +0 -360
- package/dist/lib/node-esm/chunk-3XMJFSID.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-C6YINTWG.mjs +0 -482
- package/dist/lib/node-esm/chunk-C6YINTWG.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-DHGBFXSZ.mjs +0 -12
- package/dist/lib/node-esm/chunk-DHGBFXSZ.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-O2SXVYU5.mjs +0 -72
- package/dist/lib/node-esm/chunk-O2SXVYU5.mjs.map +0 -7
- package/dist/lib/node-esm/testing/index.mjs +0 -670
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/types/index.mjs +0 -51
- package/dist/lib/node-esm/types/index.mjs.map +0 -7
- package/dist/types/src/browser/index.d.ts +0 -2
- package/dist/types/src/browser/index.d.ts.map +0 -1
- package/dist/types/src/function/function-registry.d.ts +0 -25
- package/dist/types/src/function/function-registry.d.ts.map +0 -1
- package/dist/types/src/function/function-registry.test.d.ts +0 -2
- package/dist/types/src/function/function-registry.test.d.ts.map +0 -1
- package/dist/types/src/function/index.d.ts +0 -2
- package/dist/types/src/function/index.d.ts.map +0 -1
- package/dist/types/src/runtime/dev-server.d.ts +0 -52
- package/dist/types/src/runtime/dev-server.d.ts.map +0 -1
- package/dist/types/src/runtime/dev-server.test.d.ts +0 -2
- package/dist/types/src/runtime/dev-server.test.d.ts.map +0 -1
- package/dist/types/src/runtime/index.d.ts +0 -3
- package/dist/types/src/runtime/index.d.ts.map +0 -1
- package/dist/types/src/runtime/scheduler.d.ts +0 -34
- package/dist/types/src/runtime/scheduler.d.ts.map +0 -1
- package/dist/types/src/runtime/scheduler.test.d.ts +0 -2
- package/dist/types/src/runtime/scheduler.test.d.ts.map +0 -1
- package/dist/types/src/testing/functions-integration.test.d.ts +0 -2
- package/dist/types/src/testing/functions-integration.test.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -5
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/manifest.d.ts +0 -3
- package/dist/types/src/testing/manifest.d.ts.map +0 -1
- package/dist/types/src/testing/plugin-init.d.ts +0 -6
- package/dist/types/src/testing/plugin-init.d.ts.map +0 -1
- package/dist/types/src/testing/setup.d.ts +0 -15
- package/dist/types/src/testing/setup.d.ts.map +0 -1
- package/dist/types/src/testing/test/handler.d.ts +0 -4
- package/dist/types/src/testing/test/handler.d.ts.map +0 -1
- package/dist/types/src/testing/test/index.d.ts +0 -3
- package/dist/types/src/testing/test/index.d.ts.map +0 -1
- package/dist/types/src/testing/types.d.ts +0 -10
- package/dist/types/src/testing/types.d.ts.map +0 -1
- package/dist/types/src/testing/util.d.ts +0 -5
- package/dist/types/src/testing/util.d.ts.map +0 -1
- package/dist/types/src/trigger/index.d.ts +0 -3
- package/dist/types/src/trigger/index.d.ts.map +0 -1
- package/dist/types/src/trigger/trigger-registry.d.ts +0 -38
- package/dist/types/src/trigger/trigger-registry.d.ts.map +0 -1
- package/dist/types/src/trigger/trigger-registry.test.d.ts +0 -2
- package/dist/types/src/trigger/trigger-registry.test.d.ts.map +0 -1
- package/dist/types/src/trigger/type/index.d.ts +0 -3
- package/dist/types/src/trigger/type/index.d.ts.map +0 -1
- package/dist/types/src/trigger/type/subscription-trigger.d.ts +0 -4
- package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +0 -1
- package/dist/types/src/trigger/type/timer-trigger.d.ts +0 -4
- package/dist/types/src/trigger/type/timer-trigger.d.ts.map +0 -1
- package/dist/types/src/trigger/type/webhook-trigger.d.ts +0 -4
- package/dist/types/src/trigger/type/webhook-trigger.d.ts.map +0 -1
- package/dist/types/src/types/index.d.ts +0 -5
- package/dist/types/src/types/index.d.ts.map +0 -1
- package/dist/types/src/types/schema.d.ts +0 -53
- package/dist/types/src/types/schema.d.ts.map +0 -1
- package/dist/types/src/types/trace.d.ts +0 -146
- package/dist/types/src/types/trace.d.ts.map +0 -1
- package/dist/types/src/types/types.d.ts +0 -265
- package/dist/types/src/types/types.d.ts.map +0 -1
- package/dist/types/src/types/url.d.ts.map +0 -1
- package/dist/types/tools/schema.d.ts +0 -2
- package/dist/types/tools/schema.d.ts.map +0 -1
- package/schema/functions.json +0 -211
- package/src/browser/index.ts +0 -5
- package/src/function/function-registry.test.ts +0 -118
- package/src/function/function-registry.ts +0 -104
- package/src/function/index.ts +0 -5
- package/src/runtime/dev-server.test.ts +0 -79
- package/src/runtime/dev-server.ts +0 -240
- package/src/runtime/index.ts +0 -6
- package/src/runtime/scheduler.test.ts +0 -152
- package/src/runtime/scheduler.ts +0 -170
- package/src/testing/functions-integration.test.ts +0 -65
- package/src/testing/index.ts +0 -8
- package/src/testing/manifest.ts +0 -15
- package/src/testing/plugin-init.ts +0 -20
- package/src/testing/setup.ts +0 -109
- package/src/testing/test/handler.ts +0 -15
- package/src/testing/test/index.ts +0 -7
- package/src/testing/types.ts +0 -9
- package/src/testing/util.ts +0 -26
- package/src/trigger/index.ts +0 -6
- package/src/trigger/trigger-registry.test.ts +0 -278
- package/src/trigger/trigger-registry.ts +0 -218
- package/src/trigger/type/index.ts +0 -7
- package/src/trigger/type/subscription-trigger.ts +0 -84
- package/src/trigger/type/timer-trigger.ts +0 -48
- package/src/trigger/type/webhook-trigger.ts +0 -48
- package/src/types/index.ts +0 -8
- package/src/types/schema.ts +0 -46
- package/src/types/types.ts +0 -163
package/src/handler.ts
CHANGED
|
@@ -2,18 +2,15 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Schema
|
|
5
|
+
import { Schema } from 'effect';
|
|
6
6
|
import { type Effect } from 'effect';
|
|
7
7
|
|
|
8
8
|
import { type AIServiceClient } from '@dxos/assistant';
|
|
9
|
-
import { type
|
|
10
|
-
import {
|
|
11
|
-
import type { CoreDatabase, EchoDatabase, ReactiveEchoObject } from '@dxos/echo-db';
|
|
9
|
+
// import { type Space } from '@dxos/client/echo';
|
|
10
|
+
import type { CoreDatabase, EchoDatabase } from '@dxos/echo-db';
|
|
12
11
|
import { type HasId } from '@dxos/echo-schema';
|
|
13
12
|
import { type SpaceId, type DXN } from '@dxos/keys';
|
|
14
|
-
import { log } from '@dxos/log';
|
|
15
13
|
import { type QueryResult } from '@dxos/protocols';
|
|
16
|
-
import { isNonNullable } from '@dxos/util';
|
|
17
14
|
|
|
18
15
|
// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
|
|
19
16
|
// https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
|
|
@@ -23,13 +20,18 @@ import { isNonNullable } from '@dxos/util';
|
|
|
23
20
|
/**
|
|
24
21
|
* Function handler.
|
|
25
22
|
*/
|
|
26
|
-
export type FunctionHandler<TData = {},
|
|
23
|
+
export type FunctionHandler<TData = {}, TOutput = any> = (params: {
|
|
24
|
+
/**
|
|
25
|
+
* Services and context available to the function.
|
|
26
|
+
*/
|
|
27
27
|
context: FunctionContext;
|
|
28
|
-
|
|
28
|
+
|
|
29
29
|
/**
|
|
30
|
-
*
|
|
30
|
+
* Data passed as the input to the function.
|
|
31
|
+
* Must match the function's input schema.
|
|
32
|
+
* This will be the payload from the trigger or other data passed into the function in a workflow.
|
|
31
33
|
*/
|
|
32
|
-
|
|
34
|
+
data: TData;
|
|
33
35
|
}) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any>;
|
|
34
36
|
|
|
35
37
|
/**
|
|
@@ -44,17 +46,6 @@ export interface FunctionContext {
|
|
|
44
46
|
space: SpaceAPI | undefined;
|
|
45
47
|
|
|
46
48
|
ai: AIServiceClient;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* @deprecated
|
|
50
|
-
*/
|
|
51
|
-
// TODO(burdon): Limit access to individual space.
|
|
52
|
-
client: Client;
|
|
53
|
-
/**
|
|
54
|
-
* @deprecated
|
|
55
|
-
*/
|
|
56
|
-
// TODO(burdon): Replace with storage service abstraction.
|
|
57
|
-
dataDir?: string;
|
|
58
49
|
}
|
|
59
50
|
|
|
60
51
|
export interface FunctionContextAi {
|
|
@@ -62,28 +53,6 @@ export interface FunctionContextAi {
|
|
|
62
53
|
run(model: string, inputs: any, options?: any): Promise<any>;
|
|
63
54
|
}
|
|
64
55
|
|
|
65
|
-
/**
|
|
66
|
-
* Event payload.
|
|
67
|
-
*/
|
|
68
|
-
// TODO(dmaretskyi): Update type definitions to match the actual payload.
|
|
69
|
-
export type FunctionEvent<TData = {}, TMeta = {}> = {
|
|
70
|
-
data: FunctionEventMeta<TMeta> & TData;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Metadata from trigger.
|
|
75
|
-
*/
|
|
76
|
-
export type FunctionEventMeta<TMeta = {}> = {
|
|
77
|
-
meta: TMeta;
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Function response.
|
|
82
|
-
*/
|
|
83
|
-
export type FunctionResponse = {
|
|
84
|
-
status(code: number): FunctionResponse;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
56
|
//
|
|
88
57
|
// API.
|
|
89
58
|
//
|
|
@@ -109,28 +78,21 @@ export interface SpaceAPI {
|
|
|
109
78
|
get queues(): QueuesAPI;
|
|
110
79
|
}
|
|
111
80
|
|
|
112
|
-
// TODO(wittjosiah):
|
|
81
|
+
// TODO(wittjosiah): Queues are incompatible.
|
|
113
82
|
const __assertFunctionSpaceIsCompatibleWithTheClientSpace = () => {
|
|
114
83
|
// const _: SpaceAPI = {} as Space;
|
|
115
84
|
};
|
|
116
85
|
|
|
117
|
-
export type FunctionDefinition = {
|
|
118
|
-
description?: string;
|
|
119
|
-
inputSchema: S.Schema.AnyNoContext;
|
|
120
|
-
outputSchema?: S.Schema.AnyNoContext;
|
|
121
|
-
handler: FunctionHandler<any>;
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
export type DefineFunctionParams<T, O = any> = {
|
|
86
|
+
export type FunctionDefinition<T = {}, O = any> = {
|
|
125
87
|
description?: string;
|
|
126
|
-
inputSchema:
|
|
127
|
-
outputSchema?:
|
|
128
|
-
handler: FunctionHandler<T,
|
|
88
|
+
inputSchema: Schema.Schema<T, any>;
|
|
89
|
+
outputSchema?: Schema.Schema<O, any>;
|
|
90
|
+
handler: FunctionHandler<T, O>;
|
|
129
91
|
};
|
|
130
92
|
|
|
131
93
|
// TODO(dmaretskyi): Bind input type to function handler.
|
|
132
|
-
export const defineFunction = <T, O>(params:
|
|
133
|
-
if (!
|
|
94
|
+
export const defineFunction = <T, O>(params: FunctionDefinition<T, O>): FunctionDefinition<T, O> => {
|
|
95
|
+
if (!Schema.isSchema(params.inputSchema)) {
|
|
134
96
|
throw new Error('Input schema must be a valid schema');
|
|
135
97
|
}
|
|
136
98
|
if (typeof params.handler !== 'function') {
|
|
@@ -140,71 +102,7 @@ export const defineFunction = <T, O>(params: DefineFunctionParams<T, O>): Functi
|
|
|
140
102
|
return {
|
|
141
103
|
description: params.description,
|
|
142
104
|
inputSchema: params.inputSchema,
|
|
143
|
-
outputSchema: params.outputSchema ??
|
|
105
|
+
outputSchema: params.outputSchema ?? Schema.Any,
|
|
144
106
|
handler: params.handler,
|
|
145
107
|
};
|
|
146
108
|
};
|
|
147
|
-
|
|
148
|
-
//
|
|
149
|
-
// Subscription utils.
|
|
150
|
-
//
|
|
151
|
-
|
|
152
|
-
export type RawSubscriptionData = {
|
|
153
|
-
spaceKey?: string;
|
|
154
|
-
objects?: string[];
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
export type SubscriptionData = {
|
|
158
|
-
space?: Space;
|
|
159
|
-
objects?: ReactiveEchoObject<any>[];
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Handler wrapper for subscription events; extracts space and objects.
|
|
164
|
-
*
|
|
165
|
-
* To test:
|
|
166
|
-
* ```
|
|
167
|
-
* curl -s -X POST -H "Content-Type: application/json" --data '{"space": "0446...1cbb"}' http://localhost:7100/dev/email-extractor
|
|
168
|
-
* ```
|
|
169
|
-
*
|
|
170
|
-
* NOTE: Get space key from devtools or `dx space list --json`
|
|
171
|
-
*/
|
|
172
|
-
// TODO(burdon): Evolve into plugin definition like Composer.
|
|
173
|
-
export const subscriptionHandler = <TMeta>(
|
|
174
|
-
handler: FunctionHandler<SubscriptionData, TMeta>,
|
|
175
|
-
types?: S.Schema.AnyNoContext[],
|
|
176
|
-
): FunctionHandler<RawSubscriptionData, TMeta> => {
|
|
177
|
-
return async ({ event: { data }, context, response, ...rest }) => {
|
|
178
|
-
const { client } = context;
|
|
179
|
-
const space = data.spaceKey ? client.spaces.get(PublicKey.from(data.spaceKey)) : undefined;
|
|
180
|
-
if (!space) {
|
|
181
|
-
log.error('Invalid space');
|
|
182
|
-
return response.status(500);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
registerTypes(space, types);
|
|
186
|
-
const objects = space
|
|
187
|
-
? data.objects
|
|
188
|
-
?.map<ReactiveEchoObject<any> | undefined>((id) => space!.db.getObjectById(id))
|
|
189
|
-
.filter(isNonNullable)
|
|
190
|
-
: [];
|
|
191
|
-
|
|
192
|
-
if (!!data.spaceKey && !space) {
|
|
193
|
-
log.warn('invalid space', { data });
|
|
194
|
-
} else {
|
|
195
|
-
log.info('handler', { space: space?.key.truncate(), objects: objects?.length });
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
return handler({ event: { data: { ...data, space, objects } }, context, response, ...rest });
|
|
199
|
-
};
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
// TODO(burdon): Evolve types as part of function metadata.
|
|
203
|
-
const registerTypes = (space: Space, types: S.Schema.AnyNoContext[] = []) => {
|
|
204
|
-
const registry = space.db.graph.schemaRegistry;
|
|
205
|
-
for (const type of types) {
|
|
206
|
-
if (!registry.hasSchema(type)) {
|
|
207
|
-
registry.addSchema([type]);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
};
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
//
|
|
2
|
-
// Copyright
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
export * from './edge';
|
|
6
|
-
export * from './function';
|
|
7
5
|
export * from './handler';
|
|
8
|
-
|
|
9
|
-
export * from './
|
|
6
|
+
export * from './schema';
|
|
7
|
+
export * from './trace';
|
|
10
8
|
export * from './types';
|
|
9
|
+
export * from './url';
|
package/src/schema.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Schema } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { EchoObject, JsonSchemaType, LabelAnnotationId, Ref, TypedObject } from '@dxos/echo-schema';
|
|
8
|
+
import { DataType } from '@dxos/schema';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Source script.
|
|
12
|
+
*/
|
|
13
|
+
export const ScriptType = Schema.Struct({
|
|
14
|
+
name: Schema.optional(Schema.String),
|
|
15
|
+
description: Schema.optional(Schema.String),
|
|
16
|
+
// TODO(burdon): Change to hash of deployed content.
|
|
17
|
+
// Whether source has changed since last deploy.
|
|
18
|
+
changed: Schema.optional(Schema.Boolean),
|
|
19
|
+
source: Ref(DataType.Text),
|
|
20
|
+
})
|
|
21
|
+
.annotations({ [LabelAnnotationId]: 'name' })
|
|
22
|
+
.pipe(
|
|
23
|
+
EchoObject({
|
|
24
|
+
typename: 'dxos.org/type/Script',
|
|
25
|
+
version: '0.1.0',
|
|
26
|
+
}),
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
export type ScriptType = Schema.Schema.Type<typeof ScriptType>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Function deployment.
|
|
33
|
+
*/
|
|
34
|
+
export class FunctionType extends TypedObject({
|
|
35
|
+
typename: 'dxos.org/type/Function',
|
|
36
|
+
version: '0.1.0',
|
|
37
|
+
})({
|
|
38
|
+
// TODO(burdon): Rename to id/uri?
|
|
39
|
+
name: Schema.NonEmptyString,
|
|
40
|
+
version: Schema.String,
|
|
41
|
+
|
|
42
|
+
description: Schema.optional(Schema.String),
|
|
43
|
+
|
|
44
|
+
// Reference to a source script if it exists within ECHO.
|
|
45
|
+
// TODO(burdon): Don't ref ScriptType directly (core).
|
|
46
|
+
source: Schema.optional(Ref(ScriptType)),
|
|
47
|
+
|
|
48
|
+
inputSchema: Schema.optional(JsonSchemaType),
|
|
49
|
+
outputSchema: Schema.optional(JsonSchemaType),
|
|
50
|
+
|
|
51
|
+
// Local binding to a function name.
|
|
52
|
+
binding: Schema.optional(Schema.String),
|
|
53
|
+
}) {}
|
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { Schema } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { EchoObject, Expando, ObjectId, Ref } from '@dxos/echo-schema';
|
|
6
8
|
import { log } from '@dxos/log';
|
|
7
9
|
|
|
8
10
|
import { FunctionTrigger, type FunctionTriggerType } from './types';
|
|
@@ -19,20 +21,20 @@ export enum InvocationTraceEventType {
|
|
|
19
21
|
END = 'end',
|
|
20
22
|
}
|
|
21
23
|
|
|
22
|
-
export const TraceEventException =
|
|
23
|
-
timestampMs:
|
|
24
|
-
message:
|
|
25
|
-
name:
|
|
26
|
-
stack:
|
|
24
|
+
export const TraceEventException = Schema.Struct({
|
|
25
|
+
timestampMs: Schema.Number,
|
|
26
|
+
message: Schema.String,
|
|
27
|
+
name: Schema.String,
|
|
28
|
+
stack: Schema.optional(Schema.String),
|
|
27
29
|
});
|
|
28
|
-
export type TraceEventException =
|
|
30
|
+
export type TraceEventException = Schema.Schema.Type<typeof TraceEventException>;
|
|
29
31
|
|
|
30
|
-
export const InvocationTraceStartEvent =
|
|
32
|
+
export const InvocationTraceStartEvent = Schema.Struct({
|
|
31
33
|
/**
|
|
32
34
|
* Queue message id.
|
|
33
35
|
*/
|
|
34
36
|
id: ObjectId,
|
|
35
|
-
type:
|
|
37
|
+
type: Schema.Literal(InvocationTraceEventType.START),
|
|
36
38
|
/**
|
|
37
39
|
* Invocation id, the same for invocation start and end events.
|
|
38
40
|
*/
|
|
@@ -40,12 +42,12 @@ export const InvocationTraceStartEvent = S.Struct({
|
|
|
40
42
|
/**
|
|
41
43
|
* Event generation time.
|
|
42
44
|
*/
|
|
43
|
-
timestampMs:
|
|
45
|
+
timestampMs: Schema.Number,
|
|
44
46
|
/**
|
|
45
47
|
* Data passed to function / workflow as an argument.
|
|
46
48
|
*/
|
|
47
49
|
// TODO(burdon): Input schema?
|
|
48
|
-
input:
|
|
50
|
+
input: Schema.Object,
|
|
49
51
|
/**
|
|
50
52
|
* Queue DXN for function/workflow invocation events.
|
|
51
53
|
*/
|
|
@@ -58,17 +60,17 @@ export const InvocationTraceStartEvent = S.Struct({
|
|
|
58
60
|
/**
|
|
59
61
|
* Present for automatic invocations.
|
|
60
62
|
*/
|
|
61
|
-
trigger:
|
|
63
|
+
trigger: Schema.optional(Ref(FunctionTrigger)),
|
|
62
64
|
}).pipe(EchoObject({ typename: 'dxos.org/type/InvocationTraceStart', version: '0.1.0' }));
|
|
63
65
|
|
|
64
|
-
export type InvocationTraceStartEvent =
|
|
66
|
+
export type InvocationTraceStartEvent = Schema.Schema.Type<typeof InvocationTraceStartEvent>;
|
|
65
67
|
|
|
66
|
-
export const InvocationTraceEndEvent =
|
|
68
|
+
export const InvocationTraceEndEvent = Schema.Struct({
|
|
67
69
|
/**
|
|
68
70
|
* Trace event id.
|
|
69
71
|
*/
|
|
70
72
|
id: ObjectId,
|
|
71
|
-
type:
|
|
73
|
+
type: Schema.Literal(InvocationTraceEventType.END),
|
|
72
74
|
/**
|
|
73
75
|
* Invocation id, will be the same for invocation start and end.
|
|
74
76
|
*/
|
|
@@ -77,36 +79,36 @@ export const InvocationTraceEndEvent = S.Struct({
|
|
|
77
79
|
* Event generation time.
|
|
78
80
|
*/
|
|
79
81
|
// TODO(burdon): Remove ms suffix.
|
|
80
|
-
timestampMs:
|
|
81
|
-
outcome:
|
|
82
|
-
exception:
|
|
82
|
+
timestampMs: Schema.Number,
|
|
83
|
+
outcome: Schema.Enums(InvocationOutcome),
|
|
84
|
+
exception: Schema.optional(TraceEventException),
|
|
83
85
|
}).pipe(EchoObject({ typename: 'dxos.org/type/InvocationTraceEnd', version: '0.1.0' }));
|
|
84
86
|
|
|
85
|
-
export type InvocationTraceEndEvent =
|
|
87
|
+
export type InvocationTraceEndEvent = Schema.Schema.Type<typeof InvocationTraceEndEvent>;
|
|
86
88
|
|
|
87
89
|
export type InvocationTraceEvent = InvocationTraceStartEvent | InvocationTraceEndEvent;
|
|
88
90
|
|
|
89
|
-
export const TraceEventLog =
|
|
90
|
-
timestampMs:
|
|
91
|
-
level:
|
|
92
|
-
message:
|
|
93
|
-
context:
|
|
91
|
+
export const TraceEventLog = Schema.Struct({
|
|
92
|
+
timestampMs: Schema.Number,
|
|
93
|
+
level: Schema.String,
|
|
94
|
+
message: Schema.String,
|
|
95
|
+
context: Schema.optional(Schema.Object),
|
|
94
96
|
});
|
|
95
97
|
|
|
96
|
-
export const TraceEvent =
|
|
98
|
+
export const TraceEvent = Schema.Struct({
|
|
97
99
|
id: ObjectId,
|
|
98
100
|
// TODO(burdon): Need enum/numeric result (not string).
|
|
99
|
-
outcome:
|
|
100
|
-
truncated:
|
|
101
|
+
outcome: Schema.String,
|
|
102
|
+
truncated: Schema.Boolean,
|
|
101
103
|
/**
|
|
102
104
|
* Time when the event was persisted.
|
|
103
105
|
*/
|
|
104
|
-
ingestionTimestampMs:
|
|
105
|
-
logs:
|
|
106
|
-
exceptions:
|
|
106
|
+
ingestionTimestampMs: Schema.Number,
|
|
107
|
+
logs: Schema.Array(TraceEventLog),
|
|
108
|
+
exceptions: Schema.Array(TraceEventException),
|
|
107
109
|
}).pipe(EchoObject({ typename: 'dxos.org/type/TraceEvent', version: '0.1.0' }));
|
|
108
110
|
|
|
109
|
-
export type TraceEvent =
|
|
111
|
+
export type TraceEvent = Schema.Schema.Type<typeof TraceEvent>;
|
|
110
112
|
|
|
111
113
|
/**
|
|
112
114
|
* Deprecated InvocationTrace event format.
|
package/src/translations.ts
CHANGED
package/src/types.ts
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Schema, SchemaAST } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { Expando, OptionsAnnotationId, TypedObject, DXN, Ref, RawObject } from '@dxos/echo-schema';
|
|
8
|
+
|
|
9
|
+
import { FunctionType } from './schema';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Type discriminator for TriggerType.
|
|
13
|
+
* Every spec has a type field of type TriggerKind that we can use to understand which type we're working with.
|
|
14
|
+
* https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions
|
|
15
|
+
*/
|
|
16
|
+
export enum TriggerKind {
|
|
17
|
+
Timer = 'timer',
|
|
18
|
+
Webhook = 'webhook',
|
|
19
|
+
Subscription = 'subscription',
|
|
20
|
+
Email = 'email',
|
|
21
|
+
Queue = 'queue',
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const kindLiteralAnnotations = { [SchemaAST.TitleAnnotationId]: 'Kind' };
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Cron timer.
|
|
28
|
+
*/
|
|
29
|
+
const TimerTriggerSchema = Schema.Struct({
|
|
30
|
+
kind: Schema.Literal(TriggerKind.Timer).annotations(kindLiteralAnnotations),
|
|
31
|
+
cron: Schema.String.annotations({
|
|
32
|
+
[SchemaAST.TitleAnnotationId]: 'Cron',
|
|
33
|
+
[SchemaAST.ExamplesAnnotationId]: ['0 0 * * *'],
|
|
34
|
+
}),
|
|
35
|
+
}).pipe(Schema.mutable);
|
|
36
|
+
export type TimerTrigger = Schema.Schema.Type<typeof TimerTriggerSchema>;
|
|
37
|
+
|
|
38
|
+
const EmailTriggerSchema = Schema.Struct({
|
|
39
|
+
kind: Schema.Literal(TriggerKind.Email).annotations(kindLiteralAnnotations),
|
|
40
|
+
}).pipe(Schema.mutable);
|
|
41
|
+
export type EmailTrigger = Schema.Schema.Type<typeof EmailTriggerSchema>;
|
|
42
|
+
|
|
43
|
+
const QueueTriggerSchema = Schema.Struct({
|
|
44
|
+
kind: Schema.Literal(TriggerKind.Queue).annotations(kindLiteralAnnotations),
|
|
45
|
+
queue: DXN,
|
|
46
|
+
}).pipe(Schema.mutable);
|
|
47
|
+
export type QueueTrigger = Schema.Schema.Type<typeof QueueTriggerSchema>;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Webhook.
|
|
51
|
+
*/
|
|
52
|
+
const WebhookTriggerSchema = Schema.Struct({
|
|
53
|
+
kind: Schema.Literal(TriggerKind.Webhook).annotations(kindLiteralAnnotations),
|
|
54
|
+
method: Schema.optional(
|
|
55
|
+
Schema.String.annotations({
|
|
56
|
+
[SchemaAST.TitleAnnotationId]: 'Method',
|
|
57
|
+
[OptionsAnnotationId]: ['GET', 'POST'],
|
|
58
|
+
}),
|
|
59
|
+
),
|
|
60
|
+
port: Schema.optional(
|
|
61
|
+
Schema.Number.annotations({
|
|
62
|
+
[SchemaAST.TitleAnnotationId]: 'Port',
|
|
63
|
+
}),
|
|
64
|
+
),
|
|
65
|
+
}).pipe(Schema.mutable);
|
|
66
|
+
export type WebhookTrigger = Schema.Schema.Type<typeof WebhookTriggerSchema>;
|
|
67
|
+
|
|
68
|
+
// TODO(burdon): Use ECHO definition (from https://github.com/dxos/dxos/pull/8233).
|
|
69
|
+
const QuerySchema = Schema.Struct({
|
|
70
|
+
type: Schema.optional(Schema.String.annotations({ [SchemaAST.TitleAnnotationId]: 'Type' })),
|
|
71
|
+
props: Schema.optional(Schema.Record({ key: Schema.String, value: Schema.Any })),
|
|
72
|
+
}).annotations({ [SchemaAST.TitleAnnotationId]: 'Query' });
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Subscription.
|
|
76
|
+
*/
|
|
77
|
+
const SubscriptionTriggerSchema = Schema.Struct({
|
|
78
|
+
kind: Schema.Literal(TriggerKind.Subscription).annotations(kindLiteralAnnotations),
|
|
79
|
+
// TODO(burdon): Define query DSL (from ECHO). Reconcile with Table.Query.
|
|
80
|
+
filter: QuerySchema,
|
|
81
|
+
options: Schema.optional(
|
|
82
|
+
Schema.Struct({
|
|
83
|
+
// Watch changes to object (not just creation).
|
|
84
|
+
deep: Schema.optional(Schema.Boolean.annotations({ [SchemaAST.TitleAnnotationId]: 'Nested' })),
|
|
85
|
+
// Debounce changes (delay in ms).
|
|
86
|
+
delay: Schema.optional(Schema.Number.annotations({ [SchemaAST.TitleAnnotationId]: 'Delay' })),
|
|
87
|
+
}).annotations({ [SchemaAST.TitleAnnotationId]: 'Options' }),
|
|
88
|
+
),
|
|
89
|
+
}).pipe(Schema.mutable);
|
|
90
|
+
export type SubscriptionTrigger = Schema.Schema.Type<typeof SubscriptionTriggerSchema>;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Trigger schema (discriminated union).
|
|
94
|
+
*/
|
|
95
|
+
export const TriggerSchema = Schema.Union(
|
|
96
|
+
TimerTriggerSchema,
|
|
97
|
+
WebhookTriggerSchema,
|
|
98
|
+
SubscriptionTriggerSchema,
|
|
99
|
+
EmailTriggerSchema,
|
|
100
|
+
QueueTriggerSchema,
|
|
101
|
+
).annotations({
|
|
102
|
+
[SchemaAST.TitleAnnotationId]: 'Trigger',
|
|
103
|
+
});
|
|
104
|
+
export type TriggerType = Schema.Schema.Type<typeof TriggerSchema>;
|
|
105
|
+
|
|
106
|
+
export type EventType =
|
|
107
|
+
| EmailTriggerOutput
|
|
108
|
+
| WebhookTriggerOutput
|
|
109
|
+
| QueueTriggerOutput
|
|
110
|
+
| SubscriptionTriggerOutput
|
|
111
|
+
| TimerTriggerOutput;
|
|
112
|
+
|
|
113
|
+
// TODO(burdon): Reuse trigger schema from @dxos/functions (TriggerType).
|
|
114
|
+
export const EmailTriggerOutput = Schema.mutable(
|
|
115
|
+
Schema.Struct({
|
|
116
|
+
from: Schema.String,
|
|
117
|
+
to: Schema.String,
|
|
118
|
+
subject: Schema.String,
|
|
119
|
+
created: Schema.String,
|
|
120
|
+
body: Schema.String,
|
|
121
|
+
}),
|
|
122
|
+
);
|
|
123
|
+
export type EmailTriggerOutput = Schema.Schema.Type<typeof EmailTriggerOutput>;
|
|
124
|
+
|
|
125
|
+
export const WebhookTriggerOutput = Schema.mutable(
|
|
126
|
+
Schema.Struct({
|
|
127
|
+
url: Schema.String,
|
|
128
|
+
method: Schema.Literal('GET', 'POST'),
|
|
129
|
+
headers: Schema.Record({ key: Schema.String, value: Schema.String }),
|
|
130
|
+
bodyText: Schema.String,
|
|
131
|
+
}),
|
|
132
|
+
);
|
|
133
|
+
export type WebhookTriggerOutput = Schema.Schema.Type<typeof WebhookTriggerOutput>;
|
|
134
|
+
|
|
135
|
+
export const QueueTriggerOutput = Schema.mutable(
|
|
136
|
+
Schema.Struct({
|
|
137
|
+
queue: DXN,
|
|
138
|
+
item: Schema.Any,
|
|
139
|
+
cursor: Schema.String,
|
|
140
|
+
}),
|
|
141
|
+
);
|
|
142
|
+
export type QueueTriggerOutput = Schema.Schema.Type<typeof QueueTriggerOutput>;
|
|
143
|
+
|
|
144
|
+
export const SubscriptionTriggerOutput = Schema.mutable(
|
|
145
|
+
Schema.Struct({ type: Schema.String, changedObjectId: Schema.String }),
|
|
146
|
+
);
|
|
147
|
+
export type SubscriptionTriggerOutput = Schema.Schema.Type<typeof SubscriptionTriggerOutput>;
|
|
148
|
+
|
|
149
|
+
export const TimerTriggerOutput = Schema.mutable(Schema.Struct({ tick: Schema.Number }));
|
|
150
|
+
export type TimerTriggerOutput = Schema.Schema.Type<typeof TimerTriggerOutput>;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Function trigger.
|
|
154
|
+
* Function is invoked with the `payload` passed as input data.
|
|
155
|
+
* The event that triggers the function is available in the function context.
|
|
156
|
+
*/
|
|
157
|
+
export const FunctionTriggerSchema = Schema.Struct({
|
|
158
|
+
/**
|
|
159
|
+
* Function or workflow to invoke.
|
|
160
|
+
*/
|
|
161
|
+
// TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
|
|
162
|
+
function: Schema.optional(Ref(Expando).annotations({ [SchemaAST.TitleAnnotationId]: 'Function' })),
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Only used for workflowSchema.
|
|
166
|
+
* Specifies the input node in the circuit.
|
|
167
|
+
* @deprecated Remove and enforce a single input node in all compute graphSchema.
|
|
168
|
+
*/
|
|
169
|
+
inputNodeId: Schema.optional(Schema.String.annotations({ [SchemaAST.TitleAnnotationId]: 'Input Node ID' })),
|
|
170
|
+
|
|
171
|
+
enabled: Schema.optional(Schema.Boolean.annotations({ [SchemaAST.TitleAnnotationId]: 'Enabled' })),
|
|
172
|
+
|
|
173
|
+
spec: Schema.optional(TriggerSchema),
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Passed as the input data to the function.
|
|
177
|
+
* Must match the function's input schema.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* {
|
|
181
|
+
* item: '{{$.trigger.event}}',
|
|
182
|
+
* instructions: 'Summarize and perform entity-extraction'
|
|
183
|
+
* mailbox: { '/': 'dxn:echo:AAA:ZZZ' }
|
|
184
|
+
* }
|
|
185
|
+
*/
|
|
186
|
+
input: Schema.optional(Schema.mutable(Schema.Record({ key: Schema.String, value: Schema.Any }))),
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
export type FunctionTriggerType = Schema.Schema.Type<typeof FunctionTriggerSchema>;
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Function trigger.
|
|
193
|
+
*/
|
|
194
|
+
export class FunctionTrigger extends TypedObject({
|
|
195
|
+
typename: 'dxos.org/type/FunctionTrigger',
|
|
196
|
+
version: '0.2.0',
|
|
197
|
+
})(FunctionTriggerSchema.fields) {}
|
|
198
|
+
|
|
199
|
+
// TODO(wittjosiah): Remove?
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Function manifest file.
|
|
203
|
+
*/
|
|
204
|
+
export const FunctionManifestSchema = Schema.Struct({
|
|
205
|
+
functions: Schema.optional(Schema.mutable(Schema.Array(RawObject(FunctionType)))),
|
|
206
|
+
triggers: Schema.optional(Schema.mutable(Schema.Array(RawObject(FunctionTrigger)))),
|
|
207
|
+
});
|
|
208
|
+
export type FunctionManifest = Schema.Schema.Type<typeof FunctionManifestSchema>;
|
|
209
|
+
|
|
210
|
+
export const FUNCTION_TYPES = [FunctionType, FunctionTrigger];
|
|
@@ -32,7 +32,7 @@ export const setUserFunctionUrlInMetadata = (meta: ObjectMeta, functionUrl: stri
|
|
|
32
32
|
/**
|
|
33
33
|
* NOTE: functionId is backend ID, not ECHO object id.
|
|
34
34
|
*/
|
|
35
|
-
export const makeFunctionUrl = (
|
|
35
|
+
export const makeFunctionUrl = (fn: { functionId: string }) => `/${fn.functionId}`;
|
|
36
36
|
|
|
37
37
|
export const getInvocationUrl = (functionUrl: string, edgeUrl: string, options: InvocationOptions = {}) => {
|
|
38
38
|
const baseUrl = new URL('functions/', edgeUrl);
|