cwtools-shared 0.2.9 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/generated/mcpTools.d.ts +45 -0
- package/dist/generated/mcpTools.js +90 -45
- package/dist/generated/workshopDetection.d.ts +22 -0
- package/dist/generated/workshopDetection.js +50 -0
- package/dist/generated/workspaceDetectionProfiles.d.ts +2 -0
- package/dist/generated/workspaceDetectionProfiles.js +84 -0
- package/dist/generated/workspaceGameDetection.d.ts +39 -0
- package/dist/generated/workspaceGameDetection.js +424 -0
- package/dist/host/capabilities.d.ts +39 -0
- package/dist/host/capabilities.js +124 -0
- package/dist/host/diagnostics.d.ts +5 -1
- package/dist/host/hostServices.d.ts +5 -0
- package/dist/host/lsp.d.ts +9 -0
- package/dist/host/lsp.js +27 -0
- package/dist/host/vanillaCache.js +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/tools/callHandler.d.ts +10 -0
- package/dist/tools/callHandler.js +72 -0
- package/dist/tools/mcpSchema.d.ts +20 -0
- package/dist/tools/mcpSchema.js +30 -2
- package/dist/tools/names.js +1 -1
- package/dist/tools/registry.d.ts +1 -0
- package/dist/tools/targetingWeights.d.ts +206 -0
- package/dist/tools/targetingWeights.js +350 -0
- package/dist/tools/toolHandlers.d.ts +5 -1
- package/dist/tools/toolHandlers.js +48 -25
- package/package.json +1 -1
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Stellaris 4.5.2 战斗索敌权重计算(纯计算,无副作用)。
|
|
4
|
+
*
|
|
5
|
+
* 实现 docs/stellaris-targeting-weight-4.5.2.md §2(舰船层乘法链 W1..W6 与整目标否决)
|
|
6
|
+
* 与 §3(武器层 p / ΣD 三层分摊 / S0..S + wk 加法项)的**归一化形式**:把引擎内部的
|
|
7
|
+
* 定点数 F = 100000 当作实数 1.0,除法按 C 风格向零截断的误差忽略不计。
|
|
8
|
+
*
|
|
9
|
+
* 两层互不相乘,各自 argmax(CShipTargeting::CalcBestTarget 与
|
|
10
|
+
* CWeaponComponentTargeting::CalcBestTarget);武器模板写 use_ship_main_target = yes 时
|
|
11
|
+
* 武器直接沿用舰船主目标,武器层分数不参与(误区 8)。
|
|
12
|
+
*
|
|
13
|
+
* 本模块刻意不 import vscode / fs / path,供后续 webview 面板(P3-1)直接复用。
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.VANILLA_TARGETING_DEFINES = void 0;
|
|
17
|
+
exports.computeTargetingChain = computeTargetingChain;
|
|
18
|
+
exports.executeTargetingWeightQuery = executeTargetingWeightQuery;
|
|
19
|
+
exports.parseTargetingChainInput = parseTargetingChainInput;
|
|
20
|
+
/** vanilla 取值(Stellaris 4.5.2 common/defines/00_defines.txt, NCombat / NShip)。 */
|
|
21
|
+
exports.VANILLA_TARGETING_DEFINES = {
|
|
22
|
+
sizeSlope: 0.03,
|
|
23
|
+
sizeMax: 10,
|
|
24
|
+
trackingMult: 2.0,
|
|
25
|
+
evasionWeight: 2.0,
|
|
26
|
+
lowHealthThreshold: 0.5,
|
|
27
|
+
lowHealthWeight: 1.5,
|
|
28
|
+
distanceWeight: 0.002,
|
|
29
|
+
distanceWeightMin: 0.10,
|
|
30
|
+
unarmedWeight: 0.005,
|
|
31
|
+
starbaseWeight: 0.75,
|
|
32
|
+
avoidMinShipSize: 8,
|
|
33
|
+
avoidThreshold: 0.7,
|
|
34
|
+
randomWeight: 3.0,
|
|
35
|
+
currentTargetMultiplier: 1.1,
|
|
36
|
+
recentTargetPenalizer: 0.3,
|
|
37
|
+
};
|
|
38
|
+
/** NShip 块常量:尺寸放大上限 MAX_DAMAGE_SCALING。 */
|
|
39
|
+
const MAX_DAMAGE_SCALING = 100;
|
|
40
|
+
function clamp(value, min, max) {
|
|
41
|
+
return value < min ? min : value > max ? max : value;
|
|
42
|
+
}
|
|
43
|
+
function resolveDefines(partial) {
|
|
44
|
+
const merged = { ...exports.VANILLA_TARGETING_DEFINES };
|
|
45
|
+
if (!partial) {
|
|
46
|
+
return merged;
|
|
47
|
+
}
|
|
48
|
+
for (const key of Object.keys(exports.VANILLA_TARGETING_DEFINES)) {
|
|
49
|
+
const value = partial[key];
|
|
50
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
51
|
+
merged[key] = value;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return merged;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 计算单个候选目标的完整索敌权重链。纯函数:不读文件、不依赖 vscode。
|
|
58
|
+
*/
|
|
59
|
+
function computeTargetingChain(input) {
|
|
60
|
+
const defs = resolveDefines(input.defines);
|
|
61
|
+
const attacker = input.attacker;
|
|
62
|
+
const weapon = input.weapon;
|
|
63
|
+
const target = input.target;
|
|
64
|
+
// ---- 舰船层(§2) ----------------------------------------------------
|
|
65
|
+
const sizeCapped = Math.min(target.sizeMultiplier, defs.sizeMax);
|
|
66
|
+
const W1 = 1 / (1 + sizeCapped * defs.sizeSlope);
|
|
67
|
+
const shipTracking = attacker.tracking * defs.trackingMult;
|
|
68
|
+
const evasionDelta = target.evasion - shipTracking;
|
|
69
|
+
const vetoCheck = {
|
|
70
|
+
evasionDeltaPositive: evasionDelta > 0,
|
|
71
|
+
attackerSizeLargeEnough: attacker.sizeMultiplier >= defs.avoidMinShipSize,
|
|
72
|
+
deltaAboveThreshold: evasionDelta >= defs.avoidThreshold,
|
|
73
|
+
fleetHasHittable: input.fleetHasHittable === true,
|
|
74
|
+
};
|
|
75
|
+
const veto = vetoCheck.evasionDeltaPositive
|
|
76
|
+
&& vetoCheck.attackerSizeLargeEnough
|
|
77
|
+
&& vetoCheck.deltaAboveThreshold
|
|
78
|
+
&& vetoCheck.fleetHasHittable;
|
|
79
|
+
const evasionFactor = evasionDelta > 0 ? 1 / (1 + evasionDelta * defs.evasionWeight) : 1;
|
|
80
|
+
const distanceFactor = Math.max(1 - target.distance * defs.distanceWeight, defs.distanceWeightMin);
|
|
81
|
+
const targetWeight = 1 + (input.rho ?? 0) + (input.targetWeightMultModifier ?? 0);
|
|
82
|
+
const maxHull = target.maxHull;
|
|
83
|
+
const hasHealthRatio = typeof maxHull === 'number' && Number.isFinite(maxHull) && maxHull > 0;
|
|
84
|
+
const healthRatio = hasHealthRatio ? clamp(target.hull / maxHull, 0, 1) : null;
|
|
85
|
+
const lowHealthFactor = healthRatio !== null && healthRatio < defs.lowHealthThreshold ? defs.lowHealthWeight : 1;
|
|
86
|
+
const unarmedFactor = target.armed ? 1 : defs.unarmedWeight;
|
|
87
|
+
const starbaseFactor = target.isStarbase === true && target.isCurrentMainTarget !== true
|
|
88
|
+
? defs.starbaseWeight
|
|
89
|
+
: 1;
|
|
90
|
+
const randomRoll = clamp(input.randomRoll ?? 0, 0, 1);
|
|
91
|
+
const randomFactor = input.useRandom === true ? 1 + randomRoll * defs.randomWeight : 1;
|
|
92
|
+
// 引擎在否决时直接 return 0,后续各项不再计算;这里保留因子供诊断,但把 W2..W6 归零。
|
|
93
|
+
const W2 = veto ? 0 : W1 * evasionFactor;
|
|
94
|
+
const W3 = veto ? 0 : W2 * distanceFactor;
|
|
95
|
+
const W4 = veto ? 0 : W3 * targetWeight;
|
|
96
|
+
const W5 = veto ? 0 : W4 * lowHealthFactor * unarmedFactor * starbaseFactor;
|
|
97
|
+
const W6 = veto ? 0 : W5 * randomFactor;
|
|
98
|
+
const shipChain = {
|
|
99
|
+
veto,
|
|
100
|
+
vetoCheck,
|
|
101
|
+
sizeCapped,
|
|
102
|
+
W1,
|
|
103
|
+
shipTracking,
|
|
104
|
+
evasionDelta,
|
|
105
|
+
evasionFactor,
|
|
106
|
+
W2,
|
|
107
|
+
distanceFactor,
|
|
108
|
+
W3,
|
|
109
|
+
targetWeight,
|
|
110
|
+
W4,
|
|
111
|
+
lowHealthFactor,
|
|
112
|
+
unarmedFactor,
|
|
113
|
+
starbaseFactor,
|
|
114
|
+
W5,
|
|
115
|
+
randomFactor,
|
|
116
|
+
W6,
|
|
117
|
+
weight: W6,
|
|
118
|
+
};
|
|
119
|
+
// ---- 武器层(§3) ----------------------------------------------------
|
|
120
|
+
const baseDamage = (weapon.minDamage + weapon.maxDamage) / 2;
|
|
121
|
+
const sizeFactor = weapon.sizeDamageFactor ?? 0;
|
|
122
|
+
const scaledDamage = sizeFactor > 0
|
|
123
|
+
? baseDamage * (1 + clamp(target.sizeMultiplier, 1, MAX_DAMAGE_SCALING) * sizeFactor)
|
|
124
|
+
: baseDamage;
|
|
125
|
+
// D 在每一层都被 min(·) 扣减,层分量本身不被该层存量夹住——这就是"盾甲吸引仇恨"的根因。
|
|
126
|
+
let remaining = scaledDamage;
|
|
127
|
+
let Ds = 0;
|
|
128
|
+
let Da = 0;
|
|
129
|
+
if (target.shield > 0) {
|
|
130
|
+
const s = clamp((weapon.shieldPenetration ?? 0) - (target.shieldHardening ?? 0), 0, 1);
|
|
131
|
+
const Cs = (1 - s) * remaining;
|
|
132
|
+
remaining -= Math.min(Cs, target.shield);
|
|
133
|
+
Ds = weapon.shieldDamage * Cs;
|
|
134
|
+
}
|
|
135
|
+
if (target.armor > 0) {
|
|
136
|
+
const a = clamp((weapon.armorPenetration ?? 0) - (target.armorHardening ?? 0), 0, 1);
|
|
137
|
+
const Ca = (1 - a) * remaining;
|
|
138
|
+
remaining -= Math.min(Ca, target.armor);
|
|
139
|
+
Da = weapon.armorDamage * Ca;
|
|
140
|
+
}
|
|
141
|
+
const Dh = weapon.hullDamage * remaining;
|
|
142
|
+
const p = clamp(weapon.accuracy - clamp(target.evasion - weapon.tracking, 0, 1), 0, 1);
|
|
143
|
+
const sumD = Ds + Da + Dh;
|
|
144
|
+
const S0 = sumD * p * targetWeight;
|
|
145
|
+
const killBonus = Dh >= target.hull ? 1.5 : 1;
|
|
146
|
+
const S1 = S0 * killBonus;
|
|
147
|
+
const unarmedPenalty = target.armed ? 1 : defs.unarmedWeight;
|
|
148
|
+
const S2 = S1 * unarmedPenalty;
|
|
149
|
+
const wk = target.shipSizeId !== undefined ? weapon.targetWeights?.[target.shipSizeId] ?? 0 : 0;
|
|
150
|
+
const score = S2 + wk;
|
|
151
|
+
const currentTargetMultiplier = target.isCurrentMainTarget === true && input.targetFocus !== true
|
|
152
|
+
? defs.currentTargetMultiplier
|
|
153
|
+
: 1;
|
|
154
|
+
const recentTargetPenalizer = target.isRecentNonLatest === true ? defs.recentTargetPenalizer : 1;
|
|
155
|
+
const weaponLayer = {
|
|
156
|
+
p,
|
|
157
|
+
baseDamage,
|
|
158
|
+
damageSplit: { Ds, Da, Dh },
|
|
159
|
+
sumD,
|
|
160
|
+
S0,
|
|
161
|
+
killBonus,
|
|
162
|
+
S1,
|
|
163
|
+
unarmedPenalty,
|
|
164
|
+
S2,
|
|
165
|
+
wk,
|
|
166
|
+
score,
|
|
167
|
+
currentTargetMultiplier,
|
|
168
|
+
recentTargetPenalizer,
|
|
169
|
+
final: score * currentTargetMultiplier * recentTargetPenalizer,
|
|
170
|
+
};
|
|
171
|
+
const notes = buildNotes(input, defs, shipChain, weaponLayer, healthRatio !== null);
|
|
172
|
+
return { veto, shipChain, weapon: weaponLayer, defines: defs, notes };
|
|
173
|
+
}
|
|
174
|
+
function buildNotes(input, defs, shipChain, weaponLayer, hasHealthRatio) {
|
|
175
|
+
const notes = [];
|
|
176
|
+
notes.push('两层互不相乘:舰船层与武器层各自 argmax;武器模板写 use_ship_main_target = yes 时武器直接沿用舰船主目标,武器层分数不参与(误区 8)。');
|
|
177
|
+
notes.push('索敌不是每 tick 重算:已有有效主目标时只在 (TotalTicks + shipID) mod fp == 0 的 tick 重算,fp = 50(targetFocus = 1)或 10。');
|
|
178
|
+
if (shipChain.veto) {
|
|
179
|
+
notes.push('触发整目标否决:直接 return 0,该候选从候选集中剔除,不参与 argmax(误区 6)。判据为 Δ>0 且 σatt ≥ σavoidmin 且 Δ ≥ Tavoid 且目标舰队存在可命中目标。');
|
|
180
|
+
}
|
|
181
|
+
if (shipChain.evasionDelta <= 0) {
|
|
182
|
+
notes.push('Δ ≤ 0(追踪盖过闪避),舰船层不施加闪避惩罚。');
|
|
183
|
+
}
|
|
184
|
+
if (shipChain.distanceFactor === defs.distanceWeightMin) {
|
|
185
|
+
notes.push(`距离因子已触下限 ${defs.distanceWeightMin}(d ≥ ${(1 - defs.distanceWeightMin) / defs.distanceWeight}),更远不再衰减。`);
|
|
186
|
+
}
|
|
187
|
+
if (input.randomRoll === undefined && input.useRandom !== true) {
|
|
188
|
+
notes.push(`本次未启用随机扰动(useRandom 默认 false)。引擎默认开启,末项乘数 ∈ [1, 1+κr) = [1, ${1 + defs.randomWeight}),是索敌不稳定的主因之一(误区 10)。`);
|
|
189
|
+
}
|
|
190
|
+
if (!hasHealthRatio) {
|
|
191
|
+
notes.push('未提供 target.maxHull,舰船层不做残血判定(h < θh 收人头加成未生效)。');
|
|
192
|
+
}
|
|
193
|
+
if (weaponLayer.damageSplit.Ds > 0 || weaponLayer.damageSplit.Da > 0) {
|
|
194
|
+
notes.push('目标带盾/甲:层分量不被该层存量夹住,只有从剩余伤害里扣减的量才被 min 夹住,所以 ΣD 反而更大。有效反制只有高闪避(压低 p)(误区 1)。');
|
|
195
|
+
}
|
|
196
|
+
if (weaponLayer.killBonus > 1) {
|
|
197
|
+
notes.push('斩杀加成触发:判据是船体分量 Dh ≥ 目标当前船体,不是总伤(误区 7)。');
|
|
198
|
+
}
|
|
199
|
+
if (weaponLayer.wk > 0) {
|
|
200
|
+
notes.push('命中 weapon.target_weights:wk 是**加法项**而非倍率,可以盖过整条乘法链,用法与直觉相反(误区 4)。');
|
|
201
|
+
}
|
|
202
|
+
if (input.rho !== undefined && input.rho > 0) {
|
|
203
|
+
notes.push('ρ 是开战瞬间每艘船各掷一次、整场不变的随机量,所以两条设计完全相同的船每场战斗的 ŵtgt 也不同(误区 5)。');
|
|
204
|
+
}
|
|
205
|
+
notes.push('ai_weight 是 AI 建造/决策权重,与战斗索敌无关(误区 2)。');
|
|
206
|
+
return notes;
|
|
207
|
+
}
|
|
208
|
+
/** 工具调用入口:校验并收窄不可信的工具参数,失败时返回结构化错误而不是抛出。 */
|
|
209
|
+
function executeTargetingWeightQuery(args) {
|
|
210
|
+
let input;
|
|
211
|
+
try {
|
|
212
|
+
input = parseTargetingChainInput(args);
|
|
213
|
+
}
|
|
214
|
+
catch (error) {
|
|
215
|
+
return {
|
|
216
|
+
error: `query_targeting_weight: ${error instanceof Error ? error.message : String(error)}`,
|
|
217
|
+
hint: 'Pass numeric attacker/weapon/target fields; every field must be a finite number.',
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
return computeTargetingChain(input);
|
|
221
|
+
}
|
|
222
|
+
function isRecord(value) {
|
|
223
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
224
|
+
}
|
|
225
|
+
function requireNumber(source, key, path) {
|
|
226
|
+
const value = source[key];
|
|
227
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
228
|
+
throw new Error(`${path}.${key} must be a finite number.`);
|
|
229
|
+
}
|
|
230
|
+
return value;
|
|
231
|
+
}
|
|
232
|
+
function optionalNumber(source, key, path) {
|
|
233
|
+
const value = source[key];
|
|
234
|
+
if (value === undefined || value === null) {
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
238
|
+
throw new Error(`${path}.${key} must be a finite number when provided.`);
|
|
239
|
+
}
|
|
240
|
+
return value;
|
|
241
|
+
}
|
|
242
|
+
function requireBoolean(source, key, path) {
|
|
243
|
+
const value = source[key];
|
|
244
|
+
if (typeof value !== 'boolean') {
|
|
245
|
+
throw new Error(`${path}.${key} must be a boolean.`);
|
|
246
|
+
}
|
|
247
|
+
return value;
|
|
248
|
+
}
|
|
249
|
+
function optionalBoolean(source, key, path) {
|
|
250
|
+
const value = source[key];
|
|
251
|
+
if (value === undefined || value === null) {
|
|
252
|
+
return undefined;
|
|
253
|
+
}
|
|
254
|
+
if (typeof value !== 'boolean') {
|
|
255
|
+
throw new Error(`${path}.${key} must be a boolean when provided.`);
|
|
256
|
+
}
|
|
257
|
+
return value;
|
|
258
|
+
}
|
|
259
|
+
function optionalNumberRecord(source, key, path) {
|
|
260
|
+
const value = source[key];
|
|
261
|
+
if (value === undefined || value === null) {
|
|
262
|
+
return undefined;
|
|
263
|
+
}
|
|
264
|
+
if (!isRecord(value)) {
|
|
265
|
+
throw new Error(`${path}.${key} must be an object mapping ship_size id to number.`);
|
|
266
|
+
}
|
|
267
|
+
const out = {};
|
|
268
|
+
for (const [entryKey, entryValue] of Object.entries(value)) {
|
|
269
|
+
if (typeof entryValue !== 'number' || !Number.isFinite(entryValue)) {
|
|
270
|
+
throw new Error(`${path}.${key}.${entryKey} must be a finite number.`);
|
|
271
|
+
}
|
|
272
|
+
out[entryKey] = entryValue;
|
|
273
|
+
}
|
|
274
|
+
return out;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* 把工具参数(不可信输入)收窄为 TargetingChainInput。
|
|
278
|
+
* 未知字段一律忽略并按缺省处理;类型错误立即抛出带字段路径的消息。
|
|
279
|
+
*/
|
|
280
|
+
function parseTargetingChainInput(args) {
|
|
281
|
+
if (!isRecord(args)) {
|
|
282
|
+
throw new Error('arguments must be an object.');
|
|
283
|
+
}
|
|
284
|
+
const attackerRaw = args.attacker;
|
|
285
|
+
const weaponRaw = args.weapon;
|
|
286
|
+
const targetRaw = args.target;
|
|
287
|
+
if (!isRecord(attackerRaw) || !isRecord(weaponRaw) || !isRecord(targetRaw)) {
|
|
288
|
+
throw new Error('attacker, weapon and target must all be objects.');
|
|
289
|
+
}
|
|
290
|
+
const definesRaw = args.defines;
|
|
291
|
+
let defines;
|
|
292
|
+
if (definesRaw !== undefined && definesRaw !== null) {
|
|
293
|
+
if (!isRecord(definesRaw)) {
|
|
294
|
+
throw new Error('defines must be an object when provided.');
|
|
295
|
+
}
|
|
296
|
+
defines = {};
|
|
297
|
+
for (const key of Object.keys(exports.VANILLA_TARGETING_DEFINES)) {
|
|
298
|
+
const value = optionalNumber(definesRaw, key, 'defines');
|
|
299
|
+
if (value !== undefined) {
|
|
300
|
+
defines[key] = value;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
const shipSizeIdRaw = targetRaw.shipSizeId;
|
|
305
|
+
if (shipSizeIdRaw !== undefined && shipSizeIdRaw !== null && typeof shipSizeIdRaw !== 'string') {
|
|
306
|
+
throw new Error('target.shipSizeId must be a string when provided.');
|
|
307
|
+
}
|
|
308
|
+
return {
|
|
309
|
+
attacker: {
|
|
310
|
+
tracking: requireNumber(attackerRaw, 'tracking', 'attacker'),
|
|
311
|
+
sizeMultiplier: requireNumber(attackerRaw, 'sizeMultiplier', 'attacker'),
|
|
312
|
+
},
|
|
313
|
+
weapon: {
|
|
314
|
+
minDamage: requireNumber(weaponRaw, 'minDamage', 'weapon'),
|
|
315
|
+
maxDamage: requireNumber(weaponRaw, 'maxDamage', 'weapon'),
|
|
316
|
+
accuracy: requireNumber(weaponRaw, 'accuracy', 'weapon'),
|
|
317
|
+
tracking: requireNumber(weaponRaw, 'tracking', 'weapon'),
|
|
318
|
+
shieldDamage: requireNumber(weaponRaw, 'shieldDamage', 'weapon'),
|
|
319
|
+
armorDamage: requireNumber(weaponRaw, 'armorDamage', 'weapon'),
|
|
320
|
+
hullDamage: requireNumber(weaponRaw, 'hullDamage', 'weapon'),
|
|
321
|
+
shieldPenetration: optionalNumber(weaponRaw, 'shieldPenetration', 'weapon'),
|
|
322
|
+
armorPenetration: optionalNumber(weaponRaw, 'armorPenetration', 'weapon'),
|
|
323
|
+
sizeDamageFactor: optionalNumber(weaponRaw, 'sizeDamageFactor', 'weapon'),
|
|
324
|
+
targetWeights: optionalNumberRecord(weaponRaw, 'targetWeights', 'weapon'),
|
|
325
|
+
},
|
|
326
|
+
target: {
|
|
327
|
+
shipSizeId: shipSizeIdRaw === null ? undefined : shipSizeIdRaw,
|
|
328
|
+
sizeMultiplier: requireNumber(targetRaw, 'sizeMultiplier', 'target'),
|
|
329
|
+
evasion: requireNumber(targetRaw, 'evasion', 'target'),
|
|
330
|
+
distance: requireNumber(targetRaw, 'distance', 'target'),
|
|
331
|
+
hull: requireNumber(targetRaw, 'hull', 'target'),
|
|
332
|
+
maxHull: optionalNumber(targetRaw, 'maxHull', 'target'),
|
|
333
|
+
shield: requireNumber(targetRaw, 'shield', 'target'),
|
|
334
|
+
armor: requireNumber(targetRaw, 'armor', 'target'),
|
|
335
|
+
shieldHardening: optionalNumber(targetRaw, 'shieldHardening', 'target'),
|
|
336
|
+
armorHardening: optionalNumber(targetRaw, 'armorHardening', 'target'),
|
|
337
|
+
armed: requireBoolean(targetRaw, 'armed', 'target'),
|
|
338
|
+
isStarbase: optionalBoolean(targetRaw, 'isStarbase', 'target'),
|
|
339
|
+
isCurrentMainTarget: optionalBoolean(targetRaw, 'isCurrentMainTarget', 'target'),
|
|
340
|
+
isRecentNonLatest: optionalBoolean(targetRaw, 'isRecentNonLatest', 'target'),
|
|
341
|
+
},
|
|
342
|
+
defines,
|
|
343
|
+
rho: optionalNumber(args, 'rho', 'root'),
|
|
344
|
+
targetWeightMultModifier: optionalNumber(args, 'targetWeightMultModifier', 'root'),
|
|
345
|
+
randomRoll: optionalNumber(args, 'randomRoll', 'root'),
|
|
346
|
+
useRandom: optionalBoolean(args, 'useRandom', 'root'),
|
|
347
|
+
fleetHasHittable: optionalBoolean(args, 'fleetHasHittable', 'root'),
|
|
348
|
+
targetFocus: optionalBoolean(args, 'targetFocus', 'root'),
|
|
349
|
+
};
|
|
350
|
+
}
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import type { HostServices } from '../host/hostServices';
|
|
2
2
|
import { type SharedToolResult } from './schema';
|
|
3
|
-
|
|
3
|
+
/** Per-call context threaded from the transport (cancellation, and future knobs). */
|
|
4
|
+
export interface SharedToolContext {
|
|
5
|
+
signal?: AbortSignal;
|
|
6
|
+
}
|
|
7
|
+
export type SharedToolDispatcher = (host: HostServices, name: string, args: Record<string, unknown>, context?: SharedToolContext) => Promise<SharedToolResult>;
|
|
4
8
|
export declare const defaultSharedToolDispatcher: SharedToolDispatcher;
|
|
@@ -35,12 +35,14 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.defaultSharedToolDispatcher = void 0;
|
|
37
37
|
const path = __importStar(require("path"));
|
|
38
|
+
const lsp_1 = require("../host/lsp");
|
|
38
39
|
const diagnosticRouting_1 = require("../knowledge/diagnosticRouting");
|
|
39
40
|
const rules_1 = require("../knowledge/rules");
|
|
40
41
|
const profile_1 = require("../project/profile");
|
|
41
42
|
const knowledge_1 = require("../project/knowledge");
|
|
42
43
|
const localisation_1 = require("../safety/localisation");
|
|
43
44
|
const pdxBlock_1 = require("./pdxBlock");
|
|
45
|
+
const targetingWeights_1 = require("./targetingWeights");
|
|
44
46
|
const schema_1 = require("./schema");
|
|
45
47
|
const symbols_1 = require("./symbols");
|
|
46
48
|
function asRecord(value) {
|
|
@@ -48,8 +50,11 @@ function asRecord(value) {
|
|
|
48
50
|
? value
|
|
49
51
|
: {};
|
|
50
52
|
}
|
|
51
|
-
async function executeLspTool(host, command, args, unavailableNextStep) {
|
|
52
|
-
|
|
53
|
+
async function executeLspTool(host, command, args, unavailableNextStep, signal) {
|
|
54
|
+
// Cancellation must not be laundered into `unavailable`: the caller asked to
|
|
55
|
+
// stop, so the abort error propagates untouched.
|
|
56
|
+
(0, lsp_1.throwIfAborted)(signal);
|
|
57
|
+
const result = await host.lsp.executeCommand(command, args, signal ? { signal } : undefined);
|
|
53
58
|
const record = asRecord(result);
|
|
54
59
|
const errorRecord = asRecord(record.error);
|
|
55
60
|
const unavailable = record.status === 'unavailable' || errorRecord.code === 'lsp_unavailable';
|
|
@@ -67,8 +72,11 @@ async function executeLspTool(host, command, args, unavailableNextStep) {
|
|
|
67
72
|
nextSteps: unavailable ? [unavailableNextStep] : undefined,
|
|
68
73
|
};
|
|
69
74
|
}
|
|
70
|
-
const defaultSharedToolDispatcher = async (host, name, rawArgs) => {
|
|
75
|
+
const defaultSharedToolDispatcher = async (host, name, rawArgs, context) => {
|
|
71
76
|
const args = asRecord(rawArgs);
|
|
77
|
+
const signal = context?.signal;
|
|
78
|
+
const lspTool = (command, args, unavailableNextStep) => executeLspTool(host, command, args, unavailableNextStep, signal);
|
|
79
|
+
(0, lsp_1.throwIfAborted)(signal);
|
|
72
80
|
switch (name) {
|
|
73
81
|
case 'query_project_profile':
|
|
74
82
|
return (0, profile_1.queryProjectProfileWithHost)(host, args);
|
|
@@ -98,7 +106,7 @@ const defaultSharedToolDispatcher = async (host, name, rawArgs) => {
|
|
|
98
106
|
};
|
|
99
107
|
}
|
|
100
108
|
case 'explore_pdx_project':
|
|
101
|
-
return
|
|
109
|
+
return lspTool('cwtools.ai.exploreProject', [
|
|
102
110
|
typeof args.query === 'string' ? args.query : '',
|
|
103
111
|
typeof args.file === 'string' && args.file ? toFileUri(args.file, host.workspaceRoot) : '',
|
|
104
112
|
typeof args.typeName === 'string' ? args.typeName : '',
|
|
@@ -122,7 +130,7 @@ const defaultSharedToolDispatcher = async (host, name, rawArgs) => {
|
|
|
122
130
|
},
|
|
123
131
|
};
|
|
124
132
|
}
|
|
125
|
-
return
|
|
133
|
+
return lspTool('cwtools.ai.analyzePdxFlow', [{
|
|
126
134
|
...(file ? { file } : {}),
|
|
127
135
|
...(definitionId ? { definitionId } : {}),
|
|
128
136
|
...(typeof args.entityType === 'string' && args.entityType.trim() ? { entityType: args.entityType.trim() } : {}),
|
|
@@ -141,7 +149,7 @@ const defaultSharedToolDispatcher = async (host, name, rawArgs) => {
|
|
|
141
149
|
},
|
|
142
150
|
};
|
|
143
151
|
}
|
|
144
|
-
return
|
|
152
|
+
return lspTool('cwtools.ai.compareDefinitionWithVanilla', [args.entityType.trim(), args.symbolId.trim()], 'Start the CWTools LSP process and wait for the game model to load before comparing the definition with vanilla.');
|
|
145
153
|
}
|
|
146
154
|
case 'query_inline_instantiation': {
|
|
147
155
|
const template = typeof args.template === 'string' ? args.template.trim() : '';
|
|
@@ -157,13 +165,28 @@ const defaultSharedToolDispatcher = async (host, name, rawArgs) => {
|
|
|
157
165
|
},
|
|
158
166
|
};
|
|
159
167
|
}
|
|
160
|
-
return
|
|
168
|
+
return lspTool('cwtools.ai.exploreInlineGraph', [{
|
|
161
169
|
...(template ? { template } : {}),
|
|
162
170
|
...(file ? { file } : {}),
|
|
163
171
|
...(Number.isInteger(args.line) ? { line: Number(args.line) } : {}),
|
|
164
172
|
limit: Math.max(1, Math.min(Number(args.limit ?? 50) || 50, 200)),
|
|
165
173
|
}], 'Start the CWTools LSP process and wait for the game model to load before resolving the inline template instantiation.');
|
|
166
174
|
}
|
|
175
|
+
case 'query_targeting_weight': {
|
|
176
|
+
// Pure computation: no host, no I/O, and no backend to wait for. The shared
|
|
177
|
+
// implementation is the single source of truth for the formula, so the MCP
|
|
178
|
+
// surface, the in-extension agent and the panel produce identical numbers
|
|
179
|
+
// for the same inputs.
|
|
180
|
+
const data = (0, targetingWeights_1.executeTargetingWeightQuery)(args);
|
|
181
|
+
const failure = typeof data.error === 'string' ? data.error : undefined;
|
|
182
|
+
return {
|
|
183
|
+
ok: failure === undefined,
|
|
184
|
+
status: failure === undefined ? 'ready' : 'error',
|
|
185
|
+
source: 'cwtools-shared',
|
|
186
|
+
data,
|
|
187
|
+
error: failure === undefined ? undefined : { code: 'invalid_arguments', message: failure },
|
|
188
|
+
};
|
|
189
|
+
}
|
|
167
190
|
case 'query_localisation_index': {
|
|
168
191
|
if (!host.indexing) {
|
|
169
192
|
return (0, schema_1.toolUnavailable)(name, 'Localisation index is not available in this host.', [
|
|
@@ -172,7 +195,7 @@ const defaultSharedToolDispatcher = async (host, name, rawArgs) => {
|
|
|
172
195
|
}
|
|
173
196
|
const data = await host.indexing.queryLocalisation(args);
|
|
174
197
|
const semanticAudit = args.auditMode === true
|
|
175
|
-
? await
|
|
198
|
+
? await lspTool('cwtools.ai.queryLocalisationAudit', [{
|
|
176
199
|
...(typeof args.key === 'string' ? { key: args.key } : {}),
|
|
177
200
|
prefix: args.prefix === true,
|
|
178
201
|
contains: args.contains === true,
|
|
@@ -189,7 +212,7 @@ const defaultSharedToolDispatcher = async (host, name, rawArgs) => {
|
|
|
189
212
|
}
|
|
190
213
|
case 'get_diagnostics':
|
|
191
214
|
{
|
|
192
|
-
const diagnostics = await host.diagnostics.getDiagnostics(args);
|
|
215
|
+
const diagnostics = await host.diagnostics.getDiagnostics(args, signal ? { signal } : undefined);
|
|
193
216
|
return {
|
|
194
217
|
ok: diagnostics.ok,
|
|
195
218
|
status: diagnostics.status === 'unavailable' ? 'unavailable' : diagnostics.status === 'stale' ? 'stale' : diagnostics.status === 'pending' ? 'loading' : 'ready',
|
|
@@ -247,9 +270,9 @@ const defaultSharedToolDispatcher = async (host, name, rawArgs) => {
|
|
|
247
270
|
limit: typeof args.limit === 'number' ? args.limit : undefined,
|
|
248
271
|
});
|
|
249
272
|
case 'query_scope':
|
|
250
|
-
return
|
|
273
|
+
return lspTool('cwtools.ai.getScopeAtPosition', [toFileUri(String(args.file ?? ''), host.workspaceRoot), args.line, args.column], 'Start the CWTools LSP process or connect this host to an existing language server.');
|
|
251
274
|
case 'query_types':
|
|
252
|
-
return
|
|
275
|
+
return lspTool('cwtools.ai.queryTypes', [args.typeName, args.filter ?? '', args.limit ?? 30, args.vanilla === true], 'Start the CWTools LSP process or connect this host to an existing language server.');
|
|
253
276
|
case 'query_rules':
|
|
254
277
|
return (0, rules_1.queryRulesWithHost)(host, {
|
|
255
278
|
category: String(args.category ?? 'trigger'),
|
|
@@ -278,19 +301,19 @@ const defaultSharedToolDispatcher = async (host, name, rawArgs) => {
|
|
|
278
301
|
scope: String(args.scope ?? ''),
|
|
279
302
|
});
|
|
280
303
|
case 'parse_pdx_fragment':
|
|
281
|
-
return
|
|
304
|
+
return lspTool('cwtools.ai.parseFragment', [String(args.code ?? '')], 'Start the CWTools LSP process or connect this host to an existing language server.');
|
|
282
305
|
case 'query_scripted_effects':
|
|
283
|
-
return
|
|
306
|
+
return lspTool('cwtools.ai.queryScriptedEffects', [args.filter ?? '', args.limit ?? 50], 'Start the CWTools LSP process or connect this host to an existing language server.');
|
|
284
307
|
case 'query_scripted_triggers':
|
|
285
|
-
return
|
|
308
|
+
return lspTool('cwtools.ai.queryScriptedTriggers', [args.filter ?? '', args.limit ?? 50], 'Start the CWTools LSP process or connect this host to an existing language server.');
|
|
286
309
|
case 'query_enums':
|
|
287
|
-
return
|
|
310
|
+
return lspTool('cwtools.ai.queryEnums', [args.enumName ?? '', args.limit ?? 500], 'Start the CWTools LSP process or connect this host to an existing language server.');
|
|
288
311
|
case 'query_static_modifiers':
|
|
289
|
-
return
|
|
312
|
+
return lspTool('cwtools.ai.queryStaticModifiers', [args.filter ?? '', args.limit ?? 300], 'Start the CWTools LSP process or connect this host to an existing language server.');
|
|
290
313
|
case 'query_variables':
|
|
291
|
-
return
|
|
314
|
+
return lspTool('cwtools.ai.queryVariables', [args.filter ?? ''], 'Start the CWTools LSP process or connect this host to an existing language server.');
|
|
292
315
|
case 'get_entity_info':
|
|
293
|
-
return
|
|
316
|
+
return lspTool('cwtools.ai.getEntityInfo', [toFileUri(String(args.file ?? ''), host.workspaceRoot)], 'Start the CWTools LSP process or connect this host to an existing language server.');
|
|
294
317
|
case 'get_pdx_block':
|
|
295
318
|
return (0, pdxBlock_1.getPdxBlockWithHost)(host, {
|
|
296
319
|
file: String(args.file ?? ''),
|
|
@@ -299,23 +322,23 @@ const defaultSharedToolDispatcher = async (host, name, rawArgs) => {
|
|
|
299
322
|
// Shader knowledge tools: each LSP command takes a single JSON record
|
|
300
323
|
// argument and returns { ok, ... } / { ok: false, status: 'error', ... }.
|
|
301
324
|
case 'query_shader_symbol':
|
|
302
|
-
return
|
|
325
|
+
return lspTool('cwtools.ai.shader.symbols', [shaderRecord({
|
|
303
326
|
filter: typeof args.filter === 'string' ? args.filter : undefined,
|
|
304
327
|
kind: typeof args.kind === 'string' ? args.kind : undefined,
|
|
305
328
|
limit: typeof args.limit === 'number' ? args.limit : undefined,
|
|
306
329
|
cursor: typeof args.cursor === 'number' ? args.cursor : undefined,
|
|
307
330
|
})], 'Start the CWTools LSP process and wait for the shader model to load before querying shader symbols.');
|
|
308
331
|
case 'query_shader_compile_unit':
|
|
309
|
-
return
|
|
332
|
+
return lspTool('cwtools.ai.shader.compileUnit', [{ uri: toFileUri(String(args.file ?? ''), host.workspaceRoot) }], 'Start the CWTools LSP process and wait for the shader model to load before resolving compile units.');
|
|
310
333
|
case 'query_shader_platform_variants':
|
|
311
|
-
return
|
|
334
|
+
return lspTool('cwtools.ai.shader.variants', [{ uri: toFileUri(String(args.file ?? ''), host.workspaceRoot) }], 'Start the CWTools LSP process and wait for the shader model to load before comparing shader platform variants.');
|
|
312
335
|
case 'query_shader_callers':
|
|
313
|
-
return
|
|
336
|
+
return lspTool('cwtools.ai.shader.callers', [shaderRecord({
|
|
314
337
|
effectName: String(args.effectName ?? ''),
|
|
315
338
|
limit: typeof args.limit === 'number' ? args.limit : undefined,
|
|
316
339
|
})], 'Start the CWTools LSP process and wait for the shader model to load before querying Effect callers.');
|
|
317
340
|
case 'explain_shader_reachability':
|
|
318
|
-
return
|
|
341
|
+
return lspTool('cwtools.ai.shader.reachability', [typeof args.effectName === 'string' && args.effectName
|
|
319
342
|
? { effectName: args.effectName }
|
|
320
343
|
: shaderRecord({
|
|
321
344
|
uri: toFileUri(String(args.file ?? ''), host.workspaceRoot),
|
|
@@ -323,9 +346,9 @@ const defaultSharedToolDispatcher = async (host, name, rawArgs) => {
|
|
|
323
346
|
cursor: typeof args.cursor === 'number' ? args.cursor : undefined,
|
|
324
347
|
})], 'Start the CWTools LSP process and wait for the shader model to load before explaining Effect reachability.');
|
|
325
348
|
case 'validate_shader':
|
|
326
|
-
return
|
|
349
|
+
return lspTool('cwtools.ai.shader.validate', [{ uri: toFileUri(String(args.file ?? ''), host.workspaceRoot) }], 'Start the CWTools LSP process and wait for the shader model to load before validating shader files.');
|
|
327
350
|
case 'compare_shader_with_vanilla':
|
|
328
|
-
return
|
|
351
|
+
return lspTool('cwtools.ai.shader.compareVanilla', [typeof args.effectName === 'string' && args.effectName
|
|
329
352
|
? { effectName: args.effectName }
|
|
330
353
|
: { uri: toFileUri(String(args.file ?? ''), host.workspaceRoot) }], 'Start the CWTools LSP process and wait for the shader model to load before comparing with vanilla.');
|
|
331
354
|
case 'get_ide_context':
|