project-ux: archive/rename/Open-in-Gitea sidebar context menu, archived projects landing, create-project bootstrap with Gitea remote
Server:
- projects.status + projects.gitea_remote (additive) with CHECK ('open','archived')
- GET /api/projects?status=archived; PATCH /api/projects/:id (rename);
POST /api/projects/:id/archive | unarchive; POST /api/projects/create
- POST /api/projects ON CONFLICT (path) DO UPDATE SET status='open': re-add
of archived path restores existing row (preserves id + FKs); already-open
path returns 409. Detected-repos picker now excludes only status='open'.
- New gitea.ts (createGiteaRepo + GiteaRepoExistsError) and
project_bootstrap.ts (sanitize name, mkdir under PROJECT_ROOT_WHITELIST,
git init -b main + first commit with -c user.name/email per-command, optional
Gitea repo create + remote add + push; all via execFile, no shell).
- 3 new user-stream frames: project_archived, project_unarchived, project_updated.
- sidebar.ts now selects path + gitea_remote and filters status='open'.
- Gitea env added to config.ts (GITEA_BASE_URL, GITEA_USER, GITEA_TOKEN,
GITEA_SSH_HOST).
- docker-compose.yml /opt mount flipped to rw so create-project can mkdir.
- auto_name.ts gate relaxed from `!== 1` to `< 1` (fires on every turn while
chat name is empty, not only the first).
Web:
- ProjectSidebar: project rows use proper Radix ContextMenu; items Rename /
Archive / Open in Gitea. Inline rename, archive confirm dialog.
Removed obsolete handleRemove + DropdownMenu hack.
- Home: Add-existing + Create-new buttons; collapsible Archived Projects
section with Restore.
- New CreateProjectModal: name + live folder preview, commit msg, Private/
Public radio, create-Gitea-remote checkbox, toast on success/warnings.
- New projectUrls.ts giteaUrlFor() — uses gitea_remote when present,
falls back to convention URL.
- 3 new event types in sessionEvents.ts with idempotent useSidebar handlers.
- SidebarProject extended with path + gitea_remote so Open-in-Gitea can
resolve without a separate fetch.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -53,7 +53,7 @@ export async function maybeAutoNameChat(
|
||||
AND status = 'complete'
|
||||
AND content <> ''
|
||||
`;
|
||||
if (counts[0]?.n !== 1) return;
|
||||
if ((counts[0]?.n ?? 0) < 1) return;
|
||||
|
||||
const chatRows = await ctx.sql<
|
||||
{ id: string; name: string | null; session_id: string }[]
|
||||
|
||||
50
apps/server/src/services/gitea.ts
Normal file
50
apps/server/src/services/gitea.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
export interface GiteaConfig {
|
||||
baseUrl: string;
|
||||
user: string;
|
||||
token: string;
|
||||
}
|
||||
|
||||
export interface GiteaRepo {
|
||||
clone_url: string;
|
||||
ssh_url: string;
|
||||
html_url: string;
|
||||
}
|
||||
|
||||
export class GiteaRepoExistsError extends Error {
|
||||
constructor() {
|
||||
super('gitea-repo-exists');
|
||||
}
|
||||
}
|
||||
|
||||
export async function createGiteaRepo(
|
||||
cfg: GiteaConfig,
|
||||
name: string,
|
||||
options: { private: boolean }
|
||||
): Promise<GiteaRepo> {
|
||||
const res = await fetch(`${cfg.baseUrl}/api/v1/user/repos`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `token ${cfg.token}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
name,
|
||||
private: options.private,
|
||||
auto_init: false,
|
||||
}),
|
||||
});
|
||||
if (res.status === 409) throw new GiteaRepoExistsError();
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => '');
|
||||
throw new Error(`gitea-api-${res.status}: ${text.slice(0, 200)}`);
|
||||
}
|
||||
const body = (await res.json()) as { ssh_url?: string; clone_url?: string; html_url?: string };
|
||||
if (!body.ssh_url || !body.html_url || !body.clone_url) {
|
||||
throw new Error(`gitea-api-unexpected-shape: ${JSON.stringify(body).slice(0, 200)}`);
|
||||
}
|
||||
return {
|
||||
ssh_url: body.ssh_url,
|
||||
clone_url: body.clone_url,
|
||||
html_url: body.html_url,
|
||||
};
|
||||
}
|
||||
179
apps/server/src/services/project_bootstrap.ts
Normal file
179
apps/server/src/services/project_bootstrap.ts
Normal file
@@ -0,0 +1,179 @@
|
||||
import { execFile } from 'node:child_process';
|
||||
import { promisify } from 'node:util';
|
||||
import { mkdir, writeFile, realpath } from 'node:fs/promises';
|
||||
import { existsSync } from 'node:fs';
|
||||
import { resolve, sep } from 'node:path';
|
||||
import type { FastifyBaseLogger } from 'fastify';
|
||||
import type { Config } from '../config.js';
|
||||
import { createGiteaRepo, GiteaRepoExistsError } from './gitea.js';
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
const GITIGNORE_TEMPLATE = `# OS / editor
|
||||
.DS_Store
|
||||
*.swp
|
||||
*~
|
||||
|
||||
# Node
|
||||
node_modules/
|
||||
dist/
|
||||
build/
|
||||
.env
|
||||
.env.local
|
||||
|
||||
# Python
|
||||
__pycache__/
|
||||
*.pyc
|
||||
.venv/
|
||||
venv/
|
||||
|
||||
# AI agents
|
||||
.claude/
|
||||
.opencode/
|
||||
|
||||
# Backups
|
||||
*.bak*
|
||||
`;
|
||||
|
||||
const GIT_USER_NAME = 'indifferentketchup';
|
||||
const GIT_USER_EMAIL = 'samkintop@gmail.com';
|
||||
|
||||
export interface BootstrapResult {
|
||||
folder_real_path: string;
|
||||
folder_name: string;
|
||||
gitea_remote_url: string | null;
|
||||
folder_created: boolean;
|
||||
git_initialized: boolean;
|
||||
first_commit: boolean;
|
||||
gitea_remote_created: boolean;
|
||||
gitea_pushed: boolean;
|
||||
warnings: string[];
|
||||
}
|
||||
|
||||
const SAFE_NAME = /^[a-z0-9][a-z0-9-]{0,63}$/;
|
||||
|
||||
export function sanitizeFolderName(raw: string): string {
|
||||
return raw
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.replace(/\s+/g, '-')
|
||||
.replace(/[^a-z0-9-]/g, '')
|
||||
.replace(/-+/g, '-')
|
||||
.replace(/^-+|-+$/g, '')
|
||||
.slice(0, 64);
|
||||
}
|
||||
|
||||
export class BootstrapNameError extends Error {}
|
||||
export class BootstrapCollisionError extends Error {}
|
||||
export class BootstrapPathError extends Error {}
|
||||
|
||||
export async function bootstrapProject(
|
||||
config: Config,
|
||||
log: FastifyBaseLogger,
|
||||
options: {
|
||||
name: string;
|
||||
commitMessage: string;
|
||||
visibility: 'private' | 'public';
|
||||
createGiteaRemote: boolean;
|
||||
}
|
||||
): Promise<BootstrapResult> {
|
||||
const folder = sanitizeFolderName(options.name);
|
||||
if (folder.length === 0 || !SAFE_NAME.test(folder)) {
|
||||
throw new BootstrapNameError(`invalid name after sanitization: "${folder}"`);
|
||||
}
|
||||
|
||||
// Whitelist resolution
|
||||
const whitelistReal = await realpath(config.PROJECT_ROOT_WHITELIST);
|
||||
const fullPath = resolve(whitelistReal, folder);
|
||||
if (!fullPath.startsWith(whitelistReal + sep)) {
|
||||
throw new BootstrapPathError('path escapes whitelist');
|
||||
}
|
||||
if (existsSync(fullPath)) {
|
||||
throw new BootstrapCollisionError(`path already exists: ${fullPath}`);
|
||||
}
|
||||
|
||||
const warnings: string[] = [];
|
||||
let folder_created = false;
|
||||
let git_initialized = false;
|
||||
let first_commit = false;
|
||||
let gitea_remote_created = false;
|
||||
let gitea_pushed = false;
|
||||
let gitea_remote_url: string | null = null;
|
||||
|
||||
// Step 1: mkdir
|
||||
await mkdir(fullPath, { recursive: false });
|
||||
folder_created = true;
|
||||
log.info({ fullPath }, 'project_bootstrap: folder created');
|
||||
|
||||
// Step 2: write .gitignore
|
||||
await writeFile(resolve(fullPath, '.gitignore'), GITIGNORE_TEMPLATE, 'utf8');
|
||||
|
||||
// Step 3: git init -b main
|
||||
await execFileAsync('git', ['init', '-b', 'main'], { cwd: fullPath });
|
||||
git_initialized = true;
|
||||
|
||||
// Step 4: git add + commit (per-command -c, no global config touch)
|
||||
await execFileAsync('git', ['add', '.gitignore'], { cwd: fullPath });
|
||||
await execFileAsync(
|
||||
'git',
|
||||
[
|
||||
'-c', `user.name=${GIT_USER_NAME}`,
|
||||
'-c', `user.email=${GIT_USER_EMAIL}`,
|
||||
'commit',
|
||||
'-m', options.commitMessage,
|
||||
],
|
||||
{ cwd: fullPath }
|
||||
);
|
||||
first_commit = true;
|
||||
log.info({ folder }, 'project_bootstrap: initial commit');
|
||||
|
||||
// Step 5: optional Gitea remote
|
||||
if (options.createGiteaRemote) {
|
||||
if (!config.GITEA_TOKEN) {
|
||||
warnings.push('Gitea remote skipped — token not configured');
|
||||
} else {
|
||||
try {
|
||||
const repo = await createGiteaRepo(
|
||||
{ baseUrl: config.GITEA_BASE_URL, user: config.GITEA_USER, token: config.GITEA_TOKEN },
|
||||
folder,
|
||||
{ private: options.visibility === 'private' }
|
||||
);
|
||||
gitea_remote_created = true;
|
||||
gitea_remote_url = repo.html_url;
|
||||
log.info({ folder, html_url: repo.html_url }, 'project_bootstrap: gitea repo created');
|
||||
|
||||
// Step 6: git remote add + push
|
||||
try {
|
||||
await execFileAsync('git', ['remote', 'add', 'origin', repo.ssh_url], { cwd: fullPath });
|
||||
await execFileAsync('git', ['push', '-u', 'origin', 'main'], { cwd: fullPath });
|
||||
gitea_pushed = true;
|
||||
log.info({ folder }, 'project_bootstrap: pushed to gitea');
|
||||
} catch (pushErr) {
|
||||
const msg = pushErr instanceof Error ? pushErr.message : String(pushErr);
|
||||
warnings.push(`Push to Gitea failed: ${msg.slice(0, 200)}`);
|
||||
log.warn({ err: pushErr, folder }, 'project_bootstrap: push failed');
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof GiteaRepoExistsError) {
|
||||
warnings.push('Gitea repo already exists with this name; local repo created without remote');
|
||||
} else {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
warnings.push(`Gitea remote creation failed: ${msg.slice(0, 200)}`);
|
||||
}
|
||||
log.warn({ err, folder }, 'project_bootstrap: gitea remote step failed');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
folder_real_path: fullPath,
|
||||
folder_name: folder,
|
||||
gitea_remote_url,
|
||||
folder_created,
|
||||
git_initialized,
|
||||
first_commit,
|
||||
gitea_remote_created,
|
||||
gitea_pushed,
|
||||
warnings,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user