@morlay/session-rdb 0.1.10-alpha.0 → 0.1.10-alpha.3
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 +18 -3
- package/dist/artifact.d.mts +118 -2
- package/dist/artifact.mjs +5 -3
- package/dist/{log-TYgd8Xkr.d.mts → backend-BKtJ5sO5.d.mts} +183 -284
- package/dist/backend-rdb-BDCntVDy.mjs +1341 -0
- package/dist/branch-CG1PUlO6.mjs +833 -0
- package/dist/deletion-SSX7IqU8.mjs +35 -0
- package/dist/deletion.d.mts +1 -1
- package/dist/deletion.mjs +2 -2
- package/dist/export-CfJQjQfr.mjs +123 -0
- package/dist/export.d.mts +1 -1
- package/dist/export.mjs +31 -55
- package/dist/import.d.mts +1 -1
- package/dist/import.mjs +31 -76
- package/dist/index-CUD2ZpTA.d.mts +71 -0
- package/dist/index.d.mts +20 -2
- package/dist/index.mjs +1561 -4
- package/dist/open-YoyD_kah.mjs +561 -0
- package/dist/organize.d.mts +9 -0
- package/dist/organize.mjs +30 -0
- package/dist/route-DpfY0zq-.mjs +114 -0
- package/dist/storage.d.mts +41 -48
- package/dist/storage.mjs +3 -1
- package/dist/testing.d.mts +9 -1
- package/dist/testing.mjs +25 -4
- package/dist/usage-row-BQQM-_t6.mjs +36 -0
- package/dist/usage.d.mts +1 -1
- package/dist/usage.mjs +7 -40
- package/dist/write-guard-CYrY58GM.mjs +174 -0
- package/package.json +32 -36
- package/src/artifact.ts +4 -1
- package/src/backend-rdb.ts +331 -0
- package/src/backend.ts +32 -15
- package/src/{branch.ts → branch/index.ts} +109 -110
- package/src/branch/port.ts +62 -0
- package/src/branch/semantics.ts +69 -0
- package/src/db/open.ts +180 -0
- package/src/db/stats.ts +449 -0
- package/src/deletion-error.ts +17 -0
- package/src/deletion.ts +19 -72
- package/src/entities/index.ts +16 -1
- package/src/entities/v3/event-usage.ts +3 -3
- package/src/entities/v3/events.ts +3 -3
- package/src/entities/v3/index.ts +4 -16
- package/src/entities/v3/persistence-state.ts +3 -3
- package/src/entities/v3/schema-meta.ts +3 -3
- package/src/entities/v3/session-counts.ts +3 -3
- package/src/entities/v3/session-events.ts +3 -3
- package/src/entities/v3/session-projcache-rows.ts +3 -3
- package/src/entities/v3/session-usage.ts +3 -3
- package/src/entities/v3/sessions.ts +3 -3
- package/src/entities/v3/storage-records.ts +17 -0
- package/src/entities/v3/storage-units.ts +3 -3
- package/src/{schema.ts → event-shape.ts} +0 -46
- package/src/export.ts +32 -78
- package/src/handles.ts +349 -0
- package/src/http/route.ts +178 -0
- package/src/import-storages.ts +5 -2
- package/src/import.ts +63 -125
- package/src/index.ts +15 -1666
- package/src/legacy.ts +3 -1
- package/src/live-routing.ts +205 -0
- package/src/log/export.ts +84 -0
- package/src/{log.ts → log/repair.ts} +1 -268
- package/src/log/rows.ts +107 -0
- package/src/log/usage-row.ts +64 -0
- package/src/migrations.ts +196 -0
- package/src/organize.ts +38 -0
- package/src/read-path.ts +190 -0
- package/src/routes.ts +2 -1
- package/src/rows.ts +25 -77
- package/src/session-ops.ts +444 -0
- package/src/session-store.ts +675 -0
- package/src/stats-fold.ts +44 -0
- package/src/storage-takeover/index.ts +2 -5
- package/src/storage-takeover/legacy-domain-migration.ts +118 -0
- package/src/storage-takeover/projection-cache.ts +122 -71
- package/src/storage-takeover/read-model.ts +189 -0
- package/src/storage-takeover/repository.ts +236 -442
- package/src/storage-takeover/storage-backend.ts +44 -159
- package/src/storage-takeover/types.ts +45 -19
- package/src/storage-takeover/workspace-documents.ts +65 -0
- package/src/storage.ts +4 -2
- package/src/testing.ts +21 -1
- package/src/usage.ts +11 -65
- package/src/version.ts +13 -0
- package/src/write-path.ts +286 -0
- package/dist/branch-CoPCRKer.mjs +0 -401
- package/dist/deletion-CWl89TIR.mjs +0 -2249
- package/dist/gc.d.mts +0 -4
- package/dist/gc.mjs +0 -48
- package/dist/log-CcWy46VE.mjs +0 -558
- package/dist/routes-hZjTcX_R.mjs +0 -9
- package/dist/sqlite-BNRd1aqR.mjs +0 -1778
- package/drizzle/postgres/20260908072805_v2_initial/migration.sql +0 -58
- package/drizzle/postgres/20260908072805_v2_initial/snapshot.json +0 -686
- package/drizzle/postgres/20260908072806_v3_drop_original_seq/migration.sql +0 -1
- package/drizzle/postgres/20260908072806_v3_drop_original_seq/snapshot.json +0 -673
- package/drizzle/postgres/20260910120001_v3_storage_tables/migration.sql +0 -65
- package/drizzle/postgres/20260910120001_v3_storage_tables/snapshot.json +0 -1183
- package/drizzle/postgres/20260910130001_v3_session_title_backfill/migration.sql +0 -10
- package/drizzle/postgres/20260910130001_v3_session_title_backfill/snapshot.json +0 -1183
- package/drizzle/postgres/20260918120000_v3_event_usage/migration.sql +0 -14
- package/drizzle/postgres/20260918120000_v3_event_usage/snapshot.json +0 -1309
- package/drizzle/postgres/20260921075900_v3_agent_preset/migration.sql +0 -1
- package/drizzle/postgres/20260922090001_v3_pinned_seq/migration.sql +0 -2
- package/drizzle/postgres/20260922093001_v3_last_event_at/migration.sql +0 -10
- package/drizzle/postgres/20260923120000_v3_usage_materialized/migration.sql +0 -48
- package/drizzle/postgres/20261008103001_v3_schedule_tasks/migration.sql +0 -11
- package/drizzle/sqlite/20260908072751_v2_initial/migration.sql +0 -53
- package/drizzle/sqlite/20260908072751_v2_initial/snapshot.json +0 -492
- package/drizzle/sqlite/20260908072752_v3_drop_original_seq/migration.sql +0 -6
- package/drizzle/sqlite/20260908072752_v3_drop_original_seq/snapshot.json +0 -513
- package/drizzle/sqlite/20260910120000_v3_storage_tables/migration.sql +0 -66
- package/drizzle/sqlite/20260910120000_v3_storage_tables/snapshot.json +0 -905
- package/drizzle/sqlite/20260910130000_v3_session_title_backfill/migration.sql +0 -13
- package/drizzle/sqlite/20260910130000_v3_session_title_backfill/snapshot.json +0 -905
- package/drizzle/sqlite/20260918120000_v3_event_usage/migration.sql +0 -14
- package/drizzle/sqlite/20260918120000_v3_event_usage/snapshot.json +0 -1031
- package/drizzle/sqlite/20260921075855_v3_agent_preset/migration.sql +0 -7
- package/drizzle/sqlite/20260921075855_v3_agent_preset/snapshot.json +0 -1041
- package/drizzle/sqlite/20260922090000_v3_pinned_seq/migration.sql +0 -4
- package/drizzle/sqlite/20260922093000_v3_last_event_at/migration.sql +0 -13
- package/drizzle/sqlite/20260923120000_v3_usage_materialized/migration.sql +0 -48
- package/drizzle/sqlite/20261008103000_v3_schedule_tasks/migration.sql +0 -14
- package/src/adapters/index.ts +0 -12
- package/src/adapters/to-postgres.ts +0 -93
- package/src/adapters/to-sqlite.ts +0 -83
- package/src/adapters/types.ts +0 -44
- package/src/drizzle/postgres-v2.ts +0 -10
- package/src/drizzle/postgres-v3.ts +0 -18
- package/src/drizzle/sqlite-v2.ts +0 -9
- package/src/drizzle/sqlite-v3.ts +0 -18
- package/src/entities/v2/index.ts +0 -20
- package/src/entities/v2/session-events.ts +0 -10
- package/src/entities/v3/schedule-tasks.ts +0 -16
- package/src/entities/v3/workspace-sessions.ts +0 -19
- package/src/entities/v3/workspace-state.ts +0 -12
- package/src/entities/v3/workspaces.ts +0 -14
- package/src/gc.ts +0 -77
- package/src/postgres.ts +0 -1015
- package/src/sqlite.ts +0 -1096
package/src/import.ts
CHANGED
|
@@ -6,10 +6,11 @@ import { parseSessionFormatLogFilename } from "@deepseek-ai/dsh-session-format";
|
|
|
6
6
|
import type { SessionStorageMetadata } from "@deepseek-ai/dsh-session-persistence";
|
|
7
7
|
import { balanceRewindPrefix } from "@morlay/session-branch";
|
|
8
8
|
import { unzip } from "fflate";
|
|
9
|
-
import { replaceLiveSessionLog } from "./branch.ts";
|
|
9
|
+
import { replaceLiveSessionLog } from "./branch/index.ts";
|
|
10
10
|
import { restoreCatalog } from "./legacy.ts";
|
|
11
|
-
import type { SessionPersistenceRdb } from "./
|
|
11
|
+
import type { SessionPersistenceRdb } from "./session-store.ts";
|
|
12
12
|
import { SESSION_IMPORT_PATH } from "./routes.ts";
|
|
13
|
+
import { HttpError, mountJsonRoute } from "./http/route.ts";
|
|
13
14
|
|
|
14
15
|
export const SESSION_LOG_ARTIFACT_FILENAME = "session.jsonl";
|
|
15
16
|
|
|
@@ -210,128 +211,65 @@ export async function persistImport(
|
|
|
210
211
|
}
|
|
211
212
|
|
|
212
213
|
export function registerSessionImport(ctx: Context, persistence: SessionPersistenceRdb): void {
|
|
213
|
-
ctx
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
res.end(JSON.stringify({ error: "zip field is not valid base64" }));
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
if (zip.byteLength > MAX_IMPORT_ZIP_BYTES) {
|
|
277
|
-
res.writeHead(413, { "content-type": "application/json" });
|
|
278
|
-
res.end(JSON.stringify({ error: "imported zip exceeds the size limit" }));
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
|
-
let imported: SessionStorageMetadata & { events: SessionEvent[] };
|
|
282
|
-
try {
|
|
283
|
-
imported = await parseImportZip(zip);
|
|
284
|
-
} catch (error: unknown) {
|
|
285
|
-
res.writeHead(400, { "content-type": "application/json" });
|
|
286
|
-
res.end(
|
|
287
|
-
JSON.stringify({
|
|
288
|
-
error: error instanceof Error ? error.message : "imported zip is invalid",
|
|
289
|
-
}),
|
|
290
|
-
);
|
|
291
|
-
return;
|
|
292
|
-
}
|
|
293
|
-
const targetId =
|
|
294
|
-
typeof envelope.sessionId === "string"
|
|
295
|
-
? (envelope.sessionId as SessionId)
|
|
296
|
-
: undefined;
|
|
297
|
-
const branch = webCtx.get("sessionBranch") as unknown as
|
|
298
|
-
| {
|
|
299
|
-
rewind(id: SessionId, toBoundary: number): Promise<unknown>;
|
|
300
|
-
resetLiveDerivedState?(session: Session): void;
|
|
301
|
-
}
|
|
302
|
-
| undefined;
|
|
303
|
-
try {
|
|
304
|
-
const sessions = webCtx.get("sessions") as
|
|
305
|
-
| { get(id: SessionId): Session | undefined }
|
|
306
|
-
| undefined;
|
|
307
|
-
const id = await persistImport(
|
|
308
|
-
persistence,
|
|
309
|
-
branch,
|
|
310
|
-
imported,
|
|
311
|
-
targetId,
|
|
312
|
-
sessions,
|
|
313
|
-
(sessionId) => stopAgentLoop(webCtx, sessionId),
|
|
314
|
-
);
|
|
315
|
-
res.writeHead(200, { "content-type": "application/json" });
|
|
316
|
-
res.end(JSON.stringify({ sessionId: id }));
|
|
317
|
-
} catch (error: unknown) {
|
|
318
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
319
|
-
if (targetId !== undefined && /not found/i.test(message)) {
|
|
320
|
-
res.writeHead(404, { "content-type": "application/json" });
|
|
321
|
-
res.end(JSON.stringify({ error: `session "${targetId}" not found` }));
|
|
322
|
-
return;
|
|
323
|
-
}
|
|
324
|
-
res.writeHead(500, { "content-type": "application/json" });
|
|
325
|
-
res.end(
|
|
326
|
-
JSON.stringify({
|
|
327
|
-
error: error instanceof Error ? error.message : "import failed",
|
|
328
|
-
}),
|
|
329
|
-
);
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
},
|
|
333
|
-
}),
|
|
334
|
-
`session-rdb: ${SESSION_IMPORT_PATH} route`,
|
|
335
|
-
);
|
|
214
|
+
mountJsonRoute(ctx, {
|
|
215
|
+
path: SESSION_IMPORT_PATH,
|
|
216
|
+
handle: async (body, webCtx) => {
|
|
217
|
+
const envelope = body as { zip?: unknown; sessionId?: unknown };
|
|
218
|
+
if (typeof envelope.zip !== "string" || envelope.zip === "") {
|
|
219
|
+
throw new HttpError(400, "missing zip field");
|
|
220
|
+
}
|
|
221
|
+
if (
|
|
222
|
+
envelope.sessionId !== undefined &&
|
|
223
|
+
(typeof envelope.sessionId !== "string" || envelope.sessionId === "")
|
|
224
|
+
) {
|
|
225
|
+
throw new HttpError(400, "sessionId must be a non-empty string");
|
|
226
|
+
}
|
|
227
|
+
let zip: Uint8Array;
|
|
228
|
+
try {
|
|
229
|
+
zip = Buffer.from(envelope.zip, "base64");
|
|
230
|
+
} catch {
|
|
231
|
+
throw new HttpError(400, "zip field is not valid base64");
|
|
232
|
+
}
|
|
233
|
+
if (zip.byteLength > MAX_IMPORT_ZIP_BYTES) {
|
|
234
|
+
throw new HttpError(413, "imported zip exceeds the size limit");
|
|
235
|
+
}
|
|
236
|
+
let imported: SessionStorageMetadata & { events: SessionEvent[] };
|
|
237
|
+
try {
|
|
238
|
+
imported = await parseImportZip(zip);
|
|
239
|
+
} catch (error: unknown) {
|
|
240
|
+
throw new HttpError(
|
|
241
|
+
400,
|
|
242
|
+
error instanceof Error ? error.message : "imported zip is invalid",
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
const targetId =
|
|
246
|
+
typeof envelope.sessionId === "string" ? (envelope.sessionId as SessionId) : undefined;
|
|
247
|
+
const branch = webCtx.get("sessionBranch") as unknown as
|
|
248
|
+
| {
|
|
249
|
+
rewind(id: SessionId, toBoundary: number): Promise<unknown>;
|
|
250
|
+
resetLiveDerivedState?(session: Session): void;
|
|
251
|
+
}
|
|
252
|
+
| undefined;
|
|
253
|
+
const sessions = webCtx.get("sessions") as
|
|
254
|
+
| { get(id: SessionId): Session | undefined }
|
|
255
|
+
| undefined;
|
|
256
|
+
try {
|
|
257
|
+
const id = await persistImport(
|
|
258
|
+
persistence,
|
|
259
|
+
branch,
|
|
260
|
+
imported,
|
|
261
|
+
targetId,
|
|
262
|
+
sessions,
|
|
263
|
+
(sessionId) => stopAgentLoop(webCtx, sessionId),
|
|
264
|
+
);
|
|
265
|
+
return { body: { sessionId: id } };
|
|
266
|
+
} catch (error: unknown) {
|
|
267
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
268
|
+
if (targetId !== undefined && /not found/i.test(message)) {
|
|
269
|
+
throw new HttpError(404, `session "${targetId}" not found`);
|
|
270
|
+
}
|
|
271
|
+
throw error;
|
|
272
|
+
}
|
|
273
|
+
},
|
|
336
274
|
});
|
|
337
275
|
}
|