@ancha-labs/cli 0.4.44 → 0.4.46

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.
@@ -187632,135 +187632,145 @@ async function startIsolationGateway(options4) {
187632
187632
  headers.host = modelHost ?? new URL(exposedOrigin ?? upstream.origin).host;
187633
187633
  delete headers["x-ancha-client-origin"];
187634
187634
  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 {
187635
+ let target5;
187636
+ try {
187637
+ target5 = (modelHost ? secureRequest : request)({
187638
+ hostname: modelAddress?.address ?? upstream.hostname.replace(/^\[|\]$/g, ""),
187639
+ port: modelHost ? 443 : upstream.port || 80,
187640
+ path: targetPath,
187641
+ method: req.method,
187642
+ headers,
187643
+ agent: false,
187644
+ ...modelHost && { servername: modelHost, rejectUnauthorized: true, family: modelAddress.family }
187645
+ }, (reply6) => {
187646
+ if (!reply6.statusCode || reply6.statusCode < 100) {
187647
+ reply6.destroy();
187648
+ reject2(res, 502, "Tool connection failed.");
187649
+ return;
187648
187650
  }
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 {
187651
+ const observe2 = (host, status4, message9) => {
187652
+ try {
187653
+ options4.onModelReply?.(host, status4, message9);
187654
+ } catch {
187655
+ }
187656
+ };
187657
+ if (!toolCall && modelHost !== void 0 && reply6.statusCode !== void 0) {
187658
+ const host = modelHost, status4 = reply6.statusCode;
187659
+ if (!MODEL_REFUSALS.has(status4)) observe2(host, status4);
187660
+ else {
187661
+ const chunks2 = [];
187662
+ let size3 = 0, told = false;
187663
+ const tell = () => {
187664
+ if (told) return;
187665
+ told = true;
187666
+ observe2(host, status4, size3 <= REFUSAL_BYTES ? refusalMessage(Buffer.concat(chunks2), reply6.headers["content-encoding"]?.toLowerCase()) : void 0);
187667
+ };
187668
+ reply6.on("data", (chunk) => {
187669
+ size3 += chunk.length;
187670
+ if (size3 <= REFUSAL_BYTES) chunks2.push(chunk);
187671
+ });
187672
+ reply6.once("end", tell);
187673
+ reply6.once("close", tell);
187674
+ }
187675
+ }
187676
+ if (reply6.statusCode && reply6.statusCode >= 300 && reply6.statusCode < 400) {
187677
+ reply6.destroy();
187678
+ reject2(res, 502, "Tool redirects are not allowed through the sandbox gateway.");
187679
+ return;
187680
+ }
187681
+ const responseHeaders = toolCall ? { "content-type": reply6.headers["content-type"] ?? "application/json" } : cleanHeaders(reply6.headers);
187682
+ 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"];
187683
+ const successful = reply6.statusCode !== void 0 && reply6.statusCode >= 200 && reply6.statusCode < 300;
187684
+ if (toolCall?.response === "session-delete" && successful) route15.sessions.delete(toolCall.sessionId);
187685
+ if (toolCall && successful && (toolCall.response === "session-create" || toolCall.response === "session-get")) {
187654
187686
  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);
187687
+ let size3 = 0, failed2 = false;
187688
+ const fail6 = () => {
187689
+ if (failed2) return;
187690
+ failed2 = true;
187691
+ reply6.destroy();
187692
+ if (!res.headersSent) reject2(res, 502, "Composio returned an invalid session response.");
187693
+ else res.destroy();
187660
187694
  };
187661
187695
  reply6.on("data", (chunk) => {
187662
187696
  size3 += chunk.length;
187663
- if (size3 <= REFUSAL_BYTES) chunks2.push(chunk);
187697
+ if (size3 > 1024 * 1024) fail6();
187698
+ else if (!failed2) chunks2.push(Buffer.from(chunk));
187699
+ });
187700
+ reply6.once("error", fail6);
187701
+ reply6.once("aborted", fail6);
187702
+ reply6.once("end", () => {
187703
+ if (failed2) return;
187704
+ try {
187705
+ const value5 = rewriteComposioSession(JSON.parse(Buffer.concat(chunks2).toString("utf8")), toolCall, route15, exposedOrigin);
187706
+ const body7 = JSON.stringify(value5);
187707
+ res.writeHead(reply6.statusCode, { "content-type": "application/json", "content-length": Buffer.byteLength(body7) });
187708
+ res.end(body7);
187709
+ } catch {
187710
+ fail6();
187711
+ }
187664
187712
  });
187665
- reply6.once("end", tell);
187666
- reply6.once("close", tell);
187713
+ return;
187667
187714
  }
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
- };
187715
+ if (toolCall) {
187716
+ let received = 0;
187717
+ reply6.on("data", (chunk) => {
187718
+ received += chunk.length;
187719
+ if (received > BODY_LIMIT) {
187720
+ reply6.destroy();
187721
+ res.destroy();
187722
+ }
187723
+ });
187724
+ }
187725
+ if (!transform3) {
187726
+ res.writeHead(reply6.statusCode ?? 502, responseHeaders);
187727
+ reply6.pipe(res);
187728
+ reply6.on("error", () => res.destroy());
187729
+ return;
187730
+ }
187731
+ const chunks = [];
187732
+ let size2 = 0;
187688
187733
  reply6.on("data", (chunk) => {
187689
- size3 += chunk.length;
187690
- if (size3 > 1024 * 1024) fail6();
187691
- else if (!failed2) chunks2.push(Buffer.from(chunk));
187734
+ size2 += chunk.length;
187735
+ if (size2 > BODY_LIMIT) {
187736
+ reply6.destroy();
187737
+ reject2(res, 502, "Tool response exceeded the sandbox gateway limit.");
187738
+ } else chunks.push(chunk);
187739
+ });
187740
+ reply6.on("error", () => {
187741
+ if (!res.headersSent) reject2(res, 502, "Tool connection failed.");
187742
+ else res.destroy();
187692
187743
  });
187693
- reply6.once("error", fail6);
187694
- reply6.once("aborted", fail6);
187695
- reply6.once("end", () => {
187696
- if (failed2) return;
187744
+ reply6.on("end", () => {
187745
+ if (size2 > BODY_LIMIT) return;
187697
187746
  try {
187698
- const value5 = rewriteComposioSession(JSON.parse(Buffer.concat(chunks2).toString("utf8")), toolCall, route15, exposedOrigin);
187747
+ let value5 = JSON.parse(Buffer.concat(chunks).toString("utf8"));
187748
+ if (transform3 === "context" && reply6.statusCode === 200) {
187749
+ const connection4 = mapped(exposedOrigin ?? "");
187750
+ value5 = { ...value5, env: connection4.env, session: connection4.session };
187751
+ }
187752
+ if (transform3 === "socket" && reply6.statusCode === 200 && value5?.ok === true) {
187753
+ const grant = new URL(value5.url);
187754
+ const expected = loopbackOrigin(session2.eventsUrl);
187755
+ 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");
187756
+ for (const [path7, old] of socketGrants) if (old.expires <= Date.now()) socketGrants.delete(path7);
187757
+ if (socketGrants.size >= 128) throw new Error("Too many socket grants");
187758
+ socketGrants.set(grant.pathname, { url: grant, expires: Date.now() + 3e4 });
187759
+ value5 = { ...value5, url: (exposedOrigin ?? "").replace(/^http:/, "ws:") + grant.pathname };
187760
+ }
187699
187761
  const body7 = JSON.stringify(value5);
187700
- res.writeHead(reply6.statusCode, { "content-type": "application/json", "content-length": Buffer.byteLength(body7) });
187762
+ delete responseHeaders["content-encoding"];
187763
+ delete responseHeaders["content-length"];
187764
+ res.writeHead(reply6.statusCode ?? 502, { ...responseHeaders, "content-length": Buffer.byteLength(body7) });
187701
187765
  res.end(body7);
187702
187766
  } catch {
187703
- fail6();
187767
+ reject2(res, 502, "Tool context was not a valid bounded response.");
187704
187768
  }
187705
187769
  });
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();
187715
- }
187716
- });
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
- });
187733
- reply6.on("error", () => {
187734
- if (!res.headersSent) reject2(res, 502, "Tool connection failed.");
187735
- else res.destroy();
187736
187770
  });
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
- });
187771
+ } catch {
187772
+ return reject2(res, 502, "Tool connection failed.");
187773
+ }
187764
187774
  upstreamRequests.add(target5);
187765
187775
  target5.once("close", () => upstreamRequests.delete(target5));
187766
187776
  target5.once("error", () => {