git-iris 2.0.8

AI-powered Git workflow assistant for smart commits, code reviews, changelogs, and release notes
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//! Event-driven architecture for Iris Studio
//!
//! All state changes flow through events. This provides:
//! - Clear, traceable data flow
//! - Testable reducer paths for shared workflows
//! - Unified history across all modes
//! - Agent can control UI through tool-emitted events

use std::path::PathBuf;
use std::time::Instant;

use crossterm::event::{KeyEvent, MouseEvent};

use crate::types::GeneratedMessage;

use super::state::{Mode, PanelId};

// Note: Action and IrisQueryRequest are imported directly by reducer.rs from handlers

// ═══════════════════════════════════════════════════════════════════════════════
// Core Event Types
// ═══════════════════════════════════════════════════════════════════════════════

/// Central event type - ALL state changes go through here
#[derive(Debug, Clone)]
pub enum StudioEvent {
    // ─────────────────────────────────────────────────────────────────────────
    // User Input Events
    // ─────────────────────────────────────────────────────────────────────────
    /// Key pressed (already filtered for Press kind)
    KeyPressed(KeyEvent),

    /// Mouse event (click, scroll, etc.)
    Mouse(MouseEvent),

    // ─────────────────────────────────────────────────────────────────────────
    // Navigation Events
    // ─────────────────────────────────────────────────────────────────────────
    /// Switch to a different mode
    SwitchMode(Mode),

    /// Focus a specific panel
    FocusPanel(PanelId),

    /// Cycle focus to next panel
    FocusNext,

    /// Cycle focus to previous panel
    FocusPrev,

    // ─────────────────────────────────────────────────────────────────────────
    // Content Generation Events (user-triggered)
    // ─────────────────────────────────────────────────────────────────────────
    /// Generate commit message
    GenerateCommit {
        instructions: Option<String>,
        preset: String,
        use_gitmoji: bool,
        amend: bool,
    },

    /// Toggle amend mode for commit
    ToggleAmendMode,

    /// Generate code review
    GenerateReview { from_ref: String, to_ref: String },

    /// Generate PR description
    GeneratePR { base_branch: String, to_ref: String },

    /// Generate changelog
    GenerateChangelog { from_ref: String, to_ref: String },

    /// Generate release notes
    GenerateReleaseNotes { from_ref: String, to_ref: String },

    /// Send chat message to Iris
    ChatMessage(String),

    // ─────────────────────────────────────────────────────────────────────────
    // Agent Response Events (from async tasks)
    // ─────────────────────────────────────────────────────────────────────────
    /// Agent task started
    AgentStarted { task_type: TaskType },

    /// Agent is making progress (tool call, etc.)
    AgentProgress {
        task_type: TaskType,
        tool_name: String,
        message: String,
    },

    /// Agent task completed successfully
    AgentComplete {
        task_type: TaskType,
        result: AgentResult,
    },

    /// Agent task failed
    AgentError { task_type: TaskType, error: String },

    /// Streaming text chunk received
    StreamingChunk {
        task_type: TaskType,
        chunk: String,
        aggregated: String,
    },

    /// Streaming completed (final response)
    StreamingComplete { task_type: TaskType },

    /// Dynamic status message received from fast model
    StatusMessage(crate::agents::StatusMessage),

    // ─────────────────────────────────────────────────────────────────────────
    // Tool-Triggered Events (agent controls UI)
    // ─────────────────────────────────────────────────────────────────────────
    /// Update content (from agent tool call)
    UpdateContent {
        content_type: ContentType,
        content: ContentPayload,
    },

    /// Load/refresh data for a mode
    LoadData {
        data_type: DataType,
        from_ref: Option<String>,
        to_ref: Option<String>,
    },

    /// Stage a file (agent can stage files)
    StageFile(PathBuf),

    /// Unstage a file
    UnstageFile(PathBuf),

    // ─────────────────────────────────────────────────────────────────────────
    // File & Git Events
    // ─────────────────────────────────────────────────────────────────────────
    /// File was staged successfully
    FileStaged(PathBuf),

    /// File was unstaged successfully
    FileUnstaged(PathBuf),

    /// Refresh git status
    RefreshGitStatus,

    /// Git status refreshed
    GitStatusRefreshed,

    /// Select a file in the tree
    SelectFile(PathBuf),

    /// File log loaded for explore mode
    FileLogLoaded {
        file: PathBuf,
        entries: Vec<crate::studio::state::FileLogEntry>,
    },

    /// File log loading started
    FileLogLoading(PathBuf),

    /// Global commit log loaded
    GlobalLogLoaded {
        entries: Vec<crate::studio::state::FileLogEntry>,
    },

    /// Global log loading started
    GlobalLogLoading,

    /// Toggle between file log and global log
    ToggleGlobalLog,

    // ─────────────────────────────────────────────────────────────────────────
    // Modal Events
    // ─────────────────────────────────────────────────────────────────────────
    /// Open a modal
    OpenModal(ModalType),

    /// Close current modal
    CloseModal,

