@pipelex/sdk 0.47.0 → 0.48.1
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 +9 -7
- package/dist/artifacts.d.ts +4 -3
- package/dist/artifacts.js +20 -14
- package/dist/artifacts.js.map +1 -1
- package/dist/cli/script.d.ts +1 -1
- package/dist/cli/source.d.ts +8 -7
- package/dist/cli/source.js +11 -5
- package/dist/cli/source.js.map +1 -1
- package/dist/client.d.ts +326 -213
- package/dist/client.js +829 -517
- package/dist/client.js.map +1 -1
- package/dist/codegen-check.d.ts +2 -3
- package/dist/codegen-check.js +2 -3
- package/dist/codegen-check.js.map +1 -1
- package/dist/errors.d.ts +5 -2
- package/dist/errors.js +5 -2
- package/dist/errors.js.map +1 -1
- package/dist/hooks/bundle-gather.d.ts +47 -40
- package/dist/hooks/bundle-gather.js +121 -219
- package/dist/hooks/bundle-gather.js.map +1 -1
- package/dist/hooks/check-core.d.ts +70 -6
- package/dist/hooks/check-core.js +188 -29
- package/dist/hooks/check-core.js.map +1 -1
- package/dist/hooks/claude-mthds-check.d.ts +19 -5
- package/dist/hooks/claude-mthds-check.js +20 -53
- package/dist/hooks/claude-mthds-check.js.map +1 -1
- package/dist/hooks/secret-guard.d.ts +38 -0
- package/dist/hooks/secret-guard.js +134 -0
- package/dist/hooks/secret-guard.js.map +1 -0
- package/dist/hooks/validate-stage.d.ts +29 -0
- package/dist/hooks/validate-stage.js +66 -0
- package/dist/hooks/validate-stage.js.map +1 -0
- package/dist/index.d.ts +3 -4
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/method-trees.d.ts +220 -74
- package/dist/method-trees.js +250 -37
- package/dist/method-trees.js.map +1 -1
- package/dist/models.d.ts +162 -59
- package/dist/node-host/node.d.ts +33 -0
- package/dist/node-host/node.js +27 -0
- package/dist/node-host/node.js.map +1 -0
- package/dist/node-host/unavailable.d.ts +8 -0
- package/dist/node-host/unavailable.js +8 -0
- package/dist/node-host/unavailable.js.map +1 -0
- package/dist/node-host.d.ts +53 -0
- package/dist/node-host.js +72 -0
- package/dist/node-host.js.map +1 -0
- package/dist/prepare-inputs.d.ts +46 -27
- package/dist/prepare-inputs.js +123 -42
- package/dist/prepare-inputs.js.map +1 -1
- package/dist/product-models.d.ts +14 -16
- package/dist/upload.d.ts +4 -9
- package/dist/upload.js +13 -16
- package/dist/upload.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/docs/architecture.md +44 -21
- package/docs/cli.md +1 -1
- package/docs/crate-routes.md +43 -21
- package/docs/errors.md +5 -5
- package/docs/hook-bundle.md +27 -1
- package/docs/input-preparation.md +13 -13
- package/docs/method-trees.md +120 -22
- package/docs/run-results.md +5 -2
- package/package.json +8 -2
- package/dist/bundle-resources.d.ts +0 -62
- package/dist/bundle-resources.js +0 -165
- package/dist/bundle-resources.js.map +0 -1
- package/docs/bundle-resources.md +0 -107
package/dist/client.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
//
|
|
1
|
+
// Method-source predicates come from the standard, not a local restatement: which
|
|
2
2
|
// source combinations are legal is an invariant of `RunRequest` itself, so this
|
|
3
3
|
// client and the MTHDS runners cannot disagree about what they reject.
|
|
4
|
-
import {
|
|
4
|
+
import { assertOneMethodSource, parseJsonBody } from "mthds/protocol";
|
|
5
5
|
import { assertArtifactSelection, assertWaitOptions, pollUntilResult, readRunDegradation, RUN_STATUSES, runProgressFault, runStatusFilter, selectionIncludesMainStuff, throwIfAborted, withCheckedProgressDegradations, } from "./runs.js";
|
|
6
6
|
import { ApiResponseError, ApiUnreachableError, MissingMainStuffError, PagingNotTerminatingError, PipelineExecuteTimeoutError, PipelineRequestError, RequestArgumentError, RunLifecycleUnavailableError, RunStillRunningError, } from "./errors.js";
|
|
7
7
|
import { isPlainObject, isValidationItem, readFieldError, readMigration, readProviderMetadata, readRunErrorReport, } from "./error-models.js";
|
|
8
|
-
import { INLINE_TREE_MAX_BYTES, MAX_CONCURRENT_TRANSFERS, announcedTreeLimits, contentsTreeSource, declaresBlobStore, fileByBlob, forEachConcurrently, hashMethodFiles, inlineFilesOf, inlineMapBytes,
|
|
8
|
+
import { HANDSHAKE_TTL_MS, INLINE_TREE_MAX_BYTES, MAX_CONCURRENT_TRANSFERS, REQUEST_SENDING, SAVE_SENDING, announcedTreeLimits, contentsTreeSource, declaresBlobStore, eitherSignal, emptyTreeRefusal, fileByBlob, forEachConcurrently, hashMethodFiles, inlineFilesOf, inlineMapBytes, inlineThreshold, isMethodTreeTooLarge, listDirectoryTree, listerLimitsOf, missingBlobsOf, presentMap, readTreeBytesInto, redactSignedLinks, sha256Hex, treeShapeFault, treeListerFor, treeTotalSize, uniqueBlobs, uploadBlob, writeMethodTreeTo, } from "./method-trees.js";
|
|
9
9
|
import { buildUserAgent } from "./user-agent.js";
|
|
10
10
|
import { uploadFile as uploadFileImpl } from "./upload.js";
|
|
11
|
-
import { encodeBundleResources, hasBundleResources } from "./bundle-resources.js";
|
|
12
11
|
import { prepareInputs as prepareInputsImpl } from "./prepare-inputs.js";
|
|
13
12
|
import { downloadArtifacts as downloadArtifactsImpl, fetchArtifact as fetchArtifactImpl, resolveArtifacts as resolveArtifactsImpl, } from "./artifacts.js";
|
|
14
13
|
import { PipelexExecuteResult, resultsFromExecute } from "./execute-result.js";
|
|
@@ -323,11 +322,13 @@ export class PipelexApiClient {
|
|
|
323
322
|
/** Cached `/v1/version` handshake outcome — whether the durable lifecycle is served. */
|
|
324
323
|
lifecycleAvailable;
|
|
325
324
|
/**
|
|
326
|
-
* The
|
|
327
|
-
*
|
|
328
|
-
*
|
|
325
|
+
* The last `/v1/version` handshake that answered and when it was asked, kept for
|
|
326
|
+
* `HANDSHAKE_TTL_MS` as `mthds`'s `MthdsApiClient` keeps it: what sending a tree reads from it
|
|
327
|
+
* (`blob_store`, the tree limits) changes only with a deploy. A handshake that failed is not
|
|
328
|
+
* kept, so the next call asks again, and a refusal showing it stale drops it sooner
|
|
329
|
+
* (`forgetHandshake`).
|
|
329
330
|
*/
|
|
330
|
-
|
|
331
|
+
versionCache;
|
|
331
332
|
/** The client's request ceiling (`PipelexApiClientOptions.requestTimeoutMs`). */
|
|
332
333
|
requestTimeoutMs;
|
|
333
334
|
/**
|
|
@@ -790,46 +791,44 @@ export class PipelexApiClient {
|
|
|
790
791
|
*
|
|
791
792
|
* The answer is read with `parseExactJson`, so an integer past `Number.MAX_SAFE_INTEGER` in the
|
|
792
793
|
* working memory, the main output included, is a `bigint` holding its exact digits.
|
|
794
|
+
*
|
|
795
|
+
* The method's tree, given as a `directory`, `contents` or `tree`, is prepared before the
|
|
796
|
+
* request: listed, read, and sent inline or by hash after uploading the files the API lacks (see
|
|
797
|
+
* `PipelexRunOptions`). A tree this client refuses (two forms, no file, a directory it cannot
|
|
798
|
+
* list) is a `RequestArgumentError`, and a file that fails to upload a `MethodFileTransferError`,
|
|
799
|
+
* each before the run is asked.
|
|
793
800
|
*/
|
|
794
801
|
async execute(options) {
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
...extensions,
|
|
825
|
-
};
|
|
826
|
-
// Timed on the monotonic clock, which a change of the system's time does not move.
|
|
827
|
-
const startedAt = performance.now();
|
|
802
|
+
return this.executeRun(options);
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* `execute`, stopped by `signal` before each step of the tree's preparation and right before the
|
|
806
|
+
* request is sent, never during it, since a request sent may have started a run. `sending` is
|
|
807
|
+
* the tree already prepared, which the blocking fallback of `startAndWaitForResult` sends as the
|
|
808
|
+
* start sent it rather than preparing it again.
|
|
809
|
+
*/
|
|
810
|
+
async executeRun(options, signal, sending) {
|
|
811
|
+
const parts = this.runParts("execute", options);
|
|
812
|
+
const source = sending?.source ?? (await this.treeToSend("execute", parts.forms, REQUEST_SENDING, signal));
|
|
813
|
+
// Timed on the monotonic clock, which a change of the system's time does not move, from the
|
|
814
|
+
// request itself: the tree's preparation, uploads included, is no part of the gateway's cut-off.
|
|
815
|
+
// Set when the request is sent, which the answer read below always follows.
|
|
816
|
+
let sentAt = 0;
|
|
817
|
+
const res = await this.postMethodRequest(source, "execute", REQUEST_SENDING, { signal }, async (files) => {
|
|
818
|
+
const request = runRequestBody("execute", options, files, parts);
|
|
819
|
+
throwIfAborted(signal);
|
|
820
|
+
sentAt = performance.now();
|
|
821
|
+
try {
|
|
822
|
+
return await this.requestRaw("POST", this.url("execute"), {
|
|
823
|
+
body: request,
|
|
824
|
+
...STARTS_RUN,
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
catch (err) {
|
|
828
|
+
throw executeCutOff(err, sentAt);
|
|
829
|
+
}
|
|
830
|
+
}, sending?.initial);
|
|
828
831
|
try {
|
|
829
|
-
const res = await this.requestRaw("POST", this.url("execute"), {
|
|
830
|
-
body: request,
|
|
831
|
-
...STARTS_RUN,
|
|
832
|
-
});
|
|
833
832
|
this.throwIfExecuteDegraded(res);
|
|
834
833
|
if (res.status < 200 || res.status >= 300) {
|
|
835
834
|
this.throwApiResponseError("POST", "execute", res, STARTS_RUN);
|
|
@@ -844,16 +843,10 @@ export class PipelexApiClient {
|
|
|
844
843
|
return new PipelexExecuteResult(answer);
|
|
845
844
|
}
|
|
846
845
|
catch (err) {
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
//
|
|
850
|
-
|
|
851
|
-
// translate it into a clear, actionable error pointing at start+poll.
|
|
852
|
-
const elapsedMs = performance.now() - startedAt;
|
|
853
|
-
if (isGatewayCutOff(err, elapsedMs)) {
|
|
854
|
-
throw new PipelineExecuteTimeoutError(elapsedMs, { cause: err });
|
|
855
|
-
}
|
|
856
|
-
throw err;
|
|
846
|
+
// The hosted gateway terminates synchronous requests at ~30s. A run that exceeds that comes
|
|
847
|
+
// back as a gateway 503/504 (or a client abort) — translated into a clear, actionable error
|
|
848
|
+
// pointing at start+poll.
|
|
849
|
+
throw executeCutOff(err, sentAt);
|
|
857
850
|
}
|
|
858
851
|
}
|
|
859
852
|
/**
|
|
@@ -875,62 +868,48 @@ export class PipelexApiClient {
|
|
|
875
868
|
* `runMayHaveStarted` is true can be retried unchanged (see
|
|
876
869
|
* `PipelexStartRequestOptions`). A malformed key is a `RequestArgumentError`
|
|
877
870
|
* before anything is sent.
|
|
871
|
+
*
|
|
872
|
+
* The method's tree is prepared before the request as `execute` prepares it. The prepared body
|
|
873
|
+
* is the same for the same tree, so a start retried under its idempotency key sends the body the
|
|
874
|
+
* key was bound to. A start whose hash entries name a blob the API does not hold yet is refused
|
|
875
|
+
* with a `422` `missing_blobs`, which releases its key: send it again unchanged, which asks the
|
|
876
|
+
* blob route and uploads what is missing first.
|
|
878
877
|
*/
|
|
879
878
|
async start(options) {
|
|
879
|
+
return this.startRun(options);
|
|
880
|
+
}
|
|
881
|
+
/**
|
|
882
|
+
* `start`, stopped by `signal` as `executeRun` is, `sending` being the tree already prepared.
|
|
883
|
+
*/
|
|
884
|
+
async startRun(options, signal, sending) {
|
|
880
885
|
const idempotencyKey = idempotencyKeyOf(options);
|
|
881
|
-
const
|
|
882
|
-
const
|
|
883
|
-
const hosted = buildHostedRunExtensions(options);
|
|
884
|
-
if (!options.pipe_code &&
|
|
885
|
-
(!options.mthds_contents || options.mthds_contents.length === 0) &&
|
|
886
|
-
!hasBundlePayload(options) &&
|
|
887
|
-
Object.keys(api).length === 0 &&
|
|
888
|
-
Object.keys(hosted).length === 0 &&
|
|
889
|
-
Object.keys(extensions).length === 0) {
|
|
890
|
-
throw new RequestArgumentError("Either pipe_code, mthds_contents, a method bundle (files/bundle_b64), a method_ref, a hosted method_id or a server-specific extension arg (extra) must be provided to start().");
|
|
891
|
-
}
|
|
892
|
-
assertRunSourcesExclusive(options);
|
|
893
|
-
assertMethodRefPairsWithNothing(options);
|
|
894
|
-
assertLinkageMethodIdIsBare(options);
|
|
895
|
-
assertResourcesAccompanyABundle(options);
|
|
896
|
-
// No await when there are none: a request without them leaves on the tick it always did.
|
|
897
|
-
const resources = options.resources == null ? undefined : await encodeBundleResources(options.resources);
|
|
898
|
-
// `?? undefined` so JSON.stringify drops absent fields from the wire body.
|
|
899
|
-
const request = {
|
|
900
|
-
pipe_code: options.pipe_code ?? undefined,
|
|
901
|
-
mthds_contents: options.mthds_contents ?? undefined,
|
|
902
|
-
inputs: options.inputs ?? undefined,
|
|
903
|
-
output_name: options.output_name ?? undefined,
|
|
904
|
-
output_multiplicity: options.output_multiplicity ?? undefined,
|
|
905
|
-
dynamic_output_concept_ref: options.dynamic_output_concept_ref ?? undefined,
|
|
906
|
-
files: nonEmptyFiles(options.files),
|
|
907
|
-
bundle_b64: nonEmptyString(options.bundle_b64),
|
|
908
|
-
resources,
|
|
909
|
-
...api,
|
|
910
|
-
...hosted,
|
|
911
|
-
...extensions,
|
|
912
|
-
};
|
|
886
|
+
const parts = this.runParts("start", options);
|
|
887
|
+
const source = sending?.source ?? (await this.treeToSend("start", parts.forms, REQUEST_SENDING, signal));
|
|
913
888
|
const url = this.url("start");
|
|
914
|
-
|
|
915
|
-
//
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
889
|
+
const res = await this.postMethodRequest(source, "start", REQUEST_SENDING,
|
|
890
|
+
// A start under a key is bound to the refusal the platform stores, so it is not sent again.
|
|
891
|
+
{ signal, retryTooLarge: idempotencyKey === undefined }, async (files) => {
|
|
892
|
+
const request = runRequestBody("start", options, files, parts);
|
|
893
|
+
// `start` returns a 202 fast, so the poll timeout normally fits — with exceptions that get
|
|
894
|
+
// the blocking-execute ceiling instead. A method's tree sent inline can make the request
|
|
895
|
+
// *body* multi-megabyte when the API keeps no blob store, and so can a bundle riding
|
|
896
|
+
// `extra` as a runner's `mthds_contents` or `bundle_b64`, as the CLI sends one; the whole
|
|
897
|
+
// upload is charged against this budget — the same payload must not time out on the
|
|
898
|
+
// durable path yet succeed on the fallback. And a `method_ref` start makes the server FETCH
|
|
899
|
+
// the package before the ack (provenance rides the 202), whose clone timeout runs well past
|
|
900
|
+
// 30s on a cold cache — aborting it here would surface as `ApiUnreachableError`, blaming
|
|
901
|
+
// the network for a healthy server that is still cloning.
|
|
902
|
+
const needsLongCeiling = files !== undefined ||
|
|
903
|
+
parts.api.method_ref !== undefined ||
|
|
904
|
+
carriesExtraBundle(parts.extensions);
|
|
905
|
+
throwIfAborted(signal);
|
|
906
|
+
return this.requestRaw("POST", url, {
|
|
907
|
+
body: request,
|
|
908
|
+
timeoutMs: needsLongCeiling ? this.requestTimeoutMs : POLL_REQUEST_TIMEOUT_MS,
|
|
909
|
+
idempotencyKey,
|
|
910
|
+
...STARTS_RUN,
|
|
911
|
+
});
|
|
912
|
+
}, sending?.initial);
|
|
934
913
|
// A bare runner with no run store 404s here just as it does on the result
|
|
935
914
|
// routes — surface the same clear `RunLifecycleUnavailableError` (and let
|
|
936
915
|
// `startAndWaitForResult` fall back to the blocking `execute`).
|
|
@@ -948,30 +927,38 @@ export class PipelexApiClient {
|
|
|
948
927
|
return ack;
|
|
949
928
|
}
|
|
950
929
|
/**
|
|
951
|
-
* Parse, validate, and dry-run
|
|
930
|
+
* Parse, validate, and dry-run a method — `POST /v1/validate`.
|
|
952
931
|
*
|
|
953
932
|
* `/validate` is a diagnostic endpoint: every produced verdict rides a **200**,
|
|
954
933
|
* discriminated on `is_valid`. This returns the `PipelexValidationResult` union
|
|
955
934
|
* verbatim — `is_valid: true` ⇒ the typed `PipelexValidationReport` (structural
|
|
956
935
|
* artifacts), `is_valid: false` ⇒ a `PipelexInvalidReport` (`validation_errors[]`).
|
|
957
|
-
* An invalid
|
|
958
|
-
* *no-verdict* condition (a malformed request,
|
|
959
|
-
*
|
|
960
|
-
*
|
|
961
|
-
* `source`
|
|
962
|
-
*
|
|
963
|
-
*
|
|
964
|
-
*
|
|
965
|
-
* - **
|
|
936
|
+
* An invalid method is NOT thrown — the caller pattern-matches `is_valid`. Only a
|
|
937
|
+
* *no-verdict* condition (a malformed request, a tree the route refuses, auth, a
|
|
938
|
+
* server fault) is non-2xx and surfaces as `ApiResponseError`.
|
|
939
|
+
*
|
|
940
|
+
* `source` names WHAT is validated, in exactly one form — the strict tooling XOR
|
|
941
|
+
* (the routes are stateless, so there is no linkage exception; a second selector
|
|
942
|
+
* is a request-shape `422`, refused here first with a `RequestArgumentError`):
|
|
943
|
+
*
|
|
944
|
+
* - **the method's tree**, in one of the four forms of `MethodTreeForms`: a
|
|
945
|
+
* `directory` the `mthds` package's lister lists under the standard's
|
|
946
|
+
* membership rule (Node only), `contents` by tree path, a listed `tree`, or the
|
|
947
|
+
* protocol's `files` map, sent as given. A `directory`, `contents` or `tree` is
|
|
948
|
+
* sent as the `files` map, inline or by hash, exactly as a run sends it (see
|
|
949
|
+
* `PipelexRunOptions`). Every `.mthds` file of the tree is judged, named by its
|
|
950
|
+
* tree path in the diagnostics' `source`, and every other file is one a step may
|
|
951
|
+
* read, such as a `PipeDocGen` step's template file, whose template check's
|
|
952
|
+
* findings ride the valid arm's `warnings`;
|
|
966
953
|
* - **`{ method_ref }`** — a published method's address, resolved by the
|
|
967
954
|
* server (pipelex-api >= 0.21.0) through the same fetch path as a
|
|
968
|
-
* `method_ref` run, the package's real
|
|
955
|
+
* `method_ref` run, the package's real tree paths feeding the diagnostics'
|
|
969
956
|
* source labels;
|
|
970
957
|
* - **`{ method_id }`** — a stored method's catalog id, hosted-only: the
|
|
971
|
-
* platform resolves the version it names and
|
|
972
|
-
*
|
|
973
|
-
*
|
|
974
|
-
*
|
|
958
|
+
* platform resolves the version it names and sends that version's tree to the
|
|
959
|
+
* runner (a bare runner rejects the request as carrying no source it
|
|
960
|
+
* understands). A bare `mt_…` validates the latest published version,
|
|
961
|
+
* `mt_…@<n>` version `n` and `mt_…@draft` the draft.
|
|
975
962
|
*
|
|
976
963
|
* A selector-resolution failure (fetch failure, no package at the address, an
|
|
977
964
|
* unknown or foreign-org id, a bare id of a method never published, `409`
|
|
@@ -980,40 +967,23 @@ export class PipelexApiClient {
|
|
|
980
967
|
* `ApiResponseError` — never an `is_valid: false` verdict, which is reserved
|
|
981
968
|
* for actual MTHDS content.
|
|
982
969
|
*
|
|
983
|
-
* `
|
|
984
|
-
*
|
|
985
|
-
*
|
|
986
|
-
*
|
|
987
|
-
*
|
|
988
|
-
*
|
|
989
|
-
*
|
|
990
|
-
*
|
|
991
|
-
*
|
|
992
|
-
* `
|
|
993
|
-
*
|
|
994
|
-
*
|
|
995
|
-
*
|
|
996
|
-
*
|
|
997
|
-
*
|
|
998
|
-
*
|
|
999
|
-
*
|
|
1000
|
-
* top-level field to the valid arm — `input_form` and `output_form`. Unlike `render`,
|
|
1001
|
-
* this client sends `views` ONLY when the caller asks: the point of an opt-in view
|
|
1002
|
-
* is that the default response stays byte-identical, and the highest-frequency
|
|
1003
|
-
* consumers (hook pipelines, CI gates, agent loops) never pay for bytes they
|
|
1004
|
-
* discard. `pipelex-api` gates `input_form` on this token as of 0.18.0; a 0.17.0
|
|
1005
|
-
* runner resolved no token (the key is silently ignored, never a 422) and emitted
|
|
1006
|
-
* the field regardless — which is why `PipelexValidationReport.input_form` is typed
|
|
1007
|
-
* optional rather than required.
|
|
1008
|
-
*
|
|
1009
|
-
* `options.resources` sends the files the bundle's steps read, such as a
|
|
1010
|
-
* `PipeDocGen` step's `template_file`, keyed by the path the step names, each as
|
|
1011
|
-
* its bytes (see {@link ValidateOptions}). A step whose template file was not sent
|
|
1012
|
-
* is an `is_valid: false` verdict saying so, and a template check's findings in an
|
|
1013
|
-
* office template file ride the valid arm's `warnings`. A key no step names, or
|
|
1014
|
-
* files over the server's limits, are no-verdict refusals: a `422`
|
|
1015
|
-
* `BundleResourceNotNamedError`, a `413` `PayloadTooLarge` or
|
|
1016
|
-
* `BundleResourcesTooLargeError`.
|
|
970
|
+
* `allowSignatures` tolerates unimplemented pipe signatures, as the protocol's
|
|
971
|
+
* `validate(source, allowSignatures)` does: they surface as `pending_signatures`
|
|
972
|
+
* and `is_runnable`, never as an error.
|
|
973
|
+
*
|
|
974
|
+
* `options.render` is the Pipelex-API presentation hint — a list of view-format
|
|
975
|
+
* tokens. This client always asks for Markdown so both valid results and produced
|
|
976
|
+
* validation-error verdicts carry `rendered_markdown`; callers may add more tokens.
|
|
977
|
+
* Unknown tokens are server-side lenient-ignored (never a 422).
|
|
978
|
+
*
|
|
979
|
+
* `options.views` is the sibling opt-in for *structured* views where `render`
|
|
980
|
+
* carries *rendered text*. The two lists are independent, each resolving its own
|
|
981
|
+
* tokens against its own supported set, and each supported token adds a same-named
|
|
982
|
+
* top-level field to the valid arm — `input_form` and `output_form`. Unlike
|
|
983
|
+
* `render`, this client sends `views` ONLY when the caller asks: the point of an
|
|
984
|
+
* opt-in view is that the default response stays byte-identical, and the
|
|
985
|
+
* highest-frequency consumers (hook pipelines, CI gates, agent loops) never pay for
|
|
986
|
+
* bytes they discard.
|
|
1017
987
|
*
|
|
1018
988
|
* A `{ method_ref }` source makes the server clone a repository before it
|
|
1019
989
|
* validates anything, and it needs no special budget here: this route already
|
|
@@ -1023,85 +993,52 @@ export class PipelexApiClient {
|
|
|
1023
993
|
* exists to RAISE routes whose default is the ~30s poll ceiling; applying it here
|
|
1024
994
|
* would lower this one.
|
|
1025
995
|
*/
|
|
1026
|
-
async validate(source, allowSignatures = false,
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
if (Array.isArray(source)) {
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
throw new RequestArgumentError("mthds_sources labels inline mthds_contents; a method_ref / method_id validation gets " +
|
|
1053
|
-
"its source labels from the package's (or the stored method's) real file names.");
|
|
1054
|
-
}
|
|
1055
|
-
if (methodRef !== undefined && hasBundleResources(options.resources)) {
|
|
1056
|
-
throw new RequestArgumentError("resources accompany inline mthds_contents; a method_ref's files are the package's own.");
|
|
1057
|
-
}
|
|
996
|
+
async validate(source, allowSignatures = false, options = {}) {
|
|
997
|
+
// The illegal shapes are partly compile errors for typed callers; the runtime checks back them
|
|
998
|
+
// for untyped (JS) callers — a typed `RequestArgumentError`, never a native TypeError off a
|
|
999
|
+
// null source — mirroring the server's strict tooling XOR instead of silently picking.
|
|
1000
|
+
if (source === null || typeof source !== "object" || Array.isArray(source)) {
|
|
1001
|
+
throw new RequestArgumentError("validate() takes the method as an object naming exactly one of directory, contents, " +
|
|
1002
|
+
"tree, files, method_ref or method_id.");
|
|
1003
|
+
}
|
|
1004
|
+
const forms = protocolTreeForms("validate", source);
|
|
1005
|
+
const methodRef = nonEmptyString(source.method_ref);
|
|
1006
|
+
const methodId = nonEmptyString(source.method_id);
|
|
1007
|
+
const given = treeFormsGiven(forms);
|
|
1008
|
+
if (methodRef !== undefined)
|
|
1009
|
+
given.push("method_ref");
|
|
1010
|
+
if (methodId !== undefined)
|
|
1011
|
+
given.push("method_id");
|
|
1012
|
+
if (given.length !== 1) {
|
|
1013
|
+
throw new RequestArgumentError("validate() takes exactly one of the method's tree (directory, contents, tree or files), " +
|
|
1014
|
+
`method_ref or method_id; got ${given.length === 0 ? "none" : given.join(" and ")}.`);
|
|
1015
|
+
}
|
|
1016
|
+
const signal = options.signal;
|
|
1017
|
+
const sent = await this.treeToSend("validate", forms, REQUEST_SENDING, signal);
|
|
1018
|
+
const res = await this.postMethodRequest(sent, "validate", REQUEST_SENDING, { signal }, (files) => {
|
|
1019
|
+
const body = { allow_signatures: allowSignatures };
|
|
1020
|
+
if (files != null)
|
|
1021
|
+
body.files = files;
|
|
1058
1022
|
if (methodRef !== undefined)
|
|
1059
1023
|
body.method_ref = methodRef;
|
|
1060
1024
|
if (methodId !== undefined)
|
|
1061
1025
|
body.method_id = methodId;
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
const res = await this.requestRaw("POST", this.url("validate"), {
|
|
1073
|
-
body,
|
|
1074
|
-
timeoutMs: options.timeoutMs,
|
|
1075
|
-
signal: options.signal,
|
|
1026
|
+
body.render = withValidateMarkdownRender(options.render);
|
|
1027
|
+
if (options.views !== undefined) {
|
|
1028
|
+
body.views = options.views;
|
|
1029
|
+
}
|
|
1030
|
+
throwIfAborted(signal);
|
|
1031
|
+
return this.requestRaw("POST", this.url("validate"), {
|
|
1032
|
+
body,
|
|
1033
|
+
timeoutMs: options.timeoutMs,
|
|
1034
|
+
signal,
|
|
1035
|
+
});
|
|
1076
1036
|
});
|
|
1077
1037
|
if (res.status < 200 || res.status >= 300) {
|
|
1078
1038
|
this.throwApiResponseError("POST", "validate", res);
|
|
1079
1039
|
}
|
|
1080
1040
|
return this.readObjectAnswer("POST", "validate", res);
|
|
1081
1041
|
}
|
|
1082
|
-
/**
|
|
1083
|
-
* Validate paired MTHDS files while preserving URI attribution for diagnostics.
|
|
1084
|
-
*
|
|
1085
|
-
* This adapter intentionally keeps the low-level `validate(...)` payload shape
|
|
1086
|
-
* intact for existing consumers. When any file has a URI, every content gets a
|
|
1087
|
-
* parallel source label; inline labels are deterministic so the server never
|
|
1088
|
-
* sees a length-mismatched `mthds_sources` array.
|
|
1089
|
-
*/
|
|
1090
|
-
async validateFiles(files, options = {}) {
|
|
1091
|
-
if (files.length === 0) {
|
|
1092
|
-
throw new RequestArgumentError("At least one MTHDS file must be provided to validateFiles().");
|
|
1093
|
-
}
|
|
1094
|
-
const mthdsContents = files.map((file) => file.content);
|
|
1095
|
-
const hasAnyUri = files.some((file) => file.uri !== undefined);
|
|
1096
|
-
const mthdsSources = hasAnyUri
|
|
1097
|
-
? files.map((file, index) => file.uri ?? `inline://file-${index + 1}.mthds`)
|
|
1098
|
-
: undefined;
|
|
1099
|
-
return this.validate(mthdsContents, options.allowSignatures ?? false, mthdsSources, options.render, options.views, {
|
|
1100
|
-
resources: options.resources,
|
|
1101
|
-
timeoutMs: options.timeoutMs,
|
|
1102
|
-
signal: options.signal,
|
|
1103
|
-
});
|
|
1104
|
-
}
|
|
1105
1042
|
// ── Tools extensions (Pipelex API — `/v1/lint`, `/v1/format`) ─────────
|
|
1106
1043
|
//
|
|
1107
1044
|
// NOT REACHABLE ON ANY HOSTED ENVIRONMENT. Both are served by any `pipelex-api`
|
|
@@ -1172,11 +1109,10 @@ export class PipelexApiClient {
|
|
|
1172
1109
|
* never on a message.
|
|
1173
1110
|
*
|
|
1174
1111
|
* All of these are static and inference-free, so they default to the management-call
|
|
1175
|
-
* timeout. The internal
|
|
1112
|
+
* timeout. The internal raise is `crateRequestTimeoutMs`'s: the fetch-sized budget a
|
|
1176
1113
|
* crate route gets when its closure is a `method_ref`, because the server may clone a
|
|
1177
|
-
* repository before it answers
|
|
1178
|
-
*
|
|
1179
|
-
* what the request costs, not a caller-facing parameter.
|
|
1114
|
+
* repository before it answers. It is matched to what the request costs, not a
|
|
1115
|
+
* caller-facing parameter.
|
|
1180
1116
|
*
|
|
1181
1117
|
* **These routes deliberately expose no `timeoutMs` / `signal`, and that is not an
|
|
1182
1118
|
* oversight to be "fixed" per route.** Automated reviewers have proposed adding them
|
|
@@ -1187,11 +1123,11 @@ export class PipelexApiClient {
|
|
|
1187
1123
|
* ceiling and takes caller transport options; every extension route rides the
|
|
1188
1124
|
* static core. Giving one static route an override while its siblings lack one is
|
|
1189
1125
|
* the inconsistency.
|
|
1190
|
-
* 2. The input is **bounded server-side** — the
|
|
1191
|
-
*
|
|
1192
|
-
*
|
|
1193
|
-
*
|
|
1194
|
-
*
|
|
1126
|
+
* 2. The input is **bounded server-side** — the deployment caps a request's tree by its
|
|
1127
|
+
* count of files, a size per file, a total and the bytes it carries inline, all
|
|
1128
|
+
* announced by its `/v1/version` handshake — and none of these routes runs inference.
|
|
1129
|
+
* The upload those limits allow is what `crateRequestTimeoutMs` budgets for, not the
|
|
1130
|
+
* server's work.
|
|
1195
1131
|
* 3. On the hosted path an override would be **inert**: the gateway caps responses at
|
|
1196
1132
|
* ~30s (see `POLL_REQUEST_TIMEOUT_MS` above), so raising `timeoutMs` would still be
|
|
1197
1133
|
* cut off upstream, just with a less honest error.
|
|
@@ -1199,6 +1135,12 @@ export class PipelexApiClient {
|
|
|
1199
1135
|
* If a static route ever genuinely needs longer, give the WHOLE static family one
|
|
1200
1136
|
* uniform transport-options parameter in a single pass — do not bolt it onto one
|
|
1201
1137
|
* route.
|
|
1138
|
+
*
|
|
1139
|
+
* The one exception is the crate family's `signal` (`CrateRouteOptions`), given to all three
|
|
1140
|
+
* crate routes at once: a `directory`, `tree` or `contents` can cost the listing, the version
|
|
1141
|
+
* handshake, the blob route and uploads before the request, client-side work a caller such as
|
|
1142
|
+
* `prepareInputs` must be able to stop. It is read between those steps and stops nothing in flight, so it is no
|
|
1143
|
+
* transport option and the reasons above still hold.
|
|
1202
1144
|
*/
|
|
1203
1145
|
async requestExtension(endpoint, body, options = {}) {
|
|
1204
1146
|
const res = await this.requestRaw("POST", this.url(endpoint), {
|
|
@@ -1259,8 +1201,13 @@ export class PipelexApiClient {
|
|
|
1259
1201
|
* The handshake for feature detection (hosted extensions or not).
|
|
1260
1202
|
*/
|
|
1261
1203
|
async version() {
|
|
1204
|
+
return this.requestVersion();
|
|
1205
|
+
}
|
|
1206
|
+
/** `GET /v1/version`, as `version` asks it, stopped by `signal`. */
|
|
1207
|
+
async requestVersion(signal) {
|
|
1262
1208
|
const res = await this.requestRaw("GET", this.url("version"), {
|
|
1263
1209
|
timeoutMs: POLL_REQUEST_TIMEOUT_MS,
|
|
1210
|
+
signal,
|
|
1264
1211
|
});
|
|
1265
1212
|
if (res.status < 200 || res.status >= 300) {
|
|
1266
1213
|
this.throwApiResponseError("GET", "version", res);
|
|
@@ -1287,8 +1234,10 @@ export class PipelexApiClient {
|
|
|
1287
1234
|
// `docs/crate-routes.md`).
|
|
1288
1235
|
//
|
|
1289
1236
|
// All three are STATIC routes (no dry-run sweep), so like every static sibling they
|
|
1290
|
-
// take no `timeoutMs
|
|
1291
|
-
//
|
|
1237
|
+
// take no `timeoutMs` — see the policy note on `requestExtension` before adding one
|
|
1238
|
+
// here. Their `signal` (`CrateRouteOptions`) is that note's one exception: it is read
|
|
1239
|
+
// between the steps that prepare a `directory`, `tree` or `contents`, and stops nothing in
|
|
1240
|
+
// flight.
|
|
1292
1241
|
/**
|
|
1293
1242
|
* Resolve a closure into its normalized library crate — `POST /v1/resolve`.
|
|
1294
1243
|
*
|
|
@@ -1307,15 +1256,23 @@ export class PipelexApiClient {
|
|
|
1307
1256
|
* (fetch failure, no package at the address, an unknown or foreign-org id) is
|
|
1308
1257
|
* non-2xx too — never an `is_valid: false` verdict.
|
|
1309
1258
|
*
|
|
1310
|
-
* The closure arrives in exactly one of three forms:
|
|
1311
|
-
*
|
|
1312
|
-
*
|
|
1313
|
-
*
|
|
1259
|
+
* The closure arrives in exactly one of three forms: the method's tree, as the
|
|
1260
|
+
* protocol's `files` map or as a `directory`, `tree` or `contents` the client sends
|
|
1261
|
+
* inline when small and by hash otherwise (see {@link CrateRequestBase}); an
|
|
1262
|
+
* address-form `method_ref` (server-resolved, pipelex-api >= 0.21.0; the registry form
|
|
1263
|
+
* stays a `501`); or a hosted `method_id` (platform-resolved — see
|
|
1264
|
+
* {@link PipelexHostedToolingExtensions}). An empty map or string is absent.
|
|
1265
|
+
*
|
|
1266
|
+
* `options.signal` stops the call between the steps a `directory`, `tree` or `contents`
|
|
1267
|
+
* costs before the request — the listing, the version handshake, the blob route, each
|
|
1268
|
+
* read and upload of a missing blob — and before the request itself, throwing the
|
|
1269
|
+
* abort; a request already sent runs to its end. A blob that fails to upload throws
|
|
1270
|
+
* `MethodFileTransferError`, and a tree this client refuses to send (no file, two
|
|
1271
|
+
* forms, a directory it cannot list, a `contents` value that is neither text nor
|
|
1272
|
+
* bytes) `RequestArgumentError`, each before the route is asked.
|
|
1314
1273
|
*/
|
|
1315
|
-
async resolve(request) {
|
|
1316
|
-
return this.
|
|
1317
|
-
timeoutMs: crateRequestTimeoutMs(request, this.requestTimeoutMs),
|
|
1318
|
-
});
|
|
1274
|
+
async resolve(request, options = {}) {
|
|
1275
|
+
return this.crateRequest("resolve", "resolve", request, options);
|
|
1319
1276
|
}
|
|
1320
1277
|
/**
|
|
1321
1278
|
* Project a closure's crate into stamped typed artifacts — `POST /v1/codegen`.
|
|
@@ -1332,10 +1289,8 @@ export class PipelexApiClient {
|
|
|
1332
1289
|
* unknown `kind`/`target`, a `pipe_ref` on the concept-set-wide `types` kind, or
|
|
1333
1290
|
* a malformed selector is a 422; a registry-form `method_ref` is a `501`.
|
|
1334
1291
|
*/
|
|
1335
|
-
async codegen(request) {
|
|
1336
|
-
return this.
|
|
1337
|
-
timeoutMs: crateRequestTimeoutMs(request, this.requestTimeoutMs),
|
|
1338
|
-
});
|
|
1292
|
+
async codegen(request, options = {}) {
|
|
1293
|
+
return this.crateRequest("codegen", "codegen", request, options);
|
|
1339
1294
|
}
|
|
1340
1295
|
/**
|
|
1341
1296
|
* Read a method's three I/O artifacts — `POST /v1/pipe-io`.
|
|
@@ -1348,19 +1303,20 @@ export class PipelexApiClient {
|
|
|
1348
1303
|
* dry-runs every pipe; a caller that shows a method, prepares its inputs or generates
|
|
1349
1304
|
* types for it reads this, and one that needs the dry-run verdict stays on `validate`.
|
|
1350
1305
|
*
|
|
1351
|
-
* Selection: the request's qualified `pipe_ref`, else
|
|
1352
|
-
* `
|
|
1353
|
-
*
|
|
1354
|
-
* `include_files: true` echoes the
|
|
1306
|
+
* Selection: the request's qualified `pipe_ref`, else the `main_pipe` of the
|
|
1307
|
+
* `METHODS.toml` at the tree's root, sent in the tree or fetched with a package, else the
|
|
1308
|
+
* closure's single `main_pipe` declaration. `all_pipes: true` describes every pipe
|
|
1309
|
+
* instead, and never refuses for want of an entry pipe. `include_files: true` echoes the
|
|
1310
|
+
* method's own `.mthds` bundles as `files`, a map from tree path to each bundle's text,
|
|
1311
|
+
* in the shape of the request's `files` map.
|
|
1355
1312
|
*
|
|
1356
|
-
*
|
|
1357
|
-
*
|
|
1358
|
-
*
|
|
1359
|
-
* take it too (see {@link CrateRequestBase}).
|
|
1313
|
+
* The files the closure's steps read, such as a `PipeDocGen` step's `template_file`, are
|
|
1314
|
+
* files of the method's tree like any other; a step whose template file is not in the tree
|
|
1315
|
+
* makes the closure invalid (see {@link CrateRequestBase}).
|
|
1360
1316
|
*
|
|
1361
|
-
* Same 200-verdict discipline
|
|
1362
|
-
* (the request
|
|
1363
|
-
*
|
|
1317
|
+
* Same 200-verdict discipline, same three-form closure selector and same `options` as
|
|
1318
|
+
* {@link resolve} (the request's members are posted as given, its `directory`, `tree` or
|
|
1319
|
+
* `contents` as the `files` map, and the selector XOR is the server's to enforce).
|
|
1364
1320
|
* Only a no-verdict condition throws `ApiResponseError`: a malformed selector and an
|
|
1365
1321
|
* over-limit file are `ValidationError` `422`s; a selection the route refuses is a
|
|
1366
1322
|
* `422` whose `errorType` is `EntryPipeNotFoundError` (an unknown `pipe_ref`, no entry
|
|
@@ -1368,16 +1324,70 @@ export class PipelexApiClient {
|
|
|
1368
1324
|
* candidates in its `serverMessage`; a registry-form `method_ref` is a `501`; a pipe
|
|
1369
1325
|
* whose artifacts cannot be derived is a `500`.
|
|
1370
1326
|
*
|
|
1371
|
-
* A `method_ref` gets the fetch-sized budget, as on the other crate routes
|
|
1372
|
-
*
|
|
1373
|
-
*
|
|
1374
|
-
*
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1327
|
+
* A `method_ref` gets the fetch-sized budget, as on the other crate routes. On the
|
|
1328
|
+
* hosted API the gateway caps a request at about 30 seconds whatever the client allows,
|
|
1329
|
+
* so a cold `method_ref` clone can answer a `502` that a retry clears once the runner
|
|
1330
|
+
* has cached the clone.
|
|
1331
|
+
*/
|
|
1332
|
+
async pipeIo(request, options = {}) {
|
|
1333
|
+
return this.crateRequest("pipe-io", "pipeIo", request, options);
|
|
1334
|
+
}
|
|
1335
|
+
/**
|
|
1336
|
+
* Post a crate request: its members as given, and its tree as the `files` map a save would
|
|
1337
|
+
* send, each key left off when there is nothing to send. An empty map or string is absent, as in
|
|
1338
|
+
* the Python SDK: `files: {}` or `null`, `contents: {}`, `directory: ""` and `dependencies: {}`
|
|
1339
|
+
* are not sent, so an empty one never reaches the wire as a second selector; a `directory` that
|
|
1340
|
+
* is not a string is refused, as on a run. A `directory`, `tree` or `contents` is prepared
|
|
1341
|
+
* through the path every request and save sends a tree by (`postMethodRequest`), inline when
|
|
1342
|
+
* small and by hash otherwise, uploading the files the API lacks first; the blob route is asked
|
|
1343
|
+
* under the request's `read_scope`, the scope its hash entries are read in. `signal` is read
|
|
1344
|
+
* before each step and right before the request (see `TreeSendOptions`). The request gets the
|
|
1345
|
+
* budget `crateRequestTimeoutMs` gives it for the `files` it carries.
|
|
1346
|
+
*/
|
|
1347
|
+
async crateRequest(endpoint, call, request, options) {
|
|
1348
|
+
const signal = options.signal;
|
|
1349
|
+
throwIfAborted(signal);
|
|
1350
|
+
if (typeof request !== "object" || request === null) {
|
|
1351
|
+
return this.requestExtension(endpoint, request);
|
|
1352
|
+
}
|
|
1353
|
+
const { directory, contents, tree, files, dependencies, ...rest } = request;
|
|
1354
|
+
const source = await this.treeToSend(call, {
|
|
1355
|
+
directory: typeof directory === "string" ? nonEmptyString(directory) : directory,
|
|
1356
|
+
contents: presentMap(contents),
|
|
1357
|
+
tree: tree ?? undefined,
|
|
1358
|
+
files: presentMap(files),
|
|
1359
|
+
}, REQUEST_SENDING, signal);
|
|
1360
|
+
const packages = presentMap(dependencies);
|
|
1361
|
+
const res = await this.postMethodRequest(source, call, REQUEST_SENDING, { signal, readScope: nonEmptyString(request.read_scope) }, (sent) => {
|
|
1362
|
+
const body = { ...rest };
|
|
1363
|
+
if (sent != null)
|
|
1364
|
+
body.files = sent;
|
|
1365
|
+
if (packages !== undefined)
|
|
1366
|
+
body.dependencies = packages;
|
|
1367
|
+
throwIfAborted(signal);
|
|
1368
|
+
return this.requestRaw("POST", this.url(endpoint), {
|
|
1369
|
+
body,
|
|
1370
|
+
timeoutMs: this.crateRequestTimeoutMs(request, sent),
|
|
1371
|
+
});
|
|
1380
1372
|
});
|
|
1373
|
+
if (res.status < 200 || res.status >= 300) {
|
|
1374
|
+
this.throwApiResponseError("POST", endpoint, res);
|
|
1375
|
+
}
|
|
1376
|
+
return this.readObjectAnswer("POST", endpoint, res);
|
|
1377
|
+
}
|
|
1378
|
+
/**
|
|
1379
|
+
* The request budget for a crate route's closure (`resolve`, `codegen`, `pipeIo`): the
|
|
1380
|
+
* static-route default, unless the closure is a `method_ref`, which may make the server clone a
|
|
1381
|
+
* repository first and gets the fetch-sized budget, or its `files` carry more inline than
|
|
1382
|
+
* `INLINE_TREE_MAX_BYTES`, which gets the client's request ceiling (`requestTimeoutMs`): a tree
|
|
1383
|
+
* sent to an API keeping no blob store travels inline whatever its weight, and so does a `files`
|
|
1384
|
+
* map given as is, and the upload of the body is charged against the budget. A tree sent by
|
|
1385
|
+
* hash keeps the default, its files uploaded before the request under the client's own ceiling.
|
|
1386
|
+
*/
|
|
1387
|
+
crateRequestTimeoutMs(request, files) {
|
|
1388
|
+
if (nonEmptyString(request.method_ref) !== undefined)
|
|
1389
|
+
return METHOD_REF_FETCH_TIMEOUT_MS;
|
|
1390
|
+
return carriesLargeInlineTree(files) ? this.requestTimeoutMs : POLL_REQUEST_TIMEOUT_MS;
|
|
1381
1391
|
}
|
|
1382
1392
|
// ── Hosted extension: durable run lifecycle (NOT part of the protocol) ──
|
|
1383
1393
|
/**
|
|
@@ -1532,19 +1542,25 @@ export class PipelexApiClient {
|
|
|
1532
1542
|
* gzip stream, which `requestRaw` reports as an `ApiUnreachableError` carrying
|
|
1533
1543
|
* the decompressor's code (see `isUndecodableBody`).
|
|
1534
1544
|
*
|
|
1545
|
+
* `signal` stops the handshake: its abort is thrown as is and nothing is cached.
|
|
1546
|
+
*
|
|
1535
1547
|
* @throws {ApiUnreachableError} The handshake got no answer. Nothing is cached,
|
|
1536
1548
|
* so the next call asks again, and no start is sent to a host that did not
|
|
1537
1549
|
* answer: sending it would wait a second time for the same silence.
|
|
1538
1550
|
*/
|
|
1539
|
-
async supportsRunLifecycle() {
|
|
1551
|
+
async supportsRunLifecycle(signal) {
|
|
1540
1552
|
if (this.lifecycleAvailable === undefined) {
|
|
1541
1553
|
try {
|
|
1542
|
-
const
|
|
1543
|
-
this.
|
|
1554
|
+
const askedAt = Date.now();
|
|
1555
|
+
const info = await this.requestVersion(signal);
|
|
1556
|
+
this.versionCache = { info, askedAt };
|
|
1544
1557
|
const impl = info.implementation;
|
|
1545
1558
|
this.lifecycleAvailable = !(typeof impl === "string" && impl === BARE_RUNNER_IMPLEMENTATION);
|
|
1546
1559
|
}
|
|
1547
1560
|
catch (error) {
|
|
1561
|
+
// The caller's abort is no answer: it propagates, and nothing is cached.
|
|
1562
|
+
if (signal?.aborted === true)
|
|
1563
|
+
throw signal.reason;
|
|
1548
1564
|
// No answer at all propagates. A body that arrived and could not be decoded is an answer,
|
|
1549
1565
|
// and so is every other failure here: a non-2xx status, or a body that is no version.
|
|
1550
1566
|
if (error instanceof ApiUnreachableError && !isUndecodableBody(error))
|
|
@@ -1569,9 +1585,15 @@ export class PipelexApiClient {
|
|
|
1569
1585
|
* run, so the caller knows until then that none exists (see
|
|
1570
1586
|
* `StartAndWaitForResultOptions`).
|
|
1571
1587
|
*
|
|
1572
|
-
* `pollOptions.signal`
|
|
1573
|
-
*
|
|
1574
|
-
*
|
|
1588
|
+
* `pollOptions.signal` also stops everything before the run is created: the version handshake,
|
|
1589
|
+
* the listing of a `directory`, and the reads, the blob route and the uploads of a tree sent by
|
|
1590
|
+
* hash, each of them stopped in flight. It is read again right before the start and before the
|
|
1591
|
+
* blocking execute, so a caller that aborts before then gets its abort and no run is created.
|
|
1592
|
+
* Once one of them is sent, the abort stops only the wait.
|
|
1593
|
+
*
|
|
1594
|
+
* The method's tree is prepared once, before the start: when a runner turns out to lack the
|
|
1595
|
+
* durable routes, the blocking execute sends the same `files` map, its blobs already uploaded,
|
|
1596
|
+
* rather than listing, reading and hashing the tree again.
|
|
1575
1597
|
*
|
|
1576
1598
|
* An error from the start, or from the blocking execute, says whether that request may still
|
|
1577
1599
|
* have started a run whose id never reached the caller (`runMayHaveStarted`); when it may have,
|
|
@@ -1592,10 +1614,12 @@ export class PipelexApiClient {
|
|
|
1592
1614
|
// Before the handshake too: a key the start would refuse is the caller's to fix, whatever runs.
|
|
1593
1615
|
idempotencyKeyOf(options);
|
|
1594
1616
|
const { onStarted, onStarting, ...waitOptions } = pollOptions ?? {};
|
|
1595
|
-
const
|
|
1617
|
+
const signal = waitOptions.signal;
|
|
1618
|
+
const hosted = await this.supportsRunLifecycle(signal);
|
|
1596
1619
|
// The handshake may have outlasted the caller's patience: an abort that landed during it
|
|
1597
1620
|
// creates no run.
|
|
1598
|
-
throwIfAborted(
|
|
1621
|
+
throwIfAborted(signal);
|
|
1622
|
+
const sending = await this.prepareRunTree(hosted ? "start" : "execute", options, signal);
|
|
1599
1623
|
if (hosted) {
|
|
1600
1624
|
// A runner can look hosted yet lack the durable routes — `implementation`
|
|
1601
1625
|
// is an extension field, so a compliant bare runner that omits it is
|
|
@@ -1605,22 +1629,38 @@ export class PipelexApiClient {
|
|
|
1605
1629
|
// the durable attempt.
|
|
1606
1630
|
let ack;
|
|
1607
1631
|
try {
|
|
1632
|
+
throwIfAborted(signal);
|
|
1608
1633
|
onStarting?.();
|
|
1609
|
-
ack = await this.
|
|
1634
|
+
ack = await this.startRun(options, signal, sending);
|
|
1610
1635
|
}
|
|
1611
1636
|
catch (err) {
|
|
1612
1637
|
if (!(err instanceof RunLifecycleUnavailableError))
|
|
1613
1638
|
throw err;
|
|
1614
1639
|
this.lifecycleAvailable = false;
|
|
1615
|
-
throwIfAborted(
|
|
1640
|
+
throwIfAborted(signal);
|
|
1616
1641
|
onStarting?.();
|
|
1617
|
-
return this.executeBlocking(options);
|
|
1642
|
+
return this.executeBlocking(options, signal, sending);
|
|
1618
1643
|
}
|
|
1619
1644
|
onStarted?.(ack);
|
|
1620
1645
|
return this.waitForResult(ack.pipeline_run_id, waitOptions);
|
|
1621
1646
|
}
|
|
1647
|
+
throwIfAborted(signal);
|
|
1622
1648
|
onStarting?.();
|
|
1623
|
-
return this.executeBlocking(options);
|
|
1649
|
+
return this.executeBlocking(options, signal, sending);
|
|
1650
|
+
}
|
|
1651
|
+
/**
|
|
1652
|
+
* A run's tree prepared once for `startAndWaitForResult`, under `call`'s refusals: the run's
|
|
1653
|
+
* arguments checked, the tree listed or read, and its `files` prepared, uploads included, under
|
|
1654
|
+
* `signal`. A `files` map given as is, or no tree, needs no preparation.
|
|
1655
|
+
*/
|
|
1656
|
+
async prepareRunTree(call, options, signal) {
|
|
1657
|
+
const parts = this.runParts(call, options);
|
|
1658
|
+
const source = await this.treeToSend(call, parts.forms, REQUEST_SENDING, signal);
|
|
1659
|
+
if (source === undefined || !("tree" in source))
|
|
1660
|
+
return { source, initial: undefined };
|
|
1661
|
+
const read = new Map();
|
|
1662
|
+
const prepared = await this.prepareFiles(source, call, REQUEST_SENDING, { signal }, read);
|
|
1663
|
+
return { source, initial: { prepared, read } };
|
|
1624
1664
|
}
|
|
1625
1665
|
// ── Pipelex product surface (hosted management routes) ─────────────────
|
|
1626
1666
|
//
|
|
@@ -1735,24 +1775,15 @@ export class PipelexApiClient {
|
|
|
1735
1775
|
* room for a publish.
|
|
1736
1776
|
*/
|
|
1737
1777
|
async createMethod(input) {
|
|
1738
|
-
const
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
* The time limit of a save carrying `files`: the client's request ceiling, as for a `start`
|
|
1748
|
-
* carrying its method, since a tree sent inline to a deployment with no blob store has no size
|
|
1749
|
-
* bound and its whole upload is charged against the limit; the poll timeout for a save sending
|
|
1750
|
-
* no tree.
|
|
1751
|
-
*/
|
|
1752
|
-
saveTimeout(files) {
|
|
1753
|
-
return files != null && Object.keys(files).length > 0
|
|
1754
|
-
? { timeoutMs: this.requestTimeoutMs }
|
|
1755
|
-
: {};
|
|
1778
|
+
const source = await this.treeToSend("createMethod", input, SAVE_SENDING);
|
|
1779
|
+
return this.saveMethodTree("POST", "methods", "createMethod", source, (files) => {
|
|
1780
|
+
const body = { name: input.name };
|
|
1781
|
+
if (files != null)
|
|
1782
|
+
body.files = files;
|
|
1783
|
+
if (input.input_data !== undefined)
|
|
1784
|
+
body.input_data = input.input_data;
|
|
1785
|
+
return body;
|
|
1786
|
+
});
|
|
1756
1787
|
}
|
|
1757
1788
|
/**
|
|
1758
1789
|
* Replace a method's draft — `PUT /v1/methods/{id}/draft` — and return the method with the
|
|
@@ -1778,24 +1809,54 @@ export class PipelexApiClient {
|
|
|
1778
1809
|
*/
|
|
1779
1810
|
async writeDraft(methodId, input) {
|
|
1780
1811
|
const endpoint = `${methodPath(methodId, "writeDraft")}/draft`;
|
|
1781
|
-
if (input.
|
|
1782
|
-
throw new RequestArgumentError("writeDraft() replaces the draft's whole tree: give it as
|
|
1783
|
-
"
|
|
1784
|
-
}
|
|
1785
|
-
const
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1812
|
+
if (treeFormsGiven(input).length === 0) {
|
|
1813
|
+
throw new RequestArgumentError("writeDraft() replaces the draft's whole tree: give it as directory, contents, tree or " +
|
|
1814
|
+
"files (files: null empties the draft).");
|
|
1815
|
+
}
|
|
1816
|
+
const source = await this.treeToSend("writeDraft", input, SAVE_SENDING);
|
|
1817
|
+
return this.saveMethodTree("PUT", endpoint, "writeDraft", source, (files) => {
|
|
1818
|
+
const body = { files: files ?? null };
|
|
1819
|
+
if (input.input_data !== undefined)
|
|
1820
|
+
body.input_data = input.input_data;
|
|
1821
|
+
if (input.expected_updated_at !== undefined) {
|
|
1822
|
+
body.expected_updated_at = input.expected_updated_at;
|
|
1823
|
+
}
|
|
1824
|
+
return body;
|
|
1825
|
+
});
|
|
1826
|
+
}
|
|
1827
|
+
/**
|
|
1828
|
+
* Send a save carrying a method's tree (`createMethod`, `writeDraft`) through the path every
|
|
1829
|
+
* request sends a tree by (`postMethodRequest`), its body built by `bodyOf` from the `files`
|
|
1830
|
+
* prepared, under `saveTimeoutMs`, and read the stored method it answers as
|
|
1831
|
+
* `requestMethodData` reads it.
|
|
1832
|
+
*/
|
|
1833
|
+
async saveMethodTree(method, endpoint, call, source, bodyOf) {
|
|
1834
|
+
const res = await this.postMethodRequest(source, call, SAVE_SENDING, {}, (files) => this.requestRaw(method, this.url(endpoint), {
|
|
1835
|
+
body: bodyOf(files),
|
|
1836
|
+
timeoutMs: this.saveTimeoutMs(files),
|
|
1837
|
+
}));
|
|
1838
|
+
if (res.status < 200 || res.status >= 300) {
|
|
1839
|
+
this.throwApiResponseError(method, endpoint, res);
|
|
1791
1840
|
}
|
|
1792
|
-
return this.
|
|
1841
|
+
return this.readObjectAnswer(method, endpoint, res);
|
|
1842
|
+
}
|
|
1843
|
+
/**
|
|
1844
|
+
* The time limit of a save carrying `files`: the client's request ceiling, as for a `start`
|
|
1845
|
+
* carrying its method, since a tree sent inline to a deployment with no blob store weighs up to
|
|
1846
|
+
* its `max_inline_bytes`, a `files` map given as is whatever it weighs, and the whole upload is
|
|
1847
|
+
* charged against the limit; the poll timeout for a save sending no tree.
|
|
1848
|
+
*/
|
|
1849
|
+
saveTimeoutMs(files) {
|
|
1850
|
+
return files != null && Object.keys(files).length > 0
|
|
1851
|
+
? this.requestTimeoutMs
|
|
1852
|
+
: POLL_REQUEST_TIMEOUT_MS;
|
|
1793
1853
|
}
|
|
1794
1854
|
/**
|
|
1795
1855
|
* The limits the deployment holds a method's tree to — `GET /v1/version`, read from
|
|
1796
1856
|
* `limits.method_tree` of the public handshake — in the shape the `mthds` package's
|
|
1797
1857
|
* `listMethodTree` takes as its `limits` option, so a tree the platform would refuse is refused
|
|
1798
|
-
* while it is listed, before any file is hashed
|
|
1858
|
+
* while it is listed, before any file is hashed. A `directory` given to any route is listed under
|
|
1859
|
+
* them already; they serve a caller listing a tree of its own:
|
|
1799
1860
|
*
|
|
1800
1861
|
* ```ts
|
|
1801
1862
|
* import { listMethodTree } from "mthds";
|
|
@@ -1805,18 +1866,29 @@ export class PipelexApiClient {
|
|
|
1805
1866
|
*
|
|
1806
1867
|
* A limit the server does not announce is absent, so a bare runner, which announces none,
|
|
1807
1868
|
* yields no limit, and none is ever assumed. A value that is not a positive integer is left out
|
|
1808
|
-
* rather than trusted.
|
|
1869
|
+
* rather than trusted. The inline limit, `max_inline_bytes`, is no listing limit: the client
|
|
1870
|
+
* applies it itself when it chooses how a tree travels.
|
|
1809
1871
|
*/
|
|
1810
1872
|
async methodTreeLimits() {
|
|
1811
|
-
return announcedTreeLimits(await this.cachedVersion());
|
|
1873
|
+
return listerLimitsOf(announcedTreeLimits(await this.cachedVersion()));
|
|
1812
1874
|
}
|
|
1813
1875
|
/**
|
|
1814
|
-
* The version handshake, asked once per
|
|
1815
|
-
* and a failure is thrown and not kept, so the next call asks again.
|
|
1876
|
+
* The version handshake, asked once per `HANDSHAKE_TTL_MS`: an answer is kept that long (see
|
|
1877
|
+
* `versionCache`), and a failure is thrown and not kept, so the next call asks again. `signal`
|
|
1878
|
+
* stops the ask, its abort thrown as is.
|
|
1816
1879
|
*/
|
|
1817
|
-
async cachedVersion() {
|
|
1818
|
-
|
|
1819
|
-
|
|
1880
|
+
async cachedVersion(signal) {
|
|
1881
|
+
const cached = this.versionCache;
|
|
1882
|
+
if (cached !== undefined && Date.now() - cached.askedAt < HANDSHAKE_TTL_MS)
|
|
1883
|
+
return cached.info;
|
|
1884
|
+
const askedAt = Date.now();
|
|
1885
|
+
const info = await this.requestVersion(signal);
|
|
1886
|
+
this.versionCache = { info, askedAt };
|
|
1887
|
+
return info;
|
|
1888
|
+
}
|
|
1889
|
+
/** Drop the kept handshake, which a refusal has shown to be stale. */
|
|
1890
|
+
forgetHandshake() {
|
|
1891
|
+
this.versionCache = undefined;
|
|
1820
1892
|
}
|
|
1821
1893
|
/**
|
|
1822
1894
|
* Write a saved method's tree to disk: each file of a listing `getMethod` or `getMethodVersion`
|
|
@@ -1833,9 +1905,10 @@ export class PipelexApiClient {
|
|
|
1833
1905
|
* The links expire about fifteen minutes after the read that served them: write the tree soon
|
|
1834
1906
|
* after reading the method, and read it again for fresh links when a download is refused.
|
|
1835
1907
|
*
|
|
1836
|
-
* Throws `RequestArgumentError` before any download when
|
|
1837
|
-
* empty directory, or the listing holds a path that is not
|
|
1838
|
-
* paths that collide once case-folded or where one names a
|
|
1908
|
+
* Throws `RequestArgumentError` before any download when it runs outside Node, when the
|
|
1909
|
+
* destination exists and is not an empty directory, or when the listing holds a path that is not
|
|
1910
|
+
* a tree path, the same path twice, or paths that collide once case-folded or where one names a
|
|
1911
|
+
* directory of another;
|
|
1839
1912
|
* `MethodFileTransferError` when a file could not be downloaded, or its bytes are not the file
|
|
1840
1913
|
* listed, and nothing is left under the destination.
|
|
1841
1914
|
*/
|
|
@@ -1843,60 +1916,173 @@ export class PipelexApiClient {
|
|
|
1843
1916
|
await writeMethodTreeTo(files, options.destination, { timeoutMs: this.requestTimeoutMs });
|
|
1844
1917
|
}
|
|
1845
1918
|
/**
|
|
1846
|
-
* The
|
|
1847
|
-
*
|
|
1848
|
-
* `RequestArgumentError` before any request, naming `call
|
|
1919
|
+
* The method's tree a call was given, made ready to prepare: a `directory` listed, `contents`
|
|
1920
|
+
* turned into a tree, a `tree` checked, `files` kept as given; `undefined` when the input gives
|
|
1921
|
+
* none. Two forms are refused with `RequestArgumentError` before any request, naming `call`, and
|
|
1922
|
+
* so are a `directory` that is not a string, a `directory` where the Node host cannot be had
|
|
1923
|
+
* (outside Node, or in a toolchain that cannot load it), and a `tree` that is not a listed tree. A
|
|
1924
|
+
* `directory` is listed by the `mthds` package's lister under the limits the deployment
|
|
1925
|
+
* announces, so a tree past one is refused while it is listed, before any file is hashed; the
|
|
1926
|
+
* handshake that announces them is asked under `signal`. It is listed once per call: a request
|
|
1927
|
+
* sent again sends the tree this listing read.
|
|
1849
1928
|
*/
|
|
1850
|
-
async
|
|
1851
|
-
const given =
|
|
1852
|
-
["files", input.files !== undefined],
|
|
1853
|
-
["tree", input.tree != null],
|
|
1854
|
-
["contents", input.contents != null],
|
|
1855
|
-
]
|
|
1856
|
-
.filter(([, present]) => present)
|
|
1857
|
-
.map(([name]) => name);
|
|
1929
|
+
async treeToSend(call, input, sending, signal) {
|
|
1930
|
+
const given = treeFormsGiven(input);
|
|
1858
1931
|
if (given.length > 1) {
|
|
1859
|
-
throw new RequestArgumentError(`${call}() takes the method's tree in one form,
|
|
1932
|
+
throw new RequestArgumentError(`${call}() takes the method's tree in one form, directory, contents, tree or files; got ` +
|
|
1860
1933
|
`${given.join(" and ")}.`);
|
|
1861
1934
|
}
|
|
1935
|
+
if (given[0] === "directory") {
|
|
1936
|
+
const directory = input.directory;
|
|
1937
|
+
if (typeof directory !== "string")
|
|
1938
|
+
throw directoryRefusal(call);
|
|
1939
|
+
// A request aborted already rejects with the caller's reason, whatever the runtime. Then,
|
|
1940
|
+
// where the Node host cannot be had, the directory is refused before the handshake, so a
|
|
1941
|
+
// failed or refused handshake cannot hide that refusal.
|
|
1942
|
+
throwIfAborted(signal);
|
|
1943
|
+
await treeListerFor(directory, call);
|
|
1944
|
+
throwIfAborted(signal);
|
|
1945
|
+
const limits = listerLimitsOf(announcedTreeLimits(await this.cachedVersion(signal)));
|
|
1946
|
+
throwIfAborted(signal);
|
|
1947
|
+
return { tree: await listDirectoryTree(directory, limits, call) };
|
|
1948
|
+
}
|
|
1862
1949
|
if (input.tree != null)
|
|
1863
|
-
return
|
|
1950
|
+
return { tree: checkedTreeSource(call, input.tree) };
|
|
1864
1951
|
if (input.contents != null) {
|
|
1865
|
-
return
|
|
1866
|
-
}
|
|
1867
|
-
return input.files;
|
|
1868
|
-
}
|
|
1869
|
-
/**
|
|
1870
|
-
*
|
|
1871
|
-
*
|
|
1872
|
-
*
|
|
1873
|
-
*
|
|
1874
|
-
*
|
|
1875
|
-
*
|
|
1876
|
-
*
|
|
1877
|
-
*
|
|
1878
|
-
*
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1952
|
+
return { tree: await contentsTreeSource(input.contents, call, sending) };
|
|
1953
|
+
}
|
|
1954
|
+
return input.files === undefined ? undefined : { files: input.files };
|
|
1955
|
+
}
|
|
1956
|
+
/**
|
|
1957
|
+
* Post a request carrying a method's tree, by `mthds`'s `MthdsApiClient` rule: prepare the
|
|
1958
|
+
* tree's `files` (`prepareFiles`), hand them to `post`, which builds the body and sends it, and
|
|
1959
|
+
* answer the two refusals that call for sending the same request once more, as `mthds` does.
|
|
1960
|
+
*
|
|
1961
|
+
* - A `422` coded `request.blobs_missing` whose `missing_blobs` the tree sent by hash holds, such
|
|
1962
|
+
* as a blob dropped since the check: the blob route is asked again for those, they are
|
|
1963
|
+
* uploaded, and the request is sent once more (`postWithBlobRetry`).
|
|
1964
|
+
* - A `422` refusing a tree the client prepared as past one of the receiver's limits
|
|
1965
|
+
* (`isMethodTreeTooLarge`): the handshake that announced the limits may be stale, so it is
|
|
1966
|
+
* forgotten, and the tree prepared again on a fresh one and sent once more. A `files` map
|
|
1967
|
+
* given as is was not prepared, so it is never sent again; nor is a request whose
|
|
1968
|
+
* `options.retryTooLarge` is `false`, a start under an idempotency key, which the platform
|
|
1969
|
+
* binds to the refusal it stores.
|
|
1970
|
+
*
|
|
1971
|
+
* A `422` created nothing, so the request sent again is no repeat of one whose outcome is
|
|
1972
|
+
* unknown. `initial`, when given, is the tree already prepared for the first send, with the
|
|
1973
|
+
* bytes it read, so a caller sending one tree on two routes prepares it once. `options.signal`
|
|
1974
|
+
* is read before each step of the preparation and before each send. Answers the last answer
|
|
1975
|
+
* whole, its status unread, with the `files` it carried.
|
|
1976
|
+
*/
|
|
1977
|
+
async postMethodRequest(source, call, sending, options, post, initial) {
|
|
1978
|
+
const read = initial?.read ?? new Map();
|
|
1979
|
+
let ready = initial?.prepared;
|
|
1980
|
+
const send = async () => {
|
|
1981
|
+
const prepared = ready ?? (await this.prepareFiles(source, call, sending, options, read));
|
|
1982
|
+
ready = undefined;
|
|
1983
|
+
throwIfAborted(options.signal);
|
|
1984
|
+
return this.postWithBlobRetry(prepared, read, post, sending, options);
|
|
1985
|
+
};
|
|
1986
|
+
const res = await send();
|
|
1987
|
+
if (source === undefined ||
|
|
1988
|
+
!("tree" in source) ||
|
|
1989
|
+
options.retryTooLarge === false ||
|
|
1990
|
+
!isMethodTreeTooLarge(res.status, res.body)) {
|
|
1991
|
+
return res;
|
|
1992
|
+
}
|
|
1993
|
+
this.forgetHandshake();
|
|
1994
|
+
return send();
|
|
1995
|
+
}
|
|
1996
|
+
/**
|
|
1997
|
+
* Send a request whose tree may have travelled by hash. When it did and the receiver answers a
|
|
1998
|
+
* `422` coded `request.blobs_missing` whose `missing_blobs` the tree holds, the client asks the
|
|
1999
|
+
* blob route where to upload them, uploads them and sends the same request once more, which the
|
|
2000
|
+
* refusal left uncreated; any other answer, or a list naming a blob the tree does not hold, is
|
|
2001
|
+
* answered as it came.
|
|
2002
|
+
*/
|
|
2003
|
+
async postWithBlobRetry(prepared, read, post, sending, options) {
|
|
2004
|
+
const res = await post(prepared.files);
|
|
2005
|
+
const tree = prepared.byHashTree;
|
|
2006
|
+
if (tree === undefined)
|
|
2007
|
+
return res;
|
|
2008
|
+
const missing = missingBlobsOf(res.status, res.body);
|
|
2009
|
+
if (missing === undefined || missing.length === 0)
|
|
2010
|
+
return res;
|
|
2011
|
+
const byBlob = fileByBlob(tree);
|
|
2012
|
+
const files = missing.map((sha256) => byBlob.get(sha256));
|
|
2013
|
+
if (files.some((file) => file === undefined))
|
|
2014
|
+
return res;
|
|
2015
|
+
throwIfAborted(options.signal);
|
|
2016
|
+
const asked = await this.findMissingBlobs({ blobs: files.map((file) => ({ sha256: file.sha256, size: file.size })) }, options.readScope, options.signal);
|
|
2017
|
+
await this.uploadMissing(tree, asked, read, sending, options.signal);
|
|
2018
|
+
throwIfAborted(options.signal);
|
|
2019
|
+
return post(prepared.files);
|
|
2020
|
+
}
|
|
2021
|
+
/**
|
|
2022
|
+
* The `files` a request or a save sends for `source`: a `files` map as given, a tree prepared
|
|
2023
|
+
* (`prepareTree`), or none.
|
|
2024
|
+
*/
|
|
2025
|
+
async prepareFiles(source, call, sending, options, read) {
|
|
2026
|
+
if (source === undefined)
|
|
2027
|
+
return { files: undefined, byHashTree: undefined };
|
|
2028
|
+
if ("files" in source)
|
|
2029
|
+
return { files: source.files, byHashTree: undefined };
|
|
2030
|
+
return this.prepareTree(source.tree, call, sending, options, read);
|
|
2031
|
+
}
|
|
2032
|
+
/**
|
|
2033
|
+
* The `files` map that sends `tree`: inline, or by hash once the receiver holds every file —
|
|
2034
|
+
* `mthds`'s `MthdsApiClient` rule, read from the version handshake the client keeps.
|
|
2035
|
+
*
|
|
2036
|
+
* When the handshake declares a blob store, a tree whose inline map weighs no more than the
|
|
2037
|
+
* inline threshold travels inline, measured as the request spends it, its JSON with no
|
|
2038
|
+
* whitespace in UTF-8; the threshold is the lower of 256 KiB (`INLINE_TREE_MAX_BYTES`) and the
|
|
2039
|
+
* `max_inline_bytes` the handshake announces, and a tree whose files alone pass it is not read
|
|
2040
|
+
* to be weighed. A heavier tree travels by hash: `POST /v1/blobs/missing` is asked which files
|
|
2041
|
+
* the receiver lacks, naming the tree's digest, each one it lacks is uploaded once, and every
|
|
2042
|
+
* file travels by hash. A `404` from the blob route says the handshake that declared the store
|
|
2043
|
+
* is stale, so it is forgotten and the tree prepared once more on a fresh one. When the
|
|
2044
|
+
* handshake declares no blob store, the whole tree travels inline whatever its weight, held to
|
|
2045
|
+
* the announced `max_inline_bytes` (`inlineWithin`). Each file is read at most once into `read`,
|
|
2046
|
+
* so a file read to be weighed is uploaded from memory. A tree holding no file is refused with
|
|
2047
|
+
* `RequestArgumentError` before any request, `call` naming the caller's method.
|
|
2048
|
+
* `options.readScope` is the scope the blob route is asked under, and `options.signal` is read
|
|
2049
|
+
* before each read, the handshake, the blob route and each upload, and is passed to each of
|
|
2050
|
+
* them, so an abort also stops a request or an upload under way.
|
|
2051
|
+
*/
|
|
2052
|
+
async prepareTree(tree, call, sending, options, read, retried = false) {
|
|
2053
|
+
const { signal, readScope } = options;
|
|
2054
|
+
if (tree.files.length === 0)
|
|
2055
|
+
throw emptyTreeRefusal(call, "a tree", sending);
|
|
2056
|
+
throwIfAborted(signal);
|
|
2057
|
+
const version = await this.cachedVersion(signal);
|
|
2058
|
+
throwIfAborted(signal);
|
|
2059
|
+
const announced = announcedTreeLimits(version);
|
|
2060
|
+
if (!declaresBlobStore(version)) {
|
|
2061
|
+
const files = await inlineWithin(tree, call, sending, announced.maxInlineBytes, read, signal);
|
|
2062
|
+
return { files, byHashTree: undefined };
|
|
2063
|
+
}
|
|
2064
|
+
const threshold = inlineThreshold(announced);
|
|
1886
2065
|
// An inline map is never smaller than the bytes it carries, so a tree whose bytes alone pass
|
|
1887
2066
|
// the threshold is not read in full just to be measured.
|
|
1888
|
-
if (treeTotalSize(tree) <=
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
2067
|
+
if (treeTotalSize(tree) <= threshold) {
|
|
2068
|
+
const inline = inlineFilesOf(tree, await readTreeBytesInto(tree, read, signal));
|
|
2069
|
+
if (inlineMapBytes(inline) <= threshold)
|
|
2070
|
+
return { files: inline, byHashTree: undefined };
|
|
2071
|
+
}
|
|
2072
|
+
throwIfAborted(signal);
|
|
2073
|
+
let asked;
|
|
2074
|
+
try {
|
|
2075
|
+
asked = await this.findMissingBlobs({ blobs: uniqueBlobs(tree), digest: tree.digest }, readScope, signal);
|
|
1893
2076
|
}
|
|
1894
|
-
|
|
1895
|
-
|
|
2077
|
+
catch (err) {
|
|
2078
|
+
if (!retried && err instanceof ApiResponseError && err.status === 404) {
|
|
2079
|
+
this.forgetHandshake();
|
|
2080
|
+
return this.prepareTree(tree, call, sending, options, read, true);
|
|
2081
|
+
}
|
|
2082
|
+
throw err;
|
|
1896
2083
|
}
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
return hashMethodFiles(tree);
|
|
2084
|
+
await this.uploadMissing(tree, asked, read, sending, signal);
|
|
2085
|
+
return { files: hashMethodFiles(tree), byHashTree: tree };
|
|
1900
2086
|
}
|
|
1901
2087
|
/**
|
|
1902
2088
|
* Which of these blobs the organization lacks, and where to upload each —
|
|
@@ -1904,10 +2090,18 @@ export class PipelexApiClient {
|
|
|
1904
2090
|
* blob held is left out, and an empty `missing` means nothing to upload. An answer whose
|
|
1905
2091
|
* `missing` is not a list of blobs, each with its upload target, is an answer the SDK cannot
|
|
1906
2092
|
* read, thrown as an `ApiResponseError` holding the answer with its links hidden.
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
2093
|
+
*
|
|
2094
|
+
* `readScope`, a crate request's `read_scope`, is sent as the route's `read_scope` query, the
|
|
2095
|
+
* Pipelex API's extension naming the blob store asked, so the blobs are uploaded into the store
|
|
2096
|
+
* the request's hash entries are read from; each upload target is signed for that scope. The
|
|
2097
|
+
* hosted platform's own route reads the caller's organization instead. `signal` stops the
|
|
2098
|
+
* request, its abort thrown as is.
|
|
2099
|
+
*/
|
|
2100
|
+
async findMissingBlobs(request, readScope, signal) {
|
|
2101
|
+
const endpoint = readScope === undefined
|
|
2102
|
+
? "blobs/missing"
|
|
2103
|
+
: `blobs/missing?${new URLSearchParams({ read_scope: readScope }).toString()}`;
|
|
2104
|
+
const res = withLinks(await this.requestProductAnswer("POST", endpoint, request, { signal }));
|
|
1911
2105
|
const answer = this.readObjectAnswer("POST", endpoint, res);
|
|
1912
2106
|
const missing = answer.missing;
|
|
1913
2107
|
if (!Array.isArray(missing) || !missing.every(isMissingBlob)) {
|
|
@@ -1921,9 +2115,12 @@ export class PipelexApiClient {
|
|
|
1921
2115
|
* is the platform's fault, thrown before anything is sent as the `ApiResponseError` of an answer
|
|
1922
2116
|
* the SDK cannot read. The bytes the tree reads for a blob are checked against it before they
|
|
1923
2117
|
* are sent, since storage keeps a blob under the hash it is named by: bytes of another hash or
|
|
1924
|
-
* size are refused with `RequestArgumentError`, and nothing more is uploaded.
|
|
2118
|
+
* size are refused with `RequestArgumentError`, and nothing more is uploaded. `userSignal` is
|
|
2119
|
+
* read before each blob's read and before its upload, and reaches each upload under way: once it
|
|
2120
|
+
* has aborted, no further blob is read or sent, the uploads in flight are stopped, and the abort
|
|
2121
|
+
* is thrown rather than a transfer failure.
|
|
1925
2122
|
*/
|
|
1926
|
-
async uploadMissing(tree, asked, read) {
|
|
2123
|
+
async uploadMissing(tree, asked, read, sending, userSignal) {
|
|
1927
2124
|
const missing = asked.result.missing;
|
|
1928
2125
|
const byBlob = fileByBlob(tree);
|
|
1929
2126
|
const strangers = missing
|
|
@@ -1932,16 +2129,40 @@ export class PipelexApiClient {
|
|
|
1932
2129
|
if (strangers.length > 0) {
|
|
1933
2130
|
throw this.unreadableAnswer("POST", `/${API_PREFIX}/blobs/missing`, asked.res, `a body asking for blobs this tree does not hold: ${[...new Set(strangers)].sort().join(", ")}`);
|
|
1934
2131
|
}
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
2132
|
+
// The tasks share the one signal the run of uploads aborts on a failure, linked here once to
|
|
2133
|
+
// the caller's and detached when the uploads are over.
|
|
2134
|
+
let linked;
|
|
2135
|
+
try {
|
|
2136
|
+
await forEachConcurrently(missing, MAX_CONCURRENT_TRANSFERS, async (blob, signal) => {
|
|
2137
|
+
linked ??= eitherSignal(signal, userSignal);
|
|
2138
|
+
await this.uploadOne(tree, blob, byBlob, read, sending, linked.signal, userSignal);
|
|
2139
|
+
});
|
|
2140
|
+
}
|
|
2141
|
+
catch (err) {
|
|
2142
|
+
if (userSignal?.aborted === true)
|
|
2143
|
+
throw userSignal.reason;
|
|
2144
|
+
throw err;
|
|
2145
|
+
}
|
|
2146
|
+
finally {
|
|
2147
|
+
linked?.dispose();
|
|
2148
|
+
}
|
|
2149
|
+
}
|
|
2150
|
+
/** Read, check and upload one missing blob (see `uploadMissing`). */
|
|
2151
|
+
async uploadOne(tree, blob, byBlob, read, sending, signal, userSignal) {
|
|
2152
|
+
const file = byBlob.get(blob.sha256);
|
|
2153
|
+
throwIfAborted(userSignal);
|
|
2154
|
+
const bytes = read?.get(file.path) ?? (await tree.readFile(file.path));
|
|
2155
|
+
const bytesSha256 = await sha256Hex(bytes);
|
|
2156
|
+
if (bytes.length !== blob.size || bytesSha256 !== blob.sha256) {
|
|
2157
|
+
throw new RequestArgumentError(`The tree read ${bytes.length} bytes hashing to ${bytesSha256} for '${file.path}', which ` +
|
|
2158
|
+
`its listing names as ${blob.size} bytes hashing to ${blob.sha256}, so they were not ` +
|
|
2159
|
+
`uploaded and ${sending.unsent}.`);
|
|
2160
|
+
}
|
|
2161
|
+
throwIfAborted(userSignal);
|
|
2162
|
+
await uploadBlob(blob, bytes, {
|
|
2163
|
+
timeoutMs: this.requestTimeoutMs,
|
|
2164
|
+
signal,
|
|
2165
|
+
unsent: sending.unsent,
|
|
1945
2166
|
});
|
|
1946
2167
|
}
|
|
1947
2168
|
/**
|
|
@@ -2438,8 +2659,9 @@ export class PipelexApiClient {
|
|
|
2438
2659
|
* Name the method exactly one of three ways, all server-resolved through the
|
|
2439
2660
|
* one `pipeIo` call this composes:
|
|
2440
2661
|
*
|
|
2441
|
-
* -
|
|
2442
|
-
*
|
|
2662
|
+
* - the method's tree — a `directory`, `contents`, a listed `tree` or the `files`
|
|
2663
|
+
* map, sent as `pipeIo` sends it, with the files its steps read (a `PipeDocGen`
|
|
2664
|
+
* step's `template_file`) among its files;
|
|
2443
2665
|
* - `method_ref` — a published method's address, fetched by the runner;
|
|
2444
2666
|
* - `method_id` — a stored method's catalog id, resolved by the platform
|
|
2445
2667
|
* (hosted only; requires an API key).
|
|
@@ -2544,9 +2766,8 @@ export class PipelexApiClient {
|
|
|
2544
2766
|
/**
|
|
2545
2767
|
* The whole record for one run — `GET /v1/runs/{id}`.
|
|
2546
2768
|
*
|
|
2547
|
-
* The ONLY call that returns
|
|
2548
|
-
*
|
|
2549
|
-
* few seconds. A failed run's stored report, `error`, is checked field by
|
|
2769
|
+
* The ONLY call that returns what the run actually executed and its
|
|
2770
|
+
* `inputs`. Kept off the status read, which pollers hit every few seconds. A failed run's stored report, `error`, is checked field by
|
|
2550
2771
|
* field (see `RunErrorReport`).
|
|
2551
2772
|
*
|
|
2552
2773
|
* Read with `parseExactJson`, so an integer past `Number.MAX_SAFE_INTEGER` in the inputs is a
|
|
@@ -2563,6 +2784,30 @@ export class PipelexApiClient {
|
|
|
2563
2784
|
const segment = idPathSegment(runId, "updateRun", RUN_ID_ARGUMENT);
|
|
2564
2785
|
await this.requestProduct("PUT", `runs/${segment}`, input);
|
|
2565
2786
|
}
|
|
2787
|
+
/**
|
|
2788
|
+
* A run's arguments checked before anything is listed, read or sent, `call` naming the caller's
|
|
2789
|
+
* method in each refusal: the generic `extra` passthrough, the Pipelex API's and the hosted
|
|
2790
|
+
* API's own selectors, and the method's tree in its forms as the standard checks them; then the
|
|
2791
|
+
* "something to run" precondition, the `method_ref` pairings and the linkage form's bare
|
|
2792
|
+
* `method_id`.
|
|
2793
|
+
*/
|
|
2794
|
+
runParts(call, options) {
|
|
2795
|
+
const extensions = buildExtensions(options.extra);
|
|
2796
|
+
const api = buildApiRunExtensions(options);
|
|
2797
|
+
const hosted = buildHostedRunExtensions(options);
|
|
2798
|
+
const forms = protocolTreeForms(call, options);
|
|
2799
|
+
const treeForms = treeFormsGiven(forms);
|
|
2800
|
+
if (!options.pipe_code &&
|
|
2801
|
+
treeForms.length === 0 &&
|
|
2802
|
+
Object.keys(api).length === 0 &&
|
|
2803
|
+
Object.keys(hosted).length === 0 &&
|
|
2804
|
+
Object.keys(extensions).length === 0) {
|
|
2805
|
+
throw new RequestArgumentError(`Either pipe_code, the method's tree (directory, contents, tree or files), a method_ref, a hosted method_id or a server-specific extension arg (extra) must be provided to ${call}().`);
|
|
2806
|
+
}
|
|
2807
|
+
assertMethodRefPairsWithNothing(options, treeForms);
|
|
2808
|
+
assertLinkageMethodIdIsBare(options, treeForms);
|
|
2809
|
+
return { forms, extensions, api, hosted };
|
|
2810
|
+
}
|
|
2566
2811
|
/**
|
|
2567
2812
|
* Blocking `POST /v1/execute` adapted onto `RunResults` — the bare-runner
|
|
2568
2813
|
* path. Forwards every protocol field PLUS every extension surface: the
|
|
@@ -2574,24 +2819,24 @@ export class PipelexApiClient {
|
|
|
2574
2819
|
* so a runner that cannot resolve it says so (Rule 4) instead of the client
|
|
2575
2820
|
* silently dropping it.
|
|
2576
2821
|
*/
|
|
2577
|
-
async executeBlocking(options) {
|
|
2578
|
-
const response = await this.
|
|
2822
|
+
async executeBlocking(options, signal, sending) {
|
|
2823
|
+
const response = await this.executeRun({
|
|
2579
2824
|
pipe_code: options.pipe_code ?? undefined,
|
|
2580
|
-
mthds_contents: options.mthds_contents ?? undefined,
|
|
2581
2825
|
inputs: options.inputs ?? undefined,
|
|
2582
2826
|
output_name: options.output_name ?? undefined,
|
|
2583
2827
|
output_multiplicity: options.output_multiplicity ?? undefined,
|
|
2584
2828
|
dynamic_output_concept_ref: options.dynamic_output_concept_ref ?? undefined,
|
|
2585
|
-
// The
|
|
2586
|
-
//
|
|
2829
|
+
// The method's tree must survive the fallback too — a bare runner reached through this path
|
|
2830
|
+
// runs the same method as the durable one, or it runs nothing. Its forms are checked here as
|
|
2831
|
+
// the start checked them, and `sending` carries the tree the start prepared, sent as is.
|
|
2832
|
+
directory: options.directory ?? undefined,
|
|
2833
|
+
contents: options.contents ?? undefined,
|
|
2834
|
+
tree: options.tree ?? undefined,
|
|
2587
2835
|
files: options.files ?? undefined,
|
|
2588
|
-
bundle_b64: options.bundle_b64 ?? undefined,
|
|
2589
|
-
// And so must the files its steps read, or the same method fails to load here.
|
|
2590
|
-
resources: options.resources ?? undefined,
|
|
2591
2836
|
method_ref: options.method_ref ?? undefined,
|
|
2592
2837
|
method_id: options.method_id ?? undefined,
|
|
2593
2838
|
extra: options.extra ?? undefined,
|
|
2594
|
-
});
|
|
2839
|
+
}, signal, sending);
|
|
2595
2840
|
return resultsFromExecute(response);
|
|
2596
2841
|
}
|
|
2597
2842
|
}
|
|
@@ -2652,26 +2897,23 @@ function describeRefusedBaseUrl(value) {
|
|
|
2652
2897
|
return `${shown} with ${inWords(parts, "and")} (not shown)`;
|
|
2653
2898
|
}
|
|
2654
2899
|
// The protocol's own request fields — `extra` is for extension args only.
|
|
2655
|
-
// `files`
|
|
2656
|
-
//
|
|
2657
|
-
//
|
|
2900
|
+
// `files` is reserved too: it is the method's tree, a named option, so smuggling
|
|
2901
|
+
// it through `extra` (which merges last into the body) would overwrite the
|
|
2902
|
+
// prepared tree and bypass the one-form check.
|
|
2658
2903
|
const PROTOCOL_REQUEST_KEYS = [
|
|
2659
2904
|
"pipe_code",
|
|
2660
|
-
"mthds_contents",
|
|
2661
2905
|
"inputs",
|
|
2662
2906
|
"output_name",
|
|
2663
2907
|
"output_multiplicity",
|
|
2664
2908
|
"dynamic_output_concept_ref",
|
|
2665
2909
|
"files",
|
|
2666
|
-
"bundle_b64",
|
|
2667
2910
|
];
|
|
2668
|
-
// The PIPELEX API's own request
|
|
2669
|
-
//
|
|
2670
|
-
// the
|
|
2671
|
-
//
|
|
2672
|
-
//
|
|
2673
|
-
|
|
2674
|
-
const PIPELEX_API_REQUEST_KEYS = ["method_ref", "resources"];
|
|
2911
|
+
// The PIPELEX API's own request field — the layer-2 extension the runner reads
|
|
2912
|
+
// itself (`method_ref`, a run source in its own right). Reserved on `extra` for
|
|
2913
|
+
// the same reason the protocol fields are: `extra` merges last, so a smuggled
|
|
2914
|
+
// copy would overwrite the validated named option and bypass the
|
|
2915
|
+
// selector-exclusivity check.
|
|
2916
|
+
const PIPELEX_API_REQUEST_KEYS = ["method_ref"];
|
|
2675
2917
|
// The HOSTED API's own request fields — the layer-3 extensions this client
|
|
2676
2918
|
// names itself. Reserved on `extra` for the same reason the protocol fields
|
|
2677
2919
|
// are: `extra` merges last, so a smuggled copy would overwrite the validated
|
|
@@ -2684,15 +2926,20 @@ const PIPELEX_API_REQUEST_KEYS = ["method_ref", "resources"];
|
|
|
2684
2926
|
// keep passing that vendor's `method_id` straight through. That per-layer
|
|
2685
2927
|
// split is normative for the whole layered client stack.
|
|
2686
2928
|
const HOSTED_REQUEST_KEYS = ["method_id"];
|
|
2929
|
+
// The client-only options, which are never serialized as such: the three forms of
|
|
2930
|
+
// the method's tree the client turns into `files` (`directory`, `contents`, `tree`)
|
|
2931
|
+
// and the `bundleMain` hint. Reserved on `extra` so none of them reaches the wire
|
|
2932
|
+
// through the passthrough under its own name.
|
|
2933
|
+
const CLIENT_ONLY_KEYS = ["directory", "contents", "tree", "bundleMain"];
|
|
2687
2934
|
// Keys that must never ride `extra`: the named request options above (which
|
|
2688
2935
|
// `extra` would overwrite — it merges last into the body) plus the client-only
|
|
2689
|
-
//
|
|
2690
|
-
//
|
|
2936
|
+
// options. A field the client no longer names, such as the runner's legacy
|
|
2937
|
+
// `mthds_contents`, is NOT reserved: `extra` is the passthrough for exactly that.
|
|
2691
2938
|
const RESERVED_EXTRA_KEYS = new Set([
|
|
2692
2939
|
...PROTOCOL_REQUEST_KEYS,
|
|
2693
2940
|
...PIPELEX_API_REQUEST_KEYS,
|
|
2694
2941
|
...HOSTED_REQUEST_KEYS,
|
|
2695
|
-
|
|
2942
|
+
...CLIENT_ONLY_KEYS,
|
|
2696
2943
|
]);
|
|
2697
2944
|
// Prototype-pollution vectors. An own `__proto__` (exactly what `JSON.parse`
|
|
2698
2945
|
// yields, and `extra` is the field most likely populated from untrusted JSON),
|
|
@@ -2733,23 +2980,110 @@ function buildExtensions(extra) {
|
|
|
2733
2980
|
// caps responses regardless.
|
|
2734
2981
|
const METHOD_REF_FETCH_TIMEOUT_MS = 180_000; // 3 min — covers the server's clone + resolve
|
|
2735
2982
|
/**
|
|
2736
|
-
*
|
|
2737
|
-
*
|
|
2738
|
-
* is
|
|
2739
|
-
*
|
|
2740
|
-
* - **`resources`** make the request *body* as large as the runner's limits allow,
|
|
2741
|
-
* about 13 MiB of base64 by default, and the whole upload is charged against
|
|
2742
|
-
* this budget. They get the client's request ceiling (`requestTimeoutMs`),
|
|
2743
|
-
* which `start` gives a request carrying them and `validate` carries them under
|
|
2744
|
-
* by default, so the same bundle and files never time out on one route yet go
|
|
2745
|
-
* through on another.
|
|
2746
|
-
* - **A `method_ref`** may make the server clone a repository first, which gets
|
|
2747
|
-
* the fetch-sized budget.
|
|
2983
|
+
* Whether a crate request's `files` carry more inline than `INLINE_TREE_MAX_BYTES`, weighed as the
|
|
2984
|
+
* request spends them: a tree sent inline to an API keeping no blob store, whatever its weight, or
|
|
2985
|
+
* a `files` map given as is. A map of hash entries weighs little, and a value that is not a map
|
|
2986
|
+
* is not weighed.
|
|
2748
2987
|
*/
|
|
2749
|
-
function
|
|
2750
|
-
if (
|
|
2751
|
-
return
|
|
2752
|
-
|
|
2988
|
+
function carriesLargeInlineTree(files) {
|
|
2989
|
+
if (files === null || files === undefined || !isPlainObject(files))
|
|
2990
|
+
return false;
|
|
2991
|
+
try {
|
|
2992
|
+
return inlineMapBytes(files) > INLINE_TREE_MAX_BYTES;
|
|
2993
|
+
}
|
|
2994
|
+
catch {
|
|
2995
|
+
// A map JSON cannot carry is refused as the caller's argument when the request is written.
|
|
2996
|
+
return false;
|
|
2997
|
+
}
|
|
2998
|
+
}
|
|
2999
|
+
/**
|
|
3000
|
+
* A run's wire body: the protocol's arguments, the method's `files`, then the Pipelex API's and
|
|
3001
|
+
* the hosted API's selectors and the `extra` passthrough. A start drops a `null` argument, as it
|
|
3002
|
+
* always has; `execute` sends its arguments as given. `?? undefined` so `JSON.stringify` drops an
|
|
3003
|
+
* absent field from the wire body.
|
|
3004
|
+
*/
|
|
3005
|
+
function runRequestBody(call, options, files, parts) {
|
|
3006
|
+
const dropNull = call === "start";
|
|
3007
|
+
return {
|
|
3008
|
+
pipe_code: dropNull ? (options.pipe_code ?? undefined) : options.pipe_code,
|
|
3009
|
+
inputs: dropNull ? (options.inputs ?? undefined) : options.inputs,
|
|
3010
|
+
output_name: dropNull ? (options.output_name ?? undefined) : options.output_name,
|
|
3011
|
+
output_multiplicity: dropNull
|
|
3012
|
+
? (options.output_multiplicity ?? undefined)
|
|
3013
|
+
: options.output_multiplicity,
|
|
3014
|
+
dynamic_output_concept_ref: dropNull
|
|
3015
|
+
? (options.dynamic_output_concept_ref ?? undefined)
|
|
3016
|
+
: options.dynamic_output_concept_ref,
|
|
3017
|
+
files: files ?? undefined,
|
|
3018
|
+
...parts.api,
|
|
3019
|
+
...parts.hosted,
|
|
3020
|
+
...parts.extensions,
|
|
3021
|
+
};
|
|
3022
|
+
}
|
|
3023
|
+
/**
|
|
3024
|
+
* The error a blocking execute throws for `err`: a `PipelineExecuteTimeoutError` when the hosted
|
|
3025
|
+
* gateway cut the request off (see `isGatewayCutOff`), timed from `sentAt`, and `err` otherwise.
|
|
3026
|
+
*/
|
|
3027
|
+
function executeCutOff(err, sentAt) {
|
|
3028
|
+
if (err instanceof RunStillRunningError)
|
|
3029
|
+
return err;
|
|
3030
|
+
const elapsedMs = performance.now() - sentAt;
|
|
3031
|
+
return isGatewayCutOff(err, elapsedMs)
|
|
3032
|
+
? new PipelineExecuteTimeoutError(elapsedMs, { cause: err })
|
|
3033
|
+
: err;
|
|
3034
|
+
}
|
|
3035
|
+
/**
|
|
3036
|
+
* Whether a run's `extra` carries a method bundle a runner reads, `mthds_contents` or
|
|
3037
|
+
* `bundle_b64`, as the `pipelex-sdk` command sends one: a start carrying one gets the request
|
|
3038
|
+
* ceiling, as the Python SDK gives it, since the bundle can make the body large.
|
|
3039
|
+
*/
|
|
3040
|
+
function carriesExtraBundle(extensions) {
|
|
3041
|
+
const contents = extensions["mthds_contents"];
|
|
3042
|
+
const carriesContents = Array.isArray(contents) ? contents.length > 0 : Boolean(contents);
|
|
3043
|
+
return carriesContents || Boolean(extensions["bundle_b64"]);
|
|
3044
|
+
}
|
|
3045
|
+
/** The refusal of a `directory` that is not a path, `call` naming the caller's method. */
|
|
3046
|
+
function directoryRefusal(call) {
|
|
3047
|
+
return new RequestArgumentError(`${call}() got a directory that is not a path: give the method's directory as a string.`);
|
|
3048
|
+
}
|
|
3049
|
+
/**
|
|
3050
|
+
* `value` as a listed tree, or the refusal of one that is not, `call` naming the caller's method:
|
|
3051
|
+
* a `tree` must have its `files`, its `digest` and a `readFile`, as the `MethodTree` the `mthds`
|
|
3052
|
+
* package's `listMethodTree` returns has, which the shape check `prepareInputs` applies too.
|
|
3053
|
+
*/
|
|
3054
|
+
function checkedTreeSource(call, value) {
|
|
3055
|
+
const fault = treeShapeFault(value);
|
|
3056
|
+
if (fault !== undefined) {
|
|
3057
|
+
throw new RequestArgumentError(`${call}() got a tree that is not a listed method tree — its files, its digest and a ` +
|
|
3058
|
+
`readFile — such as the MethodTree listMethodTree returns; got ${fault}.`);
|
|
3059
|
+
}
|
|
3060
|
+
return value;
|
|
3061
|
+
}
|
|
3062
|
+
/**
|
|
3063
|
+
* The whole tree inline, for a receiver keeping no blob store, held to the inline limit it
|
|
3064
|
+
* announces as `mthds`'s `MthdsApiClient` holds it: its files' declared sizes are checked before
|
|
3065
|
+
* anything is read, since the inline map weighs at least that much, then the encoded map, so a
|
|
3066
|
+
* tree the receiver would refuse is refused here with `RequestArgumentError`, naming the limit,
|
|
3067
|
+
* without being sent. Each file is read into `read`, under `signal`.
|
|
3068
|
+
*/
|
|
3069
|
+
async function inlineWithin(tree, call, sending, maxInlineBytes, read, signal) {
|
|
3070
|
+
if (maxInlineBytes !== undefined && treeTotalSize(tree) > maxInlineBytes) {
|
|
3071
|
+
throw inlineLimitRefusal(call, sending, "its files alone weigh", treeTotalSize(tree), maxInlineBytes);
|
|
3072
|
+
}
|
|
3073
|
+
const inline = inlineFilesOf(tree, await readTreeBytesInto(tree, read, signal));
|
|
3074
|
+
if (maxInlineBytes !== undefined) {
|
|
3075
|
+
const weight = inlineMapBytes(inline);
|
|
3076
|
+
if (weight > maxInlineBytes) {
|
|
3077
|
+
throw inlineLimitRefusal(call, sending, "its inline map weighs", weight, maxInlineBytes);
|
|
3078
|
+
}
|
|
3079
|
+
}
|
|
3080
|
+
return inline;
|
|
3081
|
+
}
|
|
3082
|
+
/** The refusal of a tree past the inline limit of a receiver keeping no blob store. */
|
|
3083
|
+
function inlineLimitRefusal(call, sending, measured, weight, limit) {
|
|
3084
|
+
return new RequestArgumentError(`${call}() cannot send the method's tree: the API keeps no blob store, so the tree travels ` +
|
|
3085
|
+
`inline, and ${measured} ${weight} bytes, past the ${limit} bytes it takes inline (its ` +
|
|
3086
|
+
`max_inline_bytes), so ${sending.unsent}.`);
|
|
2753
3087
|
}
|
|
2754
3088
|
/**
|
|
2755
3089
|
* Copy the Pipelex API's own run arguments onto the wire body. Named options,
|
|
@@ -2777,14 +3111,20 @@ function buildHostedRunExtensions(options) {
|
|
|
2777
3111
|
return methodId === undefined ? {} : { method_id: methodId };
|
|
2778
3112
|
}
|
|
2779
3113
|
/**
|
|
2780
|
-
* The
|
|
2781
|
-
*
|
|
3114
|
+
* The forms of the method's tree a protocol route (a run, `validate`) was given, checked as the
|
|
3115
|
+
* MTHDS standard checks them: its `assertOneMethodSource` on `files` and `tree` (both at once, or
|
|
3116
|
+
* either holding no file, refused whatever else the request carries), its refusal rethrown as a
|
|
2782
3117
|
* `RequestArgumentError` with the same message and the standard's error as `cause`, so it carries
|
|
2783
|
-
* a verdict like every other argument the client refuses
|
|
3118
|
+
* a verdict like every other argument the client refuses; and `contents` holding no file refused
|
|
3119
|
+
* the same way. A `null` member and an empty `directory` are absent.
|
|
2784
3120
|
*/
|
|
2785
|
-
function
|
|
3121
|
+
function protocolTreeForms(call, options) {
|
|
3122
|
+
// A `tree` the standard's check would read as a tree is checked as one first, so a value that
|
|
3123
|
+
// is not one is refused as the caller's argument rather than failing as a native TypeError.
|
|
3124
|
+
if (options.tree != null)
|
|
3125
|
+
checkedTreeSource(call, options.tree);
|
|
2786
3126
|
try {
|
|
2787
|
-
|
|
3127
|
+
assertOneMethodSource({ files: options.files ?? null, tree: options.tree ?? null });
|
|
2788
3128
|
}
|
|
2789
3129
|
catch (err) {
|
|
2790
3130
|
if (err instanceof PipelineRequestError) {
|
|
@@ -2792,32 +3132,59 @@ function assertRunSourcesExclusive(options) {
|
|
|
2792
3132
|
}
|
|
2793
3133
|
throw err;
|
|
2794
3134
|
}
|
|
3135
|
+
const contents = options.contents ?? undefined;
|
|
3136
|
+
if (contents !== undefined &&
|
|
3137
|
+
typeof contents === "object" &&
|
|
3138
|
+
Object.keys(contents).length === 0) {
|
|
3139
|
+
throw emptyTreeRefusal(call, "contents", REQUEST_SENDING);
|
|
3140
|
+
}
|
|
3141
|
+
return {
|
|
3142
|
+
directory: typeof options.directory === "string"
|
|
3143
|
+
? nonEmptyString(options.directory)
|
|
3144
|
+
: (options.directory ?? undefined),
|
|
3145
|
+
contents,
|
|
3146
|
+
tree: options.tree ?? undefined,
|
|
3147
|
+
files: options.files ?? undefined,
|
|
3148
|
+
};
|
|
3149
|
+
}
|
|
3150
|
+
/**
|
|
3151
|
+
* The names of the forms of the method's tree `input` gives, in the order the refusals name them.
|
|
3152
|
+
* `files` counts when it is not `undefined`, since a save's `files: null` empties a draft; the
|
|
3153
|
+
* other forms count when they are neither `undefined` nor `null`, and `directory` when it is not
|
|
3154
|
+
* empty either. A route that reads `files: null` as absent normalizes it before asking.
|
|
3155
|
+
*/
|
|
3156
|
+
function treeFormsGiven(input) {
|
|
3157
|
+
const given = [];
|
|
3158
|
+
if (input.directory != null && input.directory !== "")
|
|
3159
|
+
given.push("directory");
|
|
3160
|
+
if (input.contents != null)
|
|
3161
|
+
given.push("contents");
|
|
3162
|
+
if (input.tree != null)
|
|
3163
|
+
given.push("tree");
|
|
3164
|
+
if (input.files !== undefined)
|
|
3165
|
+
given.push("files");
|
|
3166
|
+
return given;
|
|
2795
3167
|
}
|
|
2796
3168
|
/**
|
|
2797
3169
|
* Enforce the run routes' `method_ref` exclusivity, mirroring the server's own
|
|
2798
|
-
* 422s so an illegal pairing fails before anything
|
|
2799
|
-
* `method_ref` is a complete run source (the fetched package carries its
|
|
2800
|
-
*
|
|
2801
|
-
*
|
|
2802
|
-
*
|
|
3170
|
+
* 422s so an illegal pairing fails before anything is listed, read or sent. A
|
|
3171
|
+
* `method_ref` is a complete run source (the fetched package carries its whole
|
|
3172
|
+
* tree and its entry pipe), so it pairs with NOTHING: not with the method's tree
|
|
3173
|
+
* in any form, and not with the hosted `method_id` — an address run has its own
|
|
3174
|
+
* provenance and needs no linkage id.
|
|
2803
3175
|
*
|
|
2804
|
-
* The one documented run-route exception is deliberately NOT here:
|
|
2805
|
-
*
|
|
2806
|
-
*
|
|
2807
|
-
* `
|
|
2808
|
-
*
|
|
2809
|
-
* mirrors the server's validator; presence semantics match it as well
|
|
2810
|
-
* (`mthds_contents` counts when non-empty, a bundle encoding counts when the
|
|
2811
|
-
* key is present, the selectors count when non-empty).
|
|
3176
|
+
* The one documented run-route exception is deliberately NOT here: a tree +
|
|
3177
|
+
* `method_id` stays legal (the tree runs; the id demotes to run-history linkage),
|
|
3178
|
+
* its one condition, a bare id, held by `assertLinkageMethodIdIsBare` below.
|
|
3179
|
+
* `pipe_code` beside a `method_ref` is legal too — it overrides the manifest's
|
|
3180
|
+
* `main_pipe`. The selectors count when non-empty.
|
|
2812
3181
|
*/
|
|
2813
|
-
function assertMethodRefPairsWithNothing(options) {
|
|
3182
|
+
function assertMethodRefPairsWithNothing(options, treeForms) {
|
|
2814
3183
|
if (nonEmptyString(options.method_ref) === undefined)
|
|
2815
3184
|
return;
|
|
2816
|
-
if (
|
|
2817
|
-
throw new RequestArgumentError(
|
|
2818
|
-
|
|
2819
|
-
if (options.files != null || options.bundle_b64 != null) {
|
|
2820
|
-
throw new RequestArgumentError("method_ref and a method bundle (bundle_b64 / files) are mutually exclusive; send one or the other.");
|
|
3185
|
+
if (treeForms.length > 0) {
|
|
3186
|
+
throw new RequestArgumentError(`method_ref and the method's tree (${treeForms.join(", ")}) are mutually exclusive: a ` +
|
|
3187
|
+
"published method's tree is its package's own. Send one or the other.");
|
|
2821
3188
|
}
|
|
2822
3189
|
if (nonEmptyString(options.method_id) !== undefined) {
|
|
2823
3190
|
throw new RequestArgumentError("method_ref and method_id are mutually exclusive: an address run carries its own provenance " +
|
|
@@ -2826,79 +3193,24 @@ function assertMethodRefPairsWithNothing(options) {
|
|
|
2826
3193
|
}
|
|
2827
3194
|
/**
|
|
2828
3195
|
* Enforce the run routes' linkage clause, mirroring the platform's own `422` so a
|
|
2829
|
-
* suffixed linkage id fails before anything
|
|
2830
|
-
*
|
|
2831
|
-
*
|
|
2832
|
-
*
|
|
2833
|
-
*
|
|
2834
|
-
*
|
|
2835
|
-
*
|
|
2836
|
-
* `mthds_contents` when non-empty and a bundle encoding when `hasBundlePayload` says
|
|
2837
|
-
* it carries one, since `nonEmptyFiles` and `nonEmptyString` drop an empty `files: {}`
|
|
2838
|
-
* or `bundle_b64: ""` from the body, and a suffixed id beside one then runs the version
|
|
2839
|
-
* it names. That differs on purpose from `assertMethodRefPairsWithNothing`, which
|
|
2840
|
-
* mirrors the platform's presence-based pairing validator. Only the suffix is checked,
|
|
2841
|
-
* by its `@`, since the catalog id's alphabet has none: the id itself stays a
|
|
2842
|
-
* pass-through the platform resolves.
|
|
3196
|
+
* suffixed linkage id fails before anything is listed, read or sent. Beside the
|
|
3197
|
+
* method's tree, in any form, the `method_id` is run-history linkage and must be a
|
|
3198
|
+
* bare catalog id: the tree is what runs, so a version suffix (`mt_…@3`,
|
|
3199
|
+
* `mt_…@draft`) would claim a version that did not. A `method_id` alone keeps its
|
|
3200
|
+
* suffix, which names the version to run. Only the suffix is checked, by its `@`,
|
|
3201
|
+
* since the catalog id's alphabet has none: the id itself stays a pass-through the
|
|
3202
|
+
* platform resolves.
|
|
2843
3203
|
*/
|
|
2844
|
-
function assertLinkageMethodIdIsBare(options) {
|
|
3204
|
+
function assertLinkageMethodIdIsBare(options, treeForms) {
|
|
2845
3205
|
const methodId = nonEmptyString(options.method_id);
|
|
2846
3206
|
if (methodId === undefined || !methodId.includes("@"))
|
|
2847
3207
|
return;
|
|
2848
|
-
|
|
2849
|
-
hasBundlePayload(options);
|
|
2850
|
-
if (!hasInlineSource)
|
|
2851
|
-
return;
|
|
2852
|
-
throw new RequestArgumentError(`method_id "${methodId}" beside an inline source is run-history linkage and must be a bare ` +
|
|
2853
|
-
"catalog id: the inline source is what runs, so a version suffix would claim a version " +
|
|
2854
|
-
"that did not. Send the bare id (parseMethodSelector(...).method_id), or drop the inline " +
|
|
2855
|
-
"source to run the version the selector names.");
|
|
2856
|
-
}
|
|
2857
|
-
/**
|
|
2858
|
-
* Enforce the run routes' `resources` pairing, mirroring the runner's own `422`s
|
|
2859
|
-
* so an illegal pairing fails before anything is encoded or sent. The files a
|
|
2860
|
-
* bundle's steps read accompany that bundle: inline `mthds_contents` or a method
|
|
2861
|
-
* bundle. Beside a `method_ref` they are refused, a published package's files
|
|
2862
|
-
* being its own, and so they are with no source at all, where no step could read
|
|
2863
|
-
* them. A hosted `method_id` without an inline source is left to the server: the
|
|
2864
|
-
* platform resolves it into the bundle the runner then reads them with.
|
|
2865
|
-
*
|
|
2866
|
-
* Presence follows the other pairing checks: `resources` counts when it carries a
|
|
2867
|
-
* file, `mthds_contents` when non-empty, a bundle when `hasBundlePayload` says so.
|
|
2868
|
-
*/
|
|
2869
|
-
function assertResourcesAccompanyABundle(options) {
|
|
2870
|
-
if (!hasBundleResources(options.resources))
|
|
3208
|
+
if (treeForms.length === 0)
|
|
2871
3209
|
return;
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
hasBundlePayload(options);
|
|
2877
|
-
if (!hasInlineSource && nonEmptyString(options.method_id) === undefined) {
|
|
2878
|
-
throw new RequestArgumentError("resources accompany the bundle whose steps read them: send them with mthds_contents or a method bundle.");
|
|
2879
|
-
}
|
|
2880
|
-
}
|
|
2881
|
-
/**
|
|
2882
|
-
* A crate request as the wire takes it: the request as given, its `resources`
|
|
2883
|
-
* encoded into the base64 map, the key left off when there is nothing to send.
|
|
2884
|
-
* A request that carries no `resources` key is posted untouched.
|
|
2885
|
-
*/
|
|
2886
|
-
async function wireCrateRequest(request) {
|
|
2887
|
-
if (typeof request !== "object" || request === null || !("resources" in request)) {
|
|
2888
|
-
return request;
|
|
2889
|
-
}
|
|
2890
|
-
const { resources, ...rest } = request;
|
|
2891
|
-
const encoded = await encodeBundleResources(resources);
|
|
2892
|
-
return encoded === undefined ? rest : { ...rest, resources: encoded };
|
|
2893
|
-
}
|
|
2894
|
-
/**
|
|
2895
|
-
* Normalize a bundle encoding for the wire: an empty map / string is NOT a
|
|
2896
|
-
* runnable bundle, so it must not be sent (the runner rejects a zero-file
|
|
2897
|
-
* bundle). Exclusivity is still checked on presence upstream, so an empty
|
|
2898
|
-
* encoding supplied alongside another source has already been rejected.
|
|
2899
|
-
*/
|
|
2900
|
-
function nonEmptyFiles(files) {
|
|
2901
|
-
return files != null && Object.keys(files).length > 0 ? files : undefined;
|
|
3210
|
+
throw new RequestArgumentError(`method_id "${methodId}" beside the method's tree is run-history linkage and must be a bare ` +
|
|
3211
|
+
"catalog id: the tree is what runs, so a version suffix would claim a version that did " +
|
|
3212
|
+
"not. Send the bare id (parseMethodSelector(...).method_id), or drop the tree to run the " +
|
|
3213
|
+
"version the selector names.");
|
|
2902
3214
|
}
|
|
2903
3215
|
function nonEmptyString(value) {
|
|
2904
3216
|
return value != null && value.length > 0 ? value : undefined;
|