@powerhousedao/reactor-workflow 6.2.3-dev.44 → 6.2.3-dev.45

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,4 +1,4 @@
1
- import { A as REACTOR_FIND, B as buildDescriptor, D as jsonSafe, F as REACTOR_SUBMIT_CREATE, I as REACTOR_WAIT, L as STORE_DELETE, M as REACTOR_MODEL, N as REACTOR_MODELS, O as LOG_WRITE, P as REACTOR_SUBMIT, R as STORE_GET, Y as getActions, a as redactMessage, c as DEFAULT_EGRESS_POLICY, i as redactError, j as REACTOR_GET, k as OUTPUT_UPDATE, n as containsRedactedMarker, o as rememberSecrets, r as redact, s as secretsFor, t as collectSecretValues, v as rewriteFileRefs, x as configuredMaxFileBytes, z as STORE_PUT } from "./redact-BlKAG3f0.js";
1
+ import { L as jsonSafe, O as rewriteFileRefs, R as buildDescriptor, S as STORE_PUT, _ as MODEL_MANIFEST, a as redact, b as STORE_DELETE, c as rememberSecrets, h as LOG_WRITE, i as containsRedactedMarker, j as configuredMaxFileBytes, l as secretsFor, o as redactError, q as getActions, r as collectSecretValues, s as redactMessage, t as createIpcTransport, u as DEFAULT_EGRESS_POLICY, v as OUTPUT_UPDATE, x as STORE_GET } from "./reactor-rpc-BqAjCK1p.js";
2
2
  import { AppConnectionType, PieceCategory, Property, TriggerStrategy, createAction, createPiece, createTrigger } from "@powerhousedao/pieces-framework";
3
3
  import { execFile, fork } from "node:child_process";
4
4
  import { existsSync } from "node:fs";
@@ -8,7 +8,7 @@ import { promisify } from "node:util";
8
8
  import { gunzip } from "node:zlib";
9
9
  import { childLogger } from "document-model";
10
10
  import { fileURLToPath } from "node:url";
11
- import { BRANCH_PORTS, CORE_PIECE_NAME, CORE_PIECE_NAME as CORE_PIECE_NAME$1, NEXT_PORT, PIECE_ACTION_PORTS, SCHEDULE_MODES, SCHEDULE_UNIT_MS, TRIGGER_PORTS, undeclaredPortEdges } from "@powerhousedao/pieces-framework/workflow";
11
+ import { BRANCH_PORTS, CORE_PIECE_NAME, CORE_PIECE_NAME as CORE_PIECE_NAME$1, NEXT_PORT, PIECE_ACTION_PORTS, SCHEDULE_MODES, SCHEDULE_UNIT_MS, TRIGGER_PORTS, referenceDocuments, undeclaredPortEdges } from "@powerhousedao/pieces-framework/workflow";
12
12
  import { createHash, randomUUID } from "node:crypto";
13
13
  import { blockKey, isExactVersion } from "@powerhousedao/pieces-framework/block-type";
14
14
  //#region src/pieces/activepieces/registry-source.ts
