From e870c3c7ec37c1231103a14dfdf2ba4496ccbf12 Mon Sep 17 00:00:00 2001 From: kim Date: Mon, 19 Oct 2026 11:02:04 +0900 Subject: [PATCH] =?UTF-8?q?weather=5Fopenmeteo:=20=EA=B5=AD=EB=82=B4=20?= =?UTF-8?q?=EC=9C=84=EC=B9=98=EB=8A=94=20=EA=B1=B0=EB=B6=80=ED=95=98?= =?UTF-8?q?=EA=B3=A0=20weather=5Fkma=EB=A1=9C=20=EC=95=88=EB=82=B4(?= =?UTF-8?q?=EC=98=88=EC=99=B8:=20days>10=C2=B7hours>240=20=EC=9E=A5?= =?UTF-8?q?=EA=B8=B0=20=EA=B5=AC=EA=B0=84)=20=E2=80=94=20=EA=B5=AD?= =?UTF-8?q?=EB=82=B4=20=EC=98=88=EB=B3=B4=EB=8A=94=20=EA=B8=B0=EC=83=81?= =?UTF-8?q?=EC=B2=AD=20=EB=8B=A8=EA=B8=B0+=EC=A4=91=EA=B8=B0=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=BC=EC=9B=90=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 --- src/gateway/chat/build-tools.ts | 2 +- src/tools/weather.ts | 15 +++++++++++++++ tests/weather-multi-location.test.ts | 4 ++-- tests/weather-openmeteo-korea.test.ts | 16 ++++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 tests/weather-openmeteo-korea.test.ts diff --git a/src/gateway/chat/build-tools.ts b/src/gateway/chat/build-tools.ts index 5cb13b7..c247df4 100644 --- a/src/gateway/chat/build-tools.ts +++ b/src/gateway/chat/build-tools.ts @@ -919,7 +919,7 @@ export function createBuildTools(isOrchestrationSkillEnabled: () => boolean) { type: 'function', function: { name: 'weather_openmeteo', - description: 'Forecast tool for locations OUTSIDE Korea. For ANY Korean location ALWAYS call weather_kma FIRST — no exceptions (KMA is the authority domestically); use this tool for a Korean location only in addition to weather_kma, and only for UV index or long hourly detail. This includes "이번 주"/weekend/week-ahead questions: weather_kma now returns days 1-3 from KMA plus days 4-10 from the official KMA mid-range forecast (2026-10-14). Open-Meteo (free, no key): precipitation probability, hourly breakdown, UV index, ECMWF/GFS models, up to 16 days. For a Korean location call weather_kma first (any range up to 10 days) — it is observation-backed and its numbers differ materially (verified 2026-07-29: 20% vs 0% rain, highs 1.5-2.5°C apart for the same day). Never use web_search for weather.', + description: 'Forecast tool for locations OUTSIDE Korea. For ANY Korean location use weather_kma — this tool REFUSES Korean locations (error) unless days>10 / hours>240 is requested for a long-range outlook beyond the KMA 10-day forecast. This includes "이번 주"/weekend/week-ahead questions: weather_kma now returns days 1-3 from KMA plus days 4-10 from the official KMA mid-range forecast (2026-10-14). Open-Meteo (free, no key): precipitation probability, hourly breakdown, UV index, ECMWF/GFS models, up to 16 days. For a Korean location call weather_kma first (any range up to 10 days) — it is observation-backed and its numbers differ materially (verified 2026-07-29: 20% vs 0% rain, highs 1.5-2.5°C apart for the same day). Never use web_search for weather.', parameters: { type: 'object', required: ['location'], properties: { diff --git a/src/tools/weather.ts b/src/tools/weather.ts index 8b791bc..76f9364 100644 --- a/src/tools/weather.ts +++ b/src/tools/weather.ts @@ -912,6 +912,17 @@ async function withKmaMidTerm(text: string, lat: number, lon: number, days: numb } } +// 국내 위치는 weather_kma가 맡는다(관측망이 촘촘하고 4~10일 중기예보까지 같이 준다). Open-Meteo는 해외용이다. +// 예외는 10일을 넘는 구간(days>10 또는 hours>240) — 기상청 예보가 닿지 않는 장기 구간뿐이다. +// 2026-10-19 사용자 결정: "weather_openmeteo는 국내 검색에서 제외, 장기예보 외에는". +const KOREA_BBOX = { latMin: 33.0, latMax: 38.7, lonMin: 124.5, lonMax: 131.9 }; +export function isKoreanLatLon(lat: number, lon: number): boolean { + return lat >= KOREA_BBOX.latMin && lat <= KOREA_BBOX.latMax && lon >= KOREA_BBOX.lonMin && lon <= KOREA_BBOX.lonMax; +} +export function openMeteoKoreaBlockMessage(place: string): string { + return `${place}은(는) 국내 지역입니다. 국내 날씨·예보(3일 단기 + 4~10일 중기)는 weather_kma로 조회하세요 — 이 도구(weather_openmeteo)는 해외 지역과 10일을 넘는 장기 예보(days>10)에만 씁니다.`; +} + export const weatherOpenMeteoTool = { name: 'weather_openmeteo', description: 'Get weather forecast using Open-Meteo API (free, no API key). Powered by ECMWF, GFS, and other NWP models. Times are returned in the LOCATION\'s local timezone. For "today\'s high/low" use daily variables (temperature_2m_max, temperature_2m_min, precipitation_sum, …) — the hourly series is a rolling window from now and spans two calendar days, so its maximum is NOT the daily high. Hourly variables (temperature_2m, precipitation, wind_speed_10m, uv_index, weather_code, …) are for intra-day detail. Up to 16 days.', @@ -991,15 +1002,18 @@ export const weatherOpenMeteoTool = { }; try { + const longRange = days > 10 || hours > 240; const coords = parseCoords(location); if (coords) { const [lat, lon] = coords; + if (!longRange && isKoreanLatLon(lat, lon)) return { success: false, error: openMeteoKoreaBlockMessage(location) }; const { text, raw } = await fetchOneCity(lat, lon, location); return { success: true, stdout: await withKmaMidTerm(text, lat, lon, days, hours), data: { location, lat, lon, raw } }; } const geo = await geocodeCity(location); if (geo) { + if (!longRange && isKoreanLatLon(geo.lat, geo.lon)) return { success: false, error: openMeteoKoreaBlockMessage(geo.displayName || location) }; const { text, raw } = await fetchOneCity(geo.lat, geo.lon, geo.displayName); return { success: true, stdout: await withKmaMidTerm(text, geo.lat, geo.lon, days, hours), data: { location: geo.displayName, lat: geo.lat, lon: geo.lon, raw } }; } @@ -1016,6 +1030,7 @@ export const weatherOpenMeteoTool = { const settled = await Promise.allSettled(cities.map(async (c) => { const g = await geocodeCity(c); if (!g) throw new Error(`${c}: 위치를 찾을 수 없음`); + if (!longRange && isKoreanLatLon(g.lat, g.lon)) throw new Error(`${c}: 국내 지역은 weather_kma로 조회`); const { text } = await fetchOneCity(g.lat, g.lon, g.displayName); return { city: c, text }; })); diff --git a/tests/weather-multi-location.test.ts b/tests/weather-multi-location.test.ts index d7175e6..4604156 100644 --- a/tests/weather-multi-location.test.ts +++ b/tests/weather-multi-location.test.ts @@ -80,7 +80,7 @@ describe('배치 입력은 도구가 직접 전부 조회해 하나로 합친다 describe('단일 위치는 목록으로 오해하지 않는다', () => { test('"City, Country"는 정상 조회된다', async (t) => { if (!online) return t.skip('offline'); - assert.equal((await ask('Seoul, Korea')).success, true); + assert.equal((await ask('Paris, France')).success, true); }); test('"City,CountryCode"는 정상 조회된다', async (t) => { @@ -90,7 +90,7 @@ describe('단일 위치는 목록으로 오해하지 않는다', () => { test('쉼표가 있는 좌표는 정상 조회된다', async (t) => { if (!online) return t.skip('offline'); - assert.equal((await ask('37.5,127.0')).success, true); + assert.equal((await ask('48.85,2.35')).success, true); }); }); diff --git a/tests/weather-openmeteo-korea.test.ts b/tests/weather-openmeteo-korea.test.ts new file mode 100644 index 0000000..3eb2112 --- /dev/null +++ b/tests/weather-openmeteo-korea.test.ts @@ -0,0 +1,16 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { isKoreanLatLon, weatherOpenMeteoTool } from '../src/tools/weather'; + +test('국내 좌표 판정', () => { + assert.equal(isKoreanLatLon(36.12, 128.34), true); // 구미 + assert.equal(isKoreanLatLon(33.5, 126.5), true); // 제주 + assert.equal(isKoreanLatLon(35.68, 139.69), false); // 도쿄 + assert.equal(isKoreanLatLon(40.71, -74.0), false); // 뉴욕 +}); + +test('국내 좌표는 장기(days>10)가 아니면 거부, 도구 설명에 weather_kma 안내', async () => { + const r = await weatherOpenMeteoTool.execute({ location: '36.12,128.34', days: 3 }); + assert.equal(r.success, false); + assert.match(String(r.error), /weather_kma/); +});