import {
  clamp01
} from "./chunk-TBBDFYXW.js";

// src/retrieval.ts
var DEFAULT_STOPWORDS = /* @__PURE__ */ new Set([
  "a",
  "an",
  "and",
  "are",
  "as",
  "at",
  "be",
  "but",
  "by",
  "for",
  "from",
  "has",
  "have",
  "i",
  "in",
  "is",
  "it",
  "of",
  "on",
  "or",
  "that",
  "the",
  "this",
  "to",
  "was",
  "were",
  "with"
]);
function tokenize(query, minTokenLen) {
  return query.toLowerCase().split(/[^a-z0-9]+/g).map((t) => t.trim()).filter((t) => t.length >= minTokenLen).filter((t) => !DEFAULT_STOPWORDS.has(t));
}
function applyRuntimeRetrievalPolicy(base, runtime) {
  const fromRuntime = runtime?.recencyWeight;
  if (typeof fromRuntime !== "number") {
    return { recencyWeight: clamp01(base.recencyWeight) };
  }
  return { recencyWeight: clamp01(fromRuntime) };
}
function expandQuery(query, options) {
  const trimmed = query.trim();
  if (!trimmed) return [query];
  const maxQueries = Math.max(1, Math.min(20, options.maxQueries));
  const tokens = tokenize(trimmed, options.minTokenLen);
  const seen = /* @__PURE__ */ new Set();
  const uniqTokens = [];
  for (const t of tokens) {
    if (seen.has(t)) continue;
    seen.add(t);
    uniqTokens.push(t);
  }
  const expansions = [query];
  if (uniqTokens.length === 0) return expansions;
  const top = uniqTokens.slice(0, 6);
  const candidates = [
    top.slice(0, 3).join(" "),
    top.slice(0, 2).join(" "),
    top[0],
    top[1],
    top[2]
  ].filter(Boolean);
  for (const c of candidates) {
    if (expansions.length >= maxQueries) break;
    if (c && !expansions.includes(c)) expansions.push(c);
  }
  return expansions.slice(0, maxQueries);
}

export {
  applyRuntimeRetrievalPolicy,
  expandQuery
};
//# sourceMappingURL=chunk-5IZL4DCV.js.map