@@ -209,42 +209,38 @@ export function injectModeTransition(
209209 * - Preserves the rest of the message structure (id, role, other metadata)
210210 */
211211export function transformScriptMessagesForLLM ( messages : MuxMessage [ ] ) : MuxMessage [ ] {
212- return messages . flatMap ( ( msg ) => {
212+ return messages . map ( ( msg ) => {
213213 if ( msg . metadata ?. muxMetadata ?. type !== "script-execution" ) {
214- return [ msg ] ;
214+ return msg ;
215215 }
216216
217217 const scriptMeta = msg . metadata . muxMetadata ;
218218 const result = scriptMeta . result ;
219- // Only include the message if the script explicitly wrote to MUX_PROMPT (promptFile)
220- // or MUX_OUTPUT (outputFile).
221- // This respects the "Visible only to you" promise for stdout/stderr (result.output).
222- const promptContent = result . promptFile ?. trim ( ) ;
223- const outputContent = result . outputFile ?. trim ( ) ;
224- const hasContent = ( promptContent && promptContent . length > 0 ) || ( outputContent && outputContent . length > 0 ) ;
219+
220+ let llmContent = `Script '${ scriptMeta . scriptName } ' executed (exit code ${ result . exitCode } ).` ;
225221
226- if ( hasContent ) {
227- let llmContent = `Script '${ scriptMeta . scriptName } ' executed (exit code ${ result . exitCode } ).` ;
228-
229- if ( outputContent && outputContent . length > 0 ) {
230- llmContent += `\nMUX_OUTPUT:\n${ outputContent } ` ;
231- }
232-
233- if ( promptContent && promptContent . length > 0 ) {
234- llmContent += `\nMUX_PROMPT:\n${ promptContent } ` ;
235- }
222+ // Include Stdout/Stderr if present
223+ if ( result . output ) {
224+ llmContent += `\nStdout/Stderr:\n${ result . output } ` ;
225+ } else {
226+ llmContent += `\nStdout/Stderr: (no output)` ;
227+ }
228+
229+ // Include MUX_OUTPUT if present
230+ if ( result . outputFile && result . outputFile . trim ( ) ) {
231+ llmContent += `\nMUX_OUTPUT:\n${ result . outputFile . trim ( ) } ` ;
232+ }
236233
237- return [
238- {
239- ...msg ,
240- parts : [ { type : "text" , text : llmContent } ] ,
241- // Keep metadata for debugging but ensure downstream consumers use the new parts
242- } ,
243- ] ;
234+ // Include MUX_PROMPT if present
235+ if ( result . promptFile && result . promptFile . trim ( ) ) {
236+ llmContent += `\nMUX_PROMPT:\n${ result . promptFile . trim ( ) } ` ;
244237 }
245238
246- // Otherwise, exclude from LLM context entirely
247- return [ ] ;
239+ return {
240+ ...msg ,
241+ parts : [ { type : "text" , text : llmContent } ] ,
242+ // Keep metadata for debugging but ensure downstream consumers use the new parts
243+ } ;
248244 } ) ;
249245}
250246
0 commit comments