@@ -183,6 +183,7 @@ import { useLogger } from '@/composables/useLogger'
183
183
import { useToast } from ' @/composables/useToast'
184
184
import { fromError } from ' @/utils/error'
185
185
import { useI18n } from ' @/utils/i18n'
186
+ import { extractValueOfKeyByPattern } from ' @/utils/json/parser/pattern-extractor'
186
187
import type { LanguageCode } from ' @/utils/language/detect'
187
188
import { getLanguageName , SUPPORTED_LANGUAGES } from ' @/utils/language/detect'
188
189
import { useOllamaStatusStore } from ' @/utils/pinia-store/store'
@@ -459,8 +460,11 @@ const start = async () => {
459
460
460
461
logger .debug (' Gmail compose prompt:' , { systemPrompt , userPrompt })
461
462
462
- const retry = 0
463
+ let retry = 0
464
+ let text = ' '
463
465
do {
466
+ text = ' '
467
+ retry += 1
464
468
logger .debug (` Gmail compose attempt #${retry + 1 } ` )
465
469
const iter = streamObjectInBackground ({
466
470
prompt: userPrompt ,
@@ -470,14 +474,26 @@ const start = async () => {
470
474
})
471
475
472
476
for await (const part of iter ) {
473
- // Extract the structured result
474
477
if (part .type === ' object' ) {
475
478
runningStatus .value = ' streaming'
476
479
optimizedSubject .value = part .object .subject || ' '
477
480
optimizedBody .value = part .object .body || ' '
478
481
}
482
+ else if (part .type === ' text-delta' ) {
483
+ text += part .textDelta
484
+ }
479
485
}
480
- } while ((! optimizedSubject .value || ! optimizedBody .value ) && retry < 3 )
486
+ } while ((! optimizedSubject .value || ! optimizedBody .value ) && retry < 2 )
487
+
488
+ // Fallback: try to extract from raw text if structured extraction failed
489
+ if (! optimizedSubject .value ) {
490
+ const extractSubjectMatch = extractValueOfKeyByPattern (text , ' subject' )
491
+ if (extractSubjectMatch ) optimizedSubject .value = extractSubjectMatch
492
+ }
493
+ if (! optimizedBody .value ) {
494
+ const extractBodyMatch = extractValueOfKeyByPattern (text , ' body' )
495
+ if (extractBodyMatch ) optimizedBody .value = extractBodyMatch
496
+ }
481
497
482
498
logger .debug (' Gmail compose structured response:' , {
483
499
subject: optimizedSubject .value ,
0 commit comments