@dxos/functions 0.8.4-main.5ea62a8 → 0.8.4-main.66e292d

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 (166) hide show
  1. package/dist/lib/browser/index.mjs +874 -409
  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 +874 -409
  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 +87 -28
  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 +5 -7
  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 +100 -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 +7 -4
  38. package/dist/types/src/services/queues.d.ts.map +1 -1
  39. package/dist/types/src/services/tracing.d.ts +10 -12
  40. package/dist/types/src/services/tracing.d.ts.map +1 -1
  41. package/dist/types/src/types/Function.d.ts +63 -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 +141 -0
  46. package/dist/types/src/types/Trigger.d.ts.map +1 -0
  47. package/dist/types/src/types/TriggerEvent.d.ts +43 -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 +18 -72
  55. package/src/errors.ts +13 -5
  56. package/src/{examples → example}/fib.ts +5 -3
  57. package/src/example/forex-effect.ts +40 -0
  58. package/src/example/index.ts +13 -0
  59. package/src/{examples → example}/reply.ts +6 -3
  60. package/src/{examples → example}/sleep.ts +5 -3
  61. package/src/index.ts +5 -9
  62. package/src/{executor → protocol}/index.ts +1 -1
  63. package/src/protocol/protocol.test.ts +59 -0
  64. package/src/protocol/protocol.ts +156 -0
  65. package/src/sdk.ts +240 -0
  66. package/src/services/credentials.ts +12 -6
  67. package/src/services/event-logger.ts +12 -3
  68. package/src/services/function-invocation-service.ts +23 -0
  69. package/src/services/index.ts +7 -6
  70. package/src/services/queues.ts +9 -4
  71. package/src/services/tracing.ts +26 -52
  72. package/src/types/Function.ts +80 -0
  73. package/src/types/Script.ts +33 -0
  74. package/src/types/Trigger.ts +140 -0
  75. package/src/types/TriggerEvent.ts +62 -0
  76. package/src/types/index.ts +9 -0
  77. package/src/types/url.ts +31 -0
  78. package/dist/lib/browser/bundler/index.mjs +0 -263
  79. package/dist/lib/browser/bundler/index.mjs.map +0 -7
  80. package/dist/lib/browser/chunk-7NQ77AIQ.mjs +0 -618
  81. package/dist/lib/browser/chunk-7NQ77AIQ.mjs.map +0 -7
  82. package/dist/lib/browser/edge/index.mjs +0 -81
  83. package/dist/lib/browser/edge/index.mjs.map +0 -7
  84. package/dist/lib/browser/testing/index.mjs +0 -122
  85. package/dist/lib/browser/testing/index.mjs.map +0 -7
  86. package/dist/lib/node-esm/bundler/index.mjs +0 -265
  87. package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
  88. package/dist/lib/node-esm/chunk-KCGC6QQT.mjs +0 -620
  89. package/dist/lib/node-esm/chunk-KCGC6QQT.mjs.map +0 -7
  90. package/dist/lib/node-esm/edge/index.mjs +0 -83
  91. package/dist/lib/node-esm/edge/index.mjs.map +0 -7
  92. package/dist/lib/node-esm/testing/index.mjs +0 -123
  93. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  94. package/dist/types/src/bundler/bundler.d.ts +0 -49
  95. package/dist/types/src/bundler/bundler.d.ts.map +0 -1
  96. package/dist/types/src/bundler/bundler.test.d.ts +0 -2
  97. package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
  98. package/dist/types/src/bundler/index.d.ts +0 -2
  99. package/dist/types/src/bundler/index.d.ts.map +0 -1
  100. package/dist/types/src/edge/functions.d.ts +0 -17
  101. package/dist/types/src/edge/functions.d.ts.map +0 -1
  102. package/dist/types/src/edge/index.d.ts +0 -2
  103. package/dist/types/src/edge/index.d.ts.map +0 -1
  104. package/dist/types/src/examples/fib.d.ts.map +0 -1
  105. package/dist/types/src/examples/reply.d.ts.map +0 -1
  106. package/dist/types/src/examples/sleep.d.ts.map +0 -1
  107. package/dist/types/src/executor/executor.d.ts +0 -11
  108. package/dist/types/src/executor/executor.d.ts.map +0 -1
  109. package/dist/types/src/executor/index.d.ts +0 -2
  110. package/dist/types/src/executor/index.d.ts.map +0 -1
  111. package/dist/types/src/handler.d.ts +0 -65
  112. package/dist/types/src/handler.d.ts.map +0 -1
  113. package/dist/types/src/schema.d.ts +0 -43
  114. package/dist/types/src/schema.d.ts.map +0 -1
  115. package/dist/types/src/services/database.d.ts +0 -98
  116. package/dist/types/src/services/database.d.ts.map +0 -1
  117. package/dist/types/src/services/local-function-execution.d.ts +0 -12
  118. package/dist/types/src/services/local-function-execution.d.ts.map +0 -1
  119. package/dist/types/src/services/remote-function-execution-service.d.ts +0 -15
  120. package/dist/types/src/services/remote-function-execution-service.d.ts.map +0 -1
  121. package/dist/types/src/services/service-container.d.ts +0 -56
  122. package/dist/types/src/services/service-container.d.ts.map +0 -1
  123. package/dist/types/src/services/service-registry.d.ts +0 -29
  124. package/dist/types/src/services/service-registry.d.ts.map +0 -1
  125. package/dist/types/src/services/service-registry.test.d.ts +0 -2
  126. package/dist/types/src/services/service-registry.test.d.ts.map +0 -1
  127. package/dist/types/src/testing/index.d.ts +0 -3
  128. package/dist/types/src/testing/index.d.ts.map +0 -1
  129. package/dist/types/src/testing/layer.d.ts +0 -14
  130. package/dist/types/src/testing/layer.d.ts.map +0 -1
  131. package/dist/types/src/testing/logger.d.ts +0 -5
  132. package/dist/types/src/testing/logger.d.ts.map +0 -1
  133. package/dist/types/src/testing/persist-database.test.d.ts +0 -2
  134. package/dist/types/src/testing/persist-database.test.d.ts.map +0 -1
  135. package/dist/types/src/testing/services.d.ts +0 -59
  136. package/dist/types/src/testing/services.d.ts.map +0 -1
  137. package/dist/types/src/trace.d.ts +0 -150
  138. package/dist/types/src/trace.d.ts.map +0 -1
  139. package/dist/types/src/translations.d.ts +0 -12
  140. package/dist/types/src/translations.d.ts.map +0 -1
  141. package/dist/types/src/types.d.ts +0 -328
  142. package/dist/types/src/types.d.ts.map +0 -1
  143. package/dist/types/src/url.d.ts.map +0 -1
  144. package/src/bundler/bundler.test.ts +0 -58
  145. package/src/bundler/bundler.ts +0 -291
  146. package/src/bundler/index.ts +0 -5
  147. package/src/edge/functions.ts +0 -67
  148. package/src/edge/index.ts +0 -9
  149. package/src/executor/executor.ts +0 -54
  150. package/src/handler.ts +0 -122
  151. package/src/schema.ts +0 -68
  152. package/src/services/database.ts +0 -170
  153. package/src/services/local-function-execution.ts +0 -77
  154. package/src/services/remote-function-execution-service.ts +0 -46
  155. package/src/services/service-container.ts +0 -114
  156. package/src/services/service-registry.test.ts +0 -42
  157. package/src/services/service-registry.ts +0 -59
  158. package/src/testing/index.ts +0 -6
  159. package/src/testing/layer.ts +0 -97
  160. package/src/testing/logger.ts +0 -16
  161. package/src/testing/persist-database.test.ts +0 -87
  162. package/src/testing/services.ts +0 -115
  163. package/src/trace.ts +0 -178
  164. package/src/translations.ts +0 -20
  165. package/src/types.ts +0 -203
  166. 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.5ea62a8",
