mirror of
https://github.com/Youzini-afk/ST-Bionic-Memory-Ecology.git
synced 2026-06-13 18:31:16 +08:00
Reorganize modules into layered directories
This commit is contained in:
89
host/adapter/context.js
Normal file
89
host/adapter/context.js
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user