import { hasAdminBypass } from './chunk-SXZS6VZ3.js';
import { listToolProvidersResponseSchema, listToolProviderToolkitsResponseSchema, toolProviderIdPathParams, listToolProviderToolsResponseSchema, listToolProviderToolsQuerySchema, getToolProviderToolSchemaResponseSchema, toolSlugPathParams, authorizeToolProviderResponseSchema, authorizeToolProviderBodySchema, authStatusToolProviderResponseSchema, toolProviderAuthStatusPathParams, connectionStatusToolProviderResponseSchema, connectionStatusToolProviderBodySchema, listConnectionsResponseSchema, listConnectionsQuerySchema, listConnectionFieldsResponseSchema, listConnectionFieldsQuerySchema, disconnectConnectionResponseSchema, disconnectConnectionQuerySchema, toolProviderConnectionPathParams, updateConnectionResponseSchema, updateConnectionBodySchema, connectionUsageResponseSchema, connectionUsageQuerySchema, toolProviderHealthResponseSchema } from './chunk-SFAWHLOX.js';
import { MASTRA_USER_KEY } from './chunk-5FOSK5R3.js';
import { handleError } from './chunk-7ZWJX3AN.js';
import { createRoute } from './chunk-PIKPS6Q6.js';
import { HTTPException } from './chunk-6QWQZI4Q.js';
import { MASTRA_RESOURCE_ID_KEY } from '@mastra/core/request-context';

