@dxos/functions 0.8.4-main.84f28bd → 0.8.4-main.8baae0fced

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 (135) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +5 -7
  3. package/dist/lib/neutral/index.mjs +602 -0
  4. package/dist/lib/neutral/index.mjs.map +7 -0
  5. package/dist/lib/neutral/meta.json +1 -0
  6. package/dist/types/src/index.d.ts +3 -6
  7. package/dist/types/src/index.d.ts.map +1 -1
  8. package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
  9. package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
  10. package/dist/types/src/protocol/functions-ai-http-client.test.d.ts +2 -0
  11. package/dist/types/src/protocol/functions-ai-http-client.test.d.ts.map +1 -0
  12. package/dist/types/src/protocol/index.d.ts +2 -0
  13. package/dist/types/src/protocol/index.d.ts.map +1 -0
  14. package/dist/types/src/protocol/protocol.d.ts +24 -0
  15. package/dist/types/src/protocol/protocol.d.ts.map +1 -0
  16. package/dist/types/src/protocol/protocol.test.d.ts +2 -0
  17. package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
  18. package/dist/types/src/sdk.d.ts +10 -0
  19. package/dist/types/src/sdk.d.ts.map +1 -0
  20. package/dist/types/src/services/credentials.d.ts +24 -27
  21. package/dist/types/src/services/credentials.d.ts.map +1 -1
  22. package/dist/types/src/services/function-invocation-service.d.ts +18 -0
  23. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
  24. package/dist/types/src/services/index.d.ts +3 -8
  25. package/dist/types/src/services/index.d.ts.map +1 -1
  26. package/dist/types/src/services/queues.d.ts +1 -17
  27. package/dist/types/src/services/queues.d.ts.map +1 -1
  28. package/dist/types/src/services/tracing.d.ts +4 -14
  29. package/dist/types/src/services/tracing.d.ts.map +1 -1
  30. package/dist/types/src/types/index.d.ts +2 -0
  31. package/dist/types/src/types/index.d.ts.map +1 -0
  32. package/dist/types/src/types/url.d.ts +13 -0
  33. package/dist/types/src/types/url.d.ts.map +1 -0
  34. package/dist/types/tsconfig.tsbuildinfo +1 -1
  35. package/package.json +29 -69
  36. package/src/index.ts +3 -8
  37. package/src/protocol/functions-ai-http-client.test.ts +105 -0
  38. package/src/protocol/functions-ai-http-client.ts +141 -0
  39. package/src/{executor → protocol}/index.ts +1 -1
  40. package/src/protocol/protocol.test.ts +58 -0
  41. package/src/protocol/protocol.ts +455 -0
  42. package/src/sdk.ts +31 -0
  43. package/src/services/credentials.ts +95 -35
  44. package/src/services/function-invocation-service.ts +36 -0
  45. package/src/services/index.ts +3 -8
  46. package/src/services/queues.ts +1 -38
  47. package/src/services/tracing.ts +4 -23
  48. package/src/{testing → types}/index.ts +1 -1
  49. package/src/types/url.ts +32 -0
  50. package/dist/lib/browser/bundler/index.mjs +0 -247
  51. package/dist/lib/browser/bundler/index.mjs.map +0 -7
  52. package/dist/lib/browser/chunk-54U464M4.mjs +0 -360
  53. package/dist/lib/browser/chunk-54U464M4.mjs.map +0 -7
  54. package/dist/lib/browser/edge/index.mjs +0 -69
  55. package/dist/lib/browser/edge/index.mjs.map +0 -7
  56. package/dist/lib/browser/index.mjs +0 -492
  57. package/dist/lib/browser/index.mjs.map +0 -7
  58. package/dist/lib/browser/meta.json +0 -1
  59. package/dist/lib/browser/testing/index.mjs +0 -79
  60. package/dist/lib/browser/testing/index.mjs.map +0 -7
  61. package/dist/lib/node-esm/bundler/index.mjs +0 -249
  62. package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
  63. package/dist/lib/node-esm/chunk-XDSX35BS.mjs +0 -362
  64. package/dist/lib/node-esm/chunk-XDSX35BS.mjs.map +0 -7
  65. package/dist/lib/node-esm/edge/index.mjs +0 -71
  66. package/dist/lib/node-esm/edge/index.mjs.map +0 -7
  67. package/dist/lib/node-esm/index.mjs +0 -493
  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 -80
  71. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  72. package/dist/types/src/bundler/bundler.d.ts +0 -50
  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 -16
  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/executor/executor.d.ts +0 -8
  83. package/dist/types/src/executor/executor.d.ts.map +0 -1
  84. package/dist/types/src/executor/index.d.ts +0 -2
  85. package/dist/types/src/executor/index.d.ts.map +0 -1
  86. package/dist/types/src/handler.d.ts +0 -61
  87. package/dist/types/src/handler.d.ts.map +0 -1
  88. package/dist/types/src/schema.d.ts +0 -38
  89. package/dist/types/src/schema.d.ts.map +0 -1
  90. package/dist/types/src/services/ai.d.ts +0 -12
  91. package/dist/types/src/services/ai.d.ts.map +0 -1
  92. package/dist/types/src/services/database.d.ts +0 -15
  93. package/dist/types/src/services/database.d.ts.map +0 -1
  94. package/dist/types/src/services/event-logger.d.ts +0 -37
  95. package/dist/types/src/services/event-logger.d.ts.map +0 -1
  96. package/dist/types/src/services/function-call-service.d.ts +0 -16
  97. package/dist/types/src/services/function-call-service.d.ts.map +0 -1
  98. package/dist/types/src/services/service-container.d.ts +0 -46
  99. package/dist/types/src/services/service-container.d.ts.map +0 -1
  100. package/dist/types/src/services/tool-resolver.d.ts +0 -14
  101. package/dist/types/src/services/tool-resolver.d.ts.map +0 -1
  102. package/dist/types/src/testing/index.d.ts +0 -2
  103. package/dist/types/src/testing/index.d.ts.map +0 -1
  104. package/dist/types/src/testing/logger.d.ts +0 -5
  105. package/dist/types/src/testing/logger.d.ts.map +0 -1
  106. package/dist/types/src/testing/services.d.ts +0 -71
  107. package/dist/types/src/testing/services.d.ts.map +0 -1
  108. package/dist/types/src/trace.d.ts +0 -124
  109. package/dist/types/src/trace.d.ts.map +0 -1
  110. package/dist/types/src/translations.d.ts +0 -12
  111. package/dist/types/src/translations.d.ts.map +0 -1
  112. package/dist/types/src/types.d.ts +0 -411
  113. package/dist/types/src/types.d.ts.map +0 -1
  114. package/dist/types/src/url.d.ts +0 -17
  115. package/dist/types/src/url.d.ts.map +0 -1
  116. package/src/bundler/bundler.test.ts +0 -59
  117. package/src/bundler/bundler.ts +0 -292
  118. package/src/bundler/index.ts +0 -5
  119. package/src/edge/functions.ts +0 -64
  120. package/src/edge/index.ts +0 -9
  121. package/src/executor/executor.ts +0 -54
  122. package/src/handler.ts +0 -113
  123. package/src/schema.ts +0 -57
  124. package/src/services/ai.ts +0 -32
  125. package/src/services/database.ts +0 -50
  126. package/src/services/event-logger.ts +0 -87
  127. package/src/services/function-call-service.ts +0 -64
  128. package/src/services/service-container.ts +0 -127
  129. package/src/services/tool-resolver.ts +0 -31
  130. package/src/testing/logger.ts +0 -16
  131. package/src/testing/services.ts +0 -174
  132. package/src/trace.ts +0 -180
  133. package/src/translations.ts +0 -20
  134. package/src/types.ts +0 -211
  135. package/src/url.ts +0 -52
