@dxos/functions 0.8.4-main.84f28bd → 0.8.4-main.937b3ca

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 (161) hide show
  1. package/dist/lib/browser/index.mjs +1100 -363
  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 +1100 -363
  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 +121 -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/operation-compatibility.test.d.ts +2 -0
  22. package/dist/types/src/operation-compatibility.test.d.ts.map +1 -0
  23. package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
  24. package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
  25. package/dist/types/src/protocol/index.d.ts +2 -0
  26. package/dist/types/src/protocol/index.d.ts.map +1 -0
  27. package/dist/types/src/protocol/protocol.d.ts +7 -0
  28. package/dist/types/src/protocol/protocol.d.ts.map +1 -0
  29. package/dist/types/src/protocol/protocol.test.d.ts +2 -0
  30. package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
  31. package/dist/types/src/sdk.d.ts +114 -0
  32. package/dist/types/src/sdk.d.ts.map +1 -0
  33. package/dist/types/src/services/credentials.d.ts +23 -3
  34. package/dist/types/src/services/credentials.d.ts.map +1 -1
  35. package/dist/types/src/services/event-logger.d.ts +74 -30
  36. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  37. package/dist/types/src/services/function-invocation-service.d.ts +16 -0
  38. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
  39. package/dist/types/src/services/index.d.ts +5 -7
  40. package/dist/types/src/services/index.d.ts.map +1 -1
  41. package/dist/types/src/services/queues.d.ts +33 -4
  42. package/dist/types/src/services/queues.d.ts.map +1 -1
  43. package/dist/types/src/services/tracing.d.ts +78 -5
  44. package/dist/types/src/services/tracing.d.ts.map +1 -1
  45. package/dist/types/src/types/Function.d.ts +52 -0
  46. package/dist/types/src/types/Function.d.ts.map +1 -0
  47. package/dist/types/src/types/Script.d.ts +21 -0
  48. package/dist/types/src/types/Script.d.ts.map +1 -0
  49. package/dist/types/src/types/Trigger.d.ts +121 -0
  50. package/dist/types/src/types/Trigger.d.ts.map +1 -0
  51. package/dist/types/src/types/TriggerEvent.d.ts +74 -0
  52. package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
  53. package/dist/types/src/types/index.d.ts +6 -0
  54. package/dist/types/src/types/index.d.ts.map +1 -0
  55. package/dist/types/src/types/url.d.ts +13 -0
  56. package/dist/types/src/types/url.d.ts.map +1 -0
  57. package/dist/types/tsconfig.tsbuildinfo +1 -1
  58. package/package.json +26 -66
  59. package/src/errors.ts +21 -0
  60. package/src/example/fib.ts +32 -0
  61. package/src/example/forex-effect.ts +40 -0
  62. package/src/example/index.ts +13 -0
  63. package/src/example/reply.ts +21 -0
  64. package/src/example/sleep.ts +24 -0
  65. package/src/index.ts +5 -8
  66. package/src/operation-compatibility.test.ts +185 -0
  67. package/src/protocol/functions-ai-http-client.ts +67 -0
  68. package/src/{executor → protocol}/index.ts +1 -1
  69. package/src/protocol/protocol.test.ts +59 -0
  70. package/src/protocol/protocol.ts +262 -0
  71. package/src/sdk.ts +289 -0
  72. package/src/services/credentials.ts +108 -4
  73. package/src/services/event-logger.ts +77 -37
  74. package/src/services/function-invocation-service.ts +37 -0
  75. package/src/services/index.ts +5 -7
  76. package/src/services/queues.ts +48 -8
  77. package/src/services/tracing.ts +151 -11
  78. package/src/types/Function.ts +82 -0
  79. package/src/types/Script.ts +34 -0
  80. package/src/types/Trigger.ts +143 -0
  81. package/src/types/TriggerEvent.ts +68 -0
  82. package/src/types/index.ts +9 -0
  83. package/src/types/url.ts +32 -0
  84. package/dist/lib/browser/bundler/index.mjs +0 -247
  85. package/dist/lib/browser/bundler/index.mjs.map +0 -7
  86. package/dist/lib/browser/chunk-54U464M4.mjs +0 -360
  87. package/dist/lib/browser/chunk-54U464M4.mjs.map +0 -7
  88. package/dist/lib/browser/edge/index.mjs +0 -69
  89. package/dist/lib/browser/edge/index.mjs.map +0 -7
  90. package/dist/lib/browser/testing/index.mjs +0 -79
  91. package/dist/lib/browser/testing/index.mjs.map +0 -7
  92. package/dist/lib/node-esm/bundler/index.mjs +0 -249
  93. package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
  94. package/dist/lib/node-esm/chunk-XDSX35BS.mjs +0 -362
  95. package/dist/lib/node-esm/chunk-XDSX35BS.mjs.map +0 -7
  96. package/dist/lib/node-esm/edge/index.mjs +0 -71
  97. package/dist/lib/node-esm/edge/index.mjs.map +0 -7
  98. package/dist/lib/node-esm/testing/index.mjs +0 -80
  99. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  100. package/dist/types/src/bundler/bundler.d.ts +0 -50
  101. package/dist/types/src/bundler/bundler.d.ts.map +0 -1
  102. package/dist/types/src/bundler/bundler.test.d.ts +0 -2
  103. package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
  104. package/dist/types/src/bundler/index.d.ts +0 -2
  105. package/dist/types/src/bundler/index.d.ts.map +0 -1
  106. package/dist/types/src/edge/functions.d.ts +0 -16
  107. package/dist/types/src/edge/functions.d.ts.map +0 -1
  108. package/dist/types/src/edge/index.d.ts +0 -2
  109. package/dist/types/src/edge/index.d.ts.map +0 -1
  110. package/dist/types/src/executor/executor.d.ts +0 -8
  111. package/dist/types/src/executor/executor.d.ts.map +0 -1
  112. package/dist/types/src/executor/index.d.ts +0 -2
  113. package/dist/types/src/executor/index.d.ts.map +0 -1
  114. package/dist/types/src/handler.d.ts +0 -61
  115. package/dist/types/src/handler.d.ts.map +0 -1
  116. package/dist/types/src/schema.d.ts +0 -38
  117. package/dist/types/src/schema.d.ts.map +0 -1
  118. package/dist/types/src/services/ai.d.ts +0 -12
  119. package/dist/types/src/services/ai.d.ts.map +0 -1
  120. package/dist/types/src/services/database.d.ts +0 -15
  121. package/dist/types/src/services/database.d.ts.map +0 -1
  122. package/dist/types/src/services/function-call-service.d.ts +0 -16
  123. package/dist/types/src/services/function-call-service.d.ts.map +0 -1
  124. package/dist/types/src/services/service-container.d.ts +0 -46
  125. package/dist/types/src/services/service-container.d.ts.map +0 -1
  126. package/dist/types/src/services/tool-resolver.d.ts +0 -14
  127. package/dist/types/src/services/tool-resolver.d.ts.map +0 -1
  128. package/dist/types/src/testing/index.d.ts +0 -2
  129. package/dist/types/src/testing/index.d.ts.map +0 -1
  130. package/dist/types/src/testing/logger.d.ts +0 -5
  131. package/dist/types/src/testing/logger.d.ts.map +0 -1
  132. package/dist/types/src/testing/services.d.ts +0 -71
  133. package/dist/types/src/testing/services.d.ts.map +0 -1
  134. package/dist/types/src/trace.d.ts +0 -124
  135. package/dist/types/src/trace.d.ts.map +0 -1
  136. package/dist/types/src/translations.d.ts +0 -12
  137. package/dist/types/src/translations.d.ts.map +0 -1
  138. package/dist/types/src/types.d.ts +0 -411
  139. package/dist/types/src/types.d.ts.map +0 -1
  140. package/dist/types/src/url.d.ts +0 -17
  141. package/dist/types/src/url.d.ts.map +0 -1
  142. package/src/bundler/bundler.test.ts +0 -59
  143. package/src/bundler/bundler.ts +0 -292
  144. package/src/bundler/index.ts +0 -5
  145. package/src/edge/functions.ts +0 -64
  146. package/src/edge/index.ts +0 -9
  147. package/src/executor/executor.ts +0 -54
  148. package/src/handler.ts +0 -113
  149. package/src/schema.ts +0 -57
  150. package/src/services/ai.ts +0 -32
  151. package/src/services/database.ts +0 -50
  152. package/src/services/function-call-service.ts +0 -64
  153. package/src/services/service-container.ts +0 -127
  154. package/src/services/tool-resolver.ts +0 -31
  155. package/src/testing/index.ts +0 -5
  156. package/src/testing/logger.ts +0 -16
  157. package/src/testing/services.ts +0 -174
  158. package/src/trace.ts +0 -180
  159. package/src/translations.ts +0 -20
  160. package/src/types.ts +0 -211
  161. package/src/url.ts +0 -52
