Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 77 additions & 8 deletions src/acp_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use tokio_tungstenite::{connect_async, tungstenite::Message};

use crate::ServerChannelMsg;
use crate::acp_state::{AcpAppEvent, AcpModelsMetaInfo, AcpSessionUpdate};
use crate::domain::auth::{OAuthFlow, OAuthResult, OAuthResultStatus};
use crate::domain::model::ModelEntry;
use crate::domain::profile::{AgentInfo, ProfileInfo};
use crate::domain::session::{
Expand All @@ -27,7 +28,7 @@ use crate::domain::session::{
};
use crate::protocol::{
AuthProvidersData, ClientMsg, MeshInviteCreatedInfo, MeshNodesInfo, MeshStatusInfo,
OAuthFlowData, OAuthResultData, RedoResultData, RemoteSessionAttachInfo, RemoteSessionListInfo,
OAuthFlowDto, OAuthResultDto, RedoResultData, RemoteSessionAttachInfo, RemoteSessionListInfo,
SessionListRequest, UndoResultData, UndoStackFrame,
};

Expand Down Expand Up @@ -1220,10 +1221,12 @@ async fn handle_client_msg<C: AcpConnection>(
json!({ "provider": provider }),
)
.await?;
if let Ok(flow) =
serde_json::from_value::<OAuthFlowData>(ext_payload(&response).clone())
if let Ok(flow) = serde_json::from_value::<OAuthFlowDto>(ext_payload(&response).clone())
{
send_acp(srv_tx, AcpAppEvent::OAuthFlowStarted(flow));
send_acp(
srv_tx,
AcpAppEvent::OAuthFlowStarted(oauth_flow_from_wire(flow)),
);
}
}
ClientMsg::CompleteOAuthLogin { flow_id, response } => {
Expand All @@ -1234,9 +1237,12 @@ async fn handle_client_msg<C: AcpConnection>(
)
.await?;
if let Ok(result) =
serde_json::from_value::<OAuthResultData>(ext_payload(&response).clone())
serde_json::from_value::<OAuthResultDto>(ext_payload(&response).clone())
{
send_acp(srv_tx, AcpAppEvent::OAuthResult(result));
send_acp(
srv_tx,
AcpAppEvent::OAuthResult(oauth_result_from_wire(result)),
);
}
}
ClientMsg::DisconnectOAuth { provider } => {
Expand All @@ -1247,9 +1253,12 @@ async fn handle_client_msg<C: AcpConnection>(
)
.await?;
if let Ok(result) =
serde_json::from_value::<OAuthResultData>(ext_payload(&response).clone())
serde_json::from_value::<OAuthResultDto>(ext_payload(&response).clone())
{
send_acp(srv_tx, AcpAppEvent::OAuthResult(result));
send_acp(
srv_tx,
AcpAppEvent::OAuthResult(oauth_result_from_wire(result)),
);
}
}
ClientMsg::ElicitationResponse {
Expand Down Expand Up @@ -1518,6 +1527,27 @@ fn load_session_cwd(cwd: Option<&str>, default_cwd: PathBuf) -> PathBuf {
.unwrap_or(default_cwd)
}

fn oauth_flow_from_wire(flow: OAuthFlowDto) -> OAuthFlow {
OAuthFlow {
flow_id: flow.flow_id,
provider: flow.provider,
authorization_url: flow.authorization_url,
flow_kind: flow.flow_kind,
}
}

fn oauth_result_from_wire(result: OAuthResultDto) -> OAuthResult {
OAuthResult {
provider: result.provider,
status: if result.success {
OAuthResultStatus::Success
} else {
OAuthResultStatus::Failure
},
message: result.message,
}
}

fn undo_stack_snapshot_from_wire(frames: Vec<UndoStackFrame>) -> UndoStackSnapshot {
UndoStackSnapshot {
message_ids: frames.into_iter().map(|frame| frame.message_id).collect(),
Expand Down Expand Up @@ -2884,6 +2914,45 @@ mod tests {
.collect()
}

#[test]
fn oauth_flow_from_wire_preserves_semantic_fields() {
let flow = oauth_flow_from_wire(OAuthFlowDto {
flow_id: "flow-123".into(),
provider: "openai".into(),
authorization_url: "https://auth.example.com/authorize".into(),
flow_kind: crate::domain::auth::OAuthFlowKind::DevicePoll,
});

assert_eq!(flow.flow_id, "flow-123");
assert_eq!(flow.provider, "openai");
assert_eq!(flow.authorization_url, "https://auth.example.com/authorize");
assert_eq!(
flow.flow_kind,
crate::domain::auth::OAuthFlowKind::DevicePoll
);
}

#[test]
fn oauth_result_from_wire_maps_status_and_preserves_provider() {
let success = oauth_result_from_wire(OAuthResultDto {
provider: "openai".into(),
success: true,
message: "connected".into(),
});
assert_eq!(success.provider, "openai");
assert_eq!(success.status, OAuthResultStatus::Success);
assert_eq!(success.message, "connected");

let failure = oauth_result_from_wire(OAuthResultDto {
provider: "anthropic".into(),
success: false,
message: "authorization denied".into(),
});
assert_eq!(failure.provider, "anthropic");
assert_eq!(failure.status, OAuthResultStatus::Failure);
assert_eq!(failure.message, "authorization denied");
}

#[test]
fn undo_stack_snapshot_from_wire_preserves_message_order() {
let snapshot = undo_stack_snapshot_from_wire(wire_stack(&["message-2", "message-1"]));
Expand Down
22 changes: 13 additions & 9 deletions src/acp_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::app::{LogLevel, POPUP_SESSION_PAGE_TARGET, Popup, Screen};
use crate::domain::activity::{
ActivityState, DelegateChildState, DelegateEntry, DelegateStats, DelegateStatus,
};
use crate::domain::auth::AuthProviderEntry;
use crate::domain::auth::{AuthProviderEntry, OAuthFlow, OAuthResult};
use crate::domain::chat::ChatEntry;
use crate::domain::elicitation::ElicitationState;
use crate::domain::model::ModelEntry;
Expand All @@ -20,8 +20,8 @@ use crate::domain::session::{
};
use crate::domain::tool::ToolDetail;
use crate::protocol::{
ClientMsg, MeshInviteCreatedInfo, MeshNodesInfo, MeshStatusInfo, OAuthFlowData,
OAuthResultData, RemoteSessionAttachInfo, RemoteSessionListInfo, SessionListRequest,
ClientMsg, MeshInviteCreatedInfo, MeshNodesInfo, MeshStatusInfo, RemoteSessionAttachInfo,
RemoteSessionListInfo, SessionListRequest,
};
use crate::tool_detail;

Expand Down Expand Up @@ -166,8 +166,8 @@ pub(crate) enum AcpAppEvent {
RedoResult(RedoResult),
ForkResult(ForkResult),
AuthProviders(Vec<AuthProviderEntry>),
OAuthFlowStarted(OAuthFlowData),
OAuthResult(OAuthResultData),
OAuthFlowStarted(OAuthFlow),
OAuthResult(OAuthResult),
InfoLog {
target: &'static str,
message: String,
Expand Down Expand Up @@ -214,6 +214,7 @@ impl crate::app::App {
if let Some(effort) = reasoning_effort {
self.reasoning_effort = effort;
}
self.auth_ui_notice = None;
self.set_status(LogLevel::Info, "connection", "connected");
vec![]
}
Expand Down Expand Up @@ -530,18 +531,21 @@ impl crate::app::App {
self.auth_panel = crate::app::AuthPanel::OAuthFlow;
self.auth_oauth_response.clear();
self.auth_oauth_response_cursor = 0;
self.auth_result_message = None;
self.auth_last_result = None;
self.auth_ui_notice = None;
vec![]
}
AcpAppEvent::OAuthResult(result) => {
let level = if result.success {
let is_success = result.is_success();
let level = if is_success {
LogLevel::Info
} else {
LogLevel::Warn
};
self.push_log(level, "auth", &result.message);
self.auth_result_message = Some((result.success, result.message));
if result.success {
self.auth_ui_notice = None;
self.auth_last_result = Some(result);
if is_success {
self.auth_oauth_flow = None;
self.auth_panel = crate::app::AuthPanel::List;
}
Expand Down
39 changes: 33 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::domain::activity::{
ActivityState, DelegateChildState, DelegateEntry, DelegateStats, PendingDelegateToolCall,
SessionActivity, SessionOp, SessionStatsLite,
};
use crate::domain::auth::AuthProviderEntry;
use crate::domain::auth::{AuthProviderEntry, OAuthFlow, OAuthResult};
use crate::domain::chat::{ChatEntry, format_outcome_labels};
use crate::domain::elicitation::ElicitationState;
use crate::domain::model::{DelegateModelPreference, ModelEntry};
Expand Down Expand Up @@ -537,6 +537,13 @@ pub enum ModelPopupItem {
},
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AuthUiNotice {
pub provider: Option<String>,
pub success: bool,
pub message: String,
}

/// Which sub-panel is active in the provider auth popup.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum AuthPanel {
Expand Down Expand Up @@ -740,10 +747,11 @@ pub struct App {
pub auth_api_key_input: String,
pub auth_api_key_cursor: usize,
pub auth_api_key_masked: bool,
pub auth_oauth_flow: Option<crate::protocol::OAuthFlowData>,
pub auth_oauth_flow: Option<OAuthFlow>,
pub auth_oauth_response: String,
pub auth_oauth_response_cursor: usize,
pub auth_result_message: Option<(bool, String)>,
pub auth_last_result: Option<OAuthResult>,
pub auth_ui_notice: Option<AuthUiNotice>,
/// When clipboard copy fails, store the URL here for a fallback display popup.
pub auth_clipboard_fallback: Option<String>,

Expand Down Expand Up @@ -968,7 +976,8 @@ impl App {
auth_oauth_flow: None,
auth_oauth_response: String::new(),
auth_oauth_response_cursor: 0,
auth_result_message: None,
auth_last_result: None,
auth_ui_notice: None,
auth_clipboard_fallback: None,
delegate_entries: Vec::new(),
delegate_cursor: 0,
Expand Down Expand Up @@ -1136,7 +1145,8 @@ impl App {
self.auth_oauth_flow = None;
self.auth_oauth_response.clear();
self.auth_oauth_response_cursor = 0;
self.auth_result_message = None;
self.auth_last_result = None;
self.auth_ui_notice = None;
self.auth_clipboard_fallback = None;
}

Expand All @@ -1149,10 +1159,27 @@ impl App {
self.auth_oauth_flow = None;
self.auth_oauth_response.clear();
self.auth_oauth_response_cursor = 0;
self.auth_result_message = None;
self.auth_last_result = None;
self.auth_ui_notice = None;
self.auth_clipboard_fallback = None;
}

pub fn auth_feedback_for_provider(&self, provider: &str) -> Option<(bool, &str)> {
if let Some(notice) = self.auth_ui_notice.as_ref().filter(|notice| {
notice
.provider
.as_deref()
.is_none_or(|notice_provider| notice_provider == provider)
}) {
return Some((notice.success, notice.message.as_str()));
}

self.auth_last_result
.as_ref()
.filter(|result| result.provider == provider)
.map(|result| (result.is_success(), result.message.as_str()))
}

pub fn profile_by_id(&self, profile_id: &str) -> Option<&ProfileInfo> {
self.profiles
.iter()
Expand Down
40 changes: 40 additions & 0 deletions src/domain/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,33 @@ pub enum OAuthFlowKind {
DevicePoll,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OAuthFlow {
pub flow_id: String,
pub provider: String,
pub authorization_url: String,
pub flow_kind: OAuthFlowKind,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OAuthResultStatus {
Success,
Failure,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OAuthResult {
pub provider: String,
pub status: OAuthResultStatus,
pub message: String,
}

impl OAuthResult {
pub fn is_success(&self) -> bool {
matches!(self.status, OAuthResultStatus::Success)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -300,6 +327,19 @@ mod tests {
);
}

#[test]
fn oauth_result_reports_success_from_semantic_status() {
let mut result = OAuthResult {
provider: "openai".into(),
status: OAuthResultStatus::Success,
message: "connected".into(),
};
assert!(result.is_success());

result.status = OAuthResultStatus::Failure;
assert!(!result.is_success());
}

#[test]
fn auth_method_display_labels_match_ui_contract() {
assert_eq!(AuthMethod::OAuth.to_string(), "OAuth");
Expand Down
Loading