@powerhousedao/reactor-workflow 6.2.3-dev.76 → 6.2.3-dev.78

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/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { $ as rememberSecrets, A as AttachmentCache, B as SecretNotFoundError, C as blockLabel, D as describeBuiltinPiece, E as builtinPiece, F as shapeConnection, G as PieceWorkerPool, H as secretRefFromId, I as resolveStepInput, J as PieceWorkerError, K as PieceWorker, L as expressionsIn, N as declaredConnectionIds, O as CORE_PIECE_NAME, P as declaredReactorConnectionIds, Q as redactMessage, S as storeHandlers, T as blockPorts, U as stepBlock, V as parseSecretRef, W as triggerBlock, X as containsRedactedMarker, Y as PieceWorkerTimeoutError, Z as redact, _ as effectiveRetryPolicy, _t as npmRegistryBaseUrl, a as PieceRegistry, at as FileTooLargeError, b as isHostBound, bt as isIndeterminateError, c as packagePieces, ct as pieceModuleRef, d as runWorkflow, dt as UnsupportedPieceFeatureError, et as secretsFor, f as canonicalJson, ft as authMethodFor, g as checkDynamicProperties, h as DynamicPropertiesError, i as tracedSteps, it as extractDedupeKey, j as BoundConnectionResolver, k as CORE_PIECE_VERSION, lt as sourcedResolver, m as stepConfigHash, mt as unsupportedTrigger, n as WorkflowTelemetry, nt as isPrivateAddress, o as installedPiece, ot as maxFileBytes, p as hashOf, pt as unsupportedAuth, q as PieceWorkerDisposedError, r as tracedMethods, rt as parseAddress, s as installedPieces, st as bundleResolver, t as TELEMETRY_SCOPE, tt as DEFAULT_EGRESS_POLICY, u as errorNameOf, v as ActivepiecesBlockExecutor, vt as pieceRegistrySource, w as unpinnedNote, x as resolvedBlock, y as CompositeBlockExecutor, yt as setPieceRegistryUrl, z as SecretDeletedError } from "./telemetry-C5k5N8d4.js";
1
+ import { $ as rememberSecrets, A as AttachmentCache, B as SecretNotFoundError, C as blockLabel, D as describeBuiltinPiece, E as builtinPiece, F as shapeConnection, G as PieceWorkerPool, H as secretRefFromId, I as resolveStepInput, J as PieceWorkerError, K as PieceWorker, L as expressionsIn, N as declaredConnectionIds, O as CORE_PIECE_NAME, P as declaredReactorConnectionIds, Q as redactMessage, S as storeHandlers, T as blockPorts, U as stepBlock, V as parseSecretRef, W as triggerBlock, X as containsRedactedMarker, Y as PieceWorkerTimeoutError, Z as redact, _ as effectiveRetryPolicy, _t as npmRegistryBaseUrl, a as PieceRegistry, at as FileTooLargeError, b as isHostBound, bt as isIndeterminateError, c as packagePieces, ct as pieceModuleRef, d as runWorkflow, dt as UnsupportedPieceFeatureError, et as secretsFor, f as canonicalJson, ft as authMethodFor, g as checkDynamicProperties, h as DynamicPropertiesError, i as tracedSteps, it as extractDedupeKey, j as BoundConnectionResolver, k as CORE_PIECE_VERSION, lt as sourcedResolver, m as stepConfigHash, mt as unsupportedTrigger, n as WorkflowTelemetry, nt as isPrivateAddress, o as installedPiece, ot as maxFileBytes, p as hashOf, pt as unsupportedAuth, q as PieceWorkerDisposedError, r as tracedMethods, rt as parseAddress, s as installedPieces, st as bundleResolver, t as TELEMETRY_SCOPE, tt as DEFAULT_EGRESS_POLICY, u as errorNameOf, v as ActivepiecesBlockExecutor, vt as pieceRegistrySource, w as unpinnedNote, x as resolvedBlock, y as CompositeBlockExecutor, yt as setPieceRegistryUrl, z as SecretDeletedError } from "./telemetry-CiXdMoVa.js";
2
2
  import { ReactorAccessDeniedError, ReactorAccessDeniedError as ReactorAccessDeniedError$1, ReactorActionsFailedError, ReactorJobFailedError, ReactorJobPendingError as ReactorJobPendingError$1, WebhookHandshakeStrategy } from "@powerhousedao/pieces-framework";
3
3
  import { mkdirSync, openAsBlob, readFileSync, writeFileSync } from "node:fs";
4
4
  import { open, readdir, rm, stat } from "node:fs/promises";
@@ -1988,6 +1988,55 @@ function assertActionsApplied(job, actions) {
1988
1988
  });
1989
1989
  if (failed.length > 0) throw new ReactorError(ReactorActionsFailedError, failed.join("; "));
1990
1990
  }
1991
+ const DOCUMENT_DRIVE = "powerhouse/document-drive";
1992
+ const REACTOR_DRIVE = "powerhouse/reactor-drive";
1993
+ function isDriveType(documentType) {
1994
+ return documentType === DOCUMENT_DRIVE || documentType === REACTOR_DRIVE;
1995
+ }
1996
+ function fileCandidate(drive, document, parentFolder) {
1997
+ const { id, documentType, name } = document.header;
1998
+ if (drive.documentType === DOCUMENT_DRIVE) return {
1999
+ scope: "global",
2000
+ type: "ADD_FILE",
2001
+ input: {
2002
+ id,
2003
+ name: name || id,
2004
+ documentType,
2005
+ parentFolder
2006
+ }
2007
+ };
2008
+ return {
2009
+ scope: "document",
2010
+ type: "ADD_RELATIONSHIP",
2011
+ input: {
2012
+ sourceId: drive.id,
2013
+ targetId: id,
2014
+ relationshipType: "drive/child",
2015
+ metadata: {
2016
+ kind: "file",
2017
+ parentFolderId: parentFolder ?? null,
2018
+ documentType
2019
+ }
2020
+ }
2021
+ };
2022
+ }
2023
+ function folderCandidate(drive, name, parentFolder) {
2024
+ return drive.documentType === DOCUMENT_DRIVE ? {
2025
+ scope: "global",
2026
+ type: "ADD_FOLDER",
2027
+ input: {
2028
+ name,
2029
+ parentFolder
2030
+ }
2031
+ } : {
2032
+ scope: "document",
2033
+ type: "ADD_FOLDER",
2034
+ input: {
2035
+ name,
2036
+ parentFolderId: parentFolder ?? null
2037
+ }
2038
+ };
2039
+ }
1991
2040
  var RunScopedReactorClient = class {
1992
2041
  inner;
1993
2042
  scope;
@@ -2066,16 +2115,27 @@ var RunScopedReactorClient = class {
2066
2115
  }
2067
2116
  async execute(documentIdentifier, branch, actions, signal, subject) {
2068
2117
  if (subject !== void 0) throw accessDenied("A piece may not choose the subject it reads as");
2069
- this.assertWritable("execute");
2070
2118
  const abort = this.signal(signal);
2119
+ const { id } = await this.apply("execute", documentIdentifier, branch, actions, abort);
2120
+ return this.readBack(id, branch, abort);
2121
+ }
2122
+ async executeAsync(documentIdentifier, branch, actions, signal) {
2123
+ const abort = this.signal(signal);
2124
+ const { job } = await this.apply("executeAsync", documentIdentifier, branch, actions, abort);
2125
+ return job;
2126
+ }
2127
+ async apply(method, documentIdentifier, branch, actions, abort) {
2128
+ this.assertWritable(method);
2071
2129
  for (const action of actions) {
2072
2130
  if (action.context?.signer) throw accessDenied(`Action ${action.type} carries a signer; the host signs every action`);
2073
2131
  if (DOCUMENT_SCOPE_ACTION_TYPES.has(action.type)) throw accessDenied(`${action.type} is a document lifecycle action, which pieces may not execute`);
2074
2132
  }
2075
2133
  const id = await this.resolveReference(documentIdentifier, branch, abort);
2076
2134
  await this.gate(id, branch, actions, abort);
2077
- await this.run(id, branch, actions, abort);
2078
- return this.readBack(id, branch, abort);
2135
+ return {
2136
+ id,
2137
+ job: await this.run(id, branch, actions, abort)
2138
+ };
2079
2139
  }
2080
2140
  async deleteDocument(identifier, propagate, signal) {
2081
2141
  this.assertWritable("deleteDocument");
@@ -2093,6 +2153,58 @@ var RunScopedReactorClient = class {
2093
2153
  throw error;
2094
2154
  }
2095
2155
  }
2156
+ drives = {
2157
+ getNode: (driveIdentifier, nodeId, signal) => this.getNode(driveIdentifier, nodeId, signal),
2158
+ listNodes: (driveIdentifier, parentFolder, paging, signal) => this.listNodes(driveIdentifier, parentFolder, paging, signal),
2159
+ addFile: (driveIdentifier, document, parentFolder, signal) => this.addFile(driveIdentifier, document, parentFolder, signal),
2160
+ addFolder: (driveIdentifier, name, parentFolder, signal) => this.addFolder(driveIdentifier, name, parentFolder, signal)
2161
+ };
2162
+ async getNode(driveIdentifier, nodeId, signal) {
2163
+ const abort = this.signal(signal);
2164
+ const drive = await this.readDrive(driveIdentifier, abort);
2165
+ return this.inner.drives.getNode(drive.id, nodeId, abort);
2166
+ }
2167
+ async listNodes(driveIdentifier, parentFolder, paging, signal) {
2168
+ const abort = this.signal(signal);
2169
+ const drive = await this.readDrive(driveIdentifier, abort);
2170
+ return this.inner.drives.listNodes(drive.id, parentFolder, this.paging(paging), abort);
2171
+ }
2172
+ async addFile(driveIdentifier, document, parentFolder, signal) {
2173
+ this.assertWritable("drives.addFile");
2174
+ const abort = this.signal(signal);
2175
+ const drive = await this.readDrive(driveIdentifier, abort);
2176
+ const prepared = withSignaturePolicy(document, await this.inner.getCreateSignaturePolicy());
2177
+ const documentId = prepared.header.id;
2178
+ await this.gateCandidates(drive.id, "main", [fileCandidate(drive, prepared, parentFolder)], abort);
2179
+ this.assertTimeLeft(`a ${prepared.header.documentType} document`);
2180
+ await this.scope.journal.recordDocuments([drive.id, documentId]);
2181
+ await this.inner.drives.addFile(drive.id, prepared, parentFolder, abort);
2182
+ try {
2183
+ await this.grantRunUser(documentId, abort);
2184
+ return await this.readBack(documentId, prepared.header.branch || "main", abort);
2185
+ } catch (error) {
2186
+ if (error instanceof Error) error.message = `Document ${documentId} was created, but: ${error.message}`;
2187
+ throw error;
2188
+ }
2189
+ }
2190
+ async addFolder(driveIdentifier, name, parentFolder, signal) {
2191
+ this.assertWritable("drives.addFolder");
2192
+ const abort = this.signal(signal);
2193
+ const drive = await this.readDrive(driveIdentifier, abort);
2194
+ await this.gateCandidates(drive.id, "main", [folderCandidate(drive, name, parentFolder)], abort);
2195
+ this.assertTimeLeft("a folder");
2196
+ await this.scope.journal.recordDocuments([drive.id]);
2197
+ return this.inner.drives.addFolder(drive.id, name, parentFolder, abort);
2198
+ }
2199
+ async readDrive(driveIdentifier, signal) {
2200
+ const { id, documentType } = (await this.inner.get(driveIdentifier, this.readView({ scopes: ["document"] }), signal)).header;
2201
+ if (!isDriveType(documentType)) throw accessDenied(`Document ${id} is a ${documentType}, not a drive`);
2202
+ await this.scope.journal.recordDocuments([id]);
2203
+ return {
2204
+ id,
2205
+ documentType
2206
+ };
2207
+ }
2096
2208
  get subject() {
2097
2209
  return this.scope.runUser?.subject;
2098
2210
  }
@@ -2123,12 +2235,14 @@ var RunScopedReactorClient = class {
2123
2235
  };
2124
2236
  }
