-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
78 lines (61 loc) · 2.12 KB
/
Copy pathchat.py
File metadata and controls
78 lines (61 loc) · 2.12 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
"""
Interactive CLI client for the LocalStack Agent.
Prompts to resume the last session or start a new one, then enters a REPL loop.
"""
import sys
import uuid
import httpx
BASE_URL = "http://localhost:8000"
def get_last_session() -> str | None:
"""Fetch the most recent session ID from the server. Returns None if none exist or server is down."""
try:
resp = httpx.get(f"{BASE_URL}/sessions", timeout=5.0)
sessions = resp.json()
return sessions[0] if sessions else None
except httpx.ConnectError:
return None
def resolve_session() -> str:
"""Prompt user to resume last session or start a new one. Returns the session ID to use."""
last = get_last_session()
if last:
answer = input(f"Resume session {last}? (y/n): ").strip().lower()
if answer == "y":
return last
return str(uuid.uuid4())
def chat(session_id: str, message: str) -> str:
"""Send a message to the agent and return the response text."""
resp = httpx.post(
f"{BASE_URL}/chat",
json={"session_id": session_id, "message": message},
timeout=180.0,
)
resp.raise_for_status()
return resp.json()["message"]
def main() -> None:
"""Entry point — resolve session, print session ID, run REPL loop."""
try:
session_id = resolve_session()
except httpx.ConnectError:
print(f"Error: could not connect to {BASE_URL}. Is the server running?")
sys.exit(1)
print(f"Session: {session_id}\n")
while True:
try:
user_input = input("You: ").strip()
except (EOFError, KeyboardInterrupt):
print()
break
if user_input.lower() in ("quit", "exit"):
break
if not user_input:
continue
try:
response = chat(session_id, user_input)
print(f"Agent: {response}\n")
except httpx.ConnectError:
print(f"Error: lost connection to {BASE_URL}.")
break
except httpx.HTTPStatusError as e:
print(f"Error: server returned {e.response.status_code}")
if __name__ == "__main__":
main()