kira-glasses-bridge 2.22.0 → 2.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/agent-files.mjs +139 -0
- package/src/agents/claude.mjs +12 -2
- package/src/config-folders.mjs +124 -0
- package/src/index.mjs +4 -0
- package/src/skill-files.mjs +119 -0
package/package.json
CHANGED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// Subagents on this Mac: Claude Code's (one Markdown file each in ~/.claude/agents, front matter
|
|
2
|
+
// for its settings and the body as its system prompt, with `memory: user` kept in
|
|
3
|
+
// ~/.claude/agent-memory/<name>/MEMORY.md) and Codex's (~/.codex/agents/<name>.toml, listed only:
|
|
4
|
+
// the bridge doesn't run Codex yet). Kira's agents are Claude Code subagents: she writes their
|
|
5
|
+
// files and memory here, and a task runs as one (agents/claude.mjs).
|
|
6
|
+
import { readdir, unlink } from 'node:fs/promises'
|
|
7
|
+
import { join } from 'node:path'
|
|
8
|
+
import {
|
|
9
|
+
FOLDERS,
|
|
10
|
+
agentFolders,
|
|
11
|
+
display,
|
|
12
|
+
exists,
|
|
13
|
+
field,
|
|
14
|
+
inside,
|
|
15
|
+
readSmall,
|
|
16
|
+
refuseLinks,
|
|
17
|
+
splitFront,
|
|
18
|
+
writeWhole,
|
|
19
|
+
} from './config-folders.mjs'
|
|
20
|
+
|
|
21
|
+
const MAX_MD = 256 * 1024
|
|
22
|
+
/** Claude Code loads the first 200 lines or 25KB of MEMORY.md; Kira keeps it to this. */
|
|
23
|
+
const MAX_MEMORY = 64 * 1024
|
|
24
|
+
const WHAT = 'a subagent'
|
|
25
|
+
const NAMED = { loose: true }
|
|
26
|
+
const claudeRoot = () => agentFolders()[0].root
|
|
27
|
+
|
|
28
|
+
/** A TOML string value on its own line (`name = "x"`), enough for Codex's name and description. */
|
|
29
|
+
const tomlField = (toml, key) => {
|
|
30
|
+
const m = new RegExp(`^${key}\\s*=\\s*(?:"((?:[^"\\\\]|\\\\.)*)"|'([^']*)')`, 'm').exec(toml)
|
|
31
|
+
return m ? (m[1] ?? m[2]).replace(/\\"/g, '"') : null
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The name and description a tool shows, from a subagent file (its file name when it has none). */
|
|
35
|
+
function agentSummary(text, slug, ext = '.md') {
|
|
36
|
+
if (ext === '.toml') {
|
|
37
|
+
return { name: tomlField(text, 'name') || slug, description: tomlField(text, 'description') || '' }
|
|
38
|
+
}
|
|
39
|
+
const { front } = splitFront(text)
|
|
40
|
+
return { name: field(front, 'name') || slug, description: field(front, 'description') || '' }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const memoryFile = (name, root = FOLDERS.claude.agentMemory()) => join(inside(root, name, WHAT, NAMED), 'MEMORY.md')
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Every subagent file: its slug (the file name), name, description, where it is, its text, which
|
|
47
|
+
* tool loads it, and for Claude Code's its MEMORY.md (empty when it has none).
|
|
48
|
+
*/
|
|
49
|
+
export async function listAgents(folders = agentFolders(), memoryRoot = FOLDERS.claude.agentMemory()) {
|
|
50
|
+
const agents = []
|
|
51
|
+
for (const { root, loadedBy, ext } of folders) {
|
|
52
|
+
const entries = await readdir(root, { withFileTypes: true }).catch(() => [])
|
|
53
|
+
for (const e of entries) {
|
|
54
|
+
if (!e.name.endsWith(ext) || !(e.isFile() || e.isSymbolicLink())) continue
|
|
55
|
+
const slug = e.name.slice(0, -ext.length)
|
|
56
|
+
const text = await readSmall(join(root, e.name), MAX_MD)
|
|
57
|
+
if (text === null) continue
|
|
58
|
+
const summary = agentSummary(text, slug, ext)
|
|
59
|
+
const memory = loadedBy.includes('claude') ? await readMemory(summary.name, memoryRoot) : ''
|
|
60
|
+
agents.push({ slug, ...summary, path: display(join(root, e.name)), md: text, providers: loadedBy, memory })
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
agents.sort((a, b) => a.slug.localeCompare(b.slug) || a.path.localeCompare(b.path))
|
|
64
|
+
return { root: display(folders[0].root), agents }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function readMemory(name, root) {
|
|
68
|
+
try {
|
|
69
|
+
return (await readSmall(memoryFile(name, root), MAX_MEMORY)) ?? ''
|
|
70
|
+
} catch {
|
|
71
|
+
return '' // a name Kira can't make a folder of has no memory she can read
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** One of Claude Code's subagents, with its memory. */
|
|
76
|
+
export async function readAgent({ slug } = {}, root = claudeRoot(), memoryRoot = FOLDERS.claude.agentMemory()) {
|
|
77
|
+
const file = inside(root, slug, WHAT, { ...NAMED, ext: '.md' })
|
|
78
|
+
const md = await readSmall(file, MAX_MD)
|
|
79
|
+
if (md === null) return { error: `No subagent at ${display(file)}` }
|
|
80
|
+
const summary = agentSummary(md, slug)
|
|
81
|
+
return { slug, ...summary, path: display(file), md, memory: await readMemory(summary.name, memoryRoot) }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Write a Claude Code subagent's file, whole: replacing the one there, or with `create` only a new
|
|
86
|
+
* one (a subagent already there, Kira's or not, is never overwritten by a new one of that name).
|
|
87
|
+
*/
|
|
88
|
+
export async function writeAgent({ slug, md, create = false } = {}, root = claudeRoot()) {
|
|
89
|
+
const file = inside(root, slug, WHAT, { ...NAMED, ext: '.md' })
|
|
90
|
+
const text = String(md ?? '')
|
|
91
|
+
if (!/^---\r?\n/.test(text)) throw new Error('A subagent file starts with front matter between --- lines')
|
|
92
|
+
if (Buffer.byteLength(text) > MAX_MD) throw new Error('That subagent file is too large')
|
|
93
|
+
await refuseLinks([root, file], 'edit')
|
|
94
|
+
if (create && (await exists(file))) throw new Error(`A subagent named ${slug} is already on your Mac`)
|
|
95
|
+
await writeWhole(file, text)
|
|
96
|
+
return { slug, ...agentSummary(text, slug), path: display(file), providers: ['claude'] }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Remove a Claude Code subagent's file. Its memory folder stays (it's what it learned). */
|
|
100
|
+
export async function removeAgent({ slug } = {}, root = claudeRoot()) {
|
|
101
|
+
const file = inside(root, slug, WHAT, { ...NAMED, ext: '.md' })
|
|
102
|
+
await refuseLinks([root, file], 'remove')
|
|
103
|
+
const removed = await unlink(file).then(
|
|
104
|
+
() => true,
|
|
105
|
+
e => {
|
|
106
|
+
if (e.code === 'ENOENT') return false
|
|
107
|
+
throw e
|
|
108
|
+
},
|
|
109
|
+
)
|
|
110
|
+
return { slug, removed, path: display(file) }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Replace a subagent's MEMORY.md, whole (`name` is its front-matter name). */
|
|
114
|
+
export async function writeAgentMemory({ name, md } = {}, root = FOLDERS.claude.agentMemory()) {
|
|
115
|
+
const file = memoryFile(name, root)
|
|
116
|
+
const text = String(md ?? '')
|
|
117
|
+
if (Buffer.byteLength(text) > MAX_MEMORY) throw new Error('That memory is too large')
|
|
118
|
+
await refuseLinks([root, join(file, '..'), file], 'edit')
|
|
119
|
+
await writeWhole(file, text)
|
|
120
|
+
return { name, path: display(file), memory: text }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Add a line to a subagent's MEMORY.md (what Kira keeps after a task); the oldest go when it's full. */
|
|
124
|
+
export async function rememberAgent({ name, line } = {}, root = FOLDERS.claude.agentMemory()) {
|
|
125
|
+
const entry = `- ${String(line ?? '').replace(/\s+/g, ' ').trim().slice(0, 400)}`
|
|
126
|
+
let text = `${(await readMemory(name, root)).trimEnd()}\n${entry}`.trim() + '\n'
|
|
127
|
+
while (Buffer.byteLength(text) > MAX_MEMORY && text.includes('\n')) text = text.slice(text.indexOf('\n') + 1)
|
|
128
|
+
return writeAgentMemory({ name, md: text }, root)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The bridge tools (index.mjs). */
|
|
132
|
+
export const agentTools = {
|
|
133
|
+
list_subagents: () => listAgents(),
|
|
134
|
+
read_subagent: args => readAgent(args),
|
|
135
|
+
write_subagent: args => writeAgent(args),
|
|
136
|
+
remove_subagent: args => removeAgent(args),
|
|
137
|
+
write_subagent_memory: args => writeAgentMemory(args),
|
|
138
|
+
remember_subagent: args => rememberAgent(args),
|
|
139
|
+
}
|
package/src/agents/claude.mjs
CHANGED
|
@@ -128,6 +128,11 @@ function applyAnswer(name, input, request, answer, suggestions) {
|
|
|
128
128
|
return { behavior: 'deny', message: `The user declined this${answer.text ? ` ("${answer.text}")` : ''}. Do not achieve the same effect another way; if the task cannot proceed without it, stop and say what you could not do.` }
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
+
/** Claude Code's own prompt, with what Kira adds for this run after it. */
|
|
132
|
+
function systemPromptFor(persona) {
|
|
133
|
+
return { type: 'preset', preset: 'claude_code', ...(persona.systemPrompt ? { append: persona.systemPrompt } : {}) }
|
|
134
|
+
}
|
|
135
|
+
|
|
131
136
|
/**
|
|
132
137
|
* Run one instruction in a Claude session.
|
|
133
138
|
* @param io.emit(event) a protocol event
|
|
@@ -221,8 +226,13 @@ async function run({ prompt, sessionId, cwd, policy, persona, settings, abort },
|
|
|
221
226
|
// The model, effort and permission mode the person chose in the composer (plan mode: plan first).
|
|
222
227
|
...sdkOptionsFor(settings),
|
|
223
228
|
...(abort ? { abortController: abort } : {}),
|
|
224
|
-
// A
|
|
225
|
-
|
|
229
|
+
// A Kira agent runs as its Claude Code subagent (~/.claude/agents/<name>.md, agent-files.mjs): the
|
|
230
|
+
// file's body takes the place of Claude Code's default instructions, and what Kira adds for the
|
|
231
|
+
// run (its preset's conventions, the team) is appended. The claude_code preset has to be asked
|
|
232
|
+
// for: without it the SDK's bare prompt leaves the subagent's out (checked with SDK 0.3.278).
|
|
233
|
+
...(persona?.agent ? { agent: persona.agent } : {}),
|
|
234
|
+
...(persona?.systemPrompt || persona?.agent ? { systemPrompt: systemPromptFor(persona) } : {}),
|
|
235
|
+
// Its preset says which tools it does without (on top of any its file lists).
|
|
226
236
|
...(persona?.disallowedTools?.length ? { disallowedTools: persona.disallowedTools } : {}),
|
|
227
237
|
// Claude as the person's Kira: her tools come from her own MCP server, with a token the API
|
|
228
238
|
// made for this conversation. Claude Code loads them as it needs them (no tool ceiling).
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Where the coding agents on this Mac keep the things Kira reads and writes (skills, subagents and
|
|
2
|
+
// their memory), and the one way to touch them: a name is one plain file or folder name inside its
|
|
3
|
+
// folder, a folder that links elsewhere is never written through, and every write is whole and
|
|
4
|
+
// atomic (a temporary file renamed over the old one).
|
|
5
|
+
//
|
|
6
|
+
// Which tool loads which folder, from their docs (2026-10-12):
|
|
7
|
+
// Claude Code (code.claude.com/docs/en/skills, /sub-agents): ~/.claude/skills/<name>/SKILL.md,
|
|
8
|
+
// ~/.claude/agents/<name>.md, and a subagent's `memory: user` in ~/.claude/agent-memory/<name>/.
|
|
9
|
+
// It doesn't read ~/.agents/skills.
|
|
10
|
+
// Codex (developers.openai.com/codex: skills, subagents): ~/.agents/skills/<name>/SKILL.md (the
|
|
11
|
+
// shared Agent Skills folder) and ~/.codex/agents/<name>.toml. ~/.codex/skills isn't in its docs,
|
|
12
|
+
// but the Codex app keeps its own skills there, so Kira lists them as Codex's.
|
|
13
|
+
import { lstat, mkdir, readFile, rename, stat, writeFile } from 'node:fs/promises'
|
|
14
|
+
import { homedir } from 'node:os'
|
|
15
|
+
import { join, relative } from 'node:path'
|
|
16
|
+
|
|
17
|
+
const claudeHome = () => process.env.CLAUDE_CONFIG_DIR || join(homedir(), '.claude')
|
|
18
|
+
const codexHome = () => process.env.CODEX_HOME || join(homedir(), '.codex')
|
|
19
|
+
|
|
20
|
+
/** Each provider's folders. `shared` is the cross-tool Agent Skills folder. */
|
|
21
|
+
export const FOLDERS = {
|
|
22
|
+
claude: {
|
|
23
|
+
skills: () => join(claudeHome(), 'skills'),
|
|
24
|
+
agents: () => join(claudeHome(), 'agents'),
|
|
25
|
+
agentMemory: () => join(claudeHome(), 'agent-memory'),
|
|
26
|
+
},
|
|
27
|
+
codex: { skills: () => join(codexHome(), 'skills'), agents: () => join(codexHome(), 'agents') },
|
|
28
|
+
shared: { skills: () => join(homedir(), '.agents', 'skills') },
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The skills folders and who loads each; Kira writes only the first (Claude Code's). */
|
|
32
|
+
export const skillFolders = () => [
|
|
33
|
+
{ root: FOLDERS.claude.skills(), loadedBy: ['claude'] },
|
|
34
|
+
{ root: FOLDERS.shared.skills(), loadedBy: ['codex'] },
|
|
35
|
+
{ root: FOLDERS.codex.skills(), loadedBy: ['codex'] },
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
/** The subagent folders, their file format and who loads each; Kira writes only Claude Code's. */
|
|
39
|
+
export const agentFolders = () => [
|
|
40
|
+
{ root: FOLDERS.claude.agents(), loadedBy: ['claude'], ext: '.md' },
|
|
41
|
+
{ root: FOLDERS.codex.agents(), loadedBy: ['codex'], ext: '.toml' },
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
/** One plain name: lowercase letters, digits and dashes (and dots and underscores where `loose`). */
|
|
45
|
+
const STRICT = /^[a-z0-9][a-z0-9-]{0,63}$/
|
|
46
|
+
const LOOSE = /^[a-z0-9][a-z0-9._-]{0,63}$/
|
|
47
|
+
export const isName = (s, loose = false) => (loose ? LOOSE : STRICT).test(String(s ?? ''))
|
|
48
|
+
|
|
49
|
+
/** The path for `name` in `root`, after checking it can't name anything else. */
|
|
50
|
+
export function inside(root, name, what, { loose = false, ext = '' } = {}) {
|
|
51
|
+
const s = String(name ?? '')
|
|
52
|
+
if (!isName(s, loose)) {
|
|
53
|
+
const allowed = loose
|
|
54
|
+
? 'lowercase letters, digits, dots, dashes and underscores'
|
|
55
|
+
: 'lowercase letters, digits and dashes'
|
|
56
|
+
throw new Error(`“${s}” isn’t ${what} name (${allowed})`)
|
|
57
|
+
}
|
|
58
|
+
return join(root, s + ext)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** As people read it: under the home folder with ~. */
|
|
62
|
+
export function display(full) {
|
|
63
|
+
const rel = relative(homedir(), full)
|
|
64
|
+
return rel && !rel.startsWith('..') ? `~/${rel}` : full
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const isSymlink = p =>
|
|
68
|
+
lstat(p).then(
|
|
69
|
+
s => s.isSymbolicLink(),
|
|
70
|
+
() => false,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
/** Anything on the way that links elsewhere is changed where it really is, never through the link. */
|
|
74
|
+
export async function refuseLinks(paths, verb) {
|
|
75
|
+
for (const p of paths) {
|
|
76
|
+
if (await isSymlink(p)) throw new Error(`${display(p)} is a link to somewhere else; ${verb} it there`)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** A file's text when it's a file no bigger than `max`, else null. */
|
|
81
|
+
export async function readSmall(file, max) {
|
|
82
|
+
const st = await stat(file).catch(() => null)
|
|
83
|
+
if (!st?.isFile() || st.size > max) return null
|
|
84
|
+
return readFile(file, 'utf8').catch(() => null)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Whether something is at `p`. */
|
|
88
|
+
export const exists = p => stat(p).then(
|
|
89
|
+
() => true,
|
|
90
|
+
() => false,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
/** Write `text` to `file` whole: its folder made, through a temporary file renamed over the old one. */
|
|
94
|
+
export async function writeWhole(file, text) {
|
|
95
|
+
const dir = join(file, '..')
|
|
96
|
+
await mkdir(dir, { recursive: true })
|
|
97
|
+
const tmp = join(dir, `.${file.slice(dir.length + 1)}.${process.pid}.tmp`)
|
|
98
|
+
await writeFile(tmp, text, 'utf8')
|
|
99
|
+
await rename(tmp, file)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Front matter between --- lines, and the rest. */
|
|
103
|
+
export function splitFront(md) {
|
|
104
|
+
const m = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/.exec(md)
|
|
105
|
+
return m ? { front: m[1].replace(/\r\n/g, '\n'), body: md.slice(m[0].length) } : { front: '', body: md }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** A front-matter value: quoted or plain on its line, or a `|` / `>` block below it. */
|
|
109
|
+
export function field(front, key) {
|
|
110
|
+
const lines = front.split('\n')
|
|
111
|
+
const at = lines.findIndex(l => l.startsWith(`${key}:`))
|
|
112
|
+
if (at < 0) return null
|
|
113
|
+
const raw = lines[at].slice(key.length + 1).trim()
|
|
114
|
+
if (/^[|>][+-]?$/.test(raw)) {
|
|
115
|
+
const block = []
|
|
116
|
+
for (const l of lines.slice(at + 1)) {
|
|
117
|
+
if (l.trim() && !/^\s/.test(l)) break
|
|
118
|
+
block.push(l.trim())
|
|
119
|
+
}
|
|
120
|
+
return block.join(raw.startsWith('|') ? '\n' : ' ').trim() || null
|
|
121
|
+
}
|
|
122
|
+
const quoted = /^(["'])(.*)\1$/.exec(raw)
|
|
123
|
+
return (quoted ? quoted[2] : raw.replace(/\s+#.*$/, '')).trim() || null
|
|
124
|
+
}
|
package/src/index.mjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// Kira bridge. Connects outbound to the Kira API and runs tools on this
|
|
3
3
|
// machine on the user's behalf. Env: KIRA_BRIDGE_URL, KIRA_BRIDGE_TOKEN.
|
|
4
4
|
import { createRequire } from 'node:module'
|
|
5
|
+
import { agentTools } from './agent-files.mjs'
|
|
5
6
|
import { AGENTS } from './agents/index.mjs'
|
|
6
7
|
import { claudeTools } from './agents/claude-local.mjs'
|
|
7
8
|
import { connect, disconnect, sendIfLive } from './connection.mjs'
|
|
@@ -11,6 +12,7 @@ import { host, log, setHooks } from './host.mjs'
|
|
|
11
12
|
import { jobTools } from './job-tools.mjs'
|
|
12
13
|
import { macTools } from './mac-tools.mjs'
|
|
13
14
|
import { sessionTools } from './session-tools.mjs'
|
|
15
|
+
import { skillTools } from './skill-files.mjs'
|
|
14
16
|
import { initTerminals } from './terminals.mjs'
|
|
15
17
|
|
|
16
18
|
const { version: BRIDGE_VERSION } = createRequire(import.meta.url)('../package.json')
|
|
@@ -22,6 +24,8 @@ const tools = {
|
|
|
22
24
|
...jobTools,
|
|
23
25
|
...gitTools,
|
|
24
26
|
...claudeTools,
|
|
27
|
+
...skillTools,
|
|
28
|
+
...agentTools,
|
|
25
29
|
}
|
|
26
30
|
|
|
27
31
|
/** What this bridge build implements, told to the API after its hello. */
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// Skills on this Mac: one SKILL.md each in a folder of its own, in Claude Code's skills folder and
|
|
2
|
+
// in Codex's (config-folders.mjs says which loads which). Kira lists all of them, each with the
|
|
3
|
+
// tools that load it (a skill linked into both folders is both's), and writes and removes the ones
|
|
4
|
+
// a person makes in Kira, in Claude Code's folder.
|
|
5
|
+
import { readdir, realpath, rmdir, unlink } from 'node:fs/promises'
|
|
6
|
+
import { join } from 'node:path'
|
|
7
|
+
import {
|
|
8
|
+
display,
|
|
9
|
+
exists,
|
|
10
|
+
field,
|
|
11
|
+
inside,
|
|
12
|
+
isName,
|
|
13
|
+
readSmall,
|
|
14
|
+
refuseLinks,
|
|
15
|
+
skillFolders,
|
|
16
|
+
splitFront,
|
|
17
|
+
writeWhole,
|
|
18
|
+
} from './config-folders.mjs'
|
|
19
|
+
|
|
20
|
+
/** A SKILL.md past this isn't one: skipped when listing, refused when writing. */
|
|
21
|
+
const MAX_MD = 256 * 1024
|
|
22
|
+
const WHAT = 'a skill folder'
|
|
23
|
+
/** Where Kira writes: Claude Code's folder. */
|
|
24
|
+
const writeRoot = () => skillFolders()[0].root
|
|
25
|
+
|
|
26
|
+
/** The name and description a tool shows, from a SKILL.md (the folder's name when it has none). */
|
|
27
|
+
export function skillSummary(md, slug) {
|
|
28
|
+
const { front, body } = splitFront(md)
|
|
29
|
+
const firstLine = body.split('\n').map(l => l.trim()).find(Boolean) ?? ''
|
|
30
|
+
return { name: field(front, 'name') || slug, description: field(front, 'description') || firstLine }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The skills in one folder. */
|
|
34
|
+
async function skillsIn(root) {
|
|
35
|
+
const entries = await readdir(root, { withFileTypes: true }).catch(e => {
|
|
36
|
+
if (e.code === 'ENOENT') return []
|
|
37
|
+
throw e
|
|
38
|
+
})
|
|
39
|
+
const out = []
|
|
40
|
+
for (const e of entries) {
|
|
41
|
+
if (!isName(e.name) || !(e.isDirectory() || e.isSymbolicLink())) continue
|
|
42
|
+
const dir = join(root, e.name)
|
|
43
|
+
const md = await readSmall(join(dir, 'SKILL.md'), MAX_MD)
|
|
44
|
+
if (md !== null) out.push({ slug: e.name, dir, real: await realpath(dir).catch(() => dir), md })
|
|
45
|
+
}
|
|
46
|
+
return out
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Every skill in every folder: its slug, name, description, where it is, its SKILL.md, and which
|
|
51
|
+
* tools load it (`providers`). One folder linked into another is one skill, loaded by both.
|
|
52
|
+
*/
|
|
53
|
+
export async function listSkills(folders = skillFolders()) {
|
|
54
|
+
const byReal = new Map()
|
|
55
|
+
for (const { root, loadedBy } of folders) {
|
|
56
|
+
for (const s of await skillsIn(root)) {
|
|
57
|
+
const had = byReal.get(s.real)
|
|
58
|
+
if (had) had.providers = [...new Set([...had.providers, ...loadedBy])]
|
|
59
|
+
else {
|
|
60
|
+
const skill = { slug: s.slug, ...skillSummary(s.md, s.slug), path: display(s.dir), md: s.md }
|
|
61
|
+
byReal.set(s.real, { ...skill, providers: [...loadedBy] })
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
// By name; a name in two folders keeps the folders' order (Claude Code's first).
|
|
66
|
+
const skills = [...byReal.values()].sort((a, b) => a.slug.localeCompare(b.slug))
|
|
67
|
+
return { root: display(folders[0].root), skills }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** One skill's SKILL.md, from Claude Code's folder. */
|
|
71
|
+
export async function readSkill({ slug } = {}, root = writeRoot()) {
|
|
72
|
+
const dir = inside(root, slug, WHAT)
|
|
73
|
+
const md = await readSmall(join(dir, 'SKILL.md'), MAX_MD)
|
|
74
|
+
if (md === null) return { error: `No skill in ${display(dir)}` }
|
|
75
|
+
return { slug, ...skillSummary(md, slug), path: display(dir), md }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Write a skill's SKILL.md, whole: replacing the one there, or with `create` only a new one (a skill
|
|
80
|
+
* already in that folder, Kira's or not, is never overwritten by a new one of the same name).
|
|
81
|
+
*/
|
|
82
|
+
export async function writeSkill({ slug, md, create = false } = {}, root = writeRoot()) {
|
|
83
|
+
const dir = inside(root, slug, WHAT)
|
|
84
|
+
const text = String(md ?? '')
|
|
85
|
+
if (!/^---\r?\n/.test(text)) throw new Error('A SKILL.md starts with front matter between --- lines')
|
|
86
|
+
if (Buffer.byteLength(text) > MAX_MD) throw new Error('That SKILL.md is too large')
|
|
87
|
+
await refuseLinks([root, dir], 'edit')
|
|
88
|
+
const file = join(dir, 'SKILL.md')
|
|
89
|
+
if (create && (await exists(file))) throw new Error(`A skill named ${slug} is already on your Mac`)
|
|
90
|
+
await writeWhole(file, text)
|
|
91
|
+
return { slug, ...skillSummary(text, slug), path: display(dir), providers: ['claude'] }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Remove a skill: its SKILL.md goes (so Claude Code stops loading it), and the folder too when
|
|
96
|
+
* nothing else is in it. Anything else a skill folder holds (scripts, references) stays.
|
|
97
|
+
*/
|
|
98
|
+
export async function removeSkill({ slug } = {}, root = writeRoot()) {
|
|
99
|
+
const dir = inside(root, slug, WHAT)
|
|
100
|
+
await refuseLinks([root, dir], 'remove')
|
|
101
|
+
const gone = await unlink(join(dir, 'SKILL.md')).then(
|
|
102
|
+
() => true,
|
|
103
|
+
e => {
|
|
104
|
+
if (e.code === 'ENOENT') return false
|
|
105
|
+
throw e
|
|
106
|
+
},
|
|
107
|
+
)
|
|
108
|
+
const left = await readdir(dir).catch(() => [])
|
|
109
|
+
if (!left.length) await rmdir(dir).catch(() => {})
|
|
110
|
+
return { slug, removed: gone, path: display(dir), kept: left.length ? left : undefined }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The bridge tools (index.mjs). */
|
|
114
|
+
export const skillTools = {
|
|
115
|
+
list_skills: () => listSkills(),
|
|
116
|
+
read_skill: args => readSkill(args),
|
|
117
|
+
write_skill: args => writeSkill(args),
|
|
118
|
+
remove_skill: args => removeSkill(args),
|
|
119
|
+
}
|