Skip to content

Commit 191b86c

Browse files
committed
fix: route managed tools to owning Commons environment
1 parent 9ef2c00 commit 191b86c

5 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎apps/api/src/routes/agentRuntime.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ async function proxyAgentCommonsTools(
4747
body: { error: "Agent Commons service authorization is unavailable" },
4848
};
4949
}
50+
const targetBaseUrl =
51+
agent.commons.apiUrl?.replace(/\/$/, "") || AGC_BASE_URL;
5052
const response = await fetch(
51-
`${AGC_BASE_URL}/v1/runtime/agents/${encodeURIComponent(
53+
`${targetBaseUrl}/v1/runtime/agents/${encodeURIComponent(
5254
commons.agentId
5355
)}/tools${suffix}`,
5456
{

‎apps/api/src/routes/computers.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const AGC_URL = (
4141

4242
type ComputerRequestBody = {
4343
agentCommonsId?: string;
44+
agentCommonsApiUrl?: string | null;
4445
ownerUserId?: string | null;
4546
workspaceId?: string | null;
4647
computerId?: string;
@@ -65,6 +66,21 @@ type ComputerRequestBody = {
6566
};
6667
};
6768

69+
function agentCommonsApiUrl(c: any, value: string | null | undefined): string {
70+
if (c.get("authType") !== "service" || !value?.trim()) return AGC_URL;
71+
const url = new URL(value.trim());
72+
if (
73+
url.protocol !== "https:" ||
74+
url.username ||
75+
url.password ||
76+
url.search ||
77+
url.hash
78+
) {
79+
throw new Error("agentCommonsApiUrl must be an HTTPS API origin");
80+
}
81+
return url.origin;
82+
}
83+
6884
function runtimeRequest(body: ComputerRequestBody) {
6985
const integrationPath = body.integrationPath ?? "native";
7086
if (!["native", "openclaw", "hermes", "guest"].includes(integrationPath)) {
@@ -433,6 +449,7 @@ router.post("/", async (c) => {
433449
if ("response" in verified) return verified.response;
434450

435451
try {
452+
const commonsApiUrl = agentCommonsApiUrl(c, body.agentCommonsApiUrl);
436453
const runtime = runtimeRequest(body);
437454
const tenant = await ensureCanonicalComputerTenant(verified.owner);
438455
const fleet = await ensureTenantComputerFleet({
@@ -462,12 +479,14 @@ router.post("/", async (c) => {
462479
fleetId: fleet._id,
463480
"compute.ownerUserId": verified.owner.ownerUserId,
464481
"compute.workspaceId": verified.owner.workspaceId,
482+
"commons.apiUrl": commonsApiUrl,
465483
updatedAt: new Date(),
466484
},
467485
}
468486
);
469487
existing = {
470488
...existing,
489+
commons: { ...existing.commons, apiUrl: commonsApiUrl },
471490
tenantId: tenant._id,
472491
fleetId: fleet._id,
473492
compute: existing.compute
@@ -567,6 +586,7 @@ router.post("/", async (c) => {
567586
"config.nativeConfig": updated.config.nativeConfig,
568587
"config.openclawConfig": updated.config.openclawConfig,
569588
"config.hermesConfig": updated.config.hermesConfig,
589+
"commons.apiUrl": commonsApiUrl,
570590
resourceProfile: updated.resourceProfile,
571591
resourceMode: updated.resourceMode,
572592
resourceSpec: updated.resourceSpec,
@@ -621,6 +641,7 @@ router.post("/", async (c) => {
621641
userId: verified.owner.ownerUserId,
622642
workspaceId: verified.owner.workspaceId ?? undefined,
623643
existingCommonsAgentId: agentCommonsId,
644+
agentCommonsApiUrl: commonsApiUrl,
624645
fleet,
625646
role,
626647
systemPrompt:

‎apps/api/src/services/agentCommonsIdentity.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export function normalizeCommonsIdentity(
2424

2525
return {
2626
agentId: runtimeId,
27+
apiUrl: commons.apiUrl ?? null,
2728
apiKey: null,
2829
walletAddress: commons.walletAddress ?? null,
2930
registryAgentId,
@@ -38,6 +39,7 @@ export async function persistNormalizedCommonsIdentity(agent: AgentDoc): Promise
3839
{
3940
$set: {
4041
"commons.agentId": commons.agentId,
42+
"commons.apiUrl": commons.apiUrl ?? null,
4143
"commons.apiKey": null,
4244
"commons.walletAddress": commons.walletAddress,
4345
"commons.registryAgentId": commons.registryAgentId ?? null,

‎apps/api/src/services/provisioner.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export interface ProvisionAgentOptions {
7070
userId?: string;
7171
workspaceId?: string;
7272
existingCommonsAgentId?: string;
73+
agentCommonsApiUrl?: string | null;
7374
fleet: FleetDoc;
7475
role: string;
7576
systemPrompt: string;
@@ -136,6 +137,7 @@ export async function provisionAgent(
136137
const commons = opts.existingCommonsAgentId
137138
? {
138139
agentId: opts.existingCommonsAgentId,
140+
apiUrl: opts.agentCommonsApiUrl ?? AGC_BASE_URL,
139141
ownerUserId: opts.userId ?? null,
140142
workspaceId: opts.workspaceId ?? null,
141143
apiKey: null,
@@ -145,6 +147,7 @@ export async function provisionAgent(
145147
: opts.integrationPath === "openclaw" || opts.integrationPath === "hermes"
146148
? {
147149
agentId: null,
150+
apiUrl: null,
148151
apiKey: null,
149152
walletAddress: null,
150153
registryAgentId: null,

‎apps/api/src/types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ export interface AgentDoc {
107107
tenantId: string;
108108
commons: {
109109
agentId: string | null;
110+
/** Agent Commons API origin that owns this identity (staging or production). */
111+
apiUrl?: string | null;
110112
ownerUserId?: string | null;
111113
workspaceId?: string | null;
112114
apiKey: string | null;

0 commit comments

Comments
 (0)