(messages: readonly ContextMessage[], turnNumber: number)
| 129 | } |
| 130 | |
| 131 | function formatTurnMd(messages: readonly ContextMessage[], turnNumber: number): string { |
| 132 | const lines: string[] = [`## Turn ${String(turnNumber)}`, '']; |
| 133 | |
| 134 | const toolCallInfo = new Map<string, { name: string; hint: string }>(); |
| 135 | let assistantHeaderWritten = false; |
| 136 | |
| 137 | for (const msg of messages) { |
| 138 | if (isInternalMessage(msg)) continue; |
| 139 | |
| 140 | if (msg.role === 'user') { |
| 141 | lines.push('### User', ''); |
| 142 | for (const part of msg.content) { |
| 143 | const text = formatContentPartMd(part); |
| 144 | if (text.trim()) { |
| 145 | lines.push(text, ''); |
| 146 | } |
| 147 | } |
| 148 | } else if (msg.role === 'assistant') { |
| 149 | if (!assistantHeaderWritten) { |
| 150 | lines.push('### Assistant', ''); |
| 151 | assistantHeaderWritten = true; |
| 152 | } |
| 153 | |
| 154 | for (const part of msg.content) { |
| 155 | const text = formatContentPartMd(part); |
| 156 | if (text.trim()) { |
| 157 | lines.push(text, ''); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | for (const tc of msg.toolCalls) { |
| 162 | const hint = extractToolCallHint(tc.arguments ?? '{}'); |
| 163 | toolCallInfo.set(tc.id, { name: tc.name, hint }); |
| 164 | lines.push(formatToolCallMd(tc), ''); |
| 165 | } |
| 166 | } else if (msg.role === 'tool') { |
| 167 | const tcId = msg.toolCallId ?? ''; |
| 168 | const info = toolCallInfo.get(tcId) ?? { name: 'unknown', hint: '' }; |
| 169 | lines.push(formatToolResultMd(msg, info.name, info.hint), ''); |
| 170 | } else if (msg.role === 'system') { |
| 171 | lines.push(`### ${msg.role.charAt(0).toUpperCase()}${msg.role.slice(1)}`, ''); |
| 172 | for (const part of msg.content) { |
| 173 | const text = formatContentPartMd(part); |
| 174 | if (text.trim()) { |
| 175 | lines.push(text, ''); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return lines.join('\n'); |
| 182 | } |
| 183 | |
| 184 | function buildOverview( |
| 185 | history: readonly ContextMessage[], |
no test coverage detected