refactor: Remove multi-JSON parsing workaround from TypeScript

The complex JSON parsing logic that handled multiple JSON objects was only
needed because ApplicationFinder was incorrectly outputting errors directly.
Now that the root cause is fixed (ApplicationFinder only throws errors),
we can simplify the TypeScript code to just parse single JSON responses.

This makes the codebase cleaner and error handling more predictable.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Peter Steinberger 2025-06-08 07:10:34 +01:00
parent ed1860d546
commit f72799803b
2 changed files with 19 additions and 43 deletions

View file

@ -7,7 +7,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- **Window count display optimization**: Single-window apps no longer show "Windows: 1" in list output ([#6](https://github.com/steipete/Peekaboo/pull/6))
- Reduces visual clutter for the common case of apps with only one window
- Apps with 0, 2, or more windows still display the count
- Improves readability of the `list apps` command output
### Fixed
- **Input validation improvements**:
- Whitespace is now trimmed from `app_target` parameter (e.g., `" Spotify "` now works correctly)
- Format parameter is now case-insensitive (`"PNG"` and `"png"` both work)
- Added support for `"jpeg"` as an alias for `"jpg"` format
- **Edge case handling**:
- Float and hex screen indices now parse correctly (e.g., `screen:1.5``screen:1`, `screen:0x1``screen:0`)
- Special filesystem characters (|, :, *) in filenames are preserved as-is
- Empty questions to analyze tool are handled gracefully (analysis is skipped)
- **Swift error handling improvements**:
- Fixed CaptureError enum compatibility issues in tests
- Improved error messages with better context for ApplicationFinder errors
- Fixed overly broad permission error detection that incorrectly reported file I/O errors as screen recording permission issues
- File permission errors (e.g., writing to `/System/`) now correctly report as `FILE_IO_ERROR`
- Directory not found errors provide clear messages about missing parent directories
@ -21,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Error messages include the original system error descriptions
- Fixed duplicate error output when ApplicationFinder throws errors
- Enhanced error details for app not found errors to include list of available applications
- Removed complex multi-JSON parsing logic from TypeScript that was only needed due to duplicate error output
## [1.0.0-beta.19] - 2025-06-08

View file

@ -178,50 +178,8 @@ export async function executeSwiftCli(
}
try {
// Handle multiple JSON objects by taking the first valid one
let jsonResponse: SwiftCliResponse;
const trimmedOutput = stdout.trim();
// Try to parse as single JSON first
try {
jsonResponse = JSON.parse(trimmedOutput);
} catch (firstParseError) {
// If that fails, try to extract the first complete JSON object
// This handles cases where Swift CLI outputs multiple JSON objects
const lines = trimmedOutput.split("\n");
let braceCount = 0;
let firstJsonEnd = -1;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
for (let j = 0; j < line.length; j++) {
if (line[j] === "{") {
braceCount++;
} else if (line[j] === "}") {
braceCount--;
}
if (braceCount === 0 && line[j] === "}") {
firstJsonEnd = i;
break;
}
}
if (firstJsonEnd !== -1) {
break;
}
}
if (firstJsonEnd !== -1) {
const firstJsonLines = lines.slice(0, firstJsonEnd + 1);
const firstJsonStr = firstJsonLines.join("\n");
jsonResponse = JSON.parse(firstJsonStr);
logger.debug("Extracted first JSON object from multi-object output");
} else {
throw firstParseError; // Re-throw original error if extraction fails
}
}
const response = jsonResponse;
const response: SwiftCliResponse = JSON.parse(trimmedOutput);
// Log debug messages from Swift CLI
if (response.debug_logs && Array.isArray(response.debug_logs)) {