@uzuhq/code-sdk 0.7.6 → 0.8.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.
Files changed (53) hide show
  1. package/README.md +52 -9
  2. package/dist/dev-globals.d.ts +12 -27
  3. package/dist/dev-globals.js +0 -15
  4. package/dist/dev-hooks-D6CbhPDP.d.ts +715 -0
  5. package/dist/index.d.ts +158 -66
  6. package/dist/index.js +2178 -416
  7. package/package.json +8 -5
  8. package/dist/action-types.test-d.d.ts +0 -11
  9. package/dist/action-types.test-d.js +0 -101
  10. package/dist/dev-hooks.d.ts +0 -241
  11. package/dist/dev-hooks.js +0 -132
  12. package/dist/dev-hooks.test.d.ts +0 -1
  13. package/dist/dev-hooks.test.js +0 -294
  14. package/dist/dev-prediction-traps.d.ts +0 -32
  15. package/dist/dev-prediction-traps.js +0 -0
  16. package/dist/dev-prediction-traps.test.d.ts +0 -1
  17. package/dist/dev-prediction-traps.test.js +0 -178
  18. package/dist/dev-state-patch.d.ts +0 -81
  19. package/dist/dev-state-patch.js +0 -295
  20. package/dist/dev-state-patch.test.d.ts +0 -1
  21. package/dist/dev-state-patch.test.js +0 -333
  22. package/dist/json-patch.d.ts +0 -7
  23. package/dist/json-patch.js +0 -78
  24. package/dist/random.d.ts +0 -11
  25. package/dist/random.js +0 -34
  26. package/dist/reconnectable-ws.d.ts +0 -60
  27. package/dist/reconnectable-ws.js +0 -229
  28. package/dist/room.d.ts +0 -23
  29. package/dist/room.js +0 -36
  30. package/dist/roster-params.test.d.ts +0 -1
  31. package/dist/roster-params.test.js +0 -86
  32. package/dist/run/local-server-action.d.ts +0 -16
  33. package/dist/run/local-server-action.js +0 -217
  34. package/dist/run/local-server-action.test.d.ts +0 -1
  35. package/dist/run/local-server-action.test.js +0 -242
  36. package/dist/run/optimistic-action-client.d.ts +0 -68
  37. package/dist/run/optimistic-action-client.js +0 -209
  38. package/dist/run/optimistic-action-client.test.d.ts +0 -1
  39. package/dist/run/optimistic-action-client.test.js +0 -430
  40. package/dist/run/server-action.d.ts +0 -16
  41. package/dist/run/server-action.js +0 -181
  42. package/dist/run/server-action.test.d.ts +0 -1
  43. package/dist/run/server-action.test.js +0 -105
  44. package/dist/server-clock.d.ts +0 -29
  45. package/dist/server-clock.js +0 -40
  46. package/dist/server-only.d.ts +0 -33
  47. package/dist/server-only.js +0 -21
  48. package/dist/sync/local.d.ts +0 -8
  49. package/dist/sync/local.js +0 -50
  50. package/dist/sync/online.d.ts +0 -5
  51. package/dist/sync/online.js +0 -165
  52. package/dist/types.d.ts +0 -345
  53. package/dist/types.js +0 -8
