@dsh-agentic-roleplay/core 0.6.1 → 0.7.1
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/access-DtOxFKBK.js +150 -0
- package/lib/{authoring-tools-BGzzW6O5.js → authoring-tools-BKltdiea.js} +160 -74
- package/lib/authoring.js +10 -5
- package/lib/{child-D5SVNk4O.js → child-CyCViUKa.js} +48 -7
- package/lib/client.js +2157 -780
- package/lib/client.js.map +1 -1
- package/lib/compaction.js +3 -3
- package/lib/doctor.js +3 -3
- package/lib/{gallery-C7VOJ3xH.js → gallery-BxZGOgMb.js} +2 -2
- package/lib/{generator-CO8usAjE.js → generator-BFmSYvR8.js} +2 -2
- package/lib/helper.js +1 -1
- package/lib/{hooks-BLperIIB.js → hooks-HnqXO8mM.js} +10 -241
- package/lib/images.js +4 -4
- package/lib/index.js +1424 -203
- package/lib/{library-DsDc6fQa.js → library-BGxPKs9a.js} +6 -3
- package/lib/{lorebook-author-tools-C-cFiE6E.js → lorebook-author-tools-BAnUyRtS.js} +37 -29
- package/lib/lorebook-authoring.js +8 -3
- package/lib/{narration-_dA9oIt2.js → narration-DvFBqmqe.js} +9 -132
- package/lib/orchestration.js +13 -10
- package/lib/{pack-jail-BKFmbYP8.js → pack-jail-DLGguGS3.js} +20 -13
- package/lib/{packs-ZTG7fWut.js → packs-jowC-MN_.js} +2 -2
- package/lib/{presenter-agent-BHSbyWwZ.js → presenter-agent-DvZtnf0Q.js} +6 -6
- package/lib/{preset-CJLW8Mp0.js → preset-CXbC0NeU.js} +3 -3
- package/lib/{preset-author-tools-1RstgEZs.js → preset-author-tools-C26kj6t5.js} +14 -7
- package/lib/preset-authoring.js +9 -4
- package/lib/{reflection-6qfOC5OT.js → reflection-CvOZGjmn.js} +9 -170
- package/lib/{presets-DcPCnn8O.js → route-DRTJ_vcA.js} +165 -4
- package/lib/text-JbWfTHbr.js +295 -0
- package/lib/turn-guard-BvCs1VxB.js +53 -0
- package/package.json +29 -29
- package/prompts/en/import/cut-off.md +1 -0
- package/prompts/en/import/finish-failed.md +1 -1
- package/prompts/en/roleplay/decide-instruction.md +1 -0
- package/prompts/en/roleplay/decide-irrelevant.md +1 -0
- package/prompts/zh/import/cut-off.md +1 -0
- package/prompts/zh/import/finish-failed.md +1 -1
- package/prompts/zh/roleplay/decide-instruction.md +1 -0
- package/prompts/zh/roleplay/decide-irrelevant.md +1 -0
- package/lib/access-EbNdZFbk.js +0 -25
- package/lib/text-CT1re6XT.js +0 -18
package/lib/index.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import { a as
|
|
13
|
-
import {
|
|
1
|
+
import { a as chatSessionId, i as VARIABLE_VISIBILITIES, n as VARIABLE_KINDS, o as roleplaySessionOf, r as VARIABLE_SUBJECTS } from "./access-DtOxFKBK.js";
|
|
2
|
+
import { A as loadDice, At as InvalidEditProblems, Bt as pictureOf, C as populateRoutines, Ct as updateLorebookInfo, D as TRIGGERS_FILE_NAME, E as routineProblems, Ft as parseRelationEdit, G as partsOf, Gt as DICE_READINGS, H as loadCharacters, Ht as LORE_KINDS, I as loadLocations, It as parseThingEdit, J as loadVariables, Jt as authoringChatSessionId, K as withPlayer, Kt as DICE_RESULTS, Lt as IMPORT_COMMAND, M as CALENDAR_FILE_NAME, Mt as parseFactionEdit, N as loadCalendar, Nt as parseLoreEdit, P as parseCalendarFile, Pt as parsePlaceEdit, R as loadItems, Rt as ImportCardError, S as populateLore, St as reserveLorebook, T as populateVariables, Ut as LORE_SCOPES, V as CHARACTERS_DIR, Vt as readCard, Wt as DICE_COMPARISONS, X as variableProblems, Y as parseVariablesFile, _ as populateDice, _t as isLorebookName, at as IMPORT_FOLDER, b as populateKnowledge, bt as lorebookDirectory, ct as parseChoices, d as vetUrl, dt as parseWorldInfoEdit, f as loadValidatedWorld, ft as addLorebookEntries, g as populateClock, gt as entriesOf, h as populateCharacters, ht as deleteLorebookEntry, it as parseCharacterFile, j as parseDiceFile, jt as parseCharacterEdit, k as DICE_FILE_NAME, kt as subfolders, l as markReady, lt as WORLD_FILE_NAME, m as populateCalendar, mt as deleteLorebook, ot as ImportNotFoundError, p as knowsCharacter, pt as createLorebook, q as VARIABLES_FILE_NAME, qt as authoredWorldOf, rt as CHARACTER_FILE_NAME, st as ImportStore, t as AuthoringTools, u as guardedLookup, ut as parseWorldFile, v as populateFactions, vt as listLorebooks, w as populateTriggers, wt as writeLorebookEntry, x as populateLocations, y as populateItems, yt as loadLorebook, zt as isPng } from "./authoring-tools-BKltdiea.js";
|
|
3
|
+
import { a as lorebookImportMessage, c as readLorebook, d as lorebookOfChat, l as selectEntries, s as previewLorebook, t as LOREBOOK_UPLOAD, u as lorebookChatSessionId } from "./lorebook-author-tools-BAnUyRtS.js";
|
|
4
|
+
import { a as presetChatSessionId, n as narrationImportMessage, o as presetOfChat } from "./preset-author-tools-C26kj6t5.js";
|
|
5
|
+
import { C as readWorldPrompt, S as listWorldPrompts, _ as savePresetFromWorld, d as isPresetName, f as listPresets, g as reservePreset, l as createPresetFromShipped, m as presetDirectory, p as loadPreset, s as PRESET_FILE, u as deletePreset, w as writeWorldPrompt, x as deleteWorldPrompt, y as updatePresetInfo } from "./route-DRTJ_vcA.js";
|
|
6
|
+
import { c as copyPreset, d as switchPreset, f as readTimeline, h as defaultBoundaries, i as reflectionStatus, l as presetStatus, m as pathTo, o as TimelineError, p as writeTimeline, u as recordPreset } from "./reflection-CvOZGjmn.js";
|
|
7
|
+
import { a as liveSettings, 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 } from "./narration-DvFBqmqe.js";
|
|
8
|
+
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-HnqXO8mM.js";
|
|
9
|
+
import { i as translator, r as languageOf$1, t as trimCharacters } from "./text-JbWfTHbr.js";
|
|
10
|
+
import { d as sharedSamplingOf, i as childSamplingOf, u as parseSampling } from "./child-CyCViUKa.js";
|
|
11
|
+
import { S as uniqueKey, a as inheritImages, b as slug, c as referenceImagePath, l as sceneImagePath, t as addToLibrary } from "./library-BGxPKs9a.js";
|
|
12
|
+
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-DLGguGS3.js";
|
|
13
|
+
import { n as readPreset, r as selectPrompts, t as previewPreset } from "./preset-CXbC0NeU.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-
|
|
16
|
-
import { a as uploadReferenceImage, t as generateReferenceImage } from "./generator-
|
|
17
|
-
import { i as updateImage, n as galleryFile, r as listGallery } from "./gallery-
|
|
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-jowC-MN_.js";
|
|
16
|
+
import { a as uploadReferenceImage, t as generateReferenceImage } from "./generator-BFmSYvR8.js";
|
|
17
|
+
import { i as updateImage, n as galleryFile, r as listGallery } from "./gallery-BxZGOgMb.js";
|
|
18
18
|
import { basename, dirname, extname, isAbsolute, join, normalize, relative, resolve, sep } from "node:path";
|
|
19
19
|
import { cpSync, createReadStream, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
20
20
|
import { createHash, randomBytes, randomUUID } from "node:crypto";
|
|
@@ -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
|
-
*
|
|
63
|
-
*
|
|
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
|
|
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
|
-
|
|
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,
|
|
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({
|
|
@@ -376,10 +445,73 @@ var AuthoringLanguages = class extends Service {
|
|
|
376
445
|
//#region src/host/import/service.ts
|
|
377
446
|
/** What an import is configured to do, for the parts of the plugin that run in other scopes (the authoring chat's tools). */
|
|
378
447
|
var RoleplayImport = class extends Service {
|
|
379
|
-
|
|
380
|
-
constructor(ctx,
|
|
448
|
+
read;
|
|
449
|
+
constructor(ctx, read) {
|
|
381
450
|
super(ctx, "roleplayImport");
|
|
382
|
-
this.
|
|
451
|
+
this.read = read;
|
|
452
|
+
}
|
|
453
|
+
/** The settings as they stand now: they are edited while the plugin runs. */
|
|
454
|
+
get settings() {
|
|
455
|
+
return this.read();
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
//#endregion
|
|
459
|
+
//#region src/host/agent/authoring-sessions.ts
|
|
460
|
+
const AUTHORING_PROJECT_TITLE = "World Authoring";
|
|
461
|
+
const AUTHORING_AGENT_PRESET = "world-authoring";
|
|
462
|
+
/** The title of a world's authoring chat, in the language of the person who opened it. */
|
|
463
|
+
function authoringTitle(worldName, language) {
|
|
464
|
+
return translator(languageOf$1(language))("Authoring {name}", { name: worldName });
|
|
465
|
+
}
|
|
466
|
+
/** Creates or reopens the dedicated authoring chat of an authored world, and speaks in it for the person when asked to. */
|
|
467
|
+
var AuthoringSessions = class {
|
|
468
|
+
services;
|
|
469
|
+
roleplayRoot;
|
|
470
|
+
prompts;
|
|
471
|
+
languages;
|
|
472
|
+
constructor(services, roleplayRoot, prompts, languages) {
|
|
473
|
+
this.services = services;
|
|
474
|
+
this.roleplayRoot = roleplayRoot;
|
|
475
|
+
this.prompts = prompts;
|
|
476
|
+
this.languages = languages;
|
|
477
|
+
}
|
|
478
|
+
async ensure(worldId, worldName, title, language) {
|
|
479
|
+
const chatTitle = title ?? authoringTitle(worldName, language);
|
|
480
|
+
this.languages.set(worldId, language);
|
|
481
|
+
const services = this.services();
|
|
482
|
+
const projectPath = join(this.roleplayRoot(), "worlds");
|
|
483
|
+
const project = await services.registry.create(projectPath, AUTHORING_PROJECT_TITLE);
|
|
484
|
+
const sessionId = authoringChatSessionId(worldId);
|
|
485
|
+
const existed = project.sessionIds.includes(sessionId);
|
|
486
|
+
const stored = !existed && await isStored(services.persistence, sessionId);
|
|
487
|
+
if (!existed && !stored) {
|
|
488
|
+
if (services.persistence === void 0) throw new AgentUnavailableError("this DSH profile cannot store the opening of an authoring chat");
|
|
489
|
+
await storeAuthoringOpening(services.persistence, {
|
|
490
|
+
id: sessionId,
|
|
491
|
+
cwd: project.path,
|
|
492
|
+
agentPreset: AUTHORING_AGENT_PRESET
|
|
493
|
+
}, this.prompts.text("world/welcome", this.languages.get(worldId), { world: worldName }));
|
|
494
|
+
}
|
|
495
|
+
return await ensureAgentChat(services, {
|
|
496
|
+
projectPath,
|
|
497
|
+
projectTitle: AUTHORING_PROJECT_TITLE,
|
|
498
|
+
sessionId,
|
|
499
|
+
agentPreset: AUTHORING_AGENT_PRESET,
|
|
500
|
+
title: chatTitle,
|
|
501
|
+
renameExisting: true
|
|
502
|
+
}, project);
|
|
503
|
+
}
|
|
504
|
+
/** Sends a message into a chat as if the person had written it. */
|
|
505
|
+
async send(sessionId, text) {
|
|
506
|
+
await this.services().controller.prompt({
|
|
507
|
+
requestId: randomUUID(),
|
|
508
|
+
sessionId,
|
|
509
|
+
mode: "queue",
|
|
510
|
+
content: [{
|
|
511
|
+
type: "text",
|
|
512
|
+
text
|
|
513
|
+
}]
|
|
514
|
+
}, new AbortController().signal);
|
|
383
515
|
}
|
|
384
516
|
};
|
|
385
517
|
//#endregion
|
|
@@ -436,7 +568,10 @@ function folderNameOf(name) {
|
|
|
436
568
|
return folder === "" ? "imported world" : folder;
|
|
437
569
|
}
|
|
438
570
|
/** The command that starts the import, for a card kept in the world's import folder. */
|
|
439
|
-
|
|
571
|
+
function importMessage(language) {
|
|
572
|
+
const tr = translator(languageOf$1(language));
|
|
573
|
+
return `${IMPORT_COMMAND} ${IMPORT_FOLDER}/card.json ${tr("Import this card into this newly created world.")}`;
|
|
574
|
+
}
|
|
440
575
|
/**
|
|
441
576
|
* 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
577
|
* @throws {InvalidEditProblems} when the choices cannot be used. @throws {ImportNotFoundError} when the upload is gone.
|
|
@@ -460,10 +595,10 @@ async function startImport(deps, id, choices, language) {
|
|
|
460
595
|
if (record === void 0) throw new WorldNotFoundError(`the world "${world.name}" was made but cannot be found`);
|
|
461
596
|
await deps.store.adopt(id, record.directory);
|
|
462
597
|
await markReady(record.directory);
|
|
463
|
-
const chatSessionId = await deps.authoring.ensure(world.id, world.name,
|
|
598
|
+
const chatSessionId = await deps.authoring.ensure(world.id, world.name, authoringTitle(world.name, language), language);
|
|
464
599
|
let failure;
|
|
465
600
|
try {
|
|
466
|
-
await deps.authoring.send(chatSessionId, importMessage());
|
|
601
|
+
await deps.authoring.send(chatSessionId, importMessage(language));
|
|
467
602
|
} catch (error) {
|
|
468
603
|
failure = error instanceof Error ? error.message : String(error);
|
|
469
604
|
}
|
|
@@ -536,10 +671,10 @@ async function uploadPreset(deps, worldId, file, name, language) {
|
|
|
536
671
|
const preset = readPreset(file, name);
|
|
537
672
|
await mkdir(join(world.directory, IMPORT_FOLDER), { recursive: true });
|
|
538
673
|
await writeFile(join(world.directory, IMPORT_FOLDER, PRESET_FILE$1), file);
|
|
539
|
-
const chatSessionId = await deps.authoring.ensure(world.summary.id, world.summary.name,
|
|
674
|
+
const chatSessionId = await deps.authoring.ensure(world.summary.id, world.summary.name, authoringTitle(world.summary.name, language), language);
|
|
540
675
|
let failure;
|
|
541
676
|
try {
|
|
542
|
-
await deps.authoring.send(chatSessionId, presetMessage());
|
|
677
|
+
await deps.authoring.send(chatSessionId, presetMessage(language));
|
|
543
678
|
} catch (error) {
|
|
544
679
|
failure = error instanceof Error ? error.message : String(error);
|
|
545
680
|
}
|
|
@@ -644,60 +779,6 @@ function parseSelectionChoice(body) {
|
|
|
644
779
|
};
|
|
645
780
|
}
|
|
646
781
|
//#endregion
|
|
647
|
-
//#region src/host/agent/authoring-sessions.ts
|
|
648
|
-
const AUTHORING_PROJECT_TITLE = "World Authoring";
|
|
649
|
-
const AUTHORING_AGENT_PRESET = "world-authoring";
|
|
650
|
-
/** Creates or reopens the dedicated authoring chat of an authored world, and speaks in it for the person when asked to. */
|
|
651
|
-
var AuthoringSessions = class {
|
|
652
|
-
services;
|
|
653
|
-
roleplayRoot;
|
|
654
|
-
prompts;
|
|
655
|
-
languages;
|
|
656
|
-
constructor(services, roleplayRoot, prompts, languages) {
|
|
657
|
-
this.services = services;
|
|
658
|
-
this.roleplayRoot = roleplayRoot;
|
|
659
|
-
this.prompts = prompts;
|
|
660
|
-
this.languages = languages;
|
|
661
|
-
}
|
|
662
|
-
async ensure(worldId, worldName, title = `Authoring ${worldName}`, language) {
|
|
663
|
-
this.languages.set(worldId, language);
|
|
664
|
-
const services = this.services();
|
|
665
|
-
const projectPath = join(this.roleplayRoot(), "worlds");
|
|
666
|
-
const project = await services.registry.create(projectPath, AUTHORING_PROJECT_TITLE);
|
|
667
|
-
const sessionId = authoringChatSessionId(worldId);
|
|
668
|
-
const existed = project.sessionIds.includes(sessionId);
|
|
669
|
-
const stored = !existed && await isStored(services.persistence, sessionId);
|
|
670
|
-
if (!existed && !stored) {
|
|
671
|
-
if (services.persistence === void 0) throw new AgentUnavailableError("this DSH profile cannot store the opening of an authoring chat");
|
|
672
|
-
await storeAuthoringOpening(services.persistence, {
|
|
673
|
-
id: sessionId,
|
|
674
|
-
cwd: project.path,
|
|
675
|
-
agentPreset: AUTHORING_AGENT_PRESET
|
|
676
|
-
}, this.prompts.text("world/welcome", this.languages.get(worldId), { world: worldName }));
|
|
677
|
-
}
|
|
678
|
-
return await ensureAgentChat(services, {
|
|
679
|
-
projectPath,
|
|
680
|
-
projectTitle: AUTHORING_PROJECT_TITLE,
|
|
681
|
-
sessionId,
|
|
682
|
-
agentPreset: AUTHORING_AGENT_PRESET,
|
|
683
|
-
title,
|
|
684
|
-
renameExisting: true
|
|
685
|
-
}, project);
|
|
686
|
-
}
|
|
687
|
-
/** Sends a message into a chat as if the person had written it. */
|
|
688
|
-
async send(sessionId, text) {
|
|
689
|
-
await this.services().controller.prompt({
|
|
690
|
-
requestId: randomUUID(),
|
|
691
|
-
sessionId,
|
|
692
|
-
mode: "queue",
|
|
693
|
-
content: [{
|
|
694
|
-
type: "text",
|
|
695
|
-
text
|
|
696
|
-
}]
|
|
697
|
-
}, new AbortController().signal);
|
|
698
|
-
}
|
|
699
|
-
};
|
|
700
|
-
//#endregion
|
|
701
782
|
//#region src/host/agent/preset-authoring-sessions.ts
|
|
702
783
|
const PRESET_AUTHORING_PROJECT_TITLE = "Preset Authoring";
|
|
703
784
|
const PRESET_AUTHORING_PRESET = "preset-authoring";
|
|
@@ -1038,7 +1119,7 @@ var Lorebooks = class {
|
|
|
1038
1119
|
const chatSessionId = await this.deps.authoring.ensure(name, title ?? uploaded.name, language);
|
|
1039
1120
|
let failure;
|
|
1040
1121
|
try {
|
|
1041
|
-
await this.deps.authoring.send(chatSessionId, this.deps.importMessage());
|
|
1122
|
+
await this.deps.authoring.send(chatSessionId, this.deps.importMessage(language));
|
|
1042
1123
|
} catch (problem) {
|
|
1043
1124
|
failure = problem instanceof Error ? problem.message : String(problem);
|
|
1044
1125
|
}
|
|
@@ -1130,6 +1211,10 @@ var RoleplayOrchestration = class extends Service {
|
|
|
1130
1211
|
current = defaultBoundaries();
|
|
1131
1212
|
/** 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
1213
|
settle = () => Promise.resolve();
|
|
1214
|
+
/** Resolves once the chat's story is free: a turn waits here while the story is being synced. */
|
|
1215
|
+
unitFree = () => Promise.resolve();
|
|
1216
|
+
/** 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. */
|
|
1217
|
+
quiesce = () => Promise.resolve();
|
|
1133
1218
|
/** Stops everything a chat has in flight in the background and waits for it; there while the orchestration is. */
|
|
1134
1219
|
halt = () => Promise.resolve();
|
|
1135
1220
|
/** Starts a reflection of a chat's characters on what they have taken in, in the background; there while the orchestration is. */
|
|
@@ -1695,7 +1780,7 @@ var Presets = class {
|
|
|
1695
1780
|
const chatSessionId = await this.deps.authoring.ensure(name, title ?? uploaded.name, language);
|
|
1696
1781
|
let failure;
|
|
1697
1782
|
try {
|
|
1698
|
-
await this.deps.authoring.send(chatSessionId, this.deps.importMessage());
|
|
1783
|
+
await this.deps.authoring.send(chatSessionId, this.deps.importMessage(language));
|
|
1699
1784
|
} catch (problem) {
|
|
1700
1785
|
failure = problem instanceof Error ? problem.message : String(problem);
|
|
1701
1786
|
}
|
|
@@ -1734,7 +1819,7 @@ var Presets = class {
|
|
|
1734
1819
|
//#endregion
|
|
1735
1820
|
//#region src/host/presets/routes.ts
|
|
1736
1821
|
/** Not found is 404 and a request the library refuses is 422 with its reason; anything else is a fault. */
|
|
1737
|
-
function failure$
|
|
1822
|
+
function failure$2(res, error) {
|
|
1738
1823
|
if (error instanceof ImportNotFoundError) return sendError(res, 404, error.message);
|
|
1739
1824
|
if (error instanceof SessionNotFoundError || error instanceof WorldNotFoundError) return sendError(res, 404, error.message);
|
|
1740
1825
|
if (error instanceof PresetBusyError) return sendError(res, 409, error.message);
|
|
@@ -1753,7 +1838,7 @@ function presetRoutes(api) {
|
|
|
1753
1838
|
try {
|
|
1754
1839
|
sendJson(res, 200, api.list());
|
|
1755
1840
|
} catch (error) {
|
|
1756
|
-
failure$
|
|
1841
|
+
failure$2(res, error);
|
|
1757
1842
|
}
|
|
1758
1843
|
},
|
|
1759
1844
|
POST: async (req, res) => {
|
|
@@ -1772,7 +1857,7 @@ function presetRoutes(api) {
|
|
|
1772
1857
|
sampling: given
|
|
1773
1858
|
}));
|
|
1774
1859
|
} catch (error) {
|
|
1775
|
-
failure$
|
|
1860
|
+
failure$2(res, error);
|
|
1776
1861
|
}
|
|
1777
1862
|
}
|
|
1778
1863
|
},
|
|
@@ -1781,7 +1866,7 @@ function presetRoutes(api) {
|
|
|
1781
1866
|
try {
|
|
1782
1867
|
sendJson(res, 200, api.read(url.searchParams.get("name") ?? ""));
|
|
1783
1868
|
} catch (error) {
|
|
1784
|
-
failure$
|
|
1869
|
+
failure$2(res, error);
|
|
1785
1870
|
}
|
|
1786
1871
|
},
|
|
1787
1872
|
PUT: async (req, res, url) => {
|
|
@@ -1796,7 +1881,7 @@ function presetRoutes(api) {
|
|
|
1796
1881
|
...given === void 0 ? {} : { sampling: given }
|
|
1797
1882
|
}));
|
|
1798
1883
|
} catch (error) {
|
|
1799
|
-
failure$
|
|
1884
|
+
failure$2(res, error);
|
|
1800
1885
|
}
|
|
1801
1886
|
},
|
|
1802
1887
|
DELETE: async (_req, res, url) => {
|
|
@@ -1805,7 +1890,7 @@ function presetRoutes(api) {
|
|
|
1805
1890
|
res.statusCode = 204;
|
|
1806
1891
|
res.end();
|
|
1807
1892
|
} catch (error) {
|
|
1808
|
-
failure$
|
|
1893
|
+
failure$2(res, error);
|
|
1809
1894
|
}
|
|
1810
1895
|
}
|
|
1811
1896
|
},
|
|
@@ -1813,7 +1898,7 @@ function presetRoutes(api) {
|
|
|
1813
1898
|
try {
|
|
1814
1899
|
sendJson(res, 200, api.rulesPrompts(url.searchParams.get("name") ?? "", languageOf(url.searchParams.get("language") ?? "")));
|
|
1815
1900
|
} catch (error) {
|
|
1816
|
-
failure$
|
|
1901
|
+
failure$2(res, error);
|
|
1817
1902
|
}
|
|
1818
1903
|
} },
|
|
1819
1904
|
"/presets/prompt": {
|
|
@@ -1821,7 +1906,7 @@ function presetRoutes(api) {
|
|
|
1821
1906
|
try {
|
|
1822
1907
|
sendJson(res, 200, { text: api.promptText(url.searchParams.get("name") ?? "", languageOf(url.searchParams.get("language") ?? ""), url.searchParams.get("id") ?? "") });
|
|
1823
1908
|
} catch (error) {
|
|
1824
|
-
failure$
|
|
1909
|
+
failure$2(res, error);
|
|
1825
1910
|
}
|
|
1826
1911
|
},
|
|
1827
1912
|
PUT: async (req, res, url) => {
|
|
@@ -1830,14 +1915,14 @@ function presetRoutes(api) {
|
|
|
1830
1915
|
try {
|
|
1831
1916
|
sendJson(res, 200, await api.writePrompt(url.searchParams.get("name") ?? "", languageOf(url.searchParams.get("language") ?? ""), url.searchParams.get("id") ?? "", text));
|
|
1832
1917
|
} catch (error) {
|
|
1833
|
-
failure$
|
|
1918
|
+
failure$2(res, error);
|
|
1834
1919
|
}
|
|
1835
1920
|
},
|
|
1836
1921
|
DELETE: async (_req, res, url) => {
|
|
1837
1922
|
try {
|
|
1838
1923
|
sendJson(res, 200, await api.writePrompt(url.searchParams.get("name") ?? "", languageOf(url.searchParams.get("language") ?? ""), url.searchParams.get("id") ?? "", void 0));
|
|
1839
1924
|
} catch (error) {
|
|
1840
|
-
failure$
|
|
1925
|
+
failure$2(res, error);
|
|
1841
1926
|
}
|
|
1842
1927
|
}
|
|
1843
1928
|
},
|
|
@@ -1847,7 +1932,7 @@ function presetRoutes(api) {
|
|
|
1847
1932
|
try {
|
|
1848
1933
|
sendJson(res, 200, await api.author(url.searchParams.get("name") ?? "", language === void 0 ? void 0 : languageOf(language)));
|
|
1849
1934
|
} catch (error) {
|
|
1850
|
-
failure$
|
|
1935
|
+
failure$2(res, error);
|
|
1851
1936
|
}
|
|
1852
1937
|
} },
|
|
1853
1938
|
"/presets/import/verbatim": { POST: async (req, res) => {
|
|
@@ -1863,7 +1948,7 @@ function presetRoutes(api) {
|
|
|
1863
1948
|
...title === void 0 ? {} : { title }
|
|
1864
1949
|
}));
|
|
1865
1950
|
} catch (error) {
|
|
1866
|
-
failure$
|
|
1951
|
+
failure$2(res, error);
|
|
1867
1952
|
}
|
|
1868
1953
|
} },
|
|
1869
1954
|
"/presets/import/staged": { POST: async (req, res) => {
|
|
@@ -1884,7 +1969,7 @@ function presetRoutes(api) {
|
|
|
1884
1969
|
...language === void 0 ? {} : { language: languageOf(language) }
|
|
1885
1970
|
}));
|
|
1886
1971
|
} catch (error) {
|
|
1887
|
-
failure$
|
|
1972
|
+
failure$2(res, error);
|
|
1888
1973
|
}
|
|
1889
1974
|
} },
|
|
1890
1975
|
"/presets/import": { POST: async (req, res, url) => {
|
|
@@ -1892,7 +1977,7 @@ function presetRoutes(api) {
|
|
|
1892
1977
|
try {
|
|
1893
1978
|
sendJson(res, 200, await api.importFile(url.searchParams.get("name") ?? "", await readBinaryBody(req, api.maxUploadBytes), language === null ? void 0 : languageOf(language)));
|
|
1894
1979
|
} catch (error) {
|
|
1895
|
-
failure$
|
|
1980
|
+
failure$2(res, error);
|
|
1896
1981
|
}
|
|
1897
1982
|
} },
|
|
1898
1983
|
"/sessions/preset": {
|
|
@@ -1900,7 +1985,7 @@ function presetRoutes(api) {
|
|
|
1900
1985
|
try {
|
|
1901
1986
|
sendJson(res, 200, api.status(url.searchParams.get("id") ?? ""));
|
|
1902
1987
|
} catch (error) {
|
|
1903
|
-
failure$
|
|
1988
|
+
failure$2(res, error);
|
|
1904
1989
|
}
|
|
1905
1990
|
},
|
|
1906
1991
|
POST: async (req, res, url) => {
|
|
@@ -1909,7 +1994,7 @@ function presetRoutes(api) {
|
|
|
1909
1994
|
try {
|
|
1910
1995
|
sendJson(res, 200, api.apply(url.searchParams.get("id") ?? "", typeof name === "string" ? name : void 0));
|
|
1911
1996
|
} catch (error) {
|
|
1912
|
-
failure$
|
|
1997
|
+
failure$2(res, error);
|
|
1913
1998
|
}
|
|
1914
1999
|
}
|
|
1915
2000
|
}
|
|
@@ -1918,7 +2003,7 @@ function presetRoutes(api) {
|
|
|
1918
2003
|
//#endregion
|
|
1919
2004
|
//#region src/host/lorebooks/routes.ts
|
|
1920
2005
|
/** 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) {
|
|
2006
|
+
function failure$1(res, error) {
|
|
1922
2007
|
if (error instanceof ImportNotFoundError) return sendError(res, 404, error.message);
|
|
1923
2008
|
if (error instanceof SessionNotFoundError) return sendError(res, 404, error.message);
|
|
1924
2009
|
if (error instanceof LorebookBusyError) return sendError(res, 409, error.message);
|
|
@@ -1953,7 +2038,7 @@ function lorebookRoutes(api) {
|
|
|
1953
2038
|
try {
|
|
1954
2039
|
sendJson(res, 200, await api.status(url.searchParams.get("id") ?? ""));
|
|
1955
2040
|
} catch (error) {
|
|
1956
|
-
failure(res, error);
|
|
2041
|
+
failure$1(res, error);
|
|
1957
2042
|
}
|
|
1958
2043
|
},
|
|
1959
2044
|
POST: async (req, res, url) => {
|
|
@@ -1962,14 +2047,14 @@ function lorebookRoutes(api) {
|
|
|
1962
2047
|
try {
|
|
1963
2048
|
sendJson(res, 200, await api.addToStory(url.searchParams.get("id") ?? "", name));
|
|
1964
2049
|
} catch (error) {
|
|
1965
|
-
failure(res, error);
|
|
2050
|
+
failure$1(res, error);
|
|
1966
2051
|
}
|
|
1967
2052
|
},
|
|
1968
2053
|
DELETE: async (_req, res, url) => {
|
|
1969
2054
|
try {
|
|
1970
2055
|
sendJson(res, 200, api.removeFromStory(url.searchParams.get("id") ?? "", url.searchParams.get("name") ?? ""));
|
|
1971
2056
|
} catch (error) {
|
|
1972
|
-
failure(res, error);
|
|
2057
|
+
failure$1(res, error);
|
|
1973
2058
|
}
|
|
1974
2059
|
}
|
|
1975
2060
|
},
|
|
@@ -1978,7 +2063,7 @@ function lorebookRoutes(api) {
|
|
|
1978
2063
|
try {
|
|
1979
2064
|
sendJson(res, 200, await api.list());
|
|
1980
2065
|
} catch (error) {
|
|
1981
|
-
failure(res, error);
|
|
2066
|
+
failure$1(res, error);
|
|
1982
2067
|
}
|
|
1983
2068
|
},
|
|
1984
2069
|
POST: async (req, res) => {
|
|
@@ -1992,7 +2077,7 @@ function lorebookRoutes(api) {
|
|
|
1992
2077
|
...description === void 0 ? {} : { description }
|
|
1993
2078
|
}));
|
|
1994
2079
|
} catch (error) {
|
|
1995
|
-
failure(res, error);
|
|
2080
|
+
failure$1(res, error);
|
|
1996
2081
|
}
|
|
1997
2082
|
}
|
|
1998
2083
|
},
|
|
@@ -2001,7 +2086,7 @@ function lorebookRoutes(api) {
|
|
|
2001
2086
|
try {
|
|
2002
2087
|
sendJson(res, 200, await api.read(url.searchParams.get("name") ?? ""));
|
|
2003
2088
|
} catch (error) {
|
|
2004
|
-
failure(res, error);
|
|
2089
|
+
failure$1(res, error);
|
|
2005
2090
|
}
|
|
2006
2091
|
},
|
|
2007
2092
|
PUT: async (req, res, url) => {
|
|
@@ -2013,7 +2098,7 @@ function lorebookRoutes(api) {
|
|
|
2013
2098
|
...description === void 0 ? {} : { description }
|
|
2014
2099
|
}));
|
|
2015
2100
|
} catch (error) {
|
|
2016
|
-
failure(res, error);
|
|
2101
|
+
failure$1(res, error);
|
|
2017
2102
|
}
|
|
2018
2103
|
},
|
|
2019
2104
|
DELETE: async (_req, res, url) => {
|
|
@@ -2022,7 +2107,7 @@ function lorebookRoutes(api) {
|
|
|
2022
2107
|
res.statusCode = 204;
|
|
2023
2108
|
res.end();
|
|
2024
2109
|
} catch (error) {
|
|
2025
|
-
failure(res, error);
|
|
2110
|
+
failure$1(res, error);
|
|
2026
2111
|
}
|
|
2027
2112
|
}
|
|
2028
2113
|
},
|
|
@@ -2033,14 +2118,14 @@ function lorebookRoutes(api) {
|
|
|
2033
2118
|
try {
|
|
2034
2119
|
sendJson(res, 200, await api.writeEntry(url.searchParams.get("name") ?? "", entry));
|
|
2035
2120
|
} catch (error) {
|
|
2036
|
-
failure(res, error);
|
|
2121
|
+
failure$1(res, error);
|
|
2037
2122
|
}
|
|
2038
2123
|
},
|
|
2039
2124
|
DELETE: async (_req, res, url) => {
|
|
2040
2125
|
try {
|
|
2041
2126
|
sendJson(res, 200, await api.removeEntry(url.searchParams.get("name") ?? "", url.searchParams.get("id") ?? ""));
|
|
2042
2127
|
} catch (error) {
|
|
2043
|
-
failure(res, error);
|
|
2128
|
+
failure$1(res, error);
|
|
2044
2129
|
}
|
|
2045
2130
|
}
|
|
2046
2131
|
},
|
|
@@ -2050,7 +2135,7 @@ function lorebookRoutes(api) {
|
|
|
2050
2135
|
try {
|
|
2051
2136
|
sendJson(res, 200, await api.author(url.searchParams.get("name") ?? "", language === void 0 ? void 0 : languageOf(language)));
|
|
2052
2137
|
} catch (error) {
|
|
2053
|
-
failure(res, error);
|
|
2138
|
+
failure$1(res, error);
|
|
2054
2139
|
}
|
|
2055
2140
|
} },
|
|
2056
2141
|
"/lorebooks/import/verbatim": { POST: async (req, res) => {
|
|
@@ -2068,7 +2153,7 @@ function lorebookRoutes(api) {
|
|
|
2068
2153
|
...title === void 0 ? {} : { title }
|
|
2069
2154
|
}));
|
|
2070
2155
|
} catch (error) {
|
|
2071
|
-
failure(res, error);
|
|
2156
|
+
failure$1(res, error);
|
|
2072
2157
|
}
|
|
2073
2158
|
} },
|
|
2074
2159
|
"/lorebooks/import/staged": { POST: async (req, res) => {
|
|
@@ -2089,7 +2174,7 @@ function lorebookRoutes(api) {
|
|
|
2089
2174
|
...language === void 0 ? {} : { language: languageOf(language) }
|
|
2090
2175
|
}));
|
|
2091
2176
|
} catch (error) {
|
|
2092
|
-
failure(res, error);
|
|
2177
|
+
failure$1(res, error);
|
|
2093
2178
|
}
|
|
2094
2179
|
} },
|
|
2095
2180
|
"/lorebooks/import": { POST: async (req, res, url) => {
|
|
@@ -2097,11 +2182,116 @@ function lorebookRoutes(api) {
|
|
|
2097
2182
|
try {
|
|
2098
2183
|
sendJson(res, 200, await api.importFile(url.searchParams.get("name") ?? "", await readBinaryBody(req, api.maxUploadBytes), language === null ? void 0 : languageOf(language)));
|
|
2099
2184
|
} catch (error) {
|
|
2100
|
-
failure(res, error);
|
|
2185
|
+
failure$1(res, error);
|
|
2101
2186
|
}
|
|
2102
2187
|
} }
|
|
2103
2188
|
};
|
|
2104
2189
|
}
|
|
2190
|
+
//#endregion
|
|
2191
|
+
//#region src/host/sync/errors.ts
|
|
2192
|
+
/** A run, or a choice, cannot be made now. */
|
|
2193
|
+
var SyncRefusedError = class extends Error {};
|
|
2194
|
+
/** rclone could not be started or reported a failure. */
|
|
2195
|
+
var RcloneError = class extends Error {
|
|
2196
|
+
code;
|
|
2197
|
+
constructor(message, code) {
|
|
2198
|
+
super(message);
|
|
2199
|
+
this.code = code;
|
|
2200
|
+
}
|
|
2201
|
+
};
|
|
2202
|
+
/** The unit is being synced: nothing may write to it or read from it until the run is over. */
|
|
2203
|
+
var UnitLockedError = class extends Error {};
|
|
2204
|
+
//#endregion
|
|
2205
|
+
//#region src/shared/catalogue.ts
|
|
2206
|
+
/** What a credential reference is: letters, digits and `_`, not starting with a digit (DSH's grammar). */
|
|
2207
|
+
const CREDENTIAL_REF = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
2208
|
+
/** 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. */
|
|
2209
|
+
function catalogueKeyRef(name) {
|
|
2210
|
+
const trimmed = name.trim();
|
|
2211
|
+
const slug = trimCharacters(trimmed.normalize("NFKD").replace(/[^A-Za-z0-9]+/g, "_"), "_").toUpperCase();
|
|
2212
|
+
let checksum = 2166136261;
|
|
2213
|
+
for (const unit of trimmed) checksum = Math.imul(checksum ^ unit.codePointAt(0), 16777619) >>> 0;
|
|
2214
|
+
return `ROLEPLAY_CATALOGUE_${slug !== "" && /^[A-Za-z0-9 _.-]+$/.test(trimmed) ? slug : `${slug === "" ? "X" : slug}_${checksum.toString(16).toUpperCase()}`}_API_KEY`;
|
|
2215
|
+
}
|
|
2216
|
+
const CATALOGUE_CARD_TYPE = "sillytavern/character";
|
|
2217
|
+
const CATALOGUE_PRESET_TYPE = "sillytavern/preset";
|
|
2218
|
+
const CATALOGUE_LOREBOOK_TYPE = "sillytavern/lorebook";
|
|
2219
|
+
const CATALOGUE_WORLD_TYPE = "dsh-agentic-roleplay/world";
|
|
2220
|
+
//#endregion
|
|
2221
|
+
//#region src/host/sync/routes.ts
|
|
2222
|
+
function failure(res, error) {
|
|
2223
|
+
if (error instanceof UnitLockedError) return sendError(res, 423, error.message);
|
|
2224
|
+
if (error instanceof SyncRefusedError) return sendError(res, 409, error.message);
|
|
2225
|
+
if (error instanceof RcloneError) return sendError(res, 502, error.message);
|
|
2226
|
+
throw error;
|
|
2227
|
+
}
|
|
2228
|
+
function syncRoutes(api) {
|
|
2229
|
+
return {
|
|
2230
|
+
"/sync/status": { GET: async (_req, res) => {
|
|
2231
|
+
try {
|
|
2232
|
+
sendJson(res, 200, await api.status());
|
|
2233
|
+
} catch (error) {
|
|
2234
|
+
failure(res, error);
|
|
2235
|
+
}
|
|
2236
|
+
} },
|
|
2237
|
+
"/sync/run": { POST: async (req, res) => {
|
|
2238
|
+
const only = (await readJsonBody(req))["units"];
|
|
2239
|
+
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");
|
|
2240
|
+
try {
|
|
2241
|
+
sendJson(res, 200, await api.run(only));
|
|
2242
|
+
} catch (error) {
|
|
2243
|
+
failure(res, error);
|
|
2244
|
+
}
|
|
2245
|
+
} },
|
|
2246
|
+
"/sync/resolve": { POST: async (req, res) => {
|
|
2247
|
+
const { unit, side } = await readJsonBody(req);
|
|
2248
|
+
if (typeof unit !== "string" || side !== "local" && side !== "remote") return sendError(res, 400, "\"unit\" must be text and \"side\" local or remote");
|
|
2249
|
+
try {
|
|
2250
|
+
sendJson(res, 200, await api.resolve(unit, side) ?? null);
|
|
2251
|
+
} catch (error) {
|
|
2252
|
+
failure(res, error);
|
|
2253
|
+
}
|
|
2254
|
+
} },
|
|
2255
|
+
"/sync/forget": { POST: async (req, res) => {
|
|
2256
|
+
const { unit } = await readJsonBody(req);
|
|
2257
|
+
if (typeof unit !== "string") return sendError(res, 400, "\"unit\" must be text");
|
|
2258
|
+
try {
|
|
2259
|
+
api.forget(unit);
|
|
2260
|
+
sendJson(res, 200, {});
|
|
2261
|
+
} catch (error) {
|
|
2262
|
+
failure(res, error);
|
|
2263
|
+
}
|
|
2264
|
+
} },
|
|
2265
|
+
"/sync/restore": { POST: async (req, res) => {
|
|
2266
|
+
const { unit } = await readJsonBody(req);
|
|
2267
|
+
if (typeof unit !== "string") return sendError(res, 400, "\"unit\" must be text");
|
|
2268
|
+
try {
|
|
2269
|
+
api.restore(unit);
|
|
2270
|
+
sendJson(res, 200, {});
|
|
2271
|
+
} catch (error) {
|
|
2272
|
+
failure(res, error);
|
|
2273
|
+
}
|
|
2274
|
+
} },
|
|
2275
|
+
"/sync/password": {
|
|
2276
|
+
GET: async (_req, res, url) => {
|
|
2277
|
+
const reference = url.searchParams.get("ref") ?? void 0;
|
|
2278
|
+
if (reference !== void 0 && !CREDENTIAL_REF.test(reference)) return sendError(res, 400, "\"ref\" must be a credential name");
|
|
2279
|
+
sendJson(res, 200, await api.password.describe(reference));
|
|
2280
|
+
},
|
|
2281
|
+
PUT: async (req, res) => {
|
|
2282
|
+
const { ref, value } = await readJsonBody(req);
|
|
2283
|
+
if (typeof value !== "string") return sendError(res, 400, "\"value\" must be text");
|
|
2284
|
+
if (ref !== void 0 && (typeof ref !== "string" || !CREDENTIAL_REF.test(ref))) return sendError(res, 400, "\"ref\" must be a credential name");
|
|
2285
|
+
try {
|
|
2286
|
+
sendJson(res, 200, await api.password.keep(value, ref));
|
|
2287
|
+
} catch (error) {
|
|
2288
|
+
if (error instanceof SyncRefusedError) return sendError(res, 409, error.message);
|
|
2289
|
+
throw error;
|
|
2290
|
+
}
|
|
2291
|
+
}
|
|
2292
|
+
}
|
|
2293
|
+
};
|
|
2294
|
+
}
|
|
2105
2295
|
/** The headers of every file of a pack. */
|
|
2106
2296
|
const PACK_HEADERS = {
|
|
2107
2297
|
"content-security-policy": [
|
|
@@ -2900,7 +3090,7 @@ function languageField(value) {
|
|
|
2900
3090
|
throw new InvalidEditProblems([error instanceof Error ? error.message : String(error)]);
|
|
2901
3091
|
}
|
|
2902
3092
|
}
|
|
2903
|
-
function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld, translation, worldCharacters, importer, avatars, worldFiles, worldEditor, sessions, packs, presets, lorebooks, connection, speech, externalTools }) {
|
|
3093
|
+
function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld, translation, worldCharacters, importer, avatars, worldFiles, worldEditor, sessions, packs, presets, lorebooks, sync, locked, anyLocked, connection, speech, externalTools }) {
|
|
2904
3094
|
const routes = {
|
|
2905
3095
|
"/speech/capabilities": { GET: async (_req, res) => {
|
|
2906
3096
|
sendJson(res, 200, { streaming: speech.streaming() });
|
|
@@ -3868,7 +4058,7 @@ function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld
|
|
|
3868
4058
|
} },
|
|
3869
4059
|
"/sessions/open": { POST: async (_req, res, url) => {
|
|
3870
4060
|
try {
|
|
3871
|
-
sendJson(res, 200, await sessions.open(url.searchParams.get("id") ?? ""));
|
|
4061
|
+
sendJson(res, 200, await sessions.open(url.searchParams.get("id") ?? "", url.searchParams.get("rebuild") === "1"));
|
|
3872
4062
|
} catch (error) {
|
|
3873
4063
|
if (error instanceof SessionNotFoundError) return sendError(res, 404, error.message);
|
|
3874
4064
|
if (error instanceof SessionArchivedError) return sendError(res, 409, error.message);
|
|
@@ -4215,8 +4405,9 @@ function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld
|
|
|
4215
4405
|
} },
|
|
4216
4406
|
"/sessions/item": { DELETE: async (_req, res, url) => {
|
|
4217
4407
|
try {
|
|
4218
|
-
await sessions.delete(url.searchParams.get("id") ?? "");
|
|
4408
|
+
await sessions.delete(url.searchParams.get("id") ?? "", url.searchParams.get("everywhere") === "true");
|
|
4219
4409
|
} catch (error) {
|
|
4410
|
+
if (error instanceof SyncRefusedError || error instanceof RcloneError) return failure(res, error);
|
|
4220
4411
|
if (error instanceof SessionNotFoundError) return sendError(res, 404, error.message);
|
|
4221
4412
|
if (error instanceof AgentUnavailableError) return sendError(res, 503, error.message);
|
|
4222
4413
|
throw error;
|
|
@@ -4227,6 +4418,7 @@ function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld
|
|
|
4227
4418
|
...interfaceRoutes(packs),
|
|
4228
4419
|
...presetRoutes(presets),
|
|
4229
4420
|
...lorebookRoutes(lorebooks),
|
|
4421
|
+
...syncRoutes(sync),
|
|
4230
4422
|
"/worlds/cover": { GET: async (_req, res, url) => {
|
|
4231
4423
|
const cover = await worlds.cover(url.searchParams.get("id") ?? "");
|
|
4232
4424
|
if (cover === void 0) return sendError(res, 404, "no cover");
|
|
@@ -4259,6 +4451,10 @@ function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld
|
|
|
4259
4451
|
}
|
|
4260
4452
|
const methods = routes[path];
|
|
4261
4453
|
if (methods === void 0) return sendError(res, 404, "not found");
|
|
4454
|
+
if (anyLocked?.() === true && !path.startsWith("/sync/")) {
|
|
4455
|
+
const unit = await locked?.(url);
|
|
4456
|
+
if (unit !== void 0) return sendError(res, 423, `"${unit}" is being synced; try again when it is done`);
|
|
4457
|
+
}
|
|
4262
4458
|
const route = methods[req.method ?? ""];
|
|
4263
4459
|
if (route === void 0) {
|
|
4264
4460
|
res.setHeader("allow", Object.keys(methods).join(", "));
|
|
@@ -5495,20 +5691,84 @@ async function addAvatarsToWorld(avatars, world, keys, locations) {
|
|
|
5495
5691
|
}
|
|
5496
5692
|
return brought.map((entry) => entry.id);
|
|
5497
5693
|
}
|
|
5694
|
+
//#endregion
|
|
5695
|
+
//#region src/host/agent/history.ts
|
|
5696
|
+
/** The unknown seq of a turn that is not in the log. */
|
|
5697
|
+
const NOT_IN_LOG = -1;
|
|
5498
5698
|
/**
|
|
5499
|
-
*
|
|
5500
|
-
*
|
|
5501
|
-
*
|
|
5502
|
-
* @throws {InvalidWorldError} when the world's characters cannot be read.
|
|
5503
|
-
* @throws {InvalidStartError} when the name or the player's character is not acceptable.
|
|
5504
|
-
* @throws {AgentUnavailableError} when the chat cannot be created.
|
|
5699
|
+
* 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
|
|
5700
|
+
* 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
|
|
5701
|
+
* 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.
|
|
5505
5702
|
*/
|
|
5506
|
-
|
|
5507
|
-
|
|
5508
|
-
|
|
5509
|
-
|
|
5510
|
-
|
|
5511
|
-
|
|
5703
|
+
function rebuildChat(opening, timeline) {
|
|
5704
|
+
const session = Session.create(SessionId("rebuilt"));
|
|
5705
|
+
appendNarratedTurn(session, 1, {
|
|
5706
|
+
segments: [...opening],
|
|
5707
|
+
repaired: false
|
|
5708
|
+
});
|
|
5709
|
+
const seqs = /* @__PURE__ */ new Map();
|
|
5710
|
+
let last = 1;
|
|
5711
|
+
for (const node of pathTo(timeline, timeline.head)) {
|
|
5712
|
+
if (node.output === void 0) continue;
|
|
5713
|
+
for (let skipped = last + 1; skipped < node.turn; skipped += 1) appendEmptyTurn(session, skipped);
|
|
5714
|
+
seqs.set(node.id, appendNarratedTurn(session, node.turn, node.output, node.player));
|
|
5715
|
+
last = node.turn;
|
|
5716
|
+
}
|
|
5717
|
+
return {
|
|
5718
|
+
events: session.snapshotEvents(),
|
|
5719
|
+
seqs
|
|
5720
|
+
};
|
|
5721
|
+
}
|
|
5722
|
+
/** The timeline with its turns pointed at the rebuilt log; a turn the log does not hold is marked as not in it. */
|
|
5723
|
+
function repointTimeline(timeline, seqs) {
|
|
5724
|
+
return {
|
|
5725
|
+
...timeline,
|
|
5726
|
+
nodes: timeline.nodes.map(({ startSeq: _start, endSeq: _end, noticeSeq: _notice, ...node }) => {
|
|
5727
|
+
const at = seqs.get(node.id);
|
|
5728
|
+
return at === void 0 ? {
|
|
5729
|
+
...node,
|
|
5730
|
+
userSeq: NOT_IN_LOG
|
|
5731
|
+
} : {
|
|
5732
|
+
...node,
|
|
5733
|
+
userSeq: at.userSeq ?? NOT_IN_LOG,
|
|
5734
|
+
startSeq: at.startSeq,
|
|
5735
|
+
endSeq: at.endSeq
|
|
5736
|
+
};
|
|
5737
|
+
})
|
|
5738
|
+
};
|
|
5739
|
+
}
|
|
5740
|
+
const OPENING_FILE = "opening.json";
|
|
5741
|
+
/** 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. */
|
|
5742
|
+
const DEFAULT_OPENING = "The story begins.";
|
|
5743
|
+
/** Keeps the opening in the session folder: it is not in the database, and a chat made again must begin with it. */
|
|
5744
|
+
function writeOpening(folder, opening) {
|
|
5745
|
+
const target = join(folder, OPENING_FILE);
|
|
5746
|
+
writeFileSync(`${target}.writing`, JSON.stringify(opening));
|
|
5747
|
+
renameSync(`${target}.writing`, target);
|
|
5748
|
+
}
|
|
5749
|
+
/** The opening a session began with, or nothing for a session made before it was kept. */
|
|
5750
|
+
function readOpening(folder) {
|
|
5751
|
+
try {
|
|
5752
|
+
return JSON.parse(readFileSync(join(folder, OPENING_FILE), "utf8"));
|
|
5753
|
+
} catch (error) {
|
|
5754
|
+
if (error.code === "ENOENT") return void 0;
|
|
5755
|
+
throw error;
|
|
5756
|
+
}
|
|
5757
|
+
}
|
|
5758
|
+
/**
|
|
5759
|
+
* Start a new session for a world: create its folder and database and fill it
|
|
5760
|
+
* from the world. Either the whole session exists afterwards or nothing does.
|
|
5761
|
+
* @throws {WorldNotFoundError} when no world has that id.
|
|
5762
|
+
* @throws {InvalidWorldError} when the world's characters cannot be read.
|
|
5763
|
+
* @throws {InvalidStartError} when the name or the player's character is not acceptable.
|
|
5764
|
+
* @throws {AgentUnavailableError} when the chat cannot be created.
|
|
5765
|
+
*/
|
|
5766
|
+
/** What is unfinished in the world as it was started, errors first, for the player's card before the first message. */
|
|
5767
|
+
function worldFindings(session) {
|
|
5768
|
+
const rank = {
|
|
5769
|
+
error: 0,
|
|
5770
|
+
warning: 1,
|
|
5771
|
+
info: 2
|
|
5512
5772
|
};
|
|
5513
5773
|
return lintWorld(session).filter((finding) => finding.severity !== "info").sort((a, b) => rank[a.severity] - rank[b.severity]).map((finding) => finding.message);
|
|
5514
5774
|
}
|
|
@@ -5647,6 +5907,7 @@ async function startSession(deps, request) {
|
|
|
5647
5907
|
if (packProblems.length > 0) throw new InvalidWorldError(`the interface packs of "${world.summary.name}" have ${packProblems.length} problem(s)`, packProblems);
|
|
5648
5908
|
writeSettings(sessionFolder(sessionsDir, id), settings);
|
|
5649
5909
|
addToNote(session, "world", 1, [...unavailable, ...worldFindings(session)]);
|
|
5910
|
+
if (opening !== void 0) writeOpening(folder, opening);
|
|
5650
5911
|
chatSessionId = await deps.agent.ensure(id, name, opening);
|
|
5651
5912
|
} catch (error) {
|
|
5652
5913
|
deps.state.deleteSession(sessionsDir, id);
|
|
@@ -5686,8 +5947,6 @@ function presetFor(deps, asked, suggested) {
|
|
|
5686
5947
|
return { unavailable: [`The world suggests the preset "${name}", which cannot be used here (${reason}), so the story is told in the default style.`] };
|
|
5687
5948
|
}
|
|
5688
5949
|
}
|
|
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
5950
|
/** The opening of the world with each speaker's name, or the reason it cannot be used. */
|
|
5692
5951
|
function openingOf(world, characters) {
|
|
5693
5952
|
if (world.opening === void 0) return [{
|
|
@@ -5971,14 +6230,30 @@ function decisionsOfSession(deps, id) {
|
|
|
5971
6230
|
//#endregion
|
|
5972
6231
|
//#region src/host/sessions/open-session.ts
|
|
5973
6232
|
/** Make sure a session has its DSH chat, so that it can be opened. */
|
|
5974
|
-
async function openSession(deps, id) {
|
|
6233
|
+
async function openSession(deps, id, options = {}) {
|
|
5975
6234
|
if (!isSessionId(id) || !existsSync(sessionDatabase(deps.sessionsDir(), id))) throw new SessionNotFoundError(`no session "${id}"`);
|
|
5976
6235
|
if (readStatus(sessionFolder(deps.sessionsDir(), id)).archivedAt !== void 0) throw new SessionArchivedError(`the session "${id}" is put away: bring it back first`);
|
|
5977
6236
|
const { name } = deps.state.openSession(deps.sessionsDir(), id).metadata.get();
|
|
5978
|
-
|
|
6237
|
+
const folder = sessionFolder(deps.sessionsDir(), id);
|
|
6238
|
+
const present = await deps.agent.has(id);
|
|
6239
|
+
const again = !present || options.rebuild === true && deps.replaceChat !== void 0;
|
|
6240
|
+
const timeline = again ? readTimeline(folder) : void 0;
|
|
6241
|
+
const opening = again ? readOpening(folder) ?? (timeline.nodes.length > 0 ? [{
|
|
6242
|
+
type: "narration",
|
|
6243
|
+
text: "The story begins."
|
|
6244
|
+
}] : void 0) : void 0;
|
|
6245
|
+
if (opening === void 0 || timeline === void 0) return {
|
|
5979
6246
|
id,
|
|
5980
6247
|
chatSessionId: await deps.agent.ensure(id, name)
|
|
5981
6248
|
};
|
|
6249
|
+
const rebuilt = rebuildChat(opening, timeline);
|
|
6250
|
+
const chatSessionId$1 = present ? chatSessionId(id) : await deps.agent.ensure(id, name, opening, rebuilt.events);
|
|
6251
|
+
if (present) await deps.replaceChat(id, name, rebuilt.events);
|
|
6252
|
+
writeTimeline(folder, repointTimeline(timeline, rebuilt.seqs));
|
|
6253
|
+
return {
|
|
6254
|
+
id,
|
|
6255
|
+
chatSessionId: chatSessionId$1
|
|
6256
|
+
};
|
|
5982
6257
|
}
|
|
5983
6258
|
//#endregion
|
|
5984
6259
|
//#region src/host/sessions/list-sessions.ts
|
|
@@ -6369,22 +6644,6 @@ var WorldCatalog = class extends Service {
|
|
|
6369
6644
|
}
|
|
6370
6645
|
};
|
|
6371
6646
|
//#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
6647
|
//#region src/host/catalogue/origin.ts
|
|
6389
6648
|
const ORIGIN_FILE = ".catalogue.json";
|
|
6390
6649
|
const MAX_LISTED_CHANGES = 12;
|
|
@@ -6845,7 +7104,7 @@ const object = (value, what) => {
|
|
|
6845
7104
|
};
|
|
6846
7105
|
const text = (value) => typeof value === "string" ? value : "";
|
|
6847
7106
|
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";
|
|
7107
|
+
const kindOf$1 = (type) => type === "sillytavern/character" ? "card" : type === "sillytavern/preset" ? "preset" : type === "sillytavern/lorebook" ? "lorebook" : "world";
|
|
6849
7108
|
const TYPES = {
|
|
6850
7109
|
world: CATALOGUE_WORLD_TYPE,
|
|
6851
7110
|
card: CATALOGUE_CARD_TYPE,
|
|
@@ -6860,7 +7119,7 @@ function worldOf(value) {
|
|
|
6860
7119
|
const visibility = text(metadata["visibility"]);
|
|
6861
7120
|
return {
|
|
6862
7121
|
id,
|
|
6863
|
-
kind: kindOf(text(item["resourceType"])),
|
|
7122
|
+
kind: kindOf$1(text(item["resourceType"])),
|
|
6864
7123
|
name: text(metadata["name"]),
|
|
6865
7124
|
description: text(metadata["description"]),
|
|
6866
7125
|
tags: Array.isArray(metadata["tags"]) ? metadata["tags"].filter((tag) => typeof tag === "string") : [],
|
|
@@ -7359,6 +7618,915 @@ var Sources = class {
|
|
|
7359
7618
|
}
|
|
7360
7619
|
};
|
|
7361
7620
|
//#endregion
|
|
7621
|
+
//#region src/shared/sync.ts
|
|
7622
|
+
const DEFAULT_SYNC = {
|
|
7623
|
+
remote: "",
|
|
7624
|
+
rclone: "",
|
|
7625
|
+
configFile: "",
|
|
7626
|
+
passwordRef: "",
|
|
7627
|
+
intervalMinutes: 0
|
|
7628
|
+
};
|
|
7629
|
+
//#endregion
|
|
7630
|
+
//#region src/host/sync/gate.ts
|
|
7631
|
+
/**
|
|
7632
|
+
* Which units are being synced. A locked unit is neither written nor read until it is released, so rclone never sees a half-written
|
|
7633
|
+
* unit and the app never sees a half-downloaded one.
|
|
7634
|
+
*/
|
|
7635
|
+
var UnitGate = class {
|
|
7636
|
+
held = /* @__PURE__ */ new Map();
|
|
7637
|
+
isLocked(unit) {
|
|
7638
|
+
return this.held.has(unit);
|
|
7639
|
+
}
|
|
7640
|
+
get any() {
|
|
7641
|
+
return this.held.size > 0;
|
|
7642
|
+
}
|
|
7643
|
+
/** @throws {UnitLockedError} when the unit is being synced. */
|
|
7644
|
+
assertFree(unit) {
|
|
7645
|
+
if (this.held.has(unit)) throw new UnitLockedError(`"${unit}" is being synced; try again when it is done`);
|
|
7646
|
+
}
|
|
7647
|
+
/** Resolves once the unit is free, at once when it is. */
|
|
7648
|
+
async whenFree(unit) {
|
|
7649
|
+
while (this.held.has(unit)) await this.held.get(unit);
|
|
7650
|
+
}
|
|
7651
|
+
/** Locks a unit while `work` runs. @throws {UnitLockedError} when it is locked already. */
|
|
7652
|
+
async hold(unit, work) {
|
|
7653
|
+
this.assertFree(unit);
|
|
7654
|
+
let release;
|
|
7655
|
+
this.held.set(unit, new Promise((resolve) => {
|
|
7656
|
+
release = resolve;
|
|
7657
|
+
}));
|
|
7658
|
+
try {
|
|
7659
|
+
return await work();
|
|
7660
|
+
} finally {
|
|
7661
|
+
this.held.delete(unit);
|
|
7662
|
+
release();
|
|
7663
|
+
}
|
|
7664
|
+
}
|
|
7665
|
+
};
|
|
7666
|
+
//#endregion
|
|
7667
|
+
//#region src/host/sync/rclone.ts
|
|
7668
|
+
/** Runs the rclone the person named, with their configuration file, as a child of this process. */
|
|
7669
|
+
function spawnRclone(setup) {
|
|
7670
|
+
return { run: (args, options = {}) => new Promise((resolve, reject) => {
|
|
7671
|
+
const env = {
|
|
7672
|
+
...process.env,
|
|
7673
|
+
...setup.password === void 0 ? {} : { RCLONE_CONFIG_PASS: setup.password }
|
|
7674
|
+
};
|
|
7675
|
+
const child = spawn(setup.binary === "" ? "rclone" : setup.binary, [
|
|
7676
|
+
"--config",
|
|
7677
|
+
setup.configFile,
|
|
7678
|
+
"--ask-password=false",
|
|
7679
|
+
...args
|
|
7680
|
+
], {
|
|
7681
|
+
env,
|
|
7682
|
+
stdio: [
|
|
7683
|
+
"pipe",
|
|
7684
|
+
"pipe",
|
|
7685
|
+
"pipe"
|
|
7686
|
+
],
|
|
7687
|
+
...options.signal === void 0 ? {} : { signal: options.signal }
|
|
7688
|
+
});
|
|
7689
|
+
let stdout = "";
|
|
7690
|
+
let stderr = "";
|
|
7691
|
+
child.stdout.on("data", (chunk) => {
|
|
7692
|
+
stdout += chunk.toString();
|
|
7693
|
+
});
|
|
7694
|
+
child.stderr.on("data", (chunk) => {
|
|
7695
|
+
stderr += chunk.toString();
|
|
7696
|
+
});
|
|
7697
|
+
child.on("error", (error) => {
|
|
7698
|
+
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));
|
|
7699
|
+
});
|
|
7700
|
+
child.on("close", (code) => {
|
|
7701
|
+
resolve({
|
|
7702
|
+
code: code ?? -1,
|
|
7703
|
+
stdout,
|
|
7704
|
+
stderr
|
|
7705
|
+
});
|
|
7706
|
+
});
|
|
7707
|
+
child.stdin.on("error", () => void 0);
|
|
7708
|
+
child.stdin.end(options.input ?? "");
|
|
7709
|
+
}) };
|
|
7710
|
+
}
|
|
7711
|
+
/** What rclone said went wrong: its last error line, without the time and the colours, cut short. */
|
|
7712
|
+
function why(result) {
|
|
7713
|
+
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 !== "");
|
|
7714
|
+
const line = lines.findLast((entry) => /^(ERROR|NOTICE: Failed|CRITICAL)/.test(entry)) ?? lines.at(-1) ?? `rclone stopped with code ${result.code}`;
|
|
7715
|
+
return line.length > 300 ? `${line.slice(0, 300)}…` : line;
|
|
7716
|
+
}
|
|
7717
|
+
//#endregion
|
|
7718
|
+
//#region src/host/sync/state.ts
|
|
7719
|
+
const syncFolder = (root) => join(root, ".sync");
|
|
7720
|
+
/** What this device remembers about its syncing; it lives beside the root's content but is never a unit, so it is never synced. */
|
|
7721
|
+
function readStore(root) {
|
|
7722
|
+
try {
|
|
7723
|
+
const parsed = JSON.parse(readFileSync(join(syncFolder(root), "state.json"), "utf8"));
|
|
7724
|
+
return {
|
|
7725
|
+
device: parsed.device ?? randomUUID(),
|
|
7726
|
+
...parsed.lastRun === void 0 ? {} : { lastRun: parsed.lastRun },
|
|
7727
|
+
units: parsed.units ?? {},
|
|
7728
|
+
ignored: parsed.ignored ?? []
|
|
7729
|
+
};
|
|
7730
|
+
} catch (error) {
|
|
7731
|
+
if (error.code !== "ENOENT") throw error;
|
|
7732
|
+
return {
|
|
7733
|
+
device: randomUUID(),
|
|
7734
|
+
units: {},
|
|
7735
|
+
ignored: []
|
|
7736
|
+
};
|
|
7737
|
+
}
|
|
7738
|
+
}
|
|
7739
|
+
function writeStore(root, store) {
|
|
7740
|
+
const folder = syncFolder(root);
|
|
7741
|
+
mkdirSync(folder, { recursive: true });
|
|
7742
|
+
const temporary = join(folder, `state.json.${process.pid}.tmp`);
|
|
7743
|
+
writeFileSync(temporary, `${JSON.stringify(store, null, 2)}\n`);
|
|
7744
|
+
renameSync(temporary, join(folder, "state.json"));
|
|
7745
|
+
}
|
|
7746
|
+
//#endregion
|
|
7747
|
+
//#region src/host/sync/units.ts
|
|
7748
|
+
/** Folders of the root that are one unit each, whatever they hold. */
|
|
7749
|
+
const SHARED_UNITS = [
|
|
7750
|
+
"prompts",
|
|
7751
|
+
"presets",
|
|
7752
|
+
"lorebooks",
|
|
7753
|
+
"avatars",
|
|
7754
|
+
"ui"
|
|
7755
|
+
];
|
|
7756
|
+
const modeOf = (kind) => kind === "session" ? "whole" : "files";
|
|
7757
|
+
function kindOf(unit) {
|
|
7758
|
+
const [top, name, ...rest] = unit.split("/");
|
|
7759
|
+
if (rest.length > 0) return void 0;
|
|
7760
|
+
if (name === void 0) return SHARED_UNITS.includes(top) ? "shared" : void 0;
|
|
7761
|
+
if (top === "worlds" && !name.startsWith(".") && name !== "") return "world";
|
|
7762
|
+
if (top === "sessions" && isSessionId(name)) return "session";
|
|
7763
|
+
}
|
|
7764
|
+
async function folders(directory) {
|
|
7765
|
+
try {
|
|
7766
|
+
return (await readdir(directory, { withFileTypes: true })).filter((entry) => entry.isDirectory() && !entry.name.startsWith(".")).map((entry) => entry.name);
|
|
7767
|
+
} catch (error) {
|
|
7768
|
+
if (error.code === "ENOENT") return [];
|
|
7769
|
+
throw error;
|
|
7770
|
+
}
|
|
7771
|
+
}
|
|
7772
|
+
/** The units the root holds. */
|
|
7773
|
+
async function localUnits(root) {
|
|
7774
|
+
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);
|
|
7775
|
+
const worlds = (await folders(join(root, "worlds"))).map((name) => `worlds/${name}`);
|
|
7776
|
+
const sessions = (await folders(join(root, "sessions"))).filter(isSessionId).map((name) => `sessions/${name}`);
|
|
7777
|
+
return [
|
|
7778
|
+
...shared,
|
|
7779
|
+
...worlds,
|
|
7780
|
+
...sessions
|
|
7781
|
+
];
|
|
7782
|
+
}
|
|
7783
|
+
/** A short digest of what a folder holds (names, sizes and times), to tell whether it changed since it was last synced. */
|
|
7784
|
+
async function fingerprint(directory) {
|
|
7785
|
+
const lines = [];
|
|
7786
|
+
const walk = async (relative) => {
|
|
7787
|
+
for (const entry of await readdir(join(directory, relative), { withFileTypes: true })) {
|
|
7788
|
+
const path = relative === "" ? entry.name : `${relative}/${entry.name}`;
|
|
7789
|
+
if (entry.isDirectory()) await walk(path);
|
|
7790
|
+
else if (entry.isFile()) {
|
|
7791
|
+
const info = await stat(join(directory, path));
|
|
7792
|
+
lines.push(`${path}\t${info.size}\t${Math.trunc(info.mtimeMs)}`);
|
|
7793
|
+
}
|
|
7794
|
+
}
|
|
7795
|
+
};
|
|
7796
|
+
await walk("");
|
|
7797
|
+
return createHash("sha256").update(lines.sort((left, right) => left.localeCompare(right)).join("\n")).digest("hex");
|
|
7798
|
+
}
|
|
7799
|
+
/** The files that two copies of one file left side by side (`name.conflict1` is this device's, `name.conflict2` the remote's). */
|
|
7800
|
+
async function conflictFiles(directory) {
|
|
7801
|
+
const found = [];
|
|
7802
|
+
const walk = async (relative) => {
|
|
7803
|
+
for (const entry of await readdir(join(directory, relative), { withFileTypes: true })) {
|
|
7804
|
+
const path = relative === "" ? entry.name : `${relative}/${entry.name}`;
|
|
7805
|
+
if (entry.isDirectory()) await walk(path);
|
|
7806
|
+
else if (/\.conflict[12]$/.test(entry.name)) found.push(path);
|
|
7807
|
+
}
|
|
7808
|
+
};
|
|
7809
|
+
await walk("");
|
|
7810
|
+
return found.sort((left, right) => left.localeCompare(right));
|
|
7811
|
+
}
|
|
7812
|
+
//#endregion
|
|
7813
|
+
//#region src/host/sync/service.ts
|
|
7814
|
+
const DIVERGED = {
|
|
7815
|
+
none: void 0,
|
|
7816
|
+
first: "both sides have this and it was never synced here",
|
|
7817
|
+
both: "both sides changed it",
|
|
7818
|
+
removed: "removed on this device but still on the remote",
|
|
7819
|
+
deleted: "deleted on another device, and changed here since"
|
|
7820
|
+
};
|
|
7821
|
+
const BISYNC = [
|
|
7822
|
+
"--resilient",
|
|
7823
|
+
"--recover",
|
|
7824
|
+
"--conflict-resolve",
|
|
7825
|
+
"none",
|
|
7826
|
+
"--create-empty-src-dirs",
|
|
7827
|
+
"--max-lock",
|
|
7828
|
+
"5m"
|
|
7829
|
+
];
|
|
7830
|
+
/** The longest name rclone's file system allows for a file bisync keeps per pair of paths, which is made from both paths. */
|
|
7831
|
+
const BISYNC_NAME_LIMIT = 230;
|
|
7832
|
+
function expand(path) {
|
|
7833
|
+
const value = path.trim();
|
|
7834
|
+
return value === "~" ? homedir() : value.startsWith("~/") ? join(homedir(), value.slice(2)) : value;
|
|
7835
|
+
}
|
|
7836
|
+
/**
|
|
7837
|
+
* 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
|
|
7838
|
+
* folders. A unit is locked while it syncs. Sessions move whole, and when both sides changed one the person chooses which wins; the
|
|
7839
|
+
* 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.
|
|
7840
|
+
*/
|
|
7841
|
+
var SyncService = class {
|
|
7842
|
+
deps;
|
|
7843
|
+
active = false;
|
|
7844
|
+
constructor(deps) {
|
|
7845
|
+
this.deps = deps;
|
|
7846
|
+
}
|
|
7847
|
+
/** Why sync cannot run with the settings as they are, or nothing. */
|
|
7848
|
+
problem() {
|
|
7849
|
+
const config = this.deps.config();
|
|
7850
|
+
if (config.remote.trim() === "") return "sync is off: no remote is set";
|
|
7851
|
+
if (config.configFile.trim() === "") return "the rclone configuration file is not set";
|
|
7852
|
+
const file = expand(config.configFile);
|
|
7853
|
+
if (!isAbsolute(file)) return "the rclone configuration file must be an absolute path";
|
|
7854
|
+
const inside = relative(this.deps.root(), normalize(file));
|
|
7855
|
+
if (inside === "" || !inside.startsWith("..") && !isAbsolute(inside)) return "the rclone configuration file must be outside the roleplay root, or it would be synced";
|
|
7856
|
+
if (!existsSync(file)) return `the rclone configuration file does not exist: ${file}`;
|
|
7857
|
+
}
|
|
7858
|
+
get running() {
|
|
7859
|
+
return this.active;
|
|
7860
|
+
}
|
|
7861
|
+
async status() {
|
|
7862
|
+
const root = this.deps.root();
|
|
7863
|
+
const store = readStore(root);
|
|
7864
|
+
const ids = /* @__PURE__ */ new Set([
|
|
7865
|
+
...await localUnits(root),
|
|
7866
|
+
...Object.keys(store.units),
|
|
7867
|
+
...store.ignored
|
|
7868
|
+
]);
|
|
7869
|
+
const units = [];
|
|
7870
|
+
for (const id of [...ids].sort((left, right) => left.localeCompare(right))) {
|
|
7871
|
+
const kind = kindOf(id);
|
|
7872
|
+
if (kind === void 0) continue;
|
|
7873
|
+
units.push(store.ignored.includes(id) ? {
|
|
7874
|
+
id,
|
|
7875
|
+
kind,
|
|
7876
|
+
state: "ignored",
|
|
7877
|
+
message: "deleted here and kept on the remote; it is not brought back"
|
|
7878
|
+
} : await this.describe(root, id, kind, store.units[id]));
|
|
7879
|
+
}
|
|
7880
|
+
const problem = this.problem();
|
|
7881
|
+
return {
|
|
7882
|
+
...problem === void 0 ? {} : { problem },
|
|
7883
|
+
running: this.active,
|
|
7884
|
+
...store.lastRun === void 0 ? {} : { lastRun: store.lastRun },
|
|
7885
|
+
units
|
|
7886
|
+
};
|
|
7887
|
+
}
|
|
7888
|
+
async describe(root, id, kind, record) {
|
|
7889
|
+
const base = {
|
|
7890
|
+
id,
|
|
7891
|
+
kind,
|
|
7892
|
+
...record?.at === void 0 ? {} : { at: record.at }
|
|
7893
|
+
};
|
|
7894
|
+
const directory = join(root, id);
|
|
7895
|
+
const reading = async (read, otherwise) => {
|
|
7896
|
+
try {
|
|
7897
|
+
return await read();
|
|
7898
|
+
} catch (error) {
|
|
7899
|
+
if (error.code === "ENOENT") return otherwise;
|
|
7900
|
+
throw error;
|
|
7901
|
+
}
|
|
7902
|
+
};
|
|
7903
|
+
const conflicts = existsSync(directory) && modeOf(kind) === "files" ? await reading(() => conflictFiles(directory), []) : [];
|
|
7904
|
+
const removal = record?.diverged === "removed" || record?.diverged === "deleted" ? record.diverged : void 0;
|
|
7905
|
+
const gone = !existsSync(directory) && (record?.at !== void 0 || record?.base !== void 0);
|
|
7906
|
+
const state = async () => {
|
|
7907
|
+
if (this.deps.gate.isLocked(id)) return "busy";
|
|
7908
|
+
if (conflicts.length > 0 || record?.diverged !== void 0) return "conflict";
|
|
7909
|
+
if (record?.failed !== void 0) return "failed";
|
|
7910
|
+
if (gone) return "changed";
|
|
7911
|
+
if (record?.fingerprint === void 0 || !existsSync(directory)) return "new";
|
|
7912
|
+
return await reading(() => fingerprint(directory), void 0) === record.fingerprint ? "synced" : "changed";
|
|
7913
|
+
};
|
|
7914
|
+
const message = record?.failed ?? DIVERGED[record?.diverged ?? "none"] ?? (gone ? "removed on this device; the next run asks what to do" : void 0);
|
|
7915
|
+
return {
|
|
7916
|
+
...base,
|
|
7917
|
+
state: await state(),
|
|
7918
|
+
...message === void 0 ? {} : { message },
|
|
7919
|
+
...conflicts.length > 0 ? { conflicts } : {},
|
|
7920
|
+
...removal === void 0 ? {} : { removal }
|
|
7921
|
+
};
|
|
7922
|
+
}
|
|
7923
|
+
/**
|
|
7924
|
+
* Syncs every unit (or only `only`) that is not busy. A unit that cannot be synced is reported and the others go on.
|
|
7925
|
+
* @throws {SyncRefusedError} when sync is off or misconfigured, or a run is under way.
|
|
7926
|
+
*/
|
|
7927
|
+
async run(only) {
|
|
7928
|
+
const problem = this.problem();
|
|
7929
|
+
if (problem !== void 0) throw new SyncRefusedError(problem);
|
|
7930
|
+
if (this.active) throw new SyncRefusedError("a sync is already running");
|
|
7931
|
+
this.active = true;
|
|
7932
|
+
try {
|
|
7933
|
+
const rclone = await this.client();
|
|
7934
|
+
const root = this.deps.root();
|
|
7935
|
+
const store = readStore(root);
|
|
7936
|
+
const remote = await this.remoteUnits(rclone);
|
|
7937
|
+
const deleted = new Set(await this.tombstones(rclone));
|
|
7938
|
+
const local = await localUnits(root);
|
|
7939
|
+
const wanted = (id) => !store.ignored.includes(id) && (only === void 0 || only.includes(id));
|
|
7940
|
+
const ids = [.../* @__PURE__ */ new Set([
|
|
7941
|
+
...local,
|
|
7942
|
+
...remote,
|
|
7943
|
+
...deleted
|
|
7944
|
+
])].filter(wanted).sort((left, right) => left.localeCompare(right));
|
|
7945
|
+
for (const id of ids) if (deleted.has(id)) await this.syncDeleted(id, local.includes(id));
|
|
7946
|
+
else await this.syncUnit(rclone, id, local.includes(id), remote.includes(id));
|
|
7947
|
+
const after = readStore(root);
|
|
7948
|
+
after.lastRun = this.now();
|
|
7949
|
+
writeStore(root, after);
|
|
7950
|
+
return { units: (await this.status()).units.filter((unit) => ids.includes(unit.id)) };
|
|
7951
|
+
} finally {
|
|
7952
|
+
this.active = false;
|
|
7953
|
+
}
|
|
7954
|
+
}
|
|
7955
|
+
/** Runs when the interval has passed since the last run; the caller calls it on a timer. */
|
|
7956
|
+
async auto() {
|
|
7957
|
+
const minutes = this.deps.config().intervalMinutes;
|
|
7958
|
+
if (minutes <= 0 || this.active || this.problem() !== void 0) return;
|
|
7959
|
+
const last = readStore(this.deps.root()).lastRun;
|
|
7960
|
+
if (last !== void 0 && Date.now() - Date.parse(last) < minutes * 6e4) return;
|
|
7961
|
+
await this.run().catch((error) => {
|
|
7962
|
+
this.deps.problem(`sync: ${error instanceof Error ? error.message : String(error)}`);
|
|
7963
|
+
});
|
|
7964
|
+
}
|
|
7965
|
+
/**
|
|
7966
|
+
* 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.
|
|
7967
|
+
* @throws {SyncRefusedError} when the unit has nothing to choose or is busy.
|
|
7968
|
+
*/
|
|
7969
|
+
async resolve(id, side) {
|
|
7970
|
+
const problem = this.problem();
|
|
7971
|
+
if (problem !== void 0) throw new SyncRefusedError(problem);
|
|
7972
|
+
const kind = kindOf(id);
|
|
7973
|
+
if (kind === void 0) throw new SyncRefusedError(`"${id}" is not a unit`);
|
|
7974
|
+
if (this.active) throw new SyncRefusedError("a sync is running; choose when it is over");
|
|
7975
|
+
const root = this.deps.root();
|
|
7976
|
+
const directory = join(root, id);
|
|
7977
|
+
const conflicts = existsSync(directory) && modeOf(kind) === "files" ? await conflictFiles(directory) : [];
|
|
7978
|
+
const record = readStore(root).units[id];
|
|
7979
|
+
if (conflicts.length === 0 && record?.diverged === void 0) throw new SyncRefusedError(`"${id}" has nothing to choose`);
|
|
7980
|
+
if (kind === "session") {
|
|
7981
|
+
const reason = this.deps.busy(id.slice(9));
|
|
7982
|
+
if (reason !== void 0) throw new SyncRefusedError(`"${id}" cannot be changed now: ${reason}`);
|
|
7983
|
+
}
|
|
7984
|
+
this.active = true;
|
|
7985
|
+
try {
|
|
7986
|
+
const rclone = await this.client();
|
|
7987
|
+
await this.guard(id, kind, async () => {
|
|
7988
|
+
if (conflicts.length > 0) {
|
|
7989
|
+
await this.keepCopies(directory, conflicts, side);
|
|
7990
|
+
await this.bisync(rclone, id, false);
|
|
7991
|
+
} else if (record?.diverged === "removed") {
|
|
7992
|
+
if (side === "local") await this.erase(rclone, id, kind);
|
|
7993
|
+
else await this.bring(rclone, id, kind);
|
|
7994
|
+
} else if (record?.diverged === "deleted") {
|
|
7995
|
+
if (side === "remote") await this.removeLocal(id, kind);
|
|
7996
|
+
else {
|
|
7997
|
+
await this.clearTombstone(rclone, id);
|
|
7998
|
+
await this.bring(rclone, id, kind, "local");
|
|
7999
|
+
}
|
|
8000
|
+
} else await this.bring(rclone, id, kind, side);
|
|
8001
|
+
});
|
|
8002
|
+
} finally {
|
|
8003
|
+
this.active = false;
|
|
8004
|
+
}
|
|
8005
|
+
return (await this.status()).units.find((unit) => unit.id === id);
|
|
8006
|
+
}
|
|
8007
|
+
/** Makes one side the unit's content: the remote's copy is downloaded, or this device's is uploaded. */
|
|
8008
|
+
async bring(rclone, id, kind, side = "remote") {
|
|
8009
|
+
const directory = join(this.deps.root(), id);
|
|
8010
|
+
if (modeOf(kind) === "whole") {
|
|
8011
|
+
if (side === "local") await this.push(rclone, id);
|
|
8012
|
+
else await this.pull(rclone, id, await this.revision(rclone, id));
|
|
8013
|
+
return;
|
|
8014
|
+
}
|
|
8015
|
+
const remote = this.remotePath(id);
|
|
8016
|
+
if (side === "remote") await mkdir(dirname(directory), { recursive: true });
|
|
8017
|
+
await this.mirror(rclone, side === "local" ? directory : remote, side === "local" ? remote : directory);
|
|
8018
|
+
await this.bisync(rclone, id, true);
|
|
8019
|
+
}
|
|
8020
|
+
/**
|
|
8021
|
+
* 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
|
|
8022
|
+
* going, and every device that syncs later deletes its own copy.
|
|
8023
|
+
* @throws {SyncRefusedError} when sync is off or running, or the unit is not a session or a world.
|
|
8024
|
+
*/
|
|
8025
|
+
async deleteRemote(id) {
|
|
8026
|
+
const problem = this.problem();
|
|
8027
|
+
if (problem !== void 0) throw new SyncRefusedError(problem);
|
|
8028
|
+
const kind = kindOf(id);
|
|
8029
|
+
if (kind !== "session" && kind !== "world") throw new SyncRefusedError(`"${id}" is not a session or a world`);
|
|
8030
|
+
if (this.active) throw new SyncRefusedError("a sync is running; delete when it is over");
|
|
8031
|
+
this.active = true;
|
|
8032
|
+
try {
|
|
8033
|
+
await this.erase(await this.client(), id, kind);
|
|
8034
|
+
} finally {
|
|
8035
|
+
this.active = false;
|
|
8036
|
+
}
|
|
8037
|
+
}
|
|
8038
|
+
async erase(rclone, id, kind) {
|
|
8039
|
+
await this.writeTombstone(rclone, id);
|
|
8040
|
+
const purged = await rclone.run(["purge", this.remotePath(id)]);
|
|
8041
|
+
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);
|
|
8042
|
+
if (kind === "session") await rclone.run(["deletefile", this.revisionPath(id)]);
|
|
8043
|
+
this.drop(id);
|
|
8044
|
+
}
|
|
8045
|
+
/** 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`. */
|
|
8046
|
+
forget(id) {
|
|
8047
|
+
if (kindOf(id) === void 0) throw new SyncRefusedError(`"${id}" is not a unit`);
|
|
8048
|
+
const root = this.deps.root();
|
|
8049
|
+
const store = readStore(root);
|
|
8050
|
+
if (!store.ignored.includes(id)) store.ignored.push(id);
|
|
8051
|
+
delete store.units[id];
|
|
8052
|
+
writeStore(root, store);
|
|
8053
|
+
}
|
|
8054
|
+
/** Takes a unit off the ignored list: the next run brings the remote's copy here again. */
|
|
8055
|
+
restore(id) {
|
|
8056
|
+
this.drop(id);
|
|
8057
|
+
}
|
|
8058
|
+
drop(id) {
|
|
8059
|
+
const root = this.deps.root();
|
|
8060
|
+
const store = readStore(root);
|
|
8061
|
+
store.ignored = store.ignored.filter((entry) => entry !== id);
|
|
8062
|
+
delete store.units[id];
|
|
8063
|
+
writeStore(root, store);
|
|
8064
|
+
}
|
|
8065
|
+
now() {
|
|
8066
|
+
return (this.deps.now?.() ?? /* @__PURE__ */ new Date()).toISOString();
|
|
8067
|
+
}
|
|
8068
|
+
async client() {
|
|
8069
|
+
const config = this.deps.config();
|
|
8070
|
+
const password = await this.deps.password();
|
|
8071
|
+
const setup = {
|
|
8072
|
+
binary: expand(config.rclone),
|
|
8073
|
+
configFile: expand(config.configFile),
|
|
8074
|
+
...password === void 0 ? {} : { password }
|
|
8075
|
+
};
|
|
8076
|
+
return (this.deps.rclone ?? spawnRclone)(setup);
|
|
8077
|
+
}
|
|
8078
|
+
remotePath(id) {
|
|
8079
|
+
const remote = this.deps.config().remote.trim();
|
|
8080
|
+
return remote.endsWith(":") || remote.endsWith("/") ? `${remote}${id}` : `${remote}/${id}`;
|
|
8081
|
+
}
|
|
8082
|
+
async remoteUnits(rclone) {
|
|
8083
|
+
const listed = async (prefix) => {
|
|
8084
|
+
const base = this.deps.config().remote.trim();
|
|
8085
|
+
const path = prefix === "" ? base : this.remotePath(prefix);
|
|
8086
|
+
const result = await rclone.run([
|
|
8087
|
+
"lsf",
|
|
8088
|
+
"--dirs-only",
|
|
8089
|
+
path
|
|
8090
|
+
]);
|
|
8091
|
+
if (result.code === 3) return [];
|
|
8092
|
+
if (result.code !== 0) throw new RcloneError(`cannot list ${path}: ${why(result)}`, result.code);
|
|
8093
|
+
return result.stdout.split("\n").map((line) => line.trim().replace(/\/$/, "")).filter((line) => line !== "" && !line.startsWith("."));
|
|
8094
|
+
};
|
|
8095
|
+
const [top, worlds, sessions] = await Promise.all([
|
|
8096
|
+
listed(""),
|
|
8097
|
+
listed("worlds"),
|
|
8098
|
+
listed("sessions")
|
|
8099
|
+
]);
|
|
8100
|
+
return [
|
|
8101
|
+
...top.filter((name) => SHARED_UNITS.includes(name)),
|
|
8102
|
+
...worlds.map((name) => `worlds/${name}`),
|
|
8103
|
+
...sessions.filter(isSessionId).map((name) => `sessions/${name}`)
|
|
8104
|
+
];
|
|
8105
|
+
}
|
|
8106
|
+
/** Locks the unit, records how it went and never lets one unit's failure stop the run. */
|
|
8107
|
+
async syncUnit(rclone, id, local, remote) {
|
|
8108
|
+
const kind = kindOf(id);
|
|
8109
|
+
if (kind === void 0) return;
|
|
8110
|
+
const root = this.deps.root();
|
|
8111
|
+
const note = (change) => {
|
|
8112
|
+
const store = readStore(root);
|
|
8113
|
+
const record = store.units[id] ?? {};
|
|
8114
|
+
change(record);
|
|
8115
|
+
store.units[id] = record;
|
|
8116
|
+
writeStore(root, store);
|
|
8117
|
+
};
|
|
8118
|
+
const known = readStore(root).units[id];
|
|
8119
|
+
if (!local && remote && (known?.at !== void 0 || known?.base !== void 0)) {
|
|
8120
|
+
note((record) => {
|
|
8121
|
+
record.diverged = "removed";
|
|
8122
|
+
delete record.failed;
|
|
8123
|
+
});
|
|
8124
|
+
return;
|
|
8125
|
+
}
|
|
8126
|
+
if (kind === "session") {
|
|
8127
|
+
const reason = this.deps.busy(id.slice(9));
|
|
8128
|
+
if (reason !== void 0) {
|
|
8129
|
+
note((record) => {
|
|
8130
|
+
record.failed = `skipped: ${reason}`;
|
|
8131
|
+
});
|
|
8132
|
+
return;
|
|
8133
|
+
}
|
|
8134
|
+
}
|
|
8135
|
+
try {
|
|
8136
|
+
await this.guard(id, kind, async () => {
|
|
8137
|
+
await (modeOf(kind) === "whole" ? this.syncWhole(rclone, id, local) : this.syncFiles(rclone, id, local, remote));
|
|
8138
|
+
});
|
|
8139
|
+
} catch (error) {
|
|
8140
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
8141
|
+
note((record) => {
|
|
8142
|
+
record.failed = message;
|
|
8143
|
+
});
|
|
8144
|
+
this.deps.problem(`sync ${id}: ${message}`);
|
|
8145
|
+
}
|
|
8146
|
+
}
|
|
8147
|
+
/** 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. */
|
|
8148
|
+
async syncDeleted(id, local) {
|
|
8149
|
+
const kind = kindOf(id);
|
|
8150
|
+
if (kind !== "session" && kind !== "world") return;
|
|
8151
|
+
if (!local) {
|
|
8152
|
+
this.drop(id);
|
|
8153
|
+
return;
|
|
8154
|
+
}
|
|
8155
|
+
const record = readStore(this.deps.root()).units[id];
|
|
8156
|
+
const synced = record?.at !== void 0 || record?.base !== void 0;
|
|
8157
|
+
const changed = synced && await fingerprint(join(this.deps.root(), id)) !== record.fingerprint;
|
|
8158
|
+
if (!synced || changed) {
|
|
8159
|
+
await this.record(id, (entry) => {
|
|
8160
|
+
entry.diverged = "deleted";
|
|
8161
|
+
delete entry.failed;
|
|
8162
|
+
});
|
|
8163
|
+
return;
|
|
8164
|
+
}
|
|
8165
|
+
if (kind === "session") {
|
|
8166
|
+
const reason = this.deps.busy(id.slice(9));
|
|
8167
|
+
if (reason !== void 0) {
|
|
8168
|
+
await this.record(id, (entry) => {
|
|
8169
|
+
entry.failed = `deleted on another device; skipped: ${reason}`;
|
|
8170
|
+
});
|
|
8171
|
+
return;
|
|
8172
|
+
}
|
|
8173
|
+
}
|
|
8174
|
+
try {
|
|
8175
|
+
await this.guard(id, kind, async () => {
|
|
8176
|
+
await this.removeLocal(id, kind);
|
|
8177
|
+
});
|
|
8178
|
+
} catch (error) {
|
|
8179
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
8180
|
+
await this.record(id, (entry) => {
|
|
8181
|
+
entry.failed = message;
|
|
8182
|
+
});
|
|
8183
|
+
this.deps.problem(`sync ${id}: ${message}`);
|
|
8184
|
+
}
|
|
8185
|
+
}
|
|
8186
|
+
/** A session is deleted like any other; a world's folder is moved to the root's trash, which is emptied after a while. */
|
|
8187
|
+
async removeLocal(id, kind) {
|
|
8188
|
+
if (kind === "session") await this.deps.remove(id.slice(9));
|
|
8189
|
+
else {
|
|
8190
|
+
const trash = join(this.deps.root(), ".trash", "worlds");
|
|
8191
|
+
await mkdir(trash, { recursive: true });
|
|
8192
|
+
await rename(join(this.deps.root(), id), join(trash, `${basename(id)}-${Date.now()}`));
|
|
8193
|
+
}
|
|
8194
|
+
this.drop(id);
|
|
8195
|
+
}
|
|
8196
|
+
tombstonePath(id) {
|
|
8197
|
+
return this.remoteFile(`.tombstones/${encodeURIComponent(id)}.json`);
|
|
8198
|
+
}
|
|
8199
|
+
async tombstones(rclone) {
|
|
8200
|
+
const result = await rclone.run([
|
|
8201
|
+
"lsf",
|
|
8202
|
+
"--files-only",
|
|
8203
|
+
this.remoteFile(".tombstones")
|
|
8204
|
+
]);
|
|
8205
|
+
if (result.code === 3) return [];
|
|
8206
|
+
if (result.code !== 0) throw new RcloneError(`cannot list the deleted units: ${why(result)}`, result.code);
|
|
8207
|
+
return result.stdout.split("\n").map((line) => line.trim()).filter((line) => line.endsWith(".json")).map((line) => decodeURIComponent(line.slice(0, -5)));
|
|
8208
|
+
}
|
|
8209
|
+
async writeTombstone(rclone, id) {
|
|
8210
|
+
const result = await rclone.run(["rcat", this.tombstonePath(id)], { input: JSON.stringify({
|
|
8211
|
+
device: readStore(this.deps.root()).device,
|
|
8212
|
+
at: this.now()
|
|
8213
|
+
}) });
|
|
8214
|
+
if (result.code !== 0) throw new RcloneError(`cannot mark ${id} deleted on the remote: ${why(result)}`, result.code);
|
|
8215
|
+
}
|
|
8216
|
+
async clearTombstone(rclone, id) {
|
|
8217
|
+
const result = await rclone.run(["deletefile", this.tombstonePath(id)]);
|
|
8218
|
+
if (result.code !== 0 && result.code !== 3 && result.code !== 4) throw new RcloneError(`cannot remove the deletion mark of ${id}: ${why(result)}`, result.code);
|
|
8219
|
+
}
|
|
8220
|
+
async guard(id, kind, work) {
|
|
8221
|
+
await this.deps.gate.hold(id, async () => {
|
|
8222
|
+
if (kind === "session") await this.deps.idle(id.slice(9));
|
|
8223
|
+
await work();
|
|
8224
|
+
if (kind !== "session") await this.deps.changed(id);
|
|
8225
|
+
});
|
|
8226
|
+
}
|
|
8227
|
+
async record(id, change) {
|
|
8228
|
+
const root = this.deps.root();
|
|
8229
|
+
const store = readStore(root);
|
|
8230
|
+
const record = store.units[id] ?? {};
|
|
8231
|
+
change(record);
|
|
8232
|
+
store.units[id] = record;
|
|
8233
|
+
writeStore(root, store);
|
|
8234
|
+
}
|
|
8235
|
+
async settled(id, base) {
|
|
8236
|
+
const print = await fingerprint(join(this.deps.root(), id));
|
|
8237
|
+
await this.record(id, (record) => {
|
|
8238
|
+
delete record.failed;
|
|
8239
|
+
delete record.diverged;
|
|
8240
|
+
record.fingerprint = print;
|
|
8241
|
+
record.at = this.now();
|
|
8242
|
+
record.ready = true;
|
|
8243
|
+
if (base !== void 0) record.base = base;
|
|
8244
|
+
});
|
|
8245
|
+
}
|
|
8246
|
+
remoteFile(file) {
|
|
8247
|
+
const remote = this.deps.config().remote.trim();
|
|
8248
|
+
return remote.endsWith(":") || remote.endsWith("/") ? `${remote}${file}` : `${remote}/${file}`;
|
|
8249
|
+
}
|
|
8250
|
+
revisionPath(id) {
|
|
8251
|
+
return this.remoteFile(`.revisions/${id.replaceAll("/", "__")}.json`);
|
|
8252
|
+
}
|
|
8253
|
+
async revision(rclone, id) {
|
|
8254
|
+
const result = await rclone.run(["cat", this.revisionPath(id)]);
|
|
8255
|
+
if (result.code === 3 || result.code === 4) return void 0;
|
|
8256
|
+
if (result.code !== 0) throw new RcloneError(`cannot read the revision of ${id}: ${why(result)}`, result.code);
|
|
8257
|
+
return JSON.parse(result.stdout);
|
|
8258
|
+
}
|
|
8259
|
+
async writeRevision(rclone, id, revision) {
|
|
8260
|
+
const result = await rclone.run(["rcat", this.revisionPath(id)], { input: JSON.stringify(revision) });
|
|
8261
|
+
if (result.code !== 0) throw new RcloneError(`cannot write the revision of ${id}: ${why(result)}`, result.code);
|
|
8262
|
+
}
|
|
8263
|
+
async mirror(rclone, from, to) {
|
|
8264
|
+
const result = await rclone.run([
|
|
8265
|
+
"sync",
|
|
8266
|
+
from,
|
|
8267
|
+
to,
|
|
8268
|
+
"--checksum",
|
|
8269
|
+
"--create-empty-src-dirs"
|
|
8270
|
+
]);
|
|
8271
|
+
if (result.code !== 0) throw new RcloneError(`rclone sync failed: ${why(result)}`, result.code);
|
|
8272
|
+
}
|
|
8273
|
+
/** 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. */
|
|
8274
|
+
async syncWhole(rclone, id, local) {
|
|
8275
|
+
const root = this.deps.root();
|
|
8276
|
+
const record = readStore(root).units[id];
|
|
8277
|
+
const remote = await this.revision(rclone, id);
|
|
8278
|
+
if (!local) {
|
|
8279
|
+
if (remote !== void 0) await this.pull(rclone, id, remote);
|
|
8280
|
+
return;
|
|
8281
|
+
}
|
|
8282
|
+
if (remote === void 0) {
|
|
8283
|
+
await this.push(rclone, id);
|
|
8284
|
+
return;
|
|
8285
|
+
}
|
|
8286
|
+
if (!remote.complete && remote.device === readStore(root).device) {
|
|
8287
|
+
await this.push(rclone, id);
|
|
8288
|
+
return;
|
|
8289
|
+
}
|
|
8290
|
+
const changedHere = record?.fingerprint === void 0 || await fingerprint(join(root, id)) !== record.fingerprint;
|
|
8291
|
+
const changedThere = record?.base !== remote.rev;
|
|
8292
|
+
if (!changedHere && !changedThere) {
|
|
8293
|
+
if (record?.chat === "stale") await this.rebuildChat(id);
|
|
8294
|
+
else await this.settled(id);
|
|
8295
|
+
return;
|
|
8296
|
+
}
|
|
8297
|
+
if (changedThere && !changedHere) {
|
|
8298
|
+
await this.pull(rclone, id, remote);
|
|
8299
|
+
return;
|
|
8300
|
+
}
|
|
8301
|
+
if (!changedThere) {
|
|
8302
|
+
await this.push(rclone, id);
|
|
8303
|
+
return;
|
|
8304
|
+
}
|
|
8305
|
+
await this.record(id, (entry) => {
|
|
8306
|
+
entry.diverged = record?.base === void 0 ? "first" : "both";
|
|
8307
|
+
delete entry.failed;
|
|
8308
|
+
});
|
|
8309
|
+
}
|
|
8310
|
+
async push(rclone, id) {
|
|
8311
|
+
if (kindOf(id) === "session") await this.deps.release(id.slice(9));
|
|
8312
|
+
const revision = {
|
|
8313
|
+
rev: randomUUID(),
|
|
8314
|
+
device: readStore(this.deps.root()).device,
|
|
8315
|
+
at: this.now(),
|
|
8316
|
+
complete: false
|
|
8317
|
+
};
|
|
8318
|
+
await this.writeRevision(rclone, id, revision);
|
|
8319
|
+
await this.mirror(rclone, join(this.deps.root(), id), this.remotePath(id));
|
|
8320
|
+
await this.writeRevision(rclone, id, {
|
|
8321
|
+
...revision,
|
|
8322
|
+
complete: true
|
|
8323
|
+
});
|
|
8324
|
+
await this.settled(id, revision.rev);
|
|
8325
|
+
}
|
|
8326
|
+
async pull(rclone, id, remote) {
|
|
8327
|
+
if (remote === void 0) throw new SyncRefusedError(`the remote has no copy of "${id}"`);
|
|
8328
|
+
if (!remote.complete) throw new SyncRefusedError(`the copy of "${id}" on the remote is still being uploaded; try again later`);
|
|
8329
|
+
const session = id.slice(9);
|
|
8330
|
+
await this.deps.release(session);
|
|
8331
|
+
const directory = join(this.deps.root(), id);
|
|
8332
|
+
await mkdir(directory, { recursive: true });
|
|
8333
|
+
await this.mirror(rclone, this.remotePath(id), directory);
|
|
8334
|
+
await this.settled(id, remote.rev);
|
|
8335
|
+
await this.rebuildChat(id);
|
|
8336
|
+
}
|
|
8337
|
+
/** 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. */
|
|
8338
|
+
async rebuildChat(id) {
|
|
8339
|
+
await this.record(id, (record) => {
|
|
8340
|
+
record.chat = "stale";
|
|
8341
|
+
});
|
|
8342
|
+
await this.deps.pulled(id.slice(9));
|
|
8343
|
+
await this.settled(id);
|
|
8344
|
+
await this.record(id, (record) => {
|
|
8345
|
+
delete record.chat;
|
|
8346
|
+
});
|
|
8347
|
+
}
|
|
8348
|
+
async syncFiles(rclone, id, local, remote) {
|
|
8349
|
+
const directory = join(this.deps.root(), id);
|
|
8350
|
+
if (!local && !remote) return;
|
|
8351
|
+
if (!local) {
|
|
8352
|
+
await mkdir(dirname(directory), { recursive: true });
|
|
8353
|
+
await this.mirror(rclone, this.remotePath(id), directory);
|
|
8354
|
+
await this.bisync(rclone, id, true);
|
|
8355
|
+
return;
|
|
8356
|
+
}
|
|
8357
|
+
if (!remote) {
|
|
8358
|
+
await this.mirror(rclone, directory, this.remotePath(id));
|
|
8359
|
+
await this.bisync(rclone, id, true);
|
|
8360
|
+
return;
|
|
8361
|
+
}
|
|
8362
|
+
if (readStore(this.deps.root()).units[id]?.ready !== true) {
|
|
8363
|
+
const same = await rclone.run([
|
|
8364
|
+
"check",
|
|
8365
|
+
directory,
|
|
8366
|
+
this.remotePath(id),
|
|
8367
|
+
"--one-way"
|
|
8368
|
+
]);
|
|
8369
|
+
const other = await rclone.run([
|
|
8370
|
+
"check",
|
|
8371
|
+
this.remotePath(id),
|
|
8372
|
+
directory,
|
|
8373
|
+
"--one-way"
|
|
8374
|
+
]);
|
|
8375
|
+
if (same.code === 0 && other.code === 0) await this.bisync(rclone, id, true);
|
|
8376
|
+
else await this.record(id, (record) => {
|
|
8377
|
+
record.diverged = "first";
|
|
8378
|
+
delete record.failed;
|
|
8379
|
+
});
|
|
8380
|
+
return;
|
|
8381
|
+
}
|
|
8382
|
+
await this.bisync(rclone, id, false);
|
|
8383
|
+
}
|
|
8384
|
+
/** `baseline` makes the two sides the starting point of later runs; it is only used when they are known to be the same. */
|
|
8385
|
+
async bisync(rclone, id, baseline) {
|
|
8386
|
+
const directory = join(this.deps.root(), id);
|
|
8387
|
+
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");
|
|
8388
|
+
const result = await rclone.run([
|
|
8389
|
+
"bisync",
|
|
8390
|
+
directory,
|
|
8391
|
+
this.remotePath(id),
|
|
8392
|
+
"--workdir",
|
|
8393
|
+
join(syncFolder(this.deps.root()), "bisync"),
|
|
8394
|
+
...BISYNC,
|
|
8395
|
+
...baseline ? ["--resync"] : []
|
|
8396
|
+
]);
|
|
8397
|
+
if (result.code !== 0) throw new RcloneError(`rclone bisync failed: ${why(result)}`, result.code);
|
|
8398
|
+
if ((await conflictFiles(directory)).length > 0) {
|
|
8399
|
+
await this.record(id, (record) => {
|
|
8400
|
+
delete record.failed;
|
|
8401
|
+
record.ready = true;
|
|
8402
|
+
record.at = this.now();
|
|
8403
|
+
});
|
|
8404
|
+
return;
|
|
8405
|
+
}
|
|
8406
|
+
await this.settled(id);
|
|
8407
|
+
}
|
|
8408
|
+
/** For each file two copies were kept of: the chosen side's copy takes the file's name and the other is removed. */
|
|
8409
|
+
async keepCopies(directory, conflicts, side) {
|
|
8410
|
+
const keep = side === "local" ? "conflict1" : "conflict2";
|
|
8411
|
+
for (const path of conflicts) {
|
|
8412
|
+
const original = path.replace(/\.conflict[12]$/, "");
|
|
8413
|
+
if (path.endsWith(keep)) {
|
|
8414
|
+
await rm(join(directory, original), { force: true });
|
|
8415
|
+
await rename(join(directory, path), join(directory, original));
|
|
8416
|
+
} else await rm(join(directory, path), { force: true });
|
|
8417
|
+
}
|
|
8418
|
+
}
|
|
8419
|
+
};
|
|
8420
|
+
//#endregion
|
|
8421
|
+
//#region src/host/sync/wire.ts
|
|
8422
|
+
/** The folder that a request to the API writes or reads, if it is one of the syncable ones. */
|
|
8423
|
+
async function unitsOf(url, worlds) {
|
|
8424
|
+
const path = url.pathname.slice(`/${API_PREFIX}`.length);
|
|
8425
|
+
const id = url.searchParams.get("id");
|
|
8426
|
+
const units = [];
|
|
8427
|
+
if (id !== null && path.startsWith("/sessions") && isSessionId(id)) units.push(`sessions/${id}`);
|
|
8428
|
+
else if (id !== null && id !== "" && path.startsWith("/worlds")) {
|
|
8429
|
+
const world = await worlds.find(id);
|
|
8430
|
+
if (world !== void 0) units.push(`worlds/${basename(world.directory)}`);
|
|
8431
|
+
}
|
|
8432
|
+
if (path.startsWith("/presets")) units.push("presets");
|
|
8433
|
+
if (path.startsWith("/lorebooks")) units.push("lorebooks");
|
|
8434
|
+
if (path.startsWith("/avatars") || path.startsWith("/worlds/avatars")) units.push("avatars");
|
|
8435
|
+
return units;
|
|
8436
|
+
}
|
|
8437
|
+
/** Builds the sync service with the gate that keeps a unit untouched while it syncs, and hooks it up to the story's turns. */
|
|
8438
|
+
function createSync(ctx, wiring) {
|
|
8439
|
+
const gate = new UnitGate();
|
|
8440
|
+
const store = () => ctx.get("credentials");
|
|
8441
|
+
const ref = () => wiring.config().passwordRef.trim() || "ROLEPLAY_SYNC_RCLONE_CONFIG_PASS";
|
|
8442
|
+
const sessions = () => join(wiring.root(), "sessions");
|
|
8443
|
+
const service = new SyncService({
|
|
8444
|
+
root: wiring.root,
|
|
8445
|
+
config: wiring.config,
|
|
8446
|
+
password: async () => (await store()?.resolve(ref()))?.value,
|
|
8447
|
+
gate,
|
|
8448
|
+
busy: (session) => {
|
|
8449
|
+
const chat = chatSessionId(session);
|
|
8450
|
+
if (wiring.orchestration.playing(chat)) return "a turn is being played";
|
|
8451
|
+
if (wiring.orchestration.reflecting(chat)) return "the characters are reflecting";
|
|
8452
|
+
},
|
|
8453
|
+
idle: (session) => wiring.orchestration.quiesce(chatSessionId(session)),
|
|
8454
|
+
release: async (session) => {
|
|
8455
|
+
await wiring.orchestration.halt(chatSessionId(session));
|
|
8456
|
+
wiring.state.closeSession(sessions(), session);
|
|
8457
|
+
},
|
|
8458
|
+
pulled: wiring.rebuild,
|
|
8459
|
+
remove: wiring.remove,
|
|
8460
|
+
changed: async () => {
|
|
8461
|
+
await wiring.worlds.list({ refresh: true });
|
|
8462
|
+
},
|
|
8463
|
+
problem: (message) => {
|
|
8464
|
+
ctx.logger.warn(message);
|
|
8465
|
+
}
|
|
8466
|
+
});
|
|
8467
|
+
ctx.effect(() => {
|
|
8468
|
+
wiring.orchestration.unitFree = async (chat) => {
|
|
8469
|
+
const session = roleplaySessionOf(chat);
|
|
8470
|
+
if (session !== void 0) await gate.whenFree(`sessions/${session}`);
|
|
8471
|
+
};
|
|
8472
|
+
return () => {
|
|
8473
|
+
wiring.orchestration.unitFree = () => Promise.resolve();
|
|
8474
|
+
};
|
|
8475
|
+
}, "roleplay: story sync gate");
|
|
8476
|
+
ctx.effect(() => {
|
|
8477
|
+
const timer = setInterval(() => {
|
|
8478
|
+
service.auto();
|
|
8479
|
+
}, 6e4);
|
|
8480
|
+
timer.unref();
|
|
8481
|
+
return () => {
|
|
8482
|
+
clearInterval(timer);
|
|
8483
|
+
};
|
|
8484
|
+
}, "roleplay: sync timer");
|
|
8485
|
+
const describe = async (requested) => {
|
|
8486
|
+
const credentials = store();
|
|
8487
|
+
const reference = requested ?? ref();
|
|
8488
|
+
if (credentials === void 0) return {
|
|
8489
|
+
configured: false,
|
|
8490
|
+
writable: false,
|
|
8491
|
+
ref: reference
|
|
8492
|
+
};
|
|
8493
|
+
const status = await credentials.describe(reference);
|
|
8494
|
+
return {
|
|
8495
|
+
configured: status.configured,
|
|
8496
|
+
...status.source === void 0 ? {} : { source: status.source },
|
|
8497
|
+
writable: status.writable,
|
|
8498
|
+
ref: reference
|
|
8499
|
+
};
|
|
8500
|
+
};
|
|
8501
|
+
return {
|
|
8502
|
+
service,
|
|
8503
|
+
api: {
|
|
8504
|
+
status: () => service.status(),
|
|
8505
|
+
run: (only) => service.run(only),
|
|
8506
|
+
resolve: (unit, side) => service.resolve(unit, side),
|
|
8507
|
+
forget: (unit) => {
|
|
8508
|
+
service.forget(unit);
|
|
8509
|
+
},
|
|
8510
|
+
restore: (unit) => {
|
|
8511
|
+
service.restore(unit);
|
|
8512
|
+
},
|
|
8513
|
+
password: {
|
|
8514
|
+
describe,
|
|
8515
|
+
keep: async (value, requested) => {
|
|
8516
|
+
const credentials = store();
|
|
8517
|
+
if (credentials === void 0) throw new SyncRefusedError("this profile has no credential store to keep the password in");
|
|
8518
|
+
const reference = requested ?? ref();
|
|
8519
|
+
if (value === "") await credentials.unset(reference);
|
|
8520
|
+
else await credentials.set(reference, value);
|
|
8521
|
+
return await describe(reference);
|
|
8522
|
+
}
|
|
8523
|
+
}
|
|
8524
|
+
},
|
|
8525
|
+
gate,
|
|
8526
|
+
locked: async (url) => (await unitsOf(url, wiring.worlds)).find((unit) => gate.isLocked(unit))
|
|
8527
|
+
};
|
|
8528
|
+
}
|
|
8529
|
+
//#endregion
|
|
7362
8530
|
//#region src/host/speech/service.ts
|
|
7363
8531
|
async function voicesOf(service) {
|
|
7364
8532
|
return await service.voices();
|
|
@@ -7437,14 +8605,25 @@ const Config = z.object({
|
|
|
7437
8605
|
apiKey: z.string().default("")
|
|
7438
8606
|
})).default([]).volatile(),
|
|
7439
8607
|
authoringTools: z.dict(z.dict(z.boolean())).default({}).volatile(),
|
|
7440
|
-
|
|
7441
|
-
|
|
7442
|
-
|
|
7443
|
-
|
|
7444
|
-
|
|
7445
|
-
|
|
7446
|
-
|
|
7447
|
-
|
|
8608
|
+
sync: z.object({
|
|
8609
|
+
remote: z.string().default(""),
|
|
8610
|
+
rclone: z.string().default(""),
|
|
8611
|
+
configFile: z.string().default(""),
|
|
8612
|
+
passwordRef: z.string().default(""),
|
|
8613
|
+
intervalMinutes: z.number().min(0).default(0)
|
|
8614
|
+
}).default({ ...DEFAULT_SYNC }).volatile(),
|
|
8615
|
+
catalogueMaxBytes: z.number().min(1024).default(268435456).volatile(),
|
|
8616
|
+
catalogueTrashDays: z.number().min(0).default(30).volatile(),
|
|
8617
|
+
importMaxUploadBytes: z.number().min(1024).default(104857600).volatile(),
|
|
8618
|
+
importConcurrency: z.number().min(1).max(16).default(4).volatile(),
|
|
8619
|
+
importReasoning: z.string().default("").volatile(),
|
|
8620
|
+
childReasoning: z.string().default("").volatile(),
|
|
8621
|
+
importModel: z.string().default("").volatile(),
|
|
8622
|
+
importMaxTokens: z.number().min(1e3).max(2e5).default(16e3).volatile(),
|
|
8623
|
+
importTemperature: z.number().min(0).max(2).default(.2).volatile(),
|
|
8624
|
+
importTimeoutMinutes: z.number().min(0).max(240).default(15).volatile(),
|
|
8625
|
+
importDecisions: z.union([z.const("llm"), z.const("decision")]).default("llm").volatile(),
|
|
8626
|
+
importConfidence: z.number().min(0).max(1).default(.7).volatile()
|
|
7448
8627
|
});
|
|
7449
8628
|
//#endregion
|
|
7450
8629
|
//#region src/index.ts
|
|
@@ -7459,6 +8638,10 @@ function apply(ctx, config) {
|
|
|
7459
8638
|
const id = String(options.sessionId);
|
|
7460
8639
|
const helper = childSamplingOf(id);
|
|
7461
8640
|
if (helper !== void 0) return helper;
|
|
8641
|
+
if (authoredWorldOf(id) !== void 0 || presetOfChat(id) !== void 0 || lorebookOfChat(id) !== void 0) return {
|
|
8642
|
+
maxTokens: config.importMaxTokens.get(),
|
|
8643
|
+
priority: "foreground"
|
|
8644
|
+
};
|
|
7462
8645
|
const session = roleplaySessionOf(id);
|
|
7463
8646
|
if (session === void 0) return void 0;
|
|
7464
8647
|
const { sampling } = liveSettings(sessionFolder(join(root.path, "sessions"), session));
|
|
@@ -7470,7 +8653,7 @@ function apply(ctx, config) {
|
|
|
7470
8653
|
};
|
|
7471
8654
|
}), "roleplay: request controls");
|
|
7472
8655
|
});
|
|
7473
|
-
ctx.effect(() => useChildReasoning(config.childReasoning), "roleplay: helper thinking level");
|
|
8656
|
+
ctx.effect(() => useChildReasoning(() => config.childReasoning.get()), "roleplay: helper thinking level");
|
|
7474
8657
|
const root = new RoleplayRoot(ctx, config.rootPath);
|
|
7475
8658
|
root.start();
|
|
7476
8659
|
const orchestration = new RoleplayOrchestration(ctx);
|
|
@@ -7488,13 +8671,19 @@ function apply(ctx, config) {
|
|
|
7488
8671
|
const lorebookAuthoring = new LorebookAuthoringSessions(agentServicesOf(ctx), () => root.path, prompts, languages);
|
|
7489
8672
|
const doctors = new DoctorSessions(agentServicesOf(ctx), () => root.path, prompts);
|
|
7490
8673
|
const imports = new ImportStore(() => root.path);
|
|
7491
|
-
const roleplayImport = new RoleplayImport(ctx, {
|
|
7492
|
-
maxUploadBytes
|
|
7493
|
-
|
|
7494
|
-
|
|
7495
|
-
|
|
7496
|
-
|
|
7497
|
-
|
|
8674
|
+
const roleplayImport = new RoleplayImport(ctx, () => ({
|
|
8675
|
+
get maxUploadBytes() {
|
|
8676
|
+
return config.importMaxUploadBytes.get();
|
|
8677
|
+
},
|
|
8678
|
+
concurrency: config.importConcurrency.get(),
|
|
8679
|
+
reasoning: config.importReasoning.get() || config.childReasoning.get(),
|
|
8680
|
+
model: config.importModel.get(),
|
|
8681
|
+
maxTokens: config.importMaxTokens.get(),
|
|
8682
|
+
temperature: config.importTemperature.get(),
|
|
8683
|
+
timeoutMinutes: config.importTimeoutMinutes.get(),
|
|
8684
|
+
decisions: config.importDecisions.get() === "decision" ? "decision" : "llm",
|
|
8685
|
+
confidence: config.importConfidence.get()
|
|
8686
|
+
}));
|
|
7498
8687
|
const settingsDeps = {
|
|
7499
8688
|
sessionsDir: () => join(root.path, "sessions"),
|
|
7500
8689
|
pendingRecords: (id) => ctx.roleplayState.openSession(join(root.path, "sessions"), id).pendingCommits.list().length,
|
|
@@ -7521,7 +8710,9 @@ function apply(ctx, config) {
|
|
|
7521
8710
|
worldDirectory: async (worldId) => (await worlds.find(worldId))?.directory,
|
|
7522
8711
|
authoring: presetAuthoring,
|
|
7523
8712
|
importMessage: narrationImportMessage,
|
|
7524
|
-
maxUploadBytes
|
|
8713
|
+
get maxUploadBytes() {
|
|
8714
|
+
return config.importMaxUploadBytes.get();
|
|
8715
|
+
},
|
|
7525
8716
|
staging,
|
|
7526
8717
|
playing: (id) => orchestration.playing(chatSessionId(id))
|
|
7527
8718
|
});
|
|
@@ -7529,7 +8720,9 @@ function apply(ctx, config) {
|
|
|
7529
8720
|
root: () => root.path,
|
|
7530
8721
|
authoring: lorebookAuthoring,
|
|
7531
8722
|
importMessage: lorebookImportMessage,
|
|
7532
|
-
maxUploadBytes
|
|
8723
|
+
get maxUploadBytes() {
|
|
8724
|
+
return config.importMaxUploadBytes.get();
|
|
8725
|
+
},
|
|
7533
8726
|
staging,
|
|
7534
8727
|
session: (id) => {
|
|
7535
8728
|
const sessions = join(root.path, "sessions");
|
|
@@ -7541,9 +8734,9 @@ function apply(ctx, config) {
|
|
|
7541
8734
|
const catalogue = new Catalogue({
|
|
7542
8735
|
sources: new Sources(() => config.catalogues.get(), () => ctx.get("credentials")),
|
|
7543
8736
|
worlds,
|
|
7544
|
-
maxBytes: () => config.catalogueMaxBytes,
|
|
7545
|
-
maxCardBytes: () => config.importMaxUploadBytes,
|
|
7546
|
-
trashDays: () => config.catalogueTrashDays,
|
|
8737
|
+
maxBytes: () => config.catalogueMaxBytes.get(),
|
|
8738
|
+
maxCardBytes: () => config.importMaxUploadBytes.get(),
|
|
8739
|
+
trashDays: () => config.catalogueTrashDays.get(),
|
|
7547
8740
|
problem: (message) => {
|
|
7548
8741
|
ctx.logger.warn(message);
|
|
7549
8742
|
},
|
|
@@ -7554,21 +8747,41 @@ function apply(ctx, config) {
|
|
|
7554
8747
|
startCard: async (id, choices, language) => await startImport({
|
|
7555
8748
|
store: imports,
|
|
7556
8749
|
worlds,
|
|
7557
|
-
authoring
|
|
7558
|
-
chatTitle: (title) => `Authoring ${title}`
|
|
8750
|
+
authoring
|
|
7559
8751
|
}, id, choices, language),
|
|
7560
8752
|
importCard: async (file, name, language) => {
|
|
7561
8753
|
const preview = await imports.create(readCard(file, name), isPng(file) ? pictureOf(file) : void 0);
|
|
7562
8754
|
return await startImport({
|
|
7563
8755
|
store: imports,
|
|
7564
8756
|
worlds,
|
|
7565
|
-
authoring
|
|
7566
|
-
chatTitle: (title) => `Authoring ${title}`
|
|
8757
|
+
authoring
|
|
7567
8758
|
}, preview.id, preview.choices, language);
|
|
7568
8759
|
}
|
|
7569
8760
|
});
|
|
7570
8761
|
catalogue.tidy();
|
|
8762
|
+
const sync = createSync(ctx, {
|
|
8763
|
+
config: () => config.sync.get(),
|
|
8764
|
+
root: () => root.path,
|
|
8765
|
+
worlds,
|
|
8766
|
+
orchestration,
|
|
8767
|
+
state: ctx.roleplayState,
|
|
8768
|
+
remove: (id) => deleteSession({
|
|
8769
|
+
state: ctx.roleplayState,
|
|
8770
|
+
sessionsDir: deps.sessionsDir,
|
|
8771
|
+
purgeChat
|
|
8772
|
+
}, id),
|
|
8773
|
+
rebuild: async (id) => {
|
|
8774
|
+
if (await agent.has(id)) await openSession({
|
|
8775
|
+
...deps,
|
|
8776
|
+
replaceChat: (chat, title, events) => agent.replace(chat, title, events, purgeChat)
|
|
8777
|
+
}, id, { rebuild: true });
|
|
8778
|
+
}
|
|
8779
|
+
});
|
|
8780
|
+
prompts.frozen = () => sync.gate.isLocked("prompts");
|
|
7571
8781
|
registerApi(ctx, {
|
|
8782
|
+
sync: sync.api,
|
|
8783
|
+
locked: sync.locked,
|
|
8784
|
+
anyLocked: () => sync.gate.any,
|
|
7572
8785
|
catalogue,
|
|
7573
8786
|
presets: presetLibrary,
|
|
7574
8787
|
lorebooks: lorebookLibrary,
|
|
@@ -7603,7 +8816,9 @@ function apply(ctx, config) {
|
|
|
7603
8816
|
}, id, to, title, language)
|
|
7604
8817
|
},
|
|
7605
8818
|
importer: {
|
|
7606
|
-
maxUploadBytes
|
|
8819
|
+
get maxUploadBytes() {
|
|
8820
|
+
return config.importMaxUploadBytes.get();
|
|
8821
|
+
},
|
|
7607
8822
|
upload: async (file, name) => {
|
|
7608
8823
|
const card = readCard(file, name);
|
|
7609
8824
|
return await imports.create(card, isPng(file) ? pictureOf(file) : void 0);
|
|
@@ -7614,8 +8829,7 @@ function apply(ctx, config) {
|
|
|
7614
8829
|
start: (id, choices, language) => startImport({
|
|
7615
8830
|
store: imports,
|
|
7616
8831
|
worlds,
|
|
7617
|
-
authoring
|
|
7618
|
-
chatTitle: (name) => `Authoring ${name}`
|
|
8832
|
+
authoring
|
|
7619
8833
|
}, id, choices, language),
|
|
7620
8834
|
picture: (id) => imports.picture(id),
|
|
7621
8835
|
uploadPreset: (worldId, file, name, language) => uploadPreset({
|
|
@@ -7730,17 +8944,24 @@ function apply(ctx, config) {
|
|
|
7730
8944
|
}, id, kind, key, png, name),
|
|
7731
8945
|
list: () => listSessions(deps),
|
|
7732
8946
|
start: (request) => startSession(deps, request),
|
|
7733
|
-
open: (id) => openSession(
|
|
8947
|
+
open: (id, rebuild) => openSession({
|
|
8948
|
+
...deps,
|
|
8949
|
+
replaceChat: (id, title, events) => agent.replace(id, title, events, purgeChat)
|
|
8950
|
+
}, id, rebuild === true ? { rebuild } : {}),
|
|
7734
8951
|
send: (id, text) => agent.send(id, text),
|
|
7735
8952
|
gallery: (id) => imagesOf(ctx, deps.sessionsDir, id).gallery(id),
|
|
7736
8953
|
galleryFile: (id, path) => galleryFile(galleryFolder(deps.sessionsDir(), id), path),
|
|
7737
8954
|
uploadImage: (id, name, bytes) => imagesOf(ctx, deps.sessionsDir, id).upload(id, name, bytes),
|
|
7738
8955
|
updateImage: (id, path, update) => imagesOf(ctx, deps.sessionsDir, id).update(id, path, update),
|
|
7739
|
-
delete: (id) =>
|
|
7740
|
-
|
|
7741
|
-
|
|
7742
|
-
|
|
7743
|
-
|
|
8956
|
+
delete: async (id, everywhere) => {
|
|
8957
|
+
if (everywhere === true) await sync.service.deleteRemote(`sessions/${id}`);
|
|
8958
|
+
await deleteSession({
|
|
8959
|
+
state: ctx.roleplayState,
|
|
8960
|
+
sessionsDir: deps.sessionsDir,
|
|
8961
|
+
purgeChat
|
|
8962
|
+
}, id);
|
|
8963
|
+
if (everywhere !== true && config.sync.get().remote.trim() !== "") sync.service.forget(`sessions/${id}`);
|
|
8964
|
+
},
|
|
7744
8965
|
archive: (id) => archiveSession({
|
|
7745
8966
|
agent,
|
|
7746
8967
|
sessionsDir: deps.sessionsDir
|