@s-hirano-ist/s-scripts 4.2.0 → 4.3.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.
@@ -0,0 +1,382 @@
1
+ import { createRequire as __cliCreateRequire } from "node:module"; const require = __cliCreateRequire(import.meta.url);
2
+ import {
3
+ RAG_CONFIG,
4
+ deletePointsByChunkIds,
5
+ ensureCollection,
6
+ getCollectionStats,
7
+ getExistingHashes,
8
+ listAllChunkIds,
9
+ upsertPoints
10
+ } from "./chunk-J4VLH4VA.js";
11
+ import {
12
+ parseMarkdownFrontmatter
13
+ } from "./chunk-HDYQV2Y2.js";
14
+ import {
15
+ globPaths
16
+ } from "./chunk-AJMTRCPJ.js";
17
+ import {
18
+ reportCliError
19
+ } from "./chunk-4CXH2QIX.js";
20
+ import "./chunk-JZOR5GA2.js";
21
+
22
+ // ../search/dist/chunker.js
23
+ import { createHash } from "node:crypto";
24
+ function generateHash(content) {
25
+ return createHash("sha256").update(content).digest("hex").slice(0, 16);
26
+ }
27
+ function generateChunkId(docId, index) {
28
+ return `${docId}#${index}`;
29
+ }
30
+ function parseJsonArticle(filePath, content) {
31
+ const json = JSON.parse(content);
32
+ const docId = `file:${filePath}`;
33
+ const chunks = [];
34
+ for (let i = 0; i < json.body.length; i++) {
35
+ const item = json.body[i];
36
+ const textParts = [];
37
+ if (item.title)
38
+ textParts.push(item.title);
39
+ if (item.ogTitle && item.ogTitle !== item.title)
40
+ textParts.push(item.ogTitle);
41
+ if (item.ogDescription)
42
+ textParts.push(item.ogDescription);
43
+ if (item.quote)
44
+ textParts.push(item.quote);
45
+ const text = textParts.join("\n");
46
+ if (!text.trim())
47
+ continue;
48
+ const chunkId = generateChunkId(docId, i);
49
+ chunks.push({
50
+ type: "bookmark_json",
51
+ content_type: "articles",
52
+ top_heading: json.heading,
53
+ doc_id: docId,
54
+ chunk_id: chunkId,
55
+ title: item.title || item.ogTitle || "Untitled",
56
+ url: item.url,
57
+ heading_path: [json.heading],
58
+ text,
59
+ content_hash: generateHash(text)
60
+ });
61
+ }
62
+ return chunks;
63
+ }
64
+ function parseFrontmatter(content) {
65
+ const parsed = parseMarkdownFrontmatter(content);
66
+ const frontmatter = {
67
+ heading: typeof parsed.data.heading === "string" ? parsed.data.heading : "unknown"
68
+ };
69
+ if (typeof parsed.data.description === "string") {
70
+ frontmatter.description = parsed.data.description;
71
+ }
72
+ if (typeof parsed.data.draft === "boolean") {
73
+ frontmatter.draft = parsed.data.draft;
74
+ }
75
+ return { frontmatter, body: parsed.content };
76
+ }
77
+ function pushPreambleSection(state) {
78
+ if (state.preambleContent.trim()) {
79
+ state.sections.push({
80
+ headingPath: [],
81
+ title: "",
82
+ content: state.preambleContent,
83
+ level: 0
84
+ });
85
+ }
86
+ }
87
+ function handleHeadingLine(state, headingMatch) {
88
+ if (!state.preambleDone) {
89
+ state.preambleDone = true;
90
+ pushPreambleSection(state);
91
+ }
92
+ if (state.currentSection?.content.trim()) {
93
+ state.sections.push(state.currentSection);
94
+ }
95
+ const level = headingMatch[1].length;
96
+ const title = headingMatch[2];
97
+ const headingStack = state.headingStack;
98
+ let top = headingStack.at(-1);
99
+ while (top && top.level >= level) {
100
+ headingStack.pop();
101
+ top = headingStack.at(-1);
102
+ }
103
+ headingStack.push({ level, title });
104
+ state.currentSection = {
105
+ headingPath: headingStack.map((h) => h.title),
106
+ title,
107
+ content: "",
108
+ level
109
+ };
110
+ }
111
+ function splitMarkdownByHeadings(content) {
112
+ const state = {
113
+ sections: [],
114
+ currentSection: null,
115
+ headingStack: [],
116
+ // Preamble buffer (content before first H2/H3)
117
+ preambleContent: "",
118
+ preambleDone: false
119
+ };
120
+ for (const line of content.split("\n")) {
121
+ if (/^#\s+\S/u.test(line)) {
122
+ continue;
123
+ }
124
+ const headingMatch = /^(#{2,3})\s+(\S.*)$/u.exec(line);
125
+ if (headingMatch) {
126
+ handleHeadingLine(state, headingMatch);
127
+ } else if (!state.preambleDone) {
128
+ state.preambleContent += `${line}
129
+ `;
130
+ } else if (state.currentSection) {
131
+ state.currentSection.content += `${line}
132
+ `;
133
+ }
134
+ }
135
+ if (!state.preambleDone) {
136
+ pushPreambleSection(state);
137
+ }
138
+ if (state.currentSection?.content.trim()) {
139
+ state.sections.push(state.currentSection);
140
+ }
141
+ return state.sections;
142
+ }
143
+ function splitByParagraphs(text, maxLength) {
144
+ if (text.length <= maxLength) {
145
+ return [text];
146
+ }
147
+ const paragraphs = text.split(/\n{2,}/u);
148
+ const chunks = [];
149
+ let currentChunk = "";
150
+ for (const para of paragraphs) {
151
+ if (currentChunk.length + para.length > maxLength && currentChunk) {
152
+ chunks.push(currentChunk.trim());
153
+ currentChunk = para;
154
+ } else {
155
+ currentChunk += (currentChunk ? "\n\n" : "") + para;
156
+ }
157
+ }
158
+ if (currentChunk.trim()) {
159
+ chunks.push(currentChunk.trim());
160
+ }
161
+ return chunks;
162
+ }
163
+ function parseMarkdownWithDocId(docId, content, contentType = "notes") {
164
+ const { frontmatter, body } = parseFrontmatter(content);
165
+ const chunks = [];
166
+ if (frontmatter.draft) {
167
+ return [];
168
+ }
169
+ const sections = splitMarkdownByHeadings(body);
170
+ let chunkIndex = 0;
171
+ for (const section of sections) {
172
+ const textChunks = splitByParagraphs(section.content, RAG_CONFIG.chunking.maxChunkLength);
173
+ for (const text of textChunks) {
174
+ if (!text.trim())
175
+ continue;
176
+ const isPreamble = section.level === 0 && section.title === "";
177
+ const sectionTitle = isPreamble ? frontmatter.description ?? frontmatter.heading : section.title;
178
+ const fullHeadingPath = isPreamble ? [frontmatter.heading] : [frontmatter.heading, ...section.headingPath];
179
+ const chunkId = generateChunkId(docId, chunkIndex);
180
+ chunks.push({
181
+ type: "markdown_note",
182
+ content_type: contentType,
183
+ top_heading: frontmatter.heading,
184
+ doc_id: docId,
185
+ chunk_id: chunkId,
186
+ title: sectionTitle,
187
+ heading_path: fullHeadingPath,
188
+ text,
189
+ content_hash: generateHash(text)
190
+ });
191
+ chunkIndex++;
192
+ }
193
+ }
194
+ return chunks;
195
+ }
196
+ function parseMarkdown(filePath, content, contentType = "notes") {
197
+ return parseMarkdownWithDocId(`file:${filePath}`, content, contentType);
198
+ }
199
+
200
+ // ../search/dist/ingest.js
201
+ import { setTimeout } from "node:timers/promises";
202
+ var BATCH_SIZE = 20;
203
+ var MAX_RETRIES = 3;
204
+ var RETRY_DELAY_MS = 2e3;
205
+ async function withRetry(fn, retries = MAX_RETRIES) {
206
+ for (let i = 0; i < retries; i++) {
207
+ try {
208
+ return await fn();
209
+ } catch (error) {
210
+ if (i === retries - 1)
211
+ throw error;
212
+ console.log(` Retry ${i + 1}/${retries} after error...`);
213
+ await setTimeout(RETRY_DELAY_MS);
214
+ }
215
+ }
216
+ throw new Error("Unreachable");
217
+ }
218
+ async function ingestChunks(chunks, options) {
219
+ console.log(`Total chunks: ${chunks.length}
220
+ `);
221
+ let changedChunks;
222
+ let skippedChunks;
223
+ if (options.force) {
224
+ changedChunks = chunks;
225
+ skippedChunks = 0;
226
+ } else {
227
+ console.log("Checking for changes...");
228
+ const chunkIds = chunks.map((c) => c.chunk_id);
229
+ const existingHashes = await getExistingHashes(chunkIds);
230
+ changedChunks = chunks.filter((chunk) => {
231
+ const existingHash = existingHashes.get(chunk.chunk_id);
232
+ return existingHash !== chunk.content_hash;
233
+ });
234
+ skippedChunks = chunks.length - changedChunks.length;
235
+ }
236
+ console.log(`Changed chunks: ${changedChunks.length}`);
237
+ console.log(`Skipped (unchanged): ${skippedChunks}
238
+ `);
239
+ if (changedChunks.length === 0) {
240
+ console.log("No changes detected. Done!");
241
+ return {
242
+ totalChunks: chunks.length,
243
+ changedChunks: 0,
244
+ skippedChunks
245
+ };
246
+ }
247
+ console.log("Upserting to Qdrant...");
248
+ let processed = 0;
249
+ for (let i = 0; i < changedChunks.length; i += BATCH_SIZE) {
250
+ const batch = changedChunks.slice(i, i + BATCH_SIZE);
251
+ const points = batch.map((chunk) => ({
252
+ id: chunk.chunk_id,
253
+ text: chunk.text,
254
+ payload: chunk
255
+ }));
256
+ await withRetry(() => upsertPoints(points));
257
+ processed += batch.length;
258
+ console.log(` Progress: ${processed}/${changedChunks.length}`);
259
+ await setTimeout(100);
260
+ }
261
+ return {
262
+ totalChunks: chunks.length,
263
+ changedChunks: changedChunks.length,
264
+ skippedChunks
265
+ };
266
+ }
267
+ async function pruneOrphans(currentChunkIds) {
268
+ const existingIds = await listAllChunkIds();
269
+ const currentSet = new Set(currentChunkIds);
270
+ const orphanIds = existingIds.filter((id) => !currentSet.has(id));
271
+ console.log(`Existing Qdrant chunks: ${existingIds.length}`);
272
+ console.log(`Orphan chunks to delete: ${orphanIds.length}`);
273
+ if (orphanIds.length === 0) {
274
+ return { deletedCount: 0 };
275
+ }
276
+ let deleted = 0;
277
+ for (let i = 0; i < orphanIds.length; i += BATCH_SIZE) {
278
+ const batch = orphanIds.slice(i, i + BATCH_SIZE);
279
+ await withRetry(() => deletePointsByChunkIds(batch));
280
+ deleted += batch.length;
281
+ console.log(` Prune progress: ${deleted}/${orphanIds.length}`);
282
+ await setTimeout(100);
283
+ }
284
+ return { deletedCount: deleted };
285
+ }
286
+
287
+ // src/rag/ingest-command.ts
288
+ import { readFileSync } from "node:fs";
289
+
290
+ // src/rag/ingest-config.ts
291
+ var INGEST_CONFIG = {
292
+ paths: {
293
+ notes: "markdown/note/**/*.md",
294
+ books: "markdown/book/**/*.md",
295
+ articles: "json/article/**/*.json"
296
+ }
297
+ };
298
+
299
+ // src/rag/ingest-command.ts
300
+ async function listFiles() {
301
+ const files = [];
302
+ const articleFiles = await globPaths(INGEST_CONFIG.paths.articles);
303
+ for (const path of articleFiles) {
304
+ files.push({ path, type: "json", contentType: "articles" });
305
+ }
306
+ const noteFiles = await globPaths(INGEST_CONFIG.paths.notes);
307
+ for (const path of noteFiles) {
308
+ files.push({ path, type: "markdown", contentType: "notes" });
309
+ }
310
+ const bookFiles = await globPaths(INGEST_CONFIG.paths.books);
311
+ for (const path of bookFiles) {
312
+ files.push({ path, type: "markdown", contentType: "books" });
313
+ }
314
+ return files;
315
+ }
316
+ function parseFile(file) {
317
+ const content = readFileSync(file.path, "utf-8");
318
+ if (file.type === "json") {
319
+ return parseJsonArticle(file.path, content);
320
+ }
321
+ return parseMarkdown(file.path, content, file.contentType);
322
+ }
323
+ async function ingest(flags) {
324
+ console.log("Starting ingest...\n");
325
+ if (flags.dryRun) {
326
+ console.log("Dry-run mode: no writes to Qdrant\n");
327
+ } else {
328
+ await ensureCollection();
329
+ const initialStats = await getCollectionStats();
330
+ console.log(`Initial points count: ${initialStats.pointsCount}
331
+ `);
332
+ }
333
+ if (flags.force) {
334
+ console.log("Force mode enabled: skipping change detection\n");
335
+ }
336
+ const files = await listFiles();
337
+ const articleCount = files.filter((f) => f.contentType === "articles").length;
338
+ const noteCount = files.filter((f) => f.contentType === "notes").length;
339
+ const bookCount = files.filter((f) => f.contentType === "books").length;
340
+ console.log(`Found ${files.length} files to process`);
341
+ console.log(` - Articles: ${articleCount}`);
342
+ console.log(` - Notes: ${noteCount}`);
343
+ console.log(` - Books: ${bookCount}
344
+ `);
345
+ console.log("Parsing files...");
346
+ const allChunks = [];
347
+ for (const file of files) {
348
+ try {
349
+ const chunks = parseFile(file);
350
+ allChunks.push(...chunks);
351
+ } catch (error) {
352
+ reportCliError(error, "rag-ingest");
353
+ console.error(`Error parsing ${file.path}:`, error);
354
+ }
355
+ }
356
+ console.log(`Total chunks parsed: ${allChunks.length}`);
357
+ if (flags.dryRun) {
358
+ console.log("Dry-run complete. Skipping upsert and prune.");
359
+ return;
360
+ }
361
+ const result = await ingestChunks(allChunks, { force: flags.force });
362
+ const pruneResult = await pruneOrphans(allChunks.map((c) => c.chunk_id));
363
+ const finalStats = await getCollectionStats();
364
+ console.log(`
365
+ Final points count: ${finalStats.pointsCount}`);
366
+ console.log(
367
+ `Ingest completed! (${result.changedChunks} changed, ${result.skippedChunks} skipped, ${pruneResult.deletedCount} pruned)`
368
+ );
369
+ }
370
+ async function main() {
371
+ if (!process.env.QDRANT_URL) {
372
+ throw new Error("QDRANT_URL environment variable is required.");
373
+ }
374
+ const flags = {
375
+ force: process.argv.includes("--force"),
376
+ dryRun: process.argv.includes("--dry-run")
377
+ };
378
+ await ingest(flags);
379
+ }
380
+ export {
381
+ main
382
+ };
@@ -57,13 +57,13 @@ function createObjectStorageClient(config = loadStorageConfig()) {
57
57
  async getObject(bucket, key) {
58
58
  const result = await client.send(new GetObjectCommand({ Bucket: bucket, Key: key }));
59
59
  if (!(result.Body instanceof Readable))
60
- throw new Error("Object storage returned no Node readable body");
60
+ throw new TypeError("Object storage returned no Node readable body");
61
61
  return result.Body;
62
62
  },
63
63
  async downloadObject(bucket, key, destination) {
64
64
  const result = await client.send(new GetObjectCommand({ Bucket: bucket, Key: key }));
65
65
  if (!(result.Body instanceof Readable))
66
- throw new Error("Object storage returned no Node readable body");
66
+ throw new TypeError("Object storage returned no Node readable body");
67
67
  await pipeline(result.Body, createWriteStream(destination));
68
68
  },
69
69
  async statObject(bucket, key) {
@@ -0,0 +1,87 @@
1
+ import { createRequire as __cliCreateRequire } from "node:module"; const require = __cliCreateRequire(import.meta.url);
2
+ import {
3
+ ensureCollection,
4
+ search
5
+ } from "./chunk-J4VLH4VA.js";
6
+ import "./chunk-JZOR5GA2.js";
7
+
8
+ // src/rag/search-command.ts
9
+ function parseArgs() {
10
+ const args = process.argv.slice(2);
11
+ const options = {};
12
+ const positional = [];
13
+ for (let i = 0; i < args.length; i++) {
14
+ const arg = args[i];
15
+ if (arg === "--top-k" && i + 1 < args.length) {
16
+ options.topK = Number(args[++i]);
17
+ } else if (arg === "--type" && i + 1 < args.length) {
18
+ options.type = args[++i];
19
+ } else if (arg === "--heading" && i + 1 < args.length) {
20
+ options.heading = args[++i];
21
+ } else if (arg === "--content-type" && i + 1 < args.length) {
22
+ const value = args[++i];
23
+ const types = value.split(",");
24
+ options.contentType = types.length === 1 ? types[0] : types;
25
+ } else {
26
+ positional.push(arg);
27
+ }
28
+ }
29
+ const query = positional.join(" ");
30
+ if (!query) {
31
+ console.error(
32
+ "Usage: rag-search <query> [--top-k N] [--type markdown_note|bookmark_json] [--heading HEADING] [--content-type articles|books|notes]"
33
+ );
34
+ process.exitCode = 1;
35
+ return void 0;
36
+ }
37
+ return { query, options };
38
+ }
39
+ async function searchContent(query, options = {}) {
40
+ const results = await search(query, {
41
+ topK: options.topK,
42
+ filter: {
43
+ type: options.type,
44
+ top_heading: options.heading,
45
+ content_type: options.contentType
46
+ }
47
+ });
48
+ return { results, query, totalResults: results.length };
49
+ }
50
+ async function search2() {
51
+ const args = parseArgs();
52
+ if (!args) return;
53
+ const { query, options } = args;
54
+ console.log(`Searching for: "${query}"`);
55
+ if (options.topK) console.log(` top-k: ${options.topK}`);
56
+ if (options.type) console.log(` type: ${options.type}`);
57
+ if (options.heading) console.log(` heading: ${options.heading}`);
58
+ if (options.contentType)
59
+ console.log(
60
+ ` content-type: ${Array.isArray(options.contentType) ? options.contentType.join(",") : options.contentType}`
61
+ );
62
+ console.log();
63
+ await ensureCollection();
64
+ const response = await searchContent(query, options);
65
+ console.log(`Found ${response.totalResults} results:
66
+ `);
67
+ for (const [i, result] of response.results.entries()) {
68
+ console.log(`--- Result ${i + 1} (score: ${result.score.toFixed(4)}) ---`);
69
+ console.log(`Title: ${result.title}`);
70
+ if (result.url) console.log(`URL: ${result.url}`);
71
+ console.log(`Type: ${result.type}`);
72
+ console.log(`Content-Type: ${result.content_type}`);
73
+ console.log(`Heading: ${result.heading_path.join(" > ")}`);
74
+ console.log(`
75
+ ${result.text}
76
+ `);
77
+ }
78
+ }
79
+ async function main() {
80
+ if (!process.env.QDRANT_URL) {
81
+ throw new Error("QDRANT_URL environment variable is required.");
82
+ }
83
+ await search2();
84
+ }
85
+ export {
86
+ main
87
+ };
@@ -1,14 +1,16 @@
1
1
  #!/usr/bin/env node
2
2
  import { createRequire as __cliCreateRequire } from "node:module"; const require = __cliCreateRequire(import.meta.url);
3
- import {
4
- createObjectStorageClient
5
- } from "./chunk-M6LFKOUH.js";
6
- import {
7
- createPushoverService
8
- } from "./chunk-7PVU2Z32.js";
9
3
  import {
10
4
  globPaths
11
5
  } from "./chunk-AJMTRCPJ.js";
6
+ import {
7
+ createObjectStorageClient
8
+ } from "./chunk-MVYAZJB2.js";
9
+ import {
10
+ cliNotifications,
11
+ reportCliError,
12
+ runCli
13
+ } from "./chunk-4CXH2QIX.js";
12
14
  import "./chunk-JZOR5GA2.js";
13
15
 
14
16
  // src/cleanup-minio-images.ts
@@ -26,93 +28,72 @@ function listObjects(client, bucket, prefix) {
26
28
  async function main() {
27
29
  const dryRun = process.argv.includes("--dry-run");
28
30
  const env = {
29
- PUSHOVER_URL: process.env.PUSHOVER_URL,
30
- PUSHOVER_USER_KEY: process.env.PUSHOVER_USER_KEY,
31
- PUSHOVER_APP_TOKEN: process.env.PUSHOVER_APP_TOKEN,
32
31
  S3_BUCKET_NAME: process.env.S3_BUCKET_NAME
33
32
  };
34
33
  if (Object.values(env).some((v) => !v)) {
35
34
  throw new Error("Required environment variables are not set.");
36
35
  }
37
36
  const contentsPath = process.env.S_CONTENTS_PATH ?? process.cwd();
38
- const notificationService = createPushoverService({
39
- url: env.PUSHOVER_URL ?? "",
40
- userKey: env.PUSHOVER_USER_KEY ?? "",
41
- appToken: env.PUSHOVER_APP_TOKEN ?? ""
42
- });
37
+ const notificationService = cliNotifications;
43
38
  const storageClient = createObjectStorageClient();
44
39
  const bucketName = env.S3_BUCKET_NAME ?? "";
45
- try {
46
- const localFiles = await globPaths(`${contentsPath}/image/dump/*`);
47
- const localFileNames = new Set(
48
- localFiles.filter((f) => isImageFile(f)).map((f) => basename(f))
49
- );
50
- console.log(
51
- `\u{1F4C1} \u30ED\u30FC\u30AB\u30EB\u306B ${localFileNames.size} \u4EF6\u306E\u753B\u50CF\u30D5\u30A1\u30A4\u30EB\u3092\u691C\u51FA\u3057\u307E\u3057\u305F\u3002`
52
- );
53
- const [originalKeys, thumbnailKeys] = await Promise.all([
54
- listObjects(storageClient, bucketName, `${ORIGINAL_IMAGE_PATH}/`),
55
- listObjects(storageClient, bucketName, `${THUMBNAIL_IMAGE_PATH}/`)
56
- ]);
57
- console.log(
58
- `\u{1F4E6} S3: original ${originalKeys.length} \u4EF6, thumbnail ${thumbnailKeys.length} \u4EF6`
59
- );
60
- const orphanedOriginals = originalKeys.filter(
61
- (key) => !localFileNames.has(basename(key))
62
- );
63
- const orphanedThumbnails = thumbnailKeys.filter(
64
- (key) => !localFileNames.has(basename(key))
65
- );
66
- const totalOrphaned = orphanedOriginals.length + orphanedThumbnails.length;
67
- if (totalOrphaned === 0) {
68
- console.log("\u2705 \u5B64\u7ACB\u30D5\u30A1\u30A4\u30EB\u306A\u3057");
69
- if (!dryRun)
70
- await notificationService.notifyInfo(
71
- `${SCRIPT_NAME} completed: no orphaned files`,
72
- { caller: SCRIPT_NAME }
73
- );
74
- return;
75
- }
76
- console.log(
77
- `\u{1F5D1}\uFE0F \u5B64\u7ACB\u30D5\u30A1\u30A4\u30EB\u691C\u51FA: original ${orphanedOriginals.length} \u4EF6, thumbnail ${orphanedThumbnails.length} \u4EF6`
78
- );
79
- let deletedCount = 0;
80
- let errorCount = 0;
81
- for (const key of [...orphanedOriginals, ...orphanedThumbnails]) {
82
- try {
83
- if (dryRun) {
84
- console.log(`\u{1F50D} [dry-run] \u524A\u9664\u4E88\u5B9A: ${key}`);
85
- } else {
86
- await storageClient.removeObject(bucketName, key);
87
- console.log(`\u{1F5D1}\uFE0F \u524A\u9664: ${key}`);
88
- }
89
- deletedCount++;
90
- } catch (error) {
91
- console.error(`\u274C \u524A\u9664\u30A8\u30E9\u30FC\uFF08${key}\uFF09:`, error);
92
- errorCount++;
93
- }
94
- }
95
- console.log(
96
- `
97
- \u{1F4CA} \u7D50\u679C: \u524A\u9664 ${deletedCount} \u4EF6, \u30A8\u30E9\u30FC ${errorCount} \u4EF6${dryRun ? " (dry-run)" : ""}`
98
- );
40
+ const localFiles = await globPaths(`${contentsPath}/image/dump/*`);
41
+ const localFileNames = new Set(
42
+ localFiles.filter((f) => isImageFile(f)).map((f) => basename(f))
43
+ );
44
+ console.log(
45
+ `\u{1F4C1} \u30ED\u30FC\u30AB\u30EB\u306B ${localFileNames.size} \u4EF6\u306E\u753B\u50CF\u30D5\u30A1\u30A4\u30EB\u3092\u691C\u51FA\u3057\u307E\u3057\u305F\u3002`
46
+ );
47
+ const [originalKeys, thumbnailKeys] = await Promise.all([
48
+ listObjects(storageClient, bucketName, `${ORIGINAL_IMAGE_PATH}/`),
49
+ listObjects(storageClient, bucketName, `${THUMBNAIL_IMAGE_PATH}/`)
50
+ ]);
51
+ console.log(
52
+ `\u{1F4E6} S3: original ${originalKeys.length} \u4EF6, thumbnail ${thumbnailKeys.length} \u4EF6`
53
+ );
54
+ const orphanedOriginals = originalKeys.filter(
55
+ (key) => !localFileNames.has(basename(key))
56
+ );
57
+ const orphanedThumbnails = thumbnailKeys.filter(
58
+ (key) => !localFileNames.has(basename(key))
59
+ );
60
+ const totalOrphaned = orphanedOriginals.length + orphanedThumbnails.length;
61
+ if (totalOrphaned === 0) {
62
+ console.log("\u2705 \u5B64\u7ACB\u30D5\u30A1\u30A4\u30EB\u306A\u3057");
99
63
  if (!dryRun)
100
- await notificationService.notifyInfo(`${SCRIPT_NAME} completed`, {
101
- caller: SCRIPT_NAME
102
- });
103
- } catch (error) {
104
- console.error("\u274C \u30A8\u30E9\u30FC\u304C\u767A\u751F\u3057\u307E\u3057\u305F:", error);
105
- if (!dryRun)
106
- await notificationService.notifyError(
107
- `${SCRIPT_NAME} failed: ${String(error)}`,
108
- {
109
- caller: SCRIPT_NAME
110
- }
64
+ await notificationService.notifyInfo(
65
+ `${SCRIPT_NAME} completed: no orphaned files`,
66
+ { caller: SCRIPT_NAME }
111
67
  );
112
- process.exit(1);
68
+ return;
113
69
  }
70
+ console.log(
71
+ `\u{1F5D1}\uFE0F \u5B64\u7ACB\u30D5\u30A1\u30A4\u30EB\u691C\u51FA: original ${orphanedOriginals.length} \u4EF6, thumbnail ${orphanedThumbnails.length} \u4EF6`
72
+ );
73
+ let deletedCount = 0;
74
+ let errorCount = 0;
75
+ for (const key of [...orphanedOriginals, ...orphanedThumbnails]) {
76
+ try {
77
+ if (dryRun) {
78
+ console.log(`\u{1F50D} [dry-run] \u524A\u9664\u4E88\u5B9A: ${key}`);
79
+ } else {
80
+ await storageClient.removeObject(bucketName, key);
81
+ console.log(`\u{1F5D1}\uFE0F \u524A\u9664: ${key}`);
82
+ }
83
+ deletedCount++;
84
+ } catch (error) {
85
+ reportCliError(error, "cleanup-minio-images");
86
+ console.error(`\u274C \u524A\u9664\u30A8\u30E9\u30FC\uFF08${key}\uFF09:`, error);
87
+ errorCount++;
88
+ }
89
+ }
90
+ console.log(
91
+ `
92
+ \u{1F4CA} \u7D50\u679C: \u524A\u9664 ${deletedCount} \u4EF6, \u30A8\u30E9\u30FC ${errorCount} \u4EF6${dryRun ? " (dry-run)" : ""}`
93
+ );
94
+ if (!dryRun)
95
+ await notificationService.notifyInfo(`${SCRIPT_NAME} completed`, {
96
+ caller: SCRIPT_NAME
97
+ });
114
98
  }
115
- main().catch((error) => {
116
- console.error(error);
117
- process.exit(1);
118
- });
99
+ await runCli("cleanup-minio-images", main);
@@ -6,6 +6,10 @@ import {
6
6
  import {
7
7
  globPaths
8
8
  } from "./chunk-AJMTRCPJ.js";
9
+ import {
10
+ reportCliError,
11
+ runCli
12
+ } from "./chunk-4CXH2QIX.js";
9
13
  import "./chunk-JZOR5GA2.js";
10
14
 
11
15
  // src/enrich-books.ts
@@ -85,6 +89,7 @@ ${body}`;
85
89
  }
86
90
  enrichedCount++;
87
91
  } catch (error) {
92
+ reportCliError(error, "enrich-books");
88
93
  console.error(`\u274C ${fileName}:`, error);
89
94
  errorCount++;
90
95
  }
@@ -94,7 +99,4 @@ ${body}`;
94
99
  \u{1F4CA} \u7D50\u679C: \u66F4\u65B0 ${enrichedCount} \u4EF6, \u30A8\u30E9\u30FC ${errorCount} \u4EF6${dryRun ? " (dry-run)" : ""}`
95
100
  );
96
101
  }
97
- main().catch((error) => {
98
- console.error("\u274C \u30A8\u30E9\u30FC\u304C\u767A\u751F\u3057\u307E\u3057\u305F:", error);
99
- process.exit(1);
100
- });
102
+ await runCli("enrich-books", main);