docs(comments): 注释中文化 · internal/runtime/multimodal(21 文件,纯替换)
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled

- 只翻注释/文档字符串,代码零变化:go/scanner 词法流比对 21 文件 token+字面量全等,增删行数 1:1(467/467)
- 刻意保留非散文英文:JSON Schema description、工具 Description() 返回值、adapter prompt、API 路径与命令示例、环境变量名
- 逐行确认代码前缀未变,未触碰其他模块
This commit is contained in:
AskaEth committed 2026-10-04 12:28:16 +08:00
1 parent b9e042d07d
commit b0fbb8bc0d
21 files changed
+467 -467

No files matched your search

+31 -31
View File
@@ -6,11 +6,11 @@ import (
"sync"
)
// Service runs STT/TTS requests on a background worker pool so caller
// goroutines (the agent loop) are never blocked by provider latency. Each
// request is enqueued on a jobs channel and answered on a per-job result
// channel; a canceled caller context or a closed service unblocks the caller
// immediately and no worker goroutine is leaked. It is safe for concurrent use.
// Service 在后台工作池上运行 STT/TTS 请求,使调用方
// goroutine(智能体循环)绝不会被提供方延迟阻塞。每个
// 请求入队到 jobs channel,并在每个 job 自己的结果
// channel 上应答;调用方 context 被取消或服务已关闭时,
// 调用方立刻解除阻塞,且不泄漏任何 worker goroutine。它可安全并发使用。
type Service struct {
chain *Chain
jobs chan job
@@ -22,11 +22,11 @@ type Service struct {
closed bool
}
// ServiceOption customizes a Service.
// ServiceOption 定制 Service。
type ServiceOption func(*Service)
// WithWorkers sets the worker-pool size. Values <= 0 are ignored and the
// default single worker is kept.
// WithWorkers 设置工作池大小。小于等于 0 的值被忽略,
// 保留默认的单 worker。
func WithWorkers(n int) ServiceOption {
return func(s *Service) {
if n > 0 {
@@ -35,7 +35,7 @@ func WithWorkers(n int) ServiceOption {
}
}
// jobKind selects the operation a worker performs.
// jobKind 选择 worker 执行的操作。
type jobKind int
const (
@@ -43,7 +43,7 @@ const (
jobTTS
)
// job is one request enqueued by the caller.
// job 是调用方入队的一个请求。
type job struct {
kind jobKind
audio Audio
@@ -51,7 +51,7 @@ type job struct {
res chan jobResult
}
// jobResult carries the outcome written back to the caller.
// jobResult 承载写回调用方的结果。
type jobResult struct {
text string
audio Audio
@@ -59,7 +59,7 @@ type jobResult struct {
err error
}
// NewService builds a Service backed by chain and starts the worker pool.
// NewService 构造一个由 chain 支撑的 Service,并启动工作池。
func NewService(chain *Chain, opts ...ServiceOption) *Service {
bg, cancel := context.WithCancel(context.Background())
s := &Service{
@@ -79,7 +79,7 @@ func NewService(chain *Chain, opts ...ServiceOption) *Service {
return s
}
// worker consumes jobs until the service closes.
// worker 消费 job 直到服务关闭。
func (s *Service) worker() {
defer s.wg.Done()
for {
@@ -92,10 +92,10 @@ func (s *Service) worker() {
}
}
// run executes a job and delivers the result. Providers run under the service's
// background context so Close cancels in-flight work and no worker goroutine is
// leaked; the result is written to a buffered channel so a caller that already
// returned never blocks the worker.
// run 执行一个 job 并投递结果。提供方在服务的
// 后台 context 下运行,因此 Close 会取消在途工作,且不泄漏
// worker goroutine;结果写入带缓冲的 channel,因此已
// 返回的调用方绝不会阻塞该 worker。
func (s *Service) run(j job) {
res := jobResult{}
switch j.kind {
@@ -107,15 +107,15 @@ func (s *Service) run(j job) {
j.res <- res
}
// STT enqueues a transcription request and waits for the result. ctx cancels
// the wait and the underlying provider call.
// STT 入队一个转写请求并等待结果。ctx 会取消
// 等待以及底层提供方调用。
func (s *Service) STT(ctx context.Context, audio Audio) (string, error) {
text, _, err := s.STTWithProvider(ctx, audio)
return text, err
}
// STTWithProvider is STT but also reports the provider rung that produced the
// transcription.
// STTWithProvider 即 STT,但额外报告产出该转写的
// 提供方级别名。
func (s *Service) STTWithProvider(ctx context.Context, audio Audio) (string, string, error) {
res, err := s.submit(ctx, job{kind: jobSTT, audio: audio, res: make(chan jobResult, 1)})
if err != nil {
@@ -124,15 +124,15 @@ func (s *Service) STTWithProvider(ctx context.Context, audio Audio) (string, str
return res.text, res.provider, res.err
}
// TTS enqueues a synthesis request and waits for the result. ctx cancels the
// wait and the underlying provider call.
// TTS 入队一个合成请求并等待结果。ctx 会取消
// 等待以及底层提供方调用。
func (s *Service) TTS(ctx context.Context, text string) (Audio, error) {
audio, _, err := s.TTSWithProvider(ctx, text)
return audio, err
}
// TTSWithProvider is TTS but also reports the provider rung that produced the
// audio.
// TTSWithProvider 即 TTS,但额外报告产出该音频的
// 提供方级别名。
func (s *Service) TTSWithProvider(ctx context.Context, text string) (Audio, string, error) {
res, err := s.submit(ctx, job{kind: jobTTS, text: text, res: make(chan jobResult, 1)})
if err != nil {
@@ -141,13 +141,13 @@ func (s *Service) TTSWithProvider(ctx context.Context, text string) (Audio, stri
return res.audio, res.provider, res.err
}
// CloneVoice creates a custom voice, delegating to the underlying chain. It is a
// one-shot admin operation (not a worker-pool job), so it runs directly.
// CloneVoice 创建自定义音色,交由底层链处理。它是一次性
// 管理操作(不是工作池 job),因此直接运行。
func (s *Service) CloneVoice(ctx context.Context, referenceURL, prefix string) (string, error) {
return s.chain.CloneVoice(ctx, referenceURL, prefix)
}
// submit enqueues j and blocks until the result or a cancellation.
// submit 把 j 入队并阻塞,直到拿到结果或发生取消。
func (s *Service) submit(ctx context.Context, j job) (jobResult, error) {
select {
case s.jobs <- j:
@@ -166,9 +166,9 @@ func (s *Service) submit(ctx context.Context, j job) (jobResult, error) {
}
}
// Close cancels the worker pool and waits for in-flight jobs to finish. It is
// safe to call once; later calls are no-ops. In-flight provider calls are
// bounded by each provider's own context timeout, so Close returns promptly.
// Close 取消工作池并等待在途 job 结束。调用一次是
// 安全的;后续调用为空操作。在途提供方调用由各提供方
// 自己的 context 超时兜底,因此 Close 会迅速返回。
func (s *Service) Close() {
s.mu.Lock()
defer s.mu.Unlock()