@@ -14,17 +14,18 @@ import {
1414import { eq , desc , sql } from 'drizzle-orm'
1515import { requireCapability } from './auth.server'
1616import {
17- searchIntentPackages ,
1817 fetchPackument ,
19- isIntentCompatible ,
2018 selectVersionsToSync ,
2119 extractSkillsFromTarball ,
2220} from './intent.server'
21+ import {
22+ discoverIntentPackagesFromNpm ,
23+ discoverIntentPackagesViaGitHub ,
24+ } from './intent-discovery.server'
2325import {
2426 upsertIntentPackage ,
2527 getKnownVersions ,
2628 enqueuePackageVersion ,
27- markPackageVerified ,
2829 replaceSkillsForVersion ,
2930 getPendingVersions ,
3031 markVersionSynced ,
@@ -154,48 +155,7 @@ export const triggerIntentDiscover = createServerFn({
154155} ) . handler ( async ( ) => {
155156 await requireCapability ( { data : { capability : 'admin' } } )
156157
157- let packagesDiscovered = 0
158- let packagesVerified = 0
159- let versionsEnqueued = 0
160- const errors : Array < string > = [ ]
161-
162- const searchResults = await searchIntentPackages ( )
163- packagesDiscovered = searchResults . objects . length
164-
165- for ( const { package : pkg } of searchResults . objects ) {
166- try {
167- await upsertIntentPackage ( { name : pkg . name , verified : false } )
168-
169- const packument = await fetchPackument ( pkg . name )
170- const latestVersion = packument [ 'dist-tags' ] . latest
171- if ( ! latestVersion ) continue
172-
173- const latestMeta = packument . versions [ latestVersion ]
174- if ( ! latestMeta || ! isIntentCompatible ( latestMeta ) ) continue
175-
176- await markPackageVerified ( pkg . name )
177- packagesVerified ++
178-
179- const knownVersions = await getKnownVersions ( pkg . name )
180- const versionsToEnqueue = selectVersionsToSync ( packument , knownVersions )
181-
182- for ( const { version, tarball, publishedAt } of versionsToEnqueue ) {
183- await enqueuePackageVersion ( {
184- packageName : pkg . name ,
185- version,
186- tarballUrl : tarball ,
187- publishedAt,
188- } )
189- versionsEnqueued ++
190- }
191- } catch ( err ) {
192- errors . push (
193- `${ pkg . name } : ${ err instanceof Error ? err . message : String ( err ) } ` ,
194- )
195- }
196- }
197-
198- return { packagesDiscovered, packagesVerified, versionsEnqueued, errors }
158+ return discoverIntentPackagesFromNpm ( )
199159} )
200160
201161// ---------------------------------------------------------------------------
@@ -294,120 +254,7 @@ export const discoverViaGitHub = createServerFn({ method: 'POST' }).handler(
294254 async ( ) => {
295255 await requireCapability ( { data : { capability : 'admin' } } )
296256
297- const token = process . env . GITHUB_AUTH_TOKEN
298- if ( ! token ) throw new Error ( 'GITHUB_AUTH_TOKEN not set' )
299-
300- const ghHeaders = {
301- Authorization : `Bearer ${ token } ` ,
302- Accept : 'application/vnd.github.v3+json' ,
303- }
304-
305- // Search GitHub for package.json files referencing @tanstack /intent
306- const searchRes = await fetch (
307- 'https://api.github.com/search/code?q=%22%40tanstack%2Fintent%22+filename%3Apackage.json&per_page=100' ,
308- { headers : ghHeaders } ,
309- )
310- if ( ! searchRes . ok )
311- throw new Error ( `GitHub search failed: ${ searchRes . status } ` )
312- const searchData = ( await searchRes . json ( ) ) as {
313- total_count : number
314- items : Array < {
315- path : string
316- repository : { full_name : string }
317- url : string
318- } >
319- }
320-
321- // Deduplicate: one package.json per repo+path
322- const seen = new Set < string > ( )
323- const candidates : Array < { repo : string ; path : string } > = [ ]
324- for ( const item of searchData . items ) {
325- const key = `${ item . repository . full_name } |${ item . path } `
326- if ( ! seen . has ( key ) ) {
327- seen . add ( key )
328- candidates . push ( { repo : item . repository . full_name , path : item . path } )
329- }
330- }
331-
332- const results = {
333- searched : candidates . length ,
334- checkedOnNpm : 0 ,
335- hadSkills : 0 ,
336- enqueued : 0 ,
337- skipped : 0 ,
338- errors : [ ] as Array < string > ,
339- }
340-
341- for ( const { repo, path } of candidates ) {
342- try {
343- // Fetch the package.json content from GitHub
344- const contentRes = await fetch (
345- `https://api.github.com/repos/${ repo } /contents/${ path } ` ,
346- { headers : ghHeaders } ,
347- )
348- if ( ! contentRes . ok ) continue
349- const contentData = ( await contentRes . json ( ) ) as {
350- content ?: string
351- encoding ?: string
352- }
353- if ( ! contentData . content ) continue
354-
355- const pkgJson = JSON . parse (
356- Buffer . from ( contentData . content , 'base64' ) . toString ( 'utf-8' ) ,
357- ) as { name ?: string ; private ?: boolean }
358-
359- const pkgName = pkgJson . name
360- if ( ! pkgName || pkgJson . private ) continue
361-
362- results . checkedOnNpm ++
363-
364- // Check NPM for the package and its latest tarball
365- const npmRes = await fetch (
366- `https://registry.npmjs.org/${ encodeURIComponent ( pkgName ) } /latest` ,
367- )
368- if ( ! npmRes . ok ) continue // not published
369-
370- const npmMeta = ( await npmRes . json ( ) ) as {
371- version ?: string
372- dist ?: { tarball ?: string }
373- }
374- const version = npmMeta . version
375- const tarballUrl = npmMeta . dist ?. tarball
376- if ( ! version || ! tarballUrl ) continue
377-
378- // Peek at tarball for SKILL.md files (stream headers only)
379- const skills = await extractSkillsFromTarball ( tarballUrl )
380- if ( skills . length === 0 ) {
381- results . skipped ++
382- continue
383- }
384-
385- results . hadSkills ++
386-
387- // Seed into DB
388- await upsertIntentPackage ( { name : pkgName , verified : true } )
389-
390- const knownVersions = await getKnownVersions ( pkgName )
391- const packument = await fetchPackument ( pkgName )
392- const versionsToEnqueue = selectVersionsToSync ( packument , knownVersions )
393-
394- for ( const v of versionsToEnqueue ) {
395- await enqueuePackageVersion ( {
396- packageName : pkgName ,
397- version : v . version ,
398- tarballUrl : v . tarball ,
399- publishedAt : v . publishedAt ,
400- } )
401- results . enqueued ++
402- }
403- } catch ( err ) {
404- results . errors . push (
405- `${ repo } /${ path } : ${ err instanceof Error ? err . message : String ( err ) } ` ,
406- )
407- }
408- }
409-
410- return results
257+ return discoverIntentPackagesViaGitHub ( )
411258 } ,
412259)
413260
0 commit comments