feat: multi-session management (browse all + bind N sessions per notebook)
User can now see EVERY session on the OpenCode Serve side (not just the per-notebook mapping) and bind multiple sessions to a single notebook. One of the bound sessions is the "active" one (the one the next /edit uses); the others are historical. The user can switch, create, or delete via the new session selector UI in the inline prompt. Backend ------- - SessionManager rewritten for 1-notebook-N-sessions: keeps both an map and a map. New methods: create_new, bind_existing, set_active, unbind, unbind_active, delete_session, list_sessions_for_notebook. Existing get_or_create / peek / invalidate / list_sessions / release semantics preserved (release now deletes ALL bound sessions, not just the active). - New route GET /opencode-bridge/sessions/all — list ALL OpenCode sessions via /session. - New route GET /opencode-bridge/sessions/notebook?notebook=... — bound sessions for one notebook, enriched with title/createdAt from the global list. - POST .../sessions/notebook — create a new session, bind, set active. - PUT .../sessions/notebook — bind an existing sessionId, set active (no OpenCode round-trip). - DELETE .../sessions/notebook — delete the session on OpenCode AND remove its binding. - New route GET/PUT/DELETE /opencode-bridge/sessions/active — read / switch / unbind the active session for one notebook. - OpenCodeClient.list_all_sessions() — proxy OpenCode /session. Frontend ------- - SessionSelector dropdown in the inline prompt: lists ALL OpenCode sessions with the active one selected, plus a "+ 新建会话" sentinel entry. "+ 新建" button + "🔄" refresh button. Switching calls onSwitchSession; selecting "+ 新建会话" or clicking the new button calls onCreateSession. After both, the prompt's history is reloaded for the new active session. - New cell-action callbacks: onListSessions, onCreateSession, onSwitchSession, onReloadHistory. Each closes the SSE first so events for the OLD session don't bleed in during the switch. - 8 new API client functions in api/opencode_client.ts. - New types in types.ts: OpenCodeSessionMeta, OpenCodeNotebookSession, OpenCodeAllSessionsResponse, OpenCodeNotebookSessionsResponse, OpenCodeSessionOpResponse. Tests ----- - 73 backend pytest (was 64): +6 new SessionManager multi-session unit tests + 7 new route tests (all-sessions, notebook GET/POST/ PUT/DELETE, active GET/PUT/DELETE). - 76 frontend jest (was 72): +4 new prompt tests (session selector rendered when callbacks wired, initialize() populates the list, + 新建 creates+binds, dropdown change switches and reloads). - package.json version bumped 0.1.0 -> 0.1.1 to match the v0.1.1 tag that was already published. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -7,13 +7,31 @@ from opencode_bridge.session_manager import SessionManager
|
||||
|
||||
|
||||
class FakeClient:
|
||||
"""Mimics OpenCodeClient just enough for SessionManager."""
|
||||
def __init__(self, session_id: str = "sid-from-fake") -> None:
|
||||
"""Mimics OpenCodeClient just enough for SessionManager.
|
||||
|
||||
By default returns the same id for every create_session call
|
||||
(matching the original legacy behavior; several pre-existing tests
|
||||
rely on that). Tests that need a fresh id per call should set
|
||||
``unique_ids_per_call=True`` in the constructor.
|
||||
"""
|
||||
def __init__(
|
||||
self,
|
||||
session_id: str = "sid-from-fake",
|
||||
unique_ids_per_call: bool = False,
|
||||
) -> None:
|
||||
self._session_id = session_id
|
||||
self._unique_ids = unique_ids_per_call
|
||||
self._next_id = 0
|
||||
self.calls: list = []
|
||||
|
||||
async def create_session(self, title: str) -> dict:
|
||||
self.calls.append(("create_session", title))
|
||||
if self._unique_ids:
|
||||
self._next_id += 1
|
||||
return {
|
||||
"id": "%s-%d" % (self._session_id, self._next_id),
|
||||
"title": title,
|
||||
}
|
||||
return {"id": self._session_id, "title": title}
|
||||
|
||||
async def delete_session(self, session_id: str) -> bool:
|
||||
@@ -112,7 +130,7 @@ async def test_list_sessions_returns_all_mappings():
|
||||
sm = SessionManager(lambda: client)
|
||||
await sm.get_or_create("a.ipynb")
|
||||
# Manually inject a second to test listing
|
||||
sm._sessions["b.ipynb"] = "sid-b"
|
||||
sm._active["b.ipynb"] = "sid-b"
|
||||
listing = sm.list_sessions()
|
||||
paths = {s["notebookPath"] for s in listing}
|
||||
assert paths == {"a.ipynb", "b.ipynb"}
|
||||
@@ -133,3 +151,147 @@ async def test_concurrent_get_or_create_does_not_double_create():
|
||||
assert sids[0] == sids[1] == sids[2]
|
||||
create_calls = [c for c in client.calls if c[0] == "create_session"]
|
||||
assert len(create_calls) == 1, "concurrent get_or_create must not double-create"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Multi-session: 1 notebook can bind N sessions, one is active
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_new_binds_and_makes_active():
|
||||
client = FakeClient(unique_ids_per_call=True)
|
||||
sm = SessionManager(lambda: client)
|
||||
sid = await sm.create_new("foo.ipynb", title="custom")
|
||||
assert sid == "sid-from-fake-1"
|
||||
# New session is the active one and the only bound one.
|
||||
assert sm.get_active("foo.ipynb") == sid
|
||||
assert sm.list_sessions_for_notebook("foo.ipynb") == [sid]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_new_when_active_exists_keeps_old_bound():
|
||||
client = FakeClient(unique_ids_per_call=True)
|
||||
sm = SessionManager(lambda: client)
|
||||
first = await sm.get_or_create("foo.ipynb")
|
||||
second = await sm.create_new("foo.ipynb")
|
||||
# Two distinct session ids.
|
||||
assert first != second
|
||||
assert first == "sid-from-fake-1"
|
||||
assert second == "sid-from-fake-2"
|
||||
# Both are bound; the new one is active.
|
||||
bound = sm.list_sessions_for_notebook("foo.ipynb")
|
||||
assert set(bound) == {first, second}
|
||||
assert sm.get_active("foo.ipynb") == second
|
||||
# get_or_create still returns the active one (does NOT auto-pick the latest).
|
||||
assert await sm.get_or_create("foo.ipynb") == second
|
||||
|
||||
|
||||
def test_bind_existing_does_not_call_opencode():
|
||||
client = FakeClient()
|
||||
sm = SessionManager(lambda: client)
|
||||
sm.bind_existing("foo.ipynb", "sid-from-list")
|
||||
assert client.calls == [] # no OpenCode round-trip
|
||||
assert sm.get_active("foo.ipynb") == "sid-from-list"
|
||||
assert "sid-from-list" in sm.list_sessions_for_notebook("foo.ipynb")
|
||||
|
||||
|
||||
def test_set_active_requires_session_to_be_bound():
|
||||
sm = SessionManager(lambda: FakeClient())
|
||||
sm.bind_existing("foo.ipynb", "sid-A")
|
||||
assert sm.set_active("foo.ipynb", "sid-A") is True
|
||||
# Switching to a session that is NOT bound must fail.
|
||||
assert sm.set_active("foo.ipynb", "sid-UNKNOWN") is False
|
||||
# Active didn't change.
|
||||
assert sm.get_active("foo.ipynb") == "sid-A"
|
||||
|
||||
|
||||
def test_unbind_removes_from_bound_and_clears_active_if_was_active():
|
||||
sm = SessionManager(lambda: FakeClient())
|
||||
sm.bind_existing("foo.ipynb", "sid-A")
|
||||
sm.bind_existing("foo.ipynb", "sid-B")
|
||||
# B is the active one (last bind wins). Unbind B.
|
||||
assert sm.get_active("foo.ipynb") == "sid-B"
|
||||
assert sm.unbind("foo.ipynb", "sid-B") is True
|
||||
# Active falls back to None (no other session auto-promotes).
|
||||
assert sm.get_active("foo.ipynb") is None
|
||||
assert sm.list_sessions_for_notebook("foo.ipynb") == ["sid-A"]
|
||||
|
||||
|
||||
def test_unbind_non_active_leaves_active_intact():
|
||||
sm = SessionManager(lambda: FakeClient())
|
||||
sm.bind_existing("foo.ipynb", "sid-A")
|
||||
sm.bind_existing("foo.ipynb", "sid-B")
|
||||
assert sm.get_active("foo.ipynb") == "sid-B"
|
||||
# Unbind A (not the active one).
|
||||
assert sm.unbind("foo.ipynb", "sid-A") is True
|
||||
# Active is still B.
|
||||
assert sm.get_active("foo.ipynb") == "sid-B"
|
||||
|
||||
|
||||
def test_unbind_active_keeps_other_sessions():
|
||||
sm = SessionManager(lambda: FakeClient())
|
||||
sm.bind_existing("foo.ipynb", "sid-A")
|
||||
sm.bind_existing("foo.ipynb", "sid-B")
|
||||
sm.unbind_active("foo.ipynb")
|
||||
assert sm.get_active("foo.ipynb") is None
|
||||
# B is gone from the bound list; A remains.
|
||||
assert "sid-A" in sm.list_sessions_for_notebook("foo.ipynb")
|
||||
assert "sid-B" not in sm.list_sessions_for_notebook("foo.ipynb")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_session_unbinds_and_calls_opencode():
|
||||
client = FakeClient()
|
||||
sm = SessionManager(lambda: client)
|
||||
sm.bind_existing("foo.ipynb", "sid-A")
|
||||
sm.bind_existing("foo.ipynb", "sid-B")
|
||||
deleted = await sm.delete_session("foo.ipynb", "sid-A")
|
||||
assert deleted is True
|
||||
# OpenCode was called.
|
||||
assert any(
|
||||
c[0] == "delete_session" and c[1] == "sid-A" for c in client.calls
|
||||
)
|
||||
# sid-A is gone from bindings; sid-B remains as active.
|
||||
assert "sid-A" not in sm.list_sessions_for_notebook("foo.ipynb")
|
||||
assert sm.get_active("foo.ipynb") == "sid-B"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_session_not_bound_returns_false():
|
||||
client = FakeClient()
|
||||
sm = SessionManager(lambda: client)
|
||||
assert await sm.delete_session("foo.ipynb", "never-bound") is False
|
||||
# No OpenCode call.
|
||||
assert not any(c[0] == "delete_session" for c in client.calls)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_release_drops_all_bindings_and_deletes_active():
|
||||
client = FakeClient()
|
||||
sm = SessionManager(lambda: client)
|
||||
sm.bind_existing("foo.ipynb", "sid-A")
|
||||
sm.bind_existing("foo.ipynb", "sid-B")
|
||||
ok = await sm.release("foo.ipynb")
|
||||
assert ok is True
|
||||
# OpenCode delete called for the active one (sid-B).
|
||||
assert any(
|
||||
c[0] == "delete_session" and c[1] == "sid-B" for c in client.calls
|
||||
)
|
||||
# Both bindings cleared.
|
||||
assert sm.list_sessions_for_notebook("foo.ipynb") == []
|
||||
assert sm.get_active("foo.ipynb") is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_or_create_after_set_active_returns_the_active_one():
|
||||
client = FakeClient()
|
||||
sm = SessionManager(lambda: client)
|
||||
first = await sm.get_or_create("foo.ipynb")
|
||||
sm.bind_existing("foo.ipynb", "another-sid")
|
||||
sm.set_active("foo.ipynb", "another-sid")
|
||||
# Next /edit-style call returns the manually-set active session.
|
||||
assert await sm.get_or_create("foo.ipynb") == "another-sid"
|
||||
# No additional create_session call.
|
||||
creates = [c for c in client.calls if c[0] == "create_session"]
|
||||
assert len(creates) == 1 # only the initial get_or_create
|
||||
|
||||
Reference in New Issue
Block a user