Adds a structured `skill-invocation` chat part so a user message can carry an explicit skill. chatd loads that exact skill when the message is sent and puts its body into the user's prompt, so the skill always applies instead of relying on the model to notice it in `<available-skills>`. The composer chip that sends these parts comes in the next PR. - codersdk: a `skill-invocation` input part and message part with `skill_source` (`personal`, `org`, `project`, `workspace`) and `skill_name`. The resolved body is stored on the part as internal `skill_body`; it's hidden from TypeScript and stripped from every API response. - coderd rejects an unknown source or an invalid name with 400. - chatd resolves each invocation on create, send and edit with the chat owner's permissions, through the same loaders as `read_skill`: stored skills by source and name (project skills through the chat's project, with the root chat fallback for subagents) and workspace skills from the pinned context snapshot. The body is saved at send time so past turns show what the model received. A missing, disabled or unreadable skill rejects the send with 400 "Skill project/triage is not available." - The prompt renders a line saying the user invoked the skill, followed by `<invoked-skill name="project/triage">` with the sanitized body. Titles and hook prompt text show the invocation as `/project/triage`. - The site only gets no-op cases so the regenerated types compile. `coderd/x/chatd/ARCHITECTURE.md` gets a TODO for the author. 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>
105 lines
4.3 KiB
Go
105 lines
4.3 KiB
Go
package coderd_test
|
|
|
|
import (
|
|
"net/http"
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/coderd/coderdtest"
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
"github.com/coder/coder/v2/coderd/x/chatd"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatprompt"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
func TestChatSkillInvocation(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
client, db := newChatClientWithDatabase(t)
|
|
firstUser := coderdtest.CreateFirstUser(t, client.Client)
|
|
_ = createChatModel(t, client)
|
|
_, err := client.CreateUserSkill(ctx, codersdk.Me, codersdk.CreateSkillRequest{
|
|
Content: "---\nname: notes\ndescription: Notes\n---\n\nTake notes.\n",
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
for _, tt := range []struct {
|
|
name string
|
|
part codersdk.ChatInputPart
|
|
detail string
|
|
}{
|
|
{
|
|
name: "UnknownSource",
|
|
part: codersdk.ChatInputPart{Type: codersdk.ChatInputPartTypeSkillInvocation, SkillSource: "team", SkillName: "notes"},
|
|
detail: `content[0].skill_source "team" is not supported.`,
|
|
},
|
|
{
|
|
name: "InvalidName",
|
|
part: codersdk.ChatInputPart{Type: codersdk.ChatInputPartTypeSkillInvocation, SkillSource: codersdk.ChatSkillSourcePersonal, SkillName: "Bad_Name"},
|
|
detail: "content[0].skill_name must be a kebab-case skill name.",
|
|
},
|
|
} {
|
|
_, err := client.CreateChat(ctx, codersdk.CreateChatRequest{
|
|
OrganizationID: firstUser.OrganizationID,
|
|
Content: []codersdk.ChatInputPart{tt.part},
|
|
})
|
|
sdkErr := requireSDKError(t, err, http.StatusBadRequest)
|
|
require.Equal(t, "Invalid input part.", sdkErr.Message, tt.name)
|
|
require.Equal(t, tt.detail, sdkErr.Detail, tt.name)
|
|
}
|
|
|
|
tooMany := slices.Repeat([]codersdk.ChatInputPart{
|
|
{Type: codersdk.ChatInputPartTypeSkillInvocation, SkillSource: codersdk.ChatSkillSourcePersonal, SkillName: "notes"},
|
|
}, chatd.MaxSkillInvocationsPerMessage+1)
|
|
_, err = client.CreateChat(ctx, codersdk.CreateChatRequest{OrganizationID: firstUser.OrganizationID, Content: tooMany})
|
|
tooManyErr := requireSDKError(t, err, http.StatusBadRequest)
|
|
require.Equal(t, "Too many skill invocations.", tooManyErr.Message)
|
|
require.Equal(t, "content can invoke at most 8 skills.", tooManyErr.Detail)
|
|
|
|
// The body is stored with the message but never returned by the API.
|
|
chat, err := client.CreateChat(ctx, codersdk.CreateChatRequest{
|
|
OrganizationID: firstUser.OrganizationID,
|
|
Content: []codersdk.ChatInputPart{
|
|
{Type: codersdk.ChatInputPartTypeSkillInvocation, SkillSource: codersdk.ChatSkillSourcePersonal, SkillName: "notes"},
|
|
{Type: codersdk.ChatInputPartTypeText, Text: "go"},
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
messages, err := db.GetChatMessagesByChatID(dbauthz.AsSystemRestricted(ctx), database.GetChatMessagesByChatIDParams{ChatID: chat.ID})
|
|
require.NoError(t, err)
|
|
stored, err := chatprompt.ParseContent(findUserMessage(t, messages))
|
|
require.NoError(t, err)
|
|
require.Equal(t, codersdk.ChatMessagePartTypeSkillInvocation, stored[0].Type)
|
|
require.Equal(t, "Take notes.", stored[0].SkillBody)
|
|
|
|
response, err := client.GetChatMessages(ctx, chat.ID, nil)
|
|
require.NoError(t, err)
|
|
var returned []codersdk.ChatMessagePart
|
|
for _, message := range response.Messages {
|
|
for _, part := range message.Content {
|
|
if part.Type == codersdk.ChatMessagePartTypeSkillInvocation {
|
|
returned = append(returned, part)
|
|
}
|
|
}
|
|
}
|
|
require.Equal(t, []codersdk.ChatMessagePart{
|
|
codersdk.ChatMessageSkillInvocation(codersdk.ChatSkillSourcePersonal, "notes"),
|
|
}, returned)
|
|
|
|
// A skill the owner cannot load rejects the send and the edit.
|
|
missing := []codersdk.ChatInputPart{
|
|
{Type: codersdk.ChatInputPartTypeSkillInvocation, SkillSource: codersdk.ChatSkillSourcePersonal, SkillName: "missing"},
|
|
}
|
|
_, err = client.CreateChatMessage(ctx, chat.ID, codersdk.CreateChatMessageRequest{Content: missing})
|
|
sdkErr := requireSDKError(t, err, http.StatusBadRequest)
|
|
require.Equal(t, "Skill personal/missing is not available.", sdkErr.Message)
|
|
_, err = client.EditChatMessage(ctx, chat.ID, findUserMessage(t, messages).ID, codersdk.EditChatMessageRequest{Content: missing})
|
|
sdkErr = requireSDKError(t, err, http.StatusBadRequest)
|
|
require.Equal(t, "Skill personal/missing is not available.", sdkErr.Message)
|
|
}
|