Skip to content

Feature/custom OpenAI provider - #25

Merged
smnexstudio merged 5 commits into
mainfrom
feature/custom-openai-provider
Nov 14, 2026
Merged

smnexstudio merged 5 commits into
mainfrom
feature/custom-openai-provider

Conversation

@smnexstudio

Copy link
Copy Markdown
Owner

Custom AI: translate with your own OpenAI-compatible server

Adds a Custom AI engine. The user gives a server address, a model and an API key, and Panelglass sends a
text-only Chat Completions request to <address>/chat/completions. OpenAI, DeepSeek, OpenRouter, Gemini's
OpenAI-compatible endpoint and a home server (LM Studio, Ollama, llama.cpp behind HTTPS) all work through it, so
none of them needs an engine of its own.

The bubbles are read on the phone; no image is sent. Existing engines are unchanged.

What's in it

The engine

  • EngineId.CUSTOM: offered, needs the user's key, has no default model. The user must name both the model and
    the address.
  • CustomOpenAiEngine (core:engine), a standalone LlmEngine. A server that refuses the optional temperature
    is asked once more without it.
  • CustomEndpoint (core:model) reduces whatever the user pastes to a base address:
    • a pasted …/chat/completions is stripped;
    • the path is kept as written, and /v1 is never added;
    • HTTPS only, and no credentials, query or fragment in the address.
  • The address is kept in DataStore (Settings.customBaseUrl; it's not a secret). The key stays in
    SecureKeyStore.

Replies from servers that aren't hosted providers

  • The parser accepts:
    • a bare object instead of an array;
    • an array inside a code fence or under a translations key;
    • text under english or translation;
    • brackets inside a bubble's text;
    • content sent as a list of parts.
  • The contract is unchanged: every requested id exactly once, nothing invented, nothing reordered. A wrong type
    makes the reply malformed instead of crashing.
  • A malformed reply is retried once with a self-contained request (the input and the correction together), for the
    missing or garbled items only; good items are kept. Some local servers rewrite multi-turn history, so a
    "your previous reply was wrong" turn never worked there. Hosted providers keep their three-turn retry.

Hardening (all engine HTTP)

  • Engine calls get their own OkHttp client:
    • no redirects (a 3xx is Unavailable);
    • an HttpsOnly network interceptor;
    • a 4 MB reply cap (ReplyCap; bigger is Malformed).
  • Every cloud call sets an output limit from the item count (LlmContract.outputBudget: 300 + 120 per item, at
    most 8,192), so a translation note can't make one call write and bill thousands of tokens.
  • The key belongs to one host. Saving the sheet with a different host and no new key asks Keep key / Enter new
    key
    (keyHostChange); the key is never sent to a new server silently, and never deleted.
  • The sheet warns, without blocking, when the model name suggests a model of about 1B parameters or less, which
    usually can't follow the translation format.
  • The unused Claude, OpenAI, DeepSeek and Papago placeholder engines are removed. A saved one reads back as the
    default engine.

UI and docs

  • The key sheet gets an address field for this engine only. One save callback carries key, model and address
    through a single view-model coroutine.
  • New strings are in all 16 languages.
  • README, CHANGELOG and PRIVACY are updated: Custom AI sends the text read on the phone, the glossary and notes to
    the address the user entered.

Testing

  • Unit tests against MockWebServer (no test reaches the internet), covering:
    • CustomEndpointTest: address normalisation;
    • CustomOpenAiEngineTest: request shape and the temperature retry;
    • LlmContractTolerantParseTest: tolerant replies and salvage;
    • EngineHttpGuardTest: no redirects, HTTPS only, reply cap;
    • ApiKeyValidationTest: save rules and the host-change prompt;
    • existing engine and pipeline tests updated for the new engine.
  • On a phone (Motorola Edge 60 Pro): translated a Japanese Pepper&Carrot page through Custom AI → OpenRouter.

Notes for review

  • Custom AI is text only. Gemini keeps its own engine because it reads the bubble images itself.
  • A malformed reply costs at most one extra request, never more.

Thanks

Thanks to @marufmoinuddin for building the Custom AI engine. He wrote most of this branch:

  • the engine itself: EngineId.CUSTOM, CustomOpenAiEngine, CustomEndpoint, and the address field in the key
    sheet;
  • the output-token budget for Custom AI requests, and the updated test fixtures;
  • the tolerant parsing for replies from home servers, the self-contained retry that works when a server rewrites
    its history, and the small-model warning.

This PR builds on that work and hardens it: engine HTTP without redirects and with a reply cap, the
host-change key prompt, and a retry for only the items that failed. Thank you.

marufmoinuddin and others added 5 commits November 13, 2026 08:50
The user names the server's base address and the model in Settings and pastes
their key; the app sends a plain text Chat Completions request to
<base>/chat/completions and reads the text on the device, so no image ever
leaves the phone.

- EngineId.CUSTOM (offered, user key, no default model): the user must name
  both the model and the address.
- CustomEndpoint (core:model) reduces an address to its base: a pasted
  …/chat/completions is stripped, the path the user wrote is kept, no version
  segment is ever appended, and HTTPS is required.
- CustomOpenAiEngine (core:engine) is a standalone LlmEngine: text only, and a
  server that refuses the optional temperature is asked once more without it.
- The base URL lives in DataStore (an address, not a secret); the key stays in
  the existing SecureKeyStore.
- The key sheet gains an address field for this engine only, and one save
  callback carries the key, the model and the address through a single
  ViewModel coroutine.

Existing engines are untouched.
…ry, warn about tiny models

A server at home is not a hosted provider. It may answer with a bare
object instead of an array, with the array inside a code fence, wrapped in
a "translations" key, or with the translation under "english" or
"translation". It may also put brackets inside the text of a bubble, which
a naive cut would mistake for the array. The parser now tries the reply as
a whole, then as the outermost array, then as the outermost object, and
takes the first that yields a usable list of items.

What the reply must still carry is unchanged: every requested id exactly
once, nothing invented, nothing reordered. A wrong type anywhere makes
the reply unusable rather than throwing, so a bad reply can no longer
crash the app with a serialization error instead of saying "malformed".

The retry no longer depends on the conversation. Some local servers
rewrite a multi-turn history — the original input dropped, the order
reversed — so a retry that said "your previous reply was wrong" could
never work. The Custom AI engine now sends one fresh request that carries
the input and the correction together, and the system prompt shows what a
reply looks like when there is a single item. The hosted providers keep
the three-turn retry they had.

A reply whose content is a list of parts is read, and a reply with empty
model/created/usage fields is accepted.

The sheet warns when the model name gives away a model of about a billion
parameters or less, which usually cannot follow the translation format.
The warning does not stop the save.
@github-actions

Copy link
Copy Markdown

✅ Unit tests: PASS

803 of 803 passed · line coverage 58.1% (logic 68.5%, Compose UI 41.7%) · branch coverage 42.9%

Full HTML report (expected vs actual for every test) · Workflow run

Module Tests Passed Failed Skipped Lines Logic UI Branches
app 18 18 0 0 18.1% 6.4% 23.8% 17.4%
core/data 83 83 0 0 84.5% 84.5% – 67.6%
core/engine 151 151 0 0 78.9% 78.9% – 64.9%
core/model 67 67 0 0 91.7% 91.7% – 47.5%
core/ocr 45 45 0 0 45.9% 45.9% – 41.6%
core/pipeline 27 27 0 0 69.5% 69.5% – 47.3%
core/render 62 62 0 0 85.9% 85.9% – 67.8%
core/ui 18 18 0 0 39.9% 73.9% 36.5% 36.2%
feature/browser 49 49 0 0 20.6% 29.2% 0.0% 19.1%
feature/library 16 16 0 0 89.4% 100.0% 88.2% 68.2%
feature/settings 28 28 0 0 80.2% 66.0% 83.6% 49.5%
feature/studio 239 239 0 0 50.6% 67.7% 36.0% 36.5%

JVM unit tests (testDebugUnitTest, :core:model:test). Instrumented tests need a device and are not run in CI. Compose UI = files declaring @Composable functions; the rest, view models included, is logic.
Commit 74f1558.

@smnexstudio
smnexstudio merged commit 755b5ea into main Nov 14, 2026
2 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