Skip to content

Commit 6ef3ce1

Browse files
Merge pull request #45 from georgeistes/ui-ux-patch-3
Change WS port and update JSON parsing
2 parents 9da26df + 642e9b9 commit 6ef3ce1

File tree

5 files changed

+42
-30
lines changed

5 files changed

+42
-30
lines changed

live2d-container/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const path = require('path');
1515
const USE_WEBSOCKET = app.commandLine.getSwitchValue("use-websocket") === "false" ? false : true;
1616

1717
let mainWindow;
18-
const WS_PORT = 3000;
18+
const WS_PORT = 54321;
1919
let ws = null;
2020

2121
function showWelcomeMessage() {

src/agents/cheerleader_agent.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ import { CommentHandler } from './action_handlers/comment_handler';
1313
import { ExplainHandler } from './action_handlers/explain_handler';
1414
import { HighlightHandler } from './action_handlers/highlight_handler';
1515

16-
/**
17-
* Abstract base class for all Cheerleader agents.
18-
* It encapsulates common functionality for text and voice interactions.
19-
*
20-
*/
2116
/**
2217
* An abstract base class representing a Cheerleader Agent that provides interactive code assistance.
2318
* This class handles both voice and text-based interactions, manages action handlers, and processes AI responses.
@@ -230,34 +225,44 @@ export abstract class CheerleaderAgent {
230225
* @param response AI response string.
231226
*/
232227
private parseResponse(response: string): BasicCheerleaderAction[] {
233-
// Find start of JSON block
234-
const startIndex = response.indexOf("```json");
235-
if (startIndex === -1) {
236-
console.debug("No JSON block found in response");
237-
return [{
238-
action: "conversation",
239-
content: response.trim()
240-
}];
241-
}
228+
try {
229+
// Sanitize input
230+
const sanitizedResponse = response.trim();
231+
232+
// Find JSON block boundaries
233+
const jsonBlockMatch = sanitizedResponse.match(/```json\s*([\s\S]*?)\s*```/);
234+
235+
// If the response fails or parsing fails, do not return the block as response
236+
// but throw an error instead. This is because it will be wasting TTS token
237+
// and the user will not be able to see the error message.
238+
if (!jsonBlockMatch) {
239+
throw new Error("No JSON block found in response");
240+
}
242241

243-
// Remove markdown formatting
244-
const jsonStart = response.indexOf("[", startIndex);
245-
let jsonContent = response.substring(jsonStart);
246-
const blockEnd = jsonContent.indexOf("```");
247-
if (blockEnd !== -1) {
248-
jsonContent = jsonContent.substring(0, blockEnd);
249-
}
242+
// Extract JSON content from the matched block
243+
const jsonContent = jsonBlockMatch[1].trim();
244+
245+
if (!jsonContent) {
246+
throw new Error("Empty JSON block found");
247+
}
250248

251-
// Parse JSON
252-
try {
249+
// Parse JSON with validation
253250
const actions = JSON.parse(jsonContent);
251+
252+
// Validate that actions is an array
253+
if (!Array.isArray(actions)) {
254+
throw new Error("Parsed JSON is not an array");
255+
}
256+
254257
console.debug("Successfully parsed JSON array:", actions);
255258
return actions;
259+
256260
} catch (e) {
257-
console.debug("JSON parse error:", e);
261+
console.error("JSON parse error:", e);
262+
// Return the original response as conversation if parsing fails
258263
return [{
259264
action: "conversation",
260-
content: response.trim()
265+
content: "Oops! There was an error handling the AI response."
261266
}];
262267
}
263268
}

src/agents/inline_chat_agent.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { TextEditor } from 'vscode';
22
import { CheerleaderAgent } from './cheerleader_agent';
33

4-
const INLINE_CHAT_PROMPT = `Guide users to understand code rather than solving problems directly. YOUR RESPONSE MUST BE A VALID JSON ARRAY using this format:
4+
const INLINE_CHAT_PROMPT = `Guide users to understand code rather than solving problems directly. YOUR RESPONSE MUST BE A VALID JSON ARRAY of the following actions,
5+
but you do NOT need to use all the actions, only use the ones that are relevant and do not include the actions if they do not help solve the problem.
6+
For example, if the user asks a general question, you only need to use the "conversation" action.
7+
58
\`\`\`json
69
[
710
{
@@ -27,10 +30,12 @@ const INLINE_CHAT_PROMPT = `Guide users to understand code rather than solving p
2730
}
2831
]
2932
\`\`\`
33+
3034
If you need to include diagrams in your explanation, use the following syntax:
3135
\`\`\`mermaid
3236
<mermaid_diagram_here>
3337
\`\`\`
38+
3439
Always start with a conversational response, then add necessary actions. Focus on teaching patterns and concepts.`;
3540

3641
/**

src/agents/rubber_duck_agent.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ When the problem is solved, respond with:
4242
`;
4343

4444
/**
45-
* RubberDuckAgent specializes in debugging through conversation.
46-
* It maintains a continuous dialogue until the problem is solved.
45+
* "You must stay vigilant against the temptation of vibe coding by Daisy Duck",
46+
* commanded George to his followers. "For this reason, I shall gift you with a
47+
* rubber duck, a symbol of our commitment to the art of debugging."
48+
* -- The Georgeiste Manifesto, Chapter 3, Verse 5
4749
*/
4850
export class RubberDuckAgent extends CheerleaderAgent {
4951
private isSolved: boolean = false;

src/services/websocket_service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class WebSocketService {
66
private static instance: WebSocketService;
77
private wss: WebSocket.Server | null = null;
88
private ws: WebSocket.WebSocket | null = null;
9-
private readonly WS_PORT = 3000;
9+
private readonly WS_PORT = 54321;
1010

1111
private constructor() {}
1212

0 commit comments

Comments
 (0)