feat(subagent): add session-owned task messaging and cancellation (#5985)
let's go!
This commit is contained in:
1 parent
f75470e72f
commit
6ff96f2a8f
93 files changed
+5632
-1156
No files matched your search
@@ -38,6 +38,8 @@ import type {
|
||||
SkillsTrendingPayload,
|
||||
SlashCommand,
|
||||
SlashCommandLifecycle,
|
||||
SubagentTaskSnapshot,
|
||||
SubagentTasksPayload,
|
||||
TranscriptionSettingsUpdate,
|
||||
ThreadProjectionEvent,
|
||||
WebSearchSettingsUpdate,
|
||||
@@ -47,6 +49,7 @@ import type {
|
||||
WorkspaceScopePayload,
|
||||
} from "./types";
|
||||
import { fetchWithTimeout } from "./http";
|
||||
import { isSubagentTask } from "./subagent-tasks";
|
||||
|
||||
const API_READ_TIMEOUT_MS = 20_000;
|
||||
const API_MUTATION_TIMEOUT_MS = 20_000;
|
||||
@@ -547,6 +550,37 @@ export async function updateAutomation(
|
||||
return mutation<AutomationsPayload>(transport, "automation.update", { id, values });
|
||||
}
|
||||
|
||||
export async function fetchSubagentTasks(token: string, sessionKey: string): Promise<SubagentTasksPayload> {
|
||||
const payload = await request<unknown>(
|
||||
`/api/sessions/${encodeURIComponent(sessionKey)}/subagents`, token, undefined, API_READ_TIMEOUT_MS,
|
||||
);
|
||||
if (!isRecord(payload) || !Array.isArray(payload.tasks) || !payload.tasks.every(isSubagentTask)
|
||||
|| new Set(payload.tasks.map((task) => task.task_id)).size !== payload.tasks.length) {
|
||||
throw new Error("Invalid subagent tasks response");
|
||||
}
|
||||
return { tasks: payload.tasks };
|
||||
}
|
||||
|
||||
export async function fetchSubagentThread(
|
||||
token: string, sessionKey: string, taskId: string, signal?: AbortSignal,
|
||||
): Promise<WebuiThreadPersistedPayload> {
|
||||
const payload = await request<unknown>(
|
||||
`/api/sessions/${encodeURIComponent(sessionKey)}/subagents/${encodeURIComponent(taskId)}/webui-thread`,
|
||||
token, { signal, cache: "no-store" }, API_READ_TIMEOUT_MS,
|
||||
);
|
||||
return parseWebuiThreadPayload(payload);
|
||||
}
|
||||
|
||||
export async function cancelSubagentTask(
|
||||
transport: WebUIMutationTransport, sessionKey: string, taskId: string,
|
||||
): Promise<SubagentTaskSnapshot> {
|
||||
const result = await mutation<unknown>(transport, "subagent.cancel", {
|
||||
session_key: sessionKey, task_id: taskId,
|
||||
});
|
||||
if (!isSubagentTask(result) || result.task_id !== taskId) throw new Error("Invalid subagent cancellation response");
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function fetchSkills(
|
||||
token: string,
|
||||
base: string = "",
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import type { SubagentTaskSnapshot } from "./types";
|
||||
|
||||
export interface ObservedSubagentTask extends SubagentTaskSnapshot {
|
||||
observedAtMs: number;
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
export function isSubagentTask(value: unknown): value is SubagentTaskSnapshot {
|
||||
if (!isRecord(value)) return false;
|
||||
const nullableText = (entry: unknown) => entry === null || typeof entry === "string";
|
||||
return typeof value.task_id === "string" && value.task_id.length > 0
|
||||
&& (value.revision === undefined || (typeof value.revision === "number" && Number.isSafeInteger(value.revision) && value.revision >= 0))
|
||||
&& typeof value.label === "string" && typeof value.task_description === "string"
|
||||
&& typeof value.phase === "string" && typeof value.state === "string"
|
||||
&& ["queued", "running", "stopping", "done", "incomplete", "error", "cancelled", "interrupted"].includes(value.state)
|
||||
&& typeof value.elapsed_seconds === "number" && Number.isFinite(value.elapsed_seconds) && value.elapsed_seconds >= 0
|
||||
&& typeof value.iteration === "number" && Number.isInteger(value.iteration) && value.iteration >= 0
|
||||
&& typeof value.created_at === "number" && Number.isFinite(value.created_at)
|
||||
&& (value.completed_at === null || (typeof value.completed_at === "number" && Number.isFinite(value.completed_at)))
|
||||
&& nullableText(value.origin_turn_id) && nullableText(value.origin_message_id)
|
||||
&& nullableText(value.result) && nullableText(value.error) && nullableText(value.stop_reason)
|
||||
&& typeof value.partial === "boolean"
|
||||
&& Array.isArray(value.tool_events) && value.tool_events.every((entry: unknown) => isRecord(entry) && typeof entry.name === "string" && typeof entry.status === "string")
|
||||
&& isRecord(value.receipts) && Object.values(value.receipts).every((entry) => typeof entry === "string" && ["accepted", "delivered", "undelivered"].includes(entry))
|
||||
&& (value.usage === null || (isRecord(value.usage) && Object.values(value.usage).every((entry) => entry === null || typeof entry === "string" || (typeof entry === "number" && Number.isFinite(entry)))));
|
||||
}
|
||||
|
||||
/** Merge observations from initial reads, live events and control replies. */
|
||||
export function mergeSubagentTasks(
|
||||
current: ObservedSubagentTask[], incoming: SubagentTaskSnapshot[],
|
||||
): ObservedSubagentTask[] {
|
||||
const tasks = new Map(current.map((task) => [task.task_id, task]));
|
||||
let changed = false;
|
||||
for (const task of incoming) {
|
||||
const previous = tasks.get(task.task_id);
|
||||
if (previous) {
|
||||
const revision = task.revision ?? 0;
|
||||
const previousRevision = previous.revision ?? 0;
|
||||
if (revision < previousRevision || (revision > 0 && revision === previousRevision)) continue;
|
||||
// Read-only hosts predating events have no revision. Their late reads
|
||||
// must still respect cancellation and the immutable terminal outcome.
|
||||
if (revision === 0 && previousRevision === 0) {
|
||||
if (!["queued", "running", "stopping"].includes(previous.state)) continue;
|
||||
if (previous.state === "stopping" && ["queued", "running"].includes(task.state)) continue;
|
||||
if (previous.state === "running" && task.state === "queued") continue;
|
||||
}
|
||||
}
|
||||
tasks.set(task.task_id, { ...task, observedAtMs: performance.now() });
|
||||
changed = true;
|
||||
}
|
||||
return changed ? [...tasks.values()].sort((a, b) => a.created_at - b.created_at || a.task_id.localeCompare(b.task_id)) : current;
|
||||
}
|
||||
@@ -64,6 +64,34 @@ interface TurnUsage {
|
||||
|
||||
export type RoundUsage = TurnUsage;
|
||||
|
||||
export type SubagentTaskState = "queued" | "running" | "stopping" | "done" | "incomplete" | "error" | "cancelled" | "interrupted";
|
||||
|
||||
export interface SubagentTaskSnapshot {
|
||||
task_id: string;
|
||||
revision?: number;
|
||||
origin_message_id: string | null;
|
||||
origin_turn_id: string | null;
|
||||
created_at: number;
|
||||
completed_at: number | null;
|
||||
label: string;
|
||||
task_description: string;
|
||||
state: SubagentTaskState;
|
||||
phase: string;
|
||||
elapsed_seconds: number;
|
||||
iteration: number;
|
||||
tool_events: { name: string; status: string }[];
|
||||
usage: Record<string, number | string | null> | null;
|
||||
receipts: Record<string, "accepted" | "delivered" | "undelivered">;
|
||||
result: string | null;
|
||||
partial: boolean;
|
||||
stop_reason: string | null;
|
||||
error: string | null;
|
||||
}
|
||||
|
||||
export interface SubagentTasksPayload {
|
||||
tasks: SubagentTaskSnapshot[];
|
||||
}
|
||||
|
||||
export interface ResponseSource {
|
||||
provider: string;
|
||||
model: string;
|
||||
@@ -488,6 +516,9 @@ export interface SidebarStatePayload {
|
||||
}
|
||||
|
||||
export interface BootstrapResponse {
|
||||
terminal?: {
|
||||
webui?: { capabilities?: string[] };
|
||||
};
|
||||
token?: string;
|
||||
api_token?: string;
|
||||
ws_path: string;
|
||||
@@ -1353,6 +1384,7 @@ interface InboundTurnMetadata {
|
||||
}
|
||||
|
||||
export type InboundEvent =
|
||||
| { event: "subagent_task"; chat_id: string; task: SubagentTaskSnapshot }
|
||||
| { event: "ready"; chat_id: string; client_id: string }
|
||||
| {
|
||||
event: "attached";
|
||||
|
||||
Reference in new issue
Block a user