UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

216 lines (215 loc) • 7.21 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { t as dedupeByKey } from "./dedupe-by-key-Cpowmfct.mjs"; import { i as readSessionEntryReadOnlyInWorker } from "./session-entry-read-runtime-1Fp1sybj.mjs"; import { _ as updateSessionGoalObjective, f as clearSessionGoal, h as getSessionGoal, m as formatSessionGoalStatus, p as createSessionGoal, v as updateSessionGoalStatus } from "./sessions-D5Vvu3AE.mjs"; import { c as splitCommandAction, i as matchSlashCommandToken } from "./commands-registry.data-CoxL3Db3.mjs"; import { n as applyCommandTextToParams } from "./command-context-rewrite-Ak_4dOLJ.mjs"; import { n as commandReply, r as defineAuthorizedTextCommand } from "./command-gates-BZ-IYgiD.mjs"; //#region src/auto-reply/reply/command-session-metadata.ts const commandSessionMetadataChanges = /* @__PURE__ */ new WeakMap(); function addChange(e, t) { const n = commandSessionMetadataChanges.get(e) ?? []; if (!n.some((e) => e.sessionKey === t.sessionKey && e.agentId === t.agentId)) n.push(t); commandSessionMetadataChanges.set(e, n); } function markCommandSessionMetadataChanged(e) { const t = normalizeOptionalString(e.sessionKey); if (!t) return; const n = { sessionKey: t, ...e.agentId ? { agentId: e.agentId } : {}, reason: "command-metadata" }; if (e.rootCtx && e.rootCtx !== e.ctx) addChange(e.rootCtx, n); addChange(e.ctx, n); } function takeCommandSessionMetadataChanges(e) { const t = commandSessionMetadataChanges.get(e); commandSessionMetadataChanges.delete(e); return t && t.length > 0 ? t : void 0; } function takeCommandSessionMetadataChangesFromTargets(e) { const t = dedupeByKey([...new Set(e)].flatMap((e) => takeCommandSessionMetadataChanges(e) ?? []), (e) => JSON.stringify([ e.sessionKey, e.agentId ?? null, e.reason ])); return t.length > 0 ? t : void 0; } //#endregion //#region src/auto-reply/reply/commands-goal.ts const GOAL_COMMAND_PREFIX = "/goal"; const GOAL_CONTINUATION_PROMPT_PREFIX = "Pursue this goal exactly as written from this JSON string:"; const GOAL_RESUME_NOTE_PROMPT_PREFIX = "Continue pursuing the current goal. Interpret this JSON string as the resume note:"; const GOAL_ACTIONS = /* @__PURE__ */ new Set([ "block", "blocked", "clear", "complete", "create", "done", "edit", "pause", "resume", "set", "start", "status" ]); /** Parses /goal action text, defaulting unknown actions to goal creation. */ function parseGoalCommand(e) { const t = matchSlashCommandToken(e, GOAL_COMMAND_PREFIX); if (t === null) return null; const { action: n, args: r } = splitCommandAction(t, "status"); if (!GOAL_ACTIONS.has(n)) return { action: "start", text: t }; return { action: n, text: r }; } async function refreshGoalSessionEntry(e) { if (!e.sessionStore || !e.sessionKey) return; const t = await readSessionEntryReadOnlyInWorker({ sessionKey: e.sessionKey, storePath: e.storePath }); if (!t) return; e.sessionStore[e.sessionKey] = t; e.sessionEntry = t; } function hasCommandLikeGoalText(e) { return /(?:^|\s)\//.test(e) || e.startsWith("!"); } function encodeGoalJsonString(e) { return JSON.stringify(e).replaceAll("/", "\\/"); } function formatGoalContinuationPrompt(e) { const t = e.trim(); return hasCommandLikeGoalText(t) ? `${GOAL_CONTINUATION_PROMPT_PREFIX} ${encodeGoalJsonString(t)}` : t; } function formatGoalResumeContinuationPrompt(e) { const t = e.trim(); if (!t) return "Continue pursuing the current goal."; return hasCommandLikeGoalText(t) ? `${GOAL_RESUME_NOTE_PROMPT_PREFIX} ${encodeGoalJsonString(t)}` : `Continue pursuing the current goal. Note: ${t}`; } /** Returns true for internally generated goal continuation prompts. */ function isFormattedGoalContinuationPrompt(e) { const t = e.trim(); return t.startsWith(GOAL_CONTINUATION_PROMPT_PREFIX) || t.startsWith(GOAL_RESUME_NOTE_PROMPT_PREFIX); } /** Execute goal storage policy once for auto-reply, Gateway, and embedded callers. */ async function executeSessionGoalCommand(e) { const t = { sessionKey: e.sessionKey, storePath: e.storePath, actor: { type: "human" }, agentId: e.agentId }; const n = e.parsed.text ? { note: e.parsed.text } : {}; switch (e.parsed.action) { case "status": { const t = await getSessionGoal({ sessionKey: e.sessionKey, storePath: e.storePath, ...e.readOnlyStatus ? { fallbackEntry: e.fallbackEntry, persist: false } : {} }); return { text: formatSessionGoalStatus(t.goal), changed: false }; } case "start": case "set": case "create": case "edit": { const n = e.parsed.action === "edit"; const r = normalizeOptionalString(e.parsed.text); if (!r) return { text: `Usage: /goal ${n ? "edit" : "start"} <objective>`, changed: false }; const i = n ? await updateSessionGoalObjective({ ...t, objective: r }) : await createSessionGoal({ ...t, objective: r, fallbackEntry: e.fallbackEntry }); return { text: `Goal ${n ? "updated" : "started"}: ${i.objective}`, ...n ? {} : { continuationPrompt: formatGoalContinuationPrompt(i.objective) }, changed: true }; } case "pause": case "resume": case "complete": case "done": case "block": case "blocked": { const r = { pause: "paused", resume: "active", complete: "complete", done: "complete", block: "blocked", blocked: "blocked" }[e.parsed.action]; const i = await updateSessionGoalStatus({ ...t, status: r, ...n }); return { text: `Goal ${r === "active" ? "resumed" : r}: ${i.objective}${r === "complete" ? `\nTokens used: ${i.tokensUsed}` : ""}`, ...r === "active" ? { continuationPrompt: formatGoalResumeContinuationPrompt(e.parsed.text) } : {}, changed: true }; } case "clear": { const e = await clearSessionGoal(t); return { text: e ? "Goal cleared." : "No goal to clear.", changed: e }; } default: return { text: "Usage: /goal <objective> | /goal [status] | /goal start <objective> | /goal edit <objective> | /goal pause|resume|complete|block|clear", changed: false }; } } /** Command handler for /goal lifecycle commands. */ const handleGoalCommand = defineAuthorizedTextCommand({ label: "/goal", match: parseGoalCommand }, async (e, t) => { try { const n = await executeSessionGoalCommand({ parsed: t, sessionKey: e.sessionKey, storePath: e.storePath, fallbackEntry: e.sessionEntry, agentId: e.agentId, readOnlyStatus: true }); if (n.changed || t.action === "status" || t.action === "clear") await refreshGoalSessionEntry(e); if (n.changed) markCommandSessionMetadataChanged(e); if (n.continuationPrompt) { applyCommandTextToParams(e, n.continuationPrompt); return { shouldContinue: true }; } return commandReply(n.text); } catch (e) { const t = e instanceof Error ? e.message : String(e); return commandReply(`Goal error: ${t}`); } }); //#endregion export { markCommandSessionMetadataChanged as a, parseGoalCommand as i, handleGoalCommand as n, takeCommandSessionMetadataChanges as o, isFormattedGoalContinuationPrompt as r, takeCommandSessionMetadataChangesFromTargets as s, executeSessionGoalCommand as t };