@powerhousedao/reactor-workflow 6.2.3-dev.34 → 6.2.3-dev.35

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/dist/testing.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { C as PieceResolver, D as RecordedSchedule, M as PieceDescriptor, N as buildDescriptor, O as extractDedupeKey, P as loadPieceFromDir, S as PackagePiece, T as sourcedResolver, _ as PieceWorkerPool, a as ActivepiecesBlockExecutor, c as ReactorPort, d as InMemorySecretProvider, g as WorkflowRunResult, i as runWorkflow, o as AttachmentPort, p as BlockExecution, s as CompositeBlockExecutor, t as PieceRegistry, u as StaticConnectionResolver, v as IPieceWorker, w as PieceTarget, x as LocalPiece, y as PieceWorker } from "./piece-registry-ohYcOXd-.js";
1
+ import { C as PieceResolver, D as RecordedSchedule, M as PieceDescriptor, N as buildDescriptor, O as extractDedupeKey, P as loadPieceFromDir, S as PackagePiece, T as sourcedResolver, _ as PieceWorkerPool, a as ActivepiecesBlockExecutor, c as ReactorPort, d as InMemorySecretProvider, g as WorkflowRunResult, i as runWorkflow, o as AttachmentPort, p as BlockExecution, s as CompositeBlockExecutor, t as PieceRegistry, u as StaticConnectionResolver, v as IPieceWorker, w as PieceTarget, x as LocalPiece, y as PieceWorker } from "./piece-registry-Bt1Cd3ly.js";
2
2
  //#region src/pieces/activepieces/fetch.d.ts
3
3
  interface FetchPieceBundleOptions {
4
4
  name: string;
package/dist/testing.js CHANGED
@@ -1,4 +1,4 @@
1
- import { m as extractDedupeKey, z as buildDescriptor } from "./redact-LRWHJGW9.js";
2
- import { B as PieceWorker, D as StaticConnectionResolver, G as sourcedResolver, K as ensurePieceBundle, M as InMemorySecretProvider, a as runWorkflow, d as ActivepiecesBlockExecutor, f as CompositeBlockExecutor, q as fetchPieceBundle, t as PieceRegistry, z as PieceWorkerPool } from "./piece-registry-CAMLtGeP.js";
1
+ import { B as buildDescriptor, m as extractDedupeKey } from "./redact-DW7W5rrV.js";
2
+ import { B as PieceWorker, D as StaticConnectionResolver, G as sourcedResolver, K as ensurePieceBundle, M as InMemorySecretProvider, a as runWorkflow, d as ActivepiecesBlockExecutor, f as CompositeBlockExecutor, q as fetchPieceBundle, t as PieceRegistry, z as PieceWorkerPool } from "./piece-registry-BBLaiiwi.js";
3
3
  import { n as loadPieceFromDir } from "./loader-Gkx6I_Bo.js";
4
4
  export { ActivepiecesBlockExecutor, CompositeBlockExecutor, InMemorySecretProvider, PieceRegistry, PieceWorker, PieceWorkerPool, StaticConnectionResolver, buildDescriptor, ensurePieceBundle, extractDedupeKey, fetchPieceBundle, loadPieceFromDir, runWorkflow, sourcedResolver };
@@ -1,10 +1,17 @@
1
- import { A as REACTOR_CREATE, B as describeProperties, C as setMaxFileBytes, D as jsonSafe, E as buildActionContext, F as REACTOR_MODELS, G as UnsupportedContextMemberError, H as authMethodFor, I as STORE_DELETE, J as getActions, K as throwingStub, L as STORE_GET, M as REACTOR_FIND, N as REACTOR_GET, O as LOG_WRITE, P as REACTOR_MODEL, R as STORE_PUT, S as maxFileBytes, T as InMemoryKeyValueStore, U as unsupportedAuth, V as UnsupportedPieceFeatureError, W as unsupportedTrigger, Y as getTriggers, _ as StagedFilesService, a as redactMessage, b as assertWithinLimit, f as runWithEgressPolicy, g as DataUriFilesService, h as runTriggerHook, i as redactError, j as REACTOR_EXECUTE, k as OUTPUT_UPDATE, l as installEgressGuard, p as buildTriggerContext, q as withTouchTracking, w as InMemoryConnectionsProvider, z as buildDescriptor } from "./redact-LRWHJGW9.js";
1
+ import { A as REACTOR_FIND, B as buildDescriptor, C as setMaxFileBytes, D as jsonSafe, E as buildActionContext, F as REACTOR_SUBMIT_CREATE, G as unsupportedTrigger, H as UnsupportedPieceFeatureError, I as REACTOR_WAIT, J as withTouchTracking, K as UnsupportedContextMemberError, L as STORE_DELETE, M as REACTOR_MODEL, N as REACTOR_MODELS, O as LOG_WRITE, P as REACTOR_SUBMIT, R as STORE_GET, S as maxFileBytes, T as InMemoryKeyValueStore, U as authMethodFor, V as describeProperties, W as unsupportedAuth, X as getTriggers, Y as getActions, _ as StagedFilesService, a as redactMessage, b as assertWithinLimit, f as runWithEgressPolicy, g as DataUriFilesService, h as runTriggerHook, i as redactError, j as REACTOR_GET, k as OUTPUT_UPDATE, l as installEgressGuard, p as buildTriggerContext, q as throwingStub, w as InMemoryConnectionsProvider, z as STORE_PUT } from "./redact-DW7W5rrV.js";
2
2
  import { n as loadPieceFromDir, t as loadPiece } from "./loader-Gkx6I_Bo.js";
3
3
  import { readFile } from "node:fs/promises";
4
4
  import { format } from "node:util";
5
5
  import { arrayZipperProcessor, formatPieceError, processors, validateProperty } from "@powerhousedao/pieces-framework/host";
6
6
  //#region src/pieces/activepieces/worker/host-call.ts
7
7
  const DEFAULT_HOST_CALL_TIMEOUT_MS = 1e4;
8
+ let fromWire;
9
+ function setHostCallTimeout(timeoutMs) {
10
+ fromWire = timeoutMs;
11
+ }
12
+ function hostCallTimeoutMs() {
13
+ return fromWire ?? DEFAULT_HOST_CALL_TIMEOUT_MS;
14
+ }
8
15
  const pending = /* @__PURE__ */ new Map();
9
16
  let nextId = 1;
10
17
  let listening = false;
@@ -39,7 +46,7 @@ function ensureListening() {
39
46
  entry.resolve(message.value);
40
47
  });
