Server: one summary builder for both Wikipedia APIs, one reset-time conversion

summary() and summaryFromAction() assembled the same record field by field; the subscription
code converted reset times the way usage.ts's epochIso already does; attribution() spelled out
restTitle(). The "|0" cache-key comment says why it stays rather than how it came about.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
This commit is contained in:
thalladaandClaude Opus 5.5 committed 2026-11-17 16:07:12 -05:00
1 parent 63593fdb22
commit 7f6b9d8e77
3 files changed
+14 -26

No files matched your search

+2 -3
View File
@@ -25,7 +25,7 @@ import { TOOLS } from "../src/llm/tasks";
import type { Block, JsonReq, LLM, Req, Turn, TurnReq } from "../src/llm/types";
import { normUrl, type Research, type WebSource, type WebStep } from "../src/llm/web";
import { schemas } from "./schemas";
import { usage } from "./usage";
import { epochIso, usage } from "./usage";
/** A failed call, with the SDK's own words and an HTTP status for the browser. */
export class SubscriptionError extends Error {
@@ -91,8 +91,7 @@ export const bare = (model: string): Options => ({
permissionMode: "dontAsk",
});
const resetTime = (info?: SDKRateLimitInfo) =>
info?.resetsAt ? ` It resets at ${new Date(info.resetsAt * (info.resetsAt < 1e12 ? 1000 : 1)).toLocaleString()}.` : "";
const resetTime = (info?: SDKRateLimitInfo) => (info?.resetsAt ? ` It resets at ${new Date(epochIso(info.resetsAt)!).toLocaleString()}.` : "");
interface Call {
system: string;
+2 -2
View File
@@ -100,8 +100,8 @@ const LABELS: Record<string, string> = {
};
const labelOf = (id: string) => LABELS[id] ?? (id.startsWith("model:") ? `weekly ${id.slice(6)}` : id.replaceAll("_", " "));
/** SDK reset times are unix seconds (the subscription code accepts milliseconds too, defensively). */
const epochIso = (t: number | undefined | null) => (typeof t === "number" && Number.isFinite(t) ? new Date(t < 1e12 ? t * 1000 : t).toISOString() : null);
/** SDK reset times are unix seconds (milliseconds accepted). */
export const epochIso = (t: number | undefined | null) => (typeof t === "number" && Number.isFinite(t) ? new Date(t < 1e12 ? t * 1000 : t).toISOString() : null);
const isoOrNull = (s: string | null | undefined) => (s && !Number.isNaN(Date.parse(s)) ? new Date(s).toISOString() : null);
const finite = (n: unknown): n is number => typeof n === "number" && Number.isFinite(n);
/** A fraction as a percentage, without float noise (0.96 → 96, not 96.00000000000001). */
+10 -21
View File
@@ -587,7 +587,7 @@ export class WikiClient {
}
attribution(title: string, revisionId: number | null, modified: string | null): Attribution {
const enc = encodeURIComponent(title.replace(/ /g, "_"));
const enc = this.restTitle(title);
return {
title,
url: this.articleUrl(title),
@@ -785,22 +785,7 @@ export class WikiClient {
if (r.ok && r.status === 200 && typeof r.json?.title === "string") {
const j = r.json;
const t = String(j.titles?.normalized ?? j.title);
const rev = j.revision ? Number(j.revision) : null;
return {
ok: true,
cached: false,
value: {
title: t,
pageId: j.pageid ?? null,
description: j.description ?? null,
extract: String(j.extract ?? ""),
thumbnail: j.thumbnail ? { url: absolute(j.thumbnail.source)!, width: j.thumbnail.width, height: j.thumbnail.height } : null,
url: j.content_urls?.desktop?.page ?? this.articleUrl(t),
revisionId: rev,
disambiguation: j.type === "disambiguation",
attribution: this.attribution(t, rev, null),
},
};
return this.summaryOf(t, j.revision ? Number(j.revision) : null, j, j.content_urls?.desktop?.page ?? this.articleUrl(t), j.type === "disambiguation");
}
if (!r.ok && r.reason !== "bad-response" && r.reason !== "server") return r;
return this.summaryFromAction(p.title, o);
@@ -829,7 +814,11 @@ export class WikiClient {
if (!pg) return unavailable("bad-response", "no page in answer");
if (pg.missing || pg.invalid) return { ok: false, reason: "missing", message: `No article “${title}”` };
const t = String(pg.title);
const rev = pg.lastrevid ?? null;
return this.summaryOf(t, pg.lastrevid ?? null, pg, this.articleUrl(t), pg.pageprops?.disambiguation !== undefined);
}
/** A summary from either API's page record (the REST summary or an action=query page). */
private summaryOf(t: string, rev: number | null, pg: { pageid?: number; description?: string; extract?: unknown; thumbnail?: { source: string; width: number; height: number } }, url: string, disambiguation: boolean): Result<Summary> {
return {
ok: true,
cached: false,
@@ -839,9 +828,9 @@ export class WikiClient {
description: pg.description ?? null,
extract: String(pg.extract ?? ""),
thumbnail: pg.thumbnail ? { url: absolute(pg.thumbnail.source)!, width: pg.thumbnail.width, height: pg.thumbnail.height } : null,
url: this.articleUrl(t),
url,
revisionId: rev,
disambiguation: pg.pageprops?.disambiguation !== undefined,
disambiguation,
attribution: this.attribution(t, rev, null),
},
};
@@ -852,7 +841,7 @@ export class WikiClient {
private async loadArticle(title: string, o: CallOpts): Promise<Result<CachedArticle>> {
const p = prepareTitle(title);
if (!("title" in p)) return { ok: false, reason: "missing", message: p.invalid };
// "|0": articles were once cached with and without images; only without remains.
// The "|0" suffix keeps existing cache files valid.
const key = `${p.title}|0`;
const hit = this.articles.get(key);
if (hit?.fresh) return { ok: true, value: hit.value, cached: true };