@@ -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.object({ 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 }), tracing.getTraceContext());
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,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,12 +2,10 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- export * from './ai';
6
- export * from './database';
7
- export * from './queues';
8
- export * from './service-container';
9
5
  export * from './credentials';
10
- export * from './tracing';
6
+ export { ConfiguredCredentialsService, type ServiceCredential } from './credentials';
11
7
  export * from './event-logger';
12
- export * from './function-call-service';
13
- export * from './tool-resolver';
8
+ export { createEventLogger, createDefectLogger } from './event-logger';
9
+ export * from './function-invocation-service';
10
+ export * from './queues';
11
+ 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 { 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
- 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,60 @@ 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 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));
70
+ }
71
+
72
+ /**
73
+ * Gives access to a specific queue passed as a context.
74
+ */
75
+ export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQueueService')<
76
+ ContextQueueService,
77
+ {
78
+ readonly queue: Queue;
79
+ }
80
+ >() {
81
+ static layer = (queue: Queue) => Layer.succeed(ContextQueueService, { queue });
42
82
  }
@@ -2,26 +2,166 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context } 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 { AnyEchoObject } from '@dxos/echo-schema';
9
+ import { AgentStatus } from '@dxos/ai';
10
+ import { type DXN, Obj } from '@dxos/echo';
11
+ import { ObjectId } from '@dxos/keys';
12
+ import { Message } from '@dxos/types';
8
13
 
