diff --git a/apps/sim/tools/cloudflare/get_tunnel.ts b/apps/sim/tools/cloudflare/get_tunnel.ts index cf0faf957f0..4ab3386064e 100644 --- a/apps/sim/tools/cloudflare/get_tunnel.ts +++ b/apps/sim/tools/cloudflare/get_tunnel.ts @@ -6,7 +6,7 @@ export const getTunnelTool: ToolConfig cloudflareHeaders(params.apiKey), }, - transformResponse: async (response: Response) => { + transformResponse: async ( + response: Response, + params?: CloudflareGetTunnelParams, + context?: { signal?: AbortSignal } + ) => { const data = await response.json() + const emptyOutput = { + id: '', + name: null, + account_tag: null, + config_src: null, + status: null, + tun_type: null, + remote_config: null, + metadata: null, + created_at: null, + deleted_at: null, + conns_active_at: null, + conns_inactive_at: null, + connections: null, + } + if (!data.success) { return { success: false, - output: { - id: '', - name: null, - account_tag: null, - config_src: null, - status: null, - tun_type: null, - remote_config: null, - metadata: null, - created_at: null, - deleted_at: null, - conns_active_at: null, - conns_inactive_at: null, - connections: null, - }, + output: emptyOutput, error: cloudflareErrorMessage(data, 'Failed to get tunnel'), } } const tunnel = data.result + let connections: unknown[] | null = null + if (params?.accountId && params?.tunnelId && params?.apiKey) { + try { + const connectionsUrl = `https://api.cloudflare.com/client/v4/accounts/${params.accountId.trim()}/cfd_tunnel/${params.tunnelId.trim()}/connections` + // boundary-raw-fetch: requests the external Cloudflare connections endpoint + const connectionsRes = await fetch(connectionsUrl, { + method: 'GET', + headers: cloudflareHeaders(params.apiKey), + signal: context?.signal, + }) + const connectionsData = await connectionsRes.json() + if (!connectionsRes.ok || !connectionsData?.success) { + return { + success: false, + output: emptyOutput, + error: cloudflareErrorMessage(connectionsData, 'Failed to get tunnel connections'), + } + } + connections = Array.isArray(connectionsData.result) + ? connectionsData.result.flatMap( + (connector: { conns?: unknown[] }) => connector.conns ?? [] + ) + : [] + } catch (error) { + if (context?.signal?.aborted || (error as Error)?.name === 'AbortError') throw error + return { + success: false, + output: emptyOutput, + error: 'Failed to get tunnel connections', + } + } + } + return { success: true, output: { @@ -78,7 +117,7 @@ export const getTunnelTool: ToolConfig