143 lines
5.4 KiB
TypeScript
143 lines
5.4 KiB
TypeScript
/**
|
|
* 出货聊天:人工决策提交(confirm/reject/reparse 等)与手工上传标记。
|
|
*/
|
|
import {
|
|
autoProgressWorkspaceShippingTask,
|
|
markWorkspaceShippingManualUploaded,
|
|
submitWorkspaceShippingDecision,
|
|
} from '@/features/chat/api/shipping.api';
|
|
import { formatShippingDecisionAssistantMessage } from './shippingDecisionFormat';
|
|
import type { ChatMessage, ChatSessionItem } from '../types/chat.types';
|
|
import type { ComputedRef, Ref } from 'vue';
|
|
|
|
export interface ShippingDecisionContext {
|
|
messages: Ref<ChatMessage[]>;
|
|
conversationId: Ref<string | null>;
|
|
history: Ref<ChatSessionItem[]>;
|
|
isShippingNoticeAgent: ComputedRef<boolean>;
|
|
shippingInventorySchemaPreviewCache: Ref<Record<string, unknown>>;
|
|
getActiveShippingTaskId: () => string;
|
|
newLocalMessageId: (prefix: string) => string;
|
|
saveLocalMessageToBackend: (msg: ChatMessage, userQuery?: string) => Promise<void>;
|
|
loadHistory: () => Promise<void>;
|
|
openShippingInventoryMappingFormForTask: (taskId: string) => Promise<boolean>;
|
|
refreshShippingActions: () => Promise<void>;
|
|
msg: { warning: (text: string) => void; success: (text: string) => void };
|
|
t: (key: string, params?: Record<string, unknown>) => string;
|
|
}
|
|
|
|
export function createShippingDecisionHandlers(ctx: ShippingDecisionContext) {
|
|
const syncHistoryIfNeeded = async () => {
|
|
if (!ctx.conversationId.value && ctx.history.value.length === 0) {
|
|
await ctx.loadHistory();
|
|
} else if (ctx.conversationId.value) {
|
|
const exists = ctx.history.value.some((h) => h.conversation_id === ctx.conversationId.value);
|
|
if (!exists) await ctx.loadHistory();
|
|
}
|
|
};
|
|
|
|
const executeShippingDecision = async (decision: string, displayText?: string) => {
|
|
const taskId = ctx.getActiveShippingTaskId();
|
|
if (!taskId) {
|
|
ctx.msg.warning(ctx.t('chat.shippingNoticeMissingTaskId'));
|
|
return false;
|
|
}
|
|
try {
|
|
const result =
|
|
decision === 'confirm_inventory_mapping'
|
|
? await autoProgressWorkspaceShippingTask(taskId)
|
|
: await submitWorkspaceShippingDecision(taskId, decision);
|
|
if (decision === 'correct_inventory_mapping') {
|
|
const preview = result?.data?.schema_preview;
|
|
const key = String(taskId || '').trim().toUpperCase();
|
|
if (key && preview && typeof preview === 'object') {
|
|
ctx.shippingInventorySchemaPreviewCache.value = {
|
|
...ctx.shippingInventorySchemaPreviewCache.value,
|
|
[key]: preview,
|
|
};
|
|
}
|
|
}
|
|
const userText = (displayText || decision).trim();
|
|
const assistantContent = formatShippingDecisionAssistantMessage(
|
|
decision,
|
|
result,
|
|
taskId,
|
|
ctx.t('chat.shippingNoticeDecisionDone'),
|
|
);
|
|
const userMsg: ChatMessage = {
|
|
role: 'user',
|
|
type: 'text',
|
|
content: `${userText}\n任务编号:${taskId}`,
|
|
local_id: ctx.newLocalMessageId('shipping-user'),
|
|
local_only: true,
|
|
};
|
|
const assistantMsg: ChatMessage = {
|
|
role: 'assistant',
|
|
type: 'text',
|
|
content: assistantContent,
|
|
local_id: ctx.newLocalMessageId('shipping-assistant'),
|
|
local_only: true,
|
|
};
|
|
ctx.messages.value.push(userMsg, assistantMsg);
|
|
await ctx.saveLocalMessageToBackend(userMsg, userText);
|
|
await ctx.saveLocalMessageToBackend(assistantMsg, userText);
|
|
if (result.message && !ctx.isShippingNoticeAgent.value) {
|
|
ctx.msg.success(result.message);
|
|
}
|
|
await syncHistoryIfNeeded();
|
|
void ctx.refreshShippingActions();
|
|
if (decision === 'correct_inventory_mapping') {
|
|
await ctx.openShippingInventoryMappingFormForTask(taskId);
|
|
}
|
|
return true;
|
|
} catch (e: unknown) {
|
|
const errorMessage = e instanceof Error ? e.message : ctx.t('chat.shippingNoticeUploadFailed');
|
|
ctx.msg.warning(errorMessage);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
const executeShippingManualUpload = async (taskId: string, displayText?: string) => {
|
|
try {
|
|
const result = await markWorkspaceShippingManualUploaded(taskId);
|
|
const userText = (displayText || '已在货代系统手工上传完成').trim();
|
|
const assistantContent = [
|
|
String(result.message || '已标记为手工上传货代系统完成。').trim(),
|
|
`任务编号:${taskId}`,
|
|
].join('\n');
|
|
const userMsg: ChatMessage = {
|
|
role: 'user',
|
|
type: 'text',
|
|
content: `${userText}\n任务编号:${taskId}`,
|
|
local_id: ctx.newLocalMessageId('shipping-user'),
|
|
local_only: true,
|
|
};
|
|
const assistantMsg: ChatMessage = {
|
|
role: 'assistant',
|
|
type: 'text',
|
|
content: assistantContent,
|
|
local_id: ctx.newLocalMessageId('shipping-assistant'),
|
|
local_only: true,
|
|
shipping_actions_used: true,
|
|
};
|
|
ctx.messages.value.push(userMsg, assistantMsg);
|
|
ctx.messages.value.forEach((rawMsg) => {
|
|
const m = rawMsg as ChatMessage & { shipping_actions?: unknown };
|
|
if (m.role === 'assistant') {
|
|
m.shipping_actions = undefined;
|
|
}
|
|
});
|
|
await ctx.saveLocalMessageToBackend(userMsg, userText);
|
|
await ctx.saveLocalMessageToBackend(assistantMsg, userText);
|
|
void ctx.refreshShippingActions();
|
|
return true;
|
|
} catch (e: unknown) {
|
|
const errorMessage = e instanceof Error ? e.message : '标记手工上传失败';
|
|
ctx.msg.warning(errorMessage);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
return { executeShippingDecision, executeShippingManualUpload };
|
|
}
|