@dsh-agentic-roleplay/core 0.3.0 → 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.
Files changed (41) hide show
  1. package/lib/authoring.js +18 -223
  2. package/lib/client.js +1838 -489
  3. package/lib/client.js.map +1 -1
  4. package/lib/compaction.js +1 -1
  5. package/lib/deliver-BSsDtF6X.js +3443 -0
  6. package/lib/{documents-7GMc5NnW.js → documents-B5XAdfo2.js} +992 -14
  7. package/lib/{gallery-DUeBMHQX.js → gallery-DS5-qH9F.js} +2 -2
  8. package/lib/{generator-HPifO3Kk.js → generator-BzeepLd2.js} +3 -3
  9. package/lib/images.js +3 -3
  10. package/lib/index.js +351 -13
  11. package/lib/narration-DK2kiIjE.js +141 -0
  12. package/lib/orchestration.js +867 -1547
  13. package/lib/{packs-alJTiDM2.js → packs-BCGK9KZV.js} +51 -7
  14. package/lib/{presenter-agent-PX1WVaJB.js → presenter-agent-Cp-J0onD.js} +3 -3
  15. package/lib/{narration-CKZTk5UE.js → settings-CA6icLL_.js} +158 -142
  16. package/package.json +4 -4
  17. package/packs/scene/strip.js +1 -1
  18. package/prompts/en/roleplay/character-decide.md +1 -1
  19. package/prompts/en/roleplay/character-rules.md +1 -1
  20. package/prompts/en/roleplay/chronicler.md +1 -1
  21. package/prompts/en/roleplay/dice.md +7 -0
  22. package/prompts/en/roleplay/game-master.md +3 -1
  23. package/prompts/en/roleplay/reflection.md +4 -2
  24. package/prompts/en/roleplay/style.md +1 -1
  25. package/prompts/en/roleplay/suggester.md +1 -1
  26. package/prompts/en/translate/document.md +6 -0
  27. package/prompts/en/translate/glossary.md +5 -0
  28. package/prompts/en/translate/notice.md +2 -0
  29. package/prompts/zh/roleplay/character-decide.md +1 -1
  30. package/prompts/zh/roleplay/character-rules.md +1 -1
  31. package/prompts/zh/roleplay/chronicler.md +1 -1
  32. package/prompts/zh/roleplay/dice.md +7 -0
  33. package/prompts/zh/roleplay/game-master.md +3 -1
  34. package/prompts/zh/roleplay/reflection.md +4 -2
  35. package/prompts/zh/roleplay/style.md +1 -1
  36. package/prompts/zh/roleplay/suggester.md +1 -1
  37. package/prompts/zh/translate/document.md +6 -0
  38. package/prompts/zh/translate/glossary.md +5 -0
  39. package/prompts/zh/translate/notice.md +2 -0
  40. package/lib/deliver--uMhC6oO.js +0 -1878
  41. package/lib/src-CWTNeL6Y.js +0 -128
package/lib/authoring.js CHANGED
@@ -1,10 +1,8 @@
1
- import { $ as writeWorldPrompt, A as survey, D as importPhase, E as finish, O as isReviewing, Q as readWorldPrompt, S as authoredWorldOf, T as endReview, U as TRIGGERS_FILE_NAME, X as deleteWorldPrompt, Y as PROSE_PROMPTS, Z as listWorldPrompts, _t as parseRelationEdit, a as replaceAuthoredEntity, at as IMPORT_FOLDER, b as triggerProblemsOf, bt as parseImportCommand, d as editWorldPlots, f as editWorldRelation, ft as InvalidEditProblems, g as readWorldEditor, i as readAuthoredEntityDocument, n as createAuthoredEntity, r as deleteAuthoredEntity, t as AUTHORED_ENTITY_KINDS, w as build, x as loadWorldParts } from "./documents-7GMc5NnW.js";
2
- import { P as shippedPromptsDirectory, T as formatIssue, a as packIds, b as parsePlotsFile, 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 } from "./packs-alJTiDM2.js";
3
- import { A as runChild } from "./generator-HPifO3Kk.js";
1
+ import { A as isReviewing, C as translateTools, Dt as parseRelationEdit, E as authoredWorldOf, K as TRIGGERS_FILE_NAME, St as InvalidEditProblems, T as importTools, a as replaceAuthoredEntity, at as deleteWorldPrompt, b as triggerProblemsOf, ct as writeWorldPrompt, d as editWorldPlots, f as editWorldRelation, g as readWorldEditor, i as readAuthoredEntityDocument, it as PROSE_PROMPTS, n as createAuthoredEntity, ot as listWorldPrompts, r as deleteAuthoredEntity, st as readWorldPrompt, t as AUTHORED_ENTITY_KINDS, w as importHooks, x as loadWorldParts } from "./documents-B5XAdfo2.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-BCGK9KZV.js";
4
3
  import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
