@@ -36,12 +36,8 @@ import {
3636} from "../../../shared/text/assistant-visible-text.js" ;
3737import { classifyOAuthRefreshFailure } from "../../auth-profiles/oauth-refresh-failure.js" ;
3838import {
39- BILLING_ERROR_USER_MESSAGE ,
4039 formatAssistantErrorText ,
41- formatRawAssistantErrorForUi ,
4240 formatUserFacingAssistantErrorText ,
43- getApiErrorPayloadFingerprint ,
44- isRawApiErrorPayload ,
4541 normalizeTextForComparison ,
4642} from "../../embedded-agent-helpers.js" ;
4743import { SYNTHESIZED_TIMEOUT_ERROR_TEXT } from "../../embedded-agent-helpers/error-text.js" ;
@@ -120,10 +116,6 @@ function resolveRawAssistantAnswerText(lastAssistant: AssistantMessage | undefin
120116 ) ;
121117}
122118
123- function normalizeReplyTextForComparison ( text : string ) : string {
124- return normalizeTextForComparison ( parseReplyDirectives ( text ) . text ?? "" ) ;
125- }
126-
127119/**
128120 * Converts a completed embedded attempt into reply payloads for channels. This
129121 * is the boundary that suppresses duplicate source replies, filters raw API
@@ -250,21 +242,6 @@ export function buildEmbeddedRunPayloads(params: {
250242 authMode : params . authMode ,
251243 } )
252244 : undefined ;
253- const rawErrorFingerprint = rawErrorMessage
254- ? getApiErrorPayloadFingerprint ( rawErrorMessage )
255- : null ;
256- const formattedRawErrorMessage = rawErrorMessage
257- ? formatRawAssistantErrorForUi ( rawErrorMessage )
258- : null ;
259- const normalizedFormattedRawErrorMessage = formattedRawErrorMessage
260- ? normalizeTextForComparison ( formattedRawErrorMessage )
261- : null ;
262- const normalizedRawErrorText = rawErrorMessage
263- ? normalizeTextForComparison ( rawErrorMessage )
264- : null ;
265- const normalizedErrorText = errorText ? normalizeTextForComparison ( errorText ) : null ;
266- const normalizedGenericBillingErrorText = normalizeTextForComparison ( BILLING_ERROR_USER_MESSAGE ) ;
267- const genericErrorText = "The AI service returned an error. Please try again." ;
268245 const deferAssistantTimeoutError =
269246 params . deferAssistantTimeoutError === true &&
270247 rawErrorMessage !== undefined &&
@@ -290,79 +267,38 @@ export function buildEmbeddedRunPayloads(params: {
290267 ? extractAssistantVisibleText ( assistantForPayload )
291268 : "" ;
292269 const fallbackRawAnswerText = resolveRawAssistantAnswerText ( assistantForPayload ) ;
293- const shouldSuppressRawErrorText = ( text : string ) => {
294- if ( ! lastAssistantNeedsErrorSurface ) {
295- return false ;
296- }
297- const trimmed = text . trim ( ) ;
298- if ( ! trimmed ) {
299- return false ;
300- }
301- if ( errorText ) {
302- const normalized = normalizeTextForComparison ( trimmed ) ;
303- if ( normalized && normalizedErrorText && normalized === normalizedErrorText ) {
304- return true ;
305- }
306- if ( trimmed === genericErrorText ) {
307- return true ;
308- }
309- if (
310- normalized &&
311- normalizedGenericBillingErrorText &&
312- normalized === normalizedGenericBillingErrorText
313- ) {
314- return true ;
315- }
316- }
317- if ( rawErrorMessage && trimmed === rawErrorMessage ) {
318- return true ;
319- }
320- if ( formattedRawErrorMessage && trimmed === formattedRawErrorMessage ) {
321- return true ;
322- }
323- if ( normalizedRawErrorText ) {
324- const normalized = normalizeTextForComparison ( trimmed ) ;
325- if ( normalized && normalized === normalizedRawErrorText ) {
326- return true ;
327- }
328- }
329- if ( normalizedFormattedRawErrorMessage ) {
330- const normalized = normalizeTextForComparison ( trimmed ) ;
331- if ( normalized && normalized === normalizedFormattedRawErrorMessage ) {
332- return true ;
333- }
334- }
335- if ( rawErrorFingerprint ) {
336- const fingerprint = getApiErrorPayloadFingerprint ( trimmed ) ;
337- if ( fingerprint && fingerprint === rawErrorFingerprint ) {
338- return true ;
339- }
340- }
341- return isRawApiErrorPayload ( trimmed ) ;
342- } ;
343270 const rawAnswerDirectiveState = fallbackRawAnswerText
344271 ? parseReplyDirectives ( fallbackRawAnswerText )
345272 : null ;
346273 const rawAnswerHasMedia =
347274 ( rawAnswerDirectiveState ?. mediaUrls ?. length ?? 0 ) > 0 || rawAnswerDirectiveState ?. audioAsVoice ;
348- const assistantTextsHaveMedia = params . assistantTexts . some ( ( text ) => {
349- const parsed = parseReplyDirectives ( text ) ;
350- return ( parsed . mediaUrls ?. length ?? 0 ) > 0 || parsed . audioAsVoice ;
351- } ) ;
352- const normalizedAssistantTexts = normalizeTextForComparison ( nonEmptyAssistantTexts . join ( "\n\n" ) ) ;
353- const normalizedRawAnswerText = normalizeTextForComparison ( rawAnswerDirectiveState ?. text ?? "" ) ;
275+ const normalizedAssistantTexts =
276+ rawAnswerHasMedia &&
277+ nonEmptyAssistantTexts . length > 0 &&
278+ ! params . assistantTexts . some ( ( text ) => {
279+ const parsed = parseReplyDirectives ( text ) ;
280+ return ( parsed . mediaUrls ?. length ?? 0 ) > 0 || parsed . audioAsVoice ;
281+ } )
282+ ? normalizeTextForComparison ( nonEmptyAssistantTexts . join ( "\n\n" ) )
283+ : "" ;
354284 const shouldPreferRawAnswerText =
355285 rawAnswerHasMedia &&
356286 ( ! nonEmptyAssistantTexts . length ||
357- ( ! assistantTextsHaveMedia &&
358- normalizedAssistantTexts . length > 0 &&
359- normalizedAssistantTexts === normalizedRawAnswerText ) ) ;
287+ ( normalizedAssistantTexts . length > 0 &&
288+ normalizedAssistantTexts ===
289+ normalizeTextForComparison ( rawAnswerDirectiveState ?. text ?? "" ) ) ) ;
360290 // When streamed text lost media directives but the canonical assistant answer
361291 // still contains them, keep the raw answer so attachments are not dropped.
362292 const fallbackAnswerSourceText =
363293 shouldPreferRawAnswerText && fallbackRawAnswerText ? fallbackRawAnswerText : fallbackAnswerText ;
364- const normalizedFallbackAnswerSourceText = fallbackAnswerSourceText
365- ? normalizeReplyTextForComparison ( fallbackAnswerSourceText )
294+ const fallbackAnswerDirectiveState =
295+ fallbackAnswerSourceText === fallbackRawAnswerText
296+ ? rawAnswerDirectiveState
297+ : fallbackAnswerSourceText
298+ ? parseReplyDirectives ( fallbackAnswerSourceText )
299+ : null ;
300+ const normalizedFallbackAnswerSourceText = fallbackAnswerDirectiveState
301+ ? normalizeTextForComparison ( fallbackAnswerDirectiveState . text )
366302 : "" ;
367303 const shouldUseCanonicalFinalAnswer =
368304 ! lastAssistantNeedsErrorSurface &&
@@ -372,16 +308,19 @@ export function buildEmbeddedRunPayloads(params: {
372308 const answerTexts =
373309 suppressAssistantArtifacts || runAborted || lastAssistantNeedsErrorSurface
374310 ? [ ]
375- : ( shouldUseCanonicalFinalAnswer
376- ? [ fallbackAnswerSourceText ]
377- : shouldPreferRawAnswerText && fallbackRawAnswerText
378- ? [ fallbackRawAnswerText ]
379- : hasAssistantTextPayload
380- ? nonEmptyAssistantTexts
381- : fallbackAnswerText
382- ? [ fallbackAnswerText ]
383- : [ ]
384- ) . filter ( ( text ) => ! shouldSuppressRawErrorText ( text ) ) ;
311+ : shouldUseCanonicalFinalAnswer
312+ ? [ fallbackAnswerSourceText ]
313+ : shouldPreferRawAnswerText && fallbackRawAnswerText
314+ ? [ fallbackRawAnswerText ]
315+ : hasAssistantTextPayload
316+ ? nonEmptyAssistantTexts
317+ : fallbackAnswerText
318+ ? [ fallbackAnswerText ]
319+ : [ ] ;
320+ const preparedAnswerDirectives =
321+ shouldUseCanonicalFinalAnswer || shouldPreferRawAnswerText || ! hasAssistantTextPayload
322+ ? fallbackAnswerDirectiveState
323+ : null ;
385324 let hasUserFacingReply =
386325 Boolean ( errorText ) ||
387326 completedSourceReplyViaMessageTool ||
@@ -394,7 +333,7 @@ export function buildEmbeddedRunPayloads(params: {
394333 replyToId,
395334 replyToTag,
396335 replyToCurrent,
397- } = parseReplyDirectives ( text ) ;
336+ } = preparedAnswerDirectives ?? parseReplyDirectives ( text ) ;
398337 const ttsFacts = shouldUseCanonicalFinalAnswer ? storedDelivery ?. tts : undefined ;
399338 const delivery = shouldUseCanonicalFinalAnswer
400339 ? {
0 commit comments