diff --git a/src/gateway/chat/followup-hints.ts b/src/gateway/chat/followup-hints.ts index 44e34de..282d949 100644 --- a/src/gateway/chat/followup-hints.ts +++ b/src/gateway/chat/followup-hints.ts @@ -66,3 +66,28 @@ function placeCorrectionHint(msg: string, history: ReadonlyArray<{ role: string; if (!prevQ || !WEATHER_RE.test(prevQ)) return null; return `The user's short message "${msg}" is most likely a correction of the PLACE in their previous weather question ("${prevQ.slice(0, 80)}"): the place is "${m[1]}". Re-run the same kind of weather lookup (weather_kma for Korea) for that place and answer the previous question for it — do NOT switch to web search and do NOT ask which place they mean.`; } + +// 원화 환산 후속 질문(2026-10-25): 직전 답에 외화 금액이 있고 이번 말이 "원으로는 얼마나 되지?"처럼 원화를 +// 물으면, exchange_rate가 이번 턴 스키마에 있어도 모델(Haiku)이 "어떤 금액을 말씀하시는지…"라고 되묻고 +// "환율을 조회해서 계산해 드리겠습니다"로 끝냈다(직전 답의 달러 금액은 $1,000 하나뿐이었음). +const WON_ASK_RE = /원(?:으로|화로|화)|우리\s*돈|한화|\d\s*만\s*원|환산/; +const FOREIGN_AMOUNT_RE = /(US\$|\$|€|£|¥)\s?(\d[\d,]*(?:\.\d+)?)(?:\s*(?:~|-|–|to)\s*(?:US\$|\$|€|£|¥)?\s?(\d[\d,]*(?:\.\d+)?))?/g; +const SYMBOL_CODE: Record = { 'US$': 'USD', '$': 'USD', '€': 'EUR', '£': 'GBP', '¥': 'JPY' }; + +export function currencyFollowUpHint(message: string, lastAssistant: string): string | null { + const msg = String(message || '').trim(); + if (!msg || msg.length > MAX_FOLLOWUP_CHARS || !WON_ASK_RE.test(msg)) return null; + const byCode = new Map(); + for (const m of String(lastAssistant || '').matchAll(FOREIGN_AMOUNT_RE)) { + const code = SYMBOL_CODE[m[1]]; + const list = byCode.get(code) || []; + for (const raw of [m[2], m[3]]) { + const n = raw ? Number(raw.replace(/,/g, '')) : NaN; + if (Number.isFinite(n) && n > 0 && !list.includes(n) && list.length < 6) list.push(n); + } + byCode.set(code, list); + } + if (!byCode.size) return null; + const calls = [...byCode].map(([code, amts]) => `exchange_rate(from="${code}", to="KRW", amounts=[${amts.join(', ')}])`).join(' and '); + return `The user wants the foreign-currency amounts from your previous answer in Korean won. Call ${calls} now and answer with the converted figures next to the original ones, giving the rate date — do not ask which amount they mean and do not assume a rate.`; +} diff --git a/src/gateway/chat/handle-chat.ts b/src/gateway/chat/handle-chat.ts index cf0e0e1..e893364 100644 --- a/src/gateway/chat/handle-chat.ts +++ b/src/gateway/chat/handle-chat.ts @@ -87,7 +87,7 @@ import { selectToolsForTurn, bootAllowedTools, codeAiBlockedTools } from './tool import { buildChatSystemPrompt, groundingResultReadingRule } from './system-prompt'; import { decideRoute } from './model-routing'; import { weatherFabricationReplacement } from './tool-failure-backstop'; -import { weatherFollowUpHint } from './followup-hints'; +import { weatherFollowUpHint, currencyFollowUpHint } from './followup-hints'; import { fixKeyboardLayout } from './hangul-layout'; import { normalizeTypos } from './typo-normalize'; import { suspectSoundAlikePlace } from './voice-place-check'; @@ -1004,6 +1004,14 @@ async function handleChat( messages.push({ role: 'assistant', content: 'Understood.' }); messages.push({ role: 'user', content: `Note: ${_wfHint}` }); } + // 원화 환산 후속 질문: 직전 답의 외화 금액을 exchange_rate로 바로 환산하게 (followup-hints.ts, 2026-10-25) + const _fxHint = tools.some((t: any) => t?.function?.name === 'exchange_rate') + ? currencyFollowUpHint(message, String(lastAssistantMsg || '')) : null; + if (_fxHint) { + console.log('[v2] CURRENCY FOLLOW-UP HINT added'); + messages.push({ role: 'assistant', content: 'Understood.' }); + messages.push({ role: 'user', content: `Note: ${_fxHint}` }); + } } diff --git a/tests/followup-hints.test.ts b/tests/followup-hints.test.ts index 840f368..aa7287e 100644 --- a/tests/followup-hints.test.ts +++ b/tests/followup-hints.test.ts @@ -47,3 +47,22 @@ test('앞으로 올 비·무관한 말에는 과거 비 힌트가 붙지 않는 assert.ok(!h || !/ALREADY fell/.test(h), q); } }); + +import { describe } from 'node:test'; +import { currencyFollowUpHint } from '../src/gateway/chat/followup-hints'; + +describe('currencyFollowUpHint', () => { + test('직전 답의 달러 금액을 환산하라고 한다', () => { + const h = currencyFollowUpHint('원으로는 얼마나 되지?', '| Dillon | 월 임대료 중앙값 약 $1,000 |'); + assert.match(String(h), /exchange_rate\(from="USD", to="KRW", amounts=\[1000\]\)/); + assert.match(String(h), /do not ask which amount/); + }); + test('범위는 양 끝 모두', () => { + const h = currencyFollowUpHint('한달에 300 만원 정도란 거지?', '2베드룸은 월 $1,600~2,400 선입니다.'); + assert.match(String(h), /amounts=\[1600, 2400\]/); + }); + test('외화 금액이 없거나 원화를 묻지 않으면 없음', () => { + assert.equal(currencyFollowUpHint('원으로는 얼마나 되지?', '보즈먼은 집값이 높습니다.'), null); + assert.equal(currencyFollowUpHint('비싸군..', '중간가 $650,000'), null); + }); +});