Merge pull request '任务日志自动跟随最新事件' (#2) from fix/task-log-follow into main
Verify control plane / verify (push) Canceled after 0s

Reviewed-on: http://git.xhmcl.com/xhgit/webup-cloud/pulls/2
This commit was merged in pull request #2.
This commit is contained in:
xhgit committed 2026-11-07 10:53:32 +00:00
commit 6b5aea0a0d
3 files changed
+53 -8

No files matched your search

@@ -68,8 +68,9 @@ public class TaskController {
@AuthenticationPrincipal SecurityConfig.Identity who, @PathVariable UUID id) {
tasks.find(id, who.id());
return db.queryForList(
"SELECT * FROM ai_task_steps WHERE task_id=? AND event NOT IN"
+ " ('AUTO_SETTLEMENT','MANUAL_SETTLEMENT') ORDER BY created_at,id LIMIT 1000",
"SELECT * FROM (SELECT * FROM ai_task_steps WHERE task_id=? AND event NOT IN"
+ " ('AUTO_SETTLEMENT','MANUAL_SETTLEMENT') ORDER BY created_at DESC,id DESC LIMIT"
+ " 1000) AS recent ORDER BY created_at,id",
id);
}
+1 -1
View File
@@ -104,7 +104,7 @@
| --- | --- | --- |
| GET / POST | `/api/tasks` | 本人最近 100 条 / 创建并预留预算 |
| GET | `/api/tasks/{id}` | 本人任务、预算与消费,不返回租约令牌 |
| GET | `/api/tasks/{id}/events` | 本人任务事件(最多 1000 条) |
| GET | `/api/tasks/{id}/events` | 本人最近 1000 条任务事件,按时间从早到晚返回 |
| GET | `/api/tasks/{id}/messages` | 本人持久化消息与工具上下文 |
| POST | `/api/tasks/{id}/pause` | 暂停新的调用,保留剩余预算 |
| POST | `/api/tasks/{id}/resume` | PAUSED 任务重新入队 |
+49 -5
View File
@@ -19,6 +19,40 @@ const fileBusy = ref(false),
fileError = ref("");
let fileGeneration = 0;
let fileTaskSignature = "";
const logPanel = ref<HTMLElement | null>(null);
const followLogs = ref(true);
let logRequest = 0;
let appliedLogRequest = 0;
function scrollLogsToLatest() {
followLogs.value = true;
const panel = logPanel.value;
if (panel) panel.scrollTop = panel.scrollHeight;
}
function trackLogScroll() {
const panel = logPanel.value;
if (panel)
followLogs.value =
panel.scrollHeight - panel.scrollTop - panel.clientHeight <= 24;
}
watch(
[() => selected.value?.id, () => events.value.at(-1)?.id],
([id], [previousId]) => {
if (id !== previousId || followLogs.value) scrollLogsToLatest();
},
{ flush: "post" },
);
function selectTask(task: any) {
if (task.id !== selected.value?.id) {
events.value = [];
followLogs.value = true;
appliedLogRequest = ++logRequest;
}
selected.value = task;
}
const execution = ref<any>({
ready: false,
state: "CHECKING",
@@ -77,11 +111,13 @@ async function load() {
}
const id = selected.value?.id;
if (id) {
const request = ++logRequest;
const [task, log] = await Promise.all([
api("/tasks/" + id),
api("/tasks/" + id + "/events"),
]);
if (selected.value?.id === id) {
if (selected.value?.id === id && request > appliedLogRequest) {
appliedLogRequest = request;
const changed =
selected.value.status !== task.status ||
events.value.at(-1)?.id !== log.at(-1)?.id;
@@ -102,8 +138,7 @@ async function load() {
}
}
async function select(t: any) {
selected.value = t;
events.value = [];
selectTask(t);
form.projectId = t.project_id;
await load();
}
@@ -115,7 +150,7 @@ async function start() {
busy.value = true;
error.value = "";
try {
selected.value = await api("/tasks", { method: "POST", body: body(form) });
selectTask(await api("/tasks", { method: "POST", body: body(form) }));
form.idempotencyKey = crypto.randomUUID();
success.value = "执行服务已连接,任务已提交并预留预算。";
await load();
@@ -515,7 +550,13 @@ onUnmounted(() => clearInterval(timer));
</div>
</div>
<div v-if="selected.error" class="alert info">{{ selected.error }}</div>
<div class="event-log">
<div
ref="logPanel"
class="event-log"
aria-label="任务日志"
tabindex="0"
@scroll.passive="trackLogScroll"
>
<div v-for="e in events" :key="e.id">
<small
>{{ new Date(e.created_at).toLocaleTimeString() }} ·
@@ -525,6 +566,9 @@ onUnmounted(() => clearInterval(timer));
</div>
<p v-if="!events.length" class="empty">等待任务事件</p>
</div>
<button v-if="!followLogs" class="secondary" @click="scrollLogsToLatest">
跳到最新日志
</button>
</section>
<section class="panel form-panel">
<div class="panel-heading">