WorkflowDemoRunHandler 返回一个 HTTP handler, 用于运行内置的演示工作流。 路径示例: POST /v1/workflows/demo/run 请求体: { "workflow_id": "sequential_demo", "input": "处理用户数据" } 响应体: { "events": [ {"id":"...","agent_id":"DataCollector","text":"...","metadata":{...}}, ... ] }
()
| 172 | // ] |
| 173 | // } |
| 174 | func (s *Server) WorkflowDemoRunHandler() http.Handler { |
| 175 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 176 | if r.Method != http.MethodPost { |
| 177 | http.Error(w, "method not allowed", http.StatusMethodNotAllowed) |
| 178 | return |
| 179 | } |
| 180 | |
| 181 | var req WorkflowRunRequest |
| 182 | if err := jsonNewDecoder(r.Body).Decode(&req); err != nil { |
| 183 | http.Error(w, "invalid JSON body", http.StatusBadRequest) |
| 184 | return |
| 185 | } |
| 186 | |
| 187 | if req.WorkflowID == "" { |
| 188 | http.Error(w, "workflow_id is required", http.StatusBadRequest) |
| 189 | return |
| 190 | } |
| 191 | if req.Input == "" { |
| 192 | http.Error(w, "input is required", http.StatusBadRequest) |
| 193 | return |
| 194 | } |
| 195 | |
| 196 | ctx := r.Context() |
| 197 | ctx, cancel := context.WithTimeout(ctx, 2*time.Minute) |
| 198 | defer cancel() |
| 199 | |
| 200 | wf, err := s.buildDemoWorkflow(req.WorkflowID) |
| 201 | if err != nil { |
| 202 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 203 | return |
| 204 | } |
| 205 | |
| 206 | var events []WorkflowEvent |
| 207 | |
| 208 | reader := wf.Execute(ctx, req.Input) |
| 209 | for { |
| 210 | ev, err := reader.Recv() |
| 211 | if err != nil { |
| 212 | if errors.Is(err, io.EOF) { |
| 213 | break |
| 214 | } |
| 215 | resp := &WorkflowRunResponse{ |
| 216 | ErrorMessage: err.Error(), |
| 217 | } |
| 218 | writeJSON(w, http.StatusInternalServerError, resp) |
| 219 | return |
| 220 | } |
| 221 | if ev == nil { |
| 222 | continue |
| 223 | } |
| 224 | events = append(events, WorkflowEvent{ |
| 225 | ID: ev.ID, |
| 226 | Timestamp: ev.Timestamp, |
| 227 | AgentID: ev.AgentID, |
| 228 | Branch: ev.Branch, |
| 229 | Author: ev.Author, |
| 230 | Text: ev.Content.Content, |
| 231 | Metadata: ev.Metadata, |
nothing calls this directly
no test coverage detected