package/dist/index.d.ts CHANGED
@@ -1,72 +1,162 @@
1
+ import { $ as minus, A as DEFAULT_ICON_URLS, B as ServerActionContext, C as SetFn, D as ActionContext, E as ActionArgs, F as GameLogic, G as ServerOnlyActionContext, H as ServerActionMap, I as SERVER_TIME, J as SetupContext, K as ServerOnlyActionHandlerFn, L as Seat, M as DeadlineArgs, N as DeadlineContext, O as ActionHandler, P as Emit, Q as GameTime, R as SeededRandom, S as SendAction, T as Operation, U as ServerEvent, V as ServerActionHandler, W as ServerOnlyAction, X as UpdateContext, Y as UpdateArgs, Z as Duration, _ as PayloadMap, a as attachDevHooks, b as PlayersChangedMessage, c as getPredictionWarnings, d as ConnectionCallbacks, et as plus, f as ConnectionState, g as PatchFn, h as GameConfig, i as UzuDevHooks, it as applyJsonPatch, j as Deadline, k as ActionMap, l as BridgeChannel, m as EventSubscription, n as RunHandle, nt as JsonPatchOp, o as createDevHooks, p as EventHandler, q as SetupArgs, r as SyncHandle, rt as applyJsonMergePatch, s as PredictionWarning, t as DevHooksCtx, tt as JsonMergePatch, u as BridgeMessage, v as PlayScreenMessage, w as SyncConfig, x as SeatKind, y as PlayerVoiceState, z as ServerActionArgs } from "./dev-hooks-D6CbhPDP.js";
2
+ //#region ../engine-core/src/random.d.ts
3
+ declare class SeededRandomImpl implements SeededRandom {
4
+ private _state;
5
+ constructor(seed: number);
6
+ get state(): number;
7
+ static fromState(state: number): SeededRandomImpl;
8
+ float(): number;
9
+ int(max: number): number;
10
+ pick<T>(array: T[]): T;
11
+ shuffle<T>(array: T[]): T[];
12
+ }
13
+ //#endregion
14
+ //#region ../engine-core/src/server-only.d.ts
15
+ /**
16
+ * @deprecated `logic.serverActions` に直接書く。
17
+ *
18
+ * ```ts
19
+ * // before
20
+ * actions: { notifyExternal: serverOnly(async (state) => { ... }) }
21
+ * // after
22
+ * serverActions: { notifyExternal: async (state) => { ... } }
23
+ * ```
24
+ */
25
+ declare function serverOnly<S>(handler: ServerActionHandler<S>): ServerOnlyAction<S>;
26
+ /**
27
+ * handler が `serverOnly()` で wrap されているか判定する。
28
+ *
29
+ * 移行期の互換用。`serverActions` へ移行済みの logic では常に false になる。
30
+ */
31
+ declare function isServerOnlyAction<S>(handler: ActionHandler<S> | ServerActionHandler<S> | ServerOnlyAction<S> | undefined): handler is ServerOnlyAction<S>;
32
+ //#endregion
33
+ //#region src/server-clock.d.ts
1
34
  /**
2
- * @docs
3
- * - SDK仕様: docs/docs/uzu_code/play-screen-sdk.md
4
- * - ブリッジ仕様: docs/docs/uzu_code/bridge.md
5
- * - Relay仕様: docs/docs/uzu_code/relay.md
6
- * - SyncRoom仕様: docs/docs/uzu_code/sync-room.md
7
- * - ゲーム仕様: docs/docs/uzu_code/games.md
8
- * - システム全体像: docs/docs/uzu_code/overview.md
9
- * - AI用APIリファレンス (README): javascript/uzu_code/sdk/README.md
35
+ * ゲーム内時刻の推定値。停止中は凍る。
10
36
  *
11
- * SDK は runtime のみ。 dev harness の DOM 描画 (iframe grid / HUD / state
12
- * inspector) と in-memory サーバー (Node ws による GameRoom / SyncRoom / RelayRoom) は
13
- * `@uzuhq/code-cli` の `uzu dev` サブコマンドに統合されている。 parent frame で
14
- * `init()` / `run()` / `sync()` を呼んでも何もしない (= noop)。 harness を立てるには
15
- * scenario ディレクトリで `uzu dev` を実行するだけで良い。 子 iframe は既存 online mode
16
- * (`?server=ws://localhost:<port>` 経路) で dev-server に接続する。
37
+ * カウントダウン描画のように毎秒/毎フレーム呼ぶ用途を想定しているので、
38
+ * state の到着とは無関係にいつでも呼べる。
17
39
  */
