Skip to content
Source
Sign inSign up
TanStack/ai

feat: dual ESM+CJS builds + toJSONResponse/fetchJSON for non-streaming runtimes #478

ClosedAlemTuzlakwants to merge 12 commits intomain fromfix/cjs-output-and-json-responseopened View on GitHub

AlemTuzlakcommented

Summary

Two related fixes for Expo / Metro / non-streaming runtimes.

#308 — dual ESM + CJS output

`@tanstack/ai`, `@tanstack/ai-client`, and `@tanstack/ai-event-client` were ESM-only (`import` condition only, no `require` / `default`). Metro can't resolve that configuration, even with `unstable_enablePackageExports: true`, so consumers saw `Cannot resolve @tanstack/ai/adapters` etc.

Changes:

  • `vite.config.ts` for all three packages flipped to `cjs: true` — emits `dist/esm//*.js` + `.d.ts` and `dist/cjs//*.cjs` + `.d.cts`.
  • `package.json` `exports` for `.`, `./adapters`, `./middlewares` now use the nested `import`/`require` shape with type-aware conditions.
  • Added `main` pointing at the CJS entry so legacy resolvers still work.
  • `publint --strict` passes.

#309 — `toJSONResponse` + `fetchJSON`

Expo's `@expo/server` can't emit `ReadableStream` responses, so `toServerSentEventsResponse` / `toHttpResponse` crash with `Cannot read properties of undefined (reading 'statusText')`.

Changes:

  • `@tanstack/ai`: `toJSONResponse(stream, init?)` — drains the stream, returns `new Response(JSON.stringify(chunks), { Content-Type: application/json })`. Honours caller-provided headers / status; aborts a supplied `abortController` if the upstream throws.
  • `@tanstack/ai-client`: `fetchJSON(url, options?)` — matching connection adapter. POSTs `{ messages, data }`, parses the response as a `StreamChunk[]`, yields each chunk into the normal `ChatClient` pipeline.

Trade-off: you lose incremental rendering — the UI sees everything at once when the request resolves. Docs in both JSDoc blocks call this out and tell users to prefer SSE / HTTP-stream when the runtime supports them.

Test plan

  • Unit tests for `toJSONResponse`: defaults, header passthrough, custom `Content-Type`, abort-on-upstream-error
  • `pnpm test` across all 41 projects
  • `test:build` (publint strict) green on all three packages — dual exports map validated

