feat: roolbac

This commit is contained in:
Gab
2026-03-24 15:06:34 +11:00
parent 7c015708cb
commit ff2d13015d
23 changed files with 1847 additions and 203 deletions

View File

@@ -0,0 +1,194 @@
#!/usr/bin/env bun
import { $ } from "bun"
import fs from "fs"
import path from "path"
import { fileURLToPath } from "url"
import solidPlugin from "@opentui/solid/bun-plugin"
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const dir = path.resolve(__dirname, "..")
process.chdir(dir)
import { Script } from "@opencode-ai/script"
import pkg from "../package.json"
// tfcode version
const TFCODE_VERSION = pkg.version
const TFCODE_NAME = "tfcode"
// Fetch models snapshot
const modelsUrl = process.env.TFCODE_MODELS_URL || "https://models.dev"
const modelsData = process.env.MODELS_DEV_API_JSON
? await Bun.file(process.env.MODELS_DEV_API_JSON).text()
: await fetch(`${modelsUrl}/api.json`).then((x) => x.text())
await Bun.write(
path.join(dir, "src/provider/models-snapshot.ts"),
`// Auto-generated by build.ts - do not edit\nexport const snapshot = ${modelsData} as const\n`,
)
console.log("Generated models-snapshot.ts")
// Load migrations
const migrationDirs = (
await fs.promises.readdir(path.join(dir, "migration"), {
withFileTypes: true,
})
)
.filter((entry) => entry.isDirectory() && /^\d{4}\d{2}\d{2}\d{2}\d{2}\d{2}/.test(entry.name))
.map((entry) => entry.name)
.sort()
const migrations = await Promise.all(
migrationDirs.map(async (name) => {
const file = path.join(dir, "migration", name, "migration.sql")
const sql = await Bun.file(file).text()
const match = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/.exec(name)
const timestamp = match
? Date.UTC(
Number(match[1]),
Number(match[2]) - 1,
Number(match[3]),
Number(match[4]),
Number(match[5]),
Number(match[6]),
)
: 0
return { sql, timestamp, name }
}),
)
console.log(`Loaded ${migrations.length} migrations`)
const singleFlag = process.argv.includes("--single")
const baselineFlag = process.argv.includes("--baseline")
const skipInstall = process.argv.includes("--skip-install")
const allTargets: {
os: string
arch: "arm64" | "x64"
abi?: "musl"
avx2?: false
}[] = [
{ os: "linux", arch: "arm64" },
{ os: "linux", arch: "x64" },
{ os: "linux", arch: "x64", avx2: false },
{ os: "linux", arch: "arm64", abi: "musl" },
{ os: "linux", arch: "x64", abi: "musl" },
{ os: "linux", arch: "x64", abi: "musl", avx2: false },
{ os: "darwin", arch: "arm64" },
{ os: "darwin", arch: "x64" },
{ os: "darwin", arch: "x64", avx2: false },
{ os: "win32", arch: "arm64" },
{ os: "win32", arch: "x64" },
{ os: "win32", arch: "x64", avx2: false },
]
const targets = singleFlag
? allTargets.filter((item) => {
if (item.os !== process.platform || item.arch !== process.arch) return false
if (item.avx2 === false) return baselineFlag
if (item.abi !== undefined) return false
return true
})
: allTargets
await $`rm -rf dist`
const binaries: Record<string, string> = {}
if (!skipInstall) {
await $`bun install --os="*" --cpu="*" @opentui/core@${pkg.dependencies["@opentui/core"]}`
await $`bun install --os="*" --cpu="*" @parcel/watcher@${pkg.dependencies["@parcel/watcher"]}`
}
for (const item of targets) {
const name = [
TFCODE_NAME,
item.os === "win32" ? "windows" : item.os,
item.arch,
item.avx2 === false ? "baseline" : undefined,
item.abi === undefined ? undefined : item.abi,
]
.filter(Boolean)
.join("-")
console.log(`Building ${name}`)
await $`mkdir -p dist/${name}/bin`
const localPath = path.resolve(dir, "node_modules/@opentui/core/parser.worker.js")
const rootPath = path.resolve(dir, "../../node_modules/@opentui/core/parser.worker.js")
const parserWorker = fs.realpathSync(fs.existsSync(localPath) ? localPath : rootPath)
const workerPath = "./src/cli/cmd/tui/worker.ts"
const bunfsRoot = item.os === "win32" ? "B:/~BUN/root/" : "/$bunfs/root/"
const workerRelativePath = path.relative(dir, parserWorker).replaceAll("\\", "/")
await Bun.build({
conditions: ["browser"],
tsconfig: "./tsconfig.json",
plugins: [solidPlugin],
compile: {
autoloadBunfig: false,
autoloadDotenv: false,
autoloadTsconfig: true,
autoloadPackageJson: true,
target: name.replace(TFCODE_NAME, "bun") as any,
outfile: `dist/${name}/bin/tfcode`,
execArgv: [`--user-agent=tfcode/${TFCODE_VERSION}`, "--use-system-ca", "--"],
windows: {},
},
entrypoints: ["./src/index.ts", parserWorker, workerPath],
define: {
OPENCODE_VERSION: `'${TFCODE_VERSION}'`,
OPENCODE_MIGRATIONS: JSON.stringify(migrations),
OTUI_TREE_SITTER_WORKER_PATH: bunfsRoot + workerRelativePath,
OPENCODE_WORKER_PATH: workerPath,
OPENCODE_CHANNEL: `'${Script.channel}'`,
OPENCODE_LIBC: item.os === "linux" ? `'${item.abi ?? "glibc"}'` : "",
},
})
// Smoke test
if (item.os === process.platform && item.arch === process.arch && !item.abi) {
const binaryPath = `dist/${name}/bin/tfcode`
console.log(`Running smoke test: ${binaryPath} --version`)
try {
const versionOutput = await $`${binaryPath} --version`.text()
console.log(`Smoke test passed: ${versionOutput.trim()}`)
} catch (e) {
console.error(`Smoke test failed for ${name}:`, e)
process.exit(1)
}
}
await $`rm -rf ./dist/${name}/bin/tui`
await Bun.file(`dist/${name}/package.json`).write(
JSON.stringify(
{
name: `@toothfairyai/${name}`,
version: TFCODE_VERSION,
os: [item.os],
cpu: [item.arch],
},
null,
2,
),
)
binaries[name] = TFCODE_VERSION
}
// Package for release
if (Script.release || process.env.TFCODE_RELEASE) {
console.log("Packaging for release...")
for (const key of Object.keys(binaries)) {
if (key.includes("linux")) {
await $`tar -czf ../../${key}.tar.gz *`.cwd(`dist/${key}/bin`)
} else {
await $`zip -r ../../${key}.zip *`.cwd(`dist/${key}/bin`)
}
}
console.log("Binaries packaged. Upload to Gitea releases manually or use publish.ts")
}
export { binaries, TFCODE_VERSION }

