@dsh-agentic-roleplay/core 0.6.0 → 0.7.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/lib/index.js CHANGED
@@ -1,18 +1,18 @@
1
1
  import { n as chatSessionId, r as roleplaySessionOf } from "./access-EbNdZFbk.js";
2
- import { B as loadItems, Bt as ImportCardError, C as populateLocations, D as populateVariables, E as populateTriggers, Et as writeLorebookEntry, F as loadCalendar, Ft as parseLoreEdit, Gt as LORE_SCOPES, Ht as pictureOf, I as parseCalendarFile, It as parsePlaceEdit, J as withPlayer, Jt as DICE_RESULTS, Kt as DICE_COMPARISONS, Lt as parseRelationEdit, M as loadDice, Mt as InvalidEditProblems, N as parseDiceFile, Nt as parseCharacterEdit, O as routineProblems, P as CALENDAR_FILE_NAME, Pt as parseFactionEdit, Q as variableProblems, R as loadLocations, Rt as parseThingEdit, S as populateKnowledge, St as lorebookDirectory, T as populateRoutines, Tt as updateLorebookInfo, U as CHARACTERS_DIR, Ut as readCard, Vt as isPng, W as loadCharacters, Wt as LORE_KINDS, X as loadVariables, Y as VARIABLES_FILE_NAME, Z as parseVariablesFile, _ as populateCharacters, _t as deleteLorebookEntry, at as CHARACTER_FILE_NAME, b as populateFactions, bt as listLorebooks, ct as ImportNotFoundError, d as markReady, dt as WORLD_FILE_NAME, f as guardedLookup, ft as parseWorldFile, g as populateCalendar, gt as deleteLorebook, h as knowsCharacter, ht as createLorebook, j as DICE_FILE_NAME, jt as subfolders, k as TRIGGERS_FILE_NAME, l as authoringChatSessionId, lt as ImportStore, m as loadValidatedWorld, mt as addLorebookEntries, ot as parseCharacterFile, p as vetUrl, pt as parseWorldInfoEdit, q as partsOf, qt as DICE_READINGS, st as IMPORT_FOLDER, t as AuthoringTools, ut as parseChoices, v as populateClock, vt as entriesOf, w as populateLore, wt as reserveLorebook, x as populateItems, xt as loadLorebook, y as populateDice, yt as isLorebookName, zt as IMPORT_COMMAND } from "./authoring-tools-BGzzW6O5.js";
3
- import { _ as readWorldPrompt, a as isPresetName, c as presetDirectory, d as savePresetFromWorld, g as listWorldPrompts, h as deleteWorldPrompt, i as deletePreset, o as listPresets, p as updatePresetInfo, r as createPresetFromShipped, s as loadPreset, t as PRESET_FILE, u as reservePreset, v as writeWorldPrompt } from "./presets-DcPCnn8O.js";
4
- import { _ as readTimeline, d as TimelineError, g as switchPreset, h as recordPreset, i as reflectionStatus, m as presetStatus, p as copyPreset, v as pathTo, y as defaultBoundaries } from "./reflection-6qfOC5OT.js";
2
+ import { B as loadItems, Bt as ImportCardError, C as populateLocations, D as populateVariables, E as populateTriggers, Et as writeLorebookEntry, F as loadCalendar, Ft as parseLoreEdit, Gt as LORE_SCOPES, Ht as pictureOf, I as parseCalendarFile, It as parsePlaceEdit, J as withPlayer, Jt as DICE_RESULTS, Kt as DICE_COMPARISONS, Lt as parseRelationEdit, M as loadDice, Mt as InvalidEditProblems, N as parseDiceFile, Nt as parseCharacterEdit, O as routineProblems, P as CALENDAR_FILE_NAME, Pt as parseFactionEdit, Q as variableProblems, R as loadLocations, Rt as parseThingEdit, S as populateKnowledge, St as lorebookDirectory, T as populateRoutines, Tt as updateLorebookInfo, U as CHARACTERS_DIR, Ut as readCard, Vt as isPng, W as loadCharacters, Wt as LORE_KINDS, X as loadVariables, Y as VARIABLES_FILE_NAME, Z as parseVariablesFile, _ as populateCharacters, _t as deleteLorebookEntry, at as CHARACTER_FILE_NAME, b as populateFactions, bt as listLorebooks, ct as ImportNotFoundError, d as markReady, dt as WORLD_FILE_NAME, f as guardedLookup, ft as parseWorldFile, g as populateCalendar, gt as deleteLorebook, h as knowsCharacter, ht as createLorebook, j as DICE_FILE_NAME, jt as subfolders, k as TRIGGERS_FILE_NAME, l as authoringChatSessionId, lt as ImportStore, m as loadValidatedWorld, mt as addLorebookEntries, ot as parseCharacterFile, p as vetUrl, pt as parseWorldInfoEdit, q as partsOf, qt as DICE_READINGS, st as IMPORT_FOLDER, t as AuthoringTools, ut as parseChoices, v as populateClock, vt as entriesOf, w as populateLore, wt as reserveLorebook, x as populateItems, xt as loadLorebook, y as populateDice, yt as isLorebookName, zt as IMPORT_COMMAND } from "./authoring-tools-BWjEr_lF.js";
3
+ import { _ as readWorldPrompt, a as isPresetName, c as presetDirectory, d as savePresetFromWorld, g as listWorldPrompts, h as deleteWorldPrompt, i as deletePreset, o as listPresets, p as updatePresetInfo, r as createPresetFromShipped, s as loadPreset, t as PRESET_FILE, u as reservePreset, v as writeWorldPrompt } from "./presets-D11bxWX0.js";
4
+ import { _ as readTimeline, b as defaultBoundaries, d as TimelineError, g as switchPreset, h as recordPreset, i as reflectionStatus, m as presetStatus, p as copyPreset, v as writeTimeline, y as pathTo } from "./reflection-CRCWK2E1.js";
5
5
  import { a as liveSettings, b as VARIABLE_VISIBILITIES, c as writeSettings, d as parseSettings, i as InvalidSettingsError, l as DEFAULT_SETTINGS, n as encodeOutput, o as readSettings, p as WORLD_RULES_KEY, s as useChildReasoning, u as SETTINGS_SPEC, v as VARIABLE_KINDS, y as VARIABLE_SUBJECTS } from "./narration-_dA9oIt2.js";
6
- import { C as readNotes, G as formatIssue, H as plotReferenceProblems, K as errorMessage, M as plotProgressOf, N as populatePlots, O as zoneOf, Q as languageOf, S as notesOfSession, U as readDocument, V as parsePlotsFile, X as copyWorldPrompts, Y as PromptLibrary, _ as readRunDetail, at as InvalidWorldError, b as readWithdrawn, ct as SessionNotFoundError, g as readActivity, h as readDecisionStats, i as externalTools, it as InvalidStartError, j as PLOTS_METADATA_KEY, lt as WorldNotFoundError, m as readUsage, ot as ReflectionRefusedError, rt as AgentUnavailableError, st as SessionArchivedError, tt as shippedLanguages, w as observeAsPlayer, x as addToNote } from "./hooks-BLperIIB.js";
6
+ import { C as readNotes, G as formatIssue, H as plotReferenceProblems, K as errorMessage, M as plotProgressOf, N as populatePlots, O as zoneOf, Q as languageOf, S as notesOfSession, U as readDocument, V as parsePlotsFile, X as copyWorldPrompts, Y as PromptLibrary, _ as readRunDetail, at as InvalidWorldError, b as readWithdrawn, ct as SessionNotFoundError, dt as translator, g as readActivity, h as readDecisionStats, i as externalTools, it as InvalidStartError, j as PLOTS_METADATA_KEY, lt as WorldNotFoundError, m as readUsage, ot as ReflectionRefusedError, rt as AgentUnavailableError, st as SessionArchivedError, tt as shippedLanguages, ut as languageOf$1, w as observeAsPlayer, x as addToNote } from "./hooks-DufjUf1p.js";
7
7
  import { t as trimCharacters } from "./text-CT1re6XT.js";
8
8
  import { c as parseSampling, l as sharedSamplingOf, r as childSamplingOf } from "./child-D5SVNk4O.js";
9
9
  import { a as inheritImages, b as slug, c as referenceImagePath, l as sceneImagePath, t as addToLibrary, x as uniqueKey } from "./library-DsDc6fQa.js";
10
- import { A as worldEntityImageFile, B as readWorldInfo, C as readWorldEditor, D as uploadWorldCharacterPortrait, E as replaceFile, F as loadWorldParts, I as PRESET_FILE$1, L as presetMessage, O as uploadWorldEntityImage, S as generateWorldEntityImage, T as removeWorldTrigger, U as languageOfDocuments, V as translateMessage, W as readWorldDocuments, _ as editWorldPlots$1, a as deleteWorldPackFile, b as entityFolder, c as createAuthoredEntity, f as editWorldCharacter$1, g as editWorldPlace$1, h as editWorldNarrationVoice, j as writeWorldTrigger, k as worldCharacterPortraitFile, l as deleteAuthoredEntity, m as editWorldLore$1, n as packFileProblem, p as editWorldFaction$1, t as MAX_PACK_TEXT, u as readAuthoredEntityDocument, v as editWorldRelation$1, w as readWorldTrigger, x as generateWorldCharacterPortrait, y as editWorldThing$1, z as editWorldInfo } from "./pack-jail-BKFmbYP8.js";
11
- import { n as readPreset, r as selectPrompts, t as previewPreset } from "./preset-CJLW8Mp0.js";
12
- import { a as lorebookImportMessage, d as selectEntries, l as previewLorebook, s as lorebookChatSessionId, t as LOREBOOK_UPLOAD, u as readLorebook } from "./lorebook-author-tools-C-cFiE6E.js";
13
- import { a as presetChatSessionId, n as narrationImportMessage } from "./preset-author-tools-1RstgEZs.js";
10
+ import { A as worldEntityImageFile, B as readWorldInfo, C as readWorldEditor, D as uploadWorldCharacterPortrait, E as replaceFile, F as loadWorldParts, I as PRESET_FILE$1, L as presetMessage, O as uploadWorldEntityImage, S as generateWorldEntityImage, T as removeWorldTrigger, U as languageOfDocuments, V as translateMessage, W as readWorldDocuments, _ as editWorldPlots$1, a as deleteWorldPackFile, b as entityFolder, c as createAuthoredEntity, f as editWorldCharacter$1, g as editWorldPlace$1, h as editWorldNarrationVoice, j as writeWorldTrigger, k as worldCharacterPortraitFile, l as deleteAuthoredEntity, m as editWorldLore$1, n as packFileProblem, p as editWorldFaction$1, t as MAX_PACK_TEXT, u as readAuthoredEntityDocument, v as editWorldRelation$1, w as readWorldTrigger, x as generateWorldCharacterPortrait, y as editWorldThing$1, z as editWorldInfo } from "./pack-jail--Ij90LeY.js";
11
+ import { n as readPreset, r as selectPrompts, t as previewPreset } from "./preset-CDNyJhfU.js";
12
+ import { a as lorebookImportMessage, d as selectEntries, l as previewLorebook, s as lorebookChatSessionId, t as LOREBOOK_UPLOAD, u as readLorebook } from "./lorebook-author-tools-v-PM8Kbs.js";
13
+ import { a as presetChatSessionId, n as narrationImportMessage } from "./preset-author-tools-D7Azdlaq.js";
14
14
  import { t as doctorChatSessionId } from "./doctor-chat-ByDfCMeo.js";
15
- import { _ as API_PREFIX, a as packIds, c as describePack, d as packPath, f as readPack, l as isPackId, m as PACK_LIMITS, n as anyTab, o as syncWorldPacks, r as copyWorldPacks, s as worldPackStates, t as activePack, u as packFile } from "./packs-ZTG7fWut.js";
15
+ import { _ as API_PREFIX, a as packIds, c as describePack, d as packPath, f as readPack, l as isPackId, m as PACK_LIMITS, n as anyTab, o as syncWorldPacks, r as copyWorldPacks, s as worldPackStates, t as activePack, u as packFile } from "./packs-DWKnL4YX.js";
16
16
  import { a as uploadReferenceImage, t as generateReferenceImage } from "./generator-CO8usAjE.js";
17
17
  import { i as updateImage, n as galleryFile, r as listGallery } from "./gallery-C7VOJ3xH.js";
18
18
  import { basename, dirname, extname, isAbsolute, join, normalize, relative, resolve, sep } from "node:path";
@@ -30,6 +30,7 @@ import { request } from "node:http";
30
30
  import { request as request$1 } from "node:https";
31
31
  import { pipeline } from "node:stream/promises";
32
32
  import { homedir } from "node:os";
33
+ import { spawn } from "node:child_process";
33
34
  import z from "@deepseek-ai/schemastery";
34
35
  //#region src/host/agent/opening.ts
35
36
  /** What the opening is reported to come from, where a chat shows the source of a message. */
@@ -59,18 +60,13 @@ function reservePromptHead(session, turn, step) {
59
60
  }, { surfaceOp: "append" });
60
61
  }
61
62
  /**
62
- * The log of a chat that opens with a story: a finished first turn in which the
63
- * output was delivered the way every later turn delivers it (a `narrate` call and
64
- * its result), so it renders the same and the model reads a history that looks like
65
- * the one it will write. The player's message starts the second turn. Nothing is
66
- * asked of a model.
63
+ * One finished turn in which the output was delivered the way every turn delivers it (a `narrate` call and its result), so it renders
64
+ * the same and the model reads a history that looks like the one it will write. A turn with a player starts with the player's message.
67
65
  */
68
- function openingEvents(segments) {
69
- const session = Session.create(SessionId("opening"));
70
- const turn = 1;
66
+ function appendNarratedTurn(session, turn, output, player) {
71
67
  const step = 1;
72
- const callId = ToolCallId("opening-narrate");
73
- const sent = segments.map((segment) => segment.type === "utterance" ? {
68
+ const callId = ToolCallId(player === void 0 ? "opening-narrate" : `narrate-${turn}`);
69
+ const sent = output.segments.map((segment) => segment.type === "utterance" ? {
74
70
  type: "utterance",
75
71
  character: segment.character,
76
72
  text: segment.text
@@ -79,12 +75,21 @@ function openingEvents(segments) {
79
75
  text: segment.text
80
76
  });
81
77
  const args = JSON.stringify({ segments: sent });
82
- session.append("turn/start", { turn });
78
+ const started = session.append("turn/start", { turn });
83
79
  session.append("step/start", {
84
80
  turn,
85
81
  step
86
82
  });
87
- reservePromptHead(session, turn, step);
83
+ let asked;
84
+ if (player === void 0) reservePromptHead(session, turn, step);
85
+ else asked = session.append("user/message", createMessage({
86
+ role: "user",
87
+ content: [{
88
+ type: "text",
89
+ text: player
90
+ }],
91
+ source: { kind: "user" }
92
+ }), { surfaceOp: "append" }).seq;
88
93
  session.append("assistant/message", {
89
94
  stream: [],
90
95
  turn,
@@ -117,10 +122,7 @@ function openingEvents(segments) {
117
122
  callId,
118
123
  content: [{
119
124
  type: "text",
120
- text: `The output has been delivered to the player. The turn ends here.\n${encodeOutput({
121
- segments: [...segments],
122
- repaired: false
123
- })}`
125
+ text: `The output has been delivered to the player. The turn ends here.\n${encodeOutput(output)}`
124
126
  }],
125
127
  isError: false
126
128
  })
@@ -132,10 +134,33 @@ function openingEvents(segments) {
132
134
  turn,
133
135
  step
134
136
  });
137
+ const ended = session.append("turn/end", {
138
+ turn,
139
+ reason: { kind: "completed" }
140
+ });
141
+ return {
142
+ startSeq: started.seq,
143
+ ...asked === void 0 ? {} : { userSeq: asked },
144
+ endSeq: ended.seq
145
+ };
146
+ }
147
+ /** A turn in which nothing was said and nothing was delivered, to keep the log's turns consecutive. */
148
+ function appendEmptyTurn(session, turn) {
149
+ session.append("turn/start", { turn });
135
150
  session.append("turn/end", {
136
151
  turn,
137
152
  reason: { kind: "completed" }
138
153
  });
154
+ }
155
+ /**
156
+ * The log of a chat that opens with a story: a finished first turn. The player's message starts the second turn. Nothing is asked of a model.
157
+ */
158
+ function openingEvents(segments) {
159
+ const session = Session.create(SessionId("opening"));
160
+ appendNarratedTurn(session, 1, {
161
+ segments: [...segments],
162
+ repaired: false
163
+ });
139
164
  return session.snapshotEvents();
140
165
  }
141
166
  /** A visible first turn makes a newly created authoring chat a full conversation immediately. */
@@ -181,8 +206,8 @@ function authoringOpeningEvents(welcome, source = AUTHORING_OPENING_SOURCE) {
181
206
  * The agent loop reads its turn counter from the log when it is built, so the
182
207
  * opening has to be in the stored log by then: the chat is then adopted by id.
183
208
  */
184
- async function storeOpening(persistence, chat, segments) {
185
- await storeInitialEvents(persistence, chat, openingEvents(segments));
209
+ async function storeOpening(persistence, chat, segments, events = openingEvents(segments)) {
210
+ await storeInitialEvents(persistence, chat, events);
186
211
  }
187
212
  async function storeAuthoringOpening(persistence, chat, welcome) {
188
213
  await storeInitialEvents(persistence, chat, authoringOpeningEvents(welcome));
@@ -248,13 +273,27 @@ function agentServicesOf(ctx) {
248
273
  const registry = ctx.get("workspaceRegistry");
249
274
  if (controller === void 0 || registry === void 0) throw new AgentUnavailableError("this DSH profile has no session services, so a roleplay chat cannot be opened");
250
275
  const persistence = ctx.get("sessionPersistence");
276
+ const agents = ctx.get("agents");
251
277
  return {
252
278
  controller,
253
279
  registry,
254
- ...persistence === void 0 ? {} : { persistence }
280
+ ...persistence === void 0 ? {} : { persistence },
281
+ ...agents === void 0 ? {} : { unload: unloader(agents) }
255
282
  };
256
283
  };
257
284
  }
285
+ /** DSH has no call to unload an agent; disposing its scope is what its own teardown (a closed log, a released registration) hangs from. */
286
+ function unloader(agents) {
287
+ return async (chat) => {
288
+ const live = agents.get(chat);
289
+ if (live === void 0) return;
290
+ await (live.scope.rawDispose ?? live.scope.dispose.bind(live.scope))();
291
+ for (let waited = 0; agents.get(chat) !== void 0; waited += 50) {
292
+ if (waited >= 5e3) throw new Error(`the chat ${chat} is still loaded and cannot be replaced`);
293
+ await new Promise((resolve) => setTimeout(resolve, 50));
294
+ }
295
+ };
296
+ }
258
297
  /**
259
298
  * Gives roleplay sessions a DSH chat. All chats share one project whose folder
260
299
  * is the roleplay root, because DSH groups a session under a project only when
@@ -271,9 +310,10 @@ var AgentSessions = class {
271
310
  * Create the chat of a roleplay session unless it exists; returns its DSH session id.
272
311
  * @param title - the name to give a chat that is new; one that exists keeps its own.
273
312
  * @param opening - what a chat that is new opens with: narration and lines of its characters, shown before the player says anything.
313
+ * @param events - the whole log a chat that is new starts from, when it is made again from a session that was played (the opening is its first turn).
274
314
  * @throws {AgentUnavailableError} when there is an opening and no way to store it.
275
315
  */
276
- async ensure(sessionId, title, opening) {
316
+ async ensure(sessionId, title, opening, events) {
277
317
  const services = this.services();
278
318
  const { registry, persistence } = services;
279
319
  const project = await registry.create(this.projectPath(), PROJECT_TITLE);
@@ -282,11 +322,12 @@ var AgentSessions = class {
282
322
  const stored = opening !== void 0 && !existed && await isStored(persistence, wanted);
283
323
  if (opening !== void 0 && !existed && !stored) {
284
324
  if (persistence === void 0) throw new AgentUnavailableError("this DSH profile cannot store a chat with an opening message");
325
+ await services.unload?.(wanted);
285
326
  await storeOpening(persistence, {
286
327
  id: wanted,
287
328
  cwd: project.path,
288
329
  agentPreset: AGENT_PRESET
289
- }, opening);
330
+ }, opening, events);
290
331
  }
291
332
  return await ensureAgentChat(services, {
292
333
  projectPath: project.path,
@@ -296,6 +337,34 @@ var AgentSessions = class {
296
337
  title
297
338
  }, project);
298
339
  }
340
+ /** Whether the chat of a session exists on this installation: in the project, or stored and waiting to be attached. */
341
+ async has(sessionId) {
342
+ const { registry, persistence } = this.services();
343
+ const project = await registry.create(this.projectPath(), PROJECT_TITLE);
344
+ const wanted = chatSessionId(sessionId);
345
+ return project.sessionIds.includes(wanted) || await isStored(persistence, wanted);
346
+ }
347
+ /**
348
+ * Put a log made again in the place of the chat's own, for a session whose folder came from another installation after the chat was
349
+ * made. The old log is deleted first, and with it the chat's title, which is given again; DSH keeps the chat in its project, so the same id is stored again and `ensure` is not needed.
350
+ */
351
+ async replace(sessionId, title, events, purge) {
352
+ const { controller, registry, persistence, unload } = this.services();
353
+ if (persistence === void 0) throw new AgentUnavailableError("this DSH profile cannot store a chat");
354
+ const project = await registry.create(this.projectPath(), PROJECT_TITLE);
355
+ const chat = chatSessionId(sessionId);
356
+ await unload?.(chat);
357
+ await purge(chat);
358
+ await storeInitialEvents(persistence, {
359
+ id: chat,
360
+ cwd: project.path,
361
+ agentPreset: AGENT_PRESET
362
+ }, events);
363
+ await controller.rename({
364
+ sessionId: chat,
365
+ title
366
+ });
367
+ }
299
368
  /** Says something in the chat of a session as the player would; it is queued behind what the chat is doing. The chat must exist (`ensure`). */
300
369
  async send(sessionId, text) {
301
370
  await this.services().controller.prompt({
@@ -436,7 +505,10 @@ function folderNameOf(name) {
436
505
  return folder === "" ? "imported world" : folder;
437
506
  }
438
507
  /** The command that starts the import, for a card kept in the world's import folder. */
439
- const importMessage = () => `${IMPORT_COMMAND} ${IMPORT_FOLDER}/card.json Import this card into this newly created world.`;
508
+ function importMessage(language) {
509
+ const tr = translator(languageOf$1(language));
510
+ return `${IMPORT_COMMAND} ${IMPORT_FOLDER}/card.json ${tr("Import this card into this newly created world.")}`;
511
+ }
440
512
  /**
441
513
  * Turns an uploaded card into a new empty world with its authoring chat, and has the chat start importing it on the person's behalf.
442
514
  * @throws {InvalidEditProblems} when the choices cannot be used. @throws {ImportNotFoundError} when the upload is gone.
@@ -463,7 +535,7 @@ async function startImport(deps, id, choices, language) {
463
535
  const chatSessionId = await deps.authoring.ensure(world.id, world.name, deps.chatTitle(world.name), language);
464
536
  let failure;
465
537
  try {
466
- await deps.authoring.send(chatSessionId, importMessage());
538
+ await deps.authoring.send(chatSessionId, importMessage(language));
467
539
  } catch (error) {
468
540
  failure = error instanceof Error ? error.message : String(error);
469
541
  }
@@ -539,7 +611,7 @@ async function uploadPreset(deps, worldId, file, name, language) {
539
611
  const chatSessionId = await deps.authoring.ensure(world.summary.id, world.summary.name, `Authoring ${world.summary.name}`, language);
540
612
  let failure;
541
613
  try {
542
- await deps.authoring.send(chatSessionId, presetMessage());
614
+ await deps.authoring.send(chatSessionId, presetMessage(language));
543
615
  } catch (error) {
544
616
  failure = error instanceof Error ? error.message : String(error);
545
617
  }
@@ -1038,7 +1110,7 @@ var Lorebooks = class {
1038
1110
  const chatSessionId = await this.deps.authoring.ensure(name, title ?? uploaded.name, language);
1039
1111
  let failure;
1040
1112
  try {
1041
- await this.deps.authoring.send(chatSessionId, this.deps.importMessage());
1113
+ await this.deps.authoring.send(chatSessionId, this.deps.importMessage(language));
1042
1114
  } catch (problem) {
1043
1115
  failure = problem instanceof Error ? problem.message : String(problem);
1044
1116
  }
@@ -1130,6 +1202,10 @@ var RoleplayOrchestration = class extends Service {
1130
1202
  current = defaultBoundaries();
1131
1203
  /** Resolves once the records of a chat's recorded rounds are written (or have failed): the writing goes on behind the story, so what needs them waits here. */
1132
1204
  settle = () => Promise.resolve();
1205
+ /** Resolves once the chat's story is free: a turn waits here while the story is being synced. */
1206
+ unitFree = () => Promise.resolve();
1207
+ /** Resolves once nothing of the chat writes to its story in the background (the records, the made-up things, the characters away, the suggestions); there while the orchestration is. */
1208
+ quiesce = () => Promise.resolve();
1133
1209
  /** Stops everything a chat has in flight in the background and waits for it; there while the orchestration is. */
1134
1210
  halt = () => Promise.resolve();
1135
1211
  /** Starts a reflection of a chat's characters on what they have taken in, in the background; there while the orchestration is. */
@@ -1695,7 +1771,7 @@ var Presets = class {
1695
1771
  const chatSessionId = await this.deps.authoring.ensure(name, title ?? uploaded.name, language);
1696
1772
  let failure;
1697
1773
  try {
1698
- await this.deps.authoring.send(chatSessionId, this.deps.importMessage());
1774
+ await this.deps.authoring.send(chatSessionId, this.deps.importMessage(language));
1699
1775
  } catch (problem) {
1700
1776
  failure = problem instanceof Error ? problem.message : String(problem);
1701
1777
  }
@@ -1734,7 +1810,7 @@ var Presets = class {
1734
1810
  //#endregion
1735
1811
  //#region src/host/presets/routes.ts
1736
1812
  /** Not found is 404 and a request the library refuses is 422 with its reason; anything else is a fault. */
1737
- function failure$1(res, error) {
1813
+ function failure$2(res, error) {
1738
1814
  if (error instanceof ImportNotFoundError) return sendError(res, 404, error.message);
1739
1815
  if (error instanceof SessionNotFoundError || error instanceof WorldNotFoundError) return sendError(res, 404, error.message);
1740
1816
  if (error instanceof PresetBusyError) return sendError(res, 409, error.message);
@@ -1753,7 +1829,7 @@ function presetRoutes(api) {
1753
1829
  try {
1754
1830
  sendJson(res, 200, api.list());
1755
1831
  } catch (error) {
1756
- failure$1(res, error);
1832
+ failure$2(res, error);
1757
1833
  }
1758
1834
  },
1759
1835
  POST: async (req, res) => {
@@ -1772,7 +1848,7 @@ function presetRoutes(api) {
1772
1848
  sampling: given
1773
1849
  }));
1774
1850
  } catch (error) {
1775
- failure$1(res, error);
1851
+ failure$2(res, error);
1776
1852
  }
1777
1853
  }
1778
1854
  },
@@ -1781,7 +1857,7 @@ function presetRoutes(api) {
1781
1857
  try {
1782
1858
  sendJson(res, 200, api.read(url.searchParams.get("name") ?? ""));
1783
1859
  } catch (error) {
1784
- failure$1(res, error);
1860
+ failure$2(res, error);
1785
1861
  }
1786
1862
  },
1787
1863
  PUT: async (req, res, url) => {
@@ -1796,7 +1872,7 @@ function presetRoutes(api) {
1796
1872
  ...given === void 0 ? {} : { sampling: given }
1797
1873
  }));
1798
1874
  } catch (error) {
1799
- failure$1(res, error);
1875
+ failure$2(res, error);
1800
1876
  }
1801
1877
  },
1802
1878
  DELETE: async (_req, res, url) => {
@@ -1805,7 +1881,7 @@ function presetRoutes(api) {
1805
1881
  res.statusCode = 204;
1806
1882
  res.end();
1807
1883
  } catch (error) {
1808
- failure$1(res, error);
1884
+ failure$2(res, error);
1809
1885
  }
1810
1886
  }
1811
1887
  },
@@ -1813,7 +1889,7 @@ function presetRoutes(api) {
1813
1889
  try {
1814
1890
  sendJson(res, 200, api.rulesPrompts(url.searchParams.get("name") ?? "", languageOf(url.searchParams.get("language") ?? "")));
1815
1891
  } catch (error) {
1816
- failure$1(res, error);
1892
+ failure$2(res, error);
1817
1893
  }
1818
1894
  } },
1819
1895
  "/presets/prompt": {
@@ -1821,7 +1897,7 @@ function presetRoutes(api) {
1821
1897
  try {
1822
1898
  sendJson(res, 200, { text: api.promptText(url.searchParams.get("name") ?? "", languageOf(url.searchParams.get("language") ?? ""), url.searchParams.get("id") ?? "") });
1823
1899
  } catch (error) {
1824
- failure$1(res, error);
1900
+ failure$2(res, error);
1825
1901
  }
1826
1902
  },
1827
1903
  PUT: async (req, res, url) => {
@@ -1830,14 +1906,14 @@ function presetRoutes(api) {
1830
1906
  try {
1831
1907
  sendJson(res, 200, await api.writePrompt(url.searchParams.get("name") ?? "", languageOf(url.searchParams.get("language") ?? ""), url.searchParams.get("id") ?? "", text));
1832
1908
  } catch (error) {
1833
- failure$1(res, error);
1909
+ failure$2(res, error);
1834
1910
  }
1835
1911
  },
1836
1912
  DELETE: async (_req, res, url) => {
1837
1913
  try {
1838
1914
  sendJson(res, 200, await api.writePrompt(url.searchParams.get("name") ?? "", languageOf(url.searchParams.get("language") ?? ""), url.searchParams.get("id") ?? "", void 0));
1839
1915
  } catch (error) {
1840
- failure$1(res, error);
1916
+ failure$2(res, error);
1841
1917
  }
1842
1918
  }
1843
1919
  },
@@ -1847,7 +1923,7 @@ function presetRoutes(api) {
1847
1923
  try {
1848
1924
  sendJson(res, 200, await api.author(url.searchParams.get("name") ?? "", language === void 0 ? void 0 : languageOf(language)));
1849
1925
  } catch (error) {
1850
- failure$1(res, error);
1926
+ failure$2(res, error);
1851
1927
  }
1852
1928
  } },
1853
1929
  "/presets/import/verbatim": { POST: async (req, res) => {
@@ -1863,7 +1939,7 @@ function presetRoutes(api) {
1863
1939
  ...title === void 0 ? {} : { title }
1864
1940
  }));
1865
1941
  } catch (error) {
1866
- failure$1(res, error);
1942
+ failure$2(res, error);
1867
1943
  }
1868
1944
  } },
1869
1945
  "/presets/import/staged": { POST: async (req, res) => {
@@ -1884,7 +1960,7 @@ function presetRoutes(api) {
1884
1960
  ...language === void 0 ? {} : { language: languageOf(language) }
1885
1961
  }));
1886
1962
  } catch (error) {
1887
- failure$1(res, error);
1963
+ failure$2(res, error);
1888
1964
  }
1889
1965
  } },