    /// Modal action confirmed (e.g., commit confirmed)
    ModalConfirmed {
        modal_type: ModalType,
        data: Option<String>,
    },

    // ─────────────────────────────────────────────────────────────────────────
    // UI Events
    // ─────────────────────────────────────────────────────────────────────────
    /// Show a notification
    Notify {
        level: NotificationLevel,
        message: String,
    },

    /// Scroll content
    Scroll {
        direction: ScrollDirection,
        amount: usize,
    },

    /// Toggle editor mode (view <-> edit)
    ToggleEditMode,

    /// Cycle to next generated message variant
    NextMessageVariant,

    /// Cycle to previous generated message variant
    PrevMessageVariant,

    /// Copy content to clipboard
    CopyToClipboard(String),

    // ─────────────────────────────────────────────────────────────────────────
    // Settings Events
    // ─────────────────────────────────────────────────────────────────────────
    /// Change preset
    SetPreset(String),

    /// Toggle gitmoji
    ToggleGitmoji,

    /// Set custom emoji
    SetEmoji(String),

    // ─────────────────────────────────────────────────────────────────────────
    // Lifecycle Events
    // ─────────────────────────────────────────────────────────────────────────
    /// Request to quit the application
    Quit,

    /// Tick (for animations, polling)
    Tick,

    // ─────────────────────────────────────────────────────────────────────────
    // Companion Events (ambient awareness)
    // ─────────────────────────────────────────────────────────────────────────
    /// File was created in the repository
    CompanionFileCreated(PathBuf),

    /// File was modified in the repository
    CompanionFileModified(PathBuf),

    /// File was deleted from the repository
    CompanionFileDeleted(PathBuf),

    /// Git ref changed (branch switch, commit, etc.)
    CompanionGitRefChanged,

    /// File watcher error occurred
    CompanionWatcherError(String),

    /// Branch switched - show welcome message if returning
    CompanionBranchSwitch {
        branch: String,
        welcome_message: Option<String>,
    },
}

// ═══════════════════════════════════════════════════════════════════════════════
// Supporting Types
// ═══════════════════════════════════════════════════════════════════════════════

/// Types of agent tasks
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TaskType {
    Commit,
    Review,
    PR,
    Changelog,
    ReleaseNotes,
    Chat,
    SemanticBlame,
}

impl std::fmt::Display for TaskType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Commit => write!(f, "commit"),
            Self::Review => write!(f, "review"),
            Self::PR => write!(f, "pr"),
            Self::Changelog => write!(f, "changelog"),
            Self::ReleaseNotes => write!(f, "release_notes"),
            Self::Chat => write!(f, "chat"),
            Self::SemanticBlame => write!(f, "semantic_blame"),
        }
    }
}

/// Result from agent task completion
#[derive(Debug, Clone)]
pub enum AgentResult {
    /// Commit message(s) generated
    CommitMessages(Vec<GeneratedMessage>),

    /// Review content generated
    ReviewContent(String),

    /// PR description generated
    PRContent(String),

    /// Changelog generated
    ChangelogContent(String),

    /// Release notes generated
    ReleaseNotesContent(String),

    /// Chat response
    ChatResponse(String),

    /// Semantic blame explanation
    SemanticBlame(SemanticBlameResult),
}

/// Result from semantic blame query
#[derive(Debug, Clone)]
pub struct SemanticBlameResult {
    /// The file that was analyzed
    pub file: PathBuf,
    /// Line range analyzed
    pub start_line: usize,
    pub end_line: usize,
    /// The commit that introduced this code
    pub commit_hash: String,
    /// Commit author
    pub author: String,
    /// When the commit was made
    pub commit_date: String,
    /// Original commit message
    pub commit_message: String,
    /// AI-generated explanation of why this code exists
    pub explanation: String,
}

/// Types of content that can be updated
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ContentType {
    CommitMessage,
    PRDescription,
    CodeReview,
    Changelog,
    ReleaseNotes,
}

impl std::fmt::Display for ContentType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::CommitMessage => write!(f, "commit_message"),
            Self::PRDescription => write!(f, "pr_description"),
            Self::CodeReview => write!(f, "code_review"),
            Self::Changelog => write!(f, "changelog"),
            Self::ReleaseNotes => write!(f, "release_notes"),
        }
    }
}

/// Content payload for updates
#[derive(Debug, Clone)]
pub enum ContentPayload {
    /// Structured commit message
    Commit(GeneratedMessage),

    /// Markdown content (PR, review, changelog, release notes)
    Markdown(String),
}

/// Types of data that can be loaded
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DataType {
    GitStatus,
    CommitDiff,
    ReviewDiff,
    PRDiff,
    ChangelogCommits,
    ReleaseNotesCommits,
    /// Explore mode file tree (lazy-loaded on mode switch)
    ExploreFiles,
}

/// Modal types
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ModalType {
    Help,
    Chat,
    Settings,
    PresetSelector,
    EmojiSelector,
    RefSelector { field: RefField },
    ConfirmCommit,
    ConfirmAmend,
    ConfirmQuit,
}

