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

Method watchChatDesktop

coderd/exp_chats.go:2464–2558  ·  view source on GitHub ↗

EXPERIMENTAL: this endpoint is experimental and is subject to change. @Summary Connect to chat workspace desktop via WebSockets @ID connect-to-chat-workspace-desktop-via-websockets @Security CoderSessionToken @Tags Chats @Produce application/octet-stream @Param chat path string true "Chat ID" forma

(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

2462//
2463//nolint:revive // HTTP handler writes to ResponseWriter.
2464func (api *API) watchChatDesktop(rw http.ResponseWriter, r *http.Request) {
2465 var (
2466 ctx = r.Context()
2467 chat = httpmw.ChatParam(r)
2468 logger = api.Logger.Named("chat_desktop").With(slog.F("chat_id", chat.ID))
2469 )
2470
2471 if _, ok := api.authorizeChatWorkspaceExec(rw, r, chat, "Chat has no workspace."); !ok {
2472 return
2473 }
2474
2475 agents, err := api.Database.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx, chat.WorkspaceID.UUID)
2476 if err != nil {
2477 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
2478 Message: "Internal error fetching workspace agents.",
2479 Detail: err.Error(),
2480 })
2481 return
2482 }
2483 if len(agents) == 0 {
2484 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
2485 Message: "Chat workspace has no agents.",
2486 })
2487 return
2488 }
2489
2490 apiAgent, err := db2sdk.WorkspaceAgent(
2491 api.DERPMap(),
2492 *api.TailnetCoordinator.Load(),
2493 agents[0],
2494 nil,
2495 nil,
2496 nil,
2497 api.AgentInactiveDisconnectTimeout,
2498 api.DeploymentValues.AgentFallbackTroubleshootingURL.String(),
2499 )
2500 if err != nil {
2501 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
2502 Message: "Internal error reading workspace agent.",
2503 Detail: err.Error(),
2504 })
2505 return
2506 }
2507 if apiAgent.Status != codersdk.WorkspaceAgentConnected {
2508 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
2509 Message: fmt.Sprintf("Agent state is %q, must be connected.", apiAgent.Status),
2510 })
2511 return
2512 }
2513
2514 dialCtx, dialCancel := context.WithTimeout(ctx, 30*time.Second)
2515 defer dialCancel()
2516
2517 agentConn, release, err := api.agentProvider.AgentConn(dialCtx, agents[0].ID)
2518 if err != nil {
2519 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
2520 Message: "Failed to dial workspace agent.",
2521 Detail: err.Error(),

Callers

nothing calls this directly

Calls 15

DERPMapMethod · 0.95
ChatParamFunction · 0.92
WriteFunction · 0.92
WorkspaceAgentFunction · 0.92
WebsocketNetConnFunction · 0.92
HeartbeatCloseFunction · 0.92
BicopyFunction · 0.92
NamedMethod · 0.80
ContextMethod · 0.65
AgentConnMethod · 0.65

Tested by

no test coverage detected