Repository navigation
Add serve command and OpenAI-compatible /v1/chat/completions endpoint - #20
Conversation
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
There was a problem hiding this comment.
💡 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); |
There was a problem hiding this comment.
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 👍 / 👎.
| Msgs[i] := MakeMessage(MsgRoleFromString(MsgObj.GetStr('role', 'user')), | ||
| MsgObj.GetStr('content', '')); |
There was a problem hiding this comment.
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 👍 / 👎.
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:
New endpoint in
TGatewayServerPOST /v1/chat/completions— accepts{model, messages[], temperature?, max_tokens?, stream?}, walksmessages[]intoTMessageArray, runsRunToolLoopserver-side, returns either the OpenAIchat.completionJSON or — whenstream: trueis set — an SSE stream framed as a content chunk followed by a finish-reason chunk anddata: [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
servesubcommandThin wrapper around
TGatewayServer. Prints copy-pasteable client config (base_url, model, python + curl examples). The fullgatewaycommand still works as before.Test plan
pasclaw serveboots, prints the base_url + example snippetscurl http://localhost:<port>/v1/modelsreturns{"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 OpenAIchat.completionJSON; openai-python withbase_url=http://...:port/v1round-trips"stream": truestreamsdata: {...}\n\ndata: [DONE]\n\ngatewaypath)https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon
Generated by Claude Code