ag-ui-protocol / ag-ui-protocol/ag-ui

HttpAgent forces new instance per run when using dynamic headers (e.g. auth token) Description

Aperta
#1,113 1 commento 2 reazioni 0 assegnatari Vedi su GitHub
enhancement SDK
Lingua principale
Python
Stelle
15.9k
Fork
1.4k
Merge medio
1g 17h
PR unite (30g)
163

Descrizione

# HttpAgent requires new instance per run when using dynamic headers (e.g. auth token)

## Summary

When integrating `@ag-ui/client`'s `HttpAgent` in a React app, we need different headers on each run (e.g. fresh auth token, or per-request headers from the UI). The current API only allows setting `url` and `headers` at construction time, and `run()` accepts only `RunAgentInput`. So we have to instantiate a new `HttpAgent` on every run, which feels wrong and prevents reusing a single agent instance.

## Current behaviour

We need to do something like this on **every** run:

```ts
// Inside each run():
const token = await getAuthToken();
const headers = { ...defaultHeaders, ...options?.headers, Authorization: `Bearer ${token}` };
const agent = new HttpAgent({ url, headers });
const events$ = agent.run(params);
```

So we **create a new `HttpAgent` every time**. That feels wrong because the agent is effectively stateless for our use case; we only need different headers per request. We’d prefer to construct one agent (e.g. in a provider) and reuse it, and only vary headers per run.

## Root cause

- `HttpAgent` only accepts `url` and `headers` in the **constructor**.
- `run(input: RunAgentInput)` only takes `RunAgentInput`; there is no way to pass request-specific headers (or a request options object) into `run()`.
- So the only way to get different headers per run is to construct a new `HttpAgent` each time.

## Proposed improvement

A way to use **one** `HttpAgent` instance with **per-run (or per-request) headers**. For example:

**Option A — extend `run()`:**
`run(input: RunAgentInput, options?: { headers?: Record })`
and have the agent merge those with constructor headers when building the request (e.g. in `requestInit()`).

**Option B:**
Another supported pattern (e.g. a factory that takes headers per run) that doesn’t require instantiating `HttpAgent` on every run.

That would let us construct the agent once and reuse it while still sending a fresh auth token and per-run headers on each run.

## Environment

- **Package:** `@ag-ui/client` (e.g. 0.0.44)
- **Use case:** Browser/React app calling an AGUI backend with Bearer token and optional per-request headers.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.