Skip to content

feat(ai): add the openai, anthropic and gemini providers - #17

Merged
NLX-SeokHwanKong merged 7 commits into
mainfrom
feat/ai-providers
Nov 26, 2026
Merged

NLX-SeokHwanKong merged 7 commits into
mainfrom
feat/ai-providers

Conversation

@kp-attlab

Copy link
Copy Markdown
Collaborator

Description

Adds three assistant providers that talk to a model API directly: openai, anthropic and gemini. Until now the assistant needed the Claude CLI for live data (through the MCP subprocess) or Ollama for text-only answers. With these, [ai] provider plus an API key is enough, and the model still queries the gateway through the same read-only tools.

How it works:

  • The provider sends the question over HTTPS, the model asks for tools, loxitui runs them in-process with executeTool() against the configured LoxiLB instance and sends the results back, until the model answers. One answer may take at most ten rounds of tool calls.
  • Tools are offered under the mcp__loxilb__<name> names the system prompt and the skills already use, so prompts, skills and the sidebar's tool display are unchanged.
  • The calls use fetch; no SDK packages are added and bun.lock is unchanged.
  • The key comes from [ai] api_key, or from OPENAI_API_KEY, ANTHROPIC_API_KEY or GEMINI_API_KEY when that is empty.

Per provider:

  • openai uses the Responses API (default model gpt-6.2-astra). The current models reject function tools on Chat Completions unless reasoning is turned off, which the default model does not accept.
  • openai with a base_url is taken as an OpenAI-compatible server (vLLM, llama.cpp, LM Studio) and uses Chat Completions. model is then required and a key is optional. That server is sent only the key in [ai] api_key; an OPENAI_API_KEY in the environment is not sent to it. Every question carries the tool definitions; there is no text-only fallback for a server that rejects them.
  • anthropic uses the Messages API (default model claude-opus-5-5).
  • gemini uses the Interactions API (default model gemini-4-hydrogen).

Things worth a look:

  • As with claude-cli, what the assistant works with leaves the machine for the provider's API: the question and the conversation so far, the system prompt (skill text, matched knowledge-base excerpts, the live-state summary) and the tool results (live LoxiLB state). docs/ai-sidebar.md says so.
  • model can hold a name nobody chose: the config default (claude-3-5-sonnet-20241022), or the sonnet that setup writes for claude-cli. The providers read the config default as unset and use their own default, and the hosted APIs do the same with sonnet. With a base_url, sonnet is kept, since that server may serve a model by that name.
  • AiClient.isConfigured was always true; it is now false while an API provider has no key, and the log-analysis shortcut uses the same check. claude-cli and ollama behave as before.
  • The providers ask for a minimum number of output tokens (4096 for OpenAI, 16000 for Anthropic and Gemini) whatever the caller passes, because reasoning counts against the cap and a small cap returns an empty answer.
  • A question and the skill classifier now pass their abort signal to the provider, so Ctrl+C and the classifier's five-second limit cancel the request of an API provider. claude-cli and ollama do not read the signal.
  • A turn the token limit cuts off inside a tool call is reported and the call is not run. A call whose arguments are not valid JSON, or that lacks a required argument, is not run either; the model gets an error result and can ask again. The MCP server makes the same check on required arguments.
  • The OpenAI tools are sent with strict: false. Left out, the Responses API applies strict mode and makes every optional parameter required: asked for the global rate-limit defaults, the model then sent {"rule_ident": ""}, and with strict: false it sends {}. An answer with no text is reported as empty.
  • The Anthropic provider leaves a message that is only whitespace out of the request. The API answers 400 to one (OpenAI and Gemini accept it), and the sidebar keeps such a message in the history when the model writes just a line break before a tool call.
  • There is no retry on 429 or 503; the error is shown as it comes.
  • loxitui ask created its provider without the [loxilb] settings, so with any provider its tools reached 127.0.0.1:11111 without a token. It now passes them, as the sidebar and doctor do. This changes ask on claude-cli too.
  • loxitui ask now writes the ai.query, ai.response and ai.error audit records the sidebar writes, so the audit log shows which provider and model a headless question went to. Until now ask left only cmd.dispatch, with any provider. After a provider error it waits for the log before it exits with code 2.

Not changed here:

  • Like the MCP server, the in-process tools authenticate with [loxilb] token only; a username/password login is not used.
  • setup writes config.toml with default file permissions, as before. An API key entered there is as readable as the [loxilb] token already is; leaving the prompt blank and using the environment variable avoids that.

