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 } },
|
||||
};
|
||||
|
||||
// 如果有回执信息字段,写入处理结果
|
||||
// 如果有回执信息字段,写入处理结果(Notion rich_text 限制 2000 chars)
|
||||
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['回执信息'] = {
|
||||
rich_text: [{
|
||||
text: { content: result.slice(0, 2000) },
|
||||
text: { content: truncatedResult },
|
||||
}],
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -221,15 +221,19 @@ function loadSystemContext() {
|
|||
console.log('[WAKE] 📚 加载系统上下文...');
|
||||
const context = {};
|
||||
|
||||
// 加载 master-brain(截取前部分避免过长)
|
||||
// 加载 master-brain(截取到最近的段落分隔符避免截断)
|
||||
const masterBrainPath = path.join(ROOT, 'brain/master-brain.md');
|
||||
if (fs.existsSync(masterBrainPath)) {
|
||||
const fullContent = fs.readFileSync(masterBrainPath, 'utf-8');
|
||||
const MASTER_BRAIN_MAX_LENGTH = 3000;
|
||||
context.masterBrain = fullContent.slice(0, MASTER_BRAIN_MAX_LENGTH);
|
||||
if (fullContent.length > MASTER_BRAIN_MAX_LENGTH) {
|
||||
console.log(`[WAKE] ✅ master-brain.md 已加载 (截取 ${MASTER_BRAIN_MAX_LENGTH}/${fullContent.length} chars)`);
|
||||
const maxLen = parseInt(process.env.BRAIN_CONTEXT_MAX_LENGTH, 10) || 3000;
|
||||
if (fullContent.length > maxLen) {
|
||||
// 截取到最近的段落分隔符(---或空行)
|
||||
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 {
|
||||
context.masterBrain = fullContent;
|
||||
console.log('[WAKE] ✅ master-brain.md 已加载');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue