Compare commits

..

No commits in common. "main" and "master" have entirely different histories.
main ... master

10 changed files with 188 additions and 409 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "botlib"
version = "6.3.0"
version = "6.1.0"
edition = "2021"
description = "Shared library for General Bots - common types, utilities, and HTTP client"
license = "AGPL-3.0"
@ -10,13 +10,13 @@ keywords = ["bot", "chatbot", "ai", "conversational", "library"]
categories = ["api-bindings", "web-programming"]
[features]
default = ["database", "i18n"]
default = []
full = ["database", "http-client", "validation", "resilience", "i18n"]
database = ["i18n"]
database = []
http-client = ["dep:reqwest"]
validation = []
resilience = []
i18n = ["dep:rust-embed"]
i18n = []
[dependencies]
# Core
@ -34,9 +34,6 @@ tokio = { workspace = true, features = ["sync", "time"] }
# Optional: HTTP Client
reqwest = { workspace = true, features = ["json", "rustls-tls"], optional = true }
# Optional: i18n
rust-embed = { workspace = true, optional = true, features = ["debug-embed"] }
[dev-dependencies]
tokio = { workspace = true, features = ["rt", "macros"] }

114
PROMPT.md Normal file
View file

@ -0,0 +1,114 @@
# BotLib Development Guide
**Version:** 6.2.0
**Purpose:** Shared library for General Bots workspace
---
## ZERO TOLERANCE POLICY
**EVERY SINGLE WARNING MUST BE FIXED. NO EXCEPTIONS.**
---
## ❌ ABSOLUTE PROHIBITIONS
```
❌ NEVER use #![allow()] or #[allow()] in source code
❌ NEVER use _ prefix for unused variables - DELETE or USE them
❌ NEVER use .unwrap() - use ? or proper error handling
❌ NEVER use .expect() - use ? or proper error handling
❌ NEVER use panic!() or unreachable!()
❌ NEVER use todo!() or unimplemented!()
❌ NEVER leave unused imports or dead code
❌ NEVER add comments - code must be self-documenting
```
---
## 🏗️ MODULE STRUCTURE
```
src/
├── lib.rs # Public exports, feature gates
├── error.rs # Error types (thiserror)
├── models.rs # Shared data models
├── message_types.rs # Message type definitions
├── http_client.rs # HTTP client wrapper (feature-gated)
├── branding.rs # Version, branding constants
└── version.rs # Version information
```
---
## ✅ MANDATORY CODE PATTERNS
### Error Handling
```rust
// ❌ WRONG
let value = something.unwrap();
// ✅ CORRECT
let value = something?;
let value = something.ok_or_else(|| Error::NotFound)?;
```
### Self Usage
```rust
impl MyStruct {
fn new() -> Self { Self { } } // ✅ Not MyStruct
}
```
### Format Strings
```rust
format!("Hello {name}") // ✅ Not format!("{}", name)
```
### Display vs ToString
```rust
// ❌ WRONG
impl ToString for MyType { }
// ✅ CORRECT
impl std::fmt::Display for MyType { }
```
### Derive Eq with PartialEq
```rust
#[derive(PartialEq, Eq)] // ✅ Always both
struct MyStruct { }
```
---
## 📦 KEY DEPENDENCIES
| Library | Version | Purpose |
|---------|---------|---------|
| anyhow | 1.0 | Error handling |
| thiserror | 2.0 | Error derive |
| chrono | 0.4 | Date/time |
| serde | 1.0 | Serialization |
| uuid | 1.11 | UUIDs |
| diesel | 2.1 | Database ORM |
| reqwest | 0.12 | HTTP client |
---
## 🔑 REMEMBER
- **ZERO WARNINGS** - Every clippy warning must be fixed
- **NO ALLOW IN CODE** - Never use #[allow()] in source files
- **NO DEAD CODE** - Delete unused code
- **NO UNWRAP/EXPECT** - Use ? operator
- **INLINE FORMAT ARGS** - `format!("{name}")` not `format!("{}", name)`
- **USE SELF** - In impl blocks, use Self not the type name
- **DERIVE EQ** - Always derive Eq with PartialEq
- **DISPLAY NOT TOSTRING** - Implement Display, not ToString
- **Version 6.2.0** - do not change without approval

