@myclaw163/clawclaw-cli 0.8.11 → 0.8.13

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.
@@ -1,11 +1,9 @@
1
1
  import {
2
- cpSync,
3
2
  copyFileSync,
4
3
  existsSync,
5
4
  mkdirSync,
6
5
  readdirSync,
7
6
  readFileSync,
8
- statSync,
9
7
  writeFileSync,
10
8
  } from 'fs';
11
9
  import { createHash } from 'crypto';
@@ -100,67 +98,88 @@ export function getUserDataAccountLocalDir(
100
98
  return join(getUserDataAccountDir(workspaceDir, profile), 'local');
101
99
  }
102
100
 
101
+ export function getUserDataAccountCapabilitiesDir(
102
+ workspaceDir: string,
103
+ profile: Pick<AuthProfile, 'apiKey'>,
104
+ ): string {
105
+ return join(getUserDataAccountDir(workspaceDir, profile), 'capabilities');
106
+ }
107
+
108
+ export function getUserDataAccountDraftsDir(
109
+ workspaceDir: string,
110
+ profile: Pick<AuthProfile, 'apiKey'>,
111
+ ): string {
112
+ return join(getUserDataAccountDir(workspaceDir, profile), 'drafts');
113
+ }
114
+
115
+ export function getUserDataAccountCapabilityRegistryFile(
116
+ workspaceDir: string,
117
+ profile: Pick<AuthProfile, 'apiKey'>,
118
+ ): string {
119
+ return join(getUserDataAccountCapabilitiesDir(workspaceDir, profile), 'registry.json');
120
+ }
121
+
103
122
  export function getUserDataAccountBehaviorsRoot(
104
123
  workspaceDir: string,
105
124
  profile: Pick<AuthProfile, 'apiKey'>,
106
125
  ): string {
107
- return join(getUserDataAccountDir(workspaceDir, profile), 'behaviors');
126
+ return join(getUserDataAccountCapabilitiesDir(workspaceDir, profile), 'behaviors');
108
127
  }
109
128
 
110
129
  export function getUserDataAccountBehaviorsDir(
111
130
  workspaceDir: string,
112
131
  profile: Pick<AuthProfile, 'apiKey'>,
113
132
  ): string {
114
- return join(getUserDataAccountBehaviorsRoot(workspaceDir, profile), 'local');
133
+ return getUserDataAccountBehaviorsRoot(workspaceDir, profile);
115
134
  }
116
135
 
117
136
  export function getUserDataAccountHubBehaviorsDir(
118
137
  workspaceDir: string,
119
138
  profile: Pick<AuthProfile, 'apiKey'>,
120
139
  ): string {
121
- return join(getUserDataAccountBehaviorsRoot(workspaceDir, profile), 'hub');
140
+ return getUserDataAccountBehaviorsRoot(workspaceDir, profile);
122
141
  }
123
142
 
124
143
  export function getUserDataAccountPerceptionsRoot(
125
144
  workspaceDir: string,
126
145
  profile: Pick<AuthProfile, 'apiKey'>,
127
146
  ): string {
128
- return join(getUserDataAccountDir(workspaceDir, profile), 'perceptions');
147
+ return join(getUserDataAccountCapabilitiesDir(workspaceDir, profile), 'perceptions');
129
148
  }
130
149
 
131
150
  export function getUserDataAccountPerceptionsDir(
132
151
  workspaceDir: string,
133
152
  profile: Pick<AuthProfile, 'apiKey'>,
134
153
  ): string {
135
- return join(getUserDataAccountPerceptionsRoot(workspaceDir, profile), 'local');
154
+ return getUserDataAccountPerceptionsRoot(workspaceDir, profile);
136
155
  }
137
156
 
138
157
  export function getUserDataAccountHubPerceptionsDir(
139
158
  workspaceDir: string,
140
159
  profile: Pick<AuthProfile, 'apiKey'>,
141
160
  ): string {
142
- return join(getUserDataAccountPerceptionsRoot(workspaceDir, profile), 'hub');
161
+ return getUserDataAccountPerceptionsRoot(workspaceDir, profile);
143
162
  }
144
163
 
