feat: 添加角色事件评论系统
- 新增 Opinion 模型,记录角色对事件的观点 - 新增 opinions.py,基于规则生成观点(支持5种事件×3种情绪) - 同一事件生命周期内每个 agent 只生成一次观点 - 观点同时记录到 agent.memory - 新增 3 个测试用例 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -171,3 +171,61 @@ def test_global_event_cooldown(client):
|
||||
assert data["global_event"]["triggered"] is False
|
||||
# 冷却递减
|
||||
assert data["world_state"]["global_meter"]["cooldown"] == 4
|
||||
|
||||
|
||||
def test_opinion_generated_on_world_effect(client):
|
||||
"""测试世界事件触发时生成观点"""
|
||||
# 触发世界事件
|
||||
events = [
|
||||
{"type": "chaos", "text": f"chaos{i}", "user": "test", "ts": i}
|
||||
for i in range(20)
|
||||
]
|
||||
resp = client.post("/step", json={"events": events})
|
||||
data = resp.json()
|
||||
|
||||
# 验证事件触发
|
||||
assert data["global_event"]["triggered"] is True
|
||||
assert len(data["world_state"]["world_effects"]) > 0
|
||||
|
||||
# 验证每个 agent 都有 opinion
|
||||
for agent_id, agent in data["world_state"]["agents"].items():
|
||||
assert agent["opinion"] is not None
|
||||
assert "about" in agent["opinion"]
|
||||
assert "text" in agent["opinion"]
|
||||
assert "tick" in agent["opinion"]
|
||||
|
||||
|
||||
def test_opinion_only_once_per_effect(client):
|
||||
"""测试同一事件只生成一次观点"""
|
||||
# 触发世界事件
|
||||
events = [
|
||||
{"type": "chaos", "text": f"chaos{i}", "user": "test", "ts": i}
|
||||
for i in range(20)
|
||||
]
|
||||
resp = client.post("/step", json={"events": events})
|
||||
data = resp.json()
|
||||
|
||||
# 记录第一次的观点
|
||||
first_opinions = {
|
||||
aid: agent["opinion"]["text"]
|
||||
for aid, agent in data["world_state"]["agents"].items()
|
||||
}
|
||||
|
||||
# 再次调用 step(事件仍在持续)
|
||||
resp2 = client.post("/step", json={"events": []})
|
||||
data2 = resp2.json()
|
||||
|
||||
# 观点应该保持不变(不重新生成)
|
||||
for aid, agent in data2["world_state"]["agents"].items():
|
||||
if agent["opinion"]:
|
||||
assert agent["opinion"]["text"] == first_opinions[aid]
|
||||
|
||||
|
||||
def test_opinion_cleared_when_no_effect(client):
|
||||
"""测试无活跃事件时观点为空"""
|
||||
resp = client.post("/step", json={"events": []})
|
||||
data = resp.json()
|
||||
|
||||
# 无世界事件时,opinion 应为 None
|
||||
for agent_id, agent in data["world_state"]["agents"].items():
|
||||
assert agent["opinion"] is None
|
||||
|
||||
Reference in New Issue
Block a user