@productbrain/mcp 0.0.1-beta.15168 → 0.0.1-beta.15186

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.
@@ -799,6 +799,46 @@ function formatGatewaySeamSummary(seam) {
799
799
  return lines.join("\n");
800
800
  }
801
801
 
802
+ // src/lib/unavailableWords.ts
803
+ var UNAVAILABLE_STATUSES = [502, 503, 504];
804
+ function isUnavailableStatus(status) {
805
+ return UNAVAILABLE_STATUSES.includes(status);
806
+ }
807
+ var DOWN = "Product Brain is down on our side.";
808
+ var START_AGAIN = "Start again in a few minutes, and capture nothing until then.";
809
+ var UNAVAILABLE = {
810
+ /** What happened, at a door that was checking the key or connect command: it names neither. */
811
+ down: DOWN,
812
+ /**
813
+ * What happened, on any other call. Read from the status alone, and a proxy on the caller's side can answer the same
814
+ * statuses, so it never says the network is fine.
815
+ */
816
+ line: `${DOWN} It is not your key.`,
817
+ /** A read, any of the three statuses: nothing was typed or saved, so only when to try again. */
818
+ tryLater: "Try again in a few minutes.",
819
+ /** A write answered 503, or a preview: the host ran nothing, so nothing reached the Chain. */
820
+ nothingSaved: "Nothing was saved. Keep your text and try again in a few minutes.",
821
+ /** A 502 or 504 on a write: the host may have stopped waiting after the write landed. */
822
+ maySaved: "It may have saved. Check before you try again.",
823
+ /** pb login keeps the key unchecked, as it does when the network is down. */
824
+ keySaved: "Your key is saved but not checked yet.",
825
+ /** pb connect: the same command works again until it expires. */
826
+ connectAgain: "Try the same connect command again in a few minutes.",
827
+ /**
828
+ * pb entry accept with several ids, its closing line: ids answered 503, none may have saved. An id refused for its own
829
+ * reason was answered, so no batch line covers it; its own line carries its fix.
830
+ */
831
+ batchAgain: "In a few minutes, accept again those Product Brain did not answer.",
832
+ /** The same, when an id answered 502 or 504 may have saved: check those before any accept again. */
833
+ batchCheck: "In a few minutes, check the ones that may have saved.",
834
+ /** After batchCheck, when other ids were answered 503 too. */
835
+ batchThenOthers: "Then accept again the others Product Brain did not answer.",
836
+ /** pb session start, typed or run by a hook: no seat could have opened. */
837
+ noSession: `No session opened: ${DOWN} ${START_AGAIN}`,
838
+ /** pb session start when the seat call itself got a 502 or 504: the seat may have opened, and a rerun reuses it. */
839
+ startFailed: `Session start failed: ${DOWN} ${START_AGAIN}`
840
+ };
841
+
802
842
  // src/gatewaySeam.ts
