Skip to content

Add serve command and OpenAI-compatible /v1/chat/completions endpoint - #20

Merged
FMXExpress merged 1 commit into
mainfrom
claude/pr20-serve-openai-api
May 27, 2026
Merged

FMXExpress merged 1 commit into
mainfrom
claude/pr20-serve-openai-api

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Summary

Adds a drop-in OpenAI Chat Completions API server, NanoBot-style. Any client that speaks the OpenAI protocol (openai-python, openai-node, LangChain, autogen, curl, etc.) can point at PasClaw with:

base_url = http://<addr>:<port>/v1
api_key  = anything-nonempty

New endpoint in TGatewayServer

  • POST /v1/chat/completions — accepts {model, messages[], temperature?, max_tokens?, stream?}, walks messages[] into TMessageArray, runs RunToolLoop server-side, returns either the OpenAI chat.completion JSON or — when stream: true is set — an SSE stream framed as a content chunk followed by a finish-reason chunk and data: [DONE]. The server-side tool loop completes before the chunk goes out (same approach NanoBot uses when the underlying providers don't expose token-level streaming).
  • GET /v1/models — OpenAI-compatible list, reports the default configured model. Enough for clients that probe the endpoint before talking to it.

Temperature is only forwarded when the client passes a positive value, so the deprecated-field 400 from newer Claude models (PR #19) is also avoided here.

New serve subcommand

pasclaw serve                           # default bind/port from config
pasclaw serve --addr 0.0.0.0 --port 8088
pasclaw serve --no-tools                # skip built-in tools
pasclaw serve --no-mcp                  # skip MCP server discovery

Thin wrapper around TGatewayServer. Prints copy-pasteable client config (base_url, model, python + curl examples). The full gateway command still works as before.

Test plan

  • pasclaw serve boots, prints the base_url + example snippets
  • curl http://localhost:<port>/v1/models returns {"object":"list","data":[{"id":...,"object":"model","owned_by":"pasclaw"}]}
  • curl http://localhost:<port>/v1/chat/completions -d '{"model":"...","messages":[{"role":"user","content":"hi"}]}' returns an OpenAI chat.completion JSON; openai-python with base_url=http://...:port/v1 round-trips
  • Same request with "stream": true streams data: {...}\n\ndata: [DONE]\n\n
  • Tool calls in the existing registry still dispatch (registry shared with the gateway path)

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon


Generated by Claude Code

Introduces a drop-in OpenAI Chat Completions API server backed by the
existing tool loop. Any client that speaks the OpenAI protocol
(openai-python, openai-node, LangChain, autogen, curl, etc.) can point
at PasClaw by setting:

    base_url = http://<addr>:<port>/v1
    api_key  = anything-nonempty

New endpoint in TGatewayServer:

    POST /v1/chat/completions

Accepts the standard request shape — {model, messages[], temperature?,
max_tokens?, stream?} — walks messages[] into TMessageArray, runs
RunToolLoop server-side, and returns either an OpenAI chat.completion
JSON or, when stream:true is set, an SSE stream framed as a single
content chunk followed by a finish-reason chunk and `data: [DONE]`.
The server-side tool loop completes before the chunk goes out, so
clients see the same final text either way — matches how downstream
SDKs typically consume non-token-level streams.

Also exposes:

    GET  /v1/models   — OpenAI-compatible list (reports the default
                        configured model).

Temperature is only forwarded when the client explicitly passed a
positive value, so the deprecated-field 400 from newer Claude models
that PR #19 fixed for the agent path also applies to the API server.

New subcommand `serve` is a thin wrapper around TGatewayServer that
trims the gateway surface to the OpenAI endpoints and prints
copy-pasteable client config (base_url, model name, python + curl
examples). The full `gateway` command still works as before.

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon
@FMXExpress
FMXExpress merged commit 2c83c3e into main May 27, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f37f161002

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Exit;
end;

Req := TJsonObject.Parse(Body);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle malformed JSON before parsing

When /v1/chat/completions receives syntactically malformed JSON, TJsonObject.Parse raises EPasClawJSON rather than returning nil, so this line bypasses the intended 400 response below and falls into the outer OnCommandGet exception handler as a 500. This affects any OpenAI client/request retry path that sends a bad body; wrapping the parse in a local try/except would keep invalid requests classified as invalid_request_error instead of server errors.

Useful? React with 👍 / 👎.

Comment on lines +503 to +504
Msgs[i] := MakeMessage(MsgRoleFromString(MsgObj.GetStr('role', 'user')),
MsgObj.GetStr('content', ''));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve content-part messages instead of dropping them

For valid OpenAI chat requests where message.content is an array of content parts, such as [{'type':'text','text':'hi'}] or image+text messages, GetStr('content', '') treats the non-string value as missing and forwards an empty message to the provider. This makes compatible clients that use the standard content-parts shape get responses to blank prompts; the handler needs to detect arrays and concatenate/extract text parts rather than silently replacing them with ''.

Useful? React with 👍 / 👎.

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