Include chat, weather, image, travel, streaming ASR and text examples, shared model initialization, documentation, and existing tests. Co-authored-by: TRAE CLI <traecli@bytedance.com>
52 lines
945 B
Go
52 lines
945 B
Go
package terminal
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"io"
|
|
)
|
|
|
|
type inputLine struct {
|
|
text string
|
|
err error
|
|
}
|
|
|
|
// terminalInput 统一读取标准输入,聊天、录音结束和生图确认顺序消费。
|
|
type terminalInput struct {
|
|
lines <-chan inputLine
|
|
}
|
|
|
|
func newInput(ctx context.Context, reader io.Reader) *terminalInput {
|
|
lines := make(chan inputLine)
|
|
go func() {
|
|
defer close(lines)
|
|
scanner := bufio.NewScanner(reader)
|
|
for scanner.Scan() {
|
|
select {
|
|
case lines <- inputLine{text: scanner.Text()}:
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
if err := scanner.Err(); err != nil {
|
|
select {
|
|
case lines <- inputLine{err: err}:
|
|
case <-ctx.Done():
|
|
}
|
|
}
|
|
}()
|
|
return &terminalInput{lines: lines}
|
|
}
|
|
|
|
func (i *terminalInput) next(ctx context.Context) (string, error) {
|
|
select {
|
|
case <-ctx.Done():
|
|
return "", ctx.Err()
|
|
case line, ok := <-i.lines:
|
|
if !ok {
|
|
return "", io.EOF
|
|
}
|
|
return line.text, line.err
|
|
}
|
|
}
|