Accept story 30: Worktree-based agent orchestration
Add git worktree isolation for concurrent story agents. Each agent now runs in its own worktree with setup/teardown commands driven by .story_kit/project.toml config. Agents stream output via SSE and support start/stop lifecycle with Pending/Running/Completed/Failed statuses. Backend: config.rs (TOML parsing), worktree.rs (git worktree lifecycle), refactored agents.rs (broadcast streaming), agents_sse.rs (SSE endpoint). Frontend: AgentPanel.tsx with Run/Stop buttons and streaming output log. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
115
frontend/src/api/agents.ts
Normal file
115
frontend/src/api/agents.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
export type AgentStatusValue = "pending" | "running" | "completed" | "failed";
|
||||
|
||||
export interface AgentInfo {
|
||||
story_id: string;
|
||||
status: AgentStatusValue;
|
||||
session_id: string | null;
|
||||
worktree_path: string | null;
|
||||
}
|
||||
|
||||
export interface AgentEvent {
|
||||
type: "status" | "output" | "agent_json" | "done" | "error" | "warning";
|
||||
story_id?: string;
|
||||
status?: string;
|
||||
text?: string;
|
||||
data?: unknown;
|
||||
session_id?: string | null;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
const DEFAULT_API_BASE = "/api";
|
||||
|
||||
function buildApiUrl(path: string, baseUrl = DEFAULT_API_BASE): string {
|
||||
return `${baseUrl}${path}`;
|
||||
}
|
||||
|
||||
async function requestJson<T>(
|
||||
path: string,
|
||||
options: RequestInit = {},
|
||||
baseUrl = DEFAULT_API_BASE,
|
||||
): Promise<T> {
|
||||
const res = await fetch(buildApiUrl(path, baseUrl), {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...(options.headers ?? {}),
|
||||
},
|
||||
...options,
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const text = await res.text();
|
||||
throw new Error(text || `Request failed (${res.status})`);
|
||||
}
|
||||
|
||||
return res.json() as Promise<T>;
|
||||
}
|
||||
|
||||
export const agentsApi = {
|
||||
startAgent(storyId: string, baseUrl?: string) {
|
||||
return requestJson<AgentInfo>(
|
||||
"/agents/start",
|
||||
{
|
||||
method: "POST",
|
||||
body: JSON.stringify({ story_id: storyId }),
|
||||
},
|
||||
baseUrl,
|
||||
);
|
||||
},
|
||||
|
||||
stopAgent(storyId: string, baseUrl?: string) {
|
||||
return requestJson<boolean>(
|
||||
"/agents/stop",
|
||||
{
|
||||
method: "POST",
|
||||
body: JSON.stringify({ story_id: storyId }),
|
||||
},
|
||||
baseUrl,
|
||||
);
|
||||
},
|
||||
|
||||
listAgents(baseUrl?: string) {
|
||||
return requestJson<AgentInfo[]>("/agents", {}, baseUrl);
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Subscribe to SSE events for a running agent.
|
||||
* Returns a cleanup function to close the connection.
|
||||
*/
|
||||
export function subscribeAgentStream(
|
||||
storyId: string,
|
||||
onEvent: (event: AgentEvent) => void,
|
||||
onError?: (error: Event) => void,
|
||||
): () => void {
|
||||
const host = import.meta.env.DEV ? "http://127.0.0.1:3001" : "";
|
||||
const url = `${host}/agents/${encodeURIComponent(storyId)}/stream`;
|
||||
|
||||
const eventSource = new EventSource(url);
|
||||
|
||||
eventSource.onmessage = (e) => {
|
||||
try {
|
||||
const data = JSON.parse(e.data) as AgentEvent;
|
||||
onEvent(data);
|
||||
|
||||
// Close on terminal events
|
||||
if (
|
||||
data.type === "done" ||
|
||||
data.type === "error" ||
|
||||
(data.type === "status" && data.status === "stopped")
|
||||
) {
|
||||
eventSource.close();
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Failed to parse agent event:", err);
|
||||
}
|
||||
};
|
||||
|
||||
eventSource.onerror = (e) => {
|
||||
onError?.(e);
|
||||
eventSource.close();
|
||||
};
|
||||
|
||||
return () => {
|
||||
eventSource.close();
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user