@dxos/functions 0.8.4-main.f9ba587 → 0.8.4-main.fffef41

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 (155) hide show
  1. package/dist/lib/browser/index.mjs +795 -360
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +795 -360
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/errors.d.ts +129 -0
  8. package/dist/types/src/errors.d.ts.map +1 -0
  9. package/dist/types/src/example/fib.d.ts +7 -0
  10. package/dist/types/src/example/fib.d.ts.map +1 -0
  11. package/dist/types/src/example/forex-effect.d.ts +3 -0
  12. package/dist/types/src/example/forex-effect.d.ts.map +1 -0
  13. package/dist/types/src/example/index.d.ts +12 -0
  14. package/dist/types/src/example/index.d.ts.map +1 -0
  15. package/dist/types/src/example/reply.d.ts +3 -0
  16. package/dist/types/src/example/reply.d.ts.map +1 -0
  17. package/dist/types/src/example/sleep.d.ts +5 -0
  18. package/dist/types/src/example/sleep.d.ts.map +1 -0
  19. package/dist/types/src/index.d.ts +5 -6
  20. package/dist/types/src/index.d.ts.map +1 -1
  21. package/dist/types/src/protocol/index.d.ts +2 -0
  22. package/dist/types/src/protocol/index.d.ts.map +1 -0
  23. package/dist/types/src/protocol/protocol.d.ts +7 -0
  24. package/dist/types/src/protocol/protocol.d.ts.map +1 -0
  25. package/dist/types/src/protocol/protocol.test.d.ts +2 -0
  26. package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
  27. package/dist/types/src/sdk.d.ts +89 -0
  28. package/dist/types/src/sdk.d.ts.map +1 -0
  29. package/dist/types/src/services/credentials.d.ts +21 -3
  30. package/dist/types/src/services/credentials.d.ts.map +1 -1
  31. package/dist/types/src/services/event-logger.d.ts +80 -30
  32. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  33. package/dist/types/src/services/function-invocation-service.d.ts +11 -0
  34. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
  35. package/dist/types/src/services/index.d.ts +6 -7
  36. package/dist/types/src/services/index.d.ts.map +1 -1
  37. package/dist/types/src/services/queues.d.ts +33 -4
  38. package/dist/types/src/services/queues.d.ts.map +1 -1
  39. package/dist/types/src/services/tracing.d.ts +44 -5
  40. package/dist/types/src/services/tracing.d.ts.map +1 -1
  41. package/dist/types/src/types/Function.d.ts +58 -0
  42. package/dist/types/src/types/Function.d.ts.map +1 -0
  43. package/dist/types/src/types/Script.d.ts +28 -0
  44. package/dist/types/src/types/Script.d.ts.map +1 -0
  45. package/dist/types/src/types/Trigger.d.ts +139 -0
  46. package/dist/types/src/types/Trigger.d.ts.map +1 -0
  47. package/dist/types/src/types/TriggerEvent.d.ts +44 -0
  48. package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
  49. package/dist/types/src/types/index.d.ts +6 -0
  50. package/dist/types/src/types/index.d.ts.map +1 -0
  51. package/dist/types/src/types/url.d.ts +12 -0
  52. package/dist/types/src/types/url.d.ts.map +1 -0
  53. package/dist/types/tsconfig.tsbuildinfo +1 -1
  54. package/package.json +19 -66
  55. package/src/errors.ts +21 -0
  56. package/src/example/fib.ts +32 -0
  57. package/src/example/forex-effect.ts +40 -0
  58. package/src/example/index.ts +13 -0
  59. package/src/example/reply.ts +21 -0
  60. package/src/example/sleep.ts +24 -0
  61. package/src/index.ts +5 -8
  62. package/src/{executor → protocol}/index.ts +1 -1
  63. package/src/protocol/protocol.test.ts +59 -0
  64. package/src/protocol/protocol.ts +145 -0
  65. package/src/sdk.ts +226 -0
  66. package/src/services/credentials.ts +92 -4
  67. package/src/services/event-logger.ts +77 -37
  68. package/src/services/function-invocation-service.ts +23 -0
  69. package/src/services/index.ts +7 -7
  70. package/src/services/queues.ts +50 -8
  71. package/src/services/tracing.ts +92 -11
  72. package/src/types/Function.ts +62 -0
  73. package/src/types/Script.ts +33 -0
  74. package/src/types/Trigger.ts +139 -0
  75. package/src/types/TriggerEvent.ts +62 -0
  76. package/src/types/index.ts +9 -0
  77. package/src/types/url.ts +31 -0
  78. package/dist/lib/browser/bundler/index.mjs +0 -247
  79. package/dist/lib/browser/bundler/index.mjs.map +0 -7
  80. package/dist/lib/browser/chunk-54U464M4.mjs +0 -360
  81. package/dist/lib/browser/chunk-54U464M4.mjs.map +0 -7
  82. package/dist/lib/browser/edge/index.mjs +0 -69
  83. package/dist/lib/browser/edge/index.mjs.map +0 -7
  84. package/dist/lib/browser/testing/index.mjs +0 -79
  85. package/dist/lib/browser/testing/index.mjs.map +0 -7
  86. package/dist/lib/node-esm/bundler/index.mjs +0 -249
  87. package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
  88. package/dist/lib/node-esm/chunk-XDSX35BS.mjs +0 -362
  89. package/dist/lib/node-esm/chunk-XDSX35BS.mjs.map +0 -7
  90. package/dist/lib/node-esm/edge/index.mjs +0 -71
  91. package/dist/lib/node-esm/edge/index.mjs.map +0 -7
  92. package/dist/lib/node-esm/testing/index.mjs +0 -80
  93. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  94. package/dist/types/src/bundler/bundler.d.ts +0 -50
  95. package/dist/types/src/bundler/bundler.d.ts.map +0 -1
  96. package/dist/types/src/bundler/bundler.test.d.ts +0 -2
  97. package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
  98. package/dist/types/src/bundler/index.d.ts +0 -2
  99. package/dist/types/src/bundler/index.d.ts.map +0 -1
  100. package/dist/types/src/edge/functions.d.ts +0 -16
  101. package/dist/types/src/edge/functions.d.ts.map +0 -1
  102. package/dist/types/src/edge/index.d.ts +0 -2
  103. package/dist/types/src/edge/index.d.ts.map +0 -1
  104. package/dist/types/src/executor/executor.d.ts +0 -8
  105. package/dist/types/src/executor/executor.d.ts.map +0 -1
  106. package/dist/types/src/executor/index.d.ts +0 -2
  107. package/dist/types/src/executor/index.d.ts.map +0 -1
  108. package/dist/types/src/handler.d.ts +0 -61
  109. package/dist/types/src/handler.d.ts.map +0 -1
  110. package/dist/types/src/schema.d.ts +0 -38
  111. package/dist/types/src/schema.d.ts.map +0 -1
  112. package/dist/types/src/services/ai.d.ts +0 -12
  113. package/dist/types/src/services/ai.d.ts.map +0 -1
  114. package/dist/types/src/services/database.d.ts +0 -15
  115. package/dist/types/src/services/database.d.ts.map +0 -1
  116. package/dist/types/src/services/function-call-service.d.ts +0 -16
  117. package/dist/types/src/services/function-call-service.d.ts.map +0 -1
  118. package/dist/types/src/services/service-container.d.ts +0 -46
  119. package/dist/types/src/services/service-container.d.ts.map +0 -1
  120. package/dist/types/src/services/tool-resolver.d.ts +0 -14
  121. package/dist/types/src/services/tool-resolver.d.ts.map +0 -1
  122. package/dist/types/src/testing/index.d.ts +0 -2
  123. package/dist/types/src/testing/index.d.ts.map +0 -1
  124. package/dist/types/src/testing/logger.d.ts +0 -5
  125. package/dist/types/src/testing/logger.d.ts.map +0 -1
  126. package/dist/types/src/testing/services.d.ts +0 -71
  127. package/dist/types/src/testing/services.d.ts.map +0 -1
  128. package/dist/types/src/trace.d.ts +0 -124
  129. package/dist/types/src/trace.d.ts.map +0 -1
  130. package/dist/types/src/translations.d.ts +0 -12
  131. package/dist/types/src/translations.d.ts.map +0 -1
  132. package/dist/types/src/types.d.ts +0 -411
  133. package/dist/types/src/types.d.ts.map +0 -1
  134. package/dist/types/src/url.d.ts +0 -17
  135. package/dist/types/src/url.d.ts.map +0 -1
  136. package/src/bundler/bundler.test.ts +0 -59
  137. package/src/bundler/bundler.ts +0 -292
  138. package/src/bundler/index.ts +0 -5
  139. package/src/edge/functions.ts +0 -64
  140. package/src/edge/index.ts +0 -9
  141. package/src/executor/executor.ts +0 -54
  142. package/src/handler.ts +0 -113
  143. package/src/schema.ts +0 -57
  144. package/src/services/ai.ts +0 -32
  145. package/src/services/database.ts +0 -50
  146. package/src/services/function-call-service.ts +0 -64
  147. package/src/services/service-container.ts +0 -127
  148. package/src/services/tool-resolver.ts +0 -31
  149. package/src/testing/index.ts +0 -5
  150. package/src/testing/logger.ts +0 -16
  151. package/src/testing/services.ts +0 -174
  152. package/src/trace.ts +0 -180
  153. package/src/translations.ts +0 -20
  154. package/src/types.ts +0 -211
  155. package/src/url.ts +0 -52
