chore: standardize string quotes and fix import ordering across multiple modules

This commit is contained in:
2025-11-04 07:09:12 +00:00
parent ea29bdc403
commit e9ced4a493
82 changed files with 4963 additions and 3094 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
from .api_cache import APICache
from .tool_cache import ToolCache
__all__ = ['APICache', 'ToolCache']
__all__ = ["APICache", "ToolCache"]
+60 -26
View File
@@ -2,7 +2,8 @@ import hashlib
import json
import sqlite3
import time
from typing import Optional, Dict, Any
from typing import Any, Dict, Optional
class APICache:
def __init__(self, db_path: str, ttl_seconds: int = 3600):
@@ -13,7 +14,8 @@ class APICache:
def _initialize_cache(self):
conn = sqlite3.connect(self.db_path, check_same_thread=False)
cursor = conn.cursor()
cursor.execute('''
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS api_cache (
cache_key TEXT PRIMARY KEY,
response_data TEXT NOT NULL,
@@ -22,34 +24,44 @@ class APICache:
model TEXT,
token_count INTEGER
)
''')
cursor.execute('''
"""
)
cursor.execute(
"""
CREATE INDEX IF NOT EXISTS idx_expires_at ON api_cache(expires_at)
''')
"""
)
conn.commit()
conn.close()
def _generate_cache_key(self, model: str, messages: list, temperature: float, max_tokens: int) -> str:
def _generate_cache_key(
self, model: str, messages: list, temperature: float, max_tokens: int
) -> str:
cache_data = {
'model': model,
'messages': messages,
'temperature': temperature,
'max_tokens': max_tokens
"model": model,
"messages": messages,
"temperature": temperature,
"max_tokens": max_tokens,
}
serialized = json.dumps(cache_data, sort_keys=True)
return hashlib.sha256(serialized.encode()).hexdigest()
def get(self, model: str, messages: list, temperature: float, max_tokens: int) -> Optional[Dict[str, Any]]:
def get(
self, model: str, messages: list, temperature: float, max_tokens: int
) -> Optional[Dict[str, Any]]:
cache_key = self._generate_cache_key(model, messages, temperature, max_tokens)
conn = sqlite3.connect(self.db_path, check_same_thread=False)
cursor = conn.cursor()
current_time = int(time.time())
cursor.execute('''
cursor.execute(
"""
SELECT response_data FROM api_cache
WHERE cache_key = ? AND expires_at > ?
''', (cache_key, current_time))
""",
(cache_key, current_time),
)
row = cursor.fetchone()
conn.close()
@@ -58,8 +70,15 @@ class APICache:
return json.loads(row[0])
return None
def set(self, model: str, messages: list, temperature: float, max_tokens: int,
response: Dict[str, Any], token_count: int = 0):
def set(
self,
model: str,
messages: list,
temperature: float,
max_tokens: int,
response: Dict[str, Any],
token_count: int = 0,
):
cache_key = self._generate_cache_key(model, messages, temperature, max_tokens)
current_time = int(time.time())
@@ -68,11 +87,21 @@ class APICache:
conn = sqlite3.connect(self.db_path, check_same_thread=False)
cursor = conn.cursor()
cursor.execute('''
cursor.execute(
"""
INSERT OR REPLACE INTO api_cache
(cache_key, response_data, created_at, expires_at, model, token_count)
VALUES (?, ?, ?, ?, ?, ?)
''', (cache_key, json.dumps(response), current_time, expires_at, model, token_count))
""",
(
cache_key,
json.dumps(response),
current_time,
expires_at,
model,
token_count,
),
)
conn.commit()
conn.close()
@@ -83,7 +112,7 @@ class APICache:
conn = sqlite3.connect(self.db_path, check_same_thread=False)
cursor = conn.cursor()
cursor.execute('DELETE FROM api_cache WHERE expires_at <= ?', (current_time,))
cursor.execute("DELETE FROM api_cache WHERE expires_at <= ?", (current_time,))
deleted_count = cursor.rowcount
conn.commit()
@@ -95,7 +124,7 @@ class APICache:
conn = sqlite3.connect(self.db_path, check_same_thread=False)
cursor = conn.cursor()
cursor.execute('DELETE FROM api_cache')
cursor.execute("DELETE FROM api_cache")
deleted_count = cursor.rowcount
conn.commit()
@@ -107,21 +136,26 @@ class APICache:
conn = sqlite3.connect(self.db_path, check_same_thread=False)
cursor = conn.cursor()
cursor.execute('SELECT COUNT(*) FROM api_cache')
cursor.execute("SELECT COUNT(*) FROM api_cache")
total_entries = cursor.fetchone()[0]
current_time = int(time.time())
cursor.execute('SELECT COUNT(*) FROM api_cache WHERE expires_at > ?', (current_time,))
cursor.execute(
"SELECT COUNT(*) FROM api_cache WHERE expires_at > ?", (current_time,)
)
valid_entries = cursor.fetchone()[0]
cursor.execute('SELECT SUM(token_count) FROM api_cache WHERE expires_at > ?', (current_time,))
cursor.execute(
"SELECT SUM(token_count) FROM api_cache WHERE expires_at > ?",
(current_time,),
)
total_tokens = cursor.fetchone()[0] or 0
conn.close()
return {
'total_entries': total_entries,
'valid_entries': valid_entries,
'expired_entries': total_entries - valid_entries,
'total_cached_tokens': total_tokens
"total_entries": total_entries,
"valid_entries": valid_entries,
"expired_entries": total_entries - valid_entries,
"total_cached_tokens": total_tokens,
}
+58 -40
View File
@@ -2,16 +2,17 @@ import hashlib
import json
import sqlite3
import time
from typing import Optional, Any, Set
from typing import Any, Optional, Set
class ToolCache:
DETERMINISTIC_TOOLS: Set[str] = {
'read_file',
'list_directory',
'get_current_directory',
'db_get',
'db_query',
'index_directory'
"read_file",
"list_directory",
"get_current_directory",
"db_get",
"db_query",
"index_directory",
}
def __init__(self, db_path: str, ttl_seconds: int = 300):
@@ -22,7 +23,8 @@ class ToolCache:
def _initialize_cache(self):
conn = sqlite3.connect(self.db_path, check_same_thread=False)
cursor = conn.cursor()
cursor.execute('''
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS tool_cache (
cache_key TEXT PRIMARY KEY,
tool_name TEXT NOT NULL,
@@ -31,21 +33,23 @@ class ToolCache:
expires_at INTEGER NOT NULL,
hit_count INTEGER DEFAULT 0
)
''')
cursor.execute('''
"""
)
cursor.execute(
"""
CREATE INDEX IF NOT EXISTS idx_tool_expires ON tool_cache(expires_at)
''')
cursor.execute('''
"""
)
cursor.execute(
"""
CREATE INDEX IF NOT EXISTS idx_tool_name ON tool_cache(tool_name)
''')
"""
)
conn.commit()
conn.close()
def _generate_cache_key(self, tool_name: str, arguments: dict) -> str:
cache_data = {
'tool': tool_name,
'args': arguments
}
cache_data = {"tool": tool_name, "args": arguments}
serialized = json.dumps(cache_data, sort_keys=True)
return hashlib.sha256(serialized.encode()).hexdigest()
@@ -62,18 +66,24 @@ class ToolCache:
cursor = conn.cursor()
current_time = int(time.time())
cursor.execute('''
cursor.execute(
"""
SELECT result_data, hit_count FROM tool_cache
WHERE cache_key = ? AND expires_at > ?
''', (cache_key, current_time))
""",
(cache_key, current_time),
)
row = cursor.fetchone()
if row:
cursor.execute('''
cursor.execute(
"""
UPDATE tool_cache SET hit_count = hit_count + 1
WHERE cache_key = ?
''', (cache_key,))
""",
(cache_key,),
)
conn.commit()
conn.close()
return json.loads(row[0])
@@ -93,11 +103,14 @@ class ToolCache:
conn = sqlite3.connect(self.db_path, check_same_thread=False)
cursor = conn.cursor()
cursor.execute('''
cursor.execute(
"""
INSERT OR REPLACE INTO tool_cache
(cache_key, tool_name, result_data, created_at, expires_at, hit_count)
VALUES (?, ?, ?, ?, ?, 0)
''', (cache_key, tool_name, json.dumps(result), current_time, expires_at))
""",
(cache_key, tool_name, json.dumps(result), current_time, expires_at),
)
conn.commit()
conn.close()
@@ -106,7 +119,7 @@ class ToolCache:
conn = sqlite3.connect(self.db_path, check_same_thread=False)
cursor = conn.cursor()
cursor.execute('DELETE FROM tool_cache WHERE tool_name = ?', (tool_name,))
cursor.execute("DELETE FROM tool_cache WHERE tool_name = ?", (tool_name,))
deleted_count = cursor.rowcount
conn.commit()
@@ -120,7 +133,7 @@ class ToolCache:
conn = sqlite3.connect(self.db_path, check_same_thread=False)
cursor = conn.cursor()
cursor.execute('DELETE FROM tool_cache WHERE expires_at <= ?', (current_time,))
cursor.execute("DELETE FROM tool_cache WHERE expires_at <= ?", (current_time,))
deleted_count = cursor.rowcount
conn.commit()
@@ -132,7 +145,7 @@ class ToolCache:
conn = sqlite3.connect(self.db_path, check_same_thread=False)
cursor = conn.cursor()
cursor.execute('DELETE FROM tool_cache')
cursor.execute("DELETE FROM tool_cache")
deleted_count = cursor.rowcount
conn.commit()
@@ -144,36 +157,41 @@ class ToolCache:
conn = sqlite3.connect(self.db_path, check_same_thread=False)
cursor = conn.cursor()
cursor.execute('SELECT COUNT(*) FROM tool_cache')
cursor.execute("SELECT COUNT(*) FROM tool_cache")
total_entries = cursor.fetchone()[0]
current_time = int(time.time())
cursor.execute('SELECT COUNT(*) FROM tool_cache WHERE expires_at > ?', (current_time,))
cursor.execute(
"SELECT COUNT(*) FROM tool_cache WHERE expires_at > ?", (current_time,)
)
valid_entries = cursor.fetchone()[0]
cursor.execute('SELECT SUM(hit_count) FROM tool_cache WHERE expires_at > ?', (current_time,))
cursor.execute(
"SELECT SUM(hit_count) FROM tool_cache WHERE expires_at > ?",
(current_time,),
)
total_hits = cursor.fetchone()[0] or 0
cursor.execute('''
cursor.execute(
"""
SELECT tool_name, COUNT(*), SUM(hit_count)
FROM tool_cache
WHERE expires_at > ?
GROUP BY tool_name
''', (current_time,))
""",
(current_time,),
)
tool_stats = {}
for row in cursor.fetchall():
tool_stats[row[0]] = {
'cached_entries': row[1],
'total_hits': row[2] or 0
}
tool_stats[row[0]] = {"cached_entries": row[1], "total_hits": row[2] or 0}
conn.close()
return {
'total_entries': total_entries,
'valid_entries': valid_entries,
'expired_entries': total_entries - valid_entries,
'total_cache_hits': total_hits,
'by_tool': tool_stats
"total_entries": total_entries,
"valid_entries": valid_entries,
"expired_entries": total_entries - valid_entries,
"total_cache_hits": total_hits,
"by_tool": tool_stats,
}