opencode-mem 2.14.3 → 2.16.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 +33 -0
- package/dist/services/api-handlers.d.ts.map +1 -1
- package/dist/services/api-handlers.js +67 -28
- package/dist/services/client.d.ts.map +1 -1
- package/dist/services/client.js +27 -1
- package/dist/services/embedding.d.ts.map +1 -1
- package/dist/services/embedding.js +17 -0
- package/dist/services/sqlite/connection-manager.d.ts +1 -1
- package/dist/services/sqlite/connection-manager.d.ts.map +1 -1
- package/dist/services/sqlite/sqlite-bootstrap.d.ts +3 -1
- package/dist/services/sqlite/sqlite-bootstrap.d.ts.map +1 -1
- package/dist/services/sqlite/sqlite-bootstrap.js +90 -4
- package/dist/services/sqlite/vector-search.d.ts +1 -1
- package/dist/services/sqlite/vector-search.d.ts.map +1 -1
- package/dist/services/web-server.d.ts.map +1 -1
- package/dist/services/web-server.js +62 -1
- package/dist/web/app.js +6 -3
- package/dist/web/i18n.js +150 -0
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -141,6 +141,39 @@ Supported providers: any provider listed by `opencode providers list` (e.g. `ant
|
|
|
141
141
|
|
|
142
142
|
Full documentation available in this README.
|
|
143
143
|
|
|
144
|
+
## Public Subpath Exports
|
|
145
|
+
|
|
146
|
+
In addition to the main plugin entry, `opencode-mem` exposes one stable subpath
|
|
147
|
+
that other opencode plugins can import directly. This avoids having to
|
|
148
|
+
reverse-engineer container-tag conventions when writing third-party tools that
|
|
149
|
+
read or write into the same memory store.
|
|
150
|
+
|
|
151
|
+
### `opencode-mem/tags`
|
|
152
|
+
|
|
153
|
+
Canonical container-tag helpers. The same functions opencode-mem itself uses
|
|
154
|
+
to scope auto-captured memories.
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
import { getProjectTagInfo, getUserTagInfo, getTags } from "opencode-mem/tags";
|
|
158
|
+
|
|
159
|
+
// Canonical project tag derived from cwd (git remote URL if present, else
|
|
160
|
+
// the project root path). Format: `opencode_project_<sha16>`.
|
|
161
|
+
const projectTag = getProjectTagInfo(process.cwd()).tag;
|
|
162
|
+
|
|
163
|
+
// Canonical user tag derived from `git config user.email`.
|
|
164
|
+
// Format: `opencode_user_<sha16>`.
|
|
165
|
+
const userTag = getUserTagInfo().tag;
|
|
166
|
+
|
|
167
|
+
// Both at once.
|
|
168
|
+
const { user, project } = getTags(process.cwd());
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Tags produced by these helpers match what auto-capture writes, so third-party
|
|
172
|
+
plugins that call `POST /api/memories` will land in the same shards the rest
|
|
173
|
+
of the system already understands. Hand-rolled tags whose substring isn't
|
|
174
|
+
`_project_` or `_user_` end up in shadow shards that `/api/stats` and
|
|
175
|
+
`/api/memories` silently filter out — using these helpers avoids that pitfall.
|
|
176
|
+
|
|
144
177
|
## Development & Contribution
|
|
145
178
|
|
|
146
179
|
Build and test locally:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api-handlers.d.ts","sourceRoot":"","sources":["../../src/services/api-handlers.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAGpD,UAAU,WAAW,CAAC,CAAC,GAAG,GAAG;IAC3B,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,UAAU,MAAM;IACd,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EA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Parse(jsonString) {
|
|
|
31
31
|
return undefined;
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
+
function toBlob(vector) {
|
|
35
|
+
return vector ? new Uint8Array(vector.buffer) : null;
|
|
36
|
+
}
|
|
34
37
|
function extractScopeFromTag(tag) {
|
|
35
38
|
const parts = tag.split("_");
|
|
36
39
|
if (parts.length >= 3) {
|
|
@@ -105,11 +108,20 @@ export async function handleListMemories(tag, page = 1, pageSize = 20, includePr
|
|
|
105
108
|
}
|
|
106
109
|
}
|
|
107
110
|
else {
|
|
108
|
-
|
|
109
|
-
|
|
111
|
+
// Iterate both project- and user-scoped shards. Previously this only
|
|
112
|
+
// walked project shards, which silently hid user-scope memories from the
|
|
113
|
+
// listing endpoint (Web UI navigation, /api/memories without a tag
|
|
114
|
+
// filter, …). User-scope memories still showed up in /api/search and
|
|
115
|
+
// /api/stats `byType`, but were invisible in /api/stats `byScope` and
|
|
116
|
+
// unbrowseable in the UI — a confusing UX gap. The filter keeps the
|
|
117
|
+
// defense-in-depth check on container_tag, just widens it to both
|
|
118
|
+
// canonical scope markers.
|
|
119
|
+
const projectShards = shardManager.getAllShards("project", "");
|
|
120
|
+
const userShards = shardManager.getAllShards("user", "");
|
|
121
|
+
for (const shard of [...projectShards, ...userShards]) {
|
|
110
122
|
const db = connectionManager.getConnection(shard.dbPath);
|
|
111
123
|
const memories = vectorSearch.getAllMemories(db);
|
|
112
|
-
allMemories.push(...memories.filter((m) => m.container_tag?.includes(
|
|
124
|
+
allMemories.push(...memories.filter((m) => m.container_tag?.includes("_project_") || m.container_tag?.includes("_user_")));
|
|
113
125
|
}
|
|
114
126
|
}
|
|
115
127
|
const memoriesWithType = allMemories.map((r) => {
|
|
@@ -262,7 +274,30 @@ export async function handleAddMemory(data) {
|
|
|
262
274
|
metadata: JSON.stringify({ source: "api" }),
|
|
263
275
|
};
|
|
264
276
|
const db = connectionManager.getConnection(shard.dbPath);
|
|
265
|
-
|
|
277
|
+
// Use transaction for atomic SQLite insert
|
|
278
|
+
const insertMemory = db.transaction(() => {
|
|
279
|
+
const insertStmt = db.prepare(`
|
|
280
|
+
INSERT INTO memories (
|
|
281
|
+
id, content, vector, tags_vector, container_tag, tags, type, created_at, updated_at,
|
|
282
|
+
metadata, display_name, user_name, user_email, project_path, project_name, git_repo_url
|
|
283
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
284
|
+
`);
|
|
285
|
+
insertStmt.run(record.id, record.content, toBlob(record.vector), toBlob(record.tagsVector), record.containerTag, record.tags || null, record.type || null, record.createdAt, record.updatedAt, record.metadata || null, record.displayName || null, record.userName || null, record.userEmail || null, record.projectPath || null, record.projectName || null, record.gitRepoUrl || null);
|
|
286
|
+
});
|
|
287
|
+
insertMemory();
|
|
288
|
+
// Vector index update (outside transaction — vector backend is async)
|
|
289
|
+
try {
|
|
290
|
+
const backend = await vectorSearch.getBackend();
|
|
291
|
+
await backend.insert({ id: record.id, vector: record.vector, shard, kind: "content" });
|
|
292
|
+
if (record.tagsVector) {
|
|
293
|
+
await backend.insert({ id: record.id, vector: record.tagsVector, shard, kind: "tags" });
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
catch (error) {
|
|
297
|
+
// Rollback SQLite insert on vector backend failure
|
|
298
|
+
db.prepare(`DELETE FROM memories WHERE id = ?`).run(record.id);
|
|
299
|
+
throw error;
|
|
300
|
+
}
|
|
266
301
|
shardManager.incrementVectorCount(shard.id);
|
|
267
302
|
return { success: true, data: { id } };
|
|
268
303
|
}
|
|
@@ -323,6 +358,7 @@ export async function handleUpdateMemory(id, data) {
|
|
|
323
358
|
if (!id)
|
|
324
359
|
return { success: false, error: "id is required" };
|
|
325
360
|
await embeddingService.warmup();
|
|
361
|
+
// Find the existing memory first (read-only — no data modified yet)
|
|
326
362
|
const projectShards = shardManager.getAllShards("project", "");
|
|
327
363
|
let foundShard = null, existingMemory = null;
|
|
328
364
|
for (const shard of projectShards) {
|
|
@@ -336,36 +372,39 @@ export async function handleUpdateMemory(id, data) {
|
|
|
336
372
|
}
|
|
337
373
|
if (!foundShard || !existingMemory)
|
|
338
374
|
return { success: false, error: "Memory not found" };
|
|
339
|
-
|
|
340
|
-
await vectorSearch.deleteVector(db, id, foundShard);
|
|
341
|
-
shardManager.decrementVectorCount(foundShard.id);
|
|
375
|
+
// STEP 1: Generate new embeddings FIRST (safe — no data deleted yet)
|
|
342
376
|
const newContent = data.content || existingMemory.content;
|
|
343
|
-
const tags = data.tags ||
|
|
377
|
+
const tags = data.tags ||
|
|
378
|
+
(existingMemory.tags ? existingMemory.tags.split(",").map((t) => t.trim()) : []);
|
|
344
379
|
const vector = await embeddingService.embedWithTimeout(newContent);
|
|
345
380
|
let tagsVector = undefined;
|
|
346
381
|
if (tags.length > 0) {
|
|
347
382
|
tagsVector = await embeddingService.embedWithTimeout(tags.join(", "));
|
|
348
383
|
}
|
|
349
|
-
const
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
await
|
|
368
|
-
|
|
384
|
+
const db = connectionManager.getConnection(foundShard.dbPath);
|
|
385
|
+
// STEP 2: Wrap SQLite delete + insert in a transaction
|
|
386
|
+
const updateTransaction = db.transaction(() => {
|
|
387
|
+
// Delete old record
|
|
388
|
+
db.prepare(`DELETE FROM memories WHERE id = ?`).run(id);
|
|
389
|
+
// Insert updated record
|
|
390
|
+
const insertStmt = db.prepare(`
|
|
391
|
+
INSERT INTO memories (
|
|
392
|
+
id, content, vector, tags_vector, container_tag, tags, type, created_at, updated_at,
|
|
393
|
+
metadata, display_name, user_name, user_email, project_path, project_name, git_repo_url
|
|
394
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
395
|
+
`);
|
|
396
|
+
insertStmt.run(id, newContent, toBlob(vector), toBlob(tagsVector), existingMemory.container_tag, tags.length > 0 ? tags.join(",") : null, data.type || existingMemory.type, existingMemory.created_at, Date.now(), existingMemory.metadata, existingMemory.display_name, existingMemory.user_name, existingMemory.user_email, existingMemory.project_path, existingMemory.project_name, existingMemory.git_repo_url);
|
|
397
|
+
});
|
|
398
|
+
// Execute the SQLite transaction atomically
|
|
399
|
+
updateTransaction();
|
|
400
|
+
// STEP 3: Update vector index (outside transaction — vector backend is async/in-memory)
|
|
401
|
+
const backend = await vectorSearch.getBackend();
|
|
402
|
+
await backend.delete({ id, shard: foundShard, kind: "content" });
|
|
403
|
+
await backend.delete({ id, shard: foundShard, kind: "tags" });
|
|
404
|
+
await backend.insert({ id, vector, shard: foundShard, kind: "content" });
|
|
405
|
+
if (tagsVector) {
|
|
406
|
+
await backend.insert({ id, vector: tagsVector, shard: foundShard, kind: "tags" });
|
|
407
|
+
}
|
|
369
408
|
return { success: true };
|
|
370
409
|
}
|
|
371
410
|
catch (error) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/services/client.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAGpD,MAAM,MAAM,WAAW,GAAG,SAAS,GAAG,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/services/client.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAGpD,MAAM,MAAM,WAAW,GAAG,SAAS,GAAG,cAAc,CAAC;AAyDrD,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,WAAW,CAA8B;IACjD,OAAO,CAAC,aAAa,CAAkB;;YAIzB,UAAU;IAiBlB,MAAM,CAAC,gBAAgB,CAAC,EAAE,CAAC,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AM,EAAE,KAAK,GAAE,MAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4C5F;AAED,eAAO,MAAM,YAAY,mBAA0B,CAAC"}
|
package/dist/services/client.js
CHANGED
|
@@ -30,6 +30,9 @@ function safeJSONParse(jsonString) {
|
|
|
30
30
|
return undefined;
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
|
+
function toBlob(vector) {
|
|
34
|
+
return vector ? new Uint8Array(vector.buffer) : null;
|
|
35
|
+
}
|
|
33
36
|
function extractScopeFromContainerTag(containerTag) {
|
|
34
37
|
const parts = containerTag.split("_");
|
|
35
38
|
if (parts.length >= 3) {
|
|
@@ -134,7 +137,30 @@ export class LocalMemoryClient {
|
|
|
134
137
|
metadata: Object.keys(dynamicMetadata).length > 0 ? JSON.stringify(dynamicMetadata) : undefined,
|
|
135
138
|
};
|
|
136
139
|
const db = connectionManager.getConnection(shard.dbPath);
|
|
137
|
-
|
|
140
|
+
// Use transaction for atomic SQLite insert
|
|
141
|
+
const insertMemory = db.transaction(() => {
|
|
142
|
+
const insertStmt = db.prepare(`
|
|
143
|
+
INSERT INTO memories (
|
|
144
|
+
id, content, vector, tags_vector, container_tag, tags, type, created_at, updated_at,
|
|
145
|
+
metadata, display_name, user_name, user_email, project_path, project_name, git_repo_url
|
|
146
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
147
|
+
`);
|
|
148
|
+
insertStmt.run(record.id, record.content, toBlob(record.vector), toBlob(record.tagsVector), record.containerTag, record.tags || null, record.type || null, record.createdAt, record.updatedAt, record.metadata || null, record.displayName || null, record.userName || null, record.userEmail || null, record.projectPath || null, record.projectName || null, record.gitRepoUrl || null);
|
|
149
|
+
});
|
|
150
|
+
insertMemory();
|
|
151
|
+
// Vector index update (outside transaction — vector backend is async/in-memory)
|
|
152
|
+
try {
|
|
153
|
+
const backend = await vectorSearch.getBackend();
|
|
154
|
+
await backend.insert({ id: record.id, vector: record.vector, shard, kind: "content" });
|
|
155
|
+
if (record.tagsVector) {
|
|
156
|
+
await backend.insert({ id: record.id, vector: record.tagsVector, shard, kind: "tags" });
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
// Rollback SQLite insert on vector backend failure
|
|
161
|
+
db.prepare(`DELETE FROM memories WHERE id = ?`).run(record.id);
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
138
164
|
shardManager.incrementVectorCount(shard.id);
|
|
139
165
|
return { success: true, id };
|
|
140
166
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"embedding.d.ts","sourceRoot":"","sources":["../../src/services/embedding.ts"],"names":[],"mappings":"AA6CA,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,IAAI,CAAa;IACzB,OAAO,CAAC,WAAW,CAA8B;IAC1C,UAAU,EAAE,OAAO,CAAS;IACnC,OAAO,CAAC,KAAK,CAAwC;IACrD,OAAO,CAAC,eAAe,CAAuB;IAE9C,MAAM,CAAC,WAAW,IAAI,gBAAgB;IAOhC,MAAM,CAAC,gBAAgB,CAAC,EAAE,CAAC,QAAQ,EAAE,GAAG,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;YAOzD,eAAe;
|
|
1
|
+
{"version":3,"file":"embedding.d.ts","sourceRoot":"","sources":["../../src/services/embedding.ts"],"names":[],"mappings":"AA6CA,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,IAAI,CAAa;IACzB,OAAO,CAAC,WAAW,CAA8B;IAC1C,UAAU,EAAE,OAAO,CAAS;IACnC,OAAO,CAAC,KAAK,CAAwC;IACrD,OAAO,CAAC,eAAe,CAAuB;IAE9C,MAAM,CAAC,WAAW,IAAI,gBAAgB;IAOhC,MAAM,CAAC,gBAAgB,CAAC,EAAE,CAAC,QAAQ,EAAE,GAAG,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;YAOzD,eAAe;IA2CvB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAmD1C,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAI3D,UAAU,IAAI,IAAI;CAGnB;AAED,eAAO,MAAM,gBAAgB,kBAAiC,CAAC"}
|
|
@@ -57,9 +57,26 @@ export class EmbeddingService {
|
|
|
57
57
|
async initializeModel(progressCallback) {
|
|
58
58
|
try {
|
|
59
59
|
if (CONFIG.embeddingApiUrl && CONFIG.embeddingApiKey) {
|
|
60
|
+
// Send a probe request to verify the API endpoint is actually reachable
|
|
61
|
+
// Uses a minimal embedding of "ping" to test the full request pipeline
|
|
62
|
+
const probeResponse = await withTimeout(fetch(`${CONFIG.embeddingApiUrl}/embeddings`, {
|
|
63
|
+
method: "POST",
|
|
64
|
+
headers: {
|
|
65
|
+
"Content-Type": "application/json",
|
|
66
|
+
Authorization: `Bearer ${CONFIG.embeddingApiKey}`,
|
|
67
|
+
},
|
|
68
|
+
body: JSON.stringify({
|
|
69
|
+
input: "ping",
|
|
70
|
+
model: CONFIG.embeddingModel,
|
|
71
|
+
}),
|
|
72
|
+
}), TIMEOUT_MS);
|
|
73
|
+
if (!probeResponse.ok) {
|
|
74
|
+
throw new Error(`Embedding API health check failed: ${probeResponse.status} ${probeResponse.statusText}`);
|
|
75
|
+
}
|
|
60
76
|
this.isWarmedUp = true;
|
|
61
77
|
return;
|
|
62
78
|
}
|
|
79
|
+
// Local model path
|
|
63
80
|
const { pipeline } = await ensureTransformersLoaded();
|
|
64
81
|
this.pipe = await pipeline("feature-extraction", CONFIG.embeddingModel, {
|
|
65
82
|
progress_callback: progressCallback,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection-manager.d.ts","sourceRoot":"","sources":["../../../src/services/sqlite/connection-manager.ts"],"names":[],"mappings":"AAMA,QAAA,MAAM,QAAQ,
|
|
1
|
+
{"version":3,"file":"connection-manager.d.ts","sourceRoot":"","sources":["../../../src/services/sqlite/connection-manager.ts"],"names":[],"mappings":"AAMA,QAAA,MAAM,QAAQ,uDAAgB,CAAC;AAE/B,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,WAAW,CAAqD;IAExE,OAAO,CAAC,YAAY;IAWpB,OAAO,CAAC,aAAa;IAarB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,QAAQ,CAAC,SAAS;IAiBxD,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IASrC,QAAQ,IAAI,IAAI;IAYhB,aAAa,IAAI,IAAI;CAStB;AAED,eAAO,MAAM,iBAAiB,mBAA0B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sqlite-bootstrap.d.ts","sourceRoot":"","sources":["../../../src/services/sqlite/sqlite-bootstrap.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"sqlite-bootstrap.d.ts","sourceRoot":"","sources":["../../../src/services/sqlite/sqlite-bootstrap.ts"],"names":[],"mappings":"AAmBA,KAAK,YAAY,GAAG,KAAK,QAAQ,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,KAAK,OAAO,CAAC;AAM1E,wBAAgB,WAAW,IAAI,YAAY,CAuF1C"}
|
|
@@ -1,8 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQLite binding bootstrap — works under Bun and Node.
|
|
3
|
+
*
|
|
4
|
+
* Resolution order:
|
|
5
|
+
* 1. Bun runtime → `bun:sqlite` (built-in, fastest, zero-install)
|
|
6
|
+
* 2. Node runtime → `node:sqlite` `DatabaseSync` (built-in, Node 22.5+ experimental,
|
|
7
|
+
* stable in Node 24+)
|
|
8
|
+
* 3. Fallback → `better-sqlite3` (peer dependency, full native binary)
|
|
9
|
+
*
|
|
10
|
+
* Required because opencode 1.15.x loads plugins under Node, not Bun — `bun:sqlite`
|
|
11
|
+
* is a Bun-only built-in and Node's ESM loader rejects the `bun:` URL scheme.
|
|
12
|
+
*
|
|
13
|
+
* The detection runs once at first call; the resolved Database class is cached.
|
|
14
|
+
*/
|
|
15
|
+
import { createRequire } from "node:module";
|
|
1
16
|
let Database;
|
|
17
|
+
const isBun = typeof globalThis.Bun !== "undefined";
|
|
2
18
|
export function getDatabase() {
|
|
3
|
-
if (
|
|
4
|
-
|
|
5
|
-
|
|
19
|
+
if (Database)
|
|
20
|
+
return Database;
|
|
21
|
+
const req = createRequire(import.meta.url);
|
|
22
|
+
if (isBun) {
|
|
23
|
+
Database = req("bun:sqlite").Database;
|
|
24
|
+
return Database;
|
|
25
|
+
}
|
|
26
|
+
// Node runtime — try built-in `node:sqlite` first. It exposes `DatabaseSync`
|
|
27
|
+
// with the synchronous prepare/all/get/close API surface that matches
|
|
28
|
+
// bun:sqlite. One gap: bun:sqlite (and better-sqlite3) expose `db.run(sql)`
|
|
29
|
+
// for executing a single SQL statement without bindings — used throughout
|
|
30
|
+
// this project for PRAGMA and CREATE INDEX setup. `node:sqlite`'s
|
|
31
|
+
// DatabaseSync uses `db.exec(sql)` for that surface, so we subclass to
|
|
32
|
+
// alias `db.run(sql)` onto `db.exec(sql)` (param-bound `db.run(sql, ...)`
|
|
33
|
+
// is preserved for any future callers, falling back to a prepared statement).
|
|
34
|
+
try {
|
|
35
|
+
const DatabaseSync = req("node:sqlite")
|
|
36
|
+
.DatabaseSync;
|
|
37
|
+
class DatabaseSyncCompat extends DatabaseSync {
|
|
38
|
+
run(sql, ...params) {
|
|
39
|
+
if (params.length === 0) {
|
|
40
|
+
return this.exec(sql);
|
|
41
|
+
}
|
|
42
|
+
return this.prepare(sql).run(...params);
|
|
43
|
+
}
|
|
44
|
+
// bun:sqlite and better-sqlite3 expose `db.transaction(fn)` that returns
|
|
45
|
+
// a callable wrapping `fn` in BEGIN/COMMIT (auto-ROLLBACK on throw).
|
|
46
|
+
// `node:sqlite`'s DatabaseSync has no equivalent. Used by
|
|
47
|
+
// `api-handlers.handleAddMemory` and `services/client.addMemory`, so
|
|
48
|
+
// POST /api/memories and any auto-capture path crash without it.
|
|
49
|
+
//
|
|
50
|
+
// Single-mode semantics only (BEGIN); the `.deferred` / `.immediate` /
|
|
51
|
+
// `.exclusive` variants from better-sqlite3 are not exercised by this
|
|
52
|
+
// codebase.
|
|
53
|
+
transaction(fn) {
|
|
54
|
+
const self = this;
|
|
55
|
+
const wrapped = function (...args) {
|
|
56
|
+
self.exec("BEGIN");
|
|
57
|
+
try {
|
|
58
|
+
const result = fn.apply(this, args);
|
|
59
|
+
self.exec("COMMIT");
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
try {
|
|
64
|
+
self.exec("ROLLBACK");
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
/* rollback failures after partial state are best-effort */
|
|
68
|
+
}
|
|
69
|
+
throw err;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
return wrapped;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
Database = DatabaseSyncCompat;
|
|
76
|
+
return Database;
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
// node:sqlite isn't available (Node < 22.5, or experimental flag not set
|
|
80
|
+
// in some embedded runtimes). Fall back to better-sqlite3 — wire-compatible
|
|
81
|
+
// API, requires a native postinstall but ships prebuilt binaries for
|
|
82
|
+
// common platforms.
|
|
83
|
+
try {
|
|
84
|
+
const betterSqlite = req("better-sqlite3");
|
|
85
|
+
Database = betterSqlite;
|
|
86
|
+
return Database;
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
throw new Error("opencode-mem: no SQLite binding available. Install better-sqlite3, " +
|
|
90
|
+
"or run on Node ≥22.5 with `--experimental-sqlite`, or use Bun. " +
|
|
91
|
+
`Underlying error: ${error instanceof Error ? error.message : String(error)}`);
|
|
92
|
+
}
|
|
6
93
|
}
|
|
7
|
-
return Database;
|
|
8
94
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { MemoryRecord, SearchResult, ShardInfo } from "./types.js";
|
|
2
2
|
import type { VectorBackend } from "../vector-backends/types.js";
|
|
3
|
-
declare const Database:
|
|
3
|
+
declare const Database: new (filename?: string, options?: unknown) => unknown;
|
|
4
4
|
type DatabaseType = typeof Database.prototype;
|
|
5
5
|
export declare class VectorSearch {
|
|
6
6
|
private readonly backendPromise;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vector-search.d.ts","sourceRoot":"","sources":["../../../src/services/sqlite/vector-search.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAGxE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAEjE,QAAA,MAAM,QAAQ,
|
|
1
|
+
{"version":3,"file":"vector-search.d.ts","sourceRoot":"","sources":["../../../src/services/sqlite/vector-search.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAGxE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAEjE,QAAA,MAAM,QAAQ,uDAAgB,CAAC;AAC/B,KAAK,YAAY,GAAG,OAAO,QAAQ,CAAC,SAAS,CAAC;AAM9C,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAyB;IACxD,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAgB;gBAEpC,OAAO,CAAC,EAAE,aAAa,EAAE,eAAe,GAAE,aAAsC;YAO9E,UAAU;IAIlB,YAAY,CAAC,EAAE,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,KAAK,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;IAyCtF,aAAa,CACjB,KAAK,EAAE,SAAS,EAChB,WAAW,EAAE,YAAY,EACzB,YAAY,EAAE,MAAM,EACpB,KAAK,EAAE,MAAM,EACb,SAAS,CAA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oB,CACxB,EAAE,EAAE,YAAY,EAChB,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,IAAI,CAAC;IAgBV,kBAAkB,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;CAI1D;AAED,eAAO,MAAM,YAAY,cAAqB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"web-server.d.ts","sourceRoot":"","sources":["../../src/services/web-server.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"web-server.d.ts","sourceRoot":"","sources":["../../src/services/web-server.ts"],"names":[],"mappings":"AA2HA,UAAU,eAAe;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,qBAAa,SAAS;IACpB,OAAO,CAAC,MAAM,CAAqC;IACnD,OAAO,CAAC,MAAM,CAAkB;IAChC,OAAO,CAAC,OAAO,CAAkB;IACjC,OAAO,CAAC,YAAY,CAA8B;IAClD,OAAO,CAAC,mBAAmB,CAA+B;IAC1D,OAAO,CAAC,kBAAkB,CAAsC;gBAEpD,MAAM,EAAE,eAAe;IAInC,qBAAqB,CAAC,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI;IAIpD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;YASd,MAAM;IAgCpB,OAAO,CAAC,oBAAoB;IAe5B,OAAO,CAAC,mBAAmB;YAOb,eAAe;IA+BvB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAY3B,SAAS,IAAI,OAAO;IAIpB,aAAa,IAAI,OAAO;IAIxB,MAAM,IAAI,MAAM;IAIV,oBAAoB,IAAI,OAAO,CAAC,OAAO,CAAC;YAchC,aAAa;IAuN3B,OAAO,CAAC,eAAe;IA4BvB,OAAO,CAAC,YAAY;CAWrB;AAED,wBAAsB,cAAc,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,SAAS,CAAC,CAIhF"}
|
|
@@ -1,8 +1,69 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
|
+
import { createServer } from "node:http";
|
|
3
|
+
import { Readable } from "node:stream";
|
|
2
4
|
import { join, dirname } from "node:path";
|
|
3
5
|
import { fileURLToPath } from "node:url";
|
|
4
6
|
import { log } from "./logger.js";
|
|
5
7
|
import { handleListTags, handleListMemories, handleAddMemory, handleDeleteMemory, handleBulkDelete, handleUpdateMemory, handleSearch, handleStats, handlePinMemory, handleUnpinMemory, handleRunCleanup, handleRunDeduplication, handleDetectMigration, handleRunMigration, handleDetectTagMigration, handleRunTagMigrationBatch, handleGetTagMigrationProgress, handleDeletePrompt, handleBulkDeletePrompts, handleGetUserProfile, handleGetProfileChangelog, handleGetProfileSnapshot, handleRefreshProfile, } from "./api-handlers.js";
|
|
8
|
+
const isBun = typeof globalThis.Bun !== "undefined";
|
|
9
|
+
function serveFetch(opts) {
|
|
10
|
+
if (isBun) {
|
|
11
|
+
const bunHandle = globalThis.Bun.serve({
|
|
12
|
+
port: opts.port,
|
|
13
|
+
hostname: opts.hostname,
|
|
14
|
+
fetch: opts.fetch,
|
|
15
|
+
});
|
|
16
|
+
return { stop: () => bunHandle.stop() };
|
|
17
|
+
}
|
|
18
|
+
// Node path: wrap node:http around the fetch-style handler. The adapter
|
|
19
|
+
// converts IncomingMessage → Web Request and Web Response → ServerResponse.
|
|
20
|
+
// Bodies stream both directions via the WHATWG Streams ↔ Node Streams
|
|
21
|
+
// helpers that ship with Node 18+.
|
|
22
|
+
const server = createServer(async (req, res) => {
|
|
23
|
+
try {
|
|
24
|
+
const url = `http://${opts.hostname}:${opts.port}${req.url ?? "/"}`;
|
|
25
|
+
const method = req.method ?? "GET";
|
|
26
|
+
const hasBody = method !== "GET" && method !== "HEAD";
|
|
27
|
+
const webReq = new Request(url, {
|
|
28
|
+
method,
|
|
29
|
+
headers: req.headers,
|
|
30
|
+
body: hasBody ? Readable.toWeb(req) : undefined,
|
|
31
|
+
// `duplex: "half"` is required by Node fetch when sending a body
|
|
32
|
+
// stream. Cast keeps TS happy on older lib.dom.d.ts revisions.
|
|
33
|
+
...(hasBody ? { duplex: "half" } : {}),
|
|
34
|
+
});
|
|
35
|
+
const webRes = await opts.fetch(webReq);
|
|
36
|
+
res.statusCode = webRes.status;
|
|
37
|
+
webRes.headers.forEach((value, name) => res.setHeader(name, value));
|
|
38
|
+
if (webRes.body) {
|
|
39
|
+
Readable.fromWeb(webRes.body).pipe(res);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
res.end();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
if (!res.headersSent) {
|
|
47
|
+
res.statusCode = 500;
|
|
48
|
+
res.setHeader("Content-Type", "text/plain");
|
|
49
|
+
}
|
|
50
|
+
res.end(`Internal Server Error: ${error instanceof Error ? error.message : String(error)}`);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
// Surface EADDRINUSE synchronously so callers can detect the
|
|
54
|
+
// already-running-instance case the same way they do under Bun.
|
|
55
|
+
let listenError;
|
|
56
|
+
server.on("error", (err) => {
|
|
57
|
+
if (err.code === "EADDRINUSE") {
|
|
58
|
+
listenError = err;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
server.listen(opts.port, opts.hostname);
|
|
62
|
+
if (listenError) {
|
|
63
|
+
throw listenError;
|
|
64
|
+
}
|
|
65
|
+
return { stop: () => server.close() };
|
|
66
|
+
}
|
|
6
67
|
const __filename = fileURLToPath(import.meta.url);
|
|
7
68
|
const __dirname = dirname(__filename);
|
|
8
69
|
export class WebServer {
|
|
@@ -30,7 +91,7 @@ export class WebServer {
|
|
|
30
91
|
return;
|
|
31
92
|
}
|
|
32
93
|
try {
|
|
33
|
-
this.server =
|
|
94
|
+
this.server = serveFetch({
|
|
34
95
|
port: this.config.port,
|
|
35
96
|
hostname: this.config.host,
|
|
36
97
|
fetch: this.handleRequest.bind(this),
|
package/dist/web/app.js
CHANGED
|
@@ -645,7 +645,8 @@ function showRefreshIndicator(show) {
|
|
|
645
645
|
|
|
646
646
|
function formatDate(isoString) {
|
|
647
647
|
const date = new Date(isoString);
|
|
648
|
-
const
|
|
648
|
+
const lang = getLanguage();
|
|
649
|
+
const locale = lang === "zh" ? "zh-CN" : lang === "ar" ? "ar-SA" : "en-US";
|
|
649
650
|
return date.toLocaleString(locale, {
|
|
650
651
|
year: "numeric",
|
|
651
652
|
month: "short",
|
|
@@ -1141,10 +1142,12 @@ document.addEventListener("DOMContentLoaded", async () => {
|
|
|
1141
1142
|
});
|
|
1142
1143
|
|
|
1143
1144
|
document.getElementById("lang-toggle").addEventListener("click", () => {
|
|
1144
|
-
const
|
|
1145
|
+
const langCycle = ["en", "zh", "ar"];
|
|
1146
|
+
const currentLang = getLanguage();
|
|
1147
|
+
const currentIndex = langCycle.indexOf(currentLang);
|
|
1148
|
+
const newLang = langCycle[(currentIndex + 1) % langCycle.length];
|
|
1145
1149
|
setLanguage(newLang);
|
|
1146
1150
|
document.getElementById("lang-toggle").textContent = newLang.toUpperCase();
|
|
1147
|
-
// Re-render dynamic content
|
|
1148
1151
|
loadMemories();
|
|
1149
1152
|
loadStats();
|
|
1150
1153
|
if (state.currentView === "profile") loadUserProfile();
|
package/dist/web/i18n.js
CHANGED
|
@@ -211,6 +211,151 @@ const translations = {
|
|
|
211
211
|
"migration-mismatch-details":
|
|
212
212
|
"模型不匹配:配置使用 {configDimensions}D ({configModel}),但{shardInfo}。",
|
|
213
213
|
},
|
|
214
|
+
ar: {
|
|
215
|
+
title: "┌─ مستكشف ذاكرة OpenCode ─┐",
|
|
216
|
+
"tab-project": "ذكريات المشروع",
|
|
217
|
+
"tab-profile": "ملف المستخدم",
|
|
218
|
+
|
|
219
|
+
"label-tag": "الوسم:",
|
|
220
|
+
"label-type": "النوع:",
|
|
221
|
+
"label-tags": "الوسوم:",
|
|
222
|
+
"label-content": "المحتوى:",
|
|
223
|
+
|
|
224
|
+
"btn-cleanup": "تنظيف",
|
|
225
|
+
"btn-deduplicate": "إزالة التكرار",
|
|
226
|
+
"btn-delete-selected": "حذف المحدد",
|
|
227
|
+
"btn-select-all": "تحديد الصفحة",
|
|
228
|
+
"btn-deselect-all": "إلغاء التحديد",
|
|
229
|
+
"btn-add-memory": "إضافة ذكرى",
|
|
230
|
+
|
|
231
|
+
"section-project": "└─ ذكريات المشروع ({count}) ──",
|
|
232
|
+
"section-profile": "└─ ملف المستخدم ──",
|
|
233
|
+
"section-add": "└─ إضافة ذكرى جديدة ──",
|
|
234
|
+
|
|
235
|
+
"opt-all-tags": "جميع الوسوم",
|
|
236
|
+
"opt-select-tag": "اختر وسمًا",
|
|
237
|
+
"opt-other": "أخرى",
|
|
238
|
+
"opt-feature": "ميزة",
|
|
239
|
+
"opt-bug-fix": "إصلاح خطأ",
|
|
240
|
+
"opt-refactor": "إعادة هيكلة",
|
|
241
|
+
"opt-architecture": "معمارية",
|
|
242
|
+
"opt-rule": "قاعدة",
|
|
243
|
+
"opt-documentation": "توثيق",
|
|
244
|
+
"opt-discussion": "نقاش",
|
|
245
|
+
"opt-analysis": "تحليل",
|
|
246
|
+
"opt-configuration": "إعدادات",
|
|
247
|
+
|
|
248
|
+
"modal-edit-title": "تعديل الذكرى",
|
|
249
|
+
"modal-migration-title": "ترحيل وسوم الذكريات",
|
|
250
|
+
"modal-changelog-title": "سجل إصدارات الملف الشخصي",
|
|
251
|
+
|
|
252
|
+
"btn-cancel": "إلغاء",
|
|
253
|
+
"btn-save": "حفظ التغييرات",
|
|
254
|
+
"btn-start-migration": "بدء الترحيل",
|
|
255
|
+
|
|
256
|
+
"loading-init": "جاري التهيئة...",
|
|
257
|
+
"loading-profile": "جاري تحميل الملف الشخصي...",
|
|
258
|
+
"loading-changelog": "جاري تحميل السجل...",
|
|
259
|
+
|
|
260
|
+
"migration-mismatch": "تم اكتشاف عدم تطابق في أبعاد النموذج!",
|
|
261
|
+
"migration-understand":
|
|
262
|
+
"أفهم أن هذه العملية غير قابلة للتراجع وستؤثر على جميع الذكريات المخزنة",
|
|
263
|
+
|
|
264
|
+
"btn-fresh-start": "بداية جديدة (حذف الكل)",
|
|
265
|
+
"btn-reembed": "إعادة إنشاء المتجهات (مع الاحتفاظ بالبيانات)",
|
|
266
|
+
|
|
267
|
+
"migration-note":
|
|
268
|
+
"يرجى عدم إغلاق المتصفح. سيتم إعادة فهرسة الذكريات باستخدام وسوم تقنية لتحسين دقة البحث.",
|
|
269
|
+
|
|
270
|
+
"placeholder-search": "ابحث في الذكريات...",
|
|
271
|
+
"placeholder-tags": "react, hooks, auth (مفصولة بفواصل)",
|
|
272
|
+
"placeholder-content": "أدخل محتوى الذكرى...",
|
|
273
|
+
|
|
274
|
+
"toast-add-success": "تمت إضافة الذكرى بنجاح",
|
|
275
|
+
"toast-add-error": "المحتوى والوسم مطلوبان",
|
|
276
|
+
"toast-add-failed": "فشلت إضافة الذكرى",
|
|
277
|
+
|
|
278
|
+
"toast-delete-success": "تم حذف الذكرى بنجاح",
|
|
279
|
+
"toast-delete-failed": "فشل حذف الذكرى",
|
|
280
|
+
|
|
281
|
+
"toast-update-success": "تم تحديث الذكرى بنجاح",
|
|
282
|
+
"toast-update-failed": "فشل تحديث الذكرى",
|
|
283
|
+
|
|
284
|
+
"toast-cleanup-success": "اكتملت عملية التنظيف بنجاح",
|
|
285
|
+
"toast-cleanup-failed": "فشلت عملية التنظيف",
|
|
286
|
+
|
|
287
|
+
"toast-dedup-success": "اكتملت إزالة التكرار بنجاح",
|
|
288
|
+
"toast-dedup-failed": "فشلت إزالة التكرار",
|
|
289
|
+
|
|
290
|
+
"toast-bulk-delete-success": "تم حذف الذكريات المحددة بنجاح",
|
|
291
|
+
"toast-bulk-delete-failed": "فشل حذف الذكريات المحددة",
|
|
292
|
+
|
|
293
|
+
"toast-migration-success": "اكتملت عملية الترحيل بنجاح",
|
|
294
|
+
"toast-migration-failed": "فشلت عملية الترحيل",
|
|
295
|
+
|
|
296
|
+
"toast-fresh-start-success": "تمت البداية الجديدة بنجاح",
|
|
297
|
+
"toast-fresh-start-failed": "فشلت البداية الجديدة",
|
|
298
|
+
|
|
299
|
+
"confirm-delete": "هل تريد حذف هذه الذكرى؟",
|
|
300
|
+
"confirm-delete-pair": "هل تريد حذف هذه الذكرى والموجه المرتبط بها؟",
|
|
301
|
+
"confirm-delete-prompt": "هل تريد حذف هذا الموجه والذكرى المرتبطة به؟",
|
|
302
|
+
|
|
303
|
+
"confirm-bulk-delete": "هل تريد حذف {count} من الذكريات المحددة؟",
|
|
304
|
+
|
|
305
|
+
"confirm-cleanup": "سيؤدي هذا إلى حذف جميع الذكريات التي لم تعد ذات صلة. هل تريد المتابعة؟",
|
|
306
|
+
|
|
307
|
+
"confirm-dedup": "سيؤدي هذا إلى دمج الذكريات المتكررة أو المتشابهة جدًا. هل تريد المتابعة؟",
|
|
308
|
+
|
|
309
|
+
"text-selected": "تم تحديد {count}",
|
|
310
|
+
"text-page": "الصفحة {current} من {total}",
|
|
311
|
+
"text-total": "الإجمالي: {count}",
|
|
312
|
+
|
|
313
|
+
"empty-memories": "لم يتم العثور على ذكريات",
|
|
314
|
+
"empty-changelog": "لا يوجد سجل تغييرات",
|
|
315
|
+
|
|
316
|
+
"status-cleanup": "جاري التنظيف...",
|
|
317
|
+
"status-dedup": "جاري إزالة التكرار...",
|
|
318
|
+
"status-migration-init": "جاري تهيئة الترحيل...",
|
|
319
|
+
"status-migration-progress": "جاري الترحيل... {current}/{total}",
|
|
320
|
+
|
|
321
|
+
"profile-version": "الإصدار",
|
|
322
|
+
"profile-prompts": "الموجهات",
|
|
323
|
+
"profile-updated": "آخر تحديث",
|
|
324
|
+
"profile-preferences": "التفضيلات",
|
|
325
|
+
"profile-patterns": "الأنماط",
|
|
326
|
+
"profile-workflows": "سير العمل",
|
|
327
|
+
|
|
328
|
+
"badge-prompt": "موجه المستخدم",
|
|
329
|
+
"badge-memory": "ذكرى",
|
|
330
|
+
"badge-pinned": "مثبتة",
|
|
331
|
+
"badge-linked": "مرتبطة",
|
|
332
|
+
|
|
333
|
+
"date-created": "تاريخ الإنشاء:",
|
|
334
|
+
"date-updated": "تاريخ التحديث:",
|
|
335
|
+
|
|
336
|
+
"empty-preferences": "لم يتم تعلم أي تفضيلات بعد",
|
|
337
|
+
"empty-patterns": "لم يتم اكتشاف أي أنماط بعد",
|
|
338
|
+
"empty-workflows": "لم يتم التعرف على أي سير عمل بعد",
|
|
339
|
+
|
|
340
|
+
"btn-delete-pair": "حذف الزوج",
|
|
341
|
+
"btn-delete": "حذف",
|
|
342
|
+
|
|
343
|
+
"text-generated-above": "تم إنشاء الذكرى أعلاه",
|
|
344
|
+
"text-from-below": "من الموجه أدناه",
|
|
345
|
+
|
|
346
|
+
"btn-refresh": "تحديث",
|
|
347
|
+
|
|
348
|
+
"migration-found-tags": "تم العثور على {count} من الذكريات التي تحتاج إلى وسوم تقنية.",
|
|
349
|
+
|
|
350
|
+
"migration-stopped": "تم إيقاف الترحيل: تم الوصول إلى الحد الأقصى للمحاولات",
|
|
351
|
+
|
|
352
|
+
"migration-shards-mismatch": "{count} من الأجزاء تحتوي على أبعاد مختلفة",
|
|
353
|
+
|
|
354
|
+
"migration-dimension-mismatch": "تم اكتشاف عدم تطابق في الأبعاد",
|
|
355
|
+
|
|
356
|
+
"migration-mismatch-details":
|
|
357
|
+
"عدم تطابق النموذج: يستخدم الإعداد {configDimensions}D ({configModel}) بينما {shardInfo}.",
|
|
358
|
+
},
|
|
214
359
|
};
|
|
215
360
|
|
|
216
361
|
function getLanguage() {
|
|
@@ -219,6 +364,11 @@ function getLanguage() {
|
|
|
219
364
|
|
|
220
365
|
function setLanguage(lang) {
|
|
221
366
|
localStorage.setItem("opencode-mem-lang", lang);
|
|
367
|
+
|
|
368
|
+
document.documentElement.dir = lang === "ar" ? "rtl" : "ltr";
|
|
369
|
+
|
|
370
|
+
document.documentElement.lang = lang;
|
|
371
|
+
|
|
222
372
|
applyLanguage();
|
|
223
373
|
}
|
|
224
374
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-mem",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.16.0",
|
|
4
4
|
"description": "OpenCode plugin that gives coding agents persistent memory using local vector database",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/plugin.js",
|
|
@@ -13,6 +13,10 @@
|
|
|
13
13
|
"./server": {
|
|
14
14
|
"import": "./dist/plugin.js",
|
|
15
15
|
"types": "./dist/index.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"./tags": {
|
|
18
|
+
"import": "./dist/services/tags.js",
|
|
19
|
+
"types": "./dist/services/tags.d.ts"
|
|
16
20
|
}
|
|
17
21
|
},
|
|
18
22
|
"scripts": {
|