From 5ee4c036f95fb48109d1414e47b26dbb543e50c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=B0=8F=E7=99=BD?= <364772080@qq.com> Date: Mon, 26 Aug 2024 22:34:20 +0800 Subject: [PATCH] assistant: Normalize line endings for prompts loaded from templates (#16808) Closes #16804 Similar to #15708, when reading prompts from a template, both Windows and Linux might end up with `CRLF (\r\n)` line endings, which can result in a panic. Release Notes: - N/A --- crates/assistant/src/prompts.rs | 5 +++-- crates/text/src/text.rs | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/assistant/src/prompts.rs b/crates/assistant/src/prompts.rs index 462f74f5d3..ef49c5ced6 100644 --- a/crates/assistant/src/prompts.rs +++ b/crates/assistant/src/prompts.rs @@ -8,6 +8,7 @@ use language::BufferSnapshot; use parking_lot::Mutex; use serde::Serialize; use std::{ops::Range, path::PathBuf, sync::Arc, time::Duration}; +use text::LineEnding; use util::ResultExt; #[derive(Serialize)] @@ -191,8 +192,8 @@ impl PromptBuilder { if let Some(id) = path.split('/').last().and_then(|s| s.strip_suffix(".hbs")) { if let Some(prompt) = Assets.load(path.as_ref()).log_err().flatten() { log::info!("Registering built-in prompt template: {}", id); - handlebars - .register_template_string(id, String::from_utf8_lossy(prompt.as_ref()))? + let prompt = String::from_utf8_lossy(prompt.as_ref()); + handlebars.register_template_string(id, LineEnding::normalize_cow(prompt))? } } } diff --git a/crates/text/src/text.rs b/crates/text/src/text.rs index 6e10068d47..b17748c6d0 100644 --- a/crates/text/src/text.rs +++ b/crates/text/src/text.rs @@ -3041,4 +3041,12 @@ impl LineEnding { text } } + + pub fn normalize_cow(text: Cow) -> Cow { + if let Cow::Owned(replaced) = LINE_SEPARATORS_REGEX.replace_all(&text, "\n") { + replaced.into() + } else { + text + } + } }