Skip to content

feat: add has-ai-task filters to the /workspaces and /templates endpoints #18387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 61 additions & 8 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,17 @@ func isDeprecated(template database.Template) bool {
return template.Deprecated != ""
}

func (q *FakeQuerier) getWorkspaceBuildParametersNoLock(workspaceBuildID uuid.UUID) ([]database.WorkspaceBuildParameter, error) {
params := make([]database.WorkspaceBuildParameter, 0)
for _, param := range q.workspaceBuildParameters {
if param.WorkspaceBuildID != workspaceBuildID {
continue
}
params = append(params, param)
}
return params, nil
}

func (*FakeQuerier) AcquireLock(_ context.Context, _ int64) error {
return xerrors.New("AcquireLock must only be called within a transaction")
}
Expand Down Expand Up @@ -7898,14 +7909,7 @@ func (q *FakeQuerier) GetWorkspaceBuildParameters(_ context.Context, workspaceBu
q.mutex.RLock()
defer q.mutex.RUnlock()

params := make([]database.WorkspaceBuildParameter, 0)
for _, param := range q.workspaceBuildParameters {
if param.WorkspaceBuildID != workspaceBuildID {
continue
}
params = append(params, param)
}
return params, nil
return q.getWorkspaceBuildParametersNoLock(workspaceBuildID)
}

func (q *FakeQuerier) GetWorkspaceBuildStatsByTemplates(ctx context.Context, since time.Time) ([]database.GetWorkspaceBuildStatsByTemplatesRow, error) {
Expand Down Expand Up @@ -13233,6 +13237,18 @@ func (q *FakeQuerier) GetAuthorizedTemplates(ctx context.Context, arg database.G
continue
}
}

if arg.HasAITask.Valid {
tv, err := q.getTemplateVersionByIDNoLock(ctx, template.ActiveVersionID)
if err != nil {
return nil, xerrors.Errorf("get template version: %w", err)
}
tvHasAITask := tv.HasAITask.Valid && tv.HasAITask.Bool
if tvHasAITask != arg.HasAITask.Bool {
continue
}
}

templates = append(templates, template)
}
if len(templates) > 0 {
Expand Down Expand Up @@ -13562,6 +13578,43 @@ func (q *FakeQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg database.
}
}

if arg.HasAITask.Valid {
hasAITask, err := func() (bool, error) {
build, err := q.getLatestWorkspaceBuildByWorkspaceIDNoLock(ctx, workspace.ID)
if err != nil {
return false, xerrors.Errorf("get latest build: %w", err)
}
if build.HasAITask.Valid {
return build.HasAITask.Bool, nil
}
// If the build has a nil AI task, check if the job is in progress
// and if it has a non-empty AI Prompt parameter
job, err := q.getProvisionerJobByIDNoLock(ctx, build.JobID)
if err != nil {
return false, xerrors.Errorf("get provisioner job: %w", err)
}
if job.CompletedAt.Valid {
return false, nil
}
parameters, err := q.getWorkspaceBuildParametersNoLock(build.ID)
if err != nil {
return false, xerrors.Errorf("get workspace build parameters: %w", err)
}
for _, param := range parameters {
if param.Name == "AI Prompt" && param.Value != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: this should use the const I'm introducing in the provider: https://github.com/coder/terraform-provider-coder/blob/dk/coder-ai-task-res/provider/ai_task.go#L22

return true, nil
}
}
return false, nil
}()
if err != nil {
return nil, xerrors.Errorf("get hasAITask: %w", err)
}
if hasAITask != arg.HasAITask.Bool {
continue
}
}

// If the filter exists, ensure the object is authorized.
if prepared != nil && prepared.Authorize(ctx, workspace.RBACObject()) != nil {
continue
Expand Down
3 changes: 3 additions & 0 deletions coderd/database/modelqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (q *sqlQuerier) GetAuthorizedTemplates(ctx context.Context, arg GetTemplate
arg.FuzzyName,
pq.Array(arg.IDs),
arg.Deprecated,
arg.HasAITask,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -264,6 +265,7 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
arg.LastUsedBefore,
arg.LastUsedAfter,
arg.UsingActive,
arg.HasAITask,
arg.RequesterID,
arg.Offset,
arg.Limit,
Expand Down Expand Up @@ -311,6 +313,7 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
&i.LatestBuildError,
&i.LatestBuildTransition,
&i.LatestBuildStatus,
&i.LatestBuildHasAITask,
&i.Count,
); err != nil {
return nil, err
Expand Down
78 changes: 58 additions & 20 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading