forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext-pack.ts
More file actions
36 lines (33 loc) · 1.52 KB
/
context-pack.ts
File metadata and controls
36 lines (33 loc) · 1.52 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
import type { Plugin } from "@opencode-ai/plugin"
import { stablePrefixBlock } from "../../packages/prompt-kit/src/index"
const SENSITIVE = /\.(env|aws|ssh|git)(\.|\/|$)/i
const FRAME_RE = /<CONTEXT_FRAME[^>]*>[\s\S]*?<\/CONTEXT_FRAME>/g
export function onPreSendPrompt(input: unknown) {
const text = typeof input === "string" ? input : String(input ?? "")
const p = stablePrefixBlock()
const has = text.includes("## Stable Project Rules") && text.includes("## Coding Standards")
const base = has ? text : `${p.text}\n\n${text}`
const frames = base.match(FRAME_RE) ?? []
const last = frames.length ? frames[frames.length - 1] : ""
const stripped = base.replace(FRAME_RE, "").trim()
const joined = last ? `${last}\n\n${stripped}` : stripped
const frame = FRAME_RE.test(joined)
? joined
: `<!-- WARNING: no CONTEXT_FRAME detected; call repo.workingSet.select and include context_block -->\n${joined}`
const hint = /\[FULL\]/.test(frame) && !/tests?\//i.test(frame)
? `<!-- Hint: prefer readSpan for non-test files -->\n${frame}`
: frame
const warn = SENSITIVE.test(hint)
? `<!-- WARNING: sensitive filename detected; remove from prompt -->\n${hint}`
: hint
return `<!-- prefix_sha256=${p.sha256} -->\n${warn}`
}
export const contextPack: Plugin = async () => ({
async "chat.message"(_input, output) {
output.parts = output.parts.map((part) => {
if (part.type !== "text") return part
if (!part.text) return part
return { ...part, text: onPreSendPrompt(part.text) }
})
},
})