-
Notifications
You must be signed in to change notification settings - Fork 8
Troubleshooting stream issue #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7276fb9
Implement fake streaming for Gemini in AI service
hh54188 75094e4
Update @ant-design/x dependency to version 1.6.1 in package.json and …
hh54188 b90637a
Update App component to render ChatListTroubleshooting instead of Cop…
hh54188 c662b5c
Refactor App component to render CopilotApp and enhance chat streamin…
hh54188 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| /** | ||
| * Example usage of the fake stream method | ||
| * This shows different ways to use the fake streaming for debugging | ||
| */ | ||
|
|
||
| import aiService from './services/aiService.js'; | ||
|
|
||
| async function example1_BasicUsage() { | ||
| console.log('📝 Example 1: Basic Fake Stream Usage\n'); | ||
|
|
||
| const stream = await aiService.streamGeminiFake( | ||
| "What is the capital of France?", | ||
| [], | ||
| "gemini-1.5-flash", | ||
| "example-session-1" | ||
| ); | ||
|
|
||
| console.log('Streaming response:'); | ||
| for await (const chunk of stream) { | ||
| process.stdout.write(chunk.text); | ||
| } | ||
| console.log('\n'); | ||
| } | ||
|
|
||
| async function example2_WithConversationHistory() { | ||
| console.log('📝 Example 2: With Conversation History\n'); | ||
|
|
||
| const conversationHistory = [ | ||
| { role: 'user', content: 'Hello, I need help with JavaScript.' }, | ||
| { role: 'assistant', content: 'I\'d be happy to help you with JavaScript! What specific topic would you like to know about?' } | ||
| ]; | ||
|
|
||
| const stream = await aiService.streamGeminiFake( | ||
| "Can you explain closures?", | ||
| conversationHistory, | ||
| "gemini-1.5-flash", | ||
| "example-session-2" | ||
| ); | ||
|
|
||
| console.log('Streaming response with context:'); | ||
| for await (const chunk of stream) { | ||
| process.stdout.write(chunk.text); | ||
| } | ||
| console.log('\n'); | ||
| } | ||
|
|
||
| async function example3_WithMCPTools() { | ||
| console.log('📝 Example 3: With MCP Tools Enabled\n'); | ||
|
|
||
| const stream = await aiService.streamGeminiFake( | ||
| "Can you help me with a task?", | ||
| [], | ||
| "gemini-1.5-flash", | ||
| "example-session-3", | ||
| [], | ||
| true // Enable MCP tools simulation | ||
| ); | ||
|
|
||
| console.log('Streaming response with potential tool calls:'); | ||
| for await (const chunk of stream) { | ||
| if (chunk.toolCall) { | ||
| console.log(`\n🔧 Tool Call: ${chunk.toolCall.name}`); | ||
| console.log(` Parameters:`, chunk.toolCall.parameters); | ||
| } else { | ||
| process.stdout.write(chunk.text); | ||
| } | ||
| } | ||
| console.log('\n'); | ||
| } | ||
|
|
||
| async function example4_ErrorHandling() { | ||
| console.log('📝 Example 4: Error Handling\n'); | ||
|
|
||
| try { | ||
| const stream = await aiService.streamGeminiFake( | ||
| "This is a test message", | ||
| [], | ||
| "gemini-1.5-flash", | ||
| "example-session-4" | ||
| ); | ||
|
|
||
| console.log('Streaming response:'); | ||
| for await (const chunk of stream) { | ||
| process.stdout.write(chunk.text); | ||
| } | ||
| console.log('\n'); | ||
| } catch (error) { | ||
| console.error('Error occurred:', error.message); | ||
| } | ||
| } | ||
|
|
||
| async function example5_ChunkAnalysis() { | ||
| console.log('📝 Example 5: Detailed Chunk Analysis\n'); | ||
|
|
||
| const stream = await aiService.streamGeminiFake( | ||
| "Analyze this streaming response in detail.", | ||
| [], | ||
| "gemini-1.5-flash", | ||
| "example-session-5" | ||
| ); | ||
|
|
||
| let chunkCount = 0; | ||
| let totalLength = 0; | ||
| const chunks = []; | ||
|
|
||
| console.log('Detailed chunk analysis:'); | ||
| console.log('─'.repeat(60)); | ||
|
|
||
| for await (const chunk of stream) { | ||
| chunkCount++; | ||
| totalLength += chunk.text.length; | ||
| chunks.push({ | ||
| index: chunk.chunkIndex, | ||
| text: chunk.text.trim(), | ||
| length: chunk.text.length, | ||
| timestamp: chunk.timestamp | ||
| }); | ||
|
|
||
| console.log(`Chunk ${chunkCount}: "${chunk.text.trim()}" (${chunk.text.length} chars)`); | ||
| } | ||
|
|
||
| console.log('─'.repeat(60)); | ||
| console.log(`Total chunks: ${chunkCount}`); | ||
| console.log(`Total length: ${totalLength} characters`); | ||
| console.log(`Average chunk size: ${Math.round(totalLength / chunkCount)} characters`); | ||
| console.log(`First chunk: "${chunks[0]?.text}"`); | ||
| console.log(`Last chunk: "${chunks[chunks.length - 1]?.text}"`); | ||
| console.log(); | ||
| } | ||
|
|
||
| async function runAllExamples() { | ||
| console.log('🚀 Running Fake Stream Examples\n'); | ||
| console.log('='.repeat(60)); | ||
|
|
||
| await example1_BasicUsage(); | ||
| console.log('='.repeat(60)); | ||
|
|
||
| await example2_WithConversationHistory(); | ||
| console.log('='.repeat(60)); | ||
|
|
||
| await example3_WithMCPTools(); | ||
| console.log('='.repeat(60)); | ||
|
|
||
| await example4_ErrorHandling(); | ||
| console.log('='.repeat(60)); | ||
|
|
||
| await example5_ChunkAnalysis(); | ||
| console.log('='.repeat(60)); | ||
|
|
||
| console.log('✅ All examples completed!'); | ||
| } | ||
|
|
||
| // Run all examples | ||
| runAllExamples().catch(error => { | ||
| console.error('💥 Examples failed:', error); | ||
| process.exit(1); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -375,6 +375,79 @@ class AIService { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Fake stream Gemini method for debugging streaming responses | ||
| * Simulates chunk-by-chunk streaming with configurable delays and content | ||
| */ | ||
| async streamGeminiFake(message, conversationHistory = [], model = config.gemini.model, sessionId = 'default', files = [], useMCPTools = true) { | ||
| console.log('🔧 Using FAKE Gemini stream for debugging'); | ||
| console.log('Message:', message); | ||
| console.log('Model:', model); | ||
| console.log('Session ID:', sessionId); | ||
| console.log('Files:', files); | ||
| console.log('Use MCP Tools:', useMCPTools); | ||
|
Comment on lines
+383
to
+388
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| // Simulate a realistic AI response that gets streamed | ||
| const fakeResponse = `This is a fake streaming response for debugging purposes. | ||
|
|
||
| Your message was: "${message}" | ||
|
|
||
| I'm simulating how a real Gemini response would be streamed chunk by chunk. Each chunk represents a small piece of the complete response that would normally be generated by the AI model. | ||
|
|
||
| Key features of this fake stream: | ||
| - Configurable delay between chunks | ||
| - Realistic content structure | ||
| - Error simulation capabilities | ||
| - Tool call simulation (if enabled) | ||
|
|
||
| This helps you debug streaming issues without making actual API calls to Gemini.`; | ||
|
|
||
| // Split the response into chunks for streaming | ||
| const words = fakeResponse.split(' '); | ||
| const chunks = []; | ||
|
|
||
| // Create chunks of 2-4 words each | ||
| for (let i = 0; i < words.length; i += 3) { | ||
| const chunkWords = words.slice(i, i + 3); | ||
| chunks.push(chunkWords.join(' ') + ' '); | ||
| } | ||
|
|
||
| // Create an async generator that yields chunks with delays | ||
| const streamGenerator = async function* () { | ||
| for (let i = 0; i < chunks.length; i++) { | ||
| // Simulate network delay (50-150ms per chunk) | ||
| const delay = Math.random() * 100 + 50; | ||
| await new Promise(resolve => setTimeout(resolve, delay)); | ||
|
|
||
| // Yield chunk in the same format as real Gemini | ||
| yield { | ||
| text: chunks[i], | ||
| chunkIndex: i, | ||
| totalChunks: chunks.length, | ||
| timestamp: new Date().toISOString() | ||
| }; | ||
|
|
||
| console.log(`📦 Chunk ${i + 1}/${chunks.length}: "${chunks[i].trim()}"`); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| // Optional: Simulate a tool call at the end | ||
| if (useMCPTools && Math.random() > 0.7) { | ||
| console.log('🔧 Simulating tool call...'); | ||
| await new Promise(resolve => setTimeout(resolve, 200)); | ||
| yield { | ||
| text: '', | ||
| toolCall: { | ||
| name: 'fake_tool', | ||
| parameters: { message: 'This is a simulated tool call' } | ||
| }, | ||
| timestamp: new Date().toISOString() | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| return streamGenerator(); | ||
| } | ||
|
|
||
| /** | ||
| * Stream response from Gemini without chat session reuse | ||
| */ | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a potential division by zero error here if
chunkCountis 0, which would happen if the stream is empty. It's good practice to add a guard to prevent this.