@@ -24,6 +24,7 @@ import { logger } from '../utils/logger'
2424import {
2525 buildBashHistoryMessages ,
2626 createRunTerminalToolResult ,
27+ formatBashContextForPrompt ,
2728} from '../utils/bash-messages'
2829import { getUserMessage } from '../utils/message-history'
2930import { NETWORK_ERROR_ID } from '../utils/validation-error-helpers'
@@ -45,7 +46,6 @@ import type { ParamsOf } from '../types/function-params'
4546import type { SetElement } from '../types/utils'
4647import type { AgentMode } from '../utils/constants'
4748import type { AgentDefinition , RunState , ToolName } from '@codebuff/sdk'
48- import type { ToolMessage } from '@codebuff/common/types/messages/codebuff-message'
4949import type { SetStateAction } from 'react'
5050const hiddenToolNames = new Set < ToolName | 'spawn_agent_inline' > ( [
5151 'spawn_agent_inline' ,
@@ -452,37 +452,47 @@ export const useSendMessage = ({
452452 }
453453
454454 // Include any pending bash messages in the UI before sending
455- // (we no longer send these commands to the LLM context)
455+ // and prepare context for the LLM
456456 const { pendingBashMessages, clearPendingBashMessages } =
457457 useChatStore . getState ( )
458+
459+ // Format bash context to add to message history for the LLM
460+ const bashContext = formatBashContextForPrompt ( pendingBashMessages )
461+
458462 if ( pendingBashMessages . length > 0 ) {
459- // Convert pending bash messages to chat messages and add to history
460- applyMessageUpdate ( ( prev ) => {
461- const bashMessages : ChatMessage [ ] = [ ]
462-
463- for ( const bash of pendingBashMessages ) {
464- const toolCallId = crypto . randomUUID ( )
465- const cwd = bash . cwd || process . cwd ( )
466- const toolResultOutput = createRunTerminalToolResult ( {
467- command : bash . command ,
468- cwd,
469- stdout : bash . stdout || null ,
470- stderr : bash . stderr || null ,
471- exitCode : bash . exitCode ,
472- } )
473- const outputJson = JSON . stringify ( toolResultOutput )
474- const { assistantMessage } = buildBashHistoryMessages ( {
475- command : bash . command ,
476- cwd,
477- toolCallId,
478- output : outputJson ,
479- isComplete : true ,
480- } )
463+ // Convert pending bash messages to chat messages and add to history (UI only)
464+ // Skip messages that were already added to history (non-ghost mode)
465+ const bashMessagesToAdd = pendingBashMessages . filter (
466+ ( bash ) => ! bash . addedToHistory ,
467+ )
468+ if ( bashMessagesToAdd . length > 0 ) {
469+ applyMessageUpdate ( ( prev ) => {
470+ const bashMessages : ChatMessage [ ] = [ ]
471+
472+ for ( const bash of bashMessagesToAdd ) {
473+ const toolCallId = crypto . randomUUID ( )
474+ const cwd = bash . cwd || process . cwd ( )
475+ const toolResultOutput = createRunTerminalToolResult ( {
476+ command : bash . command ,
477+ cwd,
478+ stdout : bash . stdout || null ,
479+ stderr : bash . stderr || null ,
480+ exitCode : bash . exitCode ,
481+ } )
482+ const outputJson = JSON . stringify ( toolResultOutput )
483+ const { assistantMessage } = buildBashHistoryMessages ( {
484+ command : bash . command ,
485+ cwd,
486+ toolCallId,
487+ output : outputJson ,
488+ isComplete : true ,
489+ } )
481490
482- bashMessages . push ( assistantMessage )
483- }
484- return [ ...prev , ...bashMessages ]
485- } )
491+ bashMessages . push ( assistantMessage )
492+ }
493+ return [ ...prev , ...bashMessages ]
494+ } )
495+ }
486496 clearPendingBashMessages ( )
487497 }
488498
@@ -982,21 +992,15 @@ export const useSendMessage = ({
982992
983993 let runState : RunState
984994 try {
985- // Get any pending tool results from user-executed bash commands
986- const pendingToolResults = useChatStore . getState ( ) . pendingToolResults
987- if ( pendingToolResults . length > 0 ) {
988- useChatStore . getState ( ) . clearPendingToolResults ( )
989- }
995+ // If there's bash context, always prepend it to the user's prompt
996+ // This ensures consistent behavior whether or not there's a previous run
997+ const promptToSend = bashContext ? bashContext + content : content
990998
991999 runState = await client . run ( {
9921000 logger,
9931001 agent : selectedAgentDefinition ?? agentId ?? fallbackAgent ,
994- prompt : content ,
1002+ prompt : promptToSend ,
9951003 previousRun : previousRunStateRef . current ?? undefined ,
996- extraToolResults :
997- pendingToolResults . length > 0
998- ? ( pendingToolResults as unknown as ToolMessage [ ] )
999- : undefined ,
10001004 abortController,
10011005 retry : {
10021006 maxRetries : MAX_RETRIES_PER_MESSAGE ,
0 commit comments