Repository navigation
feat(server): chatcopsNext() catch-all route adapter for Next.js (#574) - #580
Saifjafri212213 wants to merge 4 commits into
Conversation
anurag629
left a comment
There was a problem hiding this comment.
Thanks @Saifjafri212213, this mostly works. Both /api/chat and /api/chat/session go through one handler, the four tests from #574 are there, and so are the changeset and the api/ snapshot. A few things need to change before it can go in:
-
chatcopsNextatpackages/server/src/adapters/next.ts:7-20ischatcopsVercelHandler(packages/server/src/adapters/vercel.ts:7-13) with a different return. Build it on that instead, so Next and Vercel share one code path:import { chatcopsVercelHandler } from './vercel.js'; import type { ChatCopsServerConfig } from '../config.js'; export function chatcopsNext(config: ChatCopsServerConfig) { const handler = chatcopsVercelHandler(config); return { POST: handler, OPTIONS: handler }; }
The type comes out the same, so
api/doesn't change, and your tests cover it as they are. -
Two pages still tell Next.js users to add the second route file, which is the step #574 removes:
website/src/content/docs/server/setup.mdx:230in the session tokens section, and the Next.js part ofwebsite/src/content/docs/examples/vercel-full.mdx:58-82. Point both at the catch-all route on the fetch runtimes page. Infetch-runtimes.mdx, also say the double brackets matter:app/api/chat/[...path]/route.tsdoesn't match/api/chatitself, so every chat request gets a 404. -
The description needs the "AI assistance" section (
CONTRIBUTING.md:13), like #545 and #569 had. The issue also asks what you ran in the fresh Next.js app. Please add the Next.js version, how you loaded the widget, and that a message got a reply after its/api/chat/sessionrequest.
When you push, merge dev in too. #585 and #587 went in since, with no overlap. I'll review it again then.
|
Thanks @anurag629 for the review! I've updated the PR accordingly:
|
anurag629
left a comment
There was a problem hiding this comment.
Thanks @Saifjafri212213, nearly there. chatcopsNext now builds on chatcopsVercelHandler, the double-bracket note is in fetch-runtimes.mdx:34, setup.mdx:230 and vercel-full.mdx point at the catch-all route, and the description has the Next.js 15 run and the AI section. All checks pass, including e2e.
Two more places still tell Next.js users to add app/api/chat/session/route.ts, the step #574 removes:
website/src/content/docs/server/security.mdx:68, the comment on the Vercel and Next.js example.packages/server/README.md:53, under Vercel Edge. That README is what npm shows for@chatcops/server, and people copy from it as often as from the docs.
Point both at the catch-all route the way setup.mdx:230 does now, and keep the second file only for Vercel without Next.js. I'll review it again then.
|
Thanks @anurag629! I have updated both places:
|
anurag629
left a comment
There was a problem hiding this comment.
Thanks @Saifjafri212213. security.mdx:67 and packages/server/README.md:53 now send Next.js users to the [[...path]] catch-all with chatcopsNext and keep api/chat/session.ts only for Vercel without Next.js, the same way setup.mdx:230 reads. All checks pass, including e2e.
Summary
A dedicated Next.js App Router adapter (
chatcopsNext()) that serves both the chat and session endpoints from a single catch-all route (app/api/chat/[[...path]]/route.ts), replacing the previous two-file requirement (route.tsandsession/route.ts).Closes #574
Why
Today, Next.js setups need two route files:
app/api/chat/route.tsandapp/api/chat/session/route.ts(which re-exportsPOSTandOPTIONS). Forgetting the second one quietly breaks session tokens with 404s when the chat tries to initialize.Next.js optional catch-all routes (
[[...path]]) let a single route file handle every subpath under/api/chat.fetchChatResponse()inpackages/server/src/http.tsalready routes paths ending in/sessionto the session endpoint viaisSessionPath(), so the catch-all works without needing internal routing of its own.What changed
@chatcops/server:packages/server/src/adapters/next.ts:chatcopsNext(config)builds onchatcopsVercelHandler(config)and returns{ POST: handler, OPTIONS: handler }, sharing one battle-tested code path for Vercel/Next fetch runtimes."./next"toexportsinpackages/server/package.jsonalongside"./vercel", and re-exportedchatcopsNextfrompackages/server/src/index.ts.website/src/content/docs/server/fetch-runtimes.mdxexplaining the catch-all route, emphasizing that double brackets[[...path]]are required so/api/chatitself does not 404.website/src/content/docs/server/setup.mdxandwebsite/src/content/docs/examples/vercel-full.mdxto direct Next.js users to the single catch-all route instead of two route files.chatcopsNexttoscripts/check-doc-samples.mjssopnpm docs:checkvalidates that Next doc samples configure session secrets or allow anonymous access.@chatcops/server, and committed the updated API snapshot inapi/server.api.md.Notes for review
chatcopsNextreturns{ POST, OPTIONS }. AGEThandler is left for later with visitor handoff (F-10), as noted in the issue.POSTandOPTIONSpoint to the single handler, so CORS preflight and chat/session POSTs are handled byfetchChatResponse().How I Tested This
packages/server/tests/adapters/next.test.ts(4 new tests with realRequestobjects):OPTIONSpreflight returning 204 with CORS headers.POSTto/api/chat/sessionminting a session token (200).POSTto/api/chatwithout a token returning 401 (session_required).POSTwith the session Bearer token streaming the SSE reply (200).src/adapters/next.ts.create-next-app@latestwith App Router and TypeScript).app/api/chat/[[...path]]/route.tswithchatcopsNext().chatcops.min.js) via Next.js<Script src="http://localhost:3000/chatcops.min.js" data-api-url="/api/chat" />inapp/layout.tsx./api/chat/session, received a 200 with JWT token, and subsequent user messages were sent withAuthorization: Bearer <token>and successfully streamed assistant replies.pnpm docs:checkpasses (205 blocks checked, 3 skipped),pnpm api:checkis up to date,pnpm publintreports all good,pnpm linthas 0 errors, andpnpm -r typecheckpasses cleanly.AI assistance
Written with an AI coding assistant (Google Antigravity / Gemini) for initial route scaffolding, test drafting, and documentation text. I verified every line, tested in the fresh Next.js app, and executed all automated checks above.