Commit 2b184d33 authored by Your Name's avatar Your Name

Fix circular import between codai.api.app and codai.api.images

- Create codai/api/state.py for shared global state functions
- images.py now imports get_load_mode from state instead of app
- app.py re-exports functions from state for backward compatibility
parent 405be1cb
......@@ -13,13 +13,36 @@ from fastapi.responses import FileResponse, JSONResponse
from codai.pydantic.textrequest import ModelList
from codai.models.manager import model_manager, multi_model_manager
# Import global state functions - re-export for backward compatibility
from codai.api.state import (
get_load_mode,
set_load_mode,
get_global_args,
set_global_args,
set_global_file_path,
get_global_debug,
set_global_debug,
)
# Global references to be set by coderai
# These will be imported/assigned after the app is created
# Aliases for backward compatibility
global_debug = False
global_file_path = None
def set_global_debug_wrapper(debug: bool):
"""Set the global debug flag."""
global global_debug
global_debug = debug
set_global_debug(debug)
def set_global_file_path_wrapper(path: str):
"""Set the global file path."""
global global_file_path
global_file_path = path
set_global_file_path(path)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Lifespan context manager for startup/shutdown."""
......@@ -73,30 +96,3 @@ async def get_file(filename: str):
if os.path.exists(file_path):
return FileResponse(file_path)
raise HTTPException(status_code=404, detail="File not found")
def set_global_debug(debug: bool):
"""Set the global debug flag."""
global global_debug
global_debug = debug
def set_global_file_path(path: str):
"""Set the global file path."""
global global_file_path
global_file_path = path
# Load mode - will be set by coderai
_load_mode = {"mode": "ondemand"}
def get_load_mode():
"""Get the current load mode."""
return _load_mode.get("mode", "ondemand")
def set_load_mode(mode: str):
"""Set the load mode from coderai."""
global _load_mode
_load_mode = mode
......@@ -14,7 +14,7 @@ from PIL import Image
# Import from codai modules
from codai.models.manager import multi_model_manager
from codai.pydantic.imagerequest import ImageGenerationRequest
from codai.api.app import get_load_mode
from codai.api.state import get_load_mode
# Global reference to be set by coderai
......
"""Global state for codai API modules."""
from typing import Any, Optional
# Global state variables
_args: Optional[Any] = None
_debug: bool = False
_file_path: Optional[str] = None
_system_prompt: Optional[str] = None
_tools_closer_prompt: bool = False
_grammar_guided_gen: bool = False
_load_mode: str = "ondemand"
def set_global_args(args: Any) -> None:
"""Set global arguments."""
global _args
_args = args
def get_global_args() -> Any:
"""Get global arguments."""
return _args
def set_global_debug(debug: bool) -> None:
"""Set global debug flag."""
global _debug
_debug = debug
def get_global_debug() -> bool:
"""Get global debug flag."""
return _debug
def set_global_file_path(path: Optional[str]) -> None:
"""Set global file path."""
global _file_path
_file_path = path
def get_global_file_path() -> Optional[str]:
"""Get global file path."""
return _file_path
def set_global_system_prompt(prompt: Optional[str]) -> None:
"""Set global system prompt."""
global _system_prompt
_system_prompt = prompt
def get_global_system_prompt() -> Optional[str]:
"""Get global system prompt."""
return _system_prompt
def set_global_tools_closer_prompt(enabled: bool) -> None:
"""Set global tools closer prompt flag."""
global _tools_closer_prompt
_tools_closer_prompt = enabled
def get_global_tools_closer_prompt() -> bool:
"""Get global tools closer prompt flag."""
return _tools_closer_prompt
def set_grammar_guided_gen(enabled: bool) -> None:
"""Set grammar-guided generation flag."""
global _grammar_guided_gen
_grammar_guided_gen = enabled
def get_grammar_guided_gen() -> bool:
"""Get grammar-guided generation flag."""
return _grammar_guided_gen
def set_load_mode(mode: str) -> None:
"""Set load mode (loadall, loadswap, nopreload, ondemand)."""
global _load_mode
_load_mode = mode
def get_load_mode() -> str:
"""Get load mode."""
return _load_mode
......@@ -27,7 +27,15 @@ def main():
# Import globals from codai modules
from codai.api import app
from codai.api.app import set_global_args
from codai.api.state import (
set_global_args,
set_global_debug,
set_global_system_prompt,
set_global_tools_closer_prompt,
set_global_file_path,
set_load_mode,
set_grammar_guided_gen,
)
from codai.models.manager import ModelManager, MultiModelManager
from codai.backends import detect_available_backends
from codai.models.cache import (
......@@ -79,7 +87,6 @@ def main():
# Set global file path for storing generated files
global_file_path = args.file_path
from codai.api.app import set_global_file_path
set_global_file_path(global_file_path)
if global_debug:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment