chore: Enable "curly" rule to avoid single-statement if confusion/errors.

This commit is contained in:
cpojer
2026-01-31 16:19:20 +09:00
parent 009b16fab8
commit 5ceff756e1
1266 changed files with 27871 additions and 9393 deletions

View File

@@ -95,12 +95,16 @@ export function injectHistoryImagesIntoMessages(
messages: AgentMessage[],
historyImagesByIndex: Map<number, ImageContent[]>,
): boolean {
if (historyImagesByIndex.size === 0) return false;
if (historyImagesByIndex.size === 0) {
return false;
}
let didMutate = false;
for (const [msgIndex, images] of historyImagesByIndex) {
// Bounds check: ensure index is valid before accessing
if (msgIndex < 0 || msgIndex >= messages.length) continue;
if (msgIndex < 0 || msgIndex >= messages.length) {
continue;
}
const msg = messages[msgIndex];
if (msg && msg.role === "user") {
// Convert string content to array format if needed
@@ -256,7 +260,9 @@ export async function runEmbeddedAttempt(
accountId: params.agentAccountId ?? undefined,
});
if (inlineButtonsScope !== "off") {
if (!runtimeCapabilities) runtimeCapabilities = [];
if (!runtimeCapabilities) {
runtimeCapabilities = [];
}
if (
!runtimeCapabilities.some((cap) => String(cap).trim().toLowerCase() === "inlinebuttons")
) {
@@ -575,7 +581,9 @@ export async function runEmbeddedAttempt(
};
const abortRun = (isTimeout = false, reason?: unknown) => {
aborted = true;
if (isTimeout) timedOut = true;
if (isTimeout) {
timedOut = true;
}
if (isTimeout) {
runAbortController.abort(reason ?? makeTimeoutAbortReason());
} else {
@@ -660,7 +668,9 @@ export async function runEmbeddedAttempt(
abortRun(true);
if (!abortWarnTimer) {
abortWarnTimer = setTimeout(() => {
if (!activeSession.isStreaming) return;
if (!activeSession.isStreaming) {
return;
}
if (!isProbeSession) {
log.warn(
`embedded run abort still streaming: runId=${params.runId} sessionId=${params.sessionId}`,
@@ -808,7 +818,9 @@ export async function runEmbeddedAttempt(
await waitForCompactionRetry();
} catch (err) {
if (isAbortError(err)) {
if (!promptError) promptError = err;
if (!promptError) {
promptError = err;
}
} else {
throw err;
}
@@ -846,7 +858,9 @@ export async function runEmbeddedAttempt(
}
} finally {
clearTimeout(abortTimer);
if (abortWarnTimer) clearTimeout(abortWarnTimer);
if (abortWarnTimer) {
clearTimeout(abortWarnTimer);
}
unsubscribe();
clearActiveEmbeddedRun(params.sessionId, queueHandle);
params.abortSignal?.removeEventListener?.("abort", onAbort);

View File

@@ -80,9 +80,15 @@ export function detectImageReferences(prompt: string): DetectedImageRef[] {
// Helper to add a path ref
const addPathRef = (raw: string) => {
const trimmed = raw.trim();
if (!trimmed || seen.has(trimmed.toLowerCase())) return;
if (trimmed.startsWith("http://") || trimmed.startsWith("https://")) return;
if (!isImageExtension(trimmed)) return;
if (!trimmed || seen.has(trimmed.toLowerCase())) {
return;
}
if (trimmed.startsWith("http://") || trimmed.startsWith("https://")) {
return;
}
if (!isImageExtension(trimmed)) {
return;
}
seen.add(trimmed.toLowerCase());
const resolved = trimmed.startsWith("~") ? resolveUserPath(trimmed) : trimmed;
refs.push({ raw: trimmed, type: "path", resolved });
@@ -118,7 +124,9 @@ export function detectImageReferences(prompt: string): DetectedImageRef[] {
/\[Image:\s*source:\s*([^\]]+\.(?:png|jpe?g|gif|webp|bmp|tiff?|heic|heif))\]/gi;
while ((match = messageImagePattern.exec(prompt)) !== null) {
const raw = match[1]?.trim();
if (raw) addPathRef(raw);
if (raw) {
addPathRef(raw);
}
}
// Remote HTTP(S) URLs are intentionally ignored. Native image injection is local-only.
@@ -127,7 +135,9 @@ export function detectImageReferences(prompt: string): DetectedImageRef[] {
const fileUrlPattern = /file:\/\/[^\s<>"'`\]]+\.(?:png|jpe?g|gif|webp|bmp|tiff?|heic|heif)/gi;
while ((match = fileUrlPattern.exec(prompt)) !== null) {
const raw = match[0];
if (seen.has(raw.toLowerCase())) continue;
if (seen.has(raw.toLowerCase())) {
continue;
}
seen.add(raw.toLowerCase());
// Use fileURLToPath for proper handling (e.g., file://localhost/path)
try {
@@ -148,7 +158,9 @@ export function detectImageReferences(prompt: string): DetectedImageRef[] {
/(?:^|\s|["'`(])((\.\.?\/|[~/])[^\s"'`()[\]]*\.(?:png|jpe?g|gif|webp|bmp|tiff?|heic|heif))/gi;
while ((match = pathPattern.exec(prompt)) !== null) {
// Use capture group 1 (the path without delimiter prefix); skip if undefined
if (match[1]) addPathRef(match[1]);
if (match[1]) {
addPathRef(match[1]);
}
}
return refs;
@@ -267,9 +279,13 @@ function detectImagesFromHistory(messages: unknown[]): DetectedImageRef[] {
const seen = new Set<string>();
const messageHasImageContent = (msg: unknown): boolean => {
if (!msg || typeof msg !== "object") return false;
if (!msg || typeof msg !== "object") {
return false;
}
const content = (msg as { content?: unknown }).content;
if (!Array.isArray(content)) return false;
if (!Array.isArray(content)) {
return false;
}
return content.some(
(part) =>
part != null && typeof part === "object" && (part as { type?: string }).type === "image",
@@ -278,20 +294,30 @@ function detectImagesFromHistory(messages: unknown[]): DetectedImageRef[] {
for (let i = 0; i < messages.length; i++) {
const msg = messages[i];
if (!msg || typeof msg !== "object") continue;
if (!msg || typeof msg !== "object") {
continue;
}
const message = msg as { role?: string };
// Only scan user messages for image references
if (message.role !== "user") continue;
if (message.role !== "user") {
continue;
}
// Skip if message already has image content (prevents reloading each turn)
if (messageHasImageContent(msg)) continue;
if (messageHasImageContent(msg)) {
continue;
}
const text = extractTextFromMessage(msg);
if (!text) continue;
if (!text) {
continue;
}
const refs = detectImageReferences(text);
for (const ref of refs) {
const key = ref.resolved.toLowerCase();
if (seen.has(key)) continue;
if (seen.has(key)) {
continue;
}
seen.add(key);
allRefs.push({ ...ref, messageIndex: i });
}

View File

@@ -76,7 +76,9 @@ export function buildEmbeddedRunPayloads(params: {
: null;
const normalizedErrorText = errorText ? normalizeTextForComparison(errorText) : null;
const genericErrorText = "The AI service returned an error. Please try again.";
if (errorText) replyItems.push({ text: errorText, isError: true });
if (errorText) {
replyItems.push({ text: errorText, isError: true });
}
const inlineToolResults =
params.inlineToolResultsAllowed && params.verboseLevel !== "off" && params.toolMetas.length > 0;
@@ -110,31 +112,51 @@ export function buildEmbeddedRunPayloads(params: {
params.lastAssistant && params.reasoningLevel === "on"
? formatReasoningMessage(extractAssistantThinking(params.lastAssistant))
: "";
if (reasoningText) replyItems.push({ text: reasoningText });
if (reasoningText) {
replyItems.push({ text: reasoningText });
}
const fallbackAnswerText = params.lastAssistant ? extractAssistantText(params.lastAssistant) : "";
const shouldSuppressRawErrorText = (text: string) => {
if (!lastAssistantErrored) return false;
if (!lastAssistantErrored) {
return false;
}
const trimmed = text.trim();
if (!trimmed) return false;
if (!trimmed) {
return false;
}
if (errorText) {
const normalized = normalizeTextForComparison(trimmed);
if (normalized && normalizedErrorText && normalized === normalizedErrorText) return true;
if (trimmed === genericErrorText) return true;
if (normalized && normalizedErrorText && normalized === normalizedErrorText) {
return true;
}
if (trimmed === genericErrorText) {
return true;
}
}
if (rawErrorMessage && trimmed === rawErrorMessage) {
return true;
}
if (formattedRawErrorMessage && trimmed === formattedRawErrorMessage) {
return true;
}
if (rawErrorMessage && trimmed === rawErrorMessage) return true;
if (formattedRawErrorMessage && trimmed === formattedRawErrorMessage) return true;
if (normalizedRawErrorText) {
const normalized = normalizeTextForComparison(trimmed);
if (normalized && normalized === normalizedRawErrorText) return true;
if (normalized && normalized === normalizedRawErrorText) {
return true;
}
}
if (normalizedFormattedRawErrorMessage) {
const normalized = normalizeTextForComparison(trimmed);
if (normalized && normalized === normalizedFormattedRawErrorMessage) return true;
if (normalized && normalized === normalizedFormattedRawErrorMessage) {
return true;
}
}
if (rawErrorFingerprint) {
const fingerprint = getApiErrorPayloadFingerprint(trimmed);
if (fingerprint && fingerprint === rawErrorFingerprint) return true;
if (fingerprint && fingerprint === rawErrorFingerprint) {
return true;
}
}
return isRawApiErrorPayload(trimmed);
};
@@ -222,8 +244,12 @@ export function buildEmbeddedRunPayloads(params: {
audioAsVoice: item.audioAsVoice || Boolean(hasAudioAsVoiceTag && item.media?.length),
}))
.filter((p) => {
if (!p.text && !p.mediaUrl && (!p.mediaUrls || p.mediaUrls.length === 0)) return false;
if (p.text && isSilentReplyText(p.text, SILENT_REPLY_TOKEN)) return false;
if (!p.text && !p.mediaUrl && (!p.mediaUrls || p.mediaUrls.length === 0)) {
return false;
}
if (p.text && isSilentReplyText(p.text, SILENT_REPLY_TOKEN)) {
return false;
}
return true;
});
}