Skip to content

Commit 0358c39

Browse files
committed
ai: implement backend model mapping for analyzer to improve fallback handling
1 parent d04fe17 commit 0358c39

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/lib/stores/ruixen.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,20 @@ class Ruixen {
125125
guardianStore.subscribe((g) => {
126126
this.apiKey = g.apiKey || '';
127127
this.model = g.model || 'openai/gpt-oss-120b:free';
128-
this.analyzer = g.apiKey && g.apiKeyValid ? new AIAnalyzer(g.apiKey, this.model) : null;
128+
// Map problematic models to working alternatives for backend calls
129+
const backendModel = this.getBackendModel(this.model);
130+
this.analyzer = g.apiKey ? new AIAnalyzer(g.apiKey, backendModel) : null;
129131
});
130132
}
131133

134+
private getBackendModel(frontendModel: string): string {
135+
// Map problematic models to working alternatives
136+
if (frontendModel === 'openai/gpt-oss-120b:free') {
137+
return 'deepseek/deepseek-r1:free';
138+
}
139+
return frontendModel;
140+
}
141+
132142
requestAnalysis(
133143
pet: PetPanelData,
134144
entry: JournalEntry | PetJournalEntry
@@ -402,23 +412,26 @@ class Ruixen {
402412
): Promise<AnalysisResult> {
403413
if (!this.analyzer) return this.offlineHeuristic(pet, entry);
404414
for (let i = 0; i < this.fallbackModels.length; i++) {
405-
const model = this.fallbackModels[i];
415+
const frontendModel = this.fallbackModels[i];
416+
const backendModel = this.getBackendModel(frontendModel);
406417
try {
407-
const res = await this.analyzer.analyzeJournalEntry(pet, entry, model);
418+
const res = await this.analyzer.analyzeJournalEntry(pet, entry, backendModel);
419+
// Tag with frontend model name for display
420+
res.modelUsed = frontendModel;
408421
return res;
409422
} catch (e) {
410423
const msg = String((e as Error)?.message || e);
411424
const is429 = /429|Rate limit exceeded/i.test(msg);
412425
const providerUpstream = /temporarily rate-limited upstream/i.test(msg);
413426
if (!is429) {
414-
console.warn('Model attempt failed (non-429), aborting fallbacks:', model, e);
427+
console.warn('Model attempt failed (non-429), aborting fallbacks:', backendModel, e);
415428
return this.offlineHeuristic(pet, entry);
416429
}
417430
console.warn(
418431
providerUpstream
419432
? 'Model upstream-limited, trying next free model:'
420433
: 'Model rate-limited, trying next free model:',
421-
model
434+
backendModel
422435
);
423436
if (i === this.fallbackModels.length - 1) {
424437
const today = new Date().toDateString();
@@ -435,15 +448,16 @@ class Ruixen {
435448
private async tryWeeklyModelsSequentially(pet: PetPanelData): Promise<string | null> {
436449
if (!this.analyzer) return null;
437450
for (let i = 0; i < this.fallbackModels.length; i++) {
438-
const model = this.fallbackModels[i];
451+
const frontendModel = this.fallbackModels[i];
452+
const backendModel = this.getBackendModel(frontendModel);
439453
try {
440-
const text = await this.analyzer.analyzeWeeklySummary(pet, model);
441-
return `(${model})\n${text}`;
454+
const text = await this.analyzer.analyzeWeeklySummary(pet, backendModel);
455+
return `(${frontendModel})\n${text}`;
442456
} catch (e) {
443457
const msg = String((e as Error)?.message || e);
444458
const is429 = /429|Rate limit exceeded/i.test(msg);
445459
if (!is429) {
446-
console.warn('Weekly model failed (non-429), aborting fallbacks:', model, e);
460+
console.warn('Weekly model failed (non-429), aborting fallbacks:', backendModel, e);
447461
return null;
448462
}
449463
if (i === this.fallbackModels.length - 1) {
@@ -472,7 +486,7 @@ export const ruixenHelpers = {
472486
return ruixen.analyzeWeeklyCloud(pet);
473487
},
474488
setModel(model: string) {
475-
guardianStore.update((g) => ({ ...g, model } as any));
489+
guardianStore.update((g) => ({ ...g, model }) as any);
476490
return model;
477491
},
478492
weeklySummary(pet: PetPanelData) {

0 commit comments

Comments
 (0)