View File

@@ -0,0 +1,216 @@
#!/usr/bin/env node
import fs from "fs"
import path from "path"
import os from "os"
import { spawnSync } from "child_process"
const GITEA_HOST = process.env.TFCODE_GITEA_HOST || "gitea.toothfairyai.com"
const GITEA_REPO = process.env.TFCODE_GITEA_REPO || "ToothFairyAI/tfcode"
function detectPlatform() {
let platform
switch (os.platform()) {
case "darwin": platform = "darwin"; break
case "linux": platform = "linux"; break
case "win32": platform = "windows"; break
default: platform = os.platform()
}
let arch
switch (os.arch()) {
case "x64": arch = "x64"; break
case "arm64": arch = "arm64"; break
default: arch = os.arch()
}
// Check for AVX2 on x64
let needsBaseline = false
if (arch === "x64" && (platform === "linux" || platform === "darwin")) {
try {
if (platform === "linux") {
const cpuinfo = fs.readFileSync("/proc/cpuinfo", "utf8")
needsBaseline = !cpuinfo.toLowerCase().includes("avx2")
} else if (platform === "darwin") {
const result = spawnSync("sysctl", ["-n", "hw.optional.avx2_0"], { encoding: "utf8" })
needsBaseline = result.stdout.trim() !== "1"
}
} catch {}
}
// Check for musl on Linux
let abi = ""
if (platform === "linux") {
try {
if (fs.existsSync("/etc/alpine-release")) {
abi = "musl"
} else {
const result = spawnSync("ldd", ["--version"], { encoding: "utf8" })
if ((result.stdout + result.stderr).toLowerCase().includes("musl")) {
abi = "musl"
}
}
} catch {}
}
return { platform, arch, needsBaseline, abi }
}
async function getVersion() {
try {
const res = await fetch(`https://${GITEA_HOST}/api/v1/repos/${GITEA_REPO}/releases/latest`)
const data = await res.json()
return data.tag_name?.replace(/^v/, "") || "1.0.0"
} catch {
return "1.0.0"
}
}
async function downloadBinary() {
const { platform, arch, needsBaseline, abi } = detectPlatform()
const version = await getVersion()
// Build filename
let target = `tfcode-${platform}-${arch}`
if (needsBaseline) target += "-baseline"
if (abi) target += `-${abi}`
const ext = platform === "linux" ? ".tar.gz" : ".zip"
const filename = `${target}${ext}`
const url = `https://${GITEA_HOST}/${GITEA_REPO}/releases/download/v${version}/${filename}`
console.log(`Downloading tfcode v${version} for ${target}...`)
// Download
const binDir = path.join(__dirname, "bin")
if (!fs.existsSync(binDir)) fs.mkdirSync(binDir, { recursive: true })
const tmpDir = path.join(os.tmpdir(), `tfcode-install-${process.pid}`)
fs.mkdirSync(tmpDir, { recursive: true })
const archivePath = path.join(tmpDir, filename)
// Use curl to download
const curlResult = spawnSync("curl", ["-fsSL", "-o", archivePath, url], { stdio: "inherit" })
if (curlResult.status !== 0) {
console.error(`Failed to download from ${url}`)
console.error("Trying npm package fallback...")
// Fallback to npm optionalDependencies
try {
const pkgName = `@toothfairyai/${target}`
const pkgPath = require.resolve(`${pkgName}/package.json`)
const pkgDir = path.dirname(pkgPath)
const binaryName = platform === "windows" ? "tfcode.exe" : "tfcode"
const binaryPath = path.join(pkgDir, "bin", binaryName)
if (fs.existsSync(binaryPath)) {
setupBinary(binaryPath, platform)
return
}
} catch {}
process.exit(1)
}
// Extract
console.log("Extracting...")
if (platform === "linux") {
spawnSync("tar", ["-xzf", archivePath, "-C", tmpDir], { stdio: "inherit" })
} else {
spawnSync("unzip", ["-q", archivePath, "-d", tmpDir], { stdio: "inherit" })
}
// Move binary
const binaryName = platform === "windows" ? "tfcode.exe" : "tfcode"
const extractedBinary = path.join(tmpDir, "tfcode")
const targetBinary = path.join(binDir, binaryName)
if (fs.existsSync(extractedBinary)) {
if (fs.existsSync(targetBinary)) fs.unlinkSync(targetBinary)
fs.copyFileSync(extractedBinary, targetBinary)
fs.chmodSync(targetBinary, 0o755)
console.log(`Installed tfcode to ${targetBinary}`)
}
// Cleanup
fs.rmSync(tmpDir, { recursive: true, force: true })
}
function setupBinary(sourcePath, platform) {
const binDir = path.join(__dirname, "bin")
const binaryName = platform === "windows" ? "tfcode.exe" : "tfcode"
const targetBinary = path.join(binDir, binaryName)
if (!fs.existsSync(binDir)) fs.mkdirSync(binDir, { recursive: true })
if (fs.existsSync(targetBinary)) fs.unlinkSync(targetBinary)
// Try hardlink, fall back to copy
try {
fs.linkSync(sourcePath, targetBinary)
} catch {
fs.copyFileSync(sourcePath, targetBinary)
}
fs.chmodSync(targetBinary, 0o755)
console.log(`tfcode installed to ${targetBinary}`)
}
async function main() {
console.log("")
console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
console.log(" tfcode - ToothFairyAI's official coding agent")
console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
console.log("")
// Check for Python (needed for TF integration)
try {
const result = spawnSync("python3", ["--version"], { encoding: "utf8" })
if (result.status === 0) {
console.log(`✓ Found ${result.stdout.trim()}`)
// Install Python SDK
console.log("Installing ToothFairyAI Python SDK...")
const pipResult = spawnSync("python3", ["-m", "pip", "install", "--user", "--break-system-packages", "toothfairyai", "pydantic", "httpx", "rich"], {
stdio: "inherit"
})
if (pipResult.status === 0) {
console.log("✓ Python SDK installed")
} else {
console.log("! Python SDK install failed, run manually:")
console.log(" pip install toothfairyai pydantic httpx rich")
}
}
} catch {
console.log("! Python 3.10+ not found. Install with:")
console.log(" macOS: brew install python@3.12")
console.log(" Ubuntu: sudo apt install python3.12")
console.log(" Windows: Download from python.org/downloads")
console.log("")
}
console.log("")
console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
console.log("✓ tfcode installed successfully!")
console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
console.log("")
console.log("Quick Start:")
console.log("")
console.log(" 1. Set credentials:")
console.log(" export TF_WORKSPACE_ID=\"your-workspace-id\"")
console.log(" export TF_API_KEY=\"your-api-key\"")
console.log("")
console.log(" 2. Validate:")
console.log(" tfcode validate")
console.log("")
console.log(" 3. Sync tools:")
console.log(" tfcode sync")
console.log("")
console.log(" 4. Start coding:")
console.log(" tfcode")
console.log("")
console.log("Documentation: https://toothfairyai.com/developers/tfcode")
console.log("")
}
main().catch(console.error)

