@minhluudev/ai-tools 0.1.1 → 1.0.2

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.
@@ -0,0 +1,269 @@
1
+ import { canRun, createTabbedLayout, DEBOUNCE_MOVE_MS, DEBOUNCE_SELECT_MS, renderBannerHeader } from "./tui-utils.js";
2
+ const TAB_ORDER = ["skills", "review"];
3
+ function buildInitialState() {
4
+ return {
5
+ activeTab: "skills",
6
+ selectedSkills: new Set(),
7
+ listCursor: {
8
+ skills: 0,
9
+ review: 0
10
+ },
11
+ reviewAction: "confirm",
12
+ notice: ""
13
+ };
14
+ }
15
+ function currentTabItems(state, catalog) {
16
+ if (state.activeTab === "skills") {
17
+ return catalog.skills.map((skill) => ({
18
+ id: skill.id,
19
+ label: skill.label,
20
+ description: skill.description,
21
+ kind: "skills"
22
+ }));
23
+ }
24
+ return [
25
+ {
26
+ id: "confirm",
27
+ label: "Confirm install",
28
+ description: "Continue with the selected skills.",
29
+ kind: "review"
30
+ },
31
+ {
32
+ id: "back",
33
+ label: "Back to main menu",
34
+ description: "Leave this installer and return to the main menu.",
35
+ kind: "review"
36
+ },
37
+ {
38
+ id: "cancel",
39
+ label: "Cancel",
40
+ description: "Exit without installing anything.",
41
+ kind: "review"
42
+ }
43
+ ];
44
+ }
45
+ function renderTabs(state) {
46
+ return TAB_ORDER.map((tab) => {
47
+ const label = tab.charAt(0).toUpperCase() + tab.slice(1);
48
+ return state.activeTab === tab ? `{inverse}${label}{/inverse}` : label;
49
+ }).join(" ");
50
+ }
51
+ function isItemSelected(item, state) {
52
+ if (item.kind === "skills") {
53
+ return state.selectedSkills.has(item.id);
54
+ }
55
+ return state.reviewAction === item.id;
56
+ }
57
+ function toggleItem(item, state) {
58
+ if (item.kind === "skills") {
59
+ if (state.selectedSkills.has(item.id)) {
60
+ state.selectedSkills.delete(item.id);
61
+ }
62
+ else {
63
+ state.selectedSkills.add(item.id);
64
+ }
65
+ return;
66
+ }
67
+ state.reviewAction = item.id;
68
+ }
69
+ function formatListItem(item, state, isCursorRow) {
70
+ const cursorPrefix = isCursorRow ? "> " : " ";
71
+ const wrapCursorRow = (value) => isCursorRow ? `{black-fg}{yellow-bg}${value}{/yellow-bg}{/black-fg}` : value;
72
+ if (item.kind === "review") {
73
+ return wrapCursorRow(`${cursorPrefix}${state.reviewAction === item.id ? "[x]" : "[ ]"} ${item.label}`);
74
+ }
75
+ const checked = isItemSelected(item, state) ? "x" : " ";
76
+ return wrapCursorRow(`${cursorPrefix}[${checked}] ${item.label}`);
77
+ }
78
+ function renderReviewSummary(state, catalog) {
79
+ const selectedSkills = catalog.skills
80
+ .filter((skill) => state.selectedSkills.has(skill.id))
81
+ .map((skill) => skill.label);
82
+ const lines = [
83
+ "{bold}Selections{/bold}",
84
+ `Skills: ${selectedSkills.length ? selectedSkills.join(", ") : "none"}`
85
+ ];
86
+ if (state.notice) {
87
+ lines.push("", `{yellow-fg}${state.notice}{/yellow-fg}`);
88
+ }
89
+ return lines.join("\n");
90
+ }
91
+ function renderDetailBody(state, catalog) {
92
+ if (state.activeTab === "review") {
93
+ return renderReviewSummary(state, catalog);
94
+ }
95
+ const items = currentTabItems(state, catalog);
96
+ const current = items[state.listCursor[state.activeTab]];
97
+ const lines = [];
98
+ if (state.notice) {
99
+ lines.push(`{yellow-fg}${state.notice}{/yellow-fg}`, "");
100
+ }
101
+ if (current) {
102
+ lines.push("{bold}Details{/bold}", current.description);
103
+ }
104
+ return lines.join("\n");
105
+ }
106
+ function renderFooter(state) {
107
+ if (state.activeTab === "review") {
108
+ return "← → switch tab • ↑ ↓ move • enter confirm action • q quit";
109
+ }
110
+ return "← → switch tab • ↑ ↓ move • space/enter toggle • a toggle all • q quit";
111
+ }
112
+ function moveTab(state, delta) {
113
+ const index = TAB_ORDER.indexOf(state.activeTab);
114
+ const nextIndex = (index + delta + TAB_ORDER.length) % TAB_ORDER.length;
115
+ state.activeTab = TAB_ORDER[nextIndex];
116
+ state.notice = "";
117
+ }
118
+ function moveCursor(state, catalog, delta) {
119
+ const items = currentTabItems(state, catalog);
120
+ if (items.length === 0) {
121
+ return;
122
+ }
123
+ const current = state.listCursor[state.activeTab];
124
+ const next = (current + delta + items.length) % items.length;
125
+ state.listCursor[state.activeTab] = next;
126
+ if (state.activeTab === "review") {
127
+ state.reviewAction = items[next]?.id;
128
+ }
129
+ }
130
+ function validateBeforeConfirm(state) {
131
+ if (state.selectedSkills.size === 0) {
132
+ return "Select at least one skill before confirming.";
133
+ }
134
+ return null;
135
+ }
136
+ function syncListSelection(listBox, state, catalog) {
137
+ const items = currentTabItems(state, catalog);
138
+ const maxIndex = Math.max(items.length - 1, 0);
139
+ const cursor = Math.min(state.listCursor[state.activeTab], maxIndex);
140
+ state.listCursor[state.activeTab] = cursor;
141
+ listBox.setItems(items.map((item, index) => formatListItem(item, state, index === cursor)));
142
+ listBox.select(cursor);
143
+ listBox.scrollTo(cursor);
144
+ }
145
+ export async function runProjectDocsWizard(catalog) {
146
+ return new Promise((resolve, reject) => {
147
+ const state = buildInitialState();
148
+ let lastTabSwitchAt = 0;
149
+ let lastMoveAt = 0;
150
+ let lastToggleAt = 0;
151
+ const { screen, headerBox, tabsBox, titleBox, listBox, detailBox, footerBox } = createTabbedLayout();
152
+ function cleanup() {
153
+ screen.destroy();
154
+ }
155
+ function render() {
156
+ const stepTitle = state.activeTab === "skills" ? "Select skills" : "Review selections";
157
+ headerBox.setContent(renderBannerHeader("Install project docs", "Choose skills first, then confirm from review."));
158
+ tabsBox.setContent(renderTabs(state));
159
+ titleBox.setContent(`{bold}${stepTitle}{/bold}`);
160
+ syncListSelection(listBox, state, catalog);
161
+ detailBox.setContent(renderDetailBody(state, catalog));
162
+ footerBox.setContent(renderFooter(state));
163
+ screen.render();
164
+ }
165
+ function finishBackToMenu() {
166
+ cleanup();
167
+ resolve({ backToMenu: true });
168
+ }
169
+ function finishSuccess() {
170
+ cleanup();
171
+ resolve({
172
+ selectedSkills: catalog.skills.filter((skill) => state.selectedSkills.has(skill.id))
173
+ });
174
+ }
175
+ function finishWithError(message) {
176
+ cleanup();
177
+ reject(new Error(message));
178
+ }
179
+ function handleToggleOrConfirm() {
180
+ if (!canRun(lastToggleAt, DEBOUNCE_SELECT_MS)) {
181
+ return;
182
+ }
183
+ lastToggleAt = Date.now();
184
+ if (state.activeTab === "review") {
185
+ if (state.reviewAction === "back") {
186
+ finishBackToMenu();
187
+ return;
188
+ }
189
+ if (state.reviewAction === "cancel") {
190
+ finishWithError("Installation cancelled.");
191
+ return;
192
+ }
193
+ const validationMessage = validateBeforeConfirm(state);
194
+ if (validationMessage) {
195
+ state.notice = validationMessage;
196
+ render();
197
+ return;
198
+ }
199
+ finishSuccess();
200
+ return;
201
+ }
202
+ const items = currentTabItems(state, catalog);
203
+ const current = items[state.listCursor.skills];
204
+ if (current) {
205
+ toggleItem(current, state);
206
+ }
207
+ render();
208
+ }
209
+ function toggleAllSkills() {
210
+ if (state.activeTab !== "skills") {
211
+ return;
212
+ }
213
+ const items = currentTabItems(state, catalog);
214
+ const allSelected = items.every((item) => state.selectedSkills.has(item.id));
215
+ for (const item of items) {
216
+ const selected = state.selectedSkills.has(item.id);
217
+ if (allSelected && selected) {
218
+ state.selectedSkills.delete(item.id);
219
+ }
220
+ else if (!allSelected && !selected) {
221
+ state.selectedSkills.add(item.id);
222
+ }
223
+ }
224
+ render();
225
+ }
226
+ screen.key(["left"], () => {
227
+ if (!canRun(lastTabSwitchAt, DEBOUNCE_SELECT_MS)) {
228
+ return;
229
+ }
230
+ lastTabSwitchAt = Date.now();
231
+ moveTab(state, -1);
232
+ render();
233
+ });
234
+ screen.key(["right"], () => {
235
+ if (!canRun(lastTabSwitchAt, DEBOUNCE_SELECT_MS)) {
236
+ return;
237
+ }
238
+ lastTabSwitchAt = Date.now();
239
+ moveTab(state, 1);
240
+ render();
241
+ });
242
+ screen.key(["up"], () => {
243
+ if (!canRun(lastMoveAt, DEBOUNCE_MOVE_MS)) {
244
+ return;
245
+ }
246
+ lastMoveAt = Date.now();
247
+ moveCursor(state, catalog, -1);
248
+ render();
249
+ });
250
+ screen.key(["down"], () => {
251
+ if (!canRun(lastMoveAt, DEBOUNCE_MOVE_MS)) {
252
+ return;
253
+ }
254
+ lastMoveAt = Date.now();
255
+ moveCursor(state, catalog, 1);
256
+ render();
257
+ });
258
+ screen.key(["space", "enter"], () => {
259
+ handleToggleOrConfirm();
260
+ });
261
+ screen.key(["a"], () => {
262
+ toggleAllSkills();
263
+ });
264
+ screen.key(["q", "C-c"], () => {
265
+ finishWithError("Installation cancelled.");
266
+ });
267
+ render();
268
+ });
269
+ }
@@ -0,0 +1,134 @@
1
+ import blessed from "blessed";
2
+ export const DEBOUNCE_MOVE_MS = 100;
3
+ export const DEBOUNCE_SELECT_MS = 120;
4
+ export function canRun(lastAt, thresholdMs) {
5
+ return Date.now() - lastAt >= thresholdMs;
6
+ }
7
+ export function selectionArray(items) {
8
+ return [...items];
9
+ }
10
+ export function renderBannerHeader(title, subtitle) {
11
+ return [
12
+ "{black-fg}{cyan-bg} AI-TOOLS {/cyan-bg}{/black-fg}",
13
+ `{bold}${title}{/bold}`,
14
+ `{gray-fg}${subtitle}{/gray-fg}`,
15
+ "{cyan-fg}------------------------------------------------------------------------{/cyan-fg}"
16
+ ].join("\n");
17
+ }
18
+ export async function blessedSelect(input) {
19
+ return new Promise((resolve) => {
20
+ let cursor = 0;
21
+ const screen = blessed.screen({ smartCSR: true, fullUnicode: true, title: "ai-tools" });
22
+ const promptBox = blessed.box({
23
+ parent: screen,
24
+ top: "center",
25
+ left: "center",
26
+ width: 50,
27
+ height: input.choices.length + 4,
28
+ border: { type: "line" },
29
+ tags: true
30
+ });
31
+ function render() {
32
+ const lines = [
33
+ `{bold}${input.message}{/bold}`,
34
+ ""
35
+ ];
36
+ for (let i = 0; i < input.choices.length; i++) {
37
+ const choice = input.choices[i];
38
+ const row = `${i === cursor ? "> " : " "}${choice.label}`;
39
+ lines.push(i === cursor ? `{black-fg}{yellow-bg}${row}{/yellow-bg}{/black-fg}` : row);
40
+ }
41
+ promptBox.setContent(lines.join("\n"));
42
+ screen.render();
43
+ }
44
+ screen.key(["up"], () => {
45
+ cursor = (cursor - 1 + input.choices.length) % input.choices.length;
46
+ render();
47
+ });
48
+ screen.key(["down"], () => {
49
+ cursor = (cursor + 1) % input.choices.length;
50
+ render();
51
+ });
52
+ screen.key(["enter", "space"], () => {
53
+ const value = input.choices[cursor].value;
54
+ screen.destroy();
55
+ resolve(value);
56
+ });
57
+ screen.key(["escape", "q", "C-c"], () => {
58
+ screen.destroy();
59
+ resolve(input.choices[0].value);
60
+ });
61
+ render();
62
+ });
63
+ }
64
+ export function createTabbedLayout() {
65
+ const screen = blessed.screen({
66
+ smartCSR: true,
67
+ fullUnicode: true,
68
+ title: "ai-tools"
69
+ });
70
+ const headerBox = blessed.box({
71
+ parent: screen,
72
+ top: 0,
73
+ left: 0,
74
+ width: "100%",
75
+ height: 4,
76
+ tags: true,
77
+ wrap: true
78
+ });
79
+ const tabsBox = blessed.box({
80
+ parent: screen,
81
+ top: 4,
82
+ left: 0,
83
+ width: "100%",
84
+ height: 1,
85
+ tags: true
86
+ });
87
+ const titleBox = blessed.box({
88
+ parent: screen,
89
+ top: 6,
90
+ left: 0,
91
+ width: "100%",
92
+ height: 1,
93
+ tags: true
94
+ });
95
+ const listBox = blessed.list({
96
+ parent: screen,
97
+ top: 8,
98
+ left: 0,
99
+ width: "100%",
100
+ height: "100%-13",
101
+ tags: true,
102
+ keys: false,
103
+ vi: false,
104
+ mouse: false,
105
+ interactive: false,
106
+ scrollable: true,
107
+ alwaysScroll: true,
108
+ style: {
109
+ selected: {
110
+ bold: true,
111
+ fg: "black",
112
+ bg: "yellow"
113
+ }
114
+ }
115
+ });
116
+ const detailBox = blessed.box({
117
+ parent: screen,
118
+ left: 0,
119
+ bottom: 1,
120
+ width: "100%",
121
+ height: 4,
122
+ tags: true,
123
+ wrap: true
124
+ });
125
+ const footerBox = blessed.box({
126
+ parent: screen,
127
+ bottom: 0,
128
+ left: 0,
129
+ width: "100%",
130
+ height: 1,
131
+ tags: true
132
+ });
133
+ return { screen, headerBox, tabsBox, titleBox, listBox, detailBox, footerBox };
134
+ }