@dxos/functions 0.8.4-main.e098934 → 0.8.4-main.e8ec1fe

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. package/dist/lib/browser/index.mjs +714 -1062
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +714 -1062
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/errors.d.ts +28 -36
  8. package/dist/types/src/errors.d.ts.map +1 -1
  9. package/dist/types/src/{examples → example}/fib.d.ts +1 -1
  10. package/dist/types/src/example/fib.d.ts.map +1 -0
  11. package/dist/types/src/example/forex-effect.d.ts +3 -0
  12. package/dist/types/src/example/forex-effect.d.ts.map +1 -0
  13. package/dist/types/src/example/index.d.ts +12 -0
  14. package/dist/types/src/example/index.d.ts.map +1 -0
  15. package/dist/types/src/{examples → example}/reply.d.ts +1 -1
  16. package/dist/types/src/example/reply.d.ts.map +1 -0
  17. package/dist/types/src/{examples → example}/sleep.d.ts +1 -1
  18. package/dist/types/src/example/sleep.d.ts.map +1 -0
  19. package/dist/types/src/index.d.ts +4 -8
  20. package/dist/types/src/index.d.ts.map +1 -1
  21. package/dist/types/src/protocol/index.d.ts +2 -0
  22. package/dist/types/src/protocol/index.d.ts.map +1 -0
  23. package/dist/types/src/protocol/protocol.d.ts +7 -0
  24. package/dist/types/src/protocol/protocol.d.ts.map +1 -0
  25. package/dist/types/src/protocol/protocol.test.d.ts +2 -0
  26. package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
  27. package/dist/types/src/sdk.d.ts +89 -0
  28. package/dist/types/src/sdk.d.ts.map +1 -0
  29. package/dist/types/src/services/credentials.d.ts +7 -3
  30. package/dist/types/src/services/credentials.d.ts.map +1 -1
  31. package/dist/types/src/services/event-logger.d.ts +20 -5
  32. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  33. package/dist/types/src/services/function-invocation-service.d.ts +11 -0
  34. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
  35. package/dist/types/src/services/index.d.ts +6 -6
  36. package/dist/types/src/services/index.d.ts.map +1 -1
  37. package/dist/types/src/services/queues.d.ts +3 -1
  38. package/dist/types/src/services/queues.d.ts.map +1 -1
  39. package/dist/types/src/services/tracing.d.ts +9 -12
  40. package/dist/types/src/services/tracing.d.ts.map +1 -1
  41. package/dist/types/src/types/Function.d.ts +58 -0
  42. package/dist/types/src/types/Function.d.ts.map +1 -0
  43. package/dist/types/src/types/Script.d.ts +28 -0
  44. package/dist/types/src/types/Script.d.ts.map +1 -0
  45. package/dist/types/src/types/Trigger.d.ts +139 -0
  46. package/dist/types/src/types/Trigger.d.ts.map +1 -0
  47. package/dist/types/src/types/TriggerEvent.d.ts +44 -0
  48. package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
  49. package/dist/types/src/types/index.d.ts +6 -0
  50. package/dist/types/src/types/index.d.ts.map +1 -0
  51. package/dist/types/src/{url.d.ts → types/url.d.ts} +1 -10
  52. package/dist/types/src/types/url.d.ts.map +1 -0
  53. package/dist/types/tsconfig.tsbuildinfo +1 -1
  54. package/package.json +16 -70
  55. package/src/{examples → example}/fib.ts +5 -3
  56. package/src/example/forex-effect.ts +40 -0
  57. package/src/example/index.ts +13 -0
  58. package/src/{examples → example}/reply.ts +6 -3
  59. package/src/{examples → example}/sleep.ts +5 -3
  60. package/src/index.ts +4 -8
  61. package/src/{executor → protocol}/index.ts +1 -1
  62. package/src/protocol/protocol.test.ts +59 -0
  63. package/src/protocol/protocol.ts +145 -0
  64. package/src/sdk.ts +226 -0
  65. package/src/services/credentials.ts +12 -6
  66. package/src/services/event-logger.ts +12 -3
  67. package/src/services/function-invocation-service.ts +23 -0
  68. package/src/services/index.ts +7 -6
  69. package/src/services/queues.ts +3 -1
  70. package/src/services/tracing.ts +24 -56
  71. package/src/{schema.ts → types/Function.ts} +20 -26
  72. package/src/types/Script.ts +33 -0
  73. package/src/types/Trigger.ts +139 -0
  74. package/src/types/TriggerEvent.ts +62 -0
  75. package/src/types/index.ts +9 -0
  76. package/src/types/url.ts +31 -0
  77. package/dist/lib/browser/bundler/index.mjs +0 -265
  78. package/dist/lib/browser/bundler/index.mjs.map +0 -7
  79. package/dist/lib/browser/chunk-D2XO7XXY.mjs +0 -611
  80. package/dist/lib/browser/chunk-D2XO7XXY.mjs.map +0 -7
  81. package/dist/lib/browser/chunk-J5LGTIGS.mjs +0 -10
  82. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +0 -7
  83. package/dist/lib/browser/edge/index.mjs +0 -83
  84. package/dist/lib/browser/edge/index.mjs.map +0 -7
  85. package/dist/lib/browser/testing/index.mjs +0 -129
  86. package/dist/lib/browser/testing/index.mjs.map +0 -7
  87. package/dist/lib/node-esm/bundler/index.mjs +0 -266
  88. package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
  89. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
  90. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +0 -7
  91. package/dist/lib/node-esm/chunk-Z4CJ62WS.mjs +0 -613
  92. package/dist/lib/node-esm/chunk-Z4CJ62WS.mjs.map +0 -7
  93. package/dist/lib/node-esm/edge/index.mjs +0 -84
  94. package/dist/lib/node-esm/edge/index.mjs.map +0 -7
  95. package/dist/lib/node-esm/testing/index.mjs +0 -130
  96. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  97. package/dist/types/src/bundler/bundler.d.ts +0 -49
  98. package/dist/types/src/bundler/bundler.d.ts.map +0 -1
  99. package/dist/types/src/bundler/bundler.test.d.ts +0 -2
  100. package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
  101. package/dist/types/src/bundler/index.d.ts +0 -2
  102. package/dist/types/src/bundler/index.d.ts.map +0 -1
  103. package/dist/types/src/edge/functions.d.ts +0 -17
  104. package/dist/types/src/edge/functions.d.ts.map +0 -1
  105. package/dist/types/src/edge/index.d.ts +0 -2
  106. package/dist/types/src/edge/index.d.ts.map +0 -1
  107. package/dist/types/src/examples/fib.d.ts.map +0 -1
  108. package/dist/types/src/examples/index.d.ts +0 -4
  109. package/dist/types/src/examples/index.d.ts.map +0 -1
  110. package/dist/types/src/examples/reply.d.ts.map +0 -1
  111. package/dist/types/src/examples/sleep.d.ts.map +0 -1
  112. package/dist/types/src/executor/executor.d.ts +0 -11
  113. package/dist/types/src/executor/executor.d.ts.map +0 -1
  114. package/dist/types/src/executor/index.d.ts +0 -2
  115. package/dist/types/src/executor/index.d.ts.map +0 -1
  116. package/dist/types/src/handler.d.ts +0 -94
  117. package/dist/types/src/handler.d.ts.map +0 -1
  118. package/dist/types/src/schema.d.ts +0 -43
  119. package/dist/types/src/schema.d.ts.map +0 -1
  120. package/dist/types/src/services/database.d.ts +0 -63
  121. package/dist/types/src/services/database.d.ts.map +0 -1
  122. package/dist/types/src/services/local-function-execution.d.ts +0 -25
  123. package/dist/types/src/services/local-function-execution.d.ts.map +0 -1
  124. package/dist/types/src/services/remote-function-execution-service.d.ts +0 -15
  125. package/dist/types/src/services/remote-function-execution-service.d.ts.map +0 -1
  126. package/dist/types/src/services/service-container.d.ts +0 -56
  127. package/dist/types/src/services/service-container.d.ts.map +0 -1
  128. package/dist/types/src/services/service-registry.d.ts +0 -29
  129. package/dist/types/src/services/service-registry.d.ts.map +0 -1
  130. package/dist/types/src/services/service-registry.test.d.ts +0 -2
  131. package/dist/types/src/services/service-registry.test.d.ts.map +0 -1
  132. package/dist/types/src/testing/index.d.ts +0 -3
  133. package/dist/types/src/testing/index.d.ts.map +0 -1
  134. package/dist/types/src/testing/layer.d.ts +0 -15
  135. package/dist/types/src/testing/layer.d.ts.map +0 -1
  136. package/dist/types/src/testing/logger.d.ts +0 -5
  137. package/dist/types/src/testing/logger.d.ts.map +0 -1
  138. package/dist/types/src/testing/persist-database.test.d.ts +0 -2
  139. package/dist/types/src/testing/persist-database.test.d.ts.map +0 -1
  140. package/dist/types/src/testing/services.d.ts +0 -59
  141. package/dist/types/src/testing/services.d.ts.map +0 -1
  142. package/dist/types/src/trace.d.ts +0 -122
  143. package/dist/types/src/trace.d.ts.map +0 -1
  144. package/dist/types/src/translations.d.ts +0 -12
  145. package/dist/types/src/translations.d.ts.map +0 -1
  146. package/dist/types/src/triggers/index.d.ts +0 -4
  147. package/dist/types/src/triggers/index.d.ts.map +0 -1
  148. package/dist/types/src/triggers/input-builder.d.ts +0 -3
  149. package/dist/types/src/triggers/input-builder.d.ts.map +0 -1
  150. package/dist/types/src/triggers/invocation-tracer.d.ts +0 -35
  151. package/dist/types/src/triggers/invocation-tracer.d.ts.map +0 -1
  152. package/dist/types/src/triggers/trigger-dispatcher.d.ts +0 -75
  153. package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +0 -1
  154. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +0 -2
  155. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +0 -1
  156. package/dist/types/src/triggers/trigger-state-store.d.ts +0 -27
  157. package/dist/types/src/triggers/trigger-state-store.d.ts.map +0 -1
  158. package/dist/types/src/types.d.ts +0 -211
  159. package/dist/types/src/types.d.ts.map +0 -1
  160. package/dist/types/src/url.d.ts.map +0 -1
  161. package/src/bundler/bundler.test.ts +0 -58
  162. package/src/bundler/bundler.ts +0 -291
  163. package/src/bundler/index.ts +0 -5
  164. package/src/edge/functions.ts +0 -67
  165. package/src/edge/index.ts +0 -9
  166. package/src/examples/index.ts +0 -7
  167. package/src/executor/executor.ts +0 -54
  168. package/src/handler.ts +0 -201
  169. package/src/services/database.ts +0 -171
  170. package/src/services/local-function-execution.ts +0 -114
  171. package/src/services/remote-function-execution-service.ts +0 -46
  172. package/src/services/service-container.ts +0 -114
  173. package/src/services/service-registry.test.ts +0 -42
  174. package/src/services/service-registry.ts +0 -59
  175. package/src/testing/index.ts +0 -6
  176. package/src/testing/layer.ts +0 -111
  177. package/src/testing/logger.ts +0 -16
  178. package/src/testing/persist-database.test.ts +0 -87
  179. package/src/testing/services.ts +0 -115
  180. package/src/trace.ts +0 -178
  181. package/src/translations.ts +0 -20
  182. package/src/triggers/index.ts +0 -7
  183. package/src/triggers/input-builder.ts +0 -35
  184. package/src/triggers/invocation-tracer.ts +0 -99
  185. package/src/triggers/trigger-dispatcher.test.ts +0 -652
  186. package/src/triggers/trigger-dispatcher.ts +0 -512
  187. package/src/triggers/trigger-state-store.ts +0 -60
  188. package/src/types.ts +0 -200
  189. package/src/url.ts +0 -55
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dxos/functions",
3
- "version": "0.8.4-main.e098934",
4
- "description": "Functions API and runtime.",
3
+ "version": "0.8.4-main.e8ec1fe",
4
+ "description": "Functions API.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
7
  "license": "MIT",