1890
1966
  "/presets/import": { POST: async (req, res, url) => {
@@ -1892,7 +1968,7 @@ function presetRoutes(api) {
1892
1968
  try {
1893
1969
  sendJson(res, 200, await api.importFile(url.searchParams.get("name") ?? "", await readBinaryBody(req, api.maxUploadBytes), language === null ? void 0 : languageOf(language)));
1894
1970
  } catch (error) {
1895
- failure$1(res, error);
1971
+ failure$2(res, error);
1896
1972
  }
1897
1973
  } },
1898
1974
  "/sessions/preset": {
@@ -1900,7 +1976,7 @@ function presetRoutes(api) {
1900
1976
  try {
1901
1977
  sendJson(res, 200, api.status(url.searchParams.get("id") ?? ""));
1902
1978
  } catch (error) {
1903
- failure$1(res, error);
1979
+ failure$2(res, error);
1904
1980
  }
1905
1981
  },
1906
1982
  POST: async (req, res, url) => {
@@ -1909,7 +1985,7 @@ function presetRoutes(api) {
1909
1985
  try {
1910
1986
  sendJson(res, 200, api.apply(url.searchParams.get("id") ?? "", typeof name === "string" ? name : void 0));
1911
1987
  } catch (error) {
1912
- failure$1(res, error);
1988
+ failure$2(res, error);
1913
1989
  }
1914
1990
  }
1915
1991
  }
@@ -1918,7 +1994,7 @@ function presetRoutes(api) {
1918
1994
  //#endregion
1919
1995
  //#region src/host/lorebooks/routes.ts
1920
1996
  /** Not found is 404, a story in the middle of a turn 409, and a request the library refuses 422 with its reason; anything else is a fault. */
1921
- function failure(res, error) {
1997
+ function failure$1(res, error) {
1922
1998
  if (error instanceof ImportNotFoundError) return sendError(res, 404, error.message);
1923
1999
  if (error instanceof SessionNotFoundError) return sendError(res, 404, error.message);
1924
2000
  if (error instanceof LorebookBusyError) return sendError(res, 409, error.message);
@@ -1953,7 +2029,7 @@ function lorebookRoutes(api) {
1953
2029
  try {
1954
2030
  sendJson(res, 200, await api.status(url.searchParams.get("id") ?? ""));
1955
2031
  } catch (error) {
1956
- failure(res, error);
2032
+ failure$1(res, error);
1957
2033
  }
1958
2034
  },
1959
2035
  POST: async (req, res, url) => {
@@ -1962,14 +2038,14 @@ function lorebookRoutes(api) {
1962
2038
  try {
1963
2039
  sendJson(res, 200, await api.addToStory(url.searchParams.get("id") ?? "", name));
1964
2040
  } catch (error) {
1965
- failure(res, error);
2041
+ failure$1(res, error);
1966
2042
  }
1967
2043
  },
1968
2044
  DELETE: async (_req, res, url) => {
1969
2045
  try {
1970
2046
  sendJson(res, 200, api.removeFromStory(url.searchParams.get("id") ?? "", url.searchParams.get("name") ?? ""));
1971
2047
  } catch (error) {
1972
- failure(res, error);
2048
+ failure$1(res, error);
1973
2049
  }
1974
2050
  }
1975
2051
  },
@@ -1978,7 +2054,7 @@ function lorebookRoutes(api) {
1978
2054
  try {
1979
2055
  sendJson(res, 200, await api.list());
1980
2056
  } catch (error) {
1981
- failure(res, error);
2057
+ failure$1(res, error);
1982
2058
  }
1983
2059
  },
1984
2060
  POST: async (req, res) => {
@@ -1992,7 +2068,7 @@ function lorebookRoutes(api) {
1992
2068
  ...description === void 0 ? {} : { description }
1993
2069
  }));
1994
2070
  } catch (error) {
1995
- failure(res, error);
2071
+ failure$1(res, error);
1996
2072
  }
1997
2073
  }
1998
2074
  },
@@ -2001,7 +2077,7 @@ function lorebookRoutes(api) {
2001
2077
  try {
2002
2078
  sendJson(res, 200, await api.read(url.searchParams.get("name") ?? ""));
2003
2079
  } catch (error) {
2004
- failure(res, error);
2080
+ failure$1(res, error);
2005
2081
  }
2006
2082
  },
2007
2083
  PUT: async (req, res, url) => {
@@ -2013,7 +2089,7 @@ function lorebookRoutes(api) {
2013
2089
  ...description === void 0 ? {} : { description }
2014
2090
  }));
2015
2091
  } catch (error) {
2016
- failure(res, error);
2092
+ failure$1(res, error);
2017
2093
  }
2018
2094
  },
2019
2095
  DELETE: async (_req, res, url) => {
@@ -2022,7 +2098,7 @@ function lorebookRoutes(api) {
2022
2098
  res.statusCode = 204;
2023
2099
  res.end();
2024
2100
  } catch (error) {
2025
- failure(res, error);
2101
+ failure$1(res, error);
2026
2102
  }
2027
2103
  }
2028
2104
  },
@@ -2033,14 +2109,14 @@ function lorebookRoutes(api) {
2033
2109
  try {
2034
2110
  sendJson(res, 200, await api.writeEntry(url.searchParams.get("name") ?? "", entry));
2035
2111
  } catch (error) {
2036
- failure(res, error);
2112
+ failure$1(res, error);
2037
2113
  }
2038
2114
  },
2039
2115
  DELETE: async (_req, res, url) => {
2040
2116
  try {
2041
2117
  sendJson(res, 200, await api.removeEntry(url.searchParams.get("name") ?? "", url.searchParams.get("id") ?? ""));
2042
2118
  } catch (error) {
2043
- failure(res, error);
2119
+ failure$1(res, error);
2044
2120
  }
2045
2121
  }
2046
2122
  },
@@ -2050,7 +2126,7 @@ function lorebookRoutes(api) {
2050
2126
  try {
2051
2127
  sendJson(res, 200, await api.author(url.searchParams.get("name") ?? "", language === void 0 ? void 0 : languageOf(language)));
2052
2128
  } catch (error) {
2053
- failure(res, error);
2129
+ failure$1(res, error);
2054
2130
  }
2055
2131
  } },
2056
2132
  "/lorebooks/import/verbatim": { POST: async (req, res) => {
@@ -2068,7 +2144,7 @@ function lorebookRoutes(api) {
2068
2144
  ...title === void 0 ? {} : { title }
2069
2145
  }));
2070
2146
  } catch (error) {
2071
- failure(res, error);
2147
+ failure$1(res, error);
2072
2148
  }
2073
2149
  } },
2074
2150
  "/lorebooks/import/staged": { POST: async (req, res) => {
@@ -2089,7 +2165,7 @@ function lorebookRoutes(api) {
2089
2165
  ...language === void 0 ? {} : { language: languageOf(language) }
2090
2166
  }));
2091
2167
  } catch (error) {
2092
- failure(res, error);
2168
+ failure$1(res, error);
2093
2169
  }
2094
2170
  } },
2095
2171
  "/lorebooks/import": { POST: async (req, res, url) => {
@@ -2097,11 +2173,116 @@ function lorebookRoutes(api) {
2097
2173
  try {
2098
2174
  sendJson(res, 200, await api.importFile(url.searchParams.get("name") ?? "", await readBinaryBody(req, api.maxUploadBytes), language === null ? void 0 : languageOf(language)));
2099
2175
  } catch (error) {
2100
- failure(res, error);
2176
+ failure$1(res, error);
2101
2177
  }
2102
2178
  } }
2103
2179
  };
2104
2180
  }