4
- "description": "Functions API and runtime.",
3
+ "version": "0.8.4-main.66e292d",
4
+ "description": "Functions API.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
7
  "license": "MIT",
@@ -14,87 +14,33 @@
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.5ea62a8",
66
- "@dxos/async": "0.8.4-main.5ea62a8",
67
- "@dxos/context": "0.8.4-main.5ea62a8",
68
- "@dxos/client": "0.8.4-main.5ea62a8",
69
- "@dxos/crypto": "0.8.4-main.5ea62a8",
70
- "@dxos/debug": "0.8.4-main.5ea62a8",
71
- "@dxos/echo-db": "0.8.4-main.5ea62a8",
72
- "@dxos/echo-pipeline": "0.8.4-main.5ea62a8",
73
- "@dxos/echo-schema": "0.8.4-main.5ea62a8",
74
- "@dxos/echo": "0.8.4-main.5ea62a8",
75
- "@dxos/echo-protocol": "0.8.4-main.5ea62a8",
76
- "@dxos/edge-client": "0.8.4-main.5ea62a8",
77
- "@dxos/effect": "0.8.4-main.5ea62a8",
78
- "@dxos/errors": "0.8.4-main.5ea62a8",
79
- "@dxos/invariant": "0.8.4-main.5ea62a8",
80
- "@dxos/keys": "0.8.4-main.5ea62a8",
81
- "@dxos/kv-store": "0.8.4-main.5ea62a8",
82
- "@dxos/live-object": "0.8.4-main.5ea62a8",
83
- "@dxos/log": "0.8.4-main.5ea62a8",
84
- "@dxos/protocols": "0.8.4-main.5ea62a8",
85
- "@dxos/schema": "0.8.4-main.5ea62a8",
86
- "@dxos/util": "0.8.4-main.5ea62a8",
87
- "@dxos/node-std": "0.8.4-main.5ea62a8"
88
- },
89
- "devDependencies": {
90
- "@types/express": "^4.17.17",
91
- "@types/ws": "^7.4.0",
92
- "@dxos/agent": "0.8.4-main.5ea62a8"
26
+ "@effect/platform": "0.92.1",
27
+ "effect": "3.18.3",
28
+ "@dxos/ai": "0.8.4-main.66e292d",
29
+ "@dxos/context": "0.8.4-main.66e292d",
30
+ "@dxos/echo-db": "0.8.4-main.66e292d",
31
+ "@dxos/effect": "0.8.4-main.66e292d",
32
+ "@dxos/echo": "0.8.4-main.66e292d",
33
+ "@dxos/errors": "0.8.4-main.66e292d",
34
+ "@dxos/invariant": "0.8.4-main.66e292d",
35
+ "@dxos/log": "0.8.4-main.66e292d",
36
+ "@dxos/node-std": "0.8.4-main.66e292d",
37
+ "@dxos/protocols": "0.8.4-main.66e292d",
38
+ "@dxos/keys": "0.8.4-main.66e292d",
39
+ "@dxos/types": "0.8.4-main.66e292d",
40
+ "@dxos/schema": "0.8.4-main.66e292d"
93
41
  },
