@myclaw163/clawclaw-cli 0.8.12 → 0.8.14

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 (68) hide show
  1. package/docs/configuration.md +11 -12
  2. package/docs/custom-modules/README.md +3 -3
  3. package/docs/custom-modules/behavior.md +1 -1
  4. package/docs/custom-modules/perception.md +1 -1
  5. package/docs/custom-modules/trigger.md +1 -1
  6. package/docs/runtime/official-perception-modules.md +1 -1
  7. package/package.json +1 -1
  8. package/scripts/check-skill-command-surface.mjs +1 -0
  9. package/scripts/sync-bundled-skill.mjs +89 -52
  10. package/skills/clawclaw/SKILL.md +10 -6
  11. package/skills/clawclaw/references/BEHAVIORS.md +2 -0
  12. package/skills/clawclaw/references/CHANNELS.md +1 -1
  13. package/skills/clawclaw/references/COMMANDS.md +11 -4
  14. package/skills/clawclaw/references/HUB.md +4 -2
  15. package/skills/clawclaw/references/STREAM.md +4 -2
  16. package/src/cli.ts +2 -0
  17. package/src/commands/behavior.ts +49 -8
  18. package/src/commands/capability.ts +345 -0
  19. package/src/commands/load.ts +195 -8
  20. package/src/commands/perception.ts +21 -7
  21. package/src/commands/persona.ts +6 -0
  22. package/src/commands/trigger.ts +8 -5
  23. package/src/lib/agent-workspace-skills.ts +167 -68
  24. package/src/lib/agent-workspace-sync.ts +20 -31
  25. package/src/lib/capability-cloud-refresh.ts +313 -0
  26. package/src/lib/capability-cloud-sync.ts +315 -0
  27. package/src/lib/capability-discovery.ts +94 -0
  28. package/src/lib/capability-workspace.ts +291 -0
  29. package/src/lib/game-client.ts +12 -4
  30. package/src/lib/hub-client.ts +131 -4
  31. package/src/lib/persona.ts +1 -24
  32. package/src/lib/skill-host.ts +63 -0
  33. package/src/lib/user-data.ts +47 -59
  34. package/src/lib/zip-store.ts +92 -0
  35. package/src/pipeline/event-format.ts +5 -0
  36. package/src/pipeline/event-hints.ts +10 -0
  37. package/src/runtime/clawclaw/agent-event-policy.ts +2 -0
  38. package/src/runtime/clawclaw/behavior-input.ts +5 -0
  39. package/src/runtime/clawclaw/owner.ts +12 -12
  40. package/src/runtime/clawclaw/plugins/action/action-plugin.ts +150 -18
  41. package/src/runtime/clawclaw/plugins/behavior/behavior-action.ts +8 -0
  42. package/src/runtime/clawclaw/plugins/behavior/behavior-decision-history.ts +88 -0
  43. package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +173 -11
  44. package/src/runtime/clawclaw/plugins/behavior/behavior-runner.ts +6 -0
  45. package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.ts +3 -1
  46. package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +5 -1
  47. package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-worker.ts +5 -5
  48. package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +213 -6
  49. package/src/runtime/clawclaw/plugins/source/game-transport.ts +29 -4
  50. package/src/runtime/clawclaw/plugins/source/source-plugin.ts +153 -0
  51. package/src/runtime/clawclaw/plugins/source/types.ts +1 -0
  52. package/src/runtime/clawclaw/runtime.ts +31 -3
  53. package/src/runtime/clawclaw-event-catalog.ts +2 -0
  54. package/src/runtime/framework/events/event-repository.ts +1 -1
  55. package/src/runtime/framework/events/memory-event-repository.ts +59 -1
  56. package/src/runtime/framework/events/sqlite-event-repository.ts +23 -1
  57. package/src/runtime/framework/mailbox/types.ts +2 -0
  58. package/src/runtime/framework/persistence/async-sqlite-persistence-worker.ts +58 -0
  59. package/src/runtime/framework/persistence/memory-first-persistence.ts +380 -0
  60. package/src/runtime/framework/plugins/contracts.ts +2 -2
  61. package/src/runtime/framework/plugins/runtime-input-journal.ts +173 -17
  62. package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +23 -11
  63. package/src/runtime/framework/runtime.ts +57 -4
  64. package/src/runtime/framework/state/history/contracts.ts +9 -1
  65. package/src/runtime/framework/state/history/memory-state-history-repository.ts +49 -7
  66. package/src/runtime/framework/state/history/sqlite-state-history-repository.ts +16 -0
  67. package/src/sdk/behavior-plugin/index.ts +5 -24
  68. package/src/sdk/module-input/index.ts +47 -0
