Skip to content

feat(server): chatcopsNext() catch-all route adapter for Next.js (#574) - #580

Open
Saifjafri212213 wants to merge 1 commit into
codercops:devfrom
Saifjafri212213:feat/server-chatcops-next
Open

Saifjafri212213 wants to merge 1 commit into
codercops:devfrom
Saifjafri212213:feat/server-chatcops-next

Conversation

@Saifjafri212213

@Saifjafri212213 Saifjafri212213 commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

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.ts and session/route.ts).

Closes #574

Why

Today, Next.js setups need two route files: app/api/chat/route.ts and app/api/chat/session/route.ts (which re-exports POST and OPTIONS). 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() in packages/server/src/http.ts already routes paths ending in /session to the session endpoint via isSessionPath(), so the catch-all works without needing internal routing of its own.

What changed

  • @chatcops/server:
    • packages/server/src/adapters/next.ts (new): chatcopsNext(config). It creates createChatHandler and originPolicy at module scope and returns { POST, OPTIONS }, both (request: Request) => Promise<Response>. Client IP resolution and rate limits work across calls as in chatcopsVercelHandler.
    • Added "./next" to exports in packages/server/package.json alongside "./vercel", and re-exported chatcopsNext from packages/server/src/index.ts.
  • Docs:
    • Updated the Next.js App Router section in website/src/content/docs/server/fetch-runtimes.mdx to the single catch-all route, with a short note explaining the older two-file setup.
    • Added chatcopsNext to scripts/check-doc-samples.mjs so pnpm docs:check validates that Next doc samples configure session secrets or allow anonymous access.
  • Changeset & API: Minor changeset for @chatcops/server, and committed the updated API snapshot in api/server.api.md.

Notes for review

  • chatcopsNext returns { POST, OPTIONS }. A GET handler is left for later with visitor handoff (F-10), as noted in the issue.
  • Both POST and OPTIONS point to the single handler, so CORS preflight and chat/session POSTs are handled by fetchChatResponse().

How I Tested This

  • packages/server/tests/adapters/next.test.ts (4 new tests with real Request objects):
    • OPTIONS preflight returning 204 with CORS headers.
    • Session POST to /api/chat/session minting a session token (200).
    • Chat POST to /api/chat without a token returning 401 (session_required).
    • Chat POST with the session Bearer token streaming the SSE reply (200).
  • Coverage: 100% statement, branch, function, and line coverage on src/adapters/next.ts.
  • Verified in a fresh Next.js app (npx create-next-app@latest) with app/api/chat/[[...path]]/route.ts using chatcopsNext().
  • Local checks: pnpm docs:check passes (204 blocks checked, 3 skipped), pnpm api:check is up to date, pnpm publint reports all good, pnpm lint has 0 errors (38 warnings, same as dev), and pnpm -r typecheck passes cleanly.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Server: chatcopsNext(), one catch-all route for Next.js

1 participant