Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dc64ff9

Browse files
refactor: update UI initialization to use a function for API creation (tailcallhq#982)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 7885a12 commit dc64ff9

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

crates/forge_main/src/main.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::sync::Arc;
2-
31
use anyhow::Result;
42
use clap::Parser;
53
use forge_api::ForgeAPI;
@@ -10,8 +8,8 @@ async fn main() -> Result<()> {
108
// Initialize and run the UI
119
let cli = Cli::parse();
1210
// Initialize the ForgeAPI with the restricted mode if specified
13-
let api = Arc::new(ForgeAPI::init(cli.restricted));
14-
let mut ui = UI::init(cli, api)?;
11+
let restricted = cli.restricted;
12+
let mut ui = UI::init(cli, move || ForgeAPI::init(restricted))?;
1513
ui.run().await;
1614

1715
Ok(())

crates/forge_main/src/ui.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ impl From<PartialEvent> for Event {
5252
}
5353
}
5454

55-
pub struct UI<F> {
55+
pub struct UI<A, F: Fn() -> A> {
5656
markdown: MarkdownFormat,
5757
state: UIState,
58-
api: Arc<F>,
58+
api: Arc<F::Output>,
59+
new_api: Arc<F>,
5960
console: Console,
6061
command: Arc<ForgeCommandManager>,
6162
cli: Cli,
@@ -64,7 +65,7 @@ pub struct UI<F> {
6465
_guard: forge_tracker::Guard,
6566
}
6667

67-
impl<F: API> UI<F> {
68+
impl<A: API, F: Fn() -> A> UI<A, F> {
6869
/// Writes a line to the console output
6970
/// Takes anything that implements ToString trait
7071
fn writeln<T: ToString>(&mut self, content: T) -> anyhow::Result<()> {
@@ -141,13 +142,15 @@ impl<F: API> UI<F> {
141142
))
142143
}
143144

144-
pub fn init(cli: Cli, api: Arc<F>) -> Result<Self> {
145+
pub fn init(cli: Cli, f: F) -> Result<Self> {
145146
// Parse CLI arguments first to get flags
147+
let api = Arc::new(f());
146148
let env = api.environment();
147149
let command = Arc::new(ForgeCommandManager::default());
148150
Ok(Self {
149151
state: Default::default(),
150152
api,
153+
new_api: Arc::new(f),
151154
console: Console::new(env.clone(), command.clone()),
152155
cli,
153156
command,
@@ -552,6 +555,8 @@ impl<F: API> UI<F> {
552555

553556
/// Initialize the state of the UI
554557
async fn init_state(&mut self) -> Result<Workflow> {
558+
self.api = Arc::new((self.new_api)());
559+
555560
let mut workflow = self.api.read_workflow(self.cli.workflow.as_deref()).await?;
556561
if workflow.model.is_none() {
557562
workflow.model = Some(

0 commit comments

Comments
 (0)