@ancha-labs/cli 0.4.45 → 0.4.47

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.
@@ -76192,26 +76192,47 @@ function date4(params10) {
76192
76192
  }
76193
76193
 
76194
76194
  // packages/core/src/errors.ts
76195
+ var KIND_OF = {
76196
+ USAGE: "fix",
76197
+ CONFIG_INVALID: "fix",
76198
+ SCENE_INVALID: "fix",
76199
+ AUTH_INVALID: "fix",
76200
+ AGENT_SPAWN_FAILED: "fix",
76201
+ AGENT_UNREACHABLE: "fix",
76202
+ ENV_NOT_FOUND: "fix",
76203
+ SESSION_UNAUTHORIZED: "fix",
76204
+ CHECKS_FAILED: "fix",
76205
+ SERVER_UNREACHABLE: "retry",
76206
+ SERVER_START_FAILED: "retry",
76207
+ SESSION_CONFLICT: "retry",
76208
+ UNIMPLEMENTED_ENDPOINT: "ours",
76209
+ INTERNAL: "ours"
76210
+ };
76195
76211
  var AnchaError = class extends Error {
76196
76212
  code;
76213
+ kind;
76214
+ reference;
76197
76215
  hint;
76198
76216
  docs;
76199
76217
  constructor(code2, message9, opts = {}) {
76200
76218
  super(message9, opts.cause === void 0 ? void 0 : { cause: opts.cause });
76201
76219
  this.name = "AnchaError";
76202
76220
  this.code = code2;
76221
+ this.kind = opts.kind ?? KIND_OF[code2];
76222
+ if (opts.reference !== void 0) this.reference = opts.reference;
76203
76223
  if (opts.hint !== void 0) this.hint = opts.hint;
76204
76224
  if (opts.docs !== void 0) this.docs = opts.docs;
76205
76225
  }
76206
76226
  };
76207
76227
  function toErrorJson(e) {
76208
76228
  if (e instanceof AnchaError) {
76209
- const out = { code: e.code, message: e.message };
76229
+ const out = { code: e.code, kind: e.kind, message: e.message };
76230
+ if (e.reference !== void 0) out.reference = e.reference;
76210
76231
  if (e.hint !== void 0) out.hint = e.hint;
76211
76232
  if (e.docs !== void 0) out.docs = e.docs;
76212
76233
  return out;
76213
76234
  }
76214
- return { code: "INTERNAL", message: e instanceof Error ? e.message : String(e) };
76235
+ return { code: "INTERNAL", kind: "ours", message: e instanceof Error ? e.message : String(e) };
76215
76236
  }
76216
76237
 
76217
76238
  // packages/core/src/agent-routes.ts
