mirror of
https://github.com/different-ai/openwork.git
synced 2026-11-28 23:32:20 +08:00
* chore: delete dead code, unused deps and orphaned assets Remove code that nothing reaches, verified against origin/dev with knip (new knip.jsonc) plus manual reference checks, including hidden dirs. Typecheck passes for app, server, desktop, den-api, den-web, landing, gateway and telemetry; the app builds; app boot and desktop tests pass. App (apps/app): - React-port leftovers and orphaned modules: actions store/provider, global sync and SDK providers, unused settings, connections and session modules, test-only helpers and their scripts. - Dead exports: unused utils, Advanced sections, migration deferral helpers, compactSession, the deprecated signUpEmail stub. - The unregistered OpenAI image settings state and the never-read microsandboxCreateSandbox flag. - The menu-overlay renderer (native context menus replaced it in #4650), unused i18n keys and 192 unused grain-art images. - Deps: @ai-sdk/react, @radix-ui/colors, @radix-ui/react-use-controllable-state, remark-breaks, remark-gfm. Desktop (apps/desktop) and types: - 16 renderer-unreachable IPC handlers and their types and re-exports (some wrote files), plus 7 unused browser and microphone channels. - menu-overlay preload, unused dev icons, electron-devtools-installer. Server (apps/server): - Unused exports, @types/minimatch, @openwork/headless-threads and the opencode-chrome-devtools devDep with its module declaration (the bundled openwork-chrome-devtools plugin replaced it in #4423). Den: - Orphaned landing components, the den-api plugin-system barrel and desktop-policy-role-assignments re-export, a test-only cache hook, test helpers and evidence scripts left without tests, and unused deps (grammy, drizzle-orm in gateway, zod in telemetry). Repo: - apps/ui-demo, Tauri migration scripts, the broken dev-web-local and dev-den-local scripts, the den-controller stub and DEN_CONTROLLER_PORT, one-off scripts, HANDOFF.md, committed tsbuildinfo files and tracked evals/results output, the Remote MCP Apps docs. - Add knip.jsonc and make scripts/find-unused.sh a thin knip wrapper that uses pnpm. Signed-off-by: Benjamin Shafii <ben@prologe.io> * chore(app): keep only the artifact helpers production uses lib/artifacts.ts keeps ArtifactType, getArtifactType and useOpenArtifactPath; the rest had no production caller and its tests were already removed. Signed-off-by: Benjamin Shafii <ben@prologe.io> --------- Signed-off-by: Benjamin Shafii <ben@prologe.io>
286 lines
9.2 KiB
JavaScript
286 lines
9.2 KiB
JavaScript
import { spawn } from "node:child_process"
|
|
import net from "node:net"
|
|
import os from "node:os"
|
|
import path from "node:path"
|
|
import process from "node:process"
|
|
import { fileURLToPath } from "node:url"
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
const rootDir = path.resolve(__dirname, "..")
|
|
|
|
// Load committed local-dev defaults. Real env vars take precedence (loadEnvFile
|
|
// never overwrites variables that are already set).
|
|
try {
|
|
process.loadEnvFile(path.join(rootDir, ".env.dev"))
|
|
} catch {
|
|
// .env.dev is optional; the inline fallbacks below still apply.
|
|
}
|
|
const composeFile = path.join(rootDir, "packaging", "docker", "docker-compose.web-local.yml")
|
|
const composeProject = "openwork-den-local"
|
|
|
|
const apiPort = process.env.DEN_API_PORT?.trim() || "8788"
|
|
const gatewayPort = process.env.GATEWAY_PORT ?? process.env.INFERENCE_PORT?.trim() ?? "8791"
|
|
const webPort = process.env.DEN_WEB_PORT?.trim() || "3005"
|
|
const appPort = process.env.OPENWORK_APP_PORT?.trim() || process.env.PORT?.trim() || "5173"
|
|
const extraAppPorts = (process.env.OPENWORK_EXTRA_APP_PORTS?.trim() || "5174")
|
|
.split(",")
|
|
.map((value) => value.trim())
|
|
.filter(Boolean)
|
|
const databaseUrl = process.env.DATABASE_URL?.trim() || "mysql://root:password@127.0.0.1:3306/openwork_den"
|
|
const databaseRedisUrl = process.env.DATABASE_REDIS_URL?.trim() || "redis://127.0.0.1:6379"
|
|
const dbEncryptionKey =
|
|
process.env.DEN_DB_ENCRYPTION_KEY?.trim() ||
|
|
"local-dev-db-encryption-key-please-change-1234567890"
|
|
|
|
function detectWebOrigins() {
|
|
const origins = new Set([
|
|
`http://localhost:${webPort}`,
|
|
`http://127.0.0.1:${webPort}`,
|
|
`http://0.0.0.0:${webPort}`,
|
|
`http://localhost:${appPort}`,
|
|
`http://127.0.0.1:${appPort}`,
|
|
])
|
|
|
|
for (const port of extraAppPorts) {
|
|
origins.add(`http://localhost:${port}`)
|
|
origins.add(`http://127.0.0.1:${port}`)
|
|
}
|
|
|
|
for (const entries of Object.values(os.networkInterfaces())) {
|
|
for (const entry of entries || []) {
|
|
if (!entry || entry.internal || entry.family !== "IPv4") {
|
|
continue
|
|
}
|
|
|
|
origins.add(`http://${entry.address}:${webPort}`)
|
|
}
|
|
}
|
|
|
|
return Array.from(origins).join(",")
|
|
}
|
|
|
|
function parseUrlEndpoint(value, defaultPort) {
|
|
const parsed = new URL(value)
|
|
return {
|
|
host: parsed.hostname,
|
|
port: Number(parsed.port || defaultPort),
|
|
}
|
|
}
|
|
|
|
function canReachTcp(host, port) {
|
|
return new Promise((resolve) => {
|
|
const socket = net.createConnection({ host, port })
|
|
|
|
const finalize = (result) => {
|
|
socket.destroy()
|
|
resolve(result)
|
|
}
|
|
|
|
socket.setTimeout(1500)
|
|
socket.once("connect", () => finalize(true))
|
|
socket.once("error", () => finalize(false))
|
|
socket.once("timeout", () => finalize(false))
|
|
})
|
|
}
|
|
|
|
function canListenOnPort(port) {
|
|
return new Promise((resolve) => {
|
|
const server = net.createServer()
|
|
|
|
const finalize = (result) => {
|
|
server.close(() => resolve(result))
|
|
}
|
|
|
|
server.once("error", () => resolve(false))
|
|
server.once("listening", () => finalize(true))
|
|
server.listen(port, "0.0.0.0")
|
|
})
|
|
}
|
|
|
|
function run(command, args, options = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
const child = spawn(command, args, {
|
|
cwd: rootDir,
|
|
stdio: "inherit",
|
|
...options,
|
|
shell: false,
|
|
})
|
|
|
|
child.once("error", reject)
|
|
child.once("exit", (code, signal) => {
|
|
if (code === 0) {
|
|
resolve()
|
|
return
|
|
}
|
|
|
|
const detail = signal ? `signal ${signal}` : `exit code ${code ?? 1}`
|
|
reject(new Error(`${command} ${args.join(" ")} failed with ${detail}`))
|
|
})
|
|
})
|
|
}
|
|
|
|
const startedDockerServices = new Set()
|
|
let turboChild = null
|
|
let cleaningUp = false
|
|
|
|
function stopTurboChild() {
|
|
if (!turboChild || turboChild.exitCode !== null) {
|
|
return Promise.resolve()
|
|
}
|
|
|
|
return new Promise((resolve) => {
|
|
turboChild.once("exit", resolve)
|
|
|
|
try {
|
|
if (process.platform !== "win32") {
|
|
process.kill(-turboChild.pid, "SIGINT")
|
|
} else {
|
|
turboChild.kill("SIGINT")
|
|
}
|
|
} catch {
|
|
turboChild.kill("SIGINT")
|
|
}
|
|
})
|
|
}
|
|
|
|
async function cleanup(exitCode = 0) {
|
|
if (cleaningUp) {
|
|
return
|
|
}
|
|
|
|
cleaningUp = true
|
|
|
|
await stopTurboChild()
|
|
|
|
if (startedDockerServices.size > 0) {
|
|
const services = Array.from(startedDockerServices)
|
|
await run("docker", ["compose", "-p", composeProject, "-f", composeFile, "stop", ...services], {
|
|
stdio: "inherit",
|
|
}).catch(() => {})
|
|
await run("docker", ["compose", "-p", composeProject, "-f", composeFile, "rm", "-f", ...services], {
|
|
stdio: "inherit",
|
|
}).catch(() => {})
|
|
}
|
|
|
|
process.exit(exitCode)
|
|
}
|
|
|
|
for (const signal of ["SIGINT", "SIGTERM"]) {
|
|
process.on(signal, () => {
|
|
void cleanup(0)
|
|
})
|
|
}
|
|
|
|
async function main() {
|
|
let databaseEndpoint
|
|
try {
|
|
databaseEndpoint = parseUrlEndpoint(databaseUrl, "3306")
|
|
if (new URL(databaseUrl).protocol !== "mysql:") throw new Error()
|
|
} catch {
|
|
throw new Error("DATABASE_URL must be a valid local MySQL URL (value withheld).")
|
|
}
|
|
if (!["localhost", "127.0.0.1", "[::1]", "::1"].includes(databaseEndpoint.host)) {
|
|
throw new Error("dev:web-local only migrates loopback MySQL databases. Remote databases require a separately reviewed migration procedure.")
|
|
}
|
|
if (databaseEndpoint.host === "[::1]") databaseEndpoint.host = "::1"
|
|
for (const [name, port] of [["den-web", webPort], ["den-api", apiPort], ["gateway", gatewayPort]]) {
|
|
const available = await canListenOnPort(Number(port))
|
|
if (!available) {
|
|
throw new Error(`${name} local port ${port} is already in use. Stop the existing process or rerun with a different port env override.`)
|
|
}
|
|
}
|
|
|
|
const { host, port } = databaseEndpoint
|
|
const mysqlAvailable = await canReachTcp(host, port)
|
|
|
|
if (!mysqlAvailable) {
|
|
if (!(host === "127.0.0.1" || host === "localhost")) {
|
|
throw new Error(`MySQL at ${host}:${port} is not reachable, and auto-start only supports localhost`)
|
|
}
|
|
|
|
console.log(`[den] MySQL not reachable at ${host}:${port}; starting Docker MySQL...`)
|
|
await run("docker", ["compose", "-p", composeProject, "-f", composeFile, "up", "-d", "--wait", "mysql"])
|
|
startedDockerServices.add("mysql")
|
|
} else {
|
|
console.log(`[den] Using existing MySQL at ${host}:${port}`)
|
|
}
|
|
|
|
const redis = parseUrlEndpoint(databaseRedisUrl, "6379")
|
|
const redisAvailable = await canReachTcp(redis.host, redis.port)
|
|
if (!redisAvailable) {
|
|
if (!(redis.host === "127.0.0.1" || redis.host === "localhost")) {
|
|
throw new Error(`Redis at ${redis.host}:${redis.port} is not reachable, and auto-start only supports localhost`)
|
|
}
|
|
|
|
console.log(`[den] Redis not reachable at ${redis.host}:${redis.port}; starting Docker Redis...`)
|
|
await run("docker", ["compose", "-p", composeProject, "-f", composeFile, "up", "-d", "--wait", "redis"])
|
|
startedDockerServices.add("redis")
|
|
} else {
|
|
console.log(`[den] Using existing Redis at ${redis.host}:${redis.port}`)
|
|
}
|
|
|
|
console.log("[den] Building Den database package...")
|
|
await run("pnpm", ["--filter", "@openwork-ee/den-db", "build"], {
|
|
env: { ...process.env, DATABASE_URL: databaseUrl },
|
|
})
|
|
console.log("[den] Applying ordered local migrations before starting services...")
|
|
await run("pnpm", ["--filter", "@openwork-ee/den-db", "db:migrate:local"], {
|
|
env: {
|
|
...process.env,
|
|
DATABASE_URL: databaseUrl,
|
|
},
|
|
})
|
|
|
|
const webOrigins = detectWebOrigins()
|
|
console.log(`[den] Allowed local web origins: ${webOrigins}`)
|
|
|
|
turboChild = spawn(
|
|
"pnpm",
|
|
[
|
|
"exec",
|
|
"turbo",
|
|
"run",
|
|
"dev:local",
|
|
"--output-logs=full",
|
|
"--filter=@openwork-ee/den-api",
|
|
"--filter=@openwork-ee/gateway",
|
|
"--filter=@openwork-ee/den-web",
|
|
],
|
|
{
|
|
cwd: rootDir,
|
|
stdio: "inherit",
|
|
detached: process.platform !== "win32",
|
|
env: {
|
|
...process.env,
|
|
DATABASE_URL: databaseUrl,
|
|
DATABASE_REDIS_URL: databaseRedisUrl,
|
|
DEN_DB_ENCRYPTION_KEY: dbEncryptionKey,
|
|
BETTER_AUTH_URL: process.env.BETTER_AUTH_URL?.trim() || `http://localhost:${webPort}`,
|
|
DEN_BASE_URL: process.env.DEN_BASE_URL?.trim() || `http://localhost:${webPort}`,
|
|
DEN_MCP_RESOURCE_URL: process.env.DEN_MCP_RESOURCE_URL?.trim() || `http://127.0.0.1:${apiPort}/mcp`,
|
|
DEN_BETTER_AUTH_TRUSTED_ORIGINS: process.env.DEN_BETTER_AUTH_TRUSTED_ORIGINS?.trim() || webOrigins,
|
|
CORS_ORIGINS: process.env.CORS_ORIGINS?.trim() || webOrigins,
|
|
DEN_API_PORT: apiPort,
|
|
GATEWAY_PORT: gatewayPort,
|
|
GATEWAY_PROXY_BASE_URL: process.env.GATEWAY_PROXY_BASE_URL ?? process.env.INFERENCE_PROXY_BASE_URL?.trim() ?? `http://127.0.0.1:${gatewayPort}`,
|
|
DEN_WEB_PORT: webPort,
|
|
DEN_API_BASE: process.env.DEN_API_BASE?.trim() || `http://127.0.0.1:${apiPort}`,
|
|
DEN_API_PUBLIC_URL: process.env.DEN_API_PUBLIC_URL?.trim() || `http://localhost:${apiPort}`,
|
|
DEN_AUTH_ORIGIN: process.env.DEN_AUTH_ORIGIN?.trim() || `http://localhost:${webPort}`,
|
|
DEN_AUTH_FALLBACK_BASE: process.env.DEN_AUTH_FALLBACK_BASE?.trim() || `http://127.0.0.1:${apiPort}`,
|
|
},
|
|
shell: false,
|
|
},
|
|
)
|
|
|
|
turboChild.once("exit", (code, signal) => {
|
|
const exitCode = code ?? (signal ? 1 : 0)
|
|
void cleanup(exitCode)
|
|
})
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error(error instanceof Error ? error.message : String(error))
|
|
void cleanup(1)
|
|
})
|