Skip to content
Merged
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
7 changes: 7 additions & 0 deletions hindsight-api-slim/hindsight_api/api/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def FieldWithDefault(default_factory: Callable, **kwargs) -> Any:
_get_tiktoken_encoding,
)
from hindsight_api.engine.providers.none_llm import LLMNotAvailableError
from hindsight_api.engine.reflect import ReflectToolCallError
from hindsight_api.engine.response_models import (
VALID_RECALL_FACT_TYPES,
DryRunExtractionResult,
Expand Down Expand Up @@ -4431,6 +4432,12 @@ async def api_reflect(
raise
except LLMNotAvailableError as e:
raise HTTPException(status_code=400, detail=str(e))
except ReflectToolCallError as e:
# The configured model/transport can't drive reflect's tool-calling loop.
# The request itself is fine, so this is a server-side (500) failure, not a
# 4xx -- but log at warning, not error: it's a misconfiguration, not a bug.
logger.warning("Reflect tool-calling failure in bank %s: %s", bank_id, e)
raise HTTPException(status_code=500, detail=str(e))
except TimeoutError as e:
logger.error("Timeout in /v1/default/banks/%s/reflect: %s", bank_id, e)
raise HTTPException(
Expand Down
59 changes: 57 additions & 2 deletions hindsight-api-slim/hindsight_api/engine/providers/mock_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from collections.abc import Callable
from typing import Any

from ..llm_interface import LLM_TOOL_CHOICE_AUTO, LLMInterface, LLMToolChoice
from ..llm_interface import LLM_TOOL_CHOICE_AUTO, LLMInterface, LLMToolChoice, LLMToolChoiceMode
from ..response_models import LLMToolCall, LLMToolCallResult, TokenUsage

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -266,7 +266,7 @@ async def call_with_tools(
else:
result = LLMToolCallResult(content="mock response", finish_reason="stop")
else:
result = LLMToolCallResult(content="mock response", finish_reason="stop")
result = self._compliant_tool_call(tools, tool_choice, messages)

# Set mock token usage on result if not already set
if result.input_tokens == 0:
Expand Down Expand Up @@ -297,6 +297,61 @@ async def call_with_tools(

return result

@staticmethod
def _compliant_tool_call(
tools: list[dict[str, Any]],
tool_choice: LLMToolChoice,
messages: list[dict[str, Any]],
) -> LLMToolCallResult:
"""Default tool response: simulate a compliant tool-calling model.

Real providers drive the reflect loop entirely through tool calls -- they
honor a forced tool choice, then finish via ``done`` -- and the reflect
agent now rejects a turn that yields no tool call at all (a transport that
can't tool-call raises ReflectToolCallError). So the mock must behave like a
working provider here rather than returning bare "mock response" prose,
which used to be salvaged as the answer. Only this default path is affected;
tests that script turns via ``_response_callback`` / ``_mock_response`` are not.
"""
tool_names = {t.get("function", {}).get("name") for t in tools}

def _mock_query() -> str:
for message in reversed(messages):
content = message.get("content")
if message.get("role") == "user" and isinstance(content, str) and content.strip():
return content[:200]
return "mock query"

# Honor a forced retrieval tool so the loop actually runs recall/search and
# gathers evidence (populates based_on for tests that assert on it).
if tool_choice.mode is LLMToolChoiceMode.NAMED and tool_choice.function_name in {
"search_mental_models",
"search_observations",
"recall",
}:
return LLMToolCallResult(
tool_calls=[
LLMToolCall(
id="mock_forced",
name=tool_choice.function_name,
arguments={"reason": "mock", "query": _mock_query()},
)
],
finish_reason="tool_calls",
)

# Auto turn: finish via the done tool, mirroring a model that has gathered
# enough. The reflect evidence guardrail handles the empty-bank case (no
# evidence -> forced text synthesis on the final iteration).
if "done" in tool_names:
return LLMToolCallResult(
tool_calls=[LLMToolCall(id="mock_done", name="done", arguments={"answer": "mock response"})],
finish_reason="tool_calls",
)

# No done tool offered (non-reflect tool call): fall back to plain text.
return LLMToolCallResult(content="mock response", finish_reason="stop")

@staticmethod
def _build_mock_facts(messages: list[dict]) -> dict:
"""Build a canned fact extraction response from the user message text.
Expand Down
3 changes: 2 additions & 1 deletion hindsight-api-slim/hindsight_api/engine/reflect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
3. Expand memories (get chunk/document context)
"""

from .agent import ReflectAgentResult, run_reflect_agent
from .agent import ReflectAgentResult, ReflectToolCallError, run_reflect_agent
from .models import ReflectAction, ReflectActionBatch

__all__ = [
"run_reflect_agent",
"ReflectAgentResult",
"ReflectToolCallError",
"ReflectAction",
"ReflectActionBatch",
]
Loading
Loading