Files

47 lines
1.7 KiB
TypeScript

import { NextResponse } from "next/server";
import { SessionManager, type AgentSession } from "@earendil-works/pi-coding-agent";
import { generateSessionTitle } from "@/lib/session-title";
import { getRpcSession, startRpcSession } from "@/lib/rpc-manager";
import { invalidateSessionListCache, resolveSessionPath } from "@/lib/session-reader";
export async function POST(
_req: Request,
{ params }: { params: Promise<{ id: string }> },
) {
const { id } = await params;
try {
const filePath = await resolveSessionPath(id);
if (!filePath) {
return NextResponse.json({ error: "Session not found" }, { status: 404 });
}
const cwd = SessionManager.open(filePath).getHeader()?.cwd ?? process.cwd();
const existing = getRpcSession(id);
const { session } = existing?.isAlive()
? { session: existing }
: await startRpcSession(id, filePath, cwd);
// globalThis keeps wrappers alive across dev hot reloads; older instances
// may predate waitUntilReady(), but those have already completed startup.
await session.waitUntilReady?.();
const result = await generateSessionTitle(session.inner as unknown as AgentSession);
if (!session.isAlive()) {
return NextResponse.json(
{ error: "The session was closed while its title was being generated. Please try again." },
{ status: 409 },
);
}
session.inner.setSessionName(result.title);
invalidateSessionListCache();
return NextResponse.json({ title: result.title, usage: result.usage ?? null });
} catch (error) {
return NextResponse.json(
{ error: error instanceof Error ? error.message : String(error) },
{ status: 500 },
);
}
}