Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/contrib.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Feed raw `SessionNotification` events into one accumulator per UI controller and

- `apply(notification)` merges message chunks, plan updates, mode/config changes, and `tool_call` / `tool_call_update` payloads (including out-of-order updates).
- `snapshot()` returns an immutable `SessionSnapshot` Pydantic model with copied tool input/output payloads.
- Each tool call snapshot includes the optional programmatic `name`. An update that omits the name or sets it to `null` leaves the previous name unchanged.
- `subscribe(callback)` pushes updated snapshots to subscribers whenever state changes.
- `auto_reset_on_session_change` resets accumulated state automatically when a new `session_id` arrives.

Expand Down
6 changes: 6 additions & 0 deletions src/acp/contrib/session_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class _MutableToolCallState:
def __init__(self, tool_call_id: str) -> None:
self.tool_call_id = tool_call_id
self.title: str | None = None
self.name: str | None = None
self.kind: ToolKind | None = None
self.status: ToolCallStatus | None = None
self.content: list[Any] | None = None
Expand All @@ -59,6 +60,7 @@ def __init__(self, tool_call_id: str) -> None:

def apply_start(self, update: ToolCallStart) -> None:
self.title = update.title
self.name = update.name
self.kind = update.kind
self.status = update.status
self.content = _copy_model_list(update.content)
Expand All @@ -69,6 +71,8 @@ def apply_start(self, update: ToolCallStart) -> None:
def apply_progress(self, update: ToolCallProgress) -> None:
if update.title is not None:
self.title = update.title
if update.name is not None:
self.name = update.name
if update.kind is not None:
self.kind = update.kind
if update.status is not None:
Expand All @@ -86,6 +90,7 @@ def snapshot(self) -> ToolCallView:
return ToolCallView(
tool_call_id=self.tool_call_id,
title=self.title,
name=self.name,
kind=self.kind,
status=self.status,
content=tuple(item.model_copy(deep=True) for item in self.content) if self.content else None,
Expand All @@ -102,6 +107,7 @@ class ToolCallView(BaseModel):

tool_call_id: str
title: str | None
name: str | None = None
kind: ToolKind | None
status: ToolCallStatus | None
content: tuple[Any, ...] | None
Expand Down
17 changes: 16 additions & 1 deletion tests/contrib/test_contrib_session_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ def test_session_accumulator_merges_tool_calls():
session_update="tool_call",
tool_call_id="call-1",
title="Read file",
name="read_file",
status="in_progress",
)
acc.apply(notification("s", start))
first_snapshot = acc.apply(notification("s", start))
assert first_snapshot.tool_calls["call-1"].name == "read_file"
progress = ToolCallProgress(
session_update="tool_call_update",
tool_call_id="call-1",
Expand All @@ -106,9 +108,22 @@ def test_session_accumulator_merges_tool_calls():
tool = snapshot.tool_calls["call-1"]
assert tool.status == "completed"
assert tool.title == "Read file"
assert tool.name == "read_file"
assert tool.content and tool.content[0].content.text == "Done"


def test_session_accumulator_updates_tool_name_from_progress():
acc = SessionAccumulator()
first = acc.apply(notification("s", ToolCallProgress(tool_call_id="call-1", name="read_file")))
assert first.tool_calls["call-1"].name == "read_file"

updated = acc.apply(notification("s", ToolCallProgress(tool_call_id="call-1", name="read_resource")))
assert updated.tool_calls["call-1"].name == "read_resource"

unchanged = acc.apply(notification("s", ToolCallProgress(tool_call_id="call-1", name=None)))
assert unchanged.tool_calls["call-1"].name == "read_resource"


def test_session_accumulator_records_plan_and_mode():
acc = SessionAccumulator()
acc.apply(
Expand Down
Loading