@dxos/functions 0.8.4-main.67995b8 → 0.8.4-main.6fa680abb7

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 (171) hide show
  1. package/README.md +4 -6
  2. package/dist/lib/neutral/index.mjs +1240 -0
  3. package/dist/lib/neutral/index.mjs.map +7 -0
  4. package/dist/lib/neutral/meta.json +1 -0
  5. package/dist/types/src/errors.d.ts +91 -38
  6. package/dist/types/src/errors.d.ts.map +1 -1
  7. package/dist/types/src/example/fib.d.ts +7 -0
  8. package/dist/types/src/example/fib.d.ts.map +1 -0
  9. package/dist/types/src/example/forex-effect.d.ts +3 -0
  10. package/dist/types/src/example/forex-effect.d.ts.map +1 -0
  11. package/dist/types/src/example/index.d.ts +12 -0
  12. package/dist/types/src/example/index.d.ts.map +1 -0
  13. package/dist/types/src/example/reply.d.ts +3 -0
  14. package/dist/types/src/example/reply.d.ts.map +1 -0
  15. package/dist/types/src/example/sleep.d.ts +5 -0
  16. package/dist/types/src/example/sleep.d.ts.map +1 -0
  17. package/dist/types/src/index.d.ts +5 -7
  18. package/dist/types/src/index.d.ts.map +1 -1
  19. package/dist/types/src/operation-compatibility.test.d.ts +2 -0
  20. package/dist/types/src/operation-compatibility.test.d.ts.map +1 -0
  21. package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
  22. package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
  23. package/dist/types/src/protocol/index.d.ts +2 -0
  24. package/dist/types/src/protocol/index.d.ts.map +1 -0
  25. package/dist/types/src/protocol/protocol.d.ts +7 -0
  26. package/dist/types/src/protocol/protocol.d.ts.map +1 -0
  27. package/dist/types/src/protocol/protocol.test.d.ts +2 -0
  28. package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
  29. package/dist/types/src/sdk.d.ts +117 -0
  30. package/dist/types/src/sdk.d.ts.map +1 -0
  31. package/dist/types/src/services/credentials.d.ts +21 -3
  32. package/dist/types/src/services/credentials.d.ts.map +1 -1
  33. package/dist/types/src/services/event-logger.d.ts +37 -28
  34. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  35. package/dist/types/src/services/function-invocation-service.d.ts +16 -0
  36. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
  37. package/dist/types/src/services/index.d.ts +5 -6
  38. package/dist/types/src/services/index.d.ts.map +1 -1
  39. package/dist/types/src/services/queues.d.ts +21 -5
  40. package/dist/types/src/services/queues.d.ts.map +1 -1
  41. package/dist/types/src/services/tracing.d.ts +69 -7
  42. package/dist/types/src/services/tracing.d.ts.map +1 -1
  43. package/dist/types/src/types/Function.d.ts +52 -0
  44. package/dist/types/src/types/Function.d.ts.map +1 -0
  45. package/dist/types/src/types/Script.d.ts +21 -0
  46. package/dist/types/src/types/Script.d.ts.map +1 -0
  47. package/dist/types/src/types/Trigger.d.ts +122 -0
  48. package/dist/types/src/types/Trigger.d.ts.map +1 -0
  49. package/dist/types/src/types/TriggerEvent.d.ts +75 -0
  50. package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
  51. package/dist/types/src/types/index.d.ts +6 -0
  52. package/dist/types/src/types/index.d.ts.map +1 -0
  53. package/dist/types/src/types/url.d.ts +13 -0
  54. package/dist/types/src/types/url.d.ts.map +1 -0
  55. package/dist/types/tsconfig.tsbuildinfo +1 -1
  56. package/package.json +27 -75
  57. package/src/errors.ts +13 -5
  58. package/src/example/fib.ts +32 -0
  59. package/src/example/forex-effect.ts +40 -0
  60. package/src/example/index.ts +13 -0
  61. package/src/example/reply.ts +21 -0
  62. package/src/example/sleep.ts +24 -0
  63. package/src/index.ts +5 -9
  64. package/src/operation-compatibility.test.ts +185 -0
  65. package/src/protocol/functions-ai-http-client.ts +67 -0
  66. package/src/{executor → protocol}/index.ts +1 -1
  67. package/src/protocol/protocol.test.ts +59 -0
  68. package/src/protocol/protocol.ts +258 -0
  69. package/src/sdk.ts +297 -0
  70. package/src/services/credentials.ts +100 -5
  71. package/src/services/event-logger.ts +15 -6
  72. package/src/services/function-invocation-service.ts +37 -0
  73. package/src/services/index.ts +5 -6
  74. package/src/services/queues.ts +34 -10
  75. package/src/services/tracing.ts +140 -17
  76. package/src/types/Function.ts +83 -0
  77. package/src/types/Script.ts +34 -0
  78. package/src/types/Trigger.ts +154 -0
  79. package/src/types/TriggerEvent.ts +62 -0
  80. package/src/types/index.ts +9 -0
  81. package/src/types/url.ts +32 -0
  82. package/dist/lib/browser/bundler/index.mjs +0 -247
  83. package/dist/lib/browser/bundler/index.mjs.map +0 -7
  84. package/dist/lib/browser/chunk-6PTFLPCO.mjs +0 -462
  85. package/dist/lib/browser/chunk-6PTFLPCO.mjs.map +0 -7
  86. package/dist/lib/browser/edge/index.mjs +0 -69
  87. package/dist/lib/browser/edge/index.mjs.map +0 -7
  88. package/dist/lib/browser/index.mjs +0 -501
  89. package/dist/lib/browser/index.mjs.map +0 -7
  90. package/dist/lib/browser/meta.json +0 -1
  91. package/dist/lib/browser/testing/index.mjs +0 -59
  92. package/dist/lib/browser/testing/index.mjs.map +0 -7
  93. package/dist/lib/node-esm/bundler/index.mjs +0 -249
  94. package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
  95. package/dist/lib/node-esm/chunk-NYJ2TSXO.mjs +0 -464
  96. package/dist/lib/node-esm/chunk-NYJ2TSXO.mjs.map +0 -7
  97. package/dist/lib/node-esm/edge/index.mjs +0 -71
  98. package/dist/lib/node-esm/edge/index.mjs.map +0 -7
  99. package/dist/lib/node-esm/index.mjs +0 -502
  100. package/dist/lib/node-esm/index.mjs.map +0 -7
  101. package/dist/lib/node-esm/meta.json +0 -1
  102. package/dist/lib/node-esm/testing/index.mjs +0 -60
  103. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  104. package/dist/types/src/bundler/bundler.d.ts +0 -50
  105. package/dist/types/src/bundler/bundler.d.ts.map +0 -1
  106. package/dist/types/src/bundler/bundler.test.d.ts +0 -2
  107. package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
  108. package/dist/types/src/bundler/index.d.ts +0 -2
  109. package/dist/types/src/bundler/index.d.ts.map +0 -1
  110. package/dist/types/src/edge/functions.d.ts +0 -16
  111. package/dist/types/src/edge/functions.d.ts.map +0 -1
  112. package/dist/types/src/edge/index.d.ts +0 -2
  113. package/dist/types/src/edge/index.d.ts.map +0 -1
  114. package/dist/types/src/executor/executor.d.ts +0 -8
  115. package/dist/types/src/executor/executor.d.ts.map +0 -1
  116. package/dist/types/src/executor/index.d.ts +0 -2
  117. package/dist/types/src/executor/index.d.ts.map +0 -1
  118. package/dist/types/src/handler.d.ts +0 -62
  119. package/dist/types/src/handler.d.ts.map +0 -1
  120. package/dist/types/src/schema.d.ts +0 -38
  121. package/dist/types/src/schema.d.ts.map +0 -1
  122. package/dist/types/src/services/database.d.ts +0 -30
  123. package/dist/types/src/services/database.d.ts.map +0 -1
  124. package/dist/types/src/services/local-function-execution.d.ts +0 -11
  125. package/dist/types/src/services/local-function-execution.d.ts.map +0 -1
  126. package/dist/types/src/services/remote-function-execution-service.d.ts +0 -15
  127. package/dist/types/src/services/remote-function-execution-service.d.ts.map +0 -1
  128. package/dist/types/src/services/service-container.d.ts +0 -56
  129. package/dist/types/src/services/service-container.d.ts.map +0 -1
  130. package/dist/types/src/services/service-registry.d.ts +0 -29
  131. package/dist/types/src/services/service-registry.d.ts.map +0 -1
  132. package/dist/types/src/services/service-registry.test.d.ts +0 -2
  133. package/dist/types/src/services/service-registry.test.d.ts.map +0 -1
  134. package/dist/types/src/testing/index.d.ts +0 -3
  135. package/dist/types/src/testing/index.d.ts.map +0 -1
  136. package/dist/types/src/testing/layer.d.ts +0 -10
  137. package/dist/types/src/testing/layer.d.ts.map +0 -1
  138. package/dist/types/src/testing/logger.d.ts +0 -5
  139. package/dist/types/src/testing/logger.d.ts.map +0 -1
  140. package/dist/types/src/testing/services.d.ts +0 -59
  141. package/dist/types/src/testing/services.d.ts.map +0 -1
  142. package/dist/types/src/trace.d.ts +0 -124
  143. package/dist/types/src/trace.d.ts.map +0 -1
  144. package/dist/types/src/translations.d.ts +0 -12
  145. package/dist/types/src/translations.d.ts.map +0 -1
  146. package/dist/types/src/types.d.ts +0 -411
  147. package/dist/types/src/types.d.ts.map +0 -1
  148. package/dist/types/src/url.d.ts +0 -17
  149. package/dist/types/src/url.d.ts.map +0 -1
  150. package/src/bundler/bundler.test.ts +0 -59
  151. package/src/bundler/bundler.ts +0 -292
  152. package/src/bundler/index.ts +0 -5
  153. package/src/edge/functions.ts +0 -64
  154. package/src/edge/index.ts +0 -9
  155. package/src/executor/executor.ts +0 -54
  156. package/src/handler.ts +0 -120
  157. package/src/schema.ts +0 -57
  158. package/src/services/database.ts +0 -74
  159. package/src/services/local-function-execution.ts +0 -70
  160. package/src/services/remote-function-execution-service.ts +0 -66
  161. package/src/services/service-container.ts +0 -113
  162. package/src/services/service-registry.test.ts +0 -42
  163. package/src/services/service-registry.ts +0 -59
  164. package/src/testing/index.ts +0 -6
  165. package/src/testing/layer.ts +0 -31
  166. package/src/testing/logger.ts +0 -16
  167. package/src/testing/services.ts +0 -114
  168. package/src/trace.ts +0 -180
  169. package/src/translations.ts +0 -20
  170. package/src/types.ts +0 -211
  171. package/src/url.ts +0 -52
