@tekmidian/pai 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +567 -0
- package/FEATURE.md +108 -0
- package/LICENSE +21 -0
- package/README.md +101 -0
- package/dist/auto-route-D7W6RE06.mjs +86 -0
- package/dist/auto-route-D7W6RE06.mjs.map +1 -0
- package/dist/cli/index.d.mts +1 -0
- package/dist/cli/index.mjs +5927 -0
- package/dist/cli/index.mjs.map +1 -0
- package/dist/config-DBh1bYM2.mjs +151 -0
- package/dist/config-DBh1bYM2.mjs.map +1 -0
- package/dist/daemon/index.d.mts +1 -0
- package/dist/daemon/index.mjs +56 -0
- package/dist/daemon/index.mjs.map +1 -0
- package/dist/daemon-mcp/index.d.mts +1 -0
- package/dist/daemon-mcp/index.mjs +185 -0
- package/dist/daemon-mcp/index.mjs.map +1 -0
- package/dist/daemon-v5O897D4.mjs +773 -0
- package/dist/daemon-v5O897D4.mjs.map +1 -0
- package/dist/db-4lSqLFb8.mjs +199 -0
- package/dist/db-4lSqLFb8.mjs.map +1 -0
- package/dist/db-BcDxXVBu.mjs +110 -0
- package/dist/db-BcDxXVBu.mjs.map +1 -0
- package/dist/detect-BHqYcjJ1.mjs +86 -0
- package/dist/detect-BHqYcjJ1.mjs.map +1 -0
- package/dist/detector-DKA83aTZ.mjs +74 -0
- package/dist/detector-DKA83aTZ.mjs.map +1 -0
- package/dist/embeddings-mfqv-jFu.mjs +91 -0
- package/dist/embeddings-mfqv-jFu.mjs.map +1 -0
- package/dist/factory-BDAiKtYR.mjs +42 -0
- package/dist/factory-BDAiKtYR.mjs.map +1 -0
- package/dist/index.d.mts +307 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +11 -0
- package/dist/indexer-B20bPHL-.mjs +677 -0
- package/dist/indexer-B20bPHL-.mjs.map +1 -0
- package/dist/indexer-backend-BXaocO5r.mjs +360 -0
- package/dist/indexer-backend-BXaocO5r.mjs.map +1 -0
- package/dist/ipc-client-DPy7s3iu.mjs +156 -0
- package/dist/ipc-client-DPy7s3iu.mjs.map +1 -0
- package/dist/mcp/index.d.mts +1 -0
- package/dist/mcp/index.mjs +373 -0
- package/dist/mcp/index.mjs.map +1 -0
- package/dist/migrate-Bwj7qPaE.mjs +241 -0
- package/dist/migrate-Bwj7qPaE.mjs.map +1 -0
- package/dist/pai-marker-DX_mFLum.mjs +186 -0
- package/dist/pai-marker-DX_mFLum.mjs.map +1 -0
- package/dist/postgres-Ccvpc6fC.mjs +335 -0
- package/dist/postgres-Ccvpc6fC.mjs.map +1 -0
- package/dist/rolldown-runtime-95iHPtFO.mjs +18 -0
- package/dist/schemas-DjdwzIQ8.mjs +3405 -0
- package/dist/schemas-DjdwzIQ8.mjs.map +1 -0
- package/dist/search-PjftDxxs.mjs +282 -0
- package/dist/search-PjftDxxs.mjs.map +1 -0
- package/dist/sqlite-CHUrNtbI.mjs +90 -0
- package/dist/sqlite-CHUrNtbI.mjs.map +1 -0
- package/dist/tools-CLK4080-.mjs +805 -0
- package/dist/tools-CLK4080-.mjs.map +1 -0
- package/dist/utils-DEWdIFQ0.mjs +160 -0
- package/dist/utils-DEWdIFQ0.mjs.map +1 -0
- package/package.json +72 -0
- package/templates/README.md +181 -0
- package/templates/agent-prefs.example.md +362 -0
- package/templates/claude-md.template.md +733 -0
- package/templates/pai-project.template.md +13 -0
- package/templates/voices.example.json +251 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools-CLK4080-.mjs","names":[],"sources":["../src/mcp/tools.ts"],"sourcesContent":["/**\n * PAI Knowledge OS — Pure tool handler functions (shared by daemon + legacy MCP server)\n *\n * Each function accepts pre-opened database handles and raw params, executes\n * the tool logic, and returns an MCP-style content array.\n *\n * This module does NOT import indexAll() — indexing is handled by the daemon\n * on its own schedule. The search hot path is pure DB read.\n */\n\nimport { readFileSync, existsSync, statSync } from \"node:fs\";\nimport { join, resolve, isAbsolute } from \"node:path\";\nimport type { Database } from \"better-sqlite3\";\nimport { populateSlugs, searchMemoryHybrid } from \"../memory/search.js\";\nimport { detectProject, formatDetectionJson } from \"../cli/commands/detect.js\";\nimport type { StorageBackend } from \"../storage/interface.js\";\nimport type { NotificationMode, NotificationEvent } from \"../notifications/types.js\";\n\n// ---------------------------------------------------------------------------\n// Shared types\n// ---------------------------------------------------------------------------\n\nexport interface ToolContent {\n type: \"text\";\n text: string;\n}\n\nexport interface ToolResult {\n content: ToolContent[];\n isError?: boolean;\n}\n\n// ---------------------------------------------------------------------------\n// Helper: lookup project_id by slug (also checks aliases)\n// ---------------------------------------------------------------------------\n\nexport function lookupProjectId(\n registryDb: Database,\n slug: string\n): number | null {\n const bySlug = registryDb\n .prepare(\"SELECT id FROM projects WHERE slug = ?\")\n .get(slug) as { id: number } | undefined;\n if (bySlug) return bySlug.id;\n\n const byAlias = registryDb\n .prepare(\"SELECT project_id FROM aliases WHERE alias = ?\")\n .get(slug) as { project_id: number } | undefined;\n if (byAlias) return byAlias.project_id;\n\n return null;\n}\n\n// ---------------------------------------------------------------------------\n// Helper: detect project from a filesystem path\n// ---------------------------------------------------------------------------\n\ninterface ProjectRow {\n id: number;\n slug: string;\n display_name: string;\n root_path: string;\n type: string;\n status: string;\n created_at: number;\n updated_at: number;\n archived_at?: number | null;\n parent_id?: number | null;\n obsidian_link?: string | null;\n}\n\nexport function detectProjectFromPath(\n registryDb: Database,\n fsPath: string\n): ProjectRow | null {\n const resolved = resolve(fsPath);\n\n const exact = registryDb\n .prepare(\n \"SELECT id, slug, display_name, root_path, type, status, created_at, updated_at FROM projects WHERE root_path = ?\"\n )\n .get(resolved) as ProjectRow | undefined;\n\n if (exact) return exact;\n\n const all = registryDb\n .prepare(\n \"SELECT id, slug, display_name, root_path, type, status, created_at, updated_at FROM projects ORDER BY LENGTH(root_path) DESC\"\n )\n .all() as ProjectRow[];\n\n for (const project of all) {\n if (\n resolved.startsWith(project.root_path + \"/\") ||\n resolved === project.root_path\n ) {\n return project;\n }\n }\n\n return null;\n}\n\n// ---------------------------------------------------------------------------\n// Helper: format project row for tool output\n// ---------------------------------------------------------------------------\n\nexport function formatProject(registryDb: Database, project: ProjectRow): string {\n const sessionCount = (\n registryDb\n .prepare(\"SELECT COUNT(*) AS n FROM sessions WHERE project_id = ?\")\n .get(project.id) as { n: number }\n ).n;\n\n const lastSession = registryDb\n .prepare(\n \"SELECT date FROM sessions WHERE project_id = ? ORDER BY date DESC LIMIT 1\"\n )\n .get(project.id) as { date: string } | undefined;\n\n const tags = (\n registryDb\n .prepare(\n `SELECT t.name FROM tags t\n JOIN project_tags pt ON pt.tag_id = t.id\n WHERE pt.project_id = ?\n ORDER BY t.name`\n )\n .all(project.id) as Array<{ name: string }>\n ).map((r) => r.name);\n\n const aliases = (\n registryDb\n .prepare(\"SELECT alias FROM aliases WHERE project_id = ? ORDER BY alias\")\n .all(project.id) as Array<{ alias: string }>\n ).map((r) => r.alias);\n\n const lines: string[] = [\n `slug: ${project.slug}`,\n `display_name: ${project.display_name}`,\n `root_path: ${project.root_path}`,\n `type: ${project.type}`,\n `status: ${project.status}`,\n `sessions: ${sessionCount}`,\n ];\n\n if (lastSession) lines.push(`last_session: ${lastSession.date}`);\n if (tags.length) lines.push(`tags: ${tags.join(\", \")}`);\n if (aliases.length) lines.push(`aliases: ${aliases.join(\", \")}`);\n if (project.obsidian_link) lines.push(`obsidian_link: ${project.obsidian_link}`);\n if (project.archived_at) {\n lines.push(\n `archived_at: ${new Date(project.archived_at).toISOString().slice(0, 10)}`\n );\n }\n\n return lines.join(\"\\n\");\n}\n\n// ---------------------------------------------------------------------------\n// Tool: memory_search\n// ---------------------------------------------------------------------------\n\nexport interface MemorySearchParams {\n query: string;\n project?: string;\n all_projects?: boolean;\n sources?: Array<\"memory\" | \"notes\">;\n limit?: number;\n mode?: \"keyword\" | \"semantic\" | \"hybrid\";\n /** Maximum characters per result snippet. Default 200.\n * Limit context consumption — MCP results go into Claude's context window. */\n snippetLength?: number;\n}\n\nexport async function toolMemorySearch(\n registryDb: Database,\n federation: Database | StorageBackend,\n params: MemorySearchParams\n): Promise<ToolResult> {\n try {\n const projectIds: number[] | undefined = params.project\n ? (() => {\n const id = lookupProjectId(registryDb, params.project!);\n return id != null ? [id] : [];\n })()\n : undefined;\n\n if (params.project && (!projectIds || projectIds.length === 0)) {\n return {\n content: [\n { type: \"text\", text: `Project not found: ${params.project}` },\n ],\n isError: true,\n };\n }\n\n // NOTE: No indexAll() here — indexing is handled by the daemon scheduler.\n // The daemon ensures the index stays fresh; the search hot path is read-only.\n\n const mode = params.mode ?? \"keyword\";\n // Limit context consumption — MCP results go into Claude's context window.\n // Default to 5 results and 200-char snippets to keep a single search call\n // within ~1-2K tokens rather than 5K+.\n const snippetLength = params.snippetLength ?? 200;\n const searchOpts = {\n projectIds,\n sources: params.sources,\n maxResults: params.limit ?? 5,\n };\n\n let results;\n\n // Determine if federation is a StorageBackend or a raw Database\n const isBackend = (x: Database | StorageBackend): x is StorageBackend =>\n \"backendType\" in x;\n\n if (isBackend(federation)) {\n // Use the storage backend interface (works for both SQLite and Postgres)\n if (mode === \"keyword\") {\n results = await federation.searchKeyword(params.query, searchOpts);\n } else if (mode === \"semantic\" || mode === \"hybrid\") {\n const { generateEmbedding } = await import(\"../memory/embeddings.js\");\n const queryEmbedding = await generateEmbedding(params.query, true);\n\n if (mode === \"semantic\") {\n results = await federation.searchSemantic(queryEmbedding, searchOpts);\n } else {\n // Hybrid: combine keyword + semantic\n const [kwResults, semResults] = await Promise.all([\n federation.searchKeyword(params.query, { ...searchOpts, maxResults: 50 }),\n federation.searchSemantic(queryEmbedding, { ...searchOpts, maxResults: 50 }),\n ]); // 50 candidates is sufficient for min-max normalization\n // Reuse the existing hybrid scoring logic\n results = combineHybridResults(kwResults, semResults, searchOpts.maxResults ?? 10);\n }\n } else {\n results = await federation.searchKeyword(params.query, searchOpts);\n }\n } else {\n // Legacy path: raw better-sqlite3 Database (for direct MCP server usage)\n const { searchMemory, searchMemorySemantic } = await import(\"../memory/search.js\");\n\n if (mode === \"keyword\") {\n results = searchMemory(federation, params.query, searchOpts);\n } else if (mode === \"semantic\" || mode === \"hybrid\") {\n const { generateEmbedding } = await import(\"../memory/embeddings.js\");\n const queryEmbedding = await generateEmbedding(params.query, true);\n\n if (mode === \"semantic\") {\n results = searchMemorySemantic(federation, queryEmbedding, searchOpts);\n } else {\n results = searchMemoryHybrid(\n federation,\n params.query,\n queryEmbedding,\n searchOpts\n );\n }\n } else {\n results = searchMemory(federation, params.query, searchOpts);\n }\n }\n\n const withSlugs = populateSlugs(results, registryDb);\n\n if (withSlugs.length === 0) {\n return {\n content: [\n {\n type: \"text\",\n text: `No results found for query: \"${params.query}\" (mode: ${mode})`,\n },\n ],\n };\n }\n\n const formatted = withSlugs\n .map((r, i) => {\n const header = `[${i + 1}] ${r.projectSlug ?? `project:${r.projectId}`} — ${r.path} (lines ${r.startLine}-${r.endLine}) score=${r.score.toFixed(4)} tier=${r.tier} source=${r.source}`;\n // Truncate snippet to snippetLength — limit context consumption.\n // MCP results go into Claude's context window; keep each result tight.\n const raw = r.snippet.trim();\n const snippet = raw.length > snippetLength\n ? raw.slice(0, snippetLength) + \"...\"\n : raw;\n return `${header}\\n${snippet}`;\n })\n .join(\"\\n\\n---\\n\\n\");\n\n return {\n content: [\n {\n type: \"text\",\n text: `Found ${withSlugs.length} result(s) for \"${params.query}\" (mode: ${mode}):\\n\\n${formatted}`,\n },\n ],\n };\n } catch (e) {\n return {\n content: [{ type: \"text\", text: `Search error: ${String(e)}` }],\n isError: true,\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// Tool: memory_get\n// ---------------------------------------------------------------------------\n\nexport interface MemoryGetParams {\n project: string;\n path: string;\n from?: number;\n lines?: number;\n}\n\nexport function toolMemoryGet(\n registryDb: Database,\n params: MemoryGetParams\n): ToolResult {\n try {\n const projectId = lookupProjectId(registryDb, params.project);\n if (projectId == null) {\n return {\n content: [\n { type: \"text\", text: `Project not found: ${params.project}` },\n ],\n isError: true,\n };\n }\n\n const project = registryDb\n .prepare(\"SELECT root_path FROM projects WHERE id = ?\")\n .get(projectId) as { root_path: string } | undefined;\n\n if (!project) {\n return {\n content: [\n { type: \"text\", text: `Project not found: ${params.project}` },\n ],\n isError: true,\n };\n }\n\n const requestedPath = params.path;\n if (requestedPath.includes(\"..\") || isAbsolute(requestedPath)) {\n return {\n content: [\n {\n type: \"text\",\n text: `Invalid path: ${params.path} (must be a relative path within the project root, no ../ allowed)`,\n },\n ],\n isError: true,\n };\n }\n\n const fullPath = join(project.root_path, requestedPath);\n const resolvedFull = resolve(fullPath);\n const resolvedRoot = resolve(project.root_path);\n\n if (\n !resolvedFull.startsWith(resolvedRoot + \"/\") &&\n resolvedFull !== resolvedRoot\n ) {\n return {\n content: [\n { type: \"text\", text: `Path traversal blocked: ${params.path}` },\n ],\n isError: true,\n };\n }\n\n if (!existsSync(fullPath)) {\n return {\n content: [\n {\n type: \"text\",\n text: `File not found: ${requestedPath} (project: ${params.project})`,\n },\n ],\n isError: true,\n };\n }\n\n const stat = statSync(fullPath);\n if (stat.size > 5 * 1024 * 1024) {\n return {\n content: [\n {\n type: \"text\",\n text: `Error: file too large (${(stat.size / 1024 / 1024).toFixed(1)} MB). Maximum 5 MB.`,\n },\n ],\n };\n }\n\n const content = readFileSync(fullPath, \"utf8\");\n const allLines = content.split(\"\\n\");\n\n const fromLine = (params.from ?? 1) - 1;\n const toLine =\n params.lines != null\n ? Math.min(fromLine + params.lines, allLines.length)\n : allLines.length;\n\n const selectedLines = allLines.slice(fromLine, toLine);\n const text = selectedLines.join(\"\\n\");\n\n const header =\n params.from != null\n ? `${params.project}/${requestedPath} (lines ${fromLine + 1}-${toLine}):`\n : `${params.project}/${requestedPath}:`;\n\n return {\n content: [{ type: \"text\", text: `${header}\\n\\n${text}` }],\n };\n } catch (e) {\n return {\n content: [{ type: \"text\", text: `Read error: ${String(e)}` }],\n isError: true,\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// Tool: project_info\n// ---------------------------------------------------------------------------\n\nexport interface ProjectInfoParams {\n slug?: string;\n}\n\nexport function toolProjectInfo(\n registryDb: Database,\n params: ProjectInfoParams\n): ToolResult {\n try {\n let project: ProjectRow | null = null;\n\n if (params.slug) {\n const projectId = lookupProjectId(registryDb, params.slug);\n if (projectId != null) {\n project = registryDb\n .prepare(\n \"SELECT id, slug, display_name, root_path, type, status, created_at, updated_at, archived_at, parent_id, obsidian_link FROM projects WHERE id = ?\"\n )\n .get(projectId) as ProjectRow | null;\n }\n } else {\n const cwd = process.cwd();\n project = detectProjectFromPath(registryDb, cwd);\n }\n\n if (!project) {\n const message = params.slug\n ? `Project not found: ${params.slug}`\n : `No PAI project found matching the current directory: ${process.cwd()}`;\n return {\n content: [{ type: \"text\", text: message }],\n isError: !params.slug,\n };\n }\n\n return {\n content: [{ type: \"text\", text: formatProject(registryDb, project) }],\n };\n } catch (e) {\n return {\n content: [{ type: \"text\", text: `project_info error: ${String(e)}` }],\n isError: true,\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// Tool: project_list\n// ---------------------------------------------------------------------------\n\nexport interface ProjectListParams {\n status?: \"active\" | \"archived\" | \"migrating\";\n tag?: string;\n limit?: number;\n}\n\nexport function toolProjectList(\n registryDb: Database,\n params: ProjectListParams\n): ToolResult {\n try {\n const conditions: string[] = [];\n const queryParams: (string | number)[] = [];\n\n if (params.status) {\n conditions.push(\"p.status = ?\");\n queryParams.push(params.status);\n }\n\n if (params.tag) {\n conditions.push(\n \"p.id IN (SELECT pt.project_id FROM project_tags pt JOIN tags t ON pt.tag_id = t.id WHERE t.name = ?)\"\n );\n queryParams.push(params.tag);\n }\n\n const where =\n conditions.length > 0 ? `WHERE ${conditions.join(\" AND \")}` : \"\";\n const limit = params.limit ?? 50;\n queryParams.push(limit);\n\n const projects = registryDb\n .prepare(\n `SELECT p.id, p.slug, p.display_name, p.root_path, p.type, p.status, p.updated_at\n FROM projects p\n ${where}\n ORDER BY p.updated_at DESC\n LIMIT ?`\n )\n .all(...queryParams) as Array<{\n id: number;\n slug: string;\n display_name: string;\n root_path: string;\n type: string;\n status: string;\n updated_at: number;\n }>;\n\n if (projects.length === 0) {\n return {\n content: [\n {\n type: \"text\",\n text: \"No projects found matching the given filters.\",\n },\n ],\n };\n }\n\n const lines = projects.map(\n (p) =>\n `${p.slug} [${p.status}] ${p.root_path} (updated: ${new Date(p.updated_at).toISOString().slice(0, 10)})`\n );\n\n return {\n content: [\n {\n type: \"text\",\n text: `${projects.length} project(s):\\n\\n${lines.join(\"\\n\")}`,\n },\n ],\n };\n } catch (e) {\n return {\n content: [{ type: \"text\", text: `project_list error: ${String(e)}` }],\n isError: true,\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// Tool: session_list\n// ---------------------------------------------------------------------------\n\nexport interface SessionListParams {\n project: string;\n limit?: number;\n status?: \"open\" | \"completed\" | \"compacted\";\n}\n\nexport function toolSessionList(\n registryDb: Database,\n params: SessionListParams\n): ToolResult {\n try {\n const projectId = lookupProjectId(registryDb, params.project);\n if (projectId == null) {\n return {\n content: [\n { type: \"text\", text: `Project not found: ${params.project}` },\n ],\n isError: true,\n };\n }\n\n const conditions = [\"project_id = ?\"];\n const queryParams: (string | number)[] = [projectId];\n\n if (params.status) {\n conditions.push(\"status = ?\");\n queryParams.push(params.status);\n }\n\n const limit = params.limit ?? 10;\n queryParams.push(limit);\n\n const sessions = registryDb\n .prepare(\n `SELECT number, date, title, filename, status\n FROM sessions\n WHERE ${conditions.join(\" AND \")}\n ORDER BY number DESC\n LIMIT ?`\n )\n .all(...queryParams) as Array<{\n number: number;\n date: string;\n title: string;\n filename: string;\n status: string;\n }>;\n\n if (sessions.length === 0) {\n return {\n content: [\n {\n type: \"text\",\n text: `No sessions found for project: ${params.project}`,\n },\n ],\n };\n }\n\n const lines = sessions.map(\n (s) =>\n `#${String(s.number).padStart(4, \"0\")} ${s.date} [${s.status}] ${s.title}\\n file: Notes/${s.filename}`\n );\n\n return {\n content: [\n {\n type: \"text\",\n text: `${sessions.length} session(s) for ${params.project}:\\n\\n${lines.join(\"\\n\\n\")}`,\n },\n ],\n };\n } catch (e) {\n return {\n content: [{ type: \"text\", text: `session_list error: ${String(e)}` }],\n isError: true,\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// Tool: registry_search\n// ---------------------------------------------------------------------------\n\nexport interface RegistrySearchParams {\n query: string;\n}\n\nexport function toolRegistrySearch(\n registryDb: Database,\n params: RegistrySearchParams\n): ToolResult {\n try {\n const q = `%${params.query}%`;\n const projects = registryDb\n .prepare(\n `SELECT id, slug, display_name, root_path, type, status, updated_at\n FROM projects\n WHERE slug LIKE ?\n OR display_name LIKE ?\n OR root_path LIKE ?\n ORDER BY updated_at DESC\n LIMIT 20`\n )\n .all(q, q, q) as Array<{\n id: number;\n slug: string;\n display_name: string;\n root_path: string;\n type: string;\n status: string;\n updated_at: number;\n }>;\n\n if (projects.length === 0) {\n return {\n content: [\n {\n type: \"text\",\n text: `No projects found matching: \"${params.query}\"`,\n },\n ],\n };\n }\n\n const lines = projects.map((p) => `${p.slug} [${p.status}] ${p.root_path}`);\n\n return {\n content: [\n {\n type: \"text\",\n text: `${projects.length} match(es) for \"${params.query}\":\\n\\n${lines.join(\"\\n\")}`,\n },\n ],\n };\n } catch (e) {\n return {\n content: [\n { type: \"text\", text: `registry_search error: ${String(e)}` },\n ],\n isError: true,\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// Tool: project_detect\n// ---------------------------------------------------------------------------\n\nexport interface ProjectDetectParams {\n cwd?: string;\n}\n\nexport function toolProjectDetect(\n registryDb: Database,\n params: ProjectDetectParams\n): ToolResult {\n try {\n const detection = detectProject(registryDb, params.cwd);\n\n if (!detection) {\n const target = params.cwd ?? process.cwd();\n return {\n content: [\n {\n type: \"text\",\n text: `No registered project found for path: ${target}\\n\\nRun 'pai project add .' to register this directory.`,\n },\n ],\n };\n }\n\n return {\n content: [{ type: \"text\", text: formatDetectionJson(detection) }],\n };\n } catch (e) {\n return {\n content: [\n { type: \"text\", text: `project_detect error: ${String(e)}` },\n ],\n isError: true,\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// Tool: project_health\n// ---------------------------------------------------------------------------\n\nexport interface ProjectHealthParams {\n category?: \"active\" | \"stale\" | \"dead\" | \"all\";\n}\n\nexport async function toolProjectHealth(\n registryDb: Database,\n params: ProjectHealthParams\n): Promise<ToolResult> {\n try {\n const { existsSync: fsExists, readdirSync, statSync } = await import(\n \"node:fs\"\n );\n const {\n join: pathJoin,\n basename: pathBasename,\n } = await import(\"node:path\");\n const { homedir } = await import(\"node:os\");\n const { encodeDir: enc } = await import(\"../cli/utils.js\");\n\n interface HealthRowLocal {\n id: number;\n slug: string;\n display_name: string;\n root_path: string;\n encoded_dir: string;\n status: string;\n type: string;\n session_count: number;\n }\n\n const rows = registryDb\n .prepare(\n `SELECT p.id, p.slug, p.display_name, p.root_path, p.encoded_dir, p.status, p.type,\n (SELECT COUNT(*) FROM sessions s WHERE s.project_id = p.id) AS session_count\n FROM projects p\n ORDER BY p.slug ASC`\n )\n .all() as HealthRowLocal[];\n\n const home = homedir();\n const claudeProjects = pathJoin(home, \".claude\", \"projects\");\n\n function suggestMoved(rootPath: string): string | undefined {\n const name = pathBasename(rootPath);\n const candidates = [\n pathJoin(home, \"dev\", name),\n pathJoin(home, \"dev\", \"ai\", name),\n pathJoin(home, \"Desktop\", name),\n pathJoin(home, \"Projects\", name),\n ];\n return candidates.find((c) => fsExists(c));\n }\n\n function hasClaudeNotes(encodedDir: string): boolean {\n if (!fsExists(claudeProjects)) return false;\n try {\n for (const entry of readdirSync(claudeProjects)) {\n if (entry !== encodedDir && !entry.startsWith(encodedDir)) continue;\n const full = pathJoin(claudeProjects, entry);\n try {\n if (!statSync(full).isDirectory()) continue;\n } catch {\n continue;\n }\n if (fsExists(pathJoin(full, \"Notes\"))) return true;\n }\n } catch {\n /* ignore */\n }\n return false;\n }\n\n interface HealthResult {\n slug: string;\n display_name: string;\n root_path: string;\n status: string;\n type: string;\n session_count: number;\n health: string;\n suggested_path: string | null;\n has_claude_notes: boolean;\n todo: {\n found: boolean;\n path: string | null;\n has_continue: boolean;\n };\n }\n\n function findTodoForProject(rootPath: string): {\n found: boolean;\n path: string | null;\n has_continue: boolean;\n } {\n const locs = [\n \"Notes/TODO.md\",\n \".claude/Notes/TODO.md\",\n \"tasks/todo.md\",\n \"TODO.md\",\n ];\n for (const rel of locs) {\n const full = pathJoin(rootPath, rel);\n if (fsExists(full)) {\n try {\n const raw = readFileSync(full, \"utf8\");\n const hasContinue = /^## Continue$/m.test(raw);\n return { found: true, path: rel, has_continue: hasContinue };\n } catch {\n return { found: true, path: rel, has_continue: false };\n }\n }\n }\n return { found: false, path: null, has_continue: false };\n }\n\n const results: HealthResult[] = rows.map((p) => {\n const pathExists = fsExists(p.root_path);\n let health: string;\n let suggestedPath: string | null = null;\n\n if (pathExists) {\n health = \"active\";\n } else {\n suggestedPath = suggestMoved(p.root_path) ?? null;\n health = suggestedPath ? \"stale\" : \"dead\";\n }\n\n const todo = pathExists\n ? findTodoForProject(p.root_path)\n : { found: false, path: null, has_continue: false };\n\n return {\n slug: p.slug,\n display_name: p.display_name,\n root_path: p.root_path,\n status: p.status,\n type: p.type,\n session_count: p.session_count,\n health,\n suggested_path: suggestedPath,\n has_claude_notes: hasClaudeNotes(p.encoded_dir),\n todo,\n };\n });\n\n const filtered =\n !params.category || params.category === \"all\"\n ? results\n : results.filter((r) => r.health === params.category);\n\n const summary = {\n total: rows.length,\n active: results.filter((r) => r.health === \"active\").length,\n stale: results.filter((r) => r.health === \"stale\").length,\n dead: results.filter((r) => r.health === \"dead\").length,\n };\n\n return {\n content: [\n {\n type: \"text\",\n text: JSON.stringify({ summary, projects: filtered }, null, 2),\n },\n ],\n };\n } catch (e) {\n return {\n content: [\n { type: \"text\", text: `project_health error: ${String(e)}` },\n ],\n isError: true,\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// Tool: project_todo\n// ---------------------------------------------------------------------------\n\nexport interface ProjectTodoParams {\n project?: string;\n}\n\n/**\n * TODO candidate locations searched in priority order.\n * Returns the first one that exists, along with its label.\n */\nconst TODO_LOCATIONS = [\n { rel: \"Notes/TODO.md\", label: \"Notes/TODO.md\" },\n { rel: \".claude/Notes/TODO.md\", label: \".claude/Notes/TODO.md\" },\n { rel: \"tasks/todo.md\", label: \"tasks/todo.md\" },\n { rel: \"TODO.md\", label: \"TODO.md\" },\n];\n\n/**\n * Given TODO file content, extract and surface the ## Continue section first,\n * then return the remaining content. Returns an object with:\n * continueSection: string | null\n * fullContent: string\n * hasContinue: boolean\n */\nfunction parseTodoContent(raw: string): {\n continueSection: string | null;\n fullContent: string;\n hasContinue: boolean;\n} {\n const lines = raw.split(\"\\n\");\n\n // Find the ## Continue heading\n const continueIdx = lines.findIndex(\n (l) => l.trim() === \"## Continue\"\n );\n\n if (continueIdx === -1) {\n return { continueSection: null, fullContent: raw, hasContinue: false };\n }\n\n // The section ends at the first `---` separator or next `##` heading after\n // the Continue heading (whichever comes first).\n let endIdx = lines.length;\n for (let i = continueIdx + 1; i < lines.length; i++) {\n const trimmed = lines[i].trim();\n if (trimmed === \"---\" || (trimmed.startsWith(\"##\") && trimmed !== \"## Continue\")) {\n endIdx = i;\n break;\n }\n }\n\n const continueLines = lines.slice(continueIdx, endIdx);\n const continueSection = continueLines.join(\"\\n\").trim();\n\n return { continueSection, fullContent: raw, hasContinue: true };\n}\n\nexport function toolProjectTodo(\n registryDb: Database,\n params: ProjectTodoParams\n): ToolResult {\n try {\n let rootPath: string;\n let projectSlug: string;\n\n if (params.project) {\n const projectId = lookupProjectId(registryDb, params.project);\n if (projectId == null) {\n return {\n content: [\n { type: \"text\", text: `Project not found: ${params.project}` },\n ],\n isError: true,\n };\n }\n\n const row = registryDb\n .prepare(\"SELECT root_path, slug FROM projects WHERE id = ?\")\n .get(projectId) as { root_path: string; slug: string } | undefined;\n\n if (!row) {\n return {\n content: [\n { type: \"text\", text: `Project not found: ${params.project}` },\n ],\n isError: true,\n };\n }\n\n rootPath = row.root_path;\n projectSlug = row.slug;\n } else {\n // Auto-detect from cwd\n const project = detectProjectFromPath(registryDb, process.cwd());\n if (!project) {\n return {\n content: [\n {\n type: \"text\",\n text: `No PAI project found matching the current directory: ${process.cwd()}\\n\\nProvide a project slug or run 'pai project add .' to register this directory.`,\n },\n ],\n };\n }\n rootPath = project.root_path;\n projectSlug = project.slug;\n }\n\n // Search for TODO in priority order\n for (const loc of TODO_LOCATIONS) {\n const fullPath = join(rootPath, loc.rel);\n if (existsSync(fullPath)) {\n const raw = readFileSync(fullPath, \"utf8\");\n const { continueSection, fullContent, hasContinue } = parseTodoContent(raw);\n\n let output: string;\n if (hasContinue && continueSection) {\n // Surface the ## Continue section first, then the full content\n output = [\n `TODO found: ${projectSlug}/${loc.label}`,\n \"\",\n \"=== CONTINUE SECTION (surfaced first) ===\",\n continueSection,\n \"\",\n \"=== FULL TODO CONTENT ===\",\n fullContent,\n ].join(\"\\n\");\n } else {\n output = [\n `TODO found: ${projectSlug}/${loc.label}`,\n \"\",\n fullContent,\n ].join(\"\\n\");\n }\n\n return {\n content: [{ type: \"text\", text: output }],\n };\n }\n }\n\n // No TODO found in any location\n const searched = TODO_LOCATIONS.map((l) => ` ${rootPath}/${l.rel}`).join(\"\\n\");\n return {\n content: [\n {\n type: \"text\",\n text: [\n `No TODO.md found for project: ${projectSlug}`,\n \"\",\n \"Searched locations (in order):\",\n searched,\n \"\",\n \"Create a TODO with: echo '## Tasks\\\\n- [ ] First task' > Notes/TODO.md\",\n ].join(\"\\n\"),\n },\n ],\n };\n } catch (e) {\n return {\n content: [{ type: \"text\", text: `project_todo error: ${String(e)}` }],\n isError: true,\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// Tool: notification_config\n// ---------------------------------------------------------------------------\n\nexport interface NotificationConfigParams {\n /** Action to perform */\n action: \"get\" | \"set\" | \"send\";\n /** For action=\"set\": the notification mode to activate */\n mode?: NotificationMode;\n /** For action=\"set\": partial channel config overrides (JSON object) */\n channels?: Record<string, unknown>;\n /** For action=\"set\": partial routing overrides (JSON object) */\n routing?: Record<string, unknown>;\n /** For action=\"send\": the event type */\n event?: NotificationEvent;\n /** For action=\"send\": the notification message */\n message?: string;\n /** For action=\"send\": optional title */\n title?: string;\n}\n\n/**\n * Handle notification config queries and updates via the daemon IPC.\n * Falls back gracefully if the daemon is not running.\n */\nexport async function toolNotificationConfig(\n params: NotificationConfigParams\n): Promise<ToolResult> {\n try {\n const { PaiClient } = await import(\"../daemon/ipc-client.js\");\n const client = new PaiClient();\n\n if (params.action === \"get\") {\n const { config, activeChannels } = await client.getNotificationConfig();\n const lines = [\n `mode: ${config.mode}`,\n `active_channels: ${activeChannels.join(\", \") || \"(none)\"}`,\n \"\",\n \"channels:\",\n ...Object.entries(config.channels).map(([ch, cfg]) => {\n const c = cfg as { enabled: boolean };\n return ` ${ch}: ${c.enabled ? \"enabled\" : \"disabled\"}`;\n }),\n \"\",\n \"routing:\",\n ...Object.entries(config.routing).map(\n ([event, channels]) => ` ${event}: ${(channels as string[]).join(\", \") || \"(none)\"}`\n ),\n ];\n return {\n content: [{ type: \"text\", text: lines.join(\"\\n\") }],\n };\n }\n\n if (params.action === \"set\") {\n if (!params.mode && !params.channels && !params.routing) {\n return {\n content: [\n {\n type: \"text\",\n text: \"notification_config set: provide at least one of mode, channels, or routing.\",\n },\n ],\n isError: true,\n };\n }\n const result = await client.setNotificationConfig({\n mode: params.mode,\n channels: params.channels as Parameters<typeof client.setNotificationConfig>[0][\"channels\"],\n routing: params.routing as Parameters<typeof client.setNotificationConfig>[0][\"routing\"],\n });\n return {\n content: [\n {\n type: \"text\",\n text: `Notification config updated. Mode: ${result.config.mode}`,\n },\n ],\n };\n }\n\n if (params.action === \"send\") {\n if (!params.message) {\n return {\n content: [\n { type: \"text\", text: \"notification_config send: message is required.\" },\n ],\n isError: true,\n };\n }\n const result = await client.sendNotification({\n event: params.event ?? \"info\",\n message: params.message,\n title: params.title,\n });\n const lines = [\n `mode: ${result.mode}`,\n `attempted: ${result.channelsAttempted.join(\", \") || \"(none)\"}`,\n `succeeded: ${result.channelsSucceeded.join(\", \") || \"(none)\"}`,\n ...(result.channelsFailed.length > 0\n ? [`failed: ${result.channelsFailed.join(\", \")}`]\n : []),\n ];\n return {\n content: [{ type: \"text\", text: lines.join(\"\\n\") }],\n };\n }\n\n return {\n content: [\n {\n type: \"text\",\n text: `Unknown action: ${String(params.action)}. Use \"get\", \"set\", or \"send\".`,\n },\n ],\n isError: true,\n };\n } catch (e) {\n return {\n content: [\n { type: \"text\", text: `notification_config error: ${String(e)}` },\n ],\n isError: true,\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// Tool: topic_detect\n// ---------------------------------------------------------------------------\n\nexport interface TopicDetectParams {\n /** Recent conversation context (a few sentences summarising recent activity) */\n context: string;\n /** The project slug the session is currently routed to. */\n current_project?: string;\n /**\n * Minimum confidence [0,1] to declare a shift. Default: 0.6.\n * Higher = less sensitive (fewer false positives).\n */\n threshold?: number;\n}\n\n/**\n * Detect whether recent conversation context has shifted to a different project.\n * Uses memory_search to find which project best matches the context, then\n * compares against the current project.\n *\n * Calls the daemon via IPC so it has access to the storage backend.\n * Falls back gracefully if the daemon is not running.\n */\nexport async function toolTopicDetect(\n params: TopicDetectParams\n): Promise<ToolResult> {\n try {\n const { PaiClient } = await import(\"../daemon/ipc-client.js\");\n const client = new PaiClient();\n\n const result = await client.topicCheck({\n context: params.context,\n currentProject: params.current_project,\n threshold: params.threshold,\n });\n\n const lines: string[] = [\n `shifted: ${result.shifted}`,\n `current_project: ${result.currentProject ?? \"(none)\"}`,\n `suggested_project: ${result.suggestedProject ?? \"(none)\"}`,\n `confidence: ${result.confidence.toFixed(3)}`,\n `chunks_scored: ${result.chunkCount}`,\n ];\n\n if (result.topProjects.length > 0) {\n lines.push(\"\");\n lines.push(\"top_matches:\");\n for (const p of result.topProjects) {\n lines.push(` ${p.slug}: ${(p.score * 100).toFixed(1)}%`);\n }\n }\n\n if (result.shifted) {\n lines.push(\"\");\n lines.push(\n `TOPIC SHIFT DETECTED: conversation appears to be about \"${result.suggestedProject}\" ` +\n `(confidence: ${(result.confidence * 100).toFixed(0)}%), not \"${result.currentProject}\".`\n );\n }\n\n return {\n content: [{ type: \"text\", text: lines.join(\"\\n\") }],\n };\n } catch (e) {\n return {\n content: [{ type: \"text\", text: `topic_detect error: ${String(e)}` }],\n isError: true,\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// Tool: session_route\n// ---------------------------------------------------------------------------\n\nexport interface SessionRouteParams {\n /** Working directory to route from (defaults to process.cwd()) */\n cwd?: string;\n /** Optional conversation context for topic-based fallback routing */\n context?: string;\n}\n\n/**\n * Automatically suggest which project a session belongs to.\n *\n * Strategy (in priority order):\n * 1. path — exact or parent-directory match in the project registry\n * 2. marker — walk up from cwd looking for Notes/PAI.md\n * 3. topic — BM25 keyword search against memory (requires context)\n *\n * Call this at session start (e.g., from CLAUDE.md or a session-start hook)\n * to automatically route the session to the correct project.\n */\nexport async function toolSessionRoute(\n registryDb: Database,\n federation: Database | StorageBackend,\n params: SessionRouteParams\n): Promise<ToolResult> {\n try {\n const { autoRoute, formatAutoRouteJson } = await import(\"../session/auto-route.js\");\n\n const result = await autoRoute(\n registryDb,\n federation,\n params.cwd,\n params.context\n );\n\n if (!result) {\n const target = params.cwd ?? process.cwd();\n return {\n content: [\n {\n type: \"text\",\n text: [\n `No project match found for: ${target}`,\n \"\",\n \"Tried: path match, PAI.md marker walk\" +\n (params.context ? \", topic detection\" : \"\"),\n \"\",\n \"Run 'pai project add .' to register this directory,\",\n \"or provide conversation context for topic-based routing.\",\n ].join(\"\\n\"),\n },\n ],\n };\n }\n\n return {\n content: [{ type: \"text\", text: formatAutoRouteJson(result) }],\n };\n } catch (e) {\n return {\n content: [{ type: \"text\", text: `session_route error: ${String(e)}` }],\n isError: true,\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// Hybrid search helper (backend-agnostic)\n// ---------------------------------------------------------------------------\n\nimport type { SearchResult } from \"../memory/search.js\";\n\n/**\n * Combine keyword + semantic results using min-max normalized scoring.\n * Mirrors the logic in searchMemoryHybrid() from memory/search.ts,\n * but works on pre-computed result arrays so it works for any backend.\n */\nfunction combineHybridResults(\n keywordResults: SearchResult[],\n semanticResults: SearchResult[],\n maxResults: number,\n keywordWeight = 0.5,\n semanticWeight = 0.5\n): SearchResult[] {\n if (keywordResults.length === 0 && semanticResults.length === 0) return [];\n\n const keyFor = (r: SearchResult) =>\n `${r.projectId}:${r.path}:${r.startLine}:${r.endLine}`;\n\n function minMaxNormalize(items: SearchResult[]): Map<string, number> {\n if (items.length === 0) return new Map();\n const min = Math.min(...items.map((r) => r.score));\n const max = Math.max(...items.map((r) => r.score));\n const range = max - min;\n const m = new Map<string, number>();\n for (const r of items) {\n m.set(keyFor(r), range === 0 ? 1 : (r.score - min) / range);\n }\n return m;\n }\n\n const kwNorm = minMaxNormalize(keywordResults);\n const semNorm = minMaxNormalize(semanticResults);\n\n const allKeys = new Set<string>([\n ...keywordResults.map(keyFor),\n ...semanticResults.map(keyFor),\n ]);\n\n const metaMap = new Map<string, SearchResult>();\n for (const r of [...keywordResults, ...semanticResults]) {\n metaMap.set(keyFor(r), r);\n }\n\n const combined: Array<SearchResult & { combinedScore: number }> = [];\n for (const key of allKeys) {\n const meta = metaMap.get(key)!;\n const kwScore = kwNorm.get(key) ?? 0;\n const semScore = semNorm.get(key) ?? 0;\n const combinedScore = keywordWeight * kwScore + semanticWeight * semScore;\n combined.push({ ...meta, score: combinedScore, combinedScore });\n }\n\n return combined\n .sort((a, b) => b.score - a.score)\n .slice(0, maxResults)\n .map(({ combinedScore: _unused, ...r }) => r);\n}\n"],"mappings":";;;;;;;;;;;;;;;AAoCA,SAAgB,gBACd,YACA,MACe;CACf,MAAM,SAAS,WACZ,QAAQ,yCAAyC,CACjD,IAAI,KAAK;AACZ,KAAI,OAAQ,QAAO,OAAO;CAE1B,MAAM,UAAU,WACb,QAAQ,iDAAiD,CACzD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AgB,IAAI,OAAO,CAC/B,CAAC;CAEF,MAAM,0BAAU,IAAI,KAA2B;AAC/C,MAAK,MAAM,KAAK,CAAC,GAAG,gBAAgB,GAAG,gBAAgB,CACrD,SAAQ,IAAI,OAAO,EAAE,EAAE,EAAE;CAG3B,MAAM,WAA4D,EAAE;AACpE,MAAK,MAAM,OAAO,SAAS;EACzB,MAAM,OAAO,QAAQ,IAAI,IAAI;EAC7B,MAAM,UAAU,OAAO,IAAI,IAAI,IAAI;EACnC,MAAM,WAAW,QAAQ,IAAI,IAAI,IAAI;EACrC,MAAM,gBAAgB,gBAAgB,UAAU,iBAAiB;AACjE,WAAS,KAAK;GAAE,GAAG;GAAM,OAAO;GAAe;GAAe,CAAC;;AAGjE,QAAO,SACJ,MAAM,GAAG,MAAM,EAAE,QAAQ,EAAE,MAAM,CACjC,MAAM,GAAG,WAAW,CACpB,KAAK,EAAE,eAAe,SAAS,GAAG,QAAQ,EAAE"}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-95iHPtFO.mjs";
|
|
2
|
+
import { existsSync, mkdirSync, readdirSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { basename, join, resolve } from "node:path";
|
|
5
|
+
import chalk from "chalk";
|
|
6
|
+
|
|
7
|
+
//#region src/cli/utils.ts
|
|
8
|
+
/**
|
|
9
|
+
* Shared utilities for CLI commands: formatting helpers, path encoding,
|
|
10
|
+
* slug generation, and chalk colour wrappers.
|
|
11
|
+
*/
|
|
12
|
+
var utils_exports = /* @__PURE__ */ __exportAll({
|
|
13
|
+
bold: () => bold,
|
|
14
|
+
dim: () => dim,
|
|
15
|
+
encodeDir: () => encodeDir,
|
|
16
|
+
err: () => err,
|
|
17
|
+
fmtDate: () => fmtDate,
|
|
18
|
+
header: () => header,
|
|
19
|
+
now: () => now,
|
|
20
|
+
ok: () => ok,
|
|
21
|
+
pad: () => pad,
|
|
22
|
+
renderTable: () => renderTable,
|
|
23
|
+
resolvePath: () => resolvePath,
|
|
24
|
+
scaffoldProjectDirs: () => scaffoldProjectDirs,
|
|
25
|
+
shortenPath: () => shortenPath,
|
|
26
|
+
slugFromPath: () => slugFromPath,
|
|
27
|
+
slugify: () => slugify,
|
|
28
|
+
warn: () => warn
|
|
29
|
+
});
|
|
30
|
+
const ok = (msg) => chalk.green(msg);
|
|
31
|
+
const warn = (msg) => chalk.yellow(msg);
|
|
32
|
+
const err = (msg) => chalk.red(msg);
|
|
33
|
+
const dim = (msg) => chalk.dim(msg);
|
|
34
|
+
const bold = (msg) => chalk.bold(msg);
|
|
35
|
+
const header = (msg) => chalk.bold.underline(msg);
|
|
36
|
+
/**
|
|
37
|
+
* Convert any path string into a kebab-case slug.
|
|
38
|
+
* "/Users/foo/my-project" → "my-project"
|
|
39
|
+
* "Some Cool Project" → "some-cool-project"
|
|
40
|
+
*/
|
|
41
|
+
function slugify(input) {
|
|
42
|
+
return input.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Derive a default project slug from the last component of a path.
|
|
46
|
+
* "/home/user/my-project" → "my-project"
|
|
47
|
+
*/
|
|
48
|
+
function slugFromPath(projectPath) {
|
|
49
|
+
return slugify(basename(projectPath));
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Encode an absolute path into Claude Code's encoded-dir format.
|
|
53
|
+
*
|
|
54
|
+
* Claude Code's actual encoding rules (reverse-engineered from real data):
|
|
55
|
+
* - Every `/`, ` ` (space), `.` (dot), and `-` (literal hyphen) → single `-`
|
|
56
|
+
* - The result therefore starts with `-` (from the leading `/`)
|
|
57
|
+
*
|
|
58
|
+
* This is a lossy encoding — space, dot, hyphen, and path-separator all
|
|
59
|
+
* collapse to the same token. The decode is therefore ambiguous; prefer
|
|
60
|
+
* {@link buildEncodedDirMap} from migrate.ts to get authoritative mappings.
|
|
61
|
+
*
|
|
62
|
+
* Examples:
|
|
63
|
+
* "/Users/foo/my-project" → "-Users-foo-my-project"
|
|
64
|
+
* "/Users/foo/04 - Ablage" → "-Users-foo-04---Ablage"
|
|
65
|
+
* "/Users/foo/.ssh" → "-Users-foo--ssh"
|
|
66
|
+
* "/Users/foo/MDF-System.de" → "-Users-foo-MDF-System-de"
|
|
67
|
+
*
|
|
68
|
+
* NOTE: For `project add`, prefer {@link findExistingEncodedDir} to look up
|
|
69
|
+
* whether Claude Code has already created a directory for this path — that
|
|
70
|
+
* avoids any mismatch between our encoding and Claude's.
|
|
71
|
+
*/
|
|
72
|
+
function encodeDir(absolutePath) {
|
|
73
|
+
return absolutePath.replace(/[\/\s.\-]/g, "-");
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Resolve a raw CLI path argument to an absolute path.
|
|
77
|
+
*/
|
|
78
|
+
function resolvePath(rawPath) {
|
|
79
|
+
return resolve(rawPath);
|
|
80
|
+
}
|
|
81
|
+
const MEMORY_MD_SCAFFOLD = `# Memory
|
|
82
|
+
|
|
83
|
+
Project-specific memory for PAI sessions.
|
|
84
|
+
Add persistent notes, reminders, and context here.
|
|
85
|
+
`;
|
|
86
|
+
/**
|
|
87
|
+
* Ensure Notes/ and memory/ sub-directories exist under `projectRoot`.
|
|
88
|
+
* Also creates a memory/MEMORY.md scaffold if it does not yet exist.
|
|
89
|
+
*/
|
|
90
|
+
function scaffoldProjectDirs(projectRoot) {
|
|
91
|
+
const notesDir = `${projectRoot}/Notes`;
|
|
92
|
+
const memoryDir = `${projectRoot}/memory`;
|
|
93
|
+
const memoryFile = `${memoryDir}/MEMORY.md`;
|
|
94
|
+
mkdirSync(notesDir, { recursive: true });
|
|
95
|
+
mkdirSync(memoryDir, { recursive: true });
|
|
96
|
+
if (!existsSync(memoryFile)) writeFileSync(memoryFile, MEMORY_MD_SCAFFOLD, "utf8");
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Pad a string to a minimum width (left-aligned).
|
|
100
|
+
*/
|
|
101
|
+
function pad(str, width) {
|
|
102
|
+
const extra = width - stripAnsi(str).length;
|
|
103
|
+
return str + (extra > 0 ? " ".repeat(extra) : "");
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Strip ANSI escape sequences to measure visible string length.
|
|
107
|
+
*/
|
|
108
|
+
function stripAnsi(str) {
|
|
109
|
+
return str.replace(/\x1B\[[0-9;]*m/g, "");
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Render a simple columnar table.
|
|
113
|
+
*
|
|
114
|
+
* @param headers Column header strings
|
|
115
|
+
* @param rows Array of row arrays (each cell is a string, may include chalk sequences)
|
|
116
|
+
*/
|
|
117
|
+
function renderTable(headers, rows) {
|
|
118
|
+
const allRows = [headers, ...rows];
|
|
119
|
+
const widths = headers.map((_, colIdx) => Math.max(...allRows.map((row) => stripAnsi(row[colIdx] ?? "").length)));
|
|
120
|
+
const divider = dim(" " + widths.map((w) => "-".repeat(w)).join(" "));
|
|
121
|
+
const renderRow = (row, isHeader = false) => {
|
|
122
|
+
return " " + widths.map((w, i) => {
|
|
123
|
+
const cell = row[i] ?? "";
|
|
124
|
+
return isHeader ? pad(bold(cell), w + (cell.length - stripAnsi(cell).length)) : pad(cell, w + (cell.length - stripAnsi(cell).length));
|
|
125
|
+
}).join(" ");
|
|
126
|
+
};
|
|
127
|
+
const lines = [];
|
|
128
|
+
lines.push(renderRow(headers, true));
|
|
129
|
+
lines.push(divider);
|
|
130
|
+
for (const row of rows) lines.push(renderRow(row));
|
|
131
|
+
return lines.join("\n");
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Shorten an absolute path for display: replace home dir with ~,
|
|
135
|
+
* truncate from left if still longer than maxLen.
|
|
136
|
+
*/
|
|
137
|
+
function shortenPath(absolutePath, maxLen = 40) {
|
|
138
|
+
const home = homedir();
|
|
139
|
+
let p = absolutePath;
|
|
140
|
+
if (p.startsWith(home)) p = "~" + p.slice(home.length);
|
|
141
|
+
if (p.length <= maxLen) return p;
|
|
142
|
+
return "..." + p.slice(p.length - maxLen + 3);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Format an epoch milliseconds timestamp as YYYY-MM-DD.
|
|
146
|
+
*/
|
|
147
|
+
function fmtDate(epochMs) {
|
|
148
|
+
if (epochMs == null) return dim("—");
|
|
149
|
+
return new Date(epochMs).toISOString().slice(0, 10);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Return the current epoch milliseconds.
|
|
153
|
+
*/
|
|
154
|
+
function now() {
|
|
155
|
+
return Date.now();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
//#endregion
|
|
159
|
+
export { fmtDate as a, ok as c, scaffoldProjectDirs as d, shortenPath as f, warn as g, utils_exports as h, err as i, renderTable as l, slugify as m, dim as n, header as o, slugFromPath as p, encodeDir as r, now as s, bold as t, resolvePath as u };
|
|
160
|
+
//# sourceMappingURL=utils-DEWdIFQ0.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils-DEWdIFQ0.mjs","names":[],"sources":["../src/cli/utils.ts"],"sourcesContent":["/**\n * Shared utilities for CLI commands: formatting helpers, path encoding,\n * slug generation, and chalk colour wrappers.\n */\n\nimport chalk from \"chalk\";\nimport { resolve, basename, join } from \"node:path\";\nimport { mkdirSync, existsSync, writeFileSync, readdirSync, statSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\n\n// ---------------------------------------------------------------------------\n// Chalk colour helpers (thin wrappers so callers don't import chalk directly)\n// ---------------------------------------------------------------------------\n\nexport const ok = (msg: string) => chalk.green(msg);\nexport const warn = (msg: string) => chalk.yellow(msg);\nexport const err = (msg: string) => chalk.red(msg);\nexport const dim = (msg: string) => chalk.dim(msg);\nexport const bold = (msg: string) => chalk.bold(msg);\nexport const header = (msg: string) => chalk.bold.underline(msg);\n\n// ---------------------------------------------------------------------------\n// Path / slug helpers\n// ---------------------------------------------------------------------------\n\n/**\n * Convert any path string into a kebab-case slug.\n * \"/Users/foo/my-project\" → \"my-project\"\n * \"Some Cool Project\" → \"some-cool-project\"\n */\nexport function slugify(input: string): string {\n return input\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, \"-\") // non-alphanum runs → single hyphen\n .replace(/^-+|-+$/g, \"\"); // strip leading/trailing hyphens\n}\n\n/**\n * Derive a default project slug from the last component of a path.\n * \"/home/user/my-project\" → \"my-project\"\n */\nexport function slugFromPath(projectPath: string): string {\n return slugify(basename(projectPath));\n}\n\n/**\n * Encode an absolute path into Claude Code's encoded-dir format.\n *\n * Claude Code's actual encoding rules (reverse-engineered from real data):\n * - Every `/`, ` ` (space), `.` (dot), and `-` (literal hyphen) → single `-`\n * - The result therefore starts with `-` (from the leading `/`)\n *\n * This is a lossy encoding — space, dot, hyphen, and path-separator all\n * collapse to the same token. The decode is therefore ambiguous; prefer\n * {@link buildEncodedDirMap} from migrate.ts to get authoritative mappings.\n *\n * Examples:\n * \"/Users/foo/my-project\" → \"-Users-foo-my-project\"\n * \"/Users/foo/04 - Ablage\" → \"-Users-foo-04---Ablage\"\n * \"/Users/foo/.ssh\" → \"-Users-foo--ssh\"\n * \"/Users/foo/MDF-System.de\" → \"-Users-foo-MDF-System-de\"\n *\n * NOTE: For `project add`, prefer {@link findExistingEncodedDir} to look up\n * whether Claude Code has already created a directory for this path — that\n * avoids any mismatch between our encoding and Claude's.\n */\nexport function encodeDir(absolutePath: string): string {\n // Every `/`, space, dot, and hyphen → single `-`\n // The leading `/` produces the leading `-` that all encoded dirs start with.\n return absolutePath.replace(/[\\/\\s.\\-]/g, \"-\");\n}\n\n/**\n * Look up an absolute path in ~/.claude/projects/ to find the encoded-dir\n * name that Claude Code actually uses for it.\n *\n * This is more reliable than {@link encodeDir} because it reads the real\n * filesystem rather than re-implementing Claude's encoding algorithm.\n *\n * Returns the encoded-dir string (e.g. \"-Users-foo-my-project\") if a match\n * is found in ~/.claude/projects/, or `null` if not present.\n */\nexport function findExistingEncodedDir(absolutePath: string): string | null {\n const claudeProjectsDir = join(homedir(), \".claude\", \"projects\");\n if (!existsSync(claudeProjectsDir)) return null;\n\n // Build the expected encoded form to compare against directory names\n const expected = encodeDir(absolutePath);\n\n try {\n const entries = readdirSync(claudeProjectsDir);\n // Exact match (our encoding matches Claude's)\n if (entries.includes(expected)) return expected;\n\n // Fallback: scan all entries and compare the decoded path.\n // Import decodeEncodedDir lazily to avoid circular dependency.\n for (const entry of entries) {\n const full = join(claudeProjectsDir, entry);\n try {\n if (!statSync(full).isDirectory()) continue;\n } catch {\n continue;\n }\n // Simple heuristic decode: `--` → `-`, single `-` → `/`\n // (good enough for finding exact matches via the registry JSON)\n if (entry === expected) return entry;\n }\n } catch {\n // Unreadable directory — ignore\n }\n\n return null;\n}\n\n/**\n * Decode a Claude encoded-dir back to an approximate absolute path.\n *\n * NOTE: This decode is best-effort only — the encoding is lossy (space, dot,\n * literal hyphen, and path-separator all collapse to `-`). Prefer reading\n * original_path from session-registry.json via buildEncodedDirMap() in\n * src/registry/migrate.ts for authoritative decoding.\n *\n * \"-Users-foo-my-project\" → \"/Users/foo/my-project\"\n */\nexport function decodeDir(encodedDir: string): string {\n if (!encodedDir) return \"/\";\n // Encoded dirs start with `-` (from the leading `/`).\n // Best-effort: treat every `-` as `/`.\n return encodedDir.replace(/-/g, \"/\");\n}\n\n/**\n * Resolve a raw CLI path argument to an absolute path.\n */\nexport function resolvePath(rawPath: string): string {\n return resolve(rawPath);\n}\n\n// ---------------------------------------------------------------------------\n// Filesystem scaffolding\n// ---------------------------------------------------------------------------\n\nconst MEMORY_MD_SCAFFOLD = `# Memory\n\nProject-specific memory for PAI sessions.\nAdd persistent notes, reminders, and context here.\n`;\n\n/**\n * Ensure Notes/ and memory/ sub-directories exist under `projectRoot`.\n * Also creates a memory/MEMORY.md scaffold if it does not yet exist.\n */\nexport function scaffoldProjectDirs(projectRoot: string): void {\n const notesDir = `${projectRoot}/Notes`;\n const memoryDir = `${projectRoot}/memory`;\n const memoryFile = `${memoryDir}/MEMORY.md`;\n\n mkdirSync(notesDir, { recursive: true });\n mkdirSync(memoryDir, { recursive: true });\n\n if (!existsSync(memoryFile)) {\n writeFileSync(memoryFile, MEMORY_MD_SCAFFOLD, \"utf8\");\n }\n}\n\n// ---------------------------------------------------------------------------\n// Table rendering\n// ---------------------------------------------------------------------------\n\n/**\n * Pad a string to a minimum width (left-aligned).\n */\nexport function pad(str: string, width: number): string {\n const plain = stripAnsi(str);\n const extra = width - plain.length;\n return str + (extra > 0 ? \" \".repeat(extra) : \"\");\n}\n\n/**\n * Strip ANSI escape sequences to measure visible string length.\n */\nfunction stripAnsi(str: string): string {\n // eslint-disable-next-line no-control-regex\n return str.replace(/\\x1B\\[[0-9;]*m/g, \"\");\n}\n\n/**\n * Render a simple columnar table.\n *\n * @param headers Column header strings\n * @param rows Array of row arrays (each cell is a string, may include chalk sequences)\n */\nexport function renderTable(headers: string[], rows: string[][]): string {\n const allRows = [headers, ...rows];\n\n // Compute column widths\n const widths = headers.map((_, colIdx) =>\n Math.max(...allRows.map((row) => stripAnsi(row[colIdx] ?? \"\").length))\n );\n\n const divider = dim(\" \" + widths.map((w) => \"-\".repeat(w)).join(\" \"));\n const renderRow = (row: string[], isHeader = false) => {\n const cells = widths.map((w, i) => {\n const cell = row[i] ?? \"\";\n return isHeader ? pad(bold(cell), w + (cell.length - stripAnsi(cell).length)) : pad(cell, w + (cell.length - stripAnsi(cell).length));\n });\n return \" \" + cells.join(\" \");\n };\n\n const lines: string[] = [];\n lines.push(renderRow(headers, true));\n lines.push(divider);\n for (const row of rows) {\n lines.push(renderRow(row));\n }\n return lines.join(\"\\n\");\n}\n\n// ---------------------------------------------------------------------------\n// Date helpers\n// ---------------------------------------------------------------------------\n\n/**\n * Shorten an absolute path for display: replace home dir with ~,\n * truncate from left if still longer than maxLen.\n */\nexport function shortenPath(absolutePath: string, maxLen = 40): string {\n const home = homedir();\n let p = absolutePath;\n if (p.startsWith(home)) {\n p = \"~\" + p.slice(home.length);\n }\n if (p.length <= maxLen) return p;\n return \"...\" + p.slice(p.length - maxLen + 3);\n}\n\n/**\n * Format an epoch milliseconds timestamp as YYYY-MM-DD.\n */\nexport function fmtDate(epochMs: number | null | undefined): string {\n if (epochMs == null) return dim(\"—\");\n return new Date(epochMs).toISOString().slice(0, 10);\n}\n\n/**\n * Return the current epoch milliseconds.\n */\nexport function now(): number {\n return Date.now();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAcA,MAAa,MAAM,QAAgB,MAAM,MAAM,IAAI;AACnD,MAAa,QAAQ,QAAgB,MAAM,OAAO,IAAI;AACtD,MAAa,OAAO,QAAgB,MAAM,IAAI,IAAI;AAClD,MAAa,OAAO,QAAgB,MAAM,IAAI,IAAI;AAClD,MAAa,QAAQ,QAAgB,MAAM,KAAK,IAAI;AACpD,MAAa,UAAU,QAAgB,MAAM,KAAK,UAAU,IAAI;;;;;;AAWhE,SAAgB,QAAQ,OAAuB;AAC7C,QAAO,MACJ,aAAa,CACb,QAAQ,eAAe,IAAI,CAC3B,QAAQ,YAAY,GAAG;;;;;;AAO5B,SAAgB,aAAa,aAA6B;AACxD,QAAO,QAAQ,SAAS,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;;;AAwBvC,SAAgB,UAAU,cAA8B;AAGtD,QAAO,aAAa,QAAQ,cAAc,IAAI;;;;;AAiEhD,SAAgB,YAAY,SAAyB;AACnD,QAAO,QAAQ,QAAQ;;AAOzB,MAAM,qBAAqB;;;;;;;;;AAU3B,SAAgB,oBAAoB,aAA2B;CAC7D,MAAM,WAAW,GAAG,YAAY;CAChC,MAAM,YAAY,GAAG,YAAY;CACjC,MAAM,aAAa,GAAG,UAAU;AAEhC,WAAU,UAAU,EAAE,WAAW,MAAM,CAAC;AACxC,WAAU,WAAW,EAAE,WAAW,MAAM,CAAC;AAEzC,KAAI,CAAC,WAAW,WAAW,CACzB,eAAc,YAAY,oBAAoB,OAAO;;;;;AAWzD,SAAgB,I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/C,SAAgB,QAAQ,SAA4C;AAClE,KAAI,WAAW,KAAM,QAAO,IAAI,IAAI;AACpC,QAAO,IAAI,KAAK,QAAQ,CAAC,aAAa,CAAC,MAAM,GAAG,GAAG;;;;;AAMrD,SAAgB,MAAc;AAC5B,QAAO,KAAK,KAAK"}
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tekmidian/pai",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "PAI Knowledge OS — Personal AI Infrastructure with federated memory and project management",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.mts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
11
|
+
"import": "./dist/index.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"templates",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE",
|
|
19
|
+
"ARCHITECTURE.md",
|
|
20
|
+
"FEATURE.md"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"ai",
|
|
24
|
+
"knowledge-os",
|
|
25
|
+
"personal-ai",
|
|
26
|
+
"mcp",
|
|
27
|
+
"claude",
|
|
28
|
+
"memory",
|
|
29
|
+
"project-management",
|
|
30
|
+
"cli"
|
|
31
|
+
],
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=20.0.0"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/mnott/PAI.git"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/mnott/PAI",
|
|
40
|
+
"bin": {
|
|
41
|
+
"pai": "dist/cli/index.mjs",
|
|
42
|
+
"pai-mcp": "dist/mcp/index.mjs",
|
|
43
|
+
"pai-daemon": "dist/daemon/index.mjs",
|
|
44
|
+
"pai-daemon-mcp": "dist/daemon-mcp/index.mjs"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsdown",
|
|
48
|
+
"dev": "tsdown --watch",
|
|
49
|
+
"test": "vitest",
|
|
50
|
+
"lint": "tsc --noEmit",
|
|
51
|
+
"prepublishOnly": "bun run build"
|
|
52
|
+
},
|
|
53
|
+
"author": "Matthias Nott",
|
|
54
|
+
"license": "MIT",
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@huggingface/transformers": "^3.8.1",
|
|
57
|
+
"@modelcontextprotocol/sdk": "^1.26.0",
|
|
58
|
+
"better-sqlite3": "^12.6.2",
|
|
59
|
+
"chalk": "^5.6.2",
|
|
60
|
+
"commander": "^14.0.3",
|
|
61
|
+
"pg": "^8.13.3"
|
|
62
|
+
},
|
|
63
|
+
"devDependencies": {
|
|
64
|
+
"@types/better-sqlite3": "^7.6.13",
|
|
65
|
+
"@types/node": "^25.3.0",
|
|
66
|
+
"@types/pg": "^8.11.11",
|
|
67
|
+
"tsdown": "^0.20.3",
|
|
68
|
+
"tsx": "^4.21.0",
|
|
69
|
+
"typescript": "^5.9.3",
|
|
70
|
+
"vitest": "^4.0.18"
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# PAI Configuration Templates
|
|
2
|
+
|
|
3
|
+
This directory contains example configuration files for personalizing your PAI Knowledge OS setup.
|
|
4
|
+
|
|
5
|
+
## Files
|
|
6
|
+
|
|
7
|
+
### `agent-prefs.example.md`
|
|
8
|
+
|
|
9
|
+
**Template for**: `~/.config/pai/agent-prefs.md`
|
|
10
|
+
|
|
11
|
+
Contains your personal preferences for PAI agents:
|
|
12
|
+
- Identity and role information
|
|
13
|
+
- Directory search restrictions
|
|
14
|
+
- Project-code directory mappings
|
|
15
|
+
- Notification preferences (WhatsApp, ntfy.sh)
|
|
16
|
+
- Voice configuration for TTS
|
|
17
|
+
- Git commit rules
|
|
18
|
+
- Code quality standards
|
|
19
|
+
- Language and framework preferences
|
|
20
|
+
- Workflow customization options
|
|
21
|
+
|
|
22
|
+
**Size**: 362 lines, comprehensive with examples
|
|
23
|
+
|
|
24
|
+
**Setup**:
|
|
25
|
+
```bash
|
|
26
|
+
cp agent-prefs.example.md ~/.config/pai/agent-prefs.md
|
|
27
|
+
# Then customize with your settings
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### `voices.example.json`
|
|
31
|
+
|
|
32
|
+
**Template for**: `~/.config/pai/voices.json`
|
|
33
|
+
|
|
34
|
+
Configures voice output for different agents and channels:
|
|
35
|
+
- TTS system selection (Kokoro local or ElevenLabs cloud)
|
|
36
|
+
- Voice assignments per agent type
|
|
37
|
+
- WhatsApp voice note configuration
|
|
38
|
+
- Local speaker output settings
|
|
39
|
+
- Named personas for easy reference
|
|
40
|
+
- Multiple profiles (professional, casual, focus)
|
|
41
|
+
- Complete examples for different setups
|
|
42
|
+
- Troubleshooting guide
|
|
43
|
+
|
|
44
|
+
**Size**: 251 lines, valid JSON with comments
|
|
45
|
+
|
|
46
|
+
**Setup**:
|
|
47
|
+
```bash
|
|
48
|
+
cp voices.example.json ~/.config/pai/voices.json
|
|
49
|
+
# Then customize voice selections
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Quick Start
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
# Create config directory if needed
|
|
56
|
+
mkdir -p ~/.config/pai
|
|
57
|
+
|
|
58
|
+
# Copy templates
|
|
59
|
+
cp agent-prefs.example.md ~/.config/pai/agent-prefs.md
|
|
60
|
+
cp voices.example.json ~/.config/pai/voices.json
|
|
61
|
+
|
|
62
|
+
# Edit with your preferences
|
|
63
|
+
nano ~/.config/pai/agent-prefs.md
|
|
64
|
+
nano ~/.config/pai/voices.json
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Key Features
|
|
68
|
+
|
|
69
|
+
### Agent Preferences
|
|
70
|
+
|
|
71
|
+
- **Directory Restrictions**: Prevent expensive searches in large directories
|
|
72
|
+
- **Project Mappings**: Automate end-session commits to the right directories
|
|
73
|
+
- **Workflow Customization**: Tailor agent behavior to your development style
|
|
74
|
+
- **Voice and Notifications**: Configure how agents communicate with you
|
|
75
|
+
|
|
76
|
+
### Voice Configuration
|
|
77
|
+
|
|
78
|
+
- **Kokoro TTS**: Free, local, no API key required (~160MB download once)
|
|
79
|
+
- **ElevenLabs**: Premium quality voices, requires API key
|
|
80
|
+
- **Agent-Specific Voices**: Different voices for different agent types
|
|
81
|
+
- **Profiles**: Switch between professional/casual/focus modes
|
|
82
|
+
|
|
83
|
+
## Privacy and Version Control
|
|
84
|
+
|
|
85
|
+
These files contain personal preferences and should **NOT** be committed:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
# Add to your .gitignore
|
|
89
|
+
echo "~/.config/pai/" >> ~/.gitignore
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Keep these files private:
|
|
93
|
+
```bash
|
|
94
|
+
chmod 600 ~/.config/pai/agent-prefs.md
|
|
95
|
+
chmod 600 ~/.config/pai/voices.json
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Integration with PAI
|
|
99
|
+
|
|
100
|
+
The PAI daemon automatically:
|
|
101
|
+
- Reads `~/.config/pai/agent-prefs.md` on startup
|
|
102
|
+
- Loads voice configuration from `~/.config/pai/voices.json`
|
|
103
|
+
- Uses these settings for all agent operations
|
|
104
|
+
- Falls back to sensible defaults if files are missing
|
|
105
|
+
|
|
106
|
+
**No restart needed**: Changes to these files take effect immediately on next operation.
|
|
107
|
+
|
|
108
|
+
## Customization Examples
|
|
109
|
+
|
|
110
|
+
### Minimal Setup (Just Defaults)
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
# Copy templates
|
|
114
|
+
cp agent-prefs.example.md ~/.config/pai/agent-prefs.md
|
|
115
|
+
cp voices.example.json ~/.config/pai/voices.json
|
|
116
|
+
|
|
117
|
+
# Use defaults as-is, customize later as needed
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Engineer-Heavy Workflow
|
|
121
|
+
|
|
122
|
+
Focus on technical tools and code quality:
|
|
123
|
+
- Set default language to TypeScript
|
|
124
|
+
- Configure strict testing requirements
|
|
125
|
+
- Map multiple code directories
|
|
126
|
+
- Use technical-sounding voices
|
|
127
|
+
|
|
128
|
+
### Research-Heavy Workflow
|
|
129
|
+
|
|
130
|
+
Focus on information gathering:
|
|
131
|
+
- Configure ntfy.sh notifications for task completion
|
|
132
|
+
- Set longer timeouts for research agents
|
|
133
|
+
- Map knowledge base directories
|
|
134
|
+
- Use calm, analytical voices
|
|
135
|
+
|
|
136
|
+
## Environment Variables
|
|
137
|
+
|
|
138
|
+
Some settings can also be configured via environment variables:
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
export ELEVENLABS_API_KEY="sk-..." # ElevenLabs API key
|
|
142
|
+
export NTFY_TOPIC="my-private-topic" # ntfy.sh topic
|
|
143
|
+
export PAI_CONFIG_DIR="~/.config/pai" # Custom config directory
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Troubleshooting
|
|
147
|
+
|
|
148
|
+
**Q: My preferences aren't being used**
|
|
149
|
+
- Check file exists: `ls ~/.config/pai/agent-prefs.md`
|
|
150
|
+
- Check JSON is valid: `python3 -m json.tool ~/.config/pai/voices.json`
|
|
151
|
+
- Check permissions: `ls -l ~/.config/pai/`
|
|
152
|
+
|
|
153
|
+
**Q: Voice not working**
|
|
154
|
+
- Verify ElevenLabs API key if using ElevenLabs
|
|
155
|
+
- Check internet connection if using cloud TTS
|
|
156
|
+
- Try local Kokoro TTS as fallback
|
|
157
|
+
- See troubleshooting section in `voices.example.json`
|
|
158
|
+
|
|
159
|
+
**Q: Configuration not loading**
|
|
160
|
+
- Daemon reads from `~/.config/pai/` (not `~/.claude/`)
|
|
161
|
+
- Create directory if missing: `mkdir -p ~/.config/pai`
|
|
162
|
+
- Copy templates to this location
|
|
163
|
+
|
|
164
|
+
## Further Customization
|
|
165
|
+
|
|
166
|
+
Once you've customized the basic templates, you can:
|
|
167
|
+
|
|
168
|
+
1. **Create project-specific overrides** in project `.claude.json` files
|
|
169
|
+
2. **Add team standards** to agent-prefs.md under "Team Standards"
|
|
170
|
+
3. **Define CI/CD rules** in git commit section
|
|
171
|
+
4. **Experiment with voice profiles** for different working modes
|
|
172
|
+
|
|
173
|
+
## Related Documentation
|
|
174
|
+
|
|
175
|
+
- PAI Knowledge OS: `~/projects/PAI/README.md`
|
|
176
|
+
- Configuration reference: `~/.claude.json` (Claude Code configuration)
|
|
177
|
+
- PAI daemon docs: `~/projects/PAI/src/daemon/`
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
**Template Version**: 1.0
|