turn: add file-based stage turn foundation
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package turn
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// RenderInput renders the markdown a turn receives. The first turn receives the
|
||||
// run prompt as-is. Later turns receive the previous turn's output verbatim
|
||||
// under a heading; prompt is not repeated.
|
||||
func RenderInput(prompt string, previousOutput string) string {
|
||||
if strings.TrimSpace(previousOutput) == "" {
|
||||
return prompt
|
||||
}
|
||||
return "# Previous turn output\n\n" + previousOutput
|
||||
}
|
||||
|
||||
// MemoryEntry is one completed turn in a later turn's memory file. Output is
|
||||
// the turn's markdown body, without the outcome section. PublicFiles are paths
|
||||
// relative to the run directory, so the public prefix is retained.
|
||||
type MemoryEntry struct {
|
||||
Stage string
|
||||
Visit int
|
||||
Output string
|
||||
PublicFiles []string
|
||||
}
|
||||
|
||||
// RenderMemory renders completed turns in the order supplied. Each turn's output
|
||||
// is embedded verbatim; the file list is system-written from PublicFiles and is
|
||||
// sorted and deduplicated so the result is deterministic.
|
||||
func RenderMemory(entries []MemoryEntry) string {
|
||||
var b strings.Builder
|
||||
for i, entry := range entries {
|
||||
if i > 0 {
|
||||
b.WriteString("\n")
|
||||
}
|
||||
fmt.Fprintf(&b, "## %s · visit %d\n\n", entry.Stage, entry.Visit)
|
||||
if entry.Output != "" {
|
||||
b.WriteString(entry.Output)
|
||||
if !strings.HasSuffix(entry.Output, "\n") {
|
||||
b.WriteString("\n")
|
||||
}
|
||||
b.WriteString("\n")
|
||||
}
|
||||
b.WriteString("Files added to public/:\n")
|
||||
files := sortedUnique(entry.PublicFiles)
|
||||
if len(files) == 0 {
|
||||
b.WriteString("- _(none)_\n")
|
||||
continue
|
||||
}
|
||||
for _, file := range files {
|
||||
b.WriteString("- ")
|
||||
b.WriteString(file)
|
||||
b.WriteString("\n")
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func sortedUnique(values []string) []string {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
sorted := append([]string(nil), values...)
|
||||
sort.Strings(sorted)
|
||||
unique := sorted[:0]
|
||||
for _, value := range sorted {
|
||||
if len(unique) == 0 || unique[len(unique)-1] != value {
|
||||
unique = append(unique, value)
|
||||
}
|
||||
}
|
||||
return unique
|
||||
}
|
||||
Reference in New Issue
Block a user