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

Method workspaceAgentClearChatContext

coderd/workspaceagents.go:2635–2685  ·  view source on GitHub ↗

@x-apidocgen {"skip": true}

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

Source from the content-addressed store, hash-verified

2633
2634// @x-apidocgen {"skip": true}
2635func (api *API) workspaceAgentClearChatContext(rw http.ResponseWriter, r *http.Request) {
2636 ctx := r.Context()
2637 workspaceAgent := httpmw.WorkspaceAgent(r)
2638
2639 var req agentsdk.ClearChatContextRequest
2640 populated := readChatContextBody(ctx, rw, r, &req, true)
2641 if !populated && r.Body != http.NoBody {
2642 return
2643 }
2644
2645 // Use system context for chat operations since the
2646 // workspace agent scope does not include chat resources.
2647 //nolint:gocritic // Agent needs system access to read/write chat resources.
2648 sysCtx := dbauthz.AsSystemRestricted(ctx)
2649 workspace, err := api.Database.GetWorkspaceByAgentID(sysCtx, workspaceAgent.ID)
2650 if err != nil {
2651 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
2652 Message: "Failed to determine workspace from agent token.",
2653 Detail: err.Error(),
2654 })
2655 return
2656 }
2657
2658 chat, err := resolveAgentChat(sysCtx, api.Database, workspaceAgent.ID, workspace.OwnerID, req.ChatID)
2659 if err != nil {
2660 // Zero active chats is not an error for clear.
2661 if errors.Is(err, errNoActiveChats) {
2662 httpapi.Write(ctx, rw, http.StatusOK, agentsdk.ClearChatContextResponse{})
2663 return
2664 }
2665 writeAgentChatError(ctx, rw, err)
2666 return
2667 }
2668
2669 err = clearAgentChatContext(sysCtx, api.Database, chat.ID, workspaceAgent.ID, workspace.OwnerID)
2670 if err != nil {
2671 if errors.Is(err, errChatNotActive) || errors.Is(err, errChatDoesNotBelongToAgent) || errors.Is(err, errChatDoesNotBelongToWorkspaceOwner) {
2672 writeAgentChatError(ctx, rw, err)
2673 return
2674 }
2675 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
2676 Message: "Failed to clear context from chat.",
2677 Detail: err.Error(),
2678 })
2679 return
2680 }
2681
2682 httpapi.Write(ctx, rw, http.StatusOK, agentsdk.ClearChatContextResponse{
2683 ChatID: chat.ID,
2684 })
2685}
2686
2687var (
2688 errNoActiveChats = xerrors.New("no active chats found")

Callers

nothing calls this directly

Calls 11

WorkspaceAgentFunction · 0.92
AsSystemRestrictedFunction · 0.92
WriteFunction · 0.92
readChatContextBodyFunction · 0.85
resolveAgentChatFunction · 0.85
writeAgentChatErrorFunction · 0.85
clearAgentChatContextFunction · 0.85
ContextMethod · 0.65
GetWorkspaceByAgentIDMethod · 0.65
ErrorMethod · 0.45
IsMethod · 0.45

Tested by

no test coverage detected