Summary by CodeRabbit

  • New Features

    • Dual ESM + CommonJS distributions for packages to improve CJS compatibility
    • Added toJSONResponse — server helper returning a chat stream as a single JSON-array Response
    • Added fetchJSON — client adapter that fetches JSON-array chat responses and replays them into the chat pipeline
  • Documentation

    • Guides for non-streaming runtimes (React Native / Expo) and JSON-adapter usage
  • Tests

    • Tests covering fetchJSON behavior, error cases, headers, and abort handling
    1. github-actions[bot](GitHub profile, opens in a new tab)commented

      🚀 Changeset Version Preview

      10 package(s) bumped directly, 23 bumped as dependents.

      🟥 Major bumps

      PackageVersionReason
      @tanstack/ai-anthropic0.8.3 → 1.0.0Changeset
      @tanstack/ai-event-client0.2.8 → 1.0.0Changeset
      @tanstack/ai-preact0.6.20 → 1.0.0Changeset
      @tanstack/ai-react0.8.0 → 1.0.0Changeset
      @tanstack/ai-solid0.7.0 → 1.0.0Changeset
      @tanstack/ai-svelte0.7.0 → 1.0.0Changeset
      @tanstack/ai-vue0.7.0 → 1.0.0Changeset
      @tanstack/ai-code-mode0.1.8 → 1.0.0Dependent
      @tanstack/ai-code-mode-skills0.1.8 → 1.0.0Dependent
      @tanstack/ai-elevenlabs0.2.0 → 1.0.0Dependent
      @tanstack/ai-fal0.7.0 → 1.0.0Dependent
      @tanstack/ai-gemini0.10.0 → 1.0.0Dependent
      @tanstack/ai-grok0.7.0 → 1.0.0Dependent
      @tanstack/ai-groq0.1.8 → 1.0.0Dependent
      @tanstack/ai-isolate-node0.1.8 → 1.0.0Dependent
      @tanstack/ai-isolate-quickjs0.1.8 → 1.0.0Dependent
      @tanstack/ai-ollama0.6.10 → 1.0.0Dependent
      @tanstack/ai-openai0.8.2 → 1.0.0Dependent
      @tanstack/ai-openrouter0.8.2 → 1.0.0Dependent
      @tanstack/ai-react-ui0.6.2 → 1.0.0Dependent
      @tanstack/ai-solid-ui0.6.2 → 1.0.0Dependent

      🟨 Minor bumps

      PackageVersionReason
      @tanstack/ai0.14.0 → 0.15.0Changeset
      @tanstack/ai-client0.8.0 → 0.9.0Changeset
      @tanstack/ai-isolate-cloudflare0.1.8 → 0.2.0Changeset

      🟩 Patch bumps

      PackageVersionReason
      @tanstack/ai-code-mode-models-eval0.0.12 → 0.0.13Dependent
      @tanstack/ai-devtools-core0.3.25 → 0.3.26Dependent
      @tanstack/ai-vue-ui0.1.31 → 0.1.32Dependent
      @tanstack/preact-ai-devtools0.1.29 → 0.1.30Dependent
      @tanstack/react-ai-devtools0.2.29 → 0.2.30Dependent
      @tanstack/solid-ai-devtools0.2.29 → 0.2.30Dependent
      ts-svelte-chat0.1.38 → 0.1.39Dependent
      ts-vue-chat0.1.38 → 0.1.39Dependent
      vanilla-chat0.0.35 → 0.0.36Dependent
    2. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 1a26a98e356792078bfe352c0d09d90bff7bad88

      CommandStatusDurationResult
      nx run-many --targets=build --exclude=examples/**✅ Succeeded1sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-05-08 10:12:46 UTC

    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Note

      Reviews paused

      It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

      Use the following commands to manage reviews:

      • @coderabbitai resume to resume automatic reviews.
      • @coderabbitai review to trigger a single review.

      Use the checkboxes below for quick actions:

      • ▶️ Resume reviews
      • 🔍 Trigger review
      📝 Walkthrough

      Walkthrough

      Adds dual ESM/CJS package entrypoints for several TanStack AI packages and introduces server-side toJSONResponse(stream, init?) (drains a stream → single JSON Response) and client-side fetchJSON(url, options?) (POST → JSON array → replayed StreamChunks) with accompanying tests and docs.

      Changes

      Single Cohort: Dual-package outputs + non-streaming fallback

      Layer / File(s)Summary
      Package metadata / Exports
      packages/typescript/ai/package.json, packages/typescript/ai-client/package.json, packages/typescript/ai-event-client/package.json
      Add top-level "main": "./dist/cjs/index.cjs" and change exports to conditional "import" (ESM) / "require" (CJS) targets with corresponding .d.ts / .d.cts typings.
      Build config
      packages/typescript/ai/vite.config.ts, packages/typescript/ai-client/vite.config.ts, packages/typescript/ai-event-client/vite.config.ts
      Enable CJS output by setting cjs: true in the tanstack/Vite build config.
      Core server helper
      packages/typescript/ai/src/stream-to-response.ts, packages/typescript/ai/src/index.ts
      Add toJSONResponse(stream, init?): drains an AsyncIterable into an in-memory array, returns a single Response with JSON body, handles aborts/errors, preserves init (sets Content-Type: application/json when absent), and re-export it at package top level.
      Client adapter
      packages/typescript/ai-client/src/connection-adapters.ts, packages/typescript/ai-client/src/index.ts
      Add fetchJSON(url, options?) adapter: POSTs { messages, data }, parses full JSON body expected as StreamChunk[], validates array shape, yields each element as StreamChunk via async generator; re-export at package top level.
      Tests
      packages/typescript/ai/tests/stream-to-response.test.ts, packages/typescript/ai-client/tests/connection-adapters.test.ts
      New tests for toJSONResponse (draining, headers, abort/error semantics) and fetchJSON (successful drain, non-2xx handling, non-array body error, fn/async resolution of url/options, body merging, custom fetch client, signal forwarding).
      Docs & Nav
      docs/api/ai-client.md, docs/api/ai.md, docs/chat/connection-adapters.md, docs/chat/non-streaming-runtimes.md, docs/chat/streaming.md, docs/config.json
      Document fetchJSON and toJSONResponse, add non-streaming runtimes guide and navigation entry, add examples and runtime guidance (non-incremental rendering trade-off).
      Changeset
      .changeset/cjs-output-and-json-response.md
      Release note summarizing packaging changes and new toJSONResponse() / fetchJSON() capabilities.

      Sequence Diagram(s)

      sequenceDiagram
        participant Client as Client
        participant ChatClient as ChatClient
        participant Server as Server
        participant StreamProducer as StreamProducer
      
        Client->>ChatClient: call fetchJSON(url, options)
        ChatClient->>Server: POST { messages, data }
        Server->>StreamProducer: start async chat stream
        StreamProducer-->>Server: yield StreamChunk...
        Server->>Server: toJSONResponse(stream) drains all chunks -> JSON array
        Server-->>ChatClient: HTTP 200 body: [StreamChunk, ...]
        ChatClient->>ChatClient: parse array, replay chunks into pipeline
        ChatClient->>Client: deliver reconstructed events (non-incremental)

      Estimated code review effort

      🎯 3 (Moderate) | ⏱️ ~25 minutes

      Poem

      🐰 I nibbled bytes and gathered the stream, Piled every chunk into one bright dream. CJS and ESM now hop side by side, JSON returns where streams can't ride. Fetch, replay — a tidy, cozy stride.

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title accurately summarizes the two main changes: dual ESM+CJS builds and new toJSONResponse/fetchJSON utilities for non-streaming runtimes.
      Description check✅ PassedThe PR description comprehensively covers the changes, motivation, and test plan. However, the author did not fill in the template's required checklist items and release impact section.
      Docstring Coverage✅ PassedDocstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      ✏️ Tip: You can configure your own custom pre-merge checks in the settings.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests
      • Commit unit tests in branch `fix/cjs-output-and-json-response`

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands and usage tips.
    4. pkg-pr-new[bot](GitHub profile, opens in a new tab)commented

      Open in StackBlitz

      @tanstack/ai

      npm i https://pkg.pr.new/@tanstack/ai@478

      @tanstack/ai-anthropic

      npm i https://pkg.pr.new/@tanstack/ai-anthropic@478

      @tanstack/ai-client

      npm i https://pkg.pr.new/@tanstack/ai-client@478

      @tanstack/ai-code-mode

      npm i https://pkg.pr.new/@tanstack/ai-code-mode@478

      @tanstack/ai-code-mode-skills

      npm i https://pkg.pr.new/@tanstack/ai-code-mode-skills@478

      @tanstack/ai-devtools-core

      npm i https://pkg.pr.new/@tanstack/ai-devtools-core@478

      @tanstack/ai-elevenlabs

      npm i https://pkg.pr.new/@tanstack/ai-elevenlabs@478

      @tanstack/ai-event-client

      npm i https://pkg.pr.new/@tanstack/ai-event-client@478

      @tanstack/ai-fal

      npm i https://pkg.pr.new/@tanstack/ai-fal@478

      @tanstack/ai-gemini

      npm i https://pkg.pr.new/@tanstack/ai-gemini@478

      @tanstack/ai-grok

      npm i https://pkg.pr.new/@tanstack/ai-grok@478

      @tanstack/ai-groq

      npm i https://pkg.pr.new/@tanstack/ai-groq@478

      @tanstack/ai-isolate-cloudflare

      npm i https://pkg.pr.new/@tanstack/ai-isolate-cloudflare@478

      @tanstack/ai-isolate-node

      npm i https://pkg.pr.new/@tanstack/ai-isolate-node@478

      @tanstack/ai-isolate-quickjs

      npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs@478

      @tanstack/ai-ollama

      npm i https://pkg.pr.new/@tanstack/ai-ollama@478

      @tanstack/ai-openai

      npm i https://pkg.pr.new/@tanstack/ai-openai@478

      @tanstack/ai-openrouter

      npm i https://pkg.pr.new/@tanstack/ai-openrouter@478

      @tanstack/ai-preact

      npm i https://pkg.pr.new/@tanstack/ai-preact@478

      @tanstack/ai-react

      npm i https://pkg.pr.new/@tanstack/ai-react@478

      @tanstack/ai-react-ui

      npm i https://pkg.pr.new/@tanstack/ai-react-ui@478

      @tanstack/ai-solid

      npm i https://pkg.pr.new/@tanstack/ai-solid@478

      @tanstack/ai-solid-ui

      npm i https://pkg.pr.new/@tanstack/ai-solid-ui@478

      @tanstack/ai-svelte

      npm i https://pkg.pr.new/@tanstack/ai-svelte@478

      @tanstack/ai-vue

      npm i https://pkg.pr.new/@tanstack/ai-vue@478

      @tanstack/ai-vue-ui

      npm i https://pkg.pr.new/@tanstack/ai-vue-ui@478

      @tanstack/preact-ai-devtools

      npm i https://pkg.pr.new/@tanstack/preact-ai-devtools@478

      @tanstack/react-ai-devtools

      npm i https://pkg.pr.new/@tanstack/react-ai-devtools@478

      @tanstack/solid-ai-devtools

      npm i https://pkg.pr.new/@tanstack/solid-ai-devtools@478

      commit: 9e42324

    5. 🧹 Nitpick comments (3)
      packages/typescript/ai-client/src/connection-adapters.ts (1)

      495-497: Optional: honor abortSignal while replaying chunks.

      Since the whole payload is already in memory, the loop ignores abortSignal during replay. If the consumer aborts late (e.g., user navigates away before chunks are drained by the pipeline), chunks will continue to flow. Consider a cheap check to bail out early:

      ♻️ Suggested tweak
             for (const chunk of payload) {
      +        if (abortSignal?.aborted) break
               yield chunk as StreamChunk
             }

      Not a blocker given the buffered/non-streaming nature of this adapter.

      🤖 Prompt for AI Agents
      Verify each finding against the current code and only fix it if needed.
      
      In `@packages/typescript/ai-client/src/connection-adapters.ts` around lines 495 -
      497, The replay loop that yields chunks from the in-memory payload currently
      ignores abortSignal and continues pushing chunks even after cancellation; inside
      the loop that iterates over payload and yields each item (the block yielding
      chunk as StreamChunk), check the provided abortSignal (e.g.,
      abortSignal?.aborted) on each iteration and bail out immediately (return/stop
      iteration) when aborted so the generator stops producing further StreamChunk
      values.
      packages/typescript/ai-client/package.json (1)

      21-35: Dual exports look correct; consider exposing ./package.json.

      The nested import/require conditions with type-aware types keys are the recommended Node resolution shape, and main → .cjs pairs correctly with "type": "module" (Node uses the extension to disambiguate). publint strict passing is a good signal.

      Optional: add "./package.json": "./package.json" to exports so tools that probe the manifest (some bundlers, version resolvers) don't get blocked by the closed export map. Same applies to packages/typescript/ai/package.json and packages/typescript/ai-event-client/package.json.

      Proposed addition
         "exports": {
           ".": {
             "import": {
               "types": "./dist/esm/index.d.ts",
               "default": "./dist/esm/index.js"
             },
             "require": {
               "types": "./dist/cjs/index.d.cts",
               "default": "./dist/cjs/index.cjs"
             }
      -    }
      +    },
      +    "./package.json": "./package.json"
         },
      🤖 Prompt for AI Agents
      Verify each finding against the current code and only fix it if needed.
      
      In `@packages/typescript/ai-client/package.json` around lines 21 - 35, Add an
      explicit export entry for the package manifest so consumers and tooling can read
      it: update the package.json "exports" object to include the key "./package.json"
      mapping to "./package.json" (mirror this change in
      packages/typescript/ai/package.json and
      packages/typescript/ai-event-client/package.json as well); locate the "exports"
      block that currently defines "." with "import"/"require" and add the
      "./package.json": "./package.json" mapping alongside those entries.
      packages/typescript/ai/tests/stream-to-response.test.ts (1)

      875-945: LGTM — good coverage for toJSONResponse.

      Tests cover the four meaningful branches (defaults, custom init/headers, explicit Content-Type passthrough, and abort-on-upstream-error with rethrow). Nice use of toHaveBeenCalledOnce() to assert abort happens exactly once.

      One optional addition worth considering: a test that asserts the controller is not aborted when the stream drains successfully, to lock in that behavior against regressions.

      🤖 Prompt for AI Agents
      Verify each finding against the current code and only fix it if needed.
      
      In `@packages/typescript/ai/tests/stream-to-response.test.ts` around lines 875 -
      945, Add a test to ensure the provided AbortController is NOT aborted when the
      stream drains successfully: create an AbortController, spy on its abort method
      (vi.spyOn(abortController, 'abort')), call toJSONResponse with
      createMockStream([...successful chunks...]) and the abortController in options,
      await the response.json() (or response completion), then assert abortSpy was not
      called (toHaveBeenCalledTimes(0) / not.toHaveBeenCalled()). Reference
      toJSONResponse, createMockStream, and AbortController/abort in the test so
      behavior is covered alongside the existing abort-on-error test.
      🤖 Prompt for all review comments with AI agents
      Verify each finding against the current code and only fix it if needed.
      
      Nitpick comments:
      In `@packages/typescript/ai-client/package.json`:
      - Around line 21-35: Add an explicit export entry for the package manifest so
      consumers and tooling can read it: update the package.json "exports" object to
      include the key "./package.json" mapping to "./package.json" (mirror this change
      in packages/typescript/ai/package.json and
      packages/typescript/ai-event-client/package.json as well); locate the "exports"
      block that currently defines "." with "import"/"require" and add the
      "./package.json": "./package.json" mapping alongside those entries.
      
      In `@packages/typescript/ai-client/src/connection-adapters.ts`:
      - Around line 495-497: The replay loop that yields chunks from the in-memory
      payload currently ignores abortSignal and continues pushing chunks even after
      cancellation; inside the loop that iterates over payload and yields each item
      (the block yielding chunk as StreamChunk), check the provided abortSignal (e.g.,
      abortSignal?.aborted) on each iteration and bail out immediately (return/stop
      iteration) when aborted so the generator stops producing further StreamChunk
      values.
      
      In `@packages/typescript/ai/tests/stream-to-response.test.ts`:
      - Around line 875-945: Add a test to ensure the provided AbortController is NOT
      aborted when the stream drains successfully: create an AbortController, spy on
      its abort method (vi.spyOn(abortController, 'abort')), call toJSONResponse with
      createMockStream([...successful chunks...]) and the abortController in options,
      await the response.json() (or response completion), then assert abortSpy was not
      called (toHaveBeenCalledTimes(0) / not.toHaveBeenCalled()). Reference
      toJSONResponse, createMockStream, and AbortController/abort in the test so
      behavior is covered alongside the existing abort-on-error test.

      ℹ️ Review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: d831b837-151e-456f-8d68-13b77d844f5a

      📥 Commits

      Reviewing files that changed from the base of the PR and between 1d6f3bef4fd1c4917823612fbcd9450a0fd2e627 and 1bbc932081ab497eecc95ae9f86063d89d4fc56f.

      📒 Files selected for processing (12)
      • .changeset/cjs-output-and-json-response.md
      • packages/typescript/ai-client/package.json
      • packages/typescript/ai-client/src/connection-adapters.ts
      • packages/typescript/ai-client/src/index.ts
      • packages/typescript/ai-client/vite.config.ts
      • packages/typescript/ai-event-client/package.json
      • packages/typescript/ai-event-client/vite.config.ts
      • packages/typescript/ai/package.json
      • packages/typescript/ai/src/index.ts
      • packages/typescript/ai/src/stream-to-response.ts
      • packages/typescript/ai/tests/stream-to-response.test.ts
      • packages/typescript/ai/vite.config.ts
    6. 🧹 Nitpick comments (1)
      docs/api/ai-client.md (1)

      145-184: Consider varying sentence structure to improve readability.

      Three connection adapter sections in succession begin with "Creates," making the documentation slightly repetitive. Consider varying the opening phrase for better flow.

      ✍️ Suggested rewording
       ### `fetchServerSentEvents(url, options?)`
       
      -Creates an SSE connection adapter.
      +Establishes an SSE connection adapter for server-sent events streaming.

      or

       ### `fetchJSON(url, options?)`
       
      -Creates a connection adapter for non-streaming runtimes — pair with [`toJSONResponse`](./ai#tojsonresponsestream-init) on the server.
      +Provides a connection adapter for non-streaming runtimes — pair with [`toJSONResponse`](./ai#tojsonresponsestream-init) on the server.
      🤖 Prompt for AI Agents
      Verify each finding against the current code and only fix it if needed.
      
      In `@docs/api/ai-client.md` around lines 145 - 184, The three adapter descriptions
      (fetchServerSentEvents, fetchHttpStream, fetchJSON) all start with the same verb
      "Creates," making the copy repetitive; update the lead sentence for one or two
      of these functions to vary phrasing (e.g., "Opens an SSE connection adapter
      for...", "Provides an HTTP stream adapter that...", or "Returns a JSON-based
      adapter for non-streaming runtimes...") while keeping the technical details
      intact (include options example and the note about POSTing { messages, data }
      for fetchJSON and the trade-off about no incremental rendering), and ensure the
      function names fetchServerSentEvents, fetchHttpStream, and fetchJSON remain
      present so readers can locate the API.
      🤖 Prompt for all review comments with AI agents
      Verify each finding against the current code and only fix it if needed.
      
      Nitpick comments:
      In `@docs/api/ai-client.md`:
      - Around line 145-184: The three adapter descriptions (fetchServerSentEvents,
      fetchHttpStream, fetchJSON) all start with the same verb "Creates," making the
      copy repetitive; update the lead sentence for one or two of these functions to
      vary phrasing (e.g., "Opens an SSE connection adapter for...", "Provides an HTTP
      stream adapter that...", or "Returns a JSON-based adapter for non-streaming
      runtimes...") while keeping the technical details intact (include options
      example and the note about POSTing { messages, data } for fetchJSON and the
      trade-off about no incremental rendering), and ensure the function names
      fetchServerSentEvents, fetchHttpStream, and fetchJSON remain present so readers
      can locate the API.

      ℹ️ Review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: c65a6675-dcd6-4072-9b42-d6a27f6233ca

      📥 Commits

      Reviewing files that changed from the base of the PR and between 1bbc932081ab497eecc95ae9f86063d89d4fc56f and ee3f393cdd95756f5e7bc889e8adf49fb601cff0.

      📒 Files selected for processing (6)
      • docs/api/ai-client.md
      • docs/api/ai.md
      • docs/chat/connection-adapters.md
      • docs/chat/non-streaming-runtimes.md
      • docs/chat/streaming.md
      • docs/config.json
      ✅ Files skipped from review due to trivial changes (4)
      • docs/config.json
      • docs/chat/non-streaming-runtimes.md
      • docs/chat/streaming.md
      • docs/chat/connection-adapters.md
    7. tombeckenham requested changes

      A couple of small changes to make would make this more solid

      @@ -426,0 +476,21 @@
      476+ method: 'POST',
      477+ headers: requestHeaders,
      478+ body: JSON.stringify(requestBody),
      479+ credentials: resolvedOptions.credentials || 'same-origin',
      480+ signal: abortSignal || resolvedOptions.signal,
      481+ })
      482+
      483+ if (!response.ok) {
      484+ throw new Error(
      485+ `HTTP error! status: ${response.status} ${response.statusText}`,
      486+ )
      487+ }
      488+
      489+ const payload = (await response.json()) as unknown
      490+ if (!Array.isArray(payload)) {
      491+ throw new Error(
      492+ 'fetchJSON: expected response body to be a JSON array of StreamChunks. Did you forget to use `toJSONResponse(stream)` on the server?',
      493+ )
      494+ }
      495+ for (const chunk of payload) {
      496+ yield chunk as StreamChunk
      tombeckenhamcommented

      You never check for the abort signal in this yield loop despite adding it in toJSONResponse

      @@ -426,0 +469,21 @@
      469+ messages,
      470+ data,
      471+ ...resolvedOptions.body,
      472+ }
      473+
      474+ const fetchClient = resolvedOptions.fetchClient ?? fetch
      475+ const response = await fetchClient(resolvedUrl, {
      476+ method: 'POST',
      477+ headers: requestHeaders,
      478+ body: JSON.stringify(requestBody),
      479+ credentials: resolvedOptions.credentials || 'same-origin',
      480+ signal: abortSignal || resolvedOptions.signal,
      481+ })
      482+
      483+ if (!response.ok) {
      484+ throw new Error(
      485+ `HTTP error! status: ${response.status} ${response.statusText}`,
      486+ )
      487+ }
      488+
      489+ const payload = (await response.json()) as unknown
      tombeckenhamcommented

      You need to wrap this in try catch otherwise the user will get an annoying "Unexpected token < in JSON at position 0" error instead of what probably happened e.g. a gateway error or something that returned html

      Suggested change
      489- const payload = (await response.json()) as unknown
      489+ let payload: unknown
      490+ try {
      491+ payload = await response.json()
      492+ } catch (err) {
      493+ const cause = err instanceof Error ? err.message : String(err)
      494+ throw new Error(
      495+ `fetchJSON: failed to parse response body as JSON from ${resolvedUrl} (status ${response.status}):
      496+ ${cause}`,
      497+ { cause: err },
      498+ )
      499+ }
      @@ -426,0 +463,21 @@
      463+ const requestHeaders: Record<string, string> = {
      464+ 'Content-Type': 'application/json',
      465+ ...mergeHeaders(resolvedOptions.headers),
      466+ }
      467+
      468+ const requestBody = {
      469+ messages,
      470+ data,
      471+ ...resolvedOptions.body,
      472+ }
      473+
      474+ const fetchClient = resolvedOptions.fetchClient ?? fetch
      475+ const response = await fetchClient(resolvedUrl, {
      476+ method: 'POST',
      477+ headers: requestHeaders,
      478+ body: JSON.stringify(requestBody),
      479+ credentials: resolvedOptions.credentials || 'same-origin',
      480+ signal: abortSignal || resolvedOptions.signal,
      481+ })
      482+
      483+ if (!response.ok) {
      tombeckenhamcommented

      servers usually put the actual diagnostic information in the body. e.g. OpenAI/Anthropic upstream rate limit: {"error":{"type":"rate_limit_error","message":"Rate limit exceeded for...","retryAfter":42}}

      Suggested change
      483- if (!response.ok) {
      483+ if (!response.ok) {
      484+ let bodySnippet = ''
      485+ try {
      486+ const text = await response.text()
      487+ bodySnippet = text.length > 500 ? `${text.slice(0, 500)}…` : text
      488+ } catch {
      489+ // body unreadable, fall through with status only
      490+ }
      491+ throw new Error(
      492+ `HTTP error! status: ${response.status} ${response.statusText}${
      493+ bodySnippet ? ` — ${bodySnippet}` : ''
      494+ }`,
      495+ )
      496+ }
      @@ -426,0 +431,21 @@
      431+ * server drains the chat stream fully, JSON-serialises the collected
      432+ * chunks into an array, and this adapter fetches the array and replays
      433+ * each chunk one-by-one into the normal client pipeline.
      434+ *
      435+ * Trade-off: you lose incremental rendering — the UI sees every chunk
      436+ * only after the request resolves. Use SSE/HTTP-stream adapters when the
      437+ * runtime supports them.
      438+ *
      439+ * @param url - The API endpoint URL (or a function that returns the URL)
      440+ * @param options - Fetch options (headers, credentials, body, etc.) or a function that returns options (can be async)
      441+ * @returns A connection adapter for JSON-array responses
      442+ *
      443+ * @example
      444+ * ```typescript
      445+ * // Expo / RN client that hits an Expo API route returning toJSONResponse(stream)
      446+ * const connection = fetchJSON('/api/chat')
      447+ *
      448+ * const client = new ChatClient({ connection })
      449+ * ```
      450+ */
      451+export function fetchJSON(
      tombeckenhamcommented

      You should add an e2e test for the roundtrip toJSONResponse → fetchJSON

    8. tombeckenham assignedtombeckenham

    9. AlemTuzlak closed this

Sign in to join this conversation.