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

Method Create

agent/agentcontainers/subagent.go:192–276  ·  view source on GitHub ↗
(ctx context.Context, agent SubAgent)

Source from the content-addressed store, hash-verified

190}
191
192func (a *subAgentAPIClient) Create(ctx context.Context, agent SubAgent) (_ SubAgent, err error) {
193 a.logger.Debug(ctx, "creating sub agent", slog.F("name", agent.Name), slog.F("directory", agent.Directory))
194
195 var id []byte
196 if agent.ID != uuid.Nil {
197 id = agent.ID[:]
198 }
199
200 displayApps := make([]agentproto.CreateSubAgentRequest_DisplayApp, 0, len(agent.DisplayApps))
201 for _, displayApp := range agent.DisplayApps {
202 var app agentproto.CreateSubAgentRequest_DisplayApp
203 switch displayApp {
204 case codersdk.DisplayAppPortForward:
205 app = agentproto.CreateSubAgentRequest_PORT_FORWARDING_HELPER
206 case codersdk.DisplayAppSSH:
207 app = agentproto.CreateSubAgentRequest_SSH_HELPER
208 case codersdk.DisplayAppVSCodeDesktop:
209 app = agentproto.CreateSubAgentRequest_VSCODE
210 case codersdk.DisplayAppVSCodeInsiders:
211 app = agentproto.CreateSubAgentRequest_VSCODE_INSIDERS
212 case codersdk.DisplayAppWebTerminal:
213 app = agentproto.CreateSubAgentRequest_WEB_TERMINAL
214 default:
215 return SubAgent{}, xerrors.Errorf("unexpected codersdk.DisplayApp: %#v", displayApp)
216 }
217
218 displayApps = append(displayApps, app)
219 }
220
221 apps := make([]*agentproto.CreateSubAgentRequest_App, 0, len(agent.Apps))
222 for _, app := range agent.Apps {
223 protoApp, err := app.ToProtoApp()
224 if err != nil {
225 return SubAgent{}, xerrors.Errorf("convert app: %w", err)
226 }
227
228 apps = append(apps, protoApp)
229 }
230
231 resp, err := a.api.CreateSubAgent(ctx, &agentproto.CreateSubAgentRequest{
232 Name: agent.Name,
233 Directory: agent.Directory,
234 Architecture: agent.Architecture,
235 OperatingSystem: agent.OperatingSystem,
236 DisplayApps: displayApps,
237 Apps: apps,
238 Id: id,
239 })
240 if err != nil {
241 return SubAgent{}, err
242 }
243 defer func() {
244 if err != nil {
245 // Best effort.
246 _, _ = a.api.DeleteSubAgent(ctx, &agentproto.DeleteSubAgentRequest{
247 Id: resp.GetAgent().GetId(),
248 })
249 }

Callers

nothing calls this directly

Calls 12

ToProtoAppMethod · 0.80
GetAgentMethod · 0.80
GetAuthTokenMethod · 0.80
GetAppCreationErrorsMethod · 0.80
GetIndexMethod · 0.80
GetFieldMethod · 0.80
CreateSubAgentMethod · 0.65
DeleteSubAgentMethod · 0.65
ErrorfMethod · 0.45
GetIdMethod · 0.45
GetNameMethod · 0.45
GetErrorMethod · 0.45

Tested by

no test coverage detected