use super::config::AiConfig;
use super::error::Error;
use super::http::{self, ChatMessage};
#[non_exhaustive]
#[derive(Debug, Clone, Default)]
pub struct RefineAiOptions {
pub instructions: Option<String>,
}
impl RefineAiOptions {
pub fn with_instructions(mut self, instructions: impl Into<String>) -> Self {
self.instructions = Some(instructions.into());
self
}
}
const SYSTEM_PROMPT: &str = "You clean up Markdown produced by an automated document \
converter. Fix awkward phrasing, spacing, and structure that a human editor would \
fix, without changing the document's meaning, adding information, or removing any \
content. Respond with the revised Markdown only — no commentary, no code fence \
around the whole response.";
pub fn refine_markdown(
cfg: &AiConfig,
markdown: &str,
options: &RefineAiOptions,
) -> Result<String, Error> {
let system = match &options.instructions {
Some(extra) => format!("{SYSTEM_PROMPT}\n\n{extra}"),
None => SYSTEM_PROMPT.to_string(),
};
let messages = vec![
ChatMessage::text("system", system),
ChatMessage::text("user", markdown),
];
let content = http::call(cfg, messages, false)?;
Ok(content.trim().to_string())
}