@myclaw163/clawclaw-cli 0.8.4 → 0.8.5
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/README.md +3 -2
- package/docs/configuration.md +44 -26
- package/docs/custom-modules/README.md +7 -10
- package/docs/custom-modules/behavior.md +3 -1
- package/docs/custom-modules/perception.md +1 -1
- package/docs/custom-modules/trigger.md +1 -1
- package/docs/runtime/README.md +4 -4
- package/docs/runtime/official-perception-modules.md +2 -2
- package/docs/runtime/owner-lifecycle-and-observability.md +1 -1
- package/package.json +9 -2
- package/scripts/build-official-hub-behaviors.mjs +136 -0
- package/scripts/build-official-hub-behaviors.test.mjs +178 -0
- package/scripts/enable_ccl_permissions.cjs +4 -2
- package/scripts/enable_ccl_permissions.test.mjs +8 -4
- package/scripts/postinstall.mjs +1 -4
- package/scripts/sync-bundled-skill.mjs +12 -159
- package/scripts/sync-bundled-skill.test.mjs +9 -74
- package/skills/clawclaw/SKILL.md +14 -9
- package/skills/clawclaw/references/BEHAVIORS.md +4 -4
- package/skills/clawclaw/references/COMMANDS.md +32 -16
- package/skills/clawclaw/references/HUB.md +25 -38
- package/src/commands/account.ts +58 -14
- package/src/commands/behavior.ts +116 -38
- package/src/commands/hub.ts +110 -153
- package/src/commands/load.ts +138 -33
- package/src/commands/perception.ts +55 -24
- package/src/commands/persona.ts +66 -20
- package/src/commands/skill.ts +12 -48
- package/src/commands/trigger.ts +68 -40
- package/src/hub-official-behaviors/kill-lone.ts +138 -0
- package/src/hub-official-behaviors/patrol.ts +30 -0
- package/src/hub-official-behaviors/shared.ts +125 -0
- package/src/hub-official-behaviors/task-only.ts +158 -0
- package/src/lib/account-guidance.ts +54 -9
- package/src/lib/agent-workspace-client.ts +135 -0
- package/src/lib/agent-workspace-runtime.ts +154 -0
- package/src/lib/agent-workspace-skills.ts +177 -0
- package/src/lib/agent-workspace-sync.ts +852 -0
- package/src/lib/gameplay-config.ts +29 -15
- package/src/lib/hub-client.ts +252 -34
- package/src/lib/hub-reminder.ts +22 -7
- package/src/lib/init-command.ts +0 -6
- package/src/lib/load-context.ts +12 -6
- package/src/lib/persona.ts +141 -69
- package/src/lib/server-status.ts +78 -0
- package/src/lib/skill-host.ts +160 -0
- package/src/lib/user-data.ts +197 -4
- package/src/runtime/clawclaw/owner.ts +63 -19
- package/src/runtime/clawclaw/plugins/behavior/activation-manager.ts +8 -2
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +2 -2
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +4 -1
- package/src/runtime/clawclaw/plugins/perception/perception-source.ts +11 -5
- package/src/runtime/clawclaw/plugins/perception/replay.ts +0 -2
- package/personas//345/217/243/346/230/257/345/277/203/351/235/236.md +0 -50
- package/personas//345/277/240/345/277/203/350/200/201/344/273/206.md +0 -51
- package/personas//346/227/240/344/272/272/350/256/276.md +0 -1
- package/personas//350/264/253/345/230/264/346/215/237/345/217/213.md +0 -48
- package/personas//350/275/257/350/220/214/350/205/271/351/273/221.md +0 -51
- package/src/behaviors/official-finite/index.test.ts +0 -249
- package/src/channel/claude-channel-server.test.ts +0 -202
- package/src/commands/account.test.ts +0 -570
- package/src/commands/behavior.test.ts +0 -158
- package/src/commands/communication.test.ts +0 -282
- package/src/commands/competition.test.ts +0 -116
- package/src/commands/data.test.ts +0 -89
- package/src/commands/events-canonical-state.test.ts +0 -406
- package/src/commands/events.test.ts +0 -58
- package/src/commands/game/index.test.ts +0 -1251
- package/src/commands/game-map.test.ts +0 -69
- package/src/commands/hub.test.ts +0 -124
- package/src/commands/load.test.ts +0 -440
- package/src/commands/perception.test.ts +0 -75
- package/src/commands/setup/hermes.test.ts +0 -96
- package/src/commands/setup/openclaw.test.ts +0 -124
- package/src/commands/trigger.test.ts +0 -140
- package/src/commands/upgrade.test.ts +0 -82
- package/src/lib/account-guidance.test.ts +0 -36
- package/src/lib/auth.test.ts +0 -151
- package/src/lib/behavior-export.test.ts +0 -326
- package/src/lib/behavior-export.ts +0 -537
- package/src/lib/clawclaw-skill-spectate-guidance.test.ts +0 -21
- package/src/lib/custom-tts.test.ts +0 -387
- package/src/lib/diagnostics-config.test.ts +0 -59
- package/src/lib/game-client-tts.test.ts +0 -151
- package/src/lib/game-client.test.ts +0 -250
- package/src/lib/gameplay-config.test.ts +0 -301
- package/src/lib/host-config-patcher.test.ts +0 -130
- package/src/lib/http-transport.test.ts +0 -124
- package/src/lib/hub-client.test.ts +0 -57
- package/src/lib/hub-install.test.ts +0 -135
- package/src/lib/hub-install.ts +0 -185
- package/src/lib/hub-unzip.test.ts +0 -69
- package/src/lib/init-command.test.ts +0 -103
- package/src/lib/load-context.test.ts +0 -62
- package/src/lib/match-state.test.ts +0 -134
- package/src/lib/official-resource-sync.test.ts +0 -199
- package/src/lib/official-resource-sync.ts +0 -221
- package/src/lib/perception-config.test.ts +0 -143
- package/src/lib/perception-export.test.ts +0 -145
- package/src/lib/perception-export.ts +0 -178
- package/src/lib/persona.test.ts +0 -84
- package/src/lib/room.test.ts +0 -88
- package/src/lib/server-registry.test.ts +0 -64
- package/src/lib/service-status.test.ts +0 -81
- package/src/lib/skill-version.test.ts +0 -48
- package/src/lib/trigger-config.test.ts +0 -102
- package/src/lib/trigger-export.test.ts +0 -80
- package/src/lib/trigger-export.ts +0 -210
- package/src/lib/user-data.test.ts +0 -121
- package/src/lib/workspace-argv.test.ts +0 -49
- package/src/pipeline/canonical-event-presentation.test.ts +0 -257
- package/src/pipeline/event-format.test.ts +0 -879
- package/src/pipeline/event-hints.test.ts +0 -337
- package/src/pipeline/game-sync-event-normalization.test.ts +0 -419
- package/src/pipeline/player-projection.test.ts +0 -247
- package/src/runtime/auto-upgrade.test.ts +0 -66
- package/src/runtime/canonical-event-emitter.test.ts +0 -70
- package/src/runtime/canonical-monitor.test.ts +0 -577
- package/src/runtime/clawclaw/agent-event-policy.test.ts +0 -60
- package/src/runtime/clawclaw/behavior-decision.test.ts +0 -43
- package/src/runtime/clawclaw/behavior-input-fields.test.ts +0 -175
- package/src/runtime/clawclaw/behavior-input.test.ts +0 -173
- package/src/runtime/clawclaw/behavior-observation-memory.test.ts +0 -59
- package/src/runtime/clawclaw/behavior-read-api.test.ts +0 -123
- package/src/runtime/clawclaw/notifications.test.ts +0 -47
- package/src/runtime/clawclaw/owner-lock.test.ts +0 -221
- package/src/runtime/clawclaw/owner.test.ts +0 -203
- package/src/runtime/clawclaw/plugins/action/action-plugin.test.ts +0 -328
- package/src/runtime/clawclaw/plugins/assembly.test.ts +0 -200
- package/src/runtime/clawclaw/plugins/behavior/activation-manager.test.ts +0 -180
- package/src/runtime/clawclaw/plugins/behavior/behavior-action.test.ts +0 -43
- package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.test.ts +0 -156
- package/src/runtime/clawclaw/plugins/behavior/behavior-module-loader.test.ts +0 -182
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.test.ts +0 -1094
- package/src/runtime/clawclaw/plugins/behavior/behavior-source.test.ts +0 -174
- package/src/runtime/clawclaw/plugins/behavior/control-driver.test.ts +0 -195
- package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.test.ts +0 -283
- package/src/runtime/clawclaw/plugins/behavior-input-source.test.ts +0 -80
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.test.ts +0 -267
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.test.ts +0 -226
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.test.ts +0 -239
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.test.ts +0 -407
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-push.integration.test.ts +0 -397
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.test.ts +0 -270
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-agent-event-batch.test.ts +0 -166
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.test.ts +0 -194
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.test.ts +0 -87
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.test.ts +0 -2012
- package/src/runtime/clawclaw/plugins/perception/module-runner.test.ts +0 -214
- package/src/runtime/clawclaw/plugins/perception/modules/co-movement-module.ts +0 -347
- package/src/runtime/clawclaw/plugins/perception/modules/corpse-visibility-module.test.ts +0 -92
- package/src/runtime/clawclaw/plugins/perception/modules/corpse-visibility-module.ts +0 -248
- package/src/runtime/clawclaw/plugins/perception/modules/follow-module.ts +0 -389
- package/src/runtime/clawclaw/plugins/perception/modules/index.ts +0 -8
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.test.ts +0 -342
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.ts +0 -475
- package/src/runtime/clawclaw/plugins/perception/modules/parallel-module-test-helpers.ts +0 -30
- package/src/runtime/clawclaw/plugins/perception/modules/relation-stability.test.ts +0 -304
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.test.ts +0 -179
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.ts +0 -364
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.test.ts +0 -81
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.ts +0 -200
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.test.ts +0 -216
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.ts +0 -284
- package/src/runtime/clawclaw/plugins/perception/modules/temporal-evidence.test.ts +0 -120
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.test.ts +0 -105
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.ts +0 -269
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.test.ts +0 -865
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.test.ts +0 -1878
- package/src/runtime/clawclaw/plugins/perception/perception-source.test.ts +0 -278
- package/src/runtime/clawclaw/plugins/perception/replay.test.ts +0 -77
- package/src/runtime/clawclaw/plugins/runtime-plugins.test.ts +0 -853
- package/src/runtime/clawclaw/plugins/source/game-transport.test.ts +0 -287
- package/src/runtime/clawclaw/plugins/source/player-change-tracker.test.ts +0 -321
- package/src/runtime/clawclaw/plugins/source/room-transport.test.ts +0 -307
- package/src/runtime/clawclaw/plugins/source/source-plugin.test.ts +0 -2646
- package/src/runtime/clawclaw/plugins/source/task-change-tracker.test.ts +0 -212
- package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.test.ts +0 -548
- package/src/runtime/clawclaw/plugins/trigger/trigger-source.test.ts +0 -169
- package/src/runtime/clawclaw/runtime-data-export.test.ts +0 -249
- package/src/runtime/clawclaw-event-catalog.test.ts +0 -128
- package/src/runtime/framework/action-diagnostics.test.ts +0 -81
- package/src/runtime/framework/capability-registry.test.ts +0 -423
- package/src/runtime/framework/control-coordinator.test.ts +0 -675
- package/src/runtime/framework/events/event-delivery-service.test.ts +0 -84
- package/src/runtime/framework/events/event-ingress.test.ts +0 -203
- package/src/runtime/framework/events/mailbox-event-repositories.test.ts +0 -710
- package/src/runtime/framework/events/sqlite-event-repository.test.ts +0 -802
- package/src/runtime/framework/mailbox/coalescer.test.ts +0 -55
- package/src/runtime/framework/mailbox/identity.test.ts +0 -38
- package/src/runtime/framework/mailbox/mailbox-dispatcher.test.ts +0 -513
- package/src/runtime/framework/mailbox/notification-projector.test.ts +0 -77
- package/src/runtime/framework/modules/runtime-module-loader.test.ts +0 -435
- package/src/runtime/framework/participation-manager.test.ts +0 -108
- package/src/runtime/framework/plugins/runtime-input-journal.test.ts +0 -179
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.test.ts +0 -2040
- package/src/runtime/framework/state/durable-agent-state-snapshot.test.ts +0 -144
- package/src/runtime/framework/state/history/sqlite-state-history-repository.test.ts +0 -157
- package/src/runtime/framework/state/history/state-history.test.ts +0 -251
- package/src/runtime/framework/state/state-core.test.ts +0 -197
- package/src/runtime/game-sync-protocol.test.ts +0 -123
- package/src/runtime/host-monitor-rate-gate.test.ts +0 -73
- package/src/runtime/mailbox-host-relay.test.ts +0 -293
- package/src/runtime/observability/observability.test.ts +0 -583
- package/src/runtime/owner-control.test.ts +0 -153
- package/src/runtime/parent-process-watch.test.ts +0 -27
- package/src/runtime/pending-event-handoff.test.ts +0 -29
- package/src/runtime/raw-ws-log.test.ts +0 -102
- package/src/runtime/runtime-logger.test.ts +0 -58
- package/src/runtime/runtime-persistence.test.ts +0 -26
- package/src/runtime/runtime.test.ts +0 -1770
- package/src/runtime/ws-client.test.ts +0 -562
- package/src/sdk/action.test.ts +0 -8
- package/src/sdk/behavior-plugin/index.test.ts +0 -74
- package/src/sdk/index.test.ts +0 -45
- package/src/sdk/module-input/index.test.ts +0 -49
- package/src/sdk/perception-plugin/index.test.ts +0 -22
- package/src/sdk/resource-metadata.test.ts +0 -40
- package/src/sdk/trigger-plugin/index.test.ts +0 -41
- package/src/short-command/classifier.test.ts +0 -107
- package/src/short-command/presenter.test.ts +0 -119
- package/src/short-command/runner.test.ts +0 -78
- package/src/short-command/sensitive-data-filter.test.ts +0 -56
- package/src/short-command/transport-response.test.ts +0 -39
- package/src/strategies/custom-module.test.ts +0 -267
- package/src/strategies/game-utils.test.ts +0 -305
- package/src/strategies/goals/avoid-players-top.test.ts +0 -95
- package/src/strategies/goals/crab-sabotage-top.test.ts +0 -131
- package/src/strategies/goals/kill-lone-top.test.ts +0 -96
- package/src/strategies/goals/kill-target-top.test.ts +0 -65
- package/src/strategies/goals/lone-kill-task-top.test.ts +0 -84
- package/src/strategies/goals/normal-shrimp-top.test.ts +0 -112
- package/src/strategies/goals/paradise-fish-top.test.ts +0 -203
- package/src/strategies/goals/task-only-top.test.ts +0 -88
- package/src/strategies/goals/warrior-shrimp-top.test.ts +0 -138
- package/src/strategies/meeting-gate.test.ts +0 -59
- package/src/strategies/official-long-behavior.test.ts +0 -89
- package/src/strategies/pathfind/astar-path.test.ts +0 -198
- package/src/strategies/pathfind/escape-planner.test.ts +0 -223
- package/src/strategies/player-targets.test.ts +0 -10
- package/src/strategies/task-only.test.ts +0 -138
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ClawclawBehavior,
|
|
3
|
+
type BehaviorDecision,
|
|
4
|
+
type BehaviorInput,
|
|
5
|
+
type BehaviorReadApi,
|
|
6
|
+
type Position,
|
|
7
|
+
type VisiblePlayer,
|
|
8
|
+
} from '@myclaw163/clawclaw-cli/behavior-plugin';
|
|
9
|
+
import { pointDistance, PublicRoomPatrol, selfPosition } from './shared.js';
|
|
10
|
+
|
|
11
|
+
type KillLoneInput = Readonly<Record<string, unknown>> & {
|
|
12
|
+
readonly protectedTargets?: readonly number[];
|
|
13
|
+
readonly huntTargets?: readonly number[];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function seatSet(value: readonly number[] | undefined): ReadonlySet<number> {
|
|
17
|
+
return new Set((value ?? []).filter((seat) => Number.isInteger(seat) && seat >= 1 && seat <= 10));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function playerDistance(self: Readonly<Position>, player: Readonly<VisiblePlayer>): number {
|
|
21
|
+
return Number.isFinite(player.distance) ? player.distance! : pointDistance(self, player);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default class KillLoneStrategy extends ClawclawBehavior<KillLoneInput> {
|
|
25
|
+
static readonly manifest = {
|
|
26
|
+
apiVersion: 3,
|
|
27
|
+
id: 'kill-lone',
|
|
28
|
+
name: '猎独',
|
|
29
|
+
description: '按照房间顺序巡逻。发现唯一可攻击目标时靠近并在安全距离内出刀;huntTargets 可越过人数限制主动追猎,protectedTargets 永不击杀且保护优先。',
|
|
30
|
+
execution: 'serial',
|
|
31
|
+
inputs: [
|
|
32
|
+
{
|
|
33
|
+
id: 'protectedTargets',
|
|
34
|
+
type: 'player_seats',
|
|
35
|
+
description: '禁止击杀的玩家座位号列表;与 huntTargets 重叠时保护优先。',
|
|
36
|
+
default: [],
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
id: 'huntTargets',
|
|
40
|
+
type: 'player_seats',
|
|
41
|
+
description: '主动猎杀的玩家座位号列表;看见后可越过落单人数限制追杀。',
|
|
42
|
+
default: [],
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
} as const;
|
|
46
|
+
|
|
47
|
+
private readonly patrol = new PublicRoomPatrol();
|
|
48
|
+
private chasingSeat?: number;
|
|
49
|
+
private pendingKill?: { seat: number; atMs: number };
|
|
50
|
+
|
|
51
|
+
override async decide(
|
|
52
|
+
input: Readonly<BehaviorInput<KillLoneInput>>,
|
|
53
|
+
api: BehaviorReadApi,
|
|
54
|
+
): Promise<BehaviorDecision> {
|
|
55
|
+
const self = input.gameState.you;
|
|
56
|
+
const selfPoint = selfPosition(input);
|
|
57
|
+
if (!self?.is_alive || !selfPoint) {
|
|
58
|
+
return { status: { activity: 'idle', summary: '等待可行动角色后继续猎独。' } };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const protectedTargets = seatSet(input.input.protectedTargets);
|
|
62
|
+
const huntTargets = seatSet(input.input.huntTargets);
|
|
63
|
+
const teammateSeats = new Set((self.crab_teammates ?? []).map((player) => player.seat));
|
|
64
|
+
const visible = (input.gameState.visible_players ?? [])
|
|
65
|
+
.filter((player) => (
|
|
66
|
+
player.is_alive !== false
|
|
67
|
+
&& player.player.seat !== self.player?.seat
|
|
68
|
+
&& !teammateSeats.has(player.player.seat)
|
|
69
|
+
))
|
|
70
|
+
.sort((left, right) => playerDistance(selfPoint, left) - playerDistance(selfPoint, right));
|
|
71
|
+
|
|
72
|
+
if (this.pendingKill) {
|
|
73
|
+
const known = input.gameState.players?.find((player) => player.seat === this.pendingKill!.seat);
|
|
74
|
+
if (known?.known_status.is_alive === false) this.pendingKill = undefined;
|
|
75
|
+
else if (input.nowMs - this.pendingKill.atMs < 1_200) {
|
|
76
|
+
return {
|
|
77
|
+
status: {
|
|
78
|
+
activity: 'acting',
|
|
79
|
+
targetPlayer: `${this.pendingKill.seat}号`,
|
|
80
|
+
summary: `已对${this.pendingKill.seat}号提交击杀,等待权威状态。`,
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
} else this.pendingKill = undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const hunted = visible.find((player) => (
|
|
87
|
+
huntTargets.has(player.player.seat) && !protectedTargets.has(player.player.seat)
|
|
88
|
+
));
|
|
89
|
+
const lone = visible.length === 1 && !protectedTargets.has(visible[0]!.player.seat)
|
|
90
|
+
? visible[0]
|
|
91
|
+
: undefined;
|
|
92
|
+
const target = hunted ?? lone;
|
|
93
|
+
const hasKill = self.kills_remaining == null || self.kills_remaining > 0;
|
|
94
|
+
if (!target || !hasKill) {
|
|
95
|
+
this.chasingSeat = undefined;
|
|
96
|
+
return this.patrol.decide(input, api, '巡逻寻找落单目标', visible.length === 0);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const seat = target.player.seat;
|
|
100
|
+
const label = `${seat}号${target.player.name}`;
|
|
101
|
+
const distance = playerDistance(selfPoint, target);
|
|
102
|
+
const cooldown = self.kill_cooldown_secs ?? 0;
|
|
103
|
+
if (distance <= 72 && cooldown <= 0) {
|
|
104
|
+
this.pendingKill = { seat, atMs: input.nowMs };
|
|
105
|
+
this.chasingSeat = undefined;
|
|
106
|
+
const summary = `发现目标${label}进入安全击杀距离,正在出刀。`;
|
|
107
|
+
return {
|
|
108
|
+
actions: [{ action: 'kill', target_seat: seat, thinking_content: summary }],
|
|
109
|
+
status: { activity: 'acting', targetPlayer: target.player.name, summary },
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (this.chasingSeat === seat && self.activity?.kind === 'moving') {
|
|
114
|
+
return {
|
|
115
|
+
status: {
|
|
116
|
+
activity: 'moving',
|
|
117
|
+
targetPlayer: target.player.name,
|
|
118
|
+
summary: cooldown > 0
|
|
119
|
+
? `正在贴近${label}等待攻击冷却(${cooldown}秒)。`
|
|
120
|
+
: `正在追近${label}。`,
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
const route = await api.navigation.pathDistance(selfPoint, target);
|
|
125
|
+
if (route === null) {
|
|
126
|
+
this.chasingSeat = undefined;
|
|
127
|
+
return { status: { activity: 'waiting', targetPlayer: target.player.name, summary: `${label}当前不可达。` } };
|
|
128
|
+
}
|
|
129
|
+
this.chasingSeat = seat;
|
|
130
|
+
const summary = cooldown > 0
|
|
131
|
+
? `正在贴近${label}等待攻击冷却(${cooldown}秒)。`
|
|
132
|
+
: `正在追近${label}。`;
|
|
133
|
+
return {
|
|
134
|
+
actions: [{ action: 'move', target_x: target.x, target_y: target.y, thinking_content: summary }],
|
|
135
|
+
status: { activity: 'moving', targetPlayer: target.player.name, summary },
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ClawclawBehavior,
|
|
3
|
+
type BehaviorDecision,
|
|
4
|
+
type BehaviorInput,
|
|
5
|
+
type BehaviorReadApi,
|
|
6
|
+
} from '@myclaw163/clawclaw-cli/behavior-plugin';
|
|
7
|
+
import { PublicRoomPatrol } from './shared.js';
|
|
8
|
+
|
|
9
|
+
export default class PatrolStrategy extends ClawclawBehavior {
|
|
10
|
+
static readonly manifest = {
|
|
11
|
+
apiVersion: 3,
|
|
12
|
+
id: 'patrol',
|
|
13
|
+
name: '巡逻',
|
|
14
|
+
description: '按地图房间顺序依次游走,到达一个房间后自动前往下一个,循环往复。卡路时会跳过当前目标换下一个房间。纯游走,不做任务、不打人、不报告尸体。',
|
|
15
|
+
execution: 'serial',
|
|
16
|
+
inputs: [],
|
|
17
|
+
} as const;
|
|
18
|
+
|
|
19
|
+
private readonly patrol = new PublicRoomPatrol();
|
|
20
|
+
|
|
21
|
+
override decide(
|
|
22
|
+
input: Readonly<BehaviorInput>,
|
|
23
|
+
api: BehaviorReadApi,
|
|
24
|
+
): Promise<BehaviorDecision> {
|
|
25
|
+
if (!input.gameState.you?.is_alive) {
|
|
26
|
+
return Promise.resolve({ status: { activity: 'idle', summary: '当前角色已死亡,巡逻停止行动。' } });
|
|
27
|
+
}
|
|
28
|
+
return this.patrol.decide(input, api, '巡逻');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BehaviorDecision,
|
|
3
|
+
BehaviorInput,
|
|
4
|
+
BehaviorReadApi,
|
|
5
|
+
Position,
|
|
6
|
+
SpatialRoom,
|
|
7
|
+
} from '@myclaw163/clawclaw-cli/behavior-plugin';
|
|
8
|
+
|
|
9
|
+
export const ROOM_ARRIVAL_DISTANCE = 48;
|
|
10
|
+
|
|
11
|
+
export function selfPosition(input: Readonly<BehaviorInput>): Position | undefined {
|
|
12
|
+
const self = input.gameState.you;
|
|
13
|
+
return self && Number.isFinite(self.x) && Number.isFinite(self.y)
|
|
14
|
+
? { x: self.x!, y: self.y! }
|
|
15
|
+
: undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function pointDistance(left: Readonly<Position>, right: Readonly<Position>): number {
|
|
19
|
+
return Math.hypot(left.x - right.x, left.y - right.y);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function roomCenter(room: Readonly<SpatialRoom>): Position {
|
|
23
|
+
const points = room.polygon.filter((point) => (
|
|
24
|
+
Array.isArray(point) && Number.isFinite(point[0]) && Number.isFinite(point[1])
|
|
25
|
+
));
|
|
26
|
+
if (points.length > 0) {
|
|
27
|
+
return {
|
|
28
|
+
x: points.reduce((sum, point) => sum + point[0], 0) / points.length,
|
|
29
|
+
y: points.reduce((sum, point) => sum + point[1], 0) / points.length,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
x: (room.bbox.left + room.bbox.right) / 2,
|
|
34
|
+
y: (room.bbox.top + room.bbox.bottom) / 2,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function eventType(input: Readonly<BehaviorInput>, type: string): boolean {
|
|
39
|
+
return input.events.some((event) => event.type === type);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class PublicRoomPatrol {
|
|
43
|
+
private routeIndex = 0;
|
|
44
|
+
private activeRoom?: string;
|
|
45
|
+
|
|
46
|
+
async decide(
|
|
47
|
+
input: Readonly<BehaviorInput>,
|
|
48
|
+
api: BehaviorReadApi,
|
|
49
|
+
purpose: string,
|
|
50
|
+
stopOnPlayer = false,
|
|
51
|
+
): Promise<BehaviorDecision> {
|
|
52
|
+
const self = selfPosition(input);
|
|
53
|
+
const rooms = api.spatial.rooms;
|
|
54
|
+
if (!self || rooms.length === 0) {
|
|
55
|
+
return { status: { activity: 'waiting', summary: `等待地图与自身位置后继续${purpose}。` } };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (this.activeRoom && eventType(input, 'move_interrupted')) {
|
|
59
|
+
this.routeIndex = (this.routeIndex + 1) % rooms.length;
|
|
60
|
+
this.activeRoom = undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
let target = rooms[this.routeIndex % rooms.length]!;
|
|
64
|
+
let center = roomCenter(target);
|
|
65
|
+
if (
|
|
66
|
+
input.gameState.you?.room === target.name
|
|
67
|
+
|| pointDistance(self, center) <= ROOM_ARRIVAL_DISTANCE
|
|
68
|
+
) {
|
|
69
|
+
if (rooms.length === 1) {
|
|
70
|
+
this.activeRoom = target.name;
|
|
71
|
+
return {
|
|
72
|
+
status: {
|
|
73
|
+
activity: 'waiting',
|
|
74
|
+
room: target.name,
|
|
75
|
+
summary: `已经在${target.name}${purpose}。`,
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
this.routeIndex = (this.routeIndex + 1) % rooms.length;
|
|
80
|
+
target = rooms[this.routeIndex]!;
|
|
81
|
+
center = roomCenter(target);
|
|
82
|
+
this.activeRoom = undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (
|
|
86
|
+
this.activeRoom === target.name
|
|
87
|
+
&& input.gameState.you?.activity?.kind === 'moving'
|
|
88
|
+
&& !eventType(input, 'move_end')
|
|
89
|
+
&& !eventType(input, 'move_interrupted')
|
|
90
|
+
) {
|
|
91
|
+
return {
|
|
92
|
+
status: {
|
|
93
|
+
activity: 'moving',
|
|
94
|
+
room: target.name,
|
|
95
|
+
summary: `正在前往${target.name}${purpose}。`,
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const distance = await api.navigation.pathDistance(self, center);
|
|
101
|
+
if (distance === null) {
|
|
102
|
+
this.routeIndex = (this.routeIndex + 1) % rooms.length;
|
|
103
|
+
this.activeRoom = undefined;
|
|
104
|
+
return {
|
|
105
|
+
status: {
|
|
106
|
+
activity: 'waiting',
|
|
107
|
+
summary: `${target.name}当前不可达,已跳过并准备下一个${purpose}目标。`,
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
this.activeRoom = target.name;
|
|
113
|
+
const summary = `正在前往${target.name}${purpose}。`;
|
|
114
|
+
return {
|
|
115
|
+
actions: [{
|
|
116
|
+
action: 'move',
|
|
117
|
+
target_x: center.x,
|
|
118
|
+
target_y: center.y,
|
|
119
|
+
stop_on_player: stopOnPlayer,
|
|
120
|
+
thinking_content: summary,
|
|
121
|
+
}],
|
|
122
|
+
status: { activity: 'moving', room: target.name, summary },
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ClawclawBehavior,
|
|
3
|
+
type BehaviorDecision,
|
|
4
|
+
type BehaviorInput,
|
|
5
|
+
type BehaviorReadApi,
|
|
6
|
+
type GameTask,
|
|
7
|
+
type Position,
|
|
8
|
+
} from '@myclaw163/clawclaw-cli/behavior-plugin';
|
|
9
|
+
import { pointDistance, selfPosition } from './shared.js';
|
|
10
|
+
|
|
11
|
+
type TaskTarget = Readonly<Position> & {
|
|
12
|
+
readonly id?: string;
|
|
13
|
+
readonly name: string;
|
|
14
|
+
readonly room?: string;
|
|
15
|
+
readonly status?: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function normalized(value: string | undefined): string {
|
|
19
|
+
return (value ?? '').trim().toLowerCase();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function eventMentionsTask(
|
|
23
|
+
input: Readonly<BehaviorInput>,
|
|
24
|
+
type: string,
|
|
25
|
+
task: Readonly<TaskTarget>,
|
|
26
|
+
): boolean {
|
|
27
|
+
return input.events.some((event) => {
|
|
28
|
+
if (event.type !== type) return false;
|
|
29
|
+
const values = [event.payload.task_id, event.payload.task_name, event.payload.task]
|
|
30
|
+
.filter((value): value is string => typeof value === 'string')
|
|
31
|
+
.map(normalized);
|
|
32
|
+
return [task.id, task.name].filter(Boolean).map(normalized).some((value) => values.includes(value));
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function gameTaskTarget(task: Readonly<GameTask>): TaskTarget {
|
|
37
|
+
return {
|
|
38
|
+
...(task.task_id ? { id: task.task_id } : {}),
|
|
39
|
+
name: task.name,
|
|
40
|
+
room: task.room,
|
|
41
|
+
x: task.x,
|
|
42
|
+
y: task.y,
|
|
43
|
+
status: task.status,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export default class TaskOnlyStrategy extends ClawclawBehavior {
|
|
48
|
+
static readonly manifest = {
|
|
49
|
+
apiVersion: 3,
|
|
50
|
+
id: 'task-only',
|
|
51
|
+
name: '纯任务',
|
|
52
|
+
description: '专心做任务。紧急任务出现时优先处理,其次按顺序完成普通任务,如果是蟹,则蟹的伪装任务也会做。没有尸体报告和战斗行为。',
|
|
53
|
+
execution: 'serial',
|
|
54
|
+
inputs: [],
|
|
55
|
+
} as const;
|
|
56
|
+
|
|
57
|
+
private selected?: TaskTarget;
|
|
58
|
+
private submittedAtMs?: number;
|
|
59
|
+
|
|
60
|
+
override async decide(
|
|
61
|
+
input: Readonly<BehaviorInput>,
|
|
62
|
+
api: BehaviorReadApi,
|
|
63
|
+
): Promise<BehaviorDecision> {
|
|
64
|
+
const self = input.gameState.you;
|
|
65
|
+
if (!self?.is_alive) {
|
|
66
|
+
return { status: { activity: 'idle', summary: '当前角色已死亡,纯任务停止行动。' } };
|
|
67
|
+
}
|
|
68
|
+
if (self.activity?.kind === 'task') {
|
|
69
|
+
return {
|
|
70
|
+
status: {
|
|
71
|
+
activity: 'doing_task',
|
|
72
|
+
room: self.room,
|
|
73
|
+
task: self.activity.task_name,
|
|
74
|
+
summary: self.activity.task_name
|
|
75
|
+
? `正在执行任务“${self.activity.task_name}”。`
|
|
76
|
+
: '正在执行任务。',
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const tasks = (self.tasks ?? []).filter((task) => task.status !== 'completed');
|
|
82
|
+
const emergency = input.gameState.emergency;
|
|
83
|
+
const emergencyTask = emergency?.active && emergency.task_name
|
|
84
|
+
? tasks.find((task) => normalized(task.name) === normalized(emergency.task_name))
|
|
85
|
+
: undefined;
|
|
86
|
+
const next = emergencyTask ?? tasks[0];
|
|
87
|
+
if (!next) {
|
|
88
|
+
this.selected = undefined;
|
|
89
|
+
this.submittedAtMs = undefined;
|
|
90
|
+
return {
|
|
91
|
+
status: {
|
|
92
|
+
activity: 'waiting',
|
|
93
|
+
room: self.room,
|
|
94
|
+
summary: `当前没有可用任务,正在${self.room ?? '当前位置'}等待。`,
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const target = gameTaskTarget(next);
|
|
100
|
+
if (
|
|
101
|
+
this.selected
|
|
102
|
+
&& normalized(this.selected.id ?? this.selected.name) !== normalized(target.id ?? target.name)
|
|
103
|
+
) {
|
|
104
|
+
this.submittedAtMs = undefined;
|
|
105
|
+
}
|
|
106
|
+
this.selected = target;
|
|
107
|
+
|
|
108
|
+
if (
|
|
109
|
+
next.status === 'completed'
|
|
110
|
+
|| eventMentionsTask(input, 'task_completed', target)
|
|
111
|
+
) {
|
|
112
|
+
this.selected = undefined;
|
|
113
|
+
this.submittedAtMs = undefined;
|
|
114
|
+
return { status: { activity: 'waiting', summary: `任务“${target.name}”已完成,准备下一个任务。` } };
|
|
115
|
+
}
|
|
116
|
+
if (eventMentionsTask(input, 'task_interrupted', target)) {
|
|
117
|
+
this.submittedAtMs = undefined;
|
|
118
|
+
}
|
|
119
|
+
if (this.submittedAtMs !== undefined && input.nowMs - this.submittedAtMs < 1_500) {
|
|
120
|
+
return {
|
|
121
|
+
status: {
|
|
122
|
+
activity: 'doing_task',
|
|
123
|
+
room: target.room,
|
|
124
|
+
task: target.name,
|
|
125
|
+
summary: `已提交任务“${target.name}”,等待权威任务状态。`,
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const from = selfPosition(input);
|
|
131
|
+
if (!from) return { status: { activity: 'waiting', summary: '等待自身位置后继续任务。' } };
|
|
132
|
+
const direct = pointDistance(from, target);
|
|
133
|
+
const route = await api.navigation.pathDistance(from, target);
|
|
134
|
+
if (route === null) {
|
|
135
|
+
return {
|
|
136
|
+
status: {
|
|
137
|
+
activity: 'waiting',
|
|
138
|
+
task: target.name,
|
|
139
|
+
summary: `任务“${target.name}”当前不可达,等待地图状态更新。`,
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
if (direct > 12) {
|
|
144
|
+
const summary = `正在前往${target.room ?? '任务点'}执行“${target.name}”。`;
|
|
145
|
+
return {
|
|
146
|
+
actions: [{ action: 'move', target_x: target.x, target_y: target.y, thinking_content: summary }],
|
|
147
|
+
status: { activity: 'moving', room: target.room, task: target.name, summary },
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
this.submittedAtMs = input.nowMs;
|
|
152
|
+
const summary = `正在提交任务“${target.name}”。`;
|
|
153
|
+
return {
|
|
154
|
+
actions: [{ action: 'task', task_name: target.name, thinking_content: summary }],
|
|
155
|
+
status: { activity: 'doing_task', room: target.room, task: target.name, summary },
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
@@ -1,25 +1,70 @@
|
|
|
1
1
|
export const OFFICIAL_URL = 'https://myclaw.163.com/';
|
|
2
|
-
export const REGISTER_COMMAND = 'ccl account register [--name <name>] --invite-code <code>';
|
|
3
|
-
export const
|
|
4
|
-
export const
|
|
2
|
+
export const REGISTER_COMMAND = 'ccl account register [--name <name>] [--invite-code <code>]';
|
|
3
|
+
export const REGISTRATION_CHECK_COMMAND = 'ccl account register --check';
|
|
4
|
+
export const DEFAULT_NAME_REGISTER_COMMAND = 'ccl account register';
|
|
5
|
+
export const NAMED_REGISTER_COMMAND = 'ccl account register --name <name>';
|
|
6
|
+
export const INVITE_RETRY_COMMAND = 'ccl account register --invite-code <code>';
|
|
7
|
+
export const NAMED_INVITE_RETRY_COMMAND = 'ccl account register --name <name> --invite-code <code>';
|
|
5
8
|
|
|
6
9
|
export const POST_REGISTRATION_NEXT_STEP =
|
|
7
10
|
'Ask the player whether to start matchmaking now. If yes, run `ccl load` first. When participation.status is `idle`, launch `ccl start match` with the host stream tool; if participation already exists, attach with `ccl join` instead.';
|
|
8
11
|
|
|
12
|
+
// Player-facing scripts, spoken verbatim by the Agent. This is the only place
|
|
13
|
+
// registration wording is defined, so the gated and ungated flows cannot drift.
|
|
14
|
+
// The gated script asks for the invite code and the game name in one turn,
|
|
15
|
+
// which is why the policy has to be known before the player is asked anything.
|
|
16
|
+
export const INVITE_REQUIRED_SCRIPT =
|
|
17
|
+
'注册新账号需要邀请码。请把邀请码发我;另外,你想给这个角色起什么游戏名?如果不想起名,也可以只发邀请码,我会用系统随机名。';
|
|
18
|
+
export const NO_INVITE_SCRIPT =
|
|
19
|
+
'你想给这个角色起什么游戏名?不想起名我就用系统随机名。';
|
|
20
|
+
|
|
21
|
+
export function registrationScript(inviteRequired: boolean): string {
|
|
22
|
+
return inviteRequired ? INVITE_REQUIRED_SCRIPT : NO_INVITE_SCRIPT;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// The registration policy lives on the server, so the Agent has to ask before
|
|
26
|
+
// it can know which script to use. `--check` is the only probe that never
|
|
27
|
+
// creates an account, which is what makes asking first safe.
|
|
9
28
|
const REGISTRATION_PROMPT = [
|
|
10
|
-
|
|
11
|
-
'
|
|
29
|
+
`Next: run \`${REGISTRATION_CHECK_COMMAND}\` before asking the player anything. It reports whether this server gates registration and returns the exact wording to use.`,
|
|
30
|
+
'Never guess whether an invite code is needed, and never ask about one before that check.',
|
|
31
|
+
'Then speak the returned `next_step` script verbatim and register with:',
|
|
12
32
|
` ${DEFAULT_NAME_REGISTER_COMMAND}`,
|
|
13
|
-
|
|
14
|
-
` ${
|
|
33
|
+
` ${NAMED_REGISTER_COMMAND} (only when the player explicitly gave a name)`,
|
|
34
|
+
` ${INVITE_RETRY_COMMAND} (invite code required, no game name given)`,
|
|
35
|
+
` ${NAMED_INVITE_RETRY_COMMAND} (invite code required and the player gave a name)`,
|
|
36
|
+
'A reply carrying only an invite code means the player accepted a system-generated name: register with --invite-code and do not ask about the name again.',
|
|
15
37
|
].join('\n');
|
|
16
38
|
|
|
39
|
+
export interface RegistrationCheckResult {
|
|
40
|
+
inviteRequired: boolean;
|
|
41
|
+
source: 'server' | 'fallback';
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function registrationCheckOutput(result: RegistrationCheckResult): Record<string, any> {
|
|
45
|
+
return {
|
|
46
|
+
success: true,
|
|
47
|
+
data: {
|
|
48
|
+
invite_required: result.inviteRequired,
|
|
49
|
+
source: result.source,
|
|
50
|
+
},
|
|
51
|
+
next_step: registrationScript(result.inviteRequired),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
17
55
|
type NoAccountAction = 'load' | 'gameStart' | 'accountInfo';
|
|
18
56
|
|
|
57
|
+
// Safety net for a caller that registered without checking the policy first.
|
|
58
|
+
// No account was created, so the only thing missing is the code.
|
|
19
59
|
export function missingInviteCodeMessage(): string {
|
|
20
60
|
return [
|
|
21
|
-
'
|
|
22
|
-
|
|
61
|
+
'This server requires an invite code to register. No account was created.',
|
|
62
|
+
'Next: ask the player for an invite code. Keep any game name already chosen and retry:',
|
|
63
|
+
` ${NAMED_INVITE_RETRY_COMMAND}`,
|
|
64
|
+
` ${INVITE_RETRY_COMMAND} (when no game name was chosen)`,
|
|
65
|
+
`When no game name has been chosen yet, ask with this exact wording: ${INVITE_REQUIRED_SCRIPT}`,
|
|
66
|
+
'A reply carrying only an invite code means the player accepted a system-generated name: do not ask about the name again.',
|
|
67
|
+
'Do not tell the player where to obtain an invite code. If the player has none, stop and say this server requires one.',
|
|
23
68
|
].join('\n');
|
|
24
69
|
}
|
|
25
70
|
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { AuthProfile } from './auth.js';
|
|
2
|
+
import { HttpTransport } from './http-transport.js';
|
|
3
|
+
import { ServerRegistry } from './server-registry.js';
|
|
4
|
+
import type { ResourceType } from './hub-client.js';
|
|
5
|
+
|
|
6
|
+
export interface AgentWorkspaceRemoteFile {
|
|
7
|
+
relative_path?: string;
|
|
8
|
+
file_id: string;
|
|
9
|
+
sha256: string;
|
|
10
|
+
size_bytes: number;
|
|
11
|
+
content_type: string;
|
|
12
|
+
url: string;
|
|
13
|
+
expires_in: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface AgentWorkspaceRemoteItem {
|
|
17
|
+
workspace_item_id: string;
|
|
18
|
+
resource_id: string;
|
|
19
|
+
version_id: string;
|
|
20
|
+
resource_type: ResourceType;
|
|
21
|
+
workspace_kind: 'owned' | 'installed';
|
|
22
|
+
is_created_by_me: boolean;
|
|
23
|
+
owner: {
|
|
24
|
+
id: string;
|
|
25
|
+
name: string | null;
|
|
26
|
+
avatar_url: string | null;
|
|
27
|
+
is_official: boolean;
|
|
28
|
+
};
|
|
29
|
+
content_digest: string;
|
|
30
|
+
source_files: AgentWorkspaceRemoteFile[];
|
|
31
|
+
archive_file?: AgentWorkspaceRemoteFile;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface AgentWorkspaceRemoteConfig {
|
|
35
|
+
revision: number;
|
|
36
|
+
items: Record<ResourceType, AgentWorkspaceRemoteItem[]>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
type AgentWorkspaceConfigItem = Omit<
|
|
40
|
+
AgentWorkspaceRemoteItem,
|
|
41
|
+
'workspace_kind' | 'is_created_by_me' | 'owner'
|
|
42
|
+
>;
|
|
43
|
+
|
|
44
|
+
interface AgentWorkspaceWireConfig {
|
|
45
|
+
revision: number;
|
|
46
|
+
items: Record<ResourceType, AgentWorkspaceConfigItem[]>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface Envelope<T> { success?: boolean; data: T }
|
|
50
|
+
|
|
51
|
+
interface AgentWorkspaceLoadoutItem {
|
|
52
|
+
workspace_item_id: string;
|
|
53
|
+
resource_id: string;
|
|
54
|
+
version_id: string;
|
|
55
|
+
resource_type: ResourceType;
|
|
56
|
+
kind: 'owned' | 'installed';
|
|
57
|
+
is_created_by_me: boolean;
|
|
58
|
+
owner: AgentWorkspaceRemoteItem['owner'];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface AgentWorkspaceLoadout {
|
|
62
|
+
revision: number;
|
|
63
|
+
items: Record<ResourceType, AgentWorkspaceLoadoutItem[]>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface AgentWorkspaceConfigReader {
|
|
67
|
+
getConfig(): Promise<AgentWorkspaceRemoteConfig>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export class AgentWorkspaceClient implements AgentWorkspaceConfigReader {
|
|
71
|
+
private readonly http: HttpTransport;
|
|
72
|
+
|
|
73
|
+
constructor(profile: Pick<AuthProfile, 'apiKey' | 'serverUrl'>) {
|
|
74
|
+
this.http = new HttpTransport(
|
|
75
|
+
profile.apiKey,
|
|
76
|
+
new ServerRegistry({ lobbyUrl: profile.serverUrl }),
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async getConfig(): Promise<AgentWorkspaceRemoteConfig> {
|
|
81
|
+
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
82
|
+
const configResponse = await this.http.get<Envelope<AgentWorkspaceWireConfig>>(
|
|
83
|
+
'/api/v1/agents/workspace/config',
|
|
84
|
+
AbortSignal.timeout(5_000),
|
|
85
|
+
);
|
|
86
|
+
const loadoutResponse = await this.http.get<Envelope<AgentWorkspaceLoadout>>(
|
|
87
|
+
'/api/v1/hub/workspace/loadout',
|
|
88
|
+
AbortSignal.timeout(5_000),
|
|
89
|
+
);
|
|
90
|
+
const config = configResponse.data;
|
|
91
|
+
const loadout = loadoutResponse.data;
|
|
92
|
+
if (config.revision !== loadout.revision) continue;
|
|
93
|
+
|
|
94
|
+
const metadata = new Map(
|
|
95
|
+
Object.values(loadout.items).flat().map((item) => [item.workspace_item_id, item]),
|
|
96
|
+
);
|
|
97
|
+
const items = Object.fromEntries(
|
|
98
|
+
(Object.keys(config.items) as ResourceType[]).map((type) => [
|
|
99
|
+
type,
|
|
100
|
+
config.items[type].map((item) => {
|
|
101
|
+
const selected = metadata.get(item.workspace_item_id);
|
|
102
|
+
if (
|
|
103
|
+
!selected
|
|
104
|
+
|| selected.resource_id !== item.resource_id
|
|
105
|
+
|| selected.version_id !== item.version_id
|
|
106
|
+
|| selected.resource_type !== item.resource_type
|
|
107
|
+
) {
|
|
108
|
+
throw new Error('agent_workspace_loadout_metadata_mismatch');
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
...item,
|
|
112
|
+
workspace_kind: selected.kind,
|
|
113
|
+
is_created_by_me: selected.is_created_by_me,
|
|
114
|
+
owner: selected.owner,
|
|
115
|
+
};
|
|
116
|
+
}),
|
|
117
|
+
]),
|
|
118
|
+
) as Record<ResourceType, AgentWorkspaceRemoteItem[]>;
|
|
119
|
+
return { ...config, items };
|
|
120
|
+
}
|
|
121
|
+
throw new Error('agent_workspace_revision_changed_during_metadata_sync');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export async function downloadAgentWorkspaceFile(url: string): Promise<Buffer> {
|
|
126
|
+
const parsed = new URL(url);
|
|
127
|
+
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') {
|
|
128
|
+
throw new Error(`agent_workspace_download_protocol_unsupported:${parsed.protocol}`);
|
|
129
|
+
}
|
|
130
|
+
const response = await fetch(parsed, { signal: AbortSignal.timeout(30_000) });
|
|
131
|
+
if (!response.ok) {
|
|
132
|
+
throw new Error(`agent_workspace_download_failed:${response.status}`);
|
|
133
|
+
}
|
|
134
|
+
return Buffer.from(await response.arrayBuffer());
|
|
135
|
+
}
|