openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
171 lines (170 loc) • 7.87 kB
JavaScript
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs";
import { r as readTrimmedStringAlias, t as isStringOption } from "./string-readers-BtMVaphq.mjs";
//#region src/talk/agent-consult-tool.ts
/**
* Voice providers call this function tool when a spoken request needs normal
* agent tools, memory, workspace context, or current information before reply.
*/
/** Stable provider-facing tool name for realtime voice agent delegation. */
const REALTIME_VOICE_AGENT_CONSULT_TOOL_NAME = "openclaw_agent_consult";
const REALTIME_VOICE_AGENT_CONSULT_TOOL_POLICIES = [
"safe-read-only",
"owner",
"none"
];
const REALTIME_VOICE_AGENT_CONSULT_TOOL = {
type: "function",
name: REALTIME_VOICE_AGENT_CONSULT_TOOL_NAME,
description: "Delegate the caller's request to the configured OpenClaw agent for normal tool-backed work, actions, context, memory, or reasoning before speaking.",
parameters: {
type: "object",
properties: {
question: {
type: "string",
description: "The concrete question or task the user asked."
},
context: {
type: "string",
description: "Optional relevant context or transcript summary."
},
responseStyle: {
type: "string",
description: "Optional style hint for the spoken answer."
}
},
required: ["question"]
}
};
function buildRealtimeVoiceAgentConsultWorkingResponse(e = "person") {
return {
status: "working",
tool: REALTIME_VOICE_AGENT_CONSULT_TOOL_NAME,
message: `Tell the ${e} briefly that you are checking, then wait for the final OpenClaw result before answering with the actual result.`
};
}
const SAFE_READ_ONLY_TOOLS = [
"read",
"web_search",
"web_fetch",
"x_search",
"memory_search",
"memory_get"
];
function isRealtimeVoiceAgentConsultToolPolicy(e) {
return isStringOption(e, REALTIME_VOICE_AGENT_CONSULT_TOOL_POLICIES);
}
function resolveRealtimeVoiceAgentConsultToolPolicy(e, t) {
const n = normalizeOptionalLowercaseString(e);
return isRealtimeVoiceAgentConsultToolPolicy(n) ? n : t;
}
function resolveRealtimeVoiceAgentConsultTools(e, t = []) {
const n = /* @__PURE__ */ new Map();
if (e !== "none") n.set(REALTIME_VOICE_AGENT_CONSULT_TOOL.name, REALTIME_VOICE_AGENT_CONSULT_TOOL);
for (const e of t) {
const t = readRealtimeVoiceCustomToolName(e);
if (t !== void 0 && !n.has(t)) n.set(t, e);
}
return [...n.values()];
}
function readRealtimeVoiceCustomToolName(e) {
try {
const t = e.name;
return typeof t === "string" ? t : void 0;
} catch {
return;
}
}
function resolveRealtimeVoiceAgentConsultToolsAllow(e) {
if (e === "owner") return;
if (e === "safe-read-only") return [...SAFE_READ_ONLY_TOOLS];
return [];
}
function buildRealtimeVoiceAgentConsultPolicyInstructions(e) {
if (e.toolPolicy === "none" || !e.consultPolicy || e.consultPolicy === "auto") return;
if (e.consultPolicy === "always") return [
"Consult behavior:",
"- Call openclaw_agent_consult before every substantive answer.",
"- You may answer directly only for greetings, acknowledgements, brief latency tests, or filler while waiting for the consult result.",
"- After the consult result arrives, speak that result concisely."
].join("\n");
return [
"Consult behavior:",
"- Answer directly for greetings, acknowledgements, simple conversational glue, and brief latency tests.",
"- Call openclaw_agent_consult before answering requests that need facts, memory, current information, tools, workspace state, or the user's OpenClaw-specific context.",
"- Keep spoken replies concise and natural."
].join("\n");
}
function buildRealtimeVoiceSessionInstructions(e) {
const t = [
e.base,
e.bootstrapContextInstructions?.trim(),
...e.isAgentProxy ? [
"Mode: OpenClaw agent proxy.",
"You are the realtime voice surface for the same OpenClaw agent the user can message directly.",
"Do not mention a backend, supervisor, helper, or separate system. Present the result as your own work.",
"Delegate substantive requests, actions, tool work, current facts, memory, workspace context, and user-specific context with openclaw_agent_consult.",
"Do not block, refuse, or downscope at the voice layer. Delegate to OpenClaw and treat its result as authoritative.",
"Answer directly only for greetings, acknowledgements, brief latency tests, or filler while waiting."
] : [],
"While waiting for OpenClaw data or tool results, use at most one short natural backchannel such as \"yeah\", \"mm-hmm\", \"got it\", or \"one sec\"; vary it and do not treat it as the final answer.",
...e.isAgentProxy ? ["When OpenClaw sends an internal exact answer to speak, do not call tools. Say only that answer."] : [],
buildRealtimeVoiceAgentConsultPolicyInstructions({
toolPolicy: e.toolPolicy,
consultPolicy: e.consultPolicy
})
];
return (e.isAgentProxy ? t : t.filter(Boolean)).join("\n\n");
}
function parseRealtimeVoiceAgentConsultArgs(e) {
const t = asOptionalRecord(e);
const n = t && readTrimmedStringAlias(t, [
"question",
"prompt",
"query",
"task"
]);
if (!t || !n) throw new Error("question required");
return {
question: n,
context: normalizeOptionalString(t.context),
responseStyle: normalizeOptionalString(t.responseStyle)
};
}
/** Build the plain chat message used by browser/chat forwarding paths. */
function buildRealtimeVoiceAgentConsultChatMessage(e) {
const t = parseRealtimeVoiceAgentConsultArgs(e);
return [
t.question,
t.context ? `Context:\n${t.context}` : void 0,
t.responseStyle ? `Spoken style:\n${t.responseStyle}` : void 0
].filter(Boolean).join("\n\n");
}
function buildRealtimeVoiceAgentConsultPrompt(e) {
const t = parseRealtimeVoiceAgentConsultArgs(e.args);
const n = e.assistantLabel ?? "Agent";
const r = e.questionSourceLabel ?? e.userLabel.toLowerCase();
const i = e.transcript.slice(-12).map((t) => `${t.role === "assistant" ? n : e.userLabel}: ${t.text}`).join("\n");
return [
`Live voice request from the ${r} during ${e.surface}.`,
"Act as the configured OpenClaw agent on behalf of this user. Use available tools when the request asks you to do work.",
"When finished, return only the concise result the realtime voice agent should speak back.",
"Report a security or approval block only when an actual tool result says so. Distinguish tool errors from permission denials; do not invent a blocked attempt. If a read-only call fails, correct the tool or arguments and continue when possible.",
"Do not include markdown, tool logs, or private reasoning. Include citations only when the spoken answer needs them.",
t.responseStyle ? `Spoken style: ${t.responseStyle}` : void 0,
i ? `Recent voice transcript for context:\n${i}` : void 0,
t.context ? `Additional realtime context:\n${t.context}` : void 0,
`User request:\n${t.question}`
].filter(Boolean).join("\n\n");
}
function collectRealtimeVoiceAgentConsultVisibleText(e) {
const t = [];
for (const n of e) {
if (n.isError || n.isReasoning || n.isCommentary) continue;
const e = normalizeOptionalString(n.text);
if (e) t.push(e);
}
return t.length > 0 ? t.join("\n\n").trim() : null;
}
//#endregion
export { buildRealtimeVoiceAgentConsultPolicyInstructions as a, buildRealtimeVoiceSessionInstructions as c, parseRealtimeVoiceAgentConsultArgs as d, resolveRealtimeVoiceAgentConsultToolPolicy as f, buildRealtimeVoiceAgentConsultChatMessage as i, collectRealtimeVoiceAgentConsultVisibleText as l, resolveRealtimeVoiceAgentConsultToolsAllow as m, REALTIME_VOICE_AGENT_CONSULT_TOOL_NAME as n, buildRealtimeVoiceAgentConsultPrompt as o, resolveRealtimeVoiceAgentConsultTools as p, REALTIME_VOICE_AGENT_CONSULT_TOOL_POLICIES as r, buildRealtimeVoiceAgentConsultWorkingResponse as s, REALTIME_VOICE_AGENT_CONSULT_TOOL as t, isRealtimeVoiceAgentConsultToolPolicy as u };