fix(forwarder): handle Cursor summarize actions

Map Cursor's protocol-level SummarizeAction to manual context compaction while keeping the legacy /compact directive disabled.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
DedSecer
2026-07-30 19:32:42 +08:00
co-authored by Cursor
parent 17f30a8852
commit 4451a6df3b
4 changed files with 295 additions and 5 deletions
+54 -5
View File
@@ -77,7 +77,7 @@ func (service *Service) maybeCompactBeforeProvider(stream *ActiveStream, convers
if service == nil || stream == nil || conversation == nil {
return false, nil
}
manualInstruction, manual := parseManualCompactionDirective(stream.LatestUserText)
manualInstruction, manual := streamManualCompactionDirective(stream)
plan, err := service.buildCompactionPlan(stream, conversation, compiled, manual, manualInstruction)
if err != nil {
return false, err
@@ -827,7 +827,7 @@ func buildFallbackCompactionSummary(plan *PendingCompaction) string {
sections = append(sections, "Compaction note:\n"+truncateCompactionText(plan.HookMessage, 800))
}
if strings.TrimSpace(plan.ManualInstruction) != "" {
sections = append(sections, "Manual compact instruction:\n"+truncateCompactionText(plan.ManualInstruction, 800))
sections = append(sections, "Manual summarize instruction:\n"+truncateCompactionText(plan.ManualInstruction, 800))
}
return strings.TrimSpace(truncateCompactionText(strings.Join(sections, "\n\n"), compactionSummaryMaxChars))
}
@@ -875,13 +875,62 @@ func (service *Service) resolveCompactionReserveTokens(modelID string) int64 {
return compactionAutoReserveTokens
}
func parseManualCompactionRequest(userMessage *agentv1.UserMessage) (string, bool) {
if userMessage == nil {
return "", false
}
userText := strings.TrimSpace(userMessage.GetText())
if instruction, ok := parseManualCompactionDirective(userText); ok {
return instruction, true
}
if userText != "" {
return "", false
}
selectedContext := userMessage.GetSelectedContext()
if selectedContext == nil {
return "", false
}
for _, command := range selectedContext.GetCursorCommands() {
if !isCursorSummarizeCommand(command) {
continue
}
instruction, _ := parseManualCompactionDirective(command.GetContent())
return instruction, true
}
return "", false
}
func streamManualCompactionDirective(stream *ActiveStream) (string, bool) {
if stream == nil {
return "", false
}
stream.mu.Lock()
defer stream.mu.Unlock()
if stream.ManualCompaction.Requested {
return strings.TrimSpace(stream.ManualCompaction.Instruction), true
}
return parseManualCompactionDirective(stream.LatestUserText)
}
func isCursorSummarizeCommand(command *agentv1.SelectedCursorCommand) bool {
if command == nil {
return false
}
if strings.EqualFold(strings.TrimSpace(command.GetName()), "glass-action-summarize") {
return true
}
_, ok := parseManualCompactionDirective(command.GetContent())
return ok
}
func parseManualCompactionDirective(latestUserText string) (string, bool) {
trimmed := strings.TrimSpace(latestUserText)
const directive = "/summarize"
switch {
case trimmed == "/compact":
case trimmed == directive:
return "", true
case strings.HasPrefix(trimmed, "/compact "):
return strings.TrimSpace(strings.TrimPrefix(trimmed, "/compact")), true
case strings.HasPrefix(trimmed, directive+" "):
return strings.TrimSpace(strings.TrimPrefix(trimmed, directive)), true
default:
return "", false
}