Files
Cyrene_v2/internal/runtime/loop/vision.go
T
Aska 518bd0a230
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
feat: admin 端点补齐 + 视觉真推理 + 语音链路 + wire.go 收口(A+B+第3步)
批次A admin 端点(原本 404 变 200 真实数据):
- GET /admin/llm:读 s.cfg.LLM 组 openai+anthropic 两 provider(enabled=当前 Provider、multimodal=配 VisionModel),APIKey 不进 wire 零泄露。
- GET /admin/automations:*automations.Service.List()(reminders+event rules+cron schedules,cron 加只读登记)。
- GET /admin/users:cfg.AdminUser + *auth.APIKey.List()(密钥掩码);无用户存储,恒先返回当前 admin 账号。
- GET /admin/backthink:*backthink.Backthink.List(ctx,50)。均只读、owner/admin RBAC、{data} 信封。

批次B 视觉真推理 + 语音:
- rest.go chatRequest 加 Image?;流式 publishChatStream 带 image;userChatMessages 构造 ImageMessage(model.go:71)。
- loop/vision.go:ImageResolver 可注入(MVP 先看图再对话);run.go Run 开头 resolveImages,无 resolver 透传直交主模型。
- vision/resolver.go:MessageResolver 调 vision.Router.Route——KindMain 保留图片交主模型多模态、KindVisionTool/OCRTool 经 dispatcher.Invoke 折叠成 [图片内容] 文本、KindUnsupported 记日志保留原图不崩。
- gateway/voice.go:VoiceService 接口 + SetVoice 注入点 + handleVoiceInput(未注入回 error 不崩)+ runVoiceTurn 异步 STT→loop chat→TTS→定向 voice_output 帧。
- ws_hub/ws_route:voice_input 接 handleVoiceInput;前端 ws.ts decodeBase64/playVoiceAudio/case voice_output。

第3步 wire.go 收口(唯一碰 wire.go 的一步):
- Deps 补传 Automations:a.automation / Backthink:a.backthink / Users:auth.NewAPIKey(a.cfg.Auth)(*auth.APIKey 原生满足 UserStore,避免 admin 重复的复合类型)。
- gw.SetVoice(a.voice);loop.Options.Vision = vision.NewMessageResolver(router, info, dispatcher.Invoke)(router=NewRouter(WithVisionTool/WithOCRTool)、info=cfg.LLM.VisionModel)。

实机验证:四 admin 端点 200 真实数据(llm 双 provider 脱敏、users=admin,yeij0942,owner);WS 握手正常。go build/vet/test 全绿 + 前端 tsc。wire.go 557 行。
2026-09-06 18:54:05 +08:00

72 lines
2.4 KiB
Go

package loop
import (
"context"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
)
// ImageResolver is the loop's seam to the multimodal vision layer (design/06
// §0). It is called once at the start of Run for every message that carries an
// image content part, and may rewrite that message so the main model can reason
// over the image. The concrete resolver lives in the vision package and decides,
// via the vision Router, whether to keep the image for a vision-capable main
// model or fold it into a text description produced by the vision/OCR tool ("先
// 看图再对话"). Nil leaves the image content untouched so it flows straight to a
// vision-capable main model.
type ImageResolver interface {
// Resolve rewrites msg, which carries at least one image content part. It
// returns the (possibly rewritten) message. An error leaves the original
// message in place so the turn still proceeds.
Resolve(ctx context.Context, msg model.Message) (model.Message, error)
}
// ImageResolveFunc adapts a function to the ImageResolver interface.
type ImageResolveFunc func(ctx context.Context, msg model.Message) (model.Message, error)
// Resolve implements ImageResolver.
func (f ImageResolveFunc) Resolve(ctx context.Context, msg model.Message) (model.Message, error) {
return f(ctx, msg)
}
// resolveImages rewrites every image-bearing message in messages using the
// injected resolver. It returns the input slice unchanged when no resolver is
// configured, no image is present, or a message fails to resolve (the failure is
// logged and the original message is kept so the turn still runs). A fresh slice
// is returned when any message changed.
func (l *Loop) resolveImages(ctx context.Context, messages []model.Message) []model.Message {
if l.imageResolver == nil {
return messages
}
out := make([]model.Message, len(messages))
changed := false
for i, m := range messages {
if !messageHasImage(m) {
out[i] = m
continue
}
resolved, err := l.imageResolver.Resolve(ctx, m)
if err != nil {
l.log.Warn("image resolve", "error", err)
out[i] = m
continue
}
out[i] = resolved
changed = true
}
if !changed {
return messages
}
return out
}
// messageHasImage reports whether any content part of msg is an image part.
func messageHasImage(m model.Message) bool {
for _, part := range m.Content {
if part.Type == model.ContentTypeImage {
return true
}
}
return false
}