-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontext.js
More file actions
60 lines (54 loc) · 1.98 KB
/
Copy pathcontext.js
File metadata and controls
60 lines (54 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
let doc = document;
let parentWin = window;
try {
if (window.parent && window.parent.document !== document) {
doc = window.parent.document;
parentWin = window.parent;
}
} catch (e) { }
export { doc, parentWin };
export function getSTContext() {
const st = parentWin.SillyTavern || window.SillyTavern;
return st ? st.getContext() : null;
}
export function getSTCharacters() {
if (parentWin.characters && Array.isArray(parentWin.characters)) return parentWin.characters;
const ctx = getSTContext();
if (ctx && ctx.characters) return ctx.characters;
return [];
}
/**
* 获取当前正在聊天的角色信息
* @returns {{ fileName: string, name: string } | null} 当前角色信息,无则返回 null
*/
export function getCurrentChatChar() {
try {
// 方法1: 尝试从 parentWin 直接获取 (适用于插件在 iframe 中运行)
const currentChId = parentWin.this_chid;
if (typeof currentChId !== 'undefined' && currentChId !== null &&
parentWin.characters && parentWin.characters[currentChId]) {
const char = parentWin.characters[currentChId];
return {
fileName: char.avatar,
name: char.name
};
}
// 方法2: 尝试从 SillyTavern Context API 获取
const ctx = getSTContext();
if (ctx) {
// 尝试从 context 获取当前角色 ID
const ctxCharId = ctx.characterId ?? ctx.this_chid;
if (typeof ctxCharId !== 'undefined' && ctxCharId !== null &&
ctx.characters && ctx.characters[ctxCharId]) {
const char = ctx.characters[ctxCharId];
return {
fileName: char.avatar,
name: char.name
};
}
}
} catch (e) {
console.warn('[CharManager] 获取当前聊天角色失败:', e);
}
return null;
}