@myclaw163/clawclaw-cli 0.7.15 → 0.8.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_HERMES.md +1 -1
- package/QUICKSTART_OPENCLAW.md +3 -11
- package/QUICKSTART_OTHER.md +8 -8
- package/README.md +18 -9
- package/bin/clawclaw-channel.mjs +13 -0
- package/docs/configuration.md +86 -33
- package/docs/custom-modules/README.md +148 -0
- package/docs/custom-modules/api-reference.md +767 -0
- package/docs/custom-modules/behavior.md +318 -0
- package/docs/custom-modules/perception.md +305 -0
- package/docs/custom-modules/trigger.md +292 -0
- package/docs/desktop-event-push.md +6 -4
- package/docs/runtime/README.md +28 -10
- package/docs/runtime/agent-notifications.md +48 -29
- package/docs/runtime/behavior-runtime.md +139 -0
- package/docs/runtime/components-and-boundaries.md +89 -173
- package/docs/runtime/events-and-state.md +55 -66
- package/docs/runtime/official-perception-modules.md +17 -9
- package/docs/runtime/owner-lifecycle-and-observability.md +62 -39
- package/docs/runtime/perception-replay.md +6 -6
- package/docs/runtime/short-commands-and-capabilities.md +35 -37
- package/package.json +7 -4
- package/scripts/check-skill-command-surface.mjs +54 -10
- package/scripts/compare-perception-replays.test.ts +10 -10
- package/scripts/compare-perception-replays.ts +14 -10
- package/scripts/replay-perception.ts +3 -3
- package/scripts/watch-perception-events.test.ts +1 -1
- package/skills/clawclaw/SKILL.md +97 -59
- package/skills/clawclaw/references/BEHAVIORS.md +154 -0
- package/skills/clawclaw/references/CHANNELS.md +115 -0
- package/skills/clawclaw/references/CHATTERBOX.md +3 -2
- package/skills/clawclaw/references/COMMANDS.md +108 -58
- package/skills/clawclaw/references/GAME-MECHANICS.md +4 -3
- package/skills/clawclaw/references/HUB.md +27 -23
- package/skills/clawclaw/references/STREAM.md +18 -14
- package/skills/clawclaw/references/TACTICS.md +2 -2
- package/src/behaviors/official-finite/do-task.ts +141 -0
- package/src/behaviors/official-finite/evade.ts +78 -0
- package/src/behaviors/official-finite/find-player.ts +50 -0
- package/src/behaviors/official-finite/follow-player.ts +70 -0
- package/src/behaviors/official-finite/go-to.ts +69 -0
- package/src/behaviors/official-finite/hide-temporarily.ts +77 -0
- package/src/behaviors/official-finite/hold-position.ts +48 -0
- package/src/behaviors/official-finite/hunt-player.ts +47 -0
- package/src/behaviors/official-finite/index.test.ts +202 -0
- package/src/behaviors/official-finite/index.ts +47 -0
- package/src/behaviors/official-finite/inspect-corpse.ts +64 -0
- package/src/behaviors/official-finite/patrol-area.ts +80 -0
- package/src/behaviors/official-finite/report-corpse.ts +73 -0
- package/src/behaviors/official-finite/seek-kill-opportunity.ts +152 -0
- package/src/behaviors/official-finite/shared.ts +27 -0
- package/src/behaviors/official-finite/trigger-alarm.ts +53 -0
- package/src/channel/claude-channel-ipc-server.ts +295 -0
- package/src/channel/claude-channel-ipc.ts +152 -0
- package/src/channel/claude-channel-server.test.ts +202 -0
- package/src/channel/claude-channel-server.ts +221 -0
- package/src/cli.ts +4 -2
- package/src/commands/account.test.ts +17 -17
- package/src/commands/behavior.test.ts +158 -0
- package/src/commands/behavior.ts +443 -0
- package/src/commands/communication.test.ts +12 -0
- package/src/commands/communication.ts +6 -3
- package/src/commands/data.test.ts +1 -1
- package/src/commands/data.ts +1 -1
- package/src/commands/events-canonical-state.test.ts +221 -2
- package/src/commands/events.ts +205 -12
- package/src/commands/game/index.test.ts +172 -3
- package/src/commands/game/index.ts +69 -32
- package/src/commands/hub.test.ts +25 -14
- package/src/commands/hub.ts +15 -19
- package/src/commands/load.test.ts +228 -6
- package/src/commands/load.ts +102 -18
- package/src/commands/memory.ts +1 -1
- package/src/commands/peek.ts +1 -1
- package/src/commands/perception.test.ts +30 -1
- package/src/commands/perception.ts +15 -17
- package/src/commands/trigger.test.ts +140 -0
- package/src/commands/trigger.ts +314 -0
- package/src/lib/account-guidance.test.ts +4 -0
- package/src/lib/account-guidance.ts +1 -1
- package/src/lib/behavior-export.test.ts +282 -0
- package/src/lib/behavior-export.ts +514 -0
- package/src/lib/game-client.test.ts +4 -17
- package/src/lib/game-client.ts +3 -0
- package/src/lib/gameplay-config.test.ts +258 -37
- package/src/lib/gameplay-config.ts +175 -35
- package/src/lib/hub-install.test.ts +19 -41
- package/src/lib/hub-install.ts +10 -38
- package/src/lib/hub-reminder.ts +7 -9
- package/src/lib/init-command.ts +1 -1
- package/src/lib/official-resource-sync.test.ts +199 -0
- package/src/lib/official-resource-sync.ts +221 -0
- package/src/lib/perception-config.test.ts +9 -4
- package/src/lib/perception-config.ts +10 -13
- package/src/lib/perception-export.test.ts +145 -0
- package/src/lib/perception-export.ts +178 -0
- package/src/lib/trigger-config.test.ts +102 -0
- package/src/lib/trigger-config.ts +217 -0
- package/src/lib/user-data.test.ts +23 -6
- package/src/lib/user-data.ts +15 -123
- package/src/pipeline/canonical-event-presentation.test.ts +5 -5
- package/src/pipeline/canonical-event-presentation.ts +13 -5
- package/src/pipeline/event-format.test.ts +106 -3
- package/src/pipeline/event-format.ts +172 -4
- package/src/pipeline/event-hints.test.ts +57 -4
- package/src/pipeline/event-hints.ts +34 -8
- package/src/runtime/canonical-monitor.test.ts +2 -0
- package/src/runtime/canonical-monitor.ts +6 -2
- package/src/runtime/clawclaw/agent-event-policy.test.ts +23 -2
- package/src/runtime/clawclaw/agent-event-policy.ts +12 -5
- package/src/runtime/clawclaw/behavior-decision.test.ts +29 -0
- package/src/runtime/clawclaw/behavior-decision.ts +82 -0
- package/src/runtime/clawclaw/behavior-input-fields.test.ts +175 -0
- package/src/runtime/clawclaw/behavior-input-fields.ts +270 -0
- package/src/runtime/clawclaw/behavior-input.test.ts +151 -0
- package/src/runtime/clawclaw/behavior-input.ts +70 -0
- package/src/runtime/clawclaw/behavior-observation-memory.test.ts +59 -0
- package/src/runtime/clawclaw/behavior-observation-memory.ts +142 -0
- package/src/runtime/clawclaw/behavior-read-api.test.ts +123 -0
- package/src/runtime/clawclaw/behavior-read-api.ts +158 -0
- package/src/runtime/clawclaw/{plugins/perception/visual-state-projector.ts → observation-frame-projector.ts} +44 -25
- package/src/runtime/clawclaw/owner.test.ts +187 -0
- package/src/runtime/clawclaw/owner.ts +431 -69
- package/src/runtime/clawclaw/plugins/action/action-plugin.test.ts +304 -0
- package/src/runtime/clawclaw/plugins/action/action-plugin.ts +286 -41
- package/src/runtime/clawclaw/plugins/action/automated-action-normalization.ts +82 -0
- package/src/runtime/clawclaw/plugins/assembly.test.ts +135 -29
- package/src/runtime/clawclaw/plugins/assembly.ts +54 -28
- package/src/runtime/clawclaw/plugins/behavior/activation-manager.test.ts +180 -0
- package/src/runtime/clawclaw/plugins/behavior/activation-manager.ts +468 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-action.test.ts +40 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-action.ts +30 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-analysis-thread-host.ts +152 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.test.ts +156 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.ts +104 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-manifest.ts +173 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-module-loader.test.ts +182 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-module-loader.ts +223 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-normalization.ts +157 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.test.ts +848 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +1347 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-runner.ts +212 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-source.test.ts +174 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-source.ts +621 -0
- package/src/runtime/clawclaw/plugins/behavior/control-driver.test.ts +195 -0
- package/src/runtime/clawclaw/plugins/behavior/control-driver.ts +180 -0
- package/src/runtime/clawclaw/plugins/behavior/index.ts +11 -0
- package/src/runtime/clawclaw/plugins/behavior/official-goal-behavior.ts +766 -0
- package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.test.ts +283 -0
- package/src/runtime/clawclaw/plugins/{strategy/thread-strategy-executor.ts → behavior/thread-behavior-executor.ts} +109 -121
- package/src/runtime/clawclaw/plugins/behavior-input-source.test.ts +80 -0
- package/src/runtime/clawclaw/plugins/behavior-input-source.ts +247 -0
- package/src/runtime/clawclaw/plugins/{strategy → game-query}/context-capabilities.ts +1 -1
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.test.ts +13 -10
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.ts +26 -14
- package/src/runtime/clawclaw/plugins/game-query/index.ts +1 -0
- package/src/runtime/clawclaw/plugins/index.ts +3 -1
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.test.ts +239 -0
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.ts +100 -0
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-socket.ts +346 -0
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.test.ts +230 -12
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.ts +118 -90
- package/src/runtime/clawclaw/plugins/mailbox/consumer-ids.ts +5 -0
- package/src/runtime/clawclaw/plugins/mailbox/index.ts +3 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-agent-event-batch.test.ts +1 -1
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.ts +4 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.test.ts +87 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.ts +70 -12
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-presentation.ts +94 -11
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.test.ts +1229 -345
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +479 -35
- package/src/runtime/clawclaw/plugins/perception/contracts.ts +4 -3
- package/src/runtime/clawclaw/plugins/perception/index.ts +1 -1
- package/src/runtime/clawclaw/plugins/perception/module-runner.test.ts +23 -13
- package/src/runtime/clawclaw/plugins/perception/module-runner.ts +11 -9
- package/src/runtime/clawclaw/plugins/perception/modules/co-movement-module.ts +39 -38
- package/src/runtime/clawclaw/plugins/perception/modules/corpse-visibility-module.test.ts +92 -0
- package/src/runtime/clawclaw/plugins/perception/modules/corpse-visibility-module.ts +248 -0
- package/src/runtime/clawclaw/plugins/perception/modules/follow-module.ts +39 -37
- package/src/runtime/clawclaw/plugins/perception/modules/index.ts +1 -0
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.test.ts +22 -21
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.ts +15 -9
- package/src/runtime/clawclaw/plugins/perception/modules/parallel-module-test-helpers.ts +3 -3
- package/src/runtime/clawclaw/plugins/perception/modules/relation-stability.test.ts +33 -18
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.test.ts +6 -7
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.ts +22 -16
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.test.ts +6 -7
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.ts +18 -12
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.test.ts +14 -10
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.ts +20 -14
- package/src/runtime/clawclaw/plugins/perception/modules/temporal-evidence.test.ts +5 -6
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.test.ts +5 -6
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.ts +49 -34
- package/src/runtime/clawclaw/plugins/perception/official-module-ids.ts +1 -0
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.test.ts +126 -20
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.ts +24 -3
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-worker.ts +59 -46
- package/src/runtime/clawclaw/plugins/perception/perception-module-loader.ts +27 -5
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.test.ts +381 -49
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +267 -245
- package/src/runtime/clawclaw/plugins/perception/perception-source.test.ts +98 -0
- package/src/runtime/clawclaw/plugins/perception/perception-source.ts +89 -16
- package/src/runtime/clawclaw/plugins/perception/perception-test-fixtures.ts +34 -0
- package/src/runtime/clawclaw/plugins/perception/replay.test.ts +27 -15
- package/src/runtime/clawclaw/plugins/perception/replay.ts +10 -5
- package/src/runtime/clawclaw/plugins/perception/shared/math.ts +1 -107
- package/src/runtime/clawclaw/plugins/perception/spatial-facts.ts +7 -10
- package/src/runtime/clawclaw/plugins/plugin-api-version.ts +112 -0
- package/src/runtime/clawclaw/plugins/runtime-plugins.test.ts +389 -559
- package/src/runtime/clawclaw/plugins/source/player-change-tracker.test.ts +56 -5
- package/src/runtime/clawclaw/plugins/source/player-change-tracker.ts +135 -26
- package/src/runtime/clawclaw/plugins/source/source-plugin.test.ts +249 -16
- package/src/runtime/clawclaw/plugins/source/source-plugin.ts +99 -2
- package/src/runtime/clawclaw/plugins/source/task-change-tracker.test.ts +212 -0
- package/src/runtime/clawclaw/plugins/source/task-change-tracker.ts +259 -0
- package/src/runtime/clawclaw/plugins/trigger/contracts.ts +97 -0
- package/src/runtime/clawclaw/plugins/trigger/index.ts +4 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-module-loader.ts +118 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.test.ts +548 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.ts +655 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-source.test.ts +169 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-source.ts +457 -0
- package/src/runtime/clawclaw/runtime.ts +8 -0
- package/src/runtime/clawclaw/spatial-api.ts +76 -0
- package/src/runtime/clawclaw-event-catalog.test.ts +46 -1
- package/src/runtime/clawclaw-event-catalog.ts +40 -3
- package/src/runtime/framework/capability-registry.test.ts +196 -55
- package/src/runtime/framework/capability-registry.ts +99 -17
- package/src/runtime/framework/contracts.ts +30 -4
- package/src/runtime/framework/control/control-scheduler.ts +13 -5
- package/src/runtime/framework/control/types.ts +11 -4
- package/src/runtime/framework/control-coordinator.test.ts +95 -80
- package/src/runtime/framework/events/event-delivery-service.ts +4 -2
- package/src/runtime/framework/events/event-ingress.test.ts +25 -0
- package/src/runtime/framework/events/event-ingress.ts +2 -1
- package/src/runtime/framework/events/event-repository.ts +24 -2
- package/src/runtime/framework/events/mailbox-event-repositories.test.ts +124 -8
- package/src/runtime/framework/events/memory-event-repository.ts +92 -28
- package/src/runtime/framework/events/sqlite-event-repository.test.ts +604 -2
- package/src/runtime/framework/events/sqlite-event-repository.ts +531 -21
- package/src/runtime/framework/events/types.ts +24 -8
- package/src/runtime/framework/modules/runtime-module-loader-hooks.mjs +38 -1
- package/src/runtime/framework/modules/runtime-module-loader.test.ts +233 -7
- package/src/runtime/framework/modules/runtime-module-loader.ts +69 -4
- package/src/runtime/framework/plugins/contracts.ts +12 -18
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.test.ts +81 -71
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +38 -20
- package/src/runtime/framework/runtime.ts +20 -23
- package/src/runtime/host-monitor-rate-gate.test.ts +73 -0
- package/src/runtime/host-monitor-rate-gate.ts +123 -0
- package/src/runtime/mailbox-host-relay.test.ts +45 -0
- package/src/runtime/mailbox-host-relay.ts +25 -0
- package/src/runtime/observability/factory.ts +4 -4
- package/src/runtime/observability/observability.test.ts +25 -3
- package/src/runtime/observability/reporter.ts +1 -1
- package/src/runtime/observability/types.ts +2 -2
- package/src/runtime/owner-control.ts +10 -2
- package/src/runtime/runtime-logger.test.ts +1 -1
- package/src/runtime/runtime-logger.ts +4 -6
- package/src/runtime/runtime.test.ts +88 -22
- package/src/sdk/action.ts +2 -2
- package/src/sdk/behavior-plugin/index.test.ts +74 -0
- package/src/sdk/behavior-plugin/index.ts +335 -0
- package/src/sdk/behavior-toolkit/corpse-search.ts +87 -0
- package/src/sdk/behavior-toolkit/escape.ts +40 -0
- package/src/sdk/behavior-toolkit/index.ts +7 -0
- package/src/sdk/behavior-toolkit/kill-flow.ts +299 -0
- package/src/sdk/behavior-toolkit/model.ts +263 -0
- package/src/sdk/behavior-toolkit/move-gate.ts +52 -0
- package/src/sdk/behavior-toolkit/patrol.ts +53 -0
- package/src/sdk/behavior-toolkit/search.ts +102 -0
- package/src/sdk/index.test.ts +45 -0
- package/src/sdk/index.ts +67 -73
- package/src/sdk/module-input/index.test.ts +49 -0
- package/src/sdk/module-input/index.ts +229 -0
- package/src/sdk/perception-plugin/index.test.ts +2 -2
- package/src/sdk/perception-plugin/index.ts +51 -142
- package/src/sdk/perception-plugin/math.ts +107 -0
- package/src/sdk/trigger-plugin/index.test.ts +41 -0
- package/src/sdk/trigger-plugin/index.ts +128 -0
- package/src/strategies/avoid-lone.ts +21 -10
- package/src/strategies/avoid-players.ts +26 -16
- package/src/strategies/corpse-patrol.ts +25 -14
- package/src/strategies/crab-sabotage.ts +20 -9
- package/src/strategies/find-player.ts +33 -21
- package/src/strategies/game-utils.ts +1 -1
- package/src/strategies/goals/crab-sabotage-top.test.ts +145 -0
- package/src/strategies/goals/crab-sabotage-top.ts +32 -69
- package/src/strategies/hide.ts +25 -15
- package/src/strategies/kill-frenzy.ts +21 -10
- package/src/strategies/kill-lone.ts +27 -17
- package/src/strategies/kill-target.ts +33 -22
- package/src/strategies/lone-kill-task.ts +20 -9
- package/src/strategies/move-room.ts +29 -18
- package/src/strategies/official-long-behavior.test.ts +89 -0
- package/src/strategies/paradise-fish.ts +29 -18
- package/src/strategies/pathfind/astar-path.test.ts +127 -2
- package/src/strategies/pathfind/astar-path.ts +136 -26
- package/src/strategies/patrol.ts +21 -10
- package/src/strategies/player-targets.test.ts +10 -0
- package/src/strategies/player-targets.ts +13 -0
- package/src/strategies/report-patrol.ts +21 -10
- package/src/strategies/shrimp-memory.ts +30 -19
- package/src/strategies/social-task.ts +34 -23
- package/src/strategies/task-kill-report.ts +33 -22
- package/src/strategies/task-only.test.ts +99 -85
- package/src/strategies/task-only.ts +23 -12
- package/src/strategies/task-report.ts +25 -14
- package/src/strategies/types.ts +8 -8
- package/src/strategies/warrior-memory.ts +29 -19
- package/docs/runtime/strategy-runtime.md +0 -111
- package/docs//350/207/252/345/256/232/344/271/211/346/204/237/347/237/245.md +0 -502
- package/docs//350/207/252/345/256/232/344/271/211/347/255/226/347/225/245.md +0 -395
- package/skills/clawclaw/references/STRATEGIES.md +0 -76
- package/src/commands/strategy.test.ts +0 -209
- package/src/commands/strategy.ts +0 -242
- package/src/lib/strategy-export.test.ts +0 -406
- package/src/lib/strategy-export.ts +0 -381
- package/src/runtime/clawclaw/plugins/perception/official-modules.ts +0 -31
- package/src/runtime/clawclaw/plugins/strategy/examples/task-only-strategy.test.ts +0 -177
- package/src/runtime/clawclaw/plugins/strategy/examples/task-only-strategy.ts +0 -191
- package/src/runtime/clawclaw/plugins/strategy/index.ts +0 -8
- package/src/runtime/clawclaw/plugins/strategy/local-strategy-executor.ts +0 -412
- package/src/runtime/clawclaw/plugins/strategy/strategy-action.ts +0 -80
- package/src/runtime/clawclaw/plugins/strategy/strategy-analysis-thread-host.ts +0 -183
- package/src/runtime/clawclaw/plugins/strategy/strategy-executor-types.ts +0 -29
- package/src/runtime/clawclaw/plugins/strategy/strategy-executor.test.ts +0 -693
- package/src/runtime/clawclaw/plugins/strategy/strategy-game-api.ts +0 -57
- package/src/runtime/clawclaw/plugins/strategy/strategy-module-loader.ts +0 -168
- package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.test.ts +0 -1994
- package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.ts +0 -1579
- package/src/runtime/clawclaw/plugins/strategy/strategy-source.test.ts +0 -153
- package/src/runtime/clawclaw/plugins/strategy/strategy-source.ts +0 -443
- package/src/sdk/strategy-plugin/index.test.ts +0 -958
- package/src/sdk/strategy-plugin/index.ts +0 -1350
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ClawclawBehavior,
|
|
3
|
+
type BehaviorInput,
|
|
4
|
+
type BehaviorNormalizationApi,
|
|
5
|
+
type BehaviorReadApi,
|
|
6
|
+
type ClawclawBehaviorManifest,
|
|
7
|
+
} from '../../sdk/behavior-plugin/index.js';
|
|
8
|
+
import {
|
|
9
|
+
AuthoritativeKillFlow,
|
|
10
|
+
behaviorCrabTeammatesKnown,
|
|
11
|
+
behaviorDistance,
|
|
12
|
+
behaviorHasKillUse,
|
|
13
|
+
behaviorPatrolTargets,
|
|
14
|
+
behaviorRoleCanKill,
|
|
15
|
+
behaviorRoleCanSelectKillTarget,
|
|
16
|
+
behaviorSelf,
|
|
17
|
+
behaviorTeammateSeats,
|
|
18
|
+
behaviorVisiblePlayers,
|
|
19
|
+
distancesFromSelf,
|
|
20
|
+
findBehaviorRoom,
|
|
21
|
+
SemanticMoveGate,
|
|
22
|
+
type AfterKillBehavior,
|
|
23
|
+
} from '../../sdk/behavior-toolkit/index.js';
|
|
24
|
+
import { finished, running } from './shared.js';
|
|
25
|
+
|
|
26
|
+
type SeekKillInput = Readonly<Record<string, unknown>> & {
|
|
27
|
+
readonly area?: string;
|
|
28
|
+
readonly after_kill: AfterKillBehavior;
|
|
29
|
+
readonly timeout_ms?: number;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function record(value: unknown): Readonly<Record<string, unknown>> {
|
|
33
|
+
return value && typeof value === 'object' && !Array.isArray(value)
|
|
34
|
+
? value as Readonly<Record<string, unknown>>
|
|
35
|
+
: {};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export default class SeekKillOpportunityBehavior extends ClawclawBehavior<SeekKillInput> {
|
|
39
|
+
static override readonly manifest = {
|
|
40
|
+
apiVersion: 3,
|
|
41
|
+
id: 'seek_kill_opportunity',
|
|
42
|
+
name: 'Seek Kill Opportunity',
|
|
43
|
+
description: 'Seek one safe, role-valid kill opportunity on a bounded route.',
|
|
44
|
+
localizations: { 'zh-CN': { name: '寻找击杀机会', description: '沿有限路线寻找一次角色允许的安全击杀机会。' } },
|
|
45
|
+
inputs: [
|
|
46
|
+
{ id: 'area', type: 'string', description: 'Optional known room name to search.' },
|
|
47
|
+
{ id: 'after_kill', type: 'enum', description: 'Follow-up after a confirmed kill.', values: ['escape', 'hold', 'report', 'replan'], default: 'escape' },
|
|
48
|
+
],
|
|
49
|
+
execution: 'serial',
|
|
50
|
+
runtime: { defaultMs: 45_000, deadlineOutcome: 'failed' },
|
|
51
|
+
} as const satisfies ClawclawBehaviorManifest;
|
|
52
|
+
|
|
53
|
+
private readonly kill = new AuthoritativeKillFlow();
|
|
54
|
+
private readonly move = new SemanticMoveGate();
|
|
55
|
+
private readonly learnedTeammates = new Set<number>();
|
|
56
|
+
private teammateRosterKnown = false;
|
|
57
|
+
private routeIndex = 0;
|
|
58
|
+
private targetSeat?: number;
|
|
59
|
+
|
|
60
|
+
override normalize(input: Readonly<Record<string, unknown>>, api: BehaviorNormalizationApi): SeekKillInput {
|
|
61
|
+
const area = api.optionalString(input.area, 'area');
|
|
62
|
+
return {
|
|
63
|
+
after_kill: api.choice(input.after_kill, 'after_kill', ['escape', 'hold', 'report', 'replan'] as const),
|
|
64
|
+
...api.timing(input),
|
|
65
|
+
...(area ? { area } : {}),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
override async decide(input: Readonly<BehaviorInput<SeekKillInput>>, api: BehaviorReadApi) {
|
|
70
|
+
this.learnTeammates(input);
|
|
71
|
+
if (this.targetSeat !== undefined) {
|
|
72
|
+
return this.kill.step(input, api, this.targetSeat, input.input.after_kill);
|
|
73
|
+
}
|
|
74
|
+
const role = input.gameState.you?.role;
|
|
75
|
+
if (!behaviorRoleCanKill(role)) return finished('failed', 'role_cannot_kill', '当前角色没有可用的击杀能力。');
|
|
76
|
+
if (!behaviorHasKillUse(input)) return finished('failed', 'no_kills_remaining', '当前角色已经没有剩余击杀次数。');
|
|
77
|
+
if (!behaviorRoleCanSelectKillTarget(role)) {
|
|
78
|
+
return finished('failed', 'automatic_target_selection_unsafe', '当前角色无法可靠识别全部友军;请由 Agent 用 hunt_player 明确指定目标。');
|
|
79
|
+
}
|
|
80
|
+
if (role === 'crab_generic' && !this.teammateRosterKnown) {
|
|
81
|
+
return finished('failed', 'teammate_roster_unavailable', '尚未取得权威蟹队友名单;拒绝自动选择击杀目标。');
|
|
82
|
+
}
|
|
83
|
+
if (input.input.area) {
|
|
84
|
+
const room = findBehaviorRoom(api, input.input.area);
|
|
85
|
+
if (!room) return finished('failed', 'area_unknown', `找不到机会区域“${input.input.area}”。`);
|
|
86
|
+
const self = behaviorSelf(input);
|
|
87
|
+
if (!self) return finished('failed', 'self_position_unavailable', '当前无法取得自身位置。');
|
|
88
|
+
const distance = await api.navigation.pathDistance(self, room);
|
|
89
|
+
if (input.gameState.you?.room !== room.name && (distance === null || distance > 70)) {
|
|
90
|
+
if (distance === null) return finished('failed', 'area_unreachable', `当前没有到达${room.name}的可用路径。`);
|
|
91
|
+
const summary = `正在前往${room.name}寻找机会。`;
|
|
92
|
+
const action = this.move.propose(input, room, false, summary);
|
|
93
|
+
return running(summary, 'moving', action ? [action] : [], { room: room.name });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const teammates = new Set([...behaviorTeammateSeats(input), ...this.learnedTeammates]);
|
|
98
|
+
const candidates = behaviorVisiblePlayers(input).filter((player) => (
|
|
99
|
+
player.isAlive !== false && !teammates.has(player.seat)
|
|
100
|
+
));
|
|
101
|
+
if (candidates.length === 1) {
|
|
102
|
+
this.targetSeat = candidates[0].seat;
|
|
103
|
+
return this.kill.step(input, api, this.targetSeat, input.input.after_kill);
|
|
104
|
+
}
|
|
105
|
+
const targets = behaviorPatrolTargets(input, api, input.input.area);
|
|
106
|
+
if (targets.length === 0) return finished('failed', 'search_route_unavailable', '当前区域没有可用的机会搜索路线。');
|
|
107
|
+
const distances = await distancesFromSelf(input, api, targets);
|
|
108
|
+
if (!distances) return finished('failed', 'self_position_unavailable', '当前无法取得自身位置。');
|
|
109
|
+
let selected = this.routeIndex % targets.length;
|
|
110
|
+
for (let checked = 0; checked < targets.length; checked += 1) {
|
|
111
|
+
const distance = distances[selected];
|
|
112
|
+
if (distance !== null && distance > 110) break;
|
|
113
|
+
selected = (selected + 1) % targets.length;
|
|
114
|
+
}
|
|
115
|
+
const distance = distances[selected];
|
|
116
|
+
if (distance === null || distance <= 110) {
|
|
117
|
+
if (distances.every((value) => value === null)) {
|
|
118
|
+
return finished('failed', 'search_route_unavailable', '当前没有可达的机会搜索观察点。');
|
|
119
|
+
}
|
|
120
|
+
return running(candidates.length > 1
|
|
121
|
+
? '当前可见多人,不满足低风险击杀窗口;在观察点等待局面变化。'
|
|
122
|
+
: '已到达机会搜索观察点,等待当前视野出现唯一候选。', 'waiting');
|
|
123
|
+
}
|
|
124
|
+
this.routeIndex = selected;
|
|
125
|
+
const summary = candidates.length > 1
|
|
126
|
+
? '当前可见多人,不满足低风险击杀窗口;继续寻找机会。'
|
|
127
|
+
: '当前没有可见候选,正在有限寻找机会。';
|
|
128
|
+
const action = this.move.propose(input, targets[selected], false, summary);
|
|
129
|
+
return running(summary, 'moving', action ? [action] : []);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private learnTeammates(input: Readonly<BehaviorInput>): void {
|
|
133
|
+
if (behaviorCrabTeammatesKnown(input)) {
|
|
134
|
+
this.teammateRosterKnown = true;
|
|
135
|
+
for (const seat of behaviorTeammateSeats(input)) this.learnedTeammates.add(seat);
|
|
136
|
+
}
|
|
137
|
+
for (const event of input.events) {
|
|
138
|
+
const payload = record(event.payload);
|
|
139
|
+
const values = Array.isArray(payload.crab_teammates)
|
|
140
|
+
? payload.crab_teammates
|
|
141
|
+
: Array.isArray(payload.teammates) ? payload.teammates : undefined;
|
|
142
|
+
if (!values && event.type !== 'crab_teammates') continue;
|
|
143
|
+
this.teammateRosterKnown = true;
|
|
144
|
+
for (const value of values ?? []) {
|
|
145
|
+
const seat = record(value).seat;
|
|
146
|
+
if (typeof seat === 'number' && Number.isInteger(seat) && seat >= 1 && seat <= 10) {
|
|
147
|
+
this.learnedTeammates.add(seat);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BehaviorDecision,
|
|
3
|
+
BehaviorStatus,
|
|
4
|
+
GameAction,
|
|
5
|
+
} from '../../sdk/behavior-plugin/index.js';
|
|
6
|
+
|
|
7
|
+
export function running(
|
|
8
|
+
summary: string,
|
|
9
|
+
activity: string,
|
|
10
|
+
actions: readonly Readonly<GameAction>[] = [],
|
|
11
|
+
extra: Omit<BehaviorStatus, 'summary' | 'activity'> = {},
|
|
12
|
+
): BehaviorDecision {
|
|
13
|
+
return {
|
|
14
|
+
status: { summary, activity, ...extra },
|
|
15
|
+
...(actions.length ? { actions } : {}),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export function finished(
|
|
19
|
+
outcome: 'completed' | 'failed',
|
|
20
|
+
reason: string,
|
|
21
|
+
summary: string,
|
|
22
|
+
): BehaviorDecision {
|
|
23
|
+
return {
|
|
24
|
+
status: { summary, activity: 'idle' },
|
|
25
|
+
finish: { outcome, reason, summary },
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ClawclawBehavior,
|
|
3
|
+
type BehaviorInput,
|
|
4
|
+
type BehaviorNormalizationApi,
|
|
5
|
+
type ClawclawBehaviorManifest,
|
|
6
|
+
} from '../../sdk/behavior-plugin/index.js';
|
|
7
|
+
import { behaviorHasEvent } from '../../sdk/behavior-toolkit/index.js';
|
|
8
|
+
import { finished, running } from './shared.js';
|
|
9
|
+
|
|
10
|
+
type AlarmInput = Readonly<Record<string, unknown>> & { readonly timeout_ms?: number };
|
|
11
|
+
|
|
12
|
+
export default class TriggerAlarmBehavior extends ClawclawBehavior<AlarmInput> {
|
|
13
|
+
static override readonly manifest = {
|
|
14
|
+
apiVersion: 3,
|
|
15
|
+
id: 'trigger_alarm',
|
|
16
|
+
name: 'Trigger Alarm',
|
|
17
|
+
description: 'Submit one role-valid shared emergency alarm and await authority.',
|
|
18
|
+
localizations: { 'zh-CN': { name: '触发警报', description: '提交一次角色允许的共享紧急警报并等待权威结果。' } },
|
|
19
|
+
inputs: [],
|
|
20
|
+
execution: 'serial',
|
|
21
|
+
runtime: { defaultMs: 10_000, deadlineOutcome: 'failed' },
|
|
22
|
+
} as const satisfies ClawclawBehaviorManifest;
|
|
23
|
+
|
|
24
|
+
private submitted = false;
|
|
25
|
+
|
|
26
|
+
override normalize(input: Readonly<Record<string, unknown>>, api: BehaviorNormalizationApi): AlarmInput {
|
|
27
|
+
return { ...api.timing(input) };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
override decide(input: Readonly<BehaviorInput<AlarmInput>>) {
|
|
31
|
+
if (this.submitted) {
|
|
32
|
+
if (
|
|
33
|
+
input.gameState.emergency?.active === true
|
|
34
|
+
|| input.gameState.crab_sabotage?.status === 'emergency_active'
|
|
35
|
+
|| behaviorHasEvent(input.events, ['emergency_started'])
|
|
36
|
+
) return finished('completed', 'alarm_triggered', '蟹阵营紧急警报已经触发。');
|
|
37
|
+
return running('等待警报触发的权威状态;不会重复提交。', 'acting');
|
|
38
|
+
}
|
|
39
|
+
if (input.gameState.you?.faction !== 'crab') {
|
|
40
|
+
return finished('failed', 'role_cannot_trigger_alarm', '只有蟹阵营可以触发共享紧急警报。');
|
|
41
|
+
}
|
|
42
|
+
if (
|
|
43
|
+
input.gameState.emergency?.active === true
|
|
44
|
+
|| input.gameState.crab_sabotage?.status === 'emergency_active'
|
|
45
|
+
) return finished('failed', 'emergency_already_active', '当前已经有紧急事件。');
|
|
46
|
+
if (input.gameState.crab_sabotage?.status !== 'ready') {
|
|
47
|
+
return finished('failed', 'alarm_not_ready', `蟹阵营共享警报尚未就绪(${input.gameState.crab_sabotage?.status ?? 'unknown'})。`);
|
|
48
|
+
}
|
|
49
|
+
this.submitted = true;
|
|
50
|
+
const summary = '共享警报已就绪,正在触发并等待权威结果。';
|
|
51
|
+
return running(summary, 'acting', [{ action: 'trigger_alarm', thinking_content: summary }]);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { createServer, type Server, type Socket } from 'node:net';
|
|
3
|
+
import {
|
|
4
|
+
CLAUDE_CHANNEL_IPC_SCHEMA,
|
|
5
|
+
CLAUDE_CHANNEL_MAX_FRAME_CHARACTERS,
|
|
6
|
+
CLAWCLAW_CHANNEL_EVENTS_SCHEMA,
|
|
7
|
+
createClaudeChannelSocketPath,
|
|
8
|
+
publishClaudeChannelEndpoint,
|
|
9
|
+
type PublishedClaudeChannelEndpoint,
|
|
10
|
+
} from './claude-channel-ipc.js';
|
|
11
|
+
|
|
12
|
+
const MAX_ACCEPTED_DELIVERIES = 256;
|
|
13
|
+
|
|
14
|
+
export interface ClaudeChannelIpcServerOptions {
|
|
15
|
+
deliver(payload: Record<string, unknown>, meta: Record<string, string>): Promise<void>;
|
|
16
|
+
diagnostic?: (name: string, attributes?: Record<string, unknown>) => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ClaudeChannelIpcServerStatus {
|
|
20
|
+
listening: boolean;
|
|
21
|
+
owner_connected: boolean;
|
|
22
|
+
owner_pid: number | null;
|
|
23
|
+
in_flight_delivery_id: string | null;
|
|
24
|
+
accepted_delivery_count: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface ConnectionState {
|
|
28
|
+
socket: Socket;
|
|
29
|
+
buffer: string;
|
|
30
|
+
authenticated: boolean;
|
|
31
|
+
connectionId?: string;
|
|
32
|
+
ownerPid?: number;
|
|
33
|
+
inFlightDeliveryId?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface AcceptedDelivery {
|
|
37
|
+
acceptedAt: string;
|
|
38
|
+
receiptId: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function objectFrame(value: unknown): Record<string, unknown> | null {
|
|
42
|
+
return value && typeof value === 'object' && !Array.isArray(value)
|
|
43
|
+
? value as Record<string, unknown>
|
|
44
|
+
: null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function safeText(value: unknown, maxLength = 512): string | undefined {
|
|
48
|
+
return typeof value === 'string' && value.length > 0 && value.length <= maxLength
|
|
49
|
+
? value
|
|
50
|
+
: undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Receives Owner-pushed Mailbox batches and writes them into Claude's MCP transport. */
|
|
54
|
+
export class ClaudeChannelIpcServer {
|
|
55
|
+
private readonly deliver: ClaudeChannelIpcServerOptions['deliver'];
|
|
56
|
+
private readonly diagnostic?: ClaudeChannelIpcServerOptions['diagnostic'];
|
|
57
|
+
private server?: Server;
|
|
58
|
+
private endpoint?: PublishedClaudeChannelEndpoint;
|
|
59
|
+
private active?: ConnectionState;
|
|
60
|
+
private readonly connections = new Set<ConnectionState>();
|
|
61
|
+
private readonly accepted = new Map<string, AcceptedDelivery>();
|
|
62
|
+
private readonly accepting = new Map<string, Promise<AcceptedDelivery>>();
|
|
63
|
+
private closing = false;
|
|
64
|
+
|
|
65
|
+
constructor(options: ClaudeChannelIpcServerOptions) {
|
|
66
|
+
this.deliver = options.deliver;
|
|
67
|
+
this.diagnostic = options.diagnostic;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async start(): Promise<void> {
|
|
71
|
+
if (this.server) return;
|
|
72
|
+
this.closing = false;
|
|
73
|
+
const socketPath = createClaudeChannelSocketPath();
|
|
74
|
+
const server = createServer((socket) => this.onConnection(socket));
|
|
75
|
+
this.server = server;
|
|
76
|
+
try {
|
|
77
|
+
await new Promise<void>((resolve, reject) => {
|
|
78
|
+
server.once('error', reject);
|
|
79
|
+
server.listen(socketPath, () => {
|
|
80
|
+
server.off('error', reject);
|
|
81
|
+
resolve();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
this.endpoint = publishClaudeChannelEndpoint(socketPath);
|
|
85
|
+
} catch (error) {
|
|
86
|
+
this.server = undefined;
|
|
87
|
+
if (server.listening) {
|
|
88
|
+
await new Promise<void>((resolve) => server.close(() => resolve()));
|
|
89
|
+
}
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
status(): ClaudeChannelIpcServerStatus {
|
|
95
|
+
return {
|
|
96
|
+
listening: !!this.server?.listening,
|
|
97
|
+
owner_connected: !!this.active?.authenticated,
|
|
98
|
+
owner_pid: this.active?.ownerPid ?? null,
|
|
99
|
+
in_flight_delivery_id: this.active?.inFlightDeliveryId ?? null,
|
|
100
|
+
accepted_delivery_count: this.accepted.size,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async close(): Promise<void> {
|
|
105
|
+
if (this.closing) return;
|
|
106
|
+
this.closing = true;
|
|
107
|
+
for (const connection of this.connections) connection.socket.destroy();
|
|
108
|
+
this.connections.clear();
|
|
109
|
+
this.active = undefined;
|
|
110
|
+
const server = this.server;
|
|
111
|
+
this.server = undefined;
|
|
112
|
+
if (server) {
|
|
113
|
+
await new Promise<void>((resolve) => server.close(() => resolve()));
|
|
114
|
+
}
|
|
115
|
+
this.endpoint?.release();
|
|
116
|
+
this.endpoint = undefined;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private onConnection(socket: Socket): void {
|
|
120
|
+
const connection: ConnectionState = {
|
|
121
|
+
socket,
|
|
122
|
+
buffer: '',
|
|
123
|
+
authenticated: false,
|
|
124
|
+
};
|
|
125
|
+
this.connections.add(connection);
|
|
126
|
+
socket.setEncoding('utf8');
|
|
127
|
+
socket.setNoDelay(true);
|
|
128
|
+
socket.on('error', () => {});
|
|
129
|
+
socket.on('data', (chunk: string) => this.onData(connection, chunk));
|
|
130
|
+
socket.once('close', () => {
|
|
131
|
+
this.connections.delete(connection);
|
|
132
|
+
if (this.active === connection) this.active = undefined;
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private onData(connection: ConnectionState, chunk: string): void {
|
|
137
|
+
if (this.closing || connection.socket.destroyed) return;
|
|
138
|
+
connection.buffer += chunk;
|
|
139
|
+
if (connection.buffer.length > CLAUDE_CHANNEL_MAX_FRAME_CHARACTERS) {
|
|
140
|
+
connection.socket.destroy(new Error('claude_channel_ipc_frame_too_large'));
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
for (;;) {
|
|
144
|
+
const newline = connection.buffer.indexOf('\n');
|
|
145
|
+
if (newline < 0) return;
|
|
146
|
+
const line = connection.buffer.slice(0, newline).trim();
|
|
147
|
+
connection.buffer = connection.buffer.slice(newline + 1);
|
|
148
|
+
if (!line) continue;
|
|
149
|
+
let frame: Record<string, unknown> | null;
|
|
150
|
+
try {
|
|
151
|
+
frame = objectFrame(JSON.parse(line));
|
|
152
|
+
} catch {
|
|
153
|
+
frame = null;
|
|
154
|
+
}
|
|
155
|
+
if (!frame) {
|
|
156
|
+
connection.socket.destroy(new Error('claude_channel_ipc_invalid_json'));
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (!connection.authenticated) {
|
|
160
|
+
this.handleHello(connection, frame);
|
|
161
|
+
if (!connection.authenticated) return;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
void this.handleBatch(connection, frame).catch((error) => {
|
|
165
|
+
this.diagnostic?.('channel.ipc.delivery_failed', {
|
|
166
|
+
error: error instanceof Error ? error.message : String(error),
|
|
167
|
+
});
|
|
168
|
+
connection.socket.destroy();
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private handleHello(connection: ConnectionState, frame: Record<string, unknown>): void {
|
|
174
|
+
const endpoint = this.endpoint?.descriptor;
|
|
175
|
+
const connectionId = safeText(frame.connection_id);
|
|
176
|
+
const ownerPid = Number(frame.owner_pid);
|
|
177
|
+
if (
|
|
178
|
+
!endpoint
|
|
179
|
+
|| frame.schema !== CLAUDE_CHANNEL_IPC_SCHEMA
|
|
180
|
+
|| frame.type !== 'hello'
|
|
181
|
+
|| frame.token !== endpoint.token
|
|
182
|
+
|| !connectionId
|
|
183
|
+
|| !Number.isSafeInteger(ownerPid)
|
|
184
|
+
|| ownerPid <= 0
|
|
185
|
+
) {
|
|
186
|
+
connection.socket.destroy(new Error('claude_channel_ipc_hello_rejected'));
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
const previous = this.active;
|
|
190
|
+
if (previous && previous !== connection) previous.socket.destroy();
|
|
191
|
+
connection.authenticated = true;
|
|
192
|
+
connection.connectionId = connectionId;
|
|
193
|
+
connection.ownerPid = ownerPid;
|
|
194
|
+
this.active = connection;
|
|
195
|
+
this.write(connection.socket, {
|
|
196
|
+
schema: CLAUDE_CHANNEL_IPC_SCHEMA,
|
|
197
|
+
type: 'ready',
|
|
198
|
+
connection_id: connectionId,
|
|
199
|
+
});
|
|
200
|
+
this.diagnostic?.('channel.ipc.owner_connected', { owner_pid: ownerPid });
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private async handleBatch(
|
|
204
|
+
connection: ConnectionState,
|
|
205
|
+
frame: Record<string, unknown>,
|
|
206
|
+
): Promise<void> {
|
|
207
|
+
const connectionId = connection.connectionId;
|
|
208
|
+
const deliveryId = safeText(frame.delivery_id, 1_024);
|
|
209
|
+
const seq = Number(frame.seq);
|
|
210
|
+
const payload = objectFrame(frame.payload);
|
|
211
|
+
if (
|
|
212
|
+
this.active !== connection
|
|
213
|
+
|| frame.schema !== CLAUDE_CHANNEL_IPC_SCHEMA
|
|
214
|
+
|| frame.type !== 'mailbox_batch'
|
|
215
|
+
|| frame.connection_id !== connectionId
|
|
216
|
+
|| !deliveryId
|
|
217
|
+
|| !Number.isSafeInteger(seq)
|
|
218
|
+
|| seq <= 0
|
|
219
|
+
|| !payload
|
|
220
|
+
|| payload.schema !== CLAWCLAW_CHANNEL_EVENTS_SCHEMA
|
|
221
|
+
|| !Array.isArray(payload.events)
|
|
222
|
+
|| Object.keys(payload).some((key) => key !== 'schema' && key !== 'events')
|
|
223
|
+
|| connection.inFlightDeliveryId
|
|
224
|
+
) {
|
|
225
|
+
connection.socket.destroy(new Error('claude_channel_ipc_batch_rejected'));
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
connection.inFlightDeliveryId = deliveryId;
|
|
229
|
+
try {
|
|
230
|
+
const acceptance = this.accepted.get(deliveryId)
|
|
231
|
+
?? await this.acceptOnce(deliveryId, payload, connection.ownerPid!);
|
|
232
|
+
if (this.active === connection && !connection.socket.destroyed) {
|
|
233
|
+
this.writeReceipt(connection, seq, deliveryId, acceptance);
|
|
234
|
+
}
|
|
235
|
+
} finally {
|
|
236
|
+
if (connection.inFlightDeliveryId === deliveryId) {
|
|
237
|
+
connection.inFlightDeliveryId = undefined;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private acceptOnce(
|
|
243
|
+
deliveryId: string,
|
|
244
|
+
payload: Record<string, unknown>,
|
|
245
|
+
ownerPid: number,
|
|
246
|
+
): Promise<AcceptedDelivery> {
|
|
247
|
+
const current = this.accepting.get(deliveryId);
|
|
248
|
+
if (current) return current;
|
|
249
|
+
const operation = (async () => {
|
|
250
|
+
await this.deliver(payload, {
|
|
251
|
+
kind: 'game_events',
|
|
252
|
+
schema: CLAWCLAW_CHANNEL_EVENTS_SCHEMA,
|
|
253
|
+
event_count: String((payload.events as unknown[]).length),
|
|
254
|
+
owner_pid: String(ownerPid),
|
|
255
|
+
});
|
|
256
|
+
const acceptance: AcceptedDelivery = {
|
|
257
|
+
acceptedAt: new Date().toISOString(),
|
|
258
|
+
receiptId: randomUUID(),
|
|
259
|
+
};
|
|
260
|
+
this.accepted.set(deliveryId, acceptance);
|
|
261
|
+
while (this.accepted.size > MAX_ACCEPTED_DELIVERIES) {
|
|
262
|
+
const oldest = this.accepted.keys().next().value as string | undefined;
|
|
263
|
+
if (!oldest) break;
|
|
264
|
+
this.accepted.delete(oldest);
|
|
265
|
+
}
|
|
266
|
+
return acceptance;
|
|
267
|
+
})();
|
|
268
|
+
this.accepting.set(deliveryId, operation);
|
|
269
|
+
void operation.finally(() => {
|
|
270
|
+
if (this.accepting.get(deliveryId) === operation) this.accepting.delete(deliveryId);
|
|
271
|
+
}).catch(() => {});
|
|
272
|
+
return operation;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private writeReceipt(
|
|
276
|
+
connection: ConnectionState,
|
|
277
|
+
seq: number,
|
|
278
|
+
deliveryId: string,
|
|
279
|
+
acceptance: AcceptedDelivery,
|
|
280
|
+
): void {
|
|
281
|
+
this.write(connection.socket, {
|
|
282
|
+
schema: CLAUDE_CHANNEL_IPC_SCHEMA,
|
|
283
|
+
type: 'receipt',
|
|
284
|
+
connection_id: connection.connectionId,
|
|
285
|
+
seq,
|
|
286
|
+
delivery_id: deliveryId,
|
|
287
|
+
accepted_at: acceptance.acceptedAt,
|
|
288
|
+
receipt_id: acceptance.receiptId,
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
private write(socket: Socket, frame: Record<string, unknown>): void {
|
|
293
|
+
socket.write(`${JSON.stringify(frame)}\n`);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { createHash, randomBytes, randomUUID } from 'node:crypto';
|
|
2
|
+
import {
|
|
3
|
+
chmodSync,
|
|
4
|
+
existsSync,
|
|
5
|
+
mkdirSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
renameSync,
|
|
8
|
+
unlinkSync,
|
|
9
|
+
writeFileSync,
|
|
10
|
+
} from 'node:fs';
|
|
11
|
+
import { tmpdir } from 'node:os';
|
|
12
|
+
import { dirname, join } from 'node:path';
|
|
13
|
+
import { getWorkspaceDir } from '../lib/init-command.js';
|
|
14
|
+
import { ensureUserDataLayout, getRuntimeDir } from '../lib/user-data.js';
|
|
15
|
+
|
|
16
|
+
export const CLAUDE_CHANNEL_IPC_SCHEMA = 'ccl.claude-channel-ipc.v1';
|
|
17
|
+
export const CLAUDE_CHANNEL_ENDPOINT_SCHEMA = 'ccl.claude-channel-endpoint.v1';
|
|
18
|
+
export const CLAWCLAW_CHANNEL_EVENTS_SCHEMA = 'clawclaw.channel-events.v2';
|
|
19
|
+
export const CLAUDE_CHANNEL_ENDPOINT_FILE = 'claude-channel-endpoint.json';
|
|
20
|
+
export const CLAUDE_CHANNEL_MAX_FRAME_CHARACTERS = 16 * 1024 * 1024;
|
|
21
|
+
|
|
22
|
+
export interface ClaudeChannelEndpointDescriptor {
|
|
23
|
+
schema: typeof CLAUDE_CHANNEL_ENDPOINT_SCHEMA;
|
|
24
|
+
instance_id: string;
|
|
25
|
+
pid: number;
|
|
26
|
+
socket_path: string;
|
|
27
|
+
token: string;
|
|
28
|
+
started_at: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface PublishedClaudeChannelEndpoint {
|
|
32
|
+
descriptor: ClaudeChannelEndpointDescriptor;
|
|
33
|
+
path: string;
|
|
34
|
+
release(): void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function processAlive(pid: number): boolean {
|
|
38
|
+
if (!Number.isSafeInteger(pid) || pid <= 0) return false;
|
|
39
|
+
try {
|
|
40
|
+
process.kill(pid, 0);
|
|
41
|
+
return true;
|
|
42
|
+
} catch (error: any) {
|
|
43
|
+
return error?.code === 'EPERM';
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function claudeChannelEndpointPath(workspaceDir = getWorkspaceDir()): string {
|
|
48
|
+
return join(getRuntimeDir(workspaceDir), CLAUDE_CHANNEL_ENDPOINT_FILE);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function createClaudeChannelSocketPath(
|
|
52
|
+
workspaceDir = getWorkspaceDir(),
|
|
53
|
+
pid = process.pid,
|
|
54
|
+
): string {
|
|
55
|
+
const hash = createHash('sha1').update(workspaceDir).digest('hex').slice(0, 12);
|
|
56
|
+
const nonce = randomBytes(6).toString('hex');
|
|
57
|
+
if (process.platform === 'win32') {
|
|
58
|
+
return `\\\\.\\pipe\\clawclaw-channel-${hash}-${pid}-${nonce}`;
|
|
59
|
+
}
|
|
60
|
+
const userId = typeof process.getuid === 'function' ? String(process.getuid()) : hash;
|
|
61
|
+
const dir = join(tmpdir(), `clawclaw-channel-${userId}`);
|
|
62
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
63
|
+
try { chmodSync(dir, 0o700); } catch {}
|
|
64
|
+
return join(dir, `${hash}-${pid}-${nonce}.sock`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function validDescriptor(value: unknown): value is ClaudeChannelEndpointDescriptor {
|
|
68
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
69
|
+
const descriptor = value as Record<string, unknown>;
|
|
70
|
+
return descriptor.schema === CLAUDE_CHANNEL_ENDPOINT_SCHEMA
|
|
71
|
+
&& typeof descriptor.instance_id === 'string'
|
|
72
|
+
&& descriptor.instance_id.length > 0
|
|
73
|
+
&& Number.isSafeInteger(descriptor.pid)
|
|
74
|
+
&& Number(descriptor.pid) > 0
|
|
75
|
+
&& typeof descriptor.socket_path === 'string'
|
|
76
|
+
&& descriptor.socket_path.length > 0
|
|
77
|
+
&& typeof descriptor.token === 'string'
|
|
78
|
+
&& descriptor.token.length >= 32
|
|
79
|
+
&& typeof descriptor.started_at === 'string';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function readClaudeChannelEndpoint(
|
|
83
|
+
workspaceDir = getWorkspaceDir(),
|
|
84
|
+
): ClaudeChannelEndpointDescriptor | null {
|
|
85
|
+
const path = claudeChannelEndpointPath(workspaceDir);
|
|
86
|
+
if (!existsSync(path)) return null;
|
|
87
|
+
try {
|
|
88
|
+
const value = JSON.parse(readFileSync(path, 'utf8'));
|
|
89
|
+
if (!validDescriptor(value) || !processAlive(value.pid)) return null;
|
|
90
|
+
return value;
|
|
91
|
+
} catch {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function publishClaudeChannelEndpoint(
|
|
97
|
+
socketPath: string,
|
|
98
|
+
workspaceDir = getWorkspaceDir(),
|
|
99
|
+
): PublishedClaudeChannelEndpoint {
|
|
100
|
+
ensureUserDataLayout(workspaceDir);
|
|
101
|
+
const path = claudeChannelEndpointPath(workspaceDir);
|
|
102
|
+
const existing = readClaudeChannelEndpoint(workspaceDir);
|
|
103
|
+
if (existing && existing.pid !== process.pid) {
|
|
104
|
+
throw new Error(
|
|
105
|
+
`Another ClawClaw Channel bridge is already active (pid ${existing.pid}).`,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
const descriptor: ClaudeChannelEndpointDescriptor = {
|
|
109
|
+
schema: CLAUDE_CHANNEL_ENDPOINT_SCHEMA,
|
|
110
|
+
instance_id: randomUUID(),
|
|
111
|
+
pid: process.pid,
|
|
112
|
+
socket_path: socketPath,
|
|
113
|
+
token: randomBytes(32).toString('hex'),
|
|
114
|
+
started_at: new Date().toISOString(),
|
|
115
|
+
};
|
|
116
|
+
const tempPath = `${path}.${process.pid}.tmp`;
|
|
117
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
118
|
+
writeFileSync(tempPath, JSON.stringify(descriptor), { encoding: 'utf8', mode: 0o600 });
|
|
119
|
+
try { chmodSync(tempPath, 0o600); } catch {}
|
|
120
|
+
try {
|
|
121
|
+
renameSync(tempPath, path);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
try {
|
|
124
|
+
unlinkSync(path);
|
|
125
|
+
renameSync(tempPath, path);
|
|
126
|
+
} catch {
|
|
127
|
+
try { unlinkSync(tempPath); } catch {}
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
let released = false;
|
|
132
|
+
return {
|
|
133
|
+
descriptor,
|
|
134
|
+
path,
|
|
135
|
+
release: () => {
|
|
136
|
+
if (released) return;
|
|
137
|
+
released = true;
|
|
138
|
+
try {
|
|
139
|
+
const current = JSON.parse(readFileSync(path, 'utf8')) as Record<string, unknown>;
|
|
140
|
+
if (
|
|
141
|
+
current.instance_id === descriptor.instance_id
|
|
142
|
+
&& current.pid === descriptor.pid
|
|
143
|
+
) {
|
|
144
|
+
unlinkSync(path);
|
|
145
|
+
}
|
|
146
|
+
} catch {}
|
|
147
|
+
if (process.platform !== 'win32') {
|
|
148
|
+
try { unlinkSync(socketPath); } catch {}
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|