@@ -14,83 +14,29 @@
14
14
  "types": "./dist/types/src/index.d.ts",
15
15
  "browser": "./dist/lib/browser/index.mjs",
16
16
  "node": "./dist/lib/node-esm/index.mjs"
17
- },
18
- "./bundler": {
19
- "source": "./src/bundler/index.ts",
20
- "types": "./dist/types/src/bundler/index.d.ts",
21
- "browser": "./dist/lib/browser/bundler/index.mjs",
22
- "node": "./dist/lib/node-esm/bundler/index.mjs"
23
- },
24
- "./edge": {
25
- "source": "./src/edge/index.ts",
26
- "types": "./dist/types/src/edge/index.d.ts",
27
- "browser": "./dist/lib/browser/edge/index.mjs",
28
- "node": "./dist/lib/node-esm/edge/index.mjs"
29
- },
30
- "./testing": {
31
- "source": "./src/testing/index.ts",
32
- "types": "./dist/types/src/testing/index.d.ts",
33
- "browser": "./dist/lib/browser/testing/index.mjs",
34
- "node": "./dist/lib/node-esm/testing/index.mjs"
35
17
  }
36
18
  },
37
19
  "types": "dist/types/src/index.d.ts",
38
- "typesVersions": {
39
- "*": {
40
- "bundler": [
41
- "dist/types/src/bundler/index.d.ts"
42
- ],
43
- "edge": [
44
- "dist/types/src/edge/index.d.ts"
45
- ]
46
- }
47
- },
48
20
  "files": [
49
21
  "dist",
50
22
  "schema",
51
23
  "src"
52
24
  ],
