@spellkave/session 0.0.0-bootstrap
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/LICENSE +89 -0
- package/bin/spellkave-session-daemon.js +5 -0
- package/package.json +60 -0
- package/src/acting-context.ts +222 -0
- package/src/authoring/caller-tools.ts +227 -0
- package/src/authoring/index.ts +28 -0
- package/src/authoring/live-applyset-port.ts +432 -0
- package/src/authoring/live-author-target.ts +995 -0
- package/src/authoring/live-can-i-port.ts +152 -0
- package/src/authoring/live-exploration-port.ts +274 -0
- package/src/authoring/live-grants-port.ts +172 -0
- package/src/authoring/live-scenes-port.ts +84 -0
- package/src/authoring/live-scoped-instance-port.ts +355 -0
- package/src/binding.ts +125 -0
- package/src/index.ts +24 -0
- package/src/live/discovery-closure-walk.ts +318 -0
- package/src/live/kernel-convergence-support.ts +122 -0
- package/src/live/seed.ts +37 -0
- package/src/live/support.ts +82 -0
- package/src/node/client.ts +233 -0
- package/src/node/connection-controller.ts +469 -0
- package/src/node/daemon-entry.ts +57 -0
- package/src/node/daemon.ts +629 -0
- package/src/node/index.ts +110 -0
- package/src/node/live-delta.ts +229 -0
- package/src/node/observability.ts +481 -0
- package/src/node/paths.ts +83 -0
- package/src/node/player-run-observer.ts +122 -0
- package/src/node/protocol.ts +200 -0
- package/src/node/spawn.ts +225 -0
- package/src/node/world-events.ts +270 -0
- package/src/player-tools/delta-tracker.ts +1 -0
- package/src/player-tools/index.ts +10 -0
- package/src/player-tools/node-adapter.ts +9 -0
- package/src/player-tools/observation.ts +1 -0
- package/src/player-tools/player-host.ts +92 -0
- package/src/player-tools/push-events.ts +1 -0
- package/src/player-tools/query-protocol.ts +1 -0
- package/src/player-tools/refusal-prose.ts +1 -0
- package/src/player-tools/resolution.ts +1 -0
- package/src/player-tools/tool-registry.ts +1 -0
- package/src/player-tools/tools.ts +42 -0
- package/src/player-tools/world-events.ts +119 -0
- package/src/session.ts +604 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Node-host compatibility seam. Canonical browser-safe PLAYER/DM execution
|
|
2
|
+
// lives in @spellkave/tool-player; this subpath adds explicit Node ports.
|
|
3
|
+
|
|
4
|
+
// MCP consumes the renderer through the session-owned execution seam, keeping
|
|
5
|
+
// its package edge to contracts + session only.
|
|
6
|
+
export { renderWorldEventLine } from "@spellkave/tool-runtime";
|
|
7
|
+
export * from "./node-adapter.ts";
|
|
8
|
+
export * from "./player-host.ts";
|
|
9
|
+
export * from "./tools.ts";
|
|
10
|
+
export * from "./world-events.ts";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { nodeToolCapability } from "@spellkave/tool-node";
|
|
2
|
+
import type { NodeToolCapabilities } from "@spellkave/tool-runtime";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Transitional Node/Pi composition boundary for legacy companion factories.
|
|
6
|
+
* Portable definition bodies receive this only through runtime `bindTool`;
|
|
7
|
+
* browser hosts bind tool-runtime directly and intentionally omit it.
|
|
8
|
+
*/
|
|
9
|
+
export const companionNodeCapabilities: NodeToolCapabilities = { node: nodeToolCapability };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@spellkave/tool-player";
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { createActorClient, type SpellkaveSession, snapshotDb } from "@spellkave/sdk";
|
|
2
|
+
import type { SpellkaveToolContext } from "@spellkave/tool-player";
|
|
3
|
+
import { createActingContextSelector } from "../acting-context.ts";
|
|
4
|
+
import type { ActiveConnection, ConnectionController } from "../node/connection-controller.ts";
|
|
5
|
+
import { createWorldEventFeed, type WorldEventFeed } from "./world-events.ts";
|
|
6
|
+
|
|
7
|
+
export interface PlayerHost {
|
|
8
|
+
context: SpellkaveToolContext;
|
|
9
|
+
actingContext(): ReturnType<typeof createActingContextSelector>;
|
|
10
|
+
worldEvents: WorldEventFeed;
|
|
11
|
+
requireActive(): ActiveConnection;
|
|
12
|
+
dispose(): void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Connection-following ports shared by PLAYER hosts; the caller binds the canonical definitions. */
|
|
16
|
+
export function createPlayerHost(
|
|
17
|
+
controller: ConnectionController,
|
|
18
|
+
options: { boundCharacter?: boolean } = {},
|
|
19
|
+
): PlayerHost {
|
|
20
|
+
let replyLifetime = new AbortController();
|
|
21
|
+
const unsubscribe = controller.onDisconnected(() => {
|
|
22
|
+
replyLifetime.abort(new Error("Spellkave connection closed"));
|
|
23
|
+
replyLifetime = new AbortController();
|
|
24
|
+
});
|
|
25
|
+
const requireActive = () => {
|
|
26
|
+
const active = controller.active();
|
|
27
|
+
if (!active) throw new Error("Spellkave is not connected.");
|
|
28
|
+
return active;
|
|
29
|
+
};
|
|
30
|
+
let selectingSession: SpellkaveSession | undefined;
|
|
31
|
+
let selector: ReturnType<typeof createActingContextSelector> | undefined;
|
|
32
|
+
const actingContext = () => {
|
|
33
|
+
const session = requireActive().session as SpellkaveSession;
|
|
34
|
+
if (session !== selectingSession) {
|
|
35
|
+
selectingSession = session;
|
|
36
|
+
selector = createActingContextSelector(session, {
|
|
37
|
+
boundBodyId: options.boundCharacter ? requireActive().actor : undefined,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (!selector) throw new Error("acting_context_unavailable");
|
|
41
|
+
return selector;
|
|
42
|
+
};
|
|
43
|
+
const worldEvents = createWorldEventFeed({
|
|
44
|
+
active: () => controller.active()?.session.worldEvents,
|
|
45
|
+
});
|
|
46
|
+
const actingBody = () => {
|
|
47
|
+
if (!options.boundCharacter) return actingContext().resolve().bodyId;
|
|
48
|
+
const body = requireActive().actor;
|
|
49
|
+
if (
|
|
50
|
+
!actingContext()
|
|
51
|
+
.read()
|
|
52
|
+
.some((choice) => choice.kind === "character" && choice.bodyId === body)
|
|
53
|
+
)
|
|
54
|
+
throw new Error("acting_context_unavailable");
|
|
55
|
+
return body;
|
|
56
|
+
};
|
|
57
|
+
const context = {
|
|
58
|
+
db: () => snapshotDb(requireActive().session as SpellkaveSession),
|
|
59
|
+
get controlReducers() {
|
|
60
|
+
return (requireActive().session as SpellkaveSession).conn.reducers;
|
|
61
|
+
},
|
|
62
|
+
act: {
|
|
63
|
+
actorId: () => actingBody(),
|
|
64
|
+
actorClient: () =>
|
|
65
|
+
createActorClient(requireActive().session as SpellkaveSession, actingBody(), {
|
|
66
|
+
successOnProvenance: true,
|
|
67
|
+
}),
|
|
68
|
+
},
|
|
69
|
+
worldEvents: {
|
|
70
|
+
builder: worldEvents.builder,
|
|
71
|
+
get replySignal() {
|
|
72
|
+
return replyLifetime.signal;
|
|
73
|
+
},
|
|
74
|
+
estimator: () => controller.active()?.session.estimator,
|
|
75
|
+
onUpdate: (notify: () => void) => {
|
|
76
|
+
const subscribe = requireActive().onUpdate;
|
|
77
|
+
if (!subscribe) throw new Error("Active connection has no cache-update subscription");
|
|
78
|
+
return subscribe(notify);
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
return {
|
|
83
|
+
context,
|
|
84
|
+
actingContext,
|
|
85
|
+
worldEvents,
|
|
86
|
+
requireActive,
|
|
87
|
+
dispose() {
|
|
88
|
+
unsubscribe();
|
|
89
|
+
replyLifetime.abort();
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@spellkave/tool-player";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@spellkave/tool-player";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@spellkave/tool-player";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@spellkave/tool-player";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@spellkave/tool-player";
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createDmExtensionTools as createPortableDmExtensionTools,
|
|
3
|
+
createMapTool as createPortableMapTool,
|
|
4
|
+
createOrientTool as createPortableOrientTool,
|
|
5
|
+
createPlayerFacingToolMap as createPortablePlayerFacingToolMap,
|
|
6
|
+
createPlayerFacingTools as createPortablePlayerFacingTools,
|
|
7
|
+
createSpellkaveTools as createPortableSpellkaveTools,
|
|
8
|
+
type PlayerToolState,
|
|
9
|
+
type SpellkaveToolContext,
|
|
10
|
+
} from "@spellkave/tool-player";
|
|
11
|
+
import type { NodeToolCapabilities } from "@spellkave/tool-runtime";
|
|
12
|
+
import { companionNodeCapabilities } from "./node-adapter.ts";
|
|
13
|
+
|
|
14
|
+
export * from "@spellkave/tool-player";
|
|
15
|
+
export { createPortablePlayerFacingTools };
|
|
16
|
+
|
|
17
|
+
const nodeCapabilities: NodeToolCapabilities = companionNodeCapabilities;
|
|
18
|
+
|
|
19
|
+
/** Node host compatibility: portable definitions plus the explicit Node ports. */
|
|
20
|
+
export function createSpellkaveTools(context: SpellkaveToolContext) {
|
|
21
|
+
return createPortableSpellkaveTools(context, nodeCapabilities, false);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createPlayerFacingTools(context: SpellkaveToolContext) {
|
|
25
|
+
return createPortablePlayerFacingTools(context, nodeCapabilities, false);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function createDmExtensionTools(context: SpellkaveToolContext) {
|
|
29
|
+
return createPortableDmExtensionTools(context, nodeCapabilities, false);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function createPlayerFacingToolMap(context: SpellkaveToolContext, state?: PlayerToolState) {
|
|
33
|
+
return createPortablePlayerFacingToolMap(context, nodeCapabilities, false, state);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function createOrientTool(context: SpellkaveToolContext) {
|
|
37
|
+
return createPortableOrientTool(context, nodeCapabilities, false);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function createMapTool(context: SpellkaveToolContext) {
|
|
41
|
+
return createPortableMapTool(context, nodeCapabilities, false);
|
|
42
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// sk-dms.16 — the hand-play player's EAR.
|
|
2
|
+
//
|
|
3
|
+
// The world already pushes: `@spellkave/tool-player`'s `push-events.ts`
|
|
4
|
+
// turns changes in the connected actor's own perception-gated views into a
|
|
5
|
+
// closed `WorldEvent` union (ADR-0093 D-2) — session lifecycle, turn
|
|
6
|
+
// transitions, damage taken, reaction windows, new advancements, and speech
|
|
7
|
+
// heard. `@spellkave/npc`'s seats consume it, and so does the autonomous
|
|
8
|
+
// runtime (`push-delivery.ts`). This Pi extension did not, so a human's
|
|
9
|
+
// player-agent was the only participant in the world without an ear: in the
|
|
10
|
+
// sk-dms.8 hand-play Aldwin answered in the SAME tick as the question, the
|
|
11
|
+
// server recorded Renn as having heard it, and the agent still reported —
|
|
12
|
+
// correctly, from what it could see — that nothing had been said.
|
|
13
|
+
//
|
|
14
|
+
// DELIVERY SHAPE: events are appended to the NEXT Spellkave tool result rather
|
|
15
|
+
// than steered in as their own message. Two reasons. It keeps a headless
|
|
16
|
+
// `pi --print` turn coherent (a steer mid-`-p` run has no turn boundary to
|
|
17
|
+
// land on), and it puts the world's line exactly where the agent is already
|
|
18
|
+
// looking — beside the receipt of the act that provoked it.
|
|
19
|
+
//
|
|
20
|
+
// PRIMING (D-5 recreate-trap): `session.worldEvents` on the SDK is the one
|
|
21
|
+
// cursor per CONNECTION (one per session, lazily memoized on first access;
|
|
22
|
+
// see the doc comment on `SpellkaveSession.worldEvents` in
|
|
23
|
+
// `packages/spellkave-sdk/src/connection.ts` and
|
|
24
|
+
// `ledger/designs/session-owns-the-world-event-cursor.md`). The SDK
|
|
25
|
+
// constructs it but never primes it — Pi's own priming moment is implicit:
|
|
26
|
+
// the first `drainLines()` after a connect IS the priming call and returns
|
|
27
|
+
// nothing by design, because everything already true when the connection was
|
|
28
|
+
// made is not news. `createWorldEventFeed` below returns a STABLE object for
|
|
29
|
+
// the extension's whole process lifetime (`main.ts` binds
|
|
30
|
+
// `worldEvents.builder` once at activation and cannot rebind it), but that
|
|
31
|
+
// object only FORWARDS to whichever cursor
|
|
32
|
+
// `controller.active()?.session.worldEvents` currently names — it holds no
|
|
33
|
+
// state of its own. A reconnect therefore reaches a FRESH cursor that primes
|
|
34
|
+
// quiet on the new subscription, which is the honest answer: nothing was
|
|
35
|
+
// observable while there was no subscription. Before this slice the builder
|
|
36
|
+
// was constructed ONCE per process with lazy accessors and carried its
|
|
37
|
+
// seen-sets ACROSS a disconnect/reconnect, so the first drain after
|
|
38
|
+
// reconnecting reported the whole delta accumulated while unsubscribed as
|
|
39
|
+
// news — a flood this fixes.
|
|
40
|
+
//
|
|
41
|
+
// KNOWN GAP — CROSS-TURN EVENTS ARE STILL LOST HEADLESS (sk-dms.20). The
|
|
42
|
+
// cursor above is per CONNECTION, not per TURN, so nothing is lost within one
|
|
43
|
+
// long-lived connection. But `just pi-handplay <session> "…"` is one PROCESS
|
|
44
|
+
// PER TURN, and a process boundary is also a connection boundary: each
|
|
45
|
+
// headless turn opens a fresh connection, whose fresh cursor primes against
|
|
46
|
+
// the world as it stands at that moment. An NPC's own combat turn taken
|
|
47
|
+
// between two player turns is already true by the time the next process
|
|
48
|
+
// connects, and is swallowed as not-news. This slice does not close
|
|
49
|
+
// sk-dms.20 — per the bead's 2026-08-11 operator steer, the gap is a property
|
|
50
|
+
// of ONE-PROCESS-PER-TURN, not of the wiring, and persisting seen-sets across
|
|
51
|
+
// processes is explicitly NOT the fix. The fix is a long-lived process
|
|
52
|
+
// holding the connection across turns: `pi --mode rpc`, or `herdr agent
|
|
53
|
+
// prompt` (the shape the playtest skill already uses — "interactive, never
|
|
54
|
+
// headless"). An interactive session (one long-lived process) does not have
|
|
55
|
+
// this gap today.
|
|
56
|
+
|
|
57
|
+
import type { WorldEvent, WorldEventBuilder } from "@spellkave/tool-player";
|
|
58
|
+
import { renderWorldEventLine } from "@spellkave/tool-runtime";
|
|
59
|
+
|
|
60
|
+
export interface WorldEventFeedDeps {
|
|
61
|
+
/**
|
|
62
|
+
* The active connection's world-event cursor, or `undefined` while
|
|
63
|
+
* disconnected. A THUNK, not a value — it must follow
|
|
64
|
+
* `controller.active()` the same way `estimator` does in `main.ts`, so a
|
|
65
|
+
* reconnect resolves to the NEW connection's cursor instead of forwarding
|
|
66
|
+
* to one from a connection that already ended.
|
|
67
|
+
*/
|
|
68
|
+
active: () => WorldEventBuilder | undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface WorldEventFeed {
|
|
72
|
+
/**
|
|
73
|
+
* Drain whatever the world has done to this actor since the last drain,
|
|
74
|
+
* rendered one 🌍 line each. Returns `[]` while disconnected, on the priming
|
|
75
|
+
* call, and whenever nothing happened — the common case, so callers can
|
|
76
|
+
* append unconditionally.
|
|
77
|
+
*/
|
|
78
|
+
drainLines(): string[];
|
|
79
|
+
/**
|
|
80
|
+
* sk-0m4.73: the SAME forwarding builder `drainLines()` calls
|
|
81
|
+
* `diffAndAdvance()` on — exposed so `main.ts` can inject it into
|
|
82
|
+
* `createPlayerFacingTools`'s `worldEvents.builder` option (the
|
|
83
|
+
* `communicate` reply window, and the `consequences` register every act
|
|
84
|
+
* tool takes from). This is a stable process-lifetime shim, not a cursor:
|
|
85
|
+
* it forwards each call to `deps.active()`'s CURRENT value, so every
|
|
86
|
+
* caller of `worldEvents.builder` stays correct across a reconnect without
|
|
87
|
+
* being rebound.
|
|
88
|
+
*/
|
|
89
|
+
readonly builder: WorldEventBuilder;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function createWorldEventFeed(deps: WorldEventFeedDeps): WorldEventFeed {
|
|
93
|
+
// `main.ts` binds this object once at activation and cannot rebind it, so
|
|
94
|
+
// it must stay stable for the extension's whole process lifetime. It holds
|
|
95
|
+
// no cursor of its own — each entry point re-reads `deps.active()` and
|
|
96
|
+
// delegates, returning `[]` while disconnected.
|
|
97
|
+
const builder: WorldEventBuilder = {
|
|
98
|
+
diffAndAdvance(): WorldEvent[] {
|
|
99
|
+
return deps.active()?.diffAndAdvance() ?? [];
|
|
100
|
+
},
|
|
101
|
+
takeWhere(predicate: (event: WorldEvent) => boolean): WorldEvent[] {
|
|
102
|
+
return deps.active()?.takeWhere(predicate) ?? [];
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
drainLines(): string[] {
|
|
108
|
+
try {
|
|
109
|
+
return builder.diffAndAdvance().map(renderWorldEventLine);
|
|
110
|
+
} catch {
|
|
111
|
+
// A drain must never turn a working tool call into a failed one: the
|
|
112
|
+
// events are a courtesy channel, the tool's own result is the contract.
|
|
113
|
+
// A disconnect racing the drain is the expected case here.
|
|
114
|
+
return [];
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
builder,
|
|
118
|
+
};
|
|
119
|
+
}
|