@@ -6,25 +6,40 @@ import {
6
6
  resolveLocalAgentWorkspacePath,
7
7
  type LocalAgentWorkspaceSnapshot,
8
8
  } from './agent-workspace-sync.js';
9
- import { getUserDataHubDir } from './user-data.js';
9
+ import {
10
+ capabilityWorkspacePath,
11
+ readCapabilityWorkspaceRegistry,
12
+ registerDiscoveredCapability,
13
+ } from './capability-workspace.js';
14
+ import {
15
+ getUserDataAccountCapabilitiesDir,
16
+ getUserDataAccountSkillsDir,
17
+ } from './user-data.js';
10
18
  import {
11
19
  agentSkillTargets,
12
20
  hashAgentSkillDirectory,
21
+ linkAgentSkillTarget,
13
22
  overwriteAgentSkillTarget,
14
23
  readSkillName,
24
+ resolveBundledClawclawSkill,
25
+ unlinkAgentSkillTarget,
15
26
  type SkillHostMutationResult,
16
27
  } from './skill-host.js';
17
28
 
18
29
  interface ActiveSkillEntry {
19
- resourceId: string;
20
- versionId: string;
21
- contentDigest: string;
22
- sourcePath: string;
23
- sourceDigest: string;
24
- targetBaselines: Record<string, string>;
30
+ name: string;
31
+ kind: 'official' | 'hub' | 'local';
32
+ canonicalPath: string;
33
+ sourceDigest?: string;
34
+ remoteDigest?: string;
35
+ resourceId?: string;
36
+ versionId?: string;
25
37
  }
26
38
 
27
- type ActiveSkillManifest = Record<string, ActiveSkillEntry>;
39
+ interface ActiveSkillManifest {
40
+ format: 2;
41
+ entries: Record<string, ActiveSkillEntry>;
42
+ }
28
43
 