5
4
  import { existsSync, lstatSync } from "node:fs";
6
5
  import { randomUUID } from "node:crypto";
7
- import { createUserMessage } from "@deepseek-ai/dsh-llm";
8
6
  import { defineTool } from "@deepseek-ai/dsh-tools";
9
7
  import { mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
10
8
  import { readTriggerDefinition } from "@dsh-agentic-roleplay/state";
@@ -113,20 +111,20 @@ async function deleteTriggerDocument(directory, key) {
113
111
  }
114
112
  //#endregion
115
113
  //#region src/host/authoring/read-tools.ts
116
- const output$4 = (value) => [{
114
+ const output$3 = (value) => [{
117
115
  type: "text",
118
116
  text: JSON.stringify(value, null, 2)
119
117
  }];
120
118
  const jsonOutput$3 = {
121
119
  schema: { type: "json" },
122
- render: (_args, value) => output$4(value)
120
+ render: (_args, value) => output$3(value)
123
121
  };
124
122
  const keyParameter$1 = {
125
123
  type: "string",
126
124
  required: true,
127
125
  description: "Stable key shown by the corresponding list tool."
128
126
  };
129
- const json$1 = (value) => JSON.parse(JSON.stringify(value));
127
+ const json = (value) => JSON.parse(JSON.stringify(value));
130
128
  /** Resolves every call from its authoring chat identity, then reads the files afresh. */
131
129
  var AuthoredWorldReader = class {
132
130
  worlds;
@@ -156,7 +154,7 @@ function listTool(reader, name, description, select, summarize) {
156
154
  output: jsonOutput$3,
157
155
  isConcurrencySafe: () => true,
158
156
  async execute(_args, exec) {
159
- return json$1(select((await reader.read(exec)).state).map(summarize));
157
+ return json(select((await reader.read(exec)).state).map(summarize));
160
158
  }
161
159
  });
162
160
  }
@@ -170,7 +168,7 @@ function getTool(reader, name, description, select) {
170
168
  async execute(args, exec) {
171
169
  const found = select((await reader.read(exec)).state).find((entry) => entry.key === args.key);
172
170
  if (found === void 0) throw new Error(`there is no ${name.slice(4).replaceAll("_", " ")} with key "${args.key}"`);
173
- return json$1(found);
171
+ return json(found);
174
172
  }
175
173
  });
176
174
  }
