UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

255 lines (254 loc) • 12.5 kB
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { l as isRecord, o as asRecord, t as asNonArrayRecord } from "./record-coerce-CVPuDqcc.mjs"; import { r as readTrimmedStringAlias, t as isStringOption } from "./string-readers-BtMVaphq.mjs"; //#region src/talk/provider-types.ts function realtimeVoiceAudioDurationMs(e, t) { const n = e.encoding === "pcm16" ? 2 : 1; return t * 1e3 / (e.sampleRateHz * e.channels * n); } function toOpenAICompatibleRealtimeAudioFormat(e) { return e.encoding === "pcm16" ? { type: "audio/pcm", rate: 24e3 } : { type: "audio/pcmu" }; } const REALTIME_VOICE_AUDIO_FORMAT_G711_ULAW_8KHZ = { encoding: "g711_ulaw", sampleRateHz: 8e3, channels: 1 }; const REALTIME_VOICE_AUDIO_FORMAT_PCM16_24KHZ = { encoding: "pcm16", sampleRateHz: 24e3, channels: 1 }; /** Normalizes OpenAI-style realtime response status details into the shared Talk contract. */ function normalizeRealtimeVoiceResponseOutcome(e) { const t = isRecord(e.response) ? e.response : void 0; const n = isRecord(t?.status_details) ? t.status_details : void 0; const r = isRecord(n?.error) ? n.error : void 0; const i = normalizeOptionalString(r?.code); const a = normalizeOptionalString(r?.message); const o = normalizeOptionalString(r?.type); const s = i || a || o ? { ...i ? { code: i } : {}, ...a ? { message: a } : {}, ...o ? { type: o } : {} } : void 0; const c = normalizeOptionalString(n?.reason); const l = normalizeOptionalString(t?.id) ?? normalizeOptionalString(e.responseId); const u = l ? { responseId: l } : {}; switch (t?.status) { case "completed": return { ...u, status: "completed" }; case "cancelled": return { ...u, status: "cancelled", ...c ? { reason: c } : {} }; case "failed": case "incomplete": { const n = t.status; const r = [c, a ?? i ?? o].filter(Boolean).join(": "); return { ...u, status: n, ...c ? { reason: c } : {}, ...s ? { error: s } : {}, message: `${e.providerLabel} response ${n}${r ? `: ${r}` : ""}` }; } default: { const n = normalizeOptionalString(t?.status); const r = n ? `invalid status ${n}` : "missing terminal status"; return { ...u, status: "failed", reason: "invalid_response_status", error: { type: "invalid_response_status", message: r }, message: `${e.providerLabel} response failed: ${r}` }; } } } //#endregion //#region src/talk/agent-run-control-shared.ts /** * This module owns the provider-facing control tool, conservative intent * classifier, and user-visible status/queue/cancel messages used by Talk. */ const REALTIME_VOICE_AGENT_CONTROL_MODES = [ "status", "steer", "cancel", "followup" ]; /** Stable provider-facing tool name for active-run voice control. */ const REALTIME_VOICE_AGENT_CONTROL_TOOL_NAME = "openclaw_agent_control"; const REALTIME_VOICE_AGENT_CONTROL_TOOL = { type: "function", name: REALTIME_VOICE_AGENT_CONTROL_TOOL_NAME, description: "Control an active OpenClaw tool-backed voice run. Use this when the caller asks in any language for status/progress, cancellation, a redirect/change to the active work, or a follow-up after the current work. Do not use this for ordinary greetings or chatter unless the caller is asking about the active work.", parameters: { type: "object", properties: { text: { type: "string", description: "The caller's exact spoken request or a concise semantic equivalent." }, mode: { type: "string", enum: REALTIME_VOICE_AGENT_CONTROL_MODES, description: "status for progress questions, cancel for stop/abort, steer for changing the current work, followup for work to do after the current result." } }, required: ["text", "mode"] } }; function normalizeRealtimeVoiceAgentControlMode(e) { const t = normalizeOptionalLowercaseString(e); return isStringOption(t, REALTIME_VOICE_AGENT_CONTROL_MODES) ? t : void 0; } const CANCEL_CONTROL_PATTERNS = [ /^(?:(?:ok|okay|alright|all right)[,\s]+)?(?:please\s+)?(?:cancel|cancle|abort)(?:\s+(?:that|this|it|the\s+(?:check|run|task|work)))?(?:\s*[.!?])?$/, /^(?:(?:ok|okay|alright|all right)[,\s]+)?(?:please\s+)?(?:never mind|nevermind|forget it|kill it|end that)(?:\s*[.!?])?$/, /^(?:(?:ok|okay|alright|all right)[,\s]+)?(?:please\s+)?stop(?:\s+(?:that|this|it|the\s+(?:check|run|task|work)))?(?:\s*[.!?])?$/, /^(?:(?:ok|okay|alright|all right)[,\s]+)?(?:can|could|would)\s+you\s+(?:please\s+)?(?:cancel|cancle|stop|abort)(?:\s+(?:that|this|it|the\s+(?:check|run|task|work)))?(?:\s*[.!?])?$/, /^(?:(?:ok|okay|alright|all right|actually)[,\s]+)?(?:can|could|would)\s+(?:we|you)\s+(?:just\s+)?(?:cancel|cancle|stop|abort)(?:\s+(?:that|this|it|the\s+(?:check|run|task|work)))?(?:\s*[.!?])?$/, /\b(?:cancel|cancle|stop|abort)\s+(?:that|this|it|the\s+(?:check|run|task|work))\b/ ]; const STATUS_CONTROL_PATTERNS = [ /^(?:(?:ok|okay|alright|all right)[,\s]+)?(?:status|progress|update)(?:\s*[.!?])?$/, /^(?:(?:ok|okay|alright|all right)[,\s]+)?(?:give me|what'?s|any)\s+(?:an?\s+)?update(?:\s*[.!?])?$/, /^(?:(?:ok|okay|alright|all right)[,\s]+)?(where are we|what'?s happening|what (?:are you|is it) doing|what'?s it doing|how (?:is|are) (?:it|you|that|this) going|how'?s it going|are you still working|is it done|did it finish)(\b|[.!?])/ ]; const FOLLOWUP_CONTROL_PATTERNS = [/^(after that|when you'?re done|when it'?s done|next|then|also|one more thing|follow up)(\b|[,.!?])/]; const STEER_CONTROL_PATTERNS = [ /^(?:(?:ok|okay|alright|all right)[,\s]+)?(?:please\s+)?update\s+\S/, /^(?:actually|instead|change|switch|focus|use|try|prefer|make|do|check|look at|go with|redirect|steer|tell it to)\b/, /^(?:can|could|would)\s+you\s+(?:actually\s+)?(?:change|switch|focus|use|try|prefer|make|do|check|look at|go with|redirect|steer)\b/, /\b(?:instead|not that|rather than|change that|switch to|focus on|use the|try the|go with|tell it to)\b/ ]; const STOP_REDIRECT_CONTROL_PATTERNS = [ /^(?:(?:ok|okay|alright|all right)[,\s]+)?(?:please\s+)?stop\s+(?:using|doing|checking|looking at|focusing on|trying)\b/, /^(?:(?:ok|okay|alright|all right)[,\s]+)?(?:can|could|would)\s+(?:you|we)\s+(?:please\s+)?stop\s+(?:using|doing|checking|looking at|focusing on|trying)\b/, /^(?:(?:ok|okay|alright|all right)[,\s]+)?(?:please\s+)?stop\s+(?:that|this|it|the\s+(?:check|run|task|work))\s+from\b/ ]; function hasNegatedCancelIntent(e) { return /\b(?:don'?t|do\s+not|not|never)\s+(?:please\s+)?(?:cancel|cancle|stop|abort|kill|end)\b/.test(e) || /\bstop\s+(?:it|that|this)\s+from\b/.test(e); } const CONTROL_INTENT_RULES = [ ["steer", STOP_REDIRECT_CONTROL_PATTERNS], ["cancel", CANCEL_CONTROL_PATTERNS], ["status", STATUS_CONTROL_PATTERNS], ["followup", FOLLOWUP_CONTROL_PATTERNS], ["steer", STEER_CONTROL_PATTERNS] ]; /** Classify raw spoken control text with conservative auto-control gating. */ function resolveRealtimeVoiceAgentControlIntent(e) { const t = normalizeRealtimeVoiceAgentControlMode(e.mode); if (t) return { mode: t, shouldAutoControl: true }; const n = e.text.trim().toLowerCase(); for (const [e, t] of CONTROL_INTENT_RULES) if ((e !== "cancel" || !hasNegatedCancelIntent(n)) && t.some((e) => e.test(n))) return { mode: e, shouldAutoControl: true }; return { mode: "status", shouldAutoControl: false }; } /** Return the best control mode for a spoken utterance, even if auto-routing is unsafe. */ function classifyRealtimeVoiceAgentControlText(e) { return resolveRealtimeVoiceAgentControlIntent({ text: e }).mode; } /** Whether a spoken utterance is safe to route automatically to the control tool. */ function shouldAutoControlRealtimeVoiceAgentText(e) { return resolveRealtimeVoiceAgentControlIntent({ text: e }).shouldAutoControl; } /** Parse provider-owned control tool args from JSON strings or object payloads. */ function parseRealtimeVoiceAgentControlToolArgs(e) { let t = e; if (typeof e === "string") { const n = e.trim(); try { t = n ? JSON.parse(n) : {}; } catch { t = { text: n }; } } const n = asNonArrayRecord(t); const r = readTrimmedStringAlias(n, [ "text", "message", "request", "query" ]); if (!r) throw new Error("text required"); return { text: r, mode: normalizeRealtimeVoiceAgentControlMode(n.mode) ?? resolveRealtimeVoiceAgentControlIntent({ text: r }).mode }; } /** Fixed user-visible failure; private execution/readiness errors stay in host diagnostics. */ const REALTIME_VOICE_AGENT_CONTROL_FAILURE_MESSAGE = "OpenClaw could not process that voice control. Please try again."; /** Build the system-style instruction that forces exact spoken status output. */ function buildRealtimeVoiceAgentControlSpeechMessage(e) { return [ "Internal OpenClaw voice control result.", "Do not delegate this message or call any tools.", "Speak this exact OpenClaw status to the voice call, without adding, removing, or rephrasing words.", `Status: ${JSON.stringify(e)}` ].join("\n"); } /** Provider result payload used when the control tool cancels active work. */ function buildRealtimeVoiceAgentCancelProviderResult(e = "Cancelled the active OpenClaw run.") { return { status: "cancelled", message: e }; } /** Wrap follow-up text so an active run treats it as deferred context. */ function buildRealtimeVoiceAgentFollowupSteeringText(e) { return [ "Spoken follow-up for the current voice call.", "If you are mid-task, incorporate this after the current step or result unless it directly changes the current task.", "", e ].join("\n"); } /** User-facing message for queue failures while steering or adding follow-up work. */ function formatRealtimeVoiceAgentQueueRejection(e, t) { if (t === "guarded_injection_unsupported") return "This agent runtime cannot safely accept scoped voice steering. Check status, cancel the run, or start a new explicit request. Update the runtime when guarded injection is supported."; if (t === "tool_authority_mismatch") return "This agent runtime cannot safely accept scoped voice steering. Check status, cancel the run, or start a new explicit request."; if (t === "compacting") return "OpenClaw is compacting the active run and cannot accept voice steering yet."; if (t === "injection_unavailable" || t === "not_streaming") return "OpenClaw has an active run, but it is not currently accepting steering."; return e === "followup" ? "OpenClaw could not queue that follow-up." : "OpenClaw could not steer the active run."; } /** Format a concise spoken status for the active or most recent voice run. */ function formatRealtimeVoiceAgentStatus(e) { const t = e.recentEvents ?? []; if (!e.active) return t.findLast((e) => e.type === "turn.ended") ? "OpenClaw finished the last voice request." : "I'm not working on an active request right now."; const n = t.findLast((e) => e.type.startsWith("tool.") && normalizeOptionalString(asRecord(e.payload).name) !== "openclaw_agent_control"); if (n) { const e = asRecord(n.payload); const t = normalizeOptionalString(e.name); const r = normalizeOptionalString(e.phase); if (n.type === "tool.call") return t ? `OpenClaw is starting ${t}.` : "OpenClaw is starting a tool."; if (n.type === "tool.result") return t ? `OpenClaw finished ${t} and is continuing.` : "OpenClaw finished a tool and is continuing."; if (n.type === "tool.progress") return t ? `OpenClaw is working in ${t}${r ? ` (${r})` : ""}.` : "OpenClaw is still working."; } if (e.activity?.activeToolName) return `OpenClaw is running ${e.activity.activeToolName}.`; if (e.activity?.activeWorkKind === "model_call") return "OpenClaw is waiting on the model."; return "OpenClaw is working on the current voice request."; } //#endregion export { toOpenAICompatibleRealtimeAudioFormat as _, buildRealtimeVoiceAgentControlSpeechMessage as a, formatRealtimeVoiceAgentQueueRejection as c, resolveRealtimeVoiceAgentControlIntent as d, shouldAutoControlRealtimeVoiceAgentText as f, realtimeVoiceAudioDurationMs as g, normalizeRealtimeVoiceResponseOutcome as h, buildRealtimeVoiceAgentCancelProviderResult as i, formatRealtimeVoiceAgentStatus as l, REALTIME_VOICE_AUDIO_FORMAT_PCM16_24KHZ as m, REALTIME_VOICE_AGENT_CONTROL_TOOL as n, buildRealtimeVoiceAgentFollowupSteeringText as o, REALTIME_VOICE_AUDIO_FORMAT_G711_ULAW_8KHZ as p, REALTIME_VOICE_AGENT_CONTROL_TOOL_NAME as r, classifyRealtimeVoiceAgentControlText as s, REALTIME_VOICE_AGENT_CONTROL_FAILURE_MESSAGE as t, parseRealtimeVoiceAgentControlToolArgs as u };