Skip to content

Commit cfb2ae5

Browse files
roberttwangclaude
andcommitted
fix(web): finish the English 我的知识库 strings the review flagged
- 关联知识库: the "unavailable" suffix of a missing connector is built with lfT, so the whole-string user-data title (and the aria-label built from it) no longer falls back to 「(已失效或无权限)」. - "Connector: " / "Document: " are emitted with lfT outside the user-data span, so the trailing space survives instead of being trimmed by translate(). - Caller pills: a top caller without a title arrives with the server's Chinese kind name; show the localized kind instead, so the tooltip is no longer "Personal chat · 个人对话". Kind names now match the shared dictionary (Workspace, Requirement clarification, Scheduled task) instead of a second set of wordings in the same pill. - English counts use singular forms ("1 call", "cited 1 time", "1 doc"). Co-Authored-By: Claude Code <noreply@anthropic.com>
1 parent f3c5b26 commit cfb2ae5

3 files changed

Lines changed: 90 additions & 18 deletions

File tree

‎web/app/knowledge-refs.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ window.editKnowledgeRefs = async function editKnowledgeRefs(project, refs, save)
7676
for (const ref of (refs || [])) {
7777
const rid = getRefId(ref);
7878
if (rid && !options.some(s => s.id === rid)) {
79-
options.push({ id: rid, name: rid + '(已失效或无权限)', missing: true });
79+
options.push({ id: rid, name: rid + lfT('(已失效或无权限)', ' (expired or unauthorized)'), missing: true });
8080
}
8181
}
8282

‎web/app/knowledge.js‎

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@
292292
];
293293
}
294294

