The harness was two halves that could not see each other: main.c built the
agent, ui.c ran it -- with the curses thread model, the approval condition
variable, the slash commands and the provider switching inside the drawing
code. A second front end had to copy all of it.
Now:
src/backend/ engine.c owns the session, the turn thread, the event queue
and every command; agent, transports, tools, session, config,
providers, plugins and the protocol servers move here.
src/frontend/ curses.c draws and reads keys through ui_backend.h;
ui_local.c is the engine in this process, ui_remote.c is a
served engine behind a socket. protocol.c serves the engine
as JSON lines (protocol 2: history, approvals, steering);
client.c is splinterctl, the reference client.
splinter --serve / --attach: the same screen runs as a client of a served
engine, so the socket carries everything the front end needs. docs/PROTOCOL.md
and docs/FRONTENDS.md describe the contract; docs/proto-example.py is a
60-line client. tests/test_engine, test_protocol and test_attach cover the
event order, the socket and the attach mode.
Review fixes in the same tree: dynamic line buffers (9 KB prompts and 8 KB
events were truncated), a turn event no longer ends the busy window (steer
leftovers), the screen finalises every turn, /new keeps the profile, reopens
the log and gets unique ids, the profile is remembered in <session>.agent so
--resume comes back in it, agent statistics are a locked snapshot (no data
race), !shell keeps 64 KiB with a marker, the socket probes for a live server,
compares its inode and sets CLOEXEC/SNDTIMEO, exit 2 means configuration,
BSD peer checks use getpeereid, and the i18n checker scans the new
subdirectories again.
Includes the parallel session's SSE fix: the parser reports what a line
produced, so a tool name no longer lands in the answer ("readread").
74 lines
2.4 KiB
Python
Executable File
74 lines
2.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""A minimal splinter GUI-backend client.
|
|
|
|
It speaks the line protocol from docs/PROTOCOL.md and prints the transcript as
|
|
text -- the same events a Tk/Qt/Electron window would draw. Start a server
|
|
first:
|
|
|
|
splinter --serve --socket /tmp/splinter.sock
|
|
python3 docs/proto-example.py /tmp/splinter.sock "list the C files"
|
|
|
|
Approve is answered with `y` on stdin; everything else is printed as it
|
|
arrives.
|
|
"""
|
|
import json
|
|
import socket
|
|
import sys
|
|
|
|
|
|
class Splinter:
|
|
def __init__(self, path):
|
|
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
self.sock.connect(path)
|
|
self.f = self.sock.makefile("rw", encoding="utf-8")
|
|
|
|
def send(self, **obj):
|
|
self.f.write(json.dumps(obj) + "\n")
|
|
self.f.flush()
|
|
|
|
def events(self):
|
|
for line in self.f:
|
|
line = line.strip()
|
|
if line:
|
|
yield json.loads(line)
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) < 3:
|
|
print(__doc__)
|
|
return 2
|
|
cli = Splinter(sys.argv[1])
|
|
cli.send(cmd="input", line=" ".join(sys.argv[2:]))
|
|
for ev in cli.events():
|
|
kind = ev.get("ev")
|
|
if kind == "stream" and ev.get("kind") == "text":
|
|
print(ev.get("text", ""), end="", flush=True)
|
|
elif kind == "stream" and ev.get("kind") == "think":
|
|
pass # a GUI would show this dimmed
|
|
elif kind == "tool":
|
|
print("\n>> " + ev.get("call", "?"))
|
|
elif kind == "tool_result" and not ev.get("join"):
|
|
print(ev.get("text", ""))
|
|
elif kind in ("entry", "reply", "error"):
|
|
print("[%s] %s" % (ev.get("role", kind), ev.get("text", "")))
|
|
elif kind == "approval":
|
|
print("[approval] %s (%s) [y/N] " % (ev.get("call", "?"),
|
|
ev.get("class", "?")),
|
|
end="", flush=True)
|
|
answer = sys.stdin.readline().strip().lower()
|
|
cli.send(cmd="approve", allow=answer.startswith("y"))
|
|
elif kind == "state" and ev.get("busy"):
|
|
print("[%s/%s]" % (ev.get("agent"), ev.get("model")),
|
|
file=sys.stderr)
|
|
elif kind == "turn":
|
|
print()
|
|
if not ev.get("ok"):
|
|
print("turn failed: " + ev.get("error", ""), file=sys.stderr)
|
|
return 1
|
|
return 0
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|