import { existsSync } from "node:fs"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; const REQUIRED_OPPI_DOCS = [ "extensions.md", "extension-native-ui.md", "attachment-rendering.md", ] as const; const OPPI_DOCS_HINT_PREFIX = "Oppi documentation (read only when asked about Oppi mobile/runtime behavior):"; const OLD_OPPI_DOCS_HINT_PREFIX = "Oppi documentation for mobile-compatible Pi extensions:"; function moduleDir(): string { return dirname(fileURLToPath(import.meta.url)); } function isOppiDocsDir(path: string): boolean { return REQUIRED_OPPI_DOCS.every((doc) => existsSync(join(path, doc))); } export function getOppiDocsPath(): string | undefined { const dir = moduleDir(); const candidates = [ // Packaged/bundled server build: server/dist/src/oppi-docs.js -> server/dist/docs/oppi resolve(dir, "../docs/oppi"), // Source development: server/src/oppi-docs.ts -> docs resolve(dir, "../../docs"), // Built source checkout without copied docs: server/dist/src/oppi-docs.js -> docs resolve(dir, "../../../docs"), ]; return candidates.find(isOppiDocsDir); } export function buildOppiSystemPromptAppend(docsPath = getOppiDocsPath()): string | undefined { if (!docsPath) { return undefined; } return [ OPPI_DOCS_HINT_PREFIX, `- Docs directory: ${docsPath}`, `- Extensions: ${join(docsPath, "extensions.md")}`, `- Native extension UI: ${join(docsPath, "extension-native-ui.md")}`, `- Attachment rendering: ${join(docsPath, "attachment-rendering.md")}`, "- When working on Oppi topics, read the relevant docs completely and follow .md cross-references before implementing.", ].join("\n"); } export function appendOppiSystemPromptHint(prompt: string): string { if (prompt.includes(OPPI_DOCS_HINT_PREFIX) || prompt.includes(OLD_OPPI_DOCS_HINT_PREFIX)) { return prompt; } const hint = buildOppiSystemPromptAppend(); return hint ? `${prompt}\n\n${hint}` : prompt; }