forked from alexhancock/agent-client-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.rs
More file actions
187 lines (167 loc) · 6.33 KB
/
client.rs
File metadata and controls
187 lines (167 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! A simple ACP client for educational purposes.
//!
//! The client starts an agent as a subprocess and communicates with it over stdio. Run the client like this:
//!
//! ```bash
//! cargo run --example client -- path/to/agent --agent-arg
//! ```
//!
//! To connect it to the example agent from this crate:
//!
//! ```bash
//! cargo build --example agent && cargo run --example client -- target/debug/examples/agent
//! ```
use agent_client_protocol::{
self as acp, Agent, ExtNotification, ExtRequest, ExtResponse, KillTerminalCommandResponse,
};
use anyhow::bail;
use tokio_util::compat::{TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt};
struct ExampleClient {}
#[async_trait::async_trait(?Send)]
impl acp::Client for ExampleClient {
async fn request_permission(
&self,
_args: acp::RequestPermissionRequest,
) -> anyhow::Result<acp::RequestPermissionResponse, acp::Error> {
Err(acp::Error::method_not_found())
}
async fn write_text_file(
&self,
_args: acp::WriteTextFileRequest,
) -> anyhow::Result<acp::WriteTextFileResponse, acp::Error> {
Err(acp::Error::method_not_found())
}
async fn read_text_file(
&self,
_args: acp::ReadTextFileRequest,
) -> anyhow::Result<acp::ReadTextFileResponse, acp::Error> {
Err(acp::Error::method_not_found())
}
async fn create_terminal(
&self,
_args: acp::CreateTerminalRequest,
) -> Result<acp::CreateTerminalResponse, acp::Error> {
Err(acp::Error::method_not_found())
}
async fn terminal_output(
&self,
_args: acp::TerminalOutputRequest,
) -> anyhow::Result<acp::TerminalOutputResponse, acp::Error> {
Err(acp::Error::method_not_found())
}
async fn release_terminal(
&self,
_args: acp::ReleaseTerminalRequest,
) -> anyhow::Result<acp::ReleaseTerminalResponse, acp::Error> {
Err(acp::Error::method_not_found())
}
async fn wait_for_terminal_exit(
&self,
_args: acp::WaitForTerminalExitRequest,
) -> anyhow::Result<acp::WaitForTerminalExitResponse, acp::Error> {
Err(acp::Error::method_not_found())
}
async fn kill_terminal_command(
&self,
_args: acp::KillTerminalCommandRequest,
) -> anyhow::Result<KillTerminalCommandResponse, acp::Error> {
Err(acp::Error::method_not_found())
}
async fn session_notification(
&self,
args: acp::SessionNotification,
) -> anyhow::Result<(), acp::Error> {
match args.update {
acp::SessionUpdate::AgentMessageChunk { content } => {
let text = match content {
acp::ContentBlock::Text(text_content) => text_content.text,
acp::ContentBlock::Image(_) => "<image>".into(),
acp::ContentBlock::Audio(_) => "<audio>".into(),
acp::ContentBlock::ResourceLink(resource_link) => resource_link.uri,
acp::ContentBlock::Resource(_) => "<resource>".into(),
};
println!("| Agent: {text}");
}
acp::SessionUpdate::UserMessageChunk { .. }
| acp::SessionUpdate::AgentThoughtChunk { .. }
| acp::SessionUpdate::ToolCall(_)
| acp::SessionUpdate::ToolCallUpdate(_)
| acp::SessionUpdate::Plan(_)
| acp::SessionUpdate::CurrentModeUpdate { .. }
| acp::SessionUpdate::AvailableCommandsUpdate { .. } => {}
}
Ok(())
}
async fn ext_method(&self, _args: ExtRequest) -> Result<ExtResponse, acp::Error> {
Err(acp::Error::method_not_found())
}
async fn ext_notification(&self, _args: ExtNotification) -> Result<(), acp::Error> {
Err(acp::Error::method_not_found())
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
env_logger::init();
let command = std::env::args().collect::<Vec<_>>();
let (outgoing, incoming, child) = match command.as_slice() {
[_, program, args @ ..] => {
let mut child = tokio::process::Command::new(program)
.args(args.iter())
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.kill_on_drop(true)
.spawn()?;
(
child.stdin.take().unwrap().compat_write(),
child.stdout.take().unwrap().compat(),
child,
)
}
_ => bail!("Usage: client AGENT_PROGRAM AGENT_ARG..."),
};
// The ClientSideConnection will spawn futures onto our Tokio runtime.
// LocalSet and spawn_local are used because the futures from the
// agent-client-protocol crate are not Send.
let local_set = tokio::task::LocalSet::new();
local_set
.run_until(async move {
// Set up the ExampleClient connected to the agent's stdio.
let (conn, handle_io) =
acp::ClientSideConnection::new(ExampleClient {}, outgoing, incoming, |fut| {
tokio::task::spawn_local(fut);
});
// Handle I/O in the background.
tokio::task::spawn_local(handle_io);
// Connect to the agent and set up a session.
conn.initialize(acp::InitializeRequest {
protocol_version: acp::V1,
client_capabilities: acp::ClientCapabilities::default(),
meta: None,
})
.await?;
let response = conn
.new_session(acp::NewSessionRequest {
mcp_servers: Vec::new(),
cwd: std::env::current_dir()?,
meta: None,
})
.await?;
// Send prompts to the agent until stdin is closed.
let mut rl = rustyline::DefaultEditor::new()?;
while let Ok(line) = rl.readline("> ") {
let result = conn
.prompt(acp::PromptRequest {
session_id: response.session_id.clone(),
prompt: vec![line.into()],
meta: None,
})
.await;
if let Err(e) = result {
log::error!("{e}");
}
}
drop(child);
Ok(())
})
.await
}