@@ -186,7 +184,7 @@ function authoredWorldReadTools(reader) {
186
184
  async execute(_args, exec) {
187
185
  const { record, state } = await reader.read(exec);
188
186
  const document = JSON.parse(await readFile(join(record.directory, "world.json"), "utf8"));
189
- return json$1({
187
+ return json({
190
188
  id: record.summary.id,
191
189
  name: record.summary.name,
192
190
  description: record.summary.description,
@@ -265,7 +263,7 @@ function authoredWorldReadTools(reader) {
265
263
  isConcurrencySafe: () => true,
266
264
  async execute(args, exec) {
267
265
  const { record } = await reader.read(exec);
268
- return json$1(await readAuthoredEntityDocument({
266
+ return json(await readAuthoredEntityDocument({
269
267
  id: record.summary.id,
270
268
  directory: record.directory
271
269
  }, args.kind, args.key));
@@ -304,7 +302,7 @@ function authoredWorldReadTools(reader) {
304
302
  const { record } = await reader.read(exec);
305
303
  const found = (await readTriggerDocuments(record.directory)).find((entry) => entry["key"] === args.key);
306
304
  if (found === void 0) throw new Error(`there is no trigger with key "${args.key}"`);
307
- return json$1(found);
305
+ return json(found);
308
306
  }
309
307
  }),
310
308
  defineTool({
@@ -333,7 +331,7 @@ function authoredWorldReadTools(reader) {
333
331
  output: jsonOutput$3,
334
332
  isConcurrencySafe: () => true,
335
333
  async execute(args, exec) {
336
- return json$1((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)));
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)));
337
335
  }
338
336
  })
339
337
  ];
@@ -394,13 +392,13 @@ async function deleteWorldPackFile(directory, pack, path) {
394
392
  //#endregion
395
393
  //#region src/host/authoring/pack-tools.ts
396
394
  const PACK_MUTATION_TOOLS = ["delete_world_pack_file"];
397
- const output$3 = (value) => [{
395
+ const output$2 = (value) => [{
398
396
  type: "text",
399
397
  text: JSON.stringify(value, null, 2)
400
398
  }];
401
399
  const jsonOutput$2 = {
402
400
  schema: { type: "json" },
403
- render: (_args, value) => output$3(value)
401
+ render: (_args, value) => output$2(value)
404
402
  };
405
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. */
406
404
  function relativeTo(exec, path) {
@@ -503,13 +501,13 @@ function worldPackTools(reader) {
503
501
  //#endregion
504
502
  //#region src/host/authoring/prompt-tools.ts
505
503
  const PROMPT_MUTATION_TOOLS = ["write_world_prompt", "delete_world_prompt"];
506
- const output$2 = (value) => [{
504
+ const output$1 = (value) => [{
507
505
  type: "text",
508
506
  text: JSON.stringify(value, null, 2)
509
507
  }];
510
508
  const jsonOutput$1 = {
511
509
  schema: { type: "json" },
512
- render: (_args, value) => output$2(value)
510
+ render: (_args, value) => output$1(value)
513
511
  };
514
512
  const idParameter = {
515
513
  type: "string",
@@ -598,13 +596,13 @@ function worldPromptTools(reader, shipped = shippedPromptsDirectory()) {
598
596
  }
599
597
  //#endregion
600
598
  //#region src/host/authoring/write-tools.ts
601
- const output$1 = (value) => [{
599
+ const output = (value) => [{
602
600
  type: "text",
603
601
  text: JSON.stringify(value, null, 2)
604
602
  }];
605
603
  const jsonOutput = {
606
604
  schema: { type: "json" },
607
- render: (_args, value) => output$1(value)
605
+ render: (_args, value) => output(value)
608
606
  };
609
607
  const kindParameter = {
610
608
  type: "string",
@@ -944,210 +942,6 @@ function registerPackJail(ctx, deps) {
944
942
  ctx.on("tools/pre-execute", (exec, next) => packJail(exec, next, deps), { prepend: true });
945
943
  }
946
944
  //#endregion
947
- //#region src/host/import/tools.ts
948
- const SURVEY_TOOL = "import_survey";
949
- const BUILD_TOOL = "import_build";
950
- const FINISH_TOOL = "import_finish";
951
- /** How many times a turn that has not finished the import is sent back to it. */
952
- const MAX_OBJECTIONS = 3;
953
- /** A model call that fails is made once more before the piece it was for is given up. */
954
- const ATTEMPTS = 2;
955
- const CARD = `${IMPORT_FOLDER}/card.json`;
956
- const cardParameter = {
957
- type: "string",
958
- required: true,
959
- description: `The card to import: the path that follows /import-st in the person's message (${CARD}).`
960
- };
961
- /** 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. */
962
- function checkedCard(args) {
963
- if (args.card.trim() !== CARD) throw new Error(`the card to import is ${CARD}`);
964
- }
965
- const output = (render) => ({
966
- schema: { type: "json" },
967
- render: (_args, value) => [{
968
- type: "text",
969
- text: render(value)
970
- }]
971
- });
972
- const json = (value) => JSON.parse(JSON.stringify(value));
973
- /** The runner of an import: every job is a narrow agent below the chat's own, on the chat's model. */
974
- function contextOf(exec, settings, text) {
975
- const agent = exec.agent;
976
- if (agent === void 0) throw new Error("the import tools can only be called by an agent");
977
- const run = async (job) => {
978
- for (let attempt = 1;; attempt += 1) try {
979
- return await runChild({
980
- parent: agent,
981
- signal: exec.signal,
982
- persona: job.persona,
983
- prompt: job.prompt,
984
- result: job.result,
985
- ...job.check === void 0 ? {} : { beforeSubmit: (value) => job.check?.(value) },
986
- maxTokens: settings.maxTokens,
987
- sampling: {
988
- temperature: settings.temperature,
989
- toolChoice: "required"
990
- },
991
- ...settings.reasoning === "" ? {} : { reasoningEffort: settings.reasoning }
992
- });
993
- } catch (error) {
994
- if (exec.signal.aborted || attempt >= ATTEMPTS) throw error;
995
- }
996
- };
997
- return {
998
- run,
999
- concurrency: settings.concurrency,
1000
- text,
1001
- say: () => void 0
1002
- };
1003
- }
1004
- async function worldOf(worlds, exec) {
1005
- const chat = exec.agent?.session.id;
1006
- const worldId = chat === void 0 ? void 0 : authoredWorldOf(chat);
1007
- if (worldId === void 0) throw new Error("the import tools are available only in a world authoring chat");
1008
- const record = await worlds.find(worldId);
1009
- if (record === void 0) throw new Error(`the authored world "${worldId}" no longer exists`);
1010
- return {
1011
- id: worldId,
1012
- directory: record.directory
1013
- };
1014
- }
1015
- const toolNames = {
1016
- survey: SURVEY_TOOL,
1017
- build: BUILD_TOOL,
1018
- finish: FINISH_TOOL
1019
- };
1020
- /** 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. */
1021
- function importTools(worlds, settings, prompting) {
1022
- /** 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. */
1023
- const step = async (exec, work, next) => {
1024
- const world = await worldOf(worlds, exec);
1025
- const language = prompting.languages().get(world.id);
1026
- const text = (id, variables) => prompting.prompts().text(id, language, {
1027
- ...toolNames,
1028
- ...variables
1029
- });
1030
- const result = await work(contextOf(exec, settings(), text), world.directory);
1031
- return json({
1032
- ...result,
1033
- next: next(text, result)
1034
- });
1035
- };
1036
- return [
1037
- defineTool({
1038
- name: SURVEY_TOOL,
1039
- 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.",
1040
- parameters: { card: cardParameter },
1041
- output: output((value) => JSON.stringify(value, null, 1)),
1042
- isConcurrencySafe: () => false,
1043
- async execute(args, exec) {
1044
- checkedCard(args);
1045
- return await step(exec, survey, (text) => text("import/next-survey"));
1046
- }
1047
- }),
1048
- defineTool({
1049
- name: BUILD_TOOL,
1050
- 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.",
1051
- parameters: { card: cardParameter },
1052
- output: output((value) => JSON.stringify(value, null, 1)),
1053
- isConcurrencySafe: () => false,
1054
- async execute(args, exec) {
1055
- checkedCard(args);
1056
- return await step(exec, build, (text) => text("import/next-build"));
1057
- }
1058
- }),
1059
- defineTool({
1060
- name: FINISH_TOOL,
1061
- 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.",
1062
- parameters: { card: cardParameter },
1063
- output: output((value) => JSON.stringify(value, null, 1)),
1064
- isConcurrencySafe: () => false,
1065
- async execute(args, exec) {
1066
- checkedCard(args);
1067
- return await step(exec, finish, (text, result) => text(result.ok ? "import/finish-ok" : "import/finish-failed"));
1068
- }
1069
- })
1070
- ];
1071
- }
1072
- /** The procedure of an import turn, told to the chat in place of what it does for any other message. */
1073
- function importNotice(phase, text) {
1074
- if (phase === void 0) return text("import/notice-missing");
1075
- return text("import/notice", {
1076
- card: CARD,
1077
- phase,
1078
- done: phase === "done" ? text("import/notice-done") : ""
1079
- });
1080
- }
1081
- /** 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. */
1082
- function importHooks(ctx, worlds, prompting) {
1083
- const textFor = (worldId) => (id, variables) => prompting.prompts().text(id, worldId === void 0 ? void 0 : prompting.languages().get(worldId), {
1084
- ...toolNames,
1085
- ...variables
1086
- });
1087
- const imports = /* @__PURE__ */ new Map();
1088
- const objections = /* @__PURE__ */ new Map();
1089
- const authoring = (agent) => agent.session.header.origin === "subagent" || authoredWorldOf(agent.session.id) === void 0 ? void 0 : agent.session.id;
1090
- ctx.on("agent/pre-step", async (payload, next) => {
1091
- const decision = await next();
1092
- const chat = authoring(payload.agent);
1093
- if (decision.kind !== "enter" || payload.step !== 1 || chat === void 0) return decision;
1094
- const text = payload.messages.filter((message) => message.source.kind === "user").flatMap((message) => message.content.flatMap((block) => block.type === "text" ? [block.text] : [])).join("\n");
1095
- if (parseImportCommand(text) === void 0) return decision;
1096
- const worldId = authoredWorldOf(chat);
1097
- const record = worldId === void 0 ? void 0 : await worlds.find(worldId);
1098
- const phase = record === void 0 ? void 0 : await importPhase(record.directory);
1099
- if (phase !== void 0 && phase !== "done") {
1100
- imports.set(chat, payload.turn);
1101
- objections.delete(chat);
1102
- }
1103
- return {
1104
- ...decision,
1105
- messages: [...decision.messages, createUserMessage({
1106
- content: [{
1107
- type: "text",
1108
- text: importNotice(phase, textFor(worldId))
1109
- }],
1110
- source: { kind: "roleplay" }
1111
- })]
1112
- };
1113
- });
1114
- ctx.on("session/event", async (session, event) => {
1115
- if (event.type !== "turn/end" || session.header.origin === "subagent") return;
1116
- const worldId = authoredWorldOf(session.id);
1117
- const record = worldId === void 0 ? void 0 : await worlds.find(worldId);
1118
- if (record !== void 0) await endReview(record.directory);
1119
- });
1120
- ctx.on("agent/turn-stopping", async ({ agent, turn }) => {
1121
- const chat = authoring(agent);
1122
- if (chat === void 0 || imports.get(chat) !== turn) return;
1123
- const worldId = authoredWorldOf(chat);
1124
- const record = worldId === void 0 ? void 0 : await worlds.find(worldId);
1125
- const phase = record === void 0 ? void 0 : await importPhase(record.directory);
1126
- if (phase === void 0 || phase === "done") {
1127
- imports.delete(chat);
1128
- return;
1129
- }
1130
- const told = objections.get(chat) ?? 0;
1131
- if (told >= MAX_OBJECTIONS) {
1132
- imports.delete(chat);
1133
- return;
1134
- }
1135
- objections.set(chat, told + 1);
1136
- const next = phase === "ready" ? SURVEY_TOOL : phase === "surveyed" ? BUILD_TOOL : FINISH_TOOL;
1137
- agent.steer(createUserMessage({
1138
- content: [{
1139
- type: "text",
1140
- text: textFor(worldId)("import/steer", {
1141
- phase,
1142
- next,
1143
- card: CARD
1144
- })
1145
- }],
1146
- source: { kind: "roleplay" }
1147
- }));
1148
- });
1149
- }
1150
- //#endregion
1151
945
  //#region src/authoring.ts
1152
946
  const name = "@dsh-agentic-roleplay/core/authoring";
1153
947
  const inject = [
@@ -1174,6 +968,7 @@ function apply(ctx) {
1174
968
  for (const tool of worldPromptTools(reader)) ctx.tools.register(tool);
1175
969
  for (const tool of worldPackTools(reader)) ctx.tools.register(tool);
1176
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);
1177
972
  importHooks(ctx, ctx.roleplayWorlds, prompting);
1178
973
  const reviewing = async (chat) => {
1179
974
  const id = authoredWorldOf(chat);