56 lines
2.0 KiB
Python
56 lines
2.0 KiB
Python
import os
|
|
import uuid
|
|
from dotenv import load_dotenv
|
|
from graph import build_graph
|
|
from langgraph.checkpoint.memory import InMemorySaver
|
|
import questionary
|
|
|
|
|
|
def main():
|
|
load_dotenv()
|
|
checkpoint = InMemorySaver()
|
|
graph = build_graph(checkpoint)
|
|
thread_id = str(uuid.uuid4())
|
|
config = {"configurable": {"thread_id": thread_id}}
|
|
print("=== Начало истории ===")
|
|
# Start the graph and handle interrupt
|
|
stream = graph.stream(config=config)
|
|
for chunk in stream:
|
|
if "__interrupt__" in chunk:
|
|
payload = chunk["__interrupt__"]
|
|
if payload.get("type") == "choice":
|
|
question = payload.get("question", "")
|
|
options = payload.get("options", [])
|
|
if not options:
|
|
print("Нет вариантов выбора.")
|
|
return
|
|
choice = questionary.select(question, choices=options).ask()
|
|
if choice is None:
|
|
print("Выход из игры.")
|
|
return
|
|
# Resume graph with the chosen option
|
|
resume_config = {"configurable": {"thread_id": thread_id, "choice": choice}}
|
|
for resume_chunk in graph.stream(resume_config):
|
|
if "__interrupt__" in resume_chunk:
|
|
print("Unexpected interrupt during ending.")
|
|
return
|
|
text = resume_chunk.get("text", "")
|
|
if text:
|
|
print(text, end="")
|
|
break
|
|
else:
|
|
text = chunk.get("text", "")
|
|
if text:
|
|
print(text, end="")
|
|
# After finishing, print the ending
|
|
final_state = checkpoint.get_state(thread_id)
|
|
ending = final_state.get("ending", "")
|
|
if ending:
|
|
print("\n\n=== Концовка ===")
|
|
print(ending)
|
|
else:
|
|
print("\nКонцовка не найдена.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |