Skip to content

feat(api-server): add session management API for frontend clients - #8556

Closed
Codename-11 wants to merge 2 commits into
NousResearch:mainfrom
Codename-11:feat/session-api
Closed

feat(api-server): add session management API for frontend clients#8556
Codename-11 wants to merge 2 commits into
NousResearch:mainfrom
Codename-11:feat/session-api

Conversation

@Codename-11

Copy link
Copy Markdown
Contributor

Summary

Adds 20 REST endpoints to the API server that give web and mobile frontends a complete session management API — CRUD, streaming chat with structured lifecycle events, memory, skills, and config — all flowing through the gateway's existing auth pipeline.

Motivation: The current API server exposes OpenAI-compatible endpoints (/v1/chat/completions, /v1/responses) which are stateless. Every frontend (web UIs, mobile apps, desktop clients) that wants persistent sessions, tool visibility, and session history has to reinvent session management client-side. This PR provides a server-side session API that multiple frontends can share.

Supersedes #6334 (which added read-only endpoints only — no chat capability).

Endpoints Added (20)

Session CRUD (8)

Method Path Description
GET /api/sessions List sessions (paginated, filterable by source)
POST /api/sessions Create session (optional title, model, source)
GET /api/sessions/search?q=... Full-text search across session messages
GET /api/sessions/{id} Get single session
GET /api/sessions/{id}/messages Session message history
PATCH /api/sessions/{id} Update title, system_prompt, or end_reason
DELETE /api/sessions/{id} Delete session
POST /api/sessions/{id}/fork Deep-clone session + messages

Session Chat (2) — the key addition

Method Path Description
POST /api/sessions/{id}/chat Synchronous agent chat turn
POST /api/sessions/{id}/chat/stream SSE streaming with structured events

The streaming endpoint emits rich lifecycle events:

session.created → run.started → message.started → assistant.delta* → 
tool.started → tool.completed → assistant.completed → run.completed → done

Includes periodic SSE keepalive comments (every 30s) matching the existing pattern on /v1/chat/completions — prevents client/proxy timeouts during agent init and long LLM calls with extended thinking.

Memory CRUD (4)

Method Path Description
GET /api/memory Read entries (filterable by target)
POST /api/memory Add entry
PATCH /api/memory Replace entry
DELETE /api/memory Delete entry

Skills (3, read-only)

Method Path Description
GET /api/skills List all skills
GET /api/skills/categories List categories
GET /api/skills/{name} View skill detail

Config (3)

Method Path Description
GET /api/config Current model/provider settings
PATCH /api/config Update model/provider
GET /api/available-models Curated model list per provider

Other

  • Capability probe fast-path on /v1/chat/completions: max_tokens=1 or model='test' returns an instant minimal response so frontends can detect endpoint availability without spinning up an agent.
  • Proper shutdown cleanup (SessionDB.close, memory store clear)

Architecture

All endpoints use the existing _check_auth() gate. Session state delegates to the existing SessionDB (SQLite + FTS5). Chat endpoints instantiate the same AIAgent used by /v1/chat/completions, with the addition of session-aware conversation history loading and persistence.

No parallel agent lifecycle, no gateway runner bypass — chat flows through the same agent instantiation path.

Type of Change

  • New feature (non-breaking change that adds functionality)
  • Tests (adding or improving test coverage)

Tests

91 new tests in tests/gateway/test_session_api.py:

  • Session CRUD (26 tests)
  • Memory CRUD (13 tests)
  • Skills (5 tests)
  • Config (7 tests)
  • Auth enforcement (22 tests — all endpoints blocked without key)
  • Chat endpoints (10 tests — sync + SSE streaming lifecycle)
  • Capability probe (3 tests)
python -m pytest tests/gateway/test_session_api.py -q
# 91 passed in ~5s

Checklist

  • My commit messages follow Conventional Commits
  • I searched for existing PRs to make sure this is not a duplicate
  • My PR contains only changes related to this feature
  • I've added tests that cover the new functionality
  • All 91 new tests pass
  • Existing tests unaffected (single file modified: gateway/platforms/api_server.py)

Adds 20 REST endpoints to the API server that enable web/mobile
frontends to manage sessions, chat with streaming, browse skills,
read/write memory, and configure models — all through the gateway's
existing auth and message handling pipeline.

Session CRUD:
- GET    /api/sessions                        — list (paginated)
- POST   /api/sessions                        — create
- GET    /api/sessions/search?q=...           — full-text search
- GET    /api/sessions/{id}                   — get single
- GET    /api/sessions/{id}/messages           — message history
- PATCH  /api/sessions/{id}                   — update title/prompt
- DELETE /api/sessions/{id}                   — delete
- POST   /api/sessions/{id}/fork              — deep clone

Session Chat:
- POST   /api/sessions/{id}/chat              — sync agent turn
- POST   /api/sessions/{id}/chat/stream       — SSE streaming turn
  with structured lifecycle events (session.created, run.started,
  message.started, assistant.delta, tool.started, tool.completed,
  assistant.completed, run.completed, done) and periodic keepalive
  heartbeats to prevent client/proxy timeouts during long LLM calls

Memory (CRUD):
- GET    /api/memory                          — read entries
- POST   /api/memory                          — add entry
- PATCH  /api/memory                          — replace entry
- DELETE /api/memory                          — delete entry

Skills (read-only):
- GET    /api/skills                          — list all
- GET    /api/skills/categories               — list categories
- GET    /api/skills/{name}                   — view skill detail

Config:
- GET    /api/config                          — current settings
- PATCH  /api/config                          — update model/provider
- GET    /api/available-models                — curated model list

Also adds:
- Capability probe fast-path for /v1/chat/completions (max_tokens=1
  or model='test' returns instant minimal response for frontend
  endpoint detection)
- SSE keepalive on chat/stream matching the existing pattern on
  /v1/chat/completions
- Proper shutdown cleanup (SessionDB.close, memory store clear)
- 91 new tests covering all endpoints, auth, error handling, SSE
  streaming lifecycle, and capability probe
@Codename-11
Codename-11 marked this pull request as ready for review April 12, 2026 19:48
Upstream refactor removed skills_categories from tools/skills_tool.py.
Remove the import, the _handle_skill_categories handler, and the
/api/skills/categories route registration to restore gateway startup.
The app uses /api/skills?category= for filtering; the standalone
categories endpoint was not called by any current client.
@alt-glitch alt-glitch added type/feature New feature or request P3 Low — cosmetic, nice to have comp/gateway Gateway runner, session dispatch, delivery labels Apr 28, 2026
@wirwolf

wirwolf commented May 20, 2026

Copy link
Copy Markdown

Hey. Thanks for your changes. I am waiting for this feature

@Codename-11

Copy link
Copy Markdown
Contributor Author

Superseded by #29302.

This original branch had the right product intent — API Server clients need a real session/control surface, not only OpenAI-compatible chat — but it grew into a broad/stale patch covering sessions, memory, skills, config, and other admin surfaces, and it now conflicts with current main.

I rebuilt the useful session-control portion from current upstream/main in #29302 as a smaller, reviewable PR:

  • focused /api/sessions CRUD/message/fork/chat/stream endpoints
  • /v1/capabilities discovery metadata
  • existing SessionDB + _run_agent machinery only
  • preserves current /v1/runs, gateway_session_key, and reasoning config behavior
  • explicitly defers admin/jobs/memory/skills/realtime voice expansion

Closing this one to avoid splitting review across two competing API-server branches.

@Codename-11

Copy link
Copy Markdown
Contributor Author

Closing as superseded by #29302.

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

Labels

comp/gateway Gateway runner, session dispatch, delivery P3 Low — cosmetic, nice to have type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants