Skip to content

Commit 4b85bfd

Browse files
authored
Merge pull request #15 from grimmerk/code_explainer
feat(code_explainer): implement POC feature
2 parents aa28247 + 0f0c681 commit 4b85bfd

21 files changed

+3985
-1232
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
desktop/electron/out
22

33
electron/embedded.provisionprofile
4+
dev.db
5+
electron/prisma/migrations

README.md

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -57,43 +57,15 @@ Use this to quickly open and switch VS Code projects.
5757
8. [fixed] close the packaged app via cmd+q seems not close SwitchV-server-macos process? check by command: lsof -i:55688. Use ctrl+c to stop development (running via yarn start) is OK. (add close button on tray to help? I guess it is not helping).
5858
1. Solved by electron sending kill server process in the before-quick event handler. Another person suggests to add one more step to handle SIGINT signal on server side, ref https://stackoverflow.com/questions/71523442/child-process-doesnt-exit.
5959

60-
### debugger issue
60+
### Use VS Code Debugger
6161

6262
**To debug main process**
6363

64-
Below is a workaround way to debug main process: Its reference is
65-
https://github.com/electron-userland/electron-forge/issues/1369#issuecomment-1172913835
66-
64+
In VS Code Run and Debug, choose `Electron: Main Process` to launch and debug.
6765

68-
```json
69-
{
70-
"type": "node",
71-
"request": "launch",
72-
"name": "Electron Main",
73-
"runtimeExecutable": "npm",
74-
"runtimeArgs": [
75-
"run",
76-
"start",
77-
],
78-
"cwd": "${workspaceFolder}"
79-
}
80-
```
81-
82-
electron-forge official site only mention how to debug main process but it has a bug
83-
https://github.com/electron-userland/electron-forge/issues/1369
84-
85-
```json
86-
{
87-
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron-forge-vscode-nix",
88-
}
89-
```
90-
91-
Error: Cannot find module '/Users/liamdawson/w/@electron-forge/cli/dist/electron-forge-start'
92-
93-
94-
**To debug render process (workaround way)**
66+
**To debug render process (below set up becomes broken after the timing updating electron 29 and add 2nd window, please directly set up breakpoints in the opened dev tool instead) **
9567