18
- export type { PlayScreenMessage, BridgeChannel, BridgeMessage, Seat, SeatKind, Emit, ServerEvent, SeededRandom, GameLogic, GameConfig, SyncConfig, PatchFn, SetFn, Operation, ConnectionState, ConnectionCallbacks, PlayerVoiceState, PlayersChangedMessage, ActionArgs, ActionContext, ActionHandler, ServerActionArgs, SetupArgs, SetupContext, UpdateArgs, UpdateContext, EventHandler, EventSubscription, Deadline, DeadlineArgs, DeadlineContext, ServerActionContext, ServerActionHandler, ServerOnlyAction, ServerOnlyActionContext, ServerOnlyActionHandlerFn, ActionMap, ServerActionMap, PayloadMap, SendAction, } from './types.js';
19
- export { SERVER_TIME, DEFAULT_ICON_URLS } from './types.js';
20
- export { serverOnly, isServerOnlyAction } from './server-only.js';
21
- export { serverNow } from './server-clock.js';
22
- export { Room } from './room.js';
23
- export type { RoomLike } from './room.js';
24
- export { ReconnectableWebSocket } from './reconnectable-ws.js';
25
- export type { ReconnectableWSOptions } from './reconnectable-ws.js';
26
- export { SeededRandomImpl } from './random.js';
27
- export { applyJsonMergePatch, applyJsonPatch } from './dev-state-patch.js';
28
- export type { JsonMergePatch, JsonPatchOp } from './dev-state-patch.js';
29
- export { attachDevHooks, createDevHooks } from './dev-hooks.js';
30
- export type { UzuDevHooks, DevHooksCtx, RunHandle, SyncHandle } from './dev-hooks.js';
31
- export { getPredictionWarnings } from './dev-prediction-traps.js';
32
- export type { PredictionWarning } from './dev-prediction-traps.js';
33
- import type { ActionMap, ServerActionMap, BridgeMessage, GameConfig, SyncConfig, PlayerVoiceState } from './types.js';
34
- import type { RoomLike } from './room.js';
40
+ declare function gameTime(): GameTime;
41
+ //#endregion
42
+ //#region src/pause-state.d.ts
43
+ /** 緊急停止中か。 */
44
+ declare function isPaused(): boolean;
45
+ /** 停止状態の変化を購読する。 */
46
+ declare function onPauseChange(cb: (paused: boolean) => void): void;
47
+ //#endregion
48
+ //#region src/room.d.ts
49
+ type RoomMessageHandler = (data: Record<string, unknown>) => void;
50
+ /** WebSocket 互換の send/addEventListener インターフェース */
51
+ interface WebSocketLike {
52
+ send(data: string): void;
53
+ addEventListener(type: 'message', handler: (ev: MessageEvent) => void): void;
54
+ }
55
+ /** Room の公開インターフェース。WebSocket / BroadcastChannel どちらでも実装可能。 */
56
+ interface RoomLike {
57
+ readonly myId: string;
58
+ broadcast(msg: Record<string, unknown>): void;
59
+ send(id: string, msg: Record<string, unknown>): void;
60
+ on(type: string, handler: RoomMessageHandler): void;
61
+ }
62
+ declare class Room implements RoomLike {
63
+ readonly myId: string;
64
+ private ws;
65
+ private handlers;
66
+ constructor(ws: WebSocketLike, myId: string);
67
+ broadcast(msg: Record<string, unknown>): void;
68
+ send(id: string, msg: Record<string, unknown>): void;
69
+ on(type: string, handler: RoomMessageHandler): void;
70
+ }
71
+ //#endregion
72
+ //#region src/reconnectable-ws.d.ts
73
+ /**
74
+ * 自動再接続 WebSocket ラッパー
75
+ *
76
+ * - Exponential backoff + jitter 付き自動再接続
77
+ * - Heartbeat (__ping / __pong) による接続生存確認
78
+ * - 再接続中のメッセージバッファリング
79
+ */
80
+ type ConnectionState$1 = 'connecting' | 'connected' | 'reconnecting' | 'disconnected';
81
+ interface ReconnectableWSOptions {
82
+ /** 最大再接続試行回数 (default: 15) */
83
+ maxReconnectAttempts?: number;
84
+ /** Backoff の基準遅延 ms (default: 1000) */
85
+ baseDelay?: number;
86
+ /** Backoff の最大遅延 ms (default: 30000) */
87
+ maxDelay?: number;
88
+ /** Jitter 係数 0〜1 (default: 0.5) */
89
+ jitterFactor?: number;
90
+ /** Heartbeat 送信間隔 ms (default: 25000) */
91
+ heartbeatInterval?: number;
92
+ /** Pong 待ちタイムアウト ms (default: 10000) */
93
+ heartbeatTimeout?: number;
94
+ /** 再接続中のバッファ上限 (default: 50) */
95
+ maxBufferSize?: number;
96
+ /** バッファ対象判定。false を返すとドロップ */
97
+ shouldBuffer?: (data: string) => boolean;
98
+ /** 接続状態変更コールバック */
99
+ onConnectionStateChange?: (state: ConnectionState$1) => void;
100
+ }
101
+ declare class ReconnectableWebSocket {
102
+ private ws;
103
+ private readonly url;
104
+ private readonly opts;
105
+ private _state;
106
+ private reconnectAttempt;
107
+ private messageBuffer;
108
+ private messageHandlers;
109
+ private heartbeatTimer;
110
+ private heartbeatTimeoutTimer;
111
+ private reconnectTimer;
112
+ private disposed;
113
+ constructor(url: string, options?: ReconnectableWSOptions);
114
+ get connectionState(): ConnectionState$1;
115
+ /** WebSocket.readyState 互換 (既存コードとの互換用) */
116
+ get readyState(): number;
117
+ send(data: string): void;
118
+ addEventListener(type: 'message', handler: (ev: MessageEvent) => void): void;
119
+ removeEventListener(type: 'message', handler: (ev: MessageEvent) => void): void;
120
+ /** バッファをクリア(権威的 state 受信時に呼ぶ) */
121
+ clearBuffer(): void;
122
+ /** 全リソース解放 */
123
+ dispose(): void;
124
+ private connect;
125
+ private setState;
126
+ private scheduleReconnect;
127
+ private getReconnectDelay;
128
+ private flushBuffer;
129
+ private startHeartbeat;
130
+ private stopHeartbeat;
131
+ private handlePong;
132
+ }
133
+ //#endregion
134
+ //#region src/index.d.ts
35
135
  type GameMessageHandler = (payload: Record<string, unknown>) => void;
