All files / lib/search search-index.ts

100% Statements 299/299
94.64% Branches 53/56
100% Functions 11/11
100% Lines 299/299

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 3001x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 79x 79x 79x 79x 79x 79x 79x 79x 79x 79x 79x 1x 79x 78x 78x 78x 79x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 108x 108x 8x 8x 108x 4x 4x 108x 108x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 108x 22x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 28x 2x 2x 28x 6x 6x 28x 28x 5x 5x 5x 5x 9x 9x 9x 9x 9x 9x 9x 5x 5x 1x  
"use strict";
/**
 * Local lexical search over SDK method records using MiniSearch (BM25-style
 * full-text ranking, prefix and fuzzy matching), with English stop words
 * from the stopword package and Porter stemming from the stemmer package.
 * No embeddings or external services are involved.
 *
 * @module search/search-index
 */
import MiniSearch from "minisearch";
import { stemmer } from "stemmer";
import { eng } from "stopword";
 
import type { MethodRecord } from "../types.js";
 
/** Fields indexed for full-text search. */
const FIELDS = [
  "methodName",
  "methodWords",
  "className",
  "operationId",
  "summary",
  "description",
  "tags",
  "path",
  "params",
  "types",
];
 
const SEARCH_OPTIONS = {
  boost: {
    methodName: 3,
    methodWords: 3,
    operationId: 2,
    summary: 2.5,
    tags: 1.5,
    path: 1.5,
    className: 1.2,
    types: 0.5,
  },
  prefix: true,
  fuzzy: 0.2,
  combineWith: "OR" as const,
};
 
/**
 * Words in the stopword package's English list that carry meaning in API
 * search: "get" is a method-name verb (getPetById), and the others are
 * common query-parameter names (since, before, after, between, from).
 */
const API_TERMS = new Set([
  "get",
  "since",
  "before",
  "after",
  "between",
  "from",
]);
 
/** English stop words (the stopword package's list) minus API terms. */
const STOP_WORDS = new Set(eng.filter((w) => !API_TERMS.has(w)));
 
/**
 * Normalise a search term, at indexing and at query time alike: lower-case,
 * drop English stop words, and reduce the word to its Porter stem, so
 * "cats" matches "cat" and "vaccinated" matches "vaccination".
 *
 * @param {String} term - raw term
 * @return {String|null} processed term, or null to drop it
 */