96-
[electron site](https://www.electronjs.org/docs/latest/tutorial/debugging-vscode) and [ms github site](https://github.com/Microsoft/vscode-recipes/tree/master/Electron) both mention below setting to debug main proces
68+
[electron site](https://www.electronjs.org/docs/latest/tutorial/debugging-vscode) and [ms github site](https://github.com/Microsoft/vscode-recipes/tree/master/Electron) both mention below setting to debug main process
9769

9870

9971
```json
@@ -107,7 +79,7 @@ and ms github site has a extra setting for debugging render process. But the abo
10779
1. `yarn start` (to start webpack server part)
10880
2. launch compound "Electron: All" launch setting (from ms github site) to debug main & **render processes**.
10981

110-
The drawback is you will see two copy of SwitchV. And attaching render process takes a little time (e.g. only stop at some breakpoints after a while/refresh).
82+
The drawback is you will see two copy of SwitchV. And attaching render process takes a little time (e.g. only stop at some breakpoints after a while/refresh).~~
11183

11284
## notes about packaging a macOS app
11385

electron/.vscode/launch.json

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@
22
"version": "0.2.0",
33
"configurations": [
44
{
5-
"name": "Node.js - Debug Current File",
5+
"name": "Electron: Main Process",
66
"type": "node",
77
"request": "launch",
8-
"program": "${file}"
8+
"cwd": "${workspaceFolder}",
9+
"runtimeExecutable": "yarn",
10+
"windows": {
11+
"runtimeExecutable": "yarn.cmd"
12+
},
13+
"runtimeArgs": [
14+
"start"
15+
],
16+
"outputCapture": "std",
917
},
1018
{
19+
"name": "Debug Current File",
1120
"type": "node",
1221
"request": "launch",
13-
"name": "Electron Main",
14-
"runtimeExecutable": "npm",
15-
"runtimeArgs": [
16-
"run",
17-
"start",
18-
"--remote-debugging-port=9223"
19-
],
20-
"cwd": "${workspaceFolder}"
22+
"program": "${file}",
23+
"skipFiles": [
24+
"<node_internals>/**"
25+
]
2126
},
2227
{
2328
"type": "node",

electron/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# SwitchV Electron App
2+
3+
## Code Explainer Feature
4+
5+
SwitchV now includes a Code Explainer feature powered by Anthropic's Claude AI. This feature allows you to get detailed explanations of code snippets with a simple keyboard shortcut.
6+
7+
### Setup
8+
9+
1. Make sure you have an Anthropic API key. You can get one from [Anthropic's website](https://console.anthropic.com/).
10+
11+
2. Add your API key to the `.env` file in the `electron` directory:
12+
```
13+
ANTHROPIC_API_KEY=your_api_key_here
14+
```
15+
16+
3. Install dependencies:
17+
```
18+
yarn
19+
```
20+
21+
### Usage
22+
23+
1. Start the application:
24+
```
25+
yarn start
26+
```
27+
28+
2. Select the code you want to explain in any editor.
29+
30+
3. Press `Cmd+C` to copy the selected code to your clipboard.
31+
32+
4. Press `Ctrl+Cmd+E` to open the Code Explainer window, which will:
33+
- Create a floating window with the code from your clipboard
34+
- Generate an explanation using Anthropic Claude
35+
36+
5. The window will display your code and start generating an explanation.
37+
38+
> Note: For a smooth demonstration workflow, make sure to copy your code to the clipboard before triggering the Code Explainer with Ctrl+Cmd+E.
39+
40+
### How It Works
41+
42+
- The Code Explainer uses Claude API to generate explanations.
43+
- The API request is made from the main Electron process (not the renderer) for security.
44+
- Explanations are streamed in real-time for a better user experience.
45+
- The UI is a semi-transparent floating window that can be closed when not needed.
46+
47+
### Features
48+
49+
- Syntax highlighting for various programming languages
50+
- Streaming explanation that updates in real-time
51+
- Automatic language detection
52+
- Error handling
53+
54+
### Development Notes
55+
56+
- API keys are loaded from `.env` using dotenv
57+
- The main API communication is in `AnthropicService.ts`
58+
- The UI component is in `CodeExplainer.tsx`

electron/forge.config.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import { WebpackPlugin } from '@electron-forge/plugin-webpack';
88
import { mainConfig } from './webpack.main.config';
99
import { rendererConfig } from './webpack.renderer.config';
1010

11-
const fs = require('fs');
11+
import * as fs from 'fs';
1212

13+
// Ensure compatibility with Electron Forge v7
1314
const config: ForgeConfig = {
1415
hooks: {
1516
generateAssets: async () => {
@@ -65,6 +66,14 @@ const config: ForgeConfig = {
6566
js: './src/preload.ts',
6667
},
6768
},
69+
{
70+
html: './src/explainer.html',
71+
js: './src/explainer-renderer.ts',
72+
name: 'explainer_window',
73+
preload: {
74+
js: './src/preload.ts',
75+
},
76+
},
6877
],
6978
},
7079
}),

electron/package.json

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,63 @@
66
"main": ".webpack/main",
77
"scripts": {
88
"db": "prisma migrate dev",
9-
"start": "NODE_ENV=development electron-forge start",
10-
"dev": "EMBEDSERVER=1 NODE_ENV=development electron-forge start",
9+
"start": "cross-env NODE_ENV=development electron-forge start",
10+
"dev": "cross-env EMBEDSERVER=1 NODE_ENV=development electron-forge start",
11+
"start:explainer": "cross-env NODE_ENV=development electron-forge start",
1112
"package": "electron-forge package",
12-
"package_mas": "npm run prebuild && BUILD_TYPE=prod electron-forge package --platform=mas && npm run postbuild",
13+
"package_mas": "npm run prebuild && cross-env BUILD_TYPE=prod electron-forge package --platform=darwin -- --mas && npm run postbuild",
1314
"prebuild": "sh prebuild.sh",
1415
"postbuild": "sh postbuild.sh",
15-
"make": "npm run prebuild && BUILD_TYPE=prod electron-forge make && npm run postbuild",
16-
"make_mas": "npm run prebuild && BUILD_TYPE=prod electron-forge make --platform=mas && npm run postbuild",
17-
"make:staging": "npm run prebuild && BUILD_TYPE=staging electron-forge make && npm run postbuild",
16+
"make": "npm run prebuild && cross-env BUILD_TYPE=prod electron-forge make && npm run postbuild",
17+
"make_mas": "npm run prebuild && cross-env BUILD_TYPE=prod electron-forge make --platform=darwin -- --mas && npm run postbuild",
18+
"make:staging": "npm run prebuild && cross-env BUILD_TYPE=staging electron-forge make && npm run postbuild",
1819
"make-x86": "electron-forge make --arch=x64 --platform=darwin",
1920
"publish": "electron-forge publish",
2021
"lint": "eslint --ext .ts,.tsx .",
21-
"db:migrate": "prisma migrate dev"
22+
"db:migrate": "prisma migrate dev",
23+
"watch": "webpack --watch"
24+
2225
},
2326
"keywords": [],
2427
"author": {
2528
"name": "Grimmer",
26-
"email": "grimmer0125@gmail.com"
29+
"email": "k@grimmer.com"
2730
},
2831
"license": "MIT",
29-
"config": {
30-
"forge": "./config.forge.js"
31-
},
3232
"devDependencies": {
33-
"@electron-forge/cli": "^6.0.4",
34-
"@electron-forge/maker-deb": "^6.0.4",
35-
"@electron-forge/maker-rpm": "^6.0.4",
36-
"@electron-forge/maker-squirrel": "^6.0.4",
37-
"@electron-forge/maker-zip": "^6.0.4",
38-
"@electron-forge/plugin-webpack": "^6.0.4",
33+
"@electron-forge/cli": "^7.2.0",
34+
"@electron-forge/maker-deb": "^7.2.0",
35+
"@electron-forge/maker-rpm": "^7.2.0",
36+
"@electron-forge/maker-squirrel": "^7.2.0",
37+
"@electron-forge/maker-zip": "^7.2.0",
38+
"@electron-forge/plugin-webpack": "^7.2.0",
3939
"@types/express": "^4.17.17",
40-
"@types/react": "^18.0.15",
41-
"@types/react-dom": "^18.0.6",
40+
"@types/react": "^18.2.51",
41+
"@types/react-dom": "^18.2.18",
4242
"@types/react-highlight-words": "^0.16.4",
43-
"@typescript-eslint/eslint-plugin": "^5.0.0",
44-
"@typescript-eslint/parser": "^5.0.0",
43+
"@types/react-syntax-highlighter": "^15.5.13",
44+
"@typescript-eslint/eslint-plugin": "^6.21.0",
45+
"@typescript-eslint/parser": "^6.21.0",
4546
"@vercel/webpack-asset-relocator-loader": "^1.7.3",
4647
"copy-webpack-plugin": "^11.0.0",
48+
"cross-env": "^7.0.3",
4749
"css-loader": "^6.0.0",
48-
"electron": "^22.0.0",
49-
"eslint": "^8.0.1",
50-
"eslint-config-prettier": "^8.5.0",
51-
"eslint-plugin-import": "^2.25.0",
52-
"eslint-plugin-prettier": "^4.2.1",
53-
"fork-ts-checker-webpack-plugin": "^7.2.14",
50+
"electron": "^29.4.6",
51+
"eslint": "^8.56.0",
52+
"eslint-config-prettier": "^9.1.0",
53+
"eslint-plugin-import": "^2.29.1",
54+
"eslint-plugin-prettier": "^5.1.3",
55+
"fork-ts-checker-webpack-plugin": "^9.0.2",
5456
"node-loader": "^2.0.0",
55-
"prettier": "^2.8.1",
56-
"style-loader": "^3.0.0",
57-
"ts-loader": "^9.2.2",
58-
"ts-node": "^10.9.1",
59-
"typescript": "~4.5.4",
57+
"prettier": "^3.2.5",
58+
"style-loader": "^3.3.4",
59+
"ts-loader": "^9.5.1",
60+
"ts-node": "^10.9.2",
61+
"typescript": "~5.3.3",
6062
"webpack-node-externals": "^3.0.0"
6163
},
6264
"dependencies": {
65+
"@anthropic-ai/sdk": "^0.17.1",
6366
"@atlaskit/button": "^16.5.3",
6467
"@atlaskit/dropdown-menu": "^11.5.10",
6568
"@atlaskit/popup": "^1.5.4",
@@ -70,13 +73,18 @@
7073
"@prisma/client": "^4.16.1",
7174
"class-transformer": "^0.5.1",
7275
"class-validator": "^0.14.0",
76+
"dotenv": "^16.4.5",
7377
"electron-settings": "^4.0.2",
7478
"electron-squirrel-startup": "^1.0.0",
7579
"prisma": "^4.16.1",
7680
"react": "^18.2.0",
7781
"react-dom": "^18.2.0",
7882
"react-highlight-words": "^0.18.0",
83+
"react-markdown": "^8.0.5",
7984
"react-select": "^5.4.0",
80-
"reflect-metadata": "^0.1.13"
85+
"react-syntax-highlighter": "^15.5.0",
86+
"reflect-metadata": "^0.1.13",
87+
"remark-gfm": "^3.0.1",
88+
"rxjs": "^7.8.1"
8189
}
82-
}
90+
}

0 commit comments

Comments
 (0)