@dxos/functions 0.8.4-main.406dc2a → 0.8.4-main.422d1c7879

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 (231) hide show
  1. package/README.md +4 -6
  2. package/dist/lib/neutral/Trace.mjs +42 -0
  3. package/dist/lib/neutral/chunk-27Y24OTY.mjs +167 -0
  4. package/dist/lib/neutral/chunk-27Y24OTY.mjs.map +7 -0
  5. package/dist/lib/neutral/chunk-IVDUS56O.mjs +49 -0
  6. package/dist/lib/neutral/chunk-IVDUS56O.mjs.map +7 -0
  7. package/dist/lib/neutral/fib-N45KAC7C.mjs +23 -0
  8. package/dist/lib/neutral/fib-N45KAC7C.mjs.map +7 -0
  9. package/dist/lib/neutral/index.mjs +1218 -0
  10. package/dist/lib/neutral/index.mjs.map +7 -0
  11. package/dist/lib/neutral/meta.json +1 -0
  12. package/dist/lib/neutral/reply-EUEPKNJF.mjs +19 -0
  13. package/dist/lib/neutral/reply-EUEPKNJF.mjs.map +7 -0
  14. package/dist/lib/neutral/sleep-PUK3D4FF.mjs +15 -0
  15. package/dist/lib/neutral/sleep-PUK3D4FF.mjs.map +7 -0
  16. package/dist/types/src/Trace.d.ts +174 -0
  17. package/dist/types/src/Trace.d.ts.map +1 -0
  18. package/dist/types/src/errors.d.ts +44 -60
  19. package/dist/types/src/errors.d.ts.map +1 -1
  20. package/dist/types/src/example/definitions.d.ts +11 -0
  21. package/dist/types/src/example/definitions.d.ts.map +1 -0
  22. package/dist/types/src/example/fib.d.ts +8 -0
  23. package/dist/types/src/example/fib.d.ts.map +1 -0
  24. package/dist/types/src/example/forex-effect.d.ts +3 -0
  25. package/dist/types/src/example/forex-effect.d.ts.map +1 -0
  26. package/dist/types/src/example/index.d.ts +4 -0
  27. package/dist/types/src/example/index.d.ts.map +1 -0
  28. package/dist/types/src/example/reply.d.ts +4 -0
  29. package/dist/types/src/example/reply.d.ts.map +1 -0
  30. package/dist/types/src/example/sleep.d.ts +6 -0
  31. package/dist/types/src/example/sleep.d.ts.map +1 -0
  32. package/dist/types/src/index.d.ts +8 -8
  33. package/dist/types/src/index.d.ts.map +1 -1
  34. package/dist/types/src/process/Process.d.ts +246 -0
  35. package/dist/types/src/process/Process.d.ts.map +1 -0
  36. package/dist/types/src/process/ServiceResolver.d.ts +74 -0
  37. package/dist/types/src/process/ServiceResolver.d.ts.map +1 -0
  38. package/dist/types/src/process/StorageService.d.ts +58 -0
  39. package/dist/types/src/process/StorageService.d.ts.map +1 -0
  40. package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
  41. package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
  42. package/dist/types/src/protocol/index.d.ts +2 -0
  43. package/dist/types/src/protocol/index.d.ts.map +1 -0
  44. package/dist/types/src/protocol/protocol.d.ts +7 -0
  45. package/dist/types/src/protocol/protocol.d.ts.map +1 -0
  46. package/dist/types/src/protocol/protocol.test.d.ts +2 -0
  47. package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
  48. package/dist/types/src/sdk.d.ts +11 -0
  49. package/dist/types/src/sdk.d.ts.map +1 -0
  50. package/dist/types/src/services/credentials.d.ts +6 -4
  51. package/dist/types/src/services/credentials.d.ts.map +1 -1
  52. package/dist/types/src/services/event-logger.d.ts +34 -32
  53. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  54. package/dist/types/src/services/function-invocation-service.d.ts +7 -19
  55. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
  56. package/dist/types/src/services/index.d.ts +3 -5
  57. package/dist/types/src/services/index.d.ts.map +1 -1
  58. package/dist/types/src/services/queues.d.ts +8 -5
  59. package/dist/types/src/services/queues.d.ts.map +1 -1
  60. package/dist/types/src/services/tracing.d.ts +1 -55
  61. package/dist/types/src/services/tracing.d.ts.map +1 -1
  62. package/dist/types/src/types/Script.d.ts +22 -0
  63. package/dist/types/src/types/Script.d.ts.map +1 -0
  64. package/dist/types/src/types/Trigger.d.ts +150 -0
  65. package/dist/types/src/types/Trigger.d.ts.map +1 -0
  66. package/dist/types/src/types/TriggerEvent.d.ts +75 -0
  67. package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
  68. package/dist/types/src/types/index.d.ts +5 -0
  69. package/dist/types/src/types/index.d.ts.map +1 -0
  70. package/dist/types/src/types/url.d.ts +13 -0
  71. package/dist/types/src/types/url.d.ts.map +1 -0
  72. package/dist/types/tsconfig.tsbuildinfo +1 -1
  73. package/package.json +29 -62
  74. package/src/Trace.ts +225 -0
  75. package/src/errors.ts +5 -5
  76. package/src/example/definitions.ts +49 -0
  77. package/src/example/fib.ts +23 -0
  78. package/src/example/forex-effect.ts +40 -0
  79. package/src/example/index.ts +12 -0
  80. package/src/example/reply.ts +19 -0
  81. package/src/example/sleep.ts +17 -0
  82. package/src/index.ts +8 -8
  83. package/src/process/Process.ts +487 -0
  84. package/src/process/ServiceResolver.ts +174 -0
  85. package/src/process/StorageService.ts +99 -0
  86. package/src/protocol/functions-ai-http-client.ts +67 -0
  87. package/src/{executor → protocol}/index.ts +1 -1
  88. package/src/protocol/protocol.test.ts +58 -0
  89. package/src/protocol/protocol.ts +273 -0
  90. package/src/sdk.ts +30 -0
  91. package/src/services/credentials.ts +32 -17
  92. package/src/services/event-logger.ts +11 -21
  93. package/src/services/function-invocation-service.ts +20 -69
  94. package/src/services/index.ts +3 -5
  95. package/src/services/queues.ts +15 -8
  96. package/src/services/tracing.ts +1 -136
  97. package/src/types/Script.ts +38 -0
  98. package/src/types/Trigger.ts +207 -0
  99. package/src/types/TriggerEvent.ts +62 -0
  100. package/src/types/index.ts +8 -0
  101. package/src/types/url.ts +32 -0
  102. package/dist/lib/browser/bundler/index.mjs +0 -269
  103. package/dist/lib/browser/bundler/index.mjs.map +0 -7
  104. package/dist/lib/browser/chunk-LKYT2SAL.mjs +0 -665
  105. package/dist/lib/browser/chunk-LKYT2SAL.mjs.map +0 -7
  106. package/dist/lib/browser/edge/index.mjs +0 -83
  107. package/dist/lib/browser/edge/index.mjs.map +0 -7
  108. package/dist/lib/browser/index.mjs +0 -1395
  109. package/dist/lib/browser/index.mjs.map +0 -7
  110. package/dist/lib/browser/meta.json +0 -1
  111. package/dist/lib/browser/testing/index.mjs +0 -131
  112. package/dist/lib/browser/testing/index.mjs.map +0 -7
  113. package/dist/lib/node-esm/bundler/index.mjs +0 -270
  114. package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
  115. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
  116. package/dist/lib/node-esm/chunk-NAQIKLZB.mjs +0 -667
  117. package/dist/lib/node-esm/chunk-NAQIKLZB.mjs.map +0 -7
  118. package/dist/lib/node-esm/edge/index.mjs +0 -84
  119. package/dist/lib/node-esm/edge/index.mjs.map +0 -7
  120. package/dist/lib/node-esm/index.mjs +0 -1396
  121. package/dist/lib/node-esm/index.mjs.map +0 -7
  122. package/dist/lib/node-esm/meta.json +0 -1
  123. package/dist/lib/node-esm/testing/index.mjs +0 -132
  124. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  125. package/dist/types/src/bundler/bundler.d.ts +0 -49
  126. package/dist/types/src/bundler/bundler.d.ts.map +0 -1
  127. package/dist/types/src/bundler/bundler.test.d.ts +0 -2
  128. package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
  129. package/dist/types/src/bundler/index.d.ts +0 -2
  130. package/dist/types/src/bundler/index.d.ts.map +0 -1
  131. package/dist/types/src/edge/functions.d.ts +0 -17
  132. package/dist/types/src/edge/functions.d.ts.map +0 -1
  133. package/dist/types/src/edge/index.d.ts +0 -2
  134. package/dist/types/src/edge/index.d.ts.map +0 -1
  135. package/dist/types/src/examples/fib.d.ts +0 -7
  136. package/dist/types/src/examples/fib.d.ts.map +0 -1
  137. package/dist/types/src/examples/index.d.ts +0 -4
  138. package/dist/types/src/examples/index.d.ts.map +0 -1
  139. package/dist/types/src/examples/reply.d.ts +0 -3
  140. package/dist/types/src/examples/reply.d.ts.map +0 -1
  141. package/dist/types/src/examples/sleep.d.ts +0 -5
  142. package/dist/types/src/examples/sleep.d.ts.map +0 -1
  143. package/dist/types/src/executor/executor.d.ts +0 -14
  144. package/dist/types/src/executor/executor.d.ts.map +0 -1
  145. package/dist/types/src/executor/index.d.ts +0 -2
  146. package/dist/types/src/executor/index.d.ts.map +0 -1
  147. package/dist/types/src/handler.d.ts +0 -109
  148. package/dist/types/src/handler.d.ts.map +0 -1
  149. package/dist/types/src/schema.d.ts +0 -43
  150. package/dist/types/src/schema.d.ts.map +0 -1
  151. package/dist/types/src/services/database.d.ts +0 -67
  152. package/dist/types/src/services/database.d.ts.map +0 -1
  153. package/dist/types/src/services/function-invocation-service.test.d.ts +0 -2
  154. package/dist/types/src/services/function-invocation-service.test.d.ts.map +0 -1
  155. package/dist/types/src/services/local-function-execution.d.ts +0 -34
  156. package/dist/types/src/services/local-function-execution.d.ts.map +0 -1
  157. package/dist/types/src/services/remote-function-execution-service.d.ts +0 -22
  158. package/dist/types/src/services/remote-function-execution-service.d.ts.map +0 -1
  159. package/dist/types/src/services/service-container.d.ts +0 -57
  160. package/dist/types/src/services/service-container.d.ts.map +0 -1
  161. package/dist/types/src/services/service-registry.d.ts +0 -31
  162. package/dist/types/src/services/service-registry.d.ts.map +0 -1
  163. package/dist/types/src/services/service-registry.test.d.ts +0 -2
  164. package/dist/types/src/services/service-registry.test.d.ts.map +0 -1
  165. package/dist/types/src/testing/index.d.ts +0 -3
  166. package/dist/types/src/testing/index.d.ts.map +0 -1
  167. package/dist/types/src/testing/layer.d.ts +0 -18
  168. package/dist/types/src/testing/layer.d.ts.map +0 -1
  169. package/dist/types/src/testing/logger.d.ts +0 -5
  170. package/dist/types/src/testing/logger.d.ts.map +0 -1
  171. package/dist/types/src/testing/persist-database.test.d.ts +0 -2
  172. package/dist/types/src/testing/persist-database.test.d.ts.map +0 -1
  173. package/dist/types/src/testing/services.d.ts +0 -59
  174. package/dist/types/src/testing/services.d.ts.map +0 -1
  175. package/dist/types/src/trace.d.ts +0 -122
  176. package/dist/types/src/trace.d.ts.map +0 -1
  177. package/dist/types/src/translations.d.ts +0 -12
  178. package/dist/types/src/translations.d.ts.map +0 -1
  179. package/dist/types/src/triggers/index.d.ts +0 -4
  180. package/dist/types/src/triggers/index.d.ts.map +0 -1
  181. package/dist/types/src/triggers/input-builder.d.ts +0 -3
  182. package/dist/types/src/triggers/input-builder.d.ts.map +0 -1
  183. package/dist/types/src/triggers/invocation-tracer.d.ts +0 -37
  184. package/dist/types/src/triggers/invocation-tracer.d.ts.map +0 -1
  185. package/dist/types/src/triggers/trigger-dispatcher.d.ts +0 -78
  186. package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +0 -1
  187. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +0 -2
  188. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +0 -1
  189. package/dist/types/src/triggers/trigger-state-store.d.ts +0 -28
  190. package/dist/types/src/triggers/trigger-state-store.d.ts.map +0 -1
  191. package/dist/types/src/types.d.ts +0 -230
  192. package/dist/types/src/types.d.ts.map +0 -1
  193. package/dist/types/src/url.d.ts +0 -21
  194. package/dist/types/src/url.d.ts.map +0 -1
  195. package/src/bundler/bundler.test.ts +0 -58
  196. package/src/bundler/bundler.ts +0 -295
  197. package/src/bundler/index.ts +0 -5
  198. package/src/edge/functions.ts +0 -67
  199. package/src/edge/index.ts +0 -9
  200. package/src/examples/fib.ts +0 -32
  201. package/src/examples/index.ts +0 -7
  202. package/src/examples/reply.ts +0 -21
  203. package/src/examples/sleep.ts +0 -24
  204. package/src/executor/executor.ts +0 -58
  205. package/src/handler.ts +0 -225
  206. package/src/schema.ts +0 -71
  207. package/src/services/database.ts +0 -175
  208. package/src/services/function-invocation-service.test.ts +0 -81
  209. package/src/services/local-function-execution.ts +0 -153
  210. package/src/services/remote-function-execution-service.ts +0 -63
  211. package/src/services/service-container.ts +0 -115
  212. package/src/services/service-registry.test.ts +0 -45
  213. package/src/services/service-registry.ts +0 -63
  214. package/src/testing/index.ts +0 -6
  215. package/src/testing/layer.ts +0 -114
  216. package/src/testing/logger.ts +0 -17
  217. package/src/testing/persist-database.test.ts +0 -87
  218. package/src/testing/services.ts +0 -115
  219. package/src/trace.ts +0 -178
  220. package/src/translations.ts +0 -20
  221. package/src/triggers/index.ts +0 -7
  222. package/src/triggers/input-builder.ts +0 -35
  223. package/src/triggers/invocation-tracer.ts +0 -101
  224. package/src/triggers/trigger-dispatcher.test.ts +0 -665
  225. package/src/triggers/trigger-dispatcher.ts +0 -533
  226. package/src/triggers/trigger-state-store.ts +0 -61
  227. package/src/types.ts +0 -218
  228. package/src/url.ts +0 -55
  229. /package/dist/lib/{browser/chunk-J5LGTIGS.mjs.map → neutral/Trace.mjs.map} +0 -0
  230. /package/dist/lib/{browser → neutral}/chunk-J5LGTIGS.mjs +0 -0
  231. /package/dist/lib/{node-esm/chunk-HSLMI22Q.mjs.map → neutral/chunk-J5LGTIGS.mjs.map} +0 -0
@@ -0,0 +1,99 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import * as Context from 'effect/Context';
6
+ import * as Effect from 'effect/Effect';
7
+ import * as Option from 'effect/Option';
8
+ import * as Pipeable from 'effect/Pipeable';
9
+ import * as Schema from 'effect/Schema';
10
+
11
+ export interface Service {
12
+ /** Read a value by key. Returns `None` if key does not exist. */
13
+ get<S extends Schema.Schema<any, string, any>>(
14
+ schema: S,
15
+ key: string,
16
+ ): Effect.Effect<Option.Option<Schema.Schema.Type<S>>, never, Schema.Schema.Context<S>>;
17
+
18
+ /** Write a value for the given key. */
19
+ set<S extends Schema.Schema<any, string, any>>(
20
+ schema: S,
21
+ key: string,
22
+ value: Schema.Schema.Type<S>,
23
+ ): Effect.Effect<void, never, Schema.Schema.Context<S>>;
24
+
25
+ /** Remove a key. */
26
+ delete(key: string): Effect.Effect<void>;
27
+
28
+ /** List all keys, optionally filtered by prefix. */
29
+ list(prefix?: string): Effect.Effect<readonly string[]>;
30
+
31
+ /** Remove all keys managed by this scoped store. */
32
+ clear(): Effect.Effect<void>;
33
+ }
34
+
35
+ /**
36
+ * Scoped key-value storage service for processes.
37
+ * Each process receives its own namespaced instance via the process manager.
38
+ * Construct a live implementation with `StorageService.layer` from `@dxos/functions-runtime`.
39
+ */
40
+ export class StorageService extends Context.Tag('@dxos/functions/StorageService')<StorageService, Service>() {}
41
+
42
+ export const get = Effect.serviceFunctionEffect(StorageService, (_) => _.get);
43
+ export const set = Effect.serviceFunctionEffect(StorageService, (_) => _.set);
44
+ export const deleteKey = Effect.serviceFunctionEffect(StorageService, (_) => _.delete);
45
+ export const list = Effect.serviceFunctionEffect(StorageService, (_) => _.list);
46
+ export const clear = Effect.serviceFunctionEffect(StorageService, (_) => _.clear);
47
+
48
+ /**
49
+ * Typed key in a storage service.
50
+ */
51
+ export interface Key<T> extends Pipeable.Pipeable {
52
+ readonly key: string;
53
+
54
+ get: Effect.Effect<Option.Option<T>, never, StorageService>;
55
+ set(value: T): Effect.Effect<void, never, StorageService>;
56
+ delete(): Effect.Effect<void, never, StorageService>;
57
+ }
58
+
59
+ /**
60
+ * Create a typed key in a storage service.
61
+ */
62
+ export const key = <S extends Schema.Schema<any, string, any>>(schema: S, key: string): Key<Schema.Schema.Type<S>> => {
63
+ return {
64
+ key,
65
+ get: get(schema, key),
66
+ set: (value: Schema.Schema.Type<S>) => set(schema, key, value),
67
+ delete: () => deleteKey(key),
68
+ pipe(...args: any) {
69
+ return Pipeable.pipeArguments(this, arguments);
70
+ },
71
+ };
72
+ };
73
+
74
+ /**
75
+ * Typed key in a storage service with a default value.
76
+ */
77
+ export interface KeyWithDefault<T, U> extends Pipeable.Pipeable {
78
+ readonly key: string;
79
+ get: Effect.Effect<T | U, never, StorageService>;
80
+ set(value: U): Effect.Effect<void, never, StorageService>;
81
+ delete(): Effect.Effect<void, never, StorageService>;
82
+ }
83
+
84
+ /**
85
+ * Assign a default value to a key if it is not present.
86
+ */
87
+ export const withDefault =
88
+ <T>(getDefault: () => NoInfer<T>) =>
89
+ (key: Key<T>): KeyWithDefault<T, T> => {
90
+ return {
91
+ key: key.key,
92
+ get: key.get.pipe(Effect.map(Option.getOrElse(() => getDefault()))),
93
+ set: (value) => key.set(value),
94
+ delete: () => key.delete(),
95
+ pipe(...args: any) {
96
+ return Pipeable.pipeArguments(this, arguments);
97
+ },
98
+ };
99
+ };
@@ -0,0 +1,67 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Headers from '@effect/platform/Headers';
6
+ import * as HttpClient from '@effect/platform/HttpClient';
7
+ import * as HttpClientError from '@effect/platform/HttpClientError';
8
+ import * as HttpClientResponse from '@effect/platform/HttpClientResponse';
9
+ import * as Effect from 'effect/Effect';
10
+ import * as FiberRef from 'effect/FiberRef';
11
+ import * as Layer from 'effect/Layer';
12
+ import * as Stream from 'effect/Stream';
13
+
14
+ import { log } from '@dxos/log';
15
+ import { type EdgeFunctionEnv, ErrorCodec } from '@dxos/protocols';
16
+ /**
17
+ * Copy pasted from https://github.com/Effect-TS/effect/blob/main/packages/platform/src/internal/fetchHttpClient.ts
18
+ */
19
+ export const requestInitTagKey = '@effect/platform/FetchHttpClient/FetchOptions';
20
+
21
+ export class FunctionsAiHttpClient {
22
+ static make = (service: EdgeFunctionEnv.FunctionsAiService) =>
23
+ HttpClient.make((request, url, signal, fiber) => {
24
+ const context = fiber.getFiberRef(FiberRef.currentContext);
25
+ const options: RequestInit = context.unsafeMap.get(requestInitTagKey) ?? {};
26
+ const headers = options.headers
27
+ ? Headers.merge(Headers.fromInput(options.headers), request.headers)
28
+ : request.headers;
29
+
30
+ const send = (body: BodyInit | undefined) =>
31
+ Effect.tryPromise({
32
+ try: () =>
33
+ service.fetch(
34
+ new Request(url, {
35
+ ...options,
36
+ method: request.method,
37
+ headers,
38
+ body,
39
+ // Note: Don't pass signal - it can't be serialized through RPC
40
+ }),
41
+ ),
42
+ catch: (cause) => {
43
+ log.error('Failed to fetch', { errorSerialized: ErrorCodec.encode(cause as Error) });
44
+ return new HttpClientError.RequestError({
45
+ request,
46
+ reason: 'Transport',
47
+ cause,
48
+ });
49
+ },
50
+ }).pipe(Effect.map((response) => HttpClientResponse.fromWeb(request, response)));
51
+
52
+ switch (request.body._tag) {
53
+ case 'Raw':
54
+ case 'Uint8Array':
55
+ return send(request.body.body as any);
56
+ case 'FormData':
57
+ return send(request.body.formData);
58
+ case 'Stream':
59
+ return Stream.toReadableStreamEffect(request.body.stream).pipe(Effect.flatMap(send));
60
+ }
61
+
62
+ return send(undefined);
63
+ });
64
+
65
+ static layer = (service: EdgeFunctionEnv.FunctionsAiService) =>
66
+ Layer.succeed(HttpClient.HttpClient, FunctionsAiHttpClient.make(service));
67
+ }
@@ -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,58 @@
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
+ import { wrapFunctionHandler } from './protocol';
11
+
12
+ describe('wrapFunctionHandler', () => {
13
+ test('wraps reply function and executes handler', async ({ expect }) => {
14
+ const wrapped = wrapFunctionHandler(replyFunc);
15
+
16
+ expect(wrapped.meta.key).toBe('example.org/function/reply');
17
+ expect(wrapped.meta.name).toBe('Reply');
18
+
19
+ const testData = { message: 'hello' };
20
+ const result = await wrapped.handler({
21
+ data: testData,
22
+ context: {
23
+ services: {},
24
+ },
25
+ });
26
+
27
+ expect(result).toEqual(testData);
28
+ });
29
+
30
+ test('wraps fibonacci function with valid input', async ({ expect }) => {
31
+ const wrapped = wrapFunctionHandler(fibFunc);
32
+
33
+ expect(wrapped.meta.key).toBe('example.org/function/fib');
34
+ expect(wrapped.meta.name).toBe('Fibonacci');
35
+
36
+ const result = await wrapped.handler({
37
+ data: { iterations: 10 },
38
+ context: {
39
+ services: {},
40
+ },
41
+ });
42
+
43
+ expect(result).toEqual({ result: '55' });
44
+ });
45
+
46
+ test('throws FunctionError on invalid input schema for fibonacci', async ({ expect }) => {
47
+ const wrapped = wrapFunctionHandler(fibFunc);
48
+
49
+ await expect(
50
+ wrapped.handler({
51
+ data: { iterations: 'invalid' },
52
+ context: {
53
+ services: {},
54
+ },
55
+ }),
56
+ ).rejects.toThrow(FunctionError);
57
+ });
58
+ });
@@ -0,0 +1,273 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as AnthropicClient from '@effect/ai-anthropic/AnthropicClient';
6
+ import * as Effect from 'effect/Effect';
7
+ import * as Layer from 'effect/Layer';
8
+ import * as Schema from 'effect/Schema';
9
+ import * as SchemaAST from 'effect/SchemaAST';
10
+
11
+ import { AiModelResolver, AiService } from '@dxos/ai';
12
+ import { AnthropicResolver } from '@dxos/ai/resolvers';
13
+ import { LifecycleState, Resource } from '@dxos/context';
14
+ import { Database, Feed, JsonSchema, Ref, type Type } from '@dxos/echo';
15
+ import { EchoClient, type EchoDatabaseImpl, type QueueFactory, createFeedServiceLayer } from '@dxos/echo-db';
16
+ import { refFromEncodedReference } from '@dxos/echo/internal';
17
+ import { runAndForwardErrors } from '@dxos/effect';
18
+ import { assertState, failedInvariant, invariant } from '@dxos/invariant';
19
+ import { PublicKey } from '@dxos/keys';
20
+ import { Operation } from '@dxos/operation';
21
+ import { type FunctionProtocol } from '@dxos/protocols';
22
+
23
+ import { FunctionError } from '../errors';
24
+ import { type FunctionServices } from '../sdk';
25
+ import { CredentialsService, FunctionInvocationService, QueueService } from '../services';
26
+ import * as Trace from '../Trace';
27
+ import { FunctionsAiHttpClient } from './functions-ai-http-client';
28
+
29
+ /**
30
+ * Wraps a function handler made with `defineFunction` to a protocol that the functions-runtime expects.
31
+ */
32
+ export const wrapFunctionHandler = (func: Operation.WithHandler<Operation.Definition.Any>): FunctionProtocol.Func => {
33
+ if (!Operation.isOperationWithHandler(func)) {
34
+ throw new TypeError('Expected operation with handler');
35
+ }
36
+
37
+ const serviceTags = func.services.map((service) => service.key);
38
+
39
+ return {
40
+ meta: {
41
+ key: func.meta.key,
42
+ name: func.meta.name,
43
+ description: func.meta.description,
44
+ inputSchema: JsonSchema.toJsonSchema(func.input),
45
+ outputSchema: func.output === undefined ? undefined : JsonSchema.toJsonSchema(func.output),
46
+ services: func.services.map((service) => service.key),
47
+ },
48
+ handler: async ({ data, context }) => {
49
+ if (
50
+ (serviceTags.includes(Database.Service.key) ||
51
+ serviceTags.includes(QueueService.key) ||
52
+ serviceTags.includes(Feed.FeedService.key)) &&
53
+ (!context.services.dataService || !context.services.queryService)
54
+ ) {
55
+ throw new FunctionError({
56
+ message: 'Services not provided: dataService, queryService',
57
+ });
58
+ }
59
+
60
+ // eslint-disable-next-line no-useless-catch
61
+ try {
62
+ if (!SchemaAST.isAnyKeyword(func.input.ast)) {
63
+ try {
64
+ Schema.validateSync(func.input)(data);
65
+ } catch (error) {
66
+ throw new FunctionError({ message: 'Invalid input schema', cause: error });
67
+ }
68
+ }
69
+
70
+ await using funcContext = await new FunctionContext(context).open();
71
+
72
+ if (func.types.length > 0) {
73
+ invariant(funcContext.db, 'Database is required for functions with types');
74
+ await funcContext.db.graph.schemaRegistry.register(func.types as Type.AnyEntity[]);
75
+ }
76
+
77
+ const dataWithDecodedRefs =
78
+ funcContext.db && !SchemaAST.isAnyKeyword(func.input.ast)
79
+ ? decodeRefsFromSchema(func.input.ast, data, funcContext.db)
80
+ : data;
81
+
82
+ let result: any = await func.handler(dataWithDecodedRefs);
83
+
84
+ if (Effect.isEffect(result)) {
85
+ result = await runAndForwardErrors(
86
+ (result as Effect.Effect<unknown, unknown, FunctionServices>).pipe(
87
+ Effect.orDie,
88
+ Effect.provide(funcContext.createLayer()),
89
+ ),
90
+ );
91
+ }
92
+
93
+ if (func.output && !SchemaAST.isAnyKeyword(func.output.ast)) {
94
+ Schema.validateSync(func.output)(result);
95
+ }
96
+
97
+ return result;
98
+ } catch (error) {
99
+ // TODO(dmaretskyi): We might do error wrapping here and add extra context.
100
+ throw error;
101
+ }
102
+ },
103
+ };
104
+ };
105
+
106
+ /**
107
+ * Container for services and context for a function.
108
+ */
109
+ class FunctionContext extends Resource {
110
+ readonly context: FunctionProtocol.Context;
111
+ readonly client: EchoClient | undefined;
112
+ db: EchoDatabaseImpl | undefined;
113
+ queues: QueueFactory | undefined;
114
+
115
+ constructor(context: FunctionProtocol.Context) {
116
+ super();
117
+ this.context = context;
118
+ if (context.services.dataService && context.services.queryService) {
119
+ this.client = new EchoClient().connectToService({
120
+ dataService: context.services.dataService,
121
+ queryService: context.services.queryService,
122
+ queueService: context.services.queueService,
123
+ });
124
+ }
125
+ }
126
+
127
+ override async _open() {
128
+ await this.client?.open();
129
+ this.db =
130
+ this.client && this.context.spaceId
131
+ ? this.client.constructDatabase({
132
+ spaceId: this.context.spaceId ?? failedInvariant(),
133
+ spaceKey: PublicKey.fromHex(this.context.spaceKey ?? failedInvariant('spaceKey missing in context')),
134
+ reactiveSchemaQuery: false,
135
+ preloadSchemaOnOpen: false,
136
+ })
137
+ : undefined;
138
+
139
+ await this.db?.setSpaceRoot(this.context.spaceRootUrl ?? failedInvariant('spaceRootUrl missing in context'));
140
+ await this.db?.open();
141
+ this.queues =
142
+ this.client && this.context.spaceId ? this.client.constructQueueFactory(this.context.spaceId) : undefined;
143
+ }
144
+
145
+ override async _close() {
146
+ await this.db?.close();
147
+ await this.client?.close();
148
+ }
149
+
150
+ createLayer(): Layer.Layer<FunctionServices> {
151
+ assertState(this._lifecycleState === LifecycleState.OPEN, 'FunctionContext is not open');
152
+
153
+ const dbLayer = this.db ? Database.layer(this.db) : Database.notAvailable;
154
+ const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
155
+ const feedLayer = this.queues ? createFeedServiceLayer(this.queues) : Feed.notAvailable;
156
+ const credentials = dbLayer
157
+ ? CredentialsService.layerFromDatabase({ caching: true }).pipe(Layer.provide(dbLayer))
158
+ : CredentialsService.configuredLayer([]);
159
+ const functionInvocationService = MockedFunctionInvocationService;
160
+ const operationServiceLayer = MockedOperationServiceLayer;
161
+
162
+ const aiLayer = this.context.services.functionsAiService
163
+ ? AiModelResolver.AiModelResolver.buildAiService.pipe(
164
+ Layer.provide(
165
+ AnthropicResolver.make().pipe(
166
+ Layer.provide(
167
+ AnthropicClient.layer({
168
+ // Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
169
+ apiUrl: 'http://internal/provider/anthropic',
170
+ }).pipe(Layer.provide(FunctionsAiHttpClient.layer(this.context.services.functionsAiService))),
171
+ ),
172
+ ),
173
+ ),
174
+ )
175
+ : AiService.notAvailable;
176
+
177
+ return Layer.mergeAll(
178
+ dbLayer,
179
+ queuesLayer,
180
+ feedLayer,
181
+ credentials,
182
+ functionInvocationService,
183
+ operationServiceLayer,
184
+ aiLayer,
185
+ // TODO(dmaretskyi): Forward trace events.
186
+ Trace.writerLayerNoop,
187
+ );
188
+ }
189
+ }
190
+
191
+ const MockedFunctionInvocationService = Layer.succeed(FunctionInvocationService, {
192
+ invokeFunction: () => Effect.die('Calling functions from functions is not implemented yet.'),
193
+ resolveFunction: () => Effect.die('Not implemented.'),
194
+ });
195
+
196
+ const MockedOperationServiceLayer = Layer.succeed(Operation.Service, {
197
+ invoke: () => Effect.die('Calling operations from functions is not implemented yet.'),
198
+ schedule: () => Effect.die('Not implemented.'),
199
+ invokePromise: async () => ({ error: new Error('Not implemented') }),
200
+ } as any);
201
+
202
+ const decodeRefsFromSchema = (ast: SchemaAST.AST, value: unknown, db: EchoDatabaseImpl): unknown => {
203
+ if (value == null) {
204
+ return value;
205
+ }
206
+
207
+ const encoded = SchemaAST.encodedBoundAST(ast);
208
+ if (Ref.isRefType(encoded)) {
209
+ if (Ref.isRef(value)) {
210
+ return value;
211
+ }
212
+
213
+ if (typeof value === 'object' && value !== null && typeof (value as any)['/'] === 'string') {
214
+ const resolver = db.graph.createRefResolver({ context: { space: db.spaceId } });
215
+ return refFromEncodedReference(value as any, resolver);
216
+ }
217
+
218
+ return value;
219
+ }
220
+
221
+ switch (encoded._tag) {
222
+ case 'TypeLiteral': {
223
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
224
+ return value;
225
+ }
226
+ const result: Record<string, unknown> = { ...(value as any) };
227
+ for (const prop of SchemaAST.getPropertySignatures(encoded)) {
228
+ const key = prop.name.toString();
229
+ if (key in result) {
230
+ result[key] = decodeRefsFromSchema(prop.type, (result as any)[key], db);
231
+ }
232
+ }
233
+ return result;
234
+ }
235
+
236
+ case 'TupleType': {
237
+ if (!Array.isArray(value)) {
238
+ return value;
239
+ }
240
+
241
+ // For arrays, effect uses TupleType with empty elements and a single rest element.
242
+ if (encoded.elements.length === 0 && encoded.rest.length === 1) {
243
+ const elementType = encoded.rest[0].type;
244
+ return (value as unknown[]).map((item) => decodeRefsFromSchema(elementType, item, db));
245
+ }
246
+
247
+ return value;
248
+ }
249
+
250
+ case 'Union': {
251
+ // Optional values are represented as union with undefined.
252
+ const nonUndefined = encoded.types.filter((t) => !SchemaAST.isUndefinedKeyword(t));
253
+ if (nonUndefined.length === 1) {
254
+ return decodeRefsFromSchema(nonUndefined[0], value, db);
255
+ }
256
+
257
+ // For other unions we can't safely pick a branch without validating.
258
+ return value;
259
+ }
260
+
261
+ case 'Suspend': {
262
+ return decodeRefsFromSchema(encoded.f(), value, db);
263
+ }
264
+
265
+ case 'Refinement': {
266
+ return decodeRefsFromSchema(encoded.from, value, db);
267
+ }
268
+
269
+ default: {
270
+ return value;
271
+ }
272
+ }
273
+ };
package/src/sdk.ts ADDED
@@ -0,0 +1,30 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { type AiService } from '@dxos/ai';
6
+ import { type Database, type Feed } from '@dxos/echo';
7
+ import { type Trace } from '@dxos/functions';
8
+ import type { Operation } from '@dxos/operation';
9
+
10
+ import { type CredentialsService, type FunctionInvocationService, type QueueService } from './services';
11
+
12
+ // TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
13
+ // https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
14
+ // https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
15
+ // https://www.npmjs.com/package/aws-lambda
16
+
17
+ /**
18
+ * Services that are available to invoked functions.
19
+ * @deprecated
20
+ */
21
+ export type FunctionServices =
22
+ | AiService.AiService
23
+ | CredentialsService
24
+ | Database.Service
25
+ // TODO(wittjosiah): Remove QueueService — use Feed.FeedService instead.
26
+ | QueueService
27
+ | Feed.FeedService
28
+ | Trace.TraceService
29
+ | FunctionInvocationService
30
+ | Operation.Service;
@@ -11,9 +11,8 @@ import * as Layer from 'effect/Layer';
11
11
  import * as Redacted from 'effect/Redacted';
