Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Load kernel plugins on mobile and retry early RPC calls https://github.com/siyuan-note/siyuan/issues/18271

Daniel618bf33db3e3
diff --git a/app/src/plugin/kernel.ts b/app/src/plugin/kernel.ts
index 87e2968e71..b87ddba0ee 100644
--- a/app/src/plugin/kernel.ts
+++ b/app/src/plugin/kernel.ts
@@ -2,6 +2,9 @@ import {fetchSyncPost} from "../util/fetch";
import type {EventBus} from "./EventBus";
+const KERNEL_PLUGIN_START_RETRY_INTERVAL = 100;
+const KERNEL_PLUGIN_START_RETRY_COUNT = 50;
+
export class Kernel implements IKernelPlugin {
public state: IKernelPluginState;
public rpc: IKernelPluginRpc;
@@ -153,6 +156,26 @@ export class Kernel implements IKernelPlugin {
return Lute.NewNodeID();
}
+ async #fetchRpc(body: string): Promise<any> {
+ for (let retry = 0; ; retry++) {
+ const response = await fetch(this.#rpcCallUrl, {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body,
+ });
+ if (response.status === 204) {
+ return;
+ }
+
+ const data = await response.json();
+ const code = data.error?.code;
+ if ((code !== -32001 && code !== -32002) || retry >= KERNEL_PLUGIN_START_RETRY_COUNT) {
+ return data;
+ }
+ await new Promise(resolve => window.setTimeout(resolve, KERNEL_PLUGIN_START_RETRY_INTERVAL));
+ }
+ }
+
async #initState() {
const response = await fetchSyncPost("/api/plugin/getLoadedPlugin", { name: this.#name });
if (this.state.code === -1 && response.data?.stateCode != null) {
@@ -163,13 +186,7 @@ export class Kernel implements IKernelPlugin {
async #rpcCall(method: TJsonRpcMethod, ...params: TJsonRpcMethodParams): Promise<any> {
const id = this.#generateId();
- const response = await fetch(this.#rpcCallUrl, {
- method: "POST",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ jsonrpc: "2.0", id, method, params }),
- });
-
- const data = await response.json();
+ const data = await this.#fetchRpc(JSON.stringify({ jsonrpc: "2.0", id, method, params }));
if (data.error) {
throw new JsonRpcError(data.error);
}
@@ -177,10 +194,8 @@ export class Kernel implements IKernelPlugin {
}
#rpcNotify(method: TJsonRpcMethod, ...params: TJsonRpcMethodParams): void {
- fetch(this.#rpcCallUrl, {
- method: "POST",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ jsonrpc: "2.0", method, params }),
+ this.#fetchRpc(JSON.stringify({ jsonrpc: "2.0", method, params })).catch((error) => {
+ console.error(`Failed to send JSON-RPC notification for method ${method}:`, error);
});
}
@@ -196,14 +211,7 @@ export class Kernel implements IKernelPlugin {
return request;
});
- const response = await fetch(this.#rpcCallUrl, {
- method: "POST",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify(requests),
- });
-
- const data = await response.json();
- return data;
+ return this.#fetchRpc(JSON.stringify(requests));
}
public async init() {
diff --git a/kernel/api/setting.go b/kernel/api/setting.go
index a3b6de77b2..3094053341 100644
--- a/kernel/api/setting.go
+++ b/kernel/api/setting.go
@@ -160,21 +160,20 @@ func setBazaar(c *gin.Context) {
return
}
- if bazaar.PetalDisabled || !bazaar.Trust {
- // disable all kernel plugins
- if model.OnKernelPluginsStop != nil {
+ petalsEnabled := model.IsPetalsEnabled()
+ model.Conf.Bazaar = bazaar
+ model.Conf.Save()
+ newPetalsEnabled := model.IsPetalsEnabled()
+ if petalsEnabled != newPetalsEnabled {
+ if newPetalsEnabled {
+ if model.OnKernelPluginsStart != nil {
+ model.OnKernelPluginsStart()
+ }
+ } else if model.OnKernelPluginsStop != nil {
model.OnKernelPluginsStop()
}
- } else {
- // enable all kernel plugins
- if model.OnKernelPluginsStart != nil {
- model.OnKernelPluginsStart()
- }
}
- model.Conf.Bazaar = bazaar
- model.Conf.Save()
-
ret.Data = bazaar
}
diff --git a/kernel/harmony/kernel.go b/kernel/harmony/kernel.go
index 6d3ded0103..d4f8ba5272 100644
--- a/kernel/harmony/kernel.go
+++ b/kernel/harmony/kernel.go
@@ -32,6 +32,7 @@ import (
"github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/job"
"github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/plugin"
"github.com/siyuan-note/siyuan/kernel/server"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/util"
@@ -71,6 +72,7 @@ func StartKernel(container, appDir, workspaceBaseDir, timezoneID, localIPs, lang
job.StartCron()
go model.AutoGenerateFileHistory()
go cache.LoadAssets()
+ go plugin.InitManager()
}()
}
diff --git a/kernel/mobile/kernel.go b/kernel/mobile/kernel.go
index 82293e8534..60eca2fa28 100644
--- a/kernel/mobile/kernel.go
+++ b/kernel/mobile/kernel.go
@@ -34,6 +34,7 @@ import (
"github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/job"
"github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/plugin"
"github.com/siyuan-note/siyuan/kernel/server"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/util"
@@ -229,6 +230,7 @@ func StartKernel(container, appDir, workspaceBaseDir, timezoneID, localIPs, lang
job.StartCron()
go model.AutoGenerateFileHistory()
go cache.LoadAssets()
+ go plugin.InitManager()
go model.StartEmbeddingIndexer()
}()
}
diff --git a/kernel/model/plugin_test.go b/kernel/model/plugin_test.go
new file mode 100644
index 0000000000..c7a7c59550
--- /dev/null
+++ b/kernel/model/plugin_test.go
@@ -0,0 +1,62 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package model
+
+import (
+ "testing"
+
+ "github.com/siyuan-note/siyuan/kernel/conf"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func TestIsPetalsEnabled(t *testing.T) {
+ originalConf := Conf
+ originalContainer := util.Container
+ t.Cleanup(func() {
+ Conf = originalConf
+ util.Container = originalContainer
+ })
+
+ Conf = NewAppConf()
+ Conf.Bazaar = conf.NewBazaar()
+ Conf.Bazaar.PetalDisabled = false
+ Conf.Bazaar.Trust = false
+
+ for _, container := range []string{util.ContainerAndroid, util.ContainerIOS, util.ContainerHarmony} {
+ util.Container = container
+ if !IsPetalsEnabled() {
+ t.Fatalf("petals should be enabled on mobile container [%s] without bazaar trust", container)
+ }
+ }
+
+ for _, container := range []string{util.ContainerStd, util.ContainerDocker} {
+ util.Container = container
+ if IsPetalsEnabled() {
+ t.Fatalf("petals should be disabled on container [%s] without bazaar trust", container)
+ }
+ }
+
+ Conf.Bazaar.Trust = true
+ if !IsPetalsEnabled() {
+ t.Fatal("petals should be enabled after bazaar trust")
+ }
+
+ Conf.Bazaar.PetalDisabled = true
+ if IsPetalsEnabled() {
+ t.Fatal("petals should be disabled explicitly")
+ }
+}
diff --git a/kernel/plugin/manager.go b/kernel/plugin/manager.go
index d1eee31f5f..02941455d9 100644
--- a/kernel/plugin/manager.go
+++ b/kernel/plugin/manager.go
@@ -150,7 +150,7 @@ func (m *PluginManager) Start() {
return
}
- if model.Conf.Bazaar.PetalDisabled || !model.Conf.Bazaar.Trust {
+ if !model.IsPetalsEnabled() {
logging.LogInfof("kernel plugins are disabled by configuration, skipping start")
return
}
@@ -229,7 +229,7 @@ func (m *PluginManager) StartPlugin(petal *model.Petal) (ok bool) {
}
}()
- if model.Conf.Bazaar.PetalDisabled || !model.Conf.Bazaar.Trust {
+ if !model.IsPetalsEnabled() {
ok = false
return
}