@@ -2,11 +2,14 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Effect, Context, Schema, Layer } 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
10
  import { Obj, Type } from '@dxos/echo';
8
11
  import { invariant } from '@dxos/invariant';
9
- import { log, LogLevel } from '@dxos/log';
12
+ import { LogLevel, log } from '@dxos/log';
10
13
 
11
14
  import { TracingService } from './tracing';
12
15
 
@@ -14,12 +17,18 @@ export const ComputeEventPayload = Schema.Union(
14
17
  Schema.Struct({
15
18
  type: Schema.Literal('begin-compute'),
16
19
  nodeId: Schema.String,
17
- inputs: Schema.Record({ key: Schema.String, value: Schema.Any }),
20
+ /**
21
+ * Names of the inputs begin computed.
22
+ */
23
+ inputs: Schema.Array(Schema.String),
18
24
  }),
19
25
  Schema.Struct({
20
26
  type: Schema.Literal('end-compute'),
21
27
  nodeId: Schema.String,
22
- outputs: Schema.Record({ key: Schema.String, value: Schema.Any }),
28
+ /**
29
+ * Names of the outputs computed.
30
+ */
31
+ outputs: Schema.Array(Schema.String),
23
32
  }),
24
33
  Schema.Struct({
25
34
  type: Schema.Literal('compute-input'),
@@ -43,7 +52,7 @@ export type ComputeEventPayload = Schema.Schema.Type<typeof ComputeEventPayload>
43
52
 
44
53
  export const ComputeEvent = Schema.Struct({
45
54
  payload: ComputeEventPayload,
46
- }).pipe(Type.Obj({ typename: 'dxos.org/type/ComputeEvent', version: '0.1.0' }));
55
+ }).pipe(Type.object({ typename: 'org.dxos.type.compute-event', version: '0.1.0' }));
47
56
 
48
57
  /**
49
58
  * Logs event for the compute workflows.
@@ -66,7 +75,7 @@ export class ComputeEventLogger extends Context.Tag('@dxos/functions/ComputeEven
66
75
  const tracing = yield* TracingService;
67
76
  return {
68
77
  log: (event: ComputeEventPayload) => {
69
- tracing.write(Obj.make(ComputeEvent, { payload: event }));
78
+ tracing.write(Obj.make(ComputeEvent, { payload: event }), tracing.getTraceContext());
70
79
  },
71
80
  nodeId: undefined,
72
81
  };
@@ -0,0 +1,37 @@
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 } from '../errors';
9
+ import { type FunctionDefinition, type InvocationServices } from '../sdk';
10
+
11
+ export class FunctionInvocationService extends Context.Tag('@dxos/functions/FunctionInvocationService')<
12
+ FunctionInvocationService,
13
+ {
14
+ invokeFunction<I, O>(
15
+ functionDef: FunctionDefinition<I, O, any>,
16
+ input: I,
17
+ ): Effect.Effect<O, never, InvocationServices>;
18
+
19
+ resolveFunction(key: string): Effect.Effect<FunctionDefinition.Any, FunctionNotFoundError>;
20
+ }
21
+ >() {
22
+ static layerNotAvailable = Layer.succeed(FunctionInvocationService, {
23
+ invokeFunction: () => Effect.die('FunctionInvocationService is not avaialble.'),
24
+ resolveFunction: () => Effect.die('FunctionInvocationService is not available.'),
25
+ });
26
+
27
+ static invokeFunction = <I, O>(
28
+ functionDef: FunctionDefinition<I, O, any>,
29
+ input: I,
30
+ ): Effect.Effect<O, never, FunctionInvocationService | InvocationServices> =>
31
+ Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.invokeFunction)(functionDef, input);
32
+
33
+ static resolveFunction = (
34
+ key: string,
35
+ ): Effect.Effect<FunctionDefinition.Any, FunctionNotFoundError, FunctionInvocationService> =>
36
+ Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.resolveFunction)(key);
37
+ }
@@ -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 * from './tracing';
@@ -2,9 +2,13 @@
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 { Entity } 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
13
  /**
10
14
  * Gives access to all queues.
@@ -21,30 +25,48 @@ export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
21
25
  * The queue that is used to store the context of the current research.
22
26
  * @deprecated Use `ContextQueueService` instead.
23
27
  */
24
- readonly contextQueue: Queue | undefined;
28
+ readonly queue: Queue | undefined;
25
29
  }
