Elevate MiroFish/CrowdSight from single-container dev to a SaaS foundation: - Local memory backend (Zep-compatible): memory services/models, local graph builder + updater, AgentActivity seam, import-boundary isolation; Zep stays default, local is opt-in behind MEMORY_BACKEND. Semantic parity not yet proven. - Durable product persistence: projects/simulations/reports schema (migration 0007) + tenant/owner-scoped ProductRepository + dual-write + scoped_project read-first + ArtifactStore abstraction; durable JobQueue + worker.py. - SaaS hardening: durable RateLimiter (wired to login), UsageService (LLM accounting), redacted AuditService, idempotency, CORS allowlist, safe API errors, single-use PasswordResetService + endpoints (covers invite-pending). - Exactly 3 roles (super_admin/admin/user) with tenant authz policy. - Admin UI: GET/POST/PATCH /api/admin/users + GET/PUT /api/admin/settings (super-admin only, encrypted/masked); AdminView.vue + SettingsView.vue with admin/super-admin route guards, th/en i18n. - Production deploy topology: multi-stage Dockerfile (frontend build + gunicorn wsgi + nginx SPA-proxy + supervisord worker), backend/wsgi.py, gunicorn dep. Backend 197 passed; frontend 10 tests + build green. ruff unavailable (gap). No commit of credentials; secrets handled via env/.env.example. Deferred: Zep semantic A/B parity, object storage cutover, mobile QA, EasyPanel container build of deploy topology.
83 lines
2.3 KiB
Python
83 lines
2.3 KiB
Python
import json
|
|
import os
|
|
import threading
|
|
from flask import request, has_request_context
|
|
|
|
from .language_policy import (
|
|
DEFAULT_LOCALE,
|
|
SUPPORTED_LOCALES,
|
|
locale_from_accept_language,
|
|
normalize_locale,
|
|
)
|
|
|
|
_thread_local = threading.local()
|
|
|
|
_locales_dir = os.path.join(os.path.dirname(__file__), '..', '..', '..', 'locales')
|
|
|
|
# Load language registry
|
|
with open(os.path.join(_locales_dir, 'languages.json'), 'r', encoding='utf-8') as f:
|
|
_languages = json.load(f)
|
|
|
|
# Load translation files
|
|
_translations = {}
|
|
for filename in os.listdir(_locales_dir):
|
|
if filename.endswith('.json') and filename != 'languages.json':
|
|
locale_name = filename[:-5]
|
|
if locale_name not in SUPPORTED_LOCALES:
|
|
continue
|
|
with open(os.path.join(_locales_dir, filename), 'r', encoding='utf-8') as f:
|
|
_translations[locale_name] = json.load(f)
|
|
|
|
|
|
def set_locale(locale: str):
|
|
"""Set a canonical locale for the current background thread."""
|
|
_thread_local.locale = normalize_locale(locale)
|
|
|
|
|
|
def get_locale() -> str:
|
|
if has_request_context():
|
|
return locale_from_accept_language(request.headers.get('Accept-Language', ''))
|
|
return normalize_locale(getattr(_thread_local, 'locale', DEFAULT_LOCALE))
|
|
|
|
|
|
def t(key: str, **kwargs) -> str:
|
|
locale = get_locale()
|
|
messages = _translations.get(locale, _translations.get(DEFAULT_LOCALE, {}))
|
|
|
|
value = messages
|
|
for part in key.split('.'):
|
|
if isinstance(value, dict):
|
|
value = value.get(part)
|
|
else:
|
|
value = None
|
|
break
|
|
|
|
if value is None:
|
|
value = _translations.get(DEFAULT_LOCALE, {})
|
|
for part in key.split('.'):
|
|
if isinstance(value, dict):
|
|
value = value.get(part)
|
|
else:
|
|
value = None
|
|
break
|
|
|
|
if value is None:
|
|
return key
|
|
|
|
if kwargs:
|
|
for k, v in kwargs.items():
|
|
value = value.replace(f'{{{k}}}', str(v))
|
|
|
|
return value
|
|
|
|
|
|
def get_language_instruction() -> str:
|
|
locale = get_locale()
|
|
lang_config = _languages.get(locale, _languages.get(DEFAULT_LOCALE, {}))
|
|
return lang_config.get(
|
|
'llmInstruction',
|
|
'IMPORTANT: Respond exclusively in Thai language.'
|
|
if locale == DEFAULT_LOCALE
|
|
else 'IMPORTANT: Respond exclusively in English language.',
|
|
)
|