@@ -22,6 +22,7 @@ import { neutralizeIdentity } from "./identity";
2222import { CLAUDE_CODE_HEADERS , claudeCodeSessionId } from "./client-fingerprint" ;
2323import { buildNonOpenAIToolCatalogNudgeForTools } from "./tool-catalog-nudge" ;
2424import { decodeServerSentEvents } from "../lib/sse-decoder" ;
25+ import { isTranslatorBudgetExceededError , retainTranslatedEventBatch , type TranslatorBudget } from "../lib/translator-budget" ;
2526
2627/** Map a user content part to an Anthropic content block (text or image source). */
2728function toAnthropicContentPart ( p : OcxContentPart ) : unknown {
@@ -783,12 +784,13 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
783784 return { url, method : "POST" , headers, body : JSON . stringify ( body ) } ;
784785 } ,
785786
786- async * parseStream ( response : Response ) : AsyncGenerator < AdapterEvent > {
787+ async * parseStream ( response : Response , budget : TranslatorBudget ) : AsyncGenerator < AdapterEvent > {
787788 if ( ! response . body ) {
788789 yield { type : "error" , message : "No response body" } ;
789790 return ;
790791 }
791792
793+ const budgetEncoder = new TextEncoder ( ) ;
792794 let currentBlockType = "" ;
793795 let currentToolCallId = "" ;
794796 let currentToolCallName = "" ;
@@ -807,7 +809,8 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
807809 } ;
808810 } ;
809811
810- for await ( const record of decodeServerSentEvents ( response . body , { includeComments : true } ) ) {
812+ try {
813+ for await ( const record of decodeServerSentEvents ( response . body , { includeComments : true , translatorBudget : budget } ) ) {
811814 if ( record . kind === "comment" ) {
812815 yield { type : "heartbeat" } ;
813816 continue ;
@@ -837,6 +840,7 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
837840 currentToolCallId = usableToolUseId ( block . id ) ;
838841 currentToolCallName = toolNames . fromWire ( block . name ?? "" ) ;
839842 currentToolCallJson = "" ;
843+ budget . openCall ( currentToolCallId ) ;
840844 yield { type : "tool_call_start" , id : currentToolCallId , name : currentToolCallName } ;
841845 }
842846 if ( block . type === "redacted_thinking" && typeof block . data === "string" ) {
@@ -866,7 +870,17 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
866870 // response.function_call_arguments.delta frame, so withholding fragments until
867871 // block close would leave a started call showing empty arguments. A copy is kept
868872 // to validate the assembled payload at content_block_stop.
869- currentToolCallJson += delta . partial_json ;
873+ const previousBytes = budgetEncoder . encode ( currentToolCallJson ) . byteLength ;
874+ const nextBytes = previousBytes + budgetEncoder . encode ( delta . partial_json ) . byteLength ;
875+ const reservation = budget . reserveTransient ( nextBytes , { kind : "tool_args" , callId : currentToolCallId } ) ;
876+ try {
877+ currentToolCallJson += delta . partial_json ;
878+ reservation . commitRetained ( ) ;
879+ budget . releaseRetained ( previousBytes , { kind : "tool_args" , callId : currentToolCallId } ) ;
880+ } catch ( error ) {
881+ reservation . release ( ) ;
882+ throw error ;
883+ }
870884 yield { type : "tool_call_delta" , arguments : delta . partial_json } ;
871885 }
872886 break ;
@@ -886,6 +900,7 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
886900 return ;
887901 }
888902 yield { type : "tool_call_end" } ;
903+ budget . closeCall ( currentToolCallId ) ;
889904 currentToolCallId = "" ;
890905 currentToolCallJson = "" ;
891906 }
@@ -910,6 +925,18 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
910925 }
911926 }
912927 }
928+ } catch ( error ) {
929+ if ( ! isTranslatorBudgetExceededError ( error ) ) throw error ;
930+ yield {
931+ type : "error" ,
932+ status : 502 ,
933+ errorType : "upstream_error" ,
934+ code : "translation_buffer_limit" ,
935+ message : "upstream translation buffer exceeded the safe limit" ,
936+ } ;
937+ } finally {
938+ if ( currentToolCallId ) budget . closeCall ( currentToolCallId ) ;
939+ }
913940 if ( ! emittedDone ) {
914941 // Fail closed on transport EOF. Compatible providers may omit message_stop after message_delta.stop_reason.
915942 if ( pendingStopReason !== undefined ) {
@@ -930,8 +957,11 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
930957 }
931958 } ,
932959
933- async parseResponse ( response : Response ) : Promise < AdapterEvent [ ] > {
960+ async parseResponse ( response : Response , budget : TranslatorBudget ) : Promise < AdapterEvent [ ] > {
934961 const json = await response . json ( ) as Record < string , unknown > ;
962+ const responseBytes = new TextEncoder ( ) . encode ( JSON . stringify ( json ) ) . byteLength ;
963+ budget . chargeRetained ( responseBytes , { kind : "retained_collectors" } ) ;
964+ try {
935965 const events : AdapterEvent [ ] = [ ] ;
936966 const content = json . content as { type : string ; text ?: string ; id ?: string ; name ?: string ; input ?: unknown ; thinking ?: string ; reasoning ?: string ; signature ?: string ; data ?: string } [ ] | undefined ;
937967 if ( content ) {
@@ -962,7 +992,11 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
962992 usage : usageFromAnthropic ( usage ) ,
963993 ...( stopReason ? { stopReason } : { } ) ,
964994 } ) ;
995+ retainTranslatedEventBatch ( events , budget ) ;
965996 return events ;
997+ } finally {
998+ budget . releaseRetained ( responseBytes , { kind : "retained_collectors" } ) ;
999+ }
9661000 } ,
9671001
9681002 } ;
0 commit comments