From e4e958bc24f63e297329e3720c46a26e597a340a Mon Sep 17 00:00:00 2001 From: Scream034 Date: Fri, 27 Nov 2026 12:47:06 +0500 Subject: [PATCH 1/2] fix: drop unsupported inline media in assistant history --- internal/aistudio/service.go | 2 +- internal/aistudio/turns.go | 53 ++++++++++++++++++++++++++++++++++-- internal/app/runtime.go | 1 + 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/internal/aistudio/service.go b/internal/aistudio/service.go index 4adef0a3..59e0a419 100644 --- a/internal/aistudio/service.go +++ b/internal/aistudio/service.go @@ -846,7 +846,7 @@ func (s *PooledService) CountTokens(ctx context.Context, request TokenCountReque } opts := s.requestOptions() request.System = clientSystem(request.System) - request.Contents = dropUserRejected(request.Contents) + request.Contents = DropUnsupportedAssistantMedia(dropUserRejected(request.Contents)) if opts.YouTubeLinks { request.Contents = attachYouTubeLinks(request.Contents) } diff --git a/internal/aistudio/turns.go b/internal/aistudio/turns.go index 4c1c0fae..cd460391 100644 --- a/internal/aistudio/turns.go +++ b/internal/aistudio/turns.go @@ -1,6 +1,7 @@ package aistudio import ( + "mime" "slices" "strings" ) @@ -28,10 +29,58 @@ func shouldRewriteAssistantTail(modelID string, rewrite bool) bool { return rewrite && !nativePrefillModels[modelID] && !strings.HasPrefix(modelID, "gemma-") } -// normalizeTurns 按 clientSystem 改写客户端系统提示,按 dropUserRejected 删去 user 轮中的客户端身份句;在 rewrite 时改写末尾连续 assistant 轮:prompt 非空时正文作为续写前缀连同 prompt 写入系统指令,为空时正文作为最后一轮 user;没有对话内容时以系统提示作为 user 轮 +// supportedAssistantMediaMIME 判断模型历史轮中的内联媒体 MIME 属于上游可接收的媒体类型 +func supportedAssistantMediaMIME(mimeType string) bool { + mediaType, _, err := mime.ParseMediaType(mimeType) + if err != nil { + return false + } + return strings.HasPrefix(mediaType, "text/") || + strings.HasPrefix(mediaType, "image/") || + strings.HasPrefix(mediaType, "audio/") || + strings.HasPrefix(mediaType, "video/") || + mediaType == "application/pdf" || + mediaType == "application/json" || + mediaType == "application/x-ipynb+json" +} + +// DropUnsupportedAssistantMedia 过滤模型历史轮中上游不支持的内联媒体,保留该片段携带的思考签名 +func DropUnsupportedAssistantMedia(contents []Content) []Content { + if !slices.ContainsFunc(contents, func(content Content) bool { + return content.Role == RoleAssistant && slices.ContainsFunc(content.Parts, func(part Part) bool { + return part.InlineData != nil && !supportedAssistantMediaMIME(part.InlineData.MIME) + }) + }) { + return contents + } + filtered := make([]Content, 0, len(contents)) + for _, content := range contents { + if content.Role != RoleAssistant { + filtered = append(filtered, content) + continue + } + parts := make([]Part, 0, len(content.Parts)) + for _, part := range content.Parts { + if part.InlineData != nil && !supportedAssistantMediaMIME(part.InlineData.MIME) { + if part.ThoughtSignature != "" { + parts = append(parts, Part{ThoughtSignature: part.ThoughtSignature}) + } + continue + } + parts = append(parts, part) + } + if len(parts) > 0 { + content.Parts = parts + filtered = append(filtered, content) + } + } + return filtered +} + +// normalizeTurns 按 clientSystem 改写客户端系统提示,按 dropUserRejected 删去 user 轮中的客户端身份句,按 DropUnsupportedAssistantMedia 过滤模型历史轮中不支持的内联媒体;在 rewrite 时改写末尾连续 assistant 轮:prompt 非空时正文作为续写前缀连同 prompt 写入系统指令,为空时正文作为最后一轮 user;没有对话内容时以系统提示作为 user 轮 func normalizeTurns(system []string, contents []Content, rewrite bool, prompt string) ([]string, []Content) { system = clientSystem(system) - contents = dropUserRejected(contents) + contents = DropUnsupportedAssistantMedia(dropUserRejected(contents)) if rewrite { tail := len(contents) for tail > 0 && contents[tail-1].Role == RoleAssistant { diff --git a/internal/app/runtime.go b/internal/app/runtime.go index bf088245..a8b53f0e 100644 --- a/internal/app/runtime.go +++ b/internal/app/runtime.go @@ -2363,6 +2363,7 @@ func (service *trackedService) Generate(ctx context.Context, request aistudio.Ge generationStartedAt := time.Now() model, config, suffixErr := aistudio.ApplyModelSuffix(service.mappedModel(request.Model), request.Config) request.Model, request.Config = model, config + request.Contents = aistudio.DropUnsupportedAssistantMedia(request.Contents) api.SetAccessLogTarget(ctx, request.Model, "") api.SetAccessLogGenerationConfig(ctx, request.Config) api.SetAccessLogGenerationInput(ctx, request) From 92c47fbeed5069e2256c7983bcf281be5e7dfa41 Mon Sep 17 00:00:00 2001 From: Mag1cFall Date: Mon, 30 Nov 2026 01:02:45 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E8=A1=A5=E9=BD=90=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E5=86=85=E8=81=94=E5=AA=92=E4=BD=93=E4=BF=9D?= =?UTF-8?q?=E7=95=99=E8=8C=83=E5=9B=B4=E4=B8=8E=E8=AE=A1=E6=95=B0=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E5=AF=B9=E9=BD=90=20-=20=E8=A1=A5=E9=BD=90=E4=B8=8A?= =?UTF-8?q?=E6=B8=B8=E7=9B=B4=E6=8E=A5=E6=8E=A5=E5=8F=97=E7=9A=84=E5=BD=92?= =?UTF-8?q?=E6=A1=A3=E3=80=81=E5=8A=9E=E5=85=AC=E6=96=87=E6=A1=A3=E4=B8=8E?= =?UTF-8?q?=20WAV=20=E5=88=AB=E5=90=8D=E7=B1=BB=E5=9E=8B=EF=BC=8C=E6=8C=89?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E5=90=8E=E7=9A=84=E5=9F=BA=E7=A1=80=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=88=A4=E5=AE=9A=E5=B9=B6=E5=8E=9F=E6=A0=B7=E4=BF=9D?= =?UTF-8?q?=E7=95=99=E6=96=87=E6=9C=AC=E4=B8=8E=E8=A3=B8=E9=9F=B3=E9=A2=91?= =?UTF-8?q?=E5=8F=82=E6=95=B0=20-=20=E5=9C=A8=E8=AE=A1=E6=95=B0=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E4=BF=9D=E7=95=99=E8=BF=87=E6=BB=A4=E5=89=8D=E5=90=8E?= =?UTF-8?q?=E7=9A=84=E8=BD=AE=E6=AC=A1=E4=B8=8E=E7=89=87=E6=AE=B5=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E6=98=A0=E5=B0=84=EF=BC=8C=E8=BF=94=E5=9B=9E=E6=97=B6?= =?UTF-8?q?=E4=B8=BA=E4=B8=A2=E5=BC=83=E7=9A=84=E7=89=87=E6=AE=B5=E4=B8=8E?= =?UTF-8?q?=E7=A9=BA=E6=A8=A1=E5=9E=8B=E8=BD=AE=E8=A1=A5=E9=9B=B6=E5=AF=B9?= =?UTF-8?q?=E9=BD=90=20-=20=E5=B0=86=E7=94=9F=E6=88=90=E5=85=A5=E5=8F=A3?= =?UTF-8?q?=E7=9A=84=E8=BF=87=E6=BB=A4=E7=A7=BB=E8=87=B3=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E8=B4=A6=E6=9C=AC=E8=AE=B0=E5=BD=95=E4=B9=8B=E5=90=8E=E3=80=81?= =?UTF-8?q?=E9=A2=84=E6=A0=A1=E9=AA=8C=E4=B8=8E=E4=B8=8A=E4=BC=A0=E4=B9=8B?= =?UTF-8?q?=E5=89=8D=EF=BC=8C=E4=BF=9D=E6=8C=81=E5=8E=9F=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=91=98=E8=A6=81=E5=B9=B6=E9=81=BF=E5=85=8D=E6=97=A0=E6=95=88?= =?UTF-8?q?=E9=99=84=E4=BB=B6=E8=BF=9B=E5=85=A5=E4=B8=8A=E4=BC=A0=E9=98=B6?= =?UTF-8?q?=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/aistudio/service.go | 7 +- internal/aistudio/turns.go | 202 +++++++++++++++++++++++++++++------ internal/app/runtime.go | 2 +- 3 files changed, 179 insertions(+), 32 deletions(-) diff --git a/internal/aistudio/service.go b/internal/aistudio/service.go index 59e0a419..2a7bf475 100644 --- a/internal/aistudio/service.go +++ b/internal/aistudio/service.go @@ -846,13 +846,15 @@ func (s *PooledService) CountTokens(ctx context.Context, request TokenCountReque } opts := s.requestOptions() request.System = clientSystem(request.System) - request.Contents = DropUnsupportedAssistantMedia(dropUserRejected(request.Contents)) + request.Contents = dropUserRejected(request.Contents) if opts.YouTubeLinks { request.Contents = attachYouTubeLinks(request.Contents) } if opts.StripThoughtSignatures { request.Contents = stripThoughtSignatures(request.Contents) } + var droppedMedia *assistantMediaCountMap + request.Contents, droppedMedia = dropUnsupportedAssistantMedia(request.Contents, true) systemOnly := len(request.Contents) == 0 if systemOnly { if strings.TrimSpace(SystemText(request.System)) == "" { @@ -871,6 +873,9 @@ func (s *PooledService) CountTokens(ctx context.Context, request TokenCountReque if systemOnly && len(count.Contents) == 1 { count = systemOnlyCount(count) } + if err == nil && droppedMedia != nil { + count.Contents = droppedMedia.restore(count.Contents, count.System != nil) + } return count, err } diff --git a/internal/aistudio/turns.go b/internal/aistudio/turns.go index cd460391..c7ebf628 100644 --- a/internal/aistudio/turns.go +++ b/internal/aistudio/turns.go @@ -29,55 +29,197 @@ func shouldRewriteAssistantTail(modelID string, rewrite bool) bool { return rewrite && !nativePrefillModels[modelID] && !strings.HasPrefix(modelID, "gemma-") } -// supportedAssistantMediaMIME 判断模型历史轮中的内联媒体 MIME 属于上游可接收的媒体类型 +// upstreamAssistantMediaMIME 是上游 GenerateContent(含 Drive 上传)直接接受的非 text/* 模型历史内联媒体类型 +var upstreamAssistantMediaMIME = map[string]bool{ + "application/json": true, + "application/x-ipynb+json": true, + "application/pdf": true, + "application/zip": true, + "application/x-zip": true, + "application/x-zip-compressed": true, + "application/hwp": true, + "application/hwp+zip": true, + "application/msword": true, + "application/vnd.ms-excel": true, + "application/vnd.ms-powerpoint": true, + "application/vnd.oasis.opendocument.text": true, + "application/vnd.oasis.opendocument.spreadsheet": true, + "application/vnd.oasis.opendocument.presentation": true, + "application/vnd.openxmlformats-officedocument.wordprocessingml.document": true, + "application/vnd.openxmlformats-officedocument.wordprocessingml.template": true, + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": true, + "application/vnd.openxmlformats-officedocument.presentationml.presentation": true, + "image/png": true, + "image/jpeg": true, + "image/webp": true, + "image/heic": true, + "image/heif": true, + "audio/aac": true, + "audio/aiff": true, + "audio/alaw": true, + "audio/flac": true, + "audio/l16": true, + "audio/m4a": true, + "audio/mp3": true, + "audio/mp4": true, + "audio/mpeg": true, + "audio/mulaw": true, + "audio/ogg": true, + "audio/opus": true, + "audio/pcm": true, + "audio/s16le": true, + "audio/wav": true, + "audio/wave": true, + "audio/x-wav": true, + "audio/webm": true, + "video/mp4": true, + "video/mpeg": true, + "video/mov": true, + "video/quicktime": true, + "video/avi": true, + "video/x-flv": true, + "video/mpg": true, + "video/webm": true, + "video/wmv": true, + "video/3gpp": true, +} + +// supportedAssistantMediaMIME 判断声明的媒体类型是否属于上游接受的模型历史内联媒体类型 func supportedAssistantMediaMIME(mimeType string) bool { mediaType, _, err := mime.ParseMediaType(mimeType) if err != nil { return false } - return strings.HasPrefix(mediaType, "text/") || - strings.HasPrefix(mediaType, "image/") || - strings.HasPrefix(mediaType, "audio/") || - strings.HasPrefix(mediaType, "video/") || - mediaType == "application/pdf" || - mediaType == "application/json" || - mediaType == "application/x-ipynb+json" + return strings.HasPrefix(mediaType, "text/") || upstreamAssistantMediaMIME[mediaType] } -// DropUnsupportedAssistantMedia 过滤模型历史轮中上游不支持的内联媒体,保留该片段携带的思考签名 -func DropUnsupportedAssistantMedia(contents []Content) []Content { - if !slices.ContainsFunc(contents, func(content Content) bool { - return content.Role == RoleAssistant && slices.ContainsFunc(content.Parts, func(part Part) bool { - return part.InlineData != nil && !supportedAssistantMediaMIME(part.InlineData.MIME) - }) - }) { - return contents +// assistantMediaTurnMap 记录单个原非空轮次在丢弃不支持媒体后的保留情况 +type assistantMediaTurnMap struct { + origParts int + kept []int + dropped bool +} + +// assistantMediaCountMap 记录 CountTokens 过滤模型历史不支持媒体前后的轮次与片段对应关系,用于把返回的逐项计数还原到原位置 +type assistantMediaCountMap struct { + turns []assistantMediaTurnMap + sentTurns int +} + +// restore 按过滤前的非空轮次与片段下标还原 CountTokens 返回的逐轮明细,被丢弃的片段与整轮补零 +func (m *assistantMediaCountMap) restore(got []ContentTokens, hasSystem bool) []ContentTokens { + if m == nil || len(got) != m.sentTurns || (m.sentTurns == 0 && !hasSystem) { + return got } - filtered := make([]Content, 0, len(contents)) - for _, content := range contents { + restored := make([]ContentTokens, len(m.turns)) + sent := 0 + for index, turn := range m.turns { + if turn.dropped { + restored[index] = ContentTokens{PartTokens: make([]int64, turn.origParts)} + continue + } + entry := got[sent] + sent++ + if turn.kept == nil || len(entry.PartTokens) != len(turn.kept) { + restored[index] = entry + continue + } + parts := make([]int64, turn.origParts) + for keptIndex, origIndex := range turn.kept { + parts[origIndex] = entry.PartTokens[keptIndex] + } + restored[index] = ContentTokens{PartTokens: parts, RoleTokens: entry.RoleTokens} + } + return restored +} + +// DropUnsupportedAssistantMedia 丢弃模型历史轮中上游不接受的内联媒体,保留思考签名,清空后的模型轮不发送 +func DropUnsupportedAssistantMedia(contents []Content) []Content { + filtered, _ := dropUnsupportedAssistantMedia(contents, false) + return filtered +} + +func dropUnsupportedAssistantMedia(contents []Content, trackCount bool) ([]Content, *assistantMediaCountMap) { + var filtered []Content + var keptByTurn map[int][]int + for index, content := range contents { if content.Role != RoleAssistant { - filtered = append(filtered, content) continue } - parts := make([]Part, 0, len(content.Parts)) - for _, part := range content.Parts { - if part.InlineData != nil && !supportedAssistantMediaMIME(part.InlineData.MIME) { - if part.ThoughtSignature != "" { - parts = append(parts, Part{ThoughtSignature: part.ThoughtSignature}) + var parts []Part + var kept []int + for partIndex, part := range content.Parts { + blob := part.InlineData + if blob == nil || (len(blob.Data) > 0 && supportedAssistantMediaMIME(blob.MIME)) { + if parts != nil { + parts = append(parts, part) + if trackCount { + kept = append(kept, partIndex) + } } continue } - parts = append(parts, part) + if parts == nil { + parts = make([]Part, partIndex, len(content.Parts)) + copy(parts, content.Parts[:partIndex]) + if trackCount { + kept = make([]int, partIndex, len(content.Parts)) + for i := range partIndex { + kept[i] = i + } + } + } + if part.ThoughtSignature != "" { + parts = append(parts, Part{ThoughtSignature: part.ThoughtSignature}) + if trackCount { + kept = append(kept, partIndex) + } + } } - if len(parts) > 0 { - content.Parts = parts - filtered = append(filtered, content) + if parts == nil { + continue + } + if filtered == nil { + filtered = slices.Clone(contents) + } + filtered[index].Parts = parts + if trackCount && len(kept) != len(content.Parts) { + if keptByTurn == nil { + keptByTurn = make(map[int][]int) + } + keptByTurn[index] = kept } } - return filtered + if filtered == nil { + return contents, nil + } + var countMap *assistantMediaCountMap + if trackCount && len(keptByTurn) > 0 { + countMap = &assistantMediaCountMap{turns: make([]assistantMediaTurnMap, 0, len(contents))} + for index, content := range contents { + if len(content.Parts) == 0 { + continue + } + kept, changed := keptByTurn[index] + if !changed { + countMap.turns = append(countMap.turns, assistantMediaTurnMap{origParts: len(content.Parts)}) + countMap.sentTurns++ + continue + } + if len(kept) == 0 { + countMap.turns = append(countMap.turns, assistantMediaTurnMap{origParts: len(content.Parts), dropped: true}) + continue + } + countMap.turns = append(countMap.turns, assistantMediaTurnMap{origParts: len(content.Parts), kept: kept}) + countMap.sentTurns++ + } + } + return slices.DeleteFunc(filtered, func(content Content) bool { + return content.Role == RoleAssistant && len(content.Parts) == 0 + }), countMap } -// normalizeTurns 按 clientSystem 改写客户端系统提示,按 dropUserRejected 删去 user 轮中的客户端身份句,按 DropUnsupportedAssistantMedia 过滤模型历史轮中不支持的内联媒体;在 rewrite 时改写末尾连续 assistant 轮:prompt 非空时正文作为续写前缀连同 prompt 写入系统指令,为空时正文作为最后一轮 user;没有对话内容时以系统提示作为 user 轮 +// normalizeTurns 按 clientSystem 改写客户端系统提示,按 dropUserRejected 删去 user 轮中的客户端身份句,按 DropUnsupportedAssistantMedia 丢弃模型历史轮中上游不接受的内联媒体;在 rewrite 时改写末尾连续 assistant 轮:prompt 非空时正文作为续写前缀连同 prompt 写入系统指令,为空时正文作为最后一轮 user;没有对话内容时以系统提示作为 user 轮 func normalizeTurns(system []string, contents []Content, rewrite bool, prompt string) ([]string, []Content) { system = clientSystem(system) contents = DropUnsupportedAssistantMedia(dropUserRejected(contents)) diff --git a/internal/app/runtime.go b/internal/app/runtime.go index a8b53f0e..328bf3c2 100644 --- a/internal/app/runtime.go +++ b/internal/app/runtime.go @@ -2363,11 +2363,11 @@ func (service *trackedService) Generate(ctx context.Context, request aistudio.Ge generationStartedAt := time.Now() model, config, suffixErr := aistudio.ApplyModelSuffix(service.mappedModel(request.Model), request.Config) request.Model, request.Config = model, config - request.Contents = aistudio.DropUnsupportedAssistantMedia(request.Contents) api.SetAccessLogTarget(ctx, request.Model, "") api.SetAccessLogGenerationConfig(ctx, request.Config) api.SetAccessLogGenerationInput(ctx, request) api.StartAccessLog(ctx) + request.Contents = aistudio.DropUnsupportedAssistantMedia(request.Contents) request.Model = service.catalogModelID(ctx, request.Model) requestCtx, cancel, err := service.dataRequestContext(ctx) if err == nil && suffixErr != nil {