mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-08-19 05:14:09 +08:00
- 抽离环境变量草稿解析工具,区分有效项和无效行 - 保留用户原始输入,避免无效行被静默吞掉 - 在 MCP 服务卡片中显示识别数量与无效行提示 - 补充环境变量解析与卡片提示测试
48 lines
1.1 KiB
TypeScript
48 lines
1.1 KiB
TypeScript
export interface ParsedMCPEnvDraft {
|
|
env: Record<string, string>;
|
|
invalidLines: string[];
|
|
totalLines: number;
|
|
validLines: number;
|
|
}
|
|
|
|
export const formatMCPEnvDraft = (env?: Record<string, string>): string =>
|
|
Object.entries(env || {})
|
|
.map(([key, value]) => `${key}=${value}`)
|
|
.join('\n');
|
|
|
|
export const parseMCPEnvDraft = (input: string): ParsedMCPEnvDraft => {
|
|
const env: Record<string, string> = {};
|
|
const invalidLines: string[] = [];
|
|
let totalLines = 0;
|
|
let validLines = 0;
|
|
|
|
String(input || '')
|
|
.split(/\r?\n/u)
|
|
.map((line) => line.trim())
|
|
.forEach((line) => {
|
|
if (!line) {
|
|
return;
|
|
}
|
|
totalLines += 1;
|
|
const separatorIndex = line.indexOf('=');
|
|
if (separatorIndex <= 0) {
|
|
invalidLines.push(line);
|
|
return;
|
|
}
|
|
const key = line.slice(0, separatorIndex).trim();
|
|
if (!key || /\s/u.test(key)) {
|
|
invalidLines.push(line);
|
|
return;
|
|
}
|
|
env[key] = line.slice(separatorIndex + 1);
|
|
validLines += 1;
|
|
});
|
|
|
|
return {
|
|
env,
|
|
invalidLines,
|
|
totalLines,
|
|
validLines,
|
|
};
|
|
};
|