Conversation
1 similar comment
Greptile SummaryRefactored both main.py improvements:
server.js improvements:
Critical issues found:
Confidence Score: 2/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Presto as Presto Device
participant Server as Node.js Server
participant ElevenLabs as ElevenLabs API
participant Mic as Microphone
participant Speaker as Speaker
Note over Presto: Boot & WiFi Connect
Presto->>Server: GET /status
Server-->>Presto: {sessionState: "idle"}
Note over Presto: Display IDLE
Note over Presto: User Taps Screen
Presto->>Server: POST /start
Server->>ElevenLabs: WebSocket Connect
ElevenLabs-->>Server: Connection Open
Server->>Mic: Start Recording (SoX)
Server->>Speaker: Initialize Speaker
Server-->>Presto: {ok: true, sessionState: "started"}
Note over Presto: Display ACTIVE
loop Audio Processing
Mic->>Server: Audio Chunk (PCM)
Server->>Server: Calculate RMS (Volume)
alt Volume > Threshold
Server->>ElevenLabs: Send Audio (Base64)
else Volume <= Threshold
Note over Server: Drop (Echo/Silence)
end
ElevenLabs->>Server: Audio Response (Base64)
Server->>Speaker: Play Audio
Note over Server: Set agentIsSpeaking=true
end
Note over Presto: User Taps Again
Presto->>Server: POST /stop
Server->>ElevenLabs: Close WebSocket
Server->>Mic: Stop Recording
Server->>Speaker: End Playback
Server-->>Presto: {ok: true, sessionState: "idle"}
Note over Presto: Display IDLE
|
| while not connect_wifi(): | ||
| draw_ui("offline", "WiFi Failed. Retrying...") | ||
| time.sleep(3) |
There was a problem hiding this comment.
logic: infinite loop with no escape mechanism - if WiFi credentials are wrong or router is down, device will be stuck forever with no way to recover without physical reset
| while not connect_wifi(): | |
| draw_ui("offline", "WiFi Failed. Retrying...") | |
| time.sleep(3) | |
| # 2. Connect with Retry (max attempts) | |
| retry_count = 0 | |
| max_retries = 5 | |
| while not connect_wifi() and retry_count < max_retries: | |
| retry_count += 1 | |
| draw_ui("offline", f"WiFi Failed. Retry {retry_count}/{max_retries}...") | |
| time.sleep(3) | |
| if retry_count >= max_retries: | |
| draw_ui("offline", "WiFi Failed. Check credentials.") | |
| return |
Prompt To Fix With AI
This is a comment left during a code review.
Path: main.py
Line: 111:113
Comment:
**logic:** infinite loop with no escape mechanism - if WiFi credentials are wrong or router is down, device will be stuck forever with no way to recover without physical reset
```suggestion
# 2. Connect with Retry (max attempts)
retry_count = 0
max_retries = 5
while not connect_wifi() and retry_count < max_retries:
retry_count += 1
draw_ui("offline", f"WiFi Failed. Retry {retry_count}/{max_retries}...")
time.sleep(3)
if retry_count >= max_retries:
draw_ui("offline", "WiFi Failed. Check credentials.")
return
```
How can I resolve this? If you propose a fix, please make it concise.| cleanup(); | ||
| }); | ||
|
|
||
| res.json({ ok: true, sessionState: "started" }); |
There was a problem hiding this comment.
logic: response sent inside async 'open' event handler - the HTTP request will hang because res.json() is called after WebSocket opens, but the /start route returns immediately without waiting. Is the client expecting an immediate response or should it wait for WebSocket connection?
Prompt To Fix With AI
This is a comment left during a code review.
Path: server.js
Line: 158:158
Comment:
**logic:** response sent inside async 'open' event handler - the HTTP request will hang because `res.json()` is called after WebSocket opens, but the `/start` route returns immediately without waiting. Is the client expecting an immediate response or should it wait for WebSocket connection?
How can I resolve this? If you propose a fix, please make it concise.| } catch (audioErr) { | ||
| console.error("Audio initialization failed:", audioErr); | ||
| cleanup(); | ||
| // Since we already responded (maybe), we can't easily res.json if we were inside an async callback, | ||
| // but here we are inside the sync 'open' handler, so the response hasn't been sent yet? | ||
| // Actually 'open' is async relative to the request. The request is pending. | ||
| // Wait, 'res.json' is called at the end of 'open'. | ||
| // We should ensure we don't double respond. | ||
| // In this flow, we haven't responded yet. | ||
| } |
There was a problem hiding this comment.
logic: error handling missing response - if audio initialization fails inside the 'open' handler, cleanup() is called but no response is sent to the client, causing the request to hang indefinitely
Prompt To Fix With AI
This is a comment left during a code review.
Path: server.js
Line: 160:169
Comment:
**logic:** error handling missing response - if audio initialization fails inside the 'open' handler, `cleanup()` is called but no response is sent to the client, causing the request to hang indefinitely
How can I resolve this? If you propose a fix, please make it concise.| cleanup(); | ||
| sessionState = "idle"; | ||
| res.json({ ok: true, sessionState }); | ||
| res.json({ ok: true, sessionState: state.sessionStatus }); |
There was a problem hiding this comment.
logic: race condition - sessionStatus set to "idle" inside cleanup() (line 71) which runs asynchronously when WebSocket closes, but response returns state.sessionStatus immediately, potentially returning stale "listening" or "error" status
Prompt To Fix With AI
This is a comment left during a code review.
Path: server.js
Line: 215:215
Comment:
**logic:** race condition - `sessionStatus` set to "idle" inside `cleanup()` (line 71) which runs asynchronously when WebSocket closes, but response returns `state.sessionStatus` immediately, potentially returning stale "listening" or "error" status
How can I resolve this? If you propose a fix, please make it concise.| state.recording.stream().on("error", (err) => { | ||
| console.error("Recording error:", err); | ||
| cleanup(); | ||
| }); |
There was a problem hiding this comment.
logic: no error handler for speaker stream - if speaker fails (e.g., audio device disconnected), the error will crash the process since there's no .on('error') handler attached to the speaker stream
Prompt To Fix With AI
This is a comment left during a code review.
Path: server.js
Line: 153:156
Comment:
**logic:** no error handler for speaker stream - if speaker fails (e.g., audio device disconnected), the error will crash the process since there's no `.on('error')` handler attached to the speaker stream
How can I resolve this? If you propose a fix, please make it concise.
@greptileai