mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 05:52:45 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -40,7 +40,9 @@ const DEFAULT_MAX_ATTACHMENTS = 1;
|
||||
|
||||
function normalizeAttachmentPath(raw?: string | null): string | undefined {
|
||||
const value = raw?.trim();
|
||||
if (!value) return undefined;
|
||||
if (!value) {
|
||||
return undefined;
|
||||
}
|
||||
if (value.startsWith("file://")) {
|
||||
try {
|
||||
return fileURLToPath(value);
|
||||
@@ -58,7 +60,9 @@ export function normalizeAttachments(ctx: MsgContext): MediaAttachment[] {
|
||||
const resolveMime = (count: number, index: number) => {
|
||||
const typeHint = typesFromArray?.[index];
|
||||
const trimmed = typeof typeHint === "string" ? typeHint.trim() : "";
|
||||
if (trimmed) return trimmed;
|
||||
if (trimmed) {
|
||||
return trimmed;
|
||||
}
|
||||
return count === 1 ? ctx.MediaType : undefined;
|
||||
};
|
||||
|
||||
@@ -89,7 +93,9 @@ export function normalizeAttachments(ctx: MsgContext): MediaAttachment[] {
|
||||
|
||||
const pathValue = ctx.MediaPath?.trim();
|
||||
const url = ctx.MediaUrl?.trim();
|
||||
if (!pathValue && !url) return [];
|
||||
if (!pathValue && !url) {
|
||||
return [];
|
||||
}
|
||||
return [
|
||||
{
|
||||
path: pathValue || undefined,
|
||||
@@ -104,12 +110,20 @@ export function resolveAttachmentKind(
|
||||
attachment: MediaAttachment,
|
||||
): "image" | "audio" | "video" | "document" | "unknown" {
|
||||
const kind = kindFromMime(attachment.mime);
|
||||
if (kind === "image" || kind === "audio" || kind === "video") return kind;
|
||||
if (kind === "image" || kind === "audio" || kind === "video") {
|
||||
return kind;
|
||||
}
|
||||
|
||||
const ext = getFileExtension(attachment.path ?? attachment.url);
|
||||
if (!ext) return "unknown";
|
||||
if ([".mp4", ".mov", ".mkv", ".webm", ".avi", ".m4v"].includes(ext)) return "video";
|
||||
if (isAudioFileName(attachment.path ?? attachment.url)) return "audio";
|
||||
if (!ext) {
|
||||
return "unknown";
|
||||
}
|
||||
if ([".mp4", ".mov", ".mkv", ".webm", ".avi", ".m4v"].includes(ext)) {
|
||||
return "video";
|
||||
}
|
||||
if (isAudioFileName(attachment.path ?? attachment.url)) {
|
||||
return "audio";
|
||||
}
|
||||
if ([".png", ".jpg", ".jpeg", ".webp", ".gif", ".bmp", ".tiff", ".tif"].includes(ext)) {
|
||||
return "image";
|
||||
}
|
||||
@@ -129,14 +143,22 @@ export function isImageAttachment(attachment: MediaAttachment): boolean {
|
||||
}
|
||||
|
||||
function isAbortError(err: unknown): boolean {
|
||||
if (!err) return false;
|
||||
if (err instanceof Error && err.name === "AbortError") return true;
|
||||
if (!err) {
|
||||
return false;
|
||||
}
|
||||
if (err instanceof Error && err.name === "AbortError") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function resolveRequestUrl(input: RequestInfo | URL): string {
|
||||
if (typeof input === "string") return input;
|
||||
if (input instanceof URL) return input.toString();
|
||||
if (typeof input === "string") {
|
||||
return input;
|
||||
}
|
||||
if (input instanceof URL) {
|
||||
return input.toString();
|
||||
}
|
||||
return input.url;
|
||||
}
|
||||
|
||||
@@ -144,8 +166,12 @@ function orderAttachments(
|
||||
attachments: MediaAttachment[],
|
||||
prefer?: MediaUnderstandingAttachmentsConfig["prefer"],
|
||||
): MediaAttachment[] {
|
||||
if (!prefer || prefer === "first") return attachments;
|
||||
if (prefer === "last") return [...attachments].toReversed();
|
||||
if (!prefer || prefer === "first") {
|
||||
return attachments;
|
||||
}
|
||||
if (prefer === "last") {
|
||||
return [...attachments].toReversed();
|
||||
}
|
||||
if (prefer === "path") {
|
||||
const withPath = attachments.filter((item) => item.path);
|
||||
const withoutPath = attachments.filter((item) => !item.path);
|
||||
@@ -166,11 +192,17 @@ export function selectAttachments(params: {
|
||||
}): MediaAttachment[] {
|
||||
const { capability, attachments, policy } = params;
|
||||
const matches = attachments.filter((item) => {
|
||||
if (capability === "image") return isImageAttachment(item);
|
||||
if (capability === "audio") return isAudioAttachment(item);
|
||||
if (capability === "image") {
|
||||
return isImageAttachment(item);
|
||||
}
|
||||
if (capability === "audio") {
|
||||
return isAudioAttachment(item);
|
||||
}
|
||||
return isVideoAttachment(item);
|
||||
});
|
||||
if (matches.length === 0) return [];
|
||||
if (matches.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const ordered = orderAttachments(matches, policy?.prefer);
|
||||
const mode = policy?.mode ?? "first";
|
||||
@@ -367,13 +399,19 @@ export class MediaAttachmentCache {
|
||||
|
||||
private resolveLocalPath(attachment: MediaAttachment): string | undefined {
|
||||
const rawPath = normalizeAttachmentPath(attachment.path);
|
||||
if (!rawPath) return undefined;
|
||||
if (!rawPath) {
|
||||
return undefined;
|
||||
}
|
||||
return path.isAbsolute(rawPath) ? rawPath : path.resolve(rawPath);
|
||||
}
|
||||
|
||||
private async ensureLocalStat(entry: AttachmentCacheEntry): Promise<number | undefined> {
|
||||
if (!entry.resolvedPath) return undefined;
|
||||
if (entry.statSize !== undefined) return entry.statSize;
|
||||
if (!entry.resolvedPath) {
|
||||
return undefined;
|
||||
}
|
||||
if (entry.statSize !== undefined) {
|
||||
return entry.statSize;
|
||||
}
|
||||
try {
|
||||
const stat = await fs.stat(entry.resolvedPath);
|
||||
if (!stat.isFile()) {
|
||||
|
||||
Reference in New Issue
Block a user