9
- export class TracingService extends Context.Tag('TracingService')<
14
+ import type { Trigger } from '../types';
15
+
16
+ /**
17
+ * Provides a way for compute primitives (functions, workflows, tools)
18
+ * to emit an execution trace as a series of structured ECHO objects.
19
+ */
20
+ export class TracingService extends Context.Tag('@dxos/functions/TracingService')<
10
21
  TracingService,
11
22
  {
23
+ /**
24
+ * Gets the parent message ID.
25
+ */
26
+ getTraceContext: () => TracingService.TraceContext;
27
+
12
28
  /**
13
29
  * Write an event to the tracing queue.
14
30
  * @param event - The event to write. Must be an a typed object.
15
31
  */
16
- 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>;
17
49
  }
18
50
  >() {
19
- static noop: Context.Tag.Service<TracingService> = { write: () => {} };
20
-
21
- static console: Context.Tag.Service<TracingService> = {
22
- write: (event) => {
23
- // eslint-disable-next-line no-console
24
- console.log(event);
25
- },
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(() => {}),
26
57
  };
58
+
59
+ static layerNoop: Layer.Layer<TracingService> = Layer.succeed(TracingService, TracingService.noop);
60
+
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
+ }));
89
+
90
+ /**
91
+ * Emit the current human-readable execution status.
92
+ */
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
+ });
27
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,82 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import * as Schema from 'effect/Schema';
6
+
7
+ import { Annotation, JsonSchema, Obj, 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(Type.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: 'dxos.org/type/Function',
55
+ version: '0.1.0',
56
+ }),
57
+ Annotation.LabelAnnotation.set(['name']),
58
+ SystemTypeAnnotation.set(true),
59
+ );
60
+
61
+ export interface Function extends Schema.Schema.Type<typeof Function> {}
62
+
63
+ export const make = (props: Obj.MakeProps<typeof Function>) => Obj.make(Function, props);
64
+
65
+ /**
66
+ * Copies properties from source to target.
67
+ * @param target - Target object to copy properties to.
68
+ * @param source - Source object to copy properties from.
69
+ */
70
+ export const setFrom = (target: Function, source: Function) => {
71
+ Obj.change(target, (t) => {
72
+ t.key = source.key ?? target.key;
73
+ t.name = source.name ?? target.name;
74
+ t.version = source.version;
75
+ t.description = source.description;
76
+ t.updated = source.updated;
77
+ // TODO(dmaretskyi): A workaround for an ECHO bug.
78
+ t.inputSchema = source.inputSchema ? JSON.parse(JSON.stringify(source.inputSchema)) : undefined;
79
+ t.outputSchema = source.outputSchema ? JSON.parse(JSON.stringify(source.outputSchema)) : undefined;
80
+ Obj.getMeta(t).keys = JSON.parse(JSON.stringify(Obj.getMeta(source).keys));
81
+ });
82
+ };
@@ -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: Type.Ref(Text.Text).pipe(FormInputAnnotation.set(false)),
21
+ }).pipe(
22
+ Type.object({
23
+ typename: 'dxos.org/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)) });