feat(cloud-agent): 云端 Agent - 完善运行时与资源持久化
This commit is contained in:
1 parent
48e6a705ef
commit
993580a7bd
40 files changed
+1739
-440
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
@@ -19,8 +20,12 @@ func (s *Service) cloudAgentModelList(intent *ModelRequestIntent) (any, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
logicalModels, err := s.PublicLogicalModels(intent)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := []map[string]any{}
|
||||
for _, m := range catalog.Models {
|
||||
for _, m := range logicalModels {
|
||||
if m.Available && cloudAgentGenerationModeSupported(normalizeCapability(m.Capability)) {
|
||||
items = append(items, map[string]any{"name": m.Name, "capability": m.Capability, "selection": map[string]any{"logicalModelId": m.ID}, "priceLabel": m.PriceLabel, "priceTiers": m.PriceTiers, "options": m.CapabilitySpec, "profiles": m.CapabilityProfiles, "defaults": m.DefaultOptions})
|
||||
}
|
||||
@@ -28,7 +33,7 @@ func (s *Service) cloudAgentModelList(intent *ModelRequestIntent) (any, error) {
|
||||
for _, channel := range catalog.Channels {
|
||||
for _, m := range channel.Models {
|
||||
if m.Available && cloudAgentGenerationModeSupported(normalizeCapability(m.Capability)) {
|
||||
items = append(items, map[string]any{"name": m.DisplayName, "capability": m.Capability, "selection": map[string]any{"channelId": channel.ID, "channelModelKey": m.ModelKey}, "priceLabel": m.PriceLabel, "priceTiers": m.PriceTiers, "options": m.CapabilityConfig})
|
||||
items = append(items, map[string]any{"name": m.DisplayName, "capability": m.Capability, "selection": map[string]any{"channelId": channel.ID, "channelModelKey": m.ModelKey}, "priceLabel": m.PriceLabel, "priceTiers": m.PriceTiers, "options": m.CapabilityConfig, "defaults": m.DefaultOptions})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,6 +114,9 @@ type cloudAgentMediaPlan struct {
|
||||
Args cloudAgentMediaArgs
|
||||
CallID string
|
||||
TransientReferences map[string]cloudAgentTransientReference
|
||||
// Prepared is populated by the auto path after its dry admission. Approval
|
||||
// mode keeps the same admission in state.Approval.Prepared.
|
||||
Prepared *cloudAgentPreparedMedia `json:"-"`
|
||||
}
|
||||
|
||||
// A resumed draft's incoming edges must describe the new approved inputs,
|
||||
@@ -161,7 +169,11 @@ func (s *Service) cloudAgentMediaModelName(a cloudAgentMediaArgs) (string, error
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, m := range catalog.Models {
|
||||
logicalModels, err := s.PublicLogicalModels(nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, m := range logicalModels {
|
||||
if a.LogicalModelID != "" && m.ID == a.LogicalModelID && m.Available && normalizeCapability(m.Capability) == normalizeCapability(a.Mode) {
|
||||
return m.Name, nil
|
||||
}
|
||||
@@ -422,17 +434,13 @@ func validateCloudAgentMediaArgs(a cloudAgentMediaArgs, state *cloudAgentRuntime
|
||||
if utf8.RuneCountInString(a.Prompt) > 16000 {
|
||||
return BadAuthRequest(fmt.Sprintf("提示词共%d字符,超过16000字符上限;请先告知用户,不要擅自删改关键内容", utf8.RuneCountInString(a.Prompt)))
|
||||
}
|
||||
if (mode == "image" || mode == "video") && strings.TrimSpace(a.Size) == "" {
|
||||
return BadAuthRequest("请填写模型支持的具体画幅;用户授权默认时沿用参考图比例或目录默认画幅,无需重复询问")
|
||||
}
|
||||
if a.Duration < 0 {
|
||||
return BadAuthRequest("生成时长不能为负数")
|
||||
}
|
||||
switch mode {
|
||||
case "video":
|
||||
if a.Duration == 0 {
|
||||
return BadAuthRequest("视频生成必须明确 durationSeconds")
|
||||
}
|
||||
// 0 means omitted. The selected model's capability defaults are resolved
|
||||
// during task admission; an explicit unsupported value still fails there.
|
||||
case "image", "audio":
|
||||
if a.Duration != 0 {
|
||||
return BadAuthRequest("只有视频生成允许设置 durationSeconds")
|
||||
@@ -540,14 +548,36 @@ func (s *Service) prepareCloudAgentMedia(run *model.CloudAgentExecution, state *
|
||||
if err := decodeCloudAgentJSONObject(call.Function.Arguments, &a); err != nil {
|
||||
return CreateTaskRequest{}, nil, cloudAgentJSONArgumentError(err)
|
||||
}
|
||||
if err := validateCloudAgentModelSelection(call.Function.Arguments, a); err != nil {
|
||||
a.Mode = strings.ToLower(strings.TrimSpace(a.Mode))
|
||||
defaultedModel, err := s.applyCloudAgentProjectDefaultModel(run, state, call.Function.Arguments, &a)
|
||||
if err != nil {
|
||||
return CreateTaskRequest{}, nil, err
|
||||
}
|
||||
selectionArguments := call.Function.Arguments
|
||||
if defaultedModel {
|
||||
selectionFields := map[string]string{}
|
||||
if a.LogicalModelID != "" {
|
||||
selectionFields["logicalModelId"] = a.LogicalModelID
|
||||
} else {
|
||||
selectionFields["channelId"], selectionFields["channelModelKey"] = a.ChannelID, a.ChannelModelKey
|
||||
}
|
||||
encoded, encodeErr := json.Marshal(selectionFields)
|
||||
if encodeErr != nil {
|
||||
return CreateTaskRequest{}, nil, encodeErr
|
||||
}
|
||||
selectionArguments = string(encoded)
|
||||
}
|
||||
if err := validateCloudAgentModelSelection(selectionArguments, a); err != nil {
|
||||
return CreateTaskRequest{}, nil, err
|
||||
}
|
||||
a.Mode = strings.ToLower(strings.TrimSpace(a.Mode))
|
||||
a.DraftRunID = run.ID
|
||||
if state.Approval != nil && state.Approval.Prepared != nil &&
|
||||
state.Approval.CallHash == cloudAgentApprovalCallHash(call) {
|
||||
a.Prepared = state.Approval.Prepared
|
||||
} else if state.AutoPreparedMedia != nil &&
|
||||
state.AutoPreparedCallHash == cloudAgentApprovalCallHash(call) &&
|
||||
time.Now().Before(state.AutoPreparedMedia.Quote.ExpiresAt) {
|
||||
a.Prepared = state.AutoPreparedMedia
|
||||
}
|
||||
if err := s.fillCloudAgentMediaSnapshotHash(run.UserID, state.Request.CanvasID, &a); err != nil {
|
||||
return CreateTaskRequest{}, nil, err
|
||||
@@ -593,7 +623,52 @@ func (s *Service) prepareCloudAgentMedia(run *model.CloudAgentExecution, state *
|
||||
for id, ref := range state.TransientReferences {
|
||||
transientSnapshot[id] = ref
|
||||
}
|
||||
return CreateTaskRequest{ProjectID: state.Request.CanvasID, Type: "canvas_" + a.Mode, Operation: operation, Prompt: a.Prompt, LogicalModelID: a.LogicalModelID, Model: a.ChannelModelKey, Input: input}, &cloudAgentMediaPlan{Args: a, CallID: call.ID, TransientReferences: transientSnapshot}, nil
|
||||
return CreateTaskRequest{ProjectID: state.Request.CanvasID, Type: "canvas_" + a.Mode, Operation: operation, Prompt: a.Prompt, LogicalModelID: a.LogicalModelID, Model: a.ChannelModelKey, Input: input}, &cloudAgentMediaPlan{Args: a, CallID: call.ID, TransientReferences: transientSnapshot, Prepared: a.Prepared}, nil
|
||||
}
|
||||
|
||||
// applyCloudAgentResolvedMediaDefaults makes the result of the dry admission
|
||||
// explicit in both the plan and the request that will be submitted. The
|
||||
// regular task admission already owns default resolution; this helper only
|
||||
// mirrors that resolved contract back to the Agent state so auto execution does
|
||||
// not need to fail once for size/duration and then ask the model to repair it.
|
||||
func applyCloudAgentResolvedMediaDefaults(req *CreateTaskRequest, plan *cloudAgentMediaPlan, task *model.Task) error {
|
||||
if req == nil || plan == nil || task == nil {
|
||||
return BadAuthRequest("媒体生成准入结果无效,未提交生成任务")
|
||||
}
|
||||
var input map[string]any
|
||||
if err := json.Unmarshal([]byte(task.InputJSON), &input); err != nil {
|
||||
return err
|
||||
}
|
||||
config, ok := input["config"].(map[string]any)
|
||||
if !ok {
|
||||
return BadAuthRequest("媒体生成未解析出模型配置,未提交生成任务")
|
||||
}
|
||||
|
||||
plan.Args.Size = stringValue(config["size"])
|
||||
if plan.Args.Mode == "image" {
|
||||
plan.Args.Quality = stringValue(config["quality"])
|
||||
}
|
||||
if plan.Args.Mode == "video" {
|
||||
plan.Args.Quality = stringValue(config["vquality"])
|
||||
if raw := stringValue(config["videoSeconds"]); raw != "" {
|
||||
seconds, err := strconv.Atoi(raw)
|
||||
if err != nil || seconds < 0 {
|
||||
return BadAuthRequest("模型目录返回的默认视频时长无效,未提交生成任务")
|
||||
}
|
||||
plan.Args.Duration = seconds
|
||||
}
|
||||
if raw := stringValue(config["videoGenerateAudio"]); raw != "" {
|
||||
audio, err := strconv.ParseBool(raw)
|
||||
if err != nil {
|
||||
return BadAuthRequest("模型目录返回的默认音频参数无效,未提交生成任务")
|
||||
}
|
||||
plan.Args.VideoGenerateAudio = &audio
|
||||
}
|
||||
}
|
||||
plan.Args.Prepared = nil
|
||||
req.Input = input
|
||||
req.Prompt = stringValue(input["prompt"])
|
||||
return nil
|
||||
}
|
||||
|
||||
func saveCloudAgentDocument(repo *repository.Repository, canvas *model.CanvasProject, doc map[string]any, policy RuntimePolicySetting) error {
|
||||
|
||||
Reference in new issue
Block a user