302e1ebaff
ASR 最终转写触发一次任务提交,复用 HTTP SubmitTask 那条共用关卡
(preflightCore/launchCore),语音只是"嘴替键盘",编排/工具/计费一行不新造。
- task_handler.go: preflight/launch 抽出无 gin 内核 preflightCore/launchCore
(preflightBlock 承载拦截态),gin 版做薄封装;语音会话无 gin.Context 也走同一关卡
- voice_task.go: buildVoiceGraph(转写→input→agent 单图) + submitVoiceTask(校验/关卡/落库发射)
- voice.go: 会话升级时抓租户/会话;结果 goroutine 见 Final→onFinalTranscript 提交、
回 ServerTask{task_id};去重连发的重复 final;画布图一次性消费
- voice_task_test.go: 组图合法性 + 带转写 + input→agent 连边
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
72 lines
2.7 KiB
Go
72 lines
2.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/sundynix/sundynix-gateway/internal/dsl"
|
|
"github.com/sundynix/sundynix-shared/contract"
|
|
)
|
|
|
|
// 语音上行接线:最终转写 → 组 DSL → 复用 preflightCore/launchCore 关卡 → 提交任务 → 回 task_id。
|
|
// 语音只是"嘴替键盘",一行编排/工具/计费逻辑都不新造:走的正是 HTTP SubmitTask 那条关卡
|
|
// (见记忆 execution-single-entry「提交必走 preflight()+launch() 共用关卡」)。
|
|
|
|
// voiceAgentSystem 是语音默认单 agent 的系统提示词:口语化、简洁、适合朗读(下行要过 TTS)。
|
|
const voiceAgentSystem = "你是用户的语音助手 JARVIS。用简洁、口语化、适合朗读的中文回答," +
|
|
"避免冗长的列表和代码块;必要时可调用工具获取信息后再作答。"
|
|
|
|
// buildVoiceGraph 把一句转写组成最简可执行图:input(转写) → agent(JARVIS)。
|
|
// 与前端画布 exportDsl 同构(kind=input/agent、config.text/system),dispatcher 直接吃。
|
|
func buildVoiceGraph(query string) json.RawMessage {
|
|
g := map[string]any{
|
|
"version": "voice-1",
|
|
"nodes": []map[string]any{
|
|
{"id": "voice_in", "kind": "input", "config": map[string]any{"text": query}},
|
|
{"id": "voice_agent", "kind": "agent", "config": map[string]any{"system": voiceAgentSystem}},
|
|
},
|
|
"edges": []map[string]any{
|
|
{"source": "voice_in", "target": "voice_agent"},
|
|
},
|
|
}
|
|
b, _ := json.Marshal(g)
|
|
return b
|
|
}
|
|
|
|
// submitVoiceTask 提交一次语音任务。graphOverride 非空时用客户端画布图(语音触发既有编排),
|
|
// 否则用转写现组的单 agent 图。返回 task_id。
|
|
func (s *voiceSession) submitVoiceTask(transcript, graphOverride string) (string, error) {
|
|
transcript = strings.TrimSpace(transcript)
|
|
if transcript == "" && graphOverride == "" {
|
|
return "", fmt.Errorf("空转写")
|
|
}
|
|
ctx := context.Background() // WS 会话长生命周期,不绑单条请求 ctx
|
|
|
|
var raw json.RawMessage
|
|
if strings.TrimSpace(graphOverride) != "" {
|
|
raw = json.RawMessage(graphOverride) // 语音触发画布上的既有编排图
|
|
} else {
|
|
raw = buildVoiceGraph(transcript)
|
|
}
|
|
task, err := dsl.ParseAndAssemble(raw)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// 共用关卡:预算 / 暂停 / 计费租户 / 积分硬拦截。被拦时把文案上抛(供语音播报/回传)。
|
|
billingTenant, block := s.h.preflightCore(ctx, s.uid, s.tenantID)
|
|
if block != nil {
|
|
return "", fmt.Errorf("%s", block.message())
|
|
}
|
|
task.Meta[contract.MetaUserID] = s.uid
|
|
task.Meta[contract.MetaTenantID] = billingTenant
|
|
task.Meta[contract.MetaSessionID] = s.sessionID
|
|
|
|
if err := s.h.launchCore(ctx, s.uid, task); err != nil {
|
|
return "", err
|
|
}
|
|
return task.ID, nil
|
|
}
|