]> git.giacomolli.uk - onechat/blob - main.go
Give the rebuild its own call limit
[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 chat talk to the agent in this terminal (default)
18 daemon run the agent, Telegram, and WhatsApp
19 import [-n] ZIP... import WhatsApp history (stop the daemon first)
20 -n print the plan without model calls
21 login log in to ChatGPT with a device code
22 probe LOG COUNT [BUILDS] measure the compactor on a log, with real calls
23 help show this help
24
25 Chat needs a running daemon. ONECHAT_DIR sets the data directory.
26 `
27
28 func main() {
29 err := run()
30 if err != nil {
31 fmt.Fprintln(os.Stderr, "onechat:", err)
32 os.Exit(1)
33 }
34 }
35
36 func run() error {
37 ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
38 defer cancel()
39
40 args := os.Args[1:]
41 switch {
42 case len(args) == 1 && (args[0] == "help" || args[0] == "--help" || args[0] == "-h"):
43 _, err := fmt.Print(usage)
44 if err != nil {
45 return fmt.Errorf("write command help: %w", err)
46 }
47 case len(args) == 1 && args[0] == "login":
48 err := chatgptLogin(ctx, os.Stdout)
49 if err != nil {
50 return err
51 }
52
53 // A signal is the normal stop of the daemon and the client, so their
54 // result does not include ctx.Err().
55 case len(args) == 1 && args[0] == "daemon":
56 return daemon(ctx, os.Stdout)
57 case len(args) == 0 || len(args) == 1 && args[0] == "chat":
58 return terminal(ctx, os.Stdin, os.Stdout)
59 case len(args) > 0 && args[0] == "import":
60 return whatsappImport(ctx, args[1:], os.Stdout)
61 case len(args) > 0 && args[0] == "probe":
62 err := probe(ctx, args[1:], os.Stdout)
63 if err != nil {
64 return err
65 }
66 default:
67 return fmt.Errorf("unknown command %q\n\n%s", strings.Join(args, " "), strings.TrimSuffix(usage, "\n"))
68 }
69
70 return ctx.Err()
71 }