2181
+ //#endregion
2182
+ //#region src/host/sync/errors.ts
2183
+ /** A run, or a choice, cannot be made now. */
2184
+ var SyncRefusedError = class extends Error {};
2185
+ /** rclone could not be started or reported a failure. */
2186
+ var RcloneError = class extends Error {
2187
+ code;
2188
+ constructor(message, code) {
2189
+ super(message);
2190
+ this.code = code;
2191
+ }
2192
+ };
2193
+ /** The unit is being synced: nothing may write to it or read from it until the run is over. */
2194
+ var UnitLockedError = class extends Error {};
2195
+ //#endregion
2196
+ //#region src/shared/catalogue.ts
2197
+ /** What a credential reference is: letters, digits and `_`, not starting with a digit (DSH's grammar). */
2198
+ const CREDENTIAL_REF = /^[A-Za-z_][A-Za-z0-9_]*$/;
2199
+ /** The credential a catalogue's key is stored under when its entry names none. Names with other characters get a short checksum so that two of them never share a key. */
2200
+ function catalogueKeyRef(name) {
2201
+ const trimmed = name.trim();
2202
+ const slug = trimCharacters(trimmed.normalize("NFKD").replace(/[^A-Za-z0-9]+/g, "_"), "_").toUpperCase();
2203
+ let checksum = 2166136261;
2204
+ for (const unit of trimmed) checksum = Math.imul(checksum ^ unit.codePointAt(0), 16777619) >>> 0;
2205
+ return `ROLEPLAY_CATALOGUE_${slug !== "" && /^[A-Za-z0-9 _.-]+$/.test(trimmed) ? slug : `${slug === "" ? "X" : slug}_${checksum.toString(16).toUpperCase()}`}_API_KEY`;
2206
+ }
2207
+ const CATALOGUE_CARD_TYPE = "sillytavern/character";
2208
+ const CATALOGUE_PRESET_TYPE = "sillytavern/preset";
2209
+ const CATALOGUE_LOREBOOK_TYPE = "sillytavern/lorebook";
2210
+ const CATALOGUE_WORLD_TYPE = "dsh-agentic-roleplay/world";
2211
+ //#endregion
2212
+ //#region src/host/sync/routes.ts
2213
+ function failure(res, error) {
2214
+ if (error instanceof UnitLockedError) return sendError(res, 423, error.message);
2215
+ if (error instanceof SyncRefusedError) return sendError(res, 409, error.message);
2216
+ if (error instanceof RcloneError) return sendError(res, 502, error.message);
2217
+ throw error;
2218
+ }
2219
+ function syncRoutes(api) {
2220
+ return {
2221
+ "/sync/status": { GET: async (_req, res) => {
2222
+ try {
2223
+ sendJson(res, 200, await api.status());
2224
+ } catch (error) {
2225
+ failure(res, error);
2226
+ }
2227
+ } },
2228
+ "/sync/run": { POST: async (req, res) => {
2229
+ const only = (await readJsonBody(req))["units"];
2230
+ if (only !== void 0 && (!Array.isArray(only) || only.some((entry) => typeof entry !== "string"))) return sendError(res, 400, "\"units\" must be a list of unit ids");
2231
+ try {
2232
+ sendJson(res, 200, await api.run(only));
2233
+ } catch (error) {
2234
+ failure(res, error);
2235
+ }
2236
+ } },
2237
+ "/sync/resolve": { POST: async (req, res) => {
2238
+ const { unit, side } = await readJsonBody(req);
2239
+ if (typeof unit !== "string" || side !== "local" && side !== "remote") return sendError(res, 400, "\"unit\" must be text and \"side\" local or remote");
2240
+ try {
2241
+ sendJson(res, 200, await api.resolve(unit, side) ?? null);
2242
+ } catch (error) {
2243
+ failure(res, error);
2244
+ }
2245
+ } },
2246
+ "/sync/forget": { POST: async (req, res) => {
2247
+ const { unit } = await readJsonBody(req);
2248
+ if (typeof unit !== "string") return sendError(res, 400, "\"unit\" must be text");
2249
+ try {
2250
+ api.forget(unit);
2251
+ sendJson(res, 200, {});
2252
+ } catch (error) {
2253
+ failure(res, error);
2254
+ }
2255
+ } },
2256
+ "/sync/restore": { POST: async (req, res) => {
2257
+ const { unit } = await readJsonBody(req);
2258
+ if (typeof unit !== "string") return sendError(res, 400, "\"unit\" must be text");
2259
+ try {
2260
+ api.restore(unit);
2261
+ sendJson(res, 200, {});
2262
+ } catch (error) {
2263
+ failure(res, error);
2264
+ }
2265
+ } },
2266
+ "/sync/password": {
2267
+ GET: async (_req, res, url) => {
2268
+ const reference = url.searchParams.get("ref") ?? void 0;
2269
+ if (reference !== void 0 && !CREDENTIAL_REF.test(reference)) return sendError(res, 400, "\"ref\" must be a credential name");
2270
+ sendJson(res, 200, await api.password.describe(reference));
2271
+ },
2272
+ PUT: async (req, res) => {
2273
+ const { ref, value } = await readJsonBody(req);
2274
+ if (typeof value !== "string") return sendError(res, 400, "\"value\" must be text");
2275
+ if (ref !== void 0 && (typeof ref !== "string" || !CREDENTIAL_REF.test(ref))) return sendError(res, 400, "\"ref\" must be a credential name");
2276
+ try {
2277
+ sendJson(res, 200, await api.password.keep(value, ref));
2278
+ } catch (error) {
2279
+ if (error instanceof SyncRefusedError) return sendError(res, 409, error.message);
2280
+ throw error;
2281
+ }
2282
+ }
2283
+ }
2284
+ };
2285
+ }
2105
2286
  /** The headers of every file of a pack. */
