fix(runtime): accept localized Command Pack descriptions

This commit is contained in:
若麒
2026-08-13 17:54:35 +08:00
parent a770cbe787
commit 29ce8990b9
3 changed files with 21 additions and 2 deletions
+4 -1
View File
@@ -1,5 +1,8 @@
const ping = {
description: "Ping the Command Pack fixture",
description: {
"en-US": "Ping the Command Pack fixture",
"zh-CN": "调用 Command Pack 测试命令",
},
auth: "none",
flags: {
message: {
+13 -1
View File
@@ -5,6 +5,7 @@ import {
BailianError,
COMMAND_PACK_API_VERSION,
ExitCode,
SUPPORTED_LANGUAGES,
UsageError,
formatOutput,
type AnyCommand,
@@ -12,6 +13,7 @@ import {
type CommandPackCommand,
type CommandPackMeta,
type Identity,
type LocalizedText,
} from "bailian-cli-core";
import { CommandRegistry } from "../registry.ts";
import { compareVersion } from "../utils/update-checker.ts";
@@ -67,12 +69,22 @@ function assertCommandPath(path: string, definition: CommandPackDefinition): voi
}
}
function isLocalizedText(value: unknown): value is LocalizedText {
if (typeof value === "string") return value.length > 0;
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
const localizedText = value as Record<string, unknown>;
return SUPPORTED_LANGUAGES.every(
(language) => typeof localizedText[language] === "string" && localizedText[language].length > 0,
);
}
function assertCommand(path: string, value: unknown): asserts value is CommandPackCommand<any> {
if (!value || typeof value !== "object") {
throw new Error(`Command "${path}" must export an object.`);
}
const command = value as Partial<CommandPackCommand<any>>;
if (!command.description || typeof command.description !== "string") {
if (!isLocalizedText(command.description)) {
throw new Error(`Command "${path}" is missing a description.`);
}
if (!command.auth || !AUTH_REQUIREMENTS.has(command.auth)) {
@@ -147,6 +147,10 @@ test("loads an API 1 Command Pack and preserves its command contract", async ()
]);
expect(commands["agent credential"]?.auth).toBe("apiKey");
expect(commands["agent ping"]?.auth).toBe("none");
expect(commands["agent ping"]?.description).toEqual({
"en-US": "Ping the Command Pack fixture",
"zh-CN": "调用 Command Pack 测试命令",
});
expect(commands["agent ping"]?.flags?.message).toMatchObject({ required: true, type: "string" });
});