@dxos/functions 0.8.4-main.f5c0578 → 0.8.4-main.fcfe5033a5

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 (194) hide show
  1. package/README.md +4 -6
  2. package/dist/lib/neutral/Trace.mjs +42 -0
  3. package/dist/lib/neutral/Trace.mjs.map +7 -0
  4. package/dist/lib/neutral/chunk-27Y24OTY.mjs +167 -0
  5. package/dist/lib/neutral/chunk-27Y24OTY.mjs.map +7 -0
  6. package/dist/lib/neutral/chunk-IVDUS56O.mjs +49 -0
  7. package/dist/lib/neutral/chunk-IVDUS56O.mjs.map +7 -0
  8. package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
  9. package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
  10. package/dist/lib/neutral/fib-N45KAC7C.mjs +23 -0
  11. package/dist/lib/neutral/fib-N45KAC7C.mjs.map +7 -0
  12. package/dist/lib/neutral/index.mjs +1218 -0
  13. package/dist/lib/neutral/index.mjs.map +7 -0
  14. package/dist/lib/neutral/meta.json +1 -0
  15. package/dist/lib/neutral/reply-EUEPKNJF.mjs +19 -0
  16. package/dist/lib/neutral/reply-EUEPKNJF.mjs.map +7 -0
  17. package/dist/lib/neutral/sleep-PUK3D4FF.mjs +15 -0
  18. package/dist/lib/neutral/sleep-PUK3D4FF.mjs.map +7 -0
  19. package/dist/types/src/Trace.d.ts +174 -0
  20. package/dist/types/src/Trace.d.ts.map +1 -0
  21. package/dist/types/src/errors.d.ts +89 -38
  22. package/dist/types/src/errors.d.ts.map +1 -1
  23. package/dist/types/src/example/definitions.d.ts +11 -0
  24. package/dist/types/src/example/definitions.d.ts.map +1 -0
  25. package/dist/types/src/example/fib.d.ts +8 -0
  26. package/dist/types/src/example/fib.d.ts.map +1 -0
  27. package/dist/types/src/example/forex-effect.d.ts +3 -0
  28. package/dist/types/src/example/forex-effect.d.ts.map +1 -0
  29. package/dist/types/src/example/index.d.ts +4 -0
  30. package/dist/types/src/example/index.d.ts.map +1 -0
  31. package/dist/types/src/example/reply.d.ts +4 -0
  32. package/dist/types/src/example/reply.d.ts.map +1 -0
  33. package/dist/types/src/example/sleep.d.ts +6 -0
  34. package/dist/types/src/example/sleep.d.ts.map +1 -0
  35. package/dist/types/src/index.d.ts +9 -7
  36. package/dist/types/src/index.d.ts.map +1 -1
  37. package/dist/types/src/process/Process.d.ts +246 -0
  38. package/dist/types/src/process/Process.d.ts.map +1 -0
  39. package/dist/types/src/process/ServiceResolver.d.ts +74 -0
  40. package/dist/types/src/process/ServiceResolver.d.ts.map +1 -0
  41. package/dist/types/src/process/StorageService.d.ts +58 -0
  42. package/dist/types/src/process/StorageService.d.ts.map +1 -0
  43. package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
  44. package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
  45. package/dist/types/src/protocol/index.d.ts +2 -0
  46. package/dist/types/src/protocol/index.d.ts.map +1 -0
  47. package/dist/types/src/protocol/protocol.d.ts +7 -0
  48. package/dist/types/src/protocol/protocol.d.ts.map +1 -0
  49. package/dist/types/src/protocol/protocol.test.d.ts +2 -0
  50. package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
  51. package/dist/types/src/sdk.d.ts +11 -0
  52. package/dist/types/src/sdk.d.ts.map +1 -0
  53. package/dist/types/src/services/credentials.d.ts +20 -4
  54. package/dist/types/src/services/credentials.d.ts.map +1 -1
  55. package/dist/types/src/services/event-logger.d.ts +38 -33
  56. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  57. package/dist/types/src/services/function-invocation-service.d.ts +16 -0
  58. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
  59. package/dist/types/src/services/index.d.ts +5 -6
  60. package/dist/types/src/services/index.d.ts.map +1 -1
  61. package/dist/types/src/services/queues.d.ts +11 -5
  62. package/dist/types/src/services/queues.d.ts.map +1 -1
  63. package/dist/types/src/services/tracing.d.ts +1 -51
  64. package/dist/types/src/services/tracing.d.ts.map +1 -1
  65. package/dist/types/src/types/Script.d.ts +22 -0
  66. package/dist/types/src/types/Script.d.ts.map +1 -0
  67. package/dist/types/src/types/Trigger.d.ts +150 -0
  68. package/dist/types/src/types/Trigger.d.ts.map +1 -0
  69. package/dist/types/src/types/TriggerEvent.d.ts +75 -0
  70. package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
  71. package/dist/types/src/types/index.d.ts +5 -0
  72. package/dist/types/src/types/index.d.ts.map +1 -0
  73. package/dist/types/src/types/url.d.ts +13 -0
  74. package/dist/types/src/types/url.d.ts.map +1 -0
  75. package/dist/types/tsconfig.tsbuildinfo +1 -1
  76. package/package.json +31 -73
  77. package/src/Trace.ts +225 -0
  78. package/src/errors.ts +13 -5
  79. package/src/example/definitions.ts +49 -0
  80. package/src/example/fib.ts +23 -0
  81. package/src/example/forex-effect.ts +40 -0
  82. package/src/example/index.ts +12 -0
  83. package/src/example/reply.ts +19 -0
  84. package/src/example/sleep.ts +17 -0
  85. package/src/index.ts +9 -9
  86. package/src/process/Process.ts +487 -0
  87. package/src/process/ServiceResolver.ts +174 -0
  88. package/src/process/StorageService.ts +99 -0
  89. package/src/protocol/functions-ai-http-client.ts +67 -0
  90. package/src/{executor → protocol}/index.ts +1 -1
  91. package/src/protocol/protocol.test.ts +58 -0
  92. package/src/protocol/protocol.ts +273 -0
  93. package/src/sdk.ts +30 -0
  94. package/src/services/credentials.ts +70 -9
  95. package/src/services/event-logger.ts +15 -22
  96. package/src/services/function-invocation-service.ts +35 -0
  97. package/src/services/index.ts +5 -6
  98. package/src/services/queues.ts +19 -5
  99. package/src/services/tracing.ts +1 -116
  100. package/src/types/Script.ts +38 -0
  101. package/src/types/Trigger.ts +207 -0
  102. package/src/types/TriggerEvent.ts +62 -0
  103. package/src/types/index.ts +8 -0
  104. package/src/types/url.ts +32 -0
  105. package/dist/lib/browser/bundler/index.mjs +0 -248
  106. package/dist/lib/browser/bundler/index.mjs.map +0 -7
  107. package/dist/lib/browser/chunk-IGZ7KK4A.mjs +0 -526
  108. package/dist/lib/browser/chunk-IGZ7KK4A.mjs.map +0 -7
  109. package/dist/lib/browser/edge/index.mjs +0 -69
  110. package/dist/lib/browser/edge/index.mjs.map +0 -7
  111. package/dist/lib/browser/index.mjs +0 -502
  112. package/dist/lib/browser/index.mjs.map +0 -7
  113. package/dist/lib/browser/meta.json +0 -1
  114. package/dist/lib/browser/testing/index.mjs +0 -59
  115. package/dist/lib/browser/testing/index.mjs.map +0 -7
  116. package/dist/lib/node-esm/bundler/index.mjs +0 -250
  117. package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
  118. package/dist/lib/node-esm/chunk-NE5CWLJE.mjs +0 -528
  119. package/dist/lib/node-esm/chunk-NE5CWLJE.mjs.map +0 -7
  120. package/dist/lib/node-esm/edge/index.mjs +0 -71
  121. package/dist/lib/node-esm/edge/index.mjs.map +0 -7
  122. package/dist/lib/node-esm/index.mjs +0 -503
  123. package/dist/lib/node-esm/index.mjs.map +0 -7
  124. package/dist/lib/node-esm/meta.json +0 -1
  125. package/dist/lib/node-esm/testing/index.mjs +0 -60
  126. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  127. package/dist/types/src/bundler/bundler.d.ts +0 -50
  128. package/dist/types/src/bundler/bundler.d.ts.map +0 -1
  129. package/dist/types/src/bundler/bundler.test.d.ts +0 -2
  130. package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
  131. package/dist/types/src/bundler/index.d.ts +0 -2
  132. package/dist/types/src/bundler/index.d.ts.map +0 -1
  133. package/dist/types/src/edge/functions.d.ts +0 -16
  134. package/dist/types/src/edge/functions.d.ts.map +0 -1
  135. package/dist/types/src/edge/index.d.ts +0 -2
  136. package/dist/types/src/edge/index.d.ts.map +0 -1
  137. package/dist/types/src/executor/executor.d.ts +0 -11
  138. package/dist/types/src/executor/executor.d.ts.map +0 -1
  139. package/dist/types/src/executor/index.d.ts +0 -2
  140. package/dist/types/src/executor/index.d.ts.map +0 -1
  141. package/dist/types/src/handler.d.ts +0 -65
  142. package/dist/types/src/handler.d.ts.map +0 -1
  143. package/dist/types/src/schema.d.ts +0 -38
  144. package/dist/types/src/schema.d.ts.map +0 -1
  145. package/dist/types/src/services/database.d.ts +0 -79
  146. package/dist/types/src/services/database.d.ts.map +0 -1
  147. package/dist/types/src/services/local-function-execution.d.ts +0 -12
  148. package/dist/types/src/services/local-function-execution.d.ts.map +0 -1
  149. package/dist/types/src/services/remote-function-execution-service.d.ts +0 -15
  150. package/dist/types/src/services/remote-function-execution-service.d.ts.map +0 -1
  151. package/dist/types/src/services/service-container.d.ts +0 -56
  152. package/dist/types/src/services/service-container.d.ts.map +0 -1
  153. package/dist/types/src/services/service-registry.d.ts +0 -29
  154. package/dist/types/src/services/service-registry.d.ts.map +0 -1
  155. package/dist/types/src/services/service-registry.test.d.ts +0 -2
  156. package/dist/types/src/services/service-registry.test.d.ts.map +0 -1
  157. package/dist/types/src/testing/index.d.ts +0 -3
  158. package/dist/types/src/testing/index.d.ts.map +0 -1
  159. package/dist/types/src/testing/layer.d.ts +0 -10
  160. package/dist/types/src/testing/layer.d.ts.map +0 -1
  161. package/dist/types/src/testing/logger.d.ts +0 -5
  162. package/dist/types/src/testing/logger.d.ts.map +0 -1
  163. package/dist/types/src/testing/services.d.ts +0 -59
  164. package/dist/types/src/testing/services.d.ts.map +0 -1
  165. package/dist/types/src/trace.d.ts +0 -122
  166. package/dist/types/src/trace.d.ts.map +0 -1
  167. package/dist/types/src/translations.d.ts +0 -12
  168. package/dist/types/src/translations.d.ts.map +0 -1
  169. package/dist/types/src/types.d.ts +0 -411
  170. package/dist/types/src/types.d.ts.map +0 -1
  171. package/dist/types/src/url.d.ts +0 -17
  172. package/dist/types/src/url.d.ts.map +0 -1
  173. package/src/bundler/bundler.test.ts +0 -59
  174. package/src/bundler/bundler.ts +0 -292
  175. package/src/bundler/index.ts +0 -5
  176. package/src/edge/functions.ts +0 -64
  177. package/src/edge/index.ts +0 -9
  178. package/src/executor/executor.ts +0 -54
  179. package/src/handler.ts +0 -122
  180. package/src/schema.ts +0 -57
  181. package/src/services/database.ts +0 -116
  182. package/src/services/local-function-execution.ts +0 -74
  183. package/src/services/remote-function-execution-service.ts +0 -66
  184. package/src/services/service-container.ts +0 -114
  185. package/src/services/service-registry.test.ts +0 -42
  186. package/src/services/service-registry.ts +0 -59
  187. package/src/testing/index.ts +0 -6
  188. package/src/testing/layer.ts +0 -31
  189. package/src/testing/logger.ts +0 -16
  190. package/src/testing/services.ts +0 -115
  191. package/src/trace.ts +0 -178
  192. package/src/translations.ts +0 -20
  193. package/src/types.ts +0 -211
  194. package/src/url.ts +0 -52
