chore: import clean Agent Desk snapshot (2/7)

This commit is contained in:
2026-10-16 11:57:21 +00:00
parent 79176541c7
commit 89c5231c4c
461 changed files with 60092 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
package instruction
import "strings"
type Assembler struct{}
type AssemblerInput struct {
AgentInstruction string
SkillInstruction string
ToolAppendices []string
}
type AssemblySummary struct {
SectionTitles []string
HasAgentRule bool
HasSkillRule bool
HasToolRule bool
}
type AssemblyResult struct {
Text string
Summary AssemblySummary
}
func NewAssembler() *Assembler {
return &Assembler{}
}
func (a *Assembler) Build(input AssemblerInput) string {
return a.Assemble(input).Text
}
func (a *Assembler) Assemble(input AssemblerInput) AssemblyResult {
parts := make([]string, 0, 3)
summary := AssemblySummary{SectionTitles: make([]string, 0, 3)}
if agentInstruction := strings.TrimSpace(input.AgentInstruction); agentInstruction != "" {
parts = append(parts, buildInstructionSection("Agent Rules", agentInstruction))
summary.HasAgentRule = true
summary.SectionTitles = append(summary.SectionTitles, "Agent Rules")
}
if skillInstruction := strings.TrimSpace(input.SkillInstruction); skillInstruction != "" {
parts = append(parts, buildInstructionSection("Current Skill Context", skillInstruction))
summary.HasSkillRule = true
summary.SectionTitles = append(summary.SectionTitles, "Current Skill Context")
}
if appendix := buildToolAppendix(input.ToolAppendices); appendix != "" {
parts = append(parts, buildInstructionSection("Tool Supplementary Rules", appendix))
summary.HasToolRule = true
summary.SectionTitles = append(summary.SectionTitles, "Tool Supplementary Rules")
}
return AssemblyResult{
Text: strings.TrimSpace(strings.Join(parts, "\n\n")),
Summary: summary,
}
}
func buildInstructionSection(title, body string) string {
title = strings.TrimSpace(title)
body = strings.TrimSpace(body)
if body == "" {
return ""
}
if title == "" {
return body
}
return title + ":\n" + body
}
func buildToolAppendix(input []string) string {
if len(input) == 0 {
return ""
}
parts := make([]string, 0, len(input))
for _, item := range input {
item = strings.TrimSpace(item)
if item == "" {
continue
}
parts = append(parts, item)
}
return strings.TrimSpace(strings.Join(parts, "\n\n"))
}

View File

@@ -0,0 +1,36 @@
package instruction
import (
"strings"
"testing"
)
func TestAssemblerRespectsProvidedSources(t *testing.T) {
result := NewAssembler().Assemble(AssemblerInput{
AgentInstruction: "agent-rule",
SkillInstruction: "skill-rule",
ToolAppendices: []string{"tool-rule-1", "tool-rule-2"},
})
if !strings.Contains(result.Text, "Agent Rules:\nagent-rule") {
t.Fatalf("missing agent instruction: %s", result.Text)
}
if !strings.Contains(result.Text, "Current Skill Context:\nskill-rule") {
t.Fatalf("missing skill instruction: %s", result.Text)
}
if !strings.Contains(result.Text, "Tool Supplementary Rules:\ntool-rule-1") {
t.Fatalf("missing tool appendix: %s", result.Text)
}
if !result.Summary.HasAgentRule || !result.Summary.HasSkillRule || !result.Summary.HasToolRule {
t.Fatalf("unexpected summary: %#v", result.Summary)
}
}
func TestAssemblerReturnsEmptyTextWhenInputIsEmpty(t *testing.T) {
result := NewAssembler().Assemble(AssemblerInput{})
if result.Text != "" {
t.Fatalf("expected empty assembled text, got: %s", result.Text)
}
if len(result.Summary.SectionTitles) != 0 || result.Summary.HasAgentRule || result.Summary.HasSkillRule || result.Summary.HasToolRule {
t.Fatalf("expected empty summary, got %#v", result.Summary)
}
}

View File

