ai-stream-parser
v1.0.0
Published
A TypeScript SDK for parsing AI streaming output with JSX-style syntax
Maintainers
Readme
AI Stream Parser
A TypeScript SDK designed for parsing AI streaming output with JSX-style syntax, making it easy to handle and structure real-time AI responses.
Features
- AI-First Design: Optimized for ChatGPT, Claude, and other AI model outputs
- JSX-Style Parsing: Use familiar JSX tag syntax to structure AI responses
- Real-time Streaming: Parse and build structure as content streams
- Smart Tag Handling: Custom tag handlers with priority and attribute parsing
- TypeScript Support: Full type definitions for excellent development experience
Quick Start
Installation
npm install ai-stream-parserBasic Usage
import { StreamParser, BasicTagHandler } from 'ai-stream-parser'
// Create parser instance
const parser = new StreamParser()
// Register common AI output handlers
parser.registerTagHandler(new BasicTagHandler('think', 10))
parser.registerTagHandler(new BasicTagHandler('response', 5))
parser.registerTagHandler(new BasicTagHandler('step', 3))
// Process AI streaming output
parser.process(`
<think priority="high">
Let me analyze this step by step...
<step>1. First, understand key points</step>
<step>2. Then, consider implications</step>
<step>3. Finally, form response</step>
</think>
<response type="detailed">
Based on my analysis, here's what I found...
</response>
`)
// Get structured result
const result = parser.finalize()AI Response Structures
AI Stream Parser helps you structure AI output using JSX-style syntax:
1. Thought Process
<think>
<step>Analyzing the problem...</step>
<step>Considering alternatives...</step>
<step>Selecting best option...</step>
</think>2. Response Types
<response type="detailed" confidence="high">
Here's my detailed analysis...
</response>3. Structured Data
<data>
<summary>Key findings</summary>
<details>Supporting evidence</details>
<recommendations>
<item priority="high">Primary recommendation</item>
<item priority="medium">Secondary recommendation</item>
</recommendations>
</data>4. Error Handling
<error type="validation">
<message>Invalid input format</message>
<suggestion>Please provide data in JSON format</suggestion>
</error>API Reference
StreamParser
Main parser class for handling AI streaming content.
class StreamParser {
// Register a tag handler
registerTagHandler(handler: TagHandler): void
// Process streaming content
process(text: string): void
// Finalize parsing and get structured result
finalize(): ParseResult
}BasicTagHandler
Base tag handler for processing AI response components.
class BasicTagHandler implements TagHandler {
constructor(
tagName: string,
priority: number,
options?: {
allowSelfClosing?: boolean,
attributeParser?: AttributeParser
}
)
}License
MIT
