@dxos/functions 0.8.4-main.c4373fc → 0.8.4-main.cb12b3f963

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 (181) hide show
  1. package/README.md +4 -6
  2. package/dist/lib/neutral/index.mjs +475 -0
  3. package/dist/lib/neutral/index.mjs.map +7 -0
  4. package/dist/lib/neutral/meta.json +1 -0
  5. package/dist/types/src/index.d.ts +3 -9
  6. package/dist/types/src/index.d.ts.map +1 -1
  7. package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
  8. package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
  9. package/dist/types/src/protocol/index.d.ts +2 -0
  10. package/dist/types/src/protocol/index.d.ts.map +1 -0
  11. package/dist/types/src/protocol/protocol.d.ts +7 -0
  12. package/dist/types/src/protocol/protocol.d.ts.map +1 -0
  13. package/dist/types/src/protocol/protocol.test.d.ts +2 -0
  14. package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
  15. package/dist/types/src/sdk.d.ts +10 -0
  16. package/dist/types/src/sdk.d.ts.map +1 -0
  17. package/dist/types/src/services/credentials.d.ts +17 -38
  18. package/dist/types/src/services/credentials.d.ts.map +1 -1
  19. package/dist/types/src/services/function-invocation-service.d.ts +6 -19
  20. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
  21. package/dist/types/src/services/index.d.ts +1 -6
  22. package/dist/types/src/services/index.d.ts.map +1 -1
  23. package/dist/types/src/services/queues.d.ts +1 -46
  24. package/dist/types/src/services/queues.d.ts.map +1 -1
  25. package/dist/types/src/services/tracing.d.ts +2 -56
  26. package/dist/types/src/services/tracing.d.ts.map +1 -1
  27. package/dist/types/src/types/index.d.ts +2 -0
  28. package/dist/types/src/types/index.d.ts.map +1 -0
  29. package/dist/types/src/types/url.d.ts +13 -0
  30. package/dist/types/src/types/url.d.ts.map +1 -0
  31. package/dist/types/tsconfig.tsbuildinfo +1 -1
  32. package/package.json +25 -63
  33. package/src/index.ts +3 -9
  34. package/src/protocol/functions-ai-http-client.ts +67 -0
  35. package/src/{executor → protocol}/index.ts +1 -1
  36. package/src/protocol/protocol.test.ts +58 -0
  37. package/src/protocol/protocol.ts +292 -0
  38. package/src/sdk.ts +29 -0
  39. package/src/services/credentials.ts +81 -109
  40. package/src/services/function-invocation-service.ts +19 -70
  41. package/src/services/index.ts +1 -6
  42. package/src/services/queues.ts +1 -80
  43. package/src/services/tracing.ts +1 -136
  44. package/src/types/index.ts +5 -0
  45. package/src/types/url.ts +32 -0
  46. package/dist/lib/browser/bundler/index.mjs +0 -269
  47. package/dist/lib/browser/bundler/index.mjs.map +0 -7
  48. package/dist/lib/browser/chunk-J5LGTIGS.mjs +0 -10
  49. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +0 -7
  50. package/dist/lib/browser/chunk-LKYT2SAL.mjs +0 -665
  51. package/dist/lib/browser/chunk-LKYT2SAL.mjs.map +0 -7
  52. package/dist/lib/browser/edge/index.mjs +0 -83
  53. package/dist/lib/browser/edge/index.mjs.map +0 -7
  54. package/dist/lib/browser/index.mjs +0 -1395
  55. package/dist/lib/browser/index.mjs.map +0 -7
  56. package/dist/lib/browser/meta.json +0 -1
  57. package/dist/lib/browser/testing/index.mjs +0 -131
  58. package/dist/lib/browser/testing/index.mjs.map +0 -7
  59. package/dist/lib/node-esm/bundler/index.mjs +0 -270
  60. package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
  61. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
  62. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +0 -7
  63. package/dist/lib/node-esm/chunk-NAQIKLZB.mjs +0 -667
  64. package/dist/lib/node-esm/chunk-NAQIKLZB.mjs.map +0 -7
  65. package/dist/lib/node-esm/edge/index.mjs +0 -84
  66. package/dist/lib/node-esm/edge/index.mjs.map +0 -7
  67. package/dist/lib/node-esm/index.mjs +0 -1396
  68. package/dist/lib/node-esm/index.mjs.map +0 -7
  69. package/dist/lib/node-esm/meta.json +0 -1
  70. package/dist/lib/node-esm/testing/index.mjs +0 -132
  71. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  72. package/dist/types/src/bundler/bundler.d.ts +0 -49
  73. package/dist/types/src/bundler/bundler.d.ts.map +0 -1
  74. package/dist/types/src/bundler/bundler.test.d.ts +0 -2
  75. package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
  76. package/dist/types/src/bundler/index.d.ts +0 -2
  77. package/dist/types/src/bundler/index.d.ts.map +0 -1
  78. package/dist/types/src/edge/functions.d.ts +0 -17
  79. package/dist/types/src/edge/functions.d.ts.map +0 -1
  80. package/dist/types/src/edge/index.d.ts +0 -2
  81. package/dist/types/src/edge/index.d.ts.map +0 -1
  82. package/dist/types/src/errors.d.ts +0 -137
  83. package/dist/types/src/errors.d.ts.map +0 -1
  84. package/dist/types/src/examples/fib.d.ts +0 -7
  85. package/dist/types/src/examples/fib.d.ts.map +0 -1
  86. package/dist/types/src/examples/index.d.ts +0 -4
  87. package/dist/types/src/examples/index.d.ts.map +0 -1
  88. package/dist/types/src/examples/reply.d.ts +0 -3
  89. package/dist/types/src/examples/reply.d.ts.map +0 -1
  90. package/dist/types/src/examples/sleep.d.ts +0 -5
  91. package/dist/types/src/examples/sleep.d.ts.map +0 -1
  92. package/dist/types/src/executor/executor.d.ts +0 -14
  93. package/dist/types/src/executor/executor.d.ts.map +0 -1
  94. package/dist/types/src/executor/index.d.ts +0 -2
  95. package/dist/types/src/executor/index.d.ts.map +0 -1
  96. package/dist/types/src/handler.d.ts +0 -109
  97. package/dist/types/src/handler.d.ts.map +0 -1
  98. package/dist/types/src/schema.d.ts +0 -43
  99. package/dist/types/src/schema.d.ts.map +0 -1
  100. package/dist/types/src/services/database.d.ts +0 -67
  101. package/dist/types/src/services/database.d.ts.map +0 -1
  102. package/dist/types/src/services/event-logger.d.ts +0 -75
  103. package/dist/types/src/services/event-logger.d.ts.map +0 -1
  104. package/dist/types/src/services/function-invocation-service.test.d.ts +0 -2
  105. package/dist/types/src/services/function-invocation-service.test.d.ts.map +0 -1
  106. package/dist/types/src/services/local-function-execution.d.ts +0 -34
  107. package/dist/types/src/services/local-function-execution.d.ts.map +0 -1
  108. package/dist/types/src/services/remote-function-execution-service.d.ts +0 -22
  109. package/dist/types/src/services/remote-function-execution-service.d.ts.map +0 -1
  110. package/dist/types/src/services/service-container.d.ts +0 -57
  111. package/dist/types/src/services/service-container.d.ts.map +0 -1
  112. package/dist/types/src/services/service-registry.d.ts +0 -31
  113. package/dist/types/src/services/service-registry.d.ts.map +0 -1
  114. package/dist/types/src/services/service-registry.test.d.ts +0 -2
  115. package/dist/types/src/services/service-registry.test.d.ts.map +0 -1
  116. package/dist/types/src/testing/index.d.ts +0 -3
  117. package/dist/types/src/testing/index.d.ts.map +0 -1
  118. package/dist/types/src/testing/layer.d.ts +0 -18
  119. package/dist/types/src/testing/layer.d.ts.map +0 -1
  120. package/dist/types/src/testing/logger.d.ts +0 -5
  121. package/dist/types/src/testing/logger.d.ts.map +0 -1
  122. package/dist/types/src/testing/persist-database.test.d.ts +0 -2
  123. package/dist/types/src/testing/persist-database.test.d.ts.map +0 -1
  124. package/dist/types/src/testing/services.d.ts +0 -59
  125. package/dist/types/src/testing/services.d.ts.map +0 -1
  126. package/dist/types/src/trace.d.ts +0 -122
  127. package/dist/types/src/trace.d.ts.map +0 -1
  128. package/dist/types/src/translations.d.ts +0 -12
  129. package/dist/types/src/translations.d.ts.map +0 -1
  130. package/dist/types/src/triggers/index.d.ts +0 -4
  131. package/dist/types/src/triggers/index.d.ts.map +0 -1
  132. package/dist/types/src/triggers/input-builder.d.ts +0 -3
  133. package/dist/types/src/triggers/input-builder.d.ts.map +0 -1
  134. package/dist/types/src/triggers/invocation-tracer.d.ts +0 -37
  135. package/dist/types/src/triggers/invocation-tracer.d.ts.map +0 -1
  136. package/dist/types/src/triggers/trigger-dispatcher.d.ts +0 -78
  137. package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +0 -1
  138. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +0 -2
  139. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +0 -1
  140. package/dist/types/src/triggers/trigger-state-store.d.ts +0 -28
  141. package/dist/types/src/triggers/trigger-state-store.d.ts.map +0 -1
  142. package/dist/types/src/types.d.ts +0 -230
  143. package/dist/types/src/types.d.ts.map +0 -1
  144. package/dist/types/src/url.d.ts +0 -21
  145. package/dist/types/src/url.d.ts.map +0 -1
  146. package/src/bundler/bundler.test.ts +0 -58
  147. package/src/bundler/bundler.ts +0 -295
  148. package/src/bundler/index.ts +0 -5
  149. package/src/edge/functions.ts +0 -67
  150. package/src/edge/index.ts +0 -9
  151. package/src/errors.ts +0 -21
  152. package/src/examples/fib.ts +0 -32
  153. package/src/examples/index.ts +0 -7
  154. package/src/examples/reply.ts +0 -21
  155. package/src/examples/sleep.ts +0 -24
  156. package/src/executor/executor.ts +0 -58
  157. package/src/handler.ts +0 -225
  158. package/src/schema.ts +0 -71
  159. package/src/services/database.ts +0 -175
  160. package/src/services/event-logger.ts +0 -121
  161. package/src/services/function-invocation-service.test.ts +0 -81
  162. package/src/services/local-function-execution.ts +0 -153
  163. package/src/services/remote-function-execution-service.ts +0 -63
  164. package/src/services/service-container.ts +0 -115
  165. package/src/services/service-registry.test.ts +0 -45
  166. package/src/services/service-registry.ts +0 -63
  167. package/src/testing/index.ts +0 -6
  168. package/src/testing/layer.ts +0 -114
  169. package/src/testing/logger.ts +0 -17
  170. package/src/testing/persist-database.test.ts +0 -87
  171. package/src/testing/services.ts +0 -115
  172. package/src/trace.ts +0 -178
  173. package/src/translations.ts +0 -20
  174. package/src/triggers/index.ts +0 -7
  175. package/src/triggers/input-builder.ts +0 -35
  176. package/src/triggers/invocation-tracer.ts +0 -101
  177. package/src/triggers/trigger-dispatcher.test.ts +0 -665
  178. package/src/triggers/trigger-dispatcher.ts +0 -533
  179. package/src/triggers/trigger-state-store.ts +0 -61
  180. package/src/types.ts +0 -218
  181. package/src/url.ts +0 -55
package/package.json CHANGED
@@ -1,9 +1,13 @@
1
1
  {
2
2
  "name": "@dxos/functions",
3
- "version": "0.8.4-main.c4373fc",
4
- "description": "Functions API and runtime.",
3
+ "version": "0.8.4-main.cb12b3f963",
4
+ "description": "Functions API.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
7
11
  "license": "MIT",
8
12
  "author": "info@dxos.org",
9
13
  "sideEffects": false,
@@ -12,76 +16,34 @@
12
16
  ".": {
13
17
  "source": "./src/index.ts",
14
18
  "types": "./dist/types/src/index.d.ts",
15
- "browser": "./dist/lib/browser/index.mjs",
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"
19
+ "default": "./dist/lib/neutral/index.mjs"
35
20
  }
36
21
  },
37
22
  "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
23
  "files": [
49
24
  "dist",
50
25
  "schema",
51
26
  "src"
52
27
  ],
53
28
  "dependencies": {
54
- "@effect/platform": "0.92.1",
55
- "effect": "3.18.3",
56
- "esbuild-wasm": "^0.16.14",
57
- "i18next": "^24.2.1",
58
- "iso-did": "^1.6.0",
59
- "@dxos/ai": "0.8.4-main.c4373fc",
60
- "@dxos/async": "0.8.4-main.c4373fc",
61
- "@dxos/client": "0.8.4-main.c4373fc",
62
- "@dxos/client-protocol": "0.8.4-main.c4373fc",
63
- "@dxos/context": "0.8.4-main.c4373fc",
64
- "@dxos/crypto": "0.8.4-main.c4373fc",
65
- "@dxos/debug": "0.8.4-main.c4373fc",
66
- "@dxos/echo": "0.8.4-main.c4373fc",
67
- "@dxos/echo-db": "0.8.4-main.c4373fc",
68
- "@dxos/echo-pipeline": "0.8.4-main.c4373fc",
69
- "@dxos/echo-protocol": "0.8.4-main.c4373fc",
70
- "@dxos/edge-client": "0.8.4-main.c4373fc",
71
- "@dxos/effect": "0.8.4-main.c4373fc",
72
- "@dxos/errors": "0.8.4-main.c4373fc",
73
- "@dxos/invariant": "0.8.4-main.c4373fc",
74
- "@dxos/kv-store": "0.8.4-main.c4373fc",
75
- "@dxos/live-object": "0.8.4-main.c4373fc",
76
- "@dxos/keys": "0.8.4-main.c4373fc",
77
- "@dxos/log": "0.8.4-main.c4373fc",
78
- "@dxos/node-std": "0.8.4-main.c4373fc",
79
- "@dxos/protocols": "0.8.4-main.c4373fc",
80
- "@dxos/schema": "0.8.4-main.c4373fc",
81
- "@dxos/util": "0.8.4-main.c4373fc"
82
- },
83
- "devDependencies": {
84
- "@dxos/agent": "0.8.4-main.c4373fc"
29
+ "@effect-atom/atom": "^0.5.1",
30
+ "@effect/ai-anthropic": "0.23.0",
31
+ "@effect/platform": "0.94.4",
32
+ "effect": "3.20.0",
33
+ "@dxos/ai": "0.8.4-main.cb12b3f963",
34
+ "@dxos/echo": "0.8.4-main.cb12b3f963",
35
+ "@dxos/context": "0.8.4-main.cb12b3f963",
36
+ "@dxos/effect": "0.8.4-main.cb12b3f963",
37
+ "@dxos/compute": "0.8.4-main.cb12b3f963",
38
+ "@dxos/echo-db": "0.8.4-main.cb12b3f963",
39
+ "@dxos/errors": "0.8.4-main.cb12b3f963",
40
+ "@dxos/invariant": "0.8.4-main.cb12b3f963",
41
+ "@dxos/log": "0.8.4-main.cb12b3f963",
42
+ "@dxos/keys": "0.8.4-main.cb12b3f963",
43
+ "@dxos/types": "0.8.4-main.cb12b3f963",
44
+ "@dxos/protocols": "0.8.4-main.cb12b3f963",
45
+ "@dxos/schema": "0.8.4-main.cb12b3f963",
46
+ "@dxos/node-std": "0.8.4-main.cb12b3f963"
85
47
  },
