@dsh-agentic-roleplay/core 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/{chat-AKrVcWIC.js → access-EbNdZFbk.js} +10 -2
- package/lib/authoring.js +193 -227
- package/lib/client.js +2710 -1322
- package/lib/client.js.map +1 -1
- package/lib/compaction.js +2 -3
- package/lib/deliver-BDW5WZvc.js +2262 -0
- package/lib/{documents-Bu5uYRZB.js → documents-DtlY2IRt.js} +958 -10
- package/lib/{gallery-DUeBMHQX.js → gallery-DACG5eh2.js} +2 -2
- package/lib/{generator-HPifO3Kk.js → generator-DGYcLMp7.js} +2 -2
- package/lib/images.js +3 -3
- package/lib/index.js +419 -23
- package/lib/{narration-BRyHU0H2.js → narration-5ig7Dg28.js} +12 -2
- package/lib/orchestration.js +458 -342
- package/lib/{packs-ByP1J-zj.js → packs-CkRZDCsV.js} +51 -5
- package/lib/{presenter-agent-G43ODV8I.js → presenter-agent-PK9JYExk.js} +4 -4
- package/package.json +5 -4
- package/packs/scene/map.html +12 -0
- package/packs/scene/map.js +55 -0
- package/packs/scene/pack.json +11 -0
- package/packs/scene/strip.html +11 -0
- package/packs/scene/strip.js +26 -0
- package/packs/scene/style.css +16 -0
- package/prompts/en/roleplay/character-decide.md +1 -1
- package/prompts/en/roleplay/character-rules.md +1 -1
- package/prompts/en/roleplay/chronicler.md +1 -1
- package/prompts/en/roleplay/game-master.md +1 -1
- package/prompts/en/roleplay/reflection.md +9 -0
- package/prompts/en/roleplay/style.md +1 -1
- package/prompts/en/roleplay/suggester.md +1 -1
- package/prompts/en/translate/document.md +6 -0
- package/prompts/en/translate/glossary.md +5 -0
- package/prompts/en/translate/notice.md +2 -0
- package/prompts/en/world/procedure.md +1 -1
- package/prompts/en/world/welcome.md +1 -1
- package/prompts/zh/roleplay/character-decide.md +1 -1
- package/prompts/zh/roleplay/character-rules.md +1 -1
- package/prompts/zh/roleplay/chronicler.md +1 -1
- package/prompts/zh/roleplay/game-master.md +1 -1
- package/prompts/zh/roleplay/reflection.md +9 -0
- package/prompts/zh/roleplay/style.md +1 -1
- package/prompts/zh/roleplay/suggester.md +1 -1
- package/prompts/zh/translate/document.md +6 -0
- package/prompts/zh/translate/glossary.md +5 -0
- package/prompts/zh/translate/notice.md +2 -0
- package/prompts/zh/world/procedure.md +1 -1
- package/prompts/zh/world/welcome.md +1 -1
- package/lib/access-C1nFQiyT.js +0 -12
- package/lib/activity-t7OBgHlc.js +0 -1386
|
@@ -12,6 +12,14 @@ function roleplaySessionOf(chat) {
|
|
|
12
12
|
return UUID_V4.test(id) ? id : void 0;
|
|
13
13
|
}
|
|
14
14
|
//#endregion
|
|
15
|
-
|
|
15
|
+
//#region src/host/prompts/access.ts
|
|
16
|
+
/** The prompt `id` in `language`, from the library of the agent's own context. */
|
|
17
|
+
function promptFor(agent, id, language, variables) {
|
|
18
|
+
const library = agent.ctx.get("roleplayPrompts");
|
|
19
|
+
if (library === void 0) throw new Error("the prompt library is not running");
|
|
20
|
+
return library.text(id, language, variables, roleplaySessionOf(agent.session.id));
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { chatSessionId as n, roleplaySessionOf as r, promptFor as t };
|
|
16
24
|
|
|
17
|
-
//# sourceMappingURL=
|
|
25
|
+
//# sourceMappingURL=access-EbNdZFbk.js.map
|
package/lib/authoring.js
CHANGED
|
@@ -1,26 +1,130 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
1
|
+
import { A as isReviewing, C as translateTools, E as authoredWorldOf, G as TRIGGERS_FILE_NAME, St as parseRelationEdit, T as importTools, _t as InvalidEditProblems, a as replaceAuthoredEntity, b as triggerProblemsOf, d as editWorldPlots, et as PROSE_PROMPTS, f as editWorldRelation, g as readWorldEditor, i as readAuthoredEntityDocument, it as writeWorldPrompt, n as createAuthoredEntity, nt as listWorldPrompts, r as deleteAuthoredEntity, rt as readWorldPrompt, t as AUTHORED_ENTITY_KINDS, tt as deleteWorldPrompt, w as importHooks, x as loadWorldParts } from "./documents-DtlY2IRt.js";
|
|
2
|
+
import { A as formatIssue, T as parsePlotsFile, a as packIds, d as packPath, f as readPack, g as SURFACE_NAMES, h as PACK_MEDIA_TYPES, l as isPackId, m as PACK_LIMITS, p as PACK_ACTIONS, z as shippedPromptsDirectory } from "./packs-CkRZDCsV.js";
|
|
3
|
+
import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
5
4
|
import { existsSync, lstatSync } from "node:fs";
|
|
6
|
-
import {
|
|
5
|
+
import { randomUUID } from "node:crypto";
|
|
7
6
|
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
8
|
-
import { mkdir, readFile, rename } from "node:fs/promises";
|
|
7
|
+
import { mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
8
|
+
import { readTriggerDefinition } from "@dsh-agentic-roleplay/state";
|
|
9
|
+
//#region src/host/triggers/files.ts
|
|
10
|
+
/** The fields of a trigger in the file; any other is a mistake that would otherwise be dropped without a word. */
|
|
11
|
+
const FIELDS = /* @__PURE__ */ new Set([
|
|
12
|
+
"key",
|
|
13
|
+
"name",
|
|
14
|
+
"when",
|
|
15
|
+
"effects",
|
|
16
|
+
"repeat",
|
|
17
|
+
"cooldown_seconds",
|
|
18
|
+
"chance",
|
|
19
|
+
"priority",
|
|
20
|
+
"expires",
|
|
21
|
+
"announce"
|
|
22
|
+
]);
|
|
23
|
+
const isDocument = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
24
|
+
/** The documents of `<world>/triggers.json` as written; none when the file is not there. @throws {InvalidEditProblems} when it is not a list of objects. */
|
|
25
|
+
async function readTriggerDocuments(directory) {
|
|
26
|
+
let text;
|
|
27
|
+
try {
|
|
28
|
+
text = await readFile(join(directory, TRIGGERS_FILE_NAME), "utf8");
|
|
29
|
+
} catch (error) {
|
|
30
|
+
if (error.code === "ENOENT") return [];
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
let data;
|
|
34
|
+
try {
|
|
35
|
+
data = JSON.parse(text.replace(/^/, ""));
|
|
36
|
+
} catch (error) {
|
|
37
|
+
throw new InvalidEditProblems([`triggers.json is not valid JSON: ${error instanceof Error ? error.message : String(error)}`]);
|
|
38
|
+
}
|
|
39
|
+
if (!Array.isArray(data) || !data.every(isDocument)) throw new InvalidEditProblems(["triggers.json is not a list of triggers"]);
|
|
40
|
+
return data;
|
|
41
|
+
}
|
|
42
|
+
async function writeDocuments(directory, documents) {
|
|
43
|
+
const file = join(directory, TRIGGERS_FILE_NAME);
|
|
44
|
+
const temporary = join(dirname(file), `.${basename(file)}.${process.pid}.${randomUUID()}.tmp`);
|
|
45
|
+
try {
|
|
46
|
+
const mode = await stat(file).then((value) => value.mode, () => void 0);
|
|
47
|
+
await writeFile(temporary, `${JSON.stringify(documents, null, 2)}\n`, mode === void 0 ? {} : { mode });
|
|
48
|
+
await rename(temporary, file);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
await rm(temporary, { force: true });
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** The keys of the triggers a condition waits for. */
|
|
55
|
+
function waitsFor(condition) {
|
|
56
|
+
switch (condition.type) {
|
|
57
|
+
case "fired": return [condition.trigger];
|
|
58
|
+
case "all":
|
|
59
|
+
case "any": return condition.conditions.flatMap(waitsFor);
|
|
60
|
+
case "not": return waitsFor(condition.condition);
|
|
61
|
+
default: return [];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Makes or replaces one trigger of the world, by key, from a complete document in the file format. It is read as a session start reads it, and
|
|
66
|
+
* what it names that the world does not have is refused, with every problem; nothing is written then.
|
|
67
|
+
* @throws {InvalidEditProblems}
|
|
68
|
+
*/
|
|
69
|
+
async function writeTriggerDocument(directory, parts, key, document) {
|
|
70
|
+
if (!isDocument(document)) throw new InvalidEditProblems(["document must be a JSON object"]);
|
|
71
|
+
const unknown = Object.keys(document).filter((field) => !FIELDS.has(field));
|
|
72
|
+
if (unknown.length > 0) throw new InvalidEditProblems(unknown.map((field) => `"${field}" is not a field of a trigger (${[...FIELDS].join(", ")})`));
|
|
73
|
+
const written = {
|
|
74
|
+
...document,
|
|
75
|
+
key
|
|
76
|
+
};
|
|
77
|
+
const parsed = readTriggerDefinition(written, "trigger");
|
|
78
|
+
if ("problems" in parsed) throw new InvalidEditProblems(parsed.problems);
|
|
79
|
+
const documents = await readTriggerDocuments(directory);
|
|
80
|
+
const at = documents.findIndex((entry) => entry["key"] === key);
|
|
81
|
+
const others = documents.filter((_, index) => index !== at).flatMap((entry) => {
|
|
82
|
+
const read = readTriggerDefinition(entry, "trigger");
|
|
83
|
+
return "definition" in read ? [read.definition] : [];
|
|
84
|
+
});
|
|
85
|
+
const problems = triggerProblemsOf(parts, [...others, parsed.definition]).get(key) ?? [];
|
|
86
|
+
if (problems.length > 0) throw new InvalidEditProblems(problems);
|
|
87
|
+
await writeDocuments(directory, at < 0 ? [...documents, written] : documents.map((entry, index) => index === at ? written : entry));
|
|
88
|
+
return at < 0 ? "created" : "replaced";
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Removes one trigger from the world's file, keeping its document in `.trash/triggers` so that it can be put back. A trigger that others wait for
|
|
92
|
+
* cannot be removed.
|
|
93
|
+
* @returns where the document was kept.
|
|
94
|
+
* @throws {InvalidEditProblems}
|
|
95
|
+
*/
|
|
96
|
+
async function deleteTriggerDocument(directory, key) {
|
|
97
|
+
const documents = await readTriggerDocuments(directory);
|
|
98
|
+
const found = documents.find((entry) => entry["key"] === key);
|
|
99
|
+
if (found === void 0) throw new InvalidEditProblems([`there is no trigger "${key}"`]);
|
|
100
|
+
const waiting = documents.flatMap((entry) => {
|
|
101
|
+
const read = readTriggerDefinition(entry, "trigger");
|
|
102
|
+
return "definition" in read && read.definition.key !== key && waitsFor(read.definition.condition).includes(key) ? [read.definition.key] : [];
|
|
103
|
+
});
|
|
104
|
+
if (waiting.length > 0) throw new InvalidEditProblems([`${waiting.join(", ")} wait${waiting.length === 1 ? "s" : ""} for "${key}" to fire; change ${waiting.length === 1 ? "it" : "them"} first`]);
|
|
105
|
+
const trash = join(directory, ".trash", "triggers");
|
|
106
|
+
await mkdir(trash, { recursive: true });
|
|
107
|
+
const kept = join(trash, `${key}-${Date.now()}-${randomUUID()}.json`);
|
|
108
|
+
await writeFile(kept, `${JSON.stringify(found, null, 2)}\n`, { flag: "wx" });
|
|
109
|
+
await writeDocuments(directory, documents.filter((entry) => entry !== found));
|
|
110
|
+
return kept;
|
|
111
|
+
}
|
|
112
|
+
//#endregion
|
|
9
113
|
//#region src/host/authoring/read-tools.ts
|
|
10
|
-
const output$
|
|
114
|
+
const output$3 = (value) => [{
|
|
11
115
|
type: "text",
|
|
12
116
|
text: JSON.stringify(value, null, 2)
|
|
13
117
|
}];
|
|
14
118
|
const jsonOutput$3 = {
|
|
15
119
|
schema: { type: "json" },
|
|
16
|
-
render: (_args, value) => output$
|
|
120
|
+
render: (_args, value) => output$3(value)
|
|
17
121
|
};
|
|
18
122
|
const keyParameter$1 = {
|
|
19
123
|
type: "string",
|
|
20
124
|
required: true,
|
|
21
125
|
description: "Stable key shown by the corresponding list tool."
|
|
22
126
|
};
|
|
23
|
-
const json
|
|
127
|
+
const json = (value) => JSON.parse(JSON.stringify(value));
|
|
24
128
|
/** Resolves every call from its authoring chat identity, then reads the files afresh. */
|
|
25
129
|
var AuthoredWorldReader = class {
|
|
26
130
|
worlds;
|
|
@@ -50,7 +154,7 @@ function listTool(reader, name, description, select, summarize) {
|
|
|
50
154
|
output: jsonOutput$3,
|
|
51
155
|
isConcurrencySafe: () => true,
|
|
52
156
|
async execute(_args, exec) {
|
|
53
|
-
return json
|
|
157
|
+
return json(select((await reader.read(exec)).state).map(summarize));
|
|
54
158
|
}
|
|
55
159
|
});
|
|
56
160
|
}
|
|
@@ -64,7 +168,7 @@ function getTool(reader, name, description, select) {
|
|
|
64
168
|
async execute(args, exec) {
|
|
65
169
|
const found = select((await reader.read(exec)).state).find((entry) => entry.key === args.key);
|
|
66
170
|
if (found === void 0) throw new Error(`there is no ${name.slice(4).replaceAll("_", " ")} with key "${args.key}"`);
|
|
67
|
-
return json
|
|
171
|
+
return json(found);
|
|
68
172
|
}
|
|
69
173
|
});
|
|
70
174
|
}
|
|
@@ -80,7 +184,7 @@ function authoredWorldReadTools(reader) {
|
|
|
80
184
|
async execute(_args, exec) {
|
|
81
185
|
const { record, state } = await reader.read(exec);
|
|
82
186
|
const document = JSON.parse(await readFile(join(record.directory, "world.json"), "utf8"));
|
|
83
|
-
return json
|
|
187
|
+
return json({
|
|
84
188
|
id: record.summary.id,
|
|
85
189
|
name: record.summary.name,
|
|
86
190
|
description: record.summary.description,
|
|
@@ -159,7 +263,7 @@ function authoredWorldReadTools(reader) {
|
|
|
159
263
|
isConcurrencySafe: () => true,
|
|
160
264
|
async execute(args, exec) {
|
|
161
265
|
const { record } = await reader.read(exec);
|
|
162
|
-
return json
|
|
266
|
+
return json(await readAuthoredEntityDocument({
|
|
163
267
|
id: record.summary.id,
|
|
164
268
|
directory: record.directory
|
|
165
269
|
}, args.kind, args.key));
|
|
@@ -175,6 +279,32 @@ function authoredWorldReadTools(reader) {
|
|
|
175
279
|
key: entry.definition.id,
|
|
176
280
|
name: entry.definition.name
|
|
177
281
|
}))),
|
|
282
|
+
listTool(reader, "list_world_triggers", "List the world’s triggers (what the world does by itself): key, name, what each waits for (when), the kinds of its effects, whether it repeats, and what it names that the world does not have (problems).", (world) => (world.triggers ?? []).map((entry) => ({
|
|
283
|
+
...entry,
|
|
284
|
+
name: entry.name
|
|
285
|
+
})), (entry) => ({
|
|
286
|
+
key: entry.key,
|
|
287
|
+
name: entry.name,
|
|
288
|
+
when: entry.when,
|
|
289
|
+
effects: entry.effects.map((effect) => effect.type),
|
|
290
|
+
repeat: entry.repeat,
|
|
291
|
+
priority: entry.priority,
|
|
292
|
+
announced: entry.announced,
|
|
293
|
+
problems: entry.problems
|
|
294
|
+
})),
|
|
295
|
+
defineTool({
|
|
296
|
+
name: "get_world_trigger",
|
|
297
|
+
description: "Read the exact snake_case JSON document stored for one trigger in triggers.json. Use this before replacing a trigger.",
|
|
298
|
+
parameters: { key: keyParameter$1 },
|
|
299
|
+
output: jsonOutput$3,
|
|
300
|
+
isConcurrencySafe: () => true,
|
|
301
|
+
async execute(args, exec) {
|
|
302
|
+
const { record } = await reader.read(exec);
|
|
303
|
+
const found = (await readTriggerDocuments(record.directory)).find((entry) => entry["key"] === args.key);
|
|
304
|
+
if (found === void 0) throw new Error(`there is no trigger with key "${args.key}"`);
|
|
305
|
+
return json(found);
|
|
306
|
+
}
|
|
307
|
+
}),
|
|
178
308
|
defineTool({
|
|
179
309
|
name: "list_relationships",
|
|
180
310
|
description: "Read authored character relationships, knowledge, faction memberships, social ties and map exits. Optionally narrow by kind or endpoint key.",
|
|
@@ -201,7 +331,7 @@ function authoredWorldReadTools(reader) {
|
|
|
201
331
|
output: jsonOutput$3,
|
|
202
332
|
isConcurrencySafe: () => true,
|
|
203
333
|
async execute(args, exec) {
|
|
204
|
-
return json
|
|
334
|
+
return json((await reader.read(exec)).state.relations.filter((relation) => (args.kind === void 0 || relation.kind === args.kind) && (args.from === void 0 || relation.from.key === args.from) && (args.to === void 0 || relation.to.key === args.to)));
|
|
205
335
|
}
|
|
206
336
|
})
|
|
207
337
|
];
|
|
@@ -262,13 +392,13 @@ async function deleteWorldPackFile(directory, pack, path) {
|
|
|
262
392
|
//#endregion
|
|
263
393
|
//#region src/host/authoring/pack-tools.ts
|
|
264
394
|
const PACK_MUTATION_TOOLS = ["delete_world_pack_file"];
|
|
265
|
-
const output$
|
|
395
|
+
const output$2 = (value) => [{
|
|
266
396
|
type: "text",
|
|
267
397
|
text: JSON.stringify(value, null, 2)
|
|
268
398
|
}];
|
|
269
399
|
const jsonOutput$2 = {
|
|
270
400
|
schema: { type: "json" },
|
|
271
|
-
render: (_args, value) => output$
|
|
401
|
+
render: (_args, value) => output$2(value)
|
|
272
402
|
};
|
|
273
403
|
/** A path as the file tools take it: relative to the chat's folder when it has one, which is short and hard to mistype. */
|
|
274
404
|
function relativeTo(exec, path) {
|
|
@@ -371,13 +501,13 @@ function worldPackTools(reader) {
|
|
|
371
501
|
//#endregion
|
|
372
502
|
//#region src/host/authoring/prompt-tools.ts
|
|
373
503
|
const PROMPT_MUTATION_TOOLS = ["write_world_prompt", "delete_world_prompt"];
|
|
374
|
-
const output$
|
|
504
|
+
const output$1 = (value) => [{
|
|
375
505
|
type: "text",
|
|
376
506
|
text: JSON.stringify(value, null, 2)
|
|
377
507
|
}];
|
|
378
508
|
const jsonOutput$1 = {
|
|
379
509
|
schema: { type: "json" },
|
|
380
|
-
render: (_args, value) => output$
|
|
510
|
+
render: (_args, value) => output$1(value)
|
|
381
511
|
};
|
|
382
512
|
const idParameter = {
|
|
383
513
|
type: "string",
|
|
@@ -466,13 +596,13 @@ function worldPromptTools(reader, shipped = shippedPromptsDirectory()) {
|
|
|
466
596
|
}
|
|
467
597
|
//#endregion
|
|
468
598
|
//#region src/host/authoring/write-tools.ts
|
|
469
|
-
const output
|
|
599
|
+
const output = (value) => [{
|
|
470
600
|
type: "text",
|
|
471
601
|
text: JSON.stringify(value, null, 2)
|
|
472
602
|
}];
|
|
473
603
|
const jsonOutput = {
|
|
474
604
|
schema: { type: "json" },
|
|
475
|
-
render: (_args, value) => output
|
|
605
|
+
render: (_args, value) => output(value)
|
|
476
606
|
};
|
|
477
607
|
const kindParameter = {
|
|
478
608
|
type: "string",
|
|
@@ -490,6 +620,8 @@ const AUTHORING_MUTATION_TOOLS = /* @__PURE__ */ new Set([
|
|
|
490
620
|
"delete_world_element",
|
|
491
621
|
"edit_world_relationship",
|
|
492
622
|
"replace_world_plots",
|
|
623
|
+
"write_world_trigger",
|
|
624
|
+
"delete_world_trigger",
|
|
493
625
|
...PROMPT_MUTATION_TOOLS,
|
|
494
626
|
...PACK_MUTATION_TOOLS
|
|
495
627
|
]);
|
|
@@ -622,6 +754,43 @@ function authoredWorldWriteTools(reader) {
|
|
|
622
754
|
};
|
|
623
755
|
return result("replaced", "plots", "*", await editWorldPlots(world, { plots: args.plots.map(plotDocument) }));
|
|
624
756
|
}
|
|
757
|
+
}),
|
|
758
|
+
defineTool({
|
|
759
|
+
name: "write_world_trigger",
|
|
760
|
+
description: "Create or replace one trigger (something the world does by itself) in triggers.json, from a complete document in the file format, by its key. It is read as a story start reads it and refused, with every problem, when it is malformed or names a character, place, item, variable or trigger the world does not have; read get_world_trigger first when replacing. Requires user approval.",
|
|
761
|
+
parameters: {
|
|
762
|
+
key: keyParameter,
|
|
763
|
+
document: {
|
|
764
|
+
type: "json",
|
|
765
|
+
required: true,
|
|
766
|
+
description: "Complete snake_case trigger: name, when (condition), effects (one to six: edit, beat, cue), and optionally repeat, cooldown_seconds, chance, priority, expires, announce. A beat or cue may have a while condition and names such as {where:character}."
|
|
767
|
+
}
|
|
768
|
+
},
|
|
769
|
+
output: jsonOutput,
|
|
770
|
+
async execute(args, exec) {
|
|
771
|
+
const { record } = await reader.read(exec);
|
|
772
|
+
return result(await writeTriggerDocument(record.directory, await loadWorldParts(record.directory), args.key, args.document), "trigger", args.key, await readWorldEditor({
|
|
773
|
+
id: record.summary.id,
|
|
774
|
+
directory: record.directory
|
|
775
|
+
}));
|
|
776
|
+
}
|
|
777
|
+
}),
|
|
778
|
+
defineTool({
|
|
779
|
+
name: "delete_world_trigger",
|
|
780
|
+
description: "Remove one trigger from triggers.json. Its document is kept in the world’s .trash/triggers so it can be put back. A trigger that others wait for cannot be removed. Requires user approval.",
|
|
781
|
+
parameters: { key: keyParameter },
|
|
782
|
+
output: jsonOutput,
|
|
783
|
+
async execute(args, exec) {
|
|
784
|
+
const { record } = await reader.read(exec);
|
|
785
|
+
const kept = await deleteTriggerDocument(record.directory, args.key);
|
|
786
|
+
return result("deleted", "trigger", args.key, await readWorldEditor({
|
|
787
|
+
id: record.summary.id,
|
|
788
|
+
directory: record.directory
|
|
789
|
+
}), {
|
|
790
|
+
recoverable: true,
|
|
791
|
+
trashPath: kept
|
|
792
|
+
});
|
|
793
|
+
}
|
|
625
794
|
})
|
|
626
795
|
];
|
|
627
796
|
}
|
|
@@ -668,16 +837,16 @@ async function authoringApproval(exec, next, reviewing = () => Promise.resolve(f
|
|
|
668
837
|
}
|
|
669
838
|
};
|
|
670
839
|
}
|
|
671
|
-
const kind = typeof args["kind"] === "string" ? args["kind"] : exec.name === "edit_world_relationship" ? "relationship" : "plots";
|
|
840
|
+
const kind = typeof args["kind"] === "string" ? args["kind"] : exec.name === "edit_world_relationship" ? "relationship" : exec.name.endsWith("trigger") ? "trigger" : "plots";
|
|
672
841
|
const key = typeof args["key"] === "string" ? args["key"] : "";
|
|
673
842
|
const target = key === "" ? kind : `${kind} “${key}”`;
|
|
674
|
-
const action = exec.name.startsWith("create") ? "Create" : exec.name.startsWith("delete") ? "Delete" : "Edit";
|
|
843
|
+
const action = exec.name.startsWith("create") ? "Create" : exec.name.startsWith("delete") ? "Delete" : exec.name === "write_world_trigger" ? "Write" : "Edit";
|
|
675
844
|
return {
|
|
676
845
|
kind: "ask",
|
|
677
846
|
reason: `${action.toLowerCase()} authored world ${target}`,
|
|
678
847
|
displayReason: {
|
|
679
848
|
en: `${action} ${target} in the world files?`,
|
|
680
|
-
zh: `${action === "Create" ? "创建" : action === "Delete" ? "删除" : "编辑"}世界文件中的${target}?`
|
|
849
|
+
zh: `${action === "Create" ? "创建" : action === "Delete" ? "删除" : action === "Write" ? "写入" : "编辑"}世界文件中的${target}?`
|
|
681
850
|
}
|
|
682
851
|
};
|
|
683
852
|
}
|
|
@@ -773,210 +942,6 @@ function registerPackJail(ctx, deps) {
|
|
|
773
942
|
ctx.on("tools/pre-execute", (exec, next) => packJail(exec, next, deps), { prepend: true });
|
|
774
943
|
}
|
|
775
944
|
//#endregion
|
|
776
|
-
//#region src/host/import/tools.ts
|
|
777
|
-
const SURVEY_TOOL = "import_survey";
|
|
778
|
-
const BUILD_TOOL = "import_build";
|
|
779
|
-
const FINISH_TOOL = "import_finish";
|
|
780
|
-
/** How many times a turn that has not finished the import is sent back to it. */
|
|
781
|
-
const MAX_OBJECTIONS = 3;
|
|
782
|
-
/** A model call that fails is made once more before the piece it was for is given up. */
|
|
783
|
-
const ATTEMPTS = 2;
|
|
784
|
-
const CARD = `${IMPORT_FOLDER}/card.json`;
|
|
785
|
-
const cardParameter = {
|
|
786
|
-
type: "string",
|
|
787
|
-
required: true,
|
|
788
|
-
description: `The card to import: the path that follows /import-st in the person's message (${CARD}).`
|
|
789
|
-
};
|
|
790
|
-
/** A model that is asked for a tool with nothing to fill in tends to write its name as text instead of calling it: naming the card gives it something to say. */
|
|
791
|
-
function checkedCard(args) {
|
|
792
|
-
if (args.card.trim() !== CARD) throw new Error(`the card to import is ${CARD}`);
|
|
793
|
-
}
|
|
794
|
-
const output = (render) => ({
|
|
795
|
-
schema: { type: "json" },
|
|
796
|
-
render: (_args, value) => [{
|
|
797
|
-
type: "text",
|
|
798
|
-
text: render(value)
|
|
799
|
-
}]
|
|
800
|
-
});
|
|
801
|
-
const json = (value) => JSON.parse(JSON.stringify(value));
|
|
802
|
-
/** The runner of an import: every job is a narrow agent below the chat's own, on the chat's model. */
|
|
803
|
-
function contextOf(exec, settings, text) {
|
|
804
|
-
const agent = exec.agent;
|
|
805
|
-
if (agent === void 0) throw new Error("the import tools can only be called by an agent");
|
|
806
|
-
const run = async (job) => {
|
|
807
|
-
for (let attempt = 1;; attempt += 1) try {
|
|
808
|
-
return await runChild({
|
|
809
|
-
parent: agent,
|
|
810
|
-
signal: exec.signal,
|
|
811
|
-
persona: job.persona,
|
|
812
|
-
prompt: job.prompt,
|
|
813
|
-
result: job.result,
|
|
814
|
-
...job.check === void 0 ? {} : { beforeSubmit: (value) => job.check?.(value) },
|
|
815
|
-
maxTokens: settings.maxTokens,
|
|
816
|
-
sampling: {
|
|
817
|
-
temperature: settings.temperature,
|
|
818
|
-
toolChoice: "required"
|
|
819
|
-
},
|
|
820
|
-
...settings.reasoning === "" ? {} : { reasoningEffort: settings.reasoning }
|
|
821
|
-
});
|
|
822
|
-
} catch (error) {
|
|
823
|
-
if (exec.signal.aborted || attempt >= ATTEMPTS) throw error;
|
|
824
|
-
}
|
|
825
|
-
};
|
|
826
|
-
return {
|
|
827
|
-
run,
|
|
828
|
-
concurrency: settings.concurrency,
|
|
829
|
-
text,
|
|
830
|
-
say: () => void 0
|
|
831
|
-
};
|
|
832
|
-
}
|
|
833
|
-
async function worldOf(worlds, exec) {
|
|
834
|
-
const chat = exec.agent?.session.id;
|
|
835
|
-
const worldId = chat === void 0 ? void 0 : authoredWorldOf(chat);
|
|
836
|
-
if (worldId === void 0) throw new Error("the import tools are available only in a world authoring chat");
|
|
837
|
-
const record = await worlds.find(worldId);
|
|
838
|
-
if (record === void 0) throw new Error(`the authored world "${worldId}" no longer exists`);
|
|
839
|
-
return {
|
|
840
|
-
id: worldId,
|
|
841
|
-
directory: record.directory
|
|
842
|
-
};
|
|
843
|
-
}
|
|
844
|
-
const toolNames = {
|
|
845
|
-
survey: SURVEY_TOOL,
|
|
846
|
-
build: BUILD_TOOL,
|
|
847
|
-
finish: FINISH_TOOL
|
|
848
|
-
};
|
|
849
|
-
/** The three steps of an import, as tools of the authoring chat. They do their work in agents of their own, one narrow job each. */
|
|
850
|
-
function importTools(worlds, settings, prompting) {
|
|
851
|
-
/** Runs one step for the world of the calling chat; what the model should do next is told in the result, in the person's language. */
|
|
852
|
-
const step = async (exec, work, next) => {
|
|
853
|
-
const world = await worldOf(worlds, exec);
|
|
854
|
-
const language = prompting.languages().get(world.id);
|
|
855
|
-
const text = (id, variables) => prompting.prompts().text(id, language, {
|
|
856
|
-
...toolNames,
|
|
857
|
-
...variables
|
|
858
|
-
});
|
|
859
|
-
const result = await work(contextOf(exec, settings(), text), world.directory);
|
|
860
|
-
return json({
|
|
861
|
-
...result,
|
|
862
|
-
next: next(text, result)
|
|
863
|
-
});
|
|
864
|
-
};
|
|
865
|
-
return [
|
|
866
|
-
defineTool({
|
|
867
|
-
name: SURVEY_TOOL,
|
|
868
|
-
description: "Step 1 of importing a character card into this world: read every piece of the card, say what each is (character, place, background, instruction…), and list who and what the world is made of. Takes a few minutes. Only in an import turn.",
|
|
869
|
-
parameters: { card: cardParameter },
|
|
870
|
-
output: output((value) => JSON.stringify(value, null, 1)),
|
|
871
|
-
isConcurrencySafe: () => false,
|
|
872
|
-
async execute(args, exec) {
|
|
873
|
-
checkedCard(args);
|
|
874
|
-
return await step(exec, survey, (text) => text("import/next-survey"));
|
|
875
|
-
}
|
|
876
|
-
}),
|
|
877
|
-
defineTool({
|
|
878
|
-
name: BUILD_TOOL,
|
|
879
|
-
description: "Step 2 of importing a character card: write every character, place, organisation, object and background entry from the pieces of the card that are about it. Takes several minutes. Run after the survey.",
|
|
880
|
-
parameters: { card: cardParameter },
|
|
881
|
-
output: output((value) => JSON.stringify(value, null, 1)),
|
|
882
|
-
isConcurrencySafe: () => false,
|
|
883
|
-
async execute(args, exec) {
|
|
884
|
-
checkedCard(args);
|
|
885
|
-
return await step(exec, build, (text) => text("import/next-build"));
|
|
886
|
-
}
|
|
887
|
-
}),
|
|
888
|
-
defineTool({
|
|
889
|
-
name: FINISH_TOOL,
|
|
890
|
-
description: "Step 3 of importing a character card: check that the whole world would start, write it into the world together with its description, tags, opening and cover, and report what was imported. Nothing is written when the world would not start; the problems are returned. Run after the build.",
|
|
891
|
-
parameters: { card: cardParameter },
|
|
892
|
-
output: output((value) => JSON.stringify(value, null, 1)),
|
|
893
|
-
isConcurrencySafe: () => false,
|
|
894
|
-
async execute(args, exec) {
|
|
895
|
-
checkedCard(args);
|
|
896
|
-
return await step(exec, finish, (text, result) => text(result.ok ? "import/finish-ok" : "import/finish-failed"));
|
|
897
|
-
}
|
|
898
|
-
})
|
|
899
|
-
];
|
|
900
|
-
}
|
|
901
|
-
/** The procedure of an import turn, told to the chat in place of what it does for any other message. */
|
|
902
|
-
function importNotice(phase, text) {
|
|
903
|
-
if (phase === void 0) return text("import/notice-missing");
|
|
904
|
-
return text("import/notice", {
|
|
905
|
-
card: CARD,
|
|
906
|
-
phase,
|
|
907
|
-
done: phase === "done" ? text("import/notice-done") : ""
|
|
908
|
-
});
|
|
909
|
-
}
|
|
910
|
-
/** Hears the `/import-st` command in an authoring chat, makes that turn an import turn, and does not let it end before the import is done. */
|
|
911
|
-
function importHooks(ctx, worlds, prompting) {
|
|
912
|
-
const textFor = (worldId) => (id, variables) => prompting.prompts().text(id, worldId === void 0 ? void 0 : prompting.languages().get(worldId), {
|
|
913
|
-
...toolNames,
|
|
914
|
-
...variables
|
|
915
|
-
});
|
|
916
|
-
const imports = /* @__PURE__ */ new Map();
|
|
917
|
-
const objections = /* @__PURE__ */ new Map();
|
|
918
|
-
const authoring = (agent) => agent.session.header.origin === "subagent" || authoredWorldOf(agent.session.id) === void 0 ? void 0 : agent.session.id;
|
|
919
|
-
ctx.on("agent/pre-step", async (payload, next) => {
|
|
920
|
-
const decision = await next();
|
|
921
|
-
const chat = authoring(payload.agent);
|
|
922
|
-
if (decision.kind !== "enter" || payload.step !== 1 || chat === void 0) return decision;
|
|
923
|
-
const text = payload.messages.filter((message) => message.source.kind === "user").flatMap((message) => message.content.flatMap((block) => block.type === "text" ? [block.text] : [])).join("\n");
|
|
924
|
-
if (parseImportCommand(text) === void 0) return decision;
|
|
925
|
-
const worldId = authoredWorldOf(chat);
|
|
926
|
-
const record = worldId === void 0 ? void 0 : await worlds.find(worldId);
|
|
927
|
-
const phase = record === void 0 ? void 0 : await importPhase(record.directory);
|
|
928
|
-
if (phase !== void 0 && phase !== "done") {
|
|
929
|
-
imports.set(chat, payload.turn);
|
|
930
|
-
objections.delete(chat);
|
|
931
|
-
}
|
|
932
|
-
return {
|
|
933
|
-
...decision,
|
|
934
|
-
messages: [...decision.messages, createUserMessage({
|
|
935
|
-
content: [{
|
|
936
|
-
type: "text",
|
|
937
|
-
text: importNotice(phase, textFor(worldId))
|
|
938
|
-
}],
|
|
939
|
-
source: { kind: "roleplay" }
|
|
940
|
-
})]
|
|
941
|
-
};
|
|
942
|
-
});
|
|
943
|
-
ctx.on("session/event", async (session, event) => {
|
|
944
|
-
if (event.type !== "turn/end" || session.header.origin === "subagent") return;
|
|
945
|
-
const worldId = authoredWorldOf(session.id);
|
|
946
|
-
const record = worldId === void 0 ? void 0 : await worlds.find(worldId);
|
|
947
|
-
if (record !== void 0) await endReview(record.directory);
|
|
948
|
-
});
|
|
949
|
-
ctx.on("agent/turn-stopping", async ({ agent, turn }) => {
|
|
950
|
-
const chat = authoring(agent);
|
|
951
|
-
if (chat === void 0 || imports.get(chat) !== turn) return;
|
|
952
|
-
const worldId = authoredWorldOf(chat);
|
|
953
|
-
const record = worldId === void 0 ? void 0 : await worlds.find(worldId);
|
|
954
|
-
const phase = record === void 0 ? void 0 : await importPhase(record.directory);
|
|
955
|
-
if (phase === void 0 || phase === "done") {
|
|
956
|
-
imports.delete(chat);
|
|
957
|
-
return;
|
|
958
|
-
}
|
|
959
|
-
const told = objections.get(chat) ?? 0;
|
|
960
|
-
if (told >= MAX_OBJECTIONS) {
|
|
961
|
-
imports.delete(chat);
|
|
962
|
-
return;
|
|
963
|
-
}
|
|
964
|
-
objections.set(chat, told + 1);
|
|
965
|
-
const next = phase === "ready" ? SURVEY_TOOL : phase === "surveyed" ? BUILD_TOOL : FINISH_TOOL;
|
|
966
|
-
agent.steer(createUserMessage({
|
|
967
|
-
content: [{
|
|
968
|
-
type: "text",
|
|
969
|
-
text: textFor(worldId)("import/steer", {
|
|
970
|
-
phase,
|
|
971
|
-
next,
|
|
972
|
-
card: CARD
|
|
973
|
-
})
|
|
974
|
-
}],
|
|
975
|
-
source: { kind: "roleplay" }
|
|
976
|
-
}));
|
|
977
|
-
});
|
|
978
|
-
}
|
|
979
|
-
//#endregion
|
|
980
945
|
//#region src/authoring.ts
|
|
981
946
|
const name = "@dsh-agentic-roleplay/core/authoring";
|
|
982
947
|
const inject = [
|
|
@@ -1003,6 +968,7 @@ function apply(ctx) {
|
|
|
1003
968
|
for (const tool of worldPromptTools(reader)) ctx.tools.register(tool);
|
|
1004
969
|
for (const tool of worldPackTools(reader)) ctx.tools.register(tool);
|
|
1005
970
|
for (const tool of importTools(ctx.roleplayWorlds, () => ctx.roleplayImport.settings, prompting)) ctx.tools.register(tool);
|
|
971
|
+
for (const tool of translateTools(ctx.roleplayWorlds, () => ctx.roleplayImport.settings, prompting)) ctx.tools.register(tool);
|
|
1006
972
|
importHooks(ctx, ctx.roleplayWorlds, prompting);
|
|
1007
973
|
const reviewing = async (chat) => {
|
|
1008
974
|
const id = authoredWorldOf(chat);
|