Revert "Implement token-based context usage in chat UI"
This reverts commit 82aa3e8d36.
This commit is contained in:
parent
82aa3e8d36
commit
a0c367d79b
4 changed files with 838 additions and 1162 deletions
6
package-lock.json
generated
6
package-lock.json
generated
|
|
@ -1,6 +0,0 @@
|
||||||
{
|
|
||||||
"name": "botserver",
|
|
||||||
"lockfileVersion": 3,
|
|
||||||
"requires": true,
|
|
||||||
"packages": {}
|
|
||||||
}
|
|
||||||
236
src/main.rs
236
src/main.rs
|
|
@ -56,93 +56,7 @@ use crate::web_server::{bot_index, index, static_files};
|
||||||
use crate::whatsapp::whatsapp_webhook_verify;
|
use crate::whatsapp::whatsapp_webhook_verify;
|
||||||
use crate::whatsapp::WhatsAppAdapter;
|
use crate::whatsapp::WhatsAppAdapter;
|
||||||
|
|
||||||
if args.len() > 1 {
|
|
||||||
let command = &args[1];
|
|
||||||
match command.as_str() {
|
|
||||||
async fn main() -> std::io::Result<()> {
|
|
||||||
trace!("Application starting");
|
|
||||||
let args: Vec<String> = std::env::args().collect();
|
|
||||||
trace!("Command line arguments: {:?}", args);
|
|
||||||
|
|
||||||
if args.len() > 1 {
|
|
||||||
let command = &args[1];
|
|
||||||
trace!("Processing command: {}", command);
|
|
||||||
match command.as_str() {
|
|
||||||
let args: Vec<String> = std::env::args().collect();
|
|
||||||
|
|
||||||
if args.len() > 1 {
|
|
||||||
let command = &args[1];
|
|
||||||
match command.as_str() {
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
|
||||||
trace!("Application starting");
|
|
||||||
let args: Vec<String> = std::env::args().collect();
|
|
||||||
trace!("Command line arguments: {:?}", args);
|
|
||||||
|
|
||||||
if args.len() > 1 {
|
|
||||||
let command = &args[1];
|
|
||||||
trace!("Processing command: {}", command);
|
|
||||||
match command.as_str() {
|
|
||||||
"install" | "remove" | "list" | "status" | "start" | "stop" | "restart" | "--help" | "-h" => {
|
|
||||||
match package_manager::cli::run().expect("Failed to initialize Drive");
|
|
||||||
let drive = init_drive(&config.minio)
|
|
||||||
.await
|
|
||||||
.expect("Failed to initialize Drive");
|
|
||||||
trace!("MinIO drive initialized successfully");
|
|
||||||
.await
|
|
||||||
.expect("Failed to initialize Drive");
|
|
||||||
let drive = init_drive(&config.minio)
|
|
||||||
.await
|
|
||||||
.expect("Failed to initialize Drive");
|
|
||||||
trace!("MinIO drive initialized successfully"); {
|
|
||||||
Ok(_) => return Ok(()),
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("CLI error: {}", e);
|
|
||||||
return Err(std::io::Error::new(
|
|
||||||
std::io::ErrorKind::Other,
|
|
||||||
format!("CLI command failed: {}", e),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
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),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if args.len() > 1 {
|
|
||||||
let command = &args[1];
|
|
||||||
match command.as_str() {
|
|
||||||
async fn main() -> std::io::Result<()> {
|
|
||||||
trace!("Application starting");
|
|
||||||
let args: Vec<String> = std::env::args().collect();
|
|
||||||
trace!("Command line arguments: {:?}", args);
|
|
||||||
|
|
||||||
if args.len() > 1 {
|
|
||||||
let command = &args[1];
|
|
||||||
trace!("Processing command: {}", command);
|
|
||||||
match command.as_str() {
|
|
||||||
let args: Vec<String> = std::env::args().collect();
|
|
||||||
|
|
||||||
if args.len() > 1 {
|
|
||||||
let command = &args[1];
|
|
||||||
match command.as_str() {
|
|
||||||
#[tokio::main]
|
|
||||||
async fn main() -> std::io::Result<()> {
|
|
||||||
trace!("Starting main function");
|
|
||||||
let args: Vec<String> = std::env::args().collect();
|
|
||||||
trace!("Command line arguments: {:?}", args);
|
|
||||||
|
|
||||||
if args.len() > 1 {
|
|
||||||
let command = &args[1];
|
|
||||||
trace!("Processing command: {}", command);
|
|
||||||
match command.as_str() {
|
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
let args: Vec<String> = std::env::args().collect();
|
let args: Vec<String> = std::env::args().collect();
|
||||||
|
|
||||||
|
|
@ -171,60 +85,12 @@ async fn main() -> std::io::Result<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("Starting BotServer bootstrap process");
|
dotenv().ok();
|
||||||
dotenv().ok();
|
|
||||||
env_logger::Builder::from_env(env_logger::Env::default_filter_or("info")).init();
|
|
||||||
trace!("Environment variables loaded and logger initialized");
|
|
||||||
|
|
||||||
info!("Starting BotServer bootstrap process");
|
|
||||||
trace!("Initializing bootstrap manager");
|
|
||||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
|
||||||
|
|
||||||
info!("Starting BotServer bootstrap process");
|
|
||||||
dotenv().ok();
|
|
||||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
|
||||||
trace!("Environment variables loaded and logger initialized");
|
|
||||||
|
|
||||||
info!("Starting BotServer bootstrap process");
|
|
||||||
trace!("Initializing bootstrap manager");
|
|
||||||
|
|
||||||
info!("Starting BotServer bootstrap process");
|
|
||||||
dotenv().ok();
|
|
||||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
|
||||||
trace!("Environment variables loaded and logger initialized");
|
|
||||||
|
|
||||||
info!("Starting BotServer bootstrap process");
|
|
||||||
trace!("Initializing bootstrap manager");
|
|
||||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
||||||
|
|
||||||
info!("Starting BotServer bootstrap process");
|
info!("Starting BotServer bootstrap process");
|
||||||
|
|
||||||
InstallMode::Container
|
let install_mode = if args.contains(&"--container".to_string()) {
|
||||||
} else {
|
|
||||||
InstallMode::Local
|
|
||||||
};
|
|
||||||
|
|
||||||
let tenant = if let Some(idx) = args.iter().position(|a| a == "--tenant") {
|
|
||||||
args.get(idx + 1).cloned()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
let install_mode = if args.contains(&"--container".to_string()) {
|
|
||||||
trace!("Running in container mode");
|
|
||||||
InstallMode::Container
|
|
||||||
} else {
|
|
||||||
trace!("Running in local mode");
|
|
||||||
InstallMode::Local
|
|
||||||
};
|
|
||||||
|
|
||||||
let tenant = if let Some(idx) = args.iter().position(|a| a == "--tenant") {
|
|
||||||
let tenant = args.get(idx + 1).cloned();
|
|
||||||
trace!("Tenant specified: {:?}", tenant);
|
|
||||||
tenant
|
|
||||||
} else {
|
|
||||||
trace!("No tenant specified");
|
|
||||||
None
|
|
||||||
};
|
|
||||||
InstallMode::Container
|
InstallMode::Container
|
||||||
} else {
|
} else {
|
||||||
InstallMode::Local
|
InstallMode::Local
|
||||||
|
|
@ -237,28 +103,7 @@ None
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut bootstrap = BootstrapManager::new(install_mode.clone(), tenant.clone());
|
let mut bootstrap = BootstrapManager::new(install_mode.clone(), tenant.clone());
|
||||||
info!("Bootstrap completed successfully, configuration loaded from database");
|
let cfg = match bootstrap.bootstrap() {
|
||||||
config
|
|
||||||
let cfg = match bootstrap.bootstrap() {
|
|
||||||
Ok(config) => {
|
|
||||||
info!("Bootstrap completed successfully, configuration loaded from database");
|
|
||||||
trace!("Bootstrap config: {:?}", config);
|
|
||||||
config
|
|
||||||
Ok(config) => {
|
|
||||||
info!("Bootstrap completed successfully, configuration loaded from database");
|
|
||||||
config
|
|
||||||
let cfg = match bootstrap.bootstrap() {
|
|
||||||
Ok(config) => {
|
|
||||||
info!("Bootstrap completed successfully, configuration loaded from database");
|
|
||||||
trace!("Bootstrap config: {:?}", config);
|
|
||||||
config
|
|
||||||
info!("Bootstrap completed successfully, configuration loaded from database");
|
|
||||||
config
|
|
||||||
let cfg = match bootstrap.bootstrap() {
|
|
||||||
Ok(config) => {
|
|
||||||
info!("Bootstrap completed successfully, configuration loaded from database");
|
|
||||||
trace!("Bootstrap config: {:?}", config);
|
|
||||||
config
|
|
||||||
Ok(config) => {
|
Ok(config) => {
|
||||||
info!("Bootstrap completed successfully, configuration loaded from database");
|
info!("Bootstrap completed successfully, configuration loaded from database");
|
||||||
config
|
config
|
||||||
|
|
@ -286,30 +131,10 @@ config
|
||||||
log::warn!("Failed to upload templates to MinIO: {}", e);
|
log::warn!("Failed to upload templates to MinIO: {}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("Establishing database connection to {}", cfg.database_url());
|
let config = std::sync::Arc::new(cfg.clone());
|
||||||
let config = std::sync::Arc::new(cfg.clone());
|
|
||||||
trace!("Configuration loaded: {:?}", cfg);
|
|
||||||
|
|
||||||
info!("Establishing database connection to {}", cfg.database_url());
|
|
||||||
trace!("Database URL: {}", cfg.database_url());
|
|
||||||
|
|
||||||
info!("Establishing database connection to {}", cfg.database_url());
|
info!("Establishing database connection to {}", cfg.database_url());
|
||||||
let db_pool = match diesel::Connection::establish(&cfg.database_url()) {
|
let db_pool = match diesel::Connection::establish(&cfg.database_url()) {
|
||||||
Ok(conn) => {
|
|
||||||
trace!("Database connection established successfully");
|
|
||||||
Arc::new(Mutex::new(conn))
|
|
||||||
}
|
|
||||||
Ok(conn) => Arc::new(Mutex::new(conn)),
|
|
||||||
let db_pool = match diesel::Connection::establish(&cfg.database_url()) {
|
|
||||||
Ok(conn) => {
|
|
||||||
trace!("Database connection established successfully");
|
|
||||||
Arc::new(Mutex::new(conn))
|
|
||||||
}
|
|
||||||
let db_pool = match diesel::Connection::establish(&cfg.database_url()) {
|
|
||||||
Ok(conn) => {
|
|
||||||
trace!("Database connection established successfully");
|
|
||||||
Arc::new(Mutex::new(conn))
|
|
||||||
}
|
|
||||||
Ok(conn) => Arc::new(Mutex::new(conn)),
|
Ok(conn) => Arc::new(Mutex::new(conn)),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Failed to connect to main database: {}", e);
|
log::error!("Failed to connect to main database: {}", e);
|
||||||
|
|
@ -331,21 +156,6 @@ Arc::new(Mutex::new(conn))
|
||||||
.or_else(|_| std::env::var("REDIS_URL"))
|
.or_else(|_| std::env::var("REDIS_URL"))
|
||||||
.unwrap_or_else(|_| "redis://localhost:6379".to_string());
|
.unwrap_or_else(|_| "redis://localhost:6379".to_string());
|
||||||
let redis_client = match redis::Client::open(cache_url.as_str()) {
|
let redis_client = match redis::Client::open(cache_url.as_str()) {
|
||||||
Ok(client) => {
|
|
||||||
trace!("Redis client created successfully");
|
|
||||||
Some(Arc::new(client))
|
|
||||||
}
|
|
||||||
Ok(client) => Some(Arc::new(client)),
|
|
||||||
let redis_client = match redis::Client::open(cache_url.as_str()) {
|
|
||||||
Ok(client) => {
|
|
||||||
trace!("Redis client created successfully");
|
|
||||||
Some(Arc::new(client))
|
|
||||||
}
|
|
||||||
let redis_client = match redis::Client::open(cache_url.as_str()) {
|
|
||||||
Ok(client) => {
|
|
||||||
trace!("Redis client created successfully");
|
|
||||||
Some(Arc::new(client))
|
|
||||||
}
|
|
||||||
Ok(client) => Some(Arc::new(client)),
|
Ok(client) => Some(Arc::new(client)),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::warn!("Failed to connect to Redis: Redis URL did not parse- {}", e);
|
log::warn!("Failed to connect to Redis: Redis URL did not parse- {}", e);
|
||||||
|
|
@ -373,12 +183,7 @@ Some(Arc::new(client))
|
||||||
let tool_api = Arc::new(tools::ToolApi::new());
|
let tool_api = Arc::new(tools::ToolApi::new());
|
||||||
|
|
||||||
info!("Initializing MinIO drive at {}", cfg.minio.server);
|
info!("Initializing MinIO drive at {}", cfg.minio.server);
|
||||||
.await
|
let drive = init_drive(&config.minio)
|
||||||
.expect("Failed to initialize Drive");
|
|
||||||
let drive = init_drive(&config.minio)
|
|
||||||
.await
|
|
||||||
.expect("Failed to initialize Drive");
|
|
||||||
trace!("MinIO drive initialized successfully");
|
|
||||||
.await
|
.await
|
||||||
.expect("Failed to initialize Drive");
|
.expect("Failed to initialize Drive");
|
||||||
|
|
||||||
|
|
@ -421,30 +226,12 @@ trace!("MinIO drive initialized successfully");
|
||||||
config.server.host, config.server.port
|
config.server.host, config.server.port
|
||||||
);
|
);
|
||||||
|
|
||||||
.unwrap_or(4);
|
let worker_count = std::thread::available_parallelism()
|
||||||
let worker_count = std::thread::available_parallelism()
|
|
||||||
.map(|n| n.get())
|
|
||||||
.unwrap_or(4);
|
|
||||||
trace!("Configured worker threads: {}", worker_count);
|
|
||||||
.map(|n| n.get())
|
|
||||||
.unwrap_or(4);
|
|
||||||
let worker_count = std::thread::available_parallelism()
|
|
||||||
.map(|n| n.get())
|
|
||||||
.unwrap_or(4);
|
|
||||||
trace!("Configured worker threads: {}", worker_count);
|
|
||||||
.unwrap_or(4);
|
|
||||||
let worker_count = std::thread::available_parallelism()
|
|
||||||
.map(|n| n.get())
|
|
||||||
.unwrap_or(4);
|
|
||||||
trace!("Configured worker threads: {}", worker_count);
|
|
||||||
.map(|n| n.get())
|
.map(|n| n.get())
|
||||||
.unwrap_or(4);
|
.unwrap_or(4);
|
||||||
|
|
||||||
// Spawn AutomationService in a LocalSet on a separate thread
|
// Spawn AutomationService in a LocalSet on a separate thread
|
||||||
std::thread::spawn(move || {
|
let automation_state = app_state.clone();
|
||||||
let automation_state = app_state.clone();
|
|
||||||
trace!("Spawning automation service thread");
|
|
||||||
std::thread::spawn(move || {
|
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
let rt = tokio::runtime::Builder::new_current_thread()
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
.enable_all()
|
.enable_all()
|
||||||
|
|
@ -467,15 +254,6 @@ std::thread::spawn(move || {
|
||||||
let _drive_handle = drive_monitor.spawn();
|
let _drive_handle = drive_monitor.spawn();
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
trace!("Creating new HTTP server instance");
|
|
||||||
let cors = Cors::default()
|
|
||||||
let cors = Cors::default()
|
|
||||||
HttpServer::new(move || {
|
|
||||||
trace!("Creating new HTTP server instance");
|
|
||||||
let cors = Cors::default()
|
|
||||||
HttpServer::new(move || {
|
|
||||||
trace!("Creating new HTTP server instance");
|
|
||||||
let cors = Cors::default()
|
|
||||||
let cors = Cors::default()
|
let cors = Cors::default()
|
||||||
.allow_any_origin()
|
.allow_any_origin()
|
||||||
.allow_any_method()
|
.allow_any_method()
|
||||||
|
|
|
||||||
|
|
@ -1,65 +0,0 @@
|
||||||
pub database: DatabaseConfig,
|
|
||||||
pub drive: DriveConfig,
|
|
||||||
pub meet: MeetConfig,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct DatabaseConfig {
|
|
||||||
pub url: String,
|
|
||||||
pub max_connections: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct DriveConfig {
|
|
||||||
pub storage_path: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct MeetConfig {
|
|
||||||
pub api_key: String,
|
|
||||||
pub api_secret: String,
|
|
||||||
}
|
|
||||||
use serde::Deserialize;
|
|
||||||
use dotenvy::dotenv;
|
|
||||||
use std::env;
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
pub struct AppConfig {
|
|
||||||
pub database: DatabaseConfig,
|
|
||||||
pub drive: DriveConfig,
|
|
||||||
pub meet: MeetConfig,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
pub struct DatabaseConfig {
|
|
||||||
pub url: String,
|
|
||||||
pub max_connections: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
pub struct DriveConfig {
|
|
||||||
pub storage_path: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
pub struct MeetConfig {
|
|
||||||
pub api_key: String,
|
|
||||||
pub api_secret: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AppConfig {
|
|
||||||
pub fn load() -> anyhow::Result<Self> {
|
|
||||||
dotenv().ok();
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
database: DatabaseConfig {
|
|
||||||
url: env::var("DATABASE_URL")?,
|
|
||||||
max_connections: env::var("DATABASE_MAX_CONNECTIONS")?.parse()?,
|
|
||||||
},
|
|
||||||
drive: DriveConfig {
|
|
||||||
storage_path: env::var("DRIVE_STORAGE_PATH")?,
|
|
||||||
},
|
|
||||||
meet: MeetConfig {
|
|
||||||
api_key: env::var("MEET_API_KEY")?,
|
|
||||||
api_secret: env::var("MEET_API_SECRET")?,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -924,9 +924,7 @@
|
||||||
let reconnectTimeout = null;
|
let reconnectTimeout = null;
|
||||||
let thinkingTimeout = null;
|
let thinkingTimeout = null;
|
||||||
let lastMessageLength = 0;
|
let lastMessageLength = 0;
|
||||||
let totalTokens = 0;
|
let contextUsage = 0;
|
||||||
const MAX_TOKENS = 5000;
|
|
||||||
const MIN_DISPLAY_PERCENTAGE = 20;
|
|
||||||
let isUserScrolling = false;
|
let isUserScrolling = false;
|
||||||
let autoScrollEnabled = true;
|
let autoScrollEnabled = true;
|
||||||
|
|
||||||
|
|
@ -949,14 +947,7 @@
|
||||||
gfm: true,
|
gfm: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Token estimation function (roughly 4 characters per token)
|
|
||||||
function estimateTokens(text) {
|
|
||||||
return Math.ceil(text.length / 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
function toggleSidebar() {
|
function toggleSidebar() {
|
||||||
|
|
||||||
|
|
||||||
document.getElementById("sidebar").classList.toggle("open");
|
document.getElementById("sidebar").classList.toggle("open");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1025,10 +1016,10 @@
|
||||||
|
|
||||||
scrollToBottomBtn.addEventListener("click", scrollToBottom);
|
scrollToBottomBtn.addEventListener("click", scrollToBottom);
|
||||||
|
|
||||||
// Context usage management with token-based calculation
|
// Context usage management
|
||||||
function updateContextUsage(tokens) {
|
function updateContextUsage(usage) {
|
||||||
totalTokens = tokens;
|
contextUsage = usage;
|
||||||
const percentage = Math.min(100, Math.round((tokens / MAX_TOKENS) * 100));
|
const percentage = Math.min(100, Math.round(usage * 100));
|
||||||
|
|
||||||
contextPercentage.textContent = `${percentage}%`;
|
contextPercentage.textContent = `${percentage}%`;
|
||||||
contextProgressBar.style.width = `${percentage}%`;
|
contextProgressBar.style.width = `${percentage}%`;
|
||||||
|
|
@ -1044,21 +1035,14 @@
|
||||||
contextProgressBar.className = "context-progress-bar";
|
contextProgressBar.className = "context-progress-bar";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show indicator if usage is above minimum display percentage
|
// Show indicator if usage is above 50%
|
||||||
if (percentage >= MIN_DISPLAY_PERCENTAGE) {
|
if (percentage >= 50) {
|
||||||
contextIndicator.style.display = "block";
|
contextIndicator.style.display = "block";
|
||||||
} else {
|
} else {
|
||||||
contextIndicator.style.display = "none";
|
contextIndicator.style.display = "none";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add tokens to the total count
|
|
||||||
function addToTokenCount(text) {
|
|
||||||
const tokens = estimateTokens(text);
|
|
||||||
totalTokens += tokens;
|
|
||||||
updateContextUsage(totalTokens);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function initializeAuth() {
|
async function initializeAuth() {
|
||||||
try {
|
try {
|
||||||
updateConnectionStatus("connecting");
|
updateConnectionStatus("connecting");
|
||||||
|
|
@ -1119,8 +1103,7 @@
|
||||||
<p class="empty-subtitle">Seu assistente de IA avançado</p>
|
<p class="empty-subtitle">Seu assistente de IA avançado</p>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
// Reset token count for new session
|
// Reset context usage for new session
|
||||||
totalTokens = 0;
|
|
||||||
updateContextUsage(0);
|
updateContextUsage(0);
|
||||||
if (isVoiceMode) {
|
if (isVoiceMode) {
|
||||||
await startVoiceSession();
|
await startVoiceSession();
|
||||||
|
|
@ -1169,16 +1152,14 @@
|
||||||
<p class="empty-subtitle">Seu assistente de IA avançado</p>
|
<p class="empty-subtitle">Seu assistente de IA avançado</p>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
totalTokens = 0;
|
|
||||||
updateContextUsage(0);
|
updateContextUsage(0);
|
||||||
} else {
|
} else {
|
||||||
// Calculate token count from history
|
// Display existing history
|
||||||
totalTokens = 0;
|
|
||||||
history.forEach(([role, content]) => {
|
history.forEach(([role, content]) => {
|
||||||
addMessage(role, content, false);
|
addMessage(role, content, false);
|
||||||
totalTokens += estimateTokens(content);
|
|
||||||
});
|
});
|
||||||
updateContextUsage(totalTokens);
|
// Estimate context usage based on message count
|
||||||
|
updateContextUsage(history.length / 2); // Assuming 20 messages is 100% context
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to load session history:", error);
|
console.error("Failed to load session history:", error);
|
||||||
|
|
@ -1257,11 +1238,9 @@
|
||||||
emptyState.remove();
|
emptyState.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle context usage if provided by server
|
// Handle context usage if provided
|
||||||
if (response.context_usage !== undefined) {
|
if (response.context_usage !== undefined) {
|
||||||
// Server provides usage as a ratio (0-1)
|
updateContextUsage(response.context_usage);
|
||||||
const tokens = Math.round(response.context_usage * MAX_TOKENS);
|
|
||||||
updateContextUsage(tokens);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle complete messages
|
// Handle complete messages
|
||||||
|
|
@ -1274,7 +1253,6 @@
|
||||||
} else {
|
} else {
|
||||||
// This is a complete message that wasn't being streamed
|
// This is a complete message that wasn't being streamed
|
||||||
addMessage("assistant", response.content, false);
|
addMessage("assistant", response.content, false);
|
||||||
addToTokenCount(response.content);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Handle streaming messages
|
// Handle streaming messages
|
||||||
|
|
@ -1308,9 +1286,7 @@
|
||||||
showWarning(eventData.message);
|
showWarning(eventData.message);
|
||||||
break;
|
break;
|
||||||
case "context_usage":
|
case "context_usage":
|
||||||
// Server provides usage as a ratio (0-1)
|
updateContextUsage(eventData.usage);
|
||||||
const tokens = Math.round(eventData.usage * MAX_TOKENS);
|
|
||||||
updateContextUsage(tokens);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1501,10 +1477,8 @@
|
||||||
<div class="user-message-content">${escapeHtml(content)}</div>
|
<div class="user-message-content">${escapeHtml(content)}</div>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
// Add tokens for user message
|
// Update context usage when user sends a message
|
||||||
if (!streaming) {
|
updateContextUsage(contextUsage + 0.05); // Simulate 5% increase per message
|
||||||
addToTokenCount(content);
|
|
||||||
}
|
|
||||||
} else if (role === "assistant") {
|
} else if (role === "assistant") {
|
||||||
msg.innerHTML = `
|
msg.innerHTML = `
|
||||||
<div class="assistant-message">
|
<div class="assistant-message">
|
||||||
|
|
@ -1514,10 +1488,8 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
// Add tokens for assistant message (only if not streaming)
|
// Update context usage when assistant responds
|
||||||
if (!streaming) {
|
updateContextUsage(contextUsage + 0.03); // Simulate 3% increase per response
|
||||||
addToTokenCount(content);
|
|
||||||
}
|
|
||||||
} else if (role === "voice") {
|
} else if (role === "voice") {
|
||||||
msg.innerHTML = `
|
msg.innerHTML = `
|
||||||
<div class="assistant-message">
|
<div class="assistant-message">
|
||||||
|
|
@ -1573,9 +1545,6 @@
|
||||||
);
|
);
|
||||||
msgElement.removeAttribute("id");
|
msgElement.removeAttribute("id");
|
||||||
|
|
||||||
// Add tokens for completed streaming message
|
|
||||||
addToTokenCount(currentStreamingContent);
|
|
||||||
|
|
||||||
// Auto-scroll to bottom if user isn't manually scrolling
|
// Auto-scroll to bottom if user isn't manually scrolling
|
||||||
if (!isUserScrolling) {
|
if (!isUserScrolling) {
|
||||||
scrollToBottom();
|
scrollToBottom();
|
||||||
|
|
@ -1817,4 +1786,4 @@
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue