@@ -188,15 +188,72 @@ func (api *API) tasksCreate(rw http.ResponseWriter, r *http.Request) {
188188 WorkspaceOwner : owner .Username ,
189189 },
190190 })
191-
192191 defer commitAudit ()
193192 w , err := createWorkspace (ctx , aReq , apiKey .UserID , api , owner , createReq , r )
194193 if err != nil {
195194 httperror .WriteResponseError (ctx , rw , err )
196195 return
197196 }
198197
199- httpapi .Write (ctx , rw , http .StatusCreated , w )
198+ task := taskFromWorkspace (w , req .Prompt )
199+ httpapi .Write (ctx , rw , http .StatusCreated , task )
200+ }
201+
202+ func taskFromWorkspace (ws codersdk.Workspace , initialPrompt string ) codersdk.Task {
203+ // TODO(DanielleMaywood):
204+ // This just picks up the first agent it discovers.
205+ // This approach _might_ break when a task has multiple agents,
206+ // depending on which agent was found first.
207+ //
208+ // We explicitly do not have support for running tasks
209+ // inside of a sub agent at the moment, so we can be sure
210+ // that any sub agents are not the agent we're looking for.
211+ var taskAgentID uuid.NullUUID
212+ var taskAgentLifecycle * codersdk.WorkspaceAgentLifecycle
213+ var taskAgentHealth * codersdk.WorkspaceAgentHealth
214+ for _ , resource := range ws .LatestBuild .Resources {
215+ for _ , agent := range resource .Agents {
216+ if agent .ParentID .Valid {
217+ continue
218+ }
219+
220+ taskAgentID = uuid.NullUUID {Valid : true , UUID : agent .ID }
221+ taskAgentLifecycle = & agent .LifecycleState
222+ taskAgentHealth = & agent .Health
223+ break
224+ }
225+ }
226+
227+ var currentState * codersdk.TaskStateEntry
228+ if ws .LatestAppStatus != nil {
229+ currentState = & codersdk.TaskStateEntry {
230+ Timestamp : ws .LatestAppStatus .CreatedAt ,
231+ State : codersdk .TaskState (ws .LatestAppStatus .State ),
232+ Message : ws .LatestAppStatus .Message ,
233+ URI : ws .LatestAppStatus .URI ,
234+ }
235+ }
236+
237+ return codersdk.Task {
238+ ID : ws .ID ,
239+ OrganizationID : ws .OrganizationID ,
240+ OwnerID : ws .OwnerID ,
241+ OwnerName : ws .OwnerName ,
242+ Name : ws .Name ,
243+ TemplateID : ws .TemplateID ,
244+ TemplateName : ws .TemplateName ,
245+ TemplateDisplayName : ws .TemplateDisplayName ,
246+ TemplateIcon : ws .TemplateIcon ,
247+ WorkspaceID : uuid.NullUUID {Valid : true , UUID : ws .ID },
248+ WorkspaceAgentID : taskAgentID ,
249+ WorkspaceAgentLifecycle : taskAgentLifecycle ,
250+ WorkspaceAgentHealth : taskAgentHealth ,
251+ CreatedAt : ws .CreatedAt ,
252+ UpdatedAt : ws .UpdatedAt ,
253+ InitialPrompt : initialPrompt ,
254+ Status : ws .LatestBuild .Status ,
255+ CurrentState : currentState ,
256+ }
200257}
201258
202259// tasksFromWorkspaces converts a slice of API workspaces into tasks, fetching
@@ -221,60 +278,7 @@ func (api *API) tasksFromWorkspaces(ctx context.Context, apiWorkspaces []codersd
221278
222279 tasks := make ([]codersdk.Task , 0 , len (apiWorkspaces ))
223280 for _ , ws := range apiWorkspaces {
224- // TODO(DanielleMaywood):
225- // This just picks up the first agent it discovers.
226- // This approach _might_ break when a task has multiple agents,
227- // depending on which agent was found first.
228- //
229- // We explicitly do not have support for running tasks
230- // inside of a sub agent at the moment, so we can be sure
231- // that any sub agents are not the agent we're looking for.
232- var taskAgentID uuid.NullUUID
233- var taskAgentLifecycle * codersdk.WorkspaceAgentLifecycle
234- var taskAgentHealth * codersdk.WorkspaceAgentHealth
235- for _ , resource := range ws .LatestBuild .Resources {
236- for _ , agent := range resource .Agents {
237- if agent .ParentID .Valid {
238- continue
239- }
240-
241- taskAgentID = uuid.NullUUID {Valid : true , UUID : agent .ID }
242- taskAgentLifecycle = & agent .LifecycleState
243- taskAgentHealth = & agent .Health
244- break
245- }
246- }
247-
248- var currentState * codersdk.TaskStateEntry
249- if ws .LatestAppStatus != nil {
250- currentState = & codersdk.TaskStateEntry {
251- Timestamp : ws .LatestAppStatus .CreatedAt ,
252- State : codersdk .TaskState (ws .LatestAppStatus .State ),
253- Message : ws .LatestAppStatus .Message ,
254- URI : ws .LatestAppStatus .URI ,
255- }
256- }
257-
258- tasks = append (tasks , codersdk.Task {
259- ID : ws .ID ,
260- OrganizationID : ws .OrganizationID ,
261- OwnerID : ws .OwnerID ,
262- OwnerName : ws .OwnerName ,
263- Name : ws .Name ,
264- TemplateID : ws .TemplateID ,
265- TemplateName : ws .TemplateName ,
266- TemplateDisplayName : ws .TemplateDisplayName ,
267- TemplateIcon : ws .TemplateIcon ,
268- WorkspaceID : uuid.NullUUID {Valid : true , UUID : ws .ID },
269- WorkspaceAgentID : taskAgentID ,
270- WorkspaceAgentLifecycle : taskAgentLifecycle ,
271- WorkspaceAgentHealth : taskAgentHealth ,
272- CreatedAt : ws .CreatedAt ,
273- UpdatedAt : ws .UpdatedAt ,
274- InitialPrompt : promptsByBuildID [ws .LatestBuild .ID ],
275- Status : ws .LatestBuild .Status ,
276- CurrentState : currentState ,
277- })
281+ tasks = append (tasks , taskFromWorkspace (ws , promptsByBuildID [ws .LatestBuild .ID ]))
278282 }
279283
280284 return tasks , nil
0 commit comments