fix: create /workspaces dir, clone repo before running agent
Made-with: Cursor
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import express, { Request, Response, NextFunction } from 'express';
|
||||
import cors from 'cors';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { execSync } from 'child_process';
|
||||
import { createJob, getJob, listJobs, updateJob } from './job-store';
|
||||
import { runAgent } from './agent-runner';
|
||||
import { AGENTS } from './agents';
|
||||
@@ -15,10 +18,36 @@ const PORT = process.env.PORT || 3333;
|
||||
// Build ToolContext from environment variables
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function ensureWorkspace(repo?: string): string {
|
||||
const base = process.env.WORKSPACE_BASE || '/workspaces';
|
||||
if (!repo) {
|
||||
const dir = path.join(base, 'default');
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
return dir;
|
||||
}
|
||||
const dir = path.join(base, repo.replace('/', '_'));
|
||||
const gitea = {
|
||||
apiUrl: process.env.GITEA_API_URL || '',
|
||||
apiToken: process.env.GITEA_API_TOKEN || '',
|
||||
username: process.env.GITEA_USERNAME || ''
|
||||
};
|
||||
if (!fs.existsSync(path.join(dir, '.git'))) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
const authedUrl = `${gitea.apiUrl}/${repo}.git`
|
||||
.replace('https://', `https://${gitea.username}:${gitea.apiToken}@`);
|
||||
try {
|
||||
execSync(`git clone "${authedUrl}" "${dir}"`, { stdio: 'pipe' });
|
||||
} catch {
|
||||
// Repo may not exist yet — just init
|
||||
execSync(`git init`, { cwd: dir, stdio: 'pipe' });
|
||||
execSync(`git remote add origin "${authedUrl}"`, { cwd: dir, stdio: 'pipe' });
|
||||
}
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
function buildContext(repo?: string): ToolContext {
|
||||
const workspaceRoot = repo
|
||||
? `${process.env.WORKSPACE_BASE || '/workspaces'}/${repo.replace('/', '_')}`
|
||||
: (process.env.WORKSPACE_BASE || '/workspaces/default');
|
||||
const workspaceRoot = ensureWorkspace(repo);
|
||||
|
||||
return {
|
||||
workspaceRoot,
|
||||
|
||||
Reference in New Issue
Block a user