-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: remote skill download and auto-sync #468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fd67305
feat: add remote skill download and auto-sync
shivammittal274 08b07d6
feat: make skills catalog URL configurable and add generation script
shivammittal274 cc069f4
feat: add R2 upload script and use cdn.browseros.com for catalog URL
shivammittal274 df149ab
test: add E2E tests for remote skill sync against live CDN
shivammittal274 ed456ab
fix: address code review findings — security, validation, DRY
shivammittal274 bb7c95d
test: add flow tests for all four sync scenarios against live CDN
shivammittal274 62817ce
refactor: remove redundant scripts and inline catalog generation
shivammittal274 d7f785d
test: add full E2E server flow test against live CDN
shivammittal274 bba8726
chore: remove e2e-server-flow test
shivammittal274 09f9a73
fix: address Greptile review — entry validation, size limit, DRY, no-…
shivammittal274 4792c3a
fix: prevent bundled fallback from overwriting partial remote seeds
shivammittal274 f35a5de
fix: run sync immediately on startup, not just on interval
shivammittal274 25a58ed
refactor: simplify sync — remote always wins, remove manifest
shivammittal274 12a5436
fix: skip bundled skills already installed by partial remote seed
shivammittal274 f3051f7
chore: remove unreliable Content-Length check
shivammittal274 e23e850
chore: remove size limit checks, fetch timeout is sufficient
shivammittal274 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
214 changes: 214 additions & 0 deletions
214
packages/browseros-agent/apps/server/src/skills/remote-sync.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| import { createHash } from 'node:crypto' | ||
| import { mkdir, readFile, writeFile } from 'node:fs/promises' | ||
| import { join } from 'node:path' | ||
| import { TIMEOUTS } from '@browseros/shared/constants/timeouts' | ||
| import { EXTERNAL_URLS } from '@browseros/shared/constants/urls' | ||
| import { INLINED_ENV } from '../env' | ||
| import { getSkillsDir } from '../lib/browseros-dir' | ||
| import { logger } from '../lib/logger' | ||
| import type { | ||
| ManagedSkillRecord, | ||
| RemoteSkillCatalog, | ||
| RemoteSkillEntry, | ||
| SkillManifest, | ||
| } from './types' | ||
|
|
||
| const MANIFEST_FILE = '.remote-manifest.json' | ||
|
|
||
| let syncTimer: ReturnType<typeof setInterval> | null = null | ||
|
|
||
| function contentHash(content: string): string { | ||
| return createHash('sha256').update(content).digest('hex') | ||
| } | ||
|
|
||
| function getManifestPath(): string { | ||
| return join(getSkillsDir(), MANIFEST_FILE) | ||
| } | ||
|
|
||
| export async function loadManifest(): Promise<SkillManifest> { | ||
| try { | ||
| const raw = await readFile(getManifestPath(), 'utf-8') | ||
| return JSON.parse(raw) as SkillManifest | ||
| } catch { | ||
| return { lastSyncedAt: '', skills: {} } | ||
| } | ||
| } | ||
|
|
||
| async function saveManifest(manifest: SkillManifest): Promise<void> { | ||
| await writeFile(getManifestPath(), JSON.stringify(manifest, null, 2)) | ||
| } | ||
|
|
||
| function getCatalogUrl(): string { | ||
| return INLINED_ENV.SKILLS_CATALOG_URL || EXTERNAL_URLS.SKILLS_CATALOG | ||
| } | ||
|
|
||
| export async function fetchRemoteCatalog(): Promise<RemoteSkillCatalog | null> { | ||
| try { | ||
| const response = await fetch(getCatalogUrl(), { | ||
| signal: AbortSignal.timeout(TIMEOUTS.SKILLS_FETCH), | ||
| }) | ||
| if (!response.ok) { | ||
| logger.warn('Failed to fetch remote skill catalog', { | ||
| status: response.status, | ||
| }) | ||
| return null | ||
| } | ||
| return (await response.json()) as RemoteSkillCatalog | ||
| } catch (err) { | ||
| logger.debug('Remote skill catalog unavailable', { | ||
| error: err instanceof Error ? err.message : String(err), | ||
| }) | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| function isSkillCustomized( | ||
| skillId: string, | ||
| currentContent: string, | ||
| manifest: SkillManifest, | ||
| ): boolean { | ||
| const record = manifest.skills[skillId] | ||
| if (!record) return false | ||
| return contentHash(currentContent) !== record.contentHash | ||
| } | ||
|
|
||
| async function readSkillContent(skillId: string): Promise<string | null> { | ||
| try { | ||
| return await readFile( | ||
| join(getSkillsDir(), skillId, 'SKILL.md'), | ||
| 'utf-8', | ||
| ) | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| async function writeSkillFile( | ||
| skillId: string, | ||
| content: string, | ||
| ): Promise<void> { | ||
| const targetDir = join(getSkillsDir(), skillId) | ||
| await mkdir(targetDir, { recursive: true }) | ||
| await writeFile(join(targetDir, 'SKILL.md'), content) | ||
| } | ||
|
|
||
| async function installSkill( | ||
| skill: RemoteSkillEntry, | ||
| manifest: SkillManifest, | ||
| ): Promise<void> { | ||
| await writeSkillFile(skill.id, skill.content) | ||
| manifest.skills[skill.id] = { | ||
| version: skill.version, | ||
| contentHash: contentHash(skill.content), | ||
| } | ||
| } | ||
|
|
||
| export async function syncRemoteSkills(): Promise<{ | ||
| installed: number | ||
| updated: number | ||
| skipped: number | ||
| }> { | ||
| const result = { installed: 0, updated: 0, skipped: 0 } | ||
| const catalog = await fetchRemoteCatalog() | ||
| if (!catalog) return result | ||
|
|
||
| const manifest = await loadManifest() | ||
|
|
||
| for (const remoteSkill of catalog.skills) { | ||
| const localContent = await readSkillContent(remoteSkill.id) | ||
| const localRecord: ManagedSkillRecord | undefined = | ||
| manifest.skills[remoteSkill.id] | ||
|
|
||
| if (!localContent) { | ||
| await installSkill(remoteSkill, manifest) | ||
| result.installed++ | ||
| continue | ||
| } | ||
|
|
||
| if (!localRecord) { | ||
| // Skill exists locally but isn't tracked — treat as user-managed | ||
| result.skipped++ | ||
| continue | ||
| } | ||
|
|
||
| if (localRecord.version === remoteSkill.version) { | ||
| continue | ||
| } | ||
|
|
||
| if (isSkillCustomized(remoteSkill.id, localContent, manifest)) { | ||
| result.skipped++ | ||
| continue | ||
| } | ||
|
|
||
| await installSkill(remoteSkill, manifest) | ||
| result.updated++ | ||
| } | ||
|
|
||
| manifest.lastSyncedAt = new Date().toISOString() | ||
| await saveManifest(manifest) | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| export async function seedFromRemote(): Promise<boolean> { | ||
| const catalog = await fetchRemoteCatalog() | ||
| if (!catalog || catalog.skills.length === 0) return false | ||
|
|
||
| const manifest = await loadManifest() | ||
| let seeded = 0 | ||
|
|
||
| for (const skill of catalog.skills) { | ||
| try { | ||
| await writeSkillFile(skill.id, skill.content) | ||
| manifest.skills[skill.id] = { | ||
| version: skill.version, | ||
| contentHash: contentHash(skill.content), | ||
| } | ||
| seeded++ | ||
| } catch (err) { | ||
| logger.warn('Failed to seed remote skill', { | ||
| id: skill.id, | ||
| error: err instanceof Error ? err.message : String(err), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| if (seeded > 0) { | ||
| manifest.lastSyncedAt = new Date().toISOString() | ||
| await saveManifest(manifest) | ||
| logger.info(`Seeded ${seeded} skills from remote catalog`) | ||
| } | ||
|
|
||
| return seeded > 0 | ||
| } | ||
|
|
||
| export function startSkillSync(): void { | ||
| if (syncTimer) return | ||
|
|
||
| syncTimer = setInterval(async () => { | ||
| try { | ||
| const { installed, updated, skipped } = await syncRemoteSkills() | ||
| if (installed > 0 || updated > 0) { | ||
| logger.info('Remote skill sync completed', { | ||
| installed, | ||
| updated, | ||
| skipped, | ||
| }) | ||
| } | ||
| } catch (err) { | ||
| logger.warn('Skill sync failed', { | ||
| error: err instanceof Error ? err.message : String(err), | ||
| }) | ||
| } | ||
| }, TIMEOUTS.SKILLS_SYNC_INTERVAL) | ||
|
|
||
| // Don't block process exit | ||
| syncTimer.unref() | ||
| } | ||
|
|
||
| export function stopSkillSync(): void { | ||
| if (syncTimer) { | ||
| clearInterval(syncTimer) | ||
| syncTimer = null | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.