Add CLI package, config file support, and workspace setup

Add cli/ package with initial scaffolding. Add config-schema to shared
package for typed configuration. Add server config-file loader for
paperclip.config.ts support. Register cli in pnpm workspace. Add
.paperclip/ and .pnpm-store/ to gitignore. Minor Companies page fix.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Forgotten
2026-02-17 13:39:47 -06:00
parent 0975907121
commit 5306142542
28 changed files with 1091 additions and 7 deletions

View File

@@ -0,0 +1,33 @@
import { readConfig, configExists, resolveConfigPath } from "../config/store.js";
import type { CheckResult } from "./index.js";
export function configCheck(configPath?: string): CheckResult {
const filePath = resolveConfigPath(configPath);
if (!configExists(configPath)) {
return {
name: "Config file",
status: "fail",
message: `Config file not found at ${filePath}`,
canRepair: false,
repairHint: "Run `paperclip onboard` to create one",
};
}
try {
readConfig(configPath);
return {
name: "Config file",
status: "pass",
message: `Valid config at ${filePath}`,
};
} catch (err) {
return {
name: "Config file",
status: "fail",
message: `Invalid config: ${err instanceof Error ? err.message : String(err)}`,
canRepair: false,
repairHint: "Run `paperclip onboard` to recreate",
};
}
}

View File

@@ -0,0 +1,57 @@
import fs from "node:fs";
import path from "node:path";
import type { PaperclipConfig } from "../config/schema.js";
import type { CheckResult } from "./index.js";
export async function databaseCheck(config: PaperclipConfig): Promise<CheckResult> {
if (config.database.mode === "postgres") {
if (!config.database.connectionString) {
return {
name: "Database",
status: "fail",
message: "PostgreSQL mode selected but no connection string configured",
canRepair: false,
repairHint: "Run `paperclip configure --section database`",
};
}
try {
const { createDb } = await import("@paperclip/db");
const db = createDb(config.database.connectionString);
await db.execute("SELECT 1");
return {
name: "Database",
status: "pass",
message: "PostgreSQL connection successful",
};
} catch (err) {
return {
name: "Database",
status: "fail",
message: `Cannot connect to PostgreSQL: ${err instanceof Error ? err.message : String(err)}`,
canRepair: false,
repairHint: "Check your connection string and ensure PostgreSQL is running",
};
}
}
// PGlite mode — check data dir
const dataDir = path.resolve(config.database.pgliteDataDir);
if (!fs.existsSync(dataDir)) {
return {
name: "Database",
status: "warn",
message: `PGlite data directory does not exist: ${dataDir}`,
canRepair: true,
repair: () => {
fs.mkdirSync(dataDir, { recursive: true });
},
};
}
return {
name: "Database",
status: "pass",
message: `PGlite data directory exists: ${dataDir}`,
};
}

14
cli/src/checks/index.ts Normal file
View File

@@ -0,0 +1,14 @@
export interface CheckResult {
name: string;
status: "pass" | "warn" | "fail";
message: string;
canRepair?: boolean;
repair?: () => void | Promise<void>;
repairHint?: string;
}
export { configCheck } from "./config-check.js";
export { databaseCheck } from "./database-check.js";
export { llmCheck } from "./llm-check.js";
export { logCheck } from "./log-check.js";
export { portCheck } from "./port-check.js";

View File

@@ -0,0 +1,86 @@
import type { PaperclipConfig } from "../config/schema.js";
import type { CheckResult } from "./index.js";
export async function llmCheck(config: PaperclipConfig): Promise<CheckResult> {
if (!config.llm) {
return {
name: "LLM provider",
status: "warn",
message: "No LLM provider configured",
canRepair: false,
repairHint: "Run `paperclip configure --section llm` to set one up",
};
}
if (!config.llm.apiKey) {
return {
name: "LLM provider",
status: "warn",
message: `${config.llm.provider} configured but no API key set`,
canRepair: false,
repairHint: "Run `paperclip configure --section llm`",
};
}
try {
if (config.llm.provider === "claude") {
const res = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"x-api-key": config.llm.apiKey,
"anthropic-version": "2023-06-01",
"content-type": "application/json",
},
body: JSON.stringify({
model: "claude-sonnet-4-5-20250929",
max_tokens: 1,
messages: [{ role: "user", content: "hi" }],
}),
});
if (res.ok || res.status === 400) {
return { name: "LLM provider", status: "pass", message: "Claude API key is valid" };
}
if (res.status === 401) {
return {
name: "LLM provider",
status: "fail",
message: "Claude API key is invalid (401)",
canRepair: false,
repairHint: "Run `paperclip configure --section llm`",
};
}
return {
name: "LLM provider",
status: "warn",
message: `Claude API returned status ${res.status}`,
};
} else {
const res = await fetch("https://api.openai.com/v1/models", {
headers: { Authorization: `Bearer ${config.llm.apiKey}` },
});
if (res.ok) {
return { name: "LLM provider", status: "pass", message: "OpenAI API key is valid" };
}
if (res.status === 401) {
return {
name: "LLM provider",
status: "fail",
message: "OpenAI API key is invalid (401)",
canRepair: false,
repairHint: "Run `paperclip configure --section llm`",
};
}
return {
name: "LLM provider",
status: "warn",
message: `OpenAI API returned status ${res.status}`,
};
}
} catch {
return {
name: "LLM provider",
status: "warn",
message: "Could not reach API to validate key",
};
}
}

View File

@@ -0,0 +1,37 @@
import fs from "node:fs";
import path from "node:path";
import type { PaperclipConfig } from "../config/schema.js";
import type { CheckResult } from "./index.js";
export function logCheck(config: PaperclipConfig): CheckResult {
const logDir = path.resolve(config.logging.logDir);
if (!fs.existsSync(logDir)) {
return {
name: "Log directory",
status: "warn",
message: `Log directory does not exist: ${logDir}`,
canRepair: true,
repair: () => {
fs.mkdirSync(logDir, { recursive: true });
},
};
}
try {
fs.accessSync(logDir, fs.constants.W_OK);
return {
name: "Log directory",
status: "pass",
message: `Log directory is writable: ${logDir}`,
};
} catch {
return {
name: "Log directory",
status: "fail",
message: `Log directory is not writable: ${logDir}`,
canRepair: false,
repairHint: "Check file permissions on the log directory",
};
}
}

View File

@@ -0,0 +1,24 @@
import type { PaperclipConfig } from "../config/schema.js";
import { checkPort } from "../utils/net.js";
import type { CheckResult } from "./index.js";
export async function portCheck(config: PaperclipConfig): Promise<CheckResult> {
const port = config.server.port;
const result = await checkPort(port);
if (result.available) {
return {
name: "Server port",
status: "pass",
message: `Port ${port} is available`,
};
}
return {
name: "Server port",
status: "warn",
message: result.error ?? `Port ${port} is not available`,
canRepair: false,
repairHint: `Check what's using port ${port} with: lsof -i :${port}`,
};
}

View File

