-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathservice.ts
More file actions
164 lines (144 loc) 路 4.2 KB
/
Copy pathservice.ts
File metadata and controls
164 lines (144 loc) 路 4.2 KB
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
155
156
157
158
159
160
161
162
163
164
import { mkdir, readFile, rm, stat, writeFile } from 'node:fs/promises'
import { join, resolve, sep } from 'node:path'
import matter from 'gray-matter'
import { getSkillsDir } from '../lib/browseros-dir'
import { logger } from '../lib/logger'
import { isValidFrontmatter, loadAllSkills } from './loader'
import type {
CreateSkillInput,
SkillDetail,
SkillFrontmatter,
SkillMeta,
UpdateSkillInput,
} from './types'
export function slugify(name: string): string {
return name
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-|-$/g, '')
}
export function safeSkillDir(id: string): string {
const skillsDir = getSkillsDir()
const resolved = resolve(skillsDir, id)
if (!resolved.startsWith(`${skillsDir}${sep}`)) {
throw new Error('Invalid skill id')
}
return resolved
}
function buildSkillMd(frontmatter: SkillFrontmatter, content: string): string {
return matter.stringify(content, frontmatter)
}
async function fileExists(filePath: string): Promise<boolean> {
try {
await stat(filePath)
return true
} catch {
return false
}
}
export async function listSkills(): Promise<SkillMeta[]> {
return loadAllSkills(getSkillsDir())
}
export async function getSkill(id: string): Promise<SkillDetail | null> {
const skillMdPath = join(safeSkillDir(id), 'SKILL.md')
if (!(await fileExists(skillMdPath))) return null
try {
const raw = await readFile(skillMdPath, 'utf-8')
const parsed = matter(raw)
if (!isValidFrontmatter(parsed.data)) {
logger.warn('Skill has invalid frontmatter', { id })
return null
}
const meta = parsed.data.metadata
return {
id,
name: meta?.['display-name'] || parsed.data.name,
description: parsed.data.description,
location: skillMdPath,
enabled: meta?.enabled !== 'false',
version: meta?.version,
content: parsed.content.trim(),
}
} catch (err) {
logger.warn('Failed to read skill', {
id,
error: err instanceof Error ? err.message : String(err),
})
return null
}
}
export async function createSkill(input: CreateSkillInput): Promise<SkillMeta> {
const id = slugify(input.name)
if (!id) throw new Error('Invalid skill name')
const dirPath = safeSkillDir(id)
if (await fileExists(join(dirPath, 'SKILL.md'))) {
throw new Error(`Skill "${id}" already exists`)
}
await mkdir(dirPath, { recursive: true })
const frontmatter: SkillFrontmatter = {
name: id,
description: input.description,
metadata: {
'display-name': input.name,
enabled: 'true',
},
}
await writeFile(
join(dirPath, 'SKILL.md'),
buildSkillMd(frontmatter, input.content),
)
return {
id,
name: input.name,
description: input.description,
location: join(dirPath, 'SKILL.md'),
enabled: true,
}
}
export async function updateSkill(
id: string,
input: UpdateSkillInput,
): Promise<SkillMeta> {
const skillMdPath = join(safeSkillDir(id), 'SKILL.md')
if (!(await fileExists(skillMdPath))) {
throw new Error(`Skill "${id}" not found`)
}
const raw = await readFile(skillMdPath, 'utf-8')
const parsed = matter(raw)
if (!isValidFrontmatter(parsed.data)) {
throw new Error(`Skill "${id}" has invalid frontmatter`)
}
const existing = parsed.data
const existingMeta = existing.metadata ?? {}
const displayName =
input.name ?? existingMeta['display-name'] ?? existing.name
const description = input.description ?? existing.description
const content = input.content ?? parsed.content.trim()
const enabled = input.enabled ?? existingMeta.enabled !== 'false'
const frontmatter: SkillFrontmatter = {
...existing,
name: id,
description,
metadata: {
...existingMeta,
'display-name': displayName,
enabled: String(enabled),
},
}
await writeFile(skillMdPath, buildSkillMd(frontmatter, content))
return {
id,
name: displayName,
description,
location: skillMdPath,
enabled,
version: existingMeta.version,
}
}
export async function deleteSkill(id: string): Promise<void> {
const dirPath = safeSkillDir(id)
if (!(await fileExists(join(dirPath, 'SKILL.md')))) {
throw new Error(`Skill "${id}" not found`)
}
await rm(dirPath, { recursive: true })
}