@@ -2,24 +2,31 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context, Effect, Layer, Schema } 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
 
7
- import { Obj, Type } from '@dxos/echo';
10
+ import { Type } from '@dxos/echo';
8
11
  import { invariant } from '@dxos/invariant';
9
12
  import { LogLevel, log } from '@dxos/log';
10
13
 
11
- import { TracingService } from './tracing';
12
-
13
14
  export const ComputeEventPayload = Schema.Union(
14
15
  Schema.Struct({
15
16
  type: Schema.Literal('begin-compute'),
16
17
  nodeId: Schema.String,
17
- inputs: Schema.Record({ key: Schema.String, value: Schema.Any }),
18
+ /**
19
+ * Names of the inputs begin computed.
20
+ */
21
+ inputs: Schema.Array(Schema.String),
18
22
  }),
19
23
  Schema.Struct({
20
24
  type: Schema.Literal('end-compute'),
21
25
  nodeId: Schema.String,
22
- outputs: Schema.Record({ key: Schema.String, value: Schema.Any }),
26
+ /**
27
+ * Names of the outputs computed.
28
+ */
29
+ outputs: Schema.Array(Schema.String),
23
30
  }),
24
31
  Schema.Struct({
25
32
  type: Schema.Literal('compute-input'),
@@ -43,7 +50,7 @@ export type ComputeEventPayload = Schema.Schema.Type<typeof ComputeEventPayload>
43
50
 
44
51
  export const ComputeEvent = Schema.Struct({
45
52
  payload: ComputeEventPayload,
46
- }).pipe(Type.Obj({ typename: 'dxos.org/type/ComputeEvent', version: '0.1.0' }));
53
+ }).pipe(Type.object({ typename: 'org.dxos.type.computeEvent', version: '0.1.0' }));
47
54
 
48
55
  /**
49
56
  * Logs event for the compute workflows.
@@ -57,21 +64,7 @@ export class ComputeEventLogger extends Context.Tag('@dxos/functions/ComputeEven
57
64
  nodeId: undefined,
58
65
  };
59
66
 
60
- /**
61
- * Implements ComputeEventLogger using TracingService.
62
- */
63
- static layerFromTracing = Layer.effect(
64
- ComputeEventLogger,
65
- Effect.gen(function* () {
66
- const tracing = yield* TracingService;
67
- return {
68
- log: (event: ComputeEventPayload) => {
69
- tracing.write(Obj.make(ComputeEvent, { payload: event }));
70
- },
71
- nodeId: undefined,
72
- };
73
- }),
74
- );
67
+ static layerNoop: Layer.Layer<ComputeEventLogger> = Layer.succeed(ComputeEventLogger, ComputeEventLogger.noop);
75
68
  }
