Skip to content

Commit 7e1104e

Browse files
committed
fix(a2a): handle Request objects in pinnedFetch URL extraction
1 parent ba4738b commit 7e1104e

1 file changed

Lines changed: 38 additions & 22 deletions

File tree

apps/sim/lib/a2a/utils.ts

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,48 @@ export async function createA2AClient(agentUrl: string, apiKey?: string): Promis
6868

6969
const resolvedIP = validation.resolvedIP!
7070

71-
const pinnedFetch = (
71+
const pinnedFetch = async (
7272
input: Parameters<typeof fetch>[0],
7373
init?: Parameters<typeof fetch>[1]
74-
): Promise<Response> =>
75-
secureFetchWithPinnedIP(input.toString(), resolvedIP, {
76-
method: init?.method,
77-
headers:
78-
init?.headers instanceof Headers
79-
? Object.fromEntries(init.headers.entries())
80-
: (init?.headers as Record<string, string> | undefined),
81-
body:
82-
typeof init?.body === 'string' || Buffer.isBuffer(init?.body)
83-
? (init?.body as string | Buffer)
84-
: init?.body instanceof Uint8Array
74+
): Promise<Response> => {
75+
const url = input instanceof Request ? input.url : input.toString()
76+
const method = init?.method ?? (input instanceof Request ? input.method : undefined)
77+
78+
const rawHeaders = init?.headers ?? (input instanceof Request ? input.headers : undefined)
79+
const headers =
80+
rawHeaders instanceof Headers
81+
? Object.fromEntries(rawHeaders.entries())
82+
: (rawHeaders as Record<string, string> | undefined)
83+
84+
let body: string | Buffer | Uint8Array | undefined
85+
if (init?.body !== undefined && init.body !== null) {
86+
body =
87+
typeof init.body === 'string' || Buffer.isBuffer(init.body)
88+
? (init.body as string | Buffer)
89+
: init.body instanceof Uint8Array
8590
? (init.body as Uint8Array)
86-
: undefined,
87-
signal: init?.signal instanceof AbortSignal ? init.signal : undefined,
88-
}).then(async (res) => {
89-
const headers = new Headers(res.headers.toRecord())
90-
const body = await res.text()
91-
return new Response(body, {
92-
status: res.status,
93-
statusText: res.statusText,
94-
headers,
95-
})
91+
: undefined
92+
} else if (input instanceof Request && !input.bodyUsed) {
93+
const text = await input.text()
94+
if (text) body = text
95+
}
96+
97+
const signal =
98+
init?.signal instanceof AbortSignal
99+
? init.signal
100+
: input instanceof Request && input.signal instanceof AbortSignal
101+
? input.signal
102+
: undefined
103+
104+
const res = await secureFetchWithPinnedIP(url, resolvedIP, { method, headers, body, signal })
105+
const resHeaders = new Headers(res.headers.toRecord())
106+
const resBody = await res.text()
107+
return new Response(resBody, {
108+
status: res.status,
109+
statusText: res.statusText,
110+
headers: resHeaders,
96111
})
112+
}
97113

98114
const pinnedTransports = [
99115
new JsonRpcTransportFactory({ fetchImpl: pinnedFetch }),

0 commit comments

Comments
 (0)