2106
2287
  const PACK_HEADERS = {
2107
2288
  "content-security-policy": [
@@ -2900,7 +3081,7 @@ function languageField(value) {
2900
3081
  throw new InvalidEditProblems([error instanceof Error ? error.message : String(error)]);
2901
3082
  }
2902
3083
  }
2903
- function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld, translation, worldCharacters, importer, avatars, worldFiles, worldEditor, sessions, packs, presets, lorebooks, connection, speech, externalTools }) {
3084
+ function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld, translation, worldCharacters, importer, avatars, worldFiles, worldEditor, sessions, packs, presets, lorebooks, sync, locked, anyLocked, connection, speech, externalTools }) {
2904
3085
  const routes = {
2905
3086
  "/speech/capabilities": { GET: async (_req, res) => {
2906
3087
  sendJson(res, 200, { streaming: speech.streaming() });
@@ -3868,7 +4049,7 @@ function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld
3868
4049
  } },
3869
4050
  "/sessions/open": { POST: async (_req, res, url) => {
3870
4051
  try {
3871
- sendJson(res, 200, await sessions.open(url.searchParams.get("id") ?? ""));
4052
+ sendJson(res, 200, await sessions.open(url.searchParams.get("id") ?? "", url.searchParams.get("rebuild") === "1"));
3872
4053
  } catch (error) {
3873
4054
  if (error instanceof SessionNotFoundError) return sendError(res, 404, error.message);
3874
4055
  if (error instanceof SessionArchivedError) return sendError(res, 409, error.message);
@@ -4215,8 +4396,9 @@ function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld
4215
4396
  } },
4216
4397
  "/sessions/item": { DELETE: async (_req, res, url) => {
4217
4398
  try {
4218
- await sessions.delete(url.searchParams.get("id") ?? "");
4399
+ await sessions.delete(url.searchParams.get("id") ?? "", url.searchParams.get("everywhere") === "true");
4219
4400
  } catch (error) {
4401
+ if (error instanceof SyncRefusedError || error instanceof RcloneError) return failure(res, error);
4220
4402
  if (error instanceof SessionNotFoundError) return sendError(res, 404, error.message);
4221
4403
  if (error instanceof AgentUnavailableError) return sendError(res, 503, error.message);
4222
4404
  throw error;
@@ -4227,6 +4409,7 @@ function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld
4227
4409
  ...interfaceRoutes(packs),
4228
4410
  ...presetRoutes(presets),
4229
4411
  ...lorebookRoutes(lorebooks),
4412
+ ...syncRoutes(sync),
4230
4413
  "/worlds/cover": { GET: async (_req, res, url) => {
4231
4414
  const cover = await worlds.cover(url.searchParams.get("id") ?? "");
4232
4415
  if (cover === void 0) return sendError(res, 404, "no cover");
@@ -4259,6 +4442,10 @@ function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld
4259
4442
  }
4260
4443
  const methods = routes[path];
4261
4444
  if (methods === void 0) return sendError(res, 404, "not found");
4445
+ if (anyLocked?.() === true && !path.startsWith("/sync/")) {
4446
+ const unit = await locked?.(url);
4447
+ if (unit !== void 0) return sendError(res, 423, `"${unit}" is being synced; try again when it is done`);
4448
+ }
4262
4449
  const route = methods[req.method ?? ""];
4263
4450
  if (route === void 0) {
4264
4451
  res.setHeader("allow", Object.keys(methods).join(", "));
@@ -5495,6 +5682,70 @@ async function addAvatarsToWorld(avatars, world, keys, locations) {
5495
5682
  }
5496
5683
  return brought.map((entry) => entry.id);
5497
5684
  }
5685
+ //#endregion
5686
+ //#region src/host/agent/history.ts
5687
+ /** The unknown seq of a turn that is not in the log. */
5688
+ const NOT_IN_LOG = -1;
5689
+ /**
5690
+ * The log of a chat made again from what the session folder keeps: the opening, then each turn from the first to the head as the player
5691
+ * said it and as it was delivered. The turns keep their numbers, so the story's own records (written under those numbers) and the next
5692
+ * turn agree with the log. The model's tool traffic is not kept; the story's state is in the database, not in the conversation.
5693
+ */
5694
+ function rebuildChat(opening, timeline) {
5695
+ const session = Session.create(SessionId("rebuilt"));
5696
+ appendNarratedTurn(session, 1, {
5697
+ segments: [...opening],
5698
+ repaired: false
5699
+ });
5700
+ const seqs = /* @__PURE__ */ new Map();
5701
+ let last = 1;
5702
+ for (const node of pathTo(timeline, timeline.head)) {
5703
+ if (node.output === void 0) continue;
5704
+ for (let skipped = last + 1; skipped < node.turn; skipped += 1) appendEmptyTurn(session, skipped);
5705
+ seqs.set(node.id, appendNarratedTurn(session, node.turn, node.output, node.player));
5706
+ last = node.turn;
5707
+ }
5708
+ return {
5709
+ events: session.snapshotEvents(),
5710
+ seqs
5711
+ };
5712
+ }
5713
+ /** The timeline with its turns pointed at the rebuilt log; a turn the log does not hold is marked as not in it. */
5714
+ function repointTimeline(timeline, seqs) {
5715
+ return {
5716
+ ...timeline,
5717
+ nodes: timeline.nodes.map(({ startSeq: _start, endSeq: _end, noticeSeq: _notice, ...node }) => {
5718
+ const at = seqs.get(node.id);
5719
+ return at === void 0 ? {
5720
+ ...node,
5721
+ userSeq: NOT_IN_LOG
5722
+ } : {
5723
+ ...node,
5724
+ userSeq: at.userSeq ?? NOT_IN_LOG,
5725
+ startSeq: at.startSeq,
5726
+ endSeq: at.endSeq
5727
+ };
5728
+ })
5729
+ };
5730
+ }
5731
+ const OPENING_FILE = "opening.json";
5732
+ /** What a story opens with when its world gives no opening: a chat with nothing in it is DSH's start screen, which has none of the story's tabs. */
5733
+ const DEFAULT_OPENING = "The story begins.";
5734
+ /** Keeps the opening in the session folder: it is not in the database, and a chat made again must begin with it. */
5735
+ function writeOpening(folder, opening) {
5736
+ const target = join(folder, OPENING_FILE);
5737
+ writeFileSync(`${target}.writing`, JSON.stringify(opening));
5738
+ renameSync(`${target}.writing`, target);
5739
+ }
5740
+ /** The opening a session began with, or nothing for a session made before it was kept. */
5741
+ function readOpening(folder) {
5742
+ try {
5743
+ return JSON.parse(readFileSync(join(folder, OPENING_FILE), "utf8"));
5744
+ } catch (error) {
5745
+ if (error.code === "ENOENT") return void 0;
5746
+ throw error;
5747
+ }
5748
+ }
5498
5749
  /**
5499
5750
  * Start a new session for a world: create its folder and database and fill it
5500
5751
  * from the world. Either the whole session exists afterwards or nothing does.
@@ -5647,6 +5898,7 @@ async function startSession(deps, request) {
5647
5898
  if (packProblems.length > 0) throw new InvalidWorldError(`the interface packs of "${world.summary.name}" have ${packProblems.length} problem(s)`, packProblems);
5648
5899
  writeSettings(sessionFolder(sessionsDir, id), settings);
5649
5900
  addToNote(session, "world", 1, [...unavailable, ...worldFindings(session)]);
5901
+ if (opening !== void 0) writeOpening(folder, opening);
5650
5902
  chatSessionId = await deps.agent.ensure(id, name, opening);
5651
5903
  } catch (error) {
5652
5904
  deps.state.deleteSession(sessionsDir, id);
@@ -5686,8 +5938,6 @@ function presetFor(deps, asked, suggested) {
5686
5938
  return { unavailable: [`The world suggests the preset "${name}", which cannot be used here (${reason}), so the story is told in the default style.`] };
5687
5939
  }
5688
5940
  }
5689
- /** What a story opens with when its world gives no opening: a chat with nothing in it is DSH's start screen, which has none of the story's tabs. */
5690
- const DEFAULT_OPENING = "The story begins.";
5691
5941
  /** The opening of the world with each speaker's name, or the reason it cannot be used. */
5692
5942
  function openingOf(world, characters) {
5693
5943
  if (world.opening === void 0) return [{
@@ -5971,14 +6221,30 @@ function decisionsOfSession(deps, id) {
5971
6221
  //#endregion
5972
6222
  //#region src/host/sessions/open-session.ts
5973
6223
  /** Make sure a session has its DSH chat, so that it can be opened. */
5974
- async function openSession(deps, id) {
6224
+ async function openSession(deps, id, options = {}) {
5975
6225
  if (!isSessionId(id) || !existsSync(sessionDatabase(deps.sessionsDir(), id))) throw new SessionNotFoundError(`no session "${id}"`);
5976
6226
  if (readStatus(sessionFolder(deps.sessionsDir(), id)).archivedAt !== void 0) throw new SessionArchivedError(`the session "${id}" is put away: bring it back first`);
5977
6227
  const { name } = deps.state.openSession(deps.sessionsDir(), id).metadata.get();
5978
- return {
6228
+ const folder = sessionFolder(deps.sessionsDir(), id);
6229
+ const present = await deps.agent.has(id);
6230
+ const again = !present || options.rebuild === true && deps.replaceChat !== void 0;
6231
+ const timeline = again ? readTimeline(folder) : void 0;
6232
+ const opening = again ? readOpening(folder) ?? (timeline.nodes.length > 0 ? [{
6233
+ type: "narration",
6234
+ text: "The story begins."
6235
+ }] : void 0) : void 0;
6236
+ if (opening === void 0 || timeline === void 0) return {
5979
6237
  id,
5980
6238
  chatSessionId: await deps.agent.ensure(id, name)
5981
6239
  };
6240
+ const rebuilt = rebuildChat(opening, timeline);
6241
+ const chatSessionId$1 = present ? chatSessionId(id) : await deps.agent.ensure(id, name, opening, rebuilt.events);
6242
+ if (present) await deps.replaceChat(id, name, rebuilt.events);
6243
+ writeTimeline(folder, repointTimeline(timeline, rebuilt.seqs));
6244
+ return {
6245
+ id,
6246
+ chatSessionId: chatSessionId$1
6247
+ };
5982
6248
  }
5983
6249
  //#endregion
5984
6250
  //#region src/host/sessions/list-sessions.ts
@@ -6369,22 +6635,6 @@ var WorldCatalog = class extends Service {
6369
6635
  }
6370
6636
  };
6371
6637
  //#endregion
6372
- //#region src/shared/catalogue.ts
6373
- /** What a credential reference is: letters, digits and `_`, not starting with a digit (DSH's grammar). */
6374
- const CREDENTIAL_REF = /^[A-Za-z_][A-Za-z0-9_]*$/;
6375
- /** The credential a catalogue's key is stored under when its entry names none. Names with other characters get a short checksum so that two of them never share a key. */
6376
- function catalogueKeyRef(name) {
6377
- const trimmed = name.trim();
6378
- const slug = trimCharacters(trimmed.normalize("NFKD").replace(/[^A-Za-z0-9]+/g, "_"), "_").toUpperCase();
6379
- let checksum = 2166136261;
6380
- for (const unit of trimmed) checksum = Math.imul(checksum ^ unit.codePointAt(0), 16777619) >>> 0;
6381
- return `ROLEPLAY_CATALOGUE_${slug !== "" && /^[A-Za-z0-9 _.-]+$/.test(trimmed) ? slug : `${slug === "" ? "X" : slug}_${checksum.toString(16).toUpperCase()}`}_API_KEY`;
6382
- }
6383
- const CATALOGUE_CARD_TYPE = "sillytavern/character";
6384
- const CATALOGUE_PRESET_TYPE = "sillytavern/preset";
6385
- const CATALOGUE_LOREBOOK_TYPE = "sillytavern/lorebook";
6386
- const CATALOGUE_WORLD_TYPE = "dsh-agentic-roleplay/world";
6387
- //#endregion
6388
6638
  //#region src/host/catalogue/origin.ts
6389
6639
  const ORIGIN_FILE = ".catalogue.json";
6390
6640
  const MAX_LISTED_CHANGES = 12;
@@ -6845,7 +7095,7 @@ const object = (value, what) => {
6845
7095
  };
6846
7096
  const text = (value) => typeof value === "string" ? value : "";
6847
7097
  const count = (value) => typeof value === "number" && Number.isFinite(value) ? value : 0;
6848
- const kindOf = (type) => type === "sillytavern/character" ? "card" : type === "sillytavern/preset" ? "preset" : type === "sillytavern/lorebook" ? "lorebook" : "world";
7098
+ const kindOf$1 = (type) => type === "sillytavern/character" ? "card" : type === "sillytavern/preset" ? "preset" : type === "sillytavern/lorebook" ? "lorebook" : "world";
6849
7099
  const TYPES = {
6850
7100
  world: CATALOGUE_WORLD_TYPE,
6851
7101
  card: CATALOGUE_CARD_TYPE,
@@ -6860,7 +7110,7 @@ function worldOf(value) {
6860
7110
  const visibility = text(metadata["visibility"]);
6861
7111
  return {
6862
7112
  id,
6863
- kind: kindOf(text(item["resourceType"])),
7113
+ kind: kindOf$1(text(item["resourceType"])),
6864
7114
  name: text(metadata["name"]),
6865
7115
  description: text(metadata["description"]),
6866
7116
  tags: Array.isArray(metadata["tags"]) ? metadata["tags"].filter((tag) => typeof tag === "string") : [],
@@ -7359,6 +7609,915 @@ var Sources = class {
7359
7609
  }
7360
7610
  };
7361
7611
  //#endregion
7612
+ //#region src/shared/sync.ts
7613
+ const DEFAULT_SYNC = {
7614
+ remote: "",
7615
+ rclone: "",
7616
+ configFile: "",
7617
+ passwordRef: "",
7618
+ intervalMinutes: 0
7619
+ };
7620
+ //#endregion
7621
+ //#region src/host/sync/gate.ts
7622
+ /**
7623
+ * Which units are being synced. A locked unit is neither written nor read until it is released, so rclone never sees a half-written
7624
+ * unit and the app never sees a half-downloaded one.
7625
+ */
7626
+ var UnitGate = class {
7627
+ held = /* @__PURE__ */ new Map();
7628
+ isLocked(unit) {
7629
+ return this.held.has(unit);
7630
+ }
7631
+ get any() {
7632
+ return this.held.size > 0;
7633
+ }
7634
+ /** @throws {UnitLockedError} when the unit is being synced. */
7635
+ assertFree(unit) {
7636
+ if (this.held.has(unit)) throw new UnitLockedError(`"${unit}" is being synced; try again when it is done`);
7637
+ }
7638
+ /** Resolves once the unit is free, at once when it is. */
7639
+ async whenFree(unit) {
7640
+ while (this.held.has(unit)) await this.held.get(unit);
7641
+ }
7642
+ /** Locks a unit while `work` runs. @throws {UnitLockedError} when it is locked already. */
7643
+ async hold(unit, work) {
7644
+ this.assertFree(unit);
7645
+ let release;
7646
+ this.held.set(unit, new Promise((resolve) => {
7647
+ release = resolve;
7648
+ }));
7649
+ try {
7650
+ return await work();
7651
+ } finally {
7652
+ this.held.delete(unit);
7653
+ release();
7654
+ }
7655
+ }
7656
+ };
7657
+ //#endregion
7658
+ //#region src/host/sync/rclone.ts
7659
+ /** Runs the rclone the person named, with their configuration file, as a child of this process. */
7660
+ function spawnRclone(setup) {
7661
+ return { run: (args, options = {}) => new Promise((resolve, reject) => {
7662
+ const env = {
7663
+ ...process.env,
7664
+ ...setup.password === void 0 ? {} : { RCLONE_CONFIG_PASS: setup.password }
7665
+ };
7666
+ const child = spawn(setup.binary === "" ? "rclone" : setup.binary, [
7667
+ "--config",
7668
+ setup.configFile,
7669
+ "--ask-password=false",
7670
+ ...args
7671
+ ], {
7672
+ env,
7673
+ stdio: [
7674
+ "pipe",
7675
+ "pipe",
7676
+ "pipe"
7677
+ ],
7678
+ ...options.signal === void 0 ? {} : { signal: options.signal }
7679
+ });
7680
+ let stdout = "";
7681
+ let stderr = "";
7682
+ child.stdout.on("data", (chunk) => {
7683
+ stdout += chunk.toString();
7684
+ });
7685
+ child.stderr.on("data", (chunk) => {
7686
+ stderr += chunk.toString();
7687
+ });
7688
+ child.on("error", (error) => {
7689
+ reject(new RcloneError(error.code === "ENOENT" ? `rclone was not found (${setup.binary === "" ? "not on the PATH" : setup.binary}): install it or give its path in the sync settings` : error.message, void 0));
7690
+ });
7691
+ child.on("close", (code) => {
7692
+ resolve({
7693
+ code: code ?? -1,
7694
+ stdout,
7695
+ stderr
7696
+ });
7697
+ });
7698
+ child.stdin.on("error", () => void 0);
7699
+ child.stdin.end(options.input ?? "");
7700
+ }) };
7701
+ }
7702
+ /** What rclone said went wrong: its last error line, without the time and the colours, cut short. */
7703
+ function why(result) {
7704
+ const lines = (result.stderr || result.stdout).split("\n").map((line) => line.replace(/\u001b\[[0-9;]*m/g, "").replace(/^\d{4}\/\d\d\/\d\d \d\d:\d\d:\d\d /, "").trim()).filter((line) => line !== "");
7705
+ const line = lines.findLast((entry) => /^(ERROR|NOTICE: Failed|CRITICAL)/.test(entry)) ?? lines.at(-1) ?? `rclone stopped with code ${result.code}`;
7706
+ return line.length > 300 ? `${line.slice(0, 300)}…` : line;
7707
+ }
7708
+ //#endregion
7709
+ //#region src/host/sync/state.ts
7710
+ const syncFolder = (root) => join(root, ".sync");
7711
+ /** What this device remembers about its syncing; it lives beside the root's content but is never a unit, so it is never synced. */
7712
+ function readStore(root) {
7713
+ try {
7714
+ const parsed = JSON.parse(readFileSync(join(syncFolder(root), "state.json"), "utf8"));
7715
+ return {
7716
+ device: parsed.device ?? randomUUID(),
7717
+ ...parsed.lastRun === void 0 ? {} : { lastRun: parsed.lastRun },
7718
+ units: parsed.units ?? {},
7719
+ ignored: parsed.ignored ?? []
7720
+ };
7721
+ } catch (error) {
7722
+ if (error.code !== "ENOENT") throw error;
7723
+ return {
7724
+ device: randomUUID(),
7725
+ units: {},
7726
+ ignored: []
7727
+ };
7728
+ }
7729
+ }
7730
+ function writeStore(root, store) {
7731
+ const folder = syncFolder(root);
7732
+ mkdirSync(folder, { recursive: true });
7733
+ const temporary = join(folder, `state.json.${process.pid}.tmp`);
7734
+ writeFileSync(temporary, `${JSON.stringify(store, null, 2)}\n`);
7735
+ renameSync(temporary, join(folder, "state.json"));
7736
+ }
7737
+ //#endregion
7738
+ //#region src/host/sync/units.ts
7739
+ /** Folders of the root that are one unit each, whatever they hold. */
7740
+ const SHARED_UNITS = [
7741
+ "prompts",
7742
+ "presets",
7743
+ "lorebooks",
7744
+ "avatars",
7745
+ "ui"
7746
+ ];
7747
+ const modeOf = (kind) => kind === "session" ? "whole" : "files";
7748
+ function kindOf(unit) {
7749
+ const [top, name, ...rest] = unit.split("/");
7750
+ if (rest.length > 0) return void 0;
7751
+ if (name === void 0) return SHARED_UNITS.includes(top) ? "shared" : void 0;
7752
+ if (top === "worlds" && !name.startsWith(".") && name !== "") return "world";
7753
+ if (top === "sessions" && isSessionId(name)) return "session";
7754
+ }
7755
+ async function folders(directory) {
7756
+ try {
7757
+ return (await readdir(directory, { withFileTypes: true })).filter((entry) => entry.isDirectory() && !entry.name.startsWith(".")).map((entry) => entry.name);
7758
+ } catch (error) {
7759
+ if (error.code === "ENOENT") return [];
7760
+ throw error;
7761
+ }
7762
+ }
7763
+ /** The units the root holds. */
7764
+ async function localUnits(root) {
7765
+ const shared = (await Promise.all(SHARED_UNITS.map(async (name) => await stat(join(root, name)).then((entry) => entry.isDirectory(), () => false) ? name : void 0))).filter((name) => name !== void 0);
7766
+ const worlds = (await folders(join(root, "worlds"))).map((name) => `worlds/${name}`);
7767
+ const sessions = (await folders(join(root, "sessions"))).filter(isSessionId).map((name) => `sessions/${name}`);
7768
+ return [
7769
+ ...shared,
7770
+ ...worlds,
7771
+ ...sessions
7772
+ ];
7773
+ }
7774
+ /** A short digest of what a folder holds (names, sizes and times), to tell whether it changed since it was last synced. */
7775
+ async function fingerprint(directory) {
7776
+ const lines = [];
7777
+ const walk = async (relative) => {
7778
+ for (const entry of await readdir(join(directory, relative), { withFileTypes: true })) {
7779
+ const path = relative === "" ? entry.name : `${relative}/${entry.name}`;
7780
+ if (entry.isDirectory()) await walk(path);
7781
+ else if (entry.isFile()) {
7782
+ const info = await stat(join(directory, path));
7783
+ lines.push(`${path}\t${info.size}\t${Math.trunc(info.mtimeMs)}`);
7784
+ }
7785
+ }
7786
+ };
7787
+ await walk("");
7788
+ return createHash("sha256").update(lines.sort((left, right) => left.localeCompare(right)).join("\n")).digest("hex");
7789
+ }
7790
+ /** The files that two copies of one file left side by side (`name.conflict1` is this device's, `name.conflict2` the remote's). */
7791
+ async function conflictFiles(directory) {
7792
+ const found = [];
7793
+ const walk = async (relative) => {
7794
+ for (const entry of await readdir(join(directory, relative), { withFileTypes: true })) {
7795
+ const path = relative === "" ? entry.name : `${relative}/${entry.name}`;
7796
+ if (entry.isDirectory()) await walk(path);
7797
+ else if (/\.conflict[12]$/.test(entry.name)) found.push(path);
7798
+ }
7799
+ };
7800
+ await walk("");
7801
+ return found.sort((left, right) => left.localeCompare(right));
7802
+ }
7803
+ //#endregion
7804
+ //#region src/host/sync/service.ts
7805
+ const DIVERGED = {
7806
+ none: void 0,
7807
+ first: "both sides have this and it was never synced here",
7808
+ both: "both sides changed it",
7809
+ removed: "removed on this device but still on the remote",
7810
+ deleted: "deleted on another device, and changed here since"
7811
+ };
7812
+ const BISYNC = [
7813
+ "--resilient",
7814
+ "--recover",
7815
+ "--conflict-resolve",
7816
+ "none",
7817
+ "--create-empty-src-dirs",
7818
+ "--max-lock",
7819
+ "5m"
7820
+ ];
7821
+ /** The longest name rclone's file system allows for a file bisync keeps per pair of paths, which is made from both paths. */
7822
+ const BISYNC_NAME_LIMIT = 230;
7823
+ function expand(path) {
7824
+ const value = path.trim();
7825
+ return value === "~" ? homedir() : value.startsWith("~/") ? join(homedir(), value.slice(2)) : value;
7826
+ }
7827
+ /**
7828
+ * Syncs the roleplay root with a remote through rclone, one unit at a time: a world's folder, a session's folder, or one of the shared
7829
+ * folders. A unit is locked while it syncs. Sessions move whole, and when both sides changed one the person chooses which wins; the
7830
+ * other units go through bisync file by file, and a file both sides changed is left as two copies for the person to choose from.
7831
+ */
7832
+ var SyncService = class {
7833
+ deps;
7834
+ active = false;
7835
+ constructor(deps) {
7836
+ this.deps = deps;
7837
+ }
7838
+ /** Why sync cannot run with the settings as they are, or nothing. */
7839
+ problem() {
7840
+ const config = this.deps.config();
7841
+ if (config.remote.trim() === "") return "sync is off: no remote is set";
7842
+ if (config.configFile.trim() === "") return "the rclone configuration file is not set";
7843
+ const file = expand(config.configFile);
7844
+ if (!isAbsolute(file)) return "the rclone configuration file must be an absolute path";
7845
+ const inside = relative(this.deps.root(), normalize(file));
7846
+ if (inside === "" || !inside.startsWith("..") && !isAbsolute(inside)) return "the rclone configuration file must be outside the roleplay root, or it would be synced";
7847
+ if (!existsSync(file)) return `the rclone configuration file does not exist: ${file}`;
7848
+ }
7849
+ get running() {
7850
+ return this.active;
7851
+ }
7852
+ async status() {
7853
+ const root = this.deps.root();
7854
+ const store = readStore(root);
7855
+ const ids = /* @__PURE__ */ new Set([
7856
+ ...await localUnits(root),
7857
+ ...Object.keys(store.units),
7858
+ ...store.ignored
7859
+ ]);
7860
+ const units = [];
7861
+ for (const id of [...ids].sort((left, right) => left.localeCompare(right))) {
7862
+ const kind = kindOf(id);
7863
+ if (kind === void 0) continue;
7864
+ units.push(store.ignored.includes(id) ? {
7865
+ id,
7866
+ kind,
7867
+ state: "ignored",
7868
+ message: "deleted here and kept on the remote; it is not brought back"
7869
+ } : await this.describe(root, id, kind, store.units[id]));
7870
+ }
7871
+ const problem = this.problem();
7872
+ return {
7873
+ ...problem === void 0 ? {} : { problem },
7874
+ running: this.active,
7875
+ ...store.lastRun === void 0 ? {} : { lastRun: store.lastRun },
7876
+ units
7877
+ };
7878
+ }
7879
+ async describe(root, id, kind, record) {
7880
+ const base = {
7881
+ id,
7882
+ kind,
7883
+ ...record?.at === void 0 ? {} : { at: record.at }
7884
+ };
7885
+ const directory = join(root, id);
7886
+ const reading = async (read, otherwise) => {
7887
+ try {
7888
+ return await read();
7889
+ } catch (error) {
7890
+ if (error.code === "ENOENT") return otherwise;
7891
+ throw error;
7892
+ }
7893
+ };
7894
+ const conflicts = existsSync(directory) && modeOf(kind) === "files" ? await reading(() => conflictFiles(directory), []) : [];
7895
+ const removal = record?.diverged === "removed" || record?.diverged === "deleted" ? record.diverged : void 0;
7896
+ const gone = !existsSync(directory) && (record?.at !== void 0 || record?.base !== void 0);
7897
+ const state = async () => {
7898
+ if (this.deps.gate.isLocked(id)) return "busy";
7899
+ if (conflicts.length > 0 || record?.diverged !== void 0) return "conflict";
7900
+ if (record?.failed !== void 0) return "failed";
7901
+ if (gone) return "changed";
7902
+ if (record?.fingerprint === void 0 || !existsSync(directory)) return "new";
7903
+ return await reading(() => fingerprint(directory), void 0) === record.fingerprint ? "synced" : "changed";
7904
+ };
7905
+ const message = record?.failed ?? DIVERGED[record?.diverged ?? "none"] ?? (gone ? "removed on this device; the next run asks what to do" : void 0);
7906
+ return {
7907
+ ...base,
7908
+ state: await state(),
7909
+ ...message === void 0 ? {} : { message },
7910
+ ...conflicts.length > 0 ? { conflicts } : {},
7911
+ ...removal === void 0 ? {} : { removal }
7912
+ };
7913
+ }
7914
+ /**
7915
+ * Syncs every unit (or only `only`) that is not busy. A unit that cannot be synced is reported and the others go on.
7916
+ * @throws {SyncRefusedError} when sync is off or misconfigured, or a run is under way.
7917
+ */
7918
+ async run(only) {
7919
+ const problem = this.problem();
7920
+ if (problem !== void 0) throw new SyncRefusedError(problem);
7921
+ if (this.active) throw new SyncRefusedError("a sync is already running");
7922
+ this.active = true;
7923
+ try {
7924
+ const rclone = await this.client();
7925
+ const root = this.deps.root();
7926
+ const store = readStore(root);
7927
+ const remote = await this.remoteUnits(rclone);
7928
+ const deleted = new Set(await this.tombstones(rclone));
7929
+ const local = await localUnits(root);
7930
+ const wanted = (id) => !store.ignored.includes(id) && (only === void 0 || only.includes(id));
7931
+ const ids = [.../* @__PURE__ */ new Set([
7932
+ ...local,
7933
+ ...remote,
7934
+ ...deleted
7935
+ ])].filter(wanted).sort((left, right) => left.localeCompare(right));
7936
+ for (const id of ids) if (deleted.has(id)) await this.syncDeleted(id, local.includes(id));
7937
+ else await this.syncUnit(rclone, id, local.includes(id), remote.includes(id));
7938
+ const after = readStore(root);
7939
+ after.lastRun = this.now();
7940
+ writeStore(root, after);
7941
+ return { units: (await this.status()).units.filter((unit) => ids.includes(unit.id)) };
7942
+ } finally {
7943
+ this.active = false;
7944
+ }
7945
+ }
7946
+ /** Runs when the interval has passed since the last run; the caller calls it on a timer. */
7947
+ async auto() {
7948
+ const minutes = this.deps.config().intervalMinutes;
7949
+ if (minutes <= 0 || this.active || this.problem() !== void 0) return;
7950
+ const last = readStore(this.deps.root()).lastRun;
7951
+ if (last !== void 0 && Date.now() - Date.parse(last) < minutes * 6e4) return;
7952
+ await this.run().catch((error) => {
7953
+ this.deps.problem(`sync: ${error instanceof Error ? error.message : String(error)}`);
7954
+ });
7955
+ }
7956
+ /**
7957
+ * Settles a conflict by taking one side whole (a session, or a unit never synced here) or, for a file both sides changed, that side's copy.
7958
+ * @throws {SyncRefusedError} when the unit has nothing to choose or is busy.
7959
+ */
7960
+ async resolve(id, side) {
7961
+ const problem = this.problem();
7962
+ if (problem !== void 0) throw new SyncRefusedError(problem);
7963
+ const kind = kindOf(id);
7964
+ if (kind === void 0) throw new SyncRefusedError(`"${id}" is not a unit`);
7965
+ if (this.active) throw new SyncRefusedError("a sync is running; choose when it is over");
7966
+ const root = this.deps.root();
7967
+ const directory = join(root, id);
7968
+ const conflicts = existsSync(directory) && modeOf(kind) === "files" ? await conflictFiles(directory) : [];
7969
+ const record = readStore(root).units[id];
7970
+ if (conflicts.length === 0 && record?.diverged === void 0) throw new SyncRefusedError(`"${id}" has nothing to choose`);
7971
+ if (kind === "session") {
7972
+ const reason = this.deps.busy(id.slice(9));
7973
+ if (reason !== void 0) throw new SyncRefusedError(`"${id}" cannot be changed now: ${reason}`);
7974
+ }
7975
+ this.active = true;
7976
+ try {
7977
+ const rclone = await this.client();
7978
+ await this.guard(id, kind, async () => {
7979
+ if (conflicts.length > 0) {
7980
+ await this.keepCopies(directory, conflicts, side);
7981
+ await this.bisync(rclone, id, false);
7982
+ } else if (record?.diverged === "removed") {
7983
+ if (side === "local") await this.erase(rclone, id, kind);
7984
+ else await this.bring(rclone, id, kind);
7985
+ } else if (record?.diverged === "deleted") {
7986
+ if (side === "remote") await this.removeLocal(id, kind);
7987
+ else {
7988
+ await this.clearTombstone(rclone, id);
7989
+ await this.bring(rclone, id, kind, "local");
7990
+ }
7991
+ } else await this.bring(rclone, id, kind, side);
7992
+ });
7993
+ } finally {
7994
+ this.active = false;
7995
+ }
7996
+ return (await this.status()).units.find((unit) => unit.id === id);
7997
+ }
7998
+ /** Makes one side the unit's content: the remote's copy is downloaded, or this device's is uploaded. */
7999
+ async bring(rclone, id, kind, side = "remote") {
8000
+ const directory = join(this.deps.root(), id);
8001
+ if (modeOf(kind) === "whole") {
8002
+ if (side === "local") await this.push(rclone, id);
8003
+ else await this.pull(rclone, id, await this.revision(rclone, id));
8004
+ return;
8005
+ }
8006
+ const remote = this.remotePath(id);
8007
+ if (side === "remote") await mkdir(dirname(directory), { recursive: true });
8008
+ await this.mirror(rclone, side === "local" ? directory : remote, side === "local" ? remote : directory);
8009
+ await this.bisync(rclone, id, true);
8010
+ }
8011
+ /**
8012
+ * Deletes a session or a world from the remote: a tombstone is written first, so that no device brings it back while the data is
8013
+ * going, and every device that syncs later deletes its own copy.
8014
+ * @throws {SyncRefusedError} when sync is off or running, or the unit is not a session or a world.
8015
+ */
8016
+ async deleteRemote(id) {
8017
+ const problem = this.problem();
8018
+ if (problem !== void 0) throw new SyncRefusedError(problem);
8019
+ const kind = kindOf(id);
8020
+ if (kind !== "session" && kind !== "world") throw new SyncRefusedError(`"${id}" is not a session or a world`);
8021
+ if (this.active) throw new SyncRefusedError("a sync is running; delete when it is over");
8022
+ this.active = true;
8023
+ try {
8024
+ await this.erase(await this.client(), id, kind);
8025
+ } finally {
8026
+ this.active = false;
8027
+ }
8028
+ }
8029
+ async erase(rclone, id, kind) {
8030
+ await this.writeTombstone(rclone, id);
8031
+ const purged = await rclone.run(["purge", this.remotePath(id)]);
8032
+ if (purged.code !== 0 && purged.code !== 3) throw new RcloneError(`the remote copy of ${id} is marked deleted but could not be removed: ${why(purged)}`, purged.code);
8033
+ if (kind === "session") await rclone.run(["deletefile", this.revisionPath(id)]);
8034
+ this.drop(id);
8035
+ }
8036
+ /** A unit deleted here only is not brought back from the remote by a later run (the remote copy stays where it is) until `restore`. */
8037
+ forget(id) {
8038
+ if (kindOf(id) === void 0) throw new SyncRefusedError(`"${id}" is not a unit`);
8039
+ const root = this.deps.root();
8040
+ const store = readStore(root);
8041
+ if (!store.ignored.includes(id)) store.ignored.push(id);
8042
+ delete store.units[id];
8043
+ writeStore(root, store);
8044
+ }
8045
+ /** Takes a unit off the ignored list: the next run brings the remote's copy here again. */
8046
+ restore(id) {
8047
+ this.drop(id);
8048
+ }
8049
+ drop(id) {
8050
+ const root = this.deps.root();
8051
+ const store = readStore(root);
8052
+ store.ignored = store.ignored.filter((entry) => entry !== id);
8053
+ delete store.units[id];
8054
+ writeStore(root, store);
8055
+ }
8056
+ now() {
8057
+ return (this.deps.now?.() ?? /* @__PURE__ */ new Date()).toISOString();
8058
+ }
8059
+ async client() {
8060
+ const config = this.deps.config();
8061
+ const password = await this.deps.password();
8062
+ const setup = {
8063
+ binary: expand(config.rclone),
8064
+ configFile: expand(config.configFile),
8065
+ ...password === void 0 ? {} : { password }
8066
+ };
8067
+ return (this.deps.rclone ?? spawnRclone)(setup);
8068
+ }
8069
+ remotePath(id) {
8070
+ const remote = this.deps.config().remote.trim();
8071
+ return remote.endsWith(":") || remote.endsWith("/") ? `${remote}${id}` : `${remote}/${id}`;
8072
+ }
8073
+ async remoteUnits(rclone) {
8074
+ const listed = async (prefix) => {
8075
+ const base = this.deps.config().remote.trim();
8076
+ const path = prefix === "" ? base : this.remotePath(prefix);
8077
+ const result = await rclone.run([
8078
+ "lsf",
8079
+ "--dirs-only",
8080
+ path
8081
+ ]);
8082
+ if (result.code === 3) return [];
8083
+ if (result.code !== 0) throw new RcloneError(`cannot list ${path}: ${why(result)}`, result.code);
8084
+ return result.stdout.split("\n").map((line) => line.trim().replace(/\/$/, "")).filter((line) => line !== "" && !line.startsWith("."));
8085
+ };
8086
+ const [top, worlds, sessions] = await Promise.all([
8087
+ listed(""),
8088
+ listed("worlds"),
8089
+ listed("sessions")
8090
+ ]);
8091
+ return [
8092
+ ...top.filter((name) => SHARED_UNITS.includes(name)),
8093
+ ...worlds.map((name) => `worlds/${name}`),
8094
+ ...sessions.filter(isSessionId).map((name) => `sessions/${name}`)
8095
+ ];
8096
+ }
8097
+ /** Locks the unit, records how it went and never lets one unit's failure stop the run. */
8098
+ async syncUnit(rclone, id, local, remote) {
8099
+ const kind = kindOf(id);
8100
+ if (kind === void 0) return;
8101
+ const root = this.deps.root();
8102
+ const note = (change) => {
8103
+ const store = readStore(root);
8104
+ const record = store.units[id] ?? {};
8105
+ change(record);
8106
+ store.units[id] = record;
8107
+ writeStore(root, store);
8108
+ };
8109
+ const known = readStore(root).units[id];
8110
+ if (!local && remote && (known?.at !== void 0 || known?.base !== void 0)) {
8111
+ note((record) => {
8112
+ record.diverged = "removed";
8113
+ delete record.failed;
8114
+ });
8115
+ return;
8116
+ }
8117
+ if (kind === "session") {
8118
+ const reason = this.deps.busy(id.slice(9));
8119
+ if (reason !== void 0) {
8120
+ note((record) => {
8121
+ record.failed = `skipped: ${reason}`;
8122
+ });
8123
+ return;
8124
+ }
8125
+ }
8126
+ try {
8127
+ await this.guard(id, kind, async () => {
8128
+ await (modeOf(kind) === "whole" ? this.syncWhole(rclone, id, local) : this.syncFiles(rclone, id, local, remote));
8129
+ });
8130
+ } catch (error) {
8131
+ const message = error instanceof Error ? error.message : String(error);
8132
+ note((record) => {
8133
+ record.failed = message;
8134
+ });
8135
+ this.deps.problem(`sync ${id}: ${message}`);
8136
+ }
8137
+ }
8138
+ /** Another device deleted the unit: this device's copy goes too, unless it was never synced here or changed since, which is for the person to decide. */
8139
+ async syncDeleted(id, local) {
8140
+ const kind = kindOf(id);
8141
+ if (kind !== "session" && kind !== "world") return;
8142
+ if (!local) {
8143
+ this.drop(id);
8144
+ return;
8145
+ }
8146
+ const record = readStore(this.deps.root()).units[id];
8147
+ const synced = record?.at !== void 0 || record?.base !== void 0;
8148
+ const changed = synced && await fingerprint(join(this.deps.root(), id)) !== record.fingerprint;
8149
+ if (!synced || changed) {
8150
+ await this.record(id, (entry) => {
8151
+ entry.diverged = "deleted";
8152
+ delete entry.failed;
8153
+ });
8154
+ return;
8155
+ }
8156
+ if (kind === "session") {
8157
+ const reason = this.deps.busy(id.slice(9));
8158
+ if (reason !== void 0) {
8159
+ await this.record(id, (entry) => {
8160
+ entry.failed = `deleted on another device; skipped: ${reason}`;
8161
+ });
8162
+ return;
8163
+ }
8164
+ }
8165
+ try {
8166
+ await this.guard(id, kind, async () => {
8167
+ await this.removeLocal(id, kind);
8168
+ });
8169
+ } catch (error) {
8170
+ const message = error instanceof Error ? error.message : String(error);
8171
+ await this.record(id, (entry) => {
8172
+ entry.failed = message;
8173
+ });
8174
+ this.deps.problem(`sync ${id}: ${message}`);
8175
+ }
8176
+ }
8177
+ /** A session is deleted like any other; a world's folder is moved to the root's trash, which is emptied after a while. */
8178
+ async removeLocal(id, kind) {
8179
+ if (kind === "session") await this.deps.remove(id.slice(9));
8180
+ else {
8181
+ const trash = join(this.deps.root(), ".trash", "worlds");
8182
+ await mkdir(trash, { recursive: true });
8183
+ await rename(join(this.deps.root(), id), join(trash, `${basename(id)}-${Date.now()}`));
8184
+ }
8185
+ this.drop(id);
8186
+ }
8187
+ tombstonePath(id) {
8188
+ return this.remoteFile(`.tombstones/${encodeURIComponent(id)}.json`);
8189
+ }
8190
+ async tombstones(rclone) {
8191
+ const result = await rclone.run([
8192
+ "lsf",
8193
+ "--files-only",
8194
+ this.remoteFile(".tombstones")
8195
+ ]);
8196
+ if (result.code === 3) return [];
8197
+ if (result.code !== 0) throw new RcloneError(`cannot list the deleted units: ${why(result)}`, result.code);
8198
+ return result.stdout.split("\n").map((line) => line.trim()).filter((line) => line.endsWith(".json")).map((line) => decodeURIComponent(line.slice(0, -5)));
8199
+ }
8200
+ async writeTombstone(rclone, id) {
8201
+ const result = await rclone.run(["rcat", this.tombstonePath(id)], { input: JSON.stringify({
8202
+ device: readStore(this.deps.root()).device,
8203
+ at: this.now()
8204
+ }) });
8205
+ if (result.code !== 0) throw new RcloneError(`cannot mark ${id} deleted on the remote: ${why(result)}`, result.code);
8206
+ }
8207
+ async clearTombstone(rclone, id) {
8208
+ const result = await rclone.run(["deletefile", this.tombstonePath(id)]);
8209
+ if (result.code !== 0 && result.code !== 3 && result.code !== 4) throw new RcloneError(`cannot remove the deletion mark of ${id}: ${why(result)}`, result.code);
8210
+ }
8211
+ async guard(id, kind, work) {
8212
+ await this.deps.gate.hold(id, async () => {
8213
+ if (kind === "session") await this.deps.idle(id.slice(9));
8214
+ await work();
8215
+ if (kind !== "session") await this.deps.changed(id);
8216
+ });
8217
+ }
8218
+ async record(id, change) {
8219
+ const root = this.deps.root();
8220
+ const store = readStore(root);
8221
+ const record = store.units[id] ?? {};
8222
+ change(record);
8223
+ store.units[id] = record;
8224
+ writeStore(root, store);
8225
+ }
8226
+ async settled(id, base) {
8227
+ const print = await fingerprint(join(this.deps.root(), id));
8228
+ await this.record(id, (record) => {
8229
+ delete record.failed;
8230
+ delete record.diverged;
8231
+ record.fingerprint = print;
8232
+ record.at = this.now();
8233
+ record.ready = true;
8234
+ if (base !== void 0) record.base = base;
8235
+ });
8236
+ }
8237
+ remoteFile(file) {
8238
+ const remote = this.deps.config().remote.trim();
8239
+ return remote.endsWith(":") || remote.endsWith("/") ? `${remote}${file}` : `${remote}/${file}`;
8240
+ }
8241
+ revisionPath(id) {
8242
+ return this.remoteFile(`.revisions/${id.replaceAll("/", "__")}.json`);
8243
+ }
8244
+ async revision(rclone, id) {
8245
+ const result = await rclone.run(["cat", this.revisionPath(id)]);
8246
+ if (result.code === 3 || result.code === 4) return void 0;
8247
+ if (result.code !== 0) throw new RcloneError(`cannot read the revision of ${id}: ${why(result)}`, result.code);
8248
+ return JSON.parse(result.stdout);
8249
+ }
8250
+ async writeRevision(rclone, id, revision) {
8251
+ const result = await rclone.run(["rcat", this.revisionPath(id)], { input: JSON.stringify(revision) });
8252
+ if (result.code !== 0) throw new RcloneError(`cannot write the revision of ${id}: ${why(result)}`, result.code);
8253
+ }
8254
+ async mirror(rclone, from, to) {
8255
+ const result = await rclone.run([
8256
+ "sync",
8257
+ from,
8258
+ to,
8259
+ "--checksum",
8260
+ "--create-empty-src-dirs"
8261
+ ]);
8262
+ if (result.code !== 0) throw new RcloneError(`rclone sync failed: ${why(result)}`, result.code);
8263
+ }
8264
+ /** A session moves whole. A revision says which upload a copy came from, so a change on one side only is told from a change on both. */
8265
+ async syncWhole(rclone, id, local) {
8266
+ const root = this.deps.root();
8267
+ const record = readStore(root).units[id];
8268
+ const remote = await this.revision(rclone, id);
8269
+ if (!local) {
8270
+ if (remote !== void 0) await this.pull(rclone, id, remote);
8271
+ return;
8272
+ }
8273
+ if (remote === void 0) {
8274
+ await this.push(rclone, id);
8275
+ return;
8276
+ }
8277
+ if (!remote.complete && remote.device === readStore(root).device) {
8278
+ await this.push(rclone, id);
8279
+ return;
8280
+ }
8281
+ const changedHere = record?.fingerprint === void 0 || await fingerprint(join(root, id)) !== record.fingerprint;
8282
+ const changedThere = record?.base !== remote.rev;
8283
+ if (!changedHere && !changedThere) {
8284
+ if (record?.chat === "stale") await this.rebuildChat(id);
8285
+ else await this.settled(id);
8286
+ return;
8287
+ }
8288
+ if (changedThere && !changedHere) {
8289
+ await this.pull(rclone, id, remote);
8290
+ return;
8291
+ }
8292
+ if (!changedThere) {
8293
+ await this.push(rclone, id);
8294
+ return;
8295
+ }
8296
+ await this.record(id, (entry) => {
8297
+ entry.diverged = record?.base === void 0 ? "first" : "both";
8298
+ delete entry.failed;
8299
+ });
8300
+ }
8301
+ async push(rclone, id) {
8302
+ if (kindOf(id) === "session") await this.deps.release(id.slice(9));
8303
+ const revision = {
8304
+ rev: randomUUID(),
8305
+ device: readStore(this.deps.root()).device,
8306
+ at: this.now(),
8307
+ complete: false
8308
+ };
8309
+ await this.writeRevision(rclone, id, revision);
8310
+ await this.mirror(rclone, join(this.deps.root(), id), this.remotePath(id));
8311
+ await this.writeRevision(rclone, id, {
8312
+ ...revision,
8313
+ complete: true
8314
+ });
8315
+ await this.settled(id, revision.rev);
8316
+ }
8317
+ async pull(rclone, id, remote) {
8318
+ if (remote === void 0) throw new SyncRefusedError(`the remote has no copy of "${id}"`);
8319
+ if (!remote.complete) throw new SyncRefusedError(`the copy of "${id}" on the remote is still being uploaded; try again later`);
8320
+ const session = id.slice(9);
8321
+ await this.deps.release(session);
8322
+ const directory = join(this.deps.root(), id);
8323
+ await mkdir(directory, { recursive: true });
8324
+ await this.mirror(rclone, this.remotePath(id), directory);
8325
+ await this.settled(id, remote.rev);
8326
+ await this.rebuildChat(id);
8327
+ }
8328
+ /** The chat is made again from the files, which changes them (its timeline is renumbered), so what the folder looks like is recorded after it. */
8329
+ async rebuildChat(id) {
8330
+ await this.record(id, (record) => {
8331
+ record.chat = "stale";
8332
+ });
8333
+ await this.deps.pulled(id.slice(9));
8334
+ await this.settled(id);
8335
+ await this.record(id, (record) => {
8336
+ delete record.chat;
8337
+ });
8338
+ }
8339
+ async syncFiles(rclone, id, local, remote) {
8340
+ const directory = join(this.deps.root(), id);
8341
+ if (!local && !remote) return;
8342
+ if (!local) {
8343
+ await mkdir(dirname(directory), { recursive: true });
8344
+ await this.mirror(rclone, this.remotePath(id), directory);
8345
+ await this.bisync(rclone, id, true);
8346
+ return;
8347
+ }
8348
+ if (!remote) {
8349
+ await this.mirror(rclone, directory, this.remotePath(id));
8350
+ await this.bisync(rclone, id, true);
8351
+ return;
8352
+ }
8353
+ if (readStore(this.deps.root()).units[id]?.ready !== true) {
8354
+ const same = await rclone.run([
8355
+ "check",
8356
+ directory,
8357
+ this.remotePath(id),
8358
+ "--one-way"
8359
+ ]);
8360
+ const other = await rclone.run([
8361
+ "check",
8362
+ this.remotePath(id),
8363
+ directory,
8364
+ "--one-way"
8365
+ ]);
8366
+ if (same.code === 0 && other.code === 0) await this.bisync(rclone, id, true);
8367
+ else await this.record(id, (record) => {
8368
+ record.diverged = "first";
8369
+ delete record.failed;
8370
+ });
8371
+ return;
8372
+ }
8373
+ await this.bisync(rclone, id, false);
8374
+ }
8375
+ /** `baseline` makes the two sides the starting point of later runs; it is only used when they are known to be the same. */
8376
+ async bisync(rclone, id, baseline) {
8377
+ const directory = join(this.deps.root(), id);
8378
+ if (directory.length + this.remotePath(id).length + 30 > BISYNC_NAME_LIMIT) throw new SyncRefusedError("the paths of the data folder and the remote are too long for rclone to keep its records of them: use shorter folder names");
8379
+ const result = await rclone.run([
8380
+ "bisync",
8381
+ directory,
8382
+ this.remotePath(id),
8383
+ "--workdir",
8384
+ join(syncFolder(this.deps.root()), "bisync"),
8385
+ ...BISYNC,
8386
+ ...baseline ? ["--resync"] : []
8387
+ ]);
8388
+ if (result.code !== 0) throw new RcloneError(`rclone bisync failed: ${why(result)}`, result.code);
8389
+ if ((await conflictFiles(directory)).length > 0) {
8390
+ await this.record(id, (record) => {
8391
+ delete record.failed;
8392
+ record.ready = true;
8393
+ record.at = this.now();
8394
+ });
8395
+ return;
8396
+ }
8397
+ await this.settled(id);
8398
+ }
8399
+ /** For each file two copies were kept of: the chosen side's copy takes the file's name and the other is removed. */
8400
+ async keepCopies(directory, conflicts, side) {
8401
+ const keep = side === "local" ? "conflict1" : "conflict2";
8402
+ for (const path of conflicts) {
8403
+ const original = path.replace(/\.conflict[12]$/, "");
8404
+ if (path.endsWith(keep)) {
8405
+ await rm(join(directory, original), { force: true });
8406
+ await rename(join(directory, path), join(directory, original));
8407
+ } else await rm(join(directory, path), { force: true });
8408
+ }
8409
+ }
8410
+ };
8411
+ //#endregion
8412
+ //#region src/host/sync/wire.ts
8413
+ /** The folder that a request to the API writes or reads, if it is one of the syncable ones. */
8414
+ async function unitsOf(url, worlds) {
8415
+ const path = url.pathname.slice(`/${API_PREFIX}`.length);
8416
+ const id = url.searchParams.get("id");
8417
+ const units = [];
8418
+ if (id !== null && path.startsWith("/sessions") && isSessionId(id)) units.push(`sessions/${id}`);
8419
+ else if (id !== null && id !== "" && path.startsWith("/worlds")) {
8420
+ const world = await worlds.find(id);
8421
+ if (world !== void 0) units.push(`worlds/${basename(world.directory)}`);
8422
+ }
8423
+ if (path.startsWith("/presets")) units.push("presets");
8424
+ if (path.startsWith("/lorebooks")) units.push("lorebooks");
8425
+ if (path.startsWith("/avatars") || path.startsWith("/worlds/avatars")) units.push("avatars");
8426
+ return units;
8427
+ }
8428
+ /** Builds the sync service with the gate that keeps a unit untouched while it syncs, and hooks it up to the story's turns. */
8429
+ function createSync(ctx, wiring) {
8430
+ const gate = new UnitGate();
8431
+ const store = () => ctx.get("credentials");
8432
+ const ref = () => wiring.config().passwordRef.trim() || "ROLEPLAY_SYNC_RCLONE_CONFIG_PASS";
8433
+ const sessions = () => join(wiring.root(), "sessions");
8434
+ const service = new SyncService({
8435
+ root: wiring.root,
8436
+ config: wiring.config,
8437
+ password: async () => (await store()?.resolve(ref()))?.value,
8438
+ gate,
8439
+ busy: (session) => {
8440
+ const chat = chatSessionId(session);
8441
+ if (wiring.orchestration.playing(chat)) return "a turn is being played";
8442
+ if (wiring.orchestration.reflecting(chat)) return "the characters are reflecting";
8443
+ },
8444
+ idle: (session) => wiring.orchestration.quiesce(chatSessionId(session)),
8445
+ release: async (session) => {
8446
+ await wiring.orchestration.halt(chatSessionId(session));
8447
+ wiring.state.closeSession(sessions(), session);
8448
+ },
8449
+ pulled: wiring.rebuild,
8450
+ remove: wiring.remove,
8451
+ changed: async () => {
8452
+ await wiring.worlds.list({ refresh: true });
8453
+ },
8454
+ problem: (message) => {
8455
+ ctx.logger.warn(message);
8456
+ }
8457
+ });
8458
+ ctx.effect(() => {
8459
+ wiring.orchestration.unitFree = async (chat) => {
8460
+ const session = roleplaySessionOf(chat);
8461
+ if (session !== void 0) await gate.whenFree(`sessions/${session}`);
8462
+ };
8463
+ return () => {
8464
+ wiring.orchestration.unitFree = () => Promise.resolve();
8465
+ };
8466
+ }, "roleplay: story sync gate");
8467
+ ctx.effect(() => {
8468
+ const timer = setInterval(() => {
8469
+ service.auto();
8470
+ }, 6e4);
8471
+ timer.unref();
8472
+ return () => {
8473
+ clearInterval(timer);
8474
+ };
8475
+ }, "roleplay: sync timer");
8476
+ const describe = async (requested) => {
8477
+ const credentials = store();
8478
+ const reference = requested ?? ref();
8479
+ if (credentials === void 0) return {
8480
+ configured: false,
8481
+ writable: false,
8482
+ ref: reference
8483
+ };
8484
+ const status = await credentials.describe(reference);
8485
+ return {
8486
+ configured: status.configured,
8487
+ ...status.source === void 0 ? {} : { source: status.source },
8488
+ writable: status.writable,
8489
+ ref: reference
8490
+ };
8491
+ };
8492
+ return {
8493
+ service,
8494
+ api: {
8495
+ status: () => service.status(),
8496
+ run: (only) => service.run(only),
8497
+ resolve: (unit, side) => service.resolve(unit, side),
8498
+ forget: (unit) => {
8499
+ service.forget(unit);
8500
+ },
8501
+ restore: (unit) => {
8502
+ service.restore(unit);
8503
+ },
8504
+ password: {
8505
+ describe,
8506
+ keep: async (value, requested) => {
8507
+ const credentials = store();
8508
+ if (credentials === void 0) throw new SyncRefusedError("this profile has no credential store to keep the password in");
8509
+ const reference = requested ?? ref();
8510
+ if (value === "") await credentials.unset(reference);
8511
+ else await credentials.set(reference, value);
8512
+ return await describe(reference);
8513
+ }
8514
+ }
8515
+ },
8516
+ gate,
8517
+ locked: async (url) => (await unitsOf(url, wiring.worlds)).find((unit) => gate.isLocked(unit))
8518
+ };
8519
+ }
8520
+ //#endregion
7362
8521
  //#region src/host/speech/service.ts
7363
8522
  async function voicesOf(service) {
7364
8523
  return await service.voices();
@@ -7437,6 +8596,13 @@ const Config = z.object({
7437
8596
  apiKey: z.string().default("")
7438
8597
  })).default([]).volatile(),
7439
8598
  authoringTools: z.dict(z.dict(z.boolean())).default({}).volatile(),
8599
+ sync: z.object({
8600
+ remote: z.string().default(""),
8601
+ rclone: z.string().default(""),
8602
+ configFile: z.string().default(""),
8603
+ passwordRef: z.string().default(""),
8604
+ intervalMinutes: z.number().min(0).default(0)
8605
+ }).default({ ...DEFAULT_SYNC }).volatile(),
7440
8606
  catalogueMaxBytes: z.number().min(1024).default(268435456),
7441
8607
  catalogueTrashDays: z.number().min(0).default(30),
7442
8608
  importMaxUploadBytes: z.number().min(1024).default(104857600),
@@ -7568,7 +8734,29 @@ function apply(ctx, config) {
7568
8734
  }
7569
8735
  });
7570
8736
  catalogue.tidy();
8737
+ const sync = createSync(ctx, {
8738
+ config: () => config.sync.get(),
8739
+ root: () => root.path,
8740
+ worlds,
8741
+ orchestration,
8742
+ state: ctx.roleplayState,
8743
+ remove: (id) => deleteSession({
8744
+ state: ctx.roleplayState,
8745
+ sessionsDir: deps.sessionsDir,
8746
+ purgeChat
8747
+ }, id),
8748
+ rebuild: async (id) => {
8749
+ if (await agent.has(id)) await openSession({
8750
+ ...deps,
8751
+ replaceChat: (chat, title, events) => agent.replace(chat, title, events, purgeChat)
8752
+ }, id, { rebuild: true });
8753
+ }
8754
+ });
8755
+ prompts.frozen = () => sync.gate.isLocked("prompts");
7571
8756
  registerApi(ctx, {
8757
+ sync: sync.api,
8758
+ locked: sync.locked,
8759
+ anyLocked: () => sync.gate.any,
7572
8760
  catalogue,
7573
8761
  presets: presetLibrary,
7574
8762
  lorebooks: lorebookLibrary,
@@ -7730,17 +8918,24 @@ function apply(ctx, config) {
7730
8918
  }, id, kind, key, png, name),
7731
8919
  list: () => listSessions(deps),
7732
8920
  start: (request) => startSession(deps, request),
7733
- open: (id) => openSession(deps, id),
8921
+ open: (id, rebuild) => openSession({
8922
+ ...deps,
8923
+ replaceChat: (id, title, events) => agent.replace(id, title, events, purgeChat)
8924
+ }, id, rebuild === true ? { rebuild } : {}),
7734
8925
  send: (id, text) => agent.send(id, text),
7735
8926
  gallery: (id) => imagesOf(ctx, deps.sessionsDir, id).gallery(id),
7736
8927
  galleryFile: (id, path) => galleryFile(galleryFolder(deps.sessionsDir(), id), path),
7737
8928
  uploadImage: (id, name, bytes) => imagesOf(ctx, deps.sessionsDir, id).upload(id, name, bytes),
7738
8929
  updateImage: (id, path, update) => imagesOf(ctx, deps.sessionsDir, id).update(id, path, update),
7739
- delete: (id) => deleteSession({
7740
- state: ctx.roleplayState,
7741
- sessionsDir: deps.sessionsDir,
7742
- purgeChat
7743
- }, id),
8930
+ delete: async (id, everywhere) => {
8931
+ if (everywhere === true) await sync.service.deleteRemote(`sessions/${id}`);
8932
+ await deleteSession({
8933
+ state: ctx.roleplayState,
8934
+ sessionsDir: deps.sessionsDir,
8935
+ purgeChat
8936
+ }, id);
8937
+ if (everywhere !== true && config.sync.get().remote.trim() !== "") sync.service.forget(`sessions/${id}`);
8938
+ },
7744
8939
  archive: (id) => archiveSession({
7745
8940
  agent,
7746
8941
  sessionsDir: deps.sessionsDir