maoda-commander-tt 0.0.24 → 0.0.27
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/dist/main.js +358 -152
- package/dist/main.js.map +1 -1
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -2227,9 +2227,9 @@ var ShortcutBaseCommand = class extends BaseCommand {
|
|
|
2227
2227
|
stdio: "inherit",
|
|
2228
2228
|
shell: true
|
|
2229
2229
|
});
|
|
2230
|
-
return new Promise((
|
|
2230
|
+
return new Promise((resolve10) => {
|
|
2231
2231
|
child.on("error", (error) => {
|
|
2232
|
-
|
|
2232
|
+
resolve10(
|
|
2233
2233
|
this._makeError(
|
|
2234
2234
|
1,
|
|
2235
2235
|
`\u6267\u884C ${cmdLine} \u5931\u8D25: ${error.message}`
|
|
@@ -2238,11 +2238,11 @@ var ShortcutBaseCommand = class extends BaseCommand {
|
|
|
2238
2238
|
});
|
|
2239
2239
|
child.on("close", (code, signal) => {
|
|
2240
2240
|
if (code === 0) {
|
|
2241
|
-
|
|
2241
|
+
resolve10(this._makeOk());
|
|
2242
2242
|
return;
|
|
2243
2243
|
}
|
|
2244
2244
|
const signalText = signal ? `, signal: ${signal}` : "";
|
|
2245
|
-
|
|
2245
|
+
resolve10(
|
|
2246
2246
|
this._makeError(
|
|
2247
2247
|
code ?? 1,
|
|
2248
2248
|
`\u6267\u884C ${cmdLine} \u5931\u8D25${signalText}`
|
|
@@ -2337,9 +2337,9 @@ var AiBaseCommand = class extends BaseCommand {
|
|
|
2337
2337
|
cwd: process.cwd(),
|
|
2338
2338
|
stdio: "inherit"
|
|
2339
2339
|
});
|
|
2340
|
-
return new Promise((
|
|
2340
|
+
return new Promise((resolve10) => {
|
|
2341
2341
|
child.on("error", (error) => {
|
|
2342
|
-
|
|
2342
|
+
resolve10(
|
|
2343
2343
|
this._makeError(
|
|
2344
2344
|
1,
|
|
2345
2345
|
`\u6267\u884C ${command} ${args.join(" ")} \u5931\u8D25: ${error.message}`
|
|
@@ -2348,11 +2348,11 @@ var AiBaseCommand = class extends BaseCommand {
|
|
|
2348
2348
|
});
|
|
2349
2349
|
child.on("close", (code, signal) => {
|
|
2350
2350
|
if (code === 0) {
|
|
2351
|
-
|
|
2351
|
+
resolve10(this._makeOk());
|
|
2352
2352
|
return;
|
|
2353
2353
|
}
|
|
2354
2354
|
const signalText = signal ? `, signal: ${signal}` : "";
|
|
2355
|
-
|
|
2355
|
+
resolve10(
|
|
2356
2356
|
this._makeError(
|
|
2357
2357
|
code ?? 1,
|
|
2358
2358
|
`\u6267\u884C ${command} ${args.join(" ")} \u5931\u8D25${signalText}`
|
|
@@ -2609,9 +2609,72 @@ import { spawn as spawn3 } from "child_process";
|
|
|
2609
2609
|
import * as fs5 from "fs/promises";
|
|
2610
2610
|
import { homedir as homedir7 } from "os";
|
|
2611
2611
|
import * as path6 from "path";
|
|
2612
|
-
|
|
2612
|
+
|
|
2613
|
+
// src/bedrock/uuid/uuid.ts
|
|
2614
|
+
var generateUuid = (() => {
|
|
2615
|
+
if (typeof crypto === "object" && typeof crypto.randomUUID === "function") {
|
|
2616
|
+
return crypto.randomUUID.bind(crypto);
|
|
2617
|
+
}
|
|
2618
|
+
let getRandomValues;
|
|
2619
|
+
if (typeof crypto === "object" && typeof crypto.getRandomValues === "function") {
|
|
2620
|
+
getRandomValues = crypto.getRandomValues.bind(crypto);
|
|
2621
|
+
} else {
|
|
2622
|
+
getRandomValues = function(bucket) {
|
|
2623
|
+
for (let i = 0; i < bucket.length; i++) {
|
|
2624
|
+
bucket[i] = Math.floor(Math.random() * 256);
|
|
2625
|
+
}
|
|
2626
|
+
return bucket;
|
|
2627
|
+
};
|
|
2628
|
+
}
|
|
2629
|
+
const data = new Uint8Array(16);
|
|
2630
|
+
const hex = [];
|
|
2631
|
+
for (let i = 0; i < 256; i++) {
|
|
2632
|
+
hex.push(i.toString(16).padStart(2, "0"));
|
|
2633
|
+
}
|
|
2634
|
+
return () => {
|
|
2635
|
+
getRandomValues(data);
|
|
2636
|
+
data[6] = data[6] & 15 | 64;
|
|
2637
|
+
data[8] = data[8] & 63 | 128;
|
|
2638
|
+
let i = 0;
|
|
2639
|
+
let result = "";
|
|
2640
|
+
result += hex[data[i++]];
|
|
2641
|
+
result += hex[data[i++]];
|
|
2642
|
+
result += hex[data[i++]];
|
|
2643
|
+
result += hex[data[i++]];
|
|
2644
|
+
result += "-";
|
|
2645
|
+
result += hex[data[i++]];
|
|
2646
|
+
result += hex[data[i++]];
|
|
2647
|
+
result += "-";
|
|
2648
|
+
result += hex[data[i++]];
|
|
2649
|
+
result += hex[data[i++]];
|
|
2650
|
+
result += "-";
|
|
2651
|
+
result += hex[data[i++]];
|
|
2652
|
+
result += hex[data[i++]];
|
|
2653
|
+
result += "-";
|
|
2654
|
+
result += hex[data[i++]];
|
|
2655
|
+
result += hex[data[i++]];
|
|
2656
|
+
result += hex[data[i++]];
|
|
2657
|
+
result += hex[data[i++]];
|
|
2658
|
+
result += hex[data[i++]];
|
|
2659
|
+
result += hex[data[i++]];
|
|
2660
|
+
return result;
|
|
2661
|
+
};
|
|
2662
|
+
})();
|
|
2663
|
+
|
|
2664
|
+
// src/commands/ai/ai-image-command-v2.ts
|
|
2665
|
+
var GENERATED_IMAGES_SUBDIR = "generated_imgs";
|
|
2666
|
+
var GENERATED_IMAGES_DIR = path6.join(
|
|
2667
|
+
homedir7(),
|
|
2668
|
+
".codex",
|
|
2669
|
+
GENERATED_IMAGES_SUBDIR
|
|
2670
|
+
);
|
|
2671
|
+
var GENERATED_IMAGES_PROMPT_DIR = `~/.codex/${GENERATED_IMAGES_SUBDIR}`;
|
|
2672
|
+
var LEGACY_GENERATED_IMAGES_DIR = path6.join(
|
|
2673
|
+
homedir7(),
|
|
2674
|
+
".codex",
|
|
2675
|
+
"generated_images"
|
|
2676
|
+
);
|
|
2613
2677
|
var CODEX_EMPTY_WORKDIR = path6.join(homedir7(), ".codex-empty");
|
|
2614
|
-
var CREATED_AT_TOLERANCE_MS = 1e3;
|
|
2615
2678
|
var AiImageCommandV2 = class extends AiBaseCommand {
|
|
2616
2679
|
_meta() {
|
|
2617
2680
|
return {
|
|
@@ -2631,13 +2694,14 @@ var AiImageCommandV2 = class extends AiBaseCommand {
|
|
|
2631
2694
|
this._outputJsonError(1, "\u8BF7\u8F93\u5165\u751F\u56FE\u63D0\u793A\u8BCD");
|
|
2632
2695
|
return this._makeOk();
|
|
2633
2696
|
}
|
|
2634
|
-
const
|
|
2635
|
-
const
|
|
2697
|
+
const uuid = generateUuid();
|
|
2698
|
+
const imagePath = getGeneratedImagePath(uuid);
|
|
2699
|
+
const runResult = await this._runCodexImagePrompt(prompt, uuid);
|
|
2636
2700
|
if (!runResult.ok) {
|
|
2637
2701
|
this._outputJsonError(runResult.code, runResult.msg);
|
|
2638
2702
|
return this._makeOk();
|
|
2639
2703
|
}
|
|
2640
|
-
const imagePathResult = await
|
|
2704
|
+
const imagePathResult = await findGeneratedImageFile(imagePath);
|
|
2641
2705
|
if (!imagePathResult.ok) {
|
|
2642
2706
|
this._outputJsonError(imagePathResult.code, imagePathResult.msg);
|
|
2643
2707
|
return this._makeOk();
|
|
@@ -2647,7 +2711,7 @@ var AiImageCommandV2 = class extends AiBaseCommand {
|
|
|
2647
2711
|
});
|
|
2648
2712
|
return this._makeOk();
|
|
2649
2713
|
}
|
|
2650
|
-
async _runCodexImagePrompt(prompt) {
|
|
2714
|
+
async _runCodexImagePrompt(prompt, uuid) {
|
|
2651
2715
|
try {
|
|
2652
2716
|
await fs5.mkdir(CODEX_EMPTY_WORKDIR, { recursive: true });
|
|
2653
2717
|
} catch (error) {
|
|
@@ -2657,8 +2721,19 @@ var AiImageCommandV2 = class extends AiBaseCommand {
|
|
|
2657
2721
|
`\u521B\u5EFA codex \u7A7A\u5DE5\u4F5C\u76EE\u5F55\u5931\u8D25: ${CODEX_EMPTY_WORKDIR}: ${message}`
|
|
2658
2722
|
);
|
|
2659
2723
|
}
|
|
2724
|
+
try {
|
|
2725
|
+
await fs5.mkdir(GENERATED_IMAGES_DIR, { recursive: true });
|
|
2726
|
+
} catch (error) {
|
|
2727
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2728
|
+
return this._makeError(
|
|
2729
|
+
1,
|
|
2730
|
+
`\u521B\u5EFA codex \u751F\u56FE\u8F93\u51FA\u76EE\u5F55\u5931\u8D25: ${GENERATED_IMAGES_DIR}: ${message}`
|
|
2731
|
+
);
|
|
2732
|
+
}
|
|
2660
2733
|
const codexPrompt = `\u751F\u6210\u56FE\u7247(\u4F7F\u7528\u975ECLI\u6A21\u5F0F\u6765\u751F\u6210\uFF0C\u4F7F\u7528\u53EF\u652F\u6301\u7684\u9AD8\u753B\u8D28)\uFF0C\u5177\u4F53\u8981\u6C42\u5982\u4E0B:
|
|
2661
|
-
${prompt}
|
|
2734
|
+
${prompt}
|
|
2735
|
+
|
|
2736
|
+
<system-reminder>\u751F\u6210\u7ED3\u675F\u540E\u5C06\u751F\u6210\u7684\u56FE\u7247\u62F7\u8D1D\u5230\u6307\u5B9A\u8DEF\u5F84: \`${getGeneratedImagePromptPath(uuid)}\`\u3002</system-reminder>`;
|
|
2662
2737
|
const args = [
|
|
2663
2738
|
"exec",
|
|
2664
2739
|
"-C",
|
|
@@ -2672,9 +2747,9 @@ var AiImageCommandV2 = class extends AiBaseCommand {
|
|
|
2672
2747
|
cwd: process.cwd(),
|
|
2673
2748
|
stdio: "ignore"
|
|
2674
2749
|
});
|
|
2675
|
-
return new Promise((
|
|
2750
|
+
return new Promise((resolve10) => {
|
|
2676
2751
|
child.on("error", (error) => {
|
|
2677
|
-
|
|
2752
|
+
resolve10(
|
|
2678
2753
|
this._makeError(
|
|
2679
2754
|
1,
|
|
2680
2755
|
`\u6267\u884C codex ${args.join(" ")} \u5931\u8D25: ${error.message}`
|
|
@@ -2683,11 +2758,11 @@ var AiImageCommandV2 = class extends AiBaseCommand {
|
|
|
2683
2758
|
});
|
|
2684
2759
|
child.on("close", (code, signal) => {
|
|
2685
2760
|
if (code === 0) {
|
|
2686
|
-
|
|
2761
|
+
resolve10(this._makeOk());
|
|
2687
2762
|
return;
|
|
2688
2763
|
}
|
|
2689
2764
|
const signalText = signal ? `, signal: ${signal}` : "";
|
|
2690
|
-
|
|
2765
|
+
resolve10(
|
|
2691
2766
|
this._makeError(
|
|
2692
2767
|
code ?? 1,
|
|
2693
2768
|
`\u6267\u884C codex ${args.join(" ")} \u5931\u8D25${signalText}`
|
|
@@ -2697,88 +2772,27 @@ var AiImageCommandV2 = class extends AiBaseCommand {
|
|
|
2697
2772
|
});
|
|
2698
2773
|
}
|
|
2699
2774
|
};
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
GENERATED_IMAGES_DIR,
|
|
2703
|
-
minCreatedAt
|
|
2704
|
-
);
|
|
2705
|
-
if (!latestDirectoryResult.ok) {
|
|
2706
|
-
return latestDirectoryResult;
|
|
2707
|
-
}
|
|
2708
|
-
return findLatestCreatedFile(latestDirectoryResult.value);
|
|
2775
|
+
function getGeneratedImagePath(uuid) {
|
|
2776
|
+
return path6.join(GENERATED_IMAGES_DIR, `${uuid}.png`);
|
|
2709
2777
|
}
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
if (!entriesResult.ok) {
|
|
2713
|
-
return entriesResult;
|
|
2714
|
-
}
|
|
2715
|
-
const directories = await collectCreatedEntries(
|
|
2716
|
-
directoryPath,
|
|
2717
|
-
entriesResult.value.filter((entry) => entry.isDirectory()).map((entry) => entry.name)
|
|
2718
|
-
);
|
|
2719
|
-
if (!directories.ok) {
|
|
2720
|
-
return directories;
|
|
2721
|
-
}
|
|
2722
|
-
const latest = directories.value.filter(
|
|
2723
|
-
(entry) => entry.createdAt >= minCreatedAt - CREATED_AT_TOLERANCE_MS
|
|
2724
|
-
).sort(compareCreatedEntryDesc)[0];
|
|
2725
|
-
if (!latest) {
|
|
2726
|
-
return makeErrorForPath(
|
|
2727
|
-
1,
|
|
2728
|
-
"\u672A\u627E\u5230\u672C\u6B21 codex \u751F\u56FE\u8F93\u51FA\u5B50\u76EE\u5F55",
|
|
2729
|
-
directoryPath
|
|
2730
|
-
);
|
|
2731
|
-
}
|
|
2732
|
-
return makeOkWith(latest.filePath);
|
|
2778
|
+
function getGeneratedImagePromptPath(uuid) {
|
|
2779
|
+
return `${GENERATED_IMAGES_PROMPT_DIR}/${uuid}.png`;
|
|
2733
2780
|
}
|
|
2734
|
-
async function
|
|
2735
|
-
const entriesResult = await readDirectoryEntries(directoryPath);
|
|
2736
|
-
if (!entriesResult.ok) {
|
|
2737
|
-
return entriesResult;
|
|
2738
|
-
}
|
|
2739
|
-
const files = await collectCreatedEntries(
|
|
2740
|
-
directoryPath,
|
|
2741
|
-
entriesResult.value.filter((entry) => entry.isFile()).map((entry) => entry.name)
|
|
2742
|
-
);
|
|
2743
|
-
if (!files.ok) {
|
|
2744
|
-
return files;
|
|
2745
|
-
}
|
|
2746
|
-
const latest = files.value.sort(compareCreatedEntryDesc)[0];
|
|
2747
|
-
if (!latest) {
|
|
2748
|
-
return makeErrorForPath(1, "\u6700\u65B0\u751F\u56FE\u8F93\u51FA\u76EE\u5F55\u4E2D\u672A\u627E\u5230\u6587\u4EF6", directoryPath);
|
|
2749
|
-
}
|
|
2750
|
-
return makeOkWith(path6.resolve(latest.filePath));
|
|
2751
|
-
}
|
|
2752
|
-
async function readDirectoryEntries(directoryPath) {
|
|
2781
|
+
async function findGeneratedImageFile(filePath) {
|
|
2753
2782
|
try {
|
|
2754
|
-
|
|
2783
|
+
const stats = await fs5.stat(filePath);
|
|
2784
|
+
if (!stats.isFile()) {
|
|
2785
|
+
return makeErrorForPath(1, "\u672C\u6B21 codex \u751F\u56FE\u8F93\u51FA\u8DEF\u5F84\u4E0D\u662F\u6587\u4EF6", filePath);
|
|
2786
|
+
}
|
|
2787
|
+
return makeOkWith(path6.resolve(filePath));
|
|
2755
2788
|
} catch (error) {
|
|
2756
2789
|
const message = error instanceof Error ? error.message : String(error);
|
|
2757
|
-
return makeErrorForPath(
|
|
2758
|
-
|
|
2759
|
-
}
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
for (const name of names) {
|
|
2763
|
-
const filePath = path6.join(parentPath, name);
|
|
2764
|
-
try {
|
|
2765
|
-
const stats = await fs5.stat(filePath);
|
|
2766
|
-
entries.push({
|
|
2767
|
-
filePath,
|
|
2768
|
-
createdAt: getCreatedAt(stats)
|
|
2769
|
-
});
|
|
2770
|
-
} catch (error) {
|
|
2771
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
2772
|
-
return makeErrorForPath(1, `\u8BFB\u53D6\u8DEF\u5F84\u72B6\u6001\u5931\u8D25: ${message}`, filePath);
|
|
2773
|
-
}
|
|
2790
|
+
return makeErrorForPath(
|
|
2791
|
+
1,
|
|
2792
|
+
`\u672A\u627E\u5230\u672C\u6B21 codex \u751F\u56FE\u8F93\u51FA\u6587\u4EF6: ${message}`,
|
|
2793
|
+
filePath
|
|
2794
|
+
);
|
|
2774
2795
|
}
|
|
2775
|
-
return makeOkWith(entries);
|
|
2776
|
-
}
|
|
2777
|
-
function getCreatedAt(stats) {
|
|
2778
|
-
return stats.birthtimeMs || stats.ctimeMs || stats.mtimeMs;
|
|
2779
|
-
}
|
|
2780
|
-
function compareCreatedEntryDesc(a, b) {
|
|
2781
|
-
return b.createdAt - a.createdAt || b.filePath.localeCompare(a.filePath);
|
|
2782
2796
|
}
|
|
2783
2797
|
function makeErrorForPath(code, message, filePath) {
|
|
2784
2798
|
return makeError(code, `${message}: ${filePath}`);
|
|
@@ -2788,57 +2802,6 @@ function makeErrorForPath(code, message, filePath) {
|
|
|
2788
2802
|
import * as path7 from "path";
|
|
2789
2803
|
import { Option as Option8 } from "commander";
|
|
2790
2804
|
|
|
2791
|
-
// src/bedrock/uuid/uuid.ts
|
|
2792
|
-
var generateUuid = (() => {
|
|
2793
|
-
if (typeof crypto === "object" && typeof crypto.randomUUID === "function") {
|
|
2794
|
-
return crypto.randomUUID.bind(crypto);
|
|
2795
|
-
}
|
|
2796
|
-
let getRandomValues;
|
|
2797
|
-
if (typeof crypto === "object" && typeof crypto.getRandomValues === "function") {
|
|
2798
|
-
getRandomValues = crypto.getRandomValues.bind(crypto);
|
|
2799
|
-
} else {
|
|
2800
|
-
getRandomValues = function(bucket) {
|
|
2801
|
-
for (let i = 0; i < bucket.length; i++) {
|
|
2802
|
-
bucket[i] = Math.floor(Math.random() * 256);
|
|
2803
|
-
}
|
|
2804
|
-
return bucket;
|
|
2805
|
-
};
|
|
2806
|
-
}
|
|
2807
|
-
const data = new Uint8Array(16);
|
|
2808
|
-
const hex = [];
|
|
2809
|
-
for (let i = 0; i < 256; i++) {
|
|
2810
|
-
hex.push(i.toString(16).padStart(2, "0"));
|
|
2811
|
-
}
|
|
2812
|
-
return () => {
|
|
2813
|
-
getRandomValues(data);
|
|
2814
|
-
data[6] = data[6] & 15 | 64;
|
|
2815
|
-
data[8] = data[8] & 63 | 128;
|
|
2816
|
-
let i = 0;
|
|
2817
|
-
let result = "";
|
|
2818
|
-
result += hex[data[i++]];
|
|
2819
|
-
result += hex[data[i++]];
|
|
2820
|
-
result += hex[data[i++]];
|
|
2821
|
-
result += hex[data[i++]];
|
|
2822
|
-
result += "-";
|
|
2823
|
-
result += hex[data[i++]];
|
|
2824
|
-
result += hex[data[i++]];
|
|
2825
|
-
result += "-";
|
|
2826
|
-
result += hex[data[i++]];
|
|
2827
|
-
result += hex[data[i++]];
|
|
2828
|
-
result += "-";
|
|
2829
|
-
result += hex[data[i++]];
|
|
2830
|
-
result += hex[data[i++]];
|
|
2831
|
-
result += "-";
|
|
2832
|
-
result += hex[data[i++]];
|
|
2833
|
-
result += hex[data[i++]];
|
|
2834
|
-
result += hex[data[i++]];
|
|
2835
|
-
result += hex[data[i++]];
|
|
2836
|
-
result += hex[data[i++]];
|
|
2837
|
-
result += hex[data[i++]];
|
|
2838
|
-
return result;
|
|
2839
|
-
};
|
|
2840
|
-
})();
|
|
2841
|
-
|
|
2842
2805
|
// src/commands/pippit/modules/submit-direct/submit-direct-cn.ts
|
|
2843
2806
|
var SUBMIT_DIRECT_URL = "https://xyq.jianying.com/api/biz/v1/agent/submit_run";
|
|
2844
2807
|
function normalizeToken5(token) {
|
|
@@ -3579,7 +3542,7 @@ function extractErrorText(payload) {
|
|
|
3579
3542
|
return void 0;
|
|
3580
3543
|
}
|
|
3581
3544
|
function delay(ms) {
|
|
3582
|
-
return new Promise((
|
|
3545
|
+
return new Promise((resolve10) => setTimeout(resolve10, ms));
|
|
3583
3546
|
}
|
|
3584
3547
|
async function appendPollingUrl(pollingUrl, generationId) {
|
|
3585
3548
|
const locationResult = await resolveConfigLocation();
|
|
@@ -3780,7 +3743,7 @@ var AiQueryTaskCommand = class extends AiBaseCommand {
|
|
|
3780
3743
|
PPE_ENV,
|
|
3781
3744
|
"--json"
|
|
3782
3745
|
];
|
|
3783
|
-
return new Promise((
|
|
3746
|
+
return new Promise((resolve10) => {
|
|
3784
3747
|
const child = spawn4(BIN, args, { cwd: process.cwd() });
|
|
3785
3748
|
let stdout = "";
|
|
3786
3749
|
let stderr = "";
|
|
@@ -3791,7 +3754,7 @@ var AiQueryTaskCommand = class extends AiBaseCommand {
|
|
|
3791
3754
|
stderr += chunk.toString();
|
|
3792
3755
|
});
|
|
3793
3756
|
child.on("error", (error) => {
|
|
3794
|
-
|
|
3757
|
+
resolve10(
|
|
3795
3758
|
makeError(
|
|
3796
3759
|
1,
|
|
3797
3760
|
`\u6267\u884C ${BIN} ${args.join(" ")} \u5931\u8D25: ${error.message}`
|
|
@@ -3800,13 +3763,13 @@ var AiQueryTaskCommand = class extends AiBaseCommand {
|
|
|
3800
3763
|
});
|
|
3801
3764
|
child.on("close", (code, signal) => {
|
|
3802
3765
|
if (code === 0) {
|
|
3803
|
-
|
|
3766
|
+
resolve10(makeOkWith({ stdout, stderr }));
|
|
3804
3767
|
return;
|
|
3805
3768
|
}
|
|
3806
3769
|
const signalText = signal ? `, signal: ${signal}` : "";
|
|
3807
3770
|
const stderrText = stderr.trim() ? `
|
|
3808
3771
|
${stderr.trim()}` : "";
|
|
3809
|
-
|
|
3772
|
+
resolve10(
|
|
3810
3773
|
makeError(
|
|
3811
3774
|
code ?? 1,
|
|
3812
3775
|
`\u6267\u884C ${BIN} ${args.join(" ")} \u5931\u8D25${signalText}${stderrText}`
|
|
@@ -3846,7 +3809,7 @@ function tryParseJson(text) {
|
|
|
3846
3809
|
}
|
|
3847
3810
|
}
|
|
3848
3811
|
function delay2(ms) {
|
|
3849
|
-
return new Promise((
|
|
3812
|
+
return new Promise((resolve10) => setTimeout(resolve10, ms));
|
|
3850
3813
|
}
|
|
3851
3814
|
|
|
3852
3815
|
// src/commands/ai/ai-command.ts
|
|
@@ -4303,6 +4266,248 @@ function asRecord2(value) {
|
|
|
4303
4266
|
return value;
|
|
4304
4267
|
}
|
|
4305
4268
|
|
|
4269
|
+
// src/commands/tool/tool-random-files-command.ts
|
|
4270
|
+
import { readdir as readdir3, stat as stat5 } from "fs/promises";
|
|
4271
|
+
import { homedir as homedir9 } from "os";
|
|
4272
|
+
import { extname as extname4, join as join13, resolve as resolve9 } from "path";
|
|
4273
|
+
var DEFAULT_COUNT = 1;
|
|
4274
|
+
var DEFAULT_SUFFIXES = ["png", "jpeg", "jpg"];
|
|
4275
|
+
var DEFAULT_DEPTH = 3;
|
|
4276
|
+
var ToolRandomFilesCommand = class extends BaseCommand {
|
|
4277
|
+
_meta() {
|
|
4278
|
+
return {
|
|
4279
|
+
name: "random-files",
|
|
4280
|
+
description: "\u4ECE\u6587\u4EF6\u5939\u968F\u673A\u9009\u62E9\u6587\u4EF6",
|
|
4281
|
+
aliases: ["randf"]
|
|
4282
|
+
};
|
|
4283
|
+
}
|
|
4284
|
+
_configureArguments(cmd) {
|
|
4285
|
+
cmd.argument("<folder...>", "\u76EE\u6807\u6587\u4EF6\u5939");
|
|
4286
|
+
}
|
|
4287
|
+
_configureOptions(cmd) {
|
|
4288
|
+
cmd.option("-c, --count <count>", "\u968F\u673A\u6587\u4EF6\u6570\u91CF", String(DEFAULT_COUNT));
|
|
4289
|
+
cmd.option(
|
|
4290
|
+
"-s, --suffix <suffixes>",
|
|
4291
|
+
"\u6587\u4EF6\u540E\u7F00\uFF0C\u591A\u4E2A\u540E\u7F00\u7528\u9017\u53F7\u5206\u9694",
|
|
4292
|
+
DEFAULT_SUFFIXES.join(",")
|
|
4293
|
+
);
|
|
4294
|
+
cmd.option(
|
|
4295
|
+
"-d, --depth <depth>",
|
|
4296
|
+
"\u9012\u5F52\u6587\u4EF6\u5C42\u7EA7\uFF0C\u76EE\u6807\u6587\u4EF6\u5939\u4E3A\u7B2C 1 \u5C42",
|
|
4297
|
+
String(DEFAULT_DEPTH)
|
|
4298
|
+
);
|
|
4299
|
+
cmd.addHelpText(
|
|
4300
|
+
"after",
|
|
4301
|
+
[
|
|
4302
|
+
"",
|
|
4303
|
+
"\u8F93\u5165\u6848\u4F8B:",
|
|
4304
|
+
" tt tool random-files ~/Pictures -c 3",
|
|
4305
|
+
" tt tool random-files ./assets --suffix webp,gif --depth 2"
|
|
4306
|
+
].join("\n")
|
|
4307
|
+
);
|
|
4308
|
+
}
|
|
4309
|
+
async _execute(ctx) {
|
|
4310
|
+
const folderInputResult = parseFolderInput(ctx.args[0]);
|
|
4311
|
+
if (!folderInputResult.ok) {
|
|
4312
|
+
return this._makeError(1, folderInputResult.msg);
|
|
4313
|
+
}
|
|
4314
|
+
const countResult = parsePositiveInteger(
|
|
4315
|
+
ctx.options.count,
|
|
4316
|
+
DEFAULT_COUNT,
|
|
4317
|
+
"\u968F\u673A\u6587\u4EF6\u6570\u91CF"
|
|
4318
|
+
);
|
|
4319
|
+
if (!countResult.ok) {
|
|
4320
|
+
return this._makeError(1, countResult.msg);
|
|
4321
|
+
}
|
|
4322
|
+
const suffixResult = parseSuffixes(ctx.options.suffix);
|
|
4323
|
+
if (!suffixResult.ok) {
|
|
4324
|
+
return this._makeError(1, suffixResult.msg);
|
|
4325
|
+
}
|
|
4326
|
+
const depthResult = parsePositiveInteger(
|
|
4327
|
+
ctx.options.depth,
|
|
4328
|
+
DEFAULT_DEPTH,
|
|
4329
|
+
"\u9012\u5F52\u6587\u4EF6\u5C42\u7EA7"
|
|
4330
|
+
);
|
|
4331
|
+
if (!depthResult.ok) {
|
|
4332
|
+
return this._makeError(1, depthResult.msg);
|
|
4333
|
+
}
|
|
4334
|
+
const folderResult = await resolveFolder(folderInputResult.value);
|
|
4335
|
+
if (!folderResult.ok) {
|
|
4336
|
+
return this._makeError(1, folderResult.msg);
|
|
4337
|
+
}
|
|
4338
|
+
const candidates = await collectMatchingFiles({
|
|
4339
|
+
folder: folderResult.value,
|
|
4340
|
+
suffixes: suffixResult.value,
|
|
4341
|
+
maxDepth: depthResult.value,
|
|
4342
|
+
currentDepth: 1
|
|
4343
|
+
});
|
|
4344
|
+
if (candidates.length === 0) {
|
|
4345
|
+
return this._makeError(
|
|
4346
|
+
1,
|
|
4347
|
+
`\u672A\u627E\u5230\u5339\u914D\u540E\u7F00\u7684\u6587\u4EF6: ${Array.from(suffixResult.value).join(",")}`
|
|
4348
|
+
);
|
|
4349
|
+
}
|
|
4350
|
+
if (candidates.length < countResult.value) {
|
|
4351
|
+
return this._makeError(
|
|
4352
|
+
1,
|
|
4353
|
+
`\u5339\u914D\u6587\u4EF6\u6570\u91CF\u4E0D\u8DB3: \u9700\u8981 ${countResult.value} \u4E2A\uFF0C\u627E\u5230 ${candidates.length} \u4E2A`
|
|
4354
|
+
);
|
|
4355
|
+
}
|
|
4356
|
+
this._outputJson({
|
|
4357
|
+
files: pickRandomFiles(candidates, countResult.value)
|
|
4358
|
+
});
|
|
4359
|
+
return this._makeOk();
|
|
4360
|
+
}
|
|
4361
|
+
_outputJson(data) {
|
|
4362
|
+
this._output(JSON.stringify(data, null, 2));
|
|
4363
|
+
}
|
|
4364
|
+
};
|
|
4365
|
+
function parseFolderInput(value) {
|
|
4366
|
+
const folder = stringifyFolderArg(value);
|
|
4367
|
+
if (!folder) {
|
|
4368
|
+
return { ok: false, msg: "\u76EE\u6807\u6587\u4EF6\u5939\u4E0D\u80FD\u4E3A\u7A7A" };
|
|
4369
|
+
}
|
|
4370
|
+
return {
|
|
4371
|
+
ok: true,
|
|
4372
|
+
value: {
|
|
4373
|
+
raw: folder,
|
|
4374
|
+
candidates: createFolderCandidates(folder)
|
|
4375
|
+
}
|
|
4376
|
+
};
|
|
4377
|
+
}
|
|
4378
|
+
function stringifyFolderArg(value) {
|
|
4379
|
+
if (Array.isArray(value)) {
|
|
4380
|
+
return value.map((item) => String(item)).join(" ").trim();
|
|
4381
|
+
}
|
|
4382
|
+
return String(value ?? "").trim();
|
|
4383
|
+
}
|
|
4384
|
+
function createFolderCandidates(value) {
|
|
4385
|
+
const candidates = [
|
|
4386
|
+
value,
|
|
4387
|
+
trimWrappingQuotes(value),
|
|
4388
|
+
unescapeShellPath(trimWrappingQuotes(value))
|
|
4389
|
+
];
|
|
4390
|
+
return uniqueStrings(candidates.map((candidate) => expandHomeDir2(candidate)));
|
|
4391
|
+
}
|
|
4392
|
+
function trimWrappingQuotes(value) {
|
|
4393
|
+
if (value.length < 2) {
|
|
4394
|
+
return value;
|
|
4395
|
+
}
|
|
4396
|
+
const firstChar = value[0];
|
|
4397
|
+
const lastChar = value[value.length - 1];
|
|
4398
|
+
if (firstChar === '"' && lastChar === '"' || firstChar === "'" && lastChar === "'") {
|
|
4399
|
+
return value.slice(1, -1);
|
|
4400
|
+
}
|
|
4401
|
+
return value;
|
|
4402
|
+
}
|
|
4403
|
+
function unescapeShellPath(value) {
|
|
4404
|
+
return value.replace(
|
|
4405
|
+
/\\([ \t\n\r"'`$&;()<>|*?[\]{}!#~\\])/g,
|
|
4406
|
+
"$1"
|
|
4407
|
+
);
|
|
4408
|
+
}
|
|
4409
|
+
function uniqueStrings(values) {
|
|
4410
|
+
return [...new Set(values)];
|
|
4411
|
+
}
|
|
4412
|
+
async function resolveFolder(folderInput) {
|
|
4413
|
+
let firstExistingPath;
|
|
4414
|
+
for (const candidate of folderInput.candidates) {
|
|
4415
|
+
const folderStat = await safeStat2(candidate);
|
|
4416
|
+
if (!folderStat) {
|
|
4417
|
+
continue;
|
|
4418
|
+
}
|
|
4419
|
+
if (folderStat.isDirectory()) {
|
|
4420
|
+
return { ok: true, value: candidate };
|
|
4421
|
+
}
|
|
4422
|
+
firstExistingPath ??= candidate;
|
|
4423
|
+
}
|
|
4424
|
+
if (firstExistingPath) {
|
|
4425
|
+
return { ok: false, msg: `\u76EE\u6807\u8DEF\u5F84\u4E0D\u662F\u6587\u4EF6\u5939: ${firstExistingPath}` };
|
|
4426
|
+
}
|
|
4427
|
+
return { ok: false, msg: `\u76EE\u6807\u6587\u4EF6\u5939\u4E0D\u5B58\u5728: ${folderInput.raw}` };
|
|
4428
|
+
}
|
|
4429
|
+
function parsePositiveInteger(value, defaultValue, label) {
|
|
4430
|
+
const rawValue = String(value ?? defaultValue).trim();
|
|
4431
|
+
const parsedValue = Number(rawValue);
|
|
4432
|
+
if (!Number.isInteger(parsedValue) || parsedValue <= 0) {
|
|
4433
|
+
return { ok: false, msg: `${label}\u5FC5\u987B\u662F\u6B63\u6574\u6570` };
|
|
4434
|
+
}
|
|
4435
|
+
return { ok: true, value: parsedValue };
|
|
4436
|
+
}
|
|
4437
|
+
function parseSuffixes(value) {
|
|
4438
|
+
const suffixes = String(value ?? DEFAULT_SUFFIXES.join(",")).split(/[,,\s]+/).map((suffix) => normalizeSuffix(suffix)).filter((suffix) => suffix.length > 0);
|
|
4439
|
+
if (suffixes.length === 0) {
|
|
4440
|
+
return { ok: false, msg: "\u6587\u4EF6\u540E\u7F00\u4E0D\u80FD\u4E3A\u7A7A" };
|
|
4441
|
+
}
|
|
4442
|
+
return { ok: true, value: new Set(suffixes) };
|
|
4443
|
+
}
|
|
4444
|
+
function normalizeSuffix(value) {
|
|
4445
|
+
return value.trim().replace(/^\.+/, "").toLowerCase();
|
|
4446
|
+
}
|
|
4447
|
+
async function collectMatchingFiles(options) {
|
|
4448
|
+
const entries = await safeReadDir(options.folder);
|
|
4449
|
+
const files = [];
|
|
4450
|
+
for (const entry of entries) {
|
|
4451
|
+
const entryPath = join13(options.folder, entry.name);
|
|
4452
|
+
if (entry.isFile()) {
|
|
4453
|
+
if (hasAllowedSuffix(entry.name, options.suffixes)) {
|
|
4454
|
+
files.push(entryPath);
|
|
4455
|
+
}
|
|
4456
|
+
continue;
|
|
4457
|
+
}
|
|
4458
|
+
if (entry.isDirectory() && options.currentDepth < options.maxDepth) {
|
|
4459
|
+
files.push(
|
|
4460
|
+
...await collectMatchingFiles({
|
|
4461
|
+
folder: entryPath,
|
|
4462
|
+
suffixes: options.suffixes,
|
|
4463
|
+
maxDepth: options.maxDepth,
|
|
4464
|
+
currentDepth: options.currentDepth + 1
|
|
4465
|
+
})
|
|
4466
|
+
);
|
|
4467
|
+
}
|
|
4468
|
+
}
|
|
4469
|
+
return files;
|
|
4470
|
+
}
|
|
4471
|
+
async function safeReadDir(folder) {
|
|
4472
|
+
try {
|
|
4473
|
+
const entries = await readdir3(folder, { withFileTypes: true });
|
|
4474
|
+
return entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
4475
|
+
} catch {
|
|
4476
|
+
return [];
|
|
4477
|
+
}
|
|
4478
|
+
}
|
|
4479
|
+
function hasAllowedSuffix(fileName, suffixes) {
|
|
4480
|
+
const suffix = normalizeSuffix(extname4(fileName));
|
|
4481
|
+
return suffixes.has(suffix);
|
|
4482
|
+
}
|
|
4483
|
+
function pickRandomFiles(files, count) {
|
|
4484
|
+
const shuffled = [...files];
|
|
4485
|
+
for (let index = shuffled.length - 1; index > 0; index -= 1) {
|
|
4486
|
+
const randomIndex = Math.floor(Math.random() * (index + 1));
|
|
4487
|
+
[shuffled[index], shuffled[randomIndex]] = [
|
|
4488
|
+
shuffled[randomIndex],
|
|
4489
|
+
shuffled[index]
|
|
4490
|
+
];
|
|
4491
|
+
}
|
|
4492
|
+
return shuffled.slice(0, count);
|
|
4493
|
+
}
|
|
4494
|
+
async function safeStat2(path10) {
|
|
4495
|
+
try {
|
|
4496
|
+
return await stat5(path10);
|
|
4497
|
+
} catch {
|
|
4498
|
+
return void 0;
|
|
4499
|
+
}
|
|
4500
|
+
}
|
|
4501
|
+
function expandHomeDir2(path10) {
|
|
4502
|
+
if (path10 === "~") {
|
|
4503
|
+
return homedir9();
|
|
4504
|
+
}
|
|
4505
|
+
if (path10.startsWith("~/")) {
|
|
4506
|
+
return join13(homedir9(), path10.slice(2));
|
|
4507
|
+
}
|
|
4508
|
+
return resolve9(path10);
|
|
4509
|
+
}
|
|
4510
|
+
|
|
4306
4511
|
// src/commands/tool/tool-command.ts
|
|
4307
4512
|
var ToolCommand = class extends BaseSubcommandHost {
|
|
4308
4513
|
_meta() {
|
|
@@ -4314,6 +4519,7 @@ var ToolCommand = class extends BaseSubcommandHost {
|
|
|
4314
4519
|
_registerSubcommands() {
|
|
4315
4520
|
this._addSubcommand(new ToolCanvasCommand());
|
|
4316
4521
|
this._addSubcommand(new ToolDrawCommand());
|
|
4522
|
+
this._addSubcommand(new ToolRandomFilesCommand());
|
|
4317
4523
|
}
|
|
4318
4524
|
};
|
|
4319
4525
|
|