dsh-git-bugfix 0.0.0-stage → 0.1.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/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.en.md +152 -0
- package/README.md +162 -2
- package/client.js +2681 -0
- package/cordis.patch.yml +6 -0
- package/docs/DETAIL.md +1 -0
- package/docs/DETAIL.zh.md +1 -0
- package/docs/preview.svg +62 -0
- package/icon.svg +28 -0
- package/index.js +278 -0
- package/lib/fixer.js +2184 -0
- package/lib/git.js +728 -0
- package/lib/skills.js +173 -0
- package/lib/state.js +681 -0
- package/lib/xlsx.js +526 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +69 -4
package/lib/skills.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local repository skill discovery for the defect fixer.
|
|
3
|
+
*
|
|
4
|
+
* Repositories may carry a bug-fix workflow skill under
|
|
5
|
+
* `.agents/skills/<name>/SKILL.md` (the same roots the Harness skill provider
|
|
6
|
+
* scans). The fixer lists what the repository offers, preselects the entry that
|
|
7
|
+
* looks like a bug-fix workflow, and injects the chosen skill body into each
|
|
8
|
+
* fix prompt so every fix agent follows the repository's own process.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { readdir, readFile, stat } from 'node:fs/promises'
|
|
12
|
+
import path from 'node:path'
|
|
13
|
+
|
|
14
|
+
/** Root-relative directories scanned for local skills, in priority order. */
|
|
15
|
+
const SKILL_ROOTS = [
|
|
16
|
+
{ relative: path.join('.agents', 'skills'), source: 'project-agents' },
|
|
17
|
+
{ relative: path.join('.dsh', 'skills'), source: 'project-dsh' },
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
/** Words that identify a bug-fix workflow skill, strongest first. */
|
|
21
|
+
const BUGFIX_HINTS = [
|
|
22
|
+
/bugfix/i,
|
|
23
|
+
/bug[-_ ]?fix/i,
|
|
24
|
+
/bug修复/i,
|
|
25
|
+
/缺陷修复/,
|
|
26
|
+
/defect/i,
|
|
27
|
+
/bug/i,
|
|
28
|
+
/修复/,
|
|
29
|
+
/缺陷/,
|
|
30
|
+
/fix/i,
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Parse the YAML frontmatter keys this plugin cares about.
|
|
35
|
+
* @param {string} text - skill file text.
|
|
36
|
+
* @returns {{ name: string, description: string, whenToUse: string, body: string }} parsed fields.
|
|
37
|
+
*/
|
|
38
|
+
export function parseSkillDocument(text) {
|
|
39
|
+
const normalized = text.replace(/^\uFEFF/, '')
|
|
40
|
+
const match = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/.exec(normalized)
|
|
41
|
+
const fields = { name: '', description: '', whenToUse: '' }
|
|
42
|
+
let body = normalized
|
|
43
|
+
if (match !== null) {
|
|
44
|
+
body = normalized.slice(match[0].length)
|
|
45
|
+
for (const line of match[1].split(/\r?\n/)) {
|
|
46
|
+
const pair = /^([A-Za-z][\w-]*)\s*:\s*(.*)$/.exec(line)
|
|
47
|
+
if (pair === null) continue
|
|
48
|
+
const key = pair[1]
|
|
49
|
+
let value = pair[2].trim()
|
|
50
|
+
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
|
51
|
+
value = value.slice(1, -1)
|
|
52
|
+
}
|
|
53
|
+
if (key === 'name') fields.name = value
|
|
54
|
+
else if (key === 'description') fields.description = value
|
|
55
|
+
else if (key === 'whenToUse' || key === 'when_to_use') fields.whenToUse = value
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return { ...fields, body: body.trim() }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Score how likely a skill is the repository's bug-fix workflow.
|
|
63
|
+
* @param {{ name: string, description: string, whenToUse: string }} skill - skill metadata.
|
|
64
|
+
* @returns {number} score; 0 means no signal.
|
|
65
|
+
*/
|
|
66
|
+
function bugfixScore(skill) {
|
|
67
|
+
const haystack = `${skill.name}\n${skill.description}\n${skill.whenToUse}`
|
|
68
|
+
let score = 0
|
|
69
|
+
for (let index = 0; index < BUGFIX_HINTS.length; index += 1) {
|
|
70
|
+
if (BUGFIX_HINTS[index].test(haystack)) {
|
|
71
|
+
score += BUGFIX_HINTS.length - index
|
|
72
|
+
break
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (/workflow/i.test(haystack)) score += 1
|
|
76
|
+
return score
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Read one skill candidate from a directory bundle or a flat file.
|
|
81
|
+
* @param {string} file - absolute `SKILL.md` or flat `.md` path.
|
|
82
|
+
* @param {string} source - discovery root label.
|
|
83
|
+
* @returns {Promise<null | { name: string, description: string, whenToUse: string, path: string, source: string, score: number }>}
|
|
84
|
+
* the skill metadata, or null when unreadable or unnamed.
|
|
85
|
+
*/
|
|
86
|
+
async function readSkill(file, source) {
|
|
87
|
+
try {
|
|
88
|
+
const info = await stat(file)
|
|
89
|
+
if (!info.isFile() || info.size > 512 * 1024) return null
|
|
90
|
+
const text = await readFile(file, 'utf8')
|
|
91
|
+
const parsed = parseSkillDocument(text)
|
|
92
|
+
const fallbackName = path.basename(path.dirname(file)) === path.basename(file)
|
|
93
|
+
? ''
|
|
94
|
+
: path.basename(path.dirname(file))
|
|
95
|
+
const name = parsed.name !== '' ? parsed.name : (path.extname(file).toLowerCase() === '.md' ? path.basename(file, path.extname(file)) : fallbackName)
|
|
96
|
+
if (name === '') return null
|
|
97
|
+
const skill = {
|
|
98
|
+
name,
|
|
99
|
+
description: parsed.description,
|
|
100
|
+
whenToUse: parsed.whenToUse,
|
|
101
|
+
path: file,
|
|
102
|
+
source,
|
|
103
|
+
score: 0,
|
|
104
|
+
}
|
|
105
|
+
skill.score = bugfixScore(skill)
|
|
106
|
+
return skill
|
|
107
|
+
} catch {
|
|
108
|
+
return null
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* List the local skills a repository offers.
|
|
114
|
+
* @param {string} root - repository root.
|
|
115
|
+
* @returns {Promise<Array<{ name: string, description: string, whenToUse: string, path: string, source: string, score: number }>>}
|
|
116
|
+
* discovered skills ordered by fix relevance then name.
|
|
117
|
+
*/
|
|
118
|
+
export async function scanRepoSkills(root) {
|
|
119
|
+
const found = []
|
|
120
|
+
for (const rootDefinition of SKILL_ROOTS) {
|
|
121
|
+
const directory = path.join(root, rootDefinition.relative)
|
|
122
|
+
let entries
|
|
123
|
+
try {
|
|
124
|
+
entries = await readdir(directory, { withFileTypes: true })
|
|
125
|
+
} catch {
|
|
126
|
+
continue
|
|
127
|
+
}
|
|
128
|
+
for (const entry of entries.sort((left, right) => left.name.localeCompare(right.name, 'en'))) {
|
|
129
|
+
if (entry.isDirectory()) {
|
|
130
|
+
found.push(await readSkill(path.join(directory, entry.name, 'SKILL.md'), rootDefinition.source))
|
|
131
|
+
} else if (entry.isFile() && entry.name.toLowerCase().endsWith('.md')) {
|
|
132
|
+
found.push(await readSkill(path.join(directory, entry.name), rootDefinition.source))
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const seen = new Set()
|
|
137
|
+
return found
|
|
138
|
+
.filter(skill => skill !== null && skill !== undefined)
|
|
139
|
+
.filter(skill => {
|
|
140
|
+
if (seen.has(skill.name)) return false
|
|
141
|
+
seen.add(skill.name)
|
|
142
|
+
return true
|
|
143
|
+
})
|
|
144
|
+
.sort((left, right) => right.score - left.score || left.name.localeCompare(right.name, 'en'))
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Read a selected skill's instruction body, bounded for prompt injection.
|
|
149
|
+
* @param {string} root - repository root.
|
|
150
|
+
* @param {string} file - absolute skill file path recorded by `scanRepoSkills`.
|
|
151
|
+
* @param {number} [maxBytes] - byte cap for the injected body.
|
|
152
|
+
* @returns {Promise<null | { name: string, description: string, path: string, body: string, truncated: boolean }>}
|
|
153
|
+
* the skill content, or null when the path is outside the repository or unreadable.
|
|
154
|
+
*/
|
|
155
|
+
export async function readSkillBody(root, file, maxBytes = 48 * 1024) {
|
|
156
|
+
const relative = path.relative(path.resolve(root), path.resolve(file))
|
|
157
|
+
if (relative.startsWith('..') || path.isAbsolute(relative)) return null
|
|
158
|
+
try {
|
|
159
|
+
const text = await readFile(file, 'utf8')
|
|
160
|
+
const parsed = parseSkillDocument(text)
|
|
161
|
+
const bytes = Buffer.byteLength(parsed.body, 'utf8')
|
|
162
|
+
const body = bytes > maxBytes ? `${parsed.body.slice(0, maxBytes)}\n\n(skill 内容过长,已截断)` : parsed.body
|
|
163
|
+
return {
|
|
164
|
+
name: parsed.name,
|
|
165
|
+
description: parsed.description,
|
|
166
|
+
path: file,
|
|
167
|
+
body,
|
|
168
|
+
truncated: bytes > maxBytes,
|
|
169
|
+
}
|
|
170
|
+
} catch {
|
|
171
|
+
return null
|
|
172
|
+
}
|
|
173
|
+
}
|