-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.py
More file actions
89 lines (70 loc) · 2.37 KB
/
Copy pathrun.py
File metadata and controls
89 lines (70 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""SERA CLI entry point.
Usage:
python run.py "What is the price of Bitcoin?"
python run.py --chat-id <ulid> "and ETH?"
python run.py --new-chat "..."
"""
from __future__ import annotations
import argparse
import asyncio
import sys
from dotenv import load_dotenv
load_dotenv() # load .env before importing sera modules
from sera import config # noqa: E402
from sera.agent import SERAAgent # noqa: E402
from sera.emitter import ConsoleEmitter # noqa: E402
from sera.session import ChatSession, SessionStore # noqa: E402
from sera.tracing import init_tracing, shutdown_tracing # noqa: E402
async def amain() -> int:
parser = argparse.ArgumentParser(prog="sera", description="Standalone crypto agent.")
parser.add_argument("query", nargs="+", help="The query to ask the agent.")
parser.add_argument(
"--chat-id",
default=None,
help="Resume an existing conversation. Pass the chat_id printed in a previous run.",
)
parser.add_argument(
"--new-chat",
action="store_true",
help="Force a fresh session (ignored if --chat-id not given).",
)
parser.add_argument(
"-q", "--quiet", action="store_true", help="Silence diagnostic emissions."
)
args = parser.parse_args()
if not config.LLM.api_key:
print(
"[error] LLM_API_KEY is not set. Add it to .env and try again.",
file=sys.stderr,
)
return 2
if not config.SEARCH.api_key:
print(
"[warn] EXA_API_KEY not set — web search will be disabled.",
file=sys.stderr,
)
init_tracing()
store = SessionStore(config.SESSION.storage_dir)
if args.chat_id and not args.new_chat:
session = store.load(args.chat_id)
else:
session = ChatSession()
emitter = ConsoleEmitter(verbose=not args.quiet)
agent = SERAAgent(emitter=emitter)
print(f"chat_id: {session.chat_id}\n")
query = " ".join(args.query)
async for chunk in agent.run(query=query, session=session):
print(chunk, end="", flush=True)
print() # final newline after streaming
if config.SESSION.persist:
store.save(session)
shutdown_tracing()
return 0
def main() -> int:
try:
return asyncio.run(amain())
except KeyboardInterrupt:
shutdown_tracing()
return 130
if __name__ == "__main__":
sys.exit(main())