diff --git a/src/gateway/chat/handle-chat.ts b/src/gateway/chat/handle-chat.ts index 49fa372..4b6f19c 100644 --- a/src/gateway/chat/handle-chat.ts +++ b/src/gateway/chat/handle-chat.ts @@ -2573,7 +2573,7 @@ async function handleChat( // 국내 지명의 weather_openmeteo는 weather_kma로 되돌린다(weather-args.ts, 2026-10-14). // call.function을 직접 고쳐 이후 기록·실행·메시지 이력이 모두 kma 호출로 일관되게 한다. { - const _r = redirectOpenMeteoToKmaForKorea( + const _r = await redirectOpenMeteoToKmaForKorea( String(call.function?.name || ''), normalizeToolArgs(call.function?.arguments), new Set((tools || []).map((t: any) => String(t?.function?.name || ''))), diff --git a/src/gateway/chat/weather-args.ts b/src/gateway/chat/weather-args.ts index 34412df..d788fdd 100644 --- a/src/gateway/chat/weather-args.ts +++ b/src/gateway/chat/weather-args.ts @@ -1,4 +1,4 @@ -import { lookupKoreanPlace } from '../../tools/weather.js'; +import { lookupKoreanPlace, geocodeCity, KR_CITY_COORDS } from '../../tools/weather.js'; /** * weather_kma(및 weather) 호출의 type 보정 — 코드 백스톱(2026-10-10). @@ -39,12 +39,12 @@ export function correctWeatherTypeForFuture(toolName: string, args: any, current */ const OPENMETEO_ONLY_RE = /자외선|\buv\b|시간별|시간대별|hourly/i; -export function redirectOpenMeteoToKmaForKorea( +export async function redirectOpenMeteoToKmaForKorea( toolName: string, args: any, availableToolNames: ReadonlySet | readonly string[], currentMessage: string, -): { toolName: string; args: any } { +): Promise<{ toolName: string; args: any }> { const same = { toolName, args }; if (toolName !== 'weather_openmeteo') return same; if (!args || typeof args !== 'object') return same; @@ -57,7 +57,11 @@ export function redirectOpenMeteoToKmaForKorea( if (/^-?\d+(\.\d+)?\s*[,\s]\s*-?\d+(\.\d+)?$/.test(location)) return same; if (args.variables) return same; if (OPENMETEO_ONLY_RE.test(String(currentMessage || ''))) return same; - if (!lookupKoreanPlace(location)) return same; + // 지점표에 없는 지명(현풍·읍면 단위 등)은 지오코더로 나라를 확인한다. 한국이 아니면 그대로 둔다. + if (!lookupKoreanPlace(location) && !KR_CITY_COORDS[location.toLowerCase()]) { + const geo = await geocodeCity(location).catch(() => null); + if (!geo || !/대한민국|South Korea/i.test(geo.displayName)) return same; + } console.log(`[v2] weather_openmeteo: 국내 지명(${location}) → weather_kma(forecast)로 교정`); return { toolName: 'weather_kma', args: { location, type: 'forecast' } }; } diff --git a/src/tools/weather.ts b/src/tools/weather.ts index 4a7da6a..5a9f677 100644 --- a/src/tools/weather.ts +++ b/src/tools/weather.ts @@ -176,6 +176,12 @@ const windDirectionKo = (deg: number) => // Geocode city name → lat/lon using Open-Meteo geocoding (no API key required) // Falls back to a built-in lookup table for common Korean cities. export const KR_CITY_COORDS: Record = { + // 2026-10-14: Open-Meteo 지오코더가 "현풍"을 경주의 무명 지명(36.03, 129.24)으로 풀어 대구 달성군 + // 현풍읍 대신 70km 떨어진 곳 예보가 나갔다. 사용자가 실제로 쓰는 달성군 읍 단위를 직접 넣는다. + '현풍': [35.6955, 128.4477], '현풍읍': [35.6955, 128.4477], + '화원': [35.8026, 128.5008], '화원읍': [35.8026, 128.5008], + '다사': [35.8542, 128.4533], '다사읍': [35.8542, 128.4533], + '논공': [35.7467, 128.4325], '논공읍': [35.7467, 128.4325], '서울': [37.5665, 126.9780], 'seoul': [37.5665, 126.9780], '부산': [35.1796, 129.0756], 'busan': [35.1796, 129.0756], '인천': [37.4563, 126.7052], 'incheon': [37.4563, 126.7052], diff --git a/tests/weather-redirect.test.ts b/tests/weather-redirect.test.ts index 1311073..6c4d293 100644 --- a/tests/weather-redirect.test.ts +++ b/tests/weather-redirect.test.ts @@ -8,30 +8,35 @@ import { redirectOpenMeteoToKmaForKorea as r } from '../src/gateway/chat/weather const both = new Set(['weather_kma', 'weather_openmeteo']); describe('redirectOpenMeteoToKmaForKorea', () => { - test('국내 지명은 kma forecast로 바뀐다', () => { - const out = r('weather_openmeteo', { hours: 168, location: '구미' }, both, '다음주 구미 날씨'); + test('국내 지명은 kma forecast로 바뀐다', async () => { + const out = await r('weather_openmeteo', { hours: 168, location: '구미' }, both, '다음주 구미 날씨'); assert.equal(out.toolName, 'weather_kma'); assert.deepEqual(out.args, { location: '구미', type: 'forecast' }); }); - test('해외 도시는 그대로', () => { - const out = r('weather_openmeteo', { location: 'Tokyo' }, both, '도쿄 날씨'); + test('현풍 같은 읍 단위 지명도 kma로 바뀐다', async () => { + const out = await r('weather_openmeteo', { location: '현풍' }, both, '이번주 현풍 날씨'); + assert.equal(out.toolName, 'weather_kma'); + assert.deepEqual(out.args, { location: '현풍', type: 'forecast' }); + }); + test('해외 도시는 그대로', async () => { + const out = await r('weather_openmeteo', { location: 'Tokyo' }, both, '도쿄 날씨'); assert.equal(out.toolName, 'weather_openmeteo'); }); - test('자외선·시간별 요구는 그대로', () => { - assert.equal(r('weather_openmeteo', { location: '구미' }, both, '구미 자외선 지수').toolName, 'weather_openmeteo'); - assert.equal(r('weather_openmeteo', { location: '구미' }, both, '구미 시간별 날씨').toolName, 'weather_openmeteo'); + test('자외선·시간별 요구는 그대로', async () => { + assert.equal((await r('weather_openmeteo', { location: '구미' }, both, '구미 자외선 지수')).toolName, 'weather_openmeteo'); + assert.equal((await r('weather_openmeteo', { location: '구미' }, both, '구미 시간별 날씨')).toolName, 'weather_openmeteo'); }); - test('variables를 직접 지정하면 그대로', () => { - assert.equal(r('weather_openmeteo', { location: '구미', variables: 'uv_index' }, both, '구미 날씨').toolName, 'weather_openmeteo'); + test('variables를 직접 지정하면 그대로', async () => { + assert.equal((await r('weather_openmeteo', { location: '구미', variables: 'uv_index' }, both, '구미 날씨')).toolName, 'weather_openmeteo'); }); - test('kma가 이번 턴에 없으면 그대로', () => { - assert.equal(r('weather_openmeteo', { location: '구미' }, new Set(['weather_openmeteo']), '구미 날씨').toolName, 'weather_openmeteo'); + test('kma가 이번 턴에 없으면 그대로', async () => { + assert.equal((await r('weather_openmeteo', { location: '구미' }, new Set(['weather_openmeteo']), '구미 날씨')).toolName, 'weather_openmeteo'); }); - test('여러 도시·좌표는 그대로', () => { - assert.equal(r('weather_openmeteo', { location: '구미, 대구' }, both, '날씨').toolName, 'weather_openmeteo'); - assert.equal(r('weather_openmeteo', { location: '36.1,128.3' }, both, '날씨').toolName, 'weather_openmeteo'); + test('여러 도시·좌표는 그대로', async () => { + assert.equal((await r('weather_openmeteo', { location: '구미, 대구' }, both, '날씨')).toolName, 'weather_openmeteo'); + assert.equal((await r('weather_openmeteo', { location: '36.1,128.3' }, both, '날씨')).toolName, 'weather_openmeteo'); }); - test('다른 도구는 건드리지 않는다', () => { - assert.equal(r('weather_kma', { location: '구미' }, both, '날씨').toolName, 'weather_kma'); + test('다른 도구는 건드리지 않는다', async () => { + assert.equal((await r('weather_kma', { location: '구미' }, both, '날씨')).toolName, 'weather_kma'); }); });