/**
 * DecisionGraphClient — Agent-Facing Typed Wrapper (ADR-033)
 *
 * Thin wrapper that provides the Read/Write/Update protocol for agents.
 * Agents import this client rather than interacting with raw DecisionGraph
 * or AgentDB memory APIs.
 *
 * The client:
 *   - Enforces schema compliance (producedBy, derivedFrom mandatory)
 *   - Auto-populates tags, timestamps, versions
 *   - Provides typed query builders for common patterns
 *   - Bridges to AgentDB memory sync when available
 *
 * Per ADR-033: "A thin DecisionGraphClient wrapper provides typed
 * read/write methods — agents import the client, not raw memory APIs."
 */
// ============================================================================
// DecisionGraphClient
// ============================================================================
export class DecisionGraphClient {
    graph;
    agentRef;
    pass;
    /**
     * Create a client bound to a specific agent and pass.
     *
     * All writes through this client will automatically set producedBy
     * and pass fields. Agents don't need to repeat themselves.
     *
     * @param graph - The underlying DecisionGraph instance.
     * @param agentRef - The calling agent's identity.
     * @param pass - The pipeline pass this agent is executing in.
     */
    constructor(graph, agentRef, pass) {
        this.graph = graph;
        this.agentRef = agentRef;
        this.pass = pass;
    }
    // --------------------------------------------------------------------------
    // Write Protocol (ADR-033)
    // --------------------------------------------------------------------------
    /**
     * Write a new node to the graph.
     * Auto-populates producedBy, pass, and standard tags.
     */
    write(params) {
        return this.graph.createNode({
            ...params,
            producedBy: params.producedBy ?? this.agentRef,
            pass: params.pass ?? this.pass,
        });
    }
    /**
     * Write multiple nodes in a batch. All nodes use the same agent/pass context.
     * Returns all created nodes.
     */
    writeBatch(paramsList) {
        return paramsList.map(params => this.write(params));
    }
    // --------------------------------------------------------------------------
    // Update Protocol (ADR-033 — append-only history)
    // --------------------------------------------------------------------------
    /**
     * Update an existing node.
     * Original content is preserved in version history.
     * Increments version automatically.
     */
    update(nodeId, updates) {
        return this.graph.updateNode(nodeId, updates);
    }
    // --------------------------------------------------------------------------
    // Read Protocol (ADR-033)
    // --------------------------------------------------------------------------
    /** Read a single node by ID. */
    read(nodeId) {
        const node = this.graph.getNode(nodeId);
        return node ? { found: true, node } : { found: false };
    }
    /** Read all nodes of a specific type. */
    readByType(type) {
        return this.graph.getNodesByType(type);
    }
    /** Read all nodes from a specific pass. */
    readByPass(pass) {
        return this.graph.getNodesByPass(pass);
    }
    /** Read all nodes from prior passes (useful for building context). */
    readPriorPasses() {
        const results = [];
        for (let p = 1; p < this.pass; p++) {
            const nodes = this.graph.getNodesByPass(p);
            for (const node of nodes) {
                results.push(node);
            }
        }
        return results;
    }
    /** Read all nodes produced by a specific agent. */
    readByAgent(domain, agent) {
        return this.graph.getNodesByAgent(domain, agent);
    }
    /** Read nodes matching a tag. */
    readByTag(tag) {
        return this.graph.getNodesByTag(tag);
    }
    // --------------------------------------------------------------------------
    // Advanced Querying
    // --------------------------------------------------------------------------
    /**
     * Run a compound query with optional pagination.
     * All filter fields are ANDed together.
     */
    query(filter, pagination) {
        const result = this.graph.queryNodes(filter, pagination);
        return {
            nodes: result.items,
            total: result.total,
            hasMore: result.hasMore,
        };
    }
    /**
     * Search nodes by text similarity on summary fields.
     * Returns top-K most similar nodes.
     */
    search(query, topK) {
        return this.graph.searchBySummary(query, topK);
    }
    /** Get the full derivation chain (all ancestors) for a node. */
    getDerivationChain(nodeId) {
        return this.graph.getDerivationChain(nodeId);
    }
    /** Get the immediate children of a node. */
    getChildren(nodeId) {
        return this.graph.getChildren(nodeId);
    }
    /** Get the version history for a node. */
    getHistory(nodeId) {
        return this.graph.getVersionHistory(nodeId);
    }
    /** Get a specific historical version of a node's content. */
    getAtVersion(nodeId, version) {
        return this.graph.getNodeAtVersion(nodeId, version);
    }
    // --------------------------------------------------------------------------
    // Edge Operations
    // --------------------------------------------------------------------------
    /** Add a relationship edge between two nodes. */
    addEdge(from, to, relation) {
        this.graph.addEdge(from, to, relation);
    }
    /** Get all edges originating from a node. */
    getEdgesFrom(nodeId) {
        return this.graph.getEdgesFrom(nodeId);
    }
    /** Get all edges pointing to a node. */
    getEdgesTo(nodeId) {
        return this.graph.getEdgesTo(nodeId);
    }
    // --------------------------------------------------------------------------
    // Graph Information
    // --------------------------------------------------------------------------
    /** Get aggregate statistics for the graph. */
    getStatistics() {
        return this.graph.getStatistics();
    }
    /** Get the current lifecycle state. */
    getLifecycle() {
        return this.graph.getLifecycle();
    }
    /** Get total node count. */
    getNodeCount() {
        return this.graph.nodeCount;
    }
    /** Get the execution ID. */
    getExecutionId() {
        return this.graph.getExecutionId();
    }
    // --------------------------------------------------------------------------
    // Convenience: Context Summaries for Downstream Agents
    // --------------------------------------------------------------------------
    /**
     * Get a compact summary of all prior pass outputs.
     * Useful for building the context payload sent to agents.
     */
    getPriorPassSummaries() {
        return this.readPriorPasses().map(node => ({
            type: node.type,
            name: node.name,
            summary: node.summary,
            pass: node.pass,
            confidence: node.confidence,
            status: node.status,
        }));
    }
    /**
     * Get all nodes of specific types from specific passes.
     * Common pattern: "give me all scenarios from Pass 2 and all architectures from Pass 3".
     */
    readMultiple(...selectors) {
        const results = [];
        for (const sel of selectors) {
            const nodes = this.graph.getNodesByType(sel.type);
            for (const node of nodes) {
                if (sel.pass === undefined || node.pass === sel.pass) {
                    results.push(node);
                }
            }
        }
        return results;
    }
}
// ============================================================================
// Factory
// ============================================================================
/**
 * Create a DecisionGraphClient for a specific agent in a specific pass.
 * This is the recommended entry point for agents.
 */
export function createDecisionGraphClient(graph, agentRef, pass) {
    return new DecisionGraphClient(graph, agentRef, pass);
}
//# sourceMappingURL=decision-graph-client.js.map