feat(gemini): map OpenAI stop to Gemini stopSequences - #2779
Conversation
WalkthroughAdds support for parsing and applying stop sequences when converting OpenAI-format requests to Gemini requests. Introduces a new Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@relay/channel/gemini/relay-gemini.go`:
- Around line 641-664: The parseStopSequences function currently returns
[]string unchanged for the []string case while filtering out empty strings for
[]interface{}; update the []string branch in parseStopSequences to iterate over
the input slice and append only non-empty strings (like the []interface{}
branch) so empty entries are consistently removed before returning; keep the
existing nil handling and preserve the function signature and behavior for
string and other cases.
| // parseStopSequences 解析停止序列,支持字符串或字符串数组 | ||
| func parseStopSequences(stop any) []string { | ||
| if stop == nil { | ||
| return nil | ||
| } | ||
|
|
||
| switch v := stop.(type) { | ||
| case string: | ||
| if v != "" { | ||
| return []string{v} | ||
| } | ||
| case []string: | ||
| return v | ||
| case []interface{}: | ||
| sequences := make([]string, 0, len(v)) | ||
| for _, item := range v { | ||
| if str, ok := item.(string); ok && str != "" { | ||
| sequences = append(sequences, str) | ||
| } | ||
| } | ||
| return sequences | ||
| } | ||
| return nil | ||
| } |
There was a problem hiding this comment.
Inconsistent empty-string filtering between []string and []interface{} cases.
The []interface{} case (lines 654-661) filters out empty strings, but the []string case (lines 652-653) returns the slice directly without any filtering. This inconsistency could allow empty stop sequences to reach the Gemini API when the input is []string.
🔧 Proposed fix to add filtering for []string
case []string:
- return v
+ sequences := make([]string, 0, len(v))
+ for _, s := range v {
+ if s != "" {
+ sequences = append(sequences, s)
+ }
+ }
+ return sequences
case []interface{}:📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // parseStopSequences 解析停止序列,支持字符串或字符串数组 | |
| func parseStopSequences(stop any) []string { | |
| if stop == nil { | |
| return nil | |
| } | |
| switch v := stop.(type) { | |
| case string: | |
| if v != "" { | |
| return []string{v} | |
| } | |
| case []string: | |
| return v | |
| case []interface{}: | |
| sequences := make([]string, 0, len(v)) | |
| for _, item := range v { | |
| if str, ok := item.(string); ok && str != "" { | |
| sequences = append(sequences, str) | |
| } | |
| } | |
| return sequences | |
| } | |
| return nil | |
| } | |
| // parseStopSequences 解析停止序列,支持字符串或字符串数组 | |
| func parseStopSequences(stop any) []string { | |
| if stop == nil { | |
| return nil | |
| } | |
| switch v := stop.(type) { | |
| case string: | |
| if v != "" { | |
| return []string{v} | |
| } | |
| case []string: | |
| sequences := make([]string, 0, len(v)) | |
| for _, s := range v { | |
| if s != "" { | |
| sequences = append(sequences, s) | |
| } | |
| } | |
| return sequences | |
| case []interface{}: | |
| sequences := make([]string, 0, len(v)) | |
| for _, item := range v { | |
| if str, ok := item.(string); ok && str != "" { | |
| sequences = append(sequences, str) | |
| } | |
| } | |
| return sequences | |
| } | |
| return nil | |
| } |
🤖 Prompt for AI Agents
In `@relay/channel/gemini/relay-gemini.go` around lines 641 - 664, The
parseStopSequences function currently returns []string unchanged for the
[]string case while filtering out empty strings for []interface{}; update the
[]string branch in parseStopSequences to iterate over the input slice and append
only non-empty strings (like the []interface{} branch) so empty entries are
consistently removed before returning; keep the existing nil handling and
preserve the function signature and behavior for string and other cases.
feat(gemini): map OpenAI stop to Gemini stopSequences
PR 类型
PR 是否包含破坏性更新?
PR 描述
close #2777

将 OpenAI 的 stop 参数转换为 Gemini 的 stopSequences,确保停止序列在 Gemini 渠道生效。
实现细节:
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.