diff options
| author | tanaka takashi <isazaka@gmail.com> | 2026-08-16 21:34:13 +0900 |
|---|---|---|
| committer | tanaka takashi <isazaka@gmail.com> | 2026-08-16 21:34:13 +0900 |
| commit | a9f083785a7e961a291e5366013fc89c0b801b01 (patch) | |
| tree | cb456d3ee40b00b8b475429a6e29f05c93e0efad /src/ollama_web/app.py | |
| parent | e45e2dbf160445891ec765ffb3e0e14f106cfb56 (diff) | |
refactor: streamline chat flow and improve diagnostics
Store server.log inside OLLAMA_WEB_DATA_DIR and manage the ollama-web file handler through the Starlette lifespan. Close and detach the handler on shutdown to prevent duplicate records and Windows file locks when applications are recreated.
Stop converting DuckDuckGo provider failures into empty search results. Record a query-safe traceback, re-raise the original exception, and let ToolRegistry expose the exception type and message as a tool error without adding or spoofing Referer headers.
Remove the unused non-streaming chat_with_tools implementation and its private helpers. Move capability, options, standard tool, MCP tool, and final tools=[] regression coverage onto the production streaming path.
Add integration coverage for data/server.log placement, handler cleanup, repeated lifespans, and provider 403 diagnostics. Update the Unreleased changelog entry.
Validated with 118 tests, Ruff, mypy, diff checks, real DuckDuckGo searches, successful result-page fetches, and observable 401/403 responses from sites that reject automated retrieval.
Diffstat (limited to 'src/ollama_web/app.py')
| -rw-r--r-- | src/ollama_web/app.py | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/src/ollama_web/app.py b/src/ollama_web/app.py index eeb3bae..9792c7b 100644 --- a/src/ollama_web/app.py +++ b/src/ollama_web/app.py @@ -3,6 +3,8 @@ from __future__ import annotations import logging +from collections.abc import AsyncIterator +from contextlib import asynccontextmanager from pathlib import Path from typing import Any @@ -42,17 +44,29 @@ class NoCacheStaticFiles(StaticFiles): _STATIC_DIR = Path(__file__).resolve().parent / "static" +@asynccontextmanager +async def _lifespan(app: Starlette) -> AsyncIterator[None]: + """Keep the application log handler scoped to the ASGI lifespan.""" + log_dir = Path(app.state.settings.data_dir).resolve() + log_dir.mkdir(parents=True, exist_ok=True) + handler = logging.FileHandler(str(log_dir / "server.log"), encoding="utf-8") + handler.setFormatter( + logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") + ) + logger = logging.getLogger("ollama_web") + previous_level = logger.level + logger.setLevel(logging.INFO) + logger.addHandler(handler) + try: + yield + finally: + logger.removeHandler(handler) + handler.close() + logger.setLevel(previous_level) + + def create_app() -> Starlette: """Build and return the configured Starlette application.""" - # Ensure application logs are written alongside uvicorn access logs. - log_path = Path(settings.data_dir).resolve().parent / "server.log" - log_path.parent.mkdir(parents=True, exist_ok=True) - handler = logging.FileHandler(str(log_path), encoding="utf-8") - handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")) - root = logging.getLogger() - root.setLevel(logging.INFO) - root.addHandler(handler) - routes = [ Route("/login", pages_route.login, name="login_page"), Route("/api/auth/login", auth_route.login, methods=["POST"], name="auth_login"), @@ -139,7 +153,7 @@ def create_app() -> Starlette: ) ) - app = Starlette(routes=routes, middleware=middleware) + app = Starlette(routes=routes, middleware=middleware, lifespan=_lifespan) app.state.settings = settings app.state.session_store = SessionStore(settings.data_dir) app.state.tool_names = ["web_search", "scrape_url", "search_and_fetch", "pdf_to_text"] |
