dracosh 0.7.0 → 0.8.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/README.md +28 -4
- package/locales/de.json +166 -6
- package/locales/en.json +166 -6
- package/locales/fr.json +168 -6
- package/locales/pl.json +170 -6
- package/locales/uk.json +170 -6
- package/package.json +1 -1
- package/src/ai.js +245 -0
- package/src/alerts.js +77 -23
- package/src/assist.js +232 -0
- package/src/cli.js +2 -2
- package/src/library.js +14 -2
- package/src/lock.js +1 -1
- package/src/manage.js +14 -3
- package/src/settings.js +5 -1
- package/src/ui/AddCard.js +228 -32
- package/src/ui/Ai.js +284 -0
- package/src/ui/AnswerInput.js +13 -6
- package/src/ui/App.js +38 -11
- package/src/ui/Cards.js +126 -12
- package/src/ui/Settings.js +5 -1
- package/src/ui/Voice.js +238 -0
- package/src/ui/Welcome.js +4 -3
- package/src/voice.js +129 -0
package/src/ui/Cards.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Fragment, useEffect, useRef, useState } from "react";
|
|
2
2
|
import { Box, useInput } from "ink";
|
|
3
|
-
import { t } from "../i18n.js";
|
|
3
|
+
import { language, t } from "../i18n.js";
|
|
4
|
+
import { providerName } from "../ai.js";
|
|
5
|
+
import { checkCard } from "../assist.js";
|
|
4
6
|
import { formatLine, parseLine, plainFront } from "../cardline.js";
|
|
5
7
|
import { fold } from "../judge.js";
|
|
6
8
|
import { TRASH_DAYS } from "../library.js";
|
|
@@ -9,6 +11,9 @@ import { Choice, choiceHints } from "./Choice.js";
|
|
|
9
11
|
import { cutToWidth, html, KeyHints, Text, theme, usePanelWidth } from "./kit.js";
|
|
10
12
|
|
|
11
13
|
const SHOWN = 8; // rows of the list at a time
|
|
14
|
+
const SPIN = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
15
|
+
// a card's menu: checked first for a card written by AI help; a check by AI when it's set up
|
|
16
|
+
const menuOf = (card, ai) => [...(card.ai ? ["confirm"] : []), ...(ai && !card.cloze ? ["check"] : []), "edit", "move", "delete"];
|
|
12
17
|
|
|
13
18
|
const look = (text) => fold(String(text).toLowerCase());
|
|
14
19
|
const lineOf = (card) => (card.cloze ? plainFront(card.front) : `${card.front} = ${card.back.join("; ")}`);
|
|
@@ -19,7 +24,7 @@ const matches = (card, query) => !query || [card.front, ...card.back, card.examp
|
|
|
19
24
|
// goes to the trash, and the row on top brings it straight back. `actions`: the library's;
|
|
20
25
|
// `onDeleted`(cardIds) → { entry }, `onRestored`(entry), `onEdited`(staleKeys) let the app keep the
|
|
21
26
|
// progress in step; `onAdd` opens /add for the deck; onClose(changed).
|
|
22
|
-
export function Cards({ deck, actions, onDeleted, onRestored, onEdited, onAdd, onClose }) {
|
|
27
|
+
export function Cards({ deck, actions, onDeleted, onRestored, onEdited, onAdd, onClose, ai = null }) {
|
|
23
28
|
const panel = usePanelWidth();
|
|
24
29
|
const [cards, setCards] = useState(null);
|
|
25
30
|
const [decks, setDecks] = useState([]); // your other decks, to move a card to
|
|
@@ -31,6 +36,16 @@ export function Cards({ deck, actions, onDeleted, onRestored, onEdited, onAdd, o
|
|
|
31
36
|
const [error, setError] = useState("");
|
|
32
37
|
const changed = useRef(false);
|
|
33
38
|
const busy = useRef(false);
|
|
39
|
+
const checking = useRef(null); // the check by AI in flight: its AbortController
|
|
40
|
+
const [spin, setSpin] = useState(0);
|
|
41
|
+
const menuAt = (card, step) => menuOf(card, ai).indexOf(step);
|
|
42
|
+
const waiting = open?.step === "check" && !open.verdict;
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
if (!waiting) return;
|
|
45
|
+
const timer = setInterval(() => setSpin((n) => n + 1), 100);
|
|
46
|
+
return () => clearInterval(timer);
|
|
47
|
+
}, [waiting]);
|
|
48
|
+
useEffect(() => () => checking.current?.abort(), []);
|
|
34
49
|
|
|
35
50
|
const load = () => actions.cardsIn(deck).then(setCards, (err) => setError(err.message));
|
|
36
51
|
useEffect(() => {
|
|
@@ -38,7 +53,9 @@ export function Cards({ deck, actions, onDeleted, onRestored, onEdited, onAdd, o
|
|
|
38
53
|
actions.ownDecks().then((own) => setDecks(own.filter((d) => d.name !== deck)), () => {});
|
|
39
54
|
}, []);
|
|
40
55
|
|
|
41
|
-
|
|
56
|
+
// the cards AI wrote and you haven't checked come first, in their own order
|
|
57
|
+
const found = (cards ?? []).filter((card) => matches(card, query)).sort((a, b) => Number(Boolean(b.ai)) - Number(Boolean(a.ai)));
|
|
58
|
+
const toCheck = (cards ?? []).filter((card) => card.ai).length;
|
|
42
59
|
// the rows: bring back the card just deleted, add cards (with no search), then the cards found
|
|
43
60
|
// (both only with no search: Enter on what's found never brings a card back by accident)
|
|
44
61
|
const rows = [...(undo && !query ? [{ undo: true }] : []), ...(query ? [] : [{ add: true }]), ...found];
|
|
@@ -89,7 +106,8 @@ export function Cards({ deck, actions, onDeleted, onRestored, onEdited, onAdd, o
|
|
|
89
106
|
const fields = parseLine(text);
|
|
90
107
|
if (!fields.front) throw new Error(t("add.needFront"));
|
|
91
108
|
if (!fields.cloze && !fields.back.length) throw new Error(t("add.needBack"));
|
|
92
|
-
|
|
109
|
+
// Enter on the card as it was keeps it marked as AI's: only a change or "It's right" checks it
|
|
110
|
+
const { staleKeys } = await actions.updateCardIn(deck, card.id, { ...fields, ai: card.ai && formatLine(fields) === formatLine(card) });
|
|
93
111
|
await onEdited(staleKeys);
|
|
94
112
|
setOpen(null);
|
|
95
113
|
setMessage(t("add.fixed", { front: cutToWidth(plainFront(fields.front), 40) }));
|
|
@@ -97,6 +115,52 @@ export function Cards({ deck, actions, onDeleted, onRestored, onEdited, onAdd, o
|
|
|
97
115
|
await load();
|
|
98
116
|
});
|
|
99
117
|
|
|
118
|
+
const confirm = (card) =>
|
|
119
|
+
run(async () => {
|
|
120
|
+
await actions.confirmCardIn(deck, card.id);
|
|
121
|
+
setOpen(null);
|
|
122
|
+
setMessage(t("cards.confirmed", { card: cutToWidth(lineOf(card), 40) }));
|
|
123
|
+
changed.current = true;
|
|
124
|
+
await load();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// AI looks the card over: nothing wrong, or what is, with a fix to take or leave. One check at a time,
|
|
128
|
+
// and an answer that comes after Esc (or after the menu was left) changes nothing.
|
|
129
|
+
async function check(card) {
|
|
130
|
+
if (checking.current) return;
|
|
131
|
+
const controller = new AbortController();
|
|
132
|
+
checking.current = controller;
|
|
133
|
+
setError("");
|
|
134
|
+
setOpen((o) => ({ ...o, step: "check", verdict: null, selected: 0 }));
|
|
135
|
+
const name = providerName(ai.provider);
|
|
136
|
+
try {
|
|
137
|
+
const [languages, all] = await Promise.all([actions.languagesOf(deck).catch(() => ({})), actions.cardsIn(deck).catch(() => [])]);
|
|
138
|
+
const verdict = await checkCard(ai, { card, languages, cards: all, language: language(), signal: controller.signal });
|
|
139
|
+
if (controller.signal.aborted) return;
|
|
140
|
+
const still = (o) => o?.step === "check" && !o.verdict && o.card.id === card.id;
|
|
141
|
+
if (!verdict.ok) return setOpen((o) => (still(o) ? { ...o, verdict } : o));
|
|
142
|
+
// an AI card keeps its ✦: AI saying it's right isn't you checking it
|
|
143
|
+
setOpen((o) => (still(o) ? null : o));
|
|
144
|
+
setMessage(t("cards.checkOk", { provider: name, card: cutToWidth(lineOf(card), 40) }));
|
|
145
|
+
} catch (err) {
|
|
146
|
+
if (controller.signal.aborted) return;
|
|
147
|
+
setOpen((o) => (o?.step === "check" && !o.verdict ? { ...o, step: "menu", selected: menuAt(o.card, "check") } : o));
|
|
148
|
+
setError(err.code === "unreadable" ? t("cards.checkUnclear", { provider: name }) : err.message);
|
|
149
|
+
} finally {
|
|
150
|
+
if (checking.current === controller) checking.current = null;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const useFix = (card, fix) =>
|
|
155
|
+
run(async () => {
|
|
156
|
+
const { staleKeys } = await actions.updateCardIn(deck, card.id, fix);
|
|
157
|
+
await onEdited(staleKeys);
|
|
158
|
+
setOpen(null);
|
|
159
|
+
setMessage(t("add.fixed", { front: cutToWidth(plainFront(fix.front), 40) }));
|
|
160
|
+
changed.current = true;
|
|
161
|
+
await load();
|
|
162
|
+
});
|
|
163
|
+
|
|
100
164
|
const move = (card, to) =>
|
|
101
165
|
run(async () => {
|
|
102
166
|
const { moved } = await actions.moveCards(deck, [card.id], to);
|
|
@@ -108,6 +172,14 @@ export function Cards({ deck, actions, onDeleted, onRestored, onEdited, onAdd, o
|
|
|
108
172
|
});
|
|
109
173
|
|
|
110
174
|
useInput((input, key) => {
|
|
175
|
+
if (open?.step === "check" && !open.verdict) {
|
|
176
|
+
if (key.escape || key.leftArrow) {
|
|
177
|
+
checking.current?.abort();
|
|
178
|
+
checking.current = null;
|
|
179
|
+
setOpen((o) => ({ ...o, step: "menu", selected: menuAt(o.card, "check") }));
|
|
180
|
+
}
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
111
183
|
if (open) {
|
|
112
184
|
if (key.leftArrow && open.step === "menu") setOpen(null);
|
|
113
185
|
return;
|
|
@@ -145,7 +217,7 @@ export function Cards({ deck, actions, onDeleted, onRestored, onEdited, onAdd, o
|
|
|
145
217
|
html`
|
|
146
218
|
<${Box} flexDirection="column" marginTop=${1}>
|
|
147
219
|
<${Text} dimColor>${t("cards.editHint")}<//>
|
|
148
|
-
<${Box} marginTop=${1}><${AnswerInput} plain escLeaves cardLine initial=${formatLine(open.card)} onSubmit=${(text) => edit(open.card, text)} onExit=${() => setOpen((o) => ({ ...o, step: "menu" }))} onChange=${() => setError("")} /><//>
|
|
220
|
+
<${Box} marginTop=${1}><${AnswerInput} plain escLeaves cardLine initial=${formatLine(open.card)} onSubmit=${(text) => edit(open.card, text)} onExit=${() => setOpen((o) => ({ ...o, step: "menu", selected: menuAt(o.card, "edit") }))} onChange=${() => setError("")} /><//>
|
|
149
221
|
<//>
|
|
150
222
|
`,
|
|
151
223
|
`${t("keyHints.tabPart")} · ${t("keyHints.addFixing")}`
|
|
@@ -154,7 +226,7 @@ export function Cards({ deck, actions, onDeleted, onRestored, onEdited, onAdd, o
|
|
|
154
226
|
if (open?.step === "move")
|
|
155
227
|
return frame(
|
|
156
228
|
t("cards.moveTitle", { card: cutToWidth(lineOf(open.card), 40) }),
|
|
157
|
-
html`<${Box} marginTop=${1}>${decks.length ? html`<${Choice} options=${decks.map((d) => ({ label: d.name, note: t("count.cards", { count: d.cards }) }))} selected=${open.selected} onMove=${(i) => setOpen((o) => ({ ...o, selected: i }))} onPick=${(i) => move(open.card, decks[i].name)} onCancel=${() => setOpen((o) => ({ ...o, step: "menu", selected:
|
|
229
|
+
html`<${Box} marginTop=${1}>${decks.length ? html`<${Choice} options=${decks.map((d) => ({ label: d.name, note: t("count.cards", { count: d.cards }) }))} selected=${open.selected} onMove=${(i) => setOpen((o) => ({ ...o, selected: i }))} onPick=${(i) => move(open.card, decks[i].name)} onCancel=${() => setOpen((o) => ({ ...o, step: "menu", selected: menuAt(o.card, "move") }))} />` : html`<${Text}>${t("cards.noOtherDeck")}<//>`}<//>`,
|
|
158
230
|
choiceHints("back")
|
|
159
231
|
);
|
|
160
232
|
|
|
@@ -164,22 +236,51 @@ export function Cards({ deck, actions, onDeleted, onRestored, onEdited, onAdd, o
|
|
|
164
236
|
html`
|
|
165
237
|
<${Box} flexDirection="column">
|
|
166
238
|
<${Text} dimColor>${t("cards.deleteNote", { days: TRASH_DAYS })}<//>
|
|
167
|
-
<${Box} marginTop=${1}><${Choice} options=${[{ label: t("decks.deleteYes"), danger: true }, { label: t("cards.deleteNo") }]} selected=${open.selected} onMove=${(i) => setOpen((o) => ({ ...o, selected: i }))} onPick=${(i) => (i === 0 ? remove(open.card) : setOpen((o) => ({ ...o, step: "menu", selected:
|
|
239
|
+
<${Box} marginTop=${1}><${Choice} options=${[{ label: t("decks.deleteYes"), danger: true }, { label: t("cards.deleteNo") }]} selected=${open.selected} onMove=${(i) => setOpen((o) => ({ ...o, selected: i }))} onPick=${(i) => (i === 0 ? remove(open.card) : setOpen((o) => ({ ...o, step: "menu", selected: menuAt(o.card, "delete") })))} onCancel=${() => setOpen((o) => ({ ...o, step: "menu", selected: menuAt(o.card, "delete") }))} /><//>
|
|
168
240
|
<//>
|
|
169
241
|
`,
|
|
170
242
|
choiceHints("cancel"),
|
|
171
243
|
theme.bad
|
|
172
244
|
);
|
|
173
245
|
|
|
246
|
+
if (open?.step === "check") {
|
|
247
|
+
const name = providerName(ai.provider);
|
|
248
|
+
const { verdict } = open;
|
|
249
|
+
const back = () => setOpen((o) => ({ ...o, step: "menu", verdict: null, selected: menuAt(o.card, "check") }));
|
|
250
|
+
return frame(
|
|
251
|
+
cutToWidth(lineOf(open.card), panel - 8),
|
|
252
|
+
verdict
|
|
253
|
+
? html`
|
|
254
|
+
<${Box} flexDirection="column">
|
|
255
|
+
${open.card.example && html`<${Text} dimColor italic>“${open.card.example}”<//>`}
|
|
256
|
+
<${Box} marginTop=${1}><${Box} flexShrink=${0}><${Text} color=${theme.accent}>✦ <//><//><${Text} color=${theme.accent}>${verdict.note || t("cards.checkFix", { provider: name })}<//><//>
|
|
257
|
+
<${Box} marginTop=${1} flexDirection="column" paddingLeft=${2}>
|
|
258
|
+
${verdict.fix && html`<${Fixed} card=${open.card} fix=${verdict.fix} />`}
|
|
259
|
+
<//>
|
|
260
|
+
<${Box} marginTop=${1}><${Choice} options=${verdict.fix ? [{ label: t("cards.useFix") }, { label: t("cards.keepMine") }] : [{ label: t("cards.keepMine") }]} selected=${open.selected} onMove=${(i) => setOpen((o) => ({ ...o, selected: i }))} onPick=${(i) => (verdict.fix && i === 0 ? useFix(open.card, verdict.fix) : back())} onCancel=${back} /><//>
|
|
261
|
+
<//>
|
|
262
|
+
`
|
|
263
|
+
: html`
|
|
264
|
+
<${Box} flexDirection="column">
|
|
265
|
+
${open.card.example && html`<${Text} dimColor italic>“${open.card.example}”<//>`}
|
|
266
|
+
<${Box} marginTop=${1}><${Text} color=${theme.accent}>${SPIN[spin % SPIN.length]} ${t("cards.checking", { provider: name })}<//><//>
|
|
267
|
+
<//>
|
|
268
|
+
`,
|
|
269
|
+
verdict ? choiceHints("back") : t("keyHints.aiWaiting")
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
|
|
174
273
|
if (open?.step === "menu") {
|
|
175
|
-
const
|
|
176
|
-
const
|
|
274
|
+
const steps = menuOf(open.card, ai);
|
|
275
|
+
const labels = { confirm: t("cards.confirmRow"), check: t("cards.checkRow"), edit: t("cards.editRow"), move: t("cards.moveRow"), delete: t("cards.deleteRow") };
|
|
276
|
+
const options = steps.map((step) => ({ label: labels[step], danger: step === "delete" }));
|
|
177
277
|
return frame(
|
|
178
278
|
cutToWidth(lineOf(open.card), panel - 8),
|
|
179
279
|
html`
|
|
180
280
|
<${Box} flexDirection="column">
|
|
181
281
|
${open.card.example && html`<${Text} dimColor italic>“${open.card.example}”<//>`}
|
|
182
|
-
|
|
282
|
+
${open.card.ai && html`<${Box} marginTop=${1}><${Text} color=${theme.accent}>${t("cards.aiNote")}<//><//>`}
|
|
283
|
+
<${Box} marginTop=${1}><${Choice} options=${options} selected=${open.selected} onMove=${(i) => setOpen((o) => ({ ...o, selected: i }))} onPick=${(i) => (steps[i] === "confirm" ? confirm(open.card) : steps[i] === "check" ? check(open.card) : setOpen((o) => ({ ...o, step: steps[i], selected: steps[i] === "delete" ? 1 : 0 })))} onCancel=${() => setOpen(null)} /><//>
|
|
183
284
|
<//>
|
|
184
285
|
`,
|
|
185
286
|
t("keyHints.choiceLeft", { choose: t("choice.keys") })
|
|
@@ -188,9 +289,10 @@ export function Cards({ deck, actions, onDeleted, onRestored, onEdited, onAdd, o
|
|
|
188
289
|
|
|
189
290
|
// the list, with the search on top
|
|
190
291
|
const top = Math.max(0, Math.min(selected - Math.floor(SHOWN / 2), rows.length - SHOWN));
|
|
292
|
+
const gutter = (cards ?? []).some((card) => card.ai); // room for a ✦ before every card, so they line up
|
|
191
293
|
const rowText = (row) => (row.undo ? t("cards.undoRow", { card: cutToWidth(lineOf(row.undo === true ? undo.card : row), 40) }) : row.add ? t("cards.addRow") : lineOf(row));
|
|
192
294
|
return frame(
|
|
193
|
-
t("cards.title", { deck, count: cards?.length ?? 0 }),
|
|
295
|
+
html`<${Fragment}>${t("cards.title", { deck, count: cards?.length ?? 0 })}${toCheck > 0 ? html`<${Text} bold=${false}> ${t("cards.toCheck", { count: toCheck })}<//>` : ""}<//>`,
|
|
194
296
|
html`
|
|
195
297
|
<${Box} flexDirection="column">
|
|
196
298
|
<${Box} marginTop=${1}>
|
|
@@ -202,7 +304,7 @@ export function Cards({ deck, actions, onDeleted, onRestored, onEdited, onAdd, o
|
|
|
202
304
|
<${Text} dimColor>${top > 0 ? " ↑" : " "}<//>
|
|
203
305
|
${rows.slice(top, top + SHOWN).map((row, i) => {
|
|
204
306
|
const on = top + i === selected;
|
|
205
|
-
return html`<${Text} key=${row.id ?? (row.undo ? "undo" : "add")} color=${on ? theme.accent : row.undo || row.add ? theme.good : undefined} bold=${on} wrap="truncate-end">${on ? "❯" : " "} ${rowText(row)}<//>`;
|
|
307
|
+
return html`<${Text} key=${row.id ?? (row.undo ? "undo" : "add")} color=${on ? theme.accent : row.undo || row.add ? theme.good : undefined} bold=${on} wrap="truncate-end">${on ? "❯" : " "} ${row.ai ? html`<${Text} color=${theme.accent}>✦ <//>` : gutter && row.id ? " " : ""}${rowText(row)}<//>`;
|
|
206
308
|
})}
|
|
207
309
|
<${Text} dimColor>${top + SHOWN < rows.length ? " ↓" : " "}<//>
|
|
208
310
|
${query && found.length > 0 && html`<${Text} dimColor>${t("cards.found", { count: found.length })}<//>`}
|
|
@@ -212,6 +314,18 @@ export function Cards({ deck, actions, onDeleted, onRestored, onEdited, onAdd, o
|
|
|
212
314
|
);
|
|
213
315
|
}
|
|
214
316
|
|
|
317
|
+
// the card as AI would fix it, what it changes in the accent colour
|
|
318
|
+
function Fixed({ card, fix }) {
|
|
319
|
+
const changed = (on) => (on ? theme.accent : undefined);
|
|
320
|
+
const parts = fix.back.map((answer, i) => html`<${Text} key=${i} color=${changed(!card.back.includes(answer))}>${i ? "; " : ""}${answer}<//>`);
|
|
321
|
+
return html`
|
|
322
|
+
<${Box} flexDirection="column">
|
|
323
|
+
<${Text} bold wrap="truncate-end"><${Text} color=${changed(fix.front !== card.front)}>${fix.front}<//> = ${parts}<//>
|
|
324
|
+
${fix.example && html`<${Text} italic color=${changed(fix.example !== (card.example ?? ""))} dimColor=${fix.example === (card.example ?? "")}>“${fix.example}”<//>`}
|
|
325
|
+
<//>
|
|
326
|
+
`;
|
|
327
|
+
}
|
|
328
|
+
|
|
215
329
|
// /cards while several decks are practised together: which deck's cards (`mix`: "all" or names)
|
|
216
330
|
export function CardsDeckPick({ actions, mix, current, onPick, onClose }) {
|
|
217
331
|
const panel = usePanelWidth();
|
package/src/ui/Settings.js
CHANGED
|
@@ -3,6 +3,8 @@ import { Box, useInput } from "ink";
|
|
|
3
3
|
import { language, LANGUAGES, t } from "../i18n.js";
|
|
4
4
|
import { GOALS, INTERVALS, QUIET_PRESETS, RHYTHMS, TIP_MODES, VOLUMES, formatInterval } from "../settings.js";
|
|
5
5
|
import { formatQuiet } from "../quiet.js";
|
|
6
|
+
import { aiCostLabel } from "../ai.js";
|
|
7
|
+
import { voiceLabel } from "../voice.js";
|
|
6
8
|
import { languageName } from "../language.js";
|
|
7
9
|
import { elementName } from "../progress.js";
|
|
8
10
|
import { Bar, html, KeyHints, Text, theme, usePanelWidth } from "./kit.js";
|
|
@@ -15,10 +17,12 @@ function VolumeValue({ value }) {
|
|
|
15
17
|
const ALL_ROWS = [
|
|
16
18
|
{ key: "language", options: Object.keys(LANGUAGES), format: (code) => LANGUAGES[code] },
|
|
17
19
|
{ key: "homeLanguage", action: true, format: (code) => languageName(code) ?? t("settings.notSet") },
|
|
20
|
+
{ key: "ai", action: true, format: aiCostLabel },
|
|
18
21
|
{ key: "rhythm", options: RHYTHMS, format: (v) => t(`settings.rhythms.${v}`) },
|
|
19
22
|
{ key: "everyMs", options: INTERVALS, format: formatInterval },
|
|
20
23
|
{ key: "sound", options: [true, false], format: (v) => t(v ? "settings.on" : "settings.off"), toggle: true },
|
|
21
24
|
{ key: "speak", options: [false, true], format: (v) => t(v ? "settings.on" : "settings.off"), toggle: true },
|
|
25
|
+
{ key: "voice", action: true, format: voiceLabel },
|
|
22
26
|
{ key: "volume", options: VOLUMES, format: (v) => html`<${VolumeValue} value=${v} />` },
|
|
23
27
|
{ key: "dailyGoal", options: GOALS, format: (n) => t("settings.goal", { n }) },
|
|
24
28
|
{ key: "tips", options: TIP_MODES, format: (v) => t(`settings.tipModes.${v}`) },
|
|
@@ -76,7 +80,7 @@ export function Settings({ settings, element = null, onChange, onClose, onAction
|
|
|
76
80
|
(item, i) => html`
|
|
77
81
|
<${Box} key=${item.key}>
|
|
78
82
|
<${Box} flexShrink=${0}><${Text} color=${i === row ? theme.accent : undefined} bold=${i === row}>${i === row ? "❯" : " "} ${label(item).padEnd(labelWidth)}<//><//>
|
|
79
|
-
<${Text} color=${i === row ? theme.accent : undefined}>${i === row && !item.action ? "❮ " : " "}${item.format(values[item.key])}${i === row && !item.action ? " ❯" : ""}<//>
|
|
83
|
+
<${Text} wrap="truncate-end" color=${i === row ? theme.accent : undefined}>${i === row && !item.action ? "❮ " : " "}${item.format(values[item.key])}${i === row && !item.action ? " ❯" : ""}<//>
|
|
80
84
|
<//>
|
|
81
85
|
`
|
|
82
86
|
)}
|
package/src/ui/Voice.js
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { Fragment, useRef, useState } from "react";
|
|
2
|
+
import { Box, useInput } from "ink";
|
|
3
|
+
import { language, t } from "../i18n.js";
|
|
4
|
+
import { forgetKey, maskKey, saveKey } from "../ai.js";
|
|
5
|
+
import { ELEVENLABS, findVoiceKey, listVoices, speech, voiceLabel } from "../voice.js";
|
|
6
|
+
import { fold } from "../judge.js";
|
|
7
|
+
import { AnswerInput } from "./AnswerInput.js";
|
|
8
|
+
import { Choice, choiceHints } from "./Choice.js";
|
|
9
|
+
import { cutToWidth, html, KeyHints, Text, theme, usePanelWidth } from "./kit.js";
|
|
10
|
+
|
|
11
|
+
const SHOWN = 8; // voices listed at a time
|
|
12
|
+
const look = (text) => fold(text.toLowerCase());
|
|
13
|
+
const SHORT = ["noKey", "badKey", "noCredit", "noVoice"];
|
|
14
|
+
|
|
15
|
+
// /settings → Voice: the system's voices (free, built in) or a natural one from ElevenLabs with your own
|
|
16
|
+
// key. For ElevenLabs: the key, the voice, then a short sample read in it; only then is anything saved.
|
|
17
|
+
// `current`: settings.voice. onSave(voice | null) keeps the choice. `play(file)`: plays the sample.
|
|
18
|
+
// `speakOn`: Read aloud is on already (picking a voice turns it on). Esc goes back a step, from the
|
|
19
|
+
// first one it leaves.
|
|
20
|
+
export function VoiceSetup({ current, onSave, onClose, play = () => {}, speakOn = true }) {
|
|
21
|
+
const panel = usePanelWidth();
|
|
22
|
+
const [step, setStep] = useState("provider");
|
|
23
|
+
const [selected, setSelected] = useState(current ? 1 : 0);
|
|
24
|
+
const [key, setKey] = useState(null); // { key, from: "env" | "saved" | "typed", env? }
|
|
25
|
+
const [voices, setVoices] = useState(null);
|
|
26
|
+
const [voice, setVoice] = useState(null);
|
|
27
|
+
const [query, setQuery] = useState("");
|
|
28
|
+
const [error, setError] = useState("");
|
|
29
|
+
const [message, setMessage] = useState("");
|
|
30
|
+
const history = useRef([]);
|
|
31
|
+
const job = useRef(0); // a newer step makes an older request's answer stale
|
|
32
|
+
const request = useRef(null); // the request in flight, cancelled when it goes stale (a sample is billed)
|
|
33
|
+
|
|
34
|
+
const go = (next, pick = 0) => {
|
|
35
|
+
const at = history.current.indexOf(next);
|
|
36
|
+
if (at >= 0) history.current = history.current.slice(0, at);
|
|
37
|
+
else if (step !== next && step !== "checking" && step !== "failed") history.current.push(step);
|
|
38
|
+
setError("");
|
|
39
|
+
setMessage("");
|
|
40
|
+
setSelected(pick);
|
|
41
|
+
setStep(next);
|
|
42
|
+
};
|
|
43
|
+
const back = () => {
|
|
44
|
+
job.current += 1;
|
|
45
|
+
request.current?.abort();
|
|
46
|
+
setError("");
|
|
47
|
+
setMessage("");
|
|
48
|
+
const last = history.current.pop();
|
|
49
|
+
if (!last) return onClose();
|
|
50
|
+
setSelected(last === "provider" ? 1 : 0);
|
|
51
|
+
setStep(last);
|
|
52
|
+
};
|
|
53
|
+
const stillOn = () => {
|
|
54
|
+
const mine = ++job.current;
|
|
55
|
+
request.current?.abort();
|
|
56
|
+
request.current = new AbortController();
|
|
57
|
+
return () => mine === job.current;
|
|
58
|
+
};
|
|
59
|
+
// here the message says what's wrong, not where to fix it (this is the place)
|
|
60
|
+
const said = (err) => (SHORT.includes(err.code) ? t(`voice.short.${err.code}`, { env: ELEVENLABS.keyEnv }) : err.message);
|
|
61
|
+
|
|
62
|
+
async function pickProvider(index) {
|
|
63
|
+
if (index === 0) {
|
|
64
|
+
if (current) onSave(null);
|
|
65
|
+
return onClose(current ? t("voice.systemSet") : undefined);
|
|
66
|
+
}
|
|
67
|
+
const fresh = stillOn();
|
|
68
|
+
const found = await findVoiceKey();
|
|
69
|
+
if (!fresh()) return;
|
|
70
|
+
setKey(found);
|
|
71
|
+
if (found?.from === "env") return loadVoices(found);
|
|
72
|
+
go(found ? "keep-key" : "key");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function loadVoices(withKey = key) {
|
|
76
|
+
go("voices");
|
|
77
|
+
const fresh = stillOn();
|
|
78
|
+
setVoices(null);
|
|
79
|
+
setQuery("");
|
|
80
|
+
try {
|
|
81
|
+
const list = await listVoices({ key: withKey?.key ?? null, url: current?.url, signal: request.current.signal });
|
|
82
|
+
if (!fresh()) return;
|
|
83
|
+
setVoices(list);
|
|
84
|
+
setSelected(Math.max(0, list.findIndex((item) => item.id === current?.id)));
|
|
85
|
+
} catch (err) {
|
|
86
|
+
if (!fresh()) return;
|
|
87
|
+
setVoices([]);
|
|
88
|
+
if (["badKey", "noKey"].includes(err.code) && withKey?.from !== "env") {
|
|
89
|
+
const at = history.current.indexOf("key");
|
|
90
|
+
if (at >= 0) history.current = history.current.slice(0, at);
|
|
91
|
+
setSelected(0);
|
|
92
|
+
setStep("key");
|
|
93
|
+
}
|
|
94
|
+
setError(said(err));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function check(picked) {
|
|
99
|
+
const chosen = { provider: "elevenlabs", id: picked.id, name: picked.name, ...(current?.url ? { url: current.url } : {}) };
|
|
100
|
+
setVoice(chosen);
|
|
101
|
+
go("checking");
|
|
102
|
+
const fresh = stillOn();
|
|
103
|
+
try {
|
|
104
|
+
const file = await speech(chosen, t("voice.sample"), language(), { key: key?.key ?? null, signal: request.current.signal });
|
|
105
|
+
if (!fresh()) return;
|
|
106
|
+
play(file);
|
|
107
|
+
if (key?.from === "typed") await saveKey("elevenlabs", key.key);
|
|
108
|
+
onSave(chosen);
|
|
109
|
+
onClose(t(speakOn ? "voice.ready" : "voice.readyOn", { label: voiceLabel(chosen) }));
|
|
110
|
+
} catch (err) {
|
|
111
|
+
if (!fresh()) return;
|
|
112
|
+
setError(said(err));
|
|
113
|
+
setStep("failed");
|
|
114
|
+
setSelected(0);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const found = (voices ?? []).filter((item) => !query || look(`${item.name} ${item.note}`).includes(look(query)));
|
|
119
|
+
|
|
120
|
+
useInput((input, k) => {
|
|
121
|
+
if ((step === "checking" || (step === "voices" && voices === null)) && k.escape) return back();
|
|
122
|
+
if (step !== "voices" || voices === null) return;
|
|
123
|
+
if (k.escape) return query ? (setQuery(""), setSelected(0)) : back();
|
|
124
|
+
if (k.upArrow) return setSelected((i) => Math.max(0, i - 1));
|
|
125
|
+
if (k.downArrow) return setSelected((i) => Math.min(found.length - 1, i + 1));
|
|
126
|
+
if (k.return) return found[selected] && check(found[selected]);
|
|
127
|
+
if (k.backspace || k.delete) return query && (setQuery((q) => q.slice(0, -1)), setSelected(0));
|
|
128
|
+
const text = input.replace(/[\u0000-\u001f\u007f]/g, "");
|
|
129
|
+
if (text && !k.ctrl && !k.meta) {
|
|
130
|
+
setQuery((q) => q + text);
|
|
131
|
+
setSelected(0);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const problem = error && html`<${Box} marginTop=${1}><${Text} color=${theme.bad}>${error}<//><//>`;
|
|
136
|
+
const frame = (title, body, hints, { errorAbove = false } = {}) => html`
|
|
137
|
+
<${Fragment}>
|
|
138
|
+
<${Box} flexDirection="column" borderStyle="round" borderColor=${theme.accent} paddingX=${2} width=${panel}>
|
|
139
|
+
<${Text} bold color=${theme.accent}>${title}<//>
|
|
140
|
+
${errorAbove && problem}
|
|
141
|
+
${body}
|
|
142
|
+
${message && html`<${Box} marginTop=${1}><${Text} color=${theme.good}>${message}<//><//>`}
|
|
143
|
+
${!errorAbove && problem}
|
|
144
|
+
<//>
|
|
145
|
+
<${Box} paddingX=${1}><${KeyHints} text=${hints} /><//>
|
|
146
|
+
<//>
|
|
147
|
+
`;
|
|
148
|
+
|
|
149
|
+
if (step === "provider") {
|
|
150
|
+
const options = [{ label: t("voice.system"), note: t("voice.systemNote") }, { label: "ElevenLabs", note: t("voice.elevenNote") }];
|
|
151
|
+
return frame(
|
|
152
|
+
t("voice.title"),
|
|
153
|
+
html`
|
|
154
|
+
<${Box} flexDirection="column">
|
|
155
|
+
<${Text} dimColor>${t("voice.note")}<//>
|
|
156
|
+
<${Box} marginTop=${1}><${Text} dimColor>${t("voice.now", { label: voiceLabel(current) })}<//><//>
|
|
157
|
+
<${Box} marginTop=${1}><${Choice} options=${options} selected=${selected} onMove=${setSelected} onPick=${(i) => pickProvider(i).catch((err) => setError(err.message))} onCancel=${() => onClose()} /><//>
|
|
158
|
+
<//>
|
|
159
|
+
`,
|
|
160
|
+
choiceHints("back")
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (step === "keep-key") {
|
|
165
|
+
const options = [{ label: t("ai.keepKey"), note: maskKey(key.key) }, { label: t("ai.newKey") }, { label: t("ai.forgetKey"), danger: true }];
|
|
166
|
+
return frame(
|
|
167
|
+
t("voice.keyTitle"),
|
|
168
|
+
html`<${Box} marginTop=${1}><${Choice} options=${options} selected=${selected} onMove=${setSelected} onPick=${(i) => {
|
|
169
|
+
if (i === 0) return loadVoices();
|
|
170
|
+
if (i === 1) return go("key");
|
|
171
|
+
forgetKey("elevenlabs").then(() => {
|
|
172
|
+
setKey(null);
|
|
173
|
+
go("key");
|
|
174
|
+
setMessage(t("ai.keyForgotten", { provider: "ElevenLabs" }));
|
|
175
|
+
}, (err) => setError(err.message));
|
|
176
|
+
}} onCancel=${back} /><//>`,
|
|
177
|
+
choiceHints("back")
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (step === "key")
|
|
182
|
+
return frame(
|
|
183
|
+
t("voice.keyTitle"),
|
|
184
|
+
html`
|
|
185
|
+
<${Box} flexDirection="column">
|
|
186
|
+
<${Text} dimColor>${t("voice.keyNote", { env: ELEVENLABS.keyEnv, site: ELEVENLABS.site })}<//>
|
|
187
|
+
<${Box} marginTop=${1}><${AnswerInput} key="voice-key" plain secret escLeaves onChange=${() => (setError(""), setMessage(""))} onExit=${back} onSubmit=${(text) => {
|
|
188
|
+
const typed = text.trim();
|
|
189
|
+
if (!typed) return setError(t("ai.needKey"));
|
|
190
|
+
const next = { key: typed, from: "typed" };
|
|
191
|
+
setKey(next);
|
|
192
|
+
loadVoices(next);
|
|
193
|
+
}} /><//>
|
|
194
|
+
<//>
|
|
195
|
+
`,
|
|
196
|
+
t("keyHints.importName")
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
if (step === "voices") {
|
|
200
|
+
const top = Math.max(0, Math.min(selected - Math.floor(SHOWN / 2), found.length - SHOWN));
|
|
201
|
+
return frame(
|
|
202
|
+
t("voice.pickTitle"),
|
|
203
|
+
html`
|
|
204
|
+
<${Box} flexDirection="column">
|
|
205
|
+
<${Text} dimColor>${[key?.from === "env" && t("ai.keyFromEnv", { env: key.env }), t("voice.pickNote")].filter(Boolean).join(" ")}<//>
|
|
206
|
+
${voices?.length === 0 && !error && html`<${Box} marginTop=${1}><${Text}>${t("voice.noVoices")}<//><//>`}
|
|
207
|
+
${voices === null
|
|
208
|
+
? html`<${Box} marginTop=${1}><${Text} dimColor>${t("voice.loading")}<//><//>`
|
|
209
|
+
: voices.length > 0 && html`
|
|
210
|
+
<${Box} flexDirection="column" marginTop=${1}>
|
|
211
|
+
${(voices.length > SHOWN || query) && html`<${Box}><${Box} flexShrink=${0}><${Text} dimColor>${t("languages.search")} <//><//><${Text} wrap="truncate-start">${query}<//><${Box} flexShrink=${0}><${Text} inverse> <//><//><//>`}
|
|
212
|
+
<${Text} dimColor>${top > 0 ? " ↑" : " "}<//>
|
|
213
|
+
${found.slice(top, top + SHOWN).map((item, i) => {
|
|
214
|
+
const on = top + i === selected;
|
|
215
|
+
const extra = item.id === current?.id ? t("voice.current") : item.note;
|
|
216
|
+
return html`<${Text} key=${item.id} color=${on ? theme.accent : undefined} bold=${on} wrap="truncate-end">${on ? "❯" : " "} ${cutToWidth(item.name, panel - 12)} ${extra ? html`<${Text} dimColor>· ${extra}<//>` : ""}<//>`;
|
|
217
|
+
})}
|
|
218
|
+
<${Text} dimColor>${top + SHOWN < found.length ? " ↓" : " "}<//>
|
|
219
|
+
<//>
|
|
220
|
+
`}
|
|
221
|
+
<//>
|
|
222
|
+
`,
|
|
223
|
+
voices && voices.length > SHOWN ? t("keyHints.modelPick") : choiceHints("back")
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (step === "checking")
|
|
228
|
+
return frame(t("voice.pickTitle"), html`<${Box} marginTop=${1}><${Text} dimColor>${t("voice.checking", { label: voiceLabel(voice) })}<//><//>`, t("keyHints.back"));
|
|
229
|
+
|
|
230
|
+
// failed: what now (a key from the environment is changed there, not here)
|
|
231
|
+
const options = [{ label: t("ai.tryAgain") }, { label: t("voice.otherVoice") }, ...(key?.from === "env" ? [] : [{ label: t("ai.otherKey") }])];
|
|
232
|
+
return frame(
|
|
233
|
+
t("voice.failedTitle", { label: voiceLabel(voice) }),
|
|
234
|
+
html`<${Box} marginTop=${1}><${Choice} options=${options} selected=${selected} onMove=${setSelected} onPick=${(i) => (i === 0 ? check(voice) : i === 1 ? loadVoices() : go("key"))} onCancel=${back} /><//>`,
|
|
235
|
+
choiceHints("back"),
|
|
236
|
+
{ errorAbove: true }
|
|
237
|
+
);
|
|
238
|
+
}
|
package/src/ui/Welcome.js
CHANGED
|
@@ -8,13 +8,13 @@ import { html, KeyHints, Text, theme, usePanelWidth } from "./kit.js";
|
|
|
8
8
|
import { LanguageSearch } from "./Languages.js";
|
|
9
9
|
|
|
10
10
|
export const WELCOME_STEPS = ["language", "home", "learning", "start"];
|
|
11
|
-
const FIRST_WORDS_COUNT = 30;
|
|
11
|
+
export const FIRST_WORDS_COUNT = 30;
|
|
12
12
|
|
|
13
13
|
// After the hatching, before the first question: the app's language, the one you speak, the one you're
|
|
14
14
|
// learning, and a way to start. Every answer is kept as it's given (onStep), so a welcome left halfway
|
|
15
15
|
// goes on from that step. `onPreviewLanguage` shows the texts in a language while it's only selected;
|
|
16
|
-
// onStart(how): "words" (the first words of the pair), "
|
|
17
|
-
// "own" (a deck of your own, in /add).
|
|
16
|
+
// onStart(how): "words" (the first words of the pair), "ai" (a list from AI help, set up first if it
|
|
17
|
+
// isn't), "tour", "import" (cards you have, /import) or "own" (a deck of your own, in /add).
|
|
18
18
|
// `tour`: whether there's still a tour to show (a deck of your own may have taken its place).
|
|
19
19
|
export function Welcome({ step, settings, tour = true, onPreviewLanguage, onStep, onStart }) {
|
|
20
20
|
const panel = usePanelWidth();
|
|
@@ -40,6 +40,7 @@ export function Welcome({ step, settings, tour = true, onPreviewLanguage, onStep
|
|
|
40
40
|
const words = learning && hasFirstWords(home, learning);
|
|
41
41
|
const options = [
|
|
42
42
|
words && { id: "words", label: t("welcome.start.words", { count: FIRST_WORDS_COUNT }), note: t("welcome.start.wordsNote", { language: languageName(learning), home: languageName(home) }) },
|
|
43
|
+
learning && { id: "ai", label: t("welcome.start.ai"), note: t("welcome.start.aiNote") },
|
|
43
44
|
tour && { id: "tour", label: t("welcome.start.tour"), note: t("welcome.start.tourNote", { count: entry("tour.cards").length }) },
|
|
44
45
|
{ id: "import", label: t("welcome.start.import"), note: t("welcome.start.importNote") },
|
|
45
46
|
{ id: "own", label: t("welcome.start.own"), note: t("welcome.start.ownNote") }
|