docs: annotate Foundry source and deployment lifecycle in Chinese [skip ci]
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
// AI run 准入入口:冻结依赖和输入、验证模型及队列预算,再原子保存配置、run、steps、事件和回执。
|
||||
package platform
|
||||
|
||||
import (
|
||||
@@ -15,6 +16,7 @@ import (
|
||||
"gitea.entroia.com/Zayden-Jung/foundry-core/internal/codex"
|
||||
)
|
||||
|
||||
// appendAIEvent 用单条 SQL 原子递增 run.last_seq 并插入事件,序号与业务状态一起提交。
|
||||
func appendAIEvent(rctx context.Context, tx *sql.Tx, project, workspace, run, kind string, data any) error {
|
||||
raw, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
@@ -23,6 +25,8 @@ func appendAIEvent(rctx context.Context, tx *sql.Tx, project, workspace, run, ki
|
||||
_, err = tx.ExecContext(rctx, `WITH next AS (UPDATE canvas_ai_runs SET last_seq=last_seq+1 WHERE project=$1 AND workspace_id=$2 AND id=$3 RETURNING last_seq) INSERT INTO canvas_ai_events(project,workspace_id,run_id,seq,type,data) SELECT $1,$2,$3,last_seq,$4,$5 FROM next`, project, workspace, run, kind, raw)
|
||||
return err
|
||||
}
|
||||
|
||||
// createCanvasAIRun 接受旧快照协议或 canonical v2;先按原请求身份恢复结果,后续确定拒绝可冻结零效果回执。
|
||||
func (s *service) createCanvasAIRun(w http.ResponseWriter, r *http.Request, project, account string) error {
|
||||
if r.Method != http.MethodPost {
|
||||
reject(w, 405, "method_not_allowed")
|
||||
@@ -76,6 +80,7 @@ func (s *service) createCanvasAIRun(w http.ResponseWriter, r *http.Request, proj
|
||||
reject(w, 413, "context_too_large")
|
||||
return nil
|
||||
}
|
||||
// v2 的 If-Match 也是不可变请求的一部分,不能只换版本头重用原始准入键。
|
||||
hash := payloadDigest(input)
|
||||
if input.ProtocolVersion != 0 {
|
||||
// The version header is part of the immutable canonical admission request.
|
||||
@@ -91,6 +96,7 @@ func (s *service) createCanvasAIRun(w http.ResponseWriter, r *http.Request, proj
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 只有完整 v2 请求身份才建立副作用 savepoint;旧请求沿用原 run.create_response 幂等路径。
|
||||
var receipt *canvasAIRequest
|
||||
if input.ProtocolVersion == 2 && input.RequestID == key && uuidSlugPattern.MatchString(input.OperationID) && input.TargetIncarnation > 0 {
|
||||
receipt = &canvasAIRequest{Key: key, Hash: hash, Action: "run.start", Board: board, Operation: input.OperationID}
|
||||
@@ -158,6 +164,7 @@ func (s *service) createCanvasAIRun(w http.ResponseWriter, r *http.Request, proj
|
||||
if !canvasChoice(target["kind"], "text", "script", "plain-text", "image", "plain-image") {
|
||||
return rejectRequest(422, "model_incompatible")
|
||||
}
|
||||
// canonical 目标必须显式提供当前 incarnation,旧树请求不能夹带 canonical 字段越过迁移屏障。
|
||||
root, incarnation, _, err := canvasAIIdentity(r.Context(), tx, project, workspace, board, input.NodeID)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -172,6 +179,7 @@ func (s *service) createCanvasAIRun(w http.ResponseWriter, r *http.Request, proj
|
||||
} else if input.ProtocolVersion != 0 || input.RequestID != "" || input.TargetIncarnation != 0 || input.OperationID != "" || input.ExpectedVersions != nil {
|
||||
return rejectRequest(409, "canonical_migration_required")
|
||||
}
|
||||
// 先在内存准备目标 AI 配置;所有规划与预算检查通过后才随 run 一起落库,拒绝不能泄漏配置更新。
|
||||
change, err := prepareCanvasAIPatch(r, tx, project, workspace, parent, input.NodeID, response{"ai": map[string]any{"enabled": true, "modelId": input.ModelID, "prompt": input.Prompt, "parameters": input.Parameters, "propagate": input.Propagate}})
|
||||
if err != nil {
|
||||
return rejectRequest(400, "invalid_ai_request")
|
||||
@@ -207,12 +215,14 @@ func (s *service) createCanvasAIRun(w http.ResponseWriter, r *http.Request, proj
|
||||
if err = bindCanvasAIIdentities(r.Context(), tx, project, workspace, &plan); err != nil {
|
||||
return err
|
||||
}
|
||||
// 规划结果绑定稳定目标身份后,再验证每步输出模态和所有静态/动态图片来源是否被所选模型支持。
|
||||
stepKinds := map[string]string{}
|
||||
for _, step := range plan.Steps {
|
||||
stepKinds[step.ID] = step.Kind
|
||||
}
|
||||
freezeCtx, stopFreeze := context.WithTimeout(r.Context(), 10*time.Second)
|
||||
defer stopFreeze()
|
||||
// 同一来源图片只冻结一次,十秒总上下文限制外部抓取;入队后不再依赖可变化远程 URL。
|
||||
frozenImages := map[string]map[string]any{}
|
||||
for index, step := range plan.Steps {
|
||||
if step.Status == "skipped" {
|
||||
@@ -292,6 +302,7 @@ func (s *service) createCanvasAIRun(w http.ResponseWriter, r *http.Request, proj
|
||||
if len(prompt)+len(text) > model.MaxInputBytes {
|
||||
return rejectRequest(413, "context_too_large")
|
||||
}
|
||||
// canonical 忙碌检查按根/稳定 ID/incarnation,owner 移动不能绕过同一目标的在途任务约束。
|
||||
var busy bool
|
||||
if step.CanonicalRoot != "" {
|
||||
err = tx.QueryRowContext(r.Context(), `SELECT EXISTS(SELECT 1 FROM canvas_ai_steps WHERE project=$1 AND workspace_id=$2 AND frozen_input->>'canonical_root'=$3 AND node_id=$4 AND frozen_input->>'target_incarnation'=$5::bigint::text AND status IN('queued','running'))`, project, workspace, step.CanonicalRoot, step.NodeID, step.TargetIncarnation).Scan(&busy)
|
||||
@@ -306,6 +317,7 @@ func (s *service) createCanvasAIRun(w http.ResponseWriter, r *http.Request, proj
|
||||
}
|
||||
}
|
||||
// A project-scoped advisory lock makes the queue limit atomic across workspaces.
|
||||
// 项目级事务锁跨工作区串行化队列计数与插入,防止多个工作区同时越过 128 活跃 run 上限。
|
||||
_, err = tx.ExecContext(r.Context(), `SELECT pg_advisory_xact_lock(hashtextextended($1,0))`, project+"/canvas-ai-queue")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -343,6 +355,7 @@ func (s *service) createCanvasAIRun(w http.ResponseWriter, r *http.Request, proj
|
||||
if err = appendAIEvent(r.Context(), tx, project, workspace, id, "run.queued", response{"status": "queued"}); err != nil {
|
||||
return err
|
||||
}
|
||||
// 拓扑顺序作为 ordinal 持久化;skipped 步也保存状态和初始事件,使查询能解释未执行原因。
|
||||
for ordinal, step := range plan.Steps {
|
||||
frozen, _ := json.Marshal(step)
|
||||
_, err = tx.ExecContext(r.Context(), `INSERT INTO canvas_ai_steps(project,workspace_id,run_id,id,board_id,node_id,ordinal,status,model_id,frozen_input,target_fingerprint) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)`, project, workspace, id, step.ID, step.BoardID, step.NodeID, ordinal, step.Status, step.ModelID, frozen, step.Fingerprint)
|
||||
|
||||
Reference in New Issue
Block a user