Repository navigation
Expand file tree
/
Copy pathskill.ts
More file actions
184 lines (164 loc) · 5.62 KB
/
Copy pathskill.ts
File metadata and controls
184 lines (164 loc) · 5.62 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
export * as Skill from "./skill.js"
import { makeLocationNode } from "@opencode/util/effect/app-node"
import { FSUtil } from "@opencode/util/fs-util"
import path from "path"
import { Context, Effect, Layer, Types } from "effect"
import { Skill } from "@opencode/schema/skill"
import { Bus } from "./bus.js"
import { Permission } from "./permission.js"
import { State } from "./state.js"
export const DirectorySource = Skill.DirectorySource
export type DirectorySource = Skill.DirectorySource
export const UrlSource = Skill.UrlSource
export type UrlSource = Skill.UrlSource
export const EmbeddedSource = Skill.EmbeddedSource
export type EmbeddedSource = Skill.EmbeddedSource
export const Source = Skill.Source
export type Source = Skill.Source
export const Info = Skill.Info
export type Info = Skill.Info
export const ID = Skill.ID
export type ID = Skill.ID
export const Name = Skill.Name
export type Name = Skill.Name
export { Event } from "@opencode/schema/skill"
export const available = (skills: ReadonlyArray<Info>, permissions: Permission.Ruleset) =>
skills.filter((skill) => Permission.evaluate("skill", skill.id, permissions).effect !== "deny")
export const uri = (id: string, file?: string) => (file ? `skill://${id}/${file}` : `skill://${id}`)
export const isUri = (value: string) => value.startsWith("skill://")
export const toModelOutput = (skill: Info, files: ReadonlyArray<string>) => {
const directory = isUri(skill.path) ? uri(skill.id) : path.dirname(skill.path)
return [
`<skill_content name="${skill.name}">`,
`# Skill: ${skill.name}`,
"",
skill.content.trim(),
"",
`Base directory for this skill: ${directory}`,
"Relative paths in this skill (e.g., scripts/, reference/) are relative to this base directory.",
"Note: file list is sampled.",
"",
"<skill_files>",
...files.map((file) => `<file>${file}</file>`),
"</skill_files>",
"</skill_content>",
].join("\n")
}
export const prepare = Effect.fn("Skill.prepare")(function* (
fs: FSUtil.Interface,
skill: Info,
virtualFiles?: ReadonlyMap<string, string>,
) {
if (isUri(skill.path)) {
const directory = uri(skill.id)
const files = Array.from(virtualFiles?.keys() ?? [])
.filter((file) => file !== "SKILL.md")
.toSorted()
.slice(0, 10)
.map((file) => uri(skill.id, file))
return {
directory,
output: toModelOutput(skill, files),
}
}
const directory = path.dirname(skill.path)
const files =
path.basename(skill.path) === "SKILL.md"
? (yield* fs.scan("**/*", {
cwd: directory,
absolute: true,
include: "file",
dot: true,
ignore: FSUtil.ignoreGlobs,
}))
.filter((file) => path.basename(file) !== "SKILL.md")
.toSorted()
.slice(0, 10)
: []
return {
directory,
output: toModelOutput(skill, files),
}
})
export type Data = {
skills: Map<ID, Types.DeepMutable<Info>>
files: Map<ID, ReadonlyMap<string, string>>
}
export type Editor = {
list: () => readonly Types.DeepMutable<Info>[]
get: (id: string) => Types.DeepMutable<Info> | undefined
add: (skill: Info, files?: Readonly<Record<string, string>>) => void
update: (id: string, update: (skill: Types.DeepMutable<Info>) => void) => void
remove: (id: string) => void
}
export interface Interface extends State.Transformable<Editor> {
readonly get: (id: ID) => Effect.Effect<Info | undefined>
readonly files: (id: ID) => Effect.Effect<ReadonlyMap<string, string> | undefined>
readonly list: () => Effect.Effect<Info[]>
}
export class Service extends Context.Service<Service, Interface>()("@opencode/Skill") {}
const layer = Layer.effect(
Service,
Effect.gen(function* () {
const bus = yield* Bus.Service
const state = State.create<Data, Editor>({
name: "skill",
initial: () => ({ skills: new Map(), files: new Map() }),
editor: (editor) => ({
list: () => Array.from(editor.skills.values()),
get: (id) => editor.skills.get(ID.make(id)),
add: (skill, files) => {
editor.skills.set(skill.id, { ...skill })
const entries = files
? Object.entries(files)
.filter(([file]) => file !== "SKILL.md")
.toSorted(([a], [b]) => a.localeCompare(b))
: []
if (entries.length === 0) {
editor.files.delete(skill.id)
return
}
editor.files.set(skill.id, new Map(entries))
},
update: (id, update) => {
const key = ID.make(id)
const current = editor.skills.get(key)
if (!current) return
update(current)
current.id = key
},
remove: (id) => {
const key = ID.make(id)
editor.skills.delete(key)
editor.files.delete(key)
},
}),
notify: State.changes(
(data) => [
Array.from(data.skills.values()),
Array.from(data.files, ([id, entries]) => [id, Array.from(entries)]),
],
() => bus.publish(Skill.Event.Updated, {}).pipe(Effect.asVoid),
),
})
return Service.of({
transform: state.transform,
reload: state.reload,
get: Effect.fn("Skill.get")(function* (id) {
return state.get().skills.get(id)
}),
files: Effect.fn("Skill.files")(function* (id) {
const current = state.get()
return current.skills.has(id) ? current.files.get(id) : undefined
}),
list: Effect.fn("Skill.list")(function* () {
return Array.from(state.get().skills.values())
}),
})
}),
)
export const node = makeLocationNode({
service: Service,
layer,
deps: [Bus.node],
})