:LLeMbasLogin [address] [--ca file] runs the CLI's own sign-in (lembas login --json), shows the code, opens the approval page and restarts the agent when it is done; it says whose sign-in it replaces and asks first. :LLeMbasLoginCancel and :LLeMbasLogout. A server that asks for a newer plugin (min_client in the CLI's hello) is said once. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
116 lines
4.1 KiB
Python
Executable File
116 lines
4.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""A stand-in for the `lembas` program itself, for the tests of signing in and out from an editor.
|
|
|
|
lembas serve --stdio the stand-in agent (tests/fake_agent.py); signed in only when the
|
|
state file says so
|
|
lembas login --json [address] [--ca file]
|
|
one JSON object a line, as LLeMbas CLI 2.0 writes them
|
|
(`src/lembas/login.ts`: `code`, then `done`; `error` with a message
|
|
and a machine `code`), and its exit codes
|
|
lembas logout signs out of the one login there is
|
|
|
|
The login is one for the whole computer, kept in the file FAKE_LEMBAS_STATE names. Words in the
|
|
address steer a sign-in: `unreachable` cannot be reached, `expires` is never approved and its code
|
|
runs out, `slow` waits to be cancelled, `plain` is an older CLI whose errors carry no code.
|
|
FAKE_LEMBAS_LOG gets every command line, one JSON array a line.
|
|
"""
|
|
import json
|
|
import os
|
|
import sys
|
|
import time
|
|
|
|
STATE = os.environ.get("FAKE_LEMBAS_STATE", "")
|
|
LOG = os.environ.get("FAKE_LEMBAS_LOG", "")
|
|
|
|
|
|
def state():
|
|
try:
|
|
with open(STATE) as f:
|
|
return json.load(f)
|
|
except (OSError, ValueError):
|
|
return {}
|
|
|
|
|
|
def emit(obj):
|
|
sys.stdout.write(json.dumps(obj) + "\n")
|
|
sys.stdout.flush()
|
|
|
|
|
|
def fail(message, code=1):
|
|
sys.stderr.write(message + "\n")
|
|
sys.exit(code)
|
|
|
|
|
|
def base_of(address):
|
|
a = address.strip().rstrip("/")
|
|
return a if a.startswith(("http://", "https://")) else "https://" + a
|
|
|
|
|
|
def done(base, fresh):
|
|
emit({"event": "done", "fresh": fresh, "connection": "example", "base_url": base, "version": "2.0.0", "models": 3, "changes": []})
|
|
|
|
|
|
def login(args):
|
|
ca = None
|
|
rest = []
|
|
it = iter(args)
|
|
for a in it:
|
|
if a == "--ca":
|
|
ca = next(it, None)
|
|
elif a != "--json":
|
|
rest.append(a)
|
|
if "--json" not in args:
|
|
fail("this stand-in only signs in for a program (--json)", 2)
|
|
had = state().get("base_url")
|
|
address = rest[0] if rest else ""
|
|
if not address or (had and base_of(address) == had and not ca):
|
|
# Again, with no address or the same one: the login there is, read anew.
|
|
if not had:
|
|
fail("usage: lembas login <instance address>", 2)
|
|
return done(had, False)
|
|
base = base_of(address)
|
|
if "unreachable" in address:
|
|
message = f"{base} could not be reached (connection refused) — is the address right, and the server up?"
|
|
emit({"event": "error", "message": message, **({} if "plain" in address else {"code": "unreachable"})})
|
|
fail(message)
|
|
expires = 1 if "expires" in address else 600
|
|
emit({"event": "code", "user_code": "WAYB-READ", "verification_uri": base + "/device",
|
|
"verification_uri_complete": base + "/device?code=WAYB-READ", "expires_in": expires,
|
|
"instance": {"name": "Example", "version": "2.0.0", "base_url": base}})
|
|
if "expires" in address:
|
|
time.sleep(expires + 0.2)
|
|
message = "the code ran out before it was approved — sign in again for a new one"
|
|
emit({"event": "error", "message": message, "code": "timeout"})
|
|
fail(message)
|
|
if "slow" in address:
|
|
time.sleep(30)
|
|
time.sleep(0.15)
|
|
with open(STATE, "w") as f:
|
|
json.dump({"base_url": base, "ca": ca}, f)
|
|
done(base, True)
|
|
|
|
|
|
def main():
|
|
args = sys.argv[1:]
|
|
if LOG:
|
|
with open(LOG, "a") as f:
|
|
f.write(json.dumps(args) + "\n")
|
|
if args[:1] == ["serve"]:
|
|
if STATE and not state().get("base_url"):
|
|
os.environ["FAKE_AGENT_LOGIN"] = "none"
|
|
agent = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fake_agent.py")
|
|
os.execv(sys.executable, [sys.executable, agent])
|
|
if args[:1] == ["login"]:
|
|
return login(args[1:])
|
|
if args[:1] == ["logout"]:
|
|
had = state().get("base_url")
|
|
if not had:
|
|
fail("not logged in to any instance", 2)
|
|
os.remove(STATE)
|
|
print("Signed out of example; its connection is removed.")
|
|
return
|
|
fail("usage: lembas serve --stdio | login --json [address] | logout", 2)
|
|
|
|
|
|
main()
|