Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

index.ts

64 lines2.2 KB

1/**
2 * `staticSkills` — wrap a build-time-generated catalog as a {@link SkillSource}.
3 *
4 * Edge-safe: this file imports no `node:*` modules. The Vite plugin that GLOBS
5 * `skills/<name>/SKILL.md` files and emits the catalog lives in `@tanstack/ai-skills/node`
6 * (it needs `node:fs`); the emitted catalog is plain data consumed here.
7 *
8 * The generated catalog is `as const`, so `T` is the literal union of skill
9 * names — which flows into `load_skill`'s enum, the constraint the spec
10 * recommends, obtained at build time rather than runtime.
11 */
12import type { SkillMetadata, SkillSource } from '../types'
13​
14export interface GeneratedSkill<T extends string = string> {
15 name: T
16 description: string
17 /** raw SKILL.md body, frontmatter stripped at generation time. */
18 body: string
19 compatibility?: string
20 /** embedded resource files, path → utf8 contents. */
21 resources?: Record<string, string>
23​
24export interface GeneratedCatalog<T extends string = string> {
25 revision: string
26 skills: ReadonlyArray<GeneratedSkill<T>>
28​
29export function staticSkills<T extends string>(
30 catalog: GeneratedCatalog<T>,
31): SkillSource & { names: ReadonlyArray<T> } {
32 const byName = new Map(catalog.skills.map((s) => [s.name, s]))
33 const get = (name: string): GeneratedSkill<T> => {
34 const s = byName.get(name as T)
35 if (!s) throw new Error(`static catalog has no skill named "${name}"`)
36 return s
37 }
38​
39 return {
40 names: catalog.skills.map((s) => s.name),
41 revision: () => Promise.resolve(catalog.revision),
42 list: () =>
43 Promise.resolve(
44 catalog.skills.map(
45 (s): SkillMetadata => ({
46 name: s.name,
47 description: s.description,
48 ...(s.compatibility && { compatibility: s.compatibility }),
49 }),
50 ),
51 ),
52 // Methods are async so a missing-skill throw surfaces as a rejected
53 // promise (what callers `await`), not a synchronous throw.
54 load: async (name) => get(name).body,
55 listResources: async (name) => Object.keys(get(name).resources ?? {}),
56 readResource: async (name, path) => {
57 const value = get(name).resources?.[path]
58 if (value === undefined) {
59 throw new Error(`skill "${name}" has no resource "${path}"`)
60 }
61 return value
62 },
63 }