@dlacaille/opencode-copilot-instructions 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/LICENSE +22 -0
- package/README.md +58 -0
- package/index.ts +1 -0
- package/package.json +44 -0
- package/src/frontmatter.ts +38 -0
- package/src/index.ts +95 -0
- package/src/loader.ts +57 -0
- package/src/matcher.ts +18 -0
- package/src/session-state.ts +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 ekroon (https://github.com/ekroon)
|
|
4
|
+
Copyright (c) 2026 Dominic Lacaille
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# @dlacaille/opencode-copilot-instructions
|
|
2
|
+
|
|
3
|
+
An [OpenCode](https://opencode.ai) V2 plugin that loads GitHub Copilot custom instruction files into OpenCode's context.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add the plugin to your `opencode.json`:
|
|
8
|
+
|
|
9
|
+
```json
|
|
10
|
+
{
|
|
11
|
+
"$schema": "https://opencode.ai/config.json",
|
|
12
|
+
"plugins": ["@dlacaille/opencode-copilot-instructions"]
|
|
13
|
+
}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
OpenCode installs the package from npm on startup.
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
### Repository-wide instructions
|
|
21
|
+
|
|
22
|
+
Create `.github/copilot-instructions.md`. Its content is added to every model request, including compaction.
|
|
23
|
+
|
|
24
|
+
### Path-specific instructions
|
|
25
|
+
|
|
26
|
+
Create `.github/instructions/*.instructions.md` files with an `applyTo` field:
|
|
27
|
+
|
|
28
|
+
```markdown
|
|
29
|
+
---
|
|
30
|
+
applyTo: "**/*.ts,**/*.tsx"
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
Always use explicit return types.
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
When the `read`, `edit`, or `write` tools touch a matching file, the instructions are appended to the tool result. Each file is added once per session, and again after the session is compacted.
|
|
37
|
+
|
|
38
|
+
`applyTo` accepts a comma-separated string or a YAML list of [picomatch](https://github.com/micromatch/picomatch) globs, relative to the project directory.
|
|
39
|
+
|
|
40
|
+
Instructions are loaded when the plugin starts. Restart OpenCode after changing them.
|
|
41
|
+
|
|
42
|
+
## Development
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
npm install --legacy-peer-deps
|
|
46
|
+
npm test
|
|
47
|
+
npm run typecheck
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
To try a local checkout, point `plugins` at its path, for example `"plugins": ["~/projects/opencode-copilot-instructions"]`.
|
|
51
|
+
|
|
52
|
+
## Credits
|
|
53
|
+
|
|
54
|
+
Based on [ekroon/opencode-copilot-instructions](https://github.com/ekroon/opencode-copilot-instructions) by [@ekroon](https://github.com/ekroon), ported to the OpenCode V2 plugin API.
|
|
55
|
+
|
|
56
|
+
## License
|
|
57
|
+
|
|
58
|
+
MIT
|
package/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "./src/index"
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dlacaille/opencode-copilot-instructions",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "OpenCode plugin that loads GitHub Copilot custom instruction files",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.ts"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"index.ts",
|
|
12
|
+
"src",
|
|
13
|
+
"!src/**/*.test.ts",
|
|
14
|
+
"README.md",
|
|
15
|
+
"LICENSE"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [
|
|
18
|
+
"opencode",
|
|
19
|
+
"opencode-plugin",
|
|
20
|
+
"copilot",
|
|
21
|
+
"instructions"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"test": "vitest run",
|
|
25
|
+
"typecheck": "tsc --noEmit"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"front-matter": "^4.0.2",
|
|
29
|
+
"picomatch": "^4.0.3"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@opencode/plugin": ">=2.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@opencode/plugin": "^2.0.15",
|
|
36
|
+
"@types/node": "^22.0.0",
|
|
37
|
+
"@types/picomatch": "^4.0.0",
|
|
38
|
+
"typescript": "^5.9.0",
|
|
39
|
+
"vitest": "^4.0.0"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import * as frontMatterModule from "front-matter"
|
|
2
|
+
|
|
3
|
+
const fm = (frontMatterModule as any).default ?? frontMatterModule
|
|
4
|
+
|
|
5
|
+
export interface Frontmatter {
|
|
6
|
+
applyTo?: string | string[]
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface ParsedFrontmatter {
|
|
10
|
+
frontmatter: Frontmatter
|
|
11
|
+
body: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function parseFrontmatter(content: string): ParsedFrontmatter {
|
|
15
|
+
const normalized = content.replace(/\r\n/g, "\n")
|
|
16
|
+
|
|
17
|
+
if (!fm.test(normalized)) {
|
|
18
|
+
return { frontmatter: {}, body: content }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
const parsed = fm(normalized)
|
|
23
|
+
const attrs = parsed.attributes as Record<string, unknown>
|
|
24
|
+
const result: Frontmatter = {}
|
|
25
|
+
|
|
26
|
+
if (attrs.applyTo !== undefined) {
|
|
27
|
+
if (typeof attrs.applyTo === "string") {
|
|
28
|
+
result.applyTo = attrs.applyTo
|
|
29
|
+
} else if (Array.isArray(attrs.applyTo)) {
|
|
30
|
+
result.applyTo = attrs.applyTo.filter((item): item is string => typeof item === "string")
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return { frontmatter: result, body: parsed.body }
|
|
35
|
+
} catch {
|
|
36
|
+
return { frontmatter: {}, body: content }
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import * as path from "node:path"
|
|
2
|
+
import { Plugin } from "@opencode/plugin"
|
|
3
|
+
import { loadRepoInstructions, loadPathInstructions } from "./loader"
|
|
4
|
+
import { SessionState } from "./session-state"
|
|
5
|
+
|
|
6
|
+
const FILE_TOOLS = new Set(["read", "edit", "write"])
|
|
7
|
+
|
|
8
|
+
function getRelativePath(directory: string, filePath: string): string {
|
|
9
|
+
const normalizedDir = directory.endsWith("/") ? directory.slice(0, -1) : directory
|
|
10
|
+
if (!path.isAbsolute(filePath)) return filePath
|
|
11
|
+
return path.relative(normalizedDir, filePath)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default Plugin.define({
|
|
15
|
+
id: "copilot-instructions",
|
|
16
|
+
async setup(ctx) {
|
|
17
|
+
const directory = ctx.location.directory
|
|
18
|
+
const repoInstructions = loadRepoInstructions(directory)
|
|
19
|
+
const pathInstructions = loadPathInstructions(directory)
|
|
20
|
+
|
|
21
|
+
if (repoInstructions) {
|
|
22
|
+
console.log("[copilot-instructions] Loaded repo instructions from .github/copilot-instructions.md")
|
|
23
|
+
}
|
|
24
|
+
for (const instruction of pathInstructions) {
|
|
25
|
+
console.log(`[copilot-instructions] Loaded path instructions from ${path.basename(instruction.file)}`)
|
|
26
|
+
}
|
|
27
|
+
if (!repoInstructions && pathInstructions.length === 0) {
|
|
28
|
+
console.log("[copilot-instructions] No Copilot instructions found")
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const state = new SessionState()
|
|
32
|
+
|
|
33
|
+
const injectRepoInstructions = (event: { system: Array<{ type: string; text: string }> }) => {
|
|
34
|
+
if (!repoInstructions) return
|
|
35
|
+
event.system.push({
|
|
36
|
+
type: "text",
|
|
37
|
+
text: `<copilot-instruction:copilot-instructions.md>\n${repoInstructions.trimEnd()}\n</copilot-instruction:copilot-instructions.md>`,
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Inject repo-wide instructions into every model call so they survive compaction.
|
|
42
|
+
await ctx.session.hook("context", injectRepoInstructions)
|
|
43
|
+
await ctx.session.hook("compaction", injectRepoInstructions)
|
|
44
|
+
|
|
45
|
+
await ctx.tool.hook("execute.before", (event) => {
|
|
46
|
+
if (!FILE_TOOLS.has(event.tool)) return
|
|
47
|
+
|
|
48
|
+
const input = event.input as { path?: unknown }
|
|
49
|
+
const filePath = input?.path
|
|
50
|
+
if (!filePath || typeof filePath !== "string") return
|
|
51
|
+
|
|
52
|
+
const relativePath = getRelativePath(directory, filePath)
|
|
53
|
+
const matching = pathInstructions.filter((instruction) => {
|
|
54
|
+
if (state.isFileInjected(event.sessionID, instruction.file)) return false
|
|
55
|
+
return instruction.matcher(relativePath)
|
|
56
|
+
})
|
|
57
|
+
if (matching.length === 0) return
|
|
58
|
+
|
|
59
|
+
for (const instruction of matching) state.markFileInjected(event.sessionID, instruction.file)
|
|
60
|
+
|
|
61
|
+
const text = matching
|
|
62
|
+
.map((instruction) => {
|
|
63
|
+
const filename = path.basename(instruction.file)
|
|
64
|
+
const patterns = instruction.applyTo.join(", ")
|
|
65
|
+
return `<copilot-instruction:${filename}>\n## Path-Specific Instructions (applies to: ${patterns})\n\n${instruction.content.trimEnd()}\n</copilot-instruction:${filename}>`
|
|
66
|
+
})
|
|
67
|
+
.join("\n\n")
|
|
68
|
+
|
|
69
|
+
state.setPending(event.id, text)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
await ctx.tool.hook("execute.after", (event) => {
|
|
73
|
+
const text = state.consumePending(event.id)
|
|
74
|
+
if (!text) return
|
|
75
|
+
if (event.status !== "completed") return
|
|
76
|
+
|
|
77
|
+
const existing = typeof event.result.content === "string" ? event.result.content : ""
|
|
78
|
+
event.result = {
|
|
79
|
+
...event.result,
|
|
80
|
+
content: existing ? `${existing}\n\n${text}` : text,
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
const controller = new AbortController()
|
|
85
|
+
void (async () => {
|
|
86
|
+
for await (const event of ctx.event.subscribe({ signal: controller.signal })) {
|
|
87
|
+
if (event.type === "session.compaction.ended") {
|
|
88
|
+
state.clearSession(event.data.sessionID)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
})()
|
|
92
|
+
|
|
93
|
+
return () => controller.abort()
|
|
94
|
+
},
|
|
95
|
+
})
|
package/src/loader.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import * as fs from "node:fs"
|
|
2
|
+
import * as path from "node:path"
|
|
3
|
+
import { parseFrontmatter } from "./frontmatter"
|
|
4
|
+
import { createMatcher, normalizePatterns, type Matcher } from "./matcher"
|
|
5
|
+
|
|
6
|
+
export interface PathInstruction {
|
|
7
|
+
file: string
|
|
8
|
+
applyTo: string[]
|
|
9
|
+
content: string
|
|
10
|
+
matcher: Matcher
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Load repo-wide instructions from .github/copilot-instructions.md */
|
|
14
|
+
export function loadRepoInstructions(directory: string): string | null {
|
|
15
|
+
const filePath = path.join(directory, ".github", "copilot-instructions.md")
|
|
16
|
+
try {
|
|
17
|
+
return fs.readFileSync(filePath, "utf-8")
|
|
18
|
+
} catch {
|
|
19
|
+
return null
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Load path-specific instructions from .github/instructions/*.instructions.md */
|
|
24
|
+
export function loadPathInstructions(directory: string): PathInstruction[] {
|
|
25
|
+
const instructionsDir = path.join(directory, ".github", "instructions")
|
|
26
|
+
let files: string[]
|
|
27
|
+
try {
|
|
28
|
+
files = fs.readdirSync(instructionsDir)
|
|
29
|
+
} catch {
|
|
30
|
+
return []
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const result: PathInstruction[] = []
|
|
34
|
+
for (const filename of files) {
|
|
35
|
+
if (!filename.endsWith(".instructions.md")) continue
|
|
36
|
+
|
|
37
|
+
const filePath = path.join(instructionsDir, filename)
|
|
38
|
+
let content: string
|
|
39
|
+
try {
|
|
40
|
+
content = fs.readFileSync(filePath, "utf-8")
|
|
41
|
+
} catch {
|
|
42
|
+
continue
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const parsed = parseFrontmatter(content)
|
|
46
|
+
const patterns = normalizePatterns(parsed.frontmatter.applyTo)
|
|
47
|
+
if (patterns.length === 0) continue
|
|
48
|
+
|
|
49
|
+
result.push({
|
|
50
|
+
file: filePath,
|
|
51
|
+
applyTo: patterns,
|
|
52
|
+
content: parsed.body,
|
|
53
|
+
matcher: createMatcher(patterns),
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
return result
|
|
57
|
+
}
|
package/src/matcher.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import picomatch from "picomatch"
|
|
2
|
+
|
|
3
|
+
export type Matcher = (path: string) => boolean
|
|
4
|
+
|
|
5
|
+
export function createMatcher(patterns: string[]): Matcher {
|
|
6
|
+
if (patterns.length === 0) return () => false
|
|
7
|
+
const isMatch = picomatch(patterns)
|
|
8
|
+
return (path) => isMatch(path)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function normalizePatterns(applyTo: string | string[] | undefined): string[] {
|
|
12
|
+
if (applyTo === undefined) return []
|
|
13
|
+
if (Array.isArray(applyTo)) return applyTo
|
|
14
|
+
return applyTo
|
|
15
|
+
.split(",")
|
|
16
|
+
.map((pattern) => pattern.trim())
|
|
17
|
+
.filter((pattern) => pattern.length > 0)
|
|
18
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encapsulates session-related state for the Copilot Instructions plugin.
|
|
3
|
+
*
|
|
4
|
+
* Tracks which path-specific instructions have been injected per session, and
|
|
5
|
+
* pending instructions keyed by tool call ID.
|
|
6
|
+
*/
|
|
7
|
+
export class SessionState {
|
|
8
|
+
private injectedPerSession = new Map<string, Set<string>>()
|
|
9
|
+
private pendingInstructions = new Map<string, string>()
|
|
10
|
+
|
|
11
|
+
isFileInjected(sessionId: string, file: string): boolean {
|
|
12
|
+
return this.injectedPerSession.get(sessionId)?.has(file) ?? false
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
markFileInjected(sessionId: string, file: string): void {
|
|
16
|
+
let files = this.injectedPerSession.get(sessionId)
|
|
17
|
+
if (!files) {
|
|
18
|
+
files = new Set()
|
|
19
|
+
this.injectedPerSession.set(sessionId, files)
|
|
20
|
+
}
|
|
21
|
+
files.add(file)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
clearSession(sessionId: string): void {
|
|
25
|
+
this.injectedPerSession.delete(sessionId)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
setPending(callId: string, text: string): void {
|
|
29
|
+
this.pendingInstructions.set(callId, text)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
consumePending(callId: string): string | undefined {
|
|
33
|
+
const text = this.pendingInstructions.get(callId)
|
|
34
|
+
this.pendingInstructions.delete(callId)
|
|
35
|
+
return text
|
|
36
|
+
}
|
|
37
|
+
}
|