149 lines
6.3 KiB
Python
149 lines
6.3 KiB
Python
"""Show every loop-claude on this machine that waits for you, oldest first, and answer each one here.
|
|
|
|
usage: loop-inbox
|
|
|
|
Each loop-claude that asks you a question lists it here, as well as on its own terminal, until it is
|
|
answered or the loop stops. The list shows each loop's checkout, when it asked and how long it has
|
|
waited, and the question's first lines; it refreshes every second. Type a loop's number and Enter
|
|
to see its whole question, then answer as on the loop's terminal:
|
|
|
|
- A line and Enter sends it to that loop, which goes on with it.
|
|
- /talk opens the worker's claude session here, with `claude --resume`, as /talk does on the loop's
|
|
terminal. Once that claude ends, the loop goes on, and the judge rules on what was typed there; a
|
|
loop whose claude ends with nothing typed stops.
|
|
- Enter alone, or Esc, goes back to the list; unlike on the loop's terminal, it doesn't stop the loop.
|
|
|
|
The loop takes whichever answer comes first, here or on its terminal, so it keeps working with no
|
|
loop-inbox running. A loop whose release is ready to cut, or whose /talk is open, shows that claude
|
|
is open, and is answered there. A loop whose process is gone drops off the list. q quits
|
|
loop-inbox; the loops keep waiting.
|
|
|
|
The loops' questions are files in ~/.local/state/loop-claude, read and written as
|
|
bin/loop_claude/waiting.py describes.
|
|
|
|
Exit 2 when stdin or stdout is not a terminal, or on an unknown argument.
|
|
"""
|
|
import curses
|
|
import pathlib
|
|
import sys
|
|
import typing
|
|
|
|
from loop_inbox.inbox import Answering, answering_lines, BACKSPACE, ENTER, ESCAPE, fitted, Listing, listing_lines, Open, pressed, Quit, Screen, Send
|
|
from loop_claude.waiting import Ask, ask_file, Asking, claim_talk, ClaudeEnded, MAKE_WAY_SECONDS, read_ask, send, Typed, waiting
|
|
from loop_claude.effects import Effects, system_effects
|
|
from failure import Failure
|
|
from loop_claude.cycle.interrupt import hand_over
|
|
from loop_claude.paths import Paths, paths_from
|
|
|
|
USAGE = "usage: loop-inbox"
|
|
# Milliseconds between refreshes of the list while no key is pressed.
|
|
REFRESH_MS = 1000
|
|
KEYS = {"\n": ENTER, "\r": ENTER, curses.KEY_ENTER: ENTER, "\x7f": BACKSPACE, "\b": BACKSPACE, curses.KEY_BACKSPACE: BACKSPACE, "\x1b": ESCAPE}
|
|
|
|
|
|
def main(argv: list[str], home: pathlib.Path) -> int:
|
|
if argv[:1] in (["-h"], ["--help"]):
|
|
print(__doc__.strip())
|
|
return 0
|
|
if argv:
|
|
print(f"loop-inbox: unknown argument {argv[0]}; {USAGE}", file=sys.stderr)
|
|
return 2
|
|
if not (sys.stdin.isatty() and sys.stdout.isatty()):
|
|
print("loop-inbox: stdin or stdout is not a terminal; run it on one", file=sys.stderr)
|
|
return 2
|
|
try:
|
|
curses.wrapper(lambda window: run(window, paths_from(home), system_effects()))
|
|
except KeyboardInterrupt:
|
|
pass
|
|
return 0
|
|
|
|
|
|
def run(window: curses.window, paths: Paths, effects: Effects) -> None:
|
|
folder = paths.waiting
|
|
window.timeout(REFRESH_MS)
|
|
screen: Screen = Listing()
|
|
while True:
|
|
screen = refreshed(screen, folder, effects.alive)
|
|
draw(window, screen, effects)
|
|
try:
|
|
key = window.get_wch()
|
|
except curses.error:
|
|
continue # No key within REFRESH_MS.
|
|
match pressed(screen, key_name(key)):
|
|
case Quit():
|
|
return
|
|
case Send(ask, text):
|
|
screen = Listing(note=sent(folder, ask, text))
|
|
case Open(ask):
|
|
screen = Listing(note=opened(window, paths, ask, effects))
|
|
case Listing() | Answering() as next_screen:
|
|
screen = next_screen
|
|
|
|
|
|
def key_name(key: str | int) -> str:
|
|
"""inbox's name for a key curses read; an empty string for a key loop-inbox has no use for, such as an arrow."""
|
|
return KEYS.get(key, key if isinstance(key, str) else "")
|
|
|
|
|
|
def refreshed(screen: Screen, folder: pathlib.Path, alive: typing.Callable[[int], bool]) -> Screen:
|
|
"""screen with the loops as they wait now, unless the human is typing a number; a question that no longer waits leaves the answer screen."""
|
|
match screen:
|
|
case Listing(_, "", note):
|
|
return Listing(tuple(waiting(folder, alive)), "", note)
|
|
case Answering(ask) if not still_waits(folder, ask):
|
|
return Listing(note=no_longer_waits(ask))
|
|
return screen
|
|
|
|
|
|
def draw(window: curses.window, screen: Screen, effects: Effects) -> None:
|
|
height, width = window.getmaxyx()
|
|
now = effects.now()
|
|
lines = listing_lines(screen, now, effects.local_time, width) if isinstance(screen, Listing) else answering_lines(screen, now, effects.local_time, width)
|
|
window.erase()
|
|
for row, line in enumerate(fitted(lines, height)):
|
|
# The last cell of the screen cannot be written without an error, so each line stops one short of it.
|
|
window.addnstr(row, 0, line, width - 1)
|
|
window.refresh()
|
|
|
|
|
|
def still_waits(folder: pathlib.Path, ask: Ask) -> bool:
|
|
"""Whether ask still waits for an answer from loop-inbox."""
|
|
now = read_ask(ask_file(folder, ask.session))
|
|
return now is not None and now.id == ask.id and isinstance(now.state, Asking)
|
|
|
|
|
|
def no_longer_waits(ask: Ask) -> str:
|
|
return f"{ask.root} no longer waits for that answer: it was answered on its terminal, or stopped."
|
|
|
|
|
|
def sent(folder: pathlib.Path, ask: Ask, text: str) -> str:
|
|
"""Sends text to the loop that asked ask, and says what came of it."""
|
|
if not still_waits(folder, ask):
|
|
return no_longer_waits(ask)
|
|
if unsent := send(folder, ask, Typed(text)):
|
|
return f"Could not send the answer to {ask.root}: {unsent.reason}"
|
|
return f"Sent to {ask.root}."
|
|
|
|
|
|
def opened(window: curses.window, paths: Paths, ask: Ask, effects: Effects) -> str:
|
|
"""Opens the worker's claude session on this terminal once the loop has made way for it, and says what came of it."""
|
|
folder = paths.waiting
|
|
if not (isinstance(ask.state, Asking) and still_waits(folder, ask)):
|
|
return no_longer_waits(ask)
|
|
claimed = claim_talk(folder, ask, effects.pid(), effects.now, effects.sleep)
|
|
if isinstance(claimed, Failure):
|
|
return f"Could not open claude for {ask.root}: {claimed.reason}"
|
|
if not claimed:
|
|
return f"{ask.root} did not make way for claude within {MAKE_WAY_SECONDS:g} s; it was answered on its terminal, or stopped."
|
|
curses.def_prog_mode()
|
|
curses.endwin()
|
|
try:
|
|
unstarted = hand_over(ask.state.talk, paths, effects)
|
|
finally:
|
|
unsent = send(folder, ask, ClaudeEnded())
|
|
curses.reset_prog_mode()
|
|
window.refresh()
|
|
said = f"Could not start claude for {ask.root}: {unstarted.reason}" if unstarted else f"claude for {ask.root} has ended; the loop goes on."
|
|
# Without ClaudeEnded, the loop goes on only once loop-inbox quits.
|
|
return f"{said} Could not tell the loop: {unsent.reason}; quit loop-inbox to let it go on." if unsent else said
|