/**
 * Core operations module.
 *
 * Provides a stable, typed API surface for calling Neotoma's core actions
 * (store, retrieve, create_relationship, etc.) without going through the
 * MCP JSON-RPC protocol. This is the foundation used by:
 *   - `@neotoma/client` (local transport)
 *   - hook plugins for Claude Code, Cursor, OpenCode, Codex
 *   - Claude Agent SDK adapter
 *
 * Design:
 *   - Wraps the existing `NeotomaServer.executeToolForCli` dispatcher so we
 *     reuse every business rule, idempotency check, schema validation, and
 *     provenance construction already implemented there.
 *   - Parses the MCP-standard `{ content: [{ type, text }] }` response into
 *     typed return values.
 *   - Exposes a `createOperations({ userId })` factory so each caller gets
 *     a bound instance with its authenticated user.
 *
 * See `docs/developer/mcp/instructions.md` for the semantic contract that
 * agents follow when storing and retrieving through these operations.
 */
import { NeotomaServer } from "../server.js";
function parseToolResult(raw) {
    const first = raw.content?.[0];
    if (!first || first.type !== "text") {
        return raw;
    }
    const text = first.text;
    try {
        return JSON.parse(text);
    }
    catch {
        return text;
    }
}
/**
 * Create a bound operations client for a given user.
 *
 * The caller is responsible for providing `userId` — this mirrors the
 * existing `executeToolForCli` contract and lets the same process serve
 * multiple users (e.g. API server mode). For single-user local mode use
 * the local dev user id from `services/local_auth.ts`.
 */
export function createOperations(options) {
    const ownsServer = !options.server;
    const server = options.server ?? new NeotomaServer();
    const userId = options.userId;
    async function call(name, args = {}) {
        const raw = await server.executeToolForCli(name, args, userId);
        return parseToolResult(raw);
    }
    const ops = {
        async store(input) {
            return call("store", input);
        },
        async storeStructured(input) {
            return call("store", input);
        },
        async storeUnstructured(input) {
            return call("store", input);
        },
        async retrieveEntities(input) {
            return call("retrieve_entities", input);
        },
        async retrieveEntityByIdentifier(input) {
            return call("retrieve_entity_by_identifier", input);
        },
        async retrieveEntitySnapshot(input) {
            return call("retrieve_entity_snapshot", input);
        },
        async listObservations(input) {
            return call("list_observations", input);
        },
        async listTimelineEvents(input) {
            return call("list_timeline_events", input);
        },
        async retrieveRelatedEntities(input) {
            return call("retrieve_related_entities", input);
        },
        async createRelationship(input) {
            return call("create_relationship", input);
        },
        async createRelationships(input) {
            return call("create_relationships", input);
        },
        async correct(input) {
            return call("correct", input);
        },
        async listEntityTypes(input = {}) {
            return call("list_entity_types", input);
        },
        async getEntityTypeCounts(input = {}) {
            return call("get_entity_type_counts", input);
        },
        async executeTool(name, args) {
            return call(name, args);
        },
        async dispose() {
            if (ownsServer) {
                // NeotomaServer currently has no explicit shutdown hook beyond stdio lifecycle;
                // the server instance will be garbage collected. This method exists to give the
                // client library a stable lifecycle point to hook into as that changes.
            }
        },
    };
    return ops;
}
//# sourceMappingURL=operations.js.map