1
0

ScheduleDetailPanel.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. <template>
  2. <div class="schedule-detail-panel">
  3. <div v-if="!hideHeader" class="schedule-detail-header">
  4. <span class="schedule-detail-title">定时任务</span>
  5. <div class="schedule-detail-close" @click="emit('close')">
  6. <div class="close-icon"></div>
  7. </div>
  8. </div>
  9. <div class="schedule-detail-body" v-if="task">
  10. <!-- 任务卡片 -->
  11. <div class="detail-task-card">
  12. <div class="detail-task-name">
  13. <div class="detail-task-icon"></div>
  14. <span class="detail-task-name-text">{{ task.name }}</span>
  15. <div class="detail-task-actions">
  16. <a-switch :checked="task.enabled" size="small" class="schedule-switch" @change="handleToggle" />
  17. <div class="more-btn-wrapper" ref="morePopupRef">
  18. <div class="detail-more-btn" @click="toggleMorePopup"></div>
  19. <div v-if="showMorePopup" class="more-popup" @click.stop>
  20. <div class="more-menu">
  21. <div class="more-menu-item" :class="{ disabled: !task.enabled }" @click="handleAction('run')">
  22. <div class="more-menu-icon run-icon"></div>
  23. <span>立即执行</span>
  24. </div>
  25. <div class="more-menu-item" @click="handleAction('edit')">
  26. <div class="more-menu-icon edit-icon"></div>
  27. <span>编辑</span>
  28. </div>
  29. <div class="more-menu-item delete" @click="handleAction('delete')">
  30. <div class="more-menu-icon delete-icon"></div>
  31. <span>删除</span>
  32. </div>
  33. </div>
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. <div class="detail-task-prompt">{{ task.prompt }}</div>
  39. <div class="detail-task-meta">
  40. <div class="detail-task-cron">
  41. <span class="info-label">执行计划:</span>
  42. <span class="info-value">{{ task.cron_human || task.cron_expr }}</span>
  43. </div>
  44. <div v-if="task.next_run_at" class="detail-task-next">
  45. <span class="info-label">下次执行:</span>
  46. <span class="info-value">{{ task.next_run_at }}</span>
  47. </div>
  48. </div>
  49. </div>
  50. <!-- 执行历史 -->
  51. <div class="detail-history-section">
  52. <div class="detail-history-title">
  53. <span>执行历史</span>
  54. <span v-if="historyStats" class="history-stats">
  55. <span class="stats-total">共{{ historyStats.total }}次</span>
  56. <span class="stats-sep">·</span>
  57. <span class="stats-success">{{ historyStats.success }}次成功</span>
  58. <span class="stats-sep">·</span>
  59. <span class="stats-failed">{{ historyStats.failed }}次失败</span>
  60. </span>
  61. </div>
  62. <div class="detail-history-list" v-if="!historyLoading">
  63. <template v-if="historyList.length">
  64. <div v-for="run in historyList" :key="run.id" class="history-item">
  65. <div class="history-item-header">
  66. <a-tag :color="runStatusColor(run.status)" class="history-status">{{ runStatusLabel(run.status) }}</a-tag>
  67. <span class="history-time">{{ run.started_at }}</span>
  68. <span v-if="run.duration_ms" class="history-duration">{{ (run.duration_ms / 1000).toFixed(0) }}s</span>
  69. </div>
  70. <div v-if="run.output" class="history-output">{{ run.output }}</div>
  71. <div v-if="run.error" class="history-error">{{ run.error }}</div>
  72. </div>
  73. </template>
  74. <div v-else class="history-empty">暂无执行记录</div>
  75. </div>
  76. <div v-else class="detail-history-loading">
  77. <a-spin size="small" />
  78. </div>
  79. </div>
  80. </div>
  81. <CreateScheduleModal :visible="showEditModal" :editData="editData" @close="showEditModal = false" @updated="refreshHistory" />
  82. </div>
  83. </template>
  84. <script setup lang="ts">
  85. import { ref, watch } from 'vue';
  86. import { onClickOutside } from '@vueuse/core';
  87. import { message, Modal } from 'ant-design-vue';
  88. import { getScheduleRuns, pauseSchedule, resumeSchedule, deleteSchedule } from '../../api';
  89. import CreateScheduleModal from './CreateScheduleModal.vue';
  90. import type { ScheduleTask } from './types';
  91. const props = defineProps<{ task: ScheduleTask | null; hideHeader?: boolean }>();
  92. const emit = defineEmits<{
  93. (e: 'close'): void;
  94. (e: 'deleted'): void;
  95. (e: 'updated'): void;
  96. (e: 'open-session', sessionId: string, prompt: string): void;
  97. }>();
  98. const morePopupRef = ref<HTMLElement>();
  99. const showMorePopup = ref(false);
  100. const showEditModal = ref(false);
  101. const editData = ref<ScheduleTask | null>(null);
  102. const toggleMorePopup = () => {
  103. showMorePopup.value = !showMorePopup.value;
  104. };
  105. onClickOutside(morePopupRef, () => {
  106. showMorePopup.value = false;
  107. });
  108. watch(showEditModal, (val) => {
  109. if (!val) editData.value = null;
  110. });
  111. const handleToggle = async () => {
  112. if (!props.task) return;
  113. try {
  114. if (props.task.enabled) {
  115. await pauseSchedule(props.task.id);
  116. message.success(`已暂停任务「${props.task.name}」`);
  117. } else {
  118. await resumeSchedule(props.task.id);
  119. message.success(`已恢复任务「${props.task.name}」`);
  120. }
  121. props.task.enabled = !props.task.enabled; // eslint-disable-line vue/no-mutating-props
  122. emit('updated');
  123. } catch {
  124. message.error('操作失败');
  125. }
  126. };
  127. const handleRunNow = async () => {
  128. if (!props.task || !props.task.enabled) return;
  129. if (props.task.feedback_session_id) {
  130. emit('open-session', props.task.feedback_session_id, props.task.prompt);
  131. } else {
  132. message.warning('该任务暂无关联会话');
  133. }
  134. };
  135. const handleEdit = () => {
  136. if (!props.task) return;
  137. editData.value = props.task;
  138. showEditModal.value = true;
  139. };
  140. const handleDelete = () => {
  141. if (!props.task) return;
  142. const taskName = props.task.name;
  143. const taskId = props.task.id;
  144. Modal.confirm({
  145. title: '提示',
  146. content: `是否删除定时任务「${taskName}」?`,
  147. okText: '确认',
  148. cancelText: '取消',
  149. wrapClassName: 'delete-task-confirm-modal',
  150. onOk: async () => {
  151. try {
  152. await deleteSchedule(taskId);
  153. message.success('删除成功');
  154. emit('deleted');
  155. } catch {
  156. message.error('删除失败');
  157. }
  158. },
  159. });
  160. };
  161. const handleAction = (action: 'run' | 'edit' | 'delete') => {
  162. showMorePopup.value = false;
  163. if (action === 'run') handleRunNow();
  164. else if (action === 'edit') handleEdit();
  165. else if (action === 'delete') handleDelete();
  166. };
  167. const refreshHistory = () => {
  168. if (props.task) fetchHistory(props.task.id);
  169. emit('updated');
  170. };
  171. const historyList = ref<any[]>([]);
  172. const historyLoading = ref(false);
  173. const historyStats = ref<{ total: number; success: number; failed: number } | null>(null);
  174. const fetchHistory = async (taskId: number) => {
  175. historyLoading.value = true;
  176. try {
  177. const res = await getScheduleRuns(taskId);
  178. const runs = res?.runs || (Array.isArray(res) ? res : []);
  179. historyList.value = runs;
  180. historyStats.value = {
  181. total: res?.total ?? runs.length,
  182. success: runs.filter((r: any) => r.status === 'success').length,
  183. failed: runs.filter((r: any) => r.status === 'failed').length,
  184. };
  185. } catch {
  186. historyList.value = [];
  187. historyStats.value = null;
  188. } finally {
  189. historyLoading.value = false;
  190. }
  191. };
  192. const runStatusLabel = (status: string) => {
  193. const map: Record<string, string> = { running: '执行中', success: '成功', failed: '失败', skipped: '跳过' };
  194. return map[status] || status;
  195. };
  196. const runStatusColor = (status: string) => {
  197. const map: Record<string, string> = { running: 'blue', success: 'green', failed: 'red', skipped: 'default' };
  198. return map[status] || 'default';
  199. };
  200. watch(
  201. () => props.task,
  202. (newTask) => {
  203. if (newTask) {
  204. fetchHistory(newTask.id);
  205. } else {
  206. historyList.value = [];
  207. historyStats.value = null;
  208. }
  209. },
  210. { immediate: true }
  211. );
  212. </script>
  213. <style scoped lang="less">
  214. .schedule-detail-panel {
  215. display: flex;
  216. flex-direction: column;
  217. height: 100%;
  218. overflow: hidden;
  219. // background-image: var(--img-chat-panel-bg);
  220. // background-repeat: no-repeat;
  221. // background-size: 100% 100%;
  222. border-radius: 4px;
  223. }
  224. .schedule-detail-header {
  225. display: flex;
  226. align-items: center;
  227. justify-content: space-between;
  228. padding: 12px 16px;
  229. border-bottom: 1px solid rgb(63 80 106 / 25%);
  230. flex-shrink: 0;
  231. .schedule-detail-title {
  232. font-size: 16px;
  233. font-weight: 600;
  234. color: #e8edf3;
  235. letter-spacing: 1px;
  236. }
  237. .schedule-detail-close {
  238. display: flex;
  239. align-items: center;
  240. justify-content: center;
  241. width: 28px;
  242. height: 28px;
  243. cursor: pointer;
  244. transition: background 0.2s;
  245. border-radius: 4px;
  246. &:hover {
  247. background: rgb(255 77 79 / 15%);
  248. }
  249. .close-icon {
  250. width: 16px;
  251. height: 16px;
  252. background-image: var(--img-chat-close-icon-btn);
  253. background-repeat: no-repeat;
  254. background-size: 100% 100%;
  255. }
  256. }
  257. }
  258. .schedule-detail-body {
  259. display: flex;
  260. flex: 1;
  261. flex-direction: column;
  262. padding: 16px;
  263. overflow: hidden;
  264. gap: 16px;
  265. }
  266. /* 任务卡片 */
  267. .detail-task-card {
  268. display: flex;
  269. flex-direction: column;
  270. padding: 12px;
  271. border: 1px solid rgb(63 80 106 / 15%);
  272. background: rgb(10 132 255 / 3%);
  273. border-radius: 8px;
  274. gap: 10px;
  275. }
  276. .detail-task-name {
  277. display: flex;
  278. align-items: center;
  279. gap: 10px;
  280. font-size: 15px;
  281. font-weight: 600;
  282. color: #e8edf3;
  283. .detail-task-name-text {
  284. flex: 1;
  285. min-width: 0;
  286. overflow: hidden;
  287. text-overflow: ellipsis;
  288. white-space: nowrap;
  289. }
  290. }
  291. .detail-task-actions {
  292. display: flex;
  293. align-items: center;
  294. gap: 8px;
  295. flex-shrink: 0;
  296. }
  297. .detail-more-btn {
  298. width: 24px;
  299. height: 24px;
  300. cursor: pointer;
  301. border-radius: 4px;
  302. background-image: var(--img-chat-schedule-more-icon);
  303. background-repeat: no-repeat;
  304. background-size: 14px 14px;
  305. background-position: center;
  306. transition: background-color 0.2s;
  307. &:hover {
  308. background-color: rgb(10 132 255 / 15%);
  309. }
  310. }
  311. .schedule-switch {
  312. :deep(.zxm-switch) {
  313. min-width: 36px;
  314. height: 20px;
  315. background: rgb(63 80 106 / 50%);
  316. &::after {
  317. top: 2px;
  318. left: 2px;
  319. width: 16px;
  320. height: 16px;
  321. background: #fff;
  322. transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  323. border-radius: 50%;
  324. box-shadow: 0 1px 3px rgb(0 0 0 / 30%);
  325. }
  326. &.zxm-switch-checked {
  327. background: rgb(10 132 255 / 60%);
  328. &::after {
  329. left: calc(100% - 18px);
  330. }
  331. }
  332. }
  333. }
  334. .detail-task-icon {
  335. width: 20px;
  336. height: 20px;
  337. background-image: var(--img-chat-options-schedule-icon);
  338. background-repeat: no-repeat;
  339. background-size: 100% 100%;
  340. background-position: center;
  341. opacity: 0.8;
  342. }
  343. .detail-task-meta {
  344. display: flex;
  345. flex-direction: column;
  346. gap: 6px;
  347. font-size: 13px;
  348. }
  349. .detail-task-cron,
  350. .detail-task-next {
  351. .info-label {
  352. color: #6a7a8a;
  353. }
  354. .info-value {
  355. color: #5cb8ff;
  356. }
  357. }
  358. .detail-task-prompt {
  359. font-size: 13px;
  360. color: #8b949e;
  361. line-height: 1.6;
  362. max-height: 120px;
  363. overflow-y: auto;
  364. word-break: break-all;
  365. &::-webkit-scrollbar {
  366. width: 4px;
  367. }
  368. &::-webkit-scrollbar-thumb {
  369. background: rgb(63 80 106 / 40%);
  370. border-radius: 2px;
  371. }
  372. }
  373. /* 执行历史 */
  374. .detail-history-section {
  375. display: flex;
  376. flex: 1;
  377. flex-direction: column;
  378. overflow: hidden;
  379. gap: 10px;
  380. }
  381. .detail-history-title {
  382. display: flex;
  383. align-items: center;
  384. justify-content: space-between;
  385. font-size: 14px;
  386. font-weight: 600;
  387. color: #e8edf3;
  388. .history-stats {
  389. display: inline-flex;
  390. align-items: center;
  391. gap: 0;
  392. font-size: 11px;
  393. font-weight: 400;
  394. line-height: 1.5;
  395. .stats-total {
  396. color: #8b949e;
  397. }
  398. .stats-sep {
  399. margin: 0 4px;
  400. color: #4a5568;
  401. }
  402. .stats-success {
  403. color: #52c41a;
  404. }
  405. .stats-failed {
  406. color: #ff7875;
  407. }
  408. }
  409. }
  410. .detail-history-list {
  411. display: flex;
  412. flex: 1;
  413. flex-direction: column;
  414. gap: 8px;
  415. overflow-y: auto;
  416. &::-webkit-scrollbar {
  417. width: 4px;
  418. }
  419. &::-webkit-scrollbar-thumb {
  420. background: rgb(63 80 106 / 40%);
  421. border-radius: 2px;
  422. }
  423. }
  424. .detail-history-loading {
  425. display: flex;
  426. justify-content: center;
  427. padding: 20px 0;
  428. }
  429. .history-item {
  430. padding: 10px 12px;
  431. border: 1px solid rgb(63 80 106 / 15%);
  432. background: rgb(10 132 255 / 4%);
  433. border-radius: 6px;
  434. }
  435. .history-item-header {
  436. display: flex;
  437. align-items: center;
  438. gap: 8px;
  439. margin-bottom: 6px;
  440. .history-status {
  441. font-size: 12px;
  442. }
  443. .history-time {
  444. font-size: 12px;
  445. color: #6a7a8a;
  446. }
  447. .history-duration {
  448. font-size: 12px;
  449. color: #8b949e;
  450. margin-left: auto;
  451. }
  452. }
  453. .history-output {
  454. display: -webkit-box;
  455. color: #c9d1d9;
  456. overflow: hidden;
  457. font-size: 12px;
  458. line-height: 1.6;
  459. max-height: 60px;
  460. text-overflow: ellipsis;
  461. -webkit-line-clamp: 3;
  462. -webkit-box-orient: vertical;
  463. }
  464. .history-error {
  465. font-size: 12px;
  466. color: #ff7875;
  467. line-height: 1.6;
  468. }
  469. .history-empty {
  470. padding: 8px 0;
  471. color: #6a7a8a;
  472. text-align: center;
  473. font-size: 12px;
  474. }
  475. </style>
  476. <style scoped lang="less">
  477. .more-btn-wrapper {
  478. position: relative;
  479. }
  480. .more-popup {
  481. position: absolute;
  482. top: 100%;
  483. right: 0;
  484. z-index: 100;
  485. padding: 4px;
  486. border: 1px solid rgb(63 80 106 / 40%);
  487. background: linear-gradient(135deg, #0a1628 0%, #0d2137 50%, #0a1e35 100%);
  488. margin-top: 4px;
  489. border-radius: 8px;
  490. min-width: 120px;
  491. }
  492. .more-menu {
  493. display: flex;
  494. flex-direction: column;
  495. gap: 2px;
  496. .more-menu-item {
  497. display: flex;
  498. align-items: center;
  499. padding: 8px 12px;
  500. color: #e0e6ed;
  501. cursor: pointer;
  502. transition: background 0.2s;
  503. gap: 8px;
  504. border-radius: 6px;
  505. font-size: 13px;
  506. &:hover {
  507. background: rgb(10 132 255 / 15%);
  508. }
  509. &.delete {
  510. color: #ff7875;
  511. &:hover {
  512. background: rgb(255 77 79 / 10%);
  513. }
  514. }
  515. &.disabled {
  516. cursor: not-allowed;
  517. opacity: 0.4;
  518. pointer-events: none;
  519. }
  520. .more-menu-icon {
  521. width: 16px;
  522. height: 16px;
  523. flex-shrink: 0;
  524. background-repeat: no-repeat;
  525. background-size: 14px 14px;
  526. background-position: center;
  527. &.run-icon {
  528. background-image: var(--img-chat-schedule-run-icon);
  529. }
  530. &.edit-icon {
  531. background-image: var(--img-chat-edit-icon);
  532. }
  533. &.delete-icon {
  534. background-image: var(--img-chat-delete-icon);
  535. }
  536. }
  537. }
  538. }
  539. </style>