Adds two per-skill settings that the project skills UI will expose next: - `action` marks a project skill to show as an action button above project chat composers. A `skills_action_project_only` check keeps it to project skills; the personal and organization routes reject `action` with 400. - `model_invocable` (default true, every stored source) controls automatic use. When it's false the skill is left out of `<available-skills>` and `read_skill` reports it as not found, but a structured skill invocation (a chip from the `/` menu or an action) still loads it, as long as the skill is enabled and the chat owner can read it. Workspace skills always count as model-invocable. Migration 000625 adds both columns with defaults, so existing skills keep today's behavior. The list, get, insert and update queries, `codersdk.SkillMetadata` and the create/update requests carry both fields, and the audit table tracks them. `coderd/x/chatd/ARCHITECTURE.md` gets TODO notes for the author. **NOTE**: the migration adds two columns with constant defaults and a CHECK constraint to `skills`. Stack (merge top to bottom): - #30826 - #30827 - #30838 - #30840 - #30854 - #30855 - #30856 - #30861 - #30880 - #30881 - #30882 👈 - #30884 - #31122 - #31126 --------- Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: Nick Vigilante <nickvigilante@users.noreply.github.com>
163 lines
5.3 KiB
Go
163 lines
5.3 KiB
Go
package chatd
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
"sync"
|
|
|
|
"github.com/google/uuid"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
"github.com/coder/coder/v2/coderd/rbac"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chattool"
|
|
skillspkg "github.com/coder/coder/v2/coderd/x/skills"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
// MaxSkillInvocationsPerMessage bounds the skill-invocation parts in one
|
|
// message, and with them the skill bodies a message stores.
|
|
const MaxSkillInvocationsPerMessage = 8
|
|
|
|
// ErrTooManySkillInvocations reports a message with more than
|
|
// MaxSkillInvocationsPerMessage skill-invocation parts.
|
|
var ErrTooManySkillInvocations = xerrors.Errorf("a message can invoke at most %d skills", MaxSkillInvocationsPerMessage)
|
|
|
|
// SkillInvocationUnavailableError reports a skill-invocation part whose
|
|
// skill the chat owner cannot use: it is missing, disabled, or not readable.
|
|
type SkillInvocationUnavailableError struct {
|
|
Source codersdk.ChatSkillSource
|
|
Name string
|
|
}
|
|
|
|
func (e *SkillInvocationUnavailableError) Error() string {
|
|
return fmt.Sprintf("Skill %s/%s is not available.", e.Source, e.Name)
|
|
}
|
|
|
|
// resolveChatSkillInvocations loads the chat and resolves the
|
|
// skill-invocation parts of a message sent to it. The chat is only read
|
|
// when the message invokes a skill.
|
|
func (p *Server) resolveChatSkillInvocations(
|
|
ctx context.Context,
|
|
chatID uuid.UUID,
|
|
parts []codersdk.ChatMessagePart,
|
|
) ([]codersdk.ChatMessagePart, error) {
|
|
if !slices.ContainsFunc(parts, isSkillInvocation) {
|
|
return parts, nil
|
|
}
|
|
chat, err := p.db.GetChatByID(ctx, chatID)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("load chat for skill invocations: %w", err)
|
|
}
|
|
return p.resolveSkillInvocations(ctx, chat, parts)
|
|
}
|
|
|
|
// resolveSkillInvocations stores the current body of every invoked skill on
|
|
// its part, read with the chat owner's permissions through the same loaders
|
|
// as read_skill. Skills load by source and name, so an invocation does not
|
|
// depend on the skill being listed in the turn's skill index, and skills
|
|
// that are not model-invocable still load. Workspace
|
|
// skills come from the chat's pinned context snapshot, so a chat without
|
|
// one cannot invoke them.
|
|
func (p *Server) resolveSkillInvocations(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
parts []codersdk.ChatMessagePart,
|
|
) ([]codersdk.ChatMessagePart, error) {
|
|
invocations := countSkillInvocations(parts)
|
|
if invocations == 0 {
|
|
return parts, nil
|
|
}
|
|
if invocations > MaxSkillInvocationsPerMessage {
|
|
return nil, ErrTooManySkillInvocations
|
|
}
|
|
loadOwner := sync.OnceValues(func() (rbac.Subject, error) {
|
|
return p.chatOwnerSubject(ctx, chat.OwnerID)
|
|
})
|
|
loadWorkspaceSkills := sync.OnceValues(func() ([]chattool.SkillMeta, error) {
|
|
_, skills, err := p.resolveTurnWorkspaceContext(ctx, chat, database.WorkspaceAgent{})
|
|
return skills, err
|
|
})
|
|
loadBody := func(part codersdk.ChatMessagePart) (string, error) {
|
|
switch part.SkillSource {
|
|
case codersdk.ChatSkillSourcePersonal:
|
|
parsed, err := p.loadPersonalSkillBody(ctx, chat.OwnerID, part.SkillName, skillLoaderUser)
|
|
return parsed.Body, err
|
|
case codersdk.ChatSkillSourceOrganization:
|
|
owner, err := loadOwner()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
parsed, err := p.loadOrganizationSkillBody(ctx, owner, chat.OrganizationID, part.SkillName, skillLoaderUser)
|
|
return parsed.Body, err
|
|
case codersdk.ChatSkillSourceProject:
|
|
// Read as chatd behind the chat owner's project usability check,
|
|
// like project skill loading for turns.
|
|
//nolint:gocritic // Access is gated on ChatProjectUsableBy for the chat owner.
|
|
chatdCtx := dbauthz.AsChatd(ctx)
|
|
projectID := p.resolveUsableChatProjectID(chatdCtx, p.logger, chat)
|
|
if !projectID.Valid {
|
|
return "", skillspkg.ErrSkillNotFound
|
|
}
|
|
parsed, err := p.loadProjectSkillBody(chatdCtx, projectID.UUID, part.SkillName, skillLoaderUser)
|
|
return parsed.Body, err
|
|
case codersdk.ChatSkillSourceWorkspace:
|
|
skills, err := loadWorkspaceSkills()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
for _, skill := range skills {
|
|
if skill.Name == part.SkillName {
|
|
return chattool.PinnedWorkspaceSkillBody(skill)
|
|
}
|
|
}
|
|
}
|
|
return "", skillspkg.ErrSkillNotFound
|
|
}
|
|
|
|
// A message that repeats a skill loads it once.
|
|
type skillKey struct {
|
|
source codersdk.ChatSkillSource
|
|
name string
|
|
}
|
|
bodies := make(map[skillKey]string, invocations)
|
|
resolved := slices.Clone(parts)
|
|
for i, part := range resolved {
|
|
if !isSkillInvocation(part) {
|
|
continue
|
|
}
|
|
key := skillKey{source: part.SkillSource, name: part.SkillName}
|
|
if body, ok := bodies[key]; ok {
|
|
resolved[i].SkillBody = body
|
|
continue
|
|
}
|
|
body, err := loadBody(part)
|
|
if errors.Is(err, skillspkg.ErrSkillNotFound) {
|
|
return nil, &SkillInvocationUnavailableError{Source: part.SkillSource, Name: part.SkillName}
|
|
}
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("load invoked skill %s/%s: %w", part.SkillSource, part.SkillName, err)
|
|
}
|
|
bodies[key] = body
|
|
resolved[i].SkillBody = body
|
|
}
|
|
return resolved, nil
|
|
}
|
|
|
|
func countSkillInvocations(parts []codersdk.ChatMessagePart) int {
|
|
count := 0
|
|
for _, part := range parts {
|
|
if isSkillInvocation(part) {
|
|
count++
|
|
}
|
|
}
|
|
return count
|
|
}
|
|
|
|
func isSkillInvocation(part codersdk.ChatMessagePart) bool {
|
|
return part.Type == codersdk.ChatMessagePartTypeSkillInvocation
|
|
}
|