@dsh-agentic-roleplay/core 0.1.1 → 0.2.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/README.md +1 -1
- package/lib/{activity-BbqFG9-3.js → activity-t7OBgHlc.js} +255 -61
- package/lib/authoring.js +295 -14
- package/lib/client.js +1896 -453
- package/lib/client.js.map +1 -1
- package/lib/compaction.js +1 -1
- package/lib/{documents-ZVoUTnD2.js → documents-Bu5uYRZB.js} +3 -3
- package/lib/{gallery-KGCdeCnM.js → gallery-DUeBMHQX.js} +2 -2
- package/lib/{generator-NYnNdpWb.js → generator-HPifO3Kk.js} +125 -20
- package/lib/images.js +3 -3
- package/lib/index.js +638 -14
- package/lib/{narration-DcnCqA1l.js → narration-BRyHU0H2.js} +10 -4
- package/lib/orchestration.js +303 -161
- package/lib/packs-ByP1J-zj.js +1334 -0
- package/lib/{presenter-agent-7j1c7QEz.js → presenter-agent-G43ODV8I.js} +3 -3
- package/package.json +4 -3
- package/prompts/en/roleplay/ask-answerer.md +3 -0
- package/prompts/en/roleplay/ask-question.md +2 -0
- package/prompts/en/roleplay/character-decide.md +1 -1
- package/prompts/en/roleplay/character-lookup.md +1 -1
- package/prompts/en/roleplay/game-master.md +5 -1
- package/prompts/en/roleplay/ooc-empty.md +1 -1
- package/prompts/en/world/procedure.md +1 -1
- package/prompts/zh/roleplay/ask-answerer.md +3 -0
- package/prompts/zh/roleplay/ask-question.md +2 -0
- package/prompts/zh/roleplay/character-decide.md +1 -1
- package/prompts/zh/roleplay/character-lookup.md +1 -1
- package/prompts/zh/roleplay/game-master.md +5 -1
- package/prompts/zh/roleplay/ooc-empty.md +1 -1
- package/prompts/zh/world/procedure.md +1 -1
- package/starter-worlds/blackwater-observatory/ui/notebook/map.html +12 -0
- package/starter-worlds/blackwater-observatory/ui/notebook/map.js +40 -0
- package/starter-worlds/blackwater-observatory/ui/notebook/notebook.html +29 -0
- package/starter-worlds/blackwater-observatory/ui/notebook/notebook.js +45 -0
- package/starter-worlds/blackwater-observatory/ui/notebook/pack.json +31 -0
- package/starter-worlds/blackwater-observatory/ui/notebook/skin.css +16 -0
- package/starter-worlds/blackwater-observatory/ui/notebook/strip.html +11 -0
- package/starter-worlds/blackwater-observatory/ui/notebook/strip.js +12 -0
- package/starter-worlds/blackwater-observatory/ui/notebook/style.css +23 -0
- package/lib/worlds-DYgjdH8O.js +0 -504
package/lib/orchestration.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { n as roleplaySessionOf, t as chatSessionId } from "./chat-AKrVcWIC.js";
|
|
2
|
-
import {
|
|
3
|
-
import { a as liveSettings, f as WORLD_RULES_KEY, n as encodeOutput, p as rulesOf, r as encodePreview } from "./narration-
|
|
2
|
+
import { F as shippedText, U as INTENT_STATUSES, _ as API_PREFIX, b as parsePlotsFile } from "./packs-ByP1J-zj.js";
|
|
3
|
+
import { a as liveSettings, f as WORLD_RULES_KEY, n as encodeOutput, p as rulesOf, r as encodePreview } from "./narration-BRyHU0H2.js";
|
|
4
4
|
import "./src-CWTNeL6Y.js";
|
|
5
|
-
import { A as runChild, D as childSampling, E as childModel, T as uniqueKey, k as childTrace, o as addToLibrary, w as slug } from "./generator-
|
|
6
|
-
import {
|
|
7
|
-
import { i as updateImage, r as listGallery, t as describeGalleryImage } from "./gallery-
|
|
5
|
+
import { A as runChild, D as childSampling, E as childModel, T as uniqueKey, j as estimateAnswer, k as childTrace, o as addToLibrary, w as slug } from "./generator-HPifO3Kk.js";
|
|
6
|
+
import { A as readTime, D as describeTime, E as beginTurn, F as CHARACTER_ACTION_SCHEMA, I as RESULTS, L as answerEstimate, M as zoneOf, N as referent, O as describeWhen, P as AWAY_ACTION_SCHEMA, R as parseCharacterAction, T as ClockError, _ as putNote, a as recordActivity, b as Timelines, c as describePacingAdvice, h as clearNotes, j as skipTime, k as endRound, l as pacingAdvice, m as addToNote, n as keepRunDetail, p as updatePlotProgress, s as addSessionPlot, t as forgetRunDetails, u as plotAuthorNeeded, v as readNotes, w as sameWords } from "./activity-t7OBgHlc.js";
|
|
7
|
+
import { i as updateImage, r as listGallery, t as describeGalleryImage } from "./gallery-DUeBMHQX.js";
|
|
8
8
|
import { t as promptFor } from "./access-C1nFQiyT.js";
|
|
9
|
-
import { n as interpretImage, s as presentedFor, t as runImagePresenter } from "./presenter-agent-
|
|
9
|
+
import { n as interpretImage, s as presentedFor, t as runImagePresenter } from "./presenter-agent-G43ODV8I.js";
|
|
10
10
|
import { join } from "node:path";
|
|
11
11
|
import { existsSync } from "node:fs";
|
|
12
12
|
import { SessionId } from "@deepseek-ai/dsh-session";
|
|
@@ -29,7 +29,7 @@ const VERIFY_SECTIONS = [
|
|
|
29
29
|
"engagements"
|
|
30
30
|
];
|
|
31
31
|
const label = (round) => `turn ${round.turn}, round ${round.round}`;
|
|
32
|
-
function describeRound(round) {
|
|
32
|
+
function describeRound(round, known) {
|
|
33
33
|
return [
|
|
34
34
|
`## ${label(round)}`,
|
|
35
35
|
round.offstage === true ? `Off stage (away from the player's character; only the characters named here perceive it, and only those who were together perceive each other): ${round.situation}` : `The moment (includes what the player's character did or said): ${round.situation}`,
|
|
@@ -37,7 +37,7 @@ function describeRound(round) {
|
|
|
37
37
|
...round.time === void 0 ? [] : [`When: ${describeInstant(round.time.from, round.time.zone)} (${dayPart(localTime(round.time.from, round.time.zone).minuteOfDay)})${round.time.to > round.time.from ? `; the round took ${describeDuration(round.time.to - round.time.from)}` : ""}`],
|
|
38
38
|
"What each character did, and what became of it:",
|
|
39
39
|
...round.entries.map((entry) => [
|
|
40
|
-
entry.forced === true ? `- ${entry.character} (${entry.name}): decreed, not chosen: ${entry.result}: ${entry.summary}` : entry.player === true ? `- ${entry.character} (${entry.name}, the player's character): ${entry.result}: ${entry.summary}` : `- ${entry.character} (${entry.name}): intended "${entry.intent}" [commitment: ${entry.commitment}]; ${entry.result}: ${entry.summary}`,
|
|
40
|
+
entry.forced === true ? `- ${entry.character} (${known.get(entry.character) ?? entry.name}): decreed, not chosen: ${entry.result}: ${entry.summary}` : entry.player === true ? `- ${entry.character} (${known.get(entry.character) ?? entry.name}, the player's character): ${entry.result}: ${entry.summary}` : `- ${entry.character} (${known.get(entry.character) ?? entry.name}): intended "${entry.intent}" [commitment: ${entry.commitment}]; ${entry.result}: ${entry.summary}`,
|
|
41
41
|
entry.utterance === void 0 ? "" : ` said${entry.spoken ? "" : " (but was prevented from saying)"}: "${entry.utterance}"`,
|
|
42
42
|
entry.publicState === void 0 ? "" : ` the game master says anyone present can now see: ${entry.publicState}`,
|
|
43
43
|
entry.variables === void 0 ? "" : ` the game master changed what is tracked: ${entry.variables.map((change) => `${change.subject}'s ${change.name} ${change.from} -> ${change.to}`).join("; ")}`,
|
|
@@ -56,6 +56,7 @@ function describePrompt(job, memory) {
|
|
|
56
56
|
const first = job.rounds[0];
|
|
57
57
|
const records = job.store.story.records({ timeZone: zoneOf(job.store.settings().clock) });
|
|
58
58
|
const own = job.store.story.records({ rounds }).events;
|
|
59
|
+
const known = new Map(job.store.cast().map((member) => [member.key, referent(member.character)]));
|
|
59
60
|
const before = records.events.filter((event) => event.turn < first.turn || event.turn === first.turn && event.round < first.round).slice(-6);
|
|
60
61
|
return [
|
|
61
62
|
memory.flaky ? "Perception in this chat: characters do not perceive perfectly. Mark a perception partial only when the character could take in just part of it (overheard, glimpsed), and mistaken only when the material shows it taking in something false; in both cases put the character's own version in content. Otherwise it is accurate and content is left out." : "Perception in this chat: every character perceives exactly what happened. Leave accuracy and content out.",
|
|
@@ -64,9 +65,9 @@ function describePrompt(job, memory) {
|
|
|
64
65
|
job.rounds.length > 1 ? `${job.rounds.length} rounds are due for records: write all of them, oldest first.` : `${label(first)} is due for records.`,
|
|
65
66
|
...job.focus === void 0 || job.focus.length === 0 ? [] : ["", `The game master says this is worth keeping: ${job.focus.join("; ")}`],
|
|
66
67
|
"",
|
|
67
|
-
...job.rounds.flatMap(describeRound),
|
|
68
|
+
...job.rounds.flatMap((round) => describeRound(round, known)),
|
|
68
69
|
"## State of the characters now",
|
|
69
|
-
...records.characters.map((entry) => `- ${entry.character} (${entry.name}, ${entry.controlled_by}): public: ${entry.public_state || "-"}; inner: ${entry.inner_state || "-"}; doing: ${entry.activity || "-"}${entry.location === null ? "" : `; in: ${entry.location}${entry.position === "" ? "" : ` (${entry.position})`}`}`),
|
|
70
|
+
...records.characters.map((entry) => `- ${entry.character} (${known.get(entry.character) ?? entry.name}, ${entry.controlled_by}): public: ${entry.public_state || "-"}; inner: ${entry.inner_state || "-"}; doing: ${entry.activity || "-"}${entry.location === null ? "" : `; in: ${entry.location}${entry.position === "" ? "" : ` (${entry.position})`}`}`),
|
|
70
71
|
"",
|
|
71
72
|
"## Recorded before these rounds (do not repeat)",
|
|
72
73
|
...before.length === 0 ? ["- nothing"] : before.map((event) => `- [turn ${event.turn}] ${event.text}`),
|
|
@@ -240,7 +241,7 @@ function promptOf(job) {
|
|
|
240
241
|
const place = player?.place?.location.name;
|
|
241
242
|
return [
|
|
242
243
|
`Write exactly ${job.count} suggestion${job.count === 1 ? "" : "s"}.`,
|
|
243
|
-
`The player plays ${player
|
|
244
|
+
`The player plays ${player === void 0 ? "their character" : referent(player.character)}${place === void 0 ? "" : `, who is at ${place}`}.`,
|
|
244
245
|
"",
|
|
245
246
|
"What the player has just read:",
|
|
246
247
|
readingOf(job)
|
|
@@ -255,7 +256,7 @@ function suggestionWriter(deps) {
|
|
|
255
256
|
parent,
|
|
256
257
|
signal,
|
|
257
258
|
reasoningEffort: job.store.settings().reasoning.children,
|
|
258
|
-
sampling: childSampling(job.store.settings(), "suggestions"),
|
|
259
|
+
sampling: childSampling(job.store.settings(), "suggestions", estimateAnswer(Array.from({ length: job.count }, () => 200), job.store.settings().prompts.language)),
|
|
259
260
|
...childModel(job.store.settings(), "suggestions"),
|
|
260
261
|
trace: childTrace(job.store, "suggestions", "suggestions"),
|
|
261
262
|
persona: promptFor(parent, "roleplay/suggester", job.store.settings().prompts.language, { max: 200 }),
|
|
@@ -1374,11 +1375,11 @@ function knowledgeOf(session, characterId, turn, visibleCharacters) {
|
|
|
1374
1375
|
for (const otherId of visibleCharacters ?? (placement === void 0 ? [] : session.placements.at(placement.locationId, "character").map((other) => other.entityId))) if (otherId !== characterId && session.relationships.get(characterId, otherId) === void 0) session.relationships.upsert(characterId, otherId, { label: "acquaintance" }, turn);
|
|
1375
1376
|
const locations = session.knowledge.list(characterId, "location").flatMap((entry) => {
|
|
1376
1377
|
const place = session.locations.get(entry.subjectId);
|
|
1377
|
-
return place === void 0 ? [] : [
|
|
1378
|
+
return place === void 0 ? [] : [place.name];
|
|
1378
1379
|
});
|
|
1379
1380
|
const items = session.knowledge.list(characterId, "item").flatMap((entry) => {
|
|
1380
1381
|
const item = session.items.get(entry.subjectId);
|
|
1381
|
-
return item === void 0 ? [] : [
|
|
1382
|
+
return item === void 0 ? [] : [item.name];
|
|
1382
1383
|
});
|
|
1383
1384
|
const allExits = session.locations.allExits();
|
|
1384
1385
|
const routes = session.knowledge.list(characterId, "exit").flatMap((entry) => {
|
|
@@ -1390,11 +1391,11 @@ function knowledgeOf(session, characterId, turn, visibleCharacters) {
|
|
|
1390
1391
|
return {
|
|
1391
1392
|
characters: session.relationships.from(characterId).flatMap((relation) => {
|
|
1392
1393
|
const character = session.characters.get(relation.toId);
|
|
1393
|
-
return character === void 0 ? [] : [
|
|
1394
|
+
return character === void 0 ? [] : [character.name];
|
|
1394
1395
|
}),
|
|
1395
1396
|
factions: session.factionRelationships.from("character", characterId).filter((relation) => relation.toKind === "faction").flatMap((relation) => {
|
|
1396
1397
|
const faction = session.factions.get(relation.toId);
|
|
1397
|
-
return faction === void 0 ? [] : [
|
|
1398
|
+
return faction === void 0 ? [] : [faction.name];
|
|
1398
1399
|
}),
|
|
1399
1400
|
locations,
|
|
1400
1401
|
items,
|
|
@@ -1612,8 +1613,9 @@ function storeOf(session, folder, problem, queryVector, wall, prompt) {
|
|
|
1612
1613
|
addNote: (kind, turn, lines) => {
|
|
1613
1614
|
addToNote(session, kind, turn, lines);
|
|
1614
1615
|
},
|
|
1615
|
-
recordActivity: (entry) => {
|
|
1616
|
-
|
|
1616
|
+
recordActivity: (entry, detail) => {
|
|
1617
|
+
if (detail !== void 0 && entry.run !== void 0) keepRunDetail(folder, entry.run, detail);
|
|
1618
|
+
forgetRunDetails(folder, recordActivity(session, entry));
|
|
1617
1619
|
},
|
|
1618
1620
|
notes: () => readNotes(session),
|
|
1619
1621
|
clearNotes: () => {
|
|
@@ -1661,13 +1663,18 @@ function storesOf(state, sessionsDir, problem = () => void 0, queryVector = () =
|
|
|
1661
1663
|
}
|
|
1662
1664
|
//#endregion
|
|
1663
1665
|
//#region src/shared/ooc.ts
|
|
1664
|
-
/** What the player writes to
|
|
1665
|
-
const OOC_COMMAND = /^\s*\/ooc(?=\s|$)\s*([\s\S]*)$/i;
|
|
1666
|
-
/** The command in a player's message, or nothing when the message is an ordinary action. The
|
|
1666
|
+
/** What the player writes to speak to the game master as the author of the story: a command (`/ooc`) or a question (`/ask`), a turn of its own, never mixed with an action of the character. */
|
|
1667
|
+
const OOC_COMMAND = /^\s*\/(ooc|ask)(?=\s|$)\s*([\s\S]*)$/i;
|
|
1668
|
+
/** The command or question in a player's message, or nothing when the message is an ordinary action. The request may be empty. */
|
|
1667
1669
|
function parseOoc(text) {
|
|
1668
1670
|
const match = OOC_COMMAND.exec(text);
|
|
1669
|
-
return match === null ? void 0 : {
|
|
1671
|
+
return match === null ? void 0 : {
|
|
1672
|
+
request: (match[2] ?? "").trim(),
|
|
1673
|
+
question: match[1].toLowerCase() === "ask"
|
|
1674
|
+
};
|
|
1670
1675
|
}
|
|
1676
|
+
/** The longest answer to an out-of-character question. */
|
|
1677
|
+
const MAX_OOC_ANSWER = 1200;
|
|
1671
1678
|
//#endregion
|
|
1672
1679
|
//#region src/host/orchestration/hooks.ts
|
|
1673
1680
|
const notice = (text) => createUserMessage({
|
|
@@ -1749,7 +1756,7 @@ function installHooks(ctx, deps) {
|
|
|
1749
1756
|
turn: payload.turn,
|
|
1750
1757
|
cast
|
|
1751
1758
|
}) : { ok: true };
|
|
1752
|
-
if (ooc !== void 0 && ooc.request !== "") engine.beginOoc(owner, ooc.request);
|
|
1759
|
+
if (ooc !== void 0 && ooc.request !== "") engine.beginOoc(owner, ooc.request, ooc.question);
|
|
1753
1760
|
else if (ooc === void 0) engine.expectJudgement(owner);
|
|
1754
1761
|
const queryVector = await store.queryVector(ooc?.request ?? text);
|
|
1755
1762
|
store.clearNotes();
|
|
@@ -1785,10 +1792,10 @@ function installHooks(ctx, deps) {
|
|
|
1785
1792
|
else if (ooc === void 0 && plotAuthorNeeded(store, payload.turn)) lines.push(store.text("roleplay/plot-hint"));
|
|
1786
1793
|
const rules = store.rules();
|
|
1787
1794
|
if (rules.length > 0) lines.push(store.text("roleplay/rules", { rules: rules.map((rule) => `- ${rule}`).join("\n") }));
|
|
1788
|
-
if (ooc !== void 0 && ooc.request !== "") lines.push(store.text("roleplay/ooc-command", { request: ooc.request }));
|
|
1795
|
+
if (ooc !== void 0 && ooc.request !== "") lines.push(store.text(ooc.question ? "roleplay/ask-question" : "roleplay/ooc-command", { request: ooc.request }));
|
|
1789
1796
|
if (ooc !== void 0 && ooc.request === "") {
|
|
1790
1797
|
engine.skip(owner);
|
|
1791
|
-
lines.push(store.text("roleplay/ooc-empty"));
|
|
1798
|
+
lines.push(store.text("roleplay/ooc-empty", { command: ooc.question ? "/ask" : "/ooc" }));
|
|
1792
1799
|
}
|
|
1793
1800
|
if (!verdict.ok) {
|
|
1794
1801
|
engine.skip(owner);
|
|
@@ -1816,107 +1823,6 @@ function currentTurn(agent) {
|
|
|
1816
1823
|
return 0;
|
|
1817
1824
|
}
|
|
1818
1825
|
//#endregion
|
|
1819
|
-
//#region src/host/orchestration/protocol.ts
|
|
1820
|
-
const COMMITMENTS = [
|
|
1821
|
-
"low",
|
|
1822
|
-
"medium",
|
|
1823
|
-
"high"
|
|
1824
|
-
];
|
|
1825
|
-
const RESULTS = [
|
|
1826
|
-
"succeeded",
|
|
1827
|
-
"partial",
|
|
1828
|
-
"failed"
|
|
1829
|
-
];
|
|
1830
|
-
/** What a character is asked to produce; the sub-agent runtime enforces it. */
|
|
1831
|
-
const CHARACTER_ACTION_SCHEMA = {
|
|
1832
|
-
type: "object",
|
|
1833
|
-
properties: {
|
|
1834
|
-
intent: {
|
|
1835
|
-
type: "string",
|
|
1836
|
-
description: "What the character does, or tries to do, now, as an action in one or two sentences, without any speech."
|
|
1837
|
-
},
|
|
1838
|
-
utterance: {
|
|
1839
|
-
type: "string",
|
|
1840
|
-
description: "What the character says aloud, word for word and in its own voice, exactly as it will be shown. Leave out when it says nothing."
|
|
1841
|
-
},
|
|
1842
|
-
commitment: {
|
|
1843
|
-
type: "string",
|
|
1844
|
-
enum: [...COMMITMENTS],
|
|
1845
|
-
description: "How hard the character will keep trying if this does not work out: low gives up at once, high persists."
|
|
1846
|
-
}
|
|
1847
|
-
},
|
|
1848
|
-
required: ["intent", "commitment"],
|
|
1849
|
-
additionalProperties: false
|
|
1850
|
-
};
|
|
1851
|
-
/** The schema for a character acting away from the player: the same answer, which also says in a line what happened, so no second request is needed. */
|
|
1852
|
-
const AWAY_ACTION_SCHEMA = {
|
|
1853
|
-
...CHARACTER_ACTION_SCHEMA,
|
|
1854
|
-
properties: {
|
|
1855
|
-
...CHARACTER_ACTION_SCHEMA.properties,
|
|
1856
|
-
account: {
|
|
1857
|
-
type: "string",
|
|
1858
|
-
description: "One sentence, in the third person and the past tense, of what came of this, as someone who only saw it from outside would tell it afterwards: no thoughts, no secrets."
|
|
1859
|
-
}
|
|
1860
|
-
},
|
|
1861
|
-
required: [
|
|
1862
|
-
"intent",
|
|
1863
|
-
"commitment",
|
|
1864
|
-
"account"
|
|
1865
|
-
]
|
|
1866
|
-
};
|
|
1867
|
-
const MAX_ACCOUNT = 300;
|
|
1868
|
-
/** @throws {Error} when the value is not a character action. */
|
|
1869
|
-
const QUOTE_PAIRS = [
|
|
1870
|
-
["\"", "\""],
|
|
1871
|
-
["“", "”"],
|
|
1872
|
-
["「", "」"],
|
|
1873
|
-
["『", "』"],
|
|
1874
|
-
["«", "»"],
|
|
1875
|
-
["‘", "’"]
|
|
1876
|
-
];
|
|
1877
|
-
/** Words said aloud, without the quotation marks a model puts around them: the output puts its own. */
|
|
1878
|
-
function unquoted(text) {
|
|
1879
|
-
let words = text.trim();
|
|
1880
|
-
for (let again = true; again;) {
|
|
1881
|
-
again = false;
|
|
1882
|
-
for (const [open, close] of QUOTE_PAIRS) if (words.length >= 2 && words.startsWith(open) && words.endsWith(close) && !words.slice(open.length, -close.length).includes(close)) {
|
|
1883
|
-
words = words.slice(open.length, words.length - close.length).trim();
|
|
1884
|
-
again = true;
|
|
1885
|
-
}
|
|
1886
|
-
}
|
|
1887
|
-
return words;
|
|
1888
|
-
}
|
|
1889
|
-
const LEAKED_FIELD = /<\/?(?:intent|parameter(?:=\w+)?)>/g;
|
|
1890
|
-
/** A model that writes the markup of its call into a field puts the next field's text inside this one. */
|
|
1891
|
-
function splitLeakedFields(intent) {
|
|
1892
|
-
const at = intent.search(/<parameter=utterance>/);
|
|
1893
|
-
if (at === -1) return { intent: intent.replace(LEAKED_FIELD, " ") };
|
|
1894
|
-
const spoken = intent.slice(at).replace(LEAKED_FIELD, " ").trim();
|
|
1895
|
-
return {
|
|
1896
|
-
intent: intent.slice(0, at).replace(LEAKED_FIELD, " "),
|
|
1897
|
-
...spoken === "" ? {} : { utterance: spoken }
|
|
1898
|
-
};
|
|
1899
|
-
}
|
|
1900
|
-
function parseCharacterAction(value) {
|
|
1901
|
-
if (typeof value !== "object" || value === null) throw new Error("the character did not answer with an object");
|
|
1902
|
-
const { intent: rawIntent, utterance: rawUtterance, commitment, account } = value;
|
|
1903
|
-
const { intent, utterance } = typeof rawIntent === "string" && rawUtterance === void 0 ? splitLeakedFields(rawIntent) : {
|
|
1904
|
-
intent: rawIntent,
|
|
1905
|
-
utterance: rawUtterance
|
|
1906
|
-
};
|
|
1907
|
-
if (typeof intent !== "string" || intent.trim() === "") throw new Error("the character gave no intent");
|
|
1908
|
-
if (!COMMITMENTS.includes(commitment)) throw new Error(`the character gave an unknown commitment "${String(commitment)}"`);
|
|
1909
|
-
if (utterance !== void 0 && typeof utterance !== "string") throw new Error("the character's utterance is not text");
|
|
1910
|
-
const spoken = utterance === void 0 ? void 0 : unquoted(utterance);
|
|
1911
|
-
const told = typeof account === "string" ? account.trim().slice(0, MAX_ACCOUNT) : "";
|
|
1912
|
-
return {
|
|
1913
|
-
intent: intent.trim(),
|
|
1914
|
-
...spoken === void 0 || spoken === "" ? {} : { utterance: spoken },
|
|
1915
|
-
commitment,
|
|
1916
|
-
...told === "" ? {} : { account: told }
|
|
1917
|
-
};
|
|
1918
|
-
}
|
|
1919
|
-
//#endregion
|
|
1920
1826
|
//#region src/host/orchestration/character-runner.ts
|
|
1921
1827
|
/**
|
|
1922
1828
|
* Runs each character as a one-shot child of the game master. It is the character and nothing
|
|
@@ -1933,19 +1839,64 @@ function characterRunner(deps) {
|
|
|
1933
1839
|
policy: deps.store.settings().memory,
|
|
1934
1840
|
queryVector: deps.store.queryVector
|
|
1935
1841
|
}), request.member.character.id) : [];
|
|
1936
|
-
|
|
1842
|
+
const settings = deps.store.settings();
|
|
1843
|
+
const named = request.member.character.tier === "background" && settings.models.background.trim() !== "" ? settings.models.background : settings.models.characters;
|
|
1844
|
+
const result = await runChild({
|
|
1937
1845
|
parent: deps.parent,
|
|
1938
1846
|
signal,
|
|
1939
|
-
sampling: childSampling(
|
|
1940
|
-
...childModel(
|
|
1941
|
-
trace: childTrace(deps.store, "characters", request.member.character.name),
|
|
1847
|
+
sampling: childSampling(settings, "characters", answerEstimate(settings.prompts.language, request.away === true)),
|
|
1848
|
+
...childModel(settings, "characters", named),
|
|
1849
|
+
trace: childTrace(deps.store, "characters", request.member.character.name, recalledOf(request), named),
|
|
1942
1850
|
reasoningEffort: deps.store.settings().reasoning.children,
|
|
1943
1851
|
persona: brief.persona,
|
|
1944
1852
|
prompt: brief.prompt,
|
|
1945
1853
|
tools,
|
|
1946
1854
|
maxCalls: deps.store.settings().memory.lookupCalls,
|
|
1947
|
-
result: request.away === true ? AWAY_ACTION_SCHEMA : CHARACTER_ACTION_SCHEMA
|
|
1948
|
-
|
|
1855
|
+
result: request.away === true ? AWAY_ACTION_SCHEMA : CHARACTER_ACTION_SCHEMA,
|
|
1856
|
+
unenforced: ["thought"]
|
|
1857
|
+
});
|
|
1858
|
+
return parseCharacterAction(result);
|
|
1859
|
+
};
|
|
1860
|
+
}
|
|
1861
|
+
/** What the character was reminded of and why, for the run's record, when the recall said why. */
|
|
1862
|
+
function recalledOf(request) {
|
|
1863
|
+
const explained = (request.memories ?? []).filter((memory) => memory.why !== void 0);
|
|
1864
|
+
return explained.length === 0 ? {} : { recalled: explained.slice(0, 30).map(({ clarity, turn, text, why }) => ({
|
|
1865
|
+
clarity,
|
|
1866
|
+
turn,
|
|
1867
|
+
text,
|
|
1868
|
+
why
|
|
1869
|
+
})) };
|
|
1870
|
+
}
|
|
1871
|
+
const ANSWER_SCHEMA = {
|
|
1872
|
+
type: "object",
|
|
1873
|
+
properties: { answer: {
|
|
1874
|
+
type: "string",
|
|
1875
|
+
description: "The answer to the player, in plain words, in a few sentences."
|
|
1876
|
+
} },
|
|
1877
|
+
required: ["answer"],
|
|
1878
|
+
additionalProperties: false
|
|
1879
|
+
};
|
|
1880
|
+
/**
|
|
1881
|
+
* Answers a player's question as an agent that has only what the player's character is given when it acts: the character sheet is left out and nothing the
|
|
1882
|
+
* game master alone knows is in the prompt, so the answer cannot give away what the character could not know.
|
|
1883
|
+
*/
|
|
1884
|
+
function answerRunner(deps) {
|
|
1885
|
+
return async (request, signal) => {
|
|
1886
|
+
const brief = deps.boundaries().characterContext.brief(request);
|
|
1887
|
+
return (await runChild({
|
|
1888
|
+
parent: deps.parent,
|
|
1889
|
+
signal,
|
|
1890
|
+
sampling: childSampling(deps.store.settings(), "records", estimateAnswer([MAX_OOC_ANSWER], deps.store.settings().prompts.language)),
|
|
1891
|
+
...childModel(deps.store.settings(), "records"),
|
|
1892
|
+
trace: childTrace(deps.store, "records", "answer to the player"),
|
|
1893
|
+
reasoningEffort: deps.store.settings().reasoning.children,
|
|
1894
|
+
persona: request.text("roleplay/ask-answerer", { name: request.member.character.name }),
|
|
1895
|
+
prompt: brief.prompt,
|
|
1896
|
+
tools: [],
|
|
1897
|
+
maxCalls: 3,
|
|
1898
|
+
result: ANSWER_SCHEMA
|
|
1899
|
+
})).answer;
|
|
1949
1900
|
};
|
|
1950
1901
|
}
|
|
1951
1902
|
//#endregion
|
|
@@ -2108,6 +2059,7 @@ const INTRODUCE_TOOL = "introduce_thing";
|
|
|
2108
2059
|
const TIME_TOOL = "advance_time";
|
|
2109
2060
|
const PREVIEW_TOOL = "preview_scene";
|
|
2110
2061
|
const RULING_TOOL = "ooc_ruling";
|
|
2062
|
+
const ANSWER_TOOL = "ooc_answer";
|
|
2111
2063
|
const JUDGE_TOOL = "judge_action";
|
|
2112
2064
|
const PLOT_TOOL = "record_plot_progress";
|
|
2113
2065
|
const AUTHOR_PLOT_TOOL = "author_plot";
|
|
@@ -2119,7 +2071,7 @@ function failures(list) {
|
|
|
2119
2071
|
return list.length === 0 ? [] : [
|
|
2120
2072
|
"",
|
|
2121
2073
|
"Could not act:",
|
|
2122
|
-
...list.map((failure) => `- ${failure.character} (${failure.name}): ${failure.reason}`)
|
|
2074
|
+
...list.map((failure) => failure.technical === true ? `- ${failure.character} (${failure.name}): no decision came back. That is a fault of the system, not something that happened in the story: nothing is recorded for ${failure.name} this round, so write nothing for them and say nothing about why` : `- ${failure.character} (${failure.name}): ${failure.reason}`)
|
|
2123
2075
|
];
|
|
2124
2076
|
}
|
|
2125
2077
|
function renderAttempt(attempt) {
|
|
@@ -2203,16 +2155,16 @@ function createTools$1(deps) {
|
|
|
2203
2155
|
name: ACTIVATE_TOOL,
|
|
2204
2156
|
description: "Let the characters act: one round of the turn. Each character asked is asked, separately and at the same time, what it intends to do and say in the situation you describe and how committed it is. Call it after reading what the player did, and again for a later round only when the story genuinely needs the characters to react to what just happened; the number of rounds for one player message is limited.",
|
|
2205
2157
|
parameters: {
|
|
2206
|
-
situation: {
|
|
2207
|
-
type: "string",
|
|
2208
|
-
required: true,
|
|
2209
|
-
description: "An objective description of the moment: in the first round what the player's character just did or said; in a later round what is happening now, following from the rounds before (they are told those too). Every character is told this and nothing more, so leave out anyone's private thoughts."
|
|
2210
|
-
},
|
|
2211
2158
|
characters: {
|
|
2212
2159
|
type: "array",
|
|
2213
2160
|
required: true,
|
|
2214
2161
|
items: { type: "string" },
|
|
2215
2162
|
description: "Keys of the characters to ask, chosen by you from the scene (where each is, what each is doing). Ask only those the moment reaches: those in the same place, and those elsewhere who can plainly see, hear or are summoned; a character elsewhere perceives nothing by itself. Never the whole cast. In a later round ask only those the moment calls for."
|
|
2163
|
+
},
|
|
2164
|
+
situation: {
|
|
2165
|
+
type: "string",
|
|
2166
|
+
required: true,
|
|
2167
|
+
description: "An objective description of the moment: in the first round what the player's character just did or said; in a later round what is happening now, following from the rounds before (they are told those too). Every character is told this and nothing more, so leave out anyone's private thoughts."
|
|
2216
2168
|
}
|
|
2217
2169
|
},
|
|
2218
2170
|
output: {
|
|
@@ -2241,10 +2193,6 @@ function createTools$1(deps) {
|
|
|
2241
2193
|
name: RESOLVE_TOOL,
|
|
2242
2194
|
description: "Decide what actually happens to each character that acted in the current round, from their intents and the scene only. Intents can conflict; settle them consistently. A character whose intent fails and who was highly committed gets one more chance, and you resolve that attempt too. The player's character is not asked to act, but you may include an outcome for it (its key is in the scene) when what the player did moves it somewhere or has it handle things; when nobody else needs to react (the player just walks somewhere, picks something up), call this with only that outcome, without activate_characters. When nobody retries, the round is recorded and you decide whether another round is needed or the turn goes back to the player.",
|
|
2243
2195
|
parameters: {
|
|
2244
|
-
elapsed_seconds: {
|
|
2245
|
-
type: "integer",
|
|
2246
|
-
description: "How long this round took in the story, in seconds:: a few seconds for a remark, a minute or two for a conversation, longer for work or a journey. Left out, the chat's default applies. Not when the story's time follows the wall clock."
|
|
2247
|
-
},
|
|
2248
2196
|
outcomes: {
|
|
2249
2197
|
type: "array",
|
|
2250
2198
|
required: true,
|
|
@@ -2391,6 +2339,10 @@ function createTools$1(deps) {
|
|
|
2391
2339
|
}
|
|
2392
2340
|
}
|
|
2393
2341
|
}
|
|
2342
|
+
},
|
|
2343
|
+
elapsed_seconds: {
|
|
2344
|
+
type: "integer",
|
|
2345
|
+
description: "How long this round took in the story, in seconds:: a few seconds for a remark, a minute or two for a conversation, longer for work or a journey. Left out, the chat's default applies. Not when the story's time follows the wall clock."
|
|
2394
2346
|
}
|
|
2395
2347
|
},
|
|
2396
2348
|
output: {
|
|
@@ -2695,6 +2647,11 @@ function createTools$1(deps) {
|
|
|
2695
2647
|
name: TIME_TOOL,
|
|
2696
2648
|
description: "Skip ahead in the story's time, between rounds: the player's character sleeps till morning, waits, or travels for days. Say either how long (by_minutes) or until when (until_time, and days_later for a later day). Time does not otherwise pass faster than the rounds take; use this only for a stretch that is summarised rather than played. Nothing else changes: if the skip changes what characters are doing or where they are, run a round for them afterwards. Not available when the story's time follows the wall clock.",
|
|
2697
2649
|
parameters: {
|
|
2650
|
+
reason: {
|
|
2651
|
+
type: "string",
|
|
2652
|
+
required: true,
|
|
2653
|
+
description: "What happens in the time that passes, in a sentence (\"they sleep\", \"the ferry crosses\")."
|
|
2654
|
+
},
|
|
2698
2655
|
by_minutes: {
|
|
2699
2656
|
type: "integer",
|
|
2700
2657
|
description: "How many minutes to skip."
|
|
@@ -2706,11 +2663,6 @@ function createTools$1(deps) {
|
|
|
2706
2663
|
days_later: {
|
|
2707
2664
|
type: "integer",
|
|
2708
2665
|
description: "With until_time: that many days after today (0 is later today, 1 is tomorrow)."
|
|
2709
|
-
},
|
|
2710
|
-
reason: {
|
|
2711
|
-
type: "string",
|
|
2712
|
-
required: true,
|
|
2713
|
-
description: "What happens in the time that passes, in a sentence (\"they sleep\", \"the ferry crosses\")."
|
|
2714
2666
|
}
|
|
2715
2667
|
},
|
|
2716
2668
|
output: {
|
|
@@ -2797,6 +2749,27 @@ function createTools$1(deps) {
|
|
|
2797
2749
|
return Promise.resolve(record(deps.engine.rule(owner, args.legal, args.reason)));
|
|
2798
2750
|
}
|
|
2799
2751
|
});
|
|
2752
|
+
const answer = defineTool({
|
|
2753
|
+
name: ANSWER_TOOL,
|
|
2754
|
+
description: "Have the player's out-of-character question answered, as the very first thing of a question turn. You do not write the answer: it is written from what the player's character knows (what it remembers, knows to exist, carries and sees where it is), so that nothing hidden is given away. Nothing in the story changes, nobody is asked to act and time does not pass.",
|
|
2755
|
+
parameters: {},
|
|
2756
|
+
output: {
|
|
2757
|
+
schema: { type: "json" },
|
|
2758
|
+
render: () => [{
|
|
2759
|
+
type: "text",
|
|
2760
|
+
text: `The player is shown the answer. Now call ${NARRATE_TOOL} with no segments.`
|
|
2761
|
+
}]
|
|
2762
|
+
},
|
|
2763
|
+
isConcurrencySafe: () => false,
|
|
2764
|
+
async execute(_args, exec) {
|
|
2765
|
+
const { agent, owner } = ownerOf(exec.agent);
|
|
2766
|
+
return record(await deps.engine.answer(owner, answerRunner({
|
|
2767
|
+
parent: agent,
|
|
2768
|
+
boundaries: deps.boundaries,
|
|
2769
|
+
store: storeOf(owner.chat)
|
|
2770
|
+
}), exec.signal));
|
|
2771
|
+
}
|
|
2772
|
+
});
|
|
2800
2773
|
return [
|
|
2801
2774
|
defineTool({
|
|
2802
2775
|
name: JUDGE_TOOL,
|
|
@@ -2807,6 +2780,10 @@ function createTools$1(deps) {
|
|
|
2807
2780
|
required: true,
|
|
2808
2781
|
description: "First, go through every rule the scene lists, one by one, in a few words each: does what the character tries need what the rule excludes? The rules are literal. Then whether it is viable here. Write \"no rules listed\" when there are none."
|
|
2809
2782
|
},
|
|
2783
|
+
at: {
|
|
2784
|
+
type: "string",
|
|
2785
|
+
description: "The key of the place where the action happens, only when the player says it happens at a particular place (talking to the baker in the market, searching the cellar) rather than where the character is. If the character is somewhere else the action cannot work: it has to go there first. Leave it out when the action happens where the character is, and when the player is going somewhere (the journey is its own action)."
|
|
2786
|
+
},
|
|
2810
2787
|
allowed: {
|
|
2811
2788
|
type: "boolean",
|
|
2812
2789
|
required: true,
|
|
@@ -2822,17 +2799,18 @@ function createTools$1(deps) {
|
|
|
2822
2799
|
schema: { type: "json" },
|
|
2823
2800
|
render: (_args, value) => [{
|
|
2824
2801
|
type: "text",
|
|
2825
|
-
text: value.allowed ? `Judged: it can work. Now call ${PREVIEW_TOOL} (when the scene asks for it) and ${ACTIVATE_TOOL}, describing only what the character does.` : `Judged: it cannot work. Play the failed attempt: ${RESOLVE_TOOL} with one outcome for the player's character (result failed, one sentence: what it does, what comes of it, why not) and nothing else, then ${NARRATE_TOOL}. Narrate what a witness would see of the attempt, never the effect the player claimed.`
|
|
2802
|
+
text: value.allowed ? `Judged: it can work. Now call ${PREVIEW_TOOL} (when the scene asks for it) and ${ACTIVATE_TOOL}, describing only what the character does.` : `Judged: it cannot work${value.elsewhere === true ? ` (${value.reason})` : ""}. Play the failed attempt: ${RESOLVE_TOOL} with one outcome for the player's character (result failed, one sentence: what it does, what comes of it, why not) and nothing else, then ${NARRATE_TOOL}. Narrate what a witness would see of the attempt, never the effect the player claimed.`
|
|
2826
2803
|
}]
|
|
2827
2804
|
},
|
|
2828
2805
|
isConcurrencySafe: () => false,
|
|
2829
2806
|
execute(args, exec) {
|
|
2830
2807
|
const { owner } = ownerOf(exec.agent);
|
|
2831
|
-
return Promise.resolve(record(deps.engine.judge(owner, args.allowed, args.reason)));
|
|
2808
|
+
return Promise.resolve(record(deps.engine.judge(owner, args.allowed, args.reason, args.at)));
|
|
2832
2809
|
}
|
|
2833
2810
|
}),
|
|
2834
2811
|
preview,
|
|
2835
2812
|
ruling,
|
|
2813
|
+
answer,
|
|
2836
2814
|
activate,
|
|
2837
2815
|
resolve,
|
|
2838
2816
|
skip,
|
|
@@ -3044,10 +3022,83 @@ var AfterTurnJobs = class {
|
|
|
3044
3022
|
}
|
|
3045
3023
|
};
|
|
3046
3024
|
//#endregion
|
|
3025
|
+
//#region src/host/orchestration/whereabouts.ts
|
|
3026
|
+
const within = (inner, outer, places) => {
|
|
3027
|
+
const byId = new Map(places.map((place) => [place.id, place]));
|
|
3028
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3029
|
+
for (let at = inner; at !== void 0 && !seen.has(at.id); at = at.parentId === void 0 ? void 0 : byId.get(at.parentId)) {
|
|
3030
|
+
if (at.id === outer.id) return true;
|
|
3031
|
+
seen.add(at.id);
|
|
3032
|
+
}
|
|
3033
|
+
return false;
|
|
3034
|
+
};
|
|
3035
|
+
/**
|
|
3036
|
+
* Compares the place an action is said to happen at (by key) with where the character is. Only a proven mismatch counts: a character that is
|
|
3037
|
+
* nowhere, or a world without places, is no evidence, and a place that contains the other (a room and its building) is the same for this.
|
|
3038
|
+
*/
|
|
3039
|
+
function whereabouts(places, here, at) {
|
|
3040
|
+
const target = places.find((place) => locationKey(place) === at);
|
|
3041
|
+
if (target === void 0) return { kind: "unknown" };
|
|
3042
|
+
if (here === void 0) return { kind: "unplaced" };
|
|
3043
|
+
return within(here, target, places) || within(target, here, places) ? { kind: "here" } : {
|
|
3044
|
+
kind: "elsewhere",
|
|
3045
|
+
target,
|
|
3046
|
+
here
|
|
3047
|
+
};
|
|
3048
|
+
}
|
|
3049
|
+
//#endregion
|
|
3047
3050
|
//#region src/host/orchestration/turn-engine.ts
|
|
3048
3051
|
const MAX_OBJECTIONS = 3;
|
|
3049
3052
|
/** The most a single round may be said to take: a day. A longer time passing is skipped with `advanceTime`. */
|
|
3050
3053
|
const MAX_ROUND_SECONDS = 86400;
|
|
3054
|
+
/**
|
|
3055
|
+
* A model sometimes sends who speaks as a segment of its own (an utterance with no character whose text is the character's key or name) followed by the words with no
|
|
3056
|
+
* character either. When the label is exactly a character of the scene, the label is folded into the words that follow it.
|
|
3057
|
+
*/
|
|
3058
|
+
function foldSpeakerLabels(segments, cast) {
|
|
3059
|
+
const found = (label) => {
|
|
3060
|
+
const wanted = label.trim().normalize("NFKC").toLowerCase();
|
|
3061
|
+
return wanted === "" ? void 0 : cast.find((member) => member.key.toLowerCase() === wanted || member.character.name.normalize("NFKC").toLowerCase() === wanted);
|
|
3062
|
+
};
|
|
3063
|
+
const folded = [];
|
|
3064
|
+
for (let index = 0; index < segments.length; index += 1) {
|
|
3065
|
+
const segment = segments[index];
|
|
3066
|
+
const next = segments[index + 1];
|
|
3067
|
+
const speaker = segment.type === "utterance" && (segment.character ?? "") === "" && next?.type === "utterance" && (next.character ?? "") === "" ? found(segment.text) : void 0;
|
|
3068
|
+
if (speaker === void 0) {
|
|
3069
|
+
folded.push(segment);
|
|
3070
|
+
continue;
|
|
3071
|
+
}
|
|
3072
|
+
folded.push({
|
|
3073
|
+
...next,
|
|
3074
|
+
character: speaker.key
|
|
3075
|
+
});
|
|
3076
|
+
index += 1;
|
|
3077
|
+
}
|
|
3078
|
+
return folded;
|
|
3079
|
+
}
|
|
3080
|
+
/** How alike two situations have to be for a round about the second to bring nothing new, by the share of three-letter pieces they have in common. */
|
|
3081
|
+
const SAME_SITUATION = .85;
|
|
3082
|
+
const pieces = (text) => {
|
|
3083
|
+
const found = /* @__PURE__ */ new Set();
|
|
3084
|
+
const chars = [...text];
|
|
3085
|
+
for (let index = 0; index + 3 <= chars.length; index += 1) found.add(chars.slice(index, index + 3).join(""));
|
|
3086
|
+
return found;
|
|
3087
|
+
};
|
|
3088
|
+
/** Whether the same characters (or some of them) are asked about a situation that is, but for a few words, one they were already asked about in the turn. */
|
|
3089
|
+
function repeatsEarlier(signature, asked) {
|
|
3090
|
+
const [keys = "", text = ""] = signature.split("|");
|
|
3091
|
+
const now = new Set(keys.split(","));
|
|
3092
|
+
const mine = pieces(text);
|
|
3093
|
+
return asked.some((earlier) => {
|
|
3094
|
+
const [before = "", was = ""] = earlier.split("|");
|
|
3095
|
+
if (![...now].every((key) => before.split(",").includes(key))) return false;
|
|
3096
|
+
const theirs = pieces(was);
|
|
3097
|
+
const shared = [...mine].filter((piece) => theirs.has(piece)).length;
|
|
3098
|
+
const union = mine.size + theirs.size - shared;
|
|
3099
|
+
return union === 0 || shared / union >= SAME_SITUATION;
|
|
3100
|
+
});
|
|
3101
|
+
}
|
|
3051
3102
|
/** The game master is asked to correct its output this many times before it is corrected for it. */
|
|
3052
3103
|
const MAX_REFUSALS = 2;
|
|
3053
3104
|
/** The runs of the record keeper that may fail one after another before the rounds waiting for it are given up, so that they cannot pile up for ever. */
|
|
@@ -3100,12 +3151,75 @@ var TurnEngine = class {
|
|
|
3100
3151
|
* The turn is an out-of-character command of the player: nobody is asked to act and no preview is shown. The game master rules on
|
|
3101
3152
|
* whether it is allowed (`rule`) and, if so, makes it happen with outcomes for any characters, with the world's tools, and says what the player perceives.
|
|
3102
3153
|
*/
|
|
3103
|
-
beginOoc(owner, request) {
|
|
3154
|
+
beginOoc(owner, request, question = false) {
|
|
3104
3155
|
const state = this.stateFor(owner);
|
|
3105
|
-
state.ooc = {
|
|
3156
|
+
state.ooc = {
|
|
3157
|
+
request,
|
|
3158
|
+
...question ? { question: true } : {}
|
|
3159
|
+
};
|
|
3106
3160
|
state.previewWanted = false;
|
|
3107
3161
|
}
|
|
3108
3162
|
/**
|
|
3163
|
+
* The answer to an out-of-character question, which is the whole of that turn: nothing in the story changes and nobody is asked to act. It is not the game
|
|
3164
|
+
* master's to write, who knows what the player's character does not: it comes from what the player's character knows (what it remembers, knows exists, has
|
|
3165
|
+
* on it and sees where it is), so that nothing hidden can be given away.
|
|
3166
|
+
* @throws {Error} when the turn is not a question, it was already answered, or the answer is empty.
|
|
3167
|
+
*/
|
|
3168
|
+
async answer(owner, run, signal) {
|
|
3169
|
+
const state = this.stateFor(owner);
|
|
3170
|
+
if (state.ooc?.question !== true) throw new Error("this turn is not an out-of-character question: there is nothing to answer");
|
|
3171
|
+
if (state.ooc.ruling !== void 0) throw new Error("this question was already answered");
|
|
3172
|
+
const text = (await run(await this.askedRequest(owner, state.ooc.request, signal), signal)).trim();
|
|
3173
|
+
if (text === "") throw new Error("the answer is empty");
|
|
3174
|
+
if (state.ooc.ruling !== void 0) throw new Error("this question was already answered");
|
|
3175
|
+
state.ooc.ruling = {
|
|
3176
|
+
legal: true,
|
|
3177
|
+
note: text.length > 1200 ? `${text.slice(0, 1199)}…` : text,
|
|
3178
|
+
question: true
|
|
3179
|
+
};
|
|
3180
|
+
state.phase = "between";
|
|
3181
|
+
return state.ooc.ruling;
|
|
3182
|
+
}
|
|
3183
|
+
/** What the player's character is given to answer a question with: the same as a character is given to act, with the question as the moment. */
|
|
3184
|
+
async askedRequest(owner, question, signal) {
|
|
3185
|
+
const store = this.storeOf(owner);
|
|
3186
|
+
const cast = store.cast();
|
|
3187
|
+
const member = cast.find((candidate) => candidate.character.control === "player");
|
|
3188
|
+
if (member === void 0) throw new Error("there is no player's character to answer for");
|
|
3189
|
+
const queryVector = await store.queryVector(question, signal);
|
|
3190
|
+
const memories = await this.boundaries().recaller.recall({
|
|
3191
|
+
member,
|
|
3192
|
+
situation: question,
|
|
3193
|
+
turn: owner.turn,
|
|
3194
|
+
policy: store.settings().memory,
|
|
3195
|
+
...queryVector === void 0 ? {} : { queryVector }
|
|
3196
|
+
}, store.story, signal);
|
|
3197
|
+
const place = member.place === void 0 ? void 0 : store.place(member.place.location.id);
|
|
3198
|
+
const company = place === void 0 ? cast : place.here;
|
|
3199
|
+
const relationships = this.knownRelationships(member, company, question, cast, store);
|
|
3200
|
+
const carrying = store.carried(member.character.id);
|
|
3201
|
+
const affiliations = store.affiliations(member.character.id);
|
|
3202
|
+
const tracked = store.trackedFor(member.character.id, company.map((entry) => entry.character.id));
|
|
3203
|
+
return {
|
|
3204
|
+
member,
|
|
3205
|
+
cast,
|
|
3206
|
+
situation: question,
|
|
3207
|
+
attempt: 1,
|
|
3208
|
+
language: store.settings().prompts.language,
|
|
3209
|
+
turn: owner.turn,
|
|
3210
|
+
lookup: false,
|
|
3211
|
+
text: (id, variables) => store.text(id, variables),
|
|
3212
|
+
time: store.time(owner.turn),
|
|
3213
|
+
knowledge: store.knowledge(member.character.id, owner.turn, company.map((entry) => entry.character.id)),
|
|
3214
|
+
...tracked.self === "" && tracked.others.length === 0 ? {} : { tracked },
|
|
3215
|
+
...place === void 0 ? {} : { place },
|
|
3216
|
+
...relationships.length === 0 ? {} : { relationships },
|
|
3217
|
+
...carrying.length === 0 ? {} : { carrying },
|
|
3218
|
+
...affiliations.length === 0 ? {} : { affiliations },
|
|
3219
|
+
...memories.length === 0 ? {} : { memories }
|
|
3220
|
+
};
|
|
3221
|
+
}
|
|
3222
|
+
/**
|
|
3109
3223
|
* The game master's ruling on an out-of-character command: it is legal unless it breaks the rules of the world or the player's own. The reason is
|
|
3110
3224
|
* what the player is told. Either way the turn goes on to its output; only a legal ruling lets the command change anything.
|
|
3111
3225
|
* @throws {Error} when the turn is not an out-of-character command, it was already ruled on, or the reason is empty or too long.
|
|
@@ -3113,6 +3227,7 @@ var TurnEngine = class {
|
|
|
3113
3227
|
rule(owner, legal, reason) {
|
|
3114
3228
|
const state = this.stateFor(owner);
|
|
3115
3229
|
if (state.ooc === void 0) throw new Error("this turn is not an out-of-character command: there is nothing to rule on");
|
|
3230
|
+
if (state.ooc.question === true) throw new Error("this turn is an out-of-character question: answer it with ooc_answer");
|
|
3116
3231
|
if (state.ooc.ruling !== void 0) throw new Error("this command was already ruled on");
|
|
3117
3232
|
const note = reason.trim();
|
|
3118
3233
|
if (note === "") throw new Error("give the reason, in plain words to the player");
|
|
@@ -3127,15 +3242,34 @@ var TurnEngine = class {
|
|
|
3127
3242
|
/**
|
|
3128
3243
|
* The game master judges what the player's character tries, before anything else in an ordinary turn: it works unless it is not viable or it breaks a rule of the
|
|
3129
3244
|
* world or of the player. What does not work is played as a failed attempt for the player's character: nobody is asked to act on it.
|
|
3130
|
-
*
|
|
3245
|
+
* When the action is said to happen at a place (`at`, a place key) and the character is somewhere else, it does not work whatever was judged: the character has to get there first.
|
|
3246
|
+
* @throws {Error} when the turn is not an ordinary one, was already judged, the reason is empty, or `at` is not a place.
|
|
3131
3247
|
*/
|
|
3132
|
-
judge(owner, allowed, reason) {
|
|
3248
|
+
judge(owner, allowed, reason, at) {
|
|
3133
3249
|
const state = this.stateFor(owner);
|
|
3134
|
-
if (state.ooc !== void 0) throw new Error("this turn is an out-of-character command: rule on it with ooc_ruling");
|
|
3250
|
+
if (state.ooc !== void 0) throw new Error("this turn is an out-of-character " + (state.ooc.question === true ? "question: answer it with ooc_answer" : "command: rule on it with ooc_ruling"));
|
|
3135
3251
|
if (state.phase !== "idle") throw new Error("the action is judged first, before anything else");
|
|
3136
3252
|
if (state.judgement !== void 0) throw new Error("the action was already judged");
|
|
3137
3253
|
const why = reason.trim();
|
|
3138
3254
|
if (why === "") throw new Error("give the reason: what the character tries, and the rule or the fact that decides it");
|
|
3255
|
+
const place = at?.trim() ?? "";
|
|
3256
|
+
if (place !== "") {
|
|
3257
|
+
const store = this.storeOf(owner);
|
|
3258
|
+
const player = store.cast().find((member) => member.character.control === "player");
|
|
3259
|
+
const found = whereabouts(store.story.locations.list(), player?.place?.location, place);
|
|
3260
|
+
if (found.kind === "unknown") throw new Error(`there is no place "${place}" (list_locations shows the keys): name the place where the action happens, or leave "at" out when it happens where the character is`);
|
|
3261
|
+
if (found.kind === "elsewhere") {
|
|
3262
|
+
const name = player?.character.name ?? "The character";
|
|
3263
|
+
state.judgement = {
|
|
3264
|
+
allowed: false,
|
|
3265
|
+
reason: `${name} is at ${found.here.name}, not at ${found.target.name} where this happens: ${name} has to go there first`
|
|
3266
|
+
};
|
|
3267
|
+
return {
|
|
3268
|
+
...state.judgement,
|
|
3269
|
+
elsewhere: true
|
|
3270
|
+
};
|
|
3271
|
+
}
|
|
3272
|
+
}
|
|
3139
3273
|
state.judgement = {
|
|
3140
3274
|
allowed,
|
|
3141
3275
|
reason: why
|
|
@@ -3153,6 +3287,7 @@ var TurnEngine = class {
|
|
|
3153
3287
|
/** Why an out-of-character command may not change anything yet, when it may not. */
|
|
3154
3288
|
oocBlock(state) {
|
|
3155
3289
|
if (state.ooc === void 0) return void 0;
|
|
3290
|
+
if (state.ooc.question === true) return state.ooc.ruling === void 0 ? "this turn is an out-of-character question: answer it first with ooc_answer" : "a question changes nothing: call narrate with no segments";
|
|
3156
3291
|
if (state.ooc.ruling === void 0) return "this turn is an out-of-character command: rule on it first with ooc_ruling";
|
|
3157
3292
|
return state.ooc.ruling.legal ? void 0 : "the command was refused: nothing changes; call narrate with no segments to tell the player";
|
|
3158
3293
|
}
|
|
@@ -3166,7 +3301,7 @@ var TurnEngine = class {
|
|
|
3166
3301
|
*/
|
|
3167
3302
|
async activate(owner, input, run, signal) {
|
|
3168
3303
|
const state = this.stateFor(owner);
|
|
3169
|
-
if (state.ooc !== void 0) throw new Error("an out-of-character command asks nobody to act: decree what happens with resolve_round, or narrate");
|
|
3304
|
+
if (state.ooc !== void 0) throw new Error(state.ooc.question === true ? "a question asks nobody to act and changes nothing: answer it with ooc_answer" : "an out-of-character command asks nobody to act: decree what happens with resolve_round, or narrate");
|
|
3170
3305
|
if (state.phase !== "idle" && state.phase !== "between") throw new Error(this.explain(state, "activate_characters"));
|
|
3171
3306
|
if (this.judgeDue(state)) throw new Error(this.explain(state, "activate_characters"));
|
|
3172
3307
|
if (state.judgement?.allowed === false && state.rounds === 0) throw new Error(`the action cannot work (${state.judgement.reason}): nobody is asked to act on it. Play the failed attempt with resolve_round: one outcome for the player's character, result failed`);
|
|
@@ -3185,7 +3320,7 @@ var TurnEngine = class {
|
|
|
3185
3320
|
if (keys.length === 0) throw new Error("there is no character to activate");
|
|
3186
3321
|
if (keys.length > most) throw new Error(`${keys.length} characters were asked, which is more than one round allows (${most}): ask only those the moment actually reaches, and the others in a later round if the story comes to them`);
|
|
3187
3322
|
const signature = `${[...keys].sort().join(",")}|${situation.normalize("NFKC").toLowerCase().replace(/[^\p{L}\p{N}]+/gu, " ").trim()}`;
|
|
3188
|
-
if (state.asked
|
|
3323
|
+
if (repeatsEarlier(signature, state.asked)) throw new Error("the same characters were already asked about the same situation in this message, so nothing new would come of it: say what changed, ask others, or deliver the output with narrate");
|
|
3189
3324
|
state.situation = situation;
|
|
3190
3325
|
state.cast = cast;
|
|
3191
3326
|
state.recalled.clear();
|
|
@@ -3322,7 +3457,7 @@ var TurnEngine = class {
|
|
|
3322
3457
|
*/
|
|
3323
3458
|
preview(owner, text) {
|
|
3324
3459
|
const state = this.stateFor(owner);
|
|
3325
|
-
if (state.ooc !== void 0) throw new Error("an out-of-character
|
|
3460
|
+
if (state.ooc !== void 0) throw new Error("an out-of-character turn shows no preview: go on with " + (state.ooc.question === true ? "ooc_answer" : "ooc_ruling"));
|
|
3326
3461
|
if (this.judgeDue(state)) throw new Error("judge the action first with judge_action");
|
|
3327
3462
|
if (!this.storeOf(owner).settings().preview.enabled) throw new Error("this chat shows no preview: go on with activate_characters");
|
|
3328
3463
|
if (state.previewed) throw new Error("a preview was already shown for this message: go on with activate_characters");
|
|
@@ -3386,6 +3521,7 @@ var TurnEngine = class {
|
|
|
3386
3521
|
if (unknown.length > 0) throw new Error(`addressed: ${unknown.join(", ")} ${unknown.length === 1 ? "is" : "are"} not ${unknown.length === 1 ? "a beat" : "beats"} still to weave in (the scene lists them)`);
|
|
3387
3522
|
state.addressed = [...new Set(addressed)];
|
|
3388
3523
|
if (state.ooc !== void 0) {
|
|
3524
|
+
if (state.ooc.question === true && segments.length > 0) throw new Error("a question is answered with ooc_answer: deliver no segments, the player is shown the answer");
|
|
3389
3525
|
if (state.ooc.ruling?.legal !== true && segments.length > 0) throw new Error("the command was refused: deliver no segments, the player is told why");
|
|
3390
3526
|
if (segments.some((segment) => segment.type === "utterance")) throw new Error("nobody was asked to speak in an out-of-character command: narrate only what the player perceives");
|
|
3391
3527
|
return await this.deliver(owner, state, segments.map((segment) => ({
|
|
@@ -3403,7 +3539,7 @@ var TurnEngine = class {
|
|
|
3403
3539
|
if (written === void 0) throw new Error("deliver the output: give its segments");
|
|
3404
3540
|
return await this.deliver(owner, state, written, true, run);
|
|
3405
3541
|
}
|
|
3406
|
-
const typed = segments.map((segment) => segment.type === "utterance" ? {
|
|
3542
|
+
const typed = foldSpeakerLabels(segments, state.cast).map((segment) => segment.type === "utterance" ? {
|
|
3407
3543
|
type: "utterance",
|
|
3408
3544
|
character: segment.character ?? "",
|
|
3409
3545
|
name: state.cast.find((member) => member.key === segment.character)?.character.name ?? segment.character ?? "",
|
|
@@ -3708,10 +3844,11 @@ var TurnEngine = class {
|
|
|
3708
3844
|
return state.phase === "idle" && state.previewWanted && !state.previewed && state.judgement?.allowed !== false;
|
|
3709
3845
|
}
|
|
3710
3846
|
explain(state, attempted) {
|
|
3847
|
+
if (state.ooc?.question === true && (state.phase === "idle" || state.phase === "between")) return state.ooc.ruling === void 0 ? "this turn is an out-of-character question: answer it first with ooc_answer" : "a question changes nothing: call narrate with no segments";
|
|
3711
3848
|
if (state.ooc !== void 0 && (state.phase === "idle" || state.phase === "between")) return state.ooc.ruling === void 0 ? "this turn is an out-of-character command: rule on it first with ooc_ruling" : state.ooc.ruling.legal ? "make the command happen with resolve_round, expand_world or advance_time, then call narrate with what the player perceives (narration only)" : "the command was refused: call narrate with no segments";
|
|
3712
3849
|
const waiting = (attempt) => (attempt?.reports ?? []).map((report) => report.character).join(", ");
|
|
3713
3850
|
switch (state.phase) {
|
|
3714
|
-
case "idle": return this.judgeDue(state) ? "call judge_action first: does what the player's character tries work? It does not when it is not viable or it breaks a rule of the world or of the player" : this.previewDue(state) ? "call preview_scene first: two or three sentences that show the player how the scene opens, before the characters are asked" : attempted === "activate_characters" || attempted === void 0 ? "call activate_characters to let the characters act" :
|
|
3851
|
+
case "idle": return this.judgeDue(state) ? "call judge_action first: does what the player's character tries work? It does not when it is not viable or it breaks a rule of the world or of the player" : this.previewDue(state) ? "call preview_scene first: two or three sentences that show the player how the scene opens, before the characters are asked" : state.judgement?.allowed === false && attempted !== "activate_characters" ? `the attempt does not work (${state.judgement.reason}): call resolve_round with one outcome, for the player's character only (result failed, a sentence on what it does and why nothing comes of it)${attempted === "narrate" ? ", then narrate" : ""}` : attempted === "activate_characters" || attempted === void 0 ? "call activate_characters to let the characters act" : attempted === "narrate" ? "nothing to deliver yet: call activate_characters to let the characters act, or, when nobody else needs to react, call resolve_round with an outcome for the player's character alone, and then narrate" : "nothing to resolve yet: call activate_characters first";
|
|
3715
3852
|
case "resolving": return attempted === "activate_characters" ? "the characters already acted in this round: call resolve_round" : attempted === "narrate" ? `resolve the round first: call resolve_round with one outcome for each of: ${waiting(state.first)}` : `call resolve_round with one outcome for each of: ${waiting(state.first)}`;
|
|
3716
3853
|
case "retrying": return attempted === "activate_characters" ? "the characters already acted in this round: call resolve_round" : attempted === "narrate" ? `resolve the round first: call resolve_round once more, with one outcome for each of: ${waiting(state.second)}` : `call resolve_round once more, with one outcome for each of: ${waiting(state.second)}`;
|
|
3717
3854
|
case "between": return attempted === "resolve_round" ? "the round is already recorded: call activate_characters again if the story needs it, or narrate" : `call narrate with the output of the turn${this.mustSay(state)}${this.canContinue(state)}`;
|
|
@@ -3783,6 +3920,7 @@ var TurnEngine = class {
|
|
|
3783
3920
|
situation: state.situation,
|
|
3784
3921
|
turn: owner.turn,
|
|
3785
3922
|
policy: store.settings().memory,
|
|
3923
|
+
...store.settings().diagnostics.keepRuns ? { explain: true } : {},
|
|
3786
3924
|
...queryVector === void 0 ? {} : { queryVector }
|
|
3787
3925
|
}, store.story, signal) : state.recalled.get(key) ?? [];
|
|
3788
3926
|
if (number === 1 && memories.length > 0) state.recalled.set(key, memories);
|
|
@@ -3800,6 +3938,7 @@ var TurnEngine = class {
|
|
|
3800
3938
|
cast,
|
|
3801
3939
|
situation: state.situation,
|
|
3802
3940
|
attempt: number,
|
|
3941
|
+
language: store?.settings().prompts.language ?? "en",
|
|
3803
3942
|
turn: owner.turn,
|
|
3804
3943
|
...away ? { away: true } : {},
|
|
3805
3944
|
lookup: store?.settings().memory.lookup ?? false,
|
|
@@ -3837,10 +3976,13 @@ var TurnEngine = class {
|
|
|
3837
3976
|
};
|
|
3838
3977
|
} catch (error) {
|
|
3839
3978
|
if (signal.aborted) throw error;
|
|
3979
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
3980
|
+
this.problem(`${member.character.name} could not act: ${reason}`);
|
|
3840
3981
|
return {
|
|
3841
3982
|
character: key,
|
|
3842
3983
|
name: member.character.name,
|
|
3843
|
-
reason
|
|
3984
|
+
reason,
|
|
3985
|
+
technical: true
|
|
3844
3986
|
};
|
|
3845
3987
|
}
|
|
3846
3988
|
}));
|
|
@@ -3867,7 +4009,7 @@ var TurnEngine = class {
|
|
|
3867
4009
|
if (!present.has(relation.toId) && !relevant.has(relation.toId)) return [];
|
|
3868
4010
|
const other = cast.find((candidate) => candidate.character.id === relation.toId);
|
|
3869
4011
|
return other === void 0 || other.key === member.key ? [] : [{
|
|
3870
|
-
name: other.character
|
|
4012
|
+
name: referent(other.character),
|
|
3871
4013
|
label: relation.label,
|
|
3872
4014
|
summary: relation.summary,
|
|
3873
4015
|
present: present.has(relation.toId),
|