From 588d0c2308a4cc9d2ed58b1c62b6b76813ebceab Mon Sep 17 00:00:00 2001 From: hanyewei <139319228@qq.com> Date: Wed, 2 Dec 2026 23:38:14 +0800 Subject: [PATCH 1/2] Allow plugin settings to populate startup environment --- internal/plugin/manager.go | 1 + internal/plugin/registry.go | 3 +- internal/plugin/rule_settings_test.go | 34 +++++++++++++++++ internal/plugin/rules.go | 54 ++++++++++++++++++++++----- internal/plugin/types.go | 2 +- 5 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 internal/plugin/rule_settings_test.go diff --git a/internal/plugin/manager.go b/internal/plugin/manager.go index 87ed7fe..8c63073 100644 --- a/internal/plugin/manager.go +++ b/internal/plugin/manager.go @@ -96,6 +96,7 @@ func NewManager(opts ManagerOptions) (*Manager, error) { supervisors: make(map[string]*SidecarSupervisor), l10nCache: make(map[string]map[string]string), } + reg.ruleSettings = m.GetSettings reg.OnChange(func() { m.mu.Lock() clear(m.l10nCache) diff --git a/internal/plugin/registry.go b/internal/plugin/registry.go index d3e9b2c..d85d3de 100644 --- a/internal/plugin/registry.go +++ b/internal/plugin/registry.go @@ -98,6 +98,7 @@ type Registry struct { onChange []func() logger func(source, line string) localeFn func() i18n.Locale + ruleSettings func(pluginID, vmName string, maskSecrets bool) map[string]any // Extension point tables. themes map[string]ThemeContribution @@ -612,7 +613,7 @@ func (r *Registry) registerManifestContributions(sc *Scope, m Manifest) { // Register L1 declarative rule hooks on the hook bus. if len(m.Contributes.RuleHooks) > 0 { rules := append([]RuleHook(nil), m.Contributes.RuleHooks...) - r.registerRuleHooks(sc, pid, rules) + r.registerRuleHooks(sc, pid, rules, m.Settings) } // Register declarative services declared in external Manifest.Provides. for _, pr := range m.Provides { diff --git a/internal/plugin/rule_settings_test.go b/internal/plugin/rule_settings_test.go new file mode 100644 index 0000000..4a34062 --- /dev/null +++ b/internal/plugin/rule_settings_test.go @@ -0,0 +1,34 @@ +package plugin + +import ( + "reflect" + "testing" +) + +func TestRuleEnvironmentUsesVisiblePluginSettings(t *testing.T) { + reg := NewRegistry(nil) + reg.ruleSettings = func(pluginID, vmName string, maskSecrets bool) map[string]any { + if pluginID != "local.capture-shortcut" || vmName != "test-vm" || !maskSecrets { + t.Fatalf("unexpected settings lookup: %q %q %v", pluginID, vmName, maskSecrets) + } + return map[string]any{"shortcut": "Ctrl+F12", "autoRelease": false, "token": "private"} + } + intent := StartIntent{PluginID: "local.capture-shortcut"} + schema := &SettingsSchema{Scope: "vm", Properties: map[string]SettingProperty{ + "shortcut": {}, "autoRelease": {}, "token": {Secret: true}, + }} + rule := RuleHook{Actions: []RuleAction{{Kind: "set_env", EnvFromSettings: map[string]string{ + "REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_KEYS": "shortcut", + "REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_RELEASE_ON_LEAVE": "autoRelease", + "REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_TOKEN": "token", + "PATH": "shortcut", + }}}} + reg.applyRuleEnvSettings(&intent, rule, schema, "test-vm") + want := map[string]string{ + "REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_KEYS": "Ctrl+F12", + "REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_RELEASE_ON_LEAVE": "false", + } + if !reflect.DeepEqual(intent.Env, want) { + t.Fatalf("unexpected environment: %#v", intent.Env) + } +} diff --git a/internal/plugin/rules.go b/internal/plugin/rules.go index 032a4f4..73b589c 100644 --- a/internal/plugin/rules.go +++ b/internal/plugin/rules.go @@ -16,13 +16,14 @@ type RuleCondition struct { // RuleAction is a constrained action emitted when all conditions match (§5.4). type RuleAction struct { - Kind string `json:"kind"` // "append_args" | "set_env" | "warn" | "post_start" - Key string `json:"key,omitempty"` // fragment key for append_args - Tokens []string `json:"tokens,omitempty"` // QEMU arg tokens - Env map[string]string `json:"env,omitempty"` // env overrides - Message LocalizedText `json:"message,omitempty"` // localized warning text (accepts string or {"zh-CN":..., "en-US":...}) - Action string `json:"action,omitempty"` // post_start kind - Params map[string]any `json:"params,omitempty"` + Kind string `json:"kind"` // "append_args" | "set_env" | "warn" | "post_start" + Key string `json:"key,omitempty"` // fragment key for append_args + Tokens []string `json:"tokens,omitempty"` // QEMU arg tokens + Env map[string]string `json:"env,omitempty"` // env overrides + EnvFromSettings map[string]string `json:"envFromSettings,omitempty"` // environment key to plugin setting + Message LocalizedText `json:"message,omitempty"` // localized warning text (accepts string or {"zh-CN":..., "en-US":...}) + Action string `json:"action,omitempty"` // post_start kind + Params map[string]any `json:"params,omitempty"` } // RuleHook is an L1 declarative hook rule contributed via plugin.json (§5.4). @@ -92,7 +93,7 @@ func factContains(actual, needle any) bool { } } -func (r *Registry) registerRuleHooks(sc *Scope, pluginID string, rules []RuleHook) { +func (r *Registry) registerRuleHooks(sc *Scope, pluginID string, rules []RuleHook, schema *SettingsSchema) { for _, rule := range rules { rl := rule switch rl.Event { @@ -110,7 +111,9 @@ func (r *Registry) registerRuleHooks(sc *Scope, pluginID string, rules []RuleHoo if !EvaluateRuleConditions(rl.Conditions, facts) { return nil, nil } - return buildRuleStartIntent(pluginID, rl, string(r.currentLocale())), nil + intent := buildRuleStartIntent(pluginID, rl, string(r.currentLocale())) + r.applyRuleEnvSettings(&intent, rl, schema, p.VMName) + return intent, nil }, }) case EventHostPreflight: @@ -142,6 +145,39 @@ func (r *Registry) registerRuleHooks(sc *Scope, pluginID string, rules []RuleHoo } } +func (r *Registry) applyRuleEnvSettings(intent *StartIntent, rule RuleHook, schema *SettingsSchema, vmName string) { + if schema == nil || r.ruleSettings == nil { + return + } + for _, action := range rule.Actions { + if action.Kind != "set_env" { + continue + } + for envKey, settingKey := range action.EnvFromSettings { + prop, ok := schema.Properties[settingKey] + if !ok || prop.Secret || !ValidateEnvKey(intent.PluginID, envKey) { + continue + } + scope := prop.Scope + if scope == "" { + scope = schema.Scope + } + settingsScope := "" + if scope == "vm" { + settingsScope = vmName + } + value := r.ruleSettings(intent.PluginID, settingsScope, true)[settingKey] + switch value := value.(type) { + case string, bool: + if intent.Env == nil { + intent.Env = make(map[string]string) + } + intent.Env[envKey] = fmt.Sprint(value) + } + } + } +} + func buildStartFacts(p BeforeStartPayload) map[string]any { facts := make(map[string]any, len(p.HostFacts)+8) for k, v := range p.HostFacts { diff --git a/internal/plugin/types.go b/internal/plugin/types.go index 71ead56..460e4f6 100644 --- a/internal/plugin/types.go +++ b/internal/plugin/types.go @@ -15,7 +15,7 @@ import ( const ( // PluginAPIVersion is the host plugin API contract version (SemVer). - PluginAPIVersion = "1.1.0" + PluginAPIVersion = "1.2.0" // UIAPIVersion is the frontend UI contract version (SemVer); the contract // itself lives in frontend/src/styles/ui-contract.ts (§5.1, §6.3). UIAPIVersion = "2.0.0" From a853460fc38702ce30ff743fd66bd1becbd848cb Mon Sep 17 00:00:00 2001 From: hanyewei <139319228@qq.com> Date: Fri, 4 Dec 2026 15:44:03 +0800 Subject: [PATCH 2/2] fix(plugins): show active runtime compatibility in plugin settings --- changes/plugin-runtime-compatibility.json | 7 ++++ frontend/src/App.tsx | 1 + frontend/src/components/PluginsPage.tsx | 18 +++++++- frontend/wailsjs/go/models.ts | 2 + internal/app/plugins_flow.go | 27 +++++++++++- internal/plugin/rule_settings_test.go | 39 +++++++++++++++++ internal/plugin/rules.go | 51 +++++++++++++++++++++++ internal/plugin/types.go | 1 + locales/en-US/native.json | 1 + locales/zh-CN/native.json | 1 + 10 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 changes/plugin-runtime-compatibility.json diff --git a/changes/plugin-runtime-compatibility.json b/changes/plugin-runtime-compatibility.json new file mode 100644 index 0000000..d2ef969 --- /dev/null +++ b/changes/plugin-runtime-compatibility.json @@ -0,0 +1,7 @@ +{ + "group": "fixes", + "order": 0, + "authors": ["cosimawei"], + "zh": "在插件列表和设置中提示当前运行时不支持的插件设置。", + "en": "Show which settings the active runtime does not support in the plugin list and settings dialog." +} diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 868e486..13cea07 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1212,6 +1212,7 @@ export const App: React.FC = () => { locale={locale} appearance={theme} safeMode={state.safe_mode} + runtimeCommit={state.active_runtime?.commit} onRefresh={refresh} onError={fail} onThemeChanged={() => setThemeRevision((r) => r + 1)} diff --git a/frontend/src/components/PluginsPage.tsx b/frontend/src/components/PluginsPage.tsx index 47454fd..359a555 100644 --- a/frontend/src/components/PluginsPage.tsx +++ b/frontend/src/components/PluginsPage.tsx @@ -27,6 +27,7 @@ interface Props { locale: Locale appearance: 'light' | 'dark' safeMode?: boolean + runtimeCommit?: string onRefresh: () => void onError: (title: string, e: unknown) => void onThemeChanged?: () => void @@ -46,7 +47,7 @@ function hasAppSettings(p: PluginInfo): boolean { /** Top-level Plugins & Themes management page. */ export const PluginsPage: React.FC = ({ - locale, appearance, safeMode, onRefresh, onError, onThemeChanged, onLocaleSwitch, + locale, appearance, safeMode, runtimeCommit, onRefresh, onError, onThemeChanged, onLocaleSwitch, }) => { const tr = (k: I18nKey, v?: Record) => t(locale, k, v) const [activeTab, setActiveTab] = useState<'installed' | 'market'>('installed') @@ -61,6 +62,7 @@ export const PluginsPage: React.FC = ({ const [approveChecked, setApproveChecked] = useState(false) const approveTargetIDRef = useRef('') const [settingsTarget, setSettingsTarget] = useState(null) + const settingsRuntimeWarning = plugins.find((p) => p.id === settingsTarget?.id)?.runtime_warning const [settingsValues, setSettingsValues] = useState>({}) const [logTarget, setLogTarget] = useState<{ id: string; text: string } | null>(null) const [removeTarget, setRemoveTarget] = useState(null) @@ -151,7 +153,7 @@ export const PluginsPage: React.FC = ({ onThemeChanged?.() } - useEffect(() => { void reload() }, [safeMode]) + useEffect(() => { void reload() }, [safeMode, runtimeCommit, locale]) useEffect(() => { if (activeTab === 'market') setMarketOpened(true) }, [activeTab]) @@ -429,6 +431,12 @@ export const PluginsPage: React.FC = ({ {alert} )} + {p.runtime_warning && ( +
+
+ )}
{!isBuiltin && ( @@ -868,6 +876,12 @@ export const PluginsPage: React.FC = ({ onClose={() => setSettingsTarget(null)} >
+ {settingsRuntimeWarning && ( +
+
+ )} 0 { + info.RuntimeWarning = i18n.M(locale, "plugin.runtime_settings_skipped", i18n.Vars{ + "settings": strings.Join(settings, ", "), + }) + } + } + return infos } func (a *App) InstallPluginArchive(archivePath string) (*plugin.PluginInfo, error) { diff --git a/internal/plugin/rule_settings_test.go b/internal/plugin/rule_settings_test.go index 4a34062..2ee5441 100644 --- a/internal/plugin/rule_settings_test.go +++ b/internal/plugin/rule_settings_test.go @@ -3,6 +3,8 @@ package plugin import ( "reflect" "testing" + + "github.com/Nwflower/reims-go/internal/i18n" ) func TestRuleEnvironmentUsesVisiblePluginSettings(t *testing.T) { @@ -32,3 +34,40 @@ func TestRuleEnvironmentUsesVisiblePluginSettings(t *testing.T) { t.Fatalf("unexpected environment: %#v", intent.Env) } } + +func TestRuntimeUnavailableSettings(t *testing.T) { + const capability = "window-release-shortcut" + manifest := Manifest{ + ID: "local.capture-shortcut", + Settings: &SettingsSchema{Properties: map[string]SettingProperty{ + "releaseKeys": {Title: LocalizedText{ZhCN: "释放快捷键"}}, + "releaseOnLeave": {Title: LocalizedText{ZhCN: "鼠标移出时释放"}}, + "private": {Secret: true}, + }}, + Contributes: Contributes{RuleHooks: []RuleHook{{ + Event: EventVMBeforeStart, + Conditions: []RuleCondition{{Fact: "runtime.capabilities", Operator: "contains", Value: capability}}, + Actions: []RuleAction{{Kind: "set_env", EnvFromSettings: map[string]string{ + "REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_KEYS": "releaseKeys", + "REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_RELEASE_ON_LEAVE": "releaseOnLeave", + "REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_PRIVATE": "private", + }}}, + }}}, + } + locale := i18n.Locale("zh-CN") + if got := RuntimeUnavailableSettings(manifest, nil, locale); !reflect.DeepEqual(got, []string{"释放快捷键", "鼠标移出时释放"}) { + t.Fatalf("unexpected settings for incompatible runtime: %v", got) + } + if got := RuntimeUnavailableSettings(manifest, []string{capability}, locale); len(got) != 0 { + t.Fatalf("compatible runtime should have no warning: %v", got) + } + manifest.Contributes.RuleHooks = append(manifest.Contributes.RuleHooks, RuleHook{ + Event: EventVMBeforeStart, + Actions: []RuleAction{{Kind: "set_env", EnvFromSettings: map[string]string{ + "REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_KEYS": "releaseKeys", + }}}, + }) + if got := RuntimeUnavailableSettings(manifest, nil, locale); !reflect.DeepEqual(got, []string{"鼠标移出时释放"}) { + t.Fatalf("settings covered by a fallback should have no warning: %v", got) + } +} diff --git a/internal/plugin/rules.go b/internal/plugin/rules.go index 73b589c..418b884 100644 --- a/internal/plugin/rules.go +++ b/internal/plugin/rules.go @@ -3,7 +3,10 @@ package plugin import ( "context" "fmt" + "slices" "strings" + + "github.com/Nwflower/reims-go/internal/i18n" ) // RuleCondition is a declarative, non-Turing-complete predicate over host, VM, @@ -145,6 +148,54 @@ func (r *Registry) registerRuleHooks(sc *Scope, pluginID string, rules []RuleHoo } } +// RuntimeUnavailableSettings reports settings guarded by rules that depend only +// on runtime capabilities. VM-specific rules cannot be assessed on the plugin page. +func RuntimeUnavailableSettings(m Manifest, caps []string, loc i18n.Locale) []string { + if m.Settings == nil { + return nil + } + facts := map[string]any{"runtime.capabilities": caps} + unavailable := make(map[string]bool) + available := make(map[string]bool) + for _, rule := range m.Contributes.RuleHooks { + if rule.Event != EventVMBeforeStart || slices.ContainsFunc(rule.Conditions, func(c RuleCondition) bool { + return c.Fact != "runtime.capabilities" + }) { + continue + } + matches := EvaluateRuleConditions(rule.Conditions, facts) + for _, action := range rule.Actions { + if action.Kind != "set_env" { + continue + } + for envKey, settingKey := range action.EnvFromSettings { + prop, ok := m.Settings.Properties[settingKey] + if !ok || prop.Secret || !ValidateEnvKey(m.ID, envKey) { + continue + } + if matches { + available[settingKey] = true + } else { + unavailable[settingKey] = true + } + } + } + } + var labels []string + for key := range unavailable { + if available[key] { + continue + } + label := m.Settings.Properties[key].Title.Get(string(loc)) + if label == "" { + label = key + } + labels = append(labels, label) + } + slices.Sort(labels) + return slices.Compact(labels) +} + func (r *Registry) applyRuleEnvSettings(intent *StartIntent, rule RuleHook, schema *SettingsSchema, vmName string) { if schema == nil || r.ruleSettings == nil { return diff --git a/internal/plugin/types.go b/internal/plugin/types.go index a002fe0..cb3ce98 100644 --- a/internal/plugin/types.go +++ b/internal/plugin/types.go @@ -773,6 +773,7 @@ type PluginInfo struct { InUse bool `json:"in_use"` Error string `json:"error,omitempty"` DegradedReason string `json:"degraded_reason,omitempty"` + RuntimeWarning string `json:"runtime_warning,omitempty"` Permissions []PermissionSpec `json:"permissions,omitempty"` ApprovedPermissions []string `json:"approved_permissions,omitempty"` NeedsApproval bool `json:"needs_approval"` diff --git a/locales/en-US/native.json b/locales/en-US/native.json index 804377d..8ade5b7 100644 --- a/locales/en-US/native.json +++ b/locales/en-US/native.json @@ -1,4 +1,5 @@ { + "plugin.runtime_settings_skipped": "The current runtime cannot apply these settings: {settings}. Switch to a runtime that supports them.", "network.log.create_network_prepare": "Preparing network dependencies and checking the uplink", "network.log.mode_applied": "Network mode applied: {mode}", "network.log.uplink_switched": "Bridged uplink changed. The main adapter was notified to reconnect and renew DHCP", diff --git a/locales/zh-CN/native.json b/locales/zh-CN/native.json index 909a776..fa8bdd9 100644 --- a/locales/zh-CN/native.json +++ b/locales/zh-CN/native.json @@ -1,4 +1,5 @@ { + "plugin.runtime_settings_skipped": "当前运行时无法应用以下设置:{settings}。切换到支持这些功能的运行时即可使用。", "network.log.create_network_prepare": "正在准备网络依赖并检查出口", "network.log.mode_applied": "网络模式已应用:{mode}", "network.log.uplink_switched": "桥接出口已更换,已通知主网卡重新连接并续租 DHCP",