Run runs an agent with a message
(c *gin.Context)
| 318 | |
| 319 | // Run runs an agent with a message |
| 320 | func (h *AgentHandler) Run(c *gin.Context) { |
| 321 | ctx := c.Request.Context() |
| 322 | id := c.Param("id") |
| 323 | |
| 324 | var req struct { |
| 325 | Message string `json:"message" binding:"required"` |
| 326 | Context map[string]any `json:"context"` |
| 327 | } |
| 328 | |
| 329 | if err := c.ShouldBindJSON(&req); err != nil { |
| 330 | c.JSON(http.StatusBadRequest, gin.H{ |
| 331 | "success": false, |
| 332 | "error": gin.H{ |
| 333 | "code": "bad_request", |
| 334 | "message": err.Error(), |
| 335 | }, |
| 336 | }) |
| 337 | return |
| 338 | } |
| 339 | |
| 340 | // Get agent record |
| 341 | var agentRecord AgentRecord |
| 342 | if err := (*h.store).Get(ctx, "agents", id, &agentRecord); err != nil { |
| 343 | if errors.Is(err, store.ErrNotFound) { |
| 344 | c.JSON(http.StatusNotFound, gin.H{ |
| 345 | "success": false, |
| 346 | "error": gin.H{ |
| 347 | "code": "not_found", |
| 348 | "message": "Agent not found", |
| 349 | }, |
| 350 | }) |
| 351 | return |
| 352 | } |
| 353 | c.JSON(http.StatusInternalServerError, gin.H{ |
| 354 | "success": false, |
| 355 | "error": gin.H{ |
| 356 | "code": "internal_error", |
| 357 | "message": "Failed to get agent: " + err.Error(), |
| 358 | }, |
| 359 | }) |
| 360 | return |
| 361 | } |
| 362 | |
| 363 | // Create agent instance |
| 364 | ag, err := agent.Create(ctx, agentRecord.Config, h.deps) |
| 365 | if err != nil { |
| 366 | c.JSON(http.StatusInternalServerError, gin.H{ |
| 367 | "success": false, |
| 368 | "error": gin.H{ |
| 369 | "code": "internal_error", |
| 370 | "message": "Failed to create agent: " + err.Error(), |
| 371 | }, |
| 372 | }) |
| 373 | return |
| 374 | } |
| 375 | defer func() { _ = ag.Close() }() |
| 376 | |
| 377 | // Send message |