mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-08-17 19:47:10 +08:00
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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user