Skip to content

Open-source SEO, GEO, and marketing skills for AI agents.

fix(chat): take project from URL not cookie for transcript + chat APIs

Yuting Zhong13eab3ec2078
diff --git a/src/app/api/agents/[agent]/threads/[thread]/transcript/route.ts b/src/app/api/agents/[agent]/threads/[thread]/transcript/route.ts
index b76c0958..a1834502 100644
--- a/src/app/api/agents/[agent]/threads/[thread]/transcript/route.ts
+++ b/src/app/api/agents/[agent]/threads/[thread]/transcript/route.ts
@@ -1,5 +1,6 @@
import { NextResponse } from "next/server";
import { getActiveProject } from "@/server/active-project";
+import { getProject } from "@/server/db/projects";
import { resolveAgentBySlug } from "@/server/agent-meta";
import { readTranscriptTail } from "@/server/openclaw/transcript-tail";
@@ -13,6 +14,12 @@ export const runtime = "nodejs";
* poll this with the byte offset they last saw; we return any events
* written since. Threads have no terminal state, so callers manage their
* own "stop polling" signal (the task workspace gates on task.status).
+ *
+ * Project resolution: prefer the explicit `project` query param (the page
+ * knows its URL slug and passes it through), fall back to the
+ * active-project cookie. The cookie can lag the URL on first paint after a
+ * project switch or direct deep-link, so the explicit param is the source
+ * of truth when present.
*/
export async function GET(
request: Request,
@@ -20,7 +27,11 @@ export async function GET(
) {
const { agent: agentSlug, thread: threadId } = await context.params;
- const project = await getActiveProject();
+ const url = new URL(request.url);
+ const projectParam = url.searchParams.get("project");
+ const project = projectParam
+ ? getProject(projectParam)
+ : await getActiveProject();
if (!project) {
return NextResponse.json({ error: "no active project" }, { status: 400 });
}
@@ -30,7 +41,6 @@ export async function GET(
return NextResponse.json({ error: "unknown agent" }, { status: 404 });
}
- const url = new URL(request.url);
const offsetParam = url.searchParams.get("offset");
const byteOffset = offsetParam ? Number(offsetParam) : 0;
const validOffset = Number.isFinite(byteOffset) && byteOffset >= 0 ? byteOffset : 0;
diff --git a/src/app/api/chat/route.ts b/src/app/api/chat/route.ts
index 29a49f58..96fcdfe5 100644
--- a/src/app/api/chat/route.ts
+++ b/src/app/api/chat/route.ts
@@ -1,5 +1,6 @@
import { NextResponse } from "next/server";
import { getActiveProject } from "@/server/active-project";
+import { getProject } from "@/server/db/projects";
import { resolveAgentBySlug } from "@/server/agent-meta";
import { streamChatViaGateway } from "@/server/openclaw/gateway-client";
import {
@@ -43,6 +44,12 @@ function makePerf(tag: string) {
type ChatPostBody = {
message: string;
agent?: string;
+ /**
+ * URL slug of the project this chat belongs to. The page passes it down so
+ * the route doesn't have to rely on the active-project cookie, which can
+ * lag the URL on first paint after a project switch or direct deep-link.
+ */
+ project?: string;
/** OpenClaw session UUID — used for trajectory file naming. */
sessionId?: string;
/**
@@ -57,14 +64,6 @@ type ChatPostBody = {
export async function POST(request: Request) {
const perf = makePerf("route");
perf.mark("route_start");
- const project = await getActiveProject();
- perf.mark("active_project_resolved");
- if (!project) {
- return NextResponse.json(
- { error: "No active project. Create one first." },
- { status: 400 },
- );
- }
let body: ChatPostBody;
try {
@@ -77,6 +76,19 @@ export async function POST(request: Request) {
return NextResponse.json({ error: "message is required" }, { status: 400 });
}
+ // Explicit project (from the URL slug the page knows) wins; fall back to
+ // the cookie for backwards compatibility with any non-page caller.
+ const project = body.project
+ ? getProject(body.project)
+ : await getActiveProject();
+ perf.mark("active_project_resolved");
+ if (!project) {
+ return NextResponse.json(
+ { error: "No active project. Create one first." },
+ { status: 400 },
+ );
+ }
+
const requestedSlug = (body.agent ?? "cmo").trim();
const resolved = await resolveAgentBySlug(project.slug, requestedSlug);
perf.mark("agent_resolved");
diff --git a/src/components/live-transcript.tsx b/src/components/live-transcript.tsx
index 9e23103f..bfb6c38e 100644
--- a/src/components/live-transcript.tsx
+++ b/src/components/live-transcript.tsx
@@ -160,7 +160,10 @@ export function LiveTranscript({
// ── Live tail polling. ─────────────────────────────────────────────
const pollOnce = useCallback(async () => {
try {
- const url = `/api/agents/${agentSlug}/threads/${threadId}/transcript?offset=${byteOffset}`;
+ // Pass projectSlug explicitly: the API route would otherwise fall back
+ // to the active-project cookie, which can lag the URL on first paint
+ // after a project switch or direct deep-link.
+ const url = `/api/agents/${agentSlug}/threads/${threadId}/transcript?offset=${byteOffset}&project=${encodeURIComponent(projectSlug)}`;
const res = await fetch(url, { cache: "no-store" });
if (!res.ok) return { newEvents: 0 };
const data = (await res.json()) as {
@@ -187,7 +190,7 @@ export function LiveTranscript({
} catch {
return { newEvents: 0 };
}
- }, [agentSlug, byteOffset, onPolled, threadId]);
+ }, [agentSlug, byteOffset, onPolled, projectSlug, threadId]);
// Poll faster while the parent says the task is in flight — typical
// first-task experience is "land on workspace, watch the audit run."
@@ -270,6 +273,7 @@ export function LiveTranscript({
body: JSON.stringify({
message: text,
agent: agentSlug,
+ project: projectSlug,
sessionId: threadId,
sessionKey,
}),