plugin-ai-chat-file-preview 1.0.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.
@@ -0,0 +1,96 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ const DB_NAME = 'nb-ai-chat-file-preview';
11
+ const DB_VERSION = 1;
12
+ const STORE_NAME = 'blobs';
13
+
14
+ function openDB(): Promise<IDBDatabase> {
15
+ return new Promise((resolve, reject) => {
16
+ const request = indexedDB.open(DB_NAME, DB_VERSION);
17
+ request.onupgradeneeded = () => {
18
+ const db = request.result;
19
+ if (!db.objectStoreNames.contains(STORE_NAME)) {
20
+ db.createObjectStore(STORE_NAME);
21
+ }
22
+ };
23
+ request.onsuccess = () => resolve(request.result);
24
+ request.onerror = () => reject(request.error);
25
+ });
26
+ }
27
+
28
+ function makeKey(sessionId: string, fileId: string): string {
29
+ return `${sessionId}:${fileId}`;
30
+ }
31
+
32
+ export const SessionBlobCache = {
33
+ async get(sessionId: string, fileId: string): Promise<Blob | null> {
34
+ const db = await openDB();
35
+ return new Promise((resolve, reject) => {
36
+ const tx = db.transaction(STORE_NAME, 'readonly');
37
+ const store = tx.objectStore(STORE_NAME);
38
+ const req = store.get(makeKey(sessionId, fileId));
39
+ req.onsuccess = () => resolve(req.result || null);
40
+ req.onerror = () => reject(req.error);
41
+ });
42
+ },
43
+
44
+ async put(sessionId: string, fileId: string, blob: Blob): Promise<void> {
45
+ const db = await openDB();
46
+ return new Promise((resolve, reject) => {
47
+ const tx = db.transaction(STORE_NAME, 'readwrite');
48
+ const store = tx.objectStore(STORE_NAME);
49
+ store.put(blob, makeKey(sessionId, fileId));
50
+ tx.oncomplete = () => resolve();
51
+ tx.onerror = () => reject(tx.error);
52
+ });
53
+ },
54
+
55
+ async delete(sessionId: string, fileId: string): Promise<void> {
56
+ const db = await openDB();
57
+ return new Promise((resolve, reject) => {
58
+ const tx = db.transaction(STORE_NAME, 'readwrite');
59
+ const store = tx.objectStore(STORE_NAME);
60
+ store.delete(makeKey(sessionId, fileId));
61
+ tx.oncomplete = () => resolve();
62
+ tx.onerror = () => reject(tx.error);
63
+ });
64
+ },
65
+
66
+ async clearSession(sessionId: string): Promise<void> {
67
+ const db = await openDB();
68
+ return new Promise((resolve, reject) => {
69
+ const tx = db.transaction(STORE_NAME, 'readwrite');
70
+ const store = tx.objectStore(STORE_NAME);
71
+ const req = store.openCursor();
72
+ req.onsuccess = () => {
73
+ const cursor = req.result;
74
+ if (cursor) {
75
+ if (typeof cursor.key === 'string' && cursor.key.startsWith(`${sessionId}:`)) {
76
+ cursor.delete();
77
+ }
78
+ cursor.continue();
79
+ }
80
+ };
81
+ tx.oncomplete = () => resolve();
82
+ tx.onerror = () => reject(tx.error);
83
+ });
84
+ },
85
+
86
+ async clearAll(): Promise<void> {
87
+ const db = await openDB();
88
+ return new Promise((resolve, reject) => {
89
+ const tx = db.transaction(STORE_NAME, 'readwrite');
90
+ const store = tx.objectStore(STORE_NAME);
91
+ store.clear();
92
+ tx.oncomplete = () => resolve();
93
+ tx.onerror = () => reject(tx.error);
94
+ });
95
+ },
96
+ };
@@ -0,0 +1,19 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ import { Plugin } from '@nocobase/client';
11
+ import { ChatFilePreviewProvider } from './ChatFilePreviewProvider';
12
+
13
+ export class PluginAIChatFilePreviewClient extends Plugin {
14
+ async load() {
15
+ this.app.use(ChatFilePreviewProvider);
16
+ }
17
+ }
18
+
19
+ export default PluginAIChatFilePreviewClient;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ import { useApp } from '@nocobase/client';
11
+
12
+ export function useTranslation() {
13
+ const app = useApp();
14
+ return {
15
+ t: (key: string) => app.i18n.t(key, { ns: '@nocobase/plugin-ai-chat-file-preview' }),
16
+ };
17
+ }
package/src/index.ts ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ export * from './server';
11
+ export { default } from './server';
@@ -0,0 +1,10 @@
1
+ {
2
+ "Loading preview...": "Loading preview...",
3
+ "Failed to load file preview": "Failed to load file preview",
4
+ "Failed to download file": "Failed to download file",
5
+ "Download": "Download",
6
+ "Close": "Close",
7
+ "Clear cache": "Clear cache",
8
+ "Cache cleared": "Cache cleared",
9
+ "This file type cannot be previewed. Click Download to save the file.": "This file type cannot be previewed. Click Download to save the file."
10
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "Loading preview...": "Đang tải xem trước...",
3
+ "Failed to load file preview": "Không thể tải xem trước file",
4
+ "Failed to download file": "Không thể tải file",
5
+ "Download": "Tải xuống",
6
+ "Close": "Đóng",
7
+ "Clear cache": "Xóa bộ nhớ đệm",
8
+ "Cache cleared": "Đã xóa bộ nhớ đệm",
9
+ "This file type cannot be previewed. Click Download to save the file.": "Loại file này không thể xem trước. Nhấn Tải xuống để lưu file."
10
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "Loading preview...": "加载预览中...",
3
+ "Failed to load file preview": "加载文件预览失败",
4
+ "Failed to download file": "下载文件失败",
5
+ "Download": "下载",
6
+ "Close": "关闭",
7
+ "Clear cache": "清除缓存",
8
+ "Cache cleared": "缓存已清除",
9
+ "This file type cannot be previewed. Click Download to save the file.": "此文件类型无法预览。点击下载保存文件。"
10
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ export { default } from './plugin';
@@ -0,0 +1,18 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ import { Plugin } from '@nocobase/server';
11
+
12
+ export class PluginAIChatFilePreviewServer extends Plugin {
13
+ async load() {
14
+ // Client-only plugin. No server-side logic needed.
15
+ }
16
+ }
17
+
18
+ export default PluginAIChatFilePreviewServer;