complete codex cliproxyapi parity

This commit is contained in:
cita
2026-03-20 11:23:29 +08:00
parent 5403a946c6
commit adc4fb1be7
28 changed files with 2969 additions and 204 deletions
@@ -0,0 +1,50 @@
const TOOL_NAME_LIMIT = 64;
const MCP_PREFIX = 'mcp__';
function asTrimmedString(value: unknown): string {
return typeof value === 'string' ? value.trim() : '';
}
export function shortenToolNameIfNeeded(name: string): string {
const trimmed = asTrimmedString(name);
if (trimmed.length <= TOOL_NAME_LIMIT) return trimmed;
if (trimmed.startsWith(MCP_PREFIX)) {
const lastSeparator = trimmed.lastIndexOf('__');
if (lastSeparator > 0) {
const candidate = `${MCP_PREFIX}${trimmed.slice(lastSeparator + 2)}`;
return candidate.length > TOOL_NAME_LIMIT ? candidate.slice(0, TOOL_NAME_LIMIT) : candidate;
}
}
return trimmed.slice(0, TOOL_NAME_LIMIT);
}
export function buildShortToolNameMap(names: string[]): Record<string, string> {
const uniqueNames = Array.from(new Set(
names
.map((name) => asTrimmedString(name))
.filter((name) => name.length > 0),
));
const used = new Set<string>();
const mapping: Record<string, string> = {};
for (const name of uniqueNames) {
const base = shortenToolNameIfNeeded(name);
let candidate = base;
let suffixIndex = 1;
while (used.has(candidate)) {
const suffix = `_${suffixIndex}`;
const allowedLength = Math.max(0, TOOL_NAME_LIMIT - suffix.length);
candidate = `${base.slice(0, allowedLength)}${suffix}`;
suffixIndex += 1;
}
used.add(candidate);
mapping[name] = candidate;
}
return mapping;
}
export function getShortToolName(name: string, mapping: Record<string, string>): string {
const trimmed = asTrimmedString(name);
return mapping[trimmed] || shortenToolNameIfNeeded(trimmed);
}