@@ -0,0 +1,113 @@
import * as p from "@clack/prompts";
import pc from "picocolors";
import { readConfig, writeConfig, configExists } from "../config/store.js";
import type { PaperclipConfig } from "../config/schema.js";
import { promptDatabase } from "../prompts/database.js";
import { promptLlm } from "../prompts/llm.js";
import { promptLogging } from "../prompts/logging.js";
import { promptServer } from "../prompts/server.js";
type Section = "llm" | "database" | "logging" | "server";
const SECTION_LABELS: Record<Section, string> = {
llm: "LLM Provider",
database: "Database",
logging: "Logging",
server: "Server",
};
export async function configure(opts: {
config?: string;
section?: string;
}): Promise<void> {
p.intro(pc.bgCyan(pc.black(" paperclip configure ")));
if (!configExists(opts.config)) {
p.log.error("No config file found. Run `paperclip onboard` first.");
p.outro("");
return;
}
const config = readConfig(opts.config);
if (!config) {
p.log.error("Could not read config file. Run `paperclip onboard` to recreate.");
p.outro("");
return;
}
let section: Section | undefined = opts.section as Section | undefined;
if (section && !SECTION_LABELS[section]) {
p.log.error(`Unknown section: ${section}. Choose from: ${Object.keys(SECTION_LABELS).join(", ")}`);
p.outro("");
return;
}
// Section selection loop
let continueLoop = true;
while (continueLoop) {
if (!section) {
const choice = await p.select({
message: "Which section do you want to configure?",
options: Object.entries(SECTION_LABELS).map(([value, label]) => ({
value: value as Section,
label,
})),
});
if (p.isCancel(choice)) {
p.cancel("Configuration cancelled.");
return;
}
section = choice;
}
p.log.step(pc.bold(SECTION_LABELS[section]));
switch (section) {
case "database":
config.database = await promptDatabase();
break;
case "llm": {
const llm = await promptLlm();
if (llm) {
config.llm = llm;
} else {
delete config.llm;
}
break;
}
case "logging":
config.logging = await promptLogging();
break;
case "server":
config.server = await promptServer();
break;
}
config.$meta.updatedAt = new Date().toISOString();
config.$meta.source = "configure";
writeConfig(config, opts.config);
p.log.success(`${SECTION_LABELS[section]} configuration updated.`);
// If section was provided via CLI flag, don't loop
if (opts.section) {
continueLoop = false;
} else {
const another = await p.confirm({
message: "Configure another section?",
initialValue: false,
});
if (p.isCancel(another) || !another) {
continueLoop = false;
} else {
section = undefined; // Reset to show picker again
}
}
}
p.outro("Configuration saved.");
}

120
cli/src/commands/doctor.ts Normal file
View File

@@ -0,0 +1,120 @@
import * as p from "@clack/prompts";
import pc from "picocolors";
import { readConfig } from "../config/store.js";
import {
configCheck,
databaseCheck,
llmCheck,
logCheck,
portCheck,
type CheckResult,
} from "../checks/index.js";
const STATUS_ICON = {
pass: pc.green("✓"),
warn: pc.yellow("!"),
fail: pc.red("✗"),
} as const;
export async function doctor(opts: {
config?: string;
repair?: boolean;
yes?: boolean;
}): Promise<void> {
p.intro(pc.bgCyan(pc.black(" paperclip doctor ")));
const results: CheckResult[] = [];
// 1. Config check (must pass before others)
const cfgResult = configCheck(opts.config);
results.push(cfgResult);
printResult(cfgResult);
if (cfgResult.status === "fail") {
printSummary(results);
return;
}
const config = readConfig(opts.config)!;
// 2. Database check
const dbResult = await databaseCheck(config);
results.push(dbResult);
printResult(dbResult);
await maybeRepair(dbResult, opts);
// 3. LLM check
const llmResult = await llmCheck(config);
results.push(llmResult);
printResult(llmResult);
// 4. Log directory check
const logResult = logCheck(config);
results.push(logResult);
printResult(logResult);
await maybeRepair(logResult, opts);
// 5. Port check
const portResult = await portCheck(config);
results.push(portResult);
printResult(portResult);
// Summary
printSummary(results);
}
function printResult(result: CheckResult): void {
const icon = STATUS_ICON[result.status];
p.log.message(`${icon} ${pc.bold(result.name)}: ${result.message}`);
if (result.status !== "pass" && result.repairHint) {
p.log.message(` ${pc.dim(result.repairHint)}`);
}
}
async function maybeRepair(
result: CheckResult,
opts: { repair?: boolean; yes?: boolean },
): Promise<void> {
if (result.status === "pass" || !result.canRepair || !result.repair) return;
if (!opts.repair) return;
let shouldRepair = opts.yes;
if (!shouldRepair) {
const answer = await p.confirm({
message: `Repair "${result.name}"?`,
initialValue: true,
});
if (p.isCancel(answer)) return;
shouldRepair = answer;
}
if (shouldRepair) {
try {
await result.repair();
p.log.success(`Repaired: ${result.name}`);
} catch (err) {
p.log.error(`Repair failed: ${err instanceof Error ? err.message : String(err)}`);
}
}
}
function printSummary(results: CheckResult[]): void {
const passed = results.filter((r) => r.status === "pass").length;
const warned = results.filter((r) => r.status === "warn").length;
const failed = results.filter((r) => r.status === "fail").length;
const parts: string[] = [];
parts.push(pc.green(`${passed} passed`));
if (warned) parts.push(pc.yellow(`${warned} warnings`));
if (failed) parts.push(pc.red(`${failed} failed`));
p.note(parts.join(", "), "Summary");
if (failed > 0) {
p.outro(pc.red("Some checks failed. Fix the issues above and re-run doctor."));
} else if (warned > 0) {
p.outro(pc.yellow("All critical checks passed with some warnings."));
} else {
p.outro(pc.green("All checks passed!"));
}
}

127
cli/src/commands/onboard.ts Normal file
View File

@@ -0,0 +1,127 @@
import * as p from "@clack/prompts";
import pc from "picocolors";
import { configExists, readConfig, writeConfig } from "../config/store.js";
import type { PaperclipConfig } from "../config/schema.js";
import { promptDatabase } from "../prompts/database.js";
import { promptLlm } from "../prompts/llm.js";
import { promptLogging } from "../prompts/logging.js";
import { promptServer } from "../prompts/server.js";
export async function onboard(opts: { config?: string }): Promise<void> {
p.intro(pc.bgCyan(pc.black(" paperclip onboard ")));
// Check for existing config
if (configExists(opts.config)) {
const existing = readConfig(opts.config);
if (existing) {
const overwrite = await p.confirm({
message: "A config file already exists. Overwrite it?",
initialValue: false,
});
if (p.isCancel(overwrite) || !overwrite) {
p.cancel("Keeping existing configuration.");
return;
}
}
}
// Database
p.log.step(pc.bold("Database"));
const database = await promptDatabase();
if (database.mode === "postgres" && database.connectionString) {
const s = p.spinner();
s.start("Testing database connection...");
try {
const { createDb } = await import("@paperclip/db");
const db = createDb(database.connectionString);
await db.execute("SELECT 1");
s.stop("Database connection successful");
} catch (err) {
s.stop(pc.yellow("Could not connect to database — you can fix this later with `paperclip doctor`"));
}
}
// LLM
p.log.step(pc.bold("LLM Provider"));
const llm = await promptLlm();
if (llm?.apiKey) {
const s = p.spinner();
s.start("Validating API key...");
try {
if (llm.provider === "claude") {
const res = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"x-api-key": llm.apiKey,
"anthropic-version": "2023-06-01",
"content-type": "application/json",
},
body: JSON.stringify({
model: "claude-sonnet-4-5-20250929",
max_tokens: 1,
messages: [{ role: "user", content: "hi" }],
}),
});
if (res.ok || res.status === 400) {
s.stop("API key is valid");
} else if (res.status === 401) {
s.stop(pc.yellow("API key appears invalid — you can update it later"));
} else {
s.stop(pc.yellow("Could not validate API key — continuing anyway"));
}
} else {
const res = await fetch("https://api.openai.com/v1/models", {
headers: { Authorization: `Bearer ${llm.apiKey}` },
});
if (res.ok) {
s.stop("API key is valid");
} else if (res.status === 401) {
s.stop(pc.yellow("API key appears invalid — you can update it later"));
} else {
s.stop(pc.yellow("Could not validate API key — continuing anyway"));
}
}
} catch {
s.stop(pc.yellow("Could not reach API — continuing anyway"));
}
}
// Logging
p.log.step(pc.bold("Logging"));
const logging = await promptLogging();
// Server
p.log.step(pc.bold("Server"));
const server = await promptServer();
// Assemble and write config
const config: PaperclipConfig = {
$meta: {
version: 1,
updatedAt: new Date().toISOString(),
source: "onboard",
},
...(llm && { llm }),
database,
logging,
server,
};
writeConfig(config, opts.config);
p.note(
[
`Database: ${database.mode}`,
llm ? `LLM: ${llm.provider}` : "LLM: not configured",
`Logging: ${logging.mode}${logging.logDir}`,
`Server: port ${server.port}`,
].join("\n"),
"Configuration saved",
);
p.log.info(`Run ${pc.cyan("pnpm paperclip doctor")} to verify your setup.`);
p.outro("You're all set!");
}

