feat: Add streaming token support to contextwindow for real-time LLM … #13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
feat: Add streaming token support to contextwindow for real-time LLM response display while maintaining existing persistence patterns.
Design Principles:
What was implemented
1: Core Interfaces
1.1: Define Streaming Interfaces
File:
contextwindow/contextwindow.goActions:
Add
StreamChunkstruct with fields:Delta string- token/text deltaDone bool- indicates stream completionMetadata map[string]any- provider-specific metadataError error- streaming errorsAdd
StreamCallbackfunction type:type StreamCallback func(chunk StreamChunk) errorAdd
StreamingCapableinterface:CallStreaming(ctx context.Context, inputs []Record, callback StreamCallback) ([]Record, int, error)Add
StreamingOptsCapableinterface:CallStreamingWithOpts(ctx context.Context, inputs []Record, opts CallModelOpts, callback StreamCallback) ([]Record, int, error)Testing:
1.2: Add ContextWindow Streaming Methods
File:
contextwindow/contextwindow.goActions:
Add
CallModelStreamingmethod:Add
CallModelStreamingWithOptsmethod:Testing:
2: Middleware Integration
2.1: Streaming Middleware Interface
File:
contextwindow/contextwindow.goActions:
Extend
Middlewareinterface with optional methods:OnStreamStart(ctx context.Context) errorOnStreamChunk(ctx context.Context, chunk StreamChunk) errorOnStreamComplete(ctx context.Context, fullText string, tokens int) errorMake methods optional via type assertion pattern
Add middleware invocation in CallModelStreaming
Testing:
3: OpenAI Streaming
3.1: Implement OpenAI Streaming
File:
contextwindow/openai_model.goActions:
Implement
CallStreamingmethod:ChatCompletionNewParamswithStream: trueclient.Chat.Completions.NewStreaming(ctx, params)Implement
CallStreamingWithOptsmethod:Handle streaming tool calls:
Testing:
3.2: OpenAI Streaming with Threading
File:
contextwindow/openai_model.goActions:
CallStreamingWithThreadingAndOpts:Testing:
4: Claude Streaming
4.1: Implement Claude Streaming
File:
contextwindow/claude_model.goActions:
Implement
CallStreamingmethod:MessageNewParamswithStream: trueclient.Messages.NewStreaming(ctx, params)message_start- initializecontent_block_delta- accumulate textcontent_block_start- handle tool use startmessage_delta- handle usage updatesmessage_stop- finalizeImplement
CallStreamingWithOpts:Testing:
4.2: Claude Streaming with Threading
File:
contextwindow/claude_model.goActions:
CallStreamingWithThreadingAndOpts:CallStreamingWithOptsinternallyTesting:
5: Gemini Streaming
5.1: Implement Gemini Streaming
File:
contextwindow/gemini_model.goActions:
Implement
CallStreamingmethod:model.GenerateContentStream(ctx, parts...)Implement
CallStreamingWithOpts:Testing:
6: Storage Integration
6.1: Streaming Metadata Storage
File:
contextwindow/storage.goActions:
Add optional
streamedflag to records table:ALTER TABLE records ADD COLUMN streamed BOOLEAN DEFAULT 0Recordstruct:Streamed boolInsertRecordto accept streamed flagUpdate schema initialization:
addColumnIfNotExistsTesting:
6.2: Stream Resume Support
File:
contextwindow/storage.goActions:
partial_response_id TEXTaccumulated_tokens INTTesting:
7: Testing Strategy
7.1: Unit Tests
Files:
contextwindow/contextwindow_test.gocontextwindow/openai_model_test.gocontextwindow/claude_model_test.gocontextwindow/gemini_model_test.goTest Cases:
Actions:
7.2: Integration Tests
File:
contextwindow/streaming_integration_test.goTest Cases:
Actions:
// +build integration8: Examples and Documentation
8.1: Create Streaming Example
File:
contextwindow/_examples/streaming/main.goActions:
Basic streaming example:
Advanced example with progress tracking
Tool calls during streaming example
8.2: Update Documentation
File:
contextwindow/README.mdActions:
File:
contextwindow/contextwindow.go(package doc)Actions:
9: Performance Optimization
9.1: Buffer Management
File:
contextwindow/openai_model.go,claude_model.go,gemini_model.goActions:
Testing (Optional):
9.2: Concurrent Streaming Safety
File:
contextwindow/contextwindow.goActions:
Testing:
10: Edge Cases and Error Handling
10.1: Handle Stream Interruptions
Actions:
Testing:
File Manifest
New Files
contextwindow/_examples/streaming/gemini/main.gocontextwindow/streaming_integration_test.gocontextwindow/concurrent_streaming_test.gocontextwindow/stream_interruption_test.goModified Files
contextwindow/contextwindow.go- Core streaming interfaces and methodscontextwindow/openai_model.go- OpenAI streaming implementationcontextwindow/claude_model.go- Claude streaming implementationcontextwindow/gemini_model.go- Gemini streaming implementationcontextwindow/storage.go- Storage schema updatescontextwindow/contextwindow_test.go- Streaming testscontextwindow/openai_model_test.go- OpenAI streaming testscontextwindow/claude_model_test.go- Claude streaming testscontextwindow/gemini_model_test.go- Gemini streaming testscontextwindow/README.md- Documentation updates@tqbf , what do you think?
Most of the changes are tests, really lots. Eventually, somewhere on the road, you would want to implement streaming. Can't go without it, I think. Well, this is it - streaming :)