mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 18:08:27 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -45,17 +45,27 @@ function asMessages(val: unknown): OpenAiChatMessage[] {
|
||||
}
|
||||
|
||||
function extractTextContent(content: unknown): string {
|
||||
if (typeof content === "string") return content;
|
||||
if (typeof content === "string") {
|
||||
return content;
|
||||
}
|
||||
if (Array.isArray(content)) {
|
||||
return content
|
||||
.map((part) => {
|
||||
if (!part || typeof part !== "object") return "";
|
||||
if (!part || typeof part !== "object") {
|
||||
return "";
|
||||
}
|
||||
const type = (part as { type?: unknown }).type;
|
||||
const text = (part as { text?: unknown }).text;
|
||||
const inputText = (part as { input_text?: unknown }).input_text;
|
||||
if (type === "text" && typeof text === "string") return text;
|
||||
if (type === "input_text" && typeof text === "string") return text;
|
||||
if (typeof inputText === "string") return inputText;
|
||||
if (type === "text" && typeof text === "string") {
|
||||
return text;
|
||||
}
|
||||
if (type === "input_text" && typeof text === "string") {
|
||||
return text;
|
||||
}
|
||||
if (typeof inputText === "string") {
|
||||
return inputText;
|
||||
}
|
||||
return "";
|
||||
})
|
||||
.filter(Boolean)
|
||||
@@ -75,10 +85,14 @@ function buildAgentPrompt(messagesUnknown: unknown): {
|
||||
[];
|
||||
|
||||
for (const msg of messages) {
|
||||
if (!msg || typeof msg !== "object") continue;
|
||||
if (!msg || typeof msg !== "object") {
|
||||
continue;
|
||||
}
|
||||
const role = typeof msg.role === "string" ? msg.role.trim() : "";
|
||||
const content = extractTextContent(msg.content).trim();
|
||||
if (!role || !content) continue;
|
||||
if (!role || !content) {
|
||||
continue;
|
||||
}
|
||||
if (role === "system" || role === "developer") {
|
||||
systemParts.push(content);
|
||||
continue;
|
||||
@@ -115,7 +129,9 @@ function buildAgentPrompt(messagesUnknown: unknown): {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (currentIndex < 0) currentIndex = conversationEntries.length - 1;
|
||||
if (currentIndex < 0) {
|
||||
currentIndex = conversationEntries.length - 1;
|
||||
}
|
||||
const currentEntry = conversationEntries[currentIndex]?.entry;
|
||||
if (currentEntry) {
|
||||
const historyEntries = conversationEntries.slice(0, currentIndex).map((entry) => entry.entry);
|
||||
@@ -147,7 +163,9 @@ function resolveOpenAiSessionKey(params: {
|
||||
}
|
||||
|
||||
function coerceRequest(val: unknown): OpenAiChatCompletionRequest {
|
||||
if (!val || typeof val !== "object") return {};
|
||||
if (!val || typeof val !== "object") {
|
||||
return {};
|
||||
}
|
||||
return val as OpenAiChatCompletionRequest;
|
||||
}
|
||||
|
||||
@@ -157,7 +175,9 @@ export async function handleOpenAiHttpRequest(
|
||||
opts: OpenAiHttpOptions,
|
||||
): Promise<boolean> {
|
||||
const url = new URL(req.url ?? "/", `http://${req.headers.host || "localhost"}`);
|
||||
if (url.pathname !== "/v1/chat/completions") return false;
|
||||
if (url.pathname !== "/v1/chat/completions") {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (req.method !== "POST") {
|
||||
sendMethodNotAllowed(res);
|
||||
@@ -177,7 +197,9 @@ export async function handleOpenAiHttpRequest(
|
||||
}
|
||||
|
||||
const body = await readJsonBodyOrError(req, res, opts.maxBodyBytes ?? 1024 * 1024);
|
||||
if (body === undefined) return true;
|
||||
if (body === undefined) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const payload = coerceRequest(body);
|
||||
const stream = Boolean(payload.stream);
|
||||
@@ -254,14 +276,20 @@ export async function handleOpenAiHttpRequest(
|
||||
let closed = false;
|
||||
|
||||
const unsubscribe = onAgentEvent((evt) => {
|
||||
if (evt.runId !== runId) return;
|
||||
if (closed) return;
|
||||
if (evt.runId !== runId) {
|
||||
return;
|
||||
}
|
||||
if (closed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (evt.stream === "assistant") {
|
||||
const delta = evt.data?.delta;
|
||||
const text = evt.data?.text;
|
||||
const content = typeof delta === "string" ? delta : typeof text === "string" ? text : "";
|
||||
if (!content) return;
|
||||
if (!content) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!wroteRole) {
|
||||
wroteRole = true;
|
||||
@@ -323,7 +351,9 @@ export async function handleOpenAiHttpRequest(
|
||||
deps,
|
||||
);
|
||||
|
||||
if (closed) return;
|
||||
if (closed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!sawAssistantDelta) {
|
||||
if (!wroteRole) {
|
||||
@@ -362,7 +392,9 @@ export async function handleOpenAiHttpRequest(
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
if (closed) return;
|
||||
if (closed) {
|
||||
return;
|
||||
}
|
||||
writeSse(res, {
|
||||
id: runId,
|
||||
object: "chat.completion.chunk",
|
||||
|
||||
Reference in New Issue
Block a user