import { getAuthHeaders } from '../utils/auth'; export interface ShippingTaskSummary { id: number; task_no: string; source_type: string; customer_name?: string | null; process_status: string; review_status?: string | null; match_status?: string | null; pending_human_reason?: string | null; conversation_id?: string | null; export_file_name?: string | null; freight_ref_no?: string | null; created_at?: string | null; updated_at?: string | null; } export interface ShippingTaskEvent { event_type: string; detail_json?: Record | null; created_at?: string | null; } export interface ShippingTaskDetail extends ShippingTaskSummary { customer_code?: string | null; ship_to?: string | null; po_number?: string | null; required_date?: string | null; lines: Array>; shortage?: Record | null; line_results?: Array>; parse_confidence?: number | null; error_message?: string | null; events: ShippingTaskEvent[]; } async function getJson(url: string): Promise { const res = await fetch(url, { headers: getAuthHeaders() }); if (!res.ok) { const body = await res.json().catch(() => ({})); throw new Error((body as { error?: string }).error || res.statusText); } return res.json() as Promise; } export const listWorkspaceShippingTasks = (params?: { process_status?: string; limit?: number }) => { const sp = new URLSearchParams(); if (params?.process_status) sp.set('process_status', params.process_status); if (params?.limit) sp.set('limit', String(params.limit)); const qs = sp.toString(); return getJson(`/api/workspace/shipping-tasks${qs ? '?' + qs : ''}`); }; export const getWorkspaceShippingTask = (taskNo: string) => getJson(`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}`); export const workspaceShippingExportUrl = (taskNo: string) => `/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/export`; export interface ShippingChatAction { id: string; label: string; message: string; variant: 'primary' | 'secondary' | 'danger'; } export interface ShippingChatActionsResponse { task_no: string; checkpoint?: string | null; process_status: string; review_status?: string | null; pending_human_reason?: string | null; action_buttons: ShippingChatAction[]; } export const getShippingChatActions = (taskNo: string) => getJson( `/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/chat-actions`, ); export async function uploadWorkspaceShippingInventory(taskNo: string, file: File) { const form = new FormData(); form.append('file', file); const headers = getAuthHeaders(); delete (headers as Record)['Content-Type']; const res = await fetch( `/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/upload-inventory`, { method: 'POST', headers, body: form }, ); const body = (await res.json().catch(() => ({}))) as ShippingInventoryUploadResponse & { error?: string }; if (!res.ok) { throw new Error(body.error || body.message || res.statusText); } return body; } export async function applyWorkspaceShippingInventoryMapping(taskNo: string, columnMappingJson: Record) { const res = await fetch( `/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/apply-inventory-mapping`, { method: 'POST', headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' }, body: JSON.stringify({ map_json: columnMappingJson }), }, ); const body = (await res.json().catch(() => ({}))) as ShippingInventoryUploadResponse & { error?: string }; if (!res.ok) { throw new Error(body.error || body.message || res.statusText); } return body; } export interface ShippingInventoryUploadResponse { status: string; message?: string; task_id?: string; checkpoint?: string; data?: Record; } export interface ShippingTaskCreateResponse { status: string; message?: string; task_id?: string; checkpoint?: string; data?: Record; } export async function createWorkspaceShippingTask(params?: { source_type?: string; conversation_id?: string; raw_content?: string; }) { const res = await fetch('/api/workspace/shipping-tasks', { method: 'POST', headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' }, body: JSON.stringify(params || {}), }); const body = (await res.json().catch(() => ({}))) as ShippingTaskCreateResponse & { error?: string }; if (!res.ok || body.status === 'failed') { throw new Error(body.error || body.message || res.statusText); } return body; } export interface ShippingNoticeUploadResponse { status: string; message?: string; task_id?: string; checkpoint?: string; data?: Record; human_options?: string[]; } export async function createWorkspaceShippingTaskFromNotice(file: File) { const form = new FormData(); form.append('file', file); const headers = getAuthHeaders(); delete (headers as Record)['Content-Type']; const res = await fetch('/api/workspace/shipping-tasks/create-from-notice', { method: 'POST', headers, body: form, }); const body = (await res.json().catch(() => ({}))) as ShippingNoticeUploadResponse & { error?: string }; if (!res.ok || body.status === 'failed') { throw new Error(body.error || body.message || res.statusText); } return body; } export interface ShippingDecisionResponse { status: string; message?: string; task_id?: string; checkpoint?: string; data?: Record; } export async function submitWorkspaceShippingDecision(taskNo: string, decision: string, comment?: string) { const res = await fetch( `/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/submit-decision`, { method: 'POST', headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' }, body: JSON.stringify({ decision, ...(comment ? { comment } : {}) }), }, ); const body = (await res.json().catch(() => ({}))) as ShippingDecisionResponse & { error?: string }; if (!res.ok || body.status === 'failed') { throw new Error(body.error || body.message || res.statusText); } return body; } export async function matchWorkspaceShippingInventory(taskNo: string) { const res = await fetch( `/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/match-inventory`, { method: 'POST', headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' }, body: JSON.stringify({}), }, ); const body = (await res.json().catch(() => ({}))) as ShippingDecisionResponse & { error?: string }; if (!res.ok || body.status === 'failed') { throw new Error(body.error || body.message || res.statusText); } return body; } /** Hxops 聊天兜底:等同 Dify auto_progress Tool */ export async function autoProgressWorkspaceShippingTask(taskNo: string, lang?: string) { const res = await fetch( `/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/auto-progress`, { method: 'POST', headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' }, body: JSON.stringify(lang ? { lang } : {}), }, ); const body = (await res.json().catch(() => ({}))) as ShippingDecisionResponse & { error?: string }; if (!res.ok || body.status === 'failed') { throw new Error(body.error || body.message || res.statusText); } return body; } export async function markWorkspaceShippingManualUploaded(taskNo: string) { const res = await fetch( `/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/mark-manual-uploaded`, { method: 'POST', headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' }, body: JSON.stringify({}), }, ); const body = (await res.json().catch(() => ({}))) as ShippingDecisionResponse & { error?: string }; if (!res.ok || body.status === 'failed') { throw new Error(body.error || body.message || res.statusText); } return body; } export async function generateWorkspaceShippingExcel(taskNo: string) { const res = await fetch( `/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/generate-excel`, { method: 'POST', headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' }, body: JSON.stringify({}), }, ); const body = (await res.json().catch(() => ({}))) as ShippingDecisionResponse & { error?: string }; if (!res.ok || body.status === 'failed') { throw new Error(body.error || body.message || res.statusText); } return body; } export const SHIPPING_NOTICE_AGENT_NAME = '货代发货通知 Agent';