seal-tui 0.27.1

Terminal UI for seal
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
//! `SealClient` implementation backed by `SealServices` (direct library calls).

use std::collections::{BTreeSet, HashMap};
use std::path::{Path, PathBuf};

use anyhow::Result;

use seal_core::core::{CoreContext, SealServices};
use seal_core::events::CodeSelection;
use seal_core::scm::{resolve_backend, ScmPreference};
use seal_core::sealignore::SealIgnore;

use crate::db::{
    Comment, FileContentData, FileData, ReviewData, ReviewDetail, ReviewSummary, SealClient,
    ThreadSummary,
};

/// Client that calls seal-core services directly (no subprocess).
pub struct CoreClient {
    ctx: CoreContext,
    repo_root: PathBuf,
}

impl CoreClient {
    pub fn new(ctx: CoreContext, repo_root: &Path) -> Self {
        Self {
            ctx,
            repo_root: repo_root.to_path_buf(),
        }
    }

    /// Re-sync and get fresh services.
    fn services(&self) -> Result<SealServices> {
        self.ctx.services().map_err(|e| anyhow::anyhow!("{e}"))
    }

    fn comment_agent() -> String {
        std::env::var("USER")
            .ok()
            .filter(|value| !value.trim().is_empty())
            .unwrap_or_else(|| "unknown".to_string())
    }
}

// -- Conversions from seal-core types to UI types --

fn convert_review_summary(r: &seal_core::projection::ReviewSummary) -> ReviewSummary {
    ReviewSummary {
        review_id: r.review_id.clone(),
        title: r.title.clone(),
        author: r.author.clone(),
        status: r.status.clone(),
        thread_count: r.thread_count,
        open_thread_count: r.open_thread_count,
        reviewers: r.reviewers.clone(),
    }
}

fn convert_review_detail(r: &seal_core::projection::ReviewDetail) -> ReviewDetail {
    ReviewDetail {
        review_id: r.review_id.clone(),
        jj_change_id: r.jj_change_id.clone(),
        scm_kind: r.scm_kind.clone(),
        scm_anchor: r.scm_anchor.clone(),
        initial_commit: r.initial_commit.clone(),
        final_commit: r.final_commit.clone(),
        title: r.title.clone(),
        description: r.description.clone(),
        author: r.author.clone(),
        created_at: r.created_at.clone(),
        status: r.status.clone(),
        status_changed_at: r.status_changed_at.clone(),
        status_changed_by: r.status_changed_by.clone(),
        abandon_reason: r.abandon_reason.clone(),
        thread_count: r.thread_count,
        open_thread_count: r.open_thread_count,
    }
}

fn convert_thread_summary(t: &seal_core::projection::ThreadSummary) -> ThreadSummary {
    ThreadSummary {
        thread_id: t.thread_id.clone(),
        file_path: t.file_path.clone(),
        selection_start: t.selection_start,
        selection_end: t.selection_end,
        status: t.status.clone(),
        comment_count: t.comment_count,
    }
}

fn convert_comment(c: &seal_core::projection::Comment) -> Comment {
    Comment {
        comment_id: c.comment_id.clone(),
        author: c.author.clone(),
        body: c.body.clone(),
        created_at: c.created_at.clone(),
    }
}

impl SealClient for CoreClient {
    fn list_reviews(&self, status: Option<&str>) -> Result<Vec<ReviewSummary>> {
        let services = self.services()?;
        let reviews = services
            .reviews()
            .list(status, None)
            .map_err(|e| anyhow::anyhow!("{e}"))?;
        Ok(reviews.iter().map(convert_review_summary).collect())
    }

