mirror of
https://github.com/lWolvesl/antigravity-usage-monitor.git
synced 2026-11-27 01:19:25 +08:00
perf(panel): optimize P1 hot paths and consolidate P2 shared primitives
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* @fileoverview Shared user prompt wrapper-tag stripping and short session title extraction.
|
||||
*/
|
||||
|
||||
const PROMPT_CLEAN_CONTEXT_SUMMARY_RE = /<CONTEXT_SUMMARY>[\s\S]*?<\/CONTEXT_SUMMARY>/gi;
|
||||
const PROMPT_CLEAN_SYSTEM_MSG_RE = /<SYSTEM_MESSAGE>[\s\S]*$/i;
|
||||
const PROMPT_CLEAN_METADATA_RE = /<ADDITIONAL_METADATA>[\s\S]*$/i;
|
||||
const PROMPT_CLEAN_USER_REQ_RE = /<USER_REQUEST>([\s\S]*?)<\/USER_REQUEST>/gi;
|
||||
|
||||
/**
|
||||
* Strips `<CONTEXT_SUMMARY>`, `<SYSTEM_MESSAGE>`, and `<ADDITIONAL_METADATA>` wrapper blocks
|
||||
* and returns the innermost `<USER_REQUEST>` body when present.
|
||||
*
|
||||
* @param {unknown} rawText
|
||||
* @returns {string}
|
||||
*/
|
||||
export function extractCleanUserRequestBody(rawText) {
|
||||
if (typeof rawText !== 'string' || !rawText.trim()) return '';
|
||||
const withoutSummary = rawText
|
||||
.replace(PROMPT_CLEAN_CONTEXT_SUMMARY_RE, '')
|
||||
.replace(PROMPT_CLEAN_SYSTEM_MSG_RE, '');
|
||||
const reqMatches = [...withoutSummary.matchAll(PROMPT_CLEAN_USER_REQ_RE)];
|
||||
const target =
|
||||
reqMatches.length > 0
|
||||
? reqMatches[reqMatches.length - 1][1]
|
||||
: withoutSummary.replace(PROMPT_CLEAN_METADATA_RE, '');
|
||||
return String(target || '').replace(PROMPT_CLEAN_METADATA_RE, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts a compact single-line title from raw user prompt text.
|
||||
*
|
||||
* @param {unknown} rawText
|
||||
* @param {number} [maxChars=80]
|
||||
* @param {{ firstLineOnly?: boolean }} [options={}]
|
||||
* @returns {string}
|
||||
*/
|
||||
export function extractShortPromptTitle(rawText, maxChars = 80, { firstLineOnly = false } = {}) {
|
||||
const body = extractCleanUserRequestBody(rawText);
|
||||
if (!body) return '';
|
||||
const candidate = firstLineOnly
|
||||
? body
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.replace(/\s+/g, ' ').trim())
|
||||
.find(Boolean) || ''
|
||||
: body.replace(/\s+/g, ' ').trim();
|
||||
if (!candidate) return '';
|
||||
return candidate.length <= maxChars ? candidate : `${candidate.slice(0, maxChars - 1).trimEnd()}…`;
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
* @fileoverview Shared `file://` workspace URI decoding, encoding, and SQLite column parsing helpers.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Parses a SQLite `workspace_uris` column value (either a JSON string array or a single URI string).
|
||||
*
|
||||
* @param {unknown} rawVal
|
||||
* @returns {string[]}
|
||||
*/
|
||||
export function parseWorkspaceUrisColumn(rawVal) {
|
||||
const str = String(rawVal || '').trim();
|
||||
if (!str) return [];
|
||||
if (str.startsWith('[')) {
|
||||
try {
|
||||
const parsed = JSON.parse(str);
|
||||
if (Array.isArray(parsed)) {
|
||||
return parsed.map((item) => String(item || '').trim()).filter(Boolean);
|
||||
}
|
||||
} catch {
|
||||
// Fall back to single URI string
|
||||
}
|
||||
}
|
||||
return [str];
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a `file://` URI or raw filesystem path into a clean slash-normalized path string.
|
||||
* Strips the leading slash before Windows drive letters (`/F:/...` -> `F:/...`) while keeping POSIX leading `/`.
|
||||
*
|
||||
* @param {unknown} rawUri
|
||||
* @returns {string}
|
||||
*/
|
||||
export function decodeWorkspaceFileUriToPath(rawUri) {
|
||||
const raw = String(rawUri || '').trim();
|
||||
if (!raw) return '';
|
||||
let decoded = raw;
|
||||
if (/^file:\/\//i.test(decoded)) {
|
||||
decoded = decoded.replace(/^file:\/\//i, '');
|
||||
try {
|
||||
decoded = decodeURIComponent(decoded);
|
||||
} catch {
|
||||
decoded = decoded.replace(/%3A/gi, ':');
|
||||
}
|
||||
if (/^\/[A-Za-z]:/.test(decoded)) {
|
||||
decoded = decoded.slice(1);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
decoded = decodeURIComponent(decoded);
|
||||
} catch {
|
||||
// Keep original text when percent sequences are literal path chars
|
||||
}
|
||||
}
|
||||
return decoded.replace(/\\/g, '/').replace(/\/+$/, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a workspace URI or local path for cross-platform equality comparison.
|
||||
*
|
||||
* @param {unknown} rawPath
|
||||
* @param {string} [platform=process.platform]
|
||||
* @returns {string}
|
||||
*/
|
||||
export function normalizePathForWorkspaceMatch(rawPath, platform = process.platform) {
|
||||
const clean = decodeWorkspaceFileUriToPath(rawPath).replace(/^\/+(?=[A-Za-z]:)/, '');
|
||||
return platform === 'win32' ? clean.toLowerCase() : clean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a `file://` URI or local path into a clean unencoded `file://` URI (`ag-cli` style).
|
||||
*
|
||||
* @param {unknown} rawInput
|
||||
* @returns {string}
|
||||
*/
|
||||
export function toRawWorkspaceFileUri(rawInput) {
|
||||
const raw = String(rawInput || '').trim();
|
||||
if (!raw) return '';
|
||||
if (/^file:\/\//i.test(raw)) {
|
||||
let body = raw.replace(/^file:\/\/\/?/i, '');
|
||||
try {
|
||||
body = decodeURIComponent(body);
|
||||
} catch {
|
||||
body = body.replace(/%3A/gi, ':');
|
||||
}
|
||||
body = body.replace(/\\/g, '/').replace(/^\/+/, '');
|
||||
return `file:///${body}`;
|
||||
}
|
||||
const normalized = raw.replace(/\\/g, '/');
|
||||
if (/^[a-zA-Z]:\//.test(normalized)) {
|
||||
return `file:///${normalized}`;
|
||||
}
|
||||
if (normalized.startsWith('/')) {
|
||||
return `file://${normalized}`;
|
||||
}
|
||||
return `file:///${normalized}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a `file://` URI or local path into Antigravity Desktop encoded URI form
|
||||
* (lowercase Windows drive letter + `%3A`).
|
||||
*
|
||||
* @param {unknown} rawInput
|
||||
* @returns {string}
|
||||
*/
|
||||
export function toDesktopEncodedWorkspaceUri(rawInput) {
|
||||
const raw = String(rawInput || '').trim();
|
||||
if (!raw) return '';
|
||||
if (/^file:\/\/\/[a-z]%3A\//i.test(raw)) {
|
||||
return raw.replace(/^file:\/\/\/([a-zA-Z])%3A\//i, (_, d) => `file:///${d.toLowerCase()}%3A/`);
|
||||
}
|
||||
const unencoded = toRawWorkspaceFileUri(raw);
|
||||
const body = unencoded.replace(/^file:\/\/\//i, '');
|
||||
const winMatch = body.match(/^([a-zA-Z]):(.*)$/);
|
||||
if (winMatch) {
|
||||
const drive = winMatch[1].toLowerCase();
|
||||
const rest = winMatch[2]
|
||||
.split('/')
|
||||
.map((seg) => (seg ? encodeURIComponent(seg) : ''))
|
||||
.join('/');
|
||||
return `file:///${drive}%3A${rest}`;
|
||||
}
|
||||
const encodedPath = body
|
||||
.split('/')
|
||||
.map((seg) => (seg ? encodeURIComponent(seg) : ''))
|
||||
.join('/');
|
||||
return `file:///${encodedPath}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts a short project name and readable display path from a `file:///` URI.
|
||||
*
|
||||
* @param {unknown} rawUri
|
||||
* @returns {{ name: string, displayPath: string }}
|
||||
*/
|
||||
export function parseWorkspaceDisplayLabel(rawUri) {
|
||||
const raw = String(rawUri || '').trim();
|
||||
if (!raw) return { name: '', displayPath: '' };
|
||||
let decoded = raw.replace(/^file:\/\/\/?/i, '');
|
||||
try {
|
||||
decoded = decodeURIComponent(decoded);
|
||||
} catch {
|
||||
decoded = decoded.replace(/%3A/gi, ':');
|
||||
}
|
||||
decoded = decoded.replace(/\\/g, '/').replace(/\/+$/, '');
|
||||
if (!decoded) return { name: '', displayPath: '' };
|
||||
const parts = decoded.split('/').filter(Boolean);
|
||||
const name = parts.length > 0 ? parts[parts.length - 1] : decoded;
|
||||
return { name, displayPath: decoded };
|
||||
}
|
||||
Reference in New Issue
Block a user