76
69
 
77
70
  export const logCustomEvent = (data: any) =>
@@ -0,0 +1,35 @@
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 { Operation } from '@dxos/operation';
9
+
10
+ import type { FunctionNotFoundError } from '../errors';
11
+
12
+ export class FunctionInvocationService extends Context.Tag('@dxos/functions/FunctionInvocationService')<
13
+ FunctionInvocationService,
14
+ {
15
+ invokeFunction<I, O>(functionDef: Operation.Definition<I, O, any>, input: I): Effect.Effect<O>;
16
+
17
+ resolveFunction(key: string): Effect.Effect<Operation.Definition.Any, FunctionNotFoundError>;
18
+ }
19
+ >() {
20
+ static layerNotAvailable = Layer.succeed(FunctionInvocationService, {
21
+ invokeFunction: () => Effect.die('FunctionInvocationService is not avaialble.'),
22
+ resolveFunction: () => Effect.die('FunctionInvocationService is not available.'),
23
+ });
24
+
25
+ static invokeFunction = <I, O>(
26
+ functionDef: Operation.Definition<I, O, any>,
27
+ input: I,
28
+ ): Effect.Effect<O, never, FunctionInvocationService> =>
29
+ Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.invokeFunction)(functionDef, input);
30
+
31
+ static resolveFunction = (
32
+ key: string,
33
+ ): Effect.Effect<Operation.Definition.Any, FunctionNotFoundError, FunctionInvocationService> =>
34
+ Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.resolveFunction)(key);
35
+ }
@@ -2,11 +2,10 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- export * from './database';
6
- export * from './queues';
7
- export * from './service-container';
8
5
  export * from './credentials';
9
- export * from './tracing';
6
+ export { ConfiguredCredentialsService, type ServiceCredential } from './credentials';
10
7
  export * from './event-logger';
11
- export * from './remote-function-execution-service';
12
- export * from './local-function-execution';
8
+ export { createEventLogger, createDefectLogger } from './event-logger';
9
+ export * from './function-invocation-service';
10
+ export * from './queues';
11
+ export { MESSAGE_PROPERTY_TOOL_CALL_ID } from './tracing';
@@ -2,14 +2,17 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context, Effect, 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
 
7
- import type { Obj, Relation } from '@dxos/echo';
8
- import type { Queue, QueueAPI, QueueFactory } from '@dxos/echo-db';
9
+ import { type Entity } from '@dxos/echo';
10
+ import { createFeedServiceLayer, type Queue, type QueueAPI, type QueueFactory } from '@dxos/echo-db';
9
11
  import type { DXN, QueueSubspaceTag } from '@dxos/keys';
10
12
 
11
13
  /**
12
14
  * Gives access to all queues.
15
+ * @deprecated Use Feed.FeedService instead.
13
16
  */
14
17
  export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
15
18
  QueueService,
@@ -51,21 +54,25 @@ export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
51
54
  /**
52
55
  * Gets a queue by its DXN.
53
56
  */
