@@ -937,19 +937,23 @@ export const make: (params: {
937937 const tracker = Option . getOrUndefined ( yield * Effect . serviceOption ( ResponseIdTracker . ResponseIdTracker ) )
938938 const toolChoice = options . toolChoice ?? "auto"
939939
940- const withNonIncrementalFallback = < R > (
941- effect : Effect . Effect < Array < Response . PartEncoded > , AiError . AiError , R >
942- ) : Effect . Effect < Array < Response . PartEncoded > , AiError . AiError , R | IdGenerator > =>
943- providerOptions . incrementalPrompt ?
944- effect . pipe (
945- Effect . catchReason ( "AiError" , "InvalidRequestError" , ( _ ) =>
946- params . generateText ( {
947- ...providerOptions ,
948- incrementalPrompt : undefined ,
949- previousResponseId : undefined
950- } ) )
951- ) :
952- effect
940+ const generateWithNonIncrementalFallback = ( ) => {
941+ const requestOptions : ProviderOptions = {
942+ ...providerOptions
943+ }
944+ const fallbackPrompt = requestOptions . prompt
945+ const fallbackOptions : ProviderOptions = {
946+ ...requestOptions ,
947+ prompt : fallbackPrompt ,
948+ incrementalPrompt : undefined ,
949+ previousResponseId : undefined
950+ }
951+ return requestOptions . incrementalPrompt
952+ ? params . generateText ( requestOptions ) . pipe (
953+ Effect . catchReason ( "AiError" , "InvalidRequestError" , ( _ ) => params . generateText ( fallbackOptions ) )
954+ )
955+ : params . generateText ( requestOptions )
956+ }
953957
954958 // Check for pending approvals that need resolution
955959 const { approved, denied } = collectToolApprovals (
@@ -982,7 +986,7 @@ export const make: (params: {
982986 const ResponseSchema = Schema . mutable (
983987 Schema . Array ( Response . Part ( Toolkit . empty ) )
984988 )
985- const rawContent = yield * withNonIncrementalFallback ( params . generateText ( providerOptions ) )
989+ const rawContent = yield * generateWithNonIncrementalFallback ( )
986990 const content = yield * Schema . decodeEffect ( ResponseSchema ) ( rawContent )
987991 if ( tracker ) {
988992 const responseMetadata = content . find ( ( part ) => part . type === "response-metadata" )
@@ -1020,7 +1024,7 @@ export const make: (params: {
10201024 const ResponseSchema = Schema . mutable (
10211025 Schema . Array ( Response . Part ( Toolkit . empty ) )
10221026 )
1023- const rawContent = yield * withNonIncrementalFallback ( params . generateText ( providerOptions ) )
1027+ const rawContent = yield * generateWithNonIncrementalFallback ( )
10241028 const content = yield * Schema . decodeEffect ( ResponseSchema ) ( rawContent )
10251029 if ( tracker ) {
10261030 const responseMetadata = content . find ( ( part ) => part . type === "response-metadata" )
@@ -1099,7 +1103,7 @@ export const make: (params: {
10991103 // If tool call resolution is disabled, return the response without
11001104 // resolving the tool calls that were generated
11011105 if ( options . disableToolCallResolution === true ) {
1102- const rawContent = yield * withNonIncrementalFallback ( params . generateText ( providerOptions ) )
1106+ const rawContent = yield * generateWithNonIncrementalFallback ( )
11031107 const content = yield * Schema . decodeEffect ( ResponseSchema ) ( rawContent )
11041108 if ( tracker ) {
11051109 const responseMetadata = content . find ( ( part ) => part . type === "response-metadata" )
@@ -1110,7 +1114,7 @@ export const make: (params: {
11101114 return content as Array < Response . Part < Tools > >
11111115 }
11121116
1113- const rawContent = yield * withNonIncrementalFallback ( params . generateText ( providerOptions ) )
1117+ const rawContent = yield * generateWithNonIncrementalFallback ( )
11141118
11151119 // Resolve the generated tool calls
11161120 const toolResults = yield * resolveToolCalls (
@@ -1168,19 +1172,23 @@ export const make: (params: {
11681172 const tracker = Option . getOrUndefined ( yield * Effect . serviceOption ( ResponseIdTracker . ResponseIdTracker ) )
11691173 const toolChoice = options . toolChoice ?? "auto"
11701174
1171- const withNonIncrementalFallback = < R > (
1172- stream : Stream . Stream < Response . StreamPartEncoded , AiError . AiError , R >
1173- ) : Stream . Stream < Response . StreamPartEncoded , AiError . AiError , R | IdGenerator > =>
1174- providerOptions . incrementalPrompt ?
1175- stream . pipe (
1176- Stream . catchReason ( "AiError" , "InvalidRequestError" , ( _ ) =>
1177- params . streamText ( {
1178- ...providerOptions ,
1179- incrementalPrompt : undefined ,
1180- previousResponseId : undefined
1181- } ) )
1182- ) :
1183- stream
1175+ const streamWithNonIncrementalFallback = ( ) => {
1176+ const requestOptions : ProviderOptions = {
1177+ ...providerOptions
1178+ }
1179+ const fallbackPrompt = requestOptions . prompt
1180+ const fallbackOptions : ProviderOptions = {
1181+ ...requestOptions ,
1182+ prompt : fallbackPrompt ,
1183+ incrementalPrompt : undefined ,
1184+ previousResponseId : undefined
1185+ }
1186+ return requestOptions . incrementalPrompt
1187+ ? params . streamText ( requestOptions ) . pipe (
1188+ Stream . catchReason ( "AiError" , "InvalidRequestError" , ( _ ) => params . streamText ( fallbackOptions ) )
1189+ )
1190+ : params . streamText ( requestOptions )
1191+ }
11841192
11851193 // Check for pending approvals that need resolution
11861194 const { approved : pendingApproved , denied : pendingDenied } = collectToolApprovals ( providerOptions . prompt . content , {
@@ -1212,8 +1220,7 @@ export const make: (params: {
12121220 const schema = Schema . NonEmptyArray ( Response . StreamPart ( Toolkit . empty ) )
12131221 const decodeParts = Schema . decodeEffect ( schema )
12141222 return pipe (
1215- params . streamText ( providerOptions ) ,
1216- withNonIncrementalFallback ,
1223+ streamWithNonIncrementalFallback ( ) ,
12171224 Stream . mapArrayEffect ( ( parts ) =>
12181225 decodeParts ( parts ) . pipe (
12191226 tracker ?
@@ -1262,8 +1269,7 @@ export const make: (params: {
12621269 const schema = Schema . NonEmptyArray ( Response . StreamPart ( Toolkit . empty ) )
12631270 const decodeParts = Schema . decodeEffect ( schema )
12641271 return pipe (
1265- params . streamText ( providerOptions ) ,
1266- withNonIncrementalFallback ,
1272+ streamWithNonIncrementalFallback ( ) ,
12671273 Stream . mapArrayEffect ( ( parts ) =>
12681274 decodeParts ( parts ) . pipe (
12691275 tracker ?
@@ -1369,8 +1375,7 @@ export const make: (params: {
13691375 if ( options . disableToolCallResolution === true ) {
13701376 const schema = Schema . NonEmptyArray ( Response . StreamPart ( toolkit ) )
13711377 const decodeParts = Schema . decodeEffect ( schema )
1372- return params . streamText ( providerOptions ) . pipe (
1373- withNonIncrementalFallback ,
1378+ return streamWithNonIncrementalFallback ( ) . pipe (
13741379 Stream . mapArrayEffect ( ( parts ) =>
13751380 decodeParts ( parts ) . pipe (
13761381 tracker ?
@@ -1449,8 +1454,7 @@ export const make: (params: {
14491454 )
14501455 } )
14511456
1452- yield * params . streamText ( providerOptions ) . pipe (
1453- withNonIncrementalFallback ,
1457+ yield * streamWithNonIncrementalFallback ( ) . pipe (
14541458 Stream . runForEachArray (
14551459 Effect . fnUntraced ( function * ( chunk ) {
14561460 const parts = yield * decodeParts ( chunk )
0 commit comments