53
25
  "dependencies": {
54
- "@effect/platform": "0.90.2",
55
- "@preact/signals-core": "^1.9.0",
56
- "cron": "^3.1.6",
57
- "cron-schedule": "^5.0.4",
58
- "effect": "3.17.7",
59
- "esbuild-wasm": "^0.16.14",
60
- "express": "^4.19.2",
61
- "get-port-please": "^3.1.1",
62
- "i18next": "^24.2.1",
63
- "iso-did": "^1.6.0",
64
- "ws": "^8.14.2",
65
- "@dxos/ai": "0.8.4-main.e098934",
66
- "@dxos/client": "0.8.4-main.e098934",
67
- "@dxos/async": "0.8.4-main.e098934",
68
- "@dxos/client-protocol": "0.8.4-main.e098934",
69
- "@dxos/context": "0.8.4-main.e098934",
70
- "@dxos/debug": "0.8.4-main.e098934",
71
- "@dxos/crypto": "0.8.4-main.e098934",
72
- "@dxos/echo-db": "0.8.4-main.e098934",
73
- "@dxos/echo-pipeline": "0.8.4-main.e098934",
74
- "@dxos/echo": "0.8.4-main.e098934",
75
- "@dxos/echo-protocol": "0.8.4-main.e098934",
76
- "@dxos/echo-schema": "0.8.4-main.e098934",
77
- "@dxos/edge-client": "0.8.4-main.e098934",
78
- "@dxos/effect": "0.8.4-main.e098934",
79
- "@dxos/errors": "0.8.4-main.e098934",
80
- "@dxos/invariant": "0.8.4-main.e098934",
81
- "@dxos/keys": "0.8.4-main.e098934",
82
- "@dxos/live-object": "0.8.4-main.e098934",
83
- "@dxos/kv-store": "0.8.4-main.e098934",
84
- "@dxos/log": "0.8.4-main.e098934",
85
- "@dxos/node-std": "0.8.4-main.e098934",
86
- "@dxos/protocols": "0.8.4-main.e098934",
87
- "@dxos/schema": "0.8.4-main.e098934",
88
- "@dxos/util": "0.8.4-main.e098934"
89
- },
90
- "devDependencies": {
91
- "@types/express": "^4.17.17",
92
- "@types/ws": "^7.4.0",
93
- "@dxos/agent": "0.8.4-main.e098934"
26
+ "@effect/platform": "0.92.1",
27
+ "effect": "3.18.3",
28
+ "@dxos/ai": "0.8.4-main.e8ec1fe",
29
+ "@dxos/echo-db": "0.8.4-main.e8ec1fe",
30
+ "@dxos/effect": "0.8.4-main.e8ec1fe",
31
+ "@dxos/errors": "0.8.4-main.e8ec1fe",
32
+ "@dxos/invariant": "0.8.4-main.e8ec1fe",
33
+ "@dxos/echo": "0.8.4-main.e8ec1fe",
34
+ "@dxos/keys": "0.8.4-main.e8ec1fe",
35
+ "@dxos/node-std": "0.8.4-main.e8ec1fe",
36
+ "@dxos/log": "0.8.4-main.e8ec1fe",
37
+ "@dxos/schema": "0.8.4-main.e8ec1fe",
38
+ "@dxos/types": "0.8.4-main.e8ec1fe",
39
+ "@dxos/protocols": "0.8.4-main.e8ec1fe"
94
40
  },
