diff --git a/src/gateway/chat/followup-hints.ts b/src/gateway/chat/followup-hints.ts index 44e34de..54b1409 100644 --- a/src/gateway/chat/followup-hints.ts +++ b/src/gateway/chat/followup-hints.ts @@ -66,3 +66,29 @@ 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-21). + * + * 실사고(메인채팅): "sg 90 서보 모터로 만들 수 있는 장난감 롭"이 중간에 전송되고 이어서 "로봇 뭐가 잇지?"를 보냄 → + * 사용자가 "두 문장 합해야 하네.." / "위에 두 대화 문장이 원래 한 문장이라고"라고 해도 모델(Haiku)은 합칠 원문을 + * 다시 붙여 달라고 하거나, 메타 발화 자체("두 문장 합해야 하네")를 합쳐 버렸다. 직전 사용자 발화를 그대로 짚어 준다. + */ +const SPLIT_META_RE = /(?:두|2|세|3)\s*(?:문장|메시지|개|줄|대화)|한\s*문장|원래\s*한|합(?:쳐|해|치|친|하)|이어\s*(?:서|붙|진|지)|붙여서|끊겼|끊어졌|잘렸|잘린|중간에\s*(?:보내|전송|넘어)/; + +export function splitMessageHint( + message: string, + historyIn: ReadonlyArray<{ role: string; content?: any }>, +): string | null { + const msg = String(message || '').trim(); + if (!msg || msg.length > 60 || !SPLIT_META_RE.test(msg)) return null; + const history = historyIn.length && historyIn[historyIn.length - 1].role === 'user' && String(historyIn[historyIn.length - 1].content || '').trim() === msg + ? historyIn.slice(0, -1) : historyIn; + // 앞서 같은 메타 발화를 했으면("두 문장 합해야 하네" → "원래 한 문장이라고") 그것도 빼고 실제 내용 발화만 남긴다 + const prior = history.filter(h => h.role === 'user') + .map(h => String(h.content || '').trim()) + .filter(t => t && !(t.length <= 60 && SPLIT_META_RE.test(t))) + .slice(-2); + if (prior.length < 2) return null; + return `The user is saying their earlier messages were ONE sentence that got sent in pieces (e.g. Enter was pressed mid-sentence). Their last content messages, oldest first: 1) "${prior[0]}" 2) "${prior[1]}". Join these into the single question they meant (fix an obvious cut-off word or typo where they meet), state the joined question in one short line, then answer THAT question directly. This current message is only a remark about the split — do not treat its words as content, and do not ask them to paste the text again.`; +} diff --git a/src/gateway/chat/handle-chat.ts b/src/gateway/chat/handle-chat.ts index 3f2e35e..c18f6bc 100644 --- a/src/gateway/chat/handle-chat.ts +++ b/src/gateway/chat/handle-chat.ts @@ -85,7 +85,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, splitMessageHint } from './followup-hints'; import { fixKeyboardLayout } from './hangul-layout'; import { suspectSoundAlikePlace } from './voice-place-check'; import { replyIsWrongLanguage, TRANSLATE_TO_KOREAN_SYSTEM } from './language-backstop'; @@ -975,6 +975,13 @@ async function handleChat( messages.push({ role: 'assistant', content: 'Understood.' }); messages.push({ role: 'user', content: `Note: ${_wfHint}` }); } + // 중간에 끊어 보낸 두 메시지를 "원래 한 문장"이라고 할 때 직전 발화를 짚어 준다 (followup-hints.ts, 2026-10-21) + const _splitHint = splitMessageHint(message, history); + if (_splitHint) { + console.log('[v2] SPLIT-MESSAGE HINT added'); + messages.push({ role: 'assistant', content: 'Understood.' }); + messages.push({ role: 'user', content: `Note: ${_splitHint}` }); + } } diff --git a/tests/followup-hints.test.ts b/tests/followup-hints.test.ts index 840f368..305883b 100644 --- a/tests/followup-hints.test.ts +++ b/tests/followup-hints.test.ts @@ -1,4 +1,4 @@ -import { test } from 'node:test'; +import { test, describe } from 'node:test'; import assert from 'node:assert/strict'; import { weatherFollowUpHint } from '../src/gateway/chat/followup-hints'; @@ -47,3 +47,27 @@ test('앞으로 올 비·무관한 말에는 과거 비 힌트가 붙지 않는 assert.ok(!h || !/ALREADY fell/.test(h), q); } }); + +import { splitMessageHint } from '../src/gateway/chat/followup-hints'; + +describe('splitMessageHint — 끊어 보낸 메시지 이어붙이기 (2026-10-21)', () => { + const H = [ + { role: 'user', content: 'sg 90 서보 모터로 만들 수 있는 장난감 롭' }, + { role: 'assistant', content: '...' }, + { role: 'user', content: '로봇 뭐가 잇지?' }, + { role: 'assistant', content: '...' }, + ]; + test('실측: "두 문장 합해야 하네.."는 직전 두 발화를 짚는다', () => { + const h = splitMessageHint('두 문장 합해야 하네..', H); + assert.ok(h && h.includes('장난감 롭') && h.includes('로봇 뭐가 잇지?')); + }); + test('실측 2턴째: 앞선 메타 발화는 내용에서 뺀다', () => { + const H2 = [...H, { role: 'user', content: '두 문장 합해야 하네..' }, { role: 'assistant', content: '...' }]; + const h = splitMessageHint('위에 두 대화 문장이 원래 한 문장이라고', H2); + assert.ok(h && h.includes('장난감 롭') && !h.includes('합해야 하네')); + }); + test('관계없는 짧은 말은 무시', () => { + assert.equal(splitMessageHint('고마워', H), null); + assert.equal(splitMessageHint('내일 날씨는?', H), null); + }); +});