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

Method loadWorkspaceAgentLocked

coderd/x/chatd/chatd.go:786–883  ·  view source on GitHub ↗
(
	ctx context.Context,
)

Source from the content-addressed store, hash-verified

784}
785
786func (c *turnWorkspaceContext) loadWorkspaceAgentLocked(
787 ctx context.Context,
788) (database.Chat, database.WorkspaceAgent, error) {
789 chatSnapshot := c.currentChatSnapshot()
790
791 for attempt := 0; attempt < 2; attempt++ {
792 if !chatSnapshot.WorkspaceID.Valid {
793 refreshedChat, refreshErr := refreshChatWorkspaceSnapshot(
794 ctx,
795 chatSnapshot,
796 c.loadChatSnapshot,
797 )
798 if refreshErr != nil {
799 return chatSnapshot, database.WorkspaceAgent{}, refreshErr
800 }
801 if refreshedChat.WorkspaceID.Valid {
802 c.setCurrentChat(refreshedChat)
803 chatSnapshot = refreshedChat
804 }
805 }
806
807 if !chatSnapshot.WorkspaceID.Valid {
808 return chatSnapshot, database.WorkspaceAgent{}, xerrors.New("no workspace is associated with this chat. Use the create_workspace tool to create one")
809 }
810
811 if chatSnapshot.AgentID.Valid {
812 agent, err := c.server.db.GetWorkspaceAgentByID(ctx, chatSnapshot.AgentID.UUID)
813 if err == nil {
814 latestChat, workspaceMatches := c.currentWorkspaceMatches(chatSnapshot.WorkspaceID)
815 if !workspaceMatches {
816 chatSnapshot = latestChat
817 continue
818 }
819 c.agent = agent
820 c.agentLoaded = true
821 c.cachedWorkspaceID = chatSnapshot.WorkspaceID
822 return chatSnapshot, c.agent, nil
823 }
824 if !xerrors.Is(err, sql.ErrNoRows) {
825 c.server.logger.Warn(ctx, "agent binding lookup failed, re-resolving",
826 slog.F("agent_id", chatSnapshot.AgentID.UUID),
827 slog.Error(err),
828 )
829 }
830 }
831
832 agents, err := c.server.db.GetWorkspaceAgentsInLatestBuildByWorkspaceID(
833 ctx,
834 chatSnapshot.WorkspaceID.UUID,
835 )
836 if err != nil {
837 return chatSnapshot, database.WorkspaceAgent{}, xerrors.Errorf(
838 "get workspace agents in latest build: %w",
839 err,
840 )
841 }
842 if len(agents) == 0 {
843 return chatSnapshot, database.WorkspaceAgent{}, errChatHasNoWorkspaceAgent

Callers 1

ensureWorkspaceAgentMethod · 0.95

Calls 13

currentChatSnapshotMethod · 0.95
setCurrentChatMethod · 0.95
FindChatAgentFunction · 0.92
NewMethod · 0.65
GetWorkspaceAgentByIDMethod · 0.65
IsMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected