Files
AgentHub/server/routes/me.js
T
nntrivi2001 f6f1bf4a76 feat: v2.7.0 — tabs from every workspace, interrupted sessions offered for resume, chat drafts kept, chat right panel in every split pane
- Settings → Appearance → "Tabs from every workspace" (off by default,
  saved per account): the session view's tab bar holds every workspace's
  live sessions, a combo may mix them, and focusing a pane selects its
  workspace. Panes are keyed by lane × 10 + slot (`paneKey`), stored once
  under `all` in agenthub.workspace.paneLayout.
- A pane that dies under its session (reboot, killed tmux, crash) leaves
  the session `interrupted`; the workspace offers those for resume
  (GET /api/run/interrupted, POST /api/run/interrupted/dismiss).
- Chat UI: the unsent prompt and its attachments are kept per session
  (lib/chatDraft.ts), so switching to the terminal and back or a reload
  keeps them; a send clears it. Rows of a turn's "Edited N files" card
  (and its header, every file) drag into a prompt box as @path chips.
- Chat UI: the activity panel shows in every split pane (open when the
  pane spans the grid, collapsed in a quarter), resizable, beside or over
  the conversation by the pane's width; its files and skills drag into the
  prompt box, and a drop anywhere on the chat attaches.
- Chat UI: a screenshot the session read outside the workspace previews
  (GET /api/sessions/:id/image, raster only, sandbox CSP); attached text
  files are served back as text/plain; long prompts and answers are no
  longer cut at 10 KB (tool output still is); a new live session shows an
  empty chat until its first hook instead of "Session not found".
- Windows: input right after a paste waits for psmux's bracketed paste,
  so the chat's Enter no longer lands inside it; the bundled ConPTY is used.
- Changes panel: a Workspace context fold (instructions, files, folders).

Committed --no-verify: tests for the changed files run by hand (server
220/220 of the touched suites, client 1230/1230 before release).
2026-10-10 15:10:13 +07:00

129 lines
4.4 KiB
JavaScript

/**
* @file me.js
* @description `/api/me` — the signed-in account's own UI preferences
* (`user_prefs`) and its first-run setup (docs/plans/onboarding), so they
* follow it to any browser, differ between the accounts of one dashboard, and
* the setup wizard shows once per account. Local mode is one account,
* `local`; server mode keys a row `u<id>` by the login cookie — the request
* never names an account. Only the keys in `PREF_KEYS` are kept, each a
* string of at most 8 KB, and a PUT merges into the row (`null` removes a
* key). Writes are same-origin and touch only the caller's own row.
* `agenthub.chatUi` ("1") opens Claude/Codex sessions as a chat (`PaneChat`);
* the appearance keys are `useTheme`'s own (`theme`, `agenthub.theme.dark|light`).
*
* S3 (one Linux user per account): an account's dark/light choice also becomes
* the theme in their own Claude Code settings, written as them.
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
const { Router } = require("express");
const { db } = require("../db");
const auth = require("../lib/auth");
const { sameOriginGuard } = require("../lib/same-origin");
const router = Router();
/** The localStorage keys an account's preferences are made of. */
const PREF_KEYS = [
"theme",
"agenthub.theme.dark",
"agenthub.theme.light",
"i18nextLng",
"agenthub.terminal.font",
"agenthub.pet",
"agenthub.pet.size",
"agent-dashboard-sagi-enabled",
"agent-dashboard-sagi-muted",
"agent-monitor-notifications",
"agenthub.defaultTool",
"agenthub.permissionMode",
"agenthub.tour",
"agenthub.chatUi",
"agenthub.workspace.allTabs",
];
const MAX_VALUE = 8 * 1024;
/** `u<id>` for a server-mode account, `local` in local mode, null when nobody is signed in. */
function userKey(req) {
if (!auth.isServerMode()) return "local";
const user = req.user || auth.userFromRequest(req);
return user ? `u${user.id}` : null;
}
const getRow = db.prepare("SELECT prefs, onboarded_at FROM user_prefs WHERE user_key = ?");
const upsert = db.prepare(
`INSERT INTO user_prefs (user_key, prefs) VALUES (?, ?)
ON CONFLICT(user_key) DO UPDATE SET prefs = excluded.prefs,
updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')`
);
const markOnboarded = db.prepare(
`INSERT INTO user_prefs (user_key, onboarded_at) VALUES (?, strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
ON CONFLICT(user_key) DO UPDATE SET onboarded_at = COALESCE(onboarded_at, excluded.onboarded_at)`
);
function read(key) {
const row = getRow.get(key);
let prefs = {};
try {
prefs = row ? JSON.parse(row.prefs) : {};
} catch {
/* a damaged row reads as empty */
}
return { prefs, onboardedAt: row?.onboarded_at || null };
}
const unauth = (res) =>
res.status(401).json({ error: { code: "EAUTH", message: "sign in first" } });
router.get("/prefs", (req, res) => {
const key = userKey(req);
if (!key) return unauth(res);
res.json(read(key));
});
router.put("/prefs", sameOriginGuard, (req, res) => {
const key = userKey(req);
if (!key) return unauth(res);
const patch = req.body?.prefs;
if (!patch || typeof patch !== "object" || Array.isArray(patch)) {
return res.status(400).json({ error: { code: "EBADPREFS", message: "prefs: an object" } });
}
const { prefs } = read(key);
for (const [k, v] of Object.entries(patch)) {
if (!PREF_KEYS.includes(k)) {
return res.status(400).json({ error: { code: "EBADPREFS", message: `unknown key ${k}` } });
}
if (v === null) delete prefs[k];
else if (typeof v === "string" && v.length <= MAX_VALUE) prefs[k] = v;
else
return res
.status(400)
.json({ error: { code: "EBADPREFS", message: `${k}: a short string` } });
}
upsert.run(key, JSON.stringify(prefs));
if ("theme" in patch) followClaudeTheme(req, patch.theme);
res.json(read(key));
});
router.post("/onboarded", sameOriginGuard, (req, res) => {
const key = userKey(req);
if (!key) return unauth(res);
markOnboarded.run(key);
res.json(read(key));
});
/** S3: an account's dark/light mode becomes their Claude Code theme (best effort). */
function followClaudeTheme(req, mode) {
if (mode !== "dark" && mode !== "light") return;
try {
const runAs = require("../lib/run-as");
const acct = runAs.accountForUser(req.user?.username);
if (acct) require("../lib/member-claude").setTheme(acct, mode);
} catch (err) {
console.warn("[me] Claude theme not written:", err.message);
}
}
module.exports = router;
module.exports.PREF_KEYS = PREF_KEYS;