@namewta/skills-hub 0.1.2 → 0.1.3

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 CHANGED
@@ -10,19 +10,19 @@
10
10
 
11
11
  ```bash
12
12
  # 直接进入可搜索的领域 / 技能树
13
- npx @namewta/skills-hub@0.1.2
13
+ npx @namewta/skills-hub@0.1.3
14
14
 
15
15
  # 或全局安装后运行
16
- npm install -g @namewta/skills-hub@0.1.2
16
+ npm install -g @namewta/skills-hub@0.1.3
17
17
  skills-hub
18
18
 
19
19
  # 原有命令行安装方式继续保留
20
- npx @namewta/skills-hub@0.1.2 list
21
- npx @namewta/skills-hub@0.1.2 install --domain dev
22
- npx @namewta/skills-hub@0.1.2 install --skill dev-grill dev-spec system-proxy
20
+ npx @namewta/skills-hub@0.1.3 list
21
+ npx @namewta/skills-hub@0.1.3 install --domain dev
22
+ npx @namewta/skills-hub@0.1.3 install --skill dev-grill dev-spec system-proxy
23
23
  ```
24
24
 
25
- 在目标项目根目录执行,按领域或技能选择;npm 下载包不会自动安装技能。终端中不带参数运行 `skills-hub` 或 `skills-hub install`,直接进入可搜索的领域 / 技能树,无需先选择安装模式。勾选领域会选中其全部子技能,可再取消个别子项,也能混选其他领域的技能;根节点用于全选 / 清空。`[x]` 表示全选,`[-]` 表示部分选中,`[ ]` 表示未选。直接输入名称、领域或中文用途实时模糊搜索,支持缩写与多个关键词;↑/↓ 定位、Tab 勾选/取消、Enter 确认。更换搜索词保留已选技能,Ctrl+U 清空搜索,Ctrl+C 取消。搜索时父节点仍操作整个领域(包括隐藏子项),部分选中时再勾父节点会补齐全选。支持 `skills-hub install --dry-run` 交互预览;内容冲突需检查后显式运行 `skills-hub install --replace`,不会自动覆盖。非交互环境继续要求明确参数,不会等待输入。
25
+ 在目标项目根目录执行,按领域或技能选择;npm 下载包不会自动安装技能。终端中不带参数运行 `skills-hub` 或 `skills-hub install`,直接进入可搜索的领域 / 技能树,无需先选择安装模式。勾选领域会选中其全部子技能,可再取消个别子项,也能混选其他领域的技能;根节点用于全选 / 清空。绿色 `■` 表示全选,`◩` 表示部分选中,`□` 表示未选;目录使用加粗名称与 `▸` 图标,技能用 `├─` / `└─` 树枝区分。直接输入名称、领域或中文用途实时模糊搜索,支持缩写与多个关键词;↑/↓ 页内移动、←/→ 整页翻页、Tab 勾选/取消、Enter 确认。每页只属于一个领域,领域行固定在顶部且可勾选;页内到达边界不会滚动加载,底部显示全局页码及领域内页码。技能说明只显示当前焦点项,避免长文本铺满终端。更换搜索词保留已选技能,Ctrl+U 清空搜索,Ctrl+C 取消。搜索时父节点仍操作整个领域(包括隐藏子项),部分选中时再勾父节点会补齐全选。支持 `skills-hub install --dry-run` 交互预览;内容冲突需检查后显式运行 `skills-hub install --replace`,不会自动覆盖。非交互环境继续要求明确参数,不会等待输入。
26
26
 
27
27
  交互界面使用 `@clack/prompts`、`@clack/core`、`picocolors`、`string-width` 和 `wrap-ansi`;npm 自动安装这些依赖。源码的显式参数安装器仍仅依赖 Node.js 标准库、可离线运行;源码交互模式先运行 `npm ci`。也可以克隆源码后离线运行安装器:
28
28
 
