Merge branch 'env' into dev
This commit is contained in:
commit
d79def5519
90 files changed
+16729
-1344
No files matched your search
@@ -6,6 +6,7 @@ import { computed, ref, type ComputedRef, type Ref } from 'vue';
|
||||
import { uploadChatFile } from '../api/chat.api';
|
||||
import type { PendingAttachment } from '../types/shipping.types';
|
||||
import type { ChatDeps } from './chat.deps';
|
||||
import { isProcurementContractAgent as resolveProcurementContractAgent } from '@/shared/utils/agent-registry.utils';
|
||||
|
||||
type ChatUploadFileType = 'image' | 'document' | 'audio' | 'video' | 'custom';
|
||||
|
||||
@@ -23,6 +24,8 @@ const DIFY_SUPPORTED_EXTS: Record<Exclude<ChatUploadFileType, 'custom'>, Set<str
|
||||
video: new Set(['mp4', 'mov', 'mpeg', 'webm']),
|
||||
};
|
||||
|
||||
const PROCUREMENT_CONTRACT_EXTS = new Set(['pdf', 'doc', 'docx', 'txt', 'md', 'markdown']);
|
||||
|
||||
const extractExt = (value: string) => {
|
||||
const text = (value || '').trim().toLowerCase();
|
||||
const withoutQuery = (text.split('?')[0] || '').split('#')[0] || '';
|
||||
@@ -76,9 +79,11 @@ export function useChatAttachments(options: UseChatAttachmentsOptions) {
|
||||
return (currentAgent.value?.dify_parameters?.system_parameters as any) || {};
|
||||
});
|
||||
|
||||
const isProcurementContractAgent = computed(() => resolveProcurementContractAgent(currentAgent.value));
|
||||
|
||||
const attachmentPolicy = computed(() => {
|
||||
const fileUploadAllowed = currentAgent.value?.capabilities?.file_upload;
|
||||
if (fileUploadAllowed === false) {
|
||||
if (fileUploadAllowed === false && !isProcurementContractAgent.value) {
|
||||
return {
|
||||
anyEnabled: false,
|
||||
enabled: {
|
||||
@@ -172,6 +177,10 @@ export function useChatAttachments(options: UseChatAttachmentsOptions) {
|
||||
}
|
||||
}
|
||||
|
||||
if (isProcurementContractAgent.value) {
|
||||
enabled.document = { local: true, remote: false, limit: 1 };
|
||||
}
|
||||
|
||||
const mergedLimits = {
|
||||
...(difySystemParameters.value || {}),
|
||||
...(raw?.fileUploadConfig || {}),
|
||||
@@ -189,6 +198,7 @@ export function useChatAttachments(options: UseChatAttachmentsOptions) {
|
||||
const canLocal = Object.values(enabled).some((v) => v.local);
|
||||
const canRemote = Object.values(enabled).some((v) => v.remote);
|
||||
const totalLimit = (() => {
|
||||
if (isProcurementContractAgent.value) return 1;
|
||||
const v1 = raw?.fileUploadConfig?.file_upload_limit;
|
||||
if (Number.isFinite(Number(v1))) return Number(v1);
|
||||
const v2 = raw?.number_limits;
|
||||
@@ -197,6 +207,7 @@ export function useChatAttachments(options: UseChatAttachmentsOptions) {
|
||||
})();
|
||||
|
||||
const effectiveAllowedExts: Set<string> | null = (() => {
|
||||
if (isProcurementContractAgent.value) return new Set(PROCUREMENT_CONTRACT_EXTS);
|
||||
if (!allowedExts) return null;
|
||||
const filtered = new Set<string>();
|
||||
for (const ext of allowedExts) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* features/chat/composables/useChatPage.ts
|
||||
* 对话页主编排:组合 SSE、Agent、附件、请假/出货、路由同步与 page/* 子模块。
|
||||
* 对话页主编排:组合 SSE、Agent、附件、请假/出货/采购合同、路由同步与 page/* 子模块。
|
||||
*/
|
||||
import { computed, markRaw, nextTick, ref, watch } from 'vue';
|
||||
import { storeToRefs } from 'pinia';
|
||||
@@ -24,6 +24,7 @@ import { useChatRouteSync } from './useChatRouteSync';
|
||||
import { useChatScroll } from './useChatScroll';
|
||||
import { useChatSessions } from './useChatSessions';
|
||||
import { useChatShipping } from './useChatShipping';
|
||||
import { useProcurementContractChat } from './useProcurementContractChat';
|
||||
import { useChatSourceFiles } from './useChatSourceFiles';
|
||||
import { useChatAssetHistory } from './useChatAssetHistory';
|
||||
import { useChatAssetImage } from './useChatAssetImage';
|
||||
@@ -239,6 +240,8 @@ export function useChatPage() {
|
||||
const { newLocalMessageId, saveLocalMessageToBackend } = createChatPageLocalMessageApi({
|
||||
conversationId,
|
||||
currentAgentId,
|
||||
route,
|
||||
router,
|
||||
});
|
||||
|
||||
const pushLocalAssistantMessage = async (content: string) => {
|
||||
@@ -304,6 +307,27 @@ export function useChatPage() {
|
||||
});
|
||||
resetShippingConversationState = shipping.resetShippingConversationState;
|
||||
|
||||
const procurement = useProcurementContractChat({
|
||||
currentAgent: agent.currentAgent,
|
||||
currentAgentId,
|
||||
attachments: attachmentApi.attachments,
|
||||
messages,
|
||||
isThinking,
|
||||
isUploadingAttachments: attachmentApi.isUploadingAttachments,
|
||||
input,
|
||||
clearAttachments: attachmentApi.clearAttachments,
|
||||
saveLocalMessageToBackend,
|
||||
loadHistory,
|
||||
msg,
|
||||
t,
|
||||
});
|
||||
|
||||
const allowsSendWithAttachmentsOnly = computed(
|
||||
() =>
|
||||
shipping.allowsSendWithAttachmentsOnly.value
|
||||
|| procurement.allowsSendWithAttachmentsOnly.value,
|
||||
);
|
||||
|
||||
/**
|
||||
* Drawer「重试所有失败」点击:仅打开确认弹窗,不立刻发请求。
|
||||
*/
|
||||
@@ -413,7 +437,7 @@ export function useChatPage() {
|
||||
isUploadingAttachments: attachmentApi.isUploadingAttachments,
|
||||
attachmentPolicy: attachmentApi.attachmentPolicy,
|
||||
attachments: attachmentApi.attachments,
|
||||
allowsSendWithAttachmentsOnly: shipping.allowsSendWithAttachmentsOnly,
|
||||
allowsSendWithAttachmentsOnly,
|
||||
buildFileSpecsForSend: attachmentApi.buildFileSpecsForSend,
|
||||
clearAttachments: attachmentApi.clearAttachments,
|
||||
sendMessage,
|
||||
@@ -445,6 +469,7 @@ export function useChatPage() {
|
||||
tryHandleShippingAttachmentAutoFlow: shipping.tryHandleShippingAttachmentAutoFlow,
|
||||
tryHandleLeaveSend: leave.tryHandleLeaveSend,
|
||||
tryHandleShippingSend: shipping.tryHandleShippingSend,
|
||||
tryHandleProcurementContractSend: procurement.tryHandleProcurementContractSend,
|
||||
refreshToolRunProgressAfterSend: toolRun.refreshToolRunProgressAfterSend,
|
||||
prepareToolRunProgressBeforeSend: toolRun.prepareToolRunProgressBeforeSend,
|
||||
msg,
|
||||
@@ -607,6 +632,11 @@ export function useChatPage() {
|
||||
clearShippingToast: dialogs.clearShippingToast,
|
||||
showChatRating,
|
||||
isShippingNoticeAgent: shipping.isShippingNoticeAgent,
|
||||
isProcurementContractAgent: procurement.isProcurementContractAgent,
|
||||
procurementReviewMode: procurement.reviewMode,
|
||||
procurementOriginalContractName: procurement.originalContractName,
|
||||
handleProcurementOriginalContractSelected: procurement.handleOriginalContractSelected,
|
||||
clearProcurementOriginalContract: procurement.clearOriginalContract,
|
||||
showShippingProgressWorkspace: shipping.showShippingProgressWorkspace,
|
||||
shippingBatchSummary: shipping.shippingBatchSummary,
|
||||
shippingConversationWorkspace: shipping.shippingConversationWorkspace,
|
||||
|
||||
@@ -0,0 +1,264 @@
|
||||
/**
|
||||
* 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;
|
||||
options.isUploadingAttachments.value = true;
|
||||
try {
|
||||
const asset = await uploadFileAsset(file);
|
||||
originalContractAssetNo.value = asset.asset_no;
|
||||
originalContractName.value = asset.original_filename || file.name;
|
||||
} catch (e: unknown) {
|
||||
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,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user