202 lines
6.5 KiB
TypeScript
202 lines
6.5 KiB
TypeScript
/**
|
||
* features/chat/shipping/shippingTurnActions.ts
|
||
* 用途:将发货通知菜单/决策 action.id 映射为 Chat Turn action_type + payload。
|
||
* 依赖:constants/shipping、vue-i18n(displayText)。
|
||
* 逻辑:useChatShippingDecision 与 useChatShippingMenuHandler 调用本模块,不再直调 workspace API 插消息。
|
||
*/
|
||
import i18n from '@/i18n';
|
||
import { SHIPPING_DECISION_ACTIONS } from '../constants/shipping';
|
||
|
||
export interface ShippingChatTurnSpec {
|
||
actionType: string;
|
||
payload: Record<string, unknown>;
|
||
}
|
||
|
||
const tt = (key: string, params?: Record<string, unknown>) =>
|
||
String(params ? i18n.global.t(key, params) : i18n.global.t(key));
|
||
|
||
/**
|
||
* 将发货 UI 动作 id 解析为平台 Chat Turn 规格。
|
||
* @param actionId - 状态机 action_buttons.id
|
||
* @param taskNo - 任务编号
|
||
* @param extra - 额外参数,如 confirm_inventory_source 的 source_code
|
||
* @returns Chat Turn 规格;无法映射时返回 null
|
||
*/
|
||
export function resolveShippingChatTurn(
|
||
actionId: string,
|
||
taskNo: string,
|
||
extra?: { sourceCode?: string },
|
||
): ShippingChatTurnSpec | null {
|
||
const normalizedTaskNo = String(taskNo || '').trim().toUpperCase();
|
||
if (!normalizedTaskNo) return null;
|
||
const id = String(actionId || '').trim();
|
||
if (!id) return null;
|
||
|
||
if (SHIPPING_DECISION_ACTIONS.has(id)) {
|
||
return {
|
||
actionType: 'shipping.submit_decision',
|
||
payload: { task_no: normalizedTaskNo, decision: id },
|
||
};
|
||
}
|
||
|
||
switch (id) {
|
||
case 'generate_excel':
|
||
return { actionType: 'shipping.generate_excel', payload: { task_no: normalizedTaskNo } };
|
||
case 'retry_upload':
|
||
return { actionType: 'shipping.retry_upload', payload: { task_no: normalizedTaskNo } };
|
||
case 'match_inventory':
|
||
return { actionType: 'shipping.match_inventory', payload: { task_no: normalizedTaskNo } };
|
||
case 'mark_manual_uploaded':
|
||
return { actionType: 'shipping.mark_manual_uploaded', payload: { task_no: normalizedTaskNo } };
|
||
case 'confirm_inventory_source': {
|
||
const sourceCode = String(extra?.sourceCode || '').trim().toUpperCase();
|
||
if (!sourceCode) return null;
|
||
return {
|
||
actionType: 'shipping.confirm_inventory_source',
|
||
payload: { task_no: normalizedTaskNo, source_code: sourceCode },
|
||
};
|
||
}
|
||
default:
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 构造 submit_decision Chat Turn(文本/菜单决策共用)。
|
||
*/
|
||
export function buildShippingSubmitDecisionTurn(
|
||
taskNo: string,
|
||
decision: string,
|
||
): ShippingChatTurnSpec {
|
||
return {
|
||
actionType: 'shipping.submit_decision',
|
||
payload: {
|
||
task_no: String(taskNo || '').trim().toUpperCase(),
|
||
decision: String(decision || '').trim(),
|
||
},
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 构造发货通知建批 Chat Turn(multipart 上传)。
|
||
*/
|
||
export function buildShippingCreateBatchTurn(
|
||
files: File[],
|
||
conversationId?: string | null,
|
||
idempotencyKey?: string,
|
||
): { actionType: string; displayText: string; payload: Record<string, unknown>; files: File[] } {
|
||
const count = files.length;
|
||
const displayText = count === 1
|
||
? tt('chat.shippingNoticeTurn.createBatchSingle', {
|
||
fileName: files[0]?.name || tt('chat.shippingNoticeTurn.attachmentFallback'),
|
||
})
|
||
: tt('chat.shippingNoticeTurn.createBatchMulti', { count });
|
||
return {
|
||
actionType: 'shipping.create_batch_from_notices',
|
||
displayText,
|
||
payload: {
|
||
conversation_id: conversationId || undefined,
|
||
file_count: count,
|
||
idempotency_key: idempotencyKey,
|
||
},
|
||
files,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 构造发货通知建批 Chat Turn(基于已解析 intake,不再携带 files)。
|
||
*/
|
||
export function buildShippingCreateBatchFromIntakesTurn(
|
||
intakeNos: string[],
|
||
conversationId?: string | null,
|
||
idempotencyKey?: string,
|
||
): { actionType: string; displayText: string; payload: Record<string, unknown> } {
|
||
const count = intakeNos.length;
|
||
const displayText = count === 1
|
||
? tt('chat.shippingNoticeTurn.confirmBatchSingle', { intakeNo: intakeNos[0] })
|
||
: tt('chat.shippingNoticeTurn.confirmBatchMulti', { count });
|
||
return {
|
||
actionType: 'shipping.create_batch_from_intakes',
|
||
displayText,
|
||
payload: {
|
||
intake_nos: intakeNos,
|
||
conversation_id: conversationId || undefined,
|
||
idempotency_key: idempotencyKey,
|
||
},
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 构造货代领域 refs,键名与 agent_runtime_context.refs 对齐。
|
||
*/
|
||
export function buildShippingTurnRefs(taskNo: string, batchNo?: string): Record<string, string> {
|
||
const refs: Record<string, string> = {};
|
||
const task = String(taskNo || '').trim().toUpperCase();
|
||
const batch = String(batchNo || '').trim();
|
||
if (task) refs.task_no = task;
|
||
if (batch) refs.batch_no = batch;
|
||
return refs;
|
||
}
|
||
|
||
/**
|
||
* 构造库存上传 Chat Turn。
|
||
*/
|
||
export function buildShippingUploadInventoryTurn(taskNo: string, file: File) {
|
||
const task = String(taskNo || '').trim().toUpperCase();
|
||
return {
|
||
actionType: 'shipping.upload_inventory',
|
||
displayText: tt('chat.shippingNoticeTurn.uploadInventory', { fileName: file.name }),
|
||
payload: { task_no: task },
|
||
files: [file],
|
||
refs: buildShippingTurnRefs(task),
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 构造重新上传发货通知 Chat Turn。
|
||
*/
|
||
export function buildShippingReplaceNoticeTurn(taskNo: string, file: File) {
|
||
const task = String(taskNo || '').trim().toUpperCase();
|
||
return {
|
||
actionType: 'shipping.replace_notice',
|
||
displayText: tt('chat.shippingNoticeTurn.replaceNotice', { fileName: file.name }),
|
||
payload: { task_no: task },
|
||
files: [file],
|
||
refs: buildShippingTurnRefs(task),
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 构造应用库存列映射 Chat Turn。
|
||
*/
|
||
export function buildShippingApplyMappingTurn(taskNo: string, mapping: Record<string, string>) {
|
||
const task = String(taskNo || '').trim().toUpperCase();
|
||
return {
|
||
actionType: 'shipping.apply_inventory_mapping',
|
||
displayText: tt('chat.shippingNoticeTurn.applyMapping', { taskId: task }),
|
||
payload: { task_no: task, map_json: mapping },
|
||
refs: buildShippingTurnRefs(task),
|
||
};
|
||
}
|
||
|
||
export function buildShippingSwitchFocusTurn(batchNo: string, taskNo: string, displayText: string) {
|
||
const batch = String(batchNo || '').trim();
|
||
const task = String(taskNo || '').trim().toUpperCase();
|
||
return {
|
||
actionType: 'shipping.switch_batch_focus',
|
||
displayText,
|
||
payload: { batch_no: batch, task_no: task },
|
||
refs: buildShippingTurnRefs(task, batch),
|
||
};
|
||
}
|
||
|
||
export function buildShippingQueryShortageTurn(taskNo: string, displayText: string) {
|
||
const task = String(taskNo || '').trim().toUpperCase();
|
||
return {
|
||
actionType: 'shipping.query_shortage_details',
|
||
displayText,
|
||
payload: { task_no: task },
|
||
refs: buildShippingTurnRefs(task),
|
||
};
|
||
}
|