feat(engine): 添加技能查看工具并优化异步任务管理 - 添加SkillViewTool到引擎加载器中,增强技能管理功能 - 在AgentLoop中引入_active_direct_task来跟踪活跃任务 - 实现直接任务执行时的同步处理逻辑 - 更新工具实例化方式以支持依赖注入 feat(config): 增加智能体运行时参数配置支持 - 扩展AgentDefaultsConfig添加max_tokens和temperature字段 - 实现配置解析函数_first_config_value处理多个配置源 - 支持通过Web API动态更新智能体运行时参数 - 添加前端页面配置表单和验证逻辑 refactor(provider): 统一最大令牌数参数类型为可选整型 - 将所有LLM提供者的max_tokens参数改为int | None类型 - 为AnthropicProvider实现模型特定的最大令牌数默认值 - 调整参数传递逻辑,优先级:调用参数 > 配置文件 > 模型默认值 - 移除硬编码的默认值,改用条件判断 feat(process): 增强事件投影功能 - 添加工具调用开始/结束事件的映射逻辑 - 实现技能激活事件的识别和展示 - 添加辅助函数处理工具调用名称和参数提取 - 优化运行记录关联逻辑,提升事件匹配准确性 fix(web): 更新网络请求客户端信任环境设置 - 将WebFetchTool和WebSearchTool的trust_env参数设为True - 确保HTTP客户端能够正确使用系统代理配置 - 修复可能的网络连接问题 test: 添加配置加载和事件投影相关测试 - 新增智能体默认参数配置测试用例 - 实现API配置持久化和重载测试 - 添加技能卡片和工具事件的投影测试 ```
258 lines
9.8 KiB
TypeScript
258 lines
9.8 KiB
TypeScript
'use client';
|
|
|
|
import Link from 'next/link';
|
|
import { useParams, useRouter } from 'next/navigation';
|
|
import React, { useMemo, useState } from 'react';
|
|
import { AlertCircle, ArrowLeft, Loader2, Trash2 } from 'lucide-react';
|
|
|
|
import {
|
|
TaskLiveHeader,
|
|
TaskSideRail,
|
|
TaskTimeline,
|
|
type TaskFeedbackItem,
|
|
type TaskFeedbackType,
|
|
} from '@/components/task-detail';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Card, CardContent } from '@/components/ui/card';
|
|
import { deleteBackendTask, getBackendTask, submitChatFeedback } from '@/lib/api';
|
|
import { pickAppText } from '@/lib/i18n/core';
|
|
import { useAppI18n } from '@/lib/i18n/provider';
|
|
import { useChatStore } from '@/lib/store';
|
|
import { shouldPollTaskDetail, taskDetailDurationMs } from '@/lib/task-detail-refresh';
|
|
import { buildTaskTimelineCards } from '@/lib/task-timeline';
|
|
import type { BackendTask } from '@/types';
|
|
|
|
const TERMINAL_TASK_STATUSES = new Set(['closed', 'abandoned', 'cancelled', 'error']);
|
|
const TASK_RESULT_REVIEW_ID = 'task-result-review';
|
|
|
|
export default function TaskDetailPage() {
|
|
const { locale } = useAppI18n();
|
|
const router = useRouter();
|
|
const params = useParams<{ taskId: string }>();
|
|
const taskId = decodeURIComponent(Array.isArray(params?.taskId) ? params.taskId[0] : params?.taskId ?? '');
|
|
const processRuns = useChatStore((state) => state.processRuns);
|
|
const processEvents = useChatStore((state) => state.processEvents);
|
|
const processArtifacts = useChatStore((state) => state.processArtifacts);
|
|
const setSessionProcess = useChatStore((state) => state.setSessionProcess);
|
|
const updateMessageFeedback = useChatStore((state) => state.updateMessageFeedback);
|
|
const wsStatus = useChatStore((state) => state.wsStatus);
|
|
|
|
const [backendTask, setBackendTask] = useState<BackendTask | null>(null);
|
|
const [backendTaskLoading, setBackendTaskLoading] = useState(true);
|
|
const [revision, setRevision] = useState('');
|
|
const [actionError, setActionError] = useState<string | null>(null);
|
|
const [actionBusy, setActionBusy] = useState<string | null>(null);
|
|
const mountedRef = React.useRef(true);
|
|
|
|
React.useEffect(() => {
|
|
return () => {
|
|
mountedRef.current = false;
|
|
};
|
|
}, []);
|
|
|
|
const loadBackendTask = React.useCallback(async () => {
|
|
if (!taskId) return null;
|
|
setBackendTaskLoading(true);
|
|
try {
|
|
const item = await getBackendTask(taskId);
|
|
if (!mountedRef.current) return item;
|
|
setBackendTask(item);
|
|
setSessionProcess(item.session_id, {
|
|
runs: item.process_runs ?? [],
|
|
events: item.process_events ?? [],
|
|
artifacts: item.process_artifacts ?? [],
|
|
});
|
|
return item;
|
|
} catch {
|
|
if (mountedRef.current) {
|
|
setBackendTask(null);
|
|
}
|
|
return null;
|
|
} finally {
|
|
if (mountedRef.current) {
|
|
setBackendTaskLoading(false);
|
|
}
|
|
}
|
|
}, [setSessionProcess, taskId]);
|
|
|
|
React.useEffect(() => {
|
|
void loadBackendTask();
|
|
}, [loadBackendTask]);
|
|
|
|
const isTaskLive = backendTask ? !TERMINAL_TASK_STATUSES.has(backendTask.status) : false;
|
|
|
|
React.useEffect(() => {
|
|
if (!shouldPollTaskDetail(backendTask)) return;
|
|
const id = window.setInterval(() => {
|
|
void loadBackendTask();
|
|
}, 4000);
|
|
return () => window.clearInterval(id);
|
|
}, [backendTask, loadBackendTask]);
|
|
|
|
const taskRunIds = useMemo(() => {
|
|
const ids = new Set<string>();
|
|
for (const run of backendTask?.process_runs ?? []) ids.add(run.run_id);
|
|
for (const runId of backendTask?.run_ids ?? []) ids.add(runId);
|
|
return ids;
|
|
}, [backendTask]);
|
|
|
|
const liveRuns = useMemo(
|
|
() => processRuns.filter((run) => taskRunIds.has(run.run_id) || run.metadata?.task_id === taskId),
|
|
[processRuns, taskId, taskRunIds]
|
|
);
|
|
|
|
const liveEvents = useMemo(
|
|
() => processEvents.filter((event) => taskRunIds.has(event.run_id) || event.metadata?.task_id === taskId),
|
|
[processEvents, taskId, taskRunIds]
|
|
);
|
|
|
|
const liveArtifacts = useMemo(
|
|
() => processArtifacts.filter((artifact) => taskRunIds.has(artifact.run_id) || artifact.metadata?.task_id === taskId),
|
|
[processArtifacts, taskId, taskRunIds]
|
|
);
|
|
|
|
const renderedRuns = liveRuns.length > 0 ? liveRuns : backendTask?.process_runs ?? [];
|
|
const renderedEvents = liveEvents.length > 0 ? liveEvents : backendTask?.process_events ?? [];
|
|
const renderedArtifacts = liveArtifacts.length > 0 ? liveArtifacts : backendTask?.process_artifacts ?? [];
|
|
|
|
const timelineCards = useMemo(
|
|
() =>
|
|
backendTask
|
|
? buildTaskTimelineCards({
|
|
task: backendTask,
|
|
processRuns: renderedRuns,
|
|
processEvents: renderedEvents,
|
|
processArtifacts: renderedArtifacts,
|
|
})
|
|
: [],
|
|
[backendTask, renderedArtifacts, renderedEvents, renderedRuns]
|
|
);
|
|
|
|
const activeLabel =
|
|
[...timelineCards].reverse().find((card) => !['acceptance', 'task_created'].includes(card.type))?.title ?? '-';
|
|
const durationMs = backendTask ? taskDetailDurationMs(backendTask) : null;
|
|
const feedbackRunId = backendTask ? pickFeedbackRunId(backendTask) : null;
|
|
|
|
const runAction = async (key: string, action: () => Promise<unknown>) => {
|
|
setActionBusy(key);
|
|
setActionError(null);
|
|
try {
|
|
await action();
|
|
} catch (err: any) {
|
|
setActionError(err.message || pickAppText(locale, '操作失败', 'Action failed'));
|
|
} finally {
|
|
setActionBusy(null);
|
|
}
|
|
};
|
|
|
|
const deleteCurrentBackendTask = async () => {
|
|
if (!backendTask) return;
|
|
const title = backendTask.short_title || backendTask.description || backendTask.goal || backendTask.task_id;
|
|
if (!window.confirm(pickAppText(locale, `删除任务“${title}”?`, `Delete task "${title}"?`))) {
|
|
return;
|
|
}
|
|
await runAction('delete-backend-task', async () => {
|
|
await deleteBackendTask(backendTask.task_id);
|
|
router.push('/tasks');
|
|
});
|
|
};
|
|
|
|
if (backendTask) {
|
|
const feedbackItems = backendTask.feedback || [];
|
|
|
|
return (
|
|
<div className="min-h-screen bg-background">
|
|
<TaskLiveHeader task={backendTask} activeLabel={activeLabel} durationMs={durationMs} reviewTargetId={TASK_RESULT_REVIEW_ID} />
|
|
|
|
<main className="mx-auto grid max-w-7xl gap-6 p-6 xl:grid-cols-[minmax(0,1fr)_360px]">
|
|
<div className="space-y-4">
|
|
<div className="flex justify-end">
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
className="text-destructive hover:text-destructive"
|
|
disabled={Boolean(actionBusy)}
|
|
onClick={() => void deleteCurrentBackendTask()}
|
|
>
|
|
<Trash2 className="mr-2 h-4 w-4" />
|
|
{pickAppText(locale, '删除任务', 'Delete task')}
|
|
</Button>
|
|
</div>
|
|
|
|
{actionError ? (
|
|
<Card className="border-destructive">
|
|
<CardContent className="flex items-center gap-2 p-4 text-sm text-destructive">
|
|
<AlertCircle className="h-4 w-4" />
|
|
{actionError}
|
|
</CardContent>
|
|
</Card>
|
|
) : null}
|
|
|
|
<TaskTimeline
|
|
cards={timelineCards}
|
|
isLive={isTaskLive && wsStatus === 'connected'}
|
|
reviewTargetId={TASK_RESULT_REVIEW_ID}
|
|
resultAcceptance={{
|
|
sessionId: backendTask.session_id,
|
|
runId: feedbackRunId,
|
|
taskStatus: backendTask.status,
|
|
feedbackItems: feedbackItems as TaskFeedbackItem[],
|
|
actionBusy,
|
|
revision,
|
|
onRevisionChange: setRevision,
|
|
onSubmit: (feedbackType: TaskFeedbackType, comment?: string) =>
|
|
runAction(`backend-feedback-${feedbackType}`, async () => {
|
|
if (!feedbackRunId) throw new Error(pickAppText(locale, '暂无可验收的运行记录。', 'No run is available for acceptance yet.'));
|
|
await submitChatFeedback({
|
|
sessionId: backendTask.session_id,
|
|
runId: feedbackRunId,
|
|
feedbackType,
|
|
comment,
|
|
});
|
|
updateMessageFeedback(feedbackRunId, feedbackType);
|
|
setRevision('');
|
|
await loadBackendTask();
|
|
}),
|
|
}}
|
|
/>
|
|
</div>
|
|
|
|
<TaskSideRail task={backendTask} runs={renderedRuns} artifacts={renderedArtifacts} cards={timelineCards} />
|
|
</main>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="mx-auto flex max-w-4xl flex-col gap-4 p-6">
|
|
<Button asChild variant="outline" className="w-fit">
|
|
<Link href="/tasks">
|
|
<ArrowLeft className="mr-2 h-4 w-4" />
|
|
{pickAppText(locale, '返回任务列表', 'Back to tasks')}
|
|
</Link>
|
|
</Button>
|
|
<Card className="border-dashed">
|
|
<CardContent className="py-16 text-center">
|
|
<div className="flex justify-center">
|
|
{backendTaskLoading ? <Loader2 className="mb-4 h-5 w-5 animate-spin text-muted-foreground" /> : null}
|
|
</div>
|
|
<h1 className="text-2xl font-semibold">{pickAppText(locale, '任务不存在', 'Task not found')}</h1>
|
|
<p className="mt-2 text-sm text-muted-foreground">
|
|
{backendTaskLoading
|
|
? pickAppText(locale, '正在从后端任务库加载任务。', 'Loading the task from the backend task store.')
|
|
: pickAppText(locale, '后端任务库里没有这个任务。', 'The backend task store does not contain this task.')}
|
|
</p>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function pickFeedbackRunId(task: BackendTask): string | null {
|
|
const runIds = task.run_ids.filter(Boolean);
|
|
if (runIds.length > 0) return runIds[runIds.length - 1];
|
|
const runs = task.runs ?? [];
|
|
if (runs.length > 0) return runs[runs.length - 1].run_id;
|
|
return null;
|
|
}
|