/**
 * Agent RPC Method Map — complete typed contract for ALL agent JSON-RPC methods.
 *
 * This extends GatewayRpcMethodMap (26 methods Gateway directly calls) with all
 * additional agent-specific methods (thread, memory, config, agents, solo, product, etc.).
 *
 * Consumers use this for fully typed RPC clients:
 *   import type { AgentRpcMethodMap } from "qlogicagent/protocol";
 *   async function call<M extends keyof AgentRpcMethodMap>(method: M, params: AgentRpcMethodMap[M]["params"]): Promise<AgentRpcMethodMap[M]["result"]> { ... }
 */
import type { ChatMessage, LocalizedToolText, ToolCapabilityCategory } from "./chat-types.js";
import type { GatewayRpcMethodMap } from "./gateway-rpc.js";
import type { WireTokenUsage } from "./notification-payloads.js";
export type AgentCategory = "sub-agent" | "teammate";
export type AgentProtocol = "internal" | "acp";
export type AgentStatus = "available" | "unavailable" | "handshake_failed" | "not_installed";
export interface AgentCapabilities {
    supportsMcp: boolean;
    supportsResume: boolean;
    supportsUsageUpdate: boolean;
    skillsDirs?: string[];
}
/** Descriptor for a discovered or registered agent. */
export interface AgentDescriptor {
    id: string;
    name: string;
    category: AgentCategory;
    protocol: AgentProtocol;
    status: AgentStatus;
    cliPath?: string;
    version?: string;
    capabilities?: AgentCapabilities;
    authRequired: boolean;
    hasConfig: boolean;
    supportsBaseUrlOverride: boolean;
}
/** User-provided custom agent definition. */
export interface CustomAgentDef {
    id: string;
    name: string;
    cliCommand: string;
    acpArgs: string[];
    env?: Record<string, string>;
    authRequired?: boolean;
    skillsDirs?: string[];
    supportsBaseUrlOverride?: boolean;
}
/** Persisted per-agent configuration. */
export interface AgentConfig {
    apiKey?: string;
    baseUrl?: string;
    model?: string;
    env?: Record<string, string>;
    customArgs?: string[];
    customCliPath?: string;
    modelIdMap?: Record<string, string>;
}
export type SoloState = "running" | "evaluating" | "completed" | "cancelled" | "failed";
export type SoloAgentState = "pending" | "running" | "completed" | "failed" | "idle";
export interface SoloAgentResult {
    id: string;
    state: SoloAgentState;
    progress?: string;
    resultText?: string;
    diff?: string;
    usage?: {
        inputTokens: number;
        outputTokens: number;
    };
    error?: string;
    worktreePath?: string;
}
export interface SoloStatus {
    soloId: string;
    state: SoloState;
    agents: SoloAgentResult[];
    evaluation?: {
        winnerId: string;
        reasoning: string;
    };
}
export type ProductPhase = "active" | "paused" | "completed" | "failed";
export type ProductTaskStatus = "pending" | "running" | "completed" | "failed" | "paused" | "skipped";
export interface ProductInstanceDef {
    name: string;
    role: string;
    agentId: string;
}
export interface ProductTaskDef {
    taskId: string;
    assignee: string;
    prompt: string;
    dependsOn?: string[];
}
export interface ProductStatus {
    productId: string;
    name: string;
    phase: ProductPhase;
    instances: Array<{
        instanceId: string;
        name: string;
        agentId: string;
        state: string;
        worktreePath?: string;
        usage?: {
            inputTokens: number;
            outputTokens: number;
        };
    }>;
    tasks: Array<{
        taskId: string;
        assignee: string;
        status: ProductTaskStatus;
        result?: string;
        error?: string;
        startedAt?: string;
        completedAt?: string;
    }>;
    budget: {
        maxTotalTokens?: number;
        maxDuration?: number;
        usedTokens: number;
        elapsed: number;
    };
    lastCheckpointAt: string;
}
export interface ProductSummary {
    productId: string;
    name: string;
    phase: ProductPhase;
    instanceCount: number;
    taskCount: number;
    completedTasks: number;
    createdAt: string;
    lastCheckpointAt: string;
}
/**
 * All agent RPC methods — superset of GatewayRpcMethodMap.
 *
 * Methods from GatewayRpcMethodMap (session.*, project.*, files.*, instructions.*)
 * are inherited. This adds the agent-specific protocol methods.
 */
export interface AgentRpcMethodMap extends GatewayRpcMethodMap {
    "initialize": {
        params: {
            protocolVersion: string;
            host?: {
                name: string;
                version: string;
            };
            hostName?: string;
            hostVersion?: string;
            capabilities?: {
                streaming?: boolean;
                toolApproval?: boolean;
                threads?: boolean;
            };
            config?: Record<string, unknown>;
        };
        result: {
            protocolVersion: string;
            agent: {
                name: string;
                version: string;
            };
            capabilities: {
                tools: string[];
                streaming: boolean;
                threads: boolean;
                notifications: string[];
                methods: string[];
            };
        };
    };
    "thread.create": {
        params: {
            id?: string;
            title?: string;
            cwd?: string;
            config?: Record<string, unknown>;
        };
        result: {
            id: string;
            title?: string;
            status: "active";
            createdAt: string;
        };
    };
    "thread.list": {
        params: {
            limit?: number;
        };
        result: {
            threads: Array<{
                id: string;
                title?: string;
                status: "active" | "archived";
                turnCount?: number;
                createdAt: string;
                lastActiveAt: string;
            }>;
        };
    };
    "session.resume": {
        params: {
            sessionId: string;
        };
        result: {
            metadata: {
                sessionId: string;
                projectId: string;
                createdAt: string;
                lastActiveAt: string;
                model?: string;
                cwd?: string;
                turnCount: number;
                messageCount: number;
                title?: string;
                totalInputTokens?: number;
                totalOutputTokens?: number;
            };
            messages: ChatMessage[];
        };
    };
    "session.getInfo": {
        params: {
            sessionId?: string;
        };
        result: {
            sessionId: string;
            paths: {
                sessionDir: string;
                transcript: string;
                state: string;
                agentHome: string;
                settings: string;
                cwd: string;
            };
            model?: string;
            provider?: string;
            turnCount: number;
            title?: string;
            usage?: {
                totalPromptTokens: number;
                totalCompletionTokens: number;
                totalCost?: number;
            };
        };
    };
    "memory.list": {
        params: {
            source?: "local" | "store" | "qmemory" | "all";
        };
        result: {
            entries: Array<{
                source: "local" | "store" | "qmemory";
                target?: string;
                path?: string;
                content?: string;
                lines?: number;
            }>;
        };
    };
    "memory.read": {
        params: {
            source?: string;
            target?: string;
            path?: string;
        };
        result: {
            content: string;
            source: string;
            path?: string;
        };
    };
    "memory.write": {
        params: {
            source?: string;
            target?: string;
            path?: string;
            content: string;
            mode?: "overwrite" | "append";
        };
        result: {
            ok: boolean;
            path?: string;
        };
    };
    "memory.search": {
        params: {
            query: string;
            source?: "local" | "qmemory" | "all";
            limit?: number;
        };
        result: {
            results: Array<{
                source: string;
                target?: string;
                content: string;
                score?: number;
                path?: string;
            }>;
        };
    };
    "memory.delete": {
        params: {
            source?: string;
            target?: string;
            path?: string;
        };
        result: {
            ok: boolean;
            deleted?: number;
        };
    };
    "memory.dream": {
        params: {
            scope?: "session" | "project" | "all";
        };
        result: {
            ok: boolean;
            consolidated: number;
            archived: number;
            durationMs: number;
        };
    };
    "tools.list": {
        params: {
            category?: ToolCapabilityCategory;
            includeDeferred?: boolean;
        };
        result: {
            tools: Array<{
                id: string;
                name: string;
                category: ToolCapabilityCategory;
                displayName: LocalizedToolText;
                displayDescription: LocalizedToolText;
                fallbackName: string;
                fallbackDescription: string;
                description: string;
                source: "builtin";
                deferred: boolean;
                parameters?: Record<string, unknown>;
            }>;
            total: number;
        };
    };
    "media.listModels": {
        params: {
            mediaType?: string;
        };
        result: {
            models: Array<{
                id: string;
                name: string;
                mediaType: string;
                provider: string;
                capabilities?: Record<string, unknown>;
            }>;
        };
    };
    "config.get": {
        params: {
            keys?: string[];
        };
        result: {
            config: Record<string, unknown>;
        };
    };
    "config.update": {
        params: {
            updates: Record<string, unknown>;
        };
        result: {
            ok: boolean;
        };
    };
    "todos.list": {
        params: {
            sessionId?: string;
        };
        result: {
            items: Array<{
                id: number;
                title: string;
                status: string;
                owner?: string;
                blockedBy?: number[];
            }>;
            summary: string;
        };
    };
    "tasks.list": {
        params: {
            status?: string;
        };
        result: {
            tasks: Array<{
                id: string;
                type: string;
                status: string;
                progress?: number;
                createdAt: string;
            }>;
        };
    };
    "tasks.cancel": {
        params: {
            taskId: string;
        };
        result: {
            ok: boolean;
        };
    };
    "agent.ping": {
        params: undefined;
        result: {
            status: "idle" | "busy";
            uptime: number;
            version: string;
            usage?: WireTokenUsage;
        };
    };
    "agents.scan": {
        params: {
            force?: boolean;
        };
        result: AgentDescriptor[];
    };
    "agents.list": {
        params: undefined;
        result: AgentDescriptor[];
    };
    "agents.config": {
        params: {
            action: "register" | "unregister" | "update";
            agent: CustomAgentDef;
        };
        result: {
            ok: true;
        };
    };
    "agents.setConfig": {
        params: {
            agentId: string;
            apiKey?: string;
            baseUrl?: string;
            model?: string;
            env?: Record<string, string>;
            customArgs?: string[];
            customCliPath?: string;
            modelIdMap?: Record<string, string>;
        };
        result: {
            ok: true;
        };
    };
    "agents.getConfig": {
        params: {
            agentId: string;
        };
        result: AgentConfig | null;
    };
    "agents.removeConfig": {
        params: {
            agentId: string;
        };
        result: {
            ok: true;
        };
    };
    "agents.setGateway": {
        params: {
            url: string;
        };
        result: {
            ok: true;
        };
    };
    "solo.start": {
        params: {
            task: string;
            agents: string[];
            cwd: string;
        };
        result: {
            soloId: string;
        };
    };
    "solo.status": {
        params: {
            soloId: string;
        };
        result: SoloStatus | null;
    };
    "solo.cancel": {
        params: {
            soloId: string;
        };
        result: {
            ok: true;
        };
    };
    "solo.select": {
        params: {
            soloId: string;
            winnerId: string;
        };
        result: {
            ok: true;
            mergedBranch: string;
        };
    };
    "product.create": {
        params: {
            name: string;
            cwd: string;
            instances: ProductInstanceDef[];
            tasks: ProductTaskDef[];
            budget?: {
                maxTotalTokens?: number;
                maxDuration?: number;
            };
        };
        result: {
            productId: string;
        };
    };
    "product.resume": {
        params: {
            productId: string;
        };
        result: {
            ok: true;
        };
    };
    "product.pause": {
        params: {
            productId: string;
        };
        result: {
            ok: true;
        };
    };
    "product.checkpoint": {
        params: {
            productId: string;
        };
        result: {
            ok: true;
        };
    };
    "product.status": {
        params: {
            productId: string;
        };
        result: ProductStatus | null;
    };
    "product.list": {
        params: undefined;
        result: ProductSummary[];
    };
}
