Skip to content

Add remove() method to ChatContext for flexible item removal#5131

Open
3eid wants to merge 1 commit intolivekit:mainfrom
3eid:feat/chatctx-remove
Open

Add remove() method to ChatContext for flexible item removal#5131
3eid wants to merge 1 commit intolivekit:mainfrom
3eid:feat/chatctx-remove

Conversation

@3eid
Copy link

@3eid 3eid commented Mar 17, 2026

Summary
Adds a public remove() method to ChatContext for removing items by ID, by ChatItem instance, or a sequence of either. This avoids the need to access the private _items list directly.

Problem
ChatContext already provides helpers like get_by_id() and index_by_id(), but no public way exists to remove an item. Users currently must manipulate _items directly:

idx = ctx.index_by_id(item_id)
if idx is not None:
    ctx._items.pop(idx)

Accessing _items relies on internal implementation details and can break if the structure changes.

Fix
Adds a remove() method that:

  • Accepts a single ChatItem, an item ID (str), or a sequence of either.
  • Returns a list of removed items (empty if none were found).

This follows the suggestion from @tinalenguyen to allow more flexible usage and aligns with existing helpers like insert().

Usage Examples

# Remove by ID
ctx.remove(item_id)

# Remove by ChatItem instance
ctx.remove(item)

# Remove multiple at once
ctx.remove([item_id_1, item_2])

Testing
Added comprehensive unit tests covering:

  • Single removal by ID or ChatItem
  • Removal of sequences (IDs, instances, or mixed)
  • Partial matches and nonexistent IDs
  • Removal from empty context
  • Special cases like AgentConfigUpdate items

Closes #5085

Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

@theomonnom
Copy link
Member

Hey, this is great, but can we have a similar API to list.remove? Like https://docs.python.org/3/tutorial/datastructures.html#more-on-lists

@3eid
Copy link
Author

3eid commented Mar 18, 2026

Hey, this is great, but can we have a similar API to list.remove? Like https://docs.python.org/3/tutorial/datastructures.html#more-on-lists

Good idea.

To stay close to list.remove, I’ll make remove() operate on a single item and raise if not found.

For the input, I’m leaning toward supporting both ChatItem and str (ID), since IDs are already a first-class way to reference items in ChatContext (e.g. get_by_id, index_by_id).

Internally it would just match by item.id in both cases.

Let me know if you’d prefer restricting it to ChatItem only.

@3eid 3eid force-pushed the feat/chatctx-remove branch from ae844c3 to 26a6c9d Compare March 18, 2026 23:15
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.

2 participants