@@ -23,7 +23,7 @@ export class AIAnalyzer {
23
23
24
24
async analyzeJournalEntry ( pet : PetPanelData , entry : JournalEntry ) : Promise < AnalysisResult > {
25
25
const prompt = this . buildAnalysisPrompt ( pet , entry ) ;
26
- const referer = typeof window !== 'undefined' ? window . location . origin : undefined ;
26
+ const referer = typeof window !== 'undefined' ? window . location . origin : undefined ;
27
27
28
28
if ( typeof window !== 'undefined' && ( import . meta as any ) ?. env ?. DEV ) {
29
29
console . debug ( '[Ruixen] Prompt (journal)' , {
@@ -34,14 +34,14 @@ export class AIAnalyzer {
34
34
}
35
35
36
36
try {
37
- const response = await fetch ( this . baseUrl , {
37
+ const response = await fetch ( this . baseUrl , {
38
38
method : 'POST' ,
39
39
headers : {
40
40
'Content-Type' : 'application/json' ,
41
41
} ,
42
42
body : JSON . stringify ( {
43
- apiKey : this . apiKey ,
44
- model : this . model ,
43
+ apiKey : this . apiKey ,
44
+ model : this . model ,
45
45
messages : [
46
46
{
47
47
role : 'system' ,
@@ -79,11 +79,12 @@ export class AIAnalyzer {
79
79
}
80
80
81
81
async analyzeWeeklySummary ( pet : PetPanelData ) : Promise < string > {
82
- const sevenDays = ( pet . journalEntries || [ ] )
83
- . slice ( )
84
- . filter ( ( e ) => Date . now ( ) - new Date ( e . date as any ) . getTime ( ) <= 7 * 24 * 60 * 60 * 1000 )
85
- . map ( ( e ) => `- ${ new Date ( e . date as any ) . toLocaleDateString ( ) } : ${ e . content } ` )
86
- . join ( '\n' ) || 'No entries in last 7 days.' ;
82
+ const sevenDays =
83
+ ( pet . journalEntries || [ ] )
84
+ . slice ( )
85
+ . filter ( ( e ) => Date . now ( ) - new Date ( e . date as any ) . getTime ( ) <= 7 * 24 * 60 * 60 * 1000 )
86
+ . map ( ( e ) => `- ${ new Date ( e . date as any ) . toLocaleDateString ( ) } : ${ e . content } ` )
87
+ . join ( '\n' ) || 'No entries in last 7 days.' ;
87
88
88
89
const prompt = `Summarize the last 7 days for ${ pet . name } in 4-6 concise bullet points focused on mood, activity, appetite, and any warning signs.
89
90
@@ -96,20 +97,20 @@ STRICT RULES:
96
97
PET: ${ pet . name } (${ pet . breed || pet . species || 'pet' } , ${ pet . age ?? 'unknown' } y)
97
98
LAST 7 DAYS:\n${ sevenDays } ` ;
98
99
99
- const referer = typeof window !== 'undefined' ? window . location . origin : undefined ;
100
- const response = await fetch ( this . baseUrl , {
100
+ const referer = typeof window !== 'undefined' ? window . location . origin : undefined ;
101
+ const response = await fetch ( this . baseUrl , {
101
102
method : 'POST' ,
102
103
headers : {
103
104
'Content-Type' : 'application/json' ,
104
105
} ,
105
106
body : JSON . stringify ( {
106
- apiKey : this . apiKey ,
107
- model : this . model ,
107
+ apiKey : this . apiKey ,
108
+ model : this . model ,
108
109
messages : [
109
110
{
110
111
role : 'system' ,
111
112
content :
112
- 'You are a veterinary AI assistant. Be concise and factual; never write fiction or narrative prose. Use only provided inputs.'
113
+ 'You are a veterinary AI assistant. Be concise and factual; never write fiction or narrative prose. Use only provided inputs.' ,
113
114
} ,
114
115
{ role : 'user' , content : prompt } ,
115
116
] ,
@@ -206,10 +207,7 @@ If the entry lacks enough information, use conservative defaults (moodTrend: "st
206
207
let candidate = content . substring ( braceIndex , lastBrace + 1 ) ;
207
208
candidate = candidate . replace ( / , \s * ( [ } \] ] ) / g, '$1' ) ;
208
209
// Ensure summary is a closed string (fix common unterminated quote cases)
209
- candidate = candidate . replace (
210
- / ( " s u m m a r y " \s * : \s * " [ ^ " } ] * ) $ / m,
211
- ( match ) => match + '"'
212
- ) ;
210
+ candidate = candidate . replace ( / ( " s u m m a r y " \s * : \s * " [ ^ " } ] * ) $ / m, ( match ) => match + '"' ) ;
213
211
return JSON . parse ( candidate ) ;
214
212
}
215
213
}
@@ -223,7 +221,9 @@ If the entry lacks enough information, use conservative defaults (moodTrend: "st
223
221
. split ( '\n' ) [ 0 ]
224
222
. trim ( ) ;
225
223
return {
226
- summary : summary ? summary . slice ( 0 , 180 ) + ( summary . length > 180 ? '…' : '' ) : 'No summary available' ,
224
+ summary : summary
225
+ ? summary . slice ( 0 , 180 ) + ( summary . length > 180 ? '…' : '' )
226
+ : 'No summary available' ,
227
227
moodTrend : 'stable' ,
228
228
activityLevel : 'normal' ,
229
229
healthConcerns : [ ] ,
@@ -243,7 +243,8 @@ If the entry lacks enough information, use conservative defaults (moodTrend: "st
243
243
) : AnalysisResult {
244
244
const safe = { ...res } as AnalysisResult ;
245
245
const s = String ( safe . summary || '' ) . trim ( ) ;
246
- const looksLikeTitle = / : \s | — | – / . test ( s ) && / ^ [ A - Z ] [ ^ . ! ? ] { 5 , 60 } $ / . test ( s . split ( / [ . ! ? ] / ) [ 0 ] || '' ) ;
246
+ const looksLikeTitle =
247
+ / : \s | — | – / . test ( s ) && / ^ [ A - Z ] [ ^ . ! ? ] { 5 , 60 } $ / . test ( s . split ( / [ . ! ? ] / ) [ 0 ] || '' ) ;
247
248
const hasYearOrPerson = / ( 1 8 | 1 9 | 2 0 ) \d { 2 } / . test ( s ) || / \b [ A - Z ] [ a - z ] + \s + [ A - Z ] [ a - z ] + \b / . test ( s ) ;
248
249
const notAboutPet = / ( d i a r y | n o v e l | c h a p t e r | s t o r y | k e e p e r ) / i. test ( s ) ;
249
250
const hasSpecialTokens = / < \| .* ?\| > | ` ` ` / . test ( s ) ;
@@ -284,16 +285,16 @@ If the entry lacks enough information, use conservative defaults (moodTrend: "st
284
285
285
286
async testConnection ( ) : Promise < boolean > {
286
287
try {
287
- const referer = typeof window !== 'undefined' ? window . location . origin : undefined ;
288
- const response = await fetch ( this . baseUrl , {
288
+ const referer = typeof window !== 'undefined' ? window . location . origin : undefined ;
289
+ const response = await fetch ( this . baseUrl , {
289
290
method : 'POST' ,
290
291
headers : {
291
292
'Content-Type' : 'application/json' ,
292
293
} ,
293
294
body : JSON . stringify ( {
294
- apiKey : this . apiKey ,
295
- model : this . model ,
296
- messages : [ { role : 'user' , content : 'test' } ] ,
295
+ apiKey : this . apiKey ,
296
+ model : this . model ,
297
+ messages : [ { role : 'user' , content : 'test' } ] ,
297
298
max_tokens : 1 ,
298
299
} ) ,
299
300
} ) ;
0 commit comments