feat: filter and drop empty assistant messages in Kimi executor
- Added `filterKimiEmptyAssistantMessages` to identify and remove empty assistant messages with no content, tool links, or reasoning. - Integrated logging to track the number of dropped messages. - Updated tests to validate the filtering logic for both empty and valid assistant messages. Fixed: #1730
This commit is contained in:
@@ -322,7 +322,17 @@ func normalizeKimiToolMessageLinks(body []byte) ([]byte, error) {
|
||||
return body, nil
|
||||
}
|
||||
|
||||
out := body
|
||||
msgs := messages.Array()
|
||||
out, dropped, err := filterKimiEmptyAssistantMessages(body, msgs)
|
||||
if err != nil {
|
||||
return body, err
|
||||
}
|
||||
if dropped > 0 {
|
||||
log.WithField("dropped_assistant_messages", dropped).Debug("kimi executor: dropped empty assistant messages")
|
||||
}
|
||||
|
||||
messages = gjson.GetBytes(out, "messages")
|
||||
msgs = messages.Array()
|
||||
pending := make([]string, 0)
|
||||
patched := 0
|
||||
patchedReasoning := 0
|
||||
@@ -340,7 +350,6 @@ func normalizeKimiToolMessageLinks(body []byte) ([]byte, error) {
|
||||
}
|
||||
}
|
||||
|
||||
msgs := messages.Array()
|
||||
for msgIdx := range msgs {
|
||||
msg := msgs[msgIdx]
|
||||
role := strings.TrimSpace(msg.Get("role").String())
|
||||
@@ -428,6 +437,96 @@ func normalizeKimiToolMessageLinks(body []byte) ([]byte, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func filterKimiEmptyAssistantMessages(body []byte, msgs []gjson.Result) ([]byte, int, error) {
|
||||
kept := make([]string, 0, len(msgs))
|
||||
dropped := 0
|
||||
for _, msg := range msgs {
|
||||
if shouldDropKimiAssistantMessage(msg) {
|
||||
dropped++
|
||||
continue
|
||||
}
|
||||
kept = append(kept, msg.Raw)
|
||||
}
|
||||
if dropped == 0 {
|
||||
return body, 0, nil
|
||||
}
|
||||
|
||||
rawMessages := []byte("[" + strings.Join(kept, ",") + "]")
|
||||
out, err := sjson.SetRawBytes(body, "messages", rawMessages)
|
||||
if err != nil {
|
||||
return body, 0, fmt.Errorf("kimi executor: failed to drop empty assistant messages: %w", err)
|
||||
}
|
||||
return out, dropped, nil
|
||||
}
|
||||
|
||||
func shouldDropKimiAssistantMessage(msg gjson.Result) bool {
|
||||
if strings.TrimSpace(msg.Get("role").String()) != "assistant" {
|
||||
return false
|
||||
}
|
||||
if hasKimiToolCalls(msg) || hasKimiLegacyFunctionCall(msg) || hasKimiAssistantReasoning(msg) {
|
||||
return false
|
||||
}
|
||||
return isKimiAssistantContentEmpty(msg.Get("content"))
|
||||
}
|
||||
|
||||
func hasKimiToolCalls(msg gjson.Result) bool {
|
||||
toolCalls := msg.Get("tool_calls")
|
||||
return toolCalls.Exists() && toolCalls.IsArray() && len(toolCalls.Array()) > 0
|
||||
}
|
||||
|
||||
func hasKimiLegacyFunctionCall(msg gjson.Result) bool {
|
||||
functionCall := msg.Get("function_call")
|
||||
if !functionCall.Exists() || functionCall.Type == gjson.Null {
|
||||
return false
|
||||
}
|
||||
if functionCall.IsObject() && strings.TrimSpace(functionCall.Raw) == "{}" {
|
||||
return false
|
||||
}
|
||||
return strings.TrimSpace(functionCall.Raw) != ""
|
||||
}
|
||||
|
||||
func hasKimiAssistantReasoning(msg gjson.Result) bool {
|
||||
reasoning := msg.Get("reasoning_content")
|
||||
return reasoning.Exists() && strings.TrimSpace(reasoning.String()) != ""
|
||||
}
|
||||
|
||||
func isKimiAssistantContentEmpty(content gjson.Result) bool {
|
||||
if !content.Exists() || content.Type == gjson.Null {
|
||||
return true
|
||||
}
|
||||
if content.Type == gjson.String {
|
||||
return strings.TrimSpace(content.String()) == ""
|
||||
}
|
||||
if !content.IsArray() {
|
||||
return false
|
||||
}
|
||||
for _, part := range content.Array() {
|
||||
if !isKimiAssistantContentPartEmpty(part) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isKimiAssistantContentPartEmpty(part gjson.Result) bool {
|
||||
if !part.Exists() || part.Type == gjson.Null {
|
||||
return true
|
||||
}
|
||||
if part.Type == gjson.String {
|
||||
return strings.TrimSpace(part.String()) == ""
|
||||
}
|
||||
if !part.IsObject() {
|
||||
return false
|
||||
}
|
||||
if text := part.Get("text"); text.Exists() {
|
||||
return strings.TrimSpace(text.String()) == ""
|
||||
}
|
||||
if strings.TrimSpace(part.Get("type").String()) == "text" {
|
||||
return true
|
||||
}
|
||||
return strings.TrimSpace(part.Raw) == "{}"
|
||||
}
|
||||
|
||||
func fallbackAssistantReasoning(msg gjson.Result, hasLatest bool, latest string) string {
|
||||
if hasLatest && strings.TrimSpace(latest) != "" {
|
||||
return latest
|
||||
|
||||
Reference in New Issue
Block a user