@myclaw163/clawclaw-cli 0.9.2 → 0.10.2
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/QUICKSTART.md +1 -1
- package/QUICKSTART_OTHER.md +13 -12
- package/README.md +7 -8
- package/bin/clawclaw-channel.mjs +2 -2
- package/bin/clawclaw-cli.mjs +2 -2
- package/docs/configuration.md +27 -101
- package/docs/custom-modules/README.md +23 -35
- package/docs/custom-modules/api-reference.md +95 -273
- package/docs/custom-modules/behavior.md +94 -78
- package/docs/custom-modules/examples/auto-escape.ts +5 -5
- package/docs/custom-modules/examples/go-to-point.ts +11 -11
- package/docs/custom-modules/examples/nearby-reflex.ts +2 -2
- package/docs/custom-modules/examples/task-reminder.ts +4 -5
- package/docs/custom-modules/perception.md +42 -603
- package/docs/custom-modules/skill.md +12 -10
- package/docs/desktop-event-push.md +3 -3
- package/docs/runtime/README.md +16 -17
- package/docs/runtime/agent-notifications.md +42 -12
- package/docs/runtime/behavior-runtime.md +47 -46
- package/docs/runtime/components-and-boundaries.md +70 -28
- package/docs/runtime/events-and-state.md +41 -18
- package/docs/runtime/framework-package.md +73 -0
- package/docs/runtime/official-perception-modules.md +5 -5
- package/docs/runtime/owner-lifecycle-and-observability.md +6 -6
- package/docs/runtime/short-commands-and-capabilities.md +8 -9
- package/docs/user-guide/README.md +10 -7
- package/docs/user-guide/api-reference.md +81 -164
- package/docs/user-guide/behavior.md +84 -68
- package/docs/user-guide/installation.md +7 -7
- package/docs/user-guide/perception.md +42 -586
- package/docs/user-guide/skill.md +12 -10
- package/package.json +24 -9
- package/scripts/check-framework-boundaries.mjs +27 -0
- package/scripts/check-skill-command-surface.mjs +20 -23
- package/scripts/enable_ccl_permissions.cjs +4 -0
- package/scripts/install-local-runtime.mjs +35 -0
- package/scripts/postinstall.mjs +1 -1
- package/scripts/qa-install.mjs +105 -0
- package/scripts/sync-bundled-skill.mjs +35 -28
- package/skills/clawclaw/SKILL.md +53 -57
- package/skills/clawclaw/references/BEHAVIORS.md +43 -83
- package/skills/clawclaw/references/CHANNELS.md +2 -2
- package/skills/clawclaw/references/COMMANDS.md +62 -103
- package/skills/clawclaw/references/HUB.md +2 -2
- package/skills/clawclaw/references/STREAM.md +8 -8
- package/skills/clawclaw/references/TACTICS.md +1 -1
- package/skills/clawclaw-creator/SKILL.md +82 -0
- package/skills/{clawclaw → clawclaw-creator}/references/CUSTOM-BEHAVIOR-DEVELOPMENT.md +95 -79
- package/skills/{clawclaw → clawclaw-creator}/references/CUSTOM-MODULE-API.md +95 -180
- package/skills/{clawclaw → clawclaw-creator}/references/CUSTOM-MODULES.md +26 -24
- package/skills/clawclaw-creator/references/CUSTOM-PERCEPTION-DEVELOPMENT.md +126 -0
- package/skills/clawclaw-creator/references/CUSTOM-PERSONA-DEVELOPMENT.md +29 -0
- package/skills/clawclaw-creator/references/CUSTOM-SKILL-DEVELOPMENT.md +69 -0
- package/skills/clawclaw-creator/references/SYNC-AND-REPORT.md +36 -0
- package/src/channel/claude-channel-ipc-server.ts +1 -0
- package/src/cli.ts +4 -2
- package/src/commands/behavior.ts +26 -10
- package/src/commands/capability.ts +17 -14
- package/src/commands/load.ts +5 -3
- package/src/commands/perception.ts +5 -0
- package/src/commands/skill.ts +34 -29
- package/src/commands/strategy.ts +2 -0
- package/src/commands/upgrade.ts +2 -2
- package/src/lib/agent-workspace-skills.ts +20 -13
- package/src/lib/capability-cloud-sync.ts +2 -1
- package/src/lib/capability-discovery.ts +2 -1
- package/src/lib/capability-load-diagnostics.ts +3 -3
- package/src/lib/capability-workspace.ts +2 -1
- package/src/lib/game-client.ts +7 -1
- package/src/lib/package-skills.ts +4 -0
- package/src/lib/perception-config.ts +18 -6
- package/src/lib/runtime-cadence-config.ts +16 -0
- package/src/lib/skill-host.ts +1 -1
- package/src/lib/workspace-runtime-lock.ts +3 -46
- package/src/pipeline/canonical-event-presentation.ts +23 -124
- package/src/pipeline/event-format.ts +12 -21
- package/src/pipeline/event-hints.ts +6 -6
- package/src/pipeline/game-sync-event-normalization.ts +57 -63
- package/src/runtime/clawclaw/behavior-decision.ts +7 -3
- package/src/runtime/clawclaw/behavior-input.ts +8 -4
- package/src/runtime/clawclaw/behavior-observation-memory.ts +3 -2
- package/src/runtime/clawclaw/notification-policy.ts +42 -0
- package/src/runtime/clawclaw/notification-turn.ts +19 -0
- package/src/runtime/clawclaw/observation-frame-projector.ts +3 -3
- package/src/runtime/clawclaw/owner.ts +47 -9
- package/src/runtime/clawclaw/perception-module-manifest.ts +8 -3
- package/src/runtime/clawclaw/perception-strategy-names.ts +17 -0
- package/src/runtime/clawclaw/persistence-compatibility.ts +36 -0
- package/src/runtime/clawclaw/plugins/action/action-plugin.ts +3 -3
- package/src/runtime/clawclaw/plugins/assembly.ts +7 -9
- package/src/runtime/clawclaw/plugins/behavior/activation-manager.ts +1 -495
- package/src/runtime/clawclaw/plugins/behavior/behavior-analysis-thread-host.ts +17 -190
- package/src/runtime/clawclaw/plugins/behavior/behavior-instance-snapshot.ts +1 -123
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +126 -1779
- package/src/runtime/clawclaw/plugins/behavior/behavior-runner.ts +13 -276
- package/src/runtime/clawclaw/plugins/behavior/behavior-source.ts +2 -2
- package/src/runtime/clawclaw/plugins/behavior/behavior-worker-bridge.ts +8 -0
- package/src/runtime/clawclaw/plugins/behavior/control-driver.ts +3 -180
- package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.ts +13 -972
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.ts +8 -109
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-rules.ts +2 -2
- package/src/runtime/clawclaw/plugins/derived-event/reminder-plugin.ts +27 -17
- package/src/runtime/clawclaw/plugins/derived-event/reminder-runner.ts +1 -0
- package/src/runtime/clawclaw/plugins/derived-event/state-change-rules.ts +3 -3
- package/src/runtime/clawclaw/plugins/derived-event/task-change-tracker.ts +1 -259
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.ts +4 -4
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.ts +9 -97
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-socket.ts +8 -343
- package/src/runtime/clawclaw/plugins/mailbox/adapters/game-sync-perception-report-adapter.ts +15 -20
- package/src/runtime/clawclaw/plugins/mailbox/adapters/game-sync-runtime-report-adapter.ts +59 -0
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.ts +60 -572
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.ts +6 -434
- package/src/runtime/clawclaw/plugins/mailbox/index.ts +1 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.ts +1 -266
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.ts +6 -161
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-presentation.ts +4 -23
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +49 -752
- package/src/runtime/clawclaw/plugins/perception/map-concurrent.ts +1 -0
- package/src/runtime/clawclaw/plugins/perception/notification-content.ts +60 -0
- package/src/runtime/clawclaw/plugins/perception/perception-module-plugin.ts +125 -297
- package/src/runtime/clawclaw/plugins/perception/perception-module-runner.ts +7 -56
- package/src/runtime/clawclaw/plugins/perception/perception-module-worker.ts +11 -33
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +25 -10
- package/src/runtime/clawclaw/plugins/perception/perception-source.ts +12 -6
- package/src/runtime/clawclaw/plugins/perception/report-content.ts +50 -0
- package/src/runtime/clawclaw/plugins/perception/task-baseline.ts +29 -0
- package/src/runtime/clawclaw/plugins/source/source-plugin.ts +6 -0
- package/src/runtime/clawclaw/plugins/system-perception/rule-runner.ts +107 -0
- package/src/runtime/clawclaw/plugins/system-perception/system-perceptions.ts +107 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.ts +8 -3
- package/src/runtime/clawclaw/reaction-behavior-resolution.ts +1 -23
- package/src/runtime/clawclaw/reminder-manifest.ts +5 -2
- package/src/runtime/clawclaw/runtime-cadence.ts +24 -0
- package/src/runtime/clawclaw/runtime-state.ts +11 -0
- package/src/runtime/clawclaw/runtime.ts +8 -0
- package/src/runtime/clawclaw/strategy-memory-rpc.ts +1 -63
- package/src/runtime/clawclaw/strategy-memory.ts +1 -77
- package/src/runtime/clawclaw-event-catalog.ts +12 -12
- package/src/runtime/framework/action-diagnostics.ts +3 -83
- package/src/runtime/framework/capability-registry.ts +2 -395
- package/src/runtime/framework/contracts.ts +2 -144
- package/src/runtime/framework/control/control-scheduler.ts +2 -1268
- package/src/runtime/framework/control/domain-set.ts +2 -81
- package/src/runtime/framework/control/index.ts +1 -3
- package/src/runtime/framework/control/types.ts +2 -191
- package/src/runtime/framework/control-coordinator.ts +2 -11
- package/src/runtime/framework/events/event-catalog.ts +2 -74
- package/src/runtime/framework/events/event-delivery-service.ts +2 -200
- package/src/runtime/framework/events/event-ingress.ts +2 -99
- package/src/runtime/framework/events/event-repository.ts +2 -82
- package/src/runtime/framework/events/index.ts +2 -7
- package/src/runtime/framework/events/memory-event-repository.ts +2 -1100
- package/src/runtime/framework/events/runtime-resume.ts +2 -20
- package/src/runtime/framework/events/sqlite-event-repository.ts +5 -2280
- package/src/runtime/framework/events/test-helpers.ts +1 -1
- package/src/runtime/framework/events/types.ts +2 -312
- package/src/runtime/framework/index.ts +3 -7
- package/src/runtime/framework/load-node-sqlite.ts +2 -17
- package/src/runtime/framework/mailbox/coalescer.ts +2 -83
- package/src/runtime/framework/mailbox/host-notification-sink.ts +2 -44
- package/src/runtime/framework/mailbox/identity.ts +2 -38
- package/src/runtime/framework/mailbox/index.ts +1 -7
- package/src/runtime/framework/mailbox/mailbox-dispatcher.ts +2 -282
- package/src/runtime/framework/mailbox/mailbox-repository.ts +2 -44
- package/src/runtime/framework/mailbox/notification-projector.ts +2 -65
- package/src/runtime/framework/mailbox/types.ts +2 -145
- package/src/runtime/framework/modules/runtime-module-loader-hooks.mjs +2 -154
- package/src/runtime/framework/modules/runtime-module-loader.ts +7 -51
- package/src/runtime/framework/modules/runtime-module-output.ts +2 -24
- package/src/runtime/framework/modules/runtime-module-worker-bootstrap.mjs +3 -23
- package/src/runtime/framework/participation-manager.ts +2 -253
- package/src/runtime/framework/persistence/async-sqlite-persistence-worker.ts +2 -77
- package/src/runtime/framework/persistence/memory-first-persistence.ts +4 -489
- package/src/runtime/framework/plugins/contracts.ts +2 -396
- package/src/runtime/framework/plugins/index.ts +1 -4
- package/src/runtime/framework/plugins/runtime-event-identity.ts +2 -17
- package/src/runtime/framework/plugins/runtime-input-journal.ts +2 -866
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +2 -2071
- package/src/runtime/framework/runtime.ts +5 -1843
- package/src/runtime/framework/serial-task-queue.ts +2 -14
- package/src/runtime/framework/state/contracts.ts +2 -49
- package/src/runtime/framework/state/current-state-store.ts +2 -184
- package/src/runtime/framework/state/durable-agent-state-snapshot.ts +2 -209
- package/src/runtime/framework/state/freshness.ts +2 -72
- package/src/runtime/framework/state/history/contracts.ts +2 -158
- package/src/runtime/framework/state/history/history-writer.ts +2 -194
- package/src/runtime/framework/state/history/memory-state-history-repository.ts +2 -230
- package/src/runtime/framework/state/history/sqlite-state-history-repository.ts +2 -648
- package/src/runtime/framework/state/history/trajectory-projector.ts +2 -209
- package/src/runtime/framework/state/index.ts +1 -13
- package/src/runtime/framework/state/state-ingress.ts +2 -95
- package/src/runtime/framework/state/state-provider.ts +2 -62
- package/src/runtime/framework/state/types.ts +2 -155
- package/src/runtime/game-sync-ingress-protocol.ts +3 -2
- package/src/runtime/game-sync-ingress-worker.ts +7 -1
- package/src/runtime/game-sync-protocol.ts +64 -4
- package/src/runtime/host-monitor-rate-gate.ts +1 -123
- package/src/runtime/mailbox-host-relay.ts +1 -326
- package/src/runtime/observability/detail-capture.ts +4 -108
- package/src/runtime/observability/process-timing.ts +2 -59
- package/src/runtime/observability/recorder.ts +2 -238
- package/src/runtime/observability/redaction.ts +2 -181
- package/src/runtime/observability/reporter.ts +2 -154
- package/src/runtime/observability/sinks.ts +2 -135
- package/src/runtime/observability/types.ts +2 -92
- package/src/runtime/owner-control.ts +45 -320
- package/src/runtime/parent-process-watch.ts +1 -51
- package/src/runtime/process-identity.ts +1 -72
- package/src/runtime/threaded-ws-client.ts +11 -2
- package/src/runtime/ws-client.ts +6 -0
- package/src/sdk/behavior-plugin/index.ts +9 -3
- package/src/sdk/index.ts +40 -2
- package/src/sdk/module-input/index.ts +5 -0
- package/src/sdk/perception-plugin/index.ts +3 -1
- package/src/sdk/perception-plugin/module.ts +12 -2
- package/src/sdk/perception-plugin/reminder-rules.ts +8 -42
- package/src/sdk/perception-plugin/reminder.ts +2 -1
- package/src/sdk/perception-plugin/task-change-tracker.ts +258 -0
- package/src/sdk/strategy-memory.ts +1 -12
- package/src/sdk/strategy-plugin/index.ts +36 -0
- package/src/sdk/trigger-plugin/index.ts +1 -0
- package/docs/custom-modules/trigger.md +0 -295
- package/skills/clawclaw/references/CUSTOM-PERCEPTION-DEVELOPMENT.md +0 -692
- package/skills/clawclaw/references/CUSTOM-SKILL-DEVELOPMENT.md +0 -67
|
@@ -1,209 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import type {
|
|
4
|
-
TrajectoryRecord,
|
|
5
|
-
TrajectorySubjectKind,
|
|
6
|
-
} from './contracts.js';
|
|
7
|
-
|
|
8
|
-
export interface SpatialObservation<TPosition = unknown> {
|
|
9
|
-
subjectId: string;
|
|
10
|
-
subjectKind: Exclude<TrajectorySubjectKind, 'system'>;
|
|
11
|
-
/** Players with visible=false are explicit visibility-loss observations. */
|
|
12
|
-
visible?: boolean;
|
|
13
|
-
position?: TPosition;
|
|
14
|
-
spaceId?: string;
|
|
15
|
-
areaId?: string;
|
|
16
|
-
attributes?: Record<string, unknown>;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface SpatialObservationFrame<TPosition = unknown> {
|
|
20
|
-
scope: RuntimeScope;
|
|
21
|
-
connectionId?: string;
|
|
22
|
-
sourceType: 'backend_state' | 'canonical_event';
|
|
23
|
-
sourceKey: string;
|
|
24
|
-
sourceSeq?: number | string;
|
|
25
|
-
backendTick?: number;
|
|
26
|
-
observedAtMs?: number;
|
|
27
|
-
receivedAtMs: number;
|
|
28
|
-
/** True only when omitted players are known to be outside the visible set. */
|
|
29
|
-
visibleSetComplete: boolean;
|
|
30
|
-
observations: SpatialObservation<TPosition>[];
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface StoredSpatialObservation<TPosition = unknown> {
|
|
34
|
-
position: TPosition;
|
|
35
|
-
spaceId?: string;
|
|
36
|
-
areaId?: string;
|
|
37
|
-
attributes?: Record<string, unknown>;
|
|
38
|
-
receivedAtMs: number;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface TrajectoryProjectorOptions<TPosition = unknown> {
|
|
42
|
-
maxAnchorIntervalMs?: number;
|
|
43
|
-
shouldStorePosition?: (
|
|
44
|
-
previous: StoredSpatialObservation<TPosition>,
|
|
45
|
-
current: StoredSpatialObservation<TPosition>,
|
|
46
|
-
) => boolean;
|
|
47
|
-
idFactory?: () => string;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function defaultShouldStore<TPosition>(
|
|
51
|
-
previous: StoredSpatialObservation<TPosition>,
|
|
52
|
-
current: StoredSpatialObservation<TPosition>,
|
|
53
|
-
): boolean {
|
|
54
|
-
return previous.spaceId !== current.spaceId
|
|
55
|
-
|| previous.areaId !== current.areaId
|
|
56
|
-
|| JSON.stringify(previous.position) !== JSON.stringify(current.position)
|
|
57
|
-
|| JSON.stringify(previous.attributes) !== JSON.stringify(current.attributes);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export class TrajectoryProjector<TPosition = unknown> {
|
|
61
|
-
private activeScopeKey: string | null = null;
|
|
62
|
-
private readonly lastStored = new Map<string, StoredSpatialObservation<TPosition>>();
|
|
63
|
-
private readonly visiblePlayers = new Set<string>();
|
|
64
|
-
private readonly maxAnchorIntervalMs: number;
|
|
65
|
-
private readonly shouldStorePosition: NonNullable<TrajectoryProjectorOptions<TPosition>['shouldStorePosition']>;
|
|
66
|
-
private readonly idFactory: () => string;
|
|
67
|
-
|
|
68
|
-
constructor(options: TrajectoryProjectorOptions<TPosition> = {}) {
|
|
69
|
-
this.maxAnchorIntervalMs = options.maxAnchorIntervalMs ?? 30_000;
|
|
70
|
-
this.shouldStorePosition = options.shouldStorePosition ?? defaultShouldStore;
|
|
71
|
-
this.idFactory = options.idFactory ?? randomUUID;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
project(frame: SpatialObservationFrame<TPosition>): TrajectoryRecord<TPosition>[] {
|
|
75
|
-
this.ensureScope(frame.scope);
|
|
76
|
-
const records: TrajectoryRecord<TPosition>[] = [];
|
|
77
|
-
const visibleThisFrame = new Set<string>();
|
|
78
|
-
|
|
79
|
-
for (const observation of frame.observations) {
|
|
80
|
-
if (!observation.subjectId) continue;
|
|
81
|
-
if (observation.subjectKind === 'player' && observation.visible === false) {
|
|
82
|
-
if (this.visiblePlayers.delete(observation.subjectId)) {
|
|
83
|
-
records.push(this.makeRecord(frame, observation, 'visibility_lost'));
|
|
84
|
-
}
|
|
85
|
-
this.lastStored.delete(observation.subjectId);
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (observation.subjectKind === 'player') {
|
|
90
|
-
visibleThisFrame.add(observation.subjectId);
|
|
91
|
-
if (!this.visiblePlayers.has(observation.subjectId)) {
|
|
92
|
-
records.push(this.makeRecord(frame, observation, 'visibility_gained'));
|
|
93
|
-
}
|
|
94
|
-
this.visiblePlayers.add(observation.subjectId);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (observation.position === undefined) continue;
|
|
98
|
-
const current: StoredSpatialObservation<TPosition> = {
|
|
99
|
-
position: cloneCanonical(observation.position),
|
|
100
|
-
spaceId: observation.spaceId,
|
|
101
|
-
areaId: observation.areaId,
|
|
102
|
-
attributes: observation.attributes ? cloneCanonical(observation.attributes) : undefined,
|
|
103
|
-
receivedAtMs: frame.receivedAtMs,
|
|
104
|
-
};
|
|
105
|
-
const previous = this.lastStored.get(observation.subjectId);
|
|
106
|
-
const anchorExpired = !!previous
|
|
107
|
-
&& frame.receivedAtMs - previous.receivedAtMs >= this.maxAnchorIntervalMs;
|
|
108
|
-
if (!previous || anchorExpired || this.shouldStorePosition(previous, current)) {
|
|
109
|
-
records.push(this.makeRecord(frame, observation, 'position'));
|
|
110
|
-
this.lastStored.set(observation.subjectId, current);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (frame.visibleSetComplete) {
|
|
115
|
-
for (const subjectId of Array.from(this.visiblePlayers)) {
|
|
116
|
-
if (visibleThisFrame.has(subjectId)) continue;
|
|
117
|
-
records.push(this.makeRecord(frame, {
|
|
118
|
-
subjectId,
|
|
119
|
-
subjectKind: 'player',
|
|
120
|
-
visible: false,
|
|
121
|
-
}, 'visibility_lost'));
|
|
122
|
-
this.visiblePlayers.delete(subjectId);
|
|
123
|
-
this.lastStored.delete(subjectId);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
return records;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
streamGapStarted(
|
|
130
|
-
scope: RuntimeScope,
|
|
131
|
-
connectionId: string | undefined,
|
|
132
|
-
sourceKey: string,
|
|
133
|
-
receivedAtMs = Date.now(),
|
|
134
|
-
): TrajectoryRecord<TPosition>[] {
|
|
135
|
-
this.ensureScope(scope);
|
|
136
|
-
this.visiblePlayers.clear();
|
|
137
|
-
this.lastStored.clear();
|
|
138
|
-
return [this.systemRecord(scope, connectionId, sourceKey, 'stream_gap_started', receivedAtMs)];
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
streamGapEnded(
|
|
142
|
-
scope: RuntimeScope,
|
|
143
|
-
connectionId: string | undefined,
|
|
144
|
-
sourceKey: string,
|
|
145
|
-
receivedAtMs = Date.now(),
|
|
146
|
-
): TrajectoryRecord<TPosition>[] {
|
|
147
|
-
this.ensureScope(scope);
|
|
148
|
-
return [this.systemRecord(scope, connectionId, sourceKey, 'stream_gap_ended', receivedAtMs)];
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
reset(): void {
|
|
152
|
-
this.activeScopeKey = null;
|
|
153
|
-
this.lastStored.clear();
|
|
154
|
-
this.visiblePlayers.clear();
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
private ensureScope(scope: RuntimeScope): void {
|
|
158
|
-
const key = runtimeScopeKey(scope);
|
|
159
|
-
if (this.activeScopeKey === key) return;
|
|
160
|
-
this.activeScopeKey = key;
|
|
161
|
-
this.lastStored.clear();
|
|
162
|
-
this.visiblePlayers.clear();
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
private makeRecord(
|
|
166
|
-
frame: SpatialObservationFrame<TPosition>,
|
|
167
|
-
observation: SpatialObservation<TPosition>,
|
|
168
|
-
kind: TrajectoryRecord<TPosition>['kind'],
|
|
169
|
-
): TrajectoryRecord<TPosition> {
|
|
170
|
-
return {
|
|
171
|
-
recordId: this.idFactory(),
|
|
172
|
-
scope: cloneCanonical(frame.scope),
|
|
173
|
-
connectionId: frame.connectionId,
|
|
174
|
-
subjectId: observation.subjectId,
|
|
175
|
-
subjectKind: observation.subjectKind,
|
|
176
|
-
kind,
|
|
177
|
-
sourceType: frame.sourceType,
|
|
178
|
-
sourceKey: frame.sourceKey,
|
|
179
|
-
sourceSeq: frame.sourceSeq,
|
|
180
|
-
backendTick: frame.backendTick,
|
|
181
|
-
observedAtMs: frame.observedAtMs,
|
|
182
|
-
receivedAtMs: frame.receivedAtMs,
|
|
183
|
-
spaceId: observation.spaceId,
|
|
184
|
-
areaId: observation.areaId,
|
|
185
|
-
position: observation.position === undefined ? undefined : cloneCanonical(observation.position),
|
|
186
|
-
attributes: observation.attributes ? cloneCanonical(observation.attributes) : undefined,
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
private systemRecord(
|
|
191
|
-
scope: RuntimeScope,
|
|
192
|
-
connectionId: string | undefined,
|
|
193
|
-
sourceKey: string,
|
|
194
|
-
kind: 'stream_gap_started' | 'stream_gap_ended',
|
|
195
|
-
receivedAtMs: number,
|
|
196
|
-
): TrajectoryRecord<TPosition> {
|
|
197
|
-
return {
|
|
198
|
-
recordId: this.idFactory(),
|
|
199
|
-
scope: cloneCanonical(scope),
|
|
200
|
-
connectionId,
|
|
201
|
-
subjectId: '__stream__',
|
|
202
|
-
subjectKind: 'system',
|
|
203
|
-
kind,
|
|
204
|
-
sourceType: 'local_lifecycle',
|
|
205
|
-
sourceKey,
|
|
206
|
-
receivedAtMs,
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
}
|
|
1
|
+
// Compatibility facade; implementation lives in the game-neutral runtime package.
|
|
2
|
+
export * from '@myclaw163/game-agent-runtime/framework/state/history/trajectory-projector.js';
|
|
@@ -1,13 +1 @@
|
|
|
1
|
-
export * from '
|
|
2
|
-
export * from './contracts.js';
|
|
3
|
-
export * from './current-state-store.js';
|
|
4
|
-
export * from './freshness.js';
|
|
5
|
-
export * from './state-ingress.js';
|
|
6
|
-
export * from './state-provider.js';
|
|
7
|
-
export * from './durable-agent-state-snapshot.js';
|
|
8
|
-
|
|
9
|
-
export * from './history/contracts.js';
|
|
10
|
-
export * from './history/memory-state-history-repository.js';
|
|
11
|
-
export * from './history/sqlite-state-history-repository.js';
|
|
12
|
-
export * from './history/trajectory-projector.js';
|
|
13
|
-
export * from './history/history-writer.js';
|
|
1
|
+
export * from '@myclaw163/game-agent-runtime/framework/state/index.js';
|
|
@@ -1,95 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
ApplyStateResult,
|
|
4
|
-
CurrentStateSnapshot,
|
|
5
|
-
RuntimeConnectionState,
|
|
6
|
-
RuntimeScope,
|
|
7
|
-
StateFactPatch,
|
|
8
|
-
StateFrameEnvelope,
|
|
9
|
-
} from './types.js';
|
|
10
|
-
|
|
11
|
-
export class StateIngress<TState = unknown> {
|
|
12
|
-
private readonly observers = new Set<StateIngressObserver<TState>>();
|
|
13
|
-
|
|
14
|
-
constructor(
|
|
15
|
-
private readonly store: CurrentStateWriter<TState>,
|
|
16
|
-
observers: StateIngressObserver<TState>[] = [],
|
|
17
|
-
) {
|
|
18
|
-
for (const observer of observers) this.observers.add(observer);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
addObserver(observer: StateIngressObserver<TState>): () => void {
|
|
22
|
-
this.observers.add(observer);
|
|
23
|
-
return () => this.observers.delete(observer);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
openScope(scope: RuntimeScope, atMs = Date.now()): CurrentStateSnapshot<TState> {
|
|
27
|
-
return this.store.openScope(scope, atMs);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
applyFacts(patches: StateFactPatch[], atMs = Date.now()): CurrentStateSnapshot<TState> {
|
|
31
|
-
return this.store.applyFacts(patches, atMs);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
connectionOpened(connectionId: string, atMs = Date.now()): CurrentStateSnapshot<TState> {
|
|
35
|
-
const snapshot = this.store.openConnection(connectionId, atMs);
|
|
36
|
-
this.notifyConnection(snapshot.scope, connectionId, 'connected', atMs);
|
|
37
|
-
return snapshot;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
connectionChanged(
|
|
41
|
-
connectionId: string,
|
|
42
|
-
state: RuntimeConnectionState,
|
|
43
|
-
atMs = Date.now(),
|
|
44
|
-
): CurrentStateSnapshot<TState> {
|
|
45
|
-
const snapshot = this.store.markConnectionState(connectionId, state, atMs);
|
|
46
|
-
if (snapshot.meta.connectionId === connectionId && snapshot.meta.connectionState === state) {
|
|
47
|
-
this.notifyConnection(snapshot.scope, connectionId, state, atMs);
|
|
48
|
-
}
|
|
49
|
-
return snapshot;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
livenessReceived(connectionId: string, atMs = Date.now()): boolean {
|
|
53
|
-
return this.store.markLiveness(connectionId, atMs);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
accept(frame: StateFrameEnvelope<TState>): ApplyStateResult<TState> {
|
|
57
|
-
const result = this.store.applyBackendState(frame);
|
|
58
|
-
if (result.accepted) {
|
|
59
|
-
for (const observer of this.observers) {
|
|
60
|
-
this.runObserver(observer, () => observer.onFrameAccepted?.(frame, result));
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return result;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
snapshot(): CurrentStateSnapshot<TState> {
|
|
67
|
-
return this.store.snapshot();
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
private notifyConnection(
|
|
71
|
-
scope: RuntimeScope | null,
|
|
72
|
-
connectionId: string,
|
|
73
|
-
state: RuntimeConnectionState,
|
|
74
|
-
atMs: number,
|
|
75
|
-
): void {
|
|
76
|
-
for (const observer of this.observers) {
|
|
77
|
-
this.runObserver(observer, () => observer.onConnectionChanged?.(scope, connectionId, state, atMs));
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
private runObserver(observer: StateIngressObserver<TState>, run: () => void | Promise<void> | undefined): void {
|
|
82
|
-
try {
|
|
83
|
-
const pending = run();
|
|
84
|
-
if (pending && typeof pending.then === 'function') {
|
|
85
|
-
void pending.catch((error) => this.reportObserverError(observer, error));
|
|
86
|
-
}
|
|
87
|
-
} catch (error) {
|
|
88
|
-
this.reportObserverError(observer, error);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
private reportObserverError(observer: StateIngressObserver<TState>, error: unknown): void {
|
|
93
|
-
try { observer.onObserverError?.(error); } catch {}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
1
|
+
// Compatibility facade; implementation lives in the game-neutral runtime package.
|
|
2
|
+
export * from '@myclaw163/game-agent-runtime/framework/state/state-ingress.js';
|
|
@@ -1,62 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import type {
|
|
4
|
-
CurrentStateSnapshot,
|
|
5
|
-
RuntimeScope,
|
|
6
|
-
StateProviderResult,
|
|
7
|
-
StateStatus,
|
|
8
|
-
} from './types.js';
|
|
9
|
-
|
|
10
|
-
const identityProjector = <TState>(snapshot: CurrentStateSnapshot<TState>): CurrentStateSnapshot<TState> => snapshot;
|
|
11
|
-
|
|
12
|
-
export class StateProvider<TState = unknown, TView = CurrentStateSnapshot<TState>> {
|
|
13
|
-
constructor(
|
|
14
|
-
private readonly reader: CurrentStateReader<TState> | null,
|
|
15
|
-
private readonly freshness = new StateFreshnessEvaluator(),
|
|
16
|
-
private readonly projector: StateViewProjector<TState, TView> = identityProjector as StateViewProjector<TState, TView>,
|
|
17
|
-
) {}
|
|
18
|
-
|
|
19
|
-
read(expectedScope?: RuntimeScope | null, nowMs = Date.now()): StateProviderResult<TView> {
|
|
20
|
-
if (!this.reader) return this.ownerUnavailable(expectedScope ?? null);
|
|
21
|
-
try {
|
|
22
|
-
const snapshot = this.reader.snapshot();
|
|
23
|
-
const stateStatus = this.freshness.evaluate(snapshot, expectedScope, nowMs);
|
|
24
|
-
if (stateStatus.status === 'unavailable') {
|
|
25
|
-
return {
|
|
26
|
-
state: null,
|
|
27
|
-
stateStatus,
|
|
28
|
-
stateError: {
|
|
29
|
-
code: stateStatus.reason === 'scope_mismatch' ? 'STATE_SCOPE_MISMATCH' : 'STATE_NOT_READY',
|
|
30
|
-
message: 'Current owner state is unavailable for the requested scope.',
|
|
31
|
-
},
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
return { state: this.projector(snapshot), stateStatus };
|
|
35
|
-
} catch {
|
|
36
|
-
return this.ownerUnavailable(expectedScope ?? null, 'provider_error');
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
private ownerUnavailable(scope: RuntimeScope | null, reason = 'owner_unavailable'): StateProviderResult<TView> {
|
|
41
|
-
const stateStatus: StateStatus = {
|
|
42
|
-
status: 'unavailable',
|
|
43
|
-
semantics: 'latest_received',
|
|
44
|
-
source: null,
|
|
45
|
-
scope,
|
|
46
|
-
connectionId: null,
|
|
47
|
-
connectionState: 'owner_unavailable',
|
|
48
|
-
receivedAtMs: null,
|
|
49
|
-
ageMs: null,
|
|
50
|
-
backendTick: null,
|
|
51
|
-
reason,
|
|
52
|
-
};
|
|
53
|
-
return {
|
|
54
|
-
state: null,
|
|
55
|
-
stateStatus,
|
|
56
|
-
stateError: {
|
|
57
|
-
code: 'OWNER_NOT_READY',
|
|
58
|
-
message: 'Active game owner is unavailable.',
|
|
59
|
-
},
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
}
|
|
1
|
+
// Compatibility facade; implementation lives in the game-neutral runtime package.
|
|
2
|
+
export * from '@myclaw163/game-agent-runtime/framework/state/state-provider.js';
|
|
@@ -1,155 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
accountId: string;
|
|
4
|
-
sessionId: string;
|
|
5
|
-
segmentId: string;
|
|
6
|
-
segmentSeq?: number;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export type FactLifetime = 'session' | 'segment';
|
|
10
|
-
|
|
11
|
-
export interface FactProvenance {
|
|
12
|
-
sourceType: string;
|
|
13
|
-
sourceKey?: string;
|
|
14
|
-
receivedAtMs?: number;
|
|
15
|
-
[key: string]: unknown;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface StateFactPatch {
|
|
19
|
-
key: string;
|
|
20
|
-
value: unknown;
|
|
21
|
-
lifetime: FactLifetime;
|
|
22
|
-
provenance: FactProvenance;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface StoredStateFact extends StateFactPatch {
|
|
26
|
-
updatedAtMs: number;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export type RuntimeConnectionState =
|
|
30
|
-
| 'disconnected'
|
|
31
|
-
| 'connecting'
|
|
32
|
-
| 'connected'
|
|
33
|
-
| 'reconnecting';
|
|
34
|
-
|
|
35
|
-
export interface StateFrameEnvelope<TState = unknown> {
|
|
36
|
-
scope: RuntimeScope;
|
|
37
|
-
sourceId?: string;
|
|
38
|
-
connectionId: string;
|
|
39
|
-
payload: TState;
|
|
40
|
-
/** Stable key for persistence/idempotency within this scope. */
|
|
41
|
-
sourceKey: string;
|
|
42
|
-
/** Server sequence when the transport provides one. */
|
|
43
|
-
sourceSeq?: number | string;
|
|
44
|
-
backendTick?: number;
|
|
45
|
-
observedAtMs?: number;
|
|
46
|
-
receivedAtMs: number;
|
|
47
|
-
receivedMonotonicMs?: number;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface CurrentStateMeta {
|
|
51
|
-
sourceId: string | null;
|
|
52
|
-
connectionId: string | null;
|
|
53
|
-
connectionState: RuntimeConnectionState;
|
|
54
|
-
awaitingFirstFrame: boolean;
|
|
55
|
-
receiveSeq: number;
|
|
56
|
-
lastSourceSeq: number | string | null;
|
|
57
|
-
backendTick: number | null;
|
|
58
|
-
lastStateSourceKey: string | null;
|
|
59
|
-
lastStateReceivedAtMs: number | null;
|
|
60
|
-
lastStateReceivedMonotonicMs: number | null;
|
|
61
|
-
lastLivenessAtMs: number | null;
|
|
62
|
-
gapSinceMs: number | null;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export interface CurrentStateSnapshot<TState = unknown> {
|
|
66
|
-
scope: RuntimeScope | null;
|
|
67
|
-
backendState: TState | null;
|
|
68
|
-
globalFacts: Record<string, unknown>;
|
|
69
|
-
facts: {
|
|
70
|
-
session: Record<string, StoredStateFact>;
|
|
71
|
-
segment: Record<string, StoredStateFact>;
|
|
72
|
-
};
|
|
73
|
-
meta: CurrentStateMeta;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export type ApplyStateRejectedReason =
|
|
77
|
-
| 'scope_not_open'
|
|
78
|
-
| 'scope_mismatch'
|
|
79
|
-
| 'connection_not_open'
|
|
80
|
-
| 'stale_connection'
|
|
81
|
-
| 'duplicate_or_out_of_order';
|
|
82
|
-
|
|
83
|
-
export type ApplyStateResult<TState = unknown> =
|
|
84
|
-
| {
|
|
85
|
-
accepted: true;
|
|
86
|
-
receiveSeq: number;
|
|
87
|
-
firstFrameAfterConnect: boolean;
|
|
88
|
-
snapshot: CurrentStateSnapshot<TState>;
|
|
89
|
-
}
|
|
90
|
-
| {
|
|
91
|
-
accepted: false;
|
|
92
|
-
reason: ApplyStateRejectedReason;
|
|
93
|
-
snapshot: CurrentStateSnapshot<TState>;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
export type StateAvailability = 'fresh' | 'stale' | 'unavailable';
|
|
97
|
-
|
|
98
|
-
export interface StateFreshnessPolicy {
|
|
99
|
-
/** Optional heartbeat/liveness deadline. Omit for transports without liveness evidence. */
|
|
100
|
-
maxLivenessSilenceMs?: number;
|
|
101
|
-
/** Use only when the adapter promises periodic state frames. */
|
|
102
|
-
maxStateSilenceMs?: number;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export type StateFreshnessPolicyResolver = (
|
|
106
|
-
sourceId: string | null,
|
|
107
|
-
snapshot: CurrentStateSnapshot,
|
|
108
|
-
) => StateFreshnessPolicy | undefined;
|
|
109
|
-
|
|
110
|
-
export interface StateStatus {
|
|
111
|
-
status: StateAvailability;
|
|
112
|
-
semantics: 'latest_received';
|
|
113
|
-
source: 'owner_memory' | 'durable_snapshot' | null;
|
|
114
|
-
scope: RuntimeScope | null;
|
|
115
|
-
connectionId: string | null;
|
|
116
|
-
connectionState: RuntimeConnectionState | 'owner_unavailable';
|
|
117
|
-
receivedAtMs: number | null;
|
|
118
|
-
ageMs: number | null;
|
|
119
|
-
backendTick: number | null;
|
|
120
|
-
reason: string | null;
|
|
121
|
-
/** True only when the adapter or the durable participation says this is a final snapshot. */
|
|
122
|
-
terminal?: boolean;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export interface StateProviderResult<TView = CurrentStateSnapshot> {
|
|
126
|
-
state: TView | null;
|
|
127
|
-
stateStatus: StateStatus;
|
|
128
|
-
stateError?: {
|
|
129
|
-
code: string;
|
|
130
|
-
message: string;
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export function runtimeScopeKey(scope: RuntimeScope): string {
|
|
135
|
-
return [scope.adapterId, scope.accountId, scope.sessionId, scope.segmentId].join('\u001f');
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function sameRuntimeScope(a: RuntimeScope | null | undefined, b: RuntimeScope | null | undefined): boolean {
|
|
139
|
-
return !!a && !!b
|
|
140
|
-
&& a.adapterId === b.adapterId
|
|
141
|
-
&& a.accountId === b.accountId
|
|
142
|
-
&& a.sessionId === b.sessionId
|
|
143
|
-
&& a.segmentId === b.segmentId;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export function sameRuntimeSession(a: RuntimeScope | null | undefined, b: RuntimeScope | null | undefined): boolean {
|
|
147
|
-
return !!a && !!b
|
|
148
|
-
&& a.adapterId === b.adapterId
|
|
149
|
-
&& a.accountId === b.accountId
|
|
150
|
-
&& a.sessionId === b.sessionId;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export function cloneCanonical<T>(value: T): T {
|
|
154
|
-
return structuredClone(value);
|
|
155
|
-
}
|
|
1
|
+
// Compatibility facade; implementation lives in the game-neutral runtime package.
|
|
2
|
+
export * from '@myclaw163/game-agent-runtime/framework/state/types.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { GameSyncPerceptionReport } from './game-sync-protocol.js';
|
|
1
|
+
import type { GameSyncPerceptionReport, GameSyncRuntimeReport } from './game-sync-protocol.js';
|
|
2
2
|
import type { BufferedRawWsLogOptions, RawWsLogStats } from './raw-ws-log.js';
|
|
3
3
|
import type { WsConnectionState } from '../sdk/types.js';
|
|
4
4
|
import type { GameSyncResumeState, WsCloseInfo, WsInputFrame, WsProtocolErrorInfo, WsRecoveryStartedInfo } from './ws-client.js';
|
|
@@ -17,7 +17,8 @@ export interface GameSyncIngressConfig {
|
|
|
17
17
|
|
|
18
18
|
export type IngressCommand =
|
|
19
19
|
| { type: 'consumed'; token: string }
|
|
20
|
-
| { type: 'report'; perceptions: readonly GameSyncPerceptionReport[] }
|
|
20
|
+
| { type: 'report'; perceptions: readonly GameSyncPerceptionReport[]; connectionId: string }
|
|
21
|
+
| { type: 'runtime_report'; report: GameSyncRuntimeReport; connectionId: string }
|
|
21
22
|
| { type: 'telemetry_ack' }
|
|
22
23
|
| { type: 'stop' };
|
|
23
24
|
|
|
@@ -248,7 +248,13 @@ port.on('message', (message: IngressCommand) => {
|
|
|
248
248
|
pump();
|
|
249
249
|
reconnectWhenDrained();
|
|
250
250
|
} else if (message.type === 'report') {
|
|
251
|
-
try {
|
|
251
|
+
try {
|
|
252
|
+
if (message.connectionId === client.connectionId) client.reportPerceptions(message.perceptions);
|
|
253
|
+
} finally { post({ type: 'report_done' }); }
|
|
254
|
+
} else if (message.type === 'runtime_report') {
|
|
255
|
+
try {
|
|
256
|
+
if (message.connectionId === client.connectionId) client.reportRuntime(message.report);
|
|
257
|
+
} finally { post({ type: 'report_done' }); }
|
|
252
258
|
} else if (message.type === 'telemetry_ack') {
|
|
253
259
|
telemetryPending = false;
|
|
254
260
|
} else if (message.type === 'stop') {
|
|
@@ -31,11 +31,31 @@ export interface GameSyncErrorData {
|
|
|
31
31
|
readonly message?: string;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
export
|
|
34
|
+
export type GameSyncPerceptionTime =
|
|
35
|
+
| { readonly source_tick: number; readonly from_source_tick?: never; readonly to_source_tick?: never }
|
|
36
|
+
| { readonly source_tick?: never; readonly from_source_tick: number; readonly to_source_tick: number };
|
|
37
|
+
|
|
38
|
+
export type GameSyncPerceptionReport = GameSyncPerceptionTime & {
|
|
35
39
|
readonly id: string;
|
|
40
|
+
readonly perceiver_id?: string;
|
|
36
41
|
readonly name: string;
|
|
37
42
|
readonly output: string;
|
|
38
|
-
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export interface GameSyncResourceInfo {
|
|
46
|
+
readonly id: string;
|
|
47
|
+
readonly name: string;
|
|
48
|
+
readonly description?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface GameSyncRuntimeReport {
|
|
52
|
+
readonly perceivers?: readonly GameSyncResourceInfo[] | null;
|
|
53
|
+
readonly strategies?: readonly GameSyncResourceInfo[] | null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface GameSyncRuntimeReportMessage {
|
|
57
|
+
readonly type: 'game.sync.runtime.report';
|
|
58
|
+
readonly data: GameSyncRuntimeReport;
|
|
39
59
|
}
|
|
40
60
|
|
|
41
61
|
export interface GameSyncPerceptionReportMessage {
|
|
@@ -87,6 +107,36 @@ function eventArray(value: unknown, field: string): Record<string, any>[] {
|
|
|
87
107
|
|
|
88
108
|
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
89
109
|
|
|
110
|
+
function resourceId(value: unknown, field: string): string {
|
|
111
|
+
const id = nonEmptyString(value, field).trim();
|
|
112
|
+
if (!id || [...id].length > 128) throw new TypeError(`${field} must contain 1 to 128 characters`);
|
|
113
|
+
return id;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function gameSyncRuntimeReportMessage(report: GameSyncRuntimeReport): GameSyncRuntimeReportMessage {
|
|
117
|
+
if (report.perceivers == null && report.strategies == null) {
|
|
118
|
+
throw new TypeError('GameSync runtime report requires perceivers or strategies');
|
|
119
|
+
}
|
|
120
|
+
const data: { perceivers?: GameSyncResourceInfo[] | null; strategies?: GameSyncResourceInfo[] | null } = {};
|
|
121
|
+
for (const key of ['perceivers', 'strategies'] as const) {
|
|
122
|
+
const list = report[key];
|
|
123
|
+
if (list === undefined) continue;
|
|
124
|
+
if (list === null) { data[key] = null; continue; }
|
|
125
|
+
if (!Array.isArray(list) || list.length > 50) throw new TypeError(`${key} must contain at most 50 resources`);
|
|
126
|
+
const ids = new Set<string>();
|
|
127
|
+
data[key] = list.map(item => {
|
|
128
|
+
const id = resourceId(item.id, `${key}.id`);
|
|
129
|
+
if (ids.has(id)) throw new TypeError(`${key} resource IDs must be unique`);
|
|
130
|
+
ids.add(id);
|
|
131
|
+
const name = nonEmptyString(item.name, `${key}.name`).trim();
|
|
132
|
+
if (!name) throw new TypeError(`${key}.name must not be blank`);
|
|
133
|
+
if (item.description !== undefined && typeof item.description !== 'string') throw new TypeError(`${key}.description must be a string`);
|
|
134
|
+
return { id, name, description: item.description?.trim() ?? '' };
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return { type: 'game.sync.runtime.report', data };
|
|
138
|
+
}
|
|
139
|
+
|
|
90
140
|
/** Build one client perception report frame using the backend's all-or-nothing validation rules. */
|
|
91
141
|
export function gameSyncPerceptionReportMessage(
|
|
92
142
|
perceptions: readonly GameSyncPerceptionReport[],
|
|
@@ -109,8 +159,18 @@ export function gameSyncPerceptionReportMessage(
|
|
|
109
159
|
if (typeof perception.output !== 'string') {
|
|
110
160
|
throw new TypeError(`GameSync perception report[${index}].output must be a string`);
|
|
111
161
|
}
|
|
112
|
-
|
|
113
|
-
|
|
162
|
+
const instant = perception.source_tick !== undefined;
|
|
163
|
+
const interval = perception.from_source_tick !== undefined || perception.to_source_tick !== undefined;
|
|
164
|
+
if (instant === interval) throw new TypeError('Perception report requires exactly one instantaneous or interval time');
|
|
165
|
+
if (instant) integer(perception.source_tick, `GameSync perception report[${index}].source_tick`);
|
|
166
|
+
else {
|
|
167
|
+
integer(perception.from_source_tick, `GameSync perception report[${index}].from_source_tick`);
|
|
168
|
+
integer(perception.to_source_tick, `GameSync perception report[${index}].to_source_tick`);
|
|
169
|
+
if (perception.from_source_tick! > perception.to_source_tick!) throw new TypeError('Perception report time interval is reversed');
|
|
170
|
+
}
|
|
171
|
+
return { ...perception, ...(perception.perceiver_id === undefined ? {} : {
|
|
172
|
+
perceiver_id: resourceId(perception.perceiver_id, `GameSync perception report[${index}].perceiver_id`),
|
|
173
|
+
}) };
|
|
114
174
|
});
|
|
115
175
|
return {
|
|
116
176
|
type: 'game.sync.perception.report',
|