-
Notifications
You must be signed in to change notification settings - Fork 79
feat(cli): add /btw side-channel command for asking the agent mid-task #668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
juanmichelini
wants to merge
7
commits into
main
Choose a base branch
from
feat/btw-side-channel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4e5ab25
feat(cli): add /btw side-channel command for asking the agent mid-task
openhands-agent 227b855
fix: address reviewer concerns in /btw side-channel feature
openhands-agent e2f200f
fix: fix linting issues from pre-commit hooks
openhands-agent 551d60b
fix: address remaining linting issues
openhands-agent f758414
fix: move get_btw_store import to correct module
openhands-agent b1075b4
refactor(btw): address PR review concerns - remove unused callback, i…
openhands-agent 4585d9d
fix(btw): add None guards for conversation_id to fix pyright type errors
openhands-agent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| """BTW (By The Way) interceptor for handling side-channel questions. | ||
|
|
||
| This module provides a class that intercepts /btw commands and routes them | ||
| through the ask_agent side-channel without disrupting the main task flow. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from openhands_cli.tui.core.btw_store import BtwStore | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from openhands_cli.tui.core.btw_store import BtwEntry | ||
|
|
||
|
|
||
| # The /btw command prefix | ||
| BTW_COMMAND = "/btw" | ||
| BTW_PREFIX = f"{BTW_COMMAND} " | ||
|
|
||
|
|
||
| @dataclass | ||
| class BtwResult: | ||
| """Result of processing a message through the BTW interceptor.""" | ||
|
|
||
| is_btw: bool | ||
| question: str | None = None | ||
| entry_id: str | None = None | ||
|
|
||
|
|
||
| class BtwInterceptor: | ||
| """Interceptor for /btw commands. | ||
|
|
||
| Handles command parsing and store management for side-channel questions. | ||
| The actual API call is the caller's responsibility. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| store: BtwStore, | ||
| conversation_id: str | None = None, | ||
| ) -> None: | ||
| self._store = store | ||
| self._conversation_id = conversation_id | ||
|
|
||
| def set_conversation_id(self, conversation_id: str | None) -> None: | ||
| """Update the conversation ID.""" | ||
| self._conversation_id = conversation_id | ||
|
|
||
| def process(self, message: str) -> BtwResult: | ||
| """Process a message to check if it's a BTW command. | ||
|
|
||
| Returns: | ||
| BtwResult indicating if it's a BTW command and details. | ||
| """ | ||
| trimmed = message.strip() | ||
| is_btw = trimmed == BTW_COMMAND or trimmed.startswith(BTW_PREFIX) | ||
|
|
||
| if not self._conversation_id or not is_btw: | ||
| return BtwResult(is_btw=False) | ||
|
|
||
| question = trimmed[len(BTW_COMMAND) :].strip() | ||
| if not question: | ||
| return BtwResult(is_btw=False) | ||
|
|
||
| entry_id = self._store.add_pending(self._conversation_id, question) | ||
|
|
||
| return BtwResult( | ||
| is_btw=True, | ||
| question=question, | ||
| entry_id=entry_id, | ||
| ) | ||
|
|
||
| async def resolve(self, entry_id: str, response: str) -> None: | ||
| """Resolve a BTW entry with the agent's response.""" | ||
| if self._conversation_id is None: | ||
| return | ||
| self._store.resolve(self._conversation_id, entry_id, response) | ||
|
|
||
| async def fail(self, entry_id: str, error: str) -> None: | ||
| """Mark a BTW entry as failed.""" | ||
| if self._conversation_id is None: | ||
| return | ||
| self._store.fail(self._conversation_id, entry_id, error) | ||
|
|
||
| def get_entries(self) -> list[BtwEntry]: | ||
| """Get all BTW entries for the current conversation.""" | ||
| if self._conversation_id is None: | ||
| return [] | ||
| return self._store.get_entries(self._conversation_id) | ||
|
|
||
| def dismiss(self, entry_id: str) -> None: | ||
| """Dismiss a BTW entry.""" | ||
| if self._conversation_id is None: | ||
| return | ||
| self._store.dismiss(self._conversation_id, entry_id) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| """BTW (By The Way) store for tracking side-channel questions. | ||
|
|
||
| This module provides a store for managing BTW entries - questions that users | ||
| ask via /btw command without disrupting the main task flow. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
|
|
||
|
|
||
| class BtwStatus(Enum): | ||
| """Status of a BTW entry.""" | ||
|
|
||
| PENDING = "pending" | ||
| DONE = "done" | ||
| ERROR = "error" | ||
|
|
||
|
|
||
| @dataclass | ||
| class BtwEntry: | ||
| """A single BTW entry representing a side-channel question.""" | ||
|
|
||
| id: str | ||
| question: str | ||
| response: str | None = None | ||
| status: BtwStatus = BtwStatus.PENDING | ||
|
|
||
|
|
||
| class BtwStore: | ||
| """Store for BTW entries scoped to conversations. | ||
|
|
||
| This is a simple in-memory store that tracks pending, resolved, and failed | ||
| BTW entries per conversation. | ||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| self._entries_by_conversation: dict[str, list[BtwEntry]] = {} | ||
|
|
||
| def add_pending(self, conversation_id: str, question: str) -> str: | ||
| """Add a pending BTW entry. | ||
|
|
||
| Args: | ||
| conversation_id: The conversation ID. | ||
| question: The question to ask. | ||
|
|
||
| Returns: | ||
| The ID of the new entry. | ||
| """ | ||
| import uuid | ||
|
|
||
| entry_id = str(uuid.uuid4()) | ||
| entries = self._entries_by_conversation.setdefault(conversation_id, []) | ||
| entries.append( | ||
| BtwEntry(id=entry_id, question=question, status=BtwStatus.PENDING) | ||
| ) | ||
| return entry_id | ||
|
|
||
| def resolve(self, conversation_id: str, entry_id: str, response: str) -> None: | ||
| """Mark a BTW entry as resolved. | ||
|
|
||
| Args: | ||
| conversation_id: The conversation ID. | ||
| entry_id: The entry ID. | ||
| response: The agent's response. | ||
| """ | ||
| entries = self._entries_by_conversation.get(conversation_id, []) | ||
| for entry in entries: | ||
| if entry.id == entry_id: | ||
| entry.response = response | ||
| entry.status = BtwStatus.DONE | ||
| break | ||
|
|
||
| def fail(self, conversation_id: str, entry_id: str, error: str) -> None: | ||
| """Mark a BTW entry as failed. | ||
|
|
||
| Args: | ||
| conversation_id: The conversation ID. | ||
| entry_id: The entry ID. | ||
| error: The error message. | ||
| """ | ||
| entries = self._entries_by_conversation.get(conversation_id, []) | ||
| for entry in entries: | ||
| if entry.id == entry_id: | ||
| entry.response = error | ||
| entry.status = BtwStatus.ERROR | ||
| break | ||
|
|
||
| def dismiss(self, conversation_id: str, entry_id: str) -> None: | ||
| """Dismiss (remove) a BTW entry. | ||
|
|
||
| Args: | ||
| conversation_id: The conversation ID. | ||
| entry_id: The entry ID to dismiss. | ||
| """ | ||
| entries = self._entries_by_conversation.get(conversation_id, []) | ||
| self._entries_by_conversation[conversation_id] = [ | ||
| e for e in entries if e.id != entry_id | ||
| ] | ||
|
|
||
| def get_entries(self, conversation_id: str) -> list[BtwEntry]: | ||
| """Get all BTW entries for a conversation. | ||
|
|
||
| Args: | ||
| conversation_id: The conversation ID. | ||
|
|
||
| Returns: | ||
| List of BTW entries. | ||
| """ | ||
| return list(self._entries_by_conversation.get(conversation_id, [])) | ||
|
|
||
| def clear(self, conversation_id: str | None = None) -> None: | ||
| """Clear BTW entries. | ||
|
|
||
| Args: | ||
| conversation_id: If provided, clear only entries for this conversation. | ||
| If None, clear all entries. | ||
| """ | ||
| if conversation_id is None: | ||
| self._entries_by_conversation.clear() | ||
| elif conversation_id in self._entries_by_conversation: | ||
| del self._entries_by_conversation[conversation_id] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.