86
48
  "publishConfig": {
87
49
  "access": "public"
package/src/index.ts CHANGED
@@ -2,13 +2,7 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
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';
5
+ export * from './sdk';
12
6
  export * from './services';
13
- export * from './executor';
14
- export * as exampleFunctions from './examples';
7
+ export * from './types';
8
+ export * from './protocol';
@@ -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 { InvalidOperationInputError } from '@dxos/compute';
8
+ import { FibonacciHandler, ReplyHandler } from '@dxos/compute/testing';
9
+
10
+ import { wrapFunctionHandler } from './protocol';
11
+
12
+ describe('wrapFunctionHandler', () => {
13
+ test('wraps reply function and executes handler', async ({ expect }) => {
14
+ const wrapped = wrapFunctionHandler(ReplyHandler);
15
+
16
+ expect(wrapped.meta.key).toBe('org.example.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(FibonacciHandler);
32
+
33
+ expect(wrapped.meta.key).toBe('org.example.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 InvalidOperationInputError on invalid input schema for fibonacci', async ({ expect }) => {
47
+ const wrapped = wrapFunctionHandler(FibonacciHandler);
48
+
49
+ await expect(
50
+ wrapped.handler({
51
+ data: { iterations: 'invalid' },
52
+ context: {
53
+ services: {},
54
+ },
55
+ }),
56
+ ).rejects.toThrow(InvalidOperationInputError);
57
+ });
58
+ });
@@ -0,0 +1,292 @@
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 {
14
+ FunctionError,
15
+ InvalidOperationInputError,
16
+ InvalidOperationOutputError,
17
+ Operation,
18
+ Trace,
19
+ } from '@dxos/compute';
20
+ import { LifecycleState, Resource } from '@dxos/context';
21
+ import { Database, Feed, JsonSchema, Ref, type Type } from '@dxos/echo';
22
+ import { EchoClient, type EchoDatabaseImpl, type QueueFactory, createFeedServiceLayer } from '@dxos/echo-db';
23
+ import { refFromEncodedReference } from '@dxos/echo/internal';
24
+ import { runAndForwardErrors } from '@dxos/effect';
25
+ import { assertState, failedInvariant, invariant } from '@dxos/invariant';
26
+ import { PublicKey } from '@dxos/keys';
27
+ import { type FunctionProtocol } from '@dxos/protocols';
28
+
29
+ import { type FunctionServices } from '../sdk';
30
+ import {
31
+ configuredCredentialsLayer,
32
+ credentialsLayerFromDatabase,
33
+ FunctionInvocationService,
34
+ QueueService,
35
+ } from '../services';
36
+ import { FunctionsAiHttpClient } from './functions-ai-http-client';
37
+
38
+ /**
39
+ * Wraps a function handler made with `defineFunction` to a protocol that the functions-runtime expects.
40
+ */
41
+ export const wrapFunctionHandler = (func: Operation.WithHandler<Operation.Definition.Any>): FunctionProtocol.Func => {
42
+ if (!Operation.isOperationWithHandler(func)) {
43
+ throw new TypeError('Expected operation with handler');
44
+ }
45
+
46
+ const serviceTags = func.services.map((service) => service.key);
47
+
48
+ return {
49
+ meta: {
50
+ key: func.meta.key,
51
+ name: func.meta.name,
52
+ description: func.meta.description,
53
+ inputSchema: JsonSchema.toJsonSchema(func.input),
54
+ outputSchema: func.output === undefined ? undefined : JsonSchema.toJsonSchema(func.output),
55
+ services: func.services.map((service) => service.key),
56
+ },
57
+ handler: async ({ data, context }) => {
58
+ if (
59
+ (serviceTags.includes(Database.Service.key) ||
60
+ serviceTags.includes(QueueService.key) ||
61
+ serviceTags.includes(Feed.FeedService.key)) &&
62
+ (!context.services.dataService || !context.services.queryService)
63
+ ) {
64
+ throw new FunctionError({
65
+ message: 'Services not provided: dataService, queryService',
66
+ });
67
+ }
68
+
69
+ // eslint-disable-next-line no-useless-catch
70
+ try {
71
+ if (!SchemaAST.isAnyKeyword(func.input.ast)) {
72
+ try {
73
+ Schema.validateSync(func.input, { onExcessProperty: 'error' })(data);
74
+ } catch (error: any) {
75
+ throw new InvalidOperationInputError({
76
+ message: `Operation input did not match schema (${func.meta.key}): ${error.message}`,
77
+ cause: error,
78
+ });
79
+ }
80
+ }
81
+
82
+ await using funcContext = await new FunctionContext(context).open();
83
+
84
+ if (func.types.length > 0) {
85
+ invariant(funcContext.db, 'Database is required for functions with types');
86
+ await funcContext.db.graph.schemaRegistry.register(func.types as Type.AnyEntity[]);
87
+ }
88
+
89
+ const dataWithDecodedRefs =
90
+ funcContext.db && !SchemaAST.isAnyKeyword(func.input.ast)
91
+ ? decodeRefsFromSchema(func.input.ast, data, funcContext.db)
92
+ : data;
93
+
94
+ let result: any = await func.handler(dataWithDecodedRefs);
95
+
96
+ if (Effect.isEffect(result)) {
97
+ result = await runAndForwardErrors(
98
+ (result as Effect.Effect<unknown, unknown, FunctionServices>).pipe(
99
+ Effect.orDie,
100
+ Effect.provide(funcContext.createLayer()),
101
+ ),
102
+ );
103
+ }
104
+
105
+ if (func.output && !SchemaAST.isAnyKeyword(func.output.ast)) {
106
+ try {
107
+ Schema.validateSync(func.output, { onExcessProperty: 'error' })(result);
108
+ } catch (error: any) {
109
+ throw new InvalidOperationOutputError({
110
+ message: `Operation output did not match schema (${func.meta.key}): ${error.message}`,
111
+ cause: error,
112
+ });
113
+ }
114
+ }
115
+
116
+ return result;
117
+ } catch (error) {
118
+ // TODO(dmaretskyi): We might do error wrapping here and add extra context.
119
+ throw error;
120
+ }
121
+ },
122
+ };
123
+ };
124
+
125
+ /**
126
+ * Container for services and context for a function.
127
+ */
128
+ class FunctionContext extends Resource {
129
+ readonly context: FunctionProtocol.Context;
130
+ readonly client: EchoClient | undefined;
131
+ db: EchoDatabaseImpl | undefined;
132
+ queues: QueueFactory | undefined;
133
+
134
+ constructor(context: FunctionProtocol.Context) {
135
+ super();
136
+ this.context = context;
137
+ if (context.services.dataService && context.services.queryService) {
138
+ this.client = new EchoClient().connectToService({
139
+ dataService: context.services.dataService,
140
+ queryService: context.services.queryService,
141
+ queueService: context.services.queueService,
142
+ });
143
+ }
144
+ }
145
+
146
+ override async _open() {
147
+ await this.client?.open();
148
+ this.db =
149
+ this.client && this.context.spaceId
150
+ ? this.client.constructDatabase({
151
+ spaceId: this.context.spaceId ?? failedInvariant(),
152
+ spaceKey: PublicKey.fromHex(this.context.spaceKey ?? failedInvariant('spaceKey missing in context')),
153
+ reactiveSchemaQuery: false,
154
+ preloadSchemaOnOpen: false,
155
+ })
156
+ : undefined;
157
+
158
+ await this.db?.setSpaceRoot(this.context.spaceRootUrl ?? failedInvariant('spaceRootUrl missing in context'));
159
+ await this.db?.open();
160
+ this.queues =
161
+ this.client && this.context.spaceId ? this.client.constructQueueFactory(this.context.spaceId) : undefined;
162
+ }
163
+
164
+ override async _close() {
165
+ await this.db?.close();
166
+ await this.client?.close();
167
+ }
168
+
169
+ createLayer(): Layer.Layer<FunctionServices> {
170
+ assertState(this._lifecycleState === LifecycleState.OPEN, 'FunctionContext is not open');
171
+
172
+ const dbLayer = this.db ? Database.layer(this.db) : Database.notAvailable;
173
+ const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
174
+ const feedLayer = this.queues ? createFeedServiceLayer(this.queues) : Feed.notAvailable;
175
+ const credentials = dbLayer
176
+ ? credentialsLayerFromDatabase({ caching: true }).pipe(Layer.provide(dbLayer))
177
+ : configuredCredentialsLayer([]);
178
+ const functionInvocationService = MockedFunctionInvocationService;
179
+ const operationServiceLayer = MockedOperationServiceLayer;
180
+
181
+ const aiLayer = this.context.services.functionsAiService
182
+ ? AiModelResolver.AiModelResolver.buildAiService.pipe(
183
+ Layer.provide(
184
+ AnthropicResolver.make().pipe(
185
+ Layer.provide(
186
+ AnthropicClient.layer({
187
+ // Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
188
+ apiUrl: 'http://internal/provider/anthropic',
189
+ }).pipe(Layer.provide(FunctionsAiHttpClient.layer(this.context.services.functionsAiService))),
190
+ ),
191
+ ),
192
+ ),
193
+ )
194
+ : AiService.notAvailable;
195
+
196
+ return Layer.mergeAll(
197
+ dbLayer,
198
+ queuesLayer,
199
+ feedLayer,
200
+ credentials,
201
+ functionInvocationService,
202
+ operationServiceLayer,
203
+ aiLayer,
204
+ // TODO(dmaretskyi): Forward trace events.
205
+ Trace.writerLayerNoop,
206
+ );
207
+ }
208
+ }
209
+
210
+ const MockedFunctionInvocationService = Layer.succeed(FunctionInvocationService, {
211
+ invokeFunction: () => Effect.die('Calling functions from functions is not implemented yet.'),
212
+ resolveFunction: () => Effect.die('Not implemented.'),
213
+ });
214
+
215
+ const MockedOperationServiceLayer = Layer.succeed(Operation.Service, {
216
+ invoke: () => Effect.die('Calling operations from functions is not implemented yet.'),
217
+ schedule: () => Effect.die('Not implemented.'),
218
+ invokePromise: async () => ({ error: new Error('Not implemented') }),
219
+ } as any);
220
+
221
+ const decodeRefsFromSchema = (ast: SchemaAST.AST, value: unknown, db: EchoDatabaseImpl): unknown => {
222
+ if (value == null) {
223
+ return value;
224
+ }
225
+
226
+ const encoded = SchemaAST.encodedBoundAST(ast);
227
+ if (Ref.isRefType(encoded)) {
228
+ if (Ref.isRef(value)) {
229
+ return value;
230
+ }
231
+
232
+ if (typeof value === 'object' && value !== null && typeof (value as any)['/'] === 'string') {
233
+ const resolver = db.graph.createRefResolver({ context: { space: db.spaceId } });
234
+ return refFromEncodedReference(value as any, resolver);
235
+ }
236
+
237
+ return value;
238
+ }
239
+
240
+ switch (encoded._tag) {
241
+ case 'TypeLiteral': {
242
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
243
+ return value;
244
+ }
245
+ const result: Record<string, unknown> = { ...(value as any) };
246
+ for (const prop of SchemaAST.getPropertySignatures(encoded)) {
247
+ const key = prop.name.toString();
248
+ if (key in result) {
249
+ result[key] = decodeRefsFromSchema(prop.type, (result as any)[key], db);
250
+ }
251
+ }
252
+ return result;
253
+ }
254
+
255
+ case 'TupleType': {
256
+ if (!Array.isArray(value)) {
257
+ return value;
258
+ }
259
+
260
+ // For arrays, effect uses TupleType with empty elements and a single rest element.
261
+ if (encoded.elements.length === 0 && encoded.rest.length === 1) {
262
+ const elementType = encoded.rest[0].type;
263
+ return (value as unknown[]).map((item) => decodeRefsFromSchema(elementType, item, db));
264
+ }
265
+
266
+ return value;
267
+ }
268
+
269
+ case 'Union': {
270
+ // Optional values are represented as union with undefined.
271
+ const nonUndefined = encoded.types.filter((t) => !SchemaAST.isUndefinedKeyword(t));
272
+ if (nonUndefined.length === 1) {
273
+ return decodeRefsFromSchema(nonUndefined[0], value, db);
274
+ }
275
+
276
+ // For other unions we can't safely pick a branch without validating.
277
+ return value;
278
+ }
279
+
280
+ case 'Suspend': {
281
+ return decodeRefsFromSchema(encoded.f(), value, db);
282
+ }
283
+
284
+ case 'Refinement': {
285
+ return decodeRefsFromSchema(encoded.from, value, db);
286
+ }
287
+
288
+ default: {
289
+ return value;
290
+ }
291
+ }
292
+ };
package/src/sdk.ts ADDED
@@ -0,0 +1,29 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { type AiService } from '@dxos/ai';
6
+ import { type Credential, type Operation, type Trace } from '@dxos/compute';
7
+ import { type Database, type Feed } from '@dxos/echo';
8
+
9
+ import { type FunctionInvocationService, type QueueService } from './services';
10
+
11
+ // TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
12
+ // https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
13
+ // https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
14
+ // https://www.npmjs.com/package/aws-lambda
15
+
16
+ /**
17
+ * Services that are available to invoked functions.
18
+ * @deprecated
19
+ */
20
+ export type FunctionServices =
21
+ | AiService.AiService
22
+ | Credential.CredentialsService
23
+ | Database.Service
24
+ // TODO(wittjosiah): Remove QueueService — use Feed.FeedService instead.
25
+ | QueueService
26
+ | Feed.FeedService
27
+ | Trace.TraceService
28
+ | FunctionInvocationService
29
+ | Operation.Service;