@@ -187632,135 +187653,145 @@ async function startIsolationGateway(options4) {
187632
187653
  headers.host = modelHost ?? new URL(exposedOrigin ?? upstream.origin).host;
187633
187654
  delete headers["x-ancha-client-origin"];
187634
187655
  if (transform3) headers["accept-encoding"] = "identity";
187635
- const target5 = (modelHost ? secureRequest : request)({
187636
- hostname: modelAddress?.address ?? upstream.hostname.replace(/^\[|\]$/g, ""),
187637
- port: modelHost ? 443 : upstream.port || 80,
187638
- path: targetPath,
187639
- method: req.method,
187640
- headers,
187641
- agent: false,
187642
- ...modelHost && { servername: modelHost, rejectUnauthorized: true, family: modelAddress.family }
187643
- }, (reply6) => {
187644
- const observe2 = (host, status4, message9) => {
187645
- try {
187646
- options4.onModelReply?.(host, status4, message9);
187647
- } catch {
187656
+ let target5;
187657
+ try {
187658
+ target5 = (modelHost ? secureRequest : request)({
187659
+ hostname: modelAddress?.address ?? upstream.hostname.replace(/^\[|\]$/g, ""),
187660
+ port: modelHost ? 443 : upstream.port || 80,
187661
+ path: targetPath,
187662
+ method: req.method,
187663
+ headers,
187664
+ agent: false,
187665
+ ...modelHost && { servername: modelHost, rejectUnauthorized: true, family: modelAddress.family }
187666
+ }, (reply6) => {
187667
+ if (!reply6.statusCode || reply6.statusCode < 100) {
187668
+ reply6.destroy();
187669
+ reject2(res, 502, "Tool connection failed.");
187670
+ return;
187648
187671
  }
187649
- };
187650
- if (!toolCall && modelHost !== void 0 && reply6.statusCode !== void 0) {
187651
- const host = modelHost, status4 = reply6.statusCode;
187652
- if (!MODEL_REFUSALS.has(status4)) observe2(host, status4);
187653
- else {
187672
+ const observe2 = (host, status4, message9) => {
187673
+ try {
187674
+ options4.onModelReply?.(host, status4, message9);
187675
+ } catch {
187676
+ }
187677
+ };
187678
+ if (!toolCall && modelHost !== void 0 && reply6.statusCode !== void 0) {
187679
+ const host = modelHost, status4 = reply6.statusCode;
187680
+ if (!MODEL_REFUSALS.has(status4)) observe2(host, status4);
187681
+ else {
187682
+ const chunks2 = [];
187683
+ let size3 = 0, told = false;
187684
+ const tell = () => {
187685
+ if (told) return;
187686
+ told = true;
187687
+ observe2(host, status4, size3 <= REFUSAL_BYTES ? refusalMessage(Buffer.concat(chunks2), reply6.headers["content-encoding"]?.toLowerCase()) : void 0);
187688
+ };
187689
+ reply6.on("data", (chunk) => {
187690
+ size3 += chunk.length;
187691
+ if (size3 <= REFUSAL_BYTES) chunks2.push(chunk);
187692
+ });
187693
+ reply6.once("end", tell);
187694
+ reply6.once("close", tell);
187695
+ }
187696
+ }
187697
+ if (reply6.statusCode && reply6.statusCode >= 300 && reply6.statusCode < 400) {
187698
+ reply6.destroy();
187699
+ reject2(res, 502, "Tool redirects are not allowed through the sandbox gateway.");
187700
+ return;
187701
+ }
187702
+ const responseHeaders = toolCall ? { "content-type": reply6.headers["content-type"] ?? "application/json" } : cleanHeaders(reply6.headers);
187703
+ if (toolCall?.via === "mcp" && typeof reply6.headers["mcp-session-id"] === "string" && /^[\x21-\x7e]{1,256}$/.test(reply6.headers["mcp-session-id"])) responseHeaders["mcp-session-id"] = reply6.headers["mcp-session-id"];
187704
+ const successful = reply6.statusCode !== void 0 && reply6.statusCode >= 200 && reply6.statusCode < 300;
187705
+ if (toolCall?.response === "session-delete" && successful) route15.sessions.delete(toolCall.sessionId);
187706
+ if (toolCall && successful && (toolCall.response === "session-create" || toolCall.response === "session-get")) {
187654
187707
  const chunks2 = [];
187655
- let size3 = 0, told = false;
187656
- const tell = () => {
187657
- if (told) return;
187658
- told = true;
187659
- observe2(host, status4, size3 <= REFUSAL_BYTES ? refusalMessage(Buffer.concat(chunks2), reply6.headers["content-encoding"]?.toLowerCase()) : void 0);
187708
+ let size3 = 0, failed2 = false;
187709
+ const fail6 = () => {
187710
+ if (failed2) return;
187711
+ failed2 = true;
187712
+ reply6.destroy();
187713
+ if (!res.headersSent) reject2(res, 502, "Composio returned an invalid session response.");
187714
+ else res.destroy();
187660
187715
  };
187661
187716
  reply6.on("data", (chunk) => {
187662
187717
  size3 += chunk.length;
187663
- if (size3 <= REFUSAL_BYTES) chunks2.push(chunk);
187718
+ if (size3 > 1024 * 1024) fail6();
187719
+ else if (!failed2) chunks2.push(Buffer.from(chunk));
187720
+ });
187721
+ reply6.once("error", fail6);
187722
+ reply6.once("aborted", fail6);
187723
+ reply6.once("end", () => {
187724
+ if (failed2) return;
187725
+ try {
187726
+ const value5 = rewriteComposioSession(JSON.parse(Buffer.concat(chunks2).toString("utf8")), toolCall, route15, exposedOrigin);
187727
+ const body7 = JSON.stringify(value5);
187728
+ res.writeHead(reply6.statusCode, { "content-type": "application/json", "content-length": Buffer.byteLength(body7) });
187729
+ res.end(body7);
187730
+ } catch {
187731
+ fail6();
187732
+ }
187664
187733
  });
187665
- reply6.once("end", tell);
187666
- reply6.once("close", tell);
187734
+ return;
187667
187735
  }
187668
- }
187669
- if (reply6.statusCode && reply6.statusCode >= 300 && reply6.statusCode < 400) {
187670
- reply6.destroy();
187671
- reject2(res, 502, "Tool redirects are not allowed through the sandbox gateway.");
187672
- return;
187673
- }
187674
- const responseHeaders = toolCall ? { "content-type": reply6.headers["content-type"] ?? "application/json" } : cleanHeaders(reply6.headers);
187675
- if (toolCall?.via === "mcp" && typeof reply6.headers["mcp-session-id"] === "string" && /^[\x21-\x7e]{1,256}$/.test(reply6.headers["mcp-session-id"])) responseHeaders["mcp-session-id"] = reply6.headers["mcp-session-id"];
187676
- const successful = reply6.statusCode !== void 0 && reply6.statusCode >= 200 && reply6.statusCode < 300;
187677
- if (toolCall?.response === "session-delete" && successful) route15.sessions.delete(toolCall.sessionId);
187678
- if (toolCall && successful && (toolCall.response === "session-create" || toolCall.response === "session-get")) {
187679
- const chunks2 = [];
187680
- let size3 = 0, failed2 = false;
187681
- const fail6 = () => {
187682
- if (failed2) return;
187683
- failed2 = true;
187684
- reply6.destroy();
187685
- if (!res.headersSent) reject2(res, 502, "Composio returned an invalid session response.");
187686
- else res.destroy();
187687
- };
187736
+ if (toolCall) {
187737
+ let received = 0;
187738
+ reply6.on("data", (chunk) => {
187739
+ received += chunk.length;
187740
+ if (received > BODY_LIMIT) {
187741
+ reply6.destroy();
187742
+ res.destroy();
187743
+ }
187744
+ });
187745
+ }
187746
+ if (!transform3) {
187747
+ res.writeHead(reply6.statusCode ?? 502, responseHeaders);
187748
+ reply6.pipe(res);
187749
+ reply6.on("error", () => res.destroy());
187750
+ return;
187751
+ }
187752
+ const chunks = [];
187753
+ let size2 = 0;
187688
187754
  reply6.on("data", (chunk) => {
187689
- size3 += chunk.length;
187690
- if (size3 > 1024 * 1024) fail6();
187691
- else if (!failed2) chunks2.push(Buffer.from(chunk));
187755
+ size2 += chunk.length;
187756
+ if (size2 > BODY_LIMIT) {
187757
+ reply6.destroy();
187758
+ reject2(res, 502, "Tool response exceeded the sandbox gateway limit.");
187759
+ } else chunks.push(chunk);
187692
187760
  });
187693
- reply6.once("error", fail6);
187694
- reply6.once("aborted", fail6);
187695
- reply6.once("end", () => {
187696
- if (failed2) return;
187761
+ reply6.on("error", () => {
187762
+ if (!res.headersSent) reject2(res, 502, "Tool connection failed.");
187763
+ else res.destroy();
187764
+ });
187765
+ reply6.on("end", () => {
187766
+ if (size2 > BODY_LIMIT) return;
187697
187767
  try {
187698
- const value5 = rewriteComposioSession(JSON.parse(Buffer.concat(chunks2).toString("utf8")), toolCall, route15, exposedOrigin);
187768
+ let value5 = JSON.parse(Buffer.concat(chunks).toString("utf8"));
187769
+ if (transform3 === "context" && reply6.statusCode === 200) {
187770
+ const connection4 = mapped(exposedOrigin ?? "");
187771
+ value5 = { ...value5, env: connection4.env, session: connection4.session };
187772
+ }
187773
+ if (transform3 === "socket" && reply6.statusCode === 200 && value5?.ok === true) {
187774
+ const grant = new URL(value5.url);
187775
+ const expected = loopbackOrigin(session2.eventsUrl);
187776
+ if (grant.protocol !== "ws:" || grant.hostname !== expected.hostname || grant.port !== expected.port || grant.search || grant.hash || grant.username || grant.password || !/^\/_ancha\/socket\/[a-f0-9]{64}$/.test(grant.pathname)) throw new Error("Invalid socket grant");
187777
+ for (const [path7, old] of socketGrants) if (old.expires <= Date.now()) socketGrants.delete(path7);
187778
+ if (socketGrants.size >= 128) throw new Error("Too many socket grants");
187779
+ socketGrants.set(grant.pathname, { url: grant, expires: Date.now() + 3e4 });
187780
+ value5 = { ...value5, url: (exposedOrigin ?? "").replace(/^http:/, "ws:") + grant.pathname };
187781
+ }
187699
187782
  const body7 = JSON.stringify(value5);
187700
- res.writeHead(reply6.statusCode, { "content-type": "application/json", "content-length": Buffer.byteLength(body7) });
187783
+ delete responseHeaders["content-encoding"];
187784
+ delete responseHeaders["content-length"];
187785
+ res.writeHead(reply6.statusCode ?? 502, { ...responseHeaders, "content-length": Buffer.byteLength(body7) });
187701
187786
  res.end(body7);
187702
187787
  } catch {
187703
- fail6();
187704
- }
187705
- });
187706
- return;
187707
- }
187708
- if (toolCall) {
187709
- let received = 0;
187710
- reply6.on("data", (chunk) => {
187711
- received += chunk.length;
187712
- if (received > BODY_LIMIT) {
187713
- reply6.destroy();
187714
- res.destroy();
187788
+ reject2(res, 502, "Tool context was not a valid bounded response.");
187715
187789
  }
187716
187790
  });
187717
- }
187718
- if (!transform3) {
187719
- res.writeHead(reply6.statusCode ?? 502, responseHeaders);
187720
- reply6.pipe(res);
187721
- reply6.on("error", () => res.destroy());
187722
- return;
187723
- }
187724
- const chunks = [];
187725
- let size2 = 0;
187726
- reply6.on("data", (chunk) => {
187727
- size2 += chunk.length;
187728
- if (size2 > BODY_LIMIT) {
187729
- reply6.destroy();
187730
- reject2(res, 502, "Tool response exceeded the sandbox gateway limit.");
187731
- } else chunks.push(chunk);
187732
187791
  });
187733
- reply6.on("error", () => {
187734
- if (!res.headersSent) reject2(res, 502, "Tool connection failed.");
187735
- else res.destroy();
187736
- });
187737
- reply6.on("end", () => {
187738
- if (size2 > BODY_LIMIT) return;
187739
- try {
187740
- let value5 = JSON.parse(Buffer.concat(chunks).toString("utf8"));
187741
- if (transform3 === "context" && reply6.statusCode === 200) {
187742
- const connection4 = mapped(exposedOrigin ?? "");
187743
- value5 = { ...value5, env: connection4.env, session: connection4.session };
187744
- }
187745
- if (transform3 === "socket" && reply6.statusCode === 200 && value5?.ok === true) {
187746
- const grant = new URL(value5.url);
187747
- const expected = loopbackOrigin(session2.eventsUrl);
187748
- if (grant.protocol !== "ws:" || grant.hostname !== expected.hostname || grant.port !== expected.port || grant.search || grant.hash || grant.username || grant.password || !/^\/_ancha\/socket\/[a-f0-9]{64}$/.test(grant.pathname)) throw new Error("Invalid socket grant");
187749
- for (const [path7, old] of socketGrants) if (old.expires <= Date.now()) socketGrants.delete(path7);
187750
- if (socketGrants.size >= 128) throw new Error("Too many socket grants");
187751
- socketGrants.set(grant.pathname, { url: grant, expires: Date.now() + 3e4 });
187752
- value5 = { ...value5, url: (exposedOrigin ?? "").replace(/^http:/, "ws:") + grant.pathname };
187753
- }
187754
- const body7 = JSON.stringify(value5);
187755
- delete responseHeaders["content-encoding"];
187756
- delete responseHeaders["content-length"];
187757
- res.writeHead(reply6.statusCode ?? 502, { ...responseHeaders, "content-length": Buffer.byteLength(body7) });
187758
- res.end(body7);
187759
- } catch {
187760
- reject2(res, 502, "Tool context was not a valid bounded response.");
187761
- }
187762
- });
187763
- });
187792
+ } catch {
187793
+ return reject2(res, 502, "Tool connection failed.");
187794
+ }
187764
187795
  upstreamRequests.add(target5);
187765
187796
  target5.once("close", () => upstreamRequests.delete(target5));
187766
187797
  target5.once("error", () => {
package/lib/mcp-entry.mjs CHANGED
@@ -57300,14 +57300,34 @@ var StdioServerTransport = class {
57300
57300
  };
57301
57301
 
57302
57302
  // packages/core/src/errors.ts
57303
+ var KIND_OF = {
57304
+ USAGE: "fix",
57305
+ CONFIG_INVALID: "fix",
57306
+ SCENE_INVALID: "fix",
57307
+ AUTH_INVALID: "fix",
57308
+ AGENT_SPAWN_FAILED: "fix",
57309
+ AGENT_UNREACHABLE: "fix",
57310
+ ENV_NOT_FOUND: "fix",
57311
+ SESSION_UNAUTHORIZED: "fix",
57312
+ CHECKS_FAILED: "fix",
57313
+ SERVER_UNREACHABLE: "retry",
57314
+ SERVER_START_FAILED: "retry",
57315
+ SESSION_CONFLICT: "retry",
57316
+ UNIMPLEMENTED_ENDPOINT: "ours",
57317
+ INTERNAL: "ours"
57318
+ };
57303
57319
  var AnchaError = class extends Error {
57304
57320
  code;
57321
+ kind;
57322
+ reference;
57305
57323
  hint;
57306
57324
  docs;
57307
57325
  constructor(code2, message4, opts = {}) {
57308
57326
  super(message4, opts.cause === void 0 ? void 0 : { cause: opts.cause });
57309
57327
  this.name = "AnchaError";
57310
57328
  this.code = code2;
57329
+ this.kind = opts.kind ?? KIND_OF[code2];
57330
+ if (opts.reference !== void 0) this.reference = opts.reference;
57311
57331
  if (opts.hint !== void 0) this.hint = opts.hint;
57312
57332
  if (opts.docs !== void 0) this.docs = opts.docs;
57313
57333
  }