dsh-video-insight 0.8.5 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE)
4
4
  [![DeepSeek Harness](https://img.shields.io/badge/DeepSeek%20Harness-plugin-4d6bfe.svg)](https://github.com/deepseek-ai)
5
5
  [![Platform](https://img.shields.io/badge/platform-macOS%20%7C%20Linux%20%7C%20Windows-lightgrey.svg)](#已知限制)
6
- [![Version](https://img.shields.io/badge/dsh--video--insight-v0.8.5-blue.svg)](package.json)
6
+ [![Version](https://img.shields.io/badge/dsh--video--insight-v0.9.0-blue.svg)](package.json)
7
7
 
8
8
  ![dsh-video-insight](icon.svg)
9
9
 
@@ -11,6 +11,8 @@
11
11
 
12
12
  兜底有三档:**本机 Whisper / SenseVoice 转写**(可选引擎、线程、只转前 N 秒),音频全程不出这台电脑;**纯无声/纯画面视频**(没有配音也没有字幕)还能抽帧做**画面文字识别(OCR)**,甚至直接把画面交给**视觉模型**描述场景、人物和动作(「画面理解」,会用到支持图片输入的模型),把画面内容变成带时间戳的文字稿,摘要仍用你 profile 里的模型。
13
13
 
14
+ 解析可以丢到**后台队列**:首页历史里每条视频会显示解析进度条,「全部解析」把还没分析完的历史一次性排进队列(默认同时跑 2 条,`jobs.concurrency` 可调),一条任务从头跑完 总结 → 思维导图 → 分镜 →(可选)画面描述,不用再切页签逐个点「生成」。状态只存在内存里,分析结果照常落盘。
15
+
14
16
  追问是 **RAG(检索增强)**:先把问题在转写稿里做 BM25 检索,只把最相关的几段(带时间戳)连同对话一起喂给模型,回答里标 `[mm:ss]`,气泡下面还有「依据」按钮,点一下直接跳回那段视频。
15
17
 
16
18
  总结 / 章节 / 思维导图 / 对话都会**按视频缓存**(`~/.dsh/video-analyses/`),从「历史」里点回来直接看到上次的结果,不用重新等模型;想重来就点「重新生成」,也可以一键清空某个视频的缓存。
@@ -179,6 +181,11 @@ host 半零外部依赖(只用 Node 内置 `fetch`/`crypto`),不写锁文
179
181
  | GET | `/dsh-video/api/analysis?docId=` | 取某个视频的分析缓存:`{ok,value:{docId,analysis:{summary,chapters,mindmap,chat}}}` |
180
182
  | POST | `/dsh-video/api/analysis/clear` | `{docId}` 清掉该视频全部缓存(总结/章节/导图/对话) |
181
183
  | POST | `/dsh-video/api/analysis/chat/clear` | `{docId}` 只清对话 |
184
+ | POST | `/dsh-video/api/jobs` | 开始一个后台解析任务:`{docId?/url?, describe?:true}` → `{ok,value:{job,created}}`;同一视频已在队列里时不重复排队(`created:false`) |
185
+ | GET | `/dsh-video/api/jobs` | 任务队列快照:`{ok,value:{concurrency,active,running,jobs:[{id,docId,url,title,status,stage,stageLabel,percent,describe,error,warnings,queuedAt,startedAt,finishedAt}]}}`(进度条就轮询这个) |
186
+ | POST | `/dsh-video/api/jobs/all` | 批量解析历史里还没分析完的视频:`{limit?:20, force?:false, describe?:true}` → `{ok,value:{started,skipped,jobs}}` |
187
+ | POST | `/dsh-video/api/jobs/cancel` | `{id}` 或 `{docId}` 取消排队/运行中的任务 → `{ok,value:{cancelled,jobs}}` |
188
+ | POST | `/dsh-video/api/jobs/clear` | 清掉已结束的任务记录 |
182
189
  | GET | `/dsh-video/api/history` | `{ok,value:{items:[…]}}`,最近 100 条,新的在前(解析/分析成功后 host 自动写入);带 `hasSummary/hasChapters/hasMindmap/chatCount` 标记 |
183
190
  | POST | `/dsh-video/api/history/remove` | `{docId}` 删一条 → `{ok,value:{items}}` |
184
191
  | POST | `/dsh-video/api/history/clear` | 清空历史 → `{ok,value:{items:[]}}` |
@@ -225,6 +232,8 @@ host 半零外部依赖(只用 Node 内置 `fetch`/`crypto`),不写锁文
225
232
  visionMaxFrames: 48 # 画面理解最多取几帧
226
233
  visionPack: sheet # sheet=多帧拼一张分镜图(默认,省图省时)| frames=逐帧发
227
234
  sheetCols: 3 # 分镜图列数(行数按帧数自动算)
235
+ jobs:
236
+ concurrency: 2 # 后台解析同时跑几个(1-6)
228
237
  ```
229
238
 
230
239
  面板「设置」里保存的 Cookie 与本地转写配置都写在 `~/.dsh/video-credentials.json`,**优先级高于**行配置,改完立即生效。
package/client/client.js CHANGED
@@ -19,6 +19,9 @@ window.__ModuleLoader__.load({
19
19
  const HISTORY_LIMIT = 100;
20
20
  const HOME_RECENT_LIMIT = 10;
21
21
  const SEGMENT_LIMIT = 400;
22
+ /** 后台解析队列轮询间隔:有任务在跑时 2s,空闲时 10s。 */
23
+ const JOBS_ACTIVE_MS = 2000;
24
+ const JOBS_IDLE_MS = 10000;
22
25
 
23
26
  /** 同源 API 基址:应用路由是 document-relative,所以从 baseURI 解析。 */
24
27
  const API_BASE = (() => {
@@ -790,7 +793,7 @@ window.__ModuleLoader__.load({
790
793
  * 顶部紧凑工具栏:返回 + 标题/平台/状态 + 历史/设置。
791
794
  * 视频信息压缩成两行(标题一行省略、元信息一行),不再占掉一整块纵向空间。
792
795
  */
793
- function VideoToolbar({ doc, historyCount, canGoBack, onBack, onHistory, onSettings }) {
796
+ function VideoToolbar({ doc, job, historyCount, canGoBack, onBack, onHistory, onSettings, onCancelJob }) {
794
797
  let metaBits = [];
795
798
  if (doc) {
796
799
  const stats = doc.stats ?? {};
@@ -820,6 +823,16 @@ window.__ModuleLoader__.load({
820
823
  h('div', { key: 'meta', className: 'dvv-toolbar-meta' }, metaBits),
821
824
  ] : null),
822
825
  h('div', { key: 'actions', className: 'dvv-toolbar-actions' }, [
826
+ job && jobActive(job) ? h('div', {
827
+ key: 'job', className: 'dvv-job-chip',
828
+ title: `${jobStageText(job)} · ${jobPercent(job)}%`,
829
+ }, [
830
+ h('span', { key: 't', className: 'dvv-job-chip-text' }, `后台解析 ${jobPercent(job)}% · ${jobStageText(job)}`),
831
+ h('button', {
832
+ key: 'x', type: 'button', className: 'dvv-job-chip-x', title: '取消后台解析', 'aria-label': '取消后台解析',
833
+ onClick: () => { if (onCancelJob) onCancelJob(job); },
834
+ }, '×'),
835
+ ]) : null,
823
836
  h('button', { key: 'history', type: 'button', className: 'dvv-btn', onClick: onHistory }, historyCount ? `历史 ${historyCount}` : '历史'),
824
837
  h('button', { key: 'settings', type: 'button', className: 'dvv-btn', onClick: onSettings }, '设置'),
825
838
  ]),
@@ -1015,7 +1028,7 @@ window.__ModuleLoader__.load({
1015
1028
  ]);
1016
1029
  }
1017
1030
 
1018
- function ChaptersTab({ outline, doc, currentSeconds, view, onView, onSeek, onRetry, onGenerate, onCancel }) {
1031
+ function ChaptersTab({ outline, doc, currentSeconds, view, onView, onSeek, onRetry, onGenerate, onCancel, hint, titlesBusy, onGenerateTitles, onGoMindmap }) {
1019
1032
  if (!doc) return h('div', { className: 'dvv-tabpane' }, h('div', { className: 'dvv-empty-note' }, '先解析一个视频,再来看章节。'));
1020
1033
  if (outline.loading) {
1021
1034
  return h('div', { className: 'dvv-tabpane' }, h('div', { className: 'dvv-chlist' },
@@ -1046,6 +1059,22 @@ window.__ModuleLoader__.load({
1046
1059
  outline.genLoading ? '生成中…' : visuals ? '重新生成描述' : '生成画面描述'),
1047
1060
  ].filter(Boolean));
1048
1061
  const parts = [bar];
1062
+ const hintText = String(hint || '').trim();
1063
+ if (hintText) {
1064
+ parts.push(h('div', { key: 'hint', className: 'dvv-outline-hint', role: 'status' }, [
1065
+ h('span', { key: 't', className: 'dvv-outline-hint-text' }, hintText),
1066
+ h('div', { key: 'a', className: 'dvv-outline-hint-actions' }, [
1067
+ h('button', {
1068
+ key: 'gen', type: 'button', className: 'dvv-btn', 'data-primary': 'true',
1069
+ disabled: Boolean(titlesBusy), onClick: () => { if (typeof onGenerateTitles === 'function') onGenerateTitles(); },
1070
+ }, titlesBusy ? '正在生成…' : '生成章节标题'),
1071
+ h('button', {
1072
+ key: 'mm', type: 'button', className: 'dvv-btn',
1073
+ onClick: () => { if (typeof onGoMindmap === 'function') onGoMindmap(); },
1074
+ }, '去思维导图'),
1075
+ ]),
1076
+ ]));
1077
+ }
1049
1078
  if (outline.genLoading) {
1050
1079
  parts.push(h('div', { key: 'load', className: 'dvv-tr-progress' }, [
1051
1080
  h('span', { key: 'p', className: 'dvv-note' }, outline.genPhase || '正在生成画面描述…'),
@@ -1739,7 +1768,7 @@ window.__ModuleLoader__.load({
1739
1768
  ]),
1740
1769
  h('div', { key: 'body', className: 'dvv-kf-body' }, [
1741
1770
  h('div', { key: 'title', className: 'dvv-kf-title', title }, title || '关键画面'),
1742
- h('div', { key: 'desc', className: 'dvv-kf-desc', 'data-empty': description ? undefined : 'true' }, description || '还没有描述'),
1771
+ h('div', { key: 'desc', className: 'dvv-kf-desc', 'data-empty': description ? undefined : 'true' }, description || '还没有画面描述'),
1743
1772
  ]),
1744
1773
  ]);
1745
1774
  }
@@ -1755,7 +1784,16 @@ window.__ModuleLoader__.load({
1755
1784
  }
1756
1785
 
1757
1786
  /** 一条历史记录:封面、标题、作者/时长、平台标签、相对时间、字幕标记、星标、删除。 */
1758
- function HistoryRow({ item, onOpen, onRemove, onFavorite }) {
1787
+ /* ------------------------------------------------------------ 后台解析任务 */
1788
+ /** 是否仍在跑(排队或执行中)。 */
1789
+ function jobActive(job) { const status = String((job && job.status) || ''); return status === 'queued' || status === 'running'; }
1790
+ /** 百分比:夹在 0–100,排队中恒为 0。 */
1791
+ function jobPercent(job) { if (!job || job.status === 'queued') return 0; const value = Number(job.percent); if (!Number.isFinite(value)) return 0; return Math.min(100, Math.max(0, value)); }
1792
+ /** 阶段文案:排队中 / host 的 stageLabel / 兜底。 */ function jobStageText(job) { if (!job) return ''; if (job.status === 'queued') return '排队中…'; return String(job.stageLabel || (job.status === 'done' ? '已完成' : '解析中…')); }
1793
+ /** 汇总成 docId → 最新一条任务 的 Map。 */
1794
+ function buildJobsByDoc(list) { const map = new Map(); for (const job of Array.isArray(list) ? list : []) { if (!job || !job.docId) continue; const before = map.get(job.docId); if (!before) { map.set(job.docId, job); continue; } if ((Number(job.updatedAt) || 0) >= (Number(before.updatedAt) || 0)) map.set(job.docId, job); } return map; }
1795
+
1796
+ function HistoryRow({ item, job, onOpen, onRemove, onFavorite, onCancelJob }) {
1759
1797
  const platform = item.platform || guessPlatform(item.url);
1760
1798
  const duration = Number(item.duration) > 0 ? fmtTime(item.duration) : '';
1761
1799
  return h('div', {
@@ -1779,6 +1817,23 @@ window.__ModuleLoader__.load({
1779
1817
  item.hasMindmap ? h('span', { key: 'mm', className: 'dvv-mini' }, '导图') : null,
1780
1818
  Number(item.chatCount) > 0 ? h('span', { key: 'chat', className: 'dvv-mini' }, `${Number(item.chatCount)} 条对话`) : null,
1781
1819
  ]),
1820
+ job && jobActive(job) ? h('div', {
1821
+ key: 'job', className: 'dvv-job',
1822
+ onClick: (event) => { if (event && typeof event.stopPropagation === 'function') event.stopPropagation(); },
1823
+ }, [
1824
+ h('div', { key: 'bar', className: 'dvv-jobbar', 'data-queued': job.status === 'queued' ? 'true' : undefined },
1825
+ h('div', { key: 'fill', className: 'dvv-jobbar-fill', style: { width: `${jobPercent(job)}%` } })),
1826
+ h('span', { key: 'label', className: 'dvv-job-label' }, job.status === 'queued' ? '排队中…' : `${jobPercent(job)}% · ${jobStageText(job)}`),
1827
+ onCancelJob ? h('button', {
1828
+ key: 'cancel', type: 'button', className: 'dvv-del', title: '取消后台解析', 'aria-label': '取消后台解析',
1829
+ onClick: (event) => {
1830
+ if (event && typeof event.stopPropagation === 'function') event.stopPropagation();
1831
+ onCancelJob(job);
1832
+ },
1833
+ }, '×') : null,
1834
+ ]) : (job && job.status === 'failed'
1835
+ ? h('span', { key: 'jobfail', className: 'dvv-job-failed', title: String(job.error || '') }, '解析失败')
1836
+ : null),
1782
1837
  ]),
1783
1838
  onFavorite ? h('button', {
1784
1839
  key: 'fav', type: 'button', className: 'dvv-fav', 'data-on': item.favorite ? 'true' : 'false',
@@ -1799,7 +1854,7 @@ window.__ModuleLoader__.load({
1799
1854
  }
1800
1855
 
1801
1856
  /** 无视频空态时直接铺在主区的历史列表:只展示前 10 条,更多去抽屉。 */
1802
- function HistoryList({ items, onOpen, onRemove, onFavorite, onOpenAll }) {
1857
+ function HistoryList({ items, jobs, activeCount, parseAllBusy, parseAllConfirm, onOpen, onRemove, onFavorite, onOpenAll, onCancelJob, onParseAll }) {
1803
1858
  const all = Array.isArray(items) ? items : [];
1804
1859
  if (!all.length) return null;
1805
1860
  const shown = all.slice(0, HOME_RECENT_LIMIT);
@@ -1808,17 +1863,24 @@ window.__ModuleLoader__.load({
1808
1863
  h('span', { key: 't', className: 'dvv-label' }, '最近分析'),
1809
1864
  h('span', { key: 'c', className: 'dvv-note' }, `共 ${all.length} 条 · 最多保留 ${HISTORY_LIMIT} 条`),
1810
1865
  h('div', { key: 's', className: 'dvv-spacer' }),
1866
+ onParseAll ? h('button', {
1867
+ key: 'parseAll', type: 'button', className: 'dvv-btn dvv-btn-mini',
1868
+ 'data-danger': parseAllConfirm ? 'true' : undefined, disabled: Boolean(parseAllBusy),
1869
+ onClick: () => onParseAll(),
1870
+ }, parseAllConfirm ? '确认解析?' : (Number(activeCount) > 0 ? `解析中 ${Number(activeCount)}` : '全部解析')) : null,
1811
1871
  all.length > HOME_RECENT_LIMIT && onOpenAll
1812
1872
  ? h('button', { key: 'more', type: 'button', className: 'dvv-btn dvv-btn-mini', onClick: onOpenAll }, '打开历史查看全部')
1813
1873
  : null,
1814
1874
  ].filter(Boolean)),
1815
1875
  h('div', { key: 'list', className: 'dvv-hist-list' }, shown.map((item, index) => h(HistoryRow, {
1816
- key: `${item.docId || item.url || 'item'}-${index}`, item, onOpen, onRemove, onFavorite,
1876
+ key: `${item.docId || item.url || 'item'}-${index}`, item,
1877
+ job: jobs && typeof jobs.get === 'function' ? jobs.get(item.docId) : null,
1878
+ onOpen, onRemove, onFavorite, onCancelJob,
1817
1879
  }))),
1818
1880
  ]);
1819
1881
  }
1820
1882
 
1821
- function HistoryDrawer({ items, error, query, filter, onQuery, onFilter, onOpen, onRemove, onFavorite, onClear, onClose }) {
1883
+ function HistoryDrawer({ items, jobs, error, query, filter, onQuery, onFilter, onOpen, onRemove, onFavorite, onCancelJob, onClear, onClose }) {
1822
1884
  const [confirming, setConfirming] = useState(false);
1823
1885
  const list = Array.isArray(items) ? items : [];
1824
1886
  const needle = String(query ?? '').trim().toLowerCase();
@@ -1860,8 +1922,9 @@ window.__ModuleLoader__.load({
1860
1922
  error ? h('div', { key: 'err', className: 'dvv-error' }, error) : null,
1861
1923
  visible.length ? visible.map((item, index) => h(HistoryRow, {
1862
1924
  key: `${item.docId || item.url || 'item'}-${index}`, item,
1925
+ job: jobs && typeof jobs.get === 'function' ? jobs.get(item.docId) : null,
1863
1926
  onOpen: (picked) => { setConfirming(false); onOpen(picked); },
1864
- onRemove, onFavorite,
1927
+ onRemove, onFavorite, onCancelJob,
1865
1928
  })) : h('div', { key: 'empty', className: 'dvv-empty-note' },
1866
1929
  list.length
1867
1930
  ? '没有匹配的历史记录'
@@ -2221,16 +2284,22 @@ window.__ModuleLoader__.load({
2221
2284
  const [asrNote, setAsrNote] = useState('');
2222
2285
  const [transcribe, setTranscribe] = useState(EMPTY_TRANSCRIBE);
2223
2286
  const [mindmap, setMindmap] = useState(EMPTY_MINDMAP);
2287
+ const [titlesLoading, setTitlesLoading] = useState(false);
2224
2288
  const [mmExpanded, setMmExpanded] = useState({});
2225
2289
  const [storyboard, setStoryboard] = useState(EMPTY_STORYBOARD);
2226
2290
  const [sbDescribe, setSbDescribe] = useState(false);
2227
2291
  const [outline, setOutline] = useState(EMPTY_OUTLINE);
2228
2292
  const [outlineView, setOutlineView] = useState('list');
2293
+ const [jobs, setJobs] = useState({ concurrency: 0, active: 0, running: 0, jobs: [] });
2294
+ const [jobsError, setJobsError] = useState('');
2295
+ const [parseAllBusy, setParseAllBusy] = useState(false);
2296
+ const [parseAllConfirm, setParseAllConfirm] = useState(false);
2229
2297
 
2230
2298
  const mounted = useRef(true);
2231
2299
  /** 同一时刻只允许一条在途请求(解析或流式),切视频 / 新请求前 abort。 */
2232
2300
  const active = useRef(null);
2233
2301
  const docRef = useRef(null);
2302
+ const titlesLoadingRef = useRef(false);
2234
2303
  const chatRef = useRef([]);
2235
2304
  const askBusyRef = useRef(false);
2236
2305
  const resolveTicket = useRef(0);
@@ -2242,6 +2311,10 @@ window.__ModuleLoader__.load({
2242
2311
  const autoAsrRef = useRef(new Map());
2243
2312
  /** 双语翻译请求的独立 AbortController(不打断总结 / 追问)。 */
2244
2313
  const translationAbort = useRef(null);
2314
+ /** 后台解析队列:上一轮 jobs(用于判断 running/queued → done/failed)与最近 active 数。 */
2315
+ const jobsRef = useRef([]);
2316
+ const jobsActiveRef = useRef(0);
2317
+ const jobsSeen = useRef(false);
2245
2318
  docRef.current = doc;
2246
2319
  chatRef.current = chat;
2247
2320
 
@@ -2829,16 +2902,41 @@ window.__ModuleLoader__.load({
2829
2902
  cachedAt: '',
2830
2903
  });
2831
2904
  setMmExpanded(defaultMindmapExpanded(data));
2905
+ return '';
2832
2906
  } catch (caught) {
2833
- if (!mounted.current || (caught && caught.name === 'AbortError')) return;
2907
+ if (!mounted.current || (caught && caught.name === 'AbortError')) return '';
2908
+ const message = friendlyMindmapError(caught);
2834
2909
  setMindmap({
2835
2910
  ...EMPTY_MINDMAP,
2836
- error: friendlyMindmapError(caught),
2911
+ error: message,
2837
2912
  code: String((caught && caught.code) || ''),
2838
2913
  });
2914
+ return message;
2839
2915
  }
2840
2916
  }, []);
2841
2917
 
2918
+ /* ---- 章节兜底(均匀切分)时,一键生成章节标题:先生成思维导图,再刷新大纲 ---- */
2919
+ const generateChapterTitles = useCallback(async (video) => {
2920
+ const target = video ?? docRef.current;
2921
+ if (!target || !target.docId || titlesLoadingRef.current) return;
2922
+ titlesLoadingRef.current = true;
2923
+ setTitlesLoading(true);
2924
+ try {
2925
+ const failure = await loadMindmap(target, false);
2926
+ if (!mounted.current) return;
2927
+ if (failure) {
2928
+ showToast(String(failure));
2929
+ return;
2930
+ }
2931
+ await loadOutline(docRef.current);
2932
+ } catch (caught) {
2933
+ if (mounted.current) showToast(friendlyMindmapError(caught));
2934
+ } finally {
2935
+ titlesLoadingRef.current = false;
2936
+ if (mounted.current) setTitlesLoading(false);
2937
+ }
2938
+ }, [loadMindmap, loadOutline, showToast]);
2939
+
2842
2940
  const toggleMindmapNode = useCallback((path) => {
2843
2941
  setMmExpanded((current) => ({ ...current, [path]: !current[path] }));
2844
2942
  }, []);
@@ -2854,7 +2952,7 @@ window.__ModuleLoader__.load({
2854
2952
  */
2855
2953
  const loadStoryboard = useCallback(async (video, options) => {
2856
2954
  const target = video ?? docRef.current;
2857
- const opts = options || {};
2955
+ const opts = options || {}; const silent = Boolean(opts.silent);
2858
2956
  if (!target || !target.docId || !mounted.current || storyboard.loading) return;
2859
2957
  interruptActive();
2860
2958
  const controller = new AbortController();
@@ -2910,7 +3008,7 @@ window.__ModuleLoader__.load({
2910
3008
  error: data ? '' : '分镜生成完了,但没有拿到可用的分镜数据。',
2911
3009
  code: '',
2912
3010
  });
2913
- if (data) {
3011
+ if (data && !silent) {
2914
3012
  const count = data.frames || data.cells.length;
2915
3013
  showToast(`分镜已生成 · ${count} 格${data.described ? ' · 含描述' : ''}`);
2916
3014
  }
@@ -2961,6 +3059,94 @@ window.__ModuleLoader__.load({
2961
3059
  showToast('已取消生成分镜');
2962
3060
  }, [abortActive, showToast]);
2963
3061
 
3062
+ /* ---- 后台解析队列:GET /jobs 轮询 + 取消 + 一键全部解析 ---- */
3063
+ /** 拉一次任务列表:状态迁移时刷新历史;当前视频完成时提示并刷新大纲/分镜。 */
3064
+ const refreshJobs = useCallback(async () => {
3065
+ let value = null;
3066
+ try {
3067
+ value = await call('jobs');
3068
+ } catch (caught) {
3069
+ if (mounted.current) setJobsError(`后台任务加载失败:${String(caught?.message ?? caught)}`);
3070
+ return;
3071
+ }
3072
+ if (!mounted.current) return;
3073
+ const next = value && typeof value === 'object' ? value : {};
3074
+ const list = Array.isArray(next.jobs) ? next.jobs.filter(Boolean) : [];
3075
+ const activeCount = list.reduce((sum, job) => sum + (jobActive(job) ? 1 : 0), 0);
3076
+ const prev = jobsRef.current;
3077
+ jobsRef.current = list;
3078
+ jobsActiveRef.current = Number(next.active) > 0 ? Number(next.active) : activeCount;
3079
+ setJobs({
3080
+ concurrency: Number(next.concurrency) || 0,
3081
+ active: Number(next.active) || activeCount,
3082
+ running: Number(next.running) || 0,
3083
+ jobs: list,
3084
+ });
3085
+ setJobsError('');
3086
+ const prevByDoc = buildJobsByDoc(prev);
3087
+ const nextByDoc = buildJobsByDoc(list);
3088
+ const finished = [];
3089
+ for (const [docId, job] of nextByDoc.entries()) {
3090
+ const before = prevByDoc.get(docId);
3091
+ const terminal = job.status === 'done' || job.status === 'failed';
3092
+ if (!terminal) continue;
3093
+ const wasActive = before && jobActive(before);
3094
+ const isNew = !before || before.status !== job.status;
3095
+ if (wasActive || (jobsSeen.current && isNew)) finished.push(job);
3096
+ }
3097
+ if (jobsSeen.current && finished.length) void refreshHistory();
3098
+ jobsSeen.current = true;
3099
+ const current = docRef.current;
3100
+ for (const job of finished) {
3101
+ if (!current || job.docId !== current.docId) continue;
3102
+ if (job.status === 'done') {
3103
+ showToast('后台解析完成');
3104
+ void loadOutline(current);
3105
+ void loadStoryboard(current, { silent: true });
3106
+ } else if (job.status === 'failed') {
3107
+ showToast(`后台解析失败:${String(job.error || '未知错误')}`);
3108
+ }
3109
+ }
3110
+ }, [refreshHistory, showToast, loadOutline, loadStoryboard]);
3111
+
3112
+ /** 取消某个 docId 的后台任务(历史行 / 详情工具条共用)。 */
3113
+ const cancelJob = useCallback(async (jobOrDoc) => {
3114
+ const docId = String((jobOrDoc && jobOrDoc.docId) || jobOrDoc || '');
3115
+ if (!docId) { showToast('这条任务缺少 docId,无法取消'); return; }
3116
+ try {
3117
+ await post('jobs/cancel', { docId });
3118
+ if (!mounted.current) return;
3119
+ showToast('已取消后台解析');
3120
+ void refreshJobs();
3121
+ } catch (caught) {
3122
+ if (mounted.current) showToast(`取消失败:${String(caught?.message ?? caught)}`);
3123
+ }
3124
+ }, [refreshJobs, showToast]);
3125
+
3126
+ /** 首页「全部解析」:第一次点击进入确认,第二次才真正提交。 */
3127
+ const parseAllJobs = useCallback(async () => {
3128
+ if (parseAllBusy) return;
3129
+ if (!parseAllConfirm) {
3130
+ if (!historyItems.length) { showToast('还没有历史记录可以解析'); return; }
3131
+ setParseAllConfirm(true);
3132
+ return;
3133
+ }
3134
+ setParseAllConfirm(false);
3135
+ setParseAllBusy(true);
3136
+ try {
3137
+ const value = await post('jobs/all', { limit: 20, describe: true });
3138
+ if (!mounted.current) return;
3139
+ const started = Number(value && value.started) || 0;
3140
+ const skipped = Number(value && value.skipped) || 0;
3141
+ showToast(`已开始 ${started} 个,跳过 ${skipped} 个`);
3142
+ void refreshJobs();
3143
+ } catch (caught) {
3144
+ if (mounted.current) showToast(`批量解析失败:${String(caught?.message ?? caught)}`);
3145
+ } finally {
3146
+ if (mounted.current) setParseAllBusy(false);
3147
+ }
3148
+ }, [parseAllBusy, parseAllConfirm, historyItems.length, showToast, refreshJobs]);
3149
+
2964
3150
  /* ---- 原文双语对照:GET /translate 读缓存,没有就 POST SSE 生成(独立取消,不打断其它请求) ---- */
2965
3151
 
2966
3152
  const loadTranslation = useCallback(async (video, lang, options) => {
@@ -3256,6 +3442,28 @@ window.__ModuleLoader__.load({
3256
3442
 
3257
3443
  useEffect(() => { void refreshStatus(); void refreshHistory(); void refreshAsr(); }, [refreshStatus, refreshHistory, refreshAsr]);
3258
3444
 
3445
+ // 后台解析队列轮询:挂载立即拉一次;有任务 2s、空闲 10s;页面隐藏时暂停(保持 10s 心跳)。
3446
+ useEffect(() => {
3447
+ let stopped = false;
3448
+ let timer = 0;
3449
+ const schedule = (ms) => {
3450
+ if (stopped || typeof window.setTimeout !== 'function') return;
3451
+ timer = window.setTimeout(() => { void tick(); }, ms);
3452
+ };
3453
+ const tick = async () => {
3454
+ if (stopped) return;
3455
+ if (typeof document !== 'undefined' && document.hidden) { schedule(JOBS_IDLE_MS); return; }
3456
+ await refreshJobs();
3457
+ if (stopped) return;
3458
+ schedule(jobsActiveRef.current > 0 ? JOBS_ACTIVE_MS : JOBS_IDLE_MS);
3459
+ };
3460
+ void tick();
3461
+ return () => {
3462
+ stopped = true;
3463
+ try { if (timer) window.clearTimeout(timer); } catch { /* ignore */ }
3464
+ };
3465
+ }, [refreshJobs]);
3466
+
3259
3467
  // 章节 / 关键画面共用同一份大纲:进入任一页签时按 docId 拉一次。
3260
3468
  useEffect(() => {
3261
3469
  if (tab !== 'chapters' || !doc) return;
@@ -3318,6 +3526,10 @@ window.__ModuleLoader__.load({
3318
3526
  onRetry: () => { void loadOutline(doc); },
3319
3527
  onGenerate: () => { void generateVisuals(doc, Boolean(outline.data && outline.data.visuals)); },
3320
3528
  onCancel: cancelVisuals,
3529
+ hint: outline.data && outline.data.hint ? String(outline.data.hint) : '',
3530
+ titlesBusy: titlesLoading || mindmap.loading,
3531
+ onGenerateTitles: () => { void generateChapterTitles(docRef.current); },
3532
+ onGoMindmap: () => setTab('mindmap'),
3321
3533
  });
3322
3534
  } else if (tab === 'transcript') {
3323
3535
  tabBody = h(TranscriptTab, {
@@ -3384,6 +3596,7 @@ window.__ModuleLoader__.load({
3384
3596
  setSegQuery('');
3385
3597
  setSegAll(false);
3386
3598
  setOverlay('');
3599
+ setParseAllConfirm(false);
3387
3600
  pendingSeek.current = null;
3388
3601
  void refreshHistory();
3389
3602
  }, [abortActive, refreshHistory]);
@@ -3394,6 +3607,10 @@ window.__ModuleLoader__.load({
3394
3607
  try { getLayoutService()?.selectPanel(null); } catch { /* 服务不可用:按钮已隐藏 */ }
3395
3608
  };
3396
3609
 
3610
+ const jobsByDoc = buildJobsByDoc(jobs.jobs);
3611
+ const currentJob = doc ? jobsByDoc.get(doc.docId) : null;
3612
+ const jobsActiveCount = Number(jobs.active) > 0 ? Number(jobs.active) : Array.from(jobsByDoc.values()).filter(jobActive).length;
3613
+
3397
3614
  const banners = [];
3398
3615
  if (error) banners.push(h('div', { key: 'error', className: 'dvv-error' }, error));
3399
3616
  if (resolveBusy) {
@@ -3413,10 +3630,14 @@ window.__ModuleLoader__.load({
3413
3630
  }),
3414
3631
  historyItems.length
3415
3632
  ? h('div', { key: 'recent', className: 'dvv-home-recent' }, h(HistoryList, {
3416
- items: historyItems, onOpen: openHistoryItem,
3633
+ items: historyItems, jobs: jobsByDoc, activeCount: jobsActiveCount,
3634
+ parseAllBusy, parseAllConfirm,
3635
+ onOpen: openHistoryItem,
3417
3636
  onRemove: (item) => { void removeHistory(item); },
3418
3637
  onFavorite: (item) => { void toggleHistoryFavorite(item); },
3419
3638
  onOpenAll: () => setOverlay('history'),
3639
+ onCancelJob: (job) => { void cancelJob(job); },
3640
+ onParseAll: () => { void parseAllJobs(); },
3420
3641
  }))
3421
3642
  : h('div', { key: 'none', className: 'dvv-home-none' }, '还没有分析记录,粘贴一个链接开始吧。'),
3422
3643
  ].filter(Boolean));
@@ -3452,7 +3673,8 @@ window.__ModuleLoader__.load({
3452
3673
 
3453
3674
  return h('div', { className: 'dvv-root' }, [
3454
3675
  h(VideoToolbar, {
3455
- key: 'toolbar', doc, historyCount: historyItems.length, canGoBack, onBack: goBack,
3676
+ key: 'toolbar', doc, job: currentJob, historyCount: historyItems.length, canGoBack, onBack: goBack,
3677
+ onCancelJob: (job) => { void cancelJob(job); },
3456
3678
  onHistory: () => setOverlay(overlay === 'history' ? '' : 'history'),
3457
3679
  onSettings: () => {
3458
3680
  if (overlay === 'settings') { setOverlay(''); return; }
@@ -3472,13 +3694,14 @@ window.__ModuleLoader__.load({
3472
3694
  banners.length ? h('div', { key: 'banners', className: 'dvv-banners' }, banners) : null,
3473
3695
  body,
3474
3696
  overlay === 'history' ? h(HistoryDrawer, {
3475
- key: 'drawer', items: historyItems, error: historyError,
3697
+ key: 'drawer', items: historyItems, jobs: jobsByDoc, error: historyError,
3476
3698
  query: historyQuery, filter: historyFilter,
3477
3699
  onQuery: (value) => setHistoryQuery(value),
3478
3700
  onFilter: (value) => setHistoryFilter(value),
3479
3701
  onOpen: openHistoryItem,
3480
3702
  onRemove: (item) => { void removeHistory(item); },
3481
3703
  onFavorite: (item) => { void toggleHistoryFavorite(item); },
3704
+ onCancelJob: (job) => { void cancelJob(job); },
3482
3705
  onClear: () => { void clearHistory(); },
3483
3706
  onClose: () => setOverlay(''),
3484
3707
  }) : null,
@@ -3647,6 +3870,10 @@ a.dvv-toolbar-title:hover{color:var(--dsw-alias-brand-primary)}
3647
3870
  .dvv-hist-main{flex:1;min-width:0}
3648
3871
  .dvv-hist-title{font-size:13px;line-height:19px;font-weight:550;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden}
3649
3872
  .dvv-hist-meta{display:flex;flex-wrap:wrap;align-items:center;gap:8px;margin-top:3px;font-size:11px;line-height:16px;color:var(--dsw-alias-label-secondary)}
3873
+ .dvv-job{display:flex;align-items:center;gap:6px;margin-top:4px}.dvv-jobbar{flex:1;min-width:0;height:4px;border-radius:999px;overflow:hidden;background:var(--dsw-alias-bg-layer-2,rgba(127,127,127,.16))}.dvv-jobbar-fill{height:100%;border-radius:999px;background:var(--dsw-alias-brand-primary);transition:width .25s ease}.dvv-jobbar[data-queued="true"] .dvv-jobbar-fill{width:0!important}
3874
+ .dvv-job-label{flex:none;font-size:11px;line-height:16px;color:var(--dsw-alias-label-secondary);font-variant-numeric:tabular-nums}.dvv-job-failed{font-size:11px;line-height:16px;color:var(--dsw-alias-state-error-primary)}
3875
+ .dvv-job-chip{display:flex;align-items:center;gap:6px;min-width:0;max-width:300px;padding:2px 4px 2px 10px;border:1px solid var(--dsw-alias-border-l1);border-radius:999px;color:var(--dsw-alias-label-secondary);font-size:12px;line-height:20px}.dvv-job-chip-text{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-variant-numeric:tabular-nums}
3876
+ .dvv-job-chip-x{flex:none;appearance:none;border:0;background:transparent;color:inherit;font-size:14px;line-height:18px;width:18px;height:18px;padding:0;border-radius:50%;cursor:pointer}.dvv-job-chip-x:hover{background:var(--dsw-alias-bg-layer-2,rgba(127,127,127,.16));color:var(--dsw-alias-state-error-primary)}
3650
3877
  .dvv-status-box{display:flex;flex-direction:column;gap:6px;border:1px dashed var(--dsw-alias-border-l1,rgba(0,0,0,.14));border-radius:10px;padding:10px 12px;margin-bottom:14px}
3651
3878
  .dvv-kv{display:flex;flex-wrap:wrap;align-items:baseline;gap:8px;font-size:12px;line-height:19px}
3652
3879
  .dvv-label{font-size:12px;line-height:19px;font-weight:600;color:var(--dsw-alias-label-secondary)}
@@ -3761,6 +3988,9 @@ a.dvv-toolbar-title:hover{color:var(--dsw-alias-brand-primary)}
3761
3988
  .dvv-sb-meta{font-size:11px;line-height:16px;color:var(--dsw-alias-label-secondary);font-variant-numeric:tabular-nums}
3762
3989
  .dvv-sb-check{display:flex;align-items:center;gap:4px;font-size:11px;line-height:16px;color:var(--dsw-alias-label-secondary);cursor:pointer}
3763
3990
  .dvv-sb-progress{display:flex;flex-direction:column;gap:8px;padding:12px 0}
3991
+ .dvv-outline-hint{display:flex;flex-wrap:wrap;align-items:center;gap:8px;padding:8px 12px;margin:2px 0 10px;border:1px dashed var(--dsw-alias-border-l1);border-radius:10px;background:var(--dsw-alias-bg-layer-2)}
3992
+ .dvv-outline-hint-text{flex:1 1 220px;min-width:0;font-size:12px;line-height:18px;color:var(--dsw-alias-label-secondary)}
3993
+ .dvv-outline-hint-actions{display:flex;align-items:center;gap:6px}
3764
3994
  .dvv-sb-wrap{display:flex;flex-direction:column;gap:14px}
3765
3995
  .dvv-sb-sheet{display:flex;flex-direction:column;gap:4px;min-width:0}
3766
3996
  .dvv-sb-cap{font-size:11px;line-height:16px;color:var(--dsw-alias-label-secondary);font-variant-numeric:tabular-nums}
@@ -3863,7 +4093,7 @@ a.dvv-toolbar-title:hover{color:var(--dsw-alias-brand-primary)}
3863
4093
  apply,
3864
4094
  inject,
3865
4095
  // 布局是纯函数,暴露给测试直接断言坐标/连线(Host 会忽略额外字段)。
3866
- __test: { buildMindmapLayout, mindmapTextLines, defaultMindmapExpanded, allMindmapPaths, renderMarkdown, clockToSeconds, buildNoteMarkdown, sanitizeFileName },
4096
+ __test: { buildMindmapLayout, mindmapTextLines, defaultMindmapExpanded, allMindmapPaths, renderMarkdown, clockToSeconds, buildNoteMarkdown, sanitizeFileName, JOBS_ACTIVE_MS, JOBS_IDLE_MS },
3867
4097
  };
3868
4098
  return exports_;
3869
4099
  },
package/lib/index.js CHANGED
@@ -52,6 +52,7 @@ import {
52
52
  import { clearTranslation, normalizeLang, readTranslation, runTranslate } from './translate.js';
53
53
  import { clearAssets, readCutsAsset, readVideoAsset } from './assets.js';
54
54
  import { getThumb } from './thumbs.js';
55
+ import { createJobQueue } from './jobs.js';
55
56
  import {
56
57
  clearOutlineVisuals,
57
58
  generateOutlineVisuals,
@@ -63,7 +64,7 @@ export const name = 'dsh-video-insight';
63
64
  /** Route prefix the panel calls; same origin as the GUI, so no CORS preflight. */
64
65
  export const API_PREFIX = '/dsh-video/api';
65
66
 
66
- const VERSION = '0.8.5';
67
+ const VERSION = '0.9.0';
67
68
  const CREDENTIALS_FILE = join(homedir(), '.dsh', 'video-credentials.json');
68
69
 
69
70
  const DEFAULTS = {
@@ -380,6 +381,149 @@ async function analyzeOnce(aiCtx, settings, resolver, request, handlers = {}) {
380
381
  return { doc, ...result };
381
382
  }
382
383
 
384
+ /* ------------------------------ 后台解析队列 ------------------------------ */
385
+
386
+ /** 后台任务里跑 LLM 步骤:并发时偶发空响应/限流,自动重试几次。 */
387
+ async function withRetry(task, { attempts = 3, baseDelay = 1500, signal } = {}) {
388
+ let lastError = null;
389
+ for (let attempt = 1; attempt <= attempts; attempt += 1) {
390
+ if (signal?.aborted) throw new VideoError('aborted', '已取消');
391
+ try {
392
+ return await task(attempt);
393
+ } catch (error) {
394
+ lastError = error;
395
+ const code = String(error?.code ?? '');
396
+ const retriable = code === 'llm-empty' || code === 'llm-failed' || code === 'asr-failed' || code === 'vision-failed';
397
+ if (!retriable || attempt === attempts) throw error;
398
+ await new Promise((resolve) => setTimeout(resolve, baseDelay * attempt));
399
+ }
400
+ }
401
+ throw lastError;
402
+ }
403
+
404
+ function progressBetween(from, to, percent) {
405
+ const value = Number(percent);
406
+ if (!Number.isFinite(value)) return null;
407
+ const clamped = Math.max(0, Math.min(100, value));
408
+ return Math.round(from + ((to - from) * clamped) / 100);
409
+ }
410
+
411
+ /**
412
+ * 一次完整的后台解析:解析 → 文字稿(需要时)→ 总结 → 思维导图 → 分镜 → 画面描述。
413
+ * 每一步都把进度写进 job,面板轮询 /jobs 就能画进度条;任何非致命步骤失败只记 warning。
414
+ */
415
+ async function runFullJob(job, helpers, { aiCtx, config, resolverFor }) {
416
+ const saved = await readSavedCredentials();
417
+ const settings = settingsFrom(config, saved);
418
+ const resolver = resolverFor(settings);
419
+ const signal = helpers.signal;
420
+
421
+ helpers.setStage('resolve', '正在解析视频信息…', 2);
422
+ let doc = job.docId ? resolver.cache.get(job.docId) : null;
423
+ if (!doc) {
424
+ // 只给了 docId(例如从历史里点「重新解析」)时,解析缓存可能已经过期,回历史里找链接。
425
+ let link = job.url;
426
+ if (!link && job.docId) {
427
+ const items = await readHistory().catch(() => []);
428
+ link = items.find((item) => item?.docId === job.docId)?.url ?? '';
429
+ }
430
+ if (!link) throw new VideoError('bad-url', '这个视频既不在解析缓存里,也没有可用的链接,请重新解析一次。');
431
+ job.url = link;
432
+ doc = await attachBestTranscript(await resolver.resolve(link));
433
+ }
434
+ job.docId = doc.docId;
435
+ job.url = doc.url;
436
+ job.title = doc.title;
437
+ await rememberDoc(doc);
438
+ helpers.setPercent(4);
439
+
440
+ if (!doc.transcript?.available && settings.asr?.enabled) {
441
+ helpers.setStage('transcript', '正在准备文字稿(语音 / 画面)…', 6);
442
+ doc.transcript = await transcribeVideo({
443
+ doc,
444
+ settings,
445
+ ai: aiCtx,
446
+ attachments: aiCtx.attachments,
447
+ signal,
448
+ onPhase: (text) => helpers.setStage('transcript', text, null),
449
+ onProgress: (event) => {
450
+ const mapped = progressBetween(6, 40, event?.percent);
451
+ if (mapped !== null) helpers.setPercent(mapped);
452
+ },
453
+ });
454
+ doc = (await attachBestTranscript(doc)) ?? doc;
455
+ helpers.setPercent(40);
456
+ }
457
+
458
+ helpers.setStage('summary', '正在写总结…', 42);
459
+ const summary = await withRetry(
460
+ () => runSummary(aiCtx, settings, doc, { signal }),
461
+ { signal },
462
+ );
463
+ await saveSummary(doc.docId, {
464
+ text: summary.text,
465
+ usage: summary.usage,
466
+ suggestions: summary.suggestions ?? [],
467
+ });
468
+ helpers.setPercent(58);
469
+
470
+ helpers.setStage('mindmap', '正在生成思维导图…', 60);
471
+ const mindmap = await withRetry(
472
+ () => runMindmap(aiCtx, settings, doc, { signal }),
473
+ { signal },
474
+ );
475
+ if (mindmap?.mindmap) await saveMindmap(doc.docId, mindmap.mindmap);
476
+ helpers.setPercent(72);
477
+
478
+ helpers.setStage('storyboard', '正在抽帧、拼分镜…', 74);
479
+ try {
480
+ await generateStoryboard({
481
+ doc,
482
+ settings,
483
+ aiCtx,
484
+ frames: 0,
485
+ cols: 3,
486
+ describe: false,
487
+ signal,
488
+ onPhase: (text) => helpers.setStage('storyboard', text, null),
489
+ onProgress: (event) => {
490
+ const mapped = progressBetween(74, 90, event?.percent);
491
+ if (mapped !== null) helpers.setPercent(mapped);
492
+ },
493
+ });
494
+ } catch (error) {
495
+ if (error?.code === 'aborted' || signal.aborted) throw error;
496
+ helpers.warn(`分镜没生成:${error?.message ?? error}`);
497
+ }
498
+ helpers.setPercent(90);
499
+
500
+ if (job.describe) {
501
+ helpers.setStage('visuals', '正在让视觉模型描述画面…', 91);
502
+ try {
503
+ await generateOutlineVisuals({
504
+ doc,
505
+ docId: doc.docId,
506
+ settings,
507
+ aiCtx,
508
+ signal,
509
+ force: false,
510
+ onPhase: (text) => helpers.setStage('visuals', text, null),
511
+ onProgress: (event) => {
512
+ const mapped = progressBetween(91, 98, event?.percent);
513
+ if (mapped !== null) helpers.setPercent(mapped);
514
+ },
515
+ });
516
+ } catch (error) {
517
+ if (error?.code === 'aborted' || signal.aborted) throw error;
518
+ helpers.warn(`画面描述没生成:${error?.message ?? error}`);
519
+ }
520
+ }
521
+
522
+ await rememberDoc(doc);
523
+ await recordHistory(doc, 'analyze');
524
+ helpers.setPercent(99);
525
+ }
526
+
383
527
  /* --------------------------------- tool ---------------------------------- */
384
528
 
385
529
  function renderDocForTool(doc, maxChars) {
@@ -627,6 +771,12 @@ function createApiHandler(aiCtx, config) {
627
771
  resolver = null;
628
772
  };
629
773
 
774
+ // 后台解析队列:同一视频不重复排队,并发数可在 profile 的 jobs.concurrency 里调。
775
+ const jobQueue = createJobQueue({
776
+ concurrency: Number(config?.jobs?.concurrency) || 2,
777
+ execute: (job, helpers) => runFullJob(job, helpers, { aiCtx, config, resolverFor }),
778
+ });
779
+
630
780
  return async (req, res) => {
631
781
  const url = new URL(req.url ?? '/', 'http://127.0.0.1');
632
782
  const route = url.pathname.slice(API_PREFIX.length) || '/';
@@ -1045,6 +1195,9 @@ function createApiHandler(aiCtx, config) {
1045
1195
  signature: outline.signature,
1046
1196
  visuals: outline.visuals,
1047
1197
  items: outline.items,
1198
+ hint: outline.source === 'uniform'
1199
+ ? '这段视频还没有章节标题:生成思维导图(或 AI 章节)后会自动按内容切分。'
1200
+ : '',
1048
1201
  },
1049
1202
  });
1050
1203
  return;
@@ -1384,6 +1537,81 @@ function createApiHandler(aiCtx, config) {
1384
1537
  return;
1385
1538
  }
1386
1539
 
1540
+ // 后台解析队列:开始 / 批量开始 / 进度 / 取消
1541
+ if (req.method === 'POST' && route === '/jobs') {
1542
+ const body = await readBody(req);
1543
+ try {
1544
+ const started = jobQueue.start({
1545
+ docId: String(body?.docId ?? '').trim(),
1546
+ url: String(body?.url ?? '').trim(),
1547
+ title: String(body?.title ?? '').trim(),
1548
+ describe: body?.describe !== false,
1549
+ });
1550
+ json(res, 200, { ok: true, value: started });
1551
+ } catch (error) {
1552
+ json(res, 200, { ok: false, code: error?.code ?? 'bad-param', error: String(error?.message ?? error) });
1553
+ }
1554
+ return;
1555
+ }
1556
+
1557
+ if (req.method === 'GET' && route === '/jobs') {
1558
+ json(res, 200, { ok: true, value: jobQueue.list() });
1559
+ return;
1560
+ }
1561
+
1562
+ if (req.method === 'POST' && route === '/jobs/all') {
1563
+ const body = await readBody(req).catch(() => ({}));
1564
+ try {
1565
+ const force = body?.force === true;
1566
+ const describe = body?.describe !== false;
1567
+ const limit = Math.max(1, Math.min(50, Number(body?.limit) || 20));
1568
+ const items = await readHistory();
1569
+ let started = 0;
1570
+ let skipped = 0;
1571
+ for (const item of items) {
1572
+ if (!item?.url) continue;
1573
+ if (started >= limit) break;
1574
+ if (!force) {
1575
+ const [analysis, storyboard] = await Promise.all([
1576
+ readAnalysis(item.docId).catch(() => null),
1577
+ readStoryboard(item.docId).catch(() => null),
1578
+ ]);
1579
+ if (analysis?.summary && analysis?.mindmap && storyboard) {
1580
+ skipped += 1;
1581
+ continue;
1582
+ }
1583
+ }
1584
+ if (jobQueue.isActive(item.docId)) {
1585
+ skipped += 1;
1586
+ continue;
1587
+ }
1588
+ const result = jobQueue.start({ docId: item.docId, url: item.url, title: item.title, describe });
1589
+ if (result.created) started += 1;
1590
+ else skipped += 1;
1591
+ }
1592
+ json(res, 200, { ok: true, value: { started, skipped, jobs: jobQueue.list() } });
1593
+ } catch (error) {
1594
+ json(res, 200, { ok: false, code: error?.code ?? 'internal', error: String(error?.message ?? error) });
1595
+ }
1596
+ return;
1597
+ }
1598
+
1599
+ if (req.method === 'POST' && route === '/jobs/cancel') {
1600
+ const body = await readBody(req);
1601
+ const cancelled = jobQueue.cancel({
1602
+ id: String(body?.id ?? '').trim(),
1603
+ docId: String(body?.docId ?? '').trim(),
1604
+ });
1605
+ json(res, 200, { ok: true, value: { cancelled, jobs: jobQueue.list() } });
1606
+ return;
1607
+ }
1608
+
1609
+ if (req.method === 'POST' && route === '/jobs/clear') {
1610
+ jobQueue.clearFinished();
1611
+ json(res, 200, { ok: true, value: jobQueue.list() });
1612
+ return;
1613
+ }
1614
+
1387
1615
  if (req.method === 'POST' && route === '/credentials') {
1388
1616
  const body = await readBody(req);
1389
1617
  const next = { ...saved };
package/lib/jobs.js ADDED
@@ -0,0 +1,204 @@
1
+ /**
2
+ * 后台解析队列。
3
+ *
4
+ * 一次「解析」不只是总结:转写(没字幕时)→ 总结 → 思维导图 → 分镜 → 画面描述
5
+ * 全部按顺序跑完,进度存在内存里,面板轮询 GET /jobs 拿进度条。
6
+ * 并发数默认 2(可在 profile 配置里调),同一视频不会重复排队。
7
+ */
8
+
9
+ const DEFAULT_CONCURRENCY = 2;
10
+ const MAX_RECORDS = 80;
11
+ const ACTIVE = new Set(['queued', 'running']);
12
+
13
+ function clean(value, max = 200) {
14
+ return String(value ?? '').replace(/\s+/g, ' ').trim().slice(0, max);
15
+ }
16
+
17
+ function snapshot(job) {
18
+ return {
19
+ id: job.id,
20
+ docId: job.docId || '',
21
+ url: job.url || '',
22
+ title: clean(job.title, 160),
23
+ status: job.status,
24
+ stage: job.stage,
25
+ stageLabel: job.stageLabel,
26
+ percent: job.percent,
27
+ describe: Boolean(job.describe),
28
+ error: job.error,
29
+ warnings: job.warnings.slice(0, 8),
30
+ queuedAt: job.queuedAt,
31
+ startedAt: job.startedAt,
32
+ finishedAt: job.finishedAt,
33
+ };
34
+ }
35
+
36
+ /**
37
+ * @param {object} options
38
+ * @param {number} [options.concurrency] 同时跑几个任务
39
+ * @param {(job: object, helpers: object) => Promise<void>} options.execute 真正干活
40
+ */
41
+ export function createJobQueue({ concurrency = DEFAULT_CONCURRENCY, execute } = {}) {
42
+ const limit = Math.max(1, Math.min(6, Math.round(Number(concurrency) || DEFAULT_CONCURRENCY)));
43
+ const jobs = new Map();
44
+ const queue = [];
45
+ let running = 0;
46
+ let seq = 0;
47
+
48
+ function trim() {
49
+ if (jobs.size <= MAX_RECORDS) return;
50
+ const finished = [...jobs.values()]
51
+ .filter((job) => !ACTIVE.has(job.status))
52
+ .sort((a, b) => a.queuedAt - b.queuedAt);
53
+ while (jobs.size > MAX_RECORDS && finished.length) {
54
+ jobs.delete(finished.shift().id);
55
+ }
56
+ }
57
+
58
+ function finish(job, status, error) {
59
+ job.status = status;
60
+ job.finishedAt = Date.now();
61
+ if (error) job.error = clean(error?.message ?? error, 300);
62
+ if (status === 'done') job.percent = 100;
63
+ job.stageLabel = status === 'done' ? '已完成' : status === 'failed' ? '解析失败' : job.stageLabel;
64
+ trim();
65
+ }
66
+
67
+ function pump() {
68
+ while (running < limit && queue.length) {
69
+ const job = queue.shift();
70
+ if (!job || job.status !== 'queued') continue;
71
+ job.status = 'running';
72
+ job.startedAt = Date.now();
73
+ job.stage = 'resolve';
74
+ job.stageLabel = '正在解析视频信息…';
75
+ job.percent = 1;
76
+ running += 1;
77
+
78
+ const controller = new AbortController();
79
+ job.controller = controller;
80
+ const helpers = {
81
+ signal: controller.signal,
82
+ isCancelled: () => job.status === 'cancelled' || controller.signal.aborted,
83
+ setStage(stage, label, percent) {
84
+ if (job.status === 'cancelled') return;
85
+ if (stage) job.stage = stage;
86
+ if (label) job.stageLabel = clean(label, 120);
87
+ if (typeof percent === 'number' && Number.isFinite(percent)) {
88
+ job.percent = Math.max(1, Math.min(99, Math.round(percent)));
89
+ }
90
+ },
91
+ setPercent(percent) {
92
+ if (job.status === 'cancelled') return;
93
+ if (typeof percent === 'number' && Number.isFinite(percent)) {
94
+ job.percent = Math.max(1, Math.min(99, Math.round(percent)));
95
+ }
96
+ },
97
+ warn(message) {
98
+ const text = clean(message, 160);
99
+ if (text && !job.warnings.includes(text)) job.warnings.push(text);
100
+ },
101
+ };
102
+
103
+ Promise.resolve()
104
+ .then(() => execute(job, helpers))
105
+ .then(() => {
106
+ if (job.status === 'running') finish(job, 'done');
107
+ })
108
+ .catch((error) => {
109
+ if (job.status === 'cancelled' || error?.name === 'AbortError' || error?.code === 'aborted') {
110
+ finish(job, 'cancelled');
111
+ return;
112
+ }
113
+ finish(job, 'failed', error);
114
+ })
115
+ .finally(() => {
116
+ running -= 1;
117
+ pump();
118
+ });
119
+ }
120
+ }
121
+
122
+ return {
123
+ concurrency: limit,
124
+ /** 启动一个任务;同一视频已有排队/运行中的任务时直接返回那一个。 */
125
+ start({ docId = '', url = '', title = '', describe = true } = {}) {
126
+ const id = String(docId ?? '').trim();
127
+ const link = String(url ?? '').trim();
128
+ if (!id && !link) throw new Error('需要 docId 或 url');
129
+ for (const job of jobs.values()) {
130
+ if (!ACTIVE.has(job.status)) continue;
131
+ if ((id && job.docId === id) || (!id && link && job.url === link)) return { job: snapshot(job), created: false };
132
+ }
133
+ seq += 1;
134
+ const job = {
135
+ id: `job-${Date.now().toString(36)}-${seq}`,
136
+ docId: id,
137
+ url: link,
138
+ title: clean(title, 160),
139
+ status: 'queued',
140
+ stage: 'queued',
141
+ stageLabel: '排队中…',
142
+ percent: 0,
143
+ describe: describe !== false,
144
+ error: '',
145
+ warnings: [],
146
+ queuedAt: Date.now(),
147
+ startedAt: 0,
148
+ finishedAt: 0,
149
+ controller: null,
150
+ };
151
+ jobs.set(job.id, job);
152
+ queue.push(job);
153
+ trim();
154
+ pump();
155
+ return { job: snapshot(job), created: true };
156
+ },
157
+ /** 取消排队中或运行中的任务。 */
158
+ cancel({ id = '', docId = '' } = {}) {
159
+ const key = String(id ?? '').trim();
160
+ const target = String(docId ?? '').trim();
161
+ let cancelled = 0;
162
+ for (const job of jobs.values()) {
163
+ if (!ACTIVE.has(job.status)) continue;
164
+ if (key ? job.id !== key : job.docId !== target) continue;
165
+ job.status = 'cancelled';
166
+ job.stageLabel = '已取消';
167
+ job.finishedAt = Date.now();
168
+ try {
169
+ job.controller?.abort();
170
+ } catch {
171
+ /* ignore */
172
+ }
173
+ cancelled += 1;
174
+ }
175
+ pump();
176
+ return cancelled;
177
+ },
178
+ /** 清掉已结束的记录。 */
179
+ clearFinished() {
180
+ for (const [id, job] of jobs) {
181
+ if (!ACTIVE.has(job.status)) jobs.delete(id);
182
+ }
183
+ return this.list().jobs.length;
184
+ },
185
+ list() {
186
+ const all = [...jobs.values()]
187
+ .sort((a, b) => b.queuedAt - a.queuedAt)
188
+ .slice(0, MAX_RECORDS)
189
+ .map(snapshot);
190
+ return {
191
+ concurrency: limit,
192
+ active: all.filter((job) => ACTIVE.has(job.status)).length,
193
+ running: all.filter((job) => job.status === 'running').length,
194
+ jobs: all,
195
+ };
196
+ },
197
+ isActive(docId) {
198
+ for (const job of jobs.values()) {
199
+ if (job.docId === docId && ACTIVE.has(job.status)) return true;
200
+ }
201
+ return false;
202
+ },
203
+ };
204
+ }
package/lib/outline.js CHANGED
@@ -99,7 +99,7 @@ function uniformItems(duration, count = 8) {
99
99
  for (let index = 0; index < total; index += 1) {
100
100
  items.push({
101
101
  seconds: Math.round((duration * (index + 0.5)) / total * 10) / 10,
102
- title: '',
102
+ title: `片段 ${index + 1}`,
103
103
  detail: [],
104
104
  });
105
105
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-video-insight",
3
- "version": "0.8.5",
3
+ "version": "0.9.0",
4
4
  "description": "在 DeepSeek Harness 里快速分析视频:左侧栏「视频解析」面板,粘贴 YouTube / 哔哩哔哩链接,自动抓字幕并生成总结、章节、要点、原文与追问;无字幕时可用本机 Whisper / SenseVoice 转写兜底(音频不出本机),纯无声视频还能用画面文字识别(OCR)或视觉模型描述画面;右栏有 AI 思维导图;追问是基于转写稿的 RAG 问答(回答带「依据」时间戳);分析结果按视频缓存,历史记录可回看。",
5
5
  "license": "MIT",
6
6
  "author": "Jerry (Azerz)",