Make Online Search Location Aware (#929)

## Overview
Add user country code as context for doing online search with serper.dev API.
This should find more user relevant results from online searches by Khoj

## Details
### Major
- Default to using system clock to infer user timezone on js clients
- Infer country from timezone when only timezone received by chat API
- Localize online search results to user country when location available

### Minor
- Add `__str__` func to `LocationData` class to deduplicate location string generation
This commit is contained in:
Debanjum
2024-10-03 12:33:47 -07:00
committed by GitHub
16 changed files with 95 additions and 53 deletions

View File

@@ -518,12 +518,14 @@ function EditCard(props: EditCardProps) {
updateQueryUrl += `&subject=${encodeURIComponent(values.subject)}`;
}
updateQueryUrl += `&crontime=${encodeURIComponent(cronFrequency)}`;
if (props.locationData) {
if (props.locationData && props.locationData.city)
updateQueryUrl += `&city=${encodeURIComponent(props.locationData.city)}`;
if (props.locationData && props.locationData.region)
updateQueryUrl += `&region=${encodeURIComponent(props.locationData.region)}`;
if (props.locationData && props.locationData.country)
updateQueryUrl += `&country=${encodeURIComponent(props.locationData.country)}`;
if (props.locationData && props.locationData.timezone)
updateQueryUrl += `&timezone=${encodeURIComponent(props.locationData.timezone)}`;
}
let method = props.createNew ? "POST" : "PUT";

View File

@@ -136,7 +136,9 @@ export default function Chat() {
const [uploadedFiles, setUploadedFiles] = useState<string[]>([]);
const [image64, setImage64] = useState<string>("");
const locationData = useIPLocationData();
const locationData = useIPLocationData() || {
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
};
const authenticatedData = useAuthenticatedData();
const isMobileWidth = useIsMobileWidth();
@@ -241,9 +243,10 @@ export default function Chat() {
conversation_id: conversationId,
stream: true,
...(locationData && {
city: locationData.city,
region: locationData.region,
country: locationData.country,
city: locationData.city,
country_code: locationData.countryCode,
timezone: locationData.timezone,
}),
...(image64 && { image: image64 }),

View File

@@ -2,13 +2,10 @@ import { useEffect, useState } from "react";
import useSWR from "swr";
export interface LocationData {
ip: string;
city: string;
region: string;
country: string;
postal: string;
latitude: number;
longitude: number;
city?: string;
region?: string;
country?: string;
countryCode?: string;
timezone: string;
}
@@ -50,9 +47,7 @@ export function useIPLocationData() {
{ revalidateOnFocus: false },
);
if (locationDataError) return null;
if (!locationData) return null;
if (locationDataError || !locationData) return;
return locationData;
}

View File

@@ -111,7 +111,9 @@ export default function SharedChat() {
const [paramSlug, setParamSlug] = useState<string | undefined>(undefined);
const [image64, setImage64] = useState<string>("");
const locationData = useIPLocationData();
const locationData = useIPLocationData() || {
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
};
const authenticatedData = useAuthenticatedData();
const isMobileWidth = useIsMobileWidth();
@@ -231,6 +233,7 @@ export default function SharedChat() {
region: locationData.region,
country: locationData.country,
city: locationData.city,
country_code: locationData.countryCode,
timezone: locationData.timezone,
}),
...(image64 && { image: image64 }),