101 lines
3.1 KiB
Python
101 lines
3.1 KiB
Python
from gfa_core.mas.agent_state_repository import SQLiteAgentStateRepository
|
|
from gfa_core.mas.agents.router_runtime import RouteDecision, RouterRuntime
|
|
from gfa_core.mas.persistent_conversation import PersistentConversationStore
|
|
|
|
|
|
def test_begin_turn_imports_and_returns_complete_valid_history(tmp_path):
|
|
repository = SQLiteAgentStateRepository(tmp_path / "agent-state.sqlite3")
|
|
repository.initialize()
|
|
store = PersistentConversationStore(repository)
|
|
client_history = [
|
|
{
|
|
"role": "user" if index % 2 == 0 else "assistant",
|
|
"content": f"历史消息-{index:02d}",
|
|
}
|
|
for index in range(14)
|
|
]
|
|
|
|
turn = store.begin_turn(
|
|
session_id="full-history-session",
|
|
job_id="job-full-history",
|
|
query="当前问题",
|
|
client_history=client_history,
|
|
)
|
|
|
|
assert list(turn.history) == client_history
|
|
stored = repository.list_messages(turn.conversation_id)
|
|
assert [item.content for item in stored] == [
|
|
*(item["content"] for item in client_history),
|
|
"当前问题",
|
|
]
|
|
|
|
|
|
def test_begin_turn_returns_all_persisted_messages_not_only_latest_ten(tmp_path):
|
|
repository = SQLiteAgentStateRepository(tmp_path / "agent-state.sqlite3")
|
|
repository.initialize()
|
|
store = PersistentConversationStore(repository)
|
|
first_turn = store.begin_turn(
|
|
session_id="persisted-full-history-session",
|
|
job_id="job-full-history",
|
|
query="第一个问题",
|
|
client_history=[],
|
|
)
|
|
store.finish_turn(
|
|
first_turn,
|
|
assistant_message="第一个回答",
|
|
interaction_kind="independent",
|
|
action="reply",
|
|
)
|
|
for index in range(1, 7):
|
|
turn = store.begin_turn(
|
|
session_id="persisted-full-history-session",
|
|
job_id="job-full-history",
|
|
query=f"问题-{index}",
|
|
client_history=[],
|
|
)
|
|
store.finish_turn(
|
|
turn,
|
|
assistant_message=f"回答-{index}",
|
|
interaction_kind="supplement",
|
|
action="reply",
|
|
)
|
|
|
|
final_turn = store.begin_turn(
|
|
session_id="persisted-full-history-session",
|
|
job_id="job-full-history",
|
|
query="最终问题",
|
|
client_history=[],
|
|
)
|
|
|
|
assert len(final_turn.history) == 14
|
|
assert final_turn.history[0]["content"] == "第一个问题"
|
|
assert final_turn.history[-1]["content"] == "回答-6"
|
|
|
|
|
|
def test_main_agent_prompt_receives_the_complete_history():
|
|
history = [
|
|
{
|
|
"role": "user" if index % 2 == 0 else "assistant",
|
|
"content": f"消息-{index}",
|
|
}
|
|
for index in range(12)
|
|
]
|
|
|
|
messages = RouterRuntime._initial_messages(
|
|
query="当前问题",
|
|
history=history,
|
|
session_id="full-history-session",
|
|
job_id="job-full-history",
|
|
decision=RouteDecision(
|
|
goal="general",
|
|
needs=[],
|
|
subject="继续普通问答",
|
|
interaction_kind="supplement",
|
|
),
|
|
goal_ledger=None,
|
|
)
|
|
|
|
assert [message.content for message in messages[1:-1]] == [
|
|
f"消息-{index}" for index in range(12)
|
|
]
|