narraeon 0.5.0 → 0.5.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.
@@ -1,7 +1,7 @@
1
1
  import { effectiveFollowupDefinitions } from "../play/OrderedFollowups.js";
2
2
  import { legacyAuthorPrompts } from "../play/FileNativePlayPresetStore.js";
3
3
  import { builtinAuthorPrompts, authoringMechanics, } from "../../shared/ordered-author-prompts.js";
4
- import { builtinPlayPrompts } from "../../shared/ordered-play-prompts.js";
4
+ import { builtinPlayPrompts, } from "../../shared/ordered-play-prompts.js";
5
5
  import { parseOrderedPlayPrompts } from "../play/OrderedPlayPrompts.js";
6
6
  import { renderDocumentWritePosition } from "./WorldMaintenanceReport.js";
7
7
  import { parseDocument, stringify } from "yaml";
@@ -11,6 +11,7 @@ import { checkpointHistory, checkpointReplayBlocks, playerInputAppend, isPlayerR
11
11
  import { defaultAppLocale, } from "../../protocol/appPreferences.js";
12
12
  import { WorldDocumentStore, } from "../world/WorldDocumentStore.js";
13
13
  import { defaultRuntimeToolDefinitionStrategy, registeredRuntimeToolNames, runtimeToolsForNames, } from "./FileNativeToolRegistry.js";
14
+ import { stateDirectoryHandle } from "../world/StateDirectoryHandles.js";
14
15
  export class PromptCompilationError extends Error {
15
16
  code;
16
17
  details;
@@ -190,7 +191,11 @@ export class FileNativePromptCompiler {
190
191
  markdown: presetReference,
191
192
  },
192
193
  ];
194
+ if (entry.kind === "user" && !entry.body.trim())
195
+ continue;
193
196
  logicalMessages.push({
197
+ messageRole: entry.messageRole ?? "system",
198
+ promptId: entry.id,
194
199
  role: builtin?.required
195
200
  ? "runtime_system"
196
201
  : builtin?.id === "author.play-reference"
@@ -207,11 +212,12 @@ export class FileNativePromptCompiler {
207
212
  (input.modelBinding.provider === "anthropic_messages"
208
213
  ? "explicit_anthropic_blocks"
209
214
  : "provider_managed");
210
- const stableText = cacheStableText(logicalMessages);
215
+ const provider = mapProvider(input.modelBinding.provider, logicalMessages, cacheStrategy, true, input.playPreset.definition.mergeConsecutiveMessages ?? true);
216
+ const stableText = cacheStableText(logicalMessages, provider);
211
217
  const cache = stableCacheBoundary(stableText, tools, toolStrategy);
212
218
  return {
213
219
  logicalMessages,
214
- provider: mapProvider(input.modelBinding.provider, logicalMessages, cacheStrategy, true),
220
+ provider,
215
221
  tools: structuredClone(tools),
216
222
  toolUniverse: structuredClone(tools),
217
223
  toolStrategy,
@@ -290,7 +296,7 @@ export class FileNativePromptCompiler {
290
296
  requireFormat(hostFrame, "narraeon.host-frame/v1", "host frame");
291
297
  requireFormat(worldFrame, "narraeon.world-frame/v1", "world frame");
292
298
  const instructions = readWorldInstructions(worldFiles, worldFrame);
293
- const { coverage, blocks: context, maintenance, } = this.inspectWorldMaterials(effectiveInput.world);
299
+ const { coverage, blocks: context, maintenance, } = this.playMaterials(effectiveInput);
294
300
  const hostRoles = compileHostRoles(effectiveInput.hostBinding.files, hostFrame, instructions, context, coverage, this.#locale);
295
301
  const blocks = {
296
302
  ...hostRoles,
@@ -329,7 +335,7 @@ export class FileNativePromptCompiler {
329
335
  toolUniverse: structuredClone(tools),
330
336
  toolStrategy,
331
337
  coverage,
332
- maintenance,
338
+ ...(maintenance === undefined ? {} : { maintenance }),
333
339
  budget,
334
340
  cache: {
335
341
  ...cache,
@@ -363,11 +369,28 @@ export class FileNativePromptCompiler {
363
369
  compilePlayCallChain(input, binding) {
364
370
  const presetCompilation = this.compilePlayPreset(input, binding);
365
371
  const toolUniverse = presetCompilation.toolUniverse.filter(({ name }) => playCallChainToolNames.has(name));
366
- const logicalMessages = binding.definition.playPrompts === undefined
372
+ const selectedMessages = binding.definition.playPrompts === undefined
367
373
  ? playCallChainNarrativeGuidance(presetCompilation.bootstrap.logicalMessages, binding)
368
374
  : presetCompilation.bootstrap.logicalMessages;
369
- const provider = mapProvider(input.modelBinding.provider, logicalMessages, presetCompilation.bootstrap.cache.strategy, binding.definition.playPrompts !== undefined);
370
- const stableText = cacheStableText(logicalMessages);
375
+ // Instructions keep author order; initial reads follow as protocol tool results.
376
+ const logicalMessages = [
377
+ ...selectedMessages.filter(({ role }) => role !== "world_context" && role !== "player_input"),
378
+ ...selectedMessages.filter(({ role }) => role === "world_context"),
379
+ ...selectedMessages.filter(({ role }) => role === "player_input"),
380
+ ];
381
+ const provider = mapProvider(input.modelBinding.provider, logicalMessages.filter(({ role }) => role !== "world_context"), presetCompilation.bootstrap.cache.strategy, binding.definition.playPrompts !== undefined, binding.definition.mergeConsecutiveMessages ?? true);
382
+ provider.preload = {
383
+ callId: "runtime_initial_world_context",
384
+ markdown: logicalMessages
385
+ .filter(({ role }) => role === "world_context")
386
+ .map(({ markdown }) => markdown)
387
+ .join("\n\n"),
388
+ };
389
+ const lastInstructionRole = logicalMessages
390
+ .filter(({ role }) => role === "runtime_system" || role === "author_instruction")
391
+ .at(-1).role;
392
+ const strategy = presetCompilation.bootstrap.cache.strategy;
393
+ const stableText = cacheStableText(logicalMessages, binding.definition.playPrompts ? provider : undefined);
371
394
  const bootstrap = {
372
395
  ...structuredClone(presetCompilation.bootstrap),
373
396
  logicalMessages,
@@ -376,6 +399,22 @@ export class FileNativePromptCompiler {
376
399
  toolUniverse: structuredClone(toolUniverse),
377
400
  cache: {
378
401
  ...presetCompilation.bootstrap.cache,
402
+ breakpoints: strategy === "explicit_anthropic_blocks"
403
+ ? [
404
+ ...new Set([
405
+ ...(binding.definition.playPrompts === undefined
406
+ ? ["runtime_system", "author_instruction"]
407
+ : [lastInstructionRole]),
408
+ "world_context",
409
+ ]),
410
+ ]
411
+ : strategy === "explicit_cliproxyapi_message"
412
+ ? [
413
+ input.modelBinding.provider === "chat_completions"
414
+ ? "world_context"
415
+ : lastInstructionRole,
416
+ ]
417
+ : [],
379
418
  ...stableCacheBoundary(stableText, toolUniverse, presetCompilation.toolStrategy),
380
419
  },
381
420
  };
@@ -409,13 +448,25 @@ export class FileNativePromptCompiler {
409
448
  const files = snapshotFiles(effective.world.documentSnapshot);
410
449
  const frame = readYamlRecord(files["control/frame.yaml"], "world frame");
411
450
  const instructions = readWorldInstructions(files, frame);
412
- const materials = this.inspectWorldMaterials(effective.world);
451
+ const materials = this.playMaterials(effective);
413
452
  const logicalMessages = [];
453
+ let prompt;
414
454
  const append = (role, blocks) => {
415
455
  if (blocks.length > 0)
416
- logicalMessages.push({ role, blocks, markdown: joinBlocks(blocks) });
456
+ logicalMessages.push({
457
+ role,
458
+ blocks,
459
+ markdown: joinBlocks(blocks),
460
+ ...prompt,
461
+ });
417
462
  };
418
463
  for (const entry of parseOrderedPlayPrompts(binding.definition.playPrompts)) {
464
+ prompt = {
465
+ messageRole: entry.messageRole ?? "system",
466
+ promptId: entry.id,
467
+ };
468
+ if (entry.kind === "user" && !entry.body.trim())
469
+ continue;
419
470
  if (entry.kind === "world") {
420
471
  append("author_instruction", instructions);
421
472
  append("world_context", [
@@ -440,6 +491,7 @@ export class FileNativePromptCompiler {
440
491
  ]);
441
492
  }
442
493
  }
494
+ prompt = undefined;
443
495
  if (effective.playerInputPlacement === "bootstrap")
444
496
  append("player_input", [
445
497
  { source: "player:input", markdown: effective.playerInput },
@@ -452,17 +504,20 @@ export class FileNativePromptCompiler {
452
504
  (effective.modelBinding.provider === "anthropic_messages"
453
505
  ? "explicit_anthropic_blocks"
454
506
  : "provider_managed");
507
+ const provider = mapProvider(effective.modelBinding.provider, logicalMessages, strategy, true, binding.definition.mergeConsecutiveMessages ?? true);
455
508
  return {
456
509
  logicalMessages,
457
- provider: mapProvider(effective.modelBinding.provider, logicalMessages, strategy, true),
510
+ provider,
458
511
  tools,
459
512
  toolUniverse: structuredClone(tools),
460
513
  toolStrategy,
461
514
  coverage: materials.coverage,
462
- maintenance: materials.maintenance,
515
+ ...(materials.maintenance === undefined
516
+ ? {}
517
+ : { maintenance: materials.maintenance }),
463
518
  budget: disabledPromptBudget(effective),
464
519
  cache: {
465
- ...stableCacheBoundary(cacheStableText(logicalMessages), tools, toolStrategy),
520
+ ...stableCacheBoundary(cacheStableText(logicalMessages, provider), tools, toolStrategy),
466
521
  strategy,
467
522
  breakpoints: strategy === "explicit_anthropic_blocks"
468
523
  ? [
@@ -474,6 +529,66 @@ export class FileNativePromptCompiler {
474
529
  },
475
530
  };
476
531
  }
532
+ playMaterials(input) {
533
+ if (input.frozenWorldContext !== undefined) {
534
+ const frozen = input.frozenWorldContext;
535
+ return structuredClone({
536
+ blocks: frozen.logicalMessages
537
+ .filter(({ role }) => role === "world_context")
538
+ .flatMap(({ blocks }) => blocks)
539
+ .filter(({ source }) => source !== "runtime:world-placeholder/coverage" &&
540
+ source !== "runtime:builtin/runtime.coverage"),
541
+ coverage: frozen.coverage,
542
+ ...(frozen.maintenance === undefined
543
+ ? {}
544
+ : { maintenance: frozen.maintenance }),
545
+ });
546
+ }
547
+ const materials = this.inspectWorldMaterials(input.world);
548
+ const snapshot = input.world.documentSnapshot;
549
+ const declaredDirectories = materials.coverage
550
+ .filter(({ slot }) => slot === "catalog")
551
+ .map(({ source }) => source);
552
+ const directories = new Set([""]);
553
+ const pending = [""];
554
+ for (const directory of pending) {
555
+ let cursor = null;
556
+ do {
557
+ const result = snapshot.query({
558
+ kind: "catalog",
559
+ directory,
560
+ declaredDirectories,
561
+ includeRetired: true,
562
+ limit: 100,
563
+ cursor,
564
+ });
565
+ if (result.kind === "error")
566
+ throwQueryFailure(result, "directory index");
567
+ if (result.kind !== "catalog")
568
+ throw new PromptCompilationError("world_document_query_failed", "Invalid directory index query result");
569
+ for (const entry of result.entries) {
570
+ if (entry.kind !== "directory")
571
+ continue;
572
+ const directory = entry.logicalPath.slice(`${snapshot.logicalRoot}/`.length);
573
+ if (!directories.has(directory)) {
574
+ directories.add(directory);
575
+ pending.push(directory);
576
+ }
577
+ }
578
+ cursor = result.page.nextCursor;
579
+ } while (cursor !== null);
580
+ }
581
+ materials.blocks.unshift({
582
+ source: "runtime:initial-directories",
583
+ markdown: `${this.#locale === "zh-CN" ? "# 初始目录索引\n\n全部目录句柄,可直接使用;文档正文仍按需读取。" : "# Initial directory index\n\nAll directory handles, ready to use; read document bodies as needed."}\n${[
584
+ ...directories,
585
+ ]
586
+ .sort()
587
+ .map((directory) => `- ${stateDirectoryHandle(directory)}`)
588
+ .join("\n")}`,
589
+ });
590
+ return materials;
591
+ }
477
592
  /**
478
593
  * A play preset owns the editable story policy. Its bootstrap keeps only the
479
594
  * mechanical Runtime shell; narrative semantics remain author instructions.
@@ -659,8 +774,8 @@ function settingImprovementPresetReference(binding, locale, target) {
659
774
  {
660
775
  ...worldInstructionPlaceholder,
661
776
  markdown: locale === "zh-CN"
662
- ? "完整世界提示占位在这里连续展开世界指令和 frame 选定材料(不是整棵树);请通过本轮创作工具检查实际内容。"
663
- : "The complete world placeholder expands world instructions and frame-selected material continuously here, not the entire tree. Inspect actual content through the current authoring tools.",
777
+ ? "世界指令在此展开;frame 选定材料仅在全新游玩上下文中作为初始工具结果提供。请通过本轮创作工具检查实际内容。"
778
+ : "World instructions expand here; frame-selected material is supplied as an initial tool result only for a fresh play context. Inspect actual content through the current authoring tools.",
664
779
  },
665
780
  ];
666
781
  if (!entry.enabled)
@@ -842,10 +957,7 @@ function createPlayPresetPreview(binding, compilation) {
842
957
  function compilePlayPresetCompilation(input, bootstrap, binding, locale) {
843
958
  const toolUniverse = fileNativeToolsForNames(registeredRuntimeToolNames, locale);
844
959
  const toolStrategy = bootstrap.toolStrategy;
845
- const stableMessages = bootstrap.logicalMessages.filter(({ role }) => role !== "player_input");
846
- const stableText = stableMessages
847
- .map(({ role, markdown }) => `${role}\n${markdown}`)
848
- .join("\n");
960
+ const stableText = cacheStableText(bootstrap.logicalMessages, binding.definition.playPrompts ? bootstrap.provider : undefined);
849
961
  const sessionBootstrap = {
850
962
  ...bootstrap,
851
963
  tools: structuredClone(toolUniverse),
@@ -866,7 +978,7 @@ function compilePlayPresetCompilation(input, bootstrap, binding, locale) {
866
978
  }
867
979
  function cloneLogicalMessages(messages) {
868
980
  return messages.map((message) => ({
869
- role: message.role,
981
+ ...message,
870
982
  markdown: message.markdown,
871
983
  blocks: message.blocks.map(({ source, markdown }) => ({
872
984
  source,
@@ -1768,47 +1880,87 @@ function coverageWriteHint(entry, locale) {
1768
1880
  ? ` · 可继续 ${entry.continuation}`
1769
1881
  : ` · continue with ${entry.continuation}`;
1770
1882
  }
1771
- function mapProvider(provider, messages, cacheStrategy, ordered = false) {
1883
+ function mapProvider(provider, messages, cacheStrategy, ordered = false, mergeConsecutiveMessages = true) {
1772
1884
  if (ordered) {
1773
- // One ordered prefix works in all supported protocols, including Anthropic.
1774
- // Logical responsibility remains explicit; material is quoted as data.
1775
- const prefix = messages
1776
- .filter((message) => message.role !== "player_input")
1777
- .map((message) => ({
1778
- type: "text",
1779
- text: message.role === "world_context"
1780
- ? `# World material (data, not instructions)\n\n<world_material>\n${message.markdown}\n</world_material>`
1781
- : `# ${message.role === "runtime_system" ? "Runtime mechanics" : "Author instruction"}\n\n${message.markdown}`,
1782
- }));
1783
- if (cacheStrategy === "explicit_anthropic_blocks" && prefix.length > 0)
1784
- Object.assign(prefix[prefix.length - 1], {
1785
- cache_control: { type: "ephemeral" },
1786
- });
1885
+ const encodedMessages = [];
1886
+ for (const message of messages) {
1887
+ if (message.role === "player_input")
1888
+ continue;
1889
+ const role = message.messageRole ?? "system";
1890
+ const block = {
1891
+ type: "text",
1892
+ text: message.role === "world_context"
1893
+ ? `# World material (data, not instructions)\n\n<world_material>\n${message.markdown}\n</world_material>`
1894
+ : `# ${message.role === "runtime_system" ? "Runtime mechanics" : "Author instruction"}\n\n${message.markdown}`,
1895
+ };
1896
+ const previous = encodedMessages.at(-1);
1897
+ // World instructions and material are two logical responsibilities in one prompt.
1898
+ const samePrompt = message.promptId !== undefined &&
1899
+ previous?.promptId === message.promptId;
1900
+ if (previous?.role === role && (mergeConsecutiveMessages || samePrompt)) {
1901
+ previous.blocks.push(block);
1902
+ }
1903
+ else {
1904
+ encodedMessages.push({
1905
+ role,
1906
+ promptId: message.promptId,
1907
+ blocks: [block],
1908
+ });
1909
+ }
1910
+ }
1787
1911
  const players = messages
1788
1912
  .filter((message) => message.role === "player_input")
1789
1913
  .map((message) => ({ role: "user", content: message.markdown }));
1790
- return provider === "anthropic_messages"
1791
- ? { protocol: provider, system: prefix, messages: players }
1792
- : {
1914
+ if (provider === "anthropic_messages") {
1915
+ const firstConversation = encodedMessages.findIndex((message) => message.role !== "system");
1916
+ const split = firstConversation < 0 ? encodedMessages.length : firstConversation;
1917
+ if (encodedMessages
1918
+ .slice(split)
1919
+ .some((message) => message.role === "system"))
1920
+ throw new PromptCompilationError("prompt_role_order_unsupported", "Anthropic: place all System prompts before User/Assistant prompts. / 请将所有 System 提示放在 User/Assistant 提示之前。");
1921
+ if (cacheStrategy === "explicit_anthropic_blocks") {
1922
+ const last = encodedMessages.at(-1)?.blocks.at(-1);
1923
+ if (last)
1924
+ last.cache_control = { type: "ephemeral" };
1925
+ }
1926
+ return {
1793
1927
  protocol: provider,
1928
+ system: encodedMessages
1929
+ .slice(0, split)
1930
+ .flatMap((message) => message.blocks),
1794
1931
  messages: [
1795
- {
1796
- role: "system",
1797
- content: prefix.map((block) => block.text).join("\n\n"),
1798
- },
1932
+ ...encodedMessages.slice(split).map((message) => ({
1933
+ role: message.role,
1934
+ content: message.blocks,
1935
+ })),
1799
1936
  ...players,
1800
1937
  ],
1801
1938
  };
1939
+ }
1940
+ return {
1941
+ protocol: provider,
1942
+ messages: [
1943
+ ...encodedMessages.map((message) => ({
1944
+ role: message.role,
1945
+ content: message.blocks.map((block) => block.text).join("\n\n"),
1946
+ })),
1947
+ ...players,
1948
+ ],
1949
+ };
1802
1950
  }
1803
1951
  const byRole = Object.fromEntries(messages.map((message) => [message.role, message.markdown]));
1804
1952
  const userBlocks = [
1805
- {
1806
- type: "text",
1807
- text: byRole.world_context,
1808
- ...(cacheStrategy === "provider_managed"
1809
- ? {}
1810
- : { cache_control: { type: "ephemeral" } }),
1811
- },
1953
+ ...(byRole.world_context === undefined
1954
+ ? []
1955
+ : [
1956
+ {
1957
+ type: "text",
1958
+ text: byRole.world_context,
1959
+ ...(cacheStrategy === "provider_managed"
1960
+ ? {}
1961
+ : { cache_control: { type: "ephemeral" } }),
1962
+ },
1963
+ ]),
1812
1964
  ...(typeof byRole.player_input === "string" &&
1813
1965
  byRole.player_input.length > 0
1814
1966
  ? [{ type: "text", text: byRole.player_input }]
@@ -1829,7 +1981,7 @@ function mapProvider(provider, messages, cacheStrategy, ordered = false) {
1829
1981
  cache_control: { type: "ephemeral" },
1830
1982
  },
1831
1983
  ],
1832
- messages: [{ role: "user", content: userBlocks }],
1984
+ messages: userBlocks.length === 0 ? [] : [{ role: "user", content: userBlocks }],
1833
1985
  };
1834
1986
  }
1835
1987
  return {
@@ -1839,7 +1991,9 @@ function mapProvider(provider, messages, cacheStrategy, ordered = false) {
1839
1991
  role: "system",
1840
1992
  content: `# Runtime System\n\n${byRole.runtime_system}\n\n# Author Instruction\n\n${byRole.author_instruction}`,
1841
1993
  },
1842
- { role: "user", content: userBlocks },
1994
+ ...(userBlocks.length === 0
1995
+ ? []
1996
+ : [{ role: "user", content: userBlocks }]),
1843
1997
  ],
1844
1998
  };
1845
1999
  }
@@ -1917,7 +2071,14 @@ function joinBlocks(blocks) {
1917
2071
  function utf8Bytes(value) {
1918
2072
  return new TextEncoder().encode(value).byteLength;
1919
2073
  }
1920
- function cacheStableText(messages) {
2074
+ function cacheStableText(messages, provider) {
2075
+ if (provider) {
2076
+ const playerCount = messages.filter((message) => message.role === "player_input").length;
2077
+ return JSON.stringify({
2078
+ ...provider,
2079
+ messages: provider.messages.slice(0, provider.messages.length - playerCount),
2080
+ });
2081
+ }
1921
2082
  return messages
1922
2083
  .filter(({ role }) => role !== "player_input")
1923
2084
  .map(({ role, markdown }) => `${role}\n${markdown}`)
@@ -209,24 +209,24 @@ function createToolDefinitions(locale) {
209
209
  const toolDescriptions = {
210
210
  en: {
211
211
  world_checkpoint: "Declare that the necessary state maintenance for this turn is complete and suggest starting a fresh context. No arguments. Registration becomes effective only when this turn’s final tool-free response commits, including that response in the checkpoint. This is your declaration, not Runtime verification or an automatic context switch. Further tool calls are allowed before the final nonempty tool-free response.",
212
- state_list: "List document and state directory handles known to Runtime. parent must be @dir-/ for the state root or an @dir-* handle returned by an earlier state_list result; descend only through returned directory handles. Catalog directories declared by the frozen world frame remain listed and usable when empty. A cursor is valid only for the same state snapshot, parent, and limit. Pass returned document handles to context_read. This tool never lists committed history.",
212
+ state_list: "List document and state directory handles known to Runtime. parent must be @dir-/ for the state root or an @dir-* handle from the initial directory index or an earlier state_list result; no preliminary listing is needed for an already supplied handle. Catalog directories declared by the frozen world frame remain listed and usable when empty. A cursor is valid only for the same state snapshot, parent, and limit. Pass returned document handles to context_read. This tool never lists committed history.",
213
213
  history_list: "List handles for committed history messages known to Runtime. order must be newest_first or oldest_first. A cursor is valid only for the same committed-history snapshot, order, and limit. Pass returned @history-message-* handles to context_read. This tool never lists the mutable state directory.",
214
214
  context_search: "Search literal source text in state or history. Zero matches means only that the supplied text had no literal match; it does not prove that a world fact does not exist. Player-visible narrative must not recount the search process. within accepts only an @document or @dir-* handle returned by Runtime.",
215
215
  context_read: "Read the complete content of an @document, @node, or @history-message-* handle previously returned by Runtime. The read heading, metadata boundaries, body boundaries, scope, and completion marker in the result are not source text. set_metadata may include only the fields that must change; Runtime preserves omitted metadata fields. Update the writable body between the boundaries with locators. YAML bodies omit the $document technical header; Markdown bodies begin at the document's level-one heading. Results are never truncated or paginated. Never pass file paths, natural-language names, or an invented world/... value.",
216
216
  world_patch: 'Update a document that has been read precisely or whose complete body was injected with write authorization. target must be an @short-ref. When title, summary, or aliases are stale, use set_metadata and include only the title, summary, or aliases that must change. At least one is required; Runtime preserves omitted fields from the current candidate document. A YAML edit uses locator:{yaml:["field","child"]}, for example {op:"replace",locator:{yaml:["status"]},value:"new value"}. Locator segments address map keys or a zero-based array index; remove deletes exactly the existing key or array item at its locator. A YAML value may reference a whole document only as {$ref:"@short-ref"}; the short reference must come from a Runtime list, read, or create result. Never invent a document id. If no handle is available, use ordinary text when semantically correct or call world_create first and use its returned @short-ref. To add an item to the end of an existing sequence, use append with the locator pointing to that sequence; add creates only a map key or list index that does not exist. A Markdown locator excludes the document level-one heading: {markdown:["Responsibilities"]} points exactly to ## Responsibilities, and replace_section.markdown must begin with the same heading at the same level. Use replace_preamble for text after the level-one heading and before the first level-two heading; send only that text. Use replace_body to replace the whole Markdown body and retain the original level-one heading. Success reports only whether the document changed and does not echo the body. Call context_read again only when a later decision depends on Runtime\'s exact serialized body or current metadata. Do not use path, JSON Pointer, set, or a file name.',
217
- world_create: 'Create a document inside a Runtime-known state directory. parent must be an @dir-* handle listed by state_list, such as @dir-/characters; catalog directories declared by the frozen world frame remain available when empty, and @dir-/ is the root. A syntactically valid but unknown directory is rejected. refHint is a lowercase ASCII short-reference hint, not a world/ path. A YAML body may reference a whole document only as {$ref:"@short-ref"}, and the short reference must come from a Runtime list, read, or create result. Never invent a document id.',
217
+ world_create: 'Create a document inside a Runtime-known state directory. parent must be an @dir-* handle supplied by the initial directory index or state_list, such as @dir-/characters; catalog directories declared by the frozen world frame remain available when empty, and @dir-/ is the root. A syntactically valid but unknown directory is rejected. refHint is a lowercase ASCII short-reference hint, not a world/ path. A YAML body may reference a whole document only as {$ref:"@short-ref"}, and the short reference must come from a Runtime list, read, or create result. Never invent a document id.',
218
218
  world_retire: "Retire or restore an exact state document without deleting its narrative record. target must be an @short-ref that was read precisely or injected completely. retired:true removes the document from future frame catalog injection but keeps it listed, searchable, readable, referenceable, and restorable; retired:false restores catalog participation. This changes catalog participation, not explicit full-document injection or character activity; use world_patch for ordinary location or field changes.",
219
219
  artifact_emit: "Submit an artifact declared in advance for this follow-up request. Supply only the output name and payload; Runtime fixes the channel, key, content type, renderer, retention policy, and authority meaning in the contract.",
220
220
  artifact_clear: "Clear the projection of an artifact that this follow-up declared in advance and explicitly allows to be cleared. The call cannot choose a channel, key, or any other output contract.",
221
221
  },
222
222
  "zh-CN": {
223
223
  world_checkpoint: "声明本轮必要状态已整理完成,并建议玩家在此开启全新上下文。参数为空对象。登记仅在本轮最终无工具响应提交后生效,检查点包含该响应。这是 AI 的整理声明,不是 Runtime 完整性认证,也不会自动切换上下文。最终非空且无工具调用的响应之前,仍可继续调用工具。",
224
- state_list: "列出 Runtime 已知的文档与状态目录句柄。状态根目录的 parent 使用 @dir-/;继续下级时只能使用先前 state_list 结果返回的 @dir-*。冻结世界 frame 声明的 catalog 目录即使为空,也会继续列出并可用。cursor 只对同一状态快照、parent 和 limit 有效。返回的文档句柄交给 context_read;本工具不列出已提交历史。",
224
+ state_list: "列出 Runtime 已知的文档与状态目录句柄。状态根目录的 parent 使用 @dir-/;继续下级时使用初始目录索引或 state_list 返回的 @dir-*;已有句柄无需再次列目录确认。冻结世界 frame 声明的 catalog 目录即使为空,也会继续列出并可用。cursor 只对同一状态快照、parent 和 limit 有效。返回的文档句柄交给 context_read;本工具不列出已提交历史。",
225
225
  history_list: "列出 Runtime 已知的已提交历史消息句柄。order 必须是 newest_first 或 oldest_first;cursor 只对同一历史快照、order 和 limit 有效。返回的 @history-message-* 交给 context_read;本工具不列出可变状态目录。",
226
226
  context_search: "在 state 或 history 的原文字面中搜索。0 命中只表示给定文本没有字面命中,不证明世界事实不存在;玩家可见叙事不得复述搜索过程。within 只能使用 Runtime 已返回的 @文档或 @dir-* 句柄。",
227
227
  context_read: "完整读取 Runtime 先前返回的 @文档、@节点或 @history-message-* 句柄。结果中的读取标题、元信息边界、正文边界、范围和完整标记不属于源文;set_metadata 可以只提交需要改变的字段,Runtime 会保留未提供的元数据字段。中间的可写正文用 locator 更新。YAML 正文不含 $document 技术头,Markdown 正文从文档 # 一级标题开始。结果不会截断或分页。不要传文件路径、自然语言名称或自行拼造的 world/...。",
228
228
  world_patch: '更新已精确读取,或已随写入资格注入完整正文的文档。target 必须使用 @短引用;文档 title、summary 或 aliases 过时时使用 set_metadata,只提供需要改变的 title、summary 或 aliases。至少提供一项;未提供的字段由 Runtime 从当前候选文档保留。YAML edit 使用 locator:{yaml:["字段","子字段"]},例如 {op:"replace",locator:{yaml:["情况"]},value:"新值"}。locator 的每一段可以定位 map key 或从 0 开始的数组下标;remove 只删除 locator 精确指向的已有键或数组项。YAML value 需要引用整份文档时只能写 {$ref:"@短引用"},短引用必须来自 Runtime 的 list、read 或 create 结果;不得自行编造文档 id。没有可用句柄时,根据语义使用普通文本,或先 world_create 后使用其返回的 @短引用。向已存在的 sequence 末尾加一项必须使用 append 并把 locator 指向该 sequence;add 只创建尚不存在的 map key 或 list index。Markdown locator 不包含文档 # 一级标题:{markdown:["职责"]} 精确指向 ## 职责,replace_section.markdown 必须从同级同名标题开始。修改 # 标题下、第一个 ## 前的文字用 replace_preamble(只传该段文字);替换整个 Markdown 正文用 replace_body,且 markdown 必须保留原 # 一级标题。成功结果只报告文档是否发生变化,不回显正文;只有后续决策依赖 Runtime 序列化后的精确正文或当前元数据时才重新 context_read。不要使用 path、JSON Pointer、set 或文件名。',
229
- world_create: '在 Runtime 已知的状态目录中创建文档。parent 必须是 state_list 列出的 @dir-*,例如 @dir-/characters;冻结世界 frame 声明的 catalog 目录为空时仍然可用,根目录使用 @dir-/。只有句柄形式正确但 Runtime 并不知道的目录会被拒绝。refHint 是小写 ASCII 短引用,不要传 world/ 路径。YAML body 需要引用整份文档时只能写 {$ref:"@短引用"},且短引用必须来自 Runtime 的 list、read 或 create 结果;不得自行编造文档 id。',
229
+ world_create: '在 Runtime 已知的状态目录中创建文档。parent 必须是初始目录索引或 state_list 列出的 @dir-*,例如 @dir-/characters;冻结世界 frame 声明的 catalog 目录为空时仍然可用,根目录使用 @dir-/。只有句柄形式正确但 Runtime 并不知道的目录会被拒绝。refHint 是小写 ASCII 短引用,不要传 world/ 路径。YAML body 需要引用整份文档时只能写 {$ref:"@短引用"},且短引用必须来自 Runtime 的 list、read 或 create 结果;不得自行编造文档 id。',
230
230
  world_retire: "让一份精确状态文档退役或恢复,而不销毁叙事记录。target 必须是已精确读取或已完整注入的 @短引用。retired:true 会让文档不再进入后续 frame catalog 注入,但仍可列出、搜索、读取、引用和恢复;retired:false 恢复 catalog 参与。它只改变 catalog 参与,不取消显式全文注入、不冻结人物活动;普通位置或字段变化仍用 world_patch。",
231
231
  artifact_emit: "提交本次后置请求预先声明的产物。只能传 output name 与 payload;频道、key、内容类型、renderer、保存策略和权威含义由 Runtime contract 固定。",
232
232
  artifact_clear: "清除本次后置请求预先声明且允许 clear 的产物投影;不能选择频道、key 或其他输出契约。",
@@ -1,3 +1,4 @@
1
+ import { isPromptMessageRole } from "../../shared/ordered-play-prompts.js";
1
2
  import { isWorldExtensionControl } from "../../protocol/worldExtensions.js";
2
3
  import { isWorldPromptMaintenance } from "../../protocol/worldMaintenance.js";
3
4
  /** Canonical validators for saved compiler output. Validate without projecting or rebuilding it. */
@@ -167,7 +168,11 @@ export function validLogicalMessages(value) {
167
168
  }
168
169
  function validLogicalMessage(value) {
169
170
  return (isRecord(value) &&
170
- hasExactKeys(value, ["role", "markdown", "blocks"]) &&
171
+ hasExactKeys(value, ["role", "markdown", "blocks"], ["messageRole", "promptId"]) &&
172
+ (value.messageRole === undefined ||
173
+ isPromptMessageRole(value.messageRole)) &&
174
+ (value.promptId === undefined ||
175
+ (typeof value.promptId === "string" && value.promptId.length > 0)) &&
171
176
  [
172
177
  "runtime_system",
173
178
  "author_instruction",
@@ -185,14 +190,19 @@ function validLogicalMessage(value) {
185
190
  }
186
191
  function validPromptProvider(value) {
187
192
  return (isRecord(value) &&
188
- hasExactKeys(value, ["protocol", "messages"], ["system"]) &&
193
+ hasExactKeys(value, ["protocol", "messages"], ["system", "preload"]) &&
194
+ (value.preload === undefined ||
195
+ (isRecord(value.preload) &&
196
+ hasExactKeys(value.preload, ["callId", "markdown"]) &&
197
+ value.preload.callId === "runtime_initial_world_context" &&
198
+ typeof value.preload.markdown === "string")) &&
189
199
  validProviderKind(value.protocol) &&
190
200
  (value.system === undefined ||
191
201
  (Array.isArray(value.system) && value.system.every(validSystemBlock))) &&
192
202
  Array.isArray(value.messages) &&
193
203
  value.messages.every((message) => isRecord(message) &&
194
204
  hasExactKeys(message, ["role", "content"]) &&
195
- (message.role === "system" || message.role === "user") &&
205
+ isPromptMessageRole(message.role) &&
196
206
  Object.hasOwn(message, "content")));
197
207
  }
198
208
  function validSystemBlock(value) {
@@ -0,0 +1,31 @@
1
+ export function stateDirectoryHandle(directory) {
2
+ if (directory === "")
3
+ return "@dir-/";
4
+ const encoded = directory.split("/").map(encodeURIComponent).join("/");
5
+ return `@dir-/${encoded}`;
6
+ }
7
+ export function parseStateDirectoryHandle(handle) {
8
+ if (handle === "@dir-/")
9
+ return "";
10
+ if (!handle.startsWith("@dir-/") || handle.length === "@dir-/".length)
11
+ return null;
12
+ try {
13
+ const directory = handle
14
+ .slice("@dir-/".length)
15
+ .split("/")
16
+ .map(decodeURIComponent)
17
+ .join("/");
18
+ if (directory
19
+ .split("/")
20
+ .some((segment) => segment === "" ||
21
+ segment === "." ||
22
+ segment === ".." ||
23
+ segment.includes("\\")) ||
24
+ stateDirectoryHandle(directory) !== handle)
25
+ return null;
26
+ return directory;
27
+ }
28
+ catch {
29
+ return null;
30
+ }
31
+ }
@@ -1,3 +1,4 @@
1
+ import { validLogicalMessages } from "../prompt/PromptCompilationCodec.js";
1
2
  import { isPackageFollowupControlPath } from "../content/PackageFollowups.js";
2
3
  import { validAuthoringRequests, } from "../authoring/AuthoringRequestSnapshot.js";
3
4
  import { createHash, randomUUID } from "node:crypto";
@@ -446,25 +447,6 @@ function validPromptCompilation(value) {
446
447
  isRecord(value.budget) &&
447
448
  isRecord(value.cache));
448
449
  }
449
- function validLogicalMessages(value) {
450
- return (Array.isArray(value) &&
451
- value.every((message) => isRecord(message) &&
452
- hasExactKeys(message, ["role", "markdown", "blocks"]) &&
453
- [
454
- "runtime_system",
455
- "author_instruction",
456
- "world_context",
457
- "player_input",
458
- "assistant",
459
- "tool",
460
- ].includes(String(message.role)) &&
461
- typeof message.markdown === "string" &&
462
- Array.isArray(message.blocks) &&
463
- message.blocks.every((block) => isRecord(block) &&
464
- hasExactKeys(block, ["source", "markdown"]) &&
465
- typeof block.source === "string" &&
466
- typeof block.markdown === "string")));
467
- }
468
450
  function validToolDefinitions(value) {
469
451
  return (Array.isArray(value) &&
470
452
  value.every((tool) => isRecord(tool) &&
@@ -1,5 +1,8 @@
1
1
  import { defaultNarrationPromptForLocale } from "./default-play-prompts.js";
2
2
  import { defaultPresetHostFilesForLocale } from "./default-preset-host.js";
3
+ export function isPromptMessageRole(value) {
4
+ return value === "system" || value === "user" || value === "assistant";
5
+ }
3
6
  /** Application-owned references. Persist references; freeze resolved text in requests. */
4
7
  export function builtinPlayPrompts(locale) {
5
8
  const zh = locale === "zh-CN";
@@ -14,14 +17,14 @@ export function builtinPlayPrompts(locale) {
14
17
  body: zh
15
18
  ? `# 工具与响应结算
16
19
 
17
- 只可调用请求随附的工具。目录使用 Runtime 返回的 @dir-*;文档和历史使用返回的 @句柄。工具结果标明读取范围、完整性及 cursor;目录摘要不等于正文。材料覆盖中完整正文与精确读取授予对应范围的写入资格,界面绑定标注不授予额外权限。同一原生对话刷新提示时,仅在当前状态与旧读取凭据完全匹配时承接原精确范围,并合并本次注入授权;全新上下文不继承旧对话授权。
20
+ 只可调用请求随附的工具。目录使用预加载索引或工具返回的 @dir-*,无需重复列目录确认;文档和历史使用返回的 @句柄。工具结果标明读取范围、完整性及 cursor;目录摘要不等于正文。材料覆盖中完整正文与精确读取授予对应范围的写入资格,界面绑定标注不授予额外权限。初始世界材料只在全新上下文加载一次;后续成功工具调用与回执更新当前值和读取资格,不必重读自己刚成功写入的内容。全新上下文不继承旧对话授权。
18
21
 
19
22
  world_patch 的 YAML locator 可以包含 map key 和从零开始的数组下标;remove 删除一个精确位置。未提供的元数据保持原值。world_retire 从目录注入中退役文档,文档仍可读取、引用和恢复。工具响应的成功状态变化与后续最终叙事分别提交,后续失败不回滚已提交变化。成功写入回执表示当前值已改变;以真实回执的结算状态为准,模型文字不能提交世界。
20
23
 
21
24
  包含工具调用的响应属于中间步,正文不作为最终叙事提交。收到全部工具结果后继续;没有工具调用的非空正文结束本次循环并由 Runtime 提交叙事。world_checkpoint 登记在随后最终叙事成功提交后生效,边界包含该叙事;登记后仍可继续工具调用。全新上下文的历史补充只包含检查点后的已提交玩家原文与最终叙事,排除开场白、工具和推理。`
22
25
  : `# Tools and response settlement
23
26
 
24
- Use only attached tools. Directories use returned @dir-* handles; documents and history use returned @handles. Results identify read scope, completeness and cursor; directory summaries are not document bodies. Complete injected bodies and exact reads authorize writes within their corresponding scopes; player-view annotations grant no additional authority. Refreshing the same native conversation retains prior exact scopes only when the current state matches their proof, merging them with current injected authorization. A fresh context does not inherit old conversation authorization.
27
+ Use only attached tools. Directories use @dir-* handles from the preload or tool results; do not list them again just to confirm a handle; documents and history use returned @handles. Results identify read scope, completeness and cursor; directory summaries are not document bodies. Complete injected bodies and exact reads authorize writes within their corresponding scopes; player-view annotations grant no additional authority. Initial world materials load once per fresh context. Later successful tool calls and receipts update current values and read scopes; do not re-read merely to confirm your own successful writes. A fresh context does not inherit old conversation authorization.
25
28
 
26
29
  YAML world_patch locators accept map keys and zero-based array indexes; remove deletes one exact location. Omitted metadata remains unchanged. world_retire removes a document from catalog injection while keeping it readable, referenceable and restorable. Successful tool-response state changes and later final narrative commit separately; later failure does not roll back committed changes. Successful write receipts change current values; the actual receipt determines settlement, not model prose.
27
30