@ancha-labs/cli 0.4.45 → 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.
- package/lib/ancha.mjs +220 -180
- package/lib/assets/harbor/companion.mjs +123 -113
- package/lib/worker.mjs +165 -122
- package/package.json +1 -1
package/lib/ancha.mjs
CHANGED
|
@@ -108101,10 +108101,10 @@ var init_connect_registry = __esm({
|
|
|
108101
108101
|
});
|
|
108102
108102
|
|
|
108103
108103
|
// packages/server/src/twins/stripe/connect.ts
|
|
108104
|
-
function requestAccount(ctx, header4,
|
|
108104
|
+
function requestAccount(ctx, header4, routeKey2) {
|
|
108105
108105
|
if (header4 === void 0) return null;
|
|
108106
108106
|
const id15 = header4.trim();
|
|
108107
|
-
if (!ACCOUNT_SCOPED.has(
|
|
108107
|
+
if (!ACCOUNT_SCOPED.has(routeKey2)) unsupported8(`the Stripe-Account header on ${routeKey2}`);
|
|
108108
108108
|
const account9 = load2(ctx, id15);
|
|
108109
108109
|
if (!account9 || account9.object !== "account" || account9.deleted) {
|
|
108110
108110
|
throw new StripeError(403, "invalid_request_error", `The provided key does not have access to account '${id15}' (or that account does not exist). Application access may have been revoked.`, { code: "account_invalid" });
|
|
@@ -232034,135 +232034,145 @@ async function startIsolationGateway(options5) {
|
|
|
232034
232034
|
headers.host = modelHost ?? new URL(exposedOrigin ?? upstream.origin).host;
|
|
232035
232035
|
delete headers["x-ancha-client-origin"];
|
|
232036
232036
|
if (transform3) headers["accept-encoding"] = "identity";
|
|
232037
|
-
|
|
232038
|
-
|
|
232039
|
-
|
|
232040
|
-
|
|
232041
|
-
|
|
232042
|
-
|
|
232043
|
-
|
|
232044
|
-
|
|
232045
|
-
|
|
232046
|
-
|
|
232047
|
-
|
|
232048
|
-
|
|
232049
|
-
|
|
232037
|
+
let target6;
|
|
232038
|
+
try {
|
|
232039
|
+
target6 = (modelHost ? secureRequest : request)({
|
|
232040
|
+
hostname: modelAddress?.address ?? upstream.hostname.replace(/^\[|\]$/g, ""),
|
|
232041
|
+
port: modelHost ? 443 : upstream.port || 80,
|
|
232042
|
+
path: targetPath,
|
|
232043
|
+
method: req.method,
|
|
232044
|
+
headers,
|
|
232045
|
+
agent: false,
|
|
232046
|
+
...modelHost && { servername: modelHost, rejectUnauthorized: true, family: modelAddress.family }
|
|
232047
|
+
}, (reply6) => {
|
|
232048
|
+
if (!reply6.statusCode || reply6.statusCode < 100) {
|
|
232049
|
+
reply6.destroy();
|
|
232050
|
+
reject3(res, 502, "Tool connection failed.");
|
|
232051
|
+
return;
|
|
232050
232052
|
}
|
|
232051
|
-
|
|
232052
|
-
|
|
232053
|
-
|
|
232054
|
-
|
|
232055
|
-
|
|
232053
|
+
const observe2 = (host, status6, message8) => {
|
|
232054
|
+
try {
|
|
232055
|
+
options5.onModelReply?.(host, status6, message8);
|
|
232056
|
+
} catch {
|
|
232057
|
+
}
|
|
232058
|
+
};
|
|
232059
|
+
if (!toolCall && modelHost !== void 0 && reply6.statusCode !== void 0) {
|
|
232060
|
+
const host = modelHost, status6 = reply6.statusCode;
|
|
232061
|
+
if (!MODEL_REFUSALS.has(status6)) observe2(host, status6);
|
|
232062
|
+
else {
|
|
232063
|
+
const chunks2 = [];
|
|
232064
|
+
let size3 = 0, told = false;
|
|
232065
|
+
const tell = () => {
|
|
232066
|
+
if (told) return;
|
|
232067
|
+
told = true;
|
|
232068
|
+
observe2(host, status6, size3 <= REFUSAL_BYTES ? refusalMessage(Buffer.concat(chunks2), reply6.headers["content-encoding"]?.toLowerCase()) : void 0);
|
|
232069
|
+
};
|
|
232070
|
+
reply6.on("data", (chunk) => {
|
|
232071
|
+
size3 += chunk.length;
|
|
232072
|
+
if (size3 <= REFUSAL_BYTES) chunks2.push(chunk);
|
|
232073
|
+
});
|
|
232074
|
+
reply6.once("end", tell);
|
|
232075
|
+
reply6.once("close", tell);
|
|
232076
|
+
}
|
|
232077
|
+
}
|
|
232078
|
+
if (reply6.statusCode && reply6.statusCode >= 300 && reply6.statusCode < 400) {
|
|
232079
|
+
reply6.destroy();
|
|
232080
|
+
reject3(res, 502, "Tool redirects are not allowed through the sandbox gateway.");
|
|
232081
|
+
return;
|
|
232082
|
+
}
|
|
232083
|
+
const responseHeaders = toolCall ? { "content-type": reply6.headers["content-type"] ?? "application/json" } : cleanHeaders(reply6.headers);
|
|
232084
|
+
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"];
|
|
232085
|
+
const successful2 = reply6.statusCode !== void 0 && reply6.statusCode >= 200 && reply6.statusCode < 300;
|
|
232086
|
+
if (toolCall?.response === "session-delete" && successful2) route15.sessions.delete(toolCall.sessionId);
|
|
232087
|
+
if (toolCall && successful2 && (toolCall.response === "session-create" || toolCall.response === "session-get")) {
|
|
232056
232088
|
const chunks2 = [];
|
|
232057
|
-
let size3 = 0,
|
|
232058
|
-
const
|
|
232059
|
-
if (
|
|
232060
|
-
|
|
232061
|
-
|
|
232089
|
+
let size3 = 0, failed2 = false;
|
|
232090
|
+
const fail9 = () => {
|
|
232091
|
+
if (failed2) return;
|
|
232092
|
+
failed2 = true;
|
|
232093
|
+
reply6.destroy();
|
|
232094
|
+
if (!res.headersSent) reject3(res, 502, "Composio returned an invalid session response.");
|
|
232095
|
+
else res.destroy();
|
|
232062
232096
|
};
|
|
232063
232097
|
reply6.on("data", (chunk) => {
|
|
232064
232098
|
size3 += chunk.length;
|
|
232065
|
-
if (size3
|
|
232099
|
+
if (size3 > 1024 * 1024) fail9();
|
|
232100
|
+
else if (!failed2) chunks2.push(Buffer.from(chunk));
|
|
232066
232101
|
});
|
|
232067
|
-
reply6.once("
|
|
232068
|
-
reply6.once("
|
|
232102
|
+
reply6.once("error", fail9);
|
|
232103
|
+
reply6.once("aborted", fail9);
|
|
232104
|
+
reply6.once("end", () => {
|
|
232105
|
+
if (failed2) return;
|
|
232106
|
+
try {
|
|
232107
|
+
const value6 = rewriteComposioSession(JSON.parse(Buffer.concat(chunks2).toString("utf8")), toolCall, route15, exposedOrigin);
|
|
232108
|
+
const body8 = JSON.stringify(value6);
|
|
232109
|
+
res.writeHead(reply6.statusCode, { "content-type": "application/json", "content-length": Buffer.byteLength(body8) });
|
|
232110
|
+
res.end(body8);
|
|
232111
|
+
} catch {
|
|
232112
|
+
fail9();
|
|
232113
|
+
}
|
|
232114
|
+
});
|
|
232115
|
+
return;
|
|
232069
232116
|
}
|
|
232070
|
-
|
|
232071
|
-
|
|
232072
|
-
|
|
232073
|
-
|
|
232074
|
-
|
|
232075
|
-
|
|
232076
|
-
|
|
232077
|
-
|
|
232078
|
-
|
|
232079
|
-
|
|
232080
|
-
|
|
232081
|
-
|
|
232082
|
-
|
|
232083
|
-
|
|
232084
|
-
|
|
232085
|
-
|
|
232086
|
-
|
|
232087
|
-
|
|
232088
|
-
else res.destroy();
|
|
232089
|
-
};
|
|
232117
|
+
if (toolCall) {
|
|
232118
|
+
let received = 0;
|
|
232119
|
+
reply6.on("data", (chunk) => {
|
|
232120
|
+
received += chunk.length;
|
|
232121
|
+
if (received > BODY_LIMIT) {
|
|
232122
|
+
reply6.destroy();
|
|
232123
|
+
res.destroy();
|
|
232124
|
+
}
|
|
232125
|
+
});
|
|
232126
|
+
}
|
|
232127
|
+
if (!transform3) {
|
|
232128
|
+
res.writeHead(reply6.statusCode ?? 502, responseHeaders);
|
|
232129
|
+
reply6.pipe(res);
|
|
232130
|
+
reply6.on("error", () => res.destroy());
|
|
232131
|
+
return;
|
|
232132
|
+
}
|
|
232133
|
+
const chunks = [];
|
|
232134
|
+
let size2 = 0;
|
|
232090
232135
|
reply6.on("data", (chunk) => {
|
|
232091
|
-
|
|
232092
|
-
if (
|
|
232093
|
-
|
|
232136
|
+
size2 += chunk.length;
|
|
232137
|
+
if (size2 > BODY_LIMIT) {
|
|
232138
|
+
reply6.destroy();
|
|
232139
|
+
reject3(res, 502, "Tool response exceeded the sandbox gateway limit.");
|
|
232140
|
+
} else chunks.push(chunk);
|
|
232094
232141
|
});
|
|
232095
|
-
reply6.
|
|
232096
|
-
|
|
232097
|
-
|
|
232098
|
-
|
|
232142
|
+
reply6.on("error", () => {
|
|
232143
|
+
if (!res.headersSent) reject3(res, 502, "Tool connection failed.");
|
|
232144
|
+
else res.destroy();
|
|
232145
|
+
});
|
|
232146
|
+
reply6.on("end", () => {
|
|
232147
|
+
if (size2 > BODY_LIMIT) return;
|
|
232099
232148
|
try {
|
|
232100
|
-
|
|
232149
|
+
let value6 = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
232150
|
+
if (transform3 === "context" && reply6.statusCode === 200) {
|
|
232151
|
+
const connection4 = mapped(exposedOrigin ?? "");
|
|
232152
|
+
value6 = { ...value6, env: connection4.env, session: connection4.session };
|
|
232153
|
+
}
|
|
232154
|
+
if (transform3 === "socket" && reply6.statusCode === 200 && value6?.ok === true) {
|
|
232155
|
+
const grant = new URL(value6.url);
|
|
232156
|
+
const expected = loopbackOrigin(session2.eventsUrl);
|
|
232157
|
+
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");
|
|
232158
|
+
for (const [path10, old] of socketGrants) if (old.expires <= Date.now()) socketGrants.delete(path10);
|
|
232159
|
+
if (socketGrants.size >= 128) throw new Error("Too many socket grants");
|
|
232160
|
+
socketGrants.set(grant.pathname, { url: grant, expires: Date.now() + 3e4 });
|
|
232161
|
+
value6 = { ...value6, url: (exposedOrigin ?? "").replace(/^http:/, "ws:") + grant.pathname };
|
|
232162
|
+
}
|
|
232101
232163
|
const body8 = JSON.stringify(value6);
|
|
232102
|
-
|
|
232164
|
+
delete responseHeaders["content-encoding"];
|
|
232165
|
+
delete responseHeaders["content-length"];
|
|
232166
|
+
res.writeHead(reply6.statusCode ?? 502, { ...responseHeaders, "content-length": Buffer.byteLength(body8) });
|
|
232103
232167
|
res.end(body8);
|
|
232104
232168
|
} catch {
|
|
232105
|
-
|
|
232169
|
+
reject3(res, 502, "Tool context was not a valid bounded response.");
|
|
232106
232170
|
}
|
|
232107
232171
|
});
|
|
232108
|
-
return;
|
|
232109
|
-
}
|
|
232110
|
-
if (toolCall) {
|
|
232111
|
-
let received = 0;
|
|
232112
|
-
reply6.on("data", (chunk) => {
|
|
232113
|
-
received += chunk.length;
|
|
232114
|
-
if (received > BODY_LIMIT) {
|
|
232115
|
-
reply6.destroy();
|
|
232116
|
-
res.destroy();
|
|
232117
|
-
}
|
|
232118
|
-
});
|
|
232119
|
-
}
|
|
232120
|
-
if (!transform3) {
|
|
232121
|
-
res.writeHead(reply6.statusCode ?? 502, responseHeaders);
|
|
232122
|
-
reply6.pipe(res);
|
|
232123
|
-
reply6.on("error", () => res.destroy());
|
|
232124
|
-
return;
|
|
232125
|
-
}
|
|
232126
|
-
const chunks = [];
|
|
232127
|
-
let size2 = 0;
|
|
232128
|
-
reply6.on("data", (chunk) => {
|
|
232129
|
-
size2 += chunk.length;
|
|
232130
|
-
if (size2 > BODY_LIMIT) {
|
|
232131
|
-
reply6.destroy();
|
|
232132
|
-
reject3(res, 502, "Tool response exceeded the sandbox gateway limit.");
|
|
232133
|
-
} else chunks.push(chunk);
|
|
232134
|
-
});
|
|
232135
|
-
reply6.on("error", () => {
|
|
232136
|
-
if (!res.headersSent) reject3(res, 502, "Tool connection failed.");
|
|
232137
|
-
else res.destroy();
|
|
232138
232172
|
});
|
|
232139
|
-
|
|
232140
|
-
|
|
232141
|
-
|
|
232142
|
-
let value6 = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
232143
|
-
if (transform3 === "context" && reply6.statusCode === 200) {
|
|
232144
|
-
const connection4 = mapped(exposedOrigin ?? "");
|
|
232145
|
-
value6 = { ...value6, env: connection4.env, session: connection4.session };
|
|
232146
|
-
}
|
|
232147
|
-
if (transform3 === "socket" && reply6.statusCode === 200 && value6?.ok === true) {
|
|
232148
|
-
const grant = new URL(value6.url);
|
|
232149
|
-
const expected = loopbackOrigin(session2.eventsUrl);
|
|
232150
|
-
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");
|
|
232151
|
-
for (const [path10, old] of socketGrants) if (old.expires <= Date.now()) socketGrants.delete(path10);
|
|
232152
|
-
if (socketGrants.size >= 128) throw new Error("Too many socket grants");
|
|
232153
|
-
socketGrants.set(grant.pathname, { url: grant, expires: Date.now() + 3e4 });
|
|
232154
|
-
value6 = { ...value6, url: (exposedOrigin ?? "").replace(/^http:/, "ws:") + grant.pathname };
|
|
232155
|
-
}
|
|
232156
|
-
const body8 = JSON.stringify(value6);
|
|
232157
|
-
delete responseHeaders["content-encoding"];
|
|
232158
|
-
delete responseHeaders["content-length"];
|
|
232159
|
-
res.writeHead(reply6.statusCode ?? 502, { ...responseHeaders, "content-length": Buffer.byteLength(body8) });
|
|
232160
|
-
res.end(body8);
|
|
232161
|
-
} catch {
|
|
232162
|
-
reject3(res, 502, "Tool context was not a valid bounded response.");
|
|
232163
|
-
}
|
|
232164
|
-
});
|
|
232165
|
-
});
|
|
232173
|
+
} catch {
|
|
232174
|
+
return reject3(res, 502, "Tool connection failed.");
|
|
232175
|
+
}
|
|
232166
232176
|
upstreamRequests.add(target6);
|
|
232167
232177
|
target6.once("close", () => upstreamRequests.delete(target6));
|
|
232168
232178
|
target6.once("error", () => {
|
|
@@ -232490,7 +232500,8 @@ function createComposioConnection(options5) {
|
|
|
232490
232500
|
signal.removeEventListener("abort", abort);
|
|
232491
232501
|
}
|
|
232492
232502
|
}
|
|
232493
|
-
const tag3 = (expiry, lease) => createHmac13("sha256",
|
|
232503
|
+
const tag3 = (expiry, lease, key5 = options5.apiKey) => createHmac13("sha256", key5).update(`ancha-route:${expiry}:${lease}`).digest("hex").slice(0, 16);
|
|
232504
|
+
const signers = [options5.apiKey, ...options5.savedApiKey && options5.savedApiKey !== options5.apiKey ? [options5.savedApiKey] : []];
|
|
232494
232505
|
async function sweep() {
|
|
232495
232506
|
const signal = AbortSignal.timeout(5e3);
|
|
232496
232507
|
let cursor4, deletions = 0;
|
|
@@ -232508,7 +232519,7 @@ function createComposioConnection(options5) {
|
|
|
232508
232519
|
if (!config12.success || visited.has(config12.data.id)) continue;
|
|
232509
232520
|
visited.add(config12.data.id);
|
|
232510
232521
|
const match3 = /^ancha-route-(\d{10})-([a-z0-9]{12})-([a-f0-9]{16})$/.exec(config12.data.name);
|
|
232511
|
-
if (!match3 || Number(match3[1]) * 1e3 >= Date.now() || !timingSafeEqual12(Buffer.from(match3[3]), Buffer.from(tag3(match3[1], match3[2])))) continue;
|
|
232522
|
+
if (!match3 || Number(match3[1]) * 1e3 >= Date.now() || !signers.some((key5) => timingSafeEqual12(Buffer.from(match3[3]), Buffer.from(tag3(match3[1], match3[2], key5))))) continue;
|
|
232512
232523
|
const accounts2 = await call4(`/connected_accounts?${new URLSearchParams({ auth_config_ids: config12.data.id, limit: "50" })}`, "GET", void 0, signal);
|
|
232513
232524
|
if (accounts2.status < 200 || accounts2.status >= 300) continue;
|
|
232514
232525
|
const listed2 = pageSchema.safeParse(accounts2.data);
|
|
@@ -232660,6 +232671,77 @@ var init_composio_connection = __esm({
|
|
|
232660
232671
|
}
|
|
232661
232672
|
});
|
|
232662
232673
|
|
|
232674
|
+
// backend/src/execution/connection-route.ts
|
|
232675
|
+
function routedTools(routes2) {
|
|
232676
|
+
return ["quickbooks", "netsuite"].filter((tool) => routes2?.[tool] === "composio");
|
|
232677
|
+
}
|
|
232678
|
+
function routedProbeTask(routes2) {
|
|
232679
|
+
const systems = routedTools(routes2).map((tool) => tool === "netsuite" ? "NetSuite (NETSUITE_RUN_SUITEQL_QUERY)" : "QuickBooks (QUICKBOOKS_QUERY_ENTITIES)");
|
|
232680
|
+
return `Ancha connection check: read one customer in each of these systems and reply with their names: ${systems.join(", ")}. Change nothing.`;
|
|
232681
|
+
}
|
|
232682
|
+
function unusableRouteKey(value6) {
|
|
232683
|
+
const key5 = routeKey(value6);
|
|
232684
|
+
if (!key5 || /^[\x21-\x7e]+$/.test(key5)) return void 0;
|
|
232685
|
+
return /[\r\n]/.test(key5) ? "a line break" : /[ \t]/.test(key5) ? "a space or tab" : /[^\x00-\x7f]/.test(key5) ? "a character that is not plain ASCII, such as a curly quote" : "a control character";
|
|
232686
|
+
}
|
|
232687
|
+
function connectionRouteEvidence(routes2, summary, trace2, log3) {
|
|
232688
|
+
const tools2 = routedTools(routes2);
|
|
232689
|
+
if (!tools2.length) return { details: [] };
|
|
232690
|
+
const counts2 = tools2.map((tool) => ({ tool, route: summary?.[tool] }));
|
|
232691
|
+
const details = counts2.map(({ tool, route: route15 }) => `${tool === "netsuite" ? "NetSuite" : "QuickBooks"} via Composio: ${route15?.executions ?? 0} execution${route15?.executions === 1 ? "" : "s"}, ${route15?.reached ?? 0} reached the world, ${route15?.refused ?? 0} refused${route15?.via ? ` \xB7 via direct ${route15.via.direct} / session ${route15.via.session} / MCP ${route15.via.mcp}` : ""}`);
|
|
232692
|
+
if (routeKeyRejected(trace2)) return { details, reason: { ...ROUTE_KEY_REASON } };
|
|
232693
|
+
const refused2 = trace2.filter((entry3) => entry3.routeEvent?.leg === "platform" && entry3.routeEvent.op === "refused");
|
|
232694
|
+
for (const entry3 of refused2.slice(0, 5)) {
|
|
232695
|
+
const event3 = entry3.routeEvent;
|
|
232696
|
+
if (event3.leg !== "platform") continue;
|
|
232697
|
+
details.push(`refused: ${event3.method} ${event3.slug ?? "Composio call"}${event3.via ? ` via ${event3.via}` : ""} (${event3.reason ?? "REFUSED"})`);
|
|
232698
|
+
}
|
|
232699
|
+
const first2 = refused2[0];
|
|
232700
|
+
if (first2 || counts2.some(({ route: route15 }) => (route15?.refused ?? 0) > 0)) return { details, reason: {
|
|
232701
|
+
code: "refused",
|
|
232702
|
+
level: "fail",
|
|
232703
|
+
message: "The Composio route refused a call.",
|
|
232704
|
+
fix: first2?.issue?.message ?? "Use the tools declared in agent.routes through COMPOSIO_BASE_URL."
|
|
232705
|
+
} };
|
|
232706
|
+
const platformCalled = trace2.some((entry3) => entry3.routeEvent?.leg === "platform") || counts2.some(({ route: route15 }) => (route15?.executions ?? 0) > 0 || (route15?.sessions ?? 0) > 0);
|
|
232707
|
+
if (!platformCalled && /\b(?:backend|api)\.composio\.(?:dev|ai)\b/i.test(log3)) return { details, reason: {
|
|
232708
|
+
code: "outside_host",
|
|
232709
|
+
level: "fail",
|
|
232710
|
+
message: "The agent called Composio\u2019s own API host without reaching the Ancha gateway.",
|
|
232711
|
+
fix: "The Composio client pins its API URL. Configure the client to take its API URL from COMPOSIO_BASE_URL, then run ancha agent upload again."
|
|
232712
|
+
} };
|
|
232713
|
+
const missed = counts2.filter(({ route: route15 }) => (route15?.executions ?? 0) > (route15?.reached ?? 0));
|
|
232714
|
+
const issue9 = trace2.find((entry3) => entry3.issue?.code === "ROUTE_NOT_REACHED" || entry3.issue?.code === "ROUTE_ARGUMENTS")?.issue;
|
|
232715
|
+
if (missed.length) return { details, reason: {
|
|
232716
|
+
code: "unserved",
|
|
232717
|
+
level: "warning",
|
|
232718
|
+
message: `The check\u2019s Composio executions did not reach ${missed.map(({ tool }) => tool === "netsuite" ? "NetSuite" : "QuickBooks").join(" or ")}.`,
|
|
232719
|
+
fix: issue9?.code === "ROUTE_ARGUMENTS" ? "Send NETSUITE_RUN_SUITEQL_QUERY its SuiteQL query in q, with integer limit and offset when paging." : issue9?.message ?? "Run ancha agent upload again."
|
|
232720
|
+
} };
|
|
232721
|
+
if (counts2.some(({ route: route15 }) => !route15?.reached)) return { details };
|
|
232722
|
+
return { details, reason: { code: "answered", level: "pass", message: "The agent\u2019s Composio executions reached each routed world." } };
|
|
232723
|
+
}
|
|
232724
|
+
var ROUTE_KEY_REASON, routeKeyRejected, routeKey, unusableKeyReason;
|
|
232725
|
+
var init_connection_route = __esm({
|
|
232726
|
+
"backend/src/execution/connection-route.ts"() {
|
|
232727
|
+
"use strict";
|
|
232728
|
+
ROUTE_KEY_REASON = {
|
|
232729
|
+
code: "route_key",
|
|
232730
|
+
level: "fail",
|
|
232731
|
+
message: "Composio did not accept the workspace secret COMPOSIO_API_KEY: it answered 401 to the agent\u2019s call through the route.",
|
|
232732
|
+
fix: "Export the Composio project\u2019s API key as COMPOSIO_API_KEY, run ancha secrets set COMPOSIO_API_KEY, then run ancha agent upload again."
|
|
232733
|
+
};
|
|
232734
|
+
routeKeyRejected = (entries4) => entries4.some((entry3) => entry3.issue?.code === "ROUTE_KEY_REJECTED");
|
|
232735
|
+
routeKey = (value6) => value6?.trim() ?? "";
|
|
232736
|
+
unusableKeyReason = (what, next2 = "run ancha agent upload again") => ({
|
|
232737
|
+
code: "route_key",
|
|
232738
|
+
level: "fail",
|
|
232739
|
+
message: `The workspace secret COMPOSIO_API_KEY contains ${what}, which an API key cannot carry in a request header.`,
|
|
232740
|
+
fix: `Export the Composio project\u2019s API key as COMPOSIO_API_KEY without that character, run ancha secrets set COMPOSIO_API_KEY, then ${next2}.`
|
|
232741
|
+
});
|
|
232742
|
+
}
|
|
232743
|
+
});
|
|
232744
|
+
|
|
232663
232745
|
// backend/src/execution/ingress-client.ts
|
|
232664
232746
|
import { createHash as createHash61, randomBytes as randomBytes6 } from "node:crypto";
|
|
232665
232747
|
function ingressPath(path9, realm) {
|
|
@@ -232696,7 +232778,10 @@ async function startIngressClient(options5) {
|
|
|
232696
232778
|
for (const toolkit of ["quickbooks", "netsuite"]) if (toolkits[toolkit] && twins.includes(toolkit)) activeToolkits[toolkit] = "composio";
|
|
232697
232779
|
const qb = !!activeToolkits.quickbooks;
|
|
232698
232780
|
const connection4 = qb ? env2.services.quickbooks : { url: "http://127.0.0.1", token: "", accountId: "" };
|
|
232699
|
-
|
|
232781
|
+
const apiKey = routeKey(descriptor3.secretValues.COMPOSIO_API_KEY);
|
|
232782
|
+
if (!connection4 || qb && (!connection4.accountId || !connection4.token) || !apiKey) throw new AnchaError("CONFIG_INVALID", "Composio route credentials are required");
|
|
232783
|
+
const unusable = unusableRouteKey(apiKey);
|
|
232784
|
+
if (unusable) throw new AnchaError("CONFIG_INVALID", unusableKeyReason(unusable).message);
|
|
232700
232785
|
const local2 = new URL(connection4.url);
|
|
232701
232786
|
if (qb && (local2.protocol !== "http:" || local2.hostname !== "127.0.0.1" || local2.username || local2.password || local2.search || local2.hash || local2.pathname.replace(/\/$/, "") !== `/e/${env2.id}/quickbooks`)) throw new Error("QuickBooks route must target its local world");
|
|
232702
232787
|
const ns = activeToolkits.netsuite ? env2.services.netsuite : void 0;
|
|
@@ -232815,7 +232900,8 @@ async function startIngressClient(options5) {
|
|
|
232815
232900
|
const user9 = `ancha-${hash6(descriptor3.attemptId)}`;
|
|
232816
232901
|
const lifecycle2 = createComposioConnection({
|
|
232817
232902
|
...options5.composio,
|
|
232818
|
-
apiKey
|
|
232903
|
+
apiKey,
|
|
232904
|
+
savedApiKey: descriptor3.secretValues.COMPOSIO_API_KEY,
|
|
232819
232905
|
user: user9,
|
|
232820
232906
|
publicUrl,
|
|
232821
232907
|
token: connection4.token,
|
|
@@ -232841,14 +232927,14 @@ async function startIngressClient(options5) {
|
|
|
232841
232927
|
publicUrl,
|
|
232842
232928
|
token: connection4.token,
|
|
232843
232929
|
realm: connection4.accountId,
|
|
232844
|
-
apiKey
|
|
232930
|
+
apiKey,
|
|
232845
232931
|
attemptId: descriptor3.attemptId,
|
|
232846
232932
|
agentKey,
|
|
232847
232933
|
user: user9,
|
|
232848
232934
|
connection: () => !qb || closed || failed2 || signal.aborted || Date.now() >= expires ? Promise.reject(new ComposioConnectionError(503, "ROUTE_CLOSED")) : lifecycle2.connection(),
|
|
232849
232935
|
sessions: sessions2,
|
|
232850
232936
|
narrowed: (toolkits2) => {
|
|
232851
|
-
for (const toolkit of toolkits2) if (![agentKey,
|
|
232937
|
+
for (const toolkit of toolkits2) if (![agentKey, apiKey, connection4.token, ns?.token, publicUrl].some((secret) => secret && toolkit.includes(secret))) narrowed.add(toolkit);
|
|
232852
232938
|
},
|
|
232853
232939
|
signal: failure6.signal,
|
|
232854
232940
|
get failed() {
|
|
@@ -232924,6 +233010,7 @@ var init_ingress_client = __esm({
|
|
|
232924
233010
|
init_src();
|
|
232925
233011
|
init_composio();
|
|
232926
233012
|
init_composio_connection();
|
|
233013
|
+
init_connection_route();
|
|
232927
233014
|
headersSchema = external_exports.record(external_exports.string(), external_exports.string());
|
|
232928
233015
|
requestSchema = external_exports.strictObject({ rid: external_exports.string().min(1).max(256), method: external_exports.enum(["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"]), path: external_exports.string().max(8192), query: external_exports.string().max(8192), headers: headersSchema, body_b64: external_exports.string().max(87384) });
|
|
232929
233016
|
batchSchema = external_exports.strictObject({ requests: external_exports.array(requestSchema).max(8) });
|
|
@@ -232984,6 +233071,12 @@ async function runRoutedTrial(spec, run7) {
|
|
|
232984
233071
|
if (!spec.agent.routes?.quickbooks && !spec.agent.routes?.netsuite) return run7(spec);
|
|
232985
233072
|
const descriptor3 = attemptDescriptor();
|
|
232986
233073
|
const startedAt = performance.now();
|
|
233074
|
+
const unusable = unusableRouteKey(descriptor3.secretValues.COMPOSIO_API_KEY);
|
|
233075
|
+
if (unusable) {
|
|
233076
|
+
const { message: message8, fix } = unusableKeyReason(unusable, "run again");
|
|
233077
|
+
const result = await invalidTrial({ scene: spec.scene, trial: spec.trial, dir: spec.dir, startedAt, envId: spec.env.id, service: spec.agent.routes.quickbooks ? "quickbooks" : "netsuite", error: { code: "ROUTE_KEY_REJECTED", message: `${message8} ${fix}` } });
|
|
233078
|
+
return { ...result, harnessIssues: result.harnessIssues.map((issue9) => ({ ...issue9, code: "ROUTE_KEY_REJECTED" })) };
|
|
233079
|
+
}
|
|
232987
233080
|
let sessionId, captured;
|
|
232988
233081
|
const route15 = await startIngressClient({
|
|
232989
233082
|
routes: spec.agent.routes,
|
|
@@ -233048,6 +233141,7 @@ var init_routed_trial = __esm({
|
|
|
233048
233141
|
init_invalid_trial();
|
|
233049
233142
|
init_runtime3();
|
|
233050
233143
|
init_ingress_client();
|
|
233144
|
+
init_connection_route();
|
|
233051
233145
|
connections3 = /* @__PURE__ */ new WeakMap();
|
|
233052
233146
|
captures = /* @__PURE__ */ new WeakMap();
|
|
233053
233147
|
trialIngress = (env2) => connections3.get(env2);
|
|
@@ -243397,65 +243491,6 @@ var init_dependency_plan = __esm({
|
|
|
243397
243491
|
}
|
|
243398
243492
|
});
|
|
243399
243493
|
|
|
243400
|
-
// backend/src/execution/connection-route.ts
|
|
243401
|
-
function routedTools(routes2) {
|
|
243402
|
-
return ["quickbooks", "netsuite"].filter((tool) => routes2?.[tool] === "composio");
|
|
243403
|
-
}
|
|
243404
|
-
function routedProbeTask(routes2) {
|
|
243405
|
-
const systems = routedTools(routes2).map((tool) => tool === "netsuite" ? "NetSuite (NETSUITE_RUN_SUITEQL_QUERY)" : "QuickBooks (QUICKBOOKS_QUERY_ENTITIES)");
|
|
243406
|
-
return `Ancha connection check: read one customer in each of these systems and reply with their names: ${systems.join(", ")}. Change nothing.`;
|
|
243407
|
-
}
|
|
243408
|
-
function connectionRouteEvidence(routes2, summary, trace2, log3) {
|
|
243409
|
-
const tools2 = routedTools(routes2);
|
|
243410
|
-
if (!tools2.length) return { details: [] };
|
|
243411
|
-
const counts2 = tools2.map((tool) => ({ tool, route: summary?.[tool] }));
|
|
243412
|
-
const details = counts2.map(({ tool, route: route15 }) => `${tool === "netsuite" ? "NetSuite" : "QuickBooks"} via Composio: ${route15?.executions ?? 0} execution${route15?.executions === 1 ? "" : "s"}, ${route15?.reached ?? 0} reached the world, ${route15?.refused ?? 0} refused${route15?.via ? ` \xB7 via direct ${route15.via.direct} / session ${route15.via.session} / MCP ${route15.via.mcp}` : ""}`);
|
|
243413
|
-
if (routeKeyRejected(trace2)) return { details, reason: { ...ROUTE_KEY_REASON } };
|
|
243414
|
-
const refused2 = trace2.filter((entry3) => entry3.routeEvent?.leg === "platform" && entry3.routeEvent.op === "refused");
|
|
243415
|
-
for (const entry3 of refused2.slice(0, 5)) {
|
|
243416
|
-
const event3 = entry3.routeEvent;
|
|
243417
|
-
if (event3.leg !== "platform") continue;
|
|
243418
|
-
details.push(`refused: ${event3.method} ${event3.slug ?? "Composio call"}${event3.via ? ` via ${event3.via}` : ""} (${event3.reason ?? "REFUSED"})`);
|
|
243419
|
-
}
|
|
243420
|
-
const first2 = refused2[0];
|
|
243421
|
-
if (first2 || counts2.some(({ route: route15 }) => (route15?.refused ?? 0) > 0)) return { details, reason: {
|
|
243422
|
-
code: "refused",
|
|
243423
|
-
level: "fail",
|
|
243424
|
-
message: "The Composio route refused a call.",
|
|
243425
|
-
fix: first2?.issue?.message ?? "Use the tools declared in agent.routes through COMPOSIO_BASE_URL."
|
|
243426
|
-
} };
|
|
243427
|
-
const platformCalled = trace2.some((entry3) => entry3.routeEvent?.leg === "platform") || counts2.some(({ route: route15 }) => (route15?.executions ?? 0) > 0 || (route15?.sessions ?? 0) > 0);
|
|
243428
|
-
if (!platformCalled && /\b(?:backend|api)\.composio\.(?:dev|ai)\b/i.test(log3)) return { details, reason: {
|
|
243429
|
-
code: "outside_host",
|
|
243430
|
-
level: "fail",
|
|
243431
|
-
message: "The agent called Composio\u2019s own API host without reaching the Ancha gateway.",
|
|
243432
|
-
fix: "The Composio client pins its API URL. Configure the client to take its API URL from COMPOSIO_BASE_URL, then run ancha agent upload again."
|
|
243433
|
-
} };
|
|
243434
|
-
const missed = counts2.filter(({ route: route15 }) => (route15?.executions ?? 0) > (route15?.reached ?? 0));
|
|
243435
|
-
const issue9 = trace2.find((entry3) => entry3.issue?.code === "ROUTE_NOT_REACHED" || entry3.issue?.code === "ROUTE_ARGUMENTS")?.issue;
|
|
243436
|
-
if (missed.length) return { details, reason: {
|
|
243437
|
-
code: "unserved",
|
|
243438
|
-
level: "warning",
|
|
243439
|
-
message: `The check\u2019s Composio executions did not reach ${missed.map(({ tool }) => tool === "netsuite" ? "NetSuite" : "QuickBooks").join(" or ")}.`,
|
|
243440
|
-
fix: issue9?.code === "ROUTE_ARGUMENTS" ? "Send NETSUITE_RUN_SUITEQL_QUERY its SuiteQL query in q, with integer limit and offset when paging." : issue9?.message ?? "Run ancha agent upload again."
|
|
243441
|
-
} };
|
|
243442
|
-
if (counts2.some(({ route: route15 }) => !route15?.reached)) return { details };
|
|
243443
|
-
return { details, reason: { code: "answered", level: "pass", message: "The agent\u2019s Composio executions reached each routed world." } };
|
|
243444
|
-
}
|
|
243445
|
-
var ROUTE_KEY_REASON, routeKeyRejected;
|
|
243446
|
-
var init_connection_route = __esm({
|
|
243447
|
-
"backend/src/execution/connection-route.ts"() {
|
|
243448
|
-
"use strict";
|
|
243449
|
-
ROUTE_KEY_REASON = {
|
|
243450
|
-
code: "route_key",
|
|
243451
|
-
level: "fail",
|
|
243452
|
-
message: "Composio did not accept the workspace secret COMPOSIO_API_KEY: it answered 401 to the agent\u2019s call through the route.",
|
|
243453
|
-
fix: "Export the Composio project\u2019s API key as COMPOSIO_API_KEY, run ancha secrets set COMPOSIO_API_KEY, then run ancha agent upload again."
|
|
243454
|
-
};
|
|
243455
|
-
routeKeyRejected = (entries4) => entries4.some((entry3) => entry3.issue?.code === "ROUTE_KEY_REJECTED");
|
|
243456
|
-
}
|
|
243457
|
-
});
|
|
243458
|
-
|
|
243459
243494
|
// backend/src/execution/vm-connection.ts
|
|
243460
243495
|
var vm_connection_exports = {};
|
|
243461
243496
|
__export(vm_connection_exports, {
|
|
@@ -243530,6 +243565,11 @@ async function probeVmConnection(descriptor3, deps = {}) {
|
|
|
243530
243565
|
const config12 = deps.agent ? void 0 : await loadConfig(descriptor3.projectDir, descriptor3.configPath);
|
|
243531
243566
|
const agent = deps.agent ?? withLocalEnvironment(resolveRunAgent(config12, descriptor3.projectDir, { ...descriptor3.options.agentCommand !== null && { agent: descriptor3.options.agentCommand }, parallel: 1 }).agent, config12);
|
|
243532
243567
|
if (agent.mode === "endpoint") assertVmEndpoint(agent);
|
|
243568
|
+
const unusable = routedTools(agent.routes).length ? unusableRouteKey(descriptor3.secretValues.COMPOSIO_API_KEY) : void 0;
|
|
243569
|
+
if (unusable) {
|
|
243570
|
+
probe.reason = unusableKeyReason(unusable);
|
|
243571
|
+
return { state: "failed", probe, message: probe.reason.message };
|
|
243572
|
+
}
|
|
243533
243573
|
if (agent.slackTransport === "socket" || agent.mode === "endpoint" && agent.slackTransport === "http" && (!agent.readyUrl || config12?.agent?.slackTransport === "http" || deps.agent?.slackTransport === "http")) {
|
|
243534
243574
|
return await probeSlackApp(descriptor3, agent, probe, deps);
|
|
243535
243575
|
}
|
|
@@ -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
|
-
|
|
187636
|
-
|
|
187637
|
-
|
|
187638
|
-
|
|
187639
|
-
|
|
187640
|
-
|
|
187641
|
-
|
|
187642
|
-
|
|
187643
|
-
|
|
187644
|
-
|
|
187645
|
-
|
|
187646
|
-
|
|
187647
|
-
|
|
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
|
-
|
|
187651
|
-
|
|
187652
|
-
|
|
187653
|
-
|
|
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,
|
|
187656
|
-
const
|
|
187657
|
-
if (
|
|
187658
|
-
|
|
187659
|
-
|
|
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
|
|
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
|
-
|
|
187666
|
-
reply6.once("close", tell);
|
|
187713
|
+
return;
|
|
187667
187714
|
}
|
|
187668
|
-
|
|
187669
|
-
|
|
187670
|
-
|
|
187671
|
-
|
|
187672
|
-
|
|
187673
|
-
|
|
187674
|
-
|
|
187675
|
-
|
|
187676
|
-
|
|
187677
|
-
|
|
187678
|
-
|
|
187679
|
-
|
|
187680
|
-
|
|
187681
|
-
|
|
187682
|
-
|
|
187683
|
-
|
|
187684
|
-
|
|
187685
|
-
|
|
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
|
-
|
|
187690
|
-
if (
|
|
187691
|
-
|
|
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.
|
|
187694
|
-
|
|
187695
|
-
reply6.once("end", () => {
|
|
187696
|
-
if (failed2) return;
|
|
187744
|
+
reply6.on("end", () => {
|
|
187745
|
+
if (size2 > BODY_LIMIT) return;
|
|
187697
187746
|
try {
|
|
187698
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
187738
|
-
|
|
187739
|
-
|
|
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", () => {
|
package/lib/worker.mjs
CHANGED
|
@@ -71724,135 +71724,145 @@ async function startIsolationGateway(options5) {
|
|
|
71724
71724
|
headers.host = modelHost ?? new URL(exposedOrigin ?? upstream.origin).host;
|
|
71725
71725
|
delete headers["x-ancha-client-origin"];
|
|
71726
71726
|
if (transform3) headers["accept-encoding"] = "identity";
|
|
71727
|
-
|
|
71728
|
-
|
|
71729
|
-
|
|
71730
|
-
|
|
71731
|
-
|
|
71732
|
-
|
|
71733
|
-
|
|
71734
|
-
|
|
71735
|
-
|
|
71736
|
-
|
|
71737
|
-
|
|
71738
|
-
|
|
71739
|
-
|
|
71727
|
+
let target5;
|
|
71728
|
+
try {
|
|
71729
|
+
target5 = (modelHost ? secureRequest : request)({
|
|
71730
|
+
hostname: modelAddress?.address ?? upstream.hostname.replace(/^\[|\]$/g, ""),
|
|
71731
|
+
port: modelHost ? 443 : upstream.port || 80,
|
|
71732
|
+
path: targetPath,
|
|
71733
|
+
method: req.method,
|
|
71734
|
+
headers,
|
|
71735
|
+
agent: false,
|
|
71736
|
+
...modelHost && { servername: modelHost, rejectUnauthorized: true, family: modelAddress.family }
|
|
71737
|
+
}, (reply6) => {
|
|
71738
|
+
if (!reply6.statusCode || reply6.statusCode < 100) {
|
|
71739
|
+
reply6.destroy();
|
|
71740
|
+
reject2(res, 502, "Tool connection failed.");
|
|
71741
|
+
return;
|
|
71740
71742
|
}
|
|
71741
|
-
|
|
71742
|
-
|
|
71743
|
-
|
|
71744
|
-
|
|
71745
|
-
|
|
71743
|
+
const observe2 = (host, status5, message8) => {
|
|
71744
|
+
try {
|
|
71745
|
+
options5.onModelReply?.(host, status5, message8);
|
|
71746
|
+
} catch {
|
|
71747
|
+
}
|
|
71748
|
+
};
|
|
71749
|
+
if (!toolCall && modelHost !== void 0 && reply6.statusCode !== void 0) {
|
|
71750
|
+
const host = modelHost, status5 = reply6.statusCode;
|
|
71751
|
+
if (!MODEL_REFUSALS.has(status5)) observe2(host, status5);
|
|
71752
|
+
else {
|
|
71753
|
+
const chunks2 = [];
|
|
71754
|
+
let size3 = 0, told = false;
|
|
71755
|
+
const tell = () => {
|
|
71756
|
+
if (told) return;
|
|
71757
|
+
told = true;
|
|
71758
|
+
observe2(host, status5, size3 <= REFUSAL_BYTES ? refusalMessage(Buffer.concat(chunks2), reply6.headers["content-encoding"]?.toLowerCase()) : void 0);
|
|
71759
|
+
};
|
|
71760
|
+
reply6.on("data", (chunk) => {
|
|
71761
|
+
size3 += chunk.length;
|
|
71762
|
+
if (size3 <= REFUSAL_BYTES) chunks2.push(chunk);
|
|
71763
|
+
});
|
|
71764
|
+
reply6.once("end", tell);
|
|
71765
|
+
reply6.once("close", tell);
|
|
71766
|
+
}
|
|
71767
|
+
}
|
|
71768
|
+
if (reply6.statusCode && reply6.statusCode >= 300 && reply6.statusCode < 400) {
|
|
71769
|
+
reply6.destroy();
|
|
71770
|
+
reject2(res, 502, "Tool redirects are not allowed through the sandbox gateway.");
|
|
71771
|
+
return;
|
|
71772
|
+
}
|
|
71773
|
+
const responseHeaders = toolCall ? { "content-type": reply6.headers["content-type"] ?? "application/json" } : cleanHeaders(reply6.headers);
|
|
71774
|
+
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"];
|
|
71775
|
+
const successful2 = reply6.statusCode !== void 0 && reply6.statusCode >= 200 && reply6.statusCode < 300;
|
|
71776
|
+
if (toolCall?.response === "session-delete" && successful2) route15.sessions.delete(toolCall.sessionId);
|
|
71777
|
+
if (toolCall && successful2 && (toolCall.response === "session-create" || toolCall.response === "session-get")) {
|
|
71746
71778
|
const chunks2 = [];
|
|
71747
|
-
let size3 = 0,
|
|
71748
|
-
const
|
|
71749
|
-
if (
|
|
71750
|
-
|
|
71751
|
-
|
|
71779
|
+
let size3 = 0, failed2 = false;
|
|
71780
|
+
const fail6 = () => {
|
|
71781
|
+
if (failed2) return;
|
|
71782
|
+
failed2 = true;
|
|
71783
|
+
reply6.destroy();
|
|
71784
|
+
if (!res.headersSent) reject2(res, 502, "Composio returned an invalid session response.");
|
|
71785
|
+
else res.destroy();
|
|
71752
71786
|
};
|
|
71753
71787
|
reply6.on("data", (chunk) => {
|
|
71754
71788
|
size3 += chunk.length;
|
|
71755
|
-
if (size3
|
|
71789
|
+
if (size3 > 1024 * 1024) fail6();
|
|
71790
|
+
else if (!failed2) chunks2.push(Buffer.from(chunk));
|
|
71791
|
+
});
|
|
71792
|
+
reply6.once("error", fail6);
|
|
71793
|
+
reply6.once("aborted", fail6);
|
|
71794
|
+
reply6.once("end", () => {
|
|
71795
|
+
if (failed2) return;
|
|
71796
|
+
try {
|
|
71797
|
+
const value5 = rewriteComposioSession(JSON.parse(Buffer.concat(chunks2).toString("utf8")), toolCall, route15, exposedOrigin);
|
|
71798
|
+
const body7 = JSON.stringify(value5);
|
|
71799
|
+
res.writeHead(reply6.statusCode, { "content-type": "application/json", "content-length": Buffer.byteLength(body7) });
|
|
71800
|
+
res.end(body7);
|
|
71801
|
+
} catch {
|
|
71802
|
+
fail6();
|
|
71803
|
+
}
|
|
71756
71804
|
});
|
|
71757
|
-
|
|
71758
|
-
reply6.once("close", tell);
|
|
71805
|
+
return;
|
|
71759
71806
|
}
|
|
71760
|
-
|
|
71761
|
-
|
|
71762
|
-
|
|
71763
|
-
|
|
71764
|
-
|
|
71765
|
-
|
|
71766
|
-
|
|
71767
|
-
|
|
71768
|
-
|
|
71769
|
-
|
|
71770
|
-
|
|
71771
|
-
|
|
71772
|
-
|
|
71773
|
-
|
|
71774
|
-
|
|
71775
|
-
|
|
71776
|
-
|
|
71777
|
-
|
|
71778
|
-
else res.destroy();
|
|
71779
|
-
};
|
|
71807
|
+
if (toolCall) {
|
|
71808
|
+
let received = 0;
|
|
71809
|
+
reply6.on("data", (chunk) => {
|
|
71810
|
+
received += chunk.length;
|
|
71811
|
+
if (received > BODY_LIMIT) {
|
|
71812
|
+
reply6.destroy();
|
|
71813
|
+
res.destroy();
|
|
71814
|
+
}
|
|
71815
|
+
});
|
|
71816
|
+
}
|
|
71817
|
+
if (!transform3) {
|
|
71818
|
+
res.writeHead(reply6.statusCode ?? 502, responseHeaders);
|
|
71819
|
+
reply6.pipe(res);
|
|
71820
|
+
reply6.on("error", () => res.destroy());
|
|
71821
|
+
return;
|
|
71822
|
+
}
|
|
71823
|
+
const chunks = [];
|
|
71824
|
+
let size2 = 0;
|
|
71780
71825
|
reply6.on("data", (chunk) => {
|
|
71781
|
-
|
|
71782
|
-
if (
|
|
71783
|
-
|
|
71784
|
-
|
|
71785
|
-
|
|
71786
|
-
|
|
71787
|
-
reply6.
|
|
71788
|
-
if (
|
|
71826
|
+
size2 += chunk.length;
|
|
71827
|
+
if (size2 > BODY_LIMIT) {
|
|
71828
|
+
reply6.destroy();
|
|
71829
|
+
reject2(res, 502, "Tool response exceeded the sandbox gateway limit.");
|
|
71830
|
+
} else chunks.push(chunk);
|
|
71831
|
+
});
|
|
71832
|
+
reply6.on("error", () => {
|
|
71833
|
+
if (!res.headersSent) reject2(res, 502, "Tool connection failed.");
|
|
71834
|
+
else res.destroy();
|
|
71835
|
+
});
|
|
71836
|
+
reply6.on("end", () => {
|
|
71837
|
+
if (size2 > BODY_LIMIT) return;
|
|
71789
71838
|
try {
|
|
71790
|
-
|
|
71839
|
+
let value5 = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
71840
|
+
if (transform3 === "context" && reply6.statusCode === 200) {
|
|
71841
|
+
const connection4 = mapped(exposedOrigin ?? "");
|
|
71842
|
+
value5 = { ...value5, env: connection4.env, session: connection4.session };
|
|
71843
|
+
}
|
|
71844
|
+
if (transform3 === "socket" && reply6.statusCode === 200 && value5?.ok === true) {
|
|
71845
|
+
const grant = new URL(value5.url);
|
|
71846
|
+
const expected = loopbackOrigin(session2.eventsUrl);
|
|
71847
|
+
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");
|
|
71848
|
+
for (const [path9, old] of socketGrants) if (old.expires <= Date.now()) socketGrants.delete(path9);
|
|
71849
|
+
if (socketGrants.size >= 128) throw new Error("Too many socket grants");
|
|
71850
|
+
socketGrants.set(grant.pathname, { url: grant, expires: Date.now() + 3e4 });
|
|
71851
|
+
value5 = { ...value5, url: (exposedOrigin ?? "").replace(/^http:/, "ws:") + grant.pathname };
|
|
71852
|
+
}
|
|
71791
71853
|
const body7 = JSON.stringify(value5);
|
|
71792
|
-
|
|
71854
|
+
delete responseHeaders["content-encoding"];
|
|
71855
|
+
delete responseHeaders["content-length"];
|
|
71856
|
+
res.writeHead(reply6.statusCode ?? 502, { ...responseHeaders, "content-length": Buffer.byteLength(body7) });
|
|
71793
71857
|
res.end(body7);
|
|
71794
71858
|
} catch {
|
|
71795
|
-
|
|
71859
|
+
reject2(res, 502, "Tool context was not a valid bounded response.");
|
|
71796
71860
|
}
|
|
71797
71861
|
});
|
|
71798
|
-
return;
|
|
71799
|
-
}
|
|
71800
|
-
if (toolCall) {
|
|
71801
|
-
let received = 0;
|
|
71802
|
-
reply6.on("data", (chunk) => {
|
|
71803
|
-
received += chunk.length;
|
|
71804
|
-
if (received > BODY_LIMIT) {
|
|
71805
|
-
reply6.destroy();
|
|
71806
|
-
res.destroy();
|
|
71807
|
-
}
|
|
71808
|
-
});
|
|
71809
|
-
}
|
|
71810
|
-
if (!transform3) {
|
|
71811
|
-
res.writeHead(reply6.statusCode ?? 502, responseHeaders);
|
|
71812
|
-
reply6.pipe(res);
|
|
71813
|
-
reply6.on("error", () => res.destroy());
|
|
71814
|
-
return;
|
|
71815
|
-
}
|
|
71816
|
-
const chunks = [];
|
|
71817
|
-
let size2 = 0;
|
|
71818
|
-
reply6.on("data", (chunk) => {
|
|
71819
|
-
size2 += chunk.length;
|
|
71820
|
-
if (size2 > BODY_LIMIT) {
|
|
71821
|
-
reply6.destroy();
|
|
71822
|
-
reject2(res, 502, "Tool response exceeded the sandbox gateway limit.");
|
|
71823
|
-
} else chunks.push(chunk);
|
|
71824
|
-
});
|
|
71825
|
-
reply6.on("error", () => {
|
|
71826
|
-
if (!res.headersSent) reject2(res, 502, "Tool connection failed.");
|
|
71827
|
-
else res.destroy();
|
|
71828
|
-
});
|
|
71829
|
-
reply6.on("end", () => {
|
|
71830
|
-
if (size2 > BODY_LIMIT) return;
|
|
71831
|
-
try {
|
|
71832
|
-
let value5 = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
71833
|
-
if (transform3 === "context" && reply6.statusCode === 200) {
|
|
71834
|
-
const connection4 = mapped(exposedOrigin ?? "");
|
|
71835
|
-
value5 = { ...value5, env: connection4.env, session: connection4.session };
|
|
71836
|
-
}
|
|
71837
|
-
if (transform3 === "socket" && reply6.statusCode === 200 && value5?.ok === true) {
|
|
71838
|
-
const grant = new URL(value5.url);
|
|
71839
|
-
const expected = loopbackOrigin(session2.eventsUrl);
|
|
71840
|
-
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");
|
|
71841
|
-
for (const [path9, old] of socketGrants) if (old.expires <= Date.now()) socketGrants.delete(path9);
|
|
71842
|
-
if (socketGrants.size >= 128) throw new Error("Too many socket grants");
|
|
71843
|
-
socketGrants.set(grant.pathname, { url: grant, expires: Date.now() + 3e4 });
|
|
71844
|
-
value5 = { ...value5, url: (exposedOrigin ?? "").replace(/^http:/, "ws:") + grant.pathname };
|
|
71845
|
-
}
|
|
71846
|
-
const body7 = JSON.stringify(value5);
|
|
71847
|
-
delete responseHeaders["content-encoding"];
|
|
71848
|
-
delete responseHeaders["content-length"];
|
|
71849
|
-
res.writeHead(reply6.statusCode ?? 502, { ...responseHeaders, "content-length": Buffer.byteLength(body7) });
|
|
71850
|
-
res.end(body7);
|
|
71851
|
-
} catch {
|
|
71852
|
-
reject2(res, 502, "Tool context was not a valid bounded response.");
|
|
71853
|
-
}
|
|
71854
71862
|
});
|
|
71855
|
-
}
|
|
71863
|
+
} catch {
|
|
71864
|
+
return reject2(res, 502, "Tool connection failed.");
|
|
71865
|
+
}
|
|
71856
71866
|
upstreamRequests.add(target5);
|
|
71857
71867
|
target5.once("close", () => upstreamRequests.delete(target5));
|
|
71858
71868
|
target5.once("error", () => {
|
|
@@ -72172,7 +72182,8 @@ function createComposioConnection(options5) {
|
|
|
72172
72182
|
signal.removeEventListener("abort", abort);
|
|
72173
72183
|
}
|
|
72174
72184
|
}
|
|
72175
|
-
const tag3 = (expiry, lease) => createHmac("sha256",
|
|
72185
|
+
const tag3 = (expiry, lease, key5 = options5.apiKey) => createHmac("sha256", key5).update(`ancha-route:${expiry}:${lease}`).digest("hex").slice(0, 16);
|
|
72186
|
+
const signers = [options5.apiKey, ...options5.savedApiKey && options5.savedApiKey !== options5.apiKey ? [options5.savedApiKey] : []];
|
|
72176
72187
|
async function sweep() {
|
|
72177
72188
|
const signal = AbortSignal.timeout(5e3);
|
|
72178
72189
|
let cursor4, deletions = 0;
|
|
@@ -72190,7 +72201,7 @@ function createComposioConnection(options5) {
|
|
|
72190
72201
|
if (!config12.success || visited.has(config12.data.id)) continue;
|
|
72191
72202
|
visited.add(config12.data.id);
|
|
72192
72203
|
const match3 = /^ancha-route-(\d{10})-([a-z0-9]{12})-([a-f0-9]{16})$/.exec(config12.data.name);
|
|
72193
|
-
if (!match3 || Number(match3[1]) * 1e3 >= Date.now() || !timingSafeEqual2(Buffer.from(match3[3]), Buffer.from(tag3(match3[1], match3[2])))) continue;
|
|
72204
|
+
if (!match3 || Number(match3[1]) * 1e3 >= Date.now() || !signers.some((key5) => timingSafeEqual2(Buffer.from(match3[3]), Buffer.from(tag3(match3[1], match3[2], key5))))) continue;
|
|
72194
72205
|
const accounts2 = await call2(`/connected_accounts?${new URLSearchParams({ auth_config_ids: config12.data.id, limit: "50" })}`, "GET", void 0, signal);
|
|
72195
72206
|
if (accounts2.status < 200 || accounts2.status >= 300) continue;
|
|
72196
72207
|
const listed = pageSchema.safeParse(accounts2.data);
|
|
@@ -72342,6 +72353,26 @@ var init_composio_connection = __esm({
|
|
|
72342
72353
|
}
|
|
72343
72354
|
});
|
|
72344
72355
|
|
|
72356
|
+
// backend/src/execution/connection-route.ts
|
|
72357
|
+
function unusableRouteKey(value5) {
|
|
72358
|
+
const key5 = routeKey(value5);
|
|
72359
|
+
if (!key5 || /^[\x21-\x7e]+$/.test(key5)) return void 0;
|
|
72360
|
+
return /[\r\n]/.test(key5) ? "a line break" : /[ \t]/.test(key5) ? "a space or tab" : /[^\x00-\x7f]/.test(key5) ? "a character that is not plain ASCII, such as a curly quote" : "a control character";
|
|
72361
|
+
}
|
|
72362
|
+
var routeKey, unusableKeyReason;
|
|
72363
|
+
var init_connection_route = __esm({
|
|
72364
|
+
"backend/src/execution/connection-route.ts"() {
|
|
72365
|
+
"use strict";
|
|
72366
|
+
routeKey = (value5) => value5?.trim() ?? "";
|
|
72367
|
+
unusableKeyReason = (what, next2 = "run ancha agent upload again") => ({
|
|
72368
|
+
code: "route_key",
|
|
72369
|
+
level: "fail",
|
|
72370
|
+
message: `The workspace secret COMPOSIO_API_KEY contains ${what}, which an API key cannot carry in a request header.`,
|
|
72371
|
+
fix: `Export the Composio project\u2019s API key as COMPOSIO_API_KEY without that character, run ancha secrets set COMPOSIO_API_KEY, then ${next2}.`
|
|
72372
|
+
});
|
|
72373
|
+
}
|
|
72374
|
+
});
|
|
72375
|
+
|
|
72345
72376
|
// backend/src/execution/ingress-client.ts
|
|
72346
72377
|
import { createHash as createHash5, randomBytes as randomBytes2 } from "node:crypto";
|
|
72347
72378
|
function ingressPath(path8, realm) {
|
|
@@ -72378,7 +72409,10 @@ async function startIngressClient(options5) {
|
|
|
72378
72409
|
for (const toolkit of ["quickbooks", "netsuite"]) if (toolkits[toolkit] && twins.includes(toolkit)) activeToolkits[toolkit] = "composio";
|
|
72379
72410
|
const qb = !!activeToolkits.quickbooks;
|
|
72380
72411
|
const connection4 = qb ? env.services.quickbooks : { url: "http://127.0.0.1", token: "", accountId: "" };
|
|
72381
|
-
|
|
72412
|
+
const apiKey = routeKey(descriptor2.secretValues.COMPOSIO_API_KEY);
|
|
72413
|
+
if (!connection4 || qb && (!connection4.accountId || !connection4.token) || !apiKey) throw new AnchaError("CONFIG_INVALID", "Composio route credentials are required");
|
|
72414
|
+
const unusable = unusableRouteKey(apiKey);
|
|
72415
|
+
if (unusable) throw new AnchaError("CONFIG_INVALID", unusableKeyReason(unusable).message);
|
|
72382
72416
|
const local2 = new URL(connection4.url);
|
|
72383
72417
|
if (qb && (local2.protocol !== "http:" || local2.hostname !== "127.0.0.1" || local2.username || local2.password || local2.search || local2.hash || local2.pathname.replace(/\/$/, "") !== `/e/${env.id}/quickbooks`)) throw new Error("QuickBooks route must target its local world");
|
|
72384
72418
|
const ns = activeToolkits.netsuite ? env.services.netsuite : void 0;
|
|
@@ -72497,7 +72531,8 @@ async function startIngressClient(options5) {
|
|
|
72497
72531
|
const user9 = `ancha-${hash2(descriptor2.attemptId)}`;
|
|
72498
72532
|
const lifecycle2 = createComposioConnection({
|
|
72499
72533
|
...options5.composio,
|
|
72500
|
-
apiKey
|
|
72534
|
+
apiKey,
|
|
72535
|
+
savedApiKey: descriptor2.secretValues.COMPOSIO_API_KEY,
|
|
72501
72536
|
user: user9,
|
|
72502
72537
|
publicUrl,
|
|
72503
72538
|
token: connection4.token,
|
|
@@ -72523,14 +72558,14 @@ async function startIngressClient(options5) {
|
|
|
72523
72558
|
publicUrl,
|
|
72524
72559
|
token: connection4.token,
|
|
72525
72560
|
realm: connection4.accountId,
|
|
72526
|
-
apiKey
|
|
72561
|
+
apiKey,
|
|
72527
72562
|
attemptId: descriptor2.attemptId,
|
|
72528
72563
|
agentKey,
|
|
72529
72564
|
user: user9,
|
|
72530
72565
|
connection: () => !qb || closed || failed2 || signal.aborted || Date.now() >= expires ? Promise.reject(new ComposioConnectionError(503, "ROUTE_CLOSED")) : lifecycle2.connection(),
|
|
72531
72566
|
sessions: sessions2,
|
|
72532
72567
|
narrowed: (toolkits2) => {
|
|
72533
|
-
for (const toolkit of toolkits2) if (![agentKey,
|
|
72568
|
+
for (const toolkit of toolkits2) if (![agentKey, apiKey, connection4.token, ns?.token, publicUrl].some((secret) => secret && toolkit.includes(secret))) narrowed.add(toolkit);
|
|
72534
72569
|
},
|
|
72535
72570
|
signal: failure5.signal,
|
|
72536
72571
|
get failed() {
|
|
@@ -72606,6 +72641,7 @@ var init_ingress_client = __esm({
|
|
|
72606
72641
|
init_src();
|
|
72607
72642
|
init_composio();
|
|
72608
72643
|
init_composio_connection();
|
|
72644
|
+
init_connection_route();
|
|
72609
72645
|
headersSchema = external_exports.record(external_exports.string(), external_exports.string());
|
|
72610
72646
|
requestSchema = external_exports.strictObject({ rid: external_exports.string().min(1).max(256), method: external_exports.enum(["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"]), path: external_exports.string().max(8192), query: external_exports.string().max(8192), headers: headersSchema, body_b64: external_exports.string().max(87384) });
|
|
72611
72647
|
batchSchema = external_exports.strictObject({ requests: external_exports.array(requestSchema).max(8) });
|
|
@@ -72666,6 +72702,12 @@ async function runRoutedTrial(spec, run5) {
|
|
|
72666
72702
|
if (!spec.agent.routes?.quickbooks && !spec.agent.routes?.netsuite) return run5(spec);
|
|
72667
72703
|
const descriptor2 = attemptDescriptor();
|
|
72668
72704
|
const startedAt = performance.now();
|
|
72705
|
+
const unusable = unusableRouteKey(descriptor2.secretValues.COMPOSIO_API_KEY);
|
|
72706
|
+
if (unusable) {
|
|
72707
|
+
const { message: message8, fix } = unusableKeyReason(unusable, "run again");
|
|
72708
|
+
const result = await invalidTrial({ scene: spec.scene, trial: spec.trial, dir: spec.dir, startedAt, envId: spec.env.id, service: spec.agent.routes.quickbooks ? "quickbooks" : "netsuite", error: { code: "ROUTE_KEY_REJECTED", message: `${message8} ${fix}` } });
|
|
72709
|
+
return { ...result, harnessIssues: result.harnessIssues.map((issue9) => ({ ...issue9, code: "ROUTE_KEY_REJECTED" })) };
|
|
72710
|
+
}
|
|
72669
72711
|
let sessionId, captured;
|
|
72670
72712
|
const route15 = await startIngressClient({
|
|
72671
72713
|
routes: spec.agent.routes,
|
|
@@ -72730,6 +72772,7 @@ var init_routed_trial = __esm({
|
|
|
72730
72772
|
init_invalid_trial();
|
|
72731
72773
|
init_runtime();
|
|
72732
72774
|
init_ingress_client();
|
|
72775
|
+
init_connection_route();
|
|
72733
72776
|
connections2 = /* @__PURE__ */ new WeakMap();
|
|
72734
72777
|
captures = /* @__PURE__ */ new WeakMap();
|
|
72735
72778
|
trialIngress = (env) => connections2.get(env);
|
|
@@ -128820,10 +128863,10 @@ var init_connect_registry = __esm({
|
|
|
128820
128863
|
});
|
|
128821
128864
|
|
|
128822
128865
|
// packages/server/src/twins/stripe/connect.ts
|
|
128823
|
-
function requestAccount(ctx, header4,
|
|
128866
|
+
function requestAccount(ctx, header4, routeKey2) {
|
|
128824
128867
|
if (header4 === void 0) return null;
|
|
128825
128868
|
const id15 = header4.trim();
|
|
128826
|
-
if (!ACCOUNT_SCOPED.has(
|
|
128869
|
+
if (!ACCOUNT_SCOPED.has(routeKey2)) unsupported8(`the Stripe-Account header on ${routeKey2}`);
|
|
128827
128870
|
const account9 = load2(ctx, id15);
|
|
128828
128871
|
if (!account9 || account9.object !== "account" || account9.deleted) {
|
|
128829
128872
|
throw new StripeError(403, "invalid_request_error", `The provided key does not have access to account '${id15}' (or that account does not exist). Application access may have been revoked.`, { code: "account_invalid" });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ancha-labs/cli",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.46",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Simulation playgrounds for agents: run your agent against stateful replicas of Slack, GitHub, Datadog and more, each attempt on a fresh cloud machine in your app.ancha.io workspace, or on your computer.",
|
|
6
6
|
"bugs": {
|