26
30
  >() {
27
31
  static notAvailable = Layer.succeed(QueueService, {
28
32
  queues: {
29
- get(dxn) {
33
+ get(_dxn) {
30
34
  throw new Error('Queues not available');
31
35
  },
32
36
  create() {
33
37
  throw new Error('Queues not available');
34
38
  },
35
39
  },
36
- contextQueue: undefined,
40
+ queue: undefined,
37
41
  });
38
42
 
39
- static make = (queues: QueueFactory, contextQueue?: Queue): Context.Tag.Service<QueueService> => {
43
+ static make = (queues: QueueFactory, queue?: Queue): Context.Tag.Service<QueueService> => {
40
44
  return {
41
45
  queues,
42
- contextQueue,
46
+ queue,
43
47
  };
44
48
  };
45
49
 
46
- static makeLayer = (queues: QueueFactory, contextQueue?: Queue): Layer.Layer<QueueService> =>
47
- Layer.succeed(QueueService, QueueService.make(queues, contextQueue));
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 Entity.Unknown = Entity.Unknown>(
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 Entity.Unknown = Entity.Unknown>(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 Entity.Unknown = Entity.Unknown>(queue: Queue<T>, objects: T[]): Effect.Effect<void> =>
69
+ Effect.promise(() => queue.append(objects));
48
70
  }
49
71
 
50
72
  /**
@@ -53,6 +75,8 @@ export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
53
75
  export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQueueService')<
54
76
  ContextQueueService,
55
77
  {
56
- readonly contextQueue: Queue;
78
+ readonly queue: Queue;
57
79
  }
58
- >() {}
80
+ >() {
81
+ static layer = (queue: Queue) => Layer.succeed(ContextQueueService, { queue });
82
+ }
@@ -2,11 +2,16 @@
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
9
  import { AgentStatus } from '@dxos/ai';
8
- import { Obj } from '@dxos/echo';
9
- import type { AnyEchoObject } from '@dxos/echo-schema';
10
+ import { type DXN, Obj } from '@dxos/echo';
11
+ import { ObjectId } from '@dxos/keys';
12
+ import { Message } from '@dxos/types';
13
+
14
+ import type { Trigger } from '../types';
10
15
 
11
16
  /**
12
17
  * Provides a way for compute primitives (functions, workflows, tools)
@@ -15,30 +20,148 @@ import type { AnyEchoObject } from '@dxos/echo-schema';
15
20
  export class TracingService extends Context.Tag('@dxos/functions/TracingService')<
16
21
  TracingService,
17
22
  {
23
+ /**
24
+ * Gets the parent message ID.
25
+ */
26
+ getTraceContext: () => TracingService.TraceContext;
27
+
18
28
  /**
19
29
  * Write an event to the tracing queue.
20
30
  * @param event - The event to write. Must be an a typed object.
21
31
  */
22
- write(event: AnyEchoObject): void;
32
+ write: (event: Obj.Unknown, traceContext: TracingService.TraceContext) => void;
33
+
34
+ traceInvocationStart({
35
+ payload,
36
+ target,
37
+ }: {
38
+ payload: TracingService.FunctionInvocationPayload;
39
+ target?: DXN;
40
+ }): Effect.Effect<TracingService.InvocationTraceData>;
41
+
42
+ traceInvocationEnd({
43
+ trace,
44
+ exception,
45
+ }: {
46
+ trace: TracingService.InvocationTraceData;
47
+ exception?: any;
48
+ }): Effect.Effect<void>;
23
49
  }
24
50
  >() {
25
- static noop: Context.Tag.Service<TracingService> = { write: () => {} };
51
+ static noop: Context.Tag.Service<TracingService> = {
52
+ getTraceContext: () => ({}),
53
+ write: () => {},
54
+ traceInvocationStart: () =>
55
+ Effect.sync(() => ({ invocationId: ObjectId.random(), invocationTraceQueue: undefined })),
56
+ traceInvocationEnd: () => Effect.sync(() => {}),
57
+ };
26
58
 
27
- static layerNoop = Layer.succeed(TracingService, TracingService.noop);
59
+ static layerNoop: Layer.Layer<TracingService> = Layer.succeed(TracingService, TracingService.noop);
28
60
 
29
- static console: Context.Tag.Service<TracingService> = {
30
- write: (event) => {
31
- // eslint-disable-next-line no-console
32
- console.log(event);
33
- },
34
- };
61
+ /**
62
+ * Creates a TracingService layer that emits events to the parent tracing service.
63
+ */
64
+ static layerSubframe = (mapContext: (currentContext: TracingService.TraceContext) => TracingService.TraceContext) =>
65
+ Layer.effect(
66
+ TracingService,
67
+ Effect.gen(function* () {
68
+ const tracing = yield* TracingService;
69
+ const context = mapContext(tracing.getTraceContext());
70
+ return {
71
+ write: (event, context) => tracing.write(event, context),
72
+ getTraceContext: () => context,
73
+ traceInvocationStart: () => Effect.die('Tracing invocation inside another invocation is not supported.'),
74
+ traceInvocationEnd: () => Effect.die('Tracing invocation inside another invocation is not supported.'),
75
+ };
76
+ }),
77
+ );
78
+
79
+ /**
80
+ * Create sublayer to trace an invocation.
81
+ * @param data
82
+ * @returns
83
+ */
84
+ static layerInvocation = (data: TracingService.InvocationTraceData) =>
85
+ TracingService.layerSubframe((context) => ({
86
+ ...context,
87
+ currentInvocation: data,
88
+ }));
35
89
 
36
90
  /**
37
91
  * Emit the current human-readable execution status.
38
92
  */
39
- static emitStatus: (data: Obj.MakeProps<typeof AgentStatus>) => Effect.Effect<unknown, never, void> =
40
- Effect.fnUntraced(function* (data) {
41
- const tracing = yield* TracingService;
42
- tracing.write(Obj.make(AgentStatus, data));
43
- });
93
+ static emitStatus: (
94
+ data: Omit<Obj.MakeProps<typeof AgentStatus>, 'created'>,
95
+ ) => Effect.Effect<void, never, TracingService> = Effect.fnUntraced(function* (data) {
96
+ const tracing = yield* TracingService;
97
+ tracing.write(
98
+ Obj.make(AgentStatus, {
99
+ parentMessage: tracing.getTraceContext().parentMessage,
100
+ toolCallId: tracing.getTraceContext().toolCallId,
101
+ created: new Date().toISOString(),
102
+ ...data,
103
+ }),
104
+ tracing.getTraceContext(),
105
+ );
106
+ });
107
+
108
+ static emitConverationMessage: (
109
+ data: Obj.MakeProps<typeof Message.Message>,
110
+ ) => Effect.Effect<void, never, TracingService> = Effect.fnUntraced(function* (data) {
111
+ const tracing = yield* TracingService;
112
+ tracing.write(
113
+ Obj.make(Message.Message, {
114
+ parentMessage: tracing.getTraceContext().parentMessage,
115
+ ...data,
116
+ properties: {
117
+ [MESSAGE_PROPERTY_TOOL_CALL_ID]: tracing.getTraceContext().toolCallId,
118
+ ...data.properties,
119
+ },
120
+ }),
121
+ tracing.getTraceContext(),
122
+ );
123
+ });
44
124
  }
125
+
126
+ export namespace TracingService {
127
+ export interface TraceContext {
128
+ currentInvocation?: InvocationTraceData;
129
+
130
+ /**
131
+ * If this thread sprung from a tool call, this is the ID of the message containing the tool call.
132
+ */
133
+ parentMessage?: ObjectId;
134
+
135
+ /**
136
+ * If the current thread is a byproduct of a tool call, this is the ID of the tool call.
137
+ */
138
+ toolCallId?: string;
139
+
140
+ debugInfo?: unknown;
141
+ }
142
+
143
+ /**
144
+ * Trace data for a function/trigger invocation.
145
+ */
146
+ export interface InvocationTraceData {
147
+ invocationId: ObjectId;
148
+ invocationTraceQueue?: DXN.String;
149
+ }
150
+
151
+ /**
152
+ * Payload for a function/trigger invocation.
153
+ */
154
+ export interface FunctionInvocationPayload {
155
+ data?: any;
156
+ inputNodeId?: string;
157
+ trigger?: {
158
+ id: string;
159
+ kind: Trigger.Kind;
160
+ };
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Goes into {@link Message['properties']}
166
+ */
167
+ export const MESSAGE_PROPERTY_TOOL_CALL_ID = 'toolCallId' as const;
@@ -0,0 +1,83 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import * as Schema from 'effect/Schema';
6
+
7
+ import { Annotation, JsonSchema, Obj, Ref, Type } from '@dxos/echo';
8
+ import { SystemTypeAnnotation } from '@dxos/echo/internal';
9
+
10
+ import { Script } from './Script';
11
+
12
+ /**
13
+ * Function deployment.
14
+ */
15
+ export const Function = Schema.Struct({
16
+ /**
17
+ * Global registry ID.
18
+ * NOTE: The `key` property refers to the original registry entry.
19
+ */
20
+ // TODO(burdon): Create Format type for DXN-like ids, such as this and schema type.
21
+ // TODO(dmaretskyi): Consider making it part of ECHO meta.
22
+ // TODO(dmaretskyi): Make required.
23
+ key: Schema.optional(Schema.String).annotations({
24
+ description: 'Unique registration key for the blueprint',
25
+ }),
26
+
27
+ name: Schema.NonEmptyString,
28
+ version: Schema.String,
29
+
30
+ description: Schema.optional(Schema.String),
31
+
32
+ /**
33
+ * ISO date string of the last deployment.
34
+ */
35
+ updated: Schema.optional(Schema.String),
36
+
37
+ // Reference to a source script if it exists within ECHO.
38
+ // TODO(burdon): Don't ref ScriptType directly (core).
39
+ source: Schema.optional(Ref.Ref(Script)),
40
+
41
+ inputSchema: Schema.optional(JsonSchema.JsonSchema),
42
+ outputSchema: Schema.optional(JsonSchema.JsonSchema),
43
+
44
+ /**
45
+ * List of required services.
46
+ * Match the Context.Tag keys of the FunctionServices variants.
47
+ */
48
+ services: Schema.optional(Schema.Array(Schema.String)),
49
+
50
+ // Local binding to a function name.
51
+ binding: Schema.optional(Schema.String),
52
+ }).pipe(
53
+ Type.object({
54
+ typename: 'org.dxos.type.function',
55
+ version: '0.1.0',
56
+ }),
57
+ Annotation.LabelAnnotation.set(['name']),
58
+ Annotation.IconAnnotation.set({ icon: 'ph--function--regular', hue: 'blue' }),
59
+ SystemTypeAnnotation.set(true),
60
+ );
61
+
62
+ export interface Function extends Schema.Schema.Type<typeof Function> {}
63
+
64
+ export const make = (props: Obj.MakeProps<typeof Function>) => Obj.make(Function, props);
65
+
66
+ /**
67
+ * Copies properties from source to target.
68
+ * @param target - Target object to copy properties to.
69
+ * @param source - Source object to copy properties from.
70
+ */
71
+ export const setFrom = (target: Function, source: Function) => {
72
+ Obj.change(target, (t) => {
73
+ t.key = source.key ?? target.key;
74
+ t.name = source.name ?? target.name;
75
+ t.version = source.version;
76
+ t.description = source.description;
77
+ t.updated = source.updated;
78
+ // TODO(dmaretskyi): A workaround for an ECHO bug.
79
+ t.inputSchema = source.inputSchema ? JSON.parse(JSON.stringify(source.inputSchema)) : undefined;
80
+ t.outputSchema = source.outputSchema ? JSON.parse(JSON.stringify(source.outputSchema)) : undefined;
81
+ Obj.getMeta(t).keys = JSON.parse(JSON.stringify(Obj.getMeta(source).keys));
82
+ });
83
+ };
@@ -0,0 +1,34 @@
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
+ );
28
+
29
+ export interface Script extends Schema.Schema.Type<typeof Script> {}
30
+
31
+ type Props = Omit<Obj.MakeProps<typeof Script>, 'source'> & { source?: string };
32
+
33
+ export const make = ({ source = '', ...props }: Props = {}): Script =>
34
+ Obj.make(Script, { ...props, source: Ref.make(Text.make(source)) });
@@ -0,0 +1,154 @@
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, Obj, QueryAST, Ref, Type } from '@dxos/echo';
9
+ import { OptionsAnnotationId, SystemTypeAnnotation } from '@dxos/echo/internal';
10
+ import { DXN } from '@dxos/keys';
11
+ import { Expando } from '@dxos/schema';
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
+ // TODO(wittjosiah): Remove. Migrate to Subscription triggers once EDGE supports them for feed queries.
29
+ export const QueueSpec = Schema.Struct({
30
+ kind: Schema.Literal('queue').annotations(kindLiteralAnnotations),
31
+
32
+ // TODO(dmaretskyi): Change to a reference.
33
+ queue: DXN.Schema,
34
+ });
35
+ export type QueueSpec = Schema.Schema.Type<typeof QueueSpec>;
36
+
37
+ /**
38
+ * Subscription.
39
+ */
40
+ export const SubscriptionSpec = Schema.Struct({
41
+ kind: Schema.Literal('subscription').annotations(kindLiteralAnnotations),
42
+ query: Schema.Struct({
43
+ raw: Schema.optional(Schema.String.annotations({ title: 'Query' })),
44
+ ast: QueryAST.Query,
45
+ }),
46
+ options: Schema.optional(
47
+ Schema.Struct({
48
+ // Watch changes to object (not just creation).
49
+ deep: Schema.optional(Schema.Boolean.annotations({ title: 'Nested' })),
50
+ // Debounce changes (delay in ms).
51
+ delay: Schema.optional(Schema.Number.annotations({ title: 'Delay' })),
52
+ }).annotations({ title: 'Options' }),
53
+ ),
54
+ });
55
+ export type SubscriptionSpec = Schema.Schema.Type<typeof SubscriptionSpec>;
56
+
57
+ /**
58
+ * Cron timer.
59
+ */
60
+ export const TimerSpec = Schema.Struct({
61
+ kind: Schema.Literal('timer').annotations(kindLiteralAnnotations),
62
+ cron: Schema.String.annotations({
63
+ title: 'Cron',
64
+ [SchemaAST.ExamplesAnnotationId]: ['0 0 * * *'],
65
+ }),
66
+ });
67
+ export type TimerSpec = Schema.Schema.Type<typeof TimerSpec>;
68
+
69
+ /**
70
+ * Webhook.
71
+ */
72
+ export const WebhookSpec = Schema.Struct({
73
+ kind: Schema.Literal('webhook').annotations(kindLiteralAnnotations),
74
+ method: Schema.optional(
75
+ Schema.String.annotations({
76
+ title: 'Method',
77
+ [OptionsAnnotationId]: ['GET', 'POST'],
78
+ }),
79
+ ),
80
+ port: Schema.optional(
81
+ Schema.Number.annotations({
82
+ title: 'Port',
83
+ }),
84
+ ),
85
+ });
86
+ export type WebhookSpec = Schema.Schema.Type<typeof WebhookSpec>;
87
+
88
+ /**
89
+ * Trigger schema.
90
+ */
91
+ export const Spec = Schema.Union(EmailSpec, QueueSpec, SubscriptionSpec, TimerSpec, WebhookSpec).annotations({
92
+ title: 'Trigger',
93
+ });
94
+ export type Spec = Schema.Schema.Type<typeof Spec>;
95
+
96
+ /**
97
+ * Function trigger.
98
+ * Function is invoked with the `payload` passed as input data.
99
+ * The event that triggers the function is available in the function context.
100
+ */
101
+ const TriggerSchema = Schema.Struct({
102
+ /**
103
+ * Function or workflow to invoke.
104
+ */
105
+ // TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
106
+ function: Schema.optional(Ref.Ref(Expando.Expando).annotations({ title: 'Function' })),
107
+
108
+ /**
109
+ * Only used for workflowSchema.
110
+ * Specifies the input node in the circuit.
111
+ * @deprecated Remove and enforce a single input node in all compute graphSchema.
112
+ */
113
+ inputNodeId: Schema.optional(Schema.String.annotations({ title: 'Input Node ID' })),
114
+
115
+ // TODO(burdon): NO BOOLEAN PROPERTIES (enabld/disabled/paused, etc.)
116
+ // Need lint rule; or agent rule to require PR review for "boolean" key word.
117
+ enabled: Schema.optional(Schema.Boolean.annotations({ title: 'Enabled' })),
118
+
119
+ spec: Schema.optional(Spec),
120
+
121
+ concurrency: Schema.optional(
122
+ Schema.Number.annotations({
123
+ title: 'Concurrency',
124
+ default: 1,
125
+ description:
126
+ 'Maximum number of concurrent invocations of the trigger. For queue triggers, this will process queue items in parallel.',
127
+ }),
128
+ ),
129
+
130
+ /**
131
+ * Passed as the input data to the function.
132
+ * Must match the function's input schema.
133
+ *
134
+ * @example
135
+ * {
136
+ * item: '{{$.trigger.event}}',
137
+ * instructions: 'Summarize and perform entity-extraction'
138
+ * mailbox: { '/': 'dxn:echo:AAA:ZZZ' }
139
+ * }
140
+ */
141
+ input: Schema.optional(Schema.Record({ key: Schema.String, value: Schema.Any })),
142
+ }).pipe(
143
+ Type.object({
144
+ typename: 'org.dxos.type.trigger',
145
+ version: '0.1.0',
146
+ }),
147
+ Annotation.IconAnnotation.set({ icon: 'ph--lightning--regular', hue: 'yellow' }),
148
+ SystemTypeAnnotation.set(true),
149
+ );
150
+
151
+ export interface Trigger extends Schema.Schema.Type<typeof TriggerSchema> {}
152
+ export const Trigger: Type.Obj<Trigger> = TriggerSchema as any;
153
+
154
+ export const make = (props: Obj.MakeProps<typeof Trigger>) => Obj.make(Trigger, props);