Files
deerflow-code/frontend-web/src/core/tools/skill-invocation.ts
T

55 lines
1.4 KiB
TypeScript

const SKILL_NAME_ARG_TOOLS = new Set([
"skill_view",
"skill_manage",
"skill_archive",
]);
const SKILL_PATH_PATTERN =
/(?:^|[\\/])skills[\\/](?:public|custom)[\\/]([^\\/\s"'`;&|<>()]+)(?=[\\/]|$)/i;
function findSkillNameInValue(
value: unknown,
seen: Set<object>,
): string | undefined {
if (typeof value === "string") {
return SKILL_PATH_PATTERN.exec(value)?.[1];
}
if (!value || typeof value !== "object" || seen.has(value)) {
return undefined;
}
seen.add(value);
const values = Array.isArray(value)
? value
: Object.values(value as Record<string, unknown>);
for (const nested of values) {
const name = findSkillNameInValue(nested, seen);
if (name) {
return name;
}
}
return undefined;
}
/** Resolve a skill from actual tool arguments rather than model-authored prose. */
export function extractInvokedSkillName(
toolName: string,
args: unknown,
): string | undefined {
if (
SKILL_NAME_ARG_TOOLS.has(toolName) &&
args &&
typeof args === "object" &&
typeof (args as Record<string, unknown>).name === "string"
) {
const name = ((args as Record<string, unknown>).name as string).trim();
if (name) {
return name;
}
}
return findSkillNameInValue(args, new Set());
}
export function skillInvocationLabel(skillName: string): string {
return `调用技能「${skillName}」`;
}