56 lines
3.1 KiB
TypeScript
56 lines
3.1 KiB
TypeScript
import { CATEGORY_LABELS, GROUP_LABELS, type AssistantResult } from "../../../shared/assets/assistant";
|
|
|
|
export const BUSINESS_QUESTIONS = [
|
|
"现在有多少车在运营、多少车在库存?整体运营率是多少?",
|
|
"库存主要集中在哪些车型和区域?按数量从多到少统计。",
|
|
"最近7天交车、还车分别多少?其中替换交车多少?",
|
|
"查看当前异动车辆,显示异动类型和预计完成时间。",
|
|
] as const;
|
|
|
|
export function conversationTitle(question: string, result?: AssistantResult): string {
|
|
if (result?.sections?.length) return `${result.sections.length} 项关联分析`;
|
|
if (result?.presentation?.title) return result.presentation.title.slice(0, 24);
|
|
if (!result || result.plan.kind === "clarify") {
|
|
if (/历史|上个?月月底|损失|成本/.test(question)) return "历史与成本分析";
|
|
return question.replace(/[\s,。?!;:、]/g, "").slice(0, 14) || "资产分析";
|
|
}
|
|
const p = result.plan;
|
|
if (p.analysis === "briefing") return "资产总览";
|
|
if (p.kind === "flow") return p.comparePrevious ? "交还车周期对比" : "交还车统计";
|
|
if (p.view === "list") return `${CATEGORY_LABELS[p.category].replace("车辆", "")}清单`;
|
|
const category = {all:"资产",inventory:"库存",available:"可运营库存",awaiting:"待运营库存",operating:"运营",pending:"待交车",abnormal:"异动"}[p.category];
|
|
return `${p.groupBy === "none" ? "" : GROUP_LABELS[p.groupBy]}${category}`;
|
|
}
|
|
|
|
|
|
type ResultTurn = { result?: AssistantResult; resultHistory?: AssistantResult[] };
|
|
export function findConversationResult(turns: ResultTurn[], resultId: string) {
|
|
for (const turn of turns) {
|
|
if (turn.result?.resultId === resultId) return turn.result;
|
|
const current = turn.result?.sections?.find(section => section.result?.resultId === resultId)?.result;
|
|
if (current) return current;
|
|
for (const historical of [...(turn.resultHistory || [])].reverse()) {
|
|
if (historical.resultId === resultId) return historical;
|
|
const child = historical.sections?.find(section => section.result?.resultId === resultId)?.result;
|
|
if (child) return child;
|
|
}
|
|
}
|
|
}
|
|
export function replaceAnalysisResult<T extends ResultTurn>(turn: T, result: AssistantResult, baseResultId?: string): T & ResultTurn {
|
|
const history = [...(turn.resultHistory || [])];
|
|
const sectionId = baseResultId ? [turn.result, ...history.slice().reverse()]
|
|
.flatMap(item => item?.sections || [])
|
|
.find(section => section.result?.resultId === baseResultId)?.id : undefined;
|
|
if (turn.result?.resultId && (turn.result.sections || !history.some(item => item.resultId === turn.result!.resultId))) history.push(turn.result);
|
|
if (sectionId && turn.result?.sections) {
|
|
return { ...turn, result: {
|
|
...turn.result,
|
|
sections: turn.result.sections.map(section => section.id === sectionId ? {
|
|
...section, result, error: undefined,
|
|
display: result.plan.groupBy === "none" && result.plan.view === "chart" && result.plan.kind === "assets" ? "metrics" : result.plan.view,
|
|
} : section),
|
|
}, resultHistory: history };
|
|
}
|
|
return { ...turn, result, resultHistory: history };
|
|
}
|