fix(kiro): fix translator format mismatch for OpenAI protocol

Amp-Thread-ID: https://ampcode.com/threads/T-019b092b-f2de-72a1-b428-72511c0de628
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Ravens
2025-12-11 01:15:00 +08:00
parent 084e2666cb
commit 8d5f89ccfd
3 changed files with 77 additions and 77 deletions
+5 -50
View File
@@ -1,14 +1,11 @@
// Package claude provides translation between Kiro and Claude formats.
// Since Kiro uses Claude-compatible format internally, translations are mostly pass-through.
// However, SSE events require proper "event: <type>" prefix for Claude clients.
// Since Kiro executor generates Claude-compatible SSE format internally (with event: prefix),
// translations are pass-through.
package claude
import (
"bytes"
"context"
"strings"
"github.com/tidwall/gjson"
)
// ConvertClaudeRequestToKiro converts Claude request to Kiro format.
@@ -18,52 +15,10 @@ func ConvertClaudeRequestToKiro(modelName string, inputRawJSON []byte, stream bo
}
// ConvertKiroResponseToClaude converts Kiro streaming response to Claude format.
// It adds the required "event: <type>" prefix for SSE compliance with Claude clients.
// Input format: "data: {\"type\":\"message_start\",...}"
// Output format: "event: message_start\ndata: {\"type\":\"message_start\",...}"
// Kiro executor already generates complete SSE format with "event:" prefix,
// so this is a simple pass-through.
func ConvertKiroResponseToClaude(ctx context.Context, model string, originalRequest, request, rawResponse []byte, param *any) []string {
raw := string(rawResponse)
// Handle multiple data blocks (e.g., message_delta + message_stop)
lines := strings.Split(raw, "\n\n")
var results []string
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Extract event type from JSON and add "event:" prefix
formatted := addEventPrefix(line)
if formatted != "" {
results = append(results, formatted)
}
}
if len(results) == 0 {
return []string{raw}
}
return results
}
// addEventPrefix extracts the event type from the data line and adds the event: prefix.
// Input: "data: {\"type\":\"message_start\",...}"
// Output: "event: message_start\ndata: {\"type\":\"message_start\",...}"
func addEventPrefix(dataLine string) string {
if !strings.HasPrefix(dataLine, "data: ") {
return dataLine
}
jsonPart := strings.TrimPrefix(dataLine, "data: ")
eventType := gjson.Get(jsonPart, "type").String()
if eventType == "" {
return dataLine
}
return "event: " + eventType + "\n" + dataLine
return []string{string(rawResponse)}
}
// ConvertKiroResponseToClaudeNonStream converts Kiro non-streaming response to Claude format.