-
Notifications
You must be signed in to change notification settings - Fork 5
Feat/adding memory layer #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 17 commits
8a6d4c1
b15dea5
1dc8315
8a09391
3c53689
e58f570
df8886f
80d7ddd
58c6c9c
d864dfc
e78d07c
e5e3822
2f065bd
eb0c5b2
8a9a72c
371fd01
79d01df
76e0795
b3b89dd
70b3d22
16de51e
2983a1d
642a342
d84e027
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -155,4 +155,7 @@ Thumbs.db | |
| .env.test | ||
| .env.production | ||
|
|
||
| .actrc | ||
| # Database files | ||
| *.db | ||
| *.sqlite | ||
| *.sqlite3 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "database": { | ||
| "url": "sqlite://:memory:", | ||
| "max_context_records": 20, | ||
| "context_enrichment_count": 10, | ||
| "record_retention_days": 1 | ||
| }, | ||
| "enable_llm_fallback": true | ||
| } | ||
doriwal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
doriwal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| """ | ||
| Configuration management for MCP as a Judge. | ||
|
|
||
| This module handles loading and managing configuration from various sources | ||
| including config files, environment variables, and defaults. | ||
| """ | ||
|
|
||
| import json | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| from pydantic import BaseModel, Field | ||
|
|
||
| from .models import DatabaseConfig | ||
|
|
||
|
|
||
| class Config(BaseModel): | ||
| """Main configuration model for the application.""" | ||
|
|
||
| database: DatabaseConfig = Field(default_factory=DatabaseConfig) | ||
| enable_llm_fallback: bool = Field( | ||
| default=True, | ||
| description="Whether to enable LLM fallback when MCP sampling is not available", | ||
| ) | ||
|
|
||
|
|
||
| def load_config(config_path: str | None = None) -> Config: | ||
doriwal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| Load configuration from file and environment variables. | ||
|
|
||
| Args: | ||
| config_path: Path to config file. If None, looks for config.json in current directory | ||
|
|
||
| Returns: | ||
| Config object with loaded settings | ||
| """ | ||
| # Default configuration | ||
| config_data = {} | ||
|
|
||
| # Try to load from config file | ||
| if config_path is None: | ||
| config_path = "config.json" | ||
|
|
||
| config_file = Path(config_path) | ||
| if config_file.exists(): | ||
| try: | ||
| with open(config_file) as f: | ||
| config_data = json.load(f) | ||
| except (json.JSONDecodeError, OSError) as e: | ||
| print(f"Warning: Could not load config file {config_path}: {e}") | ||
|
|
||
| # Override with environment variables if present | ||
| db_provider = os.getenv("MCP_JUDGE_DB_PROVIDER") | ||
| if db_provider: | ||
| if "database" not in config_data: | ||
| config_data["database"] = {} | ||
| config_data["database"]["provider"] = db_provider | ||
|
|
||
| db_url = os.getenv("MCP_JUDGE_DB_URL") | ||
| if db_url: | ||
| if "database" not in config_data: | ||
| config_data["database"] = {} | ||
| config_data["database"]["url"] = db_url | ||
|
|
||
| max_context = os.getenv("MCP_JUDGE_MAX_CONTEXT_RECORDS") | ||
| if max_context: | ||
| if "database" not in config_data: | ||
| config_data["database"] = {} | ||
| try: | ||
| config_data["database"]["max_context_records"] = int(max_context) | ||
| except ValueError: | ||
| print( | ||
| f"Warning: Invalid value for MCP_JUDGE_MAX_CONTEXT_RECORDS: {max_context}" | ||
| ) | ||
|
|
||
| llm_fallback = os.getenv("MCP_JUDGE_ENABLE_LLM_FALLBACK") | ||
| if llm_fallback: | ||
| config_data["enable_llm_fallback"] = llm_fallback.lower() in ( | ||
| "true", | ||
| "1", | ||
| "yes", | ||
| "on", | ||
| ) | ||
|
|
||
| return Config(**config_data) | ||
|
|
||
|
|
||
| def create_default_config_file(config_path: str = "config.json") -> None: | ||
doriwal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| Create a default configuration file. | ||
|
|
||
| Args: | ||
| config_path: Path where to create the config file | ||
| """ | ||
| default_config = { | ||
| "database": {"provider": "in_memory", "url": "", "max_context_records": 10}, | ||
| "enable_llm_fallback": True, | ||
| } | ||
|
|
||
| with open(config_path, "w") as f: | ||
| json.dump(default_config, f, indent=2) | ||
|
|
||
| print(f"Created default configuration file: {config_path}") | ||
|
|
||
|
|
||
| def get_database_provider_from_url(url: str) -> str: | ||
|
||
| """ | ||
| Determine database provider from URL. | ||
|
|
||
| Args: | ||
| url: Database connection URL | ||
|
|
||
| Returns: | ||
| Provider name based on URL scheme | ||
|
|
||
| Examples: | ||
| "" or None -> "in_memory" (SQLite in-memory) | ||
| "sqlite://:memory:" -> "in_memory" (SQLite in-memory) | ||
| "sqlite:///path/to/file.db" -> "sqlite" (SQLite file) | ||
| "postgresql://..." -> "postgresql" | ||
| "mysql://..." -> "mysql" | ||
| """ | ||
| if not url or url.strip() == "": | ||
| return "in_memory" | ||
|
|
||
| url_lower = url.lower().strip() | ||
|
|
||
| # SQLite in-memory | ||
| if url_lower == "sqlite://:memory:" or url_lower == ":memory:": | ||
| return "in_memory" | ||
|
|
||
| # SQLite file | ||
| elif url_lower.startswith("sqlite://") or url_lower.endswith(".db"): | ||
| return "sqlite" | ||
|
|
||
| # PostgreSQL | ||
| elif url_lower.startswith("postgresql://") or url_lower.startswith("postgres://"): | ||
| return "postgresql" | ||
|
|
||
| # MySQL | ||
| elif url_lower.startswith("mysql://") or url_lower.startswith("mysql+"): | ||
| return "mysql" | ||
|
|
||
| else: | ||
| # Default to in_memory for unknown URLs | ||
| return "in_memory" | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename the package to memory
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why, its storage that can be in memory and can be regular , i think db is a good name for that |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| """ | ||
| Database abstraction layer for MCP as a Judge. | ||
|
|
||
| This module provides database interfaces and providers for storing | ||
| conversation history and tool interactions. | ||
| """ | ||
|
|
||
| from .factory import DatabaseFactory, create_database_provider | ||
doriwal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| from .interface import ConversationHistoryDB, ConversationRecord | ||
| from .providers import SQLiteProvider | ||
doriwal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| __all__ = [ | ||
| "ConversationHistoryDB", | ||
| "ConversationRecord", | ||
| "DatabaseFactory", | ||
| "SQLiteProvider", | ||
| "create_database_provider", | ||
| ] | ||
Uh oh!
There was an error while loading. Please reload this page.