/**
 * Public LLM-tools API. Thin layer over the preset registry — every call here
 * resolves a preset (defaulting to `legacy` for backwards compat) and delegates
 * to it.
 *
 * Presets are the unit of swapping. To add a new tool surface (e.g. handwritten
 * "core" tools, prompt-caching variant, lazy-load experiment), register a new
 * descriptor in `presets.ts` — no changes here required.
 */
import { DEFAULT_PRESET, getPreset, listPresets, } from './presets.js';
export { DEFAULT_PRESET, getPreset, listPresets };
/**
 * Select tools for a specific provider from a preset.
 *
 * @example
 * ```ts
 * // Default — legacy preset, no cache markers.
 * const { tools, meta } = await chooseTools({ provider: 'vercel' });
 *
 * // Anthropic — last tool gets cache_control automatically.
 * const { tools, meta } = await chooseTools({ provider: 'anthropic', cache: true });
 *
 * // Pick a specific preset by ID.
 * const { tools, meta } = await chooseTools({ provider: 'openai', preset: 'legacy' });
 * ```
 */
export async function chooseTools(input) {
    const presetId = input.preset ?? DEFAULT_PRESET;
    const preset = getPreset(presetId);
    const { tools, cacheStrategy } = await preset.getTools(input.provider, {
        cache: input.cache === true,
    });
    return {
        tools,
        meta: {
            provider: input.provider,
            preset: presetId,
            toolCount: tools.length,
            cacheStrategy,
        },
    };
}
// ---------------------------------------------------------------------------
// Catalog + listings (preset-scoped; default to legacy)
// ---------------------------------------------------------------------------
/** Return the full tool catalog for a preset (default: legacy). */
export async function getToolCatalog(preset) {
    return getPreset(preset ?? DEFAULT_PRESET).getCatalog();
}
/**
 * Return the raw tool array for a provider from a preset (default: legacy).
 *
 * No cache markers are applied. Use {@link chooseTools} when you need cache
 * markers and metadata.
 */
export async function listTools(provider, preset) {
    const { tools } = await getPreset(preset ?? DEFAULT_PRESET).getTools(provider, { cache: false });
    return tools;
}
// ---------------------------------------------------------------------------
// Dispatch
// ---------------------------------------------------------------------------
/**
 * Dispatch a tool call against a bound document handle using the default
 * preset (`legacy`).
 *
 * The document handle injects session targeting automatically; tool arguments
 * should not contain `doc` or `sessionId`.
 *
 * For preset-aware dispatch — e.g. when comparing two presets — call
 * `getPreset('id').dispatch(...)` directly.
 */
export async function dispatchSuperDocTool(documentHandle, toolName, args = {}, invokeOptions) {
    return getPreset(DEFAULT_PRESET).dispatch(documentHandle, toolName, args, invokeOptions);
}
// ---------------------------------------------------------------------------
// System prompts (preset-scoped; default to legacy)
// ---------------------------------------------------------------------------
/**
 * Read the packaged SDK system prompt (default preset: legacy).
 *
 * Includes a persona preamble ("You are a document editing assistant…")
 * suitable for embedded LLM usage (OpenAI, Anthropic, Vercel APIs). For MCP
 * server instructions, use {@link getMcpPrompt} instead.
 */
export async function getSystemPrompt(preset) {
    return getPreset(preset ?? DEFAULT_PRESET).getSystemPrompt();
}
/**
 * Read the packaged MCP system prompt for intent tools (default preset: legacy).
 *
 * Omits the persona preamble and includes session lifecycle instructions
 * (open/save/close) suitable for MCP server `instructions`.
 */
export async function getMcpPrompt(preset) {
    return getPreset(preset ?? DEFAULT_PRESET).getMcpPrompt();
}
/**
 * Get the system prompt formatted for a specific LLM provider, with optional
 * prompt caching applied.
 *
 * - **anthropic** with `cache: true`: returns a content array with
 *   `cache_control: { type: "ephemeral" }` so the system prompt block is
 *   cached. Pass directly as the `system` parameter on `messages.create()`.
 * - **openai**: returns the prompt as a string. OpenAI caches prompts
 *   ≥ 1024 tokens automatically — `cache: true` is informational only and
 *   sets `cacheStrategy: 'automatic'`.
 * - **vercel** / **generic**: returns the prompt as a string. Caching is
 *   delegated to the underlying model.
 *
 * @example
 * ```ts
 * // Anthropic
 * const sys = await getSystemPromptForProvider({ provider: 'anthropic', cache: true });
 * await client.messages.create({ system: sys.content, tools, messages, model });
 *
 * // OpenAI
 * const sys = await getSystemPromptForProvider({ provider: 'openai', cache: true });
 * messages.unshift({ role: 'system', content: sys.content });
 * ```
 */
export async function getSystemPromptForProvider(input) {
    const text = await getSystemPrompt(input.preset);
    const cacheRequested = input.cache === true;
    if (input.provider === 'anthropic') {
        const block = { type: 'text', text };
        if (cacheRequested)
            block.cache_control = { type: 'ephemeral' };
        return {
            provider: 'anthropic',
            content: [block],
            cacheStrategy: cacheRequested ? 'explicit' : 'disabled',
        };
    }
    const cacheStrategy = !cacheRequested
        ? 'disabled'
        : input.provider === 'openai'
            ? 'automatic'
            : 'unsupported';
    return { provider: input.provider, content: text, cacheStrategy };
}