95
41
  "publishConfig": {
96
42
  "access": "public"
@@ -2,12 +2,14 @@
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 { defineFunction } from '../handler';
8
+ import { defineFunction } from '../sdk';
8
9
 
9
10
  export default defineFunction({
10
- name: 'example.org/function/fib',
11
+ key: 'example.org/function/fib',
12
+ name: 'Fibonacci',
11
13
  description: 'Function that calculates a Fibonacci number',
12
14
  inputSchema: Schema.Struct({
13
15
  iterations: Schema.optional(Schema.Number).annotations({
@@ -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
+ }
@@ -2,12 +2,15 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Console, Effect, Schema } from 'effect';
5
+ import * as Console from 'effect/Console';
6
+ import * as Effect from 'effect/Effect';
7
+ import * as Schema from 'effect/Schema';
6
8
 
7
- import { defineFunction } from '../handler';
9
+ import { defineFunction } from '../sdk';
8
10
 
9
11
  export default defineFunction({
10
- name: 'example.org/function/reply',
12
+ key: 'example.org/function/reply',
13
+ name: 'Reply',
11
14
  description: 'Function that echoes the input',
12
15
  inputSchema: Schema.Any,
13
16
  outputSchema: Schema.Any,
@@ -2,12 +2,14 @@
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 { defineFunction } from '../handler';
8
+ import { defineFunction } from '../sdk';
8
9
 
9
10
  export default defineFunction({
10
- name: 'example.org/function/sleep',
11
+ key: 'example.org/function/sleep',
12
+ name: 'Sleep',
11
13
  description: 'Function that sleeps for a given amount of time',
12
14
  inputSchema: Schema.Struct({
13
15
  duration: Schema.optional(Schema.Number).annotations({
package/src/index.ts CHANGED
@@ -3,12 +3,8 @@
3
3
  //
4
4
 
5
5
  export * from './errors';
6
- export * from './handler';
7
- export * from './schema';
8
- export * from './trace';
9
- export * from './types';
10
- export * from './url';
11
- export * from './triggers';
6
+ export * from './example';
7
+ export * from './sdk';
12
8
  export * from './services';
13
- export * from './executor';
14
- export * as exampleFunctions from './examples';
9
+ export * from './types';
10
+ export * from './protocol';
@@ -2,4 +2,4 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- export * from './executor';
5
+ export * from './protocol';
@@ -0,0 +1,59 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { describe, test } from 'vitest';
6
+
7
+ import { FunctionError } from '../errors';
8
+ import fibFunc from '../example/fib';
9
+ import replyFunc from '../example/reply';
10
+
11
+ import { wrapFunctionHandler } from './protocol';
12
+
13
+ describe('wrapFunctionHandler', () => {
14
+ test('wraps reply function and executes handler', async ({ expect }) => {
15
+ const wrapped = wrapFunctionHandler(replyFunc);
16
+
17
+ expect(wrapped.meta.key).toBe('example.org/function/reply');
18
+ expect(wrapped.meta.name).toBe('Reply');
19
+
20
+ const testData = { message: 'hello' };
21
+ const result = await wrapped.handler({
22
+ data: testData,
23
+ context: {
24
+ services: {},
25
+ },
26
+ });
27
+
28
+ expect(result).toEqual(testData);
29
+ });
30
+
31
+ test('wraps fibonacci function with valid input', async ({ expect }) => {
32
+ const wrapped = wrapFunctionHandler(fibFunc);
33
+
34
+ expect(wrapped.meta.key).toBe('example.org/function/fib');
35
+ expect(wrapped.meta.name).toBe('Fibonacci');
36
+
37
+ const result = await wrapped.handler({
38
+ data: { iterations: 10 },
39
+ context: {
40
+ services: {},
41
+ },
42
+ });
43
+
44
+ expect(result).toEqual({ result: '55' });
45
+ });
46
+
47
+ test('throws FunctionError on invalid input schema for fibonacci', async ({ expect }) => {
48
+ const wrapped = wrapFunctionHandler(fibFunc);
49
+
50
+ await expect(
51
+ wrapped.handler({
52
+ data: { iterations: 'invalid' },
53
+ context: {
54
+ services: {},
55
+ },
56
+ }),
57
+ ).rejects.toThrow(FunctionError);
58
+ });
59
+ });
@@ -0,0 +1,145 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Effect from 'effect/Effect';
6
+ import * as Layer from 'effect/Layer';
7
+ import * as Schema from 'effect/Schema';
8
+ import * as SchemaAST from 'effect/SchemaAST';
9
+
10
+ import { AiService } from '@dxos/ai';
11
+ import { Type } from '@dxos/echo';
12
+ import { EchoClient } from '@dxos/echo-db';
13
+ import { acquireReleaseResource } from '@dxos/effect';
14
+ import { failedInvariant, invariant } from '@dxos/invariant';
15
+ import { PublicKey } from '@dxos/keys';
16
+ import { type FunctionProtocol } from '@dxos/protocols';
17
+
18
+ import { FunctionError } from '../errors';
19
+ import { FunctionDefinition, type FunctionServices } from '../sdk';
20
+ import { CredentialsService, DatabaseService, FunctionInvocationService, TracingService } from '../services';
21
+ import { QueueService } from '../services';
22
+
23
+ /**
24
+ * Wraps a function handler made with `defineFunction` to a protocol that the functions-runtime expects.
25
+ */
26
+ export const wrapFunctionHandler = (func: FunctionDefinition): FunctionProtocol.Func => {
27
+ if (!FunctionDefinition.isFunction(func)) {
28
+ throw new TypeError('Invalid function definition');
29
+ }
30
+
31
+ return {
32
+ meta: {
33
+ key: func.key,
34
+ name: func.name,
35
+ description: func.description,
36
+ inputSchema: Type.toJsonSchema(func.inputSchema),
37
+ outputSchema: func.outputSchema === undefined ? undefined : Type.toJsonSchema(func.outputSchema),
38
+ services: func.services,
39
+ },
40
+ handler: async ({ data, context }) => {
41
+ if (
42
+ (func.services.includes(DatabaseService.key) || func.services.includes(QueueService.key)) &&
43
+ (!context.services.dataService || !context.services.queryService)
44
+ ) {
45
+ throw new FunctionError({
46
+ message: 'Services not provided: dataService, queryService',
47
+ });
48
+ }
49
+
50
+ try {
51
+ if (!SchemaAST.isAnyKeyword(func.inputSchema.ast)) {
52
+ Schema.validateSync(func.inputSchema)(data);
53
+ }
54
+
55
+ let result = await func.handler({
56
+ // TODO(dmaretskyi): Fix the types.
57
+ context: context as any,
58
+ data,
59
+ });
60
+
61
+ if (Effect.isEffect(result)) {
62
+ result = await Effect.runPromise(
63
+ (result as Effect.Effect<unknown, unknown, FunctionServices>).pipe(
64
+ Effect.orDie,
65
+ Effect.provide(createServiceLayer(context)),
66
+ ),
67
+ );
68
+ }
69
+
70
+ if (func.outputSchema && !SchemaAST.isAnyKeyword(func.outputSchema.ast)) {
71
+ Schema.validateSync(func.outputSchema)(result);
72
+ }
73
+
74
+ return result;
75
+ } catch (error) {
76
+ if (FunctionError.is(error)) {
77
+ throw error;
78
+ } else {
79
+ throw new FunctionError({
80
+ cause: error,
81
+ context: { func: func.key },
82
+ });
83
+ }
84
+ }
85
+ },
86
+ };
87
+ };
88
+
89
+ /**
90
+ * Creates a layer of services for the function.
91
+ */
92
+ const createServiceLayer = (context: FunctionProtocol.Context): Layer.Layer<FunctionServices> => {
93
+ return Layer.unwrapScoped(
94
+ Effect.gen(function* () {
95
+ let client: EchoClient | undefined;
96
+
97
+ if (context.services.dataService && context.services.queryService) {
98
+ client = yield* acquireReleaseResource(() => {
99
+ invariant(context.services.dataService && context.services.queryService);
100
+ // TODO(dmaretskyi): Queues service.
101
+ return new EchoClient().connectToService({
102
+ dataService: context.services.dataService,
103
+ queryService: context.services.queryService,
104
+ queueService: context.services.queueService,
105
+ });
106
+ });
107
+ }
108
+
109
+ const db =
110
+ client && context.spaceId
111
+ ? yield* acquireReleaseResource(() =>
112
+ client.constructDatabase({
113
+ spaceId: context.spaceId ?? failedInvariant(),
114
+ spaceKey: PublicKey.fromHex(context.spaceKey ?? failedInvariant('spaceKey missing in context')),
115
+ reactiveSchemaQuery: false,
116
+ }),
117
+ )
118
+ : undefined;
119
+
120
+ if (db) {
121
+ console.log('Setting space root', context.spaceRootUrl);
122
+ yield* Effect.promise(() =>
123
+ db!.setSpaceRoot(context.spaceRootUrl ?? failedInvariant('spaceRootUrl missing in context')),
124
+ );
125
+ }
126
+
127
+ const queues = client && context.spaceId ? client.constructQueueFactory(context.spaceId) : undefined;
128
+
129
+ const dbLayer = db ? DatabaseService.layer(db) : DatabaseService.notAvailable;
130
+ const queuesLayer = queues ? QueueService.layer(queues) : QueueService.notAvailable;
131
+ const credentials = dbLayer
132
+ ? CredentialsService.layerFromDatabase().pipe(Layer.provide(dbLayer))
133
+ : CredentialsService.configuredLayer([]);
134
+ const functionInvocationService = MockedFunctionInvocationService;
135
+ const aiService = AiService.notAvailable;
136
+ const tracing = TracingService.layerNoop;
137
+
138
+ return Layer.mergeAll(dbLayer, queuesLayer, credentials, functionInvocationService, aiService, tracing);
139
+ }),
140
+ );
141
+ };
142
+
143
+ const MockedFunctionInvocationService = Layer.succeed(FunctionInvocationService, {
144
+ invokeFunction: () => Effect.die('Calling functions from functions is not implemented yet.'),
145
+ });
package/src/sdk.ts ADDED
@@ -0,0 +1,226 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import type * as Context from 'effect/Context';
6
+ import * as Effect from 'effect/Effect';
7
+ import * as Schema from 'effect/Schema';
8
+
9
+ import { type AiService } from '@dxos/ai';
10
+ import { Obj, Type } from '@dxos/echo';
11
+ import { type DatabaseService } from '@dxos/echo-db';
12
+ import { assertArgument, failedInvariant } from '@dxos/invariant';
13
+
14
+ import {
15
+ type CredentialsService,
16
+ type FunctionInvocationService,
17
+ type QueueService,
18
+ type TracingService,
19
+ } from './services';
20
+ import { Function } from './types';
21
+ import { getUserFunctionIdInMetadata, setUserFunctionIdInMetadata } from './types';
22
+
23
+ // TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
24
+ // https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
25
+ // https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
26
+ // https://www.npmjs.com/package/aws-lambda
27
+
28
+ /**
29
+ * Services that are provided at the function call site by the caller.
30
+ */
31
+ export type InvocationServices = TracingService;
32
+
33
+ /**
34
+ * Services that are available to invoked functions.
35
+ */
36
+ export type FunctionServices =
37
+ | InvocationServices
38
+ | AiService.AiService
39
+ | CredentialsService
40
+ | DatabaseService
41
+ | QueueService
42
+ | FunctionInvocationService;
43
+
44
+ /**
45
+ * Function handler.
46
+ */
47
+ export type FunctionHandler<TData = {}, TOutput = any, S extends FunctionServices = FunctionServices> = (params: {
48
+ /**
49
+ * Context available to the function.
50
+ */
51
+ context: FunctionContext;
52
+
53
+ /**
54
+ * Data passed as the input to the function.
55
+ * Must match the function's input schema.
56
+ * This will be the payload from the trigger or other data passed into the function in a workflow.
57
+ */
58
+ data: TData;
59
+ }) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any, S>;
60
+
61
+ /**
62
+ * Function context.
63
+ */
64
+ export interface FunctionContext {
65
+ // TODO(dmaretskyi): Consider what we should put into context.
66
+ }
67
+
68
+ const typeId = Symbol.for('@dxos/functions/FunctionDefinition');
69
+
70
+ export type FunctionDefinition<T = any, O = any, S extends FunctionServices = FunctionServices> = {
71
+ [typeId]: true;
72
+ key: string;
73
+ name: string;
74
+ description?: string;
75
+ inputSchema: Schema.Schema<T, any>;
76
+ outputSchema?: Schema.Schema<O, any>;
77
+
78
+ /**
79
+ * Keys of the required services.
80
+ */
81
+ services: readonly string[];
82
+
83
+ handler: FunctionHandler<T, O, S>;
84
+ meta?: {
85
+ /**
86
+ * Tools that are projected from functions have this annotation.
87
+ *
88
+ * deployedFunctionId:
89
+ * - Backend deployment ID assigned by the EDGE function service (typically a UUID).
90
+ * - Used for remote invocation via `FunctionInvocationService` → `RemoteFunctionExecutionService`.
91
+ * - Persisted on the corresponding ECHO `Function.Function` object's metadata under the
92
+ * `FUNCTIONS_META_KEY` and retrieved with `getUserFunctionIdInMetadata`.
93
+ */
94
+ deployedFunctionId?: string;
95
+ };
96
+ };
97
+
98
+ export declare namespace FunctionDefinition {
99
+ export type Any = FunctionDefinition<any, any, any>;
100
+ export type Input<T extends Any> = T extends FunctionDefinition<infer I, infer _O, infer _S> ? I : never;
101
+ export type Output<T extends Any> = T extends FunctionDefinition<infer _I, infer O, infer _S> ? O : never;
102
+ export type Services<T extends Any> = T extends FunctionDefinition<infer _I, infer _O, infer S> ? S : never;
103
+ }
104
+
105
+ export type FunctionProps<T, O> = {
106
+ key: string;
107
+ name: string;
108
+ description?: string;
109
+ inputSchema: Schema.Schema<T, any>;
110
+ outputSchema?: Schema.Schema<O, any>;
111
+ // TODO(dmaretskyi): This currently doesn't cause a compile-time error if the handler requests a service that is not specified
112
+ services?: readonly Context.Tag<any, any>[];
113
+
114
+ handler: FunctionHandler<T, O, FunctionServices>;
115
+ };
116
+
117
+ // TODO(dmaretskyi): Output type doesn't get typechecked.
118
+ export const defineFunction: {
119
+ <I, O>(params: FunctionProps<I, O>): FunctionDefinition<I, O, FunctionServices>;
120
+ } = ({ key, name, description, inputSchema, outputSchema = Schema.Any, handler, services }) => {
121
+ if (!Schema.isSchema(inputSchema)) {
122
+ throw new Error('Input schema must be a valid schema');
123
+ }
124
+ if (typeof handler !== 'function') {
125
+ throw new Error('Handler must be a function');
126
+ }
127
+
128
+ // Captures the function definition location.
129
+ const limit = Error.stackTraceLimit;
130
+ Error.stackTraceLimit = 2;
131
+ const traceError = new Error();
132
+ Error.stackTraceLimit = limit;
133
+ let cache: false | string = false;
134
+ const captureStackTrace = () => {
135
+ if (cache !== false) {
136
+ return cache;
137
+ }
138
+ if (traceError.stack !== undefined) {
139
+ const stack = traceError.stack.split('\n');
140
+ if (stack[2] !== undefined) {
141
+ cache = stack[2].trim();
142
+ return cache;
143
+ }
144
+ }
145
+ };
146
+
147
+ const handlerWithSpan = (...args: any[]) => {
148
+ const result = (handler as any)(...args);
149
+ if (Effect.isEffect(result)) {
150
+ return Effect.withSpan(result, `${key ?? name}`, {
151
+ captureStackTrace,
152
+ });
153
+ }
154
+ return result;
155
+ };
156
+
157
+ return {
158
+ [typeId]: true,
159
+ key,
160
+ name,
161
+ description,
162
+ inputSchema,
163
+ outputSchema,
164
+ handler: handlerWithSpan,
165
+ services: !services ? [] : getServiceKeys(services),
166
+ } satisfies FunctionDefinition.Any;
167
+ };
168
+
169
+ const getServiceKeys = (services: readonly Context.Tag<any, any>[]) => {
170
+ return services.map((tag: any) => {
171
+ if (typeof tag.key === 'string') {
172
+ return tag.key;
173
+ }
174
+ console.log(tag);
175
+ failedInvariant();
176
+ });
177
+ };
178
+
179
+ export const FunctionDefinition = {
180
+ make: defineFunction,
181
+ isFunction: (value: unknown): value is FunctionDefinition.Any => {
182
+ return typeof value === 'object' && value !== null && Symbol.for('@dxos/functions/FunctionDefinition') in value;
183
+ },
184
+ serialize: (functionDef: FunctionDefinition.Any): Function.Function => {
185
+ assertArgument(FunctionDefinition.isFunction(functionDef), 'functionDef');
186
+ return serializeFunction(functionDef);
187
+ },
188
+ deserialize: (functionObj: Function.Function): FunctionDefinition.Any => {
189
+ assertArgument(Obj.instanceOf(Function.Function, functionObj), 'functionObj');
190
+ return deserializeFunction(functionObj);
191
+ },
192
+ };
193
+
194
+ export const serializeFunction = (functionDef: FunctionDefinition.Any): Function.Function => {
195
+ const fn = Function.make({
196
+ key: functionDef.key,
197
+ name: functionDef.name,
198
+ version: '0.1.0',
199
+ description: functionDef.description,
200
+ inputSchema: Type.toJsonSchema(functionDef.inputSchema),
201
+ outputSchema: !functionDef.outputSchema ? undefined : Type.toJsonSchema(functionDef.outputSchema),
202
+ services: functionDef.services,
203
+ });
204
+ if (functionDef.meta?.deployedFunctionId) {
205
+ setUserFunctionIdInMetadata(Obj.getMeta(fn), functionDef.meta.deployedFunctionId);
206
+ }
207
+ return fn;
208
+ };
209
+
210
+ export const deserializeFunction = (functionObj: Function.Function): FunctionDefinition<unknown, unknown> => {
211
+ return {
212
+ [typeId]: true,
213
+ // TODO(dmaretskyi): Fix key.
214
+ key: functionObj.key ?? functionObj.name,
215
+ name: functionObj.name,
216
+ description: functionObj.description,
217
+ inputSchema: !functionObj.inputSchema ? Schema.Unknown : Type.toEffectSchema(functionObj.inputSchema),
218
+ outputSchema: !functionObj.outputSchema ? undefined : Type.toEffectSchema(functionObj.outputSchema),
219
+ // TODO(dmaretskyi): This should throw error.
220
+ handler: () => {},
221
+ services: functionObj.services ?? [],
222
+ meta: {
223
+ deployedFunctionId: getUserFunctionIdInMetadata(Obj.getMeta(functionObj)),
224
+ },
225
+ };
226
+ };