Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions convex/lib/paperPdfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
}
Expand Down
32 changes: 20 additions & 12 deletions convex/research/paperPdf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]) {
Expand Down Expand Up @@ -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);
Expand All @@ -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 });
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions convex/research/papers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (`<library>/lib/<key>`); null for any other address. */
Expand Down
Loading