Open-source SEO, GEO, and marketing skills for AI agents.
fix(tasks): stream agent tokens live by routing kickoff through /api/chat
Yuting Zhong31163e343d2a
diff --git a/src/app/(app)/[project]/agents/[agent]/tasks/page.tsx b/src/app/(app)/[project]/agents/[agent]/tasks/page.tsxindex 82398e57..278eaa0b 100644--- a/src/app/(app)/[project]/agents/[agent]/tasks/page.tsx+++ b/src/app/(app)/[project]/agents/[agent]/tasks/page.tsx@@ -10,7 +10,8 @@ import {type TranscriptEvent,} from "@/server/openclaw/transcript-tail";import { generateTaskThreadId } from "@/server/orchestration/process-blocks";-import { startTaskIfProposed } from "@/server/orchestration/run-task";+import { claimTaskIfProposed } from "@/server/orchestration/run-task";+import { buildTaskKickoffMessage } from "@/server/orchestration/task-kickoff";import type { Task } from "@/types";type Props = {@@ -24,6 +25,13 @@ type SelectedBundle = {sessionKey: string;initialEvents: TranscriptEvent[];initialByteOffset: number;+ /**+ * When set, the client should auto-send this message via /api/chat to+ * kick the task off — that path streams agent tokens as SSE in real+ * time. Only populated when this page-load is the one that flipped the+ * task from proposed to running.+ */+ kickoffMessage: string | null;};export default async function AgentTasksPage({ params, searchParams }: Props) {@@ -80,9 +88,14 @@ async function loadSelectedBundle(if (!task.thread_id) return null;const threadId = task.thread_id;- // Atomically transition proposed → running and fire the kickoff. No-op- // when the task isn't proposed, so reloading the page doesn't restart.- task = startTaskIfProposed(task);+ // Claim proposed → running but DON'T fire the server-side kickoff. The+ // client will send the kickoff message through /api/chat so the agent's+ // tokens stream live as SSE deltas. No-op when the task isn't proposed,+ // so reloading the page doesn't restart it. The justClaimed flag tells us+ // whether this page-load is the one that should drive the kickoff.+ const { task: nextTask, justClaimed } = claimTaskIfProposed(task);+ task = nextTask;+ const kickoffMessage = justClaimed ? buildTaskKickoffMessage(task) : null;// Resolve canonical sessionKey for /api/chat composer sends (when task// is done and user wants to keep chatting). The pending key is a safe@@ -99,5 +112,6 @@ async function loadSelectedBundle(sessionKey,initialEvents: events,initialByteOffset: byteOffset,+ kickoffMessage,};}diff --git a/src/components/agent-task-workspace.tsx b/src/components/agent-task-workspace.tsxindex 63593ed9..51b44b7d 100644--- a/src/components/agent-task-workspace.tsx+++ b/src/components/agent-task-workspace.tsx@@ -53,6 +53,13 @@ type SelectedTaskBundle = {sessionKey: string;initialEvents: TranscriptEvent[];initialByteOffset: number;+ /**+ * Server-built kickoff prompt. Non-null only when this page-load was the+ * one that flipped the task from proposed → running; the client fires it+ * via /api/chat so the agent's response streams as SSE deltas in real+ * time instead of arriving as a single JSONL-flushed wall of text.+ */+ kickoffMessage: string | null;};type Props = {@@ -307,6 +314,8 @@ function SelectedTaskPanel({initialByteOffset={selected.initialByteOffset}composerDisabled={isInFlight}onPolled={onPolled}+ autoKickoff={!!selected.kickoffMessage}+ kickoffMessage={selected.kickoffMessage ?? undefined}/>);}diff --git a/src/server/orchestration/run-task.ts b/src/server/orchestration/run-task.tsindex 78864467..27a830b3 100644--- a/src/server/orchestration/run-task.ts+++ b/src/server/orchestration/run-task.ts@@ -11,14 +11,16 @@ import { buildTaskKickoffMessage } from "./task-kickoff";/*** Idempotent "claim and kickoff" — flips a proposed task to running and* fires the server-side kickoff. No-op when the task isn't proposed (already- * started, already done, etc.). Used by the agent workspace so opening a- * proposed task auto-starts it the same way "Start all" does for batches,- * without bouncing through the chat client.+ * started, already done, etc.). Used by sub-task delegation paths (CMO+ * creates a task for a specialist, no client is watching) and the "Start+ * all" batch button.*- * Returns the post-transition task (still proposed if no claim happened, or- * running on success). The kickoff itself runs fire-and-forget — callers- * shouldn't await it; the workspace's live transcript polling will reveal- * progress.+ * The kickoff itself runs fire-and-forget — callers shouldn't await it.+ * Since the gateway stream is consumed server-side, the page only sees+ * tokens once OpenClaw flushes the complete assistant message to JSONL+ * (typically when the turn ends). For tasks the user is actively watching+ * in the workspace, prefer `claimTaskIfProposed` and let the client run the+ * kickoff via `/api/chat` so SSE deltas stream live.*/export function startTaskIfProposed(task: Task): Task {if (task.status !== "proposed") return task;@@ -30,6 +32,26 @@ export function startTaskIfProposed(task: Task): Task {return claimed;}+/**+ * Flip a proposed task to running without firing the gateway. Used by the+ * task workspace so opening a proposed task immediately reflects "running"+ * in the UI while the client drives the kickoff itself via `/api/chat` —+ * that path streams the agent's tokens as SSE so the user sees the response+ * forming in real time instead of a wall of text dumping in at the end.+ *+ * Returns the (possibly-claimed) task plus a `justClaimed` flag the page+ * uses to decide whether the client should auto-send the kickoff message.+ */+export function claimTaskIfProposed(task: Task): {+ task: Task;+ justClaimed: boolean;+} {+ if (task.status !== "proposed") return { task, justClaimed: false };+ const claimed = updateTask(task.id, { status: "running" });+ if (!claimed) return { task, justClaimed: false };+ return { task: claimed, justClaimed: true };+}+/*** Server-side kickoff for a task. Consumes the full gateway stream (no SSE* pipe to a client) and applies orchestration blocks the assignee emits.