Home: point at My model keys when a personal key is missing

A provider with key_mode = "user" is useless until the person stores
their own key, and the only place to do that is Settings -> My model
keys. Home now says so (GET /me/model-keys, only when a key is missing
and the server can store one), and the chat shows the hub's own
sentence for model_provider_user_key_missing/_unreadable instead of
"Nie udalo sie: ...". chatBusy (from #32) keeps its name; the codes it
handles are one CHAT_REFUSALS set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 14:10:12 +01:00
co-authored by Claude Opus 5.5
parent 5c9c44c9ef
commit e776cb8a39
2 changed files with 36 additions and 5 deletions
+11
View File
@@ -160,3 +160,14 @@ def test_my_keys_is_everybodys_routed_screen_calling_only_real_routes(console, m
def test_the_key_mode_strings_are_in_both_languages(console, phrase):
assert phrase in console
assert phrase in _i18n(console), f"no English for {phrase!r}"
def test_home_points_at_my_keys_when_a_personal_key_is_missing(console):
home = console.split("async function viewHome(C) {", 1)[1].split("\nasync function loadHomeStats", 1)[0]
assert '<div id="homeKeys"></div>' in home and "loadHomeKeys();" in home
hint = home.split("async function loadHomeKeys() {", 1)[1]
assert "api('GET', '/me/model-keys')" in hint
assert "!p.key_prefix" in hint and "r.keys_writable" in hint
assert 'href="#/my-keys"' in hint and "esc(p.name)" in hint
# The chat says where to add the key instead of a bare "Nie udało się".
assert "'model_provider_user_key_missing'" in console.split("const CHAT_REFUSALS", 1)[1].split(";", 1)[0]
+25 -5
View File
@@ -681,6 +681,7 @@ const I18N = {
'Dostawcy modeli, którzy używają twojego własnego klucza API zamiast klucza firmy.': 'Model providers that use your own API key instead of the firm’s.',
'Żaden dostawca nie potrzebuje twojego klucza.': 'No provider needs your key.',
'Dodaj klucz': 'Add key',
'Twój własny klucz API jest potrzebny dla:': 'Your own API key is needed for:',
'Zmień klucz': 'Change key',
'Usunąć twój zapisany klucz API tego dostawcy?': 'Remove your stored API key for this provider?',
};
@@ -2435,9 +2436,10 @@ function chatVerify(text, sources, { question = '', review = null } = {}) {
return { hard, issues, retry: hard ? `W odpowiedzi są: ${said.join(', ')}, których nie ma w źródłach albo które się z nimi nie zgadzają. Odpowiedz ponownie tylko ze źródeł.` : '' };
}
// A refusal that is itself the sentence to show (`model_busy`: the running model and who may switch it). Not named
// chatRefusal: that is the coded no-access answer above, and a second declaration silently replaced it (every
// question answered "Error", 2026-11-06).
// A refusal that is itself the sentence to show (`model_busy`: the running model and who may switch it;
// a missing or unreadable personal key: where to add it). Not named chatRefusal: that is the coded no-access
// answer above, and a second declaration silently replaced it (every question answered "Error", 2026-11-06).
const CHAT_REFUSALS = new Set(['model_busy', 'model_provider_user_key_missing', 'model_provider_user_key_unreadable']);
function chatBusy(problem) {
const err = new Error(coreRefusal(problem) || problem.detail || problem.error);
err.refusal = true;
@@ -2457,7 +2459,7 @@ async function chatRound(model, messages, answer, onUpdate, { signal = chat.ctl.
let detail = 'HTTP ' + res.status;
let p = null;
try { p = await res.json(); detail = p.detail || p.title || detail; } catch { /* the status is the message */ }
if (p && p.code === 'model_busy') throw chatBusy(p);
if (p && CHAT_REFUSALS.has(p.code)) throw chatBusy(p);
throw new Error(detail);
}
const reader = res.body.getReader();
@@ -2481,7 +2483,7 @@ async function chatRound(model, messages, answer, onUpdate, { signal = chat.ctl.
if (ours) {
if (d.loading) answer.status = 'Ładuję model do pamięci karty graficznej, to trwa około minuty…';
if (d.loaded) answer.status = 'Myślę…';
if (d.error) throw d.code === 'model_busy' ? chatBusy(d) : new Error(d.error);
if (d.error) throw CHAT_REFUSALS.has(d.code) ? chatBusy(d) : new Error(d.error);
continue;
}
const c = (d.choices && d.choices[0]) || {};
@@ -3125,6 +3127,7 @@ async function viewHome(C) {
<button class="btn sm ghost home-new" type="button" onclick="chatReset()">Nowa rozmowa</button></div>
</form>
<div id="homeResults" class="home-results hidden" aria-live="polite"></div>` : '')
+ '<div id="homeKeys"></div>'
+ '<div id="homeStats" class="home-stats" aria-live="polite"></div>'
+ '<div id="homeModStats" class="home-stats" aria-live="polite"></div>'
/* Your apps are the sidebar; the home page mentions them only when there are none. */
@@ -3135,10 +3138,27 @@ async function viewHome(C) {
+ '</div>';
chatRestore();
chatRender();
loadHomeKeys();
loadHomeStats();
loadModuleStats();
}
/* A provider that calls with the asker's OWN key (key_mode = "user") is useless until they store one,
and the only place to do that is Settings → My model keys. Say so on Home, where the picker is,
instead of letting the first question fail. Quiet when nothing is missing or the call fails. */
async function loadHomeKeys() {
let r;
try { r = await api('GET', '/me/model-keys'); } catch { return; }
const box = $('homeKeys');
if (!box) return; // navigated away meanwhile
const missing = (r.providers || []).filter((p) => !p.key_prefix);
if (!missing.length || !r.keys_writable) return;
box.innerHTML = `<div class="banner"><span>Twój własny klucz API jest potrzebny dla:</span>
<span data-no-i18n>${missing.map((p) => esc(p.name)).join(', ')}</span>.
<a href="#/my-keys">Dodaj klucz</a></div>`;
localize(box);
}
/* The home page's numbers: yours for everybody, the platform's for an administrator. Loaded after
the page is drawn, so a slow query never holds up the search box; a failure just leaves it out. */
async function loadHomeStats() {