mirror of
https://github.com/Wx-2025/ST-Amily2-Chat-Optimisation.git
synced 2026-06-10 05:25:51 +00:00
Add files via upload
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { amilyHelper } from "../tavern-helper/main.js";
|
||||
import * as charApi from "./char-api.js";
|
||||
import { callAi } from "./api.js";
|
||||
|
||||
export const tools = {
|
||||
|
||||
@@ -17,12 +18,12 @@ export const tools = {
|
||||
return {
|
||||
uid: e.uid,
|
||||
keys: keys,
|
||||
comment: e.comment || keys || "Unnamed Entry",
|
||||
comment: e.comment || keys || "未命名条目",
|
||||
};
|
||||
});
|
||||
|
||||
return JSON.stringify({
|
||||
info: "Index of world book entries. Use 'read_world_entry' with 'uid' to read specific content.",
|
||||
info: "世界书条目索引。请使用带有 'uid' 的 'read_world_entry' 来读取具体内容。",
|
||||
total_entries: entries.length,
|
||||
entries: summary
|
||||
}, null, 2);
|
||||
@@ -33,10 +34,18 @@ export const tools = {
|
||||
const entry = entries.find(e => String(e.uid) === String(uid));
|
||||
|
||||
if (!entry) {
|
||||
return `Entry with UID ${uid} not found in world book "${book_name}".`;
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "ENTRY_NOT_FOUND",
|
||||
message: `在世界书 "${book_name}" 中未找到 UID 为 ${uid} 的条目。`,
|
||||
suggestion: "请使用 'read_world_info' 查看可用的 UID。"
|
||||
});
|
||||
}
|
||||
|
||||
return JSON.stringify(entry, null, 2);
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
data: entry
|
||||
}, null, 2);
|
||||
},
|
||||
|
||||
write_world_info_entry: async ({ book_name, entries }) => {
|
||||
@@ -45,14 +54,22 @@ export const tools = {
|
||||
const cleanEntries = entries.replace(/```json/g, '').replace(/```/g, '').trim();
|
||||
entries = JSON.parse(cleanEntries);
|
||||
} catch (e) {
|
||||
return `错误: 'entries' 参数必须是有效的 JSON 数组。解析错误: ${e.message}`;
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "INVALID_JSON",
|
||||
message: `'entries' 参数必须是有效的 JSON 数组。解析错误: ${e.message}`
|
||||
});
|
||||
}
|
||||
}
|
||||
if (!Array.isArray(entries)) {
|
||||
if (typeof entries === 'object' && entries !== null) {
|
||||
entries = [entries];
|
||||
} else {
|
||||
return "错误: 'entries' 参数必须是数组或对象。";
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "INVALID_TYPE",
|
||||
message: "'entries' 参数必须是数组或对象。"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,26 +84,61 @@ export const tools = {
|
||||
}
|
||||
}
|
||||
|
||||
let resultMsg = "";
|
||||
let updatedCount = 0;
|
||||
let createdCount = 0;
|
||||
let errors = [];
|
||||
|
||||
if (updates.length > 0) {
|
||||
const success = await amilyHelper.setLorebookEntries(book_name, updates);
|
||||
resultMsg += success ? `成功更新了 ${updates.length} 个条目。 ` : `更新条目失败。 `;
|
||||
if (success) updatedCount = updates.length;
|
||||
else errors.push("更新条目失败。");
|
||||
}
|
||||
if (creates.length > 0) {
|
||||
const success = await amilyHelper.createLorebookEntries(book_name, creates);
|
||||
resultMsg += success ? `成功创建了 ${creates.length} 个条目。 ` : `创建条目失败。 `;
|
||||
if (success) createdCount = creates.length;
|
||||
else errors.push("创建条目失败。");
|
||||
}
|
||||
return resultMsg || "未执行任何操作。";
|
||||
|
||||
if (errors.length > 0 && updatedCount === 0 && createdCount === 0) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "WRITE_FAILED",
|
||||
message: errors.join(" ")
|
||||
});
|
||||
}
|
||||
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: `成功更新了 ${updatedCount} 个条目,创建了 ${createdCount} 个条目。`,
|
||||
data: { updated: updatedCount, created: createdCount }
|
||||
});
|
||||
},
|
||||
|
||||
create_world_book: async ({ book_name }) => {
|
||||
const success = await amilyHelper.createLorebook(book_name);
|
||||
return success ? `世界书 "${book_name}" 创建成功。` : `创建世界书 "${book_name}" 失败。`;
|
||||
if (success) {
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: `世界书 "${book_name}" 创建成功。`
|
||||
});
|
||||
} else {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "CREATE_FAILED",
|
||||
message: `创建世界书 "${book_name}" 失败。`
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
read_character_card: async ({ chid }) => {
|
||||
const char = charApi.getCharacter(chid);
|
||||
if (!char) return "未找到角色。";
|
||||
if (!char) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "CHAR_NOT_FOUND",
|
||||
message: "未找到角色。"
|
||||
});
|
||||
}
|
||||
|
||||
const safeChar = {
|
||||
name: char.name,
|
||||
@@ -97,7 +149,10 @@ export const tools = {
|
||||
mes_example: char.mes_example,
|
||||
alternate_greetings: char.data?.alternate_greetings || []
|
||||
};
|
||||
return JSON.stringify(safeChar, null, 2);
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
data: safeChar
|
||||
}, null, 2);
|
||||
},
|
||||
|
||||
update_character_card: async (args) => {
|
||||
@@ -107,25 +162,42 @@ export const tools = {
|
||||
const success = charApi.updateCharacter(chid, finalUpdates);
|
||||
if (success) {
|
||||
const updatedFields = Object.keys(finalUpdates).join(', ');
|
||||
return `角色卡更新成功 [ID: ${chid}]。已更新字段: ${updatedFields}。`;
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: `角色卡更新成功 [ID: ${chid}]。`,
|
||||
data: { updated_fields: updatedFields }
|
||||
});
|
||||
} else {
|
||||
return "更新角色卡失败。";
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "UPDATE_FAILED",
|
||||
message: "更新角色卡失败。"
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
edit_character_text: async ({ chid, field, diff }) => {
|
||||
const char = charApi.getCharacter(chid);
|
||||
if (!char) return "未找到角色。";
|
||||
if (!char) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "CHAR_NOT_FOUND",
|
||||
message: "未找到角色。"
|
||||
});
|
||||
}
|
||||
|
||||
const allowedFields = ['description', 'personality', 'scenario', 'first_mes', 'mes_example'];
|
||||
if (!allowedFields.includes(field)) {
|
||||
return `无效的字段。允许的字段: ${allowedFields.join(', ')}`;
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "INVALID_FIELD",
|
||||
message: `无效的字段。允许的字段: ${allowedFields.join(', ')}`
|
||||
});
|
||||
}
|
||||
|
||||
let content = char[field] || '';
|
||||
const changes = diff.split('------- SEARCH');
|
||||
|
||||
// Remove the first empty split if any
|
||||
|
||||
if (changes[0].trim() === '') changes.shift();
|
||||
|
||||
for (const change of changes) {
|
||||
@@ -136,14 +208,30 @@ export const tools = {
|
||||
const replaceBlock = parts[1].split('+++++++ REPLACE')[0].trim();
|
||||
|
||||
if (!content.includes(searchBlock)) {
|
||||
return `错误: 在字段 '${field}' 中未找到以下搜索块:\n${searchBlock}`;
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "SEARCH_NOT_FOUND",
|
||||
message: `在字段 '${field}' 中未找到搜索块。`,
|
||||
suggestion: "请确保 SEARCH 块与现有内容完全匹配(包括空格)。"
|
||||
});
|
||||
}
|
||||
|
||||
content = content.replace(searchBlock, replaceBlock);
|
||||
}
|
||||
|
||||
const success = charApi.updateCharacter(chid, { [field]: content });
|
||||
return success ? `字段 '${field}' 更新成功。` : `更新字段 '${field}' 失败。`;
|
||||
if (success) {
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: `字段 '${field}' 更新成功。`
|
||||
});
|
||||
} else {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "UPDATE_FAILED",
|
||||
message: `更新字段 '${field}' 失败。`
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
manage_first_message: async ({ action, chid, index, message }) => {
|
||||
@@ -159,16 +247,140 @@ export const tools = {
|
||||
success = charApi.removeFirstMessage(chid, index);
|
||||
break;
|
||||
default:
|
||||
return "无效的操作。";
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "INVALID_ACTION",
|
||||
message: "无效的操作。"
|
||||
});
|
||||
}
|
||||
|
||||
if (success) {
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: `开场白 ${action} 成功。`
|
||||
});
|
||||
} else {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "ACTION_FAILED",
|
||||
message: `开场白 ${action} 失败。`
|
||||
});
|
||||
}
|
||||
return success ? `开场白 ${action} 成功。` : `开场白 ${action} 失败。`;
|
||||
},
|
||||
|
||||
create_character: async ({ name }) => {
|
||||
const result = await charApi.createNewCharacter(name);
|
||||
if (result === -1) return "创建角色失败。";
|
||||
if (result === -2) return "角色创建请求已发送。请手动刷新角色列表以查看新角色。";
|
||||
return `角色创建成功,ID: ${result}`;
|
||||
if (result === -1) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "CREATE_FAILED",
|
||||
message: "创建角色失败。"
|
||||
});
|
||||
}
|
||||
if (result === -2) {
|
||||
return JSON.stringify({
|
||||
status: "warning",
|
||||
code: "CREATE_PENDING",
|
||||
message: "角色创建请求已发送。请手动刷新。"
|
||||
});
|
||||
}
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: `角色创建成功。`,
|
||||
data: { id: result }
|
||||
});
|
||||
},
|
||||
|
||||
simulate_chat: async ({ chid, message }) => {
|
||||
const char = charApi.getCharacter(chid);
|
||||
if (!char) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "CHAR_NOT_FOUND",
|
||||
message: "未找到角色。"
|
||||
});
|
||||
}
|
||||
|
||||
const systemPrompt = `You are roleplaying as ${char.name}.
|
||||
Description: ${char.description}
|
||||
Personality: ${char.personality}
|
||||
Scenario: ${char.scenario}
|
||||
|
||||
Reply to the user's message in character. Stay in character.`;
|
||||
|
||||
const messages = [
|
||||
{ role: 'system', content: systemPrompt },
|
||||
{ role: 'user', content: message }
|
||||
];
|
||||
|
||||
try {
|
||||
const response = await callAi('executor', messages, { temperature: 0.9 });
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
data: {
|
||||
character: char.name,
|
||||
response: response
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "SIMULATION_FAILED",
|
||||
message: `模拟对话失败: ${error.message}`
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
set_style_reference: async ({ style }) => {
|
||||
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: `样式参考已设置为: ${style}`,
|
||||
_action: "update_task_state",
|
||||
_updates: { style_reference: style }
|
||||
});
|
||||
},
|
||||
|
||||
analyze_entities: async ({ text }) => {
|
||||
const systemPrompt = `You are an expert World Builder and Entity Extractor.
|
||||
Analyze the provided text and identify key entities that should have their own World Info (Lorebook) entries.
|
||||
Focus on:
|
||||
- Proper Nouns (People, Places, Organizations, Artifacts)
|
||||
- Unique Concepts (Magic systems, Historical events, Species)
|
||||
|
||||
Return a JSON object with a "entities" array. Each entity should have:
|
||||
- "name": The name of the entity.
|
||||
- "type": The type (Person, Location, Organization, etc.).
|
||||
- "description": A brief summary based on the text (1-2 sentences).
|
||||
- "confidence": A score (0-1) of how important this entity seems.
|
||||
|
||||
Output ONLY valid JSON.`;
|
||||
|
||||
const messages = [
|
||||
{ role: 'system', content: systemPrompt },
|
||||
{ role: 'user', content: text }
|
||||
];
|
||||
|
||||
try {
|
||||
const response = await callAi('executor', messages, { temperature: 0.1 });
|
||||
const cleanResponse = response.replace(/```json/g, '').replace(/```/g, '').trim();
|
||||
return cleanResponse;
|
||||
} catch (error) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
code: "ANALYSIS_FAILED",
|
||||
message: `实体分析失败: ${error.message}`
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
ask_user: async ({ question }) => {
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: `已向用户提问: ${question}`,
|
||||
_action: "stop_and_wait",
|
||||
data: { question }
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -176,50 +388,50 @@ export function getToolDefinitions() {
|
||||
return [
|
||||
{
|
||||
name: "read_world_info",
|
||||
description: "Read the index (list of entries with keys and comments) of a world book. Does NOT return full content.",
|
||||
description: "读取世界书的索引(包含关键字和注释的条目列表)。不返回完整内容。",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
book_name: { type: "string", description: "The name of the world book." }
|
||||
book_name: { type: "string", description: "世界书名称。" }
|
||||
},
|
||||
required: ["book_name"]
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "read_world_entry",
|
||||
description: "Read the full content of a specific world book entry.",
|
||||
description: "读取特定世界书条目的完整内容。",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
book_name: { type: "string", description: "The name of the world book." },
|
||||
uid: { type: "number", description: "The UID of the entry to read." }
|
||||
book_name: { type: "string", description: "世界书名称。" },
|
||||
uid: { type: "number", description: "要读取的条目 UID。" }
|
||||
},
|
||||
required: ["book_name", "uid"]
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "write_world_info_entry",
|
||||
description: "Create or update entries in a world book.",
|
||||
description: "创建或更新世界书中的条目。",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
book_name: { type: "string", description: "The name of the world book." },
|
||||
book_name: { type: "string", description: "世界书名称。" },
|
||||
entries: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
properties: {
|
||||
uid: { type: "number", description: "Entry ID (optional, for update)." },
|
||||
comment: { type: "string", description: "Entry title/comment." },
|
||||
content: { type: "string", description: "Entry content." },
|
||||
key: { type: "array", items: { type: "string" }, description: "Keywords." },
|
||||
enabled: { type: "boolean", description: "Is enabled." },
|
||||
constant: { type: "boolean", description: "Constant (Blue light)." },
|
||||
position: { type: "string", enum: ["before_character_definition", "after_character_definition", "before_author_note", "after_author_note", "at_depth_as_system"], description: "Insertion position." },
|
||||
depth: { type: "number", description: "Insertion depth." },
|
||||
scanDepth: { type: "number", description: "Scan depth." },
|
||||
exclude_recursion: { type: "boolean", description: "Exclude from recursion." },
|
||||
prevent_recursion: { type: "boolean", description: "Prevent recursion." }
|
||||
uid: { type: "number", description: "条目 ID(可选,用于更新)。" },
|
||||
comment: { type: "string", description: "条目标题/注释。" },
|
||||
content: { type: "string", description: "条目内容。" },
|
||||
key: { type: "array", items: { type: "string" }, description: "关键字。" },
|
||||
enabled: { type: "boolean", description: "是否启用。" },
|
||||
constant: { type: "boolean", description: "常驻(蓝灯)。" },
|
||||
position: { type: "string", enum: ["before_character_definition", "after_character_definition", "before_author_note", "after_author_note", "at_depth_as_system"], description: "插入位置。" },
|
||||
depth: { type: "number", description: "插入深度。" },
|
||||
scanDepth: { type: "number", description: "扫描深度。" },
|
||||
exclude_recursion: { type: "boolean", description: "排除递归。" },
|
||||
prevent_recursion: { type: "boolean", description: "防止递归。" }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -229,33 +441,33 @@ export function getToolDefinitions() {
|
||||
},
|
||||
{
|
||||
name: "create_world_book",
|
||||
description: "Create a new empty world book.",
|
||||
description: "创建一个新的空世界书。",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
book_name: { type: "string", description: "The name of the new world book." }
|
||||
book_name: { type: "string", description: "新世界书的名称。" }
|
||||
},
|
||||
required: ["book_name"]
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "read_character_card",
|
||||
description: "Read character card data.",
|
||||
description: "读取角色卡数据。",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
chid: { type: "number", description: "Character ID." }
|
||||
chid: { type: "number", description: "角色 ID。" }
|
||||
},
|
||||
required: ["chid"]
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "update_character_card",
|
||||
description: "Update character card fields (overwrite).",
|
||||
description: "更新角色卡字段(覆盖)。",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
chid: { type: "number", description: "Character ID." },
|
||||
chid: { type: "number", description: "角色 ID。" },
|
||||
name: { type: "string" },
|
||||
description: { type: "string" },
|
||||
personality: { type: "string" },
|
||||
@@ -268,15 +480,15 @@ export function getToolDefinitions() {
|
||||
},
|
||||
{
|
||||
name: "edit_character_text",
|
||||
description: "Edit a specific text field of a character using SEARCH/REPLACE blocks.",
|
||||
description: "使用 搜索/替换 块编辑角色的特定文本字段。",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
chid: { type: "number", description: "Character ID." },
|
||||
field: { type: "string", enum: ["description", "personality", "scenario", "first_mes", "mes_example"], description: "The field to edit." },
|
||||
chid: { type: "number", description: "角色 ID。" },
|
||||
field: { type: "string", enum: ["description", "personality", "scenario", "first_mes", "mes_example"], description: "要编辑的字段。" },
|
||||
diff: {
|
||||
type: "string",
|
||||
description: "One or more SEARCH/REPLACE blocks following this exact format:\n------- SEARCH\n[exact content to find]\n=======\n[new content to replace with]\n+++++++ REPLACE"
|
||||
description: "一个或多个遵循此确切格式的 搜索/替换 块:\n------- SEARCH\n[exact content to find]\n=======\n[new content to replace with]\n+++++++ REPLACE"
|
||||
}
|
||||
},
|
||||
required: ["chid", "field", "diff"]
|
||||
@@ -284,28 +496,73 @@ export function getToolDefinitions() {
|
||||
},
|
||||
{
|
||||
name: "manage_first_message",
|
||||
description: "Add, update, or remove alternate greetings.",
|
||||
description: "添加、更新或删除候补开场白。",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
action: { type: "string", enum: ["add", "update", "remove"] },
|
||||
chid: { type: "number", description: "Character ID." },
|
||||
index: { type: "number", description: "Index of the greeting (required for update/remove)." },
|
||||
message: { type: "string", description: "Content of the greeting (required for add/update)." }
|
||||
chid: { type: "number", description: "角色 ID。" },
|
||||
index: { type: "number", description: "开场白索引(更新/删除时必需)。" },
|
||||
message: { type: "string", description: "开场白内容(添加/更新时必需)。" }
|
||||
},
|
||||
required: ["action", "chid"]
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "create_character",
|
||||
description: "Create a new character card.",
|
||||
description: "创建一个新角色卡。",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
name: { type: "string", description: "Name of the new character." }
|
||||
name: { type: "string", description: "新角色的名字。" }
|
||||
},
|
||||
required: ["name"]
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "simulate_chat",
|
||||
description: "与角色模拟对话以测试其性格和设定。",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
chid: { type: "number", description: "角色 ID。" },
|
||||
message: { type: "string", description: "发送给角色的消息。" }
|
||||
},
|
||||
required: ["chid", "message"]
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "set_style_reference",
|
||||
description: "设置生成内容的风格参考或模板(例如:'黑暗奇幻风格','莎士比亚风格','JSON格式模板')。",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
style: { type: "string", description: "风格描述或模板内容。" }
|
||||
},
|
||||
required: ["style"]
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "analyze_entities",
|
||||
description: "分析文本并提取潜在的世界书条目(实体)。",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
text: { type: "string", description: "要分析的文本。" }
|
||||
},
|
||||
required: ["text"]
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "ask_user",
|
||||
description: "向用户提问以获取更多信息或确认。这将暂停自动执行并等待用户回复。",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
question: { type: "string", description: "要问的问题。" }
|
||||
},
|
||||
required: ["question"]
|
||||
}
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user