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: Stabilize agent welcome suggestion widths https://github.com/siyuan-note/siyuan/issues/20471
Daniel440677b0236a
diff --git a/app/src/assets/scss/business/_ai_agent.scss b/app/src/assets/scss/business/_ai_agent.scssindex 28815dee46..fca61555e3 100644--- a/app/src/assets/scss/business/_ai_agent.scss+++ b/app/src/assets/scss/business/_ai_agent.scss@@ -99,7 +99,7 @@text-align: center;padding: 16px 24px;gap: 12px;- margin: auto;+ margin: auto 0;}.agent-welcome__icon {diff --git a/app/tests/agentWelcomeLayout.test.js b/app/tests/agentWelcomeLayout.test.jsnew file mode 100644index 0000000000..68a32d4723--- /dev/null+++ b/app/tests/agentWelcomeLayout.test.js@@ -0,0 +1,166 @@+const assert = require("node:assert/strict");+const {mkdtempSync, readFileSync, readdirSync, rmSync} = require("node:fs");+const os = require("node:os");+const path = require("node:path");+const {test} = require("node:test");+const {runInNewContext} = require("node:vm");+const sass = require("sass");+const ts = require("typescript");++const stylesheet = name => sass.compile(path.join(__dirname, `../src/assets/scss/${name}.scss`), {+ logger: sass.Logger.silent,+}).css;+const declarations = (css, selector) => {+ const result = {};+ for (const match of css.matchAll(/([^{}]+)\{([^{}]*)\}/g)) {+ if (!match[1].split(",").map(item => item.trim()).includes(selector)) {+ continue;+ }+ for (const declaration of match[2].split(";")) {+ const separator = declaration.indexOf(":");+ if (separator > -1) {+ result[declaration.slice(0, separator).trim()] = declaration.slice(separator + 1).trim();+ }+ }+ }+ return result;+};++for (const name of ["base", "mobile"]) {+ test(`${name} welcome stretches across the message area without changing suggestion widths or centering`, () => {+ const css = stylesheet(name);+ const messages = declarations(css, ".agent-chat__messages");+ assert.equal(messages.display, "flex");+ assert.equal(messages["flex-direction"], "column");+ assert.ok([undefined, "normal", "stretch"].includes(messages["align-items"]));+ const welcome = declarations(css, ".agent-welcome");+ assert.equal(welcome.margin, "auto 0");+ assert.equal(welcome.width, undefined);+ assert.equal(welcome.padding, "16px 24px");+ assert.equal(welcome["align-items"], "center");+ assert.equal(welcome["text-align"], "center");+ const examples = declarations(css, ".agent-welcome__examples");+ assert.equal(examples.width, "100%");+ assert.equal(examples["max-width"], "300px");+ assert.equal(declarations(css, ".agent-welcome__greeting")["white-space"], "pre-line");+ });+}++const compile = file => ts.transpileModule(readFileSync(path.join(__dirname, "../src", file), "utf8"), {+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2021},+}).outputText;+const samples = () => {+ const escape = {};+ runInNewContext(compile("util/escape.ts"), {exports: escape});+ const renderer = compile("layout/dock/agent/AgentMessageRenderer.ts");+ const directory = path.join(__dirname, "../appearance/langs");+ return readdirSync(directory).filter(file => file.endsWith(".json")).map(file => {+ const languages = JSON.parse(readFileSync(path.join(directory, file), "utf8"));+ const exports = {};+ runInNewContext(renderer, {exports, require: () => escape, window: {siyuan: {languages}}});+ const keys = Object.keys(languages).filter(key => /^agentWelcome(?:First|Return|Morning|Day|Evening|Late)\d$/.test(key));+ assert.equal(keys.length, 24, `${file}: all greeting variants enter the layout regression`);+ return {locale: file, greetings: keys.map(key => ({key, html: exports.renderWelcomeHTML(true, key),+ noModelHTML: exports.renderWelcomeHTML(false, key)}))};+ });+};++// 使用真实渲染器和编译样式测量布局;无浏览器的环境只执行上面的样式契约检查。+const browserCases = ({css, languages, mobile}) => {+ const check = require("node:assert/strict");+ const style = document.createElement("style");+ style.textContent = css + "body{font-size:14px}#host{display:flex;flex-direction:column;height:800px}";+ document.head.append(style);+ document.body.innerHTML = `<div id="host" class="sy__agentChat${mobile ? " sy__agentChat--mobile" : ""}">` ++ '<div class="agent-chat__messages-wrap"><div class="agent-chat__messages fn__flex-1"></div></div></div>';+ const host = document.getElementById("host");+ const messages = document.querySelector(".agent-chat__messages");+ const near = (actual, expected, label) => check.ok(Math.abs(actual - expected) < 1, `${label}: ${actual} / ${expected}`);+ for (const width of [240, 320, 380, 760]) {+ host.style.width = width + "px";+ for (const language of languages) {+ let firstWidth;+ for (const greeting of language.greetings) {+ for (const hasModel of [true, false]) {+ messages.innerHTML = hasModel ? greeting.html : greeting.noModelHTML;+ const label = `${mobile}/${width}/${language.locale}/${greeting.key}/${hasModel}`;+ const welcome = document.querySelector(".agent-welcome");+ const outer = welcome.getBoundingClientRect();+ const padding = getComputedStyle(welcome);+ const messagePadding = getComputedStyle(messages);+ const available = messages.clientWidth - parseFloat(messagePadding.paddingLeft) - parseFloat(messagePadding.paddingRight);+ near(outer.width, available, `welcome includes padding ${label}`);+ near(outer.left, messages.getBoundingClientRect().left + parseFloat(messagePadding.paddingLeft), `left edge ${label}`);+ if (hasModel) {+ const innerWidth = outer.width - parseFloat(padding.paddingLeft) - parseFloat(padding.paddingRight);+ const expected = Math.min(300, innerWidth);+ const buttons = [...document.querySelectorAll(".agent-welcome__example")];+ check.equal(buttons.length, 4, label);+ for (const button of buttons) {+ const bounds = button.getBoundingClientRect();+ near(bounds.width, expected, `button width ${label}`);+ near(bounds.left + bounds.width / 2, outer.left + outer.width / 2, `center ${label}`);+ near(bounds.width, firstWidth ?? bounds.width, `stable across greetings ${label}`);+ }+ firstWidth = buttons[0].getBoundingClientRect().width;+ } else {+ const card = document.querySelector(".agent-welcome__no-model").getBoundingClientRect();+ check.ok(card.left >= outer.left && card.right <= outer.right, `no-model bounds ${label}`);+ near(card.left + card.width / 2, outer.left + outer.width / 2, `no-model center ${label}`);+ }+ }+ }+ }+ }+ return "Agent welcome layout cases passed";+};++if (process.versions.electron && process.type === "browser") {+ const {app, BrowserWindow} = require("electron");+ app.setPath("userData", process.argv[2]);+ app.whenReady().then(async () => {+ const win = new BrowserWindow({show: false, width: 900, height: 900,+ webPreferences: {nodeIntegration: true, contextIsolation: false}});+ try {+ const languages = samples();+ for (const name of ["base", "mobile"]) {+ const compiled = stylesheet(name);+ for (const theme of ["daylight", "midnight"]) {+ await win.loadURL("data:text/html,<html><body></body></html>");+ const css = readFileSync(path.join(__dirname, `../appearance/themes/${theme}/theme.css`), "utf8") + compiled;+ assert.equal(await win.webContents.executeJavaScript(+ `(${browserCases.toString()})(${JSON.stringify({css, languages, mobile: name === "mobile"})})`),+ "Agent welcome layout cases passed");+ }+ }+ console.log("Agent welcome layout cases passed");+ win.destroy();+ app.exit(0);+ } catch (error) {+ console.error(error);+ win.destroy();+ app.exit(1);+ }+ }).catch(error => {+ console.error(error);+ app.exit(1);+ });+} else {+ test("welcome buttons stay stable across localized greetings, narrow widths and both model states", {+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,+ timeout: 120000,+ }, async () => {+ const env = {...process.env};+ delete env.ELECTRON_RUN_AS_NODE;+ const profile = mkdtempSync(path.join(os.tmpdir(), "siyuan-agent-welcome-"));+ try {+ const {stdout} = await require("node:util").promisify(require("node:child_process").execFile)(+ require("electron"), [__filename, profile], {env, windowsHide: true, timeout: 110000});+ assert.match(stdout, /Agent welcome layout cases passed/);+ } finally {+ assert.equal(path.dirname(path.resolve(profile)), path.resolve(os.tmpdir()));+ assert.ok(path.basename(profile).startsWith("siyuan-agent-welcome-"));+ rmSync(profile, {recursive: true, force: true, maxRetries: 5, retryDelay: 100});+ }+ });+}