Repository navigation
Expand file tree
/
Copy pathsessions.py
More file actions
145 lines (109 loc) · 4.53 KB
/
Copy pathsessions.py
File metadata and controls
145 lines (109 loc) · 4.53 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
"""Session management utilities for Koder Agent."""
import logging
from datetime import datetime
from typing import List, Optional, Tuple
logger = logging.getLogger(__name__)
def default_session_local_ms() -> str:
"""Generate a local time session id precise to milliseconds.
Format: YYYY-MM-DDTHH:MM:SS.mmm (local time)
"""
return datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
def parse_session_dt(sid: str) -> Tuple[int, Optional[datetime]]:
"""Parse session id to datetime if possible. Return a sort key for desc order.
Supports formats like YYYY-MM-DDTHH:MM:SS.mmm or with microseconds.
Unparsable sids get None and are sorted to the end.
"""
fmts = ["%Y-%m-%dT%H:%M:%S.%f", "%Y-%m-%d %H:%M:%S.%f", "%Y-%m-%dT%H:%M:%S"]
for fmt in fmts:
try:
dt = datetime.strptime(sid, fmt)
return (0, dt)
except Exception:
logger.debug("Failed to parse session datetime '%s'", sid, exc_info=True)
continue
return (1, None)
def sort_sessions_desc(sids: List[str]) -> List[str]:
"""Sort session IDs in descending order by datetime, with unparsable ones at the end."""
parsed = [(parse_session_dt(s), s) for s in sids]
dated = [(dt, s) for (flag, dt), s in parsed if flag == 0 and dt is not None]
others = [s for (flag_dt, s) in parsed if flag_dt[0] == 1]
dated.sort(key=lambda x: x[0], reverse=True)
return [s for _, s in dated] + others
def picker_arrows(sessions: List[str]) -> Optional[str]:
"""Simple numbered list picker for session selection."""
import sys
if not sys.stdin.isatty() or not sys.stdout.isatty():
return None
print("\nAvailable sessions:")
for i, session in enumerate(sessions, 1):
print(f"{i}. {session}")
print("\nEnter the number of the session to select (or press Enter to cancel):")
max_attempts = 3
for _ in range(max_attempts):
try:
user_input = input("> ").strip()
# Empty input means cancel
if not user_input:
return None
# Check if input is a valid number
if not user_input.isdigit():
print(f"Please enter a valid number between 1 and {len(sessions)}.")
continue
choice = int(user_input)
# Check if choice is in valid range
if 1 <= choice <= len(sessions):
return sessions[choice - 1]
else:
print(f"Please enter a number between 1 and {len(sessions)}.")
except (EOFError, KeyboardInterrupt):
return None
except Exception:
print("Invalid input. Please try again.")
print("Too many invalid attempts. Cancelling selection.")
return None
def picker_arrows_with_titles(sessions: List[Tuple[str, Optional[str]]]) -> Optional[str]:
"""Session picker that displays titles when available.
Args:
sessions: List of (session_id, title) tuples
Returns:
Selected session ID or None if cancelled
"""
import sys
if not sys.stdin.isatty() or not sys.stdout.isatty():
return None
print("\nAvailable sessions:")
for i, (sid, title) in enumerate(sessions, 1):
if title:
# Parse datetime from session ID for suffix
_, dt = parse_session_dt(sid)
if dt:
display = f"{title} - {dt.strftime('%Y-%m-%d %H:%M')}"
else:
display = title
else:
display = sid
print(f"{i}. {display}")
print("\nEnter the number of the session to select (or press Enter to cancel):")
max_attempts = 3
for _ in range(max_attempts):
try:
user_input = input("> ").strip()
# Empty input means cancel
if not user_input:
return None
# Check if input is a valid number
if not user_input.isdigit():
print(f"Please enter a valid number between 1 and {len(sessions)}.")
continue
choice = int(user_input)
# Check if choice is in valid range
if 1 <= choice <= len(sessions):
return sessions[choice - 1][0] # Return session ID, not title
else:
print(f"Please enter a number between 1 and {len(sessions)}.")
except (EOFError, KeyboardInterrupt):
return None
except Exception:
print("Invalid input. Please try again.")
print("Too many invalid attempts. Cancelling selection.")
return None