Skip to content

feat(ai-pi-stream): put TanStack useChat in front of a pi agent - #1911

Closed
tombeckenham wants to merge 1 commit into
mainfrom
feat/ai-pi-agent
Closed

tombeckenham wants to merge 1 commit into
mainfrom
feat/ai-pi-agent

Conversation

@tombeckenham

Copy link
Copy Markdown
Contributor

Put TanStack AI useChat in front of a pi agent. The new package @tanstack/ai-pi-stream has runPiAgent, which runs the pi agent loop for a useChat request and streams AG-UI events. pi tools in needsApproval pause for a useChat approval, and the client tools of useChat run in the browser.

This PR replaces the useChat-to-pi half of #1894. The comparison of the two toolkits is in #1910.

🎯 Changes

  • New package @tanstack/ai-pi-stream:
    • runPiAgent(params, { config, streamFn, tools, needsApproval, systemPrompts }) serves a pi agent to useChat, with approvals, client tools and resume.
    • fromPiAgentEvents and toPiMessages cover a hand-run agentLoop().
    • It uses only exports that are on main now. @tanstack/ai and the other packages do not change.
  • Docs: the guide docs/migration/pi-agent-tanstack-client.md, the table docs/migration/pi-compatibility.md, and their nav entries (addedAt 2026-10-26). The pi part of the tanstack-ai-migration skill.
  • E2E: the /pi-agent page and API route in testing/e2e, and tests/pi-stream.spec.ts.
  • Repo config:
    • A pnpm override pins the Bedrock SDK that @earendil-works/pi-ai pulls in.
    • kiira and tsconfig.docs.json resolve the pi packages for the doc samples.
  • Changeset: @tanstack/ai-pi-stream minor.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.
  • Docs: I updated docs/ for this change, or this change is not user-facing.
  • Changeset: I added a changeset (pnpm changeset), or this PR does not change a published package.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Testing

Commands run

  • @tanstack/ai-pi-stream: test:types, vitest run (17 passed), test:oxlint, build and test:build (publint). All pass.
  • pnpm test:knip, pnpm test:sherif and pnpm test:docs pass. sherif has one old warning about examples/react.
  • kiira check on the two pages and the package README: 5 snippets pass.
  • @tanstack/ai-e2e:test:types passes.
  • playwright test pi-stream.spec.ts: 14 passed.
  • Not run: the full pnpm test:pr and the full e2e suite. CI runs them.

Manual test

  1. Run pnpm build:all, then pnpm --filter @tanstack/ai-e2e dev. No API keys are necessary.
  2. Open /pi-agent. Click each scenario link. The page shows the expected result next to each link.
  3. Click Delete a.txt, then Approve. The pi tool runs and the reply continues. Do it again and click Deny. The model sees the denial.
  4. Click Locate me. The client tool runs in the browser, and pi continues from its result.
  5. Click the ?raw= links. Each one sends a request that useChat never sends, and ends with a RUN_ERROR code, not an HTTP 500.

How this PR makes testing easy

  • Unit tests: packages/ai-pi-stream/tests/, with the faux pi model and no network.
  • E2E: testing/e2e/tests/pi-stream.spec.ts has 14 tests, one for each scenario link on /pi-agent.

Risk / rollback

Low. The PR adds a new package and changes no published package. Revert the PR to remove it.

Public API change

Before

// No way to serve a pi agent to TanStack useChat.

After

import { chatParamsFromRequest, toServerSentEventsResponse } from '@tanstack/ai'
import { runPiAgent } from '@tanstack/ai-pi-stream'

export async function POST(request: Request) {
  return toServerSentEventsResponse(
    runPiAgent(await chatParamsFromRequest(request), {
      config: { model, convertToLlm: (piMessages) => piMessages },
      streamFn,
      tools,
      needsApproval: ['deleteFile'],
    }),
  )
}

runPiAgent runs the pi agent loop for a useChat request and streams
AG-UI events, with tool approvals and client tools. fromPiAgentEvents
and toPiMessages cover a hand-run agentLoop(). Adds the /pi-agent e2e
route and the guide and compatibility docs.
@changeset-bot

changeset-bot Bot commented Oct 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 485b6bb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@tanstack/ai-pi-stream Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Caution

Review failed

The pull request is closed.

Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@socket-security

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addednpm/​@​earendil-works/​pi-agent-core@​1.4.010010010098100
Addednpm/​@​earendil-works/​pi-ai@​1.4.010010010098100

View full report

@tombeckenham

Copy link
Copy Markdown
Contributor Author

Closing: no one is asking for a pi bridge. The branch stays if that changes.

@github-actions

Copy link
Copy Markdown
Contributor

Coverage

✅ Coverage held across 75 compared package(s).

Each package is measured twice in this job — on this PR and on its merge-base with main — and compared. A metric fails only when it is at or above 60% on the merge-base and under 60% on this PR. Other drops are listed but do not fail. Packages your PR doesn't affect are not measured and not listed.

