Files
syops/frontend/src/features/chat/shipping/shippingText.ts
T

72 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 出货聊天:消息文本净化与动作指令解析(纯函数,无 Vue 依赖)。
*
* - sanitizeShippingConversationText:隐藏后台状态 token,统一 task_id 展示
* - extractShippingActionsDirective:解析 <!-- HXOPS_SHIPPING_ACTIONS --> 注释块
* - normalizeShippingHeaderForDisplay:表头列名展示(空格转 NBSP)
*/
import { SHIPPING_INTERNAL_STATUS_TOKENS, SHIPPING_KNOWN_ACTION_ID_SET } from '@/constants/shippingNoticeActions';
import type { ShippingActionButton, ShippingActionVariant } from '../types/shipping.types';
export function sanitizeShippingConversationText(text: string, enabled: boolean): string {
if (!enabled) return text;
let result = String(text || '')
.replace(/\btask_id\s*[::]/gi, '任务编号:')
.replace(/\btask_id\b/gi, '任务编号');
result = result.replace(/[((]\s*显示\s*[「"'`][^"'`]+[」"'`]\s*[))]/g, '');
result = result.replace(
/\b(process_status|pending_human_reason|review_status|checkpoint|match_status|human_options)\s*[::=]\s*[「"'`]?[\w.-]+[「"'`]?/gi,
'',
);
for (const token of SHIPPING_INTERNAL_STATUS_TOKENS) {
const quoted = new RegExp(`[「"'“”‘’]\\s*${token}\\s*[」"'“”‘’]`, 'gi');
result = result.replace(quoted, '');
result = result.replace(new RegExp(`\`${token}\``, 'gi'), '');
}
return result.replace(/\n{3,}/g, '\n\n').trim();
}
export function extractShippingActionsDirective(rawText: string): {
cleanedText: string;
buttons: ShippingActionButton[];
} {
const text = String(rawText || '');
const re = /<!--\s*HXOPS_SHIPPING_ACTIONS\s*:\s*([\s\S]*?)\s*-->/gi;
const buttons: ShippingActionButton[] = [];
for (;;) {
const m = re.exec(text);
if (!m) break;
const body = String(m[1] || '').trim();
if (!body) continue;
try {
const parsed = JSON.parse(body);
if (Array.isArray(parsed)) {
for (const item of parsed) {
if (!item || typeof item !== 'object') continue;
const id = String((item as Record<string, unknown>).id || '').trim();
const label = String((item as Record<string, unknown>).label || '').trim();
const message = String((item as Record<string, unknown>).message || '').trim();
const variantRaw = String((item as Record<string, unknown>).variant || '').trim();
const variant: ShippingActionVariant | undefined = (variantRaw === 'primary' || variantRaw === 'secondary' || variantRaw === 'danger')
? (variantRaw as ShippingActionVariant)
: undefined;
if (!id || !label || !message) continue;
if (!SHIPPING_KNOWN_ACTION_ID_SET.has(id)) {
console.warn(`[shipping] unknown action id in HXOPS_SHIPPING_ACTIONS: ${id}`);
}
buttons.push({ id, label, message, variant });
}
}
} catch {
// ignore malformed directive
}
}
const cleaned = text.replace(re, '').trim();
return { cleanedText: cleaned, buttons };
}
export function normalizeShippingHeaderForDisplay(raw: unknown): string {
const text = String(raw ?? '').replace(/\s+/g, ' ').trim();
return text.replace(/ /g, '\u00A0');
}