castle-web-sdk 0.4.44 → 0.4.46
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 +487 -53
- package/dist/agent.js +4 -4
- package/dist/avatar.d.ts +12 -0
- package/dist/avatar.js +23 -0
- package/dist/castle.d.ts +10 -1
- package/dist/castle.js +5 -0
- package/dist/commands.d.ts +229 -0
- package/dist/commands.js +16 -0
- package/dist/context.d.ts +6 -0
- package/dist/experiments.d.ts +15 -0
- package/dist/experiments.js +83 -0
- package/dist/hub.d.ts +85 -0
- package/dist/hub.js +107 -0
- package/dist/multiplayer.d.ts +88 -6
- package/dist/multiplayer.js +219 -8
- package/dist/save.d.ts +197 -0
- package/dist/save.js +525 -0
- package/dist/saveCoordinator.d.ts +39 -0
- package/dist/saveCoordinator.js +252 -0
- package/dist/server/hub.d.ts +169 -0
- package/dist/server/hub.js +125 -0
- package/dist/server/platformHandle.d.ts +51 -2
- package/dist/server/storage.d.ts +58 -4
- package/dist/server/storage.js +25 -5
- package/dist/server/wrapper.d.ts +147 -6
- package/dist/server/wrapper.js +741 -34
- package/dist/store.d.ts +3 -2
- package/dist/store.js +5 -0
- package/dist/transport.d.ts +2 -1
- package/dist/transport.js +13 -1
- package/package.json +1 -1
package/dist/store.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { StoreAllowance } from "./commands";
|
|
2
2
|
import type { Json } from "./types";
|
|
3
|
+
import { type SaveApi, type SaveBucketApi } from "./save";
|
|
3
4
|
export interface StoreListOptions {
|
|
4
5
|
/** Only keys beginning with this. Omit for every key in the scope. */
|
|
5
6
|
prefix?: string;
|
|
@@ -21,7 +22,7 @@ export interface StoreListPage<T extends Json = Json> {
|
|
|
21
22
|
*/
|
|
22
23
|
cursor: string | null;
|
|
23
24
|
}
|
|
24
|
-
export interface StoreScopeApi {
|
|
25
|
+
export interface StoreScopeApi extends SaveBucketApi {
|
|
25
26
|
/** Reads keys. Keys with no value are absent from the result. */
|
|
26
27
|
get<T extends Json = Json>(keys: string[]): Promise<Record<string, T>>;
|
|
27
28
|
/**
|
|
@@ -63,7 +64,7 @@ export interface StoreUsage {
|
|
|
63
64
|
/** `Store.deck`: one allowance shared by every player of the deck. */
|
|
64
65
|
shared: StoreAllowance;
|
|
65
66
|
}
|
|
66
|
-
export interface CastleStoreApi {
|
|
67
|
+
export interface CastleStoreApi extends SaveApi {
|
|
67
68
|
/** Shared by everyone playing the deck. */
|
|
68
69
|
readonly deck: StoreScopeApi;
|
|
69
70
|
/** The current player's data, which other players can read. */
|
package/dist/store.js
CHANGED
|
@@ -2,6 +2,7 @@ import { chunked } from "./chunk";
|
|
|
2
2
|
import { decodeStorageValue, encodeStorageValue, storageError } from "./storageJson";
|
|
3
3
|
import { hostRequest } from "./transport";
|
|
4
4
|
import { onUnloadFlush } from "./unloadFlush";
|
|
5
|
+
import { createSaveApi, createSaveBucket, } from "./save";
|
|
5
6
|
// Per-key deck storage for Cauldron decks.
|
|
6
7
|
//
|
|
7
8
|
// The older Storage/SharedStorage keep one document per bucket and rewrite the
|
|
@@ -212,6 +213,7 @@ function assertKeys(keys, operation) {
|
|
|
212
213
|
}
|
|
213
214
|
function createScope(scope, subject) {
|
|
214
215
|
return {
|
|
216
|
+
...createSaveBucket(saveApi, scope, subject ?? undefined),
|
|
215
217
|
async get(keys) {
|
|
216
218
|
const { entries } = await hostRequest("cauldronStorage.get", {
|
|
217
219
|
scope,
|
|
@@ -309,7 +311,10 @@ async function usage() {
|
|
|
309
311
|
const { usage: allowances } = await hostRequest("cauldronStorage.usage", {});
|
|
310
312
|
return { player: allowances.player, shared: allowances.shared };
|
|
311
313
|
}
|
|
314
|
+
// Saves skip the write coalescing above: each is one request, sent at once.
|
|
315
|
+
const saveApi = createSaveApi((verb, input) => hostRequest(`cauldronStorage.${verb}`, input));
|
|
312
316
|
export const Store = {
|
|
317
|
+
...saveApi,
|
|
313
318
|
deck: createScope("deck", null),
|
|
314
319
|
me: createScope("user", null),
|
|
315
320
|
user(userId) {
|
package/dist/transport.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type CommandName, type CommandParams, type CommandResult, type LifecycleEvent, type VoiceEvent } from "./commands";
|
|
1
|
+
import { type CommandName, type CommandParams, type CommandResult, type HubEvent, type LifecycleEvent, type VoiceEvent } from "./commands";
|
|
2
2
|
type PostChannel = "mobile" | "web";
|
|
3
3
|
export interface HostStreamOptions {
|
|
4
4
|
onStream?: (event: unknown) => void;
|
|
@@ -21,5 +21,6 @@ export declare function hostNotify(event: LifecycleEvent): void;
|
|
|
21
21
|
export declare function postUnloadDone(unloadId: string, state?: string): void;
|
|
22
22
|
export declare function setUnloadHandler(handler: (unloadId: string) => void): void;
|
|
23
23
|
export declare function setVoiceEventHandler(handler: ((event: VoiceEvent) => void) | null): void;
|
|
24
|
+
export declare function setHubEventHandler(handler: ((event: HubEvent) => void) | null): void;
|
|
24
25
|
export declare function installHostListener(): void;
|
|
25
26
|
export {};
|
package/dist/transport.js
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
// A streaming command (agent.chat) gets stream envelopes on its requestId before
|
|
12
12
|
// its response. Each one goes to the caller's onStream and restarts the wait,
|
|
13
13
|
// and the caller's AbortSignal sends the host a cancel envelope.
|
|
14
|
-
import { CASTLE_SDK_PROTOCOL, COMMAND_TIMEOUTS_MS, STREAM_IDLE_TIMEOUT_MS, isResponseEnvelope, isStreamEnvelope, unloadRequestId, voiceEventFrom, } from "./commands";
|
|
14
|
+
import { CASTLE_SDK_PROTOCOL, COMMAND_TIMEOUTS_MS, STREAM_IDLE_TIMEOUT_MS, isResponseEnvelope, isStreamEnvelope, hubEventFrom, unloadRequestId, voiceEventFrom, } from "./commands";
|
|
15
15
|
import { getCastleEmbed } from "./context";
|
|
16
16
|
import { CastleError } from "./errors";
|
|
17
17
|
import { cancelLocalCommand, releaseLocalCommand, sendLocalCommand } from "./runtime";
|
|
@@ -35,6 +35,7 @@ const pending = new Map();
|
|
|
35
35
|
let listenersInstalled = false;
|
|
36
36
|
let unloadHandler = null;
|
|
37
37
|
let voiceEventHandler = null;
|
|
38
|
+
let hubEventHandler = null;
|
|
38
39
|
export async function hostRequest(command, params, stream = {}) {
|
|
39
40
|
try {
|
|
40
41
|
const env = await sendCommand(resolveChannel(), command, params, stream);
|
|
@@ -103,6 +104,12 @@ export function setVoiceEventHandler(handler) {
|
|
|
103
104
|
if (handler)
|
|
104
105
|
installHostListener();
|
|
105
106
|
}
|
|
107
|
+
// Registered by hub.ts while the deck listens for the host's Back control.
|
|
108
|
+
export function setHubEventHandler(handler) {
|
|
109
|
+
hubEventHandler = handler;
|
|
110
|
+
if (handler)
|
|
111
|
+
installHostListener();
|
|
112
|
+
}
|
|
106
113
|
function resolveChannel() {
|
|
107
114
|
if (typeof window === "undefined")
|
|
108
115
|
return "local";
|
|
@@ -233,6 +240,11 @@ function receive(message) {
|
|
|
233
240
|
voiceEventHandler?.(voiceEvent);
|
|
234
241
|
return;
|
|
235
242
|
}
|
|
243
|
+
const hubEvent = hubEventFrom(message);
|
|
244
|
+
if (hubEvent) {
|
|
245
|
+
hubEventHandler?.(hubEvent);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
236
248
|
const unloadId = unloadRequestId(message);
|
|
237
249
|
if (unloadId !== null)
|
|
238
250
|
unloadHandler?.(unloadId);
|