@@ -493,6 +493,7 @@ var PieceWorker = class {
493
493
  connect;
494
494
  defaultTimeoutMs;
495
495
  hostCallTimeoutMs;
496
+ models;
496
497
  worker;
497
498
  queue = Promise.resolve();
498
499
  nextId = 1;
@@ -501,6 +502,7 @@ var PieceWorker = class {
501
502
  this.connect = options.transport ?? (() => createForkTransport(entryPath ?? defaultEntryPath()));
502
503
  this.defaultTimeoutMs = options.defaultTimeoutMs ?? 3e4;
503
504
  this.hostCallTimeoutMs = options.hostCallTimeoutMs;
505
+ this.models = options.models;
504
506
  }
505
507
  runAction(request, options = {}) {
506
508
  return this.enqueue("run", request, options.timeoutMs, options);
@@ -529,6 +531,7 @@ var PieceWorker = class {
529
531
  spawn() {
530
532
  if (this.worker) return this.worker;
531
533
  const worker = this.connect();
534
+ this.sendModels(worker);
532
535
  const forget = () => {
533
536
  if (this.worker === worker) this.worker = void 0;
534
537
  worker.off("exit", forget);
@@ -537,11 +540,55 @@ var PieceWorker = class {
537
540
  this.worker = worker;
538
541
  return worker;
539
542
  }
543
+ sendModels(worker) {
544
+ const entries = this.models?.entries() ?? [];
545
+ if (entries.length === 0 || !worker.connected) return;
546
+ const message = {
547
+ type: MODEL_MANIFEST,
548
+ entries
549
+ };
550
+ worker.send(jsonSafe(message));
551
+ }
552
+ async modelEntries(payload) {
553
+ const documentType = payload?.documentType;
554
+ return await this.models?.lookup?.(documentType) ?? [];
555
+ }
556
+ openReactor(worker, tap, deadline) {
557
+ const requestId = randomUUID();
558
+ const transport = createIpcTransport(worker, requestId);
559
+ let stopServing;
560
+ try {
561
+ stopServing = tap.open(transport, {
562
+ requestId,
563
+ deadline
564
+ });
565
+ } catch (error) {
566
+ transport.close();
567
+ throw error;
568
+ }
569
+ return {
570
+ binding: {
571
+ requestId,
572
+ requireReactor: tap.requireReactor
573
+ },
574
+ stop: () => {
575
+ transport.close();
576
+ stopServing();
577
+ }
578
+ };
579
+ }
540
580
  execute(type, request, timeoutMs, taps) {
541
581
  const worker = this.spawn();
542
582
  const id = this.nextId++;
543
583
  return new Promise((resolve, reject) => {
544
584
  const deadline = Date.now() + timeoutMs;
585
+ let reactor;
586
+ if (taps.reactor && type !== "check-connection" && type !== "describe") try {
587
+ reactor = this.openReactor(worker, taps.reactor, deadline);
588
+ } catch (error) {
589
+ reject(error instanceof Error ? error : new Error(String(error)));
590
+ return;
591
+ }
545
592
  const timer = setTimeout(() => {
546
593
  cleanup();
547
594
  worker.kill();
@@ -550,7 +597,7 @@ var PieceWorker = class {
550
597
  }, timeoutMs);
551
598
  const onMessage = (value) => {
552
599
  const response = value;
553
- if (response.type === "host-call" || response.type === "host-notify") return;
600
+ if (response.type === "host-call" || response.type === "host-notify" || response.type === "reactor-rpc") return;
554
601
  if (response.id !== id) return;
555
602
  cleanup();
556
603
  if (response.type === "result") resolve({
@@ -577,6 +624,8 @@ var PieceWorker = class {
577
624
  if (message.type === "host-notify") serveNotify(message, taps.notifications);
578
625
  };
579
626
  const cleanup = () => {
627
+ reactor?.stop();
628
+ reactor = void 0;
580
629
  clearTimeout(timer);
581
630
  worker.off("message", onMessage);
582
631
  worker.off("message", onHostCall);
@@ -592,7 +641,8 @@ var PieceWorker = class {
592
641
  maxFileBytes: configuredMaxFileBytes(),
593
642
  deadline,
594
643
  ...this.hostCallTimeoutMs ? { hostCallTimeoutMs: this.hostCallTimeoutMs } : {},
595
- ...request
644
+ ...request,
645
+ ...reactor ? { reactor: reactor.binding } : {}
596
646
  }
597
647
  }));
598
648
  });
@@ -600,7 +650,7 @@ var PieceWorker = class {
600
650
  async serveHostCall(worker, message, handlers) {
601
651
  let response;
602
652
  try {
603
- const handler = handlers?.[message.method];
653
+ const handler = message.method === "model-entries" ? (payload) => this.modelEntries(payload) : handlers?.[message.method];
604
654
  if (!handler) throw new Error(`No host handler for "${message.method}"`);
605
655
  response = {
606
656
  id: message.id,
@@ -1333,6 +1383,15 @@ function declaredConnectionIds(definition) {
1333
1383
  for (const step of definition.steps) declare(step.connectionId);
1334
1384
  return declared;
1335
1385
  }
1386
+ function declaredReactorConnectionIds(definition) {
1387
+ const declared = /* @__PURE__ */ new Set();
1388
+ const declare = (connectionId) => {
1389
+ if (connectionId && !connectionId.includes("{{")) declared.add(connectionId);
1390
+ };
1391
+ declare(definition.trigger?.reactorConnectionId);
1392
+ for (const step of definition.steps) declare(step.reactorConnectionId);
1393
+ return declared;
1394
+ }
1336
1395
  var BoundConnectionResolver = class {
1337
1396
  inner;
1338
1397
  binding;
@@ -1687,7 +1746,7 @@ const manualTrigger = createTrigger({
1687
1746
  });
1688
1747
  //#endregion
1689
1748
  //#region src/pieces/core/version.ts
1690
- const CORE_PIECE_VERSION = "6.2.3-dev.44";
1749
+ const CORE_PIECE_VERSION = "6.2.3-dev.45";
1691
1750
  //#endregion
1692
1751
  //#region src/pieces/core/index.ts
1693
1752
  const corePiece = createPiece({
@@ -1825,101 +1884,6 @@ function storeKeyOf(payload) {
1825
1884
  if (typeof key !== "string" || key === "") throw new Error("Store call carried no key");
1826
1885
  return key;
1827
1886
  }
1828
- const MAX_REACTOR_WAIT_MS = 5e3;
1829
- function waitMs(value) {
1830
- if (typeof value !== "number" || !Number.isFinite(value)) return 0;
1831
- return Math.min(Math.max(Math.floor(value), 0), MAX_REACTOR_WAIT_MS);
1832
- }
1833
- function reactorInput(payload) {
1834
- if (typeof payload !== "object" || payload === null || Array.isArray(payload)) throw new Error("Reactor call carried no input object");
1835
- return payload;
1836
- }
1837
- function requiredString(payload, field) {
1838
- const value = payload[field];
1839
- if (typeof value !== "string" || value === "") throw new Error(`Reactor call carried no "${field}"`);
1840
- return value;
1841
- }
1842
- function optionalString(payload, field) {
1843
- const value = payload[field];
1844
- return typeof value === "string" && value !== "" ? value : void 0;
1845
- }
1846
- function reactorActions(payload) {
1847
- const actions = payload.actions;
1848
- if (!Array.isArray(actions) || actions.length === 0) throw new Error("Reactor call carried no actions");
1849
- return actions.map((entry, index) => {
1850
- const action = entry;
1851
- if (!action || typeof action.type !== "string") throw new Error(`Reactor call: actions[${index}] needs a string "type"`);
1852
- return {
1853
- type: action.type,
1854
- input: action.input,
1855
- ...typeof action.scope === "string" ? { scope: action.scope } : {}
1856
- };
1857
- });
1858
- }
1859
- const MAX_FIND_LIMIT = 100;
1860
- function findLimit(value) {
1861
- if (typeof value !== "number" || !Number.isFinite(value)) return void 0;
1862
- return Math.min(Math.max(Math.floor(value), 1), MAX_FIND_LIMIT);
1863
- }
1864
- function findMatch(value) {
1865
- if (typeof value !== "object" || value === null) return void 0;
1866
- const record = value;
1867
- const path = record.path;
1868
- const wanted = record.value;
1869
- if (typeof path !== "string" || path.trim() === "") return void 0;
1870
- if (typeof wanted !== "string") return void 0;
1871
- return {
1872
- path: path.trim(),
1873
- value: wanted
1874
- };
1875
- }
1876
- function executeInput(payload) {
1877
- const input = reactorInput(payload);
1878
- return {
1879
- documentId: requiredString(input, "documentId"),
1880
- ...optionalString(input, "branch") ? { branch: optionalString(input, "branch") } : {},
1881
- actions: reactorActions(input)
1882
- };
1883
- }
1884
- function reactorHandlers(port) {
1885
- return {
1886
- [REACTOR_MODELS]: () => port.models(),
1887
- [REACTOR_MODEL]: (payload) => port.model(requiredString(reactorInput(payload), "documentType")),
1888
- [REACTOR_GET]: (payload) => {
1889
- const input = reactorInput(payload);
1890
- return port.get({
1891
- documentId: requiredString(input, "documentId"),
1892
- ...optionalString(input, "branch") ? { branch: optionalString(input, "branch") } : {}
1893
- });
1894
- },
1895
- [REACTOR_FIND]: (payload) => {
1896
- const input = reactorInput(payload);
1897
- return port.find({
1898
- ...optionalString(input, "documentType") ? { documentType: optionalString(input, "documentType") } : {},
1899
- ...optionalString(input, "parentId") ? { parentId: optionalString(input, "parentId") } : {},
1900
- ...findLimit(input.limit) !== void 0 ? { limit: findLimit(input.limit) } : {},
1901
- ...findMatch(input.match) ? { match: findMatch(input.match) } : {},
1902
- ...input.withState === true ? { withState: true } : {}
1903
- });
1904
- },
1905
- [REACTOR_SUBMIT_CREATE]: (payload) => {
1906
- const input = reactorInput(payload);
1907
- return port.submitCreate({
1908
- documentType: requiredString(input, "documentType"),
1909
- ...optionalString(input, "name") ? { name: optionalString(input, "name") } : {},
1910
- ...optionalString(input, "parentId") ? { parentId: optionalString(input, "parentId") } : {}
1911
- });
1912
- },
1913
- [REACTOR_SUBMIT]: (payload) => port.submit(executeInput(payload)),
1914
- [REACTOR_WAIT]: (payload) => {
1915
- const input = reactorInput(payload);
1916
- return port.wait({
1917
- jobId: requiredString(input, "jobId"),
1918
- maxWaitMs: waitMs(input.maxWaitMs)
1919
- });
1920
- }
1921
- };
1922
- }
1923
1887
  function storeHandlers(port) {
1924
1888
  return {
1925
1889
  [STORE_GET]: (payload) => port.get(storeKeyOf(payload), storeScopeOf(payload)),
@@ -1956,12 +1920,9 @@ function redactThrown(error, values) {
1956
1920
  }
1957
1921
  return rememberSecrets(redactError(error, { values }), values);
1958
1922
  }
1959
- const REACTOR_PORT_PIECE = "@powerhousedao/piece-reactor";
1960
- function servesReactorPort(packageName) {
1961
- return packageName === REACTOR_PORT_PIECE;
1962
- }
1923
+ const HOST_SHIPPED_PIECE = "@powerhousedao/piece-reactor";
1963
1924
  function isHostBound(packageName) {
1964
- return servesReactorPort(packageName) || isBuiltinPiece(packageName);
1925
+ return packageName === HOST_SHIPPED_PIECE || isBuiltinPiece(packageName);
1965
1926
  }
1966
1927
  function resolvedBlock(resolution) {
1967
1928
  const { requested, resolved } = resolution;
@@ -2039,7 +2000,10 @@ var ActivepiecesBlockExecutor = class {
2039
2000
  const pieceStore = this.options.pieceStore;
2040
2001
  const notifications = stepTaps(this.options, execution, redactValues);
2041
2002
  const egress = this.options.egress === void 0 ? DEFAULT_EGRESS_POLICY : this.options.egress;
2042
- const reactor = servesReactorPort(parsed.packageName) ? this.options.reactor : void 0;
2003
+ const reactorTap = await this.options.reactorAccess?.({
2004
+ block: parsed,
2005
+ reactorConnectionId: execution.reactorConnectionId
2006
+ });
2043
2007
  const result = await this.worker().runAction({
2044
2008
  ...pieceModuleRef(piece),
2045
2009
  actionName: parsed.name,
@@ -2054,17 +2018,14 @@ var ActivepiecesBlockExecutor = class {
2054
2018
  ...stagingDir ? { stagingDir } : {},
2055
2019
  ...stagedInputs ? { stagedInputs } : {},
2056
2020
  ...pieceStore ? { durableStore: true } : {},
2057
- ...reactor ? { reactorAccess: true } : {},
2058
2021
  ...this.options.onPieceLog ? { captureLogs: true } : {},
2059
2022
  ...this.options.onPartialOutput ? { liveOutput: true } : {},
2060
2023
  ...egress ? { egress } : {}
2061
2024
  }, {
2062
2025
  ...timeoutMs ? { timeoutMs } : {},
2063
- ...pieceStore || reactor ? { hostCalls: {
2064
- ...pieceStore ? storeHandlers(pieceStore) : {},
2065
- ...reactor ? reactorHandlers(reactor) : {}
2066
- } } : {},
2067
- ...notifications ? { notifications } : {}
2026
+ ...pieceStore ? { hostCalls: storeHandlers(pieceStore) } : {},
2027
+ ...notifications ? { notifications } : {},
2028
+ ...reactorTap ? { reactor: reactorTap } : {}
2068
2029
  });
2069
2030
  return {
2070
2031
  output: await this.ingestFiles(result.output, result.files),
@@ -2209,7 +2170,8 @@ function stepConfigHash(step) {
2209
2170
  return hashOf(step.pieceName, step.pieceVersion, step.actionName, canonicalJson({
2210
2171
  config: step.config ?? {},
2211
2172
  connectionId: step.connectionId ?? null,
2212
- propertySettings: schemas
2173
+ propertySettings: schemas,
2174
+ ...step.reactorConnectionId ? { reactorConnectionId: step.reactorConnectionId } : {}
2213
2175
  }));
2214
2176
  }
2215
2177
  //#endregion
@@ -2239,6 +2201,10 @@ function errorMessage(error) {
2239
2201
  if (error instanceof Error) return error.message;
2240
2202
  return String(error);
2241
2203
  }
2204
+ function errorNameOf(error) {
2205
+ if (error instanceof PieceWorkerError) return error.serialized.name;
2206
+ return error instanceof Error ? error.name : void 0;
2207
+ }
2242
2208
  function journaled(value, values) {
2243
2209
  return value === void 0 ? void 0 : redact(value, { values });
2244
2210
  }
@@ -2253,6 +2219,7 @@ async function runWorkflow(options) {
2253
2219
  const records = /* @__PURE__ */ new Map();
2254
2220
  const edgeDecisions = /* @__PURE__ */ new Map();
2255
2221
  let runFailed;
2222
+ let runFailedName;
2256
2223
  let executedCount = 0;
2257
2224
  const journal = async (record) => {
2258
2225
  if (!options.onStep) return;
@@ -2351,6 +2318,7 @@ async function runWorkflow(options) {
2351
2318
  block: stepBlock(step),
2352
2319
  config: input,
2353
2320
  connectionId: step.connectionId,
2321
+ reactorConnectionId: step.reactorConnectionId,
2354
2322
  step,
2355
2323
  ...runSecrets.length > 0 ? { redactValues: runSecrets } : {}
2356
2324
  });
@@ -2363,7 +2331,7 @@ async function runWorkflow(options) {
2363
2331
  blockName: step.actionName,
2364
2332
  status: "SUCCEEDED",
2365
2333
  input: journaled(input, values),
2366
- output: journaled(result.output, values),
2334
+ output: journaled(referenceDocuments(result.output), values),
2367
2335
  port,
2368
2336
  startedAt,
2369
2337
  endedAt: (/* @__PURE__ */ new Date()).toISOString(),
@@ -2377,6 +2345,7 @@ async function runWorkflow(options) {
2377
2345
  } catch (error) {
2378
2346
  const values = [...runSecrets, ...secretsFor(error)];
2379
2347
  const detail = redactMessage(errorMessage(error), { values });
2348
+ const errorName = errorNameOf(error);
2380
2349
  const record = {
2381
2350
  stepId: step.id,
2382
2351
  key: step.key,
@@ -2385,6 +2354,7 @@ async function runWorkflow(options) {
2385
2354
  status: "FAILED",
2386
2355
  input: journaled(input, values),
2387
2356
  error: detail,
2357
+ ...errorName ? { errorName } : {},
2388
2358
  startedAt,
2389
2359
  endedAt: (/* @__PURE__ */ new Date()).toISOString(),
2390
2360
  ...withPiece(pieceRecord(resolutionOf(error))),
@@ -2394,7 +2364,10 @@ async function runWorkflow(options) {
2394
2364
  await journal(record);
2395
2365
  scope.steps[step.key] = { error: detail };
2396
2366
  decideOutgoing(step.id, "error");
2397
- if (!definition.edges.some((edge) => edge.from === step.id && edgeDecisions.get(edge.id))) runFailed = `Step "${step.key}" failed: ${detail}`;
2367
+ if (!definition.edges.some((edge) => edge.from === step.id && edgeDecisions.get(edge.id))) {
2368
+ runFailed = `Step "${step.key}" failed: ${detail}`;
2369
+ runFailedName = errorName;
2370
+ }
2398
2371
  }
2399
2372
  };
2400
2373
  let progressed = true;
@@ -2425,6 +2398,7 @@ async function runWorkflow(options) {
2425
2398
  status: "FAILED",
2426
2399
  steps,
2427
2400
  error: runFailed,
2401
+ ...runFailedName ? { errorName: runFailedName } : {},
2428
2402
  ...noted
2429
2403
  } : {
2430
2404
  status: "SUCCEEDED",
@@ -2479,6 +2453,6 @@ function installedPieces() {
2479
2453
  return [...builtinLocalPieces(), ...packagePieces.entries().filter((piece) => !isBuiltinPiece(piece.name))];
2480
2454
  }
2481
2455
  //#endregion
2482
- export { resolveStepInput as A, PieceWorker as B, describeBuiltinPiece as C, StaticConnectionResolver as D, BoundConnectionResolver as E, parseSecretRef as F, sourcedResolver as G, PieceWorkerTimeoutError as H, secretRefFromId as I, npmRegistryBaseUrl as J, ensurePieceBundle as K, stepBlock as L, InMemorySecretProvider as M, SecretDeletedError as N, declaredConnectionIds as O, SecretNotFoundError as P, triggerBlock as R, builtinPiece as S, CORE_PIECE_VERSION as T, bundleResolver as U, PieceWorkerError as V, pieceModuleRef as W, setPieceRegistryUrl as X, pieceRegistrySource as Y, servesReactorPort as _, runWorkflow as a, unpinnedNote as b, stepConfigHash as c, ActivepiecesBlockExecutor as d, CompositeBlockExecutor as f, resolvedBlock as g, reactorHandlers as h, packagePieces as i, expressionsIn as j, shapeConnection as k, DynamicPropertiesError as l, isHostBound as m, installedPiece as n, canonicalJson as o, REACTOR_PORT_PIECE as p, fetchPieceBundle as q, installedPieces as r, hashOf as s, PieceRegistry as t, checkDynamicProperties as u, storeHandlers as v, CORE_PIECE_NAME$1 as w, blockPorts as x, blockLabel as y, PieceWorkerPool as z };
2456
+ export { expressionsIn as A, PieceWorkerError as B, CORE_PIECE_VERSION as C, declaredReactorConnectionIds as D, declaredConnectionIds as E, secretRefFromId as F, ensurePieceBundle as G, bundleResolver as H, stepBlock as I, pieceRegistrySource as J, fetchPieceBundle as K, triggerBlock as L, SecretDeletedError as M, SecretNotFoundError as N, shapeConnection as O, parseSecretRef as P, PieceWorkerPool as R, CORE_PIECE_NAME$1 as S, StaticConnectionResolver as T, pieceModuleRef as U, PieceWorkerTimeoutError as V, sourcedResolver as W, setPieceRegistryUrl as Y, blockLabel as _, errorNameOf as a, builtinPiece as b, hashOf as c, checkDynamicProperties as d, ActivepiecesBlockExecutor as f, storeHandlers as g, resolvedBlock as h, packagePieces as i, InMemorySecretProvider as j, resolveStepInput as k, stepConfigHash as l, isHostBound as m, installedPiece as n, runWorkflow as o, CompositeBlockExecutor as p, npmRegistryBaseUrl as q, installedPieces as r, canonicalJson as s, PieceRegistry as t, DynamicPropertiesError as u, unpinnedNote as v, BoundConnectionResolver as w, describeBuiltinPiece as x, blockPorts as y, PieceWorker as z };
2483
2457
 
2484
- //# sourceMappingURL=piece-registry-DMhdxKrs.js.map
2458
+ //# sourceMappingURL=piece-registry-Cv8Zw8ex.js.map