94
42
  "publishConfig": {
95
43
  "access": "public"
96
44
  },
97
- "scripts": {
98
- "gen-schema": "ts-node ./tools/schema.ts"
99
- }
45
+ "scripts": {}
100
46
  }
package/src/errors.ts CHANGED
@@ -2,12 +2,20 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { BaseError } from '@dxos/errors';
5
+ import { BaseError, type BaseErrorOptions } from '@dxos/errors';
6
6
 
7
- export class ServiceNotAvailableError extends BaseError.extend('SERVICE_NOT_AVAILABLE') {
8
- constructor(serviceName: string) {
9
- super(`Service not available: ${serviceName}`);
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
10
  }
11
11
  }
12
12
 
13
- export class FunctionError extends BaseError.extend('FUNCTION_ERROR') {}
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') {}
@@ -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
@@ -2,13 +2,9 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- export * from './handler';
6
- export * from './schema';
7
- export * from './trace';
8
- export * from './types';
9
- export * from './url';
10
- export * from './services';
11
- export * from './executor';
12
5
  export * from './errors';
13
-
14
- // Blow up cache
6
+ export * from './example';
7
+ export * from './sdk';
8
+ export * from './services';
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,156 @@
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 { LifecycleState, Resource } from '@dxos/context';
12
+ import { Type } from '@dxos/echo';
13
+ import { EchoClient, type EchoDatabaseImpl, type QueueFactory } from '@dxos/echo-db';
14
+ import { assertState, 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
+ // eslint-disable-next-line no-useless-catch
51
+ try {
52
+ if (!SchemaAST.isAnyKeyword(func.inputSchema.ast)) {
53
+ try {
54
+ Schema.validateSync(func.inputSchema)(data);
55
+ } catch (error) {
56
+ throw new FunctionError({ message: 'Invalid input schema', cause: error });
57
+ }
58
+ }
59
+
60
+ await using funcContext = await new FunctionContext(context).open();
61
+
62
+ if (func.types.length > 0) {
63
+ invariant(funcContext.db, 'Database is required for functions with types');
64
+ funcContext.db.graph.schemaRegistry.addSchema(func.types);
65
+ }
66
+
67
+ let result = await func.handler({
68
+ // TODO(dmaretskyi): Fix the types.
69
+ context: context as any,
70
+ data,
71
+ });
72
+
73
+ if (Effect.isEffect(result)) {
74
+ result = await Effect.runPromise(
75
+ (result as Effect.Effect<unknown, unknown, FunctionServices>).pipe(
76
+ Effect.orDie,
77
+ Effect.provide(funcContext.createLayer()),
78
+ ),
79
+ );
80
+ }
81
+
82
+ if (func.outputSchema && !SchemaAST.isAnyKeyword(func.outputSchema.ast)) {
83
+ Schema.validateSync(func.outputSchema)(result);
84
+ }
85
+
86
+ return result;
87
+ } catch (error) {
88
+ // TODO(dmaretskyi): We might do error wrapping here and add extra context.
89
+ throw error;
90
+ }
91
+ },
92
+ };
93
+ };
94
+
95
+ /**
96
+ * Container for services and context for a function.
97
+ */
98
+ class FunctionContext extends Resource {
99
+ readonly context: FunctionProtocol.Context;
100
+ readonly client: EchoClient | undefined;
101
+ db: EchoDatabaseImpl | undefined;
102
+ queues: QueueFactory | undefined;
103
+
104
+ constructor(context: FunctionProtocol.Context) {
105
+ super();
106
+ this.context = context;
107
+ if (context.services.dataService && context.services.queryService) {
108
+ this.client = new EchoClient().connectToService({
109
+ dataService: context.services.dataService,
110
+ queryService: context.services.queryService,
111
+ queueService: context.services.queueService,
112
+ });
113
+ }
114
+ }
115
+
116
+ override async _open() {
117
+ await this.client?.open();
118
+ this.db =
119
+ this.client && this.context.spaceId
120
+ ? this.client.constructDatabase({
121
+ spaceId: this.context.spaceId ?? failedInvariant(),
122
+ spaceKey: PublicKey.fromHex(this.context.spaceKey ?? failedInvariant('spaceKey missing in context')),
123
+ reactiveSchemaQuery: false,
124
+ })
125
+ : undefined;
126
+
127
+ await this.db?.setSpaceRoot(this.context.spaceRootUrl ?? failedInvariant('spaceRootUrl missing in context'));
128
+ await this.db?.open();
129
+ this.queues =
130
+ this.client && this.context.spaceId ? this.client.constructQueueFactory(this.context.spaceId) : undefined;
131
+ }
132
+
133
+ override async _close() {
134
+ await this.db?.close();
135
+ await this.client?.close();
136
+ }
137
+
138
+ createLayer(): Layer.Layer<FunctionServices> {
139
+ assertState(this._lifecycleState === LifecycleState.OPEN, 'FunctionContext is not open');
140
+
141
+ const dbLayer = this.db ? DatabaseService.layer(this.db) : DatabaseService.notAvailable;
142
+ const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
143
+ const credentials = dbLayer
144
+ ? CredentialsService.layerFromDatabase().pipe(Layer.provide(dbLayer))
145
+ : CredentialsService.configuredLayer([]);
146
+ const functionInvocationService = MockedFunctionInvocationService;
147
+ const aiService = AiService.notAvailable;
148
+ const tracing = TracingService.layerNoop;
149
+
150
+ return Layer.mergeAll(dbLayer, queuesLayer, credentials, functionInvocationService, aiService, tracing);
151
+ }
152
+ }
153
+
154
+ const MockedFunctionInvocationService = Layer.succeed(FunctionInvocationService, {
155
+ invokeFunction: () => Effect.die('Calling functions from functions is not implemented yet.'),
156
+ });