Use central Anthropic key for AI insights; add update-check banner
AI insights now fetches the Claude API key from the central settings service instead of storing its own encrypted copy, so wages and other apps can share the same key. Also wires up the update-available banner using the existing version-check hook pattern. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
72f607132c
commit
4137813fc7
7 changed files with 141 additions and 42 deletions
|
|
@ -14,6 +14,7 @@ import logging
|
|||
|
||||
from database import get_db
|
||||
from auth import get_current_user, get_all_api_keys, create_api_key, revoke_api_key, delete_api_key
|
||||
from services.central_settings import get_anthropic_credentials
|
||||
|
||||
router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
|
@ -1228,7 +1229,6 @@ class AIInsightsSettingsResponse(BaseModel):
|
|||
|
||||
class AIInsightsSettingsUpdate(BaseModel):
|
||||
enabled: Optional[bool] = None
|
||||
api_key: Optional[str] = None
|
||||
model: Optional[str] = None
|
||||
schedule_time: Optional[str] = None
|
||||
daily_token_budget: Optional[int] = None
|
||||
|
|
@ -1239,23 +1239,21 @@ async def get_ai_insights_settings(
|
|||
db: AsyncSession = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user)
|
||||
):
|
||||
"""Get AI Insights configuration."""
|
||||
"""Get AI Insights configuration. The Anthropic API key itself is managed
|
||||
centrally in Portal → Settings → Integrations, not stored here."""
|
||||
result = await db.execute(
|
||||
text("""
|
||||
SELECT config_key, config_value, COALESCE(is_encrypted, false) as is_encrypted
|
||||
SELECT config_key, config_value
|
||||
FROM system_config
|
||||
WHERE config_key LIKE 'ai_insights_%'
|
||||
""")
|
||||
)
|
||||
config = {}
|
||||
for row in result.fetchall():
|
||||
config[row.config_key] = row.config_value
|
||||
if row.config_key == 'ai_insights_api_key':
|
||||
config['_api_key_set'] = bool(row.config_value)
|
||||
config = {row.config_key: row.config_value for row in result.fetchall()}
|
||||
anthropic_creds = await get_anthropic_credentials()
|
||||
|
||||
return AIInsightsSettingsResponse(
|
||||
enabled=config.get('ai_insights_enabled', 'false').lower() in ('true', '1', 'yes'),
|
||||
api_key_set=config.get('_api_key_set', False),
|
||||
api_key_set=bool(anthropic_creds and anthropic_creds.get('api_key')),
|
||||
model=config.get('ai_insights_model', 'claude-haiku-4-5-20251001'),
|
||||
schedule_time=config.get('ai_insights_schedule_time', '07:15'),
|
||||
daily_token_budget=int(config.get('ai_insights_daily_token_budget', '12000')),
|
||||
|
|
@ -1268,12 +1266,11 @@ async def update_ai_insights_settings(
|
|||
db: AsyncSession = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user)
|
||||
):
|
||||
"""Update AI Insights configuration."""
|
||||
"""Update AI Insights configuration. Does not accept an API key —
|
||||
manage that centrally in Portal → Settings → Integrations."""
|
||||
updates = {}
|
||||
if settings.enabled is not None:
|
||||
updates['ai_insights_enabled'] = ('true' if settings.enabled else 'false', False)
|
||||
if settings.api_key is not None and settings.api_key.strip():
|
||||
updates['ai_insights_api_key'] = (simple_encrypt(settings.api_key.strip()), True)
|
||||
if settings.model is not None:
|
||||
updates['ai_insights_model'] = (settings.model, False)
|
||||
if settings.schedule_time is not None:
|
||||
|
|
@ -1301,10 +1298,11 @@ async def test_ai_insights_connection(
|
|||
db: AsyncSession = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user)
|
||||
):
|
||||
"""Test Anthropic API connection with stored API key."""
|
||||
api_key = await _get_config_value(db, "ai_insights_api_key")
|
||||
"""Test Anthropic API connection using the centrally configured API key."""
|
||||
anthropic_creds = await get_anthropic_credentials()
|
||||
api_key = anthropic_creds.get('api_key') if anthropic_creds else None
|
||||
if not api_key:
|
||||
raise HTTPException(status_code=400, detail="No API key configured")
|
||||
raise HTTPException(status_code=400, detail="Anthropic API key not configured — add it in Portal → Settings → Integrations")
|
||||
|
||||
model = await _get_config_value(db, "ai_insights_model") or "claude-haiku-4-5-20251001"
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue