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: 5 additions & 2 deletions aiscript-runtime/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{env, fmt::Display, fs, ops::Deref, path::Path, sync::OnceLock};
use auth::AuthConfig;
use serde::Deserialize;

use aiscript_vm::AiConfig;
use db::DatabaseConfig;
pub use sso::{SsoConfig, get_sso_fields};

Expand Down Expand Up @@ -64,6 +65,8 @@ impl AsRef<str> for EnvString {

#[derive(Debug, Deserialize, Default)]
pub struct Config {
#[serde(default)]
pub ai: Option<AiConfig>,
#[serde(default)]
pub database: DatabaseConfig,
#[serde(default)]
Expand Down Expand Up @@ -116,9 +119,9 @@ impl Config {
}
}

pub fn load(path: &str) -> &Config {
pub fn load() -> &'static Config {
CONFIG.get_or_init(|| {
Config::new(path).unwrap_or_else(|e| {
Config::new("project.toml").unwrap_or_else(|e| {
eprintln!("Error loading config file: {}", e);
Config::default()
})
Expand Down
9 changes: 7 additions & 2 deletions aiscript-runtime/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,13 @@ impl Future for RequestProcessor {
let redis_connection = self.endpoint.redis_connection.clone();
let handle: JoinHandle<Result<ReturnValue, VmError>> =
task::spawn_blocking(move || {
let mut vm =
Vm::new(pg_connection, sqlite_connection, redis_connection);
let ai_config = Config::load().ai.clone();
let mut vm = Vm::new(
pg_connection,
sqlite_connection,
redis_connection,
ai_config,
);
if let Some(fields) = sso_fields {
vm.inject_sso_instance(fields);
}
Expand Down
15 changes: 15 additions & 0 deletions aiscript-vm/src/ai/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ pub use agent::{Agent, run_agent};
use openai_api_rs::v1::api::OpenAIClient;
pub use prompt::{PromptConfig, prompt_with_config};

use serde::Deserialize;

#[derive(Debug, Clone, Deserialize, Default)]
pub struct AiConfig {
pub openai: Option<ModelConfig>,
pub anthropic: Option<ModelConfig>,
pub deepseek: Option<ModelConfig>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct ModelConfig {
pub api_key: String,
pub model: Option<String>,
}

#[allow(unused)]
pub(crate) fn openai_client() -> OpenAIClient {
OpenAIClient::builder()
Expand Down
1 change: 1 addition & 0 deletions aiscript-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::collections::HashMap;
use std::fmt::Display;
use std::ops::Deref;

pub use ai::AiConfig;
use aiscript_arena::Collect;
use aiscript_arena::Mutation;
pub(crate) use aiscript_lexer as lexer;
Expand Down
5 changes: 4 additions & 1 deletion aiscript-vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub use state::State;

use crate::{
ReturnValue, Value,
ai::AiConfig,
ast::ChunkId,
builtins, stdlib,
string::{InternedString, InternedStringSet},
Expand Down Expand Up @@ -35,7 +36,7 @@ impl Display for VmError {

impl Default for Vm {
fn default() -> Self {
Self::new(None, None, None)
Self::new(None, None, None, None)
}
}

Expand All @@ -48,13 +49,15 @@ impl Vm {
pg_connection: Option<PgPool>,
sqlite_connection: Option<SqlitePool>,
redis_connection: Option<redis::aio::MultiplexedConnection>,
ai_config: Option<AiConfig>,
) -> Self {
let mut vm = Vm {
arena: Arena::<Rootable![State<'_>]>::new(|mc| {
let mut state = State::new(mc);
state.pg_connection = pg_connection;
state.sqlite_connection = sqlite_connection;
state.redis_connection = redis_connection;
state.ai_config = ai_config;
state
}),
};
Expand Down
4 changes: 3 additions & 1 deletion aiscript-vm/src/vm/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use sqlx::{PgPool, SqlitePool};

use crate::{
NativeFn, OpCode, ReturnValue, Value,
ai::{self, PromptConfig},
ai::{self, AiConfig, PromptConfig},
ast::{ChunkId, Visibility},
builtins::BuiltinMethods,
module::{ModuleKind, ModuleManager, ModuleSource},
Expand Down Expand Up @@ -110,6 +110,7 @@ pub struct State<'gc> {
pub pg_connection: Option<PgPool>,
pub sqlite_connection: Option<SqlitePool>,
pub redis_connection: Option<redis::aio::MultiplexedConnection>,
pub ai_config: Option<AiConfig>,
}

unsafe impl Collect for State<'_> {
Expand Down Expand Up @@ -152,6 +153,7 @@ impl<'gc> State<'gc> {
pg_connection: None,
sqlite_connection: None,
redis_connection: None,
ai_config: None,
}
}

Expand Down
9 changes: 7 additions & 2 deletions aiscript/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ enum Commands {
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
Config::load("project.toml");
let config = Config::load();

let cli = AIScriptCli::parse();
match cli.command {
Expand All @@ -69,7 +69,12 @@ async fn main() {
let sqlite_connection = aiscript_runtime::get_sqlite_connection().await;
let redis_connection = aiscript_runtime::get_redis_connection().await;
task::spawn_blocking(move || {
let mut vm = Vm::new(pg_connection, sqlite_connection, redis_connection);
let mut vm = Vm::new(
pg_connection,
sqlite_connection,
redis_connection,
config.ai.clone(),
);
vm.run_file(path);
})
.await // must use await to wait for the thread to finish
Expand Down
Loading