mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-04-06 21:08:30 +08:00
Add a dedicated panel to read/write OpenClaw's 6 workspace bootstrap files (AGENTS.md, SOUL.md, USER.md, IDENTITY.md, TOOLS.md, MEMORY.md) directly from ~/.openclaw/workspace/, with a whitelist-secured backend and Markdown editor UI. Also fix prompt auto-import missing OpenCode/OpenClaw and the PromptFormPanel filenameMap type exclusion.
60 lines
1.7 KiB
Rust
60 lines
1.7 KiB
Rust
use crate::config::write_text_file;
|
|
use crate::openclaw_config::get_openclaw_dir;
|
|
|
|
/// Allowed workspace filenames (whitelist for security)
|
|
const ALLOWED_FILES: &[&str] = &[
|
|
"AGENTS.md",
|
|
"SOUL.md",
|
|
"USER.md",
|
|
"IDENTITY.md",
|
|
"TOOLS.md",
|
|
"MEMORY.md",
|
|
];
|
|
|
|
fn validate_filename(filename: &str) -> Result<(), String> {
|
|
if !ALLOWED_FILES.contains(&filename) {
|
|
return Err(format!(
|
|
"Invalid workspace filename: {filename}. Allowed: {}",
|
|
ALLOWED_FILES.join(", ")
|
|
));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Read an OpenClaw workspace file content.
|
|
/// Returns None if the file does not exist.
|
|
#[tauri::command]
|
|
pub async fn read_workspace_file(filename: String) -> Result<Option<String>, String> {
|
|
validate_filename(&filename)?;
|
|
|
|
let path = get_openclaw_dir().join("workspace").join(&filename);
|
|
|
|
if !path.exists() {
|
|
return Ok(None);
|
|
}
|
|
|
|
std::fs::read_to_string(&path).map(Some).map_err(|e| {
|
|
format!("Failed to read workspace file {filename}: {e}")
|
|
})
|
|
}
|
|
|
|
/// Write content to an OpenClaw workspace file (atomic write).
|
|
/// Creates the workspace directory if it does not exist.
|
|
#[tauri::command]
|
|
pub async fn write_workspace_file(filename: String, content: String) -> Result<(), String> {
|
|
validate_filename(&filename)?;
|
|
|
|
let workspace_dir = get_openclaw_dir().join("workspace");
|
|
|
|
// Ensure workspace directory exists
|
|
std::fs::create_dir_all(&workspace_dir).map_err(|e| {
|
|
format!("Failed to create workspace directory: {e}")
|
|
})?;
|
|
|
|
let path = workspace_dir.join(&filename);
|
|
|
|
write_text_file(&path, &content).map_err(|e| {
|
|
format!("Failed to write workspace file {filename}: {e}")
|
|
})
|
|
}
|