@memrosetta/sync-client 0.1.3 → 0.1.4
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/index.d.ts +1 -0
- package/dist/index.js +30 -3
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -32,6 +32,7 @@ declare class Inbox {
|
|
|
32
32
|
* - `relation_created` INSERT OR IGNORE on the (src, dst, type) PK
|
|
33
33
|
* - `memory_invalidated` UPDATE of `invalidated_at`
|
|
34
34
|
* - `feedback_given` additive UPDATE of `use_count` / `success_count`
|
|
35
|
+
* plus the same salience recompute rule used by core
|
|
35
36
|
* - `memory_tier_set` UPDATE of `tier`
|
|
36
37
|
*/
|
|
37
38
|
interface ApplyResult {
|
package/dist/index.js
CHANGED
|
@@ -48,7 +48,7 @@ var Outbox = class {
|
|
|
48
48
|
addOp(op) {
|
|
49
49
|
const payloadStr = typeof op.payload === "string" ? op.payload : JSON.stringify(op.payload);
|
|
50
50
|
this.db.prepare(
|
|
51
|
-
`INSERT INTO sync_outbox (op_id, op_type, device_id, user_id, payload, created_at, pushed_at)
|
|
51
|
+
`INSERT OR IGNORE INTO sync_outbox (op_id, op_type, device_id, user_id, payload, created_at, pushed_at)
|
|
52
52
|
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
53
53
|
).run(op.opId, op.opType, op.deviceId, op.userId, payloadStr, op.createdAt, null);
|
|
54
54
|
}
|
|
@@ -119,6 +119,10 @@ function parsePayload(payload) {
|
|
|
119
119
|
}
|
|
120
120
|
return payload;
|
|
121
121
|
}
|
|
122
|
+
function serializeKeywords(keywords) {
|
|
123
|
+
if (!keywords || keywords.length === 0) return null;
|
|
124
|
+
return keywords.join(" ");
|
|
125
|
+
}
|
|
122
126
|
function applyMemoryCreated(db, op) {
|
|
123
127
|
const p = parsePayload(op.payload);
|
|
124
128
|
const stmt = db.prepare(
|
|
@@ -148,7 +152,7 @@ function applyMemoryCreated(db, op) {
|
|
|
148
152
|
source_id: p.sourceId ?? null,
|
|
149
153
|
confidence: p.confidence ?? 1,
|
|
150
154
|
salience: p.salience ?? 1,
|
|
151
|
-
keywords:
|
|
155
|
+
keywords: serializeKeywords(p.keywords),
|
|
152
156
|
event_date_start: p.eventDateStart ?? null,
|
|
153
157
|
event_date_end: p.eventDateEnd ?? null,
|
|
154
158
|
invalidated_at: p.invalidatedAt ?? null
|
|
@@ -184,6 +188,17 @@ function applyFeedbackGiven(db, op) {
|
|
|
184
188
|
"UPDATE memories SET use_count = use_count + 1 WHERE memory_id = ?"
|
|
185
189
|
).run(p.memoryId);
|
|
186
190
|
}
|
|
191
|
+
const row = db.prepare(
|
|
192
|
+
"SELECT use_count, success_count FROM memories WHERE memory_id = ?"
|
|
193
|
+
).get(p.memoryId);
|
|
194
|
+
if (row && row.use_count > 0) {
|
|
195
|
+
const successRate = row.success_count / row.use_count;
|
|
196
|
+
const newSalience = Math.min(1, Math.max(0.1, 0.5 + 0.5 * successRate));
|
|
197
|
+
db.prepare("UPDATE memories SET salience = ? WHERE memory_id = ?").run(
|
|
198
|
+
newSalience,
|
|
199
|
+
p.memoryId
|
|
200
|
+
);
|
|
201
|
+
}
|
|
187
202
|
}
|
|
188
203
|
function applyMemoryTierSet(db, op) {
|
|
189
204
|
const p = parsePayload(op.payload);
|
|
@@ -334,14 +349,26 @@ var SyncClient = class {
|
|
|
334
349
|
this.inbox.addOps(ops);
|
|
335
350
|
}
|
|
336
351
|
const pending = this.inbox.getPending();
|
|
352
|
+
let skippedCount = 0;
|
|
337
353
|
if (pending.length > 0) {
|
|
338
354
|
const result = applyInboxOps(this.db, pending);
|
|
339
355
|
if (result.applied.length > 0) {
|
|
340
356
|
this.inbox.markApplied(result.applied);
|
|
341
357
|
}
|
|
358
|
+
skippedCount = result.skipped.length;
|
|
359
|
+
if (skippedCount > 0) {
|
|
360
|
+
for (const s of result.skipped) {
|
|
361
|
+
process.stderr.write(
|
|
362
|
+
`[sync] apply skipped op ${s.opId}: ${s.reason}
|
|
363
|
+
`
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
342
367
|
}
|
|
343
368
|
this.setCursor(nextCursor);
|
|
344
|
-
|
|
369
|
+
if (skippedCount === 0) {
|
|
370
|
+
this.setState("last_pull_success_at", (/* @__PURE__ */ new Date()).toISOString());
|
|
371
|
+
}
|
|
345
372
|
return ops.length;
|
|
346
373
|
}
|
|
347
374
|
/** For tests / advanced callers: apply currently-pending inbox ops manually. */
|