export function processTerm(term: string): string | null {
  const t = term.toLowerCase().replace(/'s$/, "");
  if (STOP_WORDS.has(t)) {
    return null;
  }
  return stemmer(t);
}
 
/**
 * Split identifiers into words: "listPetVisits" -> "list pet visits".
 *
 * @param {String} s - identifier
 * @return {String} words
 */
export function splitWords(s: string | undefined): string {
  return (s ?? "")
    .replace(/([a-z0-9])([A-Z])/g, "$1 $2")
    .replace(/[_\-/{}.]+/g, " ")
    .toLowerCase();
}
 
/**
 * Convert a record to an indexable document.
 *
 * @private
 */
function _doc(r: MethodRecord): Record<string, string> {
  return {
    id: r.id,
    methodName: r.methodName,
    methodWords: splitWords(r.methodName),
    className: `${r.className} ${splitWords(r.className.replace(/Api$/, ""))}`,
    operationId: `${r.operationId ?? ""} ${splitWords(r.operationId)}`,
    summary: r.summary ?? "",
    description: r.description ?? "",
    tags: r.tags.join(" "),
    path: `${r.httpMethod} ${splitWords(r.path)}`,
    params: [
      ...r.apiParameters.map(
        // The schema type carries enum values, e.g. "dog" | "cat".
        (p) =>
          `${p.name} ${splitWords(p.name)} ${p.description ?? ""} ${p.schemaType ?? ""}`,
      ),
      ...r.parameters.map((p) => p.name),
    ].join(" "),
    types: [
      r.returnType,
      ...r.referencedTypes.map(
        (t) =>
          `${t.name} ${t.declaration.replace(/[{}();:|"'<>[\],=?]/g, " ")}`,
      ),
    ].join(" "),
  };
}
 
/** A compact search hit. */
export interface SearchHit {
  id: string;
  sdkPath: string;
  signature: string;
  summary?: string;
  httpMethod: string;
  path: string;
  requiredArguments: string[];
  auth: string;
  score: number;
}
 
/**
 * Summarise auth requirements in one line.
 *
 * @param {Object} r - method record
 * @return {String} summary
 */
export function authLine(r: MethodRecord): string {
  if (r.auth.alternatives.length === 0) {
    return "none";
  }
  const alts = r.auth.alternatives
    .map((a) => a.schemes.map((s) => `${s.name}(${s.type})`).join(" + "))
    .join(" OR ");
  return `${alts}${r.auth.optional ? " OR none" : ""}${
    r.auth.unsupportedSchemes.length
      ? ` [unsupported: ${r.auth.unsupportedSchemes.join(", ")}]`
      : ""
  }`;
}
 
/**
 * Search index over method records.
 */
export class MethodSearchIndex {
  private readonly mini: MiniSearch;
  private readonly byId: Map<string, MethodRecord>;
 
  /**
   * @param {Array} records - method records
   * @param {String} [serialised] - previously serialised MiniSearch JSON
   */
  constructor(
    readonly records: MethodRecord[],
    serialised?: string,
  ) {
    this.byId = new Map(records.map((r) => [r.id, r]));
    const options = {
      fields: FIELDS,
      storeFields: ["id"],
      processTerm,
      searchOptions: SEARCH_OPTIONS,
    };
    if (serialised) {
      this.mini = MiniSearch.loadJSON(serialised, options);
    } else {
      this.mini = new MiniSearch(options);
      this.mini.addAll(records.map(_doc));
    }
  }
 
  /**
   * Serialise the full-text index.
   *
   * @return {String} JSON
   */
  serialise(): string {
    return JSON.stringify(this.mini);
  }
 
  /**
   * Look up a record by id.
   *
   * @param {String} id - "<Class>.<method>"
   * @return {Object|undefined} record
   */
  get(id: string): MethodRecord | undefined {
    return this.byId.get(id);
  }
 
  /**
   * Ranked search restricted to visible records.
   *
   * @param {String} query - free text
   * @param {Object} opts - options
   * @param {Function} opts.visible - filter for records the caller may use
   * @param {Number} opts.limit - maximum results
   * @param {String} [opts.tag] - restrict to a tag or API class
   * @return {Object} { hits, total }
   */
  search(
    query: string,
    opts: {
      visible: (r: MethodRecord) => boolean;
      limit: number;
      tag?: string;
    },
  ): { hits: SearchHit[]; total: number } {
    const results = this.mini.search(query);
    const hits: SearchHit[] = [];
    let total = 0;
    for (const res of results) {
      const r = this.byId.get(String(res.id));
      if (!r || !opts.visible(r)) {
        continue;
      }
      if (opts.tag && !r.tags.includes(opts.tag) && r.className !== opts.tag) {
        continue;
      }
      total++;
      if (hits.length < opts.limit) {
        hits.push({
          id: r.id,
          sdkPath: r.sdkPath,
          signature: r.signature,
          summary: r.summary ?? r.description?.split("\n")[0],
          httpMethod: r.httpMethod,
          path: r.path,
          requiredArguments: r.requiredArguments,
          auth: authLine(r),
          score: Math.round(res.score * 100) / 100,
        });
      }
    }
    return { hits, total };
  }
 
  /**
   * Overview of capabilities grouped by API class, for agents that do not
   * yet know the API's vocabulary.
   *
   * @param {Object} opts - options
   * @param {Function} opts.visible - filter for records the caller may use
   * @param {Number} opts.perClass - method names listed per class
   * @param {String} [opts.tag] - restrict to a tag or API class (lists all)
   * @return {Array} groups
   */
  browse(opts: {
    visible: (r: MethodRecord) => boolean;
    perClass: number;
    tag?: string;
  }): Array<{
    api: string;
    tags: string[];
    methodCount: number;
    methods: Array<{ id: string; summary?: string }>;
    more: number;
  }> {
    const groups = new Map<string, MethodRecord[]>();
    for (const r of this.records) {
      if (!opts.visible(r)) {
        continue;
      }
      if (opts.tag && !r.tags.includes(opts.tag) && r.className !== opts.tag) {
        continue;
      }
      groups.set(r.className, [...(groups.get(r.className) ?? []), r]);
    }
    const limit = opts.tag ? 100 : opts.perClass;
    return [...groups.entries()]
      .sort(([a], [b]) => a.localeCompare(b))
      .map(([api, rs]) => ({
        api,
        tags: [...new Set(rs.flatMap((r) => r.tags))],
        methodCount: rs.length,
        methods: rs
          .slice(0, limit)
          .map((r) => ({ id: r.id, summary: r.summary })),
        more: Math.max(0, rs.length - limit),
      }));
  }
}