@retaw106/mmc-pi 0.0.0-stage → 1.2.2

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,116 @@
1
+ import {
2
+ withSqliteBackgroundWriter,
3
+ withoutSqliteTransformPass
4
+ } from "./index-wv28nczk.js";
5
+ import"./index-54cgx9gm.js";
6
+ import {
7
+ logSlowWriteTransaction
8
+ } from "./index-65ee50rm.js";
9
+
10
+ // ../plugin/src/features/magic-context/message-time-backfill.ts
11
+ var MESSAGE_TIME_BACKFILL_BATCH_SIZE = 500;
12
+ var BACKFILL_STATE_ID = 1;
13
+ var activeBackfills = new WeakMap;
14
+ function normalizedMessageTime(value) {
15
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : null;
16
+ }
17
+ function getBackfillState(db) {
18
+ const row = db.prepare(`SELECT cursor_session_id AS cursorSessionId,
19
+ cursor_ordinal AS cursorOrdinal,
20
+ completed
21
+ FROM message_time_backfill_state
22
+ WHERE id = ?`).get(BACKFILL_STATE_ID);
23
+ return {
24
+ processed: 0,
25
+ cursorSessionId: typeof row?.cursorSessionId === "string" ? row.cursorSessionId : "",
26
+ cursorOrdinal: typeof row?.cursorOrdinal === "number" && Number.isSafeInteger(row.cursorOrdinal) ? row.cursorOrdinal : 0,
27
+ completed: row?.completed === 1
28
+ };
29
+ }
30
+ function readMessageTimes(reader, rows) {
31
+ const times = new Map;
32
+ const bySession = new Map;
33
+ for (const row of rows) {
34
+ const list = bySession.get(row.sessionId) ?? [];
35
+ list.push(row);
36
+ bySession.set(row.sessionId, list);
37
+ }
38
+ for (const [sessionId, sessionRows] of bySession) {
39
+ const ordinals = sessionRows.map((row) => Number(row.messageOrdinal));
40
+ const firstOrdinal = Math.min(...ordinals);
41
+ const lastOrdinal = Math.max(...ordinals);
42
+ const messages = reader.readPage ? reader.readPage(sessionId, Math.max(0, firstOrdinal - 1), Math.max(1, lastOrdinal - firstOrdinal + 1), lastOrdinal) : reader(sessionId).filter((message) => message.ordinal >= firstOrdinal && message.ordinal <= lastOrdinal);
43
+ const wantedIds = new Set(sessionRows.map((row) => row.messageId));
44
+ for (const message of messages) {
45
+ const time = normalizedMessageTime(message.createdAt);
46
+ if (time !== null && wantedIds.has(message.id))
47
+ times.set(message.id, time);
48
+ }
49
+ }
50
+ return times;
51
+ }
52
+ function backfillMessageTimesBatch(db, reader, batchSize = MESSAGE_TIME_BACKFILL_BATCH_SIZE) {
53
+ const state = getBackfillState(db);
54
+ if (state.completed)
55
+ return state;
56
+ const boundedBatchSize = Math.max(1, Math.floor(batchSize));
57
+ const rows = db.prepare(`SELECT map.session_id AS sessionId,
58
+ map.message_ordinal AS messageOrdinal,
59
+ fts.message_id AS messageId
60
+ FROM message_fts_rowid_map AS map
61
+ JOIN message_history_fts AS fts ON fts.rowid = map.fts_rowid
62
+ WHERE map.message_time_ms IS NULL
63
+ AND (map.session_id > ?
64
+ OR (map.session_id = ? AND map.message_ordinal > ?))
65
+ ORDER BY map.session_id ASC, map.message_ordinal ASC
66
+ LIMIT ?`).all(state.cursorSessionId, state.cursorSessionId, state.cursorOrdinal, boundedBatchSize);
67
+ const times = readMessageTimes(reader, rows);
68
+ const last = rows.at(-1);
69
+ const completed = rows.length < boundedBatchSize;
70
+ const cursorSessionId = last?.sessionId ?? state.cursorSessionId;
71
+ const cursorOrdinal = last ? Number(last.messageOrdinal) : state.cursorOrdinal;
72
+ let transactionStartedAt = 0;
73
+ db.transaction(() => {
74
+ transactionStartedAt = performance.now();
75
+ db.prepare(`INSERT OR IGNORE INTO message_time_backfill_state
76
+ (id, cursor_session_id, cursor_ordinal, completed) VALUES (?, '', 0, 0)`).run(BACKFILL_STATE_ID);
77
+ const update = db.prepare(`UPDATE message_fts_rowid_map
78
+ SET message_time_ms = ?
79
+ WHERE session_id = ? AND message_ordinal = ? AND message_time_ms IS NULL`);
80
+ for (const row of rows) {
81
+ const time = times.get(row.messageId);
82
+ if (time !== undefined)
83
+ update.run(time, row.sessionId, Number(row.messageOrdinal));
84
+ }
85
+ db.prepare(`UPDATE message_time_backfill_state
86
+ SET cursor_session_id = ?, cursor_ordinal = ?, completed = ?, updated_at = ?
87
+ WHERE id = ?`).run(cursorSessionId, cursorOrdinal, completed ? 1 : 0, Date.now(), BACKFILL_STATE_ID);
88
+ }).immediate();
89
+ logSlowWriteTransaction("message_time_backfill", transactionStartedAt);
90
+ return {
91
+ processed: rows.length,
92
+ cursorSessionId,
93
+ cursorOrdinal,
94
+ completed
95
+ };
96
+ }
97
+ async function runMessageTimeBackfill(db, reader) {
98
+ await new Promise((resolve) => setTimeout(resolve, 0));
99
+ for (;; ) {
100
+ const progress = backfillMessageTimesBatch(db, reader);
101
+ if (progress.completed)
102
+ return;
103
+ await new Promise((resolve) => setTimeout(resolve, 0));
104
+ }
105
+ }
106
+ function startMessageTimeBackfill(db, reader) {
107
+ const active = activeBackfills.get(db);
108
+ if (active)
109
+ return active;
110
+ const run = withoutSqliteTransformPass(() => withSqliteBackgroundWriter(() => runMessageTimeBackfill(db, reader))).finally(() => activeBackfills.delete(db));
111
+ activeBackfills.set(db, run);
112
+ return run;
113
+ }
114
+ export {
115
+ startMessageTimeBackfill
116
+ };
@@ -0,0 +1,56 @@
1
+ import {
2
+ hasMeaningfulUserText2,
3
+ extractTexts2,
4
+ resolveHostServedBoundaryId2,
5
+ hasRawMessageProvider2,
6
+ setRawMessageProvider2,
7
+ withRawMessageProvider2,
8
+ cleanUserText2,
9
+ withRawSessionMessageCache2,
10
+ readRawSessionMessages2,
11
+ readRawSessionMessagePage2,
12
+ getRawSessionMessageOrdinalCount2,
13
+ visitRawSessionMessages2,
14
+ readRawSessionMessageRange2,
15
+ primeTailRawMessageCache2,
16
+ getCachedAbsoluteMessageCount2,
17
+ primeInMemoryTailRawMessageCache2,
18
+ readRawSessionMessageIdOrdinalsForRange2,
19
+ hasRawSessionMessageById2,
20
+ readRawSessionMessageOrdinalById2,
21
+ compareRawSessionMessageOrder2,
22
+ readRawSessionMessageById2,
23
+ getRawSessionMessageCount2,
24
+ getRawSessionTagKeysThrough2,
25
+ getLegacyProtectedTailStartOrdinal2,
26
+ readSessionChunk2
27
+ } from "./index-ztb4k1mt.js";
28
+ import"./index-wv28nczk.js";
29
+ import"./index-54cgx9gm.js";
30
+ export {
31
+ cleanUserText2 as cleanUserText,
32
+ compareRawSessionMessageOrder2 as compareRawSessionMessageOrder,
33
+ extractTexts2 as extractTexts,
34
+ getCachedAbsoluteMessageCount2 as getCachedAbsoluteMessageCount,
35
+ getLegacyProtectedTailStartOrdinal2 as getLegacyProtectedTailStartOrdinal,
36
+ getRawSessionMessageCount2 as getRawSessionMessageCount,
37
+ getRawSessionMessageOrdinalCount2 as getRawSessionMessageOrdinalCount,
38
+ getRawSessionTagKeysThrough2 as getRawSessionTagKeysThrough,
39
+ hasMeaningfulUserText2 as hasMeaningfulUserText,
40
+ hasRawMessageProvider2 as hasRawMessageProvider,
41
+ hasRawSessionMessageById2 as hasRawSessionMessageById,
42
+ primeInMemoryTailRawMessageCache2 as primeInMemoryTailRawMessageCache,
43
+ primeTailRawMessageCache2 as primeTailRawMessageCache,
44
+ readRawSessionMessageById2 as readRawSessionMessageById,
45
+ readRawSessionMessageIdOrdinalsForRange2 as readRawSessionMessageIdOrdinalsForRange,
46
+ readRawSessionMessageOrdinalById2 as readRawSessionMessageOrdinalById,
47
+ readRawSessionMessagePage2 as readRawSessionMessagePage,
48
+ readRawSessionMessageRange2 as readRawSessionMessageRange,
49
+ readRawSessionMessages2 as readRawSessionMessages,
50
+ readSessionChunk2 as readSessionChunk,
51
+ resolveHostServedBoundaryId2 as resolveHostServedBoundaryId,
52
+ setRawMessageProvider2 as setRawMessageProvider,
53
+ visitRawSessionMessages2 as visitRawSessionMessages,
54
+ withRawMessageProvider2 as withRawMessageProvider,
55
+ withRawSessionMessageCache2 as withRawSessionMessageCache
56
+ };
@@ -0,0 +1,8 @@
1
+ import {
2
+ pushNotification2,
3
+ isTuiConnected2
4
+ } from "./index-b3eqj1g6.js";
5
+ export {
6
+ isTuiConnected2 as isTuiConnected,
7
+ pushNotification2 as pushNotification
8
+ };
@@ -0,0 +1,9 @@
1
+ import {
2
+ isDefaultSessionTitle2,
3
+ waitForSafeNotificationTarget2
4
+ } from "./index-v6scmxvt.js";
5
+ import"./index-54cgx9gm.js";
6
+ export {
7
+ isDefaultSessionTitle2 as isDefaultSessionTitle,
8
+ waitForSafeNotificationTarget2 as waitForSafeNotificationTarget
9
+ };