41
48
  }
42
- function callHost(method, payload, timeoutMs = DEFAULT_HOST_CALL_TIMEOUT_MS) {
49
+ function callHost(method, payload, timeoutMs = hostCallTimeoutMs()) {
43
50
  if (!process.send) return Promise.reject(new HostCallError(method, "the worker has no channel to its host"));
44
51
  ensureListening();
45
52
  const id = nextId++;
@@ -73,7 +80,83 @@ function notifyHost(method, payload) {
73
80
  }
74
81
  //#endregion
75
82
  //#region src/pieces/activepieces/context/reactor.ts
83
+ const MAX_POLL_WAIT_MS = 4e3;
84
+ const MIN_DEADLINE_MARGIN_MS = 250;
85
+ const MAX_DEADLINE_MARGIN_MS = 2e3;
86
+ function deadlineMargin(budgetMs) {
87
+ return Math.min(Math.max(Math.floor(budgetMs / 10), MIN_DEADLINE_MARGIN_MS), MAX_DEADLINE_MARGIN_MS);
88
+ }
89
+ var ReactorJobPendingError = class extends Error {
90
+ jobId;
91
+ status;
92
+ constructor(jobId, status) {
93
+ super(status === "UNKNOWN" ? `Reactor job ${jobId} is unknown to the reactor, as after a restart; its actions may or may not have been written` : `Reactor job ${jobId} was still ${status} at the step deadline; its actions may yet be written`);
94
+ this.name = "ReactorJobPendingError";
95
+ this.jobId = jobId;
96
+ this.status = status;
97
+ }
98
+ };
99
+ var ReactorSubmitUnconfirmedError = class extends Error {
100
+ constructor(what) {
101
+ super(`Submitting ${what} to the reactor got no answer before the step deadline; it may have been submitted`);
102
+ this.name = "ReactorSubmitUnconfirmedError";
103
+ }
104
+ };
105
+ var ReactorBudgetExhaustedError = class extends Error {
106
+ constructor(what) {
107
+ super(`No time was left before the step deadline to submit ${what}; nothing was submitted`);
108
+ this.name = "ReactorBudgetExhaustedError";
109
+ }
110
+ };
111
+ var ReactorStateUnreadError = class extends Error {
112
+ jobId;
113
+ constructor(jobId) {
114
+ super(`Reactor job ${jobId} applied its actions, but the document was not read back before the step deadline`);
115
+ this.name = "ReactorStateUnreadError";
116
+ this.jobId = jobId;
117
+ }
118
+ };
119
+ var ReactorJobFailedError = class extends Error {
120
+ jobId;
121
+ constructor(jobId, detail) {
122
+ super(`Reactor job ${jobId} failed: ${detail ?? "unknown error"}`);
123
+ this.name = "ReactorJobFailedError";
124
+ this.jobId = jobId;
125
+ }
126
+ };
127
+ function assertActionsApplied(state, submission, actions) {
128
+ if (!state.actions) throw new Error(`Reactor job ${state.jobId} reported no outcome for its ${submission.actionIds.length} action(s)`);
129
+ const outcomes = new Map(state.actions.map((outcome) => [outcome.actionId, outcome]));
130
+ const failed = submission.actionIds.flatMap((actionId, index) => {
131
+ const type = actions[index]?.type ?? actionId;
132
+ const outcome = outcomes.get(actionId);
133
+ if (!outcome) return [`Action ${type} produced no operation`];
134
+ if (outcome.kind === "reducer-error") return [`Action ${type} failed: ${outcome.message ?? "unknown error"}`];
135
+ if (outcome.kind === "denied") return [`Action ${type} was denied: ${outcome.reason ?? "no reason"}`];
136
+ return [];
137
+ });
138
+ if (failed.length > 0) throw new Error(failed.join("; "));
139
+ }
140
+ function jobKey(stepName) {
141
+ return `reactor.job/${stepName ?? "step"}`;
142
+ }
76
143
  var RemoteReactorService = class {
144
+ options;
145
+ stopAt;
146
+ margin;
147
+ constructor(options = {}) {
148
+ this.options = options;
149
+ const { deadline } = options;
150
+ this.margin = deadline === void 0 ? 0 : deadlineMargin(deadline - Date.now());
151
+ this.stopAt = deadline === void 0 ? Number.POSITIVE_INFINITY : deadline - this.margin;
152
+ }
153
+ waitCallMs(maxWaitMs) {
154
+ if (this.options.deadline === void 0) return hostCallTimeoutMs();
155
+ return Math.min(hostCallTimeoutMs(), maxWaitMs + this.margin / 4);
156
+ }
157
+ afterJobMs() {
158
+ return Math.min(hostCallTimeoutMs(), Math.max(this.stopAt - Date.now(), this.margin / 2));
159
+ }
77
160
  models() {
78
161
  return callHost(REACTOR_MODELS, {});
79
162
  }
@@ -86,11 +169,85 @@ var RemoteReactorService = class {
86
169
  find(input) {
87
170
  return callHost(REACTOR_FIND, input);
88
171
  }
89
- create(input) {
90
- return callHost(REACTOR_CREATE, input);
172
+ async create(input) {
173
+ const submission = await this.submitWithin(REACTOR_SUBMIT_CREATE, input, `a ${input.documentType} document`);
174
+ const { documentId, jobIds } = submission;
175
+ this.record({
176
+ documentId,
177
+ jobIds
178
+ });
179
+ let landed;
180
+ try {
181
+ for (const jobId of jobIds) {
182
+ await this.settle(jobId);
183
+ landed = jobId;
184
+ }
185
+ for (const followUp of submission.followUps) landed = await this.write(followUp);
186
+ return await this.readBack(documentId, void 0, landed ?? documentId);
187
+ } catch (error) {
188
+ if (landed && error instanceof Error) error.message = `Document ${documentId} was created, but: ${error.message}`;
189
+ throw error;
190
+ }
191
+ }
192
+ async execute(input) {
193
+ const jobId = await this.write(input, true);
194
+ return this.readBack(input.documentId, input.branch, jobId);
195
+ }
196
+ async write(input, recorded = false) {
197
+ const submission = await this.submitWithin(REACTOR_SUBMIT, input, `${input.actions.length} action(s)`);
198
+ if (recorded) this.record(submission);
199
+ assertActionsApplied(await this.settle(submission.jobId), submission, input.actions);
200
+ return submission.jobId;
201
+ }
202
+ async readBack(documentId, branch, jobId) {
203
+ try {
204
+ return await callHost(REACTOR_GET, {
205
+ documentId,
206
+ ...branch ? { branch } : {}
207
+ }, this.afterJobMs());
208
+ } catch (error) {
209
+ if (error instanceof HostCallTimeoutError) throw new ReactorStateUnreadError(jobId);
210
+ throw error;
211
+ }
212
+ }
213
+ async submitWithin(method, input, what) {
214
+ const remaining = this.stopAt - Date.now();
215
+ if (remaining <= 0) throw new ReactorBudgetExhaustedError(what);
216
+ try {
217
+ return await callHost(method, input, Math.min(hostCallTimeoutMs(), remaining));
218
+ } catch (error) {
219
+ if (error instanceof HostCallTimeoutError) throw new ReactorSubmitUnconfirmedError(what);
220
+ throw error;
221
+ }
91
222
  }
92
- execute(input) {
93
- return callHost(REACTOR_EXECUTE, input);
223
+ async record(value) {
224
+ const { store, stepName } = this.options;
225
+ if (!store) return;
226
+ try {
227
+ await store.put(jobKey(stepName), value, "FLOW", this.afterJobMs());
228
+ } catch {}
229
+ }
230
+ async settle(jobId) {
231
+ let status = "PENDING";
232
+ for (;;) {
233
+ const remaining = this.stopAt - Date.now();
234
+ if (remaining <= 0) throw new ReactorJobPendingError(jobId, status);
235
+ const maxWaitMs = Math.min(MAX_POLL_WAIT_MS, Math.floor(hostCallTimeoutMs() / 2), remaining);
236
+ let state;
237
+ try {
238
+ state = await callHost(REACTOR_WAIT, {
239
+ jobId,
240
+ maxWaitMs
241
+ }, this.waitCallMs(maxWaitMs));
242
+ } catch (error) {
243
+ if (error instanceof HostCallTimeoutError) throw new ReactorJobPendingError(jobId, status);
244
+ throw error;
245
+ }
246
+ if (state.status === "READ_READY") return state;
247
+ if (state.status === "FAILED") throw new ReactorJobFailedError(jobId, state.error);
248
+ if (state.status === "UNKNOWN") throw new ReactorJobPendingError(jobId, "UNKNOWN");
249
+ status = state.status;
250
+ }
94
251
  }
95
252
  };
96
253
  //#endregion
@@ -395,13 +552,13 @@ async function resolveDynamicProperty(params) {
395
552
  //#endregion
396
553
  //#region src/pieces/activepieces/context/remote-store.ts
397
554
  var RemoteKeyValueStore = class {
398
- async put(key, value, scope) {
555
+ async put(key, value, scope, timeoutMs) {
399
556
  const stored = jsonSafe(value);
400
557
  await callHost(STORE_PUT, {
401
558
  key,
402
559
  value: stored,
403
560
  scope
404
- });
561
+ }, timeoutMs);
405
562
  return stored;
406
563
  }
407
564
  get(key, scope) {
@@ -519,7 +676,7 @@ async function handleResolveOptions(message) {
519
676
  searchValue: request.searchValue,
520
677
  projectId: request.projectId,
521
678
  flows: { list: () => Promise.resolve({ data: [] }) },
522
- ...request.reactorAccess ? { reactor: new RemoteReactorService() } : {}
679
+ ...request.reactorAccess ? { reactor: new RemoteReactorService({ deadline: request.deadline }) } : {}
523
680
  });
524
681
  const refresherValues = {
525
682
  ...request.auth !== void 0 ? { auth: request.auth } : {},
@@ -574,7 +731,11 @@ async function handleRun(message) {
574
731
  const files = request.stagingDir ? new StagedFilesService(request.stagingDir) : new DataUriFilesService();
575
732
  const durableStore = request.durableStore ? new RemoteKeyValueStore() : void 0;
576
733
  const liveOutput = request.liveOutput ? new RemoteOutput() : void 0;
577
- const reactor = request.reactorAccess ? new RemoteReactorService() : void 0;
734
+ const reactor = request.reactorAccess ? new RemoteReactorService({
735
+ deadline: request.deadline,
736
+ store: durableStore,
737
+ stepName: request.identity?.stepName
738
+ }) : void 0;
578
739
  const restoreConsole = request.captureLogs ? captureConsole() : void 0;
579
740
  const { context, touched } = buildActionContext({
580
741
  propsValue: await preparePropsValue(`action "${request.actionName}"`, action.props, request.propsValue, { resolveRef: stagedInputResolver(request.stagedInputs) }),
@@ -730,6 +891,7 @@ process.on("message", (message) => {
730
891
  if (!isWorkerMessage(message)) return;
731
892
  Promise.resolve().then(() => {
732
893
  setMaxFileBytes(message.request.maxFileBytes);
894
+ setHostCallTimeout(message.request.hostCallTimeoutMs);
733
895
  return runWithEgressPolicy(message.request.egress, () => dispatch(message));
734
896
  }).catch((error) => ({
735
897
  id: message.id,