Repository navigation
Feature/custom OpenAI provider - #25
Merged
Merged
Conversation
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.
✅ Unit tests: PASS803 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
JVM unit tests ( |
9 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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'sOpenAI-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 andthe address.
CustomOpenAiEngine(core:engine), a standaloneLlmEngine. A server that refuses the optionaltemperatureis asked once more without it.
CustomEndpoint(core:model) reduces whatever the user pastes to a base address:…/chat/completionsis stripped;/v1is never added;Settings.customBaseUrl; it's not a secret). The key stays inSecureKeyStore.Replies from servers that aren't hosted providers
translationskey;englishortranslation;makes the reply malformed instead of crashing.
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)
Unavailable);HttpsOnlynetwork interceptor;ReplyCap; bigger isMalformed).LlmContract.outputBudget: 300 + 120 per item, atmost 8,192), so a translation note can't make one call write and bill thousands of tokens.
key (
keyHostChange); the key is never sent to a new server silently, and never deleted.usually can't follow the translation format.
default engine.
UI and docs
through a single view-model coroutine.
the address the user entered.
Testing
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;Notes for review
Thanks
Thanks to @marufmoinuddin for building the Custom AI engine. He wrote most of this branch:
EngineId.CUSTOM,CustomOpenAiEngine,CustomEndpoint, and the address field in the keysheet;
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.