SafeSplitPoint walks backward from `desired` to find an index where the conversation is in a "clean" state — no tool_use without its tool_result on either side of the split. The split happens just before a user message that is plain text (not tool_results). Returns 0 if no safe boundary is found, me
(messages []api.Message, desired int)
| 21 | // that is plain text (not tool_results). Returns 0 if no safe boundary is |
| 22 | // found, meaning "do nothing." |
| 23 | func SafeSplitPoint(messages []api.Message, desired int) int { |
| 24 | if desired <= 0 { |
| 25 | return 0 |
| 26 | } |
| 27 | if desired >= len(messages) { |
| 28 | return len(messages) |
| 29 | } |
| 30 | for i := desired; i > 0; i-- { |
| 31 | if messages[i].Role == api.RoleUser && !messages[i].HasToolResult() { |
| 32 | return i |
| 33 | } |
| 34 | } |
| 35 | return 0 |
| 36 | } |