@dsh-agentic-roleplay/core 0.3.4 → 0.3.6
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/authoring.js +5 -3
- package/lib/client.js +3260 -493
- package/lib/client.js.map +1 -1
- package/lib/{deliver-CpZOxBX5.js → deliver-BBgciEEK.js} +3 -2
- package/lib/doctor.js +3 -3
- package/lib/{gallery-CtFDL22W.js → gallery-DIEgETzU.js} +2 -2
- package/lib/generator-DJ48HxPI.js +145 -0
- package/lib/{hooks-KTCR3fNJ.js → hooks-DkwFGtNH.js} +3 -3
- package/lib/images.js +4 -3
- package/lib/index.js +889 -52
- package/lib/language-BgyxtB2h.js +38 -0
- package/lib/{generator-B6FxlKOV.js → library-9JB9zw5a.js} +2 -143
- package/lib/{language-DWVwi1R7.js → load-CVwJjIJ5.js} +11 -45
- package/lib/lorebook-author-tools-Bj31z1cu.js +619 -0
- package/lib/lorebook-authoring.js +67 -0
- package/lib/orchestration.js +22 -14
- package/lib/pack-jail-2XztyYAX.js +1980 -0
- package/lib/{packs-BYjrAen6.js → packs-xHRCcGdb.js} +3 -29
- package/lib/{presenter-agent-CiEVKcjJ.js → presenter-agent-DnNEG_Mn.js} +4 -3
- package/lib/preset-D0FMSeZ9.js +59 -0
- package/lib/preset-author-tools-BxycmoAr.js +276 -0
- package/lib/preset-authoring.js +53 -0
- package/lib/presets-gkBWAiVb.js +374 -0
- package/lib/{reflection-waxFg1x-.js → reflection-q0UpwXlx.js} +130 -8
- package/lib/{pack-jail-CybSNzxr.js → tools-Dy1fgP6B.js} +454 -2303
- package/package.json +4 -2
- package/prompts/en/import/lorebook.md +7 -0
- package/prompts/en/lorebook/persona.md +1 -0
- package/prompts/en/lorebook/procedure.md +1 -0
- package/prompts/en/lorebook/welcome.md +1 -0
- package/prompts/en/preset/persona.md +1 -0
- package/prompts/en/preset/procedure.md +1 -0
- package/prompts/en/preset/welcome.md +1 -0
- package/prompts/zh/import/lorebook.md +7 -0
- package/prompts/zh/lorebook/persona.md +1 -0
- package/prompts/zh/lorebook/procedure.md +1 -0
- package/prompts/zh/lorebook/welcome.md +1 -0
- package/prompts/zh/preset/persona.md +1 -0
- package/prompts/zh/preset/procedure.md +1 -0
- package/prompts/zh/preset/welcome.md +1 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//#region src/host/translate/language.ts
|
|
2
|
+
const STORY_LANGUAGES = ["en", "zh"];
|
|
3
|
+
/** Less than this many letters of either script is too little to tell a language. */
|
|
4
|
+
const MIN_LETTERS = 20;
|
|
5
|
+
/** A text is Chinese when this share of its Han and Latin letters is Han: names in the other script do not decide it. */
|
|
6
|
+
const HAN_SHARE = .3;
|
|
7
|
+
const count = (text, pattern) => text.match(pattern)?.length ?? 0;
|
|
8
|
+
/** The language a text is written in, by the script of its letters; nothing when there is too little text to tell. */
|
|
9
|
+
function textLanguage(text) {
|
|
10
|
+
const han = count(text, /\p{Script=Han}/gu);
|
|
11
|
+
const latin = count(text, /\p{Script=Latin}/gu);
|
|
12
|
+
if (han + latin < MIN_LETTERS) return void 0;
|
|
13
|
+
return han / (han + latin) >= HAN_SHARE ? "zh" : "en";
|
|
14
|
+
}
|
|
15
|
+
const isStoryLanguage = (value) => value === "en" || value === "zh";
|
|
16
|
+
const LANGUAGE_NAMES = {
|
|
17
|
+
en: {
|
|
18
|
+
en: "English",
|
|
19
|
+
zh: "英文"
|
|
20
|
+
},
|
|
21
|
+
zh: {
|
|
22
|
+
en: "Chinese",
|
|
23
|
+
zh: "中文"
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
/** What is wrong with texts that should be in the chat's language but are written in the other one; nothing when they are right, too short to tell, or the language is not one we can read. */
|
|
27
|
+
function wrongLanguage(wanted, texts) {
|
|
28
|
+
if (!isStoryLanguage(wanted)) return void 0;
|
|
29
|
+
const wrong = Object.entries(texts).flatMap(([name, text]) => {
|
|
30
|
+
const found = text === void 0 ? void 0 : textLanguage(text);
|
|
31
|
+
return found === void 0 || found === wanted ? [] : [`${name} is written in ${LANGUAGE_NAMES[found].en}`];
|
|
32
|
+
});
|
|
33
|
+
return wrong.length === 0 ? void 0 : `${wrong.join(" and ")}: write it in ${LANGUAGE_NAMES[wanted].en} (${LANGUAGE_NAMES[wanted][wanted]})`;
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
export { wrongLanguage as a, textLanguage as i, STORY_LANGUAGES as n, isStoryLanguage as r, LANGUAGE_NAMES as t };
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=language-BgyxtB2h.js.map
|
|
@@ -329,147 +329,6 @@ async function addToLibrary(sessionFolder, bytes, originalName) {
|
|
|
329
329
|
return `${LIBRARY_FOLDER}/${name}`;
|
|
330
330
|
}
|
|
331
331
|
//#endregion
|
|
332
|
-
|
|
333
|
-
const unique = (values) => [...new Set(values.map((value) => value.trim()).filter((value) => value !== ""))];
|
|
334
|
-
/** What a reference image shows: the subject and its stable look. */
|
|
335
|
-
function referenceDescription(request) {
|
|
336
|
-
return `${request.subject.name} (${request.subject.kind}): ${request.identity.description}`.trim();
|
|
337
|
-
}
|
|
338
|
-
/** What a scene image shows: the established moment, then how each subject looks in it. */
|
|
339
|
-
function transientDescription(request) {
|
|
340
|
-
return [request.scene.trim(), ...request.subjects.map((subject) => subject.transient.trim() === "" ? void 0 : `${subject.name}: ${subject.transient.trim()}`)].filter((part) => part !== void 0 && part !== "").join("\n");
|
|
341
|
-
}
|
|
342
|
-
function lines(parts) {
|
|
343
|
-
return parts.filter((part) => part !== void 0 && part.trim() !== "").join("\n");
|
|
344
|
-
}
|
|
345
|
-
function referencePrompt(request) {
|
|
346
|
-
const { subject, identity } = request;
|
|
347
|
-
return lines([
|
|
348
|
-
request.artDirection,
|
|
349
|
-
`Create a canonical reference image of the ${subject.kind} ${subject.name}.`,
|
|
350
|
-
identity.description,
|
|
351
|
-
identity.tags.length === 0 ? void 0 : `Stable visual traits: ${identity.tags.join(", ")}.`,
|
|
352
|
-
"Show the subject clearly and neutrally. Do not add story events or temporary changes.",
|
|
353
|
-
identity.negativeTags.length === 0 ? void 0 : `Avoid: ${identity.negativeTags.join(", ")}.`
|
|
354
|
-
]);
|
|
355
|
-
}
|
|
356
|
-
function transientPrompt(request) {
|
|
357
|
-
return lines([
|
|
358
|
-
request.artDirection,
|
|
359
|
-
`Illustrate this established story moment: ${request.scene}`,
|
|
360
|
-
...request.subjects.map((subject) => lines([
|
|
361
|
-
`${subject.kind} ${subject.name}: ${subject.identity.description}`,
|
|
362
|
-
subject.identity.tags.length === 0 ? void 0 : `Stable visual traits: ${subject.identity.tags.join(", ")}.`,
|
|
363
|
-
subject.transient === "" ? void 0 : `Current visible state: ${subject.transient}`
|
|
364
|
-
])),
|
|
365
|
-
"Preserve every canonical identity. Depict only the stated subjects, state and action; do not invent story facts.",
|
|
366
|
-
request.subjects.flatMap((subject) => subject.identity.negativeTags).length === 0 ? void 0 : `Avoid: ${[...new Set(request.subjects.flatMap((subject) => subject.identity.negativeTags))].join(", ")}.`
|
|
367
|
-
]);
|
|
368
|
-
}
|
|
369
|
-
async function firstPng(service, prompt, options = {}) {
|
|
370
|
-
const result = await service.generate(prompt, {
|
|
371
|
-
count: 1,
|
|
372
|
-
...options
|
|
373
|
-
});
|
|
374
|
-
const image = result.images[0];
|
|
375
|
-
if (image === void 0) throw new Error(`${service.provider} returned no image`);
|
|
376
|
-
if (image.mediaType !== "image/png") throw new Error(`${service.provider} returned ${image.mediaType}; roleplay image storage currently requires PNG`);
|
|
377
|
-
return {
|
|
378
|
-
data: new Uint8Array(await image.data.arrayBuffer()),
|
|
379
|
-
model: result.model,
|
|
380
|
-
...image.revisedPrompt === void 0 ? {} : { revisedPrompt: image.revisedPrompt }
|
|
381
|
-
};
|
|
382
|
-
}
|
|
383
|
-
/** Generates the stable reference.png of one entity. It is intentionally not registered in the live roleplay loop yet. */
|
|
384
|
-
async function generateReferenceImage(service, request) {
|
|
385
|
-
const prompt = referencePrompt(request);
|
|
386
|
-
const image = await firstPng(service, prompt, request.signal === void 0 ? {} : { signal: request.signal });
|
|
387
|
-
request.signal?.throwIfAborted();
|
|
388
|
-
const path = referenceImagePath(request.root, request.subject);
|
|
389
|
-
const metadata = {
|
|
390
|
-
schema: "dsh-roleplay-image/v3",
|
|
391
|
-
purpose: "reference",
|
|
392
|
-
origin: "generated",
|
|
393
|
-
reusable: true,
|
|
394
|
-
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
395
|
-
prompt,
|
|
396
|
-
description: referenceDescription(request),
|
|
397
|
-
tags: unique([request.subject.name, ...request.identity.tags]),
|
|
398
|
-
...image.revisedPrompt === void 0 ? {} : { revisedPrompt: image.revisedPrompt },
|
|
399
|
-
provider: service.provider,
|
|
400
|
-
model: image.model,
|
|
401
|
-
subjects: [{
|
|
402
|
-
...request.subject,
|
|
403
|
-
identityVersion: request.identity.version
|
|
404
|
-
}]
|
|
405
|
-
};
|
|
406
|
-
await writeImage(path, image.data, metadata, { replace: true });
|
|
407
|
-
return path;
|
|
408
|
-
}
|
|
409
|
-
const PNG_SIGNATURE = [
|
|
410
|
-
137,
|
|
411
|
-
80,
|
|
412
|
-
78,
|
|
413
|
-
71,
|
|
414
|
-
13,
|
|
415
|
-
10,
|
|
416
|
-
26,
|
|
417
|
-
10
|
|
418
|
-
];
|
|
419
|
-
/** Takes a picture the person brought as the stable reference.png of one entity, replacing the one there. Only PNG is stored, so any other format is refused. */
|
|
420
|
-
async function uploadReferenceImage(request) {
|
|
421
|
-
if (request.png.length < PNG_SIGNATURE.length || PNG_SIGNATURE.some((byte, index) => request.png[index] !== byte)) throw new Error("the portrait must be a PNG picture: convert it first");
|
|
422
|
-
const path = referenceImagePath(request.root, request.subject);
|
|
423
|
-
const metadata = {
|
|
424
|
-
schema: "dsh-roleplay-image/v3",
|
|
425
|
-
purpose: "reference",
|
|
426
|
-
origin: "uploaded",
|
|
427
|
-
reusable: true,
|
|
428
|
-
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
429
|
-
description: `${request.subject.name}, a picture brought by the author.`,
|
|
430
|
-
tags: [request.subject.name],
|
|
431
|
-
...request.originalName === void 0 ? {} : { originalName: request.originalName },
|
|
432
|
-
subjects: [request.subject]
|
|
433
|
-
};
|
|
434
|
-
await writeImage(path, request.png, metadata, { replace: true });
|
|
435
|
-
return path;
|
|
436
|
-
}
|
|
437
|
-
/** Generates one immutable scene image for a timeline node. It is intentionally not registered in the live roleplay loop yet. */
|
|
438
|
-
async function generateTransientImage(service, request) {
|
|
439
|
-
if (request.subjects.length === 0) throw new Error("a transient image needs at least one subject");
|
|
440
|
-
const prompt = transientPrompt(request);
|
|
441
|
-
const image = await firstPng(service, prompt, {
|
|
442
|
-
...request.references === void 0 ? {} : { references: request.references },
|
|
443
|
-
...request.signal === void 0 ? {} : { signal: request.signal }
|
|
444
|
-
});
|
|
445
|
-
request.signal?.throwIfAborted();
|
|
446
|
-
const path = sceneImagePath(request.root, request.timelineNode, request.serial);
|
|
447
|
-
const metadata = {
|
|
448
|
-
schema: "dsh-roleplay-image/v3",
|
|
449
|
-
purpose: "transient",
|
|
450
|
-
origin: "generated",
|
|
451
|
-
reusable: request.reusable === true,
|
|
452
|
-
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
453
|
-
prompt,
|
|
454
|
-
description: transientDescription(request),
|
|
455
|
-
tags: unique(request.subjects.flatMap((subject) => [subject.name, ...subject.identity.tags])),
|
|
456
|
-
...image.revisedPrompt === void 0 ? {} : { revisedPrompt: image.revisedPrompt },
|
|
457
|
-
provider: service.provider,
|
|
458
|
-
model: image.model,
|
|
459
|
-
subjects: request.subjects.map(({ identity, transient: _transient, ...subject }) => ({
|
|
460
|
-
...subject,
|
|
461
|
-
identityVersion: identity.version
|
|
462
|
-
})),
|
|
463
|
-
source: {
|
|
464
|
-
turn: request.turn,
|
|
465
|
-
timelineNode: request.timelineNode,
|
|
466
|
-
...request.segment === void 0 ? {} : { segment: request.segment }
|
|
467
|
-
}
|
|
468
|
-
};
|
|
469
|
-
await writeImage(path, image.data, metadata);
|
|
470
|
-
return path;
|
|
471
|
-
}
|
|
472
|
-
//#endregion
|
|
473
|
-
export { norm as C, mapLimit as S, uniqueKey as T, readImageFileMetadata as _, uploadReferenceImage as a, readPngMetadata as b, LIBRARY_FOLDER as c, nextSceneSerial as d, readImageMetadata as f, mediaTypeOf as g, MEDIA_TYPES as h, transientPrompt as i, entityImageDirectory as l, sceneImagePath as m, generateTransientImage as n, addToLibrary as o, referenceImagePath as p, referencePrompt as r, imageExtensionOf as s, generateReferenceImage as t, inheritImages as u, sidecarOf as v, slug as w, withPngMetadata as x, writeImageFileMetadata as y };
|
|
332
|
+
export { withPngMetadata as _, inheritImages as a, slug as b, referenceImagePath as c, MEDIA_TYPES as d, mediaTypeOf as f, readPngMetadata as g, writeImageFileMetadata as h, entityImageDirectory as i, sceneImagePath as l, sidecarOf as m, imageExtensionOf as n, nextSceneSerial as o, readImageFileMetadata as p, LIBRARY_FOLDER as r, readImageMetadata as s, addToLibrary as t, writeImage as u, mapLimit as v, uniqueKey as x, norm as y };
|
|
474
333
|
|
|
475
|
-
//# sourceMappingURL=
|
|
334
|
+
//# sourceMappingURL=library-9JB9zw5a.js.map
|
|
@@ -46,7 +46,8 @@ var ReflectionRefusedError = class extends Error {};
|
|
|
46
46
|
const PROMPTS_FOLDER = "prompts";
|
|
47
47
|
const SESSION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
|
48
48
|
const ID = /^[a-z0-9-]+(\/[a-z0-9-]+)*$/;
|
|
49
|
-
const
|
|
49
|
+
const LANGUAGE_FOLDER = /^[a-z]{2,8}$/;
|
|
50
|
+
const LANGUAGE = LANGUAGE_FOLDER;
|
|
50
51
|
const VARIABLE = /\$\{([A-Za-z]+)\}/g;
|
|
51
52
|
/** The folder of the prompts that ship with the package, found from this module so that it works from source and from the build. */
|
|
52
53
|
function shippedPromptsDirectory(from = dirname(fileURLToPath(import.meta.url))) {
|
|
@@ -63,13 +64,13 @@ function languageOf(tag) {
|
|
|
63
64
|
return primary;
|
|
64
65
|
}
|
|
65
66
|
/** How a language is named inside a prompt, for the instruction to write in it. */
|
|
66
|
-
const LANGUAGE_NAMES
|
|
67
|
+
const LANGUAGE_NAMES = {
|
|
67
68
|
en: "English",
|
|
68
69
|
zh: "中文"
|
|
69
70
|
};
|
|
70
71
|
function fill(id, text, given, tag) {
|
|
71
72
|
const variables = {
|
|
72
|
-
language: LANGUAGE_NAMES
|
|
73
|
+
language: LANGUAGE_NAMES[tag] ?? tag,
|
|
73
74
|
...given
|
|
74
75
|
};
|
|
75
76
|
return text.trim().replace(/^\$\{([A-Za-z]+)\}[ \t]*(\r?\n|$)/gm, (match, name) => variables[name] === "" ? "" : match).replace(VARIABLE, (_match, name) => {
|
|
@@ -207,6 +208,11 @@ function shippedLanguages(shipped = shippedPromptsDirectory()) {
|
|
|
207
208
|
return readdirSync(shipped, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => entry.name);
|
|
208
209
|
}
|
|
209
210
|
//#endregion
|
|
211
|
+
//#region src/host/errors.ts
|
|
212
|
+
function errorMessage(error) {
|
|
213
|
+
return error instanceof Error ? error.message : String(error);
|
|
214
|
+
}
|
|
215
|
+
//#endregion
|
|
210
216
|
//#region src/host/validation.ts
|
|
211
217
|
function formatIssue({ path, message }) {
|
|
212
218
|
return path === "" ? message : `${path}: ${message}`;
|
|
@@ -342,11 +348,6 @@ var Reader = class Reader {
|
|
|
342
348
|
}
|
|
343
349
|
};
|
|
344
350
|
//#endregion
|
|
345
|
-
//#region src/host/errors.ts
|
|
346
|
-
function errorMessage(error) {
|
|
347
|
-
return error instanceof Error ? error.message : String(error);
|
|
348
|
-
}
|
|
349
|
-
//#endregion
|
|
350
351
|
//#region src/host/read-document.ts
|
|
351
352
|
/** Authored documents are short; a larger file is not one. */
|
|
352
353
|
const MAX_DOCUMENT_BYTES = 1048576;
|
|
@@ -480,41 +481,6 @@ function plotReferenceProblems(plots, entities) {
|
|
|
480
481
|
return plots.flatMap((plot) => plot.participants.filter((participant) => !known.has(participant)).map((participant) => `plot "${plot.id}" names unknown participant "${participant}"`));
|
|
481
482
|
}
|
|
482
483
|
//#endregion
|
|
483
|
-
|
|
484
|
-
const STORY_LANGUAGES = ["en", "zh"];
|
|
485
|
-
/** Less than this many letters of either script is too little to tell a language. */
|
|
486
|
-
const MIN_LETTERS = 20;
|
|
487
|
-
/** A text is Chinese when this share of its Han and Latin letters is Han: names in the other script do not decide it. */
|
|
488
|
-
const HAN_SHARE = .3;
|
|
489
|
-
const count = (text, pattern) => text.match(pattern)?.length ?? 0;
|
|
490
|
-
/** The language a text is written in, by the script of its letters; nothing when there is too little text to tell. */
|
|
491
|
-
function textLanguage(text) {
|
|
492
|
-
const han = count(text, /\p{Script=Han}/gu);
|
|
493
|
-
const latin = count(text, /\p{Script=Latin}/gu);
|
|
494
|
-
if (han + latin < MIN_LETTERS) return void 0;
|
|
495
|
-
return han / (han + latin) >= HAN_SHARE ? "zh" : "en";
|
|
496
|
-
}
|
|
497
|
-
const isStoryLanguage = (value) => value === "en" || value === "zh";
|
|
498
|
-
const LANGUAGE_NAMES = {
|
|
499
|
-
en: {
|
|
500
|
-
en: "English",
|
|
501
|
-
zh: "英文"
|
|
502
|
-
},
|
|
503
|
-
zh: {
|
|
504
|
-
en: "Chinese",
|
|
505
|
-
zh: "中文"
|
|
506
|
-
}
|
|
507
|
-
};
|
|
508
|
-
/** What is wrong with texts that should be in the chat's language but are written in the other one; nothing when they are right, too short to tell, or the language is not one we can read. */
|
|
509
|
-
function wrongLanguage(wanted, texts) {
|
|
510
|
-
if (!isStoryLanguage(wanted)) return void 0;
|
|
511
|
-
const wrong = Object.entries(texts).flatMap(([name, text]) => {
|
|
512
|
-
const found = text === void 0 ? void 0 : textLanguage(text);
|
|
513
|
-
return found === void 0 || found === wanted ? [] : [`${name} is written in ${LANGUAGE_NAMES[found].en}`];
|
|
514
|
-
});
|
|
515
|
-
return wrong.length === 0 ? void 0 : `${wrong.join(" and ")}: write it in ${LANGUAGE_NAMES[wanted].en} (${LANGUAGE_NAMES[wanted][wanted]})`;
|
|
516
|
-
}
|
|
517
|
-
//#endregion
|
|
518
|
-
export { WorldNotFoundError as A, shippedText as C, ReflectionRefusedError as D, InvalidWorldError as E, SessionArchivedError as O, shippedPromptsDirectory as S, InvalidStartError as T, inheritPrompts as _, wrongLanguage as a, promptProblem as b, parsePlotsFile as c, errorMessage as d, Reader as f, copyWorldPrompts as g, PromptLibrary as h, textLanguage as i, SessionNotFoundError as k, plotReferenceProblems as l, PROMPTS_FOLDER as m, STORY_LANGUAGES as n, PLOTS_FILE_NAME as o, formatIssue as p, isStoryLanguage as r, loadPlots as s, LANGUAGE_NAMES as t, readDocument as u, languageOf as v, AgentUnavailableError as w, shippedLanguages as x, listFiles as y };
|
|
484
|
+
export { ReflectionRefusedError as C, WorldNotFoundError as E, InvalidWorldError as S, SessionNotFoundError as T, shippedLanguages as _, readDocument as a, AgentUnavailableError as b, errorMessage as c, PromptLibrary as d, copyWorldPrompts as f, promptProblem as g, listFiles as h, plotReferenceProblems as i, LANGUAGE_FOLDER as l, languageOf as m, loadPlots as n, Reader as o, inheritPrompts as p, parsePlotsFile as r, formatIssue as s, PLOTS_FILE_NAME as t, PROMPTS_FOLDER as u, shippedPromptsDirectory as v, SessionArchivedError as w, InvalidStartError as x, shippedText as y };
|
|
519
485
|
|
|
520
|
-
//# sourceMappingURL=
|
|
486
|
+
//# sourceMappingURL=load-CVwJjIJ5.js.map
|