145
164
  export function getUserDataAccountPersonasRoot(
146
165
  workspaceDir: string,
147
166
  profile: Pick<AuthProfile, 'apiKey'>,
148
167
  ): string {
149
- return join(getUserDataAccountDir(workspaceDir, profile), 'personas');
168
+ return join(getUserDataAccountCapabilitiesDir(workspaceDir, profile), 'personas');
150
169
  }
151
170
 
152
171
  export function getUserDataAccountPersonasDir(
153
172
  workspaceDir: string,
154
173
  profile: Pick<AuthProfile, 'apiKey'>,
155
174
  ): string {
156
- return join(getUserDataAccountPersonasRoot(workspaceDir, profile), 'local');
175
+ return getUserDataAccountPersonasRoot(workspaceDir, profile);
157
176
  }
158
177
 
159
178
  export function getUserDataAccountHubPersonasDir(
160
179
  workspaceDir: string,
161
180
  profile: Pick<AuthProfile, 'apiKey'>,
162
181
  ): string {
163
- return join(getUserDataAccountPersonasRoot(workspaceDir, profile), 'hub');
182
+ return getUserDataAccountPersonasRoot(workspaceDir, profile);
164
183
  }
165
184
 
166
185
  export function getUserDataAccountPersonaSelectionFile(
@@ -174,14 +193,21 @@ export function getUserDataAccountTriggersRoot(
174
193
  workspaceDir: string,
175
194
  profile: Pick<AuthProfile, 'apiKey'>,
176
195
  ): string {
177
- return join(getUserDataAccountDir(workspaceDir, profile), 'triggers');
196
+ return join(getUserDataAccountCapabilitiesDir(workspaceDir, profile), 'triggers');
178
197
  }
179
198
 
180
199
  export function getUserDataAccountTriggersDir(
181
200
  workspaceDir: string,
182
201
  profile: Pick<AuthProfile, 'apiKey'>,
183
202
  ): string {
184
- return join(getUserDataAccountTriggersRoot(workspaceDir, profile), 'local');
203
+ return getUserDataAccountTriggersRoot(workspaceDir, profile);
204
+ }
205
+
206
+ export function getUserDataAccountSkillsDir(
207
+ workspaceDir: string,
208
+ profile: Pick<AuthProfile, 'apiKey'>,
209
+ ): string {
210
+ return join(getUserDataAccountCapabilitiesDir(workspaceDir, profile), 'skills');
185
211
  }
186
212
 
187
213
  export function getUserDataAccountGameplayConfigFile(
@@ -245,30 +271,6 @@ function writeJson(path: string, data: unknown): void {
245
271
  writeFileSync(path, JSON.stringify(data, null, 2) + '\n', 'utf8');
246
272
  }
247
273
 
248
- function contentHash(path: string): string {
249
- return createHash('sha256').update(readFileSync(path)).digest('hex').slice(0, 16);
250
- }
251
-
252
- function copyLegacyLocalModules(from: string, to: string): void {
253
- if (!existsSync(from)) return;
254
- const manifest = readJson<Record<string, { installedHash?: string }>>(
255
- join(from, '.official-manifest.json'),
256
- ) ?? {};
257
- for (const entry of readdirSync(from)) {
258
- if (entry === '.official-manifest.json') continue;
259
- const source = join(from, entry);
260
- const target = join(to, entry);
261
- if (statSync(source).isDirectory()) {
262
- cpSync(source, target, { recursive: true, force: false, errorOnExist: false });
263
- continue;
264
- }
265
- const id = entry.replace(/\.(?:ts|js)$/, '');
266
- const tracked = manifest[id];
267
- if (tracked?.installedHash && tracked.installedHash === contentHash(source)) continue;
268
- if (!existsSync(target)) copyFileSync(source, target);
269
- }
270
- }
271
-
272
274
  export function splitAuthData(data: AuthData): {
273
275
  auth: AuthData;
274
276
  runtime: RuntimeAuthState;
@@ -409,9 +411,8 @@ export function ensureUserDataLayout(workspaceDir: string): UserDataMigrationRes
409
411
  }
410
412
 
411
413
  /**
412
- * Materialize the account-scoped type/local + type/hub authoring layout without
413
- * deleting either legacy workspace-scoped or account/local source. The copy is
414
- * intentionally one-shot per account: after migration, accounts evolve independently.
414
+ * Materialize the account-scoped canonical capability workspace. Legacy local/hub
415
+ * trees are deliberately not migrated: the v3 product model has one editable source.
415
416
  */
416
417
  export function ensureAccountUserDataLayout(
417
418
  workspaceDir: string,
@@ -420,7 +421,7 @@ export function ensureAccountUserDataLayout(
420
421
  ensureUserDataLayout(workspaceDir);
421
422
  const result = EMPTY_RESULT();
422
423
  const accountDir = getUserDataAccountDir(workspaceDir, profile);
423
- const marker = join(accountDir, '.capability-layout-v2.json');
424
+ const marker = join(accountDir, '.capability-layout-v3.json');
424
425
  ensureDir(accountDir);
425
426
  ensureDir(getUserDataAccountBehaviorsDir(workspaceDir, profile));
426
427
  ensureDir(getUserDataAccountHubBehaviorsDir(workspaceDir, profile));
@@ -429,26 +430,13 @@ export function ensureAccountUserDataLayout(
429
430
  ensureDir(getUserDataAccountPersonasDir(workspaceDir, profile));
430
431
  ensureDir(getUserDataAccountHubPersonasDir(workspaceDir, profile));
431
432
  ensureDir(getUserDataAccountTriggersDir(workspaceDir, profile));
433
+ ensureDir(getUserDataAccountSkillsDir(workspaceDir, profile));
434
+ ensureDir(getUserDataAccountDraftsDir(workspaceDir, profile));
432
435
  if (existsSync(marker)) return result;
433
-
434
- const copies: Array<[string, string, string]> = [
435
- [join(getUserDataAccountLocalDir(workspaceDir, profile), 'behaviors'), getUserDataAccountBehaviorsDir(workspaceDir, profile), 'account-local/behaviors'],
436
- [join(getUserDataAccountLocalDir(workspaceDir, profile), 'perceptions'), getUserDataAccountPerceptionsDir(workspaceDir, profile), 'account-local/perceptions'],
437
- [join(getUserDataAccountLocalDir(workspaceDir, profile), 'personas'), getUserDataAccountPersonasDir(workspaceDir, profile), 'account-local/personas'],
438
- [join(getUserDataAccountLocalDir(workspaceDir, profile), 'triggers'), getUserDataAccountTriggersDir(workspaceDir, profile), 'account-local/triggers'],
439
- [getUserDataBehaviorsDir(workspaceDir), getUserDataAccountBehaviorsDir(workspaceDir, profile), 'behaviors'],
440
- [getUserDataPerceptionsDir(workspaceDir), getUserDataAccountPerceptionsDir(workspaceDir, profile), 'perceptions'],
441
- [getUserDataTriggersDir(workspaceDir), getUserDataAccountTriggersDir(workspaceDir, profile), 'triggers'],
442
- ];
443
- for (const [from, to, label] of copies) {
444
- if (!existsSync(from)) continue;
445
- copyLegacyLocalModules(from, to);
446
- result.copied.push(`user-data/${label} -> user-data/accounts/<account-hash>/<type>/local`);
447
- }
448
436
  writeJson(marker, {
449
- format: 2,
450
- migrated_at: new Date().toISOString(),
451
- copied: result.copied,
437
+ format: 3,
438
+ created_at: new Date().toISOString(),
439
+ migrated: false,
452
440
  });
453
441
  return result;
454
442
  }
@@ -0,0 +1,92 @@
1
+ import { createHash } from 'node:crypto';
2
+
3
+ export interface ZipStoreEntry {
4
+ path: string;
5
+ content: Buffer;
6
+ }
7
+
8
+ const CRC_TABLE = (() => {
9
+ const table = new Uint32Array(256);
10
+ for (let index = 0; index < 256; index += 1) {
11
+ let value = index;
12
+ for (let bit = 0; bit < 8; bit += 1) {
13
+ value = (value & 1) !== 0 ? 0xedb88320 ^ (value >>> 1) : value >>> 1;
14
+ }
15
+ table[index] = value >>> 0;
16
+ }
17
+ return table;
18
+ })();
19
+
20
+ function crc32(buffer: Buffer): number {
21
+ let crc = 0xffffffff;
22
+ for (const byte of buffer) crc = CRC_TABLE[(crc ^ byte) & 0xff]! ^ (crc >>> 8);
23
+ return (crc ^ 0xffffffff) >>> 0;
24
+ }
25
+
26
+ function normalizedPath(path: string): string {
27
+ const value = path.replaceAll('\\', '/');
28
+ if (
29
+ !value
30
+ || value.startsWith('/')
31
+ || value.split('/').some((segment) => !segment || segment === '.' || segment === '..')
32
+ ) throw new Error('zip_store_path_invalid');
33
+ return value;
34
+ }
35
+
36
+ /** Deterministic uncompressed ZIP, sufficient for Hub Skill packages. */
37
+ export function createStoredZip(entries: readonly ZipStoreEntry[]): Buffer {
38
+ const sorted = [...entries]
39
+ .map((entry) => ({ path: normalizedPath(entry.path), content: Buffer.from(entry.content) }))
40
+ .sort((left, right) => left.path.localeCompare(right.path));
41
+ if (sorted.length === 0 || sorted.length > 0xffff) throw new Error('zip_store_entry_count_invalid');
42
+ const localParts: Buffer[] = [];
43
+ const centralParts: Buffer[] = [];
44
+ let offset = 0;
45
+ for (const entry of sorted) {
46
+ const name = Buffer.from(entry.path, 'utf8');
47
+ const checksum = crc32(entry.content);
48
+ const local = Buffer.alloc(30);
49
+ local.writeUInt32LE(0x04034b50, 0);
50
+ local.writeUInt16LE(20, 4);
51
+ local.writeUInt16LE(0x0800, 6);
52
+ local.writeUInt16LE(0, 8);
53
+ local.writeUInt32LE(checksum, 14);
54
+ local.writeUInt32LE(entry.content.length, 18);
55
+ local.writeUInt32LE(entry.content.length, 22);
56
+ local.writeUInt16LE(name.length, 26);
57
+ localParts.push(local, name, entry.content);
58
+
59
+ const central = Buffer.alloc(46);
60
+ central.writeUInt32LE(0x02014b50, 0);
61
+ central.writeUInt16LE(20, 4);
62
+ central.writeUInt16LE(20, 6);
63
+ central.writeUInt16LE(0x0800, 8);
64
+ central.writeUInt16LE(0, 10);
65
+ central.writeUInt32LE(checksum, 16);
66
+ central.writeUInt32LE(entry.content.length, 20);
67
+ central.writeUInt32LE(entry.content.length, 24);
68
+ central.writeUInt16LE(name.length, 28);
69
+ central.writeUInt32LE(offset, 42);
70
+ centralParts.push(central, name);
71
+ offset += local.length + name.length + entry.content.length;
72
+ }
73
+ const centralDirectory = Buffer.concat(centralParts);
74
+ const end = Buffer.alloc(22);
75
+ end.writeUInt32LE(0x06054b50, 0);
76
+ end.writeUInt16LE(sorted.length, 8);
77
+ end.writeUInt16LE(sorted.length, 10);
78
+ end.writeUInt32LE(centralDirectory.length, 12);
79
+ end.writeUInt32LE(offset, 16);
80
+ return Buffer.concat([...localParts, centralDirectory, end]);
81
+ }
82
+
83
+ export function zipContentDigest(entries: readonly ZipStoreEntry[]): string {
84
+ const hash = createHash('sha256');
85
+ for (const entry of [...entries].sort((left, right) => left.path.localeCompare(right.path))) {
86
+ hash.update(normalizedPath(entry.path));
87
+ hash.update('\0');
88
+ hash.update(entry.content);
89
+ hash.update('\0');
90
+ }
91
+ return hash.digest('hex');
92
+ }
@@ -87,9 +87,6 @@ import {
87
87
  } from './plugins/mailbox/index.js';
88
88
  import { BEHAVIOR_PLUGIN_API_VERSION, resolveClawclawResourceMetadata } from '../../sdk/behavior-plugin/index.js';
89
89
  import { TRIGGER_PLUGIN_API_VERSION } from '../../sdk/trigger-plugin/index.js';
90
- import {
91
- listClawclawBehaviorSources,
92
- } from './plugins/behavior/behavior-source.js';
93
90
  import {
94
91
  readLocalAgentWorkspaceSnapshot,
95
92
  resolveLocalAgentWorkspaceSnapshot,
@@ -115,11 +112,9 @@ import {
115
112
  import {
116
113
  type ClawclawOwnerNotificationHost,
117
114
  } from './notifications.js';
118
- import { listClawclawPerceptionSourcesInDirectory } from './plugins/perception/perception-source.js';
115
+ import { discoverLocalCapabilities } from '../../lib/capability-discovery.js';
119
116
  import {
120
117
  ensureAccountUserDataLayout,
121
- getUserDataAccountBehaviorsDir,
122
- getUserDataAccountPerceptionsDir,
123
118
  getUserDataAccountTriggersDir,
124
119
  } from '../../lib/user-data.js';
125
120
 
@@ -733,13 +728,16 @@ export async function runClawclawOwner(
733
728
  });
734
729
  };
735
730
  const disabledBehaviors = new Set(gameplayConfig.disabled_behaviors);
731
+ const discoveredLocalCapabilities = discoverLocalCapabilities(workspaceDir, options.profile);
736
732
  const agentBehaviorSources = agentWorkspace
737
733
  ? listAgentWorkspaceBehaviorSources(workspaceDir, options.profile, agentWorkspace)
738
734
  : [];
739
735
  const behaviorSources = mergeAgentWorkspaceBehaviorSources(
740
- listClawclawBehaviorSources(
741
- getUserDataAccountBehaviorsDir(workspaceDir, options.profile),
742
- ).filter((source) => !disabledBehaviors.has(source.id)),
736
+ discoveredLocalCapabilities.behaviors
737
+ .filter(({ source, entry }) => (
738
+ entry.selection === 'equipped' && !disabledBehaviors.has(source.id)
739
+ ))
740
+ .map(({ source }) => source),
743
741
  agentBehaviorSources,
744
742
  );
745
743
  const behaviorSourceById = new Map(behaviorSources.map((source) => [source.id, source]));
@@ -767,9 +765,11 @@ export async function runClawclawOwner(
767
765
  const agentPerceptionSources = agentWorkspace
768
766
  ? listAgentWorkspacePerceptionSources(workspaceDir, options.profile, agentWorkspace)
769
767
  : [];
770
- const localPerceptionSources = listClawclawPerceptionSourcesInDirectory(
771
- getUserDataAccountPerceptionsDir(workspaceDir, options.profile),
772
- ).filter((source) => !disabledPerceptions.has(source.id));
768
+ const localPerceptionSources = discoveredLocalCapabilities.perceptions
769
+ .filter(({ source, entry }) => (
770
+ entry.selection === 'equipped' && !disabledPerceptions.has(source.id)
771
+ ))
772
+ .map(({ source }) => source);
773
773
  const perceptionSources = mergeAgentWorkspacePerceptionSources(
774
774
  localPerceptionSources,
775
775
  agentPerceptionSources,
@@ -147,7 +147,9 @@ export function composeClawclawMonitorNotification(
147
147
  details_required: detailsRequired.length > 0 ? detailsRequired : undefined,
148
148
  backlog,
149
149
  guidance: backlog
150
- ? `Monitor delegated ${backlog.remaining_events} additional Events to the durable backlog. Unless this notification contains speech_your_turn (speak first), run ccl events now before continuing.`
150
+ ? types.includes('speech_your_turn')
151
+ ? `Submit ccl speak now before any query or narration, then run ccl events to inspect the ${backlog.remaining_events} delegated backlog Events.`
152
+ : `Monitor delegated ${backlog.remaining_events} additional Events to the durable backlog. Run ccl events now before continuing.`
151
153
  : undefined,
152
154
  truncated: backlog ? true : undefined,
153
155
  }));