-
Notifications
You must be signed in to change notification settings - Fork 0
implemented handshake #214
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
Open
NikhilGangaram
wants to merge
1
commit into
main
Choose a base branch
from
bff
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -1,102 +1,89 @@ | ||
| import { ChatMessage } from './types'; | ||
|
|
||
| /** | ||
| * Simplified Gemini client for XRP Code Buddy | ||
| * All AI configuration and teaching guidelines are now handled by the backend | ||
| */ | ||
| export class GeminiClient { | ||
| private backendUrl: string; | ||
| private handshakeToken: string | null = null; | ||
|
|
||
| constructor() { | ||
| this.backendUrl = '/api'; | ||
| } | ||
|
|
||
| /** | ||
| * Check if combined documentation has been loaded on the backend for this session | ||
| * Initializes the security handshake with the backend. | ||
| * Must be called before other requests or internally. | ||
| */ | ||
| private async ensureHandshake(): Promise<string> { | ||
| if (this.handshakeToken) return this.handshakeToken; | ||
|
|
||
| try { | ||
| const response = await fetch(`${this.backendUrl}/handshake`); | ||
| if (!response.ok) throw new Error('Handshake failed'); | ||
| const data = await response.json(); | ||
| this.handshakeToken = data.handshake_token; | ||
| return this.handshakeToken!; | ||
| } catch (error) { | ||
| console.error('Critical Security Error: Could not establish handshake', error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| private async getHeaders(): Promise<HeadersInit> { | ||
| const token = await this.ensureHandshake(); | ||
| return { | ||
| 'Content-Type': 'application/json', | ||
| 'X-Handshake-Token': token | ||
| }; | ||
| } | ||
|
|
||
| async getDocsStatus(sessionId: string): Promise<{ loaded: boolean; uri?: string }> { | ||
| try { | ||
| const response = await fetch(`${this.backendUrl}/docs/status`, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| headers: await this.getHeaders(), | ||
| body: JSON.stringify({ session_id: sessionId }) | ||
| }); | ||
| if (!response.ok) { | ||
| throw new Error(`Failed to get docs status: ${response.statusText}`); | ||
| } | ||
| const data = await response.json(); | ||
| return { loaded: !!data.loaded, uri: data.uri }; | ||
| } catch (error) { | ||
| console.warn('Failed to fetch docs status from backend:', error); | ||
| return { loaded: false }; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Request backend to load combined documentation into model context for this session | ||
| */ | ||
| async loadDocs(sessionId: string): Promise<{ success: boolean; status: string; uri?: string }> { | ||
| try { | ||
| const response = await fetch(`${this.backendUrl}/docs/load`, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| headers: await this.getHeaders(), | ||
| body: JSON.stringify({ session_id: sessionId }) | ||
| }); | ||
| if (!response.ok) { | ||
| const err = await response.json().catch(() => ({})); | ||
| throw new Error(err.detail || response.statusText); | ||
| } | ||
| const data = await response.json(); | ||
| return { success: !!data.success, status: data.status, uri: data.uri }; | ||
| } catch (error) { | ||
| console.error('Failed to load docs on backend:', error); | ||
| return { success: false, status: 'error' }; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the model name for display purposes | ||
| * Note: This now fetches from backend to maintain single source of truth | ||
| */ | ||
| async getModelName(): Promise<string> { | ||
| try { | ||
| const response = await fetch(`${this.backendUrl}/model-info`); | ||
| if (response.ok) { | ||
| const data = await response.json(); | ||
| return data.model_name || 'XRPCode Buddy'; | ||
| } | ||
| } catch (error) { | ||
| console.warn('Failed to fetch model name from backend:', error); | ||
| const data = await response.json(); | ||
| return data.model_name || 'XRPCode Buddy'; | ||
| } catch { | ||
| return 'XRPCode Buddy'; | ||
|
Collaborator
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. shouldn't this be XRP Buddy?
Collaborator
Author
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. leaving this to be a separate PR since seems like there are issues with the other one |
||
| } | ||
| return 'XRPCode Buddy'; // Fallback | ||
| } | ||
|
|
||
| /** | ||
| * Clean up a session on the backend | ||
| */ | ||
| async cleanupSession(sessionId: string): Promise<void> { | ||
| try { | ||
| const response = await fetch(`${this.backendUrl}/session/${sessionId}`, { | ||
| method: 'DELETE' | ||
| await fetch(`${this.backendUrl}/session/${sessionId}`, { | ||
| method: 'DELETE', | ||
| headers: await this.getHeaders() | ||
| }); | ||
| if (response.ok) { | ||
| const data = await response.json(); | ||
| console.log(`Session ${sessionId.substring(0, 8)}... cleaned up:`, data.message); | ||
| } else { | ||
| console.warn(`Failed to cleanup session ${sessionId.substring(0, 8)}...`); | ||
| } | ||
| } catch (error) { | ||
| console.warn('Failed to cleanup session on backend:', error); | ||
| console.warn('Cleanup failed', error); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Send a simplified chat request with user message and context | ||
| */ | ||
| async chatWithContext( | ||
| sessionId: string, | ||
| userMessage: string, | ||
|
|
@@ -108,12 +95,6 @@ export class GeminiClient { | |
| signal?: AbortSignal | ||
| ): Promise<string> { | ||
| try { | ||
| // Check if already aborted | ||
| if (signal?.aborted) { | ||
| throw new DOMException('Request was aborted', 'AbortError'); | ||
| } | ||
|
|
||
| // Prepare simplified request payload | ||
| const payload = { | ||
| session_id: sessionId, | ||
| user_message: userMessage, | ||
|
|
@@ -126,63 +107,39 @@ export class GeminiClient { | |
| language: language | ||
| }; | ||
|
|
||
| console.log('Sending simplified chat request to backend'); | ||
|
|
||
| const response = await fetch(`${this.backendUrl}/chat`, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| headers: await this.getHeaders(), | ||
| body: JSON.stringify(payload), | ||
| signal: signal | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const errorData = await response.json(); | ||
| throw new Error(`Chat request failed: ${errorData.detail || response.statusText}`); | ||
| throw new Error(errorData.detail || 'Chat request failed'); | ||
| } | ||
|
|
||
| if (onStream) { | ||
| // Handle streaming response | ||
| return await this.handleStreamingResponse(response, onStream, signal); | ||
| } else { | ||
| // Handle non-streaming response (though streaming is preferred) | ||
| return await this.handleNonStreamingResponse(response); | ||
| } | ||
| return await this.handleStreamingResponse(response, onStream || (() => {}), signal); | ||
| } catch (error) { | ||
| // Re-throw abort errors as-is | ||
| if (error instanceof DOMException && error.name === 'AbortError') { | ||
| throw error; | ||
| } | ||
|
|
||
| console.error('Chat completion error:', error); | ||
| throw new Error(`Failed to get response from backend: ${error instanceof Error ? error.message : 'Unknown error'}`); | ||
| if (error instanceof DOMException && error.name === 'AbortError') throw error; | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Handle streaming response from the simplified chat endpoint | ||
| */ | ||
| private async handleStreamingResponse( | ||
| response: Response, | ||
| onStream: (content: string) => void, | ||
| signal?: AbortSignal | ||
| ): Promise<string> { | ||
| const reader = response.body?.getReader(); | ||
| if (!reader) { | ||
| throw new Error('No response body available'); | ||
| } | ||
| if (!reader) throw new Error('No response body'); | ||
|
|
||
| let fullContent = ''; | ||
| const decoder = new TextDecoder(); | ||
|
|
||
| try { | ||
| while (true) { | ||
| // Check for abortion | ||
| if (signal?.aborted) { | ||
| throw new DOMException('Request was aborted', 'AbortError'); | ||
| } | ||
|
|
||
| if (signal?.aborted) throw new DOMException('Aborted', 'AbortError'); | ||
| const { done, value } = await reader.read(); | ||
| if (done) break; | ||
|
|
||
|
|
@@ -199,100 +156,13 @@ export class GeminiClient { | |
| } else if (data.type === 'error') { | ||
| throw new Error(data.error); | ||
| } | ||
| } catch (parseError) { | ||
| console.warn('Failed to parse streaming data:', parseError); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } finally { | ||
| reader.releaseLock(); | ||
| } | ||
|
|
||
| return fullContent; | ||
| } | ||
|
|
||
| /** | ||
| * Handle non-streaming response (fallback) | ||
| */ | ||
| private async handleNonStreamingResponse(response: Response): Promise<string> { | ||
| const reader = response.body?.getReader(); | ||
| if (!reader) { | ||
| throw new Error('No response body available'); | ||
| } | ||
|
|
||
| let fullContent = ''; | ||
| const decoder = new TextDecoder(); | ||
|
|
||
| try { | ||
| while (true) { | ||
| const { done, value } = await reader.read(); | ||
| if (done) break; | ||
|
|
||
| const chunk = decoder.decode(value, { stream: true }); | ||
| const lines = chunk.split('\n'); | ||
|
|
||
| for (const line of lines) { | ||
| if (line.startsWith('data: ')) { | ||
| try { | ||
| const data = JSON.parse(line.substring(6)); | ||
| if (data.type === 'content') { | ||
| fullContent += data.content; | ||
| } else if (data.type === 'error') { | ||
| throw new Error(data.error); | ||
| } | ||
| } catch (parseError) { | ||
| console.warn('Failed to parse streaming data:', parseError); | ||
| } | ||
| } catch (e) {} | ||
| } | ||
| } | ||
| } | ||
| } finally { | ||
| reader.releaseLock(); | ||
| } | ||
|
|
||
| return fullContent; | ||
| } | ||
|
|
||
| /** | ||
| * Legacy method for backward compatibility | ||
| * @deprecated Use chatWithContext instead | ||
| */ | ||
| async chatCompletion( | ||
| messages: ChatMessage[], | ||
| onStream?: (content: string) => void, | ||
| _contextFile?: unknown, // Ignored - context now handled by backend | ||
| signal?: AbortSignal | ||
| ): Promise<string> { | ||
| console.warn('chatCompletion is deprecated, use chatWithContext instead'); | ||
|
|
||
| if (messages.length === 0) { | ||
| throw new Error('No messages provided'); | ||
| } | ||
|
|
||
| // Generate a temporary session ID for legacy calls | ||
| const randomArray = new Uint32Array(2); | ||
| void (typeof window !== 'undefined' && window.crypto | ||
| ? window.crypto.getRandomValues(randomArray) | ||
| : (typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function' | ||
| ? crypto.getRandomValues(randomArray) | ||
| : (() => { throw new Error('No secure random generator available'); })() | ||
| ) | ||
| ); | ||
| const randomString = Array.from(randomArray).map(n => n.toString(36)).join('').substr(0, 9); | ||
| const tempSessionId = `legacy-${Date.now()}-${randomString}`; | ||
| const userMessage = messages[messages.length - 1].content; | ||
| const conversationHistory = messages.slice(0, -1); | ||
|
|
||
| return this.chatWithContext( | ||
| tempSessionId, | ||
| userMessage, | ||
| conversationHistory, | ||
| '', // No editor context in legacy mode | ||
| '', // No terminal context in legacy mode | ||
| 'en', // Default language for legacy mode | ||
| onStream, | ||
| signal | ||
| ); | ||
| } | ||
| } | ||
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.
How does the backend know that the client is XRPWeb? This call does not pass anything to the backend?
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.
couple of thoughts: