Repository navigation
Add TPasClawAgent + TPasClawServer components for embedding the CLI - #37
Conversation
Two TComponent descendants let other Delphi/FPC applications embed
PasClaw functionality without shelling out to the binary. Neither
duplicates business logic — they compose the same building blocks
(LoadConfig, NewProviderFromConfig, TToolRegistry, RegisterFSTools/
RegisterShellTool, MCP bridge, TGatewayServer, RunToolLoop) the CLI
already uses, adapted to TComponent property/event idioms.
TPasClawAgent
- Chat(Prompt, out Reply, out Err): Boolean — one-shot through the
configured provider; OnText / OnToolCall / OnToolResult fire as
the tool loop runs.
- ChatHistory(History, out Reply, out Err): Boolean — multi-turn
variant taking an existing TMessage array.
- Execute(Cmd, Args): Integer — pass-through to every CLI subcommand
via the new PasClaw.Cmd.Root.DispatchCommand entry point. Mirrors
`pasclaw <cmd> <args...>` byte-for-byte (stdout goes to the host
process's console, same as the CLI).
- Published: ProviderName, Model, SystemPrompt, MaxIterations,
UseTools, UseMCP, UseHashline + OnText/OnToolCall/OnToolResult/
OnError events. Provider/Registry/Config exposed as runtime
properties for advanced callers.
TPasClawServer
- Start: Boolean / Stop / IsRunning lifecycle. Start spawns a small
internal TThread that calls TGatewayServer.Start + WaitForStop;
Stop signals shutdown and joins.
- Published: BindAddr, Port, MaxIter, Debug, EnableTools, EnableMCP,
EnableHashline, ProviderName, Model + OnStarted/OnStopped/OnError.
- Server property exposes the underlying TGatewayServer for hosts
that want to inspect it.
Single-instance contract
PasClaw.MCP.Bridge (GBindings[32]) and PasClaw.Skills.Loader
(GSpecs[64]) hold their state in module-level arrays with
compile-time fixed handler-slot tables, so only one live
TPasClawAgent and one live TPasClawServer are supported per process.
Each constructor checks a unit-level GAgentInstance / GServerInstance
pointer; the second create raises EPasClawInstance with a message
pointing at the limitation. Freeing the first then creating another
works. The destructor clears the pointer.
Refactor: PasClaw.Cmd.Root now exposes
function DispatchCommand(const Cmd: string; const Argv: array of string): Integer;
which RunRootCommand calls after CLI-specific setup (argv reading,
--no-color stripping, config-driven log level). This keeps the
dispatch logic in one place and lets non-CLI callers (the component,
tests) invoke commands without re-reading ParamStr. RunRootCommand's
behavior from a user's POV is unchanged — same stdout byte-for-byte.
Build wiring
- Makefile UNIT_DIRS gains src/pkg/component (FPC).
- PasClaw.dproj DCC_UnitSearchPath + DCCReference updated (Delphi).
- samples/component-console/SampleConsole.dpr — minimal console
program that creates TPasClawAgent, calls Execute('version', []),
then Chat('Say hi in three words.'). Builds under both compilers
once the project's PasClaw unit search path is configured.
The Component unit's `procedure Register` lights up the IDE palette
under a `PasClaw` tab when included in a design-time package; we do
not ship a .dpk in this PR — embedders can add the unit to whatever
package they already use.
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: 254c2d4b22
ℹ️ 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".
| FServer.Start(FAddr, FPort); | ||
| FServer.WaitForStop; | ||
| except | ||
| on E: Exception do FErr := E.ClassName + ': ' + E.Message; |
There was a problem hiding this comment.
Surface worker startup failures from Start
When the listener cannot bind (for example, the configured port is already in use), the exception is swallowed inside the worker and only stored in FErr, but Start has already returned True and no caller ever reads Err or fires OnError. This leaves embedders thinking the server started successfully while the background thread has already exited, so Start should either perform/confirm the bind synchronously or propagate the worker error before reporting success.
Useful? React with 👍 / 👎.
| property EnableMCP: Boolean read FEnableMCP write FEnableMCP default True; | ||
| property EnableHashline: Boolean read FEnableHashline write FEnableHashline default True; | ||
| property ProviderName: string read FProviderName write FProviderName; | ||
| property Model: string read FModel write FModel; |
There was a problem hiding this comment.
Apply the server Model property before creating the gateway
The Model property is published for TPasClawServer, but it is never read in the server path (rg FModel only finds the declaration/property here and the agent's use). As a result, embedding code that sets TPasClawServer.Model still serves /v1/chat, /v1/models, and default /v1/chat/completions responses using FConfig.DefaultModel; apply FModel to the loaded config or pass it into TGatewayServer before construction.
Useful? React with 👍 / 👎.
Summary
Lets other Delphi / FPC applications embed PasClaw without shelling out to the CLI. Two
TComponentdescendants in one new unit, plus a tiny extraction fromCmd.Rootso command dispatch can be called from non-CLI contexts.TPasClawAgentChat(Prompt; out Reply, Err): Boolean— one-shot through the configured provider;OnText/OnToolCall/OnToolResultfire as the tool loop runs.ChatHistory(History; out Reply, Err): Boolean— multi-turn variant.Execute(Cmd, Args): Integer— pass-through to every CLI subcommand via the newDispatchCommandentry point. Mirrorspasclaw <cmd> <args...>byte-for-byte (stdout goes to host process's console).ProviderName,Model,SystemPrompt,MaxIterations,UseTools,UseMCP,UseHashline+OnText/OnToolCall/OnToolResult/OnError.TPasClawServerStart: Boolean/Stop/IsRunning. Start spawns an internalTThreadthat drivesTGatewayServer.Start+WaitForStop; Stop signals shutdown and joins.BindAddr,Port,MaxIter,Debug,EnableTools,EnableMCP,EnableHashline,ProviderName,Model+OnStarted/OnStopped/OnError.Refactor
PasClaw.Cmd.Rootnow exposesfunction DispatchCommand(const Cmd: string; const Argv: array of string): Integer;whichRunRootCommandcalls after CLI-only setup (argv parsing,--no-colorstripping, log-level wiring). Same stdout byte-for-byte for the CLI; non-CLI callers (the component, tests) can dispatch any subcommand without re-readingParamStr.Single-instance contract
PasClaw.MCP.Bridge(GBindings[32]) andPasClaw.Skills.Loader(GSpecs[64]) hold their state in module-level arrays with compile-time fixed handler-slot tables. Only one liveTPasClawAgentand one liveTPasClawServerper process — each constructor checks a unit-level instance pointer and the second create raisesEPasClawInstancewith a clear message. Lifting that restriction is a larger refactor of those two units' handler tables, deferred.Sample
samples/component-console/SampleConsole.dpr— ~30-line console program that createsTPasClawAgent, callsExecute('version', []), thenChat('Say hi in three words.'). Builds under both FPC and Delphi.Test plan
make(FPC) still builds thepasclawbinary — new unit included via Makefile UNIT_DIRS.dprojstill builds — new unit listed in DCCReference + on the search pathpasclaw version,pasclaw status, etc. print identical output as before theDispatchCommandextractionTPasClawAgent/TPasClawServeron a Delphi form (with the unit installed) — they appear on thePasClawpalette tab and the published properties are editable in the Object InspectorTPasClawAgentwhile the first is live raisesEPasClawInstancewith the documented message; freeing the first then creating another succeedshttps://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon
Generated by Claude Code