295+
// English count phrase: "1 doc" / "2 docs".
296+
function kbPlural(n, one, many) {
297+
return `${n} ${Number(n) === 1 ? one : many}`;
298+
}
299+
295300
function kbEsc(s) {
296301
return String(s == null ? '' : s)
297302
.replace(/&/g, '&amp;')
@@ -505,7 +510,7 @@
505510
const toolsHTML = tools.length
506511
? `<div class="kb-chip-row"><span class="kb-muted">只读工具:</span>${tools.map(t => `<code class="kb-chip">${kbEsc(t)}</code>`).join('')}</div>`
507512
: '';
508-
const srcNote = sources.length ? lfT(` · 发现 ${sources.length} 个子知识库`, ` · ${sources.length} sub-sources found`) : '';
513+
const srcNote = sources.length ? lfT(` · 发现 ${sources.length} 个子知识库`, ` · ${kbPlural(sources.length, 'sub-source', 'sub-sources')} found`) : '';
509514
return `
510515
<div class="kb-check-banner ok">
511516
<div><strong>探测通过 · 连接正常</strong> · ${latency} ms${srcNote}</div>
@@ -526,7 +531,7 @@
526531
${sources.map(src => `
527532
<div class="kb-source-pill">
528533
<div class="kb-source-title" data-i18n-data>${kbEsc(src.name || src.id)}</div>
529-
<div class="kb-source-sub"><code>${kbEsc(src.id)}</code>${src.docCount != null ? ` · ${lfT(`${Number(src.docCount)} 篇文档`, `${Number(src.docCount)} docs`)}` : ''}</div>
534+
<div class="kb-source-sub"><code>${kbEsc(src.id)}</code>${src.docCount != null ? ` · ${lfT(`${Number(src.docCount)} 篇文档`, kbPlural(Number(src.docCount), 'doc', 'docs'))}` : ''}</div>
530535
</div>
531536
`).join('')}
532537
</div>
@@ -875,7 +880,7 @@
875880
<span class="hui-badge" data-size="sm" data-tone="neutral">${kbEsc(meta?.name || conn.type)}</span>
876881
<span class="hui-badge" data-size="sm" data-tone="${conn.enabled ? 'success' : 'neutral'}">${conn.enabled ? '已启用' : '已停用'}</span>
877882
${renderConnectorAuthBadgeHTML(conn)}
878-
${kbState.usage ? `<button type="button" class="hui-badge kb-usage-badge-btn" data-size="sm" data-tone="neutral" data-kb-conn-calls="${kbEsc(conn.id)}" title="查看该连接器的调用明细">${lfT(`调用 ${callCount} 次`, `${callCount} ${callCount === 1 ? 'call' : 'calls'}`)}</button>` : ''}
883+
${kbState.usage ? `<button type="button" class="hui-badge kb-usage-badge-btn" data-size="sm" data-tone="neutral" data-kb-conn-calls="${kbEsc(conn.id)}" title="查看该连接器的调用明细">${lfT(`调用 ${callCount} 次`, kbPlural(callCount, 'call', 'calls'))}</button>` : ''}
879884
</div>
880885
</div>
881886
<div class="kb-row-actions">
@@ -930,7 +935,7 @@
930935
${statusBadgeHTML}
931936
</div>
932937
<p class="kb-vendor-desc">${kbEsc(v.desc)}</p>
933-
${isConfigured ? `<div class="kb-vendor-meta">已绑定连接器:<span data-i18n-data>${kbEsc(conn.name)}</span></div>` : ''}
938+
${isConfigured ? `<div class="kb-vendor-meta">${lfT('已绑定连接器:', 'Connector: ')}<span data-i18n-data>${kbEsc(conn.name)}</span></div>` : ''}
934939
</div>
935940
</div>
936941
<div class="kb-vendor-actions">
@@ -982,9 +987,9 @@
982987
function formatUsageCallerKindLabel(kind) {
983988
switch (String(kind || '').toLowerCase()) {
984989
case 'sop': return 'SOP';
985-
case 'space': return lfT('协作空间', 'Shared space');
986-
case 'clarify': return lfT('需求澄清', 'Clarification');
987-
case 'scheduled': return lfT('定时任务', 'Scheduled job');
990+
case 'space': return lfT('协作空间', 'Workspace');
991+
case 'clarify': return lfT('需求澄清', 'Requirement clarification');
992+
case 'scheduled': return lfT('定时任务', 'Scheduled task');
988993
case 'report': return lfT('分析报告', 'Analysis report');
989994
default: return lfT('个人对话', 'Personal chat');
990995
}
@@ -1038,6 +1043,15 @@
10381043
return parts.join(' · ');
10391044
}
10401045

1046+
// Top callers come from the server, which falls back to the Chinese kind name
1047+
// (or "SOP 运行") when a caller has no title; show the localized name instead.
1048+
function callerPillLabel(label, kind) {
1049+
const text = String(label || '').trim();
1050+
if (!text || text === (CALLER_KIND_ZH[kind] || CALLER_KIND_ZH.chat)) return formatUsageCallerKindLabel(kind);
1051+
if (kind === 'sop' && text === 'SOP 运行') return lfT('SOP 运行', 'SOP run');
1052+
return text;
1053+
}
1054+
10411055
function callerJumpAttrs(target) {
10421056
const c = target || {};
10431057
const kind = String(c.kind || 'chat').toLowerCase();
@@ -1115,15 +1129,17 @@
11151129
return `
11161130
<div class="kb-usage-caller-pills">
11171131
${callers.map(c => {
1118-
const kindLabel = formatUsageCallerKindLabel(c.kind);
1119-
const subLabel = callerSubLabel(c.subLabel, String(c.kind || 'chat').toLowerCase());
1132+
const kind = String(c.kind || 'chat').toLowerCase();
1133+
const kindLabel = formatUsageCallerKindLabel(kind);
1134+
const label = callerPillLabel(c.label, kind);
1135+
const subLabel = callerSubLabel(c.subLabel, kind);
11201136
const countText = Number(c.callCount || 0) > 1 ? ` ×${Number(c.callCount)}` : '';
1121-
const titleText = subLabel ? `${kindLabel} · ${c.label} (${subLabel})` : `${kindLabel} · ${c.label}`;
1137+
const titleText = subLabel ? `${kindLabel} · ${label} (${subLabel})` : `${kindLabel} · ${label}`;
11221138
const jumpAttr = callerJumpAttrs(c);
11231139
if (jumpAttr) {
1124-
return `<button type="button" class="kb-usage-caller-pill is-link" ${jumpAttr} title="${kbEsc(titleText)}" data-i18n-data><span class="kb-usage-caller-tag">${kbEsc(kindLabel)}</span><span class="kb-usage-caller-name">${kbEsc(c.label)}${subLabel ? ` · ${kbEsc(subLabel)}` : ''}</span>${countText ? `<span class="kb-usage-caller-cnt">${kbEsc(countText)}</span>` : ''}</button>`;
1140+
return `<button type="button" class="kb-usage-caller-pill is-link" ${jumpAttr} title="${kbEsc(titleText)}" data-i18n-data><span class="kb-usage-caller-tag">${kbEsc(kindLabel)}</span><span class="kb-usage-caller-name">${kbEsc(label)}${subLabel ? ` · ${kbEsc(subLabel)}` : ''}</span>${countText ? `<span class="kb-usage-caller-cnt">${kbEsc(countText)}</span>` : ''}</button>`;
11251141
}
1126-
return `<span class="kb-usage-caller-pill" title="${kbEsc(titleText)}" data-i18n-data><span class="kb-usage-caller-tag">${kbEsc(kindLabel)}</span><span class="kb-usage-caller-name">${kbEsc(c.label)}${subLabel ? ` · ${kbEsc(subLabel)}` : ''}</span>${countText ? `<span class="kb-usage-caller-cnt">${kbEsc(countText)}</span>` : ''}</span>`;
1142+
return `<span class="kb-usage-caller-pill" title="${kbEsc(titleText)}" data-i18n-data><span class="kb-usage-caller-tag">${kbEsc(kindLabel)}</span><span class="kb-usage-caller-name">${kbEsc(label)}${subLabel ? ` · ${kbEsc(subLabel)}` : ''}</span>${countText ? `<span class="kb-usage-caller-cnt">${kbEsc(countText)}</span>` : ''}</span>`;
11271143
}).join('')}
11281144
</div>
11291145
`;
@@ -1198,7 +1214,7 @@
11981214
<option value="chat"${f.callerKind === 'chat' ? ' selected' : ''}>对话 / 助手</option>
11991215
</select>
12001216
<input type="search" class="kb-input kb-usage-search" data-kb-usage-filter="query" value="${kbEsc(f.query)}" placeholder="搜索文档、调用方或检索词…" aria-label="搜索调用明细">
1201-
${f.documentId ? `<span class="kb-usage-doc-chip"><span>文档:<span data-i18n-data>${kbEsc(f.documentLabel || f.documentId)}</span></span><button type="button" class="kb-usage-chip-close" data-kb-clear-doc-filter aria-label="清除文档筛选">×</button></span>` : ''}
1217+
${f.documentId ? `<span class="kb-usage-doc-chip"><span>${lfT('文档:', 'Document: ')}<span data-i18n-data>${kbEsc(f.documentLabel || f.documentId)}</span></span><button type="button" class="kb-usage-chip-close" data-kb-clear-doc-filter aria-label="清除文档筛选">×</button></span>` : ''}
12021218
${hasFilter ? '<button type="button" class="kb-btn xs" data-kb-clear-usage-filters>重置筛选</button>' : ''}
12031219
</div>
12041220
`;
@@ -1286,7 +1302,7 @@
12861302
<div class="kb-usage-head">
12871303
<div>
12881304
<h2>知识库与文档调用统计</h2>
1289-
<p class="kb-muted">${lfT(`累计调用 ${totalCalls} 次 · 文档命中/读取 ${totalDocCalls} 次 · 已引用 ${totalCited} 次`, `${totalCalls} calls in total · ${totalDocCalls} document hits/reads · cited ${totalCited} times`)}</p>
1305+
<p class="kb-muted">${lfT(`累计调用 ${totalCalls} 次 · 文档命中/读取 ${totalDocCalls} 次 · 已引用 ${totalCited} 次`, `${kbPlural(totalCalls, 'call', 'calls')} in total · ${kbPlural(totalDocCalls, 'document hit/read', 'document hits/reads')} · cited ${kbPlural(totalCited, 'time', 'times')}`)}</p>
12901306
</div>
12911307
<div class="kb-usage-head-actions">
12921308
<div class="kb-usage-tabs" role="tablist" aria-label="统计视图">

‎webtest/tests/i18n-knowledge.test.mjs‎

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ const ROUTES = [
4747
{ kind: 'sop', label: '库存缺陷修复 LOOP', subLabel: '开发实现 · 开发实现', callCount: 2, runId: 'run-1', nodeId: 'implement' },
4848
{ kind: 'chat', label: CHAT_TITLE, subLabel: '个人对话', callCount: 1, sessionId: 'ses-1' },
4949
],
50+
}, {
51+
connectorId: 'kb_obs', connectorName: '团队 Obsidian 笔记', sourceName: '库存系统', documentId: 'doc-2',
52+
title: '发布与回滚', searchHitCount: 1, readCount: 0, citedCount: 0, callCount: 2,
53+
// Callers without a title: the server falls back to the Chinese kind name.
54+
topCallers: [
55+
{ kind: 'scheduled', label: '定时任务', subLabel: '定时任务', callCount: 1 },
56+
{ kind: 'chat', label: '个人对话', subLabel: '个人对话', callCount: 1 },
57+
],
5058
}],
5159
} },
5260
{ method: 'GET', path: /^\/api\/knowledge\/usage\/events/, body: {
@@ -82,13 +90,15 @@ test('English mode leaves no Chinese copy in 我的知识库 (gallery, connector
8290
const { root } = await renderEnglishKnowledge(t);
8391
assert.deepEqual(leftoverChinese(root), []);
8492
const text = root.textContent.replace(/\s+/g, ' ');
85-
assert.match(text, /6 calls in total · 4 document hits\/reads · cited 1 times/);
93+
assert.match(text, /6 calls in total · 4 document hits\/reads · cited 1 time\b/);
94+
// Connector card: the label keeps its space before the user-named connector.
95+
assert.match(text, /Connector: 团队 Obsidian 笔记/);
8696
assert.match(text, /Timeout: 30s/);
8797
assert.match(text, /Popular knowledge bases · Quick setup/);
8898
// User data is shown as written, not run through fragment translation.
8999
assert.ok(root.querySelector('.kb-card h3, h3[data-i18n-data]').textContent.includes('团队 Obsidian 笔记'));
90100
const pills = [...root.querySelectorAll('.kb-usage-caller-name')].map(el => el.textContent);
91-
assert.deepEqual(pills, ['库存缺陷修复 LOOP · 开发实现', CHAT_TITLE]);
101+
assert.deepEqual(pills, ['库存缺陷修复 LOOP · 开发实现', CHAT_TITLE, 'Scheduled task', 'Personal chat']);
92102
});
93103

94104
test('English mode leaves no Chinese copy in the knowledge call history and keeps user titles intact', async (t) => {
@@ -112,5 +122,51 @@ test('caller sub-labels drop repeated role/node names and the kind already shown
112122
app.evaluate("switchView('knowledge')");
113123
await settle(12);
114124
const names = [...app.document.querySelectorAll('.kb-usage-caller-name')].map(el => el.textContent);
115-
assert.deepEqual(names, ['库存缺陷修复 LOOP · 开发实现', CHAT_TITLE]);
125+
assert.deepEqual(names, ['库存缺陷修复 LOOP · 开发实现', CHAT_TITLE, '定时任务', '个人对话']);
126+
});
127+
128+
test('English caller pills translate server fallback names and use the shared dictionary wording', async (t) => {
129+
const { root } = await renderEnglishKnowledge(t);
130+
const pills = [...root.querySelectorAll('.kb-usage-caller-pill')];
131+
const scheduled = pills.find(el => el.querySelector('.kb-usage-caller-tag').textContent === 'Scheduled task');
132+
const chat = pills.filter(el => el.querySelector('.kb-usage-caller-tag').textContent === 'Personal chat').pop();
133+
assert.ok(scheduled && chat);
134+
assert.equal(scheduled.querySelector('.kb-usage-caller-name').textContent, 'Scheduled task');
135+
assert.equal(scheduled.getAttribute('title'), 'Scheduled task · Scheduled task');
136+
assert.equal(chat.querySelector('.kb-usage-caller-name').textContent, 'Personal chat');
137+
assert.equal(chat.getAttribute('title'), 'Personal chat · Personal chat');
138+
assert.deepEqual(leftoverChinese(root), []);
139+
});
140+
141+
test('English counts use singular forms for one item', async (t) => {
142+
const app = await loadApp({ routes: ROUTES.map(r => r.path === '/api/knowledge/usage'
143+
? { ...r, body: { ...r.body, totalCalls: 1, totalDocumentCalls: 1, totalCitedCount: 1, connectors: [{ connectorId: 'kb_obs', connectorName: '团队 Obsidian 笔记', callCount: 1 }] } }
144+
: r) });
145+
t.after(app.close);
146+
app.evaluate("window.lfT = (zh, en) => en; toast = () => {};");
147+
app.evaluate("switchView('knowledge')");
148+
await settle(12);
149+
const text = app.document.querySelector('#knowledgeRoot').textContent.replace(/\s+/g, ' ');
150+
assert.match(text, /1 call in total · 1 document hit\/read · cited 1 time\b/);
151+
assert.match(text, /\b1 call\b/);
152+
assert.doesNotMatch(text, /\b1 (calls|times|docs)\b/);
153+
});
154+
155+
test('English 关联知识库 dialog keeps the unavailable-connector suffix in English', async (t) => {
156+
const app = await loadApp({ routes: [{ method: 'GET', path: '/api/knowledge/reference-options', body: [] }] });
157+
t.after(app.close);
158+
app.evaluate("window.lfT = (zh, en) => en; toast = () => {};");
159+
app.evaluate("window.editKnowledgeRefs('', [{ connectorId: 'kb_gone' }], async () => {})");
160+
await settle(12);
161+
const en = await englishPage();
162+
t.after(() => en.close());
163+
const root = en.document.createElement('div');
164+
const modal = app.document.querySelector('.kr-service')?.closest('.modal, [role="dialog"], .kr-modal') || app.document.querySelector('.kr-service').parentElement;
165+
root.innerHTML = modal.outerHTML;
166+
en.document.body.append(root);
167+
await new Promise(resolve => en.setTimeout(resolve, 0));
168+
const titles = [...root.querySelectorAll('.kr-service-title')].map(el => el.textContent);
169+
assert.deepEqual(titles, ['kb_gone (expired or unauthorized)']);
170+
const aria = root.querySelector('.kr-control[aria-label]').getAttribute('aria-label');
171+
assert.equal(aria, 'Allowed source IDs for kb_gone (expired or unauthorized)');
116172
});

0 commit comments

Comments
 (0)