useInsightContext.ts 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * 研判上下文组装
  3. *
  4. * 把持续联系页的「筛选条件 + 当前结果集」规整成后端要的 context 快照,
  5. * 并产出「条件是否变更」的签名 —— 抽屉开着时用户改了条件再查询,
  6. * 只提示「可重新分析」,不打断正在跑的流(一次分析 = 一个会话 = 一份上下文)。
  7. */
  8. import { computed, type Ref } from 'vue';
  9. import type { InsightCreateParams, InsightFilterSnapshot, InsightRowSnapshot } from '@/call/api/types/insight';
  10. /** 页面传给抽屉的筛选条件(已归一化,抽屉不关心页面表单的内部形状) */
  11. export interface ContinuousInsightFilters {
  12. /** 分析对象(本方姓名);空数组 = 全案对象人员 */
  13. personNames: string[];
  14. /** 持续联系月数阈值 */
  15. monthVal?: number;
  16. startDate?: string;
  17. endDate?: string;
  18. otherName?: string;
  19. otherPhone?: string;
  20. /** 条件命中的关系对总数 */
  21. total?: number;
  22. }
  23. /** 默认研判规模(后端 maxTopN=200 兜底截断) */
  24. export const DEFAULT_TOP_N = 100;
  25. /** 抽屉里的轻量 TOP N 开关(提示词 token 成本与覆盖度的折中) */
  26. export const TOP_N_OPTIONS = [50, 100, 200];
  27. /** 结果快照的一行(兼容页面 mapRow 后的 `{ raw: CallContinuousDTO }` 与裸 DTO 两种形态) */
  28. function toSnapshotRow(item: any): InsightRowSnapshot {
  29. const raw = item?.raw ?? item ?? {};
  30. return {
  31. personName: raw.personName ?? item?.selfName,
  32. otherName: raw.otherName ?? item?.otherName,
  33. otherPhone: raw.otherPhone ?? item?.otherNumber,
  34. pairContMonthCount: Number(raw.pairContMonthCount ?? item?.durationValue ?? 0) || 0,
  35. pairTotalCalls: Number(raw.pairTotalCalls ?? raw.pairTotalCall ?? raw.totalCalls ?? item?.count ?? 0) || 0,
  36. continuousStartMonth: raw.continuousStartMonth,
  37. continuousEndMonth: raw.continuousEndMonth,
  38. };
  39. }
  40. export function useInsightContext(filters: Ref<ContinuousInsightFilters>, rows: Ref<any[]>) {
  41. /** 条件签名:只含用户可见条件,用于「条件已变更」提示 */
  42. const filterSignature = computed(() =>
  43. JSON.stringify({
  44. personNames: [...(filters.value.personNames || [])].sort(),
  45. monthVal: filters.value.monthVal ?? null,
  46. startDate: filters.value.startDate ?? null,
  47. endDate: filters.value.endDate ?? null,
  48. otherName: filters.value.otherName ?? null,
  49. otherPhone: filters.value.otherPhone ?? null,
  50. }),
  51. );
  52. /** 快照签名:条件 + 结果集(行数与前若干行足以判定是否同一批数据) */
  53. const snapshotSignature = computed(() => {
  54. const head = (rows.value || [])
  55. .slice(0, 5)
  56. .map((item) => `${item?.selfName ?? item?.raw?.personName}|${item?.otherNumber ?? item?.raw?.otherPhone}`)
  57. .join(',');
  58. return `${filterSignature.value}#${rows.value?.length || 0}#${head}`;
  59. });
  60. /** 按持续月数降序取前 N 对(页面已排过序,这里再兜一次,保证与提示词口径一致) */
  61. const buildRows = (topN: number): InsightRowSnapshot[] =>
  62. [...(rows.value || [])]
  63. .map(toSnapshotRow)
  64. .sort((a, b) => (b.pairContMonthCount || 0) - (a.pairContMonthCount || 0))
  65. .slice(0, topN);
  66. const buildFilterSnapshot = (): InsightFilterSnapshot => {
  67. const f = filters.value || ({} as ContinuousInsightFilters);
  68. return {
  69. personNames: f.personNames?.length ? [...f.personNames] : undefined,
  70. monthVal: f.monthVal,
  71. startDate: f.startDate,
  72. endDate: f.endDate,
  73. otherName: f.otherName || undefined,
  74. otherPhone: f.otherPhone || undefined,
  75. total: f.total ?? rows.value?.length ?? 0,
  76. };
  77. };
  78. /** 组装创建会话入参 */
  79. const buildCreateParams = (caseId: string, topN = DEFAULT_TOP_N): InsightCreateParams => ({
  80. caseId,
  81. scope: 'BATCH',
  82. filters: buildFilterSnapshot(),
  83. rows: buildRows(topN),
  84. topN,
  85. });
  86. return { filterSignature, snapshotSignature, buildCreateParams };
  87. }