2025-12-11 11:07:31 +09:00
|
|
|
import type { PluginInput } from "@opencode-ai/plugin";
|
|
|
|
|
import { readFileSync } from "node:fs";
|
|
|
|
|
import { homedir } from "node:os";
|
|
|
|
|
import { relative, resolve } from "node:path";
|
|
|
|
|
import { findProjectRoot, findRuleFiles } from "./finder";
|
|
|
|
|
import {
|
|
|
|
|
createContentHash,
|
|
|
|
|
isDuplicateByContentHash,
|
|
|
|
|
isDuplicateByRealPath,
|
|
|
|
|
shouldApplyRule,
|
|
|
|
|
} from "./matcher";
|
|
|
|
|
import { parseRuleFrontmatter } from "./parser";
|
|
|
|
|
import {
|
|
|
|
|
clearInjectedRules,
|
|
|
|
|
loadInjectedRules,
|
|
|
|
|
saveInjectedRules,
|
|
|
|
|
} from "./storage";
|
2025-12-26 15:38:28 +09:00
|
|
|
import { createDynamicTruncator } from "../../shared/dynamic-truncator";
|
2026-02-01 16:47:50 +09:00
|
|
|
import { getRuleInjectionFilePath } from "./output-path";
|
2025-12-11 11:07:31 +09:00
|
|
|
|
|
|
|
|
interface ToolExecuteInput {
|
|
|
|
|
tool: string;
|
|
|
|
|
sessionID: string;
|
|
|
|
|
callID: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ToolExecuteOutput {
|
|
|
|
|
title: string;
|
|
|
|
|
output: string;
|
|
|
|
|
metadata: unknown;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-22 01:53:15 +09:00
|
|
|
interface ToolExecuteBeforeOutput {
|
|
|
|
|
args: unknown;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-11 11:07:31 +09:00
|
|
|
interface EventInput {
|
|
|
|
|
event: {
|
|
|
|
|
type: string;
|
|
|
|
|
properties?: unknown;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface RuleToInject {
|
|
|
|
|
relativePath: string;
|
|
|
|
|
matchReason: string;
|
|
|
|
|
content: string;
|
|
|
|
|
distance: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const TRACKED_TOOLS = ["read", "write", "edit", "multiedit"];
|
|
|
|
|
|
|
|
|
|
export function createRulesInjectorHook(ctx: PluginInput) {
|
|
|
|
|
const sessionCaches = new Map<
|
|
|
|
|
string,
|
|
|
|
|
{ contentHashes: Set<string>; realPaths: Set<string> }
|
|
|
|
|
>();
|
2025-12-26 15:38:28 +09:00
|
|
|
const truncator = createDynamicTruncator(ctx);
|
2025-12-11 11:07:31 +09:00
|
|
|
|
|
|
|
|
function getSessionCache(sessionID: string): {
|
|
|
|
|
contentHashes: Set<string>;
|
|
|
|
|
realPaths: Set<string>;
|
|
|
|
|
} {
|
|
|
|
|
if (!sessionCaches.has(sessionID)) {
|
|
|
|
|
sessionCaches.set(sessionID, loadInjectedRules(sessionID));
|
|
|
|
|
}
|
|
|
|
|
return sessionCaches.get(sessionID)!;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-22 01:53:15 +09:00
|
|
|
function resolveFilePath(path: string): string | null {
|
|
|
|
|
if (!path) return null;
|
|
|
|
|
if (path.startsWith("/")) return path;
|
|
|
|
|
return resolve(ctx.directory, path);
|
2025-12-11 11:07:31 +09:00
|
|
|
}
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
|
2025-12-26 15:38:28 +09:00
|
|
|
async function processFilePathForInjection(
|
2025-12-22 01:53:15 +09:00
|
|
|
filePath: string,
|
|
|
|
|
sessionID: string,
|
2025-12-11 11:07:31 +09:00
|
|
|
output: ToolExecuteOutput
|
2025-12-26 15:38:28 +09:00
|
|
|
): Promise<void> {
|
2025-12-22 01:53:15 +09:00
|
|
|
const resolved = resolveFilePath(filePath);
|
|
|
|
|
if (!resolved) return;
|
2025-12-11 11:07:31 +09:00
|
|
|
|
2025-12-22 01:53:15 +09:00
|
|
|
const projectRoot = findProjectRoot(resolved);
|
|
|
|
|
const cache = getSessionCache(sessionID);
|
2025-12-11 11:07:31 +09:00
|
|
|
const home = homedir();
|
|
|
|
|
|
2025-12-22 01:53:15 +09:00
|
|
|
const ruleFileCandidates = findRuleFiles(projectRoot, home, resolved);
|
2025-12-11 11:07:31 +09:00
|
|
|
const toInject: RuleToInject[] = [];
|
|
|
|
|
|
|
|
|
|
for (const candidate of ruleFileCandidates) {
|
|
|
|
|
if (isDuplicateByRealPath(candidate.realPath, cache.realPaths)) continue;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const rawContent = readFileSync(candidate.path, "utf-8");
|
|
|
|
|
const { metadata, body } = parseRuleFrontmatter(rawContent);
|
|
|
|
|
|
2026-01-02 10:27:33 +09:00
|
|
|
let matchReason: string;
|
|
|
|
|
if (candidate.isSingleFile) {
|
|
|
|
|
matchReason = "copilot-instructions (always apply)";
|
|
|
|
|
} else {
|
|
|
|
|
const matchResult = shouldApplyRule(metadata, resolved, projectRoot);
|
|
|
|
|
if (!matchResult.applies) continue;
|
|
|
|
|
matchReason = matchResult.reason ?? "matched";
|
|
|
|
|
}
|
2025-12-11 11:07:31 +09:00
|
|
|
|
|
|
|
|
const contentHash = createContentHash(body);
|
|
|
|
|
if (isDuplicateByContentHash(contentHash, cache.contentHashes)) continue;
|
|
|
|
|
|
|
|
|
|
const relativePath = projectRoot
|
|
|
|
|
? relative(projectRoot, candidate.path)
|
|
|
|
|
: candidate.path;
|
|
|
|
|
|
|
|
|
|
toInject.push({
|
|
|
|
|
relativePath,
|
2026-01-02 10:27:33 +09:00
|
|
|
matchReason,
|
2025-12-11 11:07:31 +09:00
|
|
|
content: body,
|
|
|
|
|
distance: candidate.distance,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cache.realPaths.add(candidate.realPath);
|
|
|
|
|
cache.contentHashes.add(contentHash);
|
|
|
|
|
} catch {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (toInject.length === 0) return;
|
|
|
|
|
|
|
|
|
|
toInject.sort((a, b) => a.distance - b.distance);
|
|
|
|
|
|
|
|
|
|
for (const rule of toInject) {
|
2025-12-26 15:38:28 +09:00
|
|
|
const { result, truncated } = await truncator.truncate(sessionID, rule.content);
|
|
|
|
|
const truncationNotice = truncated
|
|
|
|
|
? `\n\n[Note: Content was truncated to save context window space. For full context, please read the file directly: ${rule.relativePath}]`
|
|
|
|
|
: "";
|
|
|
|
|
output.output += `\n\n[Rule: ${rule.relativePath}]\n[Match: ${rule.matchReason}]\n${result}${truncationNotice}`;
|
2025-12-11 11:07:31 +09:00
|
|
|
}
|
|
|
|
|
|
2025-12-22 01:53:15 +09:00
|
|
|
saveInjectedRules(sessionID, cache);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const toolExecuteAfter = async (
|
|
|
|
|
input: ToolExecuteInput,
|
|
|
|
|
output: ToolExecuteOutput
|
|
|
|
|
) => {
|
|
|
|
|
const toolName = input.tool.toLowerCase();
|
|
|
|
|
|
|
|
|
|
if (TRACKED_TOOLS.includes(toolName)) {
|
2026-02-01 16:47:50 +09:00
|
|
|
const filePath = getRuleInjectionFilePath(output);
|
|
|
|
|
if (!filePath) return;
|
|
|
|
|
await processFilePathForInjection(filePath, input.sessionID, output);
|
2025-12-22 01:53:15 +09:00
|
|
|
return;
|
|
|
|
|
}
|
2026-02-01 15:01:09 +09:00
|
|
|
};
|
2025-12-22 01:53:15 +09:00
|
|
|
|
2026-02-01 15:01:09 +09:00
|
|
|
const toolExecuteBefore = async (
|
|
|
|
|
input: ToolExecuteInput,
|
|
|
|
|
output: ToolExecuteBeforeOutput
|
|
|
|
|
): Promise<void> => {
|
|
|
|
|
void input;
|
|
|
|
|
void output;
|
2025-12-11 11:07:31 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const eventHandler = async ({ event }: EventInput) => {
|
|
|
|
|
const props = event.properties as Record<string, unknown> | undefined;
|
|
|
|
|
|
|
|
|
|
if (event.type === "session.deleted") {
|
|
|
|
|
const sessionInfo = props?.info as { id?: string } | undefined;
|
|
|
|
|
if (sessionInfo?.id) {
|
|
|
|
|
sessionCaches.delete(sessionInfo.id);
|
|
|
|
|
clearInjectedRules(sessionInfo.id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === "session.compacted") {
|
|
|
|
|
const sessionID = (props?.sessionID ??
|
|
|
|
|
(props?.info as { id?: string } | undefined)?.id) as string | undefined;
|
|
|
|
|
if (sessionID) {
|
|
|
|
|
sessionCaches.delete(sessionID);
|
|
|
|
|
clearInjectedRules(sessionID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
2025-12-22 01:53:15 +09:00
|
|
|
"tool.execute.before": toolExecuteBefore,
|
2025-12-11 11:07:31 +09:00
|
|
|
"tool.execute.after": toolExecuteAfter,
|
|
|
|
|
event: eventHandler,
|
|
|
|
|
};
|
|
|
|
|
}
|