libtenx 0.0.4

A library for building AI-assisted coding tools, with session management, patch validation, and multiple model support.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! Pretty-printing functionality for various Tenx objects.
use crate::{
    config::Config,
    context,
    context::ContextProvider,
    model, patch,
    session::{Operation, Session, Step, StepType},
    Result, TenxError,
};
use colored::*;
use textwrap::{indent, wrap, Options};

const INDENT: &str = "  ";

fn get_term_width() -> usize {
    termsize::get()
        .map(|size| size.cols as usize)
        .unwrap_or(120)
}

fn format_usage(usage: &model::Usage) -> String {
    let values = usage.values();
    let mut keys: Vec<_> = values.keys().collect();
    keys.sort();
    keys.iter()
        .map(|k| format!("{}: {}", k.blue().bold(), values.get(*k).unwrap()))
        .collect::<Vec<_>>()
        .join("\n")
}

/// Converts a path to use ~ for paths under the user's home directory
fn display_path_with_home(path: &std::path::Path) -> String {
    if let Ok(home) = std::env::var("HOME") {
        let home_path = std::path::Path::new(&home);
        if let Ok(rel_path) = path.strip_prefix(home_path) {
            format!("~/{}", rel_path.display())
        } else {
            path.display().to_string()
        }
    } else {
        path.display().to_string()
    }
}

fn print_session_info(config: &Config, _: &Session) -> String {
    let mut output = String::new();
    let display_path = display_path_with_home(&config.project_root());
    output.push_str(&format!("{} {}\n", "root:".blue().bold(), display_path));
    output
}

fn print_context_specs(session: &Session) -> String {
    let mut output = String::new();
    if !session.contexts().is_empty() {
        output.push_str(&format!("{}\n", "context:".blue().bold()));
        for context in session.contexts() {
            output.push_str(&format!("{}- {}\n", INDENT, context.human()));
        }
    }
    output
}

fn print_editables(config: &Config, session: &Session) -> Result<String> {
    let mut output = String::new();
    let editables = session.abs_editables(config)?;
    if !editables.is_empty() {
        output.push_str(&format!("{}\n", "edit:".blue().bold()));
        for path in editables {
            output.push_str(&format!(
                "{}- {}\n",
                INDENT,
                config.relpath(&path).display()
            ));
        }
    }
    Ok(output)
}

fn print_operations(config: &Config, operations: &[Operation]) -> String {
    let mut output = String::new();
    output.push_str(&format!(
        "{}{}\n",
        INDENT.repeat(2),
        "operations:".blue().bold()
    ));
    for op in operations {
        match op {
            Operation::Edit(path) => {
                output.push_str(&format!(
                    "{}- edit: {}\n",
                    INDENT.repeat(3),
                    config.relpath(path).display()
                ));
            }
        }
    }
    output
}

fn print_steps(config: &Config, session: &Session, full: bool, width: usize) -> Result<String> {
    if session.steps().is_empty() {
        return Ok(String::new());
    }
    let mut output = String::new();
    for (i, step) in session.steps().iter().enumerate() {
        output.push_str(&format!("\n{}\n", "=".repeat(width)));
        output.push_str(&format!("{}\n", format!("Step {}", i).cyan().bold()));
        output.push_str(&format!("{}\n", "=".repeat(width)));
        output.push_str(&render_step_prompt(step, width, full));
        output.push('\n');
        if let Some(response) = &step.model_response {
            if let Some(comment) = &response.comment {
                output.push_str(&format!(
                    "{}{}\n",
                    INDENT.repeat(2),
                    "model comment:".blue().bold()
                ));
                let comment_text = if full {
                    comment.clone()
                } else {
                    comment.lines().next().unwrap_or("").to_string()
                };
                output.push_str(&wrapped_block(&comment_text, width, INDENT.len() * 3));
                output.push('\n');
            }
            if let Some(text) = &response.response_text {
                if full {
                    output.push_str(&format!(
                        "{}{}\n",
                        INDENT.repeat(2),
                        "raw model response:".blue().bold()
                    ));
                    output.push_str(&wrapped_block(&text.clone(), width, INDENT.len() * 3));
                    output.push('\n');
                }
            }

            if !response.operations.is_empty() {
                output.push_str(&print_operations(config, &response.operations));
            }
            if let Some(patch) = &response.patch {
                output.push_str(&print_patch(config, patch, full, width));
            }
            if let Some(usage) = &response.usage {
                output.push_str(&format!("{}{}\n", INDENT.repeat(2), "usage:".blue().bold()));
                for line in format_usage(usage).lines() {
                    output.push_str(&format!("{}{}\n", INDENT.repeat(3), line));
                }
            }
        }
        if let Some(err) = &step.err {
            output.push_str(&format!(
                "{}{}\n",
                INDENT.repeat(2),
                "error:".yellow().bold()
            ));
            let error_text = if full {
                full_error(err)
            } else {
                format!("{}", err)
            };
            output.push_str(&wrapped_block(&error_text, width, INDENT.len() * 3));
            output.push('\n');
        }
    }
    Ok(output)
}

