agcodex_core/
environment_context.rs

1use serde::Deserialize;
2use serde::Serialize;
3use strum_macros::Display as DeriveDisplay;
4
5use crate::models::ContentItem;
6use crate::models::ResponseItem;
7use crate::protocol::AskForApproval;
8use crate::protocol::SandboxPolicy;
9use agcodex_protocol::config_types::SandboxMode;
10use std::fmt::Display;
11use std::path::PathBuf;
12
13/// wraps environment context message in a tag for the model to parse more easily.
14pub(crate) const ENVIRONMENT_CONTEXT_START: &str = "<environment_context>\n";
15pub(crate) const ENVIRONMENT_CONTEXT_END: &str = "</environment_context>";
16
17#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, DeriveDisplay)]
18#[serde(rename_all = "kebab-case")]
19#[strum(serialize_all = "kebab-case")]
20pub enum NetworkAccess {
21    Restricted,
22    Enabled,
23}
24#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
25#[serde(rename = "environment_context", rename_all = "snake_case")]
26pub(crate) struct EnvironmentContext {
27    pub cwd: PathBuf,
28    pub approval_policy: AskForApproval,
29    pub sandbox_mode: SandboxMode,
30    pub network_access: NetworkAccess,
31}
32
33impl EnvironmentContext {
34    pub fn new(
35        cwd: PathBuf,
36        approval_policy: AskForApproval,
37        sandbox_policy: SandboxPolicy,
38    ) -> Self {
39        Self {
40            cwd,
41            approval_policy,
42            sandbox_mode: match sandbox_policy {
43                SandboxPolicy::DangerFullAccess => SandboxMode::DangerFullAccess,
44                SandboxPolicy::ReadOnly => SandboxMode::ReadOnly,
45                SandboxPolicy::WorkspaceWrite { .. } => SandboxMode::WorkspaceWrite,
46            },
47            network_access: match sandbox_policy {
48                SandboxPolicy::DangerFullAccess => NetworkAccess::Enabled,
49                SandboxPolicy::ReadOnly => NetworkAccess::Restricted,
50                SandboxPolicy::WorkspaceWrite { network_access, .. } => {
51                    if network_access {
52                        NetworkAccess::Enabled
53                    } else {
54                        NetworkAccess::Restricted
55                    }
56                }
57            },
58        }
59    }
60}
61
62impl Display for EnvironmentContext {
63    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        writeln!(
65            f,
66            "Current working directory: {}",
67            self.cwd.to_string_lossy()
68        )?;
69        writeln!(f, "Approval policy: {}", self.approval_policy)?;
70        writeln!(f, "Sandbox mode: {}", self.sandbox_mode)?;
71        writeln!(f, "Network access: {}", self.network_access)?;
72        Ok(())
73    }
74}
75
76impl From<EnvironmentContext> for ResponseItem {
77    fn from(ec: EnvironmentContext) -> Self {
78        ResponseItem::Message {
79            id: None,
80            role: "user".to_string(),
81            content: vec![ContentItem::InputText {
82                text: format!("{ENVIRONMENT_CONTEXT_START}{ec}{ENVIRONMENT_CONTEXT_END}"),
83            }],
84        }
85    }
86}