Skip to content

Commit 2c418f7

Browse files
authored
feat: add list_sessions and get_session_messages top-level functions (#622)
Adds `list_sessions()` and `get_session_messages()` as top-level functions, matching the TypeScript SDK's `listSessions()` and `getSessionMessages()`. These are **filesystem-reading implementations** (not CLI passthroughs) — they scan `~/.claude/projects/` directly, same as the TS SDK. ## `list_sessions(directory=None, limit=None, include_worktrees=True) -> list[SDKSessionInfo]` - Scans project directories under `~/.claude/projects/` (or `CLAUDE_CONFIG_DIR`) - Reads head/tail 64KB of each `.jsonl` file (does not load entire large transcripts) - Extracts `session_id`, `summary`, `last_modified`, `file_size`, `custom_title`, `first_prompt`, `git_branch`, `cwd` - `include_worktrees=True` expands to active git worktree session dirs; `False` filters them out - Metadata-only sessions are dropped (no `'(session)'` placeholder) ## `get_session_messages(session_id, directory=None, limit=None, offset=0) -> list[SessionMessage]` - Reads one session's `.jsonl` fully - Reconstructs the conversation chain via `parentUuid` backward traversal (leaf → root, reversed) - Handles branched transcripts (sidechains, edit history) by picking the main-line leaf (non-sidechain, non-meta, highest file position) - Filters to visible `user`/`assistant` messages only - Returns `[]` for nonexistent/invalid session IDs (never raises) <!-- CHANGELOG:START --> - Add `list_sessions()` and `get_session_messages()` top-level functions - Add `SDKSessionInfo` and `SessionMessage` dataclasses <!-- CHANGELOG:END --> ## Test plan - Unit: 59 tests covering path sanitization, JSONL head/tail parsing, parentUuid chain reconstruction, main-chain leaf selection, visibility filtering, limit/offset pagination - E2E `list_sessions`: verified against 637 real sessions — all shape checks pass, `include_worktrees` expansion confirmed on a 16-worktree repo (270 → 274 sessions), nonexistent dir → clean `[]` - E2E `get_session_messages`: 8 real sessions (~14MB JSONL) — chain reconstruction proven (0 broken links across 1,581 entries), 254-branch session correctly walks main-line, isMeta filtering verified, pagination matches exact slicing
1 parent 9af27d7 commit 2c418f7

4 files changed

Lines changed: 2072 additions & 0 deletions

File tree

src/claude_agent_sdk/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
CLINotFoundError,
1414
ProcessError,
1515
)
16+
from ._internal.sessions import get_session_messages, list_sessions
1617
from ._internal.transport import Transport
1718
from ._version import __version__
1819
from .client import ClaudeSDKClient
@@ -59,6 +60,8 @@
5960
SandboxSettings,
6061
SdkBeta,
6162
SdkPluginConfig,
63+
SDKSessionInfo,
64+
SessionMessage,
6265
SettingSource,
6366
StopHookInput,
6467
SubagentStartHookInput,
@@ -402,6 +405,11 @@ async def call_tool(name: str, arguments: dict[str, Any]) -> Any:
402405
"SettingSource",
403406
# Plugin support
404407
"SdkPluginConfig",
408+
# Session listing
409+
"list_sessions",
410+
"get_session_messages",
411+
"SDKSessionInfo",
412+
"SessionMessage",
405413
# Beta support
406414
"SdkBeta",
407415
# Sandbox support

0 commit comments

Comments
 (0)