* docs(M002): context, requirements, and roadmap * feat: port TTSR and blob/artifact storage from oh-my-pi Phase 1 — TTSR (Time Traveling Stream Rules): - TtsrManager: regex-based stream monitoring with scope filtering, repeat gating, and buffer isolation (picomatch replaces Bun.Glob) - Rule loader: scans ~/.gsd/agent/rules/*.md and .gsd/rules/*.md with YAML frontmatter parsing; project rules override global - TTSR extension: wires into pi event lifecycle (session_start, turn_start, message_update, turn_end, agent_end) to abort on match and inject violation as system reminder via sendMessage - Interrupt template for rule violation injection Phase 2 — Blob/Artifact Storage: - BlobStore: content-addressed storage at ~/.gsd/agent/blobs/ using Node crypto (sha256), sync I/O, automatic deduplication - ArtifactManager: session-scoped sequential artifact files stored alongside session JSONL (lazy dir creation, resume-safe ID scan) - Session manager integration: prepareForPersistence externalizes images ≥1KB to blob store before JSONL write; resolveBlobRefs rehydrates on session load; truncates strings >500KB - Bash tool artifact spill: uses ArtifactManager instead of temp files when available, includes artifact:// references in output Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: harden blob store, TTSR manager, and dep classification - Validate SHA-256 hex format in BlobStore.get/has/parseBlobRef to prevent path traversal via crafted blob references - Cap TTSR per-stream buffers at 512KB to prevent unbounded memory growth - Move picomatch from devDependencies to dependencies (runtime import) - Warn on invalid regex in TTSR rule conditions instead of silent skip - Remove .gsd/ planning files that were force-added past .gitignore - Add trailing newline to ttsr-interrupt.md Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * test: add tests for blob store, artifact manager, TTSR manager, and rule loader 55 tests covering: - BlobStore put/get/has, idempotency, path traversal rejection - parseBlobRef/isBlobRef validation, externalize/resolve round-trips - ArtifactManager sequential IDs, lazy dir creation, session resume - TtsrManager rule matching, scope filtering, buffer isolation, repeat gating, buffer size cap, injection persistence - Rule loader frontmatter parsing, directory scanning, merge logic Also fixes BlobStore constructor to avoid TS parameter property syntax (incompatible with Node's strip-only TypeScript mode). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
246 lines
7.9 KiB
TypeScript
246 lines
7.9 KiB
TypeScript
import { existsSync, readFileSync } from "fs";
|
|
import { homedir } from "os";
|
|
import { dirname, join, resolve } from "path";
|
|
import { fileURLToPath } from "url";
|
|
|
|
// =============================================================================
|
|
// Package Detection
|
|
// =============================================================================
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = dirname(__filename);
|
|
|
|
/**
|
|
* Detect if we're running as a Bun compiled binary.
|
|
* Bun binaries have import.meta.url containing "$bunfs", "~BUN", or "%7EBUN" (Bun's virtual filesystem path)
|
|
*/
|
|
export const isBunBinary =
|
|
import.meta.url.includes("$bunfs") || import.meta.url.includes("~BUN") || import.meta.url.includes("%7EBUN");
|
|
|
|
/** Detect if Bun is the runtime (compiled binary or bun run) */
|
|
export const isBunRuntime = !!process.versions.bun;
|
|
|
|
// =============================================================================
|
|
// Install Method Detection
|
|
// =============================================================================
|
|
|
|
export type InstallMethod = "bun-binary" | "npm" | "pnpm" | "yarn" | "bun" | "unknown";
|
|
|
|
export function detectInstallMethod(): InstallMethod {
|
|
if (isBunBinary) {
|
|
return "bun-binary";
|
|
}
|
|
|
|
const resolvedPath = `${__dirname}\0${process.execPath || ""}`.toLowerCase();
|
|
|
|
if (resolvedPath.includes("/pnpm/") || resolvedPath.includes("/.pnpm/") || resolvedPath.includes("\\pnpm\\")) {
|
|
return "pnpm";
|
|
}
|
|
if (resolvedPath.includes("/yarn/") || resolvedPath.includes("/.yarn/") || resolvedPath.includes("\\yarn\\")) {
|
|
return "yarn";
|
|
}
|
|
if (isBunRuntime) {
|
|
return "bun";
|
|
}
|
|
if (resolvedPath.includes("/npm/") || resolvedPath.includes("/node_modules/") || resolvedPath.includes("\\npm\\")) {
|
|
return "npm";
|
|
}
|
|
|
|
return "unknown";
|
|
}
|
|
|
|
export function getUpdateInstruction(packageName: string): string {
|
|
const method = detectInstallMethod();
|
|
switch (method) {
|
|
case "bun-binary":
|
|
return `Download from: https://github.com/badlogic/pi-mono/releases/latest`;
|
|
case "pnpm":
|
|
return `Run: pnpm install -g ${packageName}`;
|
|
case "yarn":
|
|
return `Run: yarn global add ${packageName}`;
|
|
case "bun":
|
|
return `Run: bun install -g ${packageName}`;
|
|
case "npm":
|
|
return `Run: npm install -g ${packageName}`;
|
|
default:
|
|
return `Run: npm install -g ${packageName}`;
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Package Asset Paths (shipped with executable)
|
|
// =============================================================================
|
|
|
|
/**
|
|
* Get the base directory for resolving package assets (themes, package.json, README.md, CHANGELOG.md).
|
|
* - For Bun binary: returns the directory containing the executable
|
|
* - For Node.js (dist/): returns __dirname (the dist/ directory)
|
|
* - For tsx (src/): returns parent directory (the package root)
|
|
*/
|
|
export function getPackageDir(): string {
|
|
// Allow override via environment variable (useful for Nix/Guix where store paths tokenize poorly)
|
|
const envDir = process.env.PI_PACKAGE_DIR;
|
|
if (envDir) {
|
|
if (envDir === "~") return homedir();
|
|
if (envDir.startsWith("~/")) return homedir() + envDir.slice(1);
|
|
return envDir;
|
|
}
|
|
|
|
if (isBunBinary) {
|
|
// Bun binary: process.execPath points to the compiled executable
|
|
return dirname(process.execPath);
|
|
}
|
|
// Node.js: walk up from __dirname until we find package.json
|
|
let dir = __dirname;
|
|
while (dir !== dirname(dir)) {
|
|
if (existsSync(join(dir, "package.json"))) {
|
|
return dir;
|
|
}
|
|
dir = dirname(dir);
|
|
}
|
|
// Fallback (shouldn't happen)
|
|
return __dirname;
|
|
}
|
|
|
|
/**
|
|
* Get path to built-in themes directory (shipped with package)
|
|
* - For Bun binary: theme/ next to executable
|
|
* - For Node.js (dist/): dist/modes/interactive/theme/
|
|
* - For tsx (src/): src/modes/interactive/theme/
|
|
*/
|
|
export function getThemesDir(): string {
|
|
if (isBunBinary) {
|
|
return join(dirname(process.execPath), "theme");
|
|
}
|
|
// Theme is in modes/interactive/theme/ relative to src/ or dist/
|
|
const packageDir = getPackageDir();
|
|
const srcOrDist = existsSync(join(packageDir, "src")) ? "src" : "dist";
|
|
return join(packageDir, srcOrDist, "modes", "interactive", "theme");
|
|
}
|
|
|
|
/**
|
|
* Get path to HTML export template directory (shipped with package)
|
|
* - For Bun binary: export-html/ next to executable
|
|
* - For Node.js (dist/): dist/core/export-html/
|
|
* - For tsx (src/): src/core/export-html/
|
|
*/
|
|
export function getExportTemplateDir(): string {
|
|
if (isBunBinary) {
|
|
return join(dirname(process.execPath), "export-html");
|
|
}
|
|
const packageDir = getPackageDir();
|
|
const srcOrDist = existsSync(join(packageDir, "src")) ? "src" : "dist";
|
|
return join(packageDir, srcOrDist, "core", "export-html");
|
|
}
|
|
|
|
/** Get path to package.json */
|
|
export function getPackageJsonPath(): string {
|
|
return join(getPackageDir(), "package.json");
|
|
}
|
|
|
|
/** Get path to README.md */
|
|
export function getReadmePath(): string {
|
|
return resolve(join(getPackageDir(), "README.md"));
|
|
}
|
|
|
|
/** Get path to docs directory */
|
|
export function getDocsPath(): string {
|
|
return resolve(join(getPackageDir(), "docs"));
|
|
}
|
|
|
|
/** Get path to examples directory */
|
|
export function getExamplesPath(): string {
|
|
return resolve(join(getPackageDir(), "examples"));
|
|
}
|
|
|
|
/** Get path to CHANGELOG.md */
|
|
export function getChangelogPath(): string {
|
|
return resolve(join(getPackageDir(), "CHANGELOG.md"));
|
|
}
|
|
|
|
// =============================================================================
|
|
// App Config (from package.json piConfig)
|
|
// =============================================================================
|
|
|
|
const pkg = JSON.parse(readFileSync(getPackageJsonPath(), "utf-8"));
|
|
|
|
export const APP_NAME: string = pkg.piConfig?.name || "pi";
|
|
export const CONFIG_DIR_NAME: string = pkg.piConfig?.configDir || ".pi";
|
|
export const VERSION: string = pkg.version;
|
|
|
|
// e.g., PI_CODING_AGENT_DIR or TAU_CODING_AGENT_DIR
|
|
export const ENV_AGENT_DIR = `${APP_NAME.toUpperCase()}_CODING_AGENT_DIR`;
|
|
|
|
const DEFAULT_SHARE_VIEWER_URL = "https://pi.dev/session/";
|
|
|
|
/** Get the share viewer URL for a gist ID */
|
|
export function getShareViewerUrl(gistId: string): string {
|
|
const baseUrl = process.env.PI_SHARE_VIEWER_URL || DEFAULT_SHARE_VIEWER_URL;
|
|
return `${baseUrl}#${gistId}`;
|
|
}
|
|
|
|
// =============================================================================
|
|
// User Config Paths (~/.pi/agent/*)
|
|
// =============================================================================
|
|
|
|
/** Get the agent config directory (e.g., ~/.pi/agent/) */
|
|
export function getAgentDir(): string {
|
|
const envDir = process.env[ENV_AGENT_DIR];
|
|
if (envDir) {
|
|
// Expand tilde to home directory
|
|
if (envDir === "~") return homedir();
|
|
if (envDir.startsWith("~/")) return homedir() + envDir.slice(1);
|
|
return envDir;
|
|
}
|
|
return join(homedir(), CONFIG_DIR_NAME, "agent");
|
|
}
|
|
|
|
/** Get path to user's custom themes directory */
|
|
export function getCustomThemesDir(): string {
|
|
return join(getAgentDir(), "themes");
|
|
}
|
|
|
|
/** Get path to models.json */
|
|
export function getModelsPath(): string {
|
|
return join(getAgentDir(), "models.json");
|
|
}
|
|
|
|
/** Get path to auth.json */
|
|
export function getAuthPath(): string {
|
|
return join(getAgentDir(), "auth.json");
|
|
}
|
|
|
|
/** Get path to settings.json */
|
|
export function getSettingsPath(): string {
|
|
return join(getAgentDir(), "settings.json");
|
|
}
|
|
|
|
/** Get path to tools directory */
|
|
export function getToolsDir(): string {
|
|
return join(getAgentDir(), "tools");
|
|
}
|
|
|
|
/** Get path to managed binaries directory (fd, rg) */
|
|
export function getBinDir(): string {
|
|
return join(getAgentDir(), "bin");
|
|
}
|
|
|
|
/** Get path to prompt templates directory */
|
|
export function getPromptsDir(): string {
|
|
return join(getAgentDir(), "prompts");
|
|
}
|
|
|
|
/** Get path to sessions directory */
|
|
export function getSessionsDir(): string {
|
|
return join(getAgentDir(), "sessions");
|
|
}
|
|
|
|
/** Get path to content-addressed blob store directory */
|
|
export function getBlobsDir(): string {
|
|
return join(getAgentDir(), "blobs");
|
|
}
|
|
|
|
/** Get path to debug log file */
|
|
export function getDebugLogPath(): string {
|
|
return join(getAgentDir(), `${APP_NAME}-debug.log`);
|
|
}
|