803
843
  var KernelCallError = class extends Error {
804
844
  status;
@@ -818,7 +858,9 @@ var KernelCallError = class extends Error {
818
858
  details;
819
859
  /** DEC-2108 (TASK-883): a 429's seconds until the key's next free slot, read by classifyError and the one retry. */
820
860
  retryAfterSeconds;
821
- constructor(message, status, code, missingRequiredFields, fieldErrors, diagnostics, details, retryAfterSeconds) {
861
+ /** TASK-1849: on a 502 or 504 from the host, whether this call could have landed a write first (mayHaveLandedOnTimeout). */
862
+ mayHaveLanded;
863
+ constructor(message, status, code, missingRequiredFields, fieldErrors, diagnostics, details, retryAfterSeconds, mayHaveLanded) {
822
864
  super(message);
823
865
  this.name = "KernelCallError";
824
866
  this.status = status;
@@ -828,19 +870,22 @@ var KernelCallError = class extends Error {
828
870
  this.diagnostics = diagnostics;
829
871
  this.details = details;
830
872
  this.retryAfterSeconds = retryAfterSeconds;
873
+ this.mayHaveLanded = mayHaveLanded;
831
874
  }
832
875
  };
833
- function kernelCallErrorFrom(fn, status, body) {
876
+ function kernelCallErrorFrom(fn, status, body, args) {
834
877
  const msg = body.error ?? body.message ?? "unknown error";
878
+ const down = isUnavailableStatus(status);
835
879
  return new KernelCallError(
836
880
  `MCP call "${fn}" failed (${status}): ${msg}`,
837
881
  status,
838
- body.code,
882
+ down ? "BACKEND_UNAVAILABLE" : body.code,
839
883
  Array.isArray(body.missingRequiredFields) ? body.missingRequiredFields : void 0,
840
884
  Array.isArray(body.fieldErrors) ? body.fieldErrors : void 0,
841
885
  body.diagnostics && typeof body.diagnostics === "object" ? body.diagnostics : void 0,
842
886
  Array.isArray(body.details) ? body.details : void 0,
843
- typeof body.retryAfterSeconds === "number" ? body.retryAfterSeconds : void 0
887
+ typeof body.retryAfterSeconds === "number" ? body.retryAfterSeconds : void 0,
888
+ down ? status !== 503 && mayHaveLandedOnTimeout(fn, args) : void 0
844
889
  );
845
890
  }
846
891
  var MAX_RATE_LIMIT_RETRY_WAIT_SECONDS = 60;
@@ -1196,6 +1241,11 @@ function classifyError(err) {
1196
1241
  const mcpErr = asKernelCallError(err);
1197
1242
  if (mcpErr) {
1198
1243
  const { code: convexCode, fn, cleanMessage, missingRequiredFields, fieldErrors } = mcpErr;
1244
+ if (convexCode === "BACKEND_UNAVAILABLE") {
1245
+ const maySaved = err.mayHaveLanded === true;
1246
+ const next2 = !routeMayMutate(fn) ? UNAVAILABLE.tryLater : maySaved ? UNAVAILABLE.maySaved : UNAVAILABLE.nothingSaved;
1247
+ return { code: "BACKEND_UNAVAILABLE", fn, message: UNAVAILABLE.line, recovery: next2 };
1248
+ }
1199
1249
  if (convexCode === "VALIDATION_FAILED") {
1200
1250
  const questionDoor = questionDoorLines(mcpErr.details);
1201
1251
  if (questionDoor) return { code: "VALIDATION_ERROR", fn, message: `${questionDoor.line}
@@ -1709,7 +1759,7 @@ async function callGateway(fn, args, recall = false) {
1709
1759
  const msg = errJson.error ?? errJson.message ?? "unknown error";
1710
1760
  audit(fn, "error", Date.now() - start, errJson.code ? `${msg} [${errJson.code}]` : msg, { budgetMs });
1711
1761
  if (res.status === 429) touch.release();
1712
- throw kernelCallErrorFrom(fn, res.status, errJson);
1762
+ throw kernelCallErrorFrom(fn, res.status, errJson, args);
1713
1763
  }
1714
1764
  audit(fn, "ok", Date.now() - start, void 0, { budgetMs });
1715
1765
  const { data, summary, next, _meta } = json;
@@ -1877,6 +1927,7 @@ export {
1877
1927
  getAuditLog,
1878
1928
  getMergedGatewaySeamCounters,
1879
1929
  formatGatewaySeamSummary,
1930
+ UNAVAILABLE,
1880
1931
  KernelCallError,
1881
1932
  hashKey,
1882
1933
  runWithAuth,
@@ -1950,4 +2001,4 @@ export {
1950
2001
  appUrl,
1951
2002
  apiKeysUrl
1952
2003
  };
1953
- //# sourceMappingURL=chunk-I6YEAE64.js.map
2004
+ //# sourceMappingURL=chunk-A5B64CDC.js.map