mirror of
https://github.com/Youzini-afk/ST-Bionic-Memory-Ecology.git
synced 2026-05-15 22:30:38 +08:00
Reorganize modules into layered directories
This commit is contained in:
59
runtime/debug-logging.js
Normal file
59
runtime/debug-logging.js
Normal file
@@ -0,0 +1,59 @@
|
||||
const MODULE_NAME = "st_bme";
|
||||
const GLOBAL_DEBUG_FLAG_KEY = "__stBmeDebugLoggingEnabled";
|
||||
|
||||
function resolveModuleSettings(settings = null) {
|
||||
if (settings && typeof settings === "object") {
|
||||
return settings;
|
||||
}
|
||||
|
||||
const moduleSettings =
|
||||
globalThis?.extension_settings?.[MODULE_NAME] ||
|
||||
globalThis?.__p0ExtensionSettings?.[MODULE_NAME] ||
|
||||
globalThis?.SillyTavern?.getContext?.()?.extensionSettings?.[MODULE_NAME] ||
|
||||
null;
|
||||
return moduleSettings && typeof moduleSettings === "object"
|
||||
? moduleSettings
|
||||
: null;
|
||||
}
|
||||
|
||||
export function isDebugLoggingEnabled(settings = null) {
|
||||
if (
|
||||
settings &&
|
||||
typeof settings === "object" &&
|
||||
Object.prototype.hasOwnProperty.call(settings, "debugLoggingEnabled")
|
||||
) {
|
||||
return Boolean(settings.debugLoggingEnabled);
|
||||
}
|
||||
|
||||
if (typeof globalThis[GLOBAL_DEBUG_FLAG_KEY] === "boolean") {
|
||||
return globalThis[GLOBAL_DEBUG_FLAG_KEY];
|
||||
}
|
||||
|
||||
return Boolean(resolveModuleSettings(settings)?.debugLoggingEnabled);
|
||||
}
|
||||
|
||||
function emitDebugLog(method, args, settings = null) {
|
||||
if (!isDebugLoggingEnabled(settings)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const target =
|
||||
typeof console?.[method] === "function" ? console[method] : console.log;
|
||||
Reflect.apply(target, console, args);
|
||||
}
|
||||
|
||||
export function debugLog(...args) {
|
||||
emitDebugLog("log", args);
|
||||
}
|
||||
|
||||
export function debugInfo(...args) {
|
||||
emitDebugLog("info", args);
|
||||
}
|
||||
|
||||
export function debugWarn(...args) {
|
||||
emitDebugLog("warn", args);
|
||||
}
|
||||
|
||||
export function debugDebug(...args) {
|
||||
emitDebugLog("debug", args);
|
||||
}
|
||||
224
runtime/generation-options.js
Normal file
224
runtime/generation-options.js
Normal file
@@ -0,0 +1,224 @@
|
||||
// ST-BME: 任务级生成参数过滤层(Phase 1)
|
||||
|
||||
import { getActiveTaskProfile } from "../prompting/prompt-profiles.js";
|
||||
|
||||
const SUPPORTED_FIELDS = [
|
||||
"max_context_tokens",
|
||||
"max_completion_tokens",
|
||||
"reply_count",
|
||||
"stream",
|
||||
"temperature",
|
||||
"top_p",
|
||||
"top_k",
|
||||
"top_a",
|
||||
"min_p",
|
||||
"seed",
|
||||
"frequency_penalty",
|
||||
"presence_penalty",
|
||||
"repetition_penalty",
|
||||
"squash_system_messages",
|
||||
"reasoning_effort",
|
||||
"request_thoughts",
|
||||
"enable_function_calling",
|
||||
"enable_web_search",
|
||||
"character_name_prefix",
|
||||
"wrap_user_messages_in_quotes",
|
||||
];
|
||||
|
||||
const CONSERVATIVE_ALLOWLIST = new Set([
|
||||
"temperature",
|
||||
"top_p",
|
||||
"seed",
|
||||
"max_completion_tokens",
|
||||
"stream",
|
||||
"frequency_penalty",
|
||||
"presence_penalty",
|
||||
]);
|
||||
|
||||
const OPENAI_COMPAT_ALLOWLIST = new Set([
|
||||
"max_completion_tokens",
|
||||
"stream",
|
||||
"temperature",
|
||||
"top_p",
|
||||
"seed",
|
||||
"frequency_penalty",
|
||||
"presence_penalty",
|
||||
"reasoning_effort",
|
||||
"request_thoughts",
|
||||
"enable_function_calling",
|
||||
"enable_web_search",
|
||||
"wrap_user_messages_in_quotes",
|
||||
]);
|
||||
|
||||
const BOOLEAN_FIELDS = new Set([
|
||||
"stream",
|
||||
"squash_system_messages",
|
||||
"request_thoughts",
|
||||
"enable_function_calling",
|
||||
"enable_web_search",
|
||||
"wrap_user_messages_in_quotes",
|
||||
]);
|
||||
|
||||
const INTEGER_FIELDS = new Set([
|
||||
"max_context_tokens",
|
||||
"max_completion_tokens",
|
||||
"reply_count",
|
||||
"top_k",
|
||||
"seed",
|
||||
]);
|
||||
|
||||
const FLOAT_FIELDS = new Set([
|
||||
"temperature",
|
||||
"top_p",
|
||||
"top_a",
|
||||
"min_p",
|
||||
"frequency_penalty",
|
||||
"presence_penalty",
|
||||
"repetition_penalty",
|
||||
]);
|
||||
|
||||
const REASONING_EFFORT_VALUES = new Set(["low", "medium", "high", "minimal"]);
|
||||
|
||||
function resolveCapabilityMode(context = {}) {
|
||||
const normalizedMode = String(context.mode || "").trim().toLowerCase();
|
||||
if (normalizedMode === "dedicated-openai-compatible") {
|
||||
return "openai-compatible";
|
||||
}
|
||||
|
||||
const normalizedSource = String(context.source || "").trim().toLowerCase();
|
||||
if (
|
||||
normalizedSource &&
|
||||
["openai", "openrouter", "mistral", "cohere", "custom", "vllm"].includes(
|
||||
normalizedSource,
|
||||
)
|
||||
) {
|
||||
return "openai-compatible";
|
||||
}
|
||||
|
||||
return "conservative";
|
||||
}
|
||||
|
||||
function getAllowlistForCapability(capabilityMode) {
|
||||
if (capabilityMode === "openai-compatible") {
|
||||
return OPENAI_COMPAT_ALLOWLIST;
|
||||
}
|
||||
return CONSERVATIVE_ALLOWLIST;
|
||||
}
|
||||
|
||||
function normalizeByField(field, rawValue) {
|
||||
if (rawValue == null || rawValue === "") {
|
||||
return { ok: false, reason: "empty_value" };
|
||||
}
|
||||
|
||||
if (BOOLEAN_FIELDS.has(field)) {
|
||||
return { ok: true, value: Boolean(rawValue) };
|
||||
}
|
||||
|
||||
if (INTEGER_FIELDS.has(field)) {
|
||||
const parsed = Number.parseInt(rawValue, 10);
|
||||
if (!Number.isFinite(parsed)) {
|
||||
return { ok: false, reason: "invalid_number" };
|
||||
}
|
||||
if (parsed < 0) {
|
||||
return { ok: false, reason: "invalid_range" };
|
||||
}
|
||||
if (field === "reply_count" && parsed < 1) {
|
||||
return { ok: false, reason: "invalid_range" };
|
||||
}
|
||||
return { ok: true, value: parsed };
|
||||
}
|
||||
|
||||
if (FLOAT_FIELDS.has(field)) {
|
||||
const parsed = Number.parseFloat(rawValue);
|
||||
if (!Number.isFinite(parsed)) {
|
||||
return { ok: false, reason: "invalid_number" };
|
||||
}
|
||||
|
||||
if (field === "temperature" && (parsed < 0 || parsed > 2)) {
|
||||
return { ok: false, reason: "invalid_range" };
|
||||
}
|
||||
if (
|
||||
["top_p", "top_a", "min_p"].includes(field) &&
|
||||
(parsed < 0 || parsed > 1)
|
||||
) {
|
||||
return { ok: false, reason: "invalid_range" };
|
||||
}
|
||||
if (
|
||||
["frequency_penalty", "presence_penalty", "repetition_penalty"].includes(
|
||||
field,
|
||||
) &&
|
||||
(parsed < -2 || parsed > 2)
|
||||
) {
|
||||
return { ok: false, reason: "invalid_range" };
|
||||
}
|
||||
|
||||
return { ok: true, value: parsed };
|
||||
}
|
||||
|
||||
if (field === "reasoning_effort") {
|
||||
const normalized = String(rawValue || "")
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
if (!normalized) {
|
||||
return { ok: false, reason: "empty_value" };
|
||||
}
|
||||
if (!REASONING_EFFORT_VALUES.has(normalized)) {
|
||||
return { ok: false, reason: "invalid_value" };
|
||||
}
|
||||
return { ok: true, value: normalized };
|
||||
}
|
||||
|
||||
if (field === "character_name_prefix") {
|
||||
return { ok: true, value: String(rawValue || "").trim() };
|
||||
}
|
||||
|
||||
return { ok: true, value: rawValue };
|
||||
}
|
||||
|
||||
export function resolveTaskGenerationOptions(
|
||||
settings = {},
|
||||
taskType,
|
||||
fallback = {},
|
||||
capabilityContext = {},
|
||||
) {
|
||||
const profile = getActiveTaskProfile(settings, taskType);
|
||||
const generation = { ...(profile?.generation || {}) };
|
||||
const filtered = {};
|
||||
const removed = [];
|
||||
const capabilityMode = resolveCapabilityMode(capabilityContext);
|
||||
const allowlist = getAllowlistForCapability(capabilityMode);
|
||||
|
||||
for (const field of SUPPORTED_FIELDS) {
|
||||
if (!Object.prototype.hasOwnProperty.call(generation, field)) continue;
|
||||
const rawValue = generation[field];
|
||||
if (rawValue == null || rawValue === "") continue;
|
||||
|
||||
if (!allowlist.has(field)) {
|
||||
removed.push({ field, reason: "capability_filtered", capabilityMode });
|
||||
continue;
|
||||
}
|
||||
|
||||
const normalized = normalizeByField(field, rawValue);
|
||||
if (!normalized.ok) {
|
||||
removed.push({ field, reason: normalized.reason, capabilityMode });
|
||||
continue;
|
||||
}
|
||||
|
||||
filtered[field] = normalized.value;
|
||||
}
|
||||
|
||||
if (!Number.isFinite(filtered.max_completion_tokens)) {
|
||||
const fallbackTokens = Number.parseInt(fallback.max_completion_tokens, 10);
|
||||
if (Number.isFinite(fallbackTokens) && fallbackTokens > 0) {
|
||||
filtered.max_completion_tokens = fallbackTokens;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
profile,
|
||||
generation,
|
||||
filtered,
|
||||
removed,
|
||||
capabilityMode,
|
||||
};
|
||||
}
|
||||
25
runtime/planner-tag-utils.js
Normal file
25
runtime/planner-tag-utils.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const DEFAULT_PLANNER_TAGS = ["plot", "note", "plot-log", "state"];
|
||||
|
||||
export function stripPlannerTags(text, tags = DEFAULT_PLANNER_TAGS) {
|
||||
let output = String(text ?? "");
|
||||
|
||||
for (const rawTag of Array.isArray(tags) ? tags : DEFAULT_PLANNER_TAGS) {
|
||||
const tag = String(rawTag || "").trim().toLowerCase();
|
||||
if (!tag) continue;
|
||||
const escaped = tag.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
output = output.replace(
|
||||
new RegExp(`<${escaped}\\b[^>]*>[\\s\\S]*?<\\/${escaped}>`, "gi"),
|
||||
"",
|
||||
);
|
||||
}
|
||||
|
||||
return output.trim();
|
||||
}
|
||||
|
||||
export function sanitizePlannerMessageText(message, tags = DEFAULT_PLANNER_TAGS) {
|
||||
if (!message) return "";
|
||||
const text = String(message.mes ?? "");
|
||||
return message.is_user ? stripPlannerTags(text, tags) : text;
|
||||
}
|
||||
|
||||
export { DEFAULT_PLANNER_TAGS };
|
||||
9
runtime/request-timeout.js
Normal file
9
runtime/request-timeout.js
Normal file
@@ -0,0 +1,9 @@
|
||||
export function resolveConfiguredTimeoutMs(
|
||||
settings = {},
|
||||
fallbackMs = 300000,
|
||||
) {
|
||||
const timeoutMs = Number(settings?.timeoutMs);
|
||||
return Number.isFinite(timeoutMs) && timeoutMs > 0
|
||||
? timeoutMs
|
||||
: fallbackMs;
|
||||
}
|
||||
94
runtime/runtime-debug.js
Normal file
94
runtime/runtime-debug.js
Normal file
@@ -0,0 +1,94 @@
|
||||
function safeClone(value, fallback = null) {
|
||||
if (value == null) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
try {
|
||||
if (typeof structuredClone === "function") {
|
||||
return structuredClone(value);
|
||||
}
|
||||
} catch {
|
||||
// ignore and fall through
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(JSON.stringify(value));
|
||||
} catch {
|
||||
return fallback ?? value;
|
||||
}
|
||||
}
|
||||
|
||||
function nowIso() {
|
||||
return new Date().toISOString();
|
||||
}
|
||||
|
||||
const runtimeDebugState = {
|
||||
hostCapabilities: null,
|
||||
taskPromptBuilds: {},
|
||||
taskLlmRequests: {},
|
||||
injections: {},
|
||||
updatedAt: "",
|
||||
};
|
||||
|
||||
function touchRuntimeDebugState() {
|
||||
runtimeDebugState.updatedAt = nowIso();
|
||||
}
|
||||
|
||||
export function resetRuntimeDebugSnapshot() {
|
||||
runtimeDebugState.hostCapabilities = null;
|
||||
runtimeDebugState.taskPromptBuilds = {};
|
||||
runtimeDebugState.taskLlmRequests = {};
|
||||
runtimeDebugState.injections = {};
|
||||
runtimeDebugState.updatedAt = nowIso();
|
||||
}
|
||||
|
||||
export function recordHostCapabilitySnapshot(snapshot = null) {
|
||||
runtimeDebugState.hostCapabilities = safeClone(snapshot, null);
|
||||
touchRuntimeDebugState();
|
||||
}
|
||||
|
||||
export function recordTaskPromptBuild(taskType, snapshot = {}) {
|
||||
const normalizedTaskType = String(taskType || "").trim() || "unknown";
|
||||
runtimeDebugState.taskPromptBuilds[normalizedTaskType] = {
|
||||
updatedAt: nowIso(),
|
||||
...safeClone(snapshot, {}),
|
||||
};
|
||||
touchRuntimeDebugState();
|
||||
}
|
||||
|
||||
export function recordTaskLlmRequest(taskType, snapshot = {}) {
|
||||
const normalizedTaskType = String(taskType || "").trim() || "unknown";
|
||||
runtimeDebugState.taskLlmRequests[normalizedTaskType] = {
|
||||
updatedAt: nowIso(),
|
||||
...safeClone(snapshot, {}),
|
||||
};
|
||||
touchRuntimeDebugState();
|
||||
}
|
||||
|
||||
export function recordInjectionSnapshot(kind, snapshot = {}) {
|
||||
const normalizedKind = String(kind || "").trim() || "default";
|
||||
runtimeDebugState.injections[normalizedKind] = {
|
||||
updatedAt: nowIso(),
|
||||
...safeClone(snapshot, {}),
|
||||
};
|
||||
touchRuntimeDebugState();
|
||||
}
|
||||
|
||||
export function getRuntimeDebugSnapshot() {
|
||||
return safeClone(
|
||||
{
|
||||
hostCapabilities: runtimeDebugState.hostCapabilities,
|
||||
taskPromptBuilds: runtimeDebugState.taskPromptBuilds,
|
||||
taskLlmRequests: runtimeDebugState.taskLlmRequests,
|
||||
injections: runtimeDebugState.injections,
|
||||
updatedAt: runtimeDebugState.updatedAt,
|
||||
},
|
||||
{
|
||||
hostCapabilities: null,
|
||||
taskPromptBuilds: {},
|
||||
taskLlmRequests: {},
|
||||
injections: {},
|
||||
updatedAt: "",
|
||||
},
|
||||
);
|
||||
}
|
||||
1180
runtime/runtime-state.js
Normal file
1180
runtime/runtime-state.js
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user