toon-package

Agent Streaming Example

Complete example demonstrating agent streaming with TOON format using @programsmagic/toon-backend-node and @programsmagic/toon-frontend.

Overview

This example shows how to:

Prerequisites

Step-by-Step Setup

Step 1: Install Dependencies

cd examples/agent-streaming
pnpm install

Step 2: Start the Server

pnpm start

The server will start on http://localhost:3000.

Step 3: Test the Streaming Endpoint

# Stream TOON format
curl "http://localhost:3000/stream/toon?file=data.json&format=json&model=gpt-4"

# Stream with format conversion
curl "http://localhost:3000/stream/toon?file=data.json&from=json&to=toon"

Features

API Endpoints

Complete Code Example

Backend (server.js)

import { createServer } from '@programsmagic/toon-backend-node';

const server = await createServer({
  port: 3000,
  schemaSource: './schema.json',
  cors: true,
});

await server.start();
console.log('Server running at http://localhost:3000');

Frontend (App.tsx)

import { StreamViewer } from '@programsmagic/toon-frontend';
import '@programsmagic/toon-frontend/styles';

function App() {
  return (
    <StreamViewer
      url="http://localhost:3000/stream/toon?file=data.json"
      protocol="sse"
      showTokenCounter={true}
      model="gpt-4"
    />
  );
}

Expected Output

When you access the streaming endpoint, you’ll see:

data: {"type":"STREAM_START","timestamp":1234567890}
data: {"type":"TOKEN_COUNT","tokens":150,"model":"gpt-4"}
data: {"type":"FORMAT_CONVERTED","from":"json","to":"toon"}
data: {"type":"STREAM_END","timestamp":1234567891}

Troubleshooting

Streaming Not Working

Problem: No data streaming from endpoint.

Solution:

Token Counting Errors

Problem: Token counting fails.

Solution:

Format Conversion Errors

Problem: Format conversion fails.

Solution:

Next Steps