feat(#172): route wires the turn-card decision, the reuse message and the cache store (BUG-1815)
- 在本轮消息组装前算指纹与判定(追问且全部要素不变才算命中);跨日、换领域、换口径、 换版本都会得到对应原因码 - 每轮都写一条回执步骤 turn-card-cache:<decision>:<reason> - 命中时把上一轮视图作为一条 user 消息插在本轮问题之前,开头写明「本轮沿用上一轮的计算结果…」 - agentContext 传 turnCard:命中预置、每轮算完写回缓存(含参数与参考日期) - 版本要素取 CONSULTATION_MODEL_PACKET_VERSION / EVIDENCE_CARD_VERSION, 构建版本取 NEXT_PUBLIC_APP_VERSION 或 DEPLOY_GIT_SHA(没有则 "unknown") Tests: consultation-agentic-runtime 87 条仍全过。 Co-Authored-By: Grok (agent loop)
This commit is contained in:
co-authored by
Grok (agent loop)
parent
b337e2aa06
commit
ff89cb98e1
@@ -103,7 +103,17 @@ import {
|
||||
createConsultationRuntimeHooks,
|
||||
createConsultationRuntimeState,
|
||||
publicConsultationRuntimeSteps,
|
||||
CONSULTATION_MODEL_PACKET_VERSION,
|
||||
appendConsultationRuntimeStep,
|
||||
} from "@/mastra/consultation-tools";
|
||||
import { EVIDENCE_CARD_VERSION } from "../../../src/lib/consultation-evidence-card.ts";
|
||||
import {
|
||||
decideTurnCardReuse,
|
||||
peekTurnCard,
|
||||
storeTurnCard,
|
||||
turnCardFingerprint,
|
||||
type TurnCardPayload,
|
||||
} from "../../../lib/consult/turn-card-cache.ts";
|
||||
import {
|
||||
applyBirthTimeModeToWorkflowContext,
|
||||
ACCEPTED_RANGE_READING_INSTRUCTION,
|
||||
@@ -1206,6 +1216,45 @@ export async function POST(request: Request) {
|
||||
: consultationMode === "declared_birth_window"
|
||||
? declaredWindowInstruction()
|
||||
: `${natalInstruction}${adoptedRangeNote}`;
|
||||
// BUG-1815:追问轮是否沿用上一轮的计算结果。只有出生资料、全部口径、领域、参考日期与
|
||||
// 版本都没变才沿用;任何一项变了就重算(reason 由判定给出)。
|
||||
const turnCardReferenceDate = new Date(requestTime.getTime() + 8 * 60 * 60 * 1000).toISOString().slice(0, 10);
|
||||
const birthToolInput = prepared.serverChart?.toolInput;
|
||||
const turnCardFingerprintValue = turnCardFingerprint({
|
||||
birth: {
|
||||
year: birthToolInput?.year, month: birthToolInput?.month, day: birthToolInput?.day,
|
||||
hour: birthToolInput?.hour, minute: birthToolInput?.minute,
|
||||
lat: birthToolInput?.lat, lon: birthToolInput?.lon, tz: birthToolInput?.tz,
|
||||
birthDate: prepared.serverChart?.truth?.birthDate,
|
||||
birthTimeSource: prepared.serverChart?.truth?.birthTimeSource,
|
||||
birthTimeStatus: prepared.serverChart?.truth?.birthTimeStatus,
|
||||
},
|
||||
ayanamsa: String(birthToolInput?.ayanamsa ?? ""),
|
||||
nodeMode: String((birthToolInput as Record<string, unknown> | undefined)?.node_mode ?? ""),
|
||||
positionMode: String((birthToolInput as Record<string, unknown> | undefined)?.position_mode ?? ""),
|
||||
functionalProfile: requestedFunctionalProfile ?? "",
|
||||
answerMode,
|
||||
gender: prepared.serverChart?.gender ?? "",
|
||||
domains: [consultationTheme],
|
||||
referenceDate: turnCardReferenceDate,
|
||||
packetVersion: CONSULTATION_MODEL_PACKET_VERSION,
|
||||
cardVersion: EVIDENCE_CARD_VERSION,
|
||||
buildVersion: process.env.NEXT_PUBLIC_APP_VERSION ?? process.env.DEPLOY_GIT_SHA ?? "unknown",
|
||||
});
|
||||
const turnCardDecision = followUpTurn && prepared.serverChart
|
||||
? decideTurnCardReuse(sessionId, turnCardFingerprintValue, {
|
||||
referenceDate: turnCardReferenceDate,
|
||||
ayanamsa: String(birthToolInput?.ayanamsa ?? ""),
|
||||
nodeMode: String((birthToolInput as Record<string, unknown> | undefined)?.node_mode ?? ""),
|
||||
positionMode: String((birthToolInput as Record<string, unknown> | undefined)?.position_mode ?? ""),
|
||||
})
|
||||
: { decision: "recomputed" as const, reason: "no_entry" as const };
|
||||
const reusedTurnCard = turnCardDecision.decision === "reused" ? peekTurnCard(sessionId) : undefined;
|
||||
appendConsultationRuntimeStep(state, {
|
||||
kind: "validation",
|
||||
name: `turn-card-cache:${turnCardDecision.decision}:${turnCardDecision.reason}`,
|
||||
status: "completed",
|
||||
});
|
||||
const consultationBaseMessages = (overflow: boolean) => {
|
||||
const tail = overflow ? lastConsultationPair(history) : history;
|
||||
const mapped = tail.map((message) => message.role === "user"
|
||||
@@ -1217,6 +1266,15 @@ export async function POST(request: Request) {
|
||||
return [
|
||||
...(cacheBoundary ? [cacheBoundary] : []),
|
||||
...cachedTail,
|
||||
...(reusedTurnCard
|
||||
? [{
|
||||
role: "user" as const,
|
||||
content: [
|
||||
"本轮沿用上一轮的计算结果(出生资料、口径、领域、日期和版本都没变)。请先核对其中的日期和参数是否就是本轮的,再据此回答;不需要再调用计算工具。",
|
||||
JSON.stringify(reusedTurnCard.payload.modelView),
|
||||
].join("\n"),
|
||||
}]
|
||||
: []),
|
||||
{
|
||||
role: "user" as const,
|
||||
content: consultationUserTurnContent({
|
||||
@@ -1578,6 +1636,23 @@ export async function POST(request: Request) {
|
||||
requestTime,
|
||||
answerMode,
|
||||
...(answerMode === "professional" && requestedFunctionalProfile ? { functionalProfile: requestedFunctionalProfile } : {}),
|
||||
// BUG-1815:命中时把上一轮结果预置进工具;每一轮算完都写回缓存,供下一轮追问使用。
|
||||
turnCard: {
|
||||
decision: turnCardDecision,
|
||||
fingerprint: turnCardFingerprintValue.fingerprint,
|
||||
referenceDate: turnCardReferenceDate,
|
||||
...(reusedTurnCard ? { reused: reusedTurnCard.payload } : {}),
|
||||
store: (payload: TurnCardPayload) => storeTurnCard(sessionId, turnCardFingerprintValue, {
|
||||
referenceDate: turnCardReferenceDate,
|
||||
parameters: {
|
||||
ayanamsa: String(birthToolInput?.ayanamsa ?? ""),
|
||||
nodeMode: String((birthToolInput as Record<string, unknown> | undefined)?.node_mode ?? ""),
|
||||
positionMode: String((birthToolInput as Record<string, unknown> | undefined)?.position_mode ?? ""),
|
||||
},
|
||||
payload,
|
||||
writtenAt: Date.now(),
|
||||
}),
|
||||
},
|
||||
});
|
||||
const agent = getJyotishAgent(selectedModel, agentContext);
|
||||
const result = await streamWithOverflowRetry(agent, natalStreamOptions);
|
||||
|
||||
Reference in New Issue
Block a user