83 lines
2.1 KiB
Go
83 lines
2.1 KiB
Go
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"))
|
|
}
|