@myclaw163/clawclaw-cli 0.8.12 → 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.
- package/docs/configuration.md +11 -12
- package/docs/custom-modules/README.md +3 -3
- package/docs/custom-modules/behavior.md +1 -1
- package/docs/custom-modules/perception.md +1 -1
- package/docs/custom-modules/trigger.md +1 -1
- package/docs/runtime/official-perception-modules.md +1 -1
- package/package.json +1 -1
- package/scripts/check-skill-command-surface.mjs +1 -0
- package/scripts/sync-bundled-skill.mjs +89 -52
- package/skills/clawclaw/SKILL.md +7 -3
- package/skills/clawclaw/references/BEHAVIORS.md +2 -0
- package/skills/clawclaw/references/COMMANDS.md +9 -2
- package/skills/clawclaw/references/HUB.md +4 -2
- package/src/cli.ts +2 -0
- package/src/commands/behavior.ts +49 -8
- package/src/commands/capability.ts +345 -0
- package/src/commands/load.ts +195 -8
- package/src/commands/perception.ts +21 -7
- package/src/commands/persona.ts +6 -0
- package/src/commands/trigger.ts +8 -5
- package/src/lib/agent-workspace-skills.ts +167 -68
- package/src/lib/agent-workspace-sync.ts +20 -31
- package/src/lib/capability-cloud-refresh.ts +313 -0
- package/src/lib/capability-cloud-sync.ts +315 -0
- package/src/lib/capability-discovery.ts +94 -0
- package/src/lib/capability-workspace.ts +291 -0
- package/src/lib/hub-client.ts +131 -4
- package/src/lib/persona.ts +1 -24
- package/src/lib/skill-host.ts +63 -0
- package/src/lib/user-data.ts +47 -59
- package/src/lib/zip-store.ts +92 -0
- package/src/runtime/clawclaw/owner.ts +12 -12
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import {
|
|
2
|
+
existsSync,
|
|
3
|
+
lstatSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
mkdtempSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
readdirSync,
|
|
8
|
+
renameSync,
|
|
9
|
+
rmSync,
|
|
10
|
+
writeFileSync,
|
|
11
|
+
} from 'node:fs';
|
|
12
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
13
|
+
import { tmpdir } from 'node:os';
|
|
14
|
+
import { basename, dirname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
15
|
+
import type { AuthProfile } from './auth.js';
|
|
16
|
+
import {
|
|
17
|
+
capabilityWorkspacePath,
|
|
18
|
+
readCapabilityWorkspaceRegistry,
|
|
19
|
+
updateCapabilityHubIdentity,
|
|
20
|
+
type CapabilityWorkspaceEntry,
|
|
21
|
+
type CapabilityWorkspaceType,
|
|
22
|
+
} from './capability-workspace.js';
|
|
23
|
+
import type { LocalAgentWorkspaceSnapshot } from './agent-workspace-sync.js';
|
|
24
|
+
import { HubClient, type ResourceType } from './hub-client.js';
|
|
25
|
+
import { findSkillRoot, overwriteAgentSkillTarget } from './skill-host.js';
|
|
26
|
+
import { unzip } from './hub-unzip.js';
|
|
27
|
+
import { zipContentDigest } from './zip-store.js';
|
|
28
|
+
|
|
29
|
+
export interface CapabilityCloudDownload {
|
|
30
|
+
type: CapabilityWorkspaceType;
|
|
31
|
+
id: string;
|
|
32
|
+
resource_id: string;
|
|
33
|
+
version_id: string;
|
|
34
|
+
result: 'updated' | 'converged';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface CapabilityCloudRefreshConflict {
|
|
38
|
+
type: CapabilityWorkspaceType;
|
|
39
|
+
id: string;
|
|
40
|
+
resource_id: string;
|
|
41
|
+
reason: 'local_and_cloud_changed';
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface CapabilityCloudRefreshFailure {
|
|
45
|
+
type: CapabilityWorkspaceType;
|
|
46
|
+
id: string;
|
|
47
|
+
action: 'download';
|
|
48
|
+
error: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface CapabilityCloudRefreshResult {
|
|
52
|
+
downloaded: CapabilityCloudDownload[];
|
|
53
|
+
conflicts: CapabilityCloudRefreshConflict[];
|
|
54
|
+
failed: CapabilityCloudRefreshFailure[];
|
|
55
|
+
unchanged: Array<{ type: CapabilityWorkspaceType; id: string }>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface CapabilityCloudRefreshClient {
|
|
59
|
+
getResource(type: ResourceType, id: string, workspaceItemId?: string): ReturnType<HubClient['getResource']>;
|
|
60
|
+
downloadArtifact(type: ResourceType, id: string, workspaceItemId?: string): ReturnType<HubClient['downloadArtifact']>;
|
|
61
|
+
findWorkspaceItem(type: ResourceType, resourceId: string): ReturnType<HubClient['findWorkspaceItem']>;
|
|
62
|
+
syncWorkspaceItem(workspaceItemId: string): ReturnType<HubClient['syncWorkspaceItem']>;
|
|
63
|
+
listWorkspace(opts?: Parameters<HubClient['listWorkspace']>[0]): ReturnType<HubClient['listWorkspace']>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface CapabilityCloudRefreshDependencies {
|
|
67
|
+
client?: CapabilityCloudRefreshClient;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface InstalledCapabilityUpdateResult {
|
|
71
|
+
updated: Array<{
|
|
72
|
+
type: ResourceType;
|
|
73
|
+
resource_id: string;
|
|
74
|
+
workspace_item_id: string;
|
|
75
|
+
version_id: string;
|
|
76
|
+
}>;
|
|
77
|
+
failed: Array<{
|
|
78
|
+
type: ResourceType;
|
|
79
|
+
resource_id: string;
|
|
80
|
+
workspace_item_id: string;
|
|
81
|
+
error: string;
|
|
82
|
+
}>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function syncInstalledCapabilityUpdates(
|
|
86
|
+
profile: Pick<AuthProfile, 'apiKey' | 'serverUrl'>,
|
|
87
|
+
dependencies: CapabilityCloudRefreshDependencies = {},
|
|
88
|
+
): Promise<InstalledCapabilityUpdateResult> {
|
|
89
|
+
const client = dependencies.client ?? HubClient.fromProfile(profile);
|
|
90
|
+
const result: InstalledCapabilityUpdateResult = { updated: [], failed: [] };
|
|
91
|
+
let cursor: string | undefined;
|
|
92
|
+
do {
|
|
93
|
+
const page = await client.listWorkspace({ cursor, limit: 100 });
|
|
94
|
+
for (const item of page.items.filter((candidate) => candidate.hasUpdate)) {
|
|
95
|
+
try {
|
|
96
|
+
const synced = await client.syncWorkspaceItem(item.workspaceItemId);
|
|
97
|
+
result.updated.push({
|
|
98
|
+
type: item.resourceType,
|
|
99
|
+
resource_id: item.resourceId,
|
|
100
|
+
workspace_item_id: item.workspaceItemId,
|
|
101
|
+
version_id: synced.versionId,
|
|
102
|
+
});
|
|
103
|
+
} catch (error) {
|
|
104
|
+
result.failed.push({
|
|
105
|
+
type: item.resourceType,
|
|
106
|
+
resource_id: item.resourceId,
|
|
107
|
+
workspace_item_id: item.workspaceItemId,
|
|
108
|
+
error: error instanceof Error ? error.message : String(error),
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
cursor = page.hasMore && page.nextCursor ? page.nextCursor : undefined;
|
|
113
|
+
} while (cursor);
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function resourceType(type: CapabilityWorkspaceType): ResourceType {
|
|
118
|
+
return type === 'behavior' ? 'strategy' : type;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function sha256(bytes: Buffer): string {
|
|
122
|
+
return createHash('sha256').update(bytes).digest('hex');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function collectSkill(root: string): Array<{ path: string; content: Buffer }> {
|
|
126
|
+
const files: Array<{ path: string; content: Buffer }> = [];
|
|
127
|
+
const visit = (directory: string): void => {
|
|
128
|
+
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
129
|
+
const path = join(directory, entry.name);
|
|
130
|
+
const stat = lstatSync(path);
|
|
131
|
+
if (stat.isSymbolicLink()) throw new Error('skill_source_symlink_unsupported');
|
|
132
|
+
if (stat.isDirectory()) visit(path);
|
|
133
|
+
else if (stat.isFile()) files.push({
|
|
134
|
+
path: relative(root, path).replaceAll('\\', '/'),
|
|
135
|
+
content: readFileSync(path),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
visit(root);
|
|
140
|
+
if (files.length === 0) throw new Error('skill_source_empty');
|
|
141
|
+
return files;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function localDigest(entry: CapabilityWorkspaceEntry, path: string): string {
|
|
145
|
+
return entry.type === 'skill'
|
|
146
|
+
? zipContentDigest(collectSkill(path))
|
|
147
|
+
: sha256(readFileSync(path));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function pathInside(root: string, candidate: string): boolean {
|
|
151
|
+
const child = relative(resolve(root), resolve(candidate));
|
|
152
|
+
return child === '' || (!child.startsWith('..') && !isAbsolute(child));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function prepareCloudArtifact(entry: CapabilityWorkspaceEntry, bytes: Buffer): {
|
|
156
|
+
digest: string;
|
|
157
|
+
apply(target: string): void;
|
|
158
|
+
cleanup(): void;
|
|
159
|
+
} {
|
|
160
|
+
if (entry.type !== 'skill') {
|
|
161
|
+
return {
|
|
162
|
+
digest: sha256(bytes),
|
|
163
|
+
apply(target: string) {
|
|
164
|
+
const stage = `${target}.ccl-cloud-${randomUUID()}.tmp`;
|
|
165
|
+
const displaced = `${target}.ccl-old-${randomUUID()}.tmp`;
|
|
166
|
+
let moved = false;
|
|
167
|
+
writeFileSync(stage, bytes);
|
|
168
|
+
try {
|
|
169
|
+
renameSync(target, displaced);
|
|
170
|
+
moved = true;
|
|
171
|
+
renameSync(stage, target);
|
|
172
|
+
if (sha256(readFileSync(target)) !== sha256(bytes)) {
|
|
173
|
+
throw new Error('capability_cloud_write_verification_failed');
|
|
174
|
+
}
|
|
175
|
+
rmSync(displaced, { force: true });
|
|
176
|
+
} catch (error) {
|
|
177
|
+
rmSync(stage, { force: true });
|
|
178
|
+
if (moved && existsSync(displaced)) {
|
|
179
|
+
rmSync(target, { force: true });
|
|
180
|
+
try { renameSync(displaced, target); } catch { /* retain displaced recovery copy */ }
|
|
181
|
+
}
|
|
182
|
+
throw error;
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
cleanup() {},
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
const stage = mkdtempSync(join(tmpdir(), 'ccl-capability-refresh-'));
|
|
189
|
+
try {
|
|
190
|
+
for (const file of unzip(bytes)) {
|
|
191
|
+
const target = resolve(stage, ...file.path.split('/'));
|
|
192
|
+
if (!pathInside(stage, target) || target === resolve(stage)) {
|
|
193
|
+
throw new Error('capability_cloud_skill_path_invalid');
|
|
194
|
+
}
|
|
195
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
196
|
+
writeFileSync(target, file.content);
|
|
197
|
+
}
|
|
198
|
+
const root = findSkillRoot(stage);
|
|
199
|
+
if (!existsSync(join(root, 'SKILL.md'))) throw new Error('capability_cloud_skill_invalid');
|
|
200
|
+
return {
|
|
201
|
+
digest: zipContentDigest(collectSkill(root)),
|
|
202
|
+
apply(target: string) {
|
|
203
|
+
const replaced = overwriteAgentSkillTarget(root, target);
|
|
204
|
+
if (!replaced.ok) throw new Error(replaced.error ?? 'capability_cloud_restore_failed');
|
|
205
|
+
},
|
|
206
|
+
cleanup() { rmSync(stage, { recursive: true, force: true }); },
|
|
207
|
+
};
|
|
208
|
+
} catch (error) {
|
|
209
|
+
rmSync(stage, { recursive: true, force: true });
|
|
210
|
+
throw error;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function activeResourceIds(snapshot: LocalAgentWorkspaceSnapshot | undefined): Set<string> {
|
|
215
|
+
if (!snapshot) return new Set();
|
|
216
|
+
return new Set(Object.values(snapshot.items).flat().map((item) => item.resourceId));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export async function refreshCapabilitiesFromCloud(
|
|
220
|
+
workspaceDir: string,
|
|
221
|
+
profile: Pick<AuthProfile, 'apiKey' | 'serverUrl'>,
|
|
222
|
+
activeWorkspace?: LocalAgentWorkspaceSnapshot,
|
|
223
|
+
dependencies: CapabilityCloudRefreshDependencies = {},
|
|
224
|
+
): Promise<CapabilityCloudRefreshResult> {
|
|
225
|
+
const client = dependencies.client ?? HubClient.fromProfile(profile);
|
|
226
|
+
const result: CapabilityCloudRefreshResult = { downloaded: [], conflicts: [], failed: [], unchanged: [] };
|
|
227
|
+
const active = activeResourceIds(activeWorkspace);
|
|
228
|
+
const entries = Object.values(readCapabilityWorkspaceRegistry(workspaceDir, profile).entries)
|
|
229
|
+
.filter((entry) => entry.runtimeId !== 'clawclaw' && entry.hub && !active.has(entry.hub.resourceId));
|
|
230
|
+
for (const entry of entries) {
|
|
231
|
+
const hub = entry.hub!;
|
|
232
|
+
const type = resourceType(entry.type);
|
|
233
|
+
const path = capabilityWorkspacePath(workspaceDir, profile, entry);
|
|
234
|
+
try {
|
|
235
|
+
const before = localDigest(entry, path);
|
|
236
|
+
let workspaceItemId = hub.publisher === 'self' ? undefined : hub.workspaceItemId;
|
|
237
|
+
if (hub.publisher !== 'self') {
|
|
238
|
+
const workspaceItem = await client.findWorkspaceItem(type, hub.resourceId);
|
|
239
|
+
if (!workspaceItem) throw new Error('capability_cloud_workspace_item_missing');
|
|
240
|
+
workspaceItemId = workspaceItem.workspaceItemId;
|
|
241
|
+
if (workspaceItem.hasUpdate) await client.syncWorkspaceItem(workspaceItem.workspaceItemId);
|
|
242
|
+
}
|
|
243
|
+
const detail = await client.getResource(type, hub.resourceId, workspaceItemId);
|
|
244
|
+
if (detail.versionId === hub.versionId) {
|
|
245
|
+
if (hub.observedCloudVersionId) updateCapabilityHubIdentity(workspaceDir, profile, entry.key, {
|
|
246
|
+
...hub,
|
|
247
|
+
observedCloudVersionId: undefined,
|
|
248
|
+
});
|
|
249
|
+
result.unchanged.push({ type: entry.type, id: entry.runtimeId });
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
const artifact = prepareCloudArtifact(
|
|
253
|
+
entry,
|
|
254
|
+
await client.downloadArtifact(type, hub.resourceId, workspaceItemId),
|
|
255
|
+
);
|
|
256
|
+
try {
|
|
257
|
+
const afterDownload = localDigest(entry, path);
|
|
258
|
+
if (afterDownload !== before) {
|
|
259
|
+
updateCapabilityHubIdentity(workspaceDir, profile, entry.key, {
|
|
260
|
+
...hub,
|
|
261
|
+
...(workspaceItemId ? { workspaceItemId } : {}),
|
|
262
|
+
observedCloudVersionId: detail.versionId,
|
|
263
|
+
});
|
|
264
|
+
if (entry.conflictChoice !== 'local') result.conflicts.push({
|
|
265
|
+
type: entry.type, id: entry.runtimeId, resource_id: hub.resourceId,
|
|
266
|
+
reason: 'local_and_cloud_changed',
|
|
267
|
+
});
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
const localUnmodified = before === hub.baseSha256 || before === hub.lastUploadedSha256;
|
|
271
|
+
if (before !== artifact.digest && !localUnmodified) {
|
|
272
|
+
updateCapabilityHubIdentity(workspaceDir, profile, entry.key, {
|
|
273
|
+
...hub,
|
|
274
|
+
...(workspaceItemId ? { workspaceItemId } : {}),
|
|
275
|
+
observedCloudVersionId: detail.versionId,
|
|
276
|
+
});
|
|
277
|
+
if (entry.conflictChoice !== 'local') result.conflicts.push({
|
|
278
|
+
type: entry.type, id: entry.runtimeId, resource_id: hub.resourceId,
|
|
279
|
+
reason: 'local_and_cloud_changed',
|
|
280
|
+
});
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
const convergence = before === artifact.digest;
|
|
284
|
+
if (!convergence) artifact.apply(path);
|
|
285
|
+
updateCapabilityHubIdentity(workspaceDir, profile, entry.key, {
|
|
286
|
+
...hub,
|
|
287
|
+
versionId: detail.versionId,
|
|
288
|
+
...(workspaceItemId ? { workspaceItemId } : {}),
|
|
289
|
+
baseSha256: artifact.digest,
|
|
290
|
+
lastUploadedSha256: artifact.digest,
|
|
291
|
+
observedCloudVersionId: undefined,
|
|
292
|
+
});
|
|
293
|
+
result.downloaded.push({
|
|
294
|
+
type: entry.type,
|
|
295
|
+
id: entry.runtimeId,
|
|
296
|
+
resource_id: hub.resourceId,
|
|
297
|
+
version_id: detail.versionId,
|
|
298
|
+
result: convergence ? 'converged' : 'updated',
|
|
299
|
+
});
|
|
300
|
+
} finally {
|
|
301
|
+
artifact.cleanup();
|
|
302
|
+
}
|
|
303
|
+
} catch (error) {
|
|
304
|
+
result.failed.push({
|
|
305
|
+
type: entry.type,
|
|
306
|
+
id: entry.runtimeId,
|
|
307
|
+
action: 'download',
|
|
308
|
+
error: error instanceof Error ? error.message : String(error),
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return result;
|
|
313
|
+
}
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import {
|
|
2
|
+
lstatSync,
|
|
3
|
+
readFileSync,
|
|
4
|
+
readdirSync,
|
|
5
|
+
} from 'node:fs';
|
|
6
|
+
import { basename, extname, join, relative } from 'node:path';
|
|
7
|
+
import { createHash } from 'node:crypto';
|
|
8
|
+
import type { AuthProfile } from './auth.js';
|
|
9
|
+
import {
|
|
10
|
+
capabilityWorkspacePath,
|
|
11
|
+
readCapabilityWorkspaceRegistry,
|
|
12
|
+
updateCapabilityHubIdentity,
|
|
13
|
+
setCapabilityConflictChoice,
|
|
14
|
+
type CapabilityWorkspaceEntry,
|
|
15
|
+
type CapabilityWorkspaceType,
|
|
16
|
+
} from './capability-workspace.js';
|
|
17
|
+
import {
|
|
18
|
+
HubClient,
|
|
19
|
+
type HubSourceFileUpload,
|
|
20
|
+
type HubUploadedSourceFile,
|
|
21
|
+
type ResourceType,
|
|
22
|
+
} from './hub-client.js';
|
|
23
|
+
import type { LocalAgentWorkspaceSnapshot } from './agent-workspace-sync.js';
|
|
24
|
+
import { createStoredZip, zipContentDigest, type ZipStoreEntry } from './zip-store.js';
|
|
25
|
+
|
|
26
|
+
export interface CapabilityCloudSyncChange {
|
|
27
|
+
type: CapabilityWorkspaceType;
|
|
28
|
+
id: string;
|
|
29
|
+
action: 'create' | 'version' | 'fork';
|
|
30
|
+
resource_id: string;
|
|
31
|
+
version_id: string;
|
|
32
|
+
workspace_item_id?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface CapabilityCloudSyncConflict {
|
|
36
|
+
type: CapabilityWorkspaceType;
|
|
37
|
+
id: string;
|
|
38
|
+
resource_id: string;
|
|
39
|
+
reason: 'local_and_cloud_changed';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface CapabilityCloudSyncFailure {
|
|
43
|
+
type: CapabilityWorkspaceType;
|
|
44
|
+
id: string;
|
|
45
|
+
action: 'create' | 'version' | 'fork';
|
|
46
|
+
error: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface CapabilityCloudSyncResult {
|
|
50
|
+
uploaded: CapabilityCloudSyncChange[];
|
|
51
|
+
conflicts: CapabilityCloudSyncConflict[];
|
|
52
|
+
failed: CapabilityCloudSyncFailure[];
|
|
53
|
+
unchanged: Array<{ type: CapabilityWorkspaceType; id: string }>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface CapabilityCloudClient {
|
|
57
|
+
uploadFiles(fileType: string, files: readonly HubSourceFileUpload[]): Promise<readonly HubUploadedSourceFile[]>;
|
|
58
|
+
createPrivateResource(input: Parameters<HubClient['createPrivateResource']>[0]): ReturnType<HubClient['createPrivateResource']>;
|
|
59
|
+
createPrivateVersion(input: Parameters<HubClient['createPrivateVersion']>[0]): ReturnType<HubClient['createPrivateVersion']>;
|
|
60
|
+
installResource(resourceId: string): ReturnType<HubClient['installResource']>;
|
|
61
|
+
getResource(type: ResourceType, id: string): ReturnType<HubClient['getResource']>;
|
|
62
|
+
findWorkspaceItem(type: ResourceType, resourceId: string): ReturnType<HubClient['findWorkspaceItem']>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface CapabilityCloudSyncDependencies {
|
|
66
|
+
client?: CapabilityCloudClient;
|
|
67
|
+
entryKeys?: readonly string[];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function resourceType(type: CapabilityWorkspaceType): ResourceType {
|
|
71
|
+
return type === 'behavior' ? 'strategy' : type;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function sourceFileType(type: CapabilityWorkspaceType): string {
|
|
75
|
+
if (type === 'behavior') return 'hub_strategy_source';
|
|
76
|
+
if (type === 'perception') return 'hub_perception_source';
|
|
77
|
+
if (type === 'persona') return 'hub_persona_source';
|
|
78
|
+
return 'hub_skill_source';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function sha256(bytes: Buffer): string {
|
|
82
|
+
return createHash('sha256').update(bytes).digest('hex');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function collectDirectory(root: string): ZipStoreEntry[] {
|
|
86
|
+
const entries: ZipStoreEntry[] = [];
|
|
87
|
+
const visit = (directory: string): void => {
|
|
88
|
+
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
89
|
+
const path = join(directory, entry.name);
|
|
90
|
+
const stat = lstatSync(path);
|
|
91
|
+
if (stat.isSymbolicLink()) throw new Error('skill_source_symlink_unsupported');
|
|
92
|
+
if (stat.isDirectory()) visit(path);
|
|
93
|
+
else if (stat.isFile()) entries.push({
|
|
94
|
+
path: relative(root, path).replaceAll('\\', '/'),
|
|
95
|
+
content: readFileSync(path),
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
visit(root);
|
|
100
|
+
return entries.sort((left, right) => left.path.localeCompare(right.path));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function uploadPayload(entry: CapabilityWorkspaceEntry, path: string): {
|
|
104
|
+
source: HubSourceFileUpload[];
|
|
105
|
+
packageUpload?: HubSourceFileUpload;
|
|
106
|
+
digest: string;
|
|
107
|
+
} {
|
|
108
|
+
if (entry.type !== 'skill') {
|
|
109
|
+
const bytes = readFileSync(path);
|
|
110
|
+
return {
|
|
111
|
+
source: [{ relativePath: basename(path), filename: basename(path), bytes }],
|
|
112
|
+
digest: sha256(bytes),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const files = collectDirectory(path);
|
|
116
|
+
if (files.length === 0) throw new Error('skill_source_empty');
|
|
117
|
+
const source = files.map((file) => ({
|
|
118
|
+
relativePath: file.path,
|
|
119
|
+
filename: basename(file.path),
|
|
120
|
+
bytes: file.content,
|
|
121
|
+
}));
|
|
122
|
+
return {
|
|
123
|
+
source,
|
|
124
|
+
packageUpload: {
|
|
125
|
+
relativePath: `${entry.runtimeId}.zip`,
|
|
126
|
+
filename: `${entry.runtimeId}.zip`,
|
|
127
|
+
bytes: createStoredZip(files),
|
|
128
|
+
},
|
|
129
|
+
digest: zipContentDigest(files),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function snapshotResolution(
|
|
134
|
+
snapshot: LocalAgentWorkspaceSnapshot,
|
|
135
|
+
resourceId: string,
|
|
136
|
+
): string | undefined {
|
|
137
|
+
return Object.values(snapshot.items).flat()
|
|
138
|
+
.find((item) => item.resourceId === resourceId)?.resolution;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export async function syncCapabilitiesToCloud(
|
|
142
|
+
workspaceDir: string,
|
|
143
|
+
profile: Pick<AuthProfile, 'apiKey' | 'serverUrl'>,
|
|
144
|
+
snapshot: LocalAgentWorkspaceSnapshot,
|
|
145
|
+
dependencies: CapabilityCloudSyncDependencies = {},
|
|
146
|
+
): Promise<CapabilityCloudSyncResult> {
|
|
147
|
+
const client = dependencies.client ?? HubClient.fromProfile(profile);
|
|
148
|
+
const result: CapabilityCloudSyncResult = {
|
|
149
|
+
uploaded: [],
|
|
150
|
+
conflicts: [],
|
|
151
|
+
failed: [],
|
|
152
|
+
unchanged: [],
|
|
153
|
+
};
|
|
154
|
+
const selectedKeys = dependencies.entryKeys ? new Set(dependencies.entryKeys) : undefined;
|
|
155
|
+
const entries = Object.values(readCapabilityWorkspaceRegistry(workspaceDir, profile).entries)
|
|
156
|
+
.filter((entry) => (
|
|
157
|
+
entry.runtimeId !== 'clawclaw' && (!selectedKeys || selectedKeys.has(entry.key))
|
|
158
|
+
));
|
|
159
|
+
for (const entry of entries) {
|
|
160
|
+
if (entry.hub?.pendingBackpackRestoreResourceId) {
|
|
161
|
+
try {
|
|
162
|
+
await client.installResource(entry.hub.pendingBackpackRestoreResourceId);
|
|
163
|
+
updateCapabilityHubIdentity(workspaceDir, profile, entry.key, {
|
|
164
|
+
...entry.hub,
|
|
165
|
+
pendingBackpackRestoreResourceId: undefined,
|
|
166
|
+
});
|
|
167
|
+
delete entry.hub.pendingBackpackRestoreResourceId;
|
|
168
|
+
} catch (error) {
|
|
169
|
+
result.failed.push({
|
|
170
|
+
type: entry.type,
|
|
171
|
+
id: entry.runtimeId,
|
|
172
|
+
action: 'fork',
|
|
173
|
+
error: `fork_source_backpack_restore_failed:${error instanceof Error ? error.message : String(error)}`,
|
|
174
|
+
});
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const path = capabilityWorkspacePath(workspaceDir, profile, entry);
|
|
179
|
+
let payload: ReturnType<typeof uploadPayload>;
|
|
180
|
+
try {
|
|
181
|
+
payload = uploadPayload(entry, path);
|
|
182
|
+
} catch (error) {
|
|
183
|
+
result.failed.push({
|
|
184
|
+
type: entry.type,
|
|
185
|
+
id: entry.runtimeId,
|
|
186
|
+
action: entry.hub?.publisher === 'self' ? 'version' : entry.hub ? 'fork' : 'create',
|
|
187
|
+
error: error instanceof Error ? error.message : String(error),
|
|
188
|
+
});
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (
|
|
192
|
+
entry.hub?.observedCloudVersionId
|
|
193
|
+
&& entry.hub.observedCloudVersionId !== entry.hub.versionId
|
|
194
|
+
&& entry.conflictChoice !== 'local'
|
|
195
|
+
) {
|
|
196
|
+
result.conflicts.push({
|
|
197
|
+
type: entry.type,
|
|
198
|
+
id: entry.runtimeId,
|
|
199
|
+
resource_id: entry.hub.resourceId,
|
|
200
|
+
reason: 'local_and_cloud_changed',
|
|
201
|
+
});
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (entry.hub?.lastUploadedSha256 === payload.digest || entry.hub?.baseSha256 === payload.digest) {
|
|
205
|
+
result.unchanged.push({ type: entry.type, id: entry.runtimeId });
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (
|
|
209
|
+
entry.hub
|
|
210
|
+
&& snapshotResolution(snapshot, entry.hub.resourceId) === 'concurrent_difference'
|
|
211
|
+
&& entry.conflictChoice !== 'local'
|
|
212
|
+
) {
|
|
213
|
+
result.conflicts.push({
|
|
214
|
+
type: entry.type,
|
|
215
|
+
id: entry.runtimeId,
|
|
216
|
+
resource_id: entry.hub.resourceId,
|
|
217
|
+
reason: 'local_and_cloud_changed',
|
|
218
|
+
});
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
const action = entry.hub?.publisher === 'self' ? 'version' : entry.hub ? 'fork' : 'create';
|
|
222
|
+
try {
|
|
223
|
+
let baseVersionId = entry.hub?.versionId;
|
|
224
|
+
if (action === 'version' && entry.hub) {
|
|
225
|
+
const latest = await client.getResource(resourceType(entry.type), entry.hub.resourceId);
|
|
226
|
+
if (latest.versionId !== entry.hub.versionId && entry.conflictChoice !== 'local') {
|
|
227
|
+
result.conflicts.push({
|
|
228
|
+
type: entry.type,
|
|
229
|
+
id: entry.runtimeId,
|
|
230
|
+
resource_id: entry.hub.resourceId,
|
|
231
|
+
reason: 'local_and_cloud_changed',
|
|
232
|
+
});
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
baseVersionId = latest.versionId;
|
|
236
|
+
}
|
|
237
|
+
const uploadedSource = await client.uploadFiles(sourceFileType(entry.type), payload.source);
|
|
238
|
+
let packageFileId: string | undefined;
|
|
239
|
+
if (payload.packageUpload) {
|
|
240
|
+
const uploadedPackage = await client.uploadFiles('hub_skill_package', [payload.packageUpload]);
|
|
241
|
+
packageFileId = uploadedPackage[0]?.file_id;
|
|
242
|
+
}
|
|
243
|
+
if (uploadPayload(entry, path).digest !== payload.digest) {
|
|
244
|
+
throw new Error('capability_changed_during_upload');
|
|
245
|
+
}
|
|
246
|
+
const mutation = action === 'version' && entry.hub && baseVersionId
|
|
247
|
+
? await client.createPrivateVersion({
|
|
248
|
+
resourceId: entry.hub.resourceId,
|
|
249
|
+
baseVersionId,
|
|
250
|
+
title: entry.title,
|
|
251
|
+
description: entry.description,
|
|
252
|
+
sourceFiles: uploadedSource,
|
|
253
|
+
packageFileId,
|
|
254
|
+
})
|
|
255
|
+
: await client.createPrivateResource({
|
|
256
|
+
resourceType: resourceType(entry.type),
|
|
257
|
+
title: entry.title,
|
|
258
|
+
description: entry.description,
|
|
259
|
+
sourceFiles: uploadedSource,
|
|
260
|
+
packageFileId,
|
|
261
|
+
forkFromWorkspaceItemId: action === 'fork' ? entry.hub?.workspaceItemId : undefined,
|
|
262
|
+
});
|
|
263
|
+
const workspaceItem = mutation.workspaceItemId
|
|
264
|
+
? undefined
|
|
265
|
+
: await client.findWorkspaceItem(resourceType(entry.type), mutation.resourceId);
|
|
266
|
+
const workspaceItemId = mutation.workspaceItemId ?? workspaceItem?.workspaceItemId;
|
|
267
|
+
const updatedHubIdentity = {
|
|
268
|
+
resourceId: mutation.resourceId,
|
|
269
|
+
versionId: mutation.versionId,
|
|
270
|
+
...(workspaceItemId ? { workspaceItemId } : {}),
|
|
271
|
+
publisher: 'self',
|
|
272
|
+
baseSha256: payload.digest,
|
|
273
|
+
lastUploadedSha256: payload.digest,
|
|
274
|
+
observedCloudVersionId: undefined,
|
|
275
|
+
...(action === 'fork' && entry.hub ? {
|
|
276
|
+
pendingBackpackRestoreResourceId: entry.hub.resourceId,
|
|
277
|
+
} : {}),
|
|
278
|
+
} as const;
|
|
279
|
+
updateCapabilityHubIdentity(workspaceDir, profile, entry.key, updatedHubIdentity);
|
|
280
|
+
setCapabilityConflictChoice(workspaceDir, profile, entry.key, undefined);
|
|
281
|
+
result.uploaded.push({
|
|
282
|
+
type: entry.type,
|
|
283
|
+
id: entry.runtimeId,
|
|
284
|
+
action,
|
|
285
|
+
resource_id: mutation.resourceId,
|
|
286
|
+
version_id: mutation.versionId,
|
|
287
|
+
...(workspaceItemId ? { workspace_item_id: workspaceItemId } : {}),
|
|
288
|
+
});
|
|
289
|
+
if (action === 'fork' && entry.hub) {
|
|
290
|
+
try {
|
|
291
|
+
await client.installResource(entry.hub.resourceId);
|
|
292
|
+
updateCapabilityHubIdentity(workspaceDir, profile, entry.key, {
|
|
293
|
+
...updatedHubIdentity,
|
|
294
|
+
pendingBackpackRestoreResourceId: undefined,
|
|
295
|
+
});
|
|
296
|
+
} catch (error) {
|
|
297
|
+
result.failed.push({
|
|
298
|
+
type: entry.type,
|
|
299
|
+
id: entry.runtimeId,
|
|
300
|
+
action: 'fork',
|
|
301
|
+
error: `fork_source_backpack_restore_failed:${error instanceof Error ? error.message : String(error)}`,
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
} catch (error) {
|
|
306
|
+
result.failed.push({
|
|
307
|
+
type: entry.type,
|
|
308
|
+
id: entry.runtimeId,
|
|
309
|
+
action,
|
|
310
|
+
error: error instanceof Error ? error.message : String(error),
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return result;
|
|
315
|
+
}
|