From 5e0b20cad6dfa70fc8183869476988505fdb81da Mon Sep 17 00:00:00 2001 From: sickerine Date: Tue, 24 Nov 2026 08:00:26 +0000 Subject: [PATCH] fix(assistant): a paper with no link of its own is cited at the address its PDF came from --- convex/lib/paperPdfs.ts | 4 ++-- convex/research/paperPdf.test.ts | 32 ++++++++++++++++++++------------ convex/research/papers.ts | 5 +++-- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/convex/lib/paperPdfs.ts b/convex/lib/paperPdfs.ts index 7efcefc46..0e931f052 100644 --- a/convex/lib/paperPdfs.ts +++ b/convex/lib/paperPdfs.ts @@ -49,7 +49,7 @@ export type PdfOptions = { maxBytes: number; bytes: boolean; charge: "self" | "r * The PDF and where it came from, with the credits it cost the scope (charged already, or for the caller to charge), * or why there's none: `credits` when the person can't pay for it. */ -export type PaperPdf = { bytes: Uint8Array | null; key: string | null; from: "stored" | "link" | "openalex"; credits: number } | { error: string; credits?: true }; +export type PaperPdf = { bytes: Uint8Array | null; key: string | null; from: "stored" | "link" | "openalex"; credits: number; /** Where a copy fetched by a link came from. */ url?: string } | { error: string; credits?: true }; function store() { const { PAPER_PDFS_ENDPOINT: endpoint, PAPER_PDFS_BUCKET: bucket, PAPER_PDFS_ACCESS_KEY_ID: accessKeyId, PAPER_PDFS_SECRET_ACCESS_KEY: secretAccessKey } = process.env; @@ -310,7 +310,7 @@ export async function paperPdf(ctx: ActionCtx, payer: PdfPayer, paper: PaperRef, if (!options.bytes && !kept) return { error: NOT_KEPT }; // The scope fetched it for nothing, so a later read in it from the store is free too. if (kept) await note(ctx, payer, key); - return { bytes, key: kept ? key : null, from: "link", credits: 0 }; + return { bytes, key: kept ? key : null, from: "link", url: foundAt, credits: 0 }; } failed = bytes ? "The paper's PDF address served something that isn't a PDF." : (error ?? "The paper's PDF couldn't be downloaded."); } diff --git a/convex/research/paperPdf.test.ts b/convex/research/paperPdf.test.ts index f4a4cce08..a91366f76 100644 --- a/convex/research/paperPdf.test.ts +++ b/convex/research/paperPdf.test.ts @@ -94,6 +94,15 @@ const pdfCharges = (t: Test) => t.run(async (ctx) => (await ctx.db.query("billin const keep = (key: string) => fakeStore.objects.set(key, new TextEncoder().encode(PDF_BYTES)); /** The key a copy fetched by a link is kept under. */ const byLink = async (url: string) => (await paperKeys({ url }))[0]; +/** Crossref holds a link to PDF for `doi`; every other request is answered as before. */ +function crossrefHolds(doi: string) { + const answer = fetchMock.getMockImplementation()!; + fetchMock.mockImplementation(async (input: RequestInfo | URL, init?: RequestInit) => { + const url = new URL(input instanceof Request ? input.url : String(input)); + if (url.hostname !== "api.crossref.org") return answer(input, init); + return new Response(JSON.stringify({ message: { DOI: doi, link: [{ URL: PDF, "content-type": "application/pdf" }] } }), { status: 200, headers: { "content-type": "application/json" } }); + }); +} /** A chat on a model that reads PDFs, with reading files allowed in it, so a read runs without asking. */ async function chat(t: Test, free = 100_000, inputs = ["text", "image", "file"]) { @@ -145,12 +154,7 @@ describe("extraction's PDFs from the store", () => { it("finds a copy Crossref holds when the paper's own link gives none", async () => { const t = convexTest(schema); - const answer = fetchMock.getMockImplementation()!; - fetchMock.mockImplementation(async (input: RequestInfo | URL, init?: RequestInit) => { - const url = new URL(input instanceof Request ? input.url : String(input)); - if (url.hostname !== "api.crossref.org") return answer(input, init); - return new Response(JSON.stringify({ message: { DOI: "10.1/held", link: [{ URL: PDF, "content-type": "application/pdf" }] } }), { status: 200, headers: { "content-type": "application/json" } }); - }); + crossrefHolds("10.1/held"); const landing = "https://example.org/articles/held"; const found = await paperPdf(actionsOf(t), payer("j1"), { openalexId: "W9", doi: "10.1/held", url: landing }, EXTRACTION); @@ -169,12 +173,7 @@ describe("extraction's PDFs from the store", () => { it("keeps a copy found by DOI for a paper with no link, under the address the copy came from", async () => { const t = convexTest(schema); - const answer = fetchMock.getMockImplementation()!; - fetchMock.mockImplementation(async (input: RequestInfo | URL, init?: RequestInit) => { - const url = new URL(input instanceof Request ? input.url : String(input)); - if (url.hostname !== "api.crossref.org") return answer(input, init); - return new Response(JSON.stringify({ message: { DOI: "10.1/nolink", link: [{ URL: PDF, "content-type": "application/pdf" }] } }), { status: 200, headers: { "content-type": "application/json" } }); - }); + crossrefHolds("10.1/nolink"); // As the assistant reads: it is sent the stored copy, so a copy that can't be kept is no use to it. const found = await paperPdf(actionsOf(t), payer("j1"), { openalexId: "W12", doi: "10.1/nolink" }, { ...EXTRACTION, bytes: false }); @@ -244,6 +243,15 @@ describe("reading a paper's PDF", () => { expect(content).toEqual([]); }); + it("cites a paper with no link of its own at the address its copy was found at, so a page citation opens the PDF", async () => { + const t = convexTest(schema); + const { asAda } = await chat(t); + WORKS.W12 = { id: "https://openalex.org/W12", display_name: "No link", doi: "https://doi.org/10.1/nolink" }; + crossrefHolds("10.1/nolink"); + + expect(await asAda.action(api.research.papers.readPdf, { id: "W12" })).toMatchObject({ paperPdf: PDF, stored: await byLink(PDF) }); + }); + it("buys OpenAlex's copy when the paper's own link fails, charging what OpenAlex reports, and serves it from the store after", async () => { const t = convexTest(schema); const { asAda } = await chat(t); diff --git a/convex/research/papers.ts b/convex/research/papers.ts index ef2dc7f3d..09afd2001 100644 --- a/convex/research/papers.ts +++ b/convex/research/papers.ts @@ -244,9 +244,10 @@ export const readPdfAction = defineUserAction({ // The model is sent the stored copy, never the file through here. const got = await paperPdf(ctx, { userId, workspaceId, scope: await chatScope(ctx, call), kind: "chat" }, { openalexId, doi: work.doi ?? undefined, url: pdfUrl ?? undefined, hasCopy: work.has_content?.pdf === true }, { maxBytes: PROVIDER_PDF_BYTES, bytes: false, charge: "self", allowHttp: true }); if ("error" in got) throw createError(got.credits ? "INSUFFICIENT_CREDITS" : "VALIDATION_ERROR", `${got.error} Ask the person to attach its PDF to the chat.`); - // A paper read from OpenAlex's copy alone is cited by its record, the only address of it a person can open. if (!got.key) throw createError("VALIDATION_ERROR", "This paper's PDF can't be kept to send. Ask the person to attach its PDF to the chat."); - return { id: openalexId, title, paperPdf: pdfUrl ?? openalexId, stored: got.key }; + // A paper with no link of its own is cited at the address its copy was found at; one read from OpenAlex's copy + // alone by its record, the only address of it a person can open. + return { id: openalexId, title, paperPdf: pdfUrl ?? got.url ?? openalexId, stored: got.key }; }, }); /** A record of a library OpenScholar runs, from its page's address (`/lib/`); null for any other address. */