清理废弃的shipping xfa ocr automark相关代码和job配置 新增收料报表管理的前端页面、路由和侧边栏入口 新增后端收料报表配置接口、数据库模型和RPA脚本 优化发货通知流程的重试策略,将shipping相关job默认不自动重试 移除废弃的库存源确认相关逻辑,更新流程门禁规则 重构订单模板的值适配器模块,新增坐标OCR值适配器 清理前端shipping菜单和action的废弃选项 更新相关文档和测试用例,调整.gitignore忽略规则
271 lines
10 KiB
TypeScript
271 lines
10 KiB
TypeScript
/**
|
|
* features/chat/composables/useProcurementContractChat.ts
|
|
* 用途:采购合同审查 Agent 的会话态(模式、双文件输入、异步发起与进度轮询)。
|
|
* 依赖:procurement-contract.api、file-assets.api、agent-registry.utils。
|
|
* 逻辑:模板比对要求我方原合同与卖方回传合同;单份审核仅要求待审合同,结果卡可在刷新后回放。
|
|
*/
|
|
import { computed, ref, watch, type ComputedRef, type Ref } from 'vue';
|
|
import {
|
|
getChatContractReview,
|
|
startChatContractReview,
|
|
type ContractReviewRow,
|
|
} from '@/features/admin/api/procurement-contract.api';
|
|
import { uploadFileAsset } from '@/shared/api/file-assets.api';
|
|
import {
|
|
isProcurementContractAgent as resolveProcurementContractAgent,
|
|
type AgentRegistryFields,
|
|
} from '@/shared/utils/agent-registry.utils';
|
|
import type { ChatMessage } from '../types/chat.types';
|
|
import type { PendingAttachment } from '../types/shipping.types';
|
|
|
|
export type ContractReviewMode = 'compare' | 'solo';
|
|
const REVIEW_POLL_INTERVAL_MS = 2000;
|
|
const REVIEW_POLL_TIMEOUT_MS = 10 * 60 * 1000;
|
|
|
|
export interface ProcurementContractChatOptions {
|
|
currentAgent: Ref<AgentRegistryFields | null>;
|
|
currentAgentId: ComputedRef<number>;
|
|
attachments: Ref<PendingAttachment[]>;
|
|
messages: Ref<ChatMessage[]>;
|
|
isThinking: Ref<boolean>;
|
|
isUploadingAttachments: Ref<boolean>;
|
|
input: Ref<string>;
|
|
clearAttachments: () => void;
|
|
saveLocalMessageToBackend: (msg: ChatMessage, userQuery?: string) => Promise<void>;
|
|
loadHistory: () => Promise<void>;
|
|
msg: { warning: (text: string) => void; success: (text: string) => void; info: (text: string) => void };
|
|
t: (key: string, params?: Record<string, unknown>) => string;
|
|
}
|
|
|
|
/**
|
|
* 从审查 API 结果构建结果卡 payload(去掉过大的冗余后仍保留 Diff/红线数据)。
|
|
*/
|
|
function buildReviewResultPayload(review: ContractReviewRow): ContractReviewRow {
|
|
return {
|
|
id: review.id,
|
|
review_no: review.review_no,
|
|
review_mode: review.review_mode,
|
|
status: review.status,
|
|
contract_file_name: review.contract_file_name,
|
|
supplier_name: review.supplier_name,
|
|
contract_no: review.contract_no,
|
|
contract_type: review.contract_type,
|
|
template_id: review.template_id,
|
|
template_source: review.template_source,
|
|
template_name: review.template_name,
|
|
total_change_count: review.total_change_count,
|
|
high_risk_count: review.high_risk_count,
|
|
medium_risk_count: review.medium_risk_count,
|
|
low_risk_count: review.low_risk_count,
|
|
user_id: review.user_id,
|
|
user_name: review.user_name,
|
|
error_message: review.error_message,
|
|
started_at: review.started_at,
|
|
finished_at: review.finished_at,
|
|
created_at: review.created_at,
|
|
progress: review.progress,
|
|
processing_stage: review.processing_stage,
|
|
processing_message: review.processing_message,
|
|
summary_json: review.summary_json,
|
|
clauses_json: review.clauses_json,
|
|
report_text: review.report_text,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 轮询单次合同审核直到成功、失败或达到前端等待上限。
|
|
* 核心逻辑:后端任务继续独立运行;前端超时只停止本页等待,不取消后台审核。
|
|
* @param reviewNo - 后端返回的合同审查编号
|
|
* @param onProgress - 每轮查询后更新当前消息卡
|
|
*/
|
|
async function pollContractReview(
|
|
reviewNo: string,
|
|
onProgress: (review: ContractReviewRow) => void,
|
|
): Promise<ContractReviewRow> {
|
|
const startedAt = Date.now();
|
|
while (Date.now() - startedAt < REVIEW_POLL_TIMEOUT_MS) {
|
|
const review = await getChatContractReview(reviewNo);
|
|
onProgress(review);
|
|
if (review.status === 'success' || review.status === 'failed') return review;
|
|
await new Promise((resolve) => window.setTimeout(resolve, REVIEW_POLL_INTERVAL_MS));
|
|
}
|
|
throw new Error('contract review polling timed out');
|
|
}
|
|
|
|
/**
|
|
* 采购合同审查 Chat 编排。
|
|
*/
|
|
export function useProcurementContractChat(options: ProcurementContractChatOptions) {
|
|
const isProcurementContractAgent = computed(() =>
|
|
resolveProcurementContractAgent(options.currentAgent.value),
|
|
);
|
|
|
|
const reviewMode = ref<ContractReviewMode>('compare');
|
|
const originalContractAssetNo = ref<string | null>(null);
|
|
const originalContractName = ref<string | null>(null);
|
|
|
|
const allowsSendWithAttachmentsOnly = computed(
|
|
() => isProcurementContractAgent.value && (
|
|
options.attachments.value.some((a) => a.kind === 'local')
|
|
|| (reviewMode.value === 'compare' && Boolean(originalContractAssetNo.value))
|
|
),
|
|
);
|
|
|
|
watch(
|
|
isProcurementContractAgent,
|
|
(active) => {
|
|
if (!active) {
|
|
reviewMode.value = 'compare';
|
|
originalContractAssetNo.value = null;
|
|
originalContractName.value = null;
|
|
}
|
|
},
|
|
{ immediate: true },
|
|
);
|
|
|
|
/**
|
|
* 上传合同比对所需的我方原合同,仅作为本次审核基准,不进入模板库。
|
|
*/
|
|
async function handleOriginalContractSelected(event: Event): Promise<void> {
|
|
const input = event.target as HTMLInputElement;
|
|
const file = input.files?.[0];
|
|
input.value = '';
|
|
if (!file) return;
|
|
// [STEP 1] 选择即展示,避免 FileAsset 网络上传期间输入栏看起来没有响应。
|
|
originalContractAssetNo.value = null;
|
|
originalContractName.value = file.name;
|
|
options.isUploadingAttachments.value = true;
|
|
try {
|
|
const asset = await uploadFileAsset(file);
|
|
// [STEP 2] 上传完成后才允许作为正式比对基准提交。
|
|
originalContractAssetNo.value = asset.asset_no;
|
|
originalContractName.value = asset.original_filename || file.name;
|
|
} catch (e: unknown) {
|
|
// 上传失败的临时卡片不可作为基准,必须移除,避免显示与提交状态不一致。
|
|
clearOriginalContract();
|
|
options.msg.warning(e instanceof Error ? e.message : options.t('chat.procurementContract.uploadFailed'));
|
|
} finally {
|
|
options.isUploadingAttachments.value = false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 清除已上传的我方原合同基准。
|
|
*/
|
|
function clearOriginalContract(): void {
|
|
originalContractAssetNo.value = null;
|
|
originalContractName.value = null;
|
|
}
|
|
|
|
/**
|
|
* 拦截带本地附件的发送:上传 FileAsset → 创建异步审查 → 轮询进度 → 写入最终结果卡。
|
|
* @returns true 表示已消费本次发送
|
|
*/
|
|
async function tryHandleProcurementContractSend(
|
|
textOrEvent: string | Event | undefined,
|
|
_content: string,
|
|
trimmed: string,
|
|
): Promise<boolean> {
|
|
if (!isProcurementContractAgent.value) return false;
|
|
const locals = options.attachments.value.filter(
|
|
(a): a is Extract<PendingAttachment, { kind: 'local' }> => a.kind === 'local',
|
|
);
|
|
if (!locals.length) {
|
|
if (reviewMode.value === 'compare' && originalContractAssetNo.value) {
|
|
options.msg.warning(options.t('chat.procurementContract.compareFilesRequired'));
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
if (options.isThinking.value || options.isUploadingAttachments.value) return true;
|
|
|
|
if (locals.length > 1) {
|
|
options.msg.warning(options.t('chat.procurementContract.oneContractOnly'));
|
|
return true;
|
|
}
|
|
if (reviewMode.value === 'compare' && !originalContractAssetNo.value) {
|
|
options.msg.warning(options.t('chat.procurementContract.compareFilesRequired'));
|
|
return true;
|
|
}
|
|
|
|
const contractFile = locals[0]!.file;
|
|
options.isUploadingAttachments.value = true;
|
|
options.isThinking.value = true;
|
|
try {
|
|
// [STEP 1] 上传待审合同
|
|
const contractAsset = await uploadFileAsset(contractFile);
|
|
|
|
// [STEP 2] 比对模式固定以用户上传的我方原合同作为基准,不再读取默认模板。
|
|
const queuedReview = await startChatContractReview({
|
|
review_mode: reviewMode.value,
|
|
contract_asset_no: contractAsset.asset_no,
|
|
original_contract_asset_no:
|
|
reviewMode.value === 'compare' ? originalContractAssetNo.value : null,
|
|
contract_type: 'general_procurement',
|
|
agent_id: options.currentAgentId.value || null,
|
|
});
|
|
|
|
// [STEP 3] 立即显示用户消息和排队进度卡
|
|
const userText =
|
|
trimmed
|
|
|| options.t('chat.procurementContract.defaultUserQuery', {
|
|
file: contractFile.name,
|
|
mode:
|
|
reviewMode.value === 'solo'
|
|
? options.t('chat.procurementContract.modeSolo')
|
|
: options.t('chat.procurementContract.modeCompare'),
|
|
});
|
|
const userMsg: ChatMessage = { role: 'user', content: userText, type: 'text' };
|
|
options.messages.value.push(userMsg);
|
|
|
|
const assistantMsg: ChatMessage = {
|
|
role: 'assistant',
|
|
type: 'procurement-contract-review',
|
|
content: options.t('chat.procurementContract.reviewQueued'),
|
|
payload: buildReviewResultPayload(queuedReview),
|
|
};
|
|
options.messages.value.push(assistantMsg);
|
|
if (typeof textOrEvent !== 'string') options.input.value = '';
|
|
options.clearAttachments();
|
|
clearOriginalContract();
|
|
options.isUploadingAttachments.value = false;
|
|
options.isThinking.value = false;
|
|
|
|
// [STEP 4] 后台完成后原地替换进度卡,再持久化正式结果。
|
|
const review = await pollContractReview(queuedReview.review_no, (latest) => {
|
|
assistantMsg.payload = buildReviewResultPayload(latest);
|
|
assistantMsg.content = latest.processing_message || options.t('chat.procurementContract.reviewRunning');
|
|
});
|
|
if (review.status === 'failed') {
|
|
throw new Error(review.error_message || options.t('chat.procurementContract.reviewFailed'));
|
|
}
|
|
assistantMsg.payload = buildReviewResultPayload(review);
|
|
assistantMsg.content =
|
|
review.summary_json && typeof review.summary_json.headline === 'string'
|
|
? String(review.summary_json.headline)
|
|
: options.t('chat.procurementContract.resultTitle');
|
|
await options.saveLocalMessageToBackend(assistantMsg, userText);
|
|
await options.loadHistory();
|
|
} catch (e: unknown) {
|
|
options.msg.warning(
|
|
e instanceof Error ? e.message : options.t('chat.procurementContract.reviewFailed'),
|
|
);
|
|
} finally {
|
|
options.isUploadingAttachments.value = false;
|
|
options.isThinking.value = false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
return {
|
|
isProcurementContractAgent,
|
|
reviewMode,
|
|
originalContractAssetNo,
|
|
originalContractName,
|
|
allowsSendWithAttachmentsOnly,
|
|
handleOriginalContractSelected,
|
|
clearOriginalContract,
|
|
tryHandleProcurementContractSend,
|
|
};
|
|
}
|