mirror of
https://github.com/Youzini-afk/ST-Bionic-Memory-Ecology.git
synced 2026-06-14 02:40:45 +08:00
Show POV owner in memory injection
This commit is contained in:
39
injector.js
39
injector.js
@@ -2,6 +2,7 @@
|
|||||||
// 将检索结果格式化为表格注入到 LLM 上下文中
|
// 将检索结果格式化为表格注入到 LLM 上下文中
|
||||||
|
|
||||||
import { getSchemaType } from "./schema.js";
|
import { getSchemaType } from "./schema.js";
|
||||||
|
import { normalizeMemoryScope } from "./memory-scope.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将检索结果转换为注入文本
|
* 将检索结果转换为注入文本
|
||||||
@@ -172,17 +173,22 @@ function formatTable(nodes, typeDef, appended = new Set()) {
|
|||||||
(n) => n.fields?.[col.name] != null && n.fields[col.name] !== "",
|
(n) => n.fields?.[col.name] != null && n.fields[col.name] !== "",
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
const derivedCols = buildDerivedColumns(uniqueNodes, typeDef);
|
||||||
|
const allCols = [...derivedCols, ...activeCols];
|
||||||
|
|
||||||
if (activeCols.length === 0) return "";
|
if (allCols.length === 0) return "";
|
||||||
|
|
||||||
// 表头
|
// 表头
|
||||||
const header = `| ${activeCols.map((c) => c.name).join(" | ")} |`;
|
const header = `| ${allCols.map((c) => c.name).join(" | ")} |`;
|
||||||
const separator = `| ${activeCols.map(() => "---").join(" | ")} |`;
|
const separator = `| ${allCols.map(() => "---").join(" | ")} |`;
|
||||||
|
|
||||||
// 数据行
|
// 数据行
|
||||||
const rows = uniqueNodes.map((node) => {
|
const rows = uniqueNodes.map((node) => {
|
||||||
const cells = activeCols.map((col) => {
|
const cells = allCols.map((col) => {
|
||||||
const val = node.fields?.[col.name] ?? "";
|
const val =
|
||||||
|
typeof col.getValue === "function"
|
||||||
|
? col.getValue(node)
|
||||||
|
: node.fields?.[col.name] ?? "";
|
||||||
// 转义管道符,限制单元格长度
|
// 转义管道符,限制单元格长度
|
||||||
return String(val)
|
return String(val)
|
||||||
.replace(/\|/g, "\\|")
|
.replace(/\|/g, "\\|")
|
||||||
@@ -195,6 +201,29 @@ function formatTable(nodes, typeDef, appended = new Set()) {
|
|||||||
return `${typeDef.tableName}:\n${header}\n${separator}\n${rows.join("\n")}`;
|
return `${typeDef.tableName}:\n${header}\n${separator}\n${rows.join("\n")}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function buildDerivedColumns(nodes, typeDef) {
|
||||||
|
if (typeDef?.id !== "pov_memory") {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
name: "owner",
|
||||||
|
getValue(node) {
|
||||||
|
const scope = normalizeMemoryScope(node?.scope);
|
||||||
|
const ownerLabel = scope.ownerName || scope.ownerId || "未命名";
|
||||||
|
if (scope.ownerType === "user") {
|
||||||
|
return `用户: ${ownerLabel}`;
|
||||||
|
}
|
||||||
|
if (scope.ownerType === "character") {
|
||||||
|
return `角色: ${ownerLabel}`;
|
||||||
|
}
|
||||||
|
return `POV: ${ownerLabel}`;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取注入提示词的总 token 估算
|
* 获取注入提示词的总 token 估算
|
||||||
* 粗略估算:1 个 token ≈ 2 个中文字符 或 4 个英文字符
|
* 粗略估算:1 个 token ≈ 2 个中文字符 或 4 个英文字符
|
||||||
|
|||||||
@@ -70,6 +70,9 @@ assert.match(text, /\[Memory - User POV \/ Not Character Facts\]/);
|
|||||||
assert.match(text, /不等于角色已知事实/);
|
assert.match(text, /不等于角色已知事实/);
|
||||||
assert.match(text, /\[Memory - Objective \/ Current Region\]/);
|
assert.match(text, /\[Memory - Objective \/ Current Region\]/);
|
||||||
assert.match(text, /pov_memory_table:/);
|
assert.match(text, /pov_memory_table:/);
|
||||||
|
assert.match(text, /\| owner \| summary \| belief \| emotion \| attitude \|/);
|
||||||
|
assert.match(text, /角色: 艾琳/);
|
||||||
|
assert.match(text, /用户: 玩家/);
|
||||||
assert.match(text, /event_table:/);
|
assert.match(text, /event_table:/);
|
||||||
|
|
||||||
console.log("injector-format tests passed");
|
console.log("injector-format tests passed");
|
||||||
|
|||||||
Reference in New Issue
Block a user