An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:sparkles: Support cross-type alternatives for deprecated bazaar packages https://github.com/siyuan-note/bazaar/issues/2347
Jeffrey Chenbf7ccefa7513
diff --git a/app/src/assets/scss/business/_config.scss b/app/src/assets/scss/business/_config.scssindex 20f13f0e49..e1d301ca9c 100644--- a/app/src/assets/scss/business/_config.scss+++ b/app/src/assets/scss/business/_config.scss@@ -1136,6 +1136,11 @@line-height: 20px;}+ &-type {+ color: var(--b3-theme-on-surface);+ margin-left: 4px;+ }+&-list {display: flex;flex-wrap: wrap;diff --git a/app/src/config/bazaar.ts b/app/src/config/bazaar.tsindex b0e06a69f5..6ef6132c2d 100644--- a/app/src/config/bazaar.ts+++ b/app/src/config/bazaar.ts@@ -22,6 +22,7 @@ import {getBazaarBackendSystemLabels,getBazaarCompatibilityData,getBazaarCompatibilityFieldVisibility,+ getBazaarDeprecationChoices,getBazaarDeprecationData,getBazaarFrontendLabels,getBazaarFundingItems,@@ -429,15 +430,19 @@ export const bazaar = {return "";}const reason = item.preferredDeprecatedReason || window.siyuan.languages.bazaarDeprecatedTip;- const alternatives = Array.isArray(item.alternatives) ? item.alternatives.filter((name) =>- typeof name === "string" && name.length > 0) : [];- const alternativesHTML = alternatives.map((name) => `<button type="button" data-type="bazaar-alternative" data-package-type="${bazaarType}" data-package-name="${escapeAttr(name)}" class="b3-chip b3-chip--small b3-chip--pointer config-bazaar__deprecated-button">${escapeHtml(name)}</button>`).join("");+ const choices = getBazaarDeprecationChoices(item);+ // 跨类型替代包需标明目标类型,同类型在同类标签页中不必重复+ const choicesHTML = choices.map((choice) => {+ const typeLabel = choice.packageType === bazaarType ? "" :+ `<span class="config-bazaar__deprecated-type">${escapeHtml(window.siyuan.languages[BAZAAR_PACKAGE_CONFIG[choice.packageType].languageKey])}</span>`;+ return `<button type="button" data-type="bazaar-alternative" data-package-type="${choice.packageType}" data-package-name="${escapeAttr(choice.name)}" class="b3-chip b3-chip--small b3-chip--pointer config-bazaar__deprecated-button">${escapeHtml(choice.name)}${typeLabel}</button>`;+ }).join("");return `<section class="config-bazaar__deprecated"><div class="config-bazaar__deprecated-title">${escapeHtml(window.siyuan.languages.bazaarDeprecated)}</div><div class="config-bazaar__deprecated-reason">${escapeHtml(reason)}</div>- ${alternativesHTML ? `<div class="config-bazaar__deprecated-alternatives">+ ${choicesHTML ? `<div class="config-bazaar__deprecated-alternatives"><span class="config-bazaar__deprecated-label">${window.siyuan.languages.bazaarAlternatives}</span>- <div class="config-bazaar__deprecated-list">${alternativesHTML}</div>+ <div class="config-bazaar__deprecated-list">${choicesHTML}</div></div>` : ""}</section>`;},diff --git a/app/src/types/api/index.d.ts b/app/src/types/api/index.d.tsindex f4e2c93372..079c265ef3 100644--- a/app/src/types/api/index.d.ts+++ b/app/src/types/api/index.d.ts@@ -698,7 +698,9 @@ export type BazaarLocalInstallResult = (BazaarLocalInstallData & { "reason"?: neexport type BazaarNotifications = { "browserCompatibility": boolean; "docTreeMaxList": boolean; "formatPainterTip"?: boolean; "selectAllIncompleteTip"?: boolean; "selectAllTip"?: boolean; "tagMaxList": boolean; "workspaceNotSSD": boolean; };-export type BazaarPackage = { "alternatives"?: Array<string>; "author": string; "backends": Array<string> | null; "bazaarIncompatible"?: boolean; "bootAppearances"?: Array<string>; "current": boolean; "deprecated"?: boolean; "deprecatedReason"?: Record<string, string>; "description": Record<string, string> | null; "disabledInPublish": boolean; "disallowInstall": boolean; "disallowUpdate": boolean; "displayName": Record<string, string> | null; "downloads": number; "enabled"?: boolean; "frontends": Array<string> | null; "funding": BazaarFunding | null; "hInstallDate": string; "hInstallSize": string; "hSize": string; "hUpdated": string; "hasStorageData"?: boolean; "icon"?: string; "iconURL": string; "installSize": number; "installTime": number; "installed": boolean; "installedIncompatible"?: boolean; "invalidReason"?: "missing-manifest" | "invalid-manifest" | "name-mismatch"; "kernels": Array<string> | null; "keywords": Array<string> | null; "minAppVersion": string; "modes"?: Array<string> | null; "name": string; "openIssues": number; "outdated": boolean; "preferredDeprecatedReason"?: string; "preferredDesc": string; "preferredFunding": string; "preferredName": string; "preferredReadme": string; "preview"?: string; "previewURL": string; "rating"?: BazaarPackageRating; "ratingAvailable": boolean; "readme": Record<string, string> | null; "repoHash": string; "repoRef"?: string; "repoURL": string; "settingsWindow"?: boolean; "size": number; "stars": number; "updateRequiredMinAppVer"?: string; "updateTime": number; "updated": string; "url": string; "userDisabledInPublish"?: boolean; "version": string; };+export type BazaarPackage = { "author": string; "backends": Array<string> | null; "bazaarIncompatible"?: boolean; "bootAppearances"?: Array<string>; "choices"?: Array<BazaarPackageChoice>; "current": boolean; "deprecated"?: boolean; "deprecatedReason"?: Record<string, string>; "description": Record<string, string> | null; "disabledInPublish": boolean; "disallowInstall": boolean; "disallowUpdate": boolean; "displayName": Record<string, string> | null; "downloads": number; "enabled"?: boolean; "frontends": Array<string> | null; "funding": BazaarFunding | null; "hInstallDate": string; "hInstallSize": string; "hSize": string; "hUpdated": string; "hasStorageData"?: boolean; "icon"?: string; "iconURL": string; "installSize": number; "installTime": number; "installed": boolean; "installedIncompatible"?: boolean; "invalidReason"?: "missing-manifest" | "invalid-manifest" | "name-mismatch"; "kernels": Array<string> | null; "keywords": Array<string> | null; "minAppVersion": string; "modes"?: Array<string> | null; "name": string; "openIssues": number; "outdated": boolean; "preferredDeprecatedReason"?: string; "preferredDesc": string; "preferredFunding": string; "preferredName": string; "preferredReadme": string; "preview"?: string; "previewURL": string; "rating"?: BazaarPackageRating; "ratingAvailable": boolean; "readme": Record<string, string> | null; "repoHash": string; "repoRef"?: string; "repoURL": string; "settingsWindow"?: boolean; "size": number; "stars": number; "updateRequiredMinAppVer"?: string; "updateTime": number; "updated": string; "url": string; "userDisabledInPublish"?: boolean; "version": string; };++export type BazaarPackageChoice = { "name": string; "packageType": string; };export type BazaarPackageDetail = { "available": BazaarPackage | null; "installed": BazaarPackage | null; };diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.tsindex be0f1b9763..dfee3f19a2 100644--- a/app/src/types/index.d.ts+++ b/app/src/types/index.d.ts@@ -1059,6 +1059,11 @@ interface IBazaarRating {distribution: TBazaarRatingDistribution;}+interface IBazaarPackageChoice {+ packageType: string;+ name: string;+}+interface IBazaarItem {preferredName: string;minAppVersion: string;@@ -1073,7 +1078,7 @@ interface IBazaarItem {deprecated?: boolean;deprecatedReason?: Record<string, string>;preferredDeprecatedReason?: string;- alternatives?: string[];+ choices?: IBazaarPackageChoice[];iconURL: string;stars: number;author: string;diff --git a/app/src/util/bazaarPackage.test.ts b/app/src/util/bazaarPackage.test.tsindex 46ad763725..3920559709 100644--- a/app/src/util/bazaarPackage.test.ts+++ b/app/src/util/bazaarPackage.test.ts@@ -8,6 +8,7 @@ import {getBazaarBackendSystemLabels,getBazaarCompatibilityData,getBazaarCompatibilityFieldVisibility,+ getBazaarDeprecationChoices,getBazaarDeprecationData,getBazaarFrontendLabels,getBazaarFundingItems,@@ -70,22 +71,23 @@ describe("bazaar package deprecation metadata", () => {deprecated?: boolean;deprecatedReason?: Record<string, string>;preferredDeprecatedReason?: string;- alternatives?: string[];+ choices?: IBazaarPackageChoice[];} = {};const deprecatedReason = {default: "No longer maintained"};- const alternatives = ["replacement"];+ const choices = [{packageType: "widgets", name: "replacement"}, {packageType: "plugins", name: "plugin"}];applyBazaarPackageDeprecation(installed, {deprecated: true,deprecatedReason,preferredDeprecatedReason: "No longer maintained",- alternatives,+ choices,});assert.equal(installed.deprecated, true);assert.deepEqual(installed.deprecatedReason, deprecatedReason);assert.notEqual(installed.deprecatedReason, deprecatedReason);assert.equal(installed.preferredDeprecatedReason, "No longer maintained");- assert.deepEqual(installed.alternatives, alternatives);- assert.notEqual(installed.alternatives, alternatives);+ assert.deepEqual(installed.choices, choices);+ assert.notEqual(installed.choices, choices);+ assert.notEqual(installed.choices?.[0], choices[0]);});it("clears stale fields when the online package is active or missing", () => {@@ -93,13 +95,36 @@ describe("bazaar package deprecation metadata", () => {deprecated: true as boolean | undefined,deprecatedReason: {default: "Stale"} as Record<string, string> | undefined,preferredDeprecatedReason: "Stale" as string | undefined,- alternatives: ["stale"] as string[] | undefined,+ choices: [{packageType: "widgets", name: "stale"}] as IBazaarPackageChoice[] | undefined,};applyBazaarPackageDeprecation(installed, {deprecated: false});assert.equal(installed.deprecated, undefined);assert.equal(installed.deprecatedReason, undefined);assert.equal(installed.preferredDeprecatedReason, undefined);- assert.equal(installed.alternatives, undefined);+ assert.equal(installed.choices, undefined);+ });+});++describe("getBazaarDeprecationChoices", () => {+ it("keeps the package type of every choice so cross-type targets stay reachable", () => {+ assert.deepEqual(getBazaarDeprecationChoices({+ choices: [{packageType: "plugins", name: "plugin"}, {packageType: "widgets", name: "widget"}],+ }), [{packageType: "plugins", name: "plugin"}, {packageType: "widgets", name: "widget"}]);+ });++ it("drops choices with an unknown package type or an invalid package name", () => {+ assert.deepEqual(getBazaarDeprecationChoices({+ choices: [+ {packageType: "plugins", name: "plugin"},+ {packageType: "unknown", name: "plugin"},+ {packageType: "themes", name: ""},+ ],+ }), [{packageType: "plugins", name: "plugin"}]);+ });++ it("returns no alternative when the index has no choices", () => {+ assert.deepEqual(getBazaarDeprecationChoices({choices: []}), []);+ assert.deepEqual(getBazaarDeprecationChoices({}), []);});});diff --git a/app/src/util/bazaarPackage.ts b/app/src/util/bazaarPackage.tsindex aa74de01fa..9d76bce6fb 100644--- a/app/src/util/bazaarPackage.ts+++ b/app/src/util/bazaarPackage.ts@@ -1,3 +1,5 @@+import {isBazaarPackageType} from "../config/bazaar/packageConfig";+const reservedWindowsDeviceNames = new Set(["CON", "PRN", "AUX", "NUL","COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",@@ -15,7 +17,13 @@ type TBazaarDeprecationFields = {deprecated?: boolean;deprecatedReason?: Record<string, string>;preferredDeprecatedReason?: string;- alternatives?: string[];+ choices?: IBazaarPackageChoice[];+};++// 已校验包类型的替代包,可直接用于打开对应类型的集市标签页+export type TBazaarDeprecationChoice = {+ packageType: TBazaarType;+ name: string;};export const getBazaarDeprecationData = <T extends object>(@@ -32,15 +40,28 @@ export const applyBazaarPackageDeprecation = <T extends TBazaarDeprecationFieldsdelete installed.deprecated;delete installed.deprecatedReason;delete installed.preferredDeprecatedReason;- delete installed.alternatives;+ delete installed.choices;return;}installed.deprecated = true;installed.deprecatedReason = available.deprecatedReason ? {...available.deprecatedReason} : {};installed.preferredDeprecatedReason = typeof available.preferredDeprecatedReason === "string" ?available.preferredDeprecatedReason : "";- installed.alternatives = Array.isArray(available.alternatives) ?- available.alternatives.filter((item): item is string => typeof item === "string") : [];+ installed.choices = Array.isArray(available.choices) ?+ available.choices.filter((item) => item && typeof item.name === "string").map((item) => ({+ packageType: item.packageType, name: item.name,+ })) : [];+};++// getBazaarDeprecationChoices 取弃用包的替代包,携带包类型,可跨类型跳转+export const getBazaarDeprecationChoices = (item: {choices?: IBazaarPackageChoice[]}): TBazaarDeprecationChoice[] => {+ const choices: TBazaarDeprecationChoice[] = [];+ (Array.isArray(item.choices) ? item.choices : []).forEach((choice) => {+ if (choice && isBazaarPackageType(choice.packageType) && typeof choice.name === "string" && choice.name.length > 0) {+ choices.push({packageType: choice.packageType, name: choice.name});+ }+ });+ return choices;};export const getBazaarCompatibilityFieldVisibility = (packageType: string) => {diff --git a/kernel/api/bazaar.go b/kernel/api/bazaar.goindex ba25e39765..738164a9e8 100644--- a/kernel/api/bazaar.go+++ b/kernel/api/bazaar.go@@ -519,7 +519,12 @@ func bazaarPackage(value *bazaar.Package) *apicontract.BazaarPackage {ret.Keywords = value.Keywordsret.Deprecated = value.Deprecatedret.DeprecatedReason = apicontract.BazaarLocaleStrings(value.DeprecatedReason)- ret.Alternatives = value.Alternatives+ if value.Choices != nil {+ ret.Choices = make([]apicontract.BazaarPackageChoice, len(value.Choices))+ for i, choice := range value.Choices {+ ret.Choices[i] = apicontract.BazaarPackageChoice{PackageType: choice.PackageType, Name: choice.Name}+ }+ }ret.PreferredFunding = value.PreferredFundingret.PreferredName = value.PreferredNameret.PreferredDesc = value.PreferredDescdiff --git a/kernel/apicontract/bazaar.go b/kernel/apicontract/bazaar.goindex b99541535d..8f40234517 100644--- a/kernel/apicontract/bazaar.go+++ b/kernel/apicontract/bazaar.go@@ -226,27 +226,31 @@ type BazaarPackageRating struct {Count int64 `json:"count"`Distribution [5]int64 `json:"distribution"`}+type BazaarPackageChoice struct {+ PackageType string `json:"packageType"`+ Name string `json:"name"`+}type BazaarPackage struct {- Author string `json:"author"`- URL string `json:"url"`- Version string `json:"version"`- MinAppVersion string `json:"minAppVersion"`- DisabledInPublish bool `json:"disabledInPublish"`- Kernels []string `json:"kernels"`- Backends []string `json:"backends"`- Frontends []string `json:"frontends"`- SettingsWindow bool `json:"settingsWindow,omitempty"` // 声明支持在独立设置窗口运行- BootAppearances []string `json:"bootAppearances,omitempty"`- DisplayName BazaarLocaleStrings `json:"displayName"`- Description BazaarLocaleStrings `json:"description"`- Readme BazaarLocaleStrings `json:"readme"`- Icon *string `json:"icon,omitempty"`- Preview *string `json:"preview,omitempty"`- Funding *BazaarFunding `json:"funding"`- Keywords []string `json:"keywords"`- Deprecated bool `json:"deprecated,omitempty"`- DeprecatedReason BazaarLocaleStrings `json:"deprecatedReason,omitempty"`- Alternatives []string `json:"alternatives,omitempty"`+ Author string `json:"author"`+ URL string `json:"url"`+ Version string `json:"version"`+ MinAppVersion string `json:"minAppVersion"`+ DisabledInPublish bool `json:"disabledInPublish"`+ Kernels []string `json:"kernels"`+ Backends []string `json:"backends"`+ Frontends []string `json:"frontends"`+ SettingsWindow bool `json:"settingsWindow,omitempty"` // 声明支持在独立设置窗口运行+ BootAppearances []string `json:"bootAppearances,omitempty"`+ DisplayName BazaarLocaleStrings `json:"displayName"`+ Description BazaarLocaleStrings `json:"description"`+ Readme BazaarLocaleStrings `json:"readme"`+ Icon *string `json:"icon,omitempty"`+ Preview *string `json:"preview,omitempty"`+ Funding *BazaarFunding `json:"funding"`+ Keywords []string `json:"keywords"`+ Deprecated bool `json:"deprecated,omitempty"`+ DeprecatedReason BazaarLocaleStrings `json:"deprecatedReason,omitempty"`+ Choices []BazaarPackageChoice `json:"choices,omitempty"` // 替代包及其类型,可跨类型PreferredFunding string `json:"preferredFunding"`PreferredName string `json:"preferredName"`diff --git a/kernel/apicontract/schema.json b/kernel/apicontract/schema.jsonindex 8049bb7661..617fe91949 100644--- a/kernel/apicontract/schema.json+++ b/kernel/apicontract/schema.json@@ -16921,12 +16921,6 @@"BazaarPackage": {"type": "object","properties": {- "alternatives": {- "type": "array",- "items": {- "type": "string"- }- },"author": {"type": "string"},@@ -16952,6 +16946,12 @@"type": "string"}},+ "choices": {+ "type": "array",+ "items": {+ "$ref": "#/$defs/BazaarPackageChoice"+ }+ },"current": {"type": "boolean"},@@ -17241,6 +17241,22 @@],"additionalProperties": false},+ "BazaarPackageChoice": {+ "type": "object",+ "properties": {+ "name": {+ "type": "string"+ },+ "packageType": {+ "type": "string"+ }+ },+ "required": [+ "name",+ "packageType"+ ],+ "additionalProperties": false+ },"BazaarPackageDetail": {"type": "object","properties": {diff --git a/kernel/bazaar/local.go b/kernel/bazaar/local.goindex d1ffa86832..508539e5e6 100644--- a/kernel/bazaar/local.go+++ b/kernel/bazaar/local.go@@ -48,6 +48,16 @@ var localPackageManifests = map[string]string{"widget.json": "widgets",}+// IsValidPackageType 判断 pkgType 是否为集市支持的包类型(复数形式),以本地包清单映射为准。+func IsValidPackageType(pkgType string) bool {+ for _, t := range localPackageManifests {+ if t == pkgType {+ return true+ }+ }+ return false+}+// ExtractLocalPackage 将本地集市包解压到临时目录并识别包类型。func ExtractLocalPackage(archivePath string) (pkgType string, pkg *Package, packagePath string, cleanup func(), err error) {pkgType, pkg, packagePath, _, cleanup, err = extractLocalPackageWithHash(context.Background(), archivePath, "", false)diff --git a/kernel/bazaar/package.go b/kernel/bazaar/package.goindex c8e7323a1f..c1b9f3e017 100644--- a/kernel/bazaar/package.go+++ b/kernel/bazaar/package.go@@ -59,30 +59,36 @@ func clearBazaarPackageRating(pkg *Package) {pkg.Rating = nil}+// PackageChoice 描述弃用包的一个替代包及其类型,由在线集市索引生成,供客户端跨类型跳转。+type PackageChoice struct {+ PackageType string `json:"packageType"`+ Name string `json:"name"`+}+// Package 描述了集市包元数据和传递给前端的其他信息。// - 集市包新增元数据字段需要同步修改 bazaar 的工作流,参考// https://github.com/siyuan-note/bazaar/commit/aa36d0003139c52d8e767c6e18a635be006323e2type Package struct {- Author string `json:"author"`- URL string `json:"url"`- Version string `json:"version"`- MinAppVersion string `json:"minAppVersion"`- DisabledInPublish bool `json:"disabledInPublish"`- Kernels []string `json:"kernels"`- Backends []string `json:"backends"`- Frontends []string `json:"frontends"`- SettingsWindow bool `json:"settingsWindow,omitempty"` // 声明支持在独立设置窗口运行- BootAppearances []string `json:"bootAppearances,omitempty"`- DisplayName LocaleStrings `json:"displayName"`- Description LocaleStrings `json:"description"`- Readme LocaleStrings `json:"readme"`- Icon *string `json:"icon,omitempty"`- Preview *string `json:"preview,omitempty"`- Funding *Funding `json:"funding"`- Keywords []string `json:"keywords"`- Deprecated bool `json:"deprecated,omitempty"`- DeprecatedReason LocaleStrings `json:"deprecatedReason,omitempty"`- Alternatives []string `json:"alternatives,omitempty"`+ Author string `json:"author"`+ URL string `json:"url"`+ Version string `json:"version"`+ MinAppVersion string `json:"minAppVersion"`+ DisabledInPublish bool `json:"disabledInPublish"`+ Kernels []string `json:"kernels"`+ Backends []string `json:"backends"`+ Frontends []string `json:"frontends"`+ SettingsWindow bool `json:"settingsWindow,omitempty"` // 声明支持在独立设置窗口运行+ BootAppearances []string `json:"bootAppearances,omitempty"`+ DisplayName LocaleStrings `json:"displayName"`+ Description LocaleStrings `json:"description"`+ Readme LocaleStrings `json:"readme"`+ Icon *string `json:"icon,omitempty"`+ Preview *string `json:"preview,omitempty"`+ Funding *Funding `json:"funding"`+ Keywords []string `json:"keywords"`+ Deprecated bool `json:"deprecated,omitempty"`+ DeprecatedReason LocaleStrings `json:"deprecatedReason,omitempty"`+ Choices []PackageChoice `json:"choices,omitempty"` // 替代包及其类型,可跨类型PreferredFunding string `json:"preferredFunding"`PreferredName string `json:"preferredName"`@@ -211,7 +217,7 @@ func clearPackageDeprecationMetadata(pkg *Package) {}pkg.Deprecated = falsepkg.DeprecatedReason = nil- pkg.Alternatives = nil+ pkg.Choices = nilpkg.PreferredDeprecatedReason = ""}@@ -256,20 +262,22 @@ func setPreferredPackageDeprecationMetadata(pkg *Package) {return}pkg.PreferredDeprecatedReason = GetPreferredLocaleString(pkg.DeprecatedReason, "")- alternatives := make([]string, 0, len(pkg.Alternatives))- seen := make(map[string]struct{}, len(pkg.Alternatives))- for _, alternative := range pkg.Alternatives {- identity := strings.ToLower(alternative)- if !IsValidPackageName(alternative) || strings.EqualFold(alternative, pkg.Name) {++ // 替代包可以跨类型,故按「包类型 + 包名」判重与校验+ choices := make([]PackageChoice, 0, len(pkg.Choices))+ seen := make(map[string]struct{}, len(pkg.Choices))+ for _, choice := range pkg.Choices {+ if !IsValidPackageType(choice.PackageType) || !IsValidPackageName(choice.Name) {continue}+ identity := choice.PackageType + "/" + strings.ToLower(choice.Name)if _, duplicate := seen[identity]; duplicate {continue}seen[identity] = struct{}{}- alternatives = append(alternatives, alternative)+ choices = append(choices, choice)}- pkg.Alternatives = alternatives+ pkg.Choices = choices}// GetPreferredLocaleString 从 LocaleStrings 中按当前语种取值,无则回退 default、en、en_US(历史命名兼容),再回退 fallback。diff --git a/kernel/bazaar/package_test.go b/kernel/bazaar/package_test.goindex 9aa47358dd..50c4fa6aac 100644--- a/kernel/bazaar/package_test.go+++ b/kernel/bazaar/package_test.go@@ -20,6 +20,7 @@ import ("encoding/json""os""path/filepath"+ "reflect""strings""testing"@@ -208,19 +209,35 @@ func TestSetPreferredPackageDeprecationMetadata(t *testing.T) {"default": "No longer maintained","zh-CN": "已停止维护",},- Alternatives: []string{"new-package", "new-package", "old-package", "插件"},+ Choices: []PackageChoice{+ {PackageType: "widgets", Name: "new-package"},+ {PackageType: "widgets", Name: "new-package"},+ {PackageType: "unknown", Name: "new-package"},+ {PackageType: "plugins", Name: "插件"},+ {PackageType: "plugins", Name: "new-package"},+ {PackageType: "themes", Name: "invalid/name"},+ {PackageType: "themes", Name: ""},+ },}setPreferredPackageDeprecationMetadata(pkg)if pkg.PreferredDeprecatedReason != "已停止维护" {t.Fatalf("unexpected preferred deprecated reason %q", pkg.PreferredDeprecatedReason)}- if len(pkg.Alternatives) != 2 || pkg.Alternatives[0] != "new-package" || pkg.Alternatives[1] != "插件" {- t.Fatalf("unexpected alternatives %#v", pkg.Alternatives)+ // 替代包可跨类型:按「包类型 + 包名」判重,同名但类型不同不得合并;+ // 包类型或包名不合法、重复的条目须被丢弃+ wantChoices := []PackageChoice{+ {PackageType: "widgets", Name: "new-package"},+ {PackageType: "plugins", Name: "插件"},+ {PackageType: "plugins", Name: "new-package"},+ }+ if !reflect.DeepEqual(pkg.Choices, wantChoices) {+ t.Fatalf("unexpected choices %#v", pkg.Choices)}pkg.Deprecated = falsesetPreferredPackageDeprecationMetadata(pkg)- if pkg.Deprecated || pkg.DeprecatedReason != nil || pkg.Alternatives != nil || pkg.PreferredDeprecatedReason != "" {+ if pkg.Deprecated || pkg.DeprecatedReason != nil || pkg.Choices != nil ||+ pkg.PreferredDeprecatedReason != "" {t.Fatalf("active package retained deprecation metadata: %#v", pkg)}}@@ -236,14 +253,18 @@ func TestBuildBazaarPackageWithDeprecationMetadata(t *testing.T) {Name: "old-package",Deprecated: true,DeprecatedReason: LocaleStrings{"default": "No longer maintained"},- Alternatives: []string{"new-package"},+ Choices: []PackageChoice{+ {PackageType: "widgets", Name: "new-package"},+ {PackageType: "plugins", Name: "new-plugin"},+ },},}, map[string]*bazaarStats{}, map[string]*PackageRating{}, false, "widgets", "")if pkg == nil || !pkg.Deprecated || pkg.PreferredDeprecatedReason != "No longer maintained" {t.Fatalf("unexpected built package %#v", pkg)}- if len(pkg.Alternatives) != 1 || pkg.Alternatives[0] != "new-package" {- t.Fatalf("unexpected built alternatives %#v", pkg.Alternatives)+ wantChoices := []PackageChoice{{PackageType: "widgets", Name: "new-package"}, {PackageType: "plugins", Name: "new-plugin"}}+ if !reflect.DeepEqual(pkg.Choices, wantChoices) {+ t.Fatalf("unexpected built choices %#v", pkg.Choices)}}@@ -257,7 +278,8 @@ func TestUnescapePackageDeprecatedReason(t *testing.T) {func TestParsePackageJSONClearsGeneratedDeprecationMetadata(t *testing.T) {filePath := filepath.Join(t.TempDir(), "plugin.json")- data := []byte(`{"name":"local-package","deprecated":true,"deprecatedReason":{"default":"Untrusted"},"alternatives":["other-package"]}`)+ // 旧索引残留的 alternatives 与本地清单自行声明的弃用元数据都不得保留+ data := []byte(`{"name":"local-package","deprecated":true,"deprecatedReason":{"default":"Untrusted"},"alternatives":["other-package"],"choices":[{"packageType":"plugins","name":"other-package"}]}`)if err := os.WriteFile(filePath, data, 0644); err != nil {t.Fatal(err)}@@ -265,7 +287,8 @@ func TestParsePackageJSONClearsGeneratedDeprecationMetadata(t *testing.T) {if err != nil {t.Fatal(err)}- if pkg.Deprecated || pkg.DeprecatedReason != nil || pkg.Alternatives != nil || pkg.PreferredDeprecatedReason != "" {+ if pkg.Deprecated || pkg.DeprecatedReason != nil || pkg.Choices != nil ||+ pkg.PreferredDeprecatedReason != "" {t.Fatalf("local manifest supplied generated deprecation metadata: %#v", pkg)}}