    fn load_review_data(&self, review_id: &str) -> Result<Option<ReviewData>> {
        let services = self.services()?;

        let detail = match services
            .reviews()
            .get_optional(review_id)
            .map_err(|e| anyhow::anyhow!("{e}"))?
        {
            Some(d) => d,
            None => return Ok(None),
        };

        let core_threads = services
            .threads()
            .list(review_id, None, None)
            .map_err(|e| anyhow::anyhow!("{e}"))?;
        let sealignore = SealIgnore::load(&self.repo_root);
        let visible_threads: Vec<_> = core_threads
            .into_iter()
            .filter(|thread| !sealignore.is_ignored(&thread.file_path))
            .collect();

        let mut threads = Vec::with_capacity(visible_threads.len());
        let mut comments: HashMap<String, Vec<Comment>> = HashMap::new();

        for t in &visible_threads {
            threads.push(convert_thread_summary(t));

            let core_comments = services
                .comments()
                .list(&t.thread_id)
                .map_err(|e| anyhow::anyhow!("{e}"))?;

            if !core_comments.is_empty() {
                comments.insert(
                    t.thread_id.clone(),
                    core_comments.iter().map(convert_comment).collect(),
                );
            }
        }

        let review_detail = convert_review_detail(&detail);

        // Build file diffs using SCM
        let files = self.build_file_diffs(&detail, &visible_threads);

        Ok(Some(ReviewData {
            detail: review_detail,
            threads,
            comments,
            files,
        }))
    }

    fn comment(
        &self,
        review_id: &str,
        file_path: &str,
        start_line: i64,
        end_line: Option<i64>,
        body: &str,
    ) -> Result<()> {
        let services = self.services()?;
        let agent = Self::comment_agent();

        // Get the review's initial commit for thread creation
        let review = services
            .reviews()
            .get(review_id)
            .map_err(|e| anyhow::anyhow!("{e}"))?;

        #[allow(clippy::cast_sign_loss)]
        let selection = match end_line {
            Some(end) if end != start_line => CodeSelection::range(start_line as u32, end as u32),
            _ => CodeSelection::line(start_line as u32),
        };

        services
            .comments()
            .add_to_review(
                review_id,
                file_path,
                selection,
                body,
                review.initial_commit.clone(),
                Some(&agent),
            )
            .map_err(|e| anyhow::anyhow!("{e}"))?;

        Ok(())
    }

    fn reply(&self, thread_id: &str, body: &str) -> Result<()> {
        let services = self.services()?;
        let agent = Self::comment_agent();

        services
            .comments()
            .add_to_thread(thread_id, body, Some(&agent))
            .map_err(|e| anyhow::anyhow!("{e}"))?;

        Ok(())
    }
}

// -- Diff assembly (mirrors CLI `build_file_diffs` logic) --

impl CoreClient {
    fn build_file_diffs(
        &self,
        review: &seal_core::projection::ReviewDetail,
        threads: &[seal_core::projection::ThreadSummary],
    ) -> Vec<FileData> {
        let scm = match resolve_backend(&self.repo_root, ScmPreference::Auto) {
            Ok(s) => s,
            Err(_) => return Vec::new(),
        };

        // Resolve target commit
        let target_commit = review
            .final_commit
            .clone()
            .or_else(|| scm.commit_for_anchor(&review.scm_anchor).ok())
            .or_else(|| scm.commit_for_anchor(&review.jj_change_id).ok())
            .unwrap_or_else(|| review.initial_commit.clone());

        let base_commit = scm
            .parent_commit(&target_commit)
            .unwrap_or_else(|_| review.initial_commit.clone());

        // Get full diff and split by file
        let full_diff = scm
            .diff_git(&base_commit, &target_commit)
            .unwrap_or_default();
        let diffs_by_file = split_diff_by_file(&full_diff);

        // Collect files: union of files with threads + files with diffs
        let sealignore = SealIgnore::load(&self.repo_root);
        let files_with_threads: BTreeSet<String> =
            threads.iter().map(|t| t.file_path.clone()).collect();
        let mut all_files: BTreeSet<String> = files_with_threads;
        for key in diffs_by_file.keys() {
            all_files.insert((*key).to_string());
        }

        // Pre-fetch file contents for thread files
        let mut file_cache: HashMap<String, String> = HashMap::new();
        for thread in threads {
            if !file_cache.contains_key(&thread.file_path) {
                if let Ok(contents) = scm.show_file(&target_commit, &thread.file_path) {
                    file_cache.insert(thread.file_path.clone(), contents);
                }
            }
        }

        let mut result = Vec::new();
        for file_path in &all_files {
            if sealignore.is_ignored(file_path) {
                continue;
            }

            let diff = diffs_by_file.get(file_path.as_str()).map(|s| s.to_string());

            // Check for orphaned threads (not covered by diff hunks)
            let file_threads: Vec<&seal_core::projection::ThreadSummary> = threads
                .iter()
                .filter(|t| &t.file_path == file_path)
                .collect();

            let content = if !file_threads.is_empty() {
                if let Some(ref diff_text) = diff {
                    let hunks = parse_hunk_ranges(diff_text);
                    let has_orphan = file_threads.iter().any(|t| {
                        let line = t.selection_start as u32;
                        !hunks.iter().any(|h| line >= h.0 && line <= h.1)
                    });
                    if has_orphan {
                        build_content_window(&file_cache, file_path, &file_threads)
                    } else {
                        None
                    }
                } else {
                    build_content_window(&file_cache, file_path, &file_threads)
                }
            } else {
                None
            };

            result.push(FileData {
                path: file_path.clone(),
                diff,
                content,
            });
        }

        result
    }
}

