58 lines
2.4 KiB
Go
58 lines
2.4 KiB
Go
// AI 图片输出持久化与模型输入/输出模态能力检查。
|
|
package platform
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"database/sql"
|
|
"encoding/hex"
|
|
"errors"
|
|
|
|
"gitea.entroia.com/Zayden-Jung/foundry-core/internal/codex"
|
|
)
|
|
|
|
// persistCanvasImage 完整解码 worker 图片并核对 MIME,以 outputID 作为幂等键存原图。
|
|
// 先绑定 run 来源素材,使冲突输出在用户显式应用前仍能按原 run 权限读取。
|
|
func (s *service) persistCanvasImage(ctx context.Context, tx *sql.Tx, run canvasQueueRun, outputID string, image *codex.CanvasImage) (response, error) {
|
|
if image == nil || len(image.Data) == 0 || len(image.Data) > assetLimit {
|
|
return nil, errors.New("invalid_image_output")
|
|
}
|
|
mime, width, height, err := inspectAsset(image.Data)
|
|
if err != nil || mime != image.MIME {
|
|
return nil, errors.New("invalid_image_output")
|
|
}
|
|
digest := sha256.Sum256(image.Data)
|
|
var id string
|
|
err = tx.QueryRowContext(ctx, `INSERT INTO canvas_assets(project,workspace_id,uploaded_by,mime_type,bytes,width,height,digest,idempotency_key) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9) RETURNING id::text`, run.Project, run.Workspace, run.Account, mime, image.Data, width, height, hex.EncodeToString(digest[:]), outputID).Scan(&id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Conflict outputs remain readable under the run's originating material even
|
|
// before explicit application. No transient or worker-local URL is persisted.
|
|
_, err = tx.ExecContext(ctx, `INSERT INTO canvas_asset_bindings(project,workspace_id,asset_id,board_id) VALUES($1,$2,$3,$4)`, run.Project, run.Workspace, id, run.Board)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return response{"output_id": outputID, "kind": "image", "assetId": id, "src": s.assetURL(run.Project, run.Workspace, id), "mime_type": mime, "bytes": len(image.Data), "width": width, "height": height}, nil
|
|
}
|
|
|
|
// canvasModelSupports 检查输出模态;支持图片输入并不意味着可生成图片。
|
|
func canvasModelSupports(model codex.CanvasModel, modality string) bool {
|
|
for _, value := range model.OutputModalities {
|
|
if value == modality {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// canvasModelAccepts 检查输入模态,准入阶段据此拒绝给纯文本输入模型喂图。
|
|
func canvasModelAccepts(model codex.CanvasModel, modality string) bool {
|
|
for _, value := range model.InputModalities {
|
|
if value == modality {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|