Merge pull request #2 from cyhhao/fix/claude-oauth-compat
Fix/claude oauth compat
This commit is contained in:
@@ -3463,9 +3463,15 @@ func (s *GatewayService) handleStreamingResponse(ctx context.Context, resp *http
|
|||||||
clientDisconnected := false // 客户端断开标志,断开后继续读取上游以获取完整usage
|
clientDisconnected := false // 客户端断开标志,断开后继续读取上游以获取完整usage
|
||||||
|
|
||||||
pendingEventLines := make([]string, 0, 4)
|
pendingEventLines := make([]string, 0, 4)
|
||||||
toolInputBuffers := make(map[int]string)
|
var toolInputBuffers map[int]string
|
||||||
|
if mimicClaudeCode {
|
||||||
|
toolInputBuffers = make(map[int]string)
|
||||||
|
}
|
||||||
|
|
||||||
transformToolInputJSON := func(raw string) string {
|
transformToolInputJSON := func(raw string) string {
|
||||||
|
if !mimicClaudeCode {
|
||||||
|
return raw
|
||||||
|
}
|
||||||
raw = strings.TrimSpace(raw)
|
raw = strings.TrimSpace(raw)
|
||||||
if raw == "" {
|
if raw == "" {
|
||||||
return raw
|
return raw
|
||||||
@@ -3522,7 +3528,10 @@ func (s *GatewayService) handleStreamingResponse(ctx context.Context, resp *http
|
|||||||
|
|
||||||
var event map[string]any
|
var event map[string]any
|
||||||
if err := json.Unmarshal([]byte(dataLine), &event); err != nil {
|
if err := json.Unmarshal([]byte(dataLine), &event); err != nil {
|
||||||
replaced := replaceToolNamesInText(dataLine, toolNameMap)
|
replaced := dataLine
|
||||||
|
if mimicClaudeCode {
|
||||||
|
replaced = replaceToolNamesInText(dataLine, toolNameMap)
|
||||||
|
}
|
||||||
block := ""
|
block := ""
|
||||||
if eventName != "" {
|
if eventName != "" {
|
||||||
block = "event: " + eventName + "\n"
|
block = "event: " + eventName + "\n"
|
||||||
@@ -3544,7 +3553,7 @@ func (s *GatewayService) handleStreamingResponse(ctx context.Context, resp *http
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if eventType == "content_block_delta" {
|
if mimicClaudeCode && eventType == "content_block_delta" {
|
||||||
if delta, ok := event["delta"].(map[string]any); ok {
|
if delta, ok := event["delta"].(map[string]any); ok {
|
||||||
if deltaType, _ := delta["type"].(string); deltaType == "input_json_delta" {
|
if deltaType, _ := delta["type"].(string); deltaType == "input_json_delta" {
|
||||||
if indexVal, ok := event["index"].(float64); ok {
|
if indexVal, ok := event["index"].(float64); ok {
|
||||||
@@ -3558,7 +3567,7 @@ func (s *GatewayService) handleStreamingResponse(ctx context.Context, resp *http
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if eventType == "content_block_stop" {
|
if mimicClaudeCode && eventType == "content_block_stop" {
|
||||||
if indexVal, ok := event["index"].(float64); ok {
|
if indexVal, ok := event["index"].(float64); ok {
|
||||||
index := int(indexVal)
|
index := int(indexVal)
|
||||||
if buffered := toolInputBuffers[index]; buffered != "" {
|
if buffered := toolInputBuffers[index]; buffered != "" {
|
||||||
@@ -3593,10 +3602,15 @@ func (s *GatewayService) handleStreamingResponse(ctx context.Context, resp *http
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rewriteToolNamesInValue(event, toolNameMap)
|
if mimicClaudeCode {
|
||||||
|
rewriteToolNamesInValue(event, toolNameMap)
|
||||||
|
}
|
||||||
newData, err := json.Marshal(event)
|
newData, err := json.Marshal(event)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
replaced := replaceToolNamesInText(dataLine, toolNameMap)
|
replaced := dataLine
|
||||||
|
if mimicClaudeCode {
|
||||||
|
replaced = replaceToolNamesInText(dataLine, toolNameMap)
|
||||||
|
}
|
||||||
block := ""
|
block := ""
|
||||||
if eventName != "" {
|
if eventName != "" {
|
||||||
block = "event: " + eventName + "\n"
|
block = "event: " + eventName + "\n"
|
||||||
|
|||||||
Reference in New Issue
Block a user