Files
agent-radar 2ed3415fb7
Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s
源码快照 @ 2026-09-01 (source-only, 剔除非源码素材)
2026-09-01 20:17:27 +08:00

104 lines
2.3 KiB
TypeScript

export function normalizeAgentTitleText(
value: string | null | undefined
): string {
const trimmed = value?.trim() ?? "";
if (!trimmed) {
return "";
}
let normalized = "";
for (let index = 0; index < trimmed.length; ) {
const link = markdownLinkAt(trimmed, index);
if (!link) {
normalized += trimmed[index];
index += 1;
continue;
}
normalized += unescapeMarkdownLabel(
trimmed.slice(link.labelStart, link.labelEnd)
);
index = link.hrefEnd + 1;
}
return normalized.replace(/\s+/gu, " ").trim();
}
function unescapeMarkdownLabel(label: string): string {
let normalized = "";
let escaped = false;
for (const character of label) {
if (escaped) {
normalized += "\\[]()".includes(character) ? character : `\\${character}`;
escaped = false;
continue;
}
if (character === "\\") {
escaped = true;
continue;
}
normalized += character;
}
return escaped ? `${normalized}\\` : normalized;
}
function markdownLinkAt(
value: string,
start: number
): {
hrefEnd: number;
labelEnd: number;
labelStart: number;
} | null {
if (value[start] !== "[") {
return null;
}
const labelEnd = findUnescaped(value, start + 1, "]");
if (labelEnd < 0 || value[labelEnd + 1] !== "(") {
return null;
}
const hrefEnd = findBalancedHrefEnd(value, labelEnd + 2);
return hrefEnd < 0 ? null : { hrefEnd, labelEnd, labelStart: start + 1 };
}
function findUnescaped(value: string, start: number, target: string): number {
let escaped = false;
for (let index = start; index < value.length; index += 1) {
if (escaped) {
escaped = false;
continue;
}
if (value[index] === "\\") {
escaped = true;
continue;
}
if (value[index] === target) {
return index;
}
}
return -1;
}
function findBalancedHrefEnd(value: string, start: number): number {
let depth = 0;
let escaped = false;
for (let index = start; index < value.length; index += 1) {
if (escaped) {
escaped = false;
continue;
}
switch (value[index]) {
case "\\":
escaped = true;
break;
case "(":
depth += 1;
break;
case ")":
if (depth === 0) {
return index;
}
depth -= 1;
break;
}
}
return -1;
}