@dxos/functions 0.8.4-main.84f28bd → 0.8.4-main.ae835ea

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 (180) hide show
  1. package/dist/lib/browser/bundler/index.mjs +43 -34
  2. package/dist/lib/browser/bundler/index.mjs.map +3 -3
  3. package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
  4. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-M6EXIREF.mjs +610 -0
  6. package/dist/lib/browser/chunk-M6EXIREF.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 +1106 -252
  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 +94 -42
  13. package/dist/lib/browser/testing/index.mjs.map +4 -4
  14. package/dist/lib/node-esm/bundler/index.mjs +42 -34
  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-P3IATZMZ.mjs +612 -0
  19. package/dist/lib/node-esm/chunk-P3IATZMZ.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 +1106 -252
  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 +94 -42
  26. package/dist/lib/node-esm/testing/index.mjs.map +4 -4
  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/e2e/deploy.test.d.ts +2 -0
  30. package/dist/types/src/e2e/deploy.test.d.ts.map +1 -0
  31. package/dist/types/src/edge/functions.d.ts +4 -3
  32. package/dist/types/src/edge/functions.d.ts.map +1 -1
  33. package/dist/types/src/errors.d.ts +137 -0
  34. package/dist/types/src/errors.d.ts.map +1 -0
  35. package/dist/types/src/example/fib.d.ts +7 -0
  36. package/dist/types/src/example/fib.d.ts.map +1 -0
  37. package/dist/types/src/example/forex-effect.d.ts +3 -0
  38. package/dist/types/src/example/forex-effect.d.ts.map +1 -0
  39. package/dist/types/src/example/index.d.ts +12 -0
  40. package/dist/types/src/example/index.d.ts.map +1 -0
  41. package/dist/types/src/example/reply.d.ts +3 -0
  42. package/dist/types/src/example/reply.d.ts.map +1 -0
  43. package/dist/types/src/example/sleep.d.ts +5 -0
  44. package/dist/types/src/example/sleep.d.ts.map +1 -0
  45. package/dist/types/src/executor/executor.d.ts +7 -1
  46. package/dist/types/src/executor/executor.d.ts.map +1 -1
  47. package/dist/types/src/handler.d.ts +54 -13
  48. package/dist/types/src/handler.d.ts.map +1 -1
  49. package/dist/types/src/index.d.ts +5 -3
  50. package/dist/types/src/index.d.ts.map +1 -1
  51. package/dist/types/src/services/credentials.d.ts +21 -3
  52. package/dist/types/src/services/credentials.d.ts.map +1 -1
  53. package/dist/types/src/services/database.d.ts +58 -6
  54. package/dist/types/src/services/database.d.ts.map +1 -1
  55. package/dist/types/src/services/event-logger.d.ts +68 -30
  56. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  57. package/dist/types/src/services/function-invocation-service.d.ts +28 -0
  58. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
  59. package/dist/types/src/services/function-invocation-service.test.d.ts +2 -0
  60. package/dist/types/src/services/function-invocation-service.test.d.ts.map +1 -0
  61. package/dist/types/src/services/index.d.ts +5 -5
  62. package/dist/types/src/services/index.d.ts.map +1 -1
  63. package/dist/types/src/services/local-function-execution.d.ts +34 -0
  64. package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
  65. package/dist/types/src/services/queues.d.ts +33 -4
  66. package/dist/types/src/services/queues.d.ts.map +1 -1
  67. package/dist/types/src/services/remote-function-execution-service.d.ts +22 -0
  68. package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -0
  69. package/dist/types/src/services/service-container.d.ts +29 -18
  70. package/dist/types/src/services/service-container.d.ts.map +1 -1
  71. package/dist/types/src/services/service-registry.d.ts +31 -0
  72. package/dist/types/src/services/service-registry.d.ts.map +1 -0
  73. package/dist/types/src/services/service-registry.test.d.ts +2 -0
  74. package/dist/types/src/services/service-registry.test.d.ts.map +1 -0
  75. package/dist/types/src/services/tracing.d.ts +48 -4
  76. package/dist/types/src/services/tracing.d.ts.map +1 -1
  77. package/dist/types/src/testing/index.d.ts +1 -0
  78. package/dist/types/src/testing/index.d.ts.map +1 -1
  79. package/dist/types/src/testing/layer.d.ts +18 -0
  80. package/dist/types/src/testing/layer.d.ts.map +1 -0
  81. package/dist/types/src/testing/logger.d.ts +4 -4
  82. package/dist/types/src/testing/logger.d.ts.map +1 -1
  83. package/dist/types/src/testing/persist-database.test.d.ts +2 -0
  84. package/dist/types/src/testing/persist-database.test.d.ts.map +1 -0
  85. package/dist/types/src/testing/services.d.ts +8 -20
  86. package/dist/types/src/testing/services.d.ts.map +1 -1
  87. package/dist/types/src/trace.d.ts +21 -23
  88. package/dist/types/src/trace.d.ts.map +1 -1
  89. package/dist/types/src/translations.d.ts +2 -2
  90. package/dist/types/src/translations.d.ts.map +1 -1
  91. package/dist/types/src/triggers/index.d.ts +4 -0
  92. package/dist/types/src/triggers/index.d.ts.map +1 -0
  93. package/dist/types/src/triggers/input-builder.d.ts +3 -0
  94. package/dist/types/src/triggers/input-builder.d.ts.map +1 -0
  95. package/dist/types/src/triggers/invocation-tracer.d.ts +37 -0
  96. package/dist/types/src/triggers/invocation-tracer.d.ts.map +1 -0
  97. package/dist/types/src/triggers/trigger-dispatcher.d.ts +78 -0
  98. package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +1 -0
  99. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +2 -0
  100. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +1 -0
  101. package/dist/types/src/triggers/trigger-state-store.d.ts +28 -0
  102. package/dist/types/src/triggers/trigger-state-store.d.ts.map +1 -0
  103. package/dist/types/src/types/Function.d.ts +47 -0
  104. package/dist/types/src/types/Function.d.ts.map +1 -0
  105. package/dist/types/src/types/Script.d.ts +28 -0
  106. package/dist/types/src/types/Script.d.ts.map +1 -0
  107. package/dist/types/src/types/Trigger.d.ts +139 -0
  108. package/dist/types/src/types/Trigger.d.ts.map +1 -0
  109. package/dist/types/src/types/TriggerEvent.d.ts +44 -0
  110. package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
  111. package/dist/types/src/types/index.d.ts +5 -0
  112. package/dist/types/src/types/index.d.ts.map +1 -0
  113. package/dist/types/src/url.d.ts +11 -7
  114. package/dist/types/src/url.d.ts.map +1 -1
  115. package/dist/types/tsconfig.tsbuildinfo +1 -1
  116. package/package.json +37 -42
  117. package/src/bundler/bundler.test.ts +8 -9
  118. package/src/bundler/bundler.ts +38 -35
  119. package/src/e2e/deploy.test.ts +69 -0
  120. package/src/edge/functions.ts +9 -6
  121. package/src/errors.ts +21 -0
  122. package/src/example/fib.ts +32 -0
  123. package/src/example/forex-effect.ts +40 -0
  124. package/src/example/index.ts +13 -0
  125. package/src/example/reply.ts +21 -0
  126. package/src/example/sleep.ts +24 -0
  127. package/src/executor/executor.ts +14 -10
  128. package/src/handler.ts +139 -26
  129. package/src/index.ts +5 -5
  130. package/src/services/credentials.ts +93 -4
  131. package/src/services/database.ts +145 -20
  132. package/src/services/event-logger.ts +71 -37
  133. package/src/services/function-invocation-service.test.ts +81 -0
  134. package/src/services/function-invocation-service.ts +84 -0
  135. package/src/services/index.ts +5 -5
  136. package/src/services/local-function-execution.ts +153 -0
  137. package/src/services/queues.ts +50 -8
  138. package/src/services/remote-function-execution-service.ts +63 -0
  139. package/src/services/service-container.ts +46 -58
  140. package/src/services/service-registry.test.ts +45 -0
  141. package/src/services/service-registry.ts +63 -0
  142. package/src/services/tracing.ts +122 -6
  143. package/src/testing/index.ts +1 -0
  144. package/src/testing/layer.ts +114 -0
  145. package/src/testing/logger.ts +5 -4
  146. package/src/testing/persist-database.test.ts +87 -0
  147. package/src/testing/services.ts +12 -71
  148. package/src/trace.ts +20 -22
  149. package/src/translations.ts +2 -2
  150. package/src/triggers/index.ts +7 -0
  151. package/src/triggers/input-builder.ts +35 -0
  152. package/src/triggers/invocation-tracer.ts +101 -0
  153. package/src/triggers/trigger-dispatcher.test.ts +664 -0
  154. package/src/triggers/trigger-dispatcher.ts +521 -0
  155. package/src/triggers/trigger-state-store.ts +61 -0
  156. package/src/types/Function.ts +51 -0
  157. package/src/types/Script.ts +33 -0
  158. package/src/types/Trigger.ts +139 -0
  159. package/src/types/TriggerEvent.ts +62 -0
  160. package/src/types/index.ts +8 -0
  161. package/src/url.ts +14 -11
  162. package/dist/lib/browser/chunk-54U464M4.mjs +0 -360
  163. package/dist/lib/browser/chunk-54U464M4.mjs.map +0 -7
  164. package/dist/lib/node-esm/chunk-XDSX35BS.mjs +0 -362
  165. package/dist/lib/node-esm/chunk-XDSX35BS.mjs.map +0 -7
  166. package/dist/types/src/schema.d.ts +0 -38
  167. package/dist/types/src/schema.d.ts.map +0 -1
  168. package/dist/types/src/services/ai.d.ts +0 -12
  169. package/dist/types/src/services/ai.d.ts.map +0 -1
  170. package/dist/types/src/services/function-call-service.d.ts +0 -16
  171. package/dist/types/src/services/function-call-service.d.ts.map +0 -1
  172. package/dist/types/src/services/tool-resolver.d.ts +0 -14
  173. package/dist/types/src/services/tool-resolver.d.ts.map +0 -1
  174. package/dist/types/src/types.d.ts +0 -411
  175. package/dist/types/src/types.d.ts.map +0 -1
  176. package/src/schema.ts +0 -57
  177. package/src/services/ai.ts +0 -32
  178. package/src/services/function-call-service.ts +0 -64
  179. package/src/services/tool-resolver.ts +0 -31
  180. package/src/types.ts +0 -211
