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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +36 -1
  3. package/dist/lib/neutral/StorageService.mjs +22 -0
  4. package/dist/lib/neutral/chunk-AKVCVI6N.mjs +25 -0
  5. package/dist/lib/neutral/chunk-AKVCVI6N.mjs.map +7 -0
  6. package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
  7. package/dist/lib/neutral/chunk-LF2ANNYW.mjs +171 -0
  8. package/dist/lib/neutral/chunk-LF2ANNYW.mjs.map +7 -0
  9. package/dist/lib/neutral/chunk-LRCABAWU.mjs +17 -0
  10. package/dist/lib/neutral/chunk-LRCABAWU.mjs.map +7 -0
  11. package/dist/lib/neutral/chunk-RCLYF66G.mjs +21 -0
  12. package/dist/lib/neutral/chunk-RCLYF66G.mjs.map +7 -0
  13. package/dist/lib/neutral/chunk-SEBWOTOU.mjs +235 -0
  14. package/dist/lib/neutral/chunk-SEBWOTOU.mjs.map +7 -0
  15. package/dist/lib/neutral/chunk-VSICPGGY.mjs +53 -0
  16. package/dist/lib/neutral/chunk-VSICPGGY.mjs.map +7 -0
  17. package/dist/lib/neutral/chunk-XEVFBI6S.mjs +62 -0
  18. package/dist/lib/neutral/chunk-XEVFBI6S.mjs.map +7 -0
  19. package/dist/lib/neutral/fib-ZSJYREBW.mjs +10 -0
  20. package/dist/lib/neutral/fib-ZSJYREBW.mjs.map +7 -0
  21. package/dist/lib/neutral/index.mjs +1225 -0
  22. package/dist/lib/neutral/index.mjs.map +7 -0
  23. package/dist/lib/neutral/meta.json +1 -0
  24. package/dist/lib/neutral/reply-YH3BVOUT.mjs +10 -0
  25. package/dist/lib/neutral/reply-YH3BVOUT.mjs.map +7 -0
  26. package/dist/lib/neutral/sleep-TSPIQ5EY.mjs +10 -0
  27. package/dist/lib/neutral/sleep-TSPIQ5EY.mjs.map +7 -0
  28. package/dist/lib/neutral/testing/index.mjs +32 -0
  29. package/dist/lib/neutral/testing/index.mjs.map +7 -0
  30. package/dist/types/src/Blueprint.d.ts +192 -0
  31. package/dist/types/src/Blueprint.d.ts.map +1 -0
  32. package/dist/types/src/Credential.d.ts +27 -0
  33. package/dist/types/src/Credential.d.ts.map +1 -0
  34. package/dist/types/src/LayerSpec.d.ts +56 -0
  35. package/dist/types/src/LayerSpec.d.ts.map +1 -0
  36. package/dist/types/src/McpServer.d.ts +27 -0
  37. package/dist/types/src/McpServer.d.ts.map +1 -0
  38. package/dist/types/src/Operation.d.ts +340 -0
  39. package/dist/types/src/Operation.d.ts.map +1 -0
  40. package/dist/types/src/OperationHandlerSet.d.ts +67 -0
  41. package/dist/types/src/OperationHandlerSet.d.ts.map +1 -0
  42. package/dist/types/src/OperationHandlerSet.test.d.ts +2 -0
  43. package/dist/types/src/OperationHandlerSet.test.d.ts.map +1 -0
  44. package/dist/types/src/OperationRegistry.d.ts +23 -0
  45. package/dist/types/src/OperationRegistry.d.ts.map +1 -0
  46. package/dist/types/src/Process.d.ts +247 -0
  47. package/dist/types/src/Process.d.ts.map +1 -0
  48. package/dist/types/src/Routine.d.ts +63 -0
  49. package/dist/types/src/Routine.d.ts.map +1 -0
  50. package/dist/types/src/Script.d.ts +22 -0
  51. package/dist/types/src/Script.d.ts.map +1 -0
  52. package/dist/types/src/ServiceResolver.d.ts +89 -0
  53. package/dist/types/src/ServiceResolver.d.ts.map +1 -0
  54. package/dist/types/src/StorageService.d.ts +58 -0
  55. package/dist/types/src/StorageService.d.ts.map +1 -0
  56. package/dist/types/src/Template.d.ts +62 -0
  57. package/dist/types/src/Template.d.ts.map +1 -0
  58. package/dist/types/src/Template.test.d.ts +2 -0
  59. package/dist/types/src/Template.test.d.ts.map +1 -0
  60. package/dist/types/src/Trace.d.ts +268 -0
  61. package/dist/types/src/Trace.d.ts.map +1 -0
  62. package/dist/types/src/Trigger.d.ts +150 -0
  63. package/dist/types/src/Trigger.d.ts.map +1 -0
  64. package/dist/types/src/TriggerEvent.d.ts +75 -0
  65. package/dist/types/src/TriggerEvent.d.ts.map +1 -0
  66. package/dist/types/src/errors.d.ts +308 -0
  67. package/dist/types/src/errors.d.ts.map +1 -0
  68. package/dist/types/src/index.d.ts +17 -5
  69. package/dist/types/src/index.d.ts.map +1 -1
  70. package/dist/types/src/testing/definitions.d.ts +11 -0
  71. package/dist/types/src/testing/definitions.d.ts.map +1 -0
  72. package/dist/types/src/testing/fib.d.ts +8 -0
  73. package/dist/types/src/testing/fib.d.ts.map +1 -0
  74. package/dist/types/src/testing/index.d.ts +6 -3
  75. package/dist/types/src/testing/index.d.ts.map +1 -1
  76. package/dist/types/src/testing/reply.d.ts +4 -0
  77. package/dist/types/src/testing/reply.d.ts.map +1 -0
  78. package/dist/types/src/testing/sleep.d.ts +6 -0
  79. package/dist/types/src/testing/sleep.d.ts.map +1 -0
  80. package/dist/types/src/umbrella.test.d.ts +2 -0
  81. package/dist/types/src/umbrella.test.d.ts.map +1 -0
  82. package/dist/types/tsconfig.tsbuildinfo +1 -1
  83. package/package.json +31 -46
  84. package/src/Blueprint.ts +303 -0
  85. package/src/Credential.ts +53 -0
  86. package/src/LayerSpec.ts +91 -0
  87. package/src/McpServer.ts +44 -0
  88. package/src/Operation.ts +593 -0
  89. package/src/OperationHandlerSet.test.ts +92 -0
  90. package/src/OperationHandlerSet.ts +174 -0
  91. package/src/OperationRegistry.ts +50 -0
  92. package/src/Process.ts +504 -0
  93. package/src/Routine.ts +75 -0
  94. package/src/Script.ts +40 -0
  95. package/src/ServiceResolver.ts +194 -0
  96. package/src/StorageService.ts +101 -0
  97. package/src/Template.test.ts +151 -0
  98. package/src/Template.ts +122 -0
  99. package/src/Trace.ts +368 -0
  100. package/src/Trigger.ts +209 -0
  101. package/src/TriggerEvent.ts +64 -0
  102. package/src/errors.ts +71 -0
  103. package/src/index.ts +18 -32
  104. package/src/testing/definitions.ts +49 -0
  105. package/src/testing/fib.ts +22 -0
  106. package/src/testing/index.ts +13 -4
  107. package/src/testing/reply.ts +18 -0
  108. package/src/testing/sleep.ts +16 -0
  109. package/src/umbrella.test.ts +51 -0
  110. package/dist/lib/browser/chunk-2PBHA7EI.mjs +0 -3078
  111. package/dist/lib/browser/chunk-2PBHA7EI.mjs.map +0 -7
  112. package/dist/lib/browser/index.mjs +0 -55
  113. package/dist/lib/browser/meta.json +0 -1
  114. package/dist/lib/browser/testing/index.mjs +0 -173
  115. package/dist/lib/browser/testing/index.mjs.map +0 -7
  116. package/dist/lib/node-esm/chunk-GTPQNWA3.mjs +0 -3080
  117. package/dist/lib/node-esm/chunk-GTPQNWA3.mjs.map +0 -7
  118. package/dist/lib/node-esm/index.mjs +0 -56
  119. package/dist/lib/node-esm/meta.json +0 -1
  120. package/dist/lib/node-esm/testing/index.mjs +0 -174
  121. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  122. package/dist/types/src/compute-graph-registry.d.ts +0 -34
  123. package/dist/types/src/compute-graph-registry.d.ts.map +0 -1
  124. package/dist/types/src/compute-graph.d.ts +0 -64
  125. package/dist/types/src/compute-graph.d.ts.map +0 -1
  126. package/dist/types/src/compute-graph.test.d.ts +0 -2
  127. package/dist/types/src/compute-graph.test.d.ts.map +0 -1
  128. package/dist/types/src/compute-node.d.ts +0 -25
  129. package/dist/types/src/compute-node.d.ts.map +0 -1
  130. package/dist/types/src/functions/async-function.d.ts +0 -66
  131. package/dist/types/src/functions/async-function.d.ts.map +0 -1
  132. package/dist/types/src/functions/edge-function.d.ts +0 -20
  133. package/dist/types/src/functions/edge-function.d.ts.map +0 -1
  134. package/dist/types/src/functions/function-defs.d.ts +0 -11
  135. package/dist/types/src/functions/function-defs.d.ts.map +0 -1
  136. package/dist/types/src/functions/index.d.ts +0 -4
  137. package/dist/types/src/functions/index.d.ts.map +0 -1
  138. package/dist/types/src/hyperformula.test.d.ts +0 -2
  139. package/dist/types/src/hyperformula.test.d.ts.map +0 -1
  140. package/dist/types/src/testing/test-builder.d.ts +0 -15
  141. package/dist/types/src/testing/test-builder.d.ts.map +0 -1
  142. package/dist/types/src/testing/test-plugin.d.ts +0 -36
  143. package/dist/types/src/testing/test-plugin.d.ts.map +0 -1
  144. package/dist/types/src/testing/util.d.ts +0 -2
  145. package/dist/types/src/testing/util.d.ts.map +0 -1
  146. package/dist/types/src/types/hyperformula.d.ts +0 -11
  147. package/dist/types/src/types/hyperformula.d.ts.map +0 -1
  148. package/dist/types/src/types/index.d.ts +0 -3
  149. package/dist/types/src/types/index.d.ts.map +0 -1
  150. package/dist/types/src/types/types.d.ts +0 -20
  151. package/dist/types/src/types/types.d.ts.map +0 -1
  152. package/dist/types/src/types/types.test.d.ts +0 -2
  153. package/dist/types/src/types/types.test.d.ts.map +0 -1
  154. package/dist/types/src/util.d.ts +0 -2
  155. package/dist/types/src/util.d.ts.map +0 -1
  156. package/dist/vendor/hyperformula.mjs +0 -36400
  157. package/src/compute-graph-registry.ts +0 -90
  158. package/src/compute-graph.test.ts +0 -88
  159. package/src/compute-graph.ts +0 -259
  160. package/src/compute-node.ts +0 -61
  161. package/src/functions/async-function.ts +0 -179
  162. package/src/functions/edge-function.ts +0 -111
  163. package/src/functions/function-defs.ts +0 -2427
  164. package/src/functions/index.ts +0 -7
  165. package/src/hyperformula.test.ts +0 -14
  166. package/src/testing/test-builder.ts +0 -54
  167. package/src/testing/test-plugin.ts +0 -100
  168. package/src/testing/util.ts +0 -9
  169. package/src/types/hyperformula.ts +0 -19
  170. package/src/types/index.ts +0 -6
  171. package/src/types/types.test.ts +0 -91
  172. package/src/types/types.ts +0 -80
  173. package/src/util.ts +0 -8
  174. /package/dist/lib/{browser/index.mjs.map → neutral/StorageService.mjs.map} +0 -0
  175. /package/dist/lib/{node-esm/index.mjs.map → neutral/chunk-J5LGTIGS.mjs.map} +0 -0
package/src/Process.ts ADDED
@@ -0,0 +1,504 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ // @import-as-namespace
6
+
7
+ import type { Atom } from '@effect-atom/atom';
8
+ import * as Context from 'effect/Context';
9
+ import * as Effect from 'effect/Effect';
10
+ import type * as Exit from 'effect/Exit';
11
+ import * as Option from 'effect/Option';
12
+ import * as Schema from 'effect/Schema';
13
+ import * as Scope from 'effect/Scope';
14
+ import type * as Types from 'effect/Types';
15
+
16
+ import { assertArgument } from '@dxos/invariant';
17
+ import { log } from '@dxos/log';
18
+ import type { ObjectId } from '@dxos/protocols';
19
+
20
+ import * as Operation from './Operation';
21
+ import * as OperationHandlerSet from './OperationHandlerSet';
22
+ import * as Trace from './Trace';
23
+
24
+ //
25
+ // Process.
26
+ //
27
+
28
+ /** Opaque process id (arbitrary string). */
29
+ export const ID = Schema.String.pipe(Schema.brand('ProcessId'));
30
+ export type ID = Schema.Schema.Type<typeof ID>;
31
+
32
+ /**
33
+ * A running process callbacks.
34
+ *
35
+ * Process lifecycle: Initial -> Running <-> Suspended -> Terminated.
36
+ *
37
+ * - onSpawn -> called once when the process is spawned.
38
+ * - onInput -> called for every input submitted to the process.
39
+ * - onAlarm -> called for processes scheduling alarms.
40
+ * - onChildEvent -> called when child process produces output or exits.
41
+ */
42
+ export interface Callbacks<I, O, R> {
43
+ /**
44
+ * Called when the process is spawned.
45
+ * Not called for processes that are resumed from a previously suspended state.
46
+ *
47
+ * @returns A signal indicating to the runtime whether the process is finished, or should be resumed later.
48
+ * @throws Throwing in the handler will terminate the process with an error.
49
+ *
50
+ * Note: This function should aim to complete in under 5 seconds to avoid exceeding limits in serverless environments.
51
+ */
52
+ onSpawn(): Effect.Effect<void, never, R | BaseServices>;
53
+
54
+ /**
55
+ * Called when there's input available to process.
56
+ *
57
+ * The function can be called in parallel.
58
+ *
59
+ * @returns A signal indicating to the runtime whether the process is finished, or should be resumed later.
60
+ * @throws Throwing in the handler will terminate the process with an error.
61
+ *
62
+ * Note: This function should aim to complete in under 5 seconds to avoid exceeding limits in serverless environments.
63
+ */
64
+ onInput(input: I): Effect.Effect<void, never, R | BaseServices>;
65
+
66
+ /**
67
+ * Called when the process's alarm is triggered.
68
+ *
69
+ * @throws Throwing in the handler will terminate the process with an error.
70
+ */
71
+ onAlarm(): Effect.Effect<void, never, R | BaseServices>;
72
+
73
+ /**
74
+ * Called when the process's child process produces output or exits.
75
+ *
76
+ * This allows the parent process to hibernate while a long-running child process is running.
77
+ */
78
+ onChildEvent(event: ChildEvent<unknown>): Effect.Effect<void, never, R | BaseServices>;
79
+ }
80
+
81
+ /**
82
+ * Services that are always available to all processes.
83
+ */
84
+ export type BaseServices = Trace.TraceService;
85
+
86
+ export type ChildEvent<T> =
87
+ | {
88
+ readonly _tag: 'output';
89
+ readonly pid: ID;
90
+ readonly data: T;
91
+ }
92
+ | {
93
+ readonly _tag: 'exited';
94
+ readonly pid: ID;
95
+ readonly result: Exit.Exit<void>;
96
+ };
97
+
98
+ export interface ProcessContext<I, O> {
99
+ readonly id: ID;
100
+
101
+ /**
102
+ * Parameters assigned during process creation.
103
+ */
104
+ readonly params: Params;
105
+
106
+ /**
107
+ * Complete this process with sucessful result.
108
+ * No additional events will be pushed to the process.
109
+ */
110
+ succeed(): void;
111
+
112
+ /**
113
+ * Complete this process with an error.
114
+ * No additional events will be pushed to the process.
115
+ */
116
+ fail(error: Error): void;
117
+
118
+ /**
119
+ * Submit output of the process.
120
+ */
121
+ submitOutput(output: O): void;
122
+
123
+ /**
124
+ * Set an alarm for the process to be woken up later.
125
+ *
126
+ * @param timeout - Optional timeout in milliseconds. If not provided, the process is woken up as soon as possible.
127
+ */
128
+ setAlarm(timeout?: number): void;
129
+ }
130
+
131
+ /**
132
+ * Generic parameters for a all processes.
133
+ */
134
+ export interface Params {
135
+ /**
136
+ * Process name for debugging purposes.
137
+ */
138
+ readonly name: string | null;
139
+
140
+ /**
141
+ * Target object that this process is assigned to.
142
+ */
143
+ readonly target: ObjectId | null;
144
+ }
145
+
146
+ //
147
+ // Executable.
148
+ //
149
+
150
+ export const ProcessTypeId = '~@dxos/functions/Process' as const;
151
+ export type ProcessTypeId = typeof ProcessTypeId;
152
+
153
+ /**
154
+ * A process (factory).
155
+ * Can be instantiated mutlitple times to produce new runtime instance with separate state and callbacks.
156
+ * `create` is used to instantiate a new process.
157
+ * Can store runtime state in scope of `create` function.
158
+ */
159
+ export interface Process<I, O, R> extends Process.Variance<I, O, R> {
160
+ /**
161
+ * Unique identifier for the executable in the reverse DNS format.
162
+ */
163
+ readonly key: string;
164
+
165
+ /**
166
+ * Human-readable label from {@link MakeExecutableOpts.name} when provided.
167
+ */
168
+ readonly name?: string;
169
+
170
+ readonly services: readonly Context.Tag<any, any>[];
171
+
172
+ /**
173
+ * Create a new instance of the process.
174
+ */
175
+ create(ctx: ProcessContext<I, O>): Effect.Effect<Callbacks<I, O, R>, never, R | BaseServices | Scope.Scope>;
176
+ }
177
+
178
+ export const isProcess = (executable: unknown): executable is Process.Any =>
179
+ typeof executable === 'object' && executable !== null && ProcessTypeId in executable;
180
+
181
+ export namespace Process {
182
+ export interface Variance<I, O, R> {
183
+ readonly [ProcessTypeId]: {
184
+ readonly _Input: Types.Contravariant<I>;
185
+ readonly _Output: Types.Covariant<O>;
186
+ readonly _Requirements: Types.Covariant<R>;
187
+ };
188
+ }
189
+
190
+ export type Any = Process<any, any, never>;
191
+ }
192
+
193
+ export interface MakeProcessOpts {
194
+ /**
195
+ * Unique identifier for the process in the reverse DNS format.
196
+ */
197
+ readonly key: string;
198
+
199
+ readonly input: Schema.Schema.AnyNoContext;
200
+ readonly output: Schema.Schema.AnyNoContext;
201
+ readonly services: readonly Context.Tag<any, any>[];
202
+ }
203
+
204
+ export const make = <const Opts extends Types.NoExcessProperties<MakeProcessOpts, Opts>>(
205
+ opts: Opts,
206
+ create: (
207
+ ctx: ProcessContext<Schema.Schema.Type<Opts['input']>, Schema.Schema.Type<Opts['output']>>,
208
+ ) => Effect.Effect<
209
+ Partial<
210
+ Callbacks<
211
+ Schema.Schema.Type<Opts['input']>,
212
+ Schema.Schema.Type<Opts['output']>,
213
+ Context.Tag.Identifier<NonNullable<Opts['services']>[number]>
214
+ >
215
+ >,
216
+ never,
217
+ Context.Tag.Identifier<NonNullable<Opts['services']>[number]> | BaseServices | Scope.Scope
218
+ >,
219
+ ): Process<
220
+ Schema.Schema.Type<Opts['input']>,
221
+ Schema.Schema.Type<Opts['output']>,
222
+ Context.Tag.Identifier<NonNullable<Opts['services']>[number]>
223
+ > => {
224
+ assertArgument(/^[a-z0-9]([a-z0-9.\-/]*[a-z0-9])?$/i.test(opts.key), 'key', 'Invalid key');
225
+ return {
226
+ [ProcessTypeId]: {} as any,
227
+ ...opts,
228
+ create: (ctx) =>
229
+ create(ctx).pipe(
230
+ Effect.map((partial) => ({
231
+ onSpawn: () => Effect.void,
232
+ onInput: () => Effect.void,
233
+ onAlarm: () => Effect.void,
234
+ onChildEvent: () => Effect.void,
235
+ ...partial,
236
+ })),
237
+ ),
238
+ };
239
+ };
240
+
241
+ export const fromOperation = <const Op extends Operation.Definition.Any>(
242
+ op: Op,
243
+ handler: OperationHandlerSet.OperationHandlerSet,
244
+ ): Process<Operation.Definition.Input<Op>, Operation.Definition.Output<Op>, Operation.Definition.Services<Op>> =>
245
+ make(
246
+ {
247
+ key: op.meta.key,
248
+ input: op.input,
249
+ output: op.output,
250
+ services: op.services,
251
+ },
252
+ (ctx) =>
253
+ Effect.gen(function* () {
254
+ const semaphore = yield* Effect.makeSemaphore(1);
255
+
256
+ return {
257
+ onInput: (input: Operation.Definition.Input<Op>) =>
258
+ Effect.gen(function* () {
259
+ // Emit operation start event.
260
+ log('operation process invoking', { key: op.meta.key, name: op.meta.name });
261
+ yield* Trace.write(Trace.OperationStart, {
262
+ key: op.meta.key,
263
+ name: op.meta.name,
264
+ });
265
+ // Emit ephemeral operation input event for live subscribers
266
+ // (history tracker, devtools) without persisting raw input.
267
+ yield* Trace.write(Trace.OperationInput, {
268
+ key: op.meta.key,
269
+ name: op.meta.name,
270
+ input,
271
+ });
272
+
273
+ const opHandler = yield* OperationHandlerSet.getHandler(handler, op).pipe(Effect.orDie);
274
+ const output = yield* opHandler
275
+ .handler(input)
276
+ .pipe(Effect.orDie, Effect.withSpan(op.meta.key)) as Effect.Effect<
277
+ Operation.Definition.Output<Op>,
278
+ never,
279
+ never
280
+ >;
281
+
282
+ ctx.submitOutput(output);
283
+ ctx.succeed();
284
+
285
+ // Emit ephemeral operation output event before the persisted
286
+ // end event so subscribers see output + completion together.
287
+ yield* Trace.write(Trace.OperationOutput, {
288
+ key: op.meta.key,
289
+ name: op.meta.name,
290
+ output,
291
+ });
292
+ // Emit operation end event with success after side-effects complete.
293
+ yield* Trace.write(Trace.OperationEnd, {
294
+ key: op.meta.key,
295
+ name: op.meta.name,
296
+ outcome: 'success',
297
+ });
298
+ }).pipe(
299
+ Effect.catchAllDefect((defect) =>
300
+ Effect.gen(function* () {
301
+ // Emit operation end event with failure.
302
+ const errorMessage = defect instanceof Error ? defect.message : String(defect);
303
+ yield* Trace.write(Trace.OperationEnd, {
304
+ key: op.meta.key,
305
+ name: op.meta.name,
306
+ outcome: 'failure',
307
+ error: errorMessage,
308
+ });
309
+ return yield* Effect.die(defect);
310
+ }),
311
+ ),
312
+ semaphore.withPermits(1),
313
+ ),
314
+ };
315
+ }),
316
+ );
317
+
318
+ /**
319
+ * Runtime state of a process.
320
+ */
321
+ export enum State {
322
+ // Process is actively running.
323
+ RUNNING = 'RUNNING',
324
+
325
+ // Process is waiting for a child process to complete or an alarm to trigger.
326
+ HYBERNATING = 'HYBERNATING',
327
+
328
+ // Process is waiting for input. It will only resume when input is submitted.
329
+ IDLE = 'IDLE',
330
+
331
+ // Process is terminating and will transition to TERMINATED state.
332
+ // TODO(dmaretskyi): Consider removing.
333
+ TERMINATING = 'TERMINATING',
334
+
335
+ // Process has been externally terminated.
336
+ TERMINATED = 'TERMINATED',
337
+
338
+ // Process has completed successfully.
339
+ SUCCEEDED = 'SUCCEEDED',
340
+
341
+ // Process has failed.
342
+ FAILED = 'FAILED',
343
+ }
344
+
345
+ /**
346
+ * Read-only view of a process tree
347
+ */
348
+ export interface Monitor {
349
+ /**
350
+ * Get the current state of the process tree.
351
+ */
352
+ processTree: Effect.Effect<readonly Info[]>;
353
+
354
+ /**
355
+ * Atom for the process tree.
356
+ */
357
+ processTreeAtom: Atom.Atom<readonly Info[]>;
358
+ }
359
+
360
+ export class ProcessMonitorService extends Context.Tag('@dxos/functions/ProcessMonitorService')<
361
+ ProcessMonitorService,
362
+ Monitor
363
+ >() {}
364
+
365
+ export interface Info {
366
+ readonly pid: ID;
367
+ readonly parentPid: ID | null;
368
+
369
+ /**
370
+ * Key of the process.
371
+ *
372
+ * NOTE: There might be multiple running processes with the same key.
373
+ */
374
+ readonly key: string;
375
+
376
+ /**
377
+ * Parameters of the process.
378
+ */
379
+ readonly params: Params;
380
+
381
+ /**
382
+ * State of the process.
383
+ */
384
+ readonly state: State;
385
+
386
+ /**
387
+ * Error of the process.
388
+ * Only for process in FAILED state.
389
+ */
390
+ readonly error: string | null;
391
+
392
+ /**
393
+ * UNIX timestamp in milliseconds.
394
+ */
395
+ readonly startedAt: number;
396
+
397
+ /**
398
+ * UNIX timestamp in milliseconds.
399
+ */
400
+ readonly completedAt: Option.Option<number>;
401
+
402
+ readonly metrics: {
403
+ /**
404
+ * Total wall time of all handler invocations of the process in milliseconds.
405
+ */
406
+ readonly wallTime: number;
407
+
408
+ /**
409
+ * Total number of inputs submitted to the process.
410
+ */
411
+ readonly inputCount: number;
412
+
413
+ /**
414
+ * Total number of outputs submitted to the process.
415
+ */
416
+ readonly outputCount: number;
417
+ };
418
+ }
419
+
420
+ /**
421
+ * New process is spawned.
422
+ */
423
+ export const SpawnedEvent = Trace.EventType('process.spawned', {
424
+ schema: Schema.Void,
425
+ isEphemeral: false,
426
+ });
427
+
428
+ /**
429
+ * Process has reached a terminal state.
430
+ */
431
+ export const ExitedEvent = Trace.EventType('process.exited', {
432
+ schema: Schema.Struct({
433
+ outcome: Schema.Literal('succeeded', 'failed', 'terminated'),
434
+ }),
435
+ isEphemeral: false,
436
+ });
437
+
438
+ /**
439
+ * Renders spawned processes as a forest: top-level rows use "- ", nested rows use ├── / └── / │.
440
+ */
441
+ export const prettyProcessTree = (tree: readonly Info[]): string => {
442
+ if (tree.length === 0) {
443
+ return '';
444
+ }
445
+
446
+ const pidSet = new Set(tree.map((node) => node.pid));
447
+ const childrenByParent = new Map<string, Info[]>();
448
+ const roots: Info[] = [];
449
+
450
+ for (const node of tree) {
451
+ const parent = node.parentPid;
452
+ if (parent === null || !pidSet.has(parent)) {
453
+ roots.push(node);
454
+ continue;
455
+ }
456
+ const key = String(parent);
457
+ const siblings = childrenByParent.get(key) ?? [];
458
+ siblings.push(node);
459
+ childrenByParent.set(key, siblings);
460
+ }
461
+
462
+ const byPid = (a: Info, b: Info) => String(a.pid).localeCompare(String(b.pid));
463
+ roots.sort(byPid);
464
+ for (const siblings of childrenByParent.values()) {
465
+ siblings.sort(byPid);
466
+ }
467
+
468
+ const formatLabel = (node: Info): string => {
469
+ const idShort = String(node.pid).slice(0, 6);
470
+ const parts = [idShort, node.state];
471
+ if (node.params.name != null && node.params.name !== '') {
472
+ parts.push(node.params.name);
473
+ }
474
+ if (node.error != null) {
475
+ parts.push(`(${node.error})`);
476
+ }
477
+ const { inputCount, outputCount, wallTime } = node.metrics;
478
+ parts.push(`[in:${inputCount} out:${outputCount} wall:${Math.round(wallTime)}ms]`);
479
+ return parts.join(' ');
480
+ };
481
+
482
+ const lines: string[] = [];
483
+
484
+ const walk = (node: Info, prefix: string, isLast: boolean, isRoot: boolean): void => {
485
+ if (isRoot) {
486
+ lines.push(`- ${formatLabel(node)}`);
487
+ } else {
488
+ const branch = isLast ? '└── ' : '├── ';
489
+ lines.push(`${prefix}${branch}${formatLabel(node)}`);
490
+ }
491
+
492
+ const children = childrenByParent.get(String(node.pid)) ?? [];
493
+ const nextPrefix = isRoot ? ' ' : `${prefix}${isLast ? ' ' : '│ '}`;
494
+ children.forEach((child, index) => {
495
+ walk(child, nextPrefix, index === children.length - 1, false);
496
+ });
497
+ };
498
+
499
+ for (const root of roots) {
500
+ walk(root, '', true, true);
501
+ }
502
+
503
+ return lines.join('\n');
504
+ };
package/src/Routine.ts ADDED
@@ -0,0 +1,75 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ // @import-as-namespace
6
+
7
+ import * as Schema from 'effect/Schema';
8
+
9
+ import { Annotation, JsonSchema, Obj, Ref, Type } from '@dxos/echo';
10
+
11
+ import * as Blueprint from './Blueprint';
12
+ import * as Template from './Template';
13
+
14
+ /**
15
+ * Prompt-based operation.
16
+ * May reference blueprints and additional context.
17
+ */
18
+ export const Routine = Schema.Struct({
19
+ name: Schema.optional(Schema.String),
20
+ description: Schema.optional(Schema.String),
21
+ input: JsonSchema.JsonSchema.pipe(Annotation.FormInputAnnotation.set(false)).annotations({
22
+ description: 'Input schema',
23
+ }),
24
+ output: JsonSchema.JsonSchema.pipe(Annotation.FormInputAnnotation.set(false)).annotations({
25
+ description: 'Output schema',
26
+ }),
27
+ // TODO(burdon): Form editor.
28
+ instructions: Template.Template.pipe(Annotation.FormInputAnnotation.set(false)).annotations({
29
+ description: 'Agent instructions',
30
+ }),
31
+ blueprints: Schema.Array(Ref.Ref(Blueprint.Blueprint)),
32
+ // TODO(burdon): Change to map?
33
+ context: Schema.Array(Schema.Any).pipe(Annotation.FormInputAnnotation.set(false)),
34
+ }).pipe(
35
+ Type.object({
36
+ typename: 'org.dxos.type.routine',
37
+ version: '0.1.0',
38
+ }),
39
+ Annotation.LabelAnnotation.set(['name']),
40
+ Annotation.IconAnnotation.set({
41
+ icon: 'ph--scroll--regular',
42
+ hue: 'sky',
43
+ }),
44
+ );
45
+
46
+ export interface Routine extends Schema.Schema.Type<typeof Routine> {}
47
+
48
+ export type MakeOptions = {
49
+ name?: string;
50
+ description?: string;
51
+ input?: Schema.Schema.AnyNoContext;
52
+ output?: Schema.Schema.AnyNoContext;
53
+ instructions?: string;
54
+ blueprints?: Ref.Ref<Blueprint.Blueprint>[];
55
+ context?: any[];
56
+ };
57
+
58
+ export const make = ({
59
+ name,
60
+ description,
61
+ input,
62
+ output,
63
+ instructions,
64
+ blueprints = [],
65
+ context = [],
66
+ }: MakeOptions): Routine =>
67
+ Obj.make(Routine, {
68
+ name,
69
+ description,
70
+ input: JsonSchema.toJsonSchema(input ?? Schema.Void),
71
+ output: JsonSchema.toJsonSchema(output ?? Schema.Void),
72
+ instructions: Template.make({ source: instructions }),
73
+ blueprints,
74
+ context,
75
+ });
package/src/Script.ts ADDED
@@ -0,0 +1,40 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ // @import-as-namespace
6
+
7
+ import * as Schema from 'effect/Schema';
8
+
9
+ import { Annotation, Obj, Ref, Type } from '@dxos/echo';
10
+ import { FormInputAnnotation } from '@dxos/echo/internal';
11
+ import { Text } from '@dxos/schema';
12
+
13
+ /**
14
+ * Source script.
15
+ */
16
+ export const Script = Schema.Struct({
17
+ name: Schema.String.pipe(Schema.optional),
18
+ description: Schema.String.pipe(Schema.optional),
19
+ // TODO(burdon): Change to hash of deployed content.
20
+ // Whether source has changed since last deploy.
21
+ changed: Schema.Boolean.pipe(FormInputAnnotation.set(false), Schema.optional),
22
+ source: Ref.Ref(Text.Text).pipe(FormInputAnnotation.set(false)),
23
+ }).pipe(
24
+ Type.object({
25
+ typename: 'org.dxos.type.script',
26
+ version: '0.1.0',
27
+ }),
28
+ Annotation.LabelAnnotation.set(['name']),
29
+ Annotation.IconAnnotation.set({
30
+ icon: 'ph--code--regular',
31
+ hue: 'sky',
32
+ }),
33
+ );
34
+
35
+ export interface Script extends Schema.Schema.Type<typeof Script> {}
36
+
37
+ type Props = Omit<Obj.MakeProps<typeof Script>, 'source'> & { source?: string };
38
+
39
+ export const make = ({ source = '', ...props }: Props = {}): Script =>
40
+ Obj.make(Script, { ...props, source: Ref.make(Text.make({ content: source })) });