@rome-os/app-runtime 0.3.1 → 0.3.2

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.
@@ -1,149 +0,0 @@
1
- // Worker-side RPC client for calling main-process services.
2
- //
3
- // The main process owns live references to ChannelManager, RoutineEngine,
4
- // and related services. Worker processes reach them over Node's child-process
5
- // IPC channel via this client — a replacement for the former HTTP callbacks
6
- // to the internal API.
7
- import { randomUUID } from "node:crypto";
8
- export class WorkerRpcTimeoutError extends Error {
9
- constructor(method, timeoutMs) {
10
- super(`WorkerRPC timeout: ${method} (${timeoutMs}ms)`);
11
- this.name = "WorkerRpcTimeoutError";
12
- }
13
- }
14
- export class WorkerRpcDisconnectError extends Error {
15
- constructor() {
16
- super("WorkerRPC main process disconnected");
17
- this.name = "WorkerRpcDisconnectError";
18
- }
19
- }
20
- const DEFAULT_TIMEOUT_MS = 30_000;
21
- // There may be more than one copy of this module loaded in the same process
22
- // (e.g. when an installed app ships its own physical copy of @rome-os/app-runtime
23
- // under node_modules/, producing a distinct ESM URL from the monorepo source).
24
- // Each copy constructs its own WorkerRpcClient with a distinct `clientId`.
25
- // Every instance installs its own process.on("message") listener; the main
26
- // process echoes `clientId` back on responses, so each listener only resolves
27
- // responses originating from its own pending map. This keeps pending maps
28
- // fully isolated across copies and avoids id-space collisions (nextId starts
29
- // from 1 independently in each copy).
30
- class WorkerRpcClient {
31
- clientId = randomUUID();
32
- pending = new Map();
33
- nextId = 1;
34
- processHandlersInstalled = false;
35
- async call(method, params, options) {
36
- if (!process.send) {
37
- // Main process: no parent IPC channel, but it directly holds the real
38
- // services. If a dispatcher is registered, run the call in-process
39
- // instead of failing. Worker processes always have `process.send` and
40
- // take the IPC path below — the two paths are mutually exclusive.
41
- const dispatcher = getInProcessDispatcher();
42
- if (dispatcher) {
43
- // Enforce the same deadline as the IPC path so both transports share
44
- // identical timeout semantics. Without this, callers like
45
- // AppManagerProxy (which set multi-minute deadlines for
46
- // apps.install/create/uninstall) would hang forever if an in-process
47
- // service operation stalls.
48
- const timeoutMs = options?.timeoutMs ?? DEFAULT_TIMEOUT_MS;
49
- let timer;
50
- const timeout = new Promise((_, reject) => {
51
- timer = setTimeout(() => reject(new WorkerRpcTimeoutError(method, timeoutMs)), timeoutMs);
52
- });
53
- try {
54
- return (await Promise.race([dispatcher(method, params), timeout]));
55
- }
56
- finally {
57
- if (timer)
58
- clearTimeout(timer);
59
- }
60
- }
61
- throw new Error(`WorkerRPC: not running in a Node.js child process (method=${method})`);
62
- }
63
- this.ensureProcessHandlers();
64
- const id = this.nextId++;
65
- const timeoutMs = options?.timeoutMs ?? DEFAULT_TIMEOUT_MS;
66
- return await new Promise((resolve, reject) => {
67
- const timeout = setTimeout(() => {
68
- this.pending.delete(id);
69
- reject(new WorkerRpcTimeoutError(method, timeoutMs));
70
- }, timeoutMs);
71
- this.pending.set(id, {
72
- resolve: (value) => resolve(value),
73
- reject,
74
- timeout,
75
- method,
76
- });
77
- const request = {
78
- type: "rpc_request",
79
- clientId: this.clientId,
80
- id,
81
- method,
82
- params,
83
- };
84
- process.send(request, (err) => {
85
- if (err) {
86
- clearTimeout(timeout);
87
- this.pending.delete(id);
88
- reject(err);
89
- }
90
- });
91
- });
92
- }
93
- handleResponse(message) {
94
- const pending = this.pending.get(message.id);
95
- if (!pending)
96
- return;
97
- clearTimeout(pending.timeout);
98
- this.pending.delete(message.id);
99
- if (message.error !== undefined) {
100
- pending.reject(new Error(message.error));
101
- }
102
- else {
103
- pending.resolve(message.result);
104
- }
105
- }
106
- ensureProcessHandlers() {
107
- if (this.processHandlersInstalled)
108
- return;
109
- this.processHandlersInstalled = true;
110
- process.on("message", (message) => {
111
- if (isWorkerRpcResponse(message) && message.clientId === this.clientId) {
112
- this.handleResponse(message);
113
- }
114
- });
115
- process.on("disconnect", () => {
116
- for (const pending of this.pending.values()) {
117
- clearTimeout(pending.timeout);
118
- pending.reject(new WorkerRpcDisconnectError());
119
- }
120
- this.pending.clear();
121
- });
122
- }
123
- }
124
- let singleton = null;
125
- export function getWorkerRpc() {
126
- if (!singleton) {
127
- singleton = new WorkerRpcClient();
128
- }
129
- return singleton;
130
- }
131
- const IN_PROCESS_DISPATCHER_KEY = Symbol.for("rome.workerRpc.inProcessDispatcher");
132
- /**
133
- * Register (or clear, with `null`) the main-process dispatcher used as a
134
- * fallback when there is no parent IPC channel. Called once during main-process
135
- * wiring with a handler backed by the live WorkerRpcServer.
136
- */
137
- export function setWorkerRpcInProcessDispatcher(dispatcher) {
138
- globalThis[IN_PROCESS_DISPATCHER_KEY] = dispatcher ?? undefined;
139
- }
140
- function getInProcessDispatcher() {
141
- return globalThis[IN_PROCESS_DISPATCHER_KEY];
142
- }
143
- export function isWorkerRpcResponse(message) {
144
- return (typeof message === "object" &&
145
- message !== null &&
146
- message.type === "rpc_response" &&
147
- typeof message.clientId === "string");
148
- }
149
- //# sourceMappingURL=worker-rpc.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"worker-rpc.js","sourceRoot":"","sources":["../src/worker-rpc.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,EAAE;AACF,0EAA0E;AAC1E,8EAA8E;AAC9E,4EAA4E;AAC5E,uBAAuB;AAEvB,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAkBzC,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAC9C,YAAY,MAAc,EAAE,SAAiB;QAC3C,KAAK,CAAC,sBAAsB,MAAM,KAAK,SAAS,KAAK,CAAC,CAAC;QACvD,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;IACtC,CAAC;CACF;AAED,MAAM,OAAO,wBAAyB,SAAQ,KAAK;IACjD;QACE,KAAK,CAAC,qCAAqC,CAAC,CAAC;QAC7C,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;IACzC,CAAC;CACF;AASD,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAElC,4EAA4E;AAC5E,kFAAkF;AAClF,+EAA+E;A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SAAS,CAAC;AACnB,CAAC;AAgBD,MAAM,yBAAyB,GAAG,MAAM,CAAC,GAAG,CAAC,oCAAoC,CAAC,CAAC;AAEnF;;;;GAIG;AACH,MAAM,UAAU,+BAA+B,CAC7C,UAA+C;IAE9C,UAAsC,CAAC,yBAAyB,CAAC,GAAG,UAAU,IAAI,SAAS,CAAC;AAC/F,CAAC;AAED,SAAS,sBAAsB;IAC7B,OAAQ,UAAsC,CAAC,yBAAyB,CAE3D,CAAC;AAChB,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,OAAgB;IAClD,OAAO,CACL,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,KAAK,IAAI;QACf,OAA6B,CAAC,IAAI,KAAK,cAAc;QACtD,OAAQ,OAAkC,CAAC,QAAQ,KAAK,QAAQ,CACjE,CAAC;AACJ,CAAC"}
@@ -1,61 +0,0 @@
1
- export type Json = unknown;
2
- /** What a step's body can reach: the action runner (to invoke any registered
3
- * action, including `summon`) and a logger. */
4
- export interface StepContext {
5
- runAction(name: string, args: Record<string, unknown>): Promise<Json>;
6
- log: {
7
- info(message: string, data?: Record<string, unknown>): void;
8
- warn(message: string, data?: Record<string, unknown>): void;
9
- };
10
- /** True on a verification/preview run. A step that writes to the outside world
11
- * (sends a message, posts to a SaaS API) must short-circuit when this is set,
12
- * so building or testing a workflow never performs a real external write. The
13
- * interpreter threads it unchanged to every leaf. */
14
- dryRun: boolean;
15
- }
16
- /** A leaf body: transform the input, optionally calling actions via `ctx`. */
17
- export type StepFn = (input: Json, ctx: StepContext) => Json | Promise<Json>;
18
- export type Workflow = {
19
- kind: "step";
20
- label: string;
21
- run: StepFn;
22
- } | {
23
- kind: "pipeline";
24
- steps: Workflow[];
25
- } | {
26
- kind: "parallel";
27
- branches: Workflow[];
28
- } | {
29
- kind: "branch";
30
- label: string;
31
- predicate: (input: Json) => boolean;
32
- then: Workflow;
33
- otherwise?: Workflow;
34
- } | {
35
- kind: "map";
36
- label: string;
37
- items?: (input: Json) => Json[];
38
- each: Workflow;
39
- };
40
- /** A leaf: author-written code. Transform the input and/or call actions via
41
- * `ctx.runAction(name, args)` — including `ctx.runAction("summon", …)` to hand
42
- * the step to an LLM agent when it needs judgment. Keep `summon` to a single
43
- * step; don't fan it across a `map`/`parallel` (see issue #639). */
44
- export declare function step(label: string, run: StepFn): Workflow;
45
- /** Run steps in order, threading each step's output into the next step's input. */
46
- export declare function pipeline(...steps: Workflow[]): Workflow;
47
- /** Run branches concurrently on the same input; resolve to the array of outputs. */
48
- export declare function parallel(...branches: Workflow[]): Workflow;
49
- /** Run `then` when the predicate holds; otherwise run `otherwise` (or pass the
50
- * input through unchanged when there is no else arm). */
51
- export declare function branch(label: string, predicate: (input: Json) => boolean, then: Workflow, otherwise?: Workflow): Workflow;
52
- /** A one-armed branch: run `then` only when the predicate holds. */
53
- export declare function when(label: string, predicate: (input: Json) => boolean, then: Workflow): Workflow;
54
- /** Fan `each` out over a runtime list, concurrently; resolve to the per-item
55
- * outputs. Keep `each` deterministic — don't fan a `summon` step across a map
56
- * (see issue #639). */
57
- export declare function map(label: string, each: Workflow, items?: (input: Json) => Json[]): Workflow;
58
- /** Walk the workflow, running each leaf through its `run` function and threading
59
- * values according to the combinators. Returns the workflow's final output. */
60
- export declare function execute(wf: Workflow, input: Json, ctx: StepContext): Promise<Json>;
61
- //# sourceMappingURL=workflow.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"workflow.d.ts","sourceRoot":"","sources":["../src/workflow.ts"],"names":[],"mappings":"AAeA,MAAM,MAAM,IAAI,GAAG,OAAO,CAAC;AAE3B;+CAC+C;AAC/C,MAAM,WAAW,WAAW;IAC1B,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACtE,GAAG,EAAE;QACH,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAA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,CAEvD;AAED,oFAAoF;AACpF,wBAAgB,QAAQ,CAAC,GAAG,QAAQ,EAAE,QAAQ,EAAE,GAAG,QAAQ,CAE1D;AAED;yDACyD;AACzD,wBAAgB,MAAM,CACpB,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,CAAC,KAAK,EAAE,IAAI,KAAK,OAAO,EACnC,IAAI,EAAE,QAAQ,EACd,SAAS,CAAC,EAAE,QAAQ,GACnB,QAAQ,CAEV;AAED,oEAAoE;AACpE,wBAAgB,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,KAAK,EAAE,IAAI,KAAK,OAAO,EAAE,IAAI,EAAE,QAAQ,GAAG,QAAQ,CAEjG;AAED;;uBAEuB;AACvB,wBAAgB,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,IAAI,KAAK,IAAI,EAAE,GAAG,QAAQ,CAE5F;AA4BD;+EAC+E;AAC/E,wBAAsB,OAAO,CAAC,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAyBxF"}
package/dist/workflow.js DELETED
@@ -1,88 +0,0 @@
1
- // The workflow combinator eDSL and its interpreter.
2
- //
3
- // A workflow is a *description* — a tree of combinators — not a procedure.
4
- // `execute` walks the tree, running each leaf and threading values between them.
5
- //
6
- // A leaf is a `step`: plain author-written code. A step transforms the value
7
- // flowing in and may reach any registered action — `send_message`, a connector
8
- // call, or `summon` (hand off to an LLM agent) — through `ctx.runAction`.
9
- // Control flow — sequencing, concurrency, conditionals, fan-out — lives in the
10
- // combinators, never in host `if`/`for`, so the structure stays explicit.
11
- //
12
- // This engine ships in the published SDK so every workflow app shares one copy:
13
- // a generated app contributes only its `buildWorkflow()` definition and imports
14
- // these combinators, rather than carrying its own interpreter.
15
- // --- constructors -----------------------------------------------------------
16
- /** A leaf: author-written code. Transform the input and/or call actions via
17
- * `ctx.runAction(name, args)` — including `ctx.runAction("summon", …)` to hand
18
- * the step to an LLM agent when it needs judgment. Keep `summon` to a single
19
- * step; don't fan it across a `map`/`parallel` (see issue #639). */
20
- export function step(label, run) {
21
- return { kind: "step", label, run };
22
- }
23
- /** Run steps in order, threading each step's output into the next step's input. */
24
- export function pipeline(...steps) {
25
- return { kind: "pipeline", steps };
26
- }
27
- /** Run branches concurrently on the same input; resolve to the array of outputs. */
28
- export function parallel(...branches) {
29
- return { kind: "parallel", branches };
30
- }
31
- /** Run `then` when the predicate holds; otherwise run `otherwise` (or pass the
32
- * input through unchanged when there is no else arm). */
33
- export function branch(label, predicate, then, otherwise) {
34
- return { kind: "branch", label, predicate, then, otherwise };
35
- }
36
- /** A one-armed branch: run `then` only when the predicate holds. */
37
- export function when(label, predicate, then) {
38
- return branch(label, predicate, then);
39
- }
40
- /** Fan `each` out over a runtime list, concurrently; resolve to the per-item
41
- * outputs. Keep `each` deterministic — don't fan a `summon` step across a map
42
- * (see issue #639). */
43
- export function map(label, each, items) {
44
- return { kind: "map", label, each, items };
45
- }
46
- // --- interpreter ------------------------------------------------------------
47
- const asArray = (v) => (Array.isArray(v) ? v : []);
48
- /** Run concurrent branches like `Promise.all`, but wait for *every* branch to
49
- * settle before reporting failure. `Promise.all` rejects on the first failure
50
- * while the other branches keep running — and because steps may call
51
- * side-effecting actions (`send_message`, connector writes), that would let a
52
- * failed run resolve while siblings are still mutating the world in the
53
- * background, making a retry unsafe. Waiting for all branches keeps the
54
- * workflow's outcome honest: on success it resolves to the per-branch outputs
55
- * in order; on any failure it throws once nothing is still in flight. */
56
- async function runConcurrent(branches, context) {
57
- const settled = await Promise.allSettled(branches);
58
- const failures = settled.filter((r) => r.status === "rejected");
59
- if (failures.length > 0) {
60
- throw new AggregateError(failures.map((f) => f.reason), `${context}: ${failures.length}/${settled.length} branch(es) failed`);
61
- }
62
- return settled.map((r) => r.value);
63
- }
64
- /** Walk the workflow, running each leaf through its `run` function and threading
65
- * values according to the combinators. Returns the workflow's final output. */
66
- export async function execute(wf, input, ctx) {
67
- switch (wf.kind) {
68
- case "step":
69
- return await wf.run(input, ctx);
70
- case "pipeline": {
71
- let value = input;
72
- for (const s of wf.steps)
73
- value = await execute(s, value, ctx);
74
- return value;
75
- }
76
- case "parallel":
77
- return runConcurrent(wf.branches.map((b) => execute(b, input, ctx)), "parallel");
78
- case "branch":
79
- if (wf.predicate(input))
80
- return execute(wf.then, input, ctx);
81
- return wf.otherwise ? execute(wf.otherwise, input, ctx) : input;
82
- case "map": {
83
- const items = wf.items ? wf.items(input) : asArray(input);
84
- return runConcurrent(items.map((it) => execute(wf.each, it, ctx)), `map "${wf.label}"`);
85
- }
86
- }
87
- }
88
- //# sourceMappingURL=workflow.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"workflow.js","sourceRoot":"","sources":["../src/workflow.ts"],"names":[],"mappings":"AAAA,oDAAoD;AACpD,EAAE;AACF,2EAA2E;AAC3E,iFAAiF;AACjF,EAAE;AACF,6EAA6E;AAC7E,+EAA+E;AAC/E,0EAA0E;AAC1E,+EAA+E;AAC/E,0EAA0E;AAC1E,EAAE;AACF,gFAAgF;AAChF,gFAAgF;AAChF,+DAA+D;AAmC/D,+EAA+E;AAE/E;;;oEAGoE;AACpE,MAAM,UAAU,IAAI,CAAC,KAAa,EAAE,GAAW;IAC7C,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC;AACtC,CAAC;AAED,mFAAmF;AACnF,MAAM,UAAU,QAAQ,CAAC,GAAG,KAAiB;IAC3C,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC;AACrC,CAAC;AAED,oFAAoF;AACpF,MAAM,UAAU,QAAQ,CAAC,GAAG,QAAoB;IAC9C,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC;AACxC,CAAC;AAED;yDACyD;AACzD,M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