@dxos/functions 0.8.4-main.ead640a → 0.8.4-main.ef1bc66f44

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 (204) hide show
  1. package/dist/lib/neutral/index.mjs +1229 -0
  2. package/dist/lib/neutral/index.mjs.map +7 -0
  3. package/dist/lib/neutral/meta.json +1 -0
  4. package/dist/types/src/errors.d.ts +44 -60
  5. package/dist/types/src/errors.d.ts.map +1 -1
  6. package/dist/types/src/{examples → example}/fib.d.ts +1 -1
  7. package/dist/types/src/example/fib.d.ts.map +1 -0
  8. package/dist/types/src/example/forex-effect.d.ts +3 -0
  9. package/dist/types/src/example/forex-effect.d.ts.map +1 -0
  10. package/dist/types/src/example/index.d.ts +12 -0
  11. package/dist/types/src/example/index.d.ts.map +1 -0
  12. package/dist/types/src/{examples → example}/reply.d.ts +1 -1
  13. package/dist/types/src/example/reply.d.ts.map +1 -0
  14. package/dist/types/src/{examples → example}/sleep.d.ts +1 -1
  15. package/dist/types/src/example/sleep.d.ts.map +1 -0
  16. package/dist/types/src/index.d.ts +4 -8
  17. package/dist/types/src/index.d.ts.map +1 -1
  18. package/dist/types/src/operation-compatibility.test.d.ts +2 -0
  19. package/dist/types/src/operation-compatibility.test.d.ts.map +1 -0
  20. package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
  21. package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
  22. package/dist/types/src/protocol/index.d.ts +2 -0
  23. package/dist/types/src/protocol/index.d.ts.map +1 -0
  24. package/dist/types/src/protocol/protocol.d.ts +7 -0
  25. package/dist/types/src/protocol/protocol.d.ts.map +1 -0
  26. package/dist/types/src/protocol/protocol.test.d.ts +2 -0
  27. package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
  28. package/dist/types/src/sdk.d.ts +114 -0
  29. package/dist/types/src/sdk.d.ts.map +1 -0
  30. package/dist/types/src/services/credentials.d.ts +6 -4
  31. package/dist/types/src/services/credentials.d.ts.map +1 -1
  32. package/dist/types/src/services/event-logger.d.ts +33 -27
  33. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  34. package/dist/types/src/services/function-invocation-service.d.ts +7 -19
  35. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
  36. package/dist/types/src/services/index.d.ts +2 -4
  37. package/dist/types/src/services/index.d.ts.map +1 -1
  38. package/dist/types/src/services/queues.d.ts +4 -4
  39. package/dist/types/src/services/queues.d.ts.map +1 -1
  40. package/dist/types/src/services/tracing.d.ts +41 -12
  41. package/dist/types/src/services/tracing.d.ts.map +1 -1
  42. package/dist/types/src/types/Function.d.ts +52 -0
  43. package/dist/types/src/types/Function.d.ts.map +1 -0
  44. package/dist/types/src/types/Script.d.ts +21 -0
  45. package/dist/types/src/types/Script.d.ts.map +1 -0
  46. package/dist/types/src/types/Trigger.d.ts +121 -0
  47. package/dist/types/src/types/Trigger.d.ts.map +1 -0
  48. package/dist/types/src/types/TriggerEvent.d.ts +74 -0
  49. package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
  50. package/dist/types/src/types/index.d.ts +6 -0
  51. package/dist/types/src/types/index.d.ts.map +1 -0
  52. package/dist/types/src/types/url.d.ts +13 -0
  53. package/dist/types/src/types/url.d.ts.map +1 -0
  54. package/dist/types/tsconfig.tsbuildinfo +1 -1
  55. package/package.json +24 -63
  56. package/src/errors.ts +4 -4
  57. package/src/{examples → example}/fib.ts +1 -1
  58. package/src/example/forex-effect.ts +40 -0
  59. package/src/example/index.ts +13 -0
  60. package/src/{examples → example}/reply.ts +1 -1
  61. package/src/{examples → example}/sleep.ts +1 -1
  62. package/src/index.ts +4 -8
  63. package/src/operation-compatibility.test.ts +185 -0
  64. package/src/protocol/functions-ai-http-client.ts +67 -0
  65. package/src/{executor → protocol}/index.ts +1 -1
  66. package/src/protocol/protocol.test.ts +59 -0
  67. package/src/protocol/protocol.ts +262 -0
  68. package/src/sdk.ts +289 -0
  69. package/src/services/credentials.ts +32 -17
  70. package/src/services/event-logger.ts +10 -4
  71. package/src/services/function-invocation-service.ts +23 -70
  72. package/src/services/index.ts +2 -4
  73. package/src/services/queues.ts +5 -7
  74. package/src/services/tracing.ts +68 -44
  75. package/src/types/Function.ts +82 -0
  76. package/src/types/Script.ts +34 -0
  77. package/src/types/Trigger.ts +143 -0
  78. package/src/types/TriggerEvent.ts +62 -0
  79. package/src/types/index.ts +9 -0
  80. package/src/types/url.ts +32 -0
  81. package/dist/lib/browser/bundler/index.mjs +0 -269
  82. package/dist/lib/browser/bundler/index.mjs.map +0 -7
  83. package/dist/lib/browser/chunk-J5LGTIGS.mjs +0 -10
  84. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +0 -7
  85. package/dist/lib/browser/chunk-LKYT2SAL.mjs +0 -665
  86. package/dist/lib/browser/chunk-LKYT2SAL.mjs.map +0 -7
  87. package/dist/lib/browser/edge/index.mjs +0 -83
  88. package/dist/lib/browser/edge/index.mjs.map +0 -7
  89. package/dist/lib/browser/index.mjs +0 -1395
  90. package/dist/lib/browser/index.mjs.map +0 -7
  91. package/dist/lib/browser/meta.json +0 -1
  92. package/dist/lib/browser/testing/index.mjs +0 -131
  93. package/dist/lib/browser/testing/index.mjs.map +0 -7
  94. package/dist/lib/node-esm/bundler/index.mjs +0 -270
  95. package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
  96. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
  97. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +0 -7
  98. package/dist/lib/node-esm/chunk-NAQIKLZB.mjs +0 -667
  99. package/dist/lib/node-esm/chunk-NAQIKLZB.mjs.map +0 -7
  100. package/dist/lib/node-esm/edge/index.mjs +0 -84
  101. package/dist/lib/node-esm/edge/index.mjs.map +0 -7
  102. package/dist/lib/node-esm/index.mjs +0 -1396
  103. package/dist/lib/node-esm/index.mjs.map +0 -7
  104. package/dist/lib/node-esm/meta.json +0 -1
  105. package/dist/lib/node-esm/testing/index.mjs +0 -132
  106. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  107. package/dist/types/src/bundler/bundler.d.ts +0 -49
  108. package/dist/types/src/bundler/bundler.d.ts.map +0 -1
  109. package/dist/types/src/bundler/bundler.test.d.ts +0 -2
  110. package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
  111. package/dist/types/src/bundler/index.d.ts +0 -2
  112. package/dist/types/src/bundler/index.d.ts.map +0 -1
  113. package/dist/types/src/edge/functions.d.ts +0 -17
  114. package/dist/types/src/edge/functions.d.ts.map +0 -1
  115. package/dist/types/src/edge/index.d.ts +0 -2
  116. package/dist/types/src/edge/index.d.ts.map +0 -1
  117. package/dist/types/src/examples/fib.d.ts.map +0 -1
  118. package/dist/types/src/examples/index.d.ts +0 -4
  119. package/dist/types/src/examples/index.d.ts.map +0 -1
  120. package/dist/types/src/examples/reply.d.ts.map +0 -1
  121. package/dist/types/src/examples/sleep.d.ts.map +0 -1
  122. package/dist/types/src/executor/executor.d.ts +0 -14
  123. package/dist/types/src/executor/executor.d.ts.map +0 -1
  124. package/dist/types/src/executor/index.d.ts +0 -2
  125. package/dist/types/src/executor/index.d.ts.map +0 -1
  126. package/dist/types/src/handler.d.ts +0 -109
  127. package/dist/types/src/handler.d.ts.map +0 -1
  128. package/dist/types/src/schema.d.ts +0 -43
  129. package/dist/types/src/schema.d.ts.map +0 -1
  130. package/dist/types/src/services/database.d.ts +0 -67
  131. package/dist/types/src/services/database.d.ts.map +0 -1
  132. package/dist/types/src/services/function-invocation-service.test.d.ts +0 -2
  133. package/dist/types/src/services/function-invocation-service.test.d.ts.map +0 -1
  134. package/dist/types/src/services/local-function-execution.d.ts +0 -34
  135. package/dist/types/src/services/local-function-execution.d.ts.map +0 -1
  136. package/dist/types/src/services/remote-function-execution-service.d.ts +0 -22
  137. package/dist/types/src/services/remote-function-execution-service.d.ts.map +0 -1
  138. package/dist/types/src/services/service-container.d.ts +0 -57
  139. package/dist/types/src/services/service-container.d.ts.map +0 -1
  140. package/dist/types/src/services/service-registry.d.ts +0 -31
  141. package/dist/types/src/services/service-registry.d.ts.map +0 -1
  142. package/dist/types/src/services/service-registry.test.d.ts +0 -2
  143. package/dist/types/src/services/service-registry.test.d.ts.map +0 -1
  144. package/dist/types/src/testing/index.d.ts +0 -3
  145. package/dist/types/src/testing/index.d.ts.map +0 -1
  146. package/dist/types/src/testing/layer.d.ts +0 -18
  147. package/dist/types/src/testing/layer.d.ts.map +0 -1
  148. package/dist/types/src/testing/logger.d.ts +0 -5
  149. package/dist/types/src/testing/logger.d.ts.map +0 -1
  150. package/dist/types/src/testing/persist-database.test.d.ts +0 -2
  151. package/dist/types/src/testing/persist-database.test.d.ts.map +0 -1
  152. package/dist/types/src/testing/services.d.ts +0 -59
  153. package/dist/types/src/testing/services.d.ts.map +0 -1
  154. package/dist/types/src/trace.d.ts +0 -122
  155. package/dist/types/src/trace.d.ts.map +0 -1
  156. package/dist/types/src/translations.d.ts +0 -12
  157. package/dist/types/src/translations.d.ts.map +0 -1
  158. package/dist/types/src/triggers/index.d.ts +0 -4
  159. package/dist/types/src/triggers/index.d.ts.map +0 -1
  160. package/dist/types/src/triggers/input-builder.d.ts +0 -3
  161. package/dist/types/src/triggers/input-builder.d.ts.map +0 -1
  162. package/dist/types/src/triggers/invocation-tracer.d.ts +0 -37
  163. package/dist/types/src/triggers/invocation-tracer.d.ts.map +0 -1
  164. package/dist/types/src/triggers/trigger-dispatcher.d.ts +0 -78
  165. package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +0 -1
  166. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +0 -2
  167. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +0 -1
  168. package/dist/types/src/triggers/trigger-state-store.d.ts +0 -28
  169. package/dist/types/src/triggers/trigger-state-store.d.ts.map +0 -1
  170. package/dist/types/src/types.d.ts +0 -230
  171. package/dist/types/src/types.d.ts.map +0 -1
  172. package/dist/types/src/url.d.ts +0 -21
  173. package/dist/types/src/url.d.ts.map +0 -1
  174. package/src/bundler/bundler.test.ts +0 -58
  175. package/src/bundler/bundler.ts +0 -295
  176. package/src/bundler/index.ts +0 -5
  177. package/src/edge/functions.ts +0 -67
  178. package/src/edge/index.ts +0 -9
  179. package/src/examples/index.ts +0 -7
  180. package/src/executor/executor.ts +0 -58
  181. package/src/handler.ts +0 -225
  182. package/src/schema.ts +0 -71
  183. package/src/services/database.ts +0 -175
  184. package/src/services/function-invocation-service.test.ts +0 -81
  185. package/src/services/local-function-execution.ts +0 -153
  186. package/src/services/remote-function-execution-service.ts +0 -63
  187. package/src/services/service-container.ts +0 -115
  188. package/src/services/service-registry.test.ts +0 -45
  189. package/src/services/service-registry.ts +0 -63
  190. package/src/testing/index.ts +0 -6
  191. package/src/testing/layer.ts +0 -114
  192. package/src/testing/logger.ts +0 -17
  193. package/src/testing/persist-database.test.ts +0 -87
  194. package/src/testing/services.ts +0 -115
  195. package/src/trace.ts +0 -178
  196. package/src/translations.ts +0 -20
  197. package/src/triggers/index.ts +0 -7
  198. package/src/triggers/input-builder.ts +0 -35
  199. package/src/triggers/invocation-tracer.ts +0 -101
  200. package/src/triggers/trigger-dispatcher.test.ts +0 -665
  201. package/src/triggers/trigger-dispatcher.ts +0 -533
  202. package/src/triggers/trigger-state-store.ts +0 -61
  203. package/src/types.ts +0 -218
  204. package/src/url.ts +0 -55