12
12
 
13
13
  import { Query } from '@dxos/echo';
14
- import { DataType } from '@dxos/schema';
15
-
16
- import { DatabaseService } from './database';
14
+ import { Database } from '@dxos/echo';
15
+ import { AccessToken } from '@dxos/types';
17
16
 
18
17
  export type CredentialQuery = {
19
18
  service?: string;
@@ -61,7 +60,12 @@ export class CredentialsService extends Context.Tag('@dxos/functions/Credentials
61
60
  static configuredLayer = (credentials: ServiceCredential[]) =>
62
61
  Layer.succeed(CredentialsService, new ConfiguredCredentialsService(credentials));
63
62
 
64
- static layerConfig = (credentials: { service: string; apiKey: Config.Config<Redacted.Redacted<string>> }[]) =>
63
+ static layerConfig = (
64
+ credentials: {
65
+ service: string;
66
+ apiKey: Config.Config<Redacted.Redacted<string>>;
67
+ }[],
68
+ ) =>
65
69
  Layer.effect(
66
70
  CredentialsService,
67
71
  Effect.gen(function* () {
@@ -78,20 +82,34 @@ export class CredentialsService extends Context.Tag('@dxos/functions/Credentials
78
82
  }),
79
83
  );
80
84
 
81
- static layerFromDatabase = () =>
85
+ static layerFromDatabase = ({ caching = false }: { caching?: boolean } = {}) =>
82
86
  Layer.effect(
83
87
  CredentialsService,
84
88
  Effect.gen(function* () {
85
- const dbService = yield* DatabaseService;
89
+ const dbService = yield* Database.Service;
90
+ const cache = new Map<string, ServiceCredential[]>();
91
+
86
92
  const queryCredentials = async (query: CredentialQuery): Promise<ServiceCredential[]> => {
87
- const { objects: accessTokens } = await dbService.db.query(Query.type(DataType.AccessToken)).run();
88
- return accessTokens
93
+ const cacheKey = JSON.stringify(query);
94
+ if (caching && cache.has(cacheKey)) {
95
+ return cache.get(cacheKey)!;
96
+ }
97
+
98
+ const accessTokens = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
99
+ const credentials = accessTokens
89
100
  .filter((accessToken) => accessToken.source === query.service)
90
101
  .map((accessToken) => ({
91
102
  service: accessToken.source,
92
103
  apiKey: accessToken.token,
93
104
  }));
105
+
106
+ if (caching) {
107
+ cache.set(cacheKey, credentials);
108
+ }
109
+
110
+ return credentials;
94
111
  };
112
+
95
113
  return {
96
114
  getCredential: async (query) => {
97
115
  const credentials = await queryCredentials(query);
@@ -132,13 +150,10 @@ export class ConfiguredCredentialsService implements Context.Tag.Service<Credent
132
150
  }
133
151
 
134
152
  /**
135
- * Maps the request to include the API key from the credential.
153
+ * Maps the request to include the given token in the Authorization header.
136
154
  */
137
- export const withAuthorization = (query: CredentialQuery, kind?: 'Bearer' | 'Basic') =>
138
- HttpClient.mapRequestEffect(
139
- Effect.fnUntraced(function* (request) {
140
- const key = yield* CredentialsService.getApiKey(query).pipe(Effect.map(Redacted.value));
141
- const authorization = kind ? `${kind} ${key}` : key;
142
- return HttpClientRequest.setHeader(request, 'Authorization', authorization);
143
- }),
144
- );
155
+ export const withAuthorization = (token: string, kind?: 'Bearer' | 'Basic') =>
156
+ HttpClient.mapRequest((request) => {
157
+ const authorization = kind ? `${kind} ${token}` : token;
158
+ return HttpClientRequest.setHeader(request, 'Authorization', authorization);
159
+ });
@@ -7,22 +7,26 @@ import * as Effect from 'effect/Effect';
7
7
  import * as Layer from 'effect/Layer';
8
8
  import * as Schema from 'effect/Schema';
9
9
 
10
- import { Obj, Type } from '@dxos/echo';
10
+ import { Type } from '@dxos/echo';
11
11
  import { invariant } from '@dxos/invariant';
12
12
  import { LogLevel, log } from '@dxos/log';
13
13
 
14
- import { TracingService } from './tracing';
15
-
16
14
  export const ComputeEventPayload = Schema.Union(
17
15
  Schema.Struct({
18
16
  type: Schema.Literal('begin-compute'),
19
17
  nodeId: Schema.String,
20
- inputs: Schema.Record({ key: Schema.String, value: Schema.Any }),
18
+ /**
19
+ * Names of the inputs begin computed.
20
+ */
21
+ inputs: Schema.Array(Schema.String),
21
22
  }),
22
23
  Schema.Struct({
23
24
  type: Schema.Literal('end-compute'),
24
25
  nodeId: Schema.String,
25
- outputs: Schema.Record({ key: Schema.String, value: Schema.Any }),
26
+ /**
27
+ * Names of the outputs computed.
28
+ */
29
+ outputs: Schema.Array(Schema.String),
26
30
  }),
27
31
  Schema.Struct({
28
32
  type: Schema.Literal('compute-input'),
@@ -46,7 +50,7 @@ export type ComputeEventPayload = Schema.Schema.Type<typeof ComputeEventPayload>
46
50
 
47
51
  export const ComputeEvent = Schema.Struct({
48
52
  payload: ComputeEventPayload,
49
- }).pipe(Type.Obj({ typename: 'dxos.org/type/ComputeEvent', version: '0.1.0' }));
53
+ }).pipe(Type.object({ typename: 'org.dxos.type.computeEvent', version: '0.1.0' }));
50
54
 
51
55
  /**
52
56
  * Logs event for the compute workflows.
@@ -60,21 +64,7 @@ export class ComputeEventLogger extends Context.Tag('@dxos/functions/ComputeEven
60
64
  nodeId: undefined,
61
65
  };
62
66
 
63
- /**
64
- * Implements ComputeEventLogger using TracingService.
65
- */
66
- static layerFromTracing = Layer.effect(
67
- ComputeEventLogger,
68
- Effect.gen(function* () {
69
- const tracing = yield* TracingService;
70
- return {
71
- log: (event: ComputeEventPayload) => {
72
- tracing.write(Obj.make(ComputeEvent, { payload: event }));
73
- },
74
- nodeId: undefined,
75
- };
76
- }),
77
- );
67
+ static layerNoop: Layer.Layer<ComputeEventLogger> = Layer.succeed(ComputeEventLogger, ComputeEventLogger.noop);
78
68
  }
79
69
 
80
70
  export const logCustomEvent = (data: any) =>