@@ -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,14 @@ 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+ . slice ( 0 , 500 ) ,
62+ 500
63+ ) ;
4464 }
4565 } catch { /* session internals unavailable: degrade to no query */ }
4666 return "" ;
@@ -85,7 +105,7 @@ function extractRounds(ctx, maxRounds) {
85105 const isUser = event ?. type === "user/message" && ( kind === undefined || kind === "user" ) ;
86106 if ( isUser ) {
87107 if ( pendingQuery ) rounds . push ( { query : pendingQuery , response : "" } ) ;
88- pendingQuery = textOf ( event ) . slice ( 0 , 500 ) ;
108+ pendingQuery = safeSlice ( textOf ( event ) , 500 ) ;
89109 continue ;
90110 }
91111 // Only assistant-originated events close a round; tool/system events
@@ -94,7 +114,7 @@ function extractRounds(ctx, maxRounds) {
94114 || kind === "assistant" ;
95115 const body = isAssistant ? textOf ( event ) : "" ;
96116 if ( ! body || ! pendingQuery ) continue ;
97- rounds . push ( { query : pendingQuery , response : body . slice ( 0 , 800 ) } ) ;
117+ rounds . push ( { query : pendingQuery , response : safeSlice ( body , 800 ) } ) ;
98118 pendingQuery = null ;
99119 }
100120 if ( pendingQuery ) rounds . push ( { query : pendingQuery , response : "" } ) ;
@@ -171,7 +191,7 @@ export function createInjector(ctx, service, settings, config) {
171191 function injectMemory ( m , maxLength = maxContent ) {
172192 const text = String ( m ?. content ?? "" ) ;
173193 if ( text . length <= maxLength ) return text ;
174- return `${ text . slice ( 0 , maxLength ) } …${ STR . truncatedHint [ language ] ( maxLength , text . length , m . id ) } ` ;
194+ return `${ safeSlice ( text , maxLength ) } …${ STR . truncatedHint [ language ] ( maxLength , text . length , m . id ) } ` ;
175195 }
176196
177197 // Prompt-variable brace escaping (issue #162, restored from v0.7.4 #40):
@@ -192,6 +212,37 @@ export function createInjector(ctx, service, settings, config) {
192212 return String ( text ) . replace ( / [ { } ] { 2 , } / g, ( run ) => run . split ( "" ) . join ( "\\" ) ) ;
193213 }
194214
215+ // Time-prefix injection (issue #34,恢复于 #333): opt-in, off by default.
216+ // When enabled the current date/time is injected once per conversation — at
217+ // the first prompt assembly of a new session — so the model can sense what
218+ // time/day it is. A per-session latch means later assemblies in the same
219+ // session never re-inject; a bare render ctx (no session id) falls back to
220+ // once per injector lifetime.
221+ // v0.7.11 面板大改版(ce4658e)时这段随一次批量删除被静默移除,CHANGELOG 无
222+ // 登记(#162 的 escapePromptVars 同刀误删、后来恢复,本函数漏了)——用户按
223+ // CHANGELOG 开着开关,实际什么都不会发生。本次按 v0.7.2 语义原样恢复:键名、
224+ // 默认值(false)、格式 `[当前时间: YYYY-MM-DD 周X HH:MM]` 全部不变,存量
225+ // feature_flags 里的配置恢复即生效。
226+ const WEEKDAYS = [ "周日" , "周一" , "周二" , "周三" , "周四" , "周五" , "周六" ] ;
227+ let timePrefixSession = null ;
228+
229+ function renderTimePrefix ( ctx ) {
230+ if ( config . injectTimePrefix !== true ) return "" ;
231+ const sessionId = ctx ?. agent ?. session ?. id ;
232+ if ( sessionId === undefined ) {
233+ if ( timePrefixSession !== null ) return "" ;
234+ timePrefixSession = true ;
235+ } else {
236+ if ( sessionId === timePrefixSession ) return "" ;
237+ timePrefixSession = sessionId ;
238+ }
239+ const now = new Date ( ) ;
240+ const pad = ( n ) => String ( n ) . padStart ( 2 , "0" ) ;
241+ const date = `${ now . getFullYear ( ) } -${ pad ( now . getMonth ( ) + 1 ) } -${ pad ( now . getDate ( ) ) } ` ;
242+ const time = `${ pad ( now . getHours ( ) ) } :${ pad ( now . getMinutes ( ) ) } ` ;
243+ return escapePromptVars ( `[当前时间: ${ date } ${ WEEKDAYS [ now . getDay ( ) ] } ${ time } ]` ) ;
244+ }
245+
195246 // Hot memory (v0.5.0 1.3): the latest rounds of THIS session, rebuilt from
196247 // the materialized event log on every render — stateless, so it survives
197248 // session switches and never persists anywhere.
@@ -335,7 +386,12 @@ export function createInjector(ctx, service, settings, config) {
335386 // separate context) keeps the prompt assembly stable at two blocks.
336387 const hotText = renderHotContext ( ctx ) ;
337388 const body = render ( candidates , pinnedStats ) ;
338- const finalBody = ! hotText ? body : body ? `${ hotText } \n\n${ body } ` : hotText ;
389+ // #333(issue #34 恢复):时间前缀只在会话首轮出现(per-session 闩锁),
390+ // 排在整个记忆块的最前——模型先知道「今天几号」,再看热上下文与长期记忆。
391+ const timePrefix = renderTimePrefix ( ctx ) ;
392+ // 拼接次序:时间 → 热上下文 → 长期记忆(三者都可能为空,逐级判空拼接)。
393+ const withHot = ! hotText ? body : body ? `${ hotText } \n\n${ body } ` : hotText ;
394+ const finalBody = ! timePrefix ? withHot : withHot ? `${ timePrefix } \n\n${ withHot } ` : timePrefix ;
339395 // Issue #179:旁路缓存一帧——面板「注入预览」卡据此展示构成与体积。
340396 // chars 为条目内容的截断后近似值(与 render 同一函数计长),totalChars
341397 // 是本次实际返回块的精确长度。maxItems 反映自适应收缩后的生效值。
0 commit comments