Add resolver, server, PDF rendering and web app

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-18 09:46:46 -07:00
1 parent 6a7fcdc4b8
commit bdfdf581f3
59 files changed
+4088

No files matched your search

+128
View File
@@ -0,0 +1,128 @@
import type { Book, BookMeta, ResolveInput, Resolution, SearchResult } from '../shared/types.ts';
import type { Db } from './db.ts';
// Structural match for Resolver in resolve/resolver.ts (Task 2).
export interface ResolverLike {
resolve(input: ResolveInput, opts?: { onMeta?: (meta: BookMeta) => void }): Promise<Resolution>;
search(title: string, author: string): Promise<SearchResult[]>;
}
export interface Jobs { enqueue(id: number): void; resume(): void; idle(): Promise<void> }
// TemporaryError lives in resolve/resolver.ts; match by class name unless the caller injects a test.
function defaultIsTemporary(e: unknown): boolean {
for (let p = e != null && typeof e === 'object' ? Object.getPrototypeOf(e) : null; p; p = Object.getPrototypeOf(p)) {
if (p.constructor?.name === 'TemporaryError') return true;
}
return false;
}
function sleep(ms: number): Promise<void> {
if (ms <= 0) return Promise.resolve();
return new Promise((resolve) => { setTimeout(resolve, ms).unref(); });
}
export function createJobs(deps: {
db: Db; resolver: ResolverLike; onChange: (book: Book) => void;
retryDelaysMs?: number[]; isTemporary?: (e: unknown) => boolean;
}): Jobs {
const { db, resolver, onChange } = deps;
const delays = deps.retryDelaysMs ?? [30000, 120000, 600000];
const isTemporary = deps.isTemporary ?? defaultIsTemporary;
const queue: number[] = [];
const queued = new Set<number>();
let running: Promise<void> | null = null;
function emit(id: number, patch: Parameters<Db['updateBook']>[1]): void {
const b = db.updateBook(id, patch);
if (b) onChange(b);
}
function applyMeta(id: number, meta: BookMeta): void {
const cur = db.getBook(id);
if (!cur) return;
const patch: Parameters<Db['updateBook']>[1] = {
subtitle: meta.subtitle ?? cur.subtitle ?? undefined,
publisher: meta.publisher ?? cur.publisher ?? undefined,
year: meta.year ?? cur.year ?? undefined,
subjects: meta.subjects.length ? meta.subjects : cur.subjects,
lccn: meta.lccn ?? cur.lccn ?? undefined,
};
// A hand-edited book keeps the title and authors the user typed.
if (!cur.manualOverride) {
if (meta.title) patch.title = meta.title;
if (meta.authors.length) patch.authors = meta.authors;
}
emit(id, patch);
}
async function lookup(id: number): Promise<void> {
for (let attempt = 0; ; attempt++) {
const book = db.getBook(id);
if (!book || book.status !== 'pending') return;
const input: ResolveInput = {};
if (book.isbn13) input.isbn13 = book.isbn13;
if (book.lccn) input.lccn = book.lccn;
if (!book.isbn13) {
input.title = book.title;
if (book.authors[0]) input.author = book.authors[0];
}
if (book.year) input.year = book.year;
try {
const res = await resolver.resolve(input, { onMeta: (m) => applyMeta(id, m) });
const cur = db.getBook(id);
if (!cur) return;
if (res.meta) applyMeta(id, res.meta);
const dewey = res.dewey ?? cur.dewey ?? undefined;
if (cur.manualOverride) {
emit(id, { dewey });
} else if (res.callNumber) {
emit(id, {
callNumber: res.callNumber, sortKey: res.sortKey, confidence: res.confidence,
source: res.source, dewey, status: 'resolved', attentionReason: null,
});
} else {
emit(id, { dewey, status: 'needs_attention', attentionReason: res.reason ?? 'No Library of Congress number found' });
}
return;
} catch (e) {
const message = e instanceof Error ? e.message : String(e);
if (isTemporary(e) && attempt < delays.length) {
await sleep(delays[attempt]!);
continue;
}
const cur = db.getBook(id);
if (cur && !cur.manualOverride) {
emit(id, { status: 'needs_attention', attentionReason: 'Lookup failed: ' + message });
}
return;
}
}
}
async function drain(): Promise<void> {
while (queue.length) {
const id = queue.shift()!;
queued.delete(id);
try { await lookup(id); } catch { /* a failure on one book must not stop the queue */ }
}
}
function start(): void {
if (running) return;
running = drain().finally(() => { running = null; if (queue.length) start(); });
}
return {
enqueue(id) {
if (queued.has(id)) return;
queued.add(id);
queue.push(id);
start();
},
resume() { for (const id of db.listPendingIds()) this.enqueue(id); },
async idle() {
while (running || queue.length) await running;
},
};
}