package/src/sdk.ts ADDED
@@ -0,0 +1,226 @@
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 DatabaseService } from '@dxos/echo-db';
12
+ import { assertArgument, failedInvariant } from '@dxos/invariant';
13
+
14
+ import {
15
+ type CredentialsService,
16
+ type FunctionInvocationService,
17
+ type QueueService,
18
+ type TracingService,
19
+ } from './services';
20
+ import { Function } from './types';
21
+ import { getUserFunctionIdInMetadata, setUserFunctionIdInMetadata } from './types';
22
+
23
+ // TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
24
+ // https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
25
+ // https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
26
+ // https://www.npmjs.com/package/aws-lambda
27
+
28
+ /**
29
+ * Services that are provided at the function call site by the caller.
30
+ */
31
+ export type InvocationServices = TracingService;
32
+
33
+ /**
34
+ * Services that are available to invoked functions.
35
+ */
36
+ export type FunctionServices =
37
+ | InvocationServices
38
+ | AiService.AiService
39
+ | CredentialsService
40
+ | DatabaseService
41
+ | QueueService
42
+ | FunctionInvocationService;
43
+
44
+ /**
45
+ * Function handler.
46
+ */
47
+ export type FunctionHandler<TData = {}, TOutput = any, S extends FunctionServices = FunctionServices> = (params: {
48
+ /**
49
+ * Context available to the function.
50
+ */
51
+ context: FunctionContext;
52
+
53
+ /**
54
+ * Data passed as the input to the function.
55
+ * Must match the function's input schema.
56
+ * This will be the payload from the trigger or other data passed into the function in a workflow.
57
+ */
58
+ data: TData;
59
+ }) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any, S>;
60
+
61
+ /**
62
+ * Function context.
63
+ */
64
+ export interface FunctionContext {
65
+ // TODO(dmaretskyi): Consider what we should put into context.
66
+ }
67
+
68
+ const typeId = Symbol.for('@dxos/functions/FunctionDefinition');
69
+
70
+ export type FunctionDefinition<T = any, O = any, S extends FunctionServices = FunctionServices> = {
71
+ [typeId]: true;
72
+ key: string;
73
+ name: string;
74
+ description?: string;
75
+ inputSchema: Schema.Schema<T, any>;
76
+ outputSchema?: Schema.Schema<O, any>;
77
+
78
+ /**
79
+ * Keys of the required services.
80
+ */
81
+ services: readonly string[];
82
+
83
+ handler: FunctionHandler<T, O, S>;
84
+ meta?: {
85
+ /**
86
+ * Tools that are projected from functions have this annotation.
87
+ *
88
+ * deployedFunctionId:
89
+ * - Backend deployment ID assigned by the EDGE function service (typically a UUID).
90
+ * - Used for remote invocation via `FunctionInvocationService` → `RemoteFunctionExecutionService`.
91
+ * - Persisted on the corresponding ECHO `Function.Function` object's metadata under the
92
+ * `FUNCTIONS_META_KEY` and retrieved with `getUserFunctionIdInMetadata`.
93
+ */
94
+ deployedFunctionId?: string;
95
+ };
96
+ };
97
+
98
+ export declare namespace FunctionDefinition {
99
+ export type Any = FunctionDefinition<any, any, any>;
100
+ export type Input<T extends Any> = T extends FunctionDefinition<infer I, infer _O, infer _S> ? I : never;
101
+ export type Output<T extends Any> = T extends FunctionDefinition<infer _I, infer O, infer _S> ? O : never;
102
+ export type Services<T extends Any> = T extends FunctionDefinition<infer _I, infer _O, infer S> ? S : never;
103
+ }
104
+
105
+ export type FunctionProps<T, O> = {
106
+ key: string;
107
+ name: string;
108
+ description?: string;
109
+ inputSchema: Schema.Schema<T, any>;
110
+ outputSchema?: Schema.Schema<O, any>;
111
+ // TODO(dmaretskyi): This currently doesn't cause a compile-time error if the handler requests a service that is not specified
112
+ services?: readonly Context.Tag<any, any>[];
113
+
114
+ handler: FunctionHandler<T, O, FunctionServices>;
115
+ };
116
+
117
+ // TODO(dmaretskyi): Output type doesn't get typechecked.
118
+ export const defineFunction: {
119
+ <I, O>(params: FunctionProps<I, O>): FunctionDefinition<I, O, FunctionServices>;
120
+ } = ({ key, name, description, inputSchema, outputSchema = Schema.Any, handler, services }) => {
121
+ if (!Schema.isSchema(inputSchema)) {
122
+ throw new Error('Input schema must be a valid schema');
123
+ }
124
+ if (typeof handler !== 'function') {
125
+ throw new Error('Handler must be a function');
126
+ }
127
+
128
+ // Captures the function definition location.
129
+ const limit = Error.stackTraceLimit;
130
+ Error.stackTraceLimit = 2;
131
+ const traceError = new Error();
132
+ Error.stackTraceLimit = limit;
133
+ let cache: false | string = false;
134
+ const captureStackTrace = () => {
135
+ if (cache !== false) {
136
+ return cache;
137
+ }
138
+ if (traceError.stack !== undefined) {
139
+ const stack = traceError.stack.split('\n');
140
+ if (stack[2] !== undefined) {
141
+ cache = stack[2].trim();
142
+ return cache;
143
+ }
144
+ }
145
+ };
146
+
147
+ const handlerWithSpan = (...args: any[]) => {
148
+ const result = (handler as any)(...args);
149
+ if (Effect.isEffect(result)) {
150
+ return Effect.withSpan(result, `${key ?? name}`, {
151
+ captureStackTrace,
152
+ });
153
+ }
154
+ return result;
155
+ };
156
+
157
+ return {
158
+ [typeId]: true,
159
+ key,
160
+ name,
161
+ description,
162
+ inputSchema,
163
+ outputSchema,
164
+ handler: handlerWithSpan,
165
+ services: !services ? [] : getServiceKeys(services),
166
+ } satisfies FunctionDefinition.Any;
167
+ };
168
+
169
+ const getServiceKeys = (services: readonly Context.Tag<any, any>[]) => {
170
+ return services.map((tag: any) => {
171
+ if (typeof tag.key === 'string') {
172
+ return tag.key;
173
+ }
174
+ console.log(tag);
175
+ failedInvariant();
176
+ });
177
+ };
178
+
179
+ export const FunctionDefinition = {
180
+ make: defineFunction,
181
+ isFunction: (value: unknown): value is FunctionDefinition.Any => {
182
+ return typeof value === 'object' && value !== null && Symbol.for('@dxos/functions/FunctionDefinition') in value;
183
+ },
184
+ serialize: (functionDef: FunctionDefinition.Any): Function.Function => {
185
+ assertArgument(FunctionDefinition.isFunction(functionDef), 'functionDef');
186
+ return serializeFunction(functionDef);
187
+ },
188
+ deserialize: (functionObj: Function.Function): FunctionDefinition.Any => {
189
+ assertArgument(Obj.instanceOf(Function.Function, functionObj), 'functionObj');
190
+ return deserializeFunction(functionObj);
191
+ },
192
+ };
193
+
194
+ export const serializeFunction = (functionDef: FunctionDefinition.Any): Function.Function => {
195
+ const fn = Function.make({
196
+ key: functionDef.key,
197
+ name: functionDef.name,
198
+ version: '0.1.0',
199
+ description: functionDef.description,
200
+ inputSchema: Type.toJsonSchema(functionDef.inputSchema),
201
+ outputSchema: !functionDef.outputSchema ? undefined : Type.toJsonSchema(functionDef.outputSchema),
202
+ services: functionDef.services,
203
+ });
204
+ if (functionDef.meta?.deployedFunctionId) {
205
+ setUserFunctionIdInMetadata(Obj.getMeta(fn), functionDef.meta.deployedFunctionId);
206
+ }
207
+ return fn;
208
+ };
209
+
210
+ export const deserializeFunction = (functionObj: Function.Function): FunctionDefinition<unknown, unknown> => {
211
+ return {
212
+ [typeId]: true,
213
+ // TODO(dmaretskyi): Fix key.
214
+ key: functionObj.key ?? functionObj.name,
215
+ name: functionObj.name,
216
+ description: functionObj.description,
217
+ inputSchema: !functionObj.inputSchema ? Schema.Unknown : Type.toEffectSchema(functionObj.inputSchema),
218
+ outputSchema: !functionObj.outputSchema ? undefined : Type.toEffectSchema(functionObj.outputSchema),
219
+ // TODO(dmaretskyi): This should throw error.
220
+ handler: () => {},
221
+ services: functionObj.services ?? [],
222
+ meta: {
223
+ deployedFunctionId: getUserFunctionIdInMetadata(Obj.getMeta(functionObj)),
224
+ },
225
+ };
226
+ };
@@ -2,9 +2,19 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context } from 'effect';
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';
6
12
 
