mirror of
https://github.com/router-for-me/CLIProxyAPIPlus.git
synced 2026-04-25 04:46:46 +00:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b27a175fef | ||
|
|
8d5f89ccfd | ||
|
|
084e2666cb | ||
|
|
2eb2dbb266 | ||
|
|
e717939edb | ||
|
|
7758a86d1e | ||
|
|
9f72a875f8 | ||
|
|
94d61c7b2b | ||
|
|
f999650322 | ||
|
|
1249b07eb8 | ||
|
|
1770c491db | ||
|
|
2bf9e08b31 | ||
|
|
f56bfaa689 | ||
|
|
5d716dc796 |
@@ -1323,7 +1323,7 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out
|
|||||||
// Send message_start on first event
|
// Send message_start on first event
|
||||||
if !messageStartSent {
|
if !messageStartSent {
|
||||||
msgStart := e.buildClaudeMessageStartEvent(model, totalUsage.InputTokens)
|
msgStart := e.buildClaudeMessageStartEvent(model, totalUsage.InputTokens)
|
||||||
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, msgStart, &translatorParam)
|
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, msgStart, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
||||||
@@ -1372,7 +1372,7 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out
|
|||||||
contentBlockIndex++
|
contentBlockIndex++
|
||||||
isTextBlockOpen = true
|
isTextBlockOpen = true
|
||||||
blockStart := e.buildClaudeContentBlockStartEvent(contentBlockIndex, "text", "", "")
|
blockStart := e.buildClaudeContentBlockStartEvent(contentBlockIndex, "text", "", "")
|
||||||
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, blockStart, &translatorParam)
|
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, blockStart, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
||||||
@@ -1381,7 +1381,7 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out
|
|||||||
}
|
}
|
||||||
|
|
||||||
claudeEvent := e.buildClaudeStreamEvent(contentDelta, contentBlockIndex)
|
claudeEvent := e.buildClaudeStreamEvent(contentDelta, contentBlockIndex)
|
||||||
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, claudeEvent, &translatorParam)
|
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, claudeEvent, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
||||||
@@ -1404,7 +1404,7 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out
|
|||||||
// Close text block if open before starting tool_use block
|
// Close text block if open before starting tool_use block
|
||||||
if isTextBlockOpen && contentBlockIndex >= 0 {
|
if isTextBlockOpen && contentBlockIndex >= 0 {
|
||||||
blockStop := e.buildClaudeContentBlockStopEvent(contentBlockIndex)
|
blockStop := e.buildClaudeContentBlockStopEvent(contentBlockIndex)
|
||||||
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, blockStop, &translatorParam)
|
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, blockStop, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
||||||
@@ -1418,7 +1418,7 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out
|
|||||||
toolName := getString(tu, "name")
|
toolName := getString(tu, "name")
|
||||||
|
|
||||||
blockStart := e.buildClaudeContentBlockStartEvent(contentBlockIndex, "tool_use", toolUseID, toolName)
|
blockStart := e.buildClaudeContentBlockStartEvent(contentBlockIndex, "tool_use", toolUseID, toolName)
|
||||||
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, blockStart, &translatorParam)
|
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, blockStart, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
||||||
@@ -1433,7 +1433,7 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out
|
|||||||
// Don't continue - still need to close the block
|
// Don't continue - still need to close the block
|
||||||
} else {
|
} else {
|
||||||
inputDelta := e.buildClaudeInputJsonDeltaEvent(string(inputJSON), contentBlockIndex)
|
inputDelta := e.buildClaudeInputJsonDeltaEvent(string(inputJSON), contentBlockIndex)
|
||||||
sseData = sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, inputDelta, &translatorParam)
|
sseData = sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, inputDelta, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
||||||
@@ -1444,7 +1444,7 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out
|
|||||||
|
|
||||||
// Close tool_use block (always close even if input marshal failed)
|
// Close tool_use block (always close even if input marshal failed)
|
||||||
blockStop := e.buildClaudeContentBlockStopEvent(contentBlockIndex)
|
blockStop := e.buildClaudeContentBlockStopEvent(contentBlockIndex)
|
||||||
sseData = sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, blockStop, &translatorParam)
|
sseData = sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, blockStop, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
||||||
@@ -1464,7 +1464,7 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out
|
|||||||
// Close text block if open
|
// Close text block if open
|
||||||
if isTextBlockOpen && contentBlockIndex >= 0 {
|
if isTextBlockOpen && contentBlockIndex >= 0 {
|
||||||
blockStop := e.buildClaudeContentBlockStopEvent(contentBlockIndex)
|
blockStop := e.buildClaudeContentBlockStopEvent(contentBlockIndex)
|
||||||
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, blockStop, &translatorParam)
|
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, blockStop, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
||||||
@@ -1476,7 +1476,7 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out
|
|||||||
contentBlockIndex++
|
contentBlockIndex++
|
||||||
|
|
||||||
blockStart := e.buildClaudeContentBlockStartEvent(contentBlockIndex, "tool_use", tu.ToolUseID, tu.Name)
|
blockStart := e.buildClaudeContentBlockStartEvent(contentBlockIndex, "tool_use", tu.ToolUseID, tu.Name)
|
||||||
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, blockStart, &translatorParam)
|
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, blockStart, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
||||||
@@ -1489,7 +1489,7 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out
|
|||||||
log.Debugf("kiro: failed to marshal tool input in toolUseEvent: %v", err)
|
log.Debugf("kiro: failed to marshal tool input in toolUseEvent: %v", err)
|
||||||
} else {
|
} else {
|
||||||
inputDelta := e.buildClaudeInputJsonDeltaEvent(string(inputJSON), contentBlockIndex)
|
inputDelta := e.buildClaudeInputJsonDeltaEvent(string(inputJSON), contentBlockIndex)
|
||||||
sseData = sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, inputDelta, &translatorParam)
|
sseData = sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, inputDelta, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
||||||
@@ -1499,7 +1499,7 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out
|
|||||||
}
|
}
|
||||||
|
|
||||||
blockStop := e.buildClaudeContentBlockStopEvent(contentBlockIndex)
|
blockStop := e.buildClaudeContentBlockStopEvent(contentBlockIndex)
|
||||||
sseData = sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, blockStop, &translatorParam)
|
sseData = sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, blockStop, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
||||||
@@ -1530,7 +1530,7 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out
|
|||||||
// Close content block if open
|
// Close content block if open
|
||||||
if isTextBlockOpen && contentBlockIndex >= 0 {
|
if isTextBlockOpen && contentBlockIndex >= 0 {
|
||||||
blockStop := e.buildClaudeContentBlockStopEvent(contentBlockIndex)
|
blockStop := e.buildClaudeContentBlockStopEvent(contentBlockIndex)
|
||||||
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, blockStop, &translatorParam)
|
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, blockStop, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
||||||
@@ -1555,7 +1555,7 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out
|
|||||||
|
|
||||||
// Send message_delta and message_stop
|
// Send message_delta and message_stop
|
||||||
msgStop := e.buildClaudeMessageStopEvent(stopReason, totalUsage)
|
msgStop := e.buildClaudeMessageStopEvent(stopReason, totalUsage)
|
||||||
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, msgStop, &translatorParam)
|
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, msgStop, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunk + "\n\n")}
|
||||||
@@ -1566,6 +1566,7 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out
|
|||||||
|
|
||||||
|
|
||||||
// Claude SSE event builders
|
// Claude SSE event builders
|
||||||
|
// All builders return complete SSE format with "event:" line for Claude client compatibility.
|
||||||
func (e *KiroExecutor) buildClaudeMessageStartEvent(model string, inputTokens int64) []byte {
|
func (e *KiroExecutor) buildClaudeMessageStartEvent(model string, inputTokens int64) []byte {
|
||||||
event := map[string]interface{}{
|
event := map[string]interface{}{
|
||||||
"type": "message_start",
|
"type": "message_start",
|
||||||
@@ -1581,7 +1582,7 @@ func (e *KiroExecutor) buildClaudeMessageStartEvent(model string, inputTokens in
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
result, _ := json.Marshal(event)
|
result, _ := json.Marshal(event)
|
||||||
return []byte("data: " + string(result))
|
return []byte("event: message_start\ndata: " + string(result))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *KiroExecutor) buildClaudeContentBlockStartEvent(index int, blockType, toolUseID, toolName string) []byte {
|
func (e *KiroExecutor) buildClaudeContentBlockStartEvent(index int, blockType, toolUseID, toolName string) []byte {
|
||||||
@@ -1606,7 +1607,7 @@ func (e *KiroExecutor) buildClaudeContentBlockStartEvent(index int, blockType, t
|
|||||||
"content_block": contentBlock,
|
"content_block": contentBlock,
|
||||||
}
|
}
|
||||||
result, _ := json.Marshal(event)
|
result, _ := json.Marshal(event)
|
||||||
return []byte("data: " + string(result))
|
return []byte("event: content_block_start\ndata: " + string(result))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *KiroExecutor) buildClaudeStreamEvent(contentDelta string, index int) []byte {
|
func (e *KiroExecutor) buildClaudeStreamEvent(contentDelta string, index int) []byte {
|
||||||
@@ -1619,7 +1620,7 @@ func (e *KiroExecutor) buildClaudeStreamEvent(contentDelta string, index int) []
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
result, _ := json.Marshal(event)
|
result, _ := json.Marshal(event)
|
||||||
return []byte("data: " + string(result))
|
return []byte("event: content_block_delta\ndata: " + string(result))
|
||||||
}
|
}
|
||||||
|
|
||||||
// buildClaudeInputJsonDeltaEvent creates an input_json_delta event for tool use streaming
|
// buildClaudeInputJsonDeltaEvent creates an input_json_delta event for tool use streaming
|
||||||
@@ -1633,7 +1634,7 @@ func (e *KiroExecutor) buildClaudeInputJsonDeltaEvent(partialJSON string, index
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
result, _ := json.Marshal(event)
|
result, _ := json.Marshal(event)
|
||||||
return []byte("data: " + string(result))
|
return []byte("event: content_block_delta\ndata: " + string(result))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *KiroExecutor) buildClaudeContentBlockStopEvent(index int) []byte {
|
func (e *KiroExecutor) buildClaudeContentBlockStopEvent(index int) []byte {
|
||||||
@@ -1642,7 +1643,7 @@ func (e *KiroExecutor) buildClaudeContentBlockStopEvent(index int) []byte {
|
|||||||
"index": index,
|
"index": index,
|
||||||
}
|
}
|
||||||
result, _ := json.Marshal(event)
|
result, _ := json.Marshal(event)
|
||||||
return []byte("data: " + string(result))
|
return []byte("event: content_block_stop\ndata: " + string(result))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *KiroExecutor) buildClaudeMessageStopEvent(stopReason string, usageInfo usage.Detail) []byte {
|
func (e *KiroExecutor) buildClaudeMessageStopEvent(stopReason string, usageInfo usage.Detail) []byte {
|
||||||
@@ -1666,7 +1667,7 @@ func (e *KiroExecutor) buildClaudeMessageStopEvent(stopReason string, usageInfo
|
|||||||
}
|
}
|
||||||
stopResult, _ := json.Marshal(stopEvent)
|
stopResult, _ := json.Marshal(stopEvent)
|
||||||
|
|
||||||
return []byte("data: " + string(deltaResult) + "\n\ndata: " + string(stopResult))
|
return []byte("event: message_delta\ndata: " + string(deltaResult) + "\n\nevent: message_stop\ndata: " + string(stopResult))
|
||||||
}
|
}
|
||||||
|
|
||||||
// buildClaudeFinalEvent constructs the final Claude-style event.
|
// buildClaudeFinalEvent constructs the final Claude-style event.
|
||||||
@@ -1675,7 +1676,7 @@ func (e *KiroExecutor) buildClaudeFinalEvent() []byte {
|
|||||||
"type": "message_stop",
|
"type": "message_stop",
|
||||||
}
|
}
|
||||||
result, _ := json.Marshal(event)
|
result, _ := json.Marshal(event)
|
||||||
return []byte("data: " + string(result))
|
return []byte("event: message_stop\ndata: " + string(result))
|
||||||
}
|
}
|
||||||
|
|
||||||
// CountTokens is not supported for the Kiro provider.
|
// CountTokens is not supported for the Kiro provider.
|
||||||
@@ -1890,7 +1891,7 @@ func (e *KiroExecutor) streamEventStream(ctx context.Context, body io.Reader, c
|
|||||||
|
|
||||||
if !messageStartSent {
|
if !messageStartSent {
|
||||||
msgStart := e.buildClaudeMessageStartEvent(model, totalUsage.InputTokens)
|
msgStart := e.buildClaudeMessageStartEvent(model, totalUsage.InputTokens)
|
||||||
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, msgStart, &translatorParam)
|
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, msgStart, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
c.Writer.Write([]byte(chunk + "\n\n"))
|
c.Writer.Write([]byte(chunk + "\n\n"))
|
||||||
@@ -1921,7 +1922,7 @@ func (e *KiroExecutor) streamEventStream(ctx context.Context, body io.Reader, c
|
|||||||
contentBlockIndex++
|
contentBlockIndex++
|
||||||
isBlockOpen = true
|
isBlockOpen = true
|
||||||
blockStart := e.buildClaudeContentBlockStartEvent(contentBlockIndex, "text", "", "")
|
blockStart := e.buildClaudeContentBlockStartEvent(contentBlockIndex, "text", "", "")
|
||||||
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, blockStart, &translatorParam)
|
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, blockStart, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
c.Writer.Write([]byte(chunk + "\n\n"))
|
c.Writer.Write([]byte(chunk + "\n\n"))
|
||||||
@@ -1931,7 +1932,7 @@ func (e *KiroExecutor) streamEventStream(ctx context.Context, body io.Reader, c
|
|||||||
}
|
}
|
||||||
|
|
||||||
claudeEvent := e.buildClaudeStreamEvent(contentDelta, contentBlockIndex)
|
claudeEvent := e.buildClaudeStreamEvent(contentDelta, contentBlockIndex)
|
||||||
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, claudeEvent, &translatorParam)
|
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, claudeEvent, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
c.Writer.Write([]byte(chunk + "\n\n"))
|
c.Writer.Write([]byte(chunk + "\n\n"))
|
||||||
@@ -1964,7 +1965,7 @@ func (e *KiroExecutor) streamEventStream(ctx context.Context, body io.Reader, c
|
|||||||
// Close content block if open
|
// Close content block if open
|
||||||
if isBlockOpen && contentBlockIndex >= 0 {
|
if isBlockOpen && contentBlockIndex >= 0 {
|
||||||
blockStop := e.buildClaudeContentBlockStopEvent(contentBlockIndex)
|
blockStop := e.buildClaudeContentBlockStopEvent(contentBlockIndex)
|
||||||
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, blockStop, &translatorParam)
|
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, blockStop, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
c.Writer.Write([]byte(chunk + "\n\n"))
|
c.Writer.Write([]byte(chunk + "\n\n"))
|
||||||
@@ -1984,7 +1985,7 @@ func (e *KiroExecutor) streamEventStream(ctx context.Context, body io.Reader, c
|
|||||||
|
|
||||||
// Always use end_turn (no tool_use support)
|
// Always use end_turn (no tool_use support)
|
||||||
msgStop := e.buildClaudeMessageStopEvent("end_turn", totalUsage)
|
msgStop := e.buildClaudeMessageStopEvent("end_turn", totalUsage)
|
||||||
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("claude"), targetFormat, model, originalReq, claudeBody, msgStop, &translatorParam)
|
sseData := sdktranslator.TranslateStream(ctx, sdktranslator.FromString("kiro"), targetFormat, model, originalReq, claudeBody, msgStop, &translatorParam)
|
||||||
for _, chunk := range sseData {
|
for _, chunk := range sseData {
|
||||||
if chunk != "" {
|
if chunk != "" {
|
||||||
c.Writer.Write([]byte(chunk + "\n\n"))
|
c.Writer.Write([]byte(chunk + "\n\n"))
|
||||||
|
|||||||
@@ -157,7 +157,7 @@ func appendAPIResponseChunk(ctx context.Context, cfg *config.Config, chunk []byt
|
|||||||
if ginCtx == nil {
|
if ginCtx == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, attempt := ensureAttempt(ginCtx)
|
attempts, attempt := ensureAttempt(ginCtx)
|
||||||
ensureResponseIntro(attempt)
|
ensureResponseIntro(attempt)
|
||||||
|
|
||||||
if !attempt.headersWritten {
|
if !attempt.headersWritten {
|
||||||
@@ -175,6 +175,8 @@ func appendAPIResponseChunk(ctx context.Context, cfg *config.Config, chunk []byt
|
|||||||
}
|
}
|
||||||
attempt.response.WriteString(string(data))
|
attempt.response.WriteString(string(data))
|
||||||
attempt.bodyHasContent = true
|
attempt.bodyHasContent = true
|
||||||
|
|
||||||
|
updateAggregatedResponse(ginCtx, attempts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ginContextFrom(ctx context.Context) *gin.Context {
|
func ginContextFrom(ctx context.Context) *gin.Context {
|
||||||
|
|||||||
@@ -123,6 +123,15 @@ func ConvertClaudeRequestToAntigravity(modelName string, inputRawJSON []byte, _
|
|||||||
functionResponse := client.FunctionResponse{ID: toolCallID, Name: funcName, Response: map[string]interface{}{"result": responseData}}
|
functionResponse := client.FunctionResponse{ID: toolCallID, Name: funcName, Response: map[string]interface{}{"result": responseData}}
|
||||||
clientContent.Parts = append(clientContent.Parts, client.Part{FunctionResponse: &functionResponse})
|
clientContent.Parts = append(clientContent.Parts, client.Part{FunctionResponse: &functionResponse})
|
||||||
}
|
}
|
||||||
|
} else if contentTypeResult.Type == gjson.String && contentTypeResult.String() == "image" {
|
||||||
|
sourceResult := contentResult.Get("source")
|
||||||
|
if sourceResult.Get("type").String() == "base64" {
|
||||||
|
inlineData := &client.InlineData{
|
||||||
|
MimeType: sourceResult.Get("media_type").String(),
|
||||||
|
Data: sourceResult.Get("data").String(),
|
||||||
|
}
|
||||||
|
clientContent.Parts = append(clientContent.Parts, client.Part{InlineData: inlineData})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
contents = append(contents, clientContent)
|
contents = append(contents, clientContent)
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
. "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/gemini/openai/chat-completions"
|
. "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/gemini/openai/chat-completions"
|
||||||
@@ -24,6 +25,9 @@ type convertCliResponseToOpenAIChatParams struct {
|
|||||||
FunctionIndex int
|
FunctionIndex int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// functionCallIDCounter provides a process-wide unique counter for function call identifiers.
|
||||||
|
var functionCallIDCounter uint64
|
||||||
|
|
||||||
// ConvertAntigravityResponseToOpenAI translates a single chunk of a streaming response from the
|
// ConvertAntigravityResponseToOpenAI translates a single chunk of a streaming response from the
|
||||||
// Gemini CLI API format to the OpenAI Chat Completions streaming format.
|
// Gemini CLI API format to the OpenAI Chat Completions streaming format.
|
||||||
// It processes various Gemini CLI event types and transforms them into OpenAI-compatible JSON responses.
|
// It processes various Gemini CLI event types and transforms them into OpenAI-compatible JSON responses.
|
||||||
@@ -146,7 +150,7 @@ func ConvertAntigravityResponseToOpenAI(_ context.Context, _ string, originalReq
|
|||||||
|
|
||||||
functionCallTemplate := `{"id": "","index": 0,"type": "function","function": {"name": "","arguments": ""}}`
|
functionCallTemplate := `{"id": "","index": 0,"type": "function","function": {"name": "","arguments": ""}}`
|
||||||
fcName := functionCallResult.Get("name").String()
|
fcName := functionCallResult.Get("name").String()
|
||||||
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "id", fmt.Sprintf("%s-%d", fcName, time.Now().UnixNano()))
|
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "id", fmt.Sprintf("%s-%d-%d", fcName, time.Now().UnixNano(), atomic.AddUint64(&functionCallIDCounter, 1)))
|
||||||
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "index", functionCallIndex)
|
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "index", functionCallIndex)
|
||||||
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "function.name", fcName)
|
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "function.name", fcName)
|
||||||
if fcArgsResult := functionCallResult.Get("args"); fcArgsResult.Exists() {
|
if fcArgsResult := functionCallResult.Get("args"); fcArgsResult.Exists() {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
@@ -27,6 +28,9 @@ type Params struct {
|
|||||||
ResponseIndex int // Index counter for content blocks in the streaming response
|
ResponseIndex int // Index counter for content blocks in the streaming response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// toolUseIDCounter provides a process-wide unique counter for tool use identifiers.
|
||||||
|
var toolUseIDCounter uint64
|
||||||
|
|
||||||
// ConvertGeminiCLIResponseToClaude performs sophisticated streaming response format conversion.
|
// ConvertGeminiCLIResponseToClaude performs sophisticated streaming response format conversion.
|
||||||
// This function implements a complex state machine that translates backend client responses
|
// This function implements a complex state machine that translates backend client responses
|
||||||
// into Claude Code-compatible Server-Sent Events (SSE) format. It manages different response types
|
// into Claude Code-compatible Server-Sent Events (SSE) format. It manages different response types
|
||||||
@@ -180,7 +184,7 @@ func ConvertGeminiCLIResponseToClaude(_ context.Context, _ string, originalReque
|
|||||||
|
|
||||||
// Create the tool use block with unique ID and function details
|
// Create the tool use block with unique ID and function details
|
||||||
data := fmt.Sprintf(`{"type":"content_block_start","index":%d,"content_block":{"type":"tool_use","id":"","name":"","input":{}}}`, (*param).(*Params).ResponseIndex)
|
data := fmt.Sprintf(`{"type":"content_block_start","index":%d,"content_block":{"type":"tool_use","id":"","name":"","input":{}}}`, (*param).(*Params).ResponseIndex)
|
||||||
data, _ = sjson.Set(data, "content_block.id", fmt.Sprintf("%s-%d", fcName, time.Now().UnixNano()))
|
data, _ = sjson.Set(data, "content_block.id", fmt.Sprintf("%s-%d-%d", fcName, time.Now().UnixNano(), atomic.AddUint64(&toolUseIDCounter, 1)))
|
||||||
data, _ = sjson.Set(data, "content_block.name", fcName)
|
data, _ = sjson.Set(data, "content_block.name", fcName)
|
||||||
sb.WriteString(fmt.Sprintf("data: %s\n\n\n", data))
|
sb.WriteString(fmt.Sprintf("data: %s\n\n\n", data))
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
. "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/gemini/openai/chat-completions"
|
. "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/gemini/openai/chat-completions"
|
||||||
@@ -24,6 +25,9 @@ type convertCliResponseToOpenAIChatParams struct {
|
|||||||
FunctionIndex int
|
FunctionIndex int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// functionCallIDCounter provides a process-wide unique counter for function call identifiers.
|
||||||
|
var functionCallIDCounter uint64
|
||||||
|
|
||||||
// ConvertCliResponseToOpenAI translates a single chunk of a streaming response from the
|
// ConvertCliResponseToOpenAI translates a single chunk of a streaming response from the
|
||||||
// Gemini CLI API format to the OpenAI Chat Completions streaming format.
|
// Gemini CLI API format to the OpenAI Chat Completions streaming format.
|
||||||
// It processes various Gemini CLI event types and transforms them into OpenAI-compatible JSON responses.
|
// It processes various Gemini CLI event types and transforms them into OpenAI-compatible JSON responses.
|
||||||
@@ -146,7 +150,7 @@ func ConvertCliResponseToOpenAI(_ context.Context, _ string, originalRequestRawJ
|
|||||||
|
|
||||||
functionCallTemplate := `{"id": "","index": 0,"type": "function","function": {"name": "","arguments": ""}}`
|
functionCallTemplate := `{"id": "","index": 0,"type": "function","function": {"name": "","arguments": ""}}`
|
||||||
fcName := functionCallResult.Get("name").String()
|
fcName := functionCallResult.Get("name").String()
|
||||||
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "id", fmt.Sprintf("%s-%d", fcName, time.Now().UnixNano()))
|
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "id", fmt.Sprintf("%s-%d-%d", fcName, time.Now().UnixNano(), atomic.AddUint64(&functionCallIDCounter, 1)))
|
||||||
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "index", functionCallIndex)
|
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "index", functionCallIndex)
|
||||||
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "function.name", fcName)
|
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "function.name", fcName)
|
||||||
if fcArgsResult := functionCallResult.Get("args"); fcArgsResult.Exists() {
|
if fcArgsResult := functionCallResult.Get("args"); fcArgsResult.Exists() {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
@@ -26,6 +27,9 @@ type Params struct {
|
|||||||
ResponseIndex int
|
ResponseIndex int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// toolUseIDCounter provides a process-wide unique counter for tool use identifiers.
|
||||||
|
var toolUseIDCounter uint64
|
||||||
|
|
||||||
// ConvertGeminiResponseToClaude performs sophisticated streaming response format conversion.
|
// ConvertGeminiResponseToClaude performs sophisticated streaming response format conversion.
|
||||||
// This function implements a complex state machine that translates backend client responses
|
// This function implements a complex state machine that translates backend client responses
|
||||||
// into Claude-compatible Server-Sent Events (SSE) format. It manages different response types
|
// into Claude-compatible Server-Sent Events (SSE) format. It manages different response types
|
||||||
@@ -197,7 +201,7 @@ func ConvertGeminiResponseToClaude(_ context.Context, _ string, originalRequestR
|
|||||||
|
|
||||||
// Create the tool use block with unique ID and function details
|
// Create the tool use block with unique ID and function details
|
||||||
data := fmt.Sprintf(`{"type":"content_block_start","index":%d,"content_block":{"type":"tool_use","id":"","name":"","input":{}}}`, (*param).(*Params).ResponseIndex)
|
data := fmt.Sprintf(`{"type":"content_block_start","index":%d,"content_block":{"type":"tool_use","id":"","name":"","input":{}}}`, (*param).(*Params).ResponseIndex)
|
||||||
data, _ = sjson.Set(data, "content_block.id", fmt.Sprintf("%s-%d", fcName, time.Now().UnixNano()))
|
data, _ = sjson.Set(data, "content_block.id", fmt.Sprintf("%s-%d-%d", fcName, time.Now().UnixNano(), atomic.AddUint64(&toolUseIDCounter, 1)))
|
||||||
data, _ = sjson.Set(data, "content_block.name", fcName)
|
data, _ = sjson.Set(data, "content_block.name", fcName)
|
||||||
output = output + fmt.Sprintf("data: %s\n\n\n", data)
|
output = output + fmt.Sprintf("data: %s\n\n\n", data)
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
@@ -23,6 +24,9 @@ type convertGeminiResponseToOpenAIChatParams struct {
|
|||||||
FunctionIndex int
|
FunctionIndex int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// functionCallIDCounter provides a process-wide unique counter for function call identifiers.
|
||||||
|
var functionCallIDCounter uint64
|
||||||
|
|
||||||
// ConvertGeminiResponseToOpenAI translates a single chunk of a streaming response from the
|
// ConvertGeminiResponseToOpenAI translates a single chunk of a streaming response from the
|
||||||
// Gemini API format to the OpenAI Chat Completions streaming format.
|
// Gemini API format to the OpenAI Chat Completions streaming format.
|
||||||
// It processes various Gemini event types and transforms them into OpenAI-compatible JSON responses.
|
// It processes various Gemini event types and transforms them into OpenAI-compatible JSON responses.
|
||||||
@@ -148,7 +152,7 @@ func ConvertGeminiResponseToOpenAI(_ context.Context, _ string, originalRequestR
|
|||||||
|
|
||||||
functionCallTemplate := `{"id": "","index": 0,"type": "function","function": {"name": "","arguments": ""}}`
|
functionCallTemplate := `{"id": "","index": 0,"type": "function","function": {"name": "","arguments": ""}}`
|
||||||
fcName := functionCallResult.Get("name").String()
|
fcName := functionCallResult.Get("name").String()
|
||||||
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "id", fmt.Sprintf("%s-%d", fcName, time.Now().UnixNano()))
|
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "id", fmt.Sprintf("%s-%d-%d", fcName, time.Now().UnixNano(), atomic.AddUint64(&functionCallIDCounter, 1)))
|
||||||
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "index", functionCallIndex)
|
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "index", functionCallIndex)
|
||||||
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "function.name", fcName)
|
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "function.name", fcName)
|
||||||
if fcArgsResult := functionCallResult.Get("args"); fcArgsResult.Exists() {
|
if fcArgsResult := functionCallResult.Get("args"); fcArgsResult.Exists() {
|
||||||
@@ -281,7 +285,7 @@ func ConvertGeminiResponseToOpenAINonStream(_ context.Context, _ string, origina
|
|||||||
}
|
}
|
||||||
functionCallItemTemplate := `{"id": "","type": "function","function": {"name": "","arguments": ""}}`
|
functionCallItemTemplate := `{"id": "","type": "function","function": {"name": "","arguments": ""}}`
|
||||||
fcName := functionCallResult.Get("name").String()
|
fcName := functionCallResult.Get("name").String()
|
||||||
functionCallItemTemplate, _ = sjson.Set(functionCallItemTemplate, "id", fmt.Sprintf("%s-%d", fcName, time.Now().UnixNano()))
|
functionCallItemTemplate, _ = sjson.Set(functionCallItemTemplate, "id", fmt.Sprintf("%s-%d-%d", fcName, time.Now().UnixNano(), atomic.AddUint64(&functionCallIDCounter, 1)))
|
||||||
functionCallItemTemplate, _ = sjson.Set(functionCallItemTemplate, "function.name", fcName)
|
functionCallItemTemplate, _ = sjson.Set(functionCallItemTemplate, "function.name", fcName)
|
||||||
if fcArgsResult := functionCallResult.Get("args"); fcArgsResult.Exists() {
|
if fcArgsResult := functionCallResult.Get("args"); fcArgsResult.Exists() {
|
||||||
functionCallItemTemplate, _ = sjson.Set(functionCallItemTemplate, "function.arguments", fcArgsResult.Raw)
|
functionCallItemTemplate, _ = sjson.Set(functionCallItemTemplate, "function.arguments", fcArgsResult.Raw)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
@@ -37,6 +38,12 @@ type geminiToResponsesState struct {
|
|||||||
FuncCallIDs map[int]string
|
FuncCallIDs map[int]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// responseIDCounter provides a process-wide unique counter for synthesized response identifiers.
|
||||||
|
var responseIDCounter uint64
|
||||||
|
|
||||||
|
// funcCallIDCounter provides a process-wide unique counter for function call identifiers.
|
||||||
|
var funcCallIDCounter uint64
|
||||||
|
|
||||||
func emitEvent(event string, payload string) string {
|
func emitEvent(event string, payload string) string {
|
||||||
return fmt.Sprintf("event: %s\ndata: %s", event, payload)
|
return fmt.Sprintf("event: %s\ndata: %s", event, payload)
|
||||||
}
|
}
|
||||||
@@ -205,7 +212,7 @@ func ConvertGeminiResponseToOpenAIResponses(_ context.Context, modelName string,
|
|||||||
st.FuncArgsBuf[idx] = &strings.Builder{}
|
st.FuncArgsBuf[idx] = &strings.Builder{}
|
||||||
}
|
}
|
||||||
if st.FuncCallIDs[idx] == "" {
|
if st.FuncCallIDs[idx] == "" {
|
||||||
st.FuncCallIDs[idx] = fmt.Sprintf("call_%d", time.Now().UnixNano())
|
st.FuncCallIDs[idx] = fmt.Sprintf("call_%d_%d", time.Now().UnixNano(), atomic.AddUint64(&funcCallIDCounter, 1))
|
||||||
}
|
}
|
||||||
st.FuncNames[idx] = name
|
st.FuncNames[idx] = name
|
||||||
|
|
||||||
@@ -464,7 +471,7 @@ func ConvertGeminiResponseToOpenAIResponsesNonStream(_ context.Context, _ string
|
|||||||
// id: prefer provider responseId, otherwise synthesize
|
// id: prefer provider responseId, otherwise synthesize
|
||||||
id := root.Get("responseId").String()
|
id := root.Get("responseId").String()
|
||||||
if id == "" {
|
if id == "" {
|
||||||
id = fmt.Sprintf("resp_%x", time.Now().UnixNano())
|
id = fmt.Sprintf("resp_%x_%d", time.Now().UnixNano(), atomic.AddUint64(&responseIDCounter, 1))
|
||||||
}
|
}
|
||||||
// Normalize to response-style id (prefix resp_ if missing)
|
// Normalize to response-style id (prefix resp_ if missing)
|
||||||
if !strings.HasPrefix(id, "resp_") {
|
if !strings.HasPrefix(id, "resp_") {
|
||||||
@@ -575,7 +582,7 @@ func ConvertGeminiResponseToOpenAIResponsesNonStream(_ context.Context, _ string
|
|||||||
if fc := p.Get("functionCall"); fc.Exists() {
|
if fc := p.Get("functionCall"); fc.Exists() {
|
||||||
name := fc.Get("name").String()
|
name := fc.Get("name").String()
|
||||||
args := fc.Get("args")
|
args := fc.Get("args")
|
||||||
callID := fmt.Sprintf("call_%x", time.Now().UnixNano())
|
callID := fmt.Sprintf("call_%x_%d", time.Now().UnixNano(), atomic.AddUint64(&funcCallIDCounter, 1))
|
||||||
outputs = append(outputs, map[string]interface{}{
|
outputs = append(outputs, map[string]interface{}{
|
||||||
"id": fmt.Sprintf("fc_%s", callID),
|
"id": fmt.Sprintf("fc_%s", callID),
|
||||||
"type": "function_call",
|
"type": "function_call",
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
// Package claude provides translation between Kiro and Claude formats.
|
// Package claude provides translation between Kiro and Claude formats.
|
||||||
// Since Kiro uses Claude-compatible format internally, translations are mostly pass-through.
|
// Since Kiro executor generates Claude-compatible SSE format internally (with event: prefix),
|
||||||
|
// translations are pass-through.
|
||||||
package claude
|
package claude
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -14,6 +15,8 @@ func ConvertClaudeRequestToKiro(modelName string, inputRawJSON []byte, stream bo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ConvertKiroResponseToClaude converts Kiro streaming response to Claude format.
|
// ConvertKiroResponseToClaude converts Kiro streaming response to Claude format.
|
||||||
|
// 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 {
|
func ConvertKiroResponseToClaude(ctx context.Context, model string, originalRequest, request, rawResponse []byte, param *any) []string {
|
||||||
return []string{string(rawResponse)}
|
return []string{string(rawResponse)}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ package chat_completions
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -13,15 +14,58 @@ import (
|
|||||||
// ConvertKiroResponseToOpenAI converts Kiro streaming response to OpenAI SSE format.
|
// ConvertKiroResponseToOpenAI converts Kiro streaming response to OpenAI SSE format.
|
||||||
// Handles Claude SSE events: content_block_start, content_block_delta, input_json_delta,
|
// Handles Claude SSE events: content_block_start, content_block_delta, input_json_delta,
|
||||||
// content_block_stop, message_delta, and message_stop.
|
// content_block_stop, message_delta, and message_stop.
|
||||||
|
// Input may be in SSE format: "event: xxx\ndata: {...}" or raw JSON.
|
||||||
func ConvertKiroResponseToOpenAI(ctx context.Context, model string, originalRequest, request, rawResponse []byte, param *any) []string {
|
func ConvertKiroResponseToOpenAI(ctx context.Context, model string, originalRequest, request, rawResponse []byte, param *any) []string {
|
||||||
root := gjson.ParseBytes(rawResponse)
|
raw := string(rawResponse)
|
||||||
|
var results []string
|
||||||
|
|
||||||
|
// Handle SSE format: extract JSON from "data: " lines
|
||||||
|
// Input format: "event: message_start\ndata: {...}"
|
||||||
|
lines := strings.Split(raw, "\n")
|
||||||
|
for _, line := range lines {
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
if strings.HasPrefix(line, "data: ") {
|
||||||
|
jsonPart := strings.TrimPrefix(line, "data: ")
|
||||||
|
chunks := convertClaudeEventToOpenAI(jsonPart, model)
|
||||||
|
results = append(results, chunks...)
|
||||||
|
} else if strings.HasPrefix(line, "{") {
|
||||||
|
// Raw JSON (backward compatibility)
|
||||||
|
chunks := convertClaudeEventToOpenAI(line, model)
|
||||||
|
results = append(results, chunks...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
// convertClaudeEventToOpenAI converts a single Claude JSON event to OpenAI format
|
||||||
|
func convertClaudeEventToOpenAI(jsonStr string, model string) []string {
|
||||||
|
root := gjson.Parse(jsonStr)
|
||||||
var results []string
|
var results []string
|
||||||
|
|
||||||
eventType := root.Get("type").String()
|
eventType := root.Get("type").String()
|
||||||
|
|
||||||
switch eventType {
|
switch eventType {
|
||||||
case "message_start":
|
case "message_start":
|
||||||
// Initial message event - could emit initial chunk if needed
|
// Initial message event - emit initial chunk with role
|
||||||
|
response := map[string]interface{}{
|
||||||
|
"id": "chatcmpl-" + uuid.New().String()[:24],
|
||||||
|
"object": "chat.completion.chunk",
|
||||||
|
"created": time.Now().Unix(),
|
||||||
|
"model": model,
|
||||||
|
"choices": []map[string]interface{}{
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"delta": map[string]interface{}{
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "",
|
||||||
|
},
|
||||||
|
"finish_reason": nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
result, _ := json.Marshal(response)
|
||||||
|
results = append(results, string(result))
|
||||||
return results
|
return results
|
||||||
|
|
||||||
case "content_block_start":
|
case "content_block_start":
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
@@ -41,6 +42,9 @@ type oaiToResponsesState struct {
|
|||||||
UsageSeen bool
|
UsageSeen bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// responseIDCounter provides a process-wide unique counter for synthesized response identifiers.
|
||||||
|
var responseIDCounter uint64
|
||||||
|
|
||||||
func emitRespEvent(event string, payload string) string {
|
func emitRespEvent(event string, payload string) string {
|
||||||
return fmt.Sprintf("event: %s\ndata: %s", event, payload)
|
return fmt.Sprintf("event: %s\ndata: %s", event, payload)
|
||||||
}
|
}
|
||||||
@@ -590,7 +594,7 @@ func ConvertOpenAIChatCompletionsResponseToOpenAIResponsesNonStream(_ context.Co
|
|||||||
// id: use provider id if present, otherwise synthesize
|
// id: use provider id if present, otherwise synthesize
|
||||||
id := root.Get("id").String()
|
id := root.Get("id").String()
|
||||||
if id == "" {
|
if id == "" {
|
||||||
id = fmt.Sprintf("resp_%x", time.Now().UnixNano())
|
id = fmt.Sprintf("resp_%x_%d", time.Now().UnixNano(), atomic.AddUint64(&responseIDCounter, 1))
|
||||||
}
|
}
|
||||||
resp, _ = sjson.Set(resp, "id", id)
|
resp, _ = sjson.Set(resp, "id", id)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user