View File

@@ -0,0 +1,236 @@
#!/usr/bin/env bun
import { $ } from "bun"
import fs from "fs"
import path from "path"
import { fileURLToPath } from "url"
import pkg from "../package.json"
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const dir = path.resolve(__dirname, "..")
process.chdir(dir)
const TFCODE_VERSION = pkg.version
const GITEA_HOST = process.env.GITEA_HOST || "gitea.toothfairyai.com"
const GITEA_TOKEN = process.env.GITEA_TOKEN
const GITEA_REPO = process.env.GITEA_REPO || "ToothFairyAI/tfcode"
// Collect binaries
const binaries: Record<string, string> = {}
for (const filepath of new Bun.Glob("*/package.json").scanSync({ cwd: "./dist" })) {
const pkg = await Bun.file(`./dist/${filepath}`).json()
if (pkg.name.startsWith("@toothfairyai/tfcode-")) {
binaries[pkg.name] = pkg.version
}
}
console.log("Binaries:", binaries)
// Upload to Gitea release
async function uploadToGitea() {
if (!GITEA_TOKEN) {
console.error("GITEA_TOKEN is required")
process.exit(1)
}
// Check if release exists
const releaseUrl = `https://${GITEA_HOST}/api/v1/repos/${GITEA_REPO}/releases/tags/v${TFCODE_VERSION}`
let releaseId: string
try {
const res = await fetch(releaseUrl, {
headers: { Authorization: `token ${GITEA_TOKEN}` }
})
if (res.ok) {
const release = await res.json()
releaseId = release.id
console.log(`Release v${TFCODE_VERSION} exists, updating...`)
} else {
throw new Error("Not found")
}
} catch {
// Create new release
const createUrl = `https://${GITEA_HOST}/api/v1/repos/${GITEA_REPO}/releases`
const res = await fetch(createUrl, {
method: "POST",
headers: {
Authorization: `token ${GITEA_TOKEN}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
tag_name: `v${TFCODE_VERSION}`,
name: `v${TFCODE_VERSION}`,
body: `tfcode v${TFCODE_VERSION}\n\nSee CHANGELOG.md for details.`,
draft: false,
prerelease: TFCODE_VERSION.includes("-")
})
})
if (!res.ok) {
console.error("Failed to create release:", await res.text())
process.exit(1)
}
const release = await res.json()
releaseId = release.id
console.log(`Created release v${TFCODE_VERSION}`)
}
// Upload assets
const assets = await fs.promises.readdir("./dist")
for (const asset of assets) {
if (asset.endsWith(".tar.gz") || asset.endsWith(".zip")) {
const assetPath = `./dist/${asset}`
const file = Bun.file(assetPath)
const uploadUrl = `https://${GITEA_HOST}/api/v1/repos/${GITEA_REPO}/releases/${releaseId}/assets?name=${asset}`
console.log(`Uploading ${asset}...`)
const res = await fetch(uploadUrl, {
method: "POST",
headers: {
Authorization: `token ${GITEA_TOKEN}`,
"Content-Type": "application/octet-stream"
},
body: file
})
if (!res.ok) {
console.error(`Failed to upload ${asset}:`, await res.text())
} else {
console.log(`Uploaded ${asset}`)
}
}
}
}
// Create main npm package
async function createMainPackage() {
await $`mkdir -p ./dist/tfcode`
await $`cp -r ./bin ./dist/tfcode/bin`
await Bun.file(`./dist/tfcode/postinstall.mjs`).write(await Bun.file("./script/postinstall-tfcode.mjs").text())
await Bun.file(`./dist/tfcode/LICENSE`).write(await Bun.file("../../LICENSE").text())
await Bun.file(`./dist/tfcode/package.json`).write(
JSON.stringify(
{
name: "@toothfairyai/tfcode",
version: TFCODE_VERSION,
bin: { tfcode: "./bin/tfcode" },
scripts: { postinstall: "node ./postinstall.mjs" },
license: pkg.license,
optionalDependencies: binaries,
homepage: "https://toothfairyai.com/developers/tfcode",
repository: {
type: "git",
url: `https://${GITEA_HOST}/${GITEA_REPO}.git`
}
},
null,
2
)
)
// Pack and publish
if (process.platform !== "win32") {
await $`chmod -R 755 ./dist/tfcode`
}
for (const [name, version] of Object.entries(binaries)) {
console.log(`Publishing ${name}...`)
await $`cd ./dist/${name.replace('@toothfairyai/', '')} && bun pm pack && npm publish *.tgz --access public --tag beta`
}
console.log("Publishing main package...")
await $`cd ./dist/tfcode && bun pm pack && npm publish *.tgz --access public --tag beta`
}
// Create Homebrew formula
async function createHomebrewFormula() {
const arm64Sha = await $`sha256sum ./dist/tfcode-linux-arm64.tar.gz | cut -d' ' -f1`.text().then((x) => x.trim())
const x64Sha = await $`sha256sum ./dist/tfcode-linux-x64.tar.gz | cut -d' ' -f1`.text().then((x) => x.trim())
const macX64Sha = await $`sha256sum ./dist/tfcode-darwin-x64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
const macArm64Sha = await $`sha256sum ./dist/tfcode-darwin-arm64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
const formula = [
"# typed: false",
"# frozen_string_literal: true",
"",
"class Tfcode < Formula",
` desc "ToothFairyAI's official AI coding agent"`,
` homepage "https://toothfairyai.com/developers/tfcode"`,
` version "${TFCODE_VERSION.split("-")[0]}"`,
"",
` depends_on "ripgrep"`,
"",
" on_macos do",
" if Hardware::CPU.intel?",
` url "https://${GITEA_HOST}/${GITEA_REPO}/releases/download/v${TFCODE_VERSION}/tfcode-darwin-x64.zip"`,
` sha256 "${macX64Sha}"`,
"",
" def install",
' bin.install "tfcode"',
" end",
" end",
" if Hardware::CPU.arm?",
` url "https://${GITEA_HOST}/${GITEA_REPO}/releases/download/v${TFCODE_VERSION}/tfcode-darwin-arm64.zip"`,
` sha256 "${macArm64Sha}"`,
"",
" def install",
' bin.install "tfcode"',
" end",
" end",
" end",
"",
" on_linux do",
" if Hardware::CPU.intel? and Hardware::CPU.is_64_bit?",
` url "https://${GITEA_HOST}/${GITEA_REPO}/releases/download/v${TFCODE_VERSION}/tfcode-linux-x64.tar.gz"`,
` sha256 "${x64Sha}"`,
" def install",
' bin.install "tfcode"',
" end",
" end",
" if Hardware::CPU.arm? and Hardware::CPU.is_64_bit?",
` url "https://${GITEA_HOST}/${GITEA_REPO}/releases/download/v${TFCODE_VERSION}/tfcode-linux-arm64.tar.gz"`,
` sha256 "${arm64Sha}"`,
" def install",
' bin.install "tfcode"',
" end",
" end",
" end",
"end",
""
].join("\n")
await Bun.file("./dist/tfcode.rb").write(formula)
console.log("Created Homebrew formula: ./dist/tfcode.rb")
console.log("To publish: Create a tap repo and push this formula")
}
// Run
if (process.argv.includes("--upload")) {
await uploadToGitea()
}
if (process.argv.includes("--npm")) {
await createMainPackage()
}
if (process.argv.includes("--brew")) {
await createHomebrewFormula()
}
if (!process.argv.includes("--upload") && !process.argv.includes("--npm") && !process.argv.includes("--brew")) {
console.log(`
Usage:
bun run publish.ts --upload Upload binaries to Gitea release
bun run publish.ts --npm Publish to npm
bun run publish.ts --brew Create Homebrew formula
bun run publish.ts --all Do all of the above
`)
}
if (process.argv.includes("--all")) {
await uploadToGitea()
await createMainPackage()
await createHomebrewFormula()
}