Skip to content

Commit e06ef3d

Browse files
committed
ai: short-circuit to offline heuristic when free daily limit exhausted to avoid spinner hang
1 parent aa2cf91 commit e06ef3d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/lib/stores/ruixen.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ class Ruixen {
157157
return;
158158
}
159159

160+
// If daily free limit is exhausted for the chosen model, short-circuit to offline
161+
if (isFreeModel(this.model)) {
162+
const today = new Date().toDateString();
163+
const d = loadDailyCount();
164+
if (getExhausted() === today || d.count >= DAILY_FREE_LIMIT) {
165+
resolve(this.offlineHeuristic(pet, fullEntry));
166+
return;
167+
}
168+
}
169+
160170
// Else enqueue to respect rate limits
161171
this.queue.push({ pet, entry, resolve, reject });
162172
this.queueSize.set(this.queue.length);
@@ -195,6 +205,17 @@ class Ruixen {
195205
}
196206

197207
if (!this.limiter.canRequest(this.model)) {
208+
// If the daily free limit is exhausted, resolve offline immediately
209+
if (isFreeModel(this.model)) {
210+
const today = new Date().toDateString();
211+
const d = loadDailyCount();
212+
if (getExhausted() === today || d.count >= DAILY_FREE_LIMIT) {
213+
next.resolve(this.offlineHeuristic(next.pet, this.toJournalEntry(next.entry, next.pet)));
214+
this.queue.shift();
215+
this.queueSize.set(this.queue.length);
216+
continue;
217+
}
218+
}
198219
const wait = this.limiter.msUntilAvailable();
199220
await new Promise((r) => setTimeout(r, Math.min(wait, 1500)));
200221
continue;

0 commit comments

Comments
 (0)