/**
 * agent-tower 后端 API HTTP 客户端
 * MCP 服务器通过此客户端代理调用后端 REST API
 */
export class AgentTowerClient {
    baseUrl;
    constructor(baseUrl) {
        this.baseUrl = baseUrl;
    }
    url(path) {
        return `${this.baseUrl.replace(/\/+$/, '')}/${path.replace(/^\/+/, '')}`;
    }
    async request(method, path, body, query) {
        let url = this.url(path);
        if (query) {
            const params = new URLSearchParams(query);
            url += `?${params.toString()}`;
        }
        const resp = await fetch(url, {
            method,
            headers: body ? { 'Content-Type': 'application/json' } : undefined,
            body: body ? JSON.stringify(body) : undefined,
        });
        if (!resp.ok) {
            const text = await resp.text().catch(() => '');
            throw new Error(`API ${method} ${path} failed (${resp.status}): ${text}`);
        }
        // 204 No Content
        if (resp.status === 204)
            return undefined;
        return resp.json();
    }
    // ── Projects ──
    async listProjects(params) {
        const query = {};
        if (params?.page)
            query.page = String(params.page);
        if (params?.limit)
            query.limit = String(params.limit);
        return this.request('GET', '/api/projects', undefined, query);
    }
    // ── Tasks ──
    async listTasks(projectId, params) {
        const query = {};
        if (params?.status)
            query.status = params.status;
        if (params?.limit)
            query.limit = String(params.limit);
        if (params?.page)
            query.page = String(params.page);
        return this.request('GET', `/api/projects/${projectId}/tasks`, undefined, query);
    }
    async createTask(projectId, input) {
        return this.request('POST', `/api/projects/${projectId}/tasks`, input);
    }
    async getTask(taskId) {
        return this.request('GET', `/api/tasks/${taskId}`);
    }
    async updateTask(taskId, input) {
        return this.request('PUT', `/api/tasks/${taskId}`, input);
    }
    async updateTaskStatus(taskId, status) {
        return this.request('PATCH', `/api/tasks/${taskId}/status`, { status });
    }
    async deleteTask(taskId) {
        return this.request('DELETE', `/api/tasks/${taskId}`);
    }
    // ── Workspaces ──
    async createWorkspace(taskId, branchName) {
        return this.request('POST', `/api/tasks/${taskId}/workspaces`, branchName ? { branchName } : {});
    }
    async getWorkspaceDiff(workspaceId) {
        return this.request('GET', `/api/workspaces/${workspaceId}/diff`);
    }
    async mergeWorkspace(workspaceId) {
        return this.request('POST', `/api/workspaces/${workspaceId}/merge`);
    }
    // ── Providers ──
    async listProviders() {
        return this.request('GET', '/api/providers');
    }
    // ── Sessions ──
    async createSession(workspaceId, prompt, providerId) {
        return this.request('POST', `/api/workspaces/${workspaceId}/sessions`, {
            prompt,
            providerId,
        });
    }
    async startSession(sessionId) {
        return this.request('POST', `/api/sessions/${sessionId}/start`);
    }
    async stopSession(sessionId) {
        return this.request('POST', `/api/sessions/${sessionId}/stop`);
    }
    async sendMessage(sessionId, message) {
        return this.request('POST', `/api/sessions/${sessionId}/message`, { message });
    }
    // ── System ──
    async getWorkspaceContext(cwdPath) {
        return this.request('GET', '/api/system/workspace-context', undefined, { path: cwdPath });
    }
}
//# sourceMappingURL=http-client.js.map