Skip to content

feat: npx agentree — CLI entry point with opencode auto-detection #3

@StatPan

Description

@StatPan

Problem or motivation

Agentree currently requires cloning the repo and running pnpm dev. Users (especially those already running opencode) should be able to start Agentree with a single command:

npx agentree

Proposed solution

New file: src/server/cli.ts

CLI entry point (#!/usr/bin/env node) with the following startup sequence:

  1. Parse CLI args: --port (default 3001), --opencode-url, --help
  2. Auto-detect opencode (in priority order):
    • --opencode-url flag → use directly
    • OPENCODE_API_URL env → use directly
    • GET http://localhost:6543/global/health (2s timeout) → if 200, use
    • GET http://localhost:4096/global/health (2s timeout) → if 200, use
    • Parse ~/.config/opencode/opencode.json for server.port → health-check
    • Print friendly error with install instructions → process.exit(1)
  3. Resolve DB path: DB_PATH env → ~/.agentree/agentree.db (auto-creates dir)
  4. Set process.env.OPENCODE_API_URL, DB_PATH, CORS_ORIGIN
  5. Dynamic imports after env is set (critical — db/index.ts and opencode/client.ts read env at module-load time)
  6. Run migrations, start server with static file serving, print startup banner

Modified: src/server/app.ts

Add AppOptions = { staticDir?: string } parameter to createApp(). When staticDir is provided, add serveStatic for /assets/* and SPA fallback for all non-/api routes. No change to existing dev behavior.

Modified: package.json

  • Remove "private": true
  • Add "bin": { "agentree": "./dist/server/cli.js" }
  • Add "files": ["dist/server/", "dist/client/"]
  • Update build script to also copy drizzle/dist/server/drizzle/
  • Add "prepublishOnly": "pnpm run build"

New file: scripts/copy-migrations.mjs

import { cpSync } from 'node:fs'
cpSync('drizzle', 'dist/server/drizzle', { recursive: true })

DB path change

Context Path
pnpm dev ./agentree.db (unchanged)
npx agentree ~/.agentree/agentree.db
override DB_PATH=./path npx agentree

README

Add npx agentree quickstart section near the top.

Alternatives considered

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions