]> git.giacomolli.uk - onechat/blob - main.go
Import WhatsApp history into a child
[onechat] / main.go
1 // Command onechat runs the agent daemon and its terminal client.
2 package main
3
4 import (
5         "context"
6         "fmt"
7         "os"
8         "os/signal"
9         "strings"
10         "syscall"
11 )
12
13 // usage is the help text of the command.
14 const usage = `Usage: onechat [COMMAND]
15
16 Commands:
17   backup FILE              archive the running daemon data
18   restore FILE             restore into an empty data directory
19   chat                     talk to the agent in this terminal (default)
20   daemon                   run the agent, Telegram, and WhatsApp
21   import [-n] [--agent NAME] ZIP...
22                            import WhatsApp history (stop the daemon first)
23     -n                     print the plan without model calls
24     --agent NAME           import into an existing direct child (default: root)
25   login                    log in to ChatGPT with a device code
26   probe LOG COUNT [BUILDS] measure the compactor on a log, with real calls
27   version                  show the build version
28   help                     show this help
29
30 Chat needs a running daemon. ONECHAT_DIR sets the data directory.
31 `
32
33 // version is set by the package build and stays dev for direct Go builds.
34 var version = "dev"
35
36 func main() {
37         err := run()
38         if err != nil {
39                 fmt.Fprintln(os.Stderr, "onechat:", err)
40                 os.Exit(1)
41         }
42 }
43
44 func run() error {
45         ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
46         defer cancel()
47
48         args := os.Args[1:]
49         switch {
50         case len(args) == 1 && (args[0] == "help" || args[0] == "--help" || args[0] == "-h"):
51                 _, err := fmt.Print(usage)
52                 if err != nil {
53                         return fmt.Errorf("write command help: %w", err)
54                 }
55         case len(args) == 1 && args[0] == "version":
56                 _, err := fmt.Println(version)
57                 if err != nil {
58                         return fmt.Errorf("write build version: %w", err)
59                 }
60         case len(args) == 1 && args[0] == "login":
61                 err := chatgptLogin(ctx, os.Stdout)
62                 if err != nil {
63                         return err
64                 }
65
66         // A signal is the normal stop of the daemon and the client, so their
67         // result does not include ctx.Err().
68         case len(args) == 1 && args[0] == "daemon":
69                 return daemon(ctx, os.Stdout)
70         case len(args) == 0 || len(args) == 1 && args[0] == "chat":
71                 return terminal(ctx, os.Stdin, os.Stdout)
72         case len(args) == 2 && args[0] == "backup":
73                 return backup(ctx, args[1], os.Stdout)
74         case len(args) == 2 && args[0] == "restore":
75                 return restore(ctx, args[1], os.Stdout)
76         case len(args) > 0 && args[0] == "import":
77                 return whatsappImport(ctx, args[1:], os.Stdout)
78         case len(args) > 0 && args[0] == "probe":
79                 err := probe(ctx, args[1:], os.Stdout)
80                 if err != nil {
81                         return err
82                 }
83         default:
84                 return fmt.Errorf("unknown command %q\n\n%s", strings.Join(args, " "), strings.TrimSuffix(usage, "\n"))
85         }
86
87         return ctx.Err()
88 }