2125
2237
  async gate(documentId, branch, actions, signal) {
2126
- if (!authEnforced(this.host) || actions.length === 0) return;
2127
- const candidates = actions.map((action) => ({
2238
+ await this.gateCandidates(documentId, branch, actions.map((action) => ({
2128
2239
  scope: action.scope,
2129
2240
  type: action.type,
2130
2241
  input: action.input
2131
- }));
2242
+ })), signal);
2243
+ }
2244
+ async gateCandidates(documentId, branch, candidates, signal) {
2245
+ if (!authEnforced(this.host) || candidates.length === 0) return;
2132
2246
  let answer;
2133
2247
  try {
2134
2248
  answer = await this.inner.evaluateActions(documentId, branch, candidates, this.subject, signal);
@@ -2145,6 +2259,7 @@ var RunScopedReactorClient = class {
2145
2259
  const settled = await this.settle(info.id, signal);
2146
2260
  if (settled.status === JobStatus.FAILED) throw jobFailed(settled);
2147
2261
  assertActionsApplied(settled, actions);
2262
+ return settled;
2148
2263
  }
2149
2264
  assertTimeLeft(what) {
2150
2265
  if (Date.now() >= this.stopAt) throw new Error(`No time was left before the step deadline to submit ${what}; nothing was submitted`);
@@ -2271,9 +2386,25 @@ const SERVED = {
2271
2386
  create: true,
2272
2387
  createEmpty: true,
2273
2388
  execute: true,
2389
+ executeAsync: true,
2274
2390
  deleteDocument: true
2275
2391
  };
2392
+ const SERVED_DRIVES = {
2393
+ getNode: true,
2394
+ listNodes: true,
2395
+ addFile: true,
2396
+ addFolder: true
2397
+ };
2398
+ const REFUSED_DRIVES = {
2399
+ create: true,
2400
+ removeNode: true,
2401
+ renameNode: true,
2402
+ moveNode: true,
2403
+ copyNode: true,
2404
+ setPreferredEditorOnNode: true
2405
+ };
2276
2406
  const REFUSED = {
2407
+ getMany: true,
2277
2408
  createDocumentInDrive: true,
2278
2409
  executeBatch: true,
2279
2410
  rename: true,
@@ -2284,7 +2415,6 @@ const REFUSED = {
2284
2415
  moveRelationship: true,
2285
2416
  upgradeDocument: true,
2286
2417
  deleteDocuments: true,
2287
- executeAsync: true,
2288
2418
  createAsync: true,
2289
2419
  createEmptyAsync: true,
2290
2420
  getJobStatus: true,
@@ -2298,11 +2428,15 @@ const REFUSED = {
2298
2428
  getCreateProtocolVersions: true
2299
2429
  };
2300
2430
  function servedClient(client) {
2431
+ const facade = served(client, SERVED, REFUSED, "");
2432
+ facade.drives = served(client.drives, SERVED_DRIVES, REFUSED_DRIVES, "drives.");
2433
+ return facade;
2434
+ }
2435
+ function served(methods, serve, refuse, prefix) {
2301
2436
  const facade = Object.create(null);
2302
- const methods = client;
2303
- for (const name of Object.keys(SERVED)) facade[name] = (...args) => methods[name](...args);
2304
- for (const name of Object.keys(REFUSED)) facade[name] = () => {
2305
- throw refusedMethod(name);
2437
+ for (const name of Object.keys(serve)) facade[name] = (...args) => methods[name](...args);
2438
+ for (const name of Object.keys(refuse)) facade[name] = () => {
2439
+ throw refusedMethod(prefix + name);
2306
2440
  };
2307
2441
  return facade;
2308
2442
  }
@@ -2331,7 +2465,7 @@ function reactorTap(host, base, journal, options = {}, telemetry) {
2331
2465
  journal
2332
2466
  }, host, clientOptions));
2333
2467
  const parent = context.active();
2334
- const server = new ReactorHostServer(telemetry ? tracedMethods(served, telemetry, "reactor", parent) : served, transport);
2468
+ const server = new ReactorHostServer(telemetry ? tracedMethods(served, telemetry, "reactor", parent, ["drives"]) : served, transport);
2335
2469
  server.start();
2336
2470
  return () => server.stop();
2337
2471
  }