agentmux_srv/server/app_api/bundle/
export.rs1use super::*;
7
8#[derive(serde::Deserialize, Default, ts_rs::TS)]
19#[ts(export, export_to = "../../../frontend/types/rpc/", rename = "BundleExportReq")]
20pub(super) struct ExportReq {
21 pub(crate) id: String,
22 #[serde(default)]
23 #[ts(as = "Option<String>", optional)]
24 pub(crate) format: String,
25 #[serde(default)]
28 #[ts(as = "Option<Vec<String>>", optional)]
29 pub(crate) widgets: Vec<String>,
30}
31
32#[derive(Debug, Clone, serde::Serialize, ts_rs::TS)]
35#[ts(export, export_to = "../../../frontend/types/rpc/")]
36pub(crate) struct BundleExportResponse {
37 pub(crate) root_slug: String,
38 #[serde(skip_serializing_if = "Option::is_none")]
40 #[ts(optional)]
41 pub(crate) files: Option<Vec<crate::backend::bundle_export::BundleExportFile>>,
42 pub(crate) skipped_skills: Vec<String>,
43 pub(crate) warnings: Vec<String>,
44 pub(crate) missing_skill_ids: Vec<String>,
45 #[serde(skip_serializing_if = "Option::is_none")]
47 #[ts(optional)]
48 pub(crate) zip_base64: Option<String>,
49}
50
51pub(super) fn register_bundle_export(engine: &Arc<WshRpcEngine>, state: &AppState) {
52 let id_store = state.id_store.clone();
53 let mstore = state.mstore.clone();
54 let identity_store = state.identity_store.clone();
55 engine.register_typed(
56 COMMAND_BUNDLE_EXPORT,
57 move |req: ExportReq, _ctx| {
58 let id_store = id_store.clone();
59 let mstore = mstore.clone();
60 let identity_store = identity_store.clone();
61 async move {
62 crate::backend::bundle_use::note("export");
63 bundle_export_impl(&id_store, &mstore, &identity_store, req)
64 }
65 },
66 );
67}
68
69pub(super) fn bundle_export_impl(
75 id_store: &crate::backend::storage::store::Store,
76 mstore: &crate::backend::storage::store::Store,
77 identity_store: &crate::backend::storage::store::Store,
78 req: ExportReq,
79) -> Result<BundleExportResponse, String> {
80 let bundle = id_store
81 .bundle_get(&req.id)
82 .map_err(|e| format!("bundle.export: {e}"))?
83 .ok_or_else(|| format!("bundle.export: no bundle with id {}", req.id))?;
84
85 let components = resolve_bundle_components(mstore, identity_store, &bundle.id)
88 .map_err(|e| format!("bundle.export: {e}"))?;
89
90 let missing_skill_ids: Vec<String> = Vec::new();
97
98 let export = crate::backend::bundle_export::export_bundle(&bundle, &components.skills);
99
100 let mut all_warnings = export.warnings.clone();
101
102 if bound_agent_has_native_memory(id_store, mstore, &bundle.id) {
104 all_warnings.push(MEMORY_NOT_EXPORTED_WARNING.to_string());
105 }
106
107 if !req.widgets.is_empty() && req.format != "zip" {
108 return Err("bundle.export: widgets travel only in a zip (format: \"zip\")".to_string());
109 }
110 if req.format == "zip" {
111 let zip_bytes = if req.widgets.is_empty() {
112 crate::backend::bundle_export::zip_bundle_export(&export)
113 } else {
114 let svc = crate::backend::widget::packages::service().ok_or("bundle.export: widget packages aren't available")?;
115 let mut widgets = Vec::new();
116 for id in &req.widgets {
117 let (hash, files) = svc.approved_package(id).map_err(|e| format!("bundle.export: {e}"))?;
118 widgets.push((id.clone(), hash, files));
119 }
120 crate::backend::bundle_widgets::zip_export_with_widgets(&export, &widgets)
121 }
122 .map_err(|e| format!("bundle.export: {e}"))?;
123 use base64::Engine as _;
124 let encoded = base64::engine::general_purpose::STANDARD.encode(&zip_bytes);
125 return Ok(BundleExportResponse {
126 root_slug: export.root_slug,
127 files: None,
128 skipped_skills: export.skipped_skills,
129 warnings: all_warnings,
130 missing_skill_ids,
131 zip_base64: Some(encoded),
132 });
133 }
134
135 Ok(BundleExportResponse {
136 root_slug: export.root_slug,
137 files: Some(export.files),
138 skipped_skills: export.skipped_skills,
139 warnings: all_warnings,
140 missing_skill_ids,
141 zip_base64: None,
142 })
143}