Skip to content

feat(llama-cpp): in-process local GGUF text inference provider - #109444

Merged
steipete merged 4 commits into
mainfrom
feat/llama-cpp-inference-provider
Jul 17, 2026
Merged

steipete merged 4 commits into
mainfrom
feat/llama-cpp-inference-provider

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

OpenClaw has no zero-dependency local inference route: onboarding requires a cloud key, a CLI login, or an already-installed runtime (Ollama/LM Studio). A machine with none of those cannot complete setup at all. The llama-cpp plugin today is embedding-only, despite already owning the node-llama-cpp dependency.

Why This Change Was Made

Extends extensions/llama-cpp with an in-process text-inference provider — the universal local fallback for the local-model onboarding track (related: #109228):

  • src/inference-provider.ts: fully custom createStreamFn on node-llama-cpp's low-level LlamaChat.generateResponse — streaming text deltas, native tool calls via ChatModelFunctions (chosen over defineChatSessionFunction, which executes handlers internally; OpenClaw must own tool execution), token usage from the sequence token meter, abort handling including queued-abort, and a commented process-owned single-slot model/context cache.
  • src/setup.ts + manifest: read-only appGuidedSetup.detect (offers the provider only when a model file is already cached — discovery never downloads), success-gated prepare, and an interactive run with an explicit consent prompt before the ~2.5 GB model download (resumable, via createModelDownloader).
  • src/defaults.ts: default model Qwen3-4B-Instruct-2507 Q4_K_M (hf:bartowski/..., verified 2,497,280,736 bytes). Chosen deliberately over Gemma 4 E4B: the E4B GGUF is ~5 GB with ~6 GB resident — OOM territory on the 8 GB machines this fallback targets — while a live bench of the actual system-agent contract shows the 2507 instruct model handles the approval flows correctly (see Evidence). Users can point params.modelPath at any GGUF.
  • Context size defaults to 8k and honors contextTokens/params.contextSize caps (local KV allocation cost).
  • Shared node-llama runtime/error helpers extracted from the embedding provider; docs + plugin inventory updated.

User Impact

openclaw onboard gains a "Local model (llama.cpp)" option on every OS with no accounts, no daemons, and one consented download; once a model is cached, guided onboarding auto-detects it. Existing embedding behavior unchanged.

Evidence

  • node scripts/run-vitest.mjs extensions/llama-cpp — 3 files, 29 passed (stream mapping, tools, cache switching, queued aborts, setup discovery/consent/HF branches).
  • Testbox production + extension-test typechecks green; full extension lint 0/0.
  • Live smoke on macOS (Metal): drove the real createLlamaCppStreamFn with a downloaded GGUF — resolution, load, streamed deltas, usage accounting (26 in / 7 out), and stop reasons all correct end-to-end.
  • Default-model bench (real system-agent planner prompts + parser, Ollama-served weights of the same models, 10 scenarios x 3 repeats): qwen3:4b-instruct 20/30 strict with approval-follow-through and secret-ref flows 3/3; strict failures were prose-refusals with harmless read commands attached. Gemma 4 E4B scores higher (26/30) but its ~5 GB GGUF does not fit the fallback's low-RAM target; it remains the preferred pick when users already have it in a local runtime.
  • Autoreview (codex): clean, no actionable findings.

Related: #109228 (local-model onboarding track).

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation extensions: llama-cpp size: XL maintainer Maintainer-authored PR labels Jul 17, 2026

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

Copy link
Copy Markdown
Contributor

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: efb068e718

ℹ️ 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".

Comment on lines +47 to +50
createStreamFn: ({ config, provider }) =>
createLlamaCppStreamFn({
providerConfig: config?.models?.providers?.[provider],
}),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Badge Route simple completions through the local stream

This only wires the in-process llama.cpp implementation into embedded agent runs; routes that call completeWithPreparedSimpleCompletionModel (for example tool-title/utility-model flows) go through prepareModelForSimpleCompletion, which skips registerProviderStreamForModel whenever getApiProvider(model.api) already exists. Because the default model keeps api: "openai-completions", those simple-completion calls fall back to the built-in OpenAI-compatible HTTP transport with baseUrl: "local://llama-cpp" instead of node-llama-cpp, so they fail once a llama-cpp model is selected. Add a wrapSimpleCompletionStreamFn/alias path that uses this provider stream for simple completions too.

Useful? React with 👍 / 👎.

"provider": "llama-cpp",
"method": "local",
"choiceId": "llama-cpp",
"appGuidedDiscovery": true,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Badge Expose the download setup path before discovery succeeds

With only appGuidedDiscovery in the manifest, this choice is considered by setup only after detectLlamaCppSetup finds an already-cached GGUF; I checked setup-inference.ts/setup-inference-auth-options.ts, and the manual/auth option lists require appGuidedSecret or appGuidedAuth, while activation rejects custom methods outside the discovery path. That makes runLlamaCppSetup and its consented 2.5 GB download unreachable for fresh users, so onboarding never offers the advertised manual llama.cpp setup unless they predownload/configure the model themselves.

Useful? React with 👍 / 👎.

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

Copy link
Copy Markdown
Contributor

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: 7d0d8a8513

ℹ️ 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".

Comment on lines +192 to +195
const modelCap =
typeof model.contextTokens === "number" && model.contextTokens > 0
? Math.floor(model.contextTokens)
: DEFAULT_LLAMA_CPP_CONTEXT_SIZE;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Badge Fall back to contextWindow for custom GGUF models

When a user configures a llama.cpp model with only contextWindow (the documented and required provider/native limit in ModelDefinitionConfig) and omits the optional contextTokens, this code ignores that configured window and always caps the native context at 8192. That makes custom GGUF entries like the docs example unable to request larger contexts, and can also request too large a context for intentionally smaller models; the fallback should use model.contextWindow before DEFAULT_LLAMA_CPP_CONTEXT_SIZE, matching the core runtime's contextTokens ?? contextWindow convention.

Useful? React with 👍 / 👎.

@steipete
steipete merged commit 658b601 into main Jul 17, 2026
109 checks passed
@steipete
steipete deleted the feat/llama-cpp-inference-provider branch July 17, 2026 01:53
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 17, 2026
…law#109444)

* feat(llama-cpp): add in-process text inference

* test(llama-cpp): narrow setup provider fixture

* fix(llama-cpp): trim public surface and refresh docs map

* fix(llama-cpp): import Context type in inference test
zooqueen pushed a commit to zooqueen/openclaw that referenced this pull request Sep 11, 2026
…law#109444)

* feat(llama-cpp): add in-process text inference

* test(llama-cpp): narrow setup provider fixture

* fix(llama-cpp): trim public surface and refresh docs map

* fix(llama-cpp): import Context type in inference test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation extensions: llama-cpp maintainer Maintainer-authored PR size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant