@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,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capability auto-switch — the single, pure model-selection helper (R2.1).
|
|
3
|
+
*
|
|
4
|
+
* This is the ONE place R2 decides which model a turn runs on when a capability
|
|
5
|
+
* (today: `'vision'` when the webcam toggles ON) is needed that the bound
|
|
6
|
+
* persona's model lacks. R3's vision-source picker CONSUMES this exact helper —
|
|
7
|
+
* there is no parallel `resolveVisionCapability`. It is fully unit-testable: no
|
|
8
|
+
* I/O, no network. Capability FACTS come only from the injected R1 chat adapter
|
|
9
|
+
* registry (which mirrors `shared/model_config.py`); this helper never hardcodes
|
|
10
|
+
* which models are vision-capable.
|
|
11
|
+
*
|
|
12
|
+
* Precedence: an explicit override wins; otherwise the bound model is kept iff
|
|
13
|
+
* it already satisfies the needed caps; otherwise we auto-switch to the first
|
|
14
|
+
* registry model advertising the needed caps.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { AdapterRegistry } from '../adapters';
|
|
18
|
+
import type { ChatAdapterDescriptor } from '../adapters';
|
|
19
|
+
import type { ChatModelCapability } from '../api';
|
|
20
|
+
|
|
21
|
+
/** Why `resolveEffectiveModel` chose the model it returned. */
|
|
22
|
+
export const EffectiveModelReason = {
|
|
23
|
+
BOUND: 'bound',
|
|
24
|
+
OVERRIDE: 'override',
|
|
25
|
+
AUTO_SWITCH: 'auto_switch',
|
|
26
|
+
} as const;
|
|
27
|
+
|
|
28
|
+
export type EffectiveModelReason =
|
|
29
|
+
(typeof EffectiveModelReason)[keyof typeof EffectiveModelReason];
|
|
30
|
+
|
|
31
|
+
export interface ResolveEffectiveModelArgs {
|
|
32
|
+
/** The context-bound persona's model id (the default choice). */
|
|
33
|
+
boundModelId: string;
|
|
34
|
+
/**
|
|
35
|
+
* Capabilities required for this turn — e.g. `['vision']` when the webcam is
|
|
36
|
+
* ON. Empty means no gate, so the bound model is always kept.
|
|
37
|
+
*/
|
|
38
|
+
needed: readonly ChatModelCapability[];
|
|
39
|
+
/** An explicit user override, when set — wins over bound/auto-switch. */
|
|
40
|
+
override?: string;
|
|
41
|
+
/** The R1 chat adapter registry — the single source of capability facts. */
|
|
42
|
+
registry: Pick<
|
|
43
|
+
AdapterRegistry<ChatAdapterDescriptor>,
|
|
44
|
+
'satisfies' | 'listWithCapability'
|
|
45
|
+
>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface EffectiveModel {
|
|
49
|
+
modelId: string;
|
|
50
|
+
reason: EffectiveModelReason;
|
|
51
|
+
/** The bound model we switched away from — set ONLY for `auto_switch`. */
|
|
52
|
+
switchedFrom?: string;
|
|
53
|
+
/** Echo of the needed caps that drove the decision (for UI banners). */
|
|
54
|
+
needed: readonly ChatModelCapability[];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Resolve the model a turn should run on given the bound model, the needed
|
|
59
|
+
* capabilities, and an optional override. PURE.
|
|
60
|
+
*
|
|
61
|
+
* - `override` set ⇒ `'override'` (we trust the user's explicit pick; we do NOT
|
|
62
|
+
* second-guess its caps — that is the user's choice to make).
|
|
63
|
+
* - bound model satisfies `needed` (or `needed` is empty) ⇒ `'bound'`.
|
|
64
|
+
* - otherwise ⇒ `'auto_switch'` to the first registry model that satisfies
|
|
65
|
+
* EVERY needed cap (not just the first unmet one — a vision-only model is no
|
|
66
|
+
* good when `['vision','audio']` is needed), carrying `switchedFrom`. If NO
|
|
67
|
+
* model satisfies the full set, we keep the bound model as a fail-soft
|
|
68
|
+
* `'bound'` — the panel surfaces the gap rather than the helper throwing (a
|
|
69
|
+
* registry that lists zero vision models is a config state, not a crash).
|
|
70
|
+
*/
|
|
71
|
+
export function resolveEffectiveModel(args: ResolveEffectiveModelArgs): EffectiveModel {
|
|
72
|
+
const { boundModelId, needed, override, registry } = args;
|
|
73
|
+
|
|
74
|
+
if (override) {
|
|
75
|
+
return { modelId: override, reason: EffectiveModelReason.OVERRIDE, needed };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (needed.length === 0 || registry.satisfies(boundModelId, needed)) {
|
|
79
|
+
return { modelId: boundModelId, reason: EffectiveModelReason.BOUND, needed };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Bound model lacks a needed cap and there's no override: auto-switch to the
|
|
83
|
+
// first model that satisfies the WHOLE `needed` set. We seed the candidate
|
|
84
|
+
// pool from the first unmet cap (every valid candidate must advertise it) then
|
|
85
|
+
// require `satisfies(candidate, needed)` so multi-cap requests (e.g.
|
|
86
|
+
// ['vision','audio']) don't settle for a vision-only model. Deterministic:
|
|
87
|
+
// first matching candidate in registry order.
|
|
88
|
+
// `unmet` is always found here (needed is non-empty and the bound model failed
|
|
89
|
+
// the full set). The `?:` guard below is belt-and-suspenders for this helper's
|
|
90
|
+
// "never throw" contract: if that invariant were ever broken, `candidate` stays
|
|
91
|
+
// undefined and we fall through to the fail-soft `return bound` at the end —
|
|
92
|
+
// never calling `listWithCapability(undefined)`.
|
|
93
|
+
const unmet = needed.find((cap) => !registry.satisfies(boundModelId, [cap]));
|
|
94
|
+
const candidate = unmet
|
|
95
|
+
? registry
|
|
96
|
+
.listWithCapability(unmet)
|
|
97
|
+
.find((model) => registry.satisfies(model.id, needed))
|
|
98
|
+
: undefined;
|
|
99
|
+
if (candidate) {
|
|
100
|
+
return {
|
|
101
|
+
modelId: candidate.id,
|
|
102
|
+
reason: EffectiveModelReason.AUTO_SWITCH,
|
|
103
|
+
switchedFrom: boundModelId,
|
|
104
|
+
needed,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Nothing in the registry can satisfy the gap — keep the bound model (the
|
|
109
|
+
// panel shows the unmet cap); fail-soft, never throw.
|
|
110
|
+
return { modelId: boundModelId, reason: EffectiveModelReason.BOUND, needed };
|
|
111
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Barrel for the framework-agnostic `conversation/` core (R2.1).
|
|
3
|
+
*
|
|
4
|
+
* Re-exported from `src/index.ts` via `export * from './conversation'`. Owns the
|
|
5
|
+
* headless conversation state (participants, presence, bound context, model
|
|
6
|
+
* override + capability auto-switch) with NO DOM — the seam that lets React
|
|
7
|
+
* (R2.2) and PlayCanvas (R7) render the same controller.
|
|
8
|
+
*
|
|
9
|
+
* ADDITIVE-ONLY: every symbol here is new. `Citation`/`PlaybackState` and the
|
|
10
|
+
* `ChatModelCapability` union live on `websocket.ts` / `api.ts` and are IMPORTED
|
|
11
|
+
* by these modules, never re-declared — avoiding the documented ambiguous-export
|
|
12
|
+
* trap in the top-level barrel.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export * from './participant';
|
|
16
|
+
export * from './presenceRegistry';
|
|
17
|
+
export * from './binding';
|
|
18
|
+
export * from './effectiveModel';
|
|
19
|
+
export * from './controller';
|
|
20
|
+
export * from './types';
|
|
21
|
+
export * from './router';
|
|
22
|
+
export * from './roster';
|
|
23
|
+
export * from './rosterProjection';
|
|
24
|
+
export * from './runtime';
|
|
25
|
+
export * from './presence';
|
|
26
|
+
export * from './presenceBus';
|
|
27
|
+
export * from './voice';
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Conversation participant model (R2.1).
|
|
3
|
+
*
|
|
4
|
+
* A `ConversationParticipant` is ONE seat in a conversation — a bound persona,
|
|
5
|
+
* the Dungeon Master, or the human user. The headless `ConversationController`
|
|
6
|
+
* (controller.ts) holds a list of these plus an active one; React (R2.2) and a
|
|
7
|
+
* game engine (R7) both render the same list without owning its shape.
|
|
8
|
+
*
|
|
9
|
+
* `role` and `presence` are frozen constant objects — never inline the string
|
|
10
|
+
* literals (the cross-PR "constants over string literals" rule). The capability
|
|
11
|
+
* vocabulary is the EXISTING `ChatModelCapability` union from api.ts (the single
|
|
12
|
+
* source of truth that mirrors `shared/model_config.py:CAPABILITY_*`); R2 does
|
|
13
|
+
* NOT declare a parallel capability map.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import type { ChatModelCapability } from '../api';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The kind of seat a participant occupies. `persona` and `dm` are
|
|
20
|
+
* model-backed; `user` is the human. Frozen so a role can never be a raw
|
|
21
|
+
* string at a call site — mirrors the `ROUTE_*`/`PRESENCE_*` discipline.
|
|
22
|
+
*/
|
|
23
|
+
export const ParticipantRole = {
|
|
24
|
+
PERSONA: 'persona',
|
|
25
|
+
DM: 'dm',
|
|
26
|
+
USER: 'user',
|
|
27
|
+
} as const;
|
|
28
|
+
|
|
29
|
+
export type ParticipantRole = (typeof ParticipantRole)[keyof typeof ParticipantRole];
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* How a participant is rendered on the presence stage. `PRESENCE_AVATAR_3D` is
|
|
33
|
+
* a RESERVED stub — the kind is registerable but its renderer is a no-op until
|
|
34
|
+
* the R7 flagship lands (see `PresenceRendererRegistry` + tech-debt). The values
|
|
35
|
+
* double as the registry lookup keys.
|
|
36
|
+
*/
|
|
37
|
+
export const PresenceKind = {
|
|
38
|
+
PRESENCE_NONE: 'none',
|
|
39
|
+
PRESENCE_FACE_2D: 'face_2d',
|
|
40
|
+
PRESENCE_AVATAR_3D: 'avatar_3d',
|
|
41
|
+
} as const;
|
|
42
|
+
|
|
43
|
+
export type PresenceKind = (typeof PresenceKind)[keyof typeof PresenceKind];
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* One seat in a conversation. `modelId` is the bound model the capability gate
|
|
47
|
+
* resolves against; `capabilities` is an ADVISORY display-only mirror (see its
|
|
48
|
+
* field doc). Both optional: the `user` seat and presence-only participants
|
|
49
|
+
* carry neither.
|
|
50
|
+
*/
|
|
51
|
+
export interface ConversationParticipant {
|
|
52
|
+
/** Stable per-conversation id for this seat (not the persona id). */
|
|
53
|
+
participantId: string;
|
|
54
|
+
/** Bound persona id, when this seat portrays a persona. */
|
|
55
|
+
personaId?: string;
|
|
56
|
+
role: ParticipantRole;
|
|
57
|
+
presence: PresenceKind;
|
|
58
|
+
/** The bound model's id, when this seat is model-backed. The capability gate
|
|
59
|
+
* (`resolveEffectiveModel`) resolves caps against the R1 registry keyed by
|
|
60
|
+
* THIS id — never the `capabilities` mirror below. */
|
|
61
|
+
modelId?: string;
|
|
62
|
+
/**
|
|
63
|
+
* ADVISORY / display-only mirror of the bound model's caps (e.g. for R2.2's
|
|
64
|
+
* capability chips). It does **NOT** gate model selection — `resolveEffectiveModel`
|
|
65
|
+
* reads the authoritative caps from the R1 chat registry (`registry.satisfies`),
|
|
66
|
+
* which mirrors `shared/model_config.py` (single source of truth). Kept so the
|
|
67
|
+
* renderer can show caps without a second registry lookup; never trusted for
|
|
68
|
+
* the auto-switch decision.
|
|
69
|
+
*/
|
|
70
|
+
capabilities?: ChatModelCapability[];
|
|
71
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `PresenceAdapter` (R7.3) — the renderer-agnostic seam the
|
|
3
|
+
* `ConversationRuntime` drives. React (`CharacterFace`, 2D persona face) is
|
|
4
|
+
* ONE implementation; a PlayCanvas 3D avatar is a FUTURE implementation
|
|
5
|
+
* consuming the identical contract. Zero React/DOM/PlayCanvas imports here.
|
|
6
|
+
*
|
|
7
|
+
* Every method is keyed by `personaId`, so an N-participant scene maps cleanly
|
|
8
|
+
* onto N presence slots regardless of renderer. The method set is derived
|
|
9
|
+
* STRICTLY from what the React face already does (active swap, speaking flag,
|
|
10
|
+
* viseme) plus mount/unmount — no speculative methods (the over-abstraction
|
|
11
|
+
* guard in the R7 spec).
|
|
12
|
+
*
|
|
13
|
+
* Adapter SELECTION stays on the existing R2.1 seam: hosts register a renderer
|
|
14
|
+
* factory per `PresenceKind` on `PresenceRendererRegistry`
|
|
15
|
+
* (presenceRegistry.ts) — this module does NOT re-declare a kind→factory map.
|
|
16
|
+
* What it adds is the per-call gating contract: `presenceBus` consults
|
|
17
|
+
* {@link presenceKindHasStage} so a `PRESENCE_NONE` (text-only) participant
|
|
18
|
+
* receives no presence calls beyond `mount`/`unmount`.
|
|
19
|
+
*
|
|
20
|
+
* Viseme coupling (the R7.3 risk note): `applyViseme` takes the *scheduled
|
|
21
|
+
* viseme value* (`VisemeSymbol`, from the existing `visemeScheduler`/
|
|
22
|
+
* `visemeGeometry` pipeline) — never a DOM node. The React adapter applies it
|
|
23
|
+
* to SVG; a PlayCanvas adapter applies it to morph targets.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { PresenceKind } from './participant';
|
|
27
|
+
import type { RosterParticipant } from './types';
|
|
28
|
+
import type { VisemeSymbol } from '../visemeScheduler';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The renderer contract. A host implements this once per renderer (2D face,
|
|
32
|
+
* 3D avatar, headless recorder) and `presenceBus` drives it from
|
|
33
|
+
* `ConversationRuntime` transitions.
|
|
34
|
+
*/
|
|
35
|
+
export interface PresenceAdapter {
|
|
36
|
+
/** A participant entered the scene: create its presence slot. Called for
|
|
37
|
+
* EVERY roster member, including `PRESENCE_NONE` (so the renderer can
|
|
38
|
+
* account for text-only seats), but a `none` seat gets no further calls. */
|
|
39
|
+
mount(participant: RosterParticipant): void;
|
|
40
|
+
/** The participant left the scene (or the binding was disposed): tear the
|
|
41
|
+
* slot down. */
|
|
42
|
+
unmount(personaId: string): void;
|
|
43
|
+
/** The turn moved: `personaId` now holds the floor (`null` = nobody, e.g.
|
|
44
|
+
* after a host `releaseTurn`). Stage focus/camera/highlight here. */
|
|
45
|
+
setActiveParticipant(personaId: string | null): void;
|
|
46
|
+
/** Toggle the speaking indicator for one participant. */
|
|
47
|
+
setSpeaking(personaId: string, speaking: boolean): void;
|
|
48
|
+
/** Apply one scheduled viseme value to a participant's mouth — SVG path for
|
|
49
|
+
* the 2D face, morph target for a 3D avatar. */
|
|
50
|
+
applyViseme(personaId: string, viseme: VisemeSymbol): void;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* True when a presence kind occupies the stage — i.e. should receive
|
|
55
|
+
* `setActiveParticipant`/`setSpeaking`/`applyViseme` calls. `PRESENCE_NONE`
|
|
56
|
+
* seats are text-only: mount/unmount bookkeeping, nothing staged.
|
|
57
|
+
*/
|
|
58
|
+
export function presenceKindHasStage(kind: PresenceKind): boolean {
|
|
59
|
+
return kind !== PresenceKind.PRESENCE_NONE;
|
|
60
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `presenceBus` (R7.3) — pure glue wiring `ConversationRuntime` state
|
|
3
|
+
* transitions to `PresenceAdapter` calls. No renderer dependency, no DOM, no
|
|
4
|
+
* network: a `turn_changed`-driven swap becomes
|
|
5
|
+
* `setActiveParticipant(next)` + `setSpeaking(prev,false)` + `setSpeaking(next,true)`,
|
|
6
|
+
* gated per participant by {@link presenceKindHasStage} so text-only
|
|
7
|
+
* (`PRESENCE_NONE`) seats receive nothing beyond `mount`/`unmount`.
|
|
8
|
+
*
|
|
9
|
+
* LAND-DARK: in ordinary chat / single-DM adventures the runtime's
|
|
10
|
+
* `activePersonaId` never moves, so after the initial staging the bus emits
|
|
11
|
+
* nothing — the conformance test pins exactly one `mount` + one
|
|
12
|
+
* `setActiveParticipant` + one `setSpeaking(true)` for the lone speaker.
|
|
13
|
+
*
|
|
14
|
+
* Visemes do not flow through runtime state (the voice pipeline lifts into
|
|
15
|
+
* client-core in R7.5); the binding exposes {@link PresenceBinding.applyViseme}
|
|
16
|
+
* as the single pass-through so R7.5 has exactly one delivery path to a face.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { ConversationRuntime } from './runtime';
|
|
20
|
+
import type { ConversationRuntimeState, RosterParticipant } from './types';
|
|
21
|
+
import type { PresenceAdapter } from './presence';
|
|
22
|
+
import { presenceKindHasStage } from './presence';
|
|
23
|
+
import type { VisemeSymbol } from '../visemeScheduler';
|
|
24
|
+
|
|
25
|
+
/** Handle returned by {@link bindPresence}. */
|
|
26
|
+
export interface PresenceBinding {
|
|
27
|
+
/**
|
|
28
|
+
* Deliver one scheduled viseme value to `personaId`'s presence slot.
|
|
29
|
+
* Dropped (no adapter call) when the persona is not in the roster or its
|
|
30
|
+
* presence kind has no stage (`PRESENCE_NONE`).
|
|
31
|
+
*/
|
|
32
|
+
applyViseme(personaId: string, viseme: VisemeSymbol): void;
|
|
33
|
+
/** Unsubscribe from the runtime and unmount every mounted participant.
|
|
34
|
+
* Idempotent. */
|
|
35
|
+
dispose(): void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Bind a `PresenceAdapter` to a `ConversationRuntime`.
|
|
40
|
+
*
|
|
41
|
+
* On bind: `mount` every roster participant (in roster order), then stage the
|
|
42
|
+
* initial speaker (`setActiveParticipant` + `setSpeaking(true)`) when one is
|
|
43
|
+
* set and staged. Thereafter every speaker transition (server `turn_changed`
|
|
44
|
+
* via the runtime, or a host `releaseTurn`) is mirrored to the adapter.
|
|
45
|
+
*/
|
|
46
|
+
export function bindPresence(
|
|
47
|
+
runtime: ConversationRuntime,
|
|
48
|
+
adapter: PresenceAdapter,
|
|
49
|
+
): PresenceBinding {
|
|
50
|
+
const byPersonaId = (
|
|
51
|
+
state: ConversationRuntimeState,
|
|
52
|
+
personaId: string | null,
|
|
53
|
+
): RosterParticipant | undefined =>
|
|
54
|
+
personaId === null
|
|
55
|
+
? undefined
|
|
56
|
+
: state.participants.find((p) => p.personaId === personaId);
|
|
57
|
+
|
|
58
|
+
const staged = (state: ConversationRuntimeState, personaId: string | null): boolean => {
|
|
59
|
+
const participant = byPersonaId(state, personaId);
|
|
60
|
+
return participant !== undefined && presenceKindHasStage(participant.presence);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** Mirror one speaker move onto the adapter, honoring the stage gate. */
|
|
64
|
+
const stageSpeaker = (
|
|
65
|
+
state: ConversationRuntimeState,
|
|
66
|
+
prev: string | null,
|
|
67
|
+
next: string | null,
|
|
68
|
+
): void => {
|
|
69
|
+
const nextStaged = staged(state, next);
|
|
70
|
+
// A `none`-kind (or roster-unknown) speaker must never be the staged id
|
|
71
|
+
// (text-only contract) — the stage clears to `null` instead, exactly as
|
|
72
|
+
// when nobody holds the floor.
|
|
73
|
+
adapter.setActiveParticipant(nextStaged ? next : null);
|
|
74
|
+
if (prev !== null && staged(state, prev)) adapter.setSpeaking(prev, false);
|
|
75
|
+
if (next !== null && nextStaged) adapter.setSpeaking(next, true);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// Bind-time: mount the roster, then stage the initial speaker (if any).
|
|
79
|
+
const initial = runtime.getState();
|
|
80
|
+
for (const participant of initial.participants) adapter.mount(participant);
|
|
81
|
+
if (initial.activePersonaId !== null && staged(initial, initial.activePersonaId)) {
|
|
82
|
+
adapter.setActiveParticipant(initial.activePersonaId);
|
|
83
|
+
adapter.setSpeaking(initial.activePersonaId, true);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let lastActive = initial.activePersonaId;
|
|
87
|
+
const unsubscribe = runtime.subscribe((state) => {
|
|
88
|
+
if (state.activePersonaId !== lastActive) {
|
|
89
|
+
stageSpeaker(state, lastActive, state.activePersonaId);
|
|
90
|
+
lastActive = state.activePersonaId;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
let disposed = false;
|
|
95
|
+
return {
|
|
96
|
+
applyViseme(personaId: string, viseme: VisemeSymbol): void {
|
|
97
|
+
if (disposed) return;
|
|
98
|
+
if (staged(runtime.getState(), personaId)) adapter.applyViseme(personaId, viseme);
|
|
99
|
+
},
|
|
100
|
+
dispose(): void {
|
|
101
|
+
if (disposed) return;
|
|
102
|
+
disposed = true;
|
|
103
|
+
unsubscribe();
|
|
104
|
+
for (const participant of runtime.getState().participants) {
|
|
105
|
+
adapter.unmount(participant.personaId);
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-agnostic presence-renderer registry (R2.1).
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the named-registry discipline of the adapter registry
|
|
5
|
+
* (`adapters/registry.ts` — additive `register`, fail-loud on duplicate) and
|
|
6
|
+
* the `reactor/promptAddonRegistry.ts` precedent: it carries renderer FACTORIES
|
|
7
|
+
* keyed by `PresenceKind` and never touches the DOM itself. React (R2.2)
|
|
8
|
+
* registers a 2D-face factory; a game engine (R7) registers a 3D one; the core
|
|
9
|
+
* stays engine-free.
|
|
10
|
+
*
|
|
11
|
+
* A factory yields an opaque {@link PresenceHandle} (`mount`/`update`/`destroy`)
|
|
12
|
+
* so the caller drives the lifecycle without the registry knowing what the
|
|
13
|
+
* handle is. The registry is the single seam between the headless conversation
|
|
14
|
+
* state and whatever paints a face.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { PresenceKind } from './participant';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Opaque per-mount handle a renderer factory returns. The conversation panel
|
|
21
|
+
* calls `mount` to attach into a host element, `update` on each presence-state
|
|
22
|
+
* change, and `destroy` on teardown. `unknown` payloads keep the core agnostic
|
|
23
|
+
* to the engine's host/state types — each renderer narrows them.
|
|
24
|
+
*/
|
|
25
|
+
export interface PresenceHandle {
|
|
26
|
+
mount(host: unknown): void;
|
|
27
|
+
update(state: unknown): void;
|
|
28
|
+
destroy(): void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A renderer factory. Called once per presence mount to produce a handle. */
|
|
32
|
+
export type PresenceRendererFactory = () => PresenceHandle;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The `PRESENCE_NONE` no-op handle. Returned for any unregistered kind so the
|
|
36
|
+
* 3D-avatar stub (and any not-yet-bound kind) fails SOFT — the caller can drive
|
|
37
|
+
* the lifecycle without a null check and nothing paints. Stateless, so a single
|
|
38
|
+
* shared instance is safe.
|
|
39
|
+
*/
|
|
40
|
+
// Frozen: the no-op handle is shared by every unregistered kind, so freeze it
|
|
41
|
+
// against accidental mutation by a caller that mistakes it for a live renderer.
|
|
42
|
+
const NOOP_HANDLE: PresenceHandle = Object.freeze({
|
|
43
|
+
mount() {},
|
|
44
|
+
update() {},
|
|
45
|
+
destroy() {},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const NOOP_FACTORY: PresenceRendererFactory = () => NOOP_HANDLE;
|
|
49
|
+
|
|
50
|
+
export class PresenceRendererRegistry {
|
|
51
|
+
private readonly factories = new Map<PresenceKind, PresenceRendererFactory>();
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Register a renderer factory for a presence kind. Additive — THROWS on a
|
|
55
|
+
* duplicate kind (fail-loud), matching the adapter registry's no-silent-
|
|
56
|
+
* overwrite contract. A renderer binding is a one-time app-init step, so a
|
|
57
|
+
* second registration is a wiring bug, not a runtime condition.
|
|
58
|
+
*/
|
|
59
|
+
register(kind: PresenceKind, factory: PresenceRendererFactory): void {
|
|
60
|
+
if (this.factories.has(kind)) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
`PresenceRendererRegistry: duplicate registration for presence kind "${kind}" — ` +
|
|
63
|
+
'registration is additive and must not silently overwrite an existing renderer.',
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
this.factories.set(kind, factory);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Resolve a renderer factory for a kind. Returns the registered factory, or
|
|
71
|
+
* the `PRESENCE_NONE` no-op factory when the kind is unregistered (fail-soft
|
|
72
|
+
* — this is what lets the reserved `PRESENCE_AVATAR_3D` kind render nothing
|
|
73
|
+
* instead of throwing until R7 binds a real 3D renderer).
|
|
74
|
+
*/
|
|
75
|
+
resolve(kind: PresenceKind): PresenceRendererFactory {
|
|
76
|
+
return this.factories.get(kind) ?? NOOP_FACTORY;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** True iff a factory is registered for `kind` (the no-op fallback aside). */
|
|
80
|
+
has(kind: PresenceKind): boolean {
|
|
81
|
+
return this.factories.has(kind);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Party / roster helpers (R7.1), lifted VERBATIM (logic-preserving) from
|
|
3
|
+
* `web/app/chat-ws/adventureMode.ts` (M9 Story Engine Phase 8b Component F +
|
|
4
|
+
* M10 Phase 9b) — they were always I/O-free and misplaced in `web/`.
|
|
5
|
+
*
|
|
6
|
+
* Single source of truth for the Dungeon-Master tool names that MUTATE the
|
|
7
|
+
* server-authoritative `AdventureState` (`shared/tool_schemas.py`'s
|
|
8
|
+
* `_ADVENTURE_TOOL_SCHEMAS`). When the chat stream reports a `tool_result` for
|
|
9
|
+
* one of these, the host re-fetches `getAdventure(...)` to mirror the new
|
|
10
|
+
* state. `_ADVENTURE_TOOL_SCHEMAS` defines 7 DM tools; this is the 4 that
|
|
11
|
+
* mutate. The 3 read-only DM tools (`describe_current_scene`, `get_flag`,
|
|
12
|
+
* `get_party_status`) and ordinary chat tools must NOT be in this set, so they
|
|
13
|
+
* never trigger a re-fetch and an inactive adventure stays land-dark.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The four DM tools that mutate `AdventureState`. Mirrors the Python
|
|
18
|
+
* `_ADVENTURE_TOOL_SCHEMAS` names in `shared/tool_schemas.py`. `readonly` so the
|
|
19
|
+
* set can't be mutated by a caller. After R7.2 this is the ONLY copy — the
|
|
20
|
+
* `web/` `adventureMode.ts` re-exports it from here.
|
|
21
|
+
*/
|
|
22
|
+
export const ADVENTURE_MUTATION_TOOLS = [
|
|
23
|
+
'advance_story',
|
|
24
|
+
'set_flag',
|
|
25
|
+
'clear_flag',
|
|
26
|
+
'update_inventory',
|
|
27
|
+
] as const;
|
|
28
|
+
|
|
29
|
+
export type AdventureMutationTool = (typeof ADVENTURE_MUTATION_TOOLS)[number];
|
|
30
|
+
|
|
31
|
+
const ADVENTURE_MUTATION_TOOL_SET: ReadonlySet<string> = new Set(
|
|
32
|
+
ADVENTURE_MUTATION_TOOLS,
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* True when `tool` is one of the DM tools that mutate adventure state, so the
|
|
37
|
+
* host should re-fetch. Tolerates `undefined` (a `tool_result` frame may
|
|
38
|
+
* omit `tool`) and any non-mutation tool by returning false — the load-bearing
|
|
39
|
+
* land-dark guarantee is that this is the ONLY gate on a rail re-fetch.
|
|
40
|
+
*/
|
|
41
|
+
export function isAdventureMutationTool(
|
|
42
|
+
tool: string | undefined,
|
|
43
|
+
): tool is AdventureMutationTool {
|
|
44
|
+
return tool !== undefined && ADVENTURE_MUTATION_TOOL_SET.has(tool);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The single land-dark gate for re-fetching adventure state after a
|
|
49
|
+
* `tool_result` frame: refresh ONLY when an adventure is active AND a DM
|
|
50
|
+
* mutation tool ran. The `/chat-ws` message handler and
|
|
51
|
+
* `ConversationRuntime.applyFrame` call exactly this — so it is the one place
|
|
52
|
+
* that decides whether adventure mode does anything, and asserting it here
|
|
53
|
+
* proves the land-dark contract (no adventureSessionId ⇒ never true ⇒ no new
|
|
54
|
+
* network call, for ANY tool).
|
|
55
|
+
*/
|
|
56
|
+
export function shouldRefreshAdventureRail(
|
|
57
|
+
adventureSessionId: string | undefined,
|
|
58
|
+
tool: string | undefined,
|
|
59
|
+
): boolean {
|
|
60
|
+
return Boolean(adventureSessionId) && isAdventureMutationTool(tool);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Pure candidate filter for party-member pickers (M10 Story Engine Phase 9b),
|
|
65
|
+
* extracted — like {@link shouldRefreshAdventureRail} — so the rule is
|
|
66
|
+
* unit-testable without a DOM render of the modal or the API client.
|
|
67
|
+
*
|
|
68
|
+
* A persona is a valid party candidate when it is:
|
|
69
|
+
* 1. NOT a Dungeon Master (`is_dungeon_master !== true`),
|
|
70
|
+
* 2. `status === 'approved'`,
|
|
71
|
+
* 3. bound to a character entity (`entity_id` non-null/non-empty),
|
|
72
|
+
* 4. bound to a character that exists in THIS story (its `entity_id` is in
|
|
73
|
+
* `storyCharacterEntityIds`), and
|
|
74
|
+
* 5. NOT already in the party / the DM (`persona_id` ∉ `excludePersonaIds`).
|
|
75
|
+
*
|
|
76
|
+
* Rule (4) is relaxed when `storyCharacterEntityIds` is `null` — the caller
|
|
77
|
+
* passes `null` only when `listVideoCharacters` was unavailable or errored, so
|
|
78
|
+
* the picker degrades to the looser "any character-bound persona" set rather
|
|
79
|
+
* than offering nothing (the caller logs a console warning). The character set
|
|
80
|
+
* being empty (not null) means the story genuinely has no characters ⇒ no
|
|
81
|
+
* candidates, which is correct.
|
|
82
|
+
*
|
|
83
|
+
* Typed loosely on the persona shape so this module takes no dependency on the
|
|
84
|
+
* full `PersonaConfig`. Pure: no side effects.
|
|
85
|
+
*/
|
|
86
|
+
export interface PartyCandidatePersona {
|
|
87
|
+
persona_id: string;
|
|
88
|
+
status: 'pending' | 'approved';
|
|
89
|
+
entity_id?: string | null;
|
|
90
|
+
is_dungeon_master?: boolean;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function filterPartyCandidates<T extends PartyCandidatePersona>(
|
|
94
|
+
personas: readonly T[],
|
|
95
|
+
excludePersonaIds: readonly string[],
|
|
96
|
+
/** Character entity ids in this story, or `null` to relax the story-binding check. */
|
|
97
|
+
storyCharacterEntityIds: ReadonlySet<string> | null,
|
|
98
|
+
): T[] {
|
|
99
|
+
const excluded = new Set(excludePersonaIds);
|
|
100
|
+
return personas.filter((p) => {
|
|
101
|
+
if (p.is_dungeon_master === true) return false;
|
|
102
|
+
if (p.status !== 'approved') return false;
|
|
103
|
+
if (!p.entity_id) return false;
|
|
104
|
+
if (excluded.has(p.persona_id)) return false;
|
|
105
|
+
// Story-binding check, relaxed (fallback) when the character set is null.
|
|
106
|
+
if (storyCharacterEntityIds !== null && !storyCharacterEntityIds.has(p.entity_id)) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
return true;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `castToRoster` (R7.4) — the single DRY projection turning R5's one-call
|
|
3
|
+
* cast-with-persona response (`getCast` → `CastEntry[]`) plus the active
|
|
4
|
+
* `AdventureState` (DM persona + `partyPersonaIds`) into the
|
|
5
|
+
* {@link RosterParticipant}[] the framework-agnostic `ConversationRuntime` and
|
|
6
|
+
* `presenceBus` consume.
|
|
7
|
+
*
|
|
8
|
+
* WHY this exists: pre-R7.4 the Studio surface derived its party roster by a
|
|
9
|
+
* CLIENT-SIDE `entity_id ∈ storyCharacterEntityIds` join across two stores
|
|
10
|
+
* (`listVideoCharacters` + `listPersonas`), which depends on
|
|
11
|
+
* `generate_entity_id` vs `generate_document_entity_id` string matching
|
|
12
|
+
* (`shared/neo4j_queries.py`). R5 moved that join SERVER-SIDE
|
|
13
|
+
* (`GET /api/v1/tools/videos/{videoId}/cast`, the PORTRAYS edge +
|
|
14
|
+
* authoritative `PersonaConfig.entity_id`). This function is the one place that
|
|
15
|
+
* reshapes that authoritative projection into the renderer-facing roster — so
|
|
16
|
+
* the roster never re-derives the binding client-side again.
|
|
17
|
+
*
|
|
18
|
+
* Ordering is server-party-authoritative: the DM seat leads (it always holds
|
|
19
|
+
* the floor first — the runtime's initial `activePersonaId`), then each party
|
|
20
|
+
* member in `partyPersonaIds` order. Every server-party member gets a seat even
|
|
21
|
+
* when absent from the cast projection (a removed/foreign id), so the runtime
|
|
22
|
+
* can always stage a `turn_changed` speaker; the display name falls back to the
|
|
23
|
+
* persona id in that defensive case.
|
|
24
|
+
*
|
|
25
|
+
* Pure: no I/O, no DOM, no React — framework-agnostic, like every other
|
|
26
|
+
* `conversation/` module. The presence kind is `PRESENCE_FACE_2D` for every
|
|
27
|
+
* seat (the current renderer); a future host can re-map per participant.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { PresenceKind } from './participant';
|
|
31
|
+
import type { RosterParticipant } from './types';
|
|
32
|
+
import type { CastEntry } from '../api';
|
|
33
|
+
|
|
34
|
+
/** The DM persona's identity fields the roster needs (a thin slice of the
|
|
35
|
+
* bound `PersonaConfig`). */
|
|
36
|
+
export interface RosterDmPersona {
|
|
37
|
+
personaId: string;
|
|
38
|
+
/** Display name for the DM's transcript pill / roster chip. */
|
|
39
|
+
name: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface CastToRosterInput {
|
|
43
|
+
/** R5's one-call cast-with-persona projection (`getCast(...).cast`). */
|
|
44
|
+
cast: readonly CastEntry[];
|
|
45
|
+
/** The Dungeon-Master persona running this adventure. */
|
|
46
|
+
dmPersona: RosterDmPersona;
|
|
47
|
+
/** The server-authoritative party member persona ids, in party order
|
|
48
|
+
* (`AdventureState.partyPersonaIds`). */
|
|
49
|
+
partyPersonaIds: readonly string[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Project the cast + active adventure into the renderer-facing roster (DM seat
|
|
54
|
+
* first, then party members in party order, de-duplicated, DM never doubled).
|
|
55
|
+
*/
|
|
56
|
+
export function castToRoster(input: CastToRosterInput): RosterParticipant[] {
|
|
57
|
+
const { cast, dmPersona, partyPersonaIds } = input;
|
|
58
|
+
|
|
59
|
+
// Index the cast's bound personas by persona id so a party member resolves its
|
|
60
|
+
// display name (the persona's, not the character's) + bound entity id in O(1).
|
|
61
|
+
const castByPersonaId = new Map<string, CastEntry>();
|
|
62
|
+
for (const entry of cast) {
|
|
63
|
+
if (entry.persona) castByPersonaId.set(entry.persona.personaId, entry);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const roster: RosterParticipant[] = [
|
|
67
|
+
{
|
|
68
|
+
personaId: dmPersona.personaId,
|
|
69
|
+
isDm: true,
|
|
70
|
+
displayName: dmPersona.name,
|
|
71
|
+
presence: PresenceKind.PRESENCE_FACE_2D,
|
|
72
|
+
},
|
|
73
|
+
];
|
|
74
|
+
|
|
75
|
+
// The DM is already seated; never seat it twice, and de-duplicate repeated
|
|
76
|
+
// party ids (first occurrence wins — mirrors the server party set semantics).
|
|
77
|
+
const seen = new Set<string>([dmPersona.personaId]);
|
|
78
|
+
for (const personaId of partyPersonaIds) {
|
|
79
|
+
if (seen.has(personaId)) continue;
|
|
80
|
+
seen.add(personaId);
|
|
81
|
+
|
|
82
|
+
const entry = castByPersonaId.get(personaId);
|
|
83
|
+
roster.push({
|
|
84
|
+
personaId,
|
|
85
|
+
isDm: false,
|
|
86
|
+
// The persona's display name wins (the transcript pill the user sees);
|
|
87
|
+
// fall back to the character name, then the raw id (defensive — a removed
|
|
88
|
+
// or foreign party member still needs a seat so the runtime can stage it).
|
|
89
|
+
displayName: entry?.persona?.name ?? entry?.character.name ?? personaId,
|
|
90
|
+
...(entry?.character.entityId
|
|
91
|
+
? { entityId: entry.character.entityId }
|
|
92
|
+
: {}),
|
|
93
|
+
presence: PresenceKind.PRESENCE_FACE_2D,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return roster;
|
|
98
|
+
}
|