156 lines
6.8 KiB
TypeScript
156 lines
6.8 KiB
TypeScript
/**
|
||
* 请假:自然语言意图解析与强/弱意图区分。
|
||
*/
|
||
import { LEAVE_TYPE_MAP } from '../constants/leave';
|
||
import type { ExtractedLeaveInfo } from '../types/leave.types';
|
||
import { calculateDurationHours, isLeaveTypeByDay, parseDateTime } from './leaveDateTime';
|
||
|
||
export function extractLeaveTypeFromIntent(content: string): string {
|
||
if (!content) return '';
|
||
for (const [key, val] of Object.entries(LEAVE_TYPE_MAP)) {
|
||
if (content.includes(key)) return val;
|
||
}
|
||
return '';
|
||
}
|
||
|
||
export function extractLeaveInfoFromIntent(content: string): ExtractedLeaveInfo | null {
|
||
const normalized = content.replace(/\s+/g, ' ').trim();
|
||
let leaveType = extractLeaveTypeFromIntent(normalized);
|
||
let startTime = '';
|
||
let endTime = '';
|
||
let durationHours = 0;
|
||
|
||
const today = new Date();
|
||
const thisYear = today.getFullYear();
|
||
|
||
const parseDateStr = (m: RegExpMatchArray | null, groupOffset: number): Date | null => {
|
||
if (!m) return null;
|
||
const rawMo = m[groupOffset];
|
||
const rawDa = m[groupOffset + 1];
|
||
if (!rawMo || !rawDa) return null;
|
||
const mo = parseInt(rawMo, 10);
|
||
const da = parseInt(rawDa, 10);
|
||
if (Number.isNaN(mo) || Number.isNaN(da) || mo < 1 || mo > 12 || da < 1 || da > 31) return null;
|
||
return new Date(thisYear, mo - 1, da, 9, 0);
|
||
};
|
||
|
||
const fmtDt = (d: Date, hour: number, minute: number) => {
|
||
const y = d.getFullYear();
|
||
const mo = String(d.getMonth() + 1).padStart(2, '0');
|
||
const da = String(d.getDate()).padStart(2, '0');
|
||
const h = String(hour).padStart(2, '0');
|
||
const mi = String(minute).padStart(2, '0');
|
||
return `${y}-${mo}-${da}T${h}:${mi}`;
|
||
};
|
||
|
||
let startDay: Date | null = null;
|
||
let endDay: Date | null = null;
|
||
const startHM = { h: 9, m: 0 };
|
||
let endHM = { h: 18, m: 0 };
|
||
|
||
const rangeM = normalized.match(/从?(\d{1,2})[月./-](\d{1,2})[日号]?[到至—\-–](\d{1,2})[月./-]?(\d{1,2})[日号]?/);
|
||
if (rangeM) {
|
||
const s = parseDateStr(rangeM, 1);
|
||
if (rangeM[3] && rangeM[3].length <= 2 && rangeM[4] && rangeM[4].length <= 2) {
|
||
const eMo = parseInt(rangeM[3]!, 10);
|
||
const eDa = parseInt(rangeM[4]!, 10);
|
||
if (!Number.isNaN(eMo) && eMo >= 1 && eMo <= 12 && !Number.isNaN(eDa) && eDa >= 1 && eDa <= 31) {
|
||
endDay = new Date(thisYear, eMo - 1, eDa, 18, 0);
|
||
} else if (!Number.isNaN(eDa) && eDa >= 1 && eDa <= 31 && s) {
|
||
endDay = new Date(thisYear, s.getMonth(), eDa, 18, 0);
|
||
}
|
||
}
|
||
if (s) startDay = s;
|
||
}
|
||
|
||
if (!startDay) {
|
||
const singleM = normalized.match(/(\d{1,2})[月./-](\d{1,2})[日号]?/);
|
||
if (singleM) {
|
||
const s = parseDateStr(singleM, 1);
|
||
if (s) {
|
||
startDay = s;
|
||
const dayUnitM = normalized.match(/(\d+\.?\d*)\s*[天日]/);
|
||
if (dayUnitM?.[1]) {
|
||
const days = parseFloat(dayUnitM[1]);
|
||
if (days > 1) endDay = new Date(s.getFullYear(), s.getMonth(), s.getDate() + Math.ceil(days) - 1, 18, 0);
|
||
else endDay = new Date(s.getFullYear(), s.getMonth(), s.getDate(), 18, 0);
|
||
} else {
|
||
endDay = new Date(s.getFullYear(), s.getMonth(), s.getDate(), 18, 0);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
const hourM = normalized.match(/(\d+\.?\d*)\s*小时/);
|
||
if (hourM?.[1]) {
|
||
durationHours = parseFloat(hourM[1]);
|
||
if (startDay && !endDay) {
|
||
endDay = new Date(startDay.getTime() + durationHours * 3600000);
|
||
}
|
||
}
|
||
|
||
const dayM = normalized.match(/(\d+\.?\d*)\s*[天日]/);
|
||
if (dayM?.[1] && !hourM) {
|
||
const days = parseFloat(dayM[1]);
|
||
durationHours = days * 8;
|
||
if (startDay && !endDay) {
|
||
endDay = new Date(startDay.getFullYear(), startDay.getMonth(), startDay.getDate() + Math.ceil(days) - 1, 18, 0);
|
||
}
|
||
}
|
||
|
||
if (startDay) {
|
||
if (durationHours > 0 && durationHours <= 8 && !endDay) {
|
||
endDay = new Date(startDay.getTime() + durationHours * 3600000);
|
||
}
|
||
startTime = fmtDt(startDay, startHM.h, startHM.m);
|
||
}
|
||
|
||
if (endDay) {
|
||
if (hourM && durationHours <= 8) {
|
||
endHM = { h: 9 + Math.floor(durationHours), m: (durationHours % 1) * 60 };
|
||
} else if (!hourM && durationHours > 0 && durationHours <= 8) {
|
||
endHM = { h: 9 + Math.floor(durationHours), m: 0 };
|
||
}
|
||
endTime = fmtDt(endDay, endHM.h, endHM.m);
|
||
}
|
||
|
||
if (startTime && endTime) {
|
||
if (isLeaveTypeByDay(leaveType)) {
|
||
const s = parseDateTime(startTime);
|
||
const e = parseDateTime(endTime);
|
||
if (s && e && e.getTime() > s.getTime()) {
|
||
const sd = new Date(s.getFullYear(), s.getMonth(), s.getDate());
|
||
const ed = new Date(e.getFullYear(), e.getMonth(), e.getDate());
|
||
const days = Math.max(1, Math.round((ed.getTime() - sd.getTime()) / (24 * 3600 * 1000)) + 1);
|
||
durationHours = days * 8;
|
||
}
|
||
} else {
|
||
durationHours = durationHours || calculateDurationHours(startTime, endTime);
|
||
}
|
||
}
|
||
|
||
if (!leaveType && !startTime) return null;
|
||
return { leaveType, startTime, endTime, durationHours };
|
||
}
|
||
|
||
export function getLeaveIntentNormalized(content: string): string {
|
||
return (content || '').replace(/\s+/g, '');
|
||
}
|
||
|
||
/** 区分「真请假」与「咨询流程/材料」类弱意图 */
|
||
export function isStrongLeaveIntent(content: string): boolean {
|
||
const normalized = getLeaveIntentNormalized(content);
|
||
// [STEP 1] 薪资/扣薪属于制度计算问答,必须交给 Dify/FAQ,不能被“请假”关键词抢先拦截为申请表单。
|
||
// [PROMPT_UPDATE] 保留知识库中的计算公式、适用条件和口径,由知识库回答而非前端自行生成。
|
||
const leaveSalaryConsultationIntent = /(请假|休假|年假|事假|病假).{0,12}(薪资|工资|薪酬|扣薪|扣钱|扣款|扣除|多少钱|福利|计算|公式)|(?:薪资|工资|薪酬|扣薪|扣钱|扣款|扣除|多少钱|福利|计算公式).{0,12}(请假|休假|年假|事假|病假)/;
|
||
if (leaveSalaryConsultationIntent.test(normalized)) return false;
|
||
const weakConsultationIntent = /(请假流程|请假怎么走|请假步骤|请假如何申请|请假如何走流程|请假怎么办|请假需要什么|请假规定|请假管理办法|请假制度)/;
|
||
const weakLeaveQuestionIntent = /((怎么|如何).{0,8}(请假|请年假|休假|休年假|年假)|(请假|年假|休假).{0,12}(可以|可否|能否|是否|支持).{0,8}(按小时|按照小时|小时请|小时休))/;
|
||
const materialConsultationIntent = /(提交哪些|提交什么|需要哪些|需要什么|要提交哪些|要提交什么|哪些资料|哪些材料|什么资料|什么材料|申请材料|所需资料|所需材料)/;
|
||
if (weakConsultationIntent.test(normalized)) return false;
|
||
if (weakLeaveQuestionIntent.test(normalized)) return false;
|
||
if (materialConsultationIntent.test(normalized)) return false;
|
||
if (/^(请假|休假|年假|事假|病假|调休|休年假|请事假|请病假|请年假)$/.test(normalized)) return true;
|
||
return /(我要请|申请请|发起请|请假申请|我要休|申请休|想请|帮我请|代我请|提交请|帮我申请|我要申请|想申请|申请.*假|请.*假|休.*假)/.test(normalized);
|
||
}
|