@@ -0,0 +1,89 @@
package instruction
import (
"encoding/json"
"fmt"
"strings"
runtimetooling "agent-desk/internal/ai/runtime/tooling"
"agent-desk/internal/models"
"agent-desk/internal/pkg/toolx"
)
func BuildSelectedSkillActivationInstruction(skill *models.SkillDefinition) string {
if skill == nil {
return ""
}
lines := []string{
"Active Specialized Skill:",
fmt.Sprintf("- id: %d", skill.ID),
fmt.Sprintf("- name: %s", strings.TrimSpace(skill.Name)),
}
if desc := strings.TrimSpace(skill.Description); desc != "" {
lines = append(lines, fmt.Sprintf("- description: %s", desc))
}
lines = append(lines, "", "Execution Requirements:", "- Prioritize handling inquiries within this skill's scope this turn.", fmt.Sprintf("- When specialized handling details are needed, invoke %s to load skill documentation before proceeding.", toolx.BuiltinSkill.Name), "- If key information is missing, ask the customer for clarification first.", "- Do not invoke tools unauthorized for this skill.")
return strings.TrimSpace(strings.Join(lines, "\n"))
}
func BuildSelectedSkillDocument(skill *models.SkillDefinition, toolDefinitions []runtimetooling.MCPToolDefinition) string {
return BuildSkillDocument(skill, toolDefinitions)
}
func BuildSkillDocument(skill *models.SkillDefinition, toolDefinitions []runtimetooling.MCPToolDefinition) string {
if skill == nil {
return ""
}
lines := []string{
"Active Specialized Skill:",
fmt.Sprintf("- id: %d", skill.ID),
fmt.Sprintf("- name: %s", strings.TrimSpace(skill.Name)),
}
if desc := strings.TrimSpace(skill.Description); desc != "" {
lines = append(lines, fmt.Sprintf("- description: %s", desc))
}
if content := strings.TrimSpace(skill.Instruction); content != "" {
lines = append(lines, "", "Skill Documentation:", content)
}
if examples := parseJSONStringArray(skill.Examples); len(examples) > 0 {
lines = append(lines, "", "Typical Example Inquiries:")
for _, item := range examples {
lines = append(lines, "- "+item)
}
}
if len(toolDefinitions) > 0 {
lines = append(lines, "", "Authorized Tools for this Skill:")
for _, item := range toolDefinitions {
if strings.TrimSpace(item.ToolCode) == "" {
continue
}
line := "- " + strings.TrimSpace(item.ToolCode)
if title := strings.TrimSpace(item.Title); title != "" {
line += " | " + title
}
lines = append(lines, line)
}
}
lines = append(lines, "", "执行要求:", "- 优先遵循该技能说明完成任务。", "- 如果关键信息不足,先向用户追问。", "- 不得调用当前技能未授权的工具。")
return strings.TrimSpace(strings.Join(lines, "\n"))
}
func parseJSONStringArray(raw string) []string {
raw = strings.TrimSpace(raw)
if raw == "" {
return nil
}
var ret []string
if err := json.Unmarshal([]byte(raw), &ret); err != nil {
return nil
}
out := make([]string, 0, len(ret))
for _, item := range ret {
item = strings.TrimSpace(item)
if item == "" {
continue
}
out = append(out, item)
}
return out
}

View File

@@ -0,0 +1,36 @@
package instruction
import (
"agent-desk/internal/ai/runtime/tooling"
"agent-desk/internal/models"
"agent-desk/internal/pkg/toolx"
)
type ToolAppendixProvider struct{}
func NewToolAppendixProvider() *ToolAppendixProvider {
return &ToolAppendixProvider{}
}
type SkillInstructionProvider struct{}
func NewSkillInstructionProvider() *SkillInstructionProvider {
return &SkillInstructionProvider{}
}
func (p *SkillInstructionProvider) Resolve(selectedSkill *models.SkillDefinition) string {
return BuildSelectedSkillActivationInstruction(selectedSkill)
}
func (p *ToolAppendixProvider) Build(toolDefinitions []tooling.MCPToolDefinition, extraToolCodes map[string]string) []string {
appendixParts := make([]string, 0, 1)
toolCodes := make([]string, 0, len(toolDefinitions)+len(extraToolCodes))
for _, item := range toolDefinitions {
toolCodes = append(toolCodes, item.ToolCode)
}
for _, item := range extraToolCodes {
toolCodes = append(toolCodes, item)
}
appendixParts = append(appendixParts, toolx.BuildToolAppendicesForCodes(len(toolDefinitions) > 0, toolCodes)...)
return appendixParts
}

View File

@@ -0,0 +1,60 @@
package instruction
import (
"strings"
runtimetooling "agent-desk/internal/ai/runtime/tooling"
"agent-desk/internal/models"
)
type Service struct {
assembler *Assembler
skillInstructionProvider *SkillInstructionProvider
toolAppendixProvider *ToolAppendixProvider
}
func NewService(
assembler *Assembler,
skillProvider *SkillInstructionProvider,
toolProvider *ToolAppendixProvider,
) *Service {
if assembler == nil {
assembler = NewAssembler()
}
if skillProvider == nil {
skillProvider = NewSkillInstructionProvider()
}
if toolProvider == nil {
toolProvider = NewToolAppendixProvider()
}
return &Service{
assembler: assembler,
skillInstructionProvider: skillProvider,
toolAppendixProvider: toolProvider,
}
}
func (s *Service) Build(
aiAgent models.AIAgent,
selectedSkill *models.SkillDefinition,
toolDefinitions []runtimetooling.MCPToolDefinition,
extraToolCodes map[string]string,
) AssemblyResult {
skillInstruction := ""
toolAppendices := make([]string, 0)
if s != nil && s.skillInstructionProvider != nil {
skillInstruction = s.skillInstructionProvider.Resolve(selectedSkill)
}
if s != nil && s.toolAppendixProvider != nil {
toolAppendices = s.toolAppendixProvider.Build(toolDefinitions, extraToolCodes)
}
assembler := NewAssembler()
if s != nil && s.assembler != nil {
assembler = s.assembler
}
return assembler.Assemble(AssemblerInput{
AgentInstruction: strings.TrimSpace(aiAgent.SystemPrompt),
SkillInstruction: skillInstruction,
ToolAppendices: toolAppendices,
})
}