feat(schedule):cron支持图形,RoHS检核增加进度显示

This commit is contained in:
hugo committed 2026-06-15 13:42:15 +08:00
1 parent 6d0e78be81
commit f5aac9c893
46 files changed
+1987 -221

No files matched your search

@@ -83,6 +83,11 @@ const applyEventMetadata = (msg: ChatMessage, data: Record<string, unknown>) =>
addAttributionParts(msg, metadata?.annotation_reply);
addAttributionParts(msg, data?.citation);
addAttributionParts(msg, metadata?.citation);
const messagePayload = data?.message_payload;
if (messagePayload && typeof messagePayload === 'object') {
msg.payload = messagePayload;
}
};
export function useChat() {
@@ -124,6 +129,27 @@ export function useChat() {
return text.replace(/\\n/g, '\n');
};
const formatStreamError = (message: unknown) => {
if (typeof message === 'string') return message;
if (message && typeof message === 'object') {
const record = message as Record<string, unknown>;
const nested = record.error;
if (typeof nested === 'string') return nested;
if (nested && typeof nested === 'object') {
const nestedRecord = nested as Record<string, unknown>;
if (typeof nestedRecord.message === 'string') return nestedRecord.message;
}
if (typeof record.message === 'string') return record.message;
if (typeof record.code === 'string') return record.code;
try {
return JSON.stringify(message);
} catch {
return 'Unknown error';
}
}
return String(message ?? 'Unknown error');
};
const sendMessage = async (
query: string,
agentId: number,
@@ -269,6 +295,13 @@ export function useChat() {
const currentBotMsg = runMessages[botMsgIndex];
if (currentBotMsg) currentBotMsg.chat_log_id = Number(data.chat_log_id);
}
if (botMsgIndex !== null) {
const runId = Number(data.run_id);
if (Number.isFinite(runId) && runId > 0) {
const currentBotMsg = runMessages[botMsgIndex];
if (currentBotMsg) currentBotMsg.run_id = runId;
}
}
if (typeof data.answer === 'string' && data.answer.length > 0) {
const botMsg = ensureBotMessage();
if (!botMsg) continue;
@@ -286,7 +319,7 @@ export function useChat() {
} else if (eventName === 'error') {
const botMsg = ensureBotMessage();
if (!botMsg) continue;
botMsg.content += `\n[Error: ${data.message}]`;
botMsg.content += `\n[Error: ${formatStreamError(data.message)}]`;
run.status = 'error';
if (messages.value === runMessages) isThinking.value = false;
} else if (typeof data.answer === 'string' && data.answer.length > 0) {
@@ -287,6 +287,7 @@ export function useChatPage() {
tryHandleLeaveSend: leave.tryHandleLeaveSend,
tryHandleShippingSend: shipping.tryHandleShippingSend,
refreshToolRunProgressAfterSend: toolRun.refreshToolRunProgressAfterSend,
prepareToolRunProgressBeforeSend: toolRun.prepareToolRunProgressBeforeSend,
msg,
t,
copiedIndex,
@@ -1,15 +1,33 @@
import { computed, onBeforeUnmount, ref, type Ref, watch } from 'vue';
import { getToolRunProgress, type ToolRunProgress } from '@/api/toolRunProgress';
import { getToolRunProgress, type ToolRunProgress, type ToolRunProgressMatchSource } from '@/api/toolRunProgress';
import { buildLogProgressUrl } from '../utils/toolRunProgressConfig';
export interface ToolRunProgressConfig {
type?: string;
title?: string;
poll_interval_seconds?: number;
active_url?: string;
/** 按 chat_log_id 关联批次的轮询地址 */
log_progress_url?: string;
/** @deprecated 使用 log_progress_url */
session_progress_url?: string;
detail_url_template?: string;
schedule_task_id?: number | string | null;
task_id?: number | string | null;
chat_log_id?: number | string | null;
/** 已知批次 ID 时走 detail 轮询 */
run_id?: number | string | null;
/** for-session 空结果最大重试次数 */
empty_poll_max?: number;
/** 等待 run_id 写出前的轮询窗口(秒) */
pending_seconds?: number;
lock_input_when_running?: boolean;
lock_message?: string;
session_id?: string;
}
export interface UseToolRunProgressOptions {
allowEmptySessionPolls?: Ref<boolean>;
}
const clampPollMs = (seconds: unknown) => {
@@ -18,13 +36,35 @@ const clampPollMs = (seconds: unknown) => {
return Math.max(1, Math.min(safe, 30)) * 1000;
};
export function useToolRunProgress(configRef: Ref<ToolRunProgressConfig | null>) {
const TERMINAL_STATUSES = new Set(['success', 'partial_failed', 'failed', 'cancelled', 'canceled']);
const isTerminalStatus = (status?: string | null) => TERMINAL_STATUSES.has(String(status || '').toLowerCase());
/**
* 轮询任务进度:有 run_id 时走 detail;或走 for-log / active。
*/
export function useToolRunProgress(
configRef: Ref<ToolRunProgressConfig | null>,
options: UseToolRunProgressOptions = {},
) {
const run = ref<ToolRunProgress | null>(null);
const matchSource = ref<ToolRunProgressMatchSource | null>(null);
const loading = ref(false);
const error = ref('');
let timer: number | undefined;
let emptySessionPolls = 0;
let pollSessionKey = '';
let hasFetchedOnce = false;
let resolvedRunId: string | null = null;
const enabled = computed(() => {
const cfg = configRef.value;
if (!cfg) return false;
if (cfg.session_progress_url) return true;
if (cfg.run_id && cfg.detail_url_template) return true;
return !!cfg.active_url;
});
const enabled = computed(() => !!configRef.value?.active_url || (!!configRef.value?.detail_url_template && !!configRef.value?.run_id));
const isRunning = computed(() => run.value?.status === 'running');
const shouldLockInput = computed(() => Boolean(configRef.value?.lock_input_when_running) && isRunning.value);
const lockMessage = computed(() => configRef.value?.lock_message || '当前任务执行中,请等待完成');
@@ -38,53 +78,184 @@ export function useToolRunProgress(configRef: Ref<ToolRunProgressConfig | null>)
const scheduleNext = () => {
clearTimer();
if (!enabled.value) return;
if (!enabled.value || error.value) return;
const interval = clampPollMs(configRef.value?.poll_interval_seconds);
timer = window.setTimeout(() => {
void refresh();
}, interval);
};
const resolveProgressUrl = (cfg: ToolRunProgressConfig) => {
const runId = String(cfg.run_id || '').trim();
if (runId && cfg.detail_url_template) {
return cfg.detail_url_template.replace('{run_id}', encodeURIComponent(runId));
const resolveDetailUrl = (cfg: ToolRunProgressConfig, runId: number | string) => {
const template = String(cfg.detail_url_template || '').trim();
if (!template) return '';
return template.replace('{run_id}', encodeURIComponent(String(runId)));
};
const fetchProgressUrl = async (url: string, silent: boolean) => {
if (!silent) loading.value = true;
try {
const payload = await getToolRunProgress(url);
error.value = '';
matchSource.value = payload.match_source ?? null;
return payload.run ?? null;
} catch (e: unknown) {
const message = e instanceof Error ? e.message : '任务进度查询失败';
error.value = message;
matchSource.value = null;
clearTimer();
return null;
} finally {
if (!silent) loading.value = false;
hasFetchedOnce = true;
}
return cfg.active_url || '';
};
const refreshByDetail = async (cfg: ToolRunProgressConfig, runId: string, silent: boolean) => {
if (!runId || !cfg.detail_url_template) return null;
const detailUrl = resolveDetailUrl(cfg, runId);
if (!detailUrl) return null;
return fetchProgressUrl(detailUrl, silent);
};
const refreshBySession = async (cfg: ToolRunProgressConfig, silent: boolean) => {
const sessionUrl = String(cfg.session_progress_url || '').trim();
if (sessionUrl) {
return fetchProgressUrl(sessionUrl, silent);
}
const taskId = Number(cfg.schedule_task_id ?? cfg.task_id);
const sessionId = String(cfg.session_id || '').trim();
if (!Number.isFinite(taskId) || taskId <= 0 || !sessionId) return null;
const fallbackUrl = buildLogProgressUrl('/api/workspace/rohs-check/runs/for-log', taskId, Number(cfg.chat_log_id));
return fetchProgressUrl(fallbackUrl, silent);
};
const refresh = async () => {
const cfg = configRef.value;
const url = cfg ? resolveProgressUrl(cfg) : '';
if (!url) {
if (!cfg) {
run.value = null;
matchSource.value = null;
resolvedRunId = null;
error.value = '';
clearTimer();
return;
}
loading.value = true;
try {
const payload = await getToolRunProgress(url);
run.value = payload.run;
error.value = '';
} catch (e: any) {
error.value = e?.message || '任务进度查询失败';
} finally {
loading.value = false;
if (error.value) {
clearTimer();
return;
}
const allowEmpty = options.allowEmptySessionPolls?.value ?? false;
const silent = hasFetchedOnce || !!run.value || allowEmpty;
const explicitRunId = String(cfg.run_id || resolvedRunId || '').trim();
const hasSessionUrl = !!String(cfg.session_progress_url || cfg.session_id || '').trim();
if (hasSessionUrl) {
const sessionRun = await refreshBySession(cfg, silent);
if (sessionRun) {
run.value = sessionRun;
resolvedRunId = String(sessionRun.run_id);
} else if (!allowEmpty || error.value) {
run.value = null;
if (!allowEmpty) {
matchSource.value = null;
}
}
if (run.value?.status === 'running') {
emptySessionPolls = 0;
scheduleNext();
return;
}
if (run.value && isTerminalStatus(run.value.status)) {
emptySessionPolls = 0;
clearTimer();
return;
}
const emptyPollMax = Number(cfg.empty_poll_max) > 0 ? Number(cfg.empty_poll_max) : 30;
if (!run.value && allowEmpty && !error.value) {
emptySessionPolls += 1;
if (emptySessionPolls < emptyPollMax) {
scheduleNext();
return;
}
}
emptySessionPolls = 0;
clearTimer();
return;
}
if (explicitRunId && cfg.detail_url_template) {
const detailRun = await refreshByDetail(cfg, explicitRunId, silent);
if (detailRun) {
run.value = detailRun;
}
if (run.value?.status === 'running') {
emptySessionPolls = 0;
scheduleNext();
return;
}
if (run.value && isTerminalStatus(run.value.status)) {
emptySessionPolls = 0;
clearTimer();
return;
}
emptySessionPolls = 0;
clearTimer();
return;
}
const activeUrl = String(cfg.active_url || '').trim();
if (!activeUrl) {
run.value = null;
matchSource.value = null;
clearTimer();
return;
}
const activeRun = await fetchProgressUrl(activeUrl, silent);
if (activeRun) {
run.value = activeRun;
} else if (!allowEmpty || error.value) {
run.value = null;
matchSource.value = null;
}
if (run.value?.status === 'running') {
emptySessionPolls = 0;
scheduleNext();
} else {
clearTimer();
return;
}
if (run.value && isTerminalStatus(run.value.status)) {
emptySessionPolls = 0;
clearTimer();
return;
}
emptySessionPolls = 0;
clearTimer();
};
watch(
() => JSON.stringify(configRef.value || {}),
() => {
const cfg = configRef.value;
return cfg
? `${cfg.session_progress_url || ''}|${cfg.active_url || ''}|${cfg.schedule_task_id ?? cfg.task_id ?? ''}|${cfg.session_id || ''}|${cfg.run_id || ''}`
: '';
},
(nextKey) => {
if (nextKey === pollSessionKey) return;
pollSessionKey = nextKey;
run.value = null;
matchSource.value = null;
resolvedRunId = null;
error.value = '';
emptySessionPolls = 0;
hasFetchedOnce = false;
clearTimer();
if (enabled.value) void refresh();
},
@@ -95,6 +266,7 @@ export function useToolRunProgress(configRef: Ref<ToolRunProgressConfig | null>)
return {
run,
matchSource,
loading,
error,
isRunning,