forked from retoor/devplacepy
Add openai_gateway/apps.py as the single list of internal gateway apps and send X-OpenRouter-Title (plus legacy X-Title and HTTP-Referer) per app to every upstream. Label the Devii agent, bots, modifier, quiz grading, SEO metadata, community and sync callers. Shorten two app references that exceeded the 30 character limit and were recorded as default. Also includes the pending Muse Code, route health and Responses work. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
154 lines
4.8 KiB
Python
154 lines
4.8 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
|
|
from devplacepy.services.openai_gateway import muse_code
|
|
|
|
|
|
def test_headers_include_the_expected_client_identity_fields():
|
|
headers = muse_code.muse_impersonation_headers()
|
|
assert set(headers) == {
|
|
"User-Agent",
|
|
"X-Client-Id",
|
|
"X-Tbh-Session-Id",
|
|
"X-Meta-Ai-Gateway-Session-Id",
|
|
"X-Tbh-Thread-Id",
|
|
"X-Tbh-Root-Session-Id",
|
|
"X-Tbh-Agent-Role",
|
|
"X-Tbh-Contributor-Mode",
|
|
}
|
|
assert headers["User-Agent"].startswith("muse-build/")
|
|
assert "(non-interactive;" in headers["User-Agent"]
|
|
assert headers["X-Client-Id"] == "tbh:exec"
|
|
assert headers["X-Tbh-Agent-Role"] == "main"
|
|
|
|
|
|
def test_reserved_tool_names_cover_the_measured_denylist():
|
|
assert muse_code.MUSE_RESERVED_TOOL_NAMES == frozenset(
|
|
{
|
|
"workflow",
|
|
"read_file",
|
|
"search",
|
|
"write_file",
|
|
"edit_file",
|
|
"read_memory",
|
|
"add_memory",
|
|
"edit_memory",
|
|
"web_search",
|
|
"bash",
|
|
"bash_input",
|
|
"cron_create",
|
|
"cron_delete",
|
|
"cron_list",
|
|
"get_goal",
|
|
"create_goal",
|
|
"update_goal",
|
|
"report_progress",
|
|
"read_skill",
|
|
"write_todos",
|
|
}
|
|
)
|
|
|
|
|
|
def _func_tool(name):
|
|
return {"type": "function", "function": {"name": name, "arguments": "{}"}}
|
|
|
|
|
|
def test_tool_aliases_cover_declared_and_history_names():
|
|
payload = {
|
|
"tools": [_func_tool("bash"), _func_tool("innocent")],
|
|
"messages": [
|
|
{
|
|
"role": "assistant",
|
|
"tool_calls": [_func_tool("read_file")],
|
|
}
|
|
],
|
|
}
|
|
aliases = muse_code.tool_aliases_for(payload)
|
|
assert aliases == {"ext_bash": "bash", "ext_read_file": "read_file"}
|
|
|
|
|
|
def test_tool_aliases_grow_past_collisions():
|
|
payload = {"tools": [_func_tool("bash"), _func_tool("ext_bash")]}
|
|
aliases = muse_code.tool_aliases_for(payload)
|
|
assert aliases == {"ext_ext_bash": "bash"}
|
|
|
|
|
|
def test_tool_aliases_empty_when_clean():
|
|
assert muse_code.tool_aliases_for({"tools": [_func_tool("innocent")]}) == {}
|
|
assert muse_code.tool_aliases_for({}) == {}
|
|
|
|
|
|
def test_alias_chat_tools_rewrites_every_name_shape():
|
|
payload = {
|
|
"tools": [_func_tool("bash")],
|
|
"tool_choice": {"type": "function", "function": {"name": "bash"}},
|
|
"messages": [
|
|
{
|
|
"role": "assistant",
|
|
"tool_calls": [_func_tool("bash")],
|
|
}
|
|
],
|
|
}
|
|
renamed = muse_code.alias_chat_tools(
|
|
payload, muse_code.tool_aliases_for(payload)
|
|
)
|
|
assert renamed is not payload
|
|
assert renamed["tools"][0]["function"]["name"] == "ext_bash"
|
|
assert renamed["tool_choice"]["function"]["name"] == "ext_bash"
|
|
assert (
|
|
renamed["messages"][0]["tool_calls"][0]["function"]["name"]
|
|
== "ext_bash"
|
|
)
|
|
assert payload["tools"][0]["function"]["name"] == "bash"
|
|
|
|
|
|
def test_alias_chat_tools_rewrites_allowed_tools_entries():
|
|
payload = {
|
|
"tool_choice": {
|
|
"type": "allowed_tools",
|
|
"mode": "auto",
|
|
"tools": [_func_tool("bash"), _func_tool("innocent")],
|
|
}
|
|
}
|
|
renamed = muse_code.alias_chat_tools(payload, {"ext_bash": "bash"})
|
|
entries = renamed["tool_choice"]["tools"]
|
|
assert entries[0]["function"]["name"] == "ext_bash"
|
|
assert entries[1]["function"]["name"] == "innocent"
|
|
|
|
|
|
def test_alias_chat_tools_passthrough_when_clean():
|
|
payload = {"tools": [_func_tool("innocent")]}
|
|
assert muse_code.alias_chat_tools(payload, {}) is payload
|
|
|
|
|
|
def test_restore_completion_and_chunk_names():
|
|
aliases = {"ext_bash": "bash"}
|
|
comp = {
|
|
"choices": [
|
|
{"message": {"tool_calls": [_func_tool("ext_bash"), _func_tool("x")]}},
|
|
]
|
|
}
|
|
muse_code.restore_completion_names(comp, aliases)
|
|
names = [c["function"]["name"] for c in comp["choices"][0]["message"]["tool_calls"]]
|
|
assert names == ["bash", "x"]
|
|
chunk = {"choices": [{"delta": {"tool_calls": [_func_tool("ext_bash")]}}]}
|
|
muse_code.restore_chunk_names(chunk, aliases)
|
|
assert chunk["choices"][0]["delta"]["tool_calls"][0]["function"]["name"] == "bash"
|
|
assert muse_code.restore_completion_names(comp, {}) is comp
|
|
assert muse_code.restore_chunk_names(chunk, {}) is chunk
|
|
|
|
|
|
def test_session_id_is_stable_and_shared_across_calls():
|
|
first = muse_code.muse_impersonation_headers()
|
|
second = muse_code.muse_impersonation_headers()
|
|
session_keys = (
|
|
"X-Tbh-Session-Id",
|
|
"X-Meta-Ai-Gateway-Session-Id",
|
|
"X-Tbh-Thread-Id",
|
|
"X-Tbh-Root-Session-Id",
|
|
)
|
|
shared = first["X-Tbh-Session-Id"]
|
|
assert len(shared) == 36 and shared.count("-") == 4
|
|
for key in session_keys:
|
|
assert first[key] == shared
|
|
assert second[key] == shared
|