const { request, downloadFile, buildResultView } = require('../../utils/request'); const RESULT_PREVIEW_LIMIT = 240; function safeParseResult(raw) { if (raw === null || raw === undefined) return null; if (typeof raw === 'string') { try { return JSON.parse(raw); } catch (e) { return null; } } return raw; } function pickList(obj) { if (!obj || typeof obj !== 'object') return null; const keys = ['list', 'items', 'records', 'rows', 'data', 'result']; for (let i = 0; i < keys.length; i += 1) { const value = obj[keys[i]]; if (Array.isArray(value)) return value; } return null; } function toArray(parsed) { if (!parsed || typeof parsed !== 'object') return []; if (Array.isArray(parsed)) return parsed; const list = pickList(parsed); if (list) return list; return [parsed]; } function isTaskLike(item) { if (!item || typeof item !== 'object') return false; return item.id !== undefined || item.task_id !== undefined || item.title || item.content || item.robot_id !== undefined; } function buildTaskCards(parsed) { const list = toArray(parsed).filter(isTaskLike).slice(0, 6); return list.map(item => { const title = item.title || item.name || item.subject || item.task_name || ''; const content = item.content || item.description || item.desc || item.detail || ''; return { id: item.id || item.task_id || item.taskId || '', title: title || (item.id || item.task_id ? `任务 ${item.id || item.task_id}` : '任务'), status: item.status, priority: item.priority, robotId: item.robot_id || item.robotId, contentPreview: content ? String(content).slice(0, 80) : '' }; }); } function buildResultState(text) { const content = text || ''; return { resultHasMore: content.length > RESULT_PREVIEW_LIMIT, resultExpanded: false }; } Page({ data: { id: '', title: '', robot_id: '', content: '', status: '', priority: '', keyword: '', page: 1, page_size: 10, convId: '', result: '', resultCards: [], resultExpanded: false, resultHasMore: false, taskCards: [] }, onInput(e) { const key = e.currentTarget.dataset.key; this.setData({ [key]: e.detail.value }); }, toggleResult() { this.setData({ resultExpanded: !this.data.resultExpanded }); }, setResult(result) { const view = buildResultView(result); const parsed = safeParseResult(result); const taskCards = buildTaskCards(parsed); const state = buildResultState(view.text); this.setData({ result: view.text, resultCards: view.cards, taskCards, ...state }); }, async list() { const qs = []; qs.push(`page=${encodeURIComponent(this.data.page)}`); qs.push(`page_size=${encodeURIComponent(this.data.page_size)}`); if (this.data.robot_id) qs.push(`robot_id=${encodeURIComponent(this.data.robot_id)}`); if (this.data.status) qs.push(`status=${encodeURIComponent(this.data.status)}`); if (this.data.keyword) qs.push(`keyword=${encodeURIComponent(this.data.keyword)}`); const result = await request({ url: `/api/prompt/tasks?${qs.join('&')}` }); this.setResult(result); }, async detail() { const result = await request({ url: `/api/prompt/tasks/${encodeURIComponent(this.data.id)}?with_conversations=true` }); this.setResult(result); }, async create() { const payload = this.buildPayload(); const result = await request({ url: '/api/prompt/tasks', method: 'POST', data: payload }); this.setResult(result); }, async update() { const payload = this.buildPayload(); const result = await request({ url: `/api/prompt/tasks/${encodeURIComponent(this.data.id)}`, method: 'PUT', data: payload }); this.setResult(result); }, async remove() { const result = await request({ url: `/api/prompt/tasks/${encodeURIComponent(this.data.id)}`, method: 'DELETE' }); this.setResult(result); }, async updateStatus() { const result = await request({ url: `/api/prompt/tasks/${encodeURIComponent(this.data.id)}/status`, method: 'PUT', data: { status: Number(this.data.status) } }); this.setResult(result); }, async clearConversations() { const result = await request({ url: `/api/prompt/tasks/${encodeURIComponent(this.data.id)}/conversations`, method: 'DELETE' }); this.setResult(result); }, async deleteConversation() { const result = await request({ url: `/api/prompt/conversations/${encodeURIComponent(this.data.convId)}`, method: 'DELETE' }); this.setResult(result); }, async download() { try { const tempPath = await downloadFile({ url: `/api/prompt/tasks/${encodeURIComponent(this.data.id)}/download` }); this.setResult(`下载成功: ${tempPath}`); } catch (e) { this.setResult('下载失败'); } }, buildPayload() { return { title: this.data.title, robot_id: this.data.robot_id ? Number(this.data.robot_id) : undefined, content: this.data.content, status: this.data.status ? Number(this.data.status) : undefined, priority: this.data.priority ? Number(this.data.priority) : undefined }; } });