Skip to content

Commit 6ac041c

Browse files
author
catlog22
committed
feat: Enhance Codex CLI settings with toggle and refresh actions
1 parent 279adfd commit 6ac041c

4 files changed

Lines changed: 132 additions & 8 deletions

File tree

.codex/AGENTS.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# Codex Code Guidelines
22

3-
**Strictly follow the cli-tools.json configuration**
4-
5-
Available CLI endpoints are dynamically defined by the config file
63

74
## Code Quality Standards
85

ccw/src/core/routes/claude-routes.ts

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,9 +1099,12 @@ export async function handleClaudeRoutes(ctx: RouteContext): Promise<boolean> {
10991099
// API: Toggle Codex CLI Enhancement setting
11001100
if (pathname === '/api/language/codex-cli-enhancement' && req.method === 'POST') {
11011101
handlePostRequest(req, res, async (body: any) => {
1102-
const { enabled } = body;
1102+
const { enabled, action } = body;
11031103

1104-
if (typeof enabled !== 'boolean') {
1104+
// Support two actions: 'toggle' (default) and 'refresh'
1105+
const actionType = action || 'toggle';
1106+
1107+
if (actionType === 'toggle' && typeof enabled !== 'boolean') {
11051108
return { error: 'Missing or invalid enabled parameter', status: 400 };
11061109
}
11071110

@@ -1123,10 +1126,52 @@ export async function handleClaudeRoutes(ctx: RouteContext): Promise<boolean> {
11231126
}
11241127

11251128
const cliEnhancementSectionPattern = /\n*## CLI \n[\s\S]*?(?=\n## |$)/;
1129+
const isCurrentlyEnabled = cliEnhancementSectionPattern.test(content);
1130+
1131+
// Handle refresh action
1132+
if (actionType === 'refresh') {
1133+
if (!isCurrentlyEnabled) {
1134+
return { error: 'CLI enhancement is not enabled, cannot refresh', status: 400 };
1135+
}
1136+
1137+
// Remove existing section
1138+
content = content.replace(cliEnhancementSectionPattern, '\n');
1139+
content = content.replace(/\n{3,}/g, '\n\n').trim();
1140+
if (content) content += '\n';
1141+
1142+
// Read and add updated section
1143+
const cliToolsUsagePath = join(homedir(), '.claude', 'workflows', 'cli-tools-usage.md');
1144+
let cliToolsUsageContent = '';
1145+
if (existsSync(cliToolsUsagePath)) {
1146+
cliToolsUsageContent = readFileSync(cliToolsUsagePath, 'utf8');
1147+
} else {
1148+
return { error: 'CLI tools usage guidelines file not found at ~/.claude/workflows/cli-tools-usage.md', status: 404 };
1149+
}
1150+
1151+
const cliToolsJsonPath = join(homedir(), '.claude', 'cli-tools.json');
1152+
let cliToolsJsonContent = '';
1153+
if (existsSync(cliToolsJsonPath)) {
1154+
const cliToolsJson = JSON.parse(readFileSync(cliToolsJsonPath, 'utf8'));
1155+
cliToolsJsonContent = `\n### CLI Tools Configuration\n\n\`\`\`json\n${JSON.stringify(cliToolsJson, null, 2)}\n\`\`\`\n`;
1156+
}
1157+
1158+
const newSection = `\n## CLI 工具调用\n\n${cliToolsUsageContent}\n${cliToolsJsonContent}`;
1159+
content = content.trimEnd() + '\n' + newSection;
1160+
1161+
writeFileSync(targetFile, content, 'utf8');
1162+
1163+
broadcastToClients({
1164+
type: 'CLI_ENHANCEMENT_SETTING_CHANGED',
1165+
data: { cliEnhancement: true, refreshed: true }
1166+
});
1167+
1168+
return { success: true, refreshed: true };
1169+
}
11261170

1171+
// Handle toggle action
11271172
if (enabled) {
11281173
// Check if section already exists
1129-
if (cliEnhancementSectionPattern.test(content)) {
1174+
if (isCurrentlyEnabled) {
11301175
return { success: true, message: 'Already enabled' };
11311176
}
11321177

ccw/src/templates/dashboard-css/18-cli-settings.css

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,41 @@
6565
.cli-setting-control {
6666
margin-bottom: 0.5rem;
6767
flex-shrink: 0;
68+
display: flex;
69+
align-items: center;
70+
gap: 0.5rem;
71+
}
72+
73+
/* Small icon button for refresh etc */
74+
.btn-icon-sm {
75+
display: inline-flex;
76+
align-items: center;
77+
justify-content: center;
78+
width: 24px;
79+
height: 24px;
80+
padding: 0;
81+
border: 1px solid hsl(var(--border));
82+
border-radius: 0.375rem;
83+
background: hsl(var(--background));
84+
color: hsl(var(--muted-foreground));
85+
cursor: pointer;
86+
transition: all 0.15s ease;
87+
}
88+
89+
.btn-icon-sm:hover:not(:disabled) {
90+
border-color: hsl(var(--primary) / 0.5);
91+
color: hsl(var(--primary));
92+
background: hsl(var(--primary) / 0.05);
93+
}
94+
95+
.btn-icon-sm:disabled {
96+
opacity: 0.5;
97+
cursor: not-allowed;
98+
}
99+
100+
.btn-icon-sm i {
101+
width: 12px;
102+
height: 12px;
68103
}
69104

70105
.cli-setting-select {

ccw/src/templates/dashboard-js/views/cli-manager.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,7 @@ async function toggleCodexCliEnhancement(enabled) {
17971797
var response = await fetch('/api/language/codex-cli-enhancement', {
17981798
method: 'POST',
17991799
headers: { 'Content-Type': 'application/json' },
1800-
body: JSON.stringify({ enabled: enabled })
1800+
body: JSON.stringify({ enabled: enabled, action: 'toggle' })
18011801
});
18021802

18031803
if (!response.ok) {
@@ -1828,6 +1828,46 @@ async function toggleCodexCliEnhancement(enabled) {
18281828
}
18291829
}
18301830

1831+
async function refreshCodexCliEnhancement() {
1832+
if (codexCliEnhancementLoading) return;
1833+
1834+
codexCliEnhancementLoading = true;
1835+
1836+
try {
1837+
var response = await fetch('/api/language/codex-cli-enhancement', {
1838+
method: 'POST',
1839+
headers: { 'Content-Type': 'application/json' },
1840+
body: JSON.stringify({ action: 'refresh' })
1841+
});
1842+
1843+
if (!response.ok) {
1844+
var errData = await response.json();
1845+
var errorMsg = errData.error || 'Failed to refresh setting';
1846+
if (errorMsg.includes('not found')) {
1847+
showRefreshToast(t('lang.installRequired'), 'warning');
1848+
} else if (errorMsg.includes('not enabled')) {
1849+
showRefreshToast('CLI 调用增强未启用', 'warning');
1850+
} else {
1851+
showRefreshToast('刷新失败: ' + errorMsg, 'error');
1852+
}
1853+
throw new Error(errorMsg);
1854+
}
1855+
1856+
var data = await response.json();
1857+
1858+
// Update UI
1859+
renderLanguageSettingsSection();
1860+
1861+
// Show toast
1862+
showRefreshToast('CLI 调用增强已刷新', 'success');
1863+
} catch (err) {
1864+
console.error('Failed to refresh Codex CLI enhancement:', err);
1865+
// Error already shown in the !response.ok block
1866+
} finally {
1867+
codexCliEnhancementLoading = false;
1868+
}
1869+
}
1870+
18311871
async function renderLanguageSettingsSection() {
18321872
var container = document.getElementById('language-settings-section');
18331873
if (!container) return;
@@ -1914,8 +1954,15 @@ async function renderLanguageSettingsSection() {
19141954
'<span class="cli-setting-status ' + (codexCliEnhancementEnabled ? 'enabled' : 'disabled') + '">' +
19151955
(codexCliEnhancementEnabled ? t('lang.enabled') : t('lang.disabled')) +
19161956
'</span>' +
1957+
(codexCliEnhancementEnabled ?
1958+
'<button class="btn-icon-sm" onclick="refreshCodexCliEnhancement()" title="刷新 CLI 配置"' + (codexCliEnhancementLoading ? ' disabled' : '') + '>' +
1959+
'<i data-lucide="refresh-cw" class="w-3 h-3"></i>' +
1960+
'</button>'
1961+
: '') +
19171962
'</div>' +
1918-
'<p class="cli-setting-desc">为 Codex 启用多 CLI 工具调用功能,自动拼接 cli-tools-usage.md 和 cli-tools.json 配置</p>' +
1963+
'<p class="cli-setting-desc">为 Codex 启用多 CLI 工具调用功能,自动拼接 cli-tools-usage.md 和 cli-tools.json 配置' +
1964+
(codexCliEnhancementEnabled ? '<br><span style="color: var(--text-muted); font-size: 0.85em;">💡 配置文件变更后,点击刷新按钮更新内容</span>' : '') +
1965+
'</p>' +
19191966
'</div>' +
19201967
'</div>';
19211968

0 commit comments

Comments
 (0)