14
cli/src/config/schema.ts Normal file
View File

@@ -0,0 +1,14 @@
export {
paperclipConfigSchema,
configMetaSchema,
llmConfigSchema,
databaseConfigSchema,
loggingConfigSchema,
serverConfigSchema,
type PaperclipConfig,
type LlmConfig,
type DatabaseConfig,
type LoggingConfig,
type ServerConfig,
type ConfigMeta,
} from "@paperclip/shared";

42
cli/src/config/store.ts Normal file
View File

@@ -0,0 +1,42 @@
import fs from "node:fs";
import path from "node:path";
import { paperclipConfigSchema, type PaperclipConfig } from "./schema.js";
const DEFAULT_CONFIG_PATH = ".paperclip/config.json";
export function resolveConfigPath(overridePath?: string): string {
if (overridePath) return path.resolve(overridePath);
if (process.env.PAPERCLIP_CONFIG) return path.resolve(process.env.PAPERCLIP_CONFIG);
return path.resolve(process.cwd(), DEFAULT_CONFIG_PATH);
}
export function readConfig(configPath?: string): PaperclipConfig | null {
const filePath = resolveConfigPath(configPath);
if (!fs.existsSync(filePath)) return null;
const raw = JSON.parse(fs.readFileSync(filePath, "utf-8"));
return paperclipConfigSchema.parse(raw);
}
export function writeConfig(
config: PaperclipConfig,
configPath?: string,
): void {
const filePath = resolveConfigPath(configPath);
const dir = path.dirname(filePath);
fs.mkdirSync(dir, { recursive: true });
// Backup existing config before overwriting
if (fs.existsSync(filePath)) {
const backupPath = filePath + ".backup";
fs.copyFileSync(filePath, backupPath);
fs.chmodSync(backupPath, 0o600);
}
fs.writeFileSync(filePath, JSON.stringify(config, null, 2) + "\n", {
mode: 0o600,
});
}
export function configExists(configPath?: string): boolean {
return fs.existsSync(resolveConfigPath(configPath));
}

36
cli/src/index.ts Normal file
View File

@@ -0,0 +1,36 @@
#!/usr/bin/env node
import { Command } from "commander";
import { onboard } from "./commands/onboard.js";
import { doctor } from "./commands/doctor.js";
import { configure } from "./commands/configure.js";
const program = new Command();
program
.name("paperclip")
.description("Paperclip CLI — setup, diagnose, and configure your instance")
.version("0.0.1");
program
.command("onboard")
.description("Interactive first-run setup wizard")
.option("-c, --config <path>", "Path to config file")
.action(onboard);
program
.command("doctor")
.description("Run diagnostic checks on your Paperclip setup")
.option("-c, --config <path>", "Path to config file")
.option("--repair", "Attempt to repair issues automatically")
.alias("--fix")
.option("-y, --yes", "Skip repair confirmation prompts")
.action(doctor);
program
.command("configure")
.description("Update configuration sections")
.option("-c, --config <path>", "Path to config file")
.option("-s, --section <section>", "Section to configure (llm, database, logging, server)")
.action(configure);
program.parse();

View File

@@ -0,0 +1,48 @@
import * as p from "@clack/prompts";
import type { DatabaseConfig } from "../config/schema.js";
export async function promptDatabase(): Promise<DatabaseConfig> {
const mode = await p.select({
message: "Database mode",
options: [
{ value: "pglite" as const, label: "PGlite (embedded, no setup needed)", hint: "recommended" },
{ value: "postgres" as const, label: "PostgreSQL (external server)" },
],
});
if (p.isCancel(mode)) {
p.cancel("Setup cancelled.");
process.exit(0);
}
if (mode === "postgres") {
const connectionString = await p.text({
message: "PostgreSQL connection string",
placeholder: "postgres://user:pass@localhost:5432/paperclip",
validate: (val) => {
if (!val) return "Connection string is required for PostgreSQL mode";
if (!val.startsWith("postgres")) return "Must be a postgres:// or postgresql:// URL";
},
});
if (p.isCancel(connectionString)) {
p.cancel("Setup cancelled.");
process.exit(0);
}
return { mode: "postgres", connectionString, pgliteDataDir: "./data/pglite" };
}
const pgliteDataDir = await p.text({
message: "PGlite data directory",
defaultValue: "./data/pglite",
placeholder: "./data/pglite",
});
if (p.isCancel(pgliteDataDir)) {
p.cancel("Setup cancelled.");
process.exit(0);
}
return { mode: "pglite", pgliteDataDir: pgliteDataDir || "./data/pglite" };
}

