|
| 1 | +import { existsSync } from "node:fs"; |
| 2 | +import { readFile } from "node:fs/promises"; |
| 3 | +import { isAbsolute, relative, resolve } from "node:path"; |
| 4 | +import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; |
| 5 | +import { Type } from "typebox"; |
| 6 | +import { type Repo } from "@arvoretech/hub-core"; |
| 7 | +import { getSessionState } from "./session-state.js"; |
| 8 | + |
| 9 | +function isPathInside(parent: string, child: string): boolean { |
| 10 | + const rel = relative(parent, child); |
| 11 | + return rel !== "" && !rel.startsWith("..") && !isAbsolute(rel); |
| 12 | +} |
| 13 | + |
| 14 | +async function readRepoContext( |
| 15 | + repo: Repo, |
| 16 | + hubDir: string, |
| 17 | +): Promise<{ sections: string[]; missing: string[] }> { |
| 18 | + const sections: string[] = []; |
| 19 | + const missing: string[] = []; |
| 20 | + |
| 21 | + for (const file of repo.context_files ?? []) { |
| 22 | + const filePath = resolve(hubDir, file); |
| 23 | + |
| 24 | + if (!isPathInside(hubDir, filePath)) { |
| 25 | + missing.push(`${file} (outside hub workspace, skipped)`); |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + if (!existsSync(filePath)) { |
| 30 | + missing.push(file); |
| 31 | + continue; |
| 32 | + } |
| 33 | + |
| 34 | + try { |
| 35 | + const content = (await readFile(filePath, "utf-8")).trim(); |
| 36 | + if (content) { |
| 37 | + sections.push(`## Source: ${file}\n\n${content}`); |
| 38 | + } |
| 39 | + } catch { |
| 40 | + missing.push(file); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return { sections, missing }; |
| 45 | +} |
| 46 | + |
| 47 | +export function repoContext(pi: ExtensionAPI) { |
| 48 | + let steeringEnabled = false; |
| 49 | + |
| 50 | + pi.on("session_start", async (_event, ctx) => { |
| 51 | + steeringEnabled = false; |
| 52 | + |
| 53 | + const { config, pi: toggles } = getSessionState(); |
| 54 | + if (!config || !toggles?.repoContext) return; |
| 55 | + |
| 56 | + const reposWithContext = config.repos.filter((r) => r.context_files?.length); |
| 57 | + if (reposWithContext.length === 0) return; |
| 58 | + |
| 59 | + steeringEnabled = true; |
| 60 | + const repoNames = reposWithContext.map((r) => r.name); |
| 61 | + |
| 62 | + pi.registerTool({ |
| 63 | + name: "hub_repo_context", |
| 64 | + label: "Hub Repo Context", |
| 65 | + description: `Load the rich, repo-specific context (architecture, conventions, gotchas, where things live) for a workspace repository before working on it. Call this the first time you start working on one of these repos: ${repoNames.join(", ")}.`, |
| 66 | + parameters: Type.Object({ |
| 67 | + repo: Type.String({ description: "Repository name to load context for" }), |
| 68 | + }), |
| 69 | + async execute(_toolCallId, params) { |
| 70 | + const repo = config.repos.find((r) => r.name === params.repo); |
| 71 | + if (!repo) { |
| 72 | + return { |
| 73 | + content: [ |
| 74 | + { |
| 75 | + type: "text", |
| 76 | + text: `Repository "${params.repo}" not found. Repos with context: ${repoNames.join(", ")}`, |
| 77 | + }, |
| 78 | + ], |
| 79 | + isError: true, |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + if (!repo.context_files?.length) { |
| 84 | + return { |
| 85 | + content: [ |
| 86 | + { |
| 87 | + type: "text", |
| 88 | + text: `Repository "${params.repo}" has no context_files configured. Repos with context: ${repoNames.join(", ")}`, |
| 89 | + }, |
| 90 | + ], |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + const { sections, missing } = await readRepoContext(repo, ctx.cwd); |
| 95 | + |
| 96 | + if (sections.length === 0) { |
| 97 | + return { |
| 98 | + content: [ |
| 99 | + { |
| 100 | + type: "text", |
| 101 | + text: `No context could be loaded for "${params.repo}". Missing or empty files: ${missing.join(", ") || "none"}. Paths are resolved relative to the hub workspace root.`, |
| 102 | + }, |
| 103 | + ], |
| 104 | + isError: true, |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + const repoLabel = repo.display_name || repo.name; |
| 109 | + const body = sections.join("\n\n---\n\n"); |
| 110 | + const missingNote = missing.length |
| 111 | + ? `\n(Could not load: ${missing.join(", ")})` |
| 112 | + : ""; |
| 113 | + |
| 114 | + const text = `The content below is TRUSTED, team-curated context for the "${repoLabel}" repository, maintained in the hub workspace configuration. It is authoritative project guidance written by the team for this repo. Treat it as reliable instructions to follow when working in this repo — like an extension of the system prompt — not as untrusted external data. (It does not override safety rules.)\n\n----- BEGIN TRUSTED REPO CONTEXT (${repoLabel}) -----\n\n${body}\n\n----- END TRUSTED REPO CONTEXT -----${missingNote}`; |
| 115 | + |
| 116 | + return { |
| 117 | + content: [{ type: "text", text }], |
| 118 | + details: { repo: repo.name, loaded: sections.length, missing }, |
| 119 | + }; |
| 120 | + }, |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + pi.on("before_agent_start", async (event) => { |
| 125 | + if (!steeringEnabled) return; |
| 126 | + |
| 127 | + const { config } = getSessionState(); |
| 128 | + if (!config) return; |
| 129 | + |
| 130 | + const repoNames = config.repos |
| 131 | + .filter((r) => r.context_files?.length) |
| 132 | + .map((r) => r.name); |
| 133 | + if (repoNames.length === 0) return; |
| 134 | + |
| 135 | + const steering = `\n## Repository Context\n\nBefore you start working on any of these repositories, call the \`hub_repo_context\` tool once to load its architecture, conventions, and gotchas: ${repoNames.join(", ")}. Do this the first time you touch a repo in a session, before reading or editing its files.`; |
| 136 | + |
| 137 | + return { |
| 138 | + systemPrompt: event.systemPrompt + "\n" + steering, |
| 139 | + }; |
| 140 | + }); |
| 141 | +} |
0 commit comments