11import { describe , expect , test } from "bun:test"
22import { MessageV2 } from "../../src/session/message-v2"
3+ import type { ToolSet } from "ai"
34
45const sessionID = "session"
56
7+ // Mock tool that transforms output to content format with media support
8+ function createMockTools ( ) : ToolSet {
9+ return {
10+ bash : {
11+ description : "mock bash tool" ,
12+ inputSchema : { type : "object" , properties : { } } as any ,
13+ toModelOutput ( result : { output : string ; attachments ?: MessageV2 . FilePart [ ] } ) {
14+ return {
15+ type : "content" as const ,
16+ value : [
17+ { type : "text" as const , text : result . output } ,
18+ ...( result . attachments ?. map ( ( attachment ) => {
19+ const base64 = attachment . url . startsWith ( "data:" ) ? attachment . url . split ( "," , 2 ) [ 1 ] : attachment . url
20+ return {
21+ type : "media" as const ,
22+ data : base64 ,
23+ mediaType : attachment . mime ,
24+ }
25+ } ) ?? [ ] ) ,
26+ ] ,
27+ }
28+ } ,
29+ } ,
30+ } as ToolSet
31+ }
32+
633function userInfo ( id : string ) : MessageV2 . User {
734 return {
835 id,
@@ -259,23 +286,11 @@ describe("session.message-v2.toModelMessage", () => {
259286 } ,
260287 ]
261288
262- expect ( MessageV2 . toModelMessage ( input ) ) . toStrictEqual ( [
289+ expect ( MessageV2 . toModelMessage ( input , { tools : createMockTools ( ) } ) ) . toStrictEqual ( [
263290 {
264291 role : "user" ,
265292 content : [ { type : "text" , text : "run tool" } ] ,
266293 } ,
267- {
268- role : "user" ,
269- content : [
270- { type : "text" , text : "Tool bash returned an attachment:" } ,
271- {
272- type : "file" ,
273- mediaType : "image/png" ,
274- filename : "attachment.png" ,
275- data : "https://example.com/attachment.png" ,
276- } ,
277- ] ,
278- } ,
279294 {
280295 role : "assistant" ,
281296 content : [
@@ -297,7 +312,13 @@ describe("session.message-v2.toModelMessage", () => {
297312 type : "tool-result" ,
298313 toolCallId : "call-1" ,
299314 toolName : "bash" ,
300- output : { type : "text" , value : "ok" } ,
315+ output : {
316+ type : "content" ,
317+ value : [
318+ { type : "text" , text : "ok" } ,
319+ { type : "media" , data : "https://example.com/attachment.png" , mediaType : "image/png" } ,
320+ ] ,
321+ } ,
301322 providerOptions : { openai : { tool : "meta" } } ,
302323 } ,
303324 ] ,
@@ -341,7 +362,7 @@ describe("session.message-v2.toModelMessage", () => {
341362 } ,
342363 ]
343364
344- expect ( MessageV2 . toModelMessage ( input ) ) . toStrictEqual ( [
365+ expect ( MessageV2 . toModelMessage ( input , { tools : createMockTools ( ) } ) ) . toStrictEqual ( [
345366 {
346367 role : "user" ,
347368 content : [ { type : "text" , text : "run tool" } ] ,
@@ -365,7 +386,10 @@ describe("session.message-v2.toModelMessage", () => {
365386 type : "tool-result" ,
366387 toolCallId : "call-1" ,
367388 toolName : "bash" ,
368- output : { type : "text" , value : "[Old tool result content cleared]" } ,
389+ output : {
390+ type : "content" ,
391+ value : [ { type : "text" , text : "[Old tool result content cleared]" } ] ,
392+ } ,
369393 } ,
370394 ] ,
371395 } ,
0 commit comments