@dxos/functions 0.8.4-main.bc674ce → 0.8.4-main.bcb3aa67d6
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.
- package/README.md +4 -6
- package/dist/lib/neutral/Trace.mjs +34 -0
- package/dist/lib/neutral/Trace.mjs.map +7 -0
- package/dist/lib/neutral/chunk-BHLSCAA2.mjs +123 -0
- package/dist/lib/neutral/chunk-BHLSCAA2.mjs.map +7 -0
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/neutral/chunk-Z2XDJJVH.mjs +49 -0
- package/dist/lib/neutral/chunk-Z2XDJJVH.mjs.map +7 -0
- package/dist/lib/neutral/fib-S6PPI4UW.mjs +23 -0
- package/dist/lib/neutral/fib-S6PPI4UW.mjs.map +7 -0
- package/dist/lib/{browser → neutral}/index.mjs +649 -633
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/neutral/reply-TOHXEG7V.mjs +19 -0
- package/dist/lib/neutral/reply-TOHXEG7V.mjs.map +7 -0
- package/dist/lib/neutral/sleep-QPSZDPEH.mjs +15 -0
- package/dist/lib/neutral/sleep-QPSZDPEH.mjs.map +7 -0
- package/dist/types/src/Trace.d.ts +135 -0
- package/dist/types/src/Trace.d.ts.map +1 -0
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/example/definitions.d.ts +11 -0
- package/dist/types/src/example/definitions.d.ts.map +1 -0
- package/dist/types/src/example/fib.d.ts +3 -2
- package/dist/types/src/example/fib.d.ts.map +1 -1
- package/dist/types/src/example/index.d.ts +3 -11
- package/dist/types/src/example/index.d.ts.map +1 -1
- package/dist/types/src/example/reply.d.ts +2 -1
- package/dist/types/src/example/reply.d.ts.map +1 -1
- package/dist/types/src/example/sleep.d.ts +3 -2
- package/dist/types/src/example/sleep.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +4 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/process/Process.d.ts +247 -0
- package/dist/types/src/process/Process.d.ts.map +1 -0
- package/dist/types/src/process/ServiceResolver.d.ts +74 -0
- package/dist/types/src/process/ServiceResolver.d.ts.map +1 -0
- package/dist/types/src/process/StorageService.d.ts +58 -0
- package/dist/types/src/process/StorageService.d.ts.map +1 -0
- package/dist/types/src/protocol/protocol.d.ts +2 -2
- package/dist/types/src/protocol/protocol.d.ts.map +1 -1
- package/dist/types/src/sdk.d.ts +4 -104
- package/dist/types/src/sdk.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +4 -4
- package/dist/types/src/services/function-invocation-service.d.ts +6 -5
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +4 -2
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +25 -2
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/types/Script.d.ts +4 -3
- package/dist/types/src/types/Script.d.ts.map +1 -1
- package/dist/types/src/types/Trigger.d.ts +8 -9
- package/dist/types/src/types/Trigger.d.ts.map +1 -1
- package/dist/types/src/types/TriggerEvent.d.ts +3 -2
- package/dist/types/src/types/TriggerEvent.d.ts.map +1 -1
- package/dist/types/src/types/index.d.ts +0 -1
- package/dist/types/src/types/index.d.ts.map +1 -1
- package/dist/types/src/types/url.d.ts +2 -2
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +25 -20
- package/src/Trace.ts +162 -0
- package/src/errors.ts +1 -1
- package/src/example/definitions.ts +48 -0
- package/src/example/fib.ts +14 -24
- package/src/example/forex-effect.ts +1 -1
- package/src/example/index.ts +7 -8
- package/src/example/reply.ts +10 -13
- package/src/example/sleep.ts +8 -17
- package/src/index.ts +4 -0
- package/src/process/Process.ts +457 -0
- package/src/process/ServiceResolver.ts +173 -0
- package/src/process/StorageService.ts +99 -0
- package/src/protocol/protocol.ts +33 -27
- package/src/sdk.ts +6 -256
- package/src/services/event-logger.ts +1 -1
- package/src/services/function-invocation-service.ts +6 -5
- package/src/services/queues.ts +10 -2
- package/src/services/tracing.ts +35 -2
- package/src/types/Script.ts +7 -3
- package/src/types/Trigger.ts +17 -6
- package/src/types/TriggerEvent.ts +2 -2
- package/src/types/index.ts +0 -1
- package/src/types/url.ts +2 -2
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/node-esm/index.mjs +0 -1230
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/types/src/operation-compatibility.test.d.ts +0 -2
- package/dist/types/src/operation-compatibility.test.d.ts.map +0 -1
- package/dist/types/src/types/Function.d.ts +0 -52
- package/dist/types/src/types/Function.d.ts.map +0 -1
- package/src/operation-compatibility.test.ts +0 -185
- package/src/types/Function.ts +0 -82
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
// @import-as-namespace
|
|
6
|
+
|
|
7
|
+
import * as Scope from 'effect/Scope';
|
|
8
|
+
import * as Effect from 'effect/Effect';
|
|
9
|
+
import * as Schema from 'effect/Schema';
|
|
10
|
+
import type * as Exit from 'effect/Exit';
|
|
11
|
+
import * as Context from 'effect/Context';
|
|
12
|
+
import type * as Types from 'effect/Types';
|
|
13
|
+
|
|
14
|
+
import { Operation, OperationHandlerSet } from '@dxos/operation';
|
|
15
|
+
import type { TracingService } from '../services/tracing';
|
|
16
|
+
import * as Option from 'effect/Option';
|
|
17
|
+
import type { Atom } from '@effect-atom/atom';
|
|
18
|
+
import type { ObjectId } from '@dxos/protocols';
|
|
19
|
+
import { assertArgument } from '@dxos/invariant';
|
|
20
|
+
import * as Trace from '../Trace';
|
|
21
|
+
|
|
22
|
+
//
|
|
23
|
+
// Process.
|
|
24
|
+
//
|
|
25
|
+
|
|
26
|
+
/** Opaque process id (arbitrary string). */
|
|
27
|
+
export const ID = Schema.String.pipe(Schema.brand('ProcessId'));
|
|
28
|
+
export type ID = Schema.Schema.Type<typeof ID>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A running process callbacks.
|
|
32
|
+
*
|
|
33
|
+
* Process lifecycle: Initial -> Running <-> Suspended -> Terminated.
|
|
34
|
+
*
|
|
35
|
+
* - onSpawn -> called once when the process is spawned.
|
|
36
|
+
* - onInput -> called for every input submitted to the process.
|
|
37
|
+
* - onAlarm -> called for processes scheduling alarms.
|
|
38
|
+
* - onChildEvent -> called when child process produces output or exits.
|
|
39
|
+
*/
|
|
40
|
+
export interface Callbacks<I, O, R> {
|
|
41
|
+
/**
|
|
42
|
+
* Called when the process is spawned.
|
|
43
|
+
* Not called for processes that are resumed from a previously suspended state.
|
|
44
|
+
*
|
|
45
|
+
* @returns A signal indicating to the runtime whether the process is finished, or should be resumed later.
|
|
46
|
+
* @throws Throwing in the handler will terminate the process with an error.
|
|
47
|
+
*
|
|
48
|
+
* Note: This function should aim to complete in under 5 seconds to avoid exceeding limits in serverless environments.
|
|
49
|
+
*/
|
|
50
|
+
onSpawn(): Effect.Effect<void, never, R | BaseServices>;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Called when there's input available to process.
|
|
54
|
+
*
|
|
55
|
+
* The function can be called in parallel.
|
|
56
|
+
*
|
|
57
|
+
* @returns A signal indicating to the runtime whether the process is finished, or should be resumed later.
|
|
58
|
+
* @throws Throwing in the handler will terminate the process with an error.
|
|
59
|
+
*
|
|
60
|
+
* Note: This function should aim to complete in under 5 seconds to avoid exceeding limits in serverless environments.
|
|
61
|
+
*/
|
|
62
|
+
onInput(input: I): Effect.Effect<void, never, R | BaseServices>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Called when the process's alarm is triggered.
|
|
66
|
+
*
|
|
67
|
+
* @throws Throwing in the handler will terminate the process with an error.
|
|
68
|
+
*/
|
|
69
|
+
onAlarm(): Effect.Effect<void, never, R | BaseServices>;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Called when the process's child process produces output or exits.
|
|
73
|
+
*
|
|
74
|
+
* This allows the parent process to hibernate while a long-running child process is running.
|
|
75
|
+
*/
|
|
76
|
+
onChildEvent(event: ChildEvent<unknown>): Effect.Effect<void, never, R | BaseServices>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Services that are always available to all processes.
|
|
81
|
+
*/
|
|
82
|
+
export type BaseServices = TracingService | Trace.TraceService;
|
|
83
|
+
|
|
84
|
+
export type ChildEvent<T> =
|
|
85
|
+
| {
|
|
86
|
+
readonly _tag: 'output';
|
|
87
|
+
readonly pid: ID;
|
|
88
|
+
readonly data: T;
|
|
89
|
+
}
|
|
90
|
+
| {
|
|
91
|
+
readonly _tag: 'exited';
|
|
92
|
+
readonly pid: ID;
|
|
93
|
+
readonly result: Exit.Exit<void>;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export interface ProcessContext<I, O> {
|
|
97
|
+
readonly id: ID;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Parameters assigned during process creation.
|
|
101
|
+
*/
|
|
102
|
+
readonly params: Params;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Complete this process with sucessful result.
|
|
106
|
+
* No additional events will be pushed to the process.
|
|
107
|
+
*/
|
|
108
|
+
succeed(): void;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Complete this process with an error.
|
|
112
|
+
* No additional events will be pushed to the process.
|
|
113
|
+
*/
|
|
114
|
+
fail(error: Error): void;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Submit output of the process.
|
|
118
|
+
*/
|
|
119
|
+
submitOutput(output: O): void;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Set an alarm for the process to be woken up later.
|
|
123
|
+
*
|
|
124
|
+
* @param timeout - Optional timeout in milliseconds. If not provided, the process is woken up as soon as possible.
|
|
125
|
+
*/
|
|
126
|
+
setAlarm(timeout?: number): void;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Generic parameters for a all processes.
|
|
131
|
+
*/
|
|
132
|
+
export interface Params {
|
|
133
|
+
/**
|
|
134
|
+
* Process name for debugging purposes.
|
|
135
|
+
*/
|
|
136
|
+
readonly name: string | null;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Target object that this process is assigned to.
|
|
140
|
+
*/
|
|
141
|
+
readonly target: ObjectId | null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
//
|
|
145
|
+
// Executable.
|
|
146
|
+
//
|
|
147
|
+
|
|
148
|
+
export const ProcessTypeId = '~@dxos/functions/Process' as const;
|
|
149
|
+
export type ProcessTypeId = typeof ProcessTypeId;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* A process (factory).
|
|
153
|
+
* Can be instantiated mutlitple times to produce new runtime instance with separate state and callbacks.
|
|
154
|
+
* `create` is used to instantiate a new process.
|
|
155
|
+
* Can store runtime state in scope of `create` function.
|
|
156
|
+
*/
|
|
157
|
+
export interface Process<I, O, R> extends Process.Variance<I, O, R> {
|
|
158
|
+
/**
|
|
159
|
+
* Unique identifier for the executable in the reverse DNS format.
|
|
160
|
+
*/
|
|
161
|
+
readonly key: string;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Human-readable label from {@link MakeExecutableOpts.name} when provided.
|
|
165
|
+
*/
|
|
166
|
+
readonly name?: string;
|
|
167
|
+
|
|
168
|
+
readonly services: readonly Context.Tag<any, any>[];
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Create a new instance of the process.
|
|
172
|
+
*/
|
|
173
|
+
create(ctx: ProcessContext<I, O>): Effect.Effect<Callbacks<I, O, R>, never, R | BaseServices | Scope.Scope>;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export const isProcess = (executable: unknown): executable is Process.Any =>
|
|
177
|
+
typeof executable === 'object' && executable !== null && ProcessTypeId in executable;
|
|
178
|
+
|
|
179
|
+
export namespace Process {
|
|
180
|
+
export interface Variance<I, O, R> {
|
|
181
|
+
readonly [ProcessTypeId]: {
|
|
182
|
+
readonly _Input: Types.Contravariant<I>;
|
|
183
|
+
readonly _Output: Types.Covariant<O>;
|
|
184
|
+
readonly _Requirements: Types.Covariant<R>;
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export type Any = Process<any, any, never>;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export interface MakeProcessOpts {
|
|
192
|
+
/**
|
|
193
|
+
* Unique identifier for the process in the reverse DNS format.
|
|
194
|
+
*/
|
|
195
|
+
readonly key: string;
|
|
196
|
+
|
|
197
|
+
readonly input: Schema.Schema.AnyNoContext;
|
|
198
|
+
readonly output: Schema.Schema.AnyNoContext;
|
|
199
|
+
readonly services: readonly Context.Tag<any, any>[];
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export const make = <const Opts extends Types.NoExcessProperties<MakeProcessOpts, Opts>>(
|
|
203
|
+
opts: Opts,
|
|
204
|
+
create: (
|
|
205
|
+
ctx: ProcessContext<Schema.Schema.Type<Opts['input']>, Schema.Schema.Type<Opts['output']>>,
|
|
206
|
+
) => Effect.Effect<
|
|
207
|
+
Partial<
|
|
208
|
+
Callbacks<
|
|
209
|
+
Schema.Schema.Type<Opts['input']>,
|
|
210
|
+
Schema.Schema.Type<Opts['output']>,
|
|
211
|
+
Context.Tag.Identifier<NonNullable<Opts['services']>[number]>
|
|
212
|
+
>
|
|
213
|
+
>,
|
|
214
|
+
never,
|
|
215
|
+
Context.Tag.Identifier<NonNullable<Opts['services']>[number]> | BaseServices | Scope.Scope
|
|
216
|
+
>,
|
|
217
|
+
): Process<
|
|
218
|
+
Schema.Schema.Type<Opts['input']>,
|
|
219
|
+
Schema.Schema.Type<Opts['output']>,
|
|
220
|
+
Context.Tag.Identifier<NonNullable<Opts['services']>[number]>
|
|
221
|
+
> => {
|
|
222
|
+
assertArgument(/^[a-z0-9]([a-z0-9.\-/]*[a-z0-9])?$/i.test(opts.key), 'key', 'Invalid key');
|
|
223
|
+
return {
|
|
224
|
+
[ProcessTypeId]: {} as any,
|
|
225
|
+
...opts,
|
|
226
|
+
create: (ctx) =>
|
|
227
|
+
create(ctx).pipe(
|
|
228
|
+
Effect.map((partial) => ({
|
|
229
|
+
onSpawn: () => Effect.void,
|
|
230
|
+
onInput: () => Effect.void,
|
|
231
|
+
onAlarm: () => Effect.void,
|
|
232
|
+
onChildEvent: () => Effect.void,
|
|
233
|
+
...partial,
|
|
234
|
+
})),
|
|
235
|
+
),
|
|
236
|
+
};
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
export const fromOperation = <const Op extends Operation.Definition.Any>(
|
|
240
|
+
op: Op,
|
|
241
|
+
handler: OperationHandlerSet.OperationHandlerSet,
|
|
242
|
+
): Process<Operation.Definition.Input<Op>, Operation.Definition.Output<Op>, Operation.Definition.Services<Op>> =>
|
|
243
|
+
make(
|
|
244
|
+
{
|
|
245
|
+
key: op.meta.key,
|
|
246
|
+
input: op.input,
|
|
247
|
+
output: op.output,
|
|
248
|
+
services: op.services,
|
|
249
|
+
},
|
|
250
|
+
(ctx) =>
|
|
251
|
+
Effect.gen(function* () {
|
|
252
|
+
const semaphore = yield* Effect.makeSemaphore(1);
|
|
253
|
+
|
|
254
|
+
return {
|
|
255
|
+
onInput: (input: Operation.Definition.Input<Op>) =>
|
|
256
|
+
Effect.gen(function* () {
|
|
257
|
+
const opHandler = yield* OperationHandlerSet.getHandler(handler, op).pipe(Effect.orDie);
|
|
258
|
+
const output = yield* opHandler.handler(input).pipe(Effect.orDie) as Effect.Effect<
|
|
259
|
+
Operation.Definition.Output<Op>,
|
|
260
|
+
never,
|
|
261
|
+
never
|
|
262
|
+
>;
|
|
263
|
+
|
|
264
|
+
ctx.submitOutput(output);
|
|
265
|
+
ctx.succeed();
|
|
266
|
+
}).pipe(semaphore.withPermits(1)),
|
|
267
|
+
};
|
|
268
|
+
}),
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Runtime state of a process.
|
|
273
|
+
*/
|
|
274
|
+
export enum State {
|
|
275
|
+
// Process is actively running.
|
|
276
|
+
RUNNING = 'RUNNING',
|
|
277
|
+
|
|
278
|
+
// Process is waiting for a child process to complete or an alarm to trigger.
|
|
279
|
+
HYBERNATING = 'HYBERNATING',
|
|
280
|
+
|
|
281
|
+
// Process is waiting for input. It will only resume when input is submitted.
|
|
282
|
+
IDLE = 'IDLE',
|
|
283
|
+
|
|
284
|
+
// Process is terminating and will transition to TERMINATED state.
|
|
285
|
+
// TODO(dmaretskyi): Consider removing.
|
|
286
|
+
TERMINATING = 'TERMINATING',
|
|
287
|
+
|
|
288
|
+
// Process has been externally terminated.
|
|
289
|
+
TERMINATED = 'TERMINATED',
|
|
290
|
+
|
|
291
|
+
// Process has completed successfully.
|
|
292
|
+
SUCCEEDED = 'SUCCEEDED',
|
|
293
|
+
|
|
294
|
+
// Process has failed.
|
|
295
|
+
FAILED = 'FAILED',
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Read-only view of a process tree
|
|
300
|
+
*/
|
|
301
|
+
export interface Monitor {
|
|
302
|
+
/**
|
|
303
|
+
* Get the current state of the process tree.
|
|
304
|
+
*/
|
|
305
|
+
processTree: Effect.Effect<readonly Info[]>;
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Atom for the process tree.
|
|
309
|
+
*/
|
|
310
|
+
processTreeAtom: Atom.Atom<readonly Info[]>;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export class ProcessMonitorService extends Context.Tag('@dxos/functions/ProcessMonitorService')<
|
|
314
|
+
ProcessMonitorService,
|
|
315
|
+
Monitor
|
|
316
|
+
>() {}
|
|
317
|
+
|
|
318
|
+
export interface Info {
|
|
319
|
+
readonly pid: ID;
|
|
320
|
+
readonly parentPid: ID | null;
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Key of the process.
|
|
324
|
+
*
|
|
325
|
+
* NOTE: There might be multiple running processes with the same key.
|
|
326
|
+
*/
|
|
327
|
+
readonly key: string;
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Parameters of the process.
|
|
331
|
+
*/
|
|
332
|
+
readonly params: Params;
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* State of the process.
|
|
336
|
+
*/
|
|
337
|
+
readonly state: State;
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Error of the process.
|
|
341
|
+
* Only for process in FAILED state.
|
|
342
|
+
*/
|
|
343
|
+
readonly error: string | null;
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* UNIX timestamp in milliseconds.
|
|
347
|
+
*/
|
|
348
|
+
readonly startedAt: number;
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* UNIX timestamp in milliseconds.
|
|
352
|
+
*/
|
|
353
|
+
readonly completedAt: Option.Option<number>;
|
|
354
|
+
|
|
355
|
+
readonly metrics: {
|
|
356
|
+
/**
|
|
357
|
+
* Total wall time of all handler invocations of the process in milliseconds.
|
|
358
|
+
*/
|
|
359
|
+
readonly wallTime: number;
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Total number of inputs submitted to the process.
|
|
363
|
+
*/
|
|
364
|
+
readonly inputCount: number;
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Total number of outputs submitted to the process.
|
|
368
|
+
*/
|
|
369
|
+
readonly outputCount: number;
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* New process is spawned.
|
|
375
|
+
*/
|
|
376
|
+
export const SpawnedEvent = Trace.EventType('process.spawned', {
|
|
377
|
+
schema: Schema.Void,
|
|
378
|
+
isEphemeral: false,
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Process has reached a terminal state.
|
|
383
|
+
*/
|
|
384
|
+
export const ExitedEvent = Trace.EventType('process.exited', {
|
|
385
|
+
schema: Schema.Struct({
|
|
386
|
+
outcome: Schema.Literal('succeeded', 'failed', 'terminated'),
|
|
387
|
+
}),
|
|
388
|
+
isEphemeral: false,
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Renders spawned processes as a forest: top-level rows use "- ", nested rows use ├── / └── / │.
|
|
393
|
+
*/
|
|
394
|
+
export const prettyProcessTree = (tree: readonly Info[]): string => {
|
|
395
|
+
if (tree.length === 0) {
|
|
396
|
+
return '';
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const pidSet = new Set(tree.map((node) => node.pid));
|
|
400
|
+
const childrenByParent = new Map<string, Info[]>();
|
|
401
|
+
const roots: Info[] = [];
|
|
402
|
+
|
|
403
|
+
for (const node of tree) {
|
|
404
|
+
const parent = node.parentPid;
|
|
405
|
+
if (parent === null || !pidSet.has(parent)) {
|
|
406
|
+
roots.push(node);
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
const key = String(parent);
|
|
410
|
+
const siblings = childrenByParent.get(key) ?? [];
|
|
411
|
+
siblings.push(node);
|
|
412
|
+
childrenByParent.set(key, siblings);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
const byPid = (a: Info, b: Info) => String(a.pid).localeCompare(String(b.pid));
|
|
416
|
+
roots.sort(byPid);
|
|
417
|
+
for (const siblings of childrenByParent.values()) {
|
|
418
|
+
siblings.sort(byPid);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const formatLabel = (node: Info): string => {
|
|
422
|
+
const idShort = String(node.pid).slice(0, 6);
|
|
423
|
+
const parts = [idShort, node.state];
|
|
424
|
+
if (node.params.name != null && node.params.name !== '') {
|
|
425
|
+
parts.push(node.params.name);
|
|
426
|
+
}
|
|
427
|
+
if (node.error != null) {
|
|
428
|
+
parts.push(`(${node.error})`);
|
|
429
|
+
}
|
|
430
|
+
const { inputCount, outputCount, wallTime } = node.metrics;
|
|
431
|
+
parts.push(`[in:${inputCount} out:${outputCount} wall:${Math.round(wallTime)}ms]`);
|
|
432
|
+
return parts.join(' ');
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
const lines: string[] = [];
|
|
436
|
+
|
|
437
|
+
const walk = (node: Info, prefix: string, isLast: boolean, isRoot: boolean): void => {
|
|
438
|
+
if (isRoot) {
|
|
439
|
+
lines.push(`- ${formatLabel(node)}`);
|
|
440
|
+
} else {
|
|
441
|
+
const branch = isLast ? '└── ' : '├── ';
|
|
442
|
+
lines.push(`${prefix}${branch}${formatLabel(node)}`);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const children = childrenByParent.get(String(node.pid)) ?? [];
|
|
446
|
+
const nextPrefix = isRoot ? ' ' : `${prefix}${isLast ? ' ' : '│ '}`;
|
|
447
|
+
children.forEach((child, index) => {
|
|
448
|
+
walk(child, nextPrefix, index === children.length - 1, false);
|
|
449
|
+
});
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
for (const root of roots) {
|
|
453
|
+
walk(root, '', true, true);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
return lines.join('\n');
|
|
457
|
+
};
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
// @import-as-namespace
|
|
6
|
+
|
|
7
|
+
import * as Context from 'effect/Context';
|
|
8
|
+
import * as Effect from 'effect/Effect';
|
|
9
|
+
import * as Layer from 'effect/Layer';
|
|
10
|
+
import * as Option from 'effect/Option';
|
|
11
|
+
import type { DXN, SpaceId } from '@dxos/keys';
|
|
12
|
+
|
|
13
|
+
import { ServiceNotAvailableError } from '../errors';
|
|
14
|
+
import * as Process from './Process';
|
|
15
|
+
import * as Scope from 'effect/Scope';
|
|
16
|
+
import * as Either from 'effect/Either';
|
|
17
|
+
|
|
18
|
+
const ServiceResolverTypeId = '~@dxos/functions/ServiceResolver' as const;
|
|
19
|
+
type ServiceResolverTypeId = typeof ServiceResolverTypeId;
|
|
20
|
+
|
|
21
|
+
export interface ServiceResolver {
|
|
22
|
+
readonly [ServiceResolverTypeId]: ServiceResolverTypeId;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Resolve a set of services identified by their tags.
|
|
26
|
+
* Returns a Context containing all requested services, or fails with ServiceNotAvailableError.
|
|
27
|
+
*/
|
|
28
|
+
resolve<Tag extends Context.Tag<any, any>>(
|
|
29
|
+
tag: Tag,
|
|
30
|
+
context: ResolutionContext,
|
|
31
|
+
): Effect.Effect<Context.Tag.Service<Tag>, ServiceNotAvailableError, Scope.Scope>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Tag for the ServiceResolver service.
|
|
36
|
+
*/
|
|
37
|
+
export const ServiceResolver = Context.GenericTag<ServiceResolver>('@dxos/functions/ServiceResolver');
|
|
38
|
+
|
|
39
|
+
export const resolve = Effect.serviceFunctionEffect(ServiceResolver, (_) => _.resolve);
|
|
40
|
+
|
|
41
|
+
export const resolveAll = <const Tags extends readonly Context.Tag<any, any>[]>(
|
|
42
|
+
tags: Tags,
|
|
43
|
+
context: ResolutionContext,
|
|
44
|
+
): Effect.Effect<Context.Context<Tags[number]>, ServiceNotAvailableError, Scope.Scope | ServiceResolver> =>
|
|
45
|
+
Effect.gen(function* () {
|
|
46
|
+
const services = yield* Effect.forEach(tags, (tag) =>
|
|
47
|
+
resolve(tag, context).pipe(Effect.map((service) => Context.make(tag, service))),
|
|
48
|
+
);
|
|
49
|
+
return Context.mergeAll(...services);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Provides context for service resolution.
|
|
54
|
+
*/
|
|
55
|
+
export interface ResolutionContext {
|
|
56
|
+
/**
|
|
57
|
+
* Under which identity the process is running.
|
|
58
|
+
*/
|
|
59
|
+
readonly identity?: string;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Under which space the process is running.
|
|
63
|
+
*/
|
|
64
|
+
readonly space?: SpaceId;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* DXN of the conversation feed the process is running in.
|
|
68
|
+
*/
|
|
69
|
+
readonly conversation?: DXN.String;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Under which process the process is running.
|
|
73
|
+
*/
|
|
74
|
+
readonly process?: Process.ID;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const succeed = <I, S>(
|
|
78
|
+
tag: Context.Tag<I, S>,
|
|
79
|
+
getService: (context: ResolutionContext) => Effect.Effect<S, ServiceNotAvailableError, Scope.Scope>,
|
|
80
|
+
): ServiceResolver => {
|
|
81
|
+
return make((tag1, context) => {
|
|
82
|
+
if (tag1.key !== tag.key) {
|
|
83
|
+
return Effect.fail(new ServiceNotAvailableError(`Service not available: ${String(tag.key ?? tag)}`));
|
|
84
|
+
}
|
|
85
|
+
const service = getService(context);
|
|
86
|
+
return service as any;
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Create a ServiceResolver from a custom resolution function.
|
|
92
|
+
*/
|
|
93
|
+
export const make = (
|
|
94
|
+
resolveFn: <I, S>(
|
|
95
|
+
tag: Context.Tag<I, S>,
|
|
96
|
+
context: ResolutionContext,
|
|
97
|
+
) => Effect.Effect<S, ServiceNotAvailableError, Scope.Scope>,
|
|
98
|
+
): ServiceResolver => ({
|
|
99
|
+
[ServiceResolverTypeId]: ServiceResolverTypeId,
|
|
100
|
+
resolve: resolveFn,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Create a ServiceResolver backed by a static Context.
|
|
105
|
+
* Tags present in the context are resolved; missing tags fail with ServiceNotAvailableError.
|
|
106
|
+
*/
|
|
107
|
+
export const fromContext = (ctx: Context.Context<any>): ServiceResolver =>
|
|
108
|
+
make((tag, context) =>
|
|
109
|
+
Effect.gen(function* () {
|
|
110
|
+
const service = Context.getOption(ctx, tag);
|
|
111
|
+
if (Option.isNone(service)) {
|
|
112
|
+
return yield* Effect.fail(new ServiceNotAvailableError(String(tag.key ?? tag)));
|
|
113
|
+
}
|
|
114
|
+
return service.value;
|
|
115
|
+
}),
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Create a ServiceResolver that resolves tags from the current Effect context.
|
|
120
|
+
* Only the specified tags are available; requests for other tags fail.
|
|
121
|
+
*/
|
|
122
|
+
export const fromRequirements = <const Tags extends readonly Context.Tag<any, any>[]>(
|
|
123
|
+
...tags: Tags
|
|
124
|
+
): Effect.Effect<ServiceResolver, never, Context.Tag.Identifier<Tags[number]>> =>
|
|
125
|
+
Effect.contextWith((parentCtx: Context.Context<any>) => {
|
|
126
|
+
const available = new Set(tags.map((tag) => tag.key));
|
|
127
|
+
return make((tag, context) =>
|
|
128
|
+
Effect.gen(function* () {
|
|
129
|
+
let result: Context.Context<never> = Context.empty() as Context.Context<never>;
|
|
130
|
+
if (!available.has(tag.key)) {
|
|
131
|
+
return yield* Effect.fail(new ServiceNotAvailableError(String(tag.key ?? tag)));
|
|
132
|
+
}
|
|
133
|
+
const service = Context.getOption(parentCtx, tag);
|
|
134
|
+
if (Option.isNone(service)) {
|
|
135
|
+
return yield* Effect.fail(new ServiceNotAvailableError(String(tag.key ?? tag)));
|
|
136
|
+
}
|
|
137
|
+
return service.value;
|
|
138
|
+
}),
|
|
139
|
+
);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Like {@link fromRequirements} but returns a Layer that provides ServiceResolver.
|
|
144
|
+
*/
|
|
145
|
+
export const layerRequirements = <const Tags extends readonly Context.Tag<any, any>[]>(
|
|
146
|
+
...tags: Tags
|
|
147
|
+
): Layer.Layer<ServiceResolver, never, Context.Tag.Identifier<Tags[number]>> =>
|
|
148
|
+
Layer.effect(ServiceResolver, fromRequirements(...tags));
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Compose multiple resolvers left to right. Earlier resolvers take precedence:
|
|
152
|
+
* the first resolver that can satisfy a tag wins.
|
|
153
|
+
*/
|
|
154
|
+
export const compose = (...resolvers: readonly ServiceResolver[]): ServiceResolver =>
|
|
155
|
+
make((tag, context) =>
|
|
156
|
+
Effect.gen(function* () {
|
|
157
|
+
for (const resolver of resolvers) {
|
|
158
|
+
const single = yield* resolver.resolve(tag, context).pipe(Effect.either);
|
|
159
|
+
if (Either.isRight(single)) {
|
|
160
|
+
return single.right;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return yield* Effect.fail(new ServiceNotAvailableError(String(tag.key ?? tag)));
|
|
165
|
+
}),
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* An empty resolver that fails for any requested service.
|
|
170
|
+
*/
|
|
171
|
+
export const empty: ServiceResolver = make((tag, context) => {
|
|
172
|
+
return Effect.fail(new ServiceNotAvailableError(String(tag.key ?? tag)));
|
|
173
|
+
});
|