2026-02-27 11:50:56 -05:00
|
|
|
package chatd
|
|
|
|
|
|
|
|
|
|
import (
|
2026-03-25 13:08:27 -04:00
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
2026-02-27 11:50:56 -05:00
|
|
|
"strings"
|
|
|
|
|
|
2026-04-09 10:33:00 -04:00
|
|
|
"github.com/google/uuid"
|
|
|
|
|
|
2026-03-25 13:08:27 -04:00
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
2026-03-29 21:48:17 -04:00
|
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chattool"
|
2026-02-27 11:50:56 -05:00
|
|
|
"github.com/coder/coder/v2/codersdk"
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-27 14:21:23 -05:00
|
|
|
// formatSystemInstructions builds the <workspace-context> block from
|
2026-04-04 12:45:46 -04:00
|
|
|
// agent metadata and zero or more context-file parts. Non-context-file
|
|
|
|
|
// parts in the slice are silently skipped.
|
2026-02-27 14:21:23 -05:00
|
|
|
func formatSystemInstructions(
|
|
|
|
|
operatingSystem, directory string,
|
2026-04-04 12:45:46 -04:00
|
|
|
parts []codersdk.ChatMessagePart,
|
2026-02-27 14:21:23 -05:00
|
|
|
) string {
|
2026-04-04 12:45:46 -04:00
|
|
|
hasContent := false
|
|
|
|
|
for _, part := range parts {
|
|
|
|
|
if part.Type == codersdk.ChatMessagePartTypeContextFile && part.ContextFileContent != "" {
|
|
|
|
|
hasContent = true
|
2026-02-27 14:21:23 -05:00
|
|
|
break
|
|
|
|
|
}
|
2026-02-27 11:50:56 -05:00
|
|
|
}
|
2026-04-04 12:45:46 -04:00
|
|
|
if !hasContent && operatingSystem == "" && directory == "" {
|
2026-02-27 14:21:23 -05:00
|
|
|
return ""
|
2026-02-27 11:50:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var b strings.Builder
|
2026-02-27 14:21:23 -05:00
|
|
|
_, _ = b.WriteString("<workspace-context>\n")
|
|
|
|
|
if operatingSystem != "" {
|
|
|
|
|
_, _ = b.WriteString("Operating System: ")
|
|
|
|
|
_, _ = b.WriteString(operatingSystem)
|
|
|
|
|
_, _ = b.WriteString("\n")
|
2026-02-27 11:50:56 -05:00
|
|
|
}
|
2026-02-27 14:21:23 -05:00
|
|
|
if directory != "" {
|
|
|
|
|
_, _ = b.WriteString("Working Directory: ")
|
|
|
|
|
_, _ = b.WriteString(directory)
|
|
|
|
|
_, _ = b.WriteString("\n")
|
|
|
|
|
}
|
2026-04-04 12:45:46 -04:00
|
|
|
for _, part := range parts {
|
|
|
|
|
if part.Type != codersdk.ChatMessagePartTypeContextFile || part.ContextFileContent == "" {
|
2026-02-27 14:21:23 -05:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
_, _ = b.WriteString("\nSource: ")
|
2026-04-04 12:45:46 -04:00
|
|
|
_, _ = b.WriteString(part.ContextFilePath)
|
|
|
|
|
if part.ContextFileTruncated {
|
2026-02-27 14:21:23 -05:00
|
|
|
_, _ = b.WriteString(" (truncated to 64KiB)")
|
|
|
|
|
}
|
|
|
|
|
_, _ = b.WriteString("\n")
|
2026-04-04 12:45:46 -04:00
|
|
|
_, _ = b.WriteString(part.ContextFileContent)
|
2026-02-27 14:21:23 -05:00
|
|
|
_, _ = b.WriteString("\n")
|
|
|
|
|
}
|
|
|
|
|
_, _ = b.WriteString("</workspace-context>")
|
2026-02-27 11:50:56 -05:00
|
|
|
return b.String()
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 10:33:00 -04:00
|
|
|
// latestContextAgentID returns the most recent workspace-agent ID seen
|
|
|
|
|
// on any persisted context-file part, including the skill-only sentinel.
|
|
|
|
|
// Returns uuid.Nil, false when no stamped context-file parts exist.
|
|
|
|
|
func latestContextAgentID(messages []database.ChatMessage) (uuid.UUID, bool) {
|
|
|
|
|
var lastID uuid.UUID
|
|
|
|
|
found := false
|
|
|
|
|
for _, msg := range messages {
|
|
|
|
|
if !msg.Content.Valid ||
|
|
|
|
|
!bytes.Contains(msg.Content.RawMessage, []byte(`"context-file"`)) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
var parts []codersdk.ChatMessagePart
|
|
|
|
|
if err := json.Unmarshal(msg.Content.RawMessage, &parts); err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
for _, part := range parts {
|
|
|
|
|
if part.Type != codersdk.ChatMessagePartTypeContextFile ||
|
|
|
|
|
!part.ContextFileAgentID.Valid {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
lastID = part.ContextFileAgentID.UUID
|
|
|
|
|
found = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return lastID, found
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-25 13:08:27 -04:00
|
|
|
// instructionFromContextFiles reconstructs the formatted instruction
|
|
|
|
|
// string from persisted context-file parts. This is used on non-first
|
|
|
|
|
// turns so the instruction can be re-injected after compaction
|
|
|
|
|
// without re-dialing the workspace agent.
|
|
|
|
|
func instructionFromContextFiles(
|
|
|
|
|
messages []database.ChatMessage,
|
|
|
|
|
) string {
|
2026-04-09 10:33:00 -04:00
|
|
|
filterAgentID, filterByAgent := latestContextAgentID(messages)
|
2026-04-04 12:45:46 -04:00
|
|
|
var contextParts []codersdk.ChatMessagePart
|
2026-03-25 13:08:27 -04:00
|
|
|
var os, dir string
|
|
|
|
|
for _, msg := range messages {
|
|
|
|
|
if !msg.Content.Valid ||
|
|
|
|
|
!bytes.Contains(msg.Content.RawMessage, []byte(`"context-file"`)) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
var parts []codersdk.ChatMessagePart
|
|
|
|
|
if err := json.Unmarshal(msg.Content.RawMessage, &parts); err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
for _, part := range parts {
|
|
|
|
|
if part.Type != codersdk.ChatMessagePartTypeContextFile {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2026-04-09 10:33:00 -04:00
|
|
|
if filterByAgent && part.ContextFileAgentID.Valid &&
|
|
|
|
|
part.ContextFileAgentID.UUID != filterAgentID {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2026-03-25 13:08:27 -04:00
|
|
|
if part.ContextFileOS != "" {
|
|
|
|
|
os = part.ContextFileOS
|
|
|
|
|
}
|
|
|
|
|
if part.ContextFileDirectory != "" {
|
|
|
|
|
dir = part.ContextFileDirectory
|
|
|
|
|
}
|
|
|
|
|
if part.ContextFileContent != "" {
|
2026-04-04 12:45:46 -04:00
|
|
|
contextParts = append(contextParts, part)
|
2026-03-25 13:08:27 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-04 12:45:46 -04:00
|
|
|
return formatSystemInstructions(os, dir, contextParts)
|
2026-03-25 13:08:27 -04:00
|
|
|
}
|
|
|
|
|
|
2026-04-09 10:33:00 -04:00
|
|
|
// hasPersistedInstructionFiles reports whether messages include a
|
|
|
|
|
// persisted context-file part that should suppress another baseline
|
|
|
|
|
// instruction-file lookup. The workspace-agent skill-only sentinel is
|
|
|
|
|
// ignored so default instructions still load on fresh chats.
|
|
|
|
|
func hasPersistedInstructionFiles(
|
|
|
|
|
messages []database.ChatMessage,
|
|
|
|
|
) bool {
|
|
|
|
|
for _, msg := range messages {
|
|
|
|
|
if !msg.Content.Valid ||
|
|
|
|
|
!bytes.Contains(msg.Content.RawMessage, []byte(`"context-file"`)) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
var parts []codersdk.ChatMessagePart
|
|
|
|
|
if err := json.Unmarshal(msg.Content.RawMessage, &parts); err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
for _, part := range parts {
|
|
|
|
|
if part.Type != codersdk.ChatMessagePartTypeContextFile ||
|
|
|
|
|
!part.ContextFileAgentID.Valid ||
|
|
|
|
|
part.ContextFilePath == AgentChatContextSentinelPath {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mergeSkillMetas(
|
|
|
|
|
persisted []chattool.SkillMeta,
|
|
|
|
|
discovered []chattool.SkillMeta,
|
|
|
|
|
) []chattool.SkillMeta {
|
|
|
|
|
if len(persisted) == 0 {
|
|
|
|
|
return discovered
|
|
|
|
|
}
|
|
|
|
|
if len(discovered) == 0 {
|
|
|
|
|
return persisted
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
seen := make(map[string]struct{}, len(persisted)+len(discovered))
|
|
|
|
|
merged := make([]chattool.SkillMeta, 0, len(persisted)+len(discovered))
|
|
|
|
|
appendUnique := func(skill chattool.SkillMeta) {
|
|
|
|
|
if _, ok := seen[skill.Name]; ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
seen[skill.Name] = struct{}{}
|
|
|
|
|
merged = append(merged, skill)
|
|
|
|
|
}
|
|
|
|
|
for _, skill := range discovered {
|
|
|
|
|
appendUnique(skill)
|
|
|
|
|
}
|
|
|
|
|
for _, skill := range persisted {
|
|
|
|
|
appendUnique(skill)
|
|
|
|
|
}
|
|
|
|
|
return merged
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// selectSkillMetasForInstructionRefresh chooses which skill metadata
|
|
|
|
|
// should be injected on a turn that refreshes instruction files.
|
|
|
|
|
func selectSkillMetasForInstructionRefresh(
|
|
|
|
|
persisted []chattool.SkillMeta,
|
|
|
|
|
discovered []chattool.SkillMeta,
|
|
|
|
|
currentAgentID uuid.NullUUID,
|
|
|
|
|
latestInjectedAgentID uuid.NullUUID,
|
|
|
|
|
) []chattool.SkillMeta {
|
|
|
|
|
if currentAgentID.Valid && latestInjectedAgentID.Valid && latestInjectedAgentID.UUID == currentAgentID.UUID {
|
|
|
|
|
return mergeSkillMetas(persisted, discovered)
|
|
|
|
|
}
|
|
|
|
|
if !currentAgentID.Valid && len(discovered) == 0 {
|
|
|
|
|
return persisted
|
|
|
|
|
}
|
|
|
|
|
return discovered
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 21:48:17 -04:00
|
|
|
// skillsFromParts reconstructs skill metadata from persisted
|
|
|
|
|
// skill parts. This is analogous to instructionFromContextFiles
|
|
|
|
|
// so the skill index can be re-injected after compaction without
|
|
|
|
|
// re-dialing the workspace agent.
|
|
|
|
|
func skillsFromParts(
|
|
|
|
|
messages []database.ChatMessage,
|
|
|
|
|
) []chattool.SkillMeta {
|
2026-04-09 10:33:00 -04:00
|
|
|
filterAgentID, filterByAgent := latestContextAgentID(messages)
|
2026-03-29 21:48:17 -04:00
|
|
|
var skills []chattool.SkillMeta
|
|
|
|
|
for _, msg := range messages {
|
|
|
|
|
if !msg.Content.Valid ||
|
|
|
|
|
!bytes.Contains(msg.Content.RawMessage, []byte(`"skill"`)) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
var parts []codersdk.ChatMessagePart
|
|
|
|
|
if err := json.Unmarshal(msg.Content.RawMessage, &parts); err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
for _, part := range parts {
|
|
|
|
|
if part.Type != codersdk.ChatMessagePartTypeSkill {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2026-04-09 10:33:00 -04:00
|
|
|
if filterByAgent && part.ContextFileAgentID.Valid &&
|
|
|
|
|
part.ContextFileAgentID.UUID != filterAgentID {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2026-03-29 21:48:17 -04:00
|
|
|
skills = append(skills, chattool.SkillMeta{
|
|
|
|
|
Name: part.SkillName,
|
|
|
|
|
Description: part.SkillDescription,
|
|
|
|
|
Dir: part.SkillDir,
|
2026-04-04 12:45:46 -04:00
|
|
|
MetaFile: part.ContextFileSkillMetaFile,
|
2026-03-29 21:48:17 -04:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return skills
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 12:45:46 -04:00
|
|
|
// filterSkillParts returns stripped copies of skill-type parts from
|
|
|
|
|
// the given slice. Internal fields are removed so the result is safe
|
|
|
|
|
// for the cache column. Returns nil when no skill parts exist.
|
|
|
|
|
func filterSkillParts(parts []codersdk.ChatMessagePart) []codersdk.ChatMessagePart {
|
|
|
|
|
var out []codersdk.ChatMessagePart
|
|
|
|
|
for _, p := range parts {
|
|
|
|
|
if p.Type != codersdk.ChatMessagePartTypeSkill {
|
2026-04-01 12:28:47 -04:00
|
|
|
continue
|
|
|
|
|
}
|
2026-04-04 12:45:46 -04:00
|
|
|
cp := p
|
|
|
|
|
cp.StripInternal()
|
|
|
|
|
out = append(out, cp)
|
2026-04-01 12:28:47 -04:00
|
|
|
}
|
2026-04-04 12:45:46 -04:00
|
|
|
return out
|
2026-02-27 11:50:56 -05:00
|
|
|
}
|