@spellkave/session 0.0.0-stage → 0.1.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/LICENSE +89 -0
- package/bin/spellkave-session-daemon.js +5 -0
- package/package.json +58 -4
- 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
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# PolyForm Shield License 1.0.0
|
|
2
|
+
|
|
3
|
+
<https://polyformproject.org/licenses/shield/1.0.0>
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
|
|
8
|
+
|
|
9
|
+
## Copyright License
|
|
10
|
+
|
|
11
|
+
The licensor grants you a copyright license for the software to do everything you might do with the software that would otherwise infringe the licensor's copyright in it for any permitted purpose. However, you may only distribute the software according to [Distribution License](#distribution-license) and make changes or new works based on the software according to [Changes and New Works License](#changes-and-new-works-license).
|
|
12
|
+
|
|
13
|
+
## Distribution License
|
|
14
|
+
|
|
15
|
+
The licensor grants you an additional copyright license to distribute copies of the software. Your license to distribute covers distributing the software with changes and new works permitted by [Changes and New Works License](#changes-and-new-works-license).
|
|
16
|
+
|
|
17
|
+
## Notices
|
|
18
|
+
|
|
19
|
+
You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms or the URL for them above, as well as copies of any plain-text lines beginning with `Required Notice:` that the licensor provided with the software. For example:
|
|
20
|
+
|
|
21
|
+
> Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
|
|
22
|
+
|
|
23
|
+
## Changes and New Works License
|
|
24
|
+
|
|
25
|
+
The licensor grants you an additional copyright license to make changes and new works based on the software for any permitted purpose.
|
|
26
|
+
|
|
27
|
+
## Patent License
|
|
28
|
+
|
|
29
|
+
The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
|
|
30
|
+
|
|
31
|
+
## Noncompete
|
|
32
|
+
|
|
33
|
+
Any purpose is a permitted purpose, except for providing any product that competes with the software or any product the licensor or any of its affiliates provides using the software.
|
|
34
|
+
|
|
35
|
+
## Competition
|
|
36
|
+
|
|
37
|
+
Goods and services compete even when they provide functionality through different kinds of interfaces or for different technical platforms. Applications can compete with services, libraries with plugins, frameworks with development tools, and so on, even if they're written in different programming languages or for different computer architectures. Goods and services compete even when provided free of charge. If you market a product as a practical substitute for the software or another product, it definitely competes.
|
|
38
|
+
|
|
39
|
+
## New Products
|
|
40
|
+
|
|
41
|
+
If you are using the software to provide a product that does not compete, but the licensor or any of its affiliates brings your product into competition by providing a new version of the software or another product using the software, you may continue using versions of the software available under these terms beforehand to provide your competing product, but not any later versions.
|
|
42
|
+
|
|
43
|
+
## Discontinued Products
|
|
44
|
+
|
|
45
|
+
You may begin using the software to compete with a product or service that the licensor or any of its affiliates has stopped providing, unless the licensor includes a plain-text line beginning with `Licensor Line of Business:` with the software that mentions that line of business. For example:
|
|
46
|
+
|
|
47
|
+
> Licensor Line of Business: YoyodyneCMS Content Management System (http://example.com/cms)
|
|
48
|
+
|
|
49
|
+
## Sales of Business
|
|
50
|
+
|
|
51
|
+
If the licensor or any of its affiliates sells a line of business developing the software or using the software to provide a product, the buyer can also enforce [Noncompete](#noncompete) for that product.
|
|
52
|
+
|
|
53
|
+
## Fair Use
|
|
54
|
+
|
|
55
|
+
You may have "fair use" rights for the software under the law. These terms do not limit them.
|
|
56
|
+
|
|
57
|
+
## No Other Rights
|
|
58
|
+
|
|
59
|
+
These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
|
|
60
|
+
|
|
61
|
+
## Patent Defense
|
|
62
|
+
|
|
63
|
+
If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
64
|
+
|
|
65
|
+
## Violations
|
|
66
|
+
|
|
67
|
+
The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
|
|
68
|
+
|
|
69
|
+
## No Liability
|
|
70
|
+
|
|
71
|
+
***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
|
|
72
|
+
|
|
73
|
+
## Definitions
|
|
74
|
+
|
|
75
|
+
The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
|
|
76
|
+
|
|
77
|
+
A **product** can be a good or service, or a combination of them.
|
|
78
|
+
|
|
79
|
+
**You** refers to the individual or entity agreeing to these terms.
|
|
80
|
+
|
|
81
|
+
**Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all its affiliates.
|
|
82
|
+
|
|
83
|
+
**Affiliates** means the other organizations than an organization has control over, is under the control of, or is under common control with.
|
|
84
|
+
|
|
85
|
+
**Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
86
|
+
|
|
87
|
+
**Your licenses** are all the licenses granted to you for the software under these terms.
|
|
88
|
+
|
|
89
|
+
**Use** means anything you do with the software requiring one of your licenses.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
#!/usr/bin/env tsx
|
|
2
|
+
// Daemon process launcher — `spawn.ts`'s default `ensureDaemon` spawn target.
|
|
3
|
+
// Mirrors `packages/spellkave-cli/bin/sk.js`'s shape: a thin launcher whose
|
|
4
|
+
// one job is running the real (TypeScript) entry point.
|
|
5
|
+
await import("../src/node/daemon-entry.ts");
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spellkave/session",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Binding-keyed live Spellkave session core + daemon host (#1990) — one connect/subscribe-on-connect lifecycle shared by the daemon, the Pi extension, and future browser hosts.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"types": "./src/index.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.ts",
|
|
11
|
+
"default": "./src/index.ts"
|
|
12
|
+
},
|
|
13
|
+
"./node": {
|
|
14
|
+
"types": "./src/node/index.ts",
|
|
15
|
+
"default": "./src/node/index.ts"
|
|
16
|
+
},
|
|
17
|
+
"./node/observability": {
|
|
18
|
+
"types": "./src/node/observability.ts",
|
|
19
|
+
"default": "./src/node/observability.ts"
|
|
20
|
+
},
|
|
21
|
+
"./player-tools": {
|
|
22
|
+
"types": "./src/player-tools/index.ts",
|
|
23
|
+
"default": "./src/player-tools/index.ts"
|
|
24
|
+
},
|
|
25
|
+
"./authoring": {
|
|
26
|
+
"types": "./src/authoring/index.ts",
|
|
27
|
+
"default": "./src/authoring/index.ts"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"src",
|
|
32
|
+
"bin"
|
|
33
|
+
],
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@spellkave/agent-observability": "0.1.0",
|
|
36
|
+
"@spellkave/sdk": "0.1.0",
|
|
37
|
+
"@spellkave/tool-contracts": "0.1.0",
|
|
38
|
+
"@spellkave/tool-authoring": "0.1.0",
|
|
39
|
+
"@spellkave/tool-node": "0.1.0",
|
|
40
|
+
"@spellkave/tool-player": "0.1.0",
|
|
41
|
+
"@spellkave/tool-runtime": "0.1.0",
|
|
42
|
+
"@spellkave/world-author": "0.1.0",
|
|
43
|
+
"@spellkave/world-map": "0.1.0",
|
|
44
|
+
"tsx": "^4.19.2",
|
|
45
|
+
"@spellkave/bindings": "2.1.0"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"typebox": "*"
|
|
49
|
+
},
|
|
50
|
+
"license": "PolyForm-Shield-1.0.0",
|
|
51
|
+
"repository": {
|
|
52
|
+
"type": "git",
|
|
53
|
+
"url": "git+https://github.com/b4mad-technologies/sk-sdk.git",
|
|
54
|
+
"directory": "packages/spellkave-session"
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public",
|
|
58
|
+
"registry": "https://registry.npmjs.org/"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/** A caller-owned selection; every resolution reads the current connection. */
|
|
2
|
+
import type { ReaderManifest, SpellkaveSession } from "@spellkave/sdk";
|
|
3
|
+
import { ACTING_CONTEXT_PARAMS, type LiveToolAuthority } from "@spellkave/tool-contracts";
|
|
4
|
+
import { bindTool, defineTool } from "@spellkave/tool-runtime";
|
|
5
|
+
|
|
6
|
+
export const AUTHORITY_TOOL_READER_MANIFEST = {
|
|
7
|
+
requiredTables: [
|
|
8
|
+
"role_definition",
|
|
9
|
+
"role_aggregation",
|
|
10
|
+
"visible_role_binding",
|
|
11
|
+
"grant_inventory",
|
|
12
|
+
"capability_definition",
|
|
13
|
+
"extent_kind_definition",
|
|
14
|
+
"visible_actor_identity",
|
|
15
|
+
"visible_capability",
|
|
16
|
+
"actor",
|
|
17
|
+
"world_time",
|
|
18
|
+
],
|
|
19
|
+
} satisfies ReaderManifest;
|
|
20
|
+
|
|
21
|
+
export interface ActingContextChoice {
|
|
22
|
+
kind: "character" | "presence";
|
|
23
|
+
bodyId: string;
|
|
24
|
+
bindingId?: string;
|
|
25
|
+
capabilityIds: string[];
|
|
26
|
+
}
|
|
27
|
+
const active = (expires: bigint | undefined, tick: bigint) =>
|
|
28
|
+
expires === undefined || expires === 0n || expires > tick;
|
|
29
|
+
/**
|
|
30
|
+
* The caller's acting choices. `boundBodyId` (ADR-0161 D-5): the character
|
|
31
|
+
* body a seat-routed session was attached for; the identity's other
|
|
32
|
+
* characters are then not choices (presences stay), so neither acting nor
|
|
33
|
+
* the advertised tool authority can reach another hero of the person.
|
|
34
|
+
*/
|
|
35
|
+
export function callerActingContexts(
|
|
36
|
+
session: SpellkaveSession,
|
|
37
|
+
boundBodyId?: string,
|
|
38
|
+
): ActingContextChoice[] {
|
|
39
|
+
const bound = boundBodyId === "" ? undefined : boundBodyId;
|
|
40
|
+
const conn = session.conn;
|
|
41
|
+
const mine = conn.identity?.toHexString();
|
|
42
|
+
if (!mine || !conn.isActive) return [];
|
|
43
|
+
const db = conn.db;
|
|
44
|
+
const tick = [...db.worldTime.iter()][0]?.tick;
|
|
45
|
+
if (tick === undefined) return [];
|
|
46
|
+
const identities = [...db.visibleActorIdentity.iter()].filter(
|
|
47
|
+
(row) => row.identity.toHexString() === mine && row.role !== "co-pilot",
|
|
48
|
+
);
|
|
49
|
+
const actors = [...db.actor.iter()].filter(
|
|
50
|
+
(row) =>
|
|
51
|
+
identities.some((identity) => identity.actorId === row.actorId) && row.currentBody !== 0n,
|
|
52
|
+
);
|
|
53
|
+
const grants = [...db.visibleCapability.iter()].filter(
|
|
54
|
+
(row) => row.grantedAtTick <= tick && active(row.expiresAtTick, tick),
|
|
55
|
+
);
|
|
56
|
+
const ownedBodies = new Set(actors.map((actor) => actor.currentBody));
|
|
57
|
+
const bindings = [...db.visibleRoleBinding.iter()].filter(
|
|
58
|
+
(row) =>
|
|
59
|
+
row.subject === mine &&
|
|
60
|
+
ownedBodies.has(row.presenceEntityId) &&
|
|
61
|
+
row.realisedAtTick !== undefined &&
|
|
62
|
+
active(row.expiresAtTick, tick),
|
|
63
|
+
);
|
|
64
|
+
const choices: ActingContextChoice[] = bindings.map((binding) => ({
|
|
65
|
+
kind: "presence",
|
|
66
|
+
bodyId: String(binding.presenceEntityId),
|
|
67
|
+
bindingId: binding.bindingId,
|
|
68
|
+
capabilityIds: grants
|
|
69
|
+
.filter(
|
|
70
|
+
(grant) =>
|
|
71
|
+
grant.entityId === binding.presenceEntityId &&
|
|
72
|
+
grant.source === `role_binding:${binding.bindingId}`,
|
|
73
|
+
)
|
|
74
|
+
.map((grant) => grant.capabilityId),
|
|
75
|
+
}));
|
|
76
|
+
for (const actor of actors.filter(
|
|
77
|
+
(actor) =>
|
|
78
|
+
identities.some(
|
|
79
|
+
(identity) => identity.actorId === actor.actorId && identity.role === "primary",
|
|
80
|
+
) && !bindings.some((binding) => binding.presenceEntityId === actor.currentBody),
|
|
81
|
+
))
|
|
82
|
+
choices.push({
|
|
83
|
+
kind: "character",
|
|
84
|
+
bodyId: String(actor.currentBody),
|
|
85
|
+
capabilityIds: grants
|
|
86
|
+
.filter((grant) => grant.entityId === actor.currentBody)
|
|
87
|
+
.map((grant) => grant.capabilityId),
|
|
88
|
+
});
|
|
89
|
+
return choices.filter(
|
|
90
|
+
(choice) => choice.kind !== "character" || bound === undefined || choice.bodyId === bound,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function callerToolAuthority(
|
|
95
|
+
session: SpellkaveSession,
|
|
96
|
+
boundBodyId?: string,
|
|
97
|
+
): LiveToolAuthority {
|
|
98
|
+
const choices = callerActingContexts(session, boundBodyId);
|
|
99
|
+
const db = session.conn.db;
|
|
100
|
+
const aggregations = [...db.roleAggregation.iter()];
|
|
101
|
+
return {
|
|
102
|
+
character: choices.some((choice) => choice.kind === "character"),
|
|
103
|
+
presence: choices.some((choice) => choice.kind === "presence"),
|
|
104
|
+
publisher: false,
|
|
105
|
+
capabilities: [...new Set(choices.flatMap((choice) => choice.capabilityIds))],
|
|
106
|
+
bodyCapabilities: choices.map((choice) => choice.capabilityIds),
|
|
107
|
+
roles: [...db.roleDefinition.iter()].map((role) => ({
|
|
108
|
+
roleId: role.roleId,
|
|
109
|
+
capabilityIds: [
|
|
110
|
+
...new Set([
|
|
111
|
+
...role.capabilityIds,
|
|
112
|
+
...aggregations
|
|
113
|
+
.filter((row) => row.roleId === role.roleId)
|
|
114
|
+
.flatMap((row) => row.capabilityIds),
|
|
115
|
+
]),
|
|
116
|
+
],
|
|
117
|
+
})),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export class ActingContextError extends Error {
|
|
122
|
+
constructor(
|
|
123
|
+
readonly code: string,
|
|
124
|
+
readonly choices: readonly ActingContextChoice[],
|
|
125
|
+
) {
|
|
126
|
+
super(
|
|
127
|
+
`${code}: select one of your eligible binding ids: ${choices
|
|
128
|
+
.map((choice) => choice.bindingId)
|
|
129
|
+
.filter(Boolean)
|
|
130
|
+
.join(", ")}`,
|
|
131
|
+
);
|
|
132
|
+
this.name = "ActingContextError";
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* `boundBodyId`: the character body this session was attached for (the MCP
|
|
137
|
+
* gateway's routed seat, ADR-0161 D-5). An identity may hold several
|
|
138
|
+
* characters in one world (#4168); a session bound to one of them never acts
|
|
139
|
+
* as another, so the other characters are not choices here. Presences stay.
|
|
140
|
+
*/
|
|
141
|
+
export function createActingContextSelector(
|
|
142
|
+
session: SpellkaveSession,
|
|
143
|
+
options: { boundBodyId?: string } = {},
|
|
144
|
+
) {
|
|
145
|
+
let selected: { kind: "character" } | { kind: "presence"; bindingId: string } | undefined;
|
|
146
|
+
const read = () => callerActingContexts(session, options.boundBodyId);
|
|
147
|
+
return {
|
|
148
|
+
read,
|
|
149
|
+
select(args: { binding_id?: string; character?: boolean }) {
|
|
150
|
+
if (args.binding_id !== undefined && args.character !== undefined)
|
|
151
|
+
throw new ActingContextError("invalid_acting_context", []);
|
|
152
|
+
const candidate =
|
|
153
|
+
args.binding_id !== undefined
|
|
154
|
+
? { kind: "presence" as const, bindingId: args.binding_id }
|
|
155
|
+
: args.character === true
|
|
156
|
+
? { kind: "character" as const }
|
|
157
|
+
: selected;
|
|
158
|
+
if (
|
|
159
|
+
candidate !== undefined &&
|
|
160
|
+
!read().some(
|
|
161
|
+
(choice) =>
|
|
162
|
+
choice.kind === candidate.kind &&
|
|
163
|
+
(candidate.kind !== "presence" || choice.bindingId === candidate.bindingId),
|
|
164
|
+
)
|
|
165
|
+
) {
|
|
166
|
+
throw new ActingContextError("acting_context_unavailable", []);
|
|
167
|
+
}
|
|
168
|
+
selected = candidate;
|
|
169
|
+
return { choices: read(), selected };
|
|
170
|
+
},
|
|
171
|
+
resolve(capability?: string | readonly string[]): ActingContextChoice {
|
|
172
|
+
const required =
|
|
173
|
+
capability === undefined
|
|
174
|
+
? undefined
|
|
175
|
+
: typeof capability === "string"
|
|
176
|
+
? [capability]
|
|
177
|
+
: capability;
|
|
178
|
+
const matches = (choice: ActingContextChoice) =>
|
|
179
|
+
required === undefined || required.some((id) => choice.capabilityIds.includes(id));
|
|
180
|
+
const owned = read();
|
|
181
|
+
if (selected !== undefined) {
|
|
182
|
+
const chosen = owned.find(
|
|
183
|
+
(choice) =>
|
|
184
|
+
choice.kind === selected?.kind &&
|
|
185
|
+
(selected.kind !== "presence" || choice.bindingId === selected.bindingId),
|
|
186
|
+
);
|
|
187
|
+
if (!chosen || !matches(chosen))
|
|
188
|
+
throw new ActingContextError("acting_context_unavailable", []);
|
|
189
|
+
return chosen;
|
|
190
|
+
}
|
|
191
|
+
const characters = owned.filter((choice) => choice.kind === "character");
|
|
192
|
+
const eligible =
|
|
193
|
+
capability === undefined ? (characters.length ? characters : owned) : owned.filter(matches);
|
|
194
|
+
if (eligible.length !== 1)
|
|
195
|
+
throw new ActingContextError(
|
|
196
|
+
eligible.length ? "ambiguous_acting_context" : "acting_context_unavailable",
|
|
197
|
+
eligible,
|
|
198
|
+
);
|
|
199
|
+
const first = eligible[0];
|
|
200
|
+
if (first === undefined) throw new ActingContextError("acting_context_unavailable", []);
|
|
201
|
+
return first;
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Shared definition: the selector is host-owned and may follow a reconnect. */
|
|
207
|
+
export const actingContextToolDef = defineTool({
|
|
208
|
+
contract: "spellkave_acting_context",
|
|
209
|
+
schema: ACTING_CONTEXT_PARAMS,
|
|
210
|
+
run: async (
|
|
211
|
+
context: { selector: () => ReturnType<typeof createActingContextSelector> },
|
|
212
|
+
args,
|
|
213
|
+
) => {
|
|
214
|
+
const details = context.selector().select(args);
|
|
215
|
+
return { content: [{ type: "text" as const, text: JSON.stringify(details) }], details };
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
export function createActingContextTool(
|
|
219
|
+
selector: () => ReturnType<typeof createActingContextSelector>,
|
|
220
|
+
) {
|
|
221
|
+
return bindTool(actingContextToolDef, { context: { selector }, capabilities: {} });
|
|
222
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/** Hosted composition uses only this session's authenticated connection. */
|
|
2
|
+
import {
|
|
3
|
+
createActorClient,
|
|
4
|
+
enqueueAndWaitManifestApply,
|
|
5
|
+
OWNER_SUBSCRIPTION_TABLES,
|
|
6
|
+
snapshotDb,
|
|
7
|
+
subscribeManifestApplyWait,
|
|
8
|
+
} from "@spellkave/sdk";
|
|
9
|
+
import { type AuthorToolContext, createAuthorFacingBoundTools } from "@spellkave/tool-authoring";
|
|
10
|
+
import { toolContractsForAuthority } from "@spellkave/tool-contracts";
|
|
11
|
+
import {
|
|
12
|
+
createPaceTimeTools,
|
|
13
|
+
createSpellkaveTools,
|
|
14
|
+
type SpellkaveToolContext,
|
|
15
|
+
} from "@spellkave/tool-player";
|
|
16
|
+
import { createScopedInstanceRead } from "@spellkave/world-author";
|
|
17
|
+
import {
|
|
18
|
+
callerToolAuthority,
|
|
19
|
+
createActingContextSelector,
|
|
20
|
+
createActingContextTool,
|
|
21
|
+
} from "../acting-context";
|
|
22
|
+
import type { ToolResult } from "../player-tools/tool-registry";
|
|
23
|
+
import type { LiveSession } from "../session";
|
|
24
|
+
import { createLiveApplysetPort } from "./live-applyset-port";
|
|
25
|
+
import { createLiveAuthorTarget } from "./live-author-target";
|
|
26
|
+
import { createLiveCanIPort } from "./live-can-i-port";
|
|
27
|
+
import { createLiveGrantsPort } from "./live-grants-port";
|
|
28
|
+
import { createLiveScenesPort } from "./live-scenes-port";
|
|
29
|
+
import { createLiveScopedSnapshotPort } from "./live-scoped-instance-port";
|
|
30
|
+
|
|
31
|
+
export function createCallerSessionTools(live: LiveSession, playerContext: SpellkaveToolContext) {
|
|
32
|
+
const session = live.session;
|
|
33
|
+
const selector = createActingContextSelector(session, { boundBodyId: live.actorId });
|
|
34
|
+
const ownerAccessors = new Set<string>(
|
|
35
|
+
OWNER_SUBSCRIPTION_TABLES.flatMap((name) => [
|
|
36
|
+
name,
|
|
37
|
+
name.replace(/_([a-z])/g, (_, letter: string) => letter.toUpperCase()),
|
|
38
|
+
]),
|
|
39
|
+
);
|
|
40
|
+
// User tool readers cannot even dereference a publisher-only base handle.
|
|
41
|
+
const callerDb = new Proxy(snapshotDb(session), {
|
|
42
|
+
get: (db, name, receiver) =>
|
|
43
|
+
typeof name === "string" && ownerAccessors.has(name)
|
|
44
|
+
? undefined
|
|
45
|
+
: Reflect.get(db, name, receiver),
|
|
46
|
+
});
|
|
47
|
+
const scoped = createLiveScopedSnapshotPort({ session, world: live.binding });
|
|
48
|
+
let scratch: string | null = null;
|
|
49
|
+
let waitReady: Promise<void> | undefined;
|
|
50
|
+
const apply = async (toml: string) => {
|
|
51
|
+
waitReady ??= subscribeManifestApplyWait(session.conn.subscriptionBuilder());
|
|
52
|
+
await waitReady;
|
|
53
|
+
const result = await enqueueAndWaitManifestApply({
|
|
54
|
+
digestHandle: session.conn.db.manifestApplyDigest,
|
|
55
|
+
outcomeHandle: session.conn.db.manifestApplyOutcome,
|
|
56
|
+
onUpdate: live.onUpdate,
|
|
57
|
+
enqueue: () => session.conn.reducers.rApplyManifestDelta({ toml }),
|
|
58
|
+
});
|
|
59
|
+
return result.ok ? { ok: true } : { ok: false, detail: result.detail };
|
|
60
|
+
};
|
|
61
|
+
const applysetBackend = createLiveApplysetPort({
|
|
62
|
+
session,
|
|
63
|
+
rootPathFor: (path) => path,
|
|
64
|
+
scopedSnapshot: scoped,
|
|
65
|
+
});
|
|
66
|
+
const applyset: typeof applysetBackend = {
|
|
67
|
+
...applysetBackend,
|
|
68
|
+
applyBundle: async (call) => {
|
|
69
|
+
waitReady ??= subscribeManifestApplyWait(session.conn.subscriptionBuilder());
|
|
70
|
+
await waitReady;
|
|
71
|
+
return applysetBackend.applyBundle(call);
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
const author: AuthorToolContext = {
|
|
75
|
+
workspace: {
|
|
76
|
+
readToml: () => scratch,
|
|
77
|
+
writeToml: (text) => {
|
|
78
|
+
scratch = text;
|
|
79
|
+
return "session-scratch";
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
applyDelta: apply,
|
|
83
|
+
applyset,
|
|
84
|
+
canI: createLiveCanIPort({
|
|
85
|
+
conn: session.conn as never,
|
|
86
|
+
db: () => ({}),
|
|
87
|
+
resolveAuthoredEntity: async (key) => {
|
|
88
|
+
const captured = await scoped.read();
|
|
89
|
+
for (const row of captured.snapshot.visible_manifest_authored_entity?.iter() ?? []) {
|
|
90
|
+
if (row.manifestKey === key || String(row.manifestKey).endsWith(`:${key}`))
|
|
91
|
+
return String(row.entityId);
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
},
|
|
95
|
+
}),
|
|
96
|
+
readInstance: createScopedInstanceRead(scoped),
|
|
97
|
+
scenes: createLiveScenesPort({
|
|
98
|
+
session: () => session,
|
|
99
|
+
endpoint: () => `${live.binding.url}/${live.binding.db}`,
|
|
100
|
+
apply,
|
|
101
|
+
}),
|
|
102
|
+
grants: createLiveGrantsPort({
|
|
103
|
+
session: () => session,
|
|
104
|
+
endpoint: () => `${live.binding.url}/${live.binding.db}`,
|
|
105
|
+
apply,
|
|
106
|
+
}),
|
|
107
|
+
target: createLiveAuthorTarget({
|
|
108
|
+
db: () => callerDb,
|
|
109
|
+
readsAsPublisher: () => false,
|
|
110
|
+
scopedSnapshot: scoped,
|
|
111
|
+
applyDelta: apply,
|
|
112
|
+
}),
|
|
113
|
+
};
|
|
114
|
+
const actorClients = new Map<string, ReturnType<typeof createActorClient>>();
|
|
115
|
+
const actorClient = (bodyId: string) => {
|
|
116
|
+
let client = actorClients.get(bodyId);
|
|
117
|
+
if (!client) {
|
|
118
|
+
client = createActorClient(session, bodyId, { successOnProvenance: true });
|
|
119
|
+
actorClients.set(bodyId, client);
|
|
120
|
+
}
|
|
121
|
+
return client;
|
|
122
|
+
};
|
|
123
|
+
let invocationBody: string | undefined;
|
|
124
|
+
const context: SpellkaveToolContext = {
|
|
125
|
+
...playerContext,
|
|
126
|
+
db: () => callerDb,
|
|
127
|
+
act: {
|
|
128
|
+
actorId: () => invocationBody ?? selector.resolve().bodyId,
|
|
129
|
+
actorClient: () => actorClient(invocationBody ?? selector.resolve().bodyId),
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
const selection = createActingContextTool(() => selector);
|
|
133
|
+
const ports = [
|
|
134
|
+
"canI",
|
|
135
|
+
"actingContext",
|
|
136
|
+
"control",
|
|
137
|
+
"paceTime",
|
|
138
|
+
"applyDelta",
|
|
139
|
+
"applyset",
|
|
140
|
+
"readInstance",
|
|
141
|
+
"grants",
|
|
142
|
+
"scenes",
|
|
143
|
+
];
|
|
144
|
+
const bound = [
|
|
145
|
+
...createSpellkaveTools(context),
|
|
146
|
+
...createAuthorFacingBoundTools(author),
|
|
147
|
+
...createPaceTimeTools({ connection: session.conn }),
|
|
148
|
+
selection,
|
|
149
|
+
];
|
|
150
|
+
function tools() {
|
|
151
|
+
const contracts = toolContractsForAuthority(callerToolAuthority(session, live.actorId), ports);
|
|
152
|
+
return contracts.flatMap((contract) => {
|
|
153
|
+
const tool = bound.find((candidate) => candidate.name === contract.name);
|
|
154
|
+
if (!tool) return [];
|
|
155
|
+
return [
|
|
156
|
+
{
|
|
157
|
+
...tool,
|
|
158
|
+
execute: async (id: string, args: never) => {
|
|
159
|
+
if (
|
|
160
|
+
!toolContractsForAuthority(callerToolAuthority(session, live.actorId), ports).some(
|
|
161
|
+
(row) => row.name === tool.name,
|
|
162
|
+
)
|
|
163
|
+
)
|
|
164
|
+
throw new Error(
|
|
165
|
+
"authority_unavailable: current authority no longer offers this tool",
|
|
166
|
+
);
|
|
167
|
+
if (tool.name === "spellkave_manipulate") {
|
|
168
|
+
const action = (args as { action_id: string }).action_id;
|
|
169
|
+
const db = session.conn.db;
|
|
170
|
+
const outcomeIds = new Set(
|
|
171
|
+
[...db.outcomeTemplate.iter()]
|
|
172
|
+
.filter((row) => row.manipulationActionId === action)
|
|
173
|
+
.map((row) => row.outcomeTemplateId),
|
|
174
|
+
);
|
|
175
|
+
// A custom action may have several authored providers. Discovery is a
|
|
176
|
+
// superset; never let arbitrary catalog iteration pick a provider.
|
|
177
|
+
const capabilities = [...db.affordanceTemplate.iter()]
|
|
178
|
+
.filter(
|
|
179
|
+
(row) =>
|
|
180
|
+
outcomeIds.has(row.outcomeTemplateId) &&
|
|
181
|
+
row.capabilityId.startsWith("frontier."),
|
|
182
|
+
)
|
|
183
|
+
.map((row) => row.capabilityId);
|
|
184
|
+
const choice = selector.resolve(capabilities.length ? capabilities : undefined);
|
|
185
|
+
// The daemon serializes invocations. Keep the same event/provenance
|
|
186
|
+
// executor while selecting this call's eligible acting body.
|
|
187
|
+
invocationBody = choice.bodyId;
|
|
188
|
+
try {
|
|
189
|
+
const result = await tool.execute(id, args);
|
|
190
|
+
if (
|
|
191
|
+
!result ||
|
|
192
|
+
typeof result !== "object" ||
|
|
193
|
+
!("content" in result) ||
|
|
194
|
+
!Array.isArray(result.content)
|
|
195
|
+
)
|
|
196
|
+
throw new Error("invalid tool result envelope");
|
|
197
|
+
return result as ToolResult<unknown>;
|
|
198
|
+
} finally {
|
|
199
|
+
invocationBody = undefined;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (
|
|
203
|
+
tool.name === "spellkave_move" &&
|
|
204
|
+
(args as { as_body_id?: string }).as_body_id === undefined &&
|
|
205
|
+
selector.resolve().kind !== "character"
|
|
206
|
+
)
|
|
207
|
+
throw new Error(
|
|
208
|
+
"presence_movement_unavailable: select a character or consent-checked as_body_id",
|
|
209
|
+
);
|
|
210
|
+
// Author registries erase their result type at the heterogeneous boundary.
|
|
211
|
+
// Check the shared result envelope before crossing the IPC boundary.
|
|
212
|
+
const result = await tool.execute(id, args);
|
|
213
|
+
if (
|
|
214
|
+
result === null ||
|
|
215
|
+
typeof result !== "object" ||
|
|
216
|
+
!("content" in result) ||
|
|
217
|
+
!Array.isArray(result.content)
|
|
218
|
+
)
|
|
219
|
+
throw new Error("invalid tool result envelope");
|
|
220
|
+
return result as ToolResult<unknown>;
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
];
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
return { tools, close: () => scoped.close() };
|
|
227
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Live SDK composition for canonical AUTHOR execution. */
|
|
2
|
+
import type { SnapshotDb } from "@spellkave/sdk/replay";
|
|
3
|
+
import {
|
|
4
|
+
createLiveAuthorTarget as createSessionLiveAuthorTarget,
|
|
5
|
+
type LiveAuthorTargetDeps,
|
|
6
|
+
} from "./live-author-target.ts";
|
|
7
|
+
|
|
8
|
+
export * from "@spellkave/tool-authoring";
|
|
9
|
+
|
|
10
|
+
export interface SessionLiveAuthorTargetDeps extends Omit<LiveAuthorTargetDeps, "db"> {
|
|
11
|
+
db: SnapshotDb | (() => SnapshotDb | undefined) | undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function createLiveAuthorTarget(deps: SessionLiveAuthorTargetDeps) {
|
|
15
|
+
return createSessionLiveAuthorTarget(deps);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { createCallerSessionTools } from "./caller-tools";
|
|
19
|
+
export {
|
|
20
|
+
type ApplysetPortSession,
|
|
21
|
+
createLiveApplysetPort,
|
|
22
|
+
} from "./live-applyset-port.ts";
|
|
23
|
+
export type { LiveAuthorTargetDeps } from "./live-author-target.ts";
|
|
24
|
+
export { createLiveCanIPort, mapProcedureResult } from "./live-can-i-port";
|
|
25
|
+
export { createLiveExplorationPort } from "./live-exploration-port";
|
|
26
|
+
export { createLiveGrantsPort } from "./live-grants-port";
|
|
27
|
+
export { createLiveScenesPort } from "./live-scenes-port";
|
|
28
|
+
export { createLiveScopedSnapshotPort } from "./live-scoped-instance-port";
|