@dxos/functions 0.7.5-main.499c70c → 0.7.5-main.5ae2ba8

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.
Files changed (77) hide show
  1. package/dist/lib/browser/bundler/index.mjs +239 -0
  2. package/dist/lib/browser/bundler/index.mjs.map +7 -0
  3. package/dist/lib/browser/{chunk-PKOLPTSC.mjs → chunk-IXJNRWJP.mjs} +53 -17
  4. package/dist/lib/browser/chunk-IXJNRWJP.mjs.map +7 -0
  5. package/dist/lib/browser/{chunk-BCNGUHHW.mjs → chunk-XLM3CJXO.mjs} +2 -2
  6. package/dist/lib/browser/{chunk-B3FGASHJ.mjs → chunk-YJEIETRB.mjs} +6 -3
  7. package/dist/lib/browser/{chunk-B3FGASHJ.mjs.map → chunk-YJEIETRB.mjs.map} +3 -3
  8. package/dist/lib/browser/edge/index.mjs +1 -1
  9. package/dist/lib/browser/index.mjs +24 -10
  10. package/dist/lib/browser/index.mjs.map +3 -3
  11. package/dist/lib/browser/meta.json +1 -1
  12. package/dist/lib/browser/testing/index.mjs +2 -2
  13. package/dist/lib/browser/types/index.mjs +1 -3
  14. package/dist/lib/node/bundler/index.cjs +261 -0
  15. package/dist/lib/node/bundler/index.cjs.map +7 -0
  16. package/dist/lib/node/{chunk-RYEY2RR2.cjs → chunk-MGCKR53Q.cjs} +54 -21
  17. package/dist/lib/node/chunk-MGCKR53Q.cjs.map +7 -0
  18. package/dist/lib/node/{chunk-ZDG466QT.cjs → chunk-MZMHE4DC.cjs} +9 -6
  19. package/dist/lib/node/{chunk-ZDG466QT.cjs.map → chunk-MZMHE4DC.cjs.map} +3 -3
  20. package/dist/lib/node/{chunk-UHWSNERM.cjs → chunk-TLSUCABH.cjs} +15 -15
  21. package/dist/lib/node/edge/index.cjs +8 -8
  22. package/dist/lib/node/edge/index.cjs.map +1 -1
  23. package/dist/lib/node/index.cjs +44 -29
  24. package/dist/lib/node/index.cjs.map +3 -3
  25. package/dist/lib/node/meta.json +1 -1
  26. package/dist/lib/node/testing/index.cjs +7 -7
  27. package/dist/lib/node/types/index.cjs +10 -12
  28. package/dist/lib/node/types/index.cjs.map +2 -2
  29. package/dist/lib/node-esm/bundler/index.mjs +239 -0
  30. package/dist/lib/node-esm/bundler/index.mjs.map +7 -0
  31. package/dist/lib/node-esm/{chunk-TPGS4PIH.mjs → chunk-CKTHJQ7M.mjs} +2 -2
  32. package/dist/lib/node-esm/{chunk-2UWB6JYW.mjs → chunk-FCZGNRNF.mjs} +53 -17
  33. package/dist/lib/node-esm/chunk-FCZGNRNF.mjs.map +7 -0
  34. package/dist/lib/node-esm/{chunk-CN74WOTT.mjs → chunk-T2KBXTMR.mjs} +6 -3
  35. package/dist/lib/node-esm/{chunk-CN74WOTT.mjs.map → chunk-T2KBXTMR.mjs.map} +3 -3
  36. package/dist/lib/node-esm/edge/index.mjs +1 -1
  37. package/dist/lib/node-esm/index.mjs +24 -10
  38. package/dist/lib/node-esm/index.mjs.map +3 -3
  39. package/dist/lib/node-esm/meta.json +1 -1
  40. package/dist/lib/node-esm/testing/index.mjs +2 -2
  41. package/dist/lib/node-esm/types/index.mjs +1 -3
  42. package/dist/types/src/bundler/bundler.d.ts +51 -0
  43. package/dist/types/src/bundler/bundler.d.ts.map +1 -0
  44. package/dist/types/src/bundler/bundler.test.d.ts +2 -0
  45. package/dist/types/src/bundler/bundler.test.d.ts.map +1 -0
  46. package/dist/types/src/bundler/index.d.ts +2 -0
  47. package/dist/types/src/bundler/index.d.ts.map +1 -0
  48. package/dist/types/src/edge/functions.d.ts.map +1 -1
  49. package/dist/types/src/handler.d.ts +29 -4
  50. package/dist/types/src/handler.d.ts.map +1 -1
  51. package/dist/types/src/testing/types.d.ts +2 -2
  52. package/dist/types/src/types/index.d.ts +1 -0
  53. package/dist/types/src/types/index.d.ts.map +1 -1
  54. package/dist/types/src/types/schema.d.ts +20 -23
  55. package/dist/types/src/types/schema.d.ts.map +1 -1
  56. package/dist/types/src/types/trace.d.ts +103 -0
  57. package/dist/types/src/types/trace.d.ts.map +1 -0
  58. package/dist/types/src/types/types.d.ts +50 -9
  59. package/dist/types/src/types/types.d.ts.map +1 -1
  60. package/dist/types/tsconfig.tsbuildinfo +1 -1
  61. package/package.json +33 -19
  62. package/src/bundler/bundler.test.ts +59 -0
  63. package/src/bundler/bundler.ts +264 -0
  64. package/src/bundler/index.ts +5 -0
  65. package/src/edge/functions.ts +5 -1
  66. package/src/handler.ts +58 -9
  67. package/src/trigger/trigger-registry.test.ts +3 -1
  68. package/src/types/index.ts +1 -0
  69. package/src/types/schema.ts +4 -6
  70. package/src/types/trace.ts +49 -0
  71. package/src/types/types.ts +19 -2
  72. package/dist/lib/browser/chunk-PKOLPTSC.mjs.map +0 -7
  73. package/dist/lib/node/chunk-RYEY2RR2.cjs.map +0 -7
  74. package/dist/lib/node-esm/chunk-2UWB6JYW.mjs.map +0 -7
  75. /package/dist/lib/browser/{chunk-BCNGUHHW.mjs.map → chunk-XLM3CJXO.mjs.map} +0 -0
  76. /package/dist/lib/node/{chunk-UHWSNERM.cjs.map → chunk-TLSUCABH.cjs.map} +0 -0
  77. /package/dist/lib/node-esm/{chunk-TPGS4PIH.mjs.map → chunk-CKTHJQ7M.mjs.map} +0 -0
