@@ -118,6 +118,21 @@ type SessionUsageSnapshot = {
118118 used : number ;
119119} ;
120120
121+ function isAdminScopeProvenanceRejection ( err : unknown ) : boolean {
122+ if ( ! ( err instanceof Error ) ) {
123+ return false ;
124+ }
125+ const gatewayCode =
126+ typeof ( err as { gatewayCode ?: unknown } ) . gatewayCode === "string"
127+ ? ( err as { gatewayCode ?: string } ) . gatewayCode
128+ : undefined ;
129+ return (
130+ err . name === "GatewayClientRequestError" &&
131+ gatewayCode === "INVALID_REQUEST" &&
132+ err . message . includes ( "system provenance fields require admin scope" )
133+ ) ;
134+ }
135+
121136type SessionSnapshot = SessionPresentation & {
122137 metadata ?: SessionMetadata ;
123138 usage ?: SessionUsageSnapshot ;
@@ -642,6 +657,15 @@ export class AcpGatewayAgent implements Agent {
642657 const abortController = new AbortController ( ) ;
643658 const runId = randomUUID ( ) ;
644659 this . sessionStore . setActiveRun ( params . sessionId , runId , abortController ) ;
660+ const requestParams = {
661+ sessionKey : session . sessionKey ,
662+ message,
663+ attachments : attachments . length > 0 ? attachments : undefined ,
664+ idempotencyKey : runId ,
665+ thinking : readString ( params . _meta , [ "thinking" , "thinkingLevel" ] ) ,
666+ deliver : readBool ( params . _meta , [ "deliver" ] ) ,
667+ timeoutMs : readNumber ( params . _meta , [ "timeoutMs" ] ) ,
668+ } ;
645669
646670 return new Promise < PromptResponse > ( ( resolve , reject ) => {
647671 this . pendingPrompts . set ( params . sessionId , {
@@ -652,27 +676,34 @@ export class AcpGatewayAgent implements Agent {
652676 reject,
653677 } ) ;
654678
655- this . gateway
656- . request (
657- "chat.send" ,
658- {
659- sessionKey : session . sessionKey ,
660- message,
661- attachments : attachments . length > 0 ? attachments : undefined ,
662- idempotencyKey : runId ,
663- thinking : readString ( params . _meta , [ "thinking" , "thinkingLevel" ] ) ,
664- deliver : readBool ( params . _meta , [ "deliver" ] ) ,
665- timeoutMs : readNumber ( params . _meta , [ "timeoutMs" ] ) ,
666- systemInputProvenance,
667- systemProvenanceReceipt,
668- } ,
669- { expectFinal : true } ,
670- )
671- . catch ( ( err ) => {
672- this . pendingPrompts . delete ( params . sessionId ) ;
673- this . sessionStore . clearActiveRun ( params . sessionId ) ;
674- reject ( err instanceof Error ? err : new Error ( String ( err ) ) ) ;
675- } ) ;
679+ const sendWithProvenanceFallback = async ( ) => {
680+ try {
681+ await this . gateway . request (
682+ "chat.send" ,
683+ {
684+ ...requestParams ,
685+ systemInputProvenance,
686+ systemProvenanceReceipt,
687+ } ,
688+ { expectFinal : true } ,
689+ ) ;
690+ } catch ( err ) {
691+ if (
692+ ( systemInputProvenance || systemProvenanceReceipt ) &&
693+ isAdminScopeProvenanceRejection ( err )
694+ ) {
695+ await this . gateway . request ( "chat.send" , requestParams , { expectFinal : true } ) ;
696+ return ;
697+ }
698+ throw err ;
699+ }
700+ } ;
701+
702+ void sendWithProvenanceFallback ( ) . catch ( ( err ) => {
703+ this . pendingPrompts . delete ( params . sessionId ) ;
704+ this . sessionStore . clearActiveRun ( params . sessionId ) ;
705+ reject ( err instanceof Error ? err : new Error ( String ( err ) ) ) ;
706+ } ) ;
676707 } ) ;
677708 }
678709
0 commit comments