(messages: Message[])
| 113 | } |
| 114 | |
| 115 | export function toSDKMessages(messages: Message[]): SDKMessage[] { |
| 116 | return messages.flatMap((message): SDKMessage[] => { |
| 117 | switch (message.type) { |
| 118 | case 'assistant': |
| 119 | return [ |
| 120 | { |
| 121 | type: 'assistant', |
| 122 | message: normalizeAssistantMessageForSDK(message), |
| 123 | session_id: getSessionId(), |
| 124 | parent_tool_use_id: null, |
| 125 | uuid: message.uuid, |
| 126 | error: message.error, |
| 127 | }, |
| 128 | ] |
| 129 | case 'user': |
| 130 | return [ |
| 131 | { |
| 132 | type: 'user', |
| 133 | message: message.message, |
| 134 | session_id: getSessionId(), |
| 135 | parent_tool_use_id: null, |
| 136 | uuid: message.uuid, |
| 137 | timestamp: message.timestamp, |
| 138 | isSynthetic: message.isMeta || message.isVisibleInTranscriptOnly, |
| 139 | // Structured tool output (not the string content sent to the |
| 140 | // model — the full Output object). Rides the protobuf catchall |
| 141 | // so web viewers can read things like BriefTool's file_uuid |
| 142 | // without it polluting model context. |
| 143 | ...(message.toolUseResult !== undefined |
| 144 | ? { tool_use_result: message.toolUseResult } |
| 145 | : {}), |
| 146 | }, |
| 147 | ] |
| 148 | case 'system': |
| 149 | if (message.subtype === 'compact_boundary' && message.compactMetadata) { |
| 150 | return [ |
| 151 | { |
| 152 | type: 'system', |
| 153 | subtype: 'compact_boundary' as const, |
| 154 | session_id: getSessionId(), |
| 155 | uuid: message.uuid, |
| 156 | compact_metadata: toSDKCompactMetadata(message.compactMetadata), |
| 157 | }, |
| 158 | ] |
| 159 | } |
| 160 | // Only convert local_command messages that contain actual command |
| 161 | // output (stdout/stderr). The same subtype is also used for command |
| 162 | // input metadata (e.g. <command-name>...</command-name>) which must |
| 163 | // not leak to the RC web UI. |
| 164 | if ( |
| 165 | message.subtype === 'local_command' && |
| 166 | (message.content.includes(`<${LOCAL_COMMAND_STDOUT_TAG}>`) || |
| 167 | message.content.includes(`<${LOCAL_COMMAND_STDERR_TAG}>`)) |
| 168 | ) { |
| 169 | return [ |
| 170 | localCommandOutputToSDKAssistantMessage( |
| 171 | message.content, |
| 172 | message.uuid, |
no test coverage detected