@@ -0,0 +1,262 @@
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, Ref, Type } from '@dxos/echo';
15
+ import { refFromEncodedReference } from '@dxos/echo/internal';
16
+ import { EchoClient, type EchoDatabaseImpl, type QueueFactory } from '@dxos/echo-db';
17
+ import { runAndForwardErrors } from '@dxos/effect';
18
+ import { assertState, failedInvariant, invariant } from '@dxos/invariant';
19
+ import { PublicKey } from '@dxos/keys';
20
+ import { type FunctionProtocol } from '@dxos/protocols';
21
+
22
+ import { FunctionError } from '../errors';
23
+ import { FunctionDefinition, type FunctionServices } from '../sdk';
24
+ import { CredentialsService, FunctionInvocationService, QueueService, TracingService } from '../services';
25
+
26
+ import { FunctionsAiHttpClient } from './functions-ai-http-client';
27
+
28
+ /**
29
+ * Wraps a function handler made with `defineFunction` to a protocol that the functions-runtime expects.
30
+ */
31
+ export const wrapFunctionHandler = (func: FunctionDefinition): FunctionProtocol.Func => {
32
+ if (!FunctionDefinition.isFunction(func)) {
33
+ throw new TypeError('Invalid function definition');
34
+ }
35
+
36
+ return {
37
+ meta: {
38
+ key: func.key,
39
+ name: func.name,
40
+ description: func.description,
41
+ inputSchema: Type.toJsonSchema(func.inputSchema),
42
+ outputSchema: func.outputSchema === undefined ? undefined : Type.toJsonSchema(func.outputSchema),
43
+ services: func.services,
44
+ },
45
+ handler: async ({ data, context }) => {
46
+ if (
47
+ (func.services.includes(Database.Service.key) || func.services.includes(QueueService.key)) &&
48
+ (!context.services.dataService || !context.services.queryService)
49
+ ) {
50
+ throw new FunctionError({
51
+ message: 'Services not provided: dataService, queryService',
52
+ });
53
+ }
54
+
55
+ // eslint-disable-next-line no-useless-catch
56
+ try {
57
+ if (!SchemaAST.isAnyKeyword(func.inputSchema.ast)) {
58
+ try {
59
+ Schema.validateSync(func.inputSchema)(data);
60
+ } catch (error) {
61
+ throw new FunctionError({ message: 'Invalid input schema', cause: error });
62
+ }
63
+ }
64
+
65
+ await using funcContext = await new FunctionContext(context).open();
66
+
67
+ if (func.types.length > 0) {
68
+ invariant(funcContext.db, 'Database is required for functions with types');
69
+ await funcContext.db.graph.schemaRegistry.register(func.types as Type.Entity.Any[]);
70
+ }
71
+
72
+ const dataWithDecodedRefs =
73
+ funcContext.db && !SchemaAST.isAnyKeyword(func.inputSchema.ast)
74
+ ? decodeRefsFromSchema(func.inputSchema.ast, data, funcContext.db)
75
+ : data;
76
+
77
+ let result = await func.handler({
78
+ // TODO(dmaretskyi): Fix the types.
79
+ context: context as any,
80
+ data: dataWithDecodedRefs,
81
+ });
82
+
83
+ if (Effect.isEffect(result)) {
84
+ result = await runAndForwardErrors(
85
+ (result as Effect.Effect<unknown, unknown, FunctionServices>).pipe(
86
+ Effect.orDie,
87
+ Effect.provide(funcContext.createLayer()),
88
+ ),
89
+ );
90
+ }
91
+
92
+ if (func.outputSchema && !SchemaAST.isAnyKeyword(func.outputSchema.ast)) {
93
+ Schema.validateSync(func.outputSchema)(result);
94
+ }
95
+
96
+ return result;
97
+ } catch (error) {
98
+ // TODO(dmaretskyi): We might do error wrapping here and add extra context.
99
+ throw error;
100
+ }
101
+ },
102
+ };
103
+ };
104
+
105
+ /**
106
+ * Container for services and context for a function.
107
+ */
108
+ class FunctionContext extends Resource {
109
+ readonly context: FunctionProtocol.Context;
110
+ readonly client: EchoClient | undefined;
111
+ db: EchoDatabaseImpl | undefined;
112
+ queues: QueueFactory | undefined;
113
+
114
+ constructor(context: FunctionProtocol.Context) {
115
+ super();
116
+ this.context = context;
117
+ if (context.services.dataService && context.services.queryService) {
118
+ this.client = new EchoClient().connectToService({
119
+ dataService: context.services.dataService,
120
+ queryService: context.services.queryService,
121
+ queueService: context.services.queueService,
122
+ });
123
+ }
124
+ }
125
+
126
+ override async _open() {
127
+ await this.client?.open();
128
+ this.db =
129
+ this.client && this.context.spaceId
130
+ ? this.client.constructDatabase({
131
+ spaceId: this.context.spaceId ?? failedInvariant(),
132
+ spaceKey: PublicKey.fromHex(this.context.spaceKey ?? failedInvariant('spaceKey missing in context')),
133
+ reactiveSchemaQuery: false,
134
+ preloadSchemaOnOpen: false,
135
+ })
136
+ : undefined;
137
+
138
+ await this.db?.setSpaceRoot(this.context.spaceRootUrl ?? failedInvariant('spaceRootUrl missing in context'));
139
+ await this.db?.open();
140
+ this.queues =
141
+ this.client && this.context.spaceId ? this.client.constructQueueFactory(this.context.spaceId) : undefined;
142
+ }
143
+
144
+ override async _close() {
145
+ await this.db?.close();
146
+ await this.client?.close();
147
+ }
148
+
149
+ createLayer(): Layer.Layer<FunctionServices> {
150
+ assertState(this._lifecycleState === LifecycleState.OPEN, 'FunctionContext is not open');
151
+
152
+ const dbLayer = this.db ? Database.layer(this.db) : Database.notAvailable;
153
+ const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
154
+ const credentials = dbLayer
155
+ ? CredentialsService.layerFromDatabase({ caching: true }).pipe(Layer.provide(dbLayer))
156
+ : CredentialsService.configuredLayer([]);
157
+ const functionInvocationService = MockedFunctionInvocationService;
158
+ const tracing = TracingService.layerNoop;
159
+
160
+ const aiLayer = this.context.services.functionsAiService
161
+ ? AiModelResolver.AiModelResolver.buildAiService.pipe(
162
+ Layer.provide(
163
+ AnthropicResolver.make().pipe(
164
+ Layer.provide(
165
+ AnthropicClient.layer({
166
+ // Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
167
+ apiUrl: 'http://internal/provider/anthropic',
168
+ }).pipe(Layer.provide(FunctionsAiHttpClient.layer(this.context.services.functionsAiService))),
169
+ ),
170
+ ),
171
+ ),
172
+ )
173
+ : AiService.notAvailable;
174
+
175
+ return Layer.mergeAll(
176
+ dbLayer, //
177
+ queuesLayer,
178
+ credentials,
179
+ functionInvocationService,
180
+ aiLayer,
181
+ tracing,
182
+ );
183
+ }
184
+ }
185
+
186
+ const MockedFunctionInvocationService = Layer.succeed(FunctionInvocationService, {
187
+ invokeFunction: () => Effect.die('Calling functions from functions is not implemented yet.'),
188
+ resolveFunction: () => Effect.die('Not implemented.'),
189
+ });
190
+
191
+ const decodeRefsFromSchema = (ast: SchemaAST.AST, value: unknown, db: EchoDatabaseImpl): unknown => {
192
+ if (value == null) {
193
+ return value;
194
+ }
195
+
196
+ const encoded = SchemaAST.encodedBoundAST(ast);
197
+ if (Ref.isRefType(encoded)) {
198
+ if (Ref.isRef(value)) {
199
+ return value;
200
+ }
201
+
202
+ if (typeof value === 'object' && value !== null && typeof (value as any)['/'] === 'string') {
203
+ const resolver = db.graph.createRefResolver({ context: { space: db.spaceId } });
204
+ return refFromEncodedReference(value as any, resolver);
205
+ }
206
+
207
+ return value;
208
+ }
209
+
210
+ switch (encoded._tag) {
211
+ case 'TypeLiteral': {
212
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
213
+ return value;
214
+ }
215
+ const result: Record<string, unknown> = { ...(value as any) };
216
+ for (const prop of SchemaAST.getPropertySignatures(encoded)) {
217
+ const key = prop.name.toString();
218
+ if (key in result) {
219
+ result[key] = decodeRefsFromSchema(prop.type, (result as any)[key], db);
220
+ }
221
+ }
222
+ return result;
223
+ }
224
+
225
+ case 'TupleType': {
226
+ if (!Array.isArray(value)) {
227
+ return value;
228
+ }
229
+
230
+ // For arrays, effect uses TupleType with empty elements and a single rest element.
231
+ if (encoded.elements.length === 0 && encoded.rest.length === 1) {
232
+ const elementType = encoded.rest[0].type;
233
+ return (value as unknown[]).map((item) => decodeRefsFromSchema(elementType, item, db));
234
+ }
235
+
236
+ return value;
237
+ }
238
+
239
+ case 'Union': {
240
+ // Optional values are represented as union with undefined.
241
+ const nonUndefined = encoded.types.filter((t) => !SchemaAST.isUndefinedKeyword(t));
242
+ if (nonUndefined.length === 1) {
243
+ return decodeRefsFromSchema(nonUndefined[0], value, db);
244
+ }
245
+
246
+ // For other unions we can't safely pick a branch without validating.
247
+ return value;
248
+ }
249
+
250
+ case 'Suspend': {
251
+ return decodeRefsFromSchema(encoded.f(), value, db);
252
+ }
253
+
254
+ case 'Refinement': {
255
+ return decodeRefsFromSchema(encoded.from, value, db);
256
+ }
257
+
258
+ default: {
259
+ return value;
260
+ }
261
+ }
262
+ };
package/src/sdk.ts ADDED
@@ -0,0 +1,289 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import type * as Context from 'effect/Context';
6
+ import * as Effect from 'effect/Effect';
7
+ import * as Schema from 'effect/Schema';
8
+
9
+ import { type AiService } from '@dxos/ai';
10
+ import { Obj, Type } from '@dxos/echo';
11
+ import { type Database } from '@dxos/echo';
12
+ import { assertArgument, failedInvariant } from '@dxos/invariant';
13
+ import { Operation } from '@dxos/operation';
14
+
15
+ import {
16
+ type CredentialsService,
17
+ type FunctionInvocationService,
18
+ type QueueService,
19
+ type TracingService,
20
+ } from './services';
21
+ import { Function } from './types';
22
+ import { getUserFunctionIdInMetadata, setUserFunctionIdInMetadata } from './types';
23
+
24
+ // TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
25
+ // https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
26
+ // https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
27
+ // https://www.npmjs.com/package/aws-lambda
28
+
29
+ /**
30
+ * Services that are provided at the function call site by the caller.
31
+ */
32
+ export type InvocationServices = TracingService;
33
+
34
+ /**
35
+ * Services that are available to invoked functions.
36
+ */
37
+ export type FunctionServices =
38
+ | InvocationServices
39
+ | AiService.AiService
40
+ | CredentialsService
41
+ | Database.Service
42
+ | QueueService
43
+ | FunctionInvocationService;
44
+
45
+ /**
46
+ * Function handler.
47
+ */
48
+ export type FunctionHandler<TData = {}, TOutput = any, S extends FunctionServices = FunctionServices> = (params: {
49
+ /**
50
+ * Context available to the function.
51
+ */
52
+ context: FunctionContext;
53
+
54
+ /**
55
+ * Data passed as the input to the function.
56
+ * Must match the function's input schema.
57
+ * This will be the payload from the trigger or other data passed into the function in a workflow.
58
+ */
59
+ data: TData;
60
+ }) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any, S>;
61
+
62
+ /**
63
+ * Function context.
64
+ */
65
+ export interface FunctionContext {
66
+ // TODO(dmaretskyi): Consider what we should put into context.
67
+ }
68
+
69
+ const typeId = Symbol.for('@dxos/functions/FunctionDefinition');
70
+
71
+ export type FunctionDefinition<T = any, O = any, S extends FunctionServices = FunctionServices> = {
72
+ [typeId]: true;
73
+ key: string;
74
+ name: string;
75
+ description?: string;
76
+ inputSchema: Schema.Schema<T, any>;
77
+ outputSchema?: Schema.Schema<O, any>;
78
+
79
+ /**
80
+ * List of types the function uses.
81
+ * This is used to ensure that the types are available when the function is executed.
82
+ */
83
+ types: readonly Type.Entity.Any[];
84
+
85
+ /**
86
+ * Keys of the required services.
87
+ */
88
+ services: readonly string[];
89
+
90
+ handler: FunctionHandler<T, O, S>;
91
+ meta?: {
92
+ /**
93
+ * Tools that are projected from functions have this annotation.
94
+ *
95
+ * deployedFunctionId:
96
+ * - Backend deployment ID assigned by the EDGE function service (typically a UUID).
97
+ * - Used for remote invocation via `FunctionInvocationService` → `RemoteFunctionExecutionService`.
98
+ * - Persisted on the corresponding ECHO `Function.Function` object's metadata under the
99
+ * `FUNCTIONS_META_KEY` and retrieved with `getUserFunctionIdInMetadata`.
100
+ */
101
+ deployedFunctionId?: string;
102
+ };
103
+ };
104
+
105
+ export declare namespace FunctionDefinition {
106
+ export type Any = FunctionDefinition<any, any, any>;
107
+ export type Input<T extends Any> = T extends FunctionDefinition<infer I, infer _O, infer _S> ? I : never;
108
+ export type Output<T extends Any> = T extends FunctionDefinition<infer _I, infer O, infer _S> ? O : never;
109
+ export type Services<T extends Any> = T extends FunctionDefinition<infer _I, infer _O, infer S> ? S : never;
110
+ }
111
+
112
+ export type FunctionProps<T, O> = {
113
+ key: string;
114
+ name: string;
115
+ description?: string;
116
+ inputSchema: Schema.Schema<T, any>;
117
+ outputSchema?: Schema.Schema<O, any>;
118
+
119
+ /**
120
+ * List of types the function uses.
121
+ * This is used to ensure that the types are available when the function is executed.
122
+ */
123
+ types?: readonly Type.Entity.Any[];
124
+ // TODO(dmaretskyi): This currently doesn't cause a compile-time error if the handler requests a service that is not specified
125
+ services?: readonly Context.Tag<any, any>[];
126
+
127
+ handler: FunctionHandler<T, O, FunctionServices>;
128
+ };
129
+
130
+ // TODO(dmaretskyi): Output type doesn't get typechecked.
131
+ export const defineFunction: {
132
+ <I, O>(params: FunctionProps<I, O>): FunctionDefinition<I, O, FunctionServices>;
133
+ } = ({ key, name, description, inputSchema, outputSchema = Schema.Any, handler, types, services }) => {
134
+ if (!Schema.isSchema(inputSchema)) {
135
+ throw new Error('Input schema must be a valid schema');
136
+ }
137
+ if (typeof handler !== 'function') {
138
+ throw new Error('Handler must be a function');
139
+ }
140
+
141
+ // Captures the function definition location.
142
+ const limit = Error.stackTraceLimit;
143
+ Error.stackTraceLimit = 2;
144
+ const traceError = new Error();
145
+ Error.stackTraceLimit = limit;
146
+ let cache: false | string = false;
147
+ const captureStackTrace = () => {
148
+ if (cache !== false) {
149
+ return cache;
150
+ }
151
+ if (traceError.stack !== undefined) {
152
+ const stack = traceError.stack.split('\n');
153
+ if (stack[2] !== undefined) {
154
+ cache = stack[2].trim();
155
+ return cache;
156
+ }
157
+ }
158
+ };
159
+
160
+ const handlerWithSpan = (...args: any[]) => {
161
+ const result = (handler as any)(...args);
162
+ if (Effect.isEffect(result)) {
163
+ return Effect.withSpan(result, `${key ?? name}`, {
164
+ captureStackTrace,
165
+ });
166
+ }
167
+ return result;
168
+ };
169
+
170
+ return {
171
+ [typeId]: true,
172
+ key,
173
+ name,
174
+ description,
175
+ inputSchema,
176
+ outputSchema,
177
+ handler: handlerWithSpan,
178
+ types: types ?? [],
179
+ services: !services ? [] : getServiceKeys(services),
180
+ } satisfies FunctionDefinition.Any;
181
+ };
182
+
183
+ const getServiceKeys = (services: readonly Context.Tag<any, any>[]) => {
184
+ return services.map((tag: any) => {
185
+ if (typeof tag.key === 'string') {
186
+ return tag.key;
187
+ }
188
+ failedInvariant();
189
+ });
190
+ };
191
+
192
+ /**
193
+ * Converts a FunctionDefinition to an OperationDefinition with handler.
194
+ * The function handler is adapted to the OperationHandler format.
195
+ *
196
+ * Note: FunctionDefinition stores service keys as strings, not Tag types,
197
+ * so we can't use Operation.withHandler's type inference here.
198
+ */
199
+ export const toOperation = <T, O, S extends FunctionServices = FunctionServices>(
200
+ functionDef: FunctionDefinition<T, O, S>,
201
+ ): Operation.Definition<T, O> & { handler: Operation.Handler<T, O, any, S> } => {
202
+ const op = Operation.make({
203
+ schema: {
204
+ input: functionDef.inputSchema,
205
+ output: functionDef.outputSchema ?? Schema.Any,
206
+ },
207
+ meta: {
208
+ key: functionDef.key,
209
+ name: functionDef.name,
210
+ description: functionDef.description,
211
+ },
212
+ });
213
+
214
+ // Adapt FunctionHandler signature to OperationHandler format.
215
+ // FunctionHandler expects { context, data }, OperationHandler expects just input.
216
+ const operationHandler: Operation.Handler<T, O, any, S> = (input: T) => {
217
+ const result = functionDef.handler({
218
+ context: {} as FunctionContext,
219
+ data: input,
220
+ });
221
+
222
+ // Convert Promise or plain value to Effect.
223
+ if (Effect.isEffect(result)) {
224
+ return result;
225
+ }
226
+ if (result instanceof Promise) {
227
+ return Effect.tryPromise(() => result);
228
+ }
229
+ return Effect.succeed(result as O);
230
+ };
231
+
232
+ // Manually attach handler since FunctionDefinition stores service keys as strings,
233
+ // not Tag types, so withHandler's type inference doesn't apply.
234
+ return {
235
+ ...op,
236
+ handler: operationHandler,
237
+ };
238
+ };
239
+
240
+ export const FunctionDefinition = {
241
+ make: defineFunction,
242
+ isFunction: (value: unknown): value is FunctionDefinition.Any => {
243
+ return typeof value === 'object' && value !== null && Symbol.for('@dxos/functions/FunctionDefinition') in value;
244
+ },
245
+ serialize: (functionDef: FunctionDefinition.Any): Function.Function => {
246
+ assertArgument(FunctionDefinition.isFunction(functionDef), 'functionDef');
247
+ return serializeFunction(functionDef);
248
+ },
249
+ deserialize: (functionObj: Function.Function): FunctionDefinition.Any => {
250
+ assertArgument(Obj.instanceOf(Function.Function, functionObj), 'functionObj');
251
+ return deserializeFunction(functionObj);
252
+ },
253
+ toOperation,
254
+ };
255
+
256
+ export const serializeFunction = (functionDef: FunctionDefinition.Any): Function.Function => {
257
+ const fn = Function.make({
258
+ key: functionDef.key,
259
+ name: functionDef.name,
260
+ version: '0.1.0',
261
+ description: functionDef.description,
262
+ inputSchema: Type.toJsonSchema(functionDef.inputSchema),
263
+ outputSchema: !functionDef.outputSchema ? undefined : Type.toJsonSchema(functionDef.outputSchema),
264
+ services: functionDef.services,
265
+ });
266
+ if (functionDef.meta?.deployedFunctionId) {
267
+ Obj.change(fn, (fn) => setUserFunctionIdInMetadata(Obj.getMeta(fn), functionDef.meta!.deployedFunctionId!));
268
+ }
269
+ return fn;
270
+ };
271
+
272
+ export const deserializeFunction = (functionObj: Function.Function): FunctionDefinition<unknown, unknown> => {
273
+ return {
274
+ [typeId]: true,
275
+ // TODO(dmaretskyi): Fix key.
276
+ key: functionObj.key ?? functionObj.name,
277
+ name: functionObj.name,
278
+ description: functionObj.description,
279
+ inputSchema: !functionObj.inputSchema ? Schema.Unknown : Type.toEffectSchema(functionObj.inputSchema),
280
+ outputSchema: !functionObj.outputSchema ? undefined : Type.toEffectSchema(functionObj.outputSchema),
281
+ // TODO(dmaretskyi): This should throw error.
282
+ handler: () => {},
283
+ services: functionObj.services ?? [],
284
+ types: [],
285
+ meta: {
286
+ deployedFunctionId: getUserFunctionIdInMetadata(Obj.getMeta(functionObj)),
287
+ },
288
+ };
289
+ };
@@ -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
+ });
@@ -17,12 +17,18 @@ export const ComputeEventPayload = Schema.Union(
17
17
  Schema.Struct({
18
18
  type: Schema.Literal('begin-compute'),
19
19
  nodeId: Schema.String,
20
- inputs: Schema.Record({ key: Schema.String, value: Schema.Any }),
20
+ /**
21
+ * Names of the inputs begin computed.
22
+ */
23
+ inputs: Schema.Array(Schema.String),
21
24
  }),
22
25
  Schema.Struct({
23
26
  type: Schema.Literal('end-compute'),
24
27
  nodeId: Schema.String,
25
- outputs: Schema.Record({ key: Schema.String, value: Schema.Any }),
28
+ /**
29
+ * Names of the outputs computed.
30
+ */
31
+ outputs: Schema.Array(Schema.String),
26
32
  }),
27
33
  Schema.Struct({
28
34
  type: Schema.Literal('compute-input'),
@@ -46,7 +52,7 @@ export type ComputeEventPayload = Schema.Schema.Type<typeof ComputeEventPayload>
46
52
 
47
53
  export const ComputeEvent = Schema.Struct({
48
54
  payload: ComputeEventPayload,
49
- }).pipe(Type.Obj({ typename: 'dxos.org/type/ComputeEvent', version: '0.1.0' }));
55
+ }).pipe(Type.object({ typename: 'dxos.org/type/ComputeEvent', version: '0.1.0' }));
50
56
 
51
57
  /**
52
58
  * Logs event for the compute workflows.
@@ -69,7 +75,7 @@ export class ComputeEventLogger extends Context.Tag('@dxos/functions/ComputeEven
69
75
  const tracing = yield* TracingService;
70
76
  return {
71
77
  log: (event: ComputeEventPayload) => {
72
- tracing.write(Obj.make(ComputeEvent, { payload: event }));
78
+ tracing.write(Obj.make(ComputeEvent, { payload: event }), tracing.getTraceContext());
73
79
  },
74
80
  nodeId: undefined,
75
81
  };