15 // terminalResultLines is the most lines of a tool result that the client shows.
16 const terminalResultLines = 10
18 // errTerminalDaemonClosed tells that the daemon closed the connection.
19 var errTerminalDaemonClosed = errors.New("the daemon closed the connection")
21 // terminal runs "onechat chat": the terminal client of the daemon. Each line of
22 // input is a user message, the line /stop interrupts the running turn, and the
23 // line /stop NAME stops the subagent NAME. The client shows the replies, the
24 // tool calls, and their results as they happen. It ends at the end of the input
25 // or when the daemon closes the connection.
26 func terminal(ctx context.Context, in io.Reader, out io.Writer) error {
32 path := filepath.Join(dir, socketFileName)
33 err = checkSocketPath(path)
39 conn, err := dialer.DialContext(ctx, "unix", path)
41 return fmt.Errorf(`connect to the daemon (start it with "onechat daemon"): %w`, err)
44 return terminalSession(ctx, conn, in, out)
47 // terminalSession runs the client on conn until the input ends, the daemon
48 // closes the connection, or ctx ends, and closes conn. A read of the input that
49 // waits for a line can stay behind when the daemon closes the connection.
50 func terminalSession(ctx context.Context, conn net.Conn, in io.Reader, out io.Writer) error {
51 sent := make(chan error, 1)
53 sent <- terminalSendLines(ctx, conn, in)
56 shown := make(chan error, 1)
58 shown <- terminalShowEvents(conn, out)
64 err = errors.Join(err, conn.Close())
67 err = errors.Join(terminalCmpError(err, errTerminalDaemonClosed), conn.Close())
76 // terminalSendLines sends each line of in as a request until the input ends.
77 func terminalSendLines(ctx context.Context, conn net.Conn, in io.Reader) error {
78 encoder := json.NewEncoder(conn)
79 scanner := bufio.NewScanner(in)
80 scanner.Buffer(make([]byte, 64*1024), socketRequestBytes)
81 for ctx.Err() == nil && scanner.Scan() {
82 line := scanner.Text()
83 request := SocketRequest{
88 name, stop := stopSubagentName(line)
89 switch strings.TrimSpace(line) {
93 request = SocketRequest{
98 request = SocketRequest{
105 err := encoder.Encode(request)
107 return fmt.Errorf("send to the daemon: %w", err)
114 // terminalShowEvents writes each event from the daemon to out until the
115 // connection closes.
116 func terminalShowEvents(conn net.Conn, out io.Writer) error {
117 scanner := bufio.NewScanner(conn)
118 scanner.Buffer(make([]byte, 64*1024), socketRequestBytes)
121 err := json.Unmarshal(scanner.Bytes(), &event)
123 return fmt.Errorf("decode event: %w", err)
126 text := terminalEventText(event)
131 _, err = io.WriteString(out, text+"\n")
140 // terminalCmpError returns err, or fallback when err is nil.
141 func terminalCmpError(err, fallback error) error {
149 // terminalEventText returns the text that the client shows for event, or "" for
150 // none. The client shows a user message only from another source, such as
151 // telegram, because the user typed the others. A tool result shows its first
152 // terminalResultLines lines. The report of a subagent shows whole, and it
153 // starts with the name of the subagent.
154 func terminalEventText(event Event) string {
157 return "[connected to " + event.Text + "]"
159 if event.Source == terminalSource {
163 return "[" + event.Source + "] " + event.Text
167 return "[tool] " + event.Text
169 return "[result] " + terminalFirstLines(event.Text, terminalResultLines)
171 return "[" + event.Text + "] started"
179 return "[error] " + event.Text
185 // terminalFirstLines returns the first count lines of text, with the number of
186 // the lines left out.
187 func terminalFirstLines(text string, count int) string {
188 text = strings.TrimRight(text, "\n")
189 lines := strings.Split(text, "\n")
190 if len(lines) <= count {
194 return strings.Join(lines[:count], "\n") + fmt.Sprintf("\n[%d more lines]", len(lines)-count)