use serde::{Deserialize, Serialize};
use super::error::AiError;
use super::format::Format;
#[cfg(feature = "kalosm")]
use kalosm_sample as _;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(
feature = "kalosm",
derive(kalosm::language::Parse, kalosm::language::Schema)
)]
pub struct Plan {
#[serde(default)]
pub ops: Vec<Op>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[cfg_attr(
feature = "kalosm",
derive(kalosm::language::Parse, kalosm::language::Schema)
)]
#[serde(rename_all = "lowercase")]
pub enum Position {
Before,
#[default]
After,
Start,
End,
}
impl Position {
pub fn as_str(self) -> &'static str {
match self {
Self::Before => "before",
Self::After => "after",
Self::Start => "start",
Self::End => "end",
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(
feature = "kalosm",
derive(kalosm::language::Parse, kalosm::language::Schema)
)]
#[serde(tag = "op", rename_all = "snake_case")]
pub enum Op {
Replace {
index: u32,
old: String,
new: String,
},
Insert {
index: u32,
#[serde(default)]
position: Position,
text: String,
},
InsertParagraph {
index: u32,
#[serde(default)]
position: Position,
text: String,
},
Delete {
index: u32,
#[serde(default)]
text: String,
},
Comment {
index: u32,
#[serde(default)]
anchor: String,
text: String,
},
SetCell {
sheet: String,
cell: String,
value: String,
},
SetText {
slide: u32,
element: u32,
text: String,
},
}
impl Op {
pub fn name(&self) -> &'static str {
match self {
Self::Replace { .. } => "replace",
Self::Insert { .. } => "insert",
Self::InsertParagraph { .. } => "insert_paragraph",
Self::Delete { .. } => "delete",
Self::Comment { .. } => "comment",
Self::SetCell { .. } => "set_cell",
Self::SetText { .. } => "set_text",
}
}
pub fn format(&self) -> Format {
match self {
Self::Replace { .. }
| Self::Insert { .. }
| Self::InsertParagraph { .. }
| Self::Delete { .. }
| Self::Comment { .. } => Format::Docx,
Self::SetCell { .. } => Format::Xlsx,
Self::SetText { .. } => Format::Pptx,
}
}
pub(crate) fn require_format(&self, expected: Format) -> Result<(), AiError> {
let got = self.format();
if got == expected {
Ok(())
} else {
Err(AiError::Apply(format!(
"op {} is for {got}, but the file is {expected}",
self.name()
)))
}
}
}
pub trait Completer: Send + Sync {
fn complete(
&self,
view: &super::view::DocumentView,
instruction: &str,
) -> impl std::future::Future<Output = Result<Plan, AiError>> + Send;
}
#[derive(Debug, Clone)]
pub struct StaticCompleter {
plan: Plan,
}
impl StaticCompleter {
pub fn new(plan: Plan) -> Self {
Self { plan }
}
}
impl Completer for StaticCompleter {
async fn complete(
&self,
_view: &super::view::DocumentView,
_instruction: &str,
) -> Result<Plan, AiError> {
Ok(self.plan.clone())
}
}
pub fn system_prompt(format: Format) -> &'static str {
match format {
Format::Docx => {
"You edit a Word document by emitting operations against the numbered view. \
Each line is `INDEX| text`. Put INDEX in `index` — it is the number before |, \
not 1..N of this window. Copy a short `old`/`anchor` (a few words from the \
visible text). Never paste a whole paragraph, never copy an ellipsis (…), \
and never join several INDEX lines into one `old`. \
For capitalization, replace only the first word. Prefer replace. \
Never insert when existing words should change — emit replace. \
DOCX ops become Word tracked changes. Empty ops list if nothing must change."
}
Format::Xlsx => {
"You edit an Excel workbook by emitting set_cell operations. \
Each view line is `Sheet!A1<TAB>value`. Copy sheet names and A1 refs exactly. \
Only emit ops the instruction requires. Empty ops list if nothing must change."
}
Format::Pptx => {
"You edit a PowerPoint deck by emitting set_text operations. \
Each view line is `slide.element| text`. Slide and element are 1-based. \
Only emit ops the instruction requires. Empty ops list if nothing must change."
}
}
}
pub fn user_prompt(view: &super::view::DocumentView, instruction: &str) -> String {
format!("{}\n# Instruction\n{}", view.render(), instruction.trim())
}
pub fn json_output_instruction() -> &'static str {
" Reply with one JSON object {\"ops\":[...]} and nothing else. \
index is the number before | on the line. old/new are a few words from that one line. \
Do not join lines with |. \
Example: {\"ops\":[{\"op\":\"replace\",\"index\":21,\"old\":\"Customer\",\"new\":\"CUSTOMER\"}]} \
or {\"ops\":[]}."
}
pub(crate) fn json_object_complete(text: &str) -> bool {
extract_json_object(text).is_some()
}
pub(crate) const PLAN_MAX_TOKENS: u32 = 1536;
pub(crate) fn eprint_chunk_progress(done: usize, total: usize) {
if total <= 1 {
return;
}
use std::io::Write;
eprint!("\rplanning {done}/{total}");
let _ = std::io::stderr().flush();
if done == total {
eprintln!();
}
}
pub fn parse_plan_json(text: &str) -> Result<Plan, AiError> {
let trimmed = text.trim();
if let Ok(plan) = serde_json::from_str::<Plan>(trimmed) {
return Ok(plan);
}
if let Some(json) = extract_json_object(trimmed) {
if let Ok(plan) = serde_json::from_str::<Plan>(json) {
return Ok(plan);
}
}
if let Some(plan) = salvage_plan(trimmed) {
return Ok(plan);
}
if trimmed.contains('{') {
return Err(AiError::Model(format!(
"model cut off mid-JSON (generation cap). \
Keep `old`/`new` to a few words, not the whole paragraph. got: {}",
truncate_for_error(trimmed)
)));
}
Err(AiError::Model(format!(
"model did not emit a plan object. got: {}",
truncate_for_error(trimmed)
)))
}
fn salvage_plan(text: &str) -> Option<Plan> {
let ops_key = text.find("\"ops\"")?;
let after_key = &text[ops_key + 5..];
let bracket = after_key.find('[')?;
let body = &after_key[bracket + 1..];
let bytes = body.as_bytes();
let mut i = 0usize;
let mut ops = Vec::new();
loop {
while i < body.len() && (bytes[i].is_ascii_whitespace() || bytes[i] == b',') {
i += 1;
}
if i >= body.len() || bytes[i] == b']' {
break;
}
if bytes[i] != b'{' {
break;
}
let Some(obj) = extract_json_object(&body[i..]) else {
break;
};
match serde_json::from_str::<Op>(obj) {
Ok(op) => {
ops.push(op);
i += obj.len();
}
Err(_) => break,
}
}
if ops.is_empty() {
None
} else {
Some(Plan { ops })
}
}
fn extract_json_object(text: &str) -> Option<&str> {
let start = text.find('{')?;
let bytes = text.as_bytes();
let mut depth = 0i32;
let mut in_string = false;
let mut escape = false;
for (i, &b) in bytes.iter().enumerate().skip(start) {
if in_string {
if escape {
escape = false;
} else if b == b'\\' {
escape = true;
} else if b == b'"' {
in_string = false;
}
continue;
}
match b {
b'"' => in_string = true,
b'{' => depth += 1,
b'}' => {
depth -= 1;
if depth == 0 {
return Some(&text[start..=i]);
}
}
_ => {}
}
}
None
}
fn truncate_for_error(text: &str) -> String {
const MAX: usize = 240;
if text.len() <= MAX {
return text.to_string();
}
format!("{}…", &text[..MAX])
}
#[cfg(test)]
mod tests {
use super::{system_prompt, user_prompt, Op, Plan};
#[test]
fn plan_round_trips_json() {
let plan = Plan {
ops: vec![Op::Replace {
index: 1,
old: "thirty".into(),
new: "sixty".into(),
}],
};
let json = serde_json::to_string(&plan).unwrap();
let back: Plan = serde_json::from_str(&json).unwrap();
assert_eq!(back.ops.len(), 1);
assert_eq!(back.ops[0].name(), "replace");
}
#[test]
fn prompts_name_the_format() {
use crate::ai::format::Format;
assert!(system_prompt(Format::Docx).contains("tracked"));
assert!(system_prompt(Format::Xlsx).contains("set_cell"));
assert!(system_prompt(Format::Pptx).contains("set_text"));
let view = crate::ai::view::DocumentView {
format: Format::Docx,
lines: vec!["1| hello".into()],
truncated: false,
window: String::new(),
};
let user = user_prompt(&view, "change hello");
assert!(user.contains("hello"));
assert!(user.contains("change hello"));
assert!(user.contains("INDEX"));
}
#[test]
fn parse_plan_from_fence_and_prose() {
let plan = super::parse_plan_json(
"Sure.\n```json\n{\"ops\":[{\"op\":\"replace\",\"index\":1,\"old\":\"a\",\"new\":\"b\"}]}\n```\n",
)
.unwrap();
assert_eq!(plan.ops.len(), 1);
assert_eq!(plan.ops[0].name(), "replace");
let empty = super::parse_plan_json("here you go {\"ops\":[]} thanks").unwrap();
assert!(empty.ops.is_empty());
}
#[test]
fn salvage_keeps_complete_ops_from_truncated_json() {
let plan = super::parse_plan_json(
r#"{"ops":[
{"op":"replace","index":1,"old":"Customer:","new":"CUSTOMER:"},
{"op":"replace","index":2,"old":"Contact:","new":"CONTACT:"},
{"op":"repl"#,
)
.unwrap();
assert_eq!(plan.ops.len(), 2);
assert_eq!(plan.ops[0].name(), "replace");
}
#[test]
fn parse_plan_rejects_garbage() {
let err = super::parse_plan_json("I cannot do that.").unwrap_err();
assert!(err.to_string().contains("did not emit a plan"));
let cut = super::parse_plan_json(
r#"{"ops":[{"op":"replace","index":1,"old":"H4: Customer shall own"#,
)
.unwrap_err();
assert!(cut.to_string().contains("mid-JSON"), "{cut}");
assert!(super::json_object_complete(r#"{"ops":[]}"#));
assert!(!super::json_object_complete(r#"{"ops":[{"op":"replace""#));
}
}