54
- static getQueue = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(
57
+ static getQueue = <T extends Entity.Unknown = Entity.Unknown>(
55
58
  dxn: DXN,
56
59
  ): Effect.Effect<Queue<T>, never, QueueService> => QueueService.pipe(Effect.map(({ queues }) => queues.get<T>(dxn)));
57
60
 
58
61
  /**
59
62
  * Creates a new queue.
60
63
  */
61
- static createQueue = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(options?: {
64
+ static createQueue = <T extends Entity.Unknown = Entity.Unknown>(options?: {
62
65
  subspaceTag?: QueueSubspaceTag;
63
66
  }): Effect.Effect<Queue<T>, never, QueueService> =>
64
67
  QueueService.pipe(Effect.map(({ queues }) => queues.create<T>(options)));
68
+
69
+ static append = <T extends Entity.Unknown = Entity.Unknown>(queue: Queue<T>, objects: T[]): Effect.Effect<void> =>
70
+ Effect.promise(() => queue.append(objects));
65
71
  }
66
72
 
67
73
  /**
68
74
  * Gives access to a specific queue passed as a context.
75
+ * @deprecated Use Feed.FeedService instead.
69
76
  */
70
77
  export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQueueService')<
71
78
  ContextQueueService,
@@ -75,3 +82,10 @@ export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQue
75
82
  >() {
76
83
  static layer = (queue: Queue) => Layer.succeed(ContextQueueService, { queue });
77
84
  }
85
+
86
+ export const feedServiceFromQueueServiceLayer = Layer.unwrapEffect(
87
+ Effect.gen(function* () {
88
+ const { queues } = yield* QueueService;
89
+ return createFeedServiceLayer(queues);
90
+ }),
91
+ );
@@ -2,122 +2,7 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context, Effect, Layer } from 'effect';
6
-
7
- import { AgentStatus } from '@dxos/ai';
8
- import { Obj } from '@dxos/echo';
9
- import type { Queue } from '@dxos/echo-db';
10
- import type { ObjectId } from '@dxos/echo-schema';
11
- import { DataType } from '@dxos/schema';
12
-
13
- /**
14
- * Provides a way for compute primitives (functions, workflows, tools)
15
- * to emit an execution trace as a series of structured ECHO objects.
16
- */
17
- export class TracingService extends Context.Tag('@dxos/functions/TracingService')<
18
- TracingService,
19
- {
20
- /**
21
- * Gets the parent message ID.
22
- */
23
- getTraceContext: () => TracingService.TraceContext;
24
-
25
- /**
26
- * Write an event to the tracing queue.
27
- * @param event - The event to write. Must be an a typed object.
28
- */
29
- write: (event: Obj.Any) => void;
30
- }
31
- >() {
32
- static noop: Context.Tag.Service<TracingService> = { write: () => {}, getTraceContext: () => ({}) };
33
-
34
- static layerNoop = Layer.succeed(TracingService, TracingService.noop);
35
-
36
- static console: Context.Tag.Service<TracingService> = {
37
- write: (event) => {
38
- // eslint-disable-next-line no-console
39
- console.log(event);
40
- },
41
- getTraceContext: () => ({}),
42
- };
43
-
44
- static layerConsole = Layer.succeed(TracingService, TracingService.console);
45
-
46
- /**
47
- * Creates a TracingService layer that emits events to the parent tracing service.
48
- */
49
- static layerSubframe = (mapContext: (currentContext: TracingService.TraceContext) => TracingService.TraceContext) =>
50
- Layer.effect(
51
- TracingService,
52
- Effect.gen(function* () {
53
- const tracing = yield* TracingService;
54
- const context = mapContext(tracing.getTraceContext());
55
- return {
56
- write: (event) => tracing.write(event),
57
- getTraceContext: () => context,
58
- };
59
- }),
60
- );
61
-
62
- static layerQueue = (queue: Queue) =>
63
- Layer.effect(
64
- TracingService,
65
- Effect.gen(function* () {
66
- // TODO(dmaretskyi): Batching.
67
- return {
68
- write: (event) => queue.append([event]),
69
- getTraceContext: () => ({}),
70
- };
71
- }),
72
- );
73
-
74
- /**
75
- * Emit the current human-readable execution status.
76
- */
77
- static emitStatus: (data: Obj.MakeProps<typeof AgentStatus>) => Effect.Effect<void, never, TracingService> =
78
- Effect.fnUntraced(function* (data) {
79
- const tracing = yield* TracingService;
80
- tracing.write(
81
- Obj.make(AgentStatus, {
82
- parentMessage: tracing.getTraceContext().parentMessage,
83
- toolCallId: tracing.getTraceContext().toolCallId,
84
- ...data,
85
- }),
86
- );
87
- });
88
-
89
- static emitConverationMessage: (
90
- data: Obj.MakeProps<typeof DataType.Message>,
91
- ) => Effect.Effect<void, never, TracingService> = Effect.fnUntraced(function* (data) {
92
- const tracing = yield* TracingService;
93
- tracing.write(
94
- Obj.make(DataType.Message, {
95
- parentMessage: tracing.getTraceContext().parentMessage,
96
- ...data,
97
- properties: {
98
- [MESSAGE_PROPERTY_TOOL_CALL_ID]: tracing.getTraceContext().toolCallId,
99
- ...data.properties,
100
- },
101
- }),
102
- );
103
- });
104
- }
105
-
106
- export namespace TracingService {
107
- export interface TraceContext {
108
- /**
109
- * If this thread sprung from a tool call, this is the ID of the message containing the tool call.
110
- */
111
- parentMessage?: ObjectId;
112
-
113
- /**
114
- * If the current thread is a byproduct of a tool call, this is the ID of the tool call.
115
- */
116
- toolCallId?: string;
117
- }
118
- }
119
-
120
5
  /**
121
- * Goes into {@link DataType.Message['properties']}
6
+ * Goes into {@link Message['properties']}
122
7
  */
123
8
  export const MESSAGE_PROPERTY_TOOL_CALL_ID = 'toolCallId' as const;
@@ -0,0 +1,38 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Schema from 'effect/Schema';
6
+
7
+ import { Annotation, Obj, Ref, Type } from '@dxos/echo';
8
+ import { FormInputAnnotation } from '@dxos/echo/internal';
9
+ import { Text } from '@dxos/schema';
10
+
11
+ /**
12
+ * Source script.
13
+ */
14
+ export const Script = Schema.Struct({
15
+ name: Schema.String.pipe(Schema.optional),
16
+ description: Schema.String.pipe(Schema.optional),
17
+ // TODO(burdon): Change to hash of deployed content.
18
+ // Whether source has changed since last deploy.
19
+ changed: Schema.Boolean.pipe(FormInputAnnotation.set(false), Schema.optional),
20
+ source: Ref.Ref(Text.Text).pipe(FormInputAnnotation.set(false)),
21
+ }).pipe(
22
+ Type.object({
23
+ typename: 'org.dxos.type.script',
24
+ version: '0.1.0',
25
+ }),
26
+ Annotation.LabelAnnotation.set(['name']),
27
+ Annotation.IconAnnotation.set({
28
+ icon: 'ph--code--regular',
29
+ hue: 'sky',
30
+ }),
31
+ );
32
+
33
+ export interface Script extends Schema.Schema.Type<typeof Script> {}
34
+
35
+ type Props = Omit<Obj.MakeProps<typeof Script>, 'source'> & { source?: string };
36
+
37
+ export const make = ({ source = '', ...props }: Props = {}): Script =>
38
+ Obj.make(Script, { ...props, source: Ref.make(Text.make({ content: source })) });
@@ -0,0 +1,207 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import * as Schema from 'effect/Schema';
6
+ import * as SchemaAST from 'effect/SchemaAST';
7
+
8
+ import { Annotation, Feed, Obj, QueryAST, Ref, Type, type Query } from '@dxos/echo';
9
+ import { OptionsAnnotationId, SystemTypeAnnotation } from '@dxos/echo/internal';
10
+ import { failedInvariant } from '@dxos/invariant';
11
+ import { DXN } from '@dxos/keys';
12
+
13
+ /**
14
+ * Type discriminator for TriggerType.
15
+ * Every spec has a type field of type TriggerKind that we can use to understand which type we're working with.
16
+ * https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions
17
+ */
18
+ export const Kinds = ['email', 'queue', 'subscription', 'timer', 'webhook'] as const;
19
+ export type Kind = (typeof Kinds)[number];
20
+
21
+ const kindLiteralAnnotations = { title: 'Kind' };
22
+
23
+ export const EmailSpec = Schema.Struct({
24
+ kind: Schema.Literal('email').annotations(kindLiteralAnnotations),
25
+ });
26
+ export type EmailSpec = Schema.Schema.Type<typeof EmailSpec>;
27
+
28
+ /**
29
+ * Construct an Email trigger spec.
30
+ */
31
+ export const specEmail = (): EmailSpec => ({ kind: 'email' });
32
+
33
+ // TODO(burdon): Change to Feed.
34
+ // TODO(wittjosiah): Remove. Migrate to Subscription triggers once EDGE supports them for feed queries.
35
+ export const QueueSpec = Schema.Struct({
36
+ kind: Schema.Literal('queue').annotations(kindLiteralAnnotations),
37
+
38
+ // TODO(dmaretskyi): Rename to `feed` and change to a reference.
39
+ queue: DXN.Schema,
40
+ });
41
+ export type QueueSpec = Schema.Schema.Type<typeof QueueSpec>;
42
+
43
+ /**
44
+ * Construct a Queue trigger spec from a queue DXN string.
45
+ */
46
+ export const specQueue = (queueDxn: string): QueueSpec => ({
47
+ kind: 'queue',
48
+ queue: queueDxn,
49
+ });
50
+
51
+ /**
52
+ * Construct a Queue trigger spec from a Feed object.
53
+ */
54
+ export const specFeed = (feed: Feed.Feed): QueueSpec =>
55
+ specQueue(Feed.getQueueDxn(feed)?.toString() ?? failedInvariant(new Error('Could not extract DXN from feed')));
56
+
57
+ /**
58
+ * Subscription.
59
+ */
60
+ export const SubscriptionSpec = Schema.Struct({
61
+ kind: Schema.Literal('subscription').annotations(kindLiteralAnnotations),
62
+ query: Schema.Struct({
63
+ raw: Schema.optional(Schema.String.annotations({ title: 'Query' })),
64
+ ast: QueryAST.Query,
65
+ }),
66
+ options: Schema.optional(
67
+ Schema.Struct({
68
+ // Watch changes to object (not just creation).
69
+ deep: Schema.optional(Schema.Boolean.annotations({ title: 'Nested' })),
70
+ // Debounce changes (delay in ms).
71
+ delay: Schema.optional(Schema.Number.annotations({ title: 'Delay' })),
72
+ }).annotations({ title: 'Options' }),
73
+ ),
74
+ });
75
+ export type SubscriptionSpec = Schema.Schema.Type<typeof SubscriptionSpec>;
76
+
77
+ /**
78
+ * Construct a Subscription trigger spec from a Query object.
79
+ */
80
+ export const specSubscription = (
81
+ query: Query.Query<any>,
82
+ options?: { deep?: boolean; delay?: number },
83
+ ): SubscriptionSpec => ({
84
+ kind: 'subscription',
85
+ query: {
86
+ ast: query.ast,
87
+ },
88
+ options: options
89
+ ? {
90
+ deep: options.deep,
91
+ delay: options.delay,
92
+ }
93
+ : undefined,
94
+ });
95
+
96
+ /**
97
+ * Cron timer.
98
+ */
99
+ export const TimerSpec = Schema.Struct({
100
+ kind: Schema.Literal('timer').annotations(kindLiteralAnnotations),
101
+ cron: Schema.String.annotations({
102
+ title: 'Cron',
103
+ [SchemaAST.ExamplesAnnotationId]: ['0 0 * * *'],
104
+ }),
105
+ });
106
+ export type TimerSpec = Schema.Schema.Type<typeof TimerSpec>;
107
+
108
+ /**
109
+ * Construct a Timer trigger spec from a cron string.
110
+ */
111
+ export const specTimer = (cron: string): TimerSpec => ({ kind: 'timer', cron });
112
+
113
+ /**
114
+ * Webhook.
115
+ */
116
+ export const WebhookSpec = Schema.Struct({
117
+ kind: Schema.Literal('webhook').annotations(kindLiteralAnnotations),
118
+ method: Schema.optional(
119
+ Schema.String.annotations({
120
+ title: 'Method',
121
+ [OptionsAnnotationId]: ['GET', 'POST'],
122
+ }),
123
+ ),
124
+ port: Schema.optional(
125
+ Schema.Number.annotations({
126
+ title: 'Port',
127
+ }),
128
+ ),
129
+ });
130
+ export type WebhookSpec = Schema.Schema.Type<typeof WebhookSpec>;
131
+
132
+ /**
133
+ * Construct a Webhook trigger spec from a method and port.
134
+ */
135
+ export const specWebhook = (opts?: { method?: string; port?: number }): WebhookSpec => ({
136
+ kind: 'webhook',
137
+ method: opts?.method,
138
+ port: opts?.port,
139
+ });
140
+
141
+ /**
142
+ * Trigger schema.
143
+ */
144
+ export const Spec = Schema.Union(EmailSpec, QueueSpec, SubscriptionSpec, TimerSpec, WebhookSpec).annotations({
145
+ title: 'Trigger',
146
+ });
147
+ export type Spec = Schema.Schema.Type<typeof Spec>;
148
+
149
+ /**
150
+ * Function trigger.
151
+ * Function is invoked with the `payload` passed as input data.
152
+ * The event that triggers the function is available in the function context.
153
+ */
154
+ const TriggerSchema = Schema.Struct({
155
+ /**
156
+ * Function or workflow to invoke.
157
+ */
158
+ // TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
159
+ function: Schema.optional(Ref.Ref(Obj.Unknown).annotations({ title: 'Function' })),
160
+
161
+ /**
162
+ * Only used for workflowSchema.
163
+ * Specifies the input node in the circuit.
164
+ * @deprecated Remove and enforce a single input node in all compute graphSchema.
165
+ */
166
+ inputNodeId: Schema.optional(Schema.String.annotations({ title: 'Input Node ID' })),
167
+
168
+ // TODO(burdon): NO BOOLEAN PROPERTIES (enabld/disabled/paused, etc.)
169
+ // Need lint rule; or agent rule to require PR review for "boolean" key word.
170
+ enabled: Schema.optional(Schema.Boolean.annotations({ title: 'Enabled' })),
171
+
172
+ spec: Schema.optional(Spec),
173
+
174
+ concurrency: Schema.optional(
175
+ Schema.Number.annotations({
176
+ title: 'Concurrency',
177
+ default: 1,
178
+ description:
179
+ 'Maximum number of concurrent invocations of the trigger. For Feed triggers, this will process Feed items in parallel.',
180
+ }),
181
+ ),
182
+
183
+ /**
184
+ * Passed as the input data to the function.
185
+ * Must match the function's input schema.
186
+ *
187
+ * @example
188
+ * {
189
+ * item: '{{event.item}}',
190
+ * instructions: 'Summarize and perform entity-extraction'
191
+ * mailbox: { '/': 'dxn:echo:AAA:ZZZ' }
192
+ * }
193
+ */
194
+ input: Schema.optional(Schema.Record({ key: Schema.String, value: Schema.Any })),
195
+ }).pipe(
196
+ Type.object({
197
+ typename: 'org.dxos.type.trigger',
198
+ version: '0.1.0',
199
+ }),
200
+ Annotation.IconAnnotation.set({ icon: 'ph--lightning--regular', hue: 'yellow' }),
201
+ SystemTypeAnnotation.set(true),
202
+ );
203
+
204
+ export interface Trigger extends Schema.Schema.Type<typeof TriggerSchema> {}
205
+ export const Trigger: Type.Obj<Trigger> = TriggerSchema as any;
206
+
207
+ export const make = (props: Obj.MakeProps<typeof Trigger>) => Obj.make(Trigger, props);
@@ -0,0 +1,62 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Schema from 'effect/Schema';
6
+
7
+ import { DXN, Obj, Ref } from '@dxos/echo';
8
+
9
+ // TODO(wittjosiah): Review this type.
10
+ // - Should be discriminated union.
11
+ // - Should be more consistent (e.g. subject vs item).
12
+ // - Should re-use schemas if possible.
13
+
14
+ // TODO(burdon): Reuse trigger schema from @dxos/functions (TriggerType).
15
+ export const EmailEvent = Schema.Struct({
16
+ from: Schema.String,
17
+ to: Schema.String,
18
+ subject: Schema.String,
19
+ created: Schema.String,
20
+ body: Schema.String,
21
+ });
22
+ export type EmailEvent = Schema.Schema.Type<typeof EmailEvent>;
23
+
24
+ export const QueueEvent = Schema.Struct({
25
+ queue: DXN.Schema,
26
+ item: Schema.Any,
27
+ cursor: Schema.String,
28
+ });
29
+ export type QueueEvent = Schema.Schema.Type<typeof QueueEvent>;
30
+
31
+ export const SubscriptionEvent = Schema.Struct({
32
+ /**
33
+ * Type of the mutation.
34
+ */
35
+ // TODO(dmaretskyi): Specify enum.
36
+ type: Schema.String,
37
+
38
+ /**
39
+ * Reference to the object that was changed or created.
40
+ */
41
+ subject: Ref.Ref(Obj.Unknown),
42
+
43
+ /**
44
+ * @deprecated
45
+ */
46
+ changedObjectId: Schema.optional(Schema.String),
47
+ });
48
+ export type SubscriptionEvent = Schema.Schema.Type<typeof SubscriptionEvent>;
49
+
50
+ export const TimerEvent = Schema.Struct({ tick: Schema.Number });
51
+ export type TimerEvent = Schema.Schema.Type<typeof TimerEvent>;
52
+
53
+ export const WebhookEvent = Schema.Struct({
54
+ url: Schema.String,
55
+ method: Schema.Literal('GET', 'POST'),
56
+ headers: Schema.Record({ key: Schema.String, value: Schema.String }),
57
+ bodyText: Schema.String,
58
+ });
59
+ export type WebhookEvent = Schema.Schema.Type<typeof WebhookEvent>;
60
+
61
+ export const TriggerEvent = Schema.Union(EmailEvent, QueueEvent, SubscriptionEvent, TimerEvent, WebhookEvent);
62
+ export type TriggerEvent = Schema.Schema.Type<typeof TriggerEvent>;
@@ -0,0 +1,8 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export * as Script from './Script';
6
+ export * as Trigger from './Trigger';
7
+ export * as TriggerEvent from './TriggerEvent';
8
+ export * from './url';
@@ -0,0 +1,32 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { type Obj } from '@dxos/echo';
6
+
7
+ // TODO: use URL scheme for source?
8
+ export const FUNCTIONS_META_KEY = 'org.dxos.service.function';
9
+
10
+ export const FUNCTIONS_PRESET_META_KEY = 'org.dxos.service.function-preset';
11
+
12
+ /**
13
+ * NOTE: functionId is backend ID, not ECHO object id.
14
+ */
15
+ export const getUserFunctionIdInMetadata = (meta: Obj.ReadonlyMeta) => {
16
+ return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;
17
+ };
18
+
19
+ /**
20
+ * NOTE: functionId is backend ID, not ECHO object id.
21
+ * Must be called inside Obj.changeMeta() since it mutates the meta.
22
+ */
23
+ export const setUserFunctionIdInMetadata = (meta: Obj.Meta, functionId: string) => {
24
+ const key = meta.keys.find((key) => key.source === FUNCTIONS_META_KEY);
25
+ if (key) {
26
+ if (key.id !== functionId) {
27
+ throw new Error('Metadata mismatch');
28
+ }
29
+ } else {
30
+ meta.keys.push({ source: FUNCTIONS_META_KEY, id: functionId });
31
+ }
32
+ };