Apply canonical AI outputs through scoped identity-checked changes
This commit is contained in:
@@ -47,11 +47,15 @@ func (s *service) createCanvasAIRun(w http.ResponseWriter, r *http.Request, proj
|
||||
return nil
|
||||
}
|
||||
var input struct {
|
||||
NodeID string `json:"node_id"`
|
||||
ModelID string `json:"model_id"`
|
||||
Prompt string `json:"prompt"`
|
||||
Parameters map[string]any `json:"parameters"`
|
||||
Propagate bool `json:"propagate"`
|
||||
ProtocolVersion int `json:"protocol_version,omitempty"`
|
||||
OperationID string `json:"operation_id,omitempty"`
|
||||
RequestID string `json:"request_id,omitempty"`
|
||||
TargetIncarnation int64 `json:"target_incarnation,omitempty"`
|
||||
NodeID string `json:"node_id"`
|
||||
ModelID string `json:"model_id"`
|
||||
Prompt string `json:"prompt"`
|
||||
Parameters map[string]any `json:"parameters"`
|
||||
Propagate bool `json:"propagate"`
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(raw))
|
||||
decoder.DisallowUnknownFields()
|
||||
@@ -59,6 +63,10 @@ func (s *service) createCanvasAIRun(w http.ResponseWriter, r *http.Request, proj
|
||||
reject(w, 400, "invalid_ai_request")
|
||||
return nil
|
||||
}
|
||||
if input.OperationID != "" && !uuidSlugPattern.MatchString(input.OperationID) {
|
||||
reject(w, 400, "invalid_ai_request")
|
||||
return nil
|
||||
}
|
||||
if input.Parameters == nil {
|
||||
input.Parameters = map[string]any{}
|
||||
}
|
||||
@@ -105,6 +113,16 @@ func (s *service) createCanvasAIRun(w http.ResponseWriter, r *http.Request, proj
|
||||
if err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
if input.OperationID != "" {
|
||||
var admitted bool
|
||||
if err = tx.QueryRowContext(r.Context(), `SELECT EXISTS(SELECT 1 FROM canvas_ai_runs WHERE project=$1 AND workspace_id=$2 AND account_id=$3 AND logical_operation_id=$4)`, project, workspace, account, input.OperationID).Scan(&admitted); err != nil {
|
||||
return err
|
||||
}
|
||||
if admitted {
|
||||
reject(w, 409, "operation_identity_conflict")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
parent, err := loadCanvas(r, tx, project, workspace, board)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -130,11 +148,25 @@ func (s *service) createCanvasAIRun(w http.ResponseWriter, r *http.Request, proj
|
||||
reject(w, 422, "model_incompatible")
|
||||
return nil
|
||||
}
|
||||
target["ai"] = map[string]any{"enabled": true, "modelId": input.ModelID, "prompt": input.Prompt, "parameters": input.Parameters, "propagate": input.Propagate}
|
||||
doc["schema_version"] = 2
|
||||
encoded, _ := json.Marshal(doc)
|
||||
normalized, ok := normalizeCanvas(encoded)
|
||||
if !ok {
|
||||
root, incarnation, _, err := canvasAIIdentity(r.Context(), tx, project, workspace, board, input.NodeID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if root != "" {
|
||||
if input.ProtocolVersion != 2 || input.RequestID != key || !uuidSlugPattern.MatchString(input.OperationID) || input.TargetIncarnation < 1 {
|
||||
reject(w, 400, "invalid_ai_protocol")
|
||||
return nil
|
||||
}
|
||||
if input.TargetIncarnation != incarnation {
|
||||
reject(w, 409, "target_changed")
|
||||
return nil
|
||||
}
|
||||
} else if input.ProtocolVersion != 0 || input.RequestID != "" || input.TargetIncarnation != 0 || input.OperationID != "" {
|
||||
reject(w, 409, "canonical_migration_required")
|
||||
return nil
|
||||
}
|
||||
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 {
|
||||
reject(w, 400, "invalid_ai_request")
|
||||
return nil
|
||||
}
|
||||
@@ -149,8 +181,7 @@ func (s *service) createCanvasAIRun(w http.ResponseWriter, r *http.Request, proj
|
||||
for _, model := range models {
|
||||
catalogue[model.ID] = model
|
||||
}
|
||||
parent.Document = normalized
|
||||
parent.Version++
|
||||
parent = change.Snapshot
|
||||
plan, err := loadCanvasAIPlan(r, tx, project, workspace, account, parent, input.NodeID, input.Propagate)
|
||||
if err != nil {
|
||||
if err.Error() == "execution_cycle" {
|
||||
@@ -160,6 +191,9 @@ func (s *service) createCanvasAIRun(w http.ResponseWriter, r *http.Request, proj
|
||||
}
|
||||
return nil
|
||||
}
|
||||
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
|
||||
@@ -287,12 +321,16 @@ func (s *service) createCanvasAIRun(w http.ResponseWriter, r *http.Request, proj
|
||||
planJSON, _ := json.Marshal(plan)
|
||||
poll := "/projects/" + url.PathEscape(project) + "/workspaces/" + workspace + "/materials/" + board + "/canvas/ai/runs/" + id
|
||||
result := response{"run_id": id, "status": "queued", "poll_url": poll, "plan": publicAIPlan(plan), "canvas_version": parent.Version}
|
||||
if root != "" {
|
||||
result["protocol_version"], result["request_id"], result["admission"], result["operation_id"] = 2, key, "accepted", input.OperationID
|
||||
change.addCanonical(result)
|
||||
}
|
||||
resultJSON, _ := json.Marshal(result)
|
||||
_, err = tx.ExecContext(r.Context(), `INSERT INTO canvas_ai_runs(project,workspace_id,board_id,id,account_id,idempotency_key,payload_hash,status,plan,create_response) VALUES($1,$2,$3,$4,$5,$6,$7,'queued',$8,$9)`, project, workspace, board, id, account, key, hash, planJSON, resultJSON)
|
||||
_, err = tx.ExecContext(r.Context(), `INSERT INTO canvas_ai_runs(project,workspace_id,board_id,id,account_id,idempotency_key,payload_hash,status,plan,create_response,logical_operation_id) VALUES($1,$2,$3,$4,$5,$6,$7,'queued',$8,$9,NULLIF($10,'')::uuid)`, project, workspace, board, id, account, key, hash, planJSON, resultJSON, input.OperationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = s.saveCanvas(r, tx, project, workspace, parent); err != nil {
|
||||
if err = s.saveCanvasAIChange(r, tx, project, workspace, account, change); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = appendAIEvent(r.Context(), tx, project, workspace, id, "run.queued", response{"status": "queued"}); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user