171 lines
5.6 KiB
JavaScript
171 lines
5.6 KiB
JavaScript
const { request, downloadFile, buildResultView, normalizeResponse } = require('../../utils/request');
|
|
|
|
const RESULT_PREVIEW_LIMIT = 240;
|
|
const TASK_STATUS_TEXT = {
|
|
0: '待开始',
|
|
1: '进行中',
|
|
2: '已完成',
|
|
3: '已暂停',
|
|
4: '已取消'
|
|
};
|
|
|
|
function safeParseResult(raw) {
|
|
const normalized = normalizeResponse(raw);
|
|
const payload = normalized.payload;
|
|
if (payload === null || payload === undefined) return null;
|
|
if (typeof payload === 'string') {
|
|
try {
|
|
return JSON.parse(payload);
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
return payload;
|
|
}
|
|
|
|
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 description = item.description || item.implement_path || item.implement_rules || item.initial_prompt || item.content || item.desc || item.detail || '';
|
|
const robot = item.robot || {};
|
|
const tags = Array.isArray(item.tags) ? item.tags.join('、') : '';
|
|
const statusText = TASK_STATUS_TEXT[item.status] || '';
|
|
return {
|
|
id: item.id || item.task_id || item.taskId || '',
|
|
title: title || (item.id || item.task_id ? `任务 ${item.id || item.task_id}` : '任务'),
|
|
status: item.status,
|
|
statusText,
|
|
priority: item.priority,
|
|
progress: item.progress,
|
|
robotId: item.robot_id || item.robotId,
|
|
robotName: robot.name || '',
|
|
tags,
|
|
contentPreview: description ? String(description).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
|
|
};
|
|
}
|
|
});
|