@myclaw163/clawclaw-cli 0.9.2 → 0.10.1
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_OTHER.md +5 -5
- package/README.md +7 -8
- package/bin/clawclaw-channel.mjs +2 -2
- package/bin/clawclaw-cli.mjs +2 -2
- package/docs/configuration.md +25 -99
- 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 +1 -1
- package/docs/desktop-event-push.md +3 -3
- package/docs/runtime/README.md +16 -17
- package/docs/runtime/agent-notifications.md +31 -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 +1 -1
- package/package.json +24 -9
- package/scripts/check-framework-boundaries.mjs +27 -0
- package/scripts/check-skill-command-surface.mjs +8 -13
- package/scripts/install-local-runtime.mjs +35 -0
- package/scripts/qa-install.mjs +105 -0
- package/skills/clawclaw/SKILL.md +49 -49
- package/skills/clawclaw/references/BEHAVIORS.md +42 -73
- package/skills/clawclaw/references/CHANNELS.md +2 -2
- package/skills/clawclaw/references/COMMANDS.md +60 -101
- package/skills/clawclaw/references/CUSTOM-BEHAVIOR-DEVELOPMENT.md +94 -78
- package/skills/clawclaw/references/CUSTOM-MODULE-API.md +95 -180
- package/skills/clawclaw/references/CUSTOM-MODULES.md +24 -24
- package/skills/clawclaw/references/CUSTOM-PERCEPTION-DEVELOPMENT.md +42 -608
- package/skills/clawclaw/references/CUSTOM-SKILL-DEVELOPMENT.md +1 -1
- package/skills/clawclaw/references/HUB.md +1 -1
- package/skills/clawclaw/references/STREAM.md +8 -8
- package/skills/clawclaw/references/TACTICS.md +1 -1
- 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 +3 -3
- package/src/commands/load.ts +3 -2
- package/src/commands/perception.ts +5 -0
- package/src/commands/strategy.ts +2 -0
- package/src/lib/capability-load-diagnostics.ts +3 -3
- package/src/lib/perception-config.ts +18 -6
- package/src/lib/runtime-cadence-config.ts +16 -0
- 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 +18 -4
- 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 +25 -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/agent-mailbox-coordinator.ts +60 -572
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.ts +6 -434
- 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 +119 -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 +18 -7
- package/src/runtime/clawclaw/plugins/perception/perception-source.ts +12 -6
- 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/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/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
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import { pathToFileURL } from 'node:url';
|
|
4
|
-
import {
|
|
5
|
-
addEsmModuleRoot,
|
|
6
|
-
initialize as initializeRuntimeModuleHooks,
|
|
7
|
-
load as loadRuntimeModule,
|
|
8
|
-
resolve as resolveRuntimeModule,
|
|
9
|
-
} from './runtime-module-loader-hooks.mjs';
|
|
10
|
-
|
|
1
|
+
import * as loader from '@myclaw163/game-agent-runtime/framework/modules/runtime-module-loader.js';
|
|
2
|
+
export type { RuntimeModuleThreadLoaderConfig } from '@myclaw163/game-agent-runtime/framework/modules/runtime-module-loader.js';
|
|
11
3
|
const SDK_ALIASES = Object.freeze({
|
|
4
|
+
'@myclaw163/clawclaw-cli/strategy-plugin': new URL('../../../sdk/strategy-plugin/index.ts', import.meta.url).href,
|
|
5
|
+
'clawclaw-cli/strategy-plugin': new URL('../../../sdk/strategy-plugin/index.ts', import.meta.url).href,
|
|
12
6
|
'@myclaw163/clawclaw-cli': new URL('../../../sdk/index.ts', import.meta.url).href,
|
|
13
7
|
'@myclaw163/clawclaw-cli/sdk': new URL('../../../sdk/index.ts', import.meta.url).href,
|
|
14
8
|
'@myclaw163/clawclaw-cli/module-input': new URL(
|
|
@@ -38,44 +32,6 @@ const SDK_ALIASES = Object.freeze({
|
|
|
38
32
|
).href,
|
|
39
33
|
});
|
|
40
34
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
export
|
|
44
|
-
readonly hooksUrl: string;
|
|
45
|
-
readonly sdkAliases: Readonly<Record<string, string>>;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function moduleUrl(source: string | URL): URL {
|
|
49
|
-
if (source instanceof URL) return source;
|
|
50
|
-
return source.startsWith('file:') ? new URL(source) : pathToFileURL(resolve(source));
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Import one trusted local Behavior/Perception/Trigger module without bundling it.
|
|
55
|
-
* The scoped tsx loader maps the public SDK package names back to this exact
|
|
56
|
-
* installed CCL package, even when the module lives under the user's data path.
|
|
57
|
-
*/
|
|
58
|
-
export async function importRuntimeModule(source: string | URL): Promise<Record<string, unknown>> {
|
|
59
|
-
const target = moduleUrl(source);
|
|
60
|
-
registerRuntimeModuleLoader();
|
|
61
|
-
addEsmModuleRoot(new URL('.', target).href);
|
|
62
|
-
return import(target.href) as Promise<Record<string, unknown>>;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** Register the direct-import hooks once in the current JavaScript isolate. */
|
|
66
|
-
export function registerRuntimeModuleLoader(): void {
|
|
67
|
-
if (loaderRegistered) return;
|
|
68
|
-
// One synchronous in-process hook transforms TypeScript and maps public SDK
|
|
69
|
-
// names to this exact installed package. No module gets its own compiler.
|
|
70
|
-
initializeRuntimeModuleHooks({ sdkAliases: SDK_ALIASES, nativeTypeScript: true });
|
|
71
|
-
registerHooks({ resolve: resolveRuntimeModule, load: loadRuntimeModule });
|
|
72
|
-
loaderRegistered = true;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/** Loader data for Worker Threads, which use the same in-process TS transform. */
|
|
76
|
-
export function runtimeModuleThreadLoaderConfig(): RuntimeModuleThreadLoaderConfig {
|
|
77
|
-
return {
|
|
78
|
-
hooksUrl: new URL('./runtime-module-loader-hooks.mjs', import.meta.url).href,
|
|
79
|
-
sdkAliases: SDK_ALIASES,
|
|
80
|
-
};
|
|
81
|
-
}
|
|
35
|
+
export const importRuntimeModule = (source: string | URL) => loader.importRuntimeModule(source, SDK_ALIASES);
|
|
36
|
+
export const registerRuntimeModuleLoader = () => loader.registerRuntimeModuleLoader(SDK_ALIASES);
|
|
37
|
+
export const runtimeModuleThreadLoaderConfig = () => loader.runtimeModuleThreadLoaderConfig(SDK_ALIASES);
|
|
@@ -1,24 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export const MAX_RUNTIME_MODULE_OUTPUT_BYTES = 1024 * 1024;
|
|
4
|
-
|
|
5
|
-
export interface RuntimeModuleOutputSize {
|
|
6
|
-
readonly bytes: number;
|
|
7
|
-
readonly limitBytes: number;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/** Bounds cloneable external-module output before it is copied into the Owner. */
|
|
11
|
-
export function assertRuntimeModuleOutputSize(
|
|
12
|
-
value: unknown,
|
|
13
|
-
code: string,
|
|
14
|
-
limitBytes = MAX_RUNTIME_MODULE_OUTPUT_BYTES,
|
|
15
|
-
): RuntimeModuleOutputSize {
|
|
16
|
-
let bytes: number;
|
|
17
|
-
try {
|
|
18
|
-
bytes = serialize(value).byteLength;
|
|
19
|
-
} catch {
|
|
20
|
-
throw new Error(`${code}_not_serializable`);
|
|
21
|
-
}
|
|
22
|
-
if (bytes > limitBytes) throw new Error(`${code}_too_large:${bytes}:${limitBytes}`);
|
|
23
|
-
return { bytes, limitBytes };
|
|
24
|
-
}
|
|
1
|
+
// Compatibility facade; implementation lives in the game-neutral runtime package.
|
|
2
|
+
export * from '@myclaw163/game-agent-runtime/framework/modules/runtime-module-output.js';
|
|
@@ -1,23 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const bootstrap = workerData?.runtimeModuleBootstrap;
|
|
5
|
-
if (
|
|
6
|
-
!bootstrap
|
|
7
|
-
|| typeof bootstrap.hostUrl !== 'string'
|
|
8
|
-
|| typeof bootstrap.hooksUrl !== 'string'
|
|
9
|
-
|| !bootstrap.sdkAliases
|
|
10
|
-
|| typeof bootstrap.sdkAliases !== 'object'
|
|
11
|
-
|| (bootstrap.esmModuleRoots !== undefined && !Array.isArray(bootstrap.esmModuleRoots))
|
|
12
|
-
) {
|
|
13
|
-
throw new Error('runtime_module_worker_bootstrap_invalid');
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const hooks = await import(bootstrap.hooksUrl);
|
|
17
|
-
hooks.initialize({
|
|
18
|
-
sdkAliases: bootstrap.sdkAliases,
|
|
19
|
-
nativeTypeScript: true,
|
|
20
|
-
esmModuleRoots: bootstrap.esmModuleRoots ?? [],
|
|
21
|
-
});
|
|
22
|
-
registerHooks({ resolve: hooks.resolve, load: hooks.load });
|
|
23
|
-
await import(bootstrap.hostUrl);
|
|
1
|
+
// Compatibility facade; implementation lives in the game-neutral runtime package.
|
|
2
|
+
export * from '@myclaw163/game-agent-runtime/framework/modules/runtime-module-worker-bootstrap.mjs';
|
|
3
|
+
import '@myclaw163/game-agent-runtime/framework/modules/runtime-module-worker-bootstrap.mjs';
|
|
@@ -1,253 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export interface ParticipationSession {
|
|
5
|
-
id: string;
|
|
6
|
-
adapterId: string;
|
|
7
|
-
accountId: string;
|
|
8
|
-
resumeKey?: string;
|
|
9
|
-
mode?: string;
|
|
10
|
-
startedAt: string;
|
|
11
|
-
endedAt?: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface ParticipationSegment {
|
|
15
|
-
id: string;
|
|
16
|
-
sessionId: string;
|
|
17
|
-
kind: string;
|
|
18
|
-
identity?: string;
|
|
19
|
-
ordinal: number;
|
|
20
|
-
startedAt: string;
|
|
21
|
-
endedAt?: string;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function canReuseParticipationSegment(
|
|
25
|
-
current: Pick<ParticipationSegment, 'kind' | 'identity' | 'endedAt'>,
|
|
26
|
-
kind: string,
|
|
27
|
-
identity?: string,
|
|
28
|
-
): boolean {
|
|
29
|
-
return !current.endedAt && current.kind === kind
|
|
30
|
-
&& (identity === undefined || current.identity === undefined || current.identity === identity);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface ParticipationSnapshot {
|
|
34
|
-
session: ParticipationSession;
|
|
35
|
-
segment: ParticipationSegment;
|
|
36
|
-
connections: Record<string, string>;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export type ParticipationTransition =
|
|
40
|
-
| { type: 'session_opened'; snapshot: ParticipationSnapshot }
|
|
41
|
-
| { type: 'session_updated'; snapshot: ParticipationSnapshot }
|
|
42
|
-
| { type: 'segment_changed'; previous?: ParticipationSegment; snapshot: ParticipationSnapshot }
|
|
43
|
-
| { type: 'segment_updated'; snapshot: ParticipationSnapshot }
|
|
44
|
-
| { type: 'connection_changed'; sourceId: string; previous?: string; snapshot: ParticipationSnapshot }
|
|
45
|
-
| { type: 'session_closed'; snapshot: ParticipationSnapshot };
|
|
46
|
-
|
|
47
|
-
export interface ParticipationManagerOptions {
|
|
48
|
-
now?: () => Date;
|
|
49
|
-
createId?: (prefix: 'session' | 'segment') => string;
|
|
50
|
-
initial?: ParticipationSnapshot;
|
|
51
|
-
onTransition?: (transition: ParticipationTransition) => void;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export interface OpenSessionInput {
|
|
55
|
-
adapterId: string;
|
|
56
|
-
accountId: string;
|
|
57
|
-
resumeKey?: string;
|
|
58
|
-
mode?: string;
|
|
59
|
-
initialSegment?: {
|
|
60
|
-
kind: string;
|
|
61
|
-
identity?: string;
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function clone<T>(value: T): T {
|
|
66
|
-
return structuredClone(value);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Owns logical participation scope. A network reconnect only changes a
|
|
71
|
-
* connection id; queue/lobby/game transitions change the segment; explicitly
|
|
72
|
-
* leaving and joining again changes the session.
|
|
73
|
-
*/
|
|
74
|
-
export class ParticipationManager {
|
|
75
|
-
private readonly now: () => Date;
|
|
76
|
-
private readonly createId: (prefix: 'session' | 'segment') => string;
|
|
77
|
-
private readonly onTransition?: (transition: ParticipationTransition) => void;
|
|
78
|
-
private currentValue: ParticipationSnapshot | null;
|
|
79
|
-
|
|
80
|
-
constructor(options: ParticipationManagerOptions = {}) {
|
|
81
|
-
this.now = options.now ?? (() => new Date());
|
|
82
|
-
this.createId = options.createId ?? ((prefix) => `${prefix}_${randomUUID()}`);
|
|
83
|
-
this.onTransition = options.onTransition;
|
|
84
|
-
this.currentValue = options.initial ? clone(options.initial) : null;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
get active(): boolean {
|
|
88
|
-
return !!this.currentValue && !this.currentValue.session.endedAt;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
snapshot(): ParticipationSnapshot | null {
|
|
92
|
-
return this.currentValue ? clone(this.currentValue) : null;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
requireSnapshot(): ParticipationSnapshot {
|
|
96
|
-
const snapshot = this.snapshot();
|
|
97
|
-
if (!snapshot) throw new Error('No active participation session.');
|
|
98
|
-
return snapshot;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
scope(): ScopeRef {
|
|
102
|
-
const snapshot = this.requireSnapshot();
|
|
103
|
-
return {
|
|
104
|
-
adapterId: snapshot.session.adapterId,
|
|
105
|
-
accountId: snapshot.session.accountId,
|
|
106
|
-
sessionId: snapshot.session.id,
|
|
107
|
-
segmentId: snapshot.segment.id,
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
openSession(input: OpenSessionInput): ParticipationSnapshot {
|
|
112
|
-
const current = this.currentValue;
|
|
113
|
-
if (
|
|
114
|
-
current
|
|
115
|
-
&& !current.session.endedAt
|
|
116
|
-
&& current.session.adapterId === input.adapterId
|
|
117
|
-
&& current.session.accountId === input.accountId
|
|
118
|
-
&& current.session.resumeKey === input.resumeKey
|
|
119
|
-
) {
|
|
120
|
-
if (input.mode) this.updateMode(input.mode);
|
|
121
|
-
if (input.initialSegment) {
|
|
122
|
-
return this.activateSegment(input.initialSegment.kind, input.initialSegment.identity);
|
|
123
|
-
}
|
|
124
|
-
return this.requireSnapshot();
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (current && !current.session.endedAt) this.closeSession('replaced');
|
|
128
|
-
|
|
129
|
-
const startedAt = this.now().toISOString();
|
|
130
|
-
const session: ParticipationSession = {
|
|
131
|
-
id: this.createId('session'),
|
|
132
|
-
adapterId: input.adapterId,
|
|
133
|
-
accountId: input.accountId,
|
|
134
|
-
resumeKey: input.resumeKey,
|
|
135
|
-
mode: input.mode,
|
|
136
|
-
startedAt,
|
|
137
|
-
};
|
|
138
|
-
const initial = input.initialSegment ?? { kind: 'unknown' };
|
|
139
|
-
const segment: ParticipationSegment = {
|
|
140
|
-
id: this.createId('segment'),
|
|
141
|
-
sessionId: session.id,
|
|
142
|
-
kind: initial.kind,
|
|
143
|
-
identity: initial.identity,
|
|
144
|
-
ordinal: 1,
|
|
145
|
-
startedAt,
|
|
146
|
-
};
|
|
147
|
-
this.currentValue = { session, segment, connections: {} };
|
|
148
|
-
const snapshot = this.requireSnapshot();
|
|
149
|
-
this.onTransition?.({ type: 'session_opened', snapshot });
|
|
150
|
-
return snapshot;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
activateSegment(kind: string, identity?: string): ParticipationSnapshot {
|
|
154
|
-
if (!this.currentValue || this.currentValue.session.endedAt) {
|
|
155
|
-
throw new Error('Cannot activate a segment without an active session.');
|
|
156
|
-
}
|
|
157
|
-
const current = this.currentValue.segment;
|
|
158
|
-
if (canReuseParticipationSegment(current, kind, identity)) {
|
|
159
|
-
// An omitted identity means "keep the currently known identity". This
|
|
160
|
-
// matters for room reconnect loops, where a caller may only know that it
|
|
161
|
-
// is returning to the lobby while the durable room code is already set.
|
|
162
|
-
if (identity === undefined || current.identity === identity) {
|
|
163
|
-
return this.requireSnapshot();
|
|
164
|
-
}
|
|
165
|
-
// Learning an authoritative identity does not create a new logical
|
|
166
|
-
// segment. Persist it in place so early queue/lobby events remain in the
|
|
167
|
-
// same segment and no artificial backlog boundary is introduced.
|
|
168
|
-
if (current.identity === undefined) {
|
|
169
|
-
current.identity = identity;
|
|
170
|
-
const snapshot = this.requireSnapshot();
|
|
171
|
-
this.onTransition?.({ type: 'segment_updated', snapshot });
|
|
172
|
-
return snapshot;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
const changedAt = this.now().toISOString();
|
|
177
|
-
const previous = { ...current, endedAt: current.endedAt ?? changedAt };
|
|
178
|
-
const segment: ParticipationSegment = {
|
|
179
|
-
id: this.createId('segment'),
|
|
180
|
-
sessionId: this.currentValue.session.id,
|
|
181
|
-
kind,
|
|
182
|
-
identity,
|
|
183
|
-
ordinal: current.ordinal + 1,
|
|
184
|
-
startedAt: changedAt,
|
|
185
|
-
};
|
|
186
|
-
this.currentValue.segment = segment;
|
|
187
|
-
const snapshot = this.requireSnapshot();
|
|
188
|
-
this.onTransition?.({ type: 'segment_changed', previous, snapshot });
|
|
189
|
-
return snapshot;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Adopt the first authoritative backend participation identity.
|
|
194
|
-
*
|
|
195
|
-
* A queue session starts before a game id or room code necessarily exists.
|
|
196
|
-
* Once learned, that identity may be persisted exactly once. Replacing it
|
|
197
|
-
* with a different identity would make a later owner restart resume the
|
|
198
|
-
* wrong participation, so conflicts are rejected.
|
|
199
|
-
*/
|
|
200
|
-
adoptResumeKey(resumeKey: string): ParticipationSnapshot {
|
|
201
|
-
if (!this.currentValue || this.currentValue.session.endedAt) {
|
|
202
|
-
throw new Error('Cannot adopt a resume key without an active session.');
|
|
203
|
-
}
|
|
204
|
-
if (!resumeKey) throw new Error('resumeKey is required.');
|
|
205
|
-
const current = this.currentValue.session.resumeKey;
|
|
206
|
-
if (current === resumeKey) return this.requireSnapshot();
|
|
207
|
-
if (current !== undefined) {
|
|
208
|
-
throw new Error(`Cannot replace participation resume key ${current} with ${resumeKey}.`);
|
|
209
|
-
}
|
|
210
|
-
this.currentValue.session.resumeKey = resumeKey;
|
|
211
|
-
const snapshot = this.requireSnapshot();
|
|
212
|
-
this.onTransition?.({ type: 'session_updated', snapshot });
|
|
213
|
-
return snapshot;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
updateMode(mode: string): ParticipationSnapshot {
|
|
217
|
-
if (!this.currentValue || this.currentValue.session.endedAt) {
|
|
218
|
-
throw new Error('Cannot update mode without an active session.');
|
|
219
|
-
}
|
|
220
|
-
if (!mode) throw new Error('mode is required.');
|
|
221
|
-
if (this.currentValue.session.mode === mode) return this.requireSnapshot();
|
|
222
|
-
this.currentValue.session.mode = mode;
|
|
223
|
-
const snapshot = this.requireSnapshot();
|
|
224
|
-
this.onTransition?.({ type: 'session_updated', snapshot });
|
|
225
|
-
return snapshot;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
bindConnection(sourceId: string, connectionId: string): ParticipationSnapshot {
|
|
229
|
-
if (!this.currentValue || this.currentValue.session.endedAt) {
|
|
230
|
-
throw new Error('Cannot bind a connection without an active session.');
|
|
231
|
-
}
|
|
232
|
-
const previous = this.currentValue.connections[sourceId];
|
|
233
|
-
if (previous === connectionId) return this.requireSnapshot();
|
|
234
|
-
this.currentValue.connections[sourceId] = connectionId;
|
|
235
|
-
const snapshot = this.requireSnapshot();
|
|
236
|
-
this.onTransition?.({ type: 'connection_changed', sourceId, previous, snapshot });
|
|
237
|
-
return snapshot;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
connectionId(sourceId: string): string | undefined {
|
|
241
|
-
return this.currentValue?.connections[sourceId];
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
closeSession(_reason?: string): ParticipationSnapshot | null {
|
|
245
|
-
if (!this.currentValue || this.currentValue.session.endedAt) return this.snapshot();
|
|
246
|
-
const endedAt = this.now().toISOString();
|
|
247
|
-
this.currentValue.segment.endedAt ??= endedAt;
|
|
248
|
-
this.currentValue.session.endedAt = endedAt;
|
|
249
|
-
const snapshot = this.requireSnapshot();
|
|
250
|
-
this.onTransition?.({ type: 'session_closed', snapshot });
|
|
251
|
-
return snapshot;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
1
|
+
// Compatibility facade; implementation lives in the game-neutral runtime package.
|
|
2
|
+
export * from '@myclaw163/game-agent-runtime/framework/participation-manager.js';
|
|
@@ -1,77 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { SqliteEventRepository } from '../events/sqlite-event-repository.js';
|
|
4
|
-
import { SqliteRuntimeInputJournal } from '../plugins/runtime-input-journal.js';
|
|
5
|
-
import { SqliteStateHistoryRepository } from '../state/history/sqlite-state-history-repository.js';
|
|
6
|
-
|
|
7
|
-
interface WorkerPaths {
|
|
8
|
-
eventLedgerPath: string;
|
|
9
|
-
stateHistoryPath: string;
|
|
10
|
-
inputJournalPath: string;
|
|
11
|
-
diagnostics?: boolean;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
interface PersistenceOperation {
|
|
15
|
-
store: 'events' | 'history' | 'input';
|
|
16
|
-
method: string;
|
|
17
|
-
args: unknown[];
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const paths = workerData as WorkerPaths;
|
|
21
|
-
const opened = SqliteEventRepository.open(paths.eventLedgerPath);
|
|
22
|
-
if (!opened.ok) throw new Error(`${opened.unavailable.code}: ${opened.unavailable.message}`);
|
|
23
|
-
const stores = {
|
|
24
|
-
events: opened.repository,
|
|
25
|
-
history: new SqliteStateHistoryRepository(paths.stateHistoryPath),
|
|
26
|
-
input: new SqliteRuntimeInputJournal(paths.inputJournalPath),
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
function reportMemory(): void {
|
|
30
|
-
if (!paths.diagnostics) return;
|
|
31
|
-
const memory = process.memoryUsage();
|
|
32
|
-
parentPort?.postMessage({ type: 'diagnostics', memory: {
|
|
33
|
-
sampled_at_ms: Date.now(), thread_id: threadId,
|
|
34
|
-
heap_used_bytes: memory.heapUsed, heap_total_bytes: memory.heapTotal,
|
|
35
|
-
external_bytes: memory.external, array_buffers_bytes: memory.arrayBuffers,
|
|
36
|
-
heap_size_limit_bytes: getHeapStatistics().heap_size_limit,
|
|
37
|
-
// RSS is process-wide; never present it as this Worker's private memory.
|
|
38
|
-
} });
|
|
39
|
-
}
|
|
40
|
-
reportMemory();
|
|
41
|
-
const memoryTimer = paths.diagnostics ? setInterval(reportMemory, 5_000) : undefined;
|
|
42
|
-
memoryTimer?.unref();
|
|
43
|
-
|
|
44
|
-
parentPort?.on('message', (message: {
|
|
45
|
-
type: 'batch' | 'barrier' | 'close';
|
|
46
|
-
id: number;
|
|
47
|
-
operations?: PersistenceOperation[];
|
|
48
|
-
}) => {
|
|
49
|
-
try {
|
|
50
|
-
if (message.type === 'batch') {
|
|
51
|
-
for (const operation of message.operations ?? []) {
|
|
52
|
-
const store = stores[operation.store] as unknown as Record<string, (...args: unknown[]) => unknown>;
|
|
53
|
-
const method = store[operation.method];
|
|
54
|
-
if (typeof method !== 'function') throw new Error(`Unknown persistence operation: ${operation.store}.${operation.method}`);
|
|
55
|
-
method.apply(store, operation.args);
|
|
56
|
-
}
|
|
57
|
-
parentPort?.postMessage({ type: 'ack', id: message.id });
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
if (message.type === 'barrier') {
|
|
61
|
-
parentPort?.postMessage({ type: 'barrier', id: message.id });
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
if (memoryTimer) clearInterval(memoryTimer);
|
|
65
|
-
reportMemory();
|
|
66
|
-
stores.history.close();
|
|
67
|
-
stores.input.close();
|
|
68
|
-
stores.events.close();
|
|
69
|
-
parentPort?.postMessage({ type: 'closed', id: message.id });
|
|
70
|
-
} catch (error) {
|
|
71
|
-
parentPort?.postMessage({
|
|
72
|
-
type: 'error',
|
|
73
|
-
id: message.id,
|
|
74
|
-
error: error instanceof Error ? error.message : String(error),
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
});
|
|
1
|
+
// Compatibility facade; implementation lives in the game-neutral runtime package.
|
|
2
|
+
export * from '@myclaw163/game-agent-runtime/framework/persistence/async-sqlite-persistence-worker.js';
|