File size: 17,428 Bytes
c453128 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | import type { ActivityEvent, AgentCapabilities, AgentPersona, AgentProfile, AgentPrototype, AuditResult, DeskExport, DeskHistory, FileNode, FilePreviewData, LlmProvider, Session, SubagentRecord, TodoData, WorkerEvent } from "../types";
const BASE = "/api";
const WS_BASE = `${location.protocol === "https:" ? "wss" : "ws"}://${location.host}`;
async function errorDetail(r: Response): Promise<string> {
try {
const j = await r.json();
if (typeof j.detail === "string") return j.detail;
if (j.detail) return JSON.stringify(j.detail);
} catch { /* ignore */ }
return `${r.status} ${r.statusText}`;
}
async function get<T>(path: string): Promise<T> {
const r = await fetch(BASE + path);
if (!r.ok) throw new Error(`${r.status} ${r.statusText}`);
return r.json();
}
async function post<T>(path: string, body: unknown): Promise<T> {
const r = await fetch(BASE + path, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!r.ok) throw new Error(await errorDetail(r));
return r.json();
}
async function put<T>(path: string, body: unknown): Promise<T> {
const r = await fetch(BASE + path, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!r.ok) throw new Error(await errorDetail(r));
return r.json();
}
async function patch<T>(path: string, body: unknown): Promise<T> {
const r = await fetch(BASE + path, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!r.ok) throw new Error(await errorDetail(r));
return r.json();
}
async function del<T>(path: string): Promise<T> {
const r = await fetch(BASE + path, { method: "DELETE" });
if (!r.ok) throw new Error(`${r.status} ${r.statusText}`);
return r.json();
}
export const api = {
sessions: {
list: (limit = 50, offset = 0) =>
get<Session[]>(`/sessions?limit=${limit}&offset=${offset}`),
get: (id: string) => get<Session>(`/sessions/${id}`),
patchDeskConfig: (id: string, body: { agent?: string; model?: string; tools?: string[] }) =>
patch<Session>(`/sessions/${encodeURIComponent(id)}/desk-config`, body),
// Default to the full desk conversation (server caps far above any realistic
// desk size). Callers that only need a peek pass an explicit small limit
// (e.g. the manager's quick glance).
activity: (id: string, limit = 1_000_000, tail = false) =>
get<ActivityEvent[]>(
`/sessions/${id}/activity?limit=${limit}${tail ? "&tail=1" : ""}`,
),
overview: (id: string, limit = 10000) =>
get<{
events: ActivityEvent[];
started_at: string | null;
last_at: string | null;
message_count: number;
session_ids?: string[];
truncated?: boolean;
}>(`/sessions/${id}/overview?limit=${limit}`),
consoleHistory: (id: string, limit = 2000) =>
get<{ text: string }>(`/sessions/${id}/console?limit=${limit}`),
terminalHistory: (id: string, limit = 2000) =>
get<{ text: string }>(`/sessions/${id}/terminal?limit=${limit}`),
files: (id: string) => get<FileNode[]>(`/sessions/${id}/files`),
workspaceTree: (id: string) => get<FileNode[]>(`/sessions/${id}/workspace_tree`),
todos: (id: string) => get<TodoData>(`/sessions/${id}/todos`),
delete: (id: string) =>
del<{ ok: boolean; deleted: boolean; sandbox: boolean; workspace: boolean; transcripts: boolean; container: boolean }>(
`/sessions/${id}`,
),
interrupt: (id: string) => post<{ ok: boolean }>(`/sessions/${id}/interrupt`, {}),
inspect: (id: string, tool: string, args: Record<string, unknown>) =>
post<{ ok: boolean; tool: string; result?: string; error?: string }>(
`/sessions/${id}/inspect`, { tool, args }),
inspectStop: (id: string) =>
post<{ ok: boolean; error?: string }>(`/sessions/${id}/inspect/stop`, {}),
arrive: (id: string) => post<{ ok: boolean }>(`/sessions/${id}/arrive`, {}),
sleep: (id: string) => post<{ ok: boolean }>(`/sessions/${id}/sleep`, {}),
wake: (id: string) => post<{ ok: boolean }>(`/sessions/${id}/wake`, {}),
resume: (id: string, content: string, attachments?: { name: string; data: string }[], agent?: string, reasoning_effort?: string, api_mode?: string) =>
post<{ ok: boolean }>(`/sessions/${id}/resume`, {
content,
...(attachments?.length ? { attachments } : {}),
...(agent !== undefined ? { agent } : {}),
...(reasoning_effort !== undefined ? { reasoning_effort } : {}),
...(api_mode !== undefined ? { api_mode } : {}),
}),
redirect: (id: string, content: string, attachments?: { name: string; data: string }[], reasoning_effort?: string, api_mode?: string) =>
post<{ ok: boolean }>(`/sessions/${id}/redirect`, {
content,
...(attachments?.length ? { attachments } : {}),
...(reasoning_effort !== undefined ? { reasoning_effort } : {}),
...(api_mode !== undefined ? { api_mode } : {}),
}),
autoContinue: (id: string, enabled: boolean) =>
post<{ ok: boolean; enabled: boolean; max: number }>(`/sessions/${id}/autocontinue`, { enabled }),
reassign: (fromId: string, toId: string, message = "Continue.") =>
post<{ ok: boolean; session_id: string }>("/sessions/reassign", {
from_id: fromId,
to_id: toId,
message,
}),
taskFile: {
get: (id: string) => get<{ content: string; path: string; workspace: string }>(`/sessions/${id}/taskfile`),
save: (id: string, content: string) => put<{ ok: boolean }>(`/sessions/${id}/taskfile`, { content }),
},
// Desk session-lineage history: root session + each resume/model-switch.
history: (id: string) =>
get<DeskHistory>(`/sessions/${id}/history`),
// Full desk export (config + TASK.md + session history) as a JSON document.
export: (id: string) =>
get<DeskExport>(`/sessions/${id}/export`),
// Full-desk archive (.tar.gz of the whole sandbox) — direct download URL so
// the browser streams it to disk with the server's Content-Disposition name.
archiveUrl: (id: string) => `${BASE}/sessions/${id}/archive`,
// Load a desk previously saved via archiveUrl back into the workbench.
importDesk: async (fileBlob: File) => {
const fd = new FormData();
fd.append("file", fileBlob);
const r = await fetch(`${BASE}/sessions/import`, { method: "POST", body: fd });
if (!r.ok) throw new Error(await errorDetail(r));
return r.json() as Promise<{ ok: boolean; session_id: string; workspace_path: string | null; team_id: string | null }>;
},
listSavedDesks: () =>
get<{ dir: string; archives: { filename: string; size: number; modified_at: string }[] }>("/sessions/saved"),
importSavedDesk: (filename: string) =>
post<{ ok: boolean; session_id: string; workspace_path: string | null; team_id: string | null }>(
"/sessions/import-saved", { filename },
),
// Orchestrated evidence-based manager audit (~60s; runs the agent's model).
// Returns the cached audit instantly when state is unchanged unless force=true.
audit: (id: string, force = false) =>
post<AuditResult>(`/sessions/${id}/audit${force ? "?force=true" : ""}`, {}),
auditCached: (id: string) => get<AuditResult>(`/sessions/${id}/audit`),
// Agent progress report (PROGRESS.md). get = read cached; generate = (re)build.
progress: {
get: (id: string) => get<{ content: string; exists: boolean }>(`/sessions/${id}/progress`),
generate: (id: string) => post<{ content: string; exists: boolean }>(`/sessions/${id}/progress`, {}),
},
// Cheap, no-LLM: has the current state already been audited?
auditStatus: (id: string) =>
get<{ current_hash: string; auditable: boolean; audited: boolean;
summary: { passed: number; failed: number; unsure: number; total: number } | null }>(
`/sessions/${id}/audit/status`),
new: (
content: string,
reasoning_effort?: string,
api_mode?: string,
model?: string,
attachments?: { name: string; data: string }[],
tools?: string[],
agent?: string,
team_id?: string,
) =>
post<{ session_id: string; workspace_path?: string; response: string; session?: Session; agent?: string | null }>(
"/sessions/new",
{
content,
...(reasoning_effort !== undefined ? { reasoning_effort } : {}),
...(api_mode ? { api_mode } : {}),
...(model ? { model } : {}),
...(attachments?.length ? { attachments } : {}),
...(tools !== undefined ? { tools } : {}),
...(agent ? { agent } : {}),
...(team_id ? { team_id } : {}),
},
),
sendMessage: async (
id: string,
content: string,
onChunk?: (delta: string) => void,
): Promise<void> => {
const res = await fetch(`${BASE}/sessions/${id}/message`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ content }),
});
if (!res.ok) throw new Error(`${res.status} ${res.statusText}`);
if (!res.body) return;
const reader = res.body.getReader();
const dec = new TextDecoder();
let buf = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
buf += dec.decode(value, { stream: true });
const lines = buf.split("\n");
buf = lines.pop() ?? "";
for (const line of lines) {
if (!line.startsWith("data: ")) continue;
const payload = line.slice(6).trim();
if (payload === "[DONE]") return;
try {
const json = JSON.parse(payload);
const delta: string | undefined = json.choices?.[0]?.delta?.content;
if (delta && onChunk) onChunk(delta);
} catch { /* partial chunk */ }
}
}
},
activityWs: (
id: string,
onEvents: (events: ActivityEvent[]) => void,
onLive?: (evt: WorkerEvent) => void,
onClose?: () => void,
onSubagents?: (records: SubagentRecord[]) => void,
): WebSocket => {
const ws = new WebSocket(`${WS_BASE}/ws/activity/${id}`);
ws.onmessage = (e) => {
try {
const data = JSON.parse(e.data);
if (Array.isArray(data)) {
onEvents(data);
} else if (data.subagents && onSubagents) {
onSubagents(data.subagents as SubagentRecord[]);
} else if (data.live && onLive) {
onLive(data.live as WorkerEvent);
}
} catch { /* ignore */ }
};
ws.onclose = () => onClose?.();
return ws;
},
terminalWs: (id: string, onLine: (line: string) => void): WebSocket => {
const ws = new WebSocket(`${WS_BASE}/ws/terminal/${id}`);
ws.onmessage = (e) => { onLine(String(e.data)); };
return ws;
},
consoleWs: (id: string, onLine: (line: string) => void): WebSocket => {
const ws = new WebSocket(`${WS_BASE}/ws/console/${id}`);
ws.onmessage = (e) => { onLine(String(e.data)); };
return ws;
},
tailWs: (id: string, file: string, onLine: (line: string) => void): WebSocket => {
const ws = new WebSocket(`${WS_BASE}/ws/tail/${id}?file=${encodeURIComponent(file)}`);
ws.onmessage = (e) => { onLine(String(e.data)); };
return ws;
},
},
search: (q: string) => get<Session[]>(`/search?q=${encodeURIComponent(q)}`),
file: {
preview: (path: string) =>
get<FilePreviewData>(`/file/preview?path=${encodeURIComponent(path)}`),
tree: (root: string) =>
get<FileNode[]>(`/file/tree?root=${encodeURIComponent(root)}`),
},
hermes: {
status: () => get<{ available: boolean; [k: string]: unknown }>("/hermes/status"),
warmup: () => post<{ ok: boolean }>("/warmup", {}),
},
llm: {
models: (opts?: { baseUrl?: string; agentId?: string }) => {
const q = new URLSearchParams();
if (opts?.baseUrl) q.set("base_url", opts.baseUrl);
if (opts?.agentId) q.set("agent_id", opts.agentId);
const qs = q.toString();
return get<{ models: string[]; current: string; base_url?: string }>(
`/llm/models${qs ? `?${qs}` : ""}`,
);
},
providers: (opts?: { agentId?: string }) => {
const q = new URLSearchParams();
if (opts?.agentId) q.set("agent_id", opts.agentId);
const qs = q.toString();
return get<{ providers: LlmProvider[]; active: string }>(
`/llm/providers${qs ? `?${qs}` : ""}`,
);
},
},
manager: {
getProfile: () =>
get<{ profile: string; model: string; base_url: string }>("/manager/profile"),
setProfile: (profile: string) =>
post<{ profile: string; model: string }>("/manager/profile", { profile }),
},
/** @deprecated use api.llm.models */
ollama: {
models: (baseUrl?: string, agentId?: string) =>
get<{ models: string[]; current: string }>(
`/llm/models${(() => {
const q = new URLSearchParams();
if (baseUrl) q.set("base_url", baseUrl);
if (agentId) q.set("agent_id", agentId);
const qs = q.toString();
return qs ? `?${qs}` : "";
})()}`,
),
},
agents: {
list: () => get<{ agents: AgentProfile[] }>("/agents"),
prototypes: () => get<{ prototypes: AgentPrototype[] }>("/agents/prototypes"),
capabilities: (id: string) =>
get<AgentCapabilities>(`/agents/${encodeURIComponent(id)}/capabilities`),
persona: (id: string) => get<AgentPersona>(`/agents/${encodeURIComponent(id)}/persona`),
savePersona: (
id: string,
body: { soul: string; memory: string; model_default?: string; base_url?: string; provider?: string },
) =>
put<{ ok: boolean; id: string }>(`/agents/${encodeURIComponent(id)}/persona`, body),
create: (body: {
id: string;
clone_from: string;
name?: string;
tagline?: string;
soul?: string;
memory?: string;
model_default?: string;
base_url?: string;
provider?: string;
}) => post<{ ok: boolean; agent: AgentProfile }>("/agents", body),
delete: (id: string) =>
del<{ ok: boolean; id: string }>(`/agents/${encodeURIComponent(id)}`),
},
teams: {
files: (teamId: string) =>
get<{ files: FileNode[]; root: string }>(`/teams/${encodeURIComponent(teamId)}/files`),
upload: (teamId: string, path: string, data: string) =>
post<{ ok: boolean; path: string; synced_desks: number }>(
`/teams/${encodeURIComponent(teamId)}/files`, { path, data }),
delete: (teamId: string, path: string) =>
del<{ ok: boolean }>(`/teams/${encodeURIComponent(teamId)}/files?path=${encodeURIComponent(path)}`),
sync: (teamId: string) =>
post<{ ok: boolean; synced_desks: number }>(`/teams/${encodeURIComponent(teamId)}/sync`, {}),
register: (teamId: string, sessionIds: string[]) =>
post<{ ok: boolean; registered: number }>(
`/teams/${encodeURIComponent(teamId)}/register`, { session_ids: sessionIds }),
},
guiConfig: () =>
get<{
agent_profiles_dir: string;
desk_default_model: string | null;
agents: AgentProfile[];
prototypes: AgentPrototype[];
global?: { base_url: string; model: string };
manager: { base_url: string; model: string; uses_effective_agent_model?: boolean };
}>("/gui-config"),
globalPersona: {
get: () =>
get<{ id: string; profile_path: string; model: string; base_url: string; soul: string; memory: string }>(
"/global/persona",
),
save: (body: { soul?: string; memory?: string; model_default?: string; base_url?: string; provider?: string }) =>
put<{ ok: boolean }>("/global/persona", body),
},
models: {
// Reasoning-effort options the given model + backend supports (empty = gray out).
reasoning: (model?: string, opts?: { baseUrl?: string; agentId?: string }) => {
const q = new URLSearchParams();
if (model) q.set("model", model);
if (opts?.baseUrl) q.set("base_url", opts.baseUrl);
if (opts?.agentId) q.set("agent_id", opts.agentId);
const qs = q.toString();
return get<{ options: { value: string; label: string }[] }>(
`/models/reasoning${qs ? `?${qs}` : ""}`);
},
},
toolsets: () =>
get<{
toolsets: { name: string; label: string; lean: boolean }[];
presets: { chat: string[]; lean: string[]; full: string[] };
default: string;
}>("/toolsets"),
workspace: {
open: (path: string) => post<{ ok: boolean }>("/workspace/open", { path }),
openTerminal: (path: string) => post<{ ok: boolean }>("/workspace/open-terminal", { path }),
},
docker: {
cleanup: () =>
post<{ removed: number; kept: number; skipped: boolean; reason?: string }>(
"/docker/cleanup",
{},
),
getConfig: () => get<{ persist: boolean }>("/docker/config"),
setConfig: (persist: boolean) =>
post<{ persist: boolean }>("/docker/config", { persist }),
},
};
|