feat: add fixed questions and improve authentication
This commit is contained in:
1 parent
8af4fed89d
commit
4c9f75f31e
42 files changed
+4008
-200
No files matched your search
@@ -22,6 +22,7 @@ from langchain_core.messages import HumanMessage
|
||||
|
||||
from app.gateway.deps import get_concurrency_gate, get_run_context, get_run_manager, get_stream_bridge
|
||||
from app.gateway.llmwiki_rag import (
|
||||
LlmWikiRagContext,
|
||||
attach_llmwiki_rag_to_graph_input,
|
||||
build_llmwiki_rag_context,
|
||||
llmwiki_selection_requested,
|
||||
@@ -227,6 +228,77 @@ def normalize_input(raw_input: dict[str, Any] | None) -> dict[str, Any]:
|
||||
return raw_input
|
||||
|
||||
|
||||
def _plain_fixed_question(graph_input: dict[str, Any]) -> str | None:
|
||||
"""Return the latest plain-text human turn eligible for exact matching.
|
||||
|
||||
Messages carrying files or multimodal blocks are intentionally excluded:
|
||||
their answer depends on more than the visible question text, so replaying a
|
||||
global fixed answer would be surprising and unsafe.
|
||||
"""
|
||||
|
||||
messages = graph_input.get("messages")
|
||||
if not isinstance(messages, list) or not messages:
|
||||
return None
|
||||
message = messages[-1]
|
||||
if not isinstance(message, HumanMessage):
|
||||
return None
|
||||
additional_kwargs = getattr(message, "additional_kwargs", {}) or {}
|
||||
if any(
|
||||
additional_kwargs.get(key)
|
||||
for key in ("files", "uploaded_files", "attachments", "prompt_prefix")
|
||||
):
|
||||
return None
|
||||
content = getattr(message, "content", None)
|
||||
if isinstance(content, str):
|
||||
return content
|
||||
if not isinstance(content, list):
|
||||
return None
|
||||
parts: list[str] = []
|
||||
for item in content:
|
||||
if isinstance(item, str):
|
||||
parts.append(item)
|
||||
continue
|
||||
if not isinstance(item, Mapping):
|
||||
return None
|
||||
item_type = str(item.get("type") or "text")
|
||||
text = item.get("text")
|
||||
if item_type not in {"text", "input_text"} or not isinstance(text, str):
|
||||
return None
|
||||
parts.append(text)
|
||||
return "".join(parts)
|
||||
|
||||
|
||||
async def _find_fixed_answer(
|
||||
*,
|
||||
body: Any,
|
||||
request: Request,
|
||||
graph_input: dict[str, Any],
|
||||
run_scope: str,
|
||||
) -> dict[str, Any] | None:
|
||||
"""Look up an enabled exact match, falling back to the model on errors."""
|
||||
|
||||
if run_scope != RUN_SCOPE_EXTERNAL_ROOT or getattr(body, "command", None) is not None:
|
||||
return None
|
||||
assistant_id = str(getattr(body, "assistant_id", None) or _DEFAULT_ASSISTANT_ID)
|
||||
if assistant_id in {"canvas_agent", "ai_writing"}:
|
||||
return None
|
||||
context = getattr(body, "context", None) or {}
|
||||
if isinstance(context, Mapping) and context.get("writing_mode"):
|
||||
return None
|
||||
question = _plain_fixed_question(graph_input)
|
||||
if question is None:
|
||||
return None
|
||||
store = getattr(request.app.state, "fixed_question_store", None)
|
||||
if store is None:
|
||||
return None
|
||||
try:
|
||||
return await store.find_enabled(question)
|
||||
except Exception:
|
||||
# Configuration storage must never make normal chat unavailable.
|
||||
logger.warning("Fixed-question lookup failed; continuing with model", exc_info=True)
|
||||
return None
|
||||
|
||||
|
||||
_DEFAULT_ASSISTANT_ID = "lead_agent"
|
||||
|
||||
# Built-in LangGraph graph IDs registered in langgraph.json.
|
||||
@@ -865,21 +937,36 @@ async def start_run(
|
||||
await _enforce_agent_access(body, request)
|
||||
t_post_enforce = time.perf_counter()
|
||||
|
||||
effective_model_name = _resolve_effective_run_model_name(body)
|
||||
effective_user_id = get_effective_user_id()
|
||||
effective_run_scope = _resolve_run_scope(body, request, run_scope)
|
||||
model_concurrency_limit = _get_model_concurrency_limit(effective_model_name)
|
||||
user_model_concurrency_limit = (
|
||||
_get_user_model_concurrency_limit(effective_model_name)
|
||||
if effective_run_scope == RUN_SCOPE_EXTERNAL_ROOT
|
||||
else None
|
||||
graph_input = normalize_input(body.input)
|
||||
fixed_answer = await _find_fixed_answer(
|
||||
body=body,
|
||||
request=request,
|
||||
graph_input=graph_input,
|
||||
run_scope=effective_run_scope,
|
||||
)
|
||||
if fixed_answer is not None:
|
||||
effective_model_name = "fixed-answer"
|
||||
model_concurrency_limit = None
|
||||
user_model_concurrency_limit = None
|
||||
else:
|
||||
effective_model_name = _resolve_effective_run_model_name(body)
|
||||
model_concurrency_limit = _get_model_concurrency_limit(effective_model_name)
|
||||
user_model_concurrency_limit = (
|
||||
_get_user_model_concurrency_limit(effective_model_name)
|
||||
if effective_run_scope == RUN_SCOPE_EXTERNAL_ROOT
|
||||
else None
|
||||
)
|
||||
run_id = str(uuid.uuid4())
|
||||
|
||||
run_metadata = dict(body.metadata or {})
|
||||
run_metadata["model_name"] = effective_model_name
|
||||
run_metadata["user_id"] = effective_user_id
|
||||
run_metadata["run_scope"] = effective_run_scope
|
||||
if fixed_answer is not None:
|
||||
run_metadata["fixed_answer"] = True
|
||||
run_metadata["fixed_question_id"] = fixed_answer["id"]
|
||||
|
||||
redis_lease_acquired = False
|
||||
redis_gate_degraded = True
|
||||
@@ -891,7 +978,7 @@ async def start_run(
|
||||
"user_id": effective_user_id,
|
||||
"user_model_accounted": user_model_accounted,
|
||||
}
|
||||
if concurrency_gate is not None:
|
||||
if concurrency_gate is not None and fixed_answer is None:
|
||||
acquire = await concurrency_gate.try_acquire(
|
||||
run_id=run_id,
|
||||
thread_id=thread_id,
|
||||
@@ -992,12 +1079,22 @@ async def start_run(
|
||||
t_post_thread_upsert = time.perf_counter()
|
||||
|
||||
try:
|
||||
agent_factory = resolve_agent_factory(body.assistant_id)
|
||||
if fixed_answer is not None:
|
||||
from deerflow.agents.fixed_answer_agent import make_fixed_answer_agent
|
||||
|
||||
def agent_factory(config): # noqa: ANN001, ARG001 - worker factory contract
|
||||
return make_fixed_answer_agent(
|
||||
question=str(fixed_answer["question"]),
|
||||
answer=str(fixed_answer["answer"]),
|
||||
fixed_question_id=str(fixed_answer["id"]),
|
||||
tokens_per_second=int(fixed_answer.get("tokens_per_second", 100)),
|
||||
)
|
||||
else:
|
||||
agent_factory = resolve_agent_factory(body.assistant_id)
|
||||
except Exception:
|
||||
if concurrency_gate is not None and redis_lease_acquired:
|
||||
await concurrency_gate.release(**lease_metadata)
|
||||
raise
|
||||
graph_input = normalize_input(body.input)
|
||||
|
||||
# Admin-configurable prompt prefix: when the caller opted in via
|
||||
# context.prompt_prefix_enabled, tag the human message with the configured
|
||||
@@ -1007,14 +1104,18 @@ async def start_run(
|
||||
# applies once. We intentionally do NOT gate on thread-row existence: the
|
||||
# thread_meta row is frequently pre-created, which previously skipped the
|
||||
# prefix for genuine first turns.
|
||||
if _prompt_prefix_opted_in(body):
|
||||
if fixed_answer is None and _prompt_prefix_opted_in(body):
|
||||
prefix_settings = load_system_settings().prompt_prefix
|
||||
if prefix_settings.enabled and prefix_settings.prompt_prefix.strip():
|
||||
graph_input = _prepend_prompt_prefix(graph_input, prefix_settings.prompt_prefix.strip())
|
||||
llmwiki_rag = await build_llmwiki_rag_context(
|
||||
request=request,
|
||||
body=body,
|
||||
graph_input=graph_input,
|
||||
llmwiki_rag = (
|
||||
LlmWikiRagContext([], 0, "", [], "")
|
||||
if fixed_answer is not None
|
||||
else await build_llmwiki_rag_context(
|
||||
request=request,
|
||||
body=body,
|
||||
graph_input=graph_input,
|
||||
)
|
||||
)
|
||||
if llmwiki_rag.prompt or llmwiki_rag.sources:
|
||||
graph_input = attach_llmwiki_rag_to_graph_input(graph_input, llmwiki_rag)
|
||||
|
||||
Reference in new issue
Block a user