@dxos/functions 0.8.4-main.b97322e → 0.8.4-main.dedc0f3

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 (138) hide show
  1. package/dist/lib/browser/bundler/index.mjs +56 -38
  2. package/dist/lib/browser/bundler/index.mjs.map +3 -3
  3. package/dist/lib/browser/chunk-ANP3DFCO.mjs +623 -0
  4. package/dist/lib/browser/chunk-ANP3DFCO.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
  6. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
  7. package/dist/lib/browser/edge/index.mjs +22 -8
  8. package/dist/lib/browser/edge/index.mjs.map +3 -3
  9. package/dist/lib/browser/index.mjs +892 -130
  10. package/dist/lib/browser/index.mjs.map +4 -4
  11. package/dist/lib/browser/meta.json +1 -1
  12. package/dist/lib/browser/testing/index.mjs +77 -39
  13. package/dist/lib/browser/testing/index.mjs.map +3 -3
  14. package/dist/lib/node-esm/bundler/index.mjs +55 -38
  15. package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
  16. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
  17. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
  18. package/dist/lib/node-esm/chunk-MPKVY7ZR.mjs +625 -0
  19. package/dist/lib/node-esm/chunk-MPKVY7ZR.mjs.map +7 -0
  20. package/dist/lib/node-esm/edge/index.mjs +21 -8
  21. package/dist/lib/node-esm/edge/index.mjs.map +3 -3
  22. package/dist/lib/node-esm/index.mjs +892 -130
  23. package/dist/lib/node-esm/index.mjs.map +4 -4
  24. package/dist/lib/node-esm/meta.json +1 -1
  25. package/dist/lib/node-esm/testing/index.mjs +77 -39
  26. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  27. package/dist/types/src/bundler/bundler.d.ts +11 -12
  28. package/dist/types/src/bundler/bundler.d.ts.map +1 -1
  29. package/dist/types/src/edge/functions.d.ts +3 -2
  30. package/dist/types/src/edge/functions.d.ts.map +1 -1
  31. package/dist/types/src/errors.d.ts +77 -8
  32. package/dist/types/src/errors.d.ts.map +1 -1
  33. package/dist/types/src/examples/fib.d.ts +7 -0
  34. package/dist/types/src/examples/fib.d.ts.map +1 -0
  35. package/dist/types/src/examples/index.d.ts +4 -0
  36. package/dist/types/src/examples/index.d.ts.map +1 -0
  37. package/dist/types/src/examples/reply.d.ts +3 -0
  38. package/dist/types/src/examples/reply.d.ts.map +1 -0
  39. package/dist/types/src/examples/sleep.d.ts +5 -0
  40. package/dist/types/src/examples/sleep.d.ts.map +1 -0
  41. package/dist/types/src/executor/executor.d.ts +4 -1
  42. package/dist/types/src/executor/executor.d.ts.map +1 -1
  43. package/dist/types/src/handler.d.ts +40 -8
  44. package/dist/types/src/handler.d.ts.map +1 -1
  45. package/dist/types/src/index.d.ts +3 -1
  46. package/dist/types/src/index.d.ts.map +1 -1
  47. package/dist/types/src/schema.d.ts +6 -1
  48. package/dist/types/src/schema.d.ts.map +1 -1
  49. package/dist/types/src/services/credentials.d.ts +16 -3
  50. package/dist/types/src/services/credentials.d.ts.map +1 -1
  51. package/dist/types/src/services/database.d.ts +75 -6
  52. package/dist/types/src/services/database.d.ts.map +1 -1
  53. package/dist/types/src/services/event-logger.d.ts +65 -30
  54. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  55. package/dist/types/src/services/index.d.ts +2 -1
  56. package/dist/types/src/services/index.d.ts.map +1 -1
  57. package/dist/types/src/services/local-function-execution.d.ts +25 -0
  58. package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
  59. package/dist/types/src/services/queues.d.ts +21 -6
  60. package/dist/types/src/services/queues.d.ts.map +1 -1
  61. package/dist/types/src/services/remote-function-execution-service.d.ts +15 -0
  62. package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -0
  63. package/dist/types/src/services/service-container.d.ts +5 -5
  64. package/dist/types/src/services/service-container.d.ts.map +1 -1
  65. package/dist/types/src/services/service-registry.d.ts +1 -1
  66. package/dist/types/src/services/service-registry.d.ts.map +1 -1
  67. package/dist/types/src/services/tracing.d.ts +37 -5
  68. package/dist/types/src/services/tracing.d.ts.map +1 -1
  69. package/dist/types/src/testing/layer.d.ts +7 -2
  70. package/dist/types/src/testing/layer.d.ts.map +1 -1
  71. package/dist/types/src/testing/logger.d.ts +3 -3
  72. package/dist/types/src/testing/logger.d.ts.map +1 -1
  73. package/dist/types/src/testing/persist-database.test.d.ts +2 -0
  74. package/dist/types/src/testing/persist-database.test.d.ts.map +1 -0
  75. package/dist/types/src/testing/services.d.ts +6 -17
  76. package/dist/types/src/testing/services.d.ts.map +1 -1
  77. package/dist/types/src/trace.d.ts +20 -22
  78. package/dist/types/src/trace.d.ts.map +1 -1
  79. package/dist/types/src/triggers/index.d.ts +4 -0
  80. package/dist/types/src/triggers/index.d.ts.map +1 -0
  81. package/dist/types/src/triggers/input-builder.d.ts +3 -0
  82. package/dist/types/src/triggers/input-builder.d.ts.map +1 -0
  83. package/dist/types/src/triggers/invocation-tracer.d.ts +35 -0
  84. package/dist/types/src/triggers/invocation-tracer.d.ts.map +1 -0
  85. package/dist/types/src/triggers/trigger-dispatcher.d.ts +75 -0
  86. package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +1 -0
  87. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +2 -0
  88. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +1 -0
  89. package/dist/types/src/triggers/trigger-state-store.d.ts +27 -0
  90. package/dist/types/src/triggers/trigger-state-store.d.ts.map +1 -0
  91. package/dist/types/src/types.d.ts +49 -249
  92. package/dist/types/src/types.d.ts.map +1 -1
  93. package/dist/types/src/url.d.ts +10 -6
  94. package/dist/types/src/url.d.ts.map +1 -1
  95. package/dist/types/tsconfig.tsbuildinfo +1 -1
  96. package/package.json +39 -34
  97. package/src/bundler/bundler.test.ts +8 -9
  98. package/src/bundler/bundler.ts +32 -33
  99. package/src/edge/functions.ts +8 -5
  100. package/src/errors.ts +8 -0
  101. package/src/examples/fib.ts +30 -0
  102. package/src/examples/index.ts +7 -0
  103. package/src/examples/reply.ts +18 -0
  104. package/src/examples/sleep.ts +22 -0
  105. package/src/executor/executor.ts +9 -9
  106. package/src/handler.ts +99 -18
  107. package/src/index.ts +3 -3
  108. package/src/schema.ts +11 -0
  109. package/src/services/credentials.ts +79 -3
  110. package/src/services/database.ts +118 -18
  111. package/src/services/event-logger.ts +68 -37
  112. package/src/services/index.ts +2 -1
  113. package/src/services/local-function-execution.ts +114 -0
  114. package/src/services/queues.ts +37 -10
  115. package/src/services/remote-function-execution-service.ts +46 -0
  116. package/src/services/service-container.ts +11 -10
  117. package/src/services/service-registry.ts +5 -2
  118. package/src/services/tracing.ts +105 -7
  119. package/src/testing/layer.ts +83 -3
  120. package/src/testing/logger.ts +4 -4
  121. package/src/testing/persist-database.test.ts +87 -0
  122. package/src/testing/services.ts +10 -63
  123. package/src/trace.ts +17 -19
  124. package/src/triggers/index.ts +7 -0
  125. package/src/triggers/input-builder.ts +35 -0
  126. package/src/triggers/invocation-tracer.ts +99 -0
  127. package/src/triggers/trigger-dispatcher.test.ts +652 -0
  128. package/src/triggers/trigger-dispatcher.ts +512 -0
  129. package/src/triggers/trigger-state-store.ts +60 -0
  130. package/src/types.ts +22 -33
  131. package/src/url.ts +13 -10
  132. package/dist/lib/browser/chunk-3NGCSUEW.mjs +0 -328
  133. package/dist/lib/browser/chunk-3NGCSUEW.mjs.map +0 -7
  134. package/dist/lib/node-esm/chunk-FJ2MU7TL.mjs +0 -330
  135. package/dist/lib/node-esm/chunk-FJ2MU7TL.mjs.map +0 -7
  136. package/dist/types/src/services/function-call-service.d.ts +0 -16
  137. package/dist/types/src/services/function-call-service.d.ts.map +0 -1
  138. package/src/services/function-call-service.ts +0 -64
package/package.json CHANGED
@@ -1,30 +1,34 @@
1
1
  {
2
2
  "name": "@dxos/functions",
3
- "version": "0.8.4-main.b97322e",
3
+ "version": "0.8.4-main.dedc0f3",
4
4
  "description": "Functions API and runtime.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
7
  "license": "MIT",
8
8
  "author": "info@dxos.org",
9
- "sideEffects": true,
9
+ "sideEffects": false,
10
10
  "type": "module",
11
11
  "exports": {
12
+ ".": {
13
+ "source": "./src/index.ts",
14
+ "types": "./dist/types/src/index.d.ts",
15
+ "browser": "./dist/lib/browser/index.mjs",
16
+ "node": "./dist/lib/node-esm/index.mjs"
17
+ },
12
18
  "./bundler": {
19
+ "source": "./src/bundler/index.ts",
13
20
  "types": "./dist/types/src/bundler/index.d.ts",
14
21
  "browser": "./dist/lib/browser/bundler/index.mjs",
15
22
  "node": "./dist/lib/node-esm/bundler/index.mjs"
16
23
  },
17
24
  "./edge": {
25
+ "source": "./src/edge/index.ts",
18
26
  "types": "./dist/types/src/edge/index.d.ts",
19
27
  "browser": "./dist/lib/browser/edge/index.mjs",
20
28
  "node": "./dist/lib/node-esm/edge/index.mjs"
21
29
  },
22
- ".": {
23
- "types": "./dist/types/src/index.d.ts",
24
- "browser": "./dist/lib/browser/index.mjs",
25
- "node": "./dist/lib/node-esm/index.mjs"
26
- },
27
30
  "./testing": {
31
+ "source": "./src/testing/index.ts",
28
32
  "types": "./dist/types/src/testing/index.d.ts",
29
33
  "browser": "./dist/lib/browser/testing/index.mjs",
30
34
  "node": "./dist/lib/node-esm/testing/index.mjs"
@@ -47,48 +51,49 @@
47
51
  "src"
48
52
  ],
49
53
  "dependencies": {
50
- "@effect/platform": "0.89.0",
54
+ "@effect/platform": "0.90.2",
51
55
  "@preact/signals-core": "^1.9.0",
52
56
  "cron": "^3.1.6",
53
57
  "cron-schedule": "^5.0.4",
54
- "effect": "3.17.0",
58
+ "effect": "3.17.7",
55
59
  "esbuild-wasm": "^0.16.14",
56
60
  "express": "^4.19.2",
57
61
  "get-port-please": "^3.1.1",
58
62
  "i18next": "^24.2.1",
59
63
  "iso-did": "^1.6.0",
60
64
  "ws": "^8.14.2",
61
- "@dxos/ai": "0.8.4-main.b97322e",
62
- "@dxos/async": "0.8.4-main.b97322e",
63
- "@dxos/client": "0.8.4-main.b97322e",
64
- "@dxos/crypto": "0.8.4-main.b97322e",
65
- "@dxos/context": "0.8.4-main.b97322e",
66
- "@dxos/echo-db": "0.8.4-main.b97322e",
67
- "@dxos/echo": "0.8.4-main.b97322e",
68
- "@dxos/echo-pipeline": "0.8.4-main.b97322e",
69
- "@dxos/echo-protocol": "0.8.4-main.b97322e",
70
- "@dxos/echo-schema": "0.8.4-main.b97322e",
71
- "@dxos/effect": "0.8.4-main.b97322e",
72
- "@dxos/edge-client": "0.8.4-main.b97322e",
73
- "@dxos/errors": "0.8.4-main.b97322e",
74
- "@dxos/invariant": "0.8.4-main.b97322e",
75
- "@dxos/keys": "0.8.4-main.b97322e",
76
- "@dxos/live-object": "0.8.4-main.b97322e",
77
- "@dxos/log": "0.8.4-main.b97322e",
78
- "@dxos/node-std": "0.8.4-main.b97322e",
79
- "@dxos/protocols": "0.8.4-main.b97322e",
80
- "@dxos/schema": "0.8.4-main.b97322e",
81
- "@dxos/util": "0.8.4-main.b97322e"
65
+ "@dxos/ai": "0.8.4-main.dedc0f3",
66
+ "@dxos/async": "0.8.4-main.dedc0f3",
67
+ "@dxos/context": "0.8.4-main.dedc0f3",
68
+ "@dxos/client": "0.8.4-main.dedc0f3",
69
+ "@dxos/client-protocol": "0.8.4-main.dedc0f3",
70
+ "@dxos/crypto": "0.8.4-main.dedc0f3",
71
+ "@dxos/debug": "0.8.4-main.dedc0f3",
72
+ "@dxos/echo-db": "0.8.4-main.dedc0f3",
73
+ "@dxos/echo": "0.8.4-main.dedc0f3",
74
+ "@dxos/echo-pipeline": "0.8.4-main.dedc0f3",
75
+ "@dxos/echo-protocol": "0.8.4-main.dedc0f3",
76
+ "@dxos/echo-schema": "0.8.4-main.dedc0f3",
77
+ "@dxos/edge-client": "0.8.4-main.dedc0f3",
78
+ "@dxos/invariant": "0.8.4-main.dedc0f3",
79
+ "@dxos/effect": "0.8.4-main.dedc0f3",
80
+ "@dxos/errors": "0.8.4-main.dedc0f3",
81
+ "@dxos/keys": "0.8.4-main.dedc0f3",
82
+ "@dxos/kv-store": "0.8.4-main.dedc0f3",
83
+ "@dxos/live-object": "0.8.4-main.dedc0f3",
84
+ "@dxos/log": "0.8.4-main.dedc0f3",
85
+ "@dxos/node-std": "0.8.4-main.dedc0f3",
86
+ "@dxos/protocols": "0.8.4-main.dedc0f3",
87
+ "@dxos/util": "0.8.4-main.dedc0f3",
88
+ "@dxos/schema": "0.8.4-main.dedc0f3"
82
89
  },
83
90
  "devDependencies": {
84
91
  "@types/express": "^4.17.17",
85
92
  "@types/ws": "^7.4.0",
86
- "@dxos/agent": "0.8.4-main.b97322e"
93
+ "@dxos/agent": "0.8.4-main.dedc0f3"
87
94
  },
88
95
  "publishConfig": {
89
96
  "access": "public"
90
97
  },
91
- "scripts": {
92
- "gen-schema": "ts-node ./tools/schema.ts"
93
- }
98
+ "scripts": {}
94
99
  }
@@ -4,7 +4,7 @@
4
4
 
5
5
  // @ts-ignore
6
6
  import wasmUrl from 'esbuild-wasm/esbuild.wasm?url';
7
- import { beforeAll, describe, expect, test } from 'vitest';
7
+ import { assert, beforeAll, describe, expect, test } from 'vitest';
8
8
 
9
9
  import { isNode } from '@dxos/util';
10
10
 
@@ -20,8 +20,8 @@ describe('Bundler', () => {
20
20
  test('Basic', async () => {
21
21
  const bundler = new Bundler({ platform: 'node', sandboxedModules: [], remoteModules: {} });
22
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;
23
+ assert(!('error' in result), 'error should not exist');
24
+ expect(result.asset).toBeDefined();
25
25
  });
26
26
 
27
27
  test('Import', async () => {
@@ -33,8 +33,8 @@ describe('Bundler', () => {
33
33
  const query = Filter.typename('dxos.org/type/Example');
34
34
  `,
35
35
  });
36
- expect(result.bundle).to.exist;
37
- expect(result.error).to.not.exist;
36
+ assert(!('error' in result), 'error should not exist');
37
+ expect(result.asset).toBeDefined();
38
38
  });
39
39
 
40
40
  // TODO(dmaretskyi): Flaky on CI.
@@ -46,14 +46,13 @@ describe('Bundler', () => {
46
46
  invariant(true);
47
47
  `,
48
48
  });
49
- expect(result.bundle).to.exist;
50
- expect(result.error).to.not.exist;
49
+ assert(!('error' in result), 'error should not exist');
50
+ expect(result.asset).toBeDefined();
51
51
  });
52
52
 
53
53
  test('Error', async () => {
54
54
  const bundler = new Bundler({ platform: 'node', sandboxedModules: [], remoteModules: {} });
55
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;
56
+ assert('error' in result, 'error should exist');
58
57
  });
59
58
  });
@@ -3,8 +3,8 @@
3
3
  //
4
4
 
5
5
  import { FetchHttpClient, HttpClient } from '@effect/platform';
6
- import { Duration, Effect, pipe, Schedule } from 'effect';
7
- import { type BuildOptions, type Loader, build, initialize, type BuildResult, type Plugin } from 'esbuild-wasm';
6
+ import { Duration, Effect, Schedule, pipe } from 'effect';
7
+ import { type BuildOptions, type BuildResult, type Loader, type Plugin, build, initialize } from 'esbuild-wasm';
8
8
 
9
9
  import { subtleCrypto } from '@dxos/crypto';
10
10
  import { runAndForwardErrors } from '@dxos/effect';
@@ -18,26 +18,26 @@ export type Import = {
18
18
  };
19
19
 
20
20
  export type BundleOptions = {
21
- /**
22
- * Path to the source file on the local file system.
23
- * If provided, the path will be used instead of the `source` code.
24
- */
25
- path?: string;
26
-
27
21
  /**
28
22
  * Source code to bundle.
29
- * Required if `path` is not provided.
30
23
  */
31
- source?: string;
24
+ source: string;
32
25
  };
33
26
 
34
- export type BundleResult = {
35
- timestamp: number;
36
- sourceHash?: Buffer;
37
- imports?: Import[];
38
- bundle?: string;
39
- error?: any;
40
- };
27
+ export type BundleResult =
28
+ | {
29
+ timestamp: number;
30
+ sourceHash: Buffer;
31
+ error: unknown;
32
+ }
33
+ | {
34
+ timestamp: number;
35
+ sourceHash: Buffer;
36
+ imports: Import[];
37
+ entryPoint: string;
38
+ asset: Uint8Array;
39
+ bundle: string;
40
+ };
41
41
 
42
42
  export type BundlerOptions = {
43
43
  platform: BuildOptions['platform'];
@@ -58,16 +58,9 @@ export const initializeBundler = async (options: { wasmUrl: string }) => {
58
58
  export class Bundler {
59
59
  constructor(private readonly _options: BundlerOptions) {}
60
60
 
61
- async bundle({ path, source }: BundleOptions): Promise<BundleResult> {
61
+ async bundle({ source }: BundleOptions): Promise<BundleResult> {
62
62
  const { sandboxedModules: providedModules, ...options } = this._options;
63
-
64
- const createResult = async (result?: Partial<BundleResult>) => {
65
- return {
66
- timestamp: Date.now(),
67
- sourceHash: source ? Buffer.from(await subtleCrypto.digest('SHA-256', Buffer.from(source))) : undefined,
68
- ...result,
69
- };
70
- };
63
+ const sourceHash = Buffer.from(await subtleCrypto.digest('SHA-256', Buffer.from(source)));
71
64
 
72
65
  if (this._options.platform === 'browser') {
73
66
  invariant(initialized, 'Compiler not initialized.');
@@ -83,7 +76,10 @@ export class Bundler {
83
76
  conditions: ['workerd', 'browser'],
84
77
  metafile: true,
85
78
  write: false,
86
- entryPoints: [path ?? 'memory:main.tsx'],
79
+ entryPoints: {
80
+ // Gets mapped to `userFunc.js` by esbuild.
81
+ userFunc: 'memory:main.tsx',
82
+ },
87
83
  bundle: true,
88
84
  format: 'esm',
89
85
  plugins: [
@@ -136,12 +132,17 @@ export class Bundler {
136
132
 
137
133
  log('compile complete', result.metafile);
138
134
 
139
- return await createResult({
135
+ const entryPoint = 'userFunc.js';
136
+ return {
137
+ timestamp: Date.now(),
138
+ sourceHash,
140
139
  imports: this.analyzeImports(result),
140
+ entryPoint,
141
+ asset: result.outputFiles![0].contents,
141
142
  bundle: result.outputFiles![0].text,
142
- });
143
+ };
143
144
  } catch (err) {
144
- return await createResult({ error: err });
145
+ return { timestamp: Date.now(), sourceHash, error: err };
145
146
  }
146
147
  }
147
148
 
@@ -153,7 +154,6 @@ export class Bundler {
153
154
  const parsedImports = allMatches(IMPORT_REGEX, result.outputFiles[0].text);
154
155
  return Object.values(result.metafile!.outputs)[0].imports.map((entry): Import => {
155
156
  const namedImports: string[] = [];
156
-
157
157
  const parsedImport = parsedImports.find((capture) => capture?.[4] === entry.path);
158
158
  if (parsedImport?.[2]) {
159
159
  NAMED_IMPORTS_REGEX.lastIndex = 0;
@@ -207,10 +207,9 @@ const IMPORT_REGEX =
207
207
  const NAMED_IMPORTS_REGEX = /[ \n\t]*{((?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+)}[ \n\t]*/gm;
208
208
 
209
209
  const allMatches = (regex: RegExp, str: string) => {
210
- regex.lastIndex = 0;
211
-
212
210
  let match;
213
211
  const matches = [];
212
+ regex.lastIndex = 0;
214
213
  while ((match = regex.exec(str))) {
215
214
  matches.push(match);
216
215
  }
@@ -14,21 +14,24 @@ import { type UploadFunctionResponseBody } from '@dxos/protocols';
14
14
 
15
15
  export type UploadWorkerArgs = {
16
16
  client: Client;
17
- source: string;
18
17
  version: string;
19
18
  name?: string;
20
19
  functionId?: string;
21
20
  ownerPublicKey: PublicKey;
21
+ entryPoint: string;
22
+ assets: Record<string, Uint8Array>;
22
23
  };
23
24
 
24
25
  export const uploadWorkerFunction = async ({
25
26
  client,
26
27
  version,
27
- source,
28
28
  name,
29
29
  functionId,
30
30
  ownerPublicKey,
31
+ entryPoint,
32
+ assets,
31
33
  }: UploadWorkerArgs): Promise<UploadFunctionResponseBody> => {
34
+ log('uploading function', { functionId, name, version, ownerPublicKey });
32
35
  const edgeUrl = client.config.values.runtime?.services?.edge?.url;
33
36
  invariant(edgeUrl, 'Edge is not configured.');
34
37
  const edgeClient = new EdgeHttpClient(edgeUrl);
@@ -36,15 +39,15 @@ export const uploadWorkerFunction = async ({
36
39
  edgeClient.setIdentity(edgeIdentity);
37
40
  const response = await edgeClient.uploadFunction(
38
41
  { functionId },
39
- { name, version, script: source, ownerPublicKey: ownerPublicKey.toHex() },
42
+ { name, version, ownerPublicKey: ownerPublicKey.toHex(), entryPoint, assets },
40
43
  );
41
44
 
42
45
  // TODO(burdon): Edge service log.
43
- log.info('Uploaded', {
46
+ log('uploaded', {
44
47
  identityKey: edgeIdentity.identityKey,
45
48
  functionId,
46
49
  name,
47
- source: source.length,
50
+ version,
48
51
  response,
49
52
  });
50
53
 
package/src/errors.ts CHANGED
@@ -10,4 +10,12 @@ export class ServiceNotAvailableError extends BaseError.extend('SERVICE_NOT_AVAI
10
10
  }
11
11
  }
12
12
 
13
+ export class FunctionNotFoundError extends BaseError.extend('FUNCTION_NOT_FOUND') {
14
+ constructor(functionKey: string) {
15
+ super(`Function not found: ${functionKey}`);
16
+ }
17
+ }
18
+
13
19
  export class FunctionError extends BaseError.extend('FUNCTION_ERROR') {}
20
+
21
+ export class TriggerStateNotFoundError extends BaseError.extend('TRIGGER_STATE_NOT_FOUND') {}
@@ -0,0 +1,30 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { Effect, Schema } from 'effect';
6
+
7
+ import { defineFunction } from '../handler';
8
+
9
+ export default defineFunction({
10
+ name: 'example.org/function/fib',
11
+ description: 'Function that calculates a Fibonacci number',
12
+ inputSchema: Schema.Struct({
13
+ iterations: Schema.optional(Schema.Number).annotations({
14
+ description: 'Number of iterations',
15
+ default: 100_000,
16
+ }),
17
+ }),
18
+ outputSchema: Schema.Struct({
19
+ result: Schema.String,
20
+ }),
21
+ handler: Effect.fn(function* ({ data: { iterations = 100_000 } }) {
22
+ let a = 0n;
23
+ let b = 1n;
24
+ for (let i = 0; i < iterations; i++) {
25
+ a += b;
26
+ b = a - b;
27
+ }
28
+ return { result: a.toString() };
29
+ }),
30
+ });
@@ -0,0 +1,7 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export { default as fib } from './fib';
6
+ export { default as reply } from './reply';
7
+ export { default as sleep } from './sleep';
@@ -0,0 +1,18 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { Console, Effect, Schema } from 'effect';
6
+
7
+ import { defineFunction } from '../handler';
8
+
9
+ export default defineFunction({
10
+ name: 'example.org/function/reply',
11
+ description: 'Function that echoes the input',
12
+ inputSchema: Schema.Any,
13
+ outputSchema: Schema.Any,
14
+ handler: Effect.fn(function* ({ data }) {
15
+ yield* Console.log('reply', { data });
16
+ return data;
17
+ }),
18
+ });
@@ -0,0 +1,22 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { Effect, Schema } from 'effect';
6
+
7
+ import { defineFunction } from '../handler';
8
+
9
+ export default defineFunction({
10
+ name: 'example.org/function/sleep',
11
+ description: 'Function that sleeps for a given amount of time',
12
+ inputSchema: Schema.Struct({
13
+ duration: Schema.optional(Schema.Number).annotations({
14
+ description: 'Milliseconds to sleep',
15
+ default: 100_000,
16
+ }),
17
+ }),
18
+ outputSchema: Schema.Void,
19
+ handler: Effect.fn(function* ({ data: { duration = 100_000 } }) {
20
+ yield* Effect.sleep(duration);
21
+ }),
22
+ });
@@ -4,7 +4,7 @@
4
4
 
5
5
  import { Effect, Schema } from 'effect';
6
6
 
7
- import type { SpaceId } from '@dxos/client/echo';
7
+ import { type SpaceId } from '@dxos/client/echo';
8
8
  import { runAndForwardErrors } from '@dxos/effect';
9
9
 
10
10
  import type { FunctionContext, FunctionDefinition } from '../handler';
@@ -13,27 +13,27 @@ import type { ServiceContainer, Services } from '../services';
13
13
  export class FunctionExecutor {
14
14
  constructor(private readonly _services: ServiceContainer) {}
15
15
 
16
+ /**
17
+ * Invoke function.
18
+ */
16
19
  // TODO(dmaretskyi): Invocation context: queue, space, etc...
17
20
  async invoke<F extends FunctionDefinition<any, any>>(
18
- fnDef: F,
21
+ functionDef: F,
19
22
  input: F extends FunctionDefinition<infer I, infer _O> ? I : never,
20
23
  ): Promise<F extends FunctionDefinition<infer _I, infer O> ? O : never> {
21
24
  // Assert input matches schema
22
- const assertInput = fnDef.inputSchema.pipe(Schema.asserts);
25
+ const assertInput = functionDef.inputSchema.pipe(Schema.asserts);
23
26
  (assertInput as any)(input);
24
27
 
25
28
  const context: FunctionContext = {
29
+ space: undefined,
26
30
  getService: this._services.getService.bind(this._services),
27
31
  getSpace: async (_spaceId: SpaceId) => {
28
32
  throw new Error('Not available. Use the database service instead.');
29
33
  },
30
- space: undefined,
31
- get ai(): never {
32
- throw new Error('Not available. Use the ai service instead.');
33
- },
34
34
  };
35
35
 
36
- const result = fnDef.handler({ context, data: input });
36
+ const result = functionDef.handler({ context, data: input });
37
37
 
38
38
  let data: unknown;
39
39
  if (Effect.isEffect(result)) {
@@ -46,7 +46,7 @@ export class FunctionExecutor {
46
46
  }
47
47
 
48
48
  // Assert output matches schema
49
- const assertOutput = fnDef.outputSchema?.pipe(Schema.asserts);
49
+ const assertOutput = functionDef.outputSchema?.pipe(Schema.asserts);
50
50
  (assertOutput as any)(data);
51
51
 
52
52
  return data as any;
package/src/handler.ts CHANGED
@@ -2,16 +2,17 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { Schema, type Context, type Effect } from 'effect';
5
+ import { type Context, Effect, Schema, type Types } from 'effect';
6
6
 
7
- import { type AiServiceClient } from '@dxos/ai';
8
- // import { type Space } from '@dxos/client/echo';
9
- import type { EchoDatabase } from '@dxos/echo-db';
7
+ import { Obj, Type } from '@dxos/echo';
8
+ import { type EchoDatabase } from '@dxos/echo-db';
10
9
  import { type HasId } from '@dxos/echo-schema';
11
- import { type SpaceId, type DXN } from '@dxos/keys';
10
+ import { assertArgument } from '@dxos/invariant';
11
+ import { type DXN, type SpaceId } from '@dxos/keys';
12
12
  import { type QueryResult } from '@dxos/protocols';
13
13
 
14
- import type { Services } from './services';
14
+ import { FunctionType } from './schema';
15
+ import { type Services } from './services';
15
16
 
16
17
  // TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
17
18
  // https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
@@ -44,8 +45,6 @@ export interface FunctionContext {
44
45
  */
45
46
  space: SpaceAPI | undefined;
46
47
 
47
- ai: AiServiceClient;
48
-
49
48
  /**
50
49
  * Resolves a service available to the function.
51
50
  * @throws if the service is not available.
@@ -87,7 +86,12 @@ const __assertFunctionSpaceIsCompatibleWithTheClientSpace = () => {
87
86
  // const _: SpaceAPI = {} as Space;
88
87
  };
89
88
 
90
- export type FunctionDefinition<T = {}, O = any> = {
89
+ const typeId = Symbol.for('@dxos/functions/FunctionDefinition');
90
+
91
+ export type FunctionDefinition<T = any, O = any> = {
92
+ [typeId]: true;
93
+ // TODO(dmaretskyi): Use `key` for FQN and `name` for human-readable-name.
94
+ key: string;
91
95
  name: string;
92
96
  description?: string;
93
97
  inputSchema: Schema.Schema<T, any>;
@@ -95,14 +99,17 @@ export type FunctionDefinition<T = {}, O = any> = {
95
99
  handler: FunctionHandler<T, O>;
96
100
  };
97
101
 
98
- // TODO(dmaretskyi): Bind input type to function handler.
99
- export const defineFunction = <T, O>({
100
- name,
101
- description,
102
- inputSchema,
103
- outputSchema = Schema.Any,
104
- handler,
105
- }: FunctionDefinition<T, O>): FunctionDefinition<T, O> => {
102
+ export const defineFunction: {
103
+ <I, O>(params: {
104
+ // TODO(dmaretskyi): Make `key` required.
105
+ key?: string;
106
+ name: string;
107
+ description?: string;
108
+ inputSchema: Schema.Schema<I, any>;
109
+ outputSchema?: Schema.Schema<O, any>;
110
+ handler: Types.NoInfer<FunctionHandler<I, O>>;
111
+ }): FunctionDefinition<I, O>;
112
+ } = ({ key, name, description, inputSchema, outputSchema = Schema.Any, handler }) => {
106
113
  if (!Schema.isSchema(inputSchema)) {
107
114
  throw new Error('Input schema must be a valid schema');
108
115
  }
@@ -110,11 +117,85 @@ export const defineFunction = <T, O>({
110
117
  throw new Error('Handler must be a function');
111
118
  }
112
119
 
120
+ // Captures the function definition location.
121
+ const limit = Error.stackTraceLimit;
122
+ Error.stackTraceLimit = 2;
123
+ const traceError = new Error();
124
+ Error.stackTraceLimit = limit;
125
+ let cache: false | string = false;
126
+ const captureStackTrace = () => {
127
+ if (cache !== false) {
128
+ return cache;
129
+ }
130
+ if (traceError.stack !== undefined) {
131
+ const stack = traceError.stack.split('\n');
132
+ if (stack[2] !== undefined) {
133
+ cache = stack[2].trim();
134
+ return cache;
135
+ }
136
+ }
137
+ };
138
+
139
+ const handlerWithSpan = (...args: any[]) => {
140
+ const result = (handler as any)(...args);
141
+ if (Effect.isEffect(result)) {
142
+ return Effect.withSpan(result, `${key ?? name}`, {
143
+ captureStackTrace,
144
+ });
145
+ }
146
+ return result;
147
+ };
148
+
113
149
  return {
150
+ [typeId]: true,
151
+ key: key ?? name,
114
152
  name,
115
153
  description,
116
154
  inputSchema,
117
155
  outputSchema,
118
- handler,
156
+ handler: handlerWithSpan,
157
+ };
158
+ };
159
+
160
+ export const FunctionDefinition = {
161
+ make: defineFunction,
162
+ isFunction: (value: unknown): value is FunctionDefinition.Any => {
163
+ return typeof value === 'object' && value !== null && Symbol.for('@dxos/functions/FunctionDefinition') in value;
164
+ },
165
+ serialize: (functionDef: FunctionDefinition.Any): FunctionType => {
166
+ assertArgument(FunctionDefinition.isFunction(functionDef), 'functionDef');
167
+ return serializeFunction(functionDef);
168
+ },
169
+ deserialize: (functionObj: FunctionType): FunctionDefinition.Any => {
170
+ assertArgument(Obj.instanceOf(FunctionType, functionObj), 'functionObj');
171
+ return deserializeFunction(functionObj);
172
+ },
173
+ };
174
+ export declare namespace FunctionDefinition {
175
+ export type Any = FunctionDefinition<any, any>;
176
+ export type Input<T extends FunctionDefinition> = T extends FunctionDefinition<infer I, any> ? I : never;
177
+ export type Output<T extends FunctionDefinition> = T extends FunctionDefinition<any, infer O> ? O : never;
178
+ }
179
+
180
+ export const serializeFunction = (functionDef: FunctionDefinition<any, any>): FunctionType =>
181
+ Obj.make(FunctionType, {
182
+ key: functionDef.name,
183
+ name: functionDef.name,
184
+ version: '0.1.0',
185
+ description: functionDef.description,
186
+ inputSchema: Type.toJsonSchema(functionDef.inputSchema),
187
+ outputSchema: !functionDef.outputSchema ? undefined : Type.toJsonSchema(functionDef.outputSchema),
188
+ });
189
+
190
+ export const deserializeFunction = (functionObj: FunctionType): FunctionDefinition<unknown, unknown> => {
191
+ return {
192
+ [typeId]: true,
193
+ // TODO(dmaretskyi): Fix key.
194
+ key: functionObj.key ?? functionObj.name,
195
+ name: functionObj.name,
196
+ description: functionObj.description,
197
+ inputSchema: !functionObj.inputSchema ? Schema.Unknown : Type.toEffectSchema(functionObj.inputSchema),
198
+ outputSchema: !functionObj.outputSchema ? undefined : Type.toEffectSchema(functionObj.outputSchema),
199
+ handler: () => {},
119
200
  };
120
201
  };
package/src/index.ts CHANGED
@@ -2,13 +2,13 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
+ export * from './errors';
5
6
  export * from './handler';
6
7
  export * from './schema';
7
8
  export * from './trace';
8
9
  export * from './types';
9
10
  export * from './url';
11
+ export * from './triggers';
10
12
  export * from './services';
11
13
  export * from './executor';
12
- export * from './errors';
13
-
14
- // Blow up cache
14
+ export * as exampleFunctions from './examples';