pocket-tutor 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +185 -2
- package/bin/pocket-tutor.js +127 -0
- package/config.example.json +12 -0
- package/docs/CLIP_RULES.md +38 -0
- package/package.json +74 -4
- package/scripts/build-ui.mjs +25 -0
- package/scripts/clip-judge.js +106 -0
- package/scripts/doctor.js +58 -0
- package/scripts/setup-visuals.js +27 -0
- package/scripts/visual_scene.py +279 -0
- package/src/astryx-themes.js +18 -0
- package/src/browser.js +38 -0
- package/src/config.js +84 -0
- package/src/graph.js +114 -0
- package/src/helper.ps1 +67 -0
- package/src/main.js +526 -0
- package/src/model.js +283 -0
- package/src/native.js +50 -0
- package/src/place.js +29 -0
- package/src/preload.js +17 -0
- package/src/privacy.js +40 -0
- package/src/project.js +252 -0
- package/src/search.js +111 -0
- package/src/setup.js +60 -0
- package/src/themes/lens/lens.css +1165 -0
- package/src/themes/lens/lens.css.d.ts +9 -0
- package/src/themes/lens/lens.d.ts +14 -0
- package/src/themes/lens/lens.fonts.css +2 -0
- package/src/themes/lens/lens.js +627 -0
- package/src/themes/lens/lens.variants.d.ts +22 -0
- package/src/themes/lens/lensTheme.doc.mjs +10 -0
- package/src/themes/lens/lensTheme.ts +2 -0
- package/src/tools.js +102 -0
- package/src/ui/app.jsx +268 -0
- package/src/ui/dist/assets/KaTeX_AMS-Regular-CYEKBG2K.woff +0 -0
- package/src/ui/dist/assets/KaTeX_AMS-Regular-JKX5W2C4.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_AMS-Regular-U6PRYMIZ.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Caligraphic-Bold-5QL5CMTE.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Caligraphic-Bold-WZ3QSGD3.woff +0 -0
- package/src/ui/dist/assets/KaTeX_Caligraphic-Bold-ZTS3R3HK.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_Caligraphic-Regular-3LKEU76G.woff +0 -0
- package/src/ui/dist/assets/KaTeX_Caligraphic-Regular-A7XRTZ5Q.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_Caligraphic-Regular-KX5MEWCF.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Fraktur-Bold-2QVFK6NQ.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Fraktur-Bold-T4SWXBMT.woff +0 -0
- package/src/ui/dist/assets/KaTeX_Fraktur-Bold-WGHVTYOR.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_Fraktur-Regular-2PEIFJSJ.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Fraktur-Regular-5U4OPH2X.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_Fraktur-Regular-PQMHCIK6.woff +0 -0
- package/src/ui/dist/assets/KaTeX_Main-Bold-2GA4IZIN.woff +0 -0
- package/src/ui/dist/assets/KaTeX_Main-Bold-W5FBVCZM.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_Main-Bold-YP5VVQRP.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Main-BoldItalic-4P4C7HJH.woff +0 -0
- package/src/ui/dist/assets/KaTeX_Main-BoldItalic-N4V3DX7S.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Main-BoldItalic-ODMLBJJQ.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_Main-Italic-I43T2HSR.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_Main-Italic-RELBIK7M.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Main-Italic-SASNQFN2.woff +0 -0
- package/src/ui/dist/assets/KaTeX_Main-Regular-ARRPAO67.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Main-Regular-P5I74A2A.woff +0 -0
- package/src/ui/dist/assets/KaTeX_Main-Regular-W74P5G27.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_Math-BoldItalic-6EBV3DK5.woff +0 -0
- package/src/ui/dist/assets/KaTeX_Math-BoldItalic-K4WTGH3J.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Math-BoldItalic-VB447A4D.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_Math-Italic-6KGCHLFN.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Math-Italic-KKK3USB2.woff +0 -0
- package/src/ui/dist/assets/KaTeX_Math-Italic-SON4MRCA.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_SansSerif-Bold-RRNVJFFW.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_SansSerif-Bold-STQ6RXC7.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_SansSerif-Bold-X5M5EMOD.woff +0 -0
- package/src/ui/dist/assets/KaTeX_SansSerif-Italic-HMPFTM52.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_SansSerif-Italic-PSN4QKYX.woff +0 -0
- package/src/ui/dist/assets/KaTeX_SansSerif-Italic-WTBAZBGY.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_SansSerif-Regular-2TL3USAE.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_SansSerif-Regular-OQCII6EP.woff +0 -0
- package/src/ui/dist/assets/KaTeX_SansSerif-Regular-XIQ62X4E.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Script-Regular-72OLXYNA.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_Script-Regular-A5IFOEBS.woff +0 -0
- package/src/ui/dist/assets/KaTeX_Script-Regular-APUWIHLP.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Size1-Regular-4HRHTS65.woff +0 -0
- package/src/ui/dist/assets/KaTeX_Size1-Regular-5LRUTBFT.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Size1-Regular-7K6AASVL.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_Size2-Regular-222HN3GT.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_Size2-Regular-K5ZHAIS6.woff +0 -0
- package/src/ui/dist/assets/KaTeX_Size2-Regular-LELKET5D.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Size3-Regular-TLFPAHDE.woff +0 -0
- package/src/ui/dist/assets/KaTeX_Size3-Regular-UFCO6WCA.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_Size3-Regular-WQRQ47UD.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Size4-Regular-7PGNVPQK.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_Size4-Regular-CDMV7U5C.woff2 +0 -0
- package/src/ui/dist/assets/KaTeX_Size4-Regular-PKMWZHNC.woff +0 -0
- package/src/ui/dist/assets/KaTeX_Typewriter-Regular-3F5K6SQ6.ttf +0 -0
- package/src/ui/dist/assets/KaTeX_Typewriter-Regular-MJMFSK64.woff +0 -0
- package/src/ui/dist/assets/KaTeX_Typewriter-Regular-VBYJ4NRC.woff2 +0 -0
- package/src/ui/dist/assets/figtree-latin-400-normal-PORFSHKN.woff2 +0 -0
- package/src/ui/dist/assets/figtree-latin-500-normal-AHYR5JKA.woff2 +0 -0
- package/src/ui/dist/assets/figtree-latin-600-normal-5C6Z7QEI.woff2 +0 -0
- package/src/ui/dist/assets/figtree-latin-700-normal-M33EANAW.woff2 +0 -0
- package/src/ui/dist/assets/figtree-latin-ext-400-normal-CK4ZBFIX.woff2 +0 -0
- package/src/ui/dist/assets/figtree-latin-ext-500-normal-M4H6A6MQ.woff2 +0 -0
- package/src/ui/dist/assets/figtree-latin-ext-600-normal-KUZFNAIE.woff2 +0 -0
- package/src/ui/dist/assets/figtree-latin-ext-700-normal-U3DQVDYX.woff2 +0 -0
- package/src/ui/dist/popup.css +1 -0
- package/src/ui/dist/popup.js +546 -0
- package/src/ui/popup.html +13 -0
- package/src/ui/state.js +89 -0
- package/src/visual.js +229 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pocket Tutor contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,186 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Pocket Tutor
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An AI search box for your whole laptop. Press **Ctrl+Alt+Space** in any Windows app. If you have text selected, a small box opens at your cursor and explains it in plain English. If nothing is selected, the box opens as a question line, and you can ask anything. It works on a word in a PDF, an error message in a terminal, a line of code you didn't write, or a formula on a lecture slide. It is built for people who code with AI help and keep running into things they don't understand.
|
|
4
|
+
|
|
5
|
+
It talks to any OpenAI-compatible model: OpenAI, OpenRouter, a free Nemotron key from Vercel, or a model on your own machine through Ollama (no key, and nothing leaves your laptop). There is also a terminal command, `pocket-tutor explain`, that works on macOS and Linux too.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
You need [Node.js 24 or newer](https://nodejs.org). The tray app needs Windows 10 or 11.
|
|
10
|
+
|
|
11
|
+
```powershell
|
|
12
|
+
npm install -g pocket-tutor
|
|
13
|
+
pocket-tutor doctor # checks your model, web search and git
|
|
14
|
+
pocket-tutor # starts the tray app
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Or run it from a clone:
|
|
18
|
+
|
|
19
|
+
```powershell
|
|
20
|
+
git clone https://github.com/advaithsarva/pocket-tutor.git
|
|
21
|
+
cd pocket-tutor
|
|
22
|
+
npm install
|
|
23
|
+
npm start
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The package installs Electron, so the first install downloads about 100 MB.
|
|
27
|
+
|
|
28
|
+
## Pick a model
|
|
29
|
+
|
|
30
|
+
Set one of these, then run `pocket-tutor doctor`. Use `setx` if you want a variable to survive a restart.
|
|
31
|
+
|
|
32
|
+
| You have | Set |
|
|
33
|
+
|---|---|
|
|
34
|
+
| An OpenAI key | `OPENAI_API_KEY` (default model `gpt-4o-mini`) |
|
|
35
|
+
| An OpenRouter key | `OPENROUTER_API_KEY` |
|
|
36
|
+
| A free Vercel AI Gateway key | `AI_GATEWAY_API_KEY` (Nemotron 3.5 Lightning) |
|
|
37
|
+
| A Gemini or Groq key | `GEMINI_API_KEY` or `GROQ_API_KEY` |
|
|
38
|
+
| Ollama or LM Studio on this machine | `POCKET_TUTOR_BASE_URL=http://localhost:11434/v1` and `POCKET_TUTOR_MODEL=llama3.2`. No key needed. |
|
|
39
|
+
| Any other OpenAI-compatible server | `POCKET_TUTOR_BASE_URL`, `POCKET_TUTOR_MODEL`, and `POCKET_TUTOR_LLM_KEY` (or `OPENAI_API_KEY`) |
|
|
40
|
+
|
|
41
|
+
`POCKET_TUTOR_MODEL` changes the model for any provider. If you name no provider, the first key found wins, in the order of the table. Some newer OpenAI models reject `max_tokens` or a custom `temperature`. Pocket Tutor reads the error, drops that setting and retries, so there is nothing to configure.
|
|
42
|
+
|
|
43
|
+
The free Nemotron model is served through NVIDIA, which [may keep prompts and replies to improve its models](https://vercel.com/ai-gateway/models/nemotron-3.5-lightning-free). That includes your selection and any code snippets sent with it. Use your own key or a local model for private work.
|
|
44
|
+
|
|
45
|
+
## Use it
|
|
46
|
+
|
|
47
|
+
1. Select text anywhere.
|
|
48
|
+
2. Press **Ctrl+Alt+Space**.
|
|
49
|
+
3. Read the answer. The box keeps the size it opened at, and a long answer scrolls.
|
|
50
|
+
|
|
51
|
+
**To ask a question instead**, press the hotkey with nothing selected. The box opens with the cursor in a question line, and your question gets the same treatment as a selection: your project, your earlier lookups and the web.
|
|
52
|
+
|
|
53
|
+
The box has four controls:
|
|
54
|
+
|
|
55
|
+
- **The text line at the bottom** is for questions. If the answer didn't make sense, type what confuses you and press Enter. An empty Enter asks for the same thing again, simpler. It remembers your last four questions about that lookup.
|
|
56
|
+
- **Explain in detail** opens a larger window with a full lesson of 1,200 to 2,000 words: an analogy, the steps, the maths with small numbers, a commented example, a comparison table and a few practice questions. It also lists the code it found and links to read more.
|
|
57
|
+
- **Minimize** shrinks the box to a small "0". Click it to bring the box back.
|
|
58
|
+
- **Close**, or press Esc.
|
|
59
|
+
|
|
60
|
+
From a terminal, on any system:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pocket-tutor explain "what is a mutex"
|
|
64
|
+
echo "KeyError: 'user_id'" | pocket-tutor explain
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## It finds your workspace for you
|
|
68
|
+
|
|
69
|
+
You never say which project you mean. When you press the hotkey, Pocket Tutor looks at the window you are in:
|
|
70
|
+
|
|
71
|
+
- **VS Code, Cursor, Windsurf, VSCodium:** it reads the project folder from the title bar and finds its path from the editor's recent folders. If the title shows no folder, it uses the folder that editor had open last.
|
|
72
|
+
- **Terminals:** it reads the folder from the window title when the title shows one.
|
|
73
|
+
- **Any other app:** no workspace, so it answers from the text, your earlier lookups and the web.
|
|
74
|
+
|
|
75
|
+
With a workspace, the model gets a short overview (the project name, its dependencies and the start of the README) and snippets found with `git grep`, which includes Markdown notes and files you haven't committed. Answers point to `path:line`, which opens in your editor.
|
|
76
|
+
|
|
77
|
+
It also keeps a small graph of what you looked up before (idea names, one-sentence summaries, file paths), so a later answer can build on an earlier one. The graph lives in `%USERPROFILE%\.pocket-tutor\graph.json`. Choose **Forget saved context** in the tray menu to delete it.
|
|
78
|
+
|
|
79
|
+
## In the terminal
|
|
80
|
+
|
|
81
|
+
These work on any system, with the same model settings. No tray app is needed.
|
|
82
|
+
|
|
83
|
+
```
|
|
84
|
+
pocket-tutor setup pick a model, paste a key, test it, save it
|
|
85
|
+
pocket-tutor diff explain your uncommitted changes and what could break
|
|
86
|
+
pocket-tutor diff HEAD~1 the same for the last commit (any git diff arguments work)
|
|
87
|
+
pocket-tutor review a short quiz on the things you keep looking up
|
|
88
|
+
pocket-tutor explain --image shot.png "what is this?"
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
`diff` drops secret files such as `.env` and masks keys before anything leaves your laptop. It only knows what is in the diff, so check its claims against the code. `review` uses the lookups saved in `~/.pocket-tutor/graph.json`. `--image` needs a model that reads images, such as gpt-4o-mini or Gemini. The free Nemotron model does not, and says so.
|
|
92
|
+
|
|
93
|
+
## Math animations with ManimGL
|
|
94
|
+
|
|
95
|
+
For a formula, Pocket Tutor can draw a short animation: the formula is typeset, built piece by piece, and each part gets a colour and a plain-English note. It uses [ManimGL](https://github.com/3b1b/manim), the animation engine Grant Sanderson (3Blue1Brown) wrote for his videos. It is MIT licensed, and it is not the separate Manim Community Edition.
|
|
96
|
+
|
|
97
|
+
The animation is off until you install it, because it needs Python 3.12 and a few hundred MB of packages:
|
|
98
|
+
|
|
99
|
+
```powershell
|
|
100
|
+
pocket-tutor setup-visuals
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
After that, it runs when the model decides the selection is a formula or an equation. Ordinary words and code never trigger it. The model only fills in a small JSON description (formula, parts, notes), and one fixed scene file, `scripts/visual_scene.py`, draws it, so nothing the model writes is run as code. The rules a clip must meet are in [docs/CLIP_RULES.md](docs/CLIP_RULES.md).
|
|
104
|
+
|
|
105
|
+
## Privacy
|
|
106
|
+
|
|
107
|
+
- Nothing is read until you press the hotkey. Then it copies the selection and reads the window title.
|
|
108
|
+
- Password managers and private browser windows are skipped, and nothing is copied from them.
|
|
109
|
+
- Keys, tokens, passwords, private keys and email addresses are masked in the selection and in code snippets before they leave your computer. Files such as `.env`, `*.pem` and `id_rsa` are never searched.
|
|
110
|
+
- The model receives the selection, the window title, up to six code snippets and a few lines from your graph. For a short prose selection (25 words or fewer, not code), the first ten words also go to DuckDuckGo, or to the search engine you chose.
|
|
111
|
+
- Your clipboard is restored after each copy.
|
|
112
|
+
- API keys stay on your machine, in environment variables or in `%USERPROFILE%\.pocket-tutor\config.json`.
|
|
113
|
+
|
|
114
|
+
## Settings
|
|
115
|
+
|
|
116
|
+
Everything is optional. The file is `%USERPROFILE%\.pocket-tutor\config.json` (see `config.example.json`):
|
|
117
|
+
|
|
118
|
+
| Key | Default | What it does |
|
|
119
|
+
|---|---|---|
|
|
120
|
+
| `hotkey` | `CommandOrControl+Alt+Space` | Any [Electron accelerator](https://www.electronjs.org/docs/latest/api/accelerator) |
|
|
121
|
+
| `provider` | the first key found | `vercel`, `openai`, `openrouter`, `gemini`, `groq`, `ollama` or `custom` |
|
|
122
|
+
| `base_url`, `model`, `plan_model` | from the provider | Any OpenAI-compatible server. `plan_model` does the small planning step and can be a cheaper model. |
|
|
123
|
+
| `roots` | `["C:\\Projects"]` | Folders searched for the project named in the editor's title |
|
|
124
|
+
| `max_snippets`, `web_results` | 6, 5 | How much context goes to the model |
|
|
125
|
+
| `search_engine` | `brave` if a key is set, else `searxng` if a URL is set, else `duckduckgo` | Where web context comes from. See below. |
|
|
126
|
+
| `visuals` | `true` | Math animations, once ManimGL is installed |
|
|
127
|
+
| `remember` | `true` | Keep the graph of earlier lookups |
|
|
128
|
+
|
|
129
|
+
Choose **Reload settings** in the tray menu after editing.
|
|
130
|
+
|
|
131
|
+
### Web search engines
|
|
132
|
+
|
|
133
|
+
Web results only feed the answer, and only for short prose selections. Code is never sent to a search engine. Pick one:
|
|
134
|
+
|
|
135
|
+
| Engine | Set | Notes |
|
|
136
|
+
|---|---|---|
|
|
137
|
+
| DuckDuckGo | nothing | The default. No key. |
|
|
138
|
+
| Brave | `BRAVE_API_KEY` | Has a free tier. |
|
|
139
|
+
| SearXNG | `SEARXNG_URL` | Your own instance, with `json` listed under `search.formats` in its settings. |
|
|
140
|
+
|
|
141
|
+
Set `search_engine` (or `POCKET_TUTOR_SEARCH`) to choose one explicitly. If the chosen engine fails, for example a wrong key or a used-up quota, that lookup falls back to DuckDuckGo.
|
|
142
|
+
|
|
143
|
+
## Frequently asked questions
|
|
144
|
+
|
|
145
|
+
**Does it only work in VS Code?** No. It works in any app where you can select text and copy it. VS Code, Cursor, Windsurf and VSCodium also get answers that refer to your own project.
|
|
146
|
+
|
|
147
|
+
**Can I use a local model?** Yes. Start Ollama, set `POCKET_TUTOR_BASE_URL=http://localhost:11434/v1`, and set `POCKET_TUTOR_MODEL` to a model you have pulled. No key is needed.
|
|
148
|
+
|
|
149
|
+
**Does it run on macOS or Linux?** The tray app does not yet, because it uses Windows calls to find the active window and copy the selection. `pocket-tutor explain` runs anywhere Node runs.
|
|
150
|
+
|
|
151
|
+
**Why Ctrl+Alt+Space?** Ctrl+Shift+Space is taken by VS Code's parameter hints and several input tools. Ctrl+Alt+Space is rarely bound. Pick your own with `hotkey`.
|
|
152
|
+
|
|
153
|
+
**The hotkey does nothing.** Another app may already use it. The tray tooltip says so. Set a different `hotkey` and choose **Reload settings**.
|
|
154
|
+
|
|
155
|
+
**It says no model is set up.** Run `pocket-tutor doctor`. It names what is missing.
|
|
156
|
+
|
|
157
|
+
**Why are there no math animations?** Either you haven't run `pocket-tutor setup-visuals`, or the selection wasn't a formula. `pocket-tutor doctor` reports whether ManimGL is installed.
|
|
158
|
+
|
|
159
|
+
## Develop
|
|
160
|
+
|
|
161
|
+
```powershell
|
|
162
|
+
npm test # unit tests and a fake OpenAI-compatible server; no key or network
|
|
163
|
+
npm run build # bundles the box into src/ui/dist
|
|
164
|
+
npm run judge-clips # measures rendered math clips (needs ManimGL)
|
|
165
|
+
$env:POCKET_TUTOR_SELFTEST="$env:TEMP\box.png"; npx electron . # renders a sample box to a PNG
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
| File | Job |
|
|
169
|
+
|---|---|
|
|
170
|
+
| `bin/pocket-tutor.js` | The command line |
|
|
171
|
+
| `src/main.js` | Hotkey, clipboard, the explain pipeline, the box and the tray |
|
|
172
|
+
| `src/helper.ps1`, `src/native.js` | A PowerShell helper that finds the active window and presses Ctrl+C |
|
|
173
|
+
| `src/model.js` | Prompts and an OpenAI-compatible streaming client that adapts to each server |
|
|
174
|
+
| `src/config.js` | Provider presets, keys and settings |
|
|
175
|
+
| `src/project.js`, `src/graph.js`, `src/search.js` | Code search, the graph of earlier lookups, web search |
|
|
176
|
+
| `src/privacy.js` | Blocked apps, secret files, masking |
|
|
177
|
+
| `src/visual.js`, `scripts/visual_scene.py` | Math animations with ManimGL |
|
|
178
|
+
| `src/ui/` | The box, built with Astryx components |
|
|
179
|
+
|
|
180
|
+
## Credits
|
|
181
|
+
|
|
182
|
+
[ManimGL](https://github.com/3b1b/manim) for the math animations, [KaTeX](https://katex.org) for typesetting, [Astryx](https://www.npmjs.com/package/@astryxdesign/core) for the interface, [Electron](https://www.electronjs.org) for the window.
|
|
183
|
+
|
|
184
|
+
## License
|
|
185
|
+
|
|
186
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// pocket-tutor: the command line for the package.
|
|
3
|
+
// pocket-tutor start the tray app (Windows): select text anywhere, press the hotkey
|
|
4
|
+
// pocket-tutor explain [text] explain text in the terminal (any OS); reads stdin when no text is given
|
|
5
|
+
// pocket-tutor setup pick a model and save it (no JSON editing)
|
|
6
|
+
// pocket-tutor diff [git args] what a code change does and what could break
|
|
7
|
+
// pocket-tutor review a short quiz on what you keep looking up
|
|
8
|
+
// pocket-tutor explain --image file.png explain a screenshot (needs a model that reads images)
|
|
9
|
+
// pocket-tutor doctor check the model, web search, math clips and git
|
|
10
|
+
// pocket-tutor setup-visuals optional: install ManimGL for math animations
|
|
11
|
+
const path = require('path')
|
|
12
|
+
const { spawn } = require('child_process')
|
|
13
|
+
|
|
14
|
+
const ROOT = path.join(__dirname, '..')
|
|
15
|
+
const HELP = `Pocket Tutor: select any text, get a short explanation in plain English.
|
|
16
|
+
|
|
17
|
+
pocket-tutor start the tray app (Windows). Select text, press Ctrl+Alt+Space
|
|
18
|
+
pocket-tutor explain [text] explain text here in the terminal; reads stdin if no text is given
|
|
19
|
+
pocket-tutor setup pick a model and save it, no JSON editing
|
|
20
|
+
pocket-tutor diff [git args] explain a code change and what could break (default: all uncommitted changes)
|
|
21
|
+
pocket-tutor review a short quiz on the things you keep looking up
|
|
22
|
+
pocket-tutor explain --image file.png explain a screenshot or chart (needs a model that reads images)
|
|
23
|
+
pocket-tutor doctor check the model, web search, math animations and git
|
|
24
|
+
pocket-tutor setup-visuals optional: install ManimGL, which draws math animations
|
|
25
|
+
|
|
26
|
+
Model: any OpenAI-compatible server. Examples (PowerShell):
|
|
27
|
+
$env:OPENAI_API_KEY="..." OpenAI
|
|
28
|
+
$env:POCKET_TUTOR_BASE_URL="http://localhost:11434/v1"; $env:POCKET_TUTOR_MODEL="llama3.2" Ollama, no key
|
|
29
|
+
$env:AI_GATEWAY_API_KEY="..." free Nemotron through Vercel
|
|
30
|
+
More: https://github.com/advaithsarva/pocket-tutor`
|
|
31
|
+
|
|
32
|
+
function stream(model, cfg, messages, maxTokens = 1200) {
|
|
33
|
+
let shown = 0
|
|
34
|
+
return model.chat(cfg, cfg.model, messages, { maxTokens, onChunk: (t) => { process.stdout.write(t.slice(shown)); shown = t.length } })
|
|
35
|
+
.then((out) => { process.stdout.write(out.slice(shown) + '\n'); return 0 })
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function ready() {
|
|
39
|
+
const cfg = require('../src/config').load()
|
|
40
|
+
if (!cfg.llm_key) { console.error('No model set up. Run: pocket-tutor setup'); return null }
|
|
41
|
+
return cfg
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function diff(args) {
|
|
45
|
+
const model = require('../src/model'), tools = require('../src/tools')
|
|
46
|
+
const cfg = ready(); if (!cfg) return 2
|
|
47
|
+
let d
|
|
48
|
+
try { d = tools.readDiff(args) } catch (e) { console.error(`Could not read the diff: ${e.stderr ? String(e.stderr).trim() : e.message}`); return 2 }
|
|
49
|
+
if (!d.trim()) { console.error('No changes to explain.'); return 2 }
|
|
50
|
+
try { return await stream(model, cfg, tools.diffMessages(d, require('../src/project').profile(process.cwd()))) }
|
|
51
|
+
catch (e) { console.error(`Could not explain this: ${e.message}`); return 1 }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function review() {
|
|
55
|
+
const model = require('../src/model'), tools = require('../src/tools'), config = require('../src/config'), graph = require('../src/graph')
|
|
56
|
+
const cfg = ready(); if (!cfg) return 2
|
|
57
|
+
const concepts = tools.pickConcepts(graph.load(config.GRAPH))
|
|
58
|
+
if (!concepts.length) { console.error('Nothing to review yet. Look a few things up first, then run this again.'); return 2 }
|
|
59
|
+
const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout })
|
|
60
|
+
// read lines from a queue made up front, so piped input that ends early still works
|
|
61
|
+
const lines = rl[Symbol.asyncIterator]()
|
|
62
|
+
const ask = async (q) => { process.stdout.write(q); const l = await lines.next(); return l.done ? '' : l.value }
|
|
63
|
+
try {
|
|
64
|
+
const quiz = tools.parseQuiz(await model.chat(cfg, cfg.model, tools.quizMessages(concepts), { json: true, maxTokens: 600 }), concepts)
|
|
65
|
+
let n = 0
|
|
66
|
+
for (const q of quiz) {
|
|
67
|
+
const a = (await ask(`
|
|
68
|
+
${q.concept}: ${q.question}
|
|
69
|
+
> `)).trim()
|
|
70
|
+
const g = await model.chat(cfg, cfg.model, tools.gradeMessages(q, a), { maxTokens: 120 })
|
|
71
|
+
console.log(g); if (/^right/i.test(g)) n++
|
|
72
|
+
}
|
|
73
|
+
console.log(`
|
|
74
|
+
${n} of ${quiz.length} right.`)
|
|
75
|
+
return 0
|
|
76
|
+
} catch (e) { console.error(`Could not run the review: ${e.message}`); return 1 }
|
|
77
|
+
finally { rl.close() }
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function explain(args) {
|
|
81
|
+
const config = require('../src/config')
|
|
82
|
+
const model = require('../src/model')
|
|
83
|
+
const { mask } = require('../src/privacy')
|
|
84
|
+
const at = args.indexOf('--image')
|
|
85
|
+
if (at >= 0) {
|
|
86
|
+
const tools = require('../src/tools')
|
|
87
|
+
const file = args[at + 1]
|
|
88
|
+
if (!file) { console.error('Pass the image: pocket-tutor explain --image screenshot.png ["a question"]'); return 2 }
|
|
89
|
+
const cfg = ready(); if (!cfg) return 2
|
|
90
|
+
try {
|
|
91
|
+
const q = mask(args.filter((_, i) => i !== at && i !== at + 1).join(' ').trim())
|
|
92
|
+
return await stream(model, cfg, tools.imageMessages(tools.imageDataUrl(file), q))
|
|
93
|
+
} catch (e) { console.error(`Could not explain this image: ${String(e.message).replace(/\.$/, '')}. This model may not read images; try a vision model such as gpt-4o-mini or gemini.`); return 1 }
|
|
94
|
+
}
|
|
95
|
+
let text = args.join(' ').trim()
|
|
96
|
+
if (!text && !process.stdin.isTTY) { let s = ''; for await (const c of process.stdin) s += c; text = s.trim() }
|
|
97
|
+
if (!text) { console.error('Nothing to explain. Pass some text, or pipe it in: echo "RMSNorm" | pocket-tutor explain'); return 2 }
|
|
98
|
+
const cfg = config.load()
|
|
99
|
+
if (!cfg.llm_key) { console.error('No model set up. Run: pocket-tutor setup'); return 2 }
|
|
100
|
+
const selection = mask(text).slice(0, model.MAX_SELECTION)
|
|
101
|
+
let shown = 0
|
|
102
|
+
try {
|
|
103
|
+
const out = await model.chat(cfg, cfg.model, model.answerMessages(selection, { label: 'terminal' }, [], [], []), {
|
|
104
|
+
maxTokens: 1200, onChunk: (t) => { process.stdout.write(t.slice(shown)); shown = t.length } })
|
|
105
|
+
process.stdout.write(out.slice(shown) + '\n')
|
|
106
|
+
return 0
|
|
107
|
+
} catch (e) { console.error(`Could not explain this: ${e.message}`); return 1 }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function main() {
|
|
111
|
+
const [cmd, ...rest] = process.argv.slice(2)
|
|
112
|
+
if (cmd === 'help' || cmd === '--help' || cmd === '-h') return console.log(HELP)
|
|
113
|
+
if (cmd === 'explain') return process.exit(await explain(rest))
|
|
114
|
+
if (cmd === 'setup') return process.exit(await require('../src/setup').run())
|
|
115
|
+
if (cmd === 'diff') return process.exit(await diff(rest))
|
|
116
|
+
if (cmd === 'review') return process.exit(await review())
|
|
117
|
+
if (cmd === 'doctor') return require('../scripts/doctor.js')
|
|
118
|
+
if (cmd === 'setup-visuals') return require('../scripts/setup-visuals.js')
|
|
119
|
+
if (cmd && cmd !== 'start') { console.error(`Unknown command "${cmd}".\n\n${HELP}`); return process.exit(2) }
|
|
120
|
+
if (process.platform !== 'win32') { console.error('The tray app needs Windows (it reads the selection with Windows APIs). The terminal command works everywhere: pocket-tutor explain "text"'); return process.exit(1) }
|
|
121
|
+
// a detached Electron process, so the terminal is free again
|
|
122
|
+
const child = spawn(require('electron'), [ROOT], { detached: true, stdio: 'ignore', windowsHide: false })
|
|
123
|
+
child.unref()
|
|
124
|
+
console.log('Pocket Tutor is running in the system tray. Select text anywhere and press Ctrl+Alt+Space.')
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
main()
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"hotkey": "CommandOrControl+Alt+Space",
|
|
3
|
+
"provider": "vercel",
|
|
4
|
+
"model": "nvidia/nemotron-3.5-lightning-free",
|
|
5
|
+
"plan_model": "nvidia/nemotron-3.5-lightning-free",
|
|
6
|
+
"roots": ["C:\\Projects"],
|
|
7
|
+
"max_snippets": 6,
|
|
8
|
+
"web_results": 5,
|
|
9
|
+
"search_engine": "duckduckgo",
|
|
10
|
+
"visuals": true,
|
|
11
|
+
"remember": true
|
|
12
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Math clip rules
|
|
2
|
+
|
|
3
|
+
The math clip is Pocket Tutor's short ManimGL animation for a formula. It should teach the way a 3Blue1Brown video does: the formula is typeset properly and written piece by piece; then each part in turn gets its own colour, a brace and a few plain words while the rest dims; it ends on the one line worth remembering.
|
|
4
|
+
|
|
5
|
+
These rules came from a clip that failed every one of them (2026-11-24). The selection read "Forward pass requires O(N_L(Ld^2 + L^2 d)) FLOPs". The clip showed `4 * L * d * (d + L)`, a formula the model made up, in Consolas, inside one yellow box labelled "total FLOPs per layer", on a mostly empty 640×360 frame. The text box above it showed raw `$L^2 d$`.
|
|
6
|
+
|
|
7
|
+
Each rule names the bug it prevents and what enforces it:
|
|
8
|
+
- **Prompt:** the model-facing lines in `model.formulaMessages`.
|
|
9
|
+
- **checkSpec:** `visual.checkSpec`, run on every reply. Problems go back to the model for one retry.
|
|
10
|
+
- **Scene:** fixed behaviour of `scripts/visual_scene.py`.
|
|
11
|
+
- **Judge:** `npm run judge-clips`, which measures rendered frames.
|
|
12
|
+
|
|
13
|
+
| # | Rule | Prevents | Enforced by |
|
|
14
|
+
|---|---|---|---|
|
|
15
|
+
| R1 | **Faithful.** If the selection contains a formula, the clip shows that formula, with the same symbols. It may be rewritten in proper LaTeX, but never swapped for a different, simpler or "equivalent" one. Only a selection without a formula gets the standard formula for the idea. | The screenshot clip: `4·L·d·(d+L)` for a selection about `O(N_L(Ld²+L²d))`. The model had never been shown the selection. | Prompt; checkSpec (the selection's single-letter variables and Greek names must be ≥ 75% present in the formula, and ≥ 40% of the formula's must come from the selection); the selection is now passed in |
|
|
16
|
+
| R2 | **Typeset.** The formula is LaTeX math in the subset that matplotlib's mathtext renders with STIX. No `*`. Function names go in `\mathrm{}`. | Consolas ASCII (`4 * L * d`); `\le` and `\ge`, which mathtext can't render (it can render `\leq` and `\geq`). | checkSpec (`TEX_OK` allow-list, measured by rendering each command; balanced braces; no `*`); the scene repairs an unknown name (`\softmax` → `\mathrm{softmax}`) and only as a last resort draws readable Unicode |
|
|
17
|
+
| R3 | **One colour per part.** 1–4 parts, each a whole group (a whole `\frac{..}{..}`, `\sqrt{..}`, or a symbol with its `^` and `_`), never cut from inside `{ }`, never overlapping. Each part keeps its colour to the end. | One box around everything; a part that can't be cut out and silently vanishes. | Prompt; checkSpec (inside-braces, cut-off script, overlap); the scene drops a part it can't typeset alone rather than faking it; the judge counts the part colours by hue |
|
|
18
|
+
| R4 | **Notes are meaning, not symbols.** At most 8 plain words on what the part does ("every token looks at every other token"), never the symbols read aloud. | "total FLOPs per layer" as the only explanation. | Prompt; checkSpec (at most 9 words, never empty) |
|
|
19
|
+
| R5 | **Big.** The formula is as large as the frame allows: up to 11.5 × 2.6 of the 14.2 × 8 frame units. | A small formula on an empty frame. | Scene; the judge requires ≥ 50% of the frame width or ≥ 16% of its height |
|
|
20
|
+
| R6 | **Inside the frame.** Nothing touches the edges. Braces hang from the formula's bottom line, notes sit under the braces, and both are slid back inside the frame. | A note centred under the last part ran off the right edge; the note under `g` sat on top of the tall fraction beside it. | Scene (`inside`, braces on the formula's floor); the judge allows no ink in the outer 2% of any frame |
|
|
21
|
+
| R7 | **Ends on a takeaway.** One line, at most 70 characters, giving the consequence (how it scales, what it trades off), with the formula still coloured above it. | A clip that just stops; a takeaway that only repeats the title. | Prompt; checkSpec (needed when there are 2+ parts); the judge checks for a line near the bottom of the last frame |
|
|
22
|
+
| R8 | **Budget.** Rendered in ≤ 12 s, ≤ 500 KB, 5–24 s long, at 960×540 and 24 fps. | Sending the box a slow or huge clip; a 4-second loop that flashes past. | `visual.render` settings; the judge |
|
|
23
|
+
| R9 | **3b1b's moves, in this order.** Write the formula left to right; then for each part, colour it, dim the rest, grow a brace and fade the note in; at the end, bring everything back in its colours. Lone brackets are stretched to the fraction they enclose, as `\left( \right)` would be. | The whole formula appearing at once; short brackets beside a tall fraction. | Scene |
|
|
24
|
+
| R10 | **A wrong clip is worse than none.** One retry with the problems listed. After that, parts that still break a rule are dropped, and a formula that still breaks one means no clip at all. | Showing a confident, wrong animation under a correct explanation. | `main.js` with `visual.settle` |
|
|
25
|
+
| R11 | **The spec is data.** The model writes JSON only. The scene is fixed code, and mathtext only parses. | A prompt injected through a web page or selection running Python on the laptop. | `visual.cleanSpec`; the scene file (already a project rule) |
|
|
26
|
+
|
|
27
|
+
## The same rule in the text box
|
|
28
|
+
|
|
29
|
+
The explanation's math is typeset too. The answer and detail prompts ask for `$…$` and `$$…$$`, and the box renders them with KaTeX (`trust: false`, so no `\href`). If KaTeX can't read a formula, it is shown as code, never as raw dollar signs.
|
|
30
|
+
|
|
31
|
+
## Checking a change
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npm test # checkSpec and settle on the screenshot spec and the other cases
|
|
35
|
+
npm run judge-clips # renders test/clips/cases.json and measures the frames; all must pass
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The judge writes a contact sheet (`<name>.png`, 12 frames) for each case to `%TEMP%\pocket-tutor-judge`. Look at them as well: a frame can pass every measurement and still look wrong. Add a case to `test/clips/cases.json` whenever a real clip fails in a way the judge missed.
|
package/package.json
CHANGED
|
@@ -1,6 +1,76 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pocket-tutor",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Select any text, press a hotkey, get a plain-English explanation. Works with any OpenAI-compatible model, including local Ollama. Windows tray app plus a terminal command.",
|
|
5
|
+
"main": "src/main.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "node scripts/build-ui.mjs",
|
|
8
|
+
"start": "npm run build && electron .",
|
|
9
|
+
"test": "node --test \"test/*.test.js\"",
|
|
10
|
+
"doctor": "node scripts/doctor.js",
|
|
11
|
+
"setup-visuals": "node scripts/setup-visuals.js",
|
|
12
|
+
"judge-clips": "node scripts/clip-judge.js",
|
|
13
|
+
"prepublishOnly": "npm run build && npm test"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"esbuild": "^0.28.2",
|
|
17
|
+
"@astryxdesign/cli": "0.7.17",
|
|
18
|
+
"@astryxdesign/core": "0.7.17",
|
|
19
|
+
"@astryxdesign/theme-neutral": "0.7.17",
|
|
20
|
+
"@stylexjs/stylex": "0.19.2",
|
|
21
|
+
"katex": "0.19.1",
|
|
22
|
+
"react": "19.3.1",
|
|
23
|
+
"react-dom": "19.3.1"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"electron": "^42.2.0"
|
|
27
|
+
},
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"keywords": [
|
|
30
|
+
"ai-tutor",
|
|
31
|
+
"cli",
|
|
32
|
+
"code-explainer",
|
|
33
|
+
"desktop",
|
|
34
|
+
"electron",
|
|
35
|
+
"explain",
|
|
36
|
+
"learning",
|
|
37
|
+
"llm",
|
|
38
|
+
"math",
|
|
39
|
+
"nemotron",
|
|
40
|
+
"ollama",
|
|
41
|
+
"openai",
|
|
42
|
+
"openrouter",
|
|
43
|
+
"selected-text",
|
|
44
|
+
"students",
|
|
45
|
+
"study-tool",
|
|
46
|
+
"vscode",
|
|
47
|
+
"windows"
|
|
48
|
+
],
|
|
49
|
+
"engines": {
|
|
50
|
+
"node": ">=24"
|
|
51
|
+
},
|
|
52
|
+
"bin": {
|
|
53
|
+
"pocket-tutor": "bin/pocket-tutor.js"
|
|
54
|
+
},
|
|
55
|
+
"files": [
|
|
56
|
+
"bin",
|
|
57
|
+
"src",
|
|
58
|
+
"scripts/doctor.js",
|
|
59
|
+
"scripts/setup-visuals.js",
|
|
60
|
+
"scripts/visual_scene.py",
|
|
61
|
+
"scripts/clip-judge.js",
|
|
62
|
+
"scripts/build-ui.mjs",
|
|
63
|
+
"docs",
|
|
64
|
+
"config.example.json",
|
|
65
|
+
"LICENSE",
|
|
66
|
+
"README.md"
|
|
67
|
+
],
|
|
68
|
+
"homepage": "https://github.com/advaithsarva/pocket-tutor#readme",
|
|
69
|
+
"repository": {
|
|
70
|
+
"type": "git",
|
|
71
|
+
"url": "git+https://github.com/advaithsarva/pocket-tutor.git"
|
|
72
|
+
},
|
|
73
|
+
"bugs": {
|
|
74
|
+
"url": "https://github.com/advaithsarva/pocket-tutor/issues"
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// npm run build: bundles the popup (src/ui/app.jsx + Astryx + the lens theme) into src/ui/dist/.
|
|
2
|
+
// `npm start` runs this first.
|
|
3
|
+
import * as esbuild from 'esbuild'
|
|
4
|
+
import { fileURLToPath } from 'node:url'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
|
|
7
|
+
const root = path.dirname(path.dirname(fileURLToPath(import.meta.url)))
|
|
8
|
+
|
|
9
|
+
await esbuild.build({
|
|
10
|
+
absWorkingDir: root,
|
|
11
|
+
entryPoints: { popup: 'src/ui/app.jsx' },
|
|
12
|
+
outdir: 'src/ui/dist',
|
|
13
|
+
bundle: true,
|
|
14
|
+
minify: true,
|
|
15
|
+
sourcemap: false,
|
|
16
|
+
format: 'iife',
|
|
17
|
+
platform: 'browser',
|
|
18
|
+
target: 'chrome130',
|
|
19
|
+
jsx: 'automatic',
|
|
20
|
+
define: { 'process.env.NODE_ENV': '"production"' },
|
|
21
|
+
loader: { '.woff2': 'file', '.woff': 'file', '.ttf': 'file', '.otf': 'file', '.svg': 'file', '.png': 'file' },
|
|
22
|
+
assetNames: 'assets/[name]-[hash]',
|
|
23
|
+
logLevel: 'warning',
|
|
24
|
+
})
|
|
25
|
+
console.log('built src/ui/dist/popup.js and popup.css')
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// npm run judge-clips: the outside judge for the math clip. It renders every case in test/clips/cases.json
|
|
2
|
+
// through the app's own visual.render, decodes the frames with ffmpeg and measures them, so "the clip is
|
|
3
|
+
// good" is a number, not an opinion. Rules it checks are in docs/CLIP_RULES.md (R-numbers below).
|
|
4
|
+
// node scripts/clip-judge.js [case-name ...] → a table, contact sheets in <tmp>/pocket-tutor-judge, exit 1 on a failure
|
|
5
|
+
// Needs `pocket-tutor setup-visuals`. Doesn't call any model and needs no key.
|
|
6
|
+
const fs = require('fs')
|
|
7
|
+
const os = require('os')
|
|
8
|
+
const path = require('path')
|
|
9
|
+
const { execFileSync } = require('child_process')
|
|
10
|
+
const visual = require('../src/visual')
|
|
11
|
+
|
|
12
|
+
const ROOT = path.join(__dirname, '..')
|
|
13
|
+
const FFMPEG = path.join(require('../src/config').HOME, 'venv', 'Scripts', 'ffmpeg.exe')
|
|
14
|
+
const OUT = path.join(os.tmpdir(), 'pocket-tutor-judge')
|
|
15
|
+
const W = 320, H = 180, FPS = 6
|
|
16
|
+
const PALETTE = [[0x58, 0xC4, 0xDD], [0xF4, 0xD3, 0x45], [0x83, 0xC1, 0x67], [0xFC, 0x62, 0x55]]
|
|
17
|
+
// hue in degrees, or null for pixels too dark or too grey to have one (black, white, the dimmed formula)
|
|
18
|
+
function hue(r, g, b) {
|
|
19
|
+
const max = Math.max(r, g, b), min = Math.min(r, g, b), c = max - min
|
|
20
|
+
if (max < 70 || c / max < 0.35) return null
|
|
21
|
+
const h = max === r ? ((g - b) / c) % 6 : max === g ? (b - r) / c + 2 : (r - g) / c + 4
|
|
22
|
+
return (h * 60 + 360) % 360
|
|
23
|
+
}
|
|
24
|
+
const PAL_HUES = PALETTE.map(([r, g, b]) => hue(r, g, b))
|
|
25
|
+
const LIMITS = { renderMs: 12000, bytes: 500 * 1024, minS: 5, maxS: 24, minWidth: 0.5, minHeight: 0.16, edge: 0.02 }
|
|
26
|
+
|
|
27
|
+
function frames(mp4) {
|
|
28
|
+
const raw = execFileSync(FFMPEG, ['-v', 'error', '-i', mp4, '-vf', `fps=${FPS},scale=${W}:${H}`, '-f', 'rawvideo', '-pix_fmt', 'rgb24', 'pipe:1'],
|
|
29
|
+
{ maxBuffer: 1 << 28 })
|
|
30
|
+
const n = Math.floor(raw.length / (W * H * 3))
|
|
31
|
+
return Array.from({ length: n }, (_, i) => raw.subarray(i * W * H * 3, (i + 1) * W * H * 3))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function measure(fr) {
|
|
35
|
+
const ink = (f, x, y) => { const i = (y * W + x) * 3; return f[i] + f[i + 1] + f[i + 2] > 150 }
|
|
36
|
+
const ex = Math.ceil(W * LIMITS.edge), ey = Math.ceil(H * LIMITS.edge)
|
|
37
|
+
let edgeHits = 0, bestW = 0, bestH = 0
|
|
38
|
+
const colourFrames = PALETTE.map(() => 0)
|
|
39
|
+
for (const f of fr) {
|
|
40
|
+
// R6: nothing touches the frame's edge
|
|
41
|
+
for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) {
|
|
42
|
+
if ((x < ex || x >= W - ex || y < ey || y >= H - ey) && ink(f, x, y)) edgeHits++
|
|
43
|
+
}
|
|
44
|
+
// R5: the formula is big: ink extent in the formula band (between the title and the notes)
|
|
45
|
+
let x0 = W, x1 = -1, y0 = H, y1 = -1
|
|
46
|
+
for (let y = Math.floor(H * 0.16); y < Math.floor(H * 0.62); y++) for (let x = 0; x < W; x++) {
|
|
47
|
+
if (ink(f, x, y)) { if (x < x0) x0 = x; if (x > x1) x1 = x; if (y < y0) y0 = y; if (y > y1) y1 = y }
|
|
48
|
+
}
|
|
49
|
+
if (x1 >= 0) { bestW = Math.max(bestW, (x1 - x0) / W); bestH = Math.max(bestH, (y1 - y0) / H) }
|
|
50
|
+
// R3: each part gets its own colour. Matched by hue, not RGB distance: small antialiased text (a yellow g,
|
|
51
|
+
// its note) is blended with black at this size, so an exact-colour match missed a part that was plainly there.
|
|
52
|
+
const hits = PALETTE.map(() => 0)
|
|
53
|
+
for (let i = 0; i < f.length; i += 3) {
|
|
54
|
+
const h = hue(f[i], f[i + 1], f[i + 2])
|
|
55
|
+
if (h == null) continue
|
|
56
|
+
PAL_HUES.forEach((ph, k) => { if (Math.min(Math.abs(h - ph), 360 - Math.abs(h - ph)) < 16) hits[k]++ })
|
|
57
|
+
}
|
|
58
|
+
hits.forEach((n, k) => { if (n >= 12) colourFrames[k]++ })
|
|
59
|
+
}
|
|
60
|
+
// R7: the last frame has the takeaway line near the bottom
|
|
61
|
+
const last = fr[fr.length - 1]
|
|
62
|
+
let bottomInk = 0
|
|
63
|
+
for (let y = Math.floor(H * 0.78); y < H - ey; y++) for (let x = 0; x < W; x++) if (ink(last, x, y)) bottomInk++
|
|
64
|
+
return { edgeHits, bestW, bestH, colours: colourFrames.filter((c) => c >= 2).length, bottomInk }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function judge(c) {
|
|
68
|
+
const t = Date.now()
|
|
69
|
+
let bytes
|
|
70
|
+
try { bytes = await visual.render(visual.cleanSpec(c.spec) || c.spec) } catch (e) { return { name: c.name, fail: [`render failed: ${e.message}`] } }
|
|
71
|
+
const ms = Date.now() - t
|
|
72
|
+
const mp4 = path.join(OUT, `${c.name}.mp4`)
|
|
73
|
+
fs.writeFileSync(mp4, bytes)
|
|
74
|
+
const fr = frames(mp4)
|
|
75
|
+
const secs = fr.length / FPS
|
|
76
|
+
const m = measure(fr)
|
|
77
|
+
execFileSync(FFMPEG, ['-v', 'error', '-y', '-i', mp4, '-vf', `fps=${(12 / secs).toFixed(3)},scale=480:-1,tile=3x4:padding=4:color=gray`, '-frames:v', '1', path.join(OUT, `${c.name}.png`)])
|
|
78
|
+
const fail = []
|
|
79
|
+
if (ms > LIMITS.renderMs) fail.push(`R8 render ${ms} ms > ${LIMITS.renderMs}`)
|
|
80
|
+
if (bytes.length > LIMITS.bytes) fail.push(`R8 ${Math.round(bytes.length / 1024)} KB > ${LIMITS.bytes / 1024}`)
|
|
81
|
+
if (secs < LIMITS.minS || secs > LIMITS.maxS) fail.push(`R8 length ${secs.toFixed(1)} s outside ${LIMITS.minS}-${LIMITS.maxS}`)
|
|
82
|
+
if (m.edgeHits > 0) fail.push(`R6 ink touches the frame edge (${m.edgeHits} px)`)
|
|
83
|
+
if (m.bestW < LIMITS.minWidth && m.bestH < LIMITS.minHeight) fail.push(`R5 formula too small (${Math.round(m.bestW * 100)}% wide, ${Math.round(m.bestH * 100)}% tall)`)
|
|
84
|
+
if (m.colours < c.expect.colours) fail.push(`R3 ${m.colours} part colours seen, want ${c.expect.colours}`)
|
|
85
|
+
if (c.expect.takeaway && m.bottomInk < 40) fail.push('R7 no takeaway line on the last frame')
|
|
86
|
+
return { name: c.name, ms, kb: Math.round(bytes.length / 1024), secs, ...m, fail }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function main() {
|
|
90
|
+
if (!visual.available()) { console.log('ManimGL is not set up: run pocket-tutor setup-visuals'); process.exit(2) }
|
|
91
|
+
fs.mkdirSync(OUT, { recursive: true })
|
|
92
|
+
const only = process.argv.slice(2)
|
|
93
|
+
const cases = JSON.parse(fs.readFileSync(path.join(ROOT, 'test', 'clips', 'cases.json'), 'utf8')).filter((c) => !only.length || only.includes(c.name))
|
|
94
|
+
let bad = 0
|
|
95
|
+
for (const c of cases) {
|
|
96
|
+
const r = await judge(c)
|
|
97
|
+
const stats = r.ms ? `${(r.ms / 1000).toFixed(1)} s render · ${r.kb} KB · ${r.secs.toFixed(1)} s clip · ${Math.round(r.bestW * 100)}% wide · ${r.colours} colours` : ''
|
|
98
|
+
console.log(`${r.fail.length ? 'FAIL' : 'pass'} ${c.name.padEnd(26)} ${stats}`)
|
|
99
|
+
for (const f of r.fail) console.log(` ${f}`)
|
|
100
|
+
bad += r.fail.length ? 1 : 0
|
|
101
|
+
}
|
|
102
|
+
console.log(`\n${cases.length - bad}/${cases.length} clips pass. Contact sheets: ${OUT}`)
|
|
103
|
+
process.exit(bad ? 1 : 0)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
main()
|