fn render_step_prompt(step: &Step, width: usize, full: bool) -> String {
    let prompt_header = format!("{}{}\n", INDENT.repeat(2), "prompt:".blue().bold());
    let text = &step.prompt;
    match step.step_type {
        StepType::Code | StepType::Fix | StepType::Auto => format!(
            "{}{}",
            prompt_header,
            wrapped_block(text, width, INDENT.len() * 3)
        ),
        StepType::Error if full => format!(
            "{}{}",
            prompt_header,
            wrapped_block(text, width, INDENT.len() * 3)
        ),
        StepType::Error => {
            let lines: Vec<&str> = text.lines().collect();
            let first_line = lines.first().unwrap_or(&"");
            let remaining_lines = lines.len().saturating_sub(1);
            format!(
                "{}{}\n{}",
                prompt_header,
                wrapped_block(first_line, width, INDENT.len() * 3),
                wrapped_block(
                    &format!("... {} more lines", remaining_lines),
                    width,
                    INDENT.len() * 3
                )
            )
        }
    }
}

fn print_patch(config: &Config, patch: &patch::Patch, full: bool, width: usize) -> String {
    use std::collections::HashMap;

    let mut output = String::new();
    output.push_str(&format!(
        "{}{}\n",
        INDENT.repeat(2),
        "modified:".blue().bold()
    ));

    // Group changes by file path
    let mut changes_by_file: HashMap<&std::path::Path, Vec<&patch::Change>> = HashMap::new();
    for change in &patch.changes {
        let path = match change {
            patch::Change::Write(w) => &w.path,
            patch::Change::Replace(r) => &r.path,
            patch::Change::Smart(s) => &s.path,
            patch::Change::UDiff(_) => continue,
        };
        changes_by_file.entry(path).or_default().push(change);
    }

    for (path, changes) in changes_by_file {
        let file_path = config.relpath(path).display().to_string().green().bold();
        output.push_str(&format!("{}- {}\n", INDENT.repeat(3), file_path));

        // Count changes by type
        let mut write_count = 0;
        let mut replace_count = 0;
        let mut smart_count = 0;
        for change in &changes {
            match change {
                patch::Change::Write(_) => write_count += 1,
                patch::Change::Replace(_) => replace_count += 1,
                patch::Change::Smart(_) => smart_count += 1,
                patch::Change::UDiff(_) => (),
            }
        }

        let mut types = Vec::new();
        if write_count > 0 {
            types.push(format!("write ({})", write_count));
        }
        if replace_count > 0 {
            types.push(format!("replace ({})", replace_count));
        }
        if smart_count > 0 {
            types.push(format!("smart ({})", smart_count));
        }

        output.push_str(&format!("{}{}\n", INDENT.repeat(4), types.join(", ")));

        if full {
            for change in &changes {
                match *change {
                    patch::Change::Write(w) => {
                        output.push_str(&wrapped_block(&w.content, width, INDENT.len() * 5));
                        output.push('\n');
                    }
                    patch::Change::Replace(r) => {
                        output.push_str(&format!(
                            "{}{}\n",
                            INDENT.repeat(5),
                            "old:".yellow().bold()
                        ));
                        output.push_str(&wrapped_block(&r.old, width, INDENT.len() * 6));
                        output.push_str(&format!(
                            "\n{}{}\n",
                            INDENT.repeat(5),
                            "new:".green().bold()
                        ));
                        output.push_str(&wrapped_block(&r.new, width, INDENT.len() * 6));
                        output.push('\n');
                    }
                    patch::Change::Smart(s) => {
                        output.push_str(&wrapped_block(&s.text, width, INDENT.len() * 5));
                        output.push('\n');
                    }
                    patch::Change::UDiff(_) => (),
                }
            }
        }
    }

    // Handle UDiff changes separately
    for change in &patch.changes {
        if let patch::Change::UDiff(w) = change {
            output.push_str(&format!("{}- udiff\n", INDENT.repeat(3)));
            if full {
                output.push_str(&wrapped_block(&w.patch, width, INDENT.len() * 4));
                output.push('\n');
            }
        }
    }

    output
}

/// Pretty prints a TenxError with full details.
fn full_error(error: &TenxError) -> String {
    match error {
        TenxError::Check { name, user, model } => {
            format!(
                "{}: {}\n{}: {}\n{}: {}",
                "Check Error".red().bold(),
                name,
                "User Message".yellow().bold(),
                user,
                "Model Message".yellow().bold(),
                model
            )
        }
        TenxError::Patch { user, model } => {
            format!(
                "{}\n{}: {}\n{}: {}",
                "Patch Error".red().bold(),
                "User Message".yellow().bold(),
                user,
                "Model Message".yellow().bold(),
                model
            )
        }
        _ => format!("{:?}", error),
    }
}

fn wrapped_block(text: &str, width: usize, indent: usize) -> String {
    let ident = " ".repeat(indent);
    let options = Options::new(width - indent)
        .initial_indent(&ident)
        .subsequent_indent(&ident);
    wrap(text, &options).join("\n")
}

/// Pretty prints a context item with optional full detail
fn print_context_item(item: &context::ContextItem) -> String {
    let mut output = String::new();

    output.push_str(&format!(
        "{}{}: {}\n",
        INDENT.repeat(2),
        item.ty.blue().bold(),
        item.source
    ));

    output.push_str(&wrapped_block(
        &item.body,
        get_term_width(),
        INDENT.len() * 3,
    ));
    output.push('\n');

    output
}

/// Pretty prints the Session information.
pub fn print_session(config: &Config, session: &Session, full: bool) -> Result<String> {
    let width = get_term_width();
    let mut output = String::new();
    output.push_str(&format!("{}\n", "session:".blue().bold()));
    output.push_str(&indent(
        &format!(
            "{}{}{}{}",
            print_session_info(config, session),
            print_context_specs(session),
            print_editables(config, session)?,
            print_steps(config, session, full, width - INDENT.len())?
        ),
        INDENT,
    ));
    Ok(output)
}

/// Pretty prints project information
pub fn print_project(config: &Config) -> String {
    let mut output = String::new();
    let display_path = display_path_with_home(&config.project_root());
    output.push_str(&format!(
        "{} {}\n",
        "project root:".blue().bold(),
        display_path
    ));
    if !config.project.include.is_empty() {
        output.push_str(&format!(
            "{} {}\n",
            "globs:".blue().bold(),
            config
                .project
                .include
                .iter()
                .map(|i| i.to_string())
                .collect::<Vec<_>>()
                .join(", ")
        ));
    }
    output
}

/// Pretty prints all contexts in a session
pub fn print_contexts(config: &Config, session: &Session) -> Result<String> {
    let mut output = String::new();
    for context in session.contexts() {
        let items = context.context_items(config, &Session::default())?;
        if let Some(item) = items.into_iter().next() {
            output.push_str(&print_context_item(&item));
        }
    }
    Ok(output)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        context::Context,
        patch::Patch,
        session::{ModelResponse, Step},
        TenxError,
    };
    use tempfile::TempDir;

    fn create_test_session() -> (TempDir, Session) {
        let temp_dir = TempDir::new().unwrap();
        let root_path = temp_dir.path().to_path_buf();
        let config = Config::default();
        let mut session = Session::default();
        session
            .add_prompt(
                "test_model".into(),
                "Test prompt".to_string(),
                StepType::Code,
            )
            .unwrap();
        let test_file_path = root_path.join("test_file.rs");
        std::fs::write(&test_file_path, "Test content").unwrap();
        session.add_context(Context::new_path(&config, "test_file.rs").unwrap());
        (temp_dir, session)
    }

    #[test]
    fn test_print_steps_empty_session() {
        let config = Config::default();
        let (_temp_dir, session) = create_test_session();
        let result = print_steps(&config, &session, false, 80);
        assert!(result.is_ok());
        let output = result.unwrap();
        assert!(output.contains("Step 0"));
        assert!(output.contains("Test prompt"));
    }

    #[test]
    fn test_print_steps_with_patch() {
        let config = Config::default();
        let (_temp_dir, mut session) = create_test_session();
        if let Some(step) = session.last_step_mut() {
            step.model_response = Some(ModelResponse {
                patch: Some(Patch {
                    ..Default::default()
                }),
                operations: vec![],
                usage: None,
                comment: Some("Test comment".to_string()),
                response_text: Some("Test comment".to_string()),
            });
        }
        let result = print_steps(&config, &session, false, 80);
        assert!(result.is_ok());
        let output = result.unwrap();
        assert!(output.contains("Step 0"));
        assert!(output.contains("Test prompt"));
        assert!(output.contains("comment:"));
        assert!(output.contains("Test comment"));
    }

    #[test]
    fn test_print_steps_with_error() {
        let config = Config::default();
        let (_temp_dir, mut session) = create_test_session();
        if let Some(step) = session.last_step_mut() {
            step.err = Some(TenxError::Internal("Test error".to_string()));
        }
        let result = print_steps(&config, &session, false, 80);
        assert!(result.is_ok());
        let output = result.unwrap();
        assert!(output.contains("Step 0"));
        assert!(output.contains("Test prompt"));
        assert!(output.contains("error:"));
        assert!(output.contains("Test error"));
    }

    #[test]
    fn test_render_step_editable() {
        let step = Step::new(
            "test_model".into(),
            "Test prompt\nwith multiple\nlines".to_string(),
            StepType::Code,
        );
        let full_result = render_step_prompt(&step, 80, true);
        assert!(full_result.contains("Test prompt"));
        assert!(full_result.contains("with multiple"));
        assert!(full_result.contains("lines"));
    }
}