package/package.json CHANGED
@@ -1,30 +1,34 @@
1
1
  {
2
2
  "name": "@dxos/functions",
3
- "version": "0.8.4-main.84f28bd",
3
+ "version": "0.8.4-main.ae835ea",
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,46 +51,37 @@
47
51
  "src"
48
52
  ],
49
53
  "dependencies": {
50
- "@effect/platform": "0.88.0",
51
- "@preact/signals-core": "^1.9.0",
52
- "cron": "^3.1.6",
53
- "cron-schedule": "^5.0.4",
54
- "effect": "3.16.13",
54
+ "@effect/platform": "0.92.1",
55
+ "effect": "3.18.3",
55
56
  "esbuild-wasm": "^0.16.14",
56
- "express": "^4.19.2",
57
- "get-port-please": "^3.1.1",
58
57
  "i18next": "^24.2.1",
59
58
  "iso-did": "^1.6.0",
60
- "ws": "^8.14.2",
61
- "@dxos/ai": "0.8.4-main.84f28bd",
62
- "@dxos/client": "0.8.4-main.84f28bd",
63
- "@dxos/async": "0.8.4-main.84f28bd",
64
- "@dxos/context": "0.8.4-main.84f28bd",
65
- "@dxos/crypto": "0.8.4-main.84f28bd",
66
- "@dxos/echo": "0.8.4-main.84f28bd",
67
- "@dxos/echo-db": "0.8.4-main.84f28bd",
68
- "@dxos/echo-protocol": "0.8.4-main.84f28bd",
69
- "@dxos/echo-schema": "0.8.4-main.84f28bd",
70
- "@dxos/edge-client": "0.8.4-main.84f28bd",
71
- "@dxos/effect": "0.8.4-main.84f28bd",
72
- "@dxos/invariant": "0.8.4-main.84f28bd",
73
- "@dxos/keys": "0.8.4-main.84f28bd",
74
- "@dxos/live-object": "0.8.4-main.84f28bd",
75
- "@dxos/log": "0.8.4-main.84f28bd",
76
- "@dxos/node-std": "0.8.4-main.84f28bd",
77
- "@dxos/protocols": "0.8.4-main.84f28bd",
78
- "@dxos/schema": "0.8.4-main.84f28bd",
79
- "@dxos/util": "0.8.4-main.84f28bd"
80
- },
81
- "devDependencies": {
82
- "@types/express": "^4.17.17",
83
- "@types/ws": "^7.4.0",
84
- "@dxos/agent": "0.8.4-main.84f28bd"
59
+ "@dxos/ai": "0.8.4-main.ae835ea",
60
+ "@dxos/async": "0.8.4-main.ae835ea",
61
+ "@dxos/client": "0.8.4-main.ae835ea",
62
+ "@dxos/client-protocol": "0.8.4-main.ae835ea",
63
+ "@dxos/context": "0.8.4-main.ae835ea",
64
+ "@dxos/crypto": "0.8.4-main.ae835ea",
65
+ "@dxos/echo-db": "0.8.4-main.ae835ea",
66
+ "@dxos/debug": "0.8.4-main.ae835ea",
67
+ "@dxos/echo": "0.8.4-main.ae835ea",
68
+ "@dxos/echo-pipeline": "0.8.4-main.ae835ea",
69
+ "@dxos/echo-protocol": "0.8.4-main.ae835ea",
70
+ "@dxos/edge-client": "0.8.4-main.ae835ea",
71
+ "@dxos/effect": "0.8.4-main.ae835ea",
72
+ "@dxos/errors": "0.8.4-main.ae835ea",
73
+ "@dxos/invariant": "0.8.4-main.ae835ea",
74
+ "@dxos/kv-store": "0.8.4-main.ae835ea",
75
+ "@dxos/live-object": "0.8.4-main.ae835ea",
76
+ "@dxos/log": "0.8.4-main.ae835ea",
77
+ "@dxos/node-std": "0.8.4-main.ae835ea",
78
+ "@dxos/schema": "0.8.4-main.ae835ea",
79
+ "@dxos/protocols": "0.8.4-main.ae835ea",
80
+ "@dxos/util": "0.8.4-main.ae835ea",
81
+ "@dxos/keys": "0.8.4-main.ae835ea"
85
82
  },