43
cli/src/prompts/llm.ts Normal file
View File

@@ -0,0 +1,43 @@
import * as p from "@clack/prompts";
import type { LlmConfig } from "../config/schema.js";
export async function promptLlm(): Promise<LlmConfig | undefined> {
const configureLlm = await p.confirm({
message: "Configure an LLM provider now?",
initialValue: false,
});
if (p.isCancel(configureLlm)) {
p.cancel("Setup cancelled.");
process.exit(0);
}
if (!configureLlm) return undefined;
const provider = await p.select({
message: "LLM provider",
options: [
{ value: "claude" as const, label: "Claude (Anthropic)" },
{ value: "openai" as const, label: "OpenAI" },
],
});
if (p.isCancel(provider)) {
p.cancel("Setup cancelled.");
process.exit(0);
}
const apiKey = await p.password({
message: `${provider === "claude" ? "Anthropic" : "OpenAI"} API key`,
validate: (val) => {
if (!val) return "API key is required";
},
});
if (p.isCancel(apiKey)) {
p.cancel("Setup cancelled.");
process.exit(0);
}
return { provider, apiKey };
}

View File

@@ -0,0 +1,35 @@
import * as p from "@clack/prompts";
import type { LoggingConfig } from "../config/schema.js";
export async function promptLogging(): Promise<LoggingConfig> {
const mode = await p.select({
message: "Logging mode",
options: [
{ value: "file" as const, label: "File-based logging", hint: "recommended" },
{ value: "cloud" as const, label: "Cloud logging", hint: "coming soon" },
],
});
if (p.isCancel(mode)) {
p.cancel("Setup cancelled.");
process.exit(0);
}
if (mode === "file") {
const logDir = await p.text({
message: "Log directory",
defaultValue: "./data/logs",
placeholder: "./data/logs",
});
if (p.isCancel(logDir)) {
p.cancel("Setup cancelled.");
process.exit(0);
}
return { mode: "file", logDir: logDir || "./data/logs" };
}
p.note("Cloud logging is coming soon. Using file-based logging for now.");
return { mode: "file", logDir: "./data/logs" };
}

35
cli/src/prompts/server.ts Normal file
View File

@@ -0,0 +1,35 @@
import * as p from "@clack/prompts";
import type { ServerConfig } from "../config/schema.js";
export async function promptServer(): Promise<ServerConfig> {
const portStr = await p.text({
message: "Server port",
defaultValue: "3100",
placeholder: "3100",
validate: (val) => {
const n = Number(val);
if (isNaN(n) || n < 1 || n > 65535 || !Number.isInteger(n)) {
return "Must be an integer between 1 and 65535";
}
},
});
if (p.isCancel(portStr)) {
p.cancel("Setup cancelled.");
process.exit(0);
}
const port = Number(portStr) || 3100;
const serveUi = await p.confirm({
message: "Serve the UI from the server?",
initialValue: false,
});
if (p.isCancel(serveUi)) {
p.cancel("Setup cancelled.");
process.exit(0);
}
return { port, serveUi };
}

18
cli/src/utils/net.ts Normal file
View File

@@ -0,0 +1,18 @@
import net from "node:net";
export function checkPort(port: number): Promise<{ available: boolean; error?: string }> {
return new Promise((resolve) => {
const server = net.createServer();
server.once("error", (err: NodeJS.ErrnoException) => {
if (err.code === "EADDRINUSE") {
resolve({ available: false, error: `Port ${port} is already in use` });
} else {
resolve({ available: false, error: err.message });
}
});
server.once("listening", () => {
server.close(() => resolve({ available: true }));
});
server.listen(port, "127.0.0.1");
});
}