summaryrefslogtreecommitdiff
path: root/scripts/build-discovery-index.mjs
blob: 84a1e93a571554abef77dc625dedb348af0b6231 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env node

import { createHash } from 'node:crypto';
import { execFileSync } from 'node:child_process';
import {
  mkdirSync,
  rmSync,
  writeFileSync,
} from 'node:fs';
import { join } from 'node:path';
import { parse } from 'yaml';

const schema = 'https://schemas.agentskills.io/discovery/0.2.0/schema.json';
const baseUrl = process.argv[2];
const outputDirectory = 'dist';
const archiveEnvironment = {
  ...process.env,
  GIT_AUTHOR_DATE: '2000-01-01T00:00:00Z',
  GIT_AUTHOR_EMAIL: 'agent-skills@vercel.com',
  GIT_AUTHOR_NAME: 'Agent Skills',
  GIT_COMMITTER_DATE: '2000-01-01T00:00:00Z',
  GIT_COMMITTER_EMAIL: 'agent-skills@vercel.com',
  GIT_COMMITTER_NAME: 'Agent Skills',
};

if (!baseUrl) {
  throw new Error(
    'Usage: node scripts/build-discovery-index.mjs <artifact-base-url>',
  );
}

const readMetadata = (directory) => {
  const path = `skills/${directory}/SKILL.md`;
  const source = execFileSync('git', ['show', `HEAD:${path}`], {
    encoding: 'utf8',
  });
  const frontmatter = source.match(/^---\r?\n([\s\S]*?)\r?\n---/)?.[1];
  if (!frontmatter) throw new Error(`Missing frontmatter in ${path}`);

  let metadata;
  try {
    metadata = parse(frontmatter);
  } catch (error) {
    throw new Error(`Invalid frontmatter in ${path}`, { cause: error });
  }

  if (!metadata || typeof metadata !== 'object') {
    throw new Error(`Invalid frontmatter in ${path}`);
  }

  const { name, description } = metadata;
  if (typeof name !== 'string' || typeof description !== 'string') {
    throw new Error(`Missing name or description in ${path}`);
  }

  if (
    name.length === 0 ||
    name.length > 64 ||
    !/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name) ||
    !description ||
    description.length > 1024
  ) {
    throw new Error(`Invalid name or description in ${path}`);
  }

  return { name, description };
};

const createArchive = (directory) => {
  const tree = execFileSync(
    'git',
    ['rev-parse', `HEAD:skills/${directory}`],
    { encoding: 'utf8' },
  ).trim();
  const commit = execFileSync('git', ['commit-tree', tree], {
    encoding: 'utf8',
    env: archiveEnvironment,
    input: 'Agent Skills archive\n',
  }).trim();
  const tar = execFileSync('git', ['archive', '--format=tar', commit]);
  return execFileSync('gzip', ['-n', '-9', '-c'], { input: tar });
};

const createArtifact = (directory) => {
  const entries = execFileSync(
    'git',
    ['ls-tree', '-r', `HEAD:skills/${directory}`],
    { encoding: 'utf8' },
  )
    .trim()
    .split('\n');
  if (entries.some((entry) => !/^100(?:644|755) blob /.test(entry))) {
    throw new Error(`Unsupported archive entry in skills/${directory}`);
  }

  const files = entries.map((entry) => entry.slice(entry.indexOf('\t') + 1));

  if (files.length === 1 && files[0] === 'SKILL.md') {
    return {
      content: execFileSync('git', [
        'show',
        `HEAD:skills/${directory}/SKILL.md`,
      ]),
      extension: 'md',
      type: 'skill-md',
    };
  }

  return {
    content: createArchive(directory),
    extension: 'tar.gz',
    type: 'archive',
  };
};

rmSync(outputDirectory, { force: true, recursive: true });
mkdirSync(outputDirectory);

const directories = execFileSync(
  'git',
  ['ls-tree', '-d', '--name-only', 'HEAD:skills'],
  { encoding: 'utf8' },
)
  .trim()
  .split('\n');

const skills = directories
  .map((directory) => {
    const metadata = readMetadata(directory);
    const artifact = createArtifact(directory);
    const filename = `${metadata.name}.${artifact.extension}`;
    writeFileSync(join(outputDirectory, filename), artifact.content);

    return {
      ...metadata,
      type: artifact.type,
      url: `${baseUrl.replace(/\/$/, '')}/${filename}`,
      digest: `sha256:${createHash('sha256')
        .update(artifact.content)
        .digest('hex')}`,
    };
  })
  .sort((a, b) => a.name.localeCompare(b.name));

if (new Set(skills.map(({ name }) => name)).size !== skills.length) {
  throw new Error('Skill names must be unique');
}

writeFileSync(
  join(outputDirectory, 'index.json'),
  `${JSON.stringify({ $schema: schema, skills }, null, 2)}\n`,
);

console.log(`Published ${skills.length} skills to ${outputDirectory}`);