lantern-harness 0.1.3 → 0.1.5
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/CHANGELOG.md +68 -0
- package/README.md +9 -9
- package/builtin/session/lh-nav-groups.mjs +106 -9
- package/cli/data-root.mjs +30 -0
- package/cli/lantern.mjs +34 -6
- package/cli/server.mjs +4 -0
- package/config/app.defaults.json +5 -1
- package/dist/500.html +18 -4
- package/dist/_astro/Base.CVuoZiOB.css +1 -0
- package/dist/_astro/{Base.astro_astro_type_script_index_0_lang.Cg_8YLVA.js → Base.astro_astro_type_script_index_0_lang.BY4OccDH.js} +1 -1
- package/dist/_astro/CaseExecBoard.GIKQ6ccZ.js +1 -0
- package/dist/_astro/CaseGenerateApp.CGAvw04r.js +9 -0
- package/dist/_astro/CasesLibrary.Bz6ol7zL.js +1 -0
- package/dist/_astro/ChatApp.DZtDZRst.js +273 -0
- package/dist/_astro/ConfirmDialog.CBVsDJ8f.js +1 -0
- package/dist/_astro/DashboardBoard.CiTrVBqY.js +5 -0
- package/dist/_astro/DatePicker.CMfX7F6z.js +1 -0
- package/dist/_astro/DocsFileBrowser.CBXAmRPO.js +1 -0
- package/dist/_astro/DocsPager.CPwAnvW_.js +1 -0
- package/dist/_astro/DrawerField.KANqQwFl.js +1 -0
- package/dist/_astro/KnowledgeLibrary._QhB3GHk.js +1 -0
- package/dist/_astro/NamePromptDialog.Dl5kSnQI.js +1 -0
- package/dist/_astro/NavLibraries.fxwcz0b0.js +1 -0
- package/dist/_astro/PluginsPage.BfG3nVYJ.js +1 -0
- package/dist/_astro/PluginsPage.n_0T_ww4.js +39 -0
- package/dist/_astro/PromptsLibrary.BLPVH34I.js +1 -0
- package/dist/_astro/ReportsPage.DMk4sUqZ.js +1 -0
- package/dist/_astro/RequirementsLibrary.CyYBiauz.js +1 -0
- package/dist/_astro/SearchableSelect.yFomIhKQ.js +1 -0
- package/dist/_astro/SelectDropdown.flhHcwyd.js +1 -0
- package/dist/_astro/SettingsDialog.DrVaJAbf.js +1 -0
- package/dist/_astro/SpaceSwitcher.C84a-OBd.js +7 -0
- package/dist/_astro/TagSelect.u8DTop7v.js +1 -0
- package/dist/_astro/TasksBoard.Dwva6gEQ.js +1 -0
- package/dist/_astro/TemplatesLibrary.DV_MUASP.js +1 -0
- package/dist/_astro/TestStatsBoard.Cl6mBowv.js +1 -0
- package/dist/_astro/TodosBoard.BTl5qvQt.js +1 -0
- package/dist/_astro/active-element-history.CeefGkFQ.js +1 -0
- package/dist/_astro/app-config.BAFE-Tim.js +1 -0
- package/dist/_astro/case-exec.HjGrOY_O.js +1 -0
- package/dist/_astro/dialog.DXHznyjD.js +1 -0
- package/dist/_astro/i18n.DFwWWvim.js +7 -0
- package/dist/_astro/listbox.Di-t5U37.js +1 -0
- package/dist/_astro/menu.Bkluqxic.js +1 -0
- package/dist/_astro/{open-docs-in-session.fLwnA5qh.js → open-docs-in-session.D7Hxuu0K.js} +1 -1
- package/dist/_astro/plugins.BHRarX88.js +1 -0
- package/dist/_astro/portal.Cm9XlIPf.js +5 -0
- package/dist/_astro/report-prompt.Cmw4XzfY.js +1 -0
- package/dist/_astro/{settings.CkbipdPI.js → settings.1aF-h9sh.js} +2 -2
- package/dist/_astro/tasks.hIKi1ek-.js +1 -0
- package/dist/_astro/test-stats.BIHEJz5c.js +1 -0
- package/dist/_astro/use-text-value.BuY0Cm1z.js +1 -0
- package/dist/_astro/use-tree-walker.CfTHFX43.js +1 -0
- package/dist/_astro/useListAnim.CpGB1cft.js +1 -0
- package/dist/_astro/view-prefs.DHPN5Yq6.js +1 -0
- package/dist/case-exec/index.html +267 -0
- package/dist/cases/generate/index.html +19 -5
- package/dist/cases/library/index.html +19 -5
- package/dist/cn-holidays.json +42 -0
- package/dist/dashboard/index.html +268 -0
- package/dist/docs/changelog/index.html +81 -5
- package/dist/docs/install/index.html +21 -7
- package/dist/docs/manual/index.html +124 -19
- package/dist/index.html +19 -5
- package/dist/knowledge/index.html +19 -5
- package/dist/knowledge//345/212/237/350/203/275/346/265/213/350/257/225/347/224/250/344/276/213/347/274/226/345/206/231/350/247/204/350/214/203/index.html +18 -4
- package/dist/plugins/index.html +19 -5
- package/dist/prompts/index.html +19 -5
- package/dist/reports/index.html +267 -0
- package/dist/requirements/index.html +19 -5
- package/dist/tasks/index.html +19 -5
- package/dist/templates/index.html +19 -5
- package/dist/templates//345/212/237/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212//345/212/237/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212/346/250/241/346/235/277/index.html +18 -4
- package/dist/templates//346/200/247/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212//346/200/247/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212/346/250/241/346/235/277/index.html +18 -4
- package/dist/templates//346/200/247/350/203/275/346/265/213/350/257/225/347/224/250/344/276/213//346/200/247/350/203/275/346/265/213/350/257/225/347/224/250/344/276/213/346/250/241/346/235/277/index.html +18 -4
- package/dist/templates//346/216/245/345/217/243/346/265/213/350/257/225/346/212/245/345/221/212//346/216/245/345/217/243/346/265/213/350/257/225/346/212/245/345/221/212/346/250/241/346/235/277/index.html +18 -4
- package/dist/templates//350/207/252/345/212/250/345/214/226/346/265/213/350/257/225/347/224/250/344/276/213//350/207/252/345/212/250/345/214/226/346/265/213/350/257/225/347/224/250/344/276/213/346/250/241/346/235/277/index.html +18 -4
- package/dist/test-stats/index.html +267 -0
- package/dist/todos/index.html +19 -5
- package/docs//345/205/250/345/261/200/346/217/222/344/273/266.md +1 -1
- package/docs//345/256/211/350/243/205/346/211/213/345/206/214.md +3 -3
- package/docs//346/223/215/344/275/234/346/211/213/345/206/214.md +106 -34
- package/lh-plugins/lh-project-docs.mjs +1 -1
- package/lh-plugins/lh-reports.mjs +413 -0
- package/lh-plugins/lh-stats.mjs +409 -0
- package/lh-plugins/lh-tasks.mjs +7 -6
- package/lh-plugins/lh-todos.mjs +7 -9
- package/lh-plugins/lh-work-store.mjs +81 -0
- package/lh-plugins/storage-domain.mjs +54 -4
- package/package.json +3 -1
- package/config/seeds//346/217/220/347/244/272/350/257/215/347/256/241/347/220/206//345/212/237/350/203/275/346/265/213/350/257/225/347/224/250/344/276/213/346/217/220/347/244/272/350/257/215v1.0.md +0 -155
- package/dist/_astro/Base.ByB0D4ZA.css +0 -1
- package/dist/_astro/CaseGenerateApp.BlZyjWEr.js +0 -9
- package/dist/_astro/CasesLibrary.C76La3Xj.js +0 -1
- package/dist/_astro/ChatApp.gfsPLvan.js +0 -269
- package/dist/_astro/ConfirmDialog.BYz4HREM.js +0 -1
- package/dist/_astro/DatePicker.BdwFRioL.js +0 -1
- package/dist/_astro/DocsFileBrowser.BxAyrkkw.js +0 -1
- package/dist/_astro/KnowledgeLibrary.Cv488Hot.js +0 -1
- package/dist/_astro/NamePromptDialog.DGBlDH_k.js +0 -1
- package/dist/_astro/NavLibraries.ALZ9e12z.js +0 -1
- package/dist/_astro/PluginsPage.B2uxCiHL.js +0 -1
- package/dist/_astro/PluginsPage.DEkyOfQE.js +0 -39
- package/dist/_astro/PromptsLibrary.BUC1OiCH.js +0 -1
- package/dist/_astro/RequirementsLibrary.D3olQI63.js +0 -1
- package/dist/_astro/SearchableSelect.DP74JRWW.js +0 -1
- package/dist/_astro/SelectDropdown.D2i7-lQC.js +0 -1
- package/dist/_astro/SettingsDialog.CrMDjSLJ.js +0 -1
- package/dist/_astro/SlideOverDrawer.C7UVeCX1.js +0 -1
- package/dist/_astro/SpaceSwitcher.Wf8EPYUx.js +0 -7
- package/dist/_astro/TasksBoard.2j7rGT9O.js +0 -1
- package/dist/_astro/TemplatesLibrary.J0x7yIcY.js +0 -1
- package/dist/_astro/TodosBoard.BugeB7_7.js +0 -1
- package/dist/_astro/app-config.vqDkEMS3.js +0 -1
- package/dist/_astro/i18n.9qZsrs3u.js +0 -7
- package/dist/_astro/listbox.DdDDDJ5Z.js +0 -1
- package/dist/_astro/menu.CqkL87bV.js +0 -1
- package/dist/_astro/plugins.DSUPSizB.js +0 -1
- package/dist/_astro/portal.C3KrUREL.js +0 -5
- package/dist/_astro/use-text-value.C0IDspmh.js +0 -1
- package/dist/_astro/use-tree-walker.oRTnhHS-.js +0 -1
- package/dist/_astro/useListAnim.Y3N2budK.js +0 -1
- /package/dist/{apple-touch-icon.png → brand/apple-touch-icon.png} +0 -0
- /package/dist/{brand-mark.png → brand/brand-mark.png} +0 -0
- /package/dist/{favicon.png → brand/favicon.png} +0 -0
- /package/dist/{icon-192.png → brand/icon-192.png} +0 -0
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工作汇报插件:开关联动「提示词 + 汇报文件夹」。
|
|
3
|
+
*
|
|
4
|
+
* - 打开:补齐 `提示词管理/工作汇报提示词.md`(已存在不覆盖,保留用户改过的内容),
|
|
5
|
+
* 同时在当前空间铺出 `工作汇报/日报`、`工作汇报/周报`、`工作汇报/月报` 三个目录
|
|
6
|
+
* - 关闭:只把插件停用,**提示词文件保留**(下次打开还是原来那份,改过的内容不会丢);
|
|
7
|
+
* 汇报文件夹与里面已有的汇报同样保留
|
|
8
|
+
*
|
|
9
|
+
* 用哪份提示词可以换:状态存在 `.lantern/local/reports.json`,页面上的下拉框直接改;
|
|
10
|
+
* 选中的文件被改名或删掉时,自动回退到默认名 / 名字里带「工作汇报」的提示词。
|
|
11
|
+
*
|
|
12
|
+
* 汇报列表直接读上面三个目录里的文本文件(.md / .txt / .html),文件就是数据,
|
|
13
|
+
* 页面上按「生成日期 + 文件名」展示,点文件名用系统默认程序在本地打开(`/__tb_reports/open`);
|
|
14
|
+
* `/__tb_reports/file` 仍保留,供其它客户端直接取正文。
|
|
15
|
+
*/
|
|
16
|
+
import fs from 'node:fs';
|
|
17
|
+
import path from 'node:path';
|
|
18
|
+
|
|
19
|
+
import { getDataRoot } from '../builtin/session/space-context.mjs';
|
|
20
|
+
import { resolveContentRoot, spaceLocalFile } from '../builtin/session/space-layout.mjs';
|
|
21
|
+
import { DOC_ROOT_DIRS, openLocalPath } from './lh-project-docs.mjs';
|
|
22
|
+
|
|
23
|
+
const PREFIX = '/__tb_reports';
|
|
24
|
+
|
|
25
|
+
/** 提示词管理里的默认文件名(前端读同一个名字) */
|
|
26
|
+
export const REPORT_PROMPT_FILE = '工作汇报提示词.md';
|
|
27
|
+
|
|
28
|
+
/** 汇报根目录与三类子目录:日报 / 周报 / 月报 */
|
|
29
|
+
export const REPORT_ROOT_DIR = '工作汇报';
|
|
30
|
+
export const REPORT_KINDS = {
|
|
31
|
+
day: '日报',
|
|
32
|
+
week: '周报',
|
|
33
|
+
month: '月报',
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** 提示词下拉里不参与选择的文件:空间默认提示词不属于任何插件 */
|
|
37
|
+
const RESERVED_PROMPT_FILES = new Set(['默认提示词.md']);
|
|
38
|
+
|
|
39
|
+
const PROMPT_EXTS = new Set(['.md', '.markdown', '.txt']);
|
|
40
|
+
|
|
41
|
+
/** 打开插件时生成的提示词正文:先给一份最小可用的,用户之后自己补充 */
|
|
42
|
+
export const REPORT_PROMPT_TEMPLATE = `你是「工作汇报」助手,帮用户写日报 / 周报 / 月报。
|
|
43
|
+
|
|
44
|
+
- 用户没说明类型时按周报写;材料不足就按已知信息先写,末尾用「待补充」列出缺口,不要编造数据。
|
|
45
|
+
- 结构:一句话结论 / 已完成(做了什么 → 结果) / 进行中与风险 / 下一步计划。
|
|
46
|
+
- 条目动词开头,能写数字就写数字;默认输出 Markdown,不写入文件,除非用户明确要求。
|
|
47
|
+
- 用户要求存盘时,按类型放进「工作汇报」下对应的「日报 / 周报 / 月报」子目录,文件名用日期:日报与周报用 YYYY-MM-DD.md,月报用 YYYY-MM.md。
|
|
48
|
+
`;
|
|
49
|
+
|
|
50
|
+
/** 列表里认的文本文件:其它格式(docx / pdf)交给系统程序打开,不在页面渲染 */
|
|
51
|
+
const TEXT_EXTS = new Set(['.md', '.markdown', '.txt', '.html', '.htm']);
|
|
52
|
+
|
|
53
|
+
const CONTENT_TYPES = {
|
|
54
|
+
'.md': 'text/plain; charset=utf-8',
|
|
55
|
+
'.markdown': 'text/plain; charset=utf-8',
|
|
56
|
+
'.txt': 'text/plain; charset=utf-8',
|
|
57
|
+
'.html': 'text/html; charset=utf-8',
|
|
58
|
+
'.htm': 'text/html; charset=utf-8',
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
function sendJson(res, status, body) {
|
|
62
|
+
res.statusCode = status;
|
|
63
|
+
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
|
64
|
+
res.end(JSON.stringify(body));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function readBody(req) {
|
|
68
|
+
return new Promise((resolve, reject) => {
|
|
69
|
+
const chunks = [];
|
|
70
|
+
req.on('data', (c) => chunks.push(c));
|
|
71
|
+
req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
|
|
72
|
+
req.on('error', reject);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 当前空间的提示词目录(可能被归进分组文件夹,统一走 resolveContentRoot) */
|
|
77
|
+
function promptsDir(spaceDir) {
|
|
78
|
+
return resolveContentRoot(spaceDir, DOC_ROOT_DIRS.prompts.dirName).abs;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function reportsRoot(spaceDir) {
|
|
82
|
+
// 跟着菜单分组走:被拖进分组文件夹时,汇报目录也在分组里
|
|
83
|
+
return resolveContentRoot(spaceDir, REPORT_ROOT_DIR).abs;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** 相对空间的目录(分组后是「分组名/工作汇报/日报」),页面顶部展示用 */
|
|
87
|
+
function reportKindRel(spaceDir, kind) {
|
|
88
|
+
const dir = kindDir(spaceDir, kind);
|
|
89
|
+
if (!dir) return '';
|
|
90
|
+
return path.relative(path.resolve(spaceDir), dir).split(path.sep).join('/');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** 某一类的目录(kind 非法时返回 null) */
|
|
94
|
+
function kindDir(spaceDir, kind) {
|
|
95
|
+
const name = REPORT_KINDS[kind];
|
|
96
|
+
return name ? path.join(reportsRoot(spaceDir), name) : null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** 铺出 工作汇报/日报|周报|月报,已存在就跳过 */
|
|
100
|
+
function ensureReportDirs(spaceDir) {
|
|
101
|
+
const root = reportsRoot(spaceDir);
|
|
102
|
+
fs.mkdirSync(root, { recursive: true });
|
|
103
|
+
for (const name of Object.values(REPORT_KINDS)) {
|
|
104
|
+
fs.mkdirSync(path.join(root, name), { recursive: true });
|
|
105
|
+
}
|
|
106
|
+
return root;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/* ── 插件状态:开关 + 选中的提示词文件名 ── */
|
|
110
|
+
|
|
111
|
+
function statePath(spaceDir) {
|
|
112
|
+
return spaceLocalFile(spaceDir, 'reports.json');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function readState(spaceDir) {
|
|
116
|
+
try {
|
|
117
|
+
const raw = JSON.parse(fs.readFileSync(statePath(spaceDir), 'utf8'));
|
|
118
|
+
return {
|
|
119
|
+
enabled: raw?.enabled === true,
|
|
120
|
+
promptFile: typeof raw?.promptFile === 'string' ? raw.promptFile.trim() : '',
|
|
121
|
+
};
|
|
122
|
+
} catch {
|
|
123
|
+
return { enabled: false, promptFile: '' };
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function writeState(spaceDir, state) {
|
|
128
|
+
const fp = statePath(spaceDir);
|
|
129
|
+
try {
|
|
130
|
+
fs.mkdirSync(path.dirname(fp), { recursive: true });
|
|
131
|
+
const tmp = `${fp}.${process.pid}.tmp`;
|
|
132
|
+
fs.writeFileSync(
|
|
133
|
+
tmp,
|
|
134
|
+
`${JSON.stringify(
|
|
135
|
+
{ enabled: state.enabled === true, promptFile: state.promptFile || '' },
|
|
136
|
+
null,
|
|
137
|
+
2,
|
|
138
|
+
)}\n`,
|
|
139
|
+
'utf8',
|
|
140
|
+
);
|
|
141
|
+
fs.renameSync(tmp, fp);
|
|
142
|
+
} catch {
|
|
143
|
+
// 状态只是锦上添花:写不进去也不影响提示词本身
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** 提示词目录里的候选文件(排除空间默认提示词与隐藏文件) */
|
|
148
|
+
function listPromptFiles(spaceDir) {
|
|
149
|
+
const dir = promptsDir(spaceDir);
|
|
150
|
+
let names = [];
|
|
151
|
+
try {
|
|
152
|
+
names = fs
|
|
153
|
+
.readdirSync(dir, { withFileTypes: true })
|
|
154
|
+
.filter((ent) => ent.isFile() && !ent.name.startsWith('.'))
|
|
155
|
+
.map((ent) => ent.name)
|
|
156
|
+
.filter((name) => PROMPT_EXTS.has(path.extname(name).toLowerCase()))
|
|
157
|
+
.filter((name) => !RESERVED_PROMPT_FILES.has(name));
|
|
158
|
+
} catch {
|
|
159
|
+
return [];
|
|
160
|
+
}
|
|
161
|
+
return names
|
|
162
|
+
.map((name) => {
|
|
163
|
+
const abs = path.join(dir, name);
|
|
164
|
+
let stat = null;
|
|
165
|
+
try {
|
|
166
|
+
stat = fs.statSync(abs);
|
|
167
|
+
} catch {
|
|
168
|
+
// ignore
|
|
169
|
+
}
|
|
170
|
+
return { name, size: stat?.size ?? 0, mtime: stat?.mtimeMs ?? 0 };
|
|
171
|
+
})
|
|
172
|
+
.sort((a, b) => b.mtime - a.mtime);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 解析当前生效的提示词文件名。
|
|
177
|
+
* 选中的文件被改名 / 删除时,依次回退:默认名 → 名字里带「工作汇报」→ 无。
|
|
178
|
+
* @returns {string} 文件名;没有可用文件时为空串
|
|
179
|
+
*/
|
|
180
|
+
function resolvePromptName(spaceDir, state) {
|
|
181
|
+
const files = listPromptFiles(spaceDir);
|
|
182
|
+
const names = new Set(files.map((f) => f.name));
|
|
183
|
+
if (state.promptFile && names.has(state.promptFile)) return state.promptFile;
|
|
184
|
+
if (names.has(REPORT_PROMPT_FILE)) return REPORT_PROMPT_FILE;
|
|
185
|
+
const guess = files.find((f) => f.name.includes('工作汇报'));
|
|
186
|
+
return guess ? guess.name : '';
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** 打开插件:缺提示词就补一份;已有内容不覆盖(用户的修改优先) */
|
|
190
|
+
function ensurePrompt(spaceDir, state) {
|
|
191
|
+
// 只认现在还在的文件;选中的文件被改名 / 删掉时回退到默认名,不复活旧名字
|
|
192
|
+
const name = resolvePromptName(spaceDir, state) || REPORT_PROMPT_FILE;
|
|
193
|
+
const dir = promptsDir(spaceDir);
|
|
194
|
+
const dest = path.join(dir, name);
|
|
195
|
+
if (fs.existsSync(dest)) return { created: false, name };
|
|
196
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
197
|
+
fs.writeFileSync(dest, REPORT_PROMPT_TEMPLATE, 'utf8');
|
|
198
|
+
return { created: true, name };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** 文件读取地址:页面已不再展示,供其它客户端直接取正文 */
|
|
202
|
+
function fileUrl(kind, name) {
|
|
203
|
+
const qs = new URLSearchParams({ kind, name });
|
|
204
|
+
return `${PREFIX}/file?${qs.toString()}`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** 列表项:只认文本文件,按修改时间从新到旧 */
|
|
208
|
+
function listReports(spaceDir, kind) {
|
|
209
|
+
const dir = kindDir(spaceDir, kind);
|
|
210
|
+
if (!dir) return null;
|
|
211
|
+
ensureReportDirs(spaceDir);
|
|
212
|
+
const items = [];
|
|
213
|
+
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
214
|
+
if (!ent.isFile() || ent.name.startsWith('.')) continue;
|
|
215
|
+
const ext = path.extname(ent.name).toLowerCase();
|
|
216
|
+
if (!TEXT_EXTS.has(ext)) continue;
|
|
217
|
+
const abs = path.join(dir, ent.name);
|
|
218
|
+
let stat;
|
|
219
|
+
try {
|
|
220
|
+
stat = fs.statSync(abs);
|
|
221
|
+
} catch {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
items.push({
|
|
225
|
+
name: ent.name,
|
|
226
|
+
kind,
|
|
227
|
+
size: stat.size,
|
|
228
|
+
generatedAt: stat.mtimeMs,
|
|
229
|
+
url: fileUrl(kind, ent.name),
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
items.sort((a, b) => b.generatedAt - a.generatedAt);
|
|
233
|
+
return items;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** 解析文件请求:kind + 文件名,禁止跳出该类目录 */
|
|
237
|
+
function resolveReportFile(spaceDir, kind, name, options = {}) {
|
|
238
|
+
const dir = kindDir(spaceDir, kind);
|
|
239
|
+
if (!dir || !name) return null;
|
|
240
|
+
const base = path.basename(name);
|
|
241
|
+
if (!base || base.startsWith('.')) return null;
|
|
242
|
+
const ext = path.extname(base).toLowerCase();
|
|
243
|
+
// 页面渲染只认文本;「在本地打开」交给系统默认程序,任何文件都能开
|
|
244
|
+
if (!options.anyExt && !TEXT_EXTS.has(ext)) return null;
|
|
245
|
+
const abs = path.join(dir, base);
|
|
246
|
+
if (!abs.startsWith(dir + path.sep)) return null;
|
|
247
|
+
if (!fs.existsSync(abs)) return null;
|
|
248
|
+
return { abs, base, ext };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* @returns {import('vite').Plugin}
|
|
253
|
+
*/
|
|
254
|
+
export function lhReportsPlugin() {
|
|
255
|
+
return {
|
|
256
|
+
name: 'lh-reports',
|
|
257
|
+
configureServer(server) {
|
|
258
|
+
server.middlewares.use(async (req, res, next) => {
|
|
259
|
+
const url = req.url?.split('?')[0] ?? '';
|
|
260
|
+
if (!url.startsWith(PREFIX)) return next();
|
|
261
|
+
|
|
262
|
+
try {
|
|
263
|
+
// 数据根惰性读取:切换空间后自动作用到新空间
|
|
264
|
+
const spaceDir = getDataRoot();
|
|
265
|
+
const query = new URLSearchParams(req.url?.split('?')[1] ?? '');
|
|
266
|
+
const state = readState(spaceDir);
|
|
267
|
+
const promptName = resolvePromptName(spaceDir, state);
|
|
268
|
+
|
|
269
|
+
if (url === `${PREFIX}/health` && req.method === 'GET') {
|
|
270
|
+
return sendJson(res, 200, {
|
|
271
|
+
ok: true,
|
|
272
|
+
exists: Boolean(promptName),
|
|
273
|
+
file: promptName || REPORT_PROMPT_FILE,
|
|
274
|
+
root: REPORT_ROOT_DIR,
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** 当前状态:开关 + 生效的提示词 */
|
|
279
|
+
if (url === `${PREFIX}/state` && req.method === 'GET') {
|
|
280
|
+
return sendJson(res, 200, {
|
|
281
|
+
ok: true,
|
|
282
|
+
enabled: state.enabled,
|
|
283
|
+
promptFile: promptName,
|
|
284
|
+
exists: Boolean(promptName),
|
|
285
|
+
root: REPORT_ROOT_DIR,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** 提示词下拉的候选列表 */
|
|
290
|
+
if (url === `${PREFIX}/prompts` && req.method === 'GET') {
|
|
291
|
+
const files = listPromptFiles(spaceDir);
|
|
292
|
+
return sendJson(res, 200, {
|
|
293
|
+
ok: true,
|
|
294
|
+
promptFile: promptName,
|
|
295
|
+
items: files.map((f) => ({
|
|
296
|
+
...f,
|
|
297
|
+
selected: f.name === promptName,
|
|
298
|
+
})),
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** 切换生效的提示词:只改「用哪份」,不动文件内容 */
|
|
303
|
+
if (url === `${PREFIX}/select` && req.method === 'POST') {
|
|
304
|
+
const body = JSON.parse((await readBody(req)) || '{}');
|
|
305
|
+
const file = path.basename(String(body.file ?? '').trim());
|
|
306
|
+
const names = new Set(listPromptFiles(spaceDir).map((f) => f.name));
|
|
307
|
+
if (!file || !names.has(file)) {
|
|
308
|
+
return sendJson(res, 404, { ok: false, error: '提示词不存在' });
|
|
309
|
+
}
|
|
310
|
+
writeState(spaceDir, { enabled: state.enabled, promptFile: file });
|
|
311
|
+
return sendJson(res, 200, { ok: true, promptFile: file });
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (url === `${PREFIX}/prompt` && req.method === 'GET') {
|
|
315
|
+
if (!promptName) {
|
|
316
|
+
return sendJson(res, 200, { ok: true, exists: false, content: '' });
|
|
317
|
+
}
|
|
318
|
+
const dest = path.join(promptsDir(spaceDir), promptName);
|
|
319
|
+
return sendJson(res, 200, {
|
|
320
|
+
ok: true,
|
|
321
|
+
exists: true,
|
|
322
|
+
file: promptName,
|
|
323
|
+
content: fs.readFileSync(dest, 'utf8'),
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (url === `${PREFIX}/sync` && req.method === 'POST') {
|
|
328
|
+
const body = JSON.parse((await readBody(req)) || '{}');
|
|
329
|
+
if (body.enabled === true) {
|
|
330
|
+
const out = ensurePrompt(spaceDir, state);
|
|
331
|
+
ensureReportDirs(spaceDir);
|
|
332
|
+
writeState(spaceDir, { enabled: true, promptFile: out.name });
|
|
333
|
+
return sendJson(res, 200, {
|
|
334
|
+
ok: true,
|
|
335
|
+
enabled: true,
|
|
336
|
+
file: out.name,
|
|
337
|
+
created: out.created,
|
|
338
|
+
root: REPORT_ROOT_DIR,
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
// 关闭只停用,不删文件:重新打开仍是原来那份(改过的内容也还在)
|
|
342
|
+
writeState(spaceDir, { enabled: false, promptFile: promptName });
|
|
343
|
+
return sendJson(res, 200, {
|
|
344
|
+
ok: true,
|
|
345
|
+
enabled: false,
|
|
346
|
+
file: promptName || REPORT_PROMPT_FILE,
|
|
347
|
+
removed: false,
|
|
348
|
+
root: REPORT_ROOT_DIR,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (url === `${PREFIX}/list` && req.method === 'GET') {
|
|
353
|
+
const kind = query.get('kind') ?? 'day';
|
|
354
|
+
const items = listReports(spaceDir, kind);
|
|
355
|
+
if (!items) return sendJson(res, 400, { error: `未知的汇报类型:${kind}` });
|
|
356
|
+
return sendJson(res, 200, {
|
|
357
|
+
ok: true,
|
|
358
|
+
kind,
|
|
359
|
+
label: REPORT_KINDS[kind],
|
|
360
|
+
dir: reportKindRel(spaceDir, kind),
|
|
361
|
+
items,
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (url === `${PREFIX}/file` && req.method === 'GET') {
|
|
366
|
+
const found = resolveReportFile(
|
|
367
|
+
spaceDir,
|
|
368
|
+
query.get('kind') ?? 'day',
|
|
369
|
+
query.get('name') ?? '',
|
|
370
|
+
);
|
|
371
|
+
if (!found) return sendJson(res, 404, { error: '文件不存在' });
|
|
372
|
+
const buf = fs.readFileSync(found.abs);
|
|
373
|
+
res.statusCode = 200;
|
|
374
|
+
res.setHeader('Content-Type', CONTENT_TYPES[found.ext] ?? 'text/plain; charset=utf-8');
|
|
375
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
376
|
+
// 文件名可能含中文:按 RFC 5987 给一份 UTF-8 名,避免乱码
|
|
377
|
+
res.setHeader(
|
|
378
|
+
'Content-Disposition',
|
|
379
|
+
`inline; filename*=UTF-8''${encodeURIComponent(found.base)}`,
|
|
380
|
+
);
|
|
381
|
+
res.end(buf);
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** 在本地打开:交给系统默认程序(macOS 的访达/编辑器、Windows 的关联程序) */
|
|
386
|
+
if (url === `${PREFIX}/open` && req.method === 'POST') {
|
|
387
|
+
const body = JSON.parse((await readBody(req)) || '{}');
|
|
388
|
+
const found = resolveReportFile(
|
|
389
|
+
spaceDir,
|
|
390
|
+
typeof body.kind === 'string' ? body.kind : 'day',
|
|
391
|
+
typeof body.name === 'string' ? body.name : '',
|
|
392
|
+
{ anyExt: true },
|
|
393
|
+
);
|
|
394
|
+
if (!found) return sendJson(res, 404, { ok: false, error: '文件不存在' });
|
|
395
|
+
try {
|
|
396
|
+
await openLocalPath(found.abs);
|
|
397
|
+
return sendJson(res, 200, { ok: true, name: found.base });
|
|
398
|
+
} catch (e) {
|
|
399
|
+
return sendJson(res, 500, {
|
|
400
|
+
ok: false,
|
|
401
|
+
error: e instanceof Error ? e.message : String(e),
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
return sendJson(res, 404, { error: 'not found' });
|
|
407
|
+
} catch (e) {
|
|
408
|
+
return sendJson(res, 500, { error: e instanceof Error ? e.message : String(e) });
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
},
|
|
412
|
+
};
|
|
413
|
+
}
|