mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-28 17:01:53 +03:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
+9
-3
@@ -27,7 +27,9 @@ export type AcpClientHandle = {
|
||||
};
|
||||
|
||||
function toArgs(value: string[] | string | undefined): string[] {
|
||||
if (!value) return [];
|
||||
if (!value) {
|
||||
return [];
|
||||
}
|
||||
return Array.isArray(value) ? value : [value];
|
||||
}
|
||||
|
||||
@@ -41,7 +43,9 @@ function buildServerArgs(opts: AcpClientOptions): string[] {
|
||||
|
||||
function printSessionUpdate(notification: SessionNotification): void {
|
||||
const update = notification.update;
|
||||
if (!("sessionUpdate" in update)) return;
|
||||
if (!("sessionUpdate" in update)) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (update.sessionUpdate) {
|
||||
case "agent_message_chunk": {
|
||||
@@ -62,7 +66,9 @@ function printSessionUpdate(notification: SessionNotification): void {
|
||||
}
|
||||
case "available_commands_update": {
|
||||
const names = update.availableCommands?.map((cmd) => `/${cmd.name}`).join(" ");
|
||||
if (names) console.log(`\n[commands] ${names}`);
|
||||
if (names) {
|
||||
console.log(`\n[commands] ${names}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
default:
|
||||
|
||||
+33
-11
@@ -15,7 +15,9 @@ export function extractTextFromPrompt(prompt: ContentBlock[]): string {
|
||||
}
|
||||
if (block.type === "resource") {
|
||||
const resource = block.resource as { text?: string } | undefined;
|
||||
if (resource?.text) parts.push(resource.text);
|
||||
if (resource?.text) {
|
||||
parts.push(resource.text);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (block.type === "resource_link") {
|
||||
@@ -31,9 +33,13 @@ export function extractTextFromPrompt(prompt: ContentBlock[]): string {
|
||||
export function extractAttachmentsFromPrompt(prompt: ContentBlock[]): GatewayAttachment[] {
|
||||
const attachments: GatewayAttachment[] = [];
|
||||
for (const block of prompt) {
|
||||
if (block.type !== "image") continue;
|
||||
if (block.type !== "image") {
|
||||
continue;
|
||||
}
|
||||
const image = block as ImageContent;
|
||||
if (!image.data || !image.mimeType) continue;
|
||||
if (!image.data || !image.mimeType) {
|
||||
continue;
|
||||
}
|
||||
attachments.push({
|
||||
type: "image",
|
||||
mimeType: image.mimeType,
|
||||
@@ -48,7 +54,9 @@ export function formatToolTitle(
|
||||
args: Record<string, unknown> | undefined,
|
||||
): string {
|
||||
const base = name ?? "tool";
|
||||
if (!args || Object.keys(args).length === 0) return base;
|
||||
if (!args || Object.keys(args).length === 0) {
|
||||
return base;
|
||||
}
|
||||
const parts = Object.entries(args).map(([key, value]) => {
|
||||
const raw = typeof value === "string" ? value : JSON.stringify(value);
|
||||
const safe = raw.length > 100 ? `${raw.slice(0, 100)}...` : raw;
|
||||
@@ -58,16 +66,30 @@ export function formatToolTitle(
|
||||
}
|
||||
|
||||
export function inferToolKind(name?: string): ToolKind {
|
||||
if (!name) return "other";
|
||||
if (!name) {
|
||||
return "other";
|
||||
}
|
||||
const normalized = name.toLowerCase();
|
||||
if (normalized.includes("read")) return "read";
|
||||
if (normalized.includes("write") || normalized.includes("edit")) return "edit";
|
||||
if (normalized.includes("delete") || normalized.includes("remove")) return "delete";
|
||||
if (normalized.includes("move") || normalized.includes("rename")) return "move";
|
||||
if (normalized.includes("search") || normalized.includes("find")) return "search";
|
||||
if (normalized.includes("read")) {
|
||||
return "read";
|
||||
}
|
||||
if (normalized.includes("write") || normalized.includes("edit")) {
|
||||
return "edit";
|
||||
}
|
||||
if (normalized.includes("delete") || normalized.includes("remove")) {
|
||||
return "delete";
|
||||
}
|
||||
if (normalized.includes("move") || normalized.includes("rename")) {
|
||||
return "move";
|
||||
}
|
||||
if (normalized.includes("search") || normalized.includes("find")) {
|
||||
return "search";
|
||||
}
|
||||
if (normalized.includes("exec") || normalized.includes("run") || normalized.includes("bash")) {
|
||||
return "execute";
|
||||
}
|
||||
if (normalized.includes("fetch") || normalized.includes("http")) return "fetch";
|
||||
if (normalized.includes("fetch") || normalized.includes("http")) {
|
||||
return "fetch";
|
||||
}
|
||||
return "other";
|
||||
}
|
||||
|
||||
+18
-6
@@ -2,10 +2,14 @@ export function readString(
|
||||
meta: Record<string, unknown> | null | undefined,
|
||||
keys: string[],
|
||||
): string | undefined {
|
||||
if (!meta) return undefined;
|
||||
if (!meta) {
|
||||
return undefined;
|
||||
}
|
||||
for (const key of keys) {
|
||||
const value = meta[key];
|
||||
if (typeof value === "string" && value.trim()) return value.trim();
|
||||
if (typeof value === "string" && value.trim()) {
|
||||
return value.trim();
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
@@ -14,10 +18,14 @@ export function readBool(
|
||||
meta: Record<string, unknown> | null | undefined,
|
||||
keys: string[],
|
||||
): boolean | undefined {
|
||||
if (!meta) return undefined;
|
||||
if (!meta) {
|
||||
return undefined;
|
||||
}
|
||||
for (const key of keys) {
|
||||
const value = meta[key];
|
||||
if (typeof value === "boolean") return value;
|
||||
if (typeof value === "boolean") {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
@@ -26,10 +34,14 @@ export function readNumber(
|
||||
meta: Record<string, unknown> | null | undefined,
|
||||
keys: string[],
|
||||
): number | undefined {
|
||||
if (!meta) return undefined;
|
||||
if (!meta) {
|
||||
return undefined;
|
||||
}
|
||||
for (const key of keys) {
|
||||
const value = meta[key];
|
||||
if (typeof value === "number" && Number.isFinite(value)) return value;
|
||||
if (typeof value === "number" && Number.isFinite(value)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,9 @@ export type AcpSessionMeta = {
|
||||
};
|
||||
|
||||
export function parseSessionMeta(meta: unknown): AcpSessionMeta {
|
||||
if (!meta || typeof meta !== "object") return {};
|
||||
if (!meta || typeof meta !== "object") {
|
||||
return {};
|
||||
}
|
||||
const record = meta as Record<string, unknown>;
|
||||
return {
|
||||
sessionKey: readString(record, ["sessionKey", "session", "key"]),
|
||||
@@ -45,7 +47,9 @@ export async function resolveSessionKey(params: {
|
||||
}
|
||||
|
||||
if (params.meta.sessionKey) {
|
||||
if (!requireExisting) return params.meta.sessionKey;
|
||||
if (!requireExisting) {
|
||||
return params.meta.sessionKey;
|
||||
}
|
||||
const resolved = await params.gateway.request<{ ok: true; key: string }>("sessions.resolve", {
|
||||
key: params.meta.sessionKey,
|
||||
});
|
||||
@@ -66,7 +70,9 @@ export async function resolveSessionKey(params: {
|
||||
}
|
||||
|
||||
if (requestedKey) {
|
||||
if (!requireExisting) return requestedKey;
|
||||
if (!requireExisting) {
|
||||
return requestedKey;
|
||||
}
|
||||
const resolved = await params.gateway.request<{ ok: true; key: string }>("sessions.resolve", {
|
||||
key: requestedKey,
|
||||
});
|
||||
@@ -86,6 +92,8 @@ export async function resetSessionIfNeeded(params: {
|
||||
opts: AcpServerOptions;
|
||||
}): Promise<void> {
|
||||
const resetSession = params.meta.resetSession ?? params.opts.resetSession ?? false;
|
||||
if (!resetSession) return;
|
||||
if (!resetSession) {
|
||||
return;
|
||||
}
|
||||
await params.gateway.request("sessions.reset", { key: params.sessionKey });
|
||||
}
|
||||
|
||||
+15
-5
@@ -39,7 +39,9 @@ export function createInMemorySessionStore(): AcpSessionStore {
|
||||
|
||||
const setActiveRun: AcpSessionStore["setActiveRun"] = (sessionId, runId, abortController) => {
|
||||
const session = sessions.get(sessionId);
|
||||
if (!session) return;
|
||||
if (!session) {
|
||||
return;
|
||||
}
|
||||
session.activeRunId = runId;
|
||||
session.abortController = abortController;
|
||||
runIdToSessionId.set(runId, sessionId);
|
||||
@@ -47,17 +49,25 @@ export function createInMemorySessionStore(): AcpSessionStore {
|
||||
|
||||
const clearActiveRun: AcpSessionStore["clearActiveRun"] = (sessionId) => {
|
||||
const session = sessions.get(sessionId);
|
||||
if (!session) return;
|
||||
if (session.activeRunId) runIdToSessionId.delete(session.activeRunId);
|
||||
if (!session) {
|
||||
return;
|
||||
}
|
||||
if (session.activeRunId) {
|
||||
runIdToSessionId.delete(session.activeRunId);
|
||||
}
|
||||
session.activeRunId = null;
|
||||
session.abortController = null;
|
||||
};
|
||||
|
||||
const cancelActiveRun: AcpSessionStore["cancelActiveRun"] = (sessionId) => {
|
||||
const session = sessions.get(sessionId);
|
||||
if (!session?.abortController) return false;
|
||||
if (!session?.abortController) {
|
||||
return false;
|
||||
}
|
||||
session.abortController.abort();
|
||||
if (session.activeRunId) runIdToSessionId.delete(session.activeRunId);
|
||||
if (session.activeRunId) {
|
||||
runIdToSessionId.delete(session.activeRunId);
|
||||
}
|
||||
session.abortController = null;
|
||||
session.activeRunId = null;
|
||||
return true;
|
||||
|
||||
+48
-16
@@ -210,7 +210,9 @@ export class AcpGatewayAgent implements Agent {
|
||||
if (!session) {
|
||||
throw new Error(`Session ${params.sessionId} not found`);
|
||||
}
|
||||
if (!params.modeId) return {};
|
||||
if (!params.modeId) {
|
||||
return {};
|
||||
}
|
||||
try {
|
||||
await this.gateway.request("sessions.patch", {
|
||||
key: session.sessionKey,
|
||||
@@ -276,7 +278,9 @@ export class AcpGatewayAgent implements Agent {
|
||||
|
||||
async cancel(params: CancelNotification): Promise<void> {
|
||||
const session = this.sessionStore.getSession(params.sessionId);
|
||||
if (!session) return;
|
||||
if (!session) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.sessionStore.cancelActiveRun(params.sessionId);
|
||||
try {
|
||||
@@ -294,24 +298,38 @@ export class AcpGatewayAgent implements Agent {
|
||||
|
||||
private async handleAgentEvent(evt: EventFrame): Promise<void> {
|
||||
const payload = evt.payload as Record<string, unknown> | undefined;
|
||||
if (!payload) return;
|
||||
if (!payload) {
|
||||
return;
|
||||
}
|
||||
const stream = payload.stream as string | undefined;
|
||||
const data = payload.data as Record<string, unknown> | undefined;
|
||||
const sessionKey = payload.sessionKey as string | undefined;
|
||||
if (!stream || !data || !sessionKey) return;
|
||||
if (!stream || !data || !sessionKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (stream !== "tool") return;
|
||||
if (stream !== "tool") {
|
||||
return;
|
||||
}
|
||||
const phase = data.phase as string | undefined;
|
||||
const name = data.name as string | undefined;
|
||||
const toolCallId = data.toolCallId as string | undefined;
|
||||
if (!toolCallId) return;
|
||||
if (!toolCallId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const pending = this.findPendingBySessionKey(sessionKey);
|
||||
if (!pending) return;
|
||||
if (!pending) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (phase === "start") {
|
||||
if (!pending.toolCalls) pending.toolCalls = new Set();
|
||||
if (pending.toolCalls.has(toolCallId)) return;
|
||||
if (!pending.toolCalls) {
|
||||
pending.toolCalls = new Set();
|
||||
}
|
||||
if (pending.toolCalls.has(toolCallId)) {
|
||||
return;
|
||||
}
|
||||
pending.toolCalls.add(toolCallId);
|
||||
const args = data.args as Record<string, unknown> | undefined;
|
||||
await this.connection.sessionUpdate({
|
||||
@@ -344,17 +362,25 @@ export class AcpGatewayAgent implements Agent {
|
||||
|
||||
private async handleChatEvent(evt: EventFrame): Promise<void> {
|
||||
const payload = evt.payload as Record<string, unknown> | undefined;
|
||||
if (!payload) return;
|
||||
if (!payload) {
|
||||
return;
|
||||
}
|
||||
|
||||
const sessionKey = payload.sessionKey as string | undefined;
|
||||
const state = payload.state as string | undefined;
|
||||
const runId = payload.runId as string | undefined;
|
||||
const messageData = payload.message as Record<string, unknown> | undefined;
|
||||
if (!sessionKey || !state) return;
|
||||
if (!sessionKey || !state) {
|
||||
return;
|
||||
}
|
||||
|
||||
const pending = this.findPendingBySessionKey(sessionKey);
|
||||
if (!pending) return;
|
||||
if (runId && pending.idempotencyKey !== runId) return;
|
||||
if (!pending) {
|
||||
return;
|
||||
}
|
||||
if (runId && pending.idempotencyKey !== runId) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (state === "delta" && messageData) {
|
||||
await this.handleDeltaEvent(pending.sessionId, messageData);
|
||||
@@ -381,10 +407,14 @@ export class AcpGatewayAgent implements Agent {
|
||||
const content = messageData.content as Array<{ type: string; text?: string }> | undefined;
|
||||
const fullText = content?.find((c) => c.type === "text")?.text ?? "";
|
||||
const pending = this.pendingPrompts.get(sessionId);
|
||||
if (!pending) return;
|
||||
if (!pending) {
|
||||
return;
|
||||
}
|
||||
|
||||
const sentSoFar = pending.sentTextLength ?? 0;
|
||||
if (fullText.length <= sentSoFar) return;
|
||||
if (fullText.length <= sentSoFar) {
|
||||
return;
|
||||
}
|
||||
|
||||
const newText = fullText.slice(sentSoFar);
|
||||
pending.sentTextLength = fullText.length;
|
||||
@@ -407,7 +437,9 @@ export class AcpGatewayAgent implements Agent {
|
||||
|
||||
private findPendingBySessionKey(sessionKey: string): PendingPrompt | undefined {
|
||||
for (const pending of this.pendingPrompts.values()) {
|
||||
if (pending.sessionKey === sessionKey) return pending;
|
||||
if (pending.sessionKey === sessionKey) {
|
||||
return pending;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user