-
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 10 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
265 changes: 265 additions & 0 deletions
265
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,265 @@ | ||
| import { createHash } from 'node:crypto' | ||
| import { mkdir, readFile, writeFile } from 'node:fs/promises' | ||
| import { join } from 'node:path' | ||
| import { SKILLS_LIMITS } from '@browseros/shared/constants/limits' | ||
| 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 { safeSkillDir } from './service' | ||
| import type { | ||
| ManagedSkillRecord, | ||
| RemoteSkillCatalog, | ||
| RemoteSkillEntry, | ||
| SkillManifest, | ||
| } from './types' | ||
|
|
||
| export const MANIFEST_FILE = '.remote-manifest.json' | ||
|
|
||
| let syncTimer: ReturnType<typeof setInterval> | null = null | ||
|
|
||
| export function extractVersion(content: string): string { | ||
| const match = content.match(/^\s*version:\s*["']?([^"'\n]+)["']?/m) | ||
| return match?.[1]?.trim() || '1.0' | ||
| } | ||
|
|
||
| export function contentHash(content: string): string { | ||
| return createHash('sha256').update(content).digest('hex') | ||
| } | ||
|
|
||
| function getManifestPath(): string { | ||
| return join(getSkillsDir(), MANIFEST_FILE) | ||
| } | ||
|
|
||
| function isValidManifest(data: unknown): data is SkillManifest { | ||
| if (typeof data !== 'object' || data === null) return false | ||
| const d = data as Record<string, unknown> | ||
| return typeof d.lastSyncedAt === 'string' && typeof d.skills === 'object' && d.skills !== null | ||
| } | ||
|
|
||
| function isValidSkillEntry(entry: unknown): entry is RemoteSkillEntry { | ||
| if (typeof entry !== 'object' || entry === null) return false | ||
| const e = entry as Record<string, unknown> | ||
| return ( | ||
| typeof e.id === 'string' && | ||
| typeof e.version === 'string' && | ||
| typeof e.content === 'string' | ||
| ) | ||
| } | ||
|
|
||
| function isValidCatalog(data: unknown): data is RemoteSkillCatalog { | ||
| if (typeof data !== 'object' || data === null) return false | ||
| const d = data as Record<string, unknown> | ||
| return ( | ||
| typeof d.version === 'number' && | ||
| Array.isArray(d.skills) && | ||
| d.skills.every(isValidSkillEntry) | ||
| ) | ||
| } | ||
|
|
||
| export async function loadManifest(): Promise<SkillManifest> { | ||
| try { | ||
| const raw = await readFile(getManifestPath(), 'utf-8') | ||
| const parsed: unknown = JSON.parse(raw) | ||
| if (!isValidManifest(parsed)) { | ||
| logger.warn('Invalid manifest file, resetting') | ||
| return { lastSyncedAt: '', skills: {} } | ||
| } | ||
| return parsed | ||
| } catch { | ||
| return { lastSyncedAt: '', skills: {} } | ||
| } | ||
| } | ||
|
|
||
| export 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 | ||
| } | ||
| const text = await response.text() | ||
| if (text.length > SKILLS_LIMITS.MAX_CATALOG_BYTES) { | ||
| logger.warn('Remote skill catalog response too large', { | ||
| size: text.length, | ||
| }) | ||
| return null | ||
| } | ||
| const data: unknown = JSON.parse(text) | ||
| if (!isValidCatalog(data)) { | ||
| logger.warn('Remote skill catalog has invalid format') | ||
| return null | ||
| } | ||
| return data | ||
| } 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 { | ||
| const safeDir = safeSkillDir(skillId) | ||
| return await readFile(join(safeDir, 'SKILL.md'), 'utf-8') | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| export async function writeSkillFile( | ||
| skillId: string, | ||
| content: string, | ||
| ): Promise<void> { | ||
| const safeDir = safeSkillDir(skillId) | ||
| await mkdir(safeDir, { recursive: true }) | ||
| await writeFile(join(safeDir, 'SKILL.md'), content) | ||
| } | ||
|
|
||
| export 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) { | ||
| try { | ||
| 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) { | ||
| result.skipped++ | ||
| continue | ||
| } | ||
|
|
||
| if (localRecord.version === remoteSkill.version) { | ||
| continue | ||
| } | ||
|
|
||
| if (isSkillCustomized(remoteSkill.id, localContent, manifest)) { | ||
| result.skipped++ | ||
| continue | ||
| } | ||
|
|
||
| await installSkill(remoteSkill, manifest) | ||
| result.updated++ | ||
| } catch (err) { | ||
| logger.warn('Failed to sync skill', { | ||
| id: remoteSkill.id, | ||
| error: err instanceof Error ? err.message : String(err), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| if (result.installed > 0 || result.updated > 0) { | ||
| 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 installSkill(skill, manifest) | ||
| seeded++ | ||
| } catch (err) { | ||
| logger.warn('Failed to seed remote skill', { | ||
| id: skill.id, | ||
| error: err instanceof Error ? err.message : String(err), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| if (seeded === 0) return false | ||
|
|
||
| manifest.lastSyncedAt = new Date().toISOString() | ||
| await saveManifest(manifest) | ||
| logger.info(`Seeded ${seeded}/${catalog.skills.length} skills from remote catalog`) | ||
|
|
||
| return seeded === catalog.skills.length | ||
| } | ||
|
|
||
| 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) | ||
|
|
||
| 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
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.