Skip to main content

codex_external_agent_migration/
model.rs

1use crate::sessions::ExternalAgentSessionMigration;
2use std::path::PathBuf;
3use std::time::Duration;
4
5const DEFAULT_SESSION_IMPORT_MAX_AGE: Duration = Duration::from_secs(30 * 24 * 60 * 60);
6const DEFAULT_SESSION_IMPORT_MAX_COUNT: usize = 50;
7
8/// Bounds session discovery for an external-agent import.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub struct ExternalAgentSessionImportLimits {
11    /// Oldest source-session modification age that remains eligible.
12    pub max_age: Duration,
13    /// Maximum number of eligible sessions returned by detection.
14    pub max_sessions: usize,
15}
16
17impl Default for ExternalAgentSessionImportLimits {
18    fn default() -> Self {
19        Self {
20            max_age: DEFAULT_SESSION_IMPORT_MAX_AGE,
21            max_sessions: DEFAULT_SESSION_IMPORT_MAX_COUNT,
22        }
23    }
24}
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct ExternalAgentConfigDetectOptions {
28    pub include_home: bool,
29    pub include_memory: bool,
30    pub cwds: Option<Vec<PathBuf>>,
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub enum ExternalAgentConfigMigrationItemType {
35    Config,
36    Skills,
37    AgentsMd,
38    Plugins,
39    McpServerConfig,
40    Subagents,
41    Hooks,
42    Commands,
43    Memory,
44    Sessions,
45}
46
47#[derive(Debug, Clone, PartialEq, Eq)]
48pub struct PluginsMigration {
49    pub marketplace_name: String,
50    pub plugin_names: Vec<String>,
51}
52
53#[derive(Debug, Clone, PartialEq, Eq)]
54pub struct NamedMigration {
55    pub name: String,
56}
57
58#[derive(Debug, Clone, Default, PartialEq, Eq)]
59pub struct MigrationDetails {
60    pub plugins: Vec<PluginsMigration>,
61    pub skills: Vec<NamedMigration>,
62    pub sessions: Vec<ExternalAgentSessionMigration>,
63    pub mcp_servers: Vec<NamedMigration>,
64    pub hooks: Vec<NamedMigration>,
65    pub subagents: Vec<NamedMigration>,
66    pub commands: Vec<NamedMigration>,
67    pub memory: Vec<String>,
68}
69
70#[derive(Debug, Clone, PartialEq, Eq)]
71pub struct PendingPluginImport {
72    pub cwd: Option<PathBuf>,
73    pub description: String,
74    pub details: MigrationDetails,
75}
76
77#[derive(Debug, Clone, Default, PartialEq, Eq)]
78pub struct PluginImportOutcome {
79    pub succeeded_marketplaces: Vec<String>,
80    pub succeeded_plugin_ids: Vec<String>,
81    pub failed_marketplaces: Vec<String>,
82    pub failed_plugin_ids: Vec<String>,
83    pub raw_errors: Vec<ExternalAgentConfigImportRawError>,
84}
85
86#[derive(Debug, Clone, Default, PartialEq, Eq)]
87pub struct ExternalAgentConfigImportOutcome {
88    pub pending_plugin_imports: Vec<PendingPluginImport>,
89    pub item_results: Vec<ExternalAgentConfigImportItemResult>,
90}
91
92#[derive(Debug, Clone, PartialEq, Eq)]
93pub struct ExternalAgentConfigImportItemResult {
94    pub item_type: ExternalAgentConfigMigrationItemType,
95    pub description: String,
96    pub cwd: Option<PathBuf>,
97    pub success_count: u32,
98    pub error_count: u32,
99    pub successes: Vec<ExternalAgentConfigImportSuccess>,
100    pub raw_errors: Vec<ExternalAgentConfigImportRawError>,
101}
102
103impl ExternalAgentConfigImportItemResult {
104    pub fn new(
105        item_type: ExternalAgentConfigMigrationItemType,
106        description: String,
107        cwd: Option<PathBuf>,
108    ) -> Self {
109        Self {
110            item_type,
111            description,
112            cwd,
113            success_count: 0,
114            error_count: 0,
115            successes: Vec::new(),
116            raw_errors: Vec::new(),
117        }
118    }
119
120    pub fn record_error(&mut self, raw_error: ExternalAgentConfigImportRawError) {
121        self.error_count = self.error_count.saturating_add(1);
122        self.raw_errors.push(raw_error);
123    }
124
125    pub fn record_success(&mut self, source: Option<String>, target: Option<String>) {
126        self.success_count = self.success_count.saturating_add(1);
127        self.successes.push(ExternalAgentConfigImportSuccess {
128            item_type: self.item_type,
129            cwd: self.cwd.clone(),
130            source,
131            target,
132        });
133    }
134}
135
136#[derive(Debug, Clone, PartialEq, Eq)]
137pub struct ExternalAgentConfigImportSuccess {
138    pub item_type: ExternalAgentConfigMigrationItemType,
139    pub cwd: Option<PathBuf>,
140    pub source: Option<String>,
141    pub target: Option<String>,
142}
143
144#[derive(Debug, Clone, PartialEq, Eq)]
145pub struct ExternalAgentConfigImportRawError {
146    pub item_type: ExternalAgentConfigMigrationItemType,
147    pub error_type: Option<String>,
148    pub sub_error_type: Option<String>,
149    pub failure_stage: String,
150    pub message: String,
151    pub cwd: Option<PathBuf>,
152    pub source: Option<String>,
153}
154
155#[derive(Debug, Clone, PartialEq, Eq)]
156pub struct ExternalAgentConfigMigrationItem {
157    pub item_type: ExternalAgentConfigMigrationItemType,
158    pub description: String,
159    pub cwd: Option<PathBuf>,
160    pub details: Option<MigrationDetails>,
161}