-
Notifications
You must be signed in to change notification settings - Fork 557
Expand file tree
/
Copy pathsessions_cmd.py
More file actions
340 lines (296 loc) · 12.4 KB
/
Copy pathsessions_cmd.py
File metadata and controls
340 lines (296 loc) · 12.4 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""Sessions command — list/show/resume/delete/export sessions."""
from __future__ import annotations
import asyncio
import json
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
import typer
from rich.table import Table
from opensquilla.cli.chat.session_state import messages_to_markdown
from opensquilla.cli.gateway_client import session_history_all
from opensquilla.cli.gateway_rpc import default_gateway_url, run_gateway_sync
from opensquilla.cli.output import exit_invalid_request, print_json
from opensquilla.cli.ui import ACCENT, ACCENT_HEADER, console, error_panel
app = typer.Typer(help="Manage chat sessions.")
_CLIENT_UNAVAILABLE = object()
_ACTION_FAILED = object()
def _resolved_key(payload: dict[str, Any], fallback: str) -> str:
value = payload.get("session_key") or payload.get("key") or fallback
return str(value)
def _parse_since(value: str | None) -> datetime | None:
if not value:
return None
raw = value.strip()
if not raw:
return None
try:
if raw.isdigit():
number = float(int(raw))
if number > 10_000_000_000:
number = number / 1000
return datetime.fromtimestamp(number, tz=UTC)
parsed = datetime.fromisoformat(raw.replace("Z", "+00:00"))
if parsed.tzinfo is None:
parsed = parsed.replace(tzinfo=UTC)
return parsed
except ValueError as exc:
raise typer.BadParameter("--since must be an ISO date/datetime or epoch timestamp") from exc
def _row_datetime(row: dict[str, Any]) -> datetime | None:
value = row.get("updated_at", row.get("updatedAt"))
if value is None:
return None
if isinstance(value, (int, float)):
timestamp = float(value)
if timestamp > 10_000_000_000:
timestamp = timestamp / 1000
return datetime.fromtimestamp(timestamp, tz=UTC)
if isinstance(value, str):
try:
if value.isdigit():
return _parse_since(value)
parsed = datetime.fromisoformat(value.replace("Z", "+00:00"))
if parsed.tzinfo is None:
parsed = parsed.replace(tzinfo=UTC)
return parsed
except ValueError:
return None
return None
def _filter_sessions(
rows: list[dict[str, Any]],
*,
agent: str | None,
status: str | None,
channel: str | None,
since: datetime | None,
) -> list[dict[str, Any]]:
filtered: list[dict[str, Any]] = []
for row in rows:
if agent and str(row.get("agent_id") or row.get("agentId") or "") != agent:
continue
if status and str(row.get("status") or "").lower() != status.lower():
continue
if channel:
channel_values = {
str(row.get("channel") or ""),
str(row.get("last_channel") or ""),
str(row.get("lastChannel") or ""),
str(row.get("source_channel") or ""),
str(row.get("sourceChannel") or ""),
}
if channel not in channel_values:
continue
if since:
updated = _row_datetime(row)
if updated is None or updated < since:
continue
filtered.append(row)
return filtered
async def _with_client(action):
from opensquilla.cli.gateway_client import GatewayClient, GatewayRPCError
client = GatewayClient()
try:
# `default_gateway_url()` is what `sessions list`/`show`/`abort` reach
# through `run_gateway_sync`. The literal this replaced skipped the
# config entirely, so `resume`, `delete` and `export` went to
# 127.0.0.1:18791 no matter which profile was selected — a named
# profile's gateway on another port was invisible to them (#1379).
# `OPENSQUILLA_GATEWAY_URL` still wins; the resolver checks it first.
await client.connect(default_gateway_url())
return await action(client)
except SystemExit as exc:
console.print(f"[dim]{exc}[/dim]")
return _CLIENT_UNAVAILABLE
except GatewayRPCError as exc:
console.print(error_panel(str(exc)))
return _ACTION_FAILED
finally:
await client.close()
@app.command("list")
def sessions_list(
limit: int = typer.Option(50, "--limit", "-n", help="Maximum rows"),
agent: str | None = typer.Option(None, "--agent", help="Filter by agent id"),
status: str | None = typer.Option(None, "--status", help="Filter by session status"),
channel: str | None = typer.Option(None, "--channel", help="Filter by channel/source"),
since: str | None = typer.Option(None, "--since", help="ISO date/datetime or epoch timestamp"),
json_output: bool = typer.Option(False, "--json", help="Emit machine-readable JSON"),
) -> None:
"""List recent sessions."""
if limit < 1:
exit_invalid_request(
"--limit must be >= 1",
json_output=json_output,
details={"parameter": "limit", "minimum": 1},
)
since_dt = _parse_since(since)
async def _run(client):
return await client.list_sessions(limit=limit)
result = run_gateway_sync(_run, json_output=json_output)
raw_rows = result.get("sessions", []) if isinstance(result, dict) else []
rows = _filter_sessions(
[row for row in raw_rows if isinstance(row, dict)],
agent=agent,
status=status,
channel=channel,
since=since_dt,
)
if json_output:
payload = dict(result) if isinstance(result, dict) else {}
payload["sessions"] = rows
payload["count"] = len(rows)
print_json(payload)
return
table = Table(title="Sessions", show_header=True, header_style=ACCENT_HEADER)
table.add_column("Key")
table.add_column("Agent")
table.add_column("Status")
table.add_column("Model")
table.add_column("Messages", justify="right")
for row in rows:
table.add_row(
str(row.get("key") or ""),
str(row.get("agent_id") or row.get("agentId") or ""),
str(row.get("status") or ""),
str(row.get("model") or ""),
str(row.get("message_count") or row.get("entry_count") or 0),
)
console.print(table)
@app.command("show")
def sessions_show(
session_id: str = typer.Argument(..., help="Session ID to inspect"),
json_output: bool = typer.Option(False, "--json", help="Emit machine-readable JSON"),
) -> None:
"""Show details of a specific session."""
async def _run(client):
resolved = await client.resolve_session(session_id)
preview = await client.preview_sessions(keys=[_resolved_key(resolved, session_id)])
return {"resolved": resolved, "preview": preview}
result = run_gateway_sync(_run, json_output=json_output)
if json_output:
print_json(result)
return
resolved = result.get("resolved", {}) if isinstance(result, dict) else {}
previews = result.get("preview", {}).get("previews", []) if isinstance(result, dict) else []
preview = previews[0] if previews else {}
key = _resolved_key(resolved, session_id)
table = Table(title=f"Session {key}", show_header=True, header_style=ACCENT_HEADER)
table.add_column("Field", style=ACCENT)
table.add_column("Value")
for field, value in (
("session_key", key),
("session_id", resolved.get("session_id")),
("agent_id", resolved.get("agent_id")),
("status", resolved.get("status")),
("model", resolved.get("model")),
("updated_at", resolved.get("updated_at") or preview.get("updatedAt")),
("title", preview.get("title")),
):
if value not in (None, ""):
table.add_row(field, str(value))
console.print(table)
last_message = str(preview.get("lastMessage") or "")
if last_message:
console.print(last_message)
@app.command("resume")
def sessions_resume(session_id: str = typer.Argument(..., help="Session ID to resume")) -> None:
"""Resume a session in interactive chat."""
from opensquilla.cli.chat_cmd import run_chat
async def _run(client):
return await client.resolve_session(session_id)
result = asyncio.run(_with_client(_run))
if result is _CLIENT_UNAVAILABLE:
console.print(f"[dim]Session {session_id!r} requires a running gateway.[/dim]")
return
if result is _ACTION_FAILED:
return
run_chat(session_id=_resolved_key(result, session_id))
@app.command("abort")
def sessions_abort(
session_id: str = typer.Argument(..., help="Session ID to abort"),
json_output: bool = typer.Option(False, "--json", help="Emit machine-readable JSON"),
) -> None:
"""Abort a running session turn."""
async def _run(client):
resolved = await client.resolve_session(session_id)
key = _resolved_key(resolved, session_id)
result = await client.abort_session(key)
if isinstance(result, dict):
return {"resolved": resolved, **result}
return {"resolved": resolved, "result": result}
payload = run_gateway_sync(_run, json_output=json_output)
if json_output:
print_json(payload)
return
key = payload.get("key") or session_id
aborted = bool(payload.get("aborted", False))
console.print(f"{'Aborted' if aborted else 'No running task for'} session {key!r}")
@app.command("delete")
def sessions_delete(
session_id: str = typer.Argument(..., help="Session ID to delete"),
yes: bool = typer.Option(False, "--yes", "-y", help="Skip confirmation"),
) -> None:
"""Delete a session."""
if not yes:
confirmed = typer.confirm(f"Delete session {session_id!r}?")
if not confirmed:
raise typer.Abort()
async def _run(client):
resolved = await client.resolve_session(session_id)
key = _resolved_key(resolved, session_id)
return await client.delete_sessions([key])
result = asyncio.run(_with_client(_run))
if result is _CLIENT_UNAVAILABLE:
console.print("[dim]Session deletion requires a running gateway.[/dim]")
return
if result is _ACTION_FAILED:
return
console.print_json(data=result)
@app.command("export")
def sessions_export(
session_id: str = typer.Argument(..., help="Session ID to export"),
output: Path | None = typer.Option(None, "--output", "-o", help="Output file"),
format: str = typer.Option("md", "--format", help="Export format: md|json"),
) -> None:
"""Export session transcript and metadata.
Uses the existing chat.history RPC for persisted transcript messages and
falls back to session preview when no messages are available.
"""
if format not in {"md", "json"}:
console.print("[red]--format must be md or json[/red]")
raise typer.Exit(2)
async def _run(client):
resolved = await client.resolve_session(session_id)
key = _resolved_key(resolved, session_id)
preview = await client.preview_sessions(keys=[key])
history = await session_history_all(client.session_history, key)
return {"resolved": resolved, "preview": preview, "history": history}
result: dict[str, Any] | None = asyncio.run(_with_client(_run))
if result is _CLIENT_UNAVAILABLE:
console.print("[dim]Session export requires a running gateway.[/dim]")
return
if result is _ACTION_FAILED:
return
if result is None:
console.print("[red]Session export returned no data.[/red]")
return
target = output or Path(f"{session_id.replace(':', '-')}.{format}")
if format == "json":
target.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
else:
resolved = result.get("resolved", {})
key = _resolved_key(resolved, session_id)
previews = result.get("preview", {}).get("previews", [])
preview = previews[0] if previews else {}
messages = result.get("history", {}).get("messages", [])
transcript = messages_to_markdown(messages) if isinstance(messages, list) else ""
if not transcript.strip():
transcript = f"## Preview\n\n{preview.get('lastMessage', '')}\n"
body = (
f"# Session {key}\n\n"
f"- Status: {resolved.get('status', '')}\n"
f"- Model: {resolved.get('model') or ''}\n"
f"- Updated: {resolved.get('updated_at', '')}\n\n"
f"{transcript}"
)
target.write_text(body, encoding="utf-8")
console.print(f"[green]Exported:[/green] {target}")