CHUNKED_RESPONSE_SYSTEM.md•5.9 kB
# Chunked Response System
## Overview
The conversation detector has been updated to provide a **chunked response system** that prevents overwhelming chat contexts by returning only the first chunk with instructions to call subsequent chunks. This approach significantly reduces the amount of data returned in a single response while maintaining full functionality.
## Key Features
### 1. First Chunk Only Response
- `detectConversations()` now returns only the **first chunk**
- Includes metadata about total chunks and navigation instructions
- Prevents context overflow in chat applications
### 2. Progressive Chunk Retrieval
- `getNextChunk(chunkIndex, subtitles)` function for retrieving specific chunks
- Each chunk includes instructions for the next chunk
- Maintains conversation context and speaker information
### 3. Context Preservation
- All conversation context is preserved across chunks
- Speaker information, timing, and semantic analysis maintained
- Seamless navigation between chunks
## Usage Examples
### Basic Usage
```javascript
import { detectConversations, getNextChunk } from './dist/chunking/conversation-detector.js';
import { parseSRTFile } from './dist/parsers/srt-parser.js';
// Parse SRT content
const parseResult = parseSRTFile(srtContent);
const subtitles = parseResult.file.subtitles;
// Get first chunk with instructions
const firstChunk = detectConversations(subtitles)[0];
console.log(`Chunk ${firstChunk.context?.currentChunk} of ${firstChunk.context?.totalChunks}`);
console.log(`Instruction: ${firstChunk.context?.instructionText}`);
// Get subsequent chunks
if (firstChunk.context?.hasMoreChunks) {
const secondChunk = getNextChunk(2, subtitles);
// Process second chunk...
}
```
### Advanced Usage with Custom Parameters
```javascript
import { detectConversationsAdvanced } from './dist/chunking/conversation-detector.js';
// Use advanced detection with custom parameters
const allChunks = detectConversationsAdvanced(subtitles, {
boundaryThreshold: 0.5, // More aggressive splitting
maxChunkSize: 5, // Smaller chunks
minChunkSize: 1, // Allow single subtitle chunks
enableSemanticAnalysis: true,
enableSpeakerDiarization: true
});
// Get first chunk
const firstChunk = allChunks[0];
```
## Response Structure
### Chunk Object
```typescript
interface SRTChunk {
id: string;
startIndex: number;
endIndex: number;
subtitles: SRTSubtitle[];
context?: ConversationContext;
}
```
### Enhanced Conversation Context
```typescript
interface ConversationContext {
// Existing properties...
speaker?: string;
conversationId: string;
previousContext?: string;
nextContext?: string;
// New chunk navigation properties
totalChunks?: number; // Total number of chunks
currentChunk?: number; // Current chunk index (1-based)
hasMoreChunks?: boolean; // Whether more chunks exist
instructionText?: string; // Instruction for next chunk
}
```
## Benefits
### 1. Reduced Context Overflow
- Only returns necessary data for immediate processing
- Prevents overwhelming chat applications with large responses
- Maintains efficient memory usage
### 2. Progressive Processing
- Allows processing of large SRT files in manageable chunks
- Enables streaming-style processing for real-time applications
- Supports pagination-like navigation through conversations
### 3. Maintained Functionality
- All conversation detection algorithms remain intact
- Speaker diarization and semantic analysis preserved
- Full backward compatibility with existing code
## Migration Guide
### Before (Overwhelming Response)
```javascript
// Old approach - returns ALL chunks at once
const allChunks = detectConversations(subtitles);
// This could return hundreds of chunks, overwhelming the context
```
### After (Chunked Response)
```javascript
// New approach - returns only first chunk with instructions
const firstChunk = detectConversations(subtitles)[0];
// Get subsequent chunks as needed
const secondChunk = getNextChunk(2, subtitles);
```
## Best Practices
### 1. Check for More Chunks
Always check if more chunks are available before attempting to retrieve them:
```javascript
if (chunk.context?.hasMoreChunks) {
const nextChunk = getNextChunk(chunk.context.currentChunk + 1, subtitles);
}
```
### 2. Handle Last Chunk
The last chunk will have `hasMoreChunks: false` and appropriate instruction text:
```javascript
if (!chunk.context?.hasMoreChunks) {
console.log("This is the last chunk");
}
```
### 3. Use Appropriate Parameters
For better chunking control, use `detectConversationsAdvanced()` with custom parameters:
```javascript
const chunks = detectConversationsAdvanced(subtitles, {
boundaryThreshold: 0.6, // Adjust splitting sensitivity
maxChunkSize: 10, // Control maximum chunk size
minChunkSize: 2 // Ensure minimum chunk size
});
```
## Examples
See the following example files for complete demonstrations:
- `example-chunked-response.js` - Basic chunked response demonstration
- `example-multiple-conversations.js` - Multiple conversation handling
- `example-aggressive-splitting.js` - Advanced splitting with custom parameters
## Performance Impact
The chunked response system provides significant performance benefits:
- **Reduced Memory Usage**: Only processes and returns one chunk at a time
- **Faster Response Times**: Smaller payloads result in quicker processing
- **Better Scalability**: Can handle large SRT files without context overflow
- **Improved User Experience**: More responsive chat applications
## Compatibility
This update maintains full backward compatibility:
- Existing code using `detectConversations()` will continue to work
- The function signature remains unchanged
- All existing conversation detection algorithms are preserved
- Additional functionality is provided through new optional parameters