@myclaw163/clawclaw-cli 0.8.38 → 0.8.40

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.
Files changed (45) hide show
  1. package/docs/custom-modules/README.md +58 -65
  2. package/docs/custom-modules/api-reference.md +16 -3
  3. package/docs/custom-modules/behavior.md +167 -129
  4. package/docs/custom-modules/examples/go-to-point.ts +49 -0
  5. package/docs/custom-modules/examples/nearby-escape.ts +56 -0
  6. package/docs/custom-modules/examples/nearby-notice.ts +36 -0
  7. package/docs/custom-modules/perception-reactions.md +189 -0
  8. package/docs/custom-modules/perception-visual.md +324 -0
  9. package/docs/custom-modules/perception.md +54 -317
  10. package/docs/custom-modules/reminders.md +31 -18
  11. package/docs/custom-modules/trigger.md +2 -0
  12. package/docs/runtime/components-and-boundaries.md +2 -2
  13. package/package.json +1 -1
  14. package/skills/clawclaw/SKILL.md +11 -5
  15. package/skills/clawclaw/references/BEHAVIORS.md +13 -3
  16. package/skills/clawclaw/references/CUSTOM-BEHAVIOR-DEVELOPMENT.md +166 -142
  17. package/skills/clawclaw/references/CUSTOM-MODULE-API.md +19 -3
  18. package/skills/clawclaw/references/CUSTOM-MODULES.md +55 -54
  19. package/skills/clawclaw/references/CUSTOM-PERCEPTION-DEVELOPMENT.md +54 -320
  20. package/skills/clawclaw/references/CUSTOM-REACTION-DEVELOPMENT.md +189 -0
  21. package/skills/clawclaw/references/CUSTOM-REMINDER-DEVELOPMENT.md +31 -18
  22. package/skills/clawclaw/references/CUSTOM-VISUAL-PERCEPTION-DEVELOPMENT.md +328 -0
  23. package/src/commands/load.ts +25 -1
  24. package/src/commands/perception.ts +1 -0
  25. package/src/lib/agent-workspace-runtime.ts +11 -5
  26. package/src/lib/capability-load-diagnostics.ts +1 -1
  27. package/src/lib/capability-operation-name.ts +1 -0
  28. package/src/runtime/clawclaw/behavior-activation-capability.ts +21 -0
  29. package/src/runtime/clawclaw/owner.ts +8 -3
  30. package/src/runtime/clawclaw/plugins/assembly.ts +20 -3
  31. package/src/runtime/clawclaw/plugins/behavior/activation-manager.ts +1 -1
  32. package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.ts +4 -0
  33. package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +72 -9
  34. package/src/runtime/clawclaw/plugins/behavior/behavior-source.ts +2 -0
  35. package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +1 -1
  36. package/src/runtime/clawclaw/plugins/perception/perception-source.ts +14 -1
  37. package/src/runtime/clawclaw/plugins/perception/reaction-plugin.ts +198 -0
  38. package/src/runtime/clawclaw/plugins/perception/reaction-runner.ts +49 -0
  39. package/src/runtime/clawclaw/plugins/perception/reaction-worker.ts +26 -0
  40. package/src/runtime/clawclaw/reaction-behavior-resolution.ts +23 -0
  41. package/src/runtime/clawclaw/reaction-manifest.ts +50 -0
  42. package/src/runtime/clawclaw/runtime.ts +3 -0
  43. package/src/runtime/clawclaw-event-catalog.ts +5 -1
  44. package/src/sdk/perception-plugin/index.ts +1 -0
  45. package/src/sdk/perception-plugin/reaction.ts +78 -0
@@ -0,0 +1,23 @@
1
+ import type { ReactionBehaviorBinding } from '../../sdk/perception-plugin/reaction.js';
2
+
3
+ export interface ReactionBehaviorCandidate {
4
+ readonly id: string;
5
+ readonly declaredId?: string;
6
+ readonly resourceId?: string;
7
+ readonly loadable: boolean;
8
+ }
9
+
10
+ /** Shared by load diagnostics and the live activation path. Never selects disabled/unloaded entries. */
11
+ export function resolveReactionBehavior(binding: ReactionBehaviorBinding, candidates: readonly ReactionBehaviorCandidate[]): string | undefined {
12
+ const available = candidates.filter(candidate => candidate.loadable);
13
+ const compare = (a: ReactionBehaviorCandidate, b: ReactionBehaviorCandidate) =>
14
+ Number(b.id === binding.name) - Number(a.id === binding.name)
15
+ || a.id.localeCompare(b.id, 'en', { numeric: true }) || String(a.resourceId).localeCompare(String(b.resourceId));
16
+ const preferred = binding.resourceId && available.filter(candidate => candidate.resourceId === binding.resourceId).sort(compare)[0];
17
+ if (preferred) return preferred.id;
18
+ // Declared source identity distinguishes real duplicate aliases from an unrelated literal escape_1.
19
+ const named = available.filter(candidate => candidate.id === binding.name || candidate.declaredId === binding.name
20
+ || candidate.declaredId === undefined && candidate.id.startsWith(`${binding.name}_`)
21
+ && /^[1-9][0-9]*$/.test(candidate.id.slice(binding.name.length + 1))).sort(compare);
22
+ return named[0]?.id;
23
+ }
@@ -0,0 +1,50 @@
1
+ import type { ClawclawReactionManifest, ReactionBehaviorBinding } from '../../sdk/perception-plugin/reaction.js';
2
+ import { hasValidClawclawResourceLocalizations } from '../../sdk/resource-metadata.js';
3
+ import { normalizeBehaviorInput, normalizeBehaviorInputFields } from './behavior-input-fields.js';
4
+ import { isClawclawFixedEventType } from '../clawclaw-event-catalog.js';
5
+
6
+ const ID = /^[a-z0-9][a-z0-9._-]*$/;
7
+ const OPERATION = /^[A-Za-z0-9][A-Za-z0-9._-]{0,255}$/;
8
+ const record = (value: unknown): value is Record<string, unknown> => !!value && typeof value === 'object' && !Array.isArray(value);
9
+ const integer = (value: unknown, minimum: number, maximum: number) =>
10
+ Number.isSafeInteger(value) && Number(value) >= minimum && Number(value) <= maximum;
11
+
12
+ export function normalizeReactionManifest(value: unknown, requireCurrentVersion = true): ClawclawReactionManifest {
13
+ const m = value as ClawclawReactionManifest;
14
+ if (!record(value) || m.kind !== 'reaction' || !Number.isInteger(m.apiVersion)
15
+ || requireCurrentVersion && m.apiVersion !== 2 || typeof m.id !== 'string' || !ID.test(m.id)
16
+ || typeof m.description !== 'string' || !m.description.trim()
17
+ || m.name !== undefined && (typeof m.name !== 'string' || !m.name.trim())
18
+ || !hasValidClawclawResourceLocalizations(m.localizations)
19
+ || !Array.isArray(m.consumes) || m.consumes.some(type => typeof type !== 'string' || !ID.test(type)
20
+ || /^(reaction|perception|behavior|decision)\./.test(type))
21
+ || !Array.isArray(m.produces) || m.produces.some(type => typeof type !== 'string'
22
+ || !/^reaction\.[a-z0-9][a-z0-9._-]*$/.test(type) || isClawclawFixedEventType(type))
23
+ || m.behaviors !== undefined && !record(m.behaviors)
24
+ || m.config !== undefined && !record(m.config)
25
+ || Object.keys(m).some(key => !['apiVersion', 'kind', 'id', 'name', 'description', 'localizations',
26
+ 'consumes', 'produces', 'behaviors', 'inputs', 'config'].includes(key))) throw new Error('reaction_manifest_invalid');
27
+ const behaviors: Record<string, ReactionBehaviorBinding> = {};
28
+ if (Object.keys(m.behaviors ?? {}).length > 16) throw new Error('reaction_behavior_limit');
29
+ for (const [key, binding] of Object.entries(m.behaviors ?? {})) {
30
+ if (!ID.test(key) || ['__proto__', 'constructor', 'prototype'].includes(key) || !record(binding)
31
+ || typeof binding.name !== 'string' || !OPERATION.test(binding.name)
32
+ || binding.resourceId !== undefined && (typeof binding.resourceId !== 'string' || !binding.resourceId.trim() || binding.resourceId.length > 255)
33
+ || binding.layer !== undefined && !['reflex', 'short', 'long'].includes(binding.layer)
34
+ || binding.input !== undefined && !record(binding.input)
35
+ || binding.reflexPriority !== undefined && !integer(binding.reflexPriority, -1_000_000, 1_000_000)
36
+ || binding.cooldownMs !== undefined && !integer(binding.cooldownMs, 0, 86_400_000)
37
+ || binding.maxRuntimeMs !== undefined && !integer(binding.maxRuntimeMs, 1, 120_000)
38
+ || binding.rearm !== undefined && !['after_clear', 'after_cooldown'].includes(binding.rearm)
39
+ || Object.keys(binding).some(field => !['name', 'resourceId', 'layer', 'input', 'reflexPriority',
40
+ 'cooldownMs', 'maxRuntimeMs', 'rearm'].includes(field))) throw new Error(`reaction_behavior_invalid:${key}`);
41
+ behaviors[key] = structuredClone(binding);
42
+ }
43
+ const inputs = m.inputs ? normalizeBehaviorInputFields(m.inputs) : undefined;
44
+ const config = inputs ? normalizeBehaviorInput(m.config ?? {}, inputs) : structuredClone(m.config ?? {});
45
+ return { apiVersion: m.apiVersion, kind: 'reaction', id: m.id, description: m.description,
46
+ ...(m.name === undefined ? {} : { name: m.name }),
47
+ ...(m.localizations === undefined ? {} : { localizations: structuredClone(m.localizations) }),
48
+ consumes: [...new Set(m.consumes)], produces: [...new Set(m.produces)], behaviors,
49
+ ...(inputs ? { inputs } : {}), config };
50
+ }
@@ -48,6 +48,7 @@ export type ClawclawRuntimeOptions = Omit<
48
48
  visualEventDelivery?: ClawclawVisualEventDelivery;
