Claude 검색 비용 절감: 질문당 최대 검색 기본 3→2, 스마트 모드(맛집·레시피·뜻풀이·방법 같은 가벼운 질문은 Claude 검색 건너뛰고 무료 체인) + 설정 패널 사용 범위 선택
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
21417078fc
commit
5a1cc06c6e
5 files changed
+42
-4
No files matched your search
@@ -214,7 +214,8 @@ export function registerSettingsRoutes(app: Express): void {
|
||||
if (!Number.isFinite(v) || v < 0 || v > 100000) { res.status(400).json({ success: false, error: 'monthly_limit must be 0..100000' }); return; }
|
||||
next.monthly_limit = Math.floor(v);
|
||||
}
|
||||
if (b.max_uses !== undefined) next.max_uses = Math.min(Math.max(Number(b.max_uses) || 3, 1), 6);
|
||||
if (b.max_uses !== undefined) next.max_uses = Math.min(Math.max(Number(b.max_uses) || 2, 1), 6);
|
||||
if (b.mode !== undefined) next.mode = b.mode === 'all' ? 'all' : 'smart';
|
||||
if (b.model !== undefined && /^claude-[\w.-]{3,60}$/.test(String(b.model))) next.model = String(b.model);
|
||||
cm.updateConfig({ search: { ...(current.search || {}), claude: next } } as any);
|
||||
res.json({ success: true, ...getClaudeSearchConfig(), usage: getClaudeSearchUsage() });
|
||||
|
||||
@@ -22,7 +22,7 @@ const SYSTEM = [
|
||||
'- 확인하지 못한 것은 "확인하지 못했다"고 쓰세요.',
|
||||
].join('\n');
|
||||
|
||||
export interface ClaudeSearchConfig { enabled: boolean; model: string; max_uses: number; monthly_limit: number }
|
||||
export interface ClaudeSearchConfig { enabled: boolean; model: string; max_uses: number; monthly_limit: number; mode: 'smart' | 'all' }
|
||||
|
||||
export function getClaudeSearchConfig(): ClaudeSearchConfig {
|
||||
let c: any = {};
|
||||
@@ -30,11 +30,29 @@ export function getClaudeSearchConfig(): ClaudeSearchConfig {
|
||||
return {
|
||||
enabled: c.enabled !== false, // 기본 켜짐(키가 없으면 자동으로 건너뜀)
|
||||
model: String(c.model || DEFAULT_MODEL),
|
||||
max_uses: Math.min(Math.max(Number(c.max_uses) || 3, 1), 6),
|
||||
max_uses: Math.min(Math.max(Number(c.max_uses) || 2, 1), 6),
|
||||
mode: c.mode === 'all' ? 'all' : 'smart', // smart: 가벼운 질문(맛집·뜻풀이 등)은 Claude 검색을 건너뛰고 기존 체인으로
|
||||
monthly_limit: Number.isFinite(Number(c.monthly_limit)) && Number(c.monthly_limit) >= 0 ? Number(c.monthly_limit) : 300,
|
||||
};
|
||||
}
|
||||
|
||||
const PLACE_LEISURE = /맛집|식당|카페|레시피|가볼\s?만|놀거리|데이트\s?코스|메뉴\s?추천/;
|
||||
const FRESH_OR_HEAVY = /뉴스|속보|최신|오늘|어제|내일|이번\s?(주|달)|올해|작년|가격|시세|최저가|주가|환율|금리|선거|판결|법안|법률|논문|임상|통계|규모|발표|출시|사건|사고|폐업|논란|전망|순위|비교|\b20\d\d\b/;
|
||||
const LIGHT_TOPIC = /뜻|의미|사용법|쓰는\s?법|하는\s?법|방법|원리|차이점?|영화|드라마|노래|가사|줄거리|여행지|명소/;
|
||||
|
||||
/**
|
||||
* 질문 종류로 Claude 검색(회당 $0.01 + 토큰)을 쓸지 정한다. 최신·수치·사실 확인이 중요한 질문은 heavy, 장소·취향·뜻풀이처럼
|
||||
* 시어엔진으로도 충분한 가벼운 질문은 light. 애매하면 품질을 위해 heavy. (순수 함수 — 테스트 대상)
|
||||
*/
|
||||
export function classifySearchQuery(query: string): 'heavy' | 'light' {
|
||||
const q = String(query || '');
|
||||
if (/뉴스|속보|폐업|논란/.test(q)) return 'heavy';
|
||||
if (PLACE_LEISURE.test(q)) return 'light';
|
||||
if (FRESH_OR_HEAVY.test(q)) return 'heavy';
|
||||
if (LIGHT_TOPIC.test(q)) return 'light';
|
||||
return 'heavy';
|
||||
}
|
||||
|
||||
function usageFile(): string | null {
|
||||
try { return path.join(getConfig().getConfigDir(), 'claude-search-usage.json'); } catch { return null; }
|
||||
}
|
||||
@@ -77,6 +95,10 @@ export function parseNativeResponses(responses: any[]): ParsedNative {
|
||||
export async function claudeWebSearch(query: string): Promise<ToolResult | null> {
|
||||
const cfg = getClaudeSearchConfig();
|
||||
if (!cfg.enabled || !query?.trim()) return null;
|
||||
if (cfg.mode === 'smart' && classifySearchQuery(query) === 'light') {
|
||||
console.log('[v2] claude_search: light query — using the standard (free) search chain');
|
||||
return null;
|
||||
}
|
||||
const usage = getClaudeSearchUsage();
|
||||
if (cfg.monthly_limit > 0 && usage.count >= cfg.monthly_limit) {
|
||||
console.log(`[v2] claude_search: monthly limit reached (${usage.count}/${cfg.monthly_limit}) — falling back to the standard search chain`);
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { classifySearchQuery } from '../src/tools/claude-search';
|
||||
|
||||
for (const q of ['구미 인동 맛집 추천', '김치찌개 레시피', '여름 가볼만한 곳', '풍요롭다 뜻', '파이썬 리스트 정렬하는 법', '인셉션 줄거리'])
|
||||
test(`light: ${q}`, () => assert.equal(classifySearchQuery(q), 'light'));
|
||||
for (const q of ['오늘 미국 증시 뉴스', '올해 최저임금', '갤럭시 폴드7 최저가', '한국 기준금리 최신 결정', '구미역 KTX 정차하나', '치면열구전색 논문', '맛집 폐업 논란 뉴스'])
|
||||
test(`heavy: ${q}`, () => assert.equal(classifySearchQuery(q), 'heavy'));
|
||||
@@ -1134,10 +1134,15 @@
|
||||
<input id="cs-limit" type="number" min="0" step="10" style="width:90px;margin-left:6px;border:1px solid var(--line);border-radius:8px;padding:5px;font-size:12px"></label>
|
||||
<label style="font-size:12px;color:var(--muted)">질문당 최대 검색
|
||||
<input id="cs-max-uses" type="number" min="1" max="6" style="width:60px;margin-left:6px;border:1px solid var(--line);border-radius:8px;padding:5px;font-size:12px"></label>
|
||||
<label style="font-size:12px;color:var(--muted)">사용 범위
|
||||
<select id="cs-mode" style="margin-left:6px;border:1px solid var(--line);border-radius:8px;padding:5px;font-size:12px">
|
||||
<option value="smart">스마트 (가벼운 질문은 시어엔진)</option>
|
||||
<option value="all">모든 웹 검색</option>
|
||||
</select></label>
|
||||
<button class="btn btn-sm" onclick="saveClaudeSearch()">저장 (즉시 적용)</button>
|
||||
<span id="cs-status" style="font-size:12px;color:var(--muted)"></span>
|
||||
</div>
|
||||
<div style="font-size:11px;color:var(--muted);margin-top:8px;line-height:1.6">검색 1회 $0.01 + 토큰으로 질문당 약 2센트입니다(10개 질문 비교에서 품질 8.0점 대 기존 약 4점). 이번 달 사용 횟수는 위에 표시되고, 비용은 아래 "사용 비용" 패널에 합산됩니다.</div>
|
||||
<div style="font-size:11px;color:var(--muted);margin-top:8px;line-height:1.6">검색 1회 $0.01 + 토큰으로 질문당 약 2센트입니다. 스마트 모드는 최신·수치·사실 확인 질문만 Claude 검색을 쓰고 맛집·뜻풀이·방법 같은 가벼운 질문은 무료 체인으로 보냅니다(10개 질문 비교에서 품질 8.0점 대 기존 약 4점). 이번 달 사용 횟수는 위에 표시되고, 비용은 아래 "사용 비용" 패널에 합산됩니다.</div>
|
||||
</div>
|
||||
|
||||
<!-- 사용 비용 추산 -->
|
||||
|
||||
@@ -1641,6 +1641,7 @@ async function loadClaudeSearch() {
|
||||
document.getElementById('cs-enabled').checked = r.enabled === true;
|
||||
document.getElementById('cs-limit').value = r.monthly_limit;
|
||||
document.getElementById('cs-max-uses').value = r.max_uses;
|
||||
document.getElementById('cs-mode').value = r.mode || 'smart';
|
||||
if (st) st.textContent = '이번 달 ' + r.usage.count + ' / ' + r.monthly_limit + '회 사용';
|
||||
} catch (e) { if (st) st.textContent = '불러오기 실패: ' + e.message; }
|
||||
}
|
||||
@@ -1652,6 +1653,7 @@ async function saveClaudeSearch() {
|
||||
enabled: !!document.getElementById('cs-enabled').checked,
|
||||
monthly_limit: Number(document.getElementById('cs-limit').value),
|
||||
max_uses: Number(document.getElementById('cs-max-uses').value),
|
||||
mode: document.getElementById('cs-mode').value,
|
||||
}) });
|
||||
if (st) st.textContent = '저장됨 — ' + (r.enabled ? '켜짐' : '꺼짐') + ' · 이번 달 ' + r.usage.count + ' / ' + r.monthly_limit + '회';
|
||||
} catch (e) { if (st) st.textContent = '저장 실패: ' + e.message; }
|
||||
|
||||
Reference in new issue
Block a user