@xiaofanzhao/dsh-plugin-knowledge-base 0.0.0-stage → 0.1.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/lib/util.mjs ADDED
@@ -0,0 +1,129 @@
1
+ // ============================================================================
2
+ // 通用小工具:id、HTTP 收发、请求体读取、跨站防护。
3
+ // 只用 Node 内置模块,不引第三方依赖。
4
+ // ============================================================================
5
+ import { createHash, randomUUID } from 'node:crypto';
6
+
7
+ export const API_BASE = '/api/kb/v1';
8
+
9
+ /** 短 id:前缀 + 10 位十六进制,便于在日志和 URL 里辨认。 */
10
+ export function newId(prefix) {
11
+ return `${prefix}_${randomUUID().replace(/-/g, '').slice(0, 10)}`;
12
+ }
13
+
14
+ export function nowMs() {
15
+ return Date.now();
16
+ }
17
+
18
+ /** 展示用时间(ISO 会被前端格式化,这里只存毫秒)。 */
19
+ export function sha256Hex(buffer) {
20
+ return createHash('sha256').update(buffer).digest('hex');
21
+ }
22
+
23
+ export function sendJson(res, status, body) {
24
+ const payload = JSON.stringify(body ?? null);
25
+ res.writeHead(status, {
26
+ 'content-type': 'application/json; charset=utf-8',
27
+ 'cache-control': 'no-store',
28
+ 'content-length': Buffer.byteLength(payload),
29
+ });
30
+ res.end(payload);
31
+ }
32
+
33
+ export function sendText(res, status, text, type = 'text/plain; charset=utf-8') {
34
+ const payload = Buffer.isBuffer(text) ? text : Buffer.from(String(text ?? ''), 'utf8');
35
+ res.writeHead(status, {
36
+ 'content-type': type,
37
+ 'cache-control': 'no-store',
38
+ 'content-length': payload.length,
39
+ });
40
+ res.end(payload);
41
+ }
42
+
43
+ export function sendBytes(res, status, buffer, type, extraHeaders = {}) {
44
+ const payload = Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer);
45
+ res.writeHead(status, {
46
+ 'content-type': type || 'application/octet-stream',
47
+ 'cache-control': 'no-store',
48
+ 'content-length': payload.length,
49
+ ...extraHeaders,
50
+ });
51
+ res.end(payload);
52
+ }
53
+
54
+ /** 读 JSON/文本请求体,带硬上限,避免恶意大包。 */
55
+ export function readBody(req, limit = 1 << 20) {
56
+ return new Promise((resolve) => {
57
+ const chunks = [];
58
+ let size = 0;
59
+ let overflow = false;
60
+ req.on('data', (chunk) => {
61
+ size += chunk.length;
62
+ if (size > limit) {
63
+ overflow = true;
64
+ return;
65
+ }
66
+ chunks.push(chunk);
67
+ });
68
+ req.on('end', () => {
69
+ if (overflow) return resolve({ ok: false, code: 'too-large' });
70
+ const buffer = Buffer.concat(chunks);
71
+ try {
72
+ const value = JSON.parse(buffer.toString('utf8') || '{}');
73
+ resolve({ ok: true, value: value && typeof value === 'object' ? value : {} });
74
+ } catch {
75
+ resolve({ ok: false, code: 'bad-json' });
76
+ }
77
+ });
78
+ req.on('error', () => resolve({ ok: false, code: 'aborted' }));
79
+ });
80
+ }
81
+
82
+ /**
83
+ * 跨站防护。
84
+ *
85
+ * webServer 的路由没有全局鉴权,用户浏览器里的任意网页都能向 127.0.0.1:<port>
86
+ * 发“简单请求”。这里要求一个自定义头(会触发预检,而我们不应答 CORS,浏览器
87
+ * 就会拦下跨站请求),并在带 Origin 时校验同源。
88
+ */
89
+ export function guard(origin, req) {
90
+ if (req.headers['x-kb-client'] !== '1') return { ok: false, code: 'forbidden' };
91
+ const from = req.headers.origin;
92
+ if (typeof from === 'string' && from && origin && from !== origin) {
93
+ return { ok: false, code: 'forbidden' };
94
+ }
95
+ return { ok: true };
96
+ }
97
+
98
+ /** 从请求头取文件名(客户端用 encodeURIComponent 编码)。 */
99
+ export function filenameFromHeader(req) {
100
+ const raw = req.headers['x-kb-filename'];
101
+ if (typeof raw !== 'string' || !raw) return '';
102
+ try {
103
+ return decodeURIComponent(raw);
104
+ } catch {
105
+ return raw;
106
+ }
107
+ }
108
+
109
+ /** 只保留安全字符,用于展示;落盘永远不用它。 */
110
+ export function sanitizeName(name, fallback = '未命名') {
111
+ const cleaned = String(name ?? '')
112
+ .replace(/[\u0000-\u001f\u007f]/g, '')
113
+ .replace(/[\\/:*?"<>|]/g, ' ')
114
+ .replace(/\s+/g, ' ')
115
+ .trim()
116
+ .slice(0, 120);
117
+ return cleaned || fallback;
118
+ }
119
+
120
+ export function extOf(name) {
121
+ const match = /\.([A-Za-z0-9]{1,12})$/.exec(String(name ?? ''));
122
+ return match ? match[1].toLowerCase() : '';
123
+ }
124
+
125
+ export function clampInt(value, min, max, fallback) {
126
+ const number = Number(value);
127
+ if (!Number.isFinite(number)) return fallback;
128
+ return Math.min(max, Math.max(min, Math.trunc(number)));
129
+ }
package/locale/en.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "meta": {
3
+ "title": "Personal knowledge base",
4
+ "description": "Organize local files into a tree-shaped knowledge base: upload, read in the app, full-text search (PDF and Chinese included). The agent can search it and talk about a single document.",
5
+ "detail": "docs/DETAIL.md"
6
+ },
7
+ "knowledgeBase": {
8
+ "panel": "Knowledge base",
9
+ "panelTip": "Personal knowledge base",
10
+ "newKb": "New library",
11
+ "newFolder": "New folder",
12
+ "import": "Import files",
13
+ "search": "Search library",
14
+ "searchPlaceholder": "Search titles and text…",
15
+ "empty": "Nothing here yet. Drop files in, or create a library.",
16
+ "emptyKb": "This library is empty. Import files or drop them here.",
17
+ "emptySearch": "No match.",
18
+ "askAi": "Ask AI",
19
+ "reindex": "Reindex",
20
+ "remove": "Delete",
21
+ "rename": "Rename",
22
+ "openOriginal": "Original file",
23
+ "readable": "Read here",
24
+ "settings": "Knowledge base",
25
+ "root": "Storage folder",
26
+ "stats": "Statistics",
27
+ "rebuild": "Rebuild all indexes",
28
+ "indexing": "Indexing",
29
+ "ready": "Indexed",
30
+ "failed": "Failed",
31
+ "pending": "Pending"
32
+ }
33
+ }
package/locale/zh.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "meta": {
3
+ "title": "个人知识库",
4
+ "description": "把本地文件整理成树状知识库:上传、在线查看、全文检索(含 PDF 与中文),AI 可以检索这些资料,也可以针对某一篇文档对话。",
5
+ "detail": "docs/DETAIL.md"
6
+ },
7
+ "knowledgeBase": {
8
+ "panel": "知识库",
9
+ "panelTip": "个人知识库",
10
+ "newKb": "新建知识库",
11
+ "newFolder": "新建文件夹",
12
+ "import": "导入文件",
13
+ "search": "搜索知识库",
14
+ "searchPlaceholder": "搜索标题或正文…",
15
+ "empty": "还没有内容。拖文件进来,或点「新建知识库」开始。",
16
+ "emptyKb": "这个知识库还是空的。点「导入文件」或把文件拖进来。",
17
+ "emptySearch": "没有找到匹配的内容。",
18
+ "askAi": "问 AI",
19
+ "reindex": "重新索引",
20
+ "remove": "删除",
21
+ "rename": "重命名",
22
+ "openOriginal": "原文件",
23
+ "readable": "在线阅读",
24
+ "settings": "知识库",
25
+ "root": "存放目录",
26
+ "stats": "统计",
27
+ "rebuild": "重建全部索引",
28
+ "indexing": "索引中",
29
+ "ready": "已索引",
30
+ "failed": "解析失败",
31
+ "pending": "等待解析"
32
+ }
33
+ }
package/package.json CHANGED
@@ -1,6 +1,65 @@
1
1
  {
2
2
  "name": "@xiaofanzhao/dsh-plugin-knowledge-base",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "个人知识库:树状目录、文件上传与在线查看、全文检索(含 PDF 与中文),AI 可通过工具检索并针对文档对话。",
5
+ "type": "module",
6
+ "main": "./index.js",
7
+ "exports": {
8
+ ".": "./index.js",
9
+ "./client": "./client.js",
10
+ "./locale/*.json": "./locale/*.json",
11
+ "./icon": "./icon.svg",
12
+ "./package.json": "./package.json"
13
+ },
14
+ "files": [
15
+ "index.js",
16
+ "client.js",
17
+ "cordis.patch.yml",
18
+ "lib",
19
+ "locale",
20
+ "docs",
21
+ "icon.svg"
22
+ ],
23
+ "keywords": [
24
+ "deepseek-harness",
25
+ "dsh",
26
+ "dsh-plugin",
27
+ "knowledge-base",
28
+ "rag",
29
+ "full-text-search"
30
+ ],
31
+ "author": "FSNH",
32
+ "license": "MIT",
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "git+https://github.com/FSNH/dsh-plugin-knowledge-base.git"
36
+ },
37
+ "homepage": "https://github.com/FSNH/dsh-plugin-knowledge-base#readme",
38
+ "bugs": {
39
+ "url": "https://github.com/FSNH/dsh-plugin-knowledge-base/issues"
40
+ },
41
+ "engines": {
42
+ "node": ">=24"
43
+ },
44
+ "publishConfig": {
45
+ "access": "public",
46
+ "registry": "https://registry.npmjs.org/"
47
+ },
48
+ "dependencies": {
49
+ "pdfjs-dist": "^4.10.38"
50
+ },
51
+ "dsh": {
52
+ "manifestVersion": 1,
53
+ "bundle": {
54
+ "patch": "./cordis.patch.yml"
55
+ },
56
+ "client": {
57
+ "platform": "web",
58
+ "immediately": true,
59
+ "inject": [
60
+ "@deepseek-ai/dsh-client-ui-layout",
61
+ "@deepseek-ai/dsh-client-ui-sidebar"
62
+ ]
63
+ }
64
+ }
65
+ }