49
49
  /** Visual Event types declared by the selected perception modules. */
50
50
  visualEventTypes?: readonly string[];
51
+ reactionEventTypes?: readonly string[];
51
52
  /** Event types declared by enabled Intent and Strategy modules at startup. */
52
53
  behaviorEventTypes?: readonly string[];
53
54
  };
@@ -96,6 +97,7 @@ export function createClawclawRuntime(
96
97
  stateFreshnessPolicyResolver: configuredResolver,
97
98
  visualEventDelivery,
98
99
  visualEventTypes,
100
+ reactionEventTypes,
99
101
  behaviorEventTypes,
100
102
  ...runtimeOptions
101
103
  } = options;
@@ -144,6 +146,7 @@ export function createClawclawRuntime(
144
146
  eventCatalog: createClawclawEventCatalog({
145
147
  visualEventDelivery,
146
148
  visualEventTypes,
149
+ reactionEventTypes,
147
150
  behaviorEventTypes,
148
151
  }),
149
152
  // Mailbox ACK adapters own independent durable lanes. Disabled and
@@ -36,6 +36,8 @@ export interface ClawclawEventCatalogOptions {
36
36
  visualEventTypes?: readonly string[];
37
37
  /** Events declared by the enabled Intent and Strategy manifests at startup. */
38
38
  behaviorEventTypes?: readonly string[];
39
+ /** Own notification types from equipped reaction perceptions. */
40
+ reactionEventTypes?: readonly string[];
39
41
  }
40
42
 
41
43
  /**
@@ -181,6 +183,8 @@ export const CLAWCLAW_LOCAL_AGENT_EVENT_TYPES = [
181
183
  'players_changed',
182
184
  'reminder.task',
183
185
  'reminder.speech',
186
+ 'perception.behavior_unavailable',
187
+ 'perception.reaction_failed',
184
188
  'emergency_countdown_warning',
185
189
  'ability.ready',
186
190
  'ability.remaining_uses_changed',
@@ -257,7 +261,7 @@ function entries(options: ClawclawEventCatalogOptions = {}): EventCatalogEntry[]
257
261
  ...CLAWCLAW_FIXED_EVENT_TYPES,
258
262
  ...visualEventTypes,
259
263
  ]);
260
- const behaviorEventTypes = [...new Set(options.behaviorEventTypes ?? [])]
264
+ const behaviorEventTypes = [...new Set([...(options.behaviorEventTypes ?? []), ...(options.reactionEventTypes ?? [])])]
261
265
  .filter((type) => !fixedAndVisualTypes.has(type))
262
266
  .sort();
263
267
  return [
@@ -1,5 +1,6 @@
1
1
  export const PERCEPTION_PLUGIN_API_VERSION = 2 as const;
2
2
  export * from './reminder.js';
3
+ export * from './reaction.js';
3
4
  export { defaultReminder as evaluateReminder } from './reminder-rules.js';
4
5
 
5
6
  export {
@@ -0,0 +1,78 @@
1
+ import type { GameEvent, GameState, ObservationFrame } from '../module-input/index.js';
2
+ import type { BehaviorInputFieldSpec } from '../behavior-plugin/index.js';
3
+ import type { ClawclawResourceLocalizations } from '../resource-metadata.js';
4
+
5
+ /** A fixed dependency, resolved against this account's equipped, loadable Behaviors. */
6
+ export interface ReactionBehaviorBinding {
7
+ readonly name: string;
8
+ /** Preferred Hub identity. A missing match falls back to name and its duplicate aliases. */
9
+ readonly resourceId?: string;
10
+ readonly layer?: 'reflex' | 'short' | 'long';
11
+ readonly input?: Readonly<Record<string, unknown>>;
12
+ readonly reflexPriority?: number;
13
+ readonly cooldownMs?: number;
14
+ readonly rearm?: 'after_clear' | 'after_cooldown';
15
+ readonly maxRuntimeMs?: number;
16
+ }
17
+
18
+ export interface ClawclawReactionManifest {
19
+ readonly apiVersion: 2;
20
+ readonly kind: 'reaction';
21
+ readonly id: string;
22
+ readonly name?: string;
23
+ readonly description: string;
24
+ readonly localizations?: ClawclawResourceLocalizations;
25
+ /** Backend, derived and visual Events. Reaction outputs cannot feed reactions. */
26
+ readonly consumes: readonly string[];
27
+ /** Own Agent-visible notices, named reaction.<name>. */
28
+ readonly produces: readonly string[];
29
+ readonly behaviors?: Readonly<Record<string, ReactionBehaviorBinding>>;
30
+ readonly inputs?: readonly BehaviorInputFieldSpec[];
31
+ readonly config?: Readonly<Record<string, unknown>>;
32
+ }
33
+
34
+ export interface ReactionFact extends GameEvent {
35
+ readonly wakeAgent?: boolean;
36
+ readonly replayed: boolean;
37
+ }
38
+
39
+ export interface ReactionInput {
40
+ readonly moduleId: string;
41
+ readonly scope: Readonly<{ sessionId: string; segmentId: string }>;
42
+ readonly gameState: Readonly<GameState> | null;
43
+ readonly frame: Readonly<ObservationFrame> | null;
44
+ readonly events: readonly ReactionFact[];
45
+ readonly nowMs: number;
46
+ /** JSON data returned by the previous evaluate; do not keep state in class fields. */
47
+ readonly state: unknown;
48
+ readonly config: Readonly<Record<string, unknown>>;
49
+ readonly availableBehaviors: readonly string[];
50
+ }
51
+
52
+ export interface ReactionEvent {
53
+ readonly type: string;
54
+ /** Stable identity for this occurrence, not a new key on every frame. */
55
+ readonly key: string;
56
+ readonly wakeAgent?: boolean;
57
+ readonly payload: Readonly<Record<string, unknown>> & { readonly text: string };
58
+ }
59
+
60
+ export interface ReactionActivation {
61
+ /** Key in manifest.behaviors; never an arbitrary executable command. */
62
+ readonly behavior: string;
63
+ readonly input?: Readonly<Record<string, unknown>>;
64
+ readonly reason?: string;
65
+ }
66
+
67
+ export interface ReactionResult {
68
+ readonly state?: unknown;
69
+ readonly events?: readonly ReactionEvent[];
70
+ /** Return while the condition holds. Omitting a binding clears its after_clear latch. */
71
+ readonly activations?: readonly ReactionActivation[];
72
+ }
73
+
74
+ /** A perception resource that can notify the Agent and request fixed Behaviors. */
75
+ export abstract class ClawclawReaction {
76
+ static readonly manifest?: ClawclawReactionManifest;
77
+ abstract evaluate(input: Readonly<ReactionInput>): ReactionResult | Promise<ReactionResult>;
78
+ }