171
README.md
View file

@ -1,170 +1,3 @@
# BotLib - General Bots Shared Library
General Bots® base library for building Node.js TypeScript Apps packages (.gbapp).
**Version:** 6.2.0
**Purpose:** Shared library for General Bots workspace
---
## Overview
BotLib is the foundational shared library for the General Bots workspace, providing common types, error handling, HTTP client functionality, and utilities used across all projects. It serves as the core dependency for botserver, botui, botapp, and other workspace members, ensuring consistency and reducing code duplication.
For comprehensive documentation, see **[docs.pragmatismo.com.br](https://docs.pragmatismo.com.br)** or the **[BotBook](../botbook)** for detailed guides and API references.
---
## 🏗️ Module Structure
```
src/
├── lib.rs # Public exports, feature gates
├── error.rs # Error types (thiserror)
├── models.rs # Shared data models
├── message_types.rs # Message type definitions
├── http_client.rs # HTTP client wrapper (feature-gated)
├── branding.rs # Version, branding constants
└── version.rs # Version information
```
---
## ✅ ZERO TOLERANCE POLICY
**EVERY SINGLE WARNING MUST BE FIXED. NO EXCEPTIONS.**
### Absolute Prohibitions
```
❌ NEVER use #![allow()] or #[allow()] in source code
❌ NEVER use _ prefix for unused variables - DELETE or USE them
❌ NEVER use .unwrap() - use ? or proper error handling
❌ NEVER use .expect() - use ? or proper error handling
❌ NEVER use panic!() or unreachable!()
❌ NEVER use todo!() or unimplemented!()
❌ NEVER leave unused imports or dead code
❌ NEVER add comments - code must be self-documenting
```
---
## 📦 Key Dependencies
| Library | Version | Purpose |
|---------|---------|---------|
| anyhow | 1.0 | Error handling |
| thiserror | 2.0 | Error derive |
| chrono | 0.4 | Date/time |
| serde | 1.0 | Serialization |
| uuid | 1.11 | UUIDs |
| diesel | 2.1 | Database ORM |
| reqwest | 0.12 | HTTP client |
---
## 🔧 Features
### Feature Gates
BotLib uses Cargo features to enable optional functionality:
```toml
[features]
default = []
http-client = ["reqwest"] # Enable HTTP client
# Add more features as needed
```
### Using Features
```toml
# In dependent crate's Cargo.toml
[dependencies.botlib]
workspace = true
features = ["http-client"] # Enable HTTP client
```
---
## ✅ Mandatory Code Patterns
### Error Handling
```rust
// ❌ WRONG
let value = something.unwrap();
// ✅ CORRECT
let value = something?;
let value = something.ok_or_else(|| Error::NotFound)?;
```
### Self Usage
```rust
impl MyStruct {
fn new() -> Self { Self { } } // ✅ Not MyStruct
}
```
### Format Strings
```rust
format!("Hello {name}") // ✅ Not format!("{}", name)
```
### Display vs ToString
```rust
// ❌ WRONG
impl ToString for MyType { }
// ✅ CORRECT
impl std::fmt::Display for MyType { }
```
### Derive Eq with PartialEq
```rust
#[derive(PartialEq, Eq)] // ✅ Always both
struct MyStruct { }
```
---
## 📚 Documentation
For complete documentation, guides, and API references:
- **[docs.pragmatismo.com.br](https://docs.pragmatismo.com.br)** - Full online documentation
- **[BotBook](../botbook)** - Local comprehensive guide with tutorials and examples
- **[General Bots Repository](https://github.com/GeneralBots/BotServer)** - Main project repository
---
## 🔗 Related Projects
- **[botserver](https://github.com/GeneralBots/botserver)** - Main API server
- **[botui](https://github.com/GeneralBots/botui)** - Web UI interface
- **[botapp](https://github.com/GeneralBots/botapp)** - Desktop application
- **[botbook](https://github.com/GeneralBots/botbook)** - Documentation
---
## 🔑 Remember
- **ZERO WARNINGS** - Every clippy warning must be fixed
- **NO ALLOW IN CODE** - Never use #[allow()] in source files
- **NO DEAD CODE** - Delete unused code
- **NO UNWRAP/EXPECT** - Use ? operator
- **INLINE FORMAT ARGS** - `format!("{name}")` not `format!("{}", name)`
- **USE SELF** - In impl blocks, use Self not the type name
- **DERIVE EQ** - Always derive Eq with PartialEq
- **DISPLAY NOT TOSTRING** - Implement Display, not ToString
- **Version 6.2.0** - Do not change without approval
- **GIT WORKFLOW** - ALWAYS push to ALL repositories (github, pragmatismo)
---
## 📄 License
AGPL-3.0 - See [LICENSE](LICENSE) for details.
See: https://github.com/pragmatismo-io/botserver for main documentation.

View file

@ -1636,16 +1636,3 @@ goals-dashboard = Dashboard
goals-objectives = Objectives
goals-alignment = Alignment
goals-ai-suggestions = AI Suggestions
# CRM / Mail / Campaigns integration keys
crm-email = Email
crm-compose-email = Compose Email
crm-send-email = Send Email
mail-snooze = Snooze
mail-snooze-later-today = Later today (6:00 PM)
mail-snooze-tomorrow = Tomorrow (8:00 AM)
mail-snooze-next-week = Next week (Mon 8:00 AM)
mail-crm-log = Log to CRM
mail-crm-create-lead = Create Lead
mail-add-to-list = Add to List
campaign-send-email = Send Email

View file

@ -941,15 +941,3 @@ goals-dashboard = Panel
goals-objectives = Objetivos
goals-alignment = Alineación
goals-ai-suggestions = Sugerencias de IA
crm-email = Correo
crm-compose-email = Redactar Correo
crm-send-email = Enviar Correo
mail-snooze = Posponer
mail-snooze-later-today = Más tarde hoy (18:00)
mail-snooze-tomorrow = Mañana (08:00)
mail-snooze-next-week = Próxima semana (Lun 08:00)
mail-crm-log = Registrar en CRM
mail-crm-create-lead = Crear Lead
mail-add-to-list = Agregar a Lista
campaign-send-email = Enviar Correo

View file

@ -1636,15 +1636,3 @@ goals-dashboard = Painel
goals-objectives = Objetivos
goals-alignment = Alinhamento
goals-ai-suggestions = Sugestões da IA
crm-email = E-mail
crm-compose-email = Redigir E-mail
crm-send-email = Enviar E-mail
mail-snooze = Adiar
mail-snooze-later-today = Mais tarde hoje (18:00)
mail-snooze-tomorrow = Amanhã (08:00)
mail-snooze-next-week = Próxima semana (Seg 08:00)
mail-crm-log = Registrar no CRM
mail-crm-create-lead = Criar Lead
mail-add-to-list = Adicionar à Lista
campaign-send-email = Enviar E-mail

View file

@ -4,7 +4,7 @@ use serde::{de::DeserializeOwned, Serialize};
use std::sync::Arc;
use std::time::Duration;
const DEFAULT_BOTSERVER_URL: &str = "http://localhost:8080";
const DEFAULT_BOTSERVER_URL: &str = "https://localhost:8088";
const DEFAULT_TIMEOUT_SECS: u64 = 30;
#[derive(Clone)]
@ -217,8 +217,8 @@ mod tests {
#[test]
fn test_client_creation() {
let client = BotServerClient::new(Some("http://localhost:9000".to_string()));
assert_eq!(client.base_url(), "http://localhost:9000");
let client = BotServerClient::new(Some("http://localhost:8080".to_string()));
assert_eq!(client.base_url(), "http://localhost:8080");
}
#[test]

View file

@ -1,20 +1,10 @@
use crate::error::{BotError, BotResult};
use std::collections::HashMap;
#[cfg(not(feature = "i18n"))]
use std::fs;
#[cfg(not(feature = "i18n"))]
use std::path::Path;
#[cfg(feature = "i18n")]
use rust_embed::RustEmbed;
use super::Locale;
#[cfg(feature = "i18n")]
#[derive(RustEmbed)]
#[folder = "locales"]
struct EmbeddedLocales;
pub type MessageArgs = HashMap<String, String>;
#[derive(Debug)]
@ -69,18 +59,11 @@ impl TranslationFile {
}
fn get(&self, key: &str) -> Option<&String> {
let result = self.messages.get(key);
if result.is_none() {
log::warn!("Translation key not found in bundle: {} (available keys: {})", key, self.messages.len());
}
result
self.messages.get(key)
}
fn merge(&mut self, other: Self) {
let before = self.messages.len();
self.messages.extend(other.messages);
let after = self.messages.len();
log::debug!("Merged {} translations (total: {})", after - before, after);
}
}
@ -91,7 +74,6 @@ struct LocaleBundle {
}
impl LocaleBundle {
#[cfg(not(feature = "i18n"))]
fn load(locale_dir: &Path) -> BotResult<Self> {
let dir_name = locale_dir
.file_name()
@ -105,12 +87,14 @@ impl LocaleBundle {
messages: HashMap::new(),
};
let entries = fs::read_dir(locale_dir)
.map_err(|e| BotError::config(format!("failed to read locale directory: {e}")))?;
let entries = fs::read_dir(locale_dir).map_err(|e| {
BotError::config(format!("failed to read locale directory: {e}"))
})?;
for entry in entries {
let entry = entry
.map_err(|e| BotError::config(format!("failed to read directory entry: {e}")))?;
let entry = entry.map_err(|e| {
BotError::config(format!("failed to read directory entry: {e}"))
})?;
let path = entry.path();
@ -133,35 +117,6 @@ impl LocaleBundle {
})
}
#[cfg(feature = "i18n")]
fn load_embedded(locale_str: &str) -> BotResult<Self> {
let locale = Locale::new(locale_str)
.ok_or_else(|| BotError::config(format!("invalid locale: {locale_str}")))?;
let mut translations = TranslationFile {
messages: HashMap::new(),
};
log::info!("Loading embedded files for locale: {}", locale_str);
for file in EmbeddedLocales::iter() {
if file.starts_with(locale_str) && file.ends_with(".ftl") {
log::info!("Found .ftl file for locale {}: {}", locale_str, file);
if let Some(content_bytes) = EmbeddedLocales::get(&file) {
if let Ok(content) = std::str::from_utf8(content_bytes.data.as_ref()) {
let file_translations = TranslationFile::parse(content);
log::info!("Parsed {} keys from {}", file_translations.messages.len(), file);
translations.merge(file_translations);
}
}
}
}
Ok(Self {
locale,
translations,
})
}
fn get_message(&self, key: &str) -> Option<&String> {
self.translations.get(key)
}
@ -175,96 +130,43 @@ pub struct I18nBundle {
}
impl I18nBundle {
pub fn load(_base_path: &str) -> BotResult<Self> {
// When i18n feature is enabled, locales are ALWAYS embedded via rust-embed
// Filesystem loading is deprecated - use embedded assets only
#[cfg(feature = "i18n")]
{
log::info!("Loading embedded locale translations (rust-embed)");
Self::load_embedded()
pub fn load(base_path: &str) -> BotResult<Self> {
let base = Path::new(base_path);
if !base.exists() {
return Err(BotError::config(format!(
"locales directory not found: {base_path}"
)));
}
#[cfg(not(feature = "i18n"))]
{
// let _base_path = base_path; // Suppress unused warning when i18n is enabled
let base = Path::new(_base_path);
if !base.exists() {
return Err(BotError::config(format!(
"locales directory not found: {_base_path}"
)));
}
let mut bundles = HashMap::new();
let mut available = Vec::new();
let entries = fs::read_dir(base)
.map_err(|e| BotError::config(format!("failed to read locales directory: {e}")))?;
for entry in entries {
let entry = entry
.map_err(|e| BotError::config(format!("failed to read directory entry: {e}")))?;
let path = entry.path();
if path.is_dir() {
match LocaleBundle::load(&path) {
Ok(bundle) => {
available.push(bundle.locale.clone());
bundles.insert(bundle.locale.to_string(), bundle);
}
Err(e) => {
log::warn!("failed to load locale bundle: {e}");
}
}
}
}
let fallback = Locale::default();
Ok(Self {
bundles,
available,
fallback,
})
}
}
#[cfg(feature = "i18n")]
fn load_embedded() -> BotResult<Self> {
let mut bundles = HashMap::new();
let mut available = Vec::new();
let mut seen_locales = std::collections::HashSet::new();
let files: Vec<_> = EmbeddedLocales::iter().collect();
log::info!("Loading embedded locales, found {} files", files.len());
let entries = fs::read_dir(base).map_err(|e| {
BotError::config(format!("failed to read locales directory: {e}"))
})?;
for file in files {
// Path structure: locale/file.ftl
let parts: Vec<&str> = file.split('/').collect();
if let Some(locale_str) = parts.first() {
if !seen_locales.contains(*locale_str) {
match LocaleBundle::load_embedded(locale_str) {
Ok(bundle) => {
available.push(bundle.locale.clone());
bundles.insert(bundle.locale.to_string(), bundle);
seen_locales.insert(locale_str.to_string());
}
Err(e) => {
log::warn!(
"failed to load embedded locale bundle {}: {}",
locale_str,
e
);
}
for entry in entries {
let entry = entry.map_err(|e| {
BotError::config(format!("failed to read directory entry: {e}"))
})?;
let path = entry.path();
if path.is_dir() {
match LocaleBundle::load(&path) {
Ok(bundle) => {
available.push(bundle.locale.clone());
bundles.insert(bundle.locale.to_string(), bundle);
}
Err(e) => {
log::warn!("failed to load locale bundle: {e}");
}
}
}
}
let fallback = Locale::default();
log::info!("Loaded {} embedded locales: {:?}", available.len(), available);
Ok(Self {
bundles,
@ -356,15 +258,15 @@ impl I18nBundle {
let mut result = template.to_string();
for (key, value) in args {
if let Ok(count) = value.parse::<i64>() {
let plural_pattern = format!("{{ ${key} ->");
let count: i64 = value.parse().unwrap_or(0);
if let Some(start) = result.find(&plural_pattern) {
if let Some(end) = result[start..].find('}') {
let plural_block = &result[start..start + end + 1];
let replacement = Self::select_plural_form(plural_block, count);
result = result.replace(plural_block, &replacement);
}
let plural_pattern = format!("{{ ${key} ->");
if let Some(start) = result.find(&plural_pattern) {
if let Some(end) = result[start..].find('}') {
let plural_block = &result[start..start + end + 1];
let replacement = Self::select_plural_form(plural_block, count);
result = result.replace(plural_block, &replacement);
}
}
}
@ -428,7 +330,10 @@ world = World
greeting = Hello, { $name }!
"#;
let file = TranslationFile::parse(content);
assert_eq!(file.get("greeting"), Some(&"Hello, { $name }!".to_string()));
assert_eq!(
file.get("greeting"),
Some(&"Hello, { $name }!".to_string())
);
}
#[test]

View file

@ -2,13 +2,20 @@ use env_logger::fmt::Formatter;
use log::Record;
use std::io::Write;
// ANSI color codes
const RED: &str = "\x1b[31m";
const YELLOW: &str = "\x1b[33m";
const GREEN: &str = "\x1b[32m";
const CYAN: &str = "\x1b[36m";
const RESET: &str = "\x1b[0m";
pub fn compact_format(buf: &mut Formatter, record: &Record) -> std::io::Result<()> {
let level_str = match record.level() {
log::Level::Error => "error",
log::Level::Warn => "warn",
log::Level::Info => "info",
log::Level::Debug => "debug",
log::Level::Trace => "trace",
let (level, color) = match record.level() {
log::Level::Error => ("E", RED),
log::Level::Warn => ("W", YELLOW),
log::Level::Info => ("I", GREEN),
log::Level::Debug => ("D", CYAN),
log::Level::Trace => ("T", ""),
};
let now = chrono::Local::now();
@ -21,50 +28,10 @@ pub fn compact_format(buf: &mut Formatter, record: &Record) -> std::io::Result<(
target
};
// Format: "YYYYMMDDHHMMSS.mmm level module:"
// Length: 18 + 1 + 5 (error) + 1 + module.len() + 1 = 26 + module.len()
let prefix = format!("{} {} {}:", timestamp, level_str, module);
// Max width 100
// Indent for wrapping: 18 timestamp + 1 space + 5 (longest level "error") + 1 space = 25 spaces
let message = record.args().to_string();
let indent = " "; // 25 spaces
if prefix.len() + message.len() <= 100 {
writeln!(buf, "{}{}", prefix, message)
if color.is_empty() {
writeln!(buf, "{} {} {}:{}", timestamp, level, module, record.args())
} else {
let available_first_line = if prefix.len() < 100 {
100 - prefix.len()
} else {
0
};
let available_other_lines = 100 - 25; // 75 chars
let mut current_pos = 0;
let chars: Vec<char> = message.chars().collect();
let total_chars = chars.len();
// First line
write!(buf, "{}", prefix)?;
// If prefix is already >= 100, we force a newline immediately?
// Or we just print a bit and wrap?
// Let's assume typical usage where module name isn't huge.
let take = std::cmp::min(available_first_line, total_chars);
let first_chunk: String = chars[0..take].iter().collect();
writeln!(buf, "{}", first_chunk)?;
current_pos += take;
while current_pos < total_chars {
write!(buf, "{}", indent)?;
let remaining = total_chars - current_pos;
let take = std::cmp::min(remaining, available_other_lines);
let chunk: String = chars[current_pos..current_pos + take].iter().collect();
writeln!(buf, "{}", chunk)?;
current_pos += take;
}
Ok(())
writeln!(buf, "{} {}{}{} {}:{}", timestamp, color, level, RESET, module, record.args())
}
}
@ -75,6 +42,8 @@ pub fn init_compact_logger(default_filter: &str) {
}
pub fn init_compact_logger_with_style(default_filter: &str) {
// Style ignored to strictly follow text format spec
init_compact_logger(default_filter);
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(default_filter))
.format(compact_format)
.write_style(env_logger::WriteStyle::Always)
.init();
}

View file

@ -1,3 +1,4 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
@ -16,8 +17,6 @@ impl MessageType {
pub const SUGGESTION: Self = Self(4);
pub const CONTEXT_CHANGE: Self = Self(5);
pub const TOOL_EXEC: Self = Self(6);
}
impl From<i32> for MessageType {
@ -47,7 +46,6 @@ impl std::fmt::Display for MessageType {
3 => "CONTINUE",
4 => "SUGGESTION",
5 => "CONTEXT_CHANGE",
6 => "TOOL_EXEC",
_ => "UNKNOWN",
};
write!(f, "{name}")