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
13 changes: 12 additions & 1 deletion src/web/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
capable model pulled (see README).
"""
import configparser
import inspect
import json
import os
import queue
Expand Down Expand Up @@ -183,7 +184,17 @@ def _run_tool_call(name: str, args: dict, fn) -> str:
same tool with different arguments) that happen to need the same
underlying user/page get to share that cache hit, which a whole-tool
cache keyed on (tool, arguments) could never do."""
return fn(**args) if fn else json.dumps({"error": f"unknown tool {name}"})
if not fn:
return json.dumps({"error": f"unknown tool {name}"})
# Check the arguments against the signature first, so only a missing or
# unexpected argument is handed back to the model (which can then retry).
# A TypeError raised inside the tool is a bug and must not be mislabelled
# as bad arguments, so the call itself is not wrapped.
try:
inspect.signature(fn).bind(**args)
except TypeError as e:
return json.dumps({"error": f"Invalid tool arguments for {name}: {e}"})
return fn(**args)


app = FastAPI(title="Osintgram Web")
Expand Down
4 changes: 3 additions & 1 deletion src/web/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,16 @@ def search_location(place: str = "", limit: Optional[int] = 30) -> str:
return _to_json({"error": "Specify a place to search for."})
return _call(service.search_location, place=place, limit=limit)

def search_public_context(query: str) -> str:
def search_public_context(query: str = "") -> str:
"""Search the public web for context about a public place, venue, landmark, organization, or event.
Strictly privacy-guarded: queries containing personal usernames, profile URLs, or target handles will be rejected.
Args:
query: Name of the public landmark, venue, brand, or event to search for (e.g. 'Milan Cathedral', 'Uffizi Gallery').
"""
if not (query or "").strip():
return _to_json({"error": "Specify a public landmark, venue, brand, or event to search for."})
from src.web.web_search import search_public_web, resolve_web_search_config
cfg = resolve_web_search_config()
if not cfg.enabled:
Expand Down
44 changes: 44 additions & 0 deletions tests/test_app_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,47 @@ def test_llm_tool_call_subscriptable_and_properties():
assert tc.name == "get_user_info"
assert tc.arguments == {"target": "test"}
assert tc.get("name") == "get_user_info"


def test_run_tool_call_catches_type_error():
from src.web.app import _run_tool_call
import json
def dummy_tool(required_arg: str):
return "ok"
res = json.loads(_run_tool_call("dummy_tool", {}, dummy_tool))
assert "error" in res
assert "Invalid tool arguments" in res["error"]
assert "required_arg" in res["error"]


def test_run_tool_call_rejects_unexpected_argument():
from src.web.app import _run_tool_call
import json

def dummy_tool(limit: int = 10):
return "ok"

res = json.loads(_run_tool_call("dummy_tool", {"made_up": 1}, dummy_tool))
assert "Invalid tool arguments" in res["error"]
assert "made_up" in res["error"]


def test_run_tool_call_runs_a_valid_call():
from src.web.app import _run_tool_call

def dummy_tool(limit: int = 10):
return f"limit={limit}"

assert _run_tool_call("dummy_tool", {"limit": 3}, dummy_tool) == "limit=3"
assert _run_tool_call("dummy_tool", {}, dummy_tool) == "limit=10"


def test_run_tool_call_does_not_hide_a_bug_inside_the_tool():
import pytest
from src.web.app import _run_tool_call

def buggy_tool():
return None + 1 # a TypeError that has nothing to do with the arguments

with pytest.raises(TypeError):
_run_tool_call("buggy_tool", {}, buggy_tool)
10 changes: 10 additions & 0 deletions tests/test_web_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ def test_web_search_wikipedia_provider():
results = search_public_web("Louvre Museum")
assert len(results) > 0
assert any("louvre" in r["title"].lower() or "louvre" in r["snippet"].lower() for r in results)


def test_search_public_context_empty_query():
from src.web.tools import build_tools
import json
tools = build_tools(None)
search_tool = next(t for t in tools if t.__name__ == "search_public_context")
res = json.loads(search_tool())
assert "error" in res
assert "Specify a public landmark" in res["error"]
Loading