-
Notifications
You must be signed in to change notification settings - Fork 597
feat: add external terminal support with cross-platform detection #565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
16f2449
af69ca7
099ebaf
03103fd
ce4b9b6
502361f
111eb24
9529afb
5d68e75
c42b786
36bdf8c
306f6bb
df88857
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| /** | ||
| * Terminal endpoints for opening worktree directories in terminals | ||
| * | ||
| * POST /open-in-terminal - Open in system default terminal (integrated) | ||
| * GET /available-terminals - List all available external terminals | ||
| * GET /default-terminal - Get the default external terminal | ||
| * POST /refresh-terminals - Clear terminal cache and re-detect | ||
| * POST /open-in-external-terminal - Open a directory in an external terminal | ||
| */ | ||
|
|
||
| import type { Request, Response } from 'express'; | ||
| import { isAbsolute } from 'path'; | ||
| import { | ||
| openInTerminal, | ||
| clearTerminalCache, | ||
| detectAllTerminals, | ||
| detectDefaultTerminal, | ||
| openInExternalTerminal, | ||
| } from '@automaker/platform'; | ||
| import { createLogger } from '@automaker/utils'; | ||
| import { getErrorMessage, logError } from '../common.js'; | ||
|
|
||
| const logger = createLogger('open-in-terminal'); | ||
|
|
||
| /** | ||
| * Handler to open in system default terminal (integrated terminal behavior) | ||
| */ | ||
| export function createOpenInTerminalHandler() { | ||
| return async (req: Request, res: Response): Promise<void> => { | ||
| try { | ||
| const { worktreePath } = req.body as { | ||
| worktreePath: string; | ||
| }; | ||
|
|
||
| if (!worktreePath || typeof worktreePath !== 'string') { | ||
| res.status(400).json({ | ||
| success: false, | ||
| error: 'worktreePath required and must be a string', | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| // Security: Validate that worktreePath is an absolute path | ||
| if (!isAbsolute(worktreePath)) { | ||
| res.status(400).json({ | ||
| success: false, | ||
| error: 'worktreePath must be an absolute path', | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| // Use the platform utility to open in terminal | ||
| const result = await openInTerminal(worktreePath); | ||
| res.json({ | ||
| success: true, | ||
| result: { | ||
| message: `Opened terminal in ${worktreePath}`, | ||
| terminalName: result.terminalName, | ||
| }, | ||
| }); | ||
| } catch (error) { | ||
| logError(error, 'Open in terminal failed'); | ||
| res.status(500).json({ success: false, error: getErrorMessage(error) }); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Handler to get all available external terminals | ||
| */ | ||
| export function createGetAvailableTerminalsHandler() { | ||
| return async (_req: Request, res: Response): Promise<void> => { | ||
| try { | ||
| const terminals = await detectAllTerminals(); | ||
| res.json({ | ||
| success: true, | ||
| result: { | ||
| terminals, | ||
| }, | ||
| }); | ||
| } catch (error) { | ||
| logError(error, 'Get available terminals failed'); | ||
| res.status(500).json({ success: false, error: getErrorMessage(error) }); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Handler to get the default external terminal | ||
| */ | ||
| export function createGetDefaultTerminalHandler() { | ||
| return async (_req: Request, res: Response): Promise<void> => { | ||
| try { | ||
| const terminal = await detectDefaultTerminal(); | ||
| res.json({ | ||
| success: true, | ||
| result: terminal | ||
| ? { | ||
| terminalId: terminal.id, | ||
| terminalName: terminal.name, | ||
| terminalCommand: terminal.command, | ||
| } | ||
| : null, | ||
| }); | ||
| } catch (error) { | ||
| logError(error, 'Get default terminal failed'); | ||
| res.status(500).json({ success: false, error: getErrorMessage(error) }); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Handler to refresh the terminal cache and re-detect available terminals | ||
| * Useful when the user has installed/uninstalled terminals | ||
| */ | ||
| export function createRefreshTerminalsHandler() { | ||
| return async (_req: Request, res: Response): Promise<void> => { | ||
| try { | ||
| // Clear the cache | ||
| clearTerminalCache(); | ||
|
|
||
| // Re-detect terminals (this will repopulate the cache) | ||
| const terminals = await detectAllTerminals(); | ||
|
|
||
| logger.info(`Terminal cache refreshed, found ${terminals.length} terminals`); | ||
|
|
||
| res.json({ | ||
| success: true, | ||
| result: { | ||
| terminals, | ||
| message: `Found ${terminals.length} available external terminals`, | ||
| }, | ||
| }); | ||
| } catch (error) { | ||
| logError(error, 'Refresh terminals failed'); | ||
| res.status(500).json({ success: false, error: getErrorMessage(error) }); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Handler to open a directory in an external terminal | ||
| */ | ||
| export function createOpenInExternalTerminalHandler() { | ||
| return async (req: Request, res: Response): Promise<void> => { | ||
| try { | ||
| const { worktreePath, terminalId } = req.body as { | ||
| worktreePath: string; | ||
| terminalId?: string; | ||
| }; | ||
|
|
||
| if (!worktreePath || typeof worktreePath !== 'string') { | ||
| res.status(400).json({ | ||
| success: false, | ||
| error: 'worktreePath required and must be a string', | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| if (!isAbsolute(worktreePath)) { | ||
| res.status(400).json({ | ||
| success: false, | ||
| error: 'worktreePath must be an absolute path', | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const result = await openInExternalTerminal(worktreePath, terminalId); | ||
| res.json({ | ||
| success: true, | ||
| result: { | ||
| message: `Opened ${worktreePath} in ${result.terminalName}`, | ||
| terminalName: result.terminalName, | ||
| }, | ||
| }); | ||
|
Comment on lines
+144
to
+175
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
# Search for openInExternalTerminal function in platform terminal.ts
rg -n -C5 "openInExternalTerminal" libs/platform/src/terminal.tsRepository: AutoMaker-Org/automaker Length of output: 543 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Get more context on the openInExternalTerminal implementation
rg -n -A 30 "export async function openInExternalTerminal" libs/platform/src/terminal.tsRepository: AutoMaker-Org/automaker Length of output: 996 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Check the findTerminalById function to see how it handles the terminalId parameter
rg -n -B2 -A15 "function findTerminalById|export.*findTerminalById" libs/platform/src/terminal.tsRepository: AutoMaker-Org/automaker Length of output: 904 Validate If a client sends a non-string 🛠️ Proposed fix const { worktreePath, terminalId } = req.body as {
worktreePath: string;
terminalId?: string;
};
if (!worktreePath || typeof worktreePath !== 'string') {
res.status(400).json({
success: false,
error: 'worktreePath required and must be a string',
});
return;
}
+ if (terminalId !== undefined && typeof terminalId !== 'string') {
+ res.status(400).json({
+ success: false,
+ error: 'terminalId must be a string',
+ });
+ return;
+ }
+
if (!isAbsolute(worktreePath)) {
res.status(400).json({
success: false,
error: 'worktreePath must be an absolute path',
});
return;
}🤖 Prompt for AI Agents |
||
| } catch (error) { | ||
| logError(error, 'Open in external terminal failed'); | ||
| res.status(500).json({ success: false, error: getErrorMessage(error) }); | ||
| } | ||
| }; | ||
|
stefandevo marked this conversation as resolved.
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.