feat: add realtime ASR voice chat with Cobra subcommand
This commit is contained in:
1 parent
dab8cbb55d
commit
74367f39e0
17 files changed
+977
-82
No files matched your search
@@ -0,0 +1,51 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user