File size: 14,555 Bytes
ca3d977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""llama.cpp Web UI gateway for live Planner Cache conversations.

The browser is presentation only. Every chat request terminates here and passes
through :class:`PlannerChatSession` before the selected frozen-model runtime is
called. This prevents the llama.cpp UI from bypassing canonical memory.
"""

from __future__ import annotations

import argparse
import copy
from http import HTTPStatus
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
import json
from pathlib import Path
import sys
import threading
import time
from urllib.parse import parse_qs, urlsplit
import webbrowser

from pcm.planner.chat_cli import PlannerChatSession, display_json, parser as chat_parser


WEB_HELP = """Chat in the browser using the llama.cpp Web UI.

Terminal commands:
  /help         show this help
  /state        show active canonical P-cache entries
  /personality  show promoted personality entries and the last retrieval
  /events       show recent Planner Cache events
  /save         checkpoint P-cache and P-package state
  /quit         save and stop the Web UI
"""


def parser() -> argparse.ArgumentParser:
    result = chat_parser()
    result.description = "Planner Cache through the llama.cpp Web UI"
    result.add_argument("--web-host", default="127.0.0.1")
    result.add_argument("--web-port", type=int, default=0)
    result.add_argument("--web-ui-path", type=Path, required=True)
    result.add_argument("--no-browser", action="store_true")
    return result


def _text_content(content: object) -> str:
    if isinstance(content, str):
        return content
    if isinstance(content, list):
        parts = []
        for item in content:
            if isinstance(item, dict) and item.get("type") in {"text", "input_text"}:
                parts.append(str(item.get("text", "")))
        return "\n".join(parts)
    return str(content or "")


def newest_user_message(payload: dict[str, object]) -> str:
    messages = payload.get("messages")
    if not isinstance(messages, list):
        raise ValueError("messages must be an array")
    for row in reversed(messages):
        if isinstance(row, dict) and row.get("role") == "user":
            message = _text_content(row.get("content")).strip()
            if message:
                return message
    raise ValueError("a non-empty user message is required")


def native_messages(payload: dict[str, object]) -> list[dict[str, object]]:
    """Return an opaque structural copy for the model's native chat template."""
    messages = payload.get("messages")
    if not isinstance(messages, list):
        raise ValueError("messages must be an array")
    if not all(isinstance(row, dict) for row in messages):
        raise ValueError("every message must be an object")
    return copy.deepcopy(messages)


def completion_payload(
    *, model: str, text: str, input_tokens: int | None, output_tokens: int | None,
) -> dict[str, object]:
    return {
        "id": "chatcmpl-planner-cache",
        "object": "chat.completion",
        "created": int(time.time()),
        "model": model,
        "choices": [{
            "index": 0,
            "message": {"role": "assistant", "content": text},
            "finish_reason": "stop",
        }],
        "usage": {
            "prompt_tokens": input_tokens or 0,
            "completion_tokens": output_tokens or 0,
            "total_tokens": (input_tokens or 0) + (output_tokens or 0),
        },
    }


class PlannerWebGateway:
    """Own the exact llama.cpp UI assets and the Planner Cache API boundary."""

    def __init__(
        self,
        session: PlannerChatSession,
        ui_path: Path,
        *,
        host: str = "127.0.0.1",
        port: int = 0,
    ) -> None:
        if not (ui_path / "index.html").is_file():
            raise FileNotFoundError(f"llama.cpp Web UI not found: {ui_path}")
        self.session = session
        self.ui_path = ui_path.resolve()
        self.lock = threading.Lock()
        self.httpd = ThreadingHTTPServer((host, port), self._handler())
        self.thread: threading.Thread | None = None

    @property
    def address(self) -> tuple[str, int]:
        host, port = self.httpd.server_address[:2]
        return str(host), int(port)

    @property
    def url(self) -> str:
        host, port = self.address
        visible_host = "127.0.0.1" if host in {"0.0.0.0", "::"} else host
        return f"http://{visible_host}:{port}/"

    def _handler(self):
        gateway = self

        class Handler(SimpleHTTPRequestHandler):
            server_version = "PlannerCacheWeb/1"

            def __init__(self, *args: object, **kwargs: object) -> None:
                super().__init__(*args, directory=str(gateway.ui_path), **kwargs)

            def log_message(self, _format: str, *args: object) -> None:
                return

            def _json(self, value: object, status: int = 200) -> None:
                body = json.dumps(value, ensure_ascii=False).encode("utf-8")
                self.send_response(status)
                self.send_header("Content-Type", "application/json; charset=utf-8")
                self.send_header("Content-Length", str(len(body)))
                self.send_header("Cache-Control", "no-store")
                self.end_headers()
                self.wfile.write(body)
                self.wfile.flush()

            def _read_json(self) -> dict[str, object]:
                length = int(self.headers.get("Content-Length", "0"))
                value = json.loads(self.rfile.read(length) or b"{}")
                if not isinstance(value, dict):
                    raise ValueError("JSON request must be an object")
                return value

            def do_GET(self) -> None:
                path = urlsplit(self.path).path
                if path == "/health":
                    self._json({"status": "ok"})
                    return
                if path in {"/v1/models", "/models"}:
                    self._json({
                        "object": "list",
                        "data": [{
                            "id": gateway.session.runtime.model_id,
                            "object": "model",
                            "created": 0,
                            "owned_by": "planner-cache",
                        }],
                    })
                    return
                if path == "/props":
                    runtime = gateway.session.runtime
                    self._json({
                        "default_generation_settings": {
                            "params": {
                                "n_predict": runtime.max_new_tokens,
                                "max_tokens": runtime.max_new_tokens,
                                "temperature": runtime.temperature,
                                "top_p": runtime.top_p,
                                "stream": True,
                            }
                        },
                        "total_slots": 1,
                        "model_path": str(runtime.model_path),
                        "chat_template": "Planner Cache runtime-managed chat",
                        "chat_template_caps": {},
                        "modalities": {"vision": False},
                        "build_info": "planner-cache-gateway",
                        "is_sleeping": False,
                    })
                    return
                if path in {"/slots", "/tools", "/mcp-servers"}:
                    self._json([])
                    return
                if path == "/planner-cache/state":
                    with gateway.lock:
                        value = gateway.session.state.snapshot()
                    self._json(value)
                    return
                if path == "/planner-cache/events":
                    with gateway.lock:
                        value = list(gateway.session.recorder.recent_events)
                    self._json(value)
                    return
                if path == "/planner-cache/personality":
                    try:
                        query = parse_qs(urlsplit(self.path).query)
                        limit = int(query.get("limit", ["100"])[0])
                        offset = int(query.get("offset", ["0"])[0])
                        with gateway.lock:
                            value = gateway.session.command(
                                "/personality", source="web",
                                personality_limit=limit,
                                personality_offset=offset,
                            )
                    except (TypeError, ValueError) as error:
                        self._json({"error": {"message": str(error)}}, 400)
                        return
                    self._json(value)
                    return
                super().do_GET()

            def do_POST(self) -> None:
                path = urlsplit(self.path).path
                if path not in {"/v1/chat/completions", "/chat/completions"}:
                    self._json({"error": {"message": "unsupported gateway endpoint"}}, 404)
                    return
                try:
                    request = self._read_json()
                    message = newest_user_message(request)
                    messages = native_messages(request)
                    gateway.lock.acquire()
                    try:
                        result = gateway.session.chat(
                            message, raw_messages=messages,
                        )
                        payload = completion_payload(
                            model=gateway.session.runtime.model_id,
                            text=result.text,
                            input_tokens=result.input_tokens,
                            output_tokens=result.output_tokens,
                        )
                        try:
                            if bool(request.get("stream")):
                                self._stream(payload)
                            else:
                                self._json(payload)
                        finally:
                            gateway.session.complete_turn_review()
                    finally:
                        gateway.lock.release()
                except Exception as error:
                    gateway.session.recorder.event(
                        "ERROR", source="web_gateway",
                        error_type=type(error).__name__, message=str(error),
                    )
                    self._json({"error": {"message": str(error)}}, 500)

            def _stream(self, payload: dict[str, object]) -> None:
                choice = payload["choices"][0]
                message = choice["message"]
                chunk = {
                    "id": payload["id"],
                    "object": "chat.completion.chunk",
                    "created": payload["created"],
                    "model": payload["model"],
                    "choices": [{
                        "index": 0,
                        "delta": {
                            "role": "assistant", "content": message["content"],
                        },
                        "finish_reason": None,
                    }],
                }
                finish = {
                    **chunk,
                    "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}],
                    "usage": payload["usage"],
                }
                rows = (
                    f"data: {json.dumps(chunk, ensure_ascii=False)}\n\n"
                    f"data: {json.dumps(finish, ensure_ascii=False)}\n\n"
                    "data: [DONE]\n\n"
                ).encode("utf-8")
                self.send_response(HTTPStatus.OK)
                self.send_header("Content-Type", "text/event-stream")
                self.send_header("Cache-Control", "no-cache")
                self.send_header("Connection", "close")
                self.end_headers()
                self.wfile.write(rows)
                self.wfile.flush()

        return Handler

    def start(self) -> None:
        self.thread = threading.Thread(
            target=self.httpd.serve_forever,
            name="planner-cache-web-ui",
            daemon=True,
        )
        self.thread.start()

    def close(self) -> None:
        self.httpd.shutdown()
        self.httpd.server_close()
        if self.thread is not None:
            self.thread.join(timeout=5)
            self.thread = None


def run(args: argparse.Namespace) -> int:
    session: PlannerChatSession | None = None
    gateway: PlannerWebGateway | None = None
    reason = "normal"
    try:
        session = PlannerChatSession(args)
        gateway = PlannerWebGateway(
            session, args.web_ui_path, host=args.web_host, port=args.web_port,
        )
        gateway.start()
        print(f"\n{session.title}")
        print(f"Web UI: {gateway.url}")
        print(f"Session records: {session.recorder.directory}")
        print("Chat in the browser. Terminal commands: /help /state /personality /events /save /quit\n")
        if not args.no_browser:
            webbrowser.open(gateway.url, new=2)
        while True:
            try:
                command = input("Debug: ").strip()
            except EOFError:
                # A non-interactive launcher should keep serving until signalled.
                while True:
                    time.sleep(1)
            if not command:
                continue
            if not command.startswith("/"):
                print("Chat in the Web UI. Terminal input accepts slash commands only.")
                continue
            with gateway.lock:
                value = session.command(command)
            if command.casefold() == "/help":
                value = WEB_HELP
            if command.casefold() == "/quit":
                reason = "quit"
                break
            if isinstance(value, str):
                print(value)
            else:
                display_json(value)
    except KeyboardInterrupt:
        reason = "ctrl-c"
        print("\nStopping.")
    finally:
        if gateway is not None:
            gateway.close()
        if session is not None:
            session.close(reason=reason)
    return 0


def main() -> None:
    try:
        raise SystemExit(run(parser().parse_args()))
    except (FileNotFoundError, ValueError, RuntimeError, OSError) as error:
        print(f"Planner Cache startup failed: {error}", file=sys.stderr)
        raise SystemExit(2)


if __name__ == "__main__":
    main()