From 8304adf4b18742637d31b212788a499632761ae6 Mon Sep 17 00:00:00 2001 From: kim Date: Wed, 21 Oct 2026 13:44:04 +0900 Subject: [PATCH] =?UTF-8?q?=EB=A9=94=EC=9D=B8=EC=B1=84=ED=8C=85:=20'?= =?UTF-8?q?=EB=91=90=20=EB=AC=B8=EC=9E=A5=20=ED=95=A9=ED=95=B4=EC=95=BC/?= =?UTF-8?q?=EC=9B=90=EB=9E=98=20=ED=95=9C=20=EB=AC=B8=EC=9E=A5'=20?= =?UTF-8?q?=EA=B0=99=EC=9D=80=20=EB=A7=90=EC=97=90=20=EC=A7=81=EC=A0=84=20?= =?UTF-8?q?=EB=B0=9C=ED=99=94=20=EB=91=90=20=EA=B0=9C=EB=A5=BC=20=EC=A7=9A?= =?UTF-8?q?=EC=96=B4=20=EC=A3=BC=EB=8A=94=20=ED=9E=8C=ED=8A=B8(=EB=81=8A?= =?UTF-8?q?=EC=96=B4=20=EB=B3=B4=EB=82=B8=20=EB=A9=94=EC=8B=9C=EC=A7=80=20?= =?UTF-8?q?=EC=9D=B4=EC=96=B4=EB=B6=99=EC=9D=B4=EA=B8=B0)?= 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 | 26 ++++++++++++++++++++++++++ src/gateway/chat/handle-chat.ts | 9 ++++++++- tests/followup-hints.test.ts | 26 +++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 2 deletions(-) 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); + }); +});