feat(mindspace): 0630004 空间 UI、聊天连接、微信分享与 Agent 能力

含 MindSpace 三列布局与统计修复、聊天加载态与连接降级、平台页脚标记与 og:site_name 微信卡片、勾选资料删除 Agent 接口及内部话术过滤。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
john
2026-06-30 09:30:51 +08:00
parent 0203d81942
commit 2b8dba1784
53 changed files with 2450 additions and 313 deletions
+64 -2
View File
@@ -174,15 +174,31 @@ function resolveScheduleTimestamp({
epochMs = null,
localString = null,
timezone = DEFAULT_TIMEZONE,
fieldName = "\u65F6\u95F4"
fieldName = "\u65F6\u95F4",
now = Date.now()
} = {}) {
const local = String(localString ?? "").trim();
if (local) return parseLocalDateTimeString(local, timezone);
if (epochMs == null || epochMs === "") return null;
return assertReasonableScheduleEpoch(epochMs, { now, fieldName });
}
function assertReasonableScheduleEpoch(epochMs, { now = Date.now(), fieldName = "\u65F6\u95F4", maxPastDays = 2, maxFutureDays = 400 } = {}) {
const safe = Number(epochMs);
if (!Number.isFinite(safe) || safe <= 0) {
throw new Error(`${fieldName}\u65E0\u6548\uFF0C\u8BF7\u6539\u7528 YYYY-MM-DD HH:mm \u7684 local \u5B57\u6BB5`);
}
const min = now - maxPastDays * 864e5;
const max = now + maxFutureDays * 864e5;
if (safe < min || safe > max) {
const label = new Intl.DateTimeFormat("zh-CN", {
timeZone: normalizeTimezone(process.env.H5_DEFAULT_TIMEZONE),
dateStyle: "short",
timeStyle: "short"
}).format(new Date(safe));
throw new Error(
`${fieldName}\uFF08${label}\uFF09\u8D85\u51FA\u5408\u7406\u8303\u56F4\u3002\u8BF7\u6539\u7528 startLocal/remindLocal\uFF08YYYY-MM-DD HH:mm\uFF0C\u7528\u6237\u65F6\u533A\uFF09\uFF0C\u7981\u6B62\u81EA\u884C\u4F30\u7B97 Unix \u6BEB\u79D2\u3002`
);
}
return safe;
}
@@ -467,7 +483,7 @@ function createScheduleService(pool, options = {}) {
FROM h5_schedule_reminders r
INNER JOIN h5_schedule_items i ON i.id = r.item_id
WHERE r.user_id = ?
AND r.status IN ('pending', 'locked')
AND r.status IN ('pending', 'locked', 'sent')
AND r.remind_at >= ?
AND r.remind_at < ?
AND i.deleted_at IS NULL
@@ -494,6 +510,48 @@ function createScheduleService(pool, options = {}) {
);
return rowToItem(rows[0]);
};
const getReminder = async ({ userId, reminderId }) => {
if (!userId || !reminderId) throw new Error("\u7F3A\u5C11\u63D0\u9192\u53C2\u6570");
const [rows] = await pool.query(
`SELECT r.*,
i.title AS item_title,
i.kind AS item_kind,
i.timezone AS item_timezone,
i.start_at AS item_start_at,
i.end_at AS item_end_at
FROM h5_schedule_reminders r
INNER JOIN h5_schedule_items i ON i.id = r.item_id
WHERE r.id = ? AND r.user_id = ? AND i.deleted_at IS NULL
LIMIT 1`,
[reminderId, userId]
);
return rowToReminderWithItem(rows[0]);
};
const cancelReminder = async ({ userId, reminderId, reason = "\u7528\u6237\u5FFD\u7565" } = {}) => {
const reminder = await getReminder({ userId, reminderId });
if (!reminder) throw new Error("\u63D0\u9192\u4E0D\u5B58\u5728\u6216\u65E0\u6743\u8BBF\u95EE");
if (reminder.status === "cancelled") return reminder;
if (reminder.status === "sent") throw new Error("\u5DF2\u901A\u77E5\u7684\u63D0\u9192\u4E0D\u80FD\u5FFD\u7565");
return markReminderCancelled(reminder, reason);
};
const deleteReminder = async ({ userId, reminderId }) => {
if (!userId || !reminderId) throw new Error("\u7F3A\u5C11\u63D0\u9192\u53C2\u6570");
const [result] = await pool.query(
`DELETE FROM h5_schedule_reminders WHERE id = ? AND user_id = ?`,
[reminderId, userId]
);
return Number(result?.affectedRows ?? 0) > 0;
};
const deleteReminders = async ({ userId, reminderIds }) => {
if (!userId) throw new Error("\u7F3A\u5C11\u7528\u6237");
const ids = [...new Set((reminderIds ?? []).map((id) => String(id ?? "").trim()).filter(Boolean))];
if (ids.length === 0) return 0;
const [result] = await pool.query(
`DELETE FROM h5_schedule_reminders WHERE user_id = ? AND id IN (${ids.map(() => "?").join(", ")})`,
[userId, ...ids]
);
return Number(result?.affectedRows ?? 0);
};
const createReminder = async ({
userId,
itemId,
@@ -1041,6 +1099,10 @@ function createScheduleService(pool, options = {}) {
listTodayTodoItems,
listUpcomingItems,
listUpcomingReminders,
getReminder,
cancelReminder,
deleteReminder,
deleteReminders,
listDigestSubscriptions,
createDailyTodoDigest,
createBalanceLowAlert,
+448 -57
View File
@@ -5747,14 +5747,14 @@ function createMindSpaceService(pool2, options = {}) {
c.ai_access_policy, c.publish_policy, c.is_system, c.sort_order,
CASE
WHEN c.category_code = 'draft' THEN COALESCE(pc.item_count, 0)
WHEN c.category_code = 'public' THEN COALESCE(pub.item_count, 0)
WHEN c.category_code IN ('oa', 'public') THEN COALESCE(pc.item_count, 0) + COALESCE(ac.item_count, 0)
ELSE COALESCE(ac.item_count, 0)
END AS item_count
FROM h5_space_categories c
LEFT JOIN (
SELECT category_id, user_id, COUNT(*) AS item_count
FROM h5_assets
WHERE user_id = ? AND status <> 'deleted' AND source_type = 'upload'
WHERE user_id = ? AND status <> 'deleted'
GROUP BY category_id, user_id
) ac ON ac.category_id = c.id AND ac.user_id = c.user_id
LEFT JOIN (
@@ -5763,15 +5763,9 @@ function createMindSpaceService(pool2, options = {}) {
WHERE user_id = ? AND status <> 'deleted'
GROUP BY category_id, user_id
) pc ON pc.category_id = c.id AND pc.user_id = c.user_id
LEFT JOIN (
SELECT user_id, COUNT(DISTINCT page_id) AS item_count
FROM h5_publish_records
WHERE user_id = ? AND status = 'online'
GROUP BY user_id
) pub ON pub.user_id = c.user_id
WHERE c.user_id = ? AND c.space_id = ?
ORDER BY c.sort_order, c.category_name`,
[userId, userId, userId, userId, row.id]
[userId, userId, userId, row.id]
);
const quotaBytes = asNumber(row.quota_bytes);
const usedBytes = asNumber(row.used_bytes);
@@ -7503,7 +7497,7 @@ function renderBrandingBlock(userAddressName) {
- \u4F60\u662F **TKMind** \u52A9\u624B\uFF1B\u4ECB\u7ECD\u4EA7\u54C1\u65F6\u7528 TKMind\uFF0C\u4E0D\u8981\u79F0 goose\u3001Goose\u3001goosed
- \u4E0E\u7528\u6237\u5BF9\u8BDD\u65F6\uFF0C\u7528 **${name}** \u79F0\u547C\u7528\u6237\uFF08\u53EF\u8F85\u4EE5\u300C\u4F60/\u60A8\u300D\uFF09\uFF0C**\u7981\u6B62**\u628A\u7528\u6237\u53EB\u4F5C TKMind
- \u95EE\u5019\u793A\u4F8B\uFF1A\u300C${name}\uFF0C\u4E0B\u5348\u597D\u300D\u2014\u2014\u4E0D\u8981\u7528\u300CTKMind\uFF0C\u4E0B\u5348\u597D\u300D
- \u4EC5\u5728\u5F00\u573A\u6216\u7528\u6237\u6253\u62DB\u547C\u65F6\u4F7F\u7528\u65F6\u6BB5\u95EE\u5019\uFF0C\u4E14\u987B\u4E0E\u300CTKMind \u5F53\u524D\u65F6\u95F4\u57FA\u51C6\u300D\u4E00\u81F4\uFF08\u5982\u300C${name}\uFF0C\u65E9\u4E0A\u597D\u300D\uFF09\uFF1B\u666E\u901A\u56DE\u590D\u76F4\u63A5\u4F5C\u7B54\uFF0C\u4E0D\u8981\u6BCF\u6761\u90FD\u52A0\u95EE\u5019\uFF1B\u7981\u6B62\u628A\u7528\u6237\u53EB\u4F5C TKMind
- \u4E0D\u8981\u63CF\u8FF0\u672C\u5DE5\u4F5C\u533A\u4E3A\u300CRust goose \u9879\u76EE\u300D\u6216\u300Cgoose AI \u6846\u67B6\u300D
- \u672C\u5DE5\u4F5C\u533A\u662F TKMind **MindSpace \u7528\u6237\u7A7A\u95F4**\uFF0C\u7528\u4E8E\u6587\u4EF6\u7BA1\u7406\u4E0E\u9759\u6001\u9875\u9762\u751F\u6210
`;
@@ -7662,6 +7656,13 @@ description: \u5728\u4E13\u5C5E MindSpace \u76EE\u5F55\u751F\u6210\u53EF\u516C\u
- \u5B8C\u6574 \`<!DOCTYPE html>\`\uFF0C\`lang="zh-CN"\`
- \u79FB\u52A8\u7AEF\u53CB\u597D\uFF1A\`<meta name="viewport" content="width=device-width, initial-scale=1">\`
- \u6DF1\u8272/\u6D45\u8272\u4E0E\u5185\u5BB9\u4E00\u81F4\uFF1B\u4E3B\u8272\u5728 CSS \u4E0E mindspace-cover \u4E2D\u4FDD\u6301\u4E00\u81F4
- \u9875\u811A\u5E73\u53F0\u8054\u7CFB\u884C**\u5FC5\u987B**\u4F7F\u7528 \`data-mindspace-page-tag="platform-brand"\` \u6807\u8BB0\uFF0C\u4E14\u90AE\u7BB1/\u57DF\u540D\u53EA\u7528 **tkmind.cn**\uFF08\u5982 \`contact@tkmind.cn\`\uFF09\uFF0C**\u7981\u6B62** \`tkmind.ai\`
\`\`\`html
<p data-mindspace-page-tag="platform-brand">\u{1F4E7} contact@tkmind.cn</p>
\`\`\`
- \u5E26 \`data-mindspace-page-tag\` \u7684\u533A\u57DF\u4E3A\u5E73\u53F0\u56FA\u5B9A\u4FE1\u606F\uFF1A\u7528\u6237\u5728\u7F16\u8F91\u6A21\u5F0F\u4E2D\u4E0D\u53EF\u89C1\u3001\u4E0D\u53EF\u6539\uFF1B\u9884\u89C8\u4E0E\u53D1\u5E03\u540E\u6B63\u5E38\u663E\u793A
## \u67E5\u627E\u6587\u4EF6\uFF08CSV\u3001\u6587\u6863\u7B49\uFF09
@@ -7763,6 +7764,7 @@ function buildPublishConstraints({ slug, username, publicBaseUrl, publishDir, di
"- **\u7981\u6B62**\uFF1A\u8BBF\u95EE `assets/` \u5185\u90E8\u8DEF\u5F84\u3001\u5176\u5B83\u7528\u6237\u76EE\u5F55\u3001\u4E3B\u673A\u7EDD\u5BF9\u8DEF\u5F84\uFF1B\u7981\u6B62\u7528\u516C\u7F51 URL \u5217\u76EE\u5F55\u6216\u8BFB CSV",
"- **\u8DEF\u5F84\u89C4\u5219**\uFF1A\u53EA\u7528\u76F8\u5BF9\u8DEF\u5F84\uFF1B\u7981\u6B62 `../`\uFF1B\u5DE5\u4F5C\u533A\u5916\u7684\u8DEF\u5F84\u4F1A\u88AB\u7CFB\u7EDF\u62D2\u7EDD\uFF08OS \u5C42\u5F3A\u5236\uFF0C\u975E\u8F6F\u7EA6\u675F\uFF09",
"- **\u751F\u6210\u9875\u9762\uFF08\u5FC5\u987B\u4EB2\u81EA\u5B8C\u6210\uFF09**\uFF1A\u5148 `load_skill` \u2192 `static-page-publish`\uFF0C\u518D\u7528 `write_file`/`edit_file` \u5199\u5165 `public/\u9875\u9762.html`",
"- **\u7528\u6237\u53EF\u89C1\u56DE\u590D**\uFF1A\u4E0D\u8981\u5411\u7528\u6237\u590D\u8FF0 load_skill\u3001\u6280\u80FD\u66F4\u65B0\u3001\u9875\u811A\u6807\u8BB0\u3001mindspace-cover \u7B49\u5185\u90E8\u5B9E\u73B0\uFF1B\u5B8C\u6210\u540E\u76F4\u63A5\u7ED9\u51FA\u9875\u9762\u94FE\u63A5\u6216\u7ED3\u679C",
"- **\u7981\u6B62**\u7528 shell \u5199\u5165 HTML\uFF1B**\u7981\u6B62**\u8BA9\u7528\u6237\u300C\u624B\u52A8\u4FDD\u5B58\u5230 public \u76EE\u5F55\u300D\u6216\u8BF4\u300C\u6211\u65E0\u6CD5\u751F\u6210\u9875\u9762\u300D\u2014\u2014\u9664\u975E write_file \u5DF2\u5931\u8D25\u5E76\u62A5\u544A\u9519\u8BEF",
"- \u5B8C\u6210\u540E\u7ED9\u51FA Markdown \u53EF\u70B9\u51FB\u516C\u7F51\u94FE\u63A5 `[\u6807\u9898](URL)`\uFF1B\u5199\u5165 `public/\u9875\u9762.html` \u65F6 URL \u4E3A `.../MindSpace/<\u7528\u6237ID>/public/\u9875\u9762.html`",
`- \u53D1\u5E03\u6280\u80FD\uFF1A\`${PUBLISH_SKILL_NAME}\`\uFF08\u751F\u6210\u9875\u9762\u524D\u5E94 load_skill\uFF09`
@@ -7975,8 +7977,35 @@ function formatDateParts(now, timezone) {
day: parts.day ?? "01"
};
}
function formatLocalClockParts(now, timezone) {
const formatter = new Intl.DateTimeFormat("en-GB", {
timeZone: timezone,
hour: "2-digit",
minute: "2-digit",
hour12: false
});
const parts = Object.fromEntries(
formatter.formatToParts(new Date(now)).map((part) => [part.type, part.value])
);
return {
hour: Number(parts.hour ?? 0),
minute: Number(parts.minute ?? 0)
};
}
function resolveTimeOfDayPeriod(hour) {
const h = Number(hour);
if (h < 5) return { period: "\u51CC\u6668", greeting: "\u4F60\u597D" };
if (h < 9) return { period: "\u65E9\u4E0A", greeting: "\u65E9\u4E0A\u597D" };
if (h < 12) return { period: "\u4E0A\u5348", greeting: "\u4E0A\u5348\u597D" };
if (h < 14) return { period: "\u4E2D\u5348", greeting: "\u4E2D\u5348\u597D" };
if (h < 18) return { period: "\u4E0B\u5348", greeting: "\u4E0B\u5348\u597D" };
return { period: "\u665A\u4E0A", greeting: "\u665A\u4E0A\u597D" };
}
function renderCurrentTimeAnchor({ now = Date.now(), timezone = DEFAULT_TIMEZONE } = {}) {
const { year, month, day } = formatDateParts(now, timezone);
const { hour, minute } = formatLocalClockParts(now, timezone);
const { period, greeting } = resolveTimeOfDayPeriod(hour);
const clock = `${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`;
const weekday = new Intl.DateTimeFormat("zh-CN", {
timeZone: timezone,
weekday: "long"
@@ -7986,7 +8015,9 @@ function renderCurrentTimeAnchor({ now = Date.now(), timezone = DEFAULT_TIMEZONE
"",
`- \u5F53\u524D\u65F6\u533A\uFF1A${timezone}`,
`- \u5F53\u524D\u65E5\u671F\uFF1A${year}-${month}-${day}\uFF08${weekday}\uFF09`,
"- \u56DE\u7B54\u4E2D\u6D89\u53CA\u201C\u4ECA\u5929 / \u660E\u5929 / \u540E\u5929 / \u5468\u51E0\u201D\u65F6\uFF0C\u5FC5\u987B\u4EE5\u4E0A\u8FF0\u65E5\u671F\u4E3A\u51C6\u7EE7\u7EED\u63A8\u7B97\uFF0C\u4E0D\u8981\u81EA\u884C\u5047\u8BBE\u5F53\u524D\u65E5\u671F\u3002"
`- \u5F53\u524D\u65F6\u523B\uFF1A${clock}\uFF08${period}\uFF09`,
`- \u82E5\u9700\u65F6\u6BB5\u95EE\u5019\u53EF\u53C2\u8003\uFF1A${greeting}\uFF08\u4EC5\u65B0\u4F1A\u8BDD\u5F00\u573A\u6216\u7528\u6237\u4E3B\u52A8\u6253\u62DB\u547C\u65F6\u4F7F\u7528\uFF0C\u666E\u901A\u4EFB\u52A1\u56DE\u590D\u4E0D\u8981\u6BCF\u6761\u90FD\u52A0\uFF09`,
"- \u56DE\u7B54\u4E2D\u6D89\u53CA\u201C\u4ECA\u5929 / \u660E\u5929 / \u540E\u5929 / \u5468\u51E0 / \u65E9\u4E0A / \u4E0B\u5348 / \u665A\u4E0A\u201D\u7B49\u65F6\u95F4\u8868\u8FF0\u65F6\uFF0C\u5FC5\u987B\u4EE5\u4E0A\u8FF0\u65E5\u671F\u4E0E\u65F6\u523B\u4E3A\u51C6\uFF0C\u7981\u6B62\u81EA\u884C\u5047\u8BBE\u5F53\u524D\u65F6\u95F4\u6216\u4F7F\u7528 UTC \u7B49\u5176\u5B83\u65F6\u533A\u3002"
].join("\n");
}
function buildSessionMemoryEntries({
@@ -8018,6 +8049,18 @@ function buildSessionMemoryEntries({
title: "TKMind \u5F53\u524D\u65F6\u95F4\u57FA\u51C6",
content: renderCurrentTimeAnchor({ now, timezone })
});
const scheduleTools = (sessionPolicy?.extensionOverrides ?? []).find((ext) => ext.name === "sandbox-fs");
if ((scheduleTools?.available_tools ?? []).includes("schedule_create_item")) {
entries.push({
title: "TKMind \u65E5\u7A0B\u5199\u5165\u89C4\u5219",
content: [
"\u521B\u5EFA\u6216\u63D0\u9192\u5F85\u529E/\u65E5\u7A0B\u65F6\uFF1A",
"- \u5FC5\u987B\u4F7F\u7528 startLocal / endLocal / remindLocal\uFF0C\u683C\u5F0F YYYY-MM-DD HH:mm\uFF08\u7528\u6237\u65F6\u533A\u5899\u4E0A\u65F6\u949F\uFF09\u3002",
"- \u7981\u6B62\u81EA\u884C\u4F30\u7B97 Unix \u6BEB\u79D2\u65F6\u95F4\u6233\uFF1B\u4F20\u9519\u4F1A\u88AB\u5DE5\u5177\u62D2\u7EDD\u3002",
"- \u5199\u5165\u540E\u8C03\u7528 schedule_list_items \u6838\u5BF9\u65E5\u671F\u662F\u5426\u4E0E\u7528\u6237\u8868\u8FF0\u4E00\u81F4\u3002"
].join("\n")
});
}
if (!hasMemoryStore(sessionPolicy)) {
return entries;
}
@@ -9328,7 +9371,7 @@ function createTkmindProxy({
try {
const upstream = await apiFetch(target, apiSecret, "/sessions", {
method: "GET",
signal: AbortSignal.timeout(3e3)
signal: AbortSignal.timeout(12e3)
});
const text = await upstream.text();
if (!upstream.ok) {
@@ -10016,7 +10059,7 @@ function renderUserSpaceBrandingBlock(userAddressName) {
- \u4F60\u662F **TKMind** \u52A9\u624B\uFF1B\u4ECB\u7ECD\u4EA7\u54C1\u65F6\u7528 TKMind\uFF0C\u4E0D\u8981\u79F0 goose\u3001Goose\u3001goosed
- \u4E0E\u7528\u6237\u5BF9\u8BDD\u65F6\uFF0C\u7528 **${name}** \u79F0\u547C\u7528\u6237\uFF08\u53EF\u8F85\u4EE5\u300C\u4F60/\u60A8\u300D\uFF09\uFF0C**\u7981\u6B62**\u628A\u7528\u6237\u53EB\u4F5C TKMind
- \u95EE\u5019\u793A\u4F8B\uFF1A\u300C${name}\uFF0C\u4E0B\u5348\u597D\u300D\u2014\u2014\u4E0D\u8981\u7528\u300CTKMind\uFF0C\u4E0B\u5348\u597D\u300D
- \u4EC5\u5728\u5F00\u573A\u6216\u7528\u6237\u6253\u62DB\u547C\u65F6\u4F7F\u7528\u65F6\u6BB5\u95EE\u5019\uFF0C\u4E14\u987B\u4E0E\u300CTKMind \u5F53\u524D\u65F6\u95F4\u57FA\u51C6\u300D\u4E00\u81F4\uFF08\u5982\u300C${name}\uFF0C\u65E9\u4E0A\u597D\u300D\uFF09\uFF1B\u666E\u901A\u56DE\u590D\u76F4\u63A5\u4F5C\u7B54\uFF0C\u4E0D\u8981\u6BCF\u6761\u90FD\u52A0\u95EE\u5019\uFF1B\u7981\u6B62\u628A\u7528\u6237\u53EB\u4F5C TKMind
- \u4E0D\u8981\u63CF\u8FF0\u672C\u5DE5\u4F5C\u533A\u4E3A\u300CRust goose \u9879\u76EE\u300D\u6216\u300Cgoose AI \u6846\u67B6\u300D
- \u672C\u5DE5\u4F5C\u533A\u662F TKMind **MindSpace \u7528\u6237\u7A7A\u95F4**\uFF0C\u7528\u4E8E OA/\u516C\u5F00\u6587\u4EF6\u7BA1\u7406\u4E0E\u9759\u6001\u9875\u9762\u751F\u6210
`;
@@ -16398,6 +16441,45 @@ function resolveWorkspaceRelativeFilePath(htmlRelativePath, relativeRef) {
if (!htmlDir || htmlDir === ".") return `public/${ref}`;
return path18.posix.join(htmlDir, ref).replace(/\\/g, "/");
}
function resolveSiblingDocxPath(htmlRelativePath) {
const htmlPath = String(htmlRelativePath ?? "").replace(/^\/+/, "");
if (!htmlPath.toLowerCase().endsWith(".html")) return null;
return htmlPath.replace(/\.html$/i, ".docx");
}
function inferWorkspaceHtmlRelativePath({
snapshotRelativePath = null,
pageTitle = null,
htmlContent = null,
publishDir = null
} = {}) {
const fromSnapshot = String(snapshotRelativePath ?? "").trim();
if (fromSnapshot) {
const normalized = fromSnapshot.replace(/^\/+/, "");
if (normalized.toLowerCase().startsWith("public/")) return normalized;
if (normalized.toLowerCase().endsWith(".html")) return `public/${path18.posix.basename(normalized)}`;
return normalized;
}
const publicDir = publishDir ? path18.join(publishDir, "public") : null;
if (!publicDir || !fs14.existsSync(publicDir) || !htmlContent) {
return "public/index.html";
}
const normalizedContent = String(htmlContent);
const title = String(pageTitle ?? "").trim();
for (const name of fs14.readdirSync(publicDir)) {
if (!name.toLowerCase().endsWith(".html")) continue;
const absolutePath = path18.join(publicDir, name);
try {
const diskContent = fs14.readFileSync(absolutePath, "utf8");
if (diskContent === normalizedContent) return `public/${name}`;
if (title) {
const titleMatch = diskContent.match(/<title[^>]*>([^<]+)<\/title>/i);
if (titleMatch?.[1]?.trim() === title) return `public/${name}`;
}
} catch {
}
}
return "public/index.html";
}
function buildWorkspaceDownloadLinkIndex(assets = []) {
const byBasename = /* @__PURE__ */ new Map();
const byPath = /* @__PURE__ */ new Map();
@@ -16409,6 +16491,8 @@ function buildWorkspaceDownloadLinkIndex(assets = []) {
byBasename.set(path18.posix.basename(filename), id);
if (filename.startsWith("public/")) {
byPath.set(filename.slice("public/".length), id);
} else if (!filename.includes("/")) {
byPath.set(`public/${filename}`, id);
}
}
return { byBasename, byPath };
@@ -16434,6 +16518,19 @@ function workspaceFileExists(publishDir, workspaceRelativePath) {
return false;
}
}
function collectDownloadWorkspaceCandidates(htmlRelativePath, relativeRef, publishDir) {
const workspacePath = resolveWorkspaceRelativeFilePath(htmlRelativePath, relativeRef);
const candidates = /* @__PURE__ */ new Set([
workspacePath,
path18.posix.join("public", path18.posix.basename(workspacePath))
]);
const siblingDocx = resolveSiblingDocxPath(htmlRelativePath);
if (siblingDocx) {
candidates.add(siblingDocx);
candidates.add(path18.posix.join("public", path18.posix.basename(siblingDocx)));
}
return [...candidates];
}
function resolveDownloadTargetUrl({
relativeRef,
htmlRelativePath = "public/index.html",
@@ -16444,24 +16541,25 @@ function resolveDownloadTargetUrl({
preferAssetDownload = true
}) {
const { suffix } = splitReferenceParts(relativeRef);
const workspacePath = resolveWorkspaceRelativeFilePath(htmlRelativePath, relativeRef);
const assetId = lookupAssetIdForWorkspacePath(linkIndex, workspacePath);
const publicCandidates = [
workspacePath,
path18.posix.join("public", path18.posix.basename(workspacePath))
];
if (preferAssetDownload && assetId) {
return `${buildAssetDownloadUrl(assetId)}${suffix}`;
const candidates = collectDownloadWorkspaceCandidates(htmlRelativePath, relativeRef, publishDir);
for (const candidate of candidates) {
const assetId = lookupAssetIdForWorkspacePath(linkIndex, candidate);
if (preferAssetDownload && assetId) {
return `${buildAssetDownloadUrl(assetId)}${suffix}`;
}
}
if (publishDir && publishKey) {
for (const candidate of publicCandidates) {
for (const candidate of candidates) {
if (workspaceFileExists(publishDir, candidate)) {
return `${buildPublicWorkspaceFileUrl(publicBaseUrl, publishKey, candidate)}${suffix}`;
}
}
}
if (assetId) {
return `${buildAssetDownloadUrl(assetId)}${suffix}`;
for (const candidate of candidates) {
const assetId = lookupAssetIdForWorkspacePath(linkIndex, candidate);
if (assetId) {
return `${buildAssetDownloadUrl(assetId)}${suffix}`;
}
}
return null;
}
@@ -16492,6 +16590,37 @@ async function prepareHtmlDownloadLinks(pool2, userId, html, options = {}) {
return rewriteRelativeDownloadLinks(html, { ...options, linkIndex });
}
// mindspace-page-tag.mjs
var MINDSPACE_PAGE_TAG_ATTR = "data-mindspace-page-tag";
var MINDSPACE_PAGE_TAG_PLATFORM_BRAND = "platform-brand";
var PLATFORM_BRAND_INNER_RE = /(?:contact@tkmind\.(?:ai|cn)|📧[^<]*tkmind)/i;
var SIMPLE_BRAND_ELEMENT_RE = /<(p|div|span|small|footer|a)(\s[^>]*)?>([^<]*(?:contact@tkmind\.(?:ai|cn)|📧[^<]*tkmind)[^<]*)<\/\1>/gi;
function normalizePlatformDomainText(text) {
return String(text ?? "").replace(/tkmind\.ai/gi, "tkmind.cn");
}
function normalizePlatformBrandHtml(html) {
let next = normalizePlatformDomainText(html);
next = next.replace(/mailto:([^"'>\s]+@tkmind)\.ai/gi, "mailto:$1.cn");
next = next.replace(
/https?:\/\/([a-z0-9.-]*\.)?tkmind\.ai/gi,
(match) => match.replace(/tkmind\.ai/gi, "tkmind.cn")
);
return next;
}
function ensurePlatformBrandPageTags(html) {
const normalized = normalizePlatformBrandHtml(html);
return normalized.replace(SIMPLE_BRAND_ELEMENT_RE, (match, tag, attrs, inner) => {
const attrStr = attrs ?? "";
if (attrStr.includes(MINDSPACE_PAGE_TAG_ATTR)) return match;
if (!PLATFORM_BRAND_INNER_RE.test(inner)) return match;
const spacer = attrStr ? "" : " ";
return `<${tag}${attrStr}${spacer}${MINDSPACE_PAGE_TAG_ATTR}="${MINDSPACE_PAGE_TAG_PLATFORM_BRAND}">${inner}</${tag}>`;
});
}
function prepareHtmlPageBrandMarkers(html) {
return ensurePlatformBrandPageTags(html);
}
// mindspace-page-purge.mjs
import fs15 from "node:fs/promises";
import path19 from "node:path";
@@ -16623,6 +16752,13 @@ function parseJsonColumn(value, fallback = {}) {
function pageError(message, code, details) {
return Object.assign(new Error(message), { code, details });
}
function normalizeWorkspaceRelativePath2(relativePath) {
const parts = String(relativePath ?? "").replace(/^\/+/, "").split("/").filter((part) => part && part !== "." && part !== "..");
if (parts.length === 0) return "";
if (parts[0].toLowerCase() === "public") return parts.join("/");
if (parts.length === 1 && parts[0].toLowerCase().endsWith(".html")) return `public/${parts[0]}`;
return parts.join("/");
}
function normalizeText(value, maxLength, fieldName) {
const text = String(value ?? "").normalize("NFKC").trim();
if (!text) throw pageError(`${fieldName}\u4E0D\u80FD\u4E3A\u7A7A`, "invalid_page_input");
@@ -16633,13 +16769,16 @@ function normalizeText(value, maxLength, fieldName) {
}
function normalizePageInput(input) {
const title = normalizeText(input.title, MAX_TITLE_LENGTH, "\u6807\u9898");
const content = normalizeText(input.content, MAX_CONTENT_BYTES, "\u9875\u9762\u5185\u5BB9");
let content = normalizeText(input.content, MAX_CONTENT_BYTES, "\u9875\u9762\u5185\u5BB9");
const contentFormat = input.contentFormat === "html" ? "html" : "markdown";
if (contentFormat === "html") {
content = prepareHtmlPageBrandMarkers(content);
}
const contentBytes = Buffer.byteLength(content, "utf8");
if (contentBytes > MAX_CONTENT_BYTES) {
throw pageError("\u9875\u9762\u5185\u5BB9\u8D85\u8FC7 1 MB \u9650\u5236", "page_content_too_large");
}
const summary = String(input.summary ?? "").normalize("NFKC").trim().slice(0, MAX_SUMMARY_LENGTH);
const contentFormat = input.contentFormat === "html" ? "html" : "markdown";
const templateId = contentFormat === "html" ? "static-html" : TEMPLATE_IDS.has(input.templateId) ? input.templateId : "editorial";
const plainSummary = contentFormat === "html" ? content.replace(/<script[\s\S]*?<\/script>/gi, " ").replace(/<style[\s\S]*?<\/style>/gi, " ").replace(/<[^>]+>/g, " ").replace(/\s+/g, " ").trim().slice(0, 180) : content.replace(/\s+/g, " ").slice(0, 180);
return {
@@ -17105,6 +17244,24 @@ function createPageService(pool2, options = {}) {
);
return rows[0] ? pageResponse(rows[0]) : null;
};
const findPageByRelativePath = async (userId, relativePath) => {
const normalized = normalizeWorkspaceRelativePath2(relativePath);
if (!normalized) return null;
const [rows] = await pool2.query(
`SELECT p.*, c.category_code, pv.version_no, pr.access_mode AS pub_access_mode, pr.public_url AS pub_public_url
FROM h5_page_records p
JOIN h5_space_categories c ON c.id = p.category_id AND c.user_id = p.user_id
JOIN h5_page_versions pv ON pv.id = p.current_version_id
LEFT JOIN h5_publish_records pr ON pr.id = p.current_publish_id AND pr.status = 'online'
WHERE p.user_id = ?
AND p.status <> 'deleted'
AND JSON_UNQUOTE(JSON_EXTRACT(pv.source_snapshot_json, '$.relative_path')) = ?
ORDER BY p.updated_at DESC, p.id DESC
LIMIT 1`,
[userId, normalized]
);
return rows[0] ? pageResponse(rows[0]) : null;
};
const listPages = async (userId, filters = {}) => {
const clauses = [`p.user_id = ?`, `p.status <> 'deleted'`];
const params = [userId];
@@ -17250,9 +17407,9 @@ ${updated.content ?? ""}`,
changes
};
};
const loadPageWorkspaceContext = async (userId, pageId) => {
const loadPageWorkspaceContext = async (userId, pageId, htmlContent = null) => {
const [rows] = await pool2.query(
`SELECT pv.source_snapshot_json
`SELECT pv.source_snapshot_json, p.title
FROM h5_page_records p
JOIN h5_page_versions pv ON pv.id = p.current_version_id
WHERE p.id = ? AND p.user_id = ? AND p.status <> 'deleted'
@@ -17265,18 +17422,34 @@ ${updated.content ?? ""}`,
} catch {
snapshot = {};
}
const workspaceHtmlRelativePath = snapshot.relative_path ?? "public/index.html";
const workspacePublishDir = h5Root ? resolvePublishDir(h5Root, { id: userId }) : null;
const workspaceHtmlRelativePath = inferWorkspaceHtmlRelativePath({
snapshotRelativePath: snapshot.relative_path,
pageTitle: rows[0]?.title,
htmlContent,
publishDir: workspacePublishDir
});
return {
workspaceHtmlRelativePath,
workspacePublishDir,
publishKey: userId
};
};
const rewriteHtmlDownloadLinksForPage = async (userId, pageId, html) => {
const ctx = await loadPageWorkspaceContext(userId, pageId, html);
const { html: rewritten } = await prepareHtmlDownloadLinks(pool2, userId, html, {
htmlRelativePath: ctx.workspaceHtmlRelativePath,
publishDir: ctx.workspacePublishDir,
publishKey: ctx.publishKey,
publicBaseUrl: "",
preferAssetDownload: true
});
return rewritten;
};
const finalizeHtmlPreview = async (userId, pageId, html) => {
const shell = renderHtmlPreview(html);
try {
const ctx = await loadPageWorkspaceContext(userId, pageId);
const ctx = await loadPageWorkspaceContext(userId, pageId, html);
const { html: rewritten } = await prepareHtmlDownloadLinks(pool2, userId, shell, {
htmlRelativePath: ctx.workspaceHtmlRelativePath,
publishDir: ctx.workspacePublishDir,
@@ -17815,10 +17988,12 @@ ${updated.content ?? ""}`,
listPages,
findPageBySourceAsset,
findPageBySourceMessage,
findPageByRelativePath,
getPage,
getDeletePreview,
deletePage,
listVersions,
rewriteHtmlDownloadLinksForPage,
renderPreview: async (userId, pageId) => {
const page = await getPage(userId, pageId);
const html = page.contentFormat === "html" ? await finalizeHtmlPreview(userId, pageId, page.content) : renderPreviewHtml(page);
@@ -18090,6 +18265,33 @@ function createPageLiveEditService({ pageService, resolveUserIdForAgentSession }
};
}
// mindspace-asset-agent.mjs
function createAssetAgentService({ assetService, resolveUserIdForAgentSession }) {
return {
async applyAgentDelete(input = {}) {
const sessionId = String(input?.sessionId ?? input?.session_id ?? "").trim();
const assetId = String(input?.assetId ?? input?.asset_id ?? "").trim();
const confirmed = input?.confirmed === true || String(input?.confirmed ?? "").toLowerCase() === "true";
if (!sessionId || !assetId) {
throw Object.assign(new Error("\u7F3A\u5C11 session_id \u6216 asset_id"), { code: "invalid_request" });
}
if (!confirmed) {
throw Object.assign(new Error("\u5220\u9664\u524D\u987B\u5DF2\u5728\u5BF9\u8BDD\u4E2D\u83B7\u5F97\u7528\u6237\u786E\u8BA4"), { code: "confirmation_required" });
}
const userId = await resolveUserIdForAgentSession(sessionId);
if (!userId) {
throw Object.assign(new Error("\u65E0\u6548\u7684 Agent \u4F1A\u8BDD"), { code: "forbidden" });
}
const result = await assetService.deleteAsset(userId, assetId);
return {
assetId,
userId,
...result
};
}
};
}
// mindspace-page-edit-session.mjs
import { Agent as Agent2, fetch as undiciFetch2 } from "undici";
@@ -20530,7 +20732,8 @@ async function prepareHtmlPublishContent({
html,
ownerSlug,
urlSlug,
htmlRelativePath = `public/${urlSlug}.html`,
htmlRelativePath = null,
pageTitle = null,
absoluteStoragePath: absoluteStoragePath2,
imgproxySigner = null,
h5Root = null
@@ -20547,11 +20750,16 @@ async function prepareHtmlPublishContent({
absoluteStoragePath: absoluteStoragePath2,
imgproxySigner
});
publishContent = rewriteWorkspacePublicAssetReferences(publishContent, htmlRelativePath);
const publishDir = h5Root ? resolvePublishDir(h5Root, { id: userId }) : null;
const resolvedHtmlRelativePath = htmlRelativePath ?? inferWorkspaceHtmlRelativePath({
pageTitle,
htmlContent: publishContent,
publishDir
});
publishContent = rewriteWorkspacePublicAssetReferences(publishContent, resolvedHtmlRelativePath);
const linkIndex = await loadWorkspaceDownloadLinkIndex(pool2, userId);
publishContent = rewriteRelativeDownloadLinks(publishContent, {
htmlRelativePath,
htmlRelativePath: resolvedHtmlRelativePath,
publishDir,
linkIndex,
publishKey: userId,
@@ -20629,7 +20837,8 @@ function createPublicationService(pool2, options = {}) {
const [rows] = await pool2.query(
`SELECT p.id AS page_id, p.title, p.summary, p.page_type, p.template_id, p.current_version_id,
p.user_id, p.space_id, pv.id AS page_version_id, pv.version_no,
pv.bundle_asset_id, av.storage_key
pv.bundle_asset_id, av.storage_key,
JSON_UNQUOTE(JSON_EXTRACT(pv.source_snapshot_json, '$.relative_path')) AS source_relative_path
FROM h5_page_records p
JOIN h5_page_versions pv ON pv.page_id = p.id
JOIN h5_asset_versions av ON av.asset_id = pv.content_asset_id AND av.version_no = 1
@@ -20660,12 +20869,20 @@ function createPublicationService(pool2, options = {}) {
const preparePublishContent = async (page, ownerSlug, urlSlug) => {
let publishContent = page.content;
if (page.page_type !== "html") return publishContent;
const publishDir = h5Root ? resolvePublishDir(h5Root, { id: page.user_id }) : null;
return prepareHtmlPublishContent({
pool: pool2,
userId: page.user_id,
html: publishContent,
ownerSlug,
urlSlug,
pageTitle: page.title,
htmlRelativePath: inferWorkspaceHtmlRelativePath({
snapshotRelativePath: page.source_relative_path,
pageTitle: page.title,
htmlContent: publishContent,
publishDir
}),
absoluteStoragePath: absoluteStoragePath2,
h5Root,
imgproxySigner: imgproxySigner ? { buildUrl: (path31, preset) => imgproxySigner.buildUrl(imgproxySigner.baseUrl, path31, preset) } : null
@@ -21117,16 +21334,37 @@ ${publishContent}`, {
now
]
);
const html = await fs18.readFile(await resolveReadableStoragePath(row.storage_key), "utf8");
return {
html: await fs18.readFile(await resolveReadableStoragePath(row.storage_key), "utf8"),
html: await refreshPublishedHtmlDownloadLinks(row, html),
publication: publicationResponse({ ...row, view_count: Number(row.view_count) + 1 })
};
};
const refreshPublishedHtmlDownloadLinks = async (row, html) => {
if (!h5Root || !row?.owner_id) return html;
const source = String(html ?? "");
if (!source.trim()) return html;
const publishDir = resolvePublishDir(h5Root, { id: row.owner_id });
const linkIndex = await loadWorkspaceDownloadLinkIndex(pool2, row.owner_id);
return rewriteRelativeDownloadLinks(html, {
htmlRelativePath: inferWorkspaceHtmlRelativePath({
pageTitle: row.title,
htmlContent: html,
publishDir
}),
publishDir,
linkIndex,
publishKey: row.owner_id,
publicBaseUrl: resolvePublicBaseUrl(),
preferAssetDownload: false
}).html;
};
const resolvePublic = async (ownerSlug, urlSlug, viewerId, password, requestMeta) => {
const [rows] = await pool2.query(
`SELECT pr.*, u.id AS owner_id, av.storage_key
`SELECT pr.*, u.id AS owner_id, p.title, av.storage_key
FROM h5_publish_records pr
JOIN h5_users u ON u.id = pr.user_id
JOIN h5_page_records p ON p.id = pr.page_id
JOIN h5_page_versions pv ON pv.id = pr.page_version_id AND pv.immutable = 1
JOIN h5_asset_versions av ON av.asset_id = pv.bundle_asset_id AND av.version_no = 1
WHERE COALESCE(u.slug, u.username) = ? AND pr.url_slug = ? AND pr.status = 'online'
@@ -21139,9 +21377,10 @@ ${publishContent}`, {
const resolvePrivateLink = async (token, viewerId, requestMeta) => {
const tokenHash = crypto14.createHash("sha256").update(String(token)).digest("hex");
const [rows] = await pool2.query(
`SELECT pr.*, u.id AS owner_id, av.storage_key
`SELECT pr.*, u.id AS owner_id, p.title, av.storage_key
FROM h5_publish_records pr
JOIN h5_users u ON u.id = pr.user_id
JOIN h5_page_records p ON p.id = pr.page_id
JOIN h5_page_versions pv ON pv.id = pr.page_version_id AND pv.immutable = 1
JOIN h5_asset_versions av ON av.asset_id = pv.bundle_asset_id AND av.version_no = 1
WHERE pr.token_hash = ? AND pr.status = 'online' AND pr.access_mode = 'private_link'
@@ -26514,11 +26753,7 @@ var PUBLIC_HTML_SKIP_DIRS = /* @__PURE__ */ new Set([
"node_modules"
]);
function normalizePublicHtmlPath(relativePath) {
const parts = String(relativePath ?? "").replace(/^\/+/, "").split("/").filter((part) => part && part !== "." && part !== "..");
if (parts.length === 0) return "";
if (parts[0].toLowerCase() === "public") return parts.join("/");
if (parts.length === 1 && parts[0].toLowerCase().endsWith(".html")) return `public/${parts[0]}`;
return parts.join("/");
return normalizeWorkspaceRelativePath2(relativePath);
}
function titleFromRelativePath(relativePath) {
const basename = path27.basename(String(relativePath ?? ""), ".html");
@@ -31442,7 +31677,7 @@ function createScheduleService(pool2, options = {}) {
FROM h5_schedule_reminders r
INNER JOIN h5_schedule_items i ON i.id = r.item_id
WHERE r.user_id = ?
AND r.status IN ('pending', 'locked')
AND r.status IN ('pending', 'locked', 'sent')
AND r.remind_at >= ?
AND r.remind_at < ?
AND i.deleted_at IS NULL
@@ -31469,6 +31704,48 @@ function createScheduleService(pool2, options = {}) {
);
return rowToItem(rows[0]);
};
const getReminder = async ({ userId, reminderId }) => {
if (!userId || !reminderId) throw new Error("\u7F3A\u5C11\u63D0\u9192\u53C2\u6570");
const [rows] = await pool2.query(
`SELECT r.*,
i.title AS item_title,
i.kind AS item_kind,
i.timezone AS item_timezone,
i.start_at AS item_start_at,
i.end_at AS item_end_at
FROM h5_schedule_reminders r
INNER JOIN h5_schedule_items i ON i.id = r.item_id
WHERE r.id = ? AND r.user_id = ? AND i.deleted_at IS NULL
LIMIT 1`,
[reminderId, userId]
);
return rowToReminderWithItem(rows[0]);
};
const cancelReminder = async ({ userId, reminderId, reason = "\u7528\u6237\u5FFD\u7565" } = {}) => {
const reminder = await getReminder({ userId, reminderId });
if (!reminder) throw new Error("\u63D0\u9192\u4E0D\u5B58\u5728\u6216\u65E0\u6743\u8BBF\u95EE");
if (reminder.status === "cancelled") return reminder;
if (reminder.status === "sent") throw new Error("\u5DF2\u901A\u77E5\u7684\u63D0\u9192\u4E0D\u80FD\u5FFD\u7565");
return markReminderCancelled(reminder, reason);
};
const deleteReminder = async ({ userId, reminderId }) => {
if (!userId || !reminderId) throw new Error("\u7F3A\u5C11\u63D0\u9192\u53C2\u6570");
const [result] = await pool2.query(
`DELETE FROM h5_schedule_reminders WHERE id = ? AND user_id = ?`,
[reminderId, userId]
);
return Number(result?.affectedRows ?? 0) > 0;
};
const deleteReminders = async ({ userId, reminderIds }) => {
if (!userId) throw new Error("\u7F3A\u5C11\u7528\u6237");
const ids = [...new Set((reminderIds ?? []).map((id) => String(id ?? "").trim()).filter(Boolean))];
if (ids.length === 0) return 0;
const [result] = await pool2.query(
`DELETE FROM h5_schedule_reminders WHERE user_id = ? AND id IN (${ids.map(() => "?").join(", ")})`,
[userId, ...ids]
);
return Number(result?.affectedRows ?? 0);
};
const createReminder = async ({
userId,
itemId,
@@ -32016,6 +32293,10 @@ function createScheduleService(pool2, options = {}) {
listTodayTodoItems,
listUpcomingItems,
listUpcomingReminders,
getReminder,
cancelReminder,
deleteReminder,
deleteReminders,
listDigestSubscriptions,
createDailyTodoDigest,
createBalanceLowAlert,
@@ -33475,6 +33756,7 @@ var mindSpaceAssets = null;
var mindSpaceAudit = null;
var mindSpacePages = null;
var mindSpacePageLiveEdit = null;
var mindSpaceAssetAgent = null;
var mindSpacePageEditSession = null;
var mindSpacePublications = null;
var plazaPosts = null;
@@ -33527,15 +33809,20 @@ async function bootstrapUserAuth() {
h5Root: __dirname5,
storageRoot: process.env.MINDSPACE_STORAGE_ROOT ?? path30.join(__dirname5, "data", "mindspace")
});
const resolveUserIdForAgentSession = async (sessionId) => {
const [rows] = await pool2.query(
`SELECT user_id FROM h5_user_sessions WHERE agent_session_id = ? LIMIT 1`,
[sessionId]
);
return rows[0]?.user_id ?? null;
};
mindSpacePageLiveEdit = createPageLiveEditService({
pageService: mindSpacePages,
resolveUserIdForAgentSession: async (sessionId) => {
const [rows] = await pool2.query(
`SELECT user_id FROM h5_user_sessions WHERE agent_session_id = ? LIMIT 1`,
[sessionId]
);
return rows[0]?.user_id ?? null;
}
resolveUserIdForAgentSession
});
mindSpaceAssetAgent = createAssetAgentService({
assetService: mindSpaceAssets,
resolveUserIdForAgentSession
});
mindSpacePublications = createPublicationService(pool2, {
h5Root: __dirname5,
@@ -34805,6 +35092,7 @@ api.use(async (req, res, next) => {
if (req.path === "/status") return next();
if (req.path.startsWith("/internal/agent/")) return next();
if (req.path === "/agent/mindspace_page_patch") return next();
if (req.path === "/agent/mindspace_asset_delete") return next();
if (req.path === "/config/blocked-words") return next();
if (req.method === "GET" && /^\/mindspace\/v1\/assets\/[^/]+\/download$/.test(req.path)) {
return next();
@@ -34953,6 +35241,37 @@ api.get("/mindspace/v1/space", async (req, res) => {
if (!space) return sendError(res, req, 404, "resource_not_found", "\u7528\u6237\u7A7A\u95F4\u4E0D\u5B58\u5728");
return sendData(res, req, space);
});
api.post("/mindspace/v1/schedule/reminders/:reminderId/ignore", async (req, res) => {
if (!ensureMindSpaceEnabled(res, req) || !scheduleService) {
return sendError(res, req, 503, "feature_disabled", "\u65E5\u7A0B\u670D\u52A1\u672A\u542F\u7528");
}
try {
const reminder = await scheduleService.cancelReminder({
userId: req.currentUser.id,
reminderId: req.params.reminderId
});
return sendData(res, req, reminder);
} catch (err) {
const message = err instanceof Error ? err.message : "\u5FFD\u7565\u63D0\u9192\u5931\u8D25";
return sendError(res, req, 400, "invalid_schedule_input", message);
}
});
api.post("/mindspace/v1/schedule/reminders/bulk-delete", async (req, res) => {
if (!ensureMindSpaceEnabled(res, req) || !scheduleService) {
return sendError(res, req, 503, "feature_disabled", "\u65E5\u7A0B\u670D\u52A1\u672A\u542F\u7528");
}
try {
const ids = Array.isArray(req.body?.ids) ? req.body.ids.map(String) : [];
const deleted = await scheduleService.deleteReminders({
userId: req.currentUser.id,
reminderIds: ids
});
return sendData(res, req, { deleted });
} catch (err) {
const message = err instanceof Error ? err.message : "\u5220\u9664\u63D0\u9192\u5931\u8D25";
return sendError(res, req, 400, "invalid_schedule_input", message);
}
});
api.get("/mindspace/v1/space/quota", async (req, res) => {
if (!mindSpace) return res.status(503).json({ message: "MindSpace \u672A\u542F\u7528" });
const quota = await mindSpace.getQuota(req.currentUser.id);
@@ -35586,8 +35905,24 @@ api.post("/mindspace/v1/pages/from-asset", async (req, res) => {
req.currentUser.id,
assetId
);
if (asset.mimeType === "text/html") {
const relativePath = normalizeWorkspaceRelativePath2(
String(asset.originalFilename ?? asset.original_filename ?? "").includes("/") ? asset.originalFilename ?? asset.original_filename : `public/${asset.originalFilename ?? asset.original_filename ?? ""}`
);
const existingByPath = relativePath ? await mindSpacePages.findPageByRelativePath(req.currentUser.id, relativePath).catch(() => null) : null;
if (existingByPath) {
return sendData(res, req, {
kind: "page",
categoryCode: existingByPath.categoryCode ?? "draft",
page: existingByPath
});
}
}
const content = await fs28.promises.readFile(assetPath, "utf8");
const contentFormat = asset.mimeType === "text/html" ? "html" : "markdown";
const htmlRelativePath = contentFormat === "html" ? normalizeWorkspaceRelativePath2(
String(asset.originalFilename ?? asset.original_filename ?? "").includes("/") ? asset.originalFilename ?? asset.original_filename : `public/${asset.originalFilename ?? asset.original_filename ?? ""}`
) : null;
const page = await mindSpacePages.createFromChat(
req.currentUser.id,
{
@@ -35603,7 +35938,8 @@ api.post("/mindspace/v1/pages/from-asset", async (req, res) => {
snapshot: {
source_asset_id: asset.id,
source_category: asset.categoryCode,
content_mode: contentFormat
content_mode: contentFormat,
...htmlRelativePath ? { relative_path: htmlRelativePath } : {}
}
}
);
@@ -35659,6 +35995,14 @@ async function resolveOwnedAssistantMessage(userId, sessionId, messageId) {
}
return { session, message, content };
}
async function resolveExistingSavedPage(userId, { sessionId, messageId, relativePath } = {}) {
if (!mindSpacePages || !userId) return null;
const byMessage = await mindSpacePages.findPageBySourceMessage(userId, sessionId, messageId).catch(() => null);
if (byMessage) return byMessage;
const normalizedPath = normalizeWorkspaceRelativePath2(relativePath);
if (!normalizedPath) return null;
return mindSpacePages.findPageByRelativePath(userId, normalizedPath).catch(() => null);
}
var SAVE_TARGET_CATEGORIES = /* @__PURE__ */ new Set(["draft", "oa", "public"]);
var pageSyncInFlight = /* @__PURE__ */ new Map();
async function syncUserGeneratedPages(userId) {
@@ -35837,11 +36181,11 @@ api.post("/mindspace/v1/pages/analyze-chat-save", async (req, res) => {
thumbnailReady = false;
}
}
const existingPage = await mindSpacePages.findPageBySourceMessage(
req.currentUser.id,
const existingPage = await resolveExistingSavedPage(req.currentUser.id, {
sessionId,
messageId
).catch(() => null);
messageId,
relativePath: resolvedHtml?.relativePath ?? analysis.relativePath
});
return sendData(res, req, {
contentMode: analysis.contentMode,
links: analysis.links,
@@ -35923,7 +36267,7 @@ api.post("/mindspace/v1/pages/save-from-chat", async (req, res) => {
role: source.message.role,
content_mode: analysis.contentMode,
public_url: analysis.previewUrl,
relative_path: analysis.relativePath
relative_path: analysis.relativePath ? normalizeWorkspaceRelativePath2(analysis.relativePath) : analysis.relativePath
};
let resolvedHtml = null;
if (analysis.contentMode === "static_html") {
@@ -35994,7 +36338,12 @@ api.post("/mindspace/v1/pages/save-from-chat", async (req, res) => {
}).catch(() => {
});
}
const replacePageId = req.body?.replace_page_id ? String(req.body.replace_page_id).trim() : null;
const saveAsNew = Boolean(req.body?.save_as_new);
let replacePageId = req.body?.replace_page_id ? String(req.body.replace_page_id).trim() : null;
if (!replacePageId && !saveAsNew && analysis.contentMode === "static_html" && analysis.relativePath) {
const existingByPath = await mindSpacePages.findPageByRelativePath(req.currentUser.id, analysis.relativePath).catch(() => null);
if (existingByPath) replacePageId = existingByPath.id;
}
let page;
if (replacePageId) {
const existingPage = await mindSpacePages.getPage(req.currentUser.id, replacePageId);
@@ -36253,6 +36602,31 @@ api.post("/agent/mindspace_page_patch", async (req, res) => {
return mindSpaceError(res, req, error);
}
});
api.post("/agent/mindspace_asset_delete", async (req, res) => {
if (!mindSpaceAssetAgent) {
return res.status(503).json({ message: "MindSpace \u672A\u542F\u7528" });
}
try {
const result = await mindSpaceAssetAgent.applyAgentDelete(req.body ?? {});
await mindSpaceAudit?.write({
userId: result.userId ?? null,
action: "asset.delete",
objectType: "asset",
objectId: result.assetId,
ip: req.ip,
metadata: { via: "agent" }
});
return sendData(res, req, result);
} catch (error) {
if (error?.code === "forbidden") {
return sendError(res, req, 403, error.code, error.message);
}
if (error?.code === "invalid_request" || error?.code === "confirmation_required") {
return sendError(res, req, 400, error.code, error.message);
}
return mindSpaceError(res, req, error);
}
});
api.get("/mindspace/v1/pages/:pageId/thumbnail", async (req, res) => {
if (!mindSpacePages) return res.status(503).json({ message: "MindSpace \u672A\u542F\u7528" });
try {
@@ -36310,6 +36684,23 @@ api.post("/mindspace/v1/pages/:pageId/thumbnail/regenerate", async (req, res) =>
return mindSpaceError(res, req, error);
}
});
api.post("/mindspace/v1/pages/:pageId/rewrite-download-links", async (req, res) => {
if (!mindSpacePages) return res.status(503).json({ message: "MindSpace \u672A\u542F\u7528" });
try {
const content = String(req.body?.content ?? "").trim();
if (!content) {
throw Object.assign(new Error("\u7F3A\u5C11\u9875\u9762\u5185\u5BB9"), { code: "invalid_page_input" });
}
const html = await mindSpacePages.rewriteHtmlDownloadLinksForPage(
req.currentUser.id,
req.params.pageId,
content
);
return sendData(res, req, { html });
} catch (error) {
return mindSpaceError(res, req, error);
}
});
api.get("/mindspace/v1/pages/:pageId/preview", async (req, res) => {
if (!mindSpacePages) return res.status(503).json({ message: "MindSpace \u672A\u542F\u7528" });
try {
@@ -78,6 +78,16 @@ description: 在专属 MindSpace 目录生成可公开访问的静态 HTML 报
本地对比示例:`node scripts/thumbnail-preview-demo.mjs` → `/thumbnail-demo/`
## 平台页脚标记(必须)
页脚平台联系行**必须**使用 `data-mindspace-page-tag="platform-brand"`,且邮箱/域名只用 **tkmind.cn**(如 `contact@tkmind.cn`),**禁止** `tkmind.ai`:
```html
<p data-mindspace-page-tag="platform-brand">📧 contact@tkmind.cn</p>
```
带 `data-mindspace-page-tag` 的区域为平台固定信息:用户在编辑模式中不可见、不可改;预览与发布后正常显示。
## 附带文件下载(Word / PDF)
- 二进制文件用 `docx-generate` 脚本或平台允许的方式**单独生成**,保存到 `public/`(或 `oa/` 再复制到 `public/`)
+30
View File
@@ -25,6 +25,36 @@ export function stripMindSpaceContextPrefix(text) {
return next;
}
const ASSISTANT_DELIVERABLE_RE =
/\[[^\]]+\]\(https?:\/\/[^)]+\)|https?:\/\/(?:m\.)?[^/\s]*tkmind\.(?:cn|ai)\/MindSpace\//i;
const INTERNAL_ASSISTANT_MARKERS = [
/data-mindspace-page-tag/i,
/mindspace-cover/i,
/\bload_skill\b/i,
/static-page-publish/i,
/\.agents\/skills/i,
/SKILL\.md/i,
/注意到技能/u,
/技能更新/u,
/页脚要用.*platform-brand/u,
];
/** Agent-only process narration that should not appear in the chat UI. */
export function isInternalAssistantProcessNarration(text) {
const trimmed = String(text ?? '').trim();
if (!trimmed) return false;
if (ASSISTANT_DELIVERABLE_RE.test(trimmed)) return false;
return INTERNAL_ASSISTANT_MARKERS.some((pattern) => pattern.test(trimmed));
}
export function deriveAssistantFacingText(text) {
const trimmed = String(text ?? '').trim();
if (!trimmed) return '';
if (isInternalAssistantProcessNarration(trimmed)) return '';
return trimmed;
}
/** Strip agent-only prefixes from persisted user message content for UI display. */
export function deriveUserFacingText(text) {
let next = String(text ?? '');
+13 -1
View File
@@ -2,7 +2,7 @@ import test from 'node:test';
import assert from 'node:assert/strict';
import { buildAutoChatSkillPrefix, stripKnownChatSkillPrompt } from './chat-skills.mjs';
import { deriveUserFacingText } from './conversation-display.mjs';
import { deriveUserFacingText, deriveAssistantFacingText } from './conversation-display.mjs';
test('stripKnownChatSkillPrompt removes generate-page preface', () => {
const userText = '帮我根据这些图片生成一个主题页面,页面要做得精美一点';
@@ -24,3 +24,15 @@ test('deriveUserFacingText removes routing hint and skill preface from agent pay
].join('\n');
assert.equal(deriveUserFacingText(agentPayload), userText);
});
test('deriveAssistantFacingText hides skill/process narration without a deliverable link', () => {
const internal =
'好的,John!注意到技能更新了几个细节要求,比如页脚要用`data-mindspace-page-tag="platform-brand"`和`tkmind.cn`。我来为你重新生成全面增强版的AI机器人研究报告页面:';
assert.equal(deriveAssistantFacingText(internal), '');
});
test('deriveAssistantFacingText keeps user-facing replies with public links', () => {
const external =
'页面已生成:[AI 机器人研究报告 2026](https://m.tkmind.cn/MindSpace/john/public/ai-robot-report.html)';
assert.equal(deriveAssistantFacingText(external), external);
});
+58
View File
@@ -0,0 +1,58 @@
export function buildSelectedAssetDeleteInstructions(context) {
const selected = context.selectedAssets ?? [];
if (selected.length !== 1) return [];
const asset = selected[0];
const h5ApiBase = String(context.h5ApiBase ?? '').replace(/\/$/, '');
const sessionId = String(context.agentSessionId ?? '').trim();
if (!h5ApiBase) return [];
const sessionToken = sessionId || '<CURRENT_AGENT_SESSION>';
const sessionHint = sessionId
? ''
: '- session_id 未写入上下文时,使用当前 Agent 会话 ID 替换 `<CURRENT_AGENT_SESSION>`。\n';
return [
'【勾选资料删除(硬性)】',
`- 你对用户 MindSpace 拥有完整读写删改权限;用户已勾选「${asset.displayName}」(id: ${asset.id})。`,
'- 用户要求删除这份资料时:先说明将删除哪一份并等待用户明确确认(如「确定」「删吧」「确认删除」);**禁止**未确认就删。',
sessionHint,
'- 用户确认后,**必须立即**用 shell 执行(不要只口头说已删,也不要让用户自己去界面点删除):',
'```bash',
`curl -sS -X POST '${h5ApiBase}/api/agent/mindspace_asset_delete' \\`,
" -H 'Content-Type: application/json' \\",
` -d '{"session_id":"${sessionToken}","asset_id":"${asset.id}","confirmed":true}'`,
'```',
'- curl 成功后再回复「已删除」;若返回 asset_in_use,说明被页面引用,告知用户先删关联页面。',
'- 禁止删除未勾选的其它文件。',
'',
].filter(Boolean);
}
export function createAssetAgentService({ assetService, resolveUserIdForAgentSession }) {
return {
async applyAgentDelete(input = {}) {
const sessionId = String(input?.sessionId ?? input?.session_id ?? '').trim();
const assetId = String(input?.assetId ?? input?.asset_id ?? '').trim();
const confirmed = input?.confirmed === true || String(input?.confirmed ?? '').toLowerCase() === 'true';
if (!sessionId || !assetId) {
throw Object.assign(new Error('缺少 session_id 或 asset_id'), { code: 'invalid_request' });
}
if (!confirmed) {
throw Object.assign(new Error('删除前须已在对话中获得用户确认'), { code: 'confirmation_required' });
}
const userId = await resolveUserIdForAgentSession(sessionId);
if (!userId) {
throw Object.assign(new Error('无效的 Agent 会话'), { code: 'forbidden' });
}
const result = await assetService.deleteAsset(userId, assetId);
return {
assetId,
userId,
...result,
};
},
};
}
+49
View File
@@ -0,0 +1,49 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import {
buildSelectedAssetDeleteInstructions,
createAssetAgentService,
} from './mindspace-asset-agent.mjs';
test('buildSelectedAssetDeleteInstructions includes agent delete curl', () => {
const lines = buildSelectedAssetDeleteInstructions({
selectedAssets: [
{
id: 'asset-9',
displayName: 'sales.xlsx',
mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
},
],
h5ApiBase: 'http://127.0.0.1:8081',
agentSessionId: 'session-1',
});
const text = lines.join('\n');
assert.match(text, /完整读写删改权限/);
assert.match(text, /mindspace_asset_delete/);
assert.match(text, /asset-9/);
assert.match(text, /session-1/);
assert.match(text, /不要让用户自己去界面点删除/);
});
test('applyAgentDelete requires confirmation and valid session', async () => {
const service = createAssetAgentService({
assetService: {
deleteAsset: async () => ({ deleted: true }),
},
resolveUserIdForAgentSession: async (sessionId) =>
sessionId === 'session-1' ? 'user-1' : null,
});
await assert.rejects(
() => service.applyAgentDelete({ session_id: 'session-1', asset_id: 'asset-9' }),
(error) => error.code === 'confirmation_required',
);
const result = await service.applyAgentDelete({
session_id: 'session-1',
asset_id: 'asset-9',
confirmed: true,
});
assert.equal(result.deleted, true);
assert.equal(result.userId, 'user-1');
});
+4 -2
View File
@@ -1,4 +1,5 @@
import { buildMindSpacePageSaveInstructions } from './mindspace-page-patch.mjs';
import { buildSelectedAssetDeleteInstructions } from './mindspace-asset-agent.mjs';
const VIEW_LABELS = {
home: '空间首页',
@@ -107,7 +108,7 @@ function buildSelectedAssetInstructions(context) {
`- 用户在界面勾选了 1 份${kind}:「${asset.displayName}」(id: ${asset.id},${asset.mimeType})`,
'- 用户说「这个 / 这份 / 这篇 / 这份报告 / 这个数据 / 帮我分析 / 帮我删除」等指代词时,**必须**指这份勾选资料,不得猜测其它文件。',
`- 读取/下载:GET /api/mindspace/v1/assets/${asset.id}/download`,
`- 删除:DELETE /api/mindspace/v1/assets/${asset.id}(执行删除前必须向用户确认)`,
'- 删除:须先与用户确认,确认后由你直接执行删除(见下方「勾选资料删除」);你对该用户空间有完整读写删改权限,**禁止**推给用户自行去界面删除。',
);
if (context.h5ApiBase) {
lines.push(`- H5 API 基址:${context.h5ApiBase}`);
@@ -129,7 +130,7 @@ function buildSelectedAssetInstructions(context) {
export function buildSelectedAssetGreeting(asset) {
const kind = inferAssetKindLabel(asset);
const name = asset.displayName ?? asset.filename ?? '这份资料';
return `我看到你在界面里勾选了「${name}」。你是想查看或处理这份${kind}吗?可以直接说「帮我分析」「总结一下」或「帮我删除」——我会针对这份资料来操作。`;
return `我看到你在界面里勾选了「${name}」。你是想查看或处理这份${kind}吗?可以直接说「帮我分析」「总结一下」或「帮我删除」——确认后我可以直接在这份${kind}上操作(含删除)。`;
}
export function buildMindSpaceChatContext(input) {
@@ -362,6 +363,7 @@ export function buildContextPrefix(context) {
}
lines.push(...buildSelectedAssetInstructions(context));
lines.push(...buildSelectedAssetDeleteInstructions(context));
if (context.homeCategories?.length) {
lines.push('- 空间分类概览:');
+25 -1
View File
@@ -214,15 +214,39 @@ test('buildContextPrefix binds deictic references to a single checked asset', ()
selectedAssets: [asset],
route: '/space?category=oa',
h5ApiBase: 'http://127.0.0.1:8081',
agentSessionId: 'session-1',
}),
);
assert.match(prefix, /【界面勾选资料(硬性)】/);
assert.match(prefix, /sales\.xlsx(id: asset-9/);
assert.match(prefix, /DELETE \/api\/mindspace\/v1\/assets\/asset-9/);
assert.match(prefix, /【勾选资料删除(硬性)】/);
assert.match(prefix, /mindspace_asset_delete/);
assert.match(prefix, /完整读写删改权限/);
assert.match(prefix, /指代「这个文件」「这份报告」「这个数据」时\*\*必须\*\*指该勾选资料/);
});
test('buildContextPrefix includes delete fallback when agent session id is missing', () => {
const asset = {
id: 'asset-9',
displayName: 'sales.xlsx',
mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
};
const prefix = buildContextPrefix(
buildMindSpaceChatContext({
space: sampleSpace,
selectedCategory: sampleCategory,
selectedPageId: null,
pages: [],
assets: [asset],
selectedAssets: [asset],
route: '/space?category=oa',
h5ApiBase: 'http://127.0.0.1:8081',
}),
);
assert.match(prefix, /CURRENT_AGENT_SESSION/);
});
test('buildContextPrefix requires clarification when multiple assets are checked', () => {
const assets = [
{ id: 'asset-a', displayName: 'a.pdf', mimeType: 'application/pdf' },
+81 -11
View File
@@ -34,6 +34,57 @@ export function resolveWorkspaceRelativeFilePath(htmlRelativePath, relativeRef)
return path.posix.join(htmlDir, ref).replace(/\\/g, '/');
}
export function resolveSiblingDocxPath(htmlRelativePath) {
const htmlPath = String(htmlRelativePath ?? '').replace(/^\/+/, '');
if (!htmlPath.toLowerCase().endsWith('.html')) return null;
return htmlPath.replace(/\.html$/i, '.docx');
}
function resolveExistingSiblingDocxPath(htmlRelativePath, publishDir) {
const sibling = resolveSiblingDocxPath(htmlRelativePath);
if (!sibling || !publishDir) return null;
return workspaceFileExists(publishDir, sibling) ? sibling : null;
}
export function inferWorkspaceHtmlRelativePath({
snapshotRelativePath = null,
pageTitle = null,
htmlContent = null,
publishDir = null,
} = {}) {
const fromSnapshot = String(snapshotRelativePath ?? '').trim();
if (fromSnapshot) {
const normalized = fromSnapshot.replace(/^\/+/, '');
if (normalized.toLowerCase().startsWith('public/')) return normalized;
if (normalized.toLowerCase().endsWith('.html')) return `public/${path.posix.basename(normalized)}`;
return normalized;
}
const publicDir = publishDir ? path.join(publishDir, 'public') : null;
if (!publicDir || !fs.existsSync(publicDir) || !htmlContent) {
return 'public/index.html';
}
const normalizedContent = String(htmlContent);
const title = String(pageTitle ?? '').trim();
for (const name of fs.readdirSync(publicDir)) {
if (!name.toLowerCase().endsWith('.html')) continue;
const absolutePath = path.join(publicDir, name);
try {
const diskContent = fs.readFileSync(absolutePath, 'utf8');
if (diskContent === normalizedContent) return `public/${name}`;
if (title) {
const titleMatch = diskContent.match(/<title[^>]*>([^<]+)<\/title>/i);
if (titleMatch?.[1]?.trim() === title) return `public/${name}`;
}
} catch {
// ignore unreadable files
}
}
return 'public/index.html';
}
export function buildWorkspaceDownloadLinkIndex(assets = []) {
const byBasename = new Map();
const byPath = new Map();
@@ -45,6 +96,8 @@ export function buildWorkspaceDownloadLinkIndex(assets = []) {
byBasename.set(path.posix.basename(filename), id);
if (filename.startsWith('public/')) {
byPath.set(filename.slice('public/'.length), id);
} else if (!filename.includes('/')) {
byPath.set(`public/${filename}`, id);
}
}
return { byBasename, byPath };
@@ -80,6 +133,20 @@ function workspaceFileExists(publishDir, workspaceRelativePath) {
}
}
function collectDownloadWorkspaceCandidates(htmlRelativePath, relativeRef, publishDir) {
const workspacePath = resolveWorkspaceRelativeFilePath(htmlRelativePath, relativeRef);
const candidates = new Set([
workspacePath,
path.posix.join('public', path.posix.basename(workspacePath)),
]);
const siblingDocx = resolveSiblingDocxPath(htmlRelativePath);
if (siblingDocx) {
candidates.add(siblingDocx);
candidates.add(path.posix.join('public', path.posix.basename(siblingDocx)));
}
return [...candidates];
}
export function resolveDownloadTargetUrl({
relativeRef,
htmlRelativePath = 'public/index.html',
@@ -90,27 +157,28 @@ export function resolveDownloadTargetUrl({
preferAssetDownload = true,
}) {
const { suffix } = splitReferenceParts(relativeRef);
const workspacePath = resolveWorkspaceRelativeFilePath(htmlRelativePath, relativeRef);
const assetId = lookupAssetIdForWorkspacePath(linkIndex, workspacePath);
const publicCandidates = [
workspacePath,
path.posix.join('public', path.posix.basename(workspacePath)),
];
const candidates = collectDownloadWorkspaceCandidates(htmlRelativePath, relativeRef, publishDir);
if (preferAssetDownload && assetId) {
return `${buildAssetDownloadUrl(assetId)}${suffix}`;
for (const candidate of candidates) {
const assetId = lookupAssetIdForWorkspacePath(linkIndex, candidate);
if (preferAssetDownload && assetId) {
return `${buildAssetDownloadUrl(assetId)}${suffix}`;
}
}
if (publishDir && publishKey) {
for (const candidate of publicCandidates) {
for (const candidate of candidates) {
if (workspaceFileExists(publishDir, candidate)) {
return `${buildPublicWorkspaceFileUrl(publicBaseUrl, publishKey, candidate)}${suffix}`;
}
}
}
if (assetId) {
return `${buildAssetDownloadUrl(assetId)}${suffix}`;
for (const candidate of candidates) {
const assetId = lookupAssetIdForWorkspacePath(linkIndex, candidate);
if (assetId) {
return `${buildAssetDownloadUrl(assetId)}${suffix}`;
}
}
return null;
@@ -150,6 +218,8 @@ export async function prepareHtmlDownloadLinks(pool, userId, html, options = {})
export const downloadLinkInternals = {
isRelativeDownloadReference,
resolveWorkspaceRelativeFilePath,
resolveSiblingDocxPath,
inferWorkspaceHtmlRelativePath,
buildWorkspaceDownloadLinkIndex,
lookupAssetIdForWorkspacePath,
resolveDownloadTargetUrl,
+51 -3
View File
@@ -1,4 +1,7 @@
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import { downloadLinkInternals } from './mindspace-html-download-links.mjs';
@@ -8,6 +11,7 @@ const {
buildWorkspaceDownloadLinkIndex,
resolveDownloadTargetUrl,
rewriteRelativeDownloadLinks,
inferWorkspaceHtmlRelativePath,
} = downloadLinkInternals;
test('isRelativeDownloadReference accepts sibling docx links only', () => {
@@ -31,17 +35,38 @@ test('resolveWorkspaceRelativeFilePath resolves from public html directory', ()
test('resolveDownloadTargetUrl prefers asset download API in preview mode', () => {
const linkIndex = buildWorkspaceDownloadLinkIndex([
{ id: 'asset-1', original_filename: 'public/半导体行业趋势分析报告.docx' },
{ id: 'asset-1', original_filename: 'ai-robot-report.docx' },
]);
const url = resolveDownloadTargetUrl({
relativeRef: '半导体行业趋势分析报告.docx',
htmlRelativePath: 'public/index.html',
relativeRef: 'AI机器人研究报告.docx',
htmlRelativePath: 'public/ai-robot-report.html',
linkIndex,
preferAssetDownload: true,
});
assert.equal(url, '/api/mindspace/v1/assets/asset-1/download');
});
test('resolveDownloadTargetUrl falls back to sibling docx beside html page', () => {
const publishDir = fs.mkdtempSync(path.join(os.tmpdir(), 'download-links-'));
const publicDir = path.join(publishDir, 'public');
fs.mkdirSync(publicDir, { recursive: true });
fs.writeFileSync(path.join(publicDir, 'ai-robot-report.html'), '<html></html>');
fs.writeFileSync(path.join(publicDir, 'ai-robot-report.docx'), 'docx');
try {
const url = resolveDownloadTargetUrl({
relativeRef: 'AI机器人研究报告.docx',
htmlRelativePath: 'public/ai-robot-report.html',
publishDir,
publishKey: 'user-1',
publicBaseUrl: 'https://m.tkmind.cn',
preferAssetDownload: false,
});
assert.match(url, /MindSpace\/user-1\/public\/ai-robot-report\.docx$/);
} finally {
fs.rmSync(publishDir, { recursive: true, force: true });
}
});
test('rewriteRelativeDownloadLinks rewrites href and src download attributes', () => {
const html = [
'<a href="半导体行业趋势分析报告.docx" download>下载</a>',
@@ -59,3 +84,26 @@ test('rewriteRelativeDownloadLinks rewrites href and src download attributes', (
assert.match(rewritten, /href="\/api\/mindspace\/v1\/assets\/asset-1\/download"/);
assert.match(rewritten, /href="#summary"/);
});
test('inferWorkspaceHtmlRelativePath matches workspace html by title', () => {
const publishDir = fs.mkdtempSync(path.join(os.tmpdir(), 'infer-html-path-'));
const publicDir = path.join(publishDir, 'public');
fs.mkdirSync(publicDir, { recursive: true });
fs.writeFileSync(
path.join(publicDir, 'ai-robot-report.html'),
'<html><head><title>AI 机器人研究报告 2026</title></head><body></body></html>',
'utf8',
);
try {
assert.equal(
inferWorkspaceHtmlRelativePath({
pageTitle: 'AI 机器人研究报告 2026',
htmlContent: '<html><head><title>AI 机器人研究报告 2026</title></head><body></body></html>',
publishDir,
}),
'public/ai-robot-report.html',
);
} finally {
fs.rmSync(publishDir, { recursive: true, force: true });
}
});
+215 -41
View File
@@ -3,9 +3,13 @@
//
// Source of truth for the cover is the page's own <head> (mindspace-cover JSON /
// <meta name="description"> / hero <img>), reused via extractCoverSignals. Authors who
// already wrote their own og:image are left untouched.
// already wrote their own og:image are left untouched; missing site_name / description /
// brand icon are still backfilled.
import { extractCoverSignals } from './mindspace-thumbnails.mjs';
export const PLATFORM_SITE_NAME = 'TKMind 智趣';
export const PLATFORM_BRAND_ICON_PATH = '/brand/tkmind-icon.png';
function rawTitleFromHtml(html) {
// Keep the original <title> verbatim (emoji included) for og:title.
return String(html).match(/<title[^>]*>([^<]*)<\/title>/i)?.[1]?.trim() ?? '';
@@ -45,48 +49,151 @@ function rawMetaContent(html, attr, name) {
return match?.[1] || match?.[2] || '';
}
function hasMeta(html, attr, name) {
return new RegExp(`<meta[^>]+${attr}=["']${name}["']`, 'i').test(String(html));
}
function hasLinkRel(html, rel) {
return new RegExp(`<link[^>]+rel=["'][^"']*\\b${rel}\\b[^"']*["']`, 'i').test(String(html));
}
function escapeScriptString(value) {
return JSON.stringify(String(value ?? '')).replace(/</g, '\\u003c');
}
function appendBeforeHeadClose(html, block) {
const source = String(html);
if (!block) return source;
if (/<\/head>/i.test(source)) {
return source.replace(/<\/head>/i, `${block}\n</head>`);
}
if (/<head[^>]*>/i.test(source)) {
return source.replace(/(<head[^>]*>)/i, `$1${block}`);
}
return `${block}\n${source}`;
}
function resolveShareDescription(html, { siteName = PLATFORM_SITE_NAME, meta = {} } = {}) {
return (
rawMetaContent(html, 'property', 'og:description') ||
rawMetaContent(html, 'name', 'description') ||
extractCoverSignals(html, meta).subtitle ||
`${siteName} · 精选作品`
);
}
function resolveShareImageUrl(html, { origin, pageDirUrl, fallbackImageUrl = '', meta = {} } = {}) {
const existing = rawMetaContent(html, 'property', 'og:image');
if (existing) return existing;
const signals = extractCoverSignals(html, meta);
return resolveImageUrl(signals.image, { origin, pageDirUrl }) || fallbackImageUrl || '';
}
/**
* Extract the share-card fields WeChat / IM clients consume from HTML.
*/
export function extractSharePreviewMeta(
html,
{
origin = '',
pageUrl = '',
pageDirUrl = '',
fallbackImageUrl = '',
siteName = PLATFORM_SITE_NAME,
brandIconUrl = '',
meta = {},
} = {},
) {
const source = String(html ?? '');
const title = rawMetaContent(source, 'property', 'og:title') || rawTitleFromHtml(source) || 'MindSpace 页面';
const description = resolveShareDescription(source, { siteName, meta });
const imageUrl = resolveShareImageUrl(source, { origin, pageDirUrl, fallbackImageUrl, meta });
const resolvedSiteName = rawMetaContent(source, 'property', 'og:site_name') || siteName;
const iconUrl = brandIconUrl || (origin ? `${origin}${PLATFORM_BRAND_ICON_PATH}` : PLATFORM_BRAND_ICON_PATH);
return {
title,
description,
imageUrl,
siteName: resolvedSiteName,
iconUrl,
pageUrl,
};
}
/**
* Inject og:/twitter: meta into an HTML document.
* @param {string} html raw page HTML
* @param {{ origin: string, pageUrl: string, pageDirUrl: string, fallbackImageUrl?: string }} ctx
* origin: https://host pageUrl: canonical page URL pageDirUrl: page directory URL (trailing '/')
* fallbackImageUrl: absolute raster URL to use when the page has no cover of its own
* @param {{ origin: string, pageUrl: string, pageDirUrl: string, fallbackImageUrl?: string, siteName?: string, brandIconUrl?: string, meta?: object }} ctx
* @returns {string} HTML with tags injected (or unchanged when not applicable)
*/
export function injectOgTags(html, { origin, pageUrl, pageDirUrl, fallbackImageUrl = '' }) {
export function injectOgTags(
html,
{
origin,
pageUrl,
pageDirUrl,
fallbackImageUrl = '',
siteName = PLATFORM_SITE_NAME,
brandIconUrl = '',
meta = {},
} = {},
) {
const source = String(html);
// Respect a page that already declares its own Open Graph image.
if (/<meta[^>]+property=["']og:image["']/i.test(source)) return source;
const preview = extractSharePreviewMeta(source, {
origin,
pageUrl,
pageDirUrl,
fallbackImageUrl,
siteName,
brandIconUrl,
meta,
});
const signals = extractCoverSignals(source);
const title = rawTitleFromHtml(source) || signals.title;
const description = signals.subtitle || '';
const imageUrl = resolveImageUrl(signals.image, { origin, pageDirUrl }) || fallbackImageUrl || null;
const tags = [
'<meta property="og:type" content="article">',
pageUrl ? `<meta property="og:url" content="${escapeAttr(pageUrl)}">` : '',
title ? `<meta property="og:title" content="${escapeAttr(title)}">` : '',
description ? `<meta property="og:description" content="${escapeAttr(description)}">` : '',
imageUrl ? `<meta property="og:image" content="${escapeAttr(imageUrl)}">` : '',
`<meta name="twitter:card" content="${imageUrl ? 'summary_large_image' : 'summary'}">`,
title ? `<meta name="twitter:title" content="${escapeAttr(title)}">` : '',
description ? `<meta name="twitter:description" content="${escapeAttr(description)}">` : '',
imageUrl ? `<meta name="twitter:image" content="${escapeAttr(imageUrl)}">` : '',
].filter(Boolean);
const block = `\n${tags.map((t) => ` ${t}`).join('\n')}\n`;
if (/<\/head>/i.test(source)) {
return source.replace(/<\/head>/i, `${block}</head>`);
const tags = [];
if (!hasMeta(source, 'property', 'og:type')) {
tags.push('<meta property="og:type" content="article">');
}
if (/<head[^>]*>/i.test(source)) {
return source.replace(/(<head[^>]*>)/i, `$1${block}`);
if (pageUrl && !hasMeta(source, 'property', 'og:url')) {
tags.push(`<meta property="og:url" content="${escapeAttr(pageUrl)}">`);
}
return source;
if (preview.title && !hasMeta(source, 'property', 'og:title')) {
tags.push(`<meta property="og:title" content="${escapeAttr(preview.title)}">`);
}
if (preview.description && !hasMeta(source, 'property', 'og:description')) {
tags.push(`<meta property="og:description" content="${escapeAttr(preview.description)}">`);
}
if (preview.imageUrl && !hasMeta(source, 'property', 'og:image')) {
tags.push(`<meta property="og:image" content="${escapeAttr(preview.imageUrl)}">`);
}
if (preview.siteName && !hasMeta(source, 'property', 'og:site_name')) {
tags.push(`<meta property="og:site_name" content="${escapeAttr(preview.siteName)}">`);
}
const twitterCard = preview.imageUrl ? 'summary_large_image' : 'summary';
if (!hasMeta(source, 'name', 'twitter:card')) {
tags.push(`<meta name="twitter:card" content="${twitterCard}">`);
}
if (preview.title && !hasMeta(source, 'name', 'twitter:title')) {
tags.push(`<meta name="twitter:title" content="${escapeAttr(preview.title)}">`);
}
if (preview.description && !hasMeta(source, 'name', 'twitter:description')) {
tags.push(`<meta name="twitter:description" content="${escapeAttr(preview.description)}">`);
}
if (preview.imageUrl && !hasMeta(source, 'name', 'twitter:image')) {
tags.push(`<meta name="twitter:image" content="${escapeAttr(preview.imageUrl)}">`);
}
const links = [];
if (preview.iconUrl && !hasLinkRel(source, 'icon')) {
links.push(`<link rel="icon" type="image/png" href="${escapeAttr(preview.iconUrl)}">`);
}
if (preview.iconUrl && !hasLinkRel(source, 'apple-touch-icon')) {
links.push(`<link rel="apple-touch-icon" href="${escapeAttr(preview.iconUrl)}">`);
}
const block = [...tags, ...links].map((t) => ` ${t}`).join('\n');
if (!block) return source;
return appendBeforeHeadClose(source, `\n${block}\n`);
}
export function injectWechatShareBridge(
@@ -94,6 +201,7 @@ export function injectWechatShareBridge(
{
pageUrl,
signatureEndpoint = '/auth/wechat/public-js-sdk-signature',
siteName = PLATFORM_SITE_NAME,
} = {},
) {
const source = String(html ?? '');
@@ -101,10 +209,8 @@ export function injectWechatShareBridge(
if (source.includes('data-tkmind-wechat-share="1"')) return source;
const title = rawMetaContent(source, 'property', 'og:title') || rawTitleFromHtml(source);
const description =
rawMetaContent(source, 'property', 'og:description') ||
rawMetaContent(source, 'name', 'description') ||
'';
const resolvedSiteName = rawMetaContent(source, 'property', 'og:site_name') || siteName;
const description = resolveShareDescription(source, { siteName: resolvedSiteName });
const imageUrl = rawMetaContent(source, 'property', 'og:image') || '';
const script = `
@@ -167,11 +273,79 @@ export function injectWechatShareBridge(
})();
</script>`;
if (/<\/head>/i.test(source)) {
return source.replace(/<\/head>/i, `${script}\n</head>`);
}
if (/<head[^>]*>/i.test(source)) {
return source.replace(/(<head[^>]*>)/i, `$1${script}`);
}
return `${script}\n${source}`;
return appendBeforeHeadClose(source, script);
}
/**
* Render a lightweight WeChat-style share card for local preview.
*/
export function renderWechatSharePreviewHtml(preview, { note = '' } = {}) {
const safe = (value) =>
String(value ?? '')
.replaceAll('&', '&amp;')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
.replaceAll('"', '&quot;');
const title = safe(preview.title || '页面标题');
const description = safe(preview.description || '');
const siteName = safe(preview.siteName || PLATFORM_SITE_NAME);
const imageUrl = safe(preview.imageUrl || '');
const iconUrl = safe(preview.iconUrl || PLATFORM_BRAND_ICON_PATH);
const pageUrl = safe(preview.pageUrl || '');
const noteHtml = note ? `<p class="note">${safe(note)}</p>` : '';
return `<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>微信分享预览 · ${title}</title>
<style>
* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; padding: 24px; background: #ececec; font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, sans-serif; color: #111; }
.wrap { max-width: 420px; margin: 0 auto; }
h1 { margin: 0 0 8px; font-size: 18px; }
.hint, .note { margin: 0 0 16px; color: #666; font-size: 13px; line-height: 1.6; }
.card { display: grid; grid-template-columns: 1fr 72px; gap: 12px; padding: 12px; border-radius: 8px; background: #fff; box-shadow: 0 1px 0 rgba(0,0,0,.06); }
.card-main { min-width: 0; }
.card-title { margin: 0; font-size: 16px; font-weight: 600; line-height: 1.35; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card-desc { margin: 6px 0 0; color: #888; font-size: 13px; line-height: 1.45; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card-foot { display: flex; align-items: center; gap: 6px; margin-top: 10px; color: #999; font-size: 12px; }
.card-foot img { width: 16px; height: 16px; border-radius: 999px; object-fit: cover; }
.card-thumb { width: 72px; height: 72px; border-radius: 4px; object-fit: cover; background: #f3f3f3; }
.card-thumb-empty { display: grid; place-items: center; color: #bbb; font-size: 12px; }
.meta { margin-top: 18px; padding: 14px; border-radius: 10px; background: rgba(255,255,255,.72); font-size: 12px; line-height: 1.7; word-break: break-all; }
code { background: rgba(0,0,0,.06); padding: 1px 4px; border-radius: 4px; }
</style>
</head>
<body>
<div class="wrap">
<h1>微信链接卡片预览</h1>
<p class="hint">本地模拟的是「粘贴链接后看到的卡片」,不是 JS-SDK 内部分享弹层。${pageUrl ? `源链接:<code>${pageUrl}</code>` : ''}</p>
${noteHtml}
<article class="card" aria-label="微信分享卡片预览">
<div class="card-main">
<h2 class="card-title">${title}</h2>
${description ? `<p class="card-desc">${description}</p>` : ''}
<div class="card-foot">
<img src="${iconUrl}" alt="" onerror="this.style.display='none'">
<span>${siteName}</span>
</div>
</div>
${
imageUrl
? `<img class="card-thumb" src="${imageUrl}" alt="">`
: `<div class="card-thumb card-thumb-empty">无图</div>`
}
</article>
<div class="meta">
<div><strong>og:title</strong> ${title}</div>
<div><strong>og:description</strong> ${description || '(空)'}</div>
<div><strong>og:site_name</strong> ${siteName}</div>
<div><strong>og:image</strong> ${imageUrl || '(空)'}</div>
<div><strong>icon</strong> ${iconUrl}</div>
</div>
</div>
</body>
</html>`;
}
+58 -3
View File
@@ -1,6 +1,12 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { injectOgTags, injectWechatShareBridge } from './mindspace-og-tags.mjs';
import {
extractSharePreviewMeta,
injectOgTags,
injectWechatShareBridge,
PLATFORM_SITE_NAME,
renderWechatSharePreviewHtml,
} from './mindspace-og-tags.mjs';
const ctx = {
origin: 'https://m.tkmind.cn',
@@ -16,8 +22,9 @@ test('injects og/twitter tags with absolute image from a relative cover', () =>
assert.match(out, /<meta property="og:title" content="🚀 太空小勇士 - 闯关大冒险">/);
assert.match(out, /<meta property="og:description" content="一起闯关大冒险">/);
assert.match(out, /<meta property="og:image" content="https:\/\/m\.tkmind\.cn\/MindSpace\/john\/public\/assets\/hero\.jpg">/);
assert.match(out, /<meta property="og:site_name" content="TKMind 智趣">/);
assert.match(out, /<link rel="icon" type="image\/png" href="https:\/\/m\.tkmind\.cn\/brand\/tkmind-icon\.png">/);
assert.match(out, /<meta name="twitter:card" content="summary_large_image">/);
// injected before </head>
assert.ok(out.indexOf('og:image') < out.indexOf('</head>'));
});
@@ -28,6 +35,7 @@ test('falls back to the thumbnail png when the page has no cover of its own', ()
fallbackImageUrl: 'https://m.tkmind.cn/MindSpace/john/public/space.thumbnail.png',
});
assert.match(out, /<meta property="og:image" content="https:\/\/m\.tkmind\.cn\/MindSpace\/john\/public\/space\.thumbnail\.png">/);
assert.match(out, /<meta property="og:site_name" content="TKMind 智趣">/);
assert.match(out, /<meta name="twitter:card" content="summary_large_image">/);
});
@@ -38,10 +46,11 @@ test('prefers the page cover over the thumbnail fallback', () => {
assert.doesNotMatch(out, /thumbnail\.png/);
});
test('keeps an author-provided og:image and does not duplicate', () => {
test('keeps an author-provided og:image and still backfills site_name', () => {
const html = `<head><title>T</title><meta property="og:image" content="https://x/y.png"></head>`;
const out = injectOgTags(html, ctx);
assert.equal((out.match(/og:image/g) ?? []).length, 1);
assert.match(out, /<meta property="og:site_name" content="TKMind 智趣">/);
});
test('skips og:image for svg-only cover, falls back to summary card', () => {
@@ -49,6 +58,7 @@ test('skips og:image for svg-only cover, falls back to summary card', () => {
const out = injectOgTags(html, ctx);
assert.doesNotMatch(out, /og:image/);
assert.match(out, /<meta name="twitter:card" content="summary">/);
assert.match(out, /<meta property="og:site_name" content="TKMind 智趣">/);
});
test('passes through a full https cover url unchanged', () => {
@@ -69,10 +79,49 @@ test('escapes quotes and ampersands in title to keep the meta tag well-formed',
assert.match(out, /<meta property="og:title" content="say &quot;hi&quot; &amp; bye">/);
});
test('uses meta override for shell pages that only expose a title', () => {
const shell = `<head><title>连云港三天两夜攻略 🌊</title></head>`;
const out = injectOgTags(shell, {
...ctx,
fallbackImageUrl: 'https://m.tkmind.cn/MindSpace/john/public/demo.thumbnail.png',
meta: { subtitle: '三天两夜·沙滩酒店·必吃美食' },
});
assert.match(out, /<meta property="og:description" content="三天两夜·沙滩酒店·必吃美食">/);
assert.match(out, /<meta property="og:site_name" content="TKMind 智趣">/);
});
test('extractSharePreviewMeta returns the card fields used by preview tooling', () => {
const html = injectOgTags(
`<head><title>标题</title><meta name="description" content="描述"></head>`,
ctx,
);
const preview = extractSharePreviewMeta(html, ctx);
assert.equal(preview.title, '标题');
assert.equal(preview.description, '描述');
assert.equal(preview.siteName, PLATFORM_SITE_NAME);
assert.match(preview.iconUrl, /\/brand\/tkmind-icon\.png$/);
});
test('renderWechatSharePreviewHtml includes title, description and site footer', () => {
const html = renderWechatSharePreviewHtml({
title: '连云港三天两夜攻略 🌊',
description: '三天两夜·沙滩酒店·必吃美食',
siteName: PLATFORM_SITE_NAME,
imageUrl: 'https://example.com/cover.png',
iconUrl: '/brand/tkmind-icon.png',
pageUrl: ctx.pageUrl,
});
assert.match(html, /连云港三天两夜攻略/);
assert.match(html, /三天两夜·沙滩酒店·必吃美食/);
assert.match(html, /TKMind 智趣/);
assert.match(html, /example\.com\/cover\.png/);
});
test('injectWechatShareBridge adds WeChat share bootstrap with og-derived metadata', () => {
const html = `<html><head><title>页面标题</title>` +
`<meta property="og:title" content="分享标题">` +
`<meta property="og:description" content="分享描述">` +
`<meta property="og:site_name" content="TKMind 智趣">` +
`<meta property="og:image" content="https://g2.tkmind.cn/hero.png"></head><body></body></html>`;
const out = injectWechatShareBridge(html, {
pageUrl: 'https://m.tkmind.cn/u/john/pages/demo',
@@ -85,6 +134,12 @@ test('injectWechatShareBridge adds WeChat share bootstrap with og-derived metada
assert.match(out, /分享描述/);
});
test('injectWechatShareBridge falls back description when og tags are missing', () => {
const html = `<head><title>页面标题</title></head>`;
const out = injectWechatShareBridge(html, { pageUrl: 'https://m.tkmind.cn/u/john/pages/demo' });
assert.match(out, /TKMind 智趣 · 精选作品/);
});
test('injectWechatShareBridge is idempotent', () => {
const html = `<head><title>X</title></head>`;
const first = injectWechatShareBridge(html, { pageUrl: 'https://m.tkmind.cn/u/john/pages/demo' });
+2 -8
View File
@@ -1,5 +1,6 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { normalizeWorkspaceRelativePath } from './mindspace-pages.mjs';
const PUBLIC_HTML_SKIP_DIRS = new Set([
'.tmp-images',
@@ -10,14 +11,7 @@ const PUBLIC_HTML_SKIP_DIRS = new Set([
]);
function normalizePublicHtmlPath(relativePath) {
const parts = String(relativePath ?? '')
.replace(/^\/+/, '')
.split('/')
.filter((part) => part && part !== '.' && part !== '..');
if (parts.length === 0) return '';
if (parts[0].toLowerCase() === 'public') return parts.join('/');
if (parts.length === 1 && parts[0].toLowerCase().endsWith('.html')) return `public/${parts[0]}`;
return parts.join('/');
return normalizeWorkspaceRelativePath(relativePath);
}
function titleFromRelativePath(relativePath) {
+35
View File
@@ -0,0 +1,35 @@
export const MINDSPACE_PAGE_TAG_ATTR = 'data-mindspace-page-tag';
export const MINDSPACE_PAGE_TAG_PLATFORM_BRAND = 'platform-brand';
export const PLATFORM_CONTACT_EMAIL = 'contact@tkmind.cn';
const PLATFORM_BRAND_INNER_RE = /(?:contact@tkmind\.(?:ai|cn)|📧[^<]*tkmind)/i;
const SIMPLE_BRAND_ELEMENT_RE =
/<(p|div|span|small|footer|a)(\s[^>]*)?>([^<]*(?:contact@tkmind\.(?:ai|cn)|📧[^<]*tkmind)[^<]*)<\/\1>/gi;
export function normalizePlatformDomainText(text) {
return String(text ?? '').replace(/tkmind\.ai/gi, 'tkmind.cn');
}
export function normalizePlatformBrandHtml(html) {
let next = normalizePlatformDomainText(html);
next = next.replace(/mailto:([^"'>\s]+@tkmind)\.ai/gi, 'mailto:$1.cn');
next = next.replace(/https?:\/\/([a-z0-9.-]*\.)?tkmind\.ai/gi, (match) =>
match.replace(/tkmind\.ai/gi, 'tkmind.cn'),
);
return next;
}
export function ensurePlatformBrandPageTags(html) {
const normalized = normalizePlatformBrandHtml(html);
return normalized.replace(SIMPLE_BRAND_ELEMENT_RE, (match, tag, attrs, inner) => {
const attrStr = attrs ?? '';
if (attrStr.includes(MINDSPACE_PAGE_TAG_ATTR)) return match;
if (!PLATFORM_BRAND_INNER_RE.test(inner)) return match;
const spacer = attrStr ? '' : ' ';
return `<${tag}${attrStr}${spacer}${MINDSPACE_PAGE_TAG_ATTR}="${MINDSPACE_PAGE_TAG_PLATFORM_BRAND}">${inner}</${tag}>`;
});
}
export function prepareHtmlPageBrandMarkers(html) {
return ensurePlatformBrandPageTags(html);
}
+29
View File
@@ -0,0 +1,29 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import {
MINDSPACE_PAGE_TAG_ATTR,
MINDSPACE_PAGE_TAG_PLATFORM_BRAND,
ensurePlatformBrandPageTags,
normalizePlatformBrandHtml,
prepareHtmlPageBrandMarkers,
} from './mindspace-page-tag.mjs';
test('normalizePlatformBrandHtml rewrites tkmind.ai to tkmind.cn', () => {
const html = '<a href="mailto:contact@tkmind.ai">📧 contact@tkmind.ai</a>';
const result = normalizePlatformBrandHtml(html);
assert.match(result, /contact@tkmind\.cn/);
assert.doesNotMatch(result, /tkmind\.ai/i);
});
test('ensurePlatformBrandPageTags adds platform-brand marker to contact line', () => {
const html = '<footer><p>📧 contact@tkmind.ai</p></footer>';
const result = ensurePlatformBrandPageTags(html);
assert.match(result, new RegExp(`${MINDSPACE_PAGE_TAG_ATTR}="${MINDSPACE_PAGE_TAG_PLATFORM_BRAND}"`));
assert.match(result, /contact@tkmind\.cn/);
});
test('prepareHtmlPageBrandMarkers keeps existing marker', () => {
const html = `<p ${MINDSPACE_PAGE_TAG_ATTR}="${MINDSPACE_PAGE_TAG_PLATFORM_BRAND}">📧 contact@tkmind.cn</p>`;
const result = prepareHtmlPageBrandMarkers(html);
assert.equal(result.match(new RegExp(MINDSPACE_PAGE_TAG_ATTR, 'g'))?.length, 1);
});
+61 -7
View File
@@ -12,7 +12,8 @@ import {
bufferToImageDataUri,
} from './mindspace-thumbnails.mjs';
import { resolvePublishDir } from './user-publish.mjs';
import { prepareHtmlDownloadLinks } from './mindspace-html-download-links.mjs';
import { prepareHtmlDownloadLinks, inferWorkspaceHtmlRelativePath } from './mindspace-html-download-links.mjs';
import { prepareHtmlPageBrandMarkers } from './mindspace-page-tag.mjs';
import { purgeWorkspacePageArtifacts, extractAssetIdsFromHtml } from './mindspace-page-purge.mjs';
import { ensureWorkspaceHtmlThumbnail, workspaceThumbnailRelativePath } from './mindspace-workspace-thumbnails.mjs';
import { upsertMindspaceCoverMeta } from './mindspace-cover-meta.mjs';
@@ -43,6 +44,18 @@ function pageError(message, code, details) {
return Object.assign(new Error(message), { code, details });
}
/** Normalize workspace HTML paths so `demo.html` and `public/demo.html` match. */
export function normalizeWorkspaceRelativePath(relativePath) {
const parts = String(relativePath ?? '')
.replace(/^\/+/, '')
.split('/')
.filter((part) => part && part !== '.' && part !== '..');
if (parts.length === 0) return '';
if (parts[0].toLowerCase() === 'public') return parts.join('/');
if (parts.length === 1 && parts[0].toLowerCase().endsWith('.html')) return `public/${parts[0]}`;
return parts.join('/');
}
function normalizeText(value, maxLength, fieldName) {
const text = String(value ?? '').normalize('NFKC').trim();
if (!text) throw pageError(`${fieldName}不能为空`, 'invalid_page_input');
@@ -54,13 +67,16 @@ function normalizeText(value, maxLength, fieldName) {
function normalizePageInput(input) {
const title = normalizeText(input.title, MAX_TITLE_LENGTH, '标题');
const content = normalizeText(input.content, MAX_CONTENT_BYTES, '页面内容');
let content = normalizeText(input.content, MAX_CONTENT_BYTES, '页面内容');
const contentFormat = input.contentFormat === 'html' ? 'html' : 'markdown';
if (contentFormat === 'html') {
content = prepareHtmlPageBrandMarkers(content);
}
const contentBytes = Buffer.byteLength(content, 'utf8');
if (contentBytes > MAX_CONTENT_BYTES) {
throw pageError('页面内容超过 1 MB 限制', 'page_content_too_large');
}
const summary = String(input.summary ?? '').normalize('NFKC').trim().slice(0, MAX_SUMMARY_LENGTH);
const contentFormat = input.contentFormat === 'html' ? 'html' : 'markdown';
const templateId =
contentFormat === 'html'
? 'static-html'
@@ -579,6 +595,25 @@ export function createPageService(pool, options = {}) {
return rows[0] ? pageResponse(rows[0]) : null;
};
const findPageByRelativePath = async (userId, relativePath) => {
const normalized = normalizeWorkspaceRelativePath(relativePath);
if (!normalized) return null;
const [rows] = await pool.query(
`SELECT p.*, c.category_code, pv.version_no, pr.access_mode AS pub_access_mode, pr.public_url AS pub_public_url
FROM h5_page_records p
JOIN h5_space_categories c ON c.id = p.category_id AND c.user_id = p.user_id
JOIN h5_page_versions pv ON pv.id = p.current_version_id
LEFT JOIN h5_publish_records pr ON pr.id = p.current_publish_id AND pr.status = 'online'
WHERE p.user_id = ?
AND p.status <> 'deleted'
AND JSON_UNQUOTE(JSON_EXTRACT(pv.source_snapshot_json, '$.relative_path')) = ?
ORDER BY p.updated_at DESC, p.id DESC
LIMIT 1`,
[userId, normalized],
);
return rows[0] ? pageResponse(rows[0]) : null;
};
const listPages = async (userId, filters = {}) => {
const clauses = [`p.user_id = ?`, `p.status <> 'deleted'`];
const params = [userId];
@@ -729,9 +764,9 @@ export function createPageService(pool, options = {}) {
};
};
const loadPageWorkspaceContext = async (userId, pageId) => {
const loadPageWorkspaceContext = async (userId, pageId, htmlContent = null) => {
const [rows] = await pool.query(
`SELECT pv.source_snapshot_json
`SELECT pv.source_snapshot_json, p.title
FROM h5_page_records p
JOIN h5_page_versions pv ON pv.id = p.current_version_id
WHERE p.id = ? AND p.user_id = ? AND p.status <> 'deleted'
@@ -744,8 +779,13 @@ export function createPageService(pool, options = {}) {
} catch {
snapshot = {};
}
const workspaceHtmlRelativePath = snapshot.relative_path ?? 'public/index.html';
const workspacePublishDir = h5Root ? resolvePublishDir(h5Root, { id: userId }) : null;
const workspaceHtmlRelativePath = inferWorkspaceHtmlRelativePath({
snapshotRelativePath: snapshot.relative_path,
pageTitle: rows[0]?.title,
htmlContent,
publishDir: workspacePublishDir,
});
return {
workspaceHtmlRelativePath,
workspacePublishDir,
@@ -753,10 +793,22 @@ export function createPageService(pool, options = {}) {
};
};
const rewriteHtmlDownloadLinksForPage = async (userId, pageId, html) => {
const ctx = await loadPageWorkspaceContext(userId, pageId, html);
const { html: rewritten } = await prepareHtmlDownloadLinks(pool, userId, html, {
htmlRelativePath: ctx.workspaceHtmlRelativePath,
publishDir: ctx.workspacePublishDir,
publishKey: ctx.publishKey,
publicBaseUrl: '',
preferAssetDownload: true,
});
return rewritten;
};
const finalizeHtmlPreview = async (userId, pageId, html) => {
const shell = renderHtmlPreview(html);
try {
const ctx = await loadPageWorkspaceContext(userId, pageId);
const ctx = await loadPageWorkspaceContext(userId, pageId, html);
const { html: rewritten } = await prepareHtmlDownloadLinks(pool, userId, shell, {
htmlRelativePath: ctx.workspaceHtmlRelativePath,
publishDir: ctx.workspacePublishDir,
@@ -1334,10 +1386,12 @@ export function createPageService(pool, options = {}) {
listPages,
findPageBySourceAsset,
findPageBySourceMessage,
findPageByRelativePath,
getPage,
getDeletePreview,
deletePage,
listVersions,
rewriteHtmlDownloadLinksForPage,
renderPreview: async (userId, pageId) => {
const page = await getPage(userId, pageId);
const html =
+7 -1
View File
@@ -1,6 +1,6 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { pageInternals } from './mindspace-pages.mjs';
import { pageInternals, normalizeWorkspaceRelativePath } from './mindspace-pages.mjs';
test('parseJsonColumn accepts mysql json objects and strings', () => {
const objectValue = { relative_path: 'public/demo.html', auto_synced: true };
@@ -126,3 +126,9 @@ test('buildPageDeleteSummary lists cascade delete impact', () => {
assert.ok(lines.some((line) => line.includes('广场')));
assert.ok(lines.some((line) => line.includes('原始上传资料不会被删除')));
});
test('normalizeWorkspaceRelativePath unifies bare html filenames under public/', () => {
assert.equal(normalizeWorkspaceRelativePath('ai-robot-report.html'), 'public/ai-robot-report.html');
assert.equal(normalizeWorkspaceRelativePath('public/ai-robot-report.html'), 'public/ai-robot-report.html');
assert.equal(normalizeWorkspaceRelativePath('/public/demo.html'), 'public/demo.html');
});
+48 -7
View File
@@ -9,6 +9,7 @@ import { buildPublicUrl, resolvePublicBaseUrl, resolvePublishDir } from './user-
import {
loadWorkspaceDownloadLinkIndex,
rewriteRelativeDownloadLinks,
inferWorkspaceHtmlRelativePath,
} from './mindspace-html-download-links.mjs';
import { createImgproxySigner } from './imgproxy-signer.mjs';
@@ -248,7 +249,8 @@ async function prepareHtmlPublishContent({
html,
ownerSlug,
urlSlug,
htmlRelativePath = `public/${urlSlug}.html`,
htmlRelativePath = null,
pageTitle = null,
absoluteStoragePath,
imgproxySigner = null,
h5Root = null,
@@ -265,11 +267,18 @@ async function prepareHtmlPublishContent({
absoluteStoragePath,
imgproxySigner,
});
publishContent = rewriteWorkspacePublicAssetReferences(publishContent, htmlRelativePath);
const publishDir = h5Root ? resolvePublishDir(h5Root, { id: userId }) : null;
const resolvedHtmlRelativePath =
htmlRelativePath
?? inferWorkspaceHtmlRelativePath({
pageTitle,
htmlContent: publishContent,
publishDir,
});
publishContent = rewriteWorkspacePublicAssetReferences(publishContent, resolvedHtmlRelativePath);
const linkIndex = await loadWorkspaceDownloadLinkIndex(pool, userId);
publishContent = rewriteRelativeDownloadLinks(publishContent, {
htmlRelativePath,
htmlRelativePath: resolvedHtmlRelativePath,
publishDir,
linkIndex,
publishKey: userId,
@@ -354,7 +363,8 @@ export function createPublicationService(pool, options = {}) {
const [rows] = await pool.query(
`SELECT p.id AS page_id, p.title, p.summary, p.page_type, p.template_id, p.current_version_id,
p.user_id, p.space_id, pv.id AS page_version_id, pv.version_no,
pv.bundle_asset_id, av.storage_key
pv.bundle_asset_id, av.storage_key,
JSON_UNQUOTE(JSON_EXTRACT(pv.source_snapshot_json, '$.relative_path')) AS source_relative_path
FROM h5_page_records p
JOIN h5_page_versions pv ON pv.page_id = p.id
JOIN h5_asset_versions av ON av.asset_id = pv.content_asset_id AND av.version_no = 1
@@ -387,12 +397,20 @@ export function createPublicationService(pool, options = {}) {
const preparePublishContent = async (page, ownerSlug, urlSlug) => {
let publishContent = page.content;
if (page.page_type !== 'html') return publishContent;
const publishDir = h5Root ? resolvePublishDir(h5Root, { id: page.user_id }) : null;
return prepareHtmlPublishContent({
pool,
userId: page.user_id,
html: publishContent,
ownerSlug,
urlSlug,
pageTitle: page.title,
htmlRelativePath: inferWorkspaceHtmlRelativePath({
snapshotRelativePath: page.source_relative_path,
pageTitle: page.title,
htmlContent: publishContent,
publishDir,
}),
absoluteStoragePath,
h5Root,
imgproxySigner: imgproxySigner ? { buildUrl: (path, preset) => imgproxySigner.buildUrl(imgproxySigner.baseUrl, path, preset) } : null,
@@ -875,17 +893,39 @@ export function createPublicationService(pool, options = {}) {
now,
],
);
const html = await fs.readFile(await resolveReadableStoragePath(row.storage_key), 'utf8');
return {
html: await fs.readFile(await resolveReadableStoragePath(row.storage_key), 'utf8'),
html: await refreshPublishedHtmlDownloadLinks(row, html),
publication: publicationResponse({ ...row, view_count: Number(row.view_count) + 1 }),
};
};
const refreshPublishedHtmlDownloadLinks = async (row, html) => {
if (!h5Root || !row?.owner_id) return html;
const source = String(html ?? '');
if (!source.trim()) return html;
const publishDir = resolvePublishDir(h5Root, { id: row.owner_id });
const linkIndex = await loadWorkspaceDownloadLinkIndex(pool, row.owner_id);
return rewriteRelativeDownloadLinks(html, {
htmlRelativePath: inferWorkspaceHtmlRelativePath({
pageTitle: row.title,
htmlContent: html,
publishDir,
}),
publishDir,
linkIndex,
publishKey: row.owner_id,
publicBaseUrl: resolvePublicBaseUrl(),
preferAssetDownload: false,
}).html;
};
const resolvePublic = async (ownerSlug, urlSlug, viewerId, password, requestMeta) => {
const [rows] = await pool.query(
`SELECT pr.*, u.id AS owner_id, av.storage_key
`SELECT pr.*, u.id AS owner_id, p.title, av.storage_key
FROM h5_publish_records pr
JOIN h5_users u ON u.id = pr.user_id
JOIN h5_page_records p ON p.id = pr.page_id
JOIN h5_page_versions pv ON pv.id = pr.page_version_id AND pv.immutable = 1
JOIN h5_asset_versions av ON av.asset_id = pv.bundle_asset_id AND av.version_no = 1
WHERE COALESCE(u.slug, u.username) = ? AND pr.url_slug = ? AND pr.status = 'online'
@@ -899,9 +939,10 @@ export function createPublicationService(pool, options = {}) {
const resolvePrivateLink = async (token, viewerId, requestMeta) => {
const tokenHash = crypto.createHash('sha256').update(String(token)).digest('hex');
const [rows] = await pool.query(
`SELECT pr.*, u.id AS owner_id, av.storage_key
`SELECT pr.*, u.id AS owner_id, p.title, av.storage_key
FROM h5_publish_records pr
JOIN h5_users u ON u.id = pr.user_id
JOIN h5_page_records p ON p.id = pr.page_id
JOIN h5_page_versions pv ON pv.id = pr.page_version_id AND pv.immutable = 1
JOIN h5_asset_versions av ON av.asset_id = pv.bundle_asset_id AND av.version_no = 1
WHERE pr.token_hash = ? AND pr.status = 'online' AND pr.access_mode = 'private_link'
+12
View File
@@ -14,6 +14,18 @@ test('buildEditablePreviewDocument injects visual editor into body', () => {
assert.match(result, /data-mindspace-editing/);
assert.match(result, /<p>Hi<\/p>/);
assert.match(result, new RegExp(MINDSPACE_PAGE_CONTENT_MESSAGE.replace(':', '\\:')));
assert.match(result, /data-collapsed/);
assert.match(result, /5000/);
assert.match(result, /点击展开/);
assert.match(result, /isProtectedNode/);
});
test('buildEditablePreviewDocument normalizes platform brand contact line', () => {
const html = '<!doctype html><html><body><p>📧 contact@tkmind.ai</p></body></html>';
const result = buildEditablePreviewDocument(html);
assert.match(result, /contact@tkmind\.cn/);
assert.match(result, /data-mindspace-page-tag="platform-brand"/);
assert.doesNotMatch(result, /tkmind\.ai/i);
});
test('buildEditablePreviewDocument wraps fragment html', () => {
+3 -9
View File
@@ -181,14 +181,14 @@ export function createMindSpaceService(pool, options = {}) {
c.ai_access_policy, c.publish_policy, c.is_system, c.sort_order,
CASE
WHEN c.category_code = 'draft' THEN COALESCE(pc.item_count, 0)
WHEN c.category_code = 'public' THEN COALESCE(pub.item_count, 0)
WHEN c.category_code IN ('oa', 'public') THEN COALESCE(pc.item_count, 0) + COALESCE(ac.item_count, 0)
ELSE COALESCE(ac.item_count, 0)
END AS item_count
FROM h5_space_categories c
LEFT JOIN (
SELECT category_id, user_id, COUNT(*) AS item_count
FROM h5_assets
WHERE user_id = ? AND status <> 'deleted' AND source_type = 'upload'
WHERE user_id = ? AND status <> 'deleted'
GROUP BY category_id, user_id
) ac ON ac.category_id = c.id AND ac.user_id = c.user_id
LEFT JOIN (
@@ -197,15 +197,9 @@ export function createMindSpaceService(pool, options = {}) {
WHERE user_id = ? AND status <> 'deleted'
GROUP BY category_id, user_id
) pc ON pc.category_id = c.id AND pc.user_id = c.user_id
LEFT JOIN (
SELECT user_id, COUNT(DISTINCT page_id) AS item_count
FROM h5_publish_records
WHERE user_id = ? AND status = 'online'
GROUP BY user_id
) pub ON pub.user_id = c.user_id
WHERE c.user_id = ? AND c.space_id = ?
ORDER BY c.sort_order, c.category_name`,
[userId, userId, userId, userId, row.id],
[userId, userId, userId, row.id],
);
const quotaBytes = asNumber(row.quota_bytes);
+3 -2
View File
@@ -105,8 +105,9 @@ test('getSpace scopes space and categories to the authenticated user', async ()
assert.equal(space.quota.availableBytes, 5 * 1024 * 1024 - 3072);
assert.equal(space.categories[0].code, 'private');
assert.deepEqual(calls[0].params, ['user-1']);
assert.deepEqual(calls[1].params, ['user-1', 'user-1', 'user-1', 'user-1', 'space-1']);
assert.match(calls[1].sql, /source_type\s*=\s*'upload'/);
assert.deepEqual(calls[1].params, ['user-1', 'user-1', 'user-1', 'space-1']);
assert.match(calls[1].sql, /WHEN c\.category_code IN \('oa', 'public'\) THEN COALESCE\(pc\.item_count, 0\) \+ COALESCE\(ac\.item_count, 0\)/);
assert.doesNotMatch(calls[1].sql, /source_type\s*=\s*'upload'/);
});
test('getSpace includes schedule snapshot when schedule service is available', async () => {
Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

+50
View File
@@ -0,0 +1,50 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>微信分享预览 · 连云港三天两夜攻略 🌊</title>
<style>
* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; padding: 24px; background: #ececec; font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, sans-serif; color: #111; }
.wrap { max-width: 420px; margin: 0 auto; }
h1 { margin: 0 0 8px; font-size: 18px; }
.hint, .note { margin: 0 0 16px; color: #666; font-size: 13px; line-height: 1.6; }
.card { display: grid; grid-template-columns: 1fr 72px; gap: 12px; padding: 12px; border-radius: 8px; background: #fff; box-shadow: 0 1px 0 rgba(0,0,0,.06); }
.card-main { min-width: 0; }
.card-title { margin: 0; font-size: 16px; font-weight: 600; line-height: 1.35; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card-desc { margin: 6px 0 0; color: #888; font-size: 13px; line-height: 1.45; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card-foot { display: flex; align-items: center; gap: 6px; margin-top: 10px; color: #999; font-size: 12px; }
.card-foot img { width: 16px; height: 16px; border-radius: 999px; object-fit: cover; }
.card-thumb { width: 72px; height: 72px; border-radius: 4px; object-fit: cover; background: #f3f3f3; }
.card-thumb-empty { display: grid; place-items: center; color: #bbb; font-size: 12px; }
.meta { margin-top: 18px; padding: 14px; border-radius: 10px; background: rgba(255,255,255,.72); font-size: 12px; line-height: 1.7; word-break: break-all; }
code { background: rgba(0,0,0,.06); padding: 1px 4px; border-radius: 4px; }
</style>
</head>
<body>
<div class="wrap">
<h1>微信链接卡片预览</h1>
<p class="hint">本地模拟的是「粘贴链接后看到的卡片」,不是 JS-SDK 内部分享弹层。源链接:<code>https://m.tkmind.cn/MindSpace/john/public/lianyungang-guide.html</code></p>
<p class="note">修复后:标题 + 描述 + 缩略图 + 底部「TKMind 智趣」小图标应同时出现。</p>
<article class="card" aria-label="微信分享卡片预览">
<div class="card-main">
<h2 class="card-title">连云港三天两夜攻略 🌊</h2>
<p class="card-desc">三天两夜·沙滩酒店·必吃美食</p>
<div class="card-foot">
<img src="http://127.0.0.1:8081/brand/tkmind-icon.png" alt="" onerror="this.style.display='none'">
<span>TKMind 智趣</span>
</div>
</div>
<img class="card-thumb" src="https://images.unsplash.com/photo-1507525428034-b723cf961d3e?auto=format&amp;fit=crop&amp;w=200&amp;q=80" alt="">
</article>
<div class="meta">
<div><strong>og:title</strong> 连云港三天两夜攻略 🌊</div>
<div><strong>og:description</strong> 三天两夜·沙滩酒店·必吃美食</div>
<div><strong>og:site_name</strong> TKMind 智趣</div>
<div><strong>og:image</strong> https://images.unsplash.com/photo-1507525428034-b723cf961d3e?auto=format&amp;fit=crop&amp;w=200&amp;q=80</div>
<div><strong>icon</strong> /brand/tkmind-icon.png</div>
</div>
</div>
</body>
</html>
+41
View File
@@ -0,0 +1,41 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>微信分享预览工具</title>
<style>
* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; padding: 24px; background: #f3f3f3; font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, sans-serif; color: #111; }
.panel { max-width: 560px; margin: 0 auto; padding: 20px; border-radius: 16px; background: #fff; box-shadow: 0 8px 30px rgba(0,0,0,.06); }
h1 { margin: 0 0 8px; font-size: 22px; }
p { margin: 0 0 16px; color: #666; line-height: 1.6; font-size: 14px; }
label { display: block; margin-bottom: 8px; font-size: 13px; font-weight: 600; }
input { width: 100%; padding: 12px 14px; border: 1px solid #ddd; border-radius: 10px; font: inherit; }
button { margin-top: 14px; width: 100%; padding: 12px 14px; border: 0; border-radius: 10px; background: #2f6f57; color: #fff; font: inherit; font-weight: 700; cursor: pointer; }
code { background: rgba(0,0,0,.06); padding: 1px 4px; border-radius: 4px; }
</style>
</head>
<body>
<div class="panel">
<h1>微信分享预览</h1>
<p>输入已发布页面路径,本地模拟微信「粘贴链接后」看到的卡片。开发环境走 Portal API:<code>/dev/wechat-share-preview</code></p>
<form id="preview-form">
<label for="url">页面 URL 或路径</label>
<input id="url" name="url" placeholder="/MindSpace/john/public/demo.html" required>
<button type="submit">生成预览</button>
</form>
</div>
<script>
document.getElementById('preview-form').addEventListener('submit', (event) => {
event.preventDefault();
const value = document.getElementById('url').value.trim();
if (!value) return;
const target = value.startsWith('http')
? `/dev/wechat-share-preview?url=${encodeURIComponent(value)}`
: `/dev/wechat-share-preview?url=${encodeURIComponent(value.startsWith('/') ? value : `/${value}`)}`;
window.location.href = target;
});
</script>
</body>
</html>
+204
View File
@@ -0,0 +1,204 @@
#!/usr/bin/env node
/**
* Remove duplicate MindSpace pages for one user, keeping one page per workspace path/title.
*
* Usage:
* node scripts/dedupe-user-pages.mjs --username=john --dry-run
* node scripts/dedupe-user-pages.mjs --username=john --yes
*/
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import fs from 'node:fs';
import { createDbPool } from '../db.mjs';
import { createPageService } from '../mindspace-pages.mjs';
import { loadH5Environment } from './load-env.mjs';
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
const root = path.join(scriptDir, '..');
loadH5Environment(scriptDir);
function readArg(name) {
const prefix = `--${name}=`;
const hit = process.argv.find((arg) => arg.startsWith(prefix));
return hit ? hit.slice(prefix.length).trim() : null;
}
function loadEnvFromFile(filePath) {
if (!fs.existsSync(filePath)) return;
for (const line of fs.readFileSync(filePath, 'utf8').split('\n')) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) continue;
const eq = trimmed.indexOf('=');
if (eq < 0) continue;
const key = trimmed.slice(0, eq).trim();
const value = trimmed.slice(eq + 1).trim();
if (!process.env[key]) process.env[key] = value;
}
}
const prodEnv = '/Users/john/Project/Memind/.env';
if (fs.existsSync(prodEnv)) loadEnvFromFile(prodEnv);
const dryRun = process.argv.includes('--dry-run');
const confirmed = process.argv.includes('--yes');
const removeFromPlaza = process.argv.includes('--remove-from-plaza');
const username = readArg('username');
const userIdArg = readArg('user-id');
const storageRoot = process.env.MINDSPACE_STORAGE_ROOT
? path.resolve(process.env.MINDSPACE_STORAGE_ROOT)
: path.join(root, 'data', 'mindspace');
const h5Root = process.env.MINDSPACE_H5_ROOT
? path.resolve(process.env.MINDSPACE_H5_ROOT)
: root;
async function resolveUserId(pool) {
if (userIdArg) return userIdArg;
if (!username) throw new Error('请指定 --username=... 或 --user-id=...');
const [rows] = await pool.query(
`SELECT id, username, email FROM h5_users WHERE username = ? LIMIT 1`,
[username],
);
const row = rows[0];
if (!row) throw new Error(`用户不存在: ${username}`);
return row.id;
}
function groupKey(row) {
const relativePath = String(row.relative_path ?? '').trim();
if (relativePath) return `path:${relativePath}`;
const title = String(row.title ?? '').trim().toLowerCase();
return `title:${title || row.id}`;
}
function mergeGroupsByTitle(groups) {
const titleToPathKey = new Map();
for (const [key, items] of groups.entries()) {
if (!key.startsWith('path:')) continue;
const title = String(items[0]?.title ?? '').trim().toLowerCase();
if (title) titleToPathKey.set(title, key);
}
for (const [key, items] of [...groups.entries()]) {
if (!key.startsWith('title:')) continue;
const title = key.slice('title:'.length);
const pathKey = titleToPathKey.get(title);
if (!pathKey) continue;
groups.get(pathKey).push(...items);
groups.delete(key);
}
return groups;
}
function pickKeeper(rows) {
return [...rows].sort((left, right) => {
const leftHasPath = Boolean(String(left.relative_path ?? '').trim());
const rightHasPath = Boolean(String(right.relative_path ?? '').trim());
if (leftHasPath !== rightHasPath) return rightHasPath ? 1 : -1;
const pubDiff = Number(right.has_online_pub ?? 0) - Number(left.has_online_pub ?? 0);
if (pubDiff !== 0) return pubDiff;
const sessionDiff = Number(Boolean(right.source_session_id)) - Number(Boolean(left.source_session_id));
if (sessionDiff !== 0) return sessionDiff;
const updatedDiff = Number(right.updated_at ?? 0) - Number(left.updated_at ?? 0);
if (updatedDiff !== 0) return updatedDiff;
return String(left.id).localeCompare(String(right.id));
})[0];
}
const pool = createDbPool();
const pageService = createPageService(pool, { h5Root, storageRoot });
try {
const userId = await resolveUserId(pool);
const [userRows] = await pool.query(
`SELECT username, email FROM h5_users WHERE id = ? LIMIT 1`,
[userId],
);
const user = userRows[0];
const [rows] = await pool.query(
`SELECT p.id, p.title, p.source_session_id, p.created_at, p.updated_at,
JSON_UNQUOTE(JSON_EXTRACT(pv.source_snapshot_json, '$.relative_path')) AS relative_path,
EXISTS(
SELECT 1 FROM h5_publish_records pr
WHERE pr.page_id = p.id AND pr.user_id = p.user_id AND pr.status = 'online'
) AS has_online_pub
FROM h5_page_records p
JOIN h5_page_versions pv ON pv.id = p.current_version_id
WHERE p.user_id = ? AND p.status <> 'deleted'
ORDER BY p.updated_at DESC, p.id DESC`,
[userId],
);
const groups = new Map();
for (const row of rows) {
const key = groupKey(row);
if (!groups.has(key)) groups.set(key, []);
groups.get(key).push(row);
}
mergeGroupsByTitle(groups);
const duplicateGroups = [...groups.entries()].filter(([, items]) => items.length > 1);
const toDelete = [];
for (const [key, items] of duplicateGroups) {
const keeper = pickKeeper(items);
for (const item of items) {
if (item.id !== keeper.id) {
toDelete.push({ key, keeperId: keeper.id, page: item });
}
}
}
console.log(`用户: ${user?.username ?? userId} (${user?.email ?? 'unknown'})`);
console.log(`总页面: ${rows.length}`);
console.log(`重复组: ${duplicateGroups.length}`);
console.log(`待删除重复页: ${toDelete.length}`);
for (const [key, items] of duplicateGroups.sort((a, b) => b[1].length - a[1].length).slice(0, 20)) {
const keeper = pickKeeper(items);
console.log(`- ${key}: ${items.length} 份,保留 ${keeper.id},删除 ${items.length - 1} 份`);
}
if (duplicateGroups.length > 20) {
console.log(`... 另有 ${duplicateGroups.length - 20} 组未展开`);
}
if (toDelete.length === 0) {
console.log('没有需要清理的重复页面。');
process.exit(0);
}
if (dryRun) {
console.log('[dry-run] 未执行删除。');
process.exit(0);
}
if (!confirmed) {
console.error('请加 --yes 确认删除,或先用 --dry-run 查看。');
process.exit(1);
}
let deleted = 0;
let failed = 0;
for (const entry of toDelete) {
try {
await pageService.deletePage(userId, entry.page.id, { removeFromPlaza });
deleted += 1;
if (deleted % 25 === 0) {
console.log(`已删除 ${deleted}/${toDelete.length} ...`);
}
} catch (error) {
failed += 1;
const message = error instanceof Error ? error.message : String(error);
console.error(`删除失败 ${entry.page.id} (${entry.page.title}): ${message}`);
}
}
const [remainingRows] = await pool.query(
`SELECT COUNT(*) AS count FROM h5_page_records WHERE user_id = ? AND status <> 'deleted'`,
[userId],
);
console.log('---');
console.log(`完成:删除 ${deleted},失败 ${failed},剩余 ${Number(remainingRows[0]?.count ?? 0)} 页`);
process.exit(failed > 0 ? 1 : 0);
} finally {
await pool.end();
}
+77
View File
@@ -0,0 +1,77 @@
#!/usr/bin/env node
import crypto from 'node:crypto';
import mysql from 'mysql2/promise';
import { zonedTimeToEpochMs } from '../schedule-time.mjs';
const pool = mysql.createPool(process.env.DATABASE_URL ?? 'mysql://boot:888888@localhost:3306/memind');
const uid = '1c99b83b-0454-474f-a5d2-129d34506a32';
const tz = 'Asia/Shanghai';
const t = (y, m, d, h, mi) =>
zonedTimeToEpochMs({ year: y, month: m, day: d, hour: h, minute: mi, second: 0 }, tz);
const badItems = [
'f058e736-1e6d-4559-8399-36cb91d67634',
'be571e96-365c-4a0a-806a-491089e02144',
'03ff84f1-3ac0-4fc6-ab7e-9fe2064fbf15',
];
await pool.query('DELETE FROM h5_schedule_reminders WHERE item_id IN (?)', [badItems]);
await pool.query('DELETE FROM h5_schedule_items WHERE id IN (?)', [badItems]);
await pool.query(
'UPDATE h5_schedule_items SET kind=?, start_at=?, end_at=? WHERE id=?',
['event', t(2026, 7, 1, 6, 0), t(2026, 7, 1, 6, 30), '8b34b44e-84cf-462b-86a4-e895e656d626'],
);
await pool.query(
'UPDATE h5_schedule_items SET start_at=?, end_at=? WHERE id=?',
[t(2026, 7, 1, 12, 0), t(2026, 7, 1, 13, 0), '6fb372ca-3537-4f63-8a8c-d30615d6e832'],
);
await pool.query('UPDATE h5_schedule_reminders SET remind_at=? WHERE id=?', [
t(2026, 7, 1, 5, 55),
'633b008a-791b-455c-8f3b-331d5c7b1971',
]);
await pool.query('UPDATE h5_schedule_reminders SET remind_at=? WHERE id=?', [
t(2026, 7, 1, 11, 50),
'e423c8be-7463-481a-b881-131cb32b6f99',
]);
const [existingDinner] = await pool.query(
`SELECT id FROM h5_schedule_items WHERE user_id=? AND title LIKE '%聚餐%' AND deleted_at IS NULL LIMIT 1`,
[uid],
);
if (!existingDinner[0]) {
const dinnerId = crypto.randomUUID();
const now = Date.now();
await pool.query(
`INSERT INTO h5_schedule_items
(id, user_id, kind, title, status, start_at, end_at, all_day, timezone, source_channel, created_at, updated_at)
VALUES (?, ?, 'event', ?, 'active', ?, ?, 0, ?, 'agent', ?, ?)`,
[
dinnerId,
uid,
'同学聚餐 🍽️',
t(2026, 7, 1, 18, 30),
t(2026, 7, 1, 21, 0),
tz,
now,
now,
],
);
await pool.query(
`INSERT INTO h5_schedule_reminders
(id, user_id, item_id, remind_at, offset_minutes, channel, status, attempts, created_at, updated_at)
VALUES (?, ?, ?, ?, 30, 'wechat', 'pending', 0, ?, ?)`,
[crypto.randomUUID(), uid, dinnerId, t(2026, 7, 1, 18, 0), now, now],
);
}
const [rows] = await pool.query(
`SELECT title, start_at FROM h5_schedule_items WHERE user_id=? AND deleted_at IS NULL ORDER BY start_at`,
[uid],
);
for (const row of rows) {
console.log(
row.title,
new Date(Number(row.start_at)).toLocaleString('zh-CN', { timeZone: tz }),
);
}
await pool.end();
+2
View File
@@ -34,11 +34,13 @@ assertExcludes(
const messageTs = read('src/utils/message.ts');
assertIncludes(messageTs, 'deriveUserFacingText', 'message.ts');
assertIncludes(messageTs, 'deriveAssistantFacingText', 'message.ts');
assertIncludes(messageTs, 'chat-finish-sync.mjs', 'message.ts');
const conversationDisplay = read('conversation-display.mjs');
assertIncludes(conversationDisplay, 'TASK_ROUTING_HINT_RE', 'conversation-display.mjs');
assertIncludes(conversationDisplay, 'deriveUserFacingText', 'conversation-display.mjs');
assertIncludes(conversationDisplay, 'deriveAssistantFacingText', 'conversation-display.mjs');
const chatSkills = read('chat-skills.mjs');
assertIncludes(chatSkills, 'stripKnownChatSkillPrompt', 'chat-skills.mjs');
+59
View File
@@ -0,0 +1,59 @@
#!/usr/bin/env node
import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import {
extractSharePreviewMeta,
injectOgTags,
renderWechatSharePreviewHtml,
} from '../mindspace-og-tags.mjs';
const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
async function loadHtmlSource(input) {
if (/^https?:\/\//i.test(input)) {
const res = await fetch(input);
if (!res.ok) throw new Error(`HTTP ${res.status} for ${input}`);
return { html: await res.text(), pageUrl: input.split('#')[0] };
}
const filePath = path.resolve(process.cwd(), input);
const html = await fs.readFile(filePath, 'utf8');
const pageUrl = pathToFileURL(filePath).toString();
return { html, pageUrl };
}
async function main() {
const input = process.argv[2];
const origin = (process.argv[3] || 'https://m.tkmind.cn').replace(/\/$/, '');
if (!input) {
console.error('用法: node scripts/wechat-share-preview.mjs <页面路径或URL> [origin]');
process.exit(1);
}
const { html: rawHtml, pageUrl } = await loadHtmlSource(input);
const pageDirUrl = pageUrl.includes('/')
? `${pageUrl.slice(0, pageUrl.lastIndexOf('/') + 1)}`
: `${origin}/`;
const html = injectOgTags(rawHtml, { origin, pageUrl, pageDirUrl });
const preview = extractSharePreviewMeta(html, { origin, pageUrl, pageDirUrl });
const outDir = path.join(root, 'public', 'dev');
await fs.mkdir(outDir, { recursive: true });
const outPath = path.join(outDir, 'wechat-share-preview.generated.html');
await fs.writeFile(
outPath,
renderWechatSharePreviewHtml(preview, {
note: '由 scripts/wechat-share-preview.mjs 生成,可离线查看卡片布局。',
}),
'utf8',
);
console.log(`预览已写入 ${outPath}`);
console.log(`title: ${preview.title}`);
console.log(`description: ${preview.description}`);
console.log(`site: ${preview.siteName}`);
console.log(`image: ${preview.imageUrl || '(none)'}`);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});
+168 -15
View File
@@ -39,8 +39,9 @@ import { assertMindSpaceRoute, mindspaceFlags } from './mindspace-flags.mjs';
import { createMindSpaceService, DEFAULT_MAX_FILE_BYTES } from './mindspace.mjs';
import { ensureMindSpaceConfig } from './mindspace-config.mjs';
import { createAssetService } from './mindspace-assets.mjs';
import { createPageService, pageInternals, inlinePrivateAssetsInHtml } from './mindspace-pages.mjs';
import { createPageService, pageInternals, inlinePrivateAssetsInHtml, normalizeWorkspaceRelativePath } from './mindspace-pages.mjs';
import { createPageLiveEditService } from './mindspace-page-live-edit.mjs';
import { createAssetAgentService } from './mindspace-asset-agent.mjs';
import { createPageEditSessionService } from './mindspace-page-edit-session.mjs';
import { suggestCoverMetaWithAi } from './mindspace-cover-ai.mjs';
import { createPublicationService, rewriteWorkspacePublicAssetReferences } from './mindspace-publications.mjs';
@@ -88,8 +89,13 @@ import {
syncPublicHtmlAfterFinish,
} from './mindspace-public-finish-sync.mjs';
import { syncGeneratedPagesFromPublicAssets } from './mindspace-page-sync.mjs';
import { generateHtmlThumbnail } from './mindspace-thumbnails.mjs';
import { injectOgTags, injectWechatShareBridge } from './mindspace-og-tags.mjs';
import { extractCoverSignals, generateHtmlThumbnail } from './mindspace-thumbnails.mjs';
import {
extractSharePreviewMeta,
injectOgTags,
injectWechatShareBridge,
renderWechatSharePreviewHtml,
} from './mindspace-og-tags.mjs';
import {
ensureThumbnailPng,
rasterizeThumbnailSvgToPng,
@@ -234,6 +240,7 @@ let mindSpaceAssets = null;
let mindSpaceAudit = null;
let mindSpacePages = null;
let mindSpacePageLiveEdit = null;
let mindSpaceAssetAgent = null;
let mindSpacePageEditSession = null;
let mindSpacePublications = null;
let plazaPosts = null;
@@ -289,15 +296,20 @@ async function bootstrapUserAuth() {
storageRoot:
process.env.MINDSPACE_STORAGE_ROOT ?? path.join(__dirname, 'data', 'mindspace'),
});
const resolveUserIdForAgentSession = async (sessionId) => {
const [rows] = await pool.query(
`SELECT user_id FROM h5_user_sessions WHERE agent_session_id = ? LIMIT 1`,
[sessionId],
);
return rows[0]?.user_id ?? null;
};
mindSpacePageLiveEdit = createPageLiveEditService({
pageService: mindSpacePages,
resolveUserIdForAgentSession: async (sessionId) => {
const [rows] = await pool.query(
`SELECT user_id FROM h5_user_sessions WHERE agent_session_id = ? LIMIT 1`,
[sessionId],
);
return rows[0]?.user_id ?? null;
},
resolveUserIdForAgentSession,
});
mindSpaceAssetAgent = createAssetAgentService({
assetService: mindSpaceAssets,
resolveUserIdForAgentSession,
});
mindSpacePublications = createPublicationService(pool, {
h5Root: __dirname,
@@ -1706,6 +1718,7 @@ api.use(async (req, res, next) => {
if (req.path === '/status') return next();
if (req.path.startsWith('/internal/agent/')) return next();
if (req.path === '/agent/mindspace_page_patch') return next();
if (req.path === '/agent/mindspace_asset_delete') return next();
if (req.path === '/config/blocked-words') return next();
if (req.method === 'GET' && /^\/mindspace\/v1\/assets\/[^/]+\/download$/.test(req.path)) {
return next();
@@ -2615,8 +2628,33 @@ api.post('/mindspace/v1/pages/from-asset', async (req, res) => {
req.currentUser.id,
assetId,
);
if (asset.mimeType === 'text/html') {
const relativePath = normalizeWorkspaceRelativePath(
String(asset.originalFilename ?? asset.original_filename ?? '').includes('/')
? asset.originalFilename ?? asset.original_filename
: `public/${asset.originalFilename ?? asset.original_filename ?? ''}`,
);
const existingByPath = relativePath
? await mindSpacePages.findPageByRelativePath(req.currentUser.id, relativePath).catch(() => null)
: null;
if (existingByPath) {
return sendData(res, req, {
kind: 'page',
categoryCode: existingByPath.categoryCode ?? 'draft',
page: existingByPath,
});
}
}
const content = await fs.promises.readFile(assetPath, 'utf8');
const contentFormat = asset.mimeType === 'text/html' ? 'html' : 'markdown';
const htmlRelativePath =
contentFormat === 'html'
? normalizeWorkspaceRelativePath(
String(asset.originalFilename ?? asset.original_filename ?? '').includes('/')
? asset.originalFilename ?? asset.original_filename
: `public/${asset.originalFilename ?? asset.original_filename ?? ''}`,
)
: null;
const page = await mindSpacePages.createFromChat(
req.currentUser.id,
{
@@ -2633,6 +2671,7 @@ api.post('/mindspace/v1/pages/from-asset', async (req, res) => {
source_asset_id: asset.id,
source_category: asset.categoryCode,
content_mode: contentFormat,
...(htmlRelativePath ? { relative_path: htmlRelativePath } : {}),
},
},
);
@@ -2696,6 +2735,17 @@ async function resolveOwnedAssistantMessage(userId, sessionId, messageId) {
return { session, message, content };
}
async function resolveExistingSavedPage(userId, { sessionId, messageId, relativePath } = {}) {
if (!mindSpacePages || !userId) return null;
const byMessage = await mindSpacePages
.findPageBySourceMessage(userId, sessionId, messageId)
.catch(() => null);
if (byMessage) return byMessage;
const normalizedPath = normalizeWorkspaceRelativePath(relativePath);
if (!normalizedPath) return null;
return mindSpacePages.findPageByRelativePath(userId, normalizedPath).catch(() => null);
}
const SAVE_TARGET_CATEGORIES = new Set(['draft', 'oa', 'public']);
const pageSyncInFlight = new Map();
@@ -2911,11 +2961,11 @@ api.post('/mindspace/v1/pages/analyze-chat-save', async (req, res) => {
thumbnailReady = false;
}
}
const existingPage = await mindSpacePages.findPageBySourceMessage(
req.currentUser.id,
const existingPage = await resolveExistingSavedPage(req.currentUser.id, {
sessionId,
messageId,
).catch(() => null);
relativePath: resolvedHtml?.relativePath ?? analysis.relativePath,
});
return sendData(res, req, {
contentMode: analysis.contentMode,
links: analysis.links,
@@ -3009,7 +3059,9 @@ api.post('/mindspace/v1/pages/save-from-chat', async (req, res) => {
role: source.message.role,
content_mode: analysis.contentMode,
public_url: analysis.previewUrl,
relative_path: analysis.relativePath,
relative_path: analysis.relativePath
? normalizeWorkspaceRelativePath(analysis.relativePath)
: analysis.relativePath,
};
let resolvedHtml = null;
@@ -3086,9 +3138,16 @@ api.post('/mindspace/v1/pages/save-from-chat', async (req, res) => {
}).catch(() => {});
}
const replacePageId = req.body?.replace_page_id
const saveAsNew = Boolean(req.body?.save_as_new);
let replacePageId = req.body?.replace_page_id
? String(req.body.replace_page_id).trim()
: null;
if (!replacePageId && !saveAsNew && analysis.contentMode === 'static_html' && analysis.relativePath) {
const existingByPath = await mindSpacePages
.findPageByRelativePath(req.currentUser.id, analysis.relativePath)
.catch(() => null);
if (existingByPath) replacePageId = existingByPath.id;
}
let page;
if (replacePageId) {
const existingPage = await mindSpacePages.getPage(req.currentUser.id, replacePageId);
@@ -3363,6 +3422,32 @@ api.post('/agent/mindspace_page_patch', async (req, res) => {
}
});
api.post('/agent/mindspace_asset_delete', async (req, res) => {
if (!mindSpaceAssetAgent) {
return res.status(503).json({ message: 'MindSpace 未启用' });
}
try {
const result = await mindSpaceAssetAgent.applyAgentDelete(req.body ?? {});
await mindSpaceAudit?.write({
userId: result.userId ?? null,
action: 'asset.delete',
objectType: 'asset',
objectId: result.assetId,
ip: req.ip,
metadata: { via: 'agent' },
});
return sendData(res, req, result);
} catch (error) {
if (error?.code === 'forbidden') {
return sendError(res, req, 403, error.code, error.message);
}
if (error?.code === 'invalid_request' || error?.code === 'confirmation_required') {
return sendError(res, req, 400, error.code, error.message);
}
return mindSpaceError(res, req, error);
}
});
api.get('/mindspace/v1/pages/:pageId/thumbnail', async (req, res) => {
if (!mindSpacePages) return res.status(503).json({ message: 'MindSpace 未启用' });
try {
@@ -3426,6 +3511,24 @@ api.post('/mindspace/v1/pages/:pageId/thumbnail/regenerate', async (req, res) =>
}
});
api.post('/mindspace/v1/pages/:pageId/rewrite-download-links', async (req, res) => {
if (!mindSpacePages) return res.status(503).json({ message: 'MindSpace 未启用' });
try {
const content = String(req.body?.content ?? '').trim();
if (!content) {
throw Object.assign(new Error('缺少页面内容'), { code: 'invalid_page_input' });
}
const html = await mindSpacePages.rewriteHtmlDownloadLinksForPage(
req.currentUser.id,
req.params.pageId,
content,
);
return sendData(res, req, { html });
} catch (error) {
return mindSpaceError(res, req, error);
}
});
api.get('/mindspace/v1/pages/:pageId/preview', async (req, res) => {
if (!mindSpacePages) return res.status(503).json({ message: 'MindSpace 未启用' });
try {
@@ -4319,6 +4422,14 @@ function extractOgImageUrl(html) {
);
}
function extractShareMetaFromPageHtml(html) {
const signals = extractCoverSignals(String(html ?? ''));
return {
title: detectPublishedPageTitle(html),
subtitle: signals.subtitle,
};
}
function appendQueryParam(url, key, value) {
const separator = url.includes('?') ? '&' : '?';
return `${url}${separator}${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
@@ -4738,6 +4849,7 @@ function sendPublishedPage(req, res, result, { embed = false, raw = false } = {}
pageUrl,
pageDirUrl,
fallbackImageUrl: extractOgImageUrl(html),
meta: extractShareMetaFromPageHtml(html),
});
if (wechatShare) shellHtml = injectWechatShareBridge(shellHtml, { pageUrl });
} catch {
@@ -5282,6 +5394,47 @@ app.use(
'/plaza-covers',
express.static(path.join(__dirname, 'public/plaza-covers'), { maxAge: '7d' }),
);
app.use('/brand', express.static(path.join(__dirname, 'public/brand'), { maxAge: '7d' }));
app.get('/dev/wechat-share-preview', async (req, res) => {
try {
const rawTarget = String(req.query.url ?? req.query.path ?? '').trim();
if (!rawTarget) {
return res.status(400).type('text/plain; charset=utf-8').send('缺少 url 参数,例如 ?url=/MindSpace/john/public/demo.html');
}
const origin = resolveRequestOrigin(req) || `http://${HOST}:${PORT}`;
const targetUrl = rawTarget.startsWith('http') ? new URL(rawTarget) : new URL(rawTarget.startsWith('/') ? rawTarget : `/${rawTarget}`, origin);
const upstream = await fetch(targetUrl.toString(), {
headers: {
'user-agent': req.get('user-agent') || 'tkmind-wechat-share-preview',
accept: 'text/html,*/*',
},
redirect: 'follow',
});
if (!upstream.ok) {
return res.status(upstream.status).type('text/plain; charset=utf-8').send(`页面请求失败:HTTP ${upstream.status}`);
}
let html = await upstream.text();
const pageUrl = targetUrl.toString().split('#')[0];
const pageDirUrl = `${pageUrl.slice(0, pageUrl.lastIndexOf('/') + 1)}`;
html = injectOgTags(html, { origin: targetUrl.origin, pageUrl, pageDirUrl });
const preview = extractSharePreviewMeta(html, {
origin: targetUrl.origin,
pageUrl,
pageDirUrl,
});
res.set('Content-Type', 'text/html; charset=utf-8');
res.set('Cache-Control', 'no-store');
return res.send(
renderWechatSharePreviewHtml(preview, {
note: '此预览读取页面最终 HTML 中的 Open Graph 标签,可用来对照微信里粘贴链接后的卡片效果。',
}),
);
} catch (err) {
return res.status(500).type('text/plain; charset=utf-8').send(String(err?.message ?? err));
}
});
app.use('/dev', express.static(path.join(__dirname, 'public/dev'), { maxAge: 0 }));
app.get(/^\/MP_verify_[A-Za-z0-9]+\.txt$/, (req, res) => {
const fileName = path.basename(req.path);
+10
View File
@@ -78,6 +78,16 @@ description: 在专属 MindSpace 目录生成可公开访问的静态 HTML 报
本地对比示例:`node scripts/thumbnail-preview-demo.mjs` → `/thumbnail-demo/`
## 平台页脚标记(必须)
页脚平台联系行**必须**使用 `data-mindspace-page-tag="platform-brand"`,且邮箱/域名只用 **tkmind.cn**(如 `contact@tkmind.cn`),**禁止** `tkmind.ai`:
```html
<p data-mindspace-page-tag="platform-brand">📧 contact@tkmind.cn</p>
```
带 `data-mindspace-page-tag` 的区域为平台固定信息:用户在编辑模式中不可见、不可改;预览与发布后正常显示。
## 附带文件下载(Word / PDF)
- 二进制文件用 `docx-generate` 脚本或平台允许的方式**单独生成**,保存到 `public/`(或 `oa/` 再复制到 `public/`)
+70 -29
View File
@@ -61,6 +61,7 @@ import { normalizeConversationMessages, normalizeUserMessageForApi } from '../ut
const API = '/api';
const DEFAULT_API_TIMEOUT_MS = 20_000;
const AGENT_CONNECT_TIMEOUT_MS = 60_000;
const AGENT_RUNS_PATH = '/agent/runs';
export type AgentRun = {
@@ -184,10 +185,14 @@ function notifyUnauthorized() {
unauthorizedHandler();
}
async function fetchWithTimeout(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
async function fetchWithTimeout(
input: RequestInfo | URL,
init?: RequestInit,
timeoutMs = DEFAULT_API_TIMEOUT_MS,
): Promise<Response> {
const controller = new AbortController();
const upstreamSignal = init?.signal;
const timeout = window.setTimeout(() => controller.abort(), DEFAULT_API_TIMEOUT_MS);
const timeout = window.setTimeout(() => controller.abort(), timeoutMs);
const abortFromUpstream = () => controller.abort();
if (upstreamSignal) {
@@ -267,16 +272,24 @@ function sanitizeSessionEvent(event: SessionEvent): SessionEvent {
return { ...event, error: sanitizeUserFacingErrorMessage(event.error) };
}
async function apiFetch<T>(path: string, init?: RequestInit): Promise<T> {
async function apiFetch<T>(
path: string,
init?: RequestInit,
options?: { timeoutMs?: number },
): Promise<T> {
let res: Response;
try {
res = await fetchWithTimeout(`${API}${path}`, {
...init,
headers: {
'Content-Type': 'application/json',
...init?.headers,
res = await fetchWithTimeout(
`${API}${path}`,
{
...init,
headers: {
'Content-Type': 'application/json',
...init?.headers,
},
},
});
options?.timeoutMs,
);
} catch (err) {
throw new ApiError(0, formatNetworkError(err));
}
@@ -313,10 +326,14 @@ async function apiFetch<T>(path: string, init?: RequestInit): Promise<T> {
}
export async function startSession(): Promise<Session> {
return apiFetch<Session>('/agent/start', {
method: 'POST',
body: JSON.stringify({}),
});
return apiFetch<Session>(
'/agent/start',
{
method: 'POST',
body: JSON.stringify({}),
},
{ timeoutMs: AGENT_CONNECT_TIMEOUT_MS },
);
}
export async function bootstrapProjectMemory(
@@ -929,6 +946,7 @@ export async function saveChatMessageAsPage(input: {
selectedLinkIndex?: number;
acknowledgedFindingIds?: string[];
replacePageId?: string;
saveAsNew?: boolean;
}): Promise<ChatSaveResult> {
const result = await apiFetch<{ data: ChatSaveResult }>(
'/mindspace/v1/pages/save-from-chat',
@@ -945,6 +963,7 @@ export async function saveChatMessageAsPage(input: {
acknowledged_finding_ids: input.acknowledgedFindingIds,
page_type: input.templateId === 'report' ? 'report' : 'article',
replace_page_id: input.replacePageId,
save_as_new: input.saveAsNew ?? false,
}),
},
);
@@ -1018,6 +1037,20 @@ export async function updateMindSpacePage(
return result.data;
}
export async function rewriteMindSpacePageDownloadLinks(
pageId: string,
content: string,
): Promise<string> {
const result = await apiFetch<{ data: { html: string } }>(
`/mindspace/v1/pages/${encodeURIComponent(pageId)}/rewrite-download-links`,
{
method: 'POST',
body: JSON.stringify({ content }),
},
);
return result.data.html;
}
export async function fetchMindSpacePageDraftPreview(
pageId: string,
input: {
@@ -2112,14 +2145,18 @@ export async function resumeSession(
sessionId: string,
options?: { skipReconcile?: boolean },
): Promise<Session> {
const result = await apiFetch<{ session: Session }>('/agent/resume', {
method: 'POST',
body: JSON.stringify({
session_id: sessionId,
load_model_and_extensions: true,
...(options?.skipReconcile ? { skip_reconcile: true } : {}),
}),
});
const result = await apiFetch<{ session: Session }>(
'/agent/resume',
{
method: 'POST',
body: JSON.stringify({
session_id: sessionId,
load_model_and_extensions: true,
...(options?.skipReconcile ? { skip_reconcile: true } : {}),
}),
},
{ timeoutMs: AGENT_CONNECT_TIMEOUT_MS },
);
return result.session;
}
@@ -2211,14 +2248,18 @@ export async function createAgentRun(
requestId: string,
userMessage: Message,
): Promise<AgentRun> {
const result = await apiFetch<{ run: AgentRun }>(AGENT_RUNS_PATH, {
method: 'POST',
body: JSON.stringify({
session_id: sessionId,
request_id: requestId,
user_message: normalizeUserMessageForApi(userMessage),
}),
});
const result = await apiFetch<{ run: AgentRun }>(
AGENT_RUNS_PATH,
{
method: 'POST',
body: JSON.stringify({
session_id: sessionId,
request_id: requestId,
user_message: normalizeUserMessageForApi(userMessage),
}),
},
{ timeoutMs: AGENT_CONNECT_TIMEOUT_MS },
);
return result.run;
}
+7
View File
@@ -0,0 +1,7 @@
export function ChatLoadingSpinner({ className = '' }: { className?: string }) {
return (
<span className={`chat-loading-spinner${className ? ` ${className}` : ''}`} aria-hidden="true">
<span />
</span>
);
}
+13 -4
View File
@@ -9,6 +9,7 @@ import {
} from '../utils/imageUpload';
import { AvatarPicker } from './AvatarPicker';
import { ChatSkillPicker } from './ChatSkillPicker';
import { ChatLoadingSpinner } from './ChatLoadingSpinner';
import { DesignSkillPanel } from './DesignSkillPanel';
import { MessageList } from './MessageList';
import { PageSaveDialog } from './PageSaveDialog';
@@ -292,13 +293,15 @@ export function ChatPanel({
const placeholder = offlineBlocked
? '网络断开,恢复连接后可继续输入'
: chatState === 'connecting'
: compact
? '随时召唤你的小助手吧'
: randomPrompt;
const connectStatusText =
chatState === 'connecting'
? '正在连接会话…'
: chatState === 'waiting'
? '消息已收到,正在连接后台…'
: compact
? '随时召唤你的小助手吧'
: randomPrompt;
: null;
const mergeVoiceText = (spoken: string) => {
const base = voiceBaseRef.current.trimEnd();
@@ -609,6 +612,12 @@ export function ChatPanel({
)}
<footer className={compact ? 'space-chat-panel-footer' : `footer${showHomeWelcome ? ' footer-home' : ''}`}>
{connectStatusText && (
<div className="chat-connect-status" role="status" aria-live="polite">
<ChatLoadingSpinner />
<span>{connectStatusText}</span>
</div>
)}
{voiceNotice && (
<div className="voice-notice" role="status">
{voiceNotice}
+6 -1
View File
@@ -8,6 +8,7 @@ import { BalanceRing } from './BalanceRing';
import { HistorySidebar } from './HistorySidebar';
import { TKMindAvatar } from './TKMindAvatar';
import { ChatPanel } from './ChatPanel';
import { ChatLoadingSpinner } from './ChatLoadingSpinner';
import { WechatBindPrompt } from './WechatBindPrompt';
import { WechatAccountButton } from './WechatAccountButton';
import { ChatHeaderMoreMenu } from './ChatHeaderMoreMenu';
@@ -90,6 +91,7 @@ export function ChatView({
setSidebarOpen(false);
};
const isConnectingTitle = !session && chatState !== 'idle';
const sessionTitle = session
? getSessionDisplayName(session)
: chatState === 'idle'
@@ -145,7 +147,10 @@ export function ChatView({
<TKMindAvatar size="sm" className="header-brand-avatar" />
<div>
<div className="header-title">{user?.displayName ?? 'TKMind'}</div>
<div className="header-sub">{sessionTitle}</div>
<div className="header-sub">
{isConnectingTitle ? <ChatLoadingSpinner /> : null}
<span>{sessionTitle}</span>
</div>
</div>
</div>
<div className="header-actions header-actions-desktop">
+3 -3
View File
@@ -1,7 +1,7 @@
import { useRef, useState } from 'react';
import { useUserAvatar } from '../hooks/useUserAvatar';
import type { Message } from '../types';
import { getDisplayText, getImageUrls, getRenderableImageUrls, getThinking } from '../utils/message';
import { getDisplayText, getImageUrls, getRenderableImageUrls, getThinking, shouldShowChatMessage } from '../utils/message';
import { getMessageSaveActions } from '../utils/messageSave';
import { renderMarkdown } from '../utils/markdown';
import { filterText } from '../utils/wordFilter';
@@ -365,8 +365,8 @@ export function MessageList({
return (
<div className="message-list">
{messages.length === 0 && <ChatWelcomePanel compact={compact} />}
{messages.map((message, index) => {
const prev = index > 0 ? messages[index - 1] : undefined;
{messages.filter(shouldShowChatMessage).map((message, index, visibleMessages) => {
const prev = index > 0 ? visibleMessages[index - 1] : undefined;
const showTime = shouldShowTimestamp(message.created, prev?.created);
const anchorId = message.id ?? `msg-${index}-${message.role}-${message.created}`;
return (
+8 -22
View File
@@ -35,8 +35,7 @@ import { MindSpacePagePreviewPanel } from './MindSpacePagePreviewPanel';
import { MindSpaceModal } from './MindSpaceModal';
import { MindSpaceDeletePlazaOption } from './MindSpaceDeletePlazaOption';
import { MindSpacePublishSuccess } from './MindSpacePublishSuccess';
import { ShareSheet } from './ShareSheet';
import { buildPageSharePayload, type SharePayload } from '../utils/shareChannels';
import { prepareHtmlPageBrandMarkers } from '../../mindspace-page-tag.mjs';
type AccessMode = MindSpacePublishCheck['accessMode'];
@@ -271,7 +270,6 @@ export function MindSpacePageDetail({
const [deletePreview, setDeletePreview] = useState<MindSpacePageDeletePreview | null>(null);
const [deletePreviewLoading, setDeletePreviewLoading] = useState(false);
const [deleteRemoveFromPlaza, setDeleteRemoveFromPlaza] = useState(false);
const [sharePayload, setSharePayload] = useState<SharePayload | null>(null);
const [previewFrame, setPreviewFrame] = useState<HTMLIFrameElement | null>(null);
const [isExportingImage, setIsExportingImage] = useState(false);
const [exportFeedback, setExportFeedback] = useState<string | null>(null);
@@ -315,13 +313,17 @@ export function MindSpacePageDetail({
setError(null);
try {
const next = await getMindSpacePage(pageId);
const pageContent =
next.contentFormat === 'html'
? prepareHtmlPageBrandMarkers(next.content ?? '')
: (next.content ?? '');
setPage(next);
resetDraft({
title: next.title,
summary: next.summary,
content: next.content ?? '',
content: pageContent,
});
setPreviewContent(next.content ?? '');
setPreviewContent(pageContent);
setPreviewRefreshPending(false);
setPreviewKey((value) => value + 1);
setTemplateId(next.templateId);
@@ -773,13 +775,6 @@ export function MindSpacePageDetail({
setPreviewRefreshPending(false);
};
const handleOpenShareSheet = () => {
if (!page) return;
const shareUrl = page.publication ? resolvePublicPageUrl(page.publication.publicUrl) : undefined;
setSharePayload(buildPageSharePayload(page, shareUrl));
setExportFeedback(null);
};
const handleExportPageImage = async () => {
if (!page) return;
setIsExportingImage(true);
@@ -897,9 +892,6 @@ export function MindSpacePageDetail({
</div>
</div>
<div className="mindspace-page-floating-actions">
<button type="button" className="mindspace-page-floating-action" onClick={handleOpenShareSheet}>
分享
</button>
<button
type="button"
className="mindspace-page-floating-action is-secondary"
@@ -1445,16 +1437,10 @@ export function MindSpacePageDetail({
))}
</section>
</section>
{sharePayload ? (
<ShareSheet
payload={sharePayload}
publication={page?.publication}
onClose={() => setSharePayload(null)}
/>
) : null}
{fullscreenPreviewOpen && page && page.contentFormat === 'html' ? (
<MindSpacePageFullscreenPreview
pageId={page.id}
pageTitle={title || page.title}
title={title}
content={content}
@@ -1,4 +1,5 @@
import { useEffect, useRef, useState } from 'react';
import { rewriteMindSpacePageDownloadLinks } from '../api/client';
import { bindIframeDocumentHeightSync, measureDocumentHeight } from '../utils/iframeContentHeight';
import {
buildEditablePreviewDocument,
@@ -6,6 +7,7 @@ import {
} from '../utils/mindspaceVisualEditor';
export function MindSpacePageEditablePreviewFrame({
pageId,
title,
content,
reloadKey,
@@ -18,6 +20,7 @@ export function MindSpacePageEditablePreviewFrame({
onFrameReady,
onContentChange,
}: {
pageId: string;
title: string;
content: string;
reloadKey: number;
@@ -34,6 +37,7 @@ export function MindSpacePageEditablePreviewFrame({
const frameClassName =
className ?? (compact ? 'page-save-mini-page-frame' : 'mindspace-page-preview-frame');
const [previewFailed, setPreviewFailed] = useState(false);
const [displayContent, setDisplayContent] = useState(content);
const [srcdoc, setSrcdoc] = useState(() => buildEditablePreviewDocument(content));
const [height, setHeight] = useState(minHeight);
const iframeContentRef = useRef(content);
@@ -41,19 +45,30 @@ export function MindSpacePageEditablePreviewFrame({
const syncHeightRef = useRef<(() => void) | null>(null);
useEffect(() => {
iframeContentRef.current = content;
setSrcdoc(buildEditablePreviewDocument(content));
setPreviewFailed(false);
setHeight(minHeight);
}, [reloadKey]);
let cancelled = false;
const timer = window.setTimeout(() => {
void rewriteMindSpacePageDownloadLinks(pageId, content)
.then((html) => {
if (cancelled) return;
setDisplayContent(html);
})
.catch(() => {
if (cancelled) return;
setDisplayContent(content);
});
}, 120);
return () => {
cancelled = true;
window.clearTimeout(timer);
};
}, [pageId, content, reloadKey]);
useEffect(() => {
if (content === iframeContentRef.current) return;
iframeContentRef.current = content;
setSrcdoc(buildEditablePreviewDocument(content));
iframeContentRef.current = displayContent;
setSrcdoc(buildEditablePreviewDocument(displayContent));
setPreviewFailed(false);
setHeight(minHeight);
}, [content, minHeight]);
}, [displayContent, reloadKey, minHeight]);
useEffect(() => {
const onMessage = (event: MessageEvent) => {
@@ -3,6 +3,7 @@ import { MindSpacePageEditablePreviewFrame } from './MindSpacePageEditablePrevie
import type { MindSpacePreviewChangeHighlight } from './MindSpacePageDraftPreviewFrame';
export function MindSpacePageFullscreenPreview({
pageId,
pageTitle,
title,
content,
@@ -17,6 +18,7 @@ export function MindSpacePageFullscreenPreview({
refreshPending = false,
onContentChange,
}: {
pageId: string;
pageTitle: string;
title: string;
content: string;
@@ -101,6 +103,7 @@ export function MindSpacePageFullscreenPreview({
<div className="mindspace-page-fullscreen-preview-body">
<MindSpacePageEditablePreviewFrame
pageId={pageId}
title={title}
content={content}
reloadKey={reloadKey}
@@ -170,6 +170,7 @@ export function MindSpacePagePreviewPanel({
</p>
<div className="page-save-mini-page">
<MindSpacePageEditablePreviewFrame
pageId={pageId}
title={title}
content={content}
reloadKey={reloadKey}
+30 -33
View File
@@ -2928,20 +2928,30 @@ export function MindSpaceView({
</>
)}
{previewAsset && previewAssetFrame && (
{previewAsset && (isImageAsset(previewAsset) || previewAssetFrame) && (
<div className="mindspace-asset-preview-backdrop" role="presentation">
<section className="mindspace-asset-preview-panel" role="dialog" aria-modal="true">
<div className="mindspace-asset-preview-bar">
<strong>文件预览</strong>
<strong>{previewAsset.displayName || '文件预览'}</strong>
<button type="button" onClick={() => setPreviewAssetId(null)}>
关闭
</button>
</div>
<iframe
title="资产预览"
src={previewAssetFrame.src}
{...(previewAssetFrame.sandbox ? { sandbox: previewAssetFrame.sandbox } : {})}
/>
{isImageAsset(previewAsset) ? (
<img
className="mindspace-asset-preview-image"
src={buildAssetImageUrl(previewAsset)}
alt={previewAsset.displayName}
/>
) : (
previewAssetFrame && (
<iframe
title="资产预览"
src={previewAssetFrame.src}
{...(previewAssetFrame.sandbox ? { sandbox: previewAssetFrame.sandbox } : {})}
/>
)
)}
</section>
</div>
)}
@@ -2957,33 +2967,13 @@ export function MindSpaceView({
</div>
<span>先从成果开始,目录稍后再整理</span>
</div>
<div className="mindspace-grid">
{space.categories
.filter((category) => ['oa', 'public'].includes(category.code))
.map((category) => (
<article
className={`mindspace-card mindspace-card-${category.code}`}
key={category.id}
>
<div className="mindspace-card-top">
<span className="mindspace-card-code">{category.code.toUpperCase()}</span>
<span>{category.itemCount} 项</span>
</div>
<h3>{category.name}</h3>
<p>{CATEGORY_DESCRIPTIONS[category.code]}</p>
{CATEGORY_ACTIONS[category.code] && (
<button type="button" onClick={() => void openCategory(category)}>
{CATEGORY_ACTIONS[category.code]}
</button>
)}
</article>
))}
<div className="mindspace-grid-secondary">
<div className="mindspace-category-layout">
<div className="mindspace-grid-zones">
{space.categories
.filter((category) => category.code === 'draft')
.filter((category) => ['oa', 'public', 'archive'].includes(category.code))
.map((category) => (
<article
className={`mindspace-card mindspace-card-${category.code}`}
className={`mindspace-card mindspace-card-compact mindspace-card-${category.code}`}
key={category.id}
>
<div className="mindspace-card-top">
@@ -2999,11 +2989,13 @@ export function MindSpaceView({
)}
</article>
))}
</div>
<div className="mindspace-grid-secondary">
{space.categories
.filter((category) => category.code === 'archive')
.filter((category) => category.code === 'draft')
.map((category) => (
<article
className={`mindspace-card mindspace-card-${category.code}`}
className={`mindspace-card mindspace-card-compact mindspace-card-${category.code}`}
key={category.id}
>
<div className="mindspace-card-top">
@@ -3012,6 +3004,11 @@ export function MindSpaceView({
</div>
<h3>{category.name}</h3>
<p>{CATEGORY_DESCRIPTIONS[category.code]}</p>
{CATEGORY_ACTIONS[category.code] && (
<button type="button" onClick={() => void openCategory(category)}>
{CATEGORY_ACTIONS[category.code]}
</button>
)}
</article>
))}
</div>
+2 -1
View File
@@ -155,6 +155,7 @@ export function PageSaveDialog({
categoryCode,
selectedLinkIndex,
replacePageId,
saveAsNew: duplicateResolved === 'new',
});
if (result.kind === 'page') {
setSaveNotice(replacePageId ? '已替换原有页面' : `已保存到${CATEGORY_LABELS[categoryCode]}`);
@@ -196,7 +197,7 @@ export function PageSaveDialog({
{!analysisLoading && showDuplicatePrompt && (
<div className="page-save-duplicate">
<p>此消息已保存为「{existingPage!.title}」,请选择操作:</p>
<p>此页面已保存为「{existingPage!.title}」,请选择操作:</p>
<div className="page-save-duplicate-actions">
<button
type="button"
+3
View File
@@ -51,6 +51,7 @@ export function SpaceChatPanel({
uploadChatImage,
retryConnect,
} = chat;
const { capabilities, grantedSkills } = mainChat;
useEffect(() => {
if (!open) return;
@@ -134,6 +135,8 @@ export function SpaceChatPanel({
chatState={chatState}
pendingTool={pendingTool}
session={session}
capabilities={capabilities ?? undefined}
grantedSkills={grantedSkills}
onSubmit={(text, imageUrls, previewImageUrls) =>
chatBridge
? void submit(text, context, imageUrls, previewImageUrls)
+92 -14
View File
@@ -116,6 +116,61 @@ function isAmbiguousReplySubmitError(err: unknown) {
return err.status === 0 || err.status === 409 || err.status >= 500;
}
const SESSION_LIST_RETRY_ATTEMPTS = 3;
const SESSION_LIST_RETRY_DELAY_MS = 450;
function isTransientSessionListError(err: unknown) {
return err instanceof ApiError && (err.status === 0 || err.status >= 502);
}
function sessionListFailureMessage(err: unknown, action: 'refresh' | 'load-more') {
const fallback =
action === 'refresh' ? '历史列表暂时无法加载,请稍后重试' : '暂时无法加载更多历史,请稍后重试';
if (!(err instanceof ApiError)) return fallback;
if (err.status === 0) {
return action === 'refresh'
? '无法连接后端,历史列表未刷新(聊天仍可使用)'
: '无法连接后端,暂时无法加载更多历史';
}
return err.message?.trim() || fallback;
}
async function fetchSessionListWithRetry(
options?: Parameters<typeof listSessions>[0],
): Promise<Awaited<ReturnType<typeof listSessions>>> {
let lastErr: unknown;
for (let attempt = 0; attempt < SESSION_LIST_RETRY_ATTEMPTS; attempt += 1) {
try {
return await listSessions(options);
} catch (err) {
lastErr = err;
if (!isTransientSessionListError(err) || attempt === SESSION_LIST_RETRY_ATTEMPTS - 1) break;
await new Promise((resolve) => window.setTimeout(resolve, SESSION_LIST_RETRY_DELAY_MS * (attempt + 1)));
}
}
throw lastErr;
}
function isTransientConnectError(err: unknown) {
if (!(err instanceof ApiError)) return false;
if (err.status === 0 || err.status >= 502) return true;
return /超时|timeout/i.test(err.message);
}
async function withTransientConnectRetry<T>(fn: () => Promise<T>, attempts = 3): Promise<T> {
let lastErr: unknown;
for (let attempt = 0; attempt < attempts; attempt += 1) {
try {
return await fn();
} catch (err) {
lastErr = err;
if (!isTransientConnectError(err) || attempt === attempts - 1) break;
await new Promise((resolve) => window.setTimeout(resolve, 600 * (attempt + 1)));
}
}
throw lastErr;
}
export function useTKMindChat(
user?: PortalUser | null,
onUserUpdate?: (user: PortalUser) => void,
@@ -437,7 +492,7 @@ export function useTKMindChat(
async (options?: { preserveExisting?: boolean; query?: string }): Promise<SessionSummary[]> => {
setSessionsLoading(true);
try {
const { items, page } = await listSessions({
const { items, page } = await fetchSessionListWithRetry({
limit: appConfig.sessionPageSize,
offset: 0,
query: options?.query ?? sessionSearchQueryRef.current,
@@ -456,8 +511,8 @@ export function useTKMindChat(
setSessions(merged);
return items;
} catch (err) {
if (err instanceof ApiError && err.status === 0) {
setError('网络不可用,无法刷新历史列表');
if (err instanceof ApiError || err instanceof Error) {
setNotice(sessionListFailureMessage(err, 'refresh'));
}
return [];
} finally {
@@ -471,7 +526,7 @@ export function useTKMindChat(
if (sessionsLoading || sessionsLoadingMore || !sessionsHasMore) return;
setSessionsLoadingMore(true);
try {
const { items, page } = await listSessions({
const { items, page } = await fetchSessionListWithRetry({
limit: appConfig.sessionPageSize,
offset: sessionsOffsetRef.current,
query: sessionSearchQueryRef.current,
@@ -483,8 +538,8 @@ export function useTKMindChat(
setSessionsHasMore(merged.length < total);
setSessions(merged);
} catch (err) {
if (err instanceof ApiError && err.status === 0) {
setError('网络不可用,无法继续加载历史列表');
if (isTransientSessionListError(err) || err instanceof ApiError) {
setNotice(sessionListFailureMessage(err, 'load-more'));
}
} finally {
setSessionsLoadingMore(false);
@@ -808,7 +863,12 @@ export function useTKMindChat(
const connectSession = useCallback(
async (
sessionId: string,
options?: { showLoading?: boolean; skipResume?: boolean; seedSession?: Session },
options?: {
showLoading?: boolean;
skipResume?: boolean;
seedSession?: Session;
skipReconcile?: boolean;
},
) => {
const showLoading = options?.showLoading !== false;
const token = ++connectTokenRef.current;
@@ -825,20 +885,22 @@ export function useTKMindChat(
const resumed = options?.skipResume
? (options.seedSession ?? null)
: await resumeSession(sessionId);
: await withTransientConnectRetry(() =>
resumeSession(sessionId, {
skipReconcile: options?.skipReconcile ?? false,
}),
);
if (token !== connectTokenRef.current) return;
const knownSession = sessionsRef.current.find((s) => s.id === sessionId);
const hints = knownSession
? { messageCount: knownSession.message_count, updatedAt: knownSession.updated_at }
: undefined;
const { session: detail, messages: history, page } = await loadSessionDetail(
sessionId,
hints,
{
const { session: detail, messages: history, page } = await withTransientConnectRetry(() =>
loadSessionDetail(sessionId, hints, {
before: 0,
limit: appConfig.sessionMessagePageSize,
},
}),
);
if (token !== connectTokenRef.current) return;
@@ -957,7 +1019,10 @@ export function useTKMindChat(
if (cancelled) return;
if (restorableSessionId) {
try {
await connectSessionRef.current(restorableSessionId, { showLoading: false });
await connectSessionRef.current(restorableSessionId, {
showLoading: false,
skipReconcile: true,
});
} catch (err) {
if (err instanceof ApiError && (err.status === 403 || err.status === 404 || err.status === 400)) {
clearStoredSessionId(userRef.current?.id);
@@ -970,6 +1035,17 @@ export function useTKMindChat(
setMessageHistoryTotal(0);
setMessages([]);
setChatState('idle');
} else if (isTransientConnectError(err)) {
clearStoredSessionId(userRef.current?.id);
setSession(null);
messageHistoryLoadedCountRef.current = 0;
messageHistoryTotalRef.current = 0;
messageHistoryHasMoreRef.current = false;
setMessageHistoryHasMore(false);
setMessageHistoryTotal(0);
setMessages([]);
setChatState('idle');
setNotice('上次会话恢复超时,已为你准备新对话,可直接发送消息');
} else {
throw err;
}
@@ -1311,6 +1387,8 @@ export function useTKMindChat(
userMemoryLoading,
canUseProjectMemory,
canUseLongTermMemory,
capabilities,
grantedSkills,
submit,
stop,
approveTool,
+141 -16
View File
@@ -668,6 +668,9 @@ body,
}
.header-sub {
display: flex;
align-items: center;
gap: 6px;
font-size: 12px;
color: var(--color-text-muted);
margin-top: 2px;
@@ -676,6 +679,43 @@ body,
white-space: nowrap;
}
.chat-loading-spinner {
width: 14px;
height: 14px;
display: inline-flex;
align-items: center;
justify-content: center;
flex: 0 0 14px;
color: var(--color-text-muted);
}
.chat-loading-spinner > span {
width: 10px;
height: 10px;
border-radius: 999px;
border: 1.5px solid currentColor;
border-right-color: transparent;
animation: chat-loading-spin 0.75s linear infinite;
}
@keyframes chat-loading-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.chat-connect-status {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
font-size: 13px;
color: var(--color-text-muted);
}
.ghost-btn,
.send-btn,
.danger-btn,
@@ -1219,6 +1259,7 @@ body,
border: 1px solid rgba(238, 176, 78, 0.3);
border-radius: 26px;
color: #18211d;
color-scheme: light;
background:
radial-gradient(circle at 100% 0, rgba(47, 111, 87, 0.16), transparent 20rem),
#f8f3e8;
@@ -1313,6 +1354,11 @@ body,
font: inherit;
}
.page-save-actions button:not(.page-save-primary) {
color: #52605a;
background: rgba(24, 33, 29, 0.08);
}
.page-save-primary {
color: #fffaf0;
background: #2f6f57;
@@ -1586,6 +1632,7 @@ body,
border-radius: 10px;
border: 1px solid #c0cbc4;
background: #fff;
color: #18211d;
font-size: 13px;
font-weight: 600;
cursor: pointer;
@@ -5656,24 +5703,51 @@ body,
line-height: 1.7;
}
.mindspace-category-layout {
display: grid;
gap: 10px;
}
.mindspace-grid-zones {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 10px;
}
.mindspace-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 10px;
}
.mindspace-card {
min-height: auto;
padding: 14px;
border: 1px solid rgba(24, 33, 29, 0.12);
border-radius: 14px;
background: rgba(255, 252, 244, 0.82);
.mindspace-card-compact {
padding: 12px 14px;
border-radius: 12px;
}
.mindspace-card-compact h3 {
margin: 8px 0 4px;
font-size: 17px;
}
.mindspace-card-compact p {
font-size: 11px;
line-height: 1.45;
}
.mindspace-card-compact button {
margin-top: 8px;
padding: 5px 8px;
font-size: 11px;
}
.mindspace-card-compact .mindspace-card-top {
font-size: 11px;
}
.mindspace-grid-secondary {
grid-column: 1 / -1;
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
grid-template-columns: minmax(0, 1fr);
gap: 10px;
align-items: stretch;
}
@@ -5684,6 +5758,14 @@ body,
min-height: auto;
}
.mindspace-card {
min-height: auto;
padding: 14px;
border: 1px solid rgba(24, 33, 29, 0.12);
border-radius: 14px;
background: rgba(255, 252, 244, 0.82);
}
.mindspace-agent-jobs-module {
display: flex;
flex-direction: column;
@@ -7353,7 +7435,8 @@ body,
background: #f8f3e8;
}
.mindspace-asset-preview-panel iframe {
.mindspace-asset-preview-panel iframe,
.mindspace-asset-preview-panel .mindspace-asset-preview-image {
flex: 1;
width: 100%;
min-height: 0;
@@ -7362,6 +7445,11 @@ body,
background: #fff;
}
.mindspace-asset-preview-panel .mindspace-asset-preview-image {
object-fit: contain;
background: #0b100e;
}
.mindspace-asset-preview-bar {
display: flex;
align-items: center;
@@ -8154,12 +8242,50 @@ body,
font-size: clamp(28px, 4vw, 44px);
}
.mindspace-category-layout {
gap: 14px;
}
.mindspace-grid-zones {
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 12px;
}
.mindspace-grid {
grid-template-columns: repeat(12, 1fr);
gap: 18px;
}
.mindspace-card {
.mindspace-card.mindspace-card-compact,
.mindspace-grid-zones .mindspace-card,
.mindspace-grid-secondary .mindspace-card-compact {
grid-column: auto;
min-height: auto;
padding: 12px 14px;
border-radius: 12px;
}
.mindspace-card-compact h3 {
margin: 8px 0 4px;
font-size: 17px;
}
.mindspace-card-compact p {
font-size: 11px;
line-height: 1.45;
}
.mindspace-card-compact button {
margin-top: 8px;
padding: 5px 8px;
font-size: 11px;
}
.mindspace-card-compact .mindspace-card-top {
font-size: 11px;
}
.mindspace-card:not(.mindspace-card-compact) {
grid-column: span 4;
min-height: 220px;
padding: 24px;
@@ -8167,14 +8293,13 @@ body,
}
.mindspace-grid-secondary {
grid-column: span 12;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 18px;
grid-template-columns: minmax(0, 1fr);
gap: 14px;
}
.mindspace-grid-secondary .mindspace-card,
.mindspace-grid-secondary .mindspace-agent-jobs-module {
min-height: 180px;
min-height: auto;
}
.mindspace-agent-jobs-module {
@@ -8187,12 +8312,12 @@ body,
font-size: 24px;
}
.mindspace-card h3 {
.mindspace-card:not(.mindspace-card-compact) h3 {
margin: 30px 0 8px;
font-size: 28px;
}
.mindspace-card button {
.mindspace-card:not(.mindspace-card-compact) button {
margin-top: 8px;
padding: 9px 14px;
}
+13 -3
View File
@@ -1,7 +1,7 @@
import type { Message, MessageContent } from '../types';
import { mergeMessageContent } from '../../message-stream.mjs';
import { mergeConversationSnapshot as mergeConversationSnapshotCore } from '../../chat-finish-sync.mjs';
import { deriveUserFacingText } from '../../conversation-display.mjs';
import { deriveUserFacingText, deriveAssistantFacingText } from '../../conversation-display.mjs';
import { stripUserAddressPrefix } from './userAddress';
const IMAGE_URL_LINE_RE = /^\[图片\d+]: (.+)$/;
@@ -182,12 +182,22 @@ export function getDisplayText(message: Message): string {
return stripImageUrlLines(deriveUserFacingText(raw));
}
if ('displayText' in message.metadata) {
return stripImageUrlLines(message.metadata.displayText ?? '');
return stripImageUrlLines(deriveAssistantFacingText(message.metadata.displayText ?? ''));
}
const systemText = getSystemNotificationText(message);
if (systemText) return systemText;
const visible = getVisibleText(message);
return visible;
return stripImageUrlLines(deriveAssistantFacingText(visible));
}
export function shouldShowChatMessage(message: Message): boolean {
if (message.role === 'user') return true;
if (getDisplayText(message).trim()) return true;
if (getThinking(message)) return true;
if (getSystemNotificationText(message)) return true;
return message.content.some(
(item) => item.type === 'toolRequest' || item.type === 'toolResponse' || item.type === 'actionRequired',
);
}
export function pushMessage(messages: Message[], incoming: Message): Message[] {
+49 -4
View File
@@ -1,3 +1,5 @@
import { prepareHtmlPageBrandMarkers } from '../../mindspace-page-tag.mjs';
export const MINDSPACE_PAGE_CONTENT_MESSAGE = 'mindspace:page-content';
const EDITOR_STYLE = `<style id="mindspace-visual-editor-style">
@@ -218,6 +220,23 @@ const EDITOR_STYLE = `<style id="mindspace-visual-editor-style">
background: rgba(24, 33, 29, 0.08);
cursor: pointer;
}
.mindspace-editor-legend[data-collapsed="true"] {
width: auto;
max-width: min(280px, calc(100vw - 24px));
gap: 0;
cursor: pointer;
}
.mindspace-editor-legend[data-collapsed="true"] .mindspace-editor-legend-body {
display: none;
}
.mindspace-editor-legend[data-collapsed="true"] .mindspace-editor-legend-title::after {
content: ' · 点击展开';
font-weight: 400;
color: #6d7771;
}
[data-mindspace-editing="true"] [data-mindspace-page-tag] {
display: none !important;
}
</style>`;
const EDITOR_SCRIPT = `<script id="mindspace-visual-editor-script">
@@ -226,9 +245,14 @@ const EDITOR_SCRIPT = `<script id="mindspace-visual-editor-script">
var TEXT_SELECTOR = 'h1,h2,h3,h4,h5,h6,p,li,td,th,figcaption,blockquote,span,a,label,small,strong,em,b,i,u,sub,sup,cite,time,address,dt,dd,button,.sub,.tagline,[class*="title"],[class*="subtitle"],[class*="desc"],[class*="text"]';
var BLOCK_CHILD_SELECTOR = 'div,section,article,header,footer,main,p,h1,h2,h3,h4,h5,h6,ul,ol,table,form,video,audio,iframe,svg,canvas';
var MEDIA_BLOCK_SELECTOR = 'img,video,iframe,svg,canvas,table,ul,ol,form';
var PAGE_TAG_SELECTOR = '[data-mindspace-page-tag]';
var popover = null;
var emitTimer = null;
function isProtectedNode(el) {
return !!(el && el.closest && el.closest(PAGE_TAG_SELECTOR));
}
function debounceEmit() {
if (emitTimer) window.clearTimeout(emitTimer);
emitTimer = window.setTimeout(emitChange, 350);
@@ -479,6 +503,7 @@ const EDITOR_SCRIPT = `<script id="mindspace-visual-editor-script">
var nodes = document.querySelectorAll('body, main, section, article, header, footer, div');
nodes.forEach(function (el) {
if (el.closest('.mindspace-editor-popover,.mindspace-editor-guide,.mindspace-editor-legend')) return;
if (isProtectedNode(el)) return;
if (el.isContentEditable) return;
var style = window.getComputedStyle(el);
var cls = String(el.className || '');
@@ -497,18 +522,22 @@ const EDITOR_SCRIPT = `<script id="mindspace-visual-editor-script">
function markMediaTargets() {
document.querySelectorAll('img').forEach(function (img) {
if (img.closest('.mindspace-editor-popover,.mindspace-editor-guide,.mindspace-editor-legend')) return;
if (isProtectedNode(img)) return;
markEditable(img, 'media', '点击换图片');
});
document.querySelectorAll('video').forEach(function (video) {
if (video.closest('.mindspace-editor-popover,.mindspace-editor-guide,.mindspace-editor-legend')) return;
if (isProtectedNode(video)) return;
markEditable(video, 'media', '点击换视频');
});
document.querySelectorAll('audio').forEach(function (audio) {
if (audio.closest('.mindspace-editor-popover,.mindspace-editor-guide,.mindspace-editor-legend')) return;
if (isProtectedNode(audio)) return;
markEditable(audio, 'media', '点击换音频');
});
document.querySelectorAll('iframe').forEach(function (iframe) {
if (iframe.closest('.mindspace-editor-popover,.mindspace-editor-guide,.mindspace-editor-legend')) return;
if (isProtectedNode(iframe)) return;
iframe.setAttribute('data-mindspace-media-target', 'true');
markEditable(iframe, 'media', '点击换嵌入');
});
@@ -517,6 +546,7 @@ const EDITOR_SCRIPT = `<script id="mindspace-visual-editor-script">
function markLinkTargets() {
document.querySelectorAll('a[href]').forEach(function (anchor) {
if (anchor.closest('.mindspace-editor-popover,.mindspace-editor-guide,.mindspace-editor-legend')) return;
if (isProtectedNode(anchor)) return;
anchor.setAttribute('data-mindspace-link-target', 'true');
markEditable(anchor, 'link', '双击改链接');
});
@@ -525,6 +555,7 @@ const EDITOR_SCRIPT = `<script id="mindspace-visual-editor-script">
function enableTextEditing() {
document.querySelectorAll(TEXT_SELECTOR).forEach(function (el) {
if (el.closest('.mindspace-editor-popover,.mindspace-editor-guide,.mindspace-editor-legend')) return;
if (isProtectedNode(el)) return;
if (el.querySelector && el.querySelector(MEDIA_BLOCK_SELECTOR)) return;
if (el.matches('a[data-mindspace-link-target="true"]')) return;
var textParent = el.closest('[data-mindspace-edit-kind="text"]');
@@ -536,6 +567,7 @@ const EDITOR_SCRIPT = `<script id="mindspace-visual-editor-script">
document.querySelectorAll('div').forEach(function (el) {
if (el.closest('.mindspace-editor-popover,.mindspace-editor-guide,.mindspace-editor-legend')) return;
if (isProtectedNode(el)) return;
if (el.hasAttribute('contenteditable')) return;
if (el.querySelector(BLOCK_CHILD_SELECTOR)) return;
if (el.querySelector(MEDIA_BLOCK_SELECTOR)) return;
@@ -550,16 +582,19 @@ const EDITOR_SCRIPT = `<script id="mindspace-visual-editor-script">
var legend = document.createElement('div');
legend.className = 'mindspace-editor-legend';
legend.innerHTML =
'<strong>编辑模式 · 可修改区域</strong>' +
'<strong class="mindspace-editor-legend-title">编辑模式 · 可修改区域</strong>' +
'<div class="mindspace-editor-legend-body">' +
'<div class="mindspace-editor-legend-items">' +
'<div class="mindspace-editor-legend-item"><span class="mindspace-editor-legend-swatch" data-kind="text"></span><span>蓝色虚线 = 文字,直接点击修改</span></div>' +
'<div class="mindspace-editor-legend-item"><span class="mindspace-editor-legend-swatch" data-kind="media"></span><span>橙色虚线 = 图片/音视频,点击替换</span></div>' +
'<div class="mindspace-editor-legend-item"><span class="mindspace-editor-legend-swatch" data-kind="link"></span><span>紫色虚线 = 链接,双击改地址</span></div>' +
'<div class="mindspace-editor-legend-item"><span class="mindspace-editor-legend-swatch" data-kind="background"></span><span>绿色虚线 = 背景区域,点击换背景</span></div>' +
'</div>' +
'<button type="button" class="mindspace-editor-legend-toggle" data-action="toggle-highlights">隐藏标记</button>';
'<button type="button" class="mindspace-editor-legend-toggle" data-action="toggle-highlights">隐藏标记</button>' +
'</div>';
document.body.appendChild(legend);
legend.querySelector('[data-action="toggle-highlights"]').addEventListener('click', function () {
legend.querySelector('[data-action="toggle-highlights"]').addEventListener('click', function (event) {
event.stopPropagation();
var hidden = document.body.getAttribute('data-mindspace-highlights') === 'off';
if (hidden) {
document.body.removeAttribute('data-mindspace-highlights');
@@ -569,12 +604,21 @@ const EDITOR_SCRIPT = `<script id="mindspace-visual-editor-script">
this.textContent = '显示标记';
}
});
legend.addEventListener('click', function () {
if (legend.getAttribute('data-collapsed') === 'true') {
legend.removeAttribute('data-collapsed');
}
});
window.setTimeout(function () {
legend.setAttribute('data-collapsed', 'true');
}, 5000);
}
function onClick(event) {
var target = event.target;
if (!(target instanceof Element)) return;
if (target.closest('.mindspace-editor-popover,.mindspace-editor-guide,.mindspace-editor-legend')) return;
if (isProtectedNode(target)) return;
if (target.tagName === 'IMG') {
event.preventDefault();
@@ -601,6 +645,7 @@ const EDITOR_SCRIPT = `<script id="mindspace-visual-editor-script">
function onDblClick(event) {
var target = event.target;
if (!(target instanceof Element)) return;
if (isProtectedNode(target)) return;
var anchor = target.closest('a[data-mindspace-link-target="true"]');
if (!anchor) return;
event.preventDefault();
@@ -644,7 +689,7 @@ export function stripPreviewEditCspMeta(html: string): string {
}
export function buildEditablePreviewDocument(html: string): string {
const source = String(html ?? '').trim();
const source = prepareHtmlPageBrandMarkers(String(html ?? '').trim());
const cleaned = stripPreviewEditCspMeta(source)
.replace(/<style id="mindspace-visual-editor-style">[\s\S]*?<\/style>/gi, '')
.replace(/<script id="mindspace-visual-editor-script">[\s\S]*?<\/script>/gi, '');
+1 -1
View File
@@ -1120,7 +1120,7 @@ export function createTkmindProxy({
try {
const upstream = await apiFetch(target, apiSecret, '/sessions', {
method: 'GET',
signal: AbortSignal.timeout(3000),
signal: AbortSignal.timeout(12_000),
});
const text = await upstream.text();
if (!upstream.ok) {
+32 -1
View File
@@ -166,8 +166,37 @@ function formatDateParts(now, timezone) {
};
}
function formatLocalClockParts(now, timezone) {
const formatter = new Intl.DateTimeFormat('en-GB', {
timeZone: timezone,
hour: '2-digit',
minute: '2-digit',
hour12: false,
});
const parts = Object.fromEntries(
formatter.formatToParts(new Date(now)).map((part) => [part.type, part.value]),
);
return {
hour: Number(parts.hour ?? 0),
minute: Number(parts.minute ?? 0),
};
}
export function resolveTimeOfDayPeriod(hour) {
const h = Number(hour);
if (h < 5) return { period: '凌晨', greeting: '你好' };
if (h < 9) return { period: '早上', greeting: '早上好' };
if (h < 12) return { period: '上午', greeting: '上午好' };
if (h < 14) return { period: '中午', greeting: '中午好' };
if (h < 18) return { period: '下午', greeting: '下午好' };
return { period: '晚上', greeting: '晚上好' };
}
export function renderCurrentTimeAnchor({ now = Date.now(), timezone = DEFAULT_TIMEZONE } = {}) {
const { year, month, day } = formatDateParts(now, timezone);
const { hour, minute } = formatLocalClockParts(now, timezone);
const { period, greeting } = resolveTimeOfDayPeriod(hour);
const clock = `${String(hour).padStart(2, '0')}:${String(minute).padStart(2, '0')}`;
const weekday = new Intl.DateTimeFormat('zh-CN', {
timeZone: timezone,
weekday: 'long',
@@ -177,7 +206,9 @@ export function renderCurrentTimeAnchor({ now = Date.now(), timezone = DEFAULT_T
'',
`- 当前时区:${timezone}`,
`- 当前日期:${year}-${month}-${day}(${weekday})`,
'- 回答中涉及“今天 / 明天 / 后天 / 周几”时,必须以上述日期为准继续推算,不要自行假设当前日期。',
`- 当前时刻:${clock}(${period})`,
`- 若需时段问候可参考:${greeting}(仅新会话开场或用户主动打招呼时使用,普通任务回复不要每条都加)`,
'- 回答中涉及“今天 / 明天 / 后天 / 周几 / 早上 / 下午 / 晚上”等时间表述时,必须以上述日期与时刻为准,禁止自行假设当前时间或使用 UTC 等其它时区。',
].join('\n');
}
+23
View File
@@ -9,6 +9,7 @@ import {
resolveCodeExecutorRouting,
renderCodeExecutorGuidance,
renderCurrentTimeAnchor,
resolveTimeOfDayPeriod,
suggestCodeExecutorForTask,
buildSessionMemoryEntries,
ensureUserMemoryProfile,
@@ -86,6 +87,8 @@ test('buildSessionMemoryEntries injects sandbox, guidance, and profile', () => {
assert.match(entries[1].content, /openhands/);
assert.equal(entries[2].title, 'TKMind 当前时间基准');
assert.match(entries[2].content, /2026-06-29(星期一)/);
assert.match(entries[2].content, /13:00(中午)/);
assert.match(entries[2].content, /若需时段问候可参考:中午好/);
assert.match(entries[3].content, /长期记忆/);
assert.match(entries[4].content, /Bob/);
});
@@ -111,6 +114,26 @@ test('renderCurrentTimeAnchor formats Shanghai weekday from exact date', () => {
});
assert.match(text, /2026-06-29(星期一)/);
assert.match(text, /Asia\/Shanghai/);
assert.match(text, /13:00(中午)/);
assert.match(text, /若需时段问候可参考:中午好/);
assert.match(text, /普通任务回复不要每条都加/);
});
test('renderCurrentTimeAnchor uses local morning greeting at 07:03 Shanghai', () => {
const text = renderCurrentTimeAnchor({
now: Date.UTC(2026, 5, 29, 23, 3, 0),
timezone: 'Asia/Shanghai',
});
assert.match(text, /2026-06-30(星期二)/);
assert.match(text, /07:03(早上)/);
assert.match(text, /若需时段问候可参考:早上好/);
});
test('resolveTimeOfDayPeriod maps hours to greeting labels', () => {
assert.deepEqual(resolveTimeOfDayPeriod(2), { period: '凌晨', greeting: '你好' });
assert.deepEqual(resolveTimeOfDayPeriod(7), { period: '早上', greeting: '早上好' });
assert.deepEqual(resolveTimeOfDayPeriod(10), { period: '上午', greeting: '上午好' });
assert.deepEqual(resolveTimeOfDayPeriod(20), { period: '晚上', greeting: '晚上好' });
});
test('renderMemoryStoreGuidance scopes memory to one user', () => {
+9 -1
View File
@@ -17,7 +17,7 @@ function renderBrandingBlock(userAddressName) {
- 你是 **TKMind** 助手;介绍产品时用 TKMind,不要称 goose、Goose、goosed
- 与用户对话时,用 **${name}** 称呼用户(可辅以「你/您」),**禁止**把用户叫作 TKMind
- 问候示例:「${name},下午好」——不要用「TKMind,下午好」
- 仅在开场或用户打招呼时使用时段问候,且须与「TKMind 当前时间基准」一致(如「${name},早上好」);普通回复直接作答,不要每条都加问候;禁止把用户叫作 TKMind
- 不要描述本工作区为「Rust goose 项目」或「goose AI 框架」
- 本工作区是 TKMind **MindSpace 用户空间**,用于文件管理与静态页面生成
`;
@@ -202,6 +202,13 @@ description: 在专属 MindSpace 目录生成可公开访问的静态 HTML 报
- 完整 \`<!DOCTYPE html>\`,\`lang="zh-CN"\`
- 移动端友好:\`<meta name="viewport" content="width=device-width, initial-scale=1">\`
- 深色/浅色与内容一致;主色在 CSS 与 mindspace-cover 中保持一致
- 页脚平台联系行**必须**使用 \`data-mindspace-page-tag="platform-brand"\` 标记,且邮箱/域名只用 **tkmind.cn**(如 \`contact@tkmind.cn\`),**禁止** \`tkmind.ai\`
\`\`\`html
<p data-mindspace-page-tag="platform-brand">📧 contact@tkmind.cn</p>
\`\`\`
- 带 \`data-mindspace-page-tag\` 的区域为平台固定信息:用户在编辑模式中不可见、不可改;预览与发布后正常显示
## 查找文件(CSV、文档等)
@@ -310,6 +317,7 @@ export function buildPublishConstraints({ slug, username, publicBaseUrl, publish
'- **禁止**:访问 `assets/` 内部路径、其它用户目录、主机绝对路径;禁止用公网 URL 列目录或读 CSV',
'- **路径规则**:只用相对路径;禁止 `../`;工作区外的路径会被系统拒绝(OS 层强制,非软约束)',
'- **生成页面(必须亲自完成)**:先 `load_skill` → `static-page-publish`,再用 `write_file`/`edit_file` 写入 `public/页面.html`',
'- **用户可见回复**:不要向用户复述 load_skill、技能更新、页脚标记、mindspace-cover 等内部实现;完成后直接给出页面链接或结果',
'- **禁止**用 shell 写入 HTML;**禁止**让用户「手动保存到 public 目录」或说「我无法生成页面」——除非 write_file 已失败并报告错误',
'- 完成后给出 Markdown 可点击公网链接 `[标题](URL)`;写入 `public/页面.html` 时 URL 为 `.../MindSpace/<用户ID>/public/页面.html`',
`- 发布技能:\`${PUBLISH_SKILL_NAME}\`(生成页面前应 load_skill)`,
+1 -1
View File
@@ -24,7 +24,7 @@ function renderUserSpaceBrandingBlock(userAddressName) {
- 你是 **TKMind** 助手;介绍产品时用 TKMind,不要称 goose、Goose、goosed
- 与用户对话时,用 **${name}** 称呼用户(可辅以「你/您」),**禁止**把用户叫作 TKMind
- 问候示例:「${name},下午好」——不要用「TKMind,下午好」
- 仅在开场或用户打招呼时使用时段问候,且须与「TKMind 当前时间基准」一致(如「${name},早上好」);普通回复直接作答,不要每条都加问候;禁止把用户叫作 TKMind
- 不要描述本工作区为「Rust goose 项目」或「goose AI 框架」
- 本工作区是 TKMind **MindSpace 用户空间**,用于 OA/公开文件管理与静态页面生成
`;
+2
View File
@@ -19,6 +19,8 @@ export default defineConfig(({ mode }) => {
'/auth': portalTarget,
'/admin-api': adminTarget,
'/MindSpace': portalTarget,
'/dev': portalTarget,
'/brand': portalTarget,
'^/u/': portalTarget,
'^/s/': portalTarget,
'^/user/': portalTarget,