(input string)
| 26 | ) |
| 27 | |
| 28 | func (cli *ChatCLI) handleExportCommand(input string) { |
| 29 | if len(cli.history) == 0 { |
| 30 | fmt.Println(colorize(" "+i18n.T("export.empty"), ColorGray)) |
| 31 | return |
| 32 | } |
| 33 | |
| 34 | path := strings.TrimSpace(strings.TrimPrefix(input, "/export")) |
| 35 | if path == "" { |
| 36 | dir, err := defaultExportDir() |
| 37 | if err != nil { |
| 38 | fmt.Printf(" %s %v\n", colorize("ERR", ColorRed), err) |
| 39 | return |
| 40 | } |
| 41 | path = filepath.Join(dir, fmt.Sprintf("trajectory-%s.jsonl", time.Now().Format("20060102-150405"))) |
| 42 | } |
| 43 | if expanded, err := utils.ExpandPath(path); err == nil { |
| 44 | path = expanded |
| 45 | } |
| 46 | |
| 47 | if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { |
| 48 | fmt.Printf(" %s %v\n", colorize("ERR", ColorRed), err) |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | f, err := os.Create(path) //#nosec G304 -- user-provided export path |
| 53 | if err != nil { |
| 54 | fmt.Printf(" %s %v\n", colorize("ERR", ColorRed), err) |
| 55 | return |
| 56 | } |
| 57 | defer func() { _ = f.Close() }() |
| 58 | |
| 59 | n, err := trajectory.WriteJSONL(f, cli.history) |
| 60 | if err != nil { |
| 61 | fmt.Printf(" %s %v\n", colorize("ERR", ColorRed), err) |
| 62 | return |
| 63 | } |
| 64 | |
| 65 | fmt.Printf(" %s %s\n", colorize("OK", ColorGreen), i18n.T("export.done", n, path)) |
| 66 | } |
| 67 | |
| 68 | func defaultExportDir() (string, error) { |
| 69 | home, err := utils.GetHomeDir() |
no test coverage detected