11use anyhow:: Result ;
22use clap:: Parser ;
3- use colored:: Colorize ;
4- use forge_app:: Routes ;
5- use forge_domain:: { ChatRequest , ChatResponse , Command , ModelId , UserInput } ;
6- use forge_main:: { display_info, Console , StatusDisplay , CONSOLE } ;
7- use tokio_stream:: StreamExt ;
8-
9- fn context_reset_message ( _: & Command ) -> String {
10- "All context was cleared, and we're starting fresh. Please re-add files and details so we can get started." . to_string ( )
11- . yellow ( )
12- . bold ( )
13- . to_string ( )
14- }
3+ use forge_main:: { banner, UI } ;
154
165/// Command line arguments for the application
176#[ derive( Parser ) ]
@@ -28,141 +17,11 @@ async fn main() -> Result<()> {
2817 let cli = Cli :: parse ( ) ;
2918
3019 // Display the banner in dimmed colors
31- forge_main:: banner:: display ( ) ?;
32-
33- let mut current_conversation_id = None ;
34- let mut current_title = None ;
35- let mut current_content = None ;
36-
37- let api = Routes :: init ( )
38- . await
39- . map_err ( |e| anyhow:: anyhow!( "Failed to initialize API: {}" , e) ) ?;
40-
41- // Create a Console instance
42- let console = Console ;
43-
44- // Get initial input from file or prompt
45- let mut input = match & cli. exec {
46- Some ( ref path) => console. upload ( path) . await ?,
47- None => console. prompt ( None , None ) . await ?,
48- } ;
49- let model = ModelId :: from_env ( & api. environment ( ) . await ?) ;
50- loop {
51- match input {
52- Command :: End => break ,
53- Command :: New => {
54- CONSOLE . writeln ( context_reset_message ( & input) ) ?;
55- current_conversation_id = None ;
56- current_title = None ;
57- input = console. prompt ( None , None ) . await ?;
58- continue ;
59- }
60- Command :: Reload => {
61- CONSOLE . writeln ( context_reset_message ( & input) ) ?;
62- current_conversation_id = None ;
63- current_title = None ;
64- input = match cli. exec {
65- Some ( ref path) => console. upload ( path) . await ?,
66- None => console. prompt ( None , current_content. as_deref ( ) ) . await ?,
67- } ;
68- continue ;
69- }
70- Command :: Info => {
71- display_info ( & api. environment ( ) . await ?) ?;
72- input = console. prompt ( current_title. as_deref ( ) , None ) . await ?;
73- continue ;
74- }
75- Command :: Message ( ref content) => {
76- current_content = Some ( content. clone ( ) ) ;
77- let chat = ChatRequest {
78- content : content. clone ( ) ,
79- model : model. clone ( ) ,
80- conversation_id : current_conversation_id,
81- } ;
82-
83- match api. chat ( chat) . await {
84- Ok ( mut stream) => {
85- while let Some ( message) = stream. next ( ) . await {
86- match message {
87- Ok ( message) => match message {
88- ChatResponse :: Text ( text) => {
89- CONSOLE . write ( & text) ?;
90- }
91- ChatResponse :: ToolCallDetected ( _) => { }
92- ChatResponse :: ToolCallArgPart ( arg) => {
93- if cli. verbose {
94- CONSOLE . write ( & arg) ?;
95- }
96- }
97- ChatResponse :: ToolCallStart ( tool_call_full) => {
98- let tool_name = tool_call_full. name . as_str ( ) ;
99- CONSOLE . newline ( ) ?;
100- CONSOLE
101- . writeln ( StatusDisplay :: execute ( tool_name) . format ( ) ) ?;
102-
103- // Convert to JSON and apply dimmed style
104- let json =
105- serde_json:: to_string_pretty ( & tool_call_full. arguments )
106- . unwrap_or_else ( |_| {
107- "Failed to serialize arguments" . to_string ( )
108- } ) ;
109-
110- CONSOLE . writeln ( format ! ( "{}" , json. dimmed( ) ) ) ?;
111- }
112- ChatResponse :: ToolCallEnd ( tool_result) => {
113- let tool_name = tool_result. name . as_str ( ) ;
114- // Always show result content for errors, or in verbose mode
115- if tool_result. is_error || cli. verbose {
116- CONSOLE . writeln ( format ! (
117- "{}" ,
118- tool_result. to_string( ) . dimmed( )
119- ) ) ?;
120- }
121- let status = if tool_result. is_error {
122- StatusDisplay :: failed ( tool_name)
123- } else {
124- StatusDisplay :: success ( tool_name)
125- } ;
126- CONSOLE . write ( status. format ( ) ) ?;
127- }
128- ChatResponse :: ConversationStarted ( conversation_id) => {
129- current_conversation_id = Some ( conversation_id) ;
130- }
131- ChatResponse :: ModifyContext ( _) => { }
132- ChatResponse :: Complete => { }
133- ChatResponse :: Error ( err) => {
134- CONSOLE . writeln (
135- StatusDisplay :: failed ( err. to_string ( ) ) . format ( ) ,
136- ) ?;
137- }
138- ChatResponse :: PartialTitle ( _) => { }
139- ChatResponse :: CompleteTitle ( title) => {
140- current_title = Some ( StatusDisplay :: title ( title) . format ( ) ) ;
141- }
142- ChatResponse :: FinishReason ( _) => { }
143- } ,
144- Err ( err) => {
145- CONSOLE
146- . writeln ( StatusDisplay :: failed ( err. to_string ( ) ) . format ( ) ) ?;
147- }
148- }
149- }
150- }
151- Err ( err) => {
152- CONSOLE . writeln (
153- StatusDisplay :: failed_with (
154- err. to_string ( ) ,
155- "Failed to establish chat stream" ,
156- )
157- . format ( ) ,
158- ) ?;
159- }
160- }
20+ banner:: display ( ) ?;
16121
162- input = console. prompt ( current_title. as_deref ( ) , None ) . await ?;
163- }
164- }
165- }
22+ // Initialize and run the UI
23+ let mut ui = UI :: new ( cli. verbose , cli. exec ) . await ?;
24+ ui. run ( ) . await ?;
16625
16726 Ok ( ( ) )
16827}
0 commit comments