Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115"""Offline image service and PNG fixtures for image generation scenarios."""
import base64import http.serverimport jsonimport randomimport selectimport socketimport structimport threadingimport timefrom typing import Any, TypedDictimport zlib
class ImageRequest(TypedDict): path: str headers: dict[str, str] body: dict[str, Any]
def png(width: int, height: int, *, noise: bool = False) -> bytes: def chunk(kind: bytes, data: bytes) -> bytes: body = kind + data return struct.pack(">I", len(data)) + body + struct.pack(">I", zlib.crc32(body))
rng = random.Random(1) rows = b"".join( b"\0" + (rng.randbytes(width * 3) if noise else b"\x80\x40\x20" * width) for _ in range(height) ) return ( b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)) + chunk(b"IDAT", zlib.compress(rows)) + chunk(b"IEND", b"") )
class ImageService: """Only the image upstream is faked; model, kernel and daemon use the harness."""
def __init__(self) -> None: self.image = png(3, 2) self.requests: list[ImageRequest] = [] self.status = 200 self.body: bytes | dict[str, Any] = { "data": [ { "b64_json": base64.b64encode(self.image).decode(), "generation_id": "generation-fixture", } ], } self.drop = False self.started = threading.Event() self.disconnected = threading.Event() self.release = threading.Event() self.block = False owner = self
class Handler(http.server.BaseHTTPRequestHandler): def log_message(self, format: str, *args: object) -> None: pass
def do_POST(self) -> None: body = self.rfile.read(int(self.headers["Content-Length"])) owner.requests.append( { "path": self.path, "headers": { name.lower(): value for name, value in self.headers.items() }, "body": json.loads(body), } ) owner.started.set() if owner.block: deadline = time.monotonic() + 60 while not owner.release.wait(0.05) and time.monotonic() < deadline: readable, _, _ = select.select([self.connection], [], [], 0) if readable and not self.connection.recv(1, socket.MSG_PEEK): owner.disconnected.set() return if owner.drop: self.connection.shutdown(socket.SHUT_RDWR) self.connection.close() return payload = ( owner.body if isinstance(owner.body, bytes) else json.dumps(owner.body).encode() ) self.send_response(owner.status) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", str(len(payload))) self.send_header("x-request-id", "request-fixture") self.end_headers() try: self.wfile.write(payload) except (BrokenPipeError, ConnectionResetError): pass
self.server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler) self.server.daemon_threads = True self.thread = threading.Thread(target=self.server.serve_forever, daemon=True) self.thread.start() self.url = f"http://127.0.0.1:{self.server.server_port}/backend-api/codex"
def close(self) -> None: self.release.set() self.server.shutdown() self.server.server_close() self.thread.join()