Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/cli/src/commands/agent_stop.rs
13383 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
use ahp_types::actions::{SessionTurnCancelledAction, StateAction};
7
use ahp_types::commands::{SubscribeParams, SubscribeResult};
8
use ahp_types::state::SnapshotState;
9
10
use crate::log;
11
use crate::util::errors::{wrap, AnyError};
12
13
use super::agent;
14
use super::args::AgentStopArgs;
15
use super::CommandContext;
16
17
/// Cancels the active turn of a session on a running agent host.
18
pub async fn agent_stop(ctx: CommandContext, args: AgentStopArgs) -> Result<i32, AnyError> {
19
let client = agent::connect(&ctx, args.address.as_deref(), args.tunnel.as_deref()).await?;
20
21
// Subscribe to the session to get its current state.
22
let result: SubscribeResult = agent::request_with_auth(
23
&ctx,
24
&client,
25
"subscribe",
26
SubscribeParams {
27
resource: args.session.clone(),
28
},
29
)
30
.await?;
31
32
let turn_id = match result.snapshot.state {
33
SnapshotState::Session(session) => session.active_turn.map(|t| t.id),
34
_ => None,
35
};
36
37
let turn_id = match turn_id {
38
Some(id) => id,
39
None => {
40
ctx.log.result("No active turn to cancel.");
41
client.shutdown().await;
42
return Ok(0);
43
}
44
};
45
46
debug!(ctx.log, "Cancelling turn {} on {}", turn_id, args.session);
47
48
client
49
.dispatch(StateAction::SessionTurnCancelled(
50
SessionTurnCancelledAction {
51
session: args.session.clone(),
52
turn_id: turn_id.clone(),
53
},
54
))
55
.await
56
.map_err(|e| wrap(e, "Failed to dispatch turn cancellation"))?;
57
58
ctx.log
59
.result(format!("Cancelled turn {turn_id} on {}", args.session));
60
61
client.shutdown().await;
62
Ok(0)
63
}
64
65