The commits can be read one at a time: shared helpers, one commit per provider, the wiring, docs, then ask (the gateway settings and the audit records).

Type of change

  • Bug fix
  • New feature / enhancement
  • Documentation
  • CI / build / packaging / tooling
  • Refactor (no functional change)

How has this been tested?

  • bun run lint, bun run typecheck and bun test pass
  • Manually exercised against a LoxiLB instance (version: ghcr.io/loxilb-io/loxilb-inference-gateway:latest-u24, built 2026-11-15)

Linux, Bun 1.3.6 (the CI version): lint 0 errors, typecheck clean, 735 pass, 0 fail. Line coverage 69.27% (floor 60%), no schema drift, all four builds and the linux-x64 smoke test pass, scripts/release-hygiene.sh (with gitleaks) passes.

Windows 11, Bun 1.4.4: 734 pass, 0 fail.

tests/ai-providers.test.ts (37 tests) runs each provider against a fake fetch that delivers the event stream in 17-byte pieces: text, a full tool round with what is sent back on the second request, HTTP and stream errors, an answer or a tool call cut off by the token limit, tool arguments that are not valid JSON or lack a required one, an empty answer, a cancel while the model is silent, tool calls from a server that sends no index, the round cap, key validation, and key and provider selection. It also runs loxitui ask with each of the three providers and checks where the request went, what the audit log holds, and that a tool call reaches the gateway in the config with its token.

Against the real APIs on 2026-11-23, with the gateway above:

  • OpenAI (gpt-6.2-astra), Anthropic (claude-opus-5-5) and Gemini: validate(), a plain answer, an answer that needs tool calls ("how many LB rules are there"), a follow-up turn, and stopping mid-stream. A cancel 400 ms into a request ended it at once on all three (Gemini on gemini-3.5-flash-lite), and all three accepted a history that starts with an assistant message or repeats a role, which the sidebar can send after it trims or summarizes.
  • loxitui ask end to end and loxitui doctor with each provider; doctor also with a wrong key and an unreachable server.
  • On 2026-11-24, loxitui ask with each provider again: the audit log holds ai.query with the provider and model, the ai.tool_call and ai.response. The same day, OpenAI with gpt-4.1 and gpt-4o-mini, with and without a tool call, to check models without reasoning. And ask with [loxilb] port set to a port nothing listens on: the tool call is refused there, where it used to reach the default port.
  • The OpenAI-compatible path against Gemini's OpenAI-compatible endpoint. That run is where the extra tool-call fields turned out to be required.

Not tested:

  • A self-hosted OpenAI-compatible server (vLLM, llama.cpp, LM Studio).
  • Gemini's default model beyond one plain answer and one tool round. A free-tier key allows 20 requests a day on gemini-4-hydrogen and the API often answered 503; the remaining scenarios ran on gemini-3.5-flash-lite.
  • A reasoning item going back to OpenAI: none appeared in the live runs, so that path is covered by the fake-server test only.
  • A full answer from a compiled binary. On the linux-x64 build only doctor with a wrong Anthropic key was run: the request reaches the API and the 401 is reported.

Checklist

  • My commits are DCO signed-off (git commit -s)
  • I have not committed secrets, real infrastructure inventories, or credentials
  • No internal/planning references in comments or docs
  • Docs / CHANGELOG updated where relevant
  • A code owner (see .github/CODEOWNERS) has been requested for review

Related issues

Follows #15.

A provider that talks to a model API over HTTP has to do two things
the Claude CLI does on its own: read a server-sent event stream, and
run the tool calls the model asks for.

sseData() yields the data payload of each event wherever the chunks
are cut, with LF or CRLF line endings, and keeps a last event that has
no blank line after it. tool-calls.ts lists the read-only tools under
the mcp__loxilb__ names the system prompt and the skills already use,
runs a call through executeTool(), and turns a thrown error into a
result the model can read. A call whose arguments are not a JSON
object, or that lacks a required one, is not run, and the model is
told so; the MCP server makes the same check before it runs a tool.
One answer may take at most ten rounds of tool calls.

InferenceRequest gains disableTools, for a request that should be
answered from the prompt alone, and signal, to cancel one. The Claude
CLI and Ollama providers do not read either.

