From 7e4fbd7c0d3587a5dd8d5016de42e778616cca9b Mon Sep 17 00:00:00 2001 From: papajade55-debug Date: Sun, 23 Aug 2026 18:44:49 +0200 Subject: [PATCH] fix(ai): send full ISO datetimes to Immich search API Smart Search produced takenAfter/takenBefore as plain YYYY-MM-DD strings, but the Immich search API validates them as ISO 8601 datetimes and rejects a bare date with invalid_format / format: datetime. Every search containing a date failed with 'Failed to fetch assets'. Expand date-only values after the existing today-guardrail (which compares YYYY-MM-DD strings and must keep doing so): takenAfter to the start of the day, takenBefore to the end of the day so a single-day range such as 'videos yesterday' is not empty. Values that already carry a time component are left untouched. --- src/helpers/ai.helper.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/helpers/ai.helper.ts b/src/helpers/ai.helper.ts index 3930bed..e183316 100644 --- a/src/helpers/ai.helper.ts +++ b/src/helpers/ai.helper.ts @@ -19,6 +19,12 @@ interface FindQuery { const allowedTypes = ["IMAGE", "VIDEO", "AUDIO"]; +// Immich's search API validates takenAfter/takenBefore as full ISO 8601 +// datetimes and rejects a bare YYYY-MM-DD value with +// `invalid_format` / `format: "datetime"`, so date-only values must be expanded +// before they reach the API. +const isDateOnly = (value: string) => /^\d{4}-\d{2}-\d{2}$/.test(value); + const responseSchema = z.object({ query: z.string().describe("Gist of the query. Remove the details of tags"), @@ -107,5 +113,17 @@ export const parseFindQuery = async (query: string): Promise => { delete parsedResponse.takenBefore; } + // Expand date-only values into the datetimes Immich expects. This must run + // after the guardrail above, which compares plain YYYY-MM-DD strings. + // takenBefore uses the end of the day so that a single-day range (e.g. + // "videos yesterday", where both bounds are the same date) still covers the + // whole day instead of being an empty interval. + if (parsedResponse.takenAfter && isDateOnly(parsedResponse.takenAfter)) { + parsedResponse.takenAfter = `${parsedResponse.takenAfter}T00:00:00.000Z`; + } + if (parsedResponse.takenBefore && isDateOnly(parsedResponse.takenBefore)) { + parsedResponse.takenBefore = `${parsedResponse.takenBefore}T23:59:59.999Z`; + } + return removeNullOrUndefinedProperties(parsedResponse) as any as FindQuery; };