diff --git a/packages/core/clients/fallbackKernelAccountClient.ts b/packages/core/clients/fallbackKernelAccountClient.ts index c947c4e0..d6021c27 100644 --- a/packages/core/clients/fallbackKernelAccountClient.ts +++ b/packages/core/clients/fallbackKernelAccountClient.ts @@ -15,32 +15,46 @@ export const createFallbackKernelAccountClient = < ): KernelAccountClient => { const proxyClient = new Proxy(clients[0], { get(_target, prop, receiver) { - // biome-ignore lint/suspicious/noExplicitAny: - return async (...args: any[]) => { - for (let i = 0; i < clients.length; i++) { - try { - const value = Reflect.get(clients[i], prop, receiver) - if (typeof value === "function") { - return await value.apply(clients[i], args) - } else { - return value - } - } catch (error) { - console.error( - `Action ${String(prop)} failed with client ${ - clients[i].transport.url - }, trying next if available.`, - error - ) - // If all clients fail, throw the last encountered error - if (i === clients.length - 1) { - throw error + for (const client of clients) { + const value = Reflect.get(client, prop, receiver) + if (value !== undefined) { + // If the property is a function, wrap it to add fallback logic + if (typeof value === "function") { + // biome-ignore lint/suspicious/noExplicitAny: + return async (...args: any[]) => { + for (let i = 0; i < clients.length; i++) { + try { + const method = Reflect.get( + clients[i], + prop, + receiver + ) + if (typeof method === "function") { + // Attempt to call the function on the current client + return await method(...args) + } + } catch (error) { + console.error( + `Action ${String( + prop + )} failed with client ${ + client.transport.url + }, trying next if available.`, + error + ) + if (i === clients.length - 1) { + throw error + } + } + } } } + // For non-function properties, return the first defined value found + return value } - // If no clients have the property, return undefined - return undefined } + // If no clients have a defined value for the property, return undefined + return undefined } })