mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-22 07:01:44 +03:00
refactor(browser): dedupe role snapshot parsing
This commit is contained in:
@@ -92,6 +92,31 @@ function getIndentLevel(line: string): number {
|
|||||||
return match ? Math.floor(match[1].length / 2) : 0;
|
return match ? Math.floor(match[1].length / 2) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function matchInteractiveSnapshotLine(
|
||||||
|
line: string,
|
||||||
|
options: RoleSnapshotOptions,
|
||||||
|
): { roleRaw: string; role: string; name?: string; suffix: string } | null {
|
||||||
|
const depth = getIndentLevel(line);
|
||||||
|
if (options.maxDepth !== undefined && depth > options.maxDepth) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const match = line.match(/^(\s*-\s*)(\w+)(?:\s+"([^"]*)")?(.*)$/);
|
||||||
|
if (!match) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const [, , roleRaw, name, suffix] = match;
|
||||||
|
if (roleRaw.startsWith("/")) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const role = roleRaw.toLowerCase();
|
||||||
|
return {
|
||||||
|
roleRaw,
|
||||||
|
role,
|
||||||
|
...(name ? { name } : {}),
|
||||||
|
suffix,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
type RoleNameTracker = {
|
type RoleNameTracker = {
|
||||||
counts: Map<string, number>;
|
counts: Map<string, number>;
|
||||||
refsByKey: Map<string, string[]>;
|
refsByKey: Map<string, string[]>;
|
||||||
@@ -271,21 +296,11 @@ export function buildRoleSnapshotFromAriaSnapshot(
|
|||||||
if (options.interactive) {
|
if (options.interactive) {
|
||||||
const result: string[] = [];
|
const result: string[] = [];
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
const depth = getIndentLevel(line);
|
const parsed = matchInteractiveSnapshotLine(line, options);
|
||||||
if (options.maxDepth !== undefined && depth > options.maxDepth) {
|
if (!parsed) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
const { roleRaw, role, name, suffix } = parsed;
|
||||||
const match = line.match(/^(\s*-\s*)(\w+)(?:\s+"([^"]*)")?(.*)$/);
|
|
||||||
if (!match) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const [, , roleRaw, name, suffix] = match;
|
|
||||||
if (roleRaw.startsWith("/")) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const role = roleRaw.toLowerCase();
|
|
||||||
if (!INTERACTIVE_ROLES.has(role)) {
|
if (!INTERACTIVE_ROLES.has(role)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -357,19 +372,11 @@ export function buildRoleSnapshotFromAiSnapshot(
|
|||||||
if (options.interactive) {
|
if (options.interactive) {
|
||||||
const out: string[] = [];
|
const out: string[] = [];
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
const depth = getIndentLevel(line);
|
const parsed = matchInteractiveSnapshotLine(line, options);
|
||||||
if (options.maxDepth !== undefined && depth > options.maxDepth) {
|
if (!parsed) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const match = line.match(/^(\s*-\s*)(\w+)(?:\s+"([^"]*)")?(.*)$/);
|
const { roleRaw, role, name, suffix } = parsed;
|
||||||
if (!match) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const [, , roleRaw, name, suffix] = match;
|
|
||||||
if (roleRaw.startsWith("/")) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const role = roleRaw.toLowerCase();
|
|
||||||
if (!INTERACTIVE_ROLES.has(role)) {
|
if (!INTERACTIVE_ROLES.has(role)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user