151 lines
5.5 KiB
TypeScript
151 lines
5.5 KiB
TypeScript
/**
|
||
* features/chat/utils/toolRunProgressConfig.ts
|
||
* 用途:解析 Agent tool_run_progress 配置;构建 for-log / detail 轮询 URL。
|
||
* 依赖:ToolRunProgressConfig、ChatMessage。
|
||
* 逻辑:文件检核进度 URL 由 schedule_task_id + chat_log_id/run_id 推导,无需 Agent 配置 log_progress_url。
|
||
*/
|
||
import type { ToolRunProgressConfig } from '../composables/useToolRunProgress';
|
||
import type { ChatMessage } from '../types/chat.types';
|
||
|
||
export interface ResolvedToolRunProgressOptions {
|
||
empty_poll_max: number;
|
||
}
|
||
|
||
/** 文件检核 for-log 轮询路径(固定;query 带 task_id、chat_log_id) */
|
||
export const FILE_CHECK_LOG_PROGRESS_PATH = '/api/workspace/file-check/runs/for-log';
|
||
|
||
/** 文件检核 detail 轮询路径模板(固定;{run_id} 替换为批次 ID) */
|
||
export const FILE_CHECK_DETAIL_PATH_TEMPLATE = '/api/workspace/file-check/runs/{run_id}';
|
||
|
||
const DEFAULT_EMPTY_POLL_MAX = 30;
|
||
|
||
export function resolveToolRunProgressOptions(
|
||
raw: Record<string, unknown> | ToolRunProgressConfig | null | undefined,
|
||
): ResolvedToolRunProgressOptions {
|
||
const cfg = raw && typeof raw === 'object' ? raw : {};
|
||
const emptyRaw = Number(cfg.empty_poll_max);
|
||
|
||
return {
|
||
empty_poll_max: Number.isFinite(emptyRaw) && emptyRaw > 0
|
||
? Math.min(emptyRaw, 120)
|
||
: DEFAULT_EMPTY_POLL_MAX,
|
||
};
|
||
}
|
||
|
||
export function resolveMessageAnchorKey(msg: ChatMessage, index: number): string {
|
||
const logId = Number(msg.chat_log_id);
|
||
if (Number.isFinite(logId) && logId > 0) return `log-${logId}`;
|
||
const localId = String(msg.local_id || '').trim();
|
||
if (localId) return `local-${localId}`;
|
||
return `idx-${index}`;
|
||
}
|
||
|
||
export function assistantMessageHasError(msg: ChatMessage | null | undefined): boolean {
|
||
return String(msg?.content || '').includes('[Error:');
|
||
}
|
||
|
||
export function resolveScheduleTaskId(cfg: ToolRunProgressConfig | null | undefined): number | null {
|
||
if (!cfg) return null;
|
||
const raw = cfg.schedule_task_id ?? cfg.task_id;
|
||
const id = Number(raw);
|
||
return Number.isFinite(id) && id > 0 ? id : null;
|
||
}
|
||
|
||
/**
|
||
* 文件检核 for-log 基路径;不读 Agent 配置,避免运维维护 URL。
|
||
*/
|
||
export function resolveFileCheckLogProgressPath(): string {
|
||
return FILE_CHECK_LOG_PROGRESS_PATH;
|
||
}
|
||
|
||
/**
|
||
* 按 run_id 生成文件检核 detail 轮询 URL。
|
||
*/
|
||
export function buildFileCheckDetailUrl(runId: number): string {
|
||
if (!Number.isFinite(runId) || runId <= 0) return '';
|
||
return FILE_CHECK_DETAIL_PATH_TEMPLATE.replace('{run_id}', encodeURIComponent(String(runId)));
|
||
}
|
||
|
||
export function buildLogProgressUrl(
|
||
baseUrl: string,
|
||
taskId: number,
|
||
chatLogId: number,
|
||
): string {
|
||
const [rawPath = baseUrl, query = ''] = baseUrl.split('?');
|
||
const path = rawPath || baseUrl;
|
||
const search = new URLSearchParams(query);
|
||
search.set('task_id', String(taskId));
|
||
search.set('chat_log_id', String(chatLogId));
|
||
search.delete('session_id');
|
||
search.delete('conversation_id');
|
||
const qs = search.toString();
|
||
return qs ? `${path}?${qs}` : path;
|
||
}
|
||
|
||
export function buildFileCheckLogProgressUrl(taskId: number, chatLogId: number): string {
|
||
return buildLogProgressUrl(resolveFileCheckLogProgressPath(), taskId, chatLogId);
|
||
}
|
||
|
||
/** @deprecated 仅兼容旧调用;新逻辑请用 buildFileCheckLogProgressUrl */
|
||
export function resolveLogProgressBaseUrl(_cfg?: ToolRunProgressConfig): string {
|
||
return resolveFileCheckLogProgressPath();
|
||
}
|
||
|
||
export function getRunIdFromMessage(msg: ChatMessage | null | undefined): number | null {
|
||
const runId = Number(msg?.run_id);
|
||
return Number.isFinite(runId) && runId > 0 ? runId : null;
|
||
}
|
||
|
||
export function isRunningToolRunStatus(status?: string | null): boolean {
|
||
return String(status || '').toLowerCase() === 'running';
|
||
}
|
||
|
||
const TERMINAL_TOOL_RUN_STATUSES = new Set(['success', 'partial_failed', 'failed', 'cancelled', 'canceled']);
|
||
|
||
/**
|
||
* 判断检核批次是否已进入终态(成功/失败/取消等)。
|
||
* 核心逻辑:与 useToolRunProgress 轮询终止条件保持一致,供进度卡片展示判断复用。
|
||
*/
|
||
export function isTerminalToolRunStatus(status?: string | null): boolean {
|
||
return TERMINAL_TOOL_RUN_STATUSES.has(String(status || '').toLowerCase());
|
||
}
|
||
|
||
export function collectAssistantRunIds(messages: ChatMessage[]): number[] {
|
||
const ids = new Set<number>();
|
||
for (const msg of messages) {
|
||
if (msg?.role !== 'assistant') continue;
|
||
const runId = getRunIdFromMessage(msg);
|
||
if (runId) ids.add(runId);
|
||
}
|
||
return [...ids];
|
||
}
|
||
|
||
export interface AssistantProgressTarget {
|
||
message: ChatMessage;
|
||
messageIndex: number;
|
||
runId: number | null;
|
||
chatLogId: number | null;
|
||
}
|
||
|
||
/**
|
||
* 收集助手消息上的进度轮询目标:优先 run_id,否则用 chat_log_id 走 for-log 兜底。
|
||
* 核心逻辑:run_id 未从 SSE 回写时,仍可通过 chat_log_id 解析已绑定的检核批次。
|
||
*/
|
||
export function collectAssistantProgressTargets(messages: ChatMessage[]): AssistantProgressTarget[] {
|
||
const targets: AssistantProgressTarget[] = [];
|
||
messages.forEach((msg, messageIndex) => {
|
||
if (msg?.role !== 'assistant') return;
|
||
const runId = getRunIdFromMessage(msg);
|
||
const chatLogIdRaw = Number(msg.chat_log_id);
|
||
const chatLogId = Number.isFinite(chatLogIdRaw) && chatLogIdRaw > 0 ? chatLogIdRaw : null;
|
||
if (!runId && !chatLogId) return;
|
||
targets.push({ message: msg, messageIndex, runId, chatLogId });
|
||
});
|
||
return targets;
|
||
}
|
||
|
||
export function getChatLogIdFromMessage(msg: ChatMessage | null | undefined): number | null {
|
||
const chatLogId = Number(msg?.chat_log_id);
|
||
return Number.isFinite(chatLogId) && chatLogId > 0 ? chatLogId : null;
|
||
}
|