var TOOL_PROVIDERS_RESOURCE = "tool-providers";
var SHARED_BUCKET_ID = "shared";
var _toolProviderModule;
async function loadToolProviderModule() {
  if (!_toolProviderModule) {
    _toolProviderModule = await import('@mastra/core/tool-provider');
  }
  return _toolProviderModule;
}
function requireEditor(editor) {
  if (!editor) {
    throw new HTTPException(500, { message: "Editor is not configured" });
  }
  return editor;
}
async function resolveProvider(editor, providerId) {
  try {
    return editor.getToolProviderOrThrow(providerId);
  } catch (error) {
    const { UnknownToolProviderError } = await loadToolProviderModule();
    if (error instanceof UnknownToolProviderError) {
      throw new HTTPException(404, { message: error.message });
    }
    throw error;
  }
}
var defaultBucketWarned = false;
function warnDefaultBucketFallback(logger) {
  if (defaultBucketWarned) return;
  defaultBucketWarned = true;
  logger?.warn(
    '[tool-providers] caller-supplied scope falling back to shared "default" bucket \u2014 wire mapUserToResourceId or set MASTRA_USER_KEY to avoid cross-tenant OAuth sharing'
  );
}
function resolveOwnerId(requestContext, logger) {
  const resourceId = requestContext?.get(MASTRA_RESOURCE_ID_KEY);
  if (typeof resourceId === "string" && resourceId.length > 0) {
    return resourceId;
  }
  const user = requestContext?.get(MASTRA_USER_KEY);
  if (user && typeof user === "object" && "id" in user) {
    const id = user.id;
    if (typeof id === "string" && id.length > 0) {
      return id;
    }
  }
  warnDefaultBucketFallback(logger);
  return "default";
}
var LIST_TOOL_PROVIDERS_ROUTE = createRoute({
  method: "GET",
  path: "/tool-providers",
  responseType: "json",
  responseSchema: listToolProvidersResponseSchema,
  summary: "List tool providers",
  description: "Returns a list of all registered tool providers with their info and capabilities",
  tags: ["Tool Providers"],
  requiresAuth: true,
  handler: async ({ mastra }) => {
    try {
      const editor = requireEditor(mastra.getEditor());
      const providers = editor.getToolProviders();
      return {
        providers: Object.values(providers).map((provider) => ({
          ...provider.info,
          ...provider.displayName ? { displayName: provider.displayName } : {},
          ...provider.capabilities ? { capabilities: provider.capabilities } : {}
        }))
      };
    } catch (error) {
      return handleError(error, "Error listing tool providers");
    }
  }
});
var LIST_TOOL_PROVIDER_TOOLKITS_ROUTE = createRoute({
  method: "GET",
  path: "/tool-providers/:providerId/toolkits",
  responseType: "json",
  pathParamSchema: toolProviderIdPathParams,
  responseSchema: listToolProviderToolkitsResponseSchema,
  summary: "List tool provider toolkits",
  description: "Returns the toolkits available from a specific tool provider",
  tags: ["Tool Providers"],
  requiresAuth: true,
  handler: async ({ mastra, providerId }) => {
    try {
      const editor = requireEditor(mastra.getEditor());
      const provider = await resolveProvider(editor, providerId);
      if (provider.listToolkitsVNext) {
        return await provider.listToolkitsVNext();
      }
      if (provider.listToolkits) {
        return await provider.listToolkits();
      }
      return { data: [] };
    } catch (error) {
      return handleError(error, "Error listing tool provider toolkits");
    }
  }
});
var LIST_TOOL_PROVIDER_TOOLS_ROUTE = createRoute({
  method: "GET",
  path: "/tool-providers/:providerId/tools",
  responseType: "json",
  pathParamSchema: toolProviderIdPathParams,
  queryParamSchema: listToolProviderToolsQuerySchema,
  responseSchema: listToolProviderToolsResponseSchema,
  summary: "List tool provider tools",
  description: "Returns the tools available from a specific tool provider, with optional filtering",
  tags: ["Tool Providers"],
  requiresAuth: true,
  handler: async ({ mastra, providerId, toolkit, search, page, perPage }) => {
    try {
      const editor = requireEditor(mastra.getEditor());
      const provider = await resolveProvider(editor, providerId);
      const opts = {};
      if (toolkit !== void 0) opts.toolkit = toolkit;
      if (search !== void 0) opts.search = search;
      if (page !== void 0) opts.page = page;
      if (perPage !== void 0) opts.perPage = perPage;
      if (provider.listToolsVNext) {
        return await provider.listToolsVNext(Object.keys(opts).length > 0 ? opts : void 0);
      }
      return await provider.listTools(Object.keys(opts).length > 0 ? opts : void 0);
    } catch (error) {
      return handleError(error, "Error listing tool provider tools");
    }
  }
});
var GET_TOOL_PROVIDER_TOOL_SCHEMA_ROUTE = createRoute({
  method: "GET",
  path: "/tool-providers/:providerId/tools/:toolSlug/schema",
  responseType: "json",
  pathParamSchema: toolSlugPathParams,
  responseSchema: getToolProviderToolSchemaResponseSchema,
  summary: "Get tool provider tool schema",
  description: "Returns the schema for a specific tool from a tool provider",
  tags: ["Tool Providers"],
  requiresAuth: true,
  handler: async ({ mastra, providerId, toolSlug }) => {
    try {
      const editor = requireEditor(mastra.getEditor());
      const provider = await resolveProvider(editor, providerId);
      if (!provider.getToolSchema) {
        throw new HTTPException(404, { message: `Tool provider ${providerId} does not support getToolSchema` });
      }
      const schema = await provider.getToolSchema(toolSlug);
      if (!schema) {
        throw new HTTPException(404, { message: `Schema for tool ${toolSlug} not found in provider ${providerId}` });
      }
      return schema;
    } catch (error) {
      return handleError(error, "Error getting tool provider tool schema");
    }
  }
});
var AUTHORIZE_TOOL_PROVIDER_ROUTE = createRoute({
  method: "POST",
  path: "/tool-providers/:providerId/authorize",
  responseType: "json",
  pathParamSchema: toolProviderIdPathParams,
  bodySchema: authorizeToolProviderBodySchema,
  responseSchema: authorizeToolProviderResponseSchema,
  summary: "Authorize tool provider connection",
  description: "Starts an OAuth flow and returns a redirect URL + opaque auth handle",
  tags: ["Tool Providers"],
  requiresAuth: true,
  handler: async ({ mastra, providerId, toolkit, connectionId, toolName, config, label, scope, requestContext }) => {
    try {
      const editor = requireEditor(mastra.getEditor());
      const provider = await resolveProvider(editor, providerId);
      if (!provider.authorize) {
        throw new HTTPException(400, { message: `Tool provider ${providerId} does not support authorize` });
      }
      const effectiveScope = scope === "shared" || scope === "caller-supplied" ? scope : "per-author";
      const callerResourceId = requestContext?.get(MASTRA_RESOURCE_ID_KEY);
      if (effectiveScope === "caller-supplied") {
        if (typeof callerResourceId !== "string" || callerResourceId.length === 0) {
          throw new HTTPException(400, {
            message: `Cannot authorize caller-supplied connection: request context has no '${MASTRA_RESOURCE_ID_KEY}'. Set requestContext.set('${MASTRA_RESOURCE_ID_KEY}', <userId>) before calling /authorize.`
          });
        }
      }
      const callerAuthorId = resolveOwnerId(requestContext, mastra.getLogger());
      const ownerAuthorId = effectiveScope === "shared" ? SHARED_BUCKET_ID : effectiveScope === "caller-supplied" ? callerResourceId : callerAuthorId;
      const bucket = connectionId && connectionId.length > 0 ? connectionId : ownerAuthorId;
      const result = await provider.authorize({ toolkit, connectionId: bucket, toolName, config });
      const persistedConnectionId = connectionId && connectionId.length > 0 ? connectionId : result.authId;
      try {
        const storage = mastra.getStorage();
        const store = await storage?.getStore("toolProviderConnections");
        if (store && persistedConnectionId) {
          await store.upsertConnection({
            authorId: ownerAuthorId,
            providerId: provider.info.id,
            toolkit,
            connectionId: persistedConnectionId,
            label: typeof label === "string" && label.length > 0 ? label : null,
            scope: effectiveScope
          });
        }
      } catch (upsertError) {
        mastra.getLogger?.()?.warn?.("[tool-providers] failed to upsert tool_provider_connections label", {
          error: upsertError instanceof Error ? upsertError.message : String(upsertError),
          providerId,
          toolkit,
          connectionId: persistedConnectionId
        });
      }
      return result;
    } catch (error) {
      return handleError(error, "Error authorizing tool provider");
    }
  }
});
var GET_TOOL_PROVIDER_AUTH_STATUS_ROUTE = createRoute({
  method: "GET",
  path: "/tool-providers/:providerId/auth-status/:authId",
  responseType: "json",
  pathParamSchema: toolProviderAuthStatusPathParams,
  responseSchema: authStatusToolProviderResponseSchema,
  summary: "Get tool provider auth status",
  description: "Polls the OAuth flow status for an outstanding authorize call",
  tags: ["Tool Providers"],
  requiresAuth: true,
  handler: async ({ mastra, providerId, authId }) => {
    try {
      const editor = requireEditor(mastra.getEditor());
      const provider = await resolveProvider(editor, providerId);
      if (!provider.getAuthStatus) {
        throw new HTTPException(400, { message: `Tool provider ${providerId} does not support getAuthStatus` });
      }
      const status = await provider.getAuthStatus(authId);
      return { status };
    } catch (error) {
      return handleError(error, "Error getting tool provider auth status");
    }
  }
});
var TOOL_PROVIDER_CONNECTION_STATUS_ROUTE = createRoute({
  method: "POST",
  path: "/tool-providers/:providerId/connection-status",
  responseType: "json",
  pathParamSchema: toolProviderIdPathParams,
  bodySchema: connectionStatusToolProviderBodySchema,
  responseSchema: connectionStatusToolProviderResponseSchema,
  summary: "Get connection status for a provider",
  description: "Batch-checks whether a set of (connectionId, toolkit) tuples are still connected",
  tags: ["Tool Providers"],
  requiresAuth: true,
  handler: async ({ mastra, providerId, items }) => {
    try {
      const editor = requireEditor(mastra.getEditor());
      const provider = await resolveProvider(editor, providerId);
      if (!provider.getConnectionStatus) {
        throw new HTTPException(400, { message: `Tool provider ${providerId} does not support getConnectionStatus` });
      }
      const result = await provider.getConnectionStatus({ items });
      return { items: result };
    } catch (error) {
      return handleError(error, "Error getting connection status");
    }
  }
});
var LIST_TOOL_PROVIDER_CONNECTIONS_ROUTE = createRoute({
  method: "GET",
  path: "/tool-providers/:providerId/connections",
  responseType: "json",
  pathParamSchema: toolProviderIdPathParams,
  queryParamSchema: listConnectionsQuerySchema,
  responseSchema: listConnectionsResponseSchema,
  summary: "List existing connections",
  description: "Returns existing provider connections on a toolkit, so the picker can offer them for pinning without re-running OAuth",
  tags: ["Tool Providers"],
  requiresAuth: true,
  handler: async ({
    mastra,
    providerId,
    toolkit,
    authorId: queryAuthorId,
    scope: queryScope,
    page,
    perPage,
    requestContext
  }) => {
    try {
      const editor = requireEditor(mastra.getEditor());
      const provider = await resolveProvider(editor, providerId);
      if (!provider.listConnections) {
        throw new HTTPException(400, { message: `Tool provider ${providerId} does not support listConnections` });
      }
      const callerAuthorId = resolveOwnerId(requestContext, mastra.getLogger());
      const isAdmin = requestContext ? hasAdminBypass(requestContext, TOOL_PROVIDERS_RESOURCE) : false;
      const requestedAuthorId = isAdmin && typeof queryAuthorId === "string" && queryAuthorId.length > 0 ? queryAuthorId : void 0;
      const effectiveAuthorId = isAdmin ? requestedAuthorId : callerAuthorId;
      const storage = mastra.getStorage();
      const store = await storage?.getStore("toolProviderConnections");
      let labelRows = [];
      if (store) {
        try {
          const rows = await store.listConnectionsByAuthor({
            providerId: provider.info.id,
            toolkit
          });
          labelRows = rows.map((r) => ({
            authorId: r.authorId,
            connectionId: r.connectionId,
            label: r.label,
            scope: r.scope ?? "per-author"
          }));
        } catch (joinError) {
          mastra.getLogger?.()?.warn?.("[tool-providers] failed to join tool_provider_connections labels", {
            error: joinError instanceof Error ? joinError.message : String(joinError),
            providerId,
            toolkit
          });
        }
      }
      const userIdSet = /* @__PURE__ */ new Set();
      const wantShared = !queryScope || queryScope === "shared";
      const wantPerAuthor = !queryScope || queryScope === "per-author";
      const wantCallerSupplied = !queryScope || queryScope === "caller-supplied";
      const hasSharedRow = labelRows.some((r) => r.scope === "shared");
      if (isAdmin && effectiveAuthorId === void 0) {
        for (const r of labelRows) {
          if (r.scope === "shared" && wantShared) userIdSet.add(r.authorId);
          if (r.scope === "per-author" && wantPerAuthor) userIdSet.add(r.authorId);
          if (r.scope === "caller-supplied" && wantCallerSupplied) userIdSet.add(r.authorId);
        }
      } else if (isAdmin && effectiveAuthorId) {
        if (wantPerAuthor || wantCallerSupplied) userIdSet.add(effectiveAuthorId);
        if (wantShared && hasSharedRow) userIdSet.add(SHARED_BUCKET_ID);
      } else {
        if (wantPerAuthor) userIdSet.add(callerAuthorId);
        if (wantShared && hasSharedRow) userIdSet.add(SHARED_BUCKET_ID);
      }
      const userIds = Array.from(userIdSet);
      if (userIds.length === 0) {
        return {
          items: [],
          pagination: { page: page ?? 1, perPage, hasMore: false }
        };
      }
      const adapterResult = await provider.listConnections({
        toolkit,
        userIds,
        ...typeof page === "number" ? { page } : {},
        ...typeof perPage === "number" ? { perPage } : {}
      });
      const rowByConnId = new Map(labelRows.map((r) => [r.connectionId, r]));
      const visibleItems = adapterResult.items.filter((item) => {
        if (!queryScope) return true;
        const scope = rowByConnId.get(item.connectionId)?.scope ?? "per-author";
        return scope === queryScope;
      });
      return {
        items: visibleItems.map((item) => {
          const row = rowByConnId.get(item.connectionId);
          return {
            ...item,
            label: row?.label ?? null,
            ...row?.scope ? { scope: row.scope } : {}
          };
        }),
        pagination: adapterResult.pagination
      };
    } catch (error) {
      return handleError(error, "Error listing tool provider connections");
    }
  }
});
var LIST_TOOL_PROVIDER_CONNECTION_FIELDS_ROUTE = createRoute({
  method: "GET",
  path: "/tool-providers/:providerId/connection-fields",
  responseType: "json",
  pathParamSchema: toolProviderIdPathParams,
  queryParamSchema: listConnectionFieldsQuerySchema,
  responseSchema: listConnectionFieldsResponseSchema,
  summary: "List connection field schema",
  description: "Returns a list of provider-specific fields the UI should collect before initiating an authorize call",
  tags: ["Tool Providers"],
  requiresAuth: true,
  handler: async ({ mastra, providerId, toolkit }) => {
    try {
      const editor = requireEditor(mastra.getEditor());
      const provider = await resolveProvider(editor, providerId);
      if (!provider.listConnectionFields) {
        return { fields: [] };
      }
      const fields = await provider.listConnectionFields({ toolkit });
      return { fields };
    } catch (error) {
      return handleError(error, "Error listing tool provider connection fields");
    }
  }
});
var DISCONNECT_TOOL_PROVIDER_CONNECTION_ROUTE = createRoute({
  method: "DELETE",
  path: "/tool-providers/:providerId/connections/:connectionId",
  responseType: "json",
  pathParamSchema: toolProviderConnectionPathParams,
  queryParamSchema: disconnectConnectionQuerySchema,
  responseSchema: disconnectConnectionResponseSchema,
  summary: "Disconnect a connection",
  description: "Revokes the provider-side connection (if supported) and removes the persisted tool_provider_connections row. Use `?force=true` to bypass usage checks.",
  tags: ["Tool Providers"],
  requiresAuth: true,
  handler: async ({ mastra, providerId, connectionId, force, requestContext }) => {
    try {
      const editor = requireEditor(mastra.getEditor());
      const provider = await resolveProvider(editor, providerId);
      const callerAuthorId = resolveOwnerId(requestContext, mastra.getLogger());
      const isAdmin = requestContext ? hasAdminBypass(requestContext, TOOL_PROVIDERS_RESOURCE) : false;
      const isForce = force === true || force === "true";
      const storage = mastra.getStorage();
      const store = await storage?.getStore("toolProviderConnections");
      let ownerAuthorId;
      let ownerScope;
      let matched = false;
      if (store) {
        const rows = await store.listConnectionsByAuthor({ providerId: provider.info.id });
        const match = rows.find((r) => r.connectionId === connectionId);
        if (match) {
          matched = true;
          ownerAuthorId = match.authorId;
          ownerScope = match.scope;
        }
      }
      if (store && !matched && !isAdmin) {
        throw new HTTPException(403, {
          message: "You do not have permission to disconnect this connection"
        });
      }
      const effectiveOwner = ownerAuthorId ?? callerAuthorId;
      const isShared = ownerScope === "shared";
      if (!isShared && effectiveOwner !== callerAuthorId && !isAdmin) {
        throw new HTTPException(403, {
          message: "You do not have permission to disconnect this connection"
        });
      }
      if (!isForce) {
        const usage = await countConnectionUsage(mastra, connectionId);
        if (usage > 0) {
          throw new HTTPException(409, {
            message: `Connection ${connectionId} is still pinned by ${usage} agent(s). Pass ?force=true to disconnect anyway.`
          });
        }
      }
      let revoked = false;
      if (provider.capabilities?.supportsRevoke && typeof provider.revokeConnection === "function") {
        await provider.revokeConnection(connectionId);
        revoked = true;
      }
      if (store) {
        await store.deleteConnection({
          authorId: effectiveOwner,
          providerId: provider.info.id,
          connectionId
        });
      }
      return { ok: true, revoked };
    } catch (error) {
      return handleError(error, "Error disconnecting tool provider connection");
    }
  }
});
var UPDATE_TOOL_PROVIDER_CONNECTION_ROUTE = createRoute({
  method: "PATCH",
  path: "/tool-providers/:providerId/connections/:connectionId",
  responseType: "json",
  pathParamSchema: toolProviderConnectionPathParams,
  bodySchema: updateConnectionBodySchema,
  responseSchema: updateConnectionResponseSchema,
  summary: "Update a connection label",
  description: "Updates the persisted display label on tool_provider_connections. Returns 403 when caller is neither the owner nor admin (and the row is not shared), 404 when the row does not exist.",
  tags: ["Tool Providers"],
  requiresAuth: true,
  handler: async ({ mastra, providerId, connectionId, label, requestContext }) => {
    try {
      const editor = requireEditor(mastra.getEditor());
      const provider = await resolveProvider(editor, providerId);
      const callerAuthorId = resolveOwnerId(requestContext, mastra.getLogger());
      const isAdmin = requestContext ? hasAdminBypass(requestContext, TOOL_PROVIDERS_RESOURCE) : false;
      const storage = mastra.getStorage();
      const store = await storage?.getStore("toolProviderConnections");
      if (!store) {
        throw new HTTPException(500, {
          message: "Tool provider connections storage is not configured"
        });
      }
      const rows = await store.listConnectionsByAuthor({ providerId: provider.info.id });
      const match = rows.find((r) => r.connectionId === connectionId);
      if (!match) {
        throw new HTTPException(404, {
          message: `Connection ${connectionId} not found for provider ${providerId}`
        });
      }
      const isShared = match.scope === "shared";
      if (!isShared && match.authorId !== callerAuthorId && !isAdmin) {
        throw new HTTPException(403, {
          message: "You do not have permission to update this connection"
        });
      }
      const nextLabel = typeof label === "string" && label.trim().length > 0 ? label.trim() : null;
      await store.upsertConnection({
        authorId: match.authorId,
        providerId: provider.info.id,
        toolkit: match.toolkit,
        connectionId,
        label: nextLabel,
        scope: match.scope
      });
      return { ok: true, label: nextLabel };
    } catch (error) {
      return handleError(error, "Error updating tool provider connection");
    }
  }
});
var GET_TOOL_PROVIDER_CONNECTION_USAGE_ROUTE = createRoute({
  method: "GET",
  path: "/tool-providers/:providerId/connections/:connectionId/usage",
  responseType: "json",
  pathParamSchema: toolProviderConnectionPathParams,
  queryParamSchema: connectionUsageQuerySchema,
  responseSchema: connectionUsageResponseSchema,
  summary: "List agents using a connection",
  description: "Returns the agents that pin this connection in their toolProviders config",
  tags: ["Tool Providers"],
  requiresAuth: true,
  handler: async ({ mastra, providerId, connectionId, toolkit, requestContext }) => {
    try {
      const editor = requireEditor(mastra.getEditor());
      const provider = await resolveProvider(editor, providerId);
      const callerAuthorId = resolveOwnerId(requestContext, mastra.getLogger());
      const isAdmin = requestContext ? hasAdminBypass(requestContext, TOOL_PROVIDERS_RESOURCE) : false;
      const storage = mastra.getStorage();
      const store = await storage?.getStore("toolProviderConnections");
      let ownerAuthorId;
      let ownerScope;
      let matched = false;
      if (store) {
        const rows = await store.listConnectionsByAuthor({ providerId: provider.info.id });
        const match = rows.find((r) => r.connectionId === connectionId);
        if (match) {
          matched = true;
          ownerAuthorId = match.authorId;
          ownerScope = match.scope;
        }
      }
      if (store && !matched && !isAdmin) {
        throw new HTTPException(403, {
          message: "You do not have permission to view usage for this connection"
        });
      }
      const effectiveOwner = ownerAuthorId ?? callerAuthorId;
      const isShared = ownerScope === "shared";
      if (!isShared && effectiveOwner !== callerAuthorId && !isAdmin) {
        throw new HTTPException(403, {
          message: "You do not have permission to view usage for this connection"
        });
      }
      const agents = await scanConnectionUsage(mastra, { providerId: provider.info.id, connectionId, toolkit });
      return { agents };
    } catch (error) {
      return handleError(error, "Error listing tool provider connection usage");
    }
  }
});
var GET_TOOL_PROVIDER_HEALTH_ROUTE = createRoute({
  method: "GET",
  path: "/tool-providers/:providerId/health",
  responseType: "json",
  pathParamSchema: toolProviderIdPathParams,
  responseSchema: toolProviderHealthResponseSchema,
  summary: "Get tool provider health",
  description: "Returns provider-level health (config, reachability, etc.)",
  tags: ["Tool Providers"],
  requiresAuth: true,
  handler: async ({ mastra, providerId }) => {
    try {
      const editor = requireEditor(mastra.getEditor());
      const provider = await resolveProvider(editor, providerId);
      if (!provider.getHealth) {
        return { ok: true };
      }
      return await provider.getHealth();
    } catch (error) {
      return handleError(error, "Error getting tool provider health");
    }
  }
});
async function scanConnectionUsage(mastra, args) {
  const storage = mastra.getStorage();
  const agentsStore = await storage?.getStore("agents");
  if (!agentsStore) return [];
  const { agents } = await agentsStore.listResolved({ perPage: false });
  const out = [];
  for (const agent of agents) {
    const config = agent?.toolProviders?.[args.providerId];
    if (!config?.connections) continue;
    for (const [toolkit, connections] of Object.entries(config.connections)) {
      if (args.toolkit && toolkit !== args.toolkit) continue;
      const match = connections.some((c) => c.connectionId === args.connectionId);
      if (match) {
        out.push({ id: agent.id, name: agent.name ?? agent.id });
        break;
      }
    }
  }
  return out;
}
async function countConnectionUsage(mastra, connectionId) {
  const storage = mastra.getStorage();
  const agentsStore = await storage?.getStore("agents");
  if (!agentsStore) return 0;
  const { agents } = await agentsStore.listResolved({ perPage: false });
  let count = 0;
  for (const agent of agents) {
    const tp = agent?.toolProviders;
    if (!tp) continue;
    for (const config of Object.values(tp)) {
      const pinned = Object.values(config?.connections ?? {}).some(
        (arr) => arr.some((c) => c.connectionId === connectionId)
      );
      if (pinned) {
        count += 1;
        break;
      }
    }
  }
  return count;
}

export { AUTHORIZE_TOOL_PROVIDER_ROUTE, DISCONNECT_TOOL_PROVIDER_CONNECTION_ROUTE, GET_TOOL_PROVIDER_AUTH_STATUS_ROUTE, GET_TOOL_PROVIDER_CONNECTION_USAGE_ROUTE, GET_TOOL_PROVIDER_HEALTH_ROUTE, GET_TOOL_PROVIDER_TOOL_SCHEMA_ROUTE, LIST_TOOL_PROVIDERS_ROUTE, LIST_TOOL_PROVIDER_CONNECTIONS_ROUTE, LIST_TOOL_PROVIDER_CONNECTION_FIELDS_ROUTE, LIST_TOOL_PROVIDER_TOOLKITS_ROUTE, LIST_TOOL_PROVIDER_TOOLS_ROUTE, TOOL_PROVIDER_CONNECTION_STATUS_ROUTE, UPDATE_TOOL_PROVIDER_CONNECTION_ROUTE };
//# sourceMappingURL=chunk-POM4VGLJ.js.map
//# sourceMappingURL=chunk-POM4VGLJ.js.map