29
44
  export interface AgentWorkspaceSkillChanges {
30
45
  added: string[];
@@ -41,27 +56,38 @@ export interface AgentWorkspaceSkillDependencies {
41
56
  targets?: (name: string) => readonly string[];
42
57
  hashDirectory?: (path: string) => string | undefined;
43
58
  overwriteTarget?: (sourcePath: string, targetPath: string) => SkillHostMutationResult;
59
+ linkTarget?: (sourcePath: string, targetPath: string) => SkillHostMutationResult;
60
+ unlinkTarget?: (targetPath: string) => SkillHostMutationResult;
61
+ bundledSkill?: () => string;
44
62
  }
45
63
 
46
- function manifestPath(workspaceDir: string): string {
47
- return join(getUserDataHubDir(workspaceDir), 'active-agent-skills.json');
64
+ function manifestPath(
65
+ workspaceDir: string,
66
+ profile: Pick<AuthProfile, 'apiKey'>,
67
+ ): string {
68
+ return join(getUserDataAccountCapabilitiesDir(workspaceDir, profile), 'skill-links.json');
48
69
  }
49
70
 
50
- function readManifest(workspaceDir: string): ActiveSkillManifest {
51
- const path = manifestPath(workspaceDir);
52
- if (!existsSync(path)) return {};
71
+ function readManifest(
72
+ workspaceDir: string,
73
+ profile: Pick<AuthProfile, 'apiKey'>,
74
+ ): ActiveSkillManifest {
75
+ const path = manifestPath(workspaceDir, profile);
76
+ if (!existsSync(path)) return { format: 2, entries: {} };
53
77
  try {
54
- const value = JSON.parse(readFileSync(path, 'utf8'));
55
- return value && typeof value === 'object' && !Array.isArray(value)
56
- ? value as ActiveSkillManifest
57
- : {};
78
+ const value = JSON.parse(readFileSync(path, 'utf8')) as ActiveSkillManifest;
79
+ return value.format === 2 && value.entries ? value : { format: 2, entries: {} };
58
80
  } catch {
59
- return {};
81
+ return { format: 2, entries: {} };
60
82
  }
61
83
  }
62
84
 
63
- function writeManifest(workspaceDir: string, value: ActiveSkillManifest): void {
64
- const target = manifestPath(workspaceDir);
85
+ function writeManifest(
86
+ workspaceDir: string,
87
+ profile: Pick<AuthProfile, 'apiKey'>,
88
+ value: ActiveSkillManifest,
89
+ ): void {
90
+ const target = manifestPath(workspaceDir, profile);
65
91
  mkdirSync(dirname(target), { recursive: true });
66
92
  const temporary = `${target}.${randomUUID()}.tmp`;
67
93
  writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
@@ -78,10 +104,27 @@ export function syncAgentWorkspaceSkills(
78
104
  snapshot: LocalAgentWorkspaceSnapshot,
79
105
  dependencies: AgentWorkspaceSkillDependencies = {},
80
106
  ): AgentWorkspaceSkillChanges {
81
- const current = readManifest(workspaceDir);
82
- const desired: Record<string, Omit<ActiveSkillEntry, 'targetBaselines'>> = {};
107
+ const current = readManifest(workspaceDir, profile);
108
+ const next: ActiveSkillManifest = { format: 2, entries: {} };
83
109
  const failed: AgentWorkspaceSkillChanges['failed'] = [];
84
110
  const hashDirectory = dependencies.hashDirectory ?? hashAgentSkillDirectory;
111
+ const overwriteTarget = dependencies.overwriteTarget ?? overwriteAgentSkillTarget;
112
+ const linkTarget = dependencies.linkTarget ?? linkAgentSkillTarget;
113
+ const unlinkTarget = dependencies.unlinkTarget ?? unlinkAgentSkillTarget;
114
+ const targets = dependencies.targets ?? agentSkillTargets;
115
+ const skillsRoot = getUserDataAccountSkillsDir(workspaceDir, profile);
116
+ mkdirSync(skillsRoot, { recursive: true });
117
+
118
+ const desired = new Map<string, {
119
+ kind: ActiveSkillEntry['kind'];
120
+ sourcePath: string;
121
+ resourceId?: string;
122
+ versionId?: string;
123
+ remoteDigest?: string;
124
+ publisher?: 'official' | 'self' | 'community';
125
+ }>();
126
+ const bundled = (dependencies.bundledSkill ?? resolveBundledClawclawSkill)();
127
+ desired.set('clawclaw', { kind: 'official', sourcePath: bundled });
85
128
 
86
129
  for (const item of snapshot.items.skill) {
87
130
  if (!item.skillRootPath) continue;
@@ -90,81 +133,137 @@ export function syncAgentWorkspaceSkills(
90
133
  if (!name || !/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(name) || name === 'clawclaw') {
91
134
  failed.push({
92
135
  name: name ?? item.resourceId,
93
- action: current[name ?? ''] ? 'update' : 'add',
136
+ action: current.entries[name ?? ''] ? 'update' : 'add',
94
137
  error: name === 'clawclaw'
95
- ? 'built-in clawclaw skill cannot be replaced'
138
+ ? 'Hub resource cannot replace the official clawclaw Skill'
96
139
  : 'SKILL.md name is missing or unsafe',
97
140
  });
98
141
  continue;
99
142
  }
100
- if (desired[name]) {
143
+ if (desired.has(name)) {
101
144
  failed.push({ name, action: 'add', error: 'duplicate selected skill name' });
102
145
  continue;
103
146
  }
104
- desired[name] = {
147
+ desired.set(name, {
148
+ kind: 'hub',
149
+ sourcePath: path,
105
150
  resourceId: item.resourceId,
106
151
  versionId: item.versionId,
107
- contentDigest: item.contentDigest,
108
- sourcePath: path,
109
- sourceDigest: hashDirectory(path) ?? '',
110
- };
111
- if (!desired[name].sourceDigest) {
112
- delete desired[name];
113
- failed.push({ name, action: current[name] ? 'update' : 'add', error: 'Hub Skill package is unreadable' });
152
+ remoteDigest: hashDirectory(path),
153
+ publisher: item.hub?.publisher ?? 'community',
154
+ });
155
+ }
156
+
157
+ for (const entry of Object.values(readCapabilityWorkspaceRegistry(workspaceDir, profile).entries)) {
158
+ if (entry.type !== 'skill' || entry.origin !== 'local' || entry.selection !== 'equipped') continue;
159
+ const path = capabilityWorkspacePath(workspaceDir, profile, entry);
160
+ if (desired.has(entry.runtimeId)) {
161
+ failed.push({ name: entry.runtimeId, action: 'add', error: 'local and Hub Skill names conflict' });
162
+ continue;
114
163
  }
164
+ desired.set(entry.runtimeId, { kind: 'local', sourcePath: path });
115
165
  }
116
166
 
117
- const next: ActiveSkillManifest = { ...current };
118
167
  const added: string[] = [];
119
168
  const updated: string[] = [];
120
169
  const removed: string[] = [];
121
170
  const unchanged: string[] = [];
122
171
  const preservedModified: string[] = [];
123
- const targets = dependencies.targets ?? agentSkillTargets;
124
- const overwriteTarget = dependencies.overwriteTarget ?? overwriteAgentSkillTarget;
125
172
 
126
- for (const [name, entry] of Object.entries(desired)) {
127
- const active = current[name];
128
- const action = active ? 'update' : 'add';
129
- const legacyBaseline = active?.sourceDigest
130
- || (active?.sourcePath ? hashDirectory(active.sourcePath) : undefined);
131
- const targetBaselines = { ...(active?.targetBaselines ?? {}) };
132
- let installedTargets = 0;
133
- let updatedTargets = 0;
134
- let currentTargets = 0;
173
+ for (const [name, item] of desired) {
174
+ const canonical = join(skillsRoot, name);
175
+ const previous = current.entries[name];
176
+ const sourceDigest = hashDirectory(item.sourcePath);
177
+ if (!sourceDigest) {
178
+ failed.push({ name, action: previous ? 'update' : 'add', error: 'Skill source is unreadable' });
179
+ continue;
180
+ }
181
+ const canonicalDigest = hashDirectory(canonical);
182
+ let applied = false;
135
183
  let preserved = false;
136
- for (const rawTarget of targets(name)) {
137
- const target = resolve(rawTarget);
138
- const localDigest = hashDirectory(target);
139
- const baseline = targetBaselines[target] ?? legacyBaseline;
140
- if (localDigest === entry.sourceDigest) {
141
- targetBaselines[target] = entry.sourceDigest;
142
- currentTargets += 1;
143
- continue;
184
+ if (item.kind === 'local' && resolve(item.sourcePath) === resolve(canonical)) {
185
+ applied = true;
186
+ } else if (item.kind === 'official') {
187
+ if (canonicalDigest !== sourceDigest) {
188
+ const result = overwriteTarget(item.sourcePath, canonical);
189
+ if (!result.ok) {
190
+ failed.push({ name, action: previous ? 'update' : 'add', error: resultError(result) });
191
+ continue;
192
+ }
193
+ applied = true;
144
194
  }
145
- if (localDigest !== undefined && (baseline === undefined || localDigest !== baseline)) {
146
- preserved = true;
195
+ } else if (canonicalDigest === sourceDigest) {
196
+ applied = true;
197
+ } else if (canonicalDigest === undefined || canonicalDigest === previous?.sourceDigest) {
198
+ const result = overwriteTarget(item.sourcePath, canonical);
199
+ if (!result.ok) {
200
+ failed.push({ name, action: previous ? 'update' : 'add', error: resultError(result) });
147
201
  continue;
148
202
  }
149
- const result = overwriteTarget(entry.sourcePath, target);
203
+ applied = true;
204
+ } else {
205
+ preserved = true;
206
+ preservedModified.push(name);
207
+ }
208
+
209
+ const effectiveDigest = hashDirectory(canonical);
210
+ if (!effectiveDigest) {
211
+ failed.push({ name, action: previous ? 'update' : 'add', error: 'Canonical Skill is unavailable' });
212
+ continue;
213
+ }
214
+ let linkFailed = false;
215
+ for (const target of targets(name)) {
216
+ const result = linkTarget(canonical, target);
150
217
  if (!result.ok) {
151
- failed.push({ name, action, error: resultError(result) });
152
- continue;
218
+ linkFailed = true;
219
+ failed.push({ name, action: previous ? 'update' : 'add', error: resultError(result) });
153
220
  }
154
- targetBaselines[target] = entry.sourceDigest;
155
- if (localDigest === undefined) installedTargets += 1;
156
- else updatedTargets += 1;
157
221
  }
158
- next[name] = { ...entry, targetBaselines };
159
- if (!active && installedTargets > 0) added.push(name);
160
- else if (updatedTargets > 0) updated.push(name);
161
- else if (!preserved && failed.every((item) => item.name !== name) && currentTargets > 0) {
162
- unchanged.push(name);
222
+ if (linkFailed) continue;
223
+ registerDiscoveredCapability(workspaceDir, profile, {
224
+ type: 'skill',
225
+ path: canonical,
226
+ runtimeId: name,
227
+ title: name,
228
+ description: `Skill ${name}`,
229
+ selectedByHub: item.kind !== 'local',
230
+ ...(item.kind === 'hub' && item.resourceId && item.versionId ? {
231
+ hub: {
232
+ resourceId: item.resourceId,
233
+ versionId: item.versionId,
234
+ publisher: item.publisher ?? 'community',
235
+ baseSha256: item.remoteDigest,
236
+ },
237
+ } : {}),
238
+ });
239
+ next.entries[name] = {
240
+ name,
241
+ kind: item.kind,
242
+ canonicalPath: canonical,
243
+ sourceDigest: preserved ? previous?.sourceDigest : sourceDigest,
244
+ remoteDigest: item.remoteDigest,
245
+ resourceId: item.resourceId,
246
+ versionId: item.versionId,
247
+ };
248
+ if (!previous) added.push(name);
249
+ else if (applied && previous.sourceDigest !== sourceDigest) updated.push(name);
250
+ else unchanged.push(name);
251
+ }
252
+
253
+ for (const [name] of Object.entries(current.entries)) {
254
+ if (desired.has(name)) continue;
255
+ let failedRemoval = false;
256
+ for (const target of targets(name)) {
257
+ const result = unlinkTarget(target);
258
+ if (!result.ok) {
259
+ failedRemoval = true;
260
+ failed.push({ name, action: 'remove', error: resultError(result) });
261
+ }
163
262
  }
164
- if (preserved) preservedModified.push(name);
263
+ if (!failedRemoval) removed.push(name);
165
264
  }
166
265
 
167
- writeManifest(workspaceDir, next);
266
+ writeManifest(workspaceDir, profile, next);
168
267
  return {
169
268
  added,
170
269
  updated,
@@ -29,8 +29,11 @@ import {
29
29
  getUserDataAccountHubBehaviorsDir,
30
30
  getUserDataAccountHubPerceptionsDir,
31
31
  getUserDataAccountHubPersonasDir,
32
- getUserDataPersonaFile,
33
32
  } from './user-data.js';
33
+ import {
34
+ capabilityEntryForHubResource,
35
+ capabilityWorkspacePath,
36
+ } from './capability-workspace.js';
34
37
 
35
38
  const RESOURCE_TYPES: readonly ResourceType[] = ['skill', 'strategy', 'persona', 'perception'];
36
39
  const SHA256 = /^[0-9a-f]{64}$/;
@@ -76,7 +79,7 @@ export interface LocalAgentWorkspaceItem {
76
79
 
77
80
  export interface LocalAgentWorkspaceSnapshot {
78
81
  metadataVersion?: 1;
79
- layoutVersion?: 2;
82
+ layoutVersion?: 3;
80
83
  revision: number;
81
84
  syncedAt: string;
82
85
  relativeDir: string;
@@ -553,9 +556,7 @@ function inspectLocalSnapshot(
553
556
  );
554
557
  item.workingCopyPath = accountRelativePath(workspaceDir, profile, workingPath);
555
558
  if (!existsSync(workingPath)) {
556
- const legacyPath = type === 'persona' && existsSync(getUserDataPersonaFile(workspaceDir, profile))
557
- ? getUserDataPersonaFile(workspaceDir, profile)
558
- : projectionPath;
559
+ const legacyPath = projectionPath;
559
560
  if (existsSync(legacyPath)) writeFileAtomically(workingPath, readFileSync(legacyPath));
560
561
  }
561
562
  }
@@ -672,7 +673,7 @@ export async function syncAgentWorkspaceConfig(
672
673
  if (
673
674
  previous?.revision === remote.revision
674
675
  && previous.metadataVersion === 1
675
- && previous.layoutVersion === 2
676
+ && previous.layoutVersion === 3
676
677
  && inspected?.projectionOutdated !== true
677
678
  && inspected?.workingMetadataMissing !== true
678
679
  ) {
@@ -708,13 +709,22 @@ export async function syncAgentWorkspaceConfig(
708
709
  let workingPath: string | undefined;
709
710
  let persistedWorkingMetadata: WorkingCopyMetadata | undefined;
710
711
  if (type !== 'skill' && materialized.sourceFiles[0]) {
711
- workingPath = hubWorkingCopyPath(
712
+ const workspaceType = type === 'strategy' ? 'behavior' : type;
713
+ const registered = capabilityEntryForHubResource(
712
714
  workspaceDir,
713
715
  profile,
714
- type,
716
+ workspaceType,
715
717
  materialized.resourceId,
716
- materialized.sourceFiles[0].relativePath,
717
718
  );
719
+ workingPath = registered
720
+ ? capabilityWorkspacePath(workspaceDir, profile, registered)
721
+ : hubWorkingCopyPath(
722
+ workspaceDir,
723
+ profile,
724
+ type,
725
+ materialized.resourceId,
726
+ materialized.sourceFiles[0].relativePath,
727
+ );
718
728
  materialized.workingCopyPath = accountRelativePath(workspaceDir, profile, workingPath);
719
729
  persistedWorkingMetadata = readWorkingCopyMetadata(
720
730
  workingCopyMetadataPath(workspaceDir, profile, type, materialized.resourceId),
@@ -746,27 +756,6 @@ export async function syncAgentWorkspaceConfig(
746
756
  };
747
757
  }
748
758
  }
749
- if (
750
- !override
751
- && !previous
752
- && type === 'persona'
753
- && existsSync(getUserDataPersonaFile(workspaceDir, profile))
754
- && materialized.sourceFiles[0]
755
- ) {
756
- const personaBytes = readFileSync(getUserDataPersonaFile(workspaceDir, profile));
757
- const personaSha = sha256(personaBytes);
758
- if (personaBytes.toString('utf8').trim() && personaSha !== materialized.sourceFiles[0].sha256) {
759
- override = {
760
- bytes: personaBytes,
761
- sha256: personaSha,
762
- contentDigest: singleFileContentDigest(
763
- materialized.sourceFiles[0].relativePath,
764
- personaBytes,
765
- ),
766
- modifiedAtMs: statSync(getUserDataPersonaFile(workspaceDir, profile)).mtimeMs,
767
- };
768
- }
769
- }
770
759
  let effectiveBytes: Buffer | undefined;
771
760
  if (
772
761
  override
@@ -830,7 +819,7 @@ export async function syncAgentWorkspaceConfig(
830
819
  const relativeDir = `revision-${remote.revision}-${randomUUID()}`;
831
820
  const snapshot: LocalAgentWorkspaceSnapshot = {
832
821
  metadataVersion: 1,
833
- layoutVersion: 2,
822
+ layoutVersion: 3,
834
823
  revision: remote.revision,
835
824
  syncedAt: (dependencies.now ?? (() => new Date()))().toISOString(),
836
825
  relativeDir,