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.
This commit is contained in:
1 parent
c1ab7c6099
commit
7e4fbd7c0d
1 file changed
+18
@@ -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<FindQuery> => {
|
||||
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;
|
||||
};
|
||||
Reference in new issue
Block a user