@dsh-agentic-roleplay/core 0.3.1 → 0.3.2
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 +2 -2
- package/lib/client.js +1299 -375
- package/lib/client.js.map +1 -1
- package/lib/compaction.js +1 -1
- package/lib/{deliver-BDW5WZvc.js → deliver-BSsDtF6X.js} +1198 -17
- package/lib/{documents-DtlY2IRt.js → documents-B5XAdfo2.js} +141 -8
- package/lib/{gallery-DACG5eh2.js → gallery-DS5-qH9F.js} +2 -2
- package/lib/{generator-DGYcLMp7.js → generator-BzeepLd2.js} +2 -2
- package/lib/images.js +3 -3
- package/lib/index.js +178 -10
- package/lib/narration-DK2kiIjE.js +141 -0
- package/lib/orchestration.js +684 -1456
- package/lib/{packs-CkRZDCsV.js → packs-BCGK9KZV.js} +3 -3
- package/lib/{presenter-agent-PK9JYExk.js → presenter-agent-Cp-J0onD.js} +3 -3
- package/lib/{narration-5ig7Dg28.js → settings-CA6icLL_.js} +156 -141
- package/package.json +2 -2
- package/prompts/en/roleplay/dice.md +7 -0
- package/prompts/en/roleplay/game-master.md +2 -0
- package/prompts/en/roleplay/reflection.md +3 -1
- package/prompts/zh/roleplay/dice.md +7 -0
- package/prompts/zh/roleplay/game-master.md +2 -0
- package/prompts/zh/roleplay/reflection.md +3 -1
- package/lib/src-CWTNeL6Y.js +0 -128
package/lib/orchestration.js
CHANGED
|
@@ -1,18 +1,41 @@
|
|
|
1
1
|
import { n as chatSessionId, r as roleplaySessionOf, t as promptFor } from "./access-EbNdZFbk.js";
|
|
2
|
-
import { B as shippedText, S as wrongLanguage,
|
|
3
|
-
import { B as
|
|
4
|
-
import { a as
|
|
5
|
-
import "./
|
|
6
|
-
import { A as runChild, D as childSampling, E as childModel, M as tokensOf, T as uniqueKey, j as estimateAnswer, k as childTrace, o as addToLibrary, w as slug } from "./generator-
|
|
7
|
-
import { i as updateImage, r as listGallery, t as describeGalleryImage } from "./gallery-
|
|
8
|
-
import { n as interpretImage, s as presentedFor, t as runImagePresenter } from "./presenter-agent-
|
|
2
|
+
import { B as shippedText, S as wrongLanguage, W as ReflectionRefusedError, Y as INTENT_STATUSES, _ as API_PREFIX } from "./packs-BCGK9KZV.js";
|
|
3
|
+
import { B as sameWords, D as parseOoc, E as MAX_OOC_ANSWER, G as readTime, H as beginTurn, I as Timelines, J as referent, K as skipTime, M as readNotes, N as observeAsPlayer, O as addToNote, P as observeSurroundings, S as plotAuthorNeeded, T as AUTHORING_NEEDS, U as describeWhen, V as ClockError, W as endRound, X as languageOf, Y as RESULTS, Z as translator, _ as PREVIEW_TOOL, b as describePacingAdvice, c as dueReflection, f as whereabouts, g as toolEnabled, h as externalWithheld, i as keepRunDetail, j as putNote, k as clearNotes, l as memoryReflector, p as externalGiven, q as zoneOf, r as forgetRunDetails, s as recordActivity, t as deliverNow, u as recordFailure, v as createTools$1, x as pacingAdvice } from "./deliver-BSsDtF6X.js";
|
|
4
|
+
import { a as WORLD_RULES_KEY, o as rulesOf } from "./settings-CA6icLL_.js";
|
|
5
|
+
import { a as liveSettings } from "./narration-DK2kiIjE.js";
|
|
6
|
+
import { A as runChild, D as childSampling, E as childModel, M as tokensOf, T as uniqueKey, j as estimateAnswer, k as childTrace, o as addToLibrary, w as slug } from "./generator-BzeepLd2.js";
|
|
7
|
+
import { i as updateImage, r as listGallery, t as describeGalleryImage } from "./gallery-DS5-qH9F.js";
|
|
8
|
+
import { n as interpretImage, s as presentedFor, t as runImagePresenter } from "./presenter-agent-Cp-J0onD.js";
|
|
9
9
|
import { join } from "node:path";
|
|
10
10
|
import { existsSync } from "node:fs";
|
|
11
11
|
import { SessionId } from "@deepseek-ai/dsh-session";
|
|
12
12
|
import { createUserMessage } from "@deepseek-ai/dsh-llm";
|
|
13
|
-
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
14
13
|
import { readdir } from "node:fs/promises";
|
|
15
|
-
import { ENGAGEMENT_GRACE,
|
|
14
|
+
import { ENGAGEMENT_GRACE, InvalidEditError, MINUTE, agendaAt, announcements, applyItemPlan, changeItem, changeVariable, characterKey, createDiceTools, createTools, dayPart, describeDuration, describeHolder, describeInstant, describeValue, inAudience, itemKey, judgedStatements, lintWorld, localTime, locationKey, mentioned, nextOccurrence, occurrenceAt, parseInstant, parseRecordsEdit, planItemChange, planVariableChange, rollPlain, sessionDatabase, sessionFolder, shownVariables } from "@dsh-agentic-roleplay/state";
|
|
15
|
+
//#region src/host/tools/resource-hint.ts
|
|
16
|
+
/** The name DSH's MCP resource plugin gives the prompt paragraph it adds for every agent; a section of the same name in an agent's scope replaces it. */
|
|
17
|
+
const RESOURCE_HINT_SECTION = "mcp-resource-servers";
|
|
18
|
+
const RESOURCE_TOOLS = [
|
|
19
|
+
"list_mcp_resources",
|
|
20
|
+
"list_mcp_resource_templates",
|
|
21
|
+
"read_mcp_resource"
|
|
22
|
+
];
|
|
23
|
+
const NAMESPACED = /^mcp__(.+?)__.+$/;
|
|
24
|
+
/**
|
|
25
|
+
* What an agent is told about MCP resources: nothing unless it can see a resource tool, since the plugin's own paragraph names the tools
|
|
26
|
+
* and servers to every agent whether or not it was given them, and an agent that is told of a tool it lacks goes looking for it. The servers are
|
|
27
|
+
* those with a tool registered for anyone, so that an agent given only the resource tools still learns their names.
|
|
28
|
+
*/
|
|
29
|
+
function resourceHint(visible, registered, tr) {
|
|
30
|
+
const tools = RESOURCE_TOOLS.filter((name) => visible.includes(name));
|
|
31
|
+
if (tools.length === 0) return "";
|
|
32
|
+
const servers = [...new Set(registered.flatMap((name) => NAMESPACED.exec(name)?.[1] ?? []))].sort();
|
|
33
|
+
return servers.length === 0 ? tr("## MCP resource servers\n\nUse {tools} with the server argument naming an MCP server.", { tools: tools.join(", ") }) : tr("## MCP resource servers\n\nUse {tools} with one of these names as the server argument: {servers}.", {
|
|
34
|
+
tools: tools.join(", "),
|
|
35
|
+
servers: JSON.stringify(servers)
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
16
39
|
//#region src/host/orchestration/committer.ts
|
|
17
40
|
/** What the keeper reads back by default: the parts of the records its edits write. */
|
|
18
41
|
const VERIFY_SECTIONS = [
|
|
@@ -34,11 +57,13 @@ function describeRound(round, known) {
|
|
|
34
57
|
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}`,
|
|
35
58
|
...round.place === void 0 ? [] : [`Place of the moment: ${round.place.name} (${round.place.key})`],
|
|
36
59
|
...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)}` : ""}`],
|
|
60
|
+
...round.rolls === void 0 ? [] : ["Dice rolled this round, which are facts (not interpreted by anyone):", ...round.rolls.map((line) => `- ${line}`)],
|
|
37
61
|
"What each character did, and what became of it:",
|
|
38
62
|
...round.entries.map((entry) => [
|
|
39
63
|
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}`,
|
|
40
64
|
entry.utterance === void 0 ? "" : ` said${entry.spoken ? "" : " (but was prevented from saying)"}: "${entry.utterance}"`,
|
|
41
65
|
entry.publicState === void 0 ? "" : ` the game master says anyone present can now see: ${entry.publicState}`,
|
|
66
|
+
entry.rolled === void 0 ? "" : ` decided by the dice: ${entry.rolled}`,
|
|
42
67
|
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("; ")}`,
|
|
43
68
|
entry.items === void 0 ? "" : ` with things: ${entry.items.map((thing) => `${thing.name} (${thing.action}: ${thing.summary})`).join("; ")}`,
|
|
44
69
|
entry.at === void 0 ? "" : ` was in ${entry.at.name} (${entry.at.key})${entry.moved === void 0 ? "" : ` and went to ${entry.moved.name} (${entry.moved.key})${entry.moved.position === void 0 ? "" : `, ${entry.moved.position}`}`}`
|
|
@@ -87,7 +112,7 @@ function describePrompt(job, memory) {
|
|
|
87
112
|
...records.open_threads.length === 0 ? ["- none"] : records.open_threads.map((thread) => `- id ${thread.id}: ${thread.text}`)
|
|
88
113
|
].join("\n");
|
|
89
114
|
}
|
|
90
|
-
const RESULT$
|
|
115
|
+
const RESULT$3 = {
|
|
91
116
|
type: "object",
|
|
92
117
|
properties: {
|
|
93
118
|
complete: {
|
|
@@ -205,7 +230,8 @@ function recordKeeper(deps) {
|
|
|
205
230
|
persona: promptFor(parent, "roleplay/record-keeper", job.store.settings().prompts.language),
|
|
206
231
|
prompt: describePrompt(job, memory),
|
|
207
232
|
tools: keeperTools(job, memory, progress),
|
|
208
|
-
|
|
233
|
+
external: externalGiven(parent.ctx.tools, job.store.settings().tools, "records"),
|
|
234
|
+
result: RESULT$3,
|
|
209
235
|
beforeSubmit: () => {
|
|
210
236
|
if (progress.edits === 0 && !warned) {
|
|
211
237
|
warned = true;
|
|
@@ -234,7 +260,7 @@ function suggestionProblems(lines, wanted) {
|
|
|
234
260
|
const tidySuggestions = (lines) => lines.map((line) => line.trim().replace(/\s+/g, " "));
|
|
235
261
|
//#endregion
|
|
236
262
|
//#region src/host/orchestration/suggester.ts
|
|
237
|
-
const RESULT$
|
|
263
|
+
const RESULT$2 = {
|
|
238
264
|
type: "object",
|
|
239
265
|
properties: { options: {
|
|
240
266
|
type: "array",
|
|
@@ -273,7 +299,7 @@ function suggestionWriter(deps) {
|
|
|
273
299
|
trace: childTrace(job.store, "suggestions", "suggestions"),
|
|
274
300
|
persona: promptFor(parent, "roleplay/suggester", job.store.settings().prompts.language, { max: 200 }),
|
|
275
301
|
prompt: promptOf(job),
|
|
276
|
-
result: RESULT$
|
|
302
|
+
result: RESULT$2,
|
|
277
303
|
beforeSubmit: (value) => {
|
|
278
304
|
const problems = suggestionProblems(tidySuggestions(value.options), job.count);
|
|
279
305
|
return problems.length === 0 ? void 0 : `not acceptable: ${problems.join("; ")}`;
|
|
@@ -309,7 +335,7 @@ const verdictReader = (session) => {
|
|
|
309
335
|
};
|
|
310
336
|
//#endregion
|
|
311
337
|
//#region src/host/triggers/judge.ts
|
|
312
|
-
const RESULT$
|
|
338
|
+
const RESULT$1 = {
|
|
313
339
|
type: "object",
|
|
314
340
|
properties: { verdicts: {
|
|
315
341
|
type: "array",
|
|
@@ -376,7 +402,7 @@ function triggerJudge(deps) {
|
|
|
376
402
|
trace: childTrace(store, "records", "trigger judge"),
|
|
377
403
|
persona: promptFor(parent, "roleplay/trigger-judge", store.settings().prompts.language),
|
|
378
404
|
prompt: brief$1(job, statements),
|
|
379
|
-
result: RESULT$
|
|
405
|
+
result: RESULT$1,
|
|
380
406
|
beforeSubmit: (value) => {
|
|
381
407
|
const numbers = value.verdicts.map((verdict) => verdict.number);
|
|
382
408
|
const missing = statements.flatMap((_, index) => numbers.includes(index + 1) ? [] : [index + 1]);
|
|
@@ -448,7 +474,7 @@ function markActed(session, faction, now) {
|
|
|
448
474
|
[keyOf$1(faction)]: now
|
|
449
475
|
});
|
|
450
476
|
}
|
|
451
|
-
const RESULT
|
|
477
|
+
const RESULT = {
|
|
452
478
|
type: "object",
|
|
453
479
|
properties: {
|
|
454
480
|
summary: {
|
|
@@ -591,7 +617,7 @@ function factionStrategist(deps) {
|
|
|
591
617
|
trace: childTrace(store, "authoring", `faction ${faction.name}`),
|
|
592
618
|
persona: promptFor(parent, "roleplay/faction-strategist", store.settings().prompts.language),
|
|
593
619
|
prompt: brief(session, faction, now, zone, last[keyOf$1(faction)] ?? now),
|
|
594
|
-
result: RESULT
|
|
620
|
+
result: RESULT,
|
|
595
621
|
maxCalls: 3
|
|
596
622
|
});
|
|
597
623
|
applyStrategy(job, session, faction, strategy, now);
|
|
@@ -1678,19 +1704,17 @@ function storesOf(state, sessionsDir, problem = () => void 0, queryVector = () =
|
|
|
1678
1704
|
};
|
|
1679
1705
|
}
|
|
1680
1706
|
//#endregion
|
|
1681
|
-
//#region src/shared/
|
|
1682
|
-
/**
|
|
1683
|
-
const
|
|
1684
|
-
/** The
|
|
1685
|
-
function
|
|
1686
|
-
const match =
|
|
1707
|
+
//#region src/shared/roll.ts
|
|
1708
|
+
/** The player rolls dice before acting: `/roll 2d6+1 pick the lock`. The dice are then part of what the game master judges. */
|
|
1709
|
+
const ROLL_COMMAND = /^\s*\/roll(?=\s|$)\s*(\S*)\s*([\s\S]*)$/i;
|
|
1710
|
+
/** The roll in a player's message, or nothing when the message does not start with `/roll`. The notation may be empty, and is then refused. */
|
|
1711
|
+
function parseRoll(text) {
|
|
1712
|
+
const match = ROLL_COMMAND.exec(text);
|
|
1687
1713
|
return match === null ? void 0 : {
|
|
1688
|
-
|
|
1689
|
-
|
|
1714
|
+
expression: match[1] ?? "",
|
|
1715
|
+
reason: (match[2] ?? "").trim()
|
|
1690
1716
|
};
|
|
1691
1717
|
}
|
|
1692
|
-
/** The longest answer to an out-of-character question. */
|
|
1693
|
-
const MAX_OOC_ANSWER = 1200;
|
|
1694
1718
|
//#endregion
|
|
1695
1719
|
//#region src/host/orchestration/hooks.ts
|
|
1696
1720
|
const notice = (text) => createUserMessage({
|
|
@@ -1727,6 +1751,46 @@ function previewToggle(tool) {
|
|
|
1727
1751
|
}
|
|
1728
1752
|
};
|
|
1729
1753
|
}
|
|
1754
|
+
/** Where the withdrawals of an agent's optional tools are kept, by tool name: on the agent, so that a reloaded plugin still finds what its earlier self registered. */
|
|
1755
|
+
const OPTIONAL_SHOWN = Symbol.for("dsh-agentic-roleplay.optional-tools");
|
|
1756
|
+
/** Puts an agent's optional tools in line with the switches: registers the ones that are on and not yet there, withdraws the ones that are off. */
|
|
1757
|
+
function optionalToolSwitch(tools) {
|
|
1758
|
+
return (agent, enabled) => {
|
|
1759
|
+
const holder = agent;
|
|
1760
|
+
const shown = holder[OPTIONAL_SHOWN] ??= /* @__PURE__ */ new Map();
|
|
1761
|
+
for (const tool of tools) {
|
|
1762
|
+
const withdraw = shown.get(tool.name);
|
|
1763
|
+
if (enabled(tool.name) && withdraw === void 0) try {
|
|
1764
|
+
shown.set(tool.name, agent.ctx.tools.register(tool));
|
|
1765
|
+
} catch (error) {
|
|
1766
|
+
if (!(error instanceof Error) || !error.message.includes("already registered")) throw error;
|
|
1767
|
+
shown.set(tool.name, () => void 0);
|
|
1768
|
+
}
|
|
1769
|
+
else if (!enabled(tool.name) && withdraw !== void 0) {
|
|
1770
|
+
shown.delete(tool.name);
|
|
1771
|
+
withdraw();
|
|
1772
|
+
}
|
|
1773
|
+
}
|
|
1774
|
+
};
|
|
1775
|
+
}
|
|
1776
|
+
/** Where an agent's withdrawal of the external tools it was not given is kept: on the agent, so that a reloaded plugin still finds what its earlier self set. */
|
|
1777
|
+
const EXTERNAL_LIMIT = Symbol.for("dsh-agentic-roleplay.external-limit");
|
|
1778
|
+
/**
|
|
1779
|
+
* Takes away from an agent the tools other plugins registered for everyone that it was not given. A story gives the game master such tools (a file or
|
|
1780
|
+
* shell tool, an MCP server's) by switching them on; without it the agent has none of them. The limit follows the settings as they change.
|
|
1781
|
+
*/
|
|
1782
|
+
function externalToolLimit(agent, withheld) {
|
|
1783
|
+
const holder = agent;
|
|
1784
|
+
const key = [...withheld].sort().join("\n");
|
|
1785
|
+
const current = holder[EXTERNAL_LIMIT];
|
|
1786
|
+
if (current?.key === key) return;
|
|
1787
|
+
current?.lift();
|
|
1788
|
+
delete holder[EXTERNAL_LIMIT];
|
|
1789
|
+
if (withheld.length > 0) holder[EXTERNAL_LIMIT] = {
|
|
1790
|
+
key,
|
|
1791
|
+
lift: agent.ctx.tools.restrict({ deny: [...withheld] })
|
|
1792
|
+
};
|
|
1793
|
+
}
|
|
1730
1794
|
/**
|
|
1731
1795
|
* The listeners of a roleplay preset. Preset listeners are shared by every chat
|
|
1732
1796
|
* that uses the preset and also hear the agents that chat spawns, so each one
|
|
@@ -1735,6 +1799,18 @@ function previewToggle(tool) {
|
|
|
1735
1799
|
function installHooks(ctx, deps) {
|
|
1736
1800
|
const { engine, boundaries, stores, timelines, previewTool } = deps;
|
|
1737
1801
|
const showPreviewTool = previewToggle(previewTool);
|
|
1802
|
+
const showOptionalTools = optionalToolSwitch(deps.optionalTools ?? []);
|
|
1803
|
+
/** The tools of the game master as the chat's settings say: its optional ones, and none of other plugins' that it was not given. */
|
|
1804
|
+
const applyTools = (agent, store) => {
|
|
1805
|
+
const { tools } = store.settings();
|
|
1806
|
+
showOptionalTools(agent, (tool) => toolEnabled(tools, "gameMaster", tool));
|
|
1807
|
+
externalToolLimit(agent, externalWithheld(agent.ctx.tools, tools, "gameMaster"));
|
|
1808
|
+
};
|
|
1809
|
+
ctx.on("agent/created", ({ agent }) => {
|
|
1810
|
+
const owner = ownerOf(agent, 0);
|
|
1811
|
+
const store = owner === void 0 ? void 0 : stores(owner.chat);
|
|
1812
|
+
if (store !== void 0) applyTools(agent, store);
|
|
1813
|
+
});
|
|
1738
1814
|
ctx.on("session/event", (session, event) => {
|
|
1739
1815
|
if (event.type !== "turn/end" || session.header.origin === "subagent" || roleplaySessionOf(session.id) === void 0) return;
|
|
1740
1816
|
timelines.ended(session.id, session, event.seq);
|
|
@@ -1760,7 +1836,10 @@ function installHooks(ctx, deps) {
|
|
|
1760
1836
|
const decision = await next();
|
|
1761
1837
|
const owner = ownerOf(payload.agent, payload.turn);
|
|
1762
1838
|
const store = owner === void 0 ? void 0 : stores(owner.chat);
|
|
1763
|
-
if (owner !== void 0 && store !== void 0)
|
|
1839
|
+
if (owner !== void 0 && store !== void 0) {
|
|
1840
|
+
showPreviewTool(payload.agent, store.settings().preview.enabled);
|
|
1841
|
+
applyTools(payload.agent, store);
|
|
1842
|
+
}
|
|
1764
1843
|
if (decision.kind !== "enter" || payload.step !== 1) return decision;
|
|
1765
1844
|
if (owner === void 0 || store === void 0) return decision;
|
|
1766
1845
|
const text = payload.messages.filter((message) => message.source.kind === "user").flatMap((message) => message.content.flatMap((block) => block.type === "text" ? [block.text] : [])).join("\n");
|
|
@@ -1816,6 +1895,30 @@ function installHooks(ctx, deps) {
|
|
|
1816
1895
|
else if (ooc === void 0 && plotAuthorNeeded(store, payload.turn)) lines.push(store.text("roleplay/plot-hint"));
|
|
1817
1896
|
const rules = store.rules();
|
|
1818
1897
|
if (rules.length > 0) lines.push(store.text("roleplay/rules", { rules: rules.map((rule) => `- ${rule}`).join("\n") }));
|
|
1898
|
+
const wanted = ooc === void 0 ? parseRoll(text) : void 0;
|
|
1899
|
+
if (wanted !== void 0) try {
|
|
1900
|
+
const made = rollPlain(wanted.expression);
|
|
1901
|
+
const roll = store.story.dice.record({
|
|
1902
|
+
turn: payload.turn,
|
|
1903
|
+
expression: made.expression,
|
|
1904
|
+
faces: made.faces,
|
|
1905
|
+
kept: made.kept,
|
|
1906
|
+
modifier: made.modifier,
|
|
1907
|
+
total: made.total,
|
|
1908
|
+
value: made.total,
|
|
1909
|
+
reason: wanted.reason === "" ? tr("the player's roll") : wanted.reason,
|
|
1910
|
+
by: "player"
|
|
1911
|
+
});
|
|
1912
|
+
lines.push(tr("The player rolled {dice} ({reason}) and got {total}: dice {faces}. This is a fact, decided by the dice: take it into account when you judge the attempt and decide what happens.", {
|
|
1913
|
+
dice: roll.expression,
|
|
1914
|
+
reason: roll.reason,
|
|
1915
|
+
total: roll.total,
|
|
1916
|
+
faces: `[${roll.faces.join(", ")}]`
|
|
1917
|
+
}));
|
|
1918
|
+
} catch (error) {
|
|
1919
|
+
engine.skip(owner);
|
|
1920
|
+
lines.push(store.text("roleplay/input-refused", { reason: error instanceof Error ? error.message : String(error) }));
|
|
1921
|
+
}
|
|
1819
1922
|
if (ooc !== void 0 && ooc.request !== "") lines.push(store.text(ooc.question ? "roleplay/ask-question" : "roleplay/ooc-command", { request: ooc.request }));
|
|
1820
1923
|
if (ooc !== void 0 && ooc.request === "") {
|
|
1821
1924
|
engine.skip(owner);
|
|
@@ -1836,6 +1939,26 @@ function installHooks(ctx, deps) {
|
|
|
1836
1939
|
const PROTOCOL_SECTION = "roleplay:protocol";
|
|
1837
1940
|
const PERSONA_SECTION = "roleplay:persona";
|
|
1838
1941
|
//#endregion
|
|
1942
|
+
//#region src/host/dice/describe.ts
|
|
1943
|
+
/** The checks of a story as the game master is told of them: when each is used, what it rolls and what it needs. The result's bands are told by the roll itself. */
|
|
1944
|
+
function describeChecks(checks, tr) {
|
|
1945
|
+
if (checks.length === 0) return tr("No checks are defined: use roll_dice only.");
|
|
1946
|
+
const lines = checks.map((check) => {
|
|
1947
|
+
const needs = [
|
|
1948
|
+
tr("Rolls {dice}.", { dice: check.dice }),
|
|
1949
|
+
...check.reading === "successes" ? [tr("Counts the dice that show {hit} or more.", { hit: check.hit ?? 0 })] : [],
|
|
1950
|
+
...check.compare === "over" ? [tr("Give a target: the difficulty, the number to meet or beat; set it by how hard the attempt is.")] : check.compare === "under" ? [tr("Give a target: the number to stay under; set it by how hard the attempt is.")] : [],
|
|
1951
|
+
...check.modifier === void 0 ? [] : [tr("It adds the character's {variable}.", { variable: check.modifier })]
|
|
1952
|
+
];
|
|
1953
|
+
return `${tr("- {key} ({name}): {use}", {
|
|
1954
|
+
key: check.key,
|
|
1955
|
+
name: check.name,
|
|
1956
|
+
use: check.use.replace(/[.\s]+$/, "")
|
|
1957
|
+
})}. ${needs.join(" ")}`;
|
|
1958
|
+
});
|
|
1959
|
+
return [tr("The checks of this story:"), ...lines].join("\n");
|
|
1960
|
+
}
|
|
1961
|
+
//#endregion
|
|
1839
1962
|
//#region src/host/orchestration/current-turn.ts
|
|
1840
1963
|
/** The last turn the session started. */
|
|
1841
1964
|
function currentTurn(agent) {
|
|
@@ -1847,1020 +1970,6 @@ function currentTurn(agent) {
|
|
|
1847
1970
|
return 0;
|
|
1848
1971
|
}
|
|
1849
1972
|
//#endregion
|
|
1850
|
-
//#region src/host/orchestration/character-runner.ts
|
|
1851
|
-
/**
|
|
1852
|
-
* Runs each character as a one-shot child of the game master. It is the character and nothing
|
|
1853
|
-
* else: it knows what its brief gives it, and its only tool (when the chat allows it) reaches
|
|
1854
|
-
* the memory of that character alone, so characters acting at the same time cannot see each
|
|
1855
|
-
* other's memories.
|
|
1856
|
-
*/
|
|
1857
|
-
function characterRunner(deps) {
|
|
1858
|
-
return async (request, signal) => {
|
|
1859
|
-
const brief = deps.boundaries().characterContext.brief(request);
|
|
1860
|
-
const tools = request.lookup ? createCharacterTools(() => ({
|
|
1861
|
-
session: deps.store.story,
|
|
1862
|
-
turn: request.turn,
|
|
1863
|
-
policy: deps.store.settings().memory,
|
|
1864
|
-
queryVector: deps.store.queryVector
|
|
1865
|
-
}), request.member.character.id) : [];
|
|
1866
|
-
const settings = deps.store.settings();
|
|
1867
|
-
const named = request.member.character.tier === "background" && settings.models.background.trim() !== "" ? settings.models.background : settings.models.characters;
|
|
1868
|
-
const result = await runChild({
|
|
1869
|
-
parent: deps.parent,
|
|
1870
|
-
signal,
|
|
1871
|
-
sampling: childSampling(settings, "characters", answerEstimate(settings.prompts.language, request.away === true)),
|
|
1872
|
-
...childModel(settings, "characters", named),
|
|
1873
|
-
trace: childTrace(deps.store, "characters", request.member.character.name, recalledOf(request), named),
|
|
1874
|
-
reasoningEffort: deps.store.settings().reasoning.children,
|
|
1875
|
-
persona: brief.persona,
|
|
1876
|
-
prompt: brief.prompt,
|
|
1877
|
-
tools,
|
|
1878
|
-
maxCalls: deps.store.settings().memory.lookupCalls,
|
|
1879
|
-
result: request.away === true ? AWAY_ACTION_SCHEMA : CHARACTER_ACTION_SCHEMA,
|
|
1880
|
-
unenforced: ["thought"],
|
|
1881
|
-
beforeSubmit: (value) => spokenIn(settings.prompts.language, value)
|
|
1882
|
-
});
|
|
1883
|
-
return parseCharacterAction(result);
|
|
1884
|
-
};
|
|
1885
|
-
}
|
|
1886
|
-
/** A character whose sheet is in another language than the chat's writes in the sheet's: the answer is refused until it is in the chat's. */
|
|
1887
|
-
function spokenIn(language, value) {
|
|
1888
|
-
const given = value;
|
|
1889
|
-
return wrongLanguage(language, {
|
|
1890
|
-
thought: given.thought,
|
|
1891
|
-
intent: given.intent,
|
|
1892
|
-
utterance: given.utterance,
|
|
1893
|
-
account: given.account
|
|
1894
|
-
});
|
|
1895
|
-
}
|
|
1896
|
-
/** What the character was reminded of and why, for the run's record, when the recall said why. */
|
|
1897
|
-
function recalledOf(request) {
|
|
1898
|
-
const explained = (request.memories ?? []).filter((memory) => memory.why !== void 0);
|
|
1899
|
-
return explained.length === 0 ? {} : { recalled: explained.slice(0, 30).map(({ clarity, turn, text, why }) => ({
|
|
1900
|
-
clarity,
|
|
1901
|
-
turn,
|
|
1902
|
-
text,
|
|
1903
|
-
why
|
|
1904
|
-
})) };
|
|
1905
|
-
}
|
|
1906
|
-
const ANSWER_SCHEMA = {
|
|
1907
|
-
type: "object",
|
|
1908
|
-
properties: { answer: {
|
|
1909
|
-
type: "string",
|
|
1910
|
-
description: "The answer to the player, in plain words, in a few sentences."
|
|
1911
|
-
} },
|
|
1912
|
-
required: ["answer"],
|
|
1913
|
-
additionalProperties: false
|
|
1914
|
-
};
|
|
1915
|
-
/**
|
|
1916
|
-
* 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
|
|
1917
|
-
* game master alone knows is in the prompt, so the answer cannot give away what the character could not know.
|
|
1918
|
-
*/
|
|
1919
|
-
function answerRunner(deps) {
|
|
1920
|
-
return async (request, signal) => {
|
|
1921
|
-
const brief = deps.boundaries().characterContext.brief(request);
|
|
1922
|
-
return (await runChild({
|
|
1923
|
-
parent: deps.parent,
|
|
1924
|
-
signal,
|
|
1925
|
-
sampling: childSampling(deps.store.settings(), "records", estimateAnswer([MAX_OOC_ANSWER], deps.store.settings().prompts.language)),
|
|
1926
|
-
...childModel(deps.store.settings(), "records"),
|
|
1927
|
-
trace: childTrace(deps.store, "records", "answer to the player"),
|
|
1928
|
-
reasoningEffort: deps.store.settings().reasoning.children,
|
|
1929
|
-
persona: request.text("roleplay/ask-answerer", { name: request.member.character.name }),
|
|
1930
|
-
prompt: brief.prompt,
|
|
1931
|
-
tools: [],
|
|
1932
|
-
maxCalls: 3,
|
|
1933
|
-
result: ANSWER_SCHEMA
|
|
1934
|
-
})).answer;
|
|
1935
|
-
};
|
|
1936
|
-
}
|
|
1937
|
-
//#endregion
|
|
1938
|
-
//#region src/host/orchestration/boundaries.ts
|
|
1939
|
-
/** Why the game master may ask for something to be made up: the story genuinely needs it. */
|
|
1940
|
-
const AUTHORING_NEEDS = [
|
|
1941
|
-
"scene_change",
|
|
1942
|
-
"arrival",
|
|
1943
|
-
"introduction",
|
|
1944
|
-
"object",
|
|
1945
|
-
"variable",
|
|
1946
|
-
"trigger"
|
|
1947
|
-
];
|
|
1948
|
-
//#endregion
|
|
1949
|
-
//#region src/host/pacing/author.ts
|
|
1950
|
-
const RESULT = {
|
|
1951
|
-
type: "object",
|
|
1952
|
-
required: [
|
|
1953
|
-
"id",
|
|
1954
|
-
"name",
|
|
1955
|
-
"premise",
|
|
1956
|
-
"themes",
|
|
1957
|
-
"hook",
|
|
1958
|
-
"developments",
|
|
1959
|
-
"stakes",
|
|
1960
|
-
"resolutions",
|
|
1961
|
-
"participants"
|
|
1962
|
-
],
|
|
1963
|
-
additionalProperties: false,
|
|
1964
|
-
properties: {
|
|
1965
|
-
id: { type: "string" },
|
|
1966
|
-
name: { type: "string" },
|
|
1967
|
-
premise: { type: "string" },
|
|
1968
|
-
themes: {
|
|
1969
|
-
type: "array",
|
|
1970
|
-
items: { type: "string" }
|
|
1971
|
-
},
|
|
1972
|
-
hook: {
|
|
1973
|
-
type: "object",
|
|
1974
|
-
required: [
|
|
1975
|
-
"id",
|
|
1976
|
-
"text",
|
|
1977
|
-
"guidance",
|
|
1978
|
-
"avoid"
|
|
1979
|
-
],
|
|
1980
|
-
additionalProperties: false,
|
|
1981
|
-
properties: {
|
|
1982
|
-
id: { type: "string" },
|
|
1983
|
-
text: { type: "string" },
|
|
1984
|
-
guidance: { type: "string" },
|
|
1985
|
-
avoid: {
|
|
1986
|
-
type: "array",
|
|
1987
|
-
items: { type: "string" }
|
|
1988
|
-
}
|
|
1989
|
-
}
|
|
1990
|
-
},
|
|
1991
|
-
developments: {
|
|
1992
|
-
type: "array",
|
|
1993
|
-
items: {
|
|
1994
|
-
type: "object",
|
|
1995
|
-
required: [
|
|
1996
|
-
"id",
|
|
1997
|
-
"text",
|
|
1998
|
-
"mode",
|
|
1999
|
-
"prerequisites",
|
|
2000
|
-
"guidance",
|
|
2001
|
-
"avoid"
|
|
2002
|
-
],
|
|
2003
|
-
additionalProperties: false,
|
|
2004
|
-
properties: {
|
|
2005
|
-
id: { type: "string" },
|
|
2006
|
-
text: { type: "string" },
|
|
2007
|
-
mode: {
|
|
2008
|
-
type: "string",
|
|
2009
|
-
enum: [
|
|
2010
|
-
"opportunity",
|
|
2011
|
-
"reminder",
|
|
2012
|
-
"character",
|
|
2013
|
-
"consequence",
|
|
2014
|
-
"escalation",
|
|
2015
|
-
"breathing_room"
|
|
2016
|
-
]
|
|
2017
|
-
},
|
|
2018
|
-
prerequisites: {
|
|
2019
|
-
type: "array",
|
|
2020
|
-
items: { type: "string" }
|
|
2021
|
-
},
|
|
2022
|
-
guidance: { type: "string" },
|
|
2023
|
-
avoid: {
|
|
2024
|
-
type: "array",
|
|
2025
|
-
items: { type: "string" }
|
|
2026
|
-
}
|
|
2027
|
-
}
|
|
2028
|
-
}
|
|
2029
|
-
},
|
|
2030
|
-
stakes: {
|
|
2031
|
-
type: "array",
|
|
2032
|
-
items: { type: "string" }
|
|
2033
|
-
},
|
|
2034
|
-
resolutions: {
|
|
2035
|
-
type: "array",
|
|
2036
|
-
items: { type: "string" }
|
|
2037
|
-
},
|
|
2038
|
-
participants: {
|
|
2039
|
-
type: "array",
|
|
2040
|
-
items: { type: "string" }
|
|
2041
|
-
}
|
|
2042
|
-
}
|
|
2043
|
-
};
|
|
2044
|
-
/** A scaffold is about two dozen short texts: premise, hook, developments with their guidance, stakes and resolutions. */
|
|
2045
|
-
const PLOT_TEXTS = 24;
|
|
2046
|
-
const PLOT_TEXT_CHARS = 300;
|
|
2047
|
-
async function authorPlot(parent, store, turn, signal) {
|
|
2048
|
-
const cast = store.cast().map((member) => `${member.character.name} (${member.key}): ${member.character.summary}`).join("\n");
|
|
2049
|
-
const factions = store.story.factions.list().map((faction) => `${faction.name} (${faction.sourceId ?? faction.id}): ${faction.summary}`).join("\n");
|
|
2050
|
-
const places = store.story.locations.list().map((place) => `${place.name} (${place.sourceId ?? place.id})`).join(", ");
|
|
2051
|
-
const events = store.story.events.list().slice(-12).map((event) => `- ${event.text}`).join("\n");
|
|
2052
|
-
let checked;
|
|
2053
|
-
const result = await runChild({
|
|
2054
|
-
parent,
|
|
2055
|
-
signal,
|
|
2056
|
-
sampling: childSampling(store.settings(), "authoring", estimateAnswer(Array.from({ length: PLOT_TEXTS }, () => PLOT_TEXT_CHARS), store.settings().prompts.language)),
|
|
2057
|
-
...childModel(store.settings(), "authoring"),
|
|
2058
|
-
trace: childTrace(store, "authoring", "plot author"),
|
|
2059
|
-
persona: promptFor(parent, "roleplay/plot-author", store.settings().prompts.language),
|
|
2060
|
-
reasoningEffort: store.settings().reasoning.children,
|
|
2061
|
-
result: RESULT,
|
|
2062
|
-
maxCalls: 3,
|
|
2063
|
-
prompt: `The story has gone quiet by turn ${turn}. Propose one optional plot scaffold that grows from this state.\n\nCharacters:\n${cast || "- none"}\n\nFactions:\n${factions || "- none"}\n\nPlaces: ${places || "none"}\n\nRecent recorded events:\n${events || "- none"}`,
|
|
2064
|
-
beforeSubmit(value) {
|
|
2065
|
-
const authored = value;
|
|
2066
|
-
const candidate = {
|
|
2067
|
-
...authored,
|
|
2068
|
-
hooks: [{
|
|
2069
|
-
...authored.hook,
|
|
2070
|
-
mode: "opportunity",
|
|
2071
|
-
prerequisites: []
|
|
2072
|
-
}],
|
|
2073
|
-
start: "available",
|
|
2074
|
-
pacing: {
|
|
2075
|
-
quietTurns: 3,
|
|
2076
|
-
cooldownTurns: 4,
|
|
2077
|
-
cooldownDays: 1
|
|
2078
|
-
}
|
|
2079
|
-
};
|
|
2080
|
-
const parsed = parsePlotsFile(JSON.stringify([candidate]));
|
|
2081
|
-
if (!parsed.ok) return parsed.issues.map((issue) => `${issue.path}: ${issue.message}`).join("\n");
|
|
2082
|
-
checked = parsed.value[0];
|
|
2083
|
-
}
|
|
2084
|
-
});
|
|
2085
|
-
if (checked === void 0) throw new Error(`the plot author returned an invalid plot: ${result.name}`);
|
|
2086
|
-
addSessionPlot(store, checked);
|
|
2087
|
-
return checked;
|
|
2088
|
-
}
|
|
2089
|
-
//#endregion
|
|
2090
|
-
//#region src/host/orchestration/tools.ts
|
|
2091
|
-
const ACTIVATE_TOOL = "activate_characters";
|
|
2092
|
-
const RESOLVE_TOOL = "resolve_round";
|
|
2093
|
-
const NARRATE_TOOL = "narrate";
|
|
2094
|
-
const RECORD_TOOL = "record_memory";
|
|
2095
|
-
const EXPAND_TOOL = "expand_world";
|
|
2096
|
-
const INTRODUCE_TOOL = "introduce_thing";
|
|
2097
|
-
const TIME_TOOL = "advance_time";
|
|
2098
|
-
const PREVIEW_TOOL = "preview_scene";
|
|
2099
|
-
const RULING_TOOL = "ooc_ruling";
|
|
2100
|
-
const ANSWER_TOOL = "ooc_answer";
|
|
2101
|
-
const JUDGE_TOOL = "judge_action";
|
|
2102
|
-
const PLOT_TOOL = "record_plot_progress";
|
|
2103
|
-
const AUTHOR_PLOT_TOOL = "author_plot";
|
|
2104
|
-
/** The JSON value the tool records; the model reads the rendered text. */
|
|
2105
|
-
function record(value) {
|
|
2106
|
-
return JSON.parse(JSON.stringify(value));
|
|
2107
|
-
}
|
|
2108
|
-
function failures(list) {
|
|
2109
|
-
return list.length === 0 ? [] : [
|
|
2110
|
-
"",
|
|
2111
|
-
"Could not act:",
|
|
2112
|
-
...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}`)
|
|
2113
|
-
];
|
|
2114
|
-
}
|
|
2115
|
-
function renderAttempt(attempt) {
|
|
2116
|
-
const heading = attempt.attempt === 1 ? "The characters acted. Their intents may conflict with each other; you decide what actually happens." : "These characters failed but were highly committed, so each tried once more, knowing what just happened.";
|
|
2117
|
-
const lines = attempt.reports.map((report) => `- ${report.character} (${report.name}): ${report.utterance === void 0 ? "says nothing" : `says "${report.utterance}"`}; does: ${report.intent} [commitment: ${report.commitment}]`);
|
|
2118
|
-
const next = attempt.reports.length === 0 ? ["", "Nobody could act. Fix the problem or call activate_characters again with a different set."] : attempt.attempt === 1 ? ["", `Now call ${RESOLVE_TOOL} with one outcome for each of: ${attempt.reports.map((report) => report.character).join(", ")}.`] : ["", `Call ${RESOLVE_TOOL} once more with one outcome for each of: ${attempt.reports.map((report) => report.character).join(", ")}. There are no further retries: what you decide now is final.`];
|
|
2119
|
-
return [
|
|
2120
|
-
heading,
|
|
2121
|
-
...lines,
|
|
2122
|
-
...failures(attempt.failures),
|
|
2123
|
-
...next
|
|
2124
|
-
].join("\n");
|
|
2125
|
-
}
|
|
2126
|
-
/** Who went where because their day says so. */
|
|
2127
|
-
const routineLine = (move) => `By their routine, ${move.name} is now at ${move.to} (${move.activity}).`;
|
|
2128
|
-
const OUTPUT_RULES = "Deliver the output with the narrate tool as ordered segments: utterances and narration. Narration may come before, between and after utterances and is yours to write. Utterances are the characters' words: include every one exactly as given, once, in the order they were spoken, and never change, add or remove one.";
|
|
2129
|
-
const listed = (utterances) => utterances.map((utterance) => `- ${utterance.character} (${utterance.name}), round ${utterance.round}: "${utterance.text}"`).join("\n");
|
|
2130
|
-
/** What the world did by itself, as the game master is told: what happened, what characters notice, and what it is to weave in. */
|
|
2131
|
-
function renderWorld(world) {
|
|
2132
|
-
return world.flatMap((entry) => [
|
|
2133
|
-
...entry.events.map((text) => `The world acted (${entry.name}): ${text}`),
|
|
2134
|
-
...entry.cues.map((cue) => `${cue.names.join(" and ")} will notice: ${cue.text}`),
|
|
2135
|
-
...entry.beats.map((beat) => `Weave this into the story, then say you did with addressed: [${beat.id}] in narrate (${beat.text})`)
|
|
2136
|
-
]);
|
|
2137
|
-
}
|
|
2138
|
-
function renderResolution(report) {
|
|
2139
|
-
if (report.next !== void 0) return renderAttempt(report.next);
|
|
2140
|
-
const done = report.done;
|
|
2141
|
-
if (done === void 0) return "";
|
|
2142
|
-
const left = done.limit - done.round;
|
|
2143
|
-
const passed = done.time.to - done.time.from;
|
|
2144
|
-
const lines = [
|
|
2145
|
-
`Round ${done.round} is recorded (${done.updated.join(", ")}).`,
|
|
2146
|
-
`It is now ${describeTime({
|
|
2147
|
-
now: done.time.to,
|
|
2148
|
-
zone: done.time.zone
|
|
2149
|
-
})}${passed > 0 ? `; ${describeDuration(passed)} passed` : ""}.`,
|
|
2150
|
-
...done.moved.map(routineLine),
|
|
2151
|
-
...done.moves.map((move) => `${move.name} is now at ${move.to}${move.via > 0 ? ` (by way of ${move.via} other place${move.via === 1 ? "" : "s"})` : ""}.`),
|
|
2152
|
-
...done.things.map((thing) => `${thing.name}: ${thing.summary}.`),
|
|
2153
|
-
...done.variables.map((line) => `${line}.`),
|
|
2154
|
-
...renderWorld(done.world),
|
|
2155
|
-
...failures(done.retryFailures)
|
|
2156
|
-
];
|
|
2157
|
-
lines.push(done.spoken.length === 0 ? "Nobody has said anything yet this turn." : `Everything said so far this turn, in order; the output must carry all of it exactly:\n${listed(done.spoken)}`);
|
|
2158
|
-
lines.push(left > 0 ? `Now decide. If the story genuinely flows from what just happened (someone is directly answered, a reaction is natural, something is left unresolved that the characters would act on), call activate_characters for the next round: ${left} round(s) are left for this message. Otherwise give control back to the player now.` : "That was the last round allowed for this message: give control back to the player now.");
|
|
2159
|
-
lines.push(done.waiting === 0 ? "Nothing is waiting to be recorded." : `${done.waiting} round${done.waiting === 1 ? " is" : "s are"} waiting to be recorded in the story's memory. Memory is not written after every round: call ${RECORD_TOOL} when the story just moved on (a decision made, a secret or fact revealed, a promise, an injury, a relationship changed, the scene or time moved); skip it for small talk and routine movement, which are kept and recorded together with the next thing that matters.`);
|
|
2160
|
-
lines.push(OUTPUT_RULES);
|
|
2161
|
-
lines.push("Narrate only what the player's character can perceive.");
|
|
2162
|
-
return lines.join("\n");
|
|
2163
|
-
}
|
|
2164
|
-
function renderExpansion(report) {
|
|
2165
|
-
if (report.created.length === 0) return `Nothing was made up: ${(report.declined ?? "the world author found nothing the story needs").replace(/[.\s]+$/, "")}. Work with what exists.`;
|
|
2166
|
-
const lines = report.created.map((entry) => `- ${entry.kind} ${entry.key} (${entry.name})`);
|
|
2167
|
-
const characters = report.created.filter((entry) => entry.kind === "character");
|
|
2168
|
-
return [
|
|
2169
|
-
"Made up and fitted into the world:",
|
|
2170
|
-
...lines,
|
|
2171
|
-
...characters.length === 0 ? [] : [`${characters.length === 1 ? "They are" : "They are all"} now in the cast: ask them to act with ${ACTIVATE_TOOL}, by their keys, like anyone else.`],
|
|
2172
|
-
...report.declined === void 0 ? [] : [`Not made: ${report.declined}`]
|
|
2173
|
-
].join("\n");
|
|
2174
|
-
}
|
|
2175
|
-
/** The tools the game master plays a turn with. */
|
|
2176
|
-
function createTools$1(deps) {
|
|
2177
|
-
const ownerOf = (agent) => {
|
|
2178
|
-
if (agent === void 0) throw new Error("the roleplay tools can only be called by an agent");
|
|
2179
|
-
return {
|
|
2180
|
-
agent,
|
|
2181
|
-
owner: {
|
|
2182
|
-
chat: agent.session.id,
|
|
2183
|
-
turn: deps.turnOf(agent)
|
|
2184
|
-
}
|
|
2185
|
-
};
|
|
2186
|
-
};
|
|
2187
|
-
const storeOf = (chat) => {
|
|
2188
|
-
const store = deps.stores(chat);
|
|
2189
|
-
if (store === void 0) throw new Error("this chat is not a roleplay session");
|
|
2190
|
-
return store;
|
|
2191
|
-
};
|
|
2192
|
-
const activate = defineTool({
|
|
2193
|
-
name: ACTIVATE_TOOL,
|
|
2194
|
-
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.",
|
|
2195
|
-
parameters: {
|
|
2196
|
-
characters: {
|
|
2197
|
-
type: "array",
|
|
2198
|
-
required: true,
|
|
2199
|
-
items: { type: "string" },
|
|
2200
|
-
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."
|
|
2201
|
-
},
|
|
2202
|
-
situation: {
|
|
2203
|
-
type: "string",
|
|
2204
|
-
required: true,
|
|
2205
|
-
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."
|
|
2206
|
-
}
|
|
2207
|
-
},
|
|
2208
|
-
output: {
|
|
2209
|
-
schema: { type: "json" },
|
|
2210
|
-
render: (_args, value) => [{
|
|
2211
|
-
type: "text",
|
|
2212
|
-
text: renderAttempt(value)
|
|
2213
|
-
}]
|
|
2214
|
-
},
|
|
2215
|
-
isConcurrencySafe: () => false,
|
|
2216
|
-
async execute(args, exec) {
|
|
2217
|
-
const { agent, owner } = ownerOf(exec.agent);
|
|
2218
|
-
await deps.engine.settle(owner.chat, owner.turn);
|
|
2219
|
-
const run = characterRunner({
|
|
2220
|
-
parent: agent,
|
|
2221
|
-
boundaries: deps.boundaries,
|
|
2222
|
-
store: storeOf(owner.chat)
|
|
2223
|
-
});
|
|
2224
|
-
return record(await deps.engine.activate(owner, {
|
|
2225
|
-
situation: args.situation,
|
|
2226
|
-
characters: args.characters
|
|
2227
|
-
}, run, exec.signal));
|
|
2228
|
-
}
|
|
2229
|
-
});
|
|
2230
|
-
const resolve = defineTool({
|
|
2231
|
-
name: RESOLVE_TOOL,
|
|
2232
|
-
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.",
|
|
2233
|
-
parameters: {
|
|
2234
|
-
outcomes: {
|
|
2235
|
-
type: "array",
|
|
2236
|
-
required: true,
|
|
2237
|
-
items: {
|
|
2238
|
-
type: "object",
|
|
2239
|
-
additionalProperties: false,
|
|
2240
|
-
properties: {
|
|
2241
|
-
character: {
|
|
2242
|
-
type: "string",
|
|
2243
|
-
required: true,
|
|
2244
|
-
description: "The character's key; for the player's character, only when it moves or handles things; in an out-of-character command, any character of the story."
|
|
2245
|
-
},
|
|
2246
|
-
result: {
|
|
2247
|
-
type: "string",
|
|
2248
|
-
required: true,
|
|
2249
|
-
enum: RESULTS,
|
|
2250
|
-
description: "Whether the character's intent went through."
|
|
2251
|
-
},
|
|
2252
|
-
summary: {
|
|
2253
|
-
type: "string",
|
|
2254
|
-
required: true,
|
|
2255
|
-
description: "What concretely happens to the character, in a sentence. It becomes what the character is doing."
|
|
2256
|
-
},
|
|
2257
|
-
spoken: {
|
|
2258
|
-
type: "boolean",
|
|
2259
|
-
description: "Whether the character gets to say its utterance. It does unless something prevents it, such as being silenced or knocked out first."
|
|
2260
|
-
},
|
|
2261
|
-
public_state: {
|
|
2262
|
-
type: "string",
|
|
2263
|
-
description: "What anyone present can now see or infer about the character, when that changed."
|
|
2264
|
-
},
|
|
2265
|
-
item_changes: {
|
|
2266
|
-
type: "array",
|
|
2267
|
-
description: "What the character does with things as a result, in order, when it does: leave out when it handles nothing. Each is checked, and you are told why when it cannot be done. Not for a character whose intent failed.",
|
|
2268
|
-
items: {
|
|
2269
|
-
type: "object",
|
|
2270
|
-
additionalProperties: false,
|
|
2271
|
-
properties: {
|
|
2272
|
-
item: {
|
|
2273
|
-
type: "string",
|
|
2274
|
-
required: true,
|
|
2275
|
-
description: "The key of the item, as the scene lists it. Leave a way's lock to exit."
|
|
2276
|
-
},
|
|
2277
|
-
action: {
|
|
2278
|
-
type: "string",
|
|
2279
|
-
required: true,
|
|
2280
|
-
enum: ITEM_ACTIONS,
|
|
2281
|
-
description: "take (pick up what is here or take it out of a container it carries), drop (leave it here), give (to someone present), put (into an open container), wear, remove (what is worn), open, close, lock, unlock, reveal (find what was hidden)."
|
|
2282
|
-
},
|
|
2283
|
-
to: {
|
|
2284
|
-
type: "string",
|
|
2285
|
-
description: "For give: who gets it. For put: the container."
|
|
2286
|
-
},
|
|
2287
|
-
quantity: {
|
|
2288
|
-
type: "integer",
|
|
2289
|
-
description: "How many, of a pile; all when left out."
|
|
2290
|
-
},
|
|
2291
|
-
position: {
|
|
2292
|
-
type: "string",
|
|
2293
|
-
description: "Where, in words, for drop and put."
|
|
2294
|
-
},
|
|
2295
|
-
feature: {
|
|
2296
|
-
type: "string",
|
|
2297
|
-
description: "For drop: the feature of the place it is left at."
|
|
2298
|
-
},
|
|
2299
|
-
with: {
|
|
2300
|
-
type: "string",
|
|
2301
|
-
description: "For lock and unlock: the key item used. It has to be in hand and fit the lock."
|
|
2302
|
-
},
|
|
2303
|
-
exit: {
|
|
2304
|
-
type: "string",
|
|
2305
|
-
description: "For lock and unlock of a way instead of an item: the key of the place it leads to."
|
|
2306
|
-
},
|
|
2307
|
-
forced: {
|
|
2308
|
-
type: "boolean",
|
|
2309
|
-
description: "Only when the story breaks the lock (picked, smashed): no key is needed."
|
|
2310
|
-
}
|
|
2311
|
-
}
|
|
2312
|
-
}
|
|
2313
|
-
},
|
|
2314
|
-
variable_changes: {
|
|
2315
|
-
type: "array",
|
|
2316
|
-
description: "What this changes of what the story keeps track of (the scene lists it; list_variables shows what exists): a feeling, an injury, a flag, a debt. Only what the outcome establishes; to is the new value (a number, true or false, or one of the words of a choice), by is an amount a number goes up or down (it stops at its bounds).",
|
|
2317
|
-
items: {
|
|
2318
|
-
type: "object",
|
|
2319
|
-
additionalProperties: false,
|
|
2320
|
-
properties: {
|
|
2321
|
-
variable: {
|
|
2322
|
-
type: "string",
|
|
2323
|
-
required: true,
|
|
2324
|
-
description: "The key of the variable."
|
|
2325
|
-
},
|
|
2326
|
-
subject: {
|
|
2327
|
-
type: "string",
|
|
2328
|
-
required: true,
|
|
2329
|
-
description: "The key of the character, item or place it is about; world for the whole story."
|
|
2330
|
-
},
|
|
2331
|
-
to: {
|
|
2332
|
-
oneOf: [
|
|
2333
|
-
{ type: "number" },
|
|
2334
|
-
{ type: "boolean" },
|
|
2335
|
-
{ type: "string" }
|
|
2336
|
-
],
|
|
2337
|
-
description: "The new value."
|
|
2338
|
-
},
|
|
2339
|
-
by: {
|
|
2340
|
-
type: "number",
|
|
2341
|
-
description: "How much a number goes up (positive) or down (negative)."
|
|
2342
|
-
},
|
|
2343
|
-
baseline: {
|
|
2344
|
-
oneOf: [
|
|
2345
|
-
{ type: "number" },
|
|
2346
|
-
{ type: "boolean" },
|
|
2347
|
-
{ type: "string" }
|
|
2348
|
-
],
|
|
2349
|
-
description: "For a variable with a temperament (feelings): the new baseline, when what happened changes it lastingly."
|
|
2350
|
-
}
|
|
2351
|
-
}
|
|
2352
|
-
}
|
|
2353
|
-
},
|
|
2354
|
-
moves_to: {
|
|
2355
|
-
type: "object",
|
|
2356
|
-
additionalProperties: false,
|
|
2357
|
-
description: "Where the character ends up if it goes somewhere as a result: leave out when it stays. It must be possible (a way over open exits); you are told why if it is not. Not for a character whose intent failed. A journey takes at least as long as the ways on it say, whatever elapsed_seconds you give.",
|
|
2358
|
-
properties: {
|
|
2359
|
-
location: {
|
|
2360
|
-
type: "string",
|
|
2361
|
-
required: true,
|
|
2362
|
-
description: "The key of the place, as the scene lists it."
|
|
2363
|
-
},
|
|
2364
|
-
position: {
|
|
2365
|
-
type: "string",
|
|
2366
|
-
description: "Where in the place, in words: \"by the window\"."
|
|
2367
|
-
},
|
|
2368
|
-
feature: {
|
|
2369
|
-
type: "string",
|
|
2370
|
-
description: "The key of a feature of that place it is at."
|
|
2371
|
-
},
|
|
2372
|
-
ignore_exits: {
|
|
2373
|
-
type: "boolean",
|
|
2374
|
-
description: "Only when the story itself carries it (carried off, teleported, falls): the exits do not matter."
|
|
2375
|
-
}
|
|
2376
|
-
}
|
|
2377
|
-
}
|
|
2378
|
-
}
|
|
2379
|
-
}
|
|
2380
|
-
},
|
|
2381
|
-
elapsed_seconds: {
|
|
2382
|
-
type: "integer",
|
|
2383
|
-
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."
|
|
2384
|
-
}
|
|
2385
|
-
},
|
|
2386
|
-
output: {
|
|
2387
|
-
schema: { type: "json" },
|
|
2388
|
-
render: (_args, value) => [{
|
|
2389
|
-
type: "text",
|
|
2390
|
-
text: renderResolution(value)
|
|
2391
|
-
}]
|
|
2392
|
-
},
|
|
2393
|
-
isConcurrencySafe: () => false,
|
|
2394
|
-
async execute(args, exec) {
|
|
2395
|
-
const { agent, owner } = ownerOf(exec.agent);
|
|
2396
|
-
await deps.engine.settle(owner.chat, owner.turn);
|
|
2397
|
-
const run = characterRunner({
|
|
2398
|
-
parent: agent,
|
|
2399
|
-
boundaries: deps.boundaries,
|
|
2400
|
-
store: storeOf(owner.chat)
|
|
2401
|
-
});
|
|
2402
|
-
const outcomes = args.outcomes.map((outcome) => ({
|
|
2403
|
-
character: outcome.character,
|
|
2404
|
-
result: outcome.result,
|
|
2405
|
-
summary: outcome.summary,
|
|
2406
|
-
...outcome.public_state === void 0 ? {} : { publicState: outcome.public_state },
|
|
2407
|
-
...outcome.spoken === void 0 ? {} : { spoken: outcome.spoken },
|
|
2408
|
-
...outcome.item_changes === void 0 || outcome.item_changes.length === 0 ? {} : { itemChanges: outcome.item_changes.map((change) => ({
|
|
2409
|
-
item: change.item,
|
|
2410
|
-
action: change.action,
|
|
2411
|
-
...change.to === void 0 ? {} : { to: change.to },
|
|
2412
|
-
...change.quantity === void 0 ? {} : { quantity: change.quantity },
|
|
2413
|
-
...change.position === void 0 ? {} : { position: change.position },
|
|
2414
|
-
...change.feature === void 0 ? {} : { feature: change.feature },
|
|
2415
|
-
...change.with === void 0 ? {} : { with: change.with },
|
|
2416
|
-
...change.exit === void 0 ? {} : { exit: change.exit },
|
|
2417
|
-
...change.forced === true ? { forced: true } : {}
|
|
2418
|
-
})) },
|
|
2419
|
-
...outcome.variable_changes === void 0 || outcome.variable_changes.length === 0 ? {} : { variableChanges: outcome.variable_changes.map((change) => ({
|
|
2420
|
-
variable: change.variable,
|
|
2421
|
-
subject: change.subject,
|
|
2422
|
-
...change.to === void 0 ? {} : { to: change.to },
|
|
2423
|
-
...change.by === void 0 ? {} : { by: change.by },
|
|
2424
|
-
...change.baseline === void 0 ? {} : { baseline: change.baseline }
|
|
2425
|
-
})) },
|
|
2426
|
-
...outcome.moves_to === void 0 ? {} : { moveTo: {
|
|
2427
|
-
location: outcome.moves_to.location,
|
|
2428
|
-
...outcome.moves_to.position === void 0 ? {} : { position: outcome.moves_to.position },
|
|
2429
|
-
...outcome.moves_to.feature === void 0 ? {} : { feature: outcome.moves_to.feature },
|
|
2430
|
-
...outcome.moves_to.ignore_exits === true ? { ignoreExits: true } : {}
|
|
2431
|
-
} }
|
|
2432
|
-
}));
|
|
2433
|
-
return record(await deps.engine.resolve(owner, outcomes, run, exec.signal, args.elapsed_seconds));
|
|
2434
|
-
}
|
|
2435
|
-
});
|
|
2436
|
-
const narrate = defineTool({
|
|
2437
|
-
name: NARRATE_TOOL,
|
|
2438
|
-
description: "Deliver the output of the turn to the player and give control back to them, once at least one round is recorded: the characters' utterances from all the rounds and your narration, as ordered segments. Every utterance a character gave must appear exactly once, exactly as given and in the order spoken; narration is free. An output that changes, drops, invents or reorders an utterance is refused with what is wrong.",
|
|
2439
|
-
parameters: {
|
|
2440
|
-
segments: {
|
|
2441
|
-
type: "array",
|
|
2442
|
-
description: "The output in order. Leave out only when a narrator other than you writes the output.",
|
|
2443
|
-
items: {
|
|
2444
|
-
type: "object",
|
|
2445
|
-
additionalProperties: false,
|
|
2446
|
-
properties: {
|
|
2447
|
-
type: {
|
|
2448
|
-
type: "string",
|
|
2449
|
-
required: true,
|
|
2450
|
-
enum: ["utterance", "narration"]
|
|
2451
|
-
},
|
|
2452
|
-
character: {
|
|
2453
|
-
type: "string",
|
|
2454
|
-
description: "For an utterance: the key of the character who says it."
|
|
2455
|
-
},
|
|
2456
|
-
text: {
|
|
2457
|
-
type: "string",
|
|
2458
|
-
required: true,
|
|
2459
|
-
description: "For an utterance: the words exactly as the character gave them, without quotation marks. For narration: what the player reads."
|
|
2460
|
-
}
|
|
2461
|
-
}
|
|
2462
|
-
}
|
|
2463
|
-
},
|
|
2464
|
-
addressed: {
|
|
2465
|
-
type: "array",
|
|
2466
|
-
items: { type: "integer" },
|
|
2467
|
-
description: "The ids of the beats you were told to weave in that this output does. Beats you do not address are told again, then let go."
|
|
2468
|
-
}
|
|
2469
|
-
},
|
|
2470
|
-
output: {
|
|
2471
|
-
schema: { type: "json" },
|
|
2472
|
-
render: (_args, value) => {
|
|
2473
|
-
const output = value;
|
|
2474
|
-
return [{
|
|
2475
|
-
type: "text",
|
|
2476
|
-
text: `${output.repaired ? "The output was corrected so that every utterance appears exactly as the characters gave it, and has been delivered to the player. The turn ends here." : "The output has been delivered to the player. The turn ends here."}\n${encodeOutput(output)}`
|
|
2477
|
-
}];
|
|
2478
|
-
}
|
|
2479
|
-
},
|
|
2480
|
-
isConcurrencySafe: () => false,
|
|
2481
|
-
async execute(args, exec) {
|
|
2482
|
-
const { agent, owner } = ownerOf(exec.agent);
|
|
2483
|
-
await deps.engine.settle(owner.chat, owner.turn);
|
|
2484
|
-
const segments = (args.segments ?? []).map((segment) => ({
|
|
2485
|
-
type: segment.type,
|
|
2486
|
-
text: segment.text,
|
|
2487
|
-
...segment.character === void 0 ? {} : { character: segment.character }
|
|
2488
|
-
}));
|
|
2489
|
-
const output = await deps.engine.narrate(owner, segments, characterRunner({
|
|
2490
|
-
parent: agent,
|
|
2491
|
-
boundaries: deps.boundaries,
|
|
2492
|
-
store: storeOf(owner.chat)
|
|
2493
|
-
}), args.addressed ?? []);
|
|
2494
|
-
const timelineNode = deps.delivered?.(owner.chat, output);
|
|
2495
|
-
const delivered = timelineNode === void 0 ? output : {
|
|
2496
|
-
...output,
|
|
2497
|
-
timelineNode
|
|
2498
|
-
};
|
|
2499
|
-
exec.concludeTurn();
|
|
2500
|
-
return record(delivered);
|
|
2501
|
-
}
|
|
2502
|
-
});
|
|
2503
|
-
const remember = defineTool({
|
|
2504
|
-
name: RECORD_TOOL,
|
|
2505
|
-
description: "Have the memory keeper write what happened since the last record into the story's lasting memory: the events, what each character took in of them, what they now believe, and the threads left open. It works in the background and covers every round still waiting, from this turn and earlier ones. Call it when the story changes phase, not after every round; rounds you do not record are kept and recorded together later.",
|
|
2506
|
-
parameters: { focus: {
|
|
2507
|
-
type: "string",
|
|
2508
|
-
description: "What is worth keeping, in a sentence: the decision, revelation or change that matters. The keeper writes it first and may leave out incidental detail."
|
|
2509
|
-
} },
|
|
2510
|
-
output: {
|
|
2511
|
-
schema: { type: "json" },
|
|
2512
|
-
render: (_args, value) => [{
|
|
2513
|
-
type: "text",
|
|
2514
|
-
text: `The memory keeper is writing ${value.rounds} round${value.rounds === 1 ? "" : "s"} into the story's memory.`
|
|
2515
|
-
}]
|
|
2516
|
-
},
|
|
2517
|
-
isConcurrencySafe: () => false,
|
|
2518
|
-
execute(args, exec) {
|
|
2519
|
-
const { owner } = ownerOf(exec.agent);
|
|
2520
|
-
return Promise.resolve(record({ rounds: deps.engine.record(owner, args.focus) }));
|
|
2521
|
-
}
|
|
2522
|
-
});
|
|
2523
|
-
const plot = defineTool({
|
|
2524
|
-
name: PLOT_TOOL,
|
|
2525
|
-
description: "Record only what play has already established about an optional plot. introduced means the named opportunity actually became perceptible in the resolved round, not merely that the pacing note suggested it. rejected means the player clearly declined that specific hook. resolved or abandoned applies to the plot as a whole. This records pacing state; it creates no event and makes nothing happen.",
|
|
2526
|
-
parameters: {
|
|
2527
|
-
plot: {
|
|
2528
|
-
type: "string",
|
|
2529
|
-
required: true,
|
|
2530
|
-
description: "The plot id from the pacing opportunity."
|
|
2531
|
-
},
|
|
2532
|
-
development: {
|
|
2533
|
-
type: "string",
|
|
2534
|
-
description: "The development id; required for introduced or rejected."
|
|
2535
|
-
},
|
|
2536
|
-
action: {
|
|
2537
|
-
type: "string",
|
|
2538
|
-
required: true,
|
|
2539
|
-
enum: [
|
|
2540
|
-
"introduced",
|
|
2541
|
-
"rejected",
|
|
2542
|
-
"resolved",
|
|
2543
|
-
"abandoned"
|
|
2544
|
-
]
|
|
2545
|
-
}
|
|
2546
|
-
},
|
|
2547
|
-
output: {
|
|
2548
|
-
schema: { type: "json" },
|
|
2549
|
-
render: (_args, value) => [{
|
|
2550
|
-
type: "text",
|
|
2551
|
-
text: `Plot pacing recorded: ${value.plot.name} is ${value.status}.`
|
|
2552
|
-
}]
|
|
2553
|
-
},
|
|
2554
|
-
isConcurrencySafe: () => false,
|
|
2555
|
-
execute(args, exec) {
|
|
2556
|
-
const { owner } = ownerOf(exec.agent);
|
|
2557
|
-
return Promise.resolve(record(updatePlotProgress(storeOf(owner.chat), args.plot, args.development, args.action)));
|
|
2558
|
-
}
|
|
2559
|
-
});
|
|
2560
|
-
const makePlot = defineTool({
|
|
2561
|
-
name: AUTHOR_PLOT_TOOL,
|
|
2562
|
-
description: "Ask a plot author for one optional, branching dramatic scaffold grounded in the current session. Use only when the scene explicitly says the quiet story has no plot scaffold and a reusable tension would help. It creates possibilities, not events, actions or obligations; do not introduce any part until a later pacing opportunity fits.",
|
|
2563
|
-
parameters: {},
|
|
2564
|
-
output: {
|
|
2565
|
-
schema: { type: "json" },
|
|
2566
|
-
render: (_args, value) => {
|
|
2567
|
-
const made = value;
|
|
2568
|
-
return [{
|
|
2569
|
-
type: "text",
|
|
2570
|
-
text: `Authored optional plot ${made.name} [${made.id}]: ${made.premise}. Nothing in it has happened. Continue this turn normally; a later pacing opportunity may offer a hook.`
|
|
2571
|
-
}];
|
|
2572
|
-
}
|
|
2573
|
-
},
|
|
2574
|
-
isConcurrencySafe: () => false,
|
|
2575
|
-
async execute(_args, exec) {
|
|
2576
|
-
const { agent, owner } = ownerOf(exec.agent);
|
|
2577
|
-
const store = storeOf(owner.chat);
|
|
2578
|
-
if (!plotAuthorNeeded(store, owner.turn)) throw new Error("the story does not currently need a plot author; continue with its existing plots, threads and character goals");
|
|
2579
|
-
return record(await authorPlot(agent, store, owner.turn, exec.signal));
|
|
2580
|
-
}
|
|
2581
|
-
});
|
|
2582
|
-
const expand = defineTool({
|
|
2583
|
-
name: EXPAND_TOOL,
|
|
2584
|
-
description: "Have a new character or place made up and fitted into the world, only when the story genuinely needs it. Valid reasons: scene_change (the story has actually gone somewhere that no known place covers, even as part of one), arrival (someone new has actually arrived or been brought into the scene), introduction (a specific person who has not appeared is needed by what is happening now and the story has established who they are), object (a thing so important or so complex that it needs inventing, such as an artifact with properties or a vehicle; or a place on the body that the world has no slot for; ordinary things the story uses are yours to bring in with introduce_thing), trigger (the story set up a moment that is bound to come, such as a deadline given, a promised arrival or a trap laid, that can be stated as a condition: a time, someone in a place, a thing held, a variable, an event), variable (a quantity, state or choice the story established matters from here on and nothing tracked fits: a debt, a health, a reputation). Not valid: colour, atmosphere, filling a quiet scene, something mentioned in passing, a passer-by or a crowd, scenery, or anything an existing character, place or thing can serve; use those. Ordinary things go through introduce_thing. A world author decides, and may decline and say what exists that can serve. A few things at most per turn.",
|
|
2585
|
-
parameters: {
|
|
2586
|
-
need: {
|
|
2587
|
-
type: "string",
|
|
2588
|
-
required: true,
|
|
2589
|
-
enum: AUTHORING_NEEDS,
|
|
2590
|
-
description: "Why: scene_change, arrival, introduction, object, variable or trigger."
|
|
2591
|
-
},
|
|
2592
|
-
what: {
|
|
2593
|
-
type: "string",
|
|
2594
|
-
required: true,
|
|
2595
|
-
description: "What is needed, from what the story has established: who or where (a name when it was given), their role, what happened to make it needed now, and where it is in relation to what exists."
|
|
2596
|
-
}
|
|
2597
|
-
},
|
|
2598
|
-
output: {
|
|
2599
|
-
schema: { type: "json" },
|
|
2600
|
-
render: (_args, value) => [{
|
|
2601
|
-
type: "text",
|
|
2602
|
-
text: renderExpansion(value)
|
|
2603
|
-
}]
|
|
2604
|
-
},
|
|
2605
|
-
isConcurrencySafe: () => false,
|
|
2606
|
-
async execute(args, exec) {
|
|
2607
|
-
const { owner } = ownerOf(exec.agent);
|
|
2608
|
-
await deps.engine.settle(owner.chat, owner.turn);
|
|
2609
|
-
return record(await deps.engine.expand(owner, {
|
|
2610
|
-
need: args.need,
|
|
2611
|
-
what: args.what
|
|
2612
|
-
}, exec.signal));
|
|
2613
|
-
}
|
|
2614
|
-
});
|
|
2615
|
-
const introduce = defineTool({
|
|
2616
|
-
name: INTRODUCE_TOOL,
|
|
2617
|
-
description: "Bring a thing into the story as a table's game master would: something the story is about to use or that the player can find, take or handle (a barricade, a tool, a note, a bottle, a key) and that does not exist yet. It is made at once, where you say: at a place (at, a place key; the player's place when you say nothing), in someone's hands (held_by, a character key) or inside a thing (inside, an item key). Afterwards it is a thing like any other: use its key in item_changes. Not for scenery nobody will touch (that is just part of the place), not for something important and complex enough to need a world author (use expand_world, reason object), and never for what exists already (list_items). A few things at most per turn.",
|
|
2618
|
-
parameters: {
|
|
2619
|
-
name: {
|
|
2620
|
-
type: "string",
|
|
2621
|
-
required: true,
|
|
2622
|
-
description: "What it is called, as the story names it."
|
|
2623
|
-
},
|
|
2624
|
-
description: {
|
|
2625
|
-
type: "string",
|
|
2626
|
-
description: "One or two sentences: what it looks like, what it is made of, what state it is in."
|
|
2627
|
-
},
|
|
2628
|
-
quantity: {
|
|
2629
|
-
type: "integer",
|
|
2630
|
-
description: "How many, when it is a number of the same thing (default 1)."
|
|
2631
|
-
},
|
|
2632
|
-
quality: {
|
|
2633
|
-
type: "string",
|
|
2634
|
-
description: "Its condition in a word or two (rusty, new), when that matters."
|
|
2635
|
-
},
|
|
2636
|
-
notable: {
|
|
2637
|
-
type: "boolean",
|
|
2638
|
-
description: "Whether the characters present would notice it at once."
|
|
2639
|
-
},
|
|
2640
|
-
hidden: {
|
|
2641
|
-
type: "boolean",
|
|
2642
|
-
description: "Whether it starts out of sight (until a round reveals it)."
|
|
2643
|
-
},
|
|
2644
|
-
at: {
|
|
2645
|
-
type: "string",
|
|
2646
|
-
description: "The key of the place it lies in."
|
|
2647
|
-
},
|
|
2648
|
-
held_by: {
|
|
2649
|
-
type: "string",
|
|
2650
|
-
description: "The key of the character who holds it."
|
|
2651
|
-
},
|
|
2652
|
-
inside: {
|
|
2653
|
-
type: "string",
|
|
2654
|
-
description: "The key of the container it is in."
|
|
2655
|
-
}
|
|
2656
|
-
},
|
|
2657
|
-
output: {
|
|
2658
|
-
schema: { type: "json" },
|
|
2659
|
-
render: (_args, value) => {
|
|
2660
|
-
const made = value;
|
|
2661
|
-
return [{
|
|
2662
|
-
type: "text",
|
|
2663
|
-
text: `Introduced ${made.name} [${made.key}], ${made.where}. Use the key in item_changes; nobody has taken it up yet.`
|
|
2664
|
-
}];
|
|
2665
|
-
}
|
|
2666
|
-
},
|
|
2667
|
-
isConcurrencySafe: () => false,
|
|
2668
|
-
async execute(args, exec) {
|
|
2669
|
-
const { owner } = ownerOf(exec.agent);
|
|
2670
|
-
await deps.engine.settle(owner.chat, owner.turn);
|
|
2671
|
-
return record(deps.engine.introduce(owner, {
|
|
2672
|
-
name: args.name,
|
|
2673
|
-
...args.description === void 0 ? {} : { description: args.description },
|
|
2674
|
-
...args.quantity === void 0 ? {} : { quantity: args.quantity },
|
|
2675
|
-
...args.quality === void 0 ? {} : { quality: args.quality },
|
|
2676
|
-
...args.notable === void 0 ? {} : { notable: args.notable },
|
|
2677
|
-
...args.hidden === void 0 ? {} : { hidden: args.hidden },
|
|
2678
|
-
...args.at === void 0 ? {} : { at: args.at },
|
|
2679
|
-
...args.held_by === void 0 ? {} : { heldBy: args.held_by },
|
|
2680
|
-
...args.inside === void 0 ? {} : { inside: args.inside }
|
|
2681
|
-
}));
|
|
2682
|
-
}
|
|
2683
|
-
});
|
|
2684
|
-
const skip = defineTool({
|
|
2685
|
-
name: TIME_TOOL,
|
|
2686
|
-
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.",
|
|
2687
|
-
parameters: {
|
|
2688
|
-
reason: {
|
|
2689
|
-
type: "string",
|
|
2690
|
-
required: true,
|
|
2691
|
-
description: "What happens in the time that passes, in a sentence (\"they sleep\", \"the ferry crosses\")."
|
|
2692
|
-
},
|
|
2693
|
-
by_minutes: {
|
|
2694
|
-
type: "integer",
|
|
2695
|
-
description: "How many minutes to skip."
|
|
2696
|
-
},
|
|
2697
|
-
until_time: {
|
|
2698
|
-
type: "string",
|
|
2699
|
-
description: "The time of day to skip to, 24-hour HH:MM: the next time the clock reads it, or the day given by days_later."
|
|
2700
|
-
},
|
|
2701
|
-
days_later: {
|
|
2702
|
-
type: "integer",
|
|
2703
|
-
description: "With until_time: that many days after today (0 is later today, 1 is tomorrow)."
|
|
2704
|
-
}
|
|
2705
|
-
},
|
|
2706
|
-
output: {
|
|
2707
|
-
schema: { type: "json" },
|
|
2708
|
-
render: (_args, value) => {
|
|
2709
|
-
const done = value;
|
|
2710
|
-
return [{
|
|
2711
|
-
type: "text",
|
|
2712
|
-
text: `${describeDuration(done.to - done.from)} pass. It is now ${describeTime({
|
|
2713
|
-
now: done.to,
|
|
2714
|
-
zone: done.zone
|
|
2715
|
-
})}.${done.moved.length === 0 ? "" : ` ${done.moved.map(routineLine).join(" ")}`} Nothing else changed: characters with you are still doing what they were, where they were; run a round for them if the time that passed changes that.${done.world.length === 0 ? "" : `\n${renderWorld(done.world).join("\n")}`}`
|
|
2716
|
-
}];
|
|
2717
|
-
}
|
|
2718
|
-
},
|
|
2719
|
-
isConcurrencySafe: () => false,
|
|
2720
|
-
async execute(args, exec) {
|
|
2721
|
-
const { owner } = ownerOf(exec.agent);
|
|
2722
|
-
await deps.engine.settle(owner.chat, owner.turn);
|
|
2723
|
-
if ([args.by_minutes !== void 0, args.until_time !== void 0].filter(Boolean).length !== 1) throw new Error("give exactly one of by_minutes and until_time");
|
|
2724
|
-
if (args.days_later !== void 0 && args.until_time === void 0) throw new Error("days_later goes with until_time");
|
|
2725
|
-
const request = args.by_minutes !== void 0 ? {
|
|
2726
|
-
byMinutes: args.by_minutes,
|
|
2727
|
-
reason: args.reason
|
|
2728
|
-
} : {
|
|
2729
|
-
untilTime: args.until_time ?? "",
|
|
2730
|
-
...args.days_later === void 0 ? {} : { daysLater: args.days_later },
|
|
2731
|
-
reason: args.reason
|
|
2732
|
-
};
|
|
2733
|
-
return record(deps.engine.advanceTime(owner, request));
|
|
2734
|
-
}
|
|
2735
|
-
});
|
|
2736
|
-
const preview = defineTool({
|
|
2737
|
-
name: PREVIEW_TOOL,
|
|
2738
|
-
description: "Show the player how the scene opens, as your very first action of a turn, before you ask any character: two or three sentences that set the mood and what the player's character perceives, ending on the open question of the moment. It is on screen at once, while the characters are still being asked, and is replaced in the player's view by the output you deliver with narrate, which may use it, change it or start over. Describe only what the scene and the player's own action establish: never what any other character does, says, decides or thinks, and nothing the player's character cannot perceive. Once per message.",
|
|
2739
|
-
parameters: { text: {
|
|
2740
|
-
type: "string",
|
|
2741
|
-
required: true,
|
|
2742
|
-
description: "The opening, plain prose in the narration voice."
|
|
2743
|
-
} },
|
|
2744
|
-
output: {
|
|
2745
|
-
schema: { type: "json" },
|
|
2746
|
-
render: (_args, value) => [{
|
|
2747
|
-
type: "text",
|
|
2748
|
-
text: `${encodePreview(value.text)}\nShown to the player. Now call activate_characters.`
|
|
2749
|
-
}]
|
|
2750
|
-
},
|
|
2751
|
-
isConcurrencySafe: () => false,
|
|
2752
|
-
execute(args, exec) {
|
|
2753
|
-
const { owner } = ownerOf(exec.agent);
|
|
2754
|
-
return Promise.resolve(record({ text: deps.engine.preview(owner, args.text) }));
|
|
2755
|
-
}
|
|
2756
|
-
});
|
|
2757
|
-
const ruling = defineTool({
|
|
2758
|
-
name: RULING_TOOL,
|
|
2759
|
-
description: "Rule on the player's out-of-character command, as the very first thing of an out-of-character turn: legal is false only when the command breaks the rules of the world or the player's own rules (the scene lists them); what is merely strange, impossible for the characters, or hard on them is legal, because the player may change the story itself. The reason is shown to the player, so write it plainly to them. Only a legal ruling lets you change anything.",
|
|
2760
|
-
parameters: {
|
|
2761
|
-
rule_check: {
|
|
2762
|
-
type: "string",
|
|
2763
|
-
required: true,
|
|
2764
|
-
description: "First, go through every rule the scene lists, one by one, in a few words each: does the command need what the rule excludes (the rules are literal: a rule against magic is broken by anything magical, however the command words it, and strange is not the same as against a rule)? Write \"no rules listed\" when there are none."
|
|
2765
|
-
},
|
|
2766
|
-
legal: {
|
|
2767
|
-
type: "boolean",
|
|
2768
|
-
required: true,
|
|
2769
|
-
description: "Whether the command is allowed: false when any listed rule is broken."
|
|
2770
|
-
},
|
|
2771
|
-
reason: {
|
|
2772
|
-
type: "string",
|
|
2773
|
-
required: true,
|
|
2774
|
-
description: "To the player, in plain words and briefly: what you will do, or which rule it breaks."
|
|
2775
|
-
}
|
|
2776
|
-
},
|
|
2777
|
-
output: {
|
|
2778
|
-
schema: { type: "json" },
|
|
2779
|
-
render: (_args, value) => [{
|
|
2780
|
-
type: "text",
|
|
2781
|
-
text: value.legal ? `Ruled legal. Now make it happen: ${RESOLVE_TOOL} (outcomes for any characters, asked or not, with their moves and things), ${EXPAND_TOOL} (for what does not exist yet), ${TIME_TOOL}; do only what the command says. Then call ${NARRATE_TOOL} with narration only, of what the player can perceive.` : `Ruled illegal; nothing changes. Call ${NARRATE_TOOL} with no segments: the player is told why.`
|
|
2782
|
-
}]
|
|
2783
|
-
},
|
|
2784
|
-
isConcurrencySafe: () => false,
|
|
2785
|
-
execute(args, exec) {
|
|
2786
|
-
const { owner } = ownerOf(exec.agent);
|
|
2787
|
-
return Promise.resolve(record(deps.engine.rule(owner, args.legal, args.reason)));
|
|
2788
|
-
}
|
|
2789
|
-
});
|
|
2790
|
-
const answer = defineTool({
|
|
2791
|
-
name: ANSWER_TOOL,
|
|
2792
|
-
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.",
|
|
2793
|
-
parameters: {},
|
|
2794
|
-
output: {
|
|
2795
|
-
schema: { type: "json" },
|
|
2796
|
-
render: () => [{
|
|
2797
|
-
type: "text",
|
|
2798
|
-
text: `The player is shown the answer. Now call ${NARRATE_TOOL} with no segments.`
|
|
2799
|
-
}]
|
|
2800
|
-
},
|
|
2801
|
-
isConcurrencySafe: () => false,
|
|
2802
|
-
async execute(_args, exec) {
|
|
2803
|
-
const { agent, owner } = ownerOf(exec.agent);
|
|
2804
|
-
return record(await deps.engine.answer(owner, answerRunner({
|
|
2805
|
-
parent: agent,
|
|
2806
|
-
boundaries: deps.boundaries,
|
|
2807
|
-
store: storeOf(owner.chat)
|
|
2808
|
-
}), exec.signal));
|
|
2809
|
-
}
|
|
2810
|
-
});
|
|
2811
|
-
return [
|
|
2812
|
-
defineTool({
|
|
2813
|
-
name: JUDGE_TOOL,
|
|
2814
|
-
description: "Judge what the player's character tries, as the very first thing of an ordinary turn (nothing else is accepted before it): allowed is false when it is not viable (what it needs is out of reach, is not there, or the character cannot do it or does not have it) or when it breaks a rule of the world or of the player (the scene lists them and they are absolute: no magic means no spell works). The player's claim of an effect is not a fact. What merely looks strange, rash or foolish is allowed. When it is not allowed the attempt is played as a failure for the player's character, and nobody is asked to act on it.",
|
|
2815
|
-
parameters: {
|
|
2816
|
-
rule_check: {
|
|
2817
|
-
type: "string",
|
|
2818
|
-
required: true,
|
|
2819
|
-
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."
|
|
2820
|
-
},
|
|
2821
|
-
at: {
|
|
2822
|
-
type: "string",
|
|
2823
|
-
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)."
|
|
2824
|
-
},
|
|
2825
|
-
allowed: {
|
|
2826
|
-
type: "boolean",
|
|
2827
|
-
required: true,
|
|
2828
|
-
description: "Whether what the character tries can work: false when any listed rule is broken or it is not viable."
|
|
2829
|
-
},
|
|
2830
|
-
reason: {
|
|
2831
|
-
type: "string",
|
|
2832
|
-
required: true,
|
|
2833
|
-
description: "What the character tries, and the rule or the fact that decides it, in a sentence."
|
|
2834
|
-
}
|
|
2835
|
-
},
|
|
2836
|
-
output: {
|
|
2837
|
-
schema: { type: "json" },
|
|
2838
|
-
render: (_args, value) => [{
|
|
2839
|
-
type: "text",
|
|
2840
|
-
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.`
|
|
2841
|
-
}]
|
|
2842
|
-
},
|
|
2843
|
-
isConcurrencySafe: () => false,
|
|
2844
|
-
execute(args, exec) {
|
|
2845
|
-
const { owner } = ownerOf(exec.agent);
|
|
2846
|
-
return Promise.resolve(record(deps.engine.judge(owner, args.allowed, args.reason, args.at)));
|
|
2847
|
-
}
|
|
2848
|
-
}),
|
|
2849
|
-
preview,
|
|
2850
|
-
ruling,
|
|
2851
|
-
answer,
|
|
2852
|
-
activate,
|
|
2853
|
-
resolve,
|
|
2854
|
-
skip,
|
|
2855
|
-
remember,
|
|
2856
|
-
expand,
|
|
2857
|
-
introduce,
|
|
2858
|
-
makePlot,
|
|
2859
|
-
plot,
|
|
2860
|
-
narrate
|
|
2861
|
-
];
|
|
2862
|
-
}
|
|
2863
|
-
//#endregion
|
|
2864
1973
|
//#region src/host/memory/embedding-index.ts
|
|
2865
1974
|
const BATCH = 32;
|
|
2866
1975
|
/**
|
|
@@ -3096,154 +2205,6 @@ var AfterTurnJobs = class {
|
|
|
3096
2205
|
}));
|
|
3097
2206
|
}
|
|
3098
2207
|
};
|
|
3099
|
-
/**
|
|
3100
|
-
* What the characters away from the player decide about what fell due, one commitment after another: their activity, and a move to where the commitment was when they
|
|
3101
|
-
* mean to keep it. It is recorded as a round only they perceive, and the keeper writes it.
|
|
3102
|
-
*/
|
|
3103
|
-
async function runOffScene(deps, owner, state, store, due, limit) {
|
|
3104
|
-
const { summary, perCharacter, joint } = store.settings().offstage;
|
|
3105
|
-
let budget = limit;
|
|
3106
|
-
const acted = /* @__PURE__ */ new Map();
|
|
3107
|
-
for (const item of due) {
|
|
3108
|
-
if (budget <= 0) break;
|
|
3109
|
-
const cast = store.cast();
|
|
3110
|
-
const keys = item.characters.filter((key) => cast.some((member) => member.key === key) && (acted.get(key) ?? 0) < perCharacter).slice(0, budget);
|
|
3111
|
-
if (keys.length === 0) continue;
|
|
3112
|
-
budget -= keys.length;
|
|
3113
|
-
for (const key of keys) acted.set(key, (acted.get(key) ?? 0) + 1);
|
|
3114
|
-
const reports = [];
|
|
3115
|
-
for (const group of joint ? keys.map((key) => [key]) : [keys]) {
|
|
3116
|
-
const attending = reports.filter((report) => report.commitment !== "low").map((report) => `${report.name} has decided: ${report.intent}${report.utterance === void 0 ? "" : `, saying "${report.utterance}"`}.`);
|
|
3117
|
-
const asked = {
|
|
3118
|
-
...state,
|
|
3119
|
-
situation: attending.length === 0 ? item.text : `${item.text}\n${attending.join("\n")}`,
|
|
3120
|
-
history: [],
|
|
3121
|
-
recalled: /* @__PURE__ */ new Map()
|
|
3122
|
-
};
|
|
3123
|
-
delete asked.first;
|
|
3124
|
-
delete asked.second;
|
|
3125
|
-
reports.push(...(await deps.ask(group, cast, asked, summary)).reports);
|
|
3126
|
-
}
|
|
3127
|
-
if (reports.length === 0) continue;
|
|
3128
|
-
const where = item.place;
|
|
3129
|
-
const moves = new Map(reports.flatMap((report) => {
|
|
3130
|
-
if (report.commitment === "low" || where === void 0) return [];
|
|
3131
|
-
const check = store.checkMove(report.character, {
|
|
3132
|
-
location: where,
|
|
3133
|
-
ignoreExits: true
|
|
3134
|
-
});
|
|
3135
|
-
return check.ok ? [[report.character, check]] : [];
|
|
3136
|
-
}));
|
|
3137
|
-
store.commit(reports.flatMap((report) => {
|
|
3138
|
-
const member = cast.find((candidate) => candidate.key === report.character);
|
|
3139
|
-
const move = moves.get(report.character);
|
|
3140
|
-
return member === void 0 ? [] : [{
|
|
3141
|
-
character: member.character.id,
|
|
3142
|
-
patch: { activity: report.intent },
|
|
3143
|
-
...move?.ok === true ? { move: move.move } : {}
|
|
3144
|
-
}];
|
|
3145
|
-
}));
|
|
3146
|
-
state.offstage += 1;
|
|
3147
|
-
const time = store.time(owner.turn);
|
|
3148
|
-
store.queueCommit({
|
|
3149
|
-
turn: owner.turn,
|
|
3150
|
-
round: 100 + state.offstage,
|
|
3151
|
-
situation: item.text,
|
|
3152
|
-
offstage: true,
|
|
3153
|
-
time: {
|
|
3154
|
-
from: time.now,
|
|
3155
|
-
to: time.now,
|
|
3156
|
-
zone: time.zone
|
|
3157
|
-
},
|
|
3158
|
-
entries: reports.map((report) => {
|
|
3159
|
-
const at = cast.find((candidate) => candidate.key === report.character)?.place?.location;
|
|
3160
|
-
const move = moves.get(report.character);
|
|
3161
|
-
return {
|
|
3162
|
-
character: report.character,
|
|
3163
|
-
name: report.name,
|
|
3164
|
-
intent: report.intent,
|
|
3165
|
-
commitment: report.commitment,
|
|
3166
|
-
result: "succeeded",
|
|
3167
|
-
summary: report.intent,
|
|
3168
|
-
spoken: false,
|
|
3169
|
-
...at === void 0 ? {} : { at: {
|
|
3170
|
-
key: locationKey(at),
|
|
3171
|
-
name: at.name
|
|
3172
|
-
} },
|
|
3173
|
-
...move?.ok === true && move.plan.from !== move.move.to ? { moved: { ...move.to } } : {}
|
|
3174
|
-
};
|
|
3175
|
-
})
|
|
3176
|
-
});
|
|
3177
|
-
store.markHandled([item.key]);
|
|
3178
|
-
deps.write();
|
|
3179
|
-
if (summary && deps.stillThis()) {
|
|
3180
|
-
const lines = reports.flatMap((report) => report.account === void 0 ? [] : [report.account]);
|
|
3181
|
-
store.addNote("away", owner.turn, lines);
|
|
3182
|
-
}
|
|
3183
|
-
}
|
|
3184
|
-
}
|
|
3185
|
-
//#endregion
|
|
3186
|
-
//#region src/host/orchestration/turn-text.ts
|
|
3187
|
-
/**
|
|
3188
|
-
* 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
|
|
3189
|
-
* character either. When the label is exactly a character of the scene, the label is folded into the words that follow it.
|
|
3190
|
-
*/
|
|
3191
|
-
function foldSpeakerLabels(segments, cast) {
|
|
3192
|
-
const found = (label) => {
|
|
3193
|
-
const wanted = label.trim().normalize("NFKC").toLowerCase();
|
|
3194
|
-
return wanted === "" ? void 0 : cast.find((member) => member.key.toLowerCase() === wanted || member.character.name.normalize("NFKC").toLowerCase() === wanted);
|
|
3195
|
-
};
|
|
3196
|
-
const folded = [];
|
|
3197
|
-
for (let index = 0; index < segments.length; index += 1) {
|
|
3198
|
-
const segment = segments[index];
|
|
3199
|
-
const next = segments[index + 1];
|
|
3200
|
-
const speaker = segment.type === "utterance" && (segment.character ?? "") === "" && next?.type === "utterance" && (next.character ?? "") === "" ? found(segment.text) : void 0;
|
|
3201
|
-
if (speaker === void 0) {
|
|
3202
|
-
folded.push(segment);
|
|
3203
|
-
continue;
|
|
3204
|
-
}
|
|
3205
|
-
folded.push({
|
|
3206
|
-
...next,
|
|
3207
|
-
character: speaker.key
|
|
3208
|
-
});
|
|
3209
|
-
index += 1;
|
|
3210
|
-
}
|
|
3211
|
-
return folded;
|
|
3212
|
-
}
|
|
3213
|
-
/** 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. */
|
|
3214
|
-
const SAME_SITUATION = .85;
|
|
3215
|
-
const pieces = (text) => {
|
|
3216
|
-
const found = /* @__PURE__ */ new Set();
|
|
3217
|
-
const chars = [...text];
|
|
3218
|
-
for (let index = 0; index + 3 <= chars.length; index += 1) found.add(chars.slice(index, index + 3).join(""));
|
|
3219
|
-
return found;
|
|
3220
|
-
};
|
|
3221
|
-
/** 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. */
|
|
3222
|
-
function repeatsEarlier(signature, asked) {
|
|
3223
|
-
const [keys = "", text = ""] = signature.split("|");
|
|
3224
|
-
const now = new Set(keys.split(","));
|
|
3225
|
-
const mine = pieces(text);
|
|
3226
|
-
return asked.some((earlier) => {
|
|
3227
|
-
const [before = "", was = ""] = earlier.split("|");
|
|
3228
|
-
if (![...now].every((key) => before.split(",").includes(key))) return false;
|
|
3229
|
-
const theirs = pieces(was);
|
|
3230
|
-
const shared = [...mine].filter((piece) => theirs.has(piece)).length;
|
|
3231
|
-
const union = mine.size + theirs.size - shared;
|
|
3232
|
-
return union === 0 || shared / union >= SAME_SITUATION;
|
|
3233
|
-
});
|
|
3234
|
-
}
|
|
3235
|
-
/** The words as the character said them, whatever quotes the game master put around them. */
|
|
3236
|
-
function asSpoken(segments, spoken) {
|
|
3237
|
-
const left = [...spoken];
|
|
3238
|
-
return segments.map((segment) => {
|
|
3239
|
-
if (segment.type !== "utterance") return segment;
|
|
3240
|
-
const at = left.findIndex((said) => said.character === segment.character && sameWords(said.text, segment.text));
|
|
3241
|
-
return at === -1 ? segment : {
|
|
3242
|
-
...segment,
|
|
3243
|
-
text: left.splice(at, 1)[0].text
|
|
3244
|
-
};
|
|
3245
|
-
});
|
|
3246
|
-
}
|
|
3247
2208
|
//#endregion
|
|
3248
2209
|
//#region src/host/orchestration/turn-rules.ts
|
|
3249
2210
|
/** Whether the game master still has to judge the player's action. */
|
|
@@ -3261,6 +2222,11 @@ function oocBlock(state) {
|
|
|
3261
2222
|
function previewDue(state) {
|
|
3262
2223
|
return state.phase === "idle" && state.previewWanted && !state.previewed && state.judgement?.allowed !== false;
|
|
3263
2224
|
}
|
|
2225
|
+
/** Whether the game master still has to roll for an attempt it judged uncertain: nobody is asked to act before the dice are rolled. */
|
|
2226
|
+
function rollDue(state, store) {
|
|
2227
|
+
return state.rollWanted === true && state.phase === "idle" && state.judgement?.allowed !== false && store.story.dice.rolls({ turn: state.turn }).every((roll) => roll.by !== "agent");
|
|
2228
|
+
}
|
|
2229
|
+
const ROLL_FIRST = "the attempt is uncertain and matters, so roll for it before anyone is asked to act: call roll_check with the check that fits (list_checks shows them), naming the character it is for; roll_dice only when no check fits";
|
|
3264
2230
|
function explainPhase(state, attempted) {
|
|
3265
2231
|
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";
|
|
3266
2232
|
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";
|
|
@@ -3282,7 +2248,7 @@ function mustSay(state) {
|
|
|
3282
2248
|
return state.utterances.length === 0 ? "" : `; it must carry these utterances exactly, in this order: ${state.utterances.map((utterance) => `${utterance.name}: "${utterance.text}"`).join(" | ")}`;
|
|
3283
2249
|
}
|
|
3284
2250
|
/** `player` is the key of the player's character when an outcome for it is acceptable: it is not asked to act, but what it did can move it or have it handle things. */
|
|
3285
|
-
function checkOutcomes(attempt, outcomes, store, player, decreed) {
|
|
2251
|
+
function checkOutcomes(attempt, outcomes, store, player, decreed, state) {
|
|
3286
2252
|
const expected = decreed === void 0 ? attempt.reports.map((report) => report.character) : [...new Set(outcomes.map((outcome) => outcome.character))];
|
|
3287
2253
|
const unknown = decreed === void 0 ? [] : [...new Set(outcomes.map((outcome) => outcome.character))].filter((key) => !decreed.some((member) => member.key === key)).map((key) => `${key} is not a character of the story`);
|
|
3288
2254
|
const given = outcomes.filter((outcome) => outcome.character !== player).map((outcome) => outcome.character);
|
|
@@ -3296,9 +2262,35 @@ function checkOutcomes(attempt, outcomes, store, player, decreed) {
|
|
|
3296
2262
|
...outcomes.filter((outcome) => outcome.summary.trim() === "").map((outcome) => `${outcome.character}: say what happens`),
|
|
3297
2263
|
...outcomes.flatMap((outcome) => moveProblems(outcome, store)),
|
|
3298
2264
|
...itemProblems(outcomes, store),
|
|
3299
|
-
...outcomes.flatMap((outcome) => store.checkVariableChanges(outcome.variableChanges ?? []).map((problem) => `${outcome.character}: ${problem}`))
|
|
2265
|
+
...outcomes.flatMap((outcome) => store.checkVariableChanges(outcome.variableChanges ?? []).map((problem) => `${outcome.character}: ${problem}`)),
|
|
2266
|
+
...state === void 0 ? [] : rollProblems(outcomes, store, state)
|
|
3300
2267
|
];
|
|
3301
2268
|
if (problems.length > 0) throw new Error(`the outcomes are not acceptable: ${problems.join("; ")} (expected exactly: ${expected.join(", ")})`);
|
|
2269
|
+
for (const outcome of outcomes) if (outcome.roll !== void 0) state?.cited.add(outcome.roll);
|
|
2270
|
+
}
|
|
2271
|
+
/**
|
|
2272
|
+
* Why an outcome does not follow the dice, if it does not. A roll decides what it was rolled for: the result of the outcome is the roll's own, a roll serves one outcome,
|
|
2273
|
+
* and a character a check was rolled for cannot be given an outcome without it.
|
|
2274
|
+
*/
|
|
2275
|
+
function rollProblems(outcomes, store, state) {
|
|
2276
|
+
const rolls = store.story.dice.rolls({ turn: state.turn });
|
|
2277
|
+
const problems = [];
|
|
2278
|
+
const taken = /* @__PURE__ */ new Set();
|
|
2279
|
+
for (const outcome of outcomes) {
|
|
2280
|
+
if (outcome.roll === void 0) continue;
|
|
2281
|
+
const roll = rolls.find((candidate) => candidate.id === outcome.roll);
|
|
2282
|
+
if (roll === void 0) problems.push(`${outcome.character}: there is no roll ${outcome.roll} in this turn: use the id the roll tool gave`);
|
|
2283
|
+
else if (roll.readout === void 0) problems.push(`${outcome.character}: roll ${roll.id} was a plain roll with no result to take: leave roll out and decide it yourself`);
|
|
2284
|
+
else if (state.cited.has(roll.id) || taken.has(roll.id)) problems.push(`${outcome.character}: roll ${roll.id} already decided another outcome: roll again for this one`);
|
|
2285
|
+
else if (roll.character !== void 0 && roll.character !== outcome.character) problems.push(`${outcome.character}: roll ${roll.id} was for ${roll.character}`);
|
|
2286
|
+
else if (roll.readout.result !== outcome.result) problems.push(`${outcome.character}: roll ${roll.id} gave ${roll.readout.result} (${roll.readout.label}), so the outcome's result is ${roll.readout.result}, not ${outcome.result}: the dice decide it`);
|
|
2287
|
+
taken.add(outcome.roll);
|
|
2288
|
+
}
|
|
2289
|
+
for (const roll of rolls) {
|
|
2290
|
+
if (roll.readout === void 0 || roll.character === void 0 || state.cited.has(roll.id) || taken.has(roll.id)) continue;
|
|
2291
|
+
if (outcomes.some((outcome) => outcome.character === roll.character && outcome.roll === void 0)) problems.push(`${roll.character}: you rolled for this character (roll ${roll.id}: ${roll.readout.label}, ${roll.readout.result}): give the outcome the result ${roll.readout.result} and cite the roll by adding "roll": ${roll.id} to that outcome`);
|
|
2292
|
+
}
|
|
2293
|
+
return problems;
|
|
3302
2294
|
}
|
|
3303
2295
|
/** Why a character cannot go where the outcome sends it, if it cannot: the game master hears it before the round is recorded. */
|
|
3304
2296
|
function moveProblems(outcome, store) {
|
|
@@ -3384,12 +2376,432 @@ function decisions(state) {
|
|
|
3384
2376
|
return decided;
|
|
3385
2377
|
}
|
|
3386
2378
|
//#endregion
|
|
2379
|
+
//#region src/host/orchestration/character-attempt.ts
|
|
2380
|
+
/** Every character acts at the same time; one that fails or answers badly is reported, not fatal. */
|
|
2381
|
+
async function askCharacters(deps, owner, number, keys, cast, state, run, signal, away = false) {
|
|
2382
|
+
const { store, boundaries } = deps;
|
|
2383
|
+
const { outputValidator } = boundaries;
|
|
2384
|
+
const reaction = number === 2 ? reactions(state) : void 0;
|
|
2385
|
+
const queryVector = number === 1 && store !== void 0 ? await store.queryVector(state.situation, signal) : void 0;
|
|
2386
|
+
const time = store?.time(owner.turn);
|
|
2387
|
+
const answers = await Promise.all(keys.map(async (key) => {
|
|
2388
|
+
const member = cast.find((candidate) => candidate.key === key);
|
|
2389
|
+
if (member === void 0) return {
|
|
2390
|
+
character: key,
|
|
2391
|
+
name: key,
|
|
2392
|
+
reason: "no such character"
|
|
2393
|
+
};
|
|
2394
|
+
const previous = number === 2 ? state.first?.reports.find((report) => report.character === key) : void 0;
|
|
2395
|
+
const memories = number === 1 && store !== void 0 ? await boundaries.recaller.recall({
|
|
2396
|
+
member,
|
|
2397
|
+
situation: state.situation,
|
|
2398
|
+
turn: owner.turn,
|
|
2399
|
+
policy: store.settings().memory,
|
|
2400
|
+
...store.settings().diagnostics.keepRuns ? { explain: true } : {},
|
|
2401
|
+
...queryVector === void 0 ? {} : { queryVector }
|
|
2402
|
+
}, store.story, signal) : state.recalled.get(key) ?? [];
|
|
2403
|
+
if (number === 1 && memories.length > 0) state.recalled.set(key, memories);
|
|
2404
|
+
const place = member.place === void 0 ? void 0 : store?.place(member.place.location.id);
|
|
2405
|
+
const relationships = store === void 0 ? [] : knownRelationships(member, place === void 0 ? cast : place.here, state.situation, cast, store);
|
|
2406
|
+
const carrying = store === void 0 ? [] : store.carried(member.character.id);
|
|
2407
|
+
const knowledge = store?.knowledge(member.character.id, owner.turn, (place === void 0 ? cast : place.here).map((entry) => entry.character.id));
|
|
2408
|
+
const affiliations = store?.affiliations(member.character.id) ?? [];
|
|
2409
|
+
const agenda = store?.agenda(owner.turn, member.character.id);
|
|
2410
|
+
const cues = cuesOf(state, key, member, store, number);
|
|
2411
|
+
const upcoming = store?.upcomingFor(member.character.id, owner.turn) ?? [];
|
|
2412
|
+
const tracked = store?.trackedFor(member.character.id, (place === void 0 ? cast : place.here).map((entry) => entry.character.id));
|
|
2413
|
+
const request = {
|
|
2414
|
+
member,
|
|
2415
|
+
cast,
|
|
2416
|
+
situation: state.situation,
|
|
2417
|
+
attempt: number,
|
|
2418
|
+
language: store?.settings().prompts.language ?? "en",
|
|
2419
|
+
turn: owner.turn,
|
|
2420
|
+
...away ? { away: true } : {},
|
|
2421
|
+
lookup: store?.settings().memory.lookup ?? false,
|
|
2422
|
+
text: (id, variables) => deps.storeOf(owner).text(id, variables),
|
|
2423
|
+
...time === void 0 ? {} : { time },
|
|
2424
|
+
...agenda === void 0 ? {} : { agenda },
|
|
2425
|
+
...cues.length === 0 ? {} : { cues },
|
|
2426
|
+
...upcoming.length === 0 ? {} : { upcoming },
|
|
2427
|
+
...tracked === void 0 || tracked.self === "" && tracked.others.length === 0 ? {} : { tracked },
|
|
2428
|
+
...place === void 0 ? {} : { place },
|
|
2429
|
+
...relationships.length === 0 ? {} : { relationships },
|
|
2430
|
+
...carrying.length === 0 ? {} : { carrying },
|
|
2431
|
+
...knowledge === void 0 ? {} : { knowledge },
|
|
2432
|
+
...affiliations.length === 0 ? {} : { affiliations },
|
|
2433
|
+
...memories.length === 0 ? {} : { memories },
|
|
2434
|
+
...state.history.length === 0 ? {} : { earlier: [...state.history] },
|
|
2435
|
+
...reaction === void 0 ? {} : { reaction },
|
|
2436
|
+
...previous === void 0 ? {} : { previous: {
|
|
2437
|
+
intent: previous.intent,
|
|
2438
|
+
commitment: previous.commitment
|
|
2439
|
+
} }
|
|
2440
|
+
};
|
|
2441
|
+
try {
|
|
2442
|
+
const action = await run(request, signal);
|
|
2443
|
+
const verdict = outputValidator.validate(action, request);
|
|
2444
|
+
if (!verdict.ok) return {
|
|
2445
|
+
character: key,
|
|
2446
|
+
name: member.character.name,
|
|
2447
|
+
reason: verdict.reason
|
|
2448
|
+
};
|
|
2449
|
+
return {
|
|
2450
|
+
character: key,
|
|
2451
|
+
name: member.character.name,
|
|
2452
|
+
...action
|
|
2453
|
+
};
|
|
2454
|
+
} catch (error) {
|
|
2455
|
+
if (signal.aborted) throw error;
|
|
2456
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
2457
|
+
deps.problem(`${member.character.name} could not act: ${reason}`);
|
|
2458
|
+
return {
|
|
2459
|
+
character: key,
|
|
2460
|
+
name: member.character.name,
|
|
2461
|
+
reason,
|
|
2462
|
+
technical: true
|
|
2463
|
+
};
|
|
2464
|
+
}
|
|
2465
|
+
}));
|
|
2466
|
+
return {
|
|
2467
|
+
reports: answers.filter((answer) => "intent" in answer),
|
|
2468
|
+
failures: answers.filter((answer) => "reason" in answer)
|
|
2469
|
+
};
|
|
2470
|
+
}
|
|
2471
|
+
//#endregion
|
|
2472
|
+
//#region src/host/orchestration/round-record.ts
|
|
2473
|
+
/** Records the game master's decisions as a finished round: what the characters did and said, what changed in the story, and the round queued for the keeper. `fire` runs the triggers the round may have made true. */
|
|
2474
|
+
function recordRound(store, owner, state, fire) {
|
|
2475
|
+
const decided = decisions(state);
|
|
2476
|
+
const limit = store.settings().maxRounds;
|
|
2477
|
+
const round = state.rounds + 1;
|
|
2478
|
+
const checked = new Map([...decided.values()].flatMap(({ outcome }) => outcome.moveTo === void 0 ? [] : [[outcome.character, store.checkMove(outcome.character, outcome.moveTo)]]));
|
|
2479
|
+
const refused = [...checked].flatMap(([character, check]) => check.ok ? [] : [`${character}: ${check.problems.join("; ")}`]);
|
|
2480
|
+
if (refused.length > 0) throw new Error(`a move is no longer possible: ${refused.join("; ")}`);
|
|
2481
|
+
const spoken = [...decided.values()].flatMap(({ outcome, report }) => report.utterance === void 0 || outcome.spoken === false || state.utterances.some((said) => said.character === report.character && sameWords(said.text, report.utterance)) ? [] : [{
|
|
2482
|
+
character: report.character,
|
|
2483
|
+
name: report.name,
|
|
2484
|
+
text: report.utterance,
|
|
2485
|
+
round
|
|
2486
|
+
}]);
|
|
2487
|
+
state.rounds = round;
|
|
2488
|
+
state.situations.push(state.situation);
|
|
2489
|
+
state.utterances.push(...spoken);
|
|
2490
|
+
for (const { outcome, report } of decided.values()) {
|
|
2491
|
+
state.reports.push(report);
|
|
2492
|
+
state.outcomes.push(outcome);
|
|
2493
|
+
state.history.push({
|
|
2494
|
+
round,
|
|
2495
|
+
character: report.character,
|
|
2496
|
+
name: report.name,
|
|
2497
|
+
intent: report.intent,
|
|
2498
|
+
...report.utterance === void 0 ? {} : { utterance: report.utterance },
|
|
2499
|
+
result: outcome.result,
|
|
2500
|
+
summary: outcome.summary
|
|
2501
|
+
});
|
|
2502
|
+
}
|
|
2503
|
+
state.limit = limit;
|
|
2504
|
+
state.phase = "between";
|
|
2505
|
+
const playerPlace = state.cast.find((member) => member.character.control === "player")?.place?.location;
|
|
2506
|
+
const updates = [...decided.values()].flatMap(({ outcome, report }) => {
|
|
2507
|
+
const member = state.cast.find((candidate) => candidate.key === report.character);
|
|
2508
|
+
if (member === void 0) return [];
|
|
2509
|
+
const check = checked.get(outcome.character);
|
|
2510
|
+
return [{
|
|
2511
|
+
key: report.character,
|
|
2512
|
+
update: {
|
|
2513
|
+
character: member.character.id,
|
|
2514
|
+
patch: {
|
|
2515
|
+
activity: outcome.summary,
|
|
2516
|
+
...outcome.publicState === void 0 || outcome.publicState.trim() === "" ? {} : { publicState: outcome.publicState.trim() }
|
|
2517
|
+
},
|
|
2518
|
+
...check?.ok === true ? { move: check.move } : {},
|
|
2519
|
+
...outcome.itemChanges === void 0 || outcome.itemChanges.length === 0 ? {} : { items: outcome.itemChanges }
|
|
2520
|
+
}
|
|
2521
|
+
}];
|
|
2522
|
+
});
|
|
2523
|
+
const effects = /* @__PURE__ */ new Map();
|
|
2524
|
+
const done = store.commit(updates.map((entry) => entry.update));
|
|
2525
|
+
updates.forEach(({ key }, index) => {
|
|
2526
|
+
effects.set(key, done[index] ?? []);
|
|
2527
|
+
});
|
|
2528
|
+
const tracked = new Map(updates.map(({ key }) => [key, store.changeVariables(owner.turn, decided.get(key)?.outcome.variableChanges ?? []).map((effect) => ({
|
|
2529
|
+
subject: effect.subjectName,
|
|
2530
|
+
name: effect.variable.name,
|
|
2531
|
+
from: describeValue(effect.variable, effect.from),
|
|
2532
|
+
to: describeValue(effect.variable, effect.to)
|
|
2533
|
+
}))]));
|
|
2534
|
+
const journey = [...checked].flatMap(([character, check]) => check.ok && state.cast.find((candidate) => candidate.key === character)?.character.control === "player" ? [check.plan.travelSeconds ?? 0] : [])[0];
|
|
2535
|
+
const time = store.endRound(owner.turn, round, state.elapsed, journey);
|
|
2536
|
+
delete state.elapsed;
|
|
2537
|
+
const world = fire(round);
|
|
2538
|
+
const loose = store.story.dice.rolls({ turn: owner.turn }).filter((roll) => !state.cited.has(roll.id) && !state.reported.has(roll.id));
|
|
2539
|
+
for (const roll of loose) state.reported.add(roll.id);
|
|
2540
|
+
store.queueCommit({
|
|
2541
|
+
turn: owner.turn,
|
|
2542
|
+
round,
|
|
2543
|
+
situation: state.situation,
|
|
2544
|
+
...loose.length === 0 ? {} : { rolls: loose.map((roll) => `${roll.reason}: ${roll.expression} came to ${roll.total}${roll.readout === void 0 ? "" : `: ${roll.readout.label}${roll.readout.critical ? " (critical)" : ""} (${roll.readout.result})`}${roll.by === "player" ? " (rolled by the player)" : ""}`) },
|
|
2545
|
+
...playerPlace === void 0 ? {} : { place: {
|
|
2546
|
+
key: locationKey(playerPlace),
|
|
2547
|
+
name: playerPlace.name
|
|
2548
|
+
} },
|
|
2549
|
+
time: {
|
|
2550
|
+
from: time.from,
|
|
2551
|
+
to: time.to,
|
|
2552
|
+
zone: time.zone
|
|
2553
|
+
},
|
|
2554
|
+
entries: [...decided.values()].map(({ outcome, report }) => {
|
|
2555
|
+
const at = state.cast.find((candidate) => candidate.key === report.character)?.place?.location;
|
|
2556
|
+
const check = checked.get(outcome.character);
|
|
2557
|
+
return {
|
|
2558
|
+
character: report.character,
|
|
2559
|
+
name: report.name,
|
|
2560
|
+
intent: report.intent,
|
|
2561
|
+
commitment: report.commitment,
|
|
2562
|
+
...report.utterance === void 0 ? {} : { utterance: report.utterance },
|
|
2563
|
+
result: outcome.result,
|
|
2564
|
+
summary: outcome.summary,
|
|
2565
|
+
...outcome.publicState === void 0 || outcome.publicState.trim() === "" ? {} : { publicState: outcome.publicState.trim() },
|
|
2566
|
+
spoken: outcome.spoken !== false,
|
|
2567
|
+
...(tracked.get(report.character) ?? []).length === 0 ? {} : { variables: tracked.get(report.character) },
|
|
2568
|
+
...state.cast.find((candidate) => candidate.key === report.character)?.character.control === "player" ? { player: true } : {},
|
|
2569
|
+
...report.forced === true ? { forced: true } : {},
|
|
2570
|
+
...(effects.get(report.character) ?? []).length === 0 ? {} : { items: (effects.get(report.character) ?? []).map((effect) => ({
|
|
2571
|
+
name: effect.item.name,
|
|
2572
|
+
action: effect.action,
|
|
2573
|
+
summary: effect.summary
|
|
2574
|
+
})) },
|
|
2575
|
+
...at === void 0 ? {} : { at: {
|
|
2576
|
+
key: locationKey(at),
|
|
2577
|
+
name: at.name
|
|
2578
|
+
} },
|
|
2579
|
+
...check?.ok === true && check.plan.from !== check.move.to ? { moved: {
|
|
2580
|
+
...check.to,
|
|
2581
|
+
...check.position === void 0 ? {} : { position: check.position }
|
|
2582
|
+
} } : {}
|
|
2583
|
+
};
|
|
2584
|
+
})
|
|
2585
|
+
});
|
|
2586
|
+
return {
|
|
2587
|
+
round,
|
|
2588
|
+
limit,
|
|
2589
|
+
updated: [...decided.keys()],
|
|
2590
|
+
retryFailures: state.second?.failures ?? [],
|
|
2591
|
+
utterances: spoken,
|
|
2592
|
+
spoken: [...state.utterances],
|
|
2593
|
+
waiting: store.pendingCommits().length,
|
|
2594
|
+
variables: [...tracked.values()].flat().map((change) => `${change.subject}'s ${change.name} is now ${change.to} (it was ${change.from})`),
|
|
2595
|
+
time: {
|
|
2596
|
+
from: time.from,
|
|
2597
|
+
to: time.to,
|
|
2598
|
+
zone: time.zone
|
|
2599
|
+
},
|
|
2600
|
+
moved: time.moved,
|
|
2601
|
+
world,
|
|
2602
|
+
things: [...effects].flatMap(([character, list]) => list.map((effect) => ({
|
|
2603
|
+
character,
|
|
2604
|
+
name: effect.item.name,
|
|
2605
|
+
action: effect.action,
|
|
2606
|
+
summary: effect.summary
|
|
2607
|
+
}))),
|
|
2608
|
+
moves: [...checked].flatMap(([character, check]) => check.ok && check.plan.from !== check.move.to ? [{
|
|
2609
|
+
character,
|
|
2610
|
+
name: decided.get(character)?.report.name ?? character,
|
|
2611
|
+
to: check.to.name,
|
|
2612
|
+
via: Math.max(0, check.plan.via.length - 1)
|
|
2613
|
+
}] : [])
|
|
2614
|
+
};
|
|
2615
|
+
}
|
|
2616
|
+
//#endregion
|
|
2617
|
+
//#region src/host/orchestration/turn-text.ts
|
|
2618
|
+
/**
|
|
2619
|
+
* 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
|
|
2620
|
+
* character either. When the label is exactly a character of the scene, the label is folded into the words that follow it.
|
|
2621
|
+
*/
|
|
2622
|
+
function foldSpeakerLabels(segments, cast) {
|
|
2623
|
+
const found = (label) => {
|
|
2624
|
+
const wanted = label.trim().normalize("NFKC").toLowerCase();
|
|
2625
|
+
return wanted === "" ? void 0 : cast.find((member) => member.key.toLowerCase() === wanted || member.character.name.normalize("NFKC").toLowerCase() === wanted);
|
|
2626
|
+
};
|
|
2627
|
+
const folded = [];
|
|
2628
|
+
for (let index = 0; index < segments.length; index += 1) {
|
|
2629
|
+
const segment = segments[index];
|
|
2630
|
+
const next = segments[index + 1];
|
|
2631
|
+
const speaker = segment.type === "utterance" && (segment.character ?? "") === "" && next?.type === "utterance" && (next.character ?? "") === "" ? found(segment.text) : void 0;
|
|
2632
|
+
if (speaker === void 0) {
|
|
2633
|
+
folded.push(segment);
|
|
2634
|
+
continue;
|
|
2635
|
+
}
|
|
2636
|
+
folded.push({
|
|
2637
|
+
...next,
|
|
2638
|
+
character: speaker.key
|
|
2639
|
+
});
|
|
2640
|
+
index += 1;
|
|
2641
|
+
}
|
|
2642
|
+
return folded;
|
|
2643
|
+
}
|
|
2644
|
+
/** 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. */
|
|
2645
|
+
const SAME_SITUATION = .85;
|
|
2646
|
+
const pieces = (text) => {
|
|
2647
|
+
const found = /* @__PURE__ */ new Set();
|
|
2648
|
+
const chars = [...text];
|
|
2649
|
+
for (let index = 0; index + 3 <= chars.length; index += 1) found.add(chars.slice(index, index + 3).join(""));
|
|
2650
|
+
return found;
|
|
2651
|
+
};
|
|
2652
|
+
/** 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. */
|
|
2653
|
+
function repeatsEarlier(signature, asked) {
|
|
2654
|
+
const [keys = "", text = ""] = signature.split("|");
|
|
2655
|
+
const now = new Set(keys.split(","));
|
|
2656
|
+
const mine = pieces(text);
|
|
2657
|
+
return asked.some((earlier) => {
|
|
2658
|
+
const [before = "", was = ""] = earlier.split("|");
|
|
2659
|
+
if (![...now].every((key) => before.split(",").includes(key))) return false;
|
|
2660
|
+
const theirs = pieces(was);
|
|
2661
|
+
const shared = [...mine].filter((piece) => theirs.has(piece)).length;
|
|
2662
|
+
const union = mine.size + theirs.size - shared;
|
|
2663
|
+
return union === 0 || shared / union >= SAME_SITUATION;
|
|
2664
|
+
});
|
|
2665
|
+
}
|
|
2666
|
+
/** The words as the character said them, whatever quotes the game master put around them. */
|
|
2667
|
+
function asSpoken(segments, spoken) {
|
|
2668
|
+
const left = [...spoken];
|
|
2669
|
+
return segments.map((segment) => {
|
|
2670
|
+
if (segment.type !== "utterance") return segment;
|
|
2671
|
+
const at = left.findIndex((said) => said.character === segment.character && sameWords(said.text, segment.text));
|
|
2672
|
+
return at === -1 ? segment : {
|
|
2673
|
+
...segment,
|
|
2674
|
+
text: left.splice(at, 1)[0].text
|
|
2675
|
+
};
|
|
2676
|
+
});
|
|
2677
|
+
}
|
|
2678
|
+
/**
|
|
2679
|
+
* The output the game master delivered, once it is acceptable: every utterance exactly once and in order, and the narration in the chat's language.
|
|
2680
|
+
* A refusal is thrown for the game master to correct; past `MAX_REFUSALS`, or when `silent`, the output is repaired instead.
|
|
2681
|
+
*/
|
|
2682
|
+
function checkedOutput(narrationChecker, language, state, segments, silent) {
|
|
2683
|
+
const problems = [...segments.flatMap((segment, index) => segment.type === "utterance" && segment.character === "" ? [`segment ${index + 1}: an utterance needs the character who says it`] : []), ...narrationChecker.check(segments, state.utterances)];
|
|
2684
|
+
const wording = wrongLanguage(language, { narration: segments.flatMap((segment) => segment.type === "narration" ? [segment.text] : []).join(" ") });
|
|
2685
|
+
if (problems.length === 0 && wording !== void 0 && !silent && state.refused < 2) {
|
|
2686
|
+
state.refused += 1;
|
|
2687
|
+
throw new Error(`the output is not acceptable: ${wording}. Deliver it again with the narration corrected; utterances may not be touched`);
|
|
2688
|
+
}
|
|
2689
|
+
if (problems.length === 0) return {
|
|
2690
|
+
segments: asSpoken(segments, state.utterances),
|
|
2691
|
+
repaired: false
|
|
2692
|
+
};
|
|
2693
|
+
state.refused += 1;
|
|
2694
|
+
if (!silent && state.refused <= 2) throw new Error(`the output is not acceptable: ${problems.join("; ")}. Deliver it again with these corrected; narration may be written freely, utterances may not be touched`);
|
|
2695
|
+
return {
|
|
2696
|
+
segments: narrationChecker.repair(segments.filter((segment) => segment.type !== "utterance" || segment.character !== ""), state.utterances),
|
|
2697
|
+
repaired: true
|
|
2698
|
+
};
|
|
2699
|
+
}
|
|
2700
|
+
//#endregion
|
|
2701
|
+
//#region src/host/dice/view.ts
|
|
2702
|
+
const sign = (value) => value >= 0 ? `+${value}` : String(value);
|
|
2703
|
+
/** A roll as the player reads it: what it was for, what fell, what it came to and what the world's check made of it. */
|
|
2704
|
+
function rollView(roll) {
|
|
2705
|
+
const kept = roll.kept.length < roll.faces.length ? ` (${roll.kept.join(", ")})` : "";
|
|
2706
|
+
const against = roll.target === void 0 ? "" : ` / ${roll.target}`;
|
|
2707
|
+
const outcome = roll.readout === void 0 ? "" : ` - ${roll.readout.label}${roll.readout.critical ? "!" : ""}`;
|
|
2708
|
+
return {
|
|
2709
|
+
id: roll.id,
|
|
2710
|
+
by: roll.by,
|
|
2711
|
+
text: `${roll.reason}: ${roll.expression} [${roll.faces.join(", ")}]${kept}${roll.modifier === 0 ? "" : ` ${sign(roll.modifier)}`} = ${roll.total}${against}${outcome}`
|
|
2712
|
+
};
|
|
2713
|
+
}
|
|
2714
|
+
/**
|
|
2715
|
+
* What the characters away from the player decide about what fell due, one commitment after another: their activity, and a move to where the commitment was when they
|
|
2716
|
+
* mean to keep it. It is recorded as a round only they perceive, and the keeper writes it.
|
|
2717
|
+
*/
|
|
2718
|
+
async function runOffScene(deps, owner, state, store, due, limit) {
|
|
2719
|
+
const { summary, perCharacter, joint } = store.settings().offstage;
|
|
2720
|
+
let budget = limit;
|
|
2721
|
+
const acted = /* @__PURE__ */ new Map();
|
|
2722
|
+
for (const item of due) {
|
|
2723
|
+
if (budget <= 0) break;
|
|
2724
|
+
const cast = store.cast();
|
|
2725
|
+
const keys = item.characters.filter((key) => cast.some((member) => member.key === key) && (acted.get(key) ?? 0) < perCharacter).slice(0, budget);
|
|
2726
|
+
if (keys.length === 0) continue;
|
|
2727
|
+
budget -= keys.length;
|
|
2728
|
+
for (const key of keys) acted.set(key, (acted.get(key) ?? 0) + 1);
|
|
2729
|
+
const reports = [];
|
|
2730
|
+
for (const group of joint ? keys.map((key) => [key]) : [keys]) {
|
|
2731
|
+
const attending = reports.filter((report) => report.commitment !== "low").map((report) => `${report.name} has decided: ${report.intent}${report.utterance === void 0 ? "" : `, saying "${report.utterance}"`}.`);
|
|
2732
|
+
const asked = {
|
|
2733
|
+
...state,
|
|
2734
|
+
situation: attending.length === 0 ? item.text : `${item.text}\n${attending.join("\n")}`,
|
|
2735
|
+
history: [],
|
|
2736
|
+
recalled: /* @__PURE__ */ new Map()
|
|
2737
|
+
};
|
|
2738
|
+
delete asked.first;
|
|
2739
|
+
delete asked.second;
|
|
2740
|
+
reports.push(...(await deps.ask(group, cast, asked, summary)).reports);
|
|
2741
|
+
}
|
|
2742
|
+
if (reports.length === 0) continue;
|
|
2743
|
+
const where = item.place;
|
|
2744
|
+
const moves = new Map(reports.flatMap((report) => {
|
|
2745
|
+
if (report.commitment === "low" || where === void 0) return [];
|
|
2746
|
+
const check = store.checkMove(report.character, {
|
|
2747
|
+
location: where,
|
|
2748
|
+
ignoreExits: true
|
|
2749
|
+
});
|
|
2750
|
+
return check.ok ? [[report.character, check]] : [];
|
|
2751
|
+
}));
|
|
2752
|
+
store.commit(reports.flatMap((report) => {
|
|
2753
|
+
const member = cast.find((candidate) => candidate.key === report.character);
|
|
2754
|
+
const move = moves.get(report.character);
|
|
2755
|
+
return member === void 0 ? [] : [{
|
|
2756
|
+
character: member.character.id,
|
|
2757
|
+
patch: { activity: report.intent },
|
|
2758
|
+
...move?.ok === true ? { move: move.move } : {}
|
|
2759
|
+
}];
|
|
2760
|
+
}));
|
|
2761
|
+
state.offstage += 1;
|
|
2762
|
+
const time = store.time(owner.turn);
|
|
2763
|
+
store.queueCommit({
|
|
2764
|
+
turn: owner.turn,
|
|
2765
|
+
round: 100 + state.offstage,
|
|
2766
|
+
situation: item.text,
|
|
2767
|
+
offstage: true,
|
|
2768
|
+
time: {
|
|
2769
|
+
from: time.now,
|
|
2770
|
+
to: time.now,
|
|
2771
|
+
zone: time.zone
|
|
2772
|
+
},
|
|
2773
|
+
entries: reports.map((report) => {
|
|
2774
|
+
const at = cast.find((candidate) => candidate.key === report.character)?.place?.location;
|
|
2775
|
+
const move = moves.get(report.character);
|
|
2776
|
+
return {
|
|
2777
|
+
character: report.character,
|
|
2778
|
+
name: report.name,
|
|
2779
|
+
intent: report.intent,
|
|
2780
|
+
commitment: report.commitment,
|
|
2781
|
+
result: "succeeded",
|
|
2782
|
+
summary: report.intent,
|
|
2783
|
+
spoken: false,
|
|
2784
|
+
...at === void 0 ? {} : { at: {
|
|
2785
|
+
key: locationKey(at),
|
|
2786
|
+
name: at.name
|
|
2787
|
+
} },
|
|
2788
|
+
...move?.ok === true && move.plan.from !== move.move.to ? { moved: { ...move.to } } : {}
|
|
2789
|
+
};
|
|
2790
|
+
})
|
|
2791
|
+
});
|
|
2792
|
+
store.markHandled([item.key]);
|
|
2793
|
+
deps.write();
|
|
2794
|
+
if (summary && deps.stillThis()) {
|
|
2795
|
+
const lines = reports.flatMap((report) => report.account === void 0 ? [] : [report.account]);
|
|
2796
|
+
store.addNote("away", owner.turn, lines);
|
|
2797
|
+
}
|
|
2798
|
+
}
|
|
2799
|
+
}
|
|
2800
|
+
//#endregion
|
|
3387
2801
|
//#region src/host/orchestration/turn-engine.ts
|
|
3388
2802
|
const MAX_OBJECTIONS = 3;
|
|
3389
2803
|
/** The most a single round may be said to take: a day. A longer time passing is skipped with `advanceTime`. */
|
|
3390
2804
|
const MAX_ROUND_SECONDS = 86400;
|
|
3391
|
-
/** The game master is asked to correct its output this many times before it is corrected for it. */
|
|
3392
|
-
const MAX_REFUSALS = 2;
|
|
3393
2805
|
/** 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. */
|
|
3394
2806
|
const MAX_FAILED_RUNS = 3;
|
|
3395
2807
|
/**
|
|
@@ -3465,7 +2877,7 @@ var TurnEngine = class {
|
|
|
3465
2877
|
if (state.ooc.ruling !== void 0) throw new Error("this question was already answered");
|
|
3466
2878
|
state.ooc.ruling = {
|
|
3467
2879
|
legal: true,
|
|
3468
|
-
note: text.length > 1200 ? `${text.slice(0,
|
|
2880
|
+
note: text.length > 1200 ? `${text.slice(0, MAX_OOC_ANSWER - 1)}…` : text,
|
|
3469
2881
|
question: true
|
|
3470
2882
|
};
|
|
3471
2883
|
state.phase = "between";
|
|
@@ -3536,7 +2948,7 @@ var TurnEngine = class {
|
|
|
3536
2948
|
* 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.
|
|
3537
2949
|
* @throws {Error} when the turn is not an ordinary one, was already judged, the reason is empty, or `at` is not a place.
|
|
3538
2950
|
*/
|
|
3539
|
-
judge(owner, allowed, reason, at) {
|
|
2951
|
+
judge(owner, allowed, reason, at, uncertain) {
|
|
3540
2952
|
const state = this.stateFor(owner);
|
|
3541
2953
|
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"));
|
|
3542
2954
|
if (state.phase !== "idle") throw new Error("the action is judged first, before anything else");
|
|
@@ -3565,6 +2977,14 @@ var TurnEngine = class {
|
|
|
3565
2977
|
allowed,
|
|
3566
2978
|
reason: why
|
|
3567
2979
|
};
|
|
2980
|
+
const { tools } = this.storeOf(owner).settings();
|
|
2981
|
+
if (allowed && uncertain === true && (toolEnabled(tools, "gameMaster", "roll_check") || toolEnabled(tools, "gameMaster", "roll_dice"))) {
|
|
2982
|
+
state.rollWanted = true;
|
|
2983
|
+
return {
|
|
2984
|
+
...state.judgement,
|
|
2985
|
+
roll: true
|
|
2986
|
+
};
|
|
2987
|
+
}
|
|
3568
2988
|
return state.judgement;
|
|
3569
2989
|
}
|
|
3570
2990
|
/** The turn is an ordinary one: the game master judges the player's action before anything else. */
|
|
@@ -3586,6 +3006,7 @@ var TurnEngine = class {
|
|
|
3586
3006
|
if (judgeDue(state)) throw new Error(explainPhase(state, "activate_characters"));
|
|
3587
3007
|
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`);
|
|
3588
3008
|
if (previewDue(state)) throw new Error(explainPhase(state, "activate_characters"));
|
|
3009
|
+
if (rollDue(state, this.storeOf(owner))) throw new Error(ROLL_FIRST);
|
|
3589
3010
|
const store = this.storeOf(owner);
|
|
3590
3011
|
const limit = store.settings().maxRounds;
|
|
3591
3012
|
if (state.rounds >= limit) throw new Error(`${state.rounds} round${state.rounds === 1 ? "" : "s"} of characters were already played for this message, which is the most this chat allows: deliver the output with narrate`);
|
|
@@ -3607,7 +3028,7 @@ var TurnEngine = class {
|
|
|
3607
3028
|
state.cues.clear();
|
|
3608
3029
|
delete state.first;
|
|
3609
3030
|
delete state.second;
|
|
3610
|
-
const attempt = await this.
|
|
3031
|
+
const attempt = await this.ask(owner, 1, keys, cast, state, run, signal);
|
|
3611
3032
|
if (attempt.reports.length === 0) return {
|
|
3612
3033
|
attempt: 1,
|
|
3613
3034
|
reports: [],
|
|
@@ -3633,13 +3054,14 @@ var TurnEngine = class {
|
|
|
3633
3054
|
if (state.ooc !== void 0) return { done: this.decree(owner, state, outcomes, elapsedSeconds) };
|
|
3634
3055
|
if (state.phase === "idle" || state.phase === "between") {
|
|
3635
3056
|
if (judgeDue(state) || previewDue(state)) throw new Error(explainPhase(state, "resolve_round"));
|
|
3057
|
+
if (rollDue(state, this.storeOf(owner))) throw new Error(ROLL_FIRST);
|
|
3636
3058
|
const player = store.cast().find((member) => member.character.control === "player");
|
|
3637
3059
|
if (player !== void 0 && outcomes.length === 1 && outcomes[0]?.character === player.key) return { done: this.resolvePlayer(owner, state, outcomes[0], elapsedSeconds) };
|
|
3638
3060
|
}
|
|
3639
3061
|
if (state.phase !== "resolving" && state.phase !== "retrying") throw new Error(explainPhase(state, "resolve_round"));
|
|
3640
3062
|
const attempt = state.phase === "resolving" ? state.first : state.second;
|
|
3641
3063
|
if (attempt === void 0) throw new Error("internal: no round to resolve");
|
|
3642
|
-
checkOutcomes(attempt, outcomes, store, state.phase === "resolving" ? this.playerKey(state) : void 0);
|
|
3064
|
+
checkOutcomes(attempt, outcomes, store, state.phase === "resolving" ? this.playerKey(state) : void 0, void 0, state);
|
|
3643
3065
|
if (elapsedSeconds !== void 0) state.elapsed = this.elapsedChecked(owner, elapsedSeconds);
|
|
3644
3066
|
attempt.outcomes = [...outcomes];
|
|
3645
3067
|
if (state.phase === "resolving") {
|
|
@@ -3648,7 +3070,7 @@ var TurnEngine = class {
|
|
|
3648
3070
|
outcomes: attempt.outcomes
|
|
3649
3071
|
}).filter((key) => attempt.reports.some((report) => report.character === key));
|
|
3650
3072
|
if (retrying.length > 0) {
|
|
3651
|
-
const second = await this.
|
|
3073
|
+
const second = await this.ask(owner, 2, retrying, state.cast, state, run, signal);
|
|
3652
3074
|
state.second = second;
|
|
3653
3075
|
if (second.reports.length > 0) {
|
|
3654
3076
|
state.phase = "retrying";
|
|
@@ -3660,7 +3082,7 @@ var TurnEngine = class {
|
|
|
3660
3082
|
}
|
|
3661
3083
|
}
|
|
3662
3084
|
}
|
|
3663
|
-
return { done: this.
|
|
3085
|
+
return { done: recordRound(this.storeOf(owner), owner, state, (round) => this.fire(owner, round)) };
|
|
3664
3086
|
}
|
|
3665
3087
|
/**
|
|
3666
3088
|
* What an out-of-character command decrees, as one round of its own: outcomes for any characters, asked or not, each checked like any other
|
|
@@ -3679,7 +3101,7 @@ var TurnEngine = class {
|
|
|
3679
3101
|
checkOutcomes({
|
|
3680
3102
|
reports: [],
|
|
3681
3103
|
failures: []
|
|
3682
|
-
}, outcomes, store, void 0, state.cast);
|
|
3104
|
+
}, outcomes, store, void 0, state.cast, state);
|
|
3683
3105
|
if (elapsedSeconds !== void 0) state.elapsed = this.elapsedChecked(owner, elapsedSeconds);
|
|
3684
3106
|
state.situation = `By the player's out-of-character command (${state.ooc?.request ?? ""}), the game master decreed:`;
|
|
3685
3107
|
state.recalled.clear();
|
|
@@ -3698,7 +3120,7 @@ var TurnEngine = class {
|
|
|
3698
3120
|
failures: [],
|
|
3699
3121
|
outcomes: [...outcomes]
|
|
3700
3122
|
};
|
|
3701
|
-
return this.
|
|
3123
|
+
return recordRound(this.storeOf(owner), owner, state, (round) => this.fire(owner, round));
|
|
3702
3124
|
}
|
|
3703
3125
|
/** The key of the player's character. */
|
|
3704
3126
|
playerKey(state) {
|
|
@@ -3718,7 +3140,7 @@ var TurnEngine = class {
|
|
|
3718
3140
|
checkOutcomes({
|
|
3719
3141
|
reports: [],
|
|
3720
3142
|
failures: []
|
|
3721
|
-
}, [outcome], store, outcome.character);
|
|
3143
|
+
}, [outcome], store, outcome.character, void 0, state);
|
|
3722
3144
|
if (elapsedSeconds !== void 0) state.elapsed = this.elapsedChecked(owner, elapsedSeconds);
|
|
3723
3145
|
state.situation = `${player?.character.name ?? outcome.character}: ${outcome.summary}`;
|
|
3724
3146
|
state.recalled.clear();
|
|
@@ -3728,7 +3150,7 @@ var TurnEngine = class {
|
|
|
3728
3150
|
failures: [],
|
|
3729
3151
|
outcomes: [outcome]
|
|
3730
3152
|
};
|
|
3731
|
-
return this.
|
|
3153
|
+
return recordRound(this.storeOf(owner), owner, state, (round) => this.fire(owner, round));
|
|
3732
3154
|
}
|
|
3733
3155
|
/**
|
|
3734
3156
|
* The game master shows the player how the scene opens, before anyone is asked: something to read while the rest of the turn is worked out.
|
|
@@ -3930,7 +3352,7 @@ var TurnEngine = class {
|
|
|
3930
3352
|
}
|
|
3931
3353
|
runOffScene(owner, state, store, due, limit, run) {
|
|
3932
3354
|
return runOffScene({
|
|
3933
|
-
ask: (group, cast, asked, summary) => this.
|
|
3355
|
+
ask: (group, cast, asked, summary) => this.ask(owner, 1, group, cast, asked, run, state.cancel.signal, summary),
|
|
3934
3356
|
write: () => {
|
|
3935
3357
|
this.write(owner, state);
|
|
3936
3358
|
},
|
|
@@ -4060,6 +3482,8 @@ var TurnEngine = class {
|
|
|
4060
3482
|
reports: [],
|
|
4061
3483
|
outcomes: [],
|
|
4062
3484
|
utterances: [],
|
|
3485
|
+
cited: /* @__PURE__ */ new Set(),
|
|
3486
|
+
reported: /* @__PURE__ */ new Set(),
|
|
4063
3487
|
refused: 0,
|
|
4064
3488
|
previewed: false,
|
|
4065
3489
|
previewWanted: this.stores(chat)?.settings().preview.enabled ?? false,
|
|
@@ -4079,236 +3503,15 @@ var TurnEngine = class {
|
|
|
4079
3503
|
if (store === void 0) throw new Error("this chat is not a roleplay session");
|
|
4080
3504
|
return store;
|
|
4081
3505
|
}
|
|
4082
|
-
|
|
4083
|
-
|
|
4084
|
-
|
|
4085
|
-
|
|
4086
|
-
|
|
4087
|
-
|
|
4088
|
-
|
|
4089
|
-
const answers = await Promise.all(keys.map(async (key) => {
|
|
4090
|
-
const member = cast.find((candidate) => candidate.key === key);
|
|
4091
|
-
if (member === void 0) return {
|
|
4092
|
-
character: key,
|
|
4093
|
-
name: key,
|
|
4094
|
-
reason: "no such character"
|
|
4095
|
-
};
|
|
4096
|
-
const previous = number === 2 ? state.first?.reports.find((report) => report.character === key) : void 0;
|
|
4097
|
-
const memories = number === 1 && store !== void 0 ? await this.boundaries().recaller.recall({
|
|
4098
|
-
member,
|
|
4099
|
-
situation: state.situation,
|
|
4100
|
-
turn: owner.turn,
|
|
4101
|
-
policy: store.settings().memory,
|
|
4102
|
-
...store.settings().diagnostics.keepRuns ? { explain: true } : {},
|
|
4103
|
-
...queryVector === void 0 ? {} : { queryVector }
|
|
4104
|
-
}, store.story, signal) : state.recalled.get(key) ?? [];
|
|
4105
|
-
if (number === 1 && memories.length > 0) state.recalled.set(key, memories);
|
|
4106
|
-
const place = member.place === void 0 ? void 0 : store?.place(member.place.location.id);
|
|
4107
|
-
const relationships = store === void 0 ? [] : knownRelationships(member, place === void 0 ? cast : place.here, state.situation, cast, store);
|
|
4108
|
-
const carrying = store === void 0 ? [] : store.carried(member.character.id);
|
|
4109
|
-
const knowledge = store?.knowledge(member.character.id, owner.turn, (place === void 0 ? cast : place.here).map((entry) => entry.character.id));
|
|
4110
|
-
const affiliations = store?.affiliations(member.character.id) ?? [];
|
|
4111
|
-
const agenda = store?.agenda(owner.turn, member.character.id);
|
|
4112
|
-
const cues = cuesOf(state, key, member, store, number);
|
|
4113
|
-
const upcoming = store?.upcomingFor(member.character.id, owner.turn) ?? [];
|
|
4114
|
-
const tracked = store?.trackedFor(member.character.id, (place === void 0 ? cast : place.here).map((entry) => entry.character.id));
|
|
4115
|
-
const request = {
|
|
4116
|
-
member,
|
|
4117
|
-
cast,
|
|
4118
|
-
situation: state.situation,
|
|
4119
|
-
attempt: number,
|
|
4120
|
-
language: store?.settings().prompts.language ?? "en",
|
|
4121
|
-
turn: owner.turn,
|
|
4122
|
-
...away ? { away: true } : {},
|
|
4123
|
-
lookup: store?.settings().memory.lookup ?? false,
|
|
4124
|
-
text: (id, variables) => this.storeOf(owner).text(id, variables),
|
|
4125
|
-
...time === void 0 ? {} : { time },
|
|
4126
|
-
...agenda === void 0 ? {} : { agenda },
|
|
4127
|
-
...cues.length === 0 ? {} : { cues },
|
|
4128
|
-
...upcoming.length === 0 ? {} : { upcoming },
|
|
4129
|
-
...tracked === void 0 || tracked.self === "" && tracked.others.length === 0 ? {} : { tracked },
|
|
4130
|
-
...place === void 0 ? {} : { place },
|
|
4131
|
-
...relationships.length === 0 ? {} : { relationships },
|
|
4132
|
-
...carrying.length === 0 ? {} : { carrying },
|
|
4133
|
-
...knowledge === void 0 ? {} : { knowledge },
|
|
4134
|
-
...affiliations.length === 0 ? {} : { affiliations },
|
|
4135
|
-
...memories.length === 0 ? {} : { memories },
|
|
4136
|
-
...state.history.length === 0 ? {} : { earlier: [...state.history] },
|
|
4137
|
-
...reaction === void 0 ? {} : { reaction },
|
|
4138
|
-
...previous === void 0 ? {} : { previous: {
|
|
4139
|
-
intent: previous.intent,
|
|
4140
|
-
commitment: previous.commitment
|
|
4141
|
-
} }
|
|
4142
|
-
};
|
|
4143
|
-
try {
|
|
4144
|
-
const action = await run(request, signal);
|
|
4145
|
-
const verdict = outputValidator.validate(action, request);
|
|
4146
|
-
if (!verdict.ok) return {
|
|
4147
|
-
character: key,
|
|
4148
|
-
name: member.character.name,
|
|
4149
|
-
reason: verdict.reason
|
|
4150
|
-
};
|
|
4151
|
-
return {
|
|
4152
|
-
character: key,
|
|
4153
|
-
name: member.character.name,
|
|
4154
|
-
...action
|
|
4155
|
-
};
|
|
4156
|
-
} catch (error) {
|
|
4157
|
-
if (signal.aborted) throw error;
|
|
4158
|
-
const reason = error instanceof Error ? error.message : String(error);
|
|
4159
|
-
this.problem(`${member.character.name} could not act: ${reason}`);
|
|
4160
|
-
return {
|
|
4161
|
-
character: key,
|
|
4162
|
-
name: member.character.name,
|
|
4163
|
-
reason,
|
|
4164
|
-
technical: true
|
|
4165
|
-
};
|
|
3506
|
+
ask(owner, number, keys, cast, state, run, signal, away = false) {
|
|
3507
|
+
return askCharacters({
|
|
3508
|
+
boundaries: this.boundaries(),
|
|
3509
|
+
store: this.stores(owner.chat),
|
|
3510
|
+
storeOf: (o) => this.storeOf(o),
|
|
3511
|
+
problem: (message) => {
|
|
3512
|
+
this.problem(message);
|
|
4166
3513
|
}
|
|
4167
|
-
})
|
|
4168
|
-
return {
|
|
4169
|
-
reports: answers.filter((answer) => "intent" in answer),
|
|
4170
|
-
failures: answers.filter((answer) => "reason" in answer)
|
|
4171
|
-
};
|
|
4172
|
-
}
|
|
4173
|
-
finishRound(owner, state) {
|
|
4174
|
-
const store = this.storeOf(owner);
|
|
4175
|
-
const decided = decisions(state);
|
|
4176
|
-
const limit = store.settings().maxRounds;
|
|
4177
|
-
const round = state.rounds + 1;
|
|
4178
|
-
const checked = new Map([...decided.values()].flatMap(({ outcome }) => outcome.moveTo === void 0 ? [] : [[outcome.character, store.checkMove(outcome.character, outcome.moveTo)]]));
|
|
4179
|
-
const refused = [...checked].flatMap(([character, check]) => check.ok ? [] : [`${character}: ${check.problems.join("; ")}`]);
|
|
4180
|
-
if (refused.length > 0) throw new Error(`a move is no longer possible: ${refused.join("; ")}`);
|
|
4181
|
-
const spoken = [...decided.values()].flatMap(({ outcome, report }) => report.utterance === void 0 || outcome.spoken === false || state.utterances.some((said) => said.character === report.character && sameWords(said.text, report.utterance)) ? [] : [{
|
|
4182
|
-
character: report.character,
|
|
4183
|
-
name: report.name,
|
|
4184
|
-
text: report.utterance,
|
|
4185
|
-
round
|
|
4186
|
-
}]);
|
|
4187
|
-
state.rounds = round;
|
|
4188
|
-
state.situations.push(state.situation);
|
|
4189
|
-
state.utterances.push(...spoken);
|
|
4190
|
-
for (const { outcome, report } of decided.values()) {
|
|
4191
|
-
state.reports.push(report);
|
|
4192
|
-
state.outcomes.push(outcome);
|
|
4193
|
-
state.history.push({
|
|
4194
|
-
round,
|
|
4195
|
-
character: report.character,
|
|
4196
|
-
name: report.name,
|
|
4197
|
-
intent: report.intent,
|
|
4198
|
-
...report.utterance === void 0 ? {} : { utterance: report.utterance },
|
|
4199
|
-
result: outcome.result,
|
|
4200
|
-
summary: outcome.summary
|
|
4201
|
-
});
|
|
4202
|
-
}
|
|
4203
|
-
state.limit = limit;
|
|
4204
|
-
state.phase = "between";
|
|
4205
|
-
const playerPlace = state.cast.find((member) => member.character.control === "player")?.place?.location;
|
|
4206
|
-
const updates = [...decided.values()].flatMap(({ outcome, report }) => {
|
|
4207
|
-
const member = state.cast.find((candidate) => candidate.key === report.character);
|
|
4208
|
-
if (member === void 0) return [];
|
|
4209
|
-
const check = checked.get(outcome.character);
|
|
4210
|
-
return [{
|
|
4211
|
-
key: report.character,
|
|
4212
|
-
update: {
|
|
4213
|
-
character: member.character.id,
|
|
4214
|
-
patch: {
|
|
4215
|
-
activity: outcome.summary,
|
|
4216
|
-
...outcome.publicState === void 0 || outcome.publicState.trim() === "" ? {} : { publicState: outcome.publicState.trim() }
|
|
4217
|
-
},
|
|
4218
|
-
...check?.ok === true ? { move: check.move } : {},
|
|
4219
|
-
...outcome.itemChanges === void 0 || outcome.itemChanges.length === 0 ? {} : { items: outcome.itemChanges }
|
|
4220
|
-
}
|
|
4221
|
-
}];
|
|
4222
|
-
});
|
|
4223
|
-
const effects = /* @__PURE__ */ new Map();
|
|
4224
|
-
const done = store.commit(updates.map((entry) => entry.update));
|
|
4225
|
-
updates.forEach(({ key }, index) => {
|
|
4226
|
-
effects.set(key, done[index] ?? []);
|
|
4227
|
-
});
|
|
4228
|
-
const tracked = new Map(updates.map(({ key }) => [key, store.changeVariables(owner.turn, decided.get(key)?.outcome.variableChanges ?? []).map((effect) => ({
|
|
4229
|
-
subject: effect.subjectName,
|
|
4230
|
-
name: effect.variable.name,
|
|
4231
|
-
from: describeValue(effect.variable, effect.from),
|
|
4232
|
-
to: describeValue(effect.variable, effect.to)
|
|
4233
|
-
}))]));
|
|
4234
|
-
const journey = [...checked].flatMap(([character, check]) => check.ok && state.cast.find((candidate) => candidate.key === character)?.character.control === "player" ? [check.plan.travelSeconds ?? 0] : [])[0];
|
|
4235
|
-
const time = store.endRound(owner.turn, round, state.elapsed, journey);
|
|
4236
|
-
delete state.elapsed;
|
|
4237
|
-
const world = this.fire(owner, round);
|
|
4238
|
-
store.queueCommit({
|
|
4239
|
-
turn: owner.turn,
|
|
4240
|
-
round,
|
|
4241
|
-
situation: state.situation,
|
|
4242
|
-
...playerPlace === void 0 ? {} : { place: {
|
|
4243
|
-
key: locationKey(playerPlace),
|
|
4244
|
-
name: playerPlace.name
|
|
4245
|
-
} },
|
|
4246
|
-
time: {
|
|
4247
|
-
from: time.from,
|
|
4248
|
-
to: time.to,
|
|
4249
|
-
zone: time.zone
|
|
4250
|
-
},
|
|
4251
|
-
entries: [...decided.values()].map(({ outcome, report }) => {
|
|
4252
|
-
const at = state.cast.find((candidate) => candidate.key === report.character)?.place?.location;
|
|
4253
|
-
const check = checked.get(outcome.character);
|
|
4254
|
-
return {
|
|
4255
|
-
character: report.character,
|
|
4256
|
-
name: report.name,
|
|
4257
|
-
intent: report.intent,
|
|
4258
|
-
commitment: report.commitment,
|
|
4259
|
-
...report.utterance === void 0 ? {} : { utterance: report.utterance },
|
|
4260
|
-
result: outcome.result,
|
|
4261
|
-
summary: outcome.summary,
|
|
4262
|
-
...outcome.publicState === void 0 || outcome.publicState.trim() === "" ? {} : { publicState: outcome.publicState.trim() },
|
|
4263
|
-
spoken: outcome.spoken !== false,
|
|
4264
|
-
...(tracked.get(report.character) ?? []).length === 0 ? {} : { variables: tracked.get(report.character) },
|
|
4265
|
-
...state.cast.find((candidate) => candidate.key === report.character)?.character.control === "player" ? { player: true } : {},
|
|
4266
|
-
...report.forced === true ? { forced: true } : {},
|
|
4267
|
-
...(effects.get(report.character) ?? []).length === 0 ? {} : { items: (effects.get(report.character) ?? []).map((effect) => ({
|
|
4268
|
-
name: effect.item.name,
|
|
4269
|
-
action: effect.action,
|
|
4270
|
-
summary: effect.summary
|
|
4271
|
-
})) },
|
|
4272
|
-
...at === void 0 ? {} : { at: {
|
|
4273
|
-
key: locationKey(at),
|
|
4274
|
-
name: at.name
|
|
4275
|
-
} },
|
|
4276
|
-
...check?.ok === true && check.plan.from !== check.move.to ? { moved: {
|
|
4277
|
-
...check.to,
|
|
4278
|
-
...check.position === void 0 ? {} : { position: check.position }
|
|
4279
|
-
} } : {}
|
|
4280
|
-
};
|
|
4281
|
-
})
|
|
4282
|
-
});
|
|
4283
|
-
return {
|
|
4284
|
-
round,
|
|
4285
|
-
limit,
|
|
4286
|
-
updated: [...decided.keys()],
|
|
4287
|
-
retryFailures: state.second?.failures ?? [],
|
|
4288
|
-
utterances: spoken,
|
|
4289
|
-
spoken: [...state.utterances],
|
|
4290
|
-
waiting: store.pendingCommits().length,
|
|
4291
|
-
variables: [...tracked.values()].flat().map((change) => `${change.subject}'s ${change.name} is now ${change.to} (it was ${change.from})`),
|
|
4292
|
-
time: {
|
|
4293
|
-
from: time.from,
|
|
4294
|
-
to: time.to,
|
|
4295
|
-
zone: time.zone
|
|
4296
|
-
},
|
|
4297
|
-
moved: time.moved,
|
|
4298
|
-
world,
|
|
4299
|
-
things: [...effects].flatMap(([character, list]) => list.map((effect) => ({
|
|
4300
|
-
character,
|
|
4301
|
-
name: effect.item.name,
|
|
4302
|
-
action: effect.action,
|
|
4303
|
-
summary: effect.summary
|
|
4304
|
-
}))),
|
|
4305
|
-
moves: [...checked].flatMap(([character, check]) => check.ok && check.plan.from !== check.move.to ? [{
|
|
4306
|
-
character,
|
|
4307
|
-
name: decided.get(character)?.report.name ?? character,
|
|
4308
|
-
to: check.to.name,
|
|
4309
|
-
via: Math.max(0, check.plan.via.length - 1)
|
|
4310
|
-
}] : [])
|
|
4311
|
-
};
|
|
3514
|
+
}, owner, number, keys, cast, state, run, signal, away);
|
|
4312
3515
|
}
|
|
4313
3516
|
/**
|
|
4314
3517
|
* The game master asks for what happened to be written into the story's lasting records. The memory
|
|
@@ -4398,30 +3601,17 @@ var TurnEngine = class {
|
|
|
4398
3601
|
if ((this.runs.get(owner.chat) ?? 0) === 0 && store !== void 0 && waiting > 0 && (state.requested || waiting >= store.settings().memory.backlogRounds)) this.write(owner, state);
|
|
4399
3602
|
}
|
|
4400
3603
|
async deliver(owner, state, segments, silent, run) {
|
|
4401
|
-
|
|
4402
|
-
const problems = [...segments.flatMap((segment, index) => segment.type === "utterance" && segment.character === "" ? [`segment ${index + 1}: an utterance needs the character who says it`] : []), ...narrationChecker.check(segments, state.utterances)];
|
|
4403
|
-
const wording = wrongLanguage(this.storeOf(owner).settings().prompts.language, { narration: segments.flatMap((segment) => segment.type === "narration" ? [segment.text] : []).join(" ") });
|
|
4404
|
-
if (problems.length === 0 && wording !== void 0 && !silent && state.refused < MAX_REFUSALS) {
|
|
4405
|
-
state.refused += 1;
|
|
4406
|
-
throw new Error(`the output is not acceptable: ${wording}. Deliver it again with the narration corrected; utterances may not be touched`);
|
|
4407
|
-
}
|
|
4408
|
-
let output;
|
|
4409
|
-
if (problems.length === 0) output = {
|
|
4410
|
-
segments: asSpoken(segments, state.utterances),
|
|
4411
|
-
repaired: false
|
|
4412
|
-
};
|
|
4413
|
-
else {
|
|
4414
|
-
state.refused += 1;
|
|
4415
|
-
if (!silent && state.refused <= MAX_REFUSALS) throw new Error(`the output is not acceptable: ${problems.join("; ")}. Deliver it again with these corrected; narration may be written freely, utterances may not be touched`);
|
|
4416
|
-
output = {
|
|
4417
|
-
segments: narrationChecker.repair(segments.filter((segment) => segment.type !== "utterance" || segment.character !== ""), state.utterances),
|
|
4418
|
-
repaired: true
|
|
4419
|
-
};
|
|
4420
|
-
}
|
|
3604
|
+
let output = checkedOutput(this.boundaries().narrationChecker, this.storeOf(owner).settings().prompts.language, state, segments, silent);
|
|
4421
3605
|
if (state.ooc?.ruling !== void 0) output = {
|
|
4422
3606
|
...output,
|
|
4423
3607
|
ooc: state.ooc.ruling
|
|
4424
3608
|
};
|
|
3609
|
+
const store = this.storeOf(owner);
|
|
3610
|
+
const rolled = store.settings().dice.visibility === "card" ? store.story.dice.rolls({ turn: owner.turn }) : [];
|
|
3611
|
+
if (rolled.length > 0) output = {
|
|
3612
|
+
...output,
|
|
3613
|
+
rolls: rolled.map(rollView)
|
|
3614
|
+
};
|
|
4425
3615
|
this.storeOf(owner).completeTurn();
|
|
4426
3616
|
this.storeOf(owner).resolveBeats(state.addressed);
|
|
4427
3617
|
state.phase = "done";
|
|
@@ -4724,6 +3914,23 @@ function apply(ctx) {
|
|
|
4724
3914
|
settings: liveSettings(sessionFolder(sessionsDir(), session), problem)
|
|
4725
3915
|
};
|
|
4726
3916
|
};
|
|
3917
|
+
/** What the game master is told of the dice, when it has a dice tool; nothing when it has none. */
|
|
3918
|
+
const diceSection = (context, chat, language) => {
|
|
3919
|
+
const chatId = context.agent?.session.id;
|
|
3920
|
+
const store = chatId === void 0 ? void 0 : stores(chatId);
|
|
3921
|
+
if (chat === void 0 || store === void 0) return "";
|
|
3922
|
+
const on = [
|
|
3923
|
+
"roll_check",
|
|
3924
|
+
"roll_dice",
|
|
3925
|
+
"list_checks"
|
|
3926
|
+
].filter((tool) => toolEnabled(chat.settings.tools, "gameMaster", tool));
|
|
3927
|
+
if (on.length === 0) return "";
|
|
3928
|
+
const checks = on.includes("roll_check") ? describeChecks(store.story.dice.checks(), translator(languageOf(language))) : "";
|
|
3929
|
+
return ctx.roleplayPrompts.text("roleplay/dice", language, {
|
|
3930
|
+
tools: on.join(", "),
|
|
3931
|
+
checks
|
|
3932
|
+
}, chat.session);
|
|
3933
|
+
};
|
|
4727
3934
|
for (const [name, order, id] of [[
|
|
4728
3935
|
PERSONA_SECTION,
|
|
4729
3936
|
ctx.systemPrompt.getSectionOrder("DEPLOYMENT_PERSONA_PREFIX"),
|
|
@@ -4740,10 +3947,22 @@ function apply(ctx) {
|
|
|
4740
3947
|
const chat = settingsOf(context);
|
|
4741
3948
|
const language = chat?.settings.prompts.language ?? "en";
|
|
4742
3949
|
const part = (name) => ctx.roleplayPrompts.text(name, language, {}, chat?.session);
|
|
4743
|
-
const variables = id === "roleplay/game-master" ? {
|
|
3950
|
+
const variables = id === "roleplay/game-master" ? {
|
|
3951
|
+
preview: chat?.settings.preview.enabled === false ? "" : part("roleplay/preview"),
|
|
3952
|
+
dice: diceSection(context, chat, language)
|
|
3953
|
+
} : id === "roleplay/persona" ? { style: part("roleplay/style") } : {};
|
|
4744
3954
|
return ctx.roleplayPrompts.text(id, language, variables, chat?.session);
|
|
4745
3955
|
}
|
|
4746
3956
|
});
|
|
3957
|
+
ctx.systemPrompt.section({
|
|
3958
|
+
name: RESOURCE_HINT_SECTION,
|
|
3959
|
+
order: ctx.systemPrompt.getSectionOrder("MCP_SERVERS"),
|
|
3960
|
+
interpolate: false,
|
|
3961
|
+
text: (context) => {
|
|
3962
|
+
const chat = settingsOf(context);
|
|
3963
|
+
return resourceHint(ctx.tools.schemas(context.scope).map((tool) => tool.name), ctx.tools.schemas().map((tool) => tool.name), translator(languageOf(chat?.settings.prompts.language ?? "en")));
|
|
3964
|
+
}
|
|
3965
|
+
});
|
|
4747
3966
|
const [previewTool, ...tools] = [...createTools$1({
|
|
4748
3967
|
engine,
|
|
4749
3968
|
boundaries,
|
|
@@ -4814,12 +4033,21 @@ function apply(ctx) {
|
|
|
4814
4033
|
ctx.roleplayOrchestration.images = void 0;
|
|
4815
4034
|
};
|
|
4816
4035
|
}, "roleplay: images");
|
|
4036
|
+
const diceLink = (chat) => {
|
|
4037
|
+
const store = stores(chat);
|
|
4038
|
+
const agent = ctx.agents.get(SessionId(chat));
|
|
4039
|
+
return store === void 0 || agent === void 0 ? void 0 : {
|
|
4040
|
+
session: store.story,
|
|
4041
|
+
turn: currentTurn(agent)
|
|
4042
|
+
};
|
|
4043
|
+
};
|
|
4817
4044
|
installHooks(ctx, {
|
|
4818
4045
|
engine,
|
|
4819
4046
|
boundaries,
|
|
4820
4047
|
stores,
|
|
4821
4048
|
timelines,
|
|
4822
|
-
previewTool
|
|
4049
|
+
previewTool,
|
|
4050
|
+
optionalTools: createDiceTools(diceLink)
|
|
4823
4051
|
});
|
|
4824
4052
|
}
|
|
4825
4053
|
//#endregion
|