(
url: string | (() => string),
options:
| FetchConnectionOptions
| (() => FetchConnectionOptions | Promise<FetchConnectionOptions>) = {},
)
| 488 | * ``` |
| 489 | */ |
| 490 | export function fetchServerSentEvents( |
| 491 | url: string | (() => string), |
| 492 | options: |
| 493 | | FetchConnectionOptions |
| 494 | | (() => FetchConnectionOptions | Promise<FetchConnectionOptions>) = {}, |
| 495 | ): ConnectConnectionAdapter { |
| 496 | return { |
| 497 | async *connect(messages, data, abortSignal, runContext) { |
| 498 | // Resolve URL and options if they are functions |
| 499 | const resolvedUrl = typeof url === 'function' ? url() : url |
| 500 | const resolvedOptions = |
| 501 | typeof options === 'function' ? await options() : options |
| 502 | |
| 503 | const requestHeaders: Record<string, string> = { |
| 504 | 'Content-Type': 'application/json', |
| 505 | ...mergeHeaders(resolvedOptions.headers), |
| 506 | } |
| 507 | |
| 508 | // Build AG-UI RunAgentInput payload. |
| 509 | // |
| 510 | // Precedence (later spreads win): static adapter `body` is the base, |
| 511 | // overridden by `runContext.forwardedProps` (constructor body / |
| 512 | // forwardedProps options), overridden by per-message `data` passed |
| 513 | // to `connection.send`. Runtime values win over static config — |
| 514 | // this matches the documented "forwardedProps wins" semantic. |
| 515 | const requestBody = buildRunAgentInputBody( |
| 516 | messages, |
| 517 | data, |
| 518 | runContext, |
| 519 | resolvedOptions, |
| 520 | ) |
| 521 | |
| 522 | const fetchClient = resolvedOptions.fetchClient ?? fetch |
| 523 | // `RequestInit.signal` is typed `AbortSignal | null` (no `undefined` |
| 524 | // under `exactOptionalPropertyTypes`), so spread it conditionally |
| 525 | // rather than passing `undefined` explicitly. |
| 526 | const signal = abortSignal || resolvedOptions.signal |
| 527 | const response = await fetchClient(resolvedUrl, { |
| 528 | method: 'POST', |
| 529 | headers: requestHeaders, |
| 530 | body: JSON.stringify(requestBody), |
| 531 | credentials: resolvedOptions.credentials || 'same-origin', |
| 532 | ...(signal ? { signal } : {}), |
| 533 | }) |
| 534 | |
| 535 | yield* responseToSSEChunks(response, abortSignal) |
| 536 | }, |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Create an HTTP streaming connection adapter (for raw streaming without SSE format) |
no outgoing calls
no test coverage detected