@@ -0,0 +1,455 @@
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 Option from 'effect/Option';
9
+ import * as Schema from 'effect/Schema';
10
+ import * as SchemaAST from 'effect/SchemaAST';
11
+
12
+ import { AiModelResolver, AiService, OpaqueToolkit } from '@dxos/ai';
13
+ import { AnthropicResolver } from '@dxos/ai/resolvers';
14
+ import {
15
+ Blueprint,
16
+ FunctionError,
17
+ InvalidOperationInputError,
18
+ InvalidOperationOutputError,
19
+ Operation,
20
+ OperationRegistry,
21
+ Trace,
22
+ } from '@dxos/compute';
23
+ import { LifecycleState, Resource } from '@dxos/context';
24
+ import { Database, Feed, JsonSchema, Ref, type Type } from '@dxos/echo';
25
+ import { createFeedServiceLayer, EchoClient, type EchoDatabaseImpl, type QueueFactory } from '@dxos/echo-db';
26
+ import { refFromEncodedReference } from '@dxos/echo/internal';
27
+ import { runAndForwardErrors } from '@dxos/effect';
28
+ import { assertState, failedInvariant, invariant } from '@dxos/invariant';
29
+ import { PublicKey, type SpaceId } from '@dxos/keys';
30
+ import { log } from '@dxos/log';
31
+ import { EdgeFunctionEnv, ErrorCodec, type FunctionProtocol, type TraceProtocol } from '@dxos/protocols';
32
+
33
+ import { type FunctionServices } from '../sdk';
34
+ import {
35
+ configuredCredentialsLayer,
36
+ credentialsLayerFromDatabase,
37
+ FunctionInvocationService,
38
+ QueueService,
39
+ } from '../services';
40
+ import { FunctionsAiHttpClient } from './functions-ai-http-client';
41
+
42
+ export interface FunctionWrappingOptions {
43
+ /**
44
+ * Additional types to register with the database.
45
+ */
46
+ types?: Type.AnyEntity[];
47
+
48
+ /**
49
+ * Toolkits to make available via the `OpaqueToolkitProvider`.
50
+ */
51
+ toolkits?: OpaqueToolkit.OpaqueToolkit[];
52
+
53
+ /**
54
+ * Blueprint registry to expose as `Blueprint.RegistryService` inside handler Effects.
55
+ * Required for operations that declare `Blueprint.RegistryService` in their `services` list.
56
+ */
57
+ blueprintRegistry?: Blueprint.Registry;
58
+ }
59
+
60
+ /**
61
+ * Wraps a function handler made with `defineFunction` to a protocol that the functions-runtime expects.
62
+ */
63
+ export const wrapFunctionHandler = (
64
+ func: Operation.WithHandler<Operation.Definition.Any>,
65
+ opts: FunctionWrappingOptions = {},
66
+ ): FunctionProtocol.Func => {
67
+ if (!Operation.isOperationWithHandler(func)) {
68
+ throw new TypeError('Expected operation with handler');
69
+ }
70
+
71
+ const serviceTags = func.services.map((service) => service.key);
72
+
73
+ return {
74
+ meta: {
75
+ key: func.meta.key,
76
+ name: func.meta.name,
77
+ description: func.meta.description,
78
+ inputSchema: JsonSchema.toJsonSchema(func.input),
79
+ outputSchema: func.output === undefined ? undefined : JsonSchema.toJsonSchema(func.output),
80
+ services: func.services.map((service) => service.key),
81
+ },
82
+ handler: async ({ data, context }) => {
83
+ if (
84
+ (serviceTags.includes(Database.Service.key) ||
85
+ serviceTags.includes(QueueService.key) ||
86
+ serviceTags.includes(Feed.FeedService.key)) &&
87
+ (!context.services.dataService || !context.services.queryService)
88
+ ) {
89
+ throw new FunctionError({
90
+ message: 'Services not provided: dataService, queryService',
91
+ });
92
+ }
93
+
94
+ // eslint-disable-next-line no-useless-catch
95
+ try {
96
+ if (!SchemaAST.isAnyKeyword(func.input.ast)) {
97
+ try {
98
+ Schema.validateSync(func.input, { onExcessProperty: 'error' })(data);
99
+ } catch (error: any) {
100
+ throw new InvalidOperationInputError({
101
+ message: `Operation input did not match schema (${func.meta.key}): ${error.message}`,
102
+ cause: error,
103
+ });
104
+ }
105
+ }
106
+
107
+ await using funcContext = await new FunctionContext(context, opts).open();
108
+
109
+ const types = [...(opts.types ?? []), ...(func.types ?? [])];
110
+ if (types.length > 0) {
111
+ invariant(funcContext.db, 'Database is required for functions with types');
112
+ await funcContext.db.graph.schemaRegistry.register(types as Type.AnyEntity[]);
113
+ }
114
+
115
+ const dataWithDecodedRefs =
116
+ funcContext.db && !SchemaAST.isAnyKeyword(func.input.ast)
117
+ ? decodeRefsFromSchema(func.input.ast, data, funcContext.db)
118
+ : data;
119
+
120
+ let result: any = await func.handler(dataWithDecodedRefs);
121
+
122
+ if (Effect.isEffect(result)) {
123
+ result = await runAndForwardErrors(
124
+ (result as Effect.Effect<unknown, unknown, FunctionServices>).pipe(
125
+ Effect.orDie,
126
+ Effect.provide(funcContext.createLayer()),
127
+ ),
128
+ );
129
+ }
130
+
131
+ // Flush in-memory ECHO writes before the function scope closes.
132
+ // Writes performed by `db.add` / `db.remove` are buffered in the in-memory
133
+ // `EchoDatabaseImpl` and only pushed across the `DataService` binding when
134
+ // `db.flush({ disk })` is called. `FunctionContext._close` (invoked by the
135
+ // `await using` above) calls `db.close()` but does NOT flush, so mutations
136
+ // performed by handlers that declare `Database.Service` (e.g. `object-create`,
137
+ // `object-update`, `relation-create`) would be silently dropped before reaching
138
+ // the edge `AutomergeReplicator`. Flushing here closes that hole.
139
+ if (serviceTags.includes(Database.Service.key) && funcContext.db) {
140
+ await funcContext.db.flush({ disk: true, indexes: false });
141
+ }
142
+
143
+ if (func.output && !SchemaAST.isAnyKeyword(func.output.ast)) {
144
+ try {
145
+ Schema.validateSync(func.output, { onExcessProperty: 'error' })(result);
146
+ } catch (error: any) {
147
+ throw new InvalidOperationOutputError({
148
+ message: `Operation output did not match schema (${func.meta.key}): ${error.message}`,
149
+ cause: error,
150
+ });
151
+ }
152
+ }
153
+
154
+ return result;
155
+ } catch (error) {
156
+ // TODO(dmaretskyi): We might do error wrapping here and add extra context.
157
+ throw error;
158
+ }
159
+ },
160
+ };
161
+ };
162
+
163
+ /**
164
+ * Container for services and context for a function.
165
+ */
166
+ class FunctionContext extends Resource {
167
+ readonly context: FunctionProtocol.Context;
168
+ readonly client: EchoClient | undefined;
169
+ db: EchoDatabaseImpl | undefined;
170
+ queues: QueueFactory | undefined;
171
+ readonly opts: FunctionWrappingOptions;
172
+
173
+ constructor(context: FunctionProtocol.Context, opts: FunctionWrappingOptions) {
174
+ super();
175
+ this.context = context;
176
+ this.opts = opts;
177
+ if (context.services.dataService && context.services.queryService) {
178
+ this.client = new EchoClient().connectToService({
179
+ dataService: context.services.dataService,
180
+ queryService: context.services.queryService,
181
+ queueService: context.services.queueService,
182
+ });
183
+ }
184
+ }
185
+
186
+ override async _open() {
187
+ await this.client?.open();
188
+ this.db =
189
+ this.client && this.context.spaceId
190
+ ? this.client.constructDatabase({
191
+ spaceId: this.context.spaceId ?? failedInvariant(),
192
+ spaceKey: PublicKey.fromHex(this.context.spaceKey ?? failedInvariant('spaceKey missing in context')),
193
+ reactiveSchemaQuery: false,
194
+ preloadSchemaOnOpen: false,
195
+ })
196
+ : undefined;
197
+
198
+ await this.db?.setSpaceRoot(this.context.spaceRootUrl ?? failedInvariant('spaceRootUrl missing in context'));
199
+ await this.db?.open();
200
+ this.queues =
201
+ this.client && this.context.spaceId ? this.client.constructQueueFactory(this.context.spaceId) : undefined;
202
+ }
203
+
204
+ override async _close() {
205
+ await this.db?.close();
206
+ await this.client?.close();
207
+ }
208
+
209
+ createLayer(): Layer.Layer<FunctionServices> {
210
+ assertState(this._lifecycleState === LifecycleState.OPEN, 'FunctionContext is not open');
211
+
212
+ const dbLayer = this.db ? Database.layer(this.db) : Database.notAvailable;
213
+ const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
214
+ const feedLayer = this.queues ? createFeedServiceLayer(this.queues) : Feed.notAvailable;
215
+ const credentials = dbLayer
216
+ ? credentialsLayerFromDatabase({ caching: true }).pipe(Layer.provide(dbLayer))
217
+ : configuredCredentialsLayer([]);
218
+
219
+ const aiLayer = this.context.services.functionsAiService
220
+ ? InternalAiServiceLayer(this.context.services.functionsAiService)
221
+ : AiService.notAvailable;
222
+
223
+ const operationServiceLayer = this.context.services.functionsService
224
+ ? makeOperationServiceLayer(this.context.services.functionsService)
225
+ : unavailableOperationServiceLayer;
226
+
227
+ const operationRegistryLayer = this.context.services.functionsService
228
+ ? makeOperationRegistryLayer(this.context.services.functionsService, this.context.spaceId as SpaceId | undefined)
229
+ : emptyOperationRegistryLayer;
230
+
231
+ const traceWriterLayer = this.context.services.traceService
232
+ ? makeTraceWriterLayer(this.context.services.traceService)
233
+ : Trace.writerLayerNoop;
234
+
235
+ log('Creating function context layer', {
236
+ traceService: !!this.context.services.traceService,
237
+ functionsService: !!this.context.services.functionsService,
238
+ functionsAiService: !!this.context.services.functionsAiService,
239
+ spaceId: this.context.spaceId,
240
+ spaceRootUrl: this.context.spaceRootUrl,
241
+ toolkits: this.opts.toolkits?.length ?? 0,
242
+ types: this.opts.types?.length ?? 0,
243
+ });
244
+
245
+ const blueprintRegistryLayer = this.opts.blueprintRegistry
246
+ ? Layer.succeed(Blueprint.RegistryService, this.opts.blueprintRegistry)
247
+ : Blueprint.RegistryService.notAvailable;
248
+
249
+ return Layer.mergeAll(
250
+ dbLayer,
251
+ queuesLayer,
252
+ feedLayer,
253
+ credentials,
254
+ operationServiceLayer,
255
+ operationRegistryLayer,
256
+ aiLayer,
257
+ OpaqueToolkit.providerLayer(OpaqueToolkit.merge(...(this.opts.toolkits ?? []))),
258
+ traceWriterLayer,
259
+ blueprintRegistryLayer,
260
+
261
+ // `FunctionInvocationService` is deprecated; new code should yield `Operation.Service`.
262
+ // The cloudflare wrapper provides only the unavailable layer to satisfy the (still-present)
263
+ // type union — handlers that yield it will die at invocation time.
264
+ FunctionInvocationService.layerNotAvailable,
265
+ );
266
+ }
267
+ }
268
+
269
+ /**
270
+ * Backs `Trace.TraceService` with the EDGE-provided `TraceService` so that operation
271
+ * handlers can write trace events that are forwarded to the runtime's trace sink.
272
+ */
273
+ const makeTraceWriterLayer = (traceService: TraceProtocol.TraceService): Layer.Layer<Trace.TraceService> =>
274
+ Layer.succeed(Trace.TraceService, {
275
+ write: (eventType, payload) => {
276
+ log('Writing trace event', {
277
+ eventType: eventType.key,
278
+ });
279
+ traceService.write([
280
+ {
281
+ key: eventType.key,
282
+ isEphemeral: eventType.isEphemeral,
283
+ data: payload,
284
+ },
285
+ ]);
286
+ },
287
+ });
288
+
289
+ /**
290
+ * AI service layer that proxies HTTP requests through the EDGE-provided `FunctionsAiService`.
291
+ */
292
+ const InternalAiServiceLayer = (functionsAiService: EdgeFunctionEnv.FunctionsAiService) =>
293
+ AiModelResolver.AiModelResolver.buildAiService.pipe(
294
+ Layer.provide(
295
+ AnthropicResolver.make().pipe(
296
+ Layer.provide(
297
+ AnthropicClient.layer({
298
+ // Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
299
+ apiUrl: 'http://internal/provider/anthropic',
300
+ }).pipe(Layer.provide(FunctionsAiHttpClient.layer(functionsAiService))),
301
+ ),
302
+ ),
303
+ ),
304
+ );
305
+
306
+ /**
307
+ * Backs `Operation.Service` with the EDGE-provided `FunctionsService` so that operation
308
+ * handlers can invoke other deployed operations remotely. The `deployedId` on the operation
309
+ * definition is used as the routing key.
310
+ */
311
+ const makeOperationServiceLayer = (
312
+ functionsService: EdgeFunctionEnv.FunctionsService,
313
+ ): Layer.Layer<Operation.Service> => {
314
+ const invokeRemote = async (
315
+ op: Operation.Definition.Any,
316
+ input: unknown,
317
+ options?: Operation.InvokeOptions,
318
+ ): Promise<{ data?: unknown; error?: Error }> => {
319
+ invariant(op.meta.deployedId, `Operation '${op.meta.key}' has no deployedId; cannot invoke remotely.`);
320
+ const result = await functionsService.invoke(op.meta.deployedId, input, {
321
+ spaceId: options?.spaceId,
322
+ // Forward the conversation DXN so the remote runtime can rebuild conversation-scoped
323
+ // services (e.g. `AiContext.Service`) needed by operations like `GetContext`.
324
+ conversation: options?.conversation,
325
+ });
326
+ if (result._kind === 'success') {
327
+ return { data: result.data };
328
+ }
329
+ return { error: ErrorCodec.decode(result.error) };
330
+ };
331
+
332
+ return Layer.succeed(Operation.Service, {
333
+ invoke: ((op: Operation.Definition.Any, input: unknown, options?: Operation.InvokeOptions) =>
334
+ Effect.tryPromise(() => invokeRemote(op, input, options)).pipe(
335
+ Effect.orDie,
336
+ Effect.flatMap((outcome) =>
337
+ outcome.error ? Effect.die(outcome.error) : Effect.succeed(outcome.data as never),
338
+ ),
339
+ )) as Operation.OperationService['invoke'],
340
+ schedule: ((op: Operation.Definition.Any, input: unknown) =>
341
+ Effect.sync(() => {
342
+ invariant(op.meta.deployedId, `Operation '${op.meta.key}' has no deployedId; cannot schedule remotely.`);
343
+ // Fire and forget — schedule is intentionally non-awaiting.
344
+ void functionsService.invoke(op.meta.deployedId, input).catch(() => {
345
+ // Swallow errors — schedule is observability-only.
346
+ });
347
+ })) as Operation.OperationService['schedule'],
348
+ invokePromise: ((op: Operation.Definition.Any, input: unknown, options?: Operation.InvokeOptions) =>
349
+ invokeRemote(op, input, options).catch((error: unknown) => ({
350
+ error: error instanceof Error ? error : new Error(String(error)),
351
+ }))) as Operation.OperationService['invokePromise'],
352
+ } satisfies Operation.OperationService);
353
+ };
354
+
355
+ const unavailableOperationServiceLayer = Layer.succeed(Operation.Service, {
356
+ invoke: () => Effect.die('Operation.Service is not available: missing functionsService in EDGE context.'),
357
+ schedule: () => Effect.die('Operation.Service is not available: missing functionsService in EDGE context.'),
358
+ invokePromise: async () => ({
359
+ error: new Error('Operation.Service is not available: missing functionsService in EDGE context.'),
360
+ }),
361
+ } as Operation.OperationService);
362
+
363
+ /**
364
+ * Backs `OperationRegistry.Service` with the EDGE-provided `FunctionsService.query`. Returns
365
+ * the first persistent operation matching the requested key, or `Option.none()` when not found.
366
+ */
367
+ const makeOperationRegistryLayer = (
368
+ functionsService: EdgeFunctionEnv.FunctionsService,
369
+ spaceId: SpaceId | undefined,
370
+ ): Layer.Layer<OperationRegistry.Service> =>
371
+ Layer.succeed(OperationRegistry.Service, {
372
+ resolve: (key: string) =>
373
+ Effect.gen(function* () {
374
+ const records = yield* Effect.tryPromise(() => functionsService.query({ spaceId })).pipe(Effect.orDie);
375
+ const match = (records as Operation.PersistentOperation[]).find((record) => Operation.getKey(record) === key);
376
+ return match ? Option.some(Operation.deserialize(match)) : Option.none();
377
+ }),
378
+ });
379
+
380
+ const emptyOperationRegistryLayer = Layer.succeed(OperationRegistry.Service, {
381
+ resolve: () => Effect.succeed(Option.none()),
382
+ });
383
+
384
+ const decodeRefsFromSchema = (ast: SchemaAST.AST, value: unknown, db: EchoDatabaseImpl): unknown => {
385
+ if (value == null) {
386
+ return value;
387
+ }
388
+
389
+ const encoded = SchemaAST.encodedBoundAST(ast);
390
+ if (Ref.isRefType(encoded)) {
391
+ if (Ref.isRef(value)) {
392
+ return value;
393
+ }
394
+
395
+ if (typeof value === 'object' && value !== null && typeof (value as any)['/'] === 'string') {
396
+ const resolver = db.graph.createRefResolver({ context: { space: db.spaceId } });
397
+ return refFromEncodedReference(value as any, resolver);
398
+ }
399
+
400
+ return value;
401
+ }
402
+
403
+ switch (encoded._tag) {
404
+ case 'TypeLiteral': {
405
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
406
+ return value;
407
+ }
408
+ const result: Record<string, unknown> = { ...(value as any) };
409
+ for (const prop of SchemaAST.getPropertySignatures(encoded)) {
410
+ const key = prop.name.toString();
411
+ if (key in result) {
412
+ result[key] = decodeRefsFromSchema(prop.type, (result as any)[key], db);
413
+ }
414
+ }
415
+ return result;
416
+ }
417
+
418
+ case 'TupleType': {
419
+ if (!Array.isArray(value)) {
420
+ return value;
421
+ }
422
+
423
+ // For arrays, effect uses TupleType with empty elements and a single rest element.
424
+ if (encoded.elements.length === 0 && encoded.rest.length === 1) {
425
+ const elementType = encoded.rest[0].type;
426
+ return (value as unknown[]).map((item) => decodeRefsFromSchema(elementType, item, db));
427
+ }
428
+
429
+ return value;
430
+ }
431
+
432
+ case 'Union': {
433
+ // Optional values are represented as union with undefined.
434
+ const nonUndefined = encoded.types.filter((t) => !SchemaAST.isUndefinedKeyword(t));
435
+ if (nonUndefined.length === 1) {
436
+ return decodeRefsFromSchema(nonUndefined[0], value, db);
437
+ }
438
+
439
+ // For other unions we can't safely pick a branch without validating.
440
+ return value;
441
+ }
442
+
443
+ case 'Suspend': {
444
+ return decodeRefsFromSchema(encoded.f(), value, db);
445
+ }
446
+
447
+ case 'Refinement': {
448
+ return decodeRefsFromSchema(encoded.from, value, db);
449
+ }
450
+
451
+ default: {
452
+ return value;
453
+ }
454
+ }
455
+ };
package/src/sdk.ts ADDED
@@ -0,0 +1,31 @@
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
+ // TODO(dmaretskyi): `FunctionInvocationService` is being phased out in favour of `Operation.Service`;
29
+ // it's kept in the union until `functions-runtime/local-function-execution.ts` migrates.
30
+ | FunctionInvocationService
31
+ | Operation.Service;
@@ -2,54 +2,114 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context } from 'effect';
6
-
7
- type CredentialQuery = {
8
- service?: string;
9
- };
10
-
11
- // TODO(dmaretskyi): Unify with other apis.
12
- // packages/sdk/schema/src/common/access-token.ts
13
- export type ServiceCredential = {
14
- service: string;
15
-
16
- // TODO(dmaretskyi): Build out.
17
- apiKey?: string;
18
- };
19
-
20
- export class CredentialsService extends Context.Tag('CredentialsService')<
21
- CredentialsService,
22
- {
23
- /**
24
- * Query all.
25
- */
26
- queryCredentials: (query: CredentialQuery) => Promise<ServiceCredential[]>;
27
-
28
- /**
29
- * Get a single credential.
30
- * @throws {Error} If no credential is found.
31
- */
32
- getCredential: (query: CredentialQuery) => Promise<ServiceCredential>;
33
- }
34
- >() {}
5
+ import * as HttpClient from '@effect/platform/HttpClient';
6
+ import * as HttpClientRequest from '@effect/platform/HttpClientRequest';
7
+ import type * as Config from 'effect/Config';
8
+ import * as Context from 'effect/Context';
9
+ import * as Effect from 'effect/Effect';
10
+ import * as Layer from 'effect/Layer';
11
+ import * as Redacted from 'effect/Redacted';
12
+
13
+ import { Credential } from '@dxos/compute';
14
+ import { Database, Query } from '@dxos/echo';
15
+ import { AccessToken } from '@dxos/types';
35
16
 
