@escapeaihq/client-core 0.12.0 → 0.42.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.
- package/README.md +31 -6
- package/dist/adapters/capabilities.d.ts +10 -0
- package/dist/adapters/capabilities.d.ts.map +1 -0
- package/dist/adapters/capabilities.js +2 -0
- package/dist/adapters/capabilities.js.map +1 -0
- package/dist/adapters/chatAdapter.d.ts +38 -0
- package/dist/adapters/chatAdapter.d.ts.map +1 -0
- package/dist/adapters/chatAdapter.js +88 -0
- package/dist/adapters/chatAdapter.js.map +1 -0
- package/dist/adapters/directorAdapter.d.ts +62 -0
- package/dist/adapters/directorAdapter.d.ts.map +1 -0
- package/dist/adapters/directorAdapter.js +71 -0
- package/dist/adapters/directorAdapter.js.map +1 -0
- package/dist/adapters/imageAdapter.d.ts +44 -0
- package/dist/adapters/imageAdapter.d.ts.map +1 -0
- package/dist/adapters/imageAdapter.js +73 -0
- package/dist/adapters/imageAdapter.js.map +1 -0
- package/dist/adapters/index.d.ts +47 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +41 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/registry.d.ts +98 -0
- package/dist/adapters/registry.d.ts.map +1 -0
- package/dist/adapters/registry.js +106 -0
- package/dist/adapters/registry.js.map +1 -0
- package/dist/adapters/worldAdapter.d.ts +142 -0
- package/dist/adapters/worldAdapter.d.ts.map +1 -0
- package/dist/adapters/worldAdapter.js +164 -0
- package/dist/adapters/worldAdapter.js.map +1 -0
- package/dist/admin.d.ts +200 -0
- package/dist/admin.d.ts.map +1 -0
- package/dist/admin.js +124 -0
- package/dist/admin.js.map +1 -0
- package/dist/api.d.ts +1185 -3
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +624 -51
- package/dist/api.js.map +1 -1
- package/dist/audio.d.ts +33 -1
- package/dist/audio.d.ts.map +1 -1
- package/dist/audio.js +4 -1
- package/dist/audio.js.map +1 -1
- package/dist/castProjection.d.ts +35 -0
- package/dist/castProjection.d.ts.map +1 -0
- package/dist/castProjection.js +80 -0
- package/dist/castProjection.js.map +1 -0
- package/dist/conversation/binding.d.ts +62 -0
- package/dist/conversation/binding.d.ts.map +1 -0
- package/dist/conversation/binding.js +51 -0
- package/dist/conversation/binding.js.map +1 -0
- package/dist/conversation/controller.d.ts +209 -0
- package/dist/conversation/controller.d.ts.map +1 -0
- package/dist/conversation/controller.js +342 -0
- package/dist/conversation/controller.js.map +1 -0
- package/dist/conversation/effectiveModel.d.ts +62 -0
- package/dist/conversation/effectiveModel.d.ts.map +1 -0
- package/dist/conversation/effectiveModel.js +73 -0
- package/dist/conversation/effectiveModel.js.map +1 -0
- package/dist/conversation/index.d.ts +27 -0
- package/dist/conversation/index.d.ts.map +1 -0
- package/dist/conversation/index.js +27 -0
- package/dist/conversation/index.js.map +1 -0
- package/dist/conversation/participant.d.ts +66 -0
- package/dist/conversation/participant.d.ts.map +1 -0
- package/dist/conversation/participant.js +36 -0
- package/dist/conversation/participant.js.map +1 -0
- package/dist/conversation/presence.d.ts +56 -0
- package/dist/conversation/presence.d.ts.map +1 -0
- package/dist/conversation/presence.js +34 -0
- package/dist/conversation/presence.js.map +1 -0
- package/dist/conversation/presenceBus.d.ts +42 -0
- package/dist/conversation/presenceBus.d.ts.map +1 -0
- package/dist/conversation/presenceBus.js +81 -0
- package/dist/conversation/presenceBus.js.map +1 -0
- package/dist/conversation/presenceRegistry.d.ts +49 -0
- package/dist/conversation/presenceRegistry.d.ts.map +1 -0
- package/dist/conversation/presenceRegistry.js +59 -0
- package/dist/conversation/presenceRegistry.js.map +1 -0
- package/dist/conversation/roster.d.ts +72 -0
- package/dist/conversation/roster.d.ts.map +1 -0
- package/dist/conversation/roster.js +69 -0
- package/dist/conversation/roster.js.map +1 -0
- package/dist/conversation/rosterProjection.d.ts +52 -0
- package/dist/conversation/rosterProjection.d.ts.map +1 -0
- package/dist/conversation/rosterProjection.js +74 -0
- package/dist/conversation/rosterProjection.js.map +1 -0
- package/dist/conversation/router.d.ts +74 -0
- package/dist/conversation/router.d.ts.map +1 -0
- package/dist/conversation/router.js +58 -0
- package/dist/conversation/router.js.map +1 -0
- package/dist/conversation/runtime.d.ts +64 -0
- package/dist/conversation/runtime.d.ts.map +1 -0
- package/dist/conversation/runtime.js +172 -0
- package/dist/conversation/runtime.js.map +1 -0
- package/dist/conversation/types.d.ts +99 -0
- package/dist/conversation/types.d.ts.map +1 -0
- package/dist/conversation/types.js +13 -0
- package/dist/conversation/types.js.map +1 -0
- package/dist/conversation/voice.d.ts +185 -0
- package/dist/conversation/voice.d.ts.map +1 -0
- package/dist/conversation/voice.js +307 -0
- package/dist/conversation/voice.js.map +1 -0
- package/dist/graphSchema.d.ts +201 -13
- package/dist/graphSchema.d.ts.map +1 -1
- package/dist/graphSchema.js +258 -9
- package/dist/graphSchema.js.map +1 -1
- package/dist/http.d.ts +20 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +57 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -1
- package/dist/platformAssistant.d.ts +155 -0
- package/dist/platformAssistant.d.ts.map +1 -0
- package/dist/platformAssistant.js +256 -0
- package/dist/platformAssistant.js.map +1 -0
- package/dist/reactor/chunkDistribution.d.ts +43 -0
- package/dist/reactor/chunkDistribution.d.ts.map +1 -0
- package/dist/reactor/chunkDistribution.js +85 -0
- package/dist/reactor/chunkDistribution.js.map +1 -0
- package/dist/reactor/index.d.ts +2 -0
- package/dist/reactor/index.d.ts.map +1 -1
- package/dist/reactor/index.js +5 -0
- package/dist/reactor/index.js.map +1 -1
- package/dist/reactor/longLiveCommandDispatcher.d.ts +103 -0
- package/dist/reactor/longLiveCommandDispatcher.d.ts.map +1 -0
- package/dist/reactor/longLiveCommandDispatcher.js +87 -0
- package/dist/reactor/longLiveCommandDispatcher.js.map +1 -0
- package/dist/reactor/scenesFromEnrichment.d.ts.map +1 -1
- package/dist/reactor/scenesFromEnrichment.js +2 -66
- package/dist/reactor/scenesFromEnrichment.js.map +1 -1
- package/dist/reactor/seedImageOrchestrator.d.ts +6 -9
- package/dist/reactor/seedImageOrchestrator.d.ts.map +1 -1
- package/dist/reactor/seedImageOrchestrator.js +12 -13
- package/dist/reactor/seedImageOrchestrator.js.map +1 -1
- package/dist/reactor/storyTimeline.d.ts +44 -0
- package/dist/reactor/storyTimeline.d.ts.map +1 -0
- package/dist/reactor/storyTimeline.js +95 -0
- package/dist/reactor/storyTimeline.js.map +1 -0
- package/dist/storyGraph.d.ts +70 -0
- package/dist/storyGraph.d.ts.map +1 -0
- package/dist/storyGraph.js +178 -0
- package/dist/storyGraph.js.map +1 -0
- package/dist/vision/VisionSourceController.d.ts +127 -0
- package/dist/vision/VisionSourceController.d.ts.map +1 -0
- package/dist/vision/VisionSourceController.js +284 -0
- package/dist/vision/VisionSourceController.js.map +1 -0
- package/dist/vision/index.d.ts +4 -0
- package/dist/vision/index.d.ts.map +1 -1
- package/dist/vision/index.js +2 -0
- package/dist/vision/index.js.map +1 -1
- package/dist/vision/sourceKinds.d.ts +65 -0
- package/dist/vision/sourceKinds.d.ts.map +1 -0
- package/dist/vision/sourceKinds.js +26 -0
- package/dist/vision/sourceKinds.js.map +1 -0
- package/dist/websocket.d.ts +73 -0
- package/dist/websocket.d.ts.map +1 -1
- package/dist/websocket.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/adapters-capabilities.test.ts +46 -0
- package/src/__tests__/adapters-chat.test.ts +172 -0
- package/src/__tests__/adapters-director.test.ts +95 -0
- package/src/__tests__/adapters-image.test.ts +133 -0
- package/src/__tests__/adapters-platform.test.ts +95 -0
- package/src/__tests__/adapters-registry.test.ts +189 -0
- package/src/__tests__/adapters-world.test.ts +296 -0
- package/src/__tests__/admin.test.ts +312 -0
- package/src/__tests__/adventures-party.test.ts +219 -0
- package/src/__tests__/adventures.test.ts +282 -0
- package/src/__tests__/api-pollers.test.ts +4 -0
- package/src/__tests__/api.clip-lookup.test.ts +64 -0
- package/src/__tests__/api.list-images-filters.test.ts +103 -0
- package/src/__tests__/api.test.ts +470 -0
- package/src/__tests__/audio.test.ts +65 -0
- package/src/__tests__/cast.test.ts +95 -0
- package/src/__tests__/castProjection.test.ts +167 -0
- package/src/__tests__/conversation/controller.test.ts +697 -0
- package/src/__tests__/conversation/effectiveModel.test.ts +169 -0
- package/src/__tests__/conversation/headlessRenderer.ts +46 -0
- package/src/__tests__/conversation/presenceBus.test.ts +236 -0
- package/src/__tests__/conversation/presenceRegistry.test.ts +73 -0
- package/src/__tests__/conversation/roster.test.ts +100 -0
- package/src/__tests__/conversation/rosterProjection.test.ts +142 -0
- package/src/__tests__/conversation/router.test.ts +81 -0
- package/src/__tests__/conversation/runtime.test.ts +206 -0
- package/src/__tests__/conversation/voice.test.ts +440 -0
- package/src/__tests__/documents.test.ts +360 -0
- package/src/__tests__/graphSchema.test.ts +218 -9
- package/src/__tests__/longLiveCommandDispatcher.test.ts +185 -0
- package/src/__tests__/platformAssistant.test.ts +484 -0
- package/src/__tests__/reactor/storyTimeline.test.ts +186 -0
- package/src/__tests__/regeneration.test.ts +486 -0
- package/src/__tests__/stories.test.ts +171 -0
- package/src/__tests__/storyGraph.test.ts +134 -0
- package/src/__tests__/tools-wrappers.test.ts +272 -0
- package/src/__tests__/videoGraph.test.ts +94 -0
- package/src/__tests__/websocket.test.ts +213 -0
- package/src/adapters/capabilities.ts +24 -0
- package/src/adapters/chatAdapter.ts +114 -0
- package/src/adapters/directorAdapter.ts +126 -0
- package/src/adapters/imageAdapter.ts +100 -0
- package/src/adapters/index.ts +67 -0
- package/src/adapters/registry.ts +159 -0
- package/src/adapters/worldAdapter.ts +272 -0
- package/src/admin.ts +310 -0
- package/src/api.ts +1731 -116
- package/src/audio.ts +35 -0
- package/src/castProjection.ts +95 -0
- package/src/conversation/binding.ts +81 -0
- package/src/conversation/controller.ts +439 -0
- package/src/conversation/effectiveModel.ts +111 -0
- package/src/conversation/index.ts +27 -0
- package/src/conversation/participant.ts +71 -0
- package/src/conversation/presence.ts +60 -0
- package/src/conversation/presenceBus.ts +109 -0
- package/src/conversation/presenceRegistry.ts +83 -0
- package/src/conversation/roster.ts +111 -0
- package/src/conversation/rosterProjection.ts +98 -0
- package/src/conversation/router.ts +96 -0
- package/src/conversation/runtime.ts +188 -0
- package/src/conversation/types.ts +100 -0
- package/src/conversation/voice.ts +402 -0
- package/src/graphSchema.ts +396 -13
- package/src/http.ts +72 -0
- package/src/index.ts +9 -0
- package/src/platformAssistant.ts +347 -0
- package/src/reactor/chunkDistribution.ts +98 -0
- package/src/reactor/index.ts +5 -0
- package/src/reactor/longLiveCommandDispatcher.ts +156 -0
- package/src/reactor/scenesFromEnrichment.ts +11 -76
- package/src/reactor/seedImageOrchestrator.ts +12 -13
- package/src/reactor/storyTimeline.ts +127 -0
- package/src/storyGraph.ts +298 -0
- package/src/vision/VisionSourceController.ts +354 -0
- package/src/vision/__tests__/VisionSourceController.test.ts +719 -0
- package/src/vision/index.ts +13 -0
- package/src/vision/sourceKinds.ts +75 -0
- package/src/websocket.ts +75 -3
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Advisory client-side routing helpers (R7.1), lifted VERBATIM
|
|
3
|
+
* (logic-preserving) from `web/app/chat-ws/adventureMode.ts` — they were
|
|
4
|
+
* always I/O-free and misplaced in `web/`.
|
|
5
|
+
*
|
|
6
|
+
* **ADVISORY ONLY.** The server `agents/orchestrator/persona_router.py`
|
|
7
|
+
* `resolve_addressed_persona` is the AUTHORITATIVE router: it alone decides
|
|
8
|
+
* which persona takes a turn. The client NEVER routes a turn — these helpers
|
|
9
|
+
* only read the server's `turn_changed` frames and per-frame `personaId`
|
|
10
|
+
* stamps so a renderer can display who is speaking. Any client-side
|
|
11
|
+
* address-resolution mirror must stay presentation-only and must never be
|
|
12
|
+
* promoted to a routing decision.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The additive multi-persona fields the M10 Story Engine Phase 9a server stamps
|
|
17
|
+
* on chat frames. This is the single, narrowly-typed structural seam for
|
|
18
|
+
* reading `type: 'turn_changed'` and the per-frame `personaId` off an incoming
|
|
19
|
+
* message without an unsafe inline cast at each call site — every member of
|
|
20
|
+
* the precise `ChatWsEvent` union (websocket.ts) is assignable to it. Both
|
|
21
|
+
* fields are optional: a non-adventure frame carries neither, so every reader
|
|
22
|
+
* degrades to the land-dark path.
|
|
23
|
+
*/
|
|
24
|
+
export interface MultiPersonaFrameFields {
|
|
25
|
+
type: string;
|
|
26
|
+
personaId?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Pull the active speaking persona id off a `turn_changed` event — the M10 9a
|
|
31
|
+
* scene orchestrator's signal that the turn passed to a different persona.
|
|
32
|
+
* Funnelling the read through one helper keeps the contract in one place
|
|
33
|
+
* (the `/chat-ws` handler and `ConversationRuntime.applyFrame` both route
|
|
34
|
+
* through it).
|
|
35
|
+
*
|
|
36
|
+
* LAND-DARK: returns `null` unless the frame is genuinely a `turn_changed`
|
|
37
|
+
* carrying a non-empty `personaId` AND an adventure is active
|
|
38
|
+
* (`adventureSessionId` set). `turn_changed` is only ever emitted in a
|
|
39
|
+
* non-empty-party adventure, but gating on the session id too means an
|
|
40
|
+
* ordinary-chat frame can never move the speaker, even defensively.
|
|
41
|
+
*/
|
|
42
|
+
export function turnChangedSpeakerId(
|
|
43
|
+
message: MultiPersonaFrameFields,
|
|
44
|
+
adventureSessionId: string | undefined,
|
|
45
|
+
): string | null {
|
|
46
|
+
if (!adventureSessionId) return null;
|
|
47
|
+
if (message.type !== 'turn_changed') return null;
|
|
48
|
+
return message.personaId && message.personaId.length > 0 ? message.personaId : null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Resolve the per-message persona "pill" for an assistant transcript bubble
|
|
53
|
+
* (M10 Phase 9b). Given the `personaId` captured off the assistant's chat
|
|
54
|
+
* frames and the backend persona list, returns the id + display name to render,
|
|
55
|
+
* or `null` when there's nothing to show. Pure so the transcript pill is
|
|
56
|
+
* unit-testable without a DOM render.
|
|
57
|
+
*
|
|
58
|
+
* LAND-DARK: returns `null` for a `personaId` of `undefined` (every
|
|
59
|
+
* non-adventure assistant message — those frames never carry one), so a normal
|
|
60
|
+
* chat bubble renders byte-identically. When the id is present but unresolvable
|
|
61
|
+
* in `personas` (e.g. a since-deleted persona), falls back to the raw id as the
|
|
62
|
+
* name so the speaker is still attributed rather than silently dropped.
|
|
63
|
+
*/
|
|
64
|
+
export interface PillPersona {
|
|
65
|
+
persona_id: string;
|
|
66
|
+
name: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function resolvePersonaPill(
|
|
70
|
+
personaId: string | undefined,
|
|
71
|
+
personas: readonly PillPersona[],
|
|
72
|
+
): { personaId: string; name: string } | null {
|
|
73
|
+
if (!personaId) return null;
|
|
74
|
+
const match = personas.find((p) => p.persona_id === personaId);
|
|
75
|
+
return { personaId, name: match?.name ?? personaId };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Advisory `partyPersonaIds` for the `send(...)` / `buildHelloPayload(...)`
|
|
80
|
+
* options when in a multi-persona adventure (M10 Phase 9b). The server's
|
|
81
|
+
* `AdventureState.party_persona_ids` is authoritative for scene routing — this
|
|
82
|
+
* is forward-compat/advisory only.
|
|
83
|
+
*
|
|
84
|
+
* LAND-DARK: returns `undefined` when there's no active adventure session OR the
|
|
85
|
+
* party is empty/absent, so both option builders drop the key entirely (they
|
|
86
|
+
* already omit `undefined`) and the wire stays byte-identical for ordinary chat
|
|
87
|
+
* and M9 single-DM adventures.
|
|
88
|
+
*/
|
|
89
|
+
export function advisoryPartyPersonaIds(
|
|
90
|
+
adventureSessionId: string | undefined,
|
|
91
|
+
partyPersonaIds: readonly string[] | undefined,
|
|
92
|
+
): string[] | undefined {
|
|
93
|
+
if (!adventureSessionId) return undefined;
|
|
94
|
+
if (!partyPersonaIds || partyPersonaIds.length === 0) return undefined;
|
|
95
|
+
return [...partyPersonaIds];
|
|
96
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ConversationRuntime` (R7.1) — a pure, framework-agnostic reducer/observer
|
|
3
|
+
* that consumes the EXISTING additive WS frames (`ChatWsEvent`,
|
|
4
|
+
* websocket.ts) and produces a renderer-agnostic
|
|
5
|
+
* {@link ConversationRuntimeState} (who is in the roster, who is speaking,
|
|
6
|
+
* per-message persona attribution, busy flag, stale-adventure flag).
|
|
7
|
+
*
|
|
8
|
+
* This is the CLIENT MIRROR of the server `SceneOrchestrator`'s observable
|
|
9
|
+
* state: it makes NO tool/Bedrock/model decisions (the server owns routing —
|
|
10
|
+
* `agents/orchestrator/persona_router.py` is authoritative; model selection
|
|
11
|
+
* belongs to R1's registry + R2's `resolveEffectiveModel`). It only derives
|
|
12
|
+
* presentation state from frames the server already emits, through the same
|
|
13
|
+
* single gates the `/chat-ws` page uses today (`turnChangedSpeakerId`,
|
|
14
|
+
* `shouldRefreshAdventureRail`) — so React (R7.2) and a PlayCanvas host (R7.3+)
|
|
15
|
+
* render identical state with zero React/DOM imports here.
|
|
16
|
+
*
|
|
17
|
+
* LAND-DARK: for ordinary chat and M9 single-DM (empty-party) adventures no
|
|
18
|
+
* `turn_changed` ever arrives and no frame carries a `personaId`, so
|
|
19
|
+
* `activePersonaId` stays the bound persona, the roster stays as constructed,
|
|
20
|
+
* and the only transitions are busy/message-attribution — byte-identical
|
|
21
|
+
* "lone speaker" behavior.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import type { ChatWsEvent } from '../websocket';
|
|
25
|
+
import { turnChangedSpeakerId, resolvePersonaPill } from './router';
|
|
26
|
+
import { shouldRefreshAdventureRail } from './roster';
|
|
27
|
+
import type {
|
|
28
|
+
ConversationRuntimeListener,
|
|
29
|
+
ConversationRuntimeOptions,
|
|
30
|
+
ConversationRuntimeState,
|
|
31
|
+
} from './types';
|
|
32
|
+
|
|
33
|
+
export class ConversationRuntime {
|
|
34
|
+
private state: ConversationRuntimeState;
|
|
35
|
+
private readonly adventureSessionId: string | undefined;
|
|
36
|
+
private readonly listeners = new Set<ConversationRuntimeListener>();
|
|
37
|
+
|
|
38
|
+
constructor(opts: ConversationRuntimeOptions = {}) {
|
|
39
|
+
this.adventureSessionId = opts.adventureSessionId;
|
|
40
|
+
this.state = Object.freeze({
|
|
41
|
+
participants: Object.freeze([...(opts.participants ?? [])]),
|
|
42
|
+
activePersonaId: opts.boundPersonaId ?? null,
|
|
43
|
+
previousPersonaId: null,
|
|
44
|
+
busy: false,
|
|
45
|
+
inFlightPersonaId: null,
|
|
46
|
+
lastFinalizedPill: null,
|
|
47
|
+
staleAdventureState: false,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Current immutable snapshot. A new object per actual transition, so hosts
|
|
52
|
+
* can use reference equality for change detection. */
|
|
53
|
+
getState(): ConversationRuntimeState {
|
|
54
|
+
return this.state;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Subscribe to state transitions. The listener fires only when a transition
|
|
59
|
+
* actually changed the state (never on no-op frames). Returns an
|
|
60
|
+
* unsubscribe function. A throwing listener is isolated (logged via
|
|
61
|
+
* `console.error`) so it cannot break sibling listeners — mirrors the
|
|
62
|
+
* observer discipline of the R2.1 controller's typed callback seam.
|
|
63
|
+
*/
|
|
64
|
+
subscribe(listener: ConversationRuntimeListener): () => void {
|
|
65
|
+
this.listeners.add(listener);
|
|
66
|
+
return () => {
|
|
67
|
+
this.listeners.delete(listener);
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Reduce one incoming chat frame into the state. Pure per-frame transition —
|
|
73
|
+
* no network, no timers, no DOM. Unknown/irrelevant frame types are no-ops.
|
|
74
|
+
*/
|
|
75
|
+
applyFrame(event: ChatWsEvent): void {
|
|
76
|
+
switch (event.type) {
|
|
77
|
+
case 'turn_changed': {
|
|
78
|
+
// The ONLY way the speaker moves: the server's turn_changed, gated on
|
|
79
|
+
// an active adventure (the client never self-routes — land-dark).
|
|
80
|
+
const speakerId = turnChangedSpeakerId(event, this.adventureSessionId);
|
|
81
|
+
if (speakerId !== null && speakerId !== this.state.activePersonaId) {
|
|
82
|
+
this.transition({
|
|
83
|
+
previousPersonaId: this.state.activePersonaId,
|
|
84
|
+
activePersonaId: speakerId,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
case 'token': {
|
|
90
|
+
// Streaming started/continuing; capture the orchestrator's persona
|
|
91
|
+
// stamp (M10 9a) for in-flight message attribution. Ordinary-chat
|
|
92
|
+
// tokens carry no personaId ⇒ attribution stays null (land-dark).
|
|
93
|
+
this.transition({
|
|
94
|
+
busy: true,
|
|
95
|
+
...(event.personaId ? { inFlightPersonaId: event.personaId } : {}),
|
|
96
|
+
});
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
case 'tool_use': {
|
|
100
|
+
this.transition({ busy: true });
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
case 'done': {
|
|
104
|
+
// Finalize the streamed message: resolve its persona pill against the
|
|
105
|
+
// roster (null when unattributed — every ordinary-chat bubble).
|
|
106
|
+
const pillSource = this.state.inFlightPersonaId ?? event.personaId;
|
|
107
|
+
const pill = resolvePersonaPill(
|
|
108
|
+
pillSource ?? undefined,
|
|
109
|
+
this.state.participants.map((p) => ({
|
|
110
|
+
persona_id: p.personaId,
|
|
111
|
+
name: p.displayName,
|
|
112
|
+
})),
|
|
113
|
+
);
|
|
114
|
+
this.transition({
|
|
115
|
+
busy: false,
|
|
116
|
+
inFlightPersonaId: null,
|
|
117
|
+
lastFinalizedPill: pill,
|
|
118
|
+
});
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
case 'error': {
|
|
122
|
+
this.transition({ busy: false, inFlightPersonaId: null });
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
case 'tool_result': {
|
|
126
|
+
// The single land-dark gate on adventure-state refresh: active
|
|
127
|
+
// adventure AND a DM mutation tool. Anything else is a no-op.
|
|
128
|
+
if (shouldRefreshAdventureRail(this.adventureSessionId, event.tool)) {
|
|
129
|
+
this.transition({ staleAdventureState: true });
|
|
130
|
+
}
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
default:
|
|
134
|
+
// citation/image/audio/transcript/heartbeat/… carry no runtime state.
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Host-driven turn release (R7.2, ADDITIVE): the host removed `personaId`
|
|
141
|
+
* from the party while it held the turn, so it loses the floor —
|
|
142
|
+
* `activePersonaId` returns to null until the server's next `turn_changed`.
|
|
143
|
+
* No-op when `personaId` is not the active speaker. This is the ONLY
|
|
144
|
+
* host-driven speaker transition (roster changes are a host concern, see
|
|
145
|
+
* `ConversationRuntimeState.participants`); a server `turn_changed` frame
|
|
146
|
+
* remains the only way the speaker MOVES to a persona (never self-routing).
|
|
147
|
+
*/
|
|
148
|
+
releaseTurn(personaId: string): void {
|
|
149
|
+
if (this.state.activePersonaId === personaId) {
|
|
150
|
+
this.transition({
|
|
151
|
+
previousPersonaId: this.state.activePersonaId,
|
|
152
|
+
activePersonaId: null,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The host re-fetched the server-authoritative `AdventureState`; clear the
|
|
159
|
+
* stale flag set by a DM mutation `tool_result`.
|
|
160
|
+
*/
|
|
161
|
+
acknowledgeAdventureState(): void {
|
|
162
|
+
if (this.state.staleAdventureState) {
|
|
163
|
+
this.transition({ staleAdventureState: false });
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Apply a partial update; notify listeners only when something changed. */
|
|
168
|
+
private transition(patch: Partial<ConversationRuntimeState>): void {
|
|
169
|
+
let changed = false;
|
|
170
|
+
for (const key of Object.keys(patch) as (keyof ConversationRuntimeState)[]) {
|
|
171
|
+
if (!Object.is(patch[key], this.state[key])) {
|
|
172
|
+
changed = true;
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (!changed) return;
|
|
177
|
+
|
|
178
|
+
this.state = Object.freeze({ ...this.state, ...patch });
|
|
179
|
+
for (const listener of this.listeners) {
|
|
180
|
+
try {
|
|
181
|
+
listener(this.state);
|
|
182
|
+
} catch (err) {
|
|
183
|
+
// Isolate listener faults so one bad subscriber can't break siblings.
|
|
184
|
+
console.error('ConversationRuntime listener threw', err);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for the `ConversationRuntime` reducer (R7.1).
|
|
3
|
+
*
|
|
4
|
+
* The presence vocabulary is NOT re-declared here: `PresenceKind` and its
|
|
5
|
+
* `PRESENCE_*` members already live on `conversation/participant.ts` (R2.1)
|
|
6
|
+
* and are imported — re-exporting them from this module would create the
|
|
7
|
+
* documented ESM ambiguous-export collision in the `export *` barrels (see
|
|
8
|
+
* `src/index.ts` collision warning). Likewise the runtime state type is
|
|
9
|
+
* `ConversationRuntimeState` (not `ConversationState`) to keep the name
|
|
10
|
+
* unambiguous next to the R2.1 controller's `ConversationSnapshot`.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { PresenceKind } from './participant';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* One persona seat in the conversation roster, as the RUNTIME sees it (R7.1).
|
|
17
|
+
* This is the renderer-facing projection — keyed by `personaId` because every
|
|
18
|
+
* chat frame attributes by persona id (`ChatWsEvent.personaId`,
|
|
19
|
+
* `turn_changed.personaId`). The R2.1 `ConversationParticipant` remains the
|
|
20
|
+
* controller's seat model (keyed by `participantId`, carries model binding);
|
|
21
|
+
* this type carries only what a presence renderer / transcript needs.
|
|
22
|
+
*/
|
|
23
|
+
export interface RosterParticipant {
|
|
24
|
+
/** The persona id chat frames attribute to (`personaId` on `ChatWsEvent`). */
|
|
25
|
+
personaId: string;
|
|
26
|
+
/** True for the Dungeon Master seat. */
|
|
27
|
+
isDm: boolean;
|
|
28
|
+
/** Display name for transcript pills and roster chips. */
|
|
29
|
+
displayName: string;
|
|
30
|
+
/** Bound character entity id, when the persona portrays a story character. */
|
|
31
|
+
entityId?: string;
|
|
32
|
+
/** How this participant renders on the presence stage (R2.1 vocabulary). */
|
|
33
|
+
presence: PresenceKind;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The renderer-agnostic state snapshot `ConversationRuntime` produces. A new
|
|
38
|
+
* (frozen-by-convention) object is created per transition; listeners are
|
|
39
|
+
* notified only when a transition actually changed something, so a host can
|
|
40
|
+
* use reference equality for change detection.
|
|
41
|
+
*/
|
|
42
|
+
export interface ConversationRuntimeState {
|
|
43
|
+
/** The roster, as constructed. The runtime never mutates membership —
|
|
44
|
+
* roster changes are a host concern (re-construct or future additive API). */
|
|
45
|
+
participants: readonly RosterParticipant[];
|
|
46
|
+
/**
|
|
47
|
+
* The persona currently holding the turn. Starts as the bound/DM persona and
|
|
48
|
+
* ONLY ever moves on a server `turn_changed` frame while an adventure is
|
|
49
|
+
* active (`turnChangedSpeakerId` gate) — the client never self-routes.
|
|
50
|
+
* LAND-DARK: in ordinary chat / single-DM adventures no `turn_changed` ever
|
|
51
|
+
* arrives, so this stays the bound persona forever.
|
|
52
|
+
*/
|
|
53
|
+
activePersonaId: string | null;
|
|
54
|
+
/** The persona that held the turn before the last swap (null until a swap). */
|
|
55
|
+
previousPersonaId: string | null;
|
|
56
|
+
/** True while an assistant reply is streaming (token/tool_use → done/error). */
|
|
57
|
+
busy: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Persona attribution of the in-flight (streaming) assistant message,
|
|
60
|
+
* captured off the first frame carrying a `personaId` (M10 9a orchestrator
|
|
61
|
+
* stamp). Null for ordinary chat — those frames never carry one (land-dark).
|
|
62
|
+
*/
|
|
63
|
+
inFlightPersonaId: string | null;
|
|
64
|
+
/**
|
|
65
|
+
* The persona pill of the most recently finalized assistant message
|
|
66
|
+
* (`done` frame), resolved against the roster via `resolvePersonaPill`.
|
|
67
|
+
* Null when the finalized message had no persona attribution (every
|
|
68
|
+
* ordinary-chat bubble — land-dark).
|
|
69
|
+
*/
|
|
70
|
+
lastFinalizedPill: { personaId: string; name: string } | null;
|
|
71
|
+
/**
|
|
72
|
+
* True after a `tool_result` for an `ADVENTURE_MUTATION_TOOLS` member while
|
|
73
|
+
* an adventure is active (`shouldRefreshAdventureRail` gate): the
|
|
74
|
+
* server-authoritative `AdventureState` changed and the host should
|
|
75
|
+
* re-fetch, then call `acknowledgeAdventureState()`.
|
|
76
|
+
*/
|
|
77
|
+
staleAdventureState: boolean;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Observer callback for {@link ConversationRuntimeState} transitions. */
|
|
81
|
+
export type ConversationRuntimeListener = (state: ConversationRuntimeState) => void;
|
|
82
|
+
|
|
83
|
+
/** Construction options for `ConversationRuntime`. */
|
|
84
|
+
export interface ConversationRuntimeOptions {
|
|
85
|
+
/**
|
|
86
|
+
* The bound/DM persona id — the initial `activePersonaId`. Optional: a
|
|
87
|
+
* persona-less chat starts with `activePersonaId = null` and (land-dark)
|
|
88
|
+
* keeps it there.
|
|
89
|
+
*/
|
|
90
|
+
boundPersonaId?: string | null;
|
|
91
|
+
/**
|
|
92
|
+
* The active adventure session id, when in an adventure. Gates BOTH the
|
|
93
|
+
* `turn_changed` speaker swap (`turnChangedSpeakerId`) and the
|
|
94
|
+
* stale-adventure flag (`shouldRefreshAdventureRail`). Undefined for
|
|
95
|
+
* ordinary chat ⇒ neither transition can ever fire (land-dark).
|
|
96
|
+
*/
|
|
97
|
+
adventureSessionId?: string;
|
|
98
|
+
/** Initial roster. Defaults to empty; the runtime never mutates it. */
|
|
99
|
+
participants?: readonly RosterParticipant[];
|
|
100
|
+
}
|