perf(subagent): read saved task history by loaded turns (#6634)
This commit is contained in:
1 parent
1196698ecb
commit
eb8546c321
20 files changed
+893
-97
No files matched your search
+46
-8
@@ -696,15 +696,53 @@ 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");
|
||||
export interface SubagentTaskWindow {
|
||||
turnIds: string[];
|
||||
trackedIds: string[];
|
||||
}
|
||||
|
||||
export async function fetchSubagentTasks(
|
||||
token: string, sessionKey: string, window: SubagentTaskWindow,
|
||||
): Promise<SubagentTasksPayload> {
|
||||
const path = `/api/sessions/${encodeURIComponent(sessionKey)}/subagents`;
|
||||
const queries: URLSearchParams[] = [];
|
||||
let query = new URLSearchParams({ scope: "window" });
|
||||
// Track the encoded length as it grows; re-serializing the whole target per id would be quadratic.
|
||||
let length = query.toString().length;
|
||||
let count = 0;
|
||||
for (const [key, values] of [
|
||||
["turn_id", window.turnIds], ["track", window.trackedIds],
|
||||
] as const) {
|
||||
for (const value of values) {
|
||||
const pair = new URLSearchParams({ [key]: value }).toString();
|
||||
// Keep request targets bounded, including percent-encoded identifiers.
|
||||
if (count && (length + 1 + pair.length > 5000 || count >= 400)) {
|
||||
queries.push(query);
|
||||
query = new URLSearchParams({ scope: "window" });
|
||||
length = query.toString().length;
|
||||
count = 0;
|
||||
}
|
||||
query.append(key, value);
|
||||
length += 1 + pair.length;
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
return { tasks: payload.tasks };
|
||||
queries.push(query);
|
||||
const tasks = new Map<string, SubagentTasksPayload["tasks"][number]>();
|
||||
for (const query of queries) {
|
||||
const payload = await request<unknown>(
|
||||
`${path}?${query}`, 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");
|
||||
}
|
||||
for (const task of payload.tasks) {
|
||||
const previous = tasks.get(task.task_id);
|
||||
if (!previous || (task.revision ?? 0) > (previous.revision ?? 0)) tasks.set(task.task_id, task);
|
||||
}
|
||||
}
|
||||
return { tasks: [...tasks.values()] };
|
||||
}
|
||||
|
||||
export async function fetchSubagentThread(
|
||||
|
||||
Reference in new issue
Block a user