insertDevcontainerSubagent creates a workspace agent for a devcontainer's subagent if one is defined. It returns the subagent ID (zero UUID if no subagent is defined).
(
ctx context.Context,
db database.Store,
dc *sdkproto.Devcontainer,
parentAgent database.WorkspaceAgent,
resourceID uuid.UUID,
appSlugs map[string]struct{},
snapshot *telemetry.Snapshot,
opts *insertWorkspaceResourceOptions,
)
| 3394 | // subagent if one is defined. It returns the subagent ID (zero UUID if no |
| 3395 | // subagent is defined). |
| 3396 | func insertDevcontainerSubagent( |
| 3397 | ctx context.Context, |
| 3398 | db database.Store, |
| 3399 | dc *sdkproto.Devcontainer, |
| 3400 | parentAgent database.WorkspaceAgent, |
| 3401 | resourceID uuid.UUID, |
| 3402 | appSlugs map[string]struct{}, |
| 3403 | snapshot *telemetry.Snapshot, |
| 3404 | opts *insertWorkspaceResourceOptions, |
| 3405 | ) (uuid.UUID, error) { |
| 3406 | // If there are no attached resources, we don't need to pre-create the |
| 3407 | // subagent. This preserves backwards compatibility where devcontainers |
| 3408 | // without resources can have their agents recreated dynamically. |
| 3409 | if len(dc.GetApps()) == 0 && len(dc.GetScripts()) == 0 && len(dc.GetEnvs()) == 0 { |
| 3410 | return uuid.UUID{}, nil |
| 3411 | } |
| 3412 | |
| 3413 | subAgentID := uuid.New() |
| 3414 | if opts.useAgentIDsFromProto { |
| 3415 | var err error |
| 3416 | subAgentID, err = uuid.Parse(dc.GetSubagentId()) |
| 3417 | if err != nil { |
| 3418 | return uuid.UUID{}, xerrors.Errorf("parse subagent id: %w", err) |
| 3419 | } |
| 3420 | } |
| 3421 | |
| 3422 | envJSON, err := encodeSubagentEnvs(dc.GetEnvs()) |
| 3423 | if err != nil { |
| 3424 | return uuid.UUID{}, err |
| 3425 | } |
| 3426 | |
| 3427 | _, err = db.InsertWorkspaceAgent(ctx, database.InsertWorkspaceAgentParams{ |
| 3428 | ID: subAgentID, |
| 3429 | ParentID: uuid.NullUUID{Valid: true, UUID: parentAgent.ID}, |
| 3430 | CreatedAt: dbtime.Now(), |
| 3431 | UpdatedAt: dbtime.Now(), |
| 3432 | ResourceID: resourceID, |
| 3433 | Name: dc.GetName(), |
| 3434 | AuthToken: uuid.New(), |
| 3435 | AuthInstanceID: sql.NullString{}, |
| 3436 | Architecture: parentAgent.Architecture, |
| 3437 | EnvironmentVariables: envJSON, |
| 3438 | Directory: dc.GetWorkspaceFolder(), |
| 3439 | InstanceMetadata: pqtype.NullRawMessage{}, |
| 3440 | ResourceMetadata: pqtype.NullRawMessage{}, |
| 3441 | OperatingSystem: parentAgent.OperatingSystem, |
| 3442 | ConnectionTimeoutSeconds: parentAgent.ConnectionTimeoutSeconds, |
| 3443 | TroubleshootingURL: parentAgent.TroubleshootingURL, |
| 3444 | MOTDFile: "", |
| 3445 | DisplayApps: []database.DisplayApp{}, |
| 3446 | DisplayOrder: 0, |
| 3447 | APIKeyScope: parentAgent.APIKeyScope, |
| 3448 | }) |
| 3449 | if err != nil { |
| 3450 | return uuid.UUID{}, xerrors.Errorf("insert subagent: %w", err) |
| 3451 | } |
| 3452 | |
| 3453 | for _, app := range dc.GetApps() { |
no test coverage detected