Files
writing-agent/src/dictate/history-window.ts
T
chenmoranzhi 9dc927282f 预设兼容:按真实抓包对齐装配与包装层,补上声明式工具与预设正则。
- 装配只认 prompt_order.enabled;squash_system_messages 只合并相邻 system(新增 system 档,连接符按酒馆用单换行)
- 包装层:new_chat_prompt、示例对话(new_example_chat_prompt + <START> 切段 + name)、角色卡三块与 personality_format/scenario_format、{{user}}/{{char}} 取值、任务契约并进预设条目
- 工具协议:按 ToolBindings / 伪工具协议数据声明真 tools(presetToolPolicy=preset),工具描述用到的条目不再重复作为消息发出
- 正则:promptOnly 作用于对话历史(含 depth 过滤)、写入时脚本改写回复;{{//}} 注释按配平扫描
- tools/st-request-diff:真假上游抓包对比工具、三份预设的机制结论与实测报告
2026-10-23 00:13:08 +08:00

52 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { DictateChatTurn } from "./types.js";
/**
* 创作上下文保留的更早对话轮数。
* 1 轮 = 一次用户输入 + 一次助手输出(不含本轮正在处理的末句用户话)。
* 负数 = 不限制。(当前缺省即不限制:块级滑窗会让整段前缀位移、击穿缓存,
* 先关掉以便量真多轮之后的首填命中,见 docs/briefs/prompt-cache-audit-2026-10-07.md)
* 可用环境变量 `DICTATE_HISTORY_TURNS` 或调用参数覆盖。
*/
export const DEFAULT_DICTATE_HISTORY_TURNS = -1;
export function resolveDictateHistoryTurns(override?: number): number {
if (typeof override === "number" && Number.isFinite(override)) {
return Math.trunc(override);
}
const raw = process.env.DICTATE_HISTORY_TURNS?.trim();
if (raw) {
const n = Number(raw);
if (Number.isFinite(n)) return Math.trunc(n);
}
return DEFAULT_DICTATE_HISTORY_TURNS;
}
/**
* 裁剪进模型的对话:保留末句用户输入 + 最近 `historyTurns` 轮更早问答。
* `historyTurns < 0` 时原样返回(= 不限制,当前缺省)。
*/
export function clipDictateDialogue(
dialogue: DictateChatTurn[],
historyTurns: number,
): DictateChatTurn[] {
if (historyTurns < 0) return dialogue;
const last = dialogue.at(-1);
const hasCurrentUser = last?.role === "user";
const earlier = hasCurrentUser ? dialogue.slice(0, -1) : [...dialogue];
const current = hasCurrentUser && last ? [last] : [];
if (historyTurns === 0) return current;
let userCount = 0;
let start = 0;
for (let i = earlier.length - 1; i >= 0; i -= 1) {
if (earlier[i]?.role === "user") {
userCount += 1;
if (userCount === historyTurns) {
start = i;
break;
}
}
}
return [...earlier.slice(start), ...current];
}