/// Split a full git-format diff into per-file sections.
fn split_diff_by_file(full_diff: &str) -> HashMap<&str, &str> {
    let mut result = HashMap::new();
    let mut current_file: Option<&str> = None;
    let mut current_start: usize = 0;
    let mut offset = 0;

    for line in full_diff.lines() {
        let byte_offset = offset;
        offset += line.len() + 1; // +1 for newline

        if line.starts_with("diff --git") {
            if let Some(file) = current_file {
                let section = &full_diff[current_start..byte_offset];
                if !section.trim().is_empty() {
                    result.insert(file, section);
                }
            }
            current_file = line
                .split_whitespace()
                .nth(3)
                .map(|s| s.trim_start_matches("b/"));
            current_start = byte_offset;
        }
    }

    if let Some(file) = current_file {
        let section = &full_diff[current_start..];
        if !section.trim().is_empty() {
            result.insert(file, section);
        }
    }

    result
}

/// Parse unified diff hunk headers to extract new-side line ranges.
fn parse_hunk_ranges(diff: &str) -> Vec<(u32, u32)> {
    let mut ranges = Vec::new();
    for line in diff.lines() {
        if !line.starts_with("@@") {
            continue;
        }
        if let Some(plus_pos) = line.find('+') {
            let after_plus = &line[plus_pos + 1..];
            let end = after_plus
                .find(|c: char| c == ' ' || c == '@')
                .unwrap_or(after_plus.len());
            let range_str = &after_plus[..end];

            if let Some((start_str, count_str)) = range_str.split_once(',') {
                if let (Ok(start), Ok(count)) = (start_str.parse::<u32>(), count_str.parse::<u32>())
                {
                    if count > 0 {
                        ranges.push((start, start + count - 1));
                    }
                }
            } else if let Ok(start) = range_str.parse::<u32>() {
                ranges.push((start, start));
            }
        }
    }
    ranges
}

/// Build a content window covering all thread locations in a file.
fn build_content_window(
    file_cache: &HashMap<String, String>,
    file_path: &str,
    threads: &[&seal_core::projection::ThreadSummary],
) -> Option<FileContentData> {
    let contents = file_cache.get(file_path)?;
    let lines: Vec<&str> = contents.lines().collect();
    if lines.is_empty() {
        return None;
    }

    // Find the range covering all threads with context
    let context = 5u32;
    let mut min_line = u32::MAX;
    let mut max_line = 0u32;

    for t in threads {
        let start = t.selection_start as u32;
        let end = t.selection_end.unwrap_or(t.selection_start) as u32;
        min_line = min_line.min(start.saturating_sub(context));
        max_line = max_line.max(end + context);
    }

    // Clamp to file bounds (1-based)
    let start_line = min_line.max(1);
    let end_line = max_line.min(lines.len() as u32);

    if start_line > end_line {
        return None;
    }

    let window_lines: Vec<String> = lines[(start_line as usize - 1)..=(end_line as usize - 1)]
        .iter()
        .map(|l| (*l).to_string())
        .collect();

    Some(FileContentData {
        start_line: i64::from(start_line),
        lines: window_lines,
    })
}