fix: truncate rwfx 3q context
This commit is contained in:
1 parent
daff70c34f
commit
7e7d3c9846
2 files changed
+48
-4
No files matched your search
@@ -66,6 +66,38 @@ import type { RwfxSqGateStartContext } from "@/strategy-components/components/ta
|
|||||||
|
|
||||||
/** 内置「模板数据转换器」agent 的稳定 id。仅此 agent 的对话页显示「页面模板 + 渲染预览」调试入口。 */
|
/** 内置「模板数据转换器」agent 的稳定 id。仅此 agent 的对话页显示「页面模板 + 渲染预览」调试入口。 */
|
||||||
const TEMPLATE_BUILDER_AGENT_ID = "template-json-builder";
|
const TEMPLATE_BUILDER_AGENT_ID = "template-json-builder";
|
||||||
|
const RWFX_SQ_REPORT_CONTEXT_MAX_CHARS = 12000;
|
||||||
|
const RWFX_SQ_REPORT_CONTEXT_TAIL_CHARS = 2000;
|
||||||
|
|
||||||
|
function stringifyForHiddenContext(value: unknown): string {
|
||||||
|
if (typeof value === "string") return value;
|
||||||
|
try {
|
||||||
|
const text = JSON.stringify(value, null, 2);
|
||||||
|
return typeof text === "string" ? text : "";
|
||||||
|
} catch {
|
||||||
|
return String(value ?? "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function truncateMiddle(text: string, maxChars: number): string {
|
||||||
|
if (text.length <= maxChars) return text;
|
||||||
|
const marker = "\n\n...[内容过长,已截断中间部分]...\n\n";
|
||||||
|
const tailChars = Math.min(RWFX_SQ_REPORT_CONTEXT_TAIL_CHARS, Math.floor(maxChars / 4));
|
||||||
|
const headChars = Math.max(0, maxChars - marker.length - tailChars);
|
||||||
|
return `${text.slice(0, headChars)}${marker}${text.slice(-tailChars)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function compactSqReportForContext(report: unknown): unknown {
|
||||||
|
if (report == null) return null;
|
||||||
|
const serialized = stringifyForHiddenContext(report);
|
||||||
|
if (serialized.length <= RWFX_SQ_REPORT_CONTEXT_MAX_CHARS) return report;
|
||||||
|
return {
|
||||||
|
truncated: true,
|
||||||
|
originalChars: serialized.length,
|
||||||
|
maxChars: RWFX_SQ_REPORT_CONTEXT_MAX_CHARS,
|
||||||
|
content: truncateMiddle(serialized, RWFX_SQ_REPORT_CONTEXT_MAX_CHARS),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function rwfxSqReportRunContext(
|
function rwfxSqReportRunContext(
|
||||||
gateContext?: RwfxSqGateStartContext,
|
gateContext?: RwfxSqGateStartContext,
|
||||||
@@ -78,9 +110,9 @@ function rwfxSqReportRunContext(
|
|||||||
title: "三情分析(3q)研判情报报告",
|
title: "三情分析(3q)研判情报报告",
|
||||||
usage:
|
usage:
|
||||||
gateContext.sqReportStatus === "complete"
|
gateContext.sqReportStatus === "complete"
|
||||||
? "这是任务分析(rwfx)问答的隐藏业务上下文。回答时应参考这些已完成的 3q 结果,但不要向用户说明页面注入了隐藏上下文,除非用户明确询问。"
|
? "这是任务分析(rwfx)问答的隐藏业务上下文。回答时应参考这些已完成的 3q 结果;如 data.truncated=true,说明内容已截断,只能基于可见片段分析。不要向用户说明页面注入了隐藏上下文,除非用户明确询问。"
|
||||||
: "3q 研判情报报告当前未提供有效结果。回答时不要编造 3q 结果,可仅基于任务信息继续分析。",
|
: "3q 研判情报报告当前未提供有效结果。回答时不要编造 3q 结果,可仅基于任务信息继续分析。",
|
||||||
data: gateContext.sqReport ?? null,
|
data: compactSqReportForContext(gateContext.sqReport),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-2
@@ -17,6 +17,17 @@ from langchain_core.messages import HumanMessage
|
|||||||
|
|
||||||
_CONTEXT_KEY = "task_external_context"
|
_CONTEXT_KEY = "task_external_context"
|
||||||
_TAG = "task_external_context"
|
_TAG = "task_external_context"
|
||||||
|
_MAX_CONTEXT_CHARS = 18000
|
||||||
|
_TAIL_CONTEXT_CHARS = 2500
|
||||||
|
|
||||||
|
|
||||||
|
def _truncate_middle(text: str, max_chars: int = _MAX_CONTEXT_CHARS) -> str:
|
||||||
|
if len(text) <= max_chars:
|
||||||
|
return text
|
||||||
|
marker = "\n\n...[隐藏任务上下文过长,已截断中间部分]...\n\n"
|
||||||
|
tail_chars = min(_TAIL_CONTEXT_CHARS, max_chars // 4)
|
||||||
|
head_chars = max(0, max_chars - len(marker) - tail_chars)
|
||||||
|
return f"{text[:head_chars]}{marker}{text[-tail_chars:]}"
|
||||||
|
|
||||||
|
|
||||||
def _render_context(value: Any) -> str:
|
def _render_context(value: Any) -> str:
|
||||||
@@ -25,9 +36,10 @@ def _render_context(value: Any) -> str:
|
|||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
return value.strip()
|
return value.strip()
|
||||||
try:
|
try:
|
||||||
return json.dumps(value, ensure_ascii=False, indent=2, default=str).strip()
|
text = json.dumps(value, ensure_ascii=False, indent=2, default=str).strip()
|
||||||
except Exception:
|
except Exception:
|
||||||
return str(value).strip()
|
text = str(value).strip()
|
||||||
|
return _truncate_middle(text)
|
||||||
|
|
||||||
|
|
||||||
def _task_context_from_request(request: Any) -> Any:
|
def _task_context_from_request(request: Any) -> Any:
|
||||||
|
|||||||
Reference in new issue
Block a user