feat: add CLI settings export/import functionality

- Implemented exportSettings and importSettings APIs for CLI settings.
- Added hooks useExportSettings and useImportSettings for managing export/import operations in the frontend.
- Updated SettingsPage to include buttons for exporting and importing CLI settings.
- Enhanced backend to handle export and import requests, including validation and conflict resolution.
- Introduced new data structures for exported settings and import options.
- Updated localization files to support new export/import features.
- Refactored CLI tool configurations to remove hardcoded model defaults, allowing dynamic model retrieval.
This commit is contained in:
catlog22
2026-02-25 21:40:24 +08:00
parent 4c2bf31525
commit b2b8688d26
24 changed files with 1287 additions and 651 deletions

View File

@@ -12,9 +12,11 @@ import {
toggleEndpointEnabled,
getSettingsFilePath,
ensureSettingsDir,
sanitizeEndpointId
sanitizeEndpointId,
exportAllSettings,
importSettings
} from '../../config/cli-settings-manager.js';
import type { SaveEndpointRequest } from '../../types/cli-settings.js';
import type { SaveEndpointRequest, ImportOptions } from '../../types/cli-settings.js';
import { validateSettings } from '../../types/cli-settings.js';
import { syncBuiltinToolsAvailability, getBuiltinToolsSyncReport } from '../../tools/claude-cli-tools.js';
@@ -275,5 +277,62 @@ export async function handleCliSettingsRoutes(ctx: RouteContext): Promise<boolea
return true;
}
// ========== EXPORT SETTINGS ==========
// GET /api/cli/settings/export
if (pathname === '/api/cli/settings/export' && req.method === 'GET') {
try {
const exportData = exportAllSettings();
const jsonContent = JSON.stringify(exportData, null, 2);
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').substring(0, 19);
const filename = `cli-settings-export-${timestamp}.json`;
res.writeHead(200, {
'Content-Type': 'application/json',
'Content-Disposition': `attachment; filename="${filename}"`,
'Content-Length': Buffer.byteLength(jsonContent, 'utf-8')
});
res.end(jsonContent);
} catch (err) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: (err as Error).message }));
}
return true;
}
// ========== IMPORT SETTINGS ==========
// POST /api/cli/settings/import
if (pathname === '/api/cli/settings/import' && req.method === 'POST') {
handlePostRequest(req, res, async (body: unknown) => {
try {
// Extract import options and data from request
const request = body as { data?: unknown; options?: ImportOptions };
if (!request.data) {
return { error: 'Missing export data in request body', status: 400 };
}
const result = importSettings(request.data, request.options);
if (result.success) {
// Broadcast import event
broadcastToClients({
type: 'CLI_SETTINGS_IMPORTED',
payload: {
imported: result.imported,
skipped: result.skipped,
importedIds: result.importedIds,
timestamp: new Date().toISOString()
}
});
}
return result;
} catch (err) {
return { error: (err as Error).message, status: 500 };
}
});
return true;
}
return false;
}