import type { AuthFlowStatus, AuthorizeOpts, BaseToolProviderOptions, ListConnectionsOpts, ListConnectionsResult, ListToolsOpts, ListToolsResult, ResolveToolsOpts, ToolProviderCapabilities, ToolProviderHealth, ToolProviderInfo, ToolProviderToolkit } from '@mastra/core/tool-provider';
import { BaseToolProvider } from '@mastra/core/tool-provider';
import type { ToolAction } from '@mastra/core/tools';
import type { AnyCorsairInstance } from 'corsair';
/**
 * A Corsair plugin's connection state for one tenant, as reported by
 * `corsair.manage.connectionStatus`.
 */
type PluginConnectionState = 'connected' | 'missing_credentials' | 'not_connected';
/**
 * Context passed to a {@link CorsairToolProviderConfig.tenantId} resolver
 * function so it can map a Mastra request to a Corsair tenant.
 */
export type TenantResolveInput = {
    /** The connection bucket Mastra is resolving, if any (decodes to a tenant). */
    connectionId?: string;
    /** The agent author's id, when the runtime provides one. */
    authorId?: string;
    /** The toolkit (Corsair plugin) slug being resolved. */
    toolkit?: string;
    /**
     * Mastra's live per-request context. Use its typed accessors to derive the
     * tenant for `caller-supplied` (multi-tenant) deployments.
     */
    requestContext?: unknown;
};
type CorsairRootInstance = Extract<AnyCorsairInstance, {
    manage: unknown;
}>;
/**
 * Configuration for {@link CorsairToolProvider}. Extends
 * {@link BaseToolProviderOptions} so `allowedToolkits` / `allowedTools` /
 * `defaultScope` are accepted alongside the Corsair-specific fields.
 */
export interface CorsairToolProviderConfig extends BaseToolProviderOptions {
    /**
     * A root Corsair instance from `createCorsair()`, single- or multi-tenant.
     * The provider scopes to a tenant itself, so pass the root, not a
     * `withTenant()` client.
     */
    corsair: CorsairRootInstance;
    /**
     * How a Mastra request maps to a Corsair **tenant** — Corsair's multi-tenancy
     * primitive, where each tenant owns its own connections and credentials.
     * Three modes, each of which also picks the Mastra connection scope for you:
     *
     * - **`string`** — pin every request to one tenant (single-tenant apps).
     *   Derives `defaultScope: 'shared'`.
     * - **function** — choose the tenant for a *new* connection from the author
     *   id or request context (multi-tenant SaaS). Derives
     *   `defaultScope: 'caller-supplied'`.
     * - **omitted** — fall back to the author id, then `'default'`. Derives
     *   `defaultScope: 'per-author'`.
     *
     * This mapping is authoritative for tenant isolation: a `connectionId` never
     * overrides a pinned tenant, and when the caller is known a connectionId
     * naming a different tenant is rejected (see {@link resolveTenant}). The
     * derived scope is only a default; pass `defaultScope` to override it.
     */
    tenantId?: string | ((input: TenantResolveInput) => string | Promise<string>);
}
/**
 * Encodes a `(tenant, toolkit)` pair into the opaque `connectionId`/`authId`
 * string Mastra passes around, so the provider can recover the pair later.
 *
 * This token is encoded, not signed — treat it as a server-minted handle, not a
 * capability. The provider never lets it override the configured tenant (see
 * {@link CorsairToolProviderConfig.tenantId}), so keep connection ids
 * server-side and derive them from your authenticated identity; do not accept
 * one verbatim from an untrusted caller.
 *
 * @param tenantId - The Corsair tenant this connection belongs to.
 * @param toolkit - The Corsair plugin (toolkit) slug.
 * @returns A base64url token that {@link decodeConnectionId} can reverse.
 */
export declare function encodeConnectionId(tenantId: string, toolkit: string): string;
/**
 * Reverses {@link encodeConnectionId}.
 *
 * @param id - A token previously produced by {@link encodeConnectionId}.
 * @returns The `(tenantId, toolkit)` pair, or `null` when the token is
 * malformed (never throws).
 */
export declare function decodeConnectionId(id: string): {
    tenantId: string;
    toolkit: string;
} | null;
/**
 * Maps a Corsair connection state to a Mastra {@link AuthFlowStatus}. Only a
 * live credential (`'connected'`) completes the flow; every other state stays
 * `'pending'` so an in-progress authorize poll is not aborted.
 */
export declare function mapAuthStatus(state?: PluginConnectionState): AuthFlowStatus;
/**
 * Splits Corsair's newline-delimited operation listing (from `listOperations`)
 * into individual tool slugs, tolerating blank lines and surrounding
 * whitespace.
 */
export declare function parseOperationPaths(listing: string): string[];
/**
 * Exposes Corsair as a Mastra {@link BaseToolProvider}: each Corsair plugin is a
 * toolkit, each operation is a tool, and Corsair Hub's managed OAuth backs the
 * authorize / connection-status lifecycle.
 *
 * Extending `BaseToolProvider` provides the allowlist filtering and the legacy
 * `listTools()` / `resolveTools()` shims for free, so the same provider drives
 * both the Mastra editor (via the VNext surface) and static, code-config agents
 * (via `resolveTools`).
 *
 * @example
 * ```ts
 * const provider = new CorsairToolProvider({
 *   corsair,                       // createCorsair({ ... })
 *   tenantId: (req) => req.authorId ?? 'default',
 * });
 *
 * // Register with the Mastra editor:
 * new Mastra({ editor: new MastraEditor({ toolProviders: { corsair: provider } }) });
 *
 * // …or resolve tools directly for a code-config agent:
 * const tools = await provider.resolveTools(['slack.api.channels.list']);
 * ```
 */
