// Adapted from ego lite (MIT): package/ego-browser/src/help-runtime.ts export interface ApiProperty { name: string; type: string; optional: boolean; doc?: string; properties?: ApiProperty[]; } export interface ApiParam { name: string; type: string; optional: boolean; rest?: boolean; doc?: string; } export interface ApiElements { param: string; typeName?: string; properties?: ApiProperty[]; variants?: { kind: string; properties: ApiProperty[] }[]; } export interface ApiEntry { name: string; kind: 'method' | 'static' | 'getter'; signatures: string[]; summary: string; details?: string; params: ApiParam[]; options?: { typeName?: string; properties: ApiProperty[] }; elements?: ApiElements[]; examples: string[]; deprecated?: string; } export interface ApiReference { version: string; entries: ApiEntry[]; } const GROUPS = ['BrowserClaw', 'browser', 'page'] as const; function callShape(entry: ApiEntry): string { if (entry.kind === 'getter') return entry.name; const params = entry.params.map((p) => `${p.rest === true ? '...' : ''}${p.name}${p.optional ? '?' : ''}`); return `${entry.name}(${params.join(', ')})`; } export function formatIndex(ref: ApiReference): string { const lines = [`browserclaw v${ref.version} API. Details: npx browserclaw help , for example help page.click`]; for (const group of GROUPS) { const entries = ref.entries.filter((e) => e.name.startsWith(`${group}.`)); if (entries.length === 0) continue; lines.push('', group); for (const entry of entries) lines.push(` ${callShape(entry)} ${entry.summary}`); } return `${lines.join('\n')}\n`; } function table(rows: string[][], indent: string): string[] { const widths = rows[0]?.map((_, col) => Math.max(...rows.map((row) => (row[col] ?? '').length))) ?? []; return rows.map((row) => `${indent}${row.map((cell, col) => (col === row.length - 1 ? cell : cell.padEnd(widths[col] ?? 0))).join(' ')}`.trimEnd(), ); } function propertyLines(properties: ApiProperty[], indent: string): string[] { const lines: string[] = []; const rows = properties.map((p) => [`${p.name}${p.optional ? '?' : ''}`, p.type, p.doc ?? '']); table(rows, indent).forEach((line, i) => { lines.push(line); const nested = properties[i]?.properties; if (nested !== undefined && nested.length > 0) lines.push(...propertyLines(nested, `${indent} `)); }); return lines; } export function formatHelp(entry: ApiEntry): string { const lines = [...entry.signatures, '', entry.summary]; if (entry.details !== undefined && entry.details !== '') lines.push('', entry.details); if (entry.deprecated !== undefined) lines.push('', `Deprecated: ${entry.deprecated}`); if (entry.params.length > 0) { lines.push('', 'Parameters:'); const rows = entry.params.map((p) => [ `${p.rest === true ? '...' : ''}${p.name}${p.optional ? '?' : ''}`, p.type, p.doc ?? '', ]); lines.push(...table(rows, ' ')); } if (entry.options !== undefined) { lines.push('', entry.options.typeName === undefined ? 'Options:' : `Options (${entry.options.typeName}):`); lines.push(...propertyLines(entry.options.properties, ' ')); } for (const elements of entry.elements ?? []) { const type = elements.typeName === undefined ? '' : ` (${elements.typeName})`; if (elements.variants !== undefined) { lines.push('', `Each ${elements.param} item${type}, by kind:`); for (const variant of elements.variants) { const props = variant.properties .filter((p) => p.name !== 'kind') .map((p) => `${p.name}${p.optional ? '?' : ''}: ${p.type}`); lines.push(` kind "${variant.kind}": ${props.join(', ')}`); } } else if (elements.properties !== undefined) { lines.push('', `Each ${elements.param} item${type}:`); lines.push(...propertyLines(elements.properties, ' ')); } } if (entry.examples.length > 0) { lines.push('', 'Examples:'); for (const example of entry.examples) lines.push(example); } return `${lines.join('\n')}\n`; } function normalize(input: string): string { return input .trim() .replace(/^await\s+/, '') .replace(/\(.*$/, '') .replace(/^CrawlPage\./, 'page.') .replace(/^browser\.(launch|connect)$/, 'BrowserClaw.$1'); } function distance(a: string, b: string): number { let previous = Array.from({ length: b.length + 1 }, (_, i) => i); for (let i = 1; i <= a.length; i++) { const current = [i]; for (let j = 1; j <= b.length; j++) { const cost = a[i - 1] === b[j - 1] ? 0 : 1; current.push(Math.min((previous[j] ?? 0) + 1, (current[j - 1] ?? 0) + 1, (previous[j - 1] ?? 0) + cost)); } previous = current; } return previous[b.length] ?? 0; } const shortName = (name: string): string => name.slice(name.indexOf('.') + 1); function closeness(input: string, candidate: string): number { const a = input.toLowerCase(); const b = candidate.toLowerCase(); if (b.includes(a)) return a === b ? 0 : 1; let prefix = Infinity; for (let k = Math.max(1, a.length - 2); k <= Math.min(b.length, a.length + 2); k++) prefix = Math.min(prefix, distance(a, b.slice(0, k))); return Math.min(distance(a, b), prefix + 0.5); } export function resolveApiName( ref: ApiReference, input: string, ): { entry: ApiEntry } | { candidates: string[] } | { matches: string[] } { const name = normalize(input); const exact = ref.entries.find((e) => e.name === name); if (exact !== undefined) return { entry: exact }; if (name.startsWith('BrowserClaw.')) { const instance = ref.entries.find((e) => e.name === `browser.${shortName(name)}`); if (instance !== undefined) return { entry: instance }; } if (!name.includes('.')) { const byShort = ref.entries.filter((e) => shortName(e.name) === name); if (byShort.length === 1) return { entry: byShort[0] }; if (byShort.length > 1) return { candidates: byShort.map((e) => e.name) }; } const receiver = name.includes('.') ? name.slice(0, name.indexOf('.')) : undefined; const pool = ref.entries.filter((e) => receiver === undefined || e.name.startsWith(`${receiver}.`)); const scored = pool .map((e) => ({ name: e.name, score: closeness(shortName(name), shortName(e.name)) })) .filter((s) => s.score <= 2) .sort((x, y) => x.score - y.score || x.name.localeCompare(y.name)); return { matches: scored.slice(0, 5).map((s) => s.name) }; }