MCPcopy Index your code
hub / github.com/codeaashu/claude-code / extractSideQuestionResponse

Function extractSideQuestionResponse

src/utils/sideQuestion.ts:125–155  ·  view source on GitHub ↗

* Extract a display string from forked agent messages. * * IMPORTANT: claude.ts yields one AssistantMessage PER CONTENT BLOCK, not one * per API response. With adaptive thinking enabled (inherited from the main * thread to preserve the cache key), a thinking response arrives as: * messages[0]

(messages: Message[])

Source from the content-addressed store, hash-verified

123 * interruption, no assistant message at all.
124 */
125function extractSideQuestionResponse(messages: Message[]): string | null {
126 // Flatten all assistant content blocks across the per-block messages.
127 const assistantBlocks = messages.flatMap(m =>
128 m.type === 'assistant' ? m.message.content : [],
129 )
130
131 if (assistantBlocks.length > 0) {
132 // Concatenate all text blocks (there's normally at most one, but be safe).
133 const text = extractTextContent(assistantBlocks, '\n\n').trim()
134 if (text) return text
135
136 // No text — check if the model tried to call a tool despite instructions.
137 const toolUse = assistantBlocks.find(b => b.type === 'tool_use')
138 if (toolUse) {
139 const toolName = 'name' in toolUse ? toolUse.name : 'a tool'
140 return `(The model tried to call ${toolName} instead of answering directly. Try rephrasing or ask in the main conversation.)`
141 }
142 }
143
144 // No assistant content — likely API error exhausted retries. Surface the
145 // first system api_error message so the user sees what happened.
146 const apiErr = messages.find(
147 (m): m is SystemAPIErrorMessage =>
148 m.type === 'system' && 'subtype' in m && m.subtype === 'api_error',
149 )
150 if (apiErr) {
151 return `(API error: ${formatAPIError(apiErr.error)})`
152 }
153
154 return null
155}
156

Callers 1

runSideQuestionFunction · 0.85

Calls 2

formatAPIErrorFunction · 0.85
extractTextContentFunction · 0.70

Tested by

no test coverage detected