mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 21:58:26 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -78,9 +78,15 @@ function resolveAvatarMime(filePath: string): string {
|
||||
}
|
||||
|
||||
function isWorkspaceRelativePath(value: string): boolean {
|
||||
if (!value) return false;
|
||||
if (value.startsWith("~")) return false;
|
||||
if (AVATAR_SCHEME_RE.test(value) && !WINDOWS_ABS_RE.test(value)) return false;
|
||||
if (!value) {
|
||||
return false;
|
||||
}
|
||||
if (value.startsWith("~")) {
|
||||
return false;
|
||||
}
|
||||
if (AVATAR_SCHEME_RE.test(value) && !WINDOWS_ABS_RE.test(value)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -89,19 +95,31 @@ function resolveIdentityAvatarUrl(
|
||||
agentId: string,
|
||||
avatar: string | undefined,
|
||||
): string | undefined {
|
||||
if (!avatar) return undefined;
|
||||
if (!avatar) {
|
||||
return undefined;
|
||||
}
|
||||
const trimmed = avatar.trim();
|
||||
if (!trimmed) return undefined;
|
||||
if (AVATAR_DATA_RE.test(trimmed) || AVATAR_HTTP_RE.test(trimmed)) return trimmed;
|
||||
if (!isWorkspaceRelativePath(trimmed)) return undefined;
|
||||
if (!trimmed) {
|
||||
return undefined;
|
||||
}
|
||||
if (AVATAR_DATA_RE.test(trimmed) || AVATAR_HTTP_RE.test(trimmed)) {
|
||||
return trimmed;
|
||||
}
|
||||
if (!isWorkspaceRelativePath(trimmed)) {
|
||||
return undefined;
|
||||
}
|
||||
const workspaceDir = resolveAgentWorkspaceDir(cfg, agentId);
|
||||
const workspaceRoot = path.resolve(workspaceDir);
|
||||
const resolved = path.resolve(workspaceRoot, trimmed);
|
||||
const relative = path.relative(workspaceRoot, resolved);
|
||||
if (relative.startsWith("..") || path.isAbsolute(relative)) return undefined;
|
||||
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
||||
return undefined;
|
||||
}
|
||||
try {
|
||||
const stat = fs.statSync(resolved);
|
||||
if (!stat.isFile() || stat.size > AVATAR_MAX_BYTES) return undefined;
|
||||
if (!stat.isFile() || stat.size > AVATAR_MAX_BYTES) {
|
||||
return undefined;
|
||||
}
|
||||
const buffer = fs.readFileSync(resolved);
|
||||
const mime = resolveAvatarMime(resolved);
|
||||
return `data:${mime};base64,${buffer.toString("base64")}`;
|
||||
@@ -121,10 +139,14 @@ function formatSessionIdPrefix(sessionId: string, updatedAt?: number | null): st
|
||||
}
|
||||
|
||||
function truncateTitle(text: string, maxLen: number): string {
|
||||
if (text.length <= maxLen) return text;
|
||||
if (text.length <= maxLen) {
|
||||
return text;
|
||||
}
|
||||
const cut = text.slice(0, maxLen - 1);
|
||||
const lastSpace = cut.lastIndexOf(" ");
|
||||
if (lastSpace > maxLen * 0.6) return cut.slice(0, lastSpace) + "…";
|
||||
if (lastSpace > maxLen * 0.6) {
|
||||
return cut.slice(0, lastSpace) + "…";
|
||||
}
|
||||
return cut + "…";
|
||||
}
|
||||
|
||||
@@ -132,7 +154,9 @@ export function deriveSessionTitle(
|
||||
entry: SessionEntry | undefined,
|
||||
firstUserMessage?: string | null,
|
||||
): string | undefined {
|
||||
if (!entry) return undefined;
|
||||
if (!entry) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (entry.displayName?.trim()) {
|
||||
return entry.displayName.trim();
|
||||
@@ -166,8 +190,12 @@ export function loadSessionEntry(sessionKey: string) {
|
||||
}
|
||||
|
||||
export function classifySessionKey(key: string, entry?: SessionEntry): GatewaySessionRow["kind"] {
|
||||
if (key === "global") return "global";
|
||||
if (key === "unknown") return "unknown";
|
||||
if (key === "global") {
|
||||
return "global";
|
||||
}
|
||||
if (key === "unknown") {
|
||||
return "unknown";
|
||||
}
|
||||
if (entry?.chatType === "group" || entry?.chatType === "channel") {
|
||||
return "group";
|
||||
}
|
||||
@@ -216,7 +244,9 @@ function listConfiguredAgentIds(cfg: OpenClawConfig): string[] {
|
||||
if (agents.length > 0) {
|
||||
const ids = new Set<string>();
|
||||
for (const entry of agents) {
|
||||
if (entry?.id) ids.add(normalizeAgentId(entry.id));
|
||||
if (entry?.id) {
|
||||
ids.add(normalizeAgentId(entry.id));
|
||||
}
|
||||
}
|
||||
const defaultId = normalizeAgentId(resolveDefaultAgentId(cfg));
|
||||
ids.add(defaultId);
|
||||
@@ -230,7 +260,9 @@ function listConfiguredAgentIds(cfg: OpenClawConfig): string[] {
|
||||
const ids = new Set<string>();
|
||||
const defaultId = normalizeAgentId(resolveDefaultAgentId(cfg));
|
||||
ids.add(defaultId);
|
||||
for (const id of listExistingAgentIdsFromDisk()) ids.add(id);
|
||||
for (const id of listExistingAgentIdsFromDisk()) {
|
||||
ids.add(id);
|
||||
}
|
||||
const sorted = Array.from(ids).filter(Boolean);
|
||||
sorted.sort((a, b) => a.localeCompare(b));
|
||||
if (sorted.includes(defaultId)) {
|
||||
@@ -253,7 +285,9 @@ export function listAgentsForGateway(cfg: OpenClawConfig): {
|
||||
{ name?: string; identity?: GatewayAgentRow["identity"] }
|
||||
>();
|
||||
for (const entry of cfg.agents?.list ?? []) {
|
||||
if (!entry?.id) continue;
|
||||
if (!entry?.id) {
|
||||
continue;
|
||||
}
|
||||
const identity = entry.identity
|
||||
? {
|
||||
name: entry.identity.name?.trim() || undefined,
|
||||
@@ -296,8 +330,12 @@ export function listAgentsForGateway(cfg: OpenClawConfig): {
|
||||
}
|
||||
|
||||
function canonicalizeSessionKeyForAgent(agentId: string, key: string): string {
|
||||
if (key === "global" || key === "unknown") return key;
|
||||
if (key.startsWith("agent:")) return key;
|
||||
if (key === "global" || key === "unknown") {
|
||||
return key;
|
||||
}
|
||||
if (key.startsWith("agent:")) {
|
||||
return key;
|
||||
}
|
||||
return `agent:${normalizeAgentId(agentId)}:${key}`;
|
||||
}
|
||||
|
||||
@@ -310,8 +348,12 @@ export function resolveSessionStoreKey(params: {
|
||||
sessionKey: string;
|
||||
}): string {
|
||||
const raw = params.sessionKey.trim();
|
||||
if (!raw) return raw;
|
||||
if (raw === "global" || raw === "unknown") return raw;
|
||||
if (!raw) {
|
||||
return raw;
|
||||
}
|
||||
if (raw === "global" || raw === "unknown") {
|
||||
return raw;
|
||||
}
|
||||
|
||||
const parsed = parseAgentSessionKey(raw);
|
||||
if (parsed) {
|
||||
@@ -321,7 +363,9 @@ export function resolveSessionStoreKey(params: {
|
||||
agentId,
|
||||
sessionKey: raw,
|
||||
});
|
||||
if (canonical !== raw) return canonical;
|
||||
if (canonical !== raw) {
|
||||
return canonical;
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
|
||||
@@ -338,15 +382,23 @@ function resolveSessionStoreAgentId(cfg: OpenClawConfig, canonicalKey: string):
|
||||
return resolveDefaultStoreAgentId(cfg);
|
||||
}
|
||||
const parsed = parseAgentSessionKey(canonicalKey);
|
||||
if (parsed?.agentId) return normalizeAgentId(parsed.agentId);
|
||||
if (parsed?.agentId) {
|
||||
return normalizeAgentId(parsed.agentId);
|
||||
}
|
||||
return resolveDefaultStoreAgentId(cfg);
|
||||
}
|
||||
|
||||
function canonicalizeSpawnedByForAgent(agentId: string, spawnedBy?: string): string | undefined {
|
||||
const raw = spawnedBy?.trim();
|
||||
if (!raw) return undefined;
|
||||
if (raw === "global" || raw === "unknown") return raw;
|
||||
if (raw.startsWith("agent:")) return raw;
|
||||
if (!raw) {
|
||||
return undefined;
|
||||
}
|
||||
if (raw === "global" || raw === "unknown") {
|
||||
return raw;
|
||||
}
|
||||
if (raw.startsWith("agent:")) {
|
||||
return raw;
|
||||
}
|
||||
return `agent:${normalizeAgentId(agentId)}:${raw}`;
|
||||
}
|
||||
|
||||
@@ -372,7 +424,9 @@ export function resolveGatewaySessionStoreTarget(params: { cfg: OpenClawConfig;
|
||||
|
||||
const storeKeys = new Set<string>();
|
||||
storeKeys.add(canonicalKey);
|
||||
if (key && key !== canonicalKey) storeKeys.add(key);
|
||||
if (key && key !== canonicalKey) {
|
||||
storeKeys.add(key);
|
||||
}
|
||||
return {
|
||||
agentId,
|
||||
storePath,
|
||||
@@ -509,23 +563,37 @@ export function listSessionsFromStore(params: {
|
||||
|
||||
let sessions = Object.entries(store)
|
||||
.filter(([key]) => {
|
||||
if (!includeGlobal && key === "global") return false;
|
||||
if (!includeUnknown && key === "unknown") return false;
|
||||
if (!includeGlobal && key === "global") {
|
||||
return false;
|
||||
}
|
||||
if (!includeUnknown && key === "unknown") {
|
||||
return false;
|
||||
}
|
||||
if (agentId) {
|
||||
if (key === "global" || key === "unknown") return false;
|
||||
if (key === "global" || key === "unknown") {
|
||||
return false;
|
||||
}
|
||||
const parsed = parseAgentSessionKey(key);
|
||||
if (!parsed) return false;
|
||||
if (!parsed) {
|
||||
return false;
|
||||
}
|
||||
return normalizeAgentId(parsed.agentId) === agentId;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.filter(([key, entry]) => {
|
||||
if (!spawnedBy) return true;
|
||||
if (key === "unknown" || key === "global") return false;
|
||||
if (!spawnedBy) {
|
||||
return true;
|
||||
}
|
||||
if (key === "unknown" || key === "global") {
|
||||
return false;
|
||||
}
|
||||
return entry?.spawnedBy === spawnedBy;
|
||||
})
|
||||
.filter(([, entry]) => {
|
||||
if (!label) return true;
|
||||
if (!label) {
|
||||
return true;
|
||||
}
|
||||
return entry?.label === label;
|
||||
})
|
||||
.map(([key, entry]) => {
|
||||
@@ -628,7 +696,9 @@ export function listSessionsFromStore(params: {
|
||||
storePath,
|
||||
entry.sessionFile,
|
||||
);
|
||||
if (lastMsg) lastMessagePreview = lastMsg;
|
||||
if (lastMsg) {
|
||||
lastMessagePreview = lastMsg;
|
||||
}
|
||||
}
|
||||
}
|
||||
return { ...rest, derivedTitle, lastMessagePreview } satisfies GatewaySessionRow;
|
||||
|
||||
Reference in New Issue
Block a user