package/cli.mjs CHANGED
@@ -25,7 +25,7 @@ export async function interactive({ argv = [], project = process.cwd(), ui = pro
25
25
  ui.intro(pc.cyan('skills-hub · 按需安装中文技能'));
26
26
  ui.note(project, '目标项目(技能写入 .agents/skills/)');
27
27
  const skills = createInstaller().catalog();
28
- ui.note('直接输入关键词,搜索领域、技能名或中文用途;↑/↓ 定位,Tab 勾选/取消,Enter 确认。\n勾选领域或“全部技能”会选择其全部子项(包括搜索隐藏项),之后可取消个别技能。\n[x] 全选,[-] 部分选中,[ ] 未选;更换搜索词保留选择,Ctrl+U 清空搜索。', '树形搜索多选');
28
+ ui.note('↑/↓ 页内移动,←/→ 整页翻页,Tab 勾选,Enter 确认。\n直接输入搜索;勾选领域包含其他页和隐藏子项,可再取消个别技能。\n■ 全选 · ◩ 部分选中 · □ 未选;Ctrl+U 清空搜索。', '树形搜索多选');
29
29
  const names = await ask(choose({ skills }));
30
30
  const selected = select(skills, [], names);
31
31
  if (!selected.length) throw new Error('请至少勾选一个技能。');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@namewta/skills-hub",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "独立中文 Agent Skills:按领域安装,覆盖开发交付、项目知识与系统维护。",
5
5
  "type": "module",
6
6
  "bin": {
package/tree-select.mjs CHANGED
@@ -1,6 +1,7 @@
1
- import { AutocompletePrompt } from '@clack/core';
1
+ import { Prompt } from '@clack/core';
2
2
  import pc from 'picocolors';
3
- import wrapAnsi from 'wrap-ansi';
3
+ import stringWidth from 'string-width';
4
+ import { stripVTControlCharacters } from 'node:util';
4
5
 
5
6
  // 非连续字符匹配,支持中文、全角英文和多关键词,不做拼音转换或拼写纠错。
6
7
  export function matchesSkillSearch(search, option) {
@@ -52,38 +53,147 @@ export function createSkillTree(skills) {
52
53
  };
53
54
  }
54
55
 
56
+ // 每页只属于一个领域;领域行始终置顶且可勾选,不与子项一起滚出屏幕。
57
+ export function createTreePager(tree, pageSize = 8) {
58
+ let query = '', size = Math.max(1, pageSize), page = 0, cursor = 0;
59
+ function pages() {
60
+ const result = [];
61
+ for (const domain of tree.nodes.filter(node => node.depth === 1)) {
62
+ const children = tree.nodes.filter(node => node.domain === domain.value && tree.matches(query, node));
63
+ for (let start = 0; start < children.length; start += size) {
64
+ result.push({ domain, children: children.slice(start, start + size),
65
+ domainPage: Math.floor(start / size) + 1, domainPages: Math.ceil(children.length / size) });
66
+ }
67
+ }
68
+ return result;
69
+ }
70
+ function view() {
71
+ const all = pages();
72
+ page = Math.max(0, Math.min(page, all.length - 1));
73
+ const current = all[page];
74
+ const rows = current ? [tree.nodes[0], current.domain, ...current.children] : [];
75
+ cursor = Math.max(0, Math.min(cursor, rows.length - 1));
76
+ return { ...current, rows, cursor, page, totalPages: all.length, query, pageSize: size };
77
+ }
78
+ return {
79
+ view,
80
+ search(value) {
81
+ if (value === query) return;
82
+ query = value; page = 0; cursor = 0;
83
+ const current = view();
84
+ if (query.trim() && current.rows.length) {
85
+ cursor = matchesSkillSearch(query, tree.nodes[0]) ? 0
86
+ : matchesSkillSearch(query, current.domain) ? 1 : 2;
87
+ }
88
+ },
89
+ resize(value) {
90
+ if (size === value) return;
91
+ const before = view();
92
+ const focused = before.rows[cursor]?.value;
93
+ size = Math.max(1, value);
94
+ const all = pages();
95
+ const target = all.findIndex(item => item.children.some(node => node.value === focused)
96
+ || ((focused === '@all' || focused === before.domain?.value) && item.domain.value === before.domain?.value));
97
+ if (target !== -1) page = target;
98
+ const current = view();
99
+ const index = current.rows.findIndex(node => node.value === focused);
100
+ if (index !== -1) cursor = index;
101
+ },
102
+ move(direction) {
103
+ const current = view();
104
+ cursor = Math.max(0, Math.min(cursor + direction, current.rows.length - 1));
105
+ },
106
+ turn(direction) {
107
+ const current = view();
108
+ const next = Math.max(0, Math.min(page + direction, current.totalPages - 1));
109
+ if (next !== page) { page = next; cursor = 2; }
110
+ },
111
+ toggle() {
112
+ const focused = view().rows[cursor];
113
+ if (focused) tree.toggle(focused.value);
114
+ },
115
+ };
116
+ }
117
+
118
+ const graphemes = new Intl.Segmenter('zh', { granularity: 'grapheme' });
119
+ export function fitTreeText(value, width) {
120
+ const text = stripVTControlCharacters(String(value)).replace(/[\u0000-\u001f\u007f]/g, ' ');
121
+ if (stringWidth(text) <= width) return text;
122
+ let result = '', used = 0;
123
+ for (const { segment } of graphemes.segment(text)) {
124
+ const length = stringWidth(segment);
125
+ if (used + length > width - 1) break;
126
+ result += segment; used += length;
127
+ }
128
+ return `${result}…`;
129
+ }
130
+
131
+ export function renderTreePage(tree, pager, { columns = 80, colors = pc, error = '' } = {}) {
132
+ const c = colors, view = pager.view(), width = Math.max(12, columns - 1);
133
+ const line = (text, style = value => value) => style(fitTreeText(text, width));
134
+ const lines = [line('◆ 搜索并选择领域 / 技能', c.green), line(`│ 搜索:${view.query || '输入名称 / 用途'} ▌`), '│'];
135
+ for (const [index, node] of view.rows.entries()) {
136
+ const active = index === view.cursor;
137
+ const state = tree.state(node.value);
138
+ const marker = { none: '□', all: '■', partial: '◩' }[state];
139
+ const branch = node.depth === 0 ? '◆ ' : node.depth === 1 ? '└─ ▸ '
140
+ : index === view.rows.length - 1 ? ' └─ ' : ' ├─ ';
141
+ const prefix = `│ ${active ? '›' : ' '} ${branch}${marker} `;
142
+ const suffix = node.depth < 2 ? ` ${node.names.filter(name => tree.names().includes(name)).length}/${node.names.length}` : '';
143
+ const name = node.depth < 2 ? `${node.label}${node.depth === 1 ? ' 领域' : ''}` : node.label;
144
+ const label = fitTreeText(`${prefix}${name}${suffix}`, width).slice(prefix.length);
145
+ // 行首保留非空树枝,避免折行工具裁掉缩进;目录与技能有不同图标和颜色。
146
+ lines.push(c.gray(prefix.slice(0, -2)) + (state === 'none' ? c.dim(`${marker} `) : c.green(`${marker} `))
147
+ + (node.depth < 2 ? c.bold(active ? c.green(label) : c.cyan(label)) : active ? c.green(label) : label));
148
+ }
149
+ if (!view.rows.length) lines.push(line('│ 无匹配;退格修改或 Ctrl+U 清空搜索。', c.yellow));
150
+ const focused = view.rows[view.cursor];
151
+ const detail = focused?.depth === 2 ? focused.hint
152
+ : focused ? '勾选将操作全部子技能,包括其他页和搜索隐藏项。' : '输入其他关键词以继续选择。';
153
+ lines.push('│', line(`│ ${focused?.label ?? '搜索提示'} · ${view.domain?.label ?? '—'}`, c.dim),
154
+ line(`│ ${detail}`, c.dim),
155
+ line(`│ 第 ${view.totalPages ? view.page + 1 : 0}/${view.totalPages} 页 · ${view.domain?.label ?? '—'} ${view.domainPage ?? 0}/${view.domainPages ?? 0} · 已选 ${tree.names().length} 个`, c.green),
156
+ line(error ? `│ ${error}` : '│ ←/→ 整页翻页 · ↑/↓ 页内移动', error ? c.red : c.dim),
157
+ line('└ Tab 勾选 · Enter 确认 · Ctrl+C 取消', c.dim));
158
+ return lines.join('\n');
159
+ }
160
+
55
161
  export function treeSelect({ skills, input = process.stdin, output = process.stdout }) {
56
162
  const tree = createSkillTree(skills);
57
- class TreePrompt extends AutocompletePrompt {
58
- toggleSelected(value) {
59
- // 父节点不是安装项;最终值始终只有明确的叶子名称。
60
- tree.toggle(value);
61
- this.selectedValues = tree.names();
163
+ const pageSize = () => Math.max(1, Math.min(8, (output.rows || 24) - 13));
164
+ const pager = createTreePager(tree, pageSize());
165
+ class TreePrompt extends Prompt {
166
+ constructor() {
167
+ super({ input, output,
168
+ render() {
169
+ if (this.state === 'submit') return pc.green(`◇ 已选择 ${tree.names().length} 个技能`);
170
+ if (this.state === 'cancel') return pc.yellow('◇ 已取消树形选择');
171
+ pager.resize(pageSize());
172
+ return renderTreePage(tree, pager, { columns: output.columns || 80, error: this.state === 'error' ? this.error : '' });
173
+ },
174
+ });
175
+ this.value = [];
176
+ this.on('userInput', value => pager.search(value));
177
+ this.on('key', (character, key) => {
178
+ if (key.name === 'up' || key.name === 'down') pager.move(key.name === 'up' ? -1 : 1);
179
+ if (key.name === 'left' || key.name === 'right') {
180
+ pager.turn(key.name === 'left' ? -1 : 1);
181
+ // 左右键被分页占用,不让 readline 光标移动后将搜索文字插到中间。
182
+ if (this.rl) this.rl.cursor = this.rl.line.length;
183
+ }
184
+ if (key.name === 'tab' || (key.name === 'space' && !pager.view().query.trim())) pager.toggle();
185
+ this.value = tree.names();
186
+ });
187
+ }
188
+ _isActionKey(character, key) {
189
+ return key.name === 'tab' || (key.name === 'space' && !pager.view().query.trim());
190
+ }
191
+ _shouldSubmit() {
192
+ if (this.value.length) return true;
193
+ this.error = '请至少勾选一个技能。';
194
+ this.state = 'error';
195
+ return false;
62
196
  }
63
197
  }
64
- return new TreePrompt({ input, output, multiple: true, options: tree.nodes,
65
- filter: (search, node) => tree.matches(search, node),
66
- validate: values => !values?.length ? '请至少勾选一个技能。' : undefined,
67
- render() {
68
- const count = tree.names().length;
69
- if (this.state === 'submit') return pc.green(`✓ 已选择 ${count} 个技能`);
70
- if (this.state === 'cancel') return pc.yellow('已取消树形选择');
71
- const rows = this.filteredOptions;
72
- const size = Math.max(1, Math.min(10, (output.rows || 24) - 8));
73
- const start = Math.max(0, Math.min(this.cursor - Math.floor(size / 2), rows.length - size));
74
- const width = Math.max(16, (output.columns || 80) - 4);
75
- const lines = rows.slice(start, start + size).map((node, index) => {
76
- const marker = { none: '[ ]', all: '[x]', partial: '[-]' }[tree.state(node.value)];
77
- const prefix = `${start + index === this.cursor ? '›' : ' '} ${' '.repeat(node.depth)}${marker} `;
78
- const detail = node.depth < 2 ? ` (${node.names.length} 个技能)` : ` · ${node.hint}`;
79
- const line = wrapAnsi(`${prefix}${node.label}${detail}`, width, { hard: true }).split('\n')[0];
80
- return start + index === this.cursor ? pc.cyan(line) : line;
81
- });
82
- return [pc.bold('搜索并选择领域 / 技能'), `搜索:${this.userInputWithCursor}`,
83
- ...(!rows.length ? [pc.yellow('无匹配;退格修改或 Ctrl+U 清空搜索。')] : lines),
84
- `已选 ${count}/${skills.length} 个技能 · 显示 ${rows.length ? start + 1 : 0}–${Math.min(start + size, rows.length)}/${rows.length} 项`,
85
- pc.dim('↑/↓ 定位 · Tab 勾选 · Enter 确认 · Ctrl+C 取消'),
86
- ...(this.state === 'error' ? [pc.red(this.error)] : [])].join('\n');
87
- },
88
- }).prompt();
198
+ return new TreePrompt().prompt();
89
199
  }