Skip to content

Commit 8321caa

Browse files
author
Dario Yoshi
committed
fix: use UTF-8 aware truncation in agent handlers to prevent panics
1 parent 7954d02 commit 8321caa

3 files changed

Lines changed: 8 additions & 8 deletions

File tree

src/cortex-cli/src/agent_cmd/handlers/generate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ pub async fn run_generate(args: CreateArgs) -> Result<()> {
117117
println!(" {}", "-".repeat(36));
118118

119119
// Show truncated prompt
120-
let prompt_preview = if generated.system_prompt.len() > 500 {
120+
let prompt_preview = if generated.system_prompt.chars().count() > 500 {
121121
format!(
122122
"{}...\n\n (truncated, {} chars total)",
123-
&generated.system_prompt[..500],
124-
generated.system_prompt.len()
123+
generated.system_prompt.chars().take(500).collect::<String>(),
124+
generated.system_prompt.chars().count()
125125
)
126126
} else {
127127
generated.system_prompt.clone()

src/cortex-cli/src/agent_cmd/handlers/list.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ pub async fn run_list(args: ListArgs) -> Result<()> {
9898
.description
9999
.as_ref()
100100
.map(|d| {
101-
if d.len() > 38 {
102-
format!("{}...", &d[..35])
101+
if d.chars().count() > 38 {
102+
format!("{}...", d.chars().take(35).collect::<String>())
103103
} else {
104104
d.clone()
105105
}

src/cortex-cli/src/agent_cmd/handlers/show.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ pub async fn run_show(args: ShowArgs) -> Result<()> {
136136
if let Some(ref prompt) = agent.prompt {
137137
println!("\nSystem Prompt:");
138138
println!("{}", "-".repeat(40));
139-
let preview = if prompt.len() > 500 {
139+
let preview = if prompt.chars().count() > 500 {
140140
format!(
141141
"{}...\n\n(truncated, {} chars total)",
142-
&prompt[..500],
143-
prompt.len()
142+
prompt.chars().take(500).collect::<String>(),
143+
prompt.chars().count()
144144
)
145145
} else {
146146
prompt.clone()

0 commit comments

Comments
 (0)