Files
DeepTutor/web/lib/reading-video-sources.ts
Bingxi Zhao (Frank) ce7c2dc2dc release: v1.6.1 — one key, one navigator, one sidebar you arrange
Model configuration was one layer replicated seven times: every service
stored its own profile with its own credentials, so a single OpenRouter
key had to be pasted into five separate pages. This release splits that
in two — a **connection** holds one vendor credential and links it down
to every service that vendor can serve, and a **task model** takes the
calls nobody asks for (naming a conversation, writing the composer's
starting points) off the model picked for actual reasoning. Both are
opt-in; existing per-service profiles resolve exactly as before.

Around that: Settings drops the card grid for a persistent searchable
navigator whose landing page answers "what state am I in" instead of
repeating the directory; Mastery Path files study conversations under
their topic and opens them in the tutor rather than dropping them on
/home; the sidebar folds features into More and reorders by drag; the
reading companion is rebuilt from /home's own chat components; and the
LightRAG engine talks to the SDK directly, so its extra no longer drags
MinerU in transitively.

Also back-merges PR #1041 (Anthropic SDK 1.x temperature via extra_body)
and PR #741 (owner-only auth secret/token files), which had been merged
straight into main and never flowed back to dev.

Release notes: assets/releases/ver1-6-1.md
Gates: pre-commit 15/15, ruff clean, pytest 5802 passed, web 450 node
tests, eslint 0 errors, docs build clean.
2026-08-30 13:19:42 +08:00

126 lines
3.5 KiB
TypeScript

export interface BilibiliSource {
bvid: string;
page: number;
startSeconds: number;
}
const YOUTUBE_ID = /^[A-Za-z0-9_-]{11}$/;
const BILIBILI_ID = /^BV[0-9A-Za-z]{10}$/i;
const BILIBILI_HOSTS = new Set([
"bilibili.com",
"www.bilibili.com",
"m.bilibili.com",
"player.bilibili.com",
"b23.tv",
]);
export function youtubeVideoId(url: string): string {
try {
const parsed = new URL(url);
const host = parsed.hostname.toLowerCase();
let candidate = "";
if (host === "youtu.be") candidate = parsed.pathname.slice(1).split("/")[0];
else if (
[
"youtube.com",
"www.youtube.com",
"m.youtube.com",
"music.youtube.com",
].includes(host)
) {
if (
parsed.pathname.startsWith("/shorts/") ||
parsed.pathname.startsWith("/live/") ||
parsed.pathname.startsWith("/embed/")
) {
candidate = parsed.pathname.split("/")[2] || "";
} else if (parsed.pathname === "/watch") {
candidate = parsed.searchParams.get("v") ?? "";
}
}
return YOUTUBE_ID.test(candidate) ? candidate : "";
} catch {
return "";
}
}
export function youtubeEntryTime(url: string): number {
try {
const parsed = new URL(url);
return parseMediaTimestamp(
parsed.searchParams.get("t") || parsed.searchParams.get("start") || "0",
);
} catch {
return 0;
}
}
export function parseBilibiliSource(url: string): BilibiliSource | null {
try {
const parsed = new URL(url);
const host = parsed.hostname.toLowerCase();
if (parsed.protocol !== "https:" && parsed.protocol !== "http:")
return null;
if (!BILIBILI_HOSTS.has(host)) return null;
let candidate = "";
if (
host === "player.bilibili.com" &&
parsed.pathname.replace(/\/$/, "") === "/player.html"
) {
candidate = parsed.searchParams.get("bvid") || "";
} else if (host === "b23.tv") {
candidate = parsed.pathname.slice(1).split("/")[0];
} else {
candidate =
/^\/video\/(BV[0-9A-Za-z]{10})\/?$/i.exec(parsed.pathname)?.[1] || "";
}
if (!BILIBILI_ID.test(candidate)) return null;
const page = Math.max(
1,
Number.parseInt(parsed.searchParams.get("p") || "1", 10) || 1,
);
const startSeconds = parseMediaTimestamp(
parsed.searchParams.get("t") || parsed.searchParams.get("start") || "0",
);
return { bvid: `BV${candidate.slice(2)}`, page, startSeconds };
} catch {
return null;
}
}
export function bilibiliEmbedUrl(
source: BilibiliSource,
startSeconds: number,
): string {
const params = new URLSearchParams({
bvid: source.bvid,
p: String(source.page),
t: String(Math.max(0, Math.floor(startSeconds))),
autoplay: "0",
muted: "0",
danmaku: "0",
});
return `https://player.bilibili.com/player.html?${params}`;
}
export function bilibiliOfficialUrl(
source: BilibiliSource,
startSeconds: number,
): string {
const params = new URLSearchParams();
if (source.page > 1) params.set("p", String(source.page));
if (startSeconds > 0) params.set("t", String(Math.floor(startSeconds)));
return `https://www.bilibili.com/video/${source.bvid}/${params.size ? `?${params}` : ""}`;
}
export function parseMediaTimestamp(value: string): number {
if (/^\d+$/.test(value)) return Math.max(0, Number(value));
const match = /^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?$/i.exec(value);
if (!match || !match.slice(1).some(Boolean)) return 0;
return (
Number(match[1] || 0) * 3600 +
Number(match[2] || 0) * 60 +
Number(match[3] || 0)
);
}