/// Which ref field is being edited
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RefField {
    From,
    To,
    Base,
}

/// Notification severity levels
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NotificationLevel {
    Info,
    Success,
    Warning,
    Error,
}

/// Scroll direction
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScrollDirection {
    Up,
    Down,
    PageUp,
    PageDown,
    Top,
    Bottom,
}

// ═══════════════════════════════════════════════════════════════════════════════
// Side Effects
// ═══════════════════════════════════════════════════════════════════════════════

/// Side effects produced by the reducer
///
/// These are executed by the app after state is updated.
/// This keeps the reducer pure (no I/O).
#[derive(Debug, Clone)]
pub enum SideEffect {
    /// Spawn an agent task
    SpawnAgent { task: AgentTask },

    /// Load data asynchronously
    LoadData {
        data_type: DataType,
        from_ref: Option<String>,
        to_ref: Option<String>,
    },

    /// Stage a file in git
    GitStage(PathBuf),

    /// Unstage a file in git
    GitUnstage(PathBuf),

    /// Stage all files
    GitStageAll,

    /// Unstage all files
    GitUnstageAll,

    /// Save settings to config
    SaveSettings,

    /// Refresh git status
    RefreshGitStatus,

    /// Copy to system clipboard
    CopyToClipboard(String),

    /// Execute git commit
    ExecuteCommit { message: String },

    /// Execute git commit --amend
    ExecuteAmend { message: String },

    /// Show notification (if needs timing/animation)
    #[allow(dead_code)] // Kept for future use - handled in executor but not yet constructed
    ShowNotification {
        level: NotificationLevel,
        message: String,
        duration_ms: u64,
    },

    /// Request terminal redraw
    #[allow(dead_code)] // Kept for future use - handled in executor but not yet constructed
    Redraw,

    /// Quit the application
    Quit,

    /// Gather blame info for a file region and spawn semantic blame agent
    GatherBlameAndSpawnAgent {
        file: PathBuf,
        start_line: usize,
        end_line: usize,
    },

    /// Load file git log for the right panel in explore mode
    LoadFileLog(PathBuf),

    /// Load global commit log (not file-specific)
    LoadGlobalLog,
}

/// Blame information gathered from git
#[derive(Debug, Clone)]
pub struct BlameInfo {
    /// The file that was blamed
    pub file: PathBuf,
    /// Start line of the region
    pub start_line: usize,
    /// End line of the region
    pub end_line: usize,
    /// Commit hash that introduced the code
    pub commit_hash: String,
    /// Author name
    pub author: String,
    /// Commit date
    pub commit_date: String,
    /// Original commit message
    pub commit_message: String,
    /// The actual code content in the range
    pub code_content: String,
}

/// Agent task to spawn
#[derive(Debug, Clone)]
pub enum AgentTask {
    Commit {
        instructions: Option<String>,
        preset: String,
        use_gitmoji: bool,
        amend: bool,
    },
    Review {
        from_ref: String,
        to_ref: String,
    },
    PR {
        base_branch: String,
        to_ref: String,
    },
    Changelog {
        from_ref: String,
        to_ref: String,
    },
    ReleaseNotes {
        from_ref: String,
        to_ref: String,
    },
    Chat {
        message: String,
        context: ChatContext,
    },
    #[allow(dead_code)]
    // Kept for future use - currently GatherBlameAndSpawnAgent handles this directly
    SemanticBlame {
        blame_info: BlameInfo,
    },
}

/// Context for chat messages
#[derive(Debug, Clone, Default)]
pub struct ChatContext {
    /// Current mode when chat was opened
    pub mode: Mode,
    /// Current content being discussed
    pub current_content: Option<String>,
    /// Diff summary for context
    #[allow(dead_code)] // Kept for future use - will provide diff context to chat
    pub diff_summary: Option<String>,
}

// ═══════════════════════════════════════════════════════════════════════════════
// Event Source Tracking
// ═══════════════════════════════════════════════════════════════════════════════

/// Where an event originated from
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventSource {
    /// User input (keyboard, mouse)
    User,
    /// Agent/LLM response
    Agent,
    /// Tool call from agent
    Tool,
    /// System (tick, refresh, etc.)
    System,
}

/// Timestamped event for history
#[derive(Debug, Clone)]
pub struct TimestampedEvent {
    pub timestamp: Instant,
    pub source: EventSource,
    pub event: StudioEvent,
}

impl TimestampedEvent {
    pub fn user(event: StudioEvent) -> Self {
        Self {
            timestamp: Instant::now(),
            source: EventSource::User,
            event,
        }
    }

    pub fn agent(event: StudioEvent) -> Self {
        Self {
            timestamp: Instant::now(),
            source: EventSource::Agent,
            event,
        }
    }

    pub fn tool(event: StudioEvent) -> Self {
        Self {
            timestamp: Instant::now(),
            source: EventSource::Tool,
            event,
        }
    }

    pub fn system(event: StudioEvent) -> Self {
        Self {
            timestamp: Instant::now(),
            source: EventSource::System,
            event,
        }
    }
}