Open History to see the last commit
1#!/usr/bin/env node
2
3/**
4 * TanStack AI - Structured Streaming Demo
5 *
6 * This demonstrates the new JSON-based streaming API that works with ALL providers:
7 * - OpenAI
8 * - Anthropic
9 * - Ollama
10 * - Google Gemini
11 */
12
13import { AI } from "../packages/ai/dist/index.js";
14import { OpenAIAdapter } from "../packages/ai-openai/dist/index.js";
15
16async function demoStructuredStreaming() {
17 console.log("🚀 TanStack AI - Structured Streaming Demo\n");
18 console.log("This shows the JSON chunks being streamed in real-time:\n");
19
20 const ai = new AI(
21 new OpenAIAdapter({
22 apiKey: process.env.OPENAI_API_KEY || "demo-key",
23 })
24 );
25
26 try {
27 console.log("📡 Starting stream...\n");
28
29 for await (const chunk of ai.streamChat({
30 model: "gpt-3.5-turbo",
31 messages: [{ role: "user", content: "Say hello in 3 words" }],
32 })) {
33 // Each chunk is a structured JSON object
34 console.log("📦 Chunk:", JSON.stringify(chunk, null, 2));
35 console.log(""); // Blank line for readability
36 }
37 } catch (error) {
38 console.error("❌ Error (expected without API key):", error.message);
39 }
40
41 console.log("\n✨ Key Features:");
42 console.log(
43 " • Works with ALL providers (OpenAI, Anthropic, Ollama, Gemini)"
44 );
45 console.log(" • Returns structured JSON chunks with type discrimination");
46 console.log(" • Includes token deltas AND accumulated content");
47 console.log(" • Supports tool calling (function streaming)");
48 console.log(" • Provides usage statistics in 'done' chunk");
49 console.log(" • Error handling with 'error' chunks");
50}
51
52demoStructuredStreaming();