Skip to content

Finish UTF-8 console on Delphi and add serve --debug request logging - #23

Merged
FMXExpress merged 1 commit into
mainfrom
claude/pr22-utf8-console-and-serve-debug
May 27, 2026
Merged

FMXExpress merged 1 commit into
mainfrom
claude/pr22-utf8-console-and-serve-debug

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Summary

Two fixes:

1. UTF-8 console output under Delphi

EnableUTF8Console already called SetConsoleCP/SetConsoleOutputCP(CP_UTF8) but the Delphi build still rendered banner / panel glyphs as mojibake while FPC printed them cleanly. Root cause: Delphi's RTL caches a per-Text-file code page independently of the Win32 console output CP. The UnicodeString → bytes conversion in WriteLn(Output, ...) used the cached value (the system ANSI CP), so the bytes the console received were never UTF-8 even though it had been told to interpret them as such.

Add the missing pair under {$IFNDEF FPC}:

SetTextCodePage(Output,    CP_UTF8);
SetTextCodePage(ErrOutput, CP_UTF8);

FPC's WriteLn emits the AnsiString-as-UTF-8 bytes verbatim, so the guards skip the Delphi-only step. Console banner, panel borders, and any other Unicode strings now render correctly in cmd.exe/PowerShell.

2. pasclaw serve --debug for diagnosing client integration

Adds a --debug / -d flag to serve:

pasclaw serve --debug
  • Flips the log level to debug.
  • Sets a new DebugIO property on TGatewayServer.

With DebugIO on, /v1/chat/completions logs:

  • Incoming body (full JSON) + remote IP + byte count.
  • Parsed request shape: model, stream, temperature, max_tokens.
  • Tool-loop result summary: iterations, input_tokens, output_tokens, finish_reason, final content.
  • Outgoing response body (200 JSON / SSE byte count) or status + reason on every early-return (400 empty body, 400 invalid JSON, 400 missing messages, 503 no provider, 502 tool loop failed).

The existing gateway: METHOD PATH debug line in OnCommandGet also lights up automatically once the level is at debug.

Test plan

  • On Windows under Delphi build: banner renders with █/╔/╚ glyphs (not mojibake) in cmd.exe, PowerShell, Windows Terminal
  • FPC/Windows still renders cleanly (no regression)
  • FPC/Linux still renders cleanly (Posix branch untouched)
  • pasclaw serve --debug boots; sending an OpenAI request prints body in/out
  • pasclaw serve without --debug keeps the current quiet stdout
  • An invalid request from a client surfaces a clear log line (e.g. chat/completions -> 400 (invalid JSON))

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon


Generated by Claude Code

EnableUTF8Console already set the Win32 console code pages but Delphi
still showed mojibake (▄▒▓) where FPC rendered the banner cleanly.
Root cause: Delphi's RTL caches a per-Text-file code page independently
of SetConsoleOutputCP. The UnicodeString -> bytes conversion in
WriteLn(Output, ...) used the cached value (the system ANSI CP), so
the bytes the console received were never UTF-8 even though it was
told to interpret them that way. Adding SetTextCodePage(Output,
CP_UTF8) and the same for ErrOutput finishes the fix. FPC's WriteLn
already emits the AnsiString-as-UTF-8 bytes verbatim, so the new
calls are guarded {\$IFNDEF FPC}.

Separately, give `pasclaw serve` a --debug / -d flag for diagnosing
clients that aren't getting expected responses through
/v1/chat/completions:

  pasclaw serve --debug

flips the log level to debug and turns on a new DebugIO property on
TGatewayServer. With it on, the chat-completions handler logs the
full incoming body (with remote IP), the parsed (model, stream,
temperature, max_tokens), tool-loop result summary (iterations,
input/output tokens, finish reason, final content), and the response
body (or status code + reason on every early-return). The existing
'gateway: METHOD PATH' debug line lights up automatically.

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants