Files
MyAiConfiguration/shared/qmd/qmd-lib.mjs
T
Julian LechnerandClaude Opus 5.5 eb7cecaaa2 feat(research): merge repository research and tune QMD search coverage
Research skills:
- Merge repository-research into research as a Repository section, so an
  open repository search loads one skill fewer; move its evals and update
  every reference.
- Tune semantic-search for QMD: one question per search, known domain
  terms in the query, --top-k 10 for survey questions, a small-repository
  exception, and a Coverage section stating what the index skips.

QMD search:
- Widen the index file mask to web templates and styles, .NET and WPF
  markup and project files, JVM build files, and more script modules, and
  reapply it to existing repository collections.
- Add qmd-search.mjs --get to read qmd:// hits from other collections.
- List the exact search command in the Claude sandbox excludedCommands,
  because the macOS/Linux sandbox blocks the localhost daemon and the
  index under the home directory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:15:44 +01:00

98 lines
3.8 KiB
JavaScript

// shared/qmd/qmd-lib.mjs
import { createHash } from 'node:crypto';
import { execFileSync } from 'node:child_process';
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
import { homedir } from 'node:os';
import { createRequire } from 'node:module';
import { dirname, join, resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
export const MODELS = {
embed: 'hf:Qwen/Qwen3-Embedding-4B-GGUF/Qwen3-Embedding-4B-Q4_K_M.gguf',
rerank: 'hf:giladgd/Qwen3-Reranker-4B-GGUF/Qwen3-Reranker-4B.Q4_K_M.gguf',
generate: 'hf:tobil/qmd-query-expansion-1.7B-gguf/qmd-query-expansion-1.7B-q4_k_m.gguf',
};
// Covers the stacks with rules in this configuration: web templates and styles, .NET and WPF markup and projects, and JVM builds.
export const MASK = '**/*.{md,py,cs,ts,tsx,mts,cts,js,jsx,mjs,cjs,go,rs,java,kt,kts,ps1,sh,json,yml,yaml,toml,xml,sql,'
+ 'html,css,scss,xaml,razor,cshtml,csproj,props,targets,gradle,properties}';
export function collectionName(repoPath) {
return 'repo-' + createHash('sha256').update(resolve(repoPath), 'utf8').digest('hex').slice(0, 12);
}
export const stateDir = (home = homedir()) => join(home, '.my-ai-configuration', 'qmd');
export const setupStatePath = (home = homedir()) => join(home, '.my-ai-configuration', 'qmd.json');
export function qmdConfigPath(env = process.env, home = homedir()) {
const dir = env.QMD_CONFIG_DIR || (env.XDG_CONFIG_HOME ? join(env.XDG_CONFIG_HOME, 'qmd') : join(home, '.config', 'qmd'));
return join(dir, 'index.yml');
}
export function qmdDbPath(env = process.env, home = homedir()) {
return join(env.XDG_CACHE_HOME || join(home, '.cache'), 'qmd', 'index.sqlite');
}
export function deviceEnv(device, env = process.env) {
const result = { ...env };
if (device === 'cpu') {
result.QMD_FORCE_CPU = '1';
delete result.QMD_LLAMA_GPU;
} else {
result.QMD_LLAMA_GPU = 'auto';
delete result.QMD_FORCE_CPU;
}
return result;
}
export function idleMinutes(env = process.env) {
const value = Number(env.QMD_IDLE_MINUTES);
return Number.isFinite(value) && value > 0 ? value : 30;
}
export function readJson(path, fallback) {
try {
return JSON.parse(readFileSync(path, 'utf8'));
} catch {
return fallback;
}
}
// Readers never see a half-written file: write a temp file next to the target, then rename it.
export function writeTextAtomic(path, text, options = {}) {
mkdirSync(dirname(path), { recursive: true });
const temporary = `${path}.${process.pid}.tmp`;
writeFileSync(temporary, text, options);
renameSync(temporary, path);
}
// The state directory lives in the user profile; mode 0600 restricts the token on POSIX.
export function writePrivateJson(path, value) {
writeTextAtomic(path, JSON.stringify(value, null, 2) + '\n', { mode: 0o600 });
}
let cachedPackageDir;
export function qmdPackageDir() {
if (process.env.QMD_PACKAGE_DIR) return process.env.QMD_PACKAGE_DIR;
if (!cachedPackageDir) {
const root = execFileSync('npm', ['root', '--global'], { encoding: 'utf8', shell: process.platform === 'win32', windowsHide: true }).trim();
cachedPackageDir = join(root, '@tobilu', 'qmd');
}
return cachedPackageDir;
}
export function importQmd(subpath = join('dist', 'index.js')) {
return import(pathToFileURL(join(qmdPackageDir(), subpath)).href);
}
// Resolve from QMD so both nested and hoisted dependency installations work.
export function importQmdDependency(name) {
const require = createRequire(pathToFileURL(join(qmdPackageDir(), 'package.json')));
return import(pathToFileURL(require.resolve(name)).href);
}
// Runs the QMD CLI through node without a shell, so free-text arguments are never re-parsed.
export function runQmdCli(args, options = {}) {
return execFileSync(process.execPath, [join(qmdPackageDir(), 'dist', 'cli', 'qmd.js'), ...args], { encoding: 'utf8', maxBuffer: 16 * 1024 * 1024, ...options });
}