16 // stopCommand is the input line that interrupts the running turn.
19 // resultLines is the most lines of a tool result that the client shows.
23 // chat runs "onechat chat": the terminal client of the daemon. Each line of
24 // input is a user message, and the line /stop interrupts the running turn. The
25 // client shows the replies, the tool calls, and their results as they happen.
26 // It ends at the end of the input or when the daemon closes the connection.
27 func chat(ctx context.Context, in io.Reader, out io.Writer) error {
33 path := filepath.Join(dir, socketFileName)
34 err = checkSocketPath(path)
40 conn, err := dialer.DialContext(ctx, "unix", path)
42 return fmt.Errorf(`connect to the daemon (start it with "onechat daemon"): %w`, err)
45 return chatSession(ctx, conn, in, out)
48 // errDaemonClosed tells that the daemon closed the connection.
49 var errDaemonClosed = errors.New("the daemon closed the connection")
51 // chatSession runs the client on conn until the input ends, the daemon closes
52 // the connection, or ctx ends, and closes conn. A read of the input that waits
53 // for a line can stay behind when the daemon closes the connection.
54 func chatSession(ctx context.Context, conn net.Conn, in io.Reader, out io.Writer) error {
55 sent := make(chan error, 1)
57 sent <- sendLines(ctx, conn, in)
60 shown := make(chan error, 1)
62 shown <- showEvents(conn, out)
68 err = errors.Join(err, conn.Close())
71 err = errors.Join(cmpError(err, errDaemonClosed), conn.Close())
80 // cmpError returns err, or fallback when err is nil.
81 func cmpError(err, fallback error) error {
89 // sendLines sends each line of in as a request until the input ends.
90 func sendLines(ctx context.Context, conn net.Conn, in io.Reader) error {
91 encoder := json.NewEncoder(conn)
92 scanner := bufio.NewScanner(in)
93 scanner.Buffer(make([]byte, 64*1024), requestBytes)
94 for ctx.Err() == nil && scanner.Scan() {
95 line := scanner.Text()
96 request := clientRequest{
101 switch strings.TrimSpace(line) {
105 request = clientRequest{
110 err := encoder.Encode(request)
112 return fmt.Errorf("send to the daemon: %w", err)
119 // showEvents writes each event from the daemon to out until the connection
121 func showEvents(conn net.Conn, out io.Writer) error {
122 scanner := bufio.NewScanner(conn)
123 scanner.Buffer(make([]byte, 64*1024), requestBytes)
126 err := json.Unmarshal(scanner.Bytes(), &event)
128 return fmt.Errorf("decode event: %w", err)
131 text := eventText(event)
136 _, err = io.WriteString(out, text+"\n")
145 // eventText returns the text that the client shows for event, or "" for none.
146 // The client shows a user message only from another source, such as telegram,
147 // because the user typed the others. A tool result shows its first resultLines
149 func eventText(event Event) string {
152 return "[connected to " + event.Text + "]"
154 if event.Source == terminalSource {
158 return "[" + event.Source + "] " + event.Text
162 return "[tool] " + event.Text
164 return "[result] " + firstLines(event.Text, resultLines)
170 return "[error] " + event.Text
176 // firstLines returns the first count lines of text, with the number of the
178 func firstLines(text string, count int) string {
179 text = strings.TrimRight(text, "\n")
180 lines := strings.Split(text, "\n")
181 if len(lines) <= count {
185 return strings.Join(lines[:count], "\n") + fmt.Sprintf("\n[%d more lines]", len(lines)-count)