86
83
  "publishConfig": {
87
84
  "access": "public"
88
85
  },
89
- "scripts": {
90
- "gen-schema": "ts-node ./tools/schema.ts"
91
- }
86
+ "scripts": {}
92
87
  }
@@ -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
  });
@@ -2,9 +2,13 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
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';
5
+ import * as FetchHttpClient from '@effect/platform/FetchHttpClient';
6
+ import * as HttpClient from '@effect/platform/HttpClient';
7
+ import * as Duration from 'effect/Duration';
8
+ import * as Effect from 'effect/Effect';
9
+ import * as Function from 'effect/Function';
10
+ import * as Schedule from 'effect/Schedule';
11
+ import { type BuildOptions, type BuildResult, type Loader, type Plugin, build, initialize } from 'esbuild-wasm';
8
12
 
9
13
  import { subtleCrypto } from '@dxos/crypto';
10
14
  import { runAndForwardErrors } from '@dxos/effect';
@@ -18,26 +22,26 @@ export type Import = {
18
22
  };
19
23
 
20
24
  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
25
  /**
28
26
  * Source code to bundle.
29
- * Required if `path` is not provided.
30
27
  */
31
- source?: string;
28
+ source: string;
32
29
  };
33
30
 
34
- export type BundleResult = {
35
- timestamp: number;
36
- sourceHash?: Buffer;
37
- imports?: Import[];
38
- bundle?: string;
39
- error?: any;
40
- };
31
+ export type BundleResult =
32
+ | {
33
+ timestamp: number;
34
+ sourceHash: Buffer;
35
+ error: unknown;
36
+ }
37
+ | {
38
+ timestamp: number;
39
+ sourceHash: Buffer;
40
+ imports: Import[];
41
+ entryPoint: string;
42
+ asset: Uint8Array;
43
+ bundle: string;
44
+ };
41
45
 
42
46
  export type BundlerOptions = {
43
47
  platform: BuildOptions['platform'];
@@ -58,16 +62,9 @@ export const initializeBundler = async (options: { wasmUrl: string }) => {
58
62
  export class Bundler {
59
63
  constructor(private readonly _options: BundlerOptions) {}
60
64
 
61
- async bundle({ path, source }: BundleOptions): Promise<BundleResult> {
65
+ async bundle({ source }: BundleOptions): Promise<BundleResult> {
62
66
  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
- };
67
+ const sourceHash = Buffer.from(await subtleCrypto.digest('SHA-256', Buffer.from(source)));
71
68
 
72
69
  if (this._options.platform === 'browser') {
73
70
  invariant(initialized, 'Compiler not initialized.');
@@ -83,7 +80,10 @@ export class Bundler {
83
80
  conditions: ['workerd', 'browser'],
84
81
  metafile: true,
85
82
  write: false,
86
- entryPoints: [path ?? 'memory:main.tsx'],
83
+ entryPoints: {
84
+ // Gets mapped to `userFunc.js` by esbuild.
85
+ userFunc: 'memory:main.tsx',
86
+ },
87
87
  bundle: true,
88
88
  format: 'esm',
89
89
  plugins: [
@@ -136,12 +136,17 @@ export class Bundler {
136
136
 
137
137
  log('compile complete', result.metafile);
138
138
 
139
- return await createResult({
139
+ const entryPoint = 'userFunc.js';
140
+ return {
141
+ timestamp: Date.now(),
142
+ sourceHash,
140
143
  imports: this.analyzeImports(result),
144
+ entryPoint,
145
+ asset: result.outputFiles![0].contents,
141
146
  bundle: result.outputFiles![0].text,
142
- });
147
+ };
143
148
  } catch (err) {
144
- return await createResult({ error: err });
149
+ return { timestamp: Date.now(), sourceHash, error: err };
145
150
  }
146
151
  }
147
152
 
@@ -153,7 +158,6 @@ export class Bundler {
153
158
  const parsedImports = allMatches(IMPORT_REGEX, result.outputFiles[0].text);
154
159
  return Object.values(result.metafile!.outputs)[0].imports.map((entry): Import => {
155
160
  const namedImports: string[] = [];
156
-
157
161
  const parsedImport = parsedImports.find((capture) => capture?.[4] === entry.path);
158
162
  if (parsedImport?.[2]) {
159
163
  NAMED_IMPORTS_REGEX.lastIndex = 0;
@@ -207,10 +211,9 @@ const IMPORT_REGEX =
207
211
  const NAMED_IMPORTS_REGEX = /[ \n\t]*{((?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+)}[ \n\t]*/gm;
208
212
 
209
213
  const allMatches = (regex: RegExp, str: string) => {
210
- regex.lastIndex = 0;
211
-
212
214
  let match;
213
215
  const matches = [];
216
+ regex.lastIndex = 0;
214
217
  while ((match = regex.exec(str))) {
215
218
  matches.push(match);
216
219
  }
@@ -278,7 +281,7 @@ const httpPlugin: Plugin = {
278
281
  return { contents: text, loader: 'jsx' as Loader };
279
282
  }).pipe(
280
283
  Effect.retry(
281
- pipe(
284
+ Function.pipe(
282
285
  Schedule.exponential(Duration.millis(INITIAL_DELAY)),
283
286
  Schedule.jittered,
284
287
  Schedule.intersect(Schedule.recurs(MAX_RETRIES - 1)),
@@ -0,0 +1,69 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { readFile } from 'node:fs/promises';
6
+
7
+ import { describe, expect, test } from 'vitest';
8
+
9
+ import { Client, Config } from '@dxos/client';
10
+ import { createEdgeIdentity } from '@dxos/client/edge';
11
+ import { Bundler } from '@dxos/functions/bundler';
12
+ import { uploadWorkerFunction } from '@dxos/functions/edge';
13
+ import { invariant } from '@dxos/invariant';
14
+ import { log } from '@dxos/log';
15
+
16
+ describe.runIf(process.env.DX_TEST_TAGS?.includes('functions-e2e'))('Functions deployment', () => {
17
+ test('deploys FOREX (effect) function and invokes it via EDGE (main)', { timeout: 120_000 }, async () => {
18
+ const config = new Config({
19
+ version: 1,
20
+ runtime: {
21
+ services: {
22
+ edge: { url: 'https://edge-main.dxos.workers.dev' },
23
+ },
24
+ },
25
+ });
26
+
27
+ const client = new Client({ config });
28
+ await client.initialize();
29
+ await client.halo.createIdentity();
30
+
31
+ const space = await client.spaces.create();
32
+ await space.waitUntilReady();
33
+
34
+ // Inline echo function source.
35
+ const source = await readFile(new URL('../examples/forex-effect.ts', import.meta.url), 'utf-8');
36
+
37
+ // Bundle and upload.
38
+ const bundler = new Bundler({ platform: 'node', sandboxedModules: [], remoteModules: {} });
39
+ const buildResult = await bundler.bundle({ source });
40
+ if ('error' in buildResult) {
41
+ throw buildResult.error ?? new Error('Bundle creation failed');
42
+ }
43
+
44
+ const { functionId } = await uploadWorkerFunction({
45
+ client,
46
+ ownerPublicKey: space.key,
47
+ version: '0.0.1',
48
+ entryPoint: buildResult.entryPoint,
49
+ assets: { [buildResult.entryPoint]: buildResult.asset },
50
+ name: 'e2e-echo',
51
+ });
52
+
53
+ expect(functionId).toBeDefined();
54
+
55
+ // Invoke deployed function via EDGE directly.
56
+ const edgeClient = client.edge;
57
+ invariant(edgeClient, 'edgeClient is required');
58
+ edgeClient.setIdentity(createEdgeIdentity(client));
59
+
60
+ const input = { from: 'USD', to: 'EUR' };
61
+ const result = await edgeClient.invokeFunction({ functionId }, input);
62
+ log.info('>>> result', { result, functionId });
63
+ const resultNumber = Number(result);
64
+ expect(resultNumber).toBeGreaterThan(0);
65
+ expect(resultNumber).toBeLessThan(100);
66
+
67
+ await client.destroy();
68
+ });
69
+ });
@@ -8,27 +8,30 @@ import { type Client } from '@dxos/client';
8
8
  import { createEdgeIdentity } from '@dxos/client/edge';
9
9
  import { EdgeHttpClient } from '@dxos/edge-client';
10
10
  import { invariant } from '@dxos/invariant';
11
- import type { PublicKey } from '@dxos/keys';
11
+ import { type PublicKey } from '@dxos/keys';
12
12
  import { log } from '@dxos/log';
13
13
  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 ADDED
@@ -0,0 +1,21 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { BaseError, type BaseErrorOptions } from '@dxos/errors';
6
+
7
+ export class ServiceNotAvailableError extends BaseError.extend('SERVICE_NOT_AVAILABLE', 'Service not available') {
8
+ constructor(service: string, options?: Omit<BaseErrorOptions, 'context'>) {
9
+ super({ context: { service }, ...options });
10
+ }
11
+ }
12
+
13
+ export class FunctionNotFoundError extends BaseError.extend('FUNCTION_NOT_FOUND', 'Function not found') {
14
+ constructor(functionKey: string, options?: Omit<BaseErrorOptions, 'context'>) {
15
+ super({ context: { function: functionKey }, ...options });
16
+ }
17
+ }
18
+
19
+ export class FunctionError extends BaseError.extend('FUNCTION_ERROR', 'Function invocation error') {}
20
+
21
+ export class TriggerStateNotFoundError extends BaseError.extend('TRIGGER_STATE_NOT_FOUND', 'Trigger state not found') {}
@@ -0,0 +1,32 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Effect from 'effect/Effect';
6
+ import * as Schema from 'effect/Schema';
7
+
8
+ import { defineFunction } from '../handler';
9
+
10
+ export default defineFunction({
11
+ key: 'example.org/function/fib',
12
+ name: 'Fibonacci',
13
+ description: 'Function that calculates a Fibonacci number',
14
+ inputSchema: Schema.Struct({
15
+ iterations: Schema.optional(Schema.Number).annotations({
16
+ description: 'Number of iterations',
17
+ default: 100_000,
18
+ }),
19
+ }),
20
+ outputSchema: Schema.Struct({
21
+ result: Schema.String,
22
+ }),
23
+ handler: Effect.fn(function* ({ data: { iterations = 100_000 } }) {
24
+ let a = 0n;
25
+ let b = 1n;
26
+ for (let i = 0; i < iterations; i++) {
27
+ a += b;
28
+ b = a - b;
29
+ }
30
+ return { result: a.toString() };
31
+ }),
32
+ });
@@ -0,0 +1,40 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ // @ts-ignore
6
+ import { S, defineFunction } from 'dxos:functions';
7
+ import {
8
+ FetchHttpClient,
9
+ HttpClient,
10
+ HttpClientRequest,
11
+ // @ts-ignore
12
+ } from 'https://esm.sh/@effect/platform@0.89.0?deps=effect@3.17.0&bundle=false';
13
+ // @ts-ignore
14
+ import { Effect, Schedule } from 'https://esm.sh/effect@3.17.0?bundle=false';
15
+
16
+ export default defineFunction({
17
+ key: 'dxos.org/script/forex-effect',
18
+ name: 'Forex Effect',
19
+ description: 'Returns the exchange rate between two currencies.',
20
+
21
+ inputSchema: S.Struct({
22
+ from: S.String.annotations({ description: 'The source currency' }),
23
+ to: S.String.annotations({ description: 'The target currency' }),
24
+ }),
25
+
26
+ outputSchema: S.String.annotations({ description: 'The exchange rate between the two currencies' }),
27
+
28
+ handler: async ({ data: { from, to } }: any) =>
29
+ Effect.gen(function* () {
30
+ const res = yield* HttpClientRequest.get(`https://free.ratesdb.com/v1/rates?from=${from}&to=${to}`).pipe(
31
+ HttpClient.execute,
32
+ Effect.flatMap((res: any) => res.json),
33
+ Effect.timeout('1 second'),
34
+ Effect.retry(Schedule.exponential(1_000).pipe(Schedule.compose(Schedule.recurs(3)))),
35
+ Effect.scoped,
36
+ );
37
+
38
+ return res.data.rates[to].toString();
39
+ }).pipe(Effect.provide(FetchHttpClient.layer)),
40
+ });
@@ -0,0 +1,13 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { default as fib$ } from './fib';
6
+ import { default as reply$ } from './reply';
7
+ import { default as sleep$ } from './sleep';
8
+
9
+ export namespace Example {
10
+ export const fib = fib$;
11
+ export const reply = reply$;
12
+ export const sleep = sleep$;
13
+ }
@@ -0,0 +1,21 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Console from 'effect/Console';
6
+ import * as Effect from 'effect/Effect';
7
+ import * as Schema from 'effect/Schema';
8
+
9
+ import { defineFunction } from '../handler';
10
+
11
+ export default defineFunction({
12
+ key: 'example.org/function/reply',
13
+ name: 'Reply',
14
+ description: 'Function that echoes the input',
15
+ inputSchema: Schema.Any,
16
+ outputSchema: Schema.Any,
17
+ handler: Effect.fn(function* ({ data }) {
18
+ yield* Console.log('reply', { data });
19
+ return data;
20
+ }),
21
+ });
@@ -0,0 +1,24 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Effect from 'effect/Effect';
6
+ import * as Schema from 'effect/Schema';
7
+
8
+ import { defineFunction } from '../handler';
9
+
10
+ export default defineFunction({
11
+ key: 'example.org/function/sleep',
12
+ name: 'Sleep',
13
+ description: 'Function that sleeps for a given amount of time',
14
+ inputSchema: Schema.Struct({
15
+ duration: Schema.optional(Schema.Number).annotations({
16
+ description: 'Milliseconds to sleep',
17
+ default: 100_000,
18
+ }),
19
+ }),
20
+ outputSchema: Schema.Void,
21
+ handler: Effect.fn(function* ({ data: { duration = 100_000 } }) {
22
+ yield* Effect.sleep(duration);
23
+ }),
24
+ });
@@ -2,38 +2,42 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Effect, Schema } from 'effect';
5
+ import * as Effect from 'effect/Effect';
6
+ import * as Schema from 'effect/Schema';
6
7
 
7
- import type { SpaceId } from '@dxos/client/echo';
8
+ import { type SpaceId } from '@dxos/client/echo';
8
9
  import { runAndForwardErrors } from '@dxos/effect';
9
10
 
10
11
  import type { FunctionContext, FunctionDefinition } from '../handler';
11
12
  import type { ServiceContainer, Services } from '../services';
12
13
 
14
+ /**
15
+ * @deprecated Use `FunctionInvocationService`
16
+ */
13
17
  export class FunctionExecutor {
14
18
  constructor(private readonly _services: ServiceContainer) {}
15
19
 
20
+ /**
21
+ * Invoke function.
22
+ */
16
23
  // TODO(dmaretskyi): Invocation context: queue, space, etc...
17
24
  async invoke<F extends FunctionDefinition<any, any>>(
18
- fnDef: F,
25
+ functionDef: F,
19
26
  input: F extends FunctionDefinition<infer I, infer _O> ? I : never,
20
27
  ): Promise<F extends FunctionDefinition<infer _I, infer O> ? O : never> {
21
28
  // Assert input matches schema
22
- const assertInput = fnDef.inputSchema.pipe(Schema.asserts);
29
+ const assertInput = functionDef.inputSchema.pipe(Schema.asserts);
23
30
  (assertInput as any)(input);
24
31
 
25
32
  const context: FunctionContext = {
33
+ space: undefined,
26
34
  getService: this._services.getService.bind(this._services),
27
35
  getSpace: async (_spaceId: SpaceId) => {
28
36
  throw new Error('Not available. Use the database service instead.');
29
37
  },
30
- space: undefined,
31
- get ai(): never {
32
- throw new Error('Not available. Use the ai service instead.');
33
- },
34
38
  };
35
39
 
36
- const result = fnDef.handler({ context, data: input });
40
+ const result = functionDef.handler({ context, data: input });
37
41
 
38
42
  let data: unknown;
39
43
  if (Effect.isEffect(result)) {
@@ -46,7 +50,7 @@ export class FunctionExecutor {
46
50
  }
47
51
 
48
52
  // Assert output matches schema
49
- const assertOutput = fnDef.outputSchema?.pipe(Schema.asserts);
53
+ const assertOutput = functionDef.outputSchema?.pipe(Schema.asserts);
50
54
  (assertOutput as any)(data);
51
55
 
52
56
  return data as any;