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

@@ -39,6 +39,7 @@ export interface ChatPageSendContext {
trimmed: string,
) => Promise<boolean>;
refreshToolRunProgressAfterSend: () => Promise<void>;
prepareToolRunProgressBeforeSend: () => Promise<void>;
msg: { warning: (text: string) => void; info: (text: string) => void };
t: (key: string) => string;
copiedIndex: Ref<number | null>;
@@ -88,6 +89,7 @@ export function createChatPageSendApi(ctx: ChatPageSendContext) {
if (typeof textOrEvent !== 'string') ctx.input.value = '';
ctx.clearAttachments();
await ctx.prepareToolRunProgressBeforeSend();
await ctx.sendMessage(content, ctx.currentAgentId.value, ctx.selectedKb.value, files);
await ctx.refreshHistoryAfterSend();
await ctx.refreshToolRunProgressAfterSend();
@@ -1,32 +1,39 @@
/**
* 对话页:Agent tool_run_progress 能力轮询(RoHS 等按消息 run_id 展示进度卡片)。
* 对话页:Agent tool_run_progress 能力轮询(RoHS 等异步任务进度卡片)。
* 依赖:useToolRunProgress、toolRunProgressConfig、ChatMessage。
* 逻辑:按助手消息 ChatLog.run_id 展示进度,每轮问答独立绑定。
*/
import { computed, nextTick } from 'vue';
import { useToolRunProgress, type ToolRunProgressConfig } from '../composables/useToolRunProgress';
import { computed, nextTick, ref, watch } from 'vue';
import type { ToolRunProgress } from '@/api/toolRunProgress';
import { getToolRunProgress } from '@/api/toolRunProgress';
import type { ToolRunProgressConfig } from '../composables/useToolRunProgress';
import type { ChatMessage } from '../types/chat.types';
import type { Ref } from 'vue';
import {
assistantMessageHasError,
collectAssistantRunIds,
getRunIdFromMessage,
isRunningToolRunStatus,
resolveScheduleTaskId,
resolveMessageAnchorKey,
} from '../utils/toolRunProgressConfig';
export interface ChatPageToolRunContext {
currentAgent: Ref<{ extra_config?: Record<string, unknown> } | null>;
currentAgent: Ref<{ id?: number; extra_config?: Record<string, unknown> } | null>;
conversationId: Ref<string | null>;
messages: Ref<ChatMessage[]>;
}
const extractToolRunIdFromContent = (content: unknown): number | null => {
const text = String(content || '');
const match = text.match(/(?:检核批次 ID|当前检核批次 ID|运行 ID|run_id|run id)\s*[::]\s*(\d+)/i);
return match?.[1] ? Number(match[1]) : null;
};
export function useChatPageToolRun(ctx: ChatPageToolRunContext) {
const activeToolRunId = computed(() => {
for (let i = ctx.messages.value.length - 1; i >= 0; i -= 1) {
const msg = ctx.messages.value[i];
if (msg?.role !== 'assistant') continue;
const runId = extractToolRunIdFromContent(msg.content);
if (runId) return runId;
}
return null;
const runsById = ref<Record<number, ToolRunProgress>>({});
const runErrors = ref<Record<number, string>>({});
const pollingRunIds = ref<number[]>([]);
let pollTimer: number | undefined;
const rawToolRunConfig = computed(() => {
const extra = ctx.currentAgent.value?.extra_config || {};
const raw = extra.tool_run_progress;
return raw && typeof raw === 'object' ? (raw as ToolRunProgressConfig) : null;
});
const toolRunProgressConfig = computed<ToolRunProgressConfig | null>(() => {
@@ -35,55 +42,190 @@ export function useChatPageToolRun(ctx: ChatPageToolRunContext) {
? extra.capabilities.map((item: unknown) => String(item))
: [];
if (!capabilities.includes('tool_run_progress')) return null;
const raw = extra.tool_run_progress;
if (!raw || typeof raw !== 'object') return null;
const cfg = raw as ToolRunProgressConfig;
const cid = String(ctx.conversationId.value || '').trim();
const runId = activeToolRunId.value;
if (runId && cfg.detail_url_template) {
return {
...cfg,
run_id: runId,
};
}
if (!cid || !cfg.active_url) return null;
const separator = cfg.active_url.includes('?') ? '&' : '?';
const cfg = rawToolRunConfig.value;
if (!cfg) return null;
return {
...cfg,
active_url: `${cfg.active_url}${separator}conversation_id=${encodeURIComponent(cid)}`,
run_id: null,
schedule_task_id: resolveScheduleTaskId(cfg),
};
});
const {
run: toolRunProgress,
loading: toolRunProgressLoading,
error: toolRunProgressError,
refresh: refreshToolRunProgress,
} = useToolRunProgress(toolRunProgressConfig);
const boundScheduleTaskId = computed(() => resolveScheduleTaskId(rawToolRunConfig.value));
const clearPollTimer = () => {
if (pollTimer !== undefined) {
window.clearTimeout(pollTimer);
pollTimer = undefined;
}
};
const resolveDetailUrl = (runId: number) => {
const template = String(rawToolRunConfig.value?.detail_url_template || '').trim();
if (!template) return '';
return template.replace('{run_id}', encodeURIComponent(String(runId)));
};
const refreshRun = async (runId: number) => {
const url = resolveDetailUrl(runId);
if (!url) return;
try {
const payload = await getToolRunProgress(url);
if (payload.run) {
runsById.value = { ...runsById.value, [runId]: payload.run };
}
const nextErrors = { ...runErrors.value };
delete nextErrors[runId];
runErrors.value = nextErrors;
} catch (e: unknown) {
runErrors.value = {
...runErrors.value,
[runId]: e instanceof Error ? e.message : '任务进度查询失败',
};
}
};
const schedulePoll = () => {
clearPollTimer();
const intervalSec = Number(rawToolRunConfig.value?.poll_interval_seconds);
const intervalMs = Math.max(1, Math.min(Number.isFinite(intervalSec) ? intervalSec : 2, 30)) * 1000;
pollTimer = window.setTimeout(() => {
void syncProgressState();
}, intervalMs);
};
const syncProgressState = async () => {
clearPollTimer();
if (!toolRunProgressConfig.value) {
runsById.value = {};
runErrors.value = {};
pollingRunIds.value = [];
return;
}
const runIds = collectAssistantRunIds(ctx.messages.value);
pollingRunIds.value = runIds;
if (!runIds.length) {
runsById.value = {};
runErrors.value = {};
return;
}
await Promise.all(runIds.map((runId) => refreshRun(runId)));
const nextRuns: Record<number, ToolRunProgress> = {};
for (const runId of runIds) {
const run = runsById.value[runId];
if (run) nextRuns[runId] = run;
}
runsById.value = nextRuns;
const hasRunning = Object.values(nextRuns).some((run) => isRunningToolRunStatus(run.status));
if (hasRunning) schedulePoll();
};
watch(
() => [ctx.currentAgent.value?.id, ctx.conversationId.value] as const,
() => {
runsById.value = {};
runErrors.value = {};
pollingRunIds.value = [];
clearPollTimer();
},
);
watch(
() => ctx.messages.value.map((msg, index) => (
msg?.role === 'assistant'
? `${resolveMessageAnchorKey(msg, index)}:${getRunIdFromMessage(msg) ?? ''}`
: ''
)).join('|'),
async () => {
await nextTick();
await syncProgressState();
},
{ immediate: true },
);
const refreshToolRunProgressAfterSend = async () => {
await nextTick();
void refreshToolRunProgress();
await syncProgressState();
};
const toolRunProgressTitle = computed(() => toolRunProgressConfig.value?.title ?? '');
const prepareToolRunProgressBeforeSend = async () => {
// 按轮 bind:发送前无需 session 级快照。
};
const shouldShowToolRunProgressAfterMessage = (msg: ChatMessage) => {
const matchesBoundScheduleTask = (taskId: unknown): boolean => {
const expected = boundScheduleTaskId.value;
if (!expected) return true;
const actual = Number(taskId);
return Number.isFinite(actual) && actual === expected;
};
const getMessageRunProgress = (msg: ChatMessage) => {
const runId = getRunIdFromMessage(msg);
if (!runId) return null;
return runsById.value[runId] ?? null;
};
const getMessageRunProgressError = (msg: ChatMessage) => {
const runId = getRunIdFromMessage(msg);
if (!runId) return '';
return runErrors.value[runId] || '';
};
const isMessageRunProgressLoading = (msg: ChatMessage) => {
const runId = getRunIdFromMessage(msg);
if (!runId) return false;
return !runsById.value[runId] && !runErrors.value[runId];
};
const shouldShowToolRunProgressAfterMessage = (msg: ChatMessage, _index: number) => {
if (msg?.role !== 'assistant') return false;
if (!(toolRunProgress.value || toolRunProgressLoading.value || toolRunProgressError.value)) return false;
const messageRunId = extractToolRunIdFromContent(msg.content);
const currentRunId = Number(toolRunProgress.value?.run_id || activeToolRunId.value || 0);
return !!messageRunId && messageRunId === currentRunId;
if (!toolRunProgressConfig.value) return false;
if (assistantMessageHasError(msg)) return false;
const runId = getRunIdFromMessage(msg);
if (!runId) return false;
const run = getMessageRunProgress(msg);
if (!run || !isRunningToolRunStatus(run.status)) return false;
if (!matchesBoundScheduleTask(run.task_id)) return false;
return true;
};
const toolRunProgress = computed(() => {
for (let i = ctx.messages.value.length - 1; i >= 0; i -= 1) {
const msg = ctx.messages.value[i];
if (!msg || !shouldShowToolRunProgressAfterMessage(msg, i)) continue;
return getMessageRunProgress(msg);
}
return null;
});
const toolRunProgressLoading = computed(() => pollingRunIds.value.length > 0 && !toolRunProgress.value);
const toolRunProgressError = computed(() => {
const ids = pollingRunIds.value;
for (const runId of ids) {
const err = runErrors.value[runId];
if (err) return err;
}
return '';
});
const toolRunProgressTitle = computed(() => toolRunProgressConfig.value?.title ?? '');
const isToolRunProgressWaiting = computed(() => false);
return {
toolRunProgressConfig,
toolRunProgress,
toolRunProgressLoading,
toolRunProgressError,
toolRunProgressTitle,
isToolRunProgressWaiting,
shouldShowToolRunProgressAfterMessage,
getMessageRunProgress,
getMessageRunProgressError,
isMessageRunProgressLoading,
prepareToolRunProgressBeforeSend,
refreshToolRunProgressAfterSend,
syncProgressForCurrentConversation: syncProgressState,
};
}