Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.
/**
* Generates src/agent/agent-features/catalog.generated.ts by deterministically
* scanning the direct children of src/agent/agent-features/ for a feature.ts
* entrypoint. The catalog is a static module manifest only: it maps each
* feature directory name to that module's namespace so the explicit builtin
* assembly can call each `create*Feature` factory with that feature's own
* narrow dependencies. `--check` verifies the committed catalog is up to
* date without writing.
*/
import { readdir, readFile, stat, writeFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
export interface FeatureCatalogLayout {
/** Directory whose direct children are feature directories. */
readonly featuresRoot: string;
/** Generated manifest path. */
readonly target: string;
}
export const defaultCatalogLayout = (): FeatureCatalogLayout => {
const featuresRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../src/agent/agent-features",
);
return {
featuresRoot,
target: path.join(featuresRoot, "catalog.generated.ts"),
};
};
const compareCodePoints = (left: string, right: string): number =>
left < right ? -1 : left > right ? 1 : 0;
/**
* Sorted names of the direct-child directories under featuresRoot that
* contain a feature.ts file. Nested feature.ts files are not discovered.
*/
export const discoverFeatureNames = async (
featuresRoot: string,
): Promise<string[]> => {
const entries = await readdir(featuresRoot, { withFileTypes: true });
const names: string[] = [];
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const entrypoint = path.join(featuresRoot, entry.name, "feature.ts");
const marker = await stat(entrypoint).catch(() => undefined);
if (marker?.isFile()) names.push(entry.name);
}
return names.sort(compareCodePoints);
};
const toModuleIdentifier = (name: string): string => {
const identifier = name.replace(/-+([A-Za-z0-9])/g, (_match, character) =>
character.toUpperCase(),
);
if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) {
throw new Error(
`feature directory ${JSON.stringify(name)} cannot form a module identifier`,
);
}
return identifier;
};
const toMapKey = (name: string): string =>
/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) ? name : JSON.stringify(name);
export const renderFeatureCatalog = (
featureNames: readonly string[],
): string => {
const identifiers = new Map<string, string>();
for (const name of featureNames) {
const identifier = toModuleIdentifier(name);
const existing = identifiers.get(identifier);
if (existing !== undefined) {
throw new Error(
`feature directories ${JSON.stringify(existing)} and ${JSON.stringify(name)} produce the same module identifier`,
);
}
identifiers.set(identifier, name);
}
const imports = [...identifiers]
.map(
([identifier, name]) =>
`import * as ${identifier} from "./${name}/feature.js";`,
)
.join("\n");
const entries = [...identifiers]
.map(([identifier, name]) => ` ${toMapKey(name)}: ${identifier},`)
.join("\n");
return `// Generated by scripts/generate-feature-catalog.ts — do not edit.
${imports}
/**
* Static manifest of every agent-features module, keyed by feature directory
* name. The explicit builtin assembly calls each module's create*Feature
* factory with that feature's narrow dependencies; this manifest never
* instantiates features and never binds service dependencies itself.
*/
export const AGENT_FEATURE_MODULES = {
${entries}
} as const;
export type AgentFeatureModuleName = keyof typeof AGENT_FEATURE_MODULES;
`;
};
interface ResolvedCatalog {
readonly featureNames: string[];
readonly output: string;
}
const resolveExpectedCatalog = async (
layout: FeatureCatalogLayout,
): Promise<ResolvedCatalog> => {
const featureNames = await discoverFeatureNames(layout.featuresRoot);
return { featureNames, output: renderFeatureCatalog(featureNames) };
};
export interface CatalogGenerationResult {
readonly featureNames: string[];
/** Whether the target file differed and was rewritten. */
readonly changed: boolean;
}
/** Writes the manifest only when the discovered modules changed. */
export const generateFeatureCatalog = async (
layout: FeatureCatalogLayout = defaultCatalogLayout(),
): Promise<CatalogGenerationResult> => {
const { featureNames, output } = await resolveExpectedCatalog(layout);
const current = await readFile(layout.target, "utf8").catch(() => "");
if (current === output) return { featureNames, changed: false };
await writeFile(layout.target, output);
return { featureNames, changed: true };
};
export interface CatalogCheckResult {
readonly featureNames: string[];
readonly upToDate: boolean;
}
/** Reports whether the committed manifest matches the discovered modules. */
export const checkFeatureCatalog = async (
layout: FeatureCatalogLayout = defaultCatalogLayout(),
): Promise<CatalogCheckResult> => {
const { featureNames, output } = await resolveExpectedCatalog(layout);
const current = await readFile(layout.target, "utf8").catch(() => "");
return { featureNames, upToDate: current === output };
};
async function main(): Promise<void> {
if (process.argv.includes("--check")) {
const { featureNames, upToDate } = await checkFeatureCatalog();
if (!upToDate) {
console.error(
`agent-features catalog is stale (${String(featureNames.length)} feature modules discovered); run \`pnpm --filter @linger/node-services catalog:generate\``,
);
process.exitCode = 1;
}
return;
}
const { featureNames, changed } = await generateFeatureCatalog();
if (changed) {
console.log(
`catalog.generated.ts updated (${String(featureNames.length)} feature modules)`,
);
}
}
if (path.resolve(process.argv[1] ?? "") === fileURLToPath(import.meta.url)) {
main().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : String(error));
process.exitCode = 1;
});
}