An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Correct ChatGPT tool schemas and avoid refreshing models on settings entry https://github.com/siyuan-note/siyuan/issues/20109
Daniel531652d9078d
diff --git a/app/src/config/tabs/ai/aiProviderUi.ts b/app/src/config/tabs/ai/aiProviderUi.tsindex d5ef941149..a7f944c351 100644--- a/app/src/config/tabs/ai/aiProviderUi.ts+++ b/app/src/config/tabs/ai/aiProviderUi.ts@@ -498,6 +498,7 @@ const openProviderDetail = (root: HTMLElement, providerId?: string, preset?: IPrlet availableModelContextLengths: Record<string, number> = {};let hasFetchedModels = false;let fetchingModels = false;+ let chatGPTAccountReady = false;let draggingModelIndex: number | undefined;let dropModelTarget: {index: number; after: boolean} | undefined;const updateResponsesCompatibility = () => {@@ -741,7 +742,7 @@ const openProviderDetail = (root: HTMLElement, providerId?: string, preset?: IPrconfirmButton.disabled = false;icon?.classList.remove("fn__rotate");if (draft.authType === "chatgpt" && draft.accountID !== requestedAccountID) {- if (draft.accountID) { fetchModels(); }+ if (chatGPTAccountReady) { fetchModels(); }} else { onFinished?.(); }});};@@ -751,10 +752,17 @@ const openProviderDetail = (root: HTMLElement, providerId?: string, preset?: IPr"[data-provider-field='protocol']", "[data-type='providerHeaders']"]) {view.querySelector(selector)?.closest(".config-item")?.classList.add("fn__none");}- mountChatGPTAccount(view, draft, ready => {+ mountChatGPTAccount(view, draft, (ready, refreshModels) => {+ chatGPTAccountReady = ready;+ if (refreshModels) {+ availableModels = [];+ availableModelDisplayNames = {};+ availableModelContextLengths = {};+ hasFetchedModels = false;+ }updateModelActionButtons();renderDraftModels(modelsContainer, draft.models, availableModels);- if (ready && draft.models.length === 0 && !fetchingModels) { fetchModels(); }+ if (ready && refreshModels && draft.models.length === 0 && !fetchingModels) { fetchModels(); }});}diff --git a/app/src/config/tabs/ai/chatGPTAccount.test.ts b/app/src/config/tabs/ai/chatGPTAccount.test.tsindex 6facfbb4e6..010c1abd8c 100644--- a/app/src/config/tabs/ai/chatGPTAccount.test.ts+++ b/app/src/config/tabs/ai/chatGPTAccount.test.ts@@ -100,10 +100,11 @@ const fixture = (remote = false, initialProfiles = [account()], accountID = "")});const draft = {authType: "chatgpt", accountID, models: [{name: "previous-model"}]};const ready: boolean[] = [];- const mount = namespace.mountChatGPTAccount as (root: unknown, draft: unknown, onChange: (ready: boolean) => void) => void;- mount(view, draft, value => ready.push(value));+ const refreshes: boolean[] = [];+ const mount = namespace.mountChatGPTAccount as (root: unknown, draft: unknown, onChange: (ready: boolean, refreshModels: boolean) => void) => void;+ mount(view, draft, (value, refreshModels) => { ready.push(value); refreshes.push(refreshModels); });return {- controls, root, view, draft, calls, timers, ready, browserWindow, profiles, confirmations, outcomes, messages,+ controls, root, view, draft, calls, timers, ready, refreshes, browserWindow, profiles, confirmations, outcomes, messages,remove: () => { view.isConnected = false; observerCallback(); },finish: (response: unknown) => pendingStatus(response),};@@ -188,12 +189,14 @@ test("a single saved account is selected and displayed as text", () => {assert.equal(f.draft.models.length, 0);assert.equal(f.ready.at(-1), true);assert.equal(f.controls.login.classes.has("fn__none"), true);+ assert.equal(f.refreshes.at(-1), false);});test("reopening a selected single account preserves its models", () => {const f = fixture(false, [account()], "account-one");assert.equal(f.draft.models.length, 1);assert.equal(f.ready.at(-1), true);+ assert.equal(f.refreshes.at(-1), false);});test("logging out shows the signed-out state and retains the registration for sign-in", async () => {@@ -249,6 +252,7 @@ test("multiple saved accounts show a picker and switching clears the previous moassert.equal(f.draft.accountID, "account-two");assert.equal(f.draft.models.length, 0);assert.equal(f.ready.at(-1), true);+ assert.equal(f.refreshes.at(-1), true);});test("logging in adds the first account and replaces the empty state with account information", async () => {@@ -265,6 +269,7 @@ test("logging in adds the first account and replaces the empty state with accounassert.equal(f.draft.accountID, "account-one");assert.equal(f.ready.at(-1), true);assert.equal(f.controls.login.classes.has("fn__none"), true);+ assert.equal(f.refreshes.at(-1), true);});test("importing another account shows the picker with the imported account selected", async () => {@@ -277,6 +282,7 @@ test("importing another account shows the picker with the imported account selecassert.equal(f.draft.accountID, "account-two");assert.equal(f.draft.models.length, 0);assert.equal(f.ready.at(-1), true);+ assert.equal(f.refreshes.at(-1), true);});test("remote account settings offer import and retain account selection", async () => {diff --git a/app/src/config/tabs/ai/chatGPTAccount.ts b/app/src/config/tabs/ai/chatGPTAccount.tsindex 280357e086..918f15e703 100644--- a/app/src/config/tabs/ai/chatGPTAccount.ts+++ b/app/src/config/tabs/ai/chatGPTAccount.ts@@ -60,7 +60,8 @@ export const genChatGPTAccountHTML = () => {</div>`;};-export const mountChatGPTAccount = (view: HTMLElement, draft: Config.IProvider, onChange: (ready: boolean) => void) => {+export const mountChatGPTAccount = (view: HTMLElement, draft: Config.IProvider,+ onChange: (ready: boolean, refreshModels: boolean) => void) => {const root = view.querySelector<HTMLElement>("[data-type='chatGPTAccount']");const localKernel = !getHostCapabilities().remoteKernel && ["localhost", "127.0.0.1", "[::1]"].includes(location.hostname);const picker = root.querySelector<HTMLSelectElement>("[data-chatgpt='account']");@@ -81,7 +82,7 @@ export const mountChatGPTAccount = (view: HTMLElement, draft: Config.IProvider,transferButton.setAttribute("aria-expanded", "false");const accountLabel = (account: ChatGPTAccount) =>(account.email || account.name || "ChatGPT") + " (" + account.id.slice(-8) + ")";- const update = () => {+ const update = (refreshModels = false) => {const selected = profiles.find(item => item.id === draft.accountID);root.querySelector<HTMLElement>("[data-chatgpt='accountRow']").classList.toggle("fn__none", profiles.length === 0 && !attemptID);const accountInfo = root.querySelector<HTMLElement>("[data-chatgpt='accountInfo']");@@ -112,9 +113,9 @@ export const mountChatGPTAccount = (view: HTMLElement, draft: Config.IProvider,status.textContent = attemptID ? L.chatGPTSignInPendingTip : selected?.sharing ? L.mcpStatusConnected :selected?.connected ? L.mcpStatusAuthorizationRequired : selected ? L.chatGPTSignedOut : "";root.querySelector<HTMLElement>("[data-chatgpt='statusRow']").classList.toggle("fn__none", !status.textContent);- onChange(!!selected?.sharing && !busy && !attemptID);+ onChange(!!selected?.sharing && !busy && !attemptID, refreshModels);};- const load = async () => {+ const load = async (refreshModels = false) => {await fetchPost("/api/ai/chatgpt/accounts", undefined, response => {if (!disposed && response.code === 0) {profiles = response.data;@@ -123,7 +124,7 @@ export const mountChatGPTAccount = (view: HTMLElement, draft: Config.IProvider,draft.models = [];exportedFile = "";}- update();+ update(refreshModels);}});};@@ -147,7 +148,7 @@ export const mountChatGPTAccount = (view: HTMLElement, draft: Config.IProvider,draft.accountID = response.data.accountID;attemptID = "";draft.models = [];- await load();+ await load(true);} else if (response.data.state === "failed") {attemptID = "";update();@@ -160,7 +161,7 @@ export const mountChatGPTAccount = (view: HTMLElement, draft: Config.IProvider,draft.accountID = picker.value;draft.models = [];exportedFile = "";- update();+ update(true);});const transferPassword = () => {if (Array.from(password.value).length < 12) {@@ -182,6 +183,7 @@ export const mountChatGPTAccount = (view: HTMLElement, draft: Config.IProvider,if (action === "cancel") { stop(); update(); return; }if (action === "import") { if (transferPassword()) { fileInput.click(); } return; }let authorizationWindow: Window;+ let refreshModels = false;/// #if BROWSERif ((action === "login" || action === "add") && !isInMobileApp()) {authorizationWindow = window.open("about:blank", "_blank");@@ -220,6 +222,7 @@ export const mountChatGPTAccount = (view: HTMLElement, draft: Config.IProvider,draft.accountID = "";draft.models = [];exportedFile = "";+ refreshModels = true;if (!response.data.revoked) { showMessage(L.chatGPTRevokeFailed); }});await load();@@ -238,7 +241,7 @@ export const mountChatGPTAccount = (view: HTMLElement, draft: Config.IProvider,} finally {if (authorizationWindow && !attemptID) { authorizationWindow.close(); }busy = false;- if (!disposed) { update(); }+ if (!disposed) { update(refreshModels); }}});fileInput.addEventListener("change", async () => {@@ -247,12 +250,17 @@ export const mountChatGPTAccount = (view: HTMLElement, draft: Config.IProvider,if (!file || !secret) { return; }if (file.size > 1024 * 1024) { showMessage(L.chatGPTRemoteTip, undefined, "error"); return; }busy = true; update();+ let imported = false;try {await fetchPost("/api/ai/chatgpt/import", {password: secret, data: await file.text()}, response => {- if (!disposed && response.code === 0) { draft.accountID = response.data.id; draft.models = []; }+ if (!disposed && response.code === 0) {+ draft.accountID = response.data.id;+ draft.models = [];+ imported = true;+ }});await load();- } finally { password.value = ""; fileInput.value = ""; busy = false; if (!disposed) { update(); } }+ } finally { password.value = ""; fileInput.value = ""; busy = false; if (!disposed) { update(imported); } }});void load();};diff --git a/app/src/config/tabs/ai/chatGPTModelRefresh.test.ts b/app/src/config/tabs/ai/chatGPTModelRefresh.test.tsnew file mode 100644index 0000000000..d277c55ebf--- /dev/null+++ b/app/src/config/tabs/ai/chatGPTModelRefresh.test.ts@@ -0,0 +1,63 @@+import * as assert from "node:assert/strict";+import {readFileSync} from "node:fs";+import test from "node:test";+import {runInNewContext} from "node:vm";+import {createSourceFile, forEachChild, isCallExpression, isIdentifier, ModuleKind, ScriptTarget, transpileModule, type Node as TSNode} from "typescript";++const fixture = (models: Array<{name: string}> = []) => {+ const source = createSourceFile("aiProviderUi.ts", readFileSync("src/config/tabs/ai/aiProviderUi.ts", "utf8"), ScriptTarget.ES2021, true);+ let callback: string;+ const visit = (node: TSNode) => {+ if (isCallExpression(node) && isIdentifier(node.expression) && node.expression.text === "mountChatGPTAccount") {+ callback = node.arguments[2].getText(source);+ }+ forEachChild(node, visit);+ };+ visit(source);+ assert.ok(callback);+ const calls: string[] = [];+ const exports = {} as {update: (ready: boolean, refreshModels: boolean) => void};+ const context = {+ exports, draft: {models}, modelsContainer: {}, fetchingModels: false, chatGPTAccountReady: false,+ availableModels: ["previous-account-model"], availableModelDisplayNames: {previous: "Previous"},+ availableModelContextLengths: {previous: 100}, hasFetchedModels: true,+ updateModelActionButtons: () => {}, renderDraftModels: () => {}, fetchModels: () => calls.push("fetch"),+ };+ runInNewContext(transpileModule("exports.update = " + callback + ";", {compilerOptions: {module: ModuleKind.CommonJS}}).outputText, context);+ return {update: exports.update, context, calls};+};++test("opening account settings does not fetch models, even when the draft is empty", () => {+ for (const models of [[], [{name: "saved-model"}]]) {+ const f = fixture(models);+ f.update(true, false);+ f.update(true, false);+ assert.equal(f.calls.length, 0);+ assert.equal(f.context.draft.models, models);+ }+});++test("a newly authorized account fetches models and clears the previous catalog", () => {+ const f = fixture();+ f.update(true, true);+ assert.equal(f.calls.length, 1);+ assert.equal(f.context.availableModels.length, 0);+ assert.equal(Object.keys(f.context.availableModelDisplayNames).length, 0);+ assert.equal(Object.keys(f.context.availableModelContextLengths).length, 0);+ assert.equal(f.context.hasFetchedModels, false);+});++test("switching to an unauthorized account clears suggestions without fetching", () => {+ const f = fixture();+ f.update(false, true);+ assert.equal(f.calls.length, 0);+ assert.equal(f.context.availableModels.length, 0);+ assert.equal(f.context.chatGPTAccountReady, false);+});++test("account updates do not start a second model request while one is in progress", () => {+ const f = fixture();+ f.context.fetchingModels = true;+ f.update(true, true);+ assert.equal(f.calls.length, 0);+});diff --git a/kernel/util/chatgpt.go b/kernel/util/chatgpt.goindex 817a1bba40..fc9fcc2224 100644--- a/kernel/util/chatgpt.go+++ b/kernel/util/chatgpt.go@@ -100,11 +100,12 @@ func chatGPTRequestBody(data []byte) ([]byte, error) {}}body["tools"], _ = json.Marshal([]struct {- Type string `json:"type"`- Name string `json:"name"`- Tools []json.RawMessage `json:"tools"`+ Type string `json:"type"`+ Name string `json:"name"`+ Description string `json:"description"`+ Tools []json.RawMessage `json:"tools"`}{- {Type: "namespace", Name: "siyuan", Tools: tools},+ {Type: "namespace", Name: "siyuan", Description: "Tools for working with notes and the SiYuan workspace", Tools: tools},})}}diff --git a/kernel/util/chatgpt_test.go b/kernel/util/chatgpt_test.goindex 5065c17ba9..ea417113bc 100644--- a/kernel/util/chatgpt_test.go+++ b/kernel/util/chatgpt_test.go@@ -36,6 +36,12 @@ func TestChatGPTRequestCapabilitiesAndToolReplay(t *testing.T) {if tool["type"] != "namespace" || tool["name"] != "siyuan" {t.Fatal("tools not namespaced")}+ if description, ok := tool["description"].(string); !ok || strings.TrimSpace(description) == "" {+ t.Fatal("tool namespace is missing its required description")+ }+ if functions, ok := tool["tools"].([]any); !ok || len(functions) != 1 || functions[0].(map[string]any)["name"] != "query" {+ t.Fatal("namespaced tool definitions were not preserved")+ }if _, err = chatGPTRequestBody([]byte(`{"input":[],"tools":[{"type":"image_generation"}]}`)); err == nil {t.Fatal("unsupported hosted tool accepted")}