@notis_ai/cli 0.2.0-beta.19.1 → 0.2.0-beta.191.1
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/README.md +421 -133
- package/bin/check-runtime.js +15 -0
- package/bin/notis.js +2 -0
- package/config/notis_app_boundary_rules.json +50 -0
- package/config/notis_app_design_rules.json +135 -0
- package/dist/agent-hooks/notis-agent-hook.mjs +19299 -0
- package/dist/base-skills/notis-apps/SKILL.md +72 -0
- package/dist/base-skills/notis-apps/references/architecture.md +167 -0
- package/dist/base-skills/notis-apps/references/context.md +81 -0
- package/dist/base-skills/notis-apps/references/design.md +178 -0
- package/dist/base-skills/notis-apps/references/reading.md +89 -0
- package/dist/base-skills/notis-apps/references/release.md +145 -0
- package/dist/base-skills/notis-apps/references/sdk.md +63 -0
- package/dist/base-skills/notis-apps/references/troubleshooting.md +23 -0
- package/dist/base-skills/notis-cli/SKILL.md +154 -0
- package/dist/base-skills/notis-cli/references/app-delivery.md +18 -0
- package/dist/base-skills/notis-cli/references/intelligence.md +94 -0
- package/dist/base-skills/notis-cli/references/native-databases.md +21 -0
- package/dist/base-skills/notis-cli/references/tool-examples.md +56 -0
- package/dist/base-skills/notis-cli/references/troubleshooting.md +39 -0
- package/dist/base-skills/notis-query/SKILL.md +67 -0
- package/dist/base-skills/notis-query/references/database-discovery.md +70 -0
- package/dist/base-skills/notis-query/references/documents.md +50 -0
- package/dist/base-skills/notis-query/references/query.md +543 -0
- package/{template/packages/notis-sdk → dist/sdk}/package.json +14 -4
- package/dist/sdk/src/agentContext.ts +36 -0
- package/dist/sdk/src/components/Dialog.tsx +46 -0
- package/dist/sdk/src/components/DocumentEditor.tsx +103 -0
- package/dist/sdk/src/components/Markdown.tsx +60 -0
- package/dist/sdk/src/components/MarkdownEditor.tsx +121 -0
- package/dist/sdk/src/components/MultiSelectActionBar.tsx +285 -0
- package/dist/sdk/src/components/MultiSelectCheckbox.tsx +97 -0
- package/dist/sdk/src/components/MultiSelectDragOverlay.tsx +39 -0
- package/dist/sdk/src/components/NotisCommentBoundary.tsx +172 -0
- package/dist/sdk/src/components/NotisSelectionBoundary.tsx +59 -0
- package/dist/sdk/src/components/ShortcutHints.tsx +56 -0
- package/dist/sdk/src/components/Skeleton.tsx +24 -0
- package/dist/sdk/src/config.ts +261 -0
- package/dist/sdk/src/documents.ts +256 -0
- package/dist/sdk/src/hooks/useActiveResource.ts +19 -0
- package/dist/sdk/src/hooks/useAgentContext.ts +23 -0
- package/dist/sdk/src/hooks/useCloudComputer.ts +64 -0
- package/dist/sdk/src/hooks/useCollectionInteractions.ts +838 -0
- package/dist/sdk/src/hooks/useDatabaseSchema.ts +49 -0
- package/dist/sdk/src/hooks/useDatabaseSubscription.ts +76 -0
- package/dist/sdk/src/hooks/useDocument.ts +43 -0
- package/dist/sdk/src/hooks/useDocuments.ts +84 -0
- package/dist/sdk/src/hooks/useHandover.ts +77 -0
- package/dist/sdk/src/hooks/useLongPressSelection.ts +79 -0
- package/dist/sdk/src/hooks/useMultiSelect.ts +95 -0
- package/{template/packages/notis-sdk → dist/sdk}/src/hooks/useNotis.ts +12 -4
- package/{template/packages/notis-sdk → dist/sdk}/src/hooks/useNotisNavigation.ts +11 -8
- package/dist/sdk/src/hooks/useQuery.ts +71 -0
- package/dist/sdk/src/hooks/useTool.ts +65 -0
- package/dist/sdk/src/hooks/useToolQuery.ts +12 -0
- package/dist/sdk/src/hooks/useTopBarSearch.ts +81 -0
- package/dist/sdk/src/hooks/useUpsertDocument.ts +95 -0
- package/dist/sdk/src/index.ts +164 -0
- package/dist/sdk/src/interactions/actions.ts +59 -0
- package/dist/sdk/src/interactions/shortcuts.tsx +726 -0
- package/dist/sdk/src/interactions/visibility.ts +13 -0
- package/dist/sdk/src/interactions.ts +45 -0
- package/dist/sdk/src/provider.tsx +44 -0
- package/dist/sdk/src/queryCache.ts +170 -0
- package/dist/sdk/src/runtime.ts +465 -0
- package/dist/sdk/src/styles.css +266 -0
- package/dist/sdk/src/tailwind.ts +66 -0
- package/dist/sdk/src/vite.ts +73 -0
- package/dist/skill-sync/index.js +1753 -0
- package/dist/skill-sync/index.js.map +7 -0
- package/dist/skill-sync-worker.mjs +3113 -0
- package/package.json +18 -7
- package/skills/notis-apps/cli.md +299 -0
- package/skills/notis-cli/AGENT_INSTRUCTIONS.md +39 -0
- package/skills/notis-onboarding/BRIEF.md +129 -0
- package/skills/notis-query/cli.md +39 -0
- package/src/agent-hook-entry.js +5 -0
- package/src/cli.js +294 -25
- package/src/command-specs/agents.js +392 -0
- package/src/command-specs/apps.js +1483 -203
- package/src/command-specs/auth.js +114 -137
- package/src/command-specs/diagnostics.js +730 -0
- package/src/command-specs/handover.js +374 -0
- package/src/command-specs/helpers.js +85 -82
- package/src/command-specs/index.js +25 -6
- package/src/command-specs/meta.js +150 -18
- package/src/command-specs/onboarding.js +290 -0
- package/src/command-specs/profile.js +358 -0
- package/src/command-specs/reports.js +97 -0
- package/src/command-specs/skills.js +75 -0
- package/src/command-specs/smoke.js +386 -0
- package/src/command-specs/tools.js +455 -139
- package/src/runtime/agent-browser.js +677 -0
- package/src/runtime/agent-memory-state.js +126 -0
- package/src/runtime/agent-setup.js +383 -0
- package/src/runtime/app-boundary-validator.js +404 -0
- package/src/runtime/app-changelog.js +79 -0
- package/src/runtime/app-platform.js +2652 -205
- package/src/runtime/app-registry-scaffolds.js +367 -0
- package/src/runtime/app-test-server.js +293 -0
- package/src/runtime/assets/store-screenshot-dark.png +0 -0
- package/src/runtime/auth-recovery.js +110 -0
- package/src/runtime/base-skills.d.ts +20 -0
- package/src/runtime/base-skills.js +167 -0
- package/src/runtime/channel.js +133 -0
- package/src/runtime/delegated-context.js +68 -0
- package/src/runtime/errors.js +1 -0
- package/src/runtime/git.js +233 -0
- package/src/runtime/login-listener.js +15 -0
- package/src/runtime/oauth.js +2622 -0
- package/src/runtime/output.js +37 -5
- package/src/runtime/ports.js +31 -0
- package/src/runtime/profiles.js +906 -55
- package/src/runtime/screenshot-framing.js +85 -0
- package/src/runtime/skill-sync/cloud-client.ts +99 -0
- package/src/runtime/skill-sync/index.ts +786 -0
- package/src/runtime/skill-sync/local-scanner.ts +1151 -0
- package/src/runtime/skill-sync/symlink-manager.ts +433 -0
- package/src/runtime/skill-sync/sync-plan.ts +47 -0
- package/src/runtime/skill-sync/types.ts +131 -0
- package/src/runtime/skill-sync/write-cloud-skill.ts +50 -0
- package/src/runtime/skill-sync-service.js +109 -0
- package/src/runtime/store-screenshot.js +146 -0
- package/src/runtime/sync-skills.d.ts +37 -0
- package/src/runtime/sync-skills.js +231 -0
- package/src/runtime/telemetry.js +92 -0
- package/src/runtime/transport.js +324 -45
- package/src/skill-sync-worker-entry.js +2 -0
- package/src/skill-sync-worker.js +50 -0
- package/template/.harness/index.html.tmpl +435 -0
- package/template/CHANGELOG.md +5 -0
- package/template/app/globals.css +28 -3
- package/template/app/layout.tsx +6 -3
- package/template/app/page.tsx +49 -42
- package/template/components/page-heading.tsx +23 -0
- package/template/components/ui/badge.tsx +7 -4
- package/template/components/ui/button.tsx +1 -1
- package/template/components/ui/card.tsx +24 -11
- package/template/components/ui/native-select.tsx +24 -0
- package/template/notis.config.ts +24 -6
- package/template/package-lock.json +3642 -0
- package/template/package.json +19 -16
- package/template/postcss.config.mjs +1 -1
- package/template/tailwind.config.ts +1 -6
- package/template/tsconfig.json +1 -0
- package/src/command-specs/db.js +0 -163
- package/src/runtime/app-preview-server.js +0 -312
- package/template/packages/notis-sdk/src/config.ts +0 -48
- package/template/packages/notis-sdk/src/helpers.ts +0 -131
- package/template/packages/notis-sdk/src/hooks/useAppState.ts +0 -50
- package/template/packages/notis-sdk/src/hooks/useCollectionItem.ts +0 -58
- package/template/packages/notis-sdk/src/hooks/useDatabase.ts +0 -87
- package/template/packages/notis-sdk/src/hooks/useDocument.ts +0 -61
- package/template/packages/notis-sdk/src/hooks/useTool.ts +0 -49
- package/template/packages/notis-sdk/src/hooks/useUpsertDocument.ts +0 -57
- package/template/packages/notis-sdk/src/index.ts +0 -47
- package/template/packages/notis-sdk/src/provider.tsx +0 -44
- package/template/packages/notis-sdk/src/runtime.ts +0 -159
- package/template/packages/notis-sdk/src/styles.css +0 -123
- package/template/packages/notis-sdk/src/vite.ts +0 -54
- package/template/packages/notis-sdk/tsconfig.json +0 -15
- /package/{template/packages/notis-sdk → dist/sdk}/src/hooks/useBackend.ts +0 -0
- /package/{template/packages/notis-sdk → dist/sdk}/src/hooks/useTools.ts +0 -0
- /package/{template/packages/notis-sdk → dist/sdk}/src/ui.ts +0 -0
|
@@ -0,0 +1,786 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import type {
|
|
3
|
+
AgentTargets,
|
|
4
|
+
LocalSkill,
|
|
5
|
+
NotisSyncState,
|
|
6
|
+
SkillSyncFailure,
|
|
7
|
+
SyncPullResponse,
|
|
8
|
+
SyncSettings,
|
|
9
|
+
} from "./types";
|
|
10
|
+
import { normalizeAgentTargets } from "./types";
|
|
11
|
+
import {
|
|
12
|
+
downloadSkillBundle,
|
|
13
|
+
fetchSyncSettings,
|
|
14
|
+
pullSkills,
|
|
15
|
+
pushChangedSkills,
|
|
16
|
+
updateAgentTargets,
|
|
17
|
+
} from "./cloud-client";
|
|
18
|
+
import {
|
|
19
|
+
deleteLocalSkill,
|
|
20
|
+
gatherTopLevelLocalSkills,
|
|
21
|
+
getSkillSyncPathsForUser,
|
|
22
|
+
readLegacySyncState,
|
|
23
|
+
readSyncState,
|
|
24
|
+
scanLocalSkills,
|
|
25
|
+
writeCloudSkillToDisk,
|
|
26
|
+
writeSyncState,
|
|
27
|
+
} from "./local-scanner";
|
|
28
|
+
import {
|
|
29
|
+
detectDeletedAgentSymlinks,
|
|
30
|
+
removeForeignAccountSymlinks,
|
|
31
|
+
removeAllSymlinksForSkill,
|
|
32
|
+
syncSymlinks,
|
|
33
|
+
type DeletedAgentSymlink,
|
|
34
|
+
} from "./symlink-manager";
|
|
35
|
+
import { getPushCandidates, selectCloudSkillsToApply } from "./sync-plan";
|
|
36
|
+
import { writeCloudSkillWithBundleFallback } from "./write-cloud-skill";
|
|
37
|
+
export { fetchSyncSettings } from './cloud-client';
|
|
38
|
+
|
|
39
|
+
export interface RunSkillSyncResult {
|
|
40
|
+
syncEnabled: boolean;
|
|
41
|
+
pushed: number;
|
|
42
|
+
pulled: number;
|
|
43
|
+
downloaded: number;
|
|
44
|
+
deleted: number;
|
|
45
|
+
/** Skills deactivated for a specific agent because the user deleted that agent's local
|
|
46
|
+
* symlink (e.g. `rm ~/.claude/skills/<skill>`); the deletion is honored instead of recreated. */
|
|
47
|
+
deactivated: number;
|
|
48
|
+
linked: number;
|
|
49
|
+
removed: number;
|
|
50
|
+
skipped: number;
|
|
51
|
+
lastSyncedAt: string | null;
|
|
52
|
+
/** Skills the server rejected (e.g. an invalid SKILL.md description). The rest of
|
|
53
|
+
* the batch still syncs; these are surfaced so the user knows what to fix. */
|
|
54
|
+
failedPushes: SkillSyncFailure[];
|
|
55
|
+
failedLinks?: SkillSyncFailure[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface RunSkillSyncOptions {
|
|
59
|
+
/** Electron's scheduled invocation honors the account preference. A manual
|
|
60
|
+
* CLI sync always runs, even when automatic Desktop refresh is disabled. */
|
|
61
|
+
honorSyncEnabled?: boolean;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const BASE_SKILL_NAMES = new Set(['notis-apps', 'notis-query', 'notis-cli']);
|
|
65
|
+
|
|
66
|
+
function withoutBaseSkills(pullResponse: SyncPullResponse): SyncPullResponse {
|
|
67
|
+
return {
|
|
68
|
+
...pullResponse,
|
|
69
|
+
skills: pullResponse.skills.filter((skill) => !BASE_SKILL_NAMES.has(skill.name)),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function withoutBaseSkillState(state: NotisSyncState): NotisSyncState {
|
|
74
|
+
return {
|
|
75
|
+
...state,
|
|
76
|
+
skills: Object.fromEntries(
|
|
77
|
+
Object.entries(state.skills).filter(([name]) => !BASE_SKILL_NAMES.has(name)),
|
|
78
|
+
),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface MaterializeCloudSkillsResult {
|
|
83
|
+
/** Cloud skills verified present in the final scoped disk scan. */
|
|
84
|
+
materializedSkillNames: string[];
|
|
85
|
+
pulled: number;
|
|
86
|
+
downloaded: number;
|
|
87
|
+
deleted: number;
|
|
88
|
+
removed: number;
|
|
89
|
+
lastSyncedAt: string | null;
|
|
90
|
+
failedLinks?: SkillSyncFailure[];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface MaterializeCloudSkillsOptions {
|
|
94
|
+
/** Server-verified Notis identity, when Desktop's auth subject differs. */
|
|
95
|
+
canonicalUserId?: string;
|
|
96
|
+
/** Re-link only these cloud skills without removing or adopting other links. */
|
|
97
|
+
relinkSkillNames?: readonly string[];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
interface RunSkillSyncDependencies {
|
|
101
|
+
fetchSyncSettings: typeof fetchSyncSettings;
|
|
102
|
+
pullSkills: typeof pullSkills;
|
|
103
|
+
pushChangedSkills: typeof pushChangedSkills;
|
|
104
|
+
downloadSkillBundle: typeof downloadSkillBundle;
|
|
105
|
+
gatherTopLevelLocalSkills: typeof gatherTopLevelLocalSkills;
|
|
106
|
+
readLegacySyncState: typeof readLegacySyncState;
|
|
107
|
+
readSyncState: typeof readSyncState;
|
|
108
|
+
scanLocalSkills: typeof scanLocalSkills;
|
|
109
|
+
deleteLocalSkill: typeof deleteLocalSkill;
|
|
110
|
+
writeCloudSkillToDisk: typeof writeCloudSkillToDisk;
|
|
111
|
+
writeSyncState: typeof writeSyncState;
|
|
112
|
+
removeAllSymlinksForSkill: typeof removeAllSymlinksForSkill;
|
|
113
|
+
syncSymlinks: typeof syncSymlinks;
|
|
114
|
+
detectDeletedAgentSymlinks: typeof detectDeletedAgentSymlinks;
|
|
115
|
+
removeForeignAccountSymlinks: typeof removeForeignAccountSymlinks;
|
|
116
|
+
updateAgentTargets: typeof updateAgentTargets;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const DEFAULT_RUN_SKILL_SYNC_DEPS: RunSkillSyncDependencies = {
|
|
120
|
+
fetchSyncSettings,
|
|
121
|
+
pullSkills,
|
|
122
|
+
pushChangedSkills,
|
|
123
|
+
downloadSkillBundle,
|
|
124
|
+
gatherTopLevelLocalSkills,
|
|
125
|
+
readLegacySyncState,
|
|
126
|
+
readSyncState,
|
|
127
|
+
scanLocalSkills,
|
|
128
|
+
deleteLocalSkill,
|
|
129
|
+
writeCloudSkillToDisk,
|
|
130
|
+
writeSyncState,
|
|
131
|
+
removeAllSymlinksForSkill,
|
|
132
|
+
syncSymlinks,
|
|
133
|
+
detectDeletedAgentSymlinks,
|
|
134
|
+
removeForeignAccountSymlinks,
|
|
135
|
+
updateAgentTargets,
|
|
136
|
+
};
|
|
137
|
+
function toSkillMap(skills: LocalSkill[]): Map<string, LocalSkill> {
|
|
138
|
+
return new Map(skills.map((skill) => [skill.name, skill]));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function decodeJwtSubject(jwt: string): string | null {
|
|
142
|
+
try {
|
|
143
|
+
const parts = jwt.split(".");
|
|
144
|
+
if (parts.length !== 3) return null;
|
|
145
|
+
const decoded = JSON.parse(
|
|
146
|
+
Buffer.from(parts[1], "base64url").toString(),
|
|
147
|
+
) as { sub?: unknown };
|
|
148
|
+
return typeof decoded.sub === "string" && decoded.sub.trim()
|
|
149
|
+
? decoded.sub.trim()
|
|
150
|
+
: null;
|
|
151
|
+
} catch {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function cloudContentHash(skill: SyncPullResponse['skills'][number]): string {
|
|
157
|
+
return createHash('sha256').update(JSON.stringify({
|
|
158
|
+
md: skill.skill_md,
|
|
159
|
+
hash: skill.skill_folder_hash,
|
|
160
|
+
source: skill.skill_source_url,
|
|
161
|
+
files: skill.bundle_files?.slice().sort((a, b) => a.path.localeCompare(b.path)),
|
|
162
|
+
hydrationFailed: skill.bundle_hydration_failed === true,
|
|
163
|
+
})).digest('hex');
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function shouldWriteCloudSkill(
|
|
167
|
+
cloudSkill: SyncPullResponse["skills"][number],
|
|
168
|
+
localSkills: Map<string, LocalSkill>,
|
|
169
|
+
previousState: NotisSyncState,
|
|
170
|
+
): boolean {
|
|
171
|
+
const skillName = cloudSkill.name;
|
|
172
|
+
const cloudHash = cloudSkill.skill_folder_hash || "";
|
|
173
|
+
const localSkill = localSkills.get(skillName);
|
|
174
|
+
if (!localSkill) {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const previous = previousState.skills[skillName];
|
|
179
|
+
if (previous?.folderHash === localSkill.folderHash
|
|
180
|
+
&& previous.cloudContentHash === cloudContentHash(cloudSkill)) return false;
|
|
181
|
+
|
|
182
|
+
// Without a stored content baseline (skills served through a signed source URL keep
|
|
183
|
+
// none, because that URL changes on every pull) the only other signal is
|
|
184
|
+
// `skill_folder_hash`, which the server does NOT always compute the way the local
|
|
185
|
+
// folder hash is computed. An app-published skill hashes its bundle entries, so that
|
|
186
|
+
// value can never equal the hash of the extracted directory, and comparing the two
|
|
187
|
+
// replaced the folder on every sync tick, deleting whatever a running skill had
|
|
188
|
+
// written inside it. Compare cloud against cloud instead: rewrite when the cloud
|
|
189
|
+
// revision we last applied has actually moved.
|
|
190
|
+
if (previous
|
|
191
|
+
&& previous.cloudContentHash === undefined
|
|
192
|
+
&& previous.cloudId === cloudSkill.id
|
|
193
|
+
&& previous.appliedCloudFolderHash !== undefined
|
|
194
|
+
&& previous.appliedCloudFolderHash === cloudHash
|
|
195
|
+
&& previous.cloudUpdatedAt === cloudSkill.updated_at
|
|
196
|
+
&& previous.folderHash === localSkill.folderHash) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (cloudSkill.source === "curated") {
|
|
201
|
+
return cloudHash ? cloudHash !== localSkill.folderHash : true;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const localChangedSinceLastSync =
|
|
205
|
+
!previous || previous.folderHash !== localSkill.folderHash;
|
|
206
|
+
return (
|
|
207
|
+
!localChangedSinceLastSync &&
|
|
208
|
+
((Boolean(cloudHash) && cloudHash !== localSkill.folderHash)
|
|
209
|
+
|| Boolean(previous?.cloudContentHash && previous.cloudContentHash !== cloudContentHash(cloudSkill)))
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
type AppliedCloudRevisions = Record<string, string | undefined>;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* The cloud revision each folder's last successful write applied: this run's revision
|
|
217
|
+
* for the skills we just wrote, the previously recorded one otherwise. A failed write
|
|
218
|
+
* records nothing, so the next sync retries it.
|
|
219
|
+
*/
|
|
220
|
+
function collectAppliedCloudRevisions(
|
|
221
|
+
pullResponse: SyncPullResponse,
|
|
222
|
+
previousState: NotisSyncState,
|
|
223
|
+
writtenSkillNames: ReadonlySet<string>,
|
|
224
|
+
): AppliedCloudRevisions {
|
|
225
|
+
const applied: AppliedCloudRevisions = {};
|
|
226
|
+
for (const skill of selectCloudSkillsToApply(pullResponse.skills)) {
|
|
227
|
+
applied[skill.name] = writtenSkillNames.has(skill.name)
|
|
228
|
+
? skill.skill_folder_hash || ""
|
|
229
|
+
: previousState.skills[skill.name]?.appliedCloudFolderHash;
|
|
230
|
+
}
|
|
231
|
+
return applied;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* The files each folder's last successful write put on disk: this run's list for the
|
|
236
|
+
* skills we just wrote, the previously recorded one otherwise. Missing means "no
|
|
237
|
+
* record", which keeps local files rather than deleting what cannot be attributed.
|
|
238
|
+
*/
|
|
239
|
+
function collectAppliedFiles(
|
|
240
|
+
pullResponse: SyncPullResponse,
|
|
241
|
+
previousState: NotisSyncState,
|
|
242
|
+
appliedFilesByName: Record<string, string[]>,
|
|
243
|
+
): Record<string, string[] | undefined> {
|
|
244
|
+
const applied: Record<string, string[] | undefined> = {};
|
|
245
|
+
for (const skill of selectCloudSkillsToApply(pullResponse.skills)) {
|
|
246
|
+
applied[skill.name] = appliedFilesByName[skill.name]
|
|
247
|
+
?? previousState.skills[skill.name]?.appliedFiles;
|
|
248
|
+
}
|
|
249
|
+
return applied;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function buildSyncState(
|
|
253
|
+
pullResponse: SyncPullResponse,
|
|
254
|
+
localSkills: LocalSkill[],
|
|
255
|
+
lastSyncedAt: string | null,
|
|
256
|
+
verifiedAgentLinks: Record<string, Partial<AgentTargets>> = {},
|
|
257
|
+
failedContentNames: ReadonlySet<string> = new Set(),
|
|
258
|
+
appliedRevisions: AppliedCloudRevisions = {},
|
|
259
|
+
appliedFiles: Record<string, string[] | undefined> = {},
|
|
260
|
+
): NotisSyncState {
|
|
261
|
+
const localSkillMap = toSkillMap(localSkills);
|
|
262
|
+
// The same row selection the writer used, so the state describes the revision that
|
|
263
|
+
// is actually on disk rather than whichever duplicate row the server listed last.
|
|
264
|
+
const skills = Object.fromEntries(
|
|
265
|
+
selectCloudSkillsToApply(pullResponse.skills).map((skill) => {
|
|
266
|
+
const localSkill = localSkillMap.get(skill.name);
|
|
267
|
+
const appliedCloudFolderHash = appliedRevisions[skill.name];
|
|
268
|
+
const appliedFileList = appliedFiles[skill.name];
|
|
269
|
+
return [
|
|
270
|
+
skill.name,
|
|
271
|
+
{
|
|
272
|
+
cloudId: skill.id,
|
|
273
|
+
folderHash: localSkill?.folderHash || skill.skill_folder_hash || "",
|
|
274
|
+
agentTargets: normalizeAgentTargets(skill.agent_targets),
|
|
275
|
+
verifiedAgentLinks: skill.status === "active" ? verifiedAgentLinks[skill.name] ?? {} : {},
|
|
276
|
+
cloudUpdatedAt: skill.updated_at,
|
|
277
|
+
...(!failedContentNames.has(skill.name) && !skill.skill_source_url
|
|
278
|
+
? { cloudContentHash: cloudContentHash(skill) } : {}),
|
|
279
|
+
...(appliedCloudFolderHash !== undefined ? { appliedCloudFolderHash } : {}),
|
|
280
|
+
...(appliedFileList !== undefined ? { appliedFiles: appliedFileList } : {}),
|
|
281
|
+
syncedAt: lastSyncedAt || new Date().toISOString(),
|
|
282
|
+
},
|
|
283
|
+
];
|
|
284
|
+
}),
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
return {
|
|
288
|
+
version: 1,
|
|
289
|
+
lastSyncedAt,
|
|
290
|
+
skills,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function buildLocalSymlinkCandidates(
|
|
295
|
+
pullResponse: SyncPullResponse,
|
|
296
|
+
localSkills: LocalSkill[],
|
|
297
|
+
previousState: NotisSyncState,
|
|
298
|
+
): SyncPullResponse["skills"] {
|
|
299
|
+
const cloudSkillNames = new Set(
|
|
300
|
+
pullResponse.skills.map((skill) => skill.name),
|
|
301
|
+
);
|
|
302
|
+
const localOnlySkills = localSkills
|
|
303
|
+
.filter((skill) => !cloudSkillNames.has(skill.name))
|
|
304
|
+
.map((skill) => {
|
|
305
|
+
const previous = previousState.skills[skill.name];
|
|
306
|
+
return {
|
|
307
|
+
id: previous?.cloudId || `local-${skill.name}`,
|
|
308
|
+
name: skill.name,
|
|
309
|
+
description: skill.description || null,
|
|
310
|
+
skill_md: skill.skillMd,
|
|
311
|
+
agent_targets: previous?.agentTargets,
|
|
312
|
+
skill_folder_hash: skill.folderHash,
|
|
313
|
+
source: "local",
|
|
314
|
+
status: "active",
|
|
315
|
+
};
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
return [...pullResponse.skills, ...localOnlySkills];
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function isEmptySyncState(state: NotisSyncState): boolean {
|
|
322
|
+
return state.lastSyncedAt === null && Object.keys(state.skills).length === 0;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function applyLegacyFirstRunState(
|
|
326
|
+
localSkills: LocalSkill[],
|
|
327
|
+
scopedState: NotisSyncState,
|
|
328
|
+
legacyState: NotisSyncState | null,
|
|
329
|
+
): NotisSyncState {
|
|
330
|
+
if (!isEmptySyncState(scopedState) || !legacyState) {
|
|
331
|
+
return scopedState;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const migratedSkills = Object.fromEntries(
|
|
335
|
+
localSkills.flatMap((skill) => {
|
|
336
|
+
const previous = legacyState.skills[skill.name];
|
|
337
|
+
if (!previous || previous.folderHash !== skill.folderHash) {
|
|
338
|
+
return [];
|
|
339
|
+
}
|
|
340
|
+
return [[skill.name, previous]];
|
|
341
|
+
}),
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
if (Object.keys(migratedSkills).length === 0) {
|
|
345
|
+
return scopedState;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return {
|
|
349
|
+
version: 1,
|
|
350
|
+
lastSyncedAt: legacyState.lastSyncedAt,
|
|
351
|
+
skills: migratedSkills,
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
async function writePulledSkillsToScopedMirror(
|
|
356
|
+
pullResponse: SyncPullResponse,
|
|
357
|
+
localSkills: LocalSkill[],
|
|
358
|
+
previousState: NotisSyncState,
|
|
359
|
+
syncPaths: ReturnType<typeof getSkillSyncPathsForUser>,
|
|
360
|
+
deps: Pick<
|
|
361
|
+
RunSkillSyncDependencies,
|
|
362
|
+
"downloadSkillBundle" | "writeCloudSkillToDisk"
|
|
363
|
+
>,
|
|
364
|
+
failures: SkillSyncFailure[] = [],
|
|
365
|
+
writtenSkillNames: Set<string> = new Set(),
|
|
366
|
+
appliedFilesByName: Record<string, string[]> = {},
|
|
367
|
+
): Promise<number> {
|
|
368
|
+
const localSkillMap = toSkillMap(localSkills);
|
|
369
|
+
const warnSkillSync = (message: string, error: unknown): void => {
|
|
370
|
+
console.warn(`[Notis] ${message}`, error);
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
let downloaded = 0;
|
|
374
|
+
for (const cloudSkill of selectCloudSkillsToApply(pullResponse.skills)) {
|
|
375
|
+
if (!shouldWriteCloudSkill(cloudSkill, localSkillMap, previousState)) {
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const outcome = await writeCloudSkillWithBundleFallback(cloudSkill, {
|
|
380
|
+
downloadSkillBundle: deps.downloadSkillBundle,
|
|
381
|
+
writeCloudSkillToDisk: (skill, bundleBytes) =>
|
|
382
|
+
deps.writeCloudSkillToDisk(skill, bundleBytes, syncPaths, {
|
|
383
|
+
// What the last write put there. Everything else in the folder was
|
|
384
|
+
// produced locally and survives this one.
|
|
385
|
+
previouslyApplied: previousState.skills[skill.name]?.appliedFiles,
|
|
386
|
+
}),
|
|
387
|
+
onWarning: warnSkillSync,
|
|
388
|
+
});
|
|
389
|
+
if (outcome) {
|
|
390
|
+
downloaded += 1;
|
|
391
|
+
writtenSkillNames.add(cloudSkill.name);
|
|
392
|
+
if (typeof outcome === "object" && Array.isArray(outcome.appliedFiles)) {
|
|
393
|
+
appliedFilesByName[cloudSkill.name] = outcome.appliedFiles;
|
|
394
|
+
}
|
|
395
|
+
} else {
|
|
396
|
+
failures.push({ name: cloudSkill.name, error: "Skill content could not be downloaded or written; sync will retry" });
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return downloaded;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function assertSkillsPullAuthorized(
|
|
404
|
+
pullResponse: SyncPullResponse,
|
|
405
|
+
): void {
|
|
406
|
+
if (
|
|
407
|
+
pullResponse.entitlement_access?.code === "entitlement_upgrade_required"
|
|
408
|
+
&& pullResponse.entitlement_access.entitlement === "skills"
|
|
409
|
+
) {
|
|
410
|
+
// Current servers return HTTP 403, so cloud-client rejects before producing
|
|
411
|
+
// a SyncPullResponse. Keep this guard for an older server's successful
|
|
412
|
+
// empty-denial envelope, but fail closed: it is never authorization to
|
|
413
|
+
// delete a user's managed local mirror.
|
|
414
|
+
throw new Error(
|
|
415
|
+
"Skill sync access was denied; preserving existing local skills.",
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
export async function materializeCloudSkillsForLocalShell(
|
|
421
|
+
serverUrl: string,
|
|
422
|
+
jwt: string,
|
|
423
|
+
dependencies: Partial<Pick<
|
|
424
|
+
RunSkillSyncDependencies,
|
|
425
|
+
| "downloadSkillBundle"
|
|
426
|
+
| "deleteLocalSkill"
|
|
427
|
+
| "pullSkills"
|
|
428
|
+
| "readSyncState"
|
|
429
|
+
| "removeAllSymlinksForSkill"
|
|
430
|
+
| "scanLocalSkills"
|
|
431
|
+
| "syncSymlinks"
|
|
432
|
+
| "writeCloudSkillToDisk"
|
|
433
|
+
| "writeSyncState"
|
|
434
|
+
>> = {},
|
|
435
|
+
options: MaterializeCloudSkillsOptions = {},
|
|
436
|
+
): Promise<MaterializeCloudSkillsResult> {
|
|
437
|
+
const deps = {
|
|
438
|
+
...DEFAULT_RUN_SKILL_SYNC_DEPS,
|
|
439
|
+
...dependencies,
|
|
440
|
+
};
|
|
441
|
+
const authUserId = decodeJwtSubject(jwt);
|
|
442
|
+
if (!authUserId) {
|
|
443
|
+
throw new Error(
|
|
444
|
+
"Cannot materialize skills without a valid authenticated desktop session.",
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
const syncPaths = getSkillSyncPathsForUser(options.canonicalUserId?.trim() || authUserId);
|
|
449
|
+
const pullResponse = await deps.pullSkills(serverUrl, jwt);
|
|
450
|
+
assertSkillsPullAuthorized(pullResponse);
|
|
451
|
+
const previousState = await deps.readSyncState(syncPaths);
|
|
452
|
+
|
|
453
|
+
const localSkills = await deps.scanLocalSkills(syncPaths);
|
|
454
|
+
const failedDownloads: SkillSyncFailure[] = [];
|
|
455
|
+
const writtenSkillNames = new Set<string>();
|
|
456
|
+
const appliedFilesByName: Record<string, string[]> = {};
|
|
457
|
+
const downloaded = await writePulledSkillsToScopedMirror(
|
|
458
|
+
pullResponse,
|
|
459
|
+
localSkills,
|
|
460
|
+
previousState,
|
|
461
|
+
syncPaths,
|
|
462
|
+
deps,
|
|
463
|
+
failedDownloads,
|
|
464
|
+
writtenSkillNames,
|
|
465
|
+
appliedFilesByName,
|
|
466
|
+
);
|
|
467
|
+
const finalLocalSkills = await deps.scanLocalSkills(syncPaths);
|
|
468
|
+
const lastSyncedAt = pullResponse.last_synced_at || new Date().toISOString();
|
|
469
|
+
|
|
470
|
+
const relinkSkillNames = new Set(options.relinkSkillNames || []);
|
|
471
|
+
const failures = [...failedDownloads];
|
|
472
|
+
const verifiedLinks: Record<string, Partial<AgentTargets>> = {};
|
|
473
|
+
for (const skill of pullResponse.skills) {
|
|
474
|
+
const previous = previousState.skills[skill.name];
|
|
475
|
+
if (skill.updated_at && previous?.cloudId === skill.id && previous.cloudUpdatedAt === skill.updated_at) {
|
|
476
|
+
verifiedLinks[skill.name] = { ...previous.verifiedAgentLinks };
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
if (relinkSkillNames.size > 0) {
|
|
480
|
+
const relinked = await deps.syncSymlinks(
|
|
481
|
+
pullResponse.skills.filter((skill) => relinkSkillNames.has(skill.name)),
|
|
482
|
+
syncPaths.skillsDir,
|
|
483
|
+
{ removeUndesired: false },
|
|
484
|
+
);
|
|
485
|
+
failures.push(...(relinked.failures ?? []).filter(
|
|
486
|
+
(failure) => !failedDownloads.some((download) => download.name === failure.name),
|
|
487
|
+
));
|
|
488
|
+
for (const name of relinkSkillNames) {
|
|
489
|
+
verifiedLinks[name] = relinked.verifiedAgentLinks?.[name] ?? {};
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
for (const failure of failedDownloads) delete verifiedLinks[failure.name];
|
|
493
|
+
|
|
494
|
+
const materializedState = buildSyncState(
|
|
495
|
+
pullResponse, finalLocalSkills, lastSyncedAt, verifiedLinks,
|
|
496
|
+
new Set(failedDownloads.map(item => item.name)),
|
|
497
|
+
collectAppliedCloudRevisions(pullResponse, previousState, writtenSkillNames),
|
|
498
|
+
collectAppliedFiles(pullResponse, previousState, appliedFilesByName),
|
|
499
|
+
);
|
|
500
|
+
// Pull-only refresh is not an upload acknowledgement. Keep content baselines
|
|
501
|
+
// unless we actually wrote cloud content, and retain cloud-missing entries so
|
|
502
|
+
// the next two-way sync can delete them instead of uploading them as new.
|
|
503
|
+
for (const [name, entry] of Object.entries(materializedState.skills)) {
|
|
504
|
+
if (writtenSkillNames.has(name)) continue;
|
|
505
|
+
const previous = previousState.skills[name];
|
|
506
|
+
if (previous) {
|
|
507
|
+
entry.folderHash = previous.folderHash;
|
|
508
|
+
entry.cloudContentHash = previous.cloudContentHash;
|
|
509
|
+
} else {
|
|
510
|
+
delete materializedState.skills[name];
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
materializedState.skills = { ...previousState.skills, ...materializedState.skills };
|
|
514
|
+
await deps.writeSyncState(materializedState, syncPaths);
|
|
515
|
+
|
|
516
|
+
return {
|
|
517
|
+
materializedSkillNames: pullResponse.skills
|
|
518
|
+
.filter((skill) => finalLocalSkills.some((local) => local.name === skill.name)
|
|
519
|
+
&& !failedDownloads.some((failure) => failure.name === skill.name))
|
|
520
|
+
.map((skill) => skill.name),
|
|
521
|
+
pulled: pullResponse.skills.length,
|
|
522
|
+
downloaded,
|
|
523
|
+
deleted: 0,
|
|
524
|
+
removed: 0,
|
|
525
|
+
lastSyncedAt,
|
|
526
|
+
...(failures.length ? { failedLinks: failures } : {}),
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* When the user deletes a skill's symlink for a single agent (e.g. `rm ~/.claude/skills/<skill>`),
|
|
532
|
+
* honor that as "remove this skill from that agent" by deactivating it in the portal, instead of
|
|
533
|
+
* recreating the symlink on the next sync. The cloud `agent_targets` are mutated in place so the
|
|
534
|
+
* subsequent symlink reconciliation treats the link as undesired. Returns the number of
|
|
535
|
+
* (skill, agent) pairs deactivated.
|
|
536
|
+
*/
|
|
537
|
+
async function deactivateDeletedAgentSkills(
|
|
538
|
+
serverUrl: string,
|
|
539
|
+
jwt: string,
|
|
540
|
+
pullResponse: SyncPullResponse,
|
|
541
|
+
previousState: NotisSyncState,
|
|
542
|
+
scopedState: NotisSyncState,
|
|
543
|
+
skillsDir: string,
|
|
544
|
+
deps: Pick<
|
|
545
|
+
RunSkillSyncDependencies,
|
|
546
|
+
"detectDeletedAgentSymlinks" | "updateAgentTargets" | "pullSkills"
|
|
547
|
+
>,
|
|
548
|
+
failures: SkillSyncFailure[],
|
|
549
|
+
): Promise<number> {
|
|
550
|
+
// First sync (incl. legacy migration) has no reliable "we created this link" signal, so we
|
|
551
|
+
// cannot tell a user deletion apart from a never-created link — skip detection entirely.
|
|
552
|
+
if (isEmptySyncState(scopedState)) {
|
|
553
|
+
return 0;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
const deletions = await deps.detectDeletedAgentSymlinks(
|
|
557
|
+
pullResponse.skills,
|
|
558
|
+
previousState,
|
|
559
|
+
skillsDir,
|
|
560
|
+
);
|
|
561
|
+
if (deletions.length === 0) {
|
|
562
|
+
return 0;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
const agentsBySkill = new Map<string, { skillName: string; agents: Set<DeletedAgentSymlink["agent"]> }>();
|
|
566
|
+
for (const deletion of deletions) {
|
|
567
|
+
const entry = agentsBySkill.get(deletion.skillId) ?? {
|
|
568
|
+
skillName: deletion.skillName,
|
|
569
|
+
agents: new Set<DeletedAgentSymlink["agent"]>(),
|
|
570
|
+
};
|
|
571
|
+
entry.agents.add(deletion.agent);
|
|
572
|
+
agentsBySkill.set(deletion.skillId, entry);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
const fresh = withoutBaseSkills(await deps.pullSkills(serverUrl, jwt));
|
|
576
|
+
assertSkillsPullAuthorized(fresh);
|
|
577
|
+
Object.assign(pullResponse, fresh);
|
|
578
|
+
let needsRefresh = false;
|
|
579
|
+
let deactivated = 0;
|
|
580
|
+
for (const [skillId, { skillName, agents }] of agentsBySkill) {
|
|
581
|
+
const skill = pullResponse.skills.find((item) => item.id === skillId);
|
|
582
|
+
const previous = previousState.skills[skillName];
|
|
583
|
+
if (!skill?.updated_at || previous?.cloudUpdatedAt !== skill.updated_at) continue;
|
|
584
|
+
const patch = Object.fromEntries([...agents].map((agent) => [agent, false]));
|
|
585
|
+
try {
|
|
586
|
+
const saved = await deps.updateAgentTargets(serverUrl, jwt, skillId, patch, skill.updated_at);
|
|
587
|
+
if (saved.success !== true || !saved.updated_at?.trim()
|
|
588
|
+
|| saved.updated_at === skill.updated_at
|
|
589
|
+
|| !['notis', 'claude_code', 'cursor', 'codex'].every((agent) =>
|
|
590
|
+
typeof saved.agent_targets?.[agent as keyof AgentTargets] === 'boolean')
|
|
591
|
+
|| ![...agents].every((agent) => saved.agent_targets[agent] === false)) {
|
|
592
|
+
throw new Error('Assignment update did not return a verified saved revision');
|
|
593
|
+
}
|
|
594
|
+
skill.agent_targets = saved.agent_targets;
|
|
595
|
+
skill.updated_at = saved.updated_at;
|
|
596
|
+
deactivated += agents.size;
|
|
597
|
+
} catch (error) {
|
|
598
|
+
needsRefresh = true;
|
|
599
|
+
failures.push({ name: skillName, error: 'Could not save the local agent removal; refreshed saved assignments' });
|
|
600
|
+
console.warn(`[skill-sync] Assignment changed or could not be saved for "${skillName}"; refreshing before reconciliation.`, error);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
if (needsRefresh) {
|
|
604
|
+
const refreshed = withoutBaseSkills(await deps.pullSkills(serverUrl, jwt));
|
|
605
|
+
assertSkillsPullAuthorized(refreshed);
|
|
606
|
+
Object.assign(pullResponse, refreshed);
|
|
607
|
+
}
|
|
608
|
+
return deactivated;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
export async function runSkillSync(
|
|
612
|
+
serverUrl: string,
|
|
613
|
+
jwt: string,
|
|
614
|
+
dependencies: Partial<RunSkillSyncDependencies> = {},
|
|
615
|
+
options: RunSkillSyncOptions = {},
|
|
616
|
+
): Promise<RunSkillSyncResult> {
|
|
617
|
+
const deps = {
|
|
618
|
+
...DEFAULT_RUN_SKILL_SYNC_DEPS,
|
|
619
|
+
...dependencies,
|
|
620
|
+
};
|
|
621
|
+
const syncSettings: SyncSettings = await deps.fetchSyncSettings(
|
|
622
|
+
serverUrl,
|
|
623
|
+
jwt,
|
|
624
|
+
);
|
|
625
|
+
// Supabase Desktop sessions use the auth-user id as JWT `sub`, while CLI
|
|
626
|
+
// OAuth credentials use the canonical Notis `users.user_id`. Trust the
|
|
627
|
+
// authenticated server response so both transports share one local mirror.
|
|
628
|
+
// The token fallback keeps the CLI compatible with an older server during
|
|
629
|
+
// rollout, where sync-settings did not yet return user_id.
|
|
630
|
+
const syncUserId = syncSettings.user_id?.trim() || decodeJwtSubject(jwt);
|
|
631
|
+
if (!syncUserId) {
|
|
632
|
+
throw new Error(
|
|
633
|
+
"Cannot sync skills without a server-verified account identity.",
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
const syncPaths = getSkillSyncPathsForUser(syncUserId);
|
|
637
|
+
const foreignLinksRemoved = await deps.removeForeignAccountSymlinks(
|
|
638
|
+
syncPaths.skillsDir,
|
|
639
|
+
);
|
|
640
|
+
if (options.honorSyncEnabled !== false && !syncSettings.sync_enabled) {
|
|
641
|
+
return {
|
|
642
|
+
syncEnabled: false,
|
|
643
|
+
pushed: 0,
|
|
644
|
+
pulled: 0,
|
|
645
|
+
downloaded: 0,
|
|
646
|
+
deleted: 0,
|
|
647
|
+
deactivated: 0,
|
|
648
|
+
linked: 0,
|
|
649
|
+
removed: foreignLinksRemoved,
|
|
650
|
+
skipped: 0,
|
|
651
|
+
lastSyncedAt: syncSettings.last_synced_at,
|
|
652
|
+
failedPushes: [],
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
let pullResponse = withoutBaseSkills(await deps.pullSkills(serverUrl, jwt));
|
|
657
|
+
assertSkillsPullAuthorized(pullResponse);
|
|
658
|
+
|
|
659
|
+
const cloudCuratedSkillNames = new Set(
|
|
660
|
+
pullResponse.skills
|
|
661
|
+
.filter((skill) => skill.source === "curated")
|
|
662
|
+
.map((skill) => skill.name),
|
|
663
|
+
);
|
|
664
|
+
const protectedSkillNames = new Set([...cloudCuratedSkillNames, ...BASE_SKILL_NAMES]);
|
|
665
|
+
const scopedState = withoutBaseSkillState(await deps.readSyncState(syncPaths));
|
|
666
|
+
const assignmentFailures: SkillSyncFailure[] = [];
|
|
667
|
+
const deactivated = syncSettings.agent_targets_conditional_updates === true
|
|
668
|
+
? await deactivateDeletedAgentSkills(
|
|
669
|
+
serverUrl, jwt, pullResponse, scopedState, scopedState, syncPaths.skillsDir, deps, assignmentFailures,
|
|
670
|
+
)
|
|
671
|
+
: 0;
|
|
672
|
+
const authUserId = decodeJwtSubject(jwt);
|
|
673
|
+
let previousAuthState: NotisSyncState | null = null;
|
|
674
|
+
if (authUserId && authUserId !== syncUserId) {
|
|
675
|
+
const previousAuthPaths = getSkillSyncPathsForUser(authUserId);
|
|
676
|
+
previousAuthState = await deps.readSyncState(previousAuthPaths);
|
|
677
|
+
await deps.gatherTopLevelLocalSkills(syncPaths, {
|
|
678
|
+
sourceRoots: [{ label: "previous-auth-scope", root: previousAuthPaths.skillsDir }],
|
|
679
|
+
protectedSkillNames,
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
await deps.gatherTopLevelLocalSkills(syncPaths, {
|
|
683
|
+
protectedSkillNames,
|
|
684
|
+
});
|
|
685
|
+
const localSkills = (await deps.scanLocalSkills(syncPaths))
|
|
686
|
+
.filter((skill) => !BASE_SKILL_NAMES.has(skill.name));
|
|
687
|
+
const previousState = withoutBaseSkillState(applyLegacyFirstRunState(
|
|
688
|
+
localSkills,
|
|
689
|
+
scopedState,
|
|
690
|
+
isEmptySyncState(scopedState)
|
|
691
|
+
? (!previousAuthState || isEmptySyncState(previousAuthState)
|
|
692
|
+
? await deps.readLegacySyncState(syncPaths)
|
|
693
|
+
: previousAuthState)
|
|
694
|
+
: null,
|
|
695
|
+
));
|
|
696
|
+
const gatheredSymlinkResult = await deps.syncSymlinks(
|
|
697
|
+
buildLocalSymlinkCandidates(pullResponse, localSkills, previousState),
|
|
698
|
+
syncPaths.skillsDir,
|
|
699
|
+
);
|
|
700
|
+
const pushCandidates = getPushCandidates(
|
|
701
|
+
localSkills,
|
|
702
|
+
previousState,
|
|
703
|
+
cloudCuratedSkillNames,
|
|
704
|
+
new Set(pullResponse.skills.map((skill) => skill.name)),
|
|
705
|
+
new Set(pullResponse.skills.filter((skill) => skill.app_owned || skill.owner_app_id).map((skill) => skill.name)),
|
|
706
|
+
);
|
|
707
|
+
|
|
708
|
+
const failedPushes: SkillSyncFailure[] = [];
|
|
709
|
+
if (pushCandidates.length > 0) {
|
|
710
|
+
const pushResult = await deps.pushChangedSkills(serverUrl, jwt, pushCandidates);
|
|
711
|
+
if (Array.isArray(pushResult?.failed) && pushResult.failed.length > 0) {
|
|
712
|
+
failedPushes.push(...pushResult.failed);
|
|
713
|
+
console.warn(
|
|
714
|
+
`[skill-sync] ${pushResult.failed.length} skill(s) were rejected during push: ` +
|
|
715
|
+
pushResult.failed.map((f) => `${f.name} (${f.error})`).join("; "),
|
|
716
|
+
);
|
|
717
|
+
}
|
|
718
|
+
pullResponse = withoutBaseSkills(await deps.pullSkills(serverUrl, jwt));
|
|
719
|
+
assertSkillsPullAuthorized(pullResponse);
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
// Delete phase: remove skills that were previously synced but are no longer in the cloud
|
|
723
|
+
const cloudSkillNames = new Set(pullResponse.skills.map((s) => s.name));
|
|
724
|
+
let deleted = 0;
|
|
725
|
+
for (const skillName of Object.keys(previousState.skills)) {
|
|
726
|
+
if (!cloudSkillNames.has(skillName)) {
|
|
727
|
+
await deps.deleteLocalSkill(skillName, syncPaths);
|
|
728
|
+
await deps.removeAllSymlinksForSkill(skillName, syncPaths.skillsDir);
|
|
729
|
+
deleted += 1;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
const failedDownloads: SkillSyncFailure[] = [];
|
|
734
|
+
const writtenSkillNames = new Set<string>();
|
|
735
|
+
const appliedFilesByName: Record<string, string[]> = {};
|
|
736
|
+
const downloaded = await writePulledSkillsToScopedMirror(
|
|
737
|
+
pullResponse,
|
|
738
|
+
localSkills,
|
|
739
|
+
previousState,
|
|
740
|
+
syncPaths,
|
|
741
|
+
deps,
|
|
742
|
+
failedDownloads,
|
|
743
|
+
writtenSkillNames,
|
|
744
|
+
appliedFilesByName,
|
|
745
|
+
);
|
|
746
|
+
|
|
747
|
+
const finalLocalSkills = (await deps.scanLocalSkills(syncPaths))
|
|
748
|
+
.filter((skill) => !BASE_SKILL_NAMES.has(skill.name));
|
|
749
|
+
const symlinkResult = await deps.syncSymlinks(
|
|
750
|
+
buildLocalSymlinkCandidates(pullResponse, finalLocalSkills, previousState),
|
|
751
|
+
syncPaths.skillsDir,
|
|
752
|
+
);
|
|
753
|
+
const verifiedLinks = { ...(symlinkResult.verifiedAgentLinks ?? {}) };
|
|
754
|
+
for (const failure of failedDownloads) delete verifiedLinks[failure.name];
|
|
755
|
+
const lastSyncedAt = pullResponse.last_synced_at || new Date().toISOString();
|
|
756
|
+
|
|
757
|
+
await deps.writeSyncState(
|
|
758
|
+
buildSyncState(
|
|
759
|
+
pullResponse,
|
|
760
|
+
finalLocalSkills,
|
|
761
|
+
lastSyncedAt,
|
|
762
|
+
verifiedLinks,
|
|
763
|
+
new Set(failedDownloads.map(item => item.name)),
|
|
764
|
+
collectAppliedCloudRevisions(pullResponse, previousState, writtenSkillNames),
|
|
765
|
+
collectAppliedFiles(pullResponse, previousState, appliedFilesByName),
|
|
766
|
+
),
|
|
767
|
+
syncPaths,
|
|
768
|
+
);
|
|
769
|
+
|
|
770
|
+
return {
|
|
771
|
+
syncEnabled: true,
|
|
772
|
+
pushed: pushCandidates.length,
|
|
773
|
+
pulled: pullResponse.skills.length,
|
|
774
|
+
downloaded,
|
|
775
|
+
deleted,
|
|
776
|
+
deactivated,
|
|
777
|
+
linked: gatheredSymlinkResult.linked + symlinkResult.linked,
|
|
778
|
+
removed: foreignLinksRemoved + gatheredSymlinkResult.removed + symlinkResult.removed,
|
|
779
|
+
skipped: symlinkResult.skipped,
|
|
780
|
+
failedLinks: [...assignmentFailures, ...failedDownloads, ...(symlinkResult.failures ?? []).filter(
|
|
781
|
+
(failure) => !failedDownloads.some((download) => download.name === failure.name),
|
|
782
|
+
)],
|
|
783
|
+
lastSyncedAt,
|
|
784
|
+
failedPushes,
|
|
785
|
+
};
|
|
786
|
+
}
|