MCPcopy Index your code
hub / github.com/coder/coder / waitForAgentAndRespond

Function waitForAgentAndRespond

coderd/x/chatd/chattool/startworkspace.go:227–271  ·  view source on GitHub ↗

waitForAgentAndRespond selects the chat agent from the workspace's latest build, waits for it to become reachable, and returns a result map. When buildID is non-zero, it is included in the result so the frontend can fetch historical build logs. Pass uuid.Nil when no build was triggered (e.g. workspa

(
	ctx context.Context,
	db database.Store,
	agentConnFn AgentConnFunc,
	ws database.Workspace,
	buildID uuid.UUID,
)

Source from the content-addressed store, hash-verified

225// fantasy.ToolResponse via toolResponse(), and may add extra
226// fields before doing so.
227func waitForAgentAndRespond(
228 ctx context.Context,
229 db database.Store,
230 agentConnFn AgentConnFunc,
231 ws database.Workspace,
232 buildID uuid.UUID,
233) map[string]any {
234 agents, err := db.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx, ws.ID)
235 if err != nil || len(agents) == 0 {
236 // Workspace started but no agent found - still report
237 // success so the model knows the workspace is up.
238 result := map[string]any{
239 "started": true,
240 "workspace_name": ws.Name,
241 "agent_status": "no_agent",
242 }
243 setBuildID(result, buildID)
244 setNoBuild(result, buildID)
245 return result
246 }
247
248 selected, err := agentselect.FindChatAgent(agents)
249 if err != nil {
250 result := map[string]any{
251 "started": true,
252 "workspace_name": ws.Name,
253 "agent_status": "selection_error",
254 "agent_error": err.Error(),
255 }
256 setBuildID(result, buildID)
257 setNoBuild(result, buildID)
258 return result
259 }
260
261 result := map[string]any{
262 "started": true,
263 "workspace_name": ws.Name,
264 }
265 setBuildID(result, buildID)
266 setNoBuild(result, buildID)
267 for k, v := range waitForAgentReady(ctx, db, selected, agentConnFn) {
268 result[k] = v
269 }
270 return result
271}

Callers 1

StartWorkspaceFunction · 0.85

Calls 6

FindChatAgentFunction · 0.92
setBuildIDFunction · 0.85
setNoBuildFunction · 0.85
waitForAgentReadyFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected