获取正则
提示
请务必先阅读如何正确使用酒馆助手
getTavernRegexes
获取酒馆正则,支持获取全局正则、角色卡局部正则或预设正则。
ts
function getTavernRegexes(option: TavernRegexOption): TavernRegex[];ts
type TavernRegexOption = TavernRegexOptionGlobal | TavernRegexOptionCharacter | TavernRegexOptionPreset;ts
type TavernRegexOptionGlobal = {
type: 'global';
};ts
type TavernRegexOptionCharacter = {
type: 'character';
name?: string | 'current';
};ts
type TavernRegexOptionPreset = {
type: 'preset';
name?: string | 'in_use';
};ts
type TavernRegex = {
id: string;
script_name: string;
enabled: boolean;
run_on_edit: boolean;
scope?: "global" | "character";
find_regex: string;
trim_strings: string;
replace_string: string;
source: {
user_input: boolean;
ai_output: boolean;
slash_command: boolean;
world_info: boolean;
};
destination: {
display: boolean;
prompt: boolean;
};
min_depth: number | null;
max_depth: number | null;
};参数
option.type
- 类型:
'global' | 'character' | 'preset' - 描述: 要操作的酒馆正则类型
'global'- 全局正则'character'- 角色卡局部正则'preset'- 预设正则
option.name (仅 character 和 preset 类型)
- 类型:
string | 'current' | 'in_use' - 描述:
- 对于
character类型:角色卡名称,'current'表示当前角色卡(默认值) - 对于
preset类型:预设名称,'in_use'表示当前使用的预设(默认值)
- 对于
返回值
- 酒馆正则数组:
TavernRegex[]
该数组依据正则作用于文本的顺序排序,也就是酒馆显示正则的地方从上到下排列。
示例
ts
const regexes = getTavernRegexes({ type: 'global' });
console.log('全局正则:', regexes);ts
const regexes = getTavernRegexes({ type: 'character', name: 'current' });
console.log('当前角色卡正则:', regexes);ts
const regexes = getTavernRegexes({ type: 'character', name: '角色卡名称' });
console.log('指定角色卡正则:', regexes);ts
const regexes = getTavernRegexes({ type: 'preset', name: 'in_use' });
console.log('当前预设正则:', regexes);ts
const regexes = getTavernRegexes({ type: 'preset', name: '预设名称' });
console.log('指定预设正则:', regexes);ts
const regexes = getTavernRegexes({ type: 'global' });
regexes.forEach(regex => {
console.log(`正则: ${regex.script_name}`);
console.log(`启用: ${regex.enabled}`);
console.log(`查找: ${regex.find_regex}`);
console.log(`替换: ${regex.replace_string}`);
});isCharacterTavernRegexesEnabled
判断局部正则是否被启用。
ts
function isCharacterTavernRegexesEnabled(): boolean;返回值
- 局部正则是否被启用:
boolean
示例
ts
const isEnabled = isCharacterTavernRegexesEnabled();
if (isEnabled) {
console.log('局部正则已启用');
} else {
console.log('局部正则未启用');
}注意
如果你是在被写在局部正则中的全局脚本调用这个函数,请保证"在编辑时运行"被启用,这样这个脚本才会无视局部正则开启情况而运行。
formatAsTavernRegexedString
对文本应用酒馆正则。
ts
function formatAsTavernRegexedString(
text: string,
source: 'user_input' | 'ai_output' | 'slash_command' | 'world_info' | 'reasoning',
destination: 'display' | 'prompt',
{ depth, character_name }?: FormatAsTavernRegexedStringOption,
): string;ts
type FormatAsTavernRegexedStringOption = {
depth?: number;
character_name?: string;
};参数
text
- 类型:
string - 描述: 要应用酒馆正则的文本
source
- 类型:
'user_input' | 'ai_output' | 'slash_command' | 'world_info' | 'reasoning' - 描述: 文本来源,例如来自用户输入或 AI 输出。对应于酒馆正则的"作用范围"选项
destination
- 类型:
'display' | 'prompt' - 描述: 文本将作为什么而使用,例如用于显示或作为提示词。对应于酒馆正则的"仅格式显示"和"仅格式提示词"选项
option?
- 类型:
FormatAsTavernRegexedStringOption - 描述: 可选选项
depth?
- 类型:
number - 描述: 文本所在的深度;不填则不考虑酒馆正则的"深度"选项:无论该深度是否在酒馆正则的"最小深度"和"最大深度"范围内都生效
character_name?
- 类型:
string - 描述: 角色卡名称;不填则使用当前角色卡名称
返回值
- 应用酒馆正则后的文本:
string
示例
ts
// 对文本应用酒馆正则,作为 AI 输出显示
const text = "Hello world!";
const result = formatAsTavernRegexedString(text, 'ai_output', 'display');
console.log('处理后的文本:', result);ts
// 获取最后一楼文本,将它视为将会作为显示的 AI 输出,对它应用酒馆正则
const message = getChatMessages(-1)[0];
const result = formatAsTavernRegexedString(
message.message,
'ai_output',
'display',
{ depth: 0 }
);ts
// 对用户输入应用正则后用于提示词
const userInput = "用户的输入文本";
const processedText = formatAsTavernRegexedString(
userInput,
'user_input',
'prompt'
);ts
// 对特定角色卡应用正则
const text = "角色的对话";
const result = formatAsTavernRegexedString(
text,
'ai_output',
'display',
{ character_name: '特定角色名' }
);