1- import { Effect , Schema } from "effect"
1+ import { Effect , Option , Schema } from "effect"
22import { Tool } from "@opencode/schema/tool"
33import { Route } from "../route/client.js"
44import { Auth } from "../route/auth.js"
@@ -76,6 +76,44 @@ const OpenAIChatAssistantToolCall = Schema.Struct({
7676} )
7777type OpenAIChatAssistantToolCall = Schema . Schema . Type < typeof OpenAIChatAssistantToolCall >
7878
79+ // `reasoning_details` carries two dialects. OpenRouter's `reasoning.*` entries
80+ // must be replayed unmodified (`index` included), so they keep every field they
81+ // arrived with. Kimi's OpenAI-compatible surface streams preserved thinking as
82+ // bare `summary` / `encrypted` entries keyed by a stream-only `index`; Kimi does
83+ // not document this publicly, so the handling follows Kimi Code (Kimi's own
84+ // client): merge summary deltas by `index`, replay without `index`, and always
85+ // send `reasoning_content` alongside. Anything else is dropped at the boundary.
86+ const OpenRouterDetailFields = {
87+ id : Schema . optional ( Schema . NullOr ( Schema . String ) ) ,
88+ format : Schema . optional ( Schema . String ) ,
89+ index : Schema . optional ( Schema . Number ) ,
90+ signature : Schema . optional ( Schema . NullOr ( Schema . String ) ) ,
91+ }
92+ const ReasoningDetail = Schema . Union ( [
93+ Schema . StructWithRest (
94+ Schema . Struct ( { type : Schema . Literal ( "reasoning.text" ) , text : Schema . optional ( Schema . String ) , ...OpenRouterDetailFields } ) ,
95+ [ Schema . Record ( Schema . String , Schema . Unknown ) ] ,
96+ ) ,
97+ Schema . StructWithRest (
98+ Schema . Struct ( {
99+ type : Schema . Literal ( "reasoning.summary" ) ,
100+ summary : Schema . optional ( Schema . String ) ,
101+ ...OpenRouterDetailFields ,
102+ } ) ,
103+ [ Schema . Record ( Schema . String , Schema . Unknown ) ] ,
104+ ) ,
105+ Schema . StructWithRest (
106+ Schema . Struct ( { type : Schema . Literal ( "reasoning.encrypted" ) , data : Schema . String , ...OpenRouterDetailFields } ) ,
107+ [ Schema . Record ( Schema . String , Schema . Unknown ) ] ,
108+ ) ,
109+ Schema . Struct ( { type : Schema . Literal ( "summary" ) , summary : Schema . String , index : Schema . optional ( Schema . Number ) } ) ,
110+ Schema . Struct ( { type : Schema . Literal ( "encrypted" ) , encrypted : Schema . String , index : Schema . optional ( Schema . Number ) } ) ,
111+ ] )
112+ type ReasoningDetail = Schema . Schema . Type < typeof ReasoningDetail >
113+ const decodeReasoningDetail = Schema . decodeUnknownOption ( ReasoningDetail )
114+ const knownReasoningDetails = ( details : ReadonlyArray < unknown > ) =>
115+ details . flatMap ( ( detail ) => Option . toArray ( decodeReasoningDetail ( detail ) ) )
116+
79117// Intentionally omit Gemini's provider-specific `extra_content.google.thought_signature`
80118// extension until direct Google OpenAI-compatible routing is supported here:
81119// https://github.com/vercel/ai/issues/11590
@@ -265,7 +303,9 @@ export interface ParserState {
265303 readonly finishReason ?: FinishReasonDetails
266304 readonly lifecycle : Lifecycle . State
267305 readonly reasoningField ?: string
268- readonly reasoningDetails : Array < unknown >
306+ /** A scalar reasoning field (`reasoning_content`, ...) has carried text in this stream. */
307+ readonly reasoningTextObserved : boolean
308+ readonly reasoningDetails : Array < ReasoningDetail >
269309 readonly reasoningDetailsObserved : boolean
270310 readonly reasoningEmitted : boolean
271311 readonly latestToolIndex ?: number
@@ -341,10 +381,21 @@ const reasoningDetails = (parts: ReadonlyArray<ReasoningPart>, native: unknown,
341381 return Array . isArray ( details ) ? details : [ ]
342382 } )
343383 if ( parts . some ( ( part ) => Array . isArray ( part . providerMetadata ?. [ providerMetadataKey ] ?. reasoningDetails ) ) )
344- return observed
345- if ( isRecord ( native ) && Array . isArray ( native . reasoning_details ) ) return native . reasoning_details
384+ return knownReasoningDetails ( observed ) . map ( lowerReasoningDetail )
385+ if ( isRecord ( native ) && Array . isArray ( native . reasoning_details ) )
386+ return knownReasoningDetails ( native . reasoning_details ) . map ( lowerReasoningDetail )
387+ }
388+
389+ // Kimi rejects its stream-only `index` on requests
390+ // ("the reasoning_details ... must not contain streaming index").
391+ const lowerReasoningDetail = ( detail : ReasoningDetail ) => {
392+ if ( detail . type === "summary" ) return { type : detail . type , summary : detail . summary }
393+ if ( detail . type === "encrypted" ) return { type : detail . type , encrypted : detail . encrypted }
394+ return detail
346395}
347396
397+ const isKimiDetail = ( detail : { readonly type : string } ) => detail . type === "summary" || detail . type === "encrypted"
398+
348399const lowerUserMessage = Effect . fn ( "OpenAIChat.lowerUserMessage" ) ( function * (
349400 message : OpenAIChatRequestMessage ,
350401 options : LoweringOptions ,
@@ -410,6 +461,9 @@ const lowerAssistantMessage = Effect.fn("OpenAIChat.lowerAssistantMessage")(func
410461 if ( observedField !== undefined ) return observedField
411462 if ( nativeReasoning !== undefined ) return "reasoning_content"
412463 if ( ! fullyStructured || requireReasoning ) return "reasoning_content"
464+ // Kimi always expects `reasoning_content` on replayed assistant messages,
465+ // even when thinking arrived only through structured details.
466+ if ( details ?. some ( isKimiDetail ) ) return "reasoning_content"
413467 } ) ( )
414468 const reasoningText = ( ( ) => {
415469 if ( configuredField !== undefined )
@@ -881,44 +935,74 @@ const reasoningDelta = (
881935 return undefined
882936}
883937
884- const detailText = ( details : ReadonlyArray < unknown > ) => {
938+ const detailText = ( details : ReadonlyArray < ReasoningDetail > , hideKimiSummary : boolean ) => {
885939 const text = details . flatMap ( ( detail ) => {
886- if ( ! isRecord ( detail ) ) return [ ]
887- if ( detail . type === "reasoning.text" && typeof detail . text === "string" && detail . text ) return [ detail . text ]
888- if ( detail . type === "reasoning.summary" && typeof detail . summary === "string" && detail . summary )
889- return [ detail . summary ]
940+ if ( detail . type === "reasoning.text" ) return detail . text ? [ detail . text ] : [ ]
941+ if ( detail . type === "reasoning.summary" ) return detail . summary ? [ detail . summary ] : [ ]
942+ // Kimi streams the full thinking through `reasoning_content` and a separate
943+ // summary through details; show the summary only when nothing else does.
944+ if ( detail . type === "summary" ) return detail . summary && ! hideKimiSummary ? [ detail . summary ] : [ ]
890945 return [ ]
891946 } )
892947 if ( text . length > 0 ) return text . join ( "" )
893948}
894949
895- const appendReasoningDetails = ( result : Array < unknown > , details : ReadonlyArray < unknown > ) => {
950+ const appendReasoningDetails = ( result : Array < ReasoningDetail > , details : ReadonlyArray < ReasoningDetail > ) => {
896951 for ( const detail of details ) {
897952 const previous = result . at ( - 1 )
898- if (
899- ! isRecord ( previous ) ||
900- previous . type !== "reasoning.text" ||
901- ! isRecord ( detail ) ||
902- detail . type !== "reasoning.text" ||
903- conflictingReasoningTextDetails ( previous , detail )
904- ) {
953+ const merged = previous === undefined ? undefined : mergeReasoningDetails ( previous , detail )
954+ if ( merged === undefined ) {
905955 result . push ( detail )
906956 continue
907957 }
908- result [ result . length - 1 ] = {
958+ result [ result . length - 1 ] = merged
959+ }
960+ }
961+
962+ // Consecutive text or summary deltas of the same kind accumulate into one
963+ // entry; encrypted entries are opaque and never merge.
964+ const mergeReasoningDetails = ( previous : ReasoningDetail , detail : ReasoningDetail ) : ReasoningDetail | undefined => {
965+ if ( conflictingReasoningDetails ( previous , detail ) ) return undefined
966+ if ( previous . type === "reasoning.text" && detail . type === "reasoning.text" )
967+ return {
909968 ...previous ,
910- ...Object . fromEntries ( Object . entries ( detail ) . filter ( ( entry ) => entry [ 1 ] !== undefined ) ) ,
911- text : `${ typeof previous . text === "string" ? previous . text : "" } ${ typeof detail . text === "string" ? detail . text : "" } ` ,
912- signature : mergeDetailValue ( previous . signature , detail . signature ) ,
913- format : mergeDetailValue ( previous . format , detail . format ) ,
969+ ...detail ,
970+ text : `${ previous . text ?? "" } ${ detail . text ?? "" } ` ,
971+ ...mergeDetailIdentity ( previous , detail ) ,
914972 }
973+ if ( previous . type === "reasoning.summary" && detail . type === "reasoning.summary" )
974+ return {
975+ ...previous ,
976+ ...detail ,
977+ summary : `${ previous . summary ?? "" } ${ detail . summary ?? "" } ` ,
978+ ...mergeDetailIdentity ( previous , detail ) ,
979+ }
980+ if ( previous . type === "summary" && detail . type === "summary" )
981+ return { ...previous , ...detail , summary : previous . summary + detail . summary }
982+ }
983+
984+ type DetailIdentity = {
985+ readonly id ?: string | null
986+ readonly index ?: number
987+ readonly format ?: string
988+ readonly signature ?: string | null
989+ }
990+
991+ // The first non-empty signature and format win; a later delta may carry the
992+ // signature for text that streamed earlier.
993+ const mergeDetailIdentity = ( previous : DetailIdentity , current : DetailIdentity ) => {
994+ const signature = mergeDetailValue ( previous . signature , current . signature )
995+ const format = mergeDetailValue ( previous . format , current . format )
996+ return {
997+ ...( signature === undefined ? { } : { signature } ) ,
998+ ...( format === undefined ? { } : { format } ) ,
915999 }
9161000}
9171001
918- const mergeDetailValue = ( previous : unknown , current : unknown ) =>
1002+ const mergeDetailValue = < T > ( previous : T | undefined , current : T | undefined ) =>
9191003 previous || current || ( previous !== undefined ? previous : current )
9201004
921- const conflictingReasoningTextDetails = ( previous : Record < string , unknown > , current : Record < string , unknown > ) =>
1005+ const conflictingReasoningDetails = ( previous : DetailIdentity , current : DetailIdentity ) =>
9221006 conflictingDetailValue ( previous . id , current . id ) ||
9231007 conflictingDetailValue ( previous . index , current . index ) ||
9241008 conflictingDetailValue ( previous . format , current . format ) ||
@@ -930,7 +1014,7 @@ const conflictingDetailValue = (previous: unknown, current: unknown) =>
9301014const reasoningMetadata = (
9311015 providerMetadataKey : string ,
9321016 field : ParserState [ "reasoningField" ] ,
933- details ?: ReadonlyArray < unknown > ,
1017+ details ?: ReadonlyArray < ReasoningDetail > ,
9341018) => ( {
9351019 [ providerMetadataKey ] : {
9361020 ...( field ? { reasoningField : field } : { } ) ,
@@ -993,11 +1077,16 @@ const step = (state: ParserState, event: OpenAIChatEvent) =>
9931077 }
9941078
9951079 const reasoningField = state . reasoningField ?? reasoning ?. field
996- const detailDelta = Array . isArray ( delta ?. reasoning_details ) ? delta . reasoning_details : undefined
1080+ const reasoningTextObserved = state . reasoningTextObserved || reasoning !== undefined
1081+ const detailDelta = Array . isArray ( delta ?. reasoning_details )
1082+ ? knownReasoningDetails ( delta . reasoning_details )
1083+ : undefined
9971084 if ( detailDelta !== undefined ) appendReasoningDetails ( state . reasoningDetails , detailDelta )
9981085 const reasoningDetailsObserved = state . reasoningDetailsObserved || detailDelta !== undefined
9991086 const deltaMetadata = reasoningMetadata ( state . providerMetadataKey , reasoningField )
1000- const text = detailDelta ?. length ? ( detailText ( detailDelta ) ?? reasoning ?. text ) : reasoning ?. text
1087+ const text = detailDelta ?. length
1088+ ? ( detailText ( detailDelta , reasoningTextObserved ) ?? reasoning ?. text )
1089+ : reasoning ?. text
10011090 if ( text !== undefined ) lifecycle = Lifecycle . reasoningDelta ( lifecycle , events , "reasoning-0" , text , deltaMetadata )
10021091 else if (
10031092 reasoningDetailsObserved &&
@@ -1093,6 +1182,7 @@ const step = (state: ParserState, event: OpenAIChatEvent) =>
10931182 finishReason,
10941183 lifecycle,
10951184 reasoningField,
1185+ reasoningTextObserved,
10961186 reasoningDetails : state . reasoningDetails ,
10971187 reasoningDetailsObserved,
10981188 reasoningEmitted,
@@ -1173,6 +1263,7 @@ export const protocol = Protocol.make({
11731263 toolCallEvents : [ ] ,
11741264 lifecycle : Lifecycle . initial ( ) ,
11751265 reasoningField : request . model . compatibility ?. reasoningField ,
1266+ reasoningTextObserved : false ,
11761267 reasoningDetails : [ ] ,
11771268 reasoningDetailsObserved : false ,
11781269 reasoningEmitted : false ,
0 commit comments