import { createLogger } from "../logger.js";
const log = createLogger("search");
function checkSamplingSupport(server) {
  try {
    const capabilities = server.getClientCapabilities();
    if (!capabilities) return false;
    return capabilities.sampling !== void 0;
  } catch (err) {
    log.debug("sampling support check failed", { error: String(err) });
    return false;
  }
}
async function requestSampling(server, messages, maxTokens) {
  if (!checkSamplingSupport(server)) {
    throw new Error("Client does not support MCP sampling");
  }
  log.debug("sending sampling request", {
    messageCount: messages.length,
    maxTokens
  });
  const response = await server.createMessage({
    messages,
    maxTokens
  });
  log.debug("sampling response received", {
    model: response?.model,
    contentType: response?.content?.type,
    textLength: response?.content?.text?.length ?? 0
  });
  return response;
}
function extractTextFromSamplingResponse(response) {
  try {
    if (!response || typeof response !== "object") return "";
    const resp = response;
    const content = resp.content;
    if (!content) return "";
    if (content.type === "text" && typeof content.text === "string") {
      return content.text.trim();
    }
    return "";
  } catch (err) {
    log.debug("failed to extract text from sampling response", { error: String(err) });
    return "";
  }
}
export {
  checkSamplingSupport,
  extractTextFromSamplingResponse,
  requestSampling
};
//# sourceMappingURL=sampling.js.map