Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ API 容器会在进程启动前按知识后端准备索引。`bm25` 模式根据
| --- | --- |
| `MEDGUIDE_MODE` | 外部模型与共享存储设为 `production`;单实例本地推理使用独立 `local` 配置 |
| `MEDGUIDE_LOCAL_LLM_BASE_URL` / `MEDGUIDE_LOCAL_LLM_MODEL` | `local` 模式专用的本机模型地址和模型 ID,不继承 `OPENAI_*` |
| `MEDGUIDE_LOCAL_LLM_PROVIDER` | 本地推理服务类型:`llama.cpp`(默认,发送 `chat_template_kwargs.enable_thinking=false`)或 `ollama`(发送 `reasoning_effort=none`);非法值启动即报错 |
| `MEDGUIDE_LOCAL_LLM_TIMEOUT_SECONDS` / `MEDGUIDE_LOCAL_LLM_MAX_TOKENS` | 本地生成预算,默认 120 秒 / 512 token |
| `MEDGUIDE_DOMAIN` | 已解析到部署主机的公网域名;TLS 网关启动时校验,缺失或非法则失败关闭 |
| `MEDGUIDE_HTTP_PORT` / `MEDGUIDE_HTTPS_PORT` | TLS 网关对外发布端口,默认 80/443 |
Expand Down
40 changes: 38 additions & 2 deletions backend/app/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class InvalidCredentialsError(AuthError):
"""Raised for an unknown username or an incorrect password."""


class UnknownUserError(AuthError):
"""Raised when a session is requested for a username that is not registered."""


class InvalidUsernameError(AuthError):
"""Raised when a username cannot be represented safely."""

Expand Down Expand Up @@ -323,6 +327,16 @@ def verify_credentials(self, username: str, password: str) -> str:

def create_session(self, username: str) -> tuple[str, int]:
normalized = normalize_username(username)
with self._connect() as connection:
row = connection.execute(
"SELECT 1 FROM users WHERE username = ?",
(normalized,),
).fetchone()
if row is None:
# Check first so a missing user is reported as such, instead of
# surfacing as a foreign-key IntegrityError retried as a
# "could not allocate a unique authentication token" failure.
raise UnknownUserError("user does not exist")
now = int(self._clock())
expires_at = now + self.session_ttl_seconds
for _ in range(3):
Expand All @@ -338,7 +352,13 @@ def create_session(self, username: str) -> tuple[str, int]:
(self._token_hash(token), normalized, now, expires_at),
)
return token, expires_at
except sqlite3.IntegrityError:
except sqlite3.IntegrityError as exc:
# Only a token-hash collision is worth retrying; any other
# constraint failure (e.g. a foreign key, if the user was
# deleted between the check above and the insert) must
# surface immediately instead of being misreported.
if not _is_unique_constraint_error(exc):
raise
continue
raise RuntimeError("could not allocate a unique authentication token")

Expand Down Expand Up @@ -593,6 +613,16 @@ def create_session(self, username: str) -> tuple[str, int]:
from sqlalchemy.exc import IntegrityError

normalized = normalize_username(username)
with self.engine.connect() as connection:
row = connection.execute(
text("SELECT 1 FROM medguide_users WHERE username = :username"),
{"username": normalized},
).first()
if row is None:
# Check first so a missing user is reported as such, instead of
# surfacing as a foreign-key IntegrityError retried as a
# "could not allocate a unique authentication token" failure.
raise UnknownUserError("user does not exist")
now = int(self._clock())
expires_at = now + self.session_ttl_seconds
for _ in range(3):
Expand All @@ -617,7 +647,13 @@ def create_session(self, username: str) -> tuple[str, int]:
},
)
return token, expires_at
except IntegrityError:
except IntegrityError as exc:
# Only a token-hash collision is worth retrying; any other
# constraint failure (e.g. a foreign key, if the user was
# deleted between the check above and the insert) must
# surface immediately instead of being misreported.
if not _is_unique_constraint_error(exc):
raise
continue
raise RuntimeError("could not allocate a unique authentication token")

Expand Down
14 changes: 11 additions & 3 deletions backend/app/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def _embedding_timeout() -> float:
return 15.0


_EMBEDDING_ENDPOINT_SUFFIXES = ("/embeddings", "/chat/completions", "/chaat/completions")
_EMBEDDING_ENDPOINT_SUFFIXES = ("/embeddings", "/chat/completions")


def _normalize_embedding_base_url(value: str | None) -> str | None:
Expand All @@ -299,11 +299,19 @@ def _normalize_embedding_base_url(value: str | None) -> str | None:
if not raw:
return None
parsed = urlsplit(raw)
if not parsed.scheme or not parsed.netloc:
if parsed.scheme.lower() not in {"http", "https"} or not parsed.netloc:
raise ValueError("Embedding base URL must include an HTTP(S) host")
path = parsed.path.rstrip("/")
for suffix in _EMBEDDING_ENDPOINT_SUFFIXES:
if path.lower().endswith(suffix):
path = path[: -len(suffix)].rstrip("/")
break
return urlunsplit((parsed.scheme, parsed.netloc, path, parsed.query, parsed.fragment)).rstrip("/")
else:
# See _normalize_base_url: a mistyped endpoint must fail closed
# instead of being silently truncated to a wrong base URL.
if path.lower().endswith("/completions"):
raise ValueError("Embedding base URL looks like a mistyped completion endpoint")
# Query strings and fragments are never part of a base URL; drop them
# instead of forwarding them (and any credentials they carry) with
# every request.
return urlunsplit((parsed.scheme, parsed.netloc, path, "", "")).rstrip("/")
21 changes: 17 additions & 4 deletions backend/app/infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,25 @@ class RedisSessionAdapter:

_UPSERT_FEEDBACK_SCRIPT = """
local previous = redis.call('HGET', KEYS[1], ARGV[1])
local stored_comment = redis.call('HGET', KEYS[1], ARGV[1] .. ':commented')
local comment_recorded = 0
if ARGV[3] == '1' and stored_comment ~= ARGV[5] then
redis.call('HSET', KEYS[1], ARGV[1] .. ':commented', ARGV[5])
if not stored_comment then
redis.call('HINCRBY', KEYS[2], 'feedback_commented', 1)
end
comment_recorded = 1
end
if previous == ARGV[2] then
redis.call('EXPIRE', KEYS[1], ARGV[4])
redis.call('EXPIRE', KEYS[2], ARGV[4])
return 0
return comment_recorded
end
if previous == 'up' or previous == 'down' then
redis.call('HINCRBY', KEYS[2], 'feedback_' .. previous, -1)
end
redis.call('HSET', KEYS[1], ARGV[1], ARGV[2])
redis.call('HINCRBY', KEYS[2], 'feedback_' .. ARGV[2], 1)
if ARGV[3] == '1' and not previous then
redis.call('HINCRBY', KEYS[2], 'feedback_commented', 1)
end
redis.call('EXPIRE', KEYS[1], ARGV[4])
redis.call('EXPIRE', KEYS[2], ARGV[4])
return 1
Expand Down Expand Up @@ -542,6 +548,12 @@ def upsert_feedback(
if not callable(evaluator):
return None
key = hashlib.sha256(f"{session_id}\x00{message_id}".encode("utf-8")).hexdigest()
# Only a hash of the comment reaches the metrics store, so a
# resubmitted rating with a new comment still counts as a change
# without storing user text in the metrics hash.
comment_hash = (
hashlib.sha256(comment.encode("utf-8")).hexdigest()[:16] if comment else ""
)
changed = evaluator(
self._UPSERT_FEEDBACK_SCRIPT,
2,
Expand All @@ -551,6 +563,7 @@ def upsert_feedback(
rating,
"1" if comment else "0",
self.ttl_seconds,
comment_hash,
)
return bool(int(changed))
except Exception as exc:
Expand Down
8 changes: 7 additions & 1 deletion backend/app/ingestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,13 @@ def _contains_nested_credentials(cls, value: str) -> bool:
break
decoded = next_value
normalized = unicodedata.normalize("NFKC", decoded)
for match in re.finditer(r"(?:^|[?&;\s])([^=&?#\s]+)\s*=", normalized):
# A nested URL carrying userinfo (scheme://user:password@host)
# hides credentials that no key=value scan would ever see.
if re.search(r"://[^/\s]*@", normalized):
return True
# `#` and `/` are delimiters too: a credential smuggled in as
# `.../x%23token=secret` or `.../x/token=secret` must be caught.
for match in re.finditer(r"(?:^|[?&;#/\s])([^=&?#\s]+)\s*=", normalized):
if cls._sensitive_query_key(match.group(1)):
return True
return False
Expand Down
42 changes: 35 additions & 7 deletions backend/app/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ def __init__(
mode = os.getenv("MEDGUIDE_MODE", "production").strip().lower()
self.local = mode == "local"
self.required = mode not in {"offline", "test"}
# The local reasoning toggle is provider-specific: llama.cpp honours
# chat_template_kwargs.enable_thinking=false, while Ollama ignores
# it and needs reasoning_effort="none" (issue #1). An unknown
# provider fails closed at startup instead of silently sending the
# wrong toggle to the local model.
self.local_llm_provider = self._read_local_provider() if self.local else None
configured_model = (
os.getenv("MEDGUIDE_LOCAL_LLM_MODEL", "")
if self.local else (model if model is not None else os.getenv("OPENAI_MODEL", ""))
Expand Down Expand Up @@ -192,6 +198,13 @@ def _read_max_tokens() -> int:
except ValueError:
return 1600

@staticmethod
def _read_local_provider() -> str:
provider = os.getenv("MEDGUIDE_LOCAL_LLM_PROVIDER", "llama.cpp").strip().lower()
if provider not in {"llama.cpp", "ollama"}:
raise ValueError("MEDGUIDE_LOCAL_LLM_PROVIDER must be llama.cpp or ollama")
return provider

@staticmethod
def _read_local_timeout() -> float:
try:
Expand All @@ -208,10 +221,15 @@ def _read_local_max_tokens() -> int:
return 512

def _local_request_options(self) -> dict[str, Any]:
return (
{"extra_body": {"chat_template_kwargs": {"enable_thinking": False}}}
if getattr(self, "local", False) else {}
)
if not getattr(self, "local", False):
return {}
# Shared by the readiness probe and generate, so one provider
# choice covers both. Ollama ignores llama.cpp's thinking toggle
# and spends the probe budget on reasoning instead, leaving an
# empty message body (issue #1).
if getattr(self, "local_llm_provider", "llama.cpp") == "ollama":
return {"extra_body": {"reasoning_effort": "none"}}
return {"extra_body": {"chat_template_kwargs": {"enable_thinking": False}}}

def generate(self, query: str, risk: str, context: str) -> str | None:
if not self.ensure_ready():
Expand Down Expand Up @@ -291,7 +309,7 @@ async def aclose(self) -> None:
self.close()


_COMPLETION_ENDPOINT_SUFFIXES = ("/chat/completions", "/chaat/completions")
_COMPLETION_ENDPOINT_SUFFIXES = ("/chat/completions",)


def _normalize_local_base_url(value: str) -> str:
Expand Down Expand Up @@ -332,11 +350,21 @@ def _normalize_base_url(value: str | None) -> str | None:
if not raw:
return None
parsed = urlsplit(raw)
if not parsed.scheme or not parsed.netloc:
if parsed.scheme.lower() not in {"http", "https"} or not parsed.netloc:
raise ValueError("OpenAI base URL must include an HTTP(S) host")
path = parsed.path.rstrip("/")
for suffix in _COMPLETION_ENDPOINT_SUFFIXES:
if path.lower().endswith(suffix):
path = path[: -len(suffix)].rstrip("/")
break
return urlunsplit((parsed.scheme, parsed.netloc, path, parsed.query, parsed.fragment)).rstrip("/")
else:
# A path ending in some other ".../completions" is almost certainly
# a mistyped endpoint (e.g. "/chaat/completions"). Fail closed
# instead of silently truncating it to a base URL that then hits
# the wrong endpoint.
if path.lower().endswith("/completions"):
raise ValueError("OpenAI base URL looks like a mistyped completion endpoint")
# Query strings and fragments are never part of a base URL; drop them
# instead of forwarding them (and any credentials they carry) with
# every request.
return urlunsplit((parsed.scheme, parsed.netloc, path, "", "")).rstrip("/")
64 changes: 54 additions & 10 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,13 @@ def _cors_origins() -> list[str]:
origins = [item.strip().rstrip("/") for item in raw.split(",") if item.strip()]
# Credentials and wildcard origins are mutually exclusive. Treat a
# wildcard as an explicit configuration error rather than silently
# downgrading browser security.
# downgrading browser security: failing startup is the only honest
# signal, otherwise an operator would believe CORS is open.
if "*" in origins:
return []
raise ValueError(
"MEDGUIDE_CORS_ORIGINS must not contain '*': wildcard origins cannot "
"be combined with credentials; list explicit origins instead."
)
return origins


Expand Down Expand Up @@ -565,9 +569,13 @@ def _require_access(
# A proxy-validated cookie can expire or be revoked before this handler.
# Cookie-bearing offline requests must not fall back to anonymous access;
# production service-header priority remains unchanged.
if principal is None and (
getattr(current, "production", False) or getattr(current, "local", False) or session_token is not None
):
# Issue #6: authentication is required in every mode. Test/offline used
# to let requests without any credential through (while rejecting an
# invalid cookie), which exposed protected APIs and other users'
# sessions anonymously. There is intentionally no anonymous fallback
# switch: /api/health and /api/ready stay public for probes, and every
# other caller must present a valid session cookie or service token.
if principal is None:
raise HTTPException(status_code=401, detail="请先登录")
_require_runtime(current)
return principal
Expand All @@ -580,11 +588,19 @@ def _require_service_access(
api_key: str | None = None,
) -> str | None:
_require_secure_transport(current, request)
if not current.production:
return None
principal = _principal_from_headers(current, authorization, api_key)
_require_runtime(current)
return principal
if current.production:
principal = _principal_from_headers(current, authorization, api_key)
_require_runtime(current)
return principal
# Issue #6: the operator endpoint was wide open outside production
# (this function returned before any check). No service API tokens are
# configured in test/offline/local modes, so a logged-in user session is
# the credential here; anonymous access is never allowed. Production
# keeps the stricter service-token-only rule.
username = _username_from_cookie(current, _session_token_from_request(current, request))
if username is None:
raise HTTPException(status_code=401, detail="请先登录")
return f"user:{username}"


def _message_fingerprint(message: str) -> str:
Expand Down Expand Up @@ -1022,6 +1038,34 @@ def create_session(
return SessionCreateResponse(session_id=state["session_id"], state=public_state(state))


@app.get("/api/sessions")
def list_sessions(
http_request: Request,
authorization: str | None = Header(default=None),
api_key: str | None = Header(default=None, alias="X-API-Key"),
) -> dict[str, Any]:
# Registered before /api/sessions/{session_id} so "sessions" is never
# treated as a session id.
current = get_services()
owner_id = _require_access(
current,
http_request,
authorization,
api_key,
_session_token_from_request(current, http_request),
)
try:
sessions = current.store.list_sessions(owner_id)
except SessionBackendError as exc:
raise HTTPException(status_code=503, detail=str(exc)) from exc
return {
"sessions": sessions,
# False when the shared backend cannot enumerate sessions; the list
# is then empty on purpose rather than partial. See store.py.
"complete": current.store.listing_complete,
}


@app.get("/api/sessions/{session_id}")
def get_session(
session_id: str,
Expand Down
24 changes: 21 additions & 3 deletions backend/app/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class MetricsStore:
feedback_by_message: dict[str, Literal["up", "down"]] = field(default_factory=dict)
backend: Any | None = field(default=None, repr=False, compare=False)
_recorded_responses: set[str] = field(default_factory=set, repr=False)
_commented_keys: set[str] = field(default_factory=set, repr=False)
_feedback_comments: dict[str, str] = field(default_factory=dict, repr=False)
_lock: threading.Lock = field(default_factory=threading.Lock, repr=False)

def record(self, state: WorkflowState) -> None:
Expand Down Expand Up @@ -94,18 +96,34 @@ def add_feedback(
with self._lock:
previous: str | None = None
feedback_key = f"{session_id}:{message_id}" if session_id and message_id else message_id
comment_recorded = False
if comment:
if feedback_key:
# "commented" counts messages that carry a comment, not
# feedback submissions: a later comment on the same
# message is stored, but the message is counted once.
# The latest comment text is kept so a resubmitted
# rating with a new comment is a real change, not a
# silent drop.
if feedback_key not in self._commented_keys:
self._commented_keys.add(feedback_key)
self.feedback["commented"] += 1
if self._feedback_comments.get(feedback_key) != comment:
self._feedback_comments[feedback_key] = comment
comment_recorded = True
else:
self.feedback["commented"] += 1
comment_recorded = True
if feedback_key:
previous = self.feedback_by_message.get(feedback_key)
if previous == rating:
return False
return comment_recorded
if previous:
self.feedback[previous] -= 1
if self.feedback[previous] <= 0:
self.feedback.pop(previous, None)
self.feedback_by_message[feedback_key] = rating
self.feedback[rating] += 1
if comment and previous is None:
self.feedback["commented"] += 1
return True

def snapshot(self) -> dict:
Expand Down
Loading