7
- type CredentialQuery = {
13
+ import { Query } from '@dxos/echo';
14
+ import { DatabaseService } from '@dxos/echo-db';
15
+ import { AccessToken } from '@dxos/types';
16
+
17
+ export type CredentialQuery = {
8
18
  service?: string;
9
19
  };
10
20
 
@@ -17,7 +27,7 @@ export type ServiceCredential = {
17
27
  apiKey?: string;
18
28
  };
19
29
 
20
- export class CredentialsService extends Context.Tag('CredentialsService')<
30
+ export class CredentialsService extends Context.Tag('@dxos/functions/CredentialsService')<
21
31
  CredentialsService,
22
32
  {
23
33
  /**
@@ -31,7 +41,72 @@ export class CredentialsService extends Context.Tag('CredentialsService')<
31
41
  */
32
42
  getCredential: (query: CredentialQuery) => Promise<ServiceCredential>;
33
43
  }
34
- >() {}
44
+ >() {
45
+ static getCredential = (query: CredentialQuery): Effect.Effect<ServiceCredential, never, CredentialsService> =>
46
+ Effect.gen(function* () {
47
+ const credentials = yield* CredentialsService;
48
+ return yield* Effect.promise(() => credentials.getCredential(query));
49
+ });
50
+
51
+ static getApiKey = (query: CredentialQuery): Effect.Effect<Redacted.Redacted<string>, never, CredentialsService> =>
52
+ Effect.gen(function* () {
53
+ const credential = yield* CredentialsService.getCredential(query);
54
+ if (!credential.apiKey) {
55
+ throw new Error(`API key not found for service: ${query.service}`);
56
+ }
57
+ return Redacted.make(credential.apiKey);
58
+ });
59
+
60
+ static configuredLayer = (credentials: ServiceCredential[]) =>
61
+ Layer.succeed(CredentialsService, new ConfiguredCredentialsService(credentials));
62
+
63
+ static layerConfig = (credentials: { service: string; apiKey: Config.Config<Redacted.Redacted<string>> }[]) =>
64
+ Layer.effect(
65
+ CredentialsService,
66
+ Effect.gen(function* () {
67
+ const serviceCredentials = yield* Effect.forEach(credentials, ({ service, apiKey }) =>
68
+ Effect.gen(function* () {
69
+ return {
70
+ service,
71
+ apiKey: Redacted.value(yield* apiKey),
72
+ };
73
+ }),
74
+ );
75
+
76
+ return new ConfiguredCredentialsService(serviceCredentials);
77
+ }),
78
+ );
79
+
80
+ static layerFromDatabase = () =>
81
+ Layer.effect(
82
+ CredentialsService,
83
+ Effect.gen(function* () {
84
+ const dbService = yield* DatabaseService;
85
+ const queryCredentials = async (query: CredentialQuery): Promise<ServiceCredential[]> => {
86
+ const { objects: accessTokens } = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
87
+ return accessTokens
88
+ .filter((accessToken) => accessToken.source === query.service)
89
+ .map((accessToken) => ({
90
+ service: accessToken.source,
91
+ apiKey: accessToken.token,
92
+ }));
93
+ };
94
+ return {
95
+ getCredential: async (query) => {
96
+ const credentials = await queryCredentials(query);
97
+ if (credentials.length === 0) {
98
+ throw new Error(`Credential not found for service: ${query.service}`);
99
+ }
100
+
101
+ return credentials[0];
102
+ },
103
+ queryCredentials: async (query) => {
104
+ return queryCredentials(query);
105
+ },
106
+ };
107
+ }),
108
+ );
109
+ }
35
110
 
36
111
  export class ConfiguredCredentialsService implements Context.Tag.Service<CredentialsService> {
37
112
  constructor(private readonly credentials: ServiceCredential[] = []) {}
@@ -50,6 +125,19 @@ export class ConfiguredCredentialsService implements Context.Tag.Service<Credent
50
125
  if (!credential) {
51
126
  throw new Error(`Credential not found for service: ${query.service}`);
52
127
  }
128
+
53
129
  return credential;
54
130
  }
55
131
  }
132
+
133
+ /**
134
+ * Maps the request to include the API key from the credential.
135
+ */
136
+ export const withAuthorization = (query: CredentialQuery, kind?: 'Bearer' | 'Basic') =>
137
+ HttpClient.mapRequestEffect(
138
+ Effect.fnUntraced(function* (request) {
139
+ const key = yield* CredentialsService.getApiKey(query).pipe(Effect.map(Redacted.value));
140
+ const authorization = kind ? `${kind} ${key}` : key;
141
+ return HttpClientRequest.setHeader(request, 'Authorization', authorization);
142
+ }),
143
+ );
@@ -2,53 +2,90 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Effect, Context } from 'effect';
5
+ import * as Context from 'effect/Context';
6
+ import * as Effect from 'effect/Effect';
7
+ import * as Layer from 'effect/Layer';
8
+ import * as Schema from 'effect/Schema';
6
9
 
10
+ import { Obj, Type } from '@dxos/echo';
7
11
  import { invariant } from '@dxos/invariant';
8
- import { log, LogLevel } from '@dxos/log';
12
+ import { LogLevel, log } from '@dxos/log';
9
13
 
10
- export type ComputeEvent =
11
- | {
12
- type: 'begin-compute';
13
- nodeId: string;
14
- inputs: Record<string, any>;
15
- }
16
- | {
17
- type: 'end-compute';
18
- nodeId: string;
19
- outputs: Record<string, any>;
20
- }
21
- | {
22
- type: 'compute-input';
23
- nodeId: string;
24
- property: string;
25
- value: any;
26
- }
27
- | {
28
- type: 'compute-output';
29
- nodeId: string;
30
- property: string;
31
- value: any;
32
- }
33
- | {
34
- type: 'custom';
35
- nodeId: string;
36
- event: any;
37
- };
14
+ import { TracingService } from './tracing';
15
+
16
+ export const ComputeEventPayload = Schema.Union(
17
+ Schema.Struct({
18
+ type: Schema.Literal('begin-compute'),
19
+ nodeId: Schema.String,
20
+ /**
21
+ * Names of the inputs begin computed.
22
+ */
23
+ inputs: Schema.Array(Schema.String),
24
+ }),
25
+ Schema.Struct({
26
+ type: Schema.Literal('end-compute'),
27
+ nodeId: Schema.String,
28
+ /**
29
+ * Names of the outputs computed.
30
+ */
31
+ outputs: Schema.Array(Schema.String),
32
+ }),
33
+ Schema.Struct({
34
+ type: Schema.Literal('compute-input'),
35
+ nodeId: Schema.String,
36
+ property: Schema.String,
37
+ value: Schema.Any,
38
+ }),
39
+ Schema.Struct({
40
+ type: Schema.Literal('compute-output'),
41
+ nodeId: Schema.String,
42
+ property: Schema.String,
43
+ value: Schema.Any,
44
+ }),
45
+ Schema.Struct({
46
+ type: Schema.Literal('custom'),
47
+ nodeId: Schema.String,
48
+ event: Schema.Any,
49
+ }),
50
+ );
51
+ export type ComputeEventPayload = Schema.Schema.Type<typeof ComputeEventPayload>;
38
52
 
39
- export class EventLogger extends Context.Tag('EventLogger')<
40
- EventLogger,
41
- { readonly log: (event: ComputeEvent) => void; readonly nodeId: string | undefined }
53
+ export const ComputeEvent = Schema.Struct({
54
+ payload: ComputeEventPayload,
55
+ }).pipe(Type.Obj({ typename: 'dxos.org/type/ComputeEvent', version: '0.1.0' }));
56
+
57
+ /**
58
+ * Logs event for the compute workflows.
59
+ */
60
+ export class ComputeEventLogger extends Context.Tag('@dxos/functions/ComputeEventLogger')<
61
+ ComputeEventLogger,
62
+ { readonly log: (event: ComputeEventPayload) => void; readonly nodeId: string | undefined }
42
63
  >() {
43
- static noop: Context.Tag.Service<EventLogger> = {
64
+ static noop: Context.Tag.Service<ComputeEventLogger> = {
44
65
  log: () => {},
45
66
  nodeId: undefined,
46
67
  };
68
+
69
+ /**
70
+ * Implements ComputeEventLogger using TracingService.
71
+ */
72
+ static layerFromTracing = Layer.effect(
73
+ ComputeEventLogger,
74
+ Effect.gen(function* () {
75
+ const tracing = yield* TracingService;
76
+ return {
77
+ log: (event: ComputeEventPayload) => {
78
+ tracing.write(Obj.make(ComputeEvent, { payload: event }));
79
+ },
80
+ nodeId: undefined,
81
+ };
82
+ }),
83
+ );
47
84
  }
48
85
 
49
86
  export const logCustomEvent = (data: any) =>
50
87
  Effect.gen(function* () {
51
- const logger = yield* EventLogger;
88
+ const logger = yield* ComputeEventLogger;
52
89
  if (!logger.nodeId) {
53
90
  throw new Error('logCustomEvent must be called within a node compute function');
54
91
  }
@@ -67,7 +104,10 @@ export const createDefectLogger = <A, E, R>(): ((self: Effect.Effect<A, E, R>) =
67
104
  }),
68
105
  );
69
106
 
70
- export const createEventLogger = (level: LogLevel, message: string = 'event'): Context.Tag.Service<EventLogger> => {
107
+ export const createEventLogger = (
108
+ level: LogLevel,
109
+ message: string = 'event',
110
+ ): Context.Tag.Service<ComputeEventLogger> => {
71
111
  const logFunction = (
72
112
  {
73
113
  [LogLevel.WARN]: log.warn,
@@ -79,7 +119,7 @@ export const createEventLogger = (level: LogLevel, message: string = 'event'): C
79
119
  )[level];
80
120
  invariant(logFunction);
81
121
  return {
82
- log: (event: ComputeEvent) => {
122
+ log: (event: ComputeEventPayload) => {
83
123
  logFunction(message, event);
84
124
  },
85
125
  nodeId: undefined,
@@ -0,0 +1,23 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+ import * as Context from 'effect/Context';
5
+ import * as Effect from 'effect/Effect';
6
+
7
+ import { type FunctionDefinition, type InvocationServices } from '../sdk';
8
+
9
+ export class FunctionInvocationService extends Context.Tag('@dxos/functions/FunctionInvocationService')<
10
+ FunctionInvocationService,
11
+ {
12
+ invokeFunction<I, O>(
13
+ functionDef: FunctionDefinition<I, O, any>,
14
+ input: I,
15
+ ): Effect.Effect<O, never, InvocationServices>;
16
+ }
17
+ >() {
18
+ static invokeFunction = <I, O>(
19
+ functionDef: FunctionDefinition<I, O, any>,
20
+ input: I,
21
+ ): Effect.Effect<O, never, FunctionInvocationService | InvocationServices> =>
22
+ Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.invokeFunction)(functionDef, input);
23
+ }
@@ -2,12 +2,12 @@
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';
5
+ export { DatabaseService } from '@dxos/echo-db';
6
+
9
7
  export * from './credentials';
10
- export * from './tracing';
8
+ export { ConfiguredCredentialsService, type ServiceCredential } from './credentials';
11
9
  export * from './event-logger';
12
- export * from './function-call-service';
13
- export * from './tool-resolver';
10
+ export { createEventLogger, createDefectLogger } from './event-logger';
11
+ export * from './function-invocation-service';
12
+ export * from './queues';
13
+ export * from './tracing';
@@ -2,11 +2,18 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context, Layer } from 'effect';
5
+ import * as Context from 'effect/Context';
6
+ import * as Effect from 'effect/Effect';
7
+ import * as Layer from 'effect/Layer';
6
8
 
9
+ import type { Obj, Relation } from '@dxos/echo';
7
10
  import type { Queue, QueueAPI, QueueFactory } from '@dxos/echo-db';
11
+ import type { DXN, QueueSubspaceTag } from '@dxos/keys';
8
12
 
9
- export class QueueService extends Context.Tag('QueueService')<
13
+ /**
14
+ * Gives access to all queues.
15
+ */
16
+ export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
10
17
  QueueService,
11
18
  {
12
19
  /**
@@ -16,27 +23,62 @@ export class QueueService extends Context.Tag('QueueService')<
16
23
 
17
24
  /**
18
25
  * The queue that is used to store the context of the current research.
26
+ * @deprecated Use `ContextQueueService` instead.
19
27
  */
20
- // TODO(dmaretskyi): Is this really part of the queue service?
21
- readonly contextQueue: Queue | undefined;
28
+ readonly queue: Queue | undefined;
22
29
  }
23
30
  >() {
24
31
  static notAvailable = Layer.succeed(QueueService, {
25
32
  queues: {
26
- get(dxn) {
33
+ get(_dxn) {
27
34
  throw new Error('Queues not available');
28
35
  },
29
36
  create() {
30
37
  throw new Error('Queues not available');
31
38
  },
32
39
  },
33
- contextQueue: undefined,
40
+ queue: undefined,
34
41
  });
35
42
 
36
- static make = (queues: QueueFactory, contextQueue: Queue | undefined): Context.Tag.Service<QueueService> => {
43
+ static make = (queues: QueueFactory, queue?: Queue): Context.Tag.Service<QueueService> => {
37
44
  return {
38
45
  queues,
39
- contextQueue,
46
+ queue,
40
47
  };
41
48
  };
49
+
50
+ static layer = (queues: QueueFactory, queue?: Queue): Layer.Layer<QueueService> =>
51
+ Layer.succeed(QueueService, QueueService.make(queues, queue));
52
+
53
+ /**
54
+ * Gets a queue by its DXN.
55
+ */
56
+ static getQueue = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(
57
+ dxn: DXN,
58
+ ): Effect.Effect<Queue<T>, never, QueueService> => QueueService.pipe(Effect.map(({ queues }) => queues.get<T>(dxn)));
59
+
60
+ /**
61
+ * Creates a new queue.
62
+ */
63
+ static createQueue = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(options?: {
64
+ subspaceTag?: QueueSubspaceTag;
65
+ }): Effect.Effect<Queue<T>, never, QueueService> =>
66
+ QueueService.pipe(Effect.map(({ queues }) => queues.create<T>(options)));
67
+
68
+ static append = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(
69
+ queue: Queue<T>,
70
+ objects: T[],
71
+ ): Effect.Effect<void> => Effect.promise(() => queue.append(objects));
72
+ }
73
+
74
+ /**
75
+ * Gives access to a specific queue passed as a context.
76
+ */
77
+ export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQueueService')<
78
+ ContextQueueService,
79
+ {
80
+ readonly queue: Queue;
81
+ }
82
+ >() {
83
+ static layer = (queue: Queue) => Layer.succeed(ContextQueueService, { queue });
42
84
  }