@@ -0,0 +1,59 @@
1
+ //
2
+ // Copyright 2022 DXOS.org
3
+ //
4
+
5
+ // @ts-ignore
6
+ import wasmUrl from 'esbuild-wasm/esbuild.wasm?url';
7
+ import { beforeAll, describe, expect, test } from 'vitest';
8
+
9
+ import { isNode } from '@dxos/util';
10
+
11
+ import { Bundler, initializeBundler } from './bundler';
12
+
13
+ describe('Bundler', () => {
14
+ beforeAll(async () => {
15
+ if (!isNode()) {
16
+ await initializeBundler({ wasmUrl });
17
+ }
18
+ });
19
+
20
+ test('Basic', async () => {
21
+ const bundler = new Bundler({ platform: 'node', sandboxedModules: [], remoteModules: {} });
22
+ const result = await bundler.bundle({ source: 'const x = 100' }); // TODO(burdon): Test import.
23
+ expect(result.bundle).to.exist;
24
+ expect(result.error).to.not.exist;
25
+ });
26
+
27
+ test('Import', async () => {
28
+ const bundler = new Bundler({ platform: 'node', sandboxedModules: [], remoteModules: {} });
29
+ const result = await bundler.bundle({
30
+ source: `
31
+ import { Filter } from './runtime.js';
32
+
33
+ const query = Filter.typename('dxos.org/type/Example');
34
+ `,
35
+ });
36
+ expect(result.bundle).to.exist;
37
+ expect(result.error).to.not.exist;
38
+ });
39
+
40
+ // TODO(dmaretskyi): Flaky on CI: https://cloud.nx.app/runs/Hjcifa8Ccq/task/plugin-script%3Atest
41
+ test.skip('HTTPS Import', async () => {
42
+ const bundler = new Bundler({ platform: 'node', sandboxedModules: [], remoteModules: {} });
43
+ const result = await bundler.bundle({
44
+ source: `
45
+ import { invariant } from 'https://esm.sh/@dxos/invariant';
46
+ invariant(true);
47
+ `,
48
+ });
49
+ expect(result.bundle).to.exist;
50
+ expect(result.error).to.not.exist;
51
+ });
52
+
53
+ test('Error', async () => {
54
+ const bundler = new Bundler({ platform: 'node', sandboxedModules: [], remoteModules: {} });
55
+ const result = await bundler.bundle({ source: "import missing from './module'; missing();" });
56
+ expect(result.bundle).to.not.exist;
57
+ expect(result.error).to.exist;
58
+ });
59
+ });
@@ -0,0 +1,264 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { type BuildOptions } from 'esbuild';
6
+ import { build, initialize, type BuildResult, type Plugin } from 'esbuild-wasm';
7
+
8
+ import { subtleCrypto } from '@dxos/crypto';
9
+ import { invariant } from '@dxos/invariant';
10
+ import { log } from '@dxos/log';
11
+
12
+ export type Import = {
13
+ moduleUrl: string;
14
+ defaultImport: boolean;
15
+ namedImports: string[];
16
+ };
17
+
18
+ export type BundleOptions = {
19
+ /**
20
+ * Path to the source file on the local file system.
21
+ * If provided, the path will be used instead of the `source` code.
22
+ */
23
+ path?: string;
24
+
25
+ /**
26
+ * Source code to bundle.
27
+ * Required if `path` is not provided.
28
+ */
29
+ source?: string;
30
+ };
31
+
32
+ export type BundleResult = {
33
+ timestamp: number;
34
+ sourceHash?: Buffer;
35
+ imports?: Import[];
36
+ bundle?: string;
37
+ error?: any;
38
+ };
39
+
40
+ export type BundlerOptions = {
41
+ platform: BuildOptions['platform'];
42
+ sandboxedModules: string[];
43
+ remoteModules: Record<string, string>;
44
+ };
45
+
46
+ let initialized: Promise<void>;
47
+ export const initializeBundler = async (options: { wasmUrl: string }) => {
48
+ await (initialized ??= initialize({
49
+ wasmURL: options.wasmUrl,
50
+ }));
51
+ };
52
+
53
+ /**
54
+ * ESBuild bundler.
55
+ */
56
+ export class Bundler {
57
+ constructor(private readonly _options: BundlerOptions) {}
58
+
59
+ async bundle({ path, source }: BundleOptions): Promise<BundleResult> {
60
+ const { sandboxedModules: providedModules, ...options } = this._options;
61
+
62
+ const createResult = async (result?: Partial<BundleResult>) => {
63
+ return {
64
+ timestamp: Date.now(),
65
+ sourceHash: source ? Buffer.from(await subtleCrypto.digest('SHA-256', Buffer.from(source))) : undefined,
66
+ ...result,
67
+ };
68
+ };
69
+
70
+ if (this._options.platform === 'browser') {
71
+ invariant(initialized, 'Compiler not initialized.');
72
+ await initialized;
73
+ }
74
+
75
+ const imports = source ? analyzeSourceFileImports(source) : [];
76
+
77
+ // https://esbuild.github.io/api/#build
78
+ try {
79
+ const result = await build({
80
+ platform: options.platform,
81
+ conditions: ['workerd', 'browser'],
82
+ metafile: true,
83
+ write: false,
84
+ entryPoints: [path ?? 'memory:main.tsx'],
85
+ bundle: true,
86
+ format: 'esm',
87
+ plugins: [
88
+ {
89
+ name: 'memory',
90
+ setup: (build) => {
91
+ build.onResolve({ filter: /^\.\/runtime\.js$/ }, ({ path }) => {
92
+ return { path, external: true };
93
+ });
94
+
95
+ build.onResolve({ filter: /^dxos:functions$/ }, ({ path }) => {
96
+ return { path: './runtime.js', external: true };
97
+ });
98
+
99
+ build.onResolve({ filter: /^memory:/ }, ({ path }) => {
100
+ return { path: path.split(':')[1], namespace: 'memory' };
101
+ });
102
+
103
+ build.onLoad({ filter: /.*/, namespace: 'memory' }, ({ path }) => {
104
+ if (path === 'main.tsx') {
105
+ return {
106
+ contents: source,
107
+ loader: 'tsx',
108
+ };
109
+ }
110
+ });
111
+
112
+ for (const module of providedModules) {
113
+ build.onResolve({ filter: new RegExp(`^${module}$`) }, ({ path }) => {
114
+ return { path, namespace: 'injected-module' };
115
+ });
116
+ }
117
+
118
+ build.onLoad({ filter: /.*/, namespace: 'injected-module' }, ({ path }) => {
119
+ const namedImports = imports.find((entry) => entry.moduleIdentifier === path)?.namedImports ?? [];
120
+ return {
121
+ contents: `
122
+ const { ${namedImports.join(',')} } = window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path)}];
123
+ export { ${namedImports.join(',')} };
124
+ export default window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path)}].default;
125
+ `,
126
+ loader: 'tsx',
127
+ };
128
+ });
129
+ },
130
+ },
131
+ httpPlugin,
132
+ ],
133
+ });
134
+
135
+ log('compile complete', result.metafile);
136
+
137
+ return await createResult({
138
+ imports: this.analyzeImports(result),
139
+ bundle: result.outputFiles![0].text,
140
+ });
141
+ } catch (err) {
142
+ return await createResult({ error: err });
143
+ }
144
+ }
145
+
146
+ // TODO(dmaretskyi): In the future we can replace the compiler with SWC with plugins running in WASM.
147
+ analyzeImports(result: BuildResult): Import[] {
148
+ invariant(result.outputFiles);
149
+
150
+ // TODO(dmaretskyi): Support import aliases and wildcard imports.
151
+ const parsedImports = allMatches(IMPORT_REGEX, result.outputFiles[0].text);
152
+ return Object.values(result.metafile!.outputs)[0].imports.map((entry): Import => {
153
+ const namedImports: string[] = [];
154
+
155
+ const parsedImport = parsedImports.find((capture) => capture?.[4] === entry.path);
156
+ if (parsedImport?.[2]) {
157
+ NAMED_IMPORTS_REGEX.lastIndex = 0;
158
+ const namedImportsMatch = NAMED_IMPORTS_REGEX.exec(parsedImport[2]);
159
+ if (namedImportsMatch) {
160
+ namedImportsMatch[1].split(',').forEach((importName) => {
161
+ namedImports.push(importName.trim());
162
+ });
163
+ }
164
+ }
165
+
166
+ return {
167
+ moduleUrl: entry.path,
168
+ defaultImport: !!parsedImport?.[1],
169
+ namedImports,
170
+ };
171
+ });
172
+ }
173
+
174
+ analyzeSourceFileImports(code: string) {
175
+ // TODO(dmaretskyi): Support import aliases and wildcard imports.
176
+ const parsedImports = allMatches(IMPORT_REGEX, code);
177
+ return parsedImports.map((capture) => {
178
+ return {
179
+ defaultImportName: capture[1],
180
+ namedImports: capture[2]?.split(',').map((importName) => importName.trim()),
181
+ wildcardImportName: capture[3],
182
+ moduleIdentifier: capture[4],
183
+ quotes: capture[5],
184
+ };
185
+ });
186
+ }
187
+ }
188
+
189
+ // https://regex101.com/r/FEN5ks/1
190
+ // https://stackoverflow.com/a/73265022
191
+ // $1 = default import name (can be non-existent)
192
+ // $2 = destructured exports (can be non-existent)
193
+ // $3 = wildcard import name (can be non-existent)
194
+ // $4 = module identifier
195
+ // $5 = quotes used (either ' or ")
196
+ const IMPORT_REGEX =
197
+ /import(?:(?:(?:[ \n\t]+([^ *\n\t{},]+)[ \n\t]*(?:,|[ \n\t]+))?([ \n\t]*{(?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+})?[ \n\t]*)|[ \n\t]*\*[ \n\t]*as[ \n\t]+([^ \n\t{}]+)[ \n\t]+)from[ \n\t]*(?:['"])([^'"\n]+)(['"])/gm;
198
+
199
+ const NAMED_IMPORTS_REGEX = /[ \n\t]*{((?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+)}[ \n\t]*/gm;
200
+
201
+ const allMatches = (regex: RegExp, str: string) => {
202
+ regex.lastIndex = 0;
203
+
204
+ let match;
205
+ const matches = [];
206
+ while ((match = regex.exec(str))) {
207
+ matches.push(match);
208
+ }
209
+
210
+ return matches;
211
+ };
212
+
213
+ type ParsedImport = {
214
+ defaultImportName?: string;
215
+ namedImports: string[];
216
+ wildcardImportName?: string;
217
+ moduleIdentifier: string;
218
+ quotes: string;
219
+ };
220
+
221
+ const analyzeSourceFileImports = (code: string): ParsedImport[] => {
222
+ // TODO(dmaretskyi): Support import aliases and wildcard imports.
223
+ const parsedImports = allMatches(IMPORT_REGEX, code);
224
+ return parsedImports.map((capture) => {
225
+ return {
226
+ defaultImportName: capture[1],
227
+ namedImports: capture[2]
228
+ ?.trim()
229
+ .slice(1, -1)
230
+ .split(',')
231
+ .map((importName) => importName.trim()),
232
+ wildcardImportName: capture[3],
233
+ moduleIdentifier: capture[4],
234
+ quotes: capture[5],
235
+ };
236
+ });
237
+ };
238
+
239
+ const httpPlugin: Plugin = {
240
+ name: 'http',
241
+ setup: (build) => {
242
+ // Intercept import paths starting with "http:" and "https:" so esbuild doesn't attempt to map them to a file system location.
243
+ // Tag them with the "http-url" namespace to associate them with this plugin.
244
+ build.onResolve({ filter: /^https?:\/\// }, (args) => ({
245
+ path: args.path,
246
+ namespace: 'http-url',
247
+ }));
248
+
249
+ // We also want to intercept all import paths inside downloaded files and resolve them against the original URL.
250
+ // All of these files will be in the "http-url" namespace.
251
+ // Make sure to keep the newly resolved URL in the "http-url" namespace so imports inside it will also be resolved as URLs recursively.
252
+ build.onResolve({ filter: /.*/, namespace: 'http-url' }, (args) => ({
253
+ path: new URL(args.path, args.importer).toString(),
254
+ namespace: 'http-url',
255
+ }));
256
+
257
+ // When a URL is loaded, we want to actually download the content from the internet.
258
+ // This has just enough logic to be able to handle the example import from unpkg.com but in reality this would probably need to be more complex.
259
+ build.onLoad({ filter: /.*/, namespace: 'http-url' }, async (args) => {
260
+ const response = await fetch(args.path);
261
+ return { contents: await response.text(), loader: 'jsx' };
262
+ });
263
+ },
264
+ };
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ export * from './bundler';
@@ -75,10 +75,14 @@ export const getInvocationUrl = (functionUrl: string, edgeUrl: string, options:
75
75
  const url = new URL(`./${relativeUrl}`, baseUrl.toString());
76
76
  options.spaceId && url.searchParams.set('spaceId', options.spaceId);
77
77
  options.subjectId && url.searchParams.set('subjectId', options.subjectId);
78
- url.protocol = 'https';
78
+ url.protocol = isSecure(url.protocol) ? 'https' : 'http';
79
79
  return url.toString();
80
80
  };
81
81
 
82
+ const isSecure = (protocol: string) => {
83
+ return protocol === 'https:' || protocol === 'wss:';
84
+ };
85
+
82
86
  const createEdgeIdentity = (client: Client): EdgeIdentity => {
83
87
  const identity = client.halo.identity.get();
84
88
  const device = client.halo.device;
package/src/handler.ts CHANGED
@@ -2,13 +2,16 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { type Schema as S } from '@effect/schema';
5
+ import { Schema as S } from '@effect/schema';
6
+ import { type Effect } from 'effect';
6
7
 
7
8
  import { type Client, PublicKey } from '@dxos/client';
8
9
  import { type Space, type SpaceId } from '@dxos/client/echo';
9
- import type { CoreDatabase, ReactiveEchoObject } from '@dxos/echo-db';
10
+ import type { CoreDatabase, EchoDatabase, QueryResult, ReactiveEchoObject } from '@dxos/echo-db';
11
+ import { type HasId } from '@dxos/echo-schema';
12
+ import { type DXN } from '@dxos/keys';
10
13
  import { log } from '@dxos/log';
11
- import { nonNullable } from '@dxos/util';
14
+ import { isNonNullable } from '@dxos/util';
12
15
 
13
16
  // TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
14
17
  // https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
@@ -18,15 +21,14 @@ import { nonNullable } from '@dxos/util';
18
21
  /**
19
22
  * Function handler.
20
23
  */
21
- export type FunctionHandler<TData = {}, TMeta = {}> = (params: {
24
+ export type FunctionHandler<TData = {}, TMeta = {}, TOutput = any> = (params: {
22
25
  context: FunctionContext;
23
26
  event: FunctionEvent<TData, TMeta>;
24
-
25
27
  /**
26
28
  * @deprecated
27
29
  */
28
30
  response: FunctionResponse;
29
- }) => Promise<Response | FunctionResponse | void>;
31
+ }) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any>;
30
32
 
31
33
  /**
32
34
  * Function context.
@@ -61,6 +63,7 @@ export interface FunctionContextAi {
61
63
  /**
62
64
  * Event payload.
63
65
  */
66
+ // TODO(dmaretskyi): Update type definitions to match the actual payload.
64
67
  export type FunctionEvent<TData = {}, TMeta = {}> = {
65
68
  data: FunctionEventMeta<TMeta> & TData;
66
69
  };
@@ -83,17 +86,61 @@ export type FunctionResponse = {
83
86
  // API.
84
87
  //
85
88
 
89
+ // TODO(dmaretskyi): Temporary API to get the queues working.
90
+ // TODO(dmaretskyi): To be replaced with integrating queues into echo.
91
+ export interface QueuesAPI {
92
+ queryQueue(queue: DXN, options?: {}): Promise<QueryResult>;
93
+ insertIntoQueue(queue: DXN, objects: HasId[]): Promise<void>;
94
+ }
95
+
86
96
  /**
87
97
  * Space interface available to functions.
88
98
  */
89
99
  export interface SpaceAPI {
90
100
  get id(): SpaceId;
101
+ /**
102
+ * @deprecated
103
+ */
91
104
  get crud(): CoreDatabase;
105
+ get db(): EchoDatabase;
106
+ // TODO(dmaretskyi): Align with echo api --- queues.get(id).append(items);
107
+ get queues(): QueuesAPI;
92
108
  }
93
109
 
110
+ // TODO(wittjosiah): Fix this.
94
111
  const __assertFunctionSpaceIsCompatibleWithTheClientSpace = () => {
95
- // eslint-disable-next-line unused-imports/no-unused-vars
96
- const y: SpaceAPI = {} as Space;
112
+ // const _: SpaceAPI = {} as Space;
113
+ };
114
+
115
+ export type FunctionDefinition = {
116
+ description?: string;
117
+ inputSchema: S.Schema.AnyNoContext;
118
+ outputSchema?: S.Schema.AnyNoContext;
119
+ handler: FunctionHandler<any>;
120
+ };
121
+
122
+ export type DefineFunctionParams<T, O = any> = {
123
+ description?: string;
124
+ inputSchema: S.Schema<T, any>;
125
+ outputSchema?: S.Schema<O, any>;
126
+ handler: FunctionHandler<T, any, O>;
127
+ };
128
+
129
+ // TODO(dmaretskyi): Bind input type to function handler.
130
+ export const defineFunction = <T, O>(params: DefineFunctionParams<T, O>): FunctionDefinition => {
131
+ if (!S.isSchema(params.inputSchema)) {
132
+ throw new Error('Input schema must be a valid schema');
133
+ }
134
+ if (typeof params.handler !== 'function') {
135
+ throw new Error('Handler must be a function');
136
+ }
137
+
138
+ return {
139
+ description: params.description,
140
+ inputSchema: params.inputSchema,
141
+ outputSchema: params.outputSchema ?? S.Any,
142
+ handler: params.handler,
143
+ };
97
144
  };
98
145
 
99
146
  //
@@ -135,7 +182,9 @@ export const subscriptionHandler = <TMeta>(
135
182
 
136
183
  registerTypes(space, types);
137
184
  const objects = space
138
- ? data.objects?.map<ReactiveEchoObject<any> | undefined>((id) => space!.db.getObjectById(id)).filter(nonNullable)
185
+ ? data.objects
186
+ ?.map<ReactiveEchoObject<any> | undefined>((id) => space!.db.getObjectById(id))
187
+ .filter(isNonNullable)
139
188
  : [];
140
189
 
141
190
  if (!!data.spaceKey && !space) {
@@ -236,7 +236,9 @@ describe('trigger registry', () => {
236
236
  const triggers = createTriggers(space, 3);
237
237
 
238
238
  const triggerLoaded = new Trigger();
239
- registry.registered.on((fn) => triggerLoaded.wake());
239
+ registry.registered.on(() => {
240
+ triggerLoaded.wake();
241
+ });
240
242
 
241
243
  const triggerRemoved = new Trigger<FunctionTrigger>();
242
244
  registry.removed.on((fn) => {
@@ -4,3 +4,4 @@
4
4
 
5
5
  export * from './schema';
6
6
  export * from './types';
7
+ export * from './trace';
@@ -3,11 +3,7 @@
3
3
  //
4
4
 
5
5
  import { JsonSchemaType, Ref, S, TypedObject } from '@dxos/echo-schema';
6
-
7
- // TODO(burdon): Reconcile with plugin-markdown.
8
- export class TextType extends TypedObject({ typename: 'dxos.org/type/Text', version: '0.1.0' })({
9
- content: S.String,
10
- }) {}
6
+ import { TextType } from '@dxos/schema';
11
7
 
12
8
  /**
13
9
  * Source script.
@@ -16,7 +12,6 @@ export class ScriptType extends TypedObject({
16
12
  typename: 'dxos.org/type/Script',
17
13
  version: '0.1.0',
18
14
  })({
19
- // TODO(burdon): Change to URI?
20
15
  name: S.optional(S.String),
21
16
  description: S.optional(S.String),
22
17
  // TODO(burdon): Change to hash of deployed content.
@@ -37,11 +32,14 @@ export class FunctionType extends TypedObject({
37
32
  name: S.NonEmptyString,
38
33
  version: S.String,
39
34
 
35
+ description: S.optional(S.String),
36
+
40
37
  // Reference to a source script if it exists within ECHO.
41
38
  // TODO(burdon): Don't ref ScriptType directly (core).
42
39
  source: S.optional(Ref(ScriptType)),
43
40
 
44
41
  inputSchema: S.optional(JsonSchemaType),
42
+ outputSchema: S.optional(JsonSchemaType),
45
43
 
46
44
  // Local binding to a function name.
47
45
  binding: S.optional(S.String),
@@ -0,0 +1,49 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { EchoObject, Expando, ObjectId, Ref, S } from '@dxos/echo-schema';
6
+
7
+ import { FunctionTrigger } from './types';
8
+
9
+ const InvocationTrace = S.Struct({
10
+ id: ObjectId,
11
+ /**
12
+ * Queue DXN for function/workflow invocation events.
13
+ */
14
+ invocationTraceQueue: Ref(Expando),
15
+ /**
16
+ * DXN of the invoked function/workflow.
17
+ */
18
+ invocationTarget: Ref(Expando),
19
+ /**
20
+ * Present for automatic invocations.
21
+ */
22
+ trigger: S.optional(Ref(FunctionTrigger)),
23
+ }).pipe(EchoObject('dxos.org/type/InvocationTrace', '0.1.0'));
24
+
25
+ export type InvocationTraceEvent = S.Schema.Type<typeof InvocationTrace>;
26
+
27
+ const TraceEventLog = S.Struct({
28
+ timestampMs: S.Number,
29
+ level: S.String,
30
+ message: S.String,
31
+ context: S.optional(S.Object),
32
+ });
33
+
34
+ const TraceEventException = S.Struct({
35
+ timestampMs: S.Number,
36
+ message: S.String,
37
+ name: S.String,
38
+ stack: S.optional(S.String),
39
+ });
40
+
41
+ const TraceEvent = S.Struct({
42
+ id: ObjectId,
43
+ outcome: S.String,
44
+ truncated: S.Boolean,
45
+ logs: S.Array(TraceEventLog),
46
+ exceptions: S.Array(TraceEventException),
47
+ }).pipe(EchoObject('dxos.org/type/TraceEvent', '0.1.0'));
48
+
49
+ export type TraceEvent = S.Schema.Type<typeof TraceEvent>;
@@ -2,7 +2,7 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { AST, OptionsAnnotationId, RawObject, S, TypedObject } from '@dxos/echo-schema';
5
+ import { AST, OptionsAnnotationId, RawObject, S, TypedObject, DXN } from '@dxos/echo-schema';
6
6
 
7
7
  /**
8
8
  * Type discriminator for TriggerType.
@@ -13,6 +13,8 @@ export enum TriggerKind {
13
13
  Timer = 'timer',
14
14
  Webhook = 'webhook',
15
15
  Subscription = 'subscription',
16
+ Email = 'email',
17
+ Queue = 'queue',
16
18
  }
17
19
 
18
20
  // TODO(burdon): Rename prop kind.
@@ -31,6 +33,19 @@ const TimerTriggerSchema = S.Struct({
31
33
 
32
34
  export type TimerTrigger = S.Schema.Type<typeof TimerTriggerSchema>;
33
35
 
36
+ const EmailTriggerSchema = S.Struct({
37
+ type: S.Literal(TriggerKind.Email).annotations(typeLiteralAnnotations),
38
+ }).pipe(S.mutable);
39
+
40
+ export type EmailTrigger = S.Schema.Type<typeof EmailTriggerSchema>;
41
+
42
+ const QueueTriggerSchema = S.Struct({
43
+ type: S.Literal(TriggerKind.Queue).annotations(typeLiteralAnnotations),
44
+ queue: DXN,
45
+ }).pipe(S.mutable);
46
+
47
+ export type QueueTrigger = S.Schema.Type<typeof QueueTriggerSchema>;
48
+
34
49
  /**
35
50
  * Webhook.
36
51
  */
@@ -80,10 +95,11 @@ export type SubscriptionTrigger = S.Schema.Type<typeof SubscriptionTriggerSchema
80
95
  * Trigger schema (discriminated union).
81
96
  */
82
97
  export const TriggerSchema = S.Union(
83
- //
84
98
  TimerTriggerSchema,
85
99
  WebhookTriggerSchema,
86
100
  SubscriptionTriggerSchema,
101
+ EmailTriggerSchema,
102
+ QueueTriggerSchema,
87
103
  ).annotations({
88
104
  [AST.TitleAnnotationId]: 'Trigger',
89
105
  });
@@ -95,6 +111,7 @@ export type TriggerType = S.Schema.Type<typeof TriggerSchema>;
95
111
  */
96
112
  export const FunctionTriggerSchema = S.Struct({
97
113
  // TODO(burdon): What type does this reference.
114
+ // TODO(wittjosiah): This should probably be a Ref?
98
115
  function: S.optional(S.String.annotations({ [AST.TitleAnnotationId]: 'Function' })),
99
116
 
100
117
  enabled: S.optional(S.Boolean.annotations({ [AST.TitleAnnotationId]: 'Enabled' })),
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../src/types/schema.ts", "../../../src/types/types.ts"],
4
- "sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { JsonSchemaType, Ref, S, TypedObject } from '@dxos/echo-schema';\n\n// TODO(burdon): Reconcile with plugin-markdown.\nexport class TextType extends TypedObject({ typename: 'dxos.org/type/Text', version: '0.1.0' })({\n content: S.String,\n}) {}\n\n/**\n * Source script.\n */\nexport class ScriptType extends TypedObject({\n typename: 'dxos.org/type/Script',\n version: '0.1.0',\n})({\n // TODO(burdon): Change to URI?\n name: S.optional(S.String),\n description: S.optional(S.String),\n // TODO(burdon): Change to hash of deployed content.\n // Whether source has changed since last deploy.\n changed: S.optional(S.Boolean),\n source: Ref(TextType),\n}) {}\n\n/**\n * Function deployment.\n */\n// TODO(burdon): Move to core/functions.\nexport class FunctionType extends TypedObject({\n typename: 'dxos.org/type/Function',\n version: '0.1.0',\n})({\n // TODO(burdon): Rename to id/uri?\n name: S.NonEmptyString,\n version: S.String,\n\n // Reference to a source script if it exists within ECHO.\n // TODO(burdon): Don't ref ScriptType directly (core).\n source: S.optional(Ref(ScriptType)),\n\n inputSchema: S.optional(JsonSchemaType),\n\n // Local binding to a function name.\n binding: S.optional(S.String),\n}) {}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { AST, OptionsAnnotationId, RawObject, S, TypedObject } from '@dxos/echo-schema';\n\n/**\n * Type discriminator for TriggerType.\n * Every spec has a type field of type TriggerKind that we can use to understand which type we're working with.\n * https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions\n */\nexport enum TriggerKind {\n Timer = 'timer',\n Webhook = 'webhook',\n Subscription = 'subscription',\n}\n\n// TODO(burdon): Rename prop kind.\nconst typeLiteralAnnotations = { [AST.TitleAnnotationId]: 'Type' };\n\n/**\n * Cron timer.\n */\nconst TimerTriggerSchema = S.Struct({\n type: S.Literal(TriggerKind.Timer).annotations(typeLiteralAnnotations),\n cron: S.String.annotations({\n [AST.TitleAnnotationId]: 'Cron',\n [AST.ExamplesAnnotationId]: ['0 0 * * *'],\n }),\n}).pipe(S.mutable);\n\nexport type TimerTrigger = S.Schema.Type<typeof TimerTriggerSchema>;\n\n/**\n * Webhook.\n */\nconst WebhookTriggerSchema = S.Struct({\n type: S.Literal(TriggerKind.Webhook).annotations(typeLiteralAnnotations),\n method: S.optional(\n S.String.annotations({\n [AST.TitleAnnotationId]: 'Method',\n [OptionsAnnotationId]: ['GET', 'POST'],\n }),\n ),\n port: S.optional(\n S.Number.annotations({\n [AST.TitleAnnotationId]: 'Port',\n }),\n ),\n}).pipe(S.mutable);\n\nexport type WebhookTrigger = S.Schema.Type<typeof WebhookTriggerSchema>;\n\n// TODO(burdon): Use ECHO definition (from https://github.com/dxos/dxos/pull/8233).\nconst QuerySchema = S.Struct({\n type: S.optional(S.String.annotations({ [AST.TitleAnnotationId]: 'Type' })),\n props: S.optional(S.Record({ key: S.String, value: S.Any })),\n}).annotations({ [AST.TitleAnnotationId]: 'Query' });\n\n/**\n * Subscription.\n */\nconst SubscriptionTriggerSchema = S.Struct({\n type: S.Literal(TriggerKind.Subscription).annotations(typeLiteralAnnotations),\n // TODO(burdon): Define query DSL (from ECHO). Reconcile with Table.Query.\n filter: QuerySchema,\n options: S.optional(\n S.Struct({\n // Watch changes to object (not just creation).\n deep: S.optional(S.Boolean.annotations({ [AST.TitleAnnotationId]: 'Nested' })),\n // Debounce changes (delay in ms).\n delay: S.optional(S.Number.annotations({ [AST.TitleAnnotationId]: 'Delay' })),\n }).annotations({ [AST.TitleAnnotationId]: 'Options' }),\n ),\n}).pipe(S.mutable);\n\nexport type SubscriptionTrigger = S.Schema.Type<typeof SubscriptionTriggerSchema>;\n\n/**\n * Trigger schema (discriminated union).\n */\nexport const TriggerSchema = S.Union(\n //\n TimerTriggerSchema,\n WebhookTriggerSchema,\n SubscriptionTriggerSchema,\n).annotations({\n [AST.TitleAnnotationId]: 'Trigger',\n});\n\nexport type TriggerType = S.Schema.Type<typeof TriggerSchema>;\n\n/**\n * Function trigger.\n */\nexport const FunctionTriggerSchema = S.Struct({\n // TODO(burdon): What type does this reference.\n function: S.optional(S.String.annotations({ [AST.TitleAnnotationId]: 'Function' })),\n\n enabled: S.optional(S.Boolean.annotations({ [AST.TitleAnnotationId]: 'Enabled' })),\n\n // TODO(burdon): Flatten entire schema.\n spec: S.optional(TriggerSchema),\n\n // TODO(burdon): Get meta from function.\n // The `meta` property is merged into the event data passed to the function.\n meta: S.optional(S.mutable(S.Record({ key: S.String, value: S.Any }))),\n});\n\nexport type FunctionTriggerType = S.Schema.Type<typeof FunctionTriggerSchema>;\n\n/**\n * Function trigger.\n */\nexport class FunctionTrigger extends TypedObject({\n typename: 'dxos.org/type/FunctionTrigger',\n version: '0.1.0',\n})(FunctionTriggerSchema.fields) {}\n\n/**\n * Function definition.\n * @deprecated (Use dxos.org/type/Function)\n */\n// TODO(burdon): Reconcile with FunctionType.\nexport class FunctionDef extends TypedObject({\n typename: 'dxos.org/type/FunctionDef',\n version: '0.1.0',\n})({\n uri: S.String,\n description: S.optional(S.String),\n route: S.String,\n handler: S.String,\n}) {}\n\n/**\n * Function manifest file.\n */\nexport const FunctionManifestSchema = S.Struct({\n functions: S.optional(S.mutable(S.Array(RawObject(FunctionDef)))),\n triggers: S.optional(S.mutable(S.Array(RawObject(FunctionTrigger)))),\n});\n\nexport type FunctionManifest = S.Schema.Type<typeof FunctionManifestSchema>;\n\nexport const FUNCTION_TYPES = [FunctionDef, FunctionTrigger];\n"],
5
- "mappings": ";;;AAIA,SAASA,gBAAgBC,KAAKC,GAAGC,mBAAmB;AAG7C,IAAMC,WAAN,cAAuBC,YAAY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AEM,OAAOF,YAAY;IAAE,CAACP,IAAIC,iBAAiB,GAAG;EAAW,CAAA,CAAA;EAEhFkC,SAAShC,GAAEY,SAASZ,GAAE0B,QAAQtB,YAAY;IAAE,CAACP,IAAIC,iBAAiB,GAAG;EAAU,CAAA,CAAA;;EAG/EmC,MAAMjC,GAAEY,SAASgB,aAAAA;;;EAIjBM,MAAMlC,GAAEY,SAASZ,GAAES,QAAQT,GAAEkB,OAAO;IAAEC,KAAKnB,GAAEM;IAAQc,OAAOpB,GAAEqB;EAAI,CAAA,CAAA,CAAA;AACpE,CAAA;AAOO,IAAMc,kBAAN,cAA8BC,aAAY;EAC/CC,UAAU;EACVC,SAAS;AACX,CAAA,EAAGR,sBAAsBS,MAAM,EAAA;AAAG;AAO3B,IAAMC,cAAN,cAA0BJ,aAAY;EAC3CC,UAAU;EACVC,SAAS;AACX,CAAA,EAAG;EACDG,KAAKzC,GAAEM;EACPoC,aAAa1C,GAAEY,SAASZ,GAAEM,MAAM;EAChCqC,OAAO3C,GAAEM;EACTsC,SAAS5C,GAAEM;AACb,CAAA,EAAA;AAAI;AAKG,IAAMuC,yBAAyB7C,GAAEC,OAAO;EAC7C6C,WAAW9C,GAAEY,SAASZ,GAAES,QAAQT,GAAE+C,MAAMC,UAAUR,WAAAA,CAAAA,CAAAA,CAAAA;EAClDS,UAAUjD,GAAEY,SAASZ,GAAES,QAAQT,GAAE+C,MAAMC,UAAUb,eAAAA,CAAAA,CAAAA,CAAAA;AACnD,CAAA;AAIO,IAAMe,iBAAiB;EAACV;EAAaL;;",
6
- "names": ["JsonSchemaType", "Ref", "S", "TypedObject", "TextType", "TypedObject", "typename", "version", "content", "S", "String", "ScriptType", "name", "optional", "description", "changed", "Boolean", "source", "Ref", "FunctionType", "NonEmptyString", "inputSchema", "JsonSchemaType", "binding", "AST", "OptionsAnnotationId", "RawObject", "S", "TypedObject", "TriggerKind", "typeLiteralAnnotations", "AST", "TitleAnnotationId", "TimerTriggerSchema", "S", "Struct", "type", "Literal", "annotations", "cron", "String", "ExamplesAnnotationId", "pipe", "mutable", "WebhookTriggerSchema", "method", "optional", "OptionsAnnotationId", "port", "Number", "QuerySchema", "props", "Record", "key", "value", "Any", "SubscriptionTriggerSchema", "filter", "options", "deep", "Boolean", "delay", "TriggerSchema", "Union", "FunctionTriggerSchema", "function", "enabled", "spec", "meta", "FunctionTrigger", "TypedObject", "typename", "version", "fields", "FunctionDef", "uri", "description", "route", "handler", "FunctionManifestSchema", "functions", "Array", "RawObject", "triggers", "FUNCTION_TYPES"]
7
- }