| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "path/filepath" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | var sessionFileComponent = regexp.MustCompile(`^[^<>:"/\\|?*\x00-\x1f\x7f]+$`) |
| 12 | |
| 13 | // ContinueSessionPath keeps an existing transcript or allocates a new path. |
| 14 | func ContinueSessionPath(prevPath, dir, model string) string { |
| 15 | if prevPath != "" { |
| 16 | return prevPath |
| 17 | } |
| 18 | if dir == "" { |
| 19 | return "" |
| 20 | } |
| 21 | return NewSessionPath(dir, model) |
| 22 | } |
| 23 | |
| 24 | // NewSessionPath returns one portable filename below the session directory. |
| 25 | // Model labels remain hints; invalid components never become filesystem paths. |
| 26 | func NewSessionPath(dir, model string) string { |
| 27 | safe := strings.NewReplacer("/", "-", "\\", "-", ":", "-", "<", "-", ">", "-", "\"", "-", "|", "-", "?", "-", "*", "-").Replace(model) |
| 28 | if safe == "" { |
| 29 | safe = "session" |
| 30 | } |
| 31 | stamp := time.Now().UTC().Format("20060102-150405.000000000") |
| 32 | name := fmt.Sprintf("%s-%s.jsonl", stamp, safe) |
| 33 | if !sessionFileComponent.MatchString(name) { |
| 34 | return filepath.Join(dir, stamp+"-session.jsonl") |
| 35 | } |
| 36 | return filepath.Join(dir, name) |
| 37 | } |
| 38 |