maoda-commander-tt 0.0.23 → 0.0.26
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 +305 -134
- package/dist/main.js.map +1 -1
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -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",
|
|
@@ -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) {
|
|
@@ -4096,6 +4059,213 @@ function compareCanvasMatches(a, b) {
|
|
|
4096
4059
|
return a.nodeIndex - b.nodeIndex;
|
|
4097
4060
|
}
|
|
4098
4061
|
|
|
4062
|
+
// src/commands/tool/tool-draw-command.ts
|
|
4063
|
+
var ToolDrawCommand = class extends BaseCommand {
|
|
4064
|
+
_meta() {
|
|
4065
|
+
return {
|
|
4066
|
+
name: "draw",
|
|
4067
|
+
description: "\u6309\u6982\u7387\u5206\u5E03\u62BD\u7B7E"
|
|
4068
|
+
};
|
|
4069
|
+
}
|
|
4070
|
+
_configureArguments(cmd) {
|
|
4071
|
+
cmd.argument("<count>", "\u8981\u62BD\u53D6\u7684\u4E2A\u6570");
|
|
4072
|
+
cmd.argument("<distribution...>", "\u6982\u7387\u5206\u5E03\uFF0C\u652F\u6301\u7B80\u5199\u6216 JSON \u6570\u7EC4");
|
|
4073
|
+
cmd.addHelpText(
|
|
4074
|
+
"after",
|
|
4075
|
+
[
|
|
4076
|
+
"",
|
|
4077
|
+
"\u8F93\u5165\u6848\u4F8B:",
|
|
4078
|
+
' tt tool draw 3 "A A:50,B B:30,C C:20"',
|
|
4079
|
+
` tt tool draw 3 '[{"name":"\u4E2D\u56FD \u6210\u90FD","weight":50},{"name":"Los Angeles","weight":50}]'`
|
|
4080
|
+
].join("\n")
|
|
4081
|
+
);
|
|
4082
|
+
}
|
|
4083
|
+
async _execute(ctx) {
|
|
4084
|
+
const countResult = parseCount(ctx.args[0]);
|
|
4085
|
+
if (!countResult.ok) {
|
|
4086
|
+
return this._makeError(1, countResult.msg);
|
|
4087
|
+
}
|
|
4088
|
+
const distributionText = stringifyDistributionArg(ctx.args[1]);
|
|
4089
|
+
if (!distributionText) {
|
|
4090
|
+
return this._makeError(1, "\u6982\u7387\u5206\u5E03\u4E0D\u80FD\u4E3A\u7A7A");
|
|
4091
|
+
}
|
|
4092
|
+
const distributionResult = parseDistribution(distributionText);
|
|
4093
|
+
if (!distributionResult.ok) {
|
|
4094
|
+
return this._makeError(1, distributionResult.msg);
|
|
4095
|
+
}
|
|
4096
|
+
this._outputJson({
|
|
4097
|
+
items: drawWeightedItems(countResult.value, distributionResult.value)
|
|
4098
|
+
});
|
|
4099
|
+
return this._makeOk();
|
|
4100
|
+
}
|
|
4101
|
+
_outputJson(data) {
|
|
4102
|
+
this._output(JSON.stringify(data, null, 2));
|
|
4103
|
+
}
|
|
4104
|
+
};
|
|
4105
|
+
function parseCount(value) {
|
|
4106
|
+
const count = Number(String(value ?? "").trim());
|
|
4107
|
+
if (!Number.isInteger(count) || count <= 0) {
|
|
4108
|
+
return { ok: false, msg: "\u62BD\u53D6\u4E2A\u6570\u5FC5\u987B\u662F\u6B63\u6574\u6570" };
|
|
4109
|
+
}
|
|
4110
|
+
return { ok: true, value: count };
|
|
4111
|
+
}
|
|
4112
|
+
function stringifyDistributionArg(value) {
|
|
4113
|
+
if (Array.isArray(value)) {
|
|
4114
|
+
return value.map((item) => String(item)).join(" ").trim();
|
|
4115
|
+
}
|
|
4116
|
+
return String(value ?? "").trim();
|
|
4117
|
+
}
|
|
4118
|
+
function parseDistribution(value) {
|
|
4119
|
+
if (value.trimStart().startsWith("[")) {
|
|
4120
|
+
return parseJsonDistribution(value);
|
|
4121
|
+
}
|
|
4122
|
+
return parseShorthandDistribution(value);
|
|
4123
|
+
}
|
|
4124
|
+
function parseJsonDistribution(value) {
|
|
4125
|
+
let parsed;
|
|
4126
|
+
try {
|
|
4127
|
+
parsed = JSON.parse(value);
|
|
4128
|
+
} catch (err) {
|
|
4129
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
4130
|
+
return { ok: false, msg: `JSON \u6982\u7387\u5206\u5E03\u89E3\u6790\u5931\u8D25: ${msg}` };
|
|
4131
|
+
}
|
|
4132
|
+
if (!Array.isArray(parsed)) {
|
|
4133
|
+
return { ok: false, msg: "JSON \u6982\u7387\u5206\u5E03\u5FC5\u987B\u662F\u6570\u7EC4" };
|
|
4134
|
+
}
|
|
4135
|
+
const entries = [];
|
|
4136
|
+
for (let index = 0; index < parsed.length; index += 1) {
|
|
4137
|
+
const entryResult = parseJsonDistributionEntry(parsed[index], index);
|
|
4138
|
+
if (!entryResult.ok) {
|
|
4139
|
+
return entryResult;
|
|
4140
|
+
}
|
|
4141
|
+
entries.push(entryResult.value);
|
|
4142
|
+
}
|
|
4143
|
+
return validateDistribution(entries);
|
|
4144
|
+
}
|
|
4145
|
+
function parseJsonDistributionEntry(value, index) {
|
|
4146
|
+
const record = asRecord2(value);
|
|
4147
|
+
if (!record) {
|
|
4148
|
+
return { ok: false, msg: `\u7B2C ${index + 1} \u9879\u5FC5\u987B\u662F\u5BF9\u8C61` };
|
|
4149
|
+
}
|
|
4150
|
+
const nameResult = parseEntryName(record.name, `\u7B2C ${index + 1} \u9879`);
|
|
4151
|
+
if (!nameResult.ok) {
|
|
4152
|
+
return nameResult;
|
|
4153
|
+
}
|
|
4154
|
+
const weightResult = parseWeight(
|
|
4155
|
+
getJsonWeightValue(record),
|
|
4156
|
+
`\u7B2C ${index + 1} \u9879`
|
|
4157
|
+
);
|
|
4158
|
+
if (!weightResult.ok) {
|
|
4159
|
+
return weightResult;
|
|
4160
|
+
}
|
|
4161
|
+
return {
|
|
4162
|
+
ok: true,
|
|
4163
|
+
value: {
|
|
4164
|
+
name: nameResult.value,
|
|
4165
|
+
weight: weightResult.value
|
|
4166
|
+
}
|
|
4167
|
+
};
|
|
4168
|
+
}
|
|
4169
|
+
function getJsonWeightValue(record) {
|
|
4170
|
+
if ("weight" in record) {
|
|
4171
|
+
return record.weight;
|
|
4172
|
+
}
|
|
4173
|
+
if ("probability" in record) {
|
|
4174
|
+
return record.probability;
|
|
4175
|
+
}
|
|
4176
|
+
if ("prob" in record) {
|
|
4177
|
+
return record.prob;
|
|
4178
|
+
}
|
|
4179
|
+
return record.p;
|
|
4180
|
+
}
|
|
4181
|
+
function parseShorthandDistribution(value) {
|
|
4182
|
+
const parts = value.split(/[,,]/);
|
|
4183
|
+
const entries = [];
|
|
4184
|
+
for (let index = 0; index < parts.length; index += 1) {
|
|
4185
|
+
const part = parts[index].trim();
|
|
4186
|
+
if (!part) {
|
|
4187
|
+
return { ok: false, msg: `\u7B2C ${index + 1} \u9879\u4E3A\u7A7A` };
|
|
4188
|
+
}
|
|
4189
|
+
const separatorIndex = findLastWeightSeparator(part);
|
|
4190
|
+
if (separatorIndex < 0) {
|
|
4191
|
+
return {
|
|
4192
|
+
ok: false,
|
|
4193
|
+
msg: `\u7B2C ${index + 1} \u9879\u7F3A\u5C11\u6982\u7387\u5206\u9694\u7B26 ":"`
|
|
4194
|
+
};
|
|
4195
|
+
}
|
|
4196
|
+
const nameResult = parseEntryName(
|
|
4197
|
+
part.slice(0, separatorIndex),
|
|
4198
|
+
`\u7B2C ${index + 1} \u9879`
|
|
4199
|
+
);
|
|
4200
|
+
if (!nameResult.ok) {
|
|
4201
|
+
return nameResult;
|
|
4202
|
+
}
|
|
4203
|
+
const weightResult = parseWeight(
|
|
4204
|
+
part.slice(separatorIndex + 1),
|
|
4205
|
+
`\u7B2C ${index + 1} \u9879`
|
|
4206
|
+
);
|
|
4207
|
+
if (!weightResult.ok) {
|
|
4208
|
+
return weightResult;
|
|
4209
|
+
}
|
|
4210
|
+
entries.push({
|
|
4211
|
+
name: nameResult.value,
|
|
4212
|
+
weight: weightResult.value
|
|
4213
|
+
});
|
|
4214
|
+
}
|
|
4215
|
+
return validateDistribution(entries);
|
|
4216
|
+
}
|
|
4217
|
+
function findLastWeightSeparator(value) {
|
|
4218
|
+
return Math.max(value.lastIndexOf(":"), value.lastIndexOf("\uFF1A"));
|
|
4219
|
+
}
|
|
4220
|
+
function parseEntryName(value, label) {
|
|
4221
|
+
if (typeof value !== "string") {
|
|
4222
|
+
return { ok: false, msg: `${label}\u7684\u540D\u79F0\u5FC5\u987B\u662F\u5B57\u7B26\u4E32` };
|
|
4223
|
+
}
|
|
4224
|
+
const name = value.trim();
|
|
4225
|
+
if (!name) {
|
|
4226
|
+
return { ok: false, msg: `${label}\u7684\u540D\u79F0\u4E0D\u80FD\u4E3A\u7A7A` };
|
|
4227
|
+
}
|
|
4228
|
+
return { ok: true, value: name };
|
|
4229
|
+
}
|
|
4230
|
+
function parseWeight(value, label) {
|
|
4231
|
+
const rawValue = typeof value === "string" ? value.trim().replace(/[%%]$/, "") : value;
|
|
4232
|
+
const weight = Number(rawValue);
|
|
4233
|
+
if (!Number.isFinite(weight) || weight <= 0) {
|
|
4234
|
+
return { ok: false, msg: `${label}\u7684\u6982\u7387\u5FC5\u987B\u662F\u5927\u4E8E 0 \u7684\u6570\u5B57` };
|
|
4235
|
+
}
|
|
4236
|
+
return { ok: true, value: weight };
|
|
4237
|
+
}
|
|
4238
|
+
function validateDistribution(entries) {
|
|
4239
|
+
if (entries.length === 0) {
|
|
4240
|
+
return { ok: false, msg: "\u6982\u7387\u5206\u5E03\u81F3\u5C11\u9700\u8981 1 \u9879" };
|
|
4241
|
+
}
|
|
4242
|
+
return { ok: true, value: entries };
|
|
4243
|
+
}
|
|
4244
|
+
function drawWeightedItems(count, entries) {
|
|
4245
|
+
const totalWeight = entries.reduce((sum, entry) => sum + entry.weight, 0);
|
|
4246
|
+
const items = [];
|
|
4247
|
+
for (let index = 0; index < count; index += 1) {
|
|
4248
|
+
items.push(drawWeightedItem(entries, totalWeight));
|
|
4249
|
+
}
|
|
4250
|
+
return items;
|
|
4251
|
+
}
|
|
4252
|
+
function drawWeightedItem(entries, totalWeight) {
|
|
4253
|
+
let cursor = Math.random() * totalWeight;
|
|
4254
|
+
for (const entry of entries) {
|
|
4255
|
+
cursor -= entry.weight;
|
|
4256
|
+
if (cursor < 0) {
|
|
4257
|
+
return entry.name;
|
|
4258
|
+
}
|
|
4259
|
+
}
|
|
4260
|
+
return entries[entries.length - 1].name;
|
|
4261
|
+
}
|
|
4262
|
+
function asRecord2(value) {
|
|
4263
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
4264
|
+
return void 0;
|
|
4265
|
+
}
|
|
4266
|
+
return value;
|
|
4267
|
+
}
|
|
4268
|
+
|
|
4099
4269
|
// src/commands/tool/tool-command.ts
|
|
4100
4270
|
var ToolCommand = class extends BaseSubcommandHost {
|
|
4101
4271
|
_meta() {
|
|
@@ -4106,6 +4276,7 @@ var ToolCommand = class extends BaseSubcommandHost {
|
|
|
4106
4276
|
}
|
|
4107
4277
|
_registerSubcommands() {
|
|
4108
4278
|
this._addSubcommand(new ToolCanvasCommand());
|
|
4279
|
+
this._addSubcommand(new ToolDrawCommand());
|
|
4109
4280
|
}
|
|
4110
4281
|
};
|
|
4111
4282
|
|