Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
import type { Fetched, SourceSpec } from '@inkeep/open-knowledge-core/skills-catalog';
import { fetchSource, fetchSourceListing } from '@inkeep/open-knowledge-core/skills-catalog';
const TTL_MS = 30_000;
interface Entry {
readonly fetched: Promise<Fetched>;
readonly expiresAt: number;
}
const cache = new Map<string, Entry>();
function sourceKey(spec: SourceSpec): string {
if (spec.kind === 'git') return `git\n${spec.url}\n${spec.subpath ?? ''}`;
if (spec.kind === 'well-known') return `wk\n${spec.origin}\n${spec.skill}`;
return `local\n${spec.path}`;
}
function sweepExpired(now: number): void {
for (const [key, entry] of cache) {
if (entry.expiresAt > now) continue;
cache.delete(key);
void entry.fetched.then((f) => f.cleanup()).catch(() => {});
}
}
export async function fetchCachedSource(spec: SourceSpec): Promise<Fetched> {
if (spec.kind === 'local') return fetchSource(spec);
return cached(sourceKey(spec), () => fetchSource(spec));
}
export async function fetchCachedListing(spec: SourceSpec): Promise<Fetched> {
if (spec.kind !== 'git' || spec.subpath) return fetchCachedSource(spec);
return cached(`listing\n${sourceKey(spec)}`, () => fetchSourceListing(spec));
}
async function cached(key: string, fetchOnce: () => Promise<Fetched>): Promise<Fetched> {
const now = Date.now();
sweepExpired(now);
let entry = cache.get(key);
if (!entry) {
const fetched = fetchOnce();
entry = { fetched, expiresAt: now + TTL_MS };
cache.set(key, entry);
const self = entry;
fetched.catch(() => {
if (cache.get(key) === self) cache.delete(key);
});
}
const fetched = await entry.fetched;
return { dir: fetched.dir, ref: fetched.ref, cleanup: () => {} };
}
export async function clearSourceCache(): Promise<void> {
const entries = [...cache.values()];
cache.clear();
await Promise.all(entries.map((e) => e.fetched.then((f) => f.cleanup()).catch(() => {})));
}