用户画像(跨会话、按登录用户隔离,只记称谓/行业/偏好三字段):新建/清空会话时把画像渲染文本+现取的近期关注(最近5条会话标题,不入库)冻结进sessions.profile_snapshot,buildMessages以system后首条user恒定注入(不入messages表故阶段归档/压缩免疫,字节恒定不破前缀缓存);唯一更新时机enterExecuting落槌——advising轮历史+当前画像+任务书快照喂一次模型调用(templates/profile-prompt.txt锁三字段JSON全量覆写语义:未提及原值拷回、被纠正才改写/清空),服务端按字段覆写、漏字段/解析失败保旧值,fire-and-forget永不抛出;user_profiles表(user主键,data JSON,enabled开关);/api/profile读写按req.user隔离,账户设置页新增用户画像子页(可见可改可停用,停用即清现存快照且不再提取);auth未启用=空串键隐式单用户
This commit is contained in:
+34
-1
@@ -16,6 +16,11 @@ const COMPRESS_PROMPT_TEMPLATE = fs.readFileSync(path.join(__dirname, 'templates
|
||||
// 末尾追加一条 user(本模板,{{language}} 占位符替换为配置语言),见 titleMessages。
|
||||
const TITLE_PROMPT_TEMPLATE = fs.readFileSync(path.join(__dirname, 'templates', 'title-prompt.txt'), 'utf8');
|
||||
|
||||
// 用户画像提取提示词(templates/profile-prompt.txt):advising→executing 落槌(enterExecuting)时触发一次。
|
||||
// 与 titleMessages 同构:advising 轮历史 + 末条 user(任务书快照 + 本模板,{{language}} 替换为配置语言),
|
||||
// 见 profileMessages;拼装后的回合不传 tools,只收一段 JSON(解析/合并在 user-profile.js)。
|
||||
const PROFILE_PROMPT_TEMPLATE = fs.readFileSync(path.join(__dirname, 'templates', 'profile-prompt.txt'), 'utf8');
|
||||
|
||||
// 拼标题回合的 messages:历史原样(只留 user/assistant 纯文本行)+ 末尾一条标题指令 user。
|
||||
// 与 buildMessages 同取舍:archived 行不进;__blocks__ 哨兵 JSON 还原为文字,图片以 [图片] 占位;
|
||||
// 含 tool_calls 的 assistant 行无文本内容则跳过,tool 回执行整体跳过(只要对话主干)。
|
||||
@@ -35,6 +40,30 @@ function titleMessages(history, language) {
|
||||
return messages;
|
||||
}
|
||||
|
||||
// 拼画像提取回合的 messages(同 titleMessages 的取行口径):历史主干 + 末条 user
|
||||
//(当前画像 + 任务书快照截 4000 字 + 本模板)。当前画像随证据一起给到,提取按全量覆写输出
|
||||
//(见 profile-prompt.txt);画像文本的渲染在 user-profile.js,此处只拼接。
|
||||
function profileMessages(history, language, currentProfileText, briefText) {
|
||||
const messages = [];
|
||||
for (const row of history || []) {
|
||||
if (row.type === 'archived') continue;
|
||||
if (row.role !== 'user' && row.role !== 'assistant') continue;
|
||||
let content = row.content || '';
|
||||
const blocks = decodeBlocks(content);
|
||||
if (blocks) content = blocks.map(b => (b && b.type === 'text' ? b.text : '[图片]')).join('\n');
|
||||
if (!content.trim()) continue;
|
||||
messages.push({ role: row.role, content });
|
||||
}
|
||||
const lang = String(language || '').trim() || 'the same language as the conversation';
|
||||
const brief = String(briefText || '').slice(0, 4000);
|
||||
messages.push({
|
||||
role: 'user',
|
||||
content: '【Current profile】\n' + String(currentProfileText || '').trim() +
|
||||
'\n\n【Task brief】\n' + brief + '\n\n' + PROFILE_PROMPT_TEMPLATE.replaceAll('{{language}}', () => lang),
|
||||
});
|
||||
return messages;
|
||||
}
|
||||
|
||||
// system prompt 进程内恒定(利于上游前缀缓存)。多套并存:默认模板 + 每套 model_profiles
|
||||
// 的自定义 prompt(文本随配置加载时读入,见 config.js normalizeModelProfiles,经
|
||||
// setProfilePrompts 注册)。请求时按 profile 名选取——纯内存指针选择,零 IO。
|
||||
@@ -144,6 +173,10 @@ function cachedDecodeBlocks(session, row) {
|
||||
// 空 = 默认全量组合。
|
||||
function buildMessages(session, language, mintUrl, profile) {
|
||||
const messages = [{ role: 'system', content: systemPromptText(language, profile) }];
|
||||
// 用户画像快照(sessions.profile_snapshot,建会话/清空时冻结,见 user-profile.js):
|
||||
// 作为 system 后首条 user 注入,会话内字节恒定不破坏前缀缓存;
|
||||
// 刻意不落 messages 表:阶段切换归档、压缩摘要都只动 messages 表,碰不到它。
|
||||
if (session.profile_snapshot) messages.push({ role: 'user', content: session.profile_snapshot });
|
||||
const history = session.history;
|
||||
// 行被删除后清理残留的图片解析缓存条目(只增不清会缓慢泄漏;条数不会超过现存行数)
|
||||
if (session.blocksCache && session.blocksCache.size > history.length) {
|
||||
@@ -490,4 +523,4 @@ async function callModel(cfg, modelId, messages, tools, options) {
|
||||
return { content, reasoning, tool_calls: Array.from(toolCalls.values()), finish_reason: finishReason, usage, aborted, sseFrames };
|
||||
}
|
||||
|
||||
module.exports = { callModel, buildMessages, buildSystemPrompt, systemPromptText, setSkillListText, setProfilePrompts, encodeBlocks, decodeBlocks, compressMessages, titleMessages };
|
||||
module.exports = { callModel, buildMessages, buildSystemPrompt, systemPromptText, setSkillListText, setProfilePrompts, encodeBlocks, decodeBlocks, compressMessages, titleMessages, profileMessages };
|
||||
|
||||
Reference in New Issue
Block a user