@Summary Send input to AI task @ID send-input-to-ai-task @Security CoderSessionToken @Accept json @Tags Tasks @Param user path string true "Username, user ID, or 'me' for the authenticated user" @Param task path string true "Task ID, or task name" @Param request body codersdk.TaskSendRequest true "T
(rw http.ResponseWriter, r *http.Request)
| 740 | // @Success 204 |
| 741 | // @Router /api/v2/tasks/{user}/{task}/send [post] |
| 742 | func (api *API) taskSend(rw http.ResponseWriter, r *http.Request) { |
| 743 | ctx := r.Context() |
| 744 | task := httpmw.TaskParam(r) |
| 745 | |
| 746 | var req codersdk.TaskSendRequest |
| 747 | if !httpapi.Read(ctx, rw, r, &req) { |
| 748 | return |
| 749 | } |
| 750 | if req.Input == "" { |
| 751 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 752 | Message: "Task input is required.", |
| 753 | }) |
| 754 | return |
| 755 | } |
| 756 | |
| 757 | if err := api.authAndDoWithTaskAppClient(r, task, func(ctx context.Context, client *http.Client, appURL *url.URL) error { |
| 758 | agentAPIClient, err := agentapisdk.NewClient(appURL.String(), agentapisdk.WithHTTPClient(client)) |
| 759 | if err != nil { |
| 760 | return httperror.NewResponseError(http.StatusBadGateway, codersdk.Response{ |
| 761 | Message: "Failed to create agentapi client.", |
| 762 | Detail: err.Error(), |
| 763 | }) |
| 764 | } |
| 765 | |
| 766 | statusResp, err := agentAPIClient.GetStatus(ctx) |
| 767 | if err != nil { |
| 768 | return httperror.NewResponseError(http.StatusBadGateway, codersdk.Response{ |
| 769 | Message: "Failed to get status from task app.", |
| 770 | Detail: err.Error(), |
| 771 | }) |
| 772 | } |
| 773 | |
| 774 | if statusResp.Status != agentapisdk.StatusStable { |
| 775 | return httperror.NewResponseError(http.StatusConflict, codersdk.Response{ |
| 776 | Message: "Task app is not ready to accept input.", |
| 777 | Detail: fmt.Sprintf("Status: %s", statusResp.Status), |
| 778 | }) |
| 779 | } |
| 780 | |
| 781 | _, err = agentAPIClient.PostMessage(ctx, agentapisdk.PostMessageParams{ |
| 782 | Content: req.Input, |
| 783 | Type: agentapisdk.MessageTypeUser, |
| 784 | }) |
| 785 | if err != nil { |
| 786 | return httperror.NewResponseError(http.StatusBadGateway, codersdk.Response{ |
| 787 | Message: "Task app rejected the message.", |
| 788 | Detail: err.Error(), |
| 789 | }) |
| 790 | } |
| 791 | |
| 792 | return nil |
| 793 | }); err != nil { |
| 794 | httperror.WriteResponseError(ctx, rw, err) |
| 795 | return |
| 796 | } |
| 797 | |
| 798 | rw.WriteHeader(http.StatusNoContent) |
| 799 | } |
nothing calls this directly
no test coverage detected