Signed-off-by: KP <kp@attlab.org>
OpenAiProvider streams from the Responses API and runs the read-only
tools in this process until the model answers. The current models
reject function tools on Chat Completions unless reasoning is turned
off, which gpt-6.2-astra does not accept, so the hosted API is reached
through /v1/responses. Requests are stateless (store: false): the
output items of a turn, encrypted reasoning included, go back
unchanged with the tool results. The tools carry strict: false; left
out, the API applies strict mode and makes every optional parameter a
required one.

OpenAiChatProvider speaks Chat Completions, for servers that implement
that API (vLLM, llama.cpp, LM Studio). It needs a model name and sends
a key only when one is set. Fields a server adds to a tool call are
returned with it; Gemini's OpenAI-compatible endpoint rejects the
follow-up request without them. Calls are told apart by index, and by
id when a server leaves the index out or repeats it.

Both ask for at least 4096 output tokens, because reasoning tokens
count against the cap. A turn the token limit cuts off inside a tool
call is reported and the call is not run, an answer with no text is
reported as empty, and the caller's signal cancels the request.

Signed-off-by: KP <kp@attlab.org>
AnthropicProvider streams from the Messages API and runs the read-only
tools in this process until the model answers. Content blocks are
rebuilt from the stream and sent back as received, thinking blocks and
their signatures included, since the API checks them on the request
that carries the tool results. All results of a turn travel in one
user message, and a call that threw is marked is_error.

Thinking is always on for claude-opus-5-5 and counts against
max_tokens, so the request asks for at least 16000. A refusal, a turn
that hits the token limit before any text and an answer with no text
are reported as errors. The caller's signal cancels the request.

A message that is only whitespace is left out of the request. The API
rejects one, and the sidebar keeps it in the history when the model
writes just a line break before a tool call.

Signed-off-by: KP <kp@attlab.org>
GeminiProvider streams from the Interactions API and runs the
read-only tools in this process until the model answers. Requests are
stateless (store: false): the conversation goes as input steps, and
the steps of a turn, thought signatures included, go back unchanged
with the function results.

max_output_tokens covers thinking as well, so the request asks for at
least 16000. A turn the token limit cuts off inside a function call is
reported and the call is not run, an answer with no text is reported
as empty, and the caller's signal cancels the request.

Signed-off-by: KP <kp@attlab.org>
[ai] provider now accepts openai, anthropic and gemini. The key comes
from [ai] api_key, or from OPENAI_API_KEY, ANTHROPIC_API_KEY or
GEMINI_API_KEY when that is empty. openai with a base_url is taken as
another server that speaks Chat Completions. It needs no key and is
sent only the one in [ai] api_key, never OPENAI_API_KEY. The config's
default model is a placeholder, so the providers read it as unset and
use their own default. The hosted APIs do the same with the sonnet
that setup writes for the Claude CLI; another server may serve a model
by that name, so there it is kept.

AiClient.isConfigured, which was always true, is false while an API
provider has no key, and the log-analysis shortcut uses the same
check. A question and the skill classifier now pass their abort signal
to the provider, so Ctrl+C and the classifier's five-second limit
cancel the request of an API provider; the classifier also asks those
providers for no tools, since it wants only a skill name.

setup asks for the model and the key, and for openai a base URL, in
which case the model has no default and stays unset when left blank.
doctor reports what the provider's validate() finds.

Signed-off-by: KP <kp@attlab.org>
The AI sidebar guide, the user guide and the README list the three
API providers and their key variables, and say what openai does with a
base_url. The sidebar guide also gives the default models, says how
the tool calls run (in loxitui, not in an MCP subprocess, with the
[loxilb] token), what leaves the machine for the provider's API, and
that a free-tier Gemini key allows only a few requests a day on the
default model.

Signed-off-by: KP <kp@attlab.org>
`loxitui ask` created its provider without the [loxilb] settings, so
the assistant's tools reached 127.0.0.1:11111 without a token whatever
the config said. It now passes them, as the sidebar and doctor do.

The sidebar records ai.query, ai.response and ai.error for every
question. ask wrote only cmd.dispatch, so the audit log did not show
which provider and model a headless question went to. It now writes
the same three records. After a provider error it waits for the log
before it exits with code 2, because process.exit does not wait for
writes that are still pending.

The tests run ask with each API provider and check where the request
went, what was recorded, and that a tool call reaches the configured
gateway with its token.

Signed-off-by: KP <kp@attlab.org>
@kp-attlab
kp-attlab marked this pull request as ready for review November 24, 2026 06:22
@NLX-SeokHwanKong
NLX-SeokHwanKong merged commit 0769797 into main Nov 26, 2026
12 of 13 checks passed
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