summaryrefslogtreecommitdiffhomepage
path: root/packages/api/src/routes/skills.ts
blob: 7696b475476571ad1128f96f3999bf27b3469a1b (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
import type { AgentSkillMapping, SkillDefinition, SkillScope } from "@dispatch/core";
import { Hono } from "hono";

let getSkills: () => { skills: SkillDefinition[]; mappings: AgentSkillMapping[] } = () => ({
	skills: [],
	mappings: [],
});

export function setSkillsGetter(
	getter: () => { skills: SkillDefinition[]; mappings: AgentSkillMapping[] },
): void {
	getSkills = getter;
}

export const skillsRoutes = new Hono();

skillsRoutes.get("/", (c) => {
	const { skills, mappings } = getSkills();
	const skillSummaries = skills.map(({ name, description, tags, scope, directory }) => ({
		name,
		description,
		tags,
		scope,
		directory,
	}));
	return c.json({ skills: skillSummaries, mappings });
});

skillsRoutes.get("/:name", (c) => {
	const { name } = c.req.param();
	const scopeParam = c.req.query("scope") as SkillScope | undefined;
	const { skills } = getSkills();

	const matches = skills.filter((s) => s.name === name);
	if (matches.length === 0) {
		return c.json({ error: "Skill not found" }, 404);
	}

	if (scopeParam) {
		const scoped = matches.find((s) => s.scope === scopeParam);
		if (!scoped) {
			return c.json({ error: "Skill not found" }, 404);
		}
		return c.json(scoped);
	}

	return c.json(matches[0]);
});