Spaces:
Running
Running
Sync from felladrin/MiniSearch@f00ca4d (part 2)
Browse filesFiles changed (2)
12345237 unmodified lines243244245import { describe, expect, it } from "vitest";import { normalizeRelativeDates } from "./relativeDates";/** 3 November 2026, midday. Every expectation below is relative to this. */const AS_OF = new Date(2026, 10, 3, 12);237 unmodified linesexpect(searchQuery.length).toBeLessThanOrEqual(2000);});});12345678237 unmodified lines246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331import { describe, expect, it } from "vitest";import {normalizeFollowUpDates,normalizeRelativeDates,} from "./relativeDates";/** 3 November 2026, midday. Every expectation below is relative to this. */const AS_OF = new Date(2026, 10, 3, 12);237 unmodified linesexpect(searchQuery.length).toBeLessThanOrEqual(2000);});});describe("normalizeFollowUpDates", () => {it.each([["who won it last year","who won world series 2026","who won world series 2025",],["what comes out next month","Disney Plus new releases November 2026","Disney Plus new releases December 2026",],["what comes out next month","disney plus new releases november 2026","disney plus new releases December 2026",],["明年的呢", "苹果 2025 新手机 发布时间", "苹果 新手机 发布时间 2027年"],["明年的呢", "iPhone 16发布时间 2027", "iPhone 16发布时间 2027年"],["明年的呢", "苹果 2026年11月 发布会", "苹果 发布会 2027年"],["明年的呢", "2025 年 iPhone 发布日期新闻", "iPhone 发布日期新闻 2027年"],["what happened last year","WWE Crown Jewel incidents 2023-2025","WWE Crown Jewel incidents 2025",],["who won it last year","Premier League winner 2024-25","Premier League winner 2025",],])("anchors %s onto %s", (question, query, searchQuery) => {expect(normalizeFollowUpDates(query, [question], AS_OF).searchQuery).toBe(searchQuery,);});it("keeps a year the user typed in the question", () => {expect(normalizeFollowUpDates("2024 model vs 2023 model",["is the 2024 model better than last year's"],AS_OF,).searchQuery,).toBe("2024 model vs model 2025");});it("keeps a year the user typed in an earlier message", () => {expect(normalizeFollowUpDates("Cyberpunk 2077 sales 2026",["cyberpunk 2077", "how did it sell last year"],AS_OF,).searchQuery,).toBe("Cyberpunk 2077 sales 2025");});it("keeps a number after a spaced dash, which is not a season", () => {expect(normalizeFollowUpDates("best TVs 2026 - 55 inch",["what were the best TVs last year"],AS_OF,).searchQuery,).toBe("best TVs - 55 inch 2025");});it("leaves the model's dates alone when the question has no relative date", () => {expect(normalizeFollowUpDates("world series 2023 winner", ["who won it"], AS_OF).searchQuery,).toBe("world series 2023 winner");});it("drops an anchor that would pass the length limit", () => {expect(normalizeFollowUpDates("who won 2026", ["who won it last year"], AS_OF, {maxLength: 9,}).searchQuery,).toBe("who won");});});
275 unmodified lines276277278279280281282283284285286287288289290291292293294295296297298299300301275 unmodified linesselected.push(hit);}if (selected.length === 0) return unchanged;const replacements = selected.map(({ phrase, anchor }) => ({phrase,anchor,}));// The search endpoint rejects an over-long query, so an anchor that would// push it past the limit is dropped rather than turning a search into a// failed request. No anchor left over means the query goes out untouched.const limit = options?.maxLength ?? Number.POSITIVE_INFINITY;const appended: RelativeDateReplacement[] = [];let searchQuery = query.trimEnd();for (const replacement of replacements) {const candidate = `${searchQuery} ${replacement.anchor}`;if (candidate.length > limit) break;searchQuery = candidate;appended.push(replacement);}if (appended.length === 0) return unchanged;return { searchQuery, replacements: appended };}275 unmodified lines276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352275 unmodified linesselected.push(hit);}return appendAnchors(query,selected.map(({ phrase, anchor }) => ({ phrase, anchor })),options?.maxLength,);}function appendAnchors(query: string,replacements: RelativeDateReplacement[],maxLength = Number.POSITIVE_INFINITY,): { searchQuery: string; replacements: RelativeDateReplacement[] } {// The search endpoint rejects an over-long query, so an anchor that would// push it past the limit is dropped rather than turning a search into a// failed request. No anchor left over means the query goes out untouched.const appended: RelativeDateReplacement[] = [];let searchQuery = query.trimEnd();for (const replacement of replacements) {const candidate = `${searchQuery} ${replacement.anchor}`;if (candidate.length > maxLength) break;searchQuery = candidate;appended.push(replacement);}if (appended.length === 0) return { searchQuery: query, replacements: [] };return { searchQuery, replacements: appended };}const YEAR = "(?<!\\d)(?:19|20)\\d{2}(?!\\d)";/*** A year a model writes into a query, alone, as a range or a season, after a* month name, or glued to Chinese text: "2023", "2023-2025", "2024-25",* "November 2026", "2026年11月", "2025 年".*/const WRITTEN_DATE = new RegExp(`(?:\\b(?:${MONTH_NAMES.join("|")})\\s+)?${YEAR}(?:\\s*[-–/]\\s*${YEAR}|[-–/]\\d{2}(?!\\d))?(?:\\s*年(?:\\d{1,2}月)?)?`,"gi",);/*** Anchors the relative dates of the question the user typed onto the query a* model wrote for it. The model often replaces "last year" with a year of its* own, and a wrong one ("2026" on 2026-11-19), which leaves nothing for the* anchor to resolve. So when the question (the last of `userMessages`) has a* relative date, the years the model wrote are dropped, and the question's* anchors are appended. A year the user typed in any message is kept, since* it can be part of a name ("Cyberpunk 2077", "RTX 2080").*/export function normalizeFollowUpDates(query: string,userMessages: readonly string[],asOfDate: Date,options?: { maxLength?: number },): { searchQuery: string; replacements: RelativeDateReplacement[] } {const question = userMessages.at(-1) ?? "";const { replacements } = normalizeRelativeDates(question, asOfDate);if (replacements.length === 0)return normalizeRelativeDates(query, asOfDate, options);const typedYears = userMessages.join("\n").match(new RegExp(YEAR, "g")) ?? [];const queryWithoutModelDates = query.replace(WRITTEN_DATE, (date) =>typedYears.some((year) => date.includes(year)) ? date : "",).replace(/\s+/g, " ").trim();return appendAnchors(queryWithoutModelDates,replacements,options?.maxLength,);}
shared/relativeDates.test.ts+87-1
shared/relativeDates.ts+60-9