aboutsummaryrefslogtreecommitdiff
path: root/user/pi/extensions/doc-list.ts
blob: 5d87352c06b7fedfb196e840acb98e203c9ddc1b (plain)
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
import { existsSync, readdirSync } from "node:fs";
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";

const docRoot = "/doc";

function buildDocList(): string | undefined {
  if (!existsSync(docRoot)) {
    return undefined;
  }

  const entries = readdirSync(docRoot, { withFileTypes: true })
    .filter((entry) => entry.isDirectory() || entry.isSymbolicLink())
    .map((entry) => entry.name)
    .sort((a, b) => a.localeCompare(b));

  if (entries.length === 0) {
    return undefined;
  }

  return `/doc/ contains documentation for ${entries.join(", ")}. List files with \`find /doc/$name/\` (mind the trailing slash; don't use -type f).`;
}

export default function docList(pi: ExtensionAPI) {
  pi.on("before_agent_start", (event) => {
    const docList = buildDocList();
    if (docList) {
      event.systemPromptOptions.sections.documentation = docList;
    } else {
      delete event.systemPromptOptions.sections.documentation;
    }
  });
}