|
8 | 8 | import RuixenInsights from '../ui/RuixenInsights.svelte'; |
9 | 9 | import DataManager from '../ui/DataManager.svelte'; |
10 | 10 | import Button from '../ui/Button.svelte'; |
| 11 | + import { toast } from '$lib/stores/toast'; |
11 | 12 | import EmptyState from '../ui/EmptyState.svelte'; |
12 | 13 | import Skeleton from '../ui/Skeleton.svelte'; |
13 | 14 | import { rightPanelView, uiHelpers } from '$lib/stores/ui'; |
|
29 | 30 | let selectedActivity = ''; |
30 | 31 | let isSubmitting = false; |
31 | 32 | let loading = false; |
32 | | - let aiEnabled = true; |
| 33 | + let apiKeyValid = false; |
| 34 | + let runningInsight = false; |
| 35 | + let runningWeekly = false; |
| 36 | + let weeklyCloudText: string | null = null; |
| 37 | + let weeklyForPetId: string | null = null; |
33 | 38 |
|
34 | 39 | // Computed values |
35 | 40 | $: lastEntry = selectedPet?.journalEntries?.length |
|
61 | 66 | } |
62 | 67 |
|
63 | 68 | onMount(() => { |
64 | | - // Reflect guardian preference for Ruixen insights |
| 69 | + // Reflect guardian API key validity for cloud capability |
65 | 70 | guardianStore.subscribe((g) => { |
66 | | - aiEnabled = !!(g && g.preferences && g.preferences.aiInsights); |
| 71 | + apiKeyValid = !!g?.apiKeyValid && !!g?.apiKey; |
67 | 72 | }); |
68 | 73 |
|
69 | 74 | // Subscribe first so incoming loads propagate into state |
|
92 | 97 | selectedPetStore.subscribe((petId) => { |
93 | 98 | selectedPetId = petId; |
94 | 99 | selectedPet = petId ? pets.find((p) => p.id === petId) || null : null; |
| 100 | + // Clear weekly cloud text if switching pets |
| 101 | + if (weeklyForPetId && petId !== weeklyForPetId) { |
| 102 | + weeklyCloudText = null; |
| 103 | + weeklyForPetId = null; |
| 104 | + } |
95 | 105 | // Keep current view unless user explicitly opened memories. |
96 | 106 | // Disable actions via disabled buttons when archived is selected. |
97 | 107 | }); |
|
126 | 136 | // Add entry to pet |
127 | 137 | petHelpers.addJournalEntry(selectedPet.id, entry); |
128 | 138 |
|
129 | | - // Analyze with Ruixen orchestrator (rate-limited with offline fallback) if enabled |
130 | | - if (aiEnabled) { |
131 | | - try { |
132 | | - const res = await ruixenHelpers.analyzeDaily(selectedPet, entry); |
133 | | - if (res) { |
134 | | - analysisStore.update((cache) => ({ ...cache, [entry.id]: res })); |
135 | | - // Persist onto the entry so it survives reloads and exports |
136 | | - const petNow = petHelpers.getPet(selectedPet.id); |
137 | | - if (petNow) { |
138 | | - const updatedEntries = (petNow.journalEntries || []).map((e) => |
139 | | - e.id === entry.id |
140 | | - ? { |
141 | | - ...e, |
142 | | - aiAnalysis: { |
143 | | - ...res, |
144 | | - modelId: (selectedPet as any)?.model || undefined, |
145 | | - analyzedAt: new Date().toISOString(), |
146 | | - }, |
147 | | - } |
148 | | - : e |
149 | | - ); |
150 | | - petHelpers.update(petNow.id, { journalEntries: updatedEntries }); |
151 | | - } |
152 | | - } |
153 | | - } catch (error) { |
154 | | - console.error('Ruixen analysis failed:', error); |
155 | | - } |
156 | | - } |
| 139 | + // Skip automatic analysis; allow manual triggers via buttons |
157 | 140 |
|
158 | 141 | // Reset form |
159 | 142 | journalInput = ''; |
|
169 | 152 | isSubmitting = false; |
170 | 153 | } |
171 | 154 | } |
| 155 | +
|
| 156 | + async function runLastEntryInsight() { |
| 157 | + if (!selectedPet || !(selectedPet.journalEntries || []).length) return; |
| 158 | + runningInsight = true; |
| 159 | + try { |
| 160 | + if (!apiKeyValid) { |
| 161 | + toast.info('Ruixen is offline', 'Set your API key in Guardian to run cloud analysis'); |
| 162 | + } |
| 163 | + const res = await ruixenHelpers.analyzeLastEntryNow(selectedPet); |
| 164 | + const last = (selectedPet.journalEntries || []).slice(-1)[0]; |
| 165 | + if (res && last) { |
| 166 | + analysisStore.update((cache) => ({ ...cache, [last.id]: res })); |
| 167 | + const petNow = petHelpers.getPet(selectedPet.id); |
| 168 | + if (petNow) { |
| 169 | + const updatedEntries = (petNow.journalEntries || []).map((e) => |
| 170 | + e.id === last.id |
| 171 | + ? { |
| 172 | + ...e, |
| 173 | + aiAnalysis: { |
| 174 | + ...res, |
| 175 | + modelId: (selectedPet as any)?.model || undefined, |
| 176 | + analyzedAt: new Date().toISOString(), |
| 177 | + }, |
| 178 | + } |
| 179 | + : e |
| 180 | + ); |
| 181 | + petHelpers.update(petNow.id, { journalEntries: updatedEntries }); |
| 182 | + } |
| 183 | + toast.success('Ruixen insight ready', 'Latest entry analyzed'); |
| 184 | + } else { |
| 185 | + toast.info('No entry to analyze'); |
| 186 | + } |
| 187 | + } catch (e) { |
| 188 | + const msg = String((e as Error)?.message || e); |
| 189 | + if (/429|Rate limit exceeded/i.test(msg)) { |
| 190 | + toast.info('Ruixen: Daily free limit reached', 'Try again tomorrow or add credits to OpenRouter'); |
| 191 | + } else { |
| 192 | + toast.error('Insight failed', (e as Error).message); |
| 193 | + } |
| 194 | + } finally { |
| 195 | + runningInsight = false; |
| 196 | + } |
| 197 | + } |
| 198 | +
|
| 199 | + async function runWeeklyCloud() { |
| 200 | + if (!selectedPet) return; |
| 201 | + runningWeekly = true; |
| 202 | + weeklyCloudText = null; |
| 203 | + try { |
| 204 | + if (!apiKeyValid) { |
| 205 | + toast.info('Ruixen is offline', 'Weekly cloud analysis requires an API key'); |
| 206 | + return; |
| 207 | + } |
| 208 | + const text = await ruixenHelpers.analyzeWeeklyCloud(selectedPet); |
| 209 | + if (text) { |
| 210 | + weeklyCloudText = text; |
| 211 | + weeklyForPetId = selectedPet.id; |
| 212 | + toast.success('Weekly cloud analysis ready'); |
| 213 | + } else { |
| 214 | + toast.warning('Weekly analysis unavailable', 'Please try again later'); |
| 215 | + } |
| 216 | + } catch (e) { |
| 217 | + const msg = String((e as Error)?.message || e); |
| 218 | + if (/429|Rate limit exceeded/i.test(msg)) { |
| 219 | + toast.info('Ruixen: Daily free limit reached', 'Try again tomorrow or add credits to OpenRouter'); |
| 220 | + } else { |
| 221 | + toast.error('Weekly analysis failed', (e as Error).message); |
| 222 | + } |
| 223 | + } finally { |
| 224 | + runningWeekly = false; |
| 225 | + } |
| 226 | + } |
172 | 227 | </script> |
173 | 228 |
|
174 | 229 | <div class="viewport-container h-full flex flex-col font-mono"> |
|
505 | 560 | <Brain size={16} class="mr-2" style="color: var(--petalytics-accent);" /> |
506 | 561 | Ruixen Insights |
507 | 562 | </h3> |
| 563 | + <div class="flex gap-2 mb-3"> |
| 564 | + <Button |
| 565 | + variant="secondary" |
| 566 | + disabled={!selectedPet?.journalEntries?.length || runningInsight || isArchived(selectedPet)} |
| 567 | + onclick={runLastEntryInsight} |
| 568 | + > |
| 569 | + {#if runningInsight} |
| 570 | + <div class="animate-spin w-4 h-4 border-2 border-current border-t-transparent rounded-full"></div> |
| 571 | + <span>Running…</span> |
| 572 | + {:else} |
| 573 | + <span>Analyze latest entry</span> |
| 574 | + {/if} |
| 575 | + </Button> |
| 576 | + <Button |
| 577 | + variant="secondary" |
| 578 | + disabled={!selectedPet || runningWeekly || isArchived(selectedPet)} |
| 579 | + onclick={runWeeklyCloud} |
| 580 | + > |
| 581 | + {#if runningWeekly} |
| 582 | + <div class="animate-spin w-4 h-4 border-2 border-current border-t-transparent rounded-full"></div> |
| 583 | + <span>Weekly summary…</span> |
| 584 | + {:else} |
| 585 | + <span>Run 1‑week analysis</span> |
| 586 | + {/if} |
| 587 | + </Button> |
| 588 | + </div> |
508 | 589 | {#if selectedPet.journalEntries.length === 0} |
509 | 590 | <p class="text-sm" style="color: var(--petalytics-subtle);"> |
510 | 591 | Add journal entries to get AI-powered insights about {selectedPet.name}'s |
511 | 592 | well-being. |
512 | 593 | </p> |
513 | | - {:else if aiEnabled} |
514 | | - <AIInsightsCard petId={selectedPet.id} entryId={lastEntry?.id} compact={true} /> |
515 | 594 | {:else} |
516 | | - <p class="text-xs" style="color: var(--petalytics-subtle);"> |
517 | | - Ruixen: offline (enable insights or add API key) |
518 | | - </p> |
| 595 | + <AIInsightsCard petId={selectedPet.id} entryId={lastEntry?.id} compact={true} /> |
519 | 596 | {/if} |
520 | 597 | </div> |
521 | 598 | </div> |
522 | 599 |
|
523 | 600 | {#if selectedPet} |
524 | 601 | <div class="mt-4"> |
525 | | - <RuixenInsights pet={selectedPet} /> |
| 602 | + <RuixenInsights pet={selectedPet} cloudWeekly={weeklyCloudText} /> |
526 | 603 | </div> |
527 | 604 | {/if} |
528 | 605 | </div> |
|
0 commit comments