dolphin-server-modules 2.9.9 → 2.11.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/dist/ai/dolphin-agent/agent.js +37 -8
- package/dist/ai/dolphin-agent/agent.js.map +1 -1
- package/dist/ai/dolphin-agent/config.js +7 -2
- package/dist/ai/dolphin-agent/config.js.map +1 -1
- package/dist/bin/cli.js +51 -17
- package/dist/bin/cli.js.map +1 -1
- package/package.json +46 -144
- package/TUTORIAL_NEPALI.md +0 -181
- package/scripts/client.js +0 -703
- package/scripts/dolphin-persist.js +0 -211
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DolphinPersist - Offline Cache Plugin for Dolphin Client
|
|
3
|
-
*
|
|
4
|
-
* Optional, zero-dependency persistence layer for DolphinStore.
|
|
5
|
-
* Supports both localStorage (simple) and IndexedDB (large data).
|
|
6
|
-
*
|
|
7
|
-
* Usage:
|
|
8
|
-
* // Auto-detect best storage:
|
|
9
|
-
* const persist = new DolphinPersist();
|
|
10
|
-
* dolphin.store.use(persist);
|
|
11
|
-
*
|
|
12
|
-
* // Force localStorage:
|
|
13
|
-
* const persist = new DolphinPersist({ driver: 'localstorage' });
|
|
14
|
-
*
|
|
15
|
-
* // Force IndexedDB:
|
|
16
|
-
* const persist = new DolphinPersist({ driver: 'indexeddb' });
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
class DolphinPersist {
|
|
20
|
-
/**
|
|
21
|
-
* @param {{ driver?: 'auto'|'localstorage'|'indexeddb', prefix?: string, ttl?: number }} options
|
|
22
|
-
*/
|
|
23
|
-
constructor(options = {}) {
|
|
24
|
-
this.driver = options.driver || 'auto';
|
|
25
|
-
this.prefix = options.prefix || 'dolphin_persist_';
|
|
26
|
-
this.ttl = options.ttl || 0; // 0 = no expiry (milliseconds)
|
|
27
|
-
this._db = null;
|
|
28
|
-
this._ready = false;
|
|
29
|
-
this._readyPromise = this._init();
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
async _init() {
|
|
33
|
-
if (this.driver === 'auto') {
|
|
34
|
-
this.driver = typeof indexedDB !== 'undefined' ? 'indexeddb' : 'localstorage';
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (this.driver === 'indexeddb') {
|
|
38
|
-
try {
|
|
39
|
-
await this._openIndexedDB();
|
|
40
|
-
} catch {
|
|
41
|
-
console.warn('[DolphinPersist] IndexedDB unavailable, falling back to localStorage');
|
|
42
|
-
this.driver = 'localstorage';
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
this._ready = true;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
_openIndexedDB() {
|
|
50
|
-
return new Promise((resolve, reject) => {
|
|
51
|
-
const req = indexedDB.open('dolphin_persist', 1);
|
|
52
|
-
req.onupgradeneeded = (e) => {
|
|
53
|
-
const db = e.target.result;
|
|
54
|
-
if (!db.objectStoreNames.contains('cache')) {
|
|
55
|
-
db.createObjectStore('cache', { keyPath: 'key' });
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
req.onsuccess = (e) => { this._db = e.target.result; resolve(); };
|
|
59
|
-
req.onerror = () => reject(req.error);
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async set(collection, data) {
|
|
64
|
-
await this._readyPromise;
|
|
65
|
-
const entry = {
|
|
66
|
-
data,
|
|
67
|
-
savedAt: Date.now(),
|
|
68
|
-
expiresAt: this.ttl ? Date.now() + this.ttl : null
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
if (this.driver === 'indexeddb') {
|
|
72
|
-
return new Promise((resolve, reject) => {
|
|
73
|
-
const tx = this._db.transaction('cache', 'readwrite');
|
|
74
|
-
tx.objectStore('cache').put({ key: this.prefix + collection, ...entry });
|
|
75
|
-
tx.oncomplete = resolve;
|
|
76
|
-
tx.onerror = reject;
|
|
77
|
-
});
|
|
78
|
-
} else {
|
|
79
|
-
try {
|
|
80
|
-
localStorage.setItem(this.prefix + collection, JSON.stringify(entry));
|
|
81
|
-
} catch (e) {
|
|
82
|
-
console.warn('[DolphinPersist] localStorage write failed:', e.message);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async get(collection) {
|
|
88
|
-
await this._readyPromise;
|
|
89
|
-
let entry = null;
|
|
90
|
-
|
|
91
|
-
if (this.driver === 'indexeddb') {
|
|
92
|
-
entry = await new Promise((resolve, reject) => {
|
|
93
|
-
const tx = this._db.transaction('cache', 'readonly');
|
|
94
|
-
const req = tx.objectStore('cache').get(this.prefix + collection);
|
|
95
|
-
req.onsuccess = () => resolve(req.result || null);
|
|
96
|
-
req.onerror = reject;
|
|
97
|
-
});
|
|
98
|
-
} else {
|
|
99
|
-
try {
|
|
100
|
-
const raw = localStorage.getItem(this.prefix + collection);
|
|
101
|
-
entry = raw ? JSON.parse(raw) : null;
|
|
102
|
-
} catch { entry = null; }
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (!entry) return null;
|
|
106
|
-
|
|
107
|
-
// TTL check
|
|
108
|
-
if (entry.expiresAt && Date.now() > entry.expiresAt) {
|
|
109
|
-
await this.clear(collection);
|
|
110
|
-
return null;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return entry.data;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
async clear(collection) {
|
|
117
|
-
await this._readyPromise;
|
|
118
|
-
if (this.driver === 'indexeddb') {
|
|
119
|
-
return new Promise((resolve) => {
|
|
120
|
-
const tx = this._db.transaction('cache', 'readwrite');
|
|
121
|
-
tx.objectStore('cache').delete(this.prefix + collection);
|
|
122
|
-
tx.oncomplete = resolve;
|
|
123
|
-
});
|
|
124
|
-
} else {
|
|
125
|
-
localStorage.removeItem(this.prefix + collection);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
async clearAll() {
|
|
130
|
-
await this._readyPromise;
|
|
131
|
-
if (this.driver === 'indexeddb') {
|
|
132
|
-
return new Promise((resolve) => {
|
|
133
|
-
const tx = this._db.transaction('cache', 'readwrite');
|
|
134
|
-
tx.objectStore('cache').clear();
|
|
135
|
-
tx.oncomplete = resolve;
|
|
136
|
-
});
|
|
137
|
-
} else {
|
|
138
|
-
const keysToRemove = [];
|
|
139
|
-
for (let i = 0; i < localStorage.length; i++) {
|
|
140
|
-
const k = localStorage.key(i);
|
|
141
|
-
if (k && k.startsWith(this.prefix)) keysToRemove.push(k);
|
|
142
|
-
}
|
|
143
|
-
keysToRemove.forEach(k => localStorage.removeItem(k));
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/** Check what's cached */
|
|
148
|
-
async info(collection) {
|
|
149
|
-
await this._readyPromise;
|
|
150
|
-
const entry = await this.get(collection);
|
|
151
|
-
if (!entry) return null;
|
|
152
|
-
return {
|
|
153
|
-
collection,
|
|
154
|
-
items: entry.length,
|
|
155
|
-
driver: this.driver,
|
|
156
|
-
savedAt: new Date(entry.savedAt)
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// ============================================
|
|
162
|
-
// Extend DolphinStore to support persist plugin
|
|
163
|
-
// ============================================
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Monkey-patches the DolphinStore to support .use(persist) plugin.
|
|
167
|
-
* Call this after DolphinClient is loaded.
|
|
168
|
-
*/
|
|
169
|
-
function enablePersist(storeInstance, persist) {
|
|
170
|
-
const originalFetch = storeInstance._fetchAndSync.bind(storeInstance);
|
|
171
|
-
|
|
172
|
-
storeInstance._fetchAndSync = async function(name) {
|
|
173
|
-
// 1. Load from cache first (instant render)
|
|
174
|
-
const cached = await persist.get(name);
|
|
175
|
-
if (cached && cached.length > 0) {
|
|
176
|
-
storeInstance.data.set(name, cached);
|
|
177
|
-
storeInstance._notify();
|
|
178
|
-
console.log(`[DolphinPersist] Loaded "${name}" from ${persist.driver} cache (${cached.length} items)`);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// 2. Fetch fresh from server
|
|
182
|
-
await originalFetch(name);
|
|
183
|
-
|
|
184
|
-
// 3. Save updated data to cache
|
|
185
|
-
const fresh = storeInstance.data.get(name) || [];
|
|
186
|
-
await persist.set(name, fresh);
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
// Also persist on every realtime update
|
|
190
|
-
const originalUpdate = storeInstance._handleRemoteUpdate.bind(storeInstance);
|
|
191
|
-
storeInstance._handleRemoteUpdate = async function(collection, update) {
|
|
192
|
-
originalUpdate(collection, update);
|
|
193
|
-
const updated = storeInstance.data.get(collection) || [];
|
|
194
|
-
await persist.set(collection, updated);
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
console.log(`[DolphinPersist] Persistence enabled using ${persist.driver}`);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// ============================================
|
|
201
|
-
// Exports
|
|
202
|
-
// ============================================
|
|
203
|
-
|
|
204
|
-
if (typeof window !== 'undefined') {
|
|
205
|
-
window.DolphinPersist = DolphinPersist;
|
|
206
|
-
window.enablePersist = enablePersist;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
if (typeof module !== 'undefined' && module.exports) {
|
|
210
|
-
module.exports = { DolphinPersist, enablePersist };
|
|
211
|
-
}
|