Skip to content

Commit

Permalink
chore: fix all lints and formats (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
baxen authored Nov 25, 2024
1 parent 89b051c commit 8d9fc0d
Show file tree
Hide file tree
Showing 24 changed files with 258 additions and 570 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ on:
- v1.0

jobs:
format:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
override: true

- name: check format
run: |
cargo fmt --check
build-and-test:
runs-on: ubuntu-latest

Expand Down Expand Up @@ -79,3 +97,7 @@ jobs:
run: cargo test --verbose
env:
OLLAMA_MODEL: "qwen2.5"

- name: check lint
run: |
cargo clippy
2 changes: 1 addition & 1 deletion crates/goose-cli/src/agents/mock_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct MockAgent;
#[async_trait]
impl Agent for MockAgent {
fn add_system(&mut self, _system: Box<dyn System>) {
();
()
}

async fn reply(&self, _messages: &[Message]) -> Result<BoxStream<'_, Result<Message>>> {
Expand Down
13 changes: 6 additions & 7 deletions crates/goose-cli/src/commands/configure.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::commands::expected_config::{get_recommended_models, RecommendedModels};
use crate::inputs::inputs::get_user_input;
use crate::profile::profile::Profile;
use crate::profile::profile_handler::{find_existing_profile, profile_path, save_profile};
use crate::profile::provider_helper::{
select_provider_lists, set_provider_config, PROVIDER_OPEN_AI,
use crate::inputs::get_user_input;
use crate::profile::{
find_existing_profile, profile_path, save_profile, select_provider_lists, set_provider_config,
Profile, PROVIDER_OPEN_AI,
};
use cliclack::spinner;
use console::style;
Expand Down Expand Up @@ -58,8 +57,8 @@ async fn check_configuration(provider_config: ProviderConfig) -> Result<(), Box<
Ok(())
}

fn get_existing_profile(profile_name: &String) -> Option<Profile> {
let existing_profile_result = find_existing_profile(profile_name.as_str());
fn get_existing_profile(profile_name: &str) -> Option<Profile> {
let existing_profile_result = find_existing_profile(profile_name);
if existing_profile_result.is_some() {
println!("Profile already exists. We are going to overwriting the existing profile...");
} else {
Expand Down
10 changes: 7 additions & 3 deletions crates/goose-cli/src/commands/expected_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This is a temporary file to simulate some configuration data from the backend

use crate::profile::provider_helper::{PROVIDER_DATABRICKS, PROVIDER_OLLAMA, PROVIDER_OPEN_AI};
use crate::profile::{PROVIDER_DATABRICKS, PROVIDER_OLLAMA, PROVIDER_OPEN_AI};
use goose::providers::ollama::OLLAMA_MODEL;

pub struct RecommendedModels {
Expand All @@ -11,9 +11,13 @@ pub fn get_recommended_models(provider_name: &str) -> RecommendedModels {
if provider_name == PROVIDER_OPEN_AI {
RecommendedModels { model: "gpt-4o" }
} else if provider_name == PROVIDER_DATABRICKS {
RecommendedModels { model: "claude-3-5-sonnet-2" }
RecommendedModels {
model: "claude-3-5-sonnet-2",
}
} else if provider_name == PROVIDER_OLLAMA {
RecommendedModels { model: OLLAMA_MODEL }
RecommendedModels {
model: OLLAMA_MODEL,
}
} else {
panic!("Invalid provider name");
}
Expand Down
12 changes: 5 additions & 7 deletions crates/goose-cli/src/commands/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ use goose::models::message::Message;
use goose::providers::factory;

use crate::commands::expected_config::get_recommended_models;
use crate::profile::profile::Profile;
use crate::profile::profile_handler::{load_profiles, PROFILE_DEFAULT_NAME};
use crate::profile::provider_helper::set_provider_config;
use crate::profile::provider_helper::PROVIDER_OPEN_AI;
use crate::profile::{
load_profiles, set_provider_config, Profile, PROFILE_DEFAULT_NAME, PROVIDER_OPEN_AI,
};
use crate::prompt::cliclack::CliclackPrompt;
use crate::prompt::prompt::Prompt;
use crate::prompt::rustyline::RustylinePrompt;
use crate::prompt::thinking::get_random_goose_action;
use crate::session::session::Session;
use crate::session::session_file::ensure_session_dir;
use crate::prompt::Prompt;
use crate::session::{ensure_session_dir, Session};

pub fn build_session<'a>(
session: Option<String>,
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion crates/goose-cli/src/inputs/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/goose-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ enum SystemCommands {
enum CliProviderVariant {
OpenAi,
Databricks,
Ollama
Ollama,
}

#[tokio::main]
Expand Down
131 changes: 131 additions & 0 deletions crates/goose-cli/src/profile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
use std::collections::HashMap;
use std::error::Error;
use std::fs;
use std::path::PathBuf;

use crate::inputs::get_env_value_or_input;
use goose::providers::configs::{
DatabricksAuth, DatabricksProviderConfig, OllamaProviderConfig, OpenAiProviderConfig,
ProviderConfig,
};
use goose::providers::factory::ProviderType;
use goose::providers::ollama::OLLAMA_HOST;
use serde::{Deserialize, Serialize};
use strum::IntoEnumIterator;

// Profile types and structures
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Profile {
pub provider: String,
pub model: String,
#[serde(default)]
pub additional_systems: Vec<AdditionalSystem>,
}

#[derive(Serialize, Deserialize)]
pub struct Profiles {
pub profile_items: HashMap<String, Profile>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct AdditionalSystem {
pub name: String,
pub location: String,
}

// Provider helper constants and functions
pub const PROVIDER_OPEN_AI: &str = "openai";
pub const PROVIDER_DATABRICKS: &str = "databricks";
pub const PROVIDER_OLLAMA: &str = "ollama";
pub const PROFILE_DEFAULT_NAME: &str = "default";

pub fn select_provider_lists() -> Vec<(&'static str, String, &'static str)> {
ProviderType::iter()
.map(|provider| match provider {
ProviderType::OpenAi => (
PROVIDER_OPEN_AI,
PROVIDER_OPEN_AI.to_string(),
"Recommended",
),
ProviderType::Databricks => (PROVIDER_DATABRICKS, PROVIDER_DATABRICKS.to_string(), ""),
ProviderType::Ollama => (PROVIDER_OLLAMA, PROVIDER_OLLAMA.to_string(), ""),
})
.collect()
}

pub fn profile_path() -> Result<PathBuf, Box<dyn Error>> {
let home_dir = dirs::home_dir().ok_or(anyhow::anyhow!("Could not determine home directory"))?;
let config_dir = home_dir.join(".config").join("goose");
if !config_dir.exists() {
fs::create_dir_all(&config_dir)?;
}
Ok(config_dir.join("profiles.json"))
}

pub fn load_profiles() -> Result<HashMap<String, Profile>, Box<dyn Error>> {
let path = profile_path()?;
if !path.exists() {
return Ok(HashMap::new());
}
let content = fs::read_to_string(path)?;
let profiles: Profiles = serde_json::from_str(&content)?;
Ok(profiles.profile_items)
}

pub fn save_profile(name: &str, profile: Profile) -> Result<(), Box<dyn Error>> {
let path = profile_path()?;
let mut profiles = load_profiles()?;
profiles.insert(name.to_string(), profile);
let profiles = Profiles {
profile_items: profiles,
};
let content = serde_json::to_string_pretty(&profiles)?;
fs::write(path, content)?;
Ok(())
}

pub fn find_existing_profile(name: &str) -> Option<Profile> {
match load_profiles() {
Ok(profiles) => profiles.get(name).cloned(),
Err(_) => None,
}
}

pub fn set_provider_config(provider_name: &str, model: String) -> ProviderConfig {
match provider_name.to_lowercase().as_str() {
PROVIDER_OPEN_AI => ProviderConfig::OpenAi(OpenAiProviderConfig {
host: "https://api.openai.com".to_string(),
api_key: get_env_value_or_input(
"OPENAI_API_KEY",
"Please enter your OpenAI API key:",
true,
),
model,
temperature: None,
max_tokens: None,
}),
PROVIDER_DATABRICKS => {
let host = get_env_value_or_input(
"DATABRICKS_HOST",
"Please enter your Databricks host:",
false,
);
ProviderConfig::Databricks(DatabricksProviderConfig {
host: host.clone(),
// TODO revisit configuration
auth: DatabricksAuth::oauth(host),
model,
temperature: None,
max_tokens: None,
image_format: goose::providers::utils::ImageFormat::Anthropic,
})
}
PROVIDER_OLLAMA => ProviderConfig::Ollama(OllamaProviderConfig {
host: std::env::var("OLLAMA_HOST").unwrap_or_else(|_| String::from(OLLAMA_HOST)),
model,
temperature: None,
max_tokens: None,
}),
_ => panic!("Invalid provider name"),
}
}
3 changes: 0 additions & 3 deletions crates/goose-cli/src/profile/mod.rs

This file was deleted.

20 changes: 0 additions & 20 deletions crates/goose-cli/src/profile/profile.rs

This file was deleted.

63 changes: 0 additions & 63 deletions crates/goose-cli/src/profile/profile_handler.rs

This file was deleted.

Loading

0 comments on commit 8d9fc0d

Please sign in to comment.