106 lines
3.2 KiB
Python
106 lines
3.2 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from pathlib import Path
|
|
|
|
from airouter.chats import (
|
|
DirectChat,
|
|
DirectChatStore,
|
|
delete_visible_message,
|
|
last_visible_index,
|
|
repair_direct_messages,
|
|
run_direct_chat,
|
|
visible_message_indices,
|
|
)
|
|
from airouter.config import Config
|
|
from airouter.session import Session
|
|
|
|
from .fake import FakeProvider, turn_text, turn_with_tool
|
|
|
|
|
|
def test_direct_chat_store_roundtrip_and_no_runtime_fields(tmp_path: Path):
|
|
chat = DirectChat(
|
|
id="chat-one",
|
|
title="Test",
|
|
provider_id="ollama",
|
|
model="qwen3",
|
|
workspace=tmp_path,
|
|
messages=[
|
|
{"role": "system", "content": "system"},
|
|
{"role": "user", "content": "hello"},
|
|
{"role": "assistant", "content": "hi"},
|
|
],
|
|
)
|
|
store = DirectChatStore(tmp_path / "chats.json")
|
|
store.save({chat.id: chat})
|
|
loaded = store.load()
|
|
assert loaded["chat-one"].meta() == chat.meta()
|
|
store.delete(loaded, "chat-one")
|
|
assert store.load() == {}
|
|
|
|
|
|
def test_message_helpers_keep_tool_pairs_intact():
|
|
messages = [
|
|
{"role": "system", "content": "sys"},
|
|
{"role": "user", "content": "do it"},
|
|
{
|
|
"role": "assistant",
|
|
"content": "",
|
|
"tool_calls": [
|
|
{"id": "call-1", "function": {"name": "bash", "arguments": "{}"}}
|
|
],
|
|
},
|
|
{"role": "tool", "tool_call_id": "call-1", "content": "ok"},
|
|
{"role": "assistant", "content": "done"},
|
|
]
|
|
assert visible_message_indices(messages) == [1, 4]
|
|
assert last_visible_index(messages) == 4
|
|
updated = delete_visible_message(messages, 4)
|
|
assert updated[-1]["role"] == "tool"
|
|
dangling = messages[:3]
|
|
repair_direct_messages(dangling)
|
|
assert dangling[-1]["role"] == "tool"
|
|
assert dangling[-1]["tool_call_id"] == "call-1"
|
|
|
|
|
|
def test_direct_chat_uses_worker_tools(tmp_path: Path):
|
|
async def scenario():
|
|
config = Config()
|
|
config.worker_model = "local-fake"
|
|
config.intelligence = {"low": "local-fake", "medium": "local-fake", "high": "local-fake"}
|
|
provider = FakeProvider(
|
|
{
|
|
"local-fake": [
|
|
turn_with_tool("write_file", {"path": "direct.txt", "content": "hello"}),
|
|
turn_text("Готово"),
|
|
]
|
|
}
|
|
)
|
|
session = Session.create(
|
|
sessions_root=tmp_path / "sessions",
|
|
config=config,
|
|
provider=provider,
|
|
cwd=tmp_path,
|
|
)
|
|
chat = DirectChat(
|
|
id="chat-one",
|
|
title="Direct",
|
|
model="local-fake",
|
|
workspace=tmp_path,
|
|
)
|
|
chat.initialize(config, "local-fake")
|
|
events = []
|
|
answer = await run_direct_chat(
|
|
session,
|
|
chat,
|
|
user_text="Создай direct.txt",
|
|
emit=lambda kind, data: events.append((kind, data)),
|
|
)
|
|
assert answer == "Готово"
|
|
assert (tmp_path / "direct.txt").read_text() == "hello"
|
|
assert any(kind == "direct_tool_result" for kind, _ in events)
|
|
assert chat.usage.total_tokens == 30
|
|
await session.close()
|
|
|
|
asyncio.run(scenario())
|