From a025b69466f14235ee7c4cca88350e95c58fb3c6 Mon Sep 17 00:00:00 2001 From: kim Date: Sun, 25 Oct 2026 10:46:30 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9B=90=ED=99=94=20=ED=99=98=EC=82=B0=20?= =?UTF-8?q?=ED=9B=84=EC=86=8D=20=EC=A7=88=EB=AC=B8=20=ED=9E=8C=ED=8A=B8(CU?= =?UTF-8?q?RRENCY=20FOLLOW-UP=20HINT):=20=EC=A7=81=EC=A0=84=20=EB=8B=B5?= =?UTF-8?q?=EC=97=90=20=EC=99=B8=ED=99=94=20=EA=B8=88=EC=95=A1($=C2=B7?= =?UTF-8?q?=E2=82=AC=C2=B7=C2=A3=C2=B7=C2=A5,=20=EB=B2=94=EC=9C=84?= =?UTF-8?q?=EB=8A=94=20=EC=96=91=20=EB=81=9D)=EC=9D=B4=20=EC=9E=88?= =?UTF-8?q?=EA=B3=A0=20=EC=9D=B4=EB=B2=88=20=EB=A7=90=EC=9D=B4=20=EC=9B=90?= =?UTF-8?q?=ED=99=94=EB=A5=BC=20=EB=AC=BC=EC=9C=BC=EB=A9=B4=20exchange=5Fr?= =?UTF-8?q?ate(from,=20KRW,=20amounts=3D[=E2=80=A6])=EB=A5=BC=20=EC=A7=80?= =?UTF-8?q?=EA=B8=88=20=EB=B6=80=EB=A5=B4=EB=9D=BC=EA=B3=A0=20=EB=B6=99?= =?UTF-8?q?=EC=9E=84=20=E2=80=94=20"=EC=9B=90=EC=9C=BC=EB=A1=9C=EB=8A=94?= =?UTF-8?q?=20=EC=96=BC=EB=A7=88=EB=82=98=20=EB=90=98=EC=A7=80=3F"?= =?UTF-8?q?=EC=97=90=20exchange=5Frate=EA=B0=80=20=EC=8A=A4=ED=82=A4?= =?UTF-8?q?=EB=A7=88=EC=97=90=20=EC=9E=88=EC=97=88=EB=8A=94=EB=8D=B0?= =?UTF-8?q?=EB=8F=84=20"=EC=96=B4=EB=96=A4=20=EA=B8=88=EC=95=A1=EC=9D=84?= =?UTF-8?q?=20=EB=A7=90=EC=94=80=ED=95=98=EC=8B=9C=EB=8A=94=EC=A7=80?= =?UTF-8?q?=E2=80=A6"=EB=9D=BC=EA=B3=A0=20=EB=90=98=EB=AC=BB=EA=B3=A0=20"?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=ED=95=B4=EC=84=9C=20=EA=B3=84=EC=82=B0?= =?UTF-8?q?=ED=95=B4=20=EB=93=9C=EB=A6=AC=EA=B2=A0=EC=8A=B5=EB=8B=88?= =?UTF-8?q?=EB=8B=A4"=EB=A1=9C=20=EB=81=9D=EB=83=84(=EC=A7=81=EC=A0=84=20?= =?UTF-8?q?=EB=8B=B5=EC=9D=98=20=EB=8B=AC=EB=9F=AC=20=EA=B8=88=EC=95=A1?= =?UTF-8?q?=EC=9D=80=20Dillon=20$1,000=20=ED=95=98=EB=82=98)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- src/gateway/chat/followup-hints.ts | 25 +++++++++++++++++++++++++ src/gateway/chat/handle-chat.ts | 10 +++++++++- tests/followup-hints.test.ts | 19 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) 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); + }); +});