Files
coder/coderd/x/chatd/projects.go
T
Jaayden Halko 3216580614 feat: load project skills in chats (#30838)
Chats that belong to a chat project now get the project's skills, next
to personal, organization and workspace skills. They show up in the
model's skill list, load through `read_skill`, and appear in a "Project
skills" group in the `/` menu.

- `coderd/x/skills` adds the `project` source. `MergeSkills` now takes a
list of sources in merge order (personal, organization, project,
workspace). Bare names stay bare unless two sources share a name, in
which case they get `project/<name>` style aliases.
- chatd loads project skills only when the chat owner owns the project
or the project is shared with them. Role grants don't count. This is the
same `usableChatProject` check that project instructions and memory use,
and one check per turn covers both instructions and skills. Disabled
skills are skipped. A failed load logs a warning and the turn continues
without them. Subagents use their root chat's project; that lookup is
now a helper shared with project instructions.
- The site loads project skills for the composer on the project page and
in project chats, behind the `chat-projects` experiment. `/clear` and
`/compact` defer to a project skill with the same name, as they already
do for other sources. When the user can't use the project, the site
treats it as no project skills, like chatd, so those commands keep
working and the `/` menu shows no retry prompt for project skills. That
covers a 403 or 404 from the project skills request and a project
missing from the user's project list, which holds only owned and shared
projects, so an administrator role that can read the project doesn't
count. Other failures are retried and leave the list unknown. A cached
project list is also unknown while it refetches or after a failed
refetch, so a share revoked since it was cached keeps those commands
pending instead of deferring to stale project skills.
- Docs gain a "Project skills" section in the extending agents page.
`coderd/x/chatd/ARCHITECTURE.md` gains a paragraph on project skills.

Dogfood UAT on this branch passed: project skills appear in the project
page `/` menu and in project chats, `read_skill` loads them, disabled
and deleted skills report not found, chats outside the project don't see
them, and a subagent gets them through its root chat. The site reads
project skills from the chat's own `project_id`, so subagent chats show
them in the composer once sub-chats carry their root's project (#30683).

| Project page `/` menu | `read_skill` in a project chat |
| --- | --- |
| <img alt="Project skills group in the project page slash menu"
src="https://raw.githubusercontent.com/coder/coder/assets/pr-30838-project-skills/pr/project-page-slash-menu.png"
width="600" /> | <img alt="read_skill loading a project skill in a
project chat"
src="https://raw.githubusercontent.com/coder/coder/assets/pr-30838-project-skills/pr/project-chat-read-skill.png"
width="600" /> |

Stack (merge top to bottom):
- #30838 👈
- #30854
- #30855
- #30856
- #30861
- #30880
- #30881
- #30882
- #30884
- #31122
2026-10-21 19:47:50 +07:00

62 lines
2.3 KiB
Go

package chatd
import (
"context"
"database/sql"
"errors"
"github.com/google/uuid"
"golang.org/x/xerrors"
"cdr.dev/slog/v3"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
)
// usableChatProject loads the project and reports whether the chat owner can
// use it. It reports false when the project is missing, the lookup or access
// check fails, or the owner cannot use it, and logs every case except a
// missing project.
func (p *Server) usableChatProject(ctx context.Context, logger slog.Logger, projectID uuid.UUID, chat database.Chat) (database.ChatProject, bool) {
logger = logger.With(slog.F("project_id", projectID))
project, err := p.db.GetChatProjectByID(ctx, projectID)
if err != nil {
// A project deleted mid-turn is expected.
if !errors.Is(err, sql.ErrNoRows) {
logger.Warn(ctx, "failed to load chat project, turn runs without project memory, instructions, or skills", slog.Error(err))
}
return database.ChatProject{}, false
}
usable, err := ChatProjectUsableBy(ctx, p.db, project, chat.OwnerID)
if err != nil {
logger.Warn(ctx, "failed to check chat project access, turn runs without project memory, instructions, or skills", slog.Error(err))
return database.ChatProject{}, false
}
if !usable {
logger.Debug(ctx, "chat owner cannot use chat project, turn runs without project memory, instructions, or skills",
slog.F("project_owner_id", project.OwnerID))
return database.ChatProject{}, false
}
return project, true
}
// ChatProjectUsableBy reports whether userID owns project or has an ACL
// grant on it. Role grants do not count, because the chat would read memory,
// instructions, and skills the project owner did not share. Memory tools and
// instruction and skill loading run as chatd, so they must use this check,
// not dbauthz.
func ChatProjectUsableBy(ctx context.Context, db database.Store, project database.ChatProject, userID uuid.UUID) (bool, error) {
if project.OwnerID == userID {
return true, nil
}
//nolint:gocritic // The check answers for the chat owner, not the caller.
usable, err := db.IsChatProjectUsableByUserID(dbauthz.AsChatd(ctx), database.IsChatProjectUsableByUserIDParams{
ProjectID: project.ID,
UserID: userID,
})
if err != nil {
return false, xerrors.Errorf("check chat project access: %w", err)
}
return usable, nil
}