package statements branches functions lines
ai 91.95% (0.00) 85.79% (0.00) 92.12% (0.00) 93.54% (0.00) ok
ai-acp 83.04% (0.00) 71.25% (0.00) 79.01% (0.00) 84.93% (0.00) ok
ai-angular 73.83% (0.00) 60.89% (0.00) 61.83% (0.00) 75.40% (0.00) ok
ai-anthropic 93.19% (0.00) 88.31% (0.00) 95.93% (0.00) 93.70% (0.00) ok
ai-bedrock 90.05% (0.00) 82.59% (0.00) 94.20% (0.00) 91.27% (0.00) ok
ai-byteplus 97.05% (0.00) 91.62% (0.00) 98.27% (0.00) 98.48% (0.00) ok
ai-claude-code 88.90% (0.00) 76.88% (0.00) 86.66% (0.00) 90.38% (0.00) ok
ai-client 87.81% (0.00) 79.97% (0.00) 87.01% (0.00) 90.13% (0.00) ok
ai-cloudflare 85.76% (0.00) 77.52% (0.00) 85.48% (0.00) 86.94% (0.00) ok
ai-code-mode 93.47% (0.00) 85.63% (0.00) 92.04% (0.00) 94.44% (0.00) ok
ai-code-mode-snippets 87.13% (0.00) 75.67% (0.00) 91.11% (0.00) 88.39% (0.00) ok
ai-codex 94.53% (0.00) 85.10% (0.00) 89.09% (0.00) 95.54% (0.00) ok
ai-cohere 88.97% (0.00) 88.37% (0.00) 85.71% (0.00) 91.80% (0.00) ok
ai-compaction 97.41% (0.00) 92.89% (0.00) 100.00% (0.00) 98.89% (0.00) ok
ai-computer-local 97.29% (0.00) 85.71% (0.00) 88.00% (0.00) 96.84% (0.00) ok
ai-computer-playwright 100.00% (0.00) 100.00% (0.00) 100.00% (0.00) 100.00% (0.00) ok
ai-computer-sandbox 96.20% (0.00) 84.37% (0.00) 100.00% (0.00) 95.94% (0.00) ok
ai-computer-use 96.95% (0.00) 92.63% (0.00) 100.00% (0.00) 99.45% (0.00) ok
ai-devtools 83.13% (0.00) 72.77% (0.00) 87.07% (0.00) 85.22% (0.00) ok
ai-durable-stream 86.70% (0.00) 82.25% (0.00) 92.30% (0.00) 88.72% (0.00) ok
ai-elevenlabs 74.68% (0.00) 69.59% (0.00) 59.13% (0.00) 75.46% (0.00) ok
ai-event-client 56.83% (0.00) 48.07% (0.00) 68.96% (0.00) 58.33% (0.00) ok
ai-fal 91.10% (0.00) 82.63% (0.00) 92.40% (0.00) 91.60% (0.00) ok
ai-gemini 68.65% (0.00) 68.66% (0.00) 65.38% (0.00) 69.25% (0.00) ok
ai-grok 48.80% (0.00) 53.49% (0.00) 56.46% (0.00) 49.63% (0.00) ok
ai-grok-build 79.14% (0.00) 63.56% (0.00) 76.99% (0.00) 80.97% (0.00) ok
ai-groq 51.30% (0.00) 47.73% (0.00) 67.39% (0.00) 53.42% (0.00) ok
ai-harness 93.79% (+0.01) 88.07% (0.00) 93.62% (+0.02) 95.58% (+0.01) ok
ai-harness-cli 91.08% (0.00) 83.24% (0.00) 83.85% (0.00) 92.12% (0.00) ok
ai-isolate-cloudflare 94.11% (0.00) 70.66% (0.00) 100.00% (0.00) 93.96% (0.00) ok
ai-isolate-daytona 90.25% (0.00) 80.59% (0.00) 100.00% (0.00) 90.15% (0.00) ok
ai-isolate-e2b 92.11% (0.00) 82.22% (0.00) 97.36% (0.00) 95.05% (0.00) ok
ai-isolate-node 84.00% (0.00) 43.39% (0.00) 100.00% (0.00) 83.78% (0.00) ok
ai-isolate-quickjs 89.07% (0.00) 76.41% (0.00) 90.90% (0.00) 90.30% (0.00) ok
ai-isolate-quickjs-bun 7.76% (0.00) 8.08% (0.00) 5.00% (0.00) 8.33% (0.00) ok
ai-llmgateway 97.77% (0.00) 100.00% (0.00) 88.88% (0.00) 97.77% (0.00) ok
ai-lovable 73.21% (0.00) 55.63% (0.00) 85.07% (0.00) 73.96% (0.00) ok
ai-mcp 94.69% (0.00) 89.38% (0.00) 96.80% (0.00) 96.84% (0.00) ok
ai-memory 73.87% (0.00) 52.70% (0.00) 79.74% (0.00) 76.04% (0.00) ok
ai-mistral 84.75% (0.00) 79.82% (0.00) 92.59% (0.00) 85.39% (0.00) ok
ai-neon 95.34% (0.00) 88.88% (0.00) 94.73% (0.00) 96.85% (0.00) ok
ai-octane 91.19% (0.00) 68.26% (0.00) 87.44% (0.00) 91.47% (0.00) ok
ai-ollama 93.80% (0.00) 80.92% (0.00) 90.62% (0.00) 93.76% (0.00) ok
ai-ollaya 86.84% (0.00) 89.28% (0.00) 85.71% (0.00) 91.66% (0.00) ok
ai-openai 65.36% (0.00) 65.83% (0.00) 66.99% (0.00) 65.80% (0.00) ok
ai-opencode 70.50% (0.00) 62.29% (0.00) 66.32% (0.00) 72.12% (0.00) ok
ai-openrouter 88.28% (0.00) 73.44% (0.00) 90.78% (0.00) 90.23% (0.00) ok
ai-perplexity 91.34% (0.00) 90.43% (0.00) 100.00% (0.00) 93.87% (0.00) ok
ai-persistence 92.06% (0.00) 78.91% (0.00) 97.81% (0.00) 96.01% (0.00) ok
ai-pi-stream 92.92% 84.13% 95.23% 94.34% new
ai-preact 86.50% (0.00) 71.00% (0.00) 87.50% (0.00) 87.98% (0.00) ok
ai-react 81.94% (0.00) 65.42% (0.00) 80.73% (0.00) 82.67% (0.00) ok
ai-reactor 94.91% (0.00) 83.33% (0.00) 93.33% (0.00) 96.55% (0.00) ok
ai-remix 43.02% (0.00) 30.35% (0.00) 39.02% (0.00) 46.23% (0.00) ok
ai-sandbox 78.42% (-0.07) 83.12% (-0.04) 75.41% (-0.11) 78.98% (-0.05) ok
ai-sandbox-blaxel 87.66% (0.00) 77.18% (0.00) 82.26% (0.00) 89.69% (0.00) ok
ai-sandbox-boxd 96.01% (0.00) 87.12% (0.00) 95.31% (0.00) 98.24% (0.00) ok
ai-sandbox-cloudflare 67.50% (0.00) 57.27% (0.00) 71.64% (0.00) 68.66% (0.00) ok
ai-sandbox-daytona 81.04% (0.00) 81.89% (0.00) 67.60% (0.00) 83.18% (0.00) ok
ai-sandbox-docker 82.34% (0.00) 73.69% (0.00) 79.82% (0.00) 84.06% (0.00) ok
ai-sandbox-e2b 94.84% (0.00) 89.70% (0.00) 93.24% (0.00) 98.64% (0.00) ok
ai-sandbox-local-process 69.27% (0.00) 53.59% (0.00) 77.01% (0.00) 70.30% (0.00) ok
ai-sandbox-railway 94.87% (0.00) 92.68% (0.00) 90.90% (0.00) 97.65% (0.00) ok
ai-sandbox-sprites 73.10% (0.00) 59.70% (0.00) 73.83% (0.00) 75.95% (0.00) ok
ai-sandbox-upstash-box 94.61% (0.00) 91.52% (0.00) 91.04% (0.00) 97.42% (0.00) ok
ai-sandbox-vercel 43.20% (0.00) 38.18% (0.00) 35.18% (0.00) 44.08% (0.00) ok
ai-skills 83.94% (0.00) 67.95% (0.00) 88.00% (0.00) 86.03% (0.00) ok
ai-solid 75.81% (0.00) 57.63% (0.00) 71.28% (0.00) 78.11% (0.00) ok
ai-svelte 74.05% (0.00) 50.99% (0.00) 70.05% (0.00) 78.64% (0.00) ok
ai-typesafe 78.04% (0.00) 84.61% (0.00) 66.66% (0.00) 82.05% (0.00) ok
ai-utils 82.81% (0.00) 75.64% (0.00) 89.47% (0.00) 84.21% (0.00) ok
ai-vercel-gateway 77.56% (0.00) 63.38% (0.00) 84.90% (0.00) 80.76% (0.00) ok
ai-vertex 96.77% (0.00) 97.22% (0.00) 100.00% (0.00) 96.77% (0.00) ok
ai-vue 73.33% (0.00) 48.73% (0.00) 73.28% (0.00) 74.66% (0.00) ok
ai-worldlabs 79.62% (0.00) 71.50% (0.00) 91.42% (0.00) 82.66% (0.00) ok
openai-base 91.04% (0.00) 79.96% (0.00) 97.81% (0.00) 93.18% (0.00) ok

No coverage on the base commit, so nothing to compare against: ai-pi-stream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant