(handle, options = {})
| 4573 | } |
| 4574 | |
| 4575 | function formatIMessageMemoryContext(handle, options = {}) { |
| 4576 | const normalized = collectIMessageFlaskEntries(handle); |
| 4577 | if (!normalized.length) return ""; |
| 4578 | |
| 4579 | const base = normalized.slice(-6); |
| 4580 | const body = normalized.slice(-18, -6); |
| 4581 | const neck = normalized.slice(0, -18); |
| 4582 | const formatLine = (entry) => { |
| 4583 | const speaker = entry.role === "assistant" ? assistantName : ownerLabel; |
| 4584 | const time = entry.at ? ` @${entry.at}` : ""; |
| 4585 | return `${speaker}${time}:${entry.text.slice(0, 420)}`; |
| 4586 | }; |
| 4587 | const bodyLines = compactIMessageFlaskEntries(body, 8, 180, formatLine); |
| 4588 | const neckLines = compactIMessageFlaskEntries(neck, 6, 120, formatLine); |
| 4589 | const priorityNote = options.desktopContextActive |
| 4590 | ? "当前存在更新的连续上下文;旧对话只用于语气、人物关系和背景。如果与更新片段冲突,以更新片段为准。" |
| 4591 | : "时效性优先:base 比 body 重要,body 比 neck 重要;旧内容只作背景,不要覆盖最新消息。"; |
| 4592 | return [ |
| 4593 | `以下是你和${ownerLabel}的统一锥形瓶上下文,请自然参考,不要逐字复述:`, |
| 4594 | priorityNote, |
| 4595 | "base / 最新手机侧原文:", |
| 4596 | ...base.map(formatLine), |
| 4597 | bodyLines.length ? "body / 较早手机侧摘要:" : "", |
| 4598 | ...bodyLines, |
| 4599 | neckLines.length ? "neck / 更早手机侧线索:" : "", |
| 4600 | ...neckLines |
| 4601 | ].filter(Boolean).join("\n"); |
| 4602 | } |
| 4603 | |
| 4604 | function compactIMessageFlaskEntries(entries, limit, maxLength, formatLine) { |
| 4605 | if (!Array.isArray(entries) || !entries.length) return []; |
no test coverage detected