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

Function openAgentConn

codersdk/toolsdk/toolsdk.go:115–152  ·  view source on GitHub ↗

openAgentConn opens a ready workspace agent session for workspace inputs in [owner/]workspace[.agent] format.

(ctx context.Context, deps Deps, workspace string)

Source from the content-addressed store, hash-verified

113// openAgentConn opens a ready workspace agent session for workspace inputs in
114// [owner/]workspace[.agent] format.
115func openAgentConn(ctx context.Context, deps Deps, workspace string) (workspacesdk.AgentConn, error) {
116 if deps.coderClient == nil {
117 return nil, xerrors.New("workspace tools require an authenticated client")
118 }
119
120 workspaceName := NormalizeWorkspaceInput(workspace)
121 _, workspaceAgent, err := findWorkspaceAndAgent(ctx, deps.coderClient, workspaceName)
122 if err != nil {
123 return nil, xerrors.Errorf("failed to find workspace: %w", err)
124 }
125
126 if err := cliui.Agent(ctx, io.Discard, workspaceAgent.ID, cliui.AgentOptions{
127 FetchInterval: 0,
128 Fetch: deps.coderClient.WorkspaceAgent,
129 FetchLogs: deps.coderClient.WorkspaceAgentLogsAfter,
130 // Always wait for startup scripts.
131 Wait: true,
132 }); err != nil {
133 return nil, xerrors.Errorf("agent not ready: %w", err)
134 }
135
136 conn, release, err := deps.agentConnFn(ctx, workspaceAgent.ID)
137 if err != nil {
138 return nil, xerrors.Errorf("failed to dial agent: %w", err)
139 }
140
141 wrappedConn := workspacesdk.WrapAgentConn(conn, func() error {
142 if release != nil {
143 release()
144 }
145 return nil
146 })
147 if wrappedConn == nil {
148 return nil, xerrors.New("agent connection function returned nil connection")
149 }
150
151 return wrappedConn, nil
152}
153
154// HandlerFunc is a typed function that handles a tool call.
155type HandlerFunc[Arg, Ret any] func(context.Context, Deps, Arg) (Ret, error)

Callers 2

toolsdk.goFile · 0.85
bash.goFile · 0.85

Calls 6

AgentFunction · 0.92
WrapAgentConnFunction · 0.92
NormalizeWorkspaceInputFunction · 0.85
findWorkspaceAndAgentFunction · 0.85
NewMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected