-
Notifications
You must be signed in to change notification settings - Fork 5.8k
fix: customised recipe to yaml string to avoid minininjia parsing error #5494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,122 @@ | ||||||
| use std::fmt::Write; | ||||||
|
|
||||||
| /// Normalizes how `serde_yaml` outputs multi-line strings. | ||||||
| /// It uses internal heuristics to decide between `|` and quoted text with escaped | ||||||
| /// `\n` and `\"`, and the quoted form breaks MiniJinja parsing. | ||||||
| /// Example before: | ||||||
| /// prompt: "Hello \\\"World\\\"\\n{% if user == \\\"admin\\\" %}Welcome{% endif %}" | ||||||
| /// After fix: | ||||||
| /// prompt: | | ||||||
| /// Hello "World" | ||||||
| /// {% if user == "admin" %}Welcome{% endif %} | ||||||
| pub fn reformat_fields_with_multiline_values(yaml: &str, multiline_fields: &[&str]) -> String { | ||||||
| let mut result = String::new(); | ||||||
|
|
||||||
| for line in yaml.lines() { | ||||||
| let trimmed = line.trim_start(); | ||||||
| if trimmed.is_empty() { | ||||||
| writeln!(result).unwrap(); | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| let indent = line.len() - trimmed.len(); | ||||||
| let indent_str = " ".repeat(indent); | ||||||
|
|
||||||
| let matched_field = multiline_fields | ||||||
| .iter() | ||||||
| .find(|&f| trimmed.starts_with(&format!("{f}: "))); | ||||||
|
|
||||||
| if let Some(field) = matched_field { | ||||||
| if let Some((_, raw_val)) = trimmed.split_once(": ") { | ||||||
| if raw_val.contains("\\n") { | ||||||
|
||||||
| if raw_val.contains("\\n") { | |
| if raw_val.contains("\\n") || raw_val.contains("\\\\n") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The list of fields to reformat is hardcoded here. Consider making this configurable or deriving it from the Recipe struct to ensure all relevant string fields are handled consistently as the struct evolves.