agent.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. import express from 'express';
  2. import { spawn } from 'child_process';
  3. import path from 'path';
  4. import os from 'os';
  5. import { promises as fs } from 'fs';
  6. import crypto from 'crypto';
  7. import { userDb, apiKeysDb, githubTokensDb } from '../database/db.js';
  8. import { addProjectManually } from '../projects.js';
  9. import { runChatViaGateway } from '../pilotdeck-bridge.js';
  10. import { Octokit } from '@octokit/rest';
  11. import { IS_PLATFORM } from '../constants/config.js';
  12. const router = express.Router();
  13. /**
  14. * Middleware to authenticate agent API requests.
  15. *
  16. * Supports two authentication modes:
  17. * 1. Platform mode (IS_PLATFORM=true): For managed/hosted deployments where
  18. * authentication is handled by an external proxy. Requests are trusted and
  19. * the default user context is used.
  20. *
  21. * 2. API key mode (default): For self-hosted deployments where users authenticate
  22. * via API keys created in the UI. Keys are validated against the local database.
  23. */
  24. const validateExternalApiKey = (req, res, next) => {
  25. // Platform mode: Authentication is handled externally (e.g., by a proxy layer).
  26. // Trust the request and use the default user context.
  27. if (IS_PLATFORM) {
  28. try {
  29. const user = userDb.getFirstUser();
  30. if (!user) {
  31. return res.status(500).json({ error: 'Platform mode: No user found in database' });
  32. }
  33. req.user = user;
  34. return next();
  35. } catch (error) {
  36. console.error('Platform mode error:', error);
  37. return res.status(500).json({ error: 'Platform mode: Failed to fetch user' });
  38. }
  39. }
  40. // Self-hosted mode: Validate API key from header or query parameter
  41. const apiKey = req.headers['x-api-key'] || req.query.apiKey;
  42. if (!apiKey) {
  43. return res.status(401).json({ error: 'API key required' });
  44. }
  45. const user = apiKeysDb.validateApiKey(apiKey);
  46. if (!user) {
  47. return res.status(401).json({ error: 'Invalid or inactive API key' });
  48. }
  49. req.user = user;
  50. next();
  51. };
  52. /**
  53. * Get the remote URL of a git repository
  54. * @param {string} repoPath - Path to the git repository
  55. * @returns {Promise<string>} - Remote URL of the repository
  56. */
  57. async function getGitRemoteUrl(repoPath) {
  58. return new Promise((resolve, reject) => {
  59. const gitProcess = spawn('git', ['config', '--get', 'remote.origin.url'], {
  60. cwd: repoPath,
  61. stdio: ['pipe', 'pipe', 'pipe']
  62. });
  63. let stdout = '';
  64. let stderr = '';
  65. gitProcess.stdout.on('data', (data) => {
  66. stdout += data.toString();
  67. });
  68. gitProcess.stderr.on('data', (data) => {
  69. stderr += data.toString();
  70. });
  71. gitProcess.on('close', (code) => {
  72. if (code === 0) {
  73. resolve(stdout.trim());
  74. } else {
  75. reject(new Error(`Failed to get git remote: ${stderr}`));
  76. }
  77. });
  78. gitProcess.on('error', (error) => {
  79. reject(new Error(`Failed to execute git: ${error.message}`));
  80. });
  81. });
  82. }
  83. /**
  84. * Normalize GitHub URLs for comparison
  85. * @param {string} url - GitHub URL
  86. * @returns {string} - Normalized URL
  87. */
  88. function normalizeGitHubUrl(url) {
  89. // Remove .git suffix
  90. let normalized = url.replace(/\.git$/, '');
  91. // Convert SSH to HTTPS format for comparison
  92. normalized = normalized.replace(/^git@github\.com:/, 'https://github.com/');
  93. // Remove trailing slash
  94. normalized = normalized.replace(/\/$/, '');
  95. return normalized.toLowerCase();
  96. }
  97. /**
  98. * Parse GitHub URL to extract owner and repo
  99. * @param {string} url - GitHub URL (HTTPS or SSH)
  100. * @returns {{owner: string, repo: string}} - Parsed owner and repo
  101. */
  102. function parseGitHubUrl(url) {
  103. // Handle HTTPS URLs: https://github.com/owner/repo or https://github.com/owner/repo.git
  104. // Handle SSH URLs: git@github.com:owner/repo or git@github.com:owner/repo.git
  105. const match = url.match(/github\.com[:/]([^/]+)\/([^/]+?)(?:\.git)?$/);
  106. if (!match) {
  107. throw new Error('Invalid GitHub URL format');
  108. }
  109. return {
  110. owner: match[1],
  111. repo: match[2].replace(/\.git$/, '')
  112. };
  113. }
  114. /**
  115. * Auto-generate a branch name from a message
  116. * @param {string} message - The agent message
  117. * @returns {string} - Generated branch name
  118. */
  119. function autogenerateBranchName(message) {
  120. // Convert to lowercase, replace spaces/special chars with hyphens
  121. let branchName = message
  122. .toLowerCase()
  123. .replace(/[^a-z0-9\s-]/g, '') // Remove special characters
  124. .replace(/\s+/g, '-') // Replace spaces with hyphens
  125. .replace(/-+/g, '-') // Replace multiple hyphens with single
  126. .replace(/^-|-$/g, ''); // Remove leading/trailing hyphens
  127. // Ensure non-empty fallback
  128. if (!branchName) {
  129. branchName = 'task';
  130. }
  131. // Generate timestamp suffix (last 6 chars of base36 timestamp)
  132. const timestamp = Date.now().toString(36).slice(-6);
  133. const suffix = `-${timestamp}`;
  134. // Limit length to ensure total length including suffix fits within 50 characters
  135. const maxBaseLength = 50 - suffix.length;
  136. if (branchName.length > maxBaseLength) {
  137. branchName = branchName.substring(0, maxBaseLength);
  138. }
  139. // Remove any trailing hyphen after truncation and ensure no leading hyphen
  140. branchName = branchName.replace(/-$/, '').replace(/^-+/, '');
  141. // If still empty or starts with hyphen after cleanup, use fallback
  142. if (!branchName || branchName.startsWith('-')) {
  143. branchName = 'task';
  144. }
  145. // Combine base name with timestamp suffix
  146. branchName = `${branchName}${suffix}`;
  147. // Final validation: ensure it matches safe pattern
  148. if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(branchName)) {
  149. // Fallback to deterministic safe name
  150. return `branch-${timestamp}`;
  151. }
  152. return branchName;
  153. }
  154. /**
  155. * Validate a Git branch name
  156. * @param {string} branchName - Branch name to validate
  157. * @returns {{valid: boolean, error?: string}} - Validation result
  158. */
  159. function validateBranchName(branchName) {
  160. if (!branchName || branchName.trim() === '') {
  161. return { valid: false, error: 'Branch name cannot be empty' };
  162. }
  163. // Git branch name rules
  164. const invalidPatterns = [
  165. { pattern: /^\./, message: 'Branch name cannot start with a dot' },
  166. { pattern: /\.$/, message: 'Branch name cannot end with a dot' },
  167. { pattern: /\.\./, message: 'Branch name cannot contain consecutive dots (..)' },
  168. { pattern: /\s/, message: 'Branch name cannot contain spaces' },
  169. { pattern: /[~^:?*\[\\]/, message: 'Branch name cannot contain special characters: ~ ^ : ? * [ \\' },
  170. { pattern: /@{/, message: 'Branch name cannot contain @{' },
  171. { pattern: /\/$/, message: 'Branch name cannot end with a slash' },
  172. { pattern: /^\//, message: 'Branch name cannot start with a slash' },
  173. { pattern: /\/\//, message: 'Branch name cannot contain consecutive slashes' },
  174. { pattern: /\.lock$/, message: 'Branch name cannot end with .lock' }
  175. ];
  176. for (const { pattern, message } of invalidPatterns) {
  177. if (pattern.test(branchName)) {
  178. return { valid: false, error: message };
  179. }
  180. }
  181. // Check for ASCII control characters
  182. if (/[\x00-\x1F\x7F]/.test(branchName)) {
  183. return { valid: false, error: 'Branch name cannot contain control characters' };
  184. }
  185. return { valid: true };
  186. }
  187. /**
  188. * Get recent commit messages from a repository
  189. * @param {string} projectPath - Path to the git repository
  190. * @param {number} limit - Number of commits to retrieve (default: 5)
  191. * @returns {Promise<string[]>} - Array of commit messages
  192. */
  193. async function getCommitMessages(projectPath, limit = 5) {
  194. return new Promise((resolve, reject) => {
  195. const gitProcess = spawn('git', ['log', `-${limit}`, '--pretty=format:%s'], {
  196. cwd: projectPath,
  197. stdio: ['pipe', 'pipe', 'pipe']
  198. });
  199. let stdout = '';
  200. let stderr = '';
  201. gitProcess.stdout.on('data', (data) => {
  202. stdout += data.toString();
  203. });
  204. gitProcess.stderr.on('data', (data) => {
  205. stderr += data.toString();
  206. });
  207. gitProcess.on('close', (code) => {
  208. if (code === 0) {
  209. const messages = stdout.trim().split('\n').filter(msg => msg.length > 0);
  210. resolve(messages);
  211. } else {
  212. reject(new Error(`Failed to get commit messages: ${stderr}`));
  213. }
  214. });
  215. gitProcess.on('error', (error) => {
  216. reject(new Error(`Failed to execute git: ${error.message}`));
  217. });
  218. });
  219. }
  220. /**
  221. * Create a new branch on GitHub using the API
  222. * @param {Octokit} octokit - Octokit instance
  223. * @param {string} owner - Repository owner
  224. * @param {string} repo - Repository name
  225. * @param {string} branchName - Name of the new branch
  226. * @param {string} baseBranch - Base branch to branch from (default: 'main')
  227. * @returns {Promise<void>}
  228. */
  229. async function createGitHubBranch(octokit, owner, repo, branchName, baseBranch = 'main') {
  230. try {
  231. // Get the SHA of the base branch
  232. const { data: ref } = await octokit.git.getRef({
  233. owner,
  234. repo,
  235. ref: `heads/${baseBranch}`
  236. });
  237. const baseSha = ref.object.sha;
  238. // Create the new branch
  239. await octokit.git.createRef({
  240. owner,
  241. repo,
  242. ref: `refs/heads/${branchName}`,
  243. sha: baseSha
  244. });
  245. console.log(`✅ Created branch '${branchName}' on GitHub`);
  246. } catch (error) {
  247. if (error.status === 422 && error.message.includes('Reference already exists')) {
  248. console.log(`ℹ️ Branch '${branchName}' already exists on GitHub`);
  249. } else {
  250. throw error;
  251. }
  252. }
  253. }
  254. /**
  255. * Create a pull request on GitHub
  256. * @param {Octokit} octokit - Octokit instance
  257. * @param {string} owner - Repository owner
  258. * @param {string} repo - Repository name
  259. * @param {string} branchName - Head branch name
  260. * @param {string} title - PR title
  261. * @param {string} body - PR body/description
  262. * @param {string} baseBranch - Base branch (default: 'main')
  263. * @returns {Promise<{number: number, url: string}>} - PR number and URL
  264. */
  265. async function createGitHubPR(octokit, owner, repo, branchName, title, body, baseBranch = 'main') {
  266. const { data: pr } = await octokit.pulls.create({
  267. owner,
  268. repo,
  269. title,
  270. head: branchName,
  271. base: baseBranch,
  272. body
  273. });
  274. console.log(`✅ Created pull request #${pr.number}: ${pr.html_url}`);
  275. return {
  276. number: pr.number,
  277. url: pr.html_url
  278. };
  279. }
  280. /**
  281. * Clone a GitHub repository to a directory
  282. * @param {string} githubUrl - GitHub repository URL
  283. * @param {string} githubToken - Optional GitHub token for private repos
  284. * @param {string} projectPath - Path for cloning the repository
  285. * @returns {Promise<string>} - Path to the cloned repository
  286. */
  287. async function cloneGitHubRepo(githubUrl, githubToken = null, projectPath) {
  288. return new Promise(async (resolve, reject) => {
  289. try {
  290. // Validate GitHub URL
  291. if (!githubUrl || !githubUrl.includes('github.com')) {
  292. throw new Error('Invalid GitHub URL');
  293. }
  294. const cloneDir = path.resolve(projectPath);
  295. // Check if directory already exists
  296. try {
  297. await fs.access(cloneDir);
  298. // Directory exists - check if it's a git repo with the same URL
  299. try {
  300. const existingUrl = await getGitRemoteUrl(cloneDir);
  301. const normalizedExisting = normalizeGitHubUrl(existingUrl);
  302. const normalizedRequested = normalizeGitHubUrl(githubUrl);
  303. if (normalizedExisting === normalizedRequested) {
  304. console.log('✅ Repository already exists at path with correct URL');
  305. return resolve(cloneDir);
  306. } else {
  307. throw new Error(`Directory ${cloneDir} already exists with a different repository (${existingUrl}). Expected: ${githubUrl}`);
  308. }
  309. } catch (gitError) {
  310. throw new Error(`Directory ${cloneDir} already exists but is not a valid git repository or git command failed`);
  311. }
  312. } catch (accessError) {
  313. // Directory doesn't exist - proceed with clone
  314. }
  315. // Ensure parent directory exists
  316. await fs.mkdir(path.dirname(cloneDir), { recursive: true });
  317. // Prepare the git clone URL with authentication if token is provided
  318. let cloneUrl = githubUrl;
  319. if (githubToken) {
  320. // Convert HTTPS URL to authenticated URL
  321. // Example: https://github.com/user/repo -> https://token@github.com/user/repo
  322. cloneUrl = githubUrl.replace('https://github.com', `https://${githubToken}@github.com`);
  323. }
  324. console.log('🔄 Cloning repository:', githubUrl);
  325. console.log('📁 Destination:', cloneDir);
  326. // Execute git clone
  327. const gitProcess = spawn('git', ['clone', '--depth', '1', cloneUrl, cloneDir], {
  328. stdio: ['pipe', 'pipe', 'pipe']
  329. });
  330. let stdout = '';
  331. let stderr = '';
  332. gitProcess.stdout.on('data', (data) => {
  333. stdout += data.toString();
  334. });
  335. gitProcess.stderr.on('data', (data) => {
  336. stderr += data.toString();
  337. console.log('Git stderr:', data.toString());
  338. });
  339. gitProcess.on('close', (code) => {
  340. if (code === 0) {
  341. console.log('✅ Repository cloned successfully');
  342. resolve(cloneDir);
  343. } else {
  344. console.error('❌ Git clone failed:', stderr);
  345. reject(new Error(`Git clone failed: ${stderr}`));
  346. }
  347. });
  348. gitProcess.on('error', (error) => {
  349. reject(new Error(`Failed to execute git: ${error.message}`));
  350. });
  351. } catch (error) {
  352. reject(error);
  353. }
  354. });
  355. }
  356. /**
  357. * @param {string} projectPath - Path to the project directory
  358. * @param {string} sessionId - Session ID to clean up
  359. */
  360. async function cleanupProject(projectPath, sessionId = null) {
  361. try {
  362. // Only clean up projects in the external-projects directory
  363. if (!projectPath.includes('.pilotdeck/external-projects')) {
  364. console.warn('⚠️ Refusing to clean up non-external project:', projectPath);
  365. return;
  366. }
  367. console.log('🧹 Cleaning up project:', projectPath);
  368. await fs.rm(projectPath, { recursive: true, force: true });
  369. console.log('✅ Project cleaned up');
  370. if (sessionId) {
  371. try {
  372. const sessionPath = path.join(os.homedir(), '.pilotdeck', 'sessions', sessionId);
  373. console.log('🧹 Cleaning up session directory:', sessionPath);
  374. await fs.rm(sessionPath, { recursive: true, force: true });
  375. console.log('✅ Session directory cleaned up');
  376. } catch (error) {
  377. console.error('⚠️ Failed to clean up session directory:', error.message);
  378. }
  379. }
  380. } catch (error) {
  381. console.error('❌ Failed to clean up project:', error);
  382. }
  383. }
  384. /**
  385. * SSE Stream Writer - Adapts SDK/CLI output to Server-Sent Events
  386. */
  387. class SSEStreamWriter {
  388. constructor(res, userId = null) {
  389. this.res = res;
  390. this.sessionId = null;
  391. this.userId = userId;
  392. this.isSSEStreamWriter = true; // Marker for transport detection
  393. }
  394. send(data) {
  395. if (this.res.writableEnded) {
  396. return;
  397. }
  398. // Format as SSE - providers send raw objects, we stringify
  399. this.res.write(`data: ${JSON.stringify(data)}\n\n`);
  400. }
  401. end() {
  402. if (!this.res.writableEnded) {
  403. this.res.write('data: {"type":"done"}\n\n');
  404. this.res.end();
  405. }
  406. }
  407. setSessionId(sessionId) {
  408. this.sessionId = sessionId;
  409. this.send({ type: 'session-id', sessionId });
  410. }
  411. getSessionId() {
  412. return this.sessionId;
  413. }
  414. }
  415. /**
  416. * Non-streaming response collector
  417. */
  418. class ResponseCollector {
  419. constructor(userId = null) {
  420. this.messages = [];
  421. this.sessionId = null;
  422. this.userId = userId;
  423. }
  424. send(data) {
  425. // Store ALL messages for now - we'll filter when returning
  426. this.messages.push(data);
  427. // Extract sessionId if present
  428. if (typeof data === 'string') {
  429. try {
  430. const parsed = JSON.parse(data);
  431. if (parsed.sessionId) {
  432. this.sessionId = parsed.sessionId;
  433. }
  434. } catch (e) {
  435. // Not JSON, ignore
  436. }
  437. } else if (data && data.sessionId) {
  438. this.sessionId = data.sessionId;
  439. }
  440. }
  441. end() {
  442. // Do nothing - we'll collect all messages
  443. }
  444. setSessionId(sessionId) {
  445. this.sessionId = sessionId;
  446. }
  447. getSessionId() {
  448. return this.sessionId;
  449. }
  450. getMessages() {
  451. return this.messages;
  452. }
  453. /**
  454. * Get filtered assistant messages only
  455. */
  456. getAssistantMessages() {
  457. const assistantMessages = [];
  458. for (const msg of this.messages) {
  459. // Skip initial status message
  460. if (msg && msg.type === 'status') {
  461. continue;
  462. }
  463. // Handle JSON strings
  464. if (typeof msg === 'string') {
  465. try {
  466. const parsed = JSON.parse(msg);
  467. if (parsed.type === 'claude-response' && parsed.data && parsed.data.type === 'assistant' || parsed.type === 'pilotdeck-response' && parsed.data && parsed.data.type === 'assistant') {
  468. assistantMessages.push(parsed.data);
  469. }
  470. } catch (e) {
  471. // Not JSON, skip
  472. }
  473. }
  474. }
  475. return assistantMessages;
  476. }
  477. /**
  478. * Calculate total tokens from all messages
  479. */
  480. getTotalTokens() {
  481. let totalInput = 0;
  482. let totalOutput = 0;
  483. let totalCacheRead = 0;
  484. let totalCacheCreation = 0;
  485. for (const msg of this.messages) {
  486. let data = msg;
  487. // Parse if string
  488. if (typeof msg === 'string') {
  489. try {
  490. data = JSON.parse(msg);
  491. } catch (e) {
  492. continue;
  493. }
  494. }
  495. if (data && (data.type === 'claude-response' || data.type === 'pilotdeck-response') && data.data) {
  496. const msgData = data.data;
  497. if (msgData.message && msgData.message.usage) {
  498. const usage = msgData.message.usage;
  499. totalInput += usage.input_tokens || 0;
  500. totalOutput += usage.output_tokens || 0;
  501. totalCacheRead += usage.cache_read_input_tokens || 0;
  502. totalCacheCreation += usage.cache_creation_input_tokens || 0;
  503. }
  504. }
  505. }
  506. return {
  507. inputTokens: totalInput,
  508. outputTokens: totalOutput,
  509. cacheReadTokens: totalCacheRead,
  510. cacheCreationTokens: totalCacheCreation,
  511. totalTokens: totalInput + totalOutput + totalCacheRead + totalCacheCreation
  512. };
  513. }
  514. }
  515. // ===============================
  516. // External API Endpoint
  517. // ===============================
  518. /**
  519. * POST /api/agent
  520. *
  521. * Supports automatic GitHub branch and pull request creation after successful completion.
  522. *
  523. * ================================================================================================
  524. * REQUEST BODY PARAMETERS
  525. * ================================================================================================
  526. *
  527. * @param {string} githubUrl - (Conditionally Required) GitHub repository URL to clone.
  528. * Supported formats:
  529. * - HTTPS: https://github.com/owner/repo
  530. * - HTTPS with .git: https://github.com/owner/repo.git
  531. * - SSH: git@github.com:owner/repo
  532. * - SSH with .git: git@github.com:owner/repo.git
  533. *
  534. * @param {string} projectPath - (Conditionally Required) Path to existing project OR destination for cloning.
  535. * Behavior depends on usage:
  536. * - If used alone: Must point to existing project directory
  537. * - If used with githubUrl: Target location for cloning
  538. *
  539. * @param {string} message - (Required) Task description for the AI agent. Used as:
  540. * - Instructions for the agent
  541. * - Source for auto-generated branch names (if createBranch=true and no branchName)
  542. * - Fallback for PR title if no commits are made
  543. *
  544. *
  545. * @param {boolean} stream - (Optional) Enable Server-Sent Events (SSE) streaming for real-time updates.
  546. * Default: true
  547. * - true: Returns text/event-stream with incremental updates
  548. * - false: Returns complete JSON response after completion
  549. *
  550. * @param {string} model - (Optional) Model identifier for providers.
  551. *
  552. * Cursor models: 'gpt-5' (default), 'gpt-5.2', 'gpt-5.2-high', 'sonnet-4.5', 'opus-4.5',
  553. * 'gemini-3-pro', 'composer-1', 'auto', 'gpt-5.1', 'gpt-5.1-high',
  554. * 'gpt-5.1-codex', 'gpt-5.1-codex-high', 'gpt-5.1-codex-max',
  555. * 'gpt-5.1-codex-max-high', 'opus-4.1', 'grok', and thinking variants
  556. * Codex models: 'gpt-5.2' (default), 'gpt-5.1-codex-max', 'o3', 'o4-mini'
  557. *
  558. * @param {boolean} cleanup - (Optional) Auto-cleanup project directory after completion.
  559. * Default: true
  560. * Behavior:
  561. * - Only applies when cloning via githubUrl (not for existing projectPath)
  562. * - Deletes cloned repository after 5 seconds
  563. * - Remote branch and PR remain on GitHub if created
  564. *
  565. * @param {string} githubToken - (Optional) GitHub Personal Access Token for authentication.
  566. * Overrides stored token from user settings.
  567. * Required for:
  568. * - Private repositories
  569. * - Branch/PR creation features
  570. * Token must have 'repo' scope for full functionality.
  571. *
  572. * @param {string} branchName - (Optional) Custom name for the Git branch.
  573. * If provided, createBranch is automatically set to true.
  574. * Validation rules (errors returned if violated):
  575. * - Cannot be empty or whitespace only
  576. * - Cannot start or end with dot (.)
  577. * - Cannot contain consecutive dots (..)
  578. * - Cannot contain spaces
  579. * - Cannot contain special characters: ~ ^ : ? * [ \
  580. * - Cannot contain @{
  581. * - Cannot start or end with forward slash (/)
  582. * - Cannot contain consecutive slashes (//)
  583. * - Cannot end with .lock
  584. * - Cannot contain ASCII control characters
  585. * Examples: 'feature/user-auth', 'bugfix/login-error', 'refactor/db-optimization'
  586. *
  587. * @param {boolean} createBranch - (Optional) Create a new Git branch after successful agent completion.
  588. * Default: false (or true if branchName is provided)
  589. * Behavior:
  590. * - Creates branch locally and pushes to remote
  591. * - If branch exists locally: Checks out existing branch (no error)
  592. * - If branch exists on remote: Uses existing branch (no error)
  593. * - Branch name: Custom (if branchName provided) or auto-generated from message
  594. * - Requires either githubUrl OR projectPath with GitHub remote
  595. *
  596. * @param {boolean} createPR - (Optional) Create a GitHub Pull Request after successful completion.
  597. * Default: false
  598. * Behavior:
  599. * - PR title: First commit message (or fallback to message parameter)
  600. * - PR description: Auto-generated from all commit messages
  601. * - Base branch: Always 'main' (currently hardcoded)
  602. * - If PR already exists: GitHub returns error with details
  603. * - Requires either githubUrl OR projectPath with GitHub remote
  604. *
  605. * ================================================================================================
  606. * PATH HANDLING BEHAVIOR
  607. * ================================================================================================
  608. *
  609. * Scenario 1: Only githubUrl provided
  610. * Input: { githubUrl: "https://github.com/owner/repo" }
  611. * Cleanup: Yes (if cleanup=true)
  612. *
  613. * Scenario 2: Only projectPath provided
  614. * Input: { projectPath: "/home/user/my-project" }
  615. * Action: Uses existing project at specified path
  616. * Validation: Path must exist and be accessible
  617. * Cleanup: No (never cleanup existing projects)
  618. *
  619. * Scenario 3: Both githubUrl and projectPath provided
  620. * Input: { githubUrl: "https://github.com/owner/repo", projectPath: "/custom/path" }
  621. * Action: Clones githubUrl to projectPath location
  622. * Validation:
  623. * - If projectPath exists with git repo:
  624. * - Compares remote URL with githubUrl
  625. * - If URLs match: Reuses existing repo
  626. * - If URLs differ: Returns error
  627. * Cleanup: Yes (if cleanup=true)
  628. *
  629. * ================================================================================================
  630. * GITHUB BRANCH/PR CREATION REQUIREMENTS
  631. * ================================================================================================
  632. *
  633. * For createBranch or createPR to work, one of the following must be true:
  634. *
  635. * Option A: githubUrl provided
  636. * - Repository URL directly specified
  637. * - Works with both cloning and existing paths
  638. *
  639. * Option B: projectPath with GitHub remote
  640. * - Project must be a Git repository
  641. * - Must have 'origin' remote configured
  642. * - Remote URL must point to github.com
  643. * - System auto-detects GitHub URL via: git remote get-url origin
  644. *
  645. * Additional Requirements:
  646. * - Valid GitHub token (from settings or githubToken parameter)
  647. * - Token must have 'repo' scope for private repos
  648. * - Project must have commits (for PR creation)
  649. *
  650. * ================================================================================================
  651. * VALIDATION & ERROR HANDLING
  652. * ================================================================================================
  653. *
  654. * Input Validations (400 Bad Request):
  655. * - Either githubUrl OR projectPath must be provided (not neither)
  656. * - message must be non-empty string
  657. * - createBranch/createPR requires githubUrl OR projectPath (not neither)
  658. * - branchName must pass Git naming rules (if provided)
  659. *
  660. * Runtime Validations (500 Internal Server Error or specific error in response):
  661. * - projectPath must exist (if used alone)
  662. * - GitHub URL format must be valid
  663. * - Git remote URL must include github.com (for projectPath + branch/PR)
  664. * - GitHub token must be available (for private repos and branch/PR)
  665. * - Directory conflicts handled (existing path with different repo)
  666. *
  667. * Branch Name Validation Errors (returned in response, not HTTP error):
  668. * Invalid names return: { branch: { error: "Invalid branch name: <reason>" } }
  669. * Examples:
  670. * - "my branch" → "Branch name cannot contain spaces"
  671. * - ".feature" → "Branch name cannot start with a dot"
  672. * - "feature.lock" → "Branch name cannot end with .lock"
  673. *
  674. * ================================================================================================
  675. * RESPONSE FORMATS
  676. * ================================================================================================
  677. *
  678. * Streaming Response (stream=true):
  679. * Content-Type: text/event-stream
  680. * Events:
  681. * - { type: "status", message: "...", projectPath: "..." }
  682. * - { type: "github-branch", branch: { name: "...", url: "..." } }
  683. * - { type: "github-pr", pullRequest: { number: 42, url: "..." } }
  684. * - { type: "github-error", error: "..." }
  685. * - { type: "done" }
  686. *
  687. * Non-Streaming Response (stream=false):
  688. * Content-Type: application/json
  689. * {
  690. * success: true,
  691. * sessionId: "session-123",
  692. * messages: [...], // Assistant messages only (filtered)
  693. * tokens: {
  694. * inputTokens: 150,
  695. * outputTokens: 50,
  696. * cacheReadTokens: 0,
  697. * cacheCreationTokens: 0,
  698. * totalTokens: 200
  699. * },
  700. * projectPath: "/path/to/project",
  701. * branch: { // Only if createBranch=true
  702. * name: "feature/xyz",
  703. * url: "https://github.com/owner/repo/tree/feature/xyz"
  704. * } | { error: "..." },
  705. * pullRequest: { // Only if createPR=true
  706. * number: 42,
  707. * url: "https://github.com/owner/repo/pull/42"
  708. * } | { error: "..." }
  709. * }
  710. *
  711. * Error Response:
  712. * HTTP Status: 400, 401, 500
  713. * Content-Type: application/json
  714. * { success: false, error: "Error description" }
  715. *
  716. * ================================================================================================
  717. * EXAMPLES
  718. * ================================================================================================
  719. *
  720. * Example 1: Clone and process with auto-cleanup
  721. * POST /api/agent
  722. * { "githubUrl": "https://github.com/user/repo", "message": "Fix bug" }
  723. *
  724. * Example 2: Use existing project with custom branch and PR
  725. * POST /api/agent
  726. * {
  727. * "projectPath": "/home/user/project",
  728. * "message": "Add feature",
  729. * "branchName": "feature/new-feature",
  730. * "createPR": true
  731. * }
  732. *
  733. * Example 3: Clone to specific path with auto-generated branch
  734. * POST /api/agent
  735. * {
  736. * "githubUrl": "https://github.com/user/repo",
  737. * "projectPath": "/tmp/work",
  738. * "message": "Refactor code",
  739. * "createBranch": true,
  740. * "cleanup": false
  741. * }
  742. */
  743. router.post('/', validateExternalApiKey, async (req, res) => {
  744. const { githubUrl, projectPath, message, provider = 'pilotdeck', model, githubToken, branchName, sessionId } = req.body;
  745. // Parse stream and cleanup as booleans (handle string "true"/"false" from curl)
  746. const stream = req.body.stream === undefined ? true : (req.body.stream === true || req.body.stream === 'true');
  747. const cleanup = req.body.cleanup === undefined ? true : (req.body.cleanup === true || req.body.cleanup === 'true');
  748. // If branchName is provided, automatically enable createBranch
  749. const createBranch = branchName ? true : (req.body.createBranch === true || req.body.createBranch === 'true');
  750. const createPR = req.body.createPR === true || req.body.createPR === 'true';
  751. // Validate inputs
  752. if (!githubUrl && !projectPath) {
  753. return res.status(400).json({ error: 'Either githubUrl or projectPath is required' });
  754. }
  755. if (!message || !message.trim()) {
  756. return res.status(400).json({ error: 'message is required' });
  757. }
  758. // After the PilotDeck-only migration any incoming `provider` is just a
  759. // label — every request is routed through `src/gateway`. We accept the
  760. // legacy values plus the new `pilotdeck` alias for forward compatibility.
  761. if (!['claude', 'cursor', 'codex', 'gemini', 'pilotdeck'].includes(provider)) {
  762. return res.status(400).json({ error: 'provider must be "claude", "cursor", "codex", "gemini", or "pilotdeck"' });
  763. }
  764. // Validate GitHub branch/PR creation requirements
  765. // Allow branch/PR creation with projectPath as long as it has a GitHub remote
  766. if ((createBranch || createPR) && !githubUrl && !projectPath) {
  767. return res.status(400).json({ error: 'createBranch and createPR require either githubUrl or projectPath with a GitHub remote' });
  768. }
  769. let finalProjectPath = null;
  770. let writer = null;
  771. try {
  772. // Determine the final project path
  773. if (githubUrl) {
  774. // Clone repository (to projectPath if provided, otherwise generate path)
  775. const tokenToUse = githubToken || githubTokensDb.getActiveGithubToken(req.user.id);
  776. let targetPath;
  777. if (projectPath) {
  778. targetPath = projectPath;
  779. } else {
  780. // Generate a unique path for cloning
  781. const repoHash = crypto.createHash('md5').update(githubUrl + Date.now()).digest('hex');
  782. targetPath = path.join(os.homedir(), '.pilotdeck', 'external-projects', repoHash);
  783. }
  784. finalProjectPath = await cloneGitHubRepo(githubUrl.trim(), tokenToUse, targetPath);
  785. } else {
  786. // Use existing project path
  787. finalProjectPath = path.resolve(projectPath);
  788. // Verify the path exists
  789. try {
  790. await fs.access(finalProjectPath);
  791. } catch (error) {
  792. throw new Error(`Project path does not exist: ${finalProjectPath}`);
  793. }
  794. }
  795. // Register the project (or use existing registration)
  796. let project;
  797. try {
  798. project = await addProjectManually(finalProjectPath);
  799. console.log('📦 Project registered:', project);
  800. } catch (error) {
  801. // If project already exists, that's fine - continue with the existing registration
  802. if (error.message && error.message.includes('Project already configured')) {
  803. console.log('📦 Using existing project registration for:', finalProjectPath);
  804. project = { path: finalProjectPath };
  805. } else {
  806. throw error;
  807. }
  808. }
  809. // Set up writer based on streaming mode
  810. if (stream) {
  811. // Set up SSE headers for streaming
  812. res.setHeader('Content-Type', 'text/event-stream');
  813. res.setHeader('Cache-Control', 'no-cache');
  814. res.setHeader('Connection', 'keep-alive');
  815. res.setHeader('X-Accel-Buffering', 'no'); // Disable nginx buffering
  816. writer = new SSEStreamWriter(res, req.user.id);
  817. // Send initial status
  818. writer.send({
  819. type: 'status',
  820. message: githubUrl ? 'Repository cloned and session started' : 'Session started',
  821. projectPath: finalProjectPath
  822. });
  823. } else {
  824. // Non-streaming mode: collect messages
  825. writer = new ResponseCollector(req.user.id);
  826. // Collect initial status message
  827. writer.send({
  828. type: 'status',
  829. message: githubUrl ? 'Repository cloned and session started' : 'Session started',
  830. projectPath: finalProjectPath
  831. });
  832. }
  833. console.log(`🛫 Starting PilotDeck gateway session (provider=${provider})`);
  834. await runChatViaGateway(
  835. message.trim(),
  836. {
  837. projectPath: finalProjectPath,
  838. cwd: finalProjectPath,
  839. sessionId: sessionId || null,
  840. model,
  841. permissionMode: 'bypassPermissions',
  842. },
  843. writer,
  844. provider,
  845. );
  846. // Handle GitHub branch and PR creation after successful agent completion
  847. let branchInfo = null;
  848. let prInfo = null;
  849. if (createBranch || createPR) {
  850. try {
  851. console.log('🔄 Starting GitHub branch/PR creation workflow...');
  852. // Get GitHub token
  853. const tokenToUse = githubToken || githubTokensDb.getActiveGithubToken(req.user.id);
  854. if (!tokenToUse) {
  855. throw new Error('GitHub token required for branch/PR creation. Please configure a GitHub token in settings.');
  856. }
  857. // Initialize Octokit
  858. const octokit = new Octokit({ auth: tokenToUse });
  859. // Get GitHub URL - either from parameter or from git remote
  860. let repoUrl = githubUrl;
  861. if (!repoUrl) {
  862. console.log('🔍 Getting GitHub URL from git remote...');
  863. try {
  864. repoUrl = await getGitRemoteUrl(finalProjectPath);
  865. if (!repoUrl.includes('github.com')) {
  866. throw new Error('Project does not have a GitHub remote configured');
  867. }
  868. console.log(`✅ Found GitHub remote: ${repoUrl}`);
  869. } catch (error) {
  870. throw new Error(`Failed to get GitHub remote URL: ${error.message}`);
  871. }
  872. }
  873. // Parse GitHub URL to get owner and repo
  874. const { owner, repo } = parseGitHubUrl(repoUrl);
  875. console.log(`📦 Repository: ${owner}/${repo}`);
  876. // Use provided branch name or auto-generate from message
  877. const finalBranchName = branchName || autogenerateBranchName(message);
  878. if (branchName) {
  879. console.log(`🌿 Using provided branch name: ${finalBranchName}`);
  880. // Validate custom branch name
  881. const validation = validateBranchName(finalBranchName);
  882. if (!validation.valid) {
  883. throw new Error(`Invalid branch name: ${validation.error}`);
  884. }
  885. } else {
  886. console.log(`🌿 Auto-generated branch name: ${finalBranchName}`);
  887. }
  888. if (createBranch) {
  889. // Create and checkout the new branch locally
  890. console.log('🔄 Creating local branch...');
  891. const checkoutProcess = spawn('git', ['checkout', '-b', finalBranchName], {
  892. cwd: finalProjectPath,
  893. stdio: 'pipe'
  894. });
  895. await new Promise((resolve, reject) => {
  896. let stderr = '';
  897. checkoutProcess.stderr.on('data', (data) => { stderr += data.toString(); });
  898. checkoutProcess.on('close', (code) => {
  899. if (code === 0) {
  900. console.log(`✅ Created and checked out local branch '${finalBranchName}'`);
  901. resolve();
  902. } else {
  903. // Branch might already exist locally, try to checkout
  904. if (stderr.includes('already exists')) {
  905. console.log(`ℹ️ Branch '${finalBranchName}' already exists locally, checking out...`);
  906. const checkoutExisting = spawn('git', ['checkout', finalBranchName], {
  907. cwd: finalProjectPath,
  908. stdio: 'pipe'
  909. });
  910. checkoutExisting.on('close', (checkoutCode) => {
  911. if (checkoutCode === 0) {
  912. console.log(`✅ Checked out existing branch '${finalBranchName}'`);
  913. resolve();
  914. } else {
  915. reject(new Error(`Failed to checkout existing branch: ${stderr}`));
  916. }
  917. });
  918. } else {
  919. reject(new Error(`Failed to create branch: ${stderr}`));
  920. }
  921. }
  922. });
  923. });
  924. // Push the branch to remote
  925. console.log('🔄 Pushing branch to remote...');
  926. const pushProcess = spawn('git', ['push', '-u', 'origin', finalBranchName], {
  927. cwd: finalProjectPath,
  928. stdio: 'pipe'
  929. });
  930. await new Promise((resolve, reject) => {
  931. let stderr = '';
  932. let stdout = '';
  933. pushProcess.stdout.on('data', (data) => { stdout += data.toString(); });
  934. pushProcess.stderr.on('data', (data) => { stderr += data.toString(); });
  935. pushProcess.on('close', (code) => {
  936. if (code === 0) {
  937. console.log(`✅ Pushed branch '${finalBranchName}' to remote`);
  938. resolve();
  939. } else {
  940. // Check if branch exists on remote but has different commits
  941. if (stderr.includes('already exists') || stderr.includes('up-to-date')) {
  942. console.log(`ℹ️ Branch '${finalBranchName}' already exists on remote, using existing branch`);
  943. resolve();
  944. } else {
  945. reject(new Error(`Failed to push branch: ${stderr}`));
  946. }
  947. }
  948. });
  949. });
  950. branchInfo = {
  951. name: finalBranchName,
  952. url: `https://github.com/${owner}/${repo}/tree/${finalBranchName}`
  953. };
  954. }
  955. if (createPR) {
  956. // Get commit messages to generate PR description
  957. console.log('🔄 Generating PR title and description...');
  958. const commitMessages = await getCommitMessages(finalProjectPath, 5);
  959. // Use the first commit message as the PR title, or fallback to the agent message
  960. const prTitle = commitMessages.length > 0 ? commitMessages[0] : message;
  961. // Generate PR body from commit messages
  962. let prBody = '## Changes\n\n';
  963. if (commitMessages.length > 0) {
  964. prBody += commitMessages.map(msg => `- ${msg}`).join('\n');
  965. } else {
  966. prBody += `Agent task: ${message}`;
  967. }
  968. prBody += '\n\n---\n*This pull request was automatically created by PilotDeck Agent.*';
  969. console.log(`📝 PR Title: ${prTitle}`);
  970. // Create the pull request
  971. console.log('🔄 Creating pull request...');
  972. prInfo = await createGitHubPR(octokit, owner, repo, finalBranchName, prTitle, prBody, 'main');
  973. }
  974. // Send branch/PR info in response
  975. if (stream) {
  976. if (branchInfo) {
  977. writer.send({
  978. type: 'github-branch',
  979. branch: branchInfo
  980. });
  981. }
  982. if (prInfo) {
  983. writer.send({
  984. type: 'github-pr',
  985. pullRequest: prInfo
  986. });
  987. }
  988. }
  989. } catch (error) {
  990. console.error('❌ GitHub branch/PR creation error:', error);
  991. // Send error but don't fail the entire request
  992. if (stream) {
  993. writer.send({
  994. type: 'github-error',
  995. error: error.message
  996. });
  997. }
  998. // Store error info for non-streaming response
  999. if (!stream) {
  1000. branchInfo = { error: error.message };
  1001. prInfo = { error: error.message };
  1002. }
  1003. }
  1004. }
  1005. // Handle response based on streaming mode
  1006. if (stream) {
  1007. // Streaming mode: end the SSE stream
  1008. writer.end();
  1009. } else {
  1010. // Non-streaming mode: send filtered messages and token summary as JSON
  1011. const assistantMessages = writer.getAssistantMessages();
  1012. const tokenSummary = writer.getTotalTokens();
  1013. const response = {
  1014. success: true,
  1015. sessionId: writer.getSessionId(),
  1016. messages: assistantMessages,
  1017. tokens: tokenSummary,
  1018. projectPath: finalProjectPath
  1019. };
  1020. // Add branch/PR info if created
  1021. if (branchInfo) {
  1022. response.branch = branchInfo;
  1023. }
  1024. if (prInfo) {
  1025. response.pullRequest = prInfo;
  1026. }
  1027. res.json(response);
  1028. }
  1029. // Clean up if requested
  1030. if (cleanup && githubUrl) {
  1031. // Only cleanup if we cloned a repo (not for existing project paths)
  1032. const sessionIdForCleanup = writer.getSessionId();
  1033. setTimeout(() => {
  1034. cleanupProject(finalProjectPath, sessionIdForCleanup);
  1035. }, 5000);
  1036. }
  1037. } catch (error) {
  1038. console.error('❌ External session error:', error);
  1039. // Clean up on error
  1040. if (finalProjectPath && cleanup && githubUrl) {
  1041. const sessionIdForCleanup = writer ? writer.getSessionId() : null;
  1042. cleanupProject(finalProjectPath, sessionIdForCleanup);
  1043. }
  1044. if (stream) {
  1045. // For streaming, send error event and stop
  1046. if (!writer) {
  1047. // Set up SSE headers if not already done
  1048. res.setHeader('Content-Type', 'text/event-stream');
  1049. res.setHeader('Cache-Control', 'no-cache');
  1050. res.setHeader('Connection', 'keep-alive');
  1051. res.setHeader('X-Accel-Buffering', 'no');
  1052. writer = new SSEStreamWriter(res, req.user.id);
  1053. }
  1054. if (!res.writableEnded) {
  1055. writer.send({
  1056. type: 'error',
  1057. error: error.message,
  1058. message: `Failed: ${error.message}`
  1059. });
  1060. writer.end();
  1061. }
  1062. } else if (!res.headersSent) {
  1063. res.status(500).json({
  1064. success: false,
  1065. error: error.message
  1066. });
  1067. }
  1068. }
  1069. });
  1070. export default router;