Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
e19e9ad2
authored
Dec 01, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(gemini): implement markdown image handling in text processing
parent
b1f2fac8
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
0 deletions
+59
-0
relay/channel/gemini/relay-gemini.go
+59
-0
No files found.
relay/channel/gemini/relay-gemini.go
View file @
e19e9ad2
...
...
@@ -446,9 +446,68 @@ func CovertOpenAI2Gemini(c *gin.Context, textRequest dto.GeneralOpenAIRequest, i
if
part
.
Text
==
""
{
continue
}
// check markdown image 
// 使用字符串查找而非正则,避免大文本性能问题
text
:=
part
.
Text
hasMarkdownImage
:=
false
for
{
// 快速检查是否包含 markdown 图片标记
startIdx
:=
strings
.
Index
(
text
,
"
if
bracketIdx
==
-
1
{
break
}
bracketIdx
+=
startIdx
// 找到闭合的 )
closeIdx
:=
strings
.
Index
(
text
[
bracketIdx
+
2
:
],
")"
)
if
closeIdx
==
-
1
{
break
}
closeIdx
+=
bracketIdx
+
2
hasMarkdownImage
=
true
// 添加图片前的文本
if
startIdx
>
0
{
textBefore
:=
text
[
:
startIdx
]
if
textBefore
!=
""
{
parts
=
append
(
parts
,
dto
.
GeminiPart
{
Text
:
textBefore
,
})
}
}
// 提取 data URL (从 "](" 后面开始,到 ")" 之前)
dataUrl
:=
text
[
bracketIdx
+
2
:
closeIdx
]
imageNum
+=
1
if
constant
.
GeminiVisionMaxImageNum
!=
-
1
&&
imageNum
>
constant
.
GeminiVisionMaxImageNum
{
return
nil
,
fmt
.
Errorf
(
"too many images in the message, max allowed is %d"
,
constant
.
GeminiVisionMaxImageNum
)
}
format
,
base64String
,
err
:=
service
.
DecodeBase64FileData
(
dataUrl
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"decode markdown base64 image data failed: %s"
,
err
.
Error
())
}
imgPart
:=
dto
.
GeminiPart
{
InlineData
:
&
dto
.
GeminiInlineData
{
MimeType
:
format
,
Data
:
base64String
,
},
}
if
shouldAttachThoughtSignature
{
imgPart
.
ThoughtSignature
=
json
.
RawMessage
(
strconv
.
Quote
(
thoughtSignatureBypassValue
))
}
parts
=
append
(
parts
,
imgPart
)
// 继续处理剩余文本
text
=
text
[
closeIdx
+
1
:
]
}
// 添加剩余文本或原始文本(如果没有找到 markdown 图片)
if
!
hasMarkdownImage
{
parts
=
append
(
parts
,
dto
.
GeminiPart
{
Text
:
part
.
Text
,
})
}
}
else
if
part
.
Type
==
dto
.
ContentTypeImageURL
{
imageNum
+=
1
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment