11import { createScopeResolver } from "./scope.js" ;
2- import { createHotMemory } from "./hot-memory.js" ;
2+ import { createHotMemory , roundsFromEvents } from "./hot-memory.js" ;
33import { STR , langOf } from "./lang.js" ;
44import { MEMORY_GUIDE_SECTION } from "./guide.js" ;
55import { adaptiveInjectBudget } from "./search/adaptive.js" ;
66import { injectChildEnabled } from "./config.js" ;
7+ import { safeSlice } from "./safe-slice.js" ;
78
89// Issue #179:注入预览的数据底座。systemPrompt 渲染是同步回调,面板只能事后
910// 拉取,所以在这里旁路缓存「最近一帧组装」——快照就是本次渲染用过的同一份
@@ -16,21 +17,19 @@ export function getInjectionSnapshot() {
1617 return injectionSnapshot ;
1718}
1819
19- // Issue #334:UTF-16 截断切开 emoji 代理对会留下孤立代理项(如 🔴 = D83D DD34
20- // 在 800 处切,D83D 留下、DD34 被切走)。DSH 把它序列化成非法 UTF-8,DeepSeek
21- // API 对每个请求回 400 INVALID_REQUEST——而且畸形文本随注入消息被永久写进会话
22- // 历史,此后**每一轮**都带着它,表现为会话从某一轮起连续 400、换话题/重启都不
23- // 恢复(会话级不可逆损坏)。防法是「少一个字符都行,绝不留半个」:尾字符是
24- // 高位代理(0xD800–0xDBFF)说明它的低位搭档被切走了,把这个高位代理整个丢掉。
25- // (反过来尾字符是低位代理不可能孤立——它的搭档必然紧邻在前、同在切点之内。)
26- // ASCII-only 的文本零开销(length 相等直接短路)。
27- function safeSlice ( text , limit ) {
28- const s = String ( text ) ;
29- if ( s . length <= limit ) return s ;
30- let cut = s . slice ( 0 , limit ) ;
31- const tail = cut . charCodeAt ( cut . length - 1 ) ;
32- if ( tail >= 0xd800 && tail <= 0xdbff ) cut = cut . slice ( 0 , - 1 ) ;
33- return cut ;
20+ // 「模型可见事件 seq」集合:宿主在 session.surface.nodes 维护可见面——rewind 的
21+ // surfaceOp replace 与 compact 的隐藏替换都作用在这层(dsh-rewind 插件即据此计算
22+ // 替换区间)。拿不到(老版本宿主)返回 null,调用方退回「全量事件」的旧行为。
23+ // 读取必须在 try 内:`surface.nodes` 是宿主 getter,其 _assertProjections 在消息投影
24+ // 所属插件被移除时会 throw(dsh-session lib/types/surface.js)。抛出等同于「没有
25+ // 可见面」,绝不能让整块热记忆消失——那是比旧偏差更糟的退化。
26+ function visibleSeqsOf ( session ) {
27+ try {
28+ const nodes = session ?. surface ?. nodes ;
29+ return Array . isArray ( nodes ) ? new Set ( nodes ) : null ;
30+ } catch {
31+ return null ;
32+ }
3433}
3534
3635// Best-effort extraction of the current user's latest message text from the
@@ -46,8 +45,11 @@ function lastUserQuery(ctx) {
4645 // still have the .events property, so fall back to it.
4746 const events = session ?. snapshotEvents ?. ( ) ?? session ?. events ;
4847 if ( ! Array . isArray ( events ) || events . length === 0 ) return "" ;
48+ const visibleSeqs = visibleSeqsOf ( session ) ;
4949 for ( let i = events . length - 1 ; i >= 0 ; i -- ) {
5050 const event = events [ i ] ;
51+ // 被撤回/被压缩遮蔽的事件不算「最近一条用户消息」(与 extractRounds 同口径)。
52+ if ( visibleSeqs && ! visibleSeqs . has ( event ?. seq ) ) continue ;
5153 if ( event ?. type !== "user/message" ) continue ;
5254 const kind = event . data ?. source ?. kind ;
5355 if ( kind !== undefined && kind !== "user" ) continue ;
@@ -66,58 +68,20 @@ function lastUserQuery(ctx) {
6668}
6769
6870// Hot-memory round extraction (v0.5.0 1.3): pairs each user/message with the
69- // next assistant reply from the materialized session log. Tolerates shapes
70- // where assistant events carry a different type tag — anything whose payload
71- // has content parts and is not a user message counts as a reply. Best-effort:
72- // returns [] on any failure, and the hot block simply does not render.
71+ // next assistant reply. The pairing lives in hot-memory.js (roundsFromEvents)
72+ // so it can be unit-tested directly; this adapter only resolves the session's
73+ // raw event log and the model-visible seq set (session.surface.nodes — see
74+ // visibleSeqsOf). Rewind-shadowed / compacted events are thereby excluded:
75+ // hot memory and the model see the same conversation. Best-effort: returns []
76+ // on any failure, and the hot block simply does not render.
7377function extractRounds ( ctx , maxRounds ) {
7478 try {
7579 const session = ctx ?. agent ?. session ;
80+ // DSH ≥0.1.2-rc only exposes events via snapshotEvents(); older builds
81+ // still have the .events property, so fall back to it.
7682 const events = session ?. snapshotEvents ?. ( ) ?? session ?. events ;
7783 if ( ! Array . isArray ( events ) || events . length === 0 ) return [ ] ;
78- const rounds = [ ] ;
79- let pendingQuery = null ;
80- const textOf = ( event ) => {
81- // user/message 的正文就在 data 上,而 assistant/message(以及 tool/result)的消息体
82- // 嵌在 data.message 下 —— DSH 侧由 @deepseek-ai/dsh-session 的
83- // assertMessageEventShape 一行写死:const message = type === "user/message" ? record : record?.["message"]。
84- // 只读 data.content 会让每一轮的 response 恒为空(Issue #129)。
85- // 这里用 ?? 兜底而不是直接改成 data.message.content:user 消息与旧的扁平形状都照旧可用。
86- const parts = event ?. data ?. content ?? event ?. data ?. message ?. content ;
87- if ( ! Array . isArray ( parts ) ) return "" ;
88- // 只取正文 part(type === "text" 或字符串),跳过 reasoning(issue #162):
89- // reasoning 里出现 Go template / Vue / Handlebars / 正则 / 日志原文的概率远高于
90- // 正文,把它带进 hot memory 会让 `{{...}}` 模板片段注入 prompt。旧的扁平形状
91- // (字符串 part)照旧取用,无 type 字段的兜底视为正文。
92- return parts
93- . map ( ( p ) => {
94- if ( typeof p === "string" ) return p ;
95- if ( p ?. type && p . type !== "text" ) return "" ;
96- return p ?. text ?? "" ;
97- } )
98- . filter ( Boolean )
99- . join ( "\n" )
100- . trim ( ) ;
101- } ;
102- for ( const event of events ) {
103- const kind = event ?. data ?. source ?. kind ;
104- const isUser = event ?. type === "user/message" && ( kind === undefined || kind === "user" ) ;
105- if ( isUser ) {
106- if ( pendingQuery ) rounds . push ( { query : pendingQuery , response : "" } ) ;
107- pendingQuery = safeSlice ( textOf ( event ) , 500 ) ;
108- continue ;
109- }
110- // Only assistant-originated events close a round; tool/system events
111- // carrying text must not be mistaken for the model's reply.
112- const isAssistant = typeof event ?. type === "string" && event . type . includes ( "assistant" )
113- || kind === "assistant" ;
114- const body = isAssistant ? textOf ( event ) : "" ;
115- if ( ! body || ! pendingQuery ) continue ;
116- rounds . push ( { query : pendingQuery , response : safeSlice ( body , 800 ) } ) ;
117- pendingQuery = null ;
118- }
119- if ( pendingQuery ) rounds . push ( { query : pendingQuery , response : "" } ) ;
120- return rounds . slice ( - maxRounds ) ;
84+ return roundsFromEvents ( events , maxRounds , visibleSeqsOf ( session ) ) ;
12185 } catch {
12286 return [ ] ;
12387 }
0 commit comments