MCPcopy
hub / github.com/codeaashu/claude-code / convertSDKMessage

Function convertSDKMessage

src/remote/sdkMessageAdapter.ts:168–278  ·  view source on GitHub ↗
(
  msg: SDKMessage,
  opts?: ConvertOptions,
)

Source from the content-addressed store, hash-verified

166 * Convert an SDKMessage to REPL message format
167 */
168export function convertSDKMessage(
169 msg: SDKMessage,
170 opts?: ConvertOptions,
171): ConvertedMessage {
172 switch (msg.type) {
173 case 'assistant':
174 return { type: 'message', message: convertAssistantMessage(msg) }
175
176 case 'user': {
177 const content = msg.message?.content
178 // Tool result messages from the remote server need to be converted so
179 // they render and collapse like local tool results. Detect via content
180 // shape (tool_result blocks) — parent_tool_use_id is NOT reliable: the
181 // agent-side normalizeMessage() hardcodes it to null for top-level
182 // tool results, so it can't distinguish tool results from prompt echoes.
183 const isToolResult =
184 Array.isArray(content) && content.some(b => b.type === 'tool_result')
185 if (opts?.convertToolResults && isToolResult) {
186 return {
187 type: 'message',
188 message: createUserMessage({
189 content,
190 toolUseResult: msg.tool_use_result,
191 uuid: msg.uuid,
192 timestamp: msg.timestamp,
193 }),
194 }
195 }
196 // When converting historical events, user-typed messages need to be
197 // rendered (they weren't added locally by the REPL). Skip tool_results
198 // here — already handled above.
199 if (opts?.convertUserTextMessages && !isToolResult) {
200 if (typeof content === 'string' || Array.isArray(content)) {
201 return {
202 type: 'message',
203 message: createUserMessage({
204 content,
205 toolUseResult: msg.tool_use_result,
206 uuid: msg.uuid,
207 timestamp: msg.timestamp,
208 }),
209 }
210 }
211 }
212 // User-typed messages (string content) are already added locally by REPL.
213 // In CCR mode, all user messages are ignored (tool results handled differently).
214 return { type: 'ignored' }
215 }
216
217 case 'stream_event':
218 return { type: 'stream_event', event: convertStreamEvent(msg) }
219
220 case 'result':
221 // Only show result messages for errors. Success results are noise
222 // in multi-turn sessions (isLoading=false is sufficient signal).
223 if (msg.subtype !== 'success') {
224 return { type: 'message', message: convertResultMessage(msg) }
225 }

Callers 4

pageToMessagesFunction · 0.85
useRemoteSessionFunction · 0.85
useSSHSessionFunction · 0.85
useDirectConnectFunction · 0.85

Calls 9

convertAssistantMessageFunction · 0.85
createUserMessageFunction · 0.85
convertStreamEventFunction · 0.85
convertResultMessageFunction · 0.85
convertInitMessageFunction · 0.85
convertStatusMessageFunction · 0.85
logForDebuggingFunction · 0.85

Tested by

no test coverage detected