History

refactor(claude): replace `strings.Builder` with simpler `output` string concatenation

Luis Pater committed 11 months agov6.6.1-1

Commit ef0edbf1 parent 242b4d51 file · +49 −32Browse files

Unified · Split

internal/translator/gemini-cli/claude/gemini-cli_claude_response.go+49−32
@@ -69,12 +69,12 @@ func ConvertGeminiCLIResponseToClaude(_ context.Context, _ string, originalReque · 68 unmodified lines
6969
7070 // Track whether tools are being used in this response chunk
7171 usedTool := false
72− var sb strings.Builder
72+ output := ""
7373
7474 // Initialize the streaming session with a message_start event
7575 // This is only sent for the very first response chunk to establish the streaming session
7676 if !(*param).(*Params).HasFirstResponse {
77− sb.WriteString("event: message_start\n")
77+ output = "event: message_start\n"
7878
7979 // Create the initial message structure with default values according to Claude Code API specification
8080 // This follows the Claude Code API specification for streaming message initialization
@@ -87,7 +87,7 @@ func ConvertGeminiCLIResponseToClaude(_ context.Context, _ string, originalReque · 6 unmodified lines
8787 if responseIDResult := gjson.GetBytes(rawJSON, "response.responseId"); responseIDResult.Exists() {
8888 messageStartTemplate, _ = sjson.Set(messageStartTemplate, "message.id", responseIDResult.String())
8989 }
90− sb.WriteString(fmt.Sprintf("data: %s\n\n\n", messageStartTemplate))
90+ output = output + fmt.Sprintf("data: %s\n\n\n", messageStartTemplate)
9191
9292 (*param).(*Params).HasFirstResponse = true
9393 }
@@ -110,7 +110,7 @@ func ConvertGeminiCLIResponseToClaude(_ context.Context, _ string, originalReque · 16 unmodified lines
110110 if partResult.Get("thought").Bool() {
111111 // Continue existing thinking block if already in thinking state
112112 if (*param).(*Params).ResponseType == 2 {
113− sb.WriteString("event: content_block_delta\n")
113+ output = output + "event: content_block_delta\n"
114114 data, _ := sjson.Set(fmt.Sprintf(`{"type":"content_block_delta","index":%d,"delta":{"type":"thinking_delta","thinking":""}}`, (*param).(*Params).ResponseIndex), "delta.thinking", partTextResult.String())
115115 output = output + fmt.Sprintf("data: %s\n\n\n", data)
116116 (*param).(*Params).HasContent = true
@@ -118,19 +118,24 @@ func ConvertGeminiCLIResponseToClaude(_ context.Context, _ string, originalReque · 1 unmodified line
118118 // Transition from another state to thinking
119119 // First, close any existing content block
120120 if (*param).(*Params).ResponseType != 0 {
121− sb.WriteString("event: content_block_stop\n")
122− sb.WriteString(fmt.Sprintf(`data: {"type":"content_block_stop","index":%d}`, (*param).(*Params).ResponseIndex))
123− sb.WriteString("\n\n\n")
121+ if (*param).(*Params).ResponseType == 2 {
122+ // output = output + "event: content_block_delta\n"
123+ // output = output + fmt.Sprintf(`data: {"type":"content_block_delta","index":%d,"delta":{"type":"signature_delta","signature":null}}`, (*param).(*Params).ResponseIndex)
124+ // output = output + "\n\n\n"
125+ }
126+ output = output + "event: content_block_stop\n"
127+ output = output + fmt.Sprintf(`data: {"type":"content_block_stop","index":%d}`, (*param).(*Params).ResponseIndex)
128+ output = output + "\n\n\n"
124129 (*param).(*Params).ResponseIndex++
125130 }
126131
127132 // Start a new thinking content block
128− sb.WriteString("event: content_block_start\n")
129− sb.WriteString(fmt.Sprintf(`data: {"type":"content_block_start","index":%d,"content_block":{"type":"thinking","thinking":""}}`, (*param).(*Params).ResponseIndex))
130− sb.WriteString("\n\n\n")
131− sb.WriteString("event: content_block_delta\n")
133+ output = output + "event: content_block_start\n"
134+ output = output + fmt.Sprintf(`data: {"type":"content_block_start","index":%d,"content_block":{"type":"thinking","thinking":""}}`, (*param).(*Params).ResponseIndex)
135+ output = output + "\n\n\n"
136+ output = output + "event: content_block_delta\n"
132137 data, _ := sjson.Set(fmt.Sprintf(`{"type":"content_block_delta","index":%d,"delta":{"type":"thinking_delta","thinking":""}}`, (*param).(*Params).ResponseIndex), "delta.thinking", partTextResult.String())
133− sb.WriteString(fmt.Sprintf("data: %s\n\n\n", data))
138+ output = output + fmt.Sprintf("data: %s\n\n\n", data)
134139 (*param).(*Params).ResponseType = 2 // Set state to thinking
135140 (*param).(*Params).HasContent = true
136141 }
@@ -138,7 +143,7 @@ func ConvertGeminiCLIResponseToClaude(_ context.Context, _ string, originalReque · 1 unmodified line
138143 // Process regular text content (user-visible output)
139144 // Continue existing text block if already in content state
140145 if (*param).(*Params).ResponseType == 1 {
141− sb.WriteString("event: content_block_delta\n")
146+ output = output + "event: content_block_delta\n"
142147 data, _ := sjson.Set(fmt.Sprintf(`{"type":"content_block_delta","index":%d,"delta":{"type":"text_delta","text":""}}`, (*param).(*Params).ResponseIndex), "delta.text", partTextResult.String())
143148 output = output + fmt.Sprintf("data: %s\n\n\n", data)
144149 (*param).(*Params).HasContent = true
@@ -146,19 +151,24 @@ func ConvertGeminiCLIResponseToClaude(_ context.Context, _ string, originalReque · 1 unmodified line
146151 // Transition from another state to text content
147152 // First, close any existing content block
148153 if (*param).(*Params).ResponseType != 0 {
149− sb.WriteString("event: content_block_stop\n")
150− sb.WriteString(fmt.Sprintf(`data: {"type":"content_block_stop","index":%d}`, (*param).(*Params).ResponseIndex))
151− sb.WriteString("\n\n\n")
154+ if (*param).(*Params).ResponseType == 2 {
155+ // output = output + "event: content_block_delta\n"
156+ // output = output + fmt.Sprintf(`data: {"type":"content_block_delta","index":%d,"delta":{"type":"signature_delta","signature":null}}`, (*param).(*Params).ResponseIndex)
157+ // output = output + "\n\n\n"
158+ }
159+ output = output + "event: content_block_stop\n"
160+ output = output + fmt.Sprintf(`data: {"type":"content_block_stop","index":%d}`, (*param).(*Params).ResponseIndex)
161+ output = output + "\n\n\n"
152162 (*param).(*Params).ResponseIndex++
153163 }
154164
155165 // Start a new text content block
156− sb.WriteString("event: content_block_start\n")
157− sb.WriteString(fmt.Sprintf(`data: {"type":"content_block_start","index":%d,"content_block":{"type":"text","text":""}}`, (*param).(*Params).ResponseIndex))
158− sb.WriteString("\n\n\n")
159− sb.WriteString("event: content_block_delta\n")
166+ output = output + "event: content_block_start\n"
167+ output = output + fmt.Sprintf(`data: {"type":"content_block_start","index":%d,"content_block":{"type":"text","text":""}}`, (*param).(*Params).ResponseIndex)
168+ output = output + "\n\n\n"
169+ output = output + "event: content_block_delta\n"
160170 data, _ := sjson.Set(fmt.Sprintf(`{"type":"content_block_delta","index":%d,"delta":{"type":"text_delta","text":""}}`, (*param).(*Params).ResponseIndex), "delta.text", partTextResult.String())
161− sb.WriteString(fmt.Sprintf("data: %s\n\n\n", data))
171+ output = output + fmt.Sprintf("data: %s\n\n\n", data)
162172 (*param).(*Params).ResponseType = 1 // Set state to content
163173 (*param).(*Params).HasContent = true
164174 }
@@ -172,35 +182,42 @@ func ConvertGeminiCLIResponseToClaude(_ context.Context, _ string, originalReque · 7 unmodified lines
172182 // Handle state transitions when switching to function calls
173183 // Close any existing function call block first
174184 if (*param).(*Params).ResponseType == 3 {
175− sb.WriteString("event: content_block_stop\n")
176− sb.WriteString(fmt.Sprintf(`data: {"type":"content_block_stop","index":%d}`, (*param).(*Params).ResponseIndex))
177− sb.WriteString("\n\n\n")
185+ output = output + "event: content_block_stop\n"
186+ output = output + fmt.Sprintf(`data: {"type":"content_block_stop","index":%d}`, (*param).(*Params).ResponseIndex)
187+ output = output + "\n\n\n"
178188 (*param).(*Params).ResponseIndex++
179189 (*param).(*Params).ResponseType = 0
180190 }
181191
192+ // Special handling for thinking state transition
193+ if (*param).(*Params).ResponseType == 2 {
194+ // output = output + "event: content_block_delta\n"
195+ // output = output + fmt.Sprintf(`data: {"type":"content_block_delta","index":%d,"delta":{"type":"signature_delta","signature":null}}`, (*param).(*Params).ResponseIndex)
196+ // output = output + "\n\n\n"
197+ }
198+
182199 // Close any other existing content block
183200 if (*param).(*Params).ResponseType != 0 {
184− sb.WriteString("event: content_block_stop\n")
185− sb.WriteString(fmt.Sprintf(`data: {"type":"content_block_stop","index":%d}`, (*param).(*Params).ResponseIndex))
186− sb.WriteString("\n\n\n")
201+ output = output + "event: content_block_stop\n"
202+ output = output + fmt.Sprintf(`data: {"type":"content_block_stop","index":%d}`, (*param).(*Params).ResponseIndex)
203+ output = output + "\n\n\n"
187204 (*param).(*Params).ResponseIndex++
188205 }
189206
190207 // Start a new tool use content block
191208 // This creates the structure for a function call in Claude Code format
192− sb.WriteString("event: content_block_start\n")
209+ output = output + "event: content_block_start\n"
193210
194211 // Create the tool use block with unique ID and function details
195212 data := fmt.Sprintf(`{"type":"content_block_start","index":%d,"content_block":{"type":"tool_use","id":"","name":"","input":{}}}`, (*param).(*Params).ResponseIndex)
196213 data, _ = sjson.Set(data, "content_block.id", fmt.Sprintf("%s-%d-%d", fcName, time.Now().UnixNano(), atomic.AddUint64(&toolUseIDCounter, 1)))
197214 data, _ = sjson.Set(data, "content_block.name", fcName)
198− sb.WriteString(fmt.Sprintf("data: %s\n\n\n", data))
215+ output = output + fmt.Sprintf("data: %s\n\n\n", data)
199216
200217 if fcArgsResult := functionCallResult.Get("args"); fcArgsResult.Exists() {
201− sb.WriteString("event: content_block_delta\n")
218+ output = output + "event: content_block_delta\n"
202219 data, _ = sjson.Set(fmt.Sprintf(`{"type":"content_block_delta","index":%d,"delta":{"type":"input_json_delta","partial_json":""}}`, (*param).(*Params).ResponseIndex), "delta.partial_json", fcArgsResult.Raw)
203− sb.WriteString(fmt.Sprintf("data: %s\n\n\n", data))
220+ output = output + fmt.Sprintf("data: %s\n\n\n", data)
204221 }
205222 (*param).(*Params).ResponseType = 3
206223 (*param).(*Params).HasContent = true
@@ -240,7 +257,7 @@ func ConvertGeminiCLIResponseToClaude(_ context.Context, _ string, originalReque · 33 unmodified lines
240257 }
241258 }
242259
243− return []string{sb.String()}
260+ return []string{output}
244261 }
245262
246263 // ConvertGeminiCLIResponseToClaudeNonStream converts a non-streaming Gemini CLI response to a non-streaming Claude response.