export declare class CorsairToolProvider extends BaseToolProvider {
    /** Provider identity surfaced in the Mastra tool picker. */
    readonly info: ToolProviderInfo;
    /** Feature flags the Mastra runtime branches on (batch status, in-place reauth, …). */
    readonly capabilities: ToolProviderCapabilities;
    private readonly corsair;
    private readonly tenantConfig;
    /**
     * @param config - The Corsair instance plus optional tenant mapping and the
     * inherited {@link BaseToolProviderOptions} allowlists. When `defaultScope`
     * is omitted it is derived from `tenantId` (see {@link scopeForTenant}).
     */
    constructor(config: CorsairToolProviderConfig);
    /** Returns the Corsair instance for use with inspect helpers. */
    private asInstance;
    /** Corsair's management namespace (plugins / connection status / connect). */
    private get manage();
    /**
     * Resolves a request to a Corsair tenant.
     *
     * The configured mapping is authoritative; a `connectionId` only ever carries
     * a *claim* that must agree with it. `connectionId` is unauthenticated
     * (base64url, not signed), so it must never let a caller reach a tenant the
     * configuration wouldn't grant:
     *
     * - **Pinned (`string`)** — always that tenant; a connectionId for a different
     *   tenant is forged or stale and is ignored.
     * - **Caller-known** (`authorId`/`requestContext` present) — derive the tenant
     *   from the caller and reject a connectionId that names a *different* tenant
     *   (cross-tenant access attempt).
     * - **No caller context** (`authorize`, `getConnectionStatus`; see
     *   {@link AuthorizeOpts}) — the connectionId is the only tenant source.
     * - **Fresh connection + function resolver** — unsupported: Mastra passes no
     *   author/request context to `authorize`, so throw rather than silently open
     *   OAuth under a fallback tenant.
     */
    private resolveTenant;
    /** Scopes the Corsair instance to a tenant (multi-tenant), else returns it as-is. */
    private scopedInstance;
    /**
     * Every registered Corsair plugin becomes a toolkit. The base class applies
     * `allowedToolkits` filtering on top before exposing this to the editor.
     */
    protected listAllToolkits(): Promise<ToolProviderToolkit[]>;
    /**
     * Lists a plugin's API operations as tools, honouring the `toolkit`, `search`
     * and pagination options. The base class applies `allowedTools` filtering on
     * top.
     */
    protected listAllTools(opts: ListToolsOpts): Promise<ListToolsResult>;
    /**
     * The JSON Schema for one tool's input, so the editor's tool-detail view can
     * render it without materialising the tool. Built from Corsair's structured
     * schema; `null` when the operation is unknown or takes no input.
     */
    getToolSchema(toolSlug: string): Promise<Record<string, unknown> | null>;
    /**
     * Materialises executable Mastra tools for the requested slugs, bound to the
     * resolved tenant's Corsair credentials. The runtime calls this once per
     * connection; each tool's `execute` runs the Corsair operation with the
     * managed token from your database.
     *
     * `ToolAction<any, any, any>` matches the abstract base class signature from
     * `@mastra/core` — the input/output schemas are dynamically derived per plugin
     * at runtime and cannot be statically parameterised.
     */
    resolveToolsVNext(opts: ResolveToolsOpts): Promise<Record<string, ToolAction<any, any, any>>>;
    /**
     * Starts Corsair Hub's managed-OAuth flow for a toolkit and returns the
     * connect URL to send the user to, plus an opaque `authId` to poll.
     */
    authorize(opts: AuthorizeOpts): Promise<{
        url: string;
        authId: string;
    }>;
    /**
     * Polls a managed-OAuth flow: returns `'completed'` once the tenant has a
     * live credential for the toolkit, otherwise `'pending'` (`'failed'` only for
     * an unreadable `authId`).
     */
    getAuthStatus(authId: string): Promise<AuthFlowStatus>;
    /**
     * Batch-checks whether each `(connectionId, toolkit)` is still connected,
     * grouping by resolved tenant so one status call covers many items. Result is
     * keyed by `connectionId`.
     */
    getConnectionStatus(opts: {
        items: Array<{
            connectionId: string;
            toolkit: string;
        }>;
    }): Promise<Record<string, {
        connected: boolean;
    }>>;
    /**
     * Lists the already-connected accounts for the given user bucket(s) + toolkit
     * so the picker can offer them without re-running OAuth. Corsair models one
     * connection per (tenant, toolkit), so at most one item per bucket.
     */
    listConnections(opts: ListConnectionsOpts): Promise<ListConnectionsResult>;
    /**
     * Revokes a connection by removing its stored credentials (and account-scoped
     * data) through Corsair's management API. Idempotent per the ToolProvider
     * contract: an unreadable `connectionId` or an already-gone connection
     * resolves without error.
     */
    revokeConnection(connectionId: string): Promise<void>;
    /** Reports provider health by probing Corsair's management API. */
    getHealth(): Promise<ToolProviderHealth>;
}
export {};
//# sourceMappingURL=corsair-tool-provider.d.ts.map