use lazy_static::lazy_static;
use std::thread::sleep;
use std::time::Duration;
pub mod chat;
lazy_static! {
static ref CLAUDE_API_PREFIX: String = String::from(
std::option_env!("CLAUDE_API_PREFIX").unwrap_or("https://claude.flows.network/api")
);
}
extern "C" {
fn get_flows_user(p: *mut u8) -> i32;
fn get_flow_id(p: *mut u8) -> i32;
}
unsafe fn _get_flows_user() -> String {
let mut flows_user = Vec::<u8>::with_capacity(100);
let c = get_flows_user(flows_user.as_mut_ptr());
flows_user.set_len(c as usize);
String::from_utf8(flows_user).unwrap()
}
unsafe fn _get_flow_id() -> String {
let mut flow_id = Vec::<u8>::with_capacity(100);
let c = get_flow_id(flow_id.as_mut_ptr());
if c == 0 {
panic!("Failed to get flow id");
}
flow_id.set_len(c as usize);
String::from_utf8(flow_id).unwrap()
}
const MAX_RETRY_TIMES: u8 = 10;
const RETRY_INTERVAL: u64 = 10;
pub enum FlowsAccount {
Default,
Provided(String),
}
pub struct ClaudeFlows {
account: FlowsAccount,
retry_times: u8,
}
impl ClaudeFlows {
pub fn new() -> ClaudeFlows {
ClaudeFlows {
account: FlowsAccount::Default,
retry_times: 0,
}
}
pub fn set_flows_account(&mut self, account: FlowsAccount) {
self.account = account;
}
pub fn set_retry_times(&mut self, retry_times: u8) {
self.retry_times = retry_times;
}
fn keep_trying<T, F>(&self, f: F) -> Result<T, String>
where
F: Fn(&str) -> Retry<T>,
{
let account = match &self.account {
FlowsAccount::Default => "",
FlowsAccount::Provided(s) => s.as_str(),
};
let mut retry_times = match self.retry_times {
r if r > MAX_RETRY_TIMES => MAX_RETRY_TIMES,
r => r,
};
loop {
match f(account) {
Retry::Yes(s) => match retry_times > 0 {
true => {
sleep(Duration::from_secs(crate::RETRY_INTERVAL));
retry_times = retry_times - 1;
continue;
}
false => return Err(s),
},
Retry::No(r) => return r,
}
}
}
}
enum Retry<T> {
Yes(String),
No(Result<T, String>),
}