openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
92 lines (91 loc) • 2.4 kB
JavaScript
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { o as resolveSessionTitleSource } from "./session-actor-memory-owner-BkB9Vtu4.mjs";
//#region src/gateway/derive-goal-session-title.ts
/**
* Deterministic session-title fallback when LLM labeling is unavailable.
* Prefers a task-bearing sentence from already-sanitized user text; never
* requires a model or local runtime.
*/
const DERIVED_GOAL_TITLE_MAX_LEN = 60;
/** Leading host wrappers that are not recoverable user intent (Tang-aligned). */
const HOST_ENVELOPE_PREFIXES = [
"# agents.md instructions for ",
"<environment_context>",
"<permissions instructions>",
"<skills_instructions>",
"<apps_instructions>",
"<plugins_instructions>",
"<recommended_plugins>",
"<external_openclaw_",
"openclaw runtime context"
];
const TASK_VERBS = /* @__PURE__ */ new Set([
"add",
"analyze",
"build",
"check",
"compare",
"create",
"debug",
"design",
"draft",
"explain",
"find",
"fix",
"give",
"help",
"implement",
"investigate",
"list",
"make",
"plan",
"prepare",
"recommend",
"recover",
"rename",
"resume",
"review",
"summarize",
"test",
"update",
"write"
]);
function truncateTitle(e) {
if (e.length <= DERIVED_GOAL_TITLE_MAX_LEN) return e;
const t = truncateUtf16Safe(e, 59);
const n = t.lastIndexOf(" ");
if (n > DERIVED_GOAL_TITLE_MAX_LEN * .6) return `${t.slice(0, n)}…`;
return `${t}…`;
}
function isHostEnvelope(e) {
const t = e.trimStart().toLowerCase();
return HOST_ENVELOPE_PREFIXES.some((e) => t.startsWith(e));
}
function toSentenceCase(e) {
if (!e) return e;
const t = e[0];
if (!t || t !== t.toLowerCase()) return e;
return `${t.toUpperCase()}${e.slice(1)}`;
}
function pickGoalSentence(e) {
const t = e.split(/(?<=[.!?])\s+/);
for (const e of t) {
const t = e.replace(/^["'`([{]+/, "").split(/\s+/)[0]?.toLowerCase();
if (t && TASK_VERBS.has(t)) return e.trim();
}
return e;
}
/**
* Build a compact topic label from a first user message without calling a model.
* Returns undefined when no usable user task remains after stripping envelopes.
*/
function deriveGoalSessionTitle(e) {
if (!e) return;
const t = resolveSessionTitleSource(e)?.replace(/\s+/g, " ").trim();
if (!t || isHostEnvelope(t)) return;
const n = pickGoalSentence(t);
if (!n) return;
return truncateTitle(toSentenceCase(n));
}
//#endregion
export { deriveGoalSessionTitle as t };