File size: 2,476 Bytes
69be42f
 
 
 
dc3879e
 
 
a57a50a
 
 
69be42f
 
 
 
 
 
 
 
 
 
 
 
 
a57a50a
69be42f
 
 
a57a50a
 
69be42f
 
 
 
dc3879e
 
 
a57a50a
dc3879e
69be42f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a57a50a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Application configuration and settings.

[Task]: T009
[From]: specs/001-user-auth/plan.md

[Task]: T003
[From]: specs/004-ai-chatbot/plan.md

Extended for ChatKit migration with Gemini OpenAI-compatible endpoint.
[From]: specs/010-chatkit-migration/tasks.md - T008
"""
import os
from pydantic_settings import BaseSettings, SettingsConfigDict
from functools import lru_cache


class Settings(BaseSettings):
    """Application settings loaded from environment variables."""

    # Database
    database_url: str

    # JWT
    jwt_secret: str
    jwt_algorithm: str = "HS256"
    jwt_expiration_days: int = 7

    # CORS
    frontend_url: str

    # Environment
    environment: str = "development"

    # Gemini API (Phase III: AI Chatbot)
    gemini_api_key: str | None = None  # Optional for migration/setup
    gemini_model: str = "gemini-2.0-flash-exp"
    gemini_base_url: str = "https://generativelanguage.googleapis.com/v1beta/openai/"  # ChatKit migration

    model_config = SettingsConfigDict(
        env_file=".env",
        case_sensitive=False,
        # Support legacy Better Auth environment variables
        env_prefix="",
        extra="ignore"
    )


@lru_cache()
def get_settings() -> Settings:
    """Get cached settings instance.

    Returns:
        Settings: Application settings

    Raises:
        ValueError: If required environment variables are not set
    """
    return Settings()


def get_gemini_client():
    """Create and return an AsyncOpenAI client configured for Gemini.

    [From]: specs/010-chatkit-migration/research.md - Section 2
    [From]: specs/010-chatkit-migration/contracts/backend.md - Tool Contracts

    This client uses Gemini's OpenAI-compatible endpoint, allowing us to use
    the OpenAI SDK and Agents SDK with Gemini as the LLM provider.

    Returns:
        AsyncOpenAI: OpenAI client configured for Gemini

    Example:
        from openai import AsyncOpenAI
        from agents import set_default_openai_client

        client = get_gemini_client()
        set_default_openai_client(client)
    """
    from openai import AsyncOpenAI

    settings = get_settings()

    if not settings.gemini_api_key:
        raise ValueError(
            "GEMINI_API_KEY is not set. Please set it in your environment or .env file. "
            "Get your API key from https://aistudio.google.com"
        )

    return AsyncOpenAI(
        api_key=settings.gemini_api_key,
        base_url=settings.gemini_base_url,
    )