Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Return canonical web URLs for spawned conversations (#1179)

Sina Matian6912d9bc3b90
diff --git a/src/api/agent-api-catalog.ts b/src/api/agent-api-catalog.ts
index 779b163d4..6e9dcc3ae 100644
--- a/src/api/agent-api-catalog.ts
+++ b/src/api/agent-api-catalog.ts
@@ -354,7 +354,7 @@ const FAMILIES: AgentApiFamily[] = [
(m === "GET" && (p === "/v1/conversations" || /^\/v1\/conversations\/[^/]+$/.test(p))) ||
(m === "POST" && (p === "/v1/conversations" || /^\/v1\/conversations\/[^/]+(?:\/fork)?$/.test(p))),
guidance:
- "These act on the ASKING PERSON's own conversation list (the web UI sidebar) — archiving, pinning, or renaming is a per-person view change, never a deletion, and never touches anyone else's list. Confirm before bulk-archiving.",
+ "These act on the ASKING PERSON's own conversation list (the web UI sidebar) — archiving, pinning, or renaming is a per-person view change, never a deletion, and never touches anyone else's list. Confirm before bulk-archiving. When handing off a newly started conversation, share the exact webUrl returned by POST /v1/conversations; never guess or reconstruct its route.",
routes: [
{
method: "GET",
@@ -378,7 +378,7 @@ const FAMILIES: AgentApiFamily[] = [
method: "POST",
path: "/v1/conversations",
summary:
- "start a FRESH conversation in this scope (no inherited transcript) — body {text, title?}; text becomes its first message and a run begins there asynchronously. Unlike /fork, the new session starts with only what you put in text. Human-attended turns only — refused (403) from crons and other automations",
+ "start a FRESH conversation in this scope (no inherited transcript) — body {text, title?}; text becomes its first message and a run begins there asynchronously. Returns {session, turn, webUrl?}; share webUrl verbatim when present (it is omitted without a valid configured public web URL). Unlike /fork, the new session starts with only what you put in text. Human-attended turns only — refused (403) from crons and other automations",
},
{
method: "POST",
diff --git a/src/api/routes/surface.ts b/src/api/routes/surface.ts
index b1ccd76ae..a5211ef00 100644
--- a/src/api/routes/surface.ts
+++ b/src/api/routes/surface.ts
@@ -59,6 +59,19 @@ function isConversationColor(value: unknown): value is string | null {
return value === null || (typeof value === "string" && /^#[0-9a-fA-F]{6}$/.test(value));
}
+function conversationWebUrl(publicWebUrl: string | undefined, sessionId: string): string | undefined {
+ const raw = publicWebUrl?.trim();
+ if (!raw || !/^https?:\/\//i.test(raw) || /[?#]/.test(raw)) return undefined;
+ try {
+ const base = new URL(raw);
+ if ((base.protocol !== "http:" && base.protocol !== "https:") || base.username || base.password) return undefined;
+ base.pathname = `${base.pathname.replace(/\/+$/, "")}/s/${encodeURIComponent(sessionId)}`;
+ return base.toString();
+ } catch {
+ return undefined;
+ }
+}
+
async function regenerateSessionTitle(ctx: ApiCtx): Promise<void> {
const { res, app, body } = ctx;
const id = ctx.params.id!;
@@ -87,7 +100,7 @@ async function forkSession(ctx: ApiCtx): Promise<void> {
}
async function spawnAgentConversation(ctx: ApiCtx): Promise<void> {
- const { res, app, body, capability } = ctx;
+ const { res, app, body, capability, deps } = ctx;
if (!capability) {
return sendJson(res, 401, { error: "capability_required", message: "this endpoint is for the agent self-API" });
}
@@ -133,7 +146,12 @@ async function spawnAgentConversation(ctx: ApiCtx): Promise<void> {
});
}
const runId = (turn as { runId?: string }).runId;
- return sendJson(res, 202, { session, turn: { status: turn.status, ...(runId ? { runId } : {}) } });
+ const webUrl = conversationWebUrl(deps.portalUrl, session.id);
+ return sendJson(res, 202, {
+ session,
+ turn: { status: turn.status, ...(runId ? { runId } : {}) },
+ ...(webUrl ? { webUrl } : {}),
+ });
}
async function forkAgentConversation(ctx: ApiCtx): Promise<void> {
diff --git a/test/agent-conversations-route.test.ts b/test/agent-conversations-route.test.ts
index 10eaadba9..1994af519 100644
--- a/test/agent-conversations-route.test.ts
+++ b/test/agent-conversations-route.test.ts
@@ -37,9 +37,26 @@ describe("agent conversations self-API", async () => {
body: JSON.stringify(body),
});
+ const spawnWithPortal = async (portalUrl: string | undefined, app: typeof built.app = built.app) => {
+ const configuredServer = createServer(app, { signingSecret: SECRET, ...(portalUrl ? { portalUrl } : {}) });
+ await new Promise<void>((resolve) => configuredServer.listen(0, resolve));
+ const configuredBase = `http://localhost:${(configuredServer.address() as AddressInfo).port}`;
+ try {
+ const response = await fetch(`${configuredBase}/v1/conversations`, {
+ method: "POST",
+ headers: { "content-type": "application/json", "x-agent-capability": await capFor("U1") },
+ body: JSON.stringify({ text: "start from configured portal" }),
+ });
+ const text = await response.text();
+ return { status: response.status, text, body: JSON.parse(text) as { session: { id: string }; webUrl?: string } };
+ } finally {
+ await new Promise<void>((resolve) => configuredServer.close(() => resolve()));
+ }
+ };
+
before(async () => {
built = buildApp(testConfig({ signingSecret: SECRET }));
- server = createServer(built.app, { signingSecret: SECRET });
+ server = createServer(built.app, { signingSecret: SECRET, portalUrl: "https://portal.example/" });
await new Promise<void>((resolve) => server.listen(0, resolve));
base = `http://localhost:${(server.address() as AddressInfo).port}`;
mineId = (await built.app.turn(dm("U1", "plan the launch", "web:U1:c1"))).sessionId!;
@@ -61,10 +78,12 @@ describe("agent conversations self-API", async () => {
const body = (await res.json()) as {
session: { id: string; scopeId: string; threadRef: string; title?: string | null };
turn: { status: string; runId?: string };
+ webUrl?: string;
};
assert.notEqual(body.session.id, mineId);
assert.equal(body.session.scopeId, scopeId("personal", "U1"));
assert.equal(body.session.title, "Flaky test hunt");
+ assert.equal(body.webUrl, `https://portal.example/s/${body.session.id}`);
const list = await get("/v1/conversations", token);
const { conversations } = (await list.json()) as { conversations: Array<{ id: string }> };
assert.ok(
@@ -85,6 +104,38 @@ describe("agent conversations self-API", async () => {
);
});
+ it("preserves the public base prefix and encodes the returned session id", async () => {
+ const sessionId = "session /?#";
+ const app: typeof built.app = {
+ ...built.app,
+ spawnSession: async (...args) => {
+ const out = await built.app.spawnSession(...args);
+ return out ? { session: { ...out.session, id: sessionId } } : null;
+ },
+ };
+ const result = await spawnWithPortal("https://portal.example/qm///", app);
+ assert.equal(result.status, 202);
+ assert.equal(result.body.session.id, sessionId);
+ assert.equal(result.body.webUrl, "https://portal.example/qm/s/session%20%2F%3F%23");
+ });
+
+ it("omits webUrl without a safe configured public web URL", async () => {
+ const unsafe = [
+ undefined,
+ "portal.example",
+ "ftp://portal.example/qm",
+ "https://user:[email protected]/qm",
+ "https://portal.example/qm?tenant=one",
+ "https://portal.example/qm#conversation",
+ ];
+ for (const portalUrl of unsafe) {
+ const result = await spawnWithPortal(portalUrl);
+ assert.equal(result.status, 202);
+ assert.equal(result.body.webUrl, undefined);
+ assert.ok(!result.text.includes("secret"));
+ }
+ });
+
it("spawns a fresh channel conversation for a current member", async () => {
await built.app.upsertDirectory([{ principalId: "U1", displayName: "User One", type: "internal" }]);
await built.app.upsertChannels(