openAgentConn opens a ready workspace agent session for workspace inputs in [owner/]workspace[.agent] format.
(ctx context.Context, deps Deps, workspace string)
| 113 | // openAgentConn opens a ready workspace agent session for workspace inputs in |
| 114 | // [owner/]workspace[.agent] format. |
| 115 | func 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. |
| 155 | type HandlerFunc[Arg, Ret any] func(context.Context, Deps, Arg) (Ret, error) |
no test coverage detected