Reorganize modules into layered directories

This commit is contained in:
Youzini-afk
2026-04-08 01:17:47 +08:00
parent 59942541ea
commit feec17f3e3
90 changed files with 284 additions and 219 deletions

89
host/adapter/context.js Normal file
View File

@@ -0,0 +1,89 @@
import { getContext as extensionGetContext } from "../../../../extensions.js";
import { buildCapabilityStatus, mergeVersionHints } from "./capabilities.js";
import { debugDebug } from "../../runtime/debug-logging.js";
function resolveContextGetter(providedGetter = null) {
if (typeof providedGetter === "function") {
return providedGetter;
}
if (typeof extensionGetContext === "function") {
return extensionGetContext;
}
const globalGetter = globalThis?.SillyTavern?.getContext;
return typeof globalGetter === "function" ? globalGetter : null;
}
function detectContextMode(getContext) {
if (typeof getContext !== "function") {
return "unavailable";
}
if (getContext === extensionGetContext) {
return "extensions-api";
}
return "global-api";
}
export function createContextHostFacade(options = {}) {
const getContext = resolveContextGetter(options.getContext);
const available = typeof getContext === "function";
const mode = detectContextMode(getContext);
return Object.freeze({
available,
mode,
fallbackReason: available ? "" : "未检测到 getContext 宿主接口",
versionHints: mergeVersionHints(
{
getter: "getContext",
source: mode,
sillyTavernGlobal:
globalThis?.SillyTavern && typeof globalThis.SillyTavern === "object"
? "available"
: "missing",
},
options.versionHints,
),
getContext: (...args) => {
if (!available) {
return null;
}
try {
return getContext(...args);
} catch (error) {
debugDebug(
"[ST-BME] host-adapter/context getContext 调用失败",
error,
);
return null;
}
},
readContextSnapshot: (...args) => {
if (!available) {
return null;
}
try {
const context = getContext(...args);
return context && typeof context === "object" ? context : null;
} catch (error) {
debugDebug("[ST-BME] host-adapter/context 读取上下文失败", error);
return null;
}
},
});
}
export function inspectContextHostCapability(options = {}) {
const facade = createContextHostFacade(options);
return buildCapabilityStatus(facade);
}
export function readHostContext(options = {}) {
return createContextHostFacade(options).readContextSnapshot();
}