|
1 | 1 | package sdk |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
4 | 6 | "maps" |
| 7 | + "reflect" |
5 | 8 | "strings" |
6 | 9 |
|
7 | 10 | "github.com/beeper/agentremote/pkg/matrixevents" |
| 11 | + "github.com/beeper/agentremote/turns" |
| 12 | + "maunium.net/go/mautrix/event" |
| 13 | + "maunium.net/go/mautrix/id" |
8 | 14 | ) |
9 | 15 |
|
| 16 | +const MaxMatrixEventContentBytes = 60000 |
| 17 | + |
10 | 18 | // BuildCompactFinalUIMessage removes streaming-only parts from a UI message so |
11 | 19 | // the payload is suitable for attachment to the final Matrix edit. |
12 | 20 | func BuildCompactFinalUIMessage(uiMessage map[string]any) map[string]any { |
@@ -44,6 +52,24 @@ func BuildCompactFinalUIMessage(uiMessage map[string]any) map[string]any { |
44 | 52 | return out |
45 | 53 | } |
46 | 54 |
|
| 55 | +// BuildMinimalFinalUIMessage removes optional detail from a UI message while |
| 56 | +// preserving stable identifiers and metadata. |
| 57 | +func BuildMinimalFinalUIMessage(uiMessage map[string]any) map[string]any { |
| 58 | + if len(uiMessage) == 0 { |
| 59 | + return nil |
| 60 | + } |
| 61 | + out := map[string]any{} |
| 62 | + for _, key := range []string{"id", "role", "metadata"} { |
| 63 | + if value, ok := uiMessage[key]; ok && value != nil { |
| 64 | + out[key] = value |
| 65 | + } |
| 66 | + } |
| 67 | + if len(out) == 0 { |
| 68 | + return nil |
| 69 | + } |
| 70 | + return out |
| 71 | +} |
| 72 | + |
47 | 73 | // BuildDefaultFinalEditExtra builds the SDK's default replacement payload |
48 | 74 | // that should live inside m.new_content for terminal final edits. |
49 | 75 | func BuildDefaultFinalEditExtra(uiMessage map[string]any) map[string]any { |
@@ -107,3 +133,180 @@ func withFinalEditFinishReason(uiMessage map[string]any, finishReason string) ma |
107 | 133 | out["metadata"] = metadata |
108 | 134 | return out |
109 | 135 | } |
| 136 | + |
| 137 | +type FinalEditFitDetails struct { |
| 138 | + OriginalSize int |
| 139 | + FinalSize int |
| 140 | + ClearedFormattedBody bool |
| 141 | + DroppedLinkPreviews bool |
| 142 | + CompactedUIMessage bool |
| 143 | + DroppedUIMessage bool |
| 144 | + DroppedExtra bool |
| 145 | + TrimmedBody bool |
| 146 | +} |
| 147 | + |
| 148 | +func (d FinalEditFitDetails) Changed() bool { |
| 149 | + return d.ClearedFormattedBody || d.DroppedLinkPreviews || d.CompactedUIMessage || d.DroppedUIMessage || d.DroppedExtra || d.TrimmedBody |
| 150 | +} |
| 151 | + |
| 152 | +func (d FinalEditFitDetails) Summary() string { |
| 153 | + steps := make([]string, 0, 6) |
| 154 | + if d.ClearedFormattedBody { |
| 155 | + steps = append(steps, "cleared_formatted_body") |
| 156 | + } |
| 157 | + if d.DroppedLinkPreviews { |
| 158 | + steps = append(steps, "dropped_link_previews") |
| 159 | + } |
| 160 | + if d.CompactedUIMessage { |
| 161 | + steps = append(steps, "compacted_ui_message") |
| 162 | + } |
| 163 | + if d.DroppedUIMessage { |
| 164 | + steps = append(steps, "dropped_ui_message") |
| 165 | + } |
| 166 | + if d.DroppedExtra { |
| 167 | + steps = append(steps, "dropped_extra") |
| 168 | + } |
| 169 | + if d.TrimmedBody { |
| 170 | + steps = append(steps, "trimmed_body") |
| 171 | + } |
| 172 | + if len(steps) == 0 { |
| 173 | + return "" |
| 174 | + } |
| 175 | + return strings.Join(steps, ",") |
| 176 | +} |
| 177 | + |
| 178 | +func cloneFinalEditPayload(payload *FinalEditPayload) *FinalEditPayload { |
| 179 | + if payload == nil { |
| 180 | + return nil |
| 181 | + } |
| 182 | + cloned := &FinalEditPayload{ |
| 183 | + Extra: maps.Clone(payload.Extra), |
| 184 | + TopLevelExtra: maps.Clone(payload.TopLevelExtra), |
| 185 | + } |
| 186 | + if payload.Content != nil { |
| 187 | + content := *payload.Content |
| 188 | + if payload.Content.Mentions != nil { |
| 189 | + mentions := *payload.Content.Mentions |
| 190 | + if len(mentions.UserIDs) > 0 { |
| 191 | + mentions.UserIDs = append([]id.UserID(nil), mentions.UserIDs...) |
| 192 | + } |
| 193 | + content.Mentions = &mentions |
| 194 | + } |
| 195 | + cloned.Content = &content |
| 196 | + } |
| 197 | + return cloned |
| 198 | +} |
| 199 | + |
| 200 | +func estimateFinalEditContentSize(payload *FinalEditPayload, target id.EventID) int { |
| 201 | + if payload == nil || payload.Content == nil { |
| 202 | + return 0 |
| 203 | + } |
| 204 | + content := *payload.Content |
| 205 | + if content.Mentions == nil { |
| 206 | + content.Mentions = &event.Mentions{} |
| 207 | + } |
| 208 | + content.SetEdit(target) |
| 209 | + raw := maps.Clone(payload.TopLevelExtra) |
| 210 | + if raw == nil { |
| 211 | + raw = map[string]any{} |
| 212 | + } |
| 213 | + if payload.Extra != nil { |
| 214 | + raw["m.new_content"] = payload.Extra |
| 215 | + } |
| 216 | + data, err := json.Marshal(&event.Content{ |
| 217 | + Parsed: &content, |
| 218 | + Raw: raw, |
| 219 | + }) |
| 220 | + if err != nil { |
| 221 | + return MaxMatrixEventContentBytes + 1 |
| 222 | + } |
| 223 | + return len(data) |
| 224 | +} |
| 225 | + |
| 226 | +func FitFinalEditPayload(payload *FinalEditPayload, target id.EventID) (*FinalEditPayload, FinalEditFitDetails) { |
| 227 | + fitted := cloneFinalEditPayload(payload) |
| 228 | + if fitted == nil || fitted.Content == nil { |
| 229 | + return fitted, FinalEditFitDetails{} |
| 230 | + } |
| 231 | + details := FinalEditFitDetails{ |
| 232 | + OriginalSize: estimateFinalEditContentSize(fitted, target), |
| 233 | + } |
| 234 | + size := details.OriginalSize |
| 235 | + if size <= MaxMatrixEventContentBytes { |
| 236 | + details.FinalSize = size |
| 237 | + return fitted, details |
| 238 | + } |
| 239 | + |
| 240 | + if fitted.Content.Format != "" || fitted.Content.FormattedBody != "" { |
| 241 | + fitted.Content.Format = "" |
| 242 | + fitted.Content.FormattedBody = "" |
| 243 | + details.ClearedFormattedBody = true |
| 244 | + size = estimateFinalEditContentSize(fitted, target) |
| 245 | + } |
| 246 | + if size > MaxMatrixEventContentBytes && fitted.Extra != nil { |
| 247 | + if _, ok := fitted.Extra["com.beeper.linkpreviews"]; ok { |
| 248 | + delete(fitted.Extra, "com.beeper.linkpreviews") |
| 249 | + details.DroppedLinkPreviews = true |
| 250 | + size = estimateFinalEditContentSize(fitted, target) |
| 251 | + } |
| 252 | + } |
| 253 | + if size > MaxMatrixEventContentBytes && fitted.Extra != nil { |
| 254 | + if rawUI, ok := fitted.Extra[matrixevents.BeeperAIKey].(map[string]any); ok { |
| 255 | + minimalUI := BuildMinimalFinalUIMessage(rawUI) |
| 256 | + switch { |
| 257 | + case minimalUI == nil: |
| 258 | + delete(fitted.Extra, matrixevents.BeeperAIKey) |
| 259 | + details.DroppedUIMessage = true |
| 260 | + case !reflect.DeepEqual(minimalUI, rawUI): |
| 261 | + fitted.Extra[matrixevents.BeeperAIKey] = minimalUI |
| 262 | + details.CompactedUIMessage = true |
| 263 | + } |
| 264 | + size = estimateFinalEditContentSize(fitted, target) |
| 265 | + } |
| 266 | + } |
| 267 | + if size > MaxMatrixEventContentBytes && fitted.Extra != nil { |
| 268 | + if _, ok := fitted.Extra[matrixevents.BeeperAIKey]; ok { |
| 269 | + delete(fitted.Extra, matrixevents.BeeperAIKey) |
| 270 | + details.DroppedUIMessage = true |
| 271 | + size = estimateFinalEditContentSize(fitted, target) |
| 272 | + } |
| 273 | + } |
| 274 | + if size > MaxMatrixEventContentBytes && len(fitted.Extra) > 0 { |
| 275 | + fitted.Extra = nil |
| 276 | + details.DroppedExtra = true |
| 277 | + size = estimateFinalEditContentSize(fitted, target) |
| 278 | + } |
| 279 | + if size > MaxMatrixEventContentBytes && fitted.Content != nil && fitted.Content.Body != "" { |
| 280 | + best := strings.TrimSpace(fitted.Content.Body) |
| 281 | + low, high := 1, len(fitted.Content.Body) |
| 282 | + for low <= high { |
| 283 | + mid := (low + high) / 2 |
| 284 | + candidate, _ := turns.SplitAtMarkdownBoundary(fitted.Content.Body, mid) |
| 285 | + candidate = strings.TrimSpace(candidate) |
| 286 | + if candidate == "" { |
| 287 | + high = mid - 1 |
| 288 | + continue |
| 289 | + } |
| 290 | + fitted.Content.Body = candidate |
| 291 | + candidateSize := estimateFinalEditContentSize(fitted, target) |
| 292 | + if candidateSize <= MaxMatrixEventContentBytes { |
| 293 | + best = candidate |
| 294 | + low = mid + 1 |
| 295 | + } else { |
| 296 | + high = mid - 1 |
| 297 | + } |
| 298 | + } |
| 299 | + fitted.Content.Body = best |
| 300 | + details.TrimmedBody = best != strings.TrimSpace(payload.Content.Body) |
| 301 | + size = estimateFinalEditContentSize(fitted, target) |
| 302 | + } |
| 303 | + details.FinalSize = size |
| 304 | + return fitted, details |
| 305 | +} |
| 306 | + |
| 307 | +func FormatFinalEditFitLog(details FinalEditFitDetails) string { |
| 308 | + if !details.Changed() { |
| 309 | + return "" |
| 310 | + } |
| 311 | + return fmt.Sprintf("%d->%d bytes (%s)", details.OriginalSize, details.FinalSize, details.Summary()) |
| 312 | +} |
0 commit comments