|
| 1 | +use crate::error::Result; |
| 2 | +use crate::types::requests::{ |
| 3 | + AddExtensionRequest, RemoveExtensionRequest, RestartAgentRequest, ResumeAgentRequest, |
| 4 | + StartAgentRequest, StopAgentRequest, UpdateFromSessionRequest, UpdateProviderRequest, |
| 5 | + UpdateWorkingDirRequest, |
| 6 | +}; |
| 7 | +use crate::types::responses::{RestartAgentResponse, ResumeAgentResponse}; |
| 8 | +use crate::GooseClient; |
| 9 | +use goose::agents::ExtensionConfig; |
| 10 | +use goose::session::Session; |
| 11 | + |
| 12 | +impl GooseClient { |
| 13 | + /// Start a new agent session and return the created `Session`. |
| 14 | + pub async fn start_agent(&self, request: StartAgentRequest) -> Result<Session> { |
| 15 | + self.http.post("/agent/start", &request).await |
| 16 | + } |
| 17 | + |
| 18 | + /// Resume an existing session, optionally reloading the model and extensions. |
| 19 | + pub async fn resume_agent( |
| 20 | + &self, |
| 21 | + session_id: impl Into<String>, |
| 22 | + load_model_and_extensions: bool, |
| 23 | + ) -> Result<ResumeAgentResponse> { |
| 24 | + self.http |
| 25 | + .post( |
| 26 | + "/agent/resume", |
| 27 | + &ResumeAgentRequest { |
| 28 | + session_id: session_id.into(), |
| 29 | + load_model_and_extensions, |
| 30 | + }, |
| 31 | + ) |
| 32 | + .await |
| 33 | + } |
| 34 | + |
| 35 | + /// Stop and remove the agent for the given session. |
| 36 | + pub async fn stop_agent(&self, session_id: impl Into<String>) -> Result<()> { |
| 37 | + self.http |
| 38 | + .post_empty( |
| 39 | + "/agent/stop", |
| 40 | + &StopAgentRequest { |
| 41 | + session_id: session_id.into(), |
| 42 | + }, |
| 43 | + ) |
| 44 | + .await |
| 45 | + } |
| 46 | + |
| 47 | + /// Restart the agent for the given session (reloads extensions and provider). |
| 48 | + pub async fn restart_agent( |
| 49 | + &self, |
| 50 | + session_id: impl Into<String>, |
| 51 | + ) -> Result<RestartAgentResponse> { |
| 52 | + self.http |
| 53 | + .post( |
| 54 | + "/agent/restart", |
| 55 | + &RestartAgentRequest { |
| 56 | + session_id: session_id.into(), |
| 57 | + }, |
| 58 | + ) |
| 59 | + .await |
| 60 | + } |
| 61 | + |
| 62 | + /// Change the working directory for an existing session. The agent is restarted. |
| 63 | + pub async fn update_working_dir( |
| 64 | + &self, |
| 65 | + session_id: impl Into<String>, |
| 66 | + working_dir: impl Into<String>, |
| 67 | + ) -> Result<()> { |
| 68 | + self.http |
| 69 | + .post_empty( |
| 70 | + "/agent/update_working_dir", |
| 71 | + &UpdateWorkingDirRequest { |
| 72 | + session_id: session_id.into(), |
| 73 | + working_dir: working_dir.into(), |
| 74 | + }, |
| 75 | + ) |
| 76 | + .await |
| 77 | + } |
| 78 | + |
| 79 | + /// Apply the session's recipe (system prompt, extensions) to the running agent. |
| 80 | + pub async fn update_from_session(&self, session_id: impl Into<String>) -> Result<()> { |
| 81 | + self.http |
| 82 | + .post_empty( |
| 83 | + "/agent/update_from_session", |
| 84 | + &UpdateFromSessionRequest { |
| 85 | + session_id: session_id.into(), |
| 86 | + }, |
| 87 | + ) |
| 88 | + .await |
| 89 | + } |
| 90 | + |
| 91 | + /// Change the LLM provider and/or model for an existing session. |
| 92 | + pub async fn update_provider(&self, request: UpdateProviderRequest) -> Result<()> { |
| 93 | + self.http |
| 94 | + .post_empty("/agent/update_provider", &request) |
| 95 | + .await |
| 96 | + } |
| 97 | + |
| 98 | + /// Add an MCP extension to a running session. |
| 99 | + pub async fn add_extension( |
| 100 | + &self, |
| 101 | + session_id: impl Into<String>, |
| 102 | + config: ExtensionConfig, |
| 103 | + ) -> Result<()> { |
| 104 | + self.http |
| 105 | + .post_empty( |
| 106 | + "/agent/add_extension", |
| 107 | + &AddExtensionRequest { |
| 108 | + session_id: session_id.into(), |
| 109 | + config, |
| 110 | + }, |
| 111 | + ) |
| 112 | + .await |
| 113 | + } |
| 114 | + |
| 115 | + /// Remove an MCP extension from a running session by name. |
| 116 | + pub async fn remove_extension( |
| 117 | + &self, |
| 118 | + session_id: impl Into<String>, |
| 119 | + name: impl Into<String>, |
| 120 | + ) -> Result<()> { |
| 121 | + self.http |
| 122 | + .post_empty( |
| 123 | + "/agent/remove_extension", |
| 124 | + &RemoveExtensionRequest { |
| 125 | + session_id: session_id.into(), |
| 126 | + name: name.into(), |
| 127 | + }, |
| 128 | + ) |
| 129 | + .await |
| 130 | + } |
| 131 | +} |
0 commit comments