@myclaw163/clawclaw-cli 0.7.5 → 0.7.7
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/docs/desktop-event-push.md +154 -0
- package/docs/runtime/README.md +1 -0
- package/docs/runtime/official-perception-modules.md +346 -0
- package/package.json +2 -1
- package/src/commands/game/index.test.ts +167 -1
- package/src/commands/game/index.ts +125 -36
- package/src/lib/game-client.test.ts +15 -0
- package/src/lib/game-client.ts +4 -0
- package/src/lib/participation-status.ts +113 -0
- package/src/lib/service-status.test.ts +56 -0
- package/src/lib/service-status.ts +111 -0
- package/src/pipeline/event-format.test.ts +30 -0
- package/src/pipeline/event-format.ts +26 -1
- package/src/pipeline/event-hints.test.ts +18 -3
- package/src/pipeline/event-hints.ts +11 -0
- package/src/pipeline/game-sync-event-normalization.test.ts +58 -1
- package/src/pipeline/game-sync-event-normalization.ts +51 -0
- package/src/runtime/clawclaw/agent-event-policy.test.ts +4 -0
- package/src/runtime/clawclaw/agent-event-policy.ts +1 -0
- package/src/runtime/clawclaw/owner.ts +62 -3
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.test.ts +189 -0
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.ts +532 -0
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-push.integration.test.ts +363 -0
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.test.ts +236 -0
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.ts +411 -0
- package/src/runtime/clawclaw/plugins/mailbox/index.ts +3 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-agent-event-batch.test.ts +166 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.test.ts +194 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.ts +262 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.ts +28 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.test.ts +49 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +77 -259
- package/src/runtime/clawclaw/plugins/perception/contracts.ts +4 -0
- package/src/runtime/clawclaw/plugins/perception/modules/co-movement-module.ts +6 -0
- package/src/runtime/clawclaw/plugins/perception/modules/follow-module.ts +6 -0
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.ts +6 -0
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.ts +6 -0
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.ts +6 -0
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.ts +6 -0
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.ts +6 -0
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.test.ts +11 -0
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +8 -0
- package/src/runtime/clawclaw/plugins/perception/perception-source.test.ts +12 -0
- package/src/runtime/clawclaw/plugins/perception/perception-source.ts +37 -1
- package/src/runtime/clawclaw/plugins/runtime-plugins.test.ts +5 -0
- package/src/runtime/clawclaw/plugins/source/room-transport.ts +2 -1
- package/src/runtime/clawclaw/plugins/source/source-plugin.test.ts +201 -1
- package/src/runtime/clawclaw/plugins/source/source-plugin.ts +113 -24
- package/src/runtime/clawclaw/plugins/source/types.ts +1 -0
- package/src/runtime/clawclaw/plugins/strategy/examples/task-only-strategy.ts +6 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-executor-types.ts +4 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-module-loader.ts +4 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.test.ts +36 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.ts +13 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-source.test.ts +22 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-source.ts +46 -16
- package/src/runtime/clawclaw-event-catalog.test.ts +2 -0
- package/src/runtime/clawclaw-event-catalog.ts +1 -0
- package/src/sdk/perception-plugin/index.ts +14 -0
- package/src/sdk/resource-metadata.test.ts +40 -0
- package/src/sdk/resource-metadata.ts +63 -0
- package/src/sdk/strategy-plugin/index.ts +14 -0
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
} from '../../../framework/modules/runtime-module-loader.js';
|
|
8
8
|
import {
|
|
9
9
|
STRATEGY_PLUGIN_API_VERSION,
|
|
10
|
+
hasValidClawclawResourceLocalizations,
|
|
10
11
|
type ClawclawStrategyManifest,
|
|
11
12
|
type StrategyControlSpec,
|
|
12
13
|
type StrategyInstance,
|
|
@@ -91,6 +92,9 @@ export function validateStrategyModule(
|
|
|
91
92
|
if (manifest.id !== source.id) throw new Error('strategy_id_mismatch');
|
|
92
93
|
if (!nonEmpty(manifest.name)) throw new Error('strategy_name_invalid');
|
|
93
94
|
if (!nonEmpty(manifest.description)) throw new Error('strategy_description_invalid');
|
|
95
|
+
if (!hasValidClawclawResourceLocalizations(manifest.localizations)) {
|
|
96
|
+
throw new Error('strategy_localizations_invalid');
|
|
97
|
+
}
|
|
94
98
|
if (
|
|
95
99
|
manifest.execution !== undefined
|
|
96
100
|
&& manifest.execution !== 'parallel_compute'
|
|
@@ -177,6 +177,42 @@ async function run(
|
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
describe('ClawClaw Strategy plugin scheduling', () => {
|
|
180
|
+
it('exposes localized metadata only after a strategy becomes active', async () => {
|
|
181
|
+
const plugin = new ClawclawStrategyPlugin({
|
|
182
|
+
executorFactory: () => ({
|
|
183
|
+
runtimeInstanceId: 'localized-metadata-test',
|
|
184
|
+
isAlive: true,
|
|
185
|
+
async start() {},
|
|
186
|
+
async reset() {},
|
|
187
|
+
async update() { return {}; },
|
|
188
|
+
async stop() {},
|
|
189
|
+
}),
|
|
190
|
+
});
|
|
191
|
+
plugin.select({
|
|
192
|
+
id: 'test-strategy',
|
|
193
|
+
name: 'Test strategy',
|
|
194
|
+
description: 'Runs a test strategy.',
|
|
195
|
+
localizations: {
|
|
196
|
+
'zh-CN': {
|
|
197
|
+
name: '测试策略',
|
|
198
|
+
description: '运行测试策略。',
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
sourcePath: 'unused',
|
|
202
|
+
sourceRoot: 'unused',
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
expect(plugin.activeResourceDescriptor('zh-CN')).toBeNull();
|
|
206
|
+
await run(plugin, input(1), context(() => {}));
|
|
207
|
+
expect(plugin.activeResourceDescriptor('zh-CN')).toEqual({
|
|
208
|
+
id: 'test-strategy',
|
|
209
|
+
name: '测试策略',
|
|
210
|
+
description: '运行测试策略。',
|
|
211
|
+
});
|
|
212
|
+
expect(plugin.activeResourceDescriptor('en-US')?.name).toBe('Test strategy');
|
|
213
|
+
await plugin.stop('shutdown');
|
|
214
|
+
});
|
|
215
|
+
|
|
180
216
|
it('releases each Strategy activation, including a same-id parameter generation', async () => {
|
|
181
217
|
const releases: Array<{ capability: string; id: string; reason?: string }> = [];
|
|
182
218
|
const baseContext = context(() => {});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isDeepStrictEqual } from 'node:util';
|
|
2
2
|
import type {
|
|
3
|
+
ClawclawResolvedResourceMetadata,
|
|
3
4
|
StrategyActionAck,
|
|
4
5
|
StrategyControlSpec,
|
|
5
6
|
StrategyControlResult,
|
|
@@ -7,6 +8,7 @@ import type {
|
|
|
7
8
|
StrategyFrame,
|
|
8
9
|
StrategyResult,
|
|
9
10
|
} from '../../../../sdk/strategy-plugin/index.js';
|
|
11
|
+
import { resolveClawclawResourceMetadata } from '../../../../sdk/strategy-plugin/index.js';
|
|
10
12
|
import type {
|
|
11
13
|
RuntimeEpochCatchUp,
|
|
12
14
|
RuntimeDiagnosticSeverity,
|
|
@@ -481,6 +483,17 @@ implements RuntimeEpochPlugin<Record<string, any>> {
|
|
|
481
483
|
};
|
|
482
484
|
}
|
|
483
485
|
|
|
486
|
+
activeResourceDescriptor(locale: string): ClawclawResolvedResourceMetadata | null {
|
|
487
|
+
const selection = this.selected;
|
|
488
|
+
if (!selection?.name?.trim() || !selection.description?.trim()) return null;
|
|
489
|
+
return resolveClawclawResourceMetadata({
|
|
490
|
+
id: selection.id,
|
|
491
|
+
name: selection.name,
|
|
492
|
+
description: selection.description,
|
|
493
|
+
...(selection.localizations ? { localizations: selection.localizations } : {}),
|
|
494
|
+
}, locale);
|
|
495
|
+
}
|
|
496
|
+
|
|
484
497
|
async update(
|
|
485
498
|
input: RuntimeEpochPluginInput<Record<string, any>>,
|
|
486
499
|
context: RuntimeEpochPluginContext<Record<string, any>>,
|
|
@@ -39,6 +39,16 @@ describe('default strategy resolution', () => {
|
|
|
39
39
|
.toEqual(Object.keys(readGameplayConfig(path).default_strategies));
|
|
40
40
|
expect(listClawclawStrategySources(path).find(source => source.id === 'task-only'))
|
|
41
41
|
.toMatchObject({ execution: 'parallel_analyze' });
|
|
42
|
+
expect(listClawclawStrategySources(path).find(source => source.id === 'runtime-task-demo'))
|
|
43
|
+
.toMatchObject({
|
|
44
|
+
name: 'Runtime task demo',
|
|
45
|
+
localizations: {
|
|
46
|
+
'zh-CN': {
|
|
47
|
+
name: 'Runtime 任务演示',
|
|
48
|
+
description: '向新进入视野的玩家打招呼,并按本地 A* 路径距离完成最近任务。',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
});
|
|
42
52
|
});
|
|
43
53
|
|
|
44
54
|
it('rejects a disabled default while ignoring unrelated stale disabled ids', async () => {
|
|
@@ -114,6 +124,12 @@ describe('default strategy resolution', () => {
|
|
|
114
124
|
id: 'invalid-hybrid-controls',
|
|
115
125
|
name: 'Invalid hybrid controls',
|
|
116
126
|
description: 'Controls would split analyzer and reducer generations.',
|
|
127
|
+
localizations: {
|
|
128
|
+
'zh-CN': {
|
|
129
|
+
name: '混合策略控制测试',
|
|
130
|
+
description: '验证分析器和归约器的参数屏障。',
|
|
131
|
+
},
|
|
132
|
+
},
|
|
117
133
|
execution: 'parallel_analyze',
|
|
118
134
|
controls: [{ id: 'target', type: 'string', description: 'Target.' }],
|
|
119
135
|
};
|
|
@@ -126,6 +142,12 @@ describe('default strategy resolution', () => {
|
|
|
126
142
|
id: 'invalid-hybrid-controls',
|
|
127
143
|
execution: 'parallel_analyze',
|
|
128
144
|
controls: [{ id: 'target', type: 'string', description: 'Target.' }],
|
|
145
|
+
localizations: {
|
|
146
|
+
'zh-CN': {
|
|
147
|
+
name: '混合策略控制测试',
|
|
148
|
+
description: '验证分析器和归约器的参数屏障。',
|
|
149
|
+
},
|
|
150
|
+
},
|
|
129
151
|
}));
|
|
130
152
|
});
|
|
131
153
|
});
|
|
@@ -23,7 +23,10 @@ import {
|
|
|
23
23
|
getUserDataStrategiesDir,
|
|
24
24
|
} from '../../../../lib/user-data.js';
|
|
25
25
|
import type { StrategySource } from './strategy-executor-types.js';
|
|
26
|
-
import type {
|
|
26
|
+
import type {
|
|
27
|
+
ClawclawResourceLocalizations,
|
|
28
|
+
StrategyControlSpec,
|
|
29
|
+
} from '../../../../sdk/strategy-plugin/index.js';
|
|
27
30
|
|
|
28
31
|
export const CLAWCLAW_TASK_ONLY_DEMO_STRATEGY_ID = 'runtime-task-demo';
|
|
29
32
|
|
|
@@ -49,20 +52,9 @@ export interface ResolvedClawclawStrategySource extends StrategySource {
|
|
|
49
52
|
}
|
|
50
53
|
|
|
51
54
|
function demoSource(): ResolvedClawclawStrategySource {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
description: 'Greets newly visible players and completes the nearest task by local A* path distance.',
|
|
56
|
-
execution: 'parallel_compute',
|
|
57
|
-
controls: [{
|
|
58
|
-
id: 'greeting',
|
|
59
|
-
type: 'string',
|
|
60
|
-
description: 'Speech template used for newly visible players; {player} is replaced with the player name.',
|
|
61
|
-
default: '你好,{player}',
|
|
62
|
-
}],
|
|
63
|
-
sourcePath: DEMO_SOURCE,
|
|
64
|
-
sourceRoot: dirname(DEMO_SOURCE),
|
|
65
|
-
};
|
|
55
|
+
const source = parseClassStrategy(DEMO_SOURCE, dirname(DEMO_SOURCE));
|
|
56
|
+
if (!source) throw new Error('Bundled Runtime task demo strategy manifest is invalid.');
|
|
57
|
+
return source;
|
|
66
58
|
}
|
|
67
59
|
|
|
68
60
|
function isPathInside(root: string, candidate: string): boolean {
|
|
@@ -258,6 +250,36 @@ function parseControlSpecs(classBody: string): StrategyControlSpec[] | undefined
|
|
|
258
250
|
return controls;
|
|
259
251
|
}
|
|
260
252
|
|
|
253
|
+
function literalLocalizations(
|
|
254
|
+
source: string,
|
|
255
|
+
): ClawclawResourceLocalizations | null | undefined {
|
|
256
|
+
const property = /\blocalizations\s*:\s*\{/m.exec(source);
|
|
257
|
+
if (!property) return undefined;
|
|
258
|
+
const open = property.index + property[0].lastIndexOf('{');
|
|
259
|
+
const block = balancedBlock(source, open);
|
|
260
|
+
if (!block) return null;
|
|
261
|
+
const values: Record<string, { name: string; description: string }> = {};
|
|
262
|
+
const entry = /(["'\x60])((?:\\.|(?!\1)[\s\S])*)\1\s*:\s*\{/g;
|
|
263
|
+
let cursor = 1;
|
|
264
|
+
let match: RegExpExecArray | null;
|
|
265
|
+
while ((match = entry.exec(block)) !== null) {
|
|
266
|
+
if (block.slice(cursor, match.index).replace(/[\s,]/g, '')) return null;
|
|
267
|
+
if (match[1] === '`' && match[2].includes('${')) return null;
|
|
268
|
+
const locale = decodeEscapedString(match[2])?.trim();
|
|
269
|
+
const entryOpen = match.index + match[0].lastIndexOf('{');
|
|
270
|
+
const entryBlock = balancedBlock(block, entryOpen);
|
|
271
|
+
if (!locale || !entryBlock || values[locale]) return null;
|
|
272
|
+
const name = literalProperty(entryBlock, 'name')?.trim();
|
|
273
|
+
const description = literalProperty(entryBlock, 'description')?.trim();
|
|
274
|
+
if (!name || !description) return null;
|
|
275
|
+
values[locale] = { name, description };
|
|
276
|
+
cursor = entryOpen + entryBlock.length;
|
|
277
|
+
entry.lastIndex = cursor;
|
|
278
|
+
}
|
|
279
|
+
if (block.slice(cursor, -1).replace(/[\s,]/g, '')) return null;
|
|
280
|
+
return values;
|
|
281
|
+
}
|
|
282
|
+
|
|
261
283
|
function parseClassStrategy(
|
|
262
284
|
sourcePath: string,
|
|
263
285
|
strategiesRoot: string,
|
|
@@ -285,7 +307,14 @@ function parseClassStrategy(
|
|
|
285
307
|
const id = literalProperty(manifest, 'id')?.trim();
|
|
286
308
|
const name = literalProperty(manifest, 'name')?.trim();
|
|
287
309
|
const description = literalProperty(manifest, 'description')?.trim();
|
|
288
|
-
|
|
310
|
+
const localizations = literalLocalizations(manifest);
|
|
311
|
+
if (
|
|
312
|
+
!id
|
|
313
|
+
|| !name
|
|
314
|
+
|| !description
|
|
315
|
+
|| localizations === null
|
|
316
|
+
|| !/^[a-z0-9][a-z0-9._-]*$/.test(id)
|
|
317
|
+
) return null;
|
|
289
318
|
const execution = literalProperty(manifest, 'execution')?.trim() ?? null;
|
|
290
319
|
const computeClass = classMatch[1] === 'ClawclawComputeStrategy';
|
|
291
320
|
const directHybridClass = classMatch[1] === 'ClawclawHybridStrategy';
|
|
@@ -320,6 +349,7 @@ function parseClassStrategy(
|
|
|
320
349
|
id,
|
|
321
350
|
name,
|
|
322
351
|
description,
|
|
352
|
+
...(localizations === undefined ? {} : { localizations }),
|
|
323
353
|
...(execution === null ? {} : { execution: execution as 'parallel_compute' | 'parallel_analyze' }),
|
|
324
354
|
...(controls === undefined ? {} : { controls }),
|
|
325
355
|
sourcePath,
|
|
@@ -11,6 +11,7 @@ describe('ClawClaw Event Catalog', () => {
|
|
|
11
11
|
it('uses the two explicit Agent registries and keeps events-only empty', () => {
|
|
12
12
|
expect(CLAWCLAW_EVENTS_ONLY_EVENT_TYPES.size).toBe(0);
|
|
13
13
|
expect(CLAWCLAW_MONITOR_AND_EVENTS_EVENT_TYPES).toContain('speech_your_turn');
|
|
14
|
+
expect(CLAWCLAW_MONITOR_AND_EVENTS_EVENT_TYPES).toContain('service_status');
|
|
14
15
|
expect(CLAWCLAW_MONITOR_AND_EVENTS_EVENT_TYPES).toContain('room_attached');
|
|
15
16
|
expect(CLAWCLAW_MONITOR_AND_EVENTS_EVENT_TYPES).toContain('quit_requested');
|
|
16
17
|
expect(CLAWCLAW_MONITOR_AND_EVENTS_EVENT_TYPES).toContain('room_created');
|
|
@@ -26,6 +27,7 @@ describe('ClawClaw Event Catalog', () => {
|
|
|
26
27
|
it('enforces a single producer for known semantics', () => {
|
|
27
28
|
const catalog = createClawclawEventCatalog();
|
|
28
29
|
expect(catalog.resolve('game_over', 'backend_push').deliveryClass).toBe('monitor_and_events');
|
|
30
|
+
expect(catalog.resolve('service_status', 'backend_push').deliveryClass).toBe('monitor_and_events');
|
|
29
31
|
expect(() => catalog.resolve('game_over', 'local_synth')).toThrow(/must be produced by backend_push/);
|
|
30
32
|
expect(catalog.resolve('future_backend_event', 'backend_push').deliveryClass).toBe('internal_only');
|
|
31
33
|
});
|
|
@@ -90,6 +90,7 @@ export const CLAWCLAW_GAME_STREAM_BACKEND_EVENT_TYPES = [
|
|
|
90
90
|
|
|
91
91
|
/** Backend facts that wake Monitor and remain available through ccl events. */
|
|
92
92
|
export const CLAWCLAW_BACKEND_AGENT_EVENT_TYPES = [
|
|
93
|
+
'service_status',
|
|
93
94
|
'move_interrupted',
|
|
94
95
|
'kill',
|
|
95
96
|
'killed',
|
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
export const PERCEPTION_PLUGIN_API_VERSION = 1 as const;
|
|
2
2
|
|
|
3
|
+
export {
|
|
4
|
+
hasValidClawclawResourceLocalizations,
|
|
5
|
+
resolveClawclawResourceMetadata,
|
|
6
|
+
} from '../resource-metadata.js';
|
|
7
|
+
export type {
|
|
8
|
+
ClawclawLocalizedResourceText,
|
|
9
|
+
ClawclawResolvedResourceMetadata,
|
|
10
|
+
ClawclawResourceLocalizations,
|
|
11
|
+
ClawclawResourceMetadata,
|
|
12
|
+
} from '../resource-metadata.js';
|
|
13
|
+
|
|
14
|
+
import type { ClawclawResourceLocalizations } from '../resource-metadata.js';
|
|
15
|
+
|
|
3
16
|
export type PerceptionSubjectScope =
|
|
4
17
|
| 'others'
|
|
5
18
|
| 'self_and_others'
|
|
@@ -188,6 +201,7 @@ export interface ClawclawPerceptionManifest {
|
|
|
188
201
|
readonly id: string;
|
|
189
202
|
readonly name: string;
|
|
190
203
|
readonly description: string;
|
|
204
|
+
readonly localizations?: ClawclawResourceLocalizations;
|
|
191
205
|
/** Describes Event subjects; every module still receives self as a spatial reference. */
|
|
192
206
|
readonly subjectScope: PerceptionSubjectScope;
|
|
193
207
|
readonly produces: readonly string[];
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
hasValidClawclawResourceLocalizations,
|
|
4
|
+
resolveClawclawResourceMetadata,
|
|
5
|
+
} from './resource-metadata.js';
|
|
6
|
+
|
|
7
|
+
const metadata = {
|
|
8
|
+
id: 'visual.motion',
|
|
9
|
+
name: 'Player movement',
|
|
10
|
+
description: 'Reports movement.',
|
|
11
|
+
localizations: {
|
|
12
|
+
'zh-CN': {
|
|
13
|
+
name: '玩家移动',
|
|
14
|
+
description: '感知玩家移动。',
|
|
15
|
+
},
|
|
16
|
+
en: {
|
|
17
|
+
name: 'Movement',
|
|
18
|
+
description: 'Perceives movement.',
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
} as const;
|
|
22
|
+
|
|
23
|
+
describe('resource metadata localization', () => {
|
|
24
|
+
it('resolves an exact locale, then language and manifest fallbacks', () => {
|
|
25
|
+
expect(resolveClawclawResourceMetadata(metadata, 'zh_CN')).toEqual({
|
|
26
|
+
id: 'visual.motion',
|
|
27
|
+
name: '玩家移动',
|
|
28
|
+
description: '感知玩家移动。',
|
|
29
|
+
});
|
|
30
|
+
expect(resolveClawclawResourceMetadata(metadata, 'en-US').name).toBe('Movement');
|
|
31
|
+
expect(resolveClawclawResourceMetadata(metadata, 'ja-JP').name).toBe('Player movement');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('rejects incomplete localized display text', () => {
|
|
35
|
+
expect(hasValidClawclawResourceLocalizations(metadata.localizations)).toBe(true);
|
|
36
|
+
expect(hasValidClawclawResourceLocalizations({
|
|
37
|
+
'zh-CN': { name: '玩家移动', description: ' ' },
|
|
38
|
+
})).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export interface ClawclawLocalizedResourceText {
|
|
2
|
+
readonly name: string;
|
|
3
|
+
readonly description: string;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export type ClawclawResourceLocalizations = Readonly<
|
|
7
|
+
Record<string, ClawclawLocalizedResourceText>
|
|
8
|
+
>;
|
|
9
|
+
|
|
10
|
+
export interface ClawclawResourceMetadata {
|
|
11
|
+
readonly id: string;
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly description: string;
|
|
14
|
+
readonly localizations?: ClawclawResourceLocalizations;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ClawclawResolvedResourceMetadata {
|
|
18
|
+
readonly id: string;
|
|
19
|
+
readonly name: string;
|
|
20
|
+
readonly description: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function normalizedLocale(value: string): string {
|
|
24
|
+
return value.trim().replaceAll('_', '-').toLowerCase();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function hasValidClawclawResourceLocalizations(
|
|
28
|
+
localizations: ClawclawResourceLocalizations | undefined,
|
|
29
|
+
): boolean {
|
|
30
|
+
if (localizations === undefined) return true;
|
|
31
|
+
if (!localizations || typeof localizations !== 'object' || Array.isArray(localizations)) return false;
|
|
32
|
+
return Object.entries(localizations).every(([locale, text]) => (
|
|
33
|
+
normalizedLocale(locale).length > 0
|
|
34
|
+
&& !!text
|
|
35
|
+
&& typeof text === 'object'
|
|
36
|
+
&& !Array.isArray(text)
|
|
37
|
+
&& typeof text.name === 'string'
|
|
38
|
+
&& text.name.trim().length > 0
|
|
39
|
+
&& typeof text.description === 'string'
|
|
40
|
+
&& text.description.trim().length > 0
|
|
41
|
+
));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Resolves localized display text while preserving manifest text as the fallback. */
|
|
45
|
+
export function resolveClawclawResourceMetadata(
|
|
46
|
+
metadata: ClawclawResourceMetadata,
|
|
47
|
+
locale: string,
|
|
48
|
+
): ClawclawResolvedResourceMetadata {
|
|
49
|
+
const requested = normalizedLocale(locale);
|
|
50
|
+
const entries = Object.entries(metadata.localizations ?? {});
|
|
51
|
+
const exact = entries.find(([key]) => normalizedLocale(key) === requested)?.[1];
|
|
52
|
+
const language = requested.split('-')[0];
|
|
53
|
+
const languageMatch = entries.find(([key]) => normalizedLocale(key) === language)?.[1];
|
|
54
|
+
const regionalMatch = entries.find(([key]) => (
|
|
55
|
+
normalizedLocale(key).startsWith(`${language}-`)
|
|
56
|
+
))?.[1];
|
|
57
|
+
const localized = exact ?? languageMatch ?? regionalMatch;
|
|
58
|
+
return {
|
|
59
|
+
id: metadata.id,
|
|
60
|
+
name: localized?.name ?? metadata.name,
|
|
61
|
+
description: localized?.description ?? metadata.description,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
@@ -22,6 +22,19 @@ import type {
|
|
|
22
22
|
|
|
23
23
|
export const STRATEGY_PLUGIN_API_VERSION = 1 as const;
|
|
24
24
|
|
|
25
|
+
export {
|
|
26
|
+
hasValidClawclawResourceLocalizations,
|
|
27
|
+
resolveClawclawResourceMetadata,
|
|
28
|
+
} from '../resource-metadata.js';
|
|
29
|
+
export type {
|
|
30
|
+
ClawclawLocalizedResourceText,
|
|
31
|
+
ClawclawResolvedResourceMetadata,
|
|
32
|
+
ClawclawResourceLocalizations,
|
|
33
|
+
ClawclawResourceMetadata,
|
|
34
|
+
} from '../resource-metadata.js';
|
|
35
|
+
|
|
36
|
+
import type { ClawclawResourceLocalizations } from '../resource-metadata.js';
|
|
37
|
+
|
|
25
38
|
export interface StrategyScope {
|
|
26
39
|
readonly sessionId: string;
|
|
27
40
|
readonly segmentId: string;
|
|
@@ -292,6 +305,7 @@ export interface ClawclawStrategyManifest {
|
|
|
292
305
|
readonly id: string;
|
|
293
306
|
readonly name: string;
|
|
294
307
|
readonly description: string;
|
|
308
|
+
readonly localizations?: ClawclawResourceLocalizations;
|
|
295
309
|
readonly controls?: readonly StrategyControlSpec[];
|
|
296
310
|
/** Omitted means the stateful, serial update() compatibility contract. */
|
|
297
311
|
readonly execution?: 'parallel_compute' | 'parallel_analyze';
|