Skip to content

Commit 6cf5ca7

Browse files
committed
refactor
1 parent 6aa8c54 commit 6cf5ca7

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/api/providers/claude-code.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class ClaudeCodeHandler extends BaseProvider implements ApiHandler {
3232
let errorOutput = ""
3333
let exitCode: number | null = null
3434

35-
claudeProcess.stdout.on("data", (data) => {
35+
claudeProcess.stdout.on("data", (data: Buffer) => {
3636
const output = data.toString()
3737
const lines = output.split("\n").filter((line: string) => line.trim() !== "")
3838

@@ -41,15 +41,15 @@ export class ClaudeCodeHandler extends BaseProvider implements ApiHandler {
4141
}
4242
})
4343

44-
claudeProcess.stderr.on("data", (data) => {
44+
claudeProcess.stderr.on("data", (data: Buffer) => {
4545
errorOutput += data.toString()
4646
})
4747

48-
claudeProcess.on("close", (code) => {
48+
claudeProcess.on("close", (code: number | null) => {
4949
exitCode = code
5050
})
5151

52-
claudeProcess.on("error", (error) => {
52+
claudeProcess.on("error", (error: Error) => {
5353
processError = error
5454
})
5555

@@ -156,12 +156,24 @@ export class ClaudeCodeHandler extends BaseProvider implements ApiHandler {
156156
}
157157
}
158158

159-
// TODO: Validate instead of parsing
159+
/**
160+
* Attempts to parse a JSON chunk from Claude Code CLI output
161+
* @param data Raw string data from Claude Code CLI
162+
* @returns Parsed ClaudeCodeMessage or null if parsing fails
163+
*/
160164
private attemptParseChunk(data: string): ClaudeCodeMessage | null {
161165
try {
162-
return JSON.parse(data)
166+
const parsed = JSON.parse(data)
167+
// Basic validation to ensure it's a valid Claude Code message
168+
if (typeof parsed === "object" && parsed !== null && "type" in parsed) {
169+
return parsed as ClaudeCodeMessage
170+
}
171+
return null
163172
} catch (error) {
164-
console.error("Error parsing chunk:", error)
173+
// Only log if it looks like it should be JSON (starts with { or [)
174+
if (data.trim().startsWith("{") || data.trim().startsWith("[")) {
175+
console.warn("Failed to parse potential JSON chunk from Claude Code:", error)
176+
}
165177
return null
166178
}
167179
}

0 commit comments

Comments
 (0)