Skip to content

Commit f749650

Browse files
committed
fix: align create_terminal return type with runtime handle
Signed-off-by: Chojan Shang <psiace@apache.org>
1 parent 4cc2dfd commit f749650

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

src/acp/interfaces.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
WriteTextFileRequest,
6565
WriteTextFileResponse,
6666
)
67+
from .terminal import TerminalHandle
6768
from .utils import param_model
6869

6970
__all__ = ["Agent", "Client"]
@@ -111,7 +112,7 @@ async def create_terminal(
111112
env: list[EnvVariable] | None = None,
112113
output_byte_limit: int | None = None,
113114
**kwargs: Any,
114-
) -> CreateTerminalResponse: ...
115+
) -> CreateTerminalResponse | TerminalHandle: ...
115116

116117
@param_model(TerminalOutputRequest)
117118
async def terminal_output(self, session_id: str, terminal_id: str, **kwargs: Any) -> TerminalOutputResponse: ...

src/acp/terminal.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def __init__(self, terminal_id: str, session_id: str, conn: Connection) -> None:
2020
self._session_id = session_id
2121
self._conn = conn
2222

23+
@property
24+
def terminal_id(self) -> str:
25+
return self.id
26+
2327
async def current_output(self) -> TerminalOutputResponse:
2428
response = await self._conn.send_request(
2529
CLIENT_METHODS["terminal_output"],

tests/test_rpc.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Agent,
1111
AuthenticateResponse,
1212
Client,
13+
CreateTerminalResponse,
1314
InitializeResponse,
1415
LoadSessionResponse,
1516
NewSessionResponse,
@@ -24,13 +25,15 @@
2425
update_agent_message_text,
2526
update_tool_call,
2627
)
28+
from acp.core import AgentSideConnection, ClientSideConnection
2729
from acp.schema import (
2830
AgentMessageChunk,
2931
AllowedOutcome,
3032
AudioContentBlock,
3133
ClientCapabilities,
3234
DeniedOutcome,
3335
EmbeddedResourceContentBlock,
36+
EnvVariable,
3437
HttpMcpServer,
3538
ImageContentBlock,
3639
Implementation,
@@ -130,6 +133,56 @@ async def test_session_notifications_flow(connect, client):
130133
assert client.notifications[0].session_id == "sess"
131134

132135

136+
@pytest.mark.asyncio
137+
async def test_on_connect_create_terminal_handle(server):
138+
class _TerminalAgent(Agent):
139+
__test__ = False
140+
141+
def __init__(self) -> None:
142+
self._conn: Client | None = None
143+
self.handle_id: str | None = None
144+
145+
def on_connect(self, conn: Client) -> None:
146+
self._conn = conn
147+
148+
async def prompt(
149+
self,
150+
prompt: list[TextContentBlock],
151+
session_id: str,
152+
**kwargs: Any,
153+
) -> PromptResponse:
154+
assert self._conn is not None
155+
handle = await self._conn.create_terminal(command="echo", session_id=session_id)
156+
self.handle_id = handle.terminal_id
157+
return PromptResponse(stop_reason="end_turn")
158+
159+
class _TerminalClient(TestClient):
160+
__test__ = False
161+
162+
async def create_terminal(
163+
self,
164+
command: str,
165+
session_id: str,
166+
args: list[str] | None = None,
167+
cwd: str | None = None,
168+
env: list[EnvVariable] | None = None,
169+
output_byte_limit: int | None = None,
170+
**kwargs: Any,
171+
) -> CreateTerminalResponse:
172+
return CreateTerminalResponse(terminal_id="term-123")
173+
174+
agent = _TerminalAgent()
175+
client = _TerminalClient()
176+
agent_conn = AgentSideConnection(agent, server.server_writer, server.server_reader, listening=True)
177+
client_conn = ClientSideConnection(client, server.client_writer, server.client_reader)
178+
179+
await client_conn.prompt(session_id="sess", prompt=[TextContentBlock(type="text", text="start")])
180+
assert agent.handle_id == "term-123"
181+
182+
await client_conn.close()
183+
await agent_conn.close()
184+
185+
133186
@pytest.mark.asyncio
134187
async def test_concurrent_reads(connect, client):
135188
for i in range(5):

0 commit comments

Comments
 (0)