- Notifications
You must be signed in to change notification settings - Fork1k
feat(coderd): add experimental tasks logs endpoint#19958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -691,6 +691,89 @@ func (api *API) taskSend(rw http.ResponseWriter, r *http.Request) { | ||
rw.WriteHeader(http.StatusNoContent) | ||
} | ||
func (api *API) taskLogs(rw http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
idStr := chi.URLParam(r, "id") | ||
taskID, err := uuid.Parse(idStr) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
Message: fmt.Sprintf("Invalid UUID %q for task ID.", idStr), | ||
}) | ||
return | ||
} | ||
var out codersdk.TaskLogsResponse | ||
if err := api.authAndDoWithTaskSidebarAppClient(r, taskID, func(ctx context.Context, client *http.Client, appURL *url.URL) error { | ||
req, err := agentapiNewRequest(ctx, http.MethodGet, appURL, "messages", nil) | ||
if err != nil { | ||
return err | ||
} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return httperror.NewResponseError(http.StatusBadGateway, codersdk.Response{ | ||
Message: "Failed to reach task app endpoint.", | ||
Detail: err.Error(), | ||
}) | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 128)) | ||
return httperror.NewResponseError(http.StatusBadGateway, codersdk.Response{ | ||
Message: "Task app rejected the request.", | ||
Detail: fmt.Sprintf("Upstream status: %d; Body: %s", resp.StatusCode, body), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 👍 this should give a good indication to the user | ||
}) | ||
} | ||
// {"$schema":"http://localhost:3284/schemas/MessagesResponseBody.json","messages":[]} | ||
var respBody struct { | ||
Messages []struct { | ||
ID int `json:"id"` | ||
Content string `json:"content"` | ||
Role string `json:"role"` | ||
Time time.Time `json:"time"` | ||
} `json:"messages"` | ||
} | ||
if err := json.NewDecoder(resp.Body).Decode(&respBody); err != nil { | ||
return httperror.NewResponseError(http.StatusBadGateway, codersdk.Response{ | ||
Message: "Failed to decode task app response body.", | ||
Detail: err.Error(), | ||
}) | ||
} | ||
logs := make([]codersdk.TaskLogEntry, 0, len(respBody.Messages)) | ||
for _, m := range respBody.Messages { | ||
var typ codersdk.TaskLogType | ||
switch strings.ToLower(m.Role) { | ||
case "user": | ||
typ = codersdk.TaskLogTypeInput | ||
case "agent": | ||
typ = codersdk.TaskLogTypeOutput | ||
Comment on lines +750 to +753 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Is there a reason to have these be different? Could we instead use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I'm somewhat indifferent and open to changing this, this is just the last option we discussed/settled on with@DanielleMaywood (correct me if I misremember). It seemed like a good idea to try to tie the concept of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Let's go with this for now. We can adjust if need be. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Yeah that was pretty much the reason, I'm also indifferent on what we pick here as well. | ||
default: | ||
return httperror.NewResponseError(http.StatusBadGateway, codersdk.Response{ | ||
Message: "Invalid task app response message role.", | ||
Detail: fmt.Sprintf(`Expected "user" or "agent", got %q.`, m.Role), | ||
}) | ||
} | ||
logs = append(logs, codersdk.TaskLogEntry{ | ||
ID: m.ID, | ||
Content: m.Content, | ||
Type: typ, | ||
Time: m.Time, | ||
}) | ||
} | ||
out = codersdk.TaskLogsResponse{Logs: logs} | ||
return nil | ||
}); err != nil { | ||
httperror.WriteResponseError(ctx, rw, err) | ||
return | ||
} | ||
httpapi.Write(ctx, rw, http.StatusOK, out) | ||
} | ||
// authAndDoWithTaskSidebarAppClient centralizes the shared logic to: | ||
// | ||
// - Fetch the task workspace | ||
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.