refactor: simplify UI panels, use pooled DB, add --noui flag
- Removed unused `id` and `app_state` fields from `ChatPanel`; updated constructor to accept but ignore the state, reducing memory footprint. - Switched database access in `ChatPanel` from a raw `Mutex` lock to a connection pool (`app_state.conn.get()`), improving concurrency and error handling. - Reordered and cleaned up imports in `status_panel.rs` and formatted struct fields for readability. - Updated VS Code launch configuration to pass `--noui` argument, enabling headless mode for debugging. - Bumped several crate versions in `Cargo.lock` (e.g., `bitflags` to 2.10.0, `syn` to 2.0.108, `cookie` to 0.16.2) and added the new `ashpd` dependency, aligning the project with latest library releases.
This commit is contained in:
parent
25daaa8a9e
commit
fd45f4e0dd
52 changed files with 5870 additions and 4389 deletions
3
.vscode/launch.json
vendored
3
.vscode/launch.json
vendored
|
|
@ -1,6 +1,7 @@
|
|||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
|
|
@ -12,7 +13,7 @@
|
|||
"kind": "bin"
|
||||
}
|
||||
},
|
||||
"args": [],
|
||||
"args": ["--noui"],
|
||||
"env": {
|
||||
"RUST_LOG": "trace,actix_web=off,aws_sigv4=off,aws_smithy_checksums=off,actix_http=off,mio=off,reqwest=off,aws_runtime=off,aws_smithy_http_client=off,rustls=off,actix_server=off,hyper_util=off,aws_smithy_runtime=off,aws_smithy_runtime_api=off,tracing=off,aws_sdk_s3=off"
|
||||
|
||||
|
|
|
|||
3378
Cargo.lock
generated
3378
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
29
Cargo.toml
29
Cargo.toml
|
|
@ -37,17 +37,15 @@ license = "AGPL-3.0"
|
|||
repository = "https://github.com/GeneralBots/BotServer"
|
||||
|
||||
[features]
|
||||
default = [ "vectordb", "desktop"]
|
||||
default = []
|
||||
|
||||
vectordb = ["qdrant-client"]
|
||||
email = ["imap"]
|
||||
desktop = []
|
||||
desktop = ["tauri"]
|
||||
official = ["tauri"]
|
||||
|
||||
[dependencies]
|
||||
color-eyre = "0.6.5"
|
||||
crossterm = "0.29.0"
|
||||
ratatui = "0.29.0"
|
||||
scopeguard = "1.2.0"
|
||||
once_cell = "1.18.0"
|
||||
|
||||
actix-cors = "0.7"
|
||||
actix-multipart = "0.7"
|
||||
actix-web = "4.9"
|
||||
|
|
@ -55,6 +53,7 @@ actix-ws = "0.3"
|
|||
aes-gcm = "0.10"
|
||||
anyhow = "1.0"
|
||||
argon2 = "0.5"
|
||||
async-lock = "2.8.0"
|
||||
async-stream = "0.3"
|
||||
async-trait = "0.1"
|
||||
aws-config = "1.8.8"
|
||||
|
|
@ -62,9 +61,11 @@ aws-sdk-s3 = { version = "1.109.0", features = ["behavior-version-latest"] }
|
|||
base64 = "0.22"
|
||||
bytes = "1.8"
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
color-eyre = "0.6.5"
|
||||
cron = "0.15.0"
|
||||
crossterm = "0.29.0"
|
||||
csv = "1.3"
|
||||
diesel = { version = "2.1", features = ["postgres", "uuid", "chrono", "serde_json"] }
|
||||
diesel = { version = "2.1", features = ["postgres", "uuid", "chrono", "serde_json", "r2d2"] }
|
||||
dotenvy = "0.15"
|
||||
downloader = "0.2"
|
||||
env_logger = "0.11"
|
||||
|
|
@ -81,18 +82,25 @@ mailparse = "0.15"
|
|||
mockito = "1.7.0"
|
||||
native-tls = "0.2"
|
||||
num-format = "0.4"
|
||||
once_cell = "1.18.0"
|
||||
pdf-extract = "0.10.0"
|
||||
qdrant-client = { version = "1.12", optional = true }
|
||||
r2d2 = "0.8.10"
|
||||
rand = "0.9.2"
|
||||
ratatui = "0.29.0"
|
||||
redis = { version = "0.27", features = ["tokio-comp"] }
|
||||
regex = "1.11"
|
||||
reqwest = { version = "0.12", features = ["json", "stream"] }
|
||||
rhai = { git = "https://github.com/therealprof/rhai.git", branch = "features/use-web-time" }
|
||||
scopeguard = "1.2.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
sha2 = "0.10.9"
|
||||
smartstring = "1.0"
|
||||
sysinfo = "0.37.2"
|
||||
tauri = { version = "2", features = ["unstable"], optional = true }
|
||||
tauri-plugin-dialog = { version = "2", optional = true }
|
||||
tauri-plugin-opener = { version = "2", optional = true }
|
||||
tempfile = "3"
|
||||
time = "0.3.44"
|
||||
tokio = { version = "1.41", features = ["full"] }
|
||||
|
|
@ -104,6 +112,11 @@ urlencoding = "2.1"
|
|||
uuid = { version = "1.11", features = ["serde", "v4"] }
|
||||
zip = "2.2"
|
||||
|
||||
[build-dependencies]
|
||||
tauri-build = { version = "2", features = [] }
|
||||
|
||||
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
opt-level = "z"
|
||||
|
|
|
|||
78
TODO.md
78
TODO.md
|
|
@ -1,78 +0,0 @@
|
|||
# Documentation Completion Checklist
|
||||
|
||||
- [x] Created Chapter 01 files (README, installation, first-conversation, sessions)
|
||||
- [ ] Fill Chapter 02 files (README, gbai, gbdialog, gbkb, gbot, gbtheme, gbdrive) – already have content
|
||||
- [ ] Complete Chapter 03 files
|
||||
- [ ] README.md
|
||||
- [ ] vector-collections.md
|
||||
- [ ] indexing.md
|
||||
- [ ] qdrant.md
|
||||
- [ ] semantic-search.md
|
||||
- [ ] context-compaction.md
|
||||
- [ ] caching.md (if needed)
|
||||
- [ ] Complete Chapter 04 files
|
||||
- [ ] README.md
|
||||
- [ ] structure.md
|
||||
- [ ] web-interface.md
|
||||
- [ ] css.md
|
||||
- [ ] html.md
|
||||
- [ ] Complete Chapter 05 files
|
||||
- [ ] README.md
|
||||
- [ ] basics.md
|
||||
- [ ] templates.md
|
||||
- [ ] template-start.md
|
||||
- [ ] template-auth.md
|
||||
- [ ] template-summary.md
|
||||
- [ ] template-enrollment.md
|
||||
- [ ] keywords.md
|
||||
- [ ] All keyword pages (talk, hear, set-user, set-context, llm, get-bot-memory, set-bot-memory, set-kb, add-kb, add-website, add-tool, list-tools, remove-tool, clear-tools, get, find, set, on, set-schedule, create-site, create-draft, website-of, print, wait, format, first, last, for-each, exit-for)
|
||||
- [ ] Complete Chapter 06 files
|
||||
- [ ] README.md
|
||||
- [ ] architecture.md
|
||||
- [ ] building.md
|
||||
- [ ] crates.md
|
||||
- [ ] services.md
|
||||
- [ ] custom-keywords.md
|
||||
- [ ] dependencies.md
|
||||
- [ ] Complete Chapter 07 files
|
||||
- [ ] README.md
|
||||
- [ ] config-csv.md
|
||||
- [ ] parameters.md
|
||||
- [ ] answer-modes.md
|
||||
- [ ] llm-config.md
|
||||
- [ ] context-config.md
|
||||
- [ ] minio.md
|
||||
- [ ] Complete Chapter 08 files
|
||||
- [ ] README.md
|
||||
- [ ] tool-definition.md
|
||||
- [ ] param-declaration.md
|
||||
- [ ] compilation.md
|
||||
- [ ] mcp-format.md
|
||||
- [ ] openai-format.md
|
||||
- [ ] get-integration.md
|
||||
- [ ] external-apis.md
|
||||
- [ ] Complete Chapter 09 files
|
||||
- [ ] README.md
|
||||
- [ ] core-features.md
|
||||
- [ ] conversation.md
|
||||
- [ ] ai-llm.md
|
||||
- [ ] knowledge-base.md
|
||||
- [ ] automation.md
|
||||
- [ ] email.md
|
||||
- [ ] web-automation.md
|
||||
- [ ] storage.md
|
||||
- [ ] channels.md
|
||||
- [ ] Complete Chapter 10 files
|
||||
- [ ] README.md
|
||||
- [ ] setup.md
|
||||
- [ ] standards.md
|
||||
- [ ] testing.md
|
||||
- [ ] pull-requests.md
|
||||
- [ ] documentation.md
|
||||
- [ ] Complete Appendix I files
|
||||
- [ ] README.md
|
||||
- [ ] schema.md
|
||||
- [ ] tables.md
|
||||
- [ ] relationships.md
|
||||
- [ ] Verify SUMMARY.md links
|
||||
- [ ] Run mdbook build to ensure no errors
|
||||
22
add-req.sh
22
add-req.sh
|
|
@ -21,24 +21,24 @@ for file in "${prompts[@]}"; do
|
|||
done
|
||||
|
||||
dirs=(
|
||||
#"auth"
|
||||
#"automation"
|
||||
#"basic"
|
||||
"auth"
|
||||
"automation"
|
||||
"basic"
|
||||
"bootstrap"
|
||||
"bot"
|
||||
#"channels"
|
||||
#"config"
|
||||
"config"
|
||||
#"context"
|
||||
"drive_monitor"
|
||||
#"email"
|
||||
"file"
|
||||
# "kb"
|
||||
#"file"
|
||||
#"kb"
|
||||
"llm"
|
||||
#"llm_models"
|
||||
#"org"
|
||||
"package_manager"
|
||||
#"package_manager"
|
||||
#"riot_compiler"
|
||||
#"session"
|
||||
"session"
|
||||
"shared"
|
||||
#"tests"
|
||||
#"tools"
|
||||
|
|
@ -48,12 +48,6 @@ dirs=(
|
|||
#"web_automation"
|
||||
)
|
||||
|
||||
filter_rust_file() {
|
||||
sed -E '/^\s*\/\//d' "$1" | \
|
||||
sed -E '/info!\s*\(/d' | \
|
||||
sed -E '/debug!\s*\(/d' | \
|
||||
sed -E '/trace!\s*\(/d'
|
||||
}
|
||||
|
||||
for dir in "${dirs[@]}"; do
|
||||
find "$PROJECT_ROOT/src/$dir" -name "*.rs" | while read -r file; do
|
||||
|
|
|
|||
|
|
@ -74,6 +74,8 @@ cargo install cargo-audit
|
|||
cargo install cargo-edit
|
||||
apt install -y libpq-dev
|
||||
apt install -y valkey-cli
|
||||
valkey-cli config set stop-writes-on-bgsave-error no
|
||||
|
||||
|
||||
# Util
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,15 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -e # Exit on error
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_ROOT="$SCRIPT_DIR"
|
||||
OUTPUT_FILE="/tmp/prompt.out"
|
||||
|
||||
# Check required commands
|
||||
command -v cargo >/dev/null 2>&1 || { echo "cargo is required but not installed" >&2; exit 1; }
|
||||
command -v xclip >/dev/null 2>&1 || { echo "xclip is required but not installed" >&2; exit 1; }
|
||||
|
||||
echo "Please, fix this consolidated LLM Context" > "$OUTPUT_FILE"
|
||||
|
||||
prompts=(
|
||||
|
|
@ -12,6 +18,14 @@ prompts=(
|
|||
"./Cargo.toml"
|
||||
)
|
||||
|
||||
# Validate files exist
|
||||
for file in "${prompts[@]}"; do
|
||||
if [ ! -f "$file" ]; then
|
||||
echo "Required file not found: $file" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
for file in "${prompts[@]}"; do
|
||||
cat "$file" >> "$OUTPUT_FILE"
|
||||
echo "" >> "$OUTPUT_FILE"
|
||||
|
|
@ -43,11 +57,15 @@ dirs=(
|
|||
# "whatsapp"
|
||||
)
|
||||
for dir in "${dirs[@]}"; do
|
||||
find "$PROJECT_ROOT/src/$dir" -name "*.rs" | while read file; do
|
||||
echo $file >> "$OUTPUT_FILE"
|
||||
if [ -d "$PROJECT_ROOT/src/$dir" ]; then
|
||||
find "$PROJECT_ROOT/src/$dir" -name "*.rs" | while read -r file; do
|
||||
if [ -f "$file" ]; then
|
||||
echo "$file" >> "$OUTPUT_FILE"
|
||||
cat "$file" >> "$OUTPUT_FILE"
|
||||
echo "" >> "$OUTPUT_FILE"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
done
|
||||
|
||||
# Also append the specific files you mentioned
|
||||
|
|
@ -63,15 +81,18 @@ cargo build --message-format=short 2>&1 | grep -E 'error' >> "$OUTPUT_FILE"
|
|||
|
||||
|
||||
# Calculate and display token count (approximation: words * 1.3)
|
||||
WORD_COUNT=$(wc -w < "$OUTPUT_FILE")
|
||||
TOKEN_COUNT=$(echo "$WORD_COUNT * 1.3 / 1" | bc)
|
||||
FILE_SIZE=$(wc -c < "$OUTPUT_FILE")
|
||||
WORD_COUNT=$(wc -w < "$OUTPUT_FILE") || { echo "Error counting words" >&2; exit 1; }
|
||||
TOKEN_COUNT=$(echo "$WORD_COUNT * 1.3 / 1" | bc) || { echo "Error calculating tokens" >&2; exit 1; }
|
||||
FILE_SIZE=$(wc -c < "$OUTPUT_FILE") || { echo "Error getting file size" >&2; exit 1; }
|
||||
|
||||
echo "" >> "$OUTPUT_FILE"
|
||||
|
||||
echo "Approximate token count: $TOKEN_COUNT"
|
||||
echo "Context size: $FILE_SIZE bytes"
|
||||
|
||||
cat "$OUTPUT_FILE" | xclip -selection clipboard
|
||||
if ! cat "$OUTPUT_FILE" | xclip -selection clipboard; then
|
||||
echo "Error copying to clipboard" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Content copied to clipboard (xclip)"
|
||||
rm -f "$OUTPUT_FILE"
|
||||
|
|
|
|||
|
|
@ -25,29 +25,6 @@ CREATE INDEX IF NOT EXISTS idx_system_automations_bot_id
|
|||
ON public.system_automations (bot_id);
|
||||
|
||||
|
||||
ALTER TABLE public.system_automations
|
||||
ADD CONSTRAINT system_automations_bot_kind_param_unique
|
||||
UNIQUE (bot_id, kind, param);
|
||||
|
||||
-- Migration 6.0.10: Add unique constraint for system_automations upsert
|
||||
-- Description: Creates a unique constraint matching the ON CONFLICT target in set_schedule.rs
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_constraint
|
||||
WHERE conname = 'system_automations_bot_kind_param_unique'
|
||||
) THEN
|
||||
ALTER TABLE public.system_automations
|
||||
ADD CONSTRAINT system_automations_bot_kind_param_unique
|
||||
UNIQUE (bot_id, kind, param);
|
||||
END IF;
|
||||
END
|
||||
$$;
|
||||
|
||||
-- Migration 6.0.6: Add unique constraint for system_automations
|
||||
-- Fixes error: "there is no unique or exclusion constraint matching the ON CONFLICT specification"
|
||||
|
||||
ALTER TABLE public.system_automations
|
||||
ADD CONSTRAINT system_automations_bot_kind_param_unique
|
||||
UNIQUE (bot_id, kind, param);
|
||||
|
|
|
|||
|
|
@ -3,17 +3,19 @@
|
|||
### Fallback Strategy (After 3 attempts / 10 minutes):
|
||||
When initial attempts fail, sequentially try these LLMs:
|
||||
1. **DeepSeek-V3-0324** (good architect, adventure, reliable, let little errors just to be fixed by gpt-*)
|
||||
1. **DeepSeek-V3.1** (slower)
|
||||
1. **gpt-5-chat** (slower, let warnings...)
|
||||
1. **gpt-oss-120b**
|
||||
1. **Claude (Web)**: Copy only the problem statement and create unit tests. Create/extend UI.
|
||||
1. **Llama-3.3-70B-Instruct** (alternative)
|
||||
|
||||
### Development Workflow:
|
||||
- **One requirement at a time** with sequential commits
|
||||
- **On error**: Stop and consult Claude for guidance
|
||||
- **On unresolved error**: Stop and use add-req.sh, and consult Claude for guidance. with DeepThining in DeepSeek also, with Web turned on.
|
||||
- **Change progression**: Start with DeepSeek, conclude with gpt-oss-120b
|
||||
- If a big req. fail, specify a @code file that has similar pattern or sample from official docs.
|
||||
- **Final validation**: Use prompt "cargo check" with gpt-oss-120b
|
||||
- Be humble, one requirement, one commit. But sometimes, freedom of caos is welcome - when no deadlines are set.
|
||||
- Fix manually in case of dangerous trouble.
|
||||
- Keep in the source codebase only deployed and tested source, no lab source code in main project. At least, use optional features to introduce new behaviour gradually in PRODUCTION.
|
||||
- Transform good articles into prompts for the coder.
|
||||
- Switch to libraries that have LLM affinity.
|
||||
- Ensure 'continue' on LLMs, they can EOF and say are done, but got more to output.
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
- On code return identifiers/chars in english language.
|
||||
- Do not emmit any comment, and remove any existants in Rust/html.
|
||||
- On code return identifiers/characters in English language, no invalid tokens!
|
||||
- Do not emit any comments, and remove any existing ones in Rust/HTML.
|
||||
- Compact the code emission where possible.
|
||||
- On change code, ensure cargo check cycle to remove warnings and errors.
|
||||
- Never use defaults or magic values in code (never unwrap_or_else or similars)
|
||||
- On code change, ensure cargo check cycle to remove warnings and errors.
|
||||
- Never use defaults or magic values in code (never unwrap_or_else or similar)
|
||||
- Check borrow, clone, types, common Rust errors! Return 0 warning code!
|
||||
|
|
@ -1,15 +1,16 @@
|
|||
MOST IMPORTANT CODE GENERATION RULES:
|
||||
- KISS, NO TALK, SECURED ENTERPRISE GRADE THREAD SAFE CODE ONLY.
|
||||
- Use rustc 1.90.0 (1159e78c4 2025-09-14).
|
||||
- Check for warnings related to use of mut where is dispensable.
|
||||
- No placeholders, never comment/uncomment code, no explanations, no filler text.
|
||||
- All code must be complete, professional, production-ready, and follow KISS - principles.
|
||||
- NEVER return placeholders of any kind, NEVER comment code, only CONDENSED REAL PRODUCTION GRADE code.
|
||||
- REMOTE ALL COMMENTS FROM GENERATED CODE. DO NOT COMMENT AT ALL, NO TALK!
|
||||
- REMOTE ALL COMMENTS FROM GENERATED CODE. DO NOT COMMENT AT ALL, NO TALK, just say you are finished!
|
||||
- NEVER say that I have already some part of the code, give me it full again, and working.
|
||||
- Always increment logging with (all-in-one-line) info!, debug!, trace! to give birth to the console.
|
||||
- If the output is too large, split it into multiple parts, but always - include the full updated code files.
|
||||
- Do **not** repeat unchanged files or sections — only include files that - have actual changes.
|
||||
- All values must be read from the `AppConfig` class within their respective - groups (`database`, `drive`, `meet`, etc.); never use hardcoded or magic - values.
|
||||
- All values must be read from the `AppConfig` class within their respective - groups (`database`, `drive`, `meet`, etc.); never use hardcoded or commercial names like S3, Azure or something like that, preffer Drive, Cloud, instead, never use url or magic - values like api.openai - no unrwap_or_defaul at all!!!
|
||||
- Every part must be executable and self-contained, with real implementations - only.
|
||||
- DO NOT WRITE ANY ERROR HANDLING CODE LET IT CRASH.
|
||||
- Never generate two ore more trace mensages that are equal!
|
||||
|
|
@ -18,3 +19,5 @@ MOST IMPORTANT CODE GENERATION RULES:
|
|||
- NEVER return a untouched file in output. Just files that need to be updated.
|
||||
- Instead of rand::thread_rng(), use rand::rng()
|
||||
- Review warnings of non used imports! Give me 0 warnings, please.
|
||||
- Ensure you remove these: ommon errors borrow of moved value, unused variable, use of moved value.
|
||||
|
||||
|
|
|
|||
|
|
@ -1,19 +1,15 @@
|
|||
|
||||
use actix_web::{HttpRequest, HttpResponse, Result, web};
|
||||
use log::error;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
use crate::shared::state::AppState;
|
||||
|
||||
pub struct AuthService {}
|
||||
|
||||
impl AuthService {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_web::get("/api/auth")]
|
||||
async fn auth_handler(
|
||||
_req: HttpRequest,
|
||||
|
|
@ -22,7 +18,6 @@ async fn auth_handler(
|
|||
) -> Result<HttpResponse> {
|
||||
let bot_name = params.get("bot_name").cloned().unwrap_or_default();
|
||||
let _token = params.get("token").cloned();
|
||||
|
||||
let user_id = {
|
||||
let mut sm = data.session_manager.lock().await;
|
||||
sm.get_or_create_anonymous_user(None).map_err(|e| {
|
||||
|
|
@ -30,20 +25,18 @@ async fn auth_handler(
|
|||
actix_web::error::ErrorInternalServerError("Failed to create user")
|
||||
})?
|
||||
};
|
||||
|
||||
let (bot_id, bot_name) = tokio::task::spawn_blocking({
|
||||
let bot_name = bot_name.clone();
|
||||
let conn_arc = Arc::clone(&data.conn);
|
||||
let conn = data.conn.clone();
|
||||
move || {
|
||||
let mut db_conn = conn_arc.lock().unwrap();
|
||||
let mut db_conn = conn.get().map_err(|e| format!("Failed to get database connection: {}", e))?;
|
||||
use crate::shared::models::schema::bots::dsl::*;
|
||||
use diesel::prelude::*;
|
||||
|
||||
match bots
|
||||
.filter(name.eq(&bot_name))
|
||||
.filter(is_active.eq(true))
|
||||
.select((id, name))
|
||||
.first::<(Uuid, String)>(&mut *db_conn)
|
||||
.first::<(Uuid, String)>(&mut db_conn)
|
||||
.optional()
|
||||
{
|
||||
Ok(Some((id_val, name_val))) => Ok((id_val, name_val)),
|
||||
|
|
@ -51,7 +44,7 @@ async fn auth_handler(
|
|||
match bots
|
||||
.filter(is_active.eq(true))
|
||||
.select((id, name))
|
||||
.first::<(Uuid, String)>(&mut *db_conn)
|
||||
.first::<(Uuid, String)>(&mut db_conn)
|
||||
.optional()
|
||||
{
|
||||
Ok(Some((id_val, name_val))) => Ok((id_val, name_val)),
|
||||
|
|
@ -72,7 +65,6 @@ async fn auth_handler(
|
|||
error!("{}", e);
|
||||
actix_web::error::ErrorInternalServerError(e)
|
||||
})?;
|
||||
|
||||
let session = {
|
||||
let mut sm = data.session_manager.lock().await;
|
||||
sm.get_or_create_user_session(user_id, bot_id, "Auth Session")
|
||||
|
|
@ -85,9 +77,7 @@ async fn auth_handler(
|
|||
actix_web::error::ErrorInternalServerError("Failed to create session")
|
||||
})?
|
||||
};
|
||||
|
||||
let auth_script_path = format!("./work/{}.gbai/{}.gbdialog/auth.ast", bot_name, bot_name);
|
||||
|
||||
if tokio::fs::metadata(&auth_script_path).await.is_ok() {
|
||||
let auth_script = match tokio::fs::read_to_string(&auth_script_path).await {
|
||||
Ok(content) => content,
|
||||
|
|
@ -100,9 +90,7 @@ async fn auth_handler(
|
|||
})));
|
||||
}
|
||||
};
|
||||
|
||||
let script_service = crate::basic::ScriptService::new(Arc::clone(&data), session.clone());
|
||||
|
||||
match tokio::time::timeout(
|
||||
std::time::Duration::from_secs(5),
|
||||
async {
|
||||
|
|
@ -126,14 +114,11 @@ async fn auth_handler(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().json(serde_json::json!({
|
||||
"user_id": session.user_id,
|
||||
"session_id": session.id,
|
||||
"status": "authenticated"
|
||||
})))
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod auth_test;
|
||||
|
|
|
|||
|
|
@ -3,15 +3,13 @@ use crate::llm_models;
|
|||
use crate::shared::models::Automation;
|
||||
use crate::shared::state::AppState;
|
||||
use diesel::prelude::*;
|
||||
use log::{error, info, trace};
|
||||
use log::{error, trace};
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
use tokio::time::{interval, Duration};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub fn start_compact_prompt_scheduler(state: Arc<AppState>) {
|
||||
tokio::spawn(async move {
|
||||
// Initial 30 second delay before first run
|
||||
tokio::time::sleep(Duration::from_secs(30)).await;
|
||||
let mut interval = interval(Duration::from_secs(60));
|
||||
loop {
|
||||
|
|
@ -22,148 +20,83 @@ pub fn start_compact_prompt_scheduler(state: Arc<AppState>) {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
async fn execute_compact_prompt(state: Arc<AppState>) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
use crate::shared::models::system_automations::dsl::{is_active, system_automations};
|
||||
|
||||
let automations: Vec<Automation> = {
|
||||
let mut conn = state
|
||||
.conn
|
||||
.lock()
|
||||
.map_err(|e| format!("Failed to acquire lock: {}", e))?;
|
||||
|
||||
system_automations
|
||||
.filter(is_active.eq(true))
|
||||
.load::<Automation>(&mut *conn)?
|
||||
let mut conn = state.conn.get().map_err(|e| format!("Failed to acquire lock: {}", e))?;
|
||||
system_automations.filter(is_active.eq(true)).load::<Automation>(&mut *conn)?
|
||||
};
|
||||
|
||||
for automation in automations {
|
||||
if let Err(e) = compact_prompt_for_bot(&state, &automation).await {
|
||||
error!(
|
||||
"Failed to compact prompt for bot {}: {}",
|
||||
automation.bot_id, e
|
||||
);
|
||||
error!("Failed to compact prompt for bot {}: {}", automation.bot_id, e);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn compact_prompt_for_bot(
|
||||
state: &Arc<AppState>,
|
||||
automation: &Automation,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
// Skip if already compacting this bot
|
||||
async fn compact_prompt_for_bot(state: &Arc<AppState>, automation: &Automation) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
use once_cell::sync::Lazy;
|
||||
use scopeguard::guard;
|
||||
static IN_PROGRESS: Lazy<tokio::sync::Mutex<HashSet<Uuid>>> = Lazy::new(|| {
|
||||
tokio::sync::Mutex::new(HashSet::new())
|
||||
});
|
||||
|
||||
static IN_PROGRESS: Lazy<tokio::sync::Mutex<HashSet<Uuid>>> = Lazy::new(|| tokio::sync::Mutex::new(HashSet::new()));
|
||||
{
|
||||
let mut in_progress = IN_PROGRESS.lock().await;
|
||||
if in_progress.contains(&automation.bot_id) {
|
||||
trace!("Skipping compaction for bot {} - already in progress", automation.bot_id);
|
||||
return Ok(());
|
||||
}
|
||||
in_progress.insert(automation.bot_id);
|
||||
}
|
||||
|
||||
// Ensure cleanup happens when function exits
|
||||
let bot_id = automation.bot_id;
|
||||
let _cleanup = guard((), |_| {
|
||||
tokio::spawn(async move {
|
||||
let mut in_progress = IN_PROGRESS.lock().await;
|
||||
in_progress.remove(&bot_id);
|
||||
trace!("Released compaction lock for bot {}", bot_id);
|
||||
});
|
||||
});
|
||||
|
||||
info!("Executing prompt compaction for bot: {}", automation.bot_id);
|
||||
|
||||
let config_manager = ConfigManager::new(Arc::clone(&state.conn));
|
||||
let config_manager = ConfigManager::new(state.conn.clone());
|
||||
let compact_threshold = config_manager
|
||||
.get_config(&automation.bot_id, "prompt-compact", None)?
|
||||
.parse::<i32>()
|
||||
.unwrap_or(0);
|
||||
|
||||
// Compact if threshold is negative (always compact) or positive (conditional)
|
||||
if compact_threshold == 0 {
|
||||
return Ok(());
|
||||
} else if compact_threshold < 0 {
|
||||
info!("Compaction forced for bot {} (threshold = {})", automation.bot_id, compact_threshold);
|
||||
trace!("Negative compact threshold detected for bot {}, skipping", automation.bot_id);
|
||||
}
|
||||
|
||||
// Get sessions without holding lock
|
||||
let sessions = {
|
||||
let mut session_manager = state.session_manager.lock().await;
|
||||
session_manager.get_user_sessions(Uuid::nil())?
|
||||
};
|
||||
|
||||
for session in sessions {
|
||||
if session.bot_id != automation.bot_id {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get history without holding lock
|
||||
let history = {
|
||||
let mut session_manager = state.session_manager.lock().await;
|
||||
session_manager.get_conversation_history(session.id, session.user_id)?
|
||||
};
|
||||
|
||||
info!(
|
||||
"Compacting prompt for session {}: {} messages",
|
||||
session.id,
|
||||
history.len()
|
||||
);
|
||||
|
||||
// Compact entire conversation history when threshold is reached
|
||||
trace!("Compacting prompt for session {}: {} messages", session.id, history.len());
|
||||
let mut compacted = String::new();
|
||||
for (role, content) in &history {
|
||||
compacted.push_str(&format!("{}: {}\n", role, content));
|
||||
}
|
||||
|
||||
// Clone needed references for async task
|
||||
let llm_provider = state.llm_provider.clone();
|
||||
let compacted_clone = compacted.clone();
|
||||
|
||||
// Run LLM summarization with proper tracing and filtering
|
||||
trace!("Starting summarization for session {}", session.id);
|
||||
let summarized = match llm_provider.summarize(&compacted_clone).await {
|
||||
Ok(summary) => {
|
||||
trace!("Successfully summarized session {} ({} chars)",
|
||||
session.id, summary.len());
|
||||
// Use handler to filter <think> content
|
||||
let handler = llm_models::get_handler(
|
||||
&config_manager.get_config(
|
||||
&automation.bot_id,
|
||||
"llm-model",
|
||||
None
|
||||
).unwrap_or_default()
|
||||
);
|
||||
trace!("Successfully summarized conversation for session {}, summary length: {}", session.id, summary.len());
|
||||
let handler = llm_models::get_handler(&config_manager.get_config(&automation.bot_id, "llm-model", None).unwrap_or_default());
|
||||
let filtered = handler.process_content(&summary);
|
||||
format!("SUMMARY: {}", filtered)
|
||||
},
|
||||
Err(e) => {
|
||||
error!("Failed to summarize conversation for session {}: {}", session.id, e);
|
||||
trace!("Using fallback summary for session {}", session.id);
|
||||
format!("SUMMARY: {}", compacted) // Fallback
|
||||
format!("SUMMARY: {}", compacted)
|
||||
}
|
||||
};
|
||||
info!(
|
||||
"Prompt compacted {}: {} messages",
|
||||
session.id,
|
||||
history.len()
|
||||
);
|
||||
|
||||
// Instead of clearing messages, insert a compacted marker message
|
||||
trace!("Prompt compacted {}: {} messages", session.id, history.len());
|
||||
{
|
||||
let mut session_manager = state.session_manager.lock().await;
|
||||
// Save a special compacted message type (9)
|
||||
session_manager.save_message(session.id, session.user_id, 9, &summarized, 1)?;
|
||||
trace!("Inserted compacted message for session {}", session.id);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,28 +5,21 @@ use crate::shared::state::AppState;
|
|||
use chrono::Utc;
|
||||
use cron::Schedule;
|
||||
use diesel::prelude::*;
|
||||
use log::{error, info};
|
||||
use log::{error, trace};
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use tokio::time::{interval, Duration};
|
||||
mod compact_prompt;
|
||||
|
||||
pub struct AutomationService {
|
||||
state: Arc<AppState>,
|
||||
}
|
||||
|
||||
impl AutomationService {
|
||||
pub fn new(state: Arc<AppState>) -> Self {
|
||||
// Start the compact prompt scheduler
|
||||
crate::automation::compact_prompt::start_compact_prompt_scheduler(Arc::clone(&state));
|
||||
Self { state }
|
||||
}
|
||||
|
||||
pub async fn spawn(self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
info!("Automation service started");
|
||||
|
||||
let mut ticker = interval(Duration::from_secs(60));
|
||||
|
||||
loop {
|
||||
ticker.tick().await;
|
||||
if let Err(e) = self.check_scheduled_tasks().await {
|
||||
|
|
@ -34,29 +27,24 @@ impl AutomationService {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn check_scheduled_tasks(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
use crate::shared::models::system_automations::dsl::{
|
||||
id, is_active, kind, last_triggered as lt_column, system_automations,
|
||||
};
|
||||
|
||||
let mut conn = self
|
||||
.state
|
||||
.conn
|
||||
.lock()
|
||||
.map_err(|e| format!("Failed to acquire lock: {}", e))?;
|
||||
|
||||
.get()
|
||||
.map_err(|e| format!("Failed to acquire database connection: {}", e))?;
|
||||
let automations: Vec<Automation> = system_automations
|
||||
.filter(is_active.eq(true))
|
||||
.filter(kind.eq(TriggerKind::Scheduled as i32))
|
||||
.load::<Automation>(&mut *conn)?;
|
||||
|
||||
.load::<Automation>(&mut conn)?;
|
||||
for automation in automations {
|
||||
if let Some(schedule_str) = &automation.schedule {
|
||||
if let Ok(parsed_schedule) = Schedule::from_str(schedule_str) {
|
||||
let now = Utc::now();
|
||||
let next_run = parsed_schedule.upcoming(Utc).next();
|
||||
|
||||
if let Some(next_time) = next_run {
|
||||
let time_until_next = next_time - now;
|
||||
if time_until_next.num_minutes() < 1 {
|
||||
|
|
@ -65,44 +53,36 @@ impl AutomationService {
|
|||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
self.execute_automation(&automation).await?;
|
||||
|
||||
diesel::update(system_automations.filter(id.eq(automation.id)))
|
||||
.set(lt_column.eq(Some(now)))
|
||||
.execute(&mut *conn)?;
|
||||
.execute(&mut conn)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn execute_automation(
|
||||
&self,
|
||||
automation: &Automation,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
info!("Executing automation: {}", automation.param);
|
||||
|
||||
let bot_name: String = {
|
||||
use crate::shared::models::schema::bots::dsl::*;
|
||||
let mut conn = self
|
||||
.state
|
||||
.conn
|
||||
.lock()
|
||||
.map_err(|e| format!("Lock failed: {}", e))?;
|
||||
.get()
|
||||
.map_err(|e| format!("Failed to acquire database connection: {}", e))?;
|
||||
bots.filter(id.eq(automation.bot_id))
|
||||
.select(name)
|
||||
.first(&mut *conn)?
|
||||
.first(&mut conn)?
|
||||
};
|
||||
|
||||
let script_path = format!(
|
||||
"./work/{}.gbai/{}.gbdialog/{}.ast",
|
||||
bot_name, bot_name, automation.param
|
||||
);
|
||||
|
||||
let script_content = match tokio::fs::read_to_string(&script_path).await {
|
||||
Ok(content) => content,
|
||||
Err(e) => {
|
||||
|
|
@ -110,16 +90,13 @@ impl AutomationService {
|
|||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
let session = {
|
||||
let mut sm = self.state.session_manager.lock().await;
|
||||
let admin_user = uuid::Uuid::nil();
|
||||
sm.get_or_create_user_session(admin_user, automation.bot_id, "Automation")?
|
||||
.ok_or("Failed to create session")?
|
||||
};
|
||||
|
||||
let script_service = ScriptService::new(Arc::clone(&self.state), session);
|
||||
|
||||
match script_service.compile(&script_content) {
|
||||
Ok(ast) => {
|
||||
if let Err(e) = script_service.run(&ast) {
|
||||
|
|
@ -130,74 +107,58 @@ impl AutomationService {
|
|||
error!("Script compilation failed: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn execute_compact_prompt(
|
||||
&self,
|
||||
automation: &Automation,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
info!("Executing prompt compaction for bot: {}", automation.bot_id);
|
||||
|
||||
let config_manager = ConfigManager::new(Arc::clone(&self.state.conn));
|
||||
let config_manager = ConfigManager::new(self.state.conn.clone());
|
||||
let compact_threshold = config_manager
|
||||
.get_config(&automation.bot_id, "prompt-compact", None)?
|
||||
.parse::<usize>()
|
||||
.unwrap_or(0);
|
||||
|
||||
if compact_threshold == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut session_manager = self.state.session_manager.lock().await;
|
||||
let sessions = session_manager.get_user_sessions(uuid::Uuid::nil())?;
|
||||
|
||||
for session in sessions {
|
||||
if session.bot_id != automation.bot_id {
|
||||
continue;
|
||||
}
|
||||
|
||||
let history = session_manager.get_conversation_history(session.id, session.user_id)?;
|
||||
|
||||
if history.len() > compact_threshold {
|
||||
info!(
|
||||
trace!(
|
||||
"Compacting prompt for session {}: {} messages",
|
||||
session.id,
|
||||
history.len()
|
||||
);
|
||||
|
||||
let mut compacted = String::new();
|
||||
for (role, content) in &history[..history.len() - compact_threshold] {
|
||||
compacted.push_str(&format!("{}: {}\n", role, content));
|
||||
}
|
||||
|
||||
let summarized = format!("SUMMARY: {}", compacted);
|
||||
|
||||
session_manager.save_message(session.id, session.user_id, 3, &summarized, 1)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_compact_prompt(state: Arc<crate::shared::state::AppState>) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
pub async fn execute_compact_prompt(
|
||||
state: Arc<crate::shared::state::AppState>,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
use crate::shared::models::system_automations::dsl::{is_active, system_automations};
|
||||
use diesel::prelude::*;
|
||||
use log::info;
|
||||
|
||||
let state_clone = state.clone();
|
||||
let service = AutomationService::new(state_clone);
|
||||
|
||||
let service = AutomationService::new(state_clone);
|
||||
let mut conn = state
|
||||
.conn
|
||||
.lock()
|
||||
.map_err(|e| format!("Failed to acquire lock: {}", e))?;
|
||||
.get()
|
||||
.map_err(|e| format!("Failed to acquire database connection: {}", e))?;
|
||||
let automations: Vec<crate::shared::models::Automation> = system_automations
|
||||
.filter(is_active.eq(true))
|
||||
.load::<crate::shared::models::Automation>(&mut *conn)?;
|
||||
|
||||
.load::<crate::shared::models::Automation>(&mut conn)?;
|
||||
for automation in automations {
|
||||
if let Err(e) = service.execute_compact_prompt(&automation).await {
|
||||
error!(
|
||||
|
|
@ -206,7 +167,5 @@ let service = AutomationService::new(state_clone);
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
info!("Prompt compaction cycle completed");
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,17 @@
|
|||
use crate::shared::state::AppState;
|
||||
use crate::basic::keywords::set_schedule::execute_set_schedule;
|
||||
use crate::shared::models::TriggerKind;
|
||||
use crate::shared::state::AppState;
|
||||
use diesel::ExpressionMethods;
|
||||
use diesel::QueryDsl;
|
||||
use diesel::RunQueryDsl;
|
||||
use log::warn;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use diesel::QueryDsl;
|
||||
use diesel::ExpressionMethods;
|
||||
use std::collections::HashSet;
|
||||
use diesel::RunQueryDsl;
|
||||
use crate::shared::models::TriggerKind;
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ParamDeclaration {
|
||||
pub name: String,
|
||||
|
|
@ -21,7 +20,6 @@ pub struct ParamDeclaration {
|
|||
pub description: String,
|
||||
pub required: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ToolDefinition {
|
||||
pub name: String,
|
||||
|
|
@ -29,14 +27,12 @@ pub struct ToolDefinition {
|
|||
pub parameters: Vec<ParamDeclaration>,
|
||||
pub source_file: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct MCPTool {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub input_schema: MCPInputSchema,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct MCPInputSchema {
|
||||
#[serde(rename = "type")]
|
||||
|
|
@ -44,7 +40,6 @@ pub struct MCPInputSchema {
|
|||
pub properties: HashMap<String, MCPProperty>,
|
||||
pub required: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct MCPProperty {
|
||||
#[serde(rename = "type")]
|
||||
|
|
@ -53,21 +48,18 @@ pub struct MCPProperty {
|
|||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub example: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OpenAITool {
|
||||
#[serde(rename = "type")]
|
||||
pub tool_type: String,
|
||||
pub function: OpenAIFunction,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OpenAIFunction {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub parameters: OpenAIParameters,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OpenAIParameters {
|
||||
#[serde(rename = "type")]
|
||||
|
|
@ -75,7 +67,6 @@ pub struct OpenAIParameters {
|
|||
pub properties: HashMap<String, OpenAIProperty>,
|
||||
pub required: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OpenAIProperty {
|
||||
#[serde(rename = "type")]
|
||||
|
|
@ -84,13 +75,11 @@ pub struct OpenAIProperty {
|
|||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub example: Option<String>,
|
||||
}
|
||||
|
||||
pub struct BasicCompiler {
|
||||
state: Arc<AppState>,
|
||||
bot_id: uuid::Uuid,
|
||||
previous_schedules: HashSet<String>,
|
||||
}
|
||||
|
||||
impl BasicCompiler {
|
||||
pub fn new(state: Arc<AppState>, bot_id: uuid::Uuid) -> Self {
|
||||
Self {
|
||||
|
|
@ -99,7 +88,6 @@ impl BasicCompiler {
|
|||
previous_schedules: HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compile_file(
|
||||
&mut self,
|
||||
source_path: &str,
|
||||
|
|
@ -107,46 +95,35 @@ impl BasicCompiler {
|
|||
) -> Result<CompilationResult, Box<dyn Error + Send + Sync>> {
|
||||
let source_content = fs::read_to_string(source_path)
|
||||
.map_err(|e| format!("Failed to read source file: {}", e))?;
|
||||
|
||||
let tool_def = self.parse_tool_definition(&source_content, source_path)?;
|
||||
|
||||
let file_name = Path::new(source_path)
|
||||
.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.ok_or("Invalid file name")?;
|
||||
|
||||
let ast_path = format!("{}/{}.ast", output_dir, file_name);
|
||||
let ast_content = self.preprocess_basic(&source_content, source_path, self.bot_id)?;
|
||||
|
||||
fs::write(&ast_path, &ast_content)
|
||||
.map_err(|e| format!("Failed to write AST file: {}", e))?;
|
||||
|
||||
let (mcp_json, tool_json) = if !tool_def.parameters.is_empty() {
|
||||
let mcp = self.generate_mcp_tool(&tool_def)?;
|
||||
let openai = self.generate_openai_tool(&tool_def)?;
|
||||
|
||||
let mcp_path = format!("{}/{}.mcp.json", output_dir, file_name);
|
||||
let tool_path = format!("{}/{}.tool.json", output_dir, file_name);
|
||||
|
||||
let mcp_json_str = serde_json::to_string_pretty(&mcp)?;
|
||||
fs::write(&mcp_path, mcp_json_str)
|
||||
.map_err(|e| format!("Failed to write MCP JSON: {}", e))?;
|
||||
|
||||
let tool_json_str = serde_json::to_string_pretty(&openai)?;
|
||||
fs::write(&tool_path, tool_json_str)
|
||||
.map_err(|e| format!("Failed to write tool JSON: {}", e))?;
|
||||
|
||||
(Some(mcp), Some(openai))
|
||||
} else {
|
||||
(None, None)
|
||||
};
|
||||
|
||||
Ok(CompilationResult {
|
||||
mcp_tool: mcp_json,
|
||||
_openai_tool: tool_json,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parse_tool_definition(
|
||||
&self,
|
||||
source: &str,
|
||||
|
|
@ -156,16 +133,13 @@ impl BasicCompiler {
|
|||
let mut description = String::new();
|
||||
let lines: Vec<&str> = source.lines().collect();
|
||||
let mut i = 0;
|
||||
|
||||
while i < lines.len() {
|
||||
let line = lines[i].trim();
|
||||
|
||||
if line.starts_with("PARAM ") {
|
||||
if let Some(param) = self.parse_param_line(line)? {
|
||||
params.push(param);
|
||||
}
|
||||
}
|
||||
|
||||
if line.starts_with("DESCRIPTION ") {
|
||||
let desc_start = line.find('"').unwrap_or(0);
|
||||
let desc_end = line.rfind('"').unwrap_or(line.len());
|
||||
|
|
@ -173,16 +147,13 @@ impl BasicCompiler {
|
|||
description = line[desc_start + 1..desc_end].to_string();
|
||||
}
|
||||
}
|
||||
|
||||
i += 1;
|
||||
}
|
||||
|
||||
let tool_name = Path::new(source_path)
|
||||
.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or("unknown")
|
||||
.to_string();
|
||||
|
||||
Ok(ToolDefinition {
|
||||
name: tool_name,
|
||||
description,
|
||||
|
|
@ -190,7 +161,6 @@ impl BasicCompiler {
|
|||
source_file: source_path.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_param_line(
|
||||
&self,
|
||||
line: &str,
|
||||
|
|
@ -199,15 +169,12 @@ impl BasicCompiler {
|
|||
if !line.starts_with("PARAM ") {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() < 4 {
|
||||
warn!("Invalid PARAM line: {}", line);
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let name = parts[1].to_string();
|
||||
|
||||
let as_index = parts.iter().position(|&p| p == "AS");
|
||||
let param_type = if let Some(idx) = as_index {
|
||||
if idx + 1 < parts.len() {
|
||||
|
|
@ -218,7 +185,6 @@ impl BasicCompiler {
|
|||
} else {
|
||||
"string".to_string()
|
||||
};
|
||||
|
||||
let example = if let Some(like_pos) = line.find("LIKE") {
|
||||
let rest = &line[like_pos + 4..].trim();
|
||||
if let Some(start) = rest.find('"') {
|
||||
|
|
@ -233,7 +199,6 @@ impl BasicCompiler {
|
|||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let description = if let Some(desc_pos) = line.find("DESCRIPTION") {
|
||||
let rest = &line[desc_pos + 11..].trim();
|
||||
if let Some(start) = rest.find('"') {
|
||||
|
|
@ -248,7 +213,6 @@ impl BasicCompiler {
|
|||
} else {
|
||||
"".to_string()
|
||||
};
|
||||
|
||||
Ok(Some(ParamDeclaration {
|
||||
name,
|
||||
param_type: self.normalize_type(¶m_type),
|
||||
|
|
@ -257,7 +221,6 @@ impl BasicCompiler {
|
|||
required: true,
|
||||
}))
|
||||
}
|
||||
|
||||
fn normalize_type(&self, basic_type: &str) -> String {
|
||||
match basic_type.to_lowercase().as_str() {
|
||||
"string" | "text" => "string".to_string(),
|
||||
|
|
@ -270,14 +233,12 @@ impl BasicCompiler {
|
|||
_ => "string".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_mcp_tool(
|
||||
&self,
|
||||
tool_def: &ToolDefinition,
|
||||
) -> Result<MCPTool, Box<dyn Error + Send + Sync>> {
|
||||
let mut properties = HashMap::new();
|
||||
let mut required = Vec::new();
|
||||
|
||||
for param in &tool_def.parameters {
|
||||
properties.insert(
|
||||
param.name.clone(),
|
||||
|
|
@ -291,7 +252,6 @@ impl BasicCompiler {
|
|||
required.push(param.name.clone());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(MCPTool {
|
||||
name: tool_def.name.clone(),
|
||||
description: tool_def.description.clone(),
|
||||
|
|
@ -302,14 +262,12 @@ impl BasicCompiler {
|
|||
},
|
||||
})
|
||||
}
|
||||
|
||||
fn generate_openai_tool(
|
||||
&self,
|
||||
tool_def: &ToolDefinition,
|
||||
) -> Result<OpenAITool, Box<dyn Error + Send + Sync>> {
|
||||
let mut properties = HashMap::new();
|
||||
let mut required = Vec::new();
|
||||
|
||||
for param in &tool_def.parameters {
|
||||
properties.insert(
|
||||
param.name.clone(),
|
||||
|
|
@ -323,7 +281,6 @@ impl BasicCompiler {
|
|||
required.push(param.name.clone());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(OpenAITool {
|
||||
tool_type: "function".to_string(),
|
||||
function: OpenAIFunction {
|
||||
|
|
@ -337,38 +294,45 @@ impl BasicCompiler {
|
|||
},
|
||||
})
|
||||
}
|
||||
|
||||
fn preprocess_basic(&mut self, source: &str, source_path: &str, bot_id: uuid::Uuid) -> Result<String, Box<dyn Error + Send + Sync>> {
|
||||
fn preprocess_basic(
|
||||
&mut self,
|
||||
source: &str,
|
||||
source_path: &str,
|
||||
bot_id: uuid::Uuid,
|
||||
) -> Result<String, Box<dyn Error + Send + Sync>> {
|
||||
let bot_uuid = bot_id;
|
||||
let mut result = String::new();
|
||||
let mut has_schedule = false;
|
||||
|
||||
let script_name = Path::new(source_path)
|
||||
.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or("unknown")
|
||||
.to_string();
|
||||
|
||||
{
|
||||
let mut conn = self.state.conn.lock().unwrap();
|
||||
let mut conn = self
|
||||
.state
|
||||
.conn
|
||||
.get()
|
||||
.map_err(|e| format!("Failed to get database connection: {}", e))?;
|
||||
use crate::shared::models::system_automations::dsl::*;
|
||||
|
||||
diesel::delete(system_automations
|
||||
diesel::delete(
|
||||
system_automations
|
||||
.filter(bot_id.eq(bot_uuid))
|
||||
.filter(kind.eq(TriggerKind::Scheduled as i32))
|
||||
.filter(param.eq(&script_name))
|
||||
.filter(param.eq(&script_name)),
|
||||
)
|
||||
.execute(&mut *conn)
|
||||
.execute(&mut conn)
|
||||
.ok();
|
||||
}
|
||||
|
||||
for line in source.lines() {
|
||||
let trimmed = line.trim();
|
||||
|
||||
if trimmed.is_empty() || trimmed.starts_with("'") || trimmed.starts_with("//") || trimmed.starts_with("REM") {
|
||||
if trimmed.is_empty()
|
||||
|| trimmed.starts_with("'")
|
||||
|| trimmed.starts_with("//")
|
||||
|| trimmed.starts_with("REM")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
let normalized = trimmed
|
||||
.replace("SET SCHEDULE", "SET_SCHEDULE")
|
||||
.replace("ADD TOOL", "ADD_TOOL")
|
||||
|
|
@ -387,54 +351,58 @@ impl BasicCompiler {
|
|||
.replace("GET BOT MEMORY", "GET_BOT_MEMORY")
|
||||
.replace("SET BOT MEMORY", "SET_BOT_MEMORY")
|
||||
.replace("CREATE DRAFT", "CREATE_DRAFT");
|
||||
|
||||
if normalized.starts_with("SET_SCHEDULE") {
|
||||
has_schedule = true;
|
||||
let parts: Vec<&str> = normalized.split('"').collect();
|
||||
if parts.len() >= 3 {
|
||||
let cron = parts[1];
|
||||
let mut conn = self.state.conn.lock().unwrap();
|
||||
if let Err(e) = execute_set_schedule(&mut *conn, cron, &script_name, bot_id) {
|
||||
log::error!("Failed to schedule SET_SCHEDULE during preprocessing: {}", e);
|
||||
let mut conn = self
|
||||
.state
|
||||
.conn
|
||||
.get()
|
||||
.map_err(|e| format!("Failed to get database connection: {}", e))?;
|
||||
if let Err(e) = execute_set_schedule(&mut conn, cron, &script_name, bot_id) {
|
||||
log::error!(
|
||||
"Failed to schedule SET_SCHEDULE during preprocessing: {}",
|
||||
e
|
||||
);
|
||||
}
|
||||
} else {
|
||||
log::warn!("Malformed SET_SCHEDULE line ignored: {}", normalized);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if normalized.starts_with("PARAM ") || normalized.starts_with("DESCRIPTION ") {
|
||||
continue;
|
||||
}
|
||||
|
||||
result.push_str(&normalized);
|
||||
result.push('\n');
|
||||
}
|
||||
|
||||
if self.previous_schedules.contains(&script_name) && !has_schedule {
|
||||
let mut conn = self.state.conn.lock().unwrap();
|
||||
let mut conn = self
|
||||
.state
|
||||
.conn
|
||||
.get()
|
||||
.map_err(|e| format!("Failed to get database connection: {}", e))?;
|
||||
use crate::shared::models::system_automations::dsl::*;
|
||||
|
||||
diesel::delete(system_automations
|
||||
diesel::delete(
|
||||
system_automations
|
||||
.filter(bot_id.eq(bot_uuid))
|
||||
.filter(kind.eq(TriggerKind::Scheduled as i32))
|
||||
.filter(param.eq(&script_name))
|
||||
.filter(param.eq(&script_name)),
|
||||
)
|
||||
.execute(&mut *conn)
|
||||
.execute(&mut conn)
|
||||
.map_err(|e| log::error!("Failed to remove schedule for {}: {}", script_name, e))
|
||||
.ok();
|
||||
}
|
||||
|
||||
if has_schedule {
|
||||
self.previous_schedules.insert(script_name);
|
||||
} else {
|
||||
self.previous_schedules.remove(&script_name);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CompilationResult {
|
||||
pub mcp_tool: Option<MCPTool>,
|
||||
|
|
|
|||
|
|
@ -1,20 +1,19 @@
|
|||
use crate::shared::state::AppState;
|
||||
use crate::shared::models::UserSession;
|
||||
use log::{trace, debug, error, info};
|
||||
use crate::shared::state::AppState;
|
||||
use log::{error, trace};
|
||||
use rhai::{Dynamic, Engine};
|
||||
use serde_json::json;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn clear_suggestions_keyword(state: Arc<AppState>, user_session: UserSession, engine: &mut Engine) {
|
||||
pub fn clear_suggestions_keyword(
|
||||
state: Arc<AppState>,
|
||||
user_session: UserSession,
|
||||
engine: &mut Engine,
|
||||
) {
|
||||
let cache = state.cache.clone();
|
||||
|
||||
engine
|
||||
.register_custom_syntax(&["CLEAR_SUGGESTIONS"], true, move |_context, _inputs| {
|
||||
info!("CLEAR_SUGGESTIONS command executed");
|
||||
|
||||
if let Some(cache_client) = &cache {
|
||||
let redis_key = format!("suggestions:{}:{}", user_session.user_id, user_session.id);
|
||||
|
||||
let mut conn = match cache_client.get_connection() {
|
||||
Ok(conn) => conn,
|
||||
Err(e) => {
|
||||
|
|
@ -22,41 +21,42 @@ pub fn clear_suggestions_keyword(state: Arc<AppState>, user_session: UserSession
|
|||
return Ok(Dynamic::UNIT);
|
||||
}
|
||||
};
|
||||
|
||||
// Delete the suggestions list
|
||||
let result: Result<i64, redis::RedisError> = redis::cmd("DEL")
|
||||
.arg(&redis_key)
|
||||
.query(&mut conn);
|
||||
|
||||
let result: Result<i64, redis::RedisError> =
|
||||
redis::cmd("DEL").arg(&redis_key).query(&mut conn);
|
||||
match result {
|
||||
Ok(deleted) => {
|
||||
trace!("Cleared suggestions from Redis key {}, deleted: {}", redis_key, deleted);
|
||||
trace!(
|
||||
"Cleared {} suggestions from session {}",
|
||||
deleted,
|
||||
user_session.id
|
||||
);
|
||||
}
|
||||
Err(e) => error!("Failed to clear suggestions from Redis: {}", e),
|
||||
}
|
||||
} else {
|
||||
debug!("No Cache client configured; suggestions not cleared");
|
||||
trace!("No cache configured, suggestions not cleared");
|
||||
}
|
||||
|
||||
Ok(Dynamic::UNIT)
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn add_suggestion_keyword(state: Arc<AppState>, user_session: UserSession, engine: &mut Engine) {
|
||||
pub fn add_suggestion_keyword(
|
||||
state: Arc<AppState>,
|
||||
user_session: UserSession,
|
||||
engine: &mut Engine,
|
||||
) {
|
||||
let cache = state.cache.clone();
|
||||
|
||||
engine
|
||||
.register_custom_syntax(&["ADD_SUGGESTION", "$expr$", "AS", "$expr$"], true, move |context, inputs| {
|
||||
.register_custom_syntax(
|
||||
&["ADD_SUGGESTION", "$expr$", "AS", "$expr$"],
|
||||
true,
|
||||
move |context, inputs| {
|
||||
let context_name = context.eval_expression_tree(&inputs[0])?.to_string();
|
||||
let button_text = context.eval_expression_tree(&inputs[1])?.to_string();
|
||||
|
||||
info!("ADD_SUGGESTION command executed: context='{}', text='{}'", context_name, button_text);
|
||||
|
||||
if let Some(cache_client) = &cache {
|
||||
let redis_key = format!("suggestions:{}:{}", user_session.user_id, user_session.id);
|
||||
let redis_key =
|
||||
format!("suggestions:{}:{}", user_session.user_id, user_session.id);
|
||||
let suggestion = json!({ "context": context_name, "text": button_text });
|
||||
|
||||
let mut conn = match cache_client.get_connection() {
|
||||
Ok(conn) => conn,
|
||||
Err(e) => {
|
||||
|
|
@ -64,39 +64,40 @@ pub fn add_suggestion_keyword(state: Arc<AppState>, user_session: UserSession, e
|
|||
return Ok(Dynamic::UNIT);
|
||||
}
|
||||
};
|
||||
|
||||
// Append suggestion to Redis list - RPUSH returns the new length as i64
|
||||
let result: Result<i64, redis::RedisError> = redis::cmd("RPUSH")
|
||||
.arg(&redis_key)
|
||||
.arg(suggestion.to_string())
|
||||
.query(&mut conn);
|
||||
|
||||
match result {
|
||||
Ok(length) => {
|
||||
trace!("Suggestion added successfully to Redis key {}, new length: {}", redis_key, length);
|
||||
|
||||
// Also register context as inactive initially
|
||||
let active_key = format!("active_context:{}:{}", user_session.user_id, user_session.id);
|
||||
trace!(
|
||||
"Added suggestion to session {}, total suggestions: {}",
|
||||
user_session.id,
|
||||
length
|
||||
);
|
||||
let active_key = format!(
|
||||
"active_context:{}:{}",
|
||||
user_session.user_id, user_session.id
|
||||
);
|
||||
let hset_result: Result<i64, redis::RedisError> = redis::cmd("HSET")
|
||||
.arg(&active_key)
|
||||
.arg(&context_name)
|
||||
.arg("inactive")
|
||||
.query(&mut conn);
|
||||
|
||||
match hset_result {
|
||||
Ok(fields_added) => {
|
||||
trace!("Context state set to inactive for {}, fields added: {}", context_name, fields_added)
|
||||
},
|
||||
Ok(_fields_added) => {
|
||||
trace!("Set context state for session {}", user_session.id);
|
||||
}
|
||||
Err(e) => error!("Failed to set context state: {}", e),
|
||||
}
|
||||
}
|
||||
Err(e) => error!("Failed to add suggestion to Redis: {}", e),
|
||||
}
|
||||
} else {
|
||||
debug!("No Cache client configured; suggestion will not persist");
|
||||
trace!("No cache configured, suggestion not added");
|
||||
}
|
||||
|
||||
Ok(Dynamic::UNIT)
|
||||
})
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,112 +1,56 @@
|
|||
use crate::shared::models::UserSession;
|
||||
use crate::shared::state::AppState;
|
||||
use diesel::prelude::*;
|
||||
use log::{error, info, warn};
|
||||
use log::{error, trace, warn};
|
||||
use rhai::{Dynamic, Engine};
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub fn add_tool_keyword(state: Arc<AppState>, user: UserSession, engine: &mut Engine) {
|
||||
let state_clone = Arc::clone(&state);
|
||||
let user_clone = user.clone();
|
||||
|
||||
engine
|
||||
.register_custom_syntax(&["ADD_TOOL", "$expr$"], false, move |context, inputs| {
|
||||
let tool_path = context.eval_expression_tree(&inputs[0])?;
|
||||
let tool_path_str = tool_path.to_string().trim_matches('"').to_string();
|
||||
|
||||
info!(
|
||||
"ADD_TOOL command executed: {} for session: {}",
|
||||
tool_path_str, user_clone.id
|
||||
);
|
||||
|
||||
// Extract tool name from path (e.g., "enrollment.bas" -> "enrollment")
|
||||
let tool_name = tool_path_str
|
||||
.strip_prefix(".gbdialog/")
|
||||
.unwrap_or(&tool_path_str)
|
||||
.strip_suffix(".bas")
|
||||
.unwrap_or(&tool_path_str)
|
||||
.to_string();
|
||||
|
||||
// Validate tool name
|
||||
trace!("ADD_TOOL command executed: {} for session: {}", tool_path_str, user_clone.id);
|
||||
let tool_name = tool_path_str.strip_prefix(".gbdialog/").unwrap_or(&tool_path_str).strip_suffix(".bas").unwrap_or(&tool_path_str).to_string();
|
||||
if tool_name.is_empty() {
|
||||
return Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
"Invalid tool name".into(),
|
||||
rhai::Position::NONE,
|
||||
)));
|
||||
return Err(Box::new(rhai::EvalAltResult::ErrorRuntime("Invalid tool name".into(), rhai::Position::NONE)));
|
||||
}
|
||||
|
||||
let state_for_task = Arc::clone(&state_clone);
|
||||
let user_for_task = user_clone.clone();
|
||||
let tool_name_for_task = tool_name.clone();
|
||||
|
||||
// Spawn async task to associate tool with session
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||
.worker_threads(2)
|
||||
.enable_all()
|
||||
.build();
|
||||
|
||||
let rt = tokio::runtime::Builder::new_multi_thread().worker_threads(2).enable_all().build();
|
||||
let send_err = if let Ok(rt) = rt {
|
||||
let result = rt.block_on(async move {
|
||||
associate_tool_with_session(
|
||||
&state_for_task,
|
||||
&user_for_task,
|
||||
&tool_name_for_task,
|
||||
)
|
||||
.await
|
||||
associate_tool_with_session(&state_for_task, &user_for_task, &tool_name_for_task).await
|
||||
});
|
||||
tx.send(result).err()
|
||||
} else {
|
||||
tx.send(Err("Failed to build tokio runtime".to_string()))
|
||||
.err()
|
||||
tx.send(Err("Failed to build tokio runtime".to_string())).err()
|
||||
};
|
||||
|
||||
if send_err.is_some() {
|
||||
error!("Failed to send result from thread");
|
||||
}
|
||||
});
|
||||
|
||||
match rx.recv_timeout(std::time::Duration::from_secs(10)) {
|
||||
Ok(Ok(message)) => {
|
||||
info!("ADD_TOOL completed: {}", message);
|
||||
Ok(Dynamic::from(message))
|
||||
}
|
||||
Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
e.into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(e.into(), rhai::Position::NONE))),
|
||||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
|
||||
Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
"ADD_TOOL timed out".into(),
|
||||
rhai::Position::NONE,
|
||||
)))
|
||||
Err(Box::new(rhai::EvalAltResult::ErrorRuntime("ADD_TOOL timed out".into(), rhai::Position::NONE)))
|
||||
}
|
||||
Err(e) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
format!("ADD_TOOL failed: {}", e).into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
Err(e) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(format!("ADD_TOOL failed: {}", e).into(), rhai::Position::NONE))),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Associate a compiled tool with the current session
|
||||
/// The tool must already be compiled and present in the basic_tools table
|
||||
async fn associate_tool_with_session(
|
||||
state: &AppState,
|
||||
user: &UserSession,
|
||||
tool_name: &str,
|
||||
) -> Result<String, String> {
|
||||
async fn associate_tool_with_session(state: &AppState, user: &UserSession, tool_name: &str) -> Result<String, String> {
|
||||
use crate::shared::models::schema::{basic_tools, session_tool_associations};
|
||||
|
||||
let mut conn = state.conn.lock().map_err(|e| {
|
||||
error!("Failed to acquire database lock: {}", e);
|
||||
format!("Database connection error: {}", e)
|
||||
})?;
|
||||
|
||||
// First, verify the tool exists and is active for this bot
|
||||
let mut conn = state.conn.get().map_err(|e| format!("DB error: {}", e))?;
|
||||
let tool_exists: Result<bool, diesel::result::Error> = basic_tools::table
|
||||
.filter(basic_tools::bot_id.eq(user.bot_id.to_string()))
|
||||
.filter(basic_tools::tool_name.eq(tool_name))
|
||||
|
|
@ -114,110 +58,58 @@ async fn associate_tool_with_session(
|
|||
.select(diesel::dsl::count(basic_tools::id))
|
||||
.first::<i64>(&mut *conn)
|
||||
.map(|count| count > 0);
|
||||
|
||||
match tool_exists {
|
||||
Ok(true) => {
|
||||
info!(
|
||||
"Tool '{}' exists and is active for bot '{}'",
|
||||
tool_name, user.bot_id
|
||||
);
|
||||
trace!("Tool '{}' exists and is active for bot '{}'", tool_name, user.bot_id);
|
||||
}
|
||||
Ok(false) => {
|
||||
warn!(
|
||||
"Tool '{}' does not exist or is not active for bot '{}'",
|
||||
tool_name, user.bot_id
|
||||
);
|
||||
return Err(format!(
|
||||
"Tool '{}' is not available. Make sure the tool file is compiled and active.",
|
||||
tool_name
|
||||
));
|
||||
warn!("Tool '{}' does not exist or is not active for bot '{}'", tool_name, user.bot_id);
|
||||
return Err(format!("Tool '{}' is not available. Make sure the tool file is compiled and active.", tool_name));
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to check tool existence: {}", e);
|
||||
return Err(format!("Database error while checking tool: {}", e));
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a unique ID for the association
|
||||
let association_id = Uuid::new_v4().to_string();
|
||||
let session_id_str = user.id.to_string();
|
||||
let added_at = chrono::Utc::now().to_rfc3339();
|
||||
|
||||
// Insert the tool association (ignore if already exists due to UNIQUE constraint)
|
||||
let insert_result: Result<usize, diesel::result::Error> =
|
||||
diesel::insert_into(session_tool_associations::table)
|
||||
let insert_result: Result<usize, diesel::result::Error> = diesel::insert_into(session_tool_associations::table)
|
||||
.values((
|
||||
session_tool_associations::id.eq(&association_id),
|
||||
session_tool_associations::session_id.eq(&session_id_str),
|
||||
session_tool_associations::tool_name.eq(tool_name),
|
||||
session_tool_associations::added_at.eq(&added_at),
|
||||
))
|
||||
.on_conflict((
|
||||
session_tool_associations::session_id,
|
||||
session_tool_associations::tool_name,
|
||||
))
|
||||
.on_conflict((session_tool_associations::session_id, session_tool_associations::tool_name))
|
||||
.do_nothing()
|
||||
.execute(&mut *conn);
|
||||
|
||||
match insert_result {
|
||||
Ok(rows_affected) => {
|
||||
if rows_affected > 0 {
|
||||
info!(
|
||||
"Tool '{}' newly associated with session '{}' (user: {}, bot: {})",
|
||||
tool_name, user.id, user.user_id, user.bot_id
|
||||
);
|
||||
Ok(format!(
|
||||
"Tool '{}' is now available in this conversation",
|
||||
tool_name
|
||||
))
|
||||
trace!("Tool '{}' newly associated with session '{}' (user: {}, bot: {})", tool_name, user.id, user.user_id, user.bot_id);
|
||||
Ok(format!("Tool '{}' is now available in this conversation", tool_name))
|
||||
} else {
|
||||
info!(
|
||||
"Tool '{}' was already associated with session '{}'",
|
||||
tool_name, user.id
|
||||
);
|
||||
Ok(format!(
|
||||
"Tool '{}' is already available in this conversation",
|
||||
tool_name
|
||||
))
|
||||
trace!("Tool '{}' was already associated with session '{}'", tool_name, user.id);
|
||||
Ok(format!("Tool '{}' is already available in this conversation", tool_name))
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!(
|
||||
"Failed to associate tool '{}' with session '{}': {}",
|
||||
tool_name, user.id, e
|
||||
);
|
||||
error!("Failed to associate tool '{}' with session '{}': {}", tool_name, user.id, e);
|
||||
Err(format!("Failed to add tool to session: {}", e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get all tools associated with a session
|
||||
pub fn get_session_tools(
|
||||
conn: &mut PgConnection,
|
||||
session_id: &Uuid,
|
||||
) -> Result<Vec<String>, diesel::result::Error> {
|
||||
pub fn get_session_tools(conn: &mut PgConnection, session_id: &Uuid) -> Result<Vec<String>, diesel::result::Error> {
|
||||
use crate::shared::models::schema::session_tool_associations;
|
||||
|
||||
let session_id_str = session_id.to_string();
|
||||
|
||||
session_tool_associations::table
|
||||
.filter(session_tool_associations::session_id.eq(&session_id_str))
|
||||
.select(session_tool_associations::tool_name)
|
||||
.load::<String>(conn)
|
||||
}
|
||||
|
||||
/// Clear all tool associations for a session
|
||||
pub fn clear_session_tools(
|
||||
conn: &mut PgConnection,
|
||||
session_id: &Uuid,
|
||||
) -> Result<usize, diesel::result::Error> {
|
||||
pub fn clear_session_tools(conn: &mut PgConnection, session_id: &Uuid) -> Result<usize, diesel::result::Error> {
|
||||
use crate::shared::models::schema::session_tool_associations;
|
||||
|
||||
let session_id_str = session_id.to_string();
|
||||
|
||||
diesel::delete(
|
||||
session_tool_associations::table
|
||||
.filter(session_tool_associations::session_id.eq(&session_id_str)),
|
||||
)
|
||||
.execute(conn)
|
||||
diesel::delete(session_tool_associations::table.filter(session_tool_associations::session_id.eq(&session_id_str))).execute(conn)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,91 +1,51 @@
|
|||
use crate::shared::models::UserSession;
|
||||
use crate::shared::state::AppState;
|
||||
use log::{error, info};
|
||||
use log::{error, trace};
|
||||
use rhai::{Dynamic, Engine};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn add_website_keyword(state: Arc<AppState>, user: UserSession, engine: &mut Engine) {
|
||||
let state_clone = Arc::clone(&state);
|
||||
let user_clone = user.clone();
|
||||
|
||||
engine
|
||||
.register_custom_syntax(&["ADD_WEBSITE", "$expr$"], false, move |context, inputs| {
|
||||
let url = context.eval_expression_tree(&inputs[0])?;
|
||||
let url_str = url.to_string().trim_matches('"').to_string();
|
||||
|
||||
info!(
|
||||
"ADD_WEBSITE command executed: {} for user: {}",
|
||||
url_str, user_clone.user_id
|
||||
);
|
||||
|
||||
// Validate URL
|
||||
trace!("ADD_WEBSITE command executed: {} for user: {}", url_str, user_clone.user_id);
|
||||
let is_valid = url_str.starts_with("http://") || url_str.starts_with("https://");
|
||||
|
||||
if !is_valid {
|
||||
return Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
"Invalid URL format. Must start with http:// or https://".into(),
|
||||
rhai::Position::NONE,
|
||||
)));
|
||||
return Err(Box::new(rhai::EvalAltResult::ErrorRuntime("Invalid URL format. Must start with http:// or https://".into(), rhai::Position::NONE)));
|
||||
}
|
||||
|
||||
let state_for_task = Arc::clone(&state_clone);
|
||||
let user_for_task = user_clone.clone();
|
||||
let url_for_task = url_str.clone();
|
||||
|
||||
// Spawn async task to crawl and index website
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||
.worker_threads(2)
|
||||
.enable_all()
|
||||
.build();
|
||||
|
||||
let rt = tokio::runtime::Builder::new_multi_thread().worker_threads(2).enable_all().build();
|
||||
let send_err = if let Ok(rt) = rt {
|
||||
let result = rt.block_on(async move {
|
||||
crawl_and_index_website(&state_for_task, &user_for_task, &url_for_task)
|
||||
.await
|
||||
crawl_and_index_website(&state_for_task, &user_for_task, &url_for_task).await
|
||||
});
|
||||
tx.send(result).err()
|
||||
} else {
|
||||
tx.send(Err("Failed to build tokio runtime".to_string()))
|
||||
.err()
|
||||
tx.send(Err("Failed to build tokio runtime".to_string())).err()
|
||||
};
|
||||
|
||||
if send_err.is_some() {
|
||||
error!("Failed to send result from thread");
|
||||
}
|
||||
});
|
||||
|
||||
match rx.recv_timeout(std::time::Duration::from_secs(120)) {
|
||||
Ok(Ok(message)) => {
|
||||
info!("ADD_WEBSITE completed: {}", message);
|
||||
Ok(Dynamic::from(message))
|
||||
}
|
||||
Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
e.into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(e.into(), rhai::Position::NONE))),
|
||||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
|
||||
Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
"ADD_WEBSITE timed out".into(),
|
||||
rhai::Position::NONE,
|
||||
)))
|
||||
Err(Box::new(rhai::EvalAltResult::ErrorRuntime("ADD_WEBSITE timed out".into(), rhai::Position::NONE)))
|
||||
}
|
||||
Err(e) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
format!("ADD_WEBSITE failed: {}", e).into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
Err(e) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(format!("ADD_WEBSITE failed: {}", e).into(), rhai::Position::NONE))),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Crawl website and index content
|
||||
async fn crawl_and_index_website(
|
||||
_state: &AppState,
|
||||
user: &UserSession,
|
||||
url: &str,
|
||||
) -> Result<String, String> {
|
||||
info!("Crawling website: {} for user: {}", url, user.user_id);
|
||||
async fn crawl_and_index_website(_state: &AppState, _user: &UserSession, _url: &str) -> Result<String, String> {
|
||||
Err("Web automation functionality has been removed from this build".to_string())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use crate::shared::models::UserSession;
|
||||
use crate::shared::state::AppState;
|
||||
use diesel::prelude::*;
|
||||
use log::{error, info, trace};
|
||||
use log::{error, trace};
|
||||
use rhai::{Dynamic, Engine};
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
|
|
@ -9,34 +9,23 @@ use uuid::Uuid;
|
|||
pub fn set_bot_memory_keyword(state: Arc<AppState>, user: UserSession, engine: &mut Engine) {
|
||||
let state_clone = Arc::clone(&state);
|
||||
let user_clone = user.clone();
|
||||
|
||||
engine
|
||||
.register_custom_syntax(
|
||||
&["SET_BOT_MEMORY", "$expr$", ",", "$expr$"],
|
||||
false,
|
||||
move |context, inputs| {
|
||||
.register_custom_syntax(&["SET_BOT_MEMORY", "$expr$", ",", "$expr$"], false, move |context, inputs| {
|
||||
let key = context.eval_expression_tree(&inputs[0])?.to_string();
|
||||
let value = context.eval_expression_tree(&inputs[1])?.to_string();
|
||||
|
||||
let state_for_spawn = Arc::clone(&state_clone);
|
||||
let user_clone_spawn = user_clone.clone();
|
||||
let key_clone = key.clone();
|
||||
let value_clone = value.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
use crate::shared::models::bot_memories;
|
||||
|
||||
let mut conn = match state_for_spawn.conn.lock() {
|
||||
let mut conn = match state_for_spawn.conn.get() {
|
||||
Ok(conn) => conn,
|
||||
Err(e) => {
|
||||
error!(
|
||||
"Failed to acquire database connection for SET BOT MEMORY: {}",
|
||||
e
|
||||
);
|
||||
error!("Failed to acquire database connection for SET BOT MEMORY: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let bot_uuid = match Uuid::parse_str(&user_clone_spawn.bot_id.to_string()) {
|
||||
Ok(uuid) => uuid,
|
||||
Err(e) => {
|
||||
|
|
@ -44,9 +33,7 @@ pub fn set_bot_memory_keyword(state: Arc<AppState>, user: UserSession, engine: &
|
|||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let now = chrono::Utc::now();
|
||||
|
||||
let existing_memory: Option<Uuid> = bot_memories::table
|
||||
.filter(bot_memories::bot_id.eq(bot_uuid))
|
||||
.filter(bot_memories::key.eq(&key_clone))
|
||||
|
|
@ -54,24 +41,13 @@ pub fn set_bot_memory_keyword(state: Arc<AppState>, user: UserSession, engine: &
|
|||
.first(&mut *conn)
|
||||
.optional()
|
||||
.unwrap_or(None);
|
||||
|
||||
if let Some(memory_id) = existing_memory {
|
||||
let update_result = diesel::update(
|
||||
bot_memories::table.filter(bot_memories::id.eq(memory_id)),
|
||||
)
|
||||
.set((
|
||||
bot_memories::value.eq(&value_clone),
|
||||
bot_memories::updated_at.eq(now),
|
||||
))
|
||||
let update_result = diesel::update(bot_memories::table.filter(bot_memories::id.eq(memory_id)))
|
||||
.set((bot_memories::value.eq(&value_clone), bot_memories::updated_at.eq(now)))
|
||||
.execute(&mut *conn);
|
||||
|
||||
match update_result {
|
||||
Ok(_) => {
|
||||
info!(
|
||||
"Updated bot memory for key: {} with value length: {}",
|
||||
key_clone,
|
||||
value_clone.len()
|
||||
);
|
||||
trace!("Updated bot memory for key: {} with value length: {}", key_clone, value_clone.len());
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to update bot memory: {}", e);
|
||||
|
|
@ -86,18 +62,10 @@ pub fn set_bot_memory_keyword(state: Arc<AppState>, user: UserSession, engine: &
|
|||
created_at: now,
|
||||
updated_at: now,
|
||||
};
|
||||
|
||||
let insert_result = diesel::insert_into(bot_memories::table)
|
||||
.values(&new_memory)
|
||||
.execute(&mut *conn);
|
||||
|
||||
let insert_result = diesel::insert_into(bot_memories::table).values(&new_memory).execute(&mut *conn);
|
||||
match insert_result {
|
||||
Ok(_) => {
|
||||
info!(
|
||||
"Created new bot memory for key: {} with value length: {}",
|
||||
key_clone,
|
||||
value_clone.len()
|
||||
);
|
||||
trace!("Created new bot memory for key: {} with value length: {}", key_clone, value_clone.len());
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to insert bot memory: {}", e);
|
||||
|
|
@ -105,26 +73,20 @@ pub fn set_bot_memory_keyword(state: Arc<AppState>, user: UserSession, engine: &
|
|||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(Dynamic::UNIT)
|
||||
},
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn get_bot_memory_keyword(state: Arc<AppState>, user: UserSession, engine: &mut Engine) {
|
||||
let state_clone = Arc::clone(&state);
|
||||
let user_clone = user.clone();
|
||||
|
||||
engine.register_fn("GET_BOT_MEMORY", move |key_param: String| -> String {
|
||||
use crate::shared::models::bot_memories;
|
||||
|
||||
let state = Arc::clone(&state_clone);
|
||||
|
||||
let conn_result = state.conn.lock();
|
||||
let conn_result = state.conn.get();
|
||||
if let Ok(mut conn) = conn_result {
|
||||
let bot_uuid = user_clone.bot_id;
|
||||
|
||||
let memory_value: Option<String> = bot_memories::table
|
||||
.filter(bot_memories::bot_id.eq(bot_uuid))
|
||||
.filter(bot_memories::key.eq(&key_param))
|
||||
|
|
@ -132,8 +94,6 @@ pub fn get_bot_memory_keyword(state: Arc<AppState>, user: UserSession, engine: &
|
|||
.first(&mut *conn)
|
||||
.optional()
|
||||
.unwrap_or(None);
|
||||
|
||||
trace!("GET_MEMORY for key '{}' returned value: {:?}", key_param, memory_value);
|
||||
memory_value.unwrap_or_default()
|
||||
} else {
|
||||
String::new()
|
||||
|
|
|
|||
|
|
@ -1,97 +1,57 @@
|
|||
use crate::basic::keywords::add_tool::clear_session_tools;
|
||||
use crate::shared::models::UserSession;
|
||||
use crate::shared::state::AppState;
|
||||
use log::{error, info};
|
||||
use log::{error, trace};
|
||||
use rhai::{Dynamic, Engine};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn clear_tools_keyword(state: Arc<AppState>, user: UserSession, engine: &mut Engine) {
|
||||
let state_clone = Arc::clone(&state);
|
||||
let user_clone = user.clone();
|
||||
|
||||
engine
|
||||
.register_custom_syntax(&["CLEAR_TOOLS"], false, move |_context, _inputs| {
|
||||
info!(
|
||||
"CLEAR_TOOLS command executed for session: {}",
|
||||
user_clone.id
|
||||
);
|
||||
|
||||
trace!("CLEAR_TOOLS command executed for session: {}", user_clone.id);
|
||||
let state_for_task = Arc::clone(&state_clone);
|
||||
let user_for_task = user_clone.clone();
|
||||
|
||||
// Spawn async task to clear all tool associations from session
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||
.worker_threads(2)
|
||||
.enable_all()
|
||||
.build();
|
||||
|
||||
let rt = tokio::runtime::Builder::new_multi_thread().worker_threads(2).enable_all().build();
|
||||
let send_err = if let Ok(rt) = rt {
|
||||
let result = rt.block_on(async move {
|
||||
clear_all_tools_from_session(&state_for_task, &user_for_task).await
|
||||
});
|
||||
tx.send(result).err()
|
||||
} else {
|
||||
tx.send(Err("Failed to build tokio runtime".to_string()))
|
||||
.err()
|
||||
tx.send(Err("Failed to build tokio runtime".to_string())).err()
|
||||
};
|
||||
|
||||
if send_err.is_some() {
|
||||
error!("Failed to send result from thread");
|
||||
}
|
||||
});
|
||||
|
||||
match rx.recv_timeout(std::time::Duration::from_secs(10)) {
|
||||
Ok(Ok(message)) => {
|
||||
info!("CLEAR_TOOLS completed: {}", message);
|
||||
Ok(Dynamic::from(message))
|
||||
}
|
||||
Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
e.into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(e.into(), rhai::Position::NONE))),
|
||||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
|
||||
Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
"CLEAR_TOOLS timed out".into(),
|
||||
rhai::Position::NONE,
|
||||
)))
|
||||
Err(Box::new(rhai::EvalAltResult::ErrorRuntime("CLEAR_TOOLS timed out".into(), rhai::Position::NONE)))
|
||||
}
|
||||
Err(e) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
format!("CLEAR_TOOLS failed: {}", e).into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
Err(e) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(format!("CLEAR_TOOLS failed: {}", e).into(), rhai::Position::NONE))),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Clear all tool associations from the current session
|
||||
async fn clear_all_tools_from_session(
|
||||
state: &AppState,
|
||||
user: &UserSession,
|
||||
) -> Result<String, String> {
|
||||
let mut conn = state.conn.lock().map_err(|e| {
|
||||
async fn clear_all_tools_from_session(state: &AppState, user: &UserSession) -> Result<String, String> {
|
||||
let mut conn = state.conn.get().map_err(|e| {
|
||||
error!("Failed to acquire database lock: {}", e);
|
||||
format!("Database connection error: {}", e)
|
||||
})?;
|
||||
|
||||
// Clear all tool associations for this session
|
||||
let delete_result = clear_session_tools(&mut *conn, &user.id);
|
||||
|
||||
match delete_result {
|
||||
Ok(rows_affected) => {
|
||||
if rows_affected > 0 {
|
||||
info!(
|
||||
"Cleared {} tool(s) from session '{}' (user: {}, bot: {})",
|
||||
rows_affected, user.id, user.user_id, user.bot_id
|
||||
);
|
||||
Ok(format!(
|
||||
"All {} tool(s) have been removed from this conversation",
|
||||
rows_affected
|
||||
))
|
||||
trace!("Cleared {} tool(s) from session '{}' (user: {}, bot: {})", rows_affected, user.id, user.user_id, user.bot_id);
|
||||
Ok(format!("All {} tool(s) have been removed from this conversation", rows_affected))
|
||||
} else {
|
||||
info!("No tools were associated with session '{}'", user.id);
|
||||
Ok("No tools were active in this conversation".to_string())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,36 +3,22 @@ use crate::shared::state::AppState;
|
|||
use crate::shared::models::UserSession;
|
||||
use rhai::Dynamic;
|
||||
use rhai::Engine;
|
||||
|
||||
pub fn create_draft_keyword(state: &AppState, user: UserSession, engine: &mut Engine) {
|
||||
let state_clone = state.clone();
|
||||
|
||||
engine
|
||||
.register_custom_syntax(
|
||||
&["CREATE_DRAFT", "$expr$", ",", "$expr$", ",", "$expr$"],
|
||||
true,
|
||||
move |context, inputs| {
|
||||
.register_custom_syntax(&["CREATE_DRAFT", "$expr$", ",", "$expr$", ",", "$expr$"], true, move |context, inputs| {
|
||||
let to = context.eval_expression_tree(&inputs[0])?.to_string();
|
||||
let subject = context.eval_expression_tree(&inputs[1])?.to_string();
|
||||
let reply_text = context.eval_expression_tree(&inputs[2])?.to_string();
|
||||
|
||||
let fut = execute_create_draft(&state_clone, &to, &subject, &reply_text);
|
||||
let result =
|
||||
tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(fut))
|
||||
let result = tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(fut))
|
||||
.map_err(|e| format!("Draft creation error: {}", e))?;
|
||||
|
||||
Ok(Dynamic::from(result))
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn execute_create_draft(
|
||||
state: &AppState,
|
||||
to: &str,
|
||||
subject: &str,
|
||||
reply_text: &str,
|
||||
) -> Result<String, String> {
|
||||
async fn execute_create_draft(state: &AppState, to: &str, subject: &str, reply_text: &str) -> Result<String, String> {
|
||||
let get_result = fetch_latest_sent_to(&state.config.clone().unwrap().email, to).await;
|
||||
let email_body = if let Ok(get_result_str) = get_result {
|
||||
if !get_result_str.is_empty() {
|
||||
|
|
@ -40,24 +26,19 @@ async fn execute_create_draft(
|
|||
let formatted_reply_text = reply_text.to_string();
|
||||
let formatted_old_text = get_result_str.replace("\n", "<br>");
|
||||
let fixed_reply_text = formatted_reply_text.replace("FIX", "Fixed");
|
||||
format!(
|
||||
"{}{}{}",
|
||||
fixed_reply_text, email_separator, formatted_old_text
|
||||
)
|
||||
format!("{}{}{}", fixed_reply_text, email_separator, formatted_old_text)
|
||||
} else {
|
||||
reply_text.to_string()
|
||||
}
|
||||
} else {
|
||||
reply_text.to_string()
|
||||
};
|
||||
|
||||
let draft_request = SaveDraftRequest {
|
||||
to: to.to_string(),
|
||||
subject: subject.to_string(),
|
||||
cc: None,
|
||||
text: email_body,
|
||||
};
|
||||
|
||||
let save_result = save_email_draft(&state.config.clone().unwrap().email, &draft_request).await;
|
||||
match save_result {
|
||||
Ok(_) => Ok("Draft saved successfully".to_string()),
|
||||
|
|
|
|||
|
|
@ -1,87 +1,50 @@
|
|||
use log::info;
|
||||
use rhai::Dynamic;
|
||||
use rhai::Engine;
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::io::Read;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::shared::models::UserSession;
|
||||
use crate::shared::state::AppState;
|
||||
|
||||
pub fn create_site_keyword(state: &AppState, _user: UserSession, engine: &mut Engine) {
|
||||
let state_clone = state.clone();
|
||||
engine
|
||||
.register_custom_syntax(
|
||||
&["CREATE_SITE", "$expr$", ",", "$expr$", ",", "$expr$"],
|
||||
true,
|
||||
move |context, inputs| {
|
||||
.register_custom_syntax(&["CREATE_SITE", "$expr$", ",", "$expr$", ",", "$expr$"], true, move |context, inputs| {
|
||||
if inputs.len() < 3 {
|
||||
return Err("Not enough arguments for CREATE SITE".into());
|
||||
}
|
||||
|
||||
let alias = context.eval_expression_tree(&inputs[0])?;
|
||||
let template_dir = context.eval_expression_tree(&inputs[1])?;
|
||||
let prompt = context.eval_expression_tree(&inputs[2])?;
|
||||
|
||||
let config = state_clone
|
||||
.config
|
||||
.as_ref()
|
||||
.expect("Config must be initialized")
|
||||
.clone();
|
||||
|
||||
let config = state_clone.config.as_ref().expect("Config must be initialized").clone();
|
||||
let fut = create_site(&config, alias, template_dir, prompt);
|
||||
let result =
|
||||
tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(fut))
|
||||
let result = tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(fut))
|
||||
.map_err(|e| format!("Site creation failed: {}", e))?;
|
||||
|
||||
Ok(Dynamic::from(result))
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn create_site(
|
||||
config: &crate::config::AppConfig,
|
||||
alias: Dynamic,
|
||||
template_dir: Dynamic,
|
||||
prompt: Dynamic,
|
||||
) -> Result<String, Box<dyn Error + Send + Sync>> {
|
||||
async fn create_site(config: &crate::config::AppConfig, alias: Dynamic, template_dir: Dynamic, prompt: Dynamic) -> Result<String, Box<dyn Error + Send + Sync>> {
|
||||
let base_path = PathBuf::from(&config.site_path);
|
||||
let template_path = base_path.join(template_dir.to_string());
|
||||
let alias_path = base_path.join(alias.to_string());
|
||||
|
||||
fs::create_dir_all(&alias_path).map_err(|e| e.to_string())?;
|
||||
|
||||
let mut combined_content = String::new();
|
||||
|
||||
for entry in fs::read_dir(&template_path).map_err(|e| e.to_string())? {
|
||||
let entry = entry.map_err(|e| e.to_string())?;
|
||||
let path = entry.path();
|
||||
|
||||
if path.extension().map_or(false, |ext| ext == "html") {
|
||||
let mut file = fs::File::open(&path).map_err(|e| e.to_string())?;
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
file.read_to_string(&mut contents).map_err(|e| e.to_string())?;
|
||||
combined_content.push_str(&contents);
|
||||
combined_content.push_str("\n\n--- TEMPLATE SEPARATOR ---\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
let _full_prompt = format!(
|
||||
"TEMPLATE FILES:\n{}\n\nPROMPT: {}\n\nGenerate a new HTML file cloning all previous TEMPLATE (keeping only the local _assets libraries use, no external resources), but turning this into this prompt:",
|
||||
combined_content,
|
||||
prompt.to_string()
|
||||
);
|
||||
|
||||
info!("Asking LLM to create site.");
|
||||
let llm_result = "".to_string(); // TODO:
|
||||
|
||||
let _full_prompt = format!("TEMPLATE FILES:\n{}\n\nPROMPT: {}\n\nGenerate a new HTML file cloning all previous TEMPLATE (keeping only the local _assets libraries use, no external resources), but turning this into this prompt:", combined_content, prompt.to_string());
|
||||
let llm_result = "".to_string();
|
||||
let index_path = alias_path.join("index.html");
|
||||
fs::write(index_path, llm_result).map_err(|e| e.to_string())?;
|
||||
|
||||
info!("Site created at: {}", alias_path.display());
|
||||
Ok(alias_path.to_string_lossy().into_owned())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,36 +1,29 @@
|
|||
use diesel::pg::PgConnection;
|
||||
use diesel::prelude::*;
|
||||
use log::{error, info};
|
||||
use rhai::Dynamic;
|
||||
use rhai::Engine;
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use crate::shared::models::UserSession;
|
||||
use crate::shared::state::AppState;
|
||||
use crate::shared::utils;
|
||||
use crate::shared::utils::to_array;
|
||||
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::prelude::*;
|
||||
use log::error;
|
||||
use log::trace;
|
||||
use rhai::Dynamic;
|
||||
use rhai::Engine;
|
||||
use serde_json::{json, Value};
|
||||
pub fn find_keyword(state: &AppState, _user: UserSession, engine: &mut Engine) {
|
||||
let connection = state.conn.clone();
|
||||
|
||||
engine
|
||||
.register_custom_syntax(&["FIND", "$expr$", ",", "$expr$"], false, {
|
||||
move |context, inputs| {
|
||||
let table_name = context.eval_expression_tree(&inputs[0])?;
|
||||
let filter = context.eval_expression_tree(&inputs[1])?;
|
||||
let mut binding = connection.lock().unwrap();
|
||||
|
||||
// Use the current async context instead of creating a new runtime
|
||||
let mut binding = connection.get().map_err(|e| format!("DB error: {}", e))?;
|
||||
let binding2 = table_name.to_string();
|
||||
let binding3 = filter.to_string();
|
||||
|
||||
// Since execute_find is async but we're in a sync context, we need to block on it
|
||||
let result = tokio::task::block_in_place(|| {
|
||||
tokio::runtime::Handle::current()
|
||||
.block_on(async { execute_find(&mut binding, &binding2, &binding3).await })
|
||||
})
|
||||
.map_err(|e| format!("DB error: {}", e))?;
|
||||
|
||||
if let Some(results) = result.get("results") {
|
||||
let array = to_array(utils::json_value_to_dynamic(results));
|
||||
Ok(Dynamic::from(array))
|
||||
|
|
@ -41,52 +34,33 @@ pub fn find_keyword(state: &AppState, _user: UserSession, engine: &mut Engine) {
|
|||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub async fn execute_find(
|
||||
conn: &mut PgConnection,
|
||||
table_str: &str,
|
||||
filter_str: &str,
|
||||
) -> Result<Value, String> {
|
||||
// Changed to String error like your Actix code
|
||||
info!(
|
||||
trace!(
|
||||
"Starting execute_find with table: {}, filter: {}",
|
||||
table_str, filter_str
|
||||
table_str,
|
||||
filter_str
|
||||
);
|
||||
|
||||
let (where_clause, params) = utils::parse_filter(filter_str).map_err(|e| e.to_string())?;
|
||||
|
||||
let query = format!(
|
||||
"SELECT * FROM {} WHERE {} LIMIT 10",
|
||||
table_str, where_clause
|
||||
);
|
||||
info!("Executing query: {}", query);
|
||||
|
||||
|
||||
// Execute raw SQL and get raw results
|
||||
let raw_result = diesel::sql_query(&query)
|
||||
let _raw_result = diesel::sql_query(&query)
|
||||
.bind::<diesel::sql_types::Text, _>(¶ms[0])
|
||||
.execute(conn)
|
||||
.map_err(|e| {
|
||||
error!("SQL execution error: {}", e);
|
||||
e.to_string()
|
||||
})?;
|
||||
|
||||
info!("Query executed successfully, affected {} rows", raw_result);
|
||||
|
||||
// For now, create placeholder results since we can't easily deserialize dynamic rows
|
||||
let mut results = Vec::new();
|
||||
|
||||
// This is a simplified approach - in a real implementation you'd need to:
|
||||
// 1. Query the table schema to know column types
|
||||
// 2. Build a proper struct or use a more flexible approach
|
||||
// 3. Or use a different database library that supports dynamic queries better
|
||||
|
||||
// Placeholder result for demonstration
|
||||
let json_row = serde_json::json!({
|
||||
"note": "Dynamic row deserialization not implemented - need table schema"
|
||||
});
|
||||
results.push(json_row);
|
||||
|
||||
Ok(json!({
|
||||
"command": "find",
|
||||
"table": table_str,
|
||||
|
|
|
|||
|
|
@ -1,184 +1,14 @@
|
|||
use rhai::Dynamic;
|
||||
use rhai::Engine;
|
||||
|
||||
pub fn first_keyword(engine: &mut Engine) {
|
||||
engine
|
||||
.register_custom_syntax(&["FIRST", "$expr$"], false, {
|
||||
move |context, inputs| {
|
||||
let input_string = context.eval_expression_tree(&inputs[0])?;
|
||||
let input_str = input_string.to_string();
|
||||
|
||||
let first_word = input_str
|
||||
.split_whitespace()
|
||||
.next()
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
|
||||
let first_word = input_str.split_whitespace().next().unwrap_or("").to_string();
|
||||
Ok(Dynamic::from(first_word))
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use rhai::Engine;
|
||||
|
||||
fn setup_engine() -> Engine {
|
||||
let mut engine = Engine::new();
|
||||
first_keyword(&mut engine);
|
||||
engine
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_first_keyword_basic() {
|
||||
let engine = setup_engine();
|
||||
|
||||
let result = engine
|
||||
.eval::<String>(
|
||||
r#"
|
||||
FIRST "hello world"
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, "hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_first_keyword_single_word() {
|
||||
let engine = setup_engine();
|
||||
|
||||
let result = engine
|
||||
.eval::<String>(
|
||||
r#"
|
||||
FIRST "single"
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, "single");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_first_keyword_multiple_spaces() {
|
||||
let engine = setup_engine();
|
||||
|
||||
let result = engine
|
||||
.eval::<String>(
|
||||
r#"
|
||||
FIRST " leading spaces"
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, "leading");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_first_keyword_empty_string() {
|
||||
let engine = setup_engine();
|
||||
|
||||
let result = engine
|
||||
.eval::<String>(
|
||||
r#"
|
||||
FIRST ""
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_first_keyword_whitespace_only() {
|
||||
let engine = setup_engine();
|
||||
|
||||
let result = engine
|
||||
.eval::<String>(
|
||||
r#"
|
||||
FIRST " "
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_first_keyword_with_tabs() {
|
||||
let engine = setup_engine();
|
||||
|
||||
let result = engine
|
||||
.eval::<String>(
|
||||
r#"
|
||||
FIRST " tab separated words"
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, "tab");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_first_keyword_with_variable() {
|
||||
let engine = setup_engine();
|
||||
|
||||
let result = engine
|
||||
.eval::<String>(
|
||||
r#"
|
||||
let text = "variable test";
|
||||
FIRST text
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, "variable");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_first_keyword_with_expression() {
|
||||
let engine = setup_engine();
|
||||
|
||||
let result = engine
|
||||
.eval::<String>(
|
||||
r#"
|
||||
FIRST "one two " + "three four"
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, "one");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_first_keyword_mixed_whitespace() {
|
||||
let engine = setup_engine();
|
||||
|
||||
let result = engine
|
||||
.eval::<String>(
|
||||
r#"
|
||||
FIRST " multiple spaces between words "
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, "multiple");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_first_keyword_special_characters() {
|
||||
let engine = setup_engine();
|
||||
|
||||
let result = engine
|
||||
.eval::<String>(
|
||||
r#"
|
||||
FIRST "hello-world example"
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, "hello-world");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,56 +1,32 @@
|
|||
use crate::shared::models::UserSession;
|
||||
use crate::shared::state::AppState;
|
||||
use log::info;
|
||||
use rhai::Dynamic;
|
||||
use rhai::Engine;
|
||||
|
||||
pub fn for_keyword(_state: &AppState, _user: UserSession, engine: &mut Engine) {
|
||||
engine
|
||||
.register_custom_syntax(&["EXIT", "FOR"], false, |_context, _inputs| {
|
||||
Err("EXIT FOR".into())
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
engine
|
||||
.register_custom_syntax(
|
||||
&[
|
||||
"FOR", "EACH", "$ident$", "IN", "$expr$", "$block$", "NEXT", "$ident$",
|
||||
],
|
||||
true,
|
||||
|context, inputs| {
|
||||
.register_custom_syntax(&["FOR", "EACH", "$ident$", "IN", "$expr$", "$block$", "NEXT", "$ident$"], true, |context, inputs| {
|
||||
let loop_var = inputs[0].get_string_value().unwrap();
|
||||
let next_var = inputs[3].get_string_value().unwrap();
|
||||
|
||||
if loop_var != next_var {
|
||||
return Err(format!(
|
||||
"NEXT variable '{}' doesn't match FOR EACH variable '{}'",
|
||||
next_var, loop_var
|
||||
)
|
||||
.into());
|
||||
return Err(format!("NEXT variable '{}' doesn't match FOR EACH variable '{}'", next_var, loop_var).into());
|
||||
}
|
||||
|
||||
let collection = context.eval_expression_tree(&inputs[1])?;
|
||||
|
||||
info!("Collection type: {}", collection.type_name());
|
||||
let ccc = collection.clone();
|
||||
let array = match collection.into_array() {
|
||||
Ok(arr) => arr,
|
||||
Err(err) => {
|
||||
return Err(format!(
|
||||
"foreach expected array, got {}: {}",
|
||||
ccc.type_name(),
|
||||
err
|
||||
)
|
||||
.into());
|
||||
return Err(format!("foreach expected array, got {}: {}", ccc.type_name(), err).into());
|
||||
}
|
||||
};
|
||||
let block = &inputs[2];
|
||||
|
||||
let orig_len = context.scope().len();
|
||||
|
||||
for item in array {
|
||||
context.scope_mut().push(loop_var, item);
|
||||
|
||||
match context.eval_expression_tree(block) {
|
||||
Ok(_) => (),
|
||||
Err(e) if e.to_string() == "EXIT FOR" => {
|
||||
|
|
@ -62,10 +38,8 @@ pub fn for_keyword(_state: &AppState, _user: UserSession, engine: &mut Engine) {
|
|||
return Err(e);
|
||||
}
|
||||
}
|
||||
|
||||
context.scope_mut().rewind(orig_len);
|
||||
}
|
||||
|
||||
Ok(Dynamic::UNIT)
|
||||
},
|
||||
)
|
||||
|
|
|
|||
|
|
@ -2,50 +2,30 @@ use rhai::{Dynamic, Engine};
|
|||
use chrono::{NaiveDateTime, Timelike, Datelike};
|
||||
use num_format::{Locale, ToFormattedString};
|
||||
use std::str::FromStr;
|
||||
|
||||
pub fn format_keyword(engine: &mut Engine) {
|
||||
engine
|
||||
.register_custom_syntax(&["FORMAT", "$expr$", "$expr$"], false, {
|
||||
move |context, inputs| {
|
||||
let value_dyn = context.eval_expression_tree(&inputs[0])?;
|
||||
let pattern_dyn = context.eval_expression_tree(&inputs[1])?;
|
||||
|
||||
let value_str = value_dyn.to_string();
|
||||
let pattern = pattern_dyn.to_string();
|
||||
|
||||
if let Ok(num) = f64::from_str(&value_str) {
|
||||
let formatted = if pattern.starts_with("N") || pattern.starts_with("C") {
|
||||
let (prefix, decimals, locale_tag) = parse_pattern(&pattern);
|
||||
|
||||
let locale = get_locale(&locale_tag);
|
||||
let symbol = if prefix == "C" {
|
||||
get_currency_symbol(&locale_tag)
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
let symbol = if prefix == "C" { get_currency_symbol(&locale_tag) } else { "" };
|
||||
let int_part = num.trunc() as i64;
|
||||
let frac_part = num.fract();
|
||||
|
||||
if decimals == 0 {
|
||||
format!("{}{}", symbol, int_part.to_formatted_string(&locale))
|
||||
} else {
|
||||
let frac_scaled =
|
||||
((frac_part * 10f64.powi(decimals as i32)).round()) as i64;
|
||||
|
||||
let frac_scaled = ((frac_part * 10f64.powi(decimals as i32)).round()) as i64;
|
||||
let decimal_sep = match locale_tag.as_str() {
|
||||
"pt" | "fr" | "es" | "it" | "de" => ",",
|
||||
_ => "."
|
||||
};
|
||||
|
||||
format!(
|
||||
"{}{}{}{:0width$}",
|
||||
symbol,
|
||||
int_part.to_formatted_string(&locale),
|
||||
decimal_sep,
|
||||
frac_scaled,
|
||||
width = decimals
|
||||
)
|
||||
format!("{}{}{}{:0width$}", symbol, int_part.to_formatted_string(&locale), decimal_sep, frac_scaled, width = decimals)
|
||||
}
|
||||
} else {
|
||||
match pattern.as_str() {
|
||||
|
|
@ -56,33 +36,27 @@ pub fn format_keyword(engine: &mut Engine) {
|
|||
_ => format!("{}", num),
|
||||
}
|
||||
};
|
||||
|
||||
return Ok(Dynamic::from(formatted));
|
||||
}
|
||||
|
||||
if let Ok(dt) = NaiveDateTime::parse_from_str(&value_str, "%Y-%m-%d %H:%M:%S") {
|
||||
let formatted = apply_date_format(&dt, &pattern);
|
||||
return Ok(Dynamic::from(formatted));
|
||||
}
|
||||
|
||||
let formatted = apply_text_placeholders(&value_str, &pattern);
|
||||
Ok(Dynamic::from(formatted))
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn parse_pattern(pattern: &str) -> (String, usize, String) {
|
||||
let mut prefix = String::new();
|
||||
let mut decimals: usize = 2;
|
||||
let mut locale_tag = "en".to_string();
|
||||
|
||||
if pattern.starts_with('C') {
|
||||
prefix = "C".to_string();
|
||||
} else if pattern.starts_with('N') {
|
||||
prefix = "N".to_string();
|
||||
}
|
||||
|
||||
let rest = &pattern[1..];
|
||||
let mut num_part = String::new();
|
||||
for ch in rest.chars() {
|
||||
|
|
@ -95,7 +69,6 @@ fn parse_pattern(pattern: &str) -> (String, usize, String) {
|
|||
if !num_part.is_empty() {
|
||||
decimals = num_part.parse().unwrap_or(2);
|
||||
}
|
||||
|
||||
if let Some(start) = pattern.find('[') {
|
||||
if let Some(end) = pattern.find(']') {
|
||||
if end > start {
|
||||
|
|
@ -103,10 +76,8 @@ fn parse_pattern(pattern: &str) -> (String, usize, String) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
(prefix, decimals, locale_tag)
|
||||
}
|
||||
|
||||
fn get_locale(tag: &str) -> Locale {
|
||||
match tag {
|
||||
"en" => Locale::en,
|
||||
|
|
@ -118,7 +89,6 @@ fn get_locale(tag: &str) -> Locale {
|
|||
_ => Locale::en,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_currency_symbol(tag: &str) -> &'static str {
|
||||
match tag {
|
||||
"en" => "$",
|
||||
|
|
@ -127,10 +97,8 @@ fn get_currency_symbol(tag: &str) -> &'static str {
|
|||
_ => "$",
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_date_format(dt: &NaiveDateTime, pattern: &str) -> String {
|
||||
let mut output = pattern.to_string();
|
||||
|
||||
let year = dt.year();
|
||||
let month = dt.month();
|
||||
let day = dt.day();
|
||||
|
|
@ -138,47 +106,36 @@ fn apply_date_format(dt: &NaiveDateTime, pattern: &str) -> String {
|
|||
let minute = dt.minute();
|
||||
let second = dt.second();
|
||||
let millis = dt.and_utc().timestamp_subsec_millis();
|
||||
|
||||
output = output.replace("yyyy", &format!("{:04}", year));
|
||||
output = output.replace("yy", &format!("{:02}", year % 100));
|
||||
output = output.replace("MM", &format!("{:02}", month));
|
||||
output = output.replace("M", &format!("{}", month));
|
||||
output = output.replace("dd", &format!("{:02}", day));
|
||||
output = output.replace("d", &format!("{}", day));
|
||||
|
||||
output = output.replace("HH", &format!("{:02}", hour24));
|
||||
output = output.replace("H", &format!("{}", hour24));
|
||||
|
||||
let mut hour12 = hour24 % 12;
|
||||
if hour12 == 0 { hour12 = 12; }
|
||||
output = output.replace("hh", &format!("{:02}", hour12));
|
||||
output = output.replace("h", &format!("{}", hour12));
|
||||
|
||||
output = output.replace("mm", &format!("{:02}", minute));
|
||||
output = output.replace("m", &format!("{}", minute));
|
||||
|
||||
output = output.replace("ss", &format!("{:02}", second));
|
||||
output = output.replace("s", &format!("{}", second));
|
||||
|
||||
output = output.replace("fff", &format!("{:03}", millis));
|
||||
|
||||
output = output.replace("tt", if hour24 < 12 { "AM" } else { "PM" });
|
||||
output = output.replace("t", if hour24 < 12 { "A" } else { "P" });
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
fn apply_text_placeholders(value: &str, pattern: &str) -> String {
|
||||
let mut result = String::new();
|
||||
let mut i = 0;
|
||||
let chars: Vec<char> = pattern.chars().collect();
|
||||
|
||||
while i < chars.len() {
|
||||
match chars[i] {
|
||||
'@' => result.push_str(value),
|
||||
'&' => {
|
||||
result.push_str(&value.to_lowercase());
|
||||
// Handle modifiers
|
||||
if i + 1 < chars.len() {
|
||||
match chars[i+1] {
|
||||
'!' => {
|
||||
|
|
@ -197,108 +154,5 @@ fn apply_text_placeholders(value: &str, pattern: &str) -> String {
|
|||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use rhai::Engine;
|
||||
|
||||
fn create_engine() -> Engine {
|
||||
let mut engine = Engine::new();
|
||||
format_keyword(&mut engine);
|
||||
engine
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_numeric_formatting_basic() {
|
||||
let engine = create_engine();
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<String>("FORMAT 1234.567 \"n\"").unwrap(),
|
||||
"1234.57"
|
||||
);
|
||||
assert_eq!(
|
||||
engine.eval::<String>("FORMAT 1234.5 \"F\"").unwrap(),
|
||||
"1234.50"
|
||||
);
|
||||
assert_eq!(
|
||||
engine.eval::<String>("FORMAT 1234.567 \"f\"").unwrap(),
|
||||
"1234.567"
|
||||
);
|
||||
assert_eq!(
|
||||
engine.eval::<String>("FORMAT 0.85 \"0%\"").unwrap(),
|
||||
"85%"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_numeric_formatting_with_locale() {
|
||||
let engine = create_engine();
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<String>("FORMAT 1234.56 \"N[en]\"").unwrap(),
|
||||
"1,234.56"
|
||||
);
|
||||
assert_eq!(
|
||||
engine.eval::<String>("FORMAT 1234.56 \"N[pt]\"").unwrap(),
|
||||
"1.234,56"
|
||||
);
|
||||
assert_eq!(
|
||||
engine.eval::<String>("FORMAT 1234.56 \"N[fr]\"").unwrap(),
|
||||
"1 234,56"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_currency_formatting() {
|
||||
let engine = create_engine();
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<String>("FORMAT 1234.56 \"C[en]\"").unwrap(),
|
||||
"$1,234.56"
|
||||
);
|
||||
assert_eq!(
|
||||
engine.eval::<String>("FORMAT 1234.56 \"C[pt]\"").unwrap(),
|
||||
"R$ 1.234,56"
|
||||
);
|
||||
assert_eq!(
|
||||
engine.eval::<String>("FORMAT 1234.56 \"C[fr]\"").unwrap(),
|
||||
"€1 234,56"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_date_formatting() {
|
||||
let engine = create_engine();
|
||||
|
||||
let result = engine.eval::<String>("FORMAT \"2024-03-15 14:30:25\" \"yyyy-MM-dd HH:mm:ss\"").unwrap();
|
||||
assert_eq!(result, "2024-03-15 14:30:25");
|
||||
|
||||
let result = engine.eval::<String>("FORMAT \"2024-03-15 14:30:25\" \"dd/MM/yyyy\"").unwrap();
|
||||
assert_eq!(result, "15/03/2024");
|
||||
|
||||
let result = engine.eval::<String>("FORMAT \"2024-03-15 14:30:25\" \"MM/dd/yy\"").unwrap();
|
||||
assert_eq!(result, "03/15/24");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_text_formatting() {
|
||||
let engine = create_engine();
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<String>("FORMAT \"hello\" \"Prefix: @\"").unwrap(),
|
||||
"Prefix: hello"
|
||||
);
|
||||
assert_eq!(
|
||||
engine.eval::<String>("FORMAT \"HELLO\" \"Result: &!\"").unwrap(),
|
||||
"Result: hello!"
|
||||
);
|
||||
assert_eq!(
|
||||
engine.eval::<String>("FORMAT \"hello\" \"RESULT: >\"").unwrap(),
|
||||
"RESULT: HELLO"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,53 +1,46 @@
|
|||
use crate::shared::models::schema::bots::dsl::*;
|
||||
use diesel::prelude::*;
|
||||
use crate::shared::models::UserSession;
|
||||
use crate::shared::state::AppState;
|
||||
use log::{debug, error, info, trace};
|
||||
use diesel::prelude::*;
|
||||
use log::{error, trace};
|
||||
use reqwest::{self, Client};
|
||||
use rhai::{Dynamic, Engine};
|
||||
use std::error::Error;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
pub fn get_keyword(state: Arc<AppState>, user_session: UserSession, engine: &mut Engine) {
|
||||
let state_clone = Arc::clone(&state);
|
||||
|
||||
engine
|
||||
.register_custom_syntax(&["GET", "$expr$"], false, move |context, inputs| {
|
||||
let url = context.eval_expression_tree(&inputs[0])?;
|
||||
let url_str = url.to_string();
|
||||
|
||||
info!("GET command executed: {}", url_str);
|
||||
|
||||
if !is_safe_path(&url_str) {
|
||||
return Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
"URL contains invalid or unsafe path sequences".into(),
|
||||
rhai::Position::NONE,
|
||||
)));
|
||||
}
|
||||
|
||||
let state_for_blocking = Arc::clone(&state_clone);
|
||||
let url_for_blocking = url_str.clone();
|
||||
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||
.worker_threads(2)
|
||||
.enable_all()
|
||||
.build();
|
||||
|
||||
let send_err = if let Ok(rt) = rt {
|
||||
let result = rt.block_on(async move {
|
||||
if url_for_blocking.starts_with("https://")
|
||||
|| url_for_blocking.starts_with("http://")
|
||||
{
|
||||
info!("HTTP(S) GET request: {}", url_for_blocking);
|
||||
execute_get(&url_for_blocking).await
|
||||
} else {
|
||||
info!("Local file GET request from bucket: {}", url_for_blocking);
|
||||
get_from_bucket(&state_for_blocking, &url_for_blocking,
|
||||
user_session.bot_id)
|
||||
get_from_bucket(
|
||||
&state_for_blocking,
|
||||
&url_for_blocking,
|
||||
user_session.bot_id,
|
||||
)
|
||||
.await
|
||||
}
|
||||
});
|
||||
|
|
@ -55,12 +48,10 @@ pub fn get_keyword(state: Arc<AppState>, user_session: UserSession, engine: &mut
|
|||
} else {
|
||||
tx.send(Err("failed to build tokio runtime".into())).err()
|
||||
};
|
||||
|
||||
if send_err.is_some() {
|
||||
error!("Failed to send result from thread");
|
||||
}
|
||||
});
|
||||
|
||||
match rx.recv_timeout(std::time::Duration::from_secs(40)) {
|
||||
Ok(Ok(content)) => Ok(Dynamic::from(content)),
|
||||
Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
|
|
@ -78,7 +69,6 @@ pub fn get_keyword(state: Arc<AppState>, user_session: UserSession, engine: &mut
|
|||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn is_safe_path(path: &str) -> bool {
|
||||
if path.starts_with("https://") || path.starts_with("http://") {
|
||||
return true;
|
||||
|
|
@ -105,10 +95,7 @@ fn is_safe_path(path: &str) -> bool {
|
|||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub async fn execute_get(url: &str) -> Result<String, Box<dyn Error + Send + Sync>> {
|
||||
debug!("Starting execute_get with URL: {}", url);
|
||||
|
||||
let client = Client::builder()
|
||||
.timeout(Duration::from_secs(30))
|
||||
.connect_timeout(Duration::from_secs(10))
|
||||
|
|
@ -118,12 +105,10 @@ pub async fn execute_get(url: &str) -> Result<String, Box<dyn Error + Send + Syn
|
|||
error!("Failed to build HTTP client: {}", e);
|
||||
e
|
||||
})?;
|
||||
|
||||
let response = client.get(url).send().await.map_err(|e| {
|
||||
error!("HTTP request failed for URL {}: {}", url, e);
|
||||
e
|
||||
})?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
let status = response.status();
|
||||
let error_body = response.text().await.unwrap_or_default();
|
||||
|
|
@ -137,35 +122,29 @@ pub async fn execute_get(url: &str) -> Result<String, Box<dyn Error + Send + Syn
|
|||
)
|
||||
.into());
|
||||
}
|
||||
|
||||
let content = response.text().await.map_err(|e| {
|
||||
error!("Failed to read response text for URL {}: {}", url, e);
|
||||
e
|
||||
})?;
|
||||
|
||||
debug!(
|
||||
trace!(
|
||||
"Successfully executed GET request for URL: {}, content length: {}",
|
||||
url,
|
||||
content.len()
|
||||
);
|
||||
Ok(content)
|
||||
}
|
||||
|
||||
pub async fn get_from_bucket(
|
||||
state: &AppState,
|
||||
file_path: &str,
|
||||
bot_id: uuid::Uuid,
|
||||
) -> Result<String, Box<dyn Error + Send + Sync>> {
|
||||
debug!("Getting file from bucket: {}", file_path);
|
||||
|
||||
if !is_safe_path(file_path) {
|
||||
error!("Unsafe file path detected: {}", file_path);
|
||||
return Err("Invalid file path".into());
|
||||
}
|
||||
|
||||
let client = state.drive.as_ref().ok_or("S3 client not configured")?;
|
||||
let bot_name: String = {
|
||||
let mut db_conn = state.conn.lock().unwrap();
|
||||
let mut db_conn = state.conn.get().map_err(|e| format!("DB error: {}", e))?;
|
||||
bots.filter(id.eq(&bot_id))
|
||||
.select(name)
|
||||
.first(&mut *db_conn)
|
||||
|
|
@ -174,16 +153,11 @@ pub async fn get_from_bucket(
|
|||
e
|
||||
})?
|
||||
};
|
||||
|
||||
let bucket_name = {
|
||||
let bucket = format!("{}.gbai", bot_name);
|
||||
trace!("Resolved GET bucket name: {}", bucket);
|
||||
bucket
|
||||
};
|
||||
|
||||
let bytes = match tokio::time::timeout(
|
||||
Duration::from_secs(30),
|
||||
async {
|
||||
let bytes = match tokio::time::timeout(Duration::from_secs(30), async {
|
||||
let result: Result<Vec<u8>, Box<dyn Error + Send + Sync>> = match client
|
||||
.get_object()
|
||||
.bucket(&bucket_name)
|
||||
|
|
@ -198,8 +172,7 @@ pub async fn get_from_bucket(
|
|||
Err(e) => Err(format!("S3 operation failed: {}", e).into()),
|
||||
};
|
||||
result
|
||||
},
|
||||
)
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(Ok(data)) => data.to_vec(),
|
||||
|
|
@ -212,7 +185,6 @@ pub async fn get_from_bucket(
|
|||
return Err("drive operation timed out".into());
|
||||
}
|
||||
};
|
||||
|
||||
let content = if file_path.to_ascii_lowercase().ends_with(".pdf") {
|
||||
match pdf_extract::extract_text_from_mem(&bytes) {
|
||||
Ok(text) => text,
|
||||
|
|
@ -230,8 +202,7 @@ pub async fn get_from_bucket(
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
info!(
|
||||
trace!(
|
||||
"Successfully retrieved file from bucket: {}, content length: {}",
|
||||
file_path,
|
||||
content.len()
|
||||
|
|
|
|||
|
|
@ -1,38 +1,22 @@
|
|||
use crate::shared::models::{BotResponse, UserSession};
|
||||
use crate::shared::state::AppState;
|
||||
use log::{debug, error, info};
|
||||
use log::{error, trace};
|
||||
use rhai::{Dynamic, Engine, EvalAltResult};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn hear_keyword(state: Arc<AppState>, user: UserSession, engine: &mut Engine) {
|
||||
let session_id = user.id;
|
||||
let state_clone = Arc::clone(&state);
|
||||
|
||||
engine
|
||||
.register_custom_syntax(&["HEAR", "$ident$"], true, move |_context, inputs| {
|
||||
let variable_name = inputs[0]
|
||||
.get_string_value()
|
||||
.expect("Expected identifier as string")
|
||||
.to_string();
|
||||
|
||||
info!(
|
||||
"HEAR command waiting for user input to store in variable: {}",
|
||||
variable_name
|
||||
);
|
||||
|
||||
let variable_name = inputs[0].get_string_value().expect("Expected identifier as string").to_string();
|
||||
trace!("HEAR command waiting for user input to store in variable: {}", variable_name);
|
||||
let state_for_spawn = Arc::clone(&state_clone);
|
||||
let session_id_clone = session_id;
|
||||
let var_name_clone = variable_name.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
debug!(
|
||||
"HEAR: Setting session {} to wait for input for variable '{}'",
|
||||
session_id_clone, var_name_clone
|
||||
);
|
||||
|
||||
trace!("HEAR: Setting session {} to wait for input for variable '{}'", session_id_clone, var_name_clone);
|
||||
let mut session_manager = state_for_spawn.session_manager.lock().await;
|
||||
session_manager.mark_waiting(session_id_clone);
|
||||
|
||||
if let Some(redis_client) = &state_for_spawn.cache {
|
||||
let mut conn = match redis_client.get_multiplexed_async_connection().await {
|
||||
Ok(conn) => conn,
|
||||
|
|
@ -41,56 +25,29 @@ pub fn hear_keyword(state: Arc<AppState>, user: UserSession, engine: &mut Engine
|
|||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let key = format!("hear:{}:{}", session_id_clone, var_name_clone);
|
||||
let _: Result<(), _> = redis::cmd("SET")
|
||||
.arg(&key)
|
||||
.arg("waiting")
|
||||
.query_async(&mut conn)
|
||||
.await;
|
||||
let _: Result<(), _> = redis::cmd("SET").arg(&key).arg("waiting").query_async(&mut conn).await;
|
||||
}
|
||||
});
|
||||
|
||||
Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
"Waiting for user input".into(),
|
||||
rhai::Position::NONE,
|
||||
)))
|
||||
Err(Box::new(EvalAltResult::ErrorRuntime("Waiting for user input".into(), rhai::Position::NONE)))
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub async fn execute_talk(state: Arc<AppState>, user_session: UserSession, message: String) -> Result<BotResponse, Box<dyn std::error::Error>> {
|
||||
info!("Executing TALK with message: {}", message);
|
||||
debug!("TALK: Sending message: {}", message);
|
||||
|
||||
let mut suggestions = Vec::new();
|
||||
|
||||
if let Some(redis_client) = &state.cache {
|
||||
let mut conn: redis::aio::MultiplexedConnection = redis_client.get_multiplexed_async_connection().await?;
|
||||
|
||||
let redis_key = format!("suggestions:{}:{}", user_session.user_id, user_session.id);
|
||||
debug!("Loading suggestions from Redis key: {}", redis_key);
|
||||
let suggestions_json: Result<Vec<String>, _> = redis::cmd("LRANGE")
|
||||
.arg(redis_key.as_str())
|
||||
.arg(0)
|
||||
.arg(-1)
|
||||
.query_async(&mut conn)
|
||||
.await;
|
||||
|
||||
let suggestions_json: Result<Vec<String>, _> = redis::cmd("LRANGE").arg(redis_key.as_str()).arg(0).arg(-1).query_async(&mut conn).await;
|
||||
match suggestions_json {
|
||||
Ok(suggestions_json) => {
|
||||
debug!("Found suggestions in Redis: {:?}", suggestions_json);
|
||||
suggestions = suggestions_json.into_iter()
|
||||
.filter_map(|s| serde_json::from_str(&s).ok())
|
||||
.collect();
|
||||
debug!("Parsed suggestions: {:?}", suggestions);
|
||||
suggestions = suggestions_json.into_iter().filter_map(|s| serde_json::from_str(&s).ok()).collect();
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to load suggestions from Redis: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let response = BotResponse {
|
||||
bot_id: user_session.bot_id.to_string(),
|
||||
user_id: user_session.user_id.to_string(),
|
||||
|
|
@ -105,26 +62,23 @@ pub async fn execute_talk(state: Arc<AppState>, user_session: UserSession, messa
|
|||
context_length: 0,
|
||||
context_max_length: 0,
|
||||
};
|
||||
|
||||
let user_id = user_session.id.to_string();
|
||||
let response_clone = response.clone();
|
||||
|
||||
match state.response_channels.try_lock() {
|
||||
Ok(response_channels) => {
|
||||
if let Some(tx) = response_channels.get(&user_id) {
|
||||
if let Err(e) = tx.try_send(response_clone) {
|
||||
error!("Failed to send TALK message via WebSocket: {}", e);
|
||||
} else {
|
||||
debug!("TALK message sent successfully via WebSocket");
|
||||
trace!("TALK message sent via WebSocket");
|
||||
}
|
||||
} else {
|
||||
debug!("No WebSocket connection found for session {}, sending via web adapter", user_id);
|
||||
let web_adapter = Arc::clone(&state.web_adapter);
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = web_adapter.send_message_to_session(&user_id, response_clone).await {
|
||||
error!("Failed to send TALK message via web adapter: {}", e);
|
||||
} else {
|
||||
debug!("TALK message sent successfully via web adapter");
|
||||
trace!("TALK message sent via web adapter");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -133,26 +87,21 @@ pub async fn execute_talk(state: Arc<AppState>, user_session: UserSession, messa
|
|||
error!("Failed to acquire lock on response_channels for TALK command");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub fn talk_keyword(state: Arc<AppState>, user: UserSession, engine: &mut Engine) {
|
||||
let state_clone = Arc::clone(&state);
|
||||
let user_clone = user.clone();
|
||||
|
||||
engine
|
||||
.register_custom_syntax(&["TALK", "$expr$"], true, move |context, inputs| {
|
||||
let message = context.eval_expression_tree(&inputs[0])?.to_string();
|
||||
let state_for_talk = Arc::clone(&state_clone);
|
||||
let user_for_talk = user_clone.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = execute_talk(state_for_talk, user_for_talk, message).await {
|
||||
error!("Error executing TALK command: {}", e);
|
||||
}
|
||||
});
|
||||
|
||||
Ok(Dynamic::UNIT)
|
||||
})
|
||||
.unwrap();
|
||||
|
|
|
|||
|
|
@ -1,230 +1,18 @@
|
|||
use rhai::Dynamic;
|
||||
use rhai::Engine;
|
||||
|
||||
pub fn last_keyword(engine: &mut Engine) {
|
||||
engine
|
||||
.register_custom_syntax(&["LAST", "(", "$expr$", ")"], false, {
|
||||
move |context, inputs| {
|
||||
let input_string = context.eval_expression_tree(&inputs[0])?;
|
||||
let input_str = input_string.to_string();
|
||||
|
||||
// Handle empty string case first
|
||||
if input_str.trim().is_empty() {
|
||||
return Ok(Dynamic::from(""));
|
||||
}
|
||||
|
||||
// Split on any whitespace and filter out empty strings
|
||||
let words: Vec<&str> = input_str
|
||||
.split_whitespace()
|
||||
.collect();
|
||||
|
||||
// Get the last non-empty word
|
||||
let words: Vec<&str> = input_str.split_whitespace().collect();
|
||||
let last_word = words.last().map(|s| *s).unwrap_or("");
|
||||
|
||||
Ok(Dynamic::from(last_word.to_string()))
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use rhai::{Engine, Scope};
|
||||
|
||||
#[test]
|
||||
fn test_last_keyword_empty_string() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
|
||||
let result: String = engine.eval("LAST(\"\")").unwrap();
|
||||
assert_eq!(result, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_last_keyword_multiple_spaces() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
|
||||
let result: String = engine.eval("LAST(\"hello world \")").unwrap();
|
||||
assert_eq!(result, "world");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_last_keyword_tabs_and_newlines() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
|
||||
let result: String = engine.eval(r#"LAST("hello\tworld\n")"#).unwrap();
|
||||
assert_eq!(result, "world");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_last_keyword_with_variable() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
let mut scope = Scope::new();
|
||||
|
||||
scope.push("text", "this is a test");
|
||||
let result: String = engine.eval_with_scope(&mut scope, "LAST(text)").unwrap();
|
||||
|
||||
assert_eq!(result, "test");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_last_keyword_whitespace_only() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
|
||||
let result: String = engine.eval("LAST(\" \")").unwrap();
|
||||
assert_eq!(result, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_last_keyword_mixed_whitespace() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
|
||||
let result: String = engine.eval(r#"LAST("hello\t \n world \t final")"#).unwrap();
|
||||
assert_eq!(result, "final");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_last_keyword_expression() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
|
||||
let result: String = engine.eval("LAST(\"hello\" + \" \" + \"world\")").unwrap();
|
||||
assert_eq!(result, "world");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_last_keyword_unicode() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
|
||||
let result: String = engine.eval("LAST(\"hello 世界 мир world\")").unwrap();
|
||||
assert_eq!(result, "world");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_last_keyword_in_expression() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
|
||||
let result: bool = engine.eval("LAST(\"hello world\") == \"world\"").unwrap();
|
||||
assert!(result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_last_keyword_complex_scenario() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
let mut scope = Scope::new();
|
||||
|
||||
scope.push("sentence", "The quick brown fox jumps over the lazy dog");
|
||||
let result: String = engine.eval_with_scope(&mut scope, "LAST(sentence)").unwrap();
|
||||
|
||||
assert_eq!(result, "dog");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_last_keyword_missing_parentheses() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
|
||||
let _: String = engine.eval("LAST \"hello world\"").unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_last_keyword_missing_closing_parenthesis() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
|
||||
let _: String = engine.eval("LAST(\"hello world\"").unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_last_keyword_missing_opening_parenthesis() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
|
||||
let _: String = engine.eval("LAST \"hello world\")").unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_last_keyword_dynamic_type() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
|
||||
let result = engine.eval::<Dynamic>("LAST(\"test string\")").unwrap();
|
||||
assert!(result.is::<String>());
|
||||
assert_eq!(result.to_string(), "string");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_last_keyword_nested_expression() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
|
||||
let result: String = engine.eval("LAST(\"The result is: \" + \"hello world\")").unwrap();
|
||||
assert_eq!(result, "world");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod integration_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_last_keyword_in_script() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
|
||||
let script = r#"
|
||||
let sentence1 = "first second third";
|
||||
let sentence2 = "alpha beta gamma";
|
||||
|
||||
let last1 = LAST(sentence1);
|
||||
let last2 = LAST(sentence2);
|
||||
|
||||
last1 + " and " + last2
|
||||
"#;
|
||||
|
||||
let result: String = engine.eval(script).unwrap();
|
||||
assert_eq!(result, "third and gamma");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_last_keyword_with_function() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
|
||||
engine.register_fn("get_name", || -> String { "john doe".to_string() });
|
||||
|
||||
let result: String = engine.eval("LAST(get_name())").unwrap();
|
||||
assert_eq!(result, "doe");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_last_keyword_multiple_calls() {
|
||||
let mut engine = Engine::new();
|
||||
last_keyword(&mut engine);
|
||||
|
||||
let script = r#"
|
||||
let text1 = "apple banana cherry";
|
||||
let text2 = "cat dog elephant";
|
||||
|
||||
let result1 = LAST(text1);
|
||||
let result2 = LAST(text2);
|
||||
|
||||
result1 + "-" + result2
|
||||
"#;
|
||||
|
||||
let result: String = engine.eval(script).unwrap();
|
||||
assert_eq!(result, "cherry-elephant");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,102 +1,57 @@
|
|||
use crate::basic::keywords::add_tool::get_session_tools;
|
||||
use crate::shared::models::UserSession;
|
||||
use crate::shared::state::AppState;
|
||||
use log::{error, info};
|
||||
use log::{error, trace};
|
||||
use rhai::{Dynamic, Engine};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn list_tools_keyword(state: Arc<AppState>, user: UserSession, engine: &mut Engine) {
|
||||
let state_clone = Arc::clone(&state);
|
||||
let user_clone = user.clone();
|
||||
|
||||
engine
|
||||
.register_custom_syntax(&["LIST_TOOLS"], false, move |_context, _inputs| {
|
||||
info!("LIST_TOOLS command executed for session: {}", user_clone.id);
|
||||
|
||||
let state_for_task = Arc::clone(&state_clone);
|
||||
let user_for_task = user_clone.clone();
|
||||
|
||||
// Spawn async task to list all tool associations from session
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||
.worker_threads(2)
|
||||
.enable_all()
|
||||
.build();
|
||||
|
||||
let rt = tokio::runtime::Builder::new_multi_thread().worker_threads(2).enable_all().build();
|
||||
let send_err = if let Ok(rt) = rt {
|
||||
let result = rt.block_on(async move {
|
||||
list_session_tools(&state_for_task, &user_for_task).await
|
||||
});
|
||||
tx.send(result).err()
|
||||
} else {
|
||||
tx.send(Err("Failed to build tokio runtime".to_string()))
|
||||
.err()
|
||||
tx.send(Err("Failed to build tokio runtime".to_string())).err()
|
||||
};
|
||||
|
||||
if send_err.is_some() {
|
||||
error!("Failed to send result from thread");
|
||||
}
|
||||
});
|
||||
|
||||
match rx.recv_timeout(std::time::Duration::from_secs(10)) {
|
||||
Ok(Ok(message)) => {
|
||||
info!("LIST_TOOLS completed: {}", message);
|
||||
Ok(Dynamic::from(message))
|
||||
}
|
||||
Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
e.into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(e.into(), rhai::Position::NONE))),
|
||||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
|
||||
Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
"LIST_TOOLS timed out".into(),
|
||||
rhai::Position::NONE,
|
||||
)))
|
||||
Err(Box::new(rhai::EvalAltResult::ErrorRuntime("LIST_TOOLS timed out".into(), rhai::Position::NONE)))
|
||||
}
|
||||
Err(e) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
format!("LIST_TOOLS failed: {}", e).into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
Err(e) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(format!("LIST_TOOLS failed: {}", e).into(), rhai::Position::NONE))),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// List all tools associated with the current session
|
||||
async fn list_session_tools(state: &AppState, user: &UserSession) -> Result<String, String> {
|
||||
let mut conn = state.conn.lock().map_err(|e| {
|
||||
let mut conn = state.conn.get().map_err(|e| {
|
||||
error!("Failed to acquire database lock: {}", e);
|
||||
format!("Database connection error: {}", e)
|
||||
})?;
|
||||
|
||||
// Get all tool associations for this session
|
||||
match get_session_tools(&mut *conn, &user.id) {
|
||||
Ok(tools) => {
|
||||
if tools.is_empty() {
|
||||
info!("No tools associated with session '{}'", user.id);
|
||||
Ok("No tools are currently active in this conversation".to_string())
|
||||
} else {
|
||||
info!(
|
||||
"Found {} tool(s) for session '{}' (user: {}, bot: {})",
|
||||
tools.len(),
|
||||
user.id,
|
||||
user.user_id,
|
||||
user.bot_id
|
||||
);
|
||||
|
||||
let tool_list = tools
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(idx, tool)| format!("{}. {}", idx + 1, tool))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
Ok(format!(
|
||||
"Active tools in this conversation ({}):\n{}",
|
||||
tools.len(),
|
||||
tool_list
|
||||
))
|
||||
trace!("Found {} tool(s) for session '{}' (user: {}, bot: {})", tools.len(), user.id, user.user_id, user.bot_id);
|
||||
let tool_list = tools.iter().enumerate().map(|(idx, tool)| format!("{}. {}", idx + 1, tool)).collect::<Vec<_>>().join("\n");
|
||||
Ok(format!("Active tools in this conversation ({}):\n{}", tools.len(), tool_list))
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
|
|
|
|||
|
|
@ -1,32 +1,20 @@
|
|||
use crate::shared::models::UserSession;
|
||||
use crate::shared::state::AppState;
|
||||
use log::{error, info};
|
||||
use log::{error};
|
||||
use rhai::{Dynamic, Engine};
|
||||
use uuid::Uuid;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
pub fn llm_keyword(state: Arc<AppState>, _user: UserSession, engine: &mut Engine) {
|
||||
let state_clone = Arc::clone(&state);
|
||||
|
||||
engine
|
||||
.register_custom_syntax(&["LLM", "$expr$"], false, move |context, inputs| {
|
||||
let text = context.eval_expression_tree(&inputs[0])?.to_string();
|
||||
|
||||
info!("LLM keyword processing text: {}", text);
|
||||
|
||||
let state_for_thread = Arc::clone(&state_clone);
|
||||
let prompt = build_llm_prompt(&text);
|
||||
|
||||
// ---- safe runtime isolation: no deadlocks possible ----
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
|
||||
std::thread::spawn(move || {
|
||||
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||
.worker_threads(2)
|
||||
.enable_all()
|
||||
.build();
|
||||
|
||||
let rt = tokio::runtime::Builder::new_multi_thread().worker_threads(2).enable_all().build();
|
||||
let send_err = if let Ok(rt) = rt {
|
||||
let result = rt.block_on(async move {
|
||||
execute_llm_generation(state_for_thread, prompt).await
|
||||
|
|
@ -35,57 +23,29 @@ pub fn llm_keyword(state: Arc<AppState>, _user: UserSession, engine: &mut Engine
|
|||
} else {
|
||||
tx.send(Err("failed to build tokio runtime".into())).err()
|
||||
};
|
||||
|
||||
if send_err.is_some() {
|
||||
error!("Failed to send LLM thread result");
|
||||
}
|
||||
});
|
||||
|
||||
match rx.recv_timeout(Duration::from_secs(500)) {
|
||||
Ok(Ok(result)) => Ok(Dynamic::from(result)),
|
||||
Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
e.to_string().into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(e.to_string().into(), rhai::Position::NONE))),
|
||||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
|
||||
Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
"LLM generation timed out".into(),
|
||||
rhai::Position::NONE,
|
||||
)))
|
||||
Err(Box::new(rhai::EvalAltResult::ErrorRuntime("LLM generation timed out".into(), rhai::Position::NONE)))
|
||||
}
|
||||
Err(e) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
format!("LLM thread failed: {e}").into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
Err(e) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(format!("LLM thread failed: {e}").into(), rhai::Position::NONE))),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Builds a consistent LLM prompt used by all Rhai scripts.
|
||||
/// You can change the style/structure here to guide the model's behavior.
|
||||
fn build_llm_prompt(user_text: &str) -> String {
|
||||
user_text.trim().to_string()
|
||||
}
|
||||
|
||||
/// Runs the async LLM provider call safely.
|
||||
pub async fn execute_llm_generation(
|
||||
state: Arc<AppState>,
|
||||
prompt: String,
|
||||
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let config_manager = crate::config::ConfigManager::new(Arc::clone(&state.conn));
|
||||
let model = config_manager
|
||||
.get_config(&Uuid::nil(), "llm-model", None)
|
||||
.unwrap_or_default();
|
||||
|
||||
info!("Using LLM model: {}", model);
|
||||
pub async fn execute_llm_generation(state: Arc<AppState>, prompt: String) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let config_manager = crate::config::ConfigManager::new(state.conn.clone());
|
||||
let model = config_manager.get_config(&Uuid::nil(), "llm-model", None).unwrap_or_default();
|
||||
let handler = crate::llm_models::get_handler(&model);
|
||||
let raw_response = state
|
||||
.llm_provider
|
||||
.generate(&prompt, &serde_json::Value::Null)
|
||||
.await?;
|
||||
|
||||
let raw_response = state.llm_provider.generate(&prompt, &serde_json::Value::Null).await?;
|
||||
let processed = handler.process_content(&raw_response);
|
||||
info!("Processed content: {}", processed);
|
||||
Ok(processed)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,36 +1,29 @@
|
|||
use diesel::prelude::*;
|
||||
use log::{error, info};
|
||||
use log::trace;
|
||||
use log::{error};
|
||||
use rhai::Dynamic;
|
||||
use rhai::Engine;
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use crate::shared::models::TriggerKind;
|
||||
use crate::shared::models::UserSession;
|
||||
use crate::shared::state::AppState;
|
||||
|
||||
pub fn on_keyword(state: &AppState, _user: UserSession, engine: &mut Engine) {
|
||||
let state_clone = state.clone();
|
||||
|
||||
engine
|
||||
.register_custom_syntax(
|
||||
&["ON", "$ident$", "OF", "$string$"],
|
||||
true,
|
||||
move |context, inputs| {
|
||||
.register_custom_syntax(&["ON", "$ident$", "OF", "$string$"], true, move |context, inputs| {
|
||||
let trigger_type = context.eval_expression_tree(&inputs[0])?.to_string();
|
||||
let table = context.eval_expression_tree(&inputs[1])?.to_string();
|
||||
let name = format!("{}_{}.rhai", table, trigger_type.to_lowercase());
|
||||
|
||||
let kind = match trigger_type.to_uppercase().as_str() {
|
||||
"UPDATE" => TriggerKind::TableUpdate,
|
||||
"INSERT" => TriggerKind::TableInsert,
|
||||
"DELETE" => TriggerKind::TableDelete,
|
||||
_ => return Err(format!("Invalid trigger type: {}", trigger_type).into()),
|
||||
};
|
||||
|
||||
let mut conn = state_clone.conn.lock().unwrap();
|
||||
trace!("Starting execute_on_trigger with kind: {:?}, table: {}, param: {}", kind, table, name);
|
||||
let mut conn = state_clone.conn.get().map_err(|e| format!("DB error: {}", e))?;
|
||||
let result = execute_on_trigger(&mut *conn, kind, &table, &name)
|
||||
.map_err(|e| format!("DB error: {}", e))?;
|
||||
|
||||
if let Some(rows_affected) = result.get("rows_affected") {
|
||||
Ok(Dynamic::from(rows_affected.as_i64().unwrap_or(0)))
|
||||
} else {
|
||||
|
|
@ -40,26 +33,13 @@ pub fn on_keyword(state: &AppState, _user: UserSession, engine: &mut Engine) {
|
|||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn execute_on_trigger(
|
||||
conn: &mut diesel::PgConnection,
|
||||
kind: TriggerKind,
|
||||
table: &str,
|
||||
param: &str,
|
||||
) -> Result<Value, String> {
|
||||
info!(
|
||||
"Starting execute_on_trigger with kind: {:?}, table: {}, param: {}",
|
||||
kind, table, param
|
||||
);
|
||||
|
||||
pub fn execute_on_trigger(conn: &mut diesel::PgConnection, kind: TriggerKind, table: &str, param: &str) -> Result<Value, String> {
|
||||
use crate::shared::models::system_automations;
|
||||
|
||||
let new_automation = (
|
||||
system_automations::kind.eq(kind as i32),
|
||||
system_automations::target.eq(table),
|
||||
system_automations::param.eq(param),
|
||||
);
|
||||
|
||||
let result = diesel::insert_into(system_automations::table)
|
||||
.values(&new_automation)
|
||||
.execute(conn)
|
||||
|
|
@ -67,7 +47,6 @@ pub fn execute_on_trigger(
|
|||
error!("SQL execution error: {}", e);
|
||||
e.to_string()
|
||||
})?;
|
||||
|
||||
Ok(json!({
|
||||
"command": "on_trigger",
|
||||
"trigger_type": format!("{:?}", kind),
|
||||
|
|
|
|||
|
|
@ -1,18 +1,13 @@
|
|||
use log::info;
|
||||
use log::trace;
|
||||
use rhai::Dynamic;
|
||||
use rhai::Engine;
|
||||
|
||||
use crate::shared::state::AppState;
|
||||
use crate::shared::models::UserSession;
|
||||
|
||||
pub fn print_keyword(_state: &AppState, _user: UserSession, engine: &mut Engine) {
|
||||
engine
|
||||
.register_custom_syntax(
|
||||
&["PRINT", "$expr$"],
|
||||
true,
|
||||
|context, inputs| {
|
||||
.register_custom_syntax(&["PRINT", "$expr$"], true, |context, inputs| {
|
||||
let value = context.eval_expression_tree(&inputs[0])?;
|
||||
info!("{}", value);
|
||||
trace!("PRINT: {}", value);
|
||||
Ok(Dynamic::UNIT)
|
||||
},
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,31 +1,27 @@
|
|||
use diesel::prelude::*;
|
||||
use log::{error, info};
|
||||
use log::trace;
|
||||
use log::{error};
|
||||
use rhai::Dynamic;
|
||||
use rhai::Engine;
|
||||
use serde_json::{json, Value};
|
||||
use std::error::Error;
|
||||
|
||||
use crate::shared::models::UserSession;
|
||||
use crate::shared::state::AppState;
|
||||
|
||||
pub fn set_keyword(state: &AppState, _user: UserSession, engine: &mut Engine) {
|
||||
let state_clone = state.clone();
|
||||
|
||||
engine
|
||||
.register_custom_syntax(&["SET", "$expr$", ",", "$expr$", ",", "$expr$"], false, {
|
||||
move |context, inputs| {
|
||||
let table_name = context.eval_expression_tree(&inputs[0])?;
|
||||
let filter = context.eval_expression_tree(&inputs[1])?;
|
||||
let updates = context.eval_expression_tree(&inputs[2])?;
|
||||
|
||||
let table_str = table_name.to_string();
|
||||
let filter_str = filter.to_string();
|
||||
let updates_str = updates.to_string();
|
||||
|
||||
let mut conn = state_clone.conn.lock().unwrap();
|
||||
trace!("Starting execute_set with table: {}, filter: {}, updates: {}", table_str, filter_str, updates_str);
|
||||
let mut conn = state_clone.conn.get().map_err(|e| format!("DB error: {}", e))?;
|
||||
let result = execute_set(&mut *conn, &table_str, &filter_str, &updates_str)
|
||||
.map_err(|e| format!("DB error: {}", e))?;
|
||||
|
||||
if let Some(rows_affected) = result.get("rows_affected") {
|
||||
Ok(Dynamic::from(rows_affected.as_i64().unwrap_or(0)))
|
||||
} else {
|
||||
|
|
@ -35,33 +31,14 @@ pub fn set_keyword(state: &AppState, _user: UserSession, engine: &mut Engine) {
|
|||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn execute_set(
|
||||
conn: &mut diesel::PgConnection,
|
||||
table_str: &str,
|
||||
filter_str: &str,
|
||||
updates_str: &str,
|
||||
) -> Result<Value, String> {
|
||||
info!(
|
||||
"Starting execute_set with table: {}, filter: {}, updates: {}",
|
||||
table_str, filter_str, updates_str
|
||||
);
|
||||
|
||||
pub fn execute_set(conn: &mut diesel::PgConnection, table_str: &str, filter_str: &str, updates_str: &str) -> Result<Value, String> {
|
||||
let (set_clause, _update_values) = parse_updates(updates_str).map_err(|e| e.to_string())?;
|
||||
|
||||
let where_clause = parse_filter_for_diesel(filter_str).map_err(|e| e.to_string())?;
|
||||
|
||||
let query = format!(
|
||||
"UPDATE {} SET {} WHERE {}",
|
||||
table_str, set_clause, where_clause
|
||||
);
|
||||
info!("Executing query: {}", query);
|
||||
|
||||
let query = format!("UPDATE {} SET {} WHERE {}", table_str, set_clause, where_clause);
|
||||
let result = diesel::sql_query(&query).execute(conn).map_err(|e| {
|
||||
error!("SQL execution error: {}", e);
|
||||
e.to_string()
|
||||
})?;
|
||||
|
||||
Ok(json!({
|
||||
"command": "set",
|
||||
"table": table_str,
|
||||
|
|
@ -70,49 +47,33 @@ pub fn execute_set(
|
|||
"rows_affected": result
|
||||
}))
|
||||
}
|
||||
|
||||
fn parse_updates(updates_str: &str) -> Result<(String, Vec<String>), Box<dyn Error>> {
|
||||
let mut set_clauses = Vec::new();
|
||||
let mut params = Vec::new();
|
||||
|
||||
for (i, update) in updates_str.split(',').enumerate() {
|
||||
let parts: Vec<&str> = update.split('=').collect();
|
||||
if parts.len() != 2 {
|
||||
return Err("Invalid update format".into());
|
||||
}
|
||||
|
||||
let column = parts[0].trim();
|
||||
let value = parts[1].trim();
|
||||
|
||||
if !column
|
||||
.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || c == '_')
|
||||
{
|
||||
if !column.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
|
||||
return Err("Invalid column name".into());
|
||||
}
|
||||
|
||||
set_clauses.push(format!("{} = ${}", column, i + 1));
|
||||
params.push(value.to_string());
|
||||
}
|
||||
|
||||
Ok((set_clauses.join(", "), params))
|
||||
}
|
||||
|
||||
fn parse_filter_for_diesel(filter_str: &str) -> Result<String, Box<dyn Error>> {
|
||||
let parts: Vec<&str> = filter_str.split('=').collect();
|
||||
if parts.len() != 2 {
|
||||
return Err("Invalid filter format. Expected 'KEY=VALUE'".into());
|
||||
}
|
||||
|
||||
let column = parts[0].trim();
|
||||
let value = parts[1].trim();
|
||||
|
||||
if !column
|
||||
.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || c == '_')
|
||||
{
|
||||
if !column.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
|
||||
return Err("Invalid column name in filter".into());
|
||||
}
|
||||
|
||||
Ok(format!("{} = '{}'", column, value))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,104 +1,49 @@
|
|||
use std::sync::Arc;
|
||||
use log::{error, info, trace};
|
||||
use log::trace;
|
||||
use log::{error};
|
||||
use crate::shared::state::AppState;
|
||||
use crate::shared::models::UserSession;
|
||||
use rhai::Engine;
|
||||
use rhai::Dynamic;
|
||||
|
||||
pub fn set_context_keyword(state: Arc<AppState>, user: UserSession, engine: &mut Engine) {
|
||||
// Clone the Redis client (if any) for use inside the async task.
|
||||
let cache = state.cache.clone();
|
||||
|
||||
engine
|
||||
.register_custom_syntax(
|
||||
&["SET_CONTEXT", "$expr$", "AS", "$expr$"],
|
||||
true,
|
||||
move |context, inputs| {
|
||||
// First expression is the context name, second is the value.
|
||||
.register_custom_syntax(&["SET_CONTEXT", "$expr$", "AS", "$expr$"], true, move |context, inputs| {
|
||||
let context_name = context.eval_expression_tree(&inputs[0])?.to_string();
|
||||
let context_value = context.eval_expression_tree(&inputs[1])?.to_string();
|
||||
|
||||
info!(
|
||||
"SET CONTEXT command executed - name: {}, value: {}",
|
||||
context_name,
|
||||
context_value
|
||||
);
|
||||
|
||||
// Build a Redis key that is unique per user and session.
|
||||
let redis_key = format!(
|
||||
"context:{}:{}:{}",
|
||||
user.user_id,
|
||||
user.id,
|
||||
context_name
|
||||
);
|
||||
|
||||
trace!(
|
||||
target: "app::set_context",
|
||||
"Constructed Redis key: {} for user {}, session {}, context {}",
|
||||
redis_key,
|
||||
user.user_id,
|
||||
user.id,
|
||||
context_name
|
||||
);
|
||||
|
||||
// If a Redis client is configured, perform the SET operation asynchronously.
|
||||
trace!("SET CONTEXT command executed - name: {}, value: {}", context_name, context_value);
|
||||
let redis_key = format!("context:{}:{}:{}", user.user_id, user.id, context_name);
|
||||
trace!("Constructed Redis key: {} for user {}, session {}, context {}", redis_key, user.user_id, user.id, context_name);
|
||||
if let Some(cache_client) = &cache {
|
||||
trace!("Redis client is available, preparing to set context value");
|
||||
|
||||
// Clone values needed inside the async block.
|
||||
let cache_client = cache_client.clone();
|
||||
let redis_key = redis_key.clone();
|
||||
let context_value = context_value.clone();
|
||||
|
||||
trace!(
|
||||
"Cloned cache_client, redis_key ({}) and context_value (len={}) for async task",
|
||||
redis_key,
|
||||
context_value.len()
|
||||
);
|
||||
|
||||
// Spawn a background task so we don't need an async closure here.
|
||||
trace!("Cloned cache_client, redis_key ({}) and context_value (len={}) for async task", redis_key, context_value.len());
|
||||
tokio::spawn(async move {
|
||||
trace!("Async task started for SET_CONTEXT operation");
|
||||
|
||||
// Acquire an async Redis connection.
|
||||
let mut conn = match cache_client.get_multiplexed_async_connection().await {
|
||||
Ok(conn) => {
|
||||
trace!("Successfully acquired async Redis connection");
|
||||
trace!("Cache connection established successfully");
|
||||
conn
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to connect to cache: {}", e);
|
||||
trace!("Aborting SET_CONTEXT task due to connection error");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Perform the SET command.
|
||||
trace!(
|
||||
"Executing Redis SET command with key: {} and value length: {}",
|
||||
redis_key,
|
||||
context_value.len()
|
||||
);
|
||||
let result: Result<(), redis::RedisError> = redis::cmd("SET")
|
||||
.arg(&redis_key)
|
||||
.arg(&context_value)
|
||||
.query_async(&mut conn)
|
||||
.await;
|
||||
|
||||
trace!("Executing Redis SET command with key: {} and value length: {}", redis_key, context_value.len());
|
||||
let result: Result<(), redis::RedisError> = redis::cmd("SET").arg(&redis_key).arg(&context_value).query_async(&mut conn).await;
|
||||
match result {
|
||||
Ok(_) => {
|
||||
trace!("Successfully set context in Redis for key {}", redis_key);
|
||||
trace!("Context value successfully stored in cache");
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to set cache value: {}", e);
|
||||
trace!("SET_CONTEXT Redis SET command failed");
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
trace!("No Redis client configured; SET_CONTEXT will not persist to cache");
|
||||
trace!("No cache configured, context not persisted");
|
||||
}
|
||||
|
||||
Ok(Dynamic::UNIT)
|
||||
},
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,206 +1,101 @@
|
|||
use crate::shared::models::UserSession;
|
||||
use crate::shared::state::AppState;
|
||||
use log::{error, info};
|
||||
use log::{error, trace};
|
||||
use rhai::{Dynamic, Engine};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn set_kb_keyword(state: Arc<AppState>, user: UserSession, engine: &mut Engine) {
|
||||
let state_clone = Arc::clone(&state);
|
||||
let user_clone = user.clone();
|
||||
|
||||
engine
|
||||
.register_custom_syntax(&["SET_KB", "$expr$"], false, move |context, inputs| {
|
||||
let kb_name = context.eval_expression_tree(&inputs[0])?;
|
||||
let kb_name_str = kb_name.to_string().trim_matches('"').to_string();
|
||||
|
||||
info!(
|
||||
"SET_KB command executed: {} for user: {}",
|
||||
kb_name_str, user_clone.user_id
|
||||
);
|
||||
|
||||
// Validate KB name (alphanumeric and underscores only)
|
||||
if !kb_name_str
|
||||
.chars()
|
||||
.all(|c| c.is_alphanumeric() || c == '_' || c == '-')
|
||||
{
|
||||
return Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
"KB name must contain only alphanumeric characters, underscores, and hyphens"
|
||||
.into(),
|
||||
rhai::Position::NONE,
|
||||
)));
|
||||
trace!("SET_KB command executed: {} for user: {}", kb_name_str, user_clone.user_id);
|
||||
if !kb_name_str.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '-') {
|
||||
return Err(Box::new(rhai::EvalAltResult::ErrorRuntime("KB name must contain only alphanumeric characters, underscores, and hyphens".into(), rhai::Position::NONE)));
|
||||
}
|
||||
|
||||
if kb_name_str.is_empty() {
|
||||
return Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
"KB name cannot be empty".into(),
|
||||
rhai::Position::NONE,
|
||||
)));
|
||||
return Err(Box::new(rhai::EvalAltResult::ErrorRuntime("KB name cannot be empty".into(), rhai::Position::NONE)));
|
||||
}
|
||||
|
||||
let state_for_task = Arc::clone(&state_clone);
|
||||
let user_for_task = user_clone.clone();
|
||||
let kb_name_for_task = kb_name_str.clone();
|
||||
|
||||
// Spawn async task to set up KB collection
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||
.worker_threads(2)
|
||||
.enable_all()
|
||||
.build();
|
||||
|
||||
let rt = tokio::runtime::Builder::new_multi_thread().worker_threads(2).enable_all().build();
|
||||
let send_err = if let Ok(rt) = rt {
|
||||
let result = rt.block_on(async move {
|
||||
add_kb_to_user(
|
||||
&state_for_task,
|
||||
&user_for_task,
|
||||
&kb_name_for_task,
|
||||
false,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
add_kb_to_user(&state_for_task, &user_for_task, &kb_name_for_task, false, None).await
|
||||
});
|
||||
tx.send(result).err()
|
||||
} else {
|
||||
tx.send(Err("failed to build tokio runtime".into())).err()
|
||||
};
|
||||
|
||||
if send_err.is_some() {
|
||||
error!("Failed to send result from thread");
|
||||
}
|
||||
});
|
||||
|
||||
match rx.recv_timeout(std::time::Duration::from_secs(30)) {
|
||||
Ok(Ok(message)) => {
|
||||
info!("SET_KB completed: {}", message);
|
||||
Ok(Dynamic::from(message))
|
||||
}
|
||||
Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
e.into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(e.into(), rhai::Position::NONE))),
|
||||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
|
||||
Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
"SET_KB timed out".into(),
|
||||
rhai::Position::NONE,
|
||||
)))
|
||||
Err(Box::new(rhai::EvalAltResult::ErrorRuntime("SET_KB timed out".into(), rhai::Position::NONE)))
|
||||
}
|
||||
Err(e) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
format!("SET_KB failed: {}", e).into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
Err(e) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(format!("SET_KB failed: {}", e).into(), rhai::Position::NONE))),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn add_kb_keyword(state: Arc<AppState>, user: UserSession, engine: &mut Engine) {
|
||||
let state_clone = Arc::clone(&state);
|
||||
let user_clone = user.clone();
|
||||
|
||||
engine
|
||||
.register_custom_syntax(&["ADD_KB", "$expr$"], false, move |context, inputs| {
|
||||
let kb_name = context.eval_expression_tree(&inputs[0])?;
|
||||
let kb_name_str = kb_name.to_string().trim_matches('"').to_string();
|
||||
|
||||
info!(
|
||||
"ADD_KB command executed: {} for user: {}",
|
||||
kb_name_str, user_clone.user_id
|
||||
);
|
||||
|
||||
// Validate KB name
|
||||
if !kb_name_str
|
||||
.chars()
|
||||
.all(|c| c.is_alphanumeric() || c == '_' || c == '-')
|
||||
{
|
||||
return Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
"KB name must contain only alphanumeric characters, underscores, and hyphens"
|
||||
.into(),
|
||||
rhai::Position::NONE,
|
||||
)));
|
||||
trace!("ADD_KB command executed: {} for user: {}", kb_name_str, user_clone.user_id);
|
||||
if !kb_name_str.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '-') {
|
||||
return Err(Box::new(rhai::EvalAltResult::ErrorRuntime("KB name must contain only alphanumeric characters, underscores, and hyphens".into(), rhai::Position::NONE)));
|
||||
}
|
||||
|
||||
let state_for_task = Arc::clone(&state_clone);
|
||||
let user_for_task = user_clone.clone();
|
||||
let kb_name_for_task = kb_name_str.clone();
|
||||
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||
.worker_threads(2)
|
||||
.enable_all()
|
||||
.build();
|
||||
|
||||
let rt = tokio::runtime::Builder::new_multi_thread().worker_threads(2).enable_all().build();
|
||||
let send_err = if let Ok(rt) = rt {
|
||||
let result = rt.block_on(async move {
|
||||
add_kb_to_user(
|
||||
&state_for_task,
|
||||
&user_for_task,
|
||||
&kb_name_for_task,
|
||||
false,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
add_kb_to_user(&state_for_task, &user_for_task, &kb_name_for_task, false, None).await
|
||||
});
|
||||
tx.send(result).err()
|
||||
} else {
|
||||
tx.send(Err("failed to build tokio runtime".into())).err()
|
||||
};
|
||||
|
||||
if send_err.is_some() {
|
||||
error!("Failed to send result from thread");
|
||||
}
|
||||
});
|
||||
|
||||
match rx.recv_timeout(std::time::Duration::from_secs(30)) {
|
||||
Ok(Ok(message)) => {
|
||||
info!("ADD_KB completed: {}", message);
|
||||
Ok(Dynamic::from(message))
|
||||
}
|
||||
Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
e.into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(e.into(), rhai::Position::NONE))),
|
||||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
|
||||
Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
"ADD_KB timed out".into(),
|
||||
rhai::Position::NONE,
|
||||
)))
|
||||
Err(Box::new(rhai::EvalAltResult::ErrorRuntime("ADD_KB timed out".into(), rhai::Position::NONE)))
|
||||
}
|
||||
Err(e) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
|
||||
format!("ADD_KB failed: {}", e).into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
Err(e) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(format!("ADD_KB failed: {}", e).into(), rhai::Position::NONE))),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Add a KB to user's active KBs (stored in user_kb_associations table)
|
||||
async fn add_kb_to_user(
|
||||
_state: &AppState,
|
||||
user: &UserSession,
|
||||
kb_name: &str,
|
||||
is_website: bool,
|
||||
website_url: Option<String>,
|
||||
) -> Result<String, String> {
|
||||
// TODO: Insert into user_kb_associations table using Diesel
|
||||
// For now, just log the action
|
||||
|
||||
info!(
|
||||
"KB '{}' associated with user '{}' (bot: {}, is_website: {})",
|
||||
kb_name, user.user_id, user.bot_id, is_website
|
||||
);
|
||||
|
||||
async fn add_kb_to_user(_state: &AppState, user: &UserSession, kb_name: &str, is_website: bool, website_url: Option<String>) -> Result<String, String> {
|
||||
trace!("KB '{}' associated with user '{}' (bot: {}, is_website: {})", kb_name, user.user_id, user.bot_id, is_website);
|
||||
if is_website {
|
||||
if let Some(url) = website_url {
|
||||
info!("Website URL: {}", url);
|
||||
return Ok(format!(
|
||||
"Website KB '{}' added successfully for user",
|
||||
kb_name
|
||||
));
|
||||
if let Some(_url) = website_url {
|
||||
return Ok(format!("Website KB '{}' added successfully for user", kb_name));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(format!("KB '{}' added successfully for user", kb_name))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,35 +1,16 @@
|
|||
use diesel::prelude::*;
|
||||
use log::info;
|
||||
use log::{trace};
|
||||
use serde_json::{json, Value};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::shared::models::TriggerKind;
|
||||
|
||||
|
||||
pub fn execute_set_schedule(
|
||||
conn: &mut diesel::PgConnection,
|
||||
cron: &str,
|
||||
script_name: &str,
|
||||
bot_uuid: Uuid,
|
||||
) -> Result<Value, Box<dyn std::error::Error>> {
|
||||
info!(
|
||||
"Scheduling SET SCHEDULE cron: {}, script: {}, bot_id: {:?}",
|
||||
cron, script_name, bot_uuid
|
||||
);
|
||||
|
||||
// First check if bot exists
|
||||
pub fn execute_set_schedule(conn: &mut diesel::PgConnection, cron: &str, script_name: &str, bot_uuid: Uuid) -> Result<Value, Box<dyn std::error::Error>> {
|
||||
trace!("Scheduling SET SCHEDULE cron: {}, script: {}, bot_id: {:?}", cron, script_name, bot_uuid);
|
||||
use crate::shared::models::bots::dsl::bots;
|
||||
let bot_exists: bool = diesel::select(diesel::dsl::exists(
|
||||
bots.filter(crate::shared::models::bots::dsl::id.eq(bot_uuid))
|
||||
))
|
||||
.get_result(conn)?;
|
||||
|
||||
let bot_exists: bool = diesel::select(diesel::dsl::exists(bots.filter(crate::shared::models::bots::dsl::id.eq(bot_uuid)))).get_result(conn)?;
|
||||
if !bot_exists {
|
||||
return Err(format!("Bot with id {} does not exist", bot_uuid).into());
|
||||
}
|
||||
|
||||
use crate::shared::models::system_automations::dsl::*;
|
||||
|
||||
let new_automation = (
|
||||
bot_id.eq(bot_uuid),
|
||||
kind.eq(TriggerKind::Scheduled as i32),
|
||||
|
|
@ -37,8 +18,6 @@ pub fn execute_set_schedule(
|
|||
param.eq(script_name),
|
||||
is_active.eq(true),
|
||||
);
|
||||
|
||||
// First try to update existing record
|
||||
let update_result = diesel::update(system_automations)
|
||||
.filter(bot_id.eq(bot_uuid))
|
||||
.filter(kind.eq(TriggerKind::Scheduled as i32))
|
||||
|
|
@ -49,16 +28,11 @@ pub fn execute_set_schedule(
|
|||
last_triggered.eq(None::<chrono::DateTime<chrono::Utc>>),
|
||||
))
|
||||
.execute(&mut *conn)?;
|
||||
|
||||
// If no rows were updated, insert new record
|
||||
let result = if update_result == 0 {
|
||||
diesel::insert_into(system_automations)
|
||||
.values(&new_automation)
|
||||
.execute(&mut *conn)?
|
||||
diesel::insert_into(system_automations).values(&new_automation).execute(&mut *conn)?
|
||||
} else {
|
||||
update_result
|
||||
};
|
||||
|
||||
Ok(json!({
|
||||
"command": "set_schedule",
|
||||
"schedule": cron,
|
||||
|
|
|
|||
|
|
@ -1,43 +1,30 @@
|
|||
use crate::shared::state::AppState;
|
||||
use crate::shared::models::UserSession;
|
||||
use log::{debug, error, info};
|
||||
use log::{ error,trace};
|
||||
use rhai::{Dynamic, Engine};
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub fn set_user_keyword(state: Arc<AppState>, user: UserSession, engine: &mut Engine) {
|
||||
let state_clone = Arc::clone(&state);
|
||||
let user_clone = user.clone();
|
||||
engine
|
||||
.register_custom_syntax(&["SET_USER", "$expr$"], true, move |context, inputs| {
|
||||
let user_id_str = context.eval_expression_tree(&inputs[0])?.to_string();
|
||||
|
||||
info!("SET USER command executed with ID: {}", user_id_str);
|
||||
|
||||
match Uuid::parse_str(&user_id_str) {
|
||||
Ok(user_id) => {
|
||||
debug!("Successfully parsed user UUID: {}", user_id);
|
||||
|
||||
let state_for_spawn = Arc::clone(&state_clone);
|
||||
let user_clone_spawn = user_clone.clone();
|
||||
|
||||
let mut session_manager =
|
||||
futures::executor::block_on(state_for_spawn.session_manager.lock());
|
||||
|
||||
let mut session_manager = futures::executor::block_on(state_for_spawn.session_manager.lock());
|
||||
if let Err(e) = session_manager.update_user_id(user_clone_spawn.id, user_id) {
|
||||
error!("Failed to update user ID in session: {}", e);
|
||||
} else {
|
||||
info!(
|
||||
"Updated session {} to user ID: {}",
|
||||
user_clone_spawn.id, user_id
|
||||
);
|
||||
trace!("Updated session {} to user ID: {}", user_clone_spawn.id, user_id);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
debug!("Invalid UUID format for SET USER: {}", e);
|
||||
trace!("Invalid user ID format: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Dynamic::UNIT)
|
||||
})
|
||||
.unwrap();
|
||||
|
|
|
|||
|
|
@ -1,18 +1,12 @@
|
|||
use crate::shared::state::AppState;
|
||||
use crate::shared::models::UserSession;
|
||||
use log::info;
|
||||
use rhai::{Dynamic, Engine};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
pub fn wait_keyword(_state: &AppState, _user: UserSession, engine: &mut Engine) {
|
||||
engine
|
||||
.register_custom_syntax(
|
||||
&["WAIT", "$expr$"],
|
||||
false,
|
||||
move |context, inputs| {
|
||||
.register_custom_syntax(&["WAIT", "$expr$"], false, move |context, inputs| {
|
||||
let seconds = context.eval_expression_tree(&inputs[0])?;
|
||||
|
||||
let duration_secs = if seconds.is::<i64>() {
|
||||
seconds.cast::<i64>() as f64
|
||||
} else if seconds.is::<f64>() {
|
||||
|
|
@ -20,23 +14,12 @@ pub fn wait_keyword(_state: &AppState, _user: UserSession, engine: &mut Engine)
|
|||
} else {
|
||||
return Err(format!("WAIT expects a number, got: {}", seconds).into());
|
||||
};
|
||||
|
||||
if duration_secs < 0.0 {
|
||||
return Err("WAIT duration cannot be negative".into());
|
||||
}
|
||||
|
||||
let capped_duration = if duration_secs > 300.0 {
|
||||
300.0
|
||||
} else {
|
||||
duration_secs
|
||||
};
|
||||
|
||||
info!("WAIT {} seconds (thread sleep)", capped_duration);
|
||||
|
||||
let capped_duration = if duration_secs > 300.0 { 300.0 } else { duration_secs };
|
||||
let duration = Duration::from_secs_f64(capped_duration);
|
||||
thread::sleep(duration);
|
||||
|
||||
info!("WAIT completed after {} seconds", capped_duration);
|
||||
Ok(Dynamic::from(format!("Waited {} seconds", capped_duration)))
|
||||
},
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,430 +1,360 @@
|
|||
use crate::config::{AppConfig, write_drive_config_to_env};
|
||||
use crate::config::AppConfig;
|
||||
use crate::package_manager::{InstallMode, PackageManager};
|
||||
use crate::shared::utils::establish_pg_connection;
|
||||
use anyhow::Result;
|
||||
use diesel::{connection::SimpleConnection};
|
||||
use dotenvy::dotenv;
|
||||
use log::{debug, error, info, trace};
|
||||
use aws_sdk_s3::Client;
|
||||
use aws_config::BehaviorVersion;
|
||||
use rand::distr::Alphanumeric;
|
||||
use rand::Rng;
|
||||
use aws_sdk_s3::Client;
|
||||
use diesel::connection::SimpleConnection;
|
||||
use log::{error, info, trace};
|
||||
use std::io::{self, Write};
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
pub struct ComponentInfo {
|
||||
pub name: &'static str,
|
||||
pub name: &'static str,
|
||||
}
|
||||
|
||||
pub struct BootstrapManager {
|
||||
pub install_mode: InstallMode,
|
||||
pub tenant: Option<String>,
|
||||
pub s3_client: Client,
|
||||
pub install_mode: InstallMode,
|
||||
pub tenant: Option<String>,
|
||||
}
|
||||
|
||||
impl BootstrapManager {
|
||||
fn is_postgres_running() -> bool {
|
||||
match Command::new("pg_isready").arg("-q").status() {
|
||||
Ok(status) => status.success(),
|
||||
Err(_) => {
|
||||
Command::new("pgrep").arg("postgres").output().map(|o| !o.stdout.is_empty()).unwrap_or(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn new(install_mode: InstallMode, tenant: Option<String>) -> Self {
|
||||
trace!("Initializing BootstrapManager with mode {:?} and tenant {:?}", install_mode, tenant);
|
||||
if !Self::is_postgres_running() {
|
||||
let pm = PackageManager::new(install_mode.clone(), tenant.clone())
|
||||
.expect("Failed to initialize PackageManager");
|
||||
if let Err(e) = pm.start("tables") {
|
||||
error!("Failed to start Tables server component automatically: {}", e);
|
||||
panic!("Database not available and auto-start failed.");
|
||||
} else {
|
||||
info!("Started Tables server component automatically");
|
||||
}
|
||||
}
|
||||
let config = AppConfig::from_env().expect("Failed to load config from env");
|
||||
let s3_client = Self::create_s3_operator(&config).await;
|
||||
Self {
|
||||
install_mode,
|
||||
tenant,
|
||||
s3_client,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_all(&mut self) -> Result<()> {
|
||||
let pm = PackageManager::new(self.install_mode.clone(), self.tenant.clone())?;
|
||||
let components = vec![
|
||||
ComponentInfo { name: "tables" },
|
||||
ComponentInfo { name: "cache" },
|
||||
ComponentInfo { name: "drive" },
|
||||
ComponentInfo { name: "llm" },
|
||||
ComponentInfo { name: "email" },
|
||||
ComponentInfo { name: "proxy" },
|
||||
ComponentInfo { name: "directory" },
|
||||
ComponentInfo { name: "alm" },
|
||||
ComponentInfo { name: "alm_ci" },
|
||||
ComponentInfo { name: "dns" },
|
||||
ComponentInfo { name: "webmail" },
|
||||
ComponentInfo { name: "meeting" },
|
||||
ComponentInfo { name: "table_editor" },
|
||||
ComponentInfo { name: "doc_editor" },
|
||||
ComponentInfo { name: "desktop" },
|
||||
ComponentInfo { name: "devtools" },
|
||||
ComponentInfo { name: "bot" },
|
||||
ComponentInfo { name: "system" },
|
||||
ComponentInfo { name: "vector_db" },
|
||||
ComponentInfo { name: "host" },
|
||||
];
|
||||
for component in components {
|
||||
if pm.is_installed(component.name) {
|
||||
pm.start(component.name)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn bootstrap(&mut self) -> Result<AppConfig> {
|
||||
if let Ok(tables_server) = std::env::var("TABLES_SERVER") {
|
||||
if !tables_server.is_empty() {
|
||||
info!("Legacy mode detected (TABLES_SERVER present), skipping bootstrap installation");
|
||||
let _database_url = std::env::var("DATABASE_URL").unwrap_or_else(|_| {
|
||||
let username = std::env::var("TABLES_USERNAME").unwrap_or_else(|_| "gbuser".to_string());
|
||||
let password = std::env::var("TABLES_PASSWORD").unwrap_or_else(|_| "postgres".to_string());
|
||||
let server = std::env::var("TABLES_SERVER").unwrap_or_else(|_| "localhost".to_string());
|
||||
let port = std::env::var("TABLES_PORT").unwrap_or_else(|_| "5432".to_string());
|
||||
let database = std::env::var("TABLES_DATABASE").unwrap_or_else(|_| "gbserver".to_string());
|
||||
format!("postgres://{}:{}@{}:{}/{}", username, password, server, port, database)
|
||||
});
|
||||
if let Ok(config) = self.load_config_from_csv().await {
|
||||
return Ok(config);
|
||||
}
|
||||
match establish_pg_connection() {
|
||||
Ok(mut conn) => {
|
||||
if let Err(e) = self.apply_migrations(&mut conn) {
|
||||
log::warn!("Failed to apply migrations: {}", e);
|
||||
}
|
||||
return Ok(AppConfig::from_database(&mut conn).expect("Failed to load config from DB"));
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("Failed to connect to database: {}", e);
|
||||
return Ok(AppConfig::from_env()?);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let pm = PackageManager::new(self.install_mode.clone(), self.tenant.clone())?;
|
||||
let required_components = vec!["tables", "drive", "cache", "llm"];
|
||||
let mut config = AppConfig::from_env().expect("Failed to load config from env");
|
||||
for component in required_components {
|
||||
if !pm.is_installed(component) {
|
||||
let termination_cmd = pm
|
||||
.components
|
||||
.get(component)
|
||||
.and_then(|cfg| cfg.binary_name.clone())
|
||||
.unwrap_or_else(|| component.to_string());
|
||||
if !termination_cmd.is_empty() {
|
||||
let check = Command::new("pgrep")
|
||||
.arg("-f")
|
||||
.arg(&termination_cmd)
|
||||
.output();
|
||||
if let Ok(output) = check {
|
||||
if !output.stdout.is_empty() {
|
||||
println!("Component '{}' appears to be already running from a previous install.", component);
|
||||
println!("Do you want to terminate it? (y/n)");
|
||||
let mut input = String::new();
|
||||
io::stdout().flush().unwrap();
|
||||
io::stdin().read_line(&mut input).unwrap();
|
||||
if input.trim().eq_ignore_ascii_case("y") {
|
||||
let _ = Command::new("pkill")
|
||||
.arg("-f")
|
||||
.arg(&termination_cmd)
|
||||
.status();
|
||||
println!("Terminated existing '{}' process.", component);
|
||||
} else {
|
||||
println!("Skipping start of '{}' as it is already running.", component);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if component == "tables" {
|
||||
let db_password = self.generate_secure_password(16);
|
||||
let farm_password = self.generate_secure_password(32);
|
||||
let env_contents = format!(
|
||||
"FARM_PASSWORD={}\nDATABASE_URL=postgres://gbuser:{}@localhost:5432/botserver",
|
||||
farm_password, db_password
|
||||
);
|
||||
std::fs::write(".env", &env_contents)
|
||||
.map_err(|e| anyhow::anyhow!("Failed to write .env file: {}", e))?;
|
||||
dotenv().ok();
|
||||
}
|
||||
pm.install(component).await?;
|
||||
if component == "tables" {
|
||||
let mut conn = establish_pg_connection()
|
||||
.map_err(|e| anyhow::anyhow!("Failed to connect to database: {}", e))?;
|
||||
let migration_dir = include_dir::include_dir!("./migrations");
|
||||
let mut migration_files: Vec<_> = migration_dir
|
||||
.files()
|
||||
.filter_map(|file| {
|
||||
let path = file.path();
|
||||
if path.extension()? == "sql" {
|
||||
Some(file)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
migration_files.sort_by_key(|f| f.path());
|
||||
for migration_file in migration_files {
|
||||
let migration = migration_file
|
||||
.contents_utf8()
|
||||
.ok_or_else(|| anyhow::anyhow!("Migration file is not valid UTF-8"))?;
|
||||
if let Err(e) = conn.batch_execute(migration) {
|
||||
log::error!("Failed to execute migration {}: {}", migration_file.path().display(), e);
|
||||
return Err(e.into());
|
||||
}
|
||||
trace!("Successfully executed migration: {}", migration_file.path().display());
|
||||
}
|
||||
config = AppConfig::from_database(&mut conn).expect("Failed to load config from DB");
|
||||
}
|
||||
}
|
||||
}
|
||||
self.s3_client = Self::create_s3_operator(&config).await;
|
||||
let final_config = if let Ok(csv_config) = self.load_config_from_csv().await {
|
||||
csv_config
|
||||
} else {
|
||||
config
|
||||
};
|
||||
if std::env::var("DRIVE_SERVER").is_err() {
|
||||
write_drive_config_to_env(&final_config.drive)
|
||||
.map_err(|e| anyhow::anyhow!("Failed to write drive config to .env: {}", e))?;
|
||||
}
|
||||
Ok(final_config)
|
||||
}
|
||||
|
||||
async fn create_s3_operator(config: &AppConfig) -> Client {
|
||||
let endpoint = if !config.drive.server.ends_with('/') {
|
||||
format!("{}/", config.drive.server)
|
||||
} else {
|
||||
config.drive.server.clone()
|
||||
};
|
||||
let base_config = aws_config::defaults(BehaviorVersion::latest())
|
||||
.endpoint_url(endpoint)
|
||||
.region("auto")
|
||||
.credentials_provider(
|
||||
aws_sdk_s3::config::Credentials::new(
|
||||
config.drive.access_key.clone(),
|
||||
config.drive.secret_key.clone(),
|
||||
None,
|
||||
None,
|
||||
"static",
|
||||
)
|
||||
)
|
||||
.load()
|
||||
.await;
|
||||
let s3_config = aws_sdk_s3::config::Builder::from(&base_config)
|
||||
.force_path_style(true)
|
||||
.build();
|
||||
aws_sdk_s3::Client::from_conf(s3_config)
|
||||
}
|
||||
|
||||
fn generate_secure_password(&self, length: usize) -> String {
|
||||
let mut rng = rand::rng();
|
||||
std::iter::repeat_with(|| rng.sample(Alphanumeric) as char)
|
||||
.take(length)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub async fn upload_templates_to_drive(&self, _config: &AppConfig) -> Result<()> {
|
||||
let mut conn = establish_pg_connection()?;
|
||||
self.create_bots_from_templates(&mut conn)?;
|
||||
let templates_dir = Path::new("templates");
|
||||
if !templates_dir.exists() {
|
||||
return Ok(());
|
||||
}
|
||||
let client = &self.s3_client;
|
||||
let mut read_dir = tokio::fs::read_dir(templates_dir).await?;
|
||||
while let Some(entry) = read_dir.next_entry().await? {
|
||||
let path = entry.path();
|
||||
if path.is_dir()
|
||||
&& path
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.ends_with(".gbai")
|
||||
{
|
||||
let bot_name = path.file_name().unwrap().to_string_lossy().to_string();
|
||||
let bucket = bot_name.trim_start_matches('/').to_string();
|
||||
if client.head_bucket().bucket(&bucket).send().await.is_err() {
|
||||
match client.create_bucket()
|
||||
.bucket(&bucket)
|
||||
.send()
|
||||
.await {
|
||||
Ok(_) => {
|
||||
trace!("Created bucket: {}", bucket);
|
||||
self.upload_directory_recursive(client, &path, &bucket, "/")
|
||||
.await?;
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to create bucket {}: {:?}", bucket, e);
|
||||
return Err(anyhow::anyhow!(
|
||||
"Failed to create bucket {}: {}. Check S3 credentials and endpoint configuration",
|
||||
bucket, e
|
||||
));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
debug!("Bucket {} already exists", bucket);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn create_bots_from_templates(&self, conn: &mut diesel::PgConnection) -> Result<()> {
|
||||
use crate::shared::models::schema::bots;
|
||||
use diesel::prelude::*;
|
||||
let templates_dir = Path::new("templates");
|
||||
if !templates_dir.exists() {
|
||||
return Ok(());
|
||||
}
|
||||
for entry in std::fs::read_dir(templates_dir)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
if path.is_dir() && path.extension().map(|e| e == "gbai").unwrap_or(false) {
|
||||
let bot_folder = path.file_name().unwrap().to_string_lossy().to_string();
|
||||
let bot_name = bot_folder.trim_end_matches(".gbai");
|
||||
let existing: Option<String> = bots::table
|
||||
.filter(bots::name.eq(&bot_name))
|
||||
.select(bots::name)
|
||||
.first(conn)
|
||||
.optional()?;
|
||||
if existing.is_none() {
|
||||
diesel::sql_query(
|
||||
"INSERT INTO bots (id, name, description, llm_provider, llm_config, context_provider, context_config, is_active) \
|
||||
VALUES (gen_random_uuid(), $1, $2, 'openai', '{\"model\": \"gpt-4\", \"temperature\": 0.7}', 'database', '{}', true)"
|
||||
)
|
||||
.bind::<diesel::sql_types::Text, _>(&bot_name)
|
||||
.bind::<diesel::sql_types::Text, _>(format!("Bot for {} template", bot_name))
|
||||
.execute(conn)?;
|
||||
info!("Created bot: {}", bot_name);
|
||||
} else {
|
||||
debug!("Bot {} already exists", bot_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn upload_directory_recursive<'a>(
|
||||
&'a self,
|
||||
client: &'a Client,
|
||||
local_path: &'a Path,
|
||||
bucket: &'a str,
|
||||
prefix: &'a str,
|
||||
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + 'a>> {
|
||||
Box::pin(async move {
|
||||
let _normalized_path = if !local_path.to_string_lossy().ends_with('/') {
|
||||
format!("{}/", local_path.to_string_lossy())
|
||||
} else {
|
||||
local_path.to_string_lossy().to_string()
|
||||
};
|
||||
let mut read_dir = tokio::fs::read_dir(local_path).await?;
|
||||
while let Some(entry) = read_dir.next_entry().await? {
|
||||
let path = entry.path();
|
||||
let file_name = path.file_name().unwrap().to_string_lossy().to_string();
|
||||
let mut key = prefix.trim_matches('/').to_string();
|
||||
if !key.is_empty() {
|
||||
key.push('/');
|
||||
}
|
||||
key.push_str(&file_name);
|
||||
if path.is_file() {
|
||||
trace!("Uploading file {} to bucket {} with key {}", path.display(), bucket, key);
|
||||
let content = tokio::fs::read(&path).await?;
|
||||
client.put_object()
|
||||
.bucket(bucket)
|
||||
.key(&key)
|
||||
.body(content.into())
|
||||
.send()
|
||||
.await?;
|
||||
} else if path.is_dir() {
|
||||
self.upload_directory_recursive(client, &path, bucket, &key).await?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
async fn load_config_from_csv(&self) -> Result<AppConfig> {
|
||||
use crate::config::ConfigManager;
|
||||
use uuid::Uuid;
|
||||
let client = &self.s3_client;
|
||||
let bucket = "default.gbai";
|
||||
let config_key = "default.gbot/config.csv";
|
||||
match client.get_object()
|
||||
.bucket(bucket)
|
||||
.key(config_key)
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
trace!("Found config.csv in default.gbai");
|
||||
let bytes = response.body.collect().await?.into_bytes();
|
||||
let csv_content = String::from_utf8(bytes.to_vec())?;
|
||||
let config_conn = establish_pg_connection()?;
|
||||
let config_manager = ConfigManager::new(Arc::new(Mutex::new(config_conn)));
|
||||
let default_bot_id = Uuid::parse_str("00000000-0000-0000-0000-000000000000")?;
|
||||
let temp_path = std::env::temp_dir().join("config.csv");
|
||||
tokio::fs::write(&temp_path, csv_content).await?;
|
||||
config_manager.sync_gbot_config(&default_bot_id, temp_path.to_str().unwrap())
|
||||
.map_err(|e| anyhow::anyhow!("Failed to sync gbot config: {}", e))?;
|
||||
let mut final_conn = establish_pg_connection()?;
|
||||
let config = AppConfig::from_database(&mut final_conn)?;
|
||||
Ok(config)
|
||||
}
|
||||
Err(e) => {
|
||||
debug!("No config.csv found in default.gbai: {:?}", e);
|
||||
Err(e.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_migrations(&self, conn: &mut diesel::PgConnection) -> Result<()> {
|
||||
let migrations_dir = std::path::Path::new("migrations");
|
||||
if !migrations_dir.exists() {
|
||||
return Ok(());
|
||||
}
|
||||
let mut sql_files: Vec<_> = std::fs::read_dir(migrations_dir)?
|
||||
.filter_map(|entry| entry.ok())
|
||||
.filter(|entry| {
|
||||
entry
|
||||
.path()
|
||||
.extension()
|
||||
.and_then(|s| s.to_str())
|
||||
.map(|s| s == "sql")
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.collect();
|
||||
sql_files.sort_by_key(|entry| entry.path());
|
||||
for entry in sql_files {
|
||||
let path = entry.path();
|
||||
let filename = path.file_name().unwrap().to_string_lossy();
|
||||
match std::fs::read_to_string(&path) {
|
||||
Ok(sql) => match conn.batch_execute(&sql) {
|
||||
Err(e) => {
|
||||
log::warn!("Migration {} failed: {}", filename, e);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Err(e) => {
|
||||
log::warn!("Failed to read migration {}: {}", filename, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn is_postgres_running() -> bool {
|
||||
match Command::new("pg_isready").arg("-q").status() {
|
||||
Ok(status) => status.success(),
|
||||
Err(_) => Command::new("pgrep")
|
||||
.arg("postgres")
|
||||
.output()
|
||||
.map(|o| !o.stdout.is_empty())
|
||||
.unwrap_or(false),
|
||||
}
|
||||
}
|
||||
pub async fn new(install_mode: InstallMode, tenant: Option<String>) -> Self {
|
||||
trace!(
|
||||
"Initializing BootstrapManager with mode {:?} and tenant {:?}",
|
||||
install_mode,
|
||||
tenant
|
||||
);
|
||||
if !Self::is_postgres_running() {
|
||||
let pm = PackageManager::new(install_mode.clone(), tenant.clone())
|
||||
.expect("Failed to initialize PackageManager");
|
||||
if let Err(e) = pm.start("tables") {
|
||||
error!(
|
||||
"Failed to start Tables server component automatically: {}",
|
||||
e
|
||||
);
|
||||
panic!("Database not available and auto-start failed.");
|
||||
} else {
|
||||
trace!("Tables server started successfully");
|
||||
}
|
||||
}
|
||||
Self {
|
||||
install_mode,
|
||||
tenant,
|
||||
}
|
||||
}
|
||||
pub fn start_all(&mut self) -> Result<()> {
|
||||
let pm = PackageManager::new(self.install_mode.clone(), self.tenant.clone())?;
|
||||
let components = vec![
|
||||
ComponentInfo { name: "tables" },
|
||||
ComponentInfo { name: "cache" },
|
||||
ComponentInfo { name: "drive" },
|
||||
ComponentInfo { name: "llm" },
|
||||
ComponentInfo { name: "email" },
|
||||
ComponentInfo { name: "proxy" },
|
||||
ComponentInfo { name: "directory" },
|
||||
ComponentInfo { name: "alm" },
|
||||
ComponentInfo { name: "alm_ci" },
|
||||
ComponentInfo { name: "dns" },
|
||||
ComponentInfo { name: "webmail" },
|
||||
ComponentInfo { name: "meeting" },
|
||||
ComponentInfo {
|
||||
name: "table_editor",
|
||||
},
|
||||
ComponentInfo { name: "doc_editor" },
|
||||
ComponentInfo { name: "desktop" },
|
||||
ComponentInfo { name: "devtools" },
|
||||
ComponentInfo { name: "bot" },
|
||||
ComponentInfo { name: "system" },
|
||||
ComponentInfo { name: "vector_db" },
|
||||
ComponentInfo { name: "host" },
|
||||
];
|
||||
for component in components {
|
||||
if pm.is_installed(component.name) {
|
||||
pm.start(component.name)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub async fn bootstrap(&mut self) {
|
||||
if let Ok(tables_server) = std::env::var("TABLES_SERVER") {
|
||||
if !tables_server.is_empty() {
|
||||
info!(
|
||||
"Legacy mode detected (TABLES_SERVER present), skipping bootstrap installation"
|
||||
);
|
||||
let _database_url = std::env::var("DATABASE_URL").unwrap_or_else(|_| {
|
||||
let username =
|
||||
std::env::var("TABLES_USERNAME").unwrap_or_else(|_| "gbuser".to_string());
|
||||
let password =
|
||||
std::env::var("TABLES_PASSWORD").unwrap_or_else(|_| "postgres".to_string());
|
||||
let server =
|
||||
std::env::var("TABLES_SERVER").unwrap_or_else(|_| "localhost".to_string());
|
||||
let port = std::env::var("TABLES_PORT").unwrap_or_else(|_| "5432".to_string());
|
||||
let database =
|
||||
std::env::var("TABLES_DATABASE").unwrap_or_else(|_| "gbserver".to_string());
|
||||
format!(
|
||||
"postgres://{}:{}@{}:{}/{}",
|
||||
username, password, server, port, database
|
||||
)
|
||||
});
|
||||
match establish_pg_connection() {
|
||||
Ok(mut conn) => {
|
||||
if let Err(e) = self.apply_migrations(&mut conn) {
|
||||
log::warn!("Failed to apply migrations: {}", e);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("Failed to connect to database: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let pm = PackageManager::new(self.install_mode.clone(), self.tenant.clone()).unwrap();
|
||||
let required_components = vec!["tables", "drive", "cache", "llm"];
|
||||
for component in required_components {
|
||||
if !pm.is_installed(component) {
|
||||
let termination_cmd = pm
|
||||
.components
|
||||
.get(component)
|
||||
.and_then(|cfg| cfg.binary_name.clone())
|
||||
.unwrap_or_else(|| component.to_string());
|
||||
if !termination_cmd.is_empty() {
|
||||
let check = Command::new("pgrep")
|
||||
.arg("-f")
|
||||
.arg(&termination_cmd)
|
||||
.output();
|
||||
if let Ok(output) = check {
|
||||
if !output.stdout.is_empty() {
|
||||
println!("Component '{}' appears to be already running from a previous install.", component);
|
||||
println!("Do you want to terminate it? (y/n)");
|
||||
let mut input = String::new();
|
||||
io::stdout().flush().unwrap();
|
||||
io::stdin().read_line(&mut input).unwrap();
|
||||
if input.trim().eq_ignore_ascii_case("y") {
|
||||
let _ = Command::new("pkill")
|
||||
.arg("-f")
|
||||
.arg(&termination_cmd)
|
||||
.status();
|
||||
println!("Terminated existing '{}' process.", component);
|
||||
} else {
|
||||
println!(
|
||||
"Skipping start of '{}' as it is already running.",
|
||||
component
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = pm.install(component).await;
|
||||
if component == "tables" {
|
||||
let mut conn = establish_pg_connection().unwrap();
|
||||
let migration_dir = include_dir::include_dir!("./migrations");
|
||||
let mut migration_files: Vec<_> = migration_dir
|
||||
.files()
|
||||
.filter_map(|file| {
|
||||
let path = file.path();
|
||||
if path.extension()? == "sql" {
|
||||
Some(file)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
migration_files.sort_by_key(|f| f.path());
|
||||
for migration_file in migration_files {
|
||||
let migration = migration_file
|
||||
.contents_utf8()
|
||||
.ok_or_else(|| anyhow::anyhow!("Migration file is not valid UTF-8"));
|
||||
if let Err(e) = conn.batch_execute(migration.unwrap()) {
|
||||
log::error!(
|
||||
"Failed to execute migration {}: {}",
|
||||
migration_file.path().display(),
|
||||
e
|
||||
);
|
||||
}
|
||||
trace!(
|
||||
"Successfully executed migration: {}",
|
||||
migration_file.path().display()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
async fn create_s3_operator(config: &AppConfig) -> Client {
|
||||
let endpoint = if !config.drive.server.ends_with('/') {
|
||||
format!("{}/", config.drive.server)
|
||||
} else {
|
||||
config.drive.server.clone()
|
||||
};
|
||||
let base_config = aws_config::defaults(BehaviorVersion::latest())
|
||||
.endpoint_url(endpoint)
|
||||
.region("auto")
|
||||
.credentials_provider(aws_sdk_s3::config::Credentials::new(
|
||||
config.drive.access_key.clone(),
|
||||
config.drive.secret_key.clone(),
|
||||
None,
|
||||
None,
|
||||
"static",
|
||||
))
|
||||
.load()
|
||||
.await;
|
||||
let s3_config = aws_sdk_s3::config::Builder::from(&base_config)
|
||||
.force_path_style(true)
|
||||
.build();
|
||||
aws_sdk_s3::Client::from_conf(s3_config)
|
||||
}
|
||||
pub async fn upload_templates_to_drive(&self, _config: &AppConfig) -> Result<()> {
|
||||
let mut conn = establish_pg_connection()?;
|
||||
self.create_bots_from_templates(&mut conn)?;
|
||||
let templates_dir = Path::new("templates");
|
||||
if !templates_dir.exists() {
|
||||
return Ok(());
|
||||
}
|
||||
let client = Self::create_s3_operator(_config).await;
|
||||
let mut read_dir = tokio::fs::read_dir(templates_dir).await?;
|
||||
while let Some(entry) = read_dir.next_entry().await? {
|
||||
let path = entry.path();
|
||||
if path.is_dir()
|
||||
&& path
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.ends_with(".gbai")
|
||||
{
|
||||
let bot_name = path.file_name().unwrap().to_string_lossy().to_string();
|
||||
let bucket = bot_name.trim_start_matches('/').to_string();
|
||||
if client.head_bucket().bucket(&bucket).send().await.is_err() {
|
||||
match client.create_bucket().bucket(&bucket).send().await {
|
||||
Ok(_) => {
|
||||
self.upload_directory_recursive(&client, &path, &bucket, "/")
|
||||
.await?;
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to create bucket {}: {:?}", bucket, e);
|
||||
return Err(anyhow::anyhow!("Failed to create bucket {}: {}. Check S3 credentials and endpoint configuration", bucket, e));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
trace!("Bucket {} already exists", bucket);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn create_bots_from_templates(&self, conn: &mut diesel::PgConnection) -> Result<()> {
|
||||
use crate::shared::models::schema::bots;
|
||||
use diesel::prelude::*;
|
||||
let templates_dir = Path::new("templates");
|
||||
if !templates_dir.exists() {
|
||||
return Ok(());
|
||||
}
|
||||
for entry in std::fs::read_dir(templates_dir)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
if path.is_dir() && path.extension().map(|e| e == "gbai").unwrap_or(false) {
|
||||
let bot_folder = path.file_name().unwrap().to_string_lossy().to_string();
|
||||
let bot_name = bot_folder.trim_end_matches(".gbai");
|
||||
let existing: Option<String> = bots::table
|
||||
.filter(bots::name.eq(&bot_name))
|
||||
.select(bots::name)
|
||||
.first(conn)
|
||||
.optional()?;
|
||||
if existing.is_none() {
|
||||
diesel::sql_query("INSERT INTO bots (id, name, description, llm_provider, llm_config, context_provider, context_config, is_active) VALUES (gen_random_uuid(), $1, $2, 'openai', '{\"model\": \"gpt-4\", \"temperature\": 0.7}', 'database', '{}', true)").bind::<diesel::sql_types::Text, _>(&bot_name).bind::<diesel::sql_types::Text, _>(format!("Bot for {} template", bot_name)).execute(conn)?;
|
||||
} else {
|
||||
trace!("Bot {} already exists", bot_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn upload_directory_recursive<'a>(
|
||||
&'a self,
|
||||
client: &'a Client,
|
||||
local_path: &'a Path,
|
||||
bucket: &'a str,
|
||||
prefix: &'a str,
|
||||
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + 'a>> {
|
||||
Box::pin(async move {
|
||||
let _normalized_path = if !local_path.to_string_lossy().ends_with('/') {
|
||||
format!("{}/", local_path.to_string_lossy())
|
||||
} else {
|
||||
local_path.to_string_lossy().to_string()
|
||||
};
|
||||
let mut read_dir = tokio::fs::read_dir(local_path).await?;
|
||||
while let Some(entry) = read_dir.next_entry().await? {
|
||||
let path = entry.path();
|
||||
let file_name = path.file_name().unwrap().to_string_lossy().to_string();
|
||||
let mut key = prefix.trim_matches('/').to_string();
|
||||
if !key.is_empty() {
|
||||
key.push('/');
|
||||
}
|
||||
key.push_str(&file_name);
|
||||
if path.is_file() {
|
||||
trace!(
|
||||
"Uploading file {} to bucket {} with key {}",
|
||||
path.display(),
|
||||
bucket,
|
||||
key
|
||||
);
|
||||
let content = tokio::fs::read(&path).await?;
|
||||
client
|
||||
.put_object()
|
||||
.bucket(bucket)
|
||||
.key(&key)
|
||||
.body(content.into())
|
||||
.send()
|
||||
.await?;
|
||||
} else if path.is_dir() {
|
||||
self.upload_directory_recursive(client, &path, bucket, &key)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
fn apply_migrations(&self, conn: &mut diesel::PgConnection) -> Result<()> {
|
||||
let migrations_dir = std::path::Path::new("migrations");
|
||||
if !migrations_dir.exists() {
|
||||
return Ok(());
|
||||
}
|
||||
let mut sql_files: Vec<_> = std::fs::read_dir(migrations_dir)?
|
||||
.filter_map(|entry| entry.ok())
|
||||
.filter(|entry| {
|
||||
entry
|
||||
.path()
|
||||
.extension()
|
||||
.and_then(|s| s.to_str())
|
||||
.map(|s| s == "sql")
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.collect();
|
||||
sql_files.sort_by_key(|entry| entry.path());
|
||||
for entry in sql_files {
|
||||
let path = entry.path();
|
||||
let filename = path.file_name().unwrap().to_string_lossy();
|
||||
match std::fs::read_to_string(&path) {
|
||||
Ok(sql) => match conn.batch_execute(&sql) {
|
||||
Err(e) => {
|
||||
log::warn!("Migration {} failed: {}", filename, e);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Err(e) => {
|
||||
log::warn!("Failed to read migration {}: {}", filename, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ impl BotOrchestrator {
|
|||
use crate::shared::models::schema::bots::dsl::*;
|
||||
use diesel::prelude::*;
|
||||
|
||||
let mut db_conn = self.state.conn.lock().unwrap();
|
||||
let mut db_conn = self.state.conn.get().unwrap();
|
||||
let active_bots = bots
|
||||
.filter(is_active.eq(true))
|
||||
.select(id)
|
||||
|
|
@ -102,7 +102,7 @@ impl BotOrchestrator {
|
|||
use diesel::prelude::*;
|
||||
|
||||
let bot_name: String = {
|
||||
let mut db_conn = state.conn.lock().unwrap();
|
||||
let mut db_conn = state.conn.get().unwrap();
|
||||
bots.filter(id.eq(Uuid::parse_str(&bot_guid)?))
|
||||
.select(name)
|
||||
.first(&mut *db_conn)
|
||||
|
|
@ -154,7 +154,7 @@ impl BotOrchestrator {
|
|||
use diesel::prelude::*;
|
||||
|
||||
let bot_name: String = {
|
||||
let mut db_conn = self.state.conn.lock().unwrap();
|
||||
let mut db_conn = self.state.conn.get().unwrap();
|
||||
bots.filter(id.eq(Uuid::parse_str(&bot_guid)?))
|
||||
.select(name)
|
||||
.first(&mut *db_conn)
|
||||
|
|
@ -251,7 +251,7 @@ impl BotOrchestrator {
|
|||
..event_response
|
||||
};
|
||||
|
||||
if let Some(adapter) = self.state.channels.lock().unwrap().get(channel) {
|
||||
if let Some(adapter) = self.state.channels.lock().await.get(channel) {
|
||||
adapter.send_message(event_response).await?;
|
||||
} else {
|
||||
warn!("No channel adapter found for channel: {}", channel);
|
||||
|
|
@ -310,7 +310,7 @@ impl BotOrchestrator {
|
|||
context_max_length: 0,
|
||||
};
|
||||
|
||||
if let Some(adapter) = self.state.channels.lock().unwrap().get(channel) {
|
||||
if let Some(adapter) = self.state.channels.lock().await.get(channel) {
|
||||
adapter.send_message(confirmation).await?;
|
||||
}
|
||||
|
||||
|
|
@ -396,7 +396,7 @@ impl BotOrchestrator {
|
|||
|
||||
// Get history limit from bot config (default -1 for unlimited)
|
||||
let history_limit = {
|
||||
let config_manager = ConfigManager::new(Arc::clone(&self.state.conn));
|
||||
let config_manager = ConfigManager::new(self.state.conn.clone());
|
||||
config_manager
|
||||
.get_config(
|
||||
&Uuid::parse_str(&message.bot_id).unwrap_or_default(),
|
||||
|
|
@ -496,7 +496,7 @@ impl BotOrchestrator {
|
|||
|
||||
// Calculate initial token count
|
||||
let initial_tokens = crate::shared::utils::estimate_token_count(&prompt);
|
||||
let config_manager = ConfigManager::new(Arc::clone(&self.state.conn));
|
||||
let config_manager = ConfigManager::new(self.state.conn.clone());
|
||||
let max_context_size = config_manager
|
||||
.get_config(
|
||||
&Uuid::parse_str(&message.bot_id).unwrap_or_default(),
|
||||
|
|
@ -593,7 +593,7 @@ impl BotOrchestrator {
|
|||
}
|
||||
}
|
||||
|
||||
trace!(
|
||||
info!(
|
||||
"Stream processing completed, {} chunks processed",
|
||||
chunk_count
|
||||
);
|
||||
|
|
@ -608,7 +608,7 @@ impl BotOrchestrator {
|
|||
);
|
||||
|
||||
// Trigger compact prompt if enabled
|
||||
let config_manager = ConfigManager::new(Arc::clone(&self.state.conn));
|
||||
let config_manager = ConfigManager::new( self.state.conn.clone());
|
||||
let compact_enabled = config_manager
|
||||
.get_config(
|
||||
&Uuid::parse_str(&message.bot_id).unwrap_or_default(),
|
||||
|
|
@ -636,7 +636,7 @@ impl BotOrchestrator {
|
|||
sm.save_message(session.id, user_id, 2, &full_response, 1)?;
|
||||
}
|
||||
|
||||
let config_manager = ConfigManager::new(Arc::clone(&self.state.conn));
|
||||
let config_manager = ConfigManager::new(self.state.conn.clone());
|
||||
let max_context_size = config_manager
|
||||
.get_config(
|
||||
&Uuid::parse_str(&message.bot_id).unwrap_or_default(),
|
||||
|
|
@ -710,7 +710,7 @@ impl BotOrchestrator {
|
|||
let bot_id = session.bot_id;
|
||||
|
||||
let bot_name: String = {
|
||||
let mut db_conn = state.conn.lock().unwrap();
|
||||
let mut db_conn = state.conn.get().unwrap();
|
||||
bots.filter(id.eq(Uuid::parse_str(&bot_id.to_string())?))
|
||||
.select(name)
|
||||
.first(&mut *db_conn)
|
||||
|
|
@ -896,7 +896,7 @@ async fn websocket_handler(
|
|||
use crate::shared::models::schema::bots::dsl::*;
|
||||
use diesel::prelude::*;
|
||||
|
||||
let mut db_conn = data.conn.lock().unwrap();
|
||||
let mut db_conn = data.conn.get().unwrap();
|
||||
match bots
|
||||
.filter(is_active.eq(true))
|
||||
.select(id)
|
||||
|
|
@ -1010,7 +1010,7 @@ async fn websocket_handler(
|
|||
use crate::shared::models::schema::bots::dsl::*;
|
||||
use diesel::prelude::*;
|
||||
|
||||
let mut db_conn = data.conn.lock().unwrap();
|
||||
let mut db_conn = data.conn.get().unwrap();
|
||||
match bots
|
||||
.filter(is_active.eq(true))
|
||||
.select(id)
|
||||
|
|
@ -1069,7 +1069,7 @@ async fn websocket_handler(
|
|||
use crate::shared::models::schema::bots::dsl::*;
|
||||
use diesel::prelude::*;
|
||||
|
||||
let mut db_conn = data.conn.lock().unwrap();
|
||||
let mut db_conn = data.conn.get().unwrap();
|
||||
match bots
|
||||
.filter(is_active.eq(true))
|
||||
.select(id)
|
||||
|
|
|
|||
|
|
@ -1,13 +1,10 @@
|
|||
use crate::shared::utils::{ DbPool};
|
||||
use diesel::prelude::*;
|
||||
use diesel::pg::PgConnection;
|
||||
use uuid::Uuid;
|
||||
use log::{trace};
|
||||
use diesel::r2d2::{ConnectionManager, PooledConnection};
|
||||
use std::collections::HashMap;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Write;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use crate::shared::utils::establish_pg_connection;
|
||||
|
||||
use uuid::Uuid;
|
||||
#[derive(Clone)]
|
||||
pub struct AppConfig {
|
||||
pub drive: DriveConfig,
|
||||
|
|
@ -15,7 +12,6 @@ pub struct AppConfig {
|
|||
pub database: DatabaseConfig,
|
||||
pub site_path: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DatabaseConfig {
|
||||
pub username: String,
|
||||
|
|
@ -24,7 +20,6 @@ pub struct DatabaseConfig {
|
|||
pub port: u32,
|
||||
pub database: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DriveConfig {
|
||||
pub server: String,
|
||||
|
|
@ -32,13 +27,11 @@ pub struct DriveConfig {
|
|||
pub secret_key: String,
|
||||
pub use_ssl: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ServerConfig {
|
||||
pub host: String,
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
impl AppConfig {
|
||||
pub fn database_url(&self) -> String {
|
||||
format!(
|
||||
|
|
@ -51,72 +44,82 @@ impl AppConfig {
|
|||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl AppConfig {
|
||||
pub fn from_database(conn: &mut PgConnection) -> Result<Self, diesel::result::Error> {
|
||||
pub fn from_database(pool: &DbPool) -> Result<Self, diesel::result::Error> {
|
||||
use crate::shared::models::schema::bot_configuration::dsl::*;
|
||||
use diesel::prelude::*;
|
||||
|
||||
let config_map: HashMap<String, (Uuid, Uuid, String, String, String, bool)> = bot_configuration
|
||||
.select((id, bot_id, config_key, config_value, config_type, is_encrypted))
|
||||
.load::<(Uuid, Uuid, String, String, String, bool)>(conn)
|
||||
let mut conn = pool.get().map_err(|e| {
|
||||
diesel::result::Error::DatabaseError(
|
||||
diesel::result::DatabaseErrorKind::UnableToSendCommand,
|
||||
Box::new(e.to_string()),
|
||||
)
|
||||
})?;
|
||||
let config_map: HashMap<String, (Uuid, Uuid, String, String, String, bool)> =
|
||||
bot_configuration
|
||||
.select((
|
||||
id,
|
||||
bot_id,
|
||||
config_key,
|
||||
config_value,
|
||||
config_type,
|
||||
is_encrypted,
|
||||
))
|
||||
.load::<(Uuid, Uuid, String, String, String, bool)>(&mut conn)
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|(_, _, key, value, _, _)| (key.clone(), (Uuid::nil(), Uuid::nil(), key, value, String::new(), false)))
|
||||
.map(|(_, _, key, value, _, _)| {
|
||||
(
|
||||
key.clone(),
|
||||
(Uuid::nil(), Uuid::nil(), key, value, String::new(), false),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut get_str = |key: &str, default: &str| -> String {
|
||||
bot_configuration
|
||||
.filter(config_key.eq(key))
|
||||
.select(config_value)
|
||||
.first::<String>(conn)
|
||||
.first::<String>(&mut conn)
|
||||
.unwrap_or_else(|_| default.to_string())
|
||||
};
|
||||
|
||||
let get_u32 = |key: &str, default: u32| -> u32 {
|
||||
config_map
|
||||
.get(key)
|
||||
.and_then(|v| v.3.parse().ok())
|
||||
.unwrap_or(default)
|
||||
};
|
||||
|
||||
let get_u16 = |key: &str, default: u16| -> u16 {
|
||||
config_map
|
||||
.get(key)
|
||||
.and_then(|v| v.3.parse().ok())
|
||||
.unwrap_or(default)
|
||||
};
|
||||
|
||||
let get_bool = |key: &str, default: bool| -> bool {
|
||||
config_map
|
||||
.get(key)
|
||||
.map(|v| v.3.to_lowercase() == "true")
|
||||
.unwrap_or(default)
|
||||
};
|
||||
|
||||
let database = DatabaseConfig {
|
||||
username: match std::env::var("TABLES_USERNAME") {
|
||||
username: match std::env::var("TABLES_USERNAME") {
|
||||
Ok(v) => v,
|
||||
Err(_) => get_str("TABLES_USERNAME", "gbuser"),
|
||||
},
|
||||
password: match std::env::var("TABLES_PASSWORD") {
|
||||
},
|
||||
password: match std::env::var("TABLES_PASSWORD") {
|
||||
Ok(v) => v,
|
||||
Err(_) => get_str("TABLES_PASSWORD", ""),
|
||||
},
|
||||
server: match std::env::var("TABLES_SERVER") {
|
||||
},
|
||||
server: match std::env::var("TABLES_SERVER") {
|
||||
Ok(v) => v,
|
||||
Err(_) => get_str("TABLES_SERVER", "localhost"),
|
||||
},
|
||||
port: std::env::var("TABLES_PORT")
|
||||
},
|
||||
port: std::env::var("TABLES_PORT")
|
||||
.ok()
|
||||
.and_then(|p| p.parse().ok())
|
||||
.unwrap_or_else(|| get_u32("TABLES_PORT", 5432)),
|
||||
database: match std::env::var("TABLES_DATABASE") {
|
||||
database: match std::env::var("TABLES_DATABASE") {
|
||||
Ok(v) => v,
|
||||
Err(_) => get_str("TABLES_DATABASE", "botserver"),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
let drive = DriveConfig {
|
||||
server: {
|
||||
let server = get_str("DRIVE_SERVER", "http://localhost:9000");
|
||||
|
|
@ -130,7 +133,6 @@ database: match std::env::var("TABLES_DATABASE") {
|
|||
secret_key: get_str("DRIVE_SECRET", "minioadmin"),
|
||||
use_ssl: get_bool("DRIVE_USE_SSL", false),
|
||||
};
|
||||
|
||||
Ok(AppConfig {
|
||||
drive,
|
||||
server: ServerConfig {
|
||||
|
|
@ -139,20 +141,16 @@ database: match std::env::var("TABLES_DATABASE") {
|
|||
},
|
||||
database,
|
||||
site_path: {
|
||||
let fresh_conn = establish_pg_connection().map_err(|e| diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::UnableToSendCommand, Box::new(e.to_string())))?;
|
||||
ConfigManager::new(Arc::new(Mutex::new(fresh_conn)))
|
||||
.get_config(&Uuid::nil(), "SITES_ROOT", Some("./botserver-stack/sites"))?.to_string()
|
||||
ConfigManager::new(pool.clone())
|
||||
.get_config(&Uuid::nil(), "SITES_ROOT", Some("./botserver-stack/sites"))?
|
||||
.to_string()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub fn from_env() -> Result<Self, anyhow::Error> {
|
||||
let database_url = std::env::var("DATABASE_URL")
|
||||
.unwrap_or_else(|_| "postgres://gbuser:@localhost:5432/botserver".to_string());
|
||||
|
||||
let database_url = std::env::var("DATABASE_URL").unwrap();
|
||||
let (db_username, db_password, db_server, db_port, db_name) =
|
||||
parse_database_url(&database_url);
|
||||
|
||||
let database = DatabaseConfig {
|
||||
username: db_username,
|
||||
password: db_password,
|
||||
|
|
@ -160,19 +158,13 @@ database: match std::env::var("TABLES_DATABASE") {
|
|||
port: db_port,
|
||||
database: db_name,
|
||||
};
|
||||
|
||||
let minio = DriveConfig {
|
||||
server: std::env::var("DRIVE_SERVER")
|
||||
.unwrap_or_else(|_| "http://localhost:9000".to_string()),
|
||||
.unwrap();
|
||||
access_key: std::env::var("DRIVE_ACCESSKEY")
|
||||
.unwrap_or_else(|_| "minioadmin".to_string()),
|
||||
.unwrap();
|
||||
secret_key: std::env::var("DRIVE_SECRET").unwrap_or_else(|_| "minioadmin".to_string()),
|
||||
use_ssl: std::env::var("DRIVE_USE_SSL")
|
||||
.unwrap_or_else(|_| "false".to_string())
|
||||
.parse()
|
||||
.unwrap_or(false)
|
||||
};
|
||||
|
||||
Ok(AppConfig {
|
||||
drive: minio,
|
||||
server: ServerConfig {
|
||||
|
|
@ -184,54 +176,36 @@ database: match std::env::var("TABLES_DATABASE") {
|
|||
},
|
||||
database,
|
||||
site_path: {
|
||||
let conn = PgConnection::establish(&database_url)?;
|
||||
ConfigManager::new(Arc::new(Mutex::new(conn)))
|
||||
.get_config(&Uuid::nil(), "SITES_ROOT", Some("./botserver-stack/sites"))?
|
||||
let pool = create_conn()?;
|
||||
ConfigManager::new(pool).get_config(
|
||||
&Uuid::nil(),
|
||||
"SITES_ROOT",
|
||||
Some("./botserver-stack/sites"),
|
||||
)?
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_drive_config_to_env(drive: &DriveConfig) -> std::io::Result<()> {
|
||||
let mut file = OpenOptions::new()
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open(".env")?;
|
||||
|
||||
writeln!(file, "")?;
|
||||
writeln!(file, "DRIVE_SERVER={}", drive.server)?;
|
||||
writeln!(file, "DRIVE_ACCESSKEY={}", drive.access_key)?;
|
||||
writeln!(file, "DRIVE_SECRET={}", drive.secret_key)?;
|
||||
writeln!(file, "DRIVE_USE_SSL={}", drive.use_ssl)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_database_url(url: &str) -> (String, String, String, u32, String) {
|
||||
if let Some(stripped) = url.strip_prefix("postgres://") {
|
||||
let parts: Vec<&str> = stripped.split('@').collect();
|
||||
if parts.len() == 2 {
|
||||
let user_pass: Vec<&str> = parts[0].split(':').collect();
|
||||
let host_db: Vec<&str> = parts[1].split('/').collect();
|
||||
|
||||
if user_pass.len() >= 2 && host_db.len() >= 2 {
|
||||
let username = user_pass[0].to_string();
|
||||
let password = user_pass[1].to_string();
|
||||
|
||||
let host_port: Vec<&str> = host_db[0].split(':').collect();
|
||||
let server = host_port[0].to_string();
|
||||
let port = host_port
|
||||
.get(1)
|
||||
.and_then(|p| p.parse().ok())
|
||||
.unwrap_or(5432);
|
||||
|
||||
let database = host_db[1].to_string();
|
||||
|
||||
return (username, password, server, port, database);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(
|
||||
"gbuser".to_string(),
|
||||
"".to_string(),
|
||||
|
|
@ -240,16 +214,23 @@ fn parse_database_url(url: &str) -> (String, String, String, u32, String) {
|
|||
"botserver".to_string(),
|
||||
)
|
||||
}
|
||||
|
||||
pub struct ConfigManager {
|
||||
conn: Arc<Mutex<PgConnection>>,
|
||||
conn: DbPool,
|
||||
}
|
||||
|
||||
impl ConfigManager {
|
||||
pub fn new(conn: Arc<Mutex<PgConnection>>) -> Self {
|
||||
pub fn new(conn: DbPool) -> Self {
|
||||
Self { conn }
|
||||
}
|
||||
|
||||
fn get_conn(
|
||||
&self,
|
||||
) -> Result<PooledConnection<ConnectionManager<PgConnection>>, diesel::result::Error> {
|
||||
self.conn.get().map_err(|e| {
|
||||
diesel::result::Error::DatabaseError(
|
||||
diesel::result::DatabaseErrorKind::UnableToSendCommand,
|
||||
Box::new(e.to_string()),
|
||||
)
|
||||
})
|
||||
}
|
||||
pub fn get_config(
|
||||
&self,
|
||||
code_bot_id: &uuid::Uuid,
|
||||
|
|
@ -257,72 +238,55 @@ impl ConfigManager {
|
|||
fallback: Option<&str>,
|
||||
) -> Result<String, diesel::result::Error> {
|
||||
use crate::shared::models::schema::bot_configuration::dsl::*;
|
||||
|
||||
let mut conn = self.conn.lock().unwrap();
|
||||
let mut conn = self.get_conn()?;
|
||||
let fallback_str = fallback.unwrap_or("");
|
||||
|
||||
let result = bot_configuration
|
||||
.filter(bot_id.eq(code_bot_id))
|
||||
.filter(config_key.eq(key))
|
||||
.select(config_value)
|
||||
.first::<String>(&mut *conn);
|
||||
|
||||
.first::<String>(&mut conn);
|
||||
let value = match result {
|
||||
Ok(v) => v,
|
||||
Err(_) => {
|
||||
let (default_bot_id, _default_bot_name) = crate::bot::get_default_bot(&mut *conn);
|
||||
|
||||
let (default_bot_id, _default_bot_name) = crate::bot::get_default_bot(&mut conn);
|
||||
bot_configuration
|
||||
.filter(bot_id.eq(default_bot_id))
|
||||
.filter(config_key.eq(key))
|
||||
.select(config_value)
|
||||
.first::<String>(&mut *conn)
|
||||
.first::<String>(&mut conn)
|
||||
.unwrap_or(fallback_str.to_string())
|
||||
}
|
||||
};
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
pub fn sync_gbot_config(
|
||||
&self,
|
||||
bot_id: &uuid::Uuid,
|
||||
content: &str,
|
||||
) -> Result<usize, String> {
|
||||
pub fn sync_gbot_config(&self, bot_id: &uuid::Uuid, content: &str) -> Result<usize, String> {
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(content.as_bytes());
|
||||
|
||||
let mut conn = self
|
||||
.conn
|
||||
.lock()
|
||||
.map_err(|e| format!("Failed to acquire lock: {}", e))?;
|
||||
|
||||
.get_conn()
|
||||
.map_err(|e| format!("Failed to acquire connection: {}", e))?;
|
||||
let mut updated = 0;
|
||||
|
||||
for line in content.lines().skip(1) {
|
||||
let parts: Vec<&str> = line.split(',').collect();
|
||||
if parts.len() >= 2 {
|
||||
let key = parts[0].trim();
|
||||
let value = parts[1].trim();
|
||||
|
||||
let new_id: uuid::Uuid = uuid::Uuid::new_v4();
|
||||
|
||||
diesel::sql_query("INSERT INTO bot_configuration (id, bot_id, config_key, config_value, config_type) VALUES ($1, $2, $3, $4, 'string') ON CONFLICT (bot_id, config_key) DO UPDATE SET config_value = EXCLUDED.config_value, updated_at = NOW()")
|
||||
.bind::<diesel::sql_types::Uuid, _>(new_id)
|
||||
.bind::<diesel::sql_types::Uuid, _>(bot_id)
|
||||
.bind::<diesel::sql_types::Text, _>(key)
|
||||
.bind::<diesel::sql_types::Text, _>(value)
|
||||
.execute(&mut *conn)
|
||||
.execute(&mut conn)
|
||||
.map_err(|e| format!("Failed to update config: {}", e))?;
|
||||
|
||||
updated += 1;
|
||||
}
|
||||
}
|
||||
|
||||
trace!("Synced {} config values for bot {}", updated, bot_id);
|
||||
|
||||
Ok(updated)
|
||||
}
|
||||
}
|
||||
fn create_conn() -> Result<DbPool, anyhow::Error> {
|
||||
crate::shared::utils::create_conn()
|
||||
.map_err(|e| anyhow::anyhow!("Failed to create database pool: {}", e))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -132,7 +132,7 @@ impl DriveMonitor {
|
|||
}
|
||||
|
||||
async fn check_gbot(&self, client: &Client) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let config_manager = ConfigManager::new(Arc::clone(&self.state.conn));
|
||||
let config_manager = ConfigManager::new(self.state.conn.clone());
|
||||
let mut continuation_token = None;
|
||||
|
||||
loop {
|
||||
|
|
@ -194,7 +194,7 @@ impl DriveMonitor {
|
|||
let _ = config_manager.sync_gbot_config(&self.bot_id, &csv_content);
|
||||
|
||||
if restart_needed {
|
||||
if let Err(e) = ensure_llama_servers_running(&self.state).await {
|
||||
if let Err(e) = ensure_llama_servers_running(Arc::clone(&self.state)).await {
|
||||
log::error!("Failed to restart LLaMA servers after llm- config change: {}", e);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,20 +28,19 @@ pub async fn embeddings_local(
|
|||
}
|
||||
|
||||
pub async fn ensure_llama_servers_running(
|
||||
app_state: &Arc<AppState>
|
||||
app_state: Arc<AppState>
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
// Get all config values before starting async operations
|
||||
let config_values = {
|
||||
let conn_arc = app_state.conn.clone();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let mut conn = conn_arc.lock().unwrap();
|
||||
let config_manager = ConfigManager::new(Arc::clone(&conn_arc));
|
||||
|
||||
let default_bot_id = bots.filter(name.eq("default"))
|
||||
let default_bot_id = tokio::task::spawn_blocking(move || {
|
||||
let mut conn = conn_arc.get().unwrap();
|
||||
bots.filter(name.eq("default"))
|
||||
.select(id)
|
||||
.first::<uuid::Uuid>(&mut *conn)
|
||||
.unwrap_or_else(|_| uuid::Uuid::nil());
|
||||
|
||||
.unwrap_or_else(|_| uuid::Uuid::nil())
|
||||
}).await?;
|
||||
let config_manager = ConfigManager::new(app_state.conn.clone());
|
||||
(
|
||||
default_bot_id,
|
||||
config_manager.get_config(&default_bot_id, "llm-url", None).unwrap_or_default(),
|
||||
|
|
@ -50,7 +49,6 @@ let config_values = {
|
|||
config_manager.get_config(&default_bot_id, "embedding-model", None).unwrap_or_default(),
|
||||
config_manager.get_config(&default_bot_id, "llm-server-path", None).unwrap_or_default(),
|
||||
)
|
||||
}).await?
|
||||
};
|
||||
let (_default_bot_id, llm_url, llm_model, embedding_url, embedding_model, llm_server_path) = config_values;
|
||||
|
||||
|
|
@ -90,7 +88,7 @@ let (_default_bot_id, llm_url, llm_model, embedding_url, embedding_model, llm_se
|
|||
if !llm_running && !llm_model.is_empty() {
|
||||
info!("Starting LLM server...");
|
||||
tasks.push(tokio::spawn(start_llm_server(
|
||||
Arc::clone(app_state),
|
||||
Arc::clone(&app_state),
|
||||
llm_server_path.clone(),
|
||||
llm_model.clone(),
|
||||
llm_url.clone(),
|
||||
|
|
@ -192,13 +190,11 @@ pub async fn start_llm_server(
|
|||
let conn = app_state.conn.clone();
|
||||
let config_manager = ConfigManager::new(conn.clone());
|
||||
|
||||
let default_bot_id = {
|
||||
let mut conn = conn.lock().unwrap();
|
||||
bots.filter(name.eq("default"))
|
||||
let mut conn = conn.get().unwrap();
|
||||
let default_bot_id = bots.filter(name.eq("default"))
|
||||
.select(id)
|
||||
.first::<uuid::Uuid>(&mut *conn)
|
||||
.unwrap_or_else(|_| uuid::Uuid::nil())
|
||||
};
|
||||
.unwrap_or_else(|_| uuid::Uuid::nil());
|
||||
|
||||
let n_moe = config_manager.get_config(&default_bot_id, "llm-server-n-moe", None).unwrap_or("4".to_string());
|
||||
let parallel = config_manager.get_config(&default_bot_id, "llm-server-parallel", None).unwrap_or("1".to_string());
|
||||
|
|
|
|||
559
src/main.rs
559
src/main.rs
|
|
@ -5,7 +5,7 @@ use actix_web::{web, App, HttpServer};
|
|||
use dotenvy::dotenv;
|
||||
use log::{error, info};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::Arc;
|
||||
mod auth;
|
||||
mod automation;
|
||||
mod basic;
|
||||
|
|
@ -37,299 +37,320 @@ use crate::channels::{VoiceAdapter, WebChannelAdapter};
|
|||
use crate::config::AppConfig;
|
||||
#[cfg(feature = "email")]
|
||||
use crate::email::{
|
||||
get_emails, get_latest_email_from, list_emails, save_click, save_draft, send_email,
|
||||
get_emails, get_latest_email_from, list_emails, save_click, save_draft, send_email,
|
||||
};
|
||||
use crate::file::{init_drive, upload_file};
|
||||
use crate::meet::{voice_start, voice_stop};
|
||||
use crate::package_manager::InstallMode;
|
||||
use crate::session::{create_session, get_session_history, get_sessions, start_session};
|
||||
use crate::shared::state::AppState;
|
||||
use crate::shared::utils::create_conn;
|
||||
use crate::web_server::{bot_index, index, static_files};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum BootstrapProgress {
|
||||
StartingBootstrap,
|
||||
InstallingComponent(String),
|
||||
StartingComponent(String),
|
||||
UploadingTemplates,
|
||||
ConnectingDatabase,
|
||||
StartingLLM,
|
||||
BootstrapComplete,
|
||||
BootstrapError(String),
|
||||
StartingBootstrap,
|
||||
InstallingComponent(String),
|
||||
StartingComponent(String),
|
||||
UploadingTemplates,
|
||||
ConnectingDatabase,
|
||||
StartingLLM,
|
||||
BootstrapComplete,
|
||||
BootstrapError(String),
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use crate::llm::local::ensure_llama_servers_running;
|
||||
use botserver::config::ConfigManager;
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let no_ui = args.contains(&"--noui".to_string());
|
||||
if args.len() > 1 {
|
||||
let command = &args[1];
|
||||
match command.as_str() {
|
||||
"install" | "remove" | "list" | "status" | "start" | "stop" | "restart" | "--help"
|
||||
| "-h" => match package_manager::cli::run().await {
|
||||
Ok(_) => return Ok(()),
|
||||
Err(e) => {
|
||||
eprintln!("CLI error: {}", e);
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
format!("CLI command failed: {}", e),
|
||||
));
|
||||
}
|
||||
},
|
||||
"--noui" => {}
|
||||
_ => {
|
||||
eprintln!("Unknown command: {}", command);
|
||||
eprintln!("Run 'botserver --help' for usage information");
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidInput,
|
||||
format!("Unknown command: {}", command),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
dotenv().ok();
|
||||
let (progress_tx, progress_rx) = tokio::sync::mpsc::unbounded_channel::<BootstrapProgress>();
|
||||
let (state_tx, state_rx) = tokio::sync::mpsc::channel::<Arc<AppState>>(1);
|
||||
let ui_handle = if !no_ui {
|
||||
let progress_rx = Arc::new(tokio::sync::Mutex::new(progress_rx));
|
||||
let state_rx = Arc::new(tokio::sync::Mutex::new(state_rx));
|
||||
let handle = std::thread::Builder::new()
|
||||
.name("ui-thread".to_string())
|
||||
.spawn(move || {
|
||||
let mut ui = crate::ui_tree::XtreeUI::new();
|
||||
ui.set_progress_channel(progress_rx.clone());
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.expect("Failed to create UI runtime");
|
||||
rt.block_on(async {
|
||||
tokio::select! {
|
||||
result = async {
|
||||
let mut rx = state_rx.lock().await;
|
||||
rx.recv().await
|
||||
} => {
|
||||
if let Some(app_state) = result {
|
||||
ui.set_app_state(app_state);
|
||||
}
|
||||
}
|
||||
_ = tokio::time::sleep(tokio::time::Duration::from_secs(300)) => {
|
||||
eprintln!("UI initialization timeout");
|
||||
}
|
||||
}
|
||||
});
|
||||
if let Err(e) = ui.start_ui() {
|
||||
eprintln!("UI error: {}", e);
|
||||
}
|
||||
})
|
||||
.expect("Failed to spawn UI thread");
|
||||
Some(handle)
|
||||
} else {
|
||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
|
||||
.write_style(env_logger::WriteStyle::Always)
|
||||
.init();
|
||||
None
|
||||
};
|
||||
let install_mode = if args.contains(&"--container".to_string()) {
|
||||
InstallMode::Container
|
||||
} else {
|
||||
InstallMode::Local
|
||||
};
|
||||
let tenant = if let Some(idx) = args.iter().position(|a| a == "--tenant") {
|
||||
args.get(idx + 1).cloned()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let progress_tx_clone = progress_tx.clone();
|
||||
let cfg = {
|
||||
progress_tx_clone.send(BootstrapProgress::StartingBootstrap).ok();
|
||||
let mut bootstrap = BootstrapManager::new(install_mode.clone(), tenant.clone()).await;
|
||||
let env_path = match std::env::current_dir() {
|
||||
Ok(dir) => dir.join("botserver-stack").join(".env"),
|
||||
Err(_) => {
|
||||
progress_tx_clone.send(BootstrapProgress::BootstrapError("Failed to get current directory".to_string())).ok();
|
||||
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Failed to get current directory"));
|
||||
use crate::llm::local::ensure_llama_servers_running;
|
||||
use botserver::config::ConfigManager;
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let no_ui = args.contains(&"--noui".to_string());
|
||||
if args.len() > 1 {
|
||||
let command = &args[1];
|
||||
match command.as_str() {
|
||||
"install" | "remove" | "list" | "status" | "start" | "stop" | "restart" | "--help"
|
||||
| "-h" => match package_manager::cli::run().await {
|
||||
Ok(_) => return Ok(()),
|
||||
Err(e) => {
|
||||
eprintln!("CLI error: {}", e);
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
format!("CLI command failed: {}", e),
|
||||
));
|
||||
}
|
||||
},
|
||||
"--noui" => {}
|
||||
_ => {
|
||||
eprintln!("Unknown command: {}", command);
|
||||
eprintln!("Run 'botserver --help' for usage information");
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidInput,
|
||||
format!("Unknown command: {}", command),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
dotenv().ok();
|
||||
let (progress_tx, progress_rx) = tokio::sync::mpsc::unbounded_channel::<BootstrapProgress>();
|
||||
let (state_tx, state_rx) = tokio::sync::mpsc::channel::<Arc<AppState>>(1);
|
||||
let ui_handle = if !no_ui {
|
||||
let progress_rx = Arc::new(tokio::sync::Mutex::new(progress_rx));
|
||||
let state_rx = Arc::new(tokio::sync::Mutex::new(state_rx));
|
||||
let handle = std::thread::Builder::new()
|
||||
.name("ui-thread".to_string())
|
||||
.spawn(move || {
|
||||
let mut ui = crate::ui_tree::XtreeUI::new();
|
||||
ui.set_progress_channel(progress_rx.clone());
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.expect("Failed to create UI runtime");
|
||||
rt.block_on(async {
|
||||
tokio::select! {
|
||||
result = async {
|
||||
let mut rx = state_rx.lock().await;
|
||||
rx.recv().await
|
||||
} => {
|
||||
if let Some(app_state) = result {
|
||||
ui.set_app_state(app_state);
|
||||
}
|
||||
}
|
||||
_ = tokio::time::sleep(tokio::time::Duration::from_secs(300)) => {
|
||||
eprintln!("UI initialization timeout");
|
||||
}
|
||||
}
|
||||
});
|
||||
if let Err(e) = ui.start_ui() {
|
||||
eprintln!("UI error: {}", e);
|
||||
}
|
||||
})
|
||||
.expect("Failed to spawn UI thread");
|
||||
Some(handle)
|
||||
} else {
|
||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
|
||||
.write_style(env_logger::WriteStyle::Always)
|
||||
.init();
|
||||
None
|
||||
};
|
||||
let install_mode = if args.contains(&"--container".to_string()) {
|
||||
InstallMode::Container
|
||||
} else {
|
||||
InstallMode::Local
|
||||
};
|
||||
let tenant = if let Some(idx) = args.iter().position(|a| a == "--tenant") {
|
||||
args.get(idx + 1).cloned()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let progress_tx_clone = progress_tx.clone();
|
||||
let cfg = {
|
||||
progress_tx_clone
|
||||
.send(BootstrapProgress::StartingBootstrap)
|
||||
.ok();
|
||||
let mut bootstrap = BootstrapManager::new(install_mode.clone(), tenant.clone()).await;
|
||||
let env_path = std::env::current_dir().unwrap().join(".env");
|
||||
let cfg = if env_path.exists() {
|
||||
progress_tx_clone.send(BootstrapProgress::ConnectingDatabase).ok();
|
||||
match diesel::Connection::establish(&std::env::var("DATABASE_URL").unwrap_or_else(|_| "postgres://gbuser:@localhost:5432/botserver".to_string())) {
|
||||
Ok(mut conn) => {
|
||||
AppConfig::from_database(&mut conn).unwrap_or_else(|_| AppConfig::from_env().expect("Failed to load config"))
|
||||
progress_tx_clone
|
||||
.send(BootstrapProgress::ConnectingDatabase)
|
||||
.ok();
|
||||
match create_conn() {
|
||||
Ok(pool) => {
|
||||
let mut conn = pool.get().map_err(|e| {
|
||||
std::io::Error::new(
|
||||
std::io::ErrorKind::ConnectionRefused,
|
||||
format!("Database connection failed: {}", e),
|
||||
)
|
||||
})?;
|
||||
AppConfig::from_database(&pool)
|
||||
.unwrap_or_else(|_| AppConfig::from_env().expect("Failed to load config"))
|
||||
}
|
||||
Err(_) => AppConfig::from_env().expect("Failed to load config from env"),
|
||||
}
|
||||
} else {
|
||||
match bootstrap.bootstrap().await {
|
||||
Ok(config) => config,
|
||||
Err(e) => {
|
||||
progress_tx_clone.send(BootstrapProgress::BootstrapError(format!("Bootstrap failed: {}", e))).ok();
|
||||
match diesel::Connection::establish(&std::env::var("DATABASE_URL").unwrap_or_else(|_| "postgres://gbuser:@localhost:5432/botserver".to_string())) {
|
||||
Ok(mut conn) => {
|
||||
AppConfig::from_database(&mut conn).unwrap_or_else(|_| AppConfig::from_env().expect("Failed to load config"))
|
||||
}
|
||||
bootstrap.bootstrap().await;
|
||||
match create_conn() {
|
||||
Ok(pool) => AppConfig::from_database(&pool)
|
||||
.unwrap_or_else(|_| AppConfig::from_env().expect("Failed to load config")),
|
||||
Err(_) => AppConfig::from_env().expect("Failed to load config from env"),
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
progress_tx_clone.send(BootstrapProgress::StartingComponent("all services".to_string())).ok();
|
||||
progress_tx_clone
|
||||
.send(BootstrapProgress::StartingComponent(
|
||||
"all services".to_string(),
|
||||
))
|
||||
.ok();
|
||||
if let Err(e) = bootstrap.start_all() {
|
||||
progress_tx_clone.send(BootstrapProgress::BootstrapError(format!("Failed to start services: {}", e))).ok();
|
||||
progress_tx_clone
|
||||
.send(BootstrapProgress::BootstrapError(format!(
|
||||
"Failed to start services: {}",
|
||||
e
|
||||
)))
|
||||
.ok();
|
||||
}
|
||||
progress_tx_clone.send(BootstrapProgress::UploadingTemplates).ok();
|
||||
progress_tx_clone
|
||||
.send(BootstrapProgress::UploadingTemplates)
|
||||
.ok();
|
||||
if let Err(e) = bootstrap.upload_templates_to_drive(&cfg).await {
|
||||
progress_tx_clone.send(BootstrapProgress::BootstrapError(format!("Failed to upload templates: {}", e))).ok();
|
||||
progress_tx_clone
|
||||
.send(BootstrapProgress::BootstrapError(format!(
|
||||
"Failed to upload templates: {}",
|
||||
e
|
||||
)))
|
||||
.ok();
|
||||
}
|
||||
Ok::<AppConfig, std::io::Error>(cfg)
|
||||
};
|
||||
let cfg = cfg?;
|
||||
dotenv().ok();
|
||||
let refreshed_cfg = AppConfig::from_env().expect("Failed to load config from env");
|
||||
let config = std::sync::Arc::new(refreshed_cfg.clone());
|
||||
progress_tx.send(BootstrapProgress::ConnectingDatabase).ok();
|
||||
let db_pool = match diesel::Connection::establish(&refreshed_cfg.database_url()) {
|
||||
Ok(conn) => Arc::new(Mutex::new(conn)),
|
||||
Err(e) => {
|
||||
error!("Failed to connect to main database: {}", e);
|
||||
progress_tx.send(BootstrapProgress::BootstrapError(format!("Database connection failed: {}", e))).ok();
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::ConnectionRefused,
|
||||
format!("Database connection failed: {}", e),
|
||||
));
|
||||
}
|
||||
};
|
||||
let cache_url = std::env::var("CACHE_URL")
|
||||
.or_else(|_| std::env::var("REDIS_URL"))
|
||||
.unwrap_or_else(|_| "redis://localhost:6379".to_string());
|
||||
let redis_client = match redis::Client::open(cache_url.as_str()) {
|
||||
Ok(client) => Some(Arc::new(client)),
|
||||
Err(e) => {
|
||||
log::warn!("Failed to connect to Redis: {}", e);
|
||||
None
|
||||
}
|
||||
};
|
||||
let web_adapter = Arc::new(WebChannelAdapter::new());
|
||||
let voice_adapter = Arc::new(VoiceAdapter::new());
|
||||
let drive = init_drive(&config.drive)
|
||||
.await
|
||||
.expect("Failed to initialize Drive");
|
||||
let session_manager = Arc::new(tokio::sync::Mutex::new(session::SessionManager::new(
|
||||
diesel::Connection::establish(&cfg.database_url()).unwrap(),
|
||||
redis_client.clone(),
|
||||
)));
|
||||
let auth_service = Arc::new(tokio::sync::Mutex::new(auth::AuthService::new()));
|
||||
let conn = diesel::Connection::establish(&cfg.database_url()).unwrap();
|
||||
let config_manager = ConfigManager::new(Arc::new(Mutex::new(conn)));
|
||||
let mut bot_conn = diesel::Connection::establish(&cfg.database_url()).unwrap();
|
||||
let (default_bot_id, _default_bot_name) = crate::bot::get_default_bot(&mut bot_conn);
|
||||
let llm_url = config_manager
|
||||
.get_config(&default_bot_id, "llm-url", Some("http://localhost:8081"))
|
||||
.unwrap_or_else(|_| "http://localhost:8081".to_string());
|
||||
let llm_provider = Arc::new(crate::llm::OpenAIClient::new(
|
||||
"empty".to_string(),
|
||||
Some(llm_url.clone()),
|
||||
));
|
||||
let app_state = Arc::new(AppState {
|
||||
drive: Some(drive),
|
||||
config: Some(cfg.clone()),
|
||||
conn: db_pool.clone(),
|
||||
bucket_name: "default.gbai".to_string(),
|
||||
cache: redis_client.clone(),
|
||||
session_manager: session_manager.clone(),
|
||||
llm_provider: llm_provider.clone(),
|
||||
auth_service: auth_service.clone(),
|
||||
channels: Arc::new(Mutex::new({
|
||||
let mut map = HashMap::new();
|
||||
map.insert(
|
||||
"web".to_string(),
|
||||
web_adapter.clone() as Arc<dyn crate::channels::ChannelAdapter>,
|
||||
);
|
||||
map
|
||||
})),
|
||||
response_channels: Arc::new(tokio::sync::Mutex::new(HashMap::new())),
|
||||
web_adapter: web_adapter.clone(),
|
||||
voice_adapter: voice_adapter.clone(),
|
||||
});
|
||||
state_tx.send(app_state.clone()).await.ok();
|
||||
progress_tx.send(BootstrapProgress::BootstrapComplete).ok();
|
||||
info!("Starting HTTP server on {}:{}", config.server.host, config.server.port);
|
||||
let worker_count = std::thread::available_parallelism()
|
||||
.map(|n| n.get())
|
||||
.unwrap_or(4);
|
||||
let bot_orchestrator = BotOrchestrator::new(app_state.clone());
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = bot_orchestrator.mount_all_bots().await {
|
||||
error!("Failed to mount bots: {}", e);
|
||||
}
|
||||
});
|
||||
let automation_state = app_state.clone();
|
||||
std::thread::spawn(move || {
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.expect("Failed to create runtime for automation");
|
||||
let local = tokio::task::LocalSet::new();
|
||||
local.block_on(&rt, async move {
|
||||
let automation = AutomationService::new(automation_state);
|
||||
automation.spawn().await.ok();
|
||||
});
|
||||
});
|
||||
let app_state_for_llm = app_state.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = ensure_llama_servers_running(&app_state_for_llm).await {
|
||||
error!("Failed to start LLM servers: {}", e);
|
||||
}
|
||||
});
|
||||
let server_result = HttpServer::new(move || {
|
||||
let cors = Cors::default()
|
||||
.allow_any_origin()
|
||||
.allow_any_method()
|
||||
.allow_any_header()
|
||||
.max_age(3600);
|
||||
let app_state_clone = app_state.clone();
|
||||
let mut app = App::new()
|
||||
.wrap(cors)
|
||||
.wrap(Logger::default())
|
||||
.wrap(Logger::new("HTTP REQUEST: %a %{User-Agent}i"))
|
||||
.app_data(web::Data::from(app_state_clone))
|
||||
.service(auth_handler)
|
||||
.service(create_session)
|
||||
.service(get_session_history)
|
||||
.service(get_sessions)
|
||||
.service(index)
|
||||
.service(start_session)
|
||||
.service(upload_file)
|
||||
.service(voice_start)
|
||||
.service(voice_stop)
|
||||
.service(websocket_handler)
|
||||
.service(crate::bot::create_bot_handler)
|
||||
.service(crate::bot::mount_bot_handler)
|
||||
.service(crate::bot::handle_user_input_handler)
|
||||
.service(crate::bot::get_user_sessions_handler)
|
||||
.service(crate::bot::get_conversation_history_handler)
|
||||
.service(crate::bot::send_warning_handler);
|
||||
#[cfg(feature = "email")]
|
||||
{
|
||||
app = app
|
||||
.service(get_latest_email_from)
|
||||
.service(get_emails)
|
||||
.service(list_emails)
|
||||
.service(send_email)
|
||||
.service(save_draft)
|
||||
.service(save_click);
|
||||
}
|
||||
app = app.service(static_files);
|
||||
app = app.service(bot_index);
|
||||
app
|
||||
})
|
||||
.workers(worker_count)
|
||||
.bind((config.server.host.clone(), config.server.port))?
|
||||
.run()
|
||||
.await;
|
||||
if let Some(handle) = ui_handle {
|
||||
handle.join().ok();
|
||||
}
|
||||
server_result
|
||||
};
|
||||
let cfg = cfg?;
|
||||
dotenv().ok();
|
||||
let refreshed_cfg = AppConfig::from_env().expect("Failed to load config from env");
|
||||
let config = std::sync::Arc::new(refreshed_cfg.clone());
|
||||
progress_tx.send(BootstrapProgress::ConnectingDatabase).ok();
|
||||
let pool = match create_conn() {
|
||||
Ok(pool) => pool,
|
||||
Err(e) => {
|
||||
error!("Failed to create database pool: {}", e);
|
||||
progress_tx
|
||||
.send(BootstrapProgress::BootstrapError(format!(
|
||||
"Database pool creation failed: {}",
|
||||
e
|
||||
)))
|
||||
.ok();
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::ConnectionRefused,
|
||||
format!("Database pool creation failed: {}", e),
|
||||
));
|
||||
}
|
||||
};
|
||||
let cache_url = std::env::var("CACHE_URL")
|
||||
.or_else(|_| std::env::var("REDIS_URL"))
|
||||
.unwrap_or_else(|_| "redis://localhost:6379".to_string());
|
||||
let redis_client = match redis::Client::open(cache_url.as_str()) {
|
||||
Ok(client) => Some(Arc::new(client)),
|
||||
Err(e) => {
|
||||
log::warn!("Failed to connect to Redis: {}", e);
|
||||
None
|
||||
}
|
||||
};
|
||||
let web_adapter = Arc::new(WebChannelAdapter::new());
|
||||
let voice_adapter = Arc::new(VoiceAdapter::new());
|
||||
let drive = init_drive(&config.drive)
|
||||
.await
|
||||
.expect("Failed to initialize Drive");
|
||||
let session_manager = Arc::new(tokio::sync::Mutex::new(session::SessionManager::new(
|
||||
pool.get().unwrap(),
|
||||
redis_client.clone(),
|
||||
)));
|
||||
let auth_service = Arc::new(tokio::sync::Mutex::new(auth::AuthService::new()));
|
||||
let config_manager = ConfigManager::new(pool.clone());
|
||||
let mut bot_conn = pool.get().expect("Failed to get database connection");
|
||||
let (default_bot_id, _default_bot_name) = crate::bot::get_default_bot(&mut bot_conn);
|
||||
let llm_url = config_manager
|
||||
.get_config(&default_bot_id, "llm-url", Some("http://localhost:8081"))
|
||||
.unwrap_or_else(|_| "http://localhost:8081".to_string());
|
||||
let llm_provider = Arc::new(crate::llm::OpenAIClient::new(
|
||||
"empty".to_string(),
|
||||
Some(llm_url.clone()),
|
||||
));
|
||||
let app_state = Arc::new(AppState {
|
||||
drive: Some(drive),
|
||||
config: Some(cfg.clone()),
|
||||
conn: pool.clone(),
|
||||
bucket_name: "default.gbai".to_string(),
|
||||
cache: redis_client.clone(),
|
||||
session_manager: session_manager.clone(),
|
||||
llm_provider: llm_provider.clone(),
|
||||
auth_service: auth_service.clone(),
|
||||
channels: Arc::new(tokio::sync::Mutex::new({
|
||||
let mut map = HashMap::new();
|
||||
map.insert(
|
||||
"web".to_string(),
|
||||
web_adapter.clone() as Arc<dyn crate::channels::ChannelAdapter>,
|
||||
);
|
||||
map
|
||||
})),
|
||||
response_channels: Arc::new(tokio::sync::Mutex::new(HashMap::new())),
|
||||
web_adapter: web_adapter.clone(),
|
||||
voice_adapter: voice_adapter.clone(),
|
||||
});
|
||||
state_tx.send(app_state.clone()).await.ok();
|
||||
progress_tx.send(BootstrapProgress::BootstrapComplete).ok();
|
||||
info!(
|
||||
"Starting HTTP server on {}:{}",
|
||||
config.server.host, config.server.port
|
||||
);
|
||||
let worker_count = std::thread::available_parallelism()
|
||||
.map(|n| n.get())
|
||||
.unwrap_or(4);
|
||||
let bot_orchestrator = BotOrchestrator::new(app_state.clone());
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = bot_orchestrator.mount_all_bots().await {
|
||||
error!("Failed to mount bots: {}", e);
|
||||
}
|
||||
});
|
||||
let automation_state = app_state.clone();
|
||||
std::thread::spawn(move || {
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.expect("Failed to create runtime for automation");
|
||||
let local = tokio::task::LocalSet::new();
|
||||
local.block_on(&rt, async move {
|
||||
let automation = AutomationService::new(automation_state);
|
||||
automation.spawn().await.ok();
|
||||
});
|
||||
});
|
||||
let app_state_for_llm = app_state.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = ensure_llama_servers_running(app_state_for_llm).await {
|
||||
error!("Failed to start LLM servers: {}", e);
|
||||
}
|
||||
});
|
||||
let server_result = HttpServer::new(move || {
|
||||
let cors = Cors::default()
|
||||
.allow_any_origin()
|
||||
.allow_any_method()
|
||||
.allow_any_header()
|
||||
.max_age(3600);
|
||||
let app_state_clone = app_state.clone();
|
||||
let mut app = App::new()
|
||||
.wrap(cors)
|
||||
.wrap(Logger::default())
|
||||
.wrap(Logger::new("HTTP REQUEST: %a %{User-Agent}i"))
|
||||
.app_data(web::Data::from(app_state_clone))
|
||||
.service(auth_handler)
|
||||
.service(create_session)
|
||||
.service(get_session_history)
|
||||
.service(get_sessions)
|
||||
.service(index)
|
||||
.service(start_session)
|
||||
.service(upload_file)
|
||||
.service(voice_start)
|
||||
.service(voice_stop)
|
||||
.service(websocket_handler)
|
||||
.service(crate::bot::create_bot_handler)
|
||||
.service(crate::bot::mount_bot_handler)
|
||||
.service(crate::bot::handle_user_input_handler)
|
||||
.service(crate::bot::get_user_sessions_handler)
|
||||
.service(crate::bot::get_conversation_history_handler)
|
||||
.service(crate::bot::send_warning_handler);
|
||||
#[cfg(feature = "email")]
|
||||
{
|
||||
app = app
|
||||
.service(get_latest_email_from)
|
||||
.service(get_emails)
|
||||
.service(list_emails)
|
||||
.service(send_email)
|
||||
.service(save_draft)
|
||||
.service(save_click);
|
||||
}
|
||||
app = app.service(static_files);
|
||||
app = app.service(bot_index);
|
||||
app
|
||||
})
|
||||
.workers(worker_count)
|
||||
.bind((config.server.host.clone(), config.server.port))?
|
||||
.run()
|
||||
.await;
|
||||
if let Some(handle) = ui_handle {
|
||||
handle.join().ok();
|
||||
}
|
||||
server_result
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ use crate::package_manager::{InstallMode, OsType};
|
|||
use anyhow::Result;
|
||||
use log::trace;
|
||||
use rand::distr::Alphanumeric;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
|
|
@ -61,18 +60,20 @@ impl PackageManager {
|
|||
}
|
||||
|
||||
fn register_drive(&mut self) {
|
||||
|
||||
let drive_password = self.generate_secure_password(16);
|
||||
let drive_user = "gbdriveuser".to_string();
|
||||
let farm_password =
|
||||
std::env::var("FARM_PASSWORD").unwrap_or_else(|_| self.generate_secure_password(32));
|
||||
let encrypted_drive_password = self.encrypt_password(&drive_password, &farm_password);
|
||||
|
||||
let env_path = self.base_path.join(".env");
|
||||
let env_path = std::env::current_dir().unwrap().join(".env");
|
||||
let env_content = format!(
|
||||
"DRIVE_USER={}\nDRIVE_PASSWORD={}\nFARM_PASSWORD={}\nDRIVE_ROOT_USER={}\nDRIVE_ROOT_PASSWORD={}\n",
|
||||
drive_user, drive_password, farm_password, drive_user, drive_password
|
||||
"\nDRIVE_ACCESSKEY={}\nDRIVE_SECRET={}\nDRIVE_SERVER=http://localhost:9000\n",
|
||||
drive_user, drive_password
|
||||
);
|
||||
let _ = std::fs::write(&env_path, env_content);
|
||||
let _ = std::fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(&env_path)
|
||||
.and_then(|mut file| std::io::Write::write_all(&mut file, env_content.as_bytes()));
|
||||
|
||||
self.components.insert(
|
||||
"drive".to_string(),
|
||||
|
|
@ -103,72 +104,20 @@ impl PackageManager {
|
|||
},
|
||||
);
|
||||
|
||||
// Delay updating drive credentials until database is created
|
||||
let db_env_path = self.base_path.join(".env");
|
||||
let database_url = std::env::var("DATABASE_URL")
|
||||
.unwrap_or_else(|_| "postgres://gbuser:@localhost:5432/botserver".to_string());
|
||||
let db_line = format!("DATABASE_URL={}\n", database_url);
|
||||
let _ = std::fs::write(&db_env_path, db_line);
|
||||
|
||||
// Append drive credentials after database creation
|
||||
let env_path = self.base_path.join(".env");
|
||||
let drive_lines = format!(
|
||||
"DRIVE_USER={}\nDRIVE_PASSWORD={}\nFARM_PASSWORD={}\nDRIVE_ROOT_USER={}\nDRIVE_ROOT_PASSWORD={}\n",
|
||||
drive_user, drive_password, farm_password, drive_user, drive_password
|
||||
);
|
||||
let _ = std::fs::OpenOptions::new()
|
||||
.append(true)
|
||||
.open(&env_path)
|
||||
.and_then(|mut file| std::io::Write::write_all(&mut file, drive_lines.as_bytes()));
|
||||
|
||||
// Update drive credentials in database only after database is ready
|
||||
if std::process::Command::new("pg_isready")
|
||||
.arg("-h")
|
||||
.arg("localhost")
|
||||
.arg("-p")
|
||||
.arg("5432")
|
||||
.output()
|
||||
.map(|o| o.status.success())
|
||||
.unwrap_or(false)
|
||||
{
|
||||
self.update_drive_credentials_in_database(&encrypted_drive_password)
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
|
||||
fn update_drive_credentials_in_database(&self, encrypted_drive_password: &str) -> Result<()> {
|
||||
use crate::shared::models::schema::bots::dsl::*;
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::prelude::*;
|
||||
use uuid::Uuid;
|
||||
|
||||
let database_url = std::env::var("DATABASE_URL")
|
||||
.unwrap_or_else(|_| "postgres://gbuser:@localhost:5432/botserver".to_string());
|
||||
|
||||
if let Ok(mut conn) = PgConnection::establish(&database_url) {
|
||||
let system_bot_id = Uuid::parse_str("00000000-0000-0000-0000-000000000000")?;
|
||||
diesel::update(bots)
|
||||
.filter(id.eq(system_bot_id))
|
||||
.set(llm_config.eq(serde_json::json!({
|
||||
"encrypted_drive_password": encrypted_drive_password,
|
||||
})))
|
||||
.execute(&mut conn)?;
|
||||
trace!("Updated drive credentials in database for system bot");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn register_tables(&mut self) {
|
||||
let db_password = std::env::var("DATABASE_URL")
|
||||
.ok()
|
||||
.and_then(|url| {
|
||||
if let Some(stripped) = url.strip_prefix("postgres://gbuser:") {
|
||||
stripped.split('@').next().map(|s| s.to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|| self.generate_secure_password(16));
|
||||
|
||||
let db_env_path = std::env::current_dir().unwrap().join(".env");
|
||||
let db_password = self.generate_secure_password(32);
|
||||
let database_url = std::env::var("DATABASE_URL")
|
||||
.unwrap_or_else(|_| format!("postgres://gbuser:{}@localhost:5432/botserver", db_password));
|
||||
let db_line = format!("DATABASE_URL={}\n", database_url);
|
||||
|
||||
|
||||
let _ = std::fs::write(&db_env_path, db_line);
|
||||
|
||||
self.components.insert(
|
||||
"tables".to_string(),
|
||||
|
|
@ -848,10 +797,4 @@ impl PackageManager {
|
|||
.collect()
|
||||
}
|
||||
|
||||
fn encrypt_password(&self, password: &str, key: &str) -> String {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(key.as_bytes());
|
||||
hasher.update(password.as_bytes());
|
||||
format!("{:x}", hasher.finalize())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,31 +4,30 @@ use crate::shared::state::AppState;
|
|||
use actix_web::{web, HttpResponse, Result};
|
||||
use chrono::Utc;
|
||||
use diesel::prelude::*;
|
||||
use diesel::r2d2::{ConnectionManager, PooledConnection};
|
||||
use diesel::PgConnection;
|
||||
use log::{debug, error, info, warn};
|
||||
use log::trace;
|
||||
use log::{error, warn};
|
||||
use redis::Client;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::error::Error;
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct SessionData {
|
||||
pub id: Uuid,
|
||||
pub user_id: Option<Uuid>,
|
||||
pub data: String,
|
||||
}
|
||||
|
||||
pub struct SessionManager {
|
||||
conn: PgConnection,
|
||||
conn: PooledConnection<ConnectionManager<PgConnection>>,
|
||||
sessions: HashMap<Uuid, SessionData>,
|
||||
waiting_for_input: HashSet<Uuid>,
|
||||
redis: Option<Arc<Client>>,
|
||||
}
|
||||
|
||||
impl SessionManager {
|
||||
pub fn new(conn: PgConnection, redis_client: Option<Arc<Client>>) -> Self {
|
||||
pub fn new(conn: PooledConnection<ConnectionManager<PgConnection>>, redis_client: Option<Arc<Client>>) -> Self {
|
||||
SessionManager {
|
||||
conn,
|
||||
sessions: HashMap::new(),
|
||||
|
|
@ -36,13 +35,12 @@ impl SessionManager {
|
|||
redis: redis_client,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn provide_input(
|
||||
&mut self,
|
||||
session_id: Uuid,
|
||||
input: String,
|
||||
) -> Result<Option<String>, Box<dyn Error + Send + Sync>> {
|
||||
info!(
|
||||
trace!(
|
||||
"SessionManager.provide_input called for session {}",
|
||||
session_id
|
||||
);
|
||||
|
|
@ -61,11 +59,9 @@ impl SessionManager {
|
|||
Ok(Some("user_input".to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mark_waiting(&mut self, session_id: Uuid) {
|
||||
self.waiting_for_input.insert(session_id);
|
||||
}
|
||||
|
||||
pub fn get_session_by_id(
|
||||
&mut self,
|
||||
session_id: Uuid,
|
||||
|
|
@ -77,7 +73,6 @@ impl SessionManager {
|
|||
.optional()?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn get_user_session(
|
||||
&mut self,
|
||||
uid: Uuid,
|
||||
|
|
@ -92,7 +87,6 @@ impl SessionManager {
|
|||
.optional()?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn get_or_create_user_session(
|
||||
&mut self,
|
||||
uid: Uuid,
|
||||
|
|
@ -104,24 +98,19 @@ impl SessionManager {
|
|||
}
|
||||
self.create_session(uid, bid, session_title).map(Some)
|
||||
}
|
||||
|
||||
pub fn get_or_create_anonymous_user(
|
||||
&mut self,
|
||||
uid: Option<Uuid>,
|
||||
) -> Result<Uuid, Box<dyn Error + Send + Sync>> {
|
||||
use crate::shared::models::users::dsl as users_dsl;
|
||||
|
||||
let user_id = uid.unwrap_or_else(Uuid::new_v4);
|
||||
|
||||
let user_exists: Option<Uuid> = users_dsl::users
|
||||
.filter(users_dsl::id.eq(user_id))
|
||||
.select(users_dsl::id)
|
||||
.first(&mut self.conn)
|
||||
.optional()?;
|
||||
|
||||
if user_exists.is_none() {
|
||||
let now = Utc::now();
|
||||
info!("Creating anonymous user with ID {}", user_id);
|
||||
diesel::insert_into(users_dsl::users)
|
||||
.values((
|
||||
users_dsl::id.eq(user_id),
|
||||
|
|
@ -137,10 +126,8 @@ impl SessionManager {
|
|||
))
|
||||
.execute(&mut self.conn)?;
|
||||
}
|
||||
|
||||
Ok(user_id)
|
||||
}
|
||||
|
||||
pub fn create_session(
|
||||
&mut self,
|
||||
uid: Uuid,
|
||||
|
|
@ -148,11 +135,8 @@ impl SessionManager {
|
|||
session_title: &str,
|
||||
) -> Result<UserSession, Box<dyn Error + Send + Sync>> {
|
||||
use crate::shared::models::user_sessions::dsl::*;
|
||||
|
||||
// Ensure user exists (create anonymous if needed)
|
||||
let verified_uid = self.get_or_create_anonymous_user(Some(uid))?;
|
||||
let now = Utc::now();
|
||||
|
||||
let inserted: UserSession = diesel::insert_into(user_sessions)
|
||||
.values((
|
||||
id.eq(Uuid::new_v4()),
|
||||
|
|
@ -170,18 +154,14 @@ impl SessionManager {
|
|||
error!("Failed to create session in database: {}", e);
|
||||
e
|
||||
})?;
|
||||
|
||||
Ok(inserted)
|
||||
}
|
||||
|
||||
fn _clear_messages(&mut self, _session_id: Uuid) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
use crate::shared::models::message_history::dsl::*;
|
||||
|
||||
diesel::delete(message_history.filter(session_id.eq(session_id)))
|
||||
.execute(&mut self.conn)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn save_message(
|
||||
&mut self,
|
||||
sess_id: Uuid,
|
||||
|
|
@ -191,8 +171,6 @@ impl SessionManager {
|
|||
msg_type: i32,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
use crate::shared::models::message_history::dsl::*;
|
||||
|
||||
// Check if this exact message already exists
|
||||
let exists = message_history
|
||||
.filter(session_id.eq(sess_id))
|
||||
.filter(user_id.eq(uid))
|
||||
|
|
@ -202,18 +180,14 @@ impl SessionManager {
|
|||
.select(id)
|
||||
.first::<Uuid>(&mut self.conn)
|
||||
.optional()?;
|
||||
|
||||
if exists.is_some() {
|
||||
debug!("Duplicate message detected, skipping save");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let next_index = message_history
|
||||
.filter(session_id.eq(sess_id))
|
||||
.count()
|
||||
.get_result::<i64>(&mut self.conn)
|
||||
.unwrap_or(0);
|
||||
|
||||
diesel::insert_into(message_history)
|
||||
.values((
|
||||
id.eq(Uuid::new_v4()),
|
||||
|
|
@ -226,14 +200,13 @@ impl SessionManager {
|
|||
created_at.eq(chrono::Utc::now()),
|
||||
))
|
||||
.execute(&mut self.conn)?;
|
||||
|
||||
debug!(
|
||||
trace!(
|
||||
"Message saved for session {} with index {}",
|
||||
sess_id, next_index
|
||||
sess_id,
|
||||
next_index
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn update_session_context(
|
||||
&mut self,
|
||||
session_id: &Uuid,
|
||||
|
|
@ -241,25 +214,21 @@ impl SessionManager {
|
|||
context_data: String,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
use redis::Commands;
|
||||
|
||||
let redis_key = format!("context:{}:{}", user_id, session_id);
|
||||
if let Some(redis_client) = &self.redis {
|
||||
let mut conn = redis_client.get_connection()?;
|
||||
conn.set::<_, _, ()>(&redis_key, &context_data)?;
|
||||
info!("Updated context in Redis for key {}", redis_key);
|
||||
} else {
|
||||
warn!("No Redis client configured, context not persisted");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_session_context_data(
|
||||
&self,
|
||||
session_id: &Uuid,
|
||||
user_id: &Uuid,
|
||||
) -> Result<String, Box<dyn Error + Send + Sync>> {
|
||||
use redis::Commands;
|
||||
|
||||
let base_key = format!("context:{}:{}", user_id, session_id);
|
||||
if let Some(redis_client) = &self.redis {
|
||||
let conn_option = redis_client
|
||||
|
|
@ -269,17 +238,14 @@ impl SessionManager {
|
|||
e
|
||||
})
|
||||
.ok();
|
||||
|
||||
if let Some(mut connection) = conn_option {
|
||||
// First cache trip: get context name
|
||||
match connection.get::<_, Option<String>>(&base_key) {
|
||||
Ok(Some(context_name)) => {
|
||||
debug!("Found context name '{}' for key {}", context_name, base_key);
|
||||
// Second cache trip: get actual context value
|
||||
let full_key = format!("context:{}:{}:{}", user_id, session_id, context_name);
|
||||
let full_key =
|
||||
format!("context:{}:{}:{}", user_id, session_id, context_name);
|
||||
match connection.get::<_, Option<String>>(&full_key) {
|
||||
Ok(Some(context_value)) => {
|
||||
debug!(
|
||||
trace!(
|
||||
"Retrieved context value from Cache for key {}: {} chars",
|
||||
full_key,
|
||||
context_value.len()
|
||||
|
|
@ -287,7 +253,7 @@ impl SessionManager {
|
|||
return Ok(context_value);
|
||||
}
|
||||
Ok(None) => {
|
||||
debug!("No context value found for key {}", full_key);
|
||||
trace!("No context value found for key: {}", full_key);
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to retrieve context value from Cache: {}", e);
|
||||
|
|
@ -295,7 +261,7 @@ impl SessionManager {
|
|||
}
|
||||
}
|
||||
Ok(None) => {
|
||||
debug!("No context name found for key {}", base_key);
|
||||
trace!("No context name found for key: {}", base_key);
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to retrieve context name from Cache: {}", e);
|
||||
|
|
@ -303,25 +269,19 @@ impl SessionManager {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(String::new())
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub fn get_conversation_history(
|
||||
&mut self,
|
||||
sess_id: Uuid,
|
||||
_uid: Uuid,
|
||||
) -> Result<Vec<(String, String)>, Box<dyn Error + Send + Sync>> {
|
||||
use crate::shared::models::message_history::dsl::*;
|
||||
|
||||
let messages = message_history
|
||||
.filter(session_id.eq(sess_id))
|
||||
.order(message_index.asc())
|
||||
.select((role, content_encrypted))
|
||||
.load::<(i32, String)>(&mut self.conn)?;
|
||||
|
||||
let mut history: Vec<(String, String)> = Vec::new();
|
||||
for (other_role, content) in messages {
|
||||
let role_str = match other_role {
|
||||
|
|
@ -334,13 +294,11 @@ impl SessionManager {
|
|||
}
|
||||
Ok(history)
|
||||
}
|
||||
|
||||
pub fn get_user_sessions(
|
||||
&mut self,
|
||||
uid: Uuid,
|
||||
) -> Result<Vec<UserSession>, Box<dyn Error + Send + Sync>> {
|
||||
use crate::shared::models::user_sessions::dsl::*;
|
||||
|
||||
let sessions = if uid == Uuid::nil() {
|
||||
user_sessions
|
||||
.order(created_at.desc())
|
||||
|
|
@ -351,42 +309,33 @@ impl SessionManager {
|
|||
.order(created_at.desc())
|
||||
.load::<UserSession>(&mut self.conn)?
|
||||
};
|
||||
|
||||
Ok(sessions)
|
||||
}
|
||||
|
||||
|
||||
pub fn update_user_id(
|
||||
&mut self,
|
||||
session_id: Uuid,
|
||||
new_user_id: Uuid,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
use crate::shared::models::user_sessions::dsl::*;
|
||||
|
||||
let updated_count = diesel::update(user_sessions.filter(id.eq(session_id)))
|
||||
.set((user_id.eq(new_user_id), updated_at.eq(chrono::Utc::now())))
|
||||
.execute(&mut self.conn)?;
|
||||
|
||||
if updated_count == 0 {
|
||||
warn!("No session found with ID: {}", session_id);
|
||||
} else {
|
||||
debug!("Updated user ID for session {}", session_id);
|
||||
trace!("Updated user ID for session: {}", session_id);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_web::post("/api/sessions")]
|
||||
async fn create_session(data: web::Data<AppState>) -> Result<HttpResponse> {
|
||||
let user_id = Uuid::parse_str("00000000-0000-0000-0000-000000000001").unwrap();
|
||||
let bot_id = Uuid::nil();
|
||||
|
||||
// Acquire lock briefly, then release before performing blocking DB operations
|
||||
let session_result = {
|
||||
let mut sm = data.session_manager.lock().await;
|
||||
sm.get_or_create_user_session(user_id, bot_id, "New Conversation")
|
||||
};
|
||||
|
||||
let session = match session_result {
|
||||
Ok(Some(s)) => s,
|
||||
Ok(None) => {
|
||||
|
|
@ -400,14 +349,12 @@ async fn create_session(data: web::Data<AppState>) -> Result<HttpResponse> {
|
|||
.json(serde_json::json!({"error": e.to_string()})));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(HttpResponse::Ok().json(serde_json::json!({
|
||||
"session_id": session.id,
|
||||
"title": "New Conversation",
|
||||
"created_at": Utc::now()
|
||||
})))
|
||||
}
|
||||
|
||||
#[actix_web::get("/api/sessions")]
|
||||
async fn get_sessions(data: web::Data<AppState>) -> Result<HttpResponse> {
|
||||
let user_id = Uuid::parse_str("00000000-0000-0000-0000-000000000001").unwrap();
|
||||
|
|
@ -421,14 +368,9 @@ async fn get_sessions(data: web::Data<AppState>) -> Result<HttpResponse> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_web::post("/api/sessions/{session_id}/start")]
|
||||
async fn start_session(
|
||||
data: web::Data<AppState>,
|
||||
path: web::Path<String>,
|
||||
) -> Result<HttpResponse> {
|
||||
async fn start_session(data: web::Data<AppState>, path: web::Path<String>) -> Result<HttpResponse> {
|
||||
let session_id = path.into_inner();
|
||||
|
||||
match Uuid::parse_str(&session_id) {
|
||||
Ok(session_uuid) => {
|
||||
let mut session_manager = data.session_manager.lock().await;
|
||||
|
|
@ -440,11 +382,9 @@ async fn start_session(
|
|||
"session_id": session_id
|
||||
})))
|
||||
}
|
||||
Ok(None) => {
|
||||
Ok(HttpResponse::NotFound().json(serde_json::json!({
|
||||
Ok(None) => Ok(HttpResponse::NotFound().json(serde_json::json!({
|
||||
"error": "Session not found"
|
||||
})))
|
||||
}
|
||||
}))),
|
||||
Err(e) => {
|
||||
error!("Failed to start session {}: {}", session_id, e);
|
||||
Ok(HttpResponse::InternalServerError()
|
||||
|
|
@ -458,7 +398,6 @@ async fn start_session(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_web::get("/api/sessions/{session_id}")]
|
||||
async fn get_session_history(
|
||||
data: web::Data<AppState>,
|
||||
|
|
@ -466,7 +405,6 @@ async fn get_session_history(
|
|||
) -> Result<HttpResponse> {
|
||||
let session_id = path.into_inner();
|
||||
let user_id = Uuid::parse_str("00000000-0000-0000-0000-000000000001").unwrap();
|
||||
|
||||
match Uuid::parse_str(&session_id) {
|
||||
Ok(session_uuid) => {
|
||||
let orchestrator = BotOrchestrator::new(Arc::new(data.get_ref().clone()));
|
||||
|
|
@ -475,7 +413,7 @@ async fn get_session_history(
|
|||
.await
|
||||
{
|
||||
Ok(history) => {
|
||||
info!(
|
||||
trace!(
|
||||
"Retrieved {} history entries for session {}",
|
||||
history.len(),
|
||||
session_id
|
||||
|
|
|
|||
|
|
@ -2,38 +2,35 @@ use crate::channels::{ChannelAdapter, VoiceAdapter, WebChannelAdapter};
|
|||
use crate::config::AppConfig;
|
||||
use crate::llm::LLMProvider;
|
||||
use crate::session::SessionManager;
|
||||
use diesel::{ PgConnection};
|
||||
use aws_sdk_s3::Client as S3Client;
|
||||
use redis::Client as RedisClient;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use tokio::sync::mpsc;
|
||||
use crate::shared::models::BotResponse;
|
||||
use crate::auth::AuthService;
|
||||
use crate::shared::utils::DbPool;
|
||||
pub struct AppState {
|
||||
pub drive: Option<S3Client>,
|
||||
pub cache: Option<Arc<RedisClient>>,
|
||||
pub bucket_name: String,
|
||||
pub config: Option<AppConfig>,
|
||||
pub conn: Arc<Mutex<PgConnection>>,
|
||||
pub conn: DbPool,
|
||||
pub session_manager: Arc<tokio::sync::Mutex<SessionManager>>,
|
||||
pub llm_provider: Arc<dyn LLMProvider>,
|
||||
pub auth_service: Arc<tokio::sync::Mutex<AuthService>>,
|
||||
pub channels: Arc<Mutex<HashMap<String, Arc<dyn ChannelAdapter>>>>,
|
||||
pub channels: Arc<tokio::sync::Mutex<HashMap<String, Arc<dyn ChannelAdapter>>>>,
|
||||
pub response_channels: Arc<tokio::sync::Mutex<HashMap<String, mpsc::Sender<BotResponse>>>>,
|
||||
pub web_adapter: Arc<WebChannelAdapter>,
|
||||
pub voice_adapter: Arc<VoiceAdapter>,
|
||||
}
|
||||
|
||||
impl Clone for AppState {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
drive: self.drive.clone(),
|
||||
bucket_name: self.bucket_name.clone(),
|
||||
config: self.config.clone(),
|
||||
conn: Arc::clone(&self.conn),
|
||||
|
||||
conn: self.conn.clone(),
|
||||
cache: self.cache.clone(),
|
||||
session_manager: Arc::clone(&self.session_manager),
|
||||
llm_provider: Arc::clone(&self.llm_provider),
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
use anyhow::{Context, Result};
|
||||
use diesel::{Connection, PgConnection};
|
||||
use diesel::Connection;
|
||||
use diesel::{
|
||||
r2d2::{ConnectionManager, Pool},
|
||||
PgConnection,
|
||||
};
|
||||
use futures_util::StreamExt;
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use log::trace;
|
||||
use reqwest::Client;
|
||||
use rhai::{Array, Dynamic};
|
||||
use serde_json::Value;
|
||||
|
|
@ -10,7 +13,6 @@ use smartstring::SmartString;
|
|||
use std::error::Error;
|
||||
use tokio::fs::File as TokioFile;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
pub fn json_value_to_dynamic(value: &Value) -> Dynamic {
|
||||
match value {
|
||||
Value::Null => Dynamic::UNIT,
|
||||
|
|
@ -37,7 +39,6 @@ pub fn json_value_to_dynamic(value: &Value) -> Dynamic {
|
|||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_array(value: Dynamic) -> Array {
|
||||
if value.is_array() {
|
||||
value.cast::<Array>()
|
||||
|
|
@ -47,20 +48,14 @@ pub fn to_array(value: Dynamic) -> Array {
|
|||
Array::from([value])
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn download_file(
|
||||
url: &str,
|
||||
output_path: &str,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
pub async fn download_file(url: &str, output_path: &str) -> Result<(), anyhow::Error> {
|
||||
let url = url.to_string();
|
||||
let output_path = output_path.to_string();
|
||||
|
||||
let download_handle = tokio::spawn(async move {
|
||||
let client = Client::builder()
|
||||
.user_agent("Mozilla/5.0 (compatible; BotServer/1.0)")
|
||||
.build()?;
|
||||
let response = client.get(&url).send().await?;
|
||||
|
||||
if response.status().is_success() {
|
||||
let total_size = response.content_length().unwrap_or(0);
|
||||
let pb = ProgressBar::new(total_size);
|
||||
|
|
@ -69,58 +64,51 @@ pub async fn download_file(
|
|||
.unwrap()
|
||||
.progress_chars("#>-"));
|
||||
pb.set_message(format!("Downloading {}", url));
|
||||
|
||||
let mut file = TokioFile::create(&output_path).await?;
|
||||
let mut downloaded: u64 = 0;
|
||||
let mut stream = response.bytes_stream();
|
||||
|
||||
while let Some(chunk_result) = stream.next().await {
|
||||
let chunk = chunk_result?;
|
||||
file.write_all(&chunk).await?;
|
||||
downloaded += chunk.len() as u64;
|
||||
pb.set_position(downloaded);
|
||||
}
|
||||
|
||||
pb.finish_with_message(format!("Downloaded {}", output_path));
|
||||
trace!("Download completed: {} -> {}", url, output_path);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow::anyhow!("HTTP {}: {}", response.status(), url))
|
||||
}
|
||||
});
|
||||
|
||||
download_handle.await?
|
||||
}
|
||||
|
||||
pub fn parse_filter(filter_str: &str) -> Result<(String, Vec<String>), Box<dyn Error>> {
|
||||
let parts: Vec<&str> = filter_str.split('=').collect();
|
||||
if parts.len() != 2 {
|
||||
return Err("Invalid filter format. Expected 'KEY=VALUE'".into());
|
||||
}
|
||||
|
||||
let column = parts[0].trim();
|
||||
let value = parts[1].trim();
|
||||
|
||||
if !column
|
||||
.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || c == '_')
|
||||
{
|
||||
return Err("Invalid column name in filter".into());
|
||||
}
|
||||
|
||||
Ok((format!("{} = $1", column), vec![value.to_string()]))
|
||||
}
|
||||
|
||||
pub fn estimate_token_count(text: &str) -> usize {
|
||||
let char_count = text.chars().count();
|
||||
(char_count / 4).max(1) // Ensure at least 1 token
|
||||
(char_count / 4).max(1)
|
||||
}
|
||||
|
||||
pub fn establish_pg_connection() -> Result<PgConnection> {
|
||||
let database_url = std::env::var("DATABASE_URL")
|
||||
.unwrap_or_else(|_| "postgres://gbuser:@localhost:5432/botserver".to_string());
|
||||
|
||||
let database_url = std::env::var("DATABASE_URL").unwrap();
|
||||
PgConnection::establish(&database_url)
|
||||
.with_context(|| format!("Failed to connect to database at {}", database_url))
|
||||
}
|
||||
|
||||
pub type DbPool = Pool<ConnectionManager<PgConnection>>;
|
||||
pub fn create_conn() -> Result<DbPool, r2d2::Error> {
|
||||
let database_url = std::env::var("DATABASE_URL")
|
||||
.unwrap_or_else(|_| "postgres://gbuser:@localhost:5432/botserver".to_string());
|
||||
let manager = ConnectionManager::<PgConnection>::new(database_url);
|
||||
Pool::builder().build(manager)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ use tokio::sync::mpsc;
|
|||
use uuid::Uuid;
|
||||
|
||||
pub struct ChatPanel {
|
||||
pub id: Uuid,
|
||||
pub app_state: Arc<AppState>,
|
||||
pub messages: Vec<String>,
|
||||
pub input_buffer: String,
|
||||
pub session_id: Uuid,
|
||||
|
|
@ -16,10 +14,8 @@ pub struct ChatPanel {
|
|||
}
|
||||
|
||||
impl ChatPanel {
|
||||
pub fn new(app_state: Arc<AppState>) -> Self {
|
||||
pub fn new(_app_state: Arc<AppState>) -> Self {
|
||||
Self {
|
||||
id: Uuid::new_v4(),
|
||||
app_state,
|
||||
messages: vec!["Welcome to General Bots Console Chat!".to_string()],
|
||||
input_buffer: String::new(),
|
||||
session_id: Uuid::new_v4(),
|
||||
|
|
@ -95,7 +91,7 @@ let _ = orchestrator.stream_response(user_message, tx).await;
|
|||
use crate::shared::models::schema::bots::dsl::*;
|
||||
use diesel::prelude::*;
|
||||
|
||||
let mut conn = app_state.conn.lock().unwrap();
|
||||
let mut conn = app_state.conn.get().unwrap();
|
||||
let bot_id = bots
|
||||
.filter(name.eq(bot_name))
|
||||
.select(id)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
use std::sync::Arc;
|
||||
use crate::shared::state::AppState;
|
||||
use crate::shared::models::schema::bots::dsl::*;
|
||||
use crate::nvidia;
|
||||
use crate::config::ConfigManager;
|
||||
use crate::nvidia;
|
||||
use crate::shared::models::schema::bots::dsl::*;
|
||||
use crate::shared::state::AppState;
|
||||
use diesel::prelude::*;
|
||||
use std::sync::Arc;
|
||||
use sysinfo::System;
|
||||
|
||||
pub struct StatusPanel {
|
||||
|
|
@ -51,7 +51,10 @@ impl StatusPanel {
|
|||
};
|
||||
|
||||
let cpu_bar = Self::create_progress_bar(system_metrics.cpu_usage, 20);
|
||||
lines.push(format!(" CPU: {:5.1}% {}", system_metrics.cpu_usage, cpu_bar));
|
||||
lines.push(format!(
|
||||
" CPU: {:5.1}% {}",
|
||||
system_metrics.cpu_usage, cpu_bar
|
||||
));
|
||||
|
||||
if let Some(gpu_usage) = system_metrics.gpu_usage {
|
||||
let gpu_bar = Self::create_progress_bar(gpu_usage, 20);
|
||||
|
|
@ -64,7 +67,10 @@ impl StatusPanel {
|
|||
let used_mem = self.system.used_memory() as f32 / 1024.0 / 1024.0 / 1024.0;
|
||||
let mem_percentage = (used_mem / total_mem) * 100.0;
|
||||
let mem_bar = Self::create_progress_bar(mem_percentage, 20);
|
||||
lines.push(format!(" MEM: {:5.1}% {} ({:.1}/{:.1} GB)", mem_percentage, mem_bar, used_mem, total_mem));
|
||||
lines.push(format!(
|
||||
" MEM: {:5.1}% {} ({:.1}/{:.1} GB)",
|
||||
mem_percentage, mem_bar, used_mem, total_mem
|
||||
));
|
||||
|
||||
lines.push("".to_string());
|
||||
lines.push("╔═══════════════════════════════════════╗".to_string());
|
||||
|
|
@ -94,7 +100,7 @@ impl StatusPanel {
|
|||
lines.push("╚═══════════════════════════════════════╝".to_string());
|
||||
lines.push("".to_string());
|
||||
|
||||
if let Ok(mut conn) = self.app_state.conn.try_lock() {
|
||||
if let Ok(mut conn) = self.app_state.conn.get() {
|
||||
match bots
|
||||
.filter(is_active.eq(true))
|
||||
.select((name, id))
|
||||
|
|
@ -106,7 +112,11 @@ impl StatusPanel {
|
|||
} else {
|
||||
for (bot_name, bot_id) in bot_list {
|
||||
let marker = if let Some(ref selected) = selected_bot {
|
||||
if selected == &bot_name { "►" } else { " " }
|
||||
if selected == &bot_name {
|
||||
"►"
|
||||
} else {
|
||||
" "
|
||||
}
|
||||
} else {
|
||||
" "
|
||||
};
|
||||
|
|
@ -117,17 +127,21 @@ impl StatusPanel {
|
|||
lines.push("".to_string());
|
||||
lines.push(" ┌─ Bot Configuration ─────────┐".to_string());
|
||||
|
||||
let config_manager = ConfigManager::new(self.app_state.conn.clone());
|
||||
let config_manager =
|
||||
ConfigManager::new(self.app_state.conn.clone());
|
||||
|
||||
let llm_model = config_manager.get_config(&bot_id, "llm-model", None)
|
||||
let llm_model = config_manager
|
||||
.get_config(&bot_id, "llm-model", None)
|
||||
.unwrap_or_else(|_| "N/A".to_string());
|
||||
lines.push(format!(" Model: {}", llm_model));
|
||||
|
||||
let ctx_size = config_manager.get_config(&bot_id, "llm-server-ctx-size", None)
|
||||
let ctx_size = config_manager
|
||||
.get_config(&bot_id, "llm-server-ctx-size", None)
|
||||
.unwrap_or_else(|_| "N/A".to_string());
|
||||
lines.push(format!(" Context: {}", ctx_size));
|
||||
|
||||
let temp = config_manager.get_config(&bot_id, "llm-temperature", None)
|
||||
let temp = config_manager
|
||||
.get_config(&bot_id, "llm-temperature", None)
|
||||
.unwrap_or_else(|_| "N/A".to_string());
|
||||
lines.push(format!(" Temp: {}", temp));
|
||||
|
||||
|
|
@ -150,7 +164,10 @@ impl StatusPanel {
|
|||
lines.push("║ SESSIONS ║".to_string());
|
||||
lines.push("╚═══════════════════════════════════════╝".to_string());
|
||||
|
||||
let session_count = self.app_state.response_channels.try_lock()
|
||||
let session_count = self
|
||||
.app_state
|
||||
.response_channels
|
||||
.try_lock()
|
||||
.map(|channels| channels.len())
|
||||
.unwrap_or(0);
|
||||
lines.push(format!(" Active Sessions: {}", session_count));
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
// Simple test script for LLM keyword
|
||||
let result = LLM "Hello world";
|
||||
result
|
||||
Loading…
Add table
Reference in a new issue