@escapeaihq/client-core 0.12.0 → 0.49.0

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 (242) hide show
  1. package/README.md +32 -7
  2. package/dist/adapters/capabilities.d.ts +10 -0
  3. package/dist/adapters/capabilities.d.ts.map +1 -0
  4. package/dist/adapters/capabilities.js +2 -0
  5. package/dist/adapters/capabilities.js.map +1 -0
  6. package/dist/adapters/chatAdapter.d.ts +38 -0
  7. package/dist/adapters/chatAdapter.d.ts.map +1 -0
  8. package/dist/adapters/chatAdapter.js +88 -0
  9. package/dist/adapters/chatAdapter.js.map +1 -0
  10. package/dist/adapters/directorAdapter.d.ts +62 -0
  11. package/dist/adapters/directorAdapter.d.ts.map +1 -0
  12. package/dist/adapters/directorAdapter.js +71 -0
  13. package/dist/adapters/directorAdapter.js.map +1 -0
  14. package/dist/adapters/imageAdapter.d.ts +44 -0
  15. package/dist/adapters/imageAdapter.d.ts.map +1 -0
  16. package/dist/adapters/imageAdapter.js +73 -0
  17. package/dist/adapters/imageAdapter.js.map +1 -0
  18. package/dist/adapters/index.d.ts +47 -0
  19. package/dist/adapters/index.d.ts.map +1 -0
  20. package/dist/adapters/index.js +41 -0
  21. package/dist/adapters/index.js.map +1 -0
  22. package/dist/adapters/registry.d.ts +98 -0
  23. package/dist/adapters/registry.d.ts.map +1 -0
  24. package/dist/adapters/registry.js +106 -0
  25. package/dist/adapters/registry.js.map +1 -0
  26. package/dist/adapters/worldAdapter.d.ts +142 -0
  27. package/dist/adapters/worldAdapter.d.ts.map +1 -0
  28. package/dist/adapters/worldAdapter.js +164 -0
  29. package/dist/adapters/worldAdapter.js.map +1 -0
  30. package/dist/admin.d.ts +323 -0
  31. package/dist/admin.d.ts.map +1 -0
  32. package/dist/admin.js +198 -0
  33. package/dist/admin.js.map +1 -0
  34. package/dist/api.d.ts +1512 -4
  35. package/dist/api.d.ts.map +1 -1
  36. package/dist/api.js +820 -52
  37. package/dist/api.js.map +1 -1
  38. package/dist/audio.d.ts +33 -1
  39. package/dist/audio.d.ts.map +1 -1
  40. package/dist/audio.js +4 -1
  41. package/dist/audio.js.map +1 -1
  42. package/dist/castProjection.d.ts +35 -0
  43. package/dist/castProjection.d.ts.map +1 -0
  44. package/dist/castProjection.js +80 -0
  45. package/dist/castProjection.js.map +1 -0
  46. package/dist/conversation/binding.d.ts +77 -0
  47. package/dist/conversation/binding.d.ts.map +1 -0
  48. package/dist/conversation/binding.js +57 -0
  49. package/dist/conversation/binding.js.map +1 -0
  50. package/dist/conversation/controller.d.ts +209 -0
  51. package/dist/conversation/controller.d.ts.map +1 -0
  52. package/dist/conversation/controller.js +350 -0
  53. package/dist/conversation/controller.js.map +1 -0
  54. package/dist/conversation/effectiveModel.d.ts +62 -0
  55. package/dist/conversation/effectiveModel.d.ts.map +1 -0
  56. package/dist/conversation/effectiveModel.js +73 -0
  57. package/dist/conversation/effectiveModel.js.map +1 -0
  58. package/dist/conversation/index.d.ts +27 -0
  59. package/dist/conversation/index.d.ts.map +1 -0
  60. package/dist/conversation/index.js +27 -0
  61. package/dist/conversation/index.js.map +1 -0
  62. package/dist/conversation/participant.d.ts +66 -0
  63. package/dist/conversation/participant.d.ts.map +1 -0
  64. package/dist/conversation/participant.js +36 -0
  65. package/dist/conversation/participant.js.map +1 -0
  66. package/dist/conversation/presence.d.ts +56 -0
  67. package/dist/conversation/presence.d.ts.map +1 -0
  68. package/dist/conversation/presence.js +34 -0
  69. package/dist/conversation/presence.js.map +1 -0
  70. package/dist/conversation/presenceBus.d.ts +42 -0
  71. package/dist/conversation/presenceBus.d.ts.map +1 -0
  72. package/dist/conversation/presenceBus.js +81 -0
  73. package/dist/conversation/presenceBus.js.map +1 -0
  74. package/dist/conversation/presenceRegistry.d.ts +49 -0
  75. package/dist/conversation/presenceRegistry.d.ts.map +1 -0
  76. package/dist/conversation/presenceRegistry.js +59 -0
  77. package/dist/conversation/presenceRegistry.js.map +1 -0
  78. package/dist/conversation/roster.d.ts +72 -0
  79. package/dist/conversation/roster.d.ts.map +1 -0
  80. package/dist/conversation/roster.js +69 -0
  81. package/dist/conversation/roster.js.map +1 -0
  82. package/dist/conversation/rosterProjection.d.ts +52 -0
  83. package/dist/conversation/rosterProjection.d.ts.map +1 -0
  84. package/dist/conversation/rosterProjection.js +74 -0
  85. package/dist/conversation/rosterProjection.js.map +1 -0
  86. package/dist/conversation/router.d.ts +74 -0
  87. package/dist/conversation/router.d.ts.map +1 -0
  88. package/dist/conversation/router.js +58 -0
  89. package/dist/conversation/router.js.map +1 -0
  90. package/dist/conversation/runtime.d.ts +64 -0
  91. package/dist/conversation/runtime.d.ts.map +1 -0
  92. package/dist/conversation/runtime.js +172 -0
  93. package/dist/conversation/runtime.js.map +1 -0
  94. package/dist/conversation/types.d.ts +99 -0
  95. package/dist/conversation/types.d.ts.map +1 -0
  96. package/dist/conversation/types.js +13 -0
  97. package/dist/conversation/types.js.map +1 -0
  98. package/dist/conversation/voice.d.ts +185 -0
  99. package/dist/conversation/voice.d.ts.map +1 -0
  100. package/dist/conversation/voice.js +307 -0
  101. package/dist/conversation/voice.js.map +1 -0
  102. package/dist/graphSchema.d.ts +201 -13
  103. package/dist/graphSchema.d.ts.map +1 -1
  104. package/dist/graphSchema.js +258 -9
  105. package/dist/graphSchema.js.map +1 -1
  106. package/dist/http.d.ts +20 -0
  107. package/dist/http.d.ts.map +1 -0
  108. package/dist/http.js +57 -0
  109. package/dist/http.js.map +1 -0
  110. package/dist/index.d.ts +6 -0
  111. package/dist/index.d.ts.map +1 -1
  112. package/dist/index.js +9 -0
  113. package/dist/index.js.map +1 -1
  114. package/dist/platformAssistant.d.ts +155 -0
  115. package/dist/platformAssistant.d.ts.map +1 -0
  116. package/dist/platformAssistant.js +256 -0
  117. package/dist/platformAssistant.js.map +1 -0
  118. package/dist/reactor/chunkDistribution.d.ts +43 -0
  119. package/dist/reactor/chunkDistribution.d.ts.map +1 -0
  120. package/dist/reactor/chunkDistribution.js +85 -0
  121. package/dist/reactor/chunkDistribution.js.map +1 -0
  122. package/dist/reactor/index.d.ts +2 -0
  123. package/dist/reactor/index.d.ts.map +1 -1
  124. package/dist/reactor/index.js +5 -0
  125. package/dist/reactor/index.js.map +1 -1
  126. package/dist/reactor/longLiveCommandDispatcher.d.ts +103 -0
  127. package/dist/reactor/longLiveCommandDispatcher.d.ts.map +1 -0
  128. package/dist/reactor/longLiveCommandDispatcher.js +87 -0
  129. package/dist/reactor/longLiveCommandDispatcher.js.map +1 -0
  130. package/dist/reactor/scenesFromEnrichment.d.ts.map +1 -1
  131. package/dist/reactor/scenesFromEnrichment.js +2 -66
  132. package/dist/reactor/scenesFromEnrichment.js.map +1 -1
  133. package/dist/reactor/seedImageOrchestrator.d.ts +6 -9
  134. package/dist/reactor/seedImageOrchestrator.d.ts.map +1 -1
  135. package/dist/reactor/seedImageOrchestrator.js +12 -13
  136. package/dist/reactor/seedImageOrchestrator.js.map +1 -1
  137. package/dist/reactor/storyTimeline.d.ts +44 -0
  138. package/dist/reactor/storyTimeline.d.ts.map +1 -0
  139. package/dist/reactor/storyTimeline.js +95 -0
  140. package/dist/reactor/storyTimeline.js.map +1 -0
  141. package/dist/storyGraph.d.ts +70 -0
  142. package/dist/storyGraph.d.ts.map +1 -0
  143. package/dist/storyGraph.js +178 -0
  144. package/dist/storyGraph.js.map +1 -0
  145. package/dist/vision/VisionSourceController.d.ts +127 -0
  146. package/dist/vision/VisionSourceController.d.ts.map +1 -0
  147. package/dist/vision/VisionSourceController.js +284 -0
  148. package/dist/vision/VisionSourceController.js.map +1 -0
  149. package/dist/vision/index.d.ts +4 -0
  150. package/dist/vision/index.d.ts.map +1 -1
  151. package/dist/vision/index.js +2 -0
  152. package/dist/vision/index.js.map +1 -1
  153. package/dist/vision/sourceKinds.d.ts +65 -0
  154. package/dist/vision/sourceKinds.d.ts.map +1 -0
  155. package/dist/vision/sourceKinds.js +26 -0
  156. package/dist/vision/sourceKinds.js.map +1 -0
  157. package/dist/websocket.d.ts +92 -0
  158. package/dist/websocket.d.ts.map +1 -1
  159. package/dist/websocket.js.map +1 -1
  160. package/package.json +1 -1
  161. package/src/__tests__/adapters-capabilities.test.ts +46 -0
  162. package/src/__tests__/adapters-chat.test.ts +172 -0
  163. package/src/__tests__/adapters-director.test.ts +95 -0
  164. package/src/__tests__/adapters-image.test.ts +133 -0
  165. package/src/__tests__/adapters-platform.test.ts +95 -0
  166. package/src/__tests__/adapters-registry.test.ts +189 -0
  167. package/src/__tests__/adapters-world.test.ts +296 -0
  168. package/src/__tests__/admin.test.ts +312 -0
  169. package/src/__tests__/adventures-party.test.ts +219 -0
  170. package/src/__tests__/adventures.test.ts +282 -0
  171. package/src/__tests__/api-pollers.test.ts +4 -0
  172. package/src/__tests__/api.clip-lookup.test.ts +64 -0
  173. package/src/__tests__/api.list-images-filters.test.ts +103 -0
  174. package/src/__tests__/api.test.ts +553 -0
  175. package/src/__tests__/audio.test.ts +65 -0
  176. package/src/__tests__/cast.test.ts +95 -0
  177. package/src/__tests__/castProjection.test.ts +167 -0
  178. package/src/__tests__/conversation/binding.test.ts +62 -0
  179. package/src/__tests__/conversation/controller.test.ts +732 -0
  180. package/src/__tests__/conversation/effectiveModel.test.ts +169 -0
  181. package/src/__tests__/conversation/headlessRenderer.ts +46 -0
  182. package/src/__tests__/conversation/presenceBus.test.ts +236 -0
  183. package/src/__tests__/conversation/presenceRegistry.test.ts +73 -0
  184. package/src/__tests__/conversation/roster.test.ts +100 -0
  185. package/src/__tests__/conversation/rosterProjection.test.ts +142 -0
  186. package/src/__tests__/conversation/router.test.ts +81 -0
  187. package/src/__tests__/conversation/runtime.test.ts +206 -0
  188. package/src/__tests__/conversation/voice.test.ts +440 -0
  189. package/src/__tests__/documents.test.ts +360 -0
  190. package/src/__tests__/graphSchema.test.ts +218 -9
  191. package/src/__tests__/longLiveCommandDispatcher.test.ts +185 -0
  192. package/src/__tests__/monitoring.test.ts +162 -0
  193. package/src/__tests__/persona-generation.test.ts +352 -0
  194. package/src/__tests__/platformAssistant.test.ts +484 -0
  195. package/src/__tests__/reactor/storyTimeline.test.ts +186 -0
  196. package/src/__tests__/regeneration.test.ts +486 -0
  197. package/src/__tests__/stories.test.ts +171 -0
  198. package/src/__tests__/storyGraph.test.ts +134 -0
  199. package/src/__tests__/tools-wrappers.test.ts +272 -0
  200. package/src/__tests__/videoGraph.test.ts +94 -0
  201. package/src/__tests__/websocket.test.ts +263 -0
  202. package/src/adapters/capabilities.ts +24 -0
  203. package/src/adapters/chatAdapter.ts +114 -0
  204. package/src/adapters/directorAdapter.ts +126 -0
  205. package/src/adapters/imageAdapter.ts +100 -0
  206. package/src/adapters/index.ts +67 -0
  207. package/src/adapters/registry.ts +159 -0
  208. package/src/adapters/worldAdapter.ts +272 -0
  209. package/src/admin.ts +517 -0
  210. package/src/api.ts +2215 -95
  211. package/src/audio.ts +35 -0
  212. package/src/castProjection.ts +95 -0
  213. package/src/conversation/binding.ts +100 -0
  214. package/src/conversation/controller.ts +447 -0
  215. package/src/conversation/effectiveModel.ts +111 -0
  216. package/src/conversation/index.ts +27 -0
  217. package/src/conversation/participant.ts +71 -0
  218. package/src/conversation/presence.ts +60 -0
  219. package/src/conversation/presenceBus.ts +109 -0
  220. package/src/conversation/presenceRegistry.ts +83 -0
  221. package/src/conversation/roster.ts +111 -0
  222. package/src/conversation/rosterProjection.ts +98 -0
  223. package/src/conversation/router.ts +96 -0
  224. package/src/conversation/runtime.ts +188 -0
  225. package/src/conversation/types.ts +100 -0
  226. package/src/conversation/voice.ts +402 -0
  227. package/src/graphSchema.ts +396 -13
  228. package/src/http.ts +72 -0
  229. package/src/index.ts +9 -0
  230. package/src/platformAssistant.ts +347 -0
  231. package/src/reactor/chunkDistribution.ts +98 -0
  232. package/src/reactor/index.ts +5 -0
  233. package/src/reactor/longLiveCommandDispatcher.ts +156 -0
  234. package/src/reactor/scenesFromEnrichment.ts +11 -76
  235. package/src/reactor/seedImageOrchestrator.ts +12 -13
  236. package/src/reactor/storyTimeline.ts +127 -0
  237. package/src/storyGraph.ts +298 -0
  238. package/src/vision/VisionSourceController.ts +354 -0
  239. package/src/vision/__tests__/VisionSourceController.test.ts +719 -0
  240. package/src/vision/index.ts +13 -0
  241. package/src/vision/sourceKinds.ts +75 -0
  242. package/src/websocket.ts +99 -3
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Tests for `castToRoster` (R7.4) — the single DRY projection turning R5's
3
+ * one-call `getCast` cast-with-persona response + the active `AdventureState`
4
+ * into the `RosterParticipant[]` the `ConversationRuntime` / `presenceBus`
5
+ * consume. This replaces the client-side `entity_id ∈ storyCharacterEntityIds`
6
+ * join the Studio surface used to do across two stores.
7
+ *
8
+ * Runs in plain node (no DOM) — the projection is pure.
9
+ */
10
+
11
+ import { castToRoster } from '../../conversation/rosterProjection';
12
+ import { PresenceKind } from '../../conversation/participant';
13
+ import type { CastEntry } from '../../api';
14
+ import type { RosterParticipant } from '../../conversation/types';
15
+
16
+ /** Minimal CastEntry factory (only the fields the projection reads). */
17
+ function entry(
18
+ entityId: string,
19
+ characterName: string,
20
+ persona: {
21
+ personaId: string;
22
+ name: string;
23
+ isDungeonMaster?: boolean;
24
+ characterImageId?: string | null;
25
+ } | null,
26
+ ): CastEntry {
27
+ return {
28
+ character: {
29
+ entityId,
30
+ name: characterName,
31
+ aliases: [],
32
+ scenes: [],
33
+ arcs: [],
34
+ relatedCharacterIds: [],
35
+ },
36
+ persona: persona
37
+ ? {
38
+ personaId: persona.personaId,
39
+ name: persona.name,
40
+ chatModelId: 'model-1',
41
+ characterImageId: persona.characterImageId ?? null,
42
+ isDungeonMaster: persona.isDungeonMaster ?? false,
43
+ }
44
+ : null,
45
+ };
46
+ }
47
+
48
+ describe('castToRoster', () => {
49
+ it('puts the DM seat first, then party members in party order', () => {
50
+ const cast: CastEntry[] = [
51
+ entry('e-legolas', 'Legolas', { personaId: 'p-legolas', name: 'Legolas' }),
52
+ entry('e-aragorn', 'Aragorn', { personaId: 'p-aragorn', name: 'Aragorn' }),
53
+ ];
54
+ const roster = castToRoster({
55
+ cast,
56
+ dmPersona: { personaId: 'p-dm', name: 'Game Master' },
57
+ partyPersonaIds: ['p-aragorn', 'p-legolas'],
58
+ });
59
+
60
+ expect(roster.map((r) => r.personaId)).toEqual([
61
+ 'p-dm',
62
+ 'p-aragorn',
63
+ 'p-legolas',
64
+ ]);
65
+ expect(roster[0]).toMatchObject<Partial<RosterParticipant>>({
66
+ personaId: 'p-dm',
67
+ isDm: true,
68
+ displayName: 'Game Master',
69
+ presence: PresenceKind.PRESENCE_FACE_2D,
70
+ });
71
+ });
72
+
73
+ it('resolves party display name + entityId from the cast projection', () => {
74
+ const cast: CastEntry[] = [
75
+ entry('e-aragorn', 'Aragorn', { personaId: 'p-aragorn', name: 'Strider' }),
76
+ ];
77
+ const roster = castToRoster({
78
+ cast,
79
+ dmPersona: { personaId: 'p-dm', name: 'DM' },
80
+ partyPersonaIds: ['p-aragorn'],
81
+ });
82
+ const member = roster.find((r) => r.personaId === 'p-aragorn');
83
+ expect(member).toMatchObject<Partial<RosterParticipant>>({
84
+ personaId: 'p-aragorn',
85
+ isDm: false,
86
+ // The persona's display name wins (what the transcript pill shows).
87
+ displayName: 'Strider',
88
+ entityId: 'e-aragorn',
89
+ presence: PresenceKind.PRESENCE_FACE_2D,
90
+ });
91
+ });
92
+
93
+ it('includes a party member even when absent from the cast (defensive)', () => {
94
+ // A removed/foreign persona id still in partyPersonaIds must not vanish —
95
+ // the runtime needs a seat for every server-party member so the speaker can
96
+ // be staged. With no cast match the display name falls back to the id.
97
+ const roster = castToRoster({
98
+ cast: [],
99
+ dmPersona: { personaId: 'p-dm', name: 'DM' },
100
+ partyPersonaIds: ['p-ghost'],
101
+ });
102
+ expect(roster.map((r) => r.personaId)).toEqual(['p-dm', 'p-ghost']);
103
+ expect(roster.find((r) => r.personaId === 'p-ghost')).toMatchObject({
104
+ displayName: 'p-ghost',
105
+ isDm: false,
106
+ });
107
+ });
108
+
109
+ it('land-dark: a single-DM (empty party) adventure yields exactly one DM seat', () => {
110
+ const roster = castToRoster({
111
+ cast: [
112
+ entry('e-x', 'X', { personaId: 'p-x', name: 'X' }), // bound but not in party
113
+ ],
114
+ dmPersona: { personaId: 'p-dm', name: 'DM' },
115
+ partyPersonaIds: [],
116
+ });
117
+ expect(roster).toHaveLength(1);
118
+ expect(roster[0].personaId).toBe('p-dm');
119
+ expect(roster[0].isDm).toBe(true);
120
+ });
121
+
122
+ it('never lists the DM twice even if it appears in partyPersonaIds', () => {
123
+ const roster = castToRoster({
124
+ cast: [],
125
+ dmPersona: { personaId: 'p-dm', name: 'DM' },
126
+ partyPersonaIds: ['p-dm', 'p-other'],
127
+ });
128
+ expect(roster.map((r) => r.personaId)).toEqual(['p-dm', 'p-other']);
129
+ });
130
+
131
+ it('deduplicates a repeated party id (first occurrence wins)', () => {
132
+ const cast: CastEntry[] = [
133
+ entry('e-a', 'A', { personaId: 'p-a', name: 'A' }),
134
+ ];
135
+ const roster = castToRoster({
136
+ cast,
137
+ dmPersona: { personaId: 'p-dm', name: 'DM' },
138
+ partyPersonaIds: ['p-a', 'p-a'],
139
+ });
140
+ expect(roster.map((r) => r.personaId)).toEqual(['p-dm', 'p-a']);
141
+ });
142
+ });
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Tests for `conversation/router.ts` (R7.1) — the advisory routing helpers
3
+ * lifted verbatim from `web/app/chat-ws/adventureMode.ts`. The assertions are
4
+ * ported 1:1 from `web/app/chat-ws/__tests__/adventureMode.test.tsx` to prove
5
+ * regression parity with the pre-lift implementation.
6
+ *
7
+ * The client router is ADVISORY ONLY: the server
8
+ * `agents/orchestrator/persona_router.py` `resolve_addressed_persona` is
9
+ * authoritative. The client never routes a turn — it only renders the server's
10
+ * `turn_changed` frames (see the land-dark gate assertions below).
11
+ */
12
+
13
+ import {
14
+ turnChangedSpeakerId,
15
+ resolvePersonaPill,
16
+ advisoryPartyPersonaIds,
17
+ } from '../../conversation/router';
18
+
19
+ describe('turnChangedSpeakerId (M10 Phase 9b live speaker gate)', () => {
20
+ // This is the EXACT gate the /chat-ws handler funnels every incoming frame
21
+ // through before moving the active speaker (which drives the CharacterFace +
22
+ // voice swap). Proving it stays null without an active adventure proves no
23
+ // ordinary-chat frame can swap the speaker (land-dark).
24
+ const turnFrame = { type: 'turn_changed', personaId: 'aragorn' };
25
+
26
+ it('returns the personaId for a turn_changed frame while an adventure is active', () => {
27
+ expect(turnChangedSpeakerId(turnFrame, 'adv-1')).toBe('aragorn');
28
+ });
29
+
30
+ it('returns null for EVERY frame when there is no active adventure (land-dark)', () => {
31
+ expect(turnChangedSpeakerId(turnFrame, undefined)).toBeNull();
32
+ expect(turnChangedSpeakerId({ type: 'token', personaId: 'aragorn' }, undefined)).toBeNull();
33
+ });
34
+
35
+ it('returns null for a non-turn_changed frame even with an active adventure', () => {
36
+ expect(turnChangedSpeakerId({ type: 'token', personaId: 'aragorn' }, 'adv-1')).toBeNull();
37
+ expect(turnChangedSpeakerId({ type: 'tool_result' }, 'adv-1')).toBeNull();
38
+ });
39
+
40
+ it('returns null for a turn_changed frame with no/empty personaId (defensive)', () => {
41
+ expect(turnChangedSpeakerId({ type: 'turn_changed' }, 'adv-1')).toBeNull();
42
+ expect(turnChangedSpeakerId({ type: 'turn_changed', personaId: '' }, 'adv-1')).toBeNull();
43
+ });
44
+ });
45
+
46
+ describe('resolvePersonaPill (M10 Phase 9b per-message pill)', () => {
47
+ const personas = [
48
+ { persona_id: 'p-aria', name: 'Aria' },
49
+ { persona_id: 'p-dm', name: 'The Narrator' },
50
+ ];
51
+
52
+ it('resolves a present personaId to its display name', () => {
53
+ expect(resolvePersonaPill('p-aria', personas)).toEqual({ personaId: 'p-aria', name: 'Aria' });
54
+ });
55
+
56
+ it('returns null for undefined (every non-adventure assistant message ⇒ no pill, land-dark)', () => {
57
+ expect(resolvePersonaPill(undefined, personas)).toBeNull();
58
+ });
59
+
60
+ it('falls back to the raw id as the name when the persona is unresolvable', () => {
61
+ expect(resolvePersonaPill('p-ghost', personas)).toEqual({ personaId: 'p-ghost', name: 'p-ghost' });
62
+ });
63
+ });
64
+
65
+ describe('advisoryPartyPersonaIds (M10 Phase 9b send/hello advisory)', () => {
66
+ it('returns a copy of the party when an adventure is active and the party is non-empty', () => {
67
+ const party = ['p-aria', 'p-borin'];
68
+ const result = advisoryPartyPersonaIds('adv-1', party);
69
+ expect(result).toEqual(party);
70
+ expect(result).not.toBe(party); // defensive copy
71
+ });
72
+
73
+ it('returns undefined with no active adventure so the wire is unchanged (land-dark)', () => {
74
+ expect(advisoryPartyPersonaIds(undefined, ['p-aria'])).toBeUndefined();
75
+ });
76
+
77
+ it('returns undefined for an empty / absent party (M9 single-DM stays byte-identical)', () => {
78
+ expect(advisoryPartyPersonaIds('adv-1', [])).toBeUndefined();
79
+ expect(advisoryPartyPersonaIds('adv-1', undefined)).toBeUndefined();
80
+ });
81
+ });
@@ -0,0 +1,206 @@
1
+ /**
2
+ * Tests for `conversation/runtime.ts` (R7.1) — the framework-agnostic
3
+ * `ConversationRuntime` reducer that derives presentation state from the
4
+ * existing `ChatWsEvent` frames. No React, no DOM, no network: every test
5
+ * builds frames as plain objects and asserts the produced
6
+ * `ConversationRuntimeState`.
7
+ */
8
+
9
+ import type { ChatWsEvent } from '../../websocket';
10
+ import { ConversationRuntime } from '../../conversation/runtime';
11
+ import { PresenceKind } from '../../conversation/participant';
12
+ import type { RosterParticipant } from '../../conversation/types';
13
+
14
+ const DM: RosterParticipant = {
15
+ personaId: 'p-dm',
16
+ isDm: true,
17
+ displayName: 'The Narrator',
18
+ presence: PresenceKind.PRESENCE_FACE_2D,
19
+ };
20
+
21
+ const ARIA: RosterParticipant = {
22
+ personaId: 'p-aria',
23
+ isDm: false,
24
+ displayName: 'Aria',
25
+ entityId: 'e-aria',
26
+ presence: PresenceKind.PRESENCE_FACE_2D,
27
+ };
28
+
29
+ function token(content: string, personaId?: string): ChatWsEvent {
30
+ return personaId === undefined
31
+ ? { type: 'token', content }
32
+ : { type: 'token', content, personaId };
33
+ }
34
+
35
+ describe('ConversationRuntime — land-dark lone speaker (ordinary chat / M9 single-DM)', () => {
36
+ it('keeps activePersonaId = the bound persona through a token…done sequence with no turn_changed', () => {
37
+ const rt = new ConversationRuntime({ boundPersonaId: 'p-dm', participants: [DM] });
38
+
39
+ const frames: ChatWsEvent[] = [token('Hello'), token(' world'), { type: 'done' }];
40
+ for (const f of frames) {
41
+ rt.applyFrame(f);
42
+ expect(rt.getState().activePersonaId).toBe('p-dm');
43
+ expect(rt.getState().participants).toHaveLength(1);
44
+ }
45
+
46
+ const state = rt.getState();
47
+ expect(state.previousPersonaId).toBeNull();
48
+ expect(state.busy).toBe(false);
49
+ expect(state.staleAdventureState).toBe(false);
50
+ // No personaId on any frame ⇒ no pill (byte-identical ordinary-chat bubble).
51
+ expect(state.lastFinalizedPill).toBeNull();
52
+ });
53
+
54
+ it('NEVER moves the speaker off a turn_changed frame when no adventure is active (defensive land-dark)', () => {
55
+ const rt = new ConversationRuntime({ boundPersonaId: 'p-dm', participants: [DM] });
56
+ rt.applyFrame({ type: 'turn_changed', personaId: 'p-aria' });
57
+ expect(rt.getState().activePersonaId).toBe('p-dm');
58
+ expect(rt.getState().previousPersonaId).toBeNull();
59
+ });
60
+
61
+ it('does not set staleAdventureState for a mutation tool_result without an adventure (land-dark gate)', () => {
62
+ const rt = new ConversationRuntime({ boundPersonaId: 'p-dm', participants: [DM] });
63
+ rt.applyFrame({ type: 'tool_result', tool: 'advance_story' });
64
+ expect(rt.getState().staleAdventureState).toBe(false);
65
+ });
66
+
67
+ it('marks busy while streaming and clears it on done / error', () => {
68
+ const rt = new ConversationRuntime({ boundPersonaId: 'p-dm' });
69
+ rt.applyFrame(token('Hi'));
70
+ expect(rt.getState().busy).toBe(true);
71
+ rt.applyFrame({ type: 'done' });
72
+ expect(rt.getState().busy).toBe(false);
73
+
74
+ rt.applyFrame(token('again'));
75
+ expect(rt.getState().busy).toBe(true);
76
+ rt.applyFrame({ type: 'error', message: 'boom' });
77
+ expect(rt.getState().busy).toBe(false);
78
+ });
79
+ });
80
+
81
+ describe('ConversationRuntime — cue-chain multi-persona adventure', () => {
82
+ function makeAdventureRuntime(): ConversationRuntime {
83
+ return new ConversationRuntime({
84
+ boundPersonaId: 'p-dm',
85
+ adventureSessionId: 'adv-1',
86
+ participants: [DM, ARIA],
87
+ });
88
+ }
89
+
90
+ it('flips activePersonaId on the server turn_changed and finalizes the party pill', () => {
91
+ const rt = makeAdventureRuntime();
92
+
93
+ // DM streams first…
94
+ rt.applyFrame(token('The door creaks', 'p-dm'));
95
+ expect(rt.getState().activePersonaId).toBe('p-dm');
96
+ expect(rt.getState().inFlightPersonaId).toBe('p-dm');
97
+
98
+ // …server hands the turn to the party member…
99
+ rt.applyFrame({ type: 'turn_changed', personaId: 'p-aria', previousPersonaId: 'p-dm' });
100
+ expect(rt.getState().activePersonaId).toBe('p-aria');
101
+ expect(rt.getState().previousPersonaId).toBe('p-dm');
102
+
103
+ // …party member streams and finishes.
104
+ rt.applyFrame(token('I draw my blade', 'p-aria'));
105
+ rt.applyFrame({ type: 'done' });
106
+
107
+ const state = rt.getState();
108
+ expect(state.activePersonaId).toBe('p-aria');
109
+ expect(state.busy).toBe(false);
110
+ expect(state.inFlightPersonaId).toBeNull();
111
+ // The finalized message pill carries the party member, resolved to the
112
+ // roster display name.
113
+ expect(state.lastFinalizedPill).toEqual({ personaId: 'p-aria', name: 'Aria' });
114
+ });
115
+
116
+ it('ignores a turn_changed with an empty personaId (defensive — mirrors turnChangedSpeakerId)', () => {
117
+ const rt = makeAdventureRuntime();
118
+ rt.applyFrame({ type: 'turn_changed', personaId: '' });
119
+ expect(rt.getState().activePersonaId).toBe('p-dm');
120
+ });
121
+
122
+ it('falls back to the raw persona id for a pill whose persona is not in the roster', () => {
123
+ const rt = makeAdventureRuntime();
124
+ rt.applyFrame(token('…', 'p-ghost'));
125
+ rt.applyFrame({ type: 'done' });
126
+ expect(rt.getState().lastFinalizedPill).toEqual({ personaId: 'p-ghost', name: 'p-ghost' });
127
+ });
128
+
129
+ it('releaseTurn(personaId) hands the floor back ONLY when that persona holds the turn (R7.2 party-removal)', () => {
130
+ const rt = makeAdventureRuntime();
131
+ rt.applyFrame({ type: 'turn_changed', personaId: 'p-aria', previousPersonaId: 'p-dm' });
132
+ expect(rt.getState().activePersonaId).toBe('p-aria');
133
+
134
+ // Releasing a persona that does NOT hold the turn is a strict no-op.
135
+ const before = rt.getState();
136
+ rt.releaseTurn('p-dm');
137
+ expect(rt.getState()).toBe(before);
138
+
139
+ // Releasing the active speaker clears the floor (until the next server
140
+ // turn_changed) — mirrors the page's remove-party-member behavior.
141
+ rt.releaseTurn('p-aria');
142
+ expect(rt.getState().activePersonaId).toBeNull();
143
+ expect(rt.getState().previousPersonaId).toBe('p-aria');
144
+ });
145
+
146
+ it('sets staleAdventureState only for a DM mutation tool_result, cleared by acknowledgeAdventureState()', () => {
147
+ const rt = makeAdventureRuntime();
148
+
149
+ rt.applyFrame({ type: 'tool_result', tool: 'describe_current_scene' });
150
+ expect(rt.getState().staleAdventureState).toBe(false);
151
+
152
+ rt.applyFrame({ type: 'tool_result', tool: 'set_flag' });
153
+ expect(rt.getState().staleAdventureState).toBe(true);
154
+
155
+ rt.acknowledgeAdventureState();
156
+ expect(rt.getState().staleAdventureState).toBe(false);
157
+ });
158
+ });
159
+
160
+ describe('ConversationRuntime — observer semantics', () => {
161
+ it('notifies subscribers only on actual change, with a fresh immutable state object', () => {
162
+ const rt = new ConversationRuntime({ boundPersonaId: 'p-dm', participants: [DM] });
163
+ const seen: unknown[] = [];
164
+ rt.subscribe((s) => seen.push(s));
165
+
166
+ const before = rt.getState();
167
+ rt.applyFrame({ type: 'heartbeat' }); // no-op frame ⇒ no notification
168
+ expect(seen).toHaveLength(0);
169
+ expect(rt.getState()).toBe(before); // identical object when nothing changed
170
+
171
+ rt.applyFrame(token('Hi')); // busy flips ⇒ one notification, new object
172
+ expect(seen).toHaveLength(1);
173
+ expect(rt.getState()).not.toBe(before);
174
+
175
+ rt.applyFrame(token(' there')); // busy already true, no personaId ⇒ no change
176
+ expect(seen).toHaveLength(1);
177
+ });
178
+
179
+ it('unsubscribe stops notifications', () => {
180
+ const rt = new ConversationRuntime({ boundPersonaId: 'p-dm' });
181
+ let count = 0;
182
+ const unsubscribe = rt.subscribe(() => {
183
+ count += 1;
184
+ });
185
+ rt.applyFrame(token('a'));
186
+ expect(count).toBe(1);
187
+ unsubscribe();
188
+ rt.applyFrame({ type: 'done' });
189
+ expect(count).toBe(1);
190
+ });
191
+
192
+ it('a throwing listener does not break other listeners', () => {
193
+ const rt = new ConversationRuntime({ boundPersonaId: 'p-dm' });
194
+ const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => undefined);
195
+ let called = false;
196
+ rt.subscribe(() => {
197
+ throw new Error('listener bug');
198
+ });
199
+ rt.subscribe(() => {
200
+ called = true;
201
+ });
202
+ rt.applyFrame(token('a'));
203
+ expect(called).toBe(true);
204
+ errorSpy.mockRestore();
205
+ });
206
+ });