use rs_genai::prelude::{CodeExecutionResult as GenaiCodeExecResult, ExecutableCode, Part};
pub fn extract_code_from_text(
text: &str,
delimiters: &[(String, String)],
) -> Option<(String, String)> {
for (start_delim, end_delim) in delimiters {
if let Some(start_idx) = text.find(start_delim.as_str()) {
let code_start = start_idx + start_delim.len();
if let Some(end_idx) = text[code_start..].find(end_delim.as_str()) {
let code = text[code_start..code_start + end_idx].to_string();
let remaining = format!(
"{}{}",
&text[..start_idx],
&text[code_start + end_idx + end_delim.len()..]
);
return Some((code, remaining));
}
}
}
None
}
pub fn build_executable_code_part(code: &str) -> Part {
Part::ExecutableCode {
executable_code: ExecutableCode {
language: "PYTHON".to_string(),
code: code.to_string(),
},
}
}
pub fn build_code_execution_result_part(stdout: &str, stderr: &str) -> Part {
let outcome = if stderr.is_empty() { "OK" } else { "FAILED" };
let output = if stderr.is_empty() {
stdout.to_string()
} else {
format!("{}\n{}", stdout, stderr)
};
Part::CodeExecutionResult {
code_execution_result: GenaiCodeExecResult {
outcome: outcome.to_string(),
output: Some(output),
},
}
}