Files
wehub-resource-sync 979fb22d7c
CI / test (20, macos-latest) (push) Has been cancelled
CI / test (20, ubuntu-latest) (push) Has been cancelled
CI / test (22, macos-latest) (push) Has been cancelled
CI / test (22, ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:01:18 +08:00

37 lines
847 B
TypeScript

import type { Adapter, RankedDoc, Session } from "../types.js";
interface GrepState {
sessions: Session[];
}
function tokenize(s: string): string[] {
return s
.toLowerCase()
.replace(/[^a-z0-9_]+/g, " ")
.split(/\s+/)
.filter((t) => t.length > 2);
}
export const grepAdapter: Adapter<GrepState> = {
name: "grep",
async init(sessions) {
return { sessions };
},
async query(q, state, k) {
const terms = tokenize(q);
const scored: RankedDoc[] = [];
for (const s of state.sessions) {
const body = s.content.toLowerCase();
let hits = 0;
for (const t of terms) {
if (body.includes(t)) hits += 1;
}
if (hits > 0) {
scored.push({ sessionId: s.id, score: hits });
}
}
scored.sort((a, b) => b.score - a.score);
return scored.slice(0, k);
},
};