36
- export class ConfiguredCredentialsService implements Context.Tag.Service<CredentialsService> {
37
- constructor(private readonly credentials: ServiceCredential[] = []) {}
17
+ export class ConfiguredCredentialsService implements Context.Tag.Service<Credential.CredentialsService> {
18
+ constructor(private readonly credentials: Credential.ServiceCredential[] = []) {}
38
19
 
39
- addCredentials(credentials: ServiceCredential[]): ConfiguredCredentialsService {
20
+ addCredentials(credentials: Credential.ServiceCredential[]): ConfiguredCredentialsService {
40
21
  this.credentials.push(...credentials);
41
22
  return this;
42
23
  }
43
24
 
44
- async queryCredentials(query: CredentialQuery): Promise<ServiceCredential[]> {
25
+ async queryCredentials(query: Credential.CredentialQuery): Promise<Credential.ServiceCredential[]> {
45
26
  return this.credentials.filter((credential) => credential.service === query.service);
46
27
  }
47
28
 
48
- async getCredential(query: CredentialQuery): Promise<ServiceCredential> {
29
+ async getCredential(query: Credential.CredentialQuery): Promise<Credential.ServiceCredential> {
49
30
  const credential = this.credentials.find((credential) => credential.service === query.service);
50
31
  if (!credential) {
51
32
  throw new Error(`Credential not found for service: ${query.service}`);
52
33
  }
34
+
53
35
  return credential;
54
36
  }
55
37
  }
38
+
39
+ /**
40
+ * Maps the request to include the given token in the Authorization header.
41
+ */
42
+ export const withAuthorization = (token: string, kind?: 'Bearer' | 'Basic') =>
43
+ HttpClient.mapRequest((request) => {
44
+ const authorization = kind ? `${kind} ${token}` : token;
45
+ return HttpClientRequest.setHeader(request, 'Authorization', authorization);
46
+ });
47
+
48
+ export const configuredCredentialsLayer = (credentials: Credential.ServiceCredential[]) =>
49
+ Layer.succeed(Credential.CredentialsService, new ConfiguredCredentialsService(credentials));
50
+
51
+ export const credentialsLayerConfig = (
52
+ credentials: {
53
+ service: string;
54
+ apiKey: Config.Config<Redacted.Redacted<string>>;
55
+ }[],
56
+ ) =>
57
+ Layer.effect(
58
+ Credential.CredentialsService,
59
+ Effect.gen(function* () {
60
+ const serviceCredentials = yield* Effect.forEach(credentials, ({ service, apiKey }) =>
61
+ Effect.gen(function* () {
62
+ return {
63
+ service,
64
+ apiKey: Redacted.value(yield* apiKey),
65
+ };
66
+ }),
67
+ );
68
+
69
+ return new ConfiguredCredentialsService(serviceCredentials);
70
+ }),
71
+ );
72
+
73
+ export const credentialsLayerFromDatabase = ({ caching = false }: { caching?: boolean } = {}) =>
74
+ Layer.effect(
75
+ Credential.CredentialsService,
76
+ Effect.gen(function* () {
77
+ const dbService = yield* Database.Service;
78
+ const cache = new Map<string, Credential.ServiceCredential[]>();
79
+
80
+ const queryCredentials = async (query: Credential.CredentialQuery): Promise<Credential.ServiceCredential[]> => {
81
+ const cacheKey = JSON.stringify(query);
82
+ if (caching && cache.has(cacheKey)) {
83
+ return cache.get(cacheKey)!;
84
+ }
85
+
86
+ const accessTokens = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
87
+ const credentials = accessTokens
88
+ .filter((accessToken) => accessToken.source === query.service)
89
+ .map((accessToken) => ({
90
+ service: accessToken.source,
91
+ apiKey: accessToken.token,
92
+ }));
93
+
94
+ if (caching) {
95
+ cache.set(cacheKey, credentials);
96
+ }
97
+
98
+ return credentials;
99
+ };
100
+
101
+ return {
102
+ getCredential: async (query) => {
103
+ const credentials = await queryCredentials(query);
104
+ if (credentials.length === 0) {
105
+ throw new Error(`Credential not found for service: ${query.service}`);
106
+ }
107
+
108
+ return credentials[0];
109
+ },
110
+ queryCredentials: async (query) => {
111
+ return queryCredentials(query);
112
+ },
113
+ };
114
+ }),
115
+ );
@@ -0,0 +1,36 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+ import * as Context from 'effect/Context';
5
+ import * as Effect from 'effect/Effect';
6
+ import * as Layer from 'effect/Layer';
7
+
8
+ import { type FunctionNotFoundError, Operation } from '@dxos/compute';
9
+
10
+ /**
11
+ * @deprecated
12
+ */
13
+ export class FunctionInvocationService extends Context.Tag('@dxos/functions/FunctionInvocationService')<
14
+ FunctionInvocationService,
15
+ {
16
+ invokeFunction<I, O>(functionDef: Operation.Definition<I, O, any>, input: I): Effect.Effect<O>;
17
+
18
+ resolveFunction(key: string): Effect.Effect<Operation.Definition.Any, FunctionNotFoundError>;
19
+ }
20
+ >() {
21
+ static layerNotAvailable = Layer.succeed(FunctionInvocationService, {
22
+ invokeFunction: () => Effect.die('FunctionInvocationService is not avaialble.'),
23
+ resolveFunction: () => Effect.die('FunctionInvocationService is not available.'),
24
+ });
25
+
26
+ static invokeFunction = <I, O>(
27
+ functionDef: Operation.Definition<I, O, any>,
28
+ input: I,
29
+ ): Effect.Effect<O, never, FunctionInvocationService> =>
30
+ Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.invokeFunction)(functionDef, input);
31
+
32
+ static resolveFunction = (
33
+ key: string,
34
+ ): Effect.Effect<Operation.Definition.Any, FunctionNotFoundError, FunctionInvocationService> =>
35
+ Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.resolveFunction)(key);
36
+ }
@@ -2,12 +2,7 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- export * from './ai';
6
- export * from './database';
7
- export * from './queues';
8
- export * from './service-container';
9
5
  export * from './credentials';
10
- export * from './tracing';
11
- export * from './event-logger';
12
- export * from './function-call-service';
13
- export * from './tool-resolver';
6
+ export * from './function-invocation-service';
7
+ export * from './queues';
8
+ export { MESSAGE_PROPERTY_TOOL_CALL_ID } from './tracing';