2026-04-14 12:00:03 +08:00
|
|
|
|
package instruction
|
2026-04-10 14:43:57 +08:00
|
|
|
|
|
2026-04-12 09:56:38 +08:00
|
|
|
|
import "strings"
|
2026-04-10 14:43:57 +08:00
|
|
|
|
|
2026-04-18 11:05:41 +08:00
|
|
|
|
type Assembler struct{}
|
2026-04-10 14:43:57 +08:00
|
|
|
|
|
2026-04-14 12:00:03 +08:00
|
|
|
|
type AssemblerInput struct {
|
2026-04-18 11:25:48 +08:00
|
|
|
|
AgentInstruction string
|
|
|
|
|
|
SkillInstruction string
|
|
|
|
|
|
ToolAppendices []string
|
2026-04-10 14:43:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-14 12:00:03 +08:00
|
|
|
|
type AssemblySummary struct {
|
2026-04-18 11:25:48 +08:00
|
|
|
|
SectionTitles []string
|
|
|
|
|
|
HasAgentRule bool
|
|
|
|
|
|
HasSkillRule bool
|
|
|
|
|
|
HasToolRule bool
|
2026-04-11 23:02:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-14 12:00:03 +08:00
|
|
|
|
type AssemblyResult struct {
|
2026-04-11 23:02:09 +08:00
|
|
|
|
Text string
|
2026-04-14 12:00:03 +08:00
|
|
|
|
Summary AssemblySummary
|
2026-04-11 23:02:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-14 12:00:03 +08:00
|
|
|
|
func NewAssembler() *Assembler {
|
2026-04-18 11:05:41 +08:00
|
|
|
|
return &Assembler{}
|
2026-04-10 14:43:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-14 12:00:03 +08:00
|
|
|
|
func (a *Assembler) Build(input AssemblerInput) string {
|
2026-04-11 23:02:09 +08:00
|
|
|
|
return a.Assemble(input).Text
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-14 12:00:03 +08:00
|
|
|
|
func (a *Assembler) Assemble(input AssemblerInput) AssemblyResult {
|
2026-04-18 11:25:48 +08:00
|
|
|
|
parts := make([]string, 0, 3)
|
|
|
|
|
|
summary := AssemblySummary{SectionTitles: make([]string, 0, 3)}
|
2026-04-10 14:43:57 +08:00
|
|
|
|
if agentInstruction := strings.TrimSpace(input.AgentInstruction); agentInstruction != "" {
|
|
|
|
|
|
parts = append(parts, buildInstructionSection("Agent 规则", agentInstruction))
|
2026-04-11 23:02:09 +08:00
|
|
|
|
summary.HasAgentRule = true
|
|
|
|
|
|
summary.SectionTitles = append(summary.SectionTitles, "Agent 规则")
|
2026-04-10 14:43:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
if skillInstruction := strings.TrimSpace(input.SkillInstruction); skillInstruction != "" {
|
|
|
|
|
|
parts = append(parts, buildInstructionSection("当前技能上下文", skillInstruction))
|
2026-04-11 23:02:09 +08:00
|
|
|
|
summary.HasSkillRule = true
|
|
|
|
|
|
summary.SectionTitles = append(summary.SectionTitles, "当前技能上下文")
|
2026-04-10 14:43:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
if appendix := buildToolAppendix(input.ToolAppendices); appendix != "" {
|
|
|
|
|
|
parts = append(parts, buildInstructionSection("工具补充规则", appendix))
|
2026-04-11 23:02:09 +08:00
|
|
|
|
summary.HasToolRule = true
|
|
|
|
|
|
summary.SectionTitles = append(summary.SectionTitles, "工具补充规则")
|
|
|
|
|
|
}
|
2026-04-14 12:00:03 +08:00
|
|
|
|
return AssemblyResult{
|
2026-04-11 23:02:09 +08:00
|
|
|
|
Text: strings.TrimSpace(strings.Join(parts, "\n\n")),
|
|
|
|
|
|
Summary: summary,
|
2026-04-10 14:43:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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"))
|
|
|
|
|
|
}
|