@@ -421,17 +421,60 @@ document.addEventListener("DOMContentLoaded", () => {
421421 } ) ;
422422
423423 async function handleToolJson ( raw , { imageUrls, audioUrls } ) {
424- try {
425- const obj = JSON . parse ( raw ) ;
424+ const obj = ( window . repairJson || ( ( ) => ( { text : raw } ) ) ) ( raw ) ;
425+ let handled = false ;
426+
427+ if ( obj . tool ) {
426428 const fn = toolbox . get ( obj . tool ) ;
427- if ( ! fn ) return { handled : false , text : raw } ;
428- const res = await fn ( obj ) ;
429- if ( res ?. imageUrl ) imageUrls . push ( res . imageUrl ) ;
430- if ( res ?. audioUrl ) audioUrls . push ( res . audioUrl ) ;
431- return { handled : true , text : res ?. text || '' } ;
432- } catch {
433- return { handled : false , text : raw } ;
429+ if ( fn ) {
430+ try {
431+ const res = await fn ( obj ) ;
432+ if ( res ?. imageUrl ) imageUrls . push ( res . imageUrl ) ;
433+ if ( res ?. audioUrl ) audioUrls . push ( res . audioUrl ) ;
434+ handled = true ;
435+ return { handled : true , text : res ?. text || '' } ;
436+ } catch ( e ) {
437+ console . warn ( 'tool execution failed' , e ) ;
438+ }
439+ }
440+ }
441+
442+ const imgPrompts = obj . image ? [ obj . image ] : Array . isArray ( obj . images ) ? obj . images : [ ] ;
443+ for ( const prompt of imgPrompts ) {
444+ if ( ! ( window . polliLib && window . polliClient ) || ! prompt ) continue ;
445+ try {
446+ const url = window . polliLib . mcp . generateImageUrl ( window . polliClient , { prompt } ) ;
447+ imageUrls . push ( url ) ;
448+ handled = true ;
449+ } catch ( e ) {
450+ console . warn ( 'polliLib generateImageUrl failed' , e ) ;
451+ }
434452 }
453+
454+ const audioText = obj . audio || obj . tts ;
455+ if ( audioText && window . polliLib && window . polliClient ) {
456+ try {
457+ const blob = await window . polliLib . tts ( audioText , { model : 'openai-audio' } , window . polliClient ) ;
458+ const url = URL . createObjectURL ( blob ) ;
459+ audioUrls . push ( url ) ;
460+ handled = true ;
461+ } catch ( e ) {
462+ console . warn ( 'polliLib tts failed' , e ) ;
463+ }
464+ }
465+
466+ const command = obj . ui || obj . command ;
467+ if ( command ) {
468+ if ( validateUICommand ( command ) ) {
469+ try { executeCommand ( command ) ; } catch ( e ) { console . warn ( 'executeCommand failed' , e ) ; }
470+ handled = true ;
471+ } else {
472+ console . warn ( 'invalid ui command' , command ) ;
473+ }
474+ }
475+
476+ const text = typeof obj . text === 'string' ? obj . text : raw ;
477+ return { handled, text } ;
435478 }
436479
437480 function handleVoiceCommand ( text ) {
0 commit comments