castle-web-sdk 0.4.45 → 0.4.47

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/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) {
@@ -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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "castle-web-sdk",
3
- "version": "0.4.45",
3
+ "version": "0.4.47",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/castle-xyz/castle-experimental-web.git"