update story teller

This commit is contained in:
bridge
2025-10-19 12:04:17 +08:00
parent e220b45b2a
commit 8eca9c0a65
5 changed files with 28 additions and 16 deletions

View File

@@ -57,11 +57,10 @@ class Battle(InstantAction):
result_text = f"{winner} 战胜了 {loser}{loser} 受伤{loser_damage}点,{winner} 也受伤{winner_damage}"
result_event = Event(self.world.month_stamp, result_text)
# 生成战斗小故事:直接复用已生成的事件文本
# 生成战斗小故事:使用便捷方法从参与者直接生成
target = self._get_target(avatar_name)
avatar_infos = StoryTeller.build_avatar_infos(self.avatar, target)
start_text = getattr(self, "_start_event_content", "") or result_event.content
story = StoryTeller.tell_story(avatar_infos, start_text, result_event.content, self.STORY_PROMPT)
story = StoryTeller.tell_from_actors(start_text, result_event.content, self.avatar, target, prompt=self.STORY_PROMPT)
story_event = Event(self.world.month_stamp, story)
return [result_event, story_event]

View File

@@ -140,13 +140,9 @@ class Breakthrough(TimedAction):
if True:
# 故事参与者:本体 +(可选)相关角色
if getattr(self, "_calamity_other", None) is not None:
avatar_infos = StoryTeller.build_avatar_infos(self.avatar, self._calamity_other)
else:
avatar_infos = StoryTeller.build_avatar_infos(self.avatar)
desc = CALAMITY_DESCRIPTIONS.get(str(calamity), "")
prompt = (STORY_PROMPT_BASE.format(calamity=str(calamity)) + (" " + desc if desc else "")).strip()
story = StoryTeller.tell_story(avatar_infos, core_text, ("突破成功" if result_ok else "突破失败"), prompt)
story = StoryTeller.tell_from_actors(core_text, ("突破成功" if result_ok else "突破失败"), self.avatar, getattr(self, "_calamity_other", None), prompt=prompt)
events.append(Event(self.world.month_stamp, story))
return events

View File

@@ -105,10 +105,9 @@ class DualCultivation(MutualAction):
result_event = Event(self.world.month_stamp, result_text)
events.append(result_event)
# 生成恋爱/双修小故事:使用通用故事模板
avatar_infos = StoryTeller.build_avatar_infos(self.avatar, target)
# 生成恋爱/双修小故事:使用 StoryTeller 便捷方法
start_text = self._start_event_content or result_event.content
story = StoryTeller.tell_story(avatar_infos, start_text, result_event.content, self.STORY_PROMPT)
story = StoryTeller.tell_from_actors(start_text, result_event.content, self.avatar, target, prompt=self.STORY_PROMPT)
story_event = Event(self.world.month_stamp, story)
events.append(story_event)
else:

View File

@@ -38,6 +38,8 @@ class StoryTeller:
"""
infos: Dict[str, dict] = {}
for av in avatars:
if av is None:
continue
infos[av.name] = av.get_info(detailed=True)
return infos
@@ -56,9 +58,25 @@ class StoryTeller:
"style": random.choice(story_styles),
"story_prompt": STORY_PROMPT or "",
}
data = get_prompt_and_call_llm(template_path, infos, mode="fast")
story = data["story"].strip()
return story
try:
data = get_prompt_and_call_llm(template_path, infos, mode="fast")
story = data.get("story", "").strip()
if story:
return story
except Exception:
# 避免过度 try/catch仅在外部依赖失败时提供降级
pass
# 降级文案(不中断主流程)
style = infos.get("style", "")
return f"{event}{res}{style}"
@staticmethod
def tell_from_actors(event: str, res: str, *actors: "Avatar", prompt: str | None = None) -> str:
"""
便捷方法:直接从参与者对象生成 avatar_infos 并讲述故事。
"""
avatar_infos = StoryTeller.build_avatar_infos(*actors)
return StoryTeller.tell_story(avatar_infos, event, res, prompt or "")
__all__ = ["StoryTeller"]