검색 포기 가드 재시도 지시에 '초안 언급 금지' 추가, Claude 검색 max_tokens 1800→3500 및 빈 답이어도 수수료 기록, 날씨 대화 중 장소 정정 응답(아니 서울)에 장소 재조회 힌트
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
913807510b
commit
b4b82d1e13
4 files changed
+30
-4
No files matched your search
@@ -12,12 +12,17 @@ const UNTIL_RE = /(?:오늘|내일|모레|글피|주말|이번\s?주|\d{1,2}\s?
|
||||
const AMOUNT_RE = /강수량|비\s?양|적설|몇\s?mm|얼마나\s*(?:오|올|내릴|내려)/;
|
||||
const MAX_FOLLOWUP_CHARS = 40;
|
||||
|
||||
const SHORT_NON_PLACE = /^(네|예|응|어|그래|그래요|고마워|고맙|감사|알겠|좋아|오케이|확인|됐어|됐다|아니야|아니요|아뇨|아냐|맞아|뭐|왜|진짜|정말|그럼|그리고|근데|하지만)/;
|
||||
// 「아니 서울」「서울은?」「부산으로」처럼 장소만 고쳐 말하는 짧은 응답
|
||||
const CORRECTION_RE = /^(?:아니|아냐|아니요|그게\s*아니라|아니\s*그게)?\s*([가-힣]{2,8}?)(?:은|는|으로|로|이|가|요|말야|말이야)?\s*[??.!]?$/;
|
||||
|
||||
export function weatherFollowUpHint(
|
||||
message: string,
|
||||
history: ReadonlyArray<{ role: string; content?: any }>,
|
||||
): string | null {
|
||||
const msg = String(message || '').trim();
|
||||
if (!msg || msg.length > MAX_FOLLOWUP_CHARS || !WEATHER_RE.test(msg)) return null;
|
||||
if (!msg || msg.length > MAX_FOLLOWUP_CHARS) return null;
|
||||
if (!WEATHER_RE.test(msg)) return placeCorrectionHint(msg, history);
|
||||
const notes: string[] = [];
|
||||
|
||||
if (TIME_BEFORE_GAJI.test(msg)) {
|
||||
@@ -37,3 +42,14 @@ export function weatherFollowUpHint(
|
||||
}
|
||||
return notes.length ? notes.join(' ') : null;
|
||||
}
|
||||
|
||||
/** 직전 사용자 질문이 날씨였고 이번이 장소만 말하는 짧은 응답이면, 그 장소로 같은 날씨 조회를 다시 하게 한다. */
|
||||
function placeCorrectionHint(msg: string, history: ReadonlyArray<{ role: string; content?: any }>): string | null {
|
||||
if (msg.length > 14 || SHORT_NON_PLACE.test(msg)) return null;
|
||||
const m = CORRECTION_RE.exec(msg);
|
||||
if (!m) return null;
|
||||
const prevUser = [...history].reverse().find(h => h.role === 'user');
|
||||
const prevQ = String(prevUser?.content || '').trim();
|
||||
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.`;
|
||||
}
|
||||
@@ -2154,7 +2154,7 @@ async function handleChat(
|
||||
messages.push({ role: 'assistant', content: candidateText });
|
||||
messages.push({
|
||||
role: 'user',
|
||||
content: `You opened by saying the search found nothing, but ${_abandon.substantiveResults} of this turn's results have real content in them. Result #1 being generic, off-topic, or a site's cookie/browser-settings boilerplate does not mean the search failed — read results #2, #3, #4 and onward before concluding. Answer from whatever those actually contain. If after reading ALL of them nothing addresses the question, say so — but say what you did find, rather than reporting the search as empty.`,
|
||||
content: `You opened by saying the search found nothing, but ${_abandon.substantiveResults} of this turn's results have real content in them. Result #1 being generic, off-topic, or a site's cookie/browser-settings boilerplate does not mean the search failed — read results #2, #3, #4 and onward before concluding. Answer from whatever those actually contain. If after reading ALL of them nothing addresses the question, say so — but say what you did find, rather than reporting the search as empty. The user never saw your previous draft: do not mention this correction or that you first said nothing was found — write the answer as if it were your first.`,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ export async function claudeWebSearch(query: string): Promise<ToolResult | null>
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json', 'x-api-key': apiKey, 'anthropic-version': '2023-06-01' },
|
||||
body: JSON.stringify({
|
||||
model: cfg.model, max_tokens: 1800, system: SYSTEM, messages,
|
||||
model: cfg.model, max_tokens: 3500, system: SYSTEM, messages,
|
||||
tools: [{ type: 'web_search_20250305', name: 'web_search', max_uses: cfg.max_uses, allowed_callers: ['direct'] }],
|
||||
}),
|
||||
signal: AbortSignal.timeout(60_000),
|
||||
@@ -132,7 +132,11 @@ export async function claudeWebSearch(query: string): Promise<ToolResult | null>
|
||||
messages = [messages[0], { role: 'assistant', content: data.content }]; // 일시정지된 턴은 그대로 돌려보내 이어간다
|
||||
}
|
||||
const p = parseNativeResponses(responses);
|
||||
if (!p.text) { console.log(`[v2] claude_search: empty answer (stop=${responses[responses.length - 1]?.stop_reason}, searches=${p.searches}) — fallback`); return null; }
|
||||
if (!p.text) {
|
||||
// 검색은 이미 돌았으니 수수료·토큰은 청구된다 — 기록은 남긴다(2026-10-18: max_tokens로 빈 답 → 비용만 샘)
|
||||
logUsage(cfg.model, p.inputTokens, p.outputTokens, p.searches * SEARCH_FEE_USD);
|
||||
addClaudeSearchUsage(p.searches);
|
||||
console.log(`[v2] claude_search: empty answer (stop=${responses[responses.length - 1]?.stop_reason}, searches=${p.searches}) — fallback`); return null; }
|
||||
// 비용 기록: 토큰은 모델 단가로, 검색 수수료는 별도 필드로(비용 패널이 합산)
|
||||
logUsage(cfg.model, p.inputTokens, p.outputTokens, p.searches * SEARCH_FEE_USD);
|
||||
addClaudeSearchUsage(p.searches);
|
||||
|
||||
@@ -22,3 +22,9 @@ test('"가지"가 채소면 오타로 보지 않음', () => assert.equal(weather
|
||||
test('"내일까지 강수량"은 누적 힌트', () => assert.match(weatherFollowUpHint('내일까지 현풍 강수량은?', [])!, /CUMULATIVE/));
|
||||
test('오타 "내일 가지 강수량"도 누적 힌트', () => assert.match(weatherFollowUpHint('내일 가지 예상 강수량은?', [])!, /CUMULATIVE/));
|
||||
test('"내일 강수량"(까지 없음)은 누적 힌트 없음', () => assert.equal(weatherFollowUpHint('내일 강수량은?', []), null));
|
||||
|
||||
const HW = [{ role: 'user', content: '오늘 세월에 비가 많이 왔나?' }, { role: 'assistant', content: '찾지 못했습니다' }];
|
||||
test('"아니 서울" → 장소 정정 힌트', () => { const h = weatherFollowUpHint('아니 서울', HW)!; assert.match(h, /"서울"/); assert.match(h, /weather_kma/); });
|
||||
test('"부산으로" / "서울은?" → 정정 힌트', () => { assert.match(weatherFollowUpHint('부산으로', HW)!, /"부산"/); assert.match(weatherFollowUpHint('서울은?', HW)!, /"서울"/); });
|
||||
test('날씨 맥락 없으면 정정 힌트 없음', () => assert.equal(weatherFollowUpHint('아니 서울', [{ role: 'user', content: '파스타 맛집' }]), null));
|
||||
test('"고마워" 같은 짧은 응답은 제외', () => { assert.equal(weatherFollowUpHint('고마워', HW), null); assert.equal(weatherFollowUpHint('알겠어요', HW), null); });
|
||||
Reference in new issue
Block a user