openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
94 lines (93 loc) • 4.31 kB
JavaScript
import { t as createDeferredCore } from "./deferred-D0La5CRk.mjs";
import { a as captureAsyncWorkTracker, t as AsyncWorkScope } from "./async-work-scope-BXagjUap.mjs";
import { r as classifySystemAgentApprovalText } from "./operator-approval-DrWu-F-I.mjs";
import { a as resolveSystemAgentVerifiedInferenceRoute } from "./verified-inference-B9tngoud.mjs";
import { c as extractEmbeddedAssistantText } from "./embedded-agent-utils-CRYA6mcb.mjs";
import { t as acquireSimpleCompletionModelForAgent } from "./simple-completion-runtime-DzeSgPc1.mjs";
import { t as completeWithPreparedSimpleCompletionModel } from "./simple-completion-execution-BueYrE3I.mjs";
//#region src/system-agent/approval-intent.ts
const APPROVAL_INTENT_TIMEOUT_MS = 1e4;
const APPROVAL_INTENT_MAX_TOKENS = 8;
const APPROVAL_INTENT_SYSTEM_PROMPT = [
"You classify one chat message from a user who was just asked to approve a pending configuration change.",
"Reply with exactly one word:",
"approve — the message clearly consents to applying the pending change now.",
"decline — the message clearly rejects or postpones the pending change.",
"other — anything else: questions, new requests, partial or conditional agreement, or unclear intent.",
"Only classify consent for the pending change itself. A message asking to change the proposal is not approval."
].join("\n");
/**
* Judge whether a message approves the pending proposal. Closed-list answers
* short-circuit so a literal "yes" cannot be reinterpreted by the conversation
* model; ambiguous messages go to a separate configured completion call.
* CLI-harness routes do not spawn a second harness for that check, so their
* ambiguous replies stay "other" and the conversation asks for a clear yes.
*/
async function classifySystemAgentApprovalIntent(e) {
const t = classifySystemAgentApprovalText(e.message);
if (t !== "other") return t;
try {
const t = await resolveSystemAgentVerifiedInferenceRoute(e.verifiedInference);
if (!t || t.runner !== "embedded" || t.agentHarnessRuntimeOverride !== "openclaw") return "other";
const n = t.authProfileId ? `${t.modelLabel}@${t.authProfileId}` : t.modelLabel;
const r = createDeferredCore();
captureAsyncWorkTracker()(async () => {
const i = await acquireSimpleCompletionModelForAgent({
cfg: t.runConfig,
agentId: t.agentId,
agentDir: t.agentDir,
modelRef: n,
...t.authProfileId ? { preferredProfile: t.authProfileId } : {},
allowMissingApiKeyModes: ["aws-sdk"],
bindAuthOwner: true
});
if ("error" in i) {
r.resolve("other");
return;
}
const a = new AsyncWorkScope();
try {
r.resolve(await a.track(async () => {
if ((i.selection.runtimeProvider ?? i.selection.provider) !== t.provider || i.selection.modelId !== t.model || i.selection.agentDir !== t.agentDir || i.selection.profileId !== t.authProfileId || i.auth.profileId !== t.authProfileId || !e.verifiedInference.auth.authFingerprint || i.sourceAuthFingerprint !== e.verifiedInference.auth.authFingerprint) return "other";
const n = new AbortController();
const r = setTimeout(() => n.abort(), APPROVAL_INTENT_TIMEOUT_MS);
try {
const r = await completeWithPreparedSimpleCompletionModel({
model: i.model,
auth: i.auth,
cfg: t.runConfig,
context: {
systemPrompt: APPROVAL_INTENT_SYSTEM_PROMPT,
messages: [{
role: "user",
content: [`Pending change: ${e.proposal ?? "a configuration change proposed in this conversation"}`, `User message: ${e.message}`].join("\n"),
timestamp: Date.now()
}]
},
options: {
maxTokens: APPROVAL_INTENT_MAX_TOKENS,
signal: n.signal
}
});
if (!await resolveSystemAgentVerifiedInferenceRoute(e.verifiedInference)) return "other";
const a = extractEmbeddedAssistantText(r)?.trim().toLowerCase().split(/\s+/)[0];
if (a === "approve" || a === "decline") return a;
return "other";
} finally {
clearTimeout(r);
}
}));
} catch {
r.resolve("other");
} finally {
await a.drain();
await i[Symbol.asyncDispose]();
}
}).catch(() => r.resolve("other"));
return await r.promise;
} catch {
return "other";
}
}
//#endregion
export { classifySystemAgentApprovalIntent };