@readwise/cli 0.5.0 → 0.5.1
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 +9 -9
- package/dist/index.js +4 -1
- package/dist/skills.d.ts +2 -0
- package/dist/skills.js +246 -0
- package/dist/tui/app.js +384 -10
- package/package.json +1 -1
- package/src/index.ts +5 -1
- package/src/skills.ts +260 -0
- package/src/tui/app.ts +424 -12
package/src/skills.ts
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import { readdir, readFile, writeFile, mkdir, rm } from "node:fs/promises";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { join, dirname } from "node:path";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
|
|
7
|
+
const REPO_OWNER = "readwiseio";
|
|
8
|
+
const REPO_NAME = "readwise-skills";
|
|
9
|
+
const SKILLS_SUBDIR = "skills";
|
|
10
|
+
const GITHUB_API = `https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}`;
|
|
11
|
+
const GITHUB_RAW = `https://raw.githubusercontent.com/${REPO_OWNER}/${REPO_NAME}/master`;
|
|
12
|
+
|
|
13
|
+
/** Local cache directory for fetched skills */
|
|
14
|
+
function cacheDir(): string {
|
|
15
|
+
return join(homedir(), ".readwise", "skills-cache");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Staleness threshold — refetch if cache is older than this */
|
|
19
|
+
const CACHE_MAX_AGE_MS = 24 * 60 * 60 * 1000; // 24 hours
|
|
20
|
+
|
|
21
|
+
function cacheMetaPath(): string {
|
|
22
|
+
return join(cacheDir(), ".fetched_at");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function isCacheFresh(): Promise<boolean> {
|
|
26
|
+
const metaPath = cacheMetaPath();
|
|
27
|
+
if (!existsSync(metaPath)) return false;
|
|
28
|
+
try {
|
|
29
|
+
const ts = Number(await readFile(metaPath, "utf-8"));
|
|
30
|
+
return Date.now() - ts < CACHE_MAX_AGE_MS;
|
|
31
|
+
} catch {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function touchCache(): Promise<void> {
|
|
37
|
+
await writeFile(cacheMetaPath(), String(Date.now()));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Fetch the list of skill directory names from GitHub API */
|
|
41
|
+
async function fetchSkillNames(): Promise<string[]> {
|
|
42
|
+
const res = await fetch(`${GITHUB_API}/contents/${SKILLS_SUBDIR}`, {
|
|
43
|
+
headers: { Accept: "application/vnd.github.v3+json", "User-Agent": "readwise-cli" },
|
|
44
|
+
});
|
|
45
|
+
if (!res.ok) throw new Error(`GitHub API error: ${res.status} ${res.statusText}`);
|
|
46
|
+
const entries = (await res.json()) as { name: string; type: string }[];
|
|
47
|
+
return entries.filter((e) => e.type === "dir").map((e) => e.name);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Fetch a single SKILL.md from GitHub */
|
|
51
|
+
async function fetchSkillFile(skillName: string): Promise<string> {
|
|
52
|
+
const url = `${GITHUB_RAW}/${SKILLS_SUBDIR}/${skillName}/SKILL.md`;
|
|
53
|
+
const res = await fetch(url, { headers: { "User-Agent": "readwise-cli" } });
|
|
54
|
+
if (!res.ok) throw new Error(`Failed to fetch ${skillName}: ${res.status}`);
|
|
55
|
+
return res.text();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Fetch all skills from GitHub and write to cache */
|
|
59
|
+
async function refreshCache(): Promise<string[]> {
|
|
60
|
+
const names = await fetchSkillNames();
|
|
61
|
+
const cache = cacheDir();
|
|
62
|
+
|
|
63
|
+
// Clear old cache
|
|
64
|
+
if (existsSync(cache)) await rm(cache, { recursive: true });
|
|
65
|
+
await mkdir(cache, { recursive: true });
|
|
66
|
+
|
|
67
|
+
// Fetch all in parallel
|
|
68
|
+
const results = await Promise.allSettled(
|
|
69
|
+
names.map(async (name) => {
|
|
70
|
+
const content = await fetchSkillFile(name);
|
|
71
|
+
const dir = join(cache, name);
|
|
72
|
+
await mkdir(dir, { recursive: true });
|
|
73
|
+
await writeFile(join(dir, "SKILL.md"), content);
|
|
74
|
+
return name;
|
|
75
|
+
})
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
const fetched = results
|
|
79
|
+
.filter((r): r is PromiseFulfilledResult<string> => r.status === "fulfilled")
|
|
80
|
+
.map((r) => r.value);
|
|
81
|
+
|
|
82
|
+
await touchCache();
|
|
83
|
+
return fetched;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Get cached skill names, refreshing if stale */
|
|
87
|
+
async function getSkillNames(forceRefresh: boolean): Promise<string[]> {
|
|
88
|
+
if (!forceRefresh && (await isCacheFresh())) {
|
|
89
|
+
return listCachedSkills();
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
return await refreshCache();
|
|
93
|
+
} catch (err) {
|
|
94
|
+
// Fall back to cache if network fails
|
|
95
|
+
const cached = await listCachedSkills();
|
|
96
|
+
if (cached.length > 0) {
|
|
97
|
+
console.error(`\x1b[33mWarning: Could not fetch from GitHub (${(err as Error).message}), using cached skills.\x1b[0m`);
|
|
98
|
+
return cached;
|
|
99
|
+
}
|
|
100
|
+
throw err;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async function listCachedSkills(): Promise<string[]> {
|
|
105
|
+
const cache = cacheDir();
|
|
106
|
+
if (!existsSync(cache)) return [];
|
|
107
|
+
const entries = await readdir(cache, { withFileTypes: true });
|
|
108
|
+
return entries
|
|
109
|
+
.filter((e) => e.isDirectory() && existsSync(join(cache, e.name, "SKILL.md")))
|
|
110
|
+
.map((e) => e.name);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function readSkillFrontmatter(skillName: string): Promise<{ name?: string; description?: string }> {
|
|
114
|
+
const content = await readFile(join(cacheDir(), skillName, "SKILL.md"), "utf-8");
|
|
115
|
+
const match = content.match(/^---\n([\s\S]*?)\n---/);
|
|
116
|
+
if (!match) return {};
|
|
117
|
+
const fm: Record<string, string> = {};
|
|
118
|
+
for (const line of match[1]!.split("\n")) {
|
|
119
|
+
const [key, ...rest] = line.split(":");
|
|
120
|
+
if (key && rest.length) fm[key.trim()] = rest.join(":").trim();
|
|
121
|
+
}
|
|
122
|
+
return fm;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Known agent platforms and their skills directories */
|
|
126
|
+
const PLATFORMS: Record<string, { name: string; path: string }> = {
|
|
127
|
+
claude: { name: "Claude Code", path: join(homedir(), ".claude", "skills") },
|
|
128
|
+
codex: { name: "Codex CLI", path: join(homedir(), ".codex", "skills") },
|
|
129
|
+
opencode: { name: "OpenCode", path: join(homedir(), ".opencode", "skills") },
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export function registerSkillsCommands(program: Command): void {
|
|
133
|
+
const skills = program.command("skills").description("Manage Readwise skills for AI agents");
|
|
134
|
+
|
|
135
|
+
skills
|
|
136
|
+
.command("list")
|
|
137
|
+
.description("List available skills (fetched from github.com/readwiseio/readwise-skills)")
|
|
138
|
+
.option("--refresh", "Force refresh from GitHub")
|
|
139
|
+
.action(async (opts: { refresh?: boolean }) => {
|
|
140
|
+
try {
|
|
141
|
+
const names = await getSkillNames(!!opts.refresh);
|
|
142
|
+
if (names.length === 0) {
|
|
143
|
+
console.log("No skills found.");
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
console.log("Available skills:\n");
|
|
147
|
+
for (const name of names) {
|
|
148
|
+
const fm = await readSkillFrontmatter(name);
|
|
149
|
+
const desc = fm.description || "";
|
|
150
|
+
console.log(` ${name.padEnd(22)} ${desc}`);
|
|
151
|
+
}
|
|
152
|
+
console.log(`\nRun \`readwise skills install <platform>\` to install. Platforms: ${Object.keys(PLATFORMS).join(", ")}`);
|
|
153
|
+
} catch (err) {
|
|
154
|
+
console.error(`\x1b[31mFailed to list skills: ${(err as Error).message}\x1b[0m`);
|
|
155
|
+
process.exitCode = 1;
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
skills
|
|
160
|
+
.command("install [platform]")
|
|
161
|
+
.description("Install skills to an agent platform (claude, codex, opencode)")
|
|
162
|
+
.option("--all", "Detect installed agents and install to all")
|
|
163
|
+
.option("--refresh", "Force refresh from GitHub before installing")
|
|
164
|
+
.action(async (platform?: string, opts?: { all?: boolean; refresh?: boolean }) => {
|
|
165
|
+
try {
|
|
166
|
+
const names = await getSkillNames(!!opts?.refresh);
|
|
167
|
+
if (names.length === 0) {
|
|
168
|
+
console.error("No skills found.");
|
|
169
|
+
process.exitCode = 1;
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
let targets: { key: string; name: string; path: string }[] = [];
|
|
174
|
+
|
|
175
|
+
if (opts?.all) {
|
|
176
|
+
for (const [key, info] of Object.entries(PLATFORMS)) {
|
|
177
|
+
const parentDir = dirname(info.path);
|
|
178
|
+
if (existsSync(parentDir)) {
|
|
179
|
+
targets.push({ key, ...info });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (targets.length === 0) {
|
|
183
|
+
console.log("No supported agents detected. Supported platforms: " + Object.keys(PLATFORMS).join(", "));
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
} else if (platform) {
|
|
187
|
+
const info = PLATFORMS[platform.toLowerCase()];
|
|
188
|
+
if (!info) {
|
|
189
|
+
console.error(`Unknown platform: ${platform}`);
|
|
190
|
+
console.error(`Supported platforms: ${Object.keys(PLATFORMS).join(", ")}`);
|
|
191
|
+
process.exitCode = 1;
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
targets = [{ key: platform.toLowerCase(), ...info }];
|
|
195
|
+
} else {
|
|
196
|
+
console.error("Specify a platform or use --all.");
|
|
197
|
+
console.error(`Supported platforms: ${Object.keys(PLATFORMS).join(", ")}`);
|
|
198
|
+
process.exitCode = 1;
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const cache = cacheDir();
|
|
203
|
+
for (const target of targets) {
|
|
204
|
+
console.log(`\nInstalling to ${target.name} (${target.path})...`);
|
|
205
|
+
await mkdir(target.path, { recursive: true });
|
|
206
|
+
|
|
207
|
+
for (const skillName of names) {
|
|
208
|
+
const srcFile = join(cache, skillName, "SKILL.md");
|
|
209
|
+
const destDir = join(target.path, skillName);
|
|
210
|
+
await mkdir(destDir, { recursive: true });
|
|
211
|
+
const content = await readFile(srcFile, "utf-8");
|
|
212
|
+
await writeFile(join(destDir, "SKILL.md"), content);
|
|
213
|
+
console.log(` \u2713 ${skillName}`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
console.log("\nDone!");
|
|
218
|
+
} catch (err) {
|
|
219
|
+
console.error(`\x1b[31mFailed to install skills: ${(err as Error).message}\x1b[0m`);
|
|
220
|
+
process.exitCode = 1;
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
skills
|
|
225
|
+
.command("show <name>")
|
|
226
|
+
.description("Print the raw SKILL.md for a skill")
|
|
227
|
+
.option("--refresh", "Force refresh from GitHub")
|
|
228
|
+
.action(async (name: string, opts: { refresh?: boolean }) => {
|
|
229
|
+
try {
|
|
230
|
+
await getSkillNames(!!opts.refresh);
|
|
231
|
+
const skillPath = join(cacheDir(), name, "SKILL.md");
|
|
232
|
+
if (!existsSync(skillPath)) {
|
|
233
|
+
console.error(`Skill not found: ${name}`);
|
|
234
|
+
const available = await listCachedSkills();
|
|
235
|
+
if (available.length) console.error(`Available: ${available.join(", ")}`);
|
|
236
|
+
process.exitCode = 1;
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
const content = await readFile(skillPath, "utf-8");
|
|
240
|
+
process.stdout.write(content);
|
|
241
|
+
} catch (err) {
|
|
242
|
+
console.error(`\x1b[31mFailed: ${(err as Error).message}\x1b[0m`);
|
|
243
|
+
process.exitCode = 1;
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
skills
|
|
248
|
+
.command("update")
|
|
249
|
+
.description("Force refresh skills from GitHub")
|
|
250
|
+
.action(async () => {
|
|
251
|
+
try {
|
|
252
|
+
console.log("Fetching skills from github.com/readwiseio/readwise-skills...");
|
|
253
|
+
const names = await refreshCache();
|
|
254
|
+
console.log(`Updated ${names.length} skills: ${names.join(", ")}`);
|
|
255
|
+
} catch (err) {
|
|
256
|
+
console.error(`\x1b[31mFailed to update: ${(err as Error).message}\x1b[0m`);
|
|
257
|
+
process.exitCode = 1;
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
}
|