11import { describe , expect , test } from "bun:test"
22import { Message , ToolMessage , ToolResultPart , Media , UserMessage , TextPart } from "@opencode/ai/effect"
3- import { boundImages , unsupportedParts } from "@opencode/core/session/model-request"
3+ import { boundMedia , unsupportedParts } from "@opencode/core/session/model-request"
44
55const capabilities = ( input : string [ ] ) => ( { tools : true , input, output : [ "text" ] } )
66
@@ -215,10 +215,28 @@ describe("SessionModelRequest.unsupportedParts", () => {
215215 } )
216216} )
217217
218- describe ( "SessionModelRequest.boundImages" , ( ) => {
218+ describe ( "SessionModelRequest.unsupportedParts audio and video" , ( ) => {
219+ test ( "refuses audio and video on protocols that cannot carry them, even when the model lists them" , ( ) => {
220+ const messages = [
221+ Message . user ( [
222+ { type : "media" , media : Media . base64 ( "AAAA" , "audio/wav" ) , filename : "clip.wav" } ,
223+ { type : "media" , media : Media . base64 ( "AAAA" , "video/mp4" ) , filename : "clip.mp4" } ,
224+ ] ) ,
225+ ]
226+ const accepts = capabilities ( [ "text" , "audio" , "video" ] )
227+
228+ expect ( unsupportedParts ( messages , accepts , "google" , "generate-content" ) ) . toEqual ( messages )
229+ expect ( unsupportedParts ( messages , accepts , "openrouter" , "chat-completions" ) [ 0 ] ?. content ) . toEqual ( [
230+ TextPart . make ( 'ERROR: Cannot read "clip.wav" (this model does not support audio input). Inform the user.' ) ,
231+ TextPart . make ( 'ERROR: Cannot read "clip.mp4" (this model does not support video input). Inform the user.' ) ,
232+ ] )
233+ } )
234+ } )
235+
236+ describe ( "SessionModelRequest.boundMedia" , ( ) => {
219237 test ( "preserves images below the trigger" , ( ) => {
220238 const messages = [ Message . user ( { type : "media" , media : Media . base64 ( "aGVsbG8=" , "image/png" ) } ) ]
221- expect ( boundImages ( messages ) ) . toBe ( messages )
239+ expect ( boundMedia ( messages ) ) . toBe ( messages )
222240 } )
223241
224242 test ( "replaces oldest images until the retained payload reaches the target" , ( ) => {
@@ -231,7 +249,7 @@ describe("SessionModelRequest.boundImages", () => {
231249 Message . user ( { type : "media" , media : Media . base64 ( image , "image/png" ) , filename : "second.png" } ) ,
232250 third ,
233251 ]
234- const result = boundImages ( messages )
252+ const result = boundMedia ( messages )
235253
236254 expect ( result [ 0 ] ) . toBeInstanceOf ( UserMessage )
237255 expect ( result [ 0 ] ?. content ) . toEqual ( [ { type : "text" , text : expect . stringContaining ( "image was removed" ) } ] )
@@ -244,21 +262,46 @@ describe("SessionModelRequest.boundImages", () => {
244262 test ( "keeps the replaced prefix stable as new images arrive, so the prompt cache survives" , ( ) => {
245263 const image = ( name : string ) =>
246264 Message . user ( { type : "media" , media : Media . base64 ( "a" . repeat ( 3 * 1024 * 1024 ) , "image/png" ) , filename : name } )
247- const removed = ( messages : ReturnType < typeof boundImages > ) =>
265+ const removed = ( messages : ReturnType < typeof boundMedia > ) =>
248266 messages . filter ( ( message ) => message . content [ 0 ] ?. type === "text" ) . length
249267 const history = Array . from ( { length : 9 } , ( _ , i ) => image ( `${ i } .png` ) )
250268
251269 // 27 MiB passes the 25 MiB trigger: the oldest four go, leaving 15 MiB.
252- expect ( removed ( boundImages ( history ) ) ) . toBe ( 4 )
270+ expect ( removed ( boundMedia ( history ) ) ) . toBe ( 4 )
253271 // Each later image used to move the cut by one; now the cut holds until the kept payload passes the trigger again.
254- expect ( removed ( boundImages ( [ ...history , image ( "9.png" ) ] ) ) ) . toBe ( 4 )
255- expect ( removed ( boundImages ( [ ...history , image ( "9.png" ) , image ( "10.png" ) , image ( "11.png" ) ] ) ) ) . toBe ( 4 )
256- expect ( removed ( boundImages ( [ ...history , ...Array . from ( { length : 4 } , ( _ , i ) => image ( `${ 9 + i } .png` ) ) ] ) ) ) . toBe ( 8 )
272+ expect ( removed ( boundMedia ( [ ...history , image ( "9.png" ) ] ) ) ) . toBe ( 4 )
273+ expect ( removed ( boundMedia ( [ ...history , image ( "9.png" ) , image ( "10.png" ) , image ( "11.png" ) ] ) ) ) . toBe ( 4 )
274+ expect ( removed ( boundMedia ( [ ...history , ...Array . from ( { length : 4 } , ( _ , i ) => image ( `${ 9 + i } .png` ) ) ] ) ) ) . toBe ( 8 )
275+ } )
276+
277+ test ( "keeps the newest audio and video and asks the model to read older ones again" , ( ) => {
278+ const clip = ( name : string , mime : string ) =>
279+ Message . tool (
280+ ToolResultPart . make ( {
281+ id : name ,
282+ name : "read" ,
283+ result : { type : "content" , value : [ { type : "file" , uri : `data:${ mime } ;base64,AAAA` , mime, name } ] } ,
284+ } ) ,
285+ )
286+ const image = Message . user ( { type : "media" , media : Media . base64 ( "aGVsbG8=" , "image/png" ) , filename : "logo.png" } )
287+ const history = [ clip ( "a.wav" , "audio/wav" ) , image , clip ( "b.mp4" , "video/mp4" ) , clip ( "c.wav" , "audio/wav" ) ]
288+
289+ expect ( boundMedia ( [ ...history , clip ( "d.mp4" , "video/mp4" ) ] ) ) . toEqual ( [ ...history , clip ( "d.mp4" , "video/mp4" ) ] )
290+ const result = boundMedia ( [ ...history , clip ( "d.mp4" , "video/mp4" ) , clip ( "e.wav" , "audio/wav" ) ] )
291+ expect ( result [ 1 ] ) . toBe ( image )
292+ expect ( result . flatMap ( ( message ) : ReadonlyArray < unknown > => message . content ) ) . toMatchObject ( [
293+ { result : { value : [ { type : "text" , text : expect . stringContaining ( "This audio was removed" ) } ] } } ,
294+ { type : "media" } ,
295+ { result : { value : [ { type : "text" , text : expect . stringContaining ( "read b.mp4 again" ) } ] } } ,
296+ { result : { value : [ { type : "text" , text : expect . stringContaining ( "read c.wav again" ) } ] } } ,
297+ { result : { value : [ { type : "file" , name : "d.mp4" } ] } } ,
298+ { result : { value : [ { type : "file" , name : "e.wav" } ] } } ,
299+ ] )
257300 } )
258301
259302 test ( "replaces images nested in tool results" , ( ) => {
260303 const image = "a" . repeat ( 13 * 1024 * 1024 )
261- const result = boundImages ( [
304+ const result = boundMedia ( [
262305 Message . tool (
263306 ToolResultPart . make ( {
264307 id : "call_1" ,
0 commit comments