36
136
  type PlayersChangedHandler = (players: Record<string, PlayerVoiceState>) => void;
37
- declare global {
38
- interface Window {
39
- __ps?: {
40
- onMessage: (msg: BridgeMessage) => void;
41
- };
42
- FlutterHost?: {
43
- postMessage: (message: string) => void;
44
- };
45
- }
46
- }
47
- export declare const isHosted: boolean;
48
- export declare function getRoom(): RoomLike | null;
49
- export declare function onRoom(callback: (room: RoomLike) => void): void;
50
- export declare function init(opts?: {
51
- wsEndpoint?: string;
52
- syncEndpoint?: string;
53
- playerCount?: number;
54
- orientation?: 'portrait' | 'landscape';
55
- /**
56
- * Dev harness の iframe inner viewport 短辺 (CSS px) の下限。default 360。
57
- * harness 側で参照される値で、 SDK runtime 本体は使わない。
58
- */
59
- devMinIframeShortEdge?: number;
137
+ declare const isHosted: boolean;
138
+ declare function getRoom(): RoomLike | null;
139
+ declare function onRoom(callback: (room: RoomLike) => void): void;
140
+ declare function init(opts?: {
141
+ wsEndpoint?: string;
142
+ syncEndpoint?: string;
143
+ playerCount?: number;
144
+ orientation?: 'portrait' | 'landscape';
145
+ /**
146
+ * Dev harness の iframe inner viewport 短辺 (CSS px) の下限。default 360。
147
+ * harness 側で参照される値で、 SDK runtime 本体は使わない。
148
+ */
149
+ devMinIframeShortEdge?: number;
60
150
  }): void;
61
151
  /** game channel でカスタムメッセージを送信する。 native コマンドには使用不可。 */
62
- export declare function send(type: string, payload?: Record<string, unknown>): void;
152
+ declare function send(type: string, payload?: Record<string, unknown>): void;
63
153
  /** game channel のカスタムメッセージを受信する。 */
64
- export declare function on(type: string, handler: GameMessageHandler): void;
65
- export declare function playSound(sound: string): void;
66
- export declare function playBgm(sound: string): void;
67
- export declare function stopBgm(): void;
68
- export declare function setMicEnabled(enabled: boolean): void;
69
- export declare function changeRoom(roomId: string | null): void;
154
+ declare function on(type: string, handler: GameMessageHandler): void;
155
+ declare function playSound(sound: string): void;
156
+ declare function playBgm(sound: string): void;
157
+ declare function stopBgm(): void;
158
+ declare function setMicEnabled(enabled: boolean): void;
159
+ declare function changeRoom(roomId: string | null): void;
70
160
  /**
71
161
  * splash 描画完了を Flutter ホストに通知する。
72
162
  *
@@ -83,7 +173,7 @@ export declare function changeRoom(roomId: string | null): void;
83
173
  * requestAnimationFrame(() => firstFrameReady());
84
174
  * ```
85
175
  */
86
- export declare function firstFrameReady(): void;
176
+ declare function firstFrameReady(): void;
87
177
  /**
88
178
  * ユーザー操作受付可能を Flutter ホストに通知する。
89
179
  *
@@ -100,9 +190,9 @@ export declare function firstFrameReady(): void;
100
190
  * gameReady();
101
191
  * ```
102
192
  */
103
- export declare function gameReady(): void;
193
+ declare function gameReady(): void;
104
194
  /** Flutter からの playersChanged メッセージを受信するハンドラを登録する。 */
105
- export declare function onPlayersChanged(handler: PlayersChangedHandler): void;
195
+ declare function onPlayersChanged(handler: PlayersChangedHandler): void;
106
196
  /**
107
197
  * `A` に既定値が要る。 TypeScript は型引数の部分推論ができないので、 既定が無いと
108
198
  * 既存の `run<State>({…})` が「2 つ必要」で落ちる。 既定を置けば型引数を書かない
@@ -112,5 +202,7 @@ export declare function onPlayersChanged(handler: PlayersChangedHandler): void;
112
202
  * `keyof (A & SA)` が `string` へ潰れ、action 名の検査が丸ごと消える。
113
203
  * `serverActions` を持たない logic でもキーが残るよう空 map を既定にする。
114
204
  */
115
- export declare function run<S, A extends ActionMap<S> = ActionMap<S>, SA extends ServerActionMap<S> = Record<never, never>>(config: GameConfig<S, A, SA>): void;
116
- export declare function sync<S>(config: SyncConfig<S>): void;
205
+ declare function run<S, A extends ActionMap<S> = ActionMap<S>, SA extends ServerActionMap<S> = Record<never, never>>(config: GameConfig<S, A, SA>): void;
206
+ declare function sync<S>(config: SyncConfig<S>): void;
207
+ //#endregion
208
+ export { type ActionArgs, type ActionContext, type ActionHandler, type ActionMap, type BridgeChannel, type BridgeMessage, type ConnectionCallbacks, type ConnectionState, DEFAULT_ICON_URLS, type Deadline, type DeadlineArgs, type DeadlineContext, type DevHooksCtx, type Duration, type Emit, type EventHandler, type EventSubscription, type GameConfig, type GameLogic, type GameTime, type JsonMergePatch, type JsonPatchOp, type Operation, type PatchFn, type PayloadMap, type PlayScreenMessage, type PlayerVoiceState, type PlayersChangedMessage, type PredictionWarning, type ReconnectableWSOptions, ReconnectableWebSocket, Room, type RoomLike, type RunHandle, SERVER_TIME, type Seat, type SeatKind, type SeededRandom, SeededRandomImpl, type SendAction, type ServerActionArgs, type ServerActionContext, type ServerActionHandler, type ServerActionMap, type ServerEvent, type ServerOnlyAction, type ServerOnlyActionContext, type ServerOnlyActionHandlerFn, type SetFn, type SetupArgs, type SetupContext, type SyncConfig, type SyncHandle, type UpdateArgs, type UpdateContext, type UzuDevHooks, applyJsonMergePatch, applyJsonPatch, attachDevHooks, changeRoom, createDevHooks, firstFrameReady, gameReady, gameTime, getPredictionWarnings, getRoom, init, isHosted, isPaused, isServerOnlyAction, minus, on, onPauseChange, onPlayersChanged, onRoom, playBgm, playSound, plus, run, send, serverOnly, setMicEnabled, stopBgm, sync };