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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
// ───────────────────────────────────────────────────────────────────
// SSE streaming
// ───────────────────────────────────────────────────────────────────
use std::sync::mpsc;
use atuin_client::settings::AiCapabilities;
use atuin_common::tls::ensure_crypto_provider;
use eventsource_stream::Eventsource;
use eye_declare::Handle;
use eyre::{Context, Result};
use futures::StreamExt;
use reqwest::Url;
use reqwest::header::USER_AGENT;
use crate::{
context::{AppContext, ClientContext},
tools::ClientToolCall,
tui::{Session, events::AiTuiEvent},
};
static APP_USER_AGENT: &str = concat!("atuin/", env!("CARGO_PKG_VERSION"));
/// Frames that alter the stream lifecycle — terminal or state-changing.
#[derive(Debug, Clone)]
pub(crate) enum StreamControl {
Done { session_id: String },
Error(String),
StatusChanged(String),
}
/// Frames that carry conversation content — they mutate the event log.
#[derive(Debug, Clone)]
pub(crate) enum StreamContent {
TextChunk(String),
ToolCall {
id: String,
name: String,
input: serde_json::Value,
},
ToolResult {
tool_use_id: String,
content: String,
is_error: bool,
remote: bool,
content_length: Option<usize>,
},
}
/// A frame from the SSE stream, classified as control or content.
#[derive(Debug, Clone)]
pub(crate) enum StreamFrame {
Content(StreamContent),
Control(StreamControl),
}
/// Per-turn request payload for the chat API.
pub(crate) struct ChatRequest {
pub messages: Vec<serde_json::Value>,
pub session_id: Option<String>,
pub capabilities: Vec<String>,
pub invocation_id: String,
}
impl ChatRequest {
pub(crate) fn new(
messages: Vec<serde_json::Value>,
session_id: Option<String>,
capabilities: &AiCapabilities,
invocation_id: String,
) -> Self {
let mut caps = vec!["client_invocations".to_string()];
if capabilities.enable_history_search.unwrap_or(true) {
caps.push("client_v1_atuin_history".to_string());
}
if capabilities.enable_file_tools.unwrap_or(true) {
caps.push("client_v1_read_file".to_string());
caps.push("client_v1_edit_file".to_string());
caps.push("client_v1_write_file".to_string());
}
if capabilities.enable_command_execution.unwrap_or(true) {
caps.push("client_v1_execute_shell_command".to_string());
}
if let Ok(extra) = std::env::var("ATUIN_AI__ADDITIONAL_CAPS") {
caps.extend(
extra
.split(',')
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty()),
);
}
Self {
messages,
session_id,
capabilities: caps,
invocation_id,
}
}
}
fn create_chat_stream(
hub_address: String,
token: String,
request: ChatRequest,
client_ctx: ClientContext,
send_cwd: bool,
last_command: Option<String>,
) -> std::pin::Pin<Box<dyn futures::Stream<Item = Result<StreamFrame>> + Send>> {
Box::pin(async_stream::stream! {
ensure_crypto_provider();
let endpoint = match hub_url(&hub_address, "/api/cli/chat") {
Ok(url) => url,
Err(e) => {
yield Err(e);
return;
}
};
tracing::debug!("Sending SSE request to {endpoint}");
let context = client_ctx.to_json(send_cwd, last_command.as_deref());
let mut request_body = serde_json::json!({
"messages": request.messages,
"context": context,
"capabilities": request.capabilities,
"invocation_id": request.invocation_id
});
if let Some(ref sid) = request.session_id {
tracing::trace!("Including session_id in request: {sid}");
request_body["session_id"] = serde_json::json!(sid);
}
let client = reqwest::Client::new();
let response = match client
.post(endpoint.clone())
.header("Accept", "text/event-stream")
.header(USER_AGENT, APP_USER_AGENT)
.bearer_auth(&token)
.json(&request_body)
.send()
.await
{
Ok(resp) => resp,
Err(e) => {
yield Err(eyre::eyre!("Failed to send SSE request: {}", e));
return;
}
};
let status = response.status();
if status == reqwest::StatusCode::UNAUTHORIZED {
tracing::error!("SSE request failed with status: {status}, clearing session");
let _ = atuin_client::hub::delete_session().await;
yield Err(eyre::eyre!("Hub session expired. Re-run to authenticate again."));
return;
}
if !status.is_success() {
let body = response.text().await.unwrap_or_default();
tracing::error!("SSE request failed ({}): {}", status, body);
yield Err(eyre::eyre!("SSE request failed ({}): {}", status, body));
return;
}
let byte_stream = response.bytes_stream();
let mut stream = byte_stream.eventsource();
while let Some(event) = stream.next().await {
match event {
Ok(sse_event) => {
let event_type = sse_event.event.as_str();
let data = sse_event.data.clone();
tracing::debug!(event_type = %event_type, "SSE event received");
match event_type {
"text" => {
if let Ok(json) = serde_json::from_str::<serde_json::Value>(&data)
&& let Some(content) = json.get("content").and_then(|v| v.as_str())
{
yield Ok(StreamFrame::Content(StreamContent::TextChunk(content.to_string())));
}
}
"tool_call" => {
if let Ok(json) = serde_json::from_str::<serde_json::Value>(&data) {
let id = json.get("id").and_then(|v| v.as_str()).unwrap_or("").to_string();
let name = json.get("name").and_then(|v| v.as_str()).unwrap_or("").to_string();
let input = json.get("input").cloned().unwrap_or(serde_json::json!({}));
yield Ok(StreamFrame::Content(StreamContent::ToolCall { id, name, input }));
}
}
"tool_result" => {
if let Ok(json) = serde_json::from_str::<serde_json::Value>(&data) {
let tool_use_id = json.get("tool_use_id").and_then(|v| v.as_str()).unwrap_or("").to_string();
let content = json.get("content").and_then(|v| v.as_str()).unwrap_or("").to_string();
let is_error = json.get("is_error").and_then(|v| v.as_bool()).unwrap_or(false);
let remote = json.get("remote").and_then(|v| v.as_bool()).unwrap_or(false);
let content_length = json.get("content_length").and_then(|v| v.as_u64()).map(|v| v as usize);
yield Ok(StreamFrame::Content(StreamContent::ToolResult { tool_use_id, content, is_error, remote, content_length }));
}
}
"status" => {
if let Ok(json) = serde_json::from_str::<serde_json::Value>(&data)
&& let Some(state) = json.get("state").and_then(|v| v.as_str())
{
yield Ok(StreamFrame::Control(StreamControl::StatusChanged(state.to_string())));
}
}
"done" => {
if let Ok(json) = serde_json::from_str::<serde_json::Value>(&data) {
let session_id = json.get("session_id")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
yield Ok(StreamFrame::Control(StreamControl::Done { session_id }));
} else {
yield Ok(StreamFrame::Control(StreamControl::Done { session_id: String::new() }));
}
break;
}
"error" => {
if let Ok(json) = serde_json::from_str::<serde_json::Value>(&data) {
let message = json.get("message").and_then(|v| v.as_str()).unwrap_or("Unknown error").to_string();
tracing::error!("SSE error: {}", message);
yield Ok(StreamFrame::Control(StreamControl::Error(message)));
} else {
tracing::error!("SSE error: {}", data);
yield Ok(StreamFrame::Control(StreamControl::Error(data)));
}
break;
}
_ => {}
}
}
Err(e) => {
yield Err(eyre::eyre!("SSE error: {}", e));
break;
}
}
}
})
}
// ───────────────────────────────────────────────────────────────────
// Async streaming task — pushes updates to app state via Handle
// ───────────────────────────────────────────────────────────────────
pub(crate) async fn run_chat_stream(
handle: Handle<Session>,
tx: mpsc::Sender<AiTuiEvent>,
app_ctx: AppContext,
client_ctx: ClientContext,
request: ChatRequest,
) {
let capabilities = request.capabilities.clone();
let stream = create_chat_stream(
app_ctx.endpoint.clone(),
app_ctx.token.clone(),
request,
client_ctx,
app_ctx.send_cwd,
app_ctx.last_command.clone(),
);
futures::pin_mut!(stream);
while let Some(event) = stream.next().await {
match event {
Ok(StreamFrame::Content(content)) => {
apply_content_frame(&handle, &tx, &capabilities, content);
}
Ok(StreamFrame::Control(control)) => {
let terminal = apply_control_frame(&handle, control);
if terminal {
break;
}
}
Err(e) => {
let msg = e.to_string();
handle.update(move |state| {
state.streaming_error(msg);
});
break;
}
}
}
}
/// Apply a content frame to session state.
/// Control flow: always continues the stream.
fn apply_content_frame(
handle: &Handle<Session>,
tx: &mpsc::Sender<AiTuiEvent>,
capabilities: &[String],
content: StreamContent,
) {
match content {
StreamContent::TextChunk(text) => {
handle.update(move |state| {
state.conversation.append_streaming_text(&text);
});
}
StreamContent::ToolCall { id, name, input } => {
if let Ok(tool) = ClientToolCall::try_from((name.as_str(), &input)) {
// Enforce capability gating: reject tool calls the client didn't advertise.
if let Some(required_cap) = tool.descriptor().capability
&& !capabilities.iter().any(|c| c == required_cap)
{
tracing::warn!(
tool = name,
capability = required_cap,
"Rejecting tool call: capability not advertised"
);
handle.update(move |state| {
state.add_tool_call(id.clone(), name, input.clone());
state.conversation.add_tool_result(
id,
format!("Tool not enabled: capability '{required_cap}' was not advertised by this client"),
true,
false,
None,
);
});
return;
}
// Client-side tool — add to tracker and conversation, queue permission check
let id_for_event = id.clone();
handle.update(move |state| {
state.handle_client_tool_call(id_for_event, tool, input);
});
let _ = tx.send(AiTuiEvent::CheckToolCallPermission(id));
} else {
// Server-side tool — just add to conversation events
handle.update(move |state| {
state.add_tool_call(id, name, input);
});
}
}
StreamContent::ToolResult {
tool_use_id,
content,
is_error,
remote,
content_length,
} => {
handle.update(move |state| {
state.conversation.add_tool_result(
tool_use_id,
content,
is_error,
remote,
content_length,
);
});
}
}
}
/// Apply a control frame to session state.
/// Returns true if the stream should terminate.
fn apply_control_frame(handle: &Handle<Session>, control: StreamControl) -> bool {
match control {
StreamControl::StatusChanged(status) => {
handle.update(move |state| {
state.update_streaming_status(&status);
});
false
}
StreamControl::Done { session_id } => {
handle.update(move |state| {
if !session_id.is_empty() {
state.conversation.store_session_id(session_id);
}
state.finalize_streaming();
});
true
}
StreamControl::Error(msg) => {
handle.update(move |state| {
state.streaming_error(msg);
});
true
}
}
}
fn hub_url(base: &str, path: &str) -> Result<Url> {
let base_with_slash = if base.ends_with('/') {
base.to_string()
} else {
format!("{base}/")
};
let stripped = path.strip_prefix('/').unwrap_or(path);
Url::parse(&base_with_slash)?
.join(stripped)
.context("failed to build hub URL")
}
|