fix: improve truncation logic and address code review feedback
Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
This commit is contained in:
parent
60ac3e86c2
commit
a191db8e83
|
|
@ -180,11 +180,22 @@ async function updateWakeRequestStatus(pageId, status, result) {
|
||||||
'状态': { select: { name: status } },
|
'状态': { select: { name: status } },
|
||||||
};
|
};
|
||||||
|
|
||||||
// 如果有回执信息字段,写入处理结果
|
// 如果有回执信息字段,写入处理结果(Notion rich_text 限制 2000 chars)
|
||||||
if (result) {
|
if (result) {
|
||||||
|
const NOTION_RICH_TEXT_LIMIT = 2000;
|
||||||
|
let truncatedResult = result;
|
||||||
|
if (result.length > NOTION_RICH_TEXT_LIMIT) {
|
||||||
|
// 截取到最近的完整句子
|
||||||
|
truncatedResult = result.slice(0, NOTION_RICH_TEXT_LIMIT);
|
||||||
|
const lastPeriod = Math.max(truncatedResult.lastIndexOf('。'), truncatedResult.lastIndexOf('. '), truncatedResult.lastIndexOf('\n'));
|
||||||
|
if (lastPeriod > NOTION_RICH_TEXT_LIMIT * 0.5) {
|
||||||
|
truncatedResult = truncatedResult.slice(0, lastPeriod + 1);
|
||||||
|
}
|
||||||
|
truncatedResult += ' ...(已截断)';
|
||||||
|
}
|
||||||
properties['回执信息'] = {
|
properties['回执信息'] = {
|
||||||
rich_text: [{
|
rich_text: [{
|
||||||
text: { content: result.slice(0, 2000) },
|
text: { content: truncatedResult },
|
||||||
}],
|
}],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -221,15 +221,19 @@ function loadSystemContext() {
|
||||||
console.log('[WAKE] 📚 加载系统上下文...');
|
console.log('[WAKE] 📚 加载系统上下文...');
|
||||||
const context = {};
|
const context = {};
|
||||||
|
|
||||||
// 加载 master-brain(截取前部分避免过长)
|
// 加载 master-brain(截取到最近的段落分隔符避免截断)
|
||||||
const masterBrainPath = path.join(ROOT, 'brain/master-brain.md');
|
const masterBrainPath = path.join(ROOT, 'brain/master-brain.md');
|
||||||
if (fs.existsSync(masterBrainPath)) {
|
if (fs.existsSync(masterBrainPath)) {
|
||||||
const fullContent = fs.readFileSync(masterBrainPath, 'utf-8');
|
const fullContent = fs.readFileSync(masterBrainPath, 'utf-8');
|
||||||
const MASTER_BRAIN_MAX_LENGTH = 3000;
|
const maxLen = parseInt(process.env.BRAIN_CONTEXT_MAX_LENGTH, 10) || 3000;
|
||||||
context.masterBrain = fullContent.slice(0, MASTER_BRAIN_MAX_LENGTH);
|
if (fullContent.length > maxLen) {
|
||||||
if (fullContent.length > MASTER_BRAIN_MAX_LENGTH) {
|
// 截取到最近的段落分隔符(---或空行)
|
||||||
console.log(`[WAKE] ✅ master-brain.md 已加载 (截取 ${MASTER_BRAIN_MAX_LENGTH}/${fullContent.length} chars)`);
|
const truncated = fullContent.slice(0, maxLen);
|
||||||
|
const lastBreak = Math.max(truncated.lastIndexOf('\n---'), truncated.lastIndexOf('\n\n'));
|
||||||
|
context.masterBrain = lastBreak > maxLen * 0.5 ? truncated.slice(0, lastBreak) : truncated;
|
||||||
|
console.log(`[WAKE] ✅ master-brain.md 已加载 (截取 ${context.masterBrain.length}/${fullContent.length} chars)`);
|
||||||
} else {
|
} else {
|
||||||
|
context.masterBrain = fullContent;
|
||||||
console.log('[WAKE] ✅ master-brain.md 已加载');
|
console.log('[WAKE] ✅ master-brain.md 已加载');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue