@@ -16,6 +16,23 @@ export function getInjectionSnapshot() {
1616 return injectionSnapshot ;
1717}
1818
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 ;
34+ }
35+
1936// Best-effort extraction of the current user's latest message text from the
2037// live session, for semantic-first injection (Bug4). The system-prompt
2138// interpolator renders synchronously, so this walks the already-materialized
@@ -36,11 +53,13 @@ function lastUserQuery(ctx) {
3653 if ( kind !== undefined && kind !== "user" ) continue ;
3754 const parts = event . data ?. content ;
3855 if ( ! Array . isArray ( parts ) || parts . length === 0 ) continue ;
39- return parts
40- . map ( ( p ) => ( typeof p === "string" ? p : p ?. text ?? "" ) )
41- . filter ( Boolean )
42- . join ( "\n" )
43- . slice ( 0 , 500 ) ;
56+ return safeSlice (
57+ parts
58+ . map ( ( p ) => ( typeof p === "string" ? p : p ?. text ?? "" ) )
59+ . filter ( Boolean )
60+ . join ( "\n" ) ,
61+ 500
62+ ) ;
4463 }
4564 } catch { /* session internals unavailable: degrade to no query */ }
4665 return "" ;
@@ -85,7 +104,7 @@ function extractRounds(ctx, maxRounds) {
85104 const isUser = event ?. type === "user/message" && ( kind === undefined || kind === "user" ) ;
86105 if ( isUser ) {
87106 if ( pendingQuery ) rounds . push ( { query : pendingQuery , response : "" } ) ;
88- pendingQuery = textOf ( event ) . slice ( 0 , 500 ) ;
107+ pendingQuery = safeSlice ( textOf ( event ) , 500 ) ;
89108 continue ;
90109 }
91110 // Only assistant-originated events close a round; tool/system events
@@ -94,7 +113,7 @@ function extractRounds(ctx, maxRounds) {
94113 || kind === "assistant" ;
95114 const body = isAssistant ? textOf ( event ) : "" ;
96115 if ( ! body || ! pendingQuery ) continue ;
97- rounds . push ( { query : pendingQuery , response : body . slice ( 0 , 800 ) } ) ;
116+ rounds . push ( { query : pendingQuery , response : safeSlice ( body , 800 ) } ) ;
98117 pendingQuery = null ;
99118 }
100119 if ( pendingQuery ) rounds . push ( { query : pendingQuery , response : "" } ) ;
@@ -171,7 +190,7 @@ export function createInjector(ctx, service, settings, config) {
171190 function injectMemory ( m , maxLength = maxContent ) {
172191 const text = String ( m ?. content ?? "" ) ;
173192 if ( text . length <= maxLength ) return text ;
174- return `${ text . slice ( 0 , maxLength ) } …${ STR . truncatedHint [ language ] ( maxLength , text . length , m . id ) } ` ;
193+ return `${ safeSlice ( text , maxLength ) } …${ STR . truncatedHint [ language ] ( maxLength , text . length , m . id ) } ` ;
175194 }
176195
177196 // Prompt-variable brace escaping (issue #162, restored from v0.7.4 #40):
@@ -192,6 +211,41 @@ export function createInjector(ctx, service, settings, config) {
192211 return String ( text ) . replace ( / [ { } ] { 2 , } / g, ( run ) => run . split ( "" ) . join ( "\\" ) ) ;
193212 }
194213
214+ // Time-prefix injection (issue #34,恢复于 #333): opt-in, off by default.
215+ // When enabled the current date/time is injected once per conversation — at
216+ // the first prompt assembly of a new session — so the model can sense what
217+ // time/day it is. A per-session latch means later assemblies in the same
218+ // session never re-inject; a bare render ctx (no session id) falls back to
219+ // once per injector lifetime.
220+ // v0.7.11 面板大改版(ce4658e)时这段随一次批量删除被静默移除,CHANGELOG 无
221+ // 登记(#162 的 escapePromptVars 同刀误删、后来恢复,本函数漏了)——用户按
222+ // CHANGELOG 开着开关,实际什么都不会发生。本次按 v0.7.2 语义原样恢复:键名、
223+ // 默认值(false)、格式 `[当前时间: YYYY-MM-DD 周X HH:MM]` 全部不变,存量
224+ // feature_flags 里的配置恢复即生效。
225+ // 已注入过时间前缀的会话集合(#335 review:单值闩锁在 A→B→A 交替渲染下会
226+ // 重复注入)。上限 500 防长驻进程无界增长,超限逐出最早记录。
227+ const timePrefixDone = new Set ( ) ;
228+ let timePrefixBare = false ;
229+
230+ function renderTimePrefix ( ctx ) {
231+ if ( config . injectTimePrefix !== true ) return "" ;
232+ const sessionId = ctx ?. agent ?. session ?. id ;
233+ if ( sessionId === undefined ) {
234+ if ( timePrefixBare ) return "" ;
235+ timePrefixBare = true ;
236+ } else {
237+ if ( timePrefixDone . has ( sessionId ) ) return "" ;
238+ if ( timePrefixDone . size >= 500 ) timePrefixDone . delete ( timePrefixDone . keys ( ) . next ( ) . value ) ;
239+ timePrefixDone . add ( sessionId ) ;
240+ }
241+ const WEEKDAYS = [ "周日" , "周一" , "周二" , "周三" , "周四" , "周五" , "周六" ] ;
242+ const now = new Date ( ) ;
243+ const pad = ( n ) => String ( n ) . padStart ( 2 , "0" ) ;
244+ const date = `${ now . getFullYear ( ) } -${ pad ( now . getMonth ( ) + 1 ) } -${ pad ( now . getDate ( ) ) } ` ;
245+ const time = `${ pad ( now . getHours ( ) ) } :${ pad ( now . getMinutes ( ) ) } ` ;
246+ return escapePromptVars ( `[当前时间: ${ date } ${ WEEKDAYS [ now . getDay ( ) ] } ${ time } ]` ) ;
247+ }
248+
195249 // Hot memory (v0.5.0 1.3): the latest rounds of THIS session, rebuilt from
196250 // the materialized event log on every render — stateless, so it survives
197251 // session switches and never persists anywhere.
@@ -335,7 +389,12 @@ export function createInjector(ctx, service, settings, config) {
335389 // separate context) keeps the prompt assembly stable at two blocks.
336390 const hotText = renderHotContext ( ctx ) ;
337391 const body = render ( candidates , pinnedStats ) ;
338- const finalBody = ! hotText ? body : body ? `${ hotText } \n\n${ body } ` : hotText ;
392+ // #333(issue #34 恢复):时间前缀只在会话首轮出现(per-session 闩锁),
393+ // 排在整个记忆块的最前——模型先知道「今天几号」,再看热上下文与长期记忆。
394+ const timePrefix = renderTimePrefix ( ctx ) ;
395+ // 拼接次序:时间 → 热上下文 → 长期记忆(三者都可能为空,逐级判空拼接)。
396+ const withHot = ! hotText ? body : body ? `${ hotText } \n\n${ body } ` : hotText ;
397+ const finalBody = ! timePrefix ? withHot : withHot ? `${ timePrefix } \n\n${ withHot } ` : timePrefix ;
339398 // Issue #179:旁路缓存一帧——面板「注入预览」卡据此展示构成与体积。
340399 // chars 为条目内容的截断后近似值(与 render 同一函数计长),totalChars
341400 // 是本次实际返回块的精确长度。maxItems 反映自适应收缩后的生效值。
0 commit comments