Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions crates/ai/src/claude_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use std::sync::atomic::AtomicBool;

use serde_json::json;

use crate::provider::{self, AiError, Provider, Request};
use crate::error::AiError;
use crate::provider::{self, Provider, Request};
use crate::transport::Transport;

const URL: &str = "https://api.anthropic.com/v1/messages";
Expand Down Expand Up @@ -159,11 +160,11 @@ mod tests {
crate::secrets::store_token(&secrets, &token()).unwrap();
let outputs = [
format!("{:?}", crate::secrets::load_token(&secrets)),
format!("{:?}", crate::provider::AiError::Secrets("x".into())),
format!("{:?}", crate::error::AiError::Secrets("x".into())),
format!("{:?}", token()),
format!("{:#?}", ClaudeApi::new(token(), Some("m".into()), Arc::new(UreqTransport))),
format!("{:?}", req()),
format!("{:?}", crate::provider::AiError::Status(401, "x".into())),
format!("{:?}", crate::error::AiError::Status(401, "x".into())),
format!("{:?}", crate::ollama::Ollama::new("m", Arc::new(UreqTransport))),
format!("{:?}", crate::claude_cli::ClaudeCli::new("/usr/bin/false".into(), None)),
];
Expand Down
3 changes: 2 additions & 1 deletion crates/ai/src/claude_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use std::time::Duration;

use serde_json::json;

use crate::provider::{AiError, Provider, Request};
use crate::error::AiError;
use crate::provider::{Provider, Request};

const HELPER: &str = "tarn-cli-bridge";

Expand Down
30 changes: 30 additions & 0 deletions crates/ai/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::fmt;

#[derive(Debug)]
pub enum AiError {
Cancelled,
Network(String),
Status(u16, String),
Protocol(String),
Refused,
Secrets(String),
Cli(String),
Unavailable(String),
}

impl fmt::Display for AiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Cancelled => f.write_str("cancelled"),
Self::Network(e) => write!(f, "network error: {e}"),
Self::Status(code, msg) => write!(f, "HTTP {code}: {msg}"),
Self::Protocol(e) => write!(f, "unexpected response: {e}"),
Self::Refused => f.write_str("the model declined to answer"),
Self::Secrets(e) => write!(f, "secret store: {e}"),
Self::Cli(e) => write!(f, "claude cli: {e}"),
Self::Unavailable(why) => f.write_str(why),
}
}
}

impl std::error::Error for AiError {}
4 changes: 3 additions & 1 deletion crates/ai/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pub mod claude_api;
pub mod claude_cli;
pub mod error;
pub mod grouping;
pub mod local;
pub mod markdown;
Expand All @@ -13,10 +14,11 @@ pub mod transport;

pub use claude_api::{ApiToken, ClaudeApi};
pub use claude_cli::ClaudeCli;
pub use error::AiError;
pub use local::{Availability, LocalModel};
pub use ollama::Ollama;
pub use page_context::PageContext;
pub use provider::{AiError, Provider, Request};
pub use provider::{Provider, Request};
pub use secrets::Secrets;
pub use transport::Transport;
#[cfg(not(target_os = "android"))]
Expand Down
5 changes: 3 additions & 2 deletions crates/ai/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use std::sync::atomic::AtomicBool;

use crate::provider::{AiError, Provider, Request};
use crate::error::AiError;
use crate::provider::{Provider, Request};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Availability {
Expand Down Expand Up @@ -102,7 +103,7 @@ mod tests {
use std::sync::{Arc, Mutex};

use super::*;
use crate::Ollama;
use crate::ollama::Ollama;
use crate::transport::{Response, Transport};

/// Answers `/api/show` with `show` (none: Ollama is not running) and `/api/chat` with one word,
Expand Down
3 changes: 2 additions & 1 deletion crates/ai/src/ollama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use std::sync::atomic::AtomicBool;

use serde_json::json;

use crate::error::AiError;
use crate::local::{Availability, Checked, LocalModel};
use crate::provider::{self, AiError, Request};
use crate::provider::{self, Request};
use crate::transport::Transport;

const BASE: &str = "http://127.0.0.1:11434";
Expand Down
31 changes: 1 addition & 30 deletions crates/ai/src/provider.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;
use std::io::{BufRead, Read};
use std::sync::atomic::{AtomicBool, Ordering};

use crate::error::AiError;
use crate::page_context::PageContext;
use crate::transport::{self, Transport};

Expand All @@ -20,35 +20,6 @@ pub trait Provider {
fn stream(&self, req: &Request, cancel: &AtomicBool, on_text: &mut dyn FnMut(&str)) -> Result<(), AiError>;
}

#[derive(Debug)]
pub enum AiError {
Cancelled,
Network(String),
Status(u16, String),
Protocol(String),
Refused,
Secrets(String),
Cli(String),
Unavailable(String),
}

impl fmt::Display for AiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Cancelled => f.write_str("cancelled"),
Self::Network(e) => write!(f, "network error: {e}"),
Self::Status(code, msg) => write!(f, "HTTP {code}: {msg}"),
Self::Protocol(e) => write!(f, "unexpected response: {e}"),
Self::Refused => f.write_str("the model declined to answer"),
Self::Secrets(e) => write!(f, "secret store: {e}"),
Self::Cli(e) => write!(f, "claude cli: {e}"),
Self::Unavailable(why) => f.write_str(why),
}
}
}

impl std::error::Error for AiError {}

impl Request {
pub fn user_message(&self) -> String {
match &self.page {
Expand Down
8 changes: 4 additions & 4 deletions crates/ai/src/secrets.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::claude_api::ApiToken;
use crate::provider::AiError;
use crate::error::AiError;

const SERVICE: &str = "de.quavon.tarn";
const ACCOUNT: &str = "anthropic-api-token";
Expand Down Expand Up @@ -35,7 +35,7 @@ mod apple {
use security_framework::passwords::{delete_generic_password, get_generic_password};

use super::{Keyring, Secrets};
use crate::provider::AiError;
use crate::error::AiError;

const ERR_SEC_ITEM_NOT_FOUND: i32 = -25300;

Expand Down Expand Up @@ -77,7 +77,7 @@ mod apple {
#[cfg(windows)]
mod windows {
use super::{Keyring, Secrets};
use crate::provider::AiError;
use crate::error::AiError;

fn target(service: &str, account: &str) -> String {
format!("{service}/{account}")
Expand Down Expand Up @@ -110,7 +110,7 @@ mod linux {
use oo7::dbus::{Collection, Service};

use super::{Keyring, Secrets};
use crate::provider::AiError;
use crate::error::AiError;

fn failed(e: oo7::dbus::Error) -> AiError {
AiError::Secrets(e.to_string())
Expand Down
4 changes: 2 additions & 2 deletions crates/ai/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::atomic::{AtomicBool, Ordering};

use url::{Host, Url};

use crate::provider::AiError;
use crate::error::AiError;

pub struct Response<'a> {
pub status: u16,
Expand Down Expand Up @@ -63,7 +63,7 @@ mod ureq_transport {
use ureq::tls::{RootCerts, TlsConfig};

use super::{Response, Transport, guard};
use crate::provider::AiError;
use crate::error::AiError;

/// Talks TLS itself